2 * Copyright (C) 2009-2011 New Dream Network
4 * This file is part of Hypertable.
6 * Hypertable is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or any later version.
11 * Hypertable is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
20 * Gregory Farnum <gfarnum@gmail.com>
21 * Colin McCabe <cmccabe@alumni.cmu.edu>
24 #include "Common/Compat.h"
26 #include "CephBroker.h"
27 #include "Common/Error.h"
28 #include "Common/FileUtils.h"
29 #include "Common/Filesystem.h"
30 #include "Common/System.h"
32 #include <cephfs/libcephfs.h>
38 #include <sys/types.h>
42 using namespace Hypertable;
44 std::atomic<int> CephBroker::ms_next_fd{0};
46 /* A thread-safe version of strerror */
47 static std::string cpp_strerror(int err)
52 std::ostringstream oss;
53 oss << strerror_r(err, buf, sizeof(buf));
57 OpenFileDataCeph::OpenFileDataCeph(struct ceph_mount_info *cmount_, const String& fname,
59 : cmount(cmount_), fd(_fd), flags(_flags), filename(fname)
63 OpenFileDataCeph::~OpenFileDataCeph() {
64 ceph_close(cmount, fd);
67 CephBroker::CephBroker(PropertiesPtr& cfg)
71 String id(cfg->get_str("CephBroker.Id"));
72 m_verbose = cfg->get_bool("Hypertable.Verbose");
73 m_root_dir = cfg->get_str("CephBroker.RootDir");
74 String mon_addr(cfg->get_str("CephBroker.MonAddr"));
76 HT_INFO("Calling ceph_create");
77 ret = ceph_create(&cmount, id.empty() ? NULL : id.c_str());
79 throw Hypertable::Exception(ret, "ceph_create failed");
81 ret = ceph_conf_set(cmount, "mon_host", mon_addr.c_str());
83 ceph_shutdown(cmount);
84 throw Hypertable::Exception(ret, "ceph_conf_set(mon_addr) failed");
87 // For Ceph debugging, uncomment these lines
88 //ceph_conf_set(cmount, "debug_client", "1");
89 //ceph_conf_set(cmount, "debug_ms", "1");
91 HT_INFO("Calling ceph_mount");
92 ret = ceph_mount(cmount, m_root_dir.empty() ? NULL : m_root_dir.c_str());
94 ceph_shutdown(cmount);
95 throw Hypertable::Exception(ret, "ceph_mount failed");
97 HT_INFO("Mounted Ceph filesystem.");
100 CephBroker::~CephBroker()
102 ceph_shutdown(cmount);
106 void CephBroker::open(ResponseCallbackOpen *cb, const char *fname,
107 uint32_t flags, uint32_t bufsz) {
110 HT_DEBUGF("open file='%s' bufsz=%d", fname, bufsz);
112 make_abs_path(fname, abspath);
114 fd = atomic_inc_return(&ms_next_fd);
116 if ((ceph_fd = ceph_open(cmount, abspath.c_str(), O_RDONLY, 0)) < 0) {
117 report_error(cb, -ceph_fd);
120 HT_INFOF("open (%s) fd=%d ceph_fd=%d", fname, fd, ceph_fd);
123 struct sockaddr_in addr;
124 OpenFileDataCephPtr fdata(new OpenFileDataCeph(cmount, abspath, ceph_fd, O_RDONLY));
126 cb->get_address(addr);
128 m_open_file_map.create(fd, addr, fdata);
134 void CephBroker::create(ResponseCallbackOpen *cb, const char *fname, uint32_t flags,
135 int32_t bufsz, int16_t replication, int64_t blksz){
140 make_abs_path(fname, abspath);
141 HT_DEBUGF("create file='%s' flags=%u bufsz=%d replication=%d blksz=%lld",
142 fname, flags, bufsz, (int)replication, (Lld)blksz);
144 fd = atomic_inc_return(&ms_next_fd);
146 if (flags & Filesystem::OPEN_FLAG_OVERWRITE)
147 oflags = O_WRONLY | O_CREAT | O_TRUNC;
149 oflags = O_WRONLY | O_CREAT | O_APPEND;
151 //make sure the directories in the path exist
152 String directory = abspath.substr(0, abspath.rfind('/'));
154 HT_INFOF("Calling mkdirs on %s", directory.c_str());
155 if((r=ceph_mkdirs(cmount, directory.c_str(), 0644)) < 0 && r!=-EEXIST) {
156 HT_ERRORF("create failed on mkdirs: dname='%s' - %d", directory.c_str(), -r);
157 report_error(cb, -r);
162 if ((ceph_fd = ceph_open(cmount, abspath.c_str(), oflags, 0644)) < 0) {
163 std::string errs(cpp_strerror(-ceph_fd));
164 HT_ERRORF("open failed: file=%s - %s", abspath.c_str(), errs.c_str());
165 report_error(cb, ceph_fd);
169 HT_INFOF("create %s = %d", fname, ceph_fd);
172 struct sockaddr_in addr;
173 OpenFileDataCephPtr fdata (new OpenFileDataCeph(cmount, fname, ceph_fd, O_WRONLY));
175 cb->get_address(addr);
177 m_open_file_map.create(fd, addr, fdata);
183 void CephBroker::close(ResponseCallback *cb, uint32_t fd) {
185 HT_INFOF("close fd=%d", fd);
187 OpenFileDataCephPtr fdata;
188 m_open_file_map.get(fd, fdata);
189 m_open_file_map.remove(fd);
193 void CephBroker::read(ResponseCallbackRead *cb, uint32_t fd, uint32_t amount) {
194 OpenFileDataCephPtr fdata;
197 StaticBuffer buf(new uint8_t [amount], amount);
199 HT_DEBUGF("read fd=%d amount = %d", fd, amount);
201 if (!m_open_file_map.get(fd, fdata)) {
203 sprintf(errbuf, "%d", fd);
204 cb->error(Error::DFSBROKER_BAD_FILE_HANDLE, errbuf);
205 HT_ERRORF("bad file handle: %d", fd);
209 if ((offset = ceph_lseek(cmount, fdata->fd, 0, SEEK_CUR)) < 0) {
210 std::string errs(cpp_strerror((int)-offset));
211 HT_ERRORF("lseek failed: fd=%d ceph_fd=%d offset=0 SEEK_CUR - %s",
212 fd, fdata->fd, errs.c_str());
213 report_error(cb, offset);
217 if ((nread = ceph_read(cmount, fdata->fd, (char *)buf.base, amount, 0)) < 0 ) {
218 HT_ERRORF("read failed: fd=%d ceph_fd=%d amount=%d", fd, fdata->fd, amount);
219 report_error(cb, -nread);
224 cb->response(offset, buf);
227 void CephBroker::append(ResponseCallbackAppend *cb, uint32_t fd,
228 uint32_t amount, const void *data, bool sync)
230 OpenFileDataCephPtr fdata;
234 HT_DEBUG_OUT << "append fd="<< fd <<" amount="<< amount <<" data='"
235 << format_bytes(20, data, amount) <<" sync="<< sync << HT_END;
237 if (!m_open_file_map.get(fd, fdata)) {
239 sprintf(errbuf, "%d", fd);
240 cb->error(Error::DFSBROKER_BAD_FILE_HANDLE, errbuf);
244 if ((offset = (uint64_t)ceph_lseek(cmount, fdata->fd, 0, SEEK_CUR)) < 0) {
245 std::string errs(cpp_strerror((int)-offset));
246 HT_ERRORF("lseek failed: fd=%d ceph_fd=%d offset=0 SEEK_CUR - %s", fd, fdata->fd,
248 report_error(cb, offset);
252 if ((nwritten = ceph_write(cmount, fdata->fd, (const char *)data, amount, 0)) < 0) {
253 std::string errs(cpp_strerror(nwritten));
254 HT_ERRORF("write failed: fd=%d ceph_fd=%d amount=%d - %s",
255 fd, fdata->fd, amount, errs.c_str());
256 report_error(cb, -nwritten);
261 if (sync && ((r = ceph_fsync(cmount, fdata->fd, true)) != 0)) {
262 std::string errs(cpp_strerror(errno));
263 HT_ERRORF("flush failed: fd=%d ceph_fd=%d - %s", fd, fdata->fd, errs.c_str());
268 cb->response(offset, nwritten);
271 void CephBroker::seek(ResponseCallback *cb, uint32_t fd, uint64_t offset) {
272 OpenFileDataCephPtr fdata;
274 HT_DEBUGF("seek fd=%lu offset=%llu", (Lu)fd, (Llu)offset);
276 if (!m_open_file_map.get(fd, fdata)) {
278 sprintf(errbuf, "%d", fd);
279 cb->error(Error::DFSBROKER_BAD_FILE_HANDLE, errbuf);
282 loff_t res = ceph_lseek(cmount, fdata->fd, offset, SEEK_SET);
284 std::string errs(cpp_strerror((int)res));
285 HT_ERRORF("lseek failed: fd=%d ceph_fd=%d offset=%llu - %s",
286 fd, fdata->fd, (Llu)offset, errs.c_str());
287 report_error(cb, offset);
294 void CephBroker::remove(ResponseCallback *cb, const char *fname) {
297 HT_DEBUGF("remove file='%s'", fname);
299 make_abs_path(fname, abspath);
302 if ((r = ceph_unlink(cmount, abspath.c_str())) < 0) {
303 std::string errs(cpp_strerror(r));
304 HT_ERRORF("unlink failed: file='%s' - %s", abspath.c_str(), errs.c_str());
311 void CephBroker::length(ResponseCallbackLength *cb, const char *fname, bool) {
313 struct ceph_statx stx;
315 HT_DEBUGF("length file='%s'", fname);
317 if ((r = ceph_statx(cmount, fname, &stx, CEPH_STATX_SIZE, AT_SYMLINK_NOFOLLOW)) < 0) {
319 make_abs_path(fname, abspath);
320 std::string errs(cpp_strerror(r));
321 HT_ERRORF("length (stat) failed: file='%s' - %s", abspath.c_str(), errs.c_str());
322 report_error(cb,- r);
325 cb->response(stx.stx_size);
328 void CephBroker::pread(ResponseCallbackRead *cb, uint32_t fd, uint64_t offset,
329 uint32_t amount, bool) {
330 OpenFileDataCephPtr fdata;
332 StaticBuffer buf(new uint8_t [amount], amount);
334 HT_DEBUGF("pread fd=%d offset=%llu amount=%d", fd, (Llu)offset, amount);
336 if (!m_open_file_map.get(fd, fdata)) {
338 sprintf(errbuf, "%d", fd);
339 cb->error(Error::DFSBROKER_BAD_FILE_HANDLE, errbuf);
343 if ((nread = ceph_read(cmount, fdata->fd, (char *)buf.base, amount, offset)) < 0) {
344 std::string errs(cpp_strerror(nread));
345 HT_ERRORF("pread failed: fd=%d ceph_fd=%d amount=%d offset=%llu - %s", fd, fdata->fd,
346 amount, (Llu)offset, errs.c_str());
347 report_error(cb, nread);
353 cb->response(offset, buf);
356 void CephBroker::mkdirs(ResponseCallback *cb, const char *dname) {
359 HT_DEBUGF("mkdirs dir='%s'", dname);
361 make_abs_path(dname, absdir);
363 if((r=ceph_mkdirs(cmount, absdir.c_str(), 0644)) < 0 && r!=-EEXIST) {
364 HT_ERRORF("mkdirs failed: dname='%s' - %d", absdir.c_str(), -r);
365 report_error(cb, -r);
371 void CephBroker::rmdir(ResponseCallback *cb, const char *dname) {
375 make_abs_path(dname, absdir);
376 if((r = rmdir_recursive(absdir.c_str())) < 0) {
377 HT_ERRORF("failed to remove dir %s, got error %d", absdir.c_str(), r);
378 report_error(cb, -r);
384 int CephBroker::rmdir_recursive(const char *directory) {
385 struct ceph_dir_result *dirp;
387 struct ceph_statx stx;
389 if ((r = ceph_opendir(cmount, directory, &dirp)) < 0)
390 return r; //failed to open
391 while ((r = ceph_readdirplus_r(cmount, dirp, &de, &stx, CEPH_STATX_INO, AT_NO_ATTR_SYNC, NULL)) > 0) {
392 String new_dir = de.d_name;
393 if(!(new_dir.compare(".")==0 || new_dir.compare("..")==0)) {
396 new_dir += de.d_name;
397 if (S_ISDIR(stx.stx_mode)) { //it's a dir, clear it out...
398 if((r=rmdir_recursive(new_dir.c_str())) < 0) return r;
399 } else { //delete this file
400 if((r=ceph_unlink(cmount, new_dir.c_str())) < 0) return r;
404 if (r < 0) return r; //we got an error
405 if ((r = ceph_closedir(cmount, dirp)) < 0) return r;
406 return ceph_rmdir(cmount, directory);
409 void CephBroker::flush(ResponseCallback *cb, uint32_t fd) {
410 OpenFileDataCephPtr fdata;
412 HT_DEBUGF("flush fd=%d", fd);
414 if (!m_open_file_map.get(fd, fdata)) {
416 sprintf(errbuf, "%d", fd);
417 cb->error(Error::DFSBROKER_BAD_FILE_HANDLE, errbuf);
422 if ((r = ceph_fsync(cmount, fdata->fd, true)) != 0) {
423 std::string errs(cpp_strerror(r));
424 HT_ERRORF("flush failed: fd=%d ceph_fd=%d - %s", fd, fdata->fd, errs.c_str());
425 report_error(cb, -r);
432 void CephBroker::status(ResponseCallback *cb) {
434 /*perhaps a total cheat, but both the local and Kosmos brokers
435 included in Hypertable also do this. */
438 void CephBroker::shutdown(ResponseCallback *cb) {
439 m_open_file_map.remove_all();
444 void CephBroker::readdir(ResponseCallbackReaddir *cb, const char *dname) {
445 std::vector<String> listing;
448 HT_DEBUGF("Readdir dir='%s'", dname);
450 //get from ceph in a buffer
451 make_abs_path(dname, absdir);
453 struct ceph_dir_result *dirp;
454 ceph_opendir(cmount, absdir.c_str(), &dirp);
456 int buflen = 100; //good default?
457 char *buf = new char[buflen];
461 r = ceph_getdnames(cmount, dirp, buf, buflen);
462 if (r==-ERANGE) { //expand the buffer
465 buf = new char[buflen];
470 //if we make it here, we got at least one name, maybe more
472 while (bufpos<r) {//make new strings and add them to listing
473 ent = new String(buf+bufpos);
474 if (ent->compare(".") && ent->compare(".."))
475 listing.push_back(*ent);
476 bufpos+=ent->size()+1;
481 ceph_closedir(cmount, dirp);
483 if (r < 0) report_error(cb, -r); //Ceph shouldn't return r<0 on getdnames
484 //(except for ERANGE) so if it happens this is bad
485 cb->response(listing);
488 void CephBroker::exists(ResponseCallbackExists *cb, const char *fname) {
490 struct ceph_statx stx;
492 HT_DEBUGF("exists file='%s'", fname);
493 make_abs_path(fname, abspath);
494 cb->response(ceph_statx(cmount, abspath.c_str(), &stx, 0, AT_SYMLINK_NOFOLLOW) == 0);
497 void CephBroker::rename(ResponseCallback *cb, const char *src, const char *dst) {
502 make_abs_path(src, src_abs);
503 make_abs_path(dst, dest_abs);
504 if ((r = ceph_rename(cmount, src_abs.c_str(), dest_abs.c_str())) <0 ) {
511 void CephBroker::debug(ResponseCallback *cb, int32_t command,
512 StaticBuffer &serialized_parameters) {
513 HT_ERROR("debug commands not implemented!");
514 cb->error(Error::NOT_IMPLEMENTED, format("Debug commands not supported"));
517 void CephBroker::report_error(ResponseCallback *cb, int error) {
521 strerror_r(error, errbuf, 128);
523 cb->error(Error::DFSBROKER_IO_ERROR, errbuf);