1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #include <sys/types.h>
27 #include "common/errno.h"
28 #include "common/safe_io.h"
29 #include "include/types.h"
33 #include "common/config.h"
34 #include "include/assert.h"
35 #include "include/cephfs/ceph_statx.h"
39 #include <fuse_lowlevel.h>
41 #define dout_context g_ceph_context
43 #define FINO_INO(x) ((x) & ((1ull<<48)-1ull))
44 #define FINO_STAG(x) ((x) >> 48)
45 #define MAKE_FINO(i,s) ((i) | ((s) << 48))
48 #define MINORMASK ((1U << MINORBITS) - 1)
50 #define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
51 #define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))
52 #define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
54 static uint32_t new_encode_dev(dev_t dev)
56 unsigned major = MAJOR(dev);
57 unsigned minor = MINOR(dev);
58 return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
61 static dev_t new_decode_dev(uint32_t dev)
63 unsigned major = (dev & 0xfff00) >> 8;
64 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
65 return MKDEV(major, minor);
68 class CephFuse::Handle {
70 Handle(Client *c, int fd);
73 int init(int argc, const char *argv[]);
78 uint64_t fino_snap(uint64_t fino);
79 uint64_t make_fake_ino(inodeno_t ino, snapid_t snapid);
80 Inode * iget(fuse_ino_t fino);
87 struct fuse_session *se;
93 ceph::unordered_map<uint64_t,int> snap_stag_map;
94 ceph::unordered_map<int,uint64_t> stag_snap_map;
96 pthread_key_t fuse_req_key;
97 void set_fuse_req(fuse_req_t);
98 fuse_req_t get_fuse_req();
100 struct fuse_args args;
103 static int getgroups(fuse_req_t req, gid_t **sgids)
105 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
107 int c = fuse_req_getgroups(req, 0, NULL);
115 gid_t *gids = new (std::nothrow) gid_t[c];
119 c = fuse_req_getgroups(req, c, gids);
130 static int getgroups_cb(void *handle, gid_t **sgids)
132 CephFuse::Handle *cfuse = (CephFuse::Handle *) handle;
133 fuse_req_t req = cfuse->get_fuse_req();
134 return getgroups(req, sgids);
137 #define GET_GROUPS(perms, req) { \
138 if (g_conf->get_val<bool>("fuse_set_user_groups")) { \
139 gid_t *gids = NULL; \
140 int count = getgroups(req, &gids); \
141 perms.init_gids(gids, count); \
146 static CephFuse::Handle *fuse_ll_req_prepare(fuse_req_t req)
148 CephFuse::Handle *cfuse = (CephFuse::Handle *)fuse_req_userdata(req);
149 cfuse->set_fuse_req(req);
153 static void fuse_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
155 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
156 const struct fuse_ctx *ctx = fuse_req_ctx(req);
157 struct fuse_entry_param fe;
158 Inode *i2, *i1 = cfuse->iget(parent); // see below
160 UserPerm perms(ctx->uid, ctx->gid);
161 GET_GROUPS(perms, req);
163 memset(&fe, 0, sizeof(fe));
164 r = cfuse->client->ll_lookup(i1, name, &fe.attr, &i2, perms);
166 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
167 fe.attr.st_rdev = new_encode_dev(fe.attr.st_rdev);
168 fuse_reply_entry(req, &fe);
170 fuse_reply_err(req, -r);
173 // XXX NB, we dont iput(i2) because FUSE will do so in a matching
178 static void fuse_ll_forget(fuse_req_t req, fuse_ino_t ino,
179 long unsigned nlookup)
181 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
182 cfuse->client->ll_forget(cfuse->iget(ino), nlookup+1);
183 fuse_reply_none(req);
186 static void fuse_ll_getattr(fuse_req_t req, fuse_ino_t ino,
187 struct fuse_file_info *fi)
189 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
190 const struct fuse_ctx *ctx = fuse_req_ctx(req);
191 Inode *in = cfuse->iget(ino);
193 UserPerm perms(ctx->uid, ctx->gid);
194 GET_GROUPS(perms, req);
198 if (cfuse->client->ll_getattr(in, &stbuf, perms)
200 stbuf.st_ino = cfuse->make_fake_ino(stbuf.st_ino, stbuf.st_dev);
201 stbuf.st_rdev = new_encode_dev(stbuf.st_rdev);
202 fuse_reply_attr(req, &stbuf, 0);
204 fuse_reply_err(req, ENOENT);
206 cfuse->iput(in); // iput required
209 static void fuse_ll_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
210 int to_set, struct fuse_file_info *fi)
212 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
213 const struct fuse_ctx *ctx = fuse_req_ctx(req);
214 Inode *in = cfuse->iget(ino);
215 UserPerm perms(ctx->uid, ctx->gid);
216 GET_GROUPS(perms, req);
219 if (to_set & FUSE_SET_ATTR_MODE) mask |= CEPH_SETATTR_MODE;
220 if (to_set & FUSE_SET_ATTR_UID) mask |= CEPH_SETATTR_UID;
221 if (to_set & FUSE_SET_ATTR_GID) mask |= CEPH_SETATTR_GID;
222 if (to_set & FUSE_SET_ATTR_MTIME) mask |= CEPH_SETATTR_MTIME;
223 if (to_set & FUSE_SET_ATTR_ATIME) mask |= CEPH_SETATTR_ATIME;
224 if (to_set & FUSE_SET_ATTR_SIZE) mask |= CEPH_SETATTR_SIZE;
226 if (to_set & FUSE_SET_ATTR_MTIME_NOW) mask |= CEPH_SETATTR_MTIME_NOW;
227 if (to_set & FUSE_SET_ATTR_ATIME_NOW) mask |= CEPH_SETATTR_ATIME_NOW;
230 int r = cfuse->client->ll_setattr(in, attr, mask, perms);
232 fuse_reply_attr(req, attr, 0);
234 fuse_reply_err(req, -r);
236 cfuse->iput(in); // iput required
241 static void fuse_ll_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
242 const char *value, size_t size,
249 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
250 const struct fuse_ctx *ctx = fuse_req_ctx(req);
251 Inode *in = cfuse->iget(ino);
252 UserPerm perms(ctx->uid, ctx->gid);
253 GET_GROUPS(perms, req);
255 int r = cfuse->client->ll_setxattr(in, name, value, size, flags, perms);
256 fuse_reply_err(req, -r);
258 cfuse->iput(in); // iput required
261 static void fuse_ll_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
263 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
264 const struct fuse_ctx *ctx = fuse_req_ctx(req);
265 Inode *in = cfuse->iget(ino);
267 UserPerm perms(ctx->uid, ctx->gid);
268 GET_GROUPS(perms, req);
270 int r = cfuse->client->ll_listxattr(in, buf, size, perms);
271 if (size == 0 && r >= 0)
272 fuse_reply_xattr(req, r);
274 fuse_reply_buf(req, buf, r);
276 fuse_reply_err(req, -r);
278 cfuse->iput(in); // iput required
281 static void fuse_ll_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
288 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
289 const struct fuse_ctx *ctx = fuse_req_ctx(req);
290 Inode *in = cfuse->iget(ino);
292 UserPerm perms(ctx->uid, ctx->gid);
293 GET_GROUPS(perms, req);
295 int r = cfuse->client->ll_getxattr(in, name, buf, size, perms);
296 if (size == 0 && r >= 0)
297 fuse_reply_xattr(req, r);
299 fuse_reply_buf(req, buf, r);
301 fuse_reply_err(req, -r);
303 cfuse->iput(in); // iput required
306 static void fuse_ll_removexattr(fuse_req_t req, fuse_ino_t ino,
309 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
310 const struct fuse_ctx *ctx = fuse_req_ctx(req);
311 Inode *in = cfuse->iget(ino);
312 UserPerm perms(ctx->uid, ctx->gid);
313 GET_GROUPS(perms, req);
315 int r = cfuse->client->ll_removexattr(in, name, perms);
316 fuse_reply_err(req, -r);
318 cfuse->iput(in); // iput required
321 static void fuse_ll_opendir(fuse_req_t req, fuse_ino_t ino,
322 struct fuse_file_info *fi)
324 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
325 const struct fuse_ctx *ctx = fuse_req_ctx(req);
326 Inode *in = cfuse->iget(ino);
329 UserPerm perms(ctx->uid, ctx->gid);
330 GET_GROUPS(perms, req);
332 int r = cfuse->client->ll_opendir(in, fi->flags, (dir_result_t **)&dirp,
335 fi->fh = (uint64_t)dirp;
336 fuse_reply_open(req, fi);
338 fuse_reply_err(req, -r);
341 cfuse->iput(in); // iput required
344 static void fuse_ll_readlink(fuse_req_t req, fuse_ino_t ino)
346 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
347 const struct fuse_ctx *ctx = fuse_req_ctx(req);
348 Inode *in = cfuse->iget(ino);
349 char buf[PATH_MAX + 1]; // leave room for a null terminator
350 UserPerm perms(ctx->uid, ctx->gid);
351 GET_GROUPS(perms, req);
353 int r = cfuse->client->ll_readlink(in, buf, sizeof(buf) - 1, perms);
356 fuse_reply_readlink(req, buf);
358 fuse_reply_err(req, -r);
361 cfuse->iput(in); // iput required
364 static void fuse_ll_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
365 mode_t mode, dev_t rdev)
367 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
368 const struct fuse_ctx *ctx = fuse_req_ctx(req);
369 Inode *i2, *i1 = cfuse->iget(parent);
370 struct fuse_entry_param fe;
371 UserPerm perms(ctx->uid, ctx->gid);
372 GET_GROUPS(perms, req);
374 memset(&fe, 0, sizeof(fe));
376 int r = cfuse->client->ll_mknod(i1, name, mode, new_decode_dev(rdev),
377 &fe.attr, &i2, perms);
379 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
380 fe.attr.st_rdev = new_encode_dev(fe.attr.st_rdev);
381 fuse_reply_entry(req, &fe);
383 fuse_reply_err(req, -r);
386 // XXX NB, we dont iput(i2) because FUSE will do so in a matching
388 cfuse->iput(i1); // iput required
391 static void fuse_ll_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
394 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
395 const struct fuse_ctx *ctx = fuse_req_ctx(req);
397 struct fuse_entry_param fe;
399 memset(&fe, 0, sizeof(fe));
400 UserPerm perm(ctx->uid, ctx->gid);
401 GET_GROUPS(perm, req);
402 #ifdef HAVE_SYS_SYNCFS
403 if (cfuse->fino_snap(parent) == CEPH_SNAPDIR &&
404 cfuse->client->cct->_conf->fuse_multithreaded &&
405 cfuse->client->cct->_conf->fuse_syncfs_on_mksnap) {
407 int fd = ::open(cfuse->mountpoint, O_RDONLY | O_DIRECTORY);
411 int r = ::syncfs(fd);
417 fuse_reply_err(req, err);
423 i1 = cfuse->iget(parent);
424 int r = cfuse->client->ll_mkdir(i1, name, mode, &fe.attr, &i2, perm);
426 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
427 fe.attr.st_rdev = new_encode_dev(fe.attr.st_rdev);
428 fuse_reply_entry(req, &fe);
430 fuse_reply_err(req, -r);
433 // XXX NB, we dont iput(i2) because FUSE will do so in a matching
435 cfuse->iput(i1); // iput required
438 static void fuse_ll_unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
440 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
441 const struct fuse_ctx *ctx = fuse_req_ctx(req);
442 Inode *in = cfuse->iget(parent);
443 UserPerm perm(ctx->uid, ctx->gid);
444 GET_GROUPS(perm, req);
446 int r = cfuse->client->ll_unlink(in, name, perm);
447 fuse_reply_err(req, -r);
449 cfuse->iput(in); // iput required
452 static void fuse_ll_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
454 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
455 const struct fuse_ctx *ctx = fuse_req_ctx(req);
456 Inode *in = cfuse->iget(parent);
457 UserPerm perms(ctx->uid, ctx->gid);
458 GET_GROUPS(perms, req);
460 int r = cfuse->client->ll_rmdir(in, name, perms);
461 fuse_reply_err(req, -r);
463 cfuse->iput(in); // iput required
466 static void fuse_ll_symlink(fuse_req_t req, const char *existing,
467 fuse_ino_t parent, const char *name)
469 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
470 const struct fuse_ctx *ctx = fuse_req_ctx(req);
471 Inode *i2, *i1 = cfuse->iget(parent);
472 struct fuse_entry_param fe;
473 UserPerm perms(ctx->uid, ctx->gid);
474 GET_GROUPS(perms, req);
476 memset(&fe, 0, sizeof(fe));
478 int r = cfuse->client->ll_symlink(i1, name, existing, &fe.attr, &i2, perms);
480 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
481 fe.attr.st_rdev = new_encode_dev(fe.attr.st_rdev);
482 fuse_reply_entry(req, &fe);
484 fuse_reply_err(req, -r);
487 // XXX NB, we dont iput(i2) because FUSE will do so in a matching
489 cfuse->iput(i1); // iput required
492 static void fuse_ll_rename(fuse_req_t req, fuse_ino_t parent, const char *name,
493 fuse_ino_t newparent, const char *newname)
495 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
496 const struct fuse_ctx *ctx = fuse_req_ctx(req);
497 Inode *in = cfuse->iget(parent);
498 Inode *nin = cfuse->iget(newparent);
499 UserPerm perm(ctx->uid, ctx->gid);
500 GET_GROUPS(perm, req);
502 int r = cfuse->client->ll_rename(in, name, nin, newname, perm);
503 fuse_reply_err(req, -r);
505 cfuse->iput(in); // iputs required
509 static void fuse_ll_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent,
512 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
513 const struct fuse_ctx *ctx = fuse_req_ctx(req);
514 Inode *in = cfuse->iget(ino);
515 Inode *nin = cfuse->iget(newparent);
516 struct fuse_entry_param fe;
518 memset(&fe, 0, sizeof(fe));
519 UserPerm perm(ctx->uid, ctx->gid);
520 GET_GROUPS(perm, req);
523 * Note that we could successfully link, but then fail the subsequent
524 * getattr and return an error. Perhaps we should ignore getattr errors,
525 * but then how do we tell FUSE that the attrs are bogus?
527 int r = cfuse->client->ll_link(in, nin, newname, perm);
529 r = cfuse->client->ll_getattr(in, &fe.attr, perm);
531 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
532 fe.attr.st_rdev = new_encode_dev(fe.attr.st_rdev);
533 fuse_reply_entry(req, &fe);
539 * Many ll operations in libcephfs return an extra inode reference, but
540 * ll_link currently does not. Still, FUSE needs one for the new dentry,
541 * so we commandeer the reference taken earlier when ll_link is successful.
542 * On error however, we must put that reference.
545 fuse_reply_err(req, -r);
551 static void fuse_ll_open(fuse_req_t req, fuse_ino_t ino,
552 struct fuse_file_info *fi)
554 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
555 const struct fuse_ctx *ctx = fuse_req_ctx(req);
556 Inode *in = cfuse->iget(ino);
558 UserPerm perms(ctx->uid, ctx->gid);
559 GET_GROUPS(perms, req);
561 int r = cfuse->client->ll_open(in, fi->flags, &fh, perms);
563 fi->fh = (uint64_t)fh;
564 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
565 if (cfuse->client->cct->_conf->fuse_disable_pagecache)
567 else if (cfuse->client->cct->_conf->fuse_use_invalidate_cb)
570 fuse_reply_open(req, fi);
572 fuse_reply_err(req, -r);
575 cfuse->iput(in); // iput required
578 static void fuse_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
579 struct fuse_file_info *fi)
581 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
582 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
584 int r = cfuse->client->ll_read(fh, off, size, &bl);
586 fuse_reply_buf(req, bl.c_str(), bl.length());
588 fuse_reply_err(req, -r);
591 static void fuse_ll_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
592 size_t size, off_t off, struct fuse_file_info *fi)
594 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
595 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
596 int r = cfuse->client->ll_write(fh, off, size, buf);
598 fuse_reply_write(req, r);
600 fuse_reply_err(req, -r);
603 static void fuse_ll_flush(fuse_req_t req, fuse_ino_t ino,
604 struct fuse_file_info *fi)
606 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
607 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
608 int r = cfuse->client->ll_flush(fh);
609 fuse_reply_err(req, -r);
612 #ifdef FUSE_IOCTL_COMPAT
613 static void fuse_ll_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, struct fuse_file_info *fi,
614 unsigned flags, const void *in_buf, size_t in_bufsz, size_t out_bufsz)
616 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
618 if (flags & FUSE_IOCTL_COMPAT) {
619 fuse_reply_err(req, ENOSYS);
623 switch (static_cast<unsigned>(cmd)) {
624 case CEPH_IOC_GET_LAYOUT: {
625 file_layout_t layout;
626 struct ceph_ioctl_layout l;
627 Fh *fh = (Fh*)fi->fh;
628 cfuse->client->ll_file_layout(fh, &layout);
629 l.stripe_unit = layout.stripe_unit;
630 l.stripe_count = layout.stripe_count;
631 l.object_size = layout.object_size;
632 l.data_pool = layout.pool_id;
633 fuse_reply_ioctl(req, 0, &l, sizeof(struct ceph_ioctl_layout));
637 fuse_reply_err(req, EINVAL);
642 #if FUSE_VERSION > FUSE_MAKE_VERSION(2, 9)
644 static void fuse_ll_fallocate(fuse_req_t req, fuse_ino_t ino, int mode,
645 off_t offset, off_t length,
646 struct fuse_file_info *fi)
648 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
649 Fh *fh = (Fh*)fi->fh;
650 int r = cfuse->client->ll_fallocate(fh, mode, offset, length);
651 fuse_reply_err(req, -r);
656 static void fuse_ll_release(fuse_req_t req, fuse_ino_t ino,
657 struct fuse_file_info *fi)
659 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
660 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
661 int r = cfuse->client->ll_release(fh);
662 fuse_reply_err(req, -r);
665 static void fuse_ll_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
666 struct fuse_file_info *fi)
668 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
669 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
670 int r = cfuse->client->ll_fsync(fh, datasync);
671 fuse_reply_err(req, -r);
674 struct readdir_context {
678 size_t pos; /* in buf */
683 * return 0 on success, -1 if out of space
685 static int fuse_ll_add_dirent(void *p, struct dirent *de,
686 struct ceph_statx *stx, off_t next_off,
689 struct readdir_context *c = (struct readdir_context *)p;
690 CephFuse::Handle *cfuse = (CephFuse::Handle *)fuse_req_userdata(c->req);
693 st.st_ino = cfuse->make_fake_ino(stx->stx_ino, c->snap);
694 st.st_mode = stx->stx_mode;
695 st.st_rdev = new_encode_dev(stx->stx_rdev);
697 size_t room = c->size - c->pos;
698 size_t entrysize = fuse_add_direntry(c->req, c->buf + c->pos, room,
699 de->d_name, &st, next_off);
700 if (entrysize > room)
708 static void fuse_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
709 off_t off, struct fuse_file_info *fi)
711 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
713 dir_result_t *dirp = reinterpret_cast<dir_result_t*>(fi->fh);
714 cfuse->client->seekdir(dirp, off);
716 struct readdir_context rc;
718 rc.buf = new char[size];
721 rc.snap = cfuse->fino_snap(ino);
723 int r = cfuse->client->readdir_r_cb(dirp, fuse_ll_add_dirent, &rc);
724 if (r == 0 || r == -ENOSPC) /* ignore ENOSPC from our callback */
725 fuse_reply_buf(req, rc.buf, rc.pos);
727 fuse_reply_err(req, -r);
731 static void fuse_ll_releasedir(fuse_req_t req, fuse_ino_t ino,
732 struct fuse_file_info *fi)
734 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
735 dir_result_t *dirp = reinterpret_cast<dir_result_t*>(fi->fh);
736 cfuse->client->ll_releasedir(dirp);
737 fuse_reply_err(req, 0);
740 static void fuse_ll_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
741 struct fuse_file_info *fi)
743 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
744 dir_result_t *dirp = reinterpret_cast<dir_result_t*>(fi->fh);
745 int r = cfuse->client->ll_fsyncdir(dirp);
746 fuse_reply_err(req, -r);
749 static void fuse_ll_access(fuse_req_t req, fuse_ino_t ino, int mask)
751 fuse_reply_err(req, 0);
754 static void fuse_ll_create(fuse_req_t req, fuse_ino_t parent, const char *name,
755 mode_t mode, struct fuse_file_info *fi)
757 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
758 const struct fuse_ctx *ctx = fuse_req_ctx(req);
759 Inode *i1 = cfuse->iget(parent), *i2;
760 struct fuse_entry_param fe;
762 UserPerm perms(ctx->uid, ctx->gid);
763 GET_GROUPS(perms, req);
765 memset(&fe, 0, sizeof(fe));
767 // pass &i2 for the created inode so that ll_create takes an initial ll_ref
768 int r = cfuse->client->ll_create(i1, name, mode, fi->flags, &fe.attr, &i2,
771 fi->fh = (uint64_t)fh;
772 fe.ino = cfuse->make_fake_ino(fe.attr.st_ino, fe.attr.st_dev);
773 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
774 if (cfuse->client->cct->_conf->fuse_disable_pagecache)
776 else if (cfuse->client->cct->_conf->fuse_use_invalidate_cb)
779 fuse_reply_create(req, &fe, fi);
781 fuse_reply_err(req, -r);
782 // XXX NB, we dont iput(i2) because FUSE will do so in a matching
784 cfuse->iput(i1); // iput required
787 static void fuse_ll_statfs(fuse_req_t req, fuse_ino_t ino)
789 struct statvfs stbuf;
790 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
791 Inode *in = cfuse->iget(ino);
792 const struct fuse_ctx *ctx = fuse_req_ctx(req);
793 UserPerm perms(ctx->uid, ctx->gid);
794 GET_GROUPS(perms, req);
796 int r = cfuse->client->ll_statfs(in, &stbuf, perms);
798 fuse_reply_statfs(req, &stbuf);
800 fuse_reply_err(req, -r);
802 cfuse->iput(in); // iput required
805 static void fuse_ll_getlk(fuse_req_t req, fuse_ino_t ino,
806 struct fuse_file_info *fi, struct flock *lock)
808 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
809 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
811 int r = cfuse->client->ll_getlk(fh, lock, fi->lock_owner);
813 fuse_reply_lock(req, lock);
815 fuse_reply_err(req, -r);
818 static void fuse_ll_setlk(fuse_req_t req, fuse_ino_t ino,
819 struct fuse_file_info *fi, struct flock *lock, int sleep)
821 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
822 Fh *fh = reinterpret_cast<Fh*>(fi->fh);
824 // must use multithread if operation may block
825 if (!cfuse->client->cct->_conf->fuse_multithreaded &&
826 sleep && lock->l_type != F_UNLCK) {
827 fuse_reply_err(req, EDEADLK);
831 int r = cfuse->client->ll_setlk(fh, lock, fi->lock_owner, sleep);
832 fuse_reply_err(req, -r);
835 static void fuse_ll_interrupt(fuse_req_t req, void* data)
837 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
838 cfuse->client->ll_interrupt(data);
841 static void switch_interrupt_cb(void *handle, void* data)
843 CephFuse::Handle *cfuse = (CephFuse::Handle *)handle;
844 fuse_req_t req = cfuse->get_fuse_req();
847 fuse_req_interrupt_func(req, fuse_ll_interrupt, data);
849 fuse_req_interrupt_func(req, NULL, NULL);
852 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
853 static void fuse_ll_flock(fuse_req_t req, fuse_ino_t ino,
854 struct fuse_file_info *fi, int cmd)
856 CephFuse::Handle *cfuse = fuse_ll_req_prepare(req);
857 Fh *fh = (Fh*)fi->fh;
859 // must use multithread if operation may block
860 if (!cfuse->client->cct->_conf->fuse_multithreaded &&
861 !(cmd & (LOCK_NB | LOCK_UN))) {
862 fuse_reply_err(req, EDEADLK);
866 int r = cfuse->client->ll_flock(fh, cmd, fi->lock_owner);
867 fuse_reply_err(req, -r);
872 static mode_t umask_cb(void *handle)
874 CephFuse::Handle *cfuse = (CephFuse::Handle *)handle;
875 fuse_req_t req = cfuse->get_fuse_req();
876 const struct fuse_ctx *ctx = fuse_req_ctx(req);
881 static void ino_invalidate_cb(void *handle, vinodeno_t vino, int64_t off,
884 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
885 CephFuse::Handle *cfuse = (CephFuse::Handle *)handle;
886 fuse_ino_t fino = cfuse->make_fake_ino(vino.ino, vino.snapid);
887 fuse_lowlevel_notify_inval_inode(cfuse->ch, fino, off, len);
891 static void dentry_invalidate_cb(void *handle, vinodeno_t dirino,
892 vinodeno_t ino, string& name)
894 CephFuse::Handle *cfuse = (CephFuse::Handle *)handle;
895 fuse_ino_t fdirino = cfuse->make_fake_ino(dirino.ino, dirino.snapid);
896 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
898 if (ino.ino != inodeno_t())
899 fino = cfuse->make_fake_ino(ino.ino, ino.snapid);
900 fuse_lowlevel_notify_delete(cfuse->ch, fdirino, fino, name.c_str(), name.length());
901 #elif FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
902 fuse_lowlevel_notify_inval_entry(cfuse->ch, fdirino, name.c_str(), name.length());
906 static int remount_cb(void *handle)
908 // used for trimming kernel dcache. when remounting a file system, linux kernel
909 // trims all unused dentries in the file system
911 CephFuse::Handle *cfuse = (CephFuse::Handle *)handle;
912 snprintf(cmd, sizeof(cmd), "mount -i -o remount %s", cfuse->mountpoint);
914 if (r != 0 && r != -1) {
921 static void do_init(void *data, fuse_conn_info *conn)
923 CephFuse::Handle *cfuse = (CephFuse::Handle *)data;
924 Client *client = cfuse->client;
927 if (!client->cct->_conf->fuse_default_permissions &&
928 client->ll_handle_umask()) {
929 // apply umask in userspace if posix acl is enabled
930 if(conn->capable & FUSE_CAP_DONT_MASK)
931 conn->want |= FUSE_CAP_DONT_MASK;
935 if (cfuse->fd_on_success) {
936 //cout << "fuse init signaling on fd " << fd_on_success << std::endl;
937 // see Preforker::daemonize(), ceph-fuse's parent process expects a `-1`
938 // from a daemonized child process.
940 int err = safe_write(cfuse->fd_on_success, &r, sizeof(r));
942 derr << "fuse_ll: do_init: safe_write failed with error "
943 << cpp_strerror(err) << dendl;
946 //cout << "fuse init done signaling on fd " << fd_on_success << std::endl;
948 // close stdout, etc.
955 const static struct fuse_lowlevel_ops fuse_ll_oper = {
958 lookup: fuse_ll_lookup,
959 forget: fuse_ll_forget,
960 getattr: fuse_ll_getattr,
961 setattr: fuse_ll_setattr,
962 readlink: fuse_ll_readlink,
963 mknod: fuse_ll_mknod,
964 mkdir: fuse_ll_mkdir,
965 unlink: fuse_ll_unlink,
966 rmdir: fuse_ll_rmdir,
967 symlink: fuse_ll_symlink,
968 rename: fuse_ll_rename,
972 write: fuse_ll_write,
973 flush: fuse_ll_flush,
974 release: fuse_ll_release,
975 fsync: fuse_ll_fsync,
976 opendir: fuse_ll_opendir,
977 readdir: fuse_ll_readdir,
978 releasedir: fuse_ll_releasedir,
979 fsyncdir: fuse_ll_fsyncdir,
980 statfs: fuse_ll_statfs,
981 setxattr: fuse_ll_setxattr,
982 getxattr: fuse_ll_getxattr,
983 listxattr: fuse_ll_listxattr,
984 removexattr: fuse_ll_removexattr,
985 access: fuse_ll_access,
986 create: fuse_ll_create,
987 getlk: fuse_ll_getlk,
988 setlk: fuse_ll_setlk,
990 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
991 #ifdef FUSE_IOCTL_COMPAT
992 ioctl: fuse_ll_ioctl,
998 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
1002 flock: fuse_ll_flock,
1004 #if FUSE_VERSION > FUSE_MAKE_VERSION(2, 9)
1005 fallocate: fuse_ll_fallocate
1010 CephFuse::Handle::Handle(Client *c, int fd) :
1016 stag_lock("fuse_ll.cc stag_lock"),
1019 snap_stag_map[CEPH_NOSNAP] = 0;
1020 stag_snap_map[0] = CEPH_NOSNAP;
1021 memset(&args, 0, sizeof(args));
1024 CephFuse::Handle::~Handle()
1026 fuse_opt_free_args(&args);
1029 void CephFuse::Handle::finalize()
1032 fuse_remove_signal_handlers(se);
1034 fuse_session_remove_chan(ch);
1036 fuse_session_destroy(se);
1038 fuse_unmount(mountpoint, ch);
1040 pthread_key_delete(fuse_req_key);
1043 int CephFuse::Handle::init(int argc, const char *argv[])
1046 int r = pthread_key_create(&fuse_req_key, NULL);
1048 derr << "pthread_key_create failed." << dendl;
1052 // set up fuse argc/argv
1054 const char **newargv = (const char **) malloc((argc + 10) * sizeof(char *));
1058 newargv[newargc++] = argv[0];
1059 newargv[newargc++] = "-f"; // stay in foreground
1061 if (client->cct->_conf->fuse_allow_other) {
1062 newargv[newargc++] = "-o";
1063 newargv[newargc++] = "allow_other";
1065 if (client->cct->_conf->fuse_default_permissions) {
1066 newargv[newargc++] = "-o";
1067 newargv[newargc++] = "default_permissions";
1069 #if defined(__linux__)
1070 if (client->cct->_conf->fuse_big_writes) {
1071 newargv[newargc++] = "-o";
1072 newargv[newargc++] = "big_writes";
1074 if (client->cct->_conf->fuse_atomic_o_trunc) {
1075 newargv[newargc++] = "-o";
1076 newargv[newargc++] = "atomic_o_trunc";
1079 if (client->cct->_conf->fuse_debug)
1080 newargv[newargc++] = "-d";
1082 for (int argctr = 1; argctr < argc; argctr++)
1083 newargv[newargc++] = argv[argctr];
1085 derr << "init, newargv = " << newargv << " newargc=" << newargc << dendl;
1086 struct fuse_args a = FUSE_ARGS_INIT(newargc, (char**)newargv);
1087 args = a; // Roundabout construction b/c FUSE_ARGS_INIT is for initialization not assignment
1089 if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) == -1) {
1090 derr << "fuse_parse_cmdline failed." << dendl;
1091 fuse_opt_free_args(&args);
1096 assert(args.allocated); // Checking fuse has realloc'd args so we can free newargv
1101 int CephFuse::Handle::start()
1103 ch = fuse_mount(mountpoint, &args);
1105 derr << "fuse_mount(mountpoint=" << mountpoint << ") failed." << dendl;
1109 se = fuse_lowlevel_new(&args, &fuse_ll_oper, sizeof(fuse_ll_oper), this);
1111 derr << "fuse_lowlevel_new failed" << dendl;
1115 signal(SIGTERM, SIG_DFL);
1116 signal(SIGINT, SIG_DFL);
1117 if (fuse_set_signal_handlers(se) == -1) {
1118 derr << "fuse_set_signal_handlers failed" << dendl;
1122 fuse_session_add_chan(se, ch);
1125 struct client_callback_args args = {
1127 ino_cb: client->cct->_conf->fuse_use_invalidate_cb ? ino_invalidate_cb : NULL,
1128 dentry_cb: dentry_invalidate_cb,
1129 switch_intr_cb: switch_interrupt_cb,
1130 #if defined(__linux__)
1131 remount_cb: remount_cb,
1133 getgroups_cb: getgroups_cb,
1134 #if !defined(DARWIN)
1138 client->ll_register_callbacks(&args);
1143 int CephFuse::Handle::loop()
1145 if (client->cct->_conf->fuse_multithreaded) {
1146 return fuse_session_loop_mt(se);
1148 return fuse_session_loop(se);
1152 uint64_t CephFuse::Handle::fino_snap(uint64_t fino)
1154 if (fino == FUSE_ROOT_ID)
1157 if (client->use_faked_inos()) {
1158 vinodeno_t vino = client->map_faked_ino(fino);
1161 Mutex::Locker l(stag_lock);
1162 uint64_t stag = FINO_STAG(fino);
1163 assert(stag_snap_map.count(stag));
1164 return stag_snap_map[stag];
1168 Inode * CephFuse::Handle::iget(fuse_ino_t fino)
1170 if (fino == FUSE_ROOT_ID)
1171 return client->get_root();
1173 if (client->use_faked_inos()) {
1174 return client->ll_get_inode((ino_t)fino);
1176 vinodeno_t vino(FINO_INO(fino), fino_snap(fino));
1177 return client->ll_get_inode(vino);
1181 void CephFuse::Handle::iput(Inode *in)
1186 uint64_t CephFuse::Handle::make_fake_ino(inodeno_t ino, snapid_t snapid)
1188 if (client->use_faked_inos()) {
1189 // already faked by libcephfs
1190 if (ino == client->get_root_ino())
1191 return FUSE_ROOT_ID;
1195 if (snapid == CEPH_NOSNAP && ino == client->get_root_ino())
1196 return FUSE_ROOT_ID;
1198 Mutex::Locker l(stag_lock);
1200 if (snap_stag_map.count(snapid) == 0) {
1202 snap_stag_map[snapid] = stag;
1203 stag_snap_map[stag] = snapid;
1205 stag = snap_stag_map[snapid];
1206 inodeno_t fino = MAKE_FINO(ino, stag);
1207 //cout << "make_fake_ino " << ino << "." << snapid << " -> " << fino << std::endl;
1212 void CephFuse::Handle::set_fuse_req(fuse_req_t req)
1214 pthread_setspecific(fuse_req_key, (void*)req);
1217 fuse_req_t CephFuse::Handle::get_fuse_req()
1219 return (fuse_req_t) pthread_getspecific(fuse_req_key);
1223 CephFuse::CephFuse(Client *c, int fd) : _handle(new CephFuse::Handle(c, fd))
1227 CephFuse::~CephFuse()
1232 int CephFuse::init(int argc, const char *argv[])
1234 return _handle->init(argc, argv);
1237 int CephFuse::start()
1239 return _handle->start();
1242 int CephFuse::loop()
1244 return _handle->loop();
1247 void CephFuse::finalize()
1249 return _handle->finalize();
1252 std::string CephFuse::get_mount_point() const
1254 if (_handle->mountpoint) {
1255 return _handle->mountpoint;