16 #include "fuse_kernel.h" 18 #include "fuse_misc.h" 19 #include "mount_util.h" 31 #ifndef F_LINUX_SPECIFIC_BASE 32 #define F_LINUX_SPECIFIC_BASE 1024 35 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7) 39 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) 40 #define OFFSET_MAX 0x7fffffffffffffffLL 42 #define container_of(ptr, type, member) ({ \ 43 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 44 (type *)( (char *)__mptr - offsetof(type,member) );}) 46 struct fuse_pollhandle {
48 struct fuse_session *se;
51 static size_t pagesize;
53 static __attribute__((constructor))
void fuse_ll_init_pagesize(
void)
55 pagesize = getpagesize();
58 static void convert_stat(
const struct stat *stbuf,
struct fuse_attr *attr)
60 attr->ino = stbuf->st_ino;
61 attr->mode = stbuf->st_mode;
62 attr->nlink = stbuf->st_nlink;
63 attr->uid = stbuf->st_uid;
64 attr->gid = stbuf->st_gid;
65 attr->rdev = stbuf->st_rdev;
66 attr->size = stbuf->st_size;
67 attr->blksize = stbuf->st_blksize;
68 attr->blocks = stbuf->st_blocks;
69 attr->atime = stbuf->st_atime;
70 attr->mtime = stbuf->st_mtime;
71 attr->ctime = stbuf->st_ctime;
72 attr->atimensec = ST_ATIM_NSEC(stbuf);
73 attr->mtimensec = ST_MTIM_NSEC(stbuf);
74 attr->ctimensec = ST_CTIM_NSEC(stbuf);
77 static void convert_attr(
const struct fuse_setattr_in *attr,
struct stat *stbuf)
79 stbuf->st_mode = attr->mode;
80 stbuf->st_uid = attr->uid;
81 stbuf->st_gid = attr->gid;
82 stbuf->st_size = attr->size;
83 stbuf->st_atime = attr->atime;
84 stbuf->st_mtime = attr->mtime;
85 stbuf->st_ctime = attr->ctime;
86 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
87 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
88 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
91 static size_t iov_length(
const struct iovec *iov,
size_t count)
96 for (seg = 0; seg < count; seg++)
97 ret += iov[seg].iov_len;
101 static void list_init_req(
struct fuse_req *req)
107 static void list_del_req(
struct fuse_req *req)
109 struct fuse_req *prev = req->prev;
110 struct fuse_req *next = req->next;
115 static void list_add_req(
struct fuse_req *req,
struct fuse_req *next)
117 struct fuse_req *prev = next->prev;
126 pthread_mutex_destroy(&req->lock);
133 struct fuse_session *se = req->se;
135 pthread_mutex_lock(&se->lock);
136 req->u.ni.func = NULL;
137 req->u.ni.data = NULL;
140 fuse_chan_put(req->ch);
142 pthread_mutex_unlock(&se->lock);
147 static struct fuse_req *fuse_ll_alloc_req(
struct fuse_session *se)
149 struct fuse_req *req;
151 req = (
struct fuse_req *) calloc(1,
sizeof(
struct fuse_req));
153 fprintf(stderr,
"fuse: failed to allocate request\n");
158 fuse_mutex_init(&req->lock);
165 static int fuse_send_msg(
struct fuse_session *se,
struct fuse_chan *ch,
166 struct iovec *iov,
int count)
168 struct fuse_out_header *out = iov[0].iov_base;
170 out->len = iov_length(iov, count);
172 if (out->unique == 0) {
173 fprintf(stderr,
"NOTIFY: code=%d length=%u\n",
174 out->error, out->len);
175 }
else if (out->error) {
177 " unique: %llu, error: %i (%s), outsize: %i\n",
178 (
unsigned long long) out->unique, out->error,
179 strerror(-out->error), out->len);
182 " unique: %llu, success, outsize: %i\n",
183 (
unsigned long long) out->unique, out->len);
187 ssize_t res = writev(ch ? ch->fd : se->fd,
196 perror(
"fuse: writing device");
204 int fuse_send_reply_iov_nofree(
fuse_req_t req,
int error,
struct iovec *iov,
207 struct fuse_out_header out;
209 if (error <= -1000 || error > 0) {
210 fprintf(stderr,
"fuse: bad error value: %i\n", error);
214 out.unique = req->unique;
217 iov[0].iov_base = &out;
218 iov[0].iov_len =
sizeof(
struct fuse_out_header);
220 return fuse_send_msg(req->se, req->ch, iov, count);
223 static int send_reply_iov(
fuse_req_t req,
int error,
struct iovec *iov,
228 res = fuse_send_reply_iov_nofree(req, error, iov, count);
233 static int send_reply(
fuse_req_t req,
int error,
const void *arg,
239 iov[1].iov_base = (
void *) arg;
240 iov[1].iov_len = argsize;
243 return send_reply_iov(req, error, iov, count);
249 struct iovec *padded_iov;
251 padded_iov = malloc((count + 1) *
sizeof(
struct iovec));
252 if (padded_iov == NULL)
255 memcpy(padded_iov + 1, iov, count *
sizeof(
struct iovec));
258 res = send_reply_iov(req, 0, padded_iov, count);
268 const char *name,
const struct stat *stbuf, off_t off)
273 size_t entlen_padded;
274 struct fuse_dirent *dirent;
276 namelen = strlen(name);
277 entlen = FUSE_NAME_OFFSET + namelen;
278 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
280 if ((buf == NULL) || (entlen_padded > bufsize))
281 return entlen_padded;
283 dirent = (
struct fuse_dirent*) buf;
284 dirent->ino = stbuf->st_ino;
286 dirent->namelen = namelen;
287 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
288 strncpy(dirent->name, name, namelen);
289 memset(dirent->name + namelen, 0, entlen_padded - entlen);
291 return entlen_padded;
294 static void convert_statfs(
const struct statvfs *stbuf,
295 struct fuse_kstatfs *kstatfs)
297 kstatfs->bsize = stbuf->f_bsize;
298 kstatfs->frsize = stbuf->f_frsize;
299 kstatfs->blocks = stbuf->f_blocks;
300 kstatfs->bfree = stbuf->f_bfree;
301 kstatfs->bavail = stbuf->f_bavail;
302 kstatfs->files = stbuf->f_files;
303 kstatfs->ffree = stbuf->f_ffree;
304 kstatfs->namelen = stbuf->f_namemax;
307 static int send_reply_ok(
fuse_req_t req,
const void *arg,
size_t argsize)
309 return send_reply(req, 0, arg, argsize);
314 return send_reply(req, -err, NULL, 0);
322 static unsigned long calc_timeout_sec(
double t)
324 if (t > (
double) ULONG_MAX)
329 return (
unsigned long) t;
332 static unsigned int calc_timeout_nsec(
double t)
334 double f = t - (double) calc_timeout_sec(t);
337 else if (f >= 0.999999999)
340 return (
unsigned int) (f * 1.0e9);
343 static void fill_entry(
struct fuse_entry_out *arg,
346 arg->nodeid = e->
ino;
351 arg->attr_valid_nsec = calc_timeout_nsec(e->
attr_timeout);
352 convert_stat(&e->
attr, &arg->attr);
364 size_t entlen_padded;
366 namelen = strlen(name);
367 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
368 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
369 if ((buf == NULL) || (entlen_padded > bufsize))
370 return entlen_padded;
372 struct fuse_direntplus *dp = (
struct fuse_direntplus *) buf;
373 memset(&dp->entry_out, 0,
sizeof(dp->entry_out));
374 fill_entry(&dp->entry_out, e);
376 struct fuse_dirent *dirent = &dp->dirent;
377 dirent->ino = e->
attr.st_ino;
379 dirent->namelen = namelen;
380 dirent->type = (e->
attr.st_mode & S_IFMT) >> 12;
381 strncpy(dirent->name, name, namelen);
382 memset(dirent->name + namelen, 0, entlen_padded - entlen);
384 return entlen_padded;
387 static void fill_open(
struct fuse_open_out *arg,
392 arg->open_flags |= FOPEN_DIRECT_IO;
394 arg->open_flags |= FOPEN_KEEP_CACHE;
396 arg->open_flags |= FOPEN_CACHE_DIR;
398 arg->open_flags |= FOPEN_NONSEEKABLE;
403 struct fuse_entry_out arg;
404 size_t size = req->se->conn.proto_minor < 9 ?
405 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(arg);
409 if (!e->
ino && req->se->conn.proto_minor < 4)
412 memset(&arg, 0,
sizeof(arg));
414 return send_reply_ok(req, &arg, size);
420 char buf[
sizeof(
struct fuse_entry_out) + sizeof(struct fuse_open_out)];
421 size_t entrysize = req->se->conn.proto_minor < 9 ?
422 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(
struct fuse_entry_out);
423 struct fuse_entry_out *earg = (
struct fuse_entry_out *) buf;
424 struct fuse_open_out *oarg = (
struct fuse_open_out *) (buf + entrysize);
426 memset(buf, 0,
sizeof(buf));
429 return send_reply_ok(req, buf,
430 entrysize +
sizeof(
struct fuse_open_out));
436 struct fuse_attr_out arg;
437 size_t size = req->se->conn.proto_minor < 9 ?
438 FUSE_COMPAT_ATTR_OUT_SIZE :
sizeof(arg);
440 memset(&arg, 0,
sizeof(arg));
441 arg.attr_valid = calc_timeout_sec(attr_timeout);
442 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
443 convert_stat(attr, &arg.attr);
445 return send_reply_ok(req, &arg, size);
450 return send_reply_ok(req, linkname, strlen(linkname));
455 struct fuse_open_out arg;
457 memset(&arg, 0,
sizeof(arg));
459 return send_reply_ok(req, &arg,
sizeof(arg));
464 struct fuse_write_out arg;
466 memset(&arg, 0,
sizeof(arg));
469 return send_reply_ok(req, &arg,
sizeof(arg));
474 return send_reply_ok(req, buf, size);
477 static int fuse_send_data_iov_fallback(
struct fuse_session *se,
478 struct fuse_chan *ch,
479 struct iovec *iov,
int iov_count,
483 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
488 if (buf->
count == 1 && buf->
idx == 0 && buf->
off == 0 &&
493 iov[iov_count].iov_base = buf->
buf[0].
mem;
494 iov[iov_count].iov_len = len;
496 return fuse_send_msg(se, ch, iov, iov_count);
499 res = posix_memalign(&mbuf, pagesize, len);
503 mem_buf.
buf[0].
mem = mbuf;
511 iov[iov_count].iov_base = mbuf;
512 iov[iov_count].iov_len = len;
514 res = fuse_send_msg(se, ch, iov, iov_count);
520 struct fuse_ll_pipe {
526 static void fuse_ll_pipe_free(
struct fuse_ll_pipe *llp)
534 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC) 535 static int fuse_pipe(
int fds[2])
542 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
543 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
544 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
545 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
553 static int fuse_pipe(
int fds[2])
555 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
559 static struct fuse_ll_pipe *fuse_ll_get_pipe(
struct fuse_session *se)
561 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
565 llp = malloc(
sizeof(
struct fuse_ll_pipe));
569 res = fuse_pipe(llp->pipe);
578 llp->size = pagesize * 16;
581 pthread_setspecific(se->pipe_key, llp);
588 static void fuse_ll_clear_pipe(
struct fuse_session *se)
590 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
592 pthread_setspecific(se->pipe_key, NULL);
593 fuse_ll_pipe_free(llp);
597 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE) 598 static int read_back(
int fd,
char *buf,
size_t len)
602 res = read(fd, buf, len);
604 fprintf(stderr,
"fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
608 fprintf(stderr,
"fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
614 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
615 struct iovec *iov,
int iov_count,
620 struct fuse_out_header *out = iov[0].iov_base;
621 struct fuse_ll_pipe *llp;
624 size_t total_fd_size;
627 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
629 if (se->broken_splice_nonblock)
636 for (idx = buf->
idx; idx < buf->count; idx++) {
640 total_fd_size -= buf->
off;
643 if (total_fd_size < 2 * pagesize)
646 if (se->conn.proto_minor < 14 ||
650 llp = fuse_ll_get_pipe(se);
655 headerlen = iov_length(iov, iov_count);
657 out->len = headerlen + len;
663 pipesize = pagesize * (iov_count + buf->
count + 1) + out->len;
665 if (llp->size < pipesize) {
667 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
674 if (llp->size < pipesize)
679 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
683 if (res != headerlen) {
685 fprintf(stderr,
"fuse: short vmsplice to pipe: %u/%zu\n", res,
691 pipe_buf.
buf[0].
fd = llp->pipe[1];
696 if (res == -EAGAIN || res == -EINVAL) {
708 se->broken_splice_nonblock = 1;
710 pthread_setspecific(se->pipe_key, NULL);
711 fuse_ll_pipe_free(llp);
718 if (res != 0 && res < len) {
719 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
721 size_t now_len = res;
731 res = posix_memalign(&mbuf, pagesize, len);
735 mem_buf.
buf[0].
mem = mbuf;
736 mem_buf.
off = now_len;
740 size_t extra_len = res;
746 tmpbuf = malloc(headerlen);
747 if (tmpbuf == NULL) {
752 res = read_back(llp->pipe[0], tmpbuf, headerlen);
758 res = read_back(llp->pipe[0], mbuf, now_len);
763 len = now_len + extra_len;
764 iov[iov_count].iov_base = mbuf;
765 iov[iov_count].iov_len = len;
767 res = fuse_send_msg(se, ch, iov, iov_count);
775 out->len = headerlen + len;
779 " unique: %llu, success, outsize: %i (splice)\n",
780 (
unsigned long long) out->unique, out->len);
786 splice_flags |= SPLICE_F_MOVE;
788 res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
789 NULL, out->len, splice_flags);
792 perror(
"fuse: splice from pipe");
795 if (res != out->len) {
797 fprintf(stderr,
"fuse: short splice from pipe: %u/%u\n",
804 fuse_ll_clear_pipe(se);
808 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
811 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
812 struct iovec *iov,
int iov_count,
818 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
826 struct fuse_out_header out;
829 iov[0].iov_base = &out;
830 iov[0].iov_len =
sizeof(
struct fuse_out_header);
832 out.unique = req->unique;
835 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
846 struct fuse_statfs_out arg;
847 size_t size = req->se->conn.proto_minor < 4 ?
848 FUSE_COMPAT_STATFS_SIZE :
sizeof(arg);
850 memset(&arg, 0,
sizeof(arg));
851 convert_statfs(stbuf, &arg.st);
853 return send_reply_ok(req, &arg, size);
858 struct fuse_getxattr_out arg;
860 memset(&arg, 0,
sizeof(arg));
863 return send_reply_ok(req, &arg,
sizeof(arg));
868 struct fuse_lk_out arg;
870 memset(&arg, 0,
sizeof(arg));
871 arg.lk.type = lock->l_type;
872 if (lock->l_type != F_UNLCK) {
873 arg.lk.start = lock->l_start;
874 if (lock->l_len == 0)
875 arg.lk.end = OFFSET_MAX;
877 arg.lk.end = lock->l_start + lock->l_len - 1;
879 arg.lk.pid = lock->l_pid;
880 return send_reply_ok(req, &arg,
sizeof(arg));
885 struct fuse_bmap_out arg;
887 memset(&arg, 0,
sizeof(arg));
890 return send_reply_ok(req, &arg,
sizeof(arg));
893 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(
const struct iovec *iov,
896 struct fuse_ioctl_iovec *fiov;
899 fiov = malloc(
sizeof(fiov[0]) * count);
903 for (i = 0; i < count; i++) {
904 fiov[i].base = (uintptr_t) iov[i].iov_base;
905 fiov[i].len = iov[i].iov_len;
912 const struct iovec *in_iov,
size_t in_count,
913 const struct iovec *out_iov,
size_t out_count)
915 struct fuse_ioctl_out arg;
916 struct fuse_ioctl_iovec *in_fiov = NULL;
917 struct fuse_ioctl_iovec *out_fiov = NULL;
922 memset(&arg, 0,
sizeof(arg));
923 arg.flags |= FUSE_IOCTL_RETRY;
924 arg.in_iovs = in_count;
925 arg.out_iovs = out_count;
926 iov[count].iov_base = &arg;
927 iov[count].iov_len =
sizeof(arg);
930 if (req->se->conn.proto_minor < 16) {
932 iov[count].iov_base = (
void *)in_iov;
933 iov[count].iov_len =
sizeof(in_iov[0]) * in_count;
938 iov[count].iov_base = (
void *)out_iov;
939 iov[count].iov_len =
sizeof(out_iov[0]) * out_count;
944 if (
sizeof(
void *) == 4 && req->ioctl_64bit) {
950 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
954 iov[count].iov_base = (
void *)in_fiov;
955 iov[count].iov_len =
sizeof(in_fiov[0]) * in_count;
959 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
963 iov[count].iov_base = (
void *)out_fiov;
964 iov[count].iov_len =
sizeof(out_fiov[0]) * out_count;
969 res = send_reply_iov(req, 0, iov, count);
983 struct fuse_ioctl_out arg;
987 memset(&arg, 0,
sizeof(arg));
989 iov[count].iov_base = &arg;
990 iov[count].iov_len =
sizeof(arg);
994 iov[count].iov_base = (
char *) buf;
995 iov[count].iov_len = size;
999 return send_reply_iov(req, 0, iov, count);
1005 struct iovec *padded_iov;
1006 struct fuse_ioctl_out arg;
1009 padded_iov = malloc((count + 2) *
sizeof(
struct iovec));
1010 if (padded_iov == NULL)
1013 memset(&arg, 0,
sizeof(arg));
1014 arg.result = result;
1015 padded_iov[1].iov_base = &arg;
1016 padded_iov[1].iov_len =
sizeof(arg);
1018 memcpy(&padded_iov[2], iov, count *
sizeof(
struct iovec));
1020 res = send_reply_iov(req, 0, padded_iov, count + 2);
1028 struct fuse_poll_out arg;
1030 memset(&arg, 0,
sizeof(arg));
1031 arg.revents = revents;
1033 return send_reply_ok(req, &arg,
sizeof(arg));
1038 char *name = (
char *) inarg;
1040 if (req->se->op.lookup)
1041 req->se->op.lookup(req, nodeid, name);
1048 struct fuse_forget_in *arg = (
struct fuse_forget_in *) inarg;
1050 if (req->se->op.forget)
1051 req->se->op.forget(req, nodeid, arg->nlookup);
1059 struct fuse_batch_forget_in *arg = (
void *) inarg;
1060 struct fuse_forget_one *param = (
void *) PARAM(arg);
1065 if (req->se->op.forget_multi) {
1066 req->se->op.forget_multi(req, arg->count,
1067 (
struct fuse_forget_data *) param);
1068 }
else if (req->se->op.forget) {
1069 for (i = 0; i < arg->count; i++) {
1070 struct fuse_forget_one *forget = ¶m[i];
1071 struct fuse_req *dummy_req;
1073 dummy_req = fuse_ll_alloc_req(req->se);
1074 if (dummy_req == NULL)
1077 dummy_req->unique = req->unique;
1078 dummy_req->ctx = req->ctx;
1079 dummy_req->ch = NULL;
1081 req->se->op.forget(dummy_req, forget->nodeid,
1095 if (req->se->conn.proto_minor >= 9) {
1096 struct fuse_getattr_in *arg = (
struct fuse_getattr_in *) inarg;
1098 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1099 memset(&fi, 0,
sizeof(fi));
1105 if (req->se->op.getattr)
1106 req->se->op.getattr(req, nodeid, fip);
1113 struct fuse_setattr_in *arg = (
struct fuse_setattr_in *) inarg;
1115 if (req->se->op.setattr) {
1119 memset(&stbuf, 0,
sizeof(stbuf));
1120 convert_attr(arg, &stbuf);
1121 if (arg->valid & FATTR_FH) {
1122 arg->valid &= ~FATTR_FH;
1123 memset(&fi_store, 0,
sizeof(fi_store));
1128 FUSE_SET_ATTR_MODE |
1131 FUSE_SET_ATTR_SIZE |
1132 FUSE_SET_ATTR_ATIME |
1133 FUSE_SET_ATTR_MTIME |
1134 FUSE_SET_ATTR_ATIME_NOW |
1135 FUSE_SET_ATTR_MTIME_NOW |
1136 FUSE_SET_ATTR_CTIME;
1138 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1145 struct fuse_access_in *arg = (
struct fuse_access_in *) inarg;
1147 if (req->se->op.access)
1148 req->se->op.access(req, nodeid, arg->mask);
1157 if (req->se->op.readlink)
1158 req->se->op.readlink(req, nodeid);
1165 struct fuse_mknod_in *arg = (
struct fuse_mknod_in *) inarg;
1166 char *name = PARAM(arg);
1168 if (req->se->conn.proto_minor >= 12)
1169 req->ctx.umask = arg->umask;
1171 name = (
char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1173 if (req->se->op.mknod)
1174 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1181 struct fuse_mkdir_in *arg = (
struct fuse_mkdir_in *) inarg;
1183 if (req->se->conn.proto_minor >= 12)
1184 req->ctx.umask = arg->umask;
1186 if (req->se->op.mkdir)
1187 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1194 char *name = (
char *) inarg;
1196 if (req->se->op.unlink)
1197 req->se->op.unlink(req, nodeid, name);
1204 char *name = (
char *) inarg;
1206 if (req->se->op.rmdir)
1207 req->se->op.rmdir(req, nodeid, name);
1214 char *name = (
char *) inarg;
1215 char *linkname = ((
char *) inarg) + strlen((
char *) inarg) + 1;
1217 if (req->se->op.symlink)
1218 req->se->op.symlink(req, linkname, nodeid, name);
1225 struct fuse_rename_in *arg = (
struct fuse_rename_in *) inarg;
1226 char *oldname = PARAM(arg);
1227 char *newname = oldname + strlen(oldname) + 1;
1229 if (req->se->op.rename)
1230 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1238 struct fuse_rename2_in *arg = (
struct fuse_rename2_in *) inarg;
1239 char *oldname = PARAM(arg);
1240 char *newname = oldname + strlen(oldname) + 1;
1242 if (req->se->op.rename)
1243 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1251 struct fuse_link_in *arg = (
struct fuse_link_in *) inarg;
1253 if (req->se->op.link)
1254 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1261 struct fuse_create_in *arg = (
struct fuse_create_in *) inarg;
1263 if (req->se->op.create) {
1265 char *name = PARAM(arg);
1267 memset(&fi, 0,
sizeof(fi));
1268 fi.
flags = arg->flags;
1270 if (req->se->conn.proto_minor >= 12)
1271 req->ctx.umask = arg->umask;
1273 name = (
char *) inarg +
sizeof(
struct fuse_open_in);
1275 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1282 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1285 memset(&fi, 0,
sizeof(fi));
1286 fi.
flags = arg->flags;
1288 if (req->se->op.open)
1289 req->se->op.open(req, nodeid, &fi);
1296 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1298 if (req->se->op.read) {
1301 memset(&fi, 0,
sizeof(fi));
1303 if (req->se->conn.proto_minor >= 9) {
1305 fi.
flags = arg->flags;
1307 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1314 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1318 memset(&fi, 0,
sizeof(fi));
1320 fi.
writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1322 if (req->se->conn.proto_minor < 9) {
1323 param = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1326 fi.
flags = arg->flags;
1330 if (req->se->op.write)
1331 req->se->op.write(req, nodeid, param, arg->size,
1340 struct fuse_session *se = req->se;
1345 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1348 memset(&fi, 0,
sizeof(fi));
1350 fi.
writepage = arg->write_flags & FUSE_WRITE_CACHE;
1352 if (se->conn.proto_minor < 9) {
1353 bufv.
buf[0].
mem = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1354 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1355 FUSE_COMPAT_WRITE_IN_SIZE;
1359 fi.
flags = arg->flags;
1361 bufv.
buf[0].
mem = PARAM(arg);
1363 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1364 sizeof(struct fuse_write_in);
1366 if (bufv.
buf[0].
size < arg->size) {
1367 fprintf(stderr,
"fuse: do_write_buf: buffer size too small\n");
1373 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1378 fuse_ll_clear_pipe(se);
1383 struct fuse_flush_in *arg = (
struct fuse_flush_in *) inarg;
1386 memset(&fi, 0,
sizeof(fi));
1389 if (req->se->conn.proto_minor >= 7)
1392 if (req->se->op.flush)
1393 req->se->op.flush(req, nodeid, &fi);
1400 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1403 memset(&fi, 0,
sizeof(fi));
1404 fi.
flags = arg->flags;
1406 if (req->se->conn.proto_minor >= 8) {
1407 fi.
flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1410 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1411 fi.flock_release = 1;
1415 if (req->se->op.release)
1416 req->se->op.release(req, nodeid, &fi);
1423 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1425 int datasync = arg->fsync_flags & 1;
1427 memset(&fi, 0,
sizeof(fi));
1430 if (req->se->op.fsync)
1431 req->se->op.fsync(req, nodeid, datasync, &fi);
1438 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1441 memset(&fi, 0,
sizeof(fi));
1442 fi.
flags = arg->flags;
1444 if (req->se->op.opendir)
1445 req->se->op.opendir(req, nodeid, &fi);
1452 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1455 memset(&fi, 0,
sizeof(fi));
1458 if (req->se->op.readdir)
1459 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1466 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1469 memset(&fi, 0,
sizeof(fi));
1472 if (req->se->op.readdirplus)
1473 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1480 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1483 memset(&fi, 0,
sizeof(fi));
1484 fi.
flags = arg->flags;
1487 if (req->se->op.releasedir)
1488 req->se->op.releasedir(req, nodeid, &fi);
1495 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1497 int datasync = arg->fsync_flags & 1;
1499 memset(&fi, 0,
sizeof(fi));
1502 if (req->se->op.fsyncdir)
1503 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1513 if (req->se->op.statfs)
1514 req->se->op.statfs(req, nodeid);
1516 struct statvfs buf = {
1526 struct fuse_setxattr_in *arg = (
struct fuse_setxattr_in *) inarg;
1527 char *name = PARAM(arg);
1528 char *value = name + strlen(name) + 1;
1530 if (req->se->op.setxattr)
1531 req->se->op.setxattr(req, nodeid, name, value, arg->size,
1539 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1541 if (req->se->op.getxattr)
1542 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1549 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1551 if (req->se->op.listxattr)
1552 req->se->op.listxattr(req, nodeid, arg->size);
1559 char *name = (
char *) inarg;
1561 if (req->se->op.removexattr)
1562 req->se->op.removexattr(req, nodeid, name);
1567 static void convert_fuse_file_lock(
struct fuse_file_lock *fl,
1568 struct flock *flock)
1570 memset(flock, 0,
sizeof(
struct flock));
1571 flock->l_type = fl->type;
1572 flock->l_whence = SEEK_SET;
1573 flock->l_start = fl->start;
1574 if (fl->end == OFFSET_MAX)
1577 flock->l_len = fl->end - fl->start + 1;
1578 flock->l_pid = fl->pid;
1583 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1587 memset(&fi, 0,
sizeof(fi));
1591 convert_fuse_file_lock(&arg->lk, &flock);
1592 if (req->se->op.getlk)
1593 req->se->op.getlk(req, nodeid, &fi, &flock);
1599 const void *inarg,
int sleep)
1601 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1605 memset(&fi, 0,
sizeof(fi));
1609 if (arg->lk_flags & FUSE_LK_FLOCK) {
1612 switch (arg->lk.type) {
1626 if (req->se->op.flock)
1627 req->se->op.flock(req, nodeid, &fi, op);
1631 convert_fuse_file_lock(&arg->lk, &flock);
1632 if (req->se->op.setlk)
1633 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1641 do_setlk_common(req, nodeid, inarg, 0);
1646 do_setlk_common(req, nodeid, inarg, 1);
1649 static int find_interrupted(
struct fuse_session *se,
struct fuse_req *req)
1651 struct fuse_req *curr;
1653 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1654 if (curr->unique == req->u.i.unique) {
1659 pthread_mutex_unlock(&se->lock);
1662 pthread_mutex_lock(&curr->lock);
1663 pthread_mutex_lock(&se->lock);
1664 curr->interrupted = 1;
1665 func = curr->u.ni.func;
1666 data = curr->u.ni.data;
1667 pthread_mutex_unlock(&se->lock);
1670 pthread_mutex_unlock(&curr->lock);
1672 pthread_mutex_lock(&se->lock);
1680 for (curr = se->interrupts.next; curr != &se->interrupts;
1681 curr = curr->next) {
1682 if (curr->u.i.unique == req->u.i.unique)
1690 struct fuse_interrupt_in *arg = (
struct fuse_interrupt_in *) inarg;
1691 struct fuse_session *se = req->se;
1695 fprintf(stderr,
"INTERRUPT: %llu\n",
1696 (
unsigned long long) arg->unique);
1698 req->u.i.unique = arg->unique;
1700 pthread_mutex_lock(&se->lock);
1701 if (find_interrupted(se, req))
1704 list_add_req(req, &se->interrupts);
1705 pthread_mutex_unlock(&se->lock);
1708 static struct fuse_req *check_interrupt(
struct fuse_session *se,
1709 struct fuse_req *req)
1711 struct fuse_req *curr;
1713 for (curr = se->interrupts.next; curr != &se->interrupts;
1714 curr = curr->next) {
1715 if (curr->u.i.unique == req->unique) {
1716 req->interrupted = 1;
1722 curr = se->interrupts.next;
1723 if (curr != &se->interrupts) {
1725 list_init_req(curr);
1733 struct fuse_bmap_in *arg = (
struct fuse_bmap_in *) inarg;
1735 if (req->se->op.bmap)
1736 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1743 struct fuse_ioctl_in *arg = (
struct fuse_ioctl_in *) inarg;
1744 unsigned int flags = arg->flags;
1745 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1748 if (flags & FUSE_IOCTL_DIR &&
1754 memset(&fi, 0,
sizeof(fi));
1757 if (
sizeof(
void *) == 4 && req->se->conn.proto_minor >= 16 &&
1758 !(flags & FUSE_IOCTL_32BIT)) {
1759 req->ioctl_64bit = 1;
1762 if (req->se->op.ioctl)
1763 req->se->op.ioctl(req, nodeid, arg->cmd,
1764 (
void *)(uintptr_t)arg->arg, &fi, flags,
1765 in_buf, arg->in_size, arg->out_size);
1777 struct fuse_poll_in *arg = (
struct fuse_poll_in *) inarg;
1780 memset(&fi, 0,
sizeof(fi));
1784 if (req->se->op.poll) {
1785 struct fuse_pollhandle *ph = NULL;
1787 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1788 ph = malloc(
sizeof(
struct fuse_pollhandle));
1797 req->se->op.poll(req, nodeid, &fi, ph);
1805 struct fuse_fallocate_in *arg = (
struct fuse_fallocate_in *) inarg;
1808 memset(&fi, 0,
sizeof(fi));
1811 if (req->se->op.fallocate)
1812 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1819 struct fuse_copy_file_range_in *arg = (
struct fuse_copy_file_range_in *) inarg;
1822 memset(&fi_in, 0,
sizeof(fi_in));
1823 fi_in.
fh = arg->fh_in;
1825 memset(&fi_out, 0,
sizeof(fi_out));
1826 fi_out.
fh = arg->fh_out;
1829 if (req->se->op.copy_file_range)
1830 req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
1831 &fi_in, arg->nodeid_out,
1832 arg->off_out, &fi_out, arg->len,
1840 struct fuse_init_in *arg = (
struct fuse_init_in *) inarg;
1841 struct fuse_init_out outarg;
1842 struct fuse_session *se = req->se;
1843 size_t bufsize = se->bufsize;
1844 size_t outargsize =
sizeof(outarg);
1848 fprintf(stderr,
"INIT: %u.%u\n", arg->major, arg->minor);
1849 if (arg->major == 7 && arg->minor >= 6) {
1850 fprintf(stderr,
"flags=0x%08x\n", arg->flags);
1851 fprintf(stderr,
"max_readahead=0x%08x\n",
1852 arg->max_readahead);
1855 se->conn.proto_major = arg->major;
1856 se->conn.proto_minor = arg->minor;
1857 se->conn.capable = 0;
1860 memset(&outarg, 0,
sizeof(outarg));
1861 outarg.major = FUSE_KERNEL_VERSION;
1862 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1864 if (arg->major < 7) {
1865 fprintf(stderr,
"fuse: unsupported protocol version: %u.%u\n",
1866 arg->major, arg->minor);
1871 if (arg->major > 7) {
1873 send_reply_ok(req, &outarg,
sizeof(outarg));
1877 if (arg->minor >= 6) {
1878 if (arg->max_readahead < se->conn.max_readahead)
1879 se->conn.max_readahead = arg->max_readahead;
1880 if (arg->flags & FUSE_ASYNC_READ)
1882 if (arg->flags & FUSE_POSIX_LOCKS)
1884 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1886 if (arg->flags & FUSE_EXPORT_SUPPORT)
1888 if (arg->flags & FUSE_DONT_MASK)
1890 if (arg->flags & FUSE_FLOCK_LOCKS)
1892 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1894 if (arg->flags & FUSE_DO_READDIRPLUS)
1896 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1898 if (arg->flags & FUSE_ASYNC_DIO)
1900 if (arg->flags & FUSE_WRITEBACK_CACHE)
1902 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1904 if (arg->flags & FUSE_PARALLEL_DIROPS)
1906 if (arg->flags & FUSE_POSIX_ACL)
1908 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1910 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT)
1912 if (!(arg->flags & FUSE_MAX_PAGES)) {
1913 size_t max_bufsize =
1914 FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
1915 + FUSE_BUFFER_HEADER_SIZE;
1916 if (bufsize > max_bufsize) {
1917 bufsize = max_bufsize;
1921 se->conn.max_readahead = 0;
1924 if (se->conn.proto_minor >= 14) {
1926 #ifdef HAVE_VMSPLICE 1932 if (se->conn.proto_minor >= 18)
1942 #define LL_SET_DEFAULT(cond, cap) \ 1943 if ((cond) && (se->conn.capable & (cap))) \ 1944 se->conn.want |= (cap) 1953 LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
1957 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1959 se->conn.time_gran = 1;
1961 if (bufsize < FUSE_MIN_READ_BUFFER) {
1962 fprintf(stderr,
"fuse: warning: buffer size too small: %zu\n",
1964 bufsize = FUSE_MIN_READ_BUFFER;
1966 se->bufsize = bufsize;
1968 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
1969 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
1973 se->op.init(se->userdata, &se->conn);
1975 if (se->conn.want & (~se->conn.capable)) {
1976 fprintf(stderr,
"fuse: error: filesystem requested capabilities " 1977 "0x%x that are not supported by kernel, aborting.\n",
1978 se->conn.want & (~se->conn.capable));
1980 se->error = -EPROTO;
1985 unsigned max_read_mo = get_max_read(se->mo);
1986 if (se->conn.max_read != max_read_mo) {
1987 fprintf(stderr,
"fuse: error: init() and fuse_session_new() " 1988 "requested different maximum read size (%u vs %u)\n",
1989 se->conn.max_read, max_read_mo);
1991 se->error = -EPROTO;
1996 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
1997 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
1999 if (arg->flags & FUSE_MAX_PAGES) {
2000 outarg.flags |= FUSE_MAX_PAGES;
2001 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2006 outarg.flags |= FUSE_BIG_WRITES;
2009 outarg.flags |= FUSE_ASYNC_READ;
2011 outarg.flags |= FUSE_POSIX_LOCKS;
2013 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2015 outarg.flags |= FUSE_EXPORT_SUPPORT;
2017 outarg.flags |= FUSE_DONT_MASK;
2019 outarg.flags |= FUSE_FLOCK_LOCKS;
2021 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2023 outarg.flags |= FUSE_DO_READDIRPLUS;
2025 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2027 outarg.flags |= FUSE_ASYNC_DIO;
2029 outarg.flags |= FUSE_WRITEBACK_CACHE;
2031 outarg.flags |= FUSE_POSIX_ACL;
2032 outarg.max_readahead = se->conn.max_readahead;
2033 outarg.max_write = se->conn.max_write;
2034 if (se->conn.proto_minor >= 13) {
2035 if (se->conn.max_background >= (1 << 16))
2036 se->conn.max_background = (1 << 16) - 1;
2037 if (se->conn.congestion_threshold > se->conn.max_background)
2038 se->conn.congestion_threshold = se->conn.max_background;
2039 if (!se->conn.congestion_threshold) {
2040 se->conn.congestion_threshold =
2041 se->conn.max_background * 3 / 4;
2044 outarg.max_background = se->conn.max_background;
2045 outarg.congestion_threshold = se->conn.congestion_threshold;
2047 if (se->conn.proto_minor >= 23)
2048 outarg.time_gran = se->conn.time_gran;
2051 fprintf(stderr,
" INIT: %u.%u\n", outarg.major, outarg.minor);
2052 fprintf(stderr,
" flags=0x%08x\n", outarg.flags);
2053 fprintf(stderr,
" max_readahead=0x%08x\n",
2054 outarg.max_readahead);
2055 fprintf(stderr,
" max_write=0x%08x\n", outarg.max_write);
2056 fprintf(stderr,
" max_background=%i\n",
2057 outarg.max_background);
2058 fprintf(stderr,
" congestion_threshold=%i\n",
2059 outarg.congestion_threshold);
2060 fprintf(stderr,
" time_gran=%u\n",
2064 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2065 else if (arg->minor < 23)
2066 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2068 send_reply_ok(req, &outarg, outargsize);
2073 struct fuse_session *se = req->se;
2078 se->got_destroy = 1;
2080 se->op.destroy(se->userdata);
2082 send_reply_ok(req, NULL, 0);
2085 static void list_del_nreq(
struct fuse_notify_req *nreq)
2087 struct fuse_notify_req *prev = nreq->prev;
2088 struct fuse_notify_req *next = nreq->next;
2093 static void list_add_nreq(
struct fuse_notify_req *nreq,
2094 struct fuse_notify_req *next)
2096 struct fuse_notify_req *prev = next->prev;
2103 static void list_init_nreq(
struct fuse_notify_req *nreq)
2110 const void *inarg,
const struct fuse_buf *buf)
2112 struct fuse_session *se = req->se;
2113 struct fuse_notify_req *nreq;
2114 struct fuse_notify_req *head;
2116 pthread_mutex_lock(&se->lock);
2117 head = &se->notify_list;
2118 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2119 if (nreq->unique == req->unique) {
2120 list_del_nreq(nreq);
2124 pthread_mutex_unlock(&se->lock);
2127 nreq->reply(nreq, req, nodeid, inarg, buf);
2130 static int send_notify_iov(
struct fuse_session *se,
int notify_code,
2131 struct iovec *iov,
int count)
2133 struct fuse_out_header out;
2139 out.error = notify_code;
2140 iov[0].iov_base = &out;
2141 iov[0].iov_len =
sizeof(
struct fuse_out_header);
2143 return fuse_send_msg(se, NULL, iov, count);
2149 struct fuse_notify_poll_wakeup_out outarg;
2150 struct iovec iov[2];
2154 iov[1].iov_base = &outarg;
2155 iov[1].iov_len =
sizeof(outarg);
2157 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2164 off_t off, off_t len)
2166 struct fuse_notify_inval_inode_out outarg;
2167 struct iovec iov[2];
2172 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2179 iov[1].iov_base = &outarg;
2180 iov[1].iov_len =
sizeof(outarg);
2182 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2186 const char *name,
size_t namelen)
2188 struct fuse_notify_inval_entry_out outarg;
2189 struct iovec iov[3];
2194 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2197 outarg.parent = parent;
2198 outarg.namelen = namelen;
2201 iov[1].iov_base = &outarg;
2202 iov[1].iov_len =
sizeof(outarg);
2203 iov[2].iov_base = (
void *)name;
2204 iov[2].iov_len = namelen + 1;
2206 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2211 const char *name,
size_t namelen)
2213 struct fuse_notify_delete_out outarg;
2214 struct iovec iov[3];
2219 if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
2222 outarg.parent = parent;
2223 outarg.child = child;
2224 outarg.namelen = namelen;
2227 iov[1].iov_base = &outarg;
2228 iov[1].iov_len =
sizeof(outarg);
2229 iov[2].iov_base = (
void *)name;
2230 iov[2].iov_len = namelen + 1;
2232 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2239 struct fuse_out_header out;
2240 struct fuse_notify_store_out outarg;
2241 struct iovec iov[3];
2248 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2252 out.error = FUSE_NOTIFY_STORE;
2254 outarg.nodeid = ino;
2255 outarg.offset = offset;
2259 iov[0].iov_base = &out;
2260 iov[0].iov_len =
sizeof(out);
2261 iov[1].iov_base = &outarg;
2262 iov[1].iov_len =
sizeof(outarg);
2264 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2271 struct fuse_retrieve_req {
2272 struct fuse_notify_req nreq;
2276 static void fuse_ll_retrieve_reply(
struct fuse_notify_req *nreq,
2281 struct fuse_session *se = req->se;
2282 struct fuse_retrieve_req *rreq =
2283 container_of(nreq,
struct fuse_retrieve_req, nreq);
2284 const struct fuse_notify_retrieve_in *arg = inarg;
2291 bufv.
buf[0].
mem = PARAM(arg);
2293 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
2294 sizeof(struct fuse_notify_retrieve_in);
2296 if (bufv.
buf[0].
size < arg->size) {
2297 fprintf(stderr,
"fuse: retrieve reply: buffer size too small\n");
2303 if (se->op.retrieve_reply) {
2304 se->op.retrieve_reply(req, rreq->cookie, ino,
2305 arg->offset, &bufv);
2312 fuse_ll_clear_pipe(se);
2316 size_t size, off_t offset,
void *cookie)
2318 struct fuse_notify_retrieve_out outarg;
2319 struct iovec iov[2];
2320 struct fuse_retrieve_req *rreq;
2326 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2329 rreq = malloc(
sizeof(*rreq));
2333 pthread_mutex_lock(&se->lock);
2334 rreq->cookie = cookie;
2335 rreq->nreq.unique = se->notify_ctr++;
2336 rreq->nreq.reply = fuse_ll_retrieve_reply;
2337 list_add_nreq(&rreq->nreq, &se->notify_list);
2338 pthread_mutex_unlock(&se->lock);
2340 outarg.notify_unique = rreq->nreq.unique;
2341 outarg.nodeid = ino;
2342 outarg.offset = offset;
2346 iov[1].iov_base = &outarg;
2347 iov[1].iov_len =
sizeof(outarg);
2349 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2351 pthread_mutex_lock(&se->lock);
2352 list_del_nreq(&rreq->nreq);
2353 pthread_mutex_unlock(&se->lock);
2362 return req->se->userdata;
2373 pthread_mutex_lock(&req->lock);
2374 pthread_mutex_lock(&req->se->lock);
2375 req->u.ni.func = func;
2376 req->u.ni.data = data;
2377 pthread_mutex_unlock(&req->se->lock);
2378 if (req->interrupted && func)
2380 pthread_mutex_unlock(&req->lock);
2387 pthread_mutex_lock(&req->se->lock);
2388 interrupted = req->interrupted;
2389 pthread_mutex_unlock(&req->se->lock);
2398 [FUSE_LOOKUP] = { do_lookup,
"LOOKUP" },
2399 [FUSE_FORGET] = { do_forget,
"FORGET" },
2400 [FUSE_GETATTR] = { do_getattr,
"GETATTR" },
2401 [FUSE_SETATTR] = { do_setattr,
"SETATTR" },
2402 [FUSE_READLINK] = { do_readlink,
"READLINK" },
2403 [FUSE_SYMLINK] = { do_symlink,
"SYMLINK" },
2404 [FUSE_MKNOD] = { do_mknod,
"MKNOD" },
2405 [FUSE_MKDIR] = { do_mkdir,
"MKDIR" },
2406 [FUSE_UNLINK] = { do_unlink,
"UNLINK" },
2407 [FUSE_RMDIR] = { do_rmdir,
"RMDIR" },
2408 [FUSE_RENAME] = { do_rename,
"RENAME" },
2409 [FUSE_LINK] = { do_link,
"LINK" },
2410 [FUSE_OPEN] = { do_open,
"OPEN" },
2411 [FUSE_READ] = { do_read,
"READ" },
2412 [FUSE_WRITE] = { do_write,
"WRITE" },
2413 [FUSE_STATFS] = { do_statfs,
"STATFS" },
2414 [FUSE_RELEASE] = { do_release,
"RELEASE" },
2415 [FUSE_FSYNC] = { do_fsync,
"FSYNC" },
2416 [FUSE_SETXATTR] = { do_setxattr,
"SETXATTR" },
2417 [FUSE_GETXATTR] = { do_getxattr,
"GETXATTR" },
2418 [FUSE_LISTXATTR] = { do_listxattr,
"LISTXATTR" },
2419 [FUSE_REMOVEXATTR] = { do_removexattr,
"REMOVEXATTR" },
2420 [FUSE_FLUSH] = { do_flush,
"FLUSH" },
2421 [FUSE_INIT] = { do_init,
"INIT" },
2422 [FUSE_OPENDIR] = { do_opendir,
"OPENDIR" },
2423 [FUSE_READDIR] = { do_readdir,
"READDIR" },
2424 [FUSE_RELEASEDIR] = { do_releasedir,
"RELEASEDIR" },
2425 [FUSE_FSYNCDIR] = { do_fsyncdir,
"FSYNCDIR" },
2426 [FUSE_GETLK] = { do_getlk,
"GETLK" },
2427 [FUSE_SETLK] = { do_setlk,
"SETLK" },
2428 [FUSE_SETLKW] = { do_setlkw,
"SETLKW" },
2429 [FUSE_ACCESS] = { do_access,
"ACCESS" },
2430 [FUSE_CREATE] = { do_create,
"CREATE" },
2431 [FUSE_INTERRUPT] = { do_interrupt,
"INTERRUPT" },
2432 [FUSE_BMAP] = { do_bmap,
"BMAP" },
2433 [FUSE_IOCTL] = { do_ioctl,
"IOCTL" },
2434 [FUSE_POLL] = { do_poll,
"POLL" },
2435 [FUSE_FALLOCATE] = { do_fallocate,
"FALLOCATE" },
2436 [FUSE_DESTROY] = { do_destroy,
"DESTROY" },
2437 [FUSE_NOTIFY_REPLY] = { (
void *) 1,
"NOTIFY_REPLY" },
2438 [FUSE_BATCH_FORGET] = { do_batch_forget,
"BATCH_FORGET" },
2439 [FUSE_READDIRPLUS] = { do_readdirplus,
"READDIRPLUS"},
2440 [FUSE_RENAME2] = { do_rename2,
"RENAME2" },
2441 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range,
"COPY_FILE_RANGE" },
2442 [CUSE_INIT] = { cuse_lowlevel_init,
"CUSE_INIT" },
2445 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) 2447 static const char *opname(
enum fuse_opcode opcode)
2449 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2452 return fuse_ll_ops[opcode].name;
2455 static int fuse_ll_copy_from_pipe(
struct fuse_bufvec *dst,
2460 fprintf(stderr,
"fuse: copy from pipe: %s\n", strerror(-res));
2464 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2473 fuse_session_process_buf_int(se, buf, NULL);
2476 void fuse_session_process_buf_int(
struct fuse_session *se,
2477 const struct fuse_buf *buf,
struct fuse_chan *ch)
2479 const size_t write_header_size =
sizeof(
struct fuse_in_header) +
2480 sizeof(struct fuse_write_in);
2482 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2483 struct fuse_in_header *in;
2485 struct fuse_req *req;
2494 mbuf = malloc(tmpbuf.
buf[0].
size);
2496 fprintf(stderr,
"fuse: failed to allocate header\n");
2499 tmpbuf.
buf[0].
mem = mbuf;
2501 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2512 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2513 (
unsigned long long) in->unique,
2514 opname((
enum fuse_opcode) in->opcode), in->opcode,
2515 (
unsigned long long) in->nodeid, buf->
size, in->pid);
2518 req = fuse_ll_alloc_req(se);
2520 struct fuse_out_header out = {
2521 .unique = in->unique,
2524 struct iovec iov = {
2526 .iov_len =
sizeof(
struct fuse_out_header),
2529 fuse_send_msg(se, ch, &iov, 1);
2533 req->unique = in->unique;
2534 req->ctx.uid = in->uid;
2535 req->ctx.gid = in->gid;
2536 req->ctx.pid = in->pid;
2537 req->ch = ch ? fuse_chan_get(ch) : NULL;
2540 if (!se->got_init) {
2541 enum fuse_opcode expected;
2543 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2544 if (in->opcode != expected)
2546 }
else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2551 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2552 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2553 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2554 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2555 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2556 in->opcode != FUSE_NOTIFY_REPLY &&
2557 in->opcode != FUSE_READDIRPLUS)
2561 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2563 if (in->opcode != FUSE_INTERRUPT) {
2564 struct fuse_req *intr;
2565 pthread_mutex_lock(&se->lock);
2566 intr = check_interrupt(se, req);
2567 list_add_req(req, &se->list);
2568 pthread_mutex_unlock(&se->lock);
2574 (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2575 in->opcode != FUSE_NOTIFY_REPLY) {
2579 newmbuf = realloc(mbuf, buf->
size);
2580 if (newmbuf == NULL)
2584 tmpbuf = FUSE_BUFVEC_INIT(buf->
size - write_header_size);
2585 tmpbuf.
buf[0].
mem = (
char *)mbuf + write_header_size;
2587 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2595 inarg = (
void *) &in[1];
2596 if (in->opcode == FUSE_WRITE && se->op.write_buf)
2597 do_write_buf(req, in->nodeid, inarg, buf);
2598 else if (in->opcode == FUSE_NOTIFY_REPLY)
2599 do_notify_reply(req, in->nodeid, inarg, buf);
2601 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2611 fuse_ll_clear_pipe(se);
2615 #define LL_OPTION(n,o,v) \ 2616 { n, offsetof(struct fuse_session, o), v } 2618 static const struct fuse_opt fuse_ll_opts[] = {
2619 LL_OPTION(
"debug", debug, 1),
2620 LL_OPTION(
"-d", debug, 1),
2621 LL_OPTION(
"--debug", debug, 1),
2622 LL_OPTION(
"allow_root", deny_others, 1),
2628 printf(
"using FUSE kernel interface version %i.%i\n",
2629 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2630 fuse_mount_version();
2638 " -o allow_other allow access by all users\n" 2639 " -o allow_root allow access by root\n" 2640 " -o auto_unmount auto unmount on process termination\n");
2645 struct fuse_ll_pipe *llp;
2647 if (se->got_init && !se->got_destroy) {
2649 se->op.destroy(se->userdata);
2651 llp = pthread_getspecific(se->pipe_key);
2653 fuse_ll_pipe_free(llp);
2654 pthread_key_delete(se->pipe_key);
2655 pthread_mutex_destroy(&se->lock);
2656 free(se->cuse_data);
2659 destroy_mount_opts(se->mo);
2664 static void fuse_ll_pipe_destructor(
void *data)
2666 struct fuse_ll_pipe *llp = data;
2667 fuse_ll_pipe_free(llp);
2672 return fuse_session_receive_buf_int(se, buf, NULL);
2675 int fuse_session_receive_buf_int(
struct fuse_session *se,
struct fuse_buf *buf,
2676 struct fuse_chan *ch)
2681 size_t bufsize = se->bufsize;
2682 struct fuse_ll_pipe *llp;
2688 llp = fuse_ll_get_pipe(se);
2692 if (llp->size < bufsize) {
2693 if (llp->can_grow) {
2694 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2701 if (llp->size < bufsize)
2705 res = splice(ch ? ch->fd : se->fd,
2706 NULL, llp->pipe[1], NULL, bufsize, 0);
2713 if (err == ENODEV) {
2719 if (err != EINTR && err != EAGAIN)
2720 perror(
"fuse: splice from device");
2724 if (res <
sizeof(
struct fuse_in_header)) {
2725 fprintf(stderr,
"short splice from fuse device\n");
2740 if (res <
sizeof(
struct fuse_in_header) +
2741 sizeof(
struct fuse_write_in) + pagesize) {
2746 buf->
mem = malloc(se->bufsize);
2749 "fuse: failed to allocate read buffer\n");
2753 buf->
size = se->bufsize;
2759 fprintf(stderr,
"fuse: copy from pipe: %s\n",
2761 fuse_ll_clear_pipe(se);
2764 if (res < tmpbuf.
size) {
2765 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2766 fuse_ll_clear_pipe(se);
2769 assert(res == tmpbuf.
size);
2773 buf->
fd = tmpbuf.
fd;
2783 buf->
mem = malloc(se->bufsize);
2786 "fuse: failed to allocate read buffer\n");
2792 res = read(ch ? ch->fd : se->fd, buf->
mem, se->bufsize);
2803 if (err == ENODEV) {
2812 if (err != EINTR && err != EAGAIN)
2813 perror(
"fuse: reading device");
2816 if ((
size_t) res <
sizeof(
struct fuse_in_header)) {
2817 fprintf(stderr,
"short read on fuse device\n");
2828 size_t op_size,
void *userdata)
2831 struct fuse_session *se;
2832 struct mount_opts *mo;
2835 fprintf(stderr,
"fuse: warning: library too old, some operations may not work\n");
2839 if (args->
argc == 0) {
2840 fprintf(stderr,
"fuse: empty argv passed to fuse_session_new().\n");
2844 se = (
struct fuse_session *) calloc(1,
sizeof(
struct fuse_session));
2846 fprintf(stderr,
"fuse: failed to allocate fuse object\n");
2850 se->conn.max_write = UINT_MAX;
2851 se->conn.max_readahead = UINT_MAX;
2856 if(se->deny_others) {
2866 mo = parse_mount_opts(args);
2870 if(args->
argc == 1 &&
2871 args->
argv[0][0] ==
'-') {
2872 fprintf(stderr,
"fuse: warning: argv[0] looks like an option, but " 2873 "will be ignored\n");
2874 }
else if (args->
argc != 1) {
2876 fprintf(stderr,
"fuse: unknown option(s): `");
2877 for(i = 1; i < args->
argc-1; i++)
2878 fprintf(stderr,
"%s ", args->
argv[i]);
2879 fprintf(stderr,
"%s'\n", args->
argv[i]);
2884 fprintf(stderr,
"FUSE library version: %s\n", PACKAGE_VERSION);
2886 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
2887 FUSE_BUFFER_HEADER_SIZE;
2889 list_init_req(&se->list);
2890 list_init_req(&se->interrupts);
2891 list_init_nreq(&se->notify_list);
2893 fuse_mutex_init(&se->lock);
2895 err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
2897 fprintf(stderr,
"fuse: failed to create thread specific key: %s\n",
2902 memcpy(&se->op, op, op_size);
2903 se->owner = getuid();
2904 se->userdata = userdata;
2910 pthread_mutex_destroy(&se->lock);
2930 fd = open(
"/dev/null", O_RDWR);
2933 }
while (fd >= 0 && fd <= 2);
2941 fd = fuse_mnt_parse_fuse_fd(mountpoint);
2943 if (fcntl(fd, F_GETFD) == -1) {
2945 "fuse: Invalid file descriptor /dev/fd/%u\n",
2954 fd = fuse_kern_mount(mountpoint, se->mo);
2960 se->mountpoint = strdup(mountpoint);
2961 if (se->mountpoint == NULL)
2967 fuse_kern_unmount(mountpoint, fd);
2978 if (se->mountpoint != NULL) {
2979 fuse_kern_unmount(se->mountpoint, se->fd);
2980 free(se->mountpoint);
2981 se->mountpoint = NULL;
2989 size_t bufsize = 1024;
2993 unsigned long pid = req->ctx.pid;
2996 sprintf(path,
"/proc/%lu/task/%lu/status", pid, pid);
2999 buf = malloc(bufsize);
3004 fd = open(path, O_RDONLY);
3008 ret = read(fd, buf, bufsize);
3015 if ((
size_t)ret == bufsize) {
3022 s = strstr(buf,
"\nGroups:");
3030 unsigned long val = strtoul(s, &end, 0);
3050 (void) req; (void) size; (void) list;
const struct fuse_ctx * fuse_req_ctx(fuse_req_t req)
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
#define FUSE_CAP_ASYNC_DIO
int fuse_session_fd(struct fuse_session *se)
int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino, size_t size, off_t offset, void *cookie)
#define FUSE_CAP_EXPORT_SUPPORT
#define FUSE_CAP_PARALLEL_DIROPS
void fuse_session_unmount(struct fuse_session *se)
#define FUSE_CAP_AUTO_INVAL_DATA
void fuse_lowlevel_version(void)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent, fuse_ino_t child, const char *name, size_t namelen)
#define FUSE_CAP_NO_OPENDIR_SUPPORT
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
int fuse_req_interrupted(fuse_req_t req)
#define FUSE_CAP_HANDLE_KILLPRIV
int fuse_reply_poll(fuse_req_t req, unsigned revents)
#define FUSE_CAP_DONT_MASK
int fuse_reply_err(fuse_req_t req, int err)
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
void fuse_session_reset(struct fuse_session *se)
#define FUSE_CAP_ASYNC_READ
struct fuse_req * fuse_req_t
int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
void * fuse_req_userdata(fuse_req_t req)
#define FUSE_CAP_READDIRPLUS
#define FUSE_CAP_IOCTL_DIR
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
void fuse_reply_none(fuse_req_t req)
#define FUSE_CAP_FLOCK_LOCKS
#define FUSE_CAP_POSIX_LOCKS
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_destroy(struct fuse_session *se)
void fuse_session_exit(struct fuse_session *se)
void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
void(* fuse_interrupt_func_t)(fuse_req_t req, void *data)
size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct fuse_entry_param *e, off_t off)
int fuse_reply_readlink(fuse_req_t req, const char *link)
#define FUSE_CAP_SPLICE_MOVE
#define FUSE_CAP_ATOMIC_O_TRUNC
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, void *data)
#define FUSE_CAP_SPLICE_WRITE
int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
enum fuse_buf_flags flags
int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, int count)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
void fuse_lowlevel_help(void)
#define FUSE_CAP_POSIX_ACL
int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, off_t offset, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
#define FUSE_CAP_NO_OPEN_SUPPORT
int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov, size_t in_count, const struct iovec *out_iov, size_t out_count)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
unsigned int cache_readdir
int fuse_session_exited(struct fuse_session *se)
int fuse_reply_xattr(fuse_req_t req, size_t count)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
void fuse_session_process_buf(struct fuse_session *se, const struct fuse_buf *buf)
#define FUSE_CAP_WRITEBACK_CACHE
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
int fuse_reply_write(fuse_req_t req, size_t count)
#define FUSE_CAP_SPLICE_READ
int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
#define FUSE_CAP_READDIRPLUS_AUTO