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 & 0170000) >> 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 & 0170000) >> 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_NONSEEKABLE;
401 struct fuse_entry_out arg;
402 size_t size = req->se->conn.proto_minor < 9 ?
403 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(arg);
407 if (!e->
ino && req->se->conn.proto_minor < 4)
410 memset(&arg, 0,
sizeof(arg));
412 return send_reply_ok(req, &arg, size);
418 char buf[
sizeof(
struct fuse_entry_out) + sizeof(struct fuse_open_out)];
419 size_t entrysize = req->se->conn.proto_minor < 9 ?
420 FUSE_COMPAT_ENTRY_OUT_SIZE :
sizeof(
struct fuse_entry_out);
421 struct fuse_entry_out *earg = (
struct fuse_entry_out *) buf;
422 struct fuse_open_out *oarg = (
struct fuse_open_out *) (buf + entrysize);
424 memset(buf, 0,
sizeof(buf));
427 return send_reply_ok(req, buf,
428 entrysize +
sizeof(
struct fuse_open_out));
434 struct fuse_attr_out arg;
435 size_t size = req->se->conn.proto_minor < 9 ?
436 FUSE_COMPAT_ATTR_OUT_SIZE :
sizeof(arg);
438 memset(&arg, 0,
sizeof(arg));
439 arg.attr_valid = calc_timeout_sec(attr_timeout);
440 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
441 convert_stat(attr, &arg.attr);
443 return send_reply_ok(req, &arg, size);
448 return send_reply_ok(req, linkname, strlen(linkname));
453 struct fuse_open_out arg;
455 memset(&arg, 0,
sizeof(arg));
457 return send_reply_ok(req, &arg,
sizeof(arg));
462 struct fuse_write_out arg;
464 memset(&arg, 0,
sizeof(arg));
467 return send_reply_ok(req, &arg,
sizeof(arg));
472 return send_reply_ok(req, buf, size);
475 static int fuse_send_data_iov_fallback(
struct fuse_session *se,
476 struct fuse_chan *ch,
477 struct iovec *iov,
int iov_count,
481 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
486 if (buf->
count == 1 && buf->
idx == 0 && buf->
off == 0 &&
491 iov[iov_count].iov_base = buf->
buf[0].
mem;
492 iov[iov_count].iov_len = len;
494 return fuse_send_msg(se, ch, iov, iov_count);
497 res = posix_memalign(&mbuf, pagesize, len);
501 mem_buf.
buf[0].
mem = mbuf;
509 iov[iov_count].iov_base = mbuf;
510 iov[iov_count].iov_len = len;
512 res = fuse_send_msg(se, ch, iov, iov_count);
518 struct fuse_ll_pipe {
524 static void fuse_ll_pipe_free(
struct fuse_ll_pipe *llp)
532 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC) 533 static int fuse_pipe(
int fds[2])
540 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
541 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
542 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
543 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
551 static int fuse_pipe(
int fds[2])
553 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
557 static struct fuse_ll_pipe *fuse_ll_get_pipe(
struct fuse_session *se)
559 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
563 llp = malloc(
sizeof(
struct fuse_ll_pipe));
567 res = fuse_pipe(llp->pipe);
576 llp->size = pagesize * 16;
579 pthread_setspecific(se->pipe_key, llp);
586 static void fuse_ll_clear_pipe(
struct fuse_session *se)
588 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
590 pthread_setspecific(se->pipe_key, NULL);
591 fuse_ll_pipe_free(llp);
595 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE) 596 static int read_back(
int fd,
char *buf,
size_t len)
600 res = read(fd, buf, len);
602 fprintf(stderr,
"fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
606 fprintf(stderr,
"fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
612 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
613 struct iovec *iov,
int iov_count,
618 struct fuse_out_header *out = iov[0].iov_base;
619 struct fuse_ll_pipe *llp;
622 size_t total_fd_size;
625 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
627 if (se->broken_splice_nonblock)
634 for (idx = buf->
idx; idx < buf->count; idx++) {
638 total_fd_size -= buf->
off;
641 if (total_fd_size < 2 * pagesize)
644 if (se->conn.proto_minor < 14 ||
648 llp = fuse_ll_get_pipe(se);
653 headerlen = iov_length(iov, iov_count);
655 out->len = headerlen + len;
661 pipesize = pagesize * (iov_count + buf->
count + 1) + out->len;
663 if (llp->size < pipesize) {
665 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
672 if (llp->size < pipesize)
677 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
681 if (res != headerlen) {
683 fprintf(stderr,
"fuse: short vmsplice to pipe: %u/%zu\n", res,
689 pipe_buf.
buf[0].
fd = llp->pipe[1];
694 if (res == -EAGAIN || res == -EINVAL) {
706 se->broken_splice_nonblock = 1;
708 pthread_setspecific(se->pipe_key, NULL);
709 fuse_ll_pipe_free(llp);
716 if (res != 0 && res < len) {
717 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
719 size_t now_len = res;
729 res = posix_memalign(&mbuf, pagesize, len);
733 mem_buf.
buf[0].
mem = mbuf;
734 mem_buf.
off = now_len;
738 size_t extra_len = res;
744 tmpbuf = malloc(headerlen);
745 if (tmpbuf == NULL) {
750 res = read_back(llp->pipe[0], tmpbuf, headerlen);
756 res = read_back(llp->pipe[0], mbuf, now_len);
761 len = now_len + extra_len;
762 iov[iov_count].iov_base = mbuf;
763 iov[iov_count].iov_len = len;
765 res = fuse_send_msg(se, ch, iov, iov_count);
773 out->len = headerlen + len;
777 " unique: %llu, success, outsize: %i (splice)\n",
778 (
unsigned long long) out->unique, out->len);
784 splice_flags |= SPLICE_F_MOVE;
786 res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
787 NULL, out->len, splice_flags);
790 perror(
"fuse: splice from pipe");
793 if (res != out->len) {
795 fprintf(stderr,
"fuse: short splice from pipe: %u/%u\n",
802 fuse_ll_clear_pipe(se);
806 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
809 static int fuse_send_data_iov(
struct fuse_session *se,
struct fuse_chan *ch,
810 struct iovec *iov,
int iov_count,
816 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
824 struct fuse_out_header out;
827 iov[0].iov_base = &out;
828 iov[0].iov_len =
sizeof(
struct fuse_out_header);
830 out.unique = req->unique;
833 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
844 struct fuse_statfs_out arg;
845 size_t size = req->se->conn.proto_minor < 4 ?
846 FUSE_COMPAT_STATFS_SIZE :
sizeof(arg);
848 memset(&arg, 0,
sizeof(arg));
849 convert_statfs(stbuf, &arg.st);
851 return send_reply_ok(req, &arg, size);
856 struct fuse_getxattr_out arg;
858 memset(&arg, 0,
sizeof(arg));
861 return send_reply_ok(req, &arg,
sizeof(arg));
866 struct fuse_lk_out arg;
868 memset(&arg, 0,
sizeof(arg));
869 arg.lk.type = lock->l_type;
870 if (lock->l_type != F_UNLCK) {
871 arg.lk.start = lock->l_start;
872 if (lock->l_len == 0)
873 arg.lk.end = OFFSET_MAX;
875 arg.lk.end = lock->l_start + lock->l_len - 1;
877 arg.lk.pid = lock->l_pid;
878 return send_reply_ok(req, &arg,
sizeof(arg));
883 struct fuse_bmap_out arg;
885 memset(&arg, 0,
sizeof(arg));
888 return send_reply_ok(req, &arg,
sizeof(arg));
891 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(
const struct iovec *iov,
894 struct fuse_ioctl_iovec *fiov;
897 fiov = malloc(
sizeof(fiov[0]) * count);
901 for (i = 0; i < count; i++) {
902 fiov[i].base = (uintptr_t) iov[i].iov_base;
903 fiov[i].len = iov[i].iov_len;
910 const struct iovec *in_iov,
size_t in_count,
911 const struct iovec *out_iov,
size_t out_count)
913 struct fuse_ioctl_out arg;
914 struct fuse_ioctl_iovec *in_fiov = NULL;
915 struct fuse_ioctl_iovec *out_fiov = NULL;
920 memset(&arg, 0,
sizeof(arg));
921 arg.flags |= FUSE_IOCTL_RETRY;
922 arg.in_iovs = in_count;
923 arg.out_iovs = out_count;
924 iov[count].iov_base = &arg;
925 iov[count].iov_len =
sizeof(arg);
928 if (req->se->conn.proto_minor < 16) {
930 iov[count].iov_base = (
void *)in_iov;
931 iov[count].iov_len =
sizeof(in_iov[0]) * in_count;
936 iov[count].iov_base = (
void *)out_iov;
937 iov[count].iov_len =
sizeof(out_iov[0]) * out_count;
942 if (
sizeof(
void *) == 4 && req->ioctl_64bit) {
948 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
952 iov[count].iov_base = (
void *)in_fiov;
953 iov[count].iov_len =
sizeof(in_fiov[0]) * in_count;
957 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
961 iov[count].iov_base = (
void *)out_fiov;
962 iov[count].iov_len =
sizeof(out_fiov[0]) * out_count;
967 res = send_reply_iov(req, 0, iov, count);
981 struct fuse_ioctl_out arg;
985 memset(&arg, 0,
sizeof(arg));
987 iov[count].iov_base = &arg;
988 iov[count].iov_len =
sizeof(arg);
992 iov[count].iov_base = (
char *) buf;
993 iov[count].iov_len = size;
997 return send_reply_iov(req, 0, iov, count);
1003 struct iovec *padded_iov;
1004 struct fuse_ioctl_out arg;
1007 padded_iov = malloc((count + 2) *
sizeof(
struct iovec));
1008 if (padded_iov == NULL)
1011 memset(&arg, 0,
sizeof(arg));
1012 arg.result = result;
1013 padded_iov[1].iov_base = &arg;
1014 padded_iov[1].iov_len =
sizeof(arg);
1016 memcpy(&padded_iov[2], iov, count *
sizeof(
struct iovec));
1018 res = send_reply_iov(req, 0, padded_iov, count + 2);
1026 struct fuse_poll_out arg;
1028 memset(&arg, 0,
sizeof(arg));
1029 arg.revents = revents;
1031 return send_reply_ok(req, &arg,
sizeof(arg));
1036 char *name = (
char *) inarg;
1038 if (req->se->op.lookup)
1039 req->se->op.lookup(req, nodeid, name);
1046 struct fuse_forget_in *arg = (
struct fuse_forget_in *) inarg;
1048 if (req->se->op.forget)
1049 req->se->op.forget(req, nodeid, arg->nlookup);
1057 struct fuse_batch_forget_in *arg = (
void *) inarg;
1058 struct fuse_forget_one *param = (
void *) PARAM(arg);
1063 if (req->se->op.forget_multi) {
1064 req->se->op.forget_multi(req, arg->count,
1065 (
struct fuse_forget_data *) param);
1066 }
else if (req->se->op.forget) {
1067 for (i = 0; i < arg->count; i++) {
1068 struct fuse_forget_one *forget = ¶m[i];
1069 struct fuse_req *dummy_req;
1071 dummy_req = fuse_ll_alloc_req(req->se);
1072 if (dummy_req == NULL)
1075 dummy_req->unique = req->unique;
1076 dummy_req->ctx = req->ctx;
1077 dummy_req->ch = NULL;
1079 req->se->op.forget(dummy_req, forget->nodeid,
1093 if (req->se->conn.proto_minor >= 9) {
1094 struct fuse_getattr_in *arg = (
struct fuse_getattr_in *) inarg;
1096 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1097 memset(&fi, 0,
sizeof(fi));
1103 if (req->se->op.getattr)
1104 req->se->op.getattr(req, nodeid, fip);
1111 struct fuse_setattr_in *arg = (
struct fuse_setattr_in *) inarg;
1113 if (req->se->op.setattr) {
1117 memset(&stbuf, 0,
sizeof(stbuf));
1118 convert_attr(arg, &stbuf);
1119 if (arg->valid & FATTR_FH) {
1120 arg->valid &= ~FATTR_FH;
1121 memset(&fi_store, 0,
sizeof(fi_store));
1126 FUSE_SET_ATTR_MODE |
1129 FUSE_SET_ATTR_SIZE |
1130 FUSE_SET_ATTR_ATIME |
1131 FUSE_SET_ATTR_MTIME |
1132 FUSE_SET_ATTR_ATIME_NOW |
1133 FUSE_SET_ATTR_MTIME_NOW |
1134 FUSE_SET_ATTR_CTIME;
1136 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1143 struct fuse_access_in *arg = (
struct fuse_access_in *) inarg;
1145 if (req->se->op.access)
1146 req->se->op.access(req, nodeid, arg->mask);
1155 if (req->se->op.readlink)
1156 req->se->op.readlink(req, nodeid);
1163 struct fuse_mknod_in *arg = (
struct fuse_mknod_in *) inarg;
1164 char *name = PARAM(arg);
1166 if (req->se->conn.proto_minor >= 12)
1167 req->ctx.umask = arg->umask;
1169 name = (
char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1171 if (req->se->op.mknod)
1172 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1179 struct fuse_mkdir_in *arg = (
struct fuse_mkdir_in *) inarg;
1181 if (req->se->conn.proto_minor >= 12)
1182 req->ctx.umask = arg->umask;
1184 if (req->se->op.mkdir)
1185 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1192 char *name = (
char *) inarg;
1194 if (req->se->op.unlink)
1195 req->se->op.unlink(req, nodeid, name);
1202 char *name = (
char *) inarg;
1204 if (req->se->op.rmdir)
1205 req->se->op.rmdir(req, nodeid, name);
1212 char *name = (
char *) inarg;
1213 char *linkname = ((
char *) inarg) + strlen((
char *) inarg) + 1;
1215 if (req->se->op.symlink)
1216 req->se->op.symlink(req, linkname, nodeid, name);
1223 struct fuse_rename_in *arg = (
struct fuse_rename_in *) inarg;
1224 char *oldname = PARAM(arg);
1225 char *newname = oldname + strlen(oldname) + 1;
1227 if (req->se->op.rename)
1228 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1236 struct fuse_rename2_in *arg = (
struct fuse_rename2_in *) inarg;
1237 char *oldname = PARAM(arg);
1238 char *newname = oldname + strlen(oldname) + 1;
1240 if (req->se->op.rename)
1241 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1249 struct fuse_link_in *arg = (
struct fuse_link_in *) inarg;
1251 if (req->se->op.link)
1252 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1259 struct fuse_create_in *arg = (
struct fuse_create_in *) inarg;
1261 if (req->se->op.create) {
1263 char *name = PARAM(arg);
1265 memset(&fi, 0,
sizeof(fi));
1266 fi.
flags = arg->flags;
1268 if (req->se->conn.proto_minor >= 12)
1269 req->ctx.umask = arg->umask;
1271 name = (
char *) inarg +
sizeof(
struct fuse_open_in);
1273 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1280 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1283 memset(&fi, 0,
sizeof(fi));
1284 fi.
flags = arg->flags;
1286 if (req->se->op.open)
1287 req->se->op.open(req, nodeid, &fi);
1294 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1296 if (req->se->op.read) {
1299 memset(&fi, 0,
sizeof(fi));
1301 if (req->se->conn.proto_minor >= 9) {
1303 fi.
flags = arg->flags;
1305 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1312 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1316 memset(&fi, 0,
sizeof(fi));
1318 fi.
writepage = (arg->write_flags & 1) != 0;
1320 if (req->se->conn.proto_minor < 9) {
1321 param = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1324 fi.
flags = arg->flags;
1328 if (req->se->op.write)
1329 req->se->op.write(req, nodeid, param, arg->size,
1338 struct fuse_session *se = req->se;
1343 struct fuse_write_in *arg = (
struct fuse_write_in *) inarg;
1346 memset(&fi, 0,
sizeof(fi));
1350 if (se->conn.proto_minor < 9) {
1351 bufv.
buf[0].
mem = ((
char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1352 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1353 FUSE_COMPAT_WRITE_IN_SIZE;
1357 fi.
flags = arg->flags;
1359 bufv.
buf[0].
mem = PARAM(arg);
1361 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
1362 sizeof(struct fuse_write_in);
1364 if (bufv.
buf[0].
size < arg->size) {
1365 fprintf(stderr,
"fuse: do_write_buf: buffer size too small\n");
1371 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1376 fuse_ll_clear_pipe(se);
1381 struct fuse_flush_in *arg = (
struct fuse_flush_in *) inarg;
1384 memset(&fi, 0,
sizeof(fi));
1387 if (req->se->conn.proto_minor >= 7)
1390 if (req->se->op.flush)
1391 req->se->op.flush(req, nodeid, &fi);
1398 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1401 memset(&fi, 0,
sizeof(fi));
1402 fi.
flags = arg->flags;
1404 if (req->se->conn.proto_minor >= 8) {
1405 fi.
flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1408 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1409 fi.flock_release = 1;
1413 if (req->se->op.release)
1414 req->se->op.release(req, nodeid, &fi);
1421 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1424 memset(&fi, 0,
sizeof(fi));
1427 if (req->se->op.fsync)
1428 req->se->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
1435 struct fuse_open_in *arg = (
struct fuse_open_in *) inarg;
1438 memset(&fi, 0,
sizeof(fi));
1439 fi.
flags = arg->flags;
1441 if (req->se->op.opendir)
1442 req->se->op.opendir(req, nodeid, &fi);
1449 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1452 memset(&fi, 0,
sizeof(fi));
1455 if (req->se->op.readdir)
1456 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1463 struct fuse_read_in *arg = (
struct fuse_read_in *) inarg;
1466 memset(&fi, 0,
sizeof(fi));
1469 if (req->se->op.readdirplus)
1470 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1477 struct fuse_release_in *arg = (
struct fuse_release_in *) inarg;
1480 memset(&fi, 0,
sizeof(fi));
1481 fi.
flags = arg->flags;
1484 if (req->se->op.releasedir)
1485 req->se->op.releasedir(req, nodeid, &fi);
1492 struct fuse_fsync_in *arg = (
struct fuse_fsync_in *) inarg;
1495 memset(&fi, 0,
sizeof(fi));
1498 if (req->se->op.fsyncdir)
1499 req->se->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
1509 if (req->se->op.statfs)
1510 req->se->op.statfs(req, nodeid);
1512 struct statvfs buf = {
1522 struct fuse_setxattr_in *arg = (
struct fuse_setxattr_in *) inarg;
1523 char *name = PARAM(arg);
1524 char *value = name + strlen(name) + 1;
1526 if (req->se->op.setxattr)
1527 req->se->op.setxattr(req, nodeid, name, value, arg->size,
1535 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1537 if (req->se->op.getxattr)
1538 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1545 struct fuse_getxattr_in *arg = (
struct fuse_getxattr_in *) inarg;
1547 if (req->se->op.listxattr)
1548 req->se->op.listxattr(req, nodeid, arg->size);
1555 char *name = (
char *) inarg;
1557 if (req->se->op.removexattr)
1558 req->se->op.removexattr(req, nodeid, name);
1563 static void convert_fuse_file_lock(
struct fuse_file_lock *fl,
1564 struct flock *flock)
1566 memset(flock, 0,
sizeof(
struct flock));
1567 flock->l_type = fl->type;
1568 flock->l_whence = SEEK_SET;
1569 flock->l_start = fl->start;
1570 if (fl->end == OFFSET_MAX)
1573 flock->l_len = fl->end - fl->start + 1;
1574 flock->l_pid = fl->pid;
1579 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1583 memset(&fi, 0,
sizeof(fi));
1587 convert_fuse_file_lock(&arg->lk, &flock);
1588 if (req->se->op.getlk)
1589 req->se->op.getlk(req, nodeid, &fi, &flock);
1595 const void *inarg,
int sleep)
1597 struct fuse_lk_in *arg = (
struct fuse_lk_in *) inarg;
1601 memset(&fi, 0,
sizeof(fi));
1605 if (arg->lk_flags & FUSE_LK_FLOCK) {
1608 switch (arg->lk.type) {
1622 if (req->se->op.flock)
1623 req->se->op.flock(req, nodeid, &fi, op);
1627 convert_fuse_file_lock(&arg->lk, &flock);
1628 if (req->se->op.setlk)
1629 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1637 do_setlk_common(req, nodeid, inarg, 0);
1642 do_setlk_common(req, nodeid, inarg, 1);
1645 static int find_interrupted(
struct fuse_session *se,
struct fuse_req *req)
1647 struct fuse_req *curr;
1649 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1650 if (curr->unique == req->u.i.unique) {
1655 pthread_mutex_unlock(&se->lock);
1658 pthread_mutex_lock(&curr->lock);
1659 pthread_mutex_lock(&se->lock);
1660 curr->interrupted = 1;
1661 func = curr->u.ni.func;
1662 data = curr->u.ni.data;
1663 pthread_mutex_unlock(&se->lock);
1666 pthread_mutex_unlock(&curr->lock);
1668 pthread_mutex_lock(&se->lock);
1676 for (curr = se->interrupts.next; curr != &se->interrupts;
1677 curr = curr->next) {
1678 if (curr->u.i.unique == req->u.i.unique)
1686 struct fuse_interrupt_in *arg = (
struct fuse_interrupt_in *) inarg;
1687 struct fuse_session *se = req->se;
1691 fprintf(stderr,
"INTERRUPT: %llu\n",
1692 (
unsigned long long) arg->unique);
1694 req->u.i.unique = arg->unique;
1696 pthread_mutex_lock(&se->lock);
1697 if (find_interrupted(se, req))
1700 list_add_req(req, &se->interrupts);
1701 pthread_mutex_unlock(&se->lock);
1704 static struct fuse_req *check_interrupt(
struct fuse_session *se,
1705 struct fuse_req *req)
1707 struct fuse_req *curr;
1709 for (curr = se->interrupts.next; curr != &se->interrupts;
1710 curr = curr->next) {
1711 if (curr->u.i.unique == req->unique) {
1712 req->interrupted = 1;
1718 curr = se->interrupts.next;
1719 if (curr != &se->interrupts) {
1721 list_init_req(curr);
1729 struct fuse_bmap_in *arg = (
struct fuse_bmap_in *) inarg;
1731 if (req->se->op.bmap)
1732 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1739 struct fuse_ioctl_in *arg = (
struct fuse_ioctl_in *) inarg;
1740 unsigned int flags = arg->flags;
1741 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1744 if (flags & FUSE_IOCTL_DIR &&
1750 memset(&fi, 0,
sizeof(fi));
1753 if (
sizeof(
void *) == 4 && req->se->conn.proto_minor >= 16 &&
1754 !(flags & FUSE_IOCTL_32BIT)) {
1755 req->ioctl_64bit = 1;
1758 if (req->se->op.ioctl)
1759 req->se->op.ioctl(req, nodeid, arg->cmd,
1760 (
void *)(uintptr_t)arg->arg, &fi, flags,
1761 in_buf, arg->in_size, arg->out_size);
1773 struct fuse_poll_in *arg = (
struct fuse_poll_in *) inarg;
1776 memset(&fi, 0,
sizeof(fi));
1780 if (req->se->op.poll) {
1781 struct fuse_pollhandle *ph = NULL;
1783 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1784 ph = malloc(
sizeof(
struct fuse_pollhandle));
1793 req->se->op.poll(req, nodeid, &fi, ph);
1801 struct fuse_fallocate_in *arg = (
struct fuse_fallocate_in *) inarg;
1804 memset(&fi, 0,
sizeof(fi));
1807 if (req->se->op.fallocate)
1808 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1815 struct fuse_copy_file_range_in *arg = (
struct fuse_copy_file_range_in *) inarg;
1818 memset(&fi_in, 0,
sizeof(fi_in));
1819 fi_in.
fh = arg->fh_in;
1821 memset(&fi_out, 0,
sizeof(fi_out));
1822 fi_out.
fh = arg->fh_out;
1825 if (req->se->op.copy_file_range)
1826 req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
1827 &fi_in, arg->nodeid_out,
1828 arg->off_out, &fi_out, arg->len,
1836 struct fuse_init_in *arg = (
struct fuse_init_in *) inarg;
1837 struct fuse_init_out outarg;
1838 struct fuse_session *se = req->se;
1839 size_t bufsize = se->bufsize;
1840 size_t outargsize =
sizeof(outarg);
1844 fprintf(stderr,
"INIT: %u.%u\n", arg->major, arg->minor);
1845 if (arg->major == 7 && arg->minor >= 6) {
1846 fprintf(stderr,
"flags=0x%08x\n", arg->flags);
1847 fprintf(stderr,
"max_readahead=0x%08x\n",
1848 arg->max_readahead);
1851 se->conn.proto_major = arg->major;
1852 se->conn.proto_minor = arg->minor;
1853 se->conn.capable = 0;
1856 memset(&outarg, 0,
sizeof(outarg));
1857 outarg.major = FUSE_KERNEL_VERSION;
1858 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1860 if (arg->major < 7) {
1861 fprintf(stderr,
"fuse: unsupported protocol version: %u.%u\n",
1862 arg->major, arg->minor);
1867 if (arg->major > 7) {
1869 send_reply_ok(req, &outarg,
sizeof(outarg));
1873 if (arg->minor >= 6) {
1874 if (arg->max_readahead < se->conn.max_readahead)
1875 se->conn.max_readahead = arg->max_readahead;
1876 if (arg->flags & FUSE_ASYNC_READ)
1878 if (arg->flags & FUSE_POSIX_LOCKS)
1880 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
1882 if (arg->flags & FUSE_EXPORT_SUPPORT)
1884 if (arg->flags & FUSE_DONT_MASK)
1886 if (arg->flags & FUSE_FLOCK_LOCKS)
1888 if (arg->flags & FUSE_AUTO_INVAL_DATA)
1890 if (arg->flags & FUSE_DO_READDIRPLUS)
1892 if (arg->flags & FUSE_READDIRPLUS_AUTO)
1894 if (arg->flags & FUSE_ASYNC_DIO)
1896 if (arg->flags & FUSE_WRITEBACK_CACHE)
1898 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
1900 if (arg->flags & FUSE_PARALLEL_DIROPS)
1902 if (arg->flags & FUSE_POSIX_ACL)
1904 if (arg->flags & FUSE_HANDLE_KILLPRIV)
1907 se->conn.max_readahead = 0;
1910 if (se->conn.proto_minor >= 14) {
1912 #ifdef HAVE_VMSPLICE 1918 if (se->conn.proto_minor >= 18)
1928 #define LL_SET_DEFAULT(cond, cap) \ 1929 if ((cond) && (se->conn.capable & (cap))) \ 1930 se->conn.want |= (cap) 1939 LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
1943 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1945 se->conn.time_gran = 1;
1947 if (bufsize < FUSE_MIN_READ_BUFFER) {
1948 fprintf(stderr,
"fuse: warning: buffer size too small: %zu\n",
1950 bufsize = FUSE_MIN_READ_BUFFER;
1954 if (bufsize < se->conn.max_write)
1955 se->conn.max_write = bufsize;
1959 se->op.init(se->userdata, &se->conn);
1961 if (se->conn.want & (~se->conn.capable)) {
1962 fprintf(stderr,
"fuse: error: filesystem requested capabilities " 1963 "0x%x that are not supported by kernel, aborting.\n",
1964 se->conn.want & (~se->conn.capable));
1966 se->error = -EPROTO;
1971 unsigned max_read_mo = get_max_read(se->mo);
1972 if (se->conn.max_read != max_read_mo) {
1973 fprintf(stderr,
"fuse: error: init() and fuse_session_new() " 1974 "requested different maximum read size (%u vs %u)\n",
1975 se->conn.max_read, max_read_mo);
1977 se->error = -EPROTO;
1984 outarg.flags |= FUSE_BIG_WRITES;
1987 outarg.flags |= FUSE_ASYNC_READ;
1989 outarg.flags |= FUSE_POSIX_LOCKS;
1991 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
1993 outarg.flags |= FUSE_EXPORT_SUPPORT;
1995 outarg.flags |= FUSE_DONT_MASK;
1997 outarg.flags |= FUSE_FLOCK_LOCKS;
1999 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2001 outarg.flags |= FUSE_DO_READDIRPLUS;
2003 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2005 outarg.flags |= FUSE_ASYNC_DIO;
2007 outarg.flags |= FUSE_WRITEBACK_CACHE;
2009 outarg.flags |= FUSE_POSIX_ACL;
2010 outarg.max_readahead = se->conn.max_readahead;
2011 outarg.max_write = se->conn.max_write;
2012 if (se->conn.proto_minor >= 13) {
2013 if (se->conn.max_background >= (1 << 16))
2014 se->conn.max_background = (1 << 16) - 1;
2015 if (se->conn.congestion_threshold > se->conn.max_background)
2016 se->conn.congestion_threshold = se->conn.max_background;
2017 if (!se->conn.congestion_threshold) {
2018 se->conn.congestion_threshold =
2019 se->conn.max_background * 3 / 4;
2022 outarg.max_background = se->conn.max_background;
2023 outarg.congestion_threshold = se->conn.congestion_threshold;
2025 if (se->conn.proto_minor >= 23)
2026 outarg.time_gran = se->conn.time_gran;
2029 fprintf(stderr,
" INIT: %u.%u\n", outarg.major, outarg.minor);
2030 fprintf(stderr,
" flags=0x%08x\n", outarg.flags);
2031 fprintf(stderr,
" max_readahead=0x%08x\n",
2032 outarg.max_readahead);
2033 fprintf(stderr,
" max_write=0x%08x\n", outarg.max_write);
2034 fprintf(stderr,
" max_background=%i\n",
2035 outarg.max_background);
2036 fprintf(stderr,
" congestion_threshold=%i\n",
2037 outarg.congestion_threshold);
2038 fprintf(stderr,
" time_gran=%u\n",
2042 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2043 else if (arg->minor < 23)
2044 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2046 send_reply_ok(req, &outarg, outargsize);
2051 struct fuse_session *se = req->se;
2056 se->got_destroy = 1;
2058 se->op.destroy(se->userdata);
2060 send_reply_ok(req, NULL, 0);
2063 static void list_del_nreq(
struct fuse_notify_req *nreq)
2065 struct fuse_notify_req *prev = nreq->prev;
2066 struct fuse_notify_req *next = nreq->next;
2071 static void list_add_nreq(
struct fuse_notify_req *nreq,
2072 struct fuse_notify_req *next)
2074 struct fuse_notify_req *prev = next->prev;
2081 static void list_init_nreq(
struct fuse_notify_req *nreq)
2088 const void *inarg,
const struct fuse_buf *buf)
2090 struct fuse_session *se = req->se;
2091 struct fuse_notify_req *nreq;
2092 struct fuse_notify_req *head;
2094 pthread_mutex_lock(&se->lock);
2095 head = &se->notify_list;
2096 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2097 if (nreq->unique == req->unique) {
2098 list_del_nreq(nreq);
2102 pthread_mutex_unlock(&se->lock);
2105 nreq->reply(nreq, req, nodeid, inarg, buf);
2108 static int send_notify_iov(
struct fuse_session *se,
int notify_code,
2109 struct iovec *iov,
int count)
2111 struct fuse_out_header out;
2117 out.error = notify_code;
2118 iov[0].iov_base = &out;
2119 iov[0].iov_len =
sizeof(
struct fuse_out_header);
2121 return fuse_send_msg(se, NULL, iov, count);
2127 struct fuse_notify_poll_wakeup_out outarg;
2128 struct iovec iov[2];
2132 iov[1].iov_base = &outarg;
2133 iov[1].iov_len =
sizeof(outarg);
2135 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2142 off_t off, off_t len)
2144 struct fuse_notify_inval_inode_out outarg;
2145 struct iovec iov[2];
2150 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2157 iov[1].iov_base = &outarg;
2158 iov[1].iov_len =
sizeof(outarg);
2160 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2164 const char *name,
size_t namelen)
2166 struct fuse_notify_inval_entry_out outarg;
2167 struct iovec iov[3];
2172 if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2175 outarg.parent = parent;
2176 outarg.namelen = namelen;
2179 iov[1].iov_base = &outarg;
2180 iov[1].iov_len =
sizeof(outarg);
2181 iov[2].iov_base = (
void *)name;
2182 iov[2].iov_len = namelen + 1;
2184 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2189 const char *name,
size_t namelen)
2191 struct fuse_notify_delete_out outarg;
2192 struct iovec iov[3];
2197 if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
2200 outarg.parent = parent;
2201 outarg.child = child;
2202 outarg.namelen = namelen;
2205 iov[1].iov_base = &outarg;
2206 iov[1].iov_len =
sizeof(outarg);
2207 iov[2].iov_base = (
void *)name;
2208 iov[2].iov_len = namelen + 1;
2210 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2217 struct fuse_out_header out;
2218 struct fuse_notify_store_out outarg;
2219 struct iovec iov[3];
2226 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2230 out.error = FUSE_NOTIFY_STORE;
2232 outarg.nodeid = ino;
2233 outarg.offset = offset;
2237 iov[0].iov_base = &out;
2238 iov[0].iov_len =
sizeof(out);
2239 iov[1].iov_base = &outarg;
2240 iov[1].iov_len =
sizeof(outarg);
2242 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2249 struct fuse_retrieve_req {
2250 struct fuse_notify_req nreq;
2254 static void fuse_ll_retrieve_reply(
struct fuse_notify_req *nreq,
2259 struct fuse_session *se = req->se;
2260 struct fuse_retrieve_req *rreq =
2261 container_of(nreq,
struct fuse_retrieve_req, nreq);
2262 const struct fuse_notify_retrieve_in *arg = inarg;
2269 bufv.
buf[0].
mem = PARAM(arg);
2271 bufv.
buf[0].
size -=
sizeof(
struct fuse_in_header) +
2272 sizeof(struct fuse_notify_retrieve_in);
2274 if (bufv.
buf[0].
size < arg->size) {
2275 fprintf(stderr,
"fuse: retrieve reply: buffer size too small\n");
2281 if (se->op.retrieve_reply) {
2282 se->op.retrieve_reply(req, rreq->cookie, ino,
2283 arg->offset, &bufv);
2290 fuse_ll_clear_pipe(se);
2294 size_t size, off_t offset,
void *cookie)
2296 struct fuse_notify_retrieve_out outarg;
2297 struct iovec iov[2];
2298 struct fuse_retrieve_req *rreq;
2304 if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2307 rreq = malloc(
sizeof(*rreq));
2311 pthread_mutex_lock(&se->lock);
2312 rreq->cookie = cookie;
2313 rreq->nreq.unique = se->notify_ctr++;
2314 rreq->nreq.reply = fuse_ll_retrieve_reply;
2315 list_add_nreq(&rreq->nreq, &se->notify_list);
2316 pthread_mutex_unlock(&se->lock);
2318 outarg.notify_unique = rreq->nreq.unique;
2319 outarg.nodeid = ino;
2320 outarg.offset = offset;
2324 iov[1].iov_base = &outarg;
2325 iov[1].iov_len =
sizeof(outarg);
2327 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2329 pthread_mutex_lock(&se->lock);
2330 list_del_nreq(&rreq->nreq);
2331 pthread_mutex_unlock(&se->lock);
2340 return req->se->userdata;
2351 pthread_mutex_lock(&req->lock);
2352 pthread_mutex_lock(&req->se->lock);
2353 req->u.ni.func = func;
2354 req->u.ni.data = data;
2355 pthread_mutex_unlock(&req->se->lock);
2356 if (req->interrupted && func)
2358 pthread_mutex_unlock(&req->lock);
2365 pthread_mutex_lock(&req->se->lock);
2366 interrupted = req->interrupted;
2367 pthread_mutex_unlock(&req->se->lock);
2376 [FUSE_LOOKUP] = { do_lookup,
"LOOKUP" },
2377 [FUSE_FORGET] = { do_forget,
"FORGET" },
2378 [FUSE_GETATTR] = { do_getattr,
"GETATTR" },
2379 [FUSE_SETATTR] = { do_setattr,
"SETATTR" },
2380 [FUSE_READLINK] = { do_readlink,
"READLINK" },
2381 [FUSE_SYMLINK] = { do_symlink,
"SYMLINK" },
2382 [FUSE_MKNOD] = { do_mknod,
"MKNOD" },
2383 [FUSE_MKDIR] = { do_mkdir,
"MKDIR" },
2384 [FUSE_UNLINK] = { do_unlink,
"UNLINK" },
2385 [FUSE_RMDIR] = { do_rmdir,
"RMDIR" },
2386 [FUSE_RENAME] = { do_rename,
"RENAME" },
2387 [FUSE_LINK] = { do_link,
"LINK" },
2388 [FUSE_OPEN] = { do_open,
"OPEN" },
2389 [FUSE_READ] = { do_read,
"READ" },
2390 [FUSE_WRITE] = { do_write,
"WRITE" },
2391 [FUSE_STATFS] = { do_statfs,
"STATFS" },
2392 [FUSE_RELEASE] = { do_release,
"RELEASE" },
2393 [FUSE_FSYNC] = { do_fsync,
"FSYNC" },
2394 [FUSE_SETXATTR] = { do_setxattr,
"SETXATTR" },
2395 [FUSE_GETXATTR] = { do_getxattr,
"GETXATTR" },
2396 [FUSE_LISTXATTR] = { do_listxattr,
"LISTXATTR" },
2397 [FUSE_REMOVEXATTR] = { do_removexattr,
"REMOVEXATTR" },
2398 [FUSE_FLUSH] = { do_flush,
"FLUSH" },
2399 [FUSE_INIT] = { do_init,
"INIT" },
2400 [FUSE_OPENDIR] = { do_opendir,
"OPENDIR" },
2401 [FUSE_READDIR] = { do_readdir,
"READDIR" },
2402 [FUSE_RELEASEDIR] = { do_releasedir,
"RELEASEDIR" },
2403 [FUSE_FSYNCDIR] = { do_fsyncdir,
"FSYNCDIR" },
2404 [FUSE_GETLK] = { do_getlk,
"GETLK" },
2405 [FUSE_SETLK] = { do_setlk,
"SETLK" },
2406 [FUSE_SETLKW] = { do_setlkw,
"SETLKW" },
2407 [FUSE_ACCESS] = { do_access,
"ACCESS" },
2408 [FUSE_CREATE] = { do_create,
"CREATE" },
2409 [FUSE_INTERRUPT] = { do_interrupt,
"INTERRUPT" },
2410 [FUSE_BMAP] = { do_bmap,
"BMAP" },
2411 [FUSE_IOCTL] = { do_ioctl,
"IOCTL" },
2412 [FUSE_POLL] = { do_poll,
"POLL" },
2413 [FUSE_FALLOCATE] = { do_fallocate,
"FALLOCATE" },
2414 [FUSE_DESTROY] = { do_destroy,
"DESTROY" },
2415 [FUSE_NOTIFY_REPLY] = { (
void *) 1,
"NOTIFY_REPLY" },
2416 [FUSE_BATCH_FORGET] = { do_batch_forget,
"BATCH_FORGET" },
2417 [FUSE_READDIRPLUS] = { do_readdirplus,
"READDIRPLUS"},
2418 [FUSE_RENAME2] = { do_rename2,
"RENAME2" },
2419 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range,
"COPY_FILE_RANGE" },
2420 [CUSE_INIT] = { cuse_lowlevel_init,
"CUSE_INIT" },
2423 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) 2425 static const char *opname(
enum fuse_opcode opcode)
2427 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2430 return fuse_ll_ops[opcode].name;
2433 static int fuse_ll_copy_from_pipe(
struct fuse_bufvec *dst,
2438 fprintf(stderr,
"fuse: copy from pipe: %s\n", strerror(-res));
2442 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2451 fuse_session_process_buf_int(se, buf, NULL);
2454 void fuse_session_process_buf_int(
struct fuse_session *se,
2455 const struct fuse_buf *buf,
struct fuse_chan *ch)
2457 const size_t write_header_size =
sizeof(
struct fuse_in_header) +
2458 sizeof(struct fuse_write_in);
2460 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2461 struct fuse_in_header *in;
2463 struct fuse_req *req;
2472 mbuf = malloc(tmpbuf.
buf[0].
size);
2474 fprintf(stderr,
"fuse: failed to allocate header\n");
2477 tmpbuf.
buf[0].
mem = mbuf;
2479 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2490 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2491 (
unsigned long long) in->unique,
2492 opname((
enum fuse_opcode) in->opcode), in->opcode,
2493 (
unsigned long long) in->nodeid, buf->
size, in->pid);
2496 req = fuse_ll_alloc_req(se);
2498 struct fuse_out_header out = {
2499 .unique = in->unique,
2502 struct iovec iov = {
2504 .iov_len =
sizeof(
struct fuse_out_header),
2507 fuse_send_msg(se, ch, &iov, 1);
2511 req->unique = in->unique;
2512 req->ctx.uid = in->uid;
2513 req->ctx.gid = in->gid;
2514 req->ctx.pid = in->pid;
2515 req->ch = ch ? fuse_chan_get(ch) : NULL;
2518 if (!se->got_init) {
2519 enum fuse_opcode expected;
2521 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2522 if (in->opcode != expected)
2524 }
else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2529 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2530 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2531 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2532 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2533 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2534 in->opcode != FUSE_NOTIFY_REPLY &&
2535 in->opcode != FUSE_READDIRPLUS)
2539 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2541 if (in->opcode != FUSE_INTERRUPT) {
2542 struct fuse_req *intr;
2543 pthread_mutex_lock(&se->lock);
2544 intr = check_interrupt(se, req);
2545 list_add_req(req, &se->list);
2546 pthread_mutex_unlock(&se->lock);
2552 (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2553 in->opcode != FUSE_NOTIFY_REPLY) {
2557 newmbuf = realloc(mbuf, buf->
size);
2558 if (newmbuf == NULL)
2562 tmpbuf = FUSE_BUFVEC_INIT(buf->
size - write_header_size);
2563 tmpbuf.
buf[0].
mem = mbuf + write_header_size;
2565 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2573 inarg = (
void *) &in[1];
2574 if (in->opcode == FUSE_WRITE && se->op.write_buf)
2575 do_write_buf(req, in->nodeid, inarg, buf);
2576 else if (in->opcode == FUSE_NOTIFY_REPLY)
2577 do_notify_reply(req, in->nodeid, inarg, buf);
2579 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2589 fuse_ll_clear_pipe(se);
2593 #define LL_OPTION(n,o,v) \ 2594 { n, offsetof(struct fuse_session, o), v } 2596 static const struct fuse_opt fuse_ll_opts[] = {
2597 LL_OPTION(
"debug", debug, 1),
2598 LL_OPTION(
"-d", debug, 1),
2599 LL_OPTION(
"--debug", debug, 1),
2600 LL_OPTION(
"allow_root", deny_others, 1),
2606 printf(
"using FUSE kernel interface version %i.%i\n",
2607 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2608 fuse_mount_version();
2616 " -o allow_other allow access by all users\n" 2617 " -o allow_root allow access by root\n" 2618 " -o auto_unmount auto unmount on process termination\n");
2623 struct fuse_ll_pipe *llp;
2625 if (se->got_init && !se->got_destroy) {
2627 se->op.destroy(se->userdata);
2629 llp = pthread_getspecific(se->pipe_key);
2631 fuse_ll_pipe_free(llp);
2632 pthread_key_delete(se->pipe_key);
2633 pthread_mutex_destroy(&se->lock);
2634 free(se->cuse_data);
2637 destroy_mount_opts(se->mo);
2642 static void fuse_ll_pipe_destructor(
void *data)
2644 struct fuse_ll_pipe *llp = data;
2645 fuse_ll_pipe_free(llp);
2650 return fuse_session_receive_buf_int(se, buf, NULL);
2653 int fuse_session_receive_buf_int(
struct fuse_session *se,
struct fuse_buf *buf,
2654 struct fuse_chan *ch)
2659 size_t bufsize = se->bufsize;
2660 struct fuse_ll_pipe *llp;
2666 llp = fuse_ll_get_pipe(se);
2670 if (llp->size < bufsize) {
2671 if (llp->can_grow) {
2672 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2679 if (llp->size < bufsize)
2683 res = splice(ch ? ch->fd : se->fd,
2684 NULL, llp->pipe[1], NULL, bufsize, 0);
2691 if (err == ENODEV) {
2697 if (err != EINTR && err != EAGAIN)
2698 perror(
"fuse: splice from device");
2702 if (res <
sizeof(
struct fuse_in_header)) {
2703 fprintf(stderr,
"short splice from fuse device\n");
2718 if (res <
sizeof(
struct fuse_in_header) +
2719 sizeof(
struct fuse_write_in) + pagesize) {
2724 buf->
mem = malloc(se->bufsize);
2727 "fuse: failed to allocate read buffer\n");
2731 buf->
size = se->bufsize;
2737 fprintf(stderr,
"fuse: copy from pipe: %s\n",
2739 fuse_ll_clear_pipe(se);
2742 if (res < tmpbuf.
size) {
2743 fprintf(stderr,
"fuse: copy from pipe: short read\n");
2744 fuse_ll_clear_pipe(se);
2747 assert(res == tmpbuf.
size);
2751 buf->
fd = tmpbuf.
fd;
2761 buf->
mem = malloc(se->bufsize);
2764 "fuse: failed to allocate read buffer\n");
2770 res = read(ch ? ch->fd : se->fd, buf->
mem, se->bufsize);
2781 if (err == ENODEV) {
2790 if (err != EINTR && err != EAGAIN)
2791 perror(
"fuse: reading device");
2794 if ((
size_t) res <
sizeof(
struct fuse_in_header)) {
2795 fprintf(stderr,
"short read on fuse device\n");
2804 #define KERNEL_BUF_PAGES 32 2807 #define HEADER_SIZE 0x1000 2811 size_t op_size,
void *userdata)
2814 struct fuse_session *se;
2815 struct mount_opts *mo;
2818 fprintf(stderr,
"fuse: warning: library too old, some operations may not work\n");
2822 if (args->
argc == 0) {
2823 fprintf(stderr,
"fuse: empty argv passed to fuse_session_new().\n");
2827 se = (
struct fuse_session *) calloc(1,
sizeof(
struct fuse_session));
2829 fprintf(stderr,
"fuse: failed to allocate fuse object\n");
2833 se->conn.max_write = UINT_MAX;
2834 se->conn.max_readahead = UINT_MAX;
2839 if(se->deny_others) {
2849 mo = parse_mount_opts(args);
2853 if(args->
argc == 1 &&
2854 args->
argv[0][0] ==
'-') {
2855 fprintf(stderr,
"fuse: warning: argv[0] looks like an option, but " 2856 "will be ignored\n");
2857 }
else if (args->
argc != 1) {
2859 fprintf(stderr,
"fuse: unknown option(s): `");
2860 for(i = 1; i < args->
argc-1; i++)
2861 fprintf(stderr,
"%s ", args->
argv[i]);
2862 fprintf(stderr,
"%s'\n", args->
argv[i]);
2867 fprintf(stderr,
"FUSE library version: %s\n", PACKAGE_VERSION);
2869 se->bufsize = KERNEL_BUF_PAGES * getpagesize() + HEADER_SIZE;
2871 list_init_req(&se->list);
2872 list_init_req(&se->interrupts);
2873 list_init_nreq(&se->notify_list);
2875 fuse_mutex_init(&se->lock);
2877 err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
2879 fprintf(stderr,
"fuse: failed to create thread specific key: %s\n",
2884 memcpy(&se->op, op, op_size);
2885 se->owner = getuid();
2886 se->userdata = userdata;
2892 pthread_mutex_destroy(&se->lock);
2912 fd = open(
"/dev/null", O_RDWR);
2915 }
while (fd >= 0 && fd <= 2);
2923 fd = fuse_mnt_parse_fuse_fd(mountpoint);
2925 if (fcntl(fd, F_GETFD) == -1) {
2927 "fuse: Invalid file descriptor /dev/fd/%u\n",
2936 fd = fuse_kern_mount(mountpoint, se->mo);
2942 se->mountpoint = strdup(mountpoint);
2943 if (se->mountpoint == NULL)
2949 fuse_kern_unmount(mountpoint, fd);
2960 if (se->mountpoint != NULL) {
2961 fuse_kern_unmount(se->mountpoint, se->fd);
2962 free(se->mountpoint);
2963 se->mountpoint = NULL;
2971 size_t bufsize = 1024;
2975 unsigned long pid = req->ctx.pid;
2978 sprintf(path,
"/proc/%lu/task/%lu/status", pid, pid);
2981 buf = malloc(bufsize);
2986 fd = open(path, O_RDONLY);
2990 ret = read(fd, buf, bufsize);
2997 if ((
size_t)ret == bufsize) {
3004 s = strstr(buf,
"\nGroups:");
3012 unsigned long val = strtoul(s, &end, 0);
3032 (void) req; (void) size; (void) list;
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_err(fuse_req_t req, int err)
#define FUSE_CAP_IOCTL_DIR
int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
void fuse_session_exit(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_delete(struct fuse_session *se, fuse_ino_t parent, fuse_ino_t child, const char *name, size_t namelen)
int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
void fuse_lowlevel_help(void)
#define FUSE_CAP_HANDLE_KILLPRIV
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_session_fd(struct fuse_session *se)
const struct fuse_ctx * fuse_req_ctx(fuse_req_t req)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
#define FUSE_CAP_ASYNC_READ
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_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
void(* fuse_interrupt_func_t)(fuse_req_t req, void *data)
int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
struct fuse_req * fuse_req_t
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
void * fuse_req_userdata(fuse_req_t req)
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
#define FUSE_CAP_EXPORT_SUPPORT
int fuse_reply_xattr(fuse_req_t req, size_t count)
int fuse_session_exited(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
#define FUSE_CAP_READDIRPLUS_AUTO
#define FUSE_CAP_SPLICE_WRITE
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
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_lock(fuse_req_t req, const struct flock *lock)
#define FUSE_CAP_NO_OPEN_SUPPORT
int fuse_req_interrupted(fuse_req_t req)
void fuse_session_reset(struct fuse_session *se)
void fuse_lowlevel_version(void)
int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, int count)
void fuse_reply_none(fuse_req_t req)
int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
void fuse_opt_free_args(struct fuse_args *args)
#define FUSE_CAP_SPLICE_MOVE
#define FUSE_CAP_AUTO_INVAL_DATA
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
#define FUSE_CAP_SPLICE_READ
void fuse_session_unmount(struct fuse_session *se)
enum fuse_buf_flags flags
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
#define FUSE_CAP_FLOCK_LOCKS
#define FUSE_CAP_ASYNC_DIO
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, void *data)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
int fuse_reply_write(fuse_req_t req, size_t count)
#define FUSE_CAP_WRITEBACK_CACHE
#define FUSE_CAP_POSIX_LOCKS
#define FUSE_CAP_POSIX_ACL
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
#define FUSE_CAP_ATOMIC_O_TRUNC
#define FUSE_CAP_READDIRPLUS
#define FUSE_CAP_PARALLEL_DIROPS
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
int fuse_reply_readlink(fuse_req_t req, const char *link)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
int fuse_reply_poll(fuse_req_t req, unsigned revents)
void fuse_session_process_buf(struct fuse_session *se, const struct fuse_buf *buf)
#define FUSE_CAP_DONT_MASK