16 #include "fuse_kernel.h" 18 #include "fuse_misc.h" 30 #ifndef F_LINUX_SPECIFIC_BASE 31 #define F_LINUX_SPECIFIC_BASE 1024 34 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7) 38 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) 39 #define OFFSET_MAX 0x7fffffffffffffffLL 41 #define container_of(ptr, type, member) ({ \ 42 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 43 (type *)( (char *)__mptr - offsetof(type,member) );}) 45 struct fuse_pollhandle {
47 struct fuse_session *se;
50 static size_t pagesize;
52 static __attribute__((constructor))
void fuse_ll_init_pagesize(
void)
54 pagesize = getpagesize();
57 static void convert_stat(
const struct stat *stbuf,
struct fuse_attr *attr)
59 attr->ino = stbuf->st_ino;
60 attr->mode = stbuf->st_mode;
61 attr->nlink = stbuf->st_nlink;
62 attr->uid = stbuf->st_uid;
63 attr->gid = stbuf->st_gid;
64 attr->rdev = stbuf->st_rdev;
65 attr->size = stbuf->st_size;
66 attr->blksize = stbuf->st_blksize;
67 attr->blocks = stbuf->st_blocks;
68 attr->atime = stbuf->st_atime;
69 attr->mtime = stbuf->st_mtime;
70 attr->ctime = stbuf->st_ctime;
71 attr->atimensec = ST_ATIM_NSEC(stbuf);
72 attr->mtimensec = ST_MTIM_NSEC(stbuf);
73 attr->ctimensec = ST_CTIM_NSEC(stbuf);
76 static void convert_attr(
const struct fuse_setattr_in *attr,
struct stat *stbuf)
78 stbuf->st_mode = attr->mode;
79 stbuf->st_uid = attr->uid;
80 stbuf->st_gid = attr->gid;
81 stbuf->st_size = attr->size;
82 stbuf->st_atime = attr->atime;
83 stbuf->st_mtime = attr->mtime;
84 stbuf->st_ctime = attr->ctime;
85 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
86 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
87 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
90 static size_t iov_length(
const struct iovec *iov,
size_t count)
95 for (seg = 0; seg < count; seg++)
96 ret += iov[seg].iov_len;
100 static void list_init_req(
struct fuse_req *req)
106 static void list_del_req(
struct fuse_req *req)
108 struct fuse_req *prev = req->prev;
109 struct fuse_req *next = req->next;
114 static void list_add_req(
struct fuse_req *req,
struct fuse_req *next)
116 struct fuse_req *prev = next->prev;
125 pthread_mutex_destroy(&req->lock);
132 struct fuse_session *se = req->se;
134 pthread_mutex_lock(&se->lock);
135 req->u.ni.func = NULL;
136 req->u.ni.data = NULL;
139 fuse_chan_put(req->ch);
141 pthread_mutex_unlock(&se->lock);
146 static struct fuse_req *fuse_ll_alloc_req(
struct fuse_session *se)
148 struct fuse_req *req;
150 req = (
struct fuse_req *) calloc(1,
sizeof(
struct fuse_req));
152 fprintf(stderr,
"fuse: failed to allocate request\n");
157 fuse_mutex_init(&req->lock);
164 static int fuse_send_msg(
struct fuse_session *se,
struct fuse_chan *ch,
165 struct iovec *iov,
int count)
167 struct fuse_out_header *out = iov[0].iov_base;
169 out->len = iov_length(iov, count);
171 if (out->unique == 0) {
172 fprintf(stderr,
"NOTIFY: code=%d length=%u\n",
173 out->error, out->len);
174 }
else if (out->error) {
176 " unique: %llu, error: %i (%s), outsize: %i\n",
177 (
unsigned long long) out->unique, out->error,
178 strerror(-out->error), out->len);
181 " unique: %llu, success, outsize: %i\n",
182 (
unsigned long long) out->unique, out->len);
186 ssize_t res = writev(ch ? ch->fd : se->fd,
195 perror(
"fuse: writing device");
203 int fuse_send_reply_iov_nofree(
fuse_req_t req,
int error,
struct iovec *iov,
206 struct fuse_out_header out;
208 if (error <= -1000 || error > 0) {
209 fprintf(stderr,
"fuse: bad error value: %i\n", error);
213 out.unique = req->unique;
216 iov[0].iov_base = &out;
217 iov[0].iov_len =
sizeof(
struct fuse_out_header);
219 return fuse_send_msg(req->se, req->ch, iov, count);
222 static int send_reply_iov(
fuse_req_t req,
int error,
struct iovec *iov,
227 res = fuse_send_reply_iov_nofree(req, error, iov, count);
232 static int send_reply(
fuse_req_t req,
int error,
const void *arg,
238 iov[1].iov_base = (
void *) arg;
239 iov[1].iov_len = argsize;
242 return send_reply_iov(req, error, iov, count);
248 struct iovec *padded_iov;
250 padded_iov = malloc((count + 1) *
sizeof(
struct iovec));
251 if (padded_iov == NULL)
254 memcpy(padded_iov + 1, iov, count *
sizeof(
struct iovec));
257 res = send_reply_iov(req, 0, padded_iov, count);
267 const char *name,
const struct stat *stbuf, off_t off)
272 size_t entlen_padded;
273 struct fuse_dirent *dirent;
275 namelen = strlen(name);
276 entlen = FUSE_NAME_OFFSET + namelen;
277 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
279 if ((buf == NULL) || (entlen_padded > bufsize))
280 return entlen_padded;
282 dirent = (
struct fuse_dirent*) buf;
283 dirent->ino = stbuf->st_ino;
285 dirent->namelen = namelen;
286 dirent->type = (stbuf->st_mode & 0170000) >> 12;
287 strncpy(dirent->name, name, namelen);
288 memset(dirent->name + namelen, 0, entlen_padded - entlen);
290 return entlen_padded;
293 static void convert_statfs(
const struct statvfs *stbuf,
294 struct fuse_kstatfs *kstatfs)
296 kstatfs->bsize = stbuf->f_bsize;
297 kstatfs->frsize = stbuf->f_frsize;
298 kstatfs->blocks = stbuf->f_blocks;
299 kstatfs->bfree = stbuf->f_bfree;
300 kstatfs->bavail = stbuf->f_bavail;
301 kstatfs->files = stbuf->f_files;
302 kstatfs->ffree = stbuf->f_ffree;
303 kstatfs->namelen = stbuf->f_namemax;
306 static int send_reply_ok(
fuse_req_t req,
const void *arg,
size_t argsize)
308 return send_reply(req, 0, arg, argsize);
313 return send_reply(req, -err, NULL, 0);
321 static unsigned long calc_timeout_sec(
double t)
323 if (t > (
double) ULONG_MAX)
328 return (
unsigned long) t;
331 static unsigned int calc_timeout_nsec(
double t)
333 double f = t - (double) calc_timeout_sec(t);
336 else if (f >= 0.999999999)
339 return (
unsigned int) (f * 1.0e9);
342 static void fill_entry(
struct fuse_entry_out *arg,
345 arg->nodeid = e->
ino;
350 arg->attr_valid_nsec = calc_timeout_nsec(e->
attr_timeout);
351 convert_stat(&e->
attr, &arg->attr);
363 size_t entlen_padded;
365 namelen = strlen(name);
366 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
367 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
368 if ((buf == NULL) || (entlen_padded > bufsize))
369 return entlen_padded;
371 struct fuse_direntplus *dp = (
struct fuse_direntplus *) buf;
372 memset(&dp->entry_out, 0,
sizeof(dp->entry_out));
373 fill_entry(&dp->entry_out, e);
375 struct fuse_dirent *dirent = &dp->dirent;
376 dirent->ino = e->
attr.st_ino;
378 dirent->namelen = namelen;
379 dirent->type = (e->
attr.st_mode & 0170000) >> 12;
380 strncpy(dirent->name, name, namelen);
381 memset(dirent->name + namelen, 0, entlen_padded - entlen);
383 return entlen_padded;
386 static void fill_open(
struct fuse_open_out *arg,
391 arg->open_flags |= FOPEN_DIRECT_IO;
393 arg->open_flags |= FOPEN_KEEP_CACHE;
395 arg->open_flags |= FOPEN_NONSEEKABLE;
400 struct fuse_entry_out arg;
401 size_t size = req->se->conn.proto_minor < 9 ?
402 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(arg);
406 if (!e->
ino && req->se->conn.proto_minor < 4)
409 memset(&arg, 0,
sizeof(arg));
411 return send_reply_ok(req, &arg, size);
417 char buf[
sizeof(
struct fuse_entry_out) + sizeof(struct fuse_open_out)];
418 size_t entrysize = req->se->conn.proto_minor < 9 ?
419 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(
struct fuse_entry_out);
420 struct fuse_entry_out *earg = (
struct fuse_entry_out *) buf;
421 struct fuse_open_out *oarg = (
struct fuse_open_out *) (buf + entrysize);
423 memset(buf, 0,
sizeof(buf));
426 return send_reply_ok(req, buf,
427 entrysize +
sizeof(
struct fuse_open_out));
433 struct fuse_attr_out arg;
434 size_t size = req->se->conn.proto_minor < 9 ?
435 FUSE_COMPAT_ATTR_OUT_SIZE :
sizeof(arg);
437 memset(&arg, 0,
sizeof(arg));
438 arg.attr_valid = calc_timeout_sec(attr_timeout);
439 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
440 convert_stat(attr, &arg.attr);
442 return send_reply_ok(req, &arg, size);
447 return send_reply_ok(req, linkname, strlen(linkname));
452 struct fuse_open_out arg;
454 memset(&arg, 0,
sizeof(arg));
456 return send_reply_ok(req, &arg,
sizeof(arg));
461 struct fuse_write_out arg;
463 memset(&arg, 0,
sizeof(arg));
466 return send_reply_ok(req, &arg,
sizeof(arg));
471 return send_reply_ok(req, buf, size);
474 static int fuse_send_data_iov_fallback(
struct fuse_session *se,
475 struct fuse_chan *ch,
476 struct iovec *iov,
int iov_count,
480 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
485 if (buf->
count == 1 && buf->
idx == 0 && buf->
off == 0 &&
490 iov[iov_count].iov_base = buf->
buf[0].
mem;
491 iov[iov_count].iov_len = len;
493 return fuse_send_msg(se, ch, iov, iov_count);
496 res = posix_memalign(&mbuf, pagesize, len);
500 mem_buf.
buf[0].
mem = mbuf;
508 iov[iov_count].iov_base = mbuf;
509 iov[iov_count].iov_len = len;
511 res = fuse_send_msg(se, ch, iov, iov_count);
517 struct fuse_ll_pipe {
523 static void fuse_ll_pipe_free(
struct fuse_ll_pipe *llp)
531 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC) 532 static int fuse_pipe(
int fds[2])
539 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
540 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
541 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
542 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
550 static int fuse_pipe(
int fds[2])
552 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
556 static struct fuse_ll_pipe *fuse_ll_get_pipe(
struct fuse_session *se)
558 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
562 llp = malloc(
sizeof(
struct fuse_ll_pipe));
566 res = fuse_pipe(llp->pipe);
575 llp->size = pagesize * 16;
578 pthread_setspecific(se->pipe_key, llp);
585 static void fuse_ll_clear_pipe(
struct fuse_session *se)
587 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
589 pthread_setspecific(se->pipe_key, NULL);
590 fuse_ll_pipe_free(llp);
594 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE) 595 static int read_back(
int fd,
char *buf,
size_t len)
599 res = read(fd, buf, len);
601 fprintf(stderr,
"fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
605 fprintf(stderr,
"fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
611 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
612 struct iovec *iov,
int iov_count,
617 struct fuse_out_header *out = iov[0].iov_base;
618 struct fuse_ll_pipe *llp;
621 size_t total_fd_size;
624 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
626 if (se->broken_splice_nonblock)
633 for (idx = buf->
idx; idx < buf->count; idx++) {
637 total_fd_size -= buf->
off;
640 if (total_fd_size < 2 * pagesize)
643 if (se->conn.proto_minor < 14 ||
647 llp = fuse_ll_get_pipe(se);
652 headerlen = iov_length(iov, iov_count);
654 out->len = headerlen + len;
660 pipesize = pagesize * (iov_count + buf->
count + 1) + out->len;
662 if (llp->size < pipesize) {
664 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
671 if (llp->size < pipesize)
676 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
680 if (res != headerlen) {
682 fprintf(stderr,
"fuse: short vmsplice to pipe: %u/%zu\n", res,
688 pipe_buf.
buf[0].
fd = llp->pipe[1];
693 if (res == -EAGAIN || res == -EINVAL) {
705 se->broken_splice_nonblock = 1;
707 pthread_setspecific(se->pipe_key, NULL);
708 fuse_ll_pipe_free(llp);
715 if (res != 0 && res < len) {
716 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
718 size_t now_len = res;
728 res = posix_memalign(&mbuf, pagesize, len);
732 mem_buf.
buf[0].
mem = mbuf;
733 mem_buf.
off = now_len;
737 size_t extra_len = res;
743 tmpbuf = malloc(headerlen);
744 if (tmpbuf == NULL) {
749 res = read_back(llp->pipe[0], tmpbuf, headerlen);
755 res = read_back(llp->pipe[0], mbuf, now_len);
760 len = now_len + extra_len;
761 iov[iov_count].iov_base = mbuf;
762 iov[iov_count].iov_len = len;
764 res = fuse_send_msg(se, ch, iov, iov_count);
772 out->len = headerlen + len;
776 " unique: %llu, success, outsize: %i (splice)\n",
777 (
unsigned long long) out->unique, out->len);
783 splice_flags |= SPLICE_F_MOVE;
785 res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
786 NULL, out->len, splice_flags);
789 perror(
"fuse: splice from pipe");
792 if (res != out->len) {
794 fprintf(stderr,
"fuse: short splice from pipe: %u/%u\n",
801 fuse_ll_clear_pipe(se);
805 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
808 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
809 struct iovec *iov,
int iov_count,
815 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
823 struct fuse_out_header out;
826 iov[0].iov_base = &out;
827 iov[0].iov_len =
sizeof(
struct fuse_out_header);
829 out.unique = req->unique;
832 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
843 struct fuse_statfs_out arg;
844 size_t size = req->se->conn.proto_minor < 4 ?
845 FUSE_COMPAT_STATFS_SIZE :
sizeof(arg);
847 memset(&arg, 0,
sizeof(arg));
848 convert_statfs(stbuf, &arg.st);
850 return send_reply_ok(req, &arg, size);
855 struct fuse_getxattr_out arg;
857 memset(&arg, 0,
sizeof(arg));
860 return send_reply_ok(req, &arg,
sizeof(arg));
865 struct fuse_lk_out arg;
867 memset(&arg, 0,
sizeof(arg));
868 arg.lk.type = lock->l_type;
869 if (lock->l_type != F_UNLCK) {
870 arg.lk.start = lock->l_start;
871 if (lock->l_len == 0)
872 arg.lk.end = OFFSET_MAX;
874 arg.lk.end = lock->l_start + lock->l_len - 1;
876 arg.lk.pid = lock->l_pid;
877 return send_reply_ok(req, &arg,
sizeof(arg));
882 struct fuse_bmap_out arg;
884 memset(&arg, 0,
sizeof(arg));
887 return send_reply_ok(req, &arg,
sizeof(arg));
890 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(
const struct iovec *iov,
893 struct fuse_ioctl_iovec *fiov;
896 fiov = malloc(
sizeof(fiov[0]) * count);
900 for (i = 0; i < count; i++) {
901 fiov[i].base = (uintptr_t) iov[i].iov_base;
902 fiov[i].len = iov[i].iov_len;
909 const struct iovec *in_iov,
size_t in_count,
910 const struct iovec *out_iov,
size_t out_count)
912 struct fuse_ioctl_out arg;
913 struct fuse_ioctl_iovec *in_fiov = NULL;
914 struct fuse_ioctl_iovec *out_fiov = NULL;
919 memset(&arg, 0,
sizeof(arg));
920 arg.flags |= FUSE_IOCTL_RETRY;
921 arg.in_iovs = in_count;
922 arg.out_iovs = out_count;
923 iov[count].iov_base = &arg;
924 iov[count].iov_len =
sizeof(arg);
927 if (req->se->conn.proto_minor < 16) {
929 iov[count].iov_base = (
void *)in_iov;
930 iov[count].iov_len =
sizeof(in_iov[0]) * in_count;
935 iov[count].iov_base = (
void *)out_iov;
936 iov[count].iov_len =
sizeof(out_iov[0]) * out_count;
941 if (
sizeof(
void *) == 4 && req->ioctl_64bit) {
947 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
951 iov[count].iov_base = (
void *)in_fiov;
952 iov[count].iov_len =
sizeof(in_fiov[0]) * in_count;
956 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
960 iov[count].iov_base = (
void *)out_fiov;
961 iov[count].iov_len =
sizeof(out_fiov[0]) * out_count;
966 res = send_reply_iov(req, 0, iov, count);
980 struct fuse_ioctl_out arg;
984 memset(&arg, 0,
sizeof(arg));
986 iov[count].iov_base = &arg;
987 iov[count].iov_len =
sizeof(arg);
991 iov[count].iov_base = (
char *) buf;
992 iov[count].iov_len = size;
996 return send_reply_iov(req, 0, iov, count);
1002 struct iovec *padded_iov;
1003 struct fuse_ioctl_out arg;
1006 padded_iov = malloc((count + 2) *
sizeof(
struct iovec));
1007 if (padded_iov == NULL)
1010 memset(&arg, 0,
sizeof(arg));
1011 arg.result = result;
1012 padded_iov[1].iov_base = &arg;
1013 padded_iov[1].iov_len =
sizeof(arg);
1015 memcpy(&padded_iov[2], iov, count *
sizeof(
struct iovec));
1017 res = send_reply_iov(req, 0, padded_iov, count + 2);
1025 struct fuse_poll_out arg;
1027 memset(&arg, 0,
sizeof(arg));
1028 arg.revents = revents;
1030 return send_reply_ok(req, &arg,
sizeof(arg));
1035 char *name = (
char *) inarg;
1037 if (req->se->op.lookup)
1038 req->se->op.lookup(req, nodeid, name);
1045 struct fuse_forget_in *arg = (
struct fuse_forget_in *) inarg;
1047 if (req->se->op.forget)
1048 req->se->op.forget(req, nodeid, arg->nlookup);
1056 struct fuse_batch_forget_in *arg = (
void *) inarg;
1057 struct fuse_forget_one *param = (
void *) PARAM(arg);
1062 if (req->se->op.forget_multi) {
1063 req->se->op.forget_multi(req, arg->count,
1064 (
struct fuse_forget_data *) param);
1065 }
else if (req->se->op.forget) {
1066 for (i = 0; i < arg->count; i++) {
1067 struct fuse_forget_one *forget = ¶m[i];
1068 struct fuse_req *dummy_req;
1070 dummy_req = fuse_ll_alloc_req(req->se);
1071 if (dummy_req == NULL)
1074 dummy_req->unique = req->unique;
1075 dummy_req->ctx = req->ctx;
1076 dummy_req->ch = NULL;
1078 req->se->op.forget(dummy_req, forget->nodeid,
1092 if (req->se->conn.proto_minor >= 9) {
1093 struct fuse_getattr_in *arg = (
struct fuse_getattr_in *) inarg;
1095 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1096 memset(&fi, 0,
sizeof(fi));
1102 if (req->se->op.getattr)
1103 req->se->op.getattr(req, nodeid, fip);
1110 struct fuse_setattr_in *arg = (
struct fuse_setattr_in *) inarg;
1112 if (req->se->op.setattr) {
1116 memset(&stbuf, 0,
sizeof(stbuf));
1117 convert_attr(arg, &stbuf);
1118 if (arg->valid & FATTR_FH) {
1119 arg->valid &= ~FATTR_FH;
1120 memset(&fi_store, 0,
sizeof(fi_store));
1125 FUSE_SET_ATTR_MODE |
1128 FUSE_SET_ATTR_SIZE |
1129 FUSE_SET_ATTR_ATIME |
1130 FUSE_SET_ATTR_MTIME |
1131 FUSE_SET_ATTR_ATIME_NOW |
1132 FUSE_SET_ATTR_MTIME_NOW |
1133 FUSE_SET_ATTR_CTIME;
1135 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1142 struct fuse_access_in *arg = (
struct fuse_access_in *) inarg;
1144 if (req->se->op.access)
1145 req->se->op.access(req, nodeid, arg->mask);
1154 if (req->se->op.readlink)
1155 req->se->op.readlink(req, nodeid);
1162 struct fuse_mknod_in *arg = (
struct fuse_mknod_in *) inarg;
1163 char *name = PARAM(arg);
1165 if (req->se->conn.proto_minor >= 12)
1166 req->ctx.umask = arg->umask;
1168 name = (
char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1170 if (req->se->op.mknod)
1171 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1178 struct fuse_mkdir_in *arg = (
struct fuse_mkdir_in *) inarg;
1180 if (req->se->conn.proto_minor >= 12)
1181 req->ctx.umask = arg->umask;
1183 if (req->se->op.mkdir)
1184 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1191 char *name = (
char *) inarg;
1193 if (req->se->op.unlink)
1194 req->se->op.unlink(req, nodeid, name);
1201 char *name = (
char *) inarg;
1203 if (req->se->op.rmdir)
1204 req->se->op.rmdir(req, nodeid, name);
1211 char *name = (
char *) inarg;
1212 char *linkname = ((
char *) inarg) + strlen((
char *) inarg) + 1;
1214 if (req->se->op.symlink)
1215 req->se->op.symlink(req, linkname, nodeid, name);
1222 struct fuse_rename_in *arg = (
struct fuse_rename_in *) inarg;
1223 char *oldname = PARAM(arg);
1224 char *newname = oldname + strlen(oldname) + 1;
1226 if (req->se->op.rename)
1227 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1235 struct fuse_rename2_in *arg = (
struct fuse_rename2_in *) inarg;
1236 char *oldname = PARAM(arg);
1237 char *newname = oldname + strlen(oldname) + 1;
1239 if (req->se->op.rename)
1240 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1248 struct fuse_link_in *arg = (
struct fuse_link_in *) inarg;
1250 if (req->se->op.link)
1251 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1258 struct fuse_create_in *arg = (
struct fuse_create_in *) inarg;
1260 if (req->se->op.create) {
1262 char *name = PARAM(arg);
1264 memset(&fi, 0,
sizeof(fi));
1265 fi.
flags = arg->flags;
1267 if (req->se->conn.proto_minor >= 12)
1268 req->ctx.umask = arg->umask;
1270 name = (
char *) inarg +
sizeof(
struct fuse_open_in);
1272 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1279 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1282 memset(&fi, 0,
sizeof(fi));
1283 fi.
flags = arg->flags;
1285 if (req->se->op.open)
1286 req->se->op.open(req, nodeid, &fi);
1293 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1295 if (req->se->op.read) {
1298 memset(&fi, 0,
sizeof(fi));
1300 if (req->se->conn.proto_minor >= 9) {
1302 fi.
flags = arg->flags;
1304 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1311 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1315 memset(&fi, 0,
sizeof(fi));
1317 fi.
writepage = (arg->write_flags & 1) != 0;
1319 if (req->se->conn.proto_minor < 9) {
1320 param = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1323 fi.
flags = arg->flags;
1327 if (req->se->op.write)
1328 req->se->op.write(req, nodeid, param, arg->size,
1337 struct fuse_session *se = req->se;
1342 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1345 memset(&fi, 0,
sizeof(fi));
1349 if (se->conn.proto_minor < 9) {
1350 bufv.
buf[0].
mem = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1351 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1352 FUSE_COMPAT_WRITE_IN_SIZE;
1356 fi.
flags = arg->flags;
1358 bufv.
buf[0].
mem = PARAM(arg);
1360 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1361 sizeof(struct fuse_write_in);
1363 if (bufv.
buf[0].
size < arg->size) {
1364 fprintf(stderr,
"fuse: do_write_buf: buffer size too small\n");
1370 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1375 fuse_ll_clear_pipe(se);
1380 struct fuse_flush_in *arg = (
struct fuse_flush_in *) inarg;
1383 memset(&fi, 0,
sizeof(fi));
1386 if (req->se->conn.proto_minor >= 7)
1389 if (req->se->op.flush)
1390 req->se->op.flush(req, nodeid, &fi);
1397 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1400 memset(&fi, 0,
sizeof(fi));
1401 fi.
flags = arg->flags;
1403 if (req->se->conn.proto_minor >= 8) {
1404 fi.
flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1407 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1408 fi.flock_release = 1;
1412 if (req->se->op.release)
1413 req->se->op.release(req, nodeid, &fi);
1420 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1423 memset(&fi, 0,
sizeof(fi));
1426 if (req->se->op.fsync)
1427 req->se->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1434 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1437 memset(&fi, 0,
sizeof(fi));
1438 fi.
flags = arg->flags;
1440 if (req->se->op.opendir)
1441 req->se->op.opendir(req, nodeid, &fi);
1448 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1451 memset(&fi, 0,
sizeof(fi));
1454 if (req->se->op.readdir)
1455 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1462 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1465 memset(&fi, 0,
sizeof(fi));
1468 if (req->se->op.readdirplus)
1469 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1476 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1479 memset(&fi, 0,
sizeof(fi));
1480 fi.
flags = arg->flags;
1483 if (req->se->op.releasedir)
1484 req->se->op.releasedir(req, nodeid, &fi);
1491 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1494 memset(&fi, 0,
sizeof(fi));
1497 if (req->se->op.fsyncdir)
1498 req->se->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1508 if (req->se->op.statfs)
1509 req->se->op.statfs(req, nodeid);
1511 struct statvfs buf = {
1521 struct fuse_setxattr_in *arg = (
struct fuse_setxattr_in *) inarg;
1522 char *name = PARAM(arg);
1523 char *value = name + strlen(name) + 1;
1525 if (req->se->op.setxattr)
1526 req->se->op.setxattr(req, nodeid, name, value, arg->size,
1534 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1536 if (req->se->op.getxattr)
1537 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1544 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1546 if (req->se->op.listxattr)
1547 req->se->op.listxattr(req, nodeid, arg->size);
1554 char *name = (
char *) inarg;
1556 if (req->se->op.removexattr)
1557 req->se->op.removexattr(req, nodeid, name);
1562 static void convert_fuse_file_lock(
struct fuse_file_lock *fl,
1563 struct flock *flock)
1565 memset(flock, 0,
sizeof(
struct flock));
1566 flock->l_type = fl->type;
1567 flock->l_whence = SEEK_SET;
1568 flock->l_start = fl->start;
1569 if (fl->end == OFFSET_MAX)
1572 flock->l_len = fl->end - fl->start + 1;
1573 flock->l_pid = fl->pid;
1578 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1582 memset(&fi, 0,
sizeof(fi));
1586 convert_fuse_file_lock(&arg->lk, &flock);
1587 if (req->se->op.getlk)
1588 req->se->op.getlk(req, nodeid, &fi, &flock);
1594 const void *inarg,
int sleep)
1596 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1600 memset(&fi, 0,
sizeof(fi));
1604 if (arg->lk_flags & FUSE_LK_FLOCK) {
1607 switch (arg->lk.type) {
1621 if (req->se->op.flock)
1622 req->se->op.flock(req, nodeid, &fi, op);
1626 convert_fuse_file_lock(&arg->lk, &flock);
1627 if (req->se->op.setlk)
1628 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1636 do_setlk_common(req, nodeid, inarg, 0);
1641 do_setlk_common(req, nodeid, inarg, 1);
1644 static int find_interrupted(
struct fuse_session *se,
struct fuse_req *req)
1646 struct fuse_req *curr;
1648 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1649 if (curr->unique == req->u.i.unique) {
1654 pthread_mutex_unlock(&se->lock);
1657 pthread_mutex_lock(&curr->lock);
1658 pthread_mutex_lock(&se->lock);
1659 curr->interrupted = 1;
1660 func = curr->u.ni.func;
1661 data = curr->u.ni.data;
1662 pthread_mutex_unlock(&se->lock);
1665 pthread_mutex_unlock(&curr->lock);
1667 pthread_mutex_lock(&se->lock);
1675 for (curr = se->interrupts.next; curr != &se->interrupts;
1676 curr = curr->next) {
1677 if (curr->u.i.unique == req->u.i.unique)
1685 struct fuse_interrupt_in *arg = (
struct fuse_interrupt_in *) inarg;
1686 struct fuse_session *se = req->se;
1690 fprintf(stderr,
"INTERRUPT: %llu\n",
1691 (
unsigned long long) arg->unique);
1693 req->u.i.unique = arg->unique;
1695 pthread_mutex_lock(&se->lock);
1696 if (find_interrupted(se, req))
1699 list_add_req(req, &se->interrupts);
1700 pthread_mutex_unlock(&se->lock);
1703 static struct fuse_req *check_interrupt(
struct fuse_session *se,
1704 struct fuse_req *req)
1706 struct fuse_req *curr;
1708 for (curr = se->interrupts.next; curr != &se->interrupts;
1709 curr = curr->next) {
1710 if (curr->u.i.unique == req->unique) {
1711 req->interrupted = 1;
1717 curr = se->interrupts.next;
1718 if (curr != &se->interrupts) {
1720 list_init_req(curr);
1728 struct fuse_bmap_in *arg = (
struct fuse_bmap_in *) inarg;
1730 if (req->se->op.bmap)
1731 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1738 struct fuse_ioctl_in *arg = (
struct fuse_ioctl_in *) inarg;
1739 unsigned int flags = arg->flags;
1740 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1743 if (flags & FUSE_IOCTL_DIR &&
1749 memset(&fi, 0,
sizeof(fi));
1752 if (
sizeof(
void *) == 4 && req->se->conn.proto_minor >= 16 &&
1753 !(flags & FUSE_IOCTL_32BIT)) {
1754 req->ioctl_64bit = 1;
1757 if (req->se->op.ioctl)
1758 req->se->op.ioctl(req, nodeid, arg->cmd,
1759 (
void *)(uintptr_t)arg->arg, &fi, flags,
1760 in_buf, arg->in_size, arg->out_size);
1772 struct fuse_poll_in *arg = (
struct fuse_poll_in *) inarg;
1775 memset(&fi, 0,
sizeof(fi));
1779 if (req->se->op.poll) {
1780 struct fuse_pollhandle *ph = NULL;
1782 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1783 ph = malloc(
sizeof(
struct fuse_pollhandle));
1792 req->se->op.poll(req, nodeid, &fi, ph);
1800 struct fuse_fallocate_in *arg = (
struct fuse_fallocate_in *) inarg;
1803 memset(&fi, 0,
sizeof(fi));
1806 if (req->se->op.fallocate)
1807 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1814 struct fuse_init_in *arg = (
struct fuse_init_in *) inarg;
1815 struct fuse_init_out outarg;
1816 struct fuse_session *se = req->se;
1817 size_t bufsize = se->bufsize;
1818 size_t outargsize =
sizeof(outarg);
1822 fprintf(stderr,
"INIT: %u.%u\n", arg->major, arg->minor);
1823 if (arg->major == 7 && arg->minor >= 6) {
1824 fprintf(stderr,
"flags=0x%08x\n", arg->flags);
1825 fprintf(stderr,
"max_readahead=0x%08x\n",
1826 arg->max_readahead);
1829 se->conn.proto_major = arg->major;
1830 se->conn.proto_minor = arg->minor;
1831 se->conn.capable = 0;
1834 memset(&outarg, 0,
sizeof(outarg));
1835 outarg.major = FUSE_KERNEL_VERSION;
1836 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1838 if (arg->major < 7) {
1839 fprintf(stderr,
"fuse: unsupported protocol version: %u.%u\n",
1840 arg->major, arg->minor);
1845 if (arg->major > 7) {
1847 send_reply_ok(req, &outarg,
sizeof(outarg));
1851 if (arg->minor >= 6) {
1852 if (arg->max_readahead < se->conn.max_readahead)
1853 se->conn.max_readahead = arg->max_readahead;
1854 if (arg->flags & FUSE_ASYNC_READ)
1856 if (arg->flags & FUSE_POSIX_LOCKS)
1858 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1860 if (arg->flags & FUSE_EXPORT_SUPPORT)
1862 if (arg->flags & FUSE_DONT_MASK)
1864 if (arg->flags & FUSE_FLOCK_LOCKS)
1866 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1868 if (arg->flags & FUSE_DO_READDIRPLUS)
1870 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1872 if (arg->flags & FUSE_ASYNC_DIO)
1874 if (arg->flags & FUSE_WRITEBACK_CACHE)
1876 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1878 if (arg->flags & FUSE_PARALLEL_DIROPS)
1880 if (arg->flags & FUSE_POSIX_ACL)
1882 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1885 se->conn.max_readahead = 0;
1888 if (se->conn.proto_minor >= 14) {
1890 #ifdef HAVE_VMSPLICE 1896 if (se->conn.proto_minor >= 18)
1906 #define LL_SET_DEFAULT(cond, cap) \ 1907 if ((cond) && (se->conn.capable & (cap))) \ 1908 se->conn.want |= (cap) 1917 LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
1922 se->conn.time_gran = 1;
1924 if (bufsize < FUSE_MIN_READ_BUFFER) {
1925 fprintf(stderr,
"fuse: warning: buffer size too small: %zu\n",
1927 bufsize = FUSE_MIN_READ_BUFFER;
1931 if (bufsize < se->conn.max_write)
1932 se->conn.max_write = bufsize;
1936 se->op.init(se->userdata, &se->conn);
1938 if (se->conn.want & (~se->conn.capable)) {
1939 fprintf(stderr,
"fuse: error: filesystem requested capabilities " 1940 "0x%x that are not supported by kernel, aborting.\n",
1941 se->conn.want & (~se->conn.capable));
1943 se->error = -EPROTO;
1948 unsigned max_read_mo = get_max_read(se->mo);
1949 if (se->conn.max_read != max_read_mo) {
1950 fprintf(stderr,
"fuse: error: init() and fuse_session_new() " 1951 "requested different maximum read size (%u vs %u)\n",
1952 se->conn.max_read, max_read_mo);
1954 se->error = -EPROTO;
1961 outarg.flags |= FUSE_BIG_WRITES;
1964 outarg.flags |= FUSE_ASYNC_READ;
1966 outarg.flags |= FUSE_POSIX_LOCKS;
1968 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1970 outarg.flags |= FUSE_EXPORT_SUPPORT;
1972 outarg.flags |= FUSE_DONT_MASK;
1974 outarg.flags |= FUSE_FLOCK_LOCKS;
1976 outarg.flags |= FUSE_AUTO_INVAL_DATA;
1978 outarg.flags |= FUSE_DO_READDIRPLUS;
1980 outarg.flags |= FUSE_READDIRPLUS_AUTO;
1982 outarg.flags |= FUSE_ASYNC_DIO;
1984 outarg.flags |= FUSE_WRITEBACK_CACHE;
1986 outarg.flags |= FUSE_POSIX_ACL;
1987 outarg.max_readahead = se->conn.max_readahead;
1988 outarg.max_write = se->conn.max_write;
1989 if (se->conn.proto_minor >= 13) {
1990 if (se->conn.max_background >= (1 << 16))
1991 se->conn.max_background = (1 << 16) - 1;
1992 if (se->conn.congestion_threshold > se->conn.max_background)
1993 se->conn.congestion_threshold = se->conn.max_background;
1994 if (!se->conn.congestion_threshold) {
1995 se->conn.congestion_threshold =
1996 se->conn.max_background * 3 / 4;
1999 outarg.max_background = se->conn.max_background;
2000 outarg.congestion_threshold = se->conn.congestion_threshold;
2002 if (se->conn.proto_minor >= 23)
2003 outarg.time_gran = se->conn.time_gran;
2006 fprintf(stderr,
" INIT: %u.%u\n", outarg.major, outarg.minor);
2007 fprintf(stderr,
" flags=0x%08x\n", outarg.flags);
2008 fprintf(stderr,
" max_readahead=0x%08x\n",
2009 outarg.max_readahead);
2010 fprintf(stderr,
" max_write=0x%08x\n", outarg.max_write);
2011 fprintf(stderr,
" max_background=%i\n",
2012 outarg.max_background);
2013 fprintf(stderr,
" congestion_threshold=%i\n",
2014 outarg.congestion_threshold);
2015 fprintf(stderr,
" time_gran=%u\n",
2019 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2020 else if (arg->minor < 23)
2021 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2023 send_reply_ok(req, &outarg, outargsize);
2028 struct fuse_session *se = req->se;
2033 se->got_destroy = 1;
2035 se->op.destroy(se->userdata);
2037 send_reply_ok(req, NULL, 0);
2040 static void list_del_nreq(
struct fuse_notify_req *nreq)
2042 struct fuse_notify_req *prev = nreq->prev;
2043 struct fuse_notify_req *next = nreq->next;
2048 static void list_add_nreq(
struct fuse_notify_req *nreq,
2049 struct fuse_notify_req *next)
2051 struct fuse_notify_req *prev = next->prev;
2058 static void list_init_nreq(
struct fuse_notify_req *nreq)
2065 const void *inarg,
const struct fuse_buf *buf)
2067 struct fuse_session *se = req->se;
2068 struct fuse_notify_req *nreq;
2069 struct fuse_notify_req *head;
2071 pthread_mutex_lock(&se->lock);
2072 head = &se->notify_list;
2073 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2074 if (nreq->unique == req->unique) {
2075 list_del_nreq(nreq);
2079 pthread_mutex_unlock(&se->lock);
2082 nreq->reply(nreq, req, nodeid, inarg, buf);
2085 static int send_notify_iov(
struct fuse_session *se,
int notify_code,
2086 struct iovec *iov,
int count)
2088 struct fuse_out_header out;
2094 out.error = notify_code;
2095 iov[0].iov_base = &out;
2096 iov[0].iov_len =
sizeof(
struct fuse_out_header);
2098 return fuse_send_msg(se, NULL, iov, count);
2104 struct fuse_notify_poll_wakeup_out outarg;
2105 struct iovec iov[2];
2109 iov[1].iov_base = &outarg;
2110 iov[1].iov_len =
sizeof(outarg);
2112 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2119 off_t off, off_t len)
2121 struct fuse_notify_inval_inode_out outarg;
2122 struct iovec iov[2];
2127 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2134 iov[1].iov_base = &outarg;
2135 iov[1].iov_len =
sizeof(outarg);
2137 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2141 const char *name,
size_t namelen)
2143 struct fuse_notify_inval_entry_out outarg;
2144 struct iovec iov[3];
2149 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2152 outarg.parent = parent;
2153 outarg.namelen = namelen;
2156 iov[1].iov_base = &outarg;
2157 iov[1].iov_len =
sizeof(outarg);
2158 iov[2].iov_base = (
void *)name;
2159 iov[2].iov_len = namelen + 1;
2161 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2166 const char *name,
size_t namelen)
2168 struct fuse_notify_delete_out outarg;
2169 struct iovec iov[3];
2174 if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
2177 outarg.parent = parent;
2178 outarg.child = child;
2179 outarg.namelen = namelen;
2182 iov[1].iov_base = &outarg;
2183 iov[1].iov_len =
sizeof(outarg);
2184 iov[2].iov_base = (
void *)name;
2185 iov[2].iov_len = namelen + 1;
2187 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2194 struct fuse_out_header out;
2195 struct fuse_notify_store_out outarg;
2196 struct iovec iov[3];
2203 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2207 out.error = FUSE_NOTIFY_STORE;
2209 outarg.nodeid = ino;
2210 outarg.offset = offset;
2214 iov[0].iov_base = &out;
2215 iov[0].iov_len =
sizeof(out);
2216 iov[1].iov_base = &outarg;
2217 iov[1].iov_len =
sizeof(outarg);
2219 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2226 struct fuse_retrieve_req {
2227 struct fuse_notify_req nreq;
2231 static void fuse_ll_retrieve_reply(
struct fuse_notify_req *nreq,
2236 struct fuse_session *se = req->se;
2237 struct fuse_retrieve_req *rreq =
2238 container_of(nreq,
struct fuse_retrieve_req, nreq);
2239 const struct fuse_notify_retrieve_in *arg = inarg;
2246 bufv.
buf[0].
mem = PARAM(arg);
2248 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
2249 sizeof(struct fuse_notify_retrieve_in);
2251 if (bufv.
buf[0].
size < arg->size) {
2252 fprintf(stderr,
"fuse: retrieve reply: buffer size too small\n");
2258 if (se->op.retrieve_reply) {
2259 se->op.retrieve_reply(req, rreq->cookie, ino,
2260 arg->offset, &bufv);
2267 fuse_ll_clear_pipe(se);
2271 size_t size, off_t offset,
void *cookie)
2273 struct fuse_notify_retrieve_out outarg;
2274 struct iovec iov[2];
2275 struct fuse_retrieve_req *rreq;
2281 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2284 rreq = malloc(
sizeof(*rreq));
2288 pthread_mutex_lock(&se->lock);
2289 rreq->cookie = cookie;
2290 rreq->nreq.unique = se->notify_ctr++;
2291 rreq->nreq.reply = fuse_ll_retrieve_reply;
2292 list_add_nreq(&rreq->nreq, &se->notify_list);
2293 pthread_mutex_unlock(&se->lock);
2295 outarg.notify_unique = rreq->nreq.unique;
2296 outarg.nodeid = ino;
2297 outarg.offset = offset;
2301 iov[1].iov_base = &outarg;
2302 iov[1].iov_len =
sizeof(outarg);
2304 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2306 pthread_mutex_lock(&se->lock);
2307 list_del_nreq(&rreq->nreq);
2308 pthread_mutex_unlock(&se->lock);
2317 return req->se->userdata;
2328 pthread_mutex_lock(&req->lock);
2329 pthread_mutex_lock(&req->se->lock);
2330 req->u.ni.func = func;
2331 req->u.ni.data = data;
2332 pthread_mutex_unlock(&req->se->lock);
2333 if (req->interrupted && func)
2335 pthread_mutex_unlock(&req->lock);
2342 pthread_mutex_lock(&req->se->lock);
2343 interrupted = req->interrupted;
2344 pthread_mutex_unlock(&req->se->lock);
2353 [FUSE_LOOKUP] = { do_lookup,
"LOOKUP" },
2354 [FUSE_FORGET] = { do_forget,
"FORGET" },
2355 [FUSE_GETATTR] = { do_getattr,
"GETATTR" },
2356 [FUSE_SETATTR] = { do_setattr,
"SETATTR" },
2357 [FUSE_READLINK] = { do_readlink,
"READLINK" },
2358 [FUSE_SYMLINK] = { do_symlink,
"SYMLINK" },
2359 [FUSE_MKNOD] = { do_mknod,
"MKNOD" },
2360 [FUSE_MKDIR] = { do_mkdir,
"MKDIR" },
2361 [FUSE_UNLINK] = { do_unlink,
"UNLINK" },
2362 [FUSE_RMDIR] = { do_rmdir,
"RMDIR" },
2363 [FUSE_RENAME] = { do_rename,
"RENAME" },
2364 [FUSE_LINK] = { do_link,
"LINK" },
2365 [FUSE_OPEN] = { do_open,
"OPEN" },
2366 [FUSE_READ] = { do_read,
"READ" },
2367 [FUSE_WRITE] = { do_write,
"WRITE" },
2368 [FUSE_STATFS] = { do_statfs,
"STATFS" },
2369 [FUSE_RELEASE] = { do_release,
"RELEASE" },
2370 [FUSE_FSYNC] = { do_fsync,
"FSYNC" },
2371 [FUSE_SETXATTR] = { do_setxattr,
"SETXATTR" },
2372 [FUSE_GETXATTR] = { do_getxattr,
"GETXATTR" },
2373 [FUSE_LISTXATTR] = { do_listxattr,
"LISTXATTR" },
2374 [FUSE_REMOVEXATTR] = { do_removexattr,
"REMOVEXATTR" },
2375 [FUSE_FLUSH] = { do_flush,
"FLUSH" },
2376 [FUSE_INIT] = { do_init,
"INIT" },
2377 [FUSE_OPENDIR] = { do_opendir,
"OPENDIR" },
2378 [FUSE_READDIR] = { do_readdir,
"READDIR" },
2379 [FUSE_RELEASEDIR] = { do_releasedir,
"RELEASEDIR" },
2380 [FUSE_FSYNCDIR] = { do_fsyncdir,
"FSYNCDIR" },
2381 [FUSE_GETLK] = { do_getlk,
"GETLK" },
2382 [FUSE_SETLK] = { do_setlk,
"SETLK" },
2383 [FUSE_SETLKW] = { do_setlkw,
"SETLKW" },
2384 [FUSE_ACCESS] = { do_access,
"ACCESS" },
2385 [FUSE_CREATE] = { do_create,
"CREATE" },
2386 [FUSE_INTERRUPT] = { do_interrupt,
"INTERRUPT" },
2387 [FUSE_BMAP] = { do_bmap,
"BMAP" },
2388 [FUSE_IOCTL] = { do_ioctl,
"IOCTL" },
2389 [FUSE_POLL] = { do_poll,
"POLL" },
2390 [FUSE_FALLOCATE] = { do_fallocate,
"FALLOCATE" },
2391 [FUSE_DESTROY] = { do_destroy,
"DESTROY" },
2392 [FUSE_NOTIFY_REPLY] = { (
void *) 1,
"NOTIFY_REPLY" },
2393 [FUSE_BATCH_FORGET] = { do_batch_forget,
"BATCH_FORGET" },
2394 [FUSE_READDIRPLUS] = { do_readdirplus,
"READDIRPLUS"},
2395 [FUSE_RENAME2] = { do_rename2,
"RENAME2" },
2396 [CUSE_INIT] = { cuse_lowlevel_init,
"CUSE_INIT" },
2399 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) 2401 static const char *opname(
enum fuse_opcode opcode)
2403 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2406 return fuse_ll_ops[opcode].name;
2409 static int fuse_ll_copy_from_pipe(
struct fuse_bufvec *dst,
2414 fprintf(stderr,
"fuse: copy from pipe: %s\n", strerror(-res));
2418 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2427 fuse_session_process_buf_int(se, buf, NULL);
2430 void fuse_session_process_buf_int(
struct fuse_session *se,
2431 const struct fuse_buf *buf,
struct fuse_chan *ch)
2433 const size_t write_header_size =
sizeof(
struct fuse_in_header) +
2434 sizeof(struct fuse_write_in);
2436 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2437 struct fuse_in_header *in;
2439 struct fuse_req *req;
2448 mbuf = malloc(tmpbuf.
buf[0].
size);
2450 fprintf(stderr,
"fuse: failed to allocate header\n");
2453 tmpbuf.
buf[0].
mem = mbuf;
2455 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2466 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2467 (
unsigned long long) in->unique,
2468 opname((
enum fuse_opcode) in->opcode), in->opcode,
2469 (
unsigned long long) in->nodeid, buf->
size, in->pid);
2472 req = fuse_ll_alloc_req(se);
2474 struct fuse_out_header out = {
2475 .unique = in->unique,
2478 struct iovec iov = {
2480 .iov_len =
sizeof(
struct fuse_out_header),
2483 fuse_send_msg(se, ch, &iov, 1);
2487 req->unique = in->unique;
2488 req->ctx.uid = in->uid;
2489 req->ctx.gid = in->gid;
2490 req->ctx.pid = in->pid;
2491 req->ch = ch ? fuse_chan_get(ch) : NULL;
2494 if (!se->got_init) {
2495 enum fuse_opcode expected;
2497 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2498 if (in->opcode != expected)
2500 }
else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2505 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2506 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2507 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2508 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2509 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2510 in->opcode != FUSE_NOTIFY_REPLY &&
2511 in->opcode != FUSE_READDIRPLUS)
2515 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2517 if (in->opcode != FUSE_INTERRUPT) {
2518 struct fuse_req *intr;
2519 pthread_mutex_lock(&se->lock);
2520 intr = check_interrupt(se, req);
2521 list_add_req(req, &se->list);
2522 pthread_mutex_unlock(&se->lock);
2528 (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2529 in->opcode != FUSE_NOTIFY_REPLY) {
2533 newmbuf = realloc(mbuf, buf->
size);
2534 if (newmbuf == NULL)
2538 tmpbuf = FUSE_BUFVEC_INIT(buf->
size - write_header_size);
2539 tmpbuf.
buf[0].
mem = mbuf + write_header_size;
2541 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2549 inarg = (
void *) &in[1];
2550 if (in->opcode == FUSE_WRITE && se->op.write_buf)
2551 do_write_buf(req, in->nodeid, inarg, buf);
2552 else if (in->opcode == FUSE_NOTIFY_REPLY)
2553 do_notify_reply(req, in->nodeid, inarg, buf);
2555 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2565 fuse_ll_clear_pipe(se);
2569 #define LL_OPTION(n,o,v) \ 2570 { n, offsetof(struct fuse_session, o), v } 2572 static const struct fuse_opt fuse_ll_opts[] = {
2573 LL_OPTION(
"debug", debug, 1),
2574 LL_OPTION(
"-d", debug, 1),
2575 LL_OPTION(
"--debug", debug, 1),
2576 LL_OPTION(
"allow_root", deny_others, 1),
2582 printf(
"using FUSE kernel interface version %i.%i\n",
2583 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2584 fuse_mount_version();
2592 " -o allow_other allow access by all users\n" 2593 " -o allow_root allow access by root\n" 2594 " -o auto_unmount auto unmount on process termination\n");
2599 struct fuse_ll_pipe *llp;
2601 if (se->got_init && !se->got_destroy) {
2603 se->op.destroy(se->userdata);
2605 llp = pthread_getspecific(se->pipe_key);
2607 fuse_ll_pipe_free(llp);
2608 pthread_key_delete(se->pipe_key);
2609 pthread_mutex_destroy(&se->lock);
2610 free(se->cuse_data);
2613 destroy_mount_opts(se->mo);
2618 static void fuse_ll_pipe_destructor(
void *data)
2620 struct fuse_ll_pipe *llp = data;
2621 fuse_ll_pipe_free(llp);
2626 return fuse_session_receive_buf_int(se, buf, NULL);
2629 int fuse_session_receive_buf_int(
struct fuse_session *se,
struct fuse_buf *buf,
2630 struct fuse_chan *ch)
2635 size_t bufsize = se->bufsize;
2636 struct fuse_ll_pipe *llp;
2642 llp = fuse_ll_get_pipe(se);
2646 if (llp->size < bufsize) {
2647 if (llp->can_grow) {
2648 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2655 if (llp->size < bufsize)
2659 res = splice(ch ? ch->fd : se->fd,
2660 NULL, llp->pipe[1], NULL, bufsize, 0);
2667 if (err == ENODEV) {
2673 if (err != EINTR && err != EAGAIN)
2674 perror(
"fuse: splice from device");
2678 if (res <
sizeof(
struct fuse_in_header)) {
2679 fprintf(stderr,
"short splice from fuse device\n");
2694 if (res <
sizeof(
struct fuse_in_header) +
2695 sizeof(
struct fuse_write_in) + pagesize) {
2700 buf->
mem = malloc(se->bufsize);
2703 "fuse: failed to allocate read buffer\n");
2707 buf->
size = se->bufsize;
2713 fprintf(stderr,
"fuse: copy from pipe: %s\n",
2715 fuse_ll_clear_pipe(se);
2718 if (res < tmpbuf.
size) {
2719 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2720 fuse_ll_clear_pipe(se);
2723 assert(res == tmpbuf.
size);
2727 buf->
fd = tmpbuf.
fd;
2737 buf->
mem = malloc(se->bufsize);
2740 "fuse: failed to allocate read buffer\n");
2746 res = read(ch ? ch->fd : se->fd, buf->
mem, se->bufsize);
2757 if (err == ENODEV) {
2766 if (err != EINTR && err != EAGAIN)
2767 perror(
"fuse: reading device");
2770 if ((
size_t) res <
sizeof(
struct fuse_in_header)) {
2771 fprintf(stderr,
"short read on fuse device\n");
2780 #define KERNEL_BUF_PAGES 32 2783 #define HEADER_SIZE 0x1000 2787 size_t op_size,
void *userdata)
2790 struct fuse_session *se;
2791 struct mount_opts *mo;
2794 fprintf(stderr,
"fuse: warning: library too old, some operations may not work\n");
2798 if (args->
argc == 0) {
2799 fprintf(stderr,
"fuse: empty argv passed to fuse_session_new().\n");
2803 se = (
struct fuse_session *) calloc(1,
sizeof(
struct fuse_session));
2805 fprintf(stderr,
"fuse: failed to allocate fuse object\n");
2809 se->conn.max_write = UINT_MAX;
2810 se->conn.max_readahead = UINT_MAX;
2815 if(se->deny_others) {
2825 mo = parse_mount_opts(args);
2829 if(args->
argc == 1 &&
2830 args->
argv[0][0] ==
'-') {
2831 fprintf(stderr,
"fuse: warning: argv[0] looks like an option, but " 2832 "will be ignored\n");
2833 }
else if (args->
argc != 1) {
2835 fprintf(stderr,
"fuse: unknown option(s): `");
2836 for(i = 1; i < args->
argc-1; i++)
2837 fprintf(stderr,
"%s ", args->
argv[i]);
2838 fprintf(stderr,
"%s'\n", args->
argv[i]);
2843 fprintf(stderr,
"FUSE library version: %s\n", PACKAGE_VERSION);
2845 se->bufsize = KERNEL_BUF_PAGES * getpagesize() + HEADER_SIZE;
2847 list_init_req(&se->list);
2848 list_init_req(&se->interrupts);
2849 list_init_nreq(&se->notify_list);
2851 fuse_mutex_init(&se->lock);
2853 err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
2855 fprintf(stderr,
"fuse: failed to create thread specific key: %s\n",
2860 memcpy(&se->op, op, op_size);
2861 se->owner = getuid();
2862 se->userdata = userdata;
2868 pthread_mutex_destroy(&se->lock);
2888 fd = open(
"/dev/null", O_RDWR);
2891 }
while (fd >= 0 && fd <= 2);
2894 fd = fuse_kern_mount(mountpoint, se->mo);
2900 se->mountpoint = strdup(mountpoint);
2901 if (se->mountpoint == NULL)
2907 fuse_kern_unmount(mountpoint, fd);
2918 fuse_kern_unmount(se->mountpoint, se->fd);
2919 free(se->mountpoint);
2920 se->mountpoint = NULL;
2927 size_t bufsize = 1024;
2931 unsigned long pid = req->ctx.pid;
2934 sprintf(path,
"/proc/%lu/task/%lu/status", pid, pid);
2937 buf = malloc(bufsize);
2942 fd = open(path, O_RDONLY);
2946 ret = read(fd, buf, bufsize);
2953 if ((
size_t)ret == bufsize) {
2960 s = strstr(buf,
"\nGroups:");
2968 unsigned long val = strtoul(s, &end, 0);
2988 (void) req; (void) size; (void) list;
#define FUSE_CAP_ASYNC_DIO
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)
int fuse_reply_write(fuse_req_t req, size_t count)
#define FUSE_CAP_DONT_MASK
#define FUSE_CAP_ATOMIC_O_TRUNC
void fuse_session_reset(struct fuse_session *se)
struct fuse_req * fuse_req_t
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
#define FUSE_CAP_IOCTL_DIR
#define FUSE_CAP_POSIX_LOCKS
#define FUSE_CAP_READDIRPLUS
const struct fuse_ctx * fuse_req_ctx(fuse_req_t req)
#define FUSE_CAP_PARALLEL_DIROPS
#define FUSE_CAP_WRITEBACK_CACHE
void fuse_session_unmount(struct fuse_session *se)
#define FUSE_CAP_SPLICE_READ
#define FUSE_CAP_AUTO_INVAL_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_data(fuse_req_t req, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
#define FUSE_CAP_FLOCK_LOCKS
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, int count)
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
void fuse_lowlevel_help(void)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
int fuse_req_interrupted(fuse_req_t req)
int fuse_reply_poll(fuse_req_t req, unsigned revents)
#define FUSE_CAP_HANDLE_KILLPRIV
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_err(fuse_req_t req, int err)
#define FUSE_CAP_SPLICE_WRITE
#define FUSE_CAP_NO_OPEN_SUPPORT
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)
void * fuse_req_userdata(fuse_req_t req)
enum fuse_buf_flags flags
void fuse_reply_none(fuse_req_t req)
int fuse_session_exited(struct fuse_session *se)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
void fuse_session_destroy(struct fuse_session *se)
#define FUSE_CAP_POSIX_ACL
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_exit(struct fuse_session *se)
int fuse_reply_readlink(fuse_req_t req, const char *link)
void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
void(* fuse_interrupt_func_t)(fuse_req_t req, void *data)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, void *data)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
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)
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
void fuse_lowlevel_version(void)
#define FUSE_CAP_EXPORT_SUPPORT
int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
#define FUSE_CAP_SPLICE_MOVE
int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_CAP_ASYNC_READ
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
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_reply_xattr(fuse_req_t req, size_t count)
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_READDIRPLUS_AUTO