2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
13 #include "fuse_kernel.h"
15 #include "fuse_misc.h"
26 #ifndef F_LINUX_SPECIFIC_BASE
27 #define F_LINUX_SPECIFIC_BASE 1024
30 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
34 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
35 #define OFFSET_MAX 0x7fffffffffffffffLL
37 #define container_of(ptr, type, member) ({ \
38 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
39 (type *)( (char *)__mptr - offsetof(type,member) );})
41 struct fuse_pollhandle
{
47 static size_t pagesize
;
49 static __attribute__((constructor
)) void fuse_ll_init_pagesize(void)
51 pagesize
= getpagesize();
54 static void convert_stat(const struct stat
*stbuf
, struct fuse_attr
*attr
)
56 attr
->ino
= stbuf
->st_ino
;
57 attr
->mode
= stbuf
->st_mode
;
58 attr
->nlink
= stbuf
->st_nlink
;
59 attr
->uid
= stbuf
->st_uid
;
60 attr
->gid
= stbuf
->st_gid
;
61 attr
->rdev
= stbuf
->st_rdev
;
62 attr
->size
= stbuf
->st_size
;
63 attr
->blksize
= stbuf
->st_blksize
;
64 attr
->blocks
= stbuf
->st_blocks
;
65 attr
->atime
= stbuf
->st_atime
;
66 attr
->mtime
= stbuf
->st_mtime
;
67 attr
->ctime
= stbuf
->st_ctime
;
68 attr
->atimensec
= ST_ATIM_NSEC(stbuf
);
69 attr
->mtimensec
= ST_MTIM_NSEC(stbuf
);
70 attr
->ctimensec
= ST_CTIM_NSEC(stbuf
);
73 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
75 stbuf
->st_mode
= attr
->mode
;
76 stbuf
->st_uid
= attr
->uid
;
77 stbuf
->st_gid
= attr
->gid
;
78 stbuf
->st_size
= attr
->size
;
79 stbuf
->st_atime
= attr
->atime
;
80 stbuf
->st_mtime
= attr
->mtime
;
81 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
82 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
85 static size_t iov_length(const struct iovec
*iov
, size_t count
)
90 for (seg
= 0; seg
< count
; seg
++)
91 ret
+= iov
[seg
].iov_len
;
95 static void list_init_req(struct fuse_req
*req
)
101 static void list_del_req(struct fuse_req
*req
)
103 struct fuse_req
*prev
= req
->prev
;
104 struct fuse_req
*next
= req
->next
;
109 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
111 struct fuse_req
*prev
= next
->prev
;
118 static void destroy_req(fuse_req_t req
)
120 pthread_mutex_destroy(&req
->lock
);
124 void fuse_free_req(fuse_req_t req
)
127 struct fuse_ll
*f
= req
->f
;
129 pthread_mutex_lock(&f
->lock
);
130 req
->u
.ni
.func
= NULL
;
131 req
->u
.ni
.data
= NULL
;
134 pthread_mutex_unlock(&f
->lock
);
139 static struct fuse_req
*fuse_ll_alloc_req(struct fuse_ll
*f
)
141 struct fuse_req
*req
;
143 req
= (struct fuse_req
*) calloc(1, sizeof(struct fuse_req
));
145 fprintf(stderr
, "fuse: failed to allocate request\n");
150 fuse_mutex_init(&req
->lock
);
156 static int fuse_chan_recv(struct fuse_session
*se
, struct fuse_buf
*buf
,
157 struct fuse_chan
*ch
)
159 struct fuse_ll
*f
= se
->f
;
164 buf
->mem
= malloc(f
->bufsize
);
167 "fuse: failed to allocate read buffer\n");
173 res
= read(fuse_chan_fd(ch
), buf
->mem
, f
->bufsize
);
176 if (fuse_session_exited(se
))
179 /* ENOENT means the operation was interrupted, it's safe
185 fuse_session_exit(se
);
188 /* Errors occurring during normal operation: EINTR (read
189 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
191 if (err
!= EINTR
&& err
!= EAGAIN
)
192 perror("fuse: reading device");
195 if ((size_t) res
< sizeof(struct fuse_in_header
)) {
196 fprintf(stderr
, "short read on fuse device\n");
205 static int fuse_chan_send(struct fuse_chan
*ch
, const struct iovec iov
[],
208 ssize_t res
= writev(fuse_chan_fd(ch
), iov
, count
);
212 struct fuse_session
*se
= fuse_chan_session(ch
);
216 /* ENOENT means the operation was interrupted */
217 if (!fuse_session_exited(se
) && err
!= ENOENT
)
218 perror("fuse: writing device");
225 void fuse_chan_close(struct fuse_chan
*ch
)
227 close(fuse_chan_fd(ch
));
231 static int fuse_send_msg(struct fuse_ll
*f
, struct fuse_chan
*ch
,
232 struct iovec
*iov
, int count
)
234 struct fuse_out_header
*out
= iov
[0].iov_base
;
236 out
->len
= iov_length(iov
, count
);
238 if (out
->unique
== 0) {
239 fprintf(stderr
, "NOTIFY: code=%d length=%u\n",
240 out
->error
, out
->len
);
241 } else if (out
->error
) {
243 " unique: %llu, error: %i (%s), outsize: %i\n",
244 (unsigned long long) out
->unique
, out
->error
,
245 strerror(-out
->error
), out
->len
);
248 " unique: %llu, success, outsize: %i\n",
249 (unsigned long long) out
->unique
, out
->len
);
253 return fuse_chan_send(ch
, iov
, count
);
256 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
259 struct fuse_out_header out
;
261 if (error
<= -1000 || error
> 0) {
262 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
266 out
.unique
= req
->unique
;
269 iov
[0].iov_base
= &out
;
270 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
272 return fuse_send_msg(req
->f
, req
->ch
, iov
, count
);
275 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
280 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
285 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
291 iov
[1].iov_base
= (void *) arg
;
292 iov
[1].iov_len
= argsize
;
295 return send_reply_iov(req
, error
, iov
, count
);
298 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
301 struct iovec
*padded_iov
;
303 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
304 if (padded_iov
== NULL
)
305 return fuse_reply_err(req
, ENOMEM
);
307 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
310 res
= send_reply_iov(req
, 0, padded_iov
, count
);
316 static size_t fuse_dirent_size(size_t namelen
)
318 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
321 static char *fuse_add_dirent(char *buf
, const char *name
,
322 const struct stat
*stbuf
, off_t off
)
324 unsigned namelen
= strlen(name
);
325 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
326 unsigned entsize
= fuse_dirent_size(namelen
);
327 unsigned padlen
= entsize
- entlen
;
328 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
330 dirent
->ino
= stbuf
->st_ino
;
332 dirent
->namelen
= namelen
;
333 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
334 strncpy(dirent
->name
, name
, namelen
);
336 memset(buf
+ entlen
, 0, padlen
);
338 return buf
+ entsize
;
341 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
342 const char *name
, const struct stat
*stbuf
, off_t off
)
347 entsize
= fuse_dirent_size(strlen(name
));
348 if (entsize
<= bufsize
&& buf
)
349 fuse_add_dirent(buf
, name
, stbuf
, off
);
353 static void convert_statfs(const struct statvfs
*stbuf
,
354 struct fuse_kstatfs
*kstatfs
)
356 kstatfs
->bsize
= stbuf
->f_bsize
;
357 kstatfs
->frsize
= stbuf
->f_frsize
;
358 kstatfs
->blocks
= stbuf
->f_blocks
;
359 kstatfs
->bfree
= stbuf
->f_bfree
;
360 kstatfs
->bavail
= stbuf
->f_bavail
;
361 kstatfs
->files
= stbuf
->f_files
;
362 kstatfs
->ffree
= stbuf
->f_ffree
;
363 kstatfs
->namelen
= stbuf
->f_namemax
;
366 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
368 return send_reply(req
, 0, arg
, argsize
);
371 int fuse_reply_err(fuse_req_t req
, int err
)
373 return send_reply(req
, -err
, NULL
, 0);
376 void fuse_reply_none(fuse_req_t req
)
381 static unsigned long calc_timeout_sec(double t
)
383 if (t
> (double) ULONG_MAX
)
388 return (unsigned long) t
;
391 static unsigned int calc_timeout_nsec(double t
)
393 double f
= t
- (double) calc_timeout_sec(t
);
396 else if (f
>= 0.999999999)
399 return (unsigned int) (f
* 1.0e9
);
402 static void fill_entry(struct fuse_entry_out
*arg
,
403 const struct fuse_entry_param
*e
)
405 arg
->nodeid
= e
->ino
;
406 arg
->generation
= e
->generation
;
407 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
408 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
409 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
410 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
411 convert_stat(&e
->attr
, &arg
->attr
);
414 size_t fuse_add_direntry_plus(fuse_req_t req
, char *buf
, size_t bufsize
,
416 const struct fuse_entry_param
*e
, off_t off
)
418 struct fuse_entry_out
*argp
;
422 entsize
= FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS
+
423 fuse_dirent_size(strlen(name
)));
424 if (entsize
<= bufsize
&& buf
){
425 argp
= (struct fuse_entry_out
*)buf
;
426 memset(argp
, 0, sizeof(*argp
));
428 fuse_add_dirent(buf
+ sizeof(*argp
), name
, &(e
->attr
), off
);
433 static void fill_open(struct fuse_open_out
*arg
,
434 const struct fuse_file_info
*f
)
438 arg
->open_flags
|= FOPEN_DIRECT_IO
;
440 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
442 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
445 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
447 struct fuse_entry_out arg
;
448 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
449 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(arg
);
451 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
453 if (!e
->ino
&& req
->f
->conn
.proto_minor
< 4)
454 return fuse_reply_err(req
, ENOENT
);
456 memset(&arg
, 0, sizeof(arg
));
458 return send_reply_ok(req
, &arg
, size
);
461 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
462 const struct fuse_file_info
*f
)
464 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
465 size_t entrysize
= req
->f
->conn
.proto_minor
< 9 ?
466 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(struct fuse_entry_out
);
467 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*) buf
;
468 struct fuse_open_out
*oarg
= (struct fuse_open_out
*) (buf
+ entrysize
);
470 memset(buf
, 0, sizeof(buf
));
473 return send_reply_ok(req
, buf
,
474 entrysize
+ sizeof(struct fuse_open_out
));
477 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
480 struct fuse_attr_out arg
;
481 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
482 FUSE_COMPAT_ATTR_OUT_SIZE
: sizeof(arg
);
484 memset(&arg
, 0, sizeof(arg
));
485 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
486 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
487 convert_stat(attr
, &arg
.attr
);
489 return send_reply_ok(req
, &arg
, size
);
492 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
494 return send_reply_ok(req
, linkname
, strlen(linkname
));
497 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
499 struct fuse_open_out arg
;
501 memset(&arg
, 0, sizeof(arg
));
503 return send_reply_ok(req
, &arg
, sizeof(arg
));
506 int fuse_reply_write(fuse_req_t req
, size_t count
)
508 struct fuse_write_out arg
;
510 memset(&arg
, 0, sizeof(arg
));
513 return send_reply_ok(req
, &arg
, sizeof(arg
));
516 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
518 return send_reply_ok(req
, buf
, size
);
521 static int fuse_send_data_iov_fallback(struct fuse_ll
*f
, struct fuse_chan
*ch
,
522 struct iovec
*iov
, int iov_count
,
523 struct fuse_bufvec
*buf
,
526 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
530 /* Optimize common case */
531 if (buf
->count
== 1 && buf
->idx
== 0 && buf
->off
== 0 &&
532 !(buf
->buf
[0].flags
& FUSE_BUF_IS_FD
)) {
533 /* FIXME: also avoid memory copy if there are multiple buffers
534 but none of them contain an fd */
536 iov
[iov_count
].iov_base
= buf
->buf
[0].mem
;
537 iov
[iov_count
].iov_len
= len
;
539 return fuse_send_msg(f
, ch
, iov
, iov_count
);
542 res
= posix_memalign(&mbuf
, pagesize
, len
);
546 mem_buf
.buf
[0].mem
= mbuf
;
547 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
554 iov
[iov_count
].iov_base
= mbuf
;
555 iov
[iov_count
].iov_len
= len
;
557 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
563 struct fuse_ll_pipe
{
569 static void fuse_ll_pipe_free(struct fuse_ll_pipe
*llp
)
577 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
578 static int fuse_pipe(int fds
[2])
585 if (fcntl(fds
[0], F_SETFL
, O_NONBLOCK
) == -1 ||
586 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
) == -1 ||
587 fcntl(fds
[0], F_SETFD
, FD_CLOEXEC
) == -1 ||
588 fcntl(fds
[1], F_SETFD
, FD_CLOEXEC
) == -1) {
596 static int fuse_pipe(int fds
[2])
598 return pipe2(fds
, O_CLOEXEC
| O_NONBLOCK
);
602 static struct fuse_ll_pipe
*fuse_ll_get_pipe(struct fuse_ll
*f
)
604 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
608 llp
= malloc(sizeof(struct fuse_ll_pipe
));
612 res
= fuse_pipe(llp
->pipe
);
619 *the default size is 16 pages on linux
621 llp
->size
= pagesize
* 16;
624 pthread_setspecific(f
->pipe_key
, llp
);
631 static void fuse_ll_clear_pipe(struct fuse_ll
*f
)
633 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
635 pthread_setspecific(f
->pipe_key
, NULL
);
636 fuse_ll_pipe_free(llp
);
640 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
641 static int read_back(int fd
, char *buf
, size_t len
)
645 res
= read(fd
, buf
, len
);
647 fprintf(stderr
, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno
));
651 fprintf(stderr
, "fuse: internal error: short read back from pipe: %i from %zi\n", res
, len
);
657 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
658 struct iovec
*iov
, int iov_count
,
659 struct fuse_bufvec
*buf
, unsigned int flags
)
662 size_t len
= fuse_buf_size(buf
);
663 struct fuse_out_header
*out
= iov
[0].iov_base
;
664 struct fuse_ll_pipe
*llp
;
667 size_t total_fd_size
;
670 struct fuse_bufvec pipe_buf
= FUSE_BUFVEC_INIT(len
);
672 if (f
->broken_splice_nonblock
)
675 if (flags
& FUSE_BUF_NO_SPLICE
)
679 for (idx
= buf
->idx
; idx
< buf
->count
; idx
++) {
680 if (buf
->buf
[idx
].flags
& FUSE_BUF_IS_FD
) {
681 total_fd_size
= buf
->buf
[idx
].size
;
683 total_fd_size
-= buf
->off
;
686 if (total_fd_size
< 2 * pagesize
)
689 if (f
->conn
.proto_minor
< 14 ||
690 !(f
->conn
.want
& FUSE_CAP_SPLICE_WRITE
))
693 llp
= fuse_ll_get_pipe(f
);
698 headerlen
= iov_length(iov
, iov_count
);
700 out
->len
= headerlen
+ len
;
703 * Heuristic for the required pipe size, does not work if the
704 * source contains less than page size fragments
706 pipesize
= pagesize
* (iov_count
+ buf
->count
+ 1) + out
->len
;
708 if (llp
->size
< pipesize
) {
710 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, pipesize
);
717 if (llp
->size
< pipesize
)
722 res
= vmsplice(llp
->pipe
[1], iov
, iov_count
, SPLICE_F_NONBLOCK
);
726 if (res
!= headerlen
) {
728 fprintf(stderr
, "fuse: short vmsplice to pipe: %u/%zu\n", res
,
733 pipe_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
;
734 pipe_buf
.buf
[0].fd
= llp
->pipe
[1];
736 res
= fuse_buf_copy(&pipe_buf
, buf
,
737 FUSE_BUF_FORCE_SPLICE
| FUSE_BUF_SPLICE_NONBLOCK
);
739 if (res
== -EAGAIN
|| res
== -EINVAL
) {
741 * Should only get EAGAIN on kernels with
742 * broken SPLICE_F_NONBLOCK support (<=
743 * 2.6.35) where this error or a short read is
744 * returned even if the pipe itself is not
747 * EINVAL might mean that splice can't handle
748 * this combination of input and output.
751 f
->broken_splice_nonblock
= 1;
753 pthread_setspecific(f
->pipe_key
, NULL
);
754 fuse_ll_pipe_free(llp
);
761 if (res
!= 0 && res
< len
) {
762 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
764 size_t now_len
= res
;
766 * For regular files a short count is either
768 * 2) because of broken SPLICE_F_NONBLOCK (see above)
770 * For other inputs it's possible that we overflowed
771 * the pipe because of small buffer fragments.
774 res
= posix_memalign(&mbuf
, pagesize
, len
);
778 mem_buf
.buf
[0].mem
= mbuf
;
779 mem_buf
.off
= now_len
;
780 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
783 size_t extra_len
= res
;
785 * Trickiest case: got more data. Need to get
786 * back the data from the pipe and then fall
787 * back to regular write.
789 tmpbuf
= malloc(headerlen
);
790 if (tmpbuf
== NULL
) {
795 res
= read_back(llp
->pipe
[0], tmpbuf
, headerlen
);
801 res
= read_back(llp
->pipe
[0], mbuf
, now_len
);
806 len
= now_len
+ extra_len
;
807 iov
[iov_count
].iov_base
= mbuf
;
808 iov
[iov_count
].iov_len
= len
;
810 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
818 out
->len
= headerlen
+ len
;
822 " unique: %llu, success, outsize: %i (splice)\n",
823 (unsigned long long) out
->unique
, out
->len
);
827 if ((flags
& FUSE_BUF_SPLICE_MOVE
) &&
828 (f
->conn
.want
& FUSE_CAP_SPLICE_MOVE
))
829 splice_flags
|= SPLICE_F_MOVE
;
831 res
= splice(llp
->pipe
[0], NULL
,
832 fuse_chan_fd(ch
), NULL
, out
->len
, splice_flags
);
835 perror("fuse: splice from pipe");
838 if (res
!= out
->len
) {
840 fprintf(stderr
, "fuse: short splice from pipe: %u/%u\n",
847 fuse_ll_clear_pipe(f
);
851 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
854 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
855 struct iovec
*iov
, int iov_count
,
856 struct fuse_bufvec
*buf
, unsigned int flags
)
858 size_t len
= fuse_buf_size(buf
);
861 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
865 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
,
866 enum fuse_buf_copy_flags flags
)
869 struct fuse_out_header out
;
872 iov
[0].iov_base
= &out
;
873 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
875 out
.unique
= req
->unique
;
878 res
= fuse_send_data_iov(req
->f
, req
->ch
, iov
, 1, bufv
, flags
);
883 return fuse_reply_err(req
, res
);
887 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
889 struct fuse_statfs_out arg
;
890 size_t size
= req
->f
->conn
.proto_minor
< 4 ?
891 FUSE_COMPAT_STATFS_SIZE
: sizeof(arg
);
893 memset(&arg
, 0, sizeof(arg
));
894 convert_statfs(stbuf
, &arg
.st
);
896 return send_reply_ok(req
, &arg
, size
);
899 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
901 struct fuse_getxattr_out arg
;
903 memset(&arg
, 0, sizeof(arg
));
906 return send_reply_ok(req
, &arg
, sizeof(arg
));
909 int fuse_reply_lock(fuse_req_t req
, const struct flock
*lock
)
911 struct fuse_lk_out arg
;
913 memset(&arg
, 0, sizeof(arg
));
914 arg
.lk
.type
= lock
->l_type
;
915 if (lock
->l_type
!= F_UNLCK
) {
916 arg
.lk
.start
= lock
->l_start
;
917 if (lock
->l_len
== 0)
918 arg
.lk
.end
= OFFSET_MAX
;
920 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
922 arg
.lk
.pid
= lock
->l_pid
;
923 return send_reply_ok(req
, &arg
, sizeof(arg
));
926 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
928 struct fuse_bmap_out arg
;
930 memset(&arg
, 0, sizeof(arg
));
933 return send_reply_ok(req
, &arg
, sizeof(arg
));
936 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
939 struct fuse_ioctl_iovec
*fiov
;
942 fiov
= malloc(sizeof(fiov
[0]) * count
);
946 for (i
= 0; i
< count
; i
++) {
947 fiov
[i
].base
= (uintptr_t) iov
[i
].iov_base
;
948 fiov
[i
].len
= iov
[i
].iov_len
;
954 int fuse_reply_ioctl_retry(fuse_req_t req
,
955 const struct iovec
*in_iov
, size_t in_count
,
956 const struct iovec
*out_iov
, size_t out_count
)
958 struct fuse_ioctl_out arg
;
959 struct fuse_ioctl_iovec
*in_fiov
= NULL
;
960 struct fuse_ioctl_iovec
*out_fiov
= NULL
;
965 memset(&arg
, 0, sizeof(arg
));
966 arg
.flags
|= FUSE_IOCTL_RETRY
;
967 arg
.in_iovs
= in_count
;
968 arg
.out_iovs
= out_count
;
969 iov
[count
].iov_base
= &arg
;
970 iov
[count
].iov_len
= sizeof(arg
);
973 if (req
->f
->conn
.proto_minor
< 16) {
975 iov
[count
].iov_base
= (void *)in_iov
;
976 iov
[count
].iov_len
= sizeof(in_iov
[0]) * in_count
;
981 iov
[count
].iov_base
= (void *)out_iov
;
982 iov
[count
].iov_len
= sizeof(out_iov
[0]) * out_count
;
986 /* Can't handle non-compat 64bit ioctls on 32bit */
987 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
988 res
= fuse_reply_err(req
, EINVAL
);
993 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
997 iov
[count
].iov_base
= (void *)in_fiov
;
998 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
1002 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
1006 iov
[count
].iov_base
= (void *)out_fiov
;
1007 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
1012 res
= send_reply_iov(req
, 0, iov
, count
);
1020 res
= fuse_reply_err(req
, ENOMEM
);
1024 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
1026 struct fuse_ioctl_out arg
;
1027 struct iovec iov
[3];
1030 memset(&arg
, 0, sizeof(arg
));
1031 arg
.result
= result
;
1032 iov
[count
].iov_base
= &arg
;
1033 iov
[count
].iov_len
= sizeof(arg
);
1037 iov
[count
].iov_base
= (char *) buf
;
1038 iov
[count
].iov_len
= size
;
1042 return send_reply_iov(req
, 0, iov
, count
);
1045 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
1048 struct iovec
*padded_iov
;
1049 struct fuse_ioctl_out arg
;
1052 padded_iov
= malloc((count
+ 2) * sizeof(struct iovec
));
1053 if (padded_iov
== NULL
)
1054 return fuse_reply_err(req
, ENOMEM
);
1056 memset(&arg
, 0, sizeof(arg
));
1057 arg
.result
= result
;
1058 padded_iov
[1].iov_base
= &arg
;
1059 padded_iov
[1].iov_len
= sizeof(arg
);
1061 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
1063 res
= send_reply_iov(req
, 0, padded_iov
, count
+ 2);
1069 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
1071 struct fuse_poll_out arg
;
1073 memset(&arg
, 0, sizeof(arg
));
1074 arg
.revents
= revents
;
1076 return send_reply_ok(req
, &arg
, sizeof(arg
));
1079 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1081 char *name
= (char *) inarg
;
1083 if (req
->f
->op
.lookup
)
1084 req
->f
->op
.lookup(req
, nodeid
, name
);
1086 fuse_reply_err(req
, ENOSYS
);
1089 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1091 struct fuse_forget_in
*arg
= (struct fuse_forget_in
*) inarg
;
1093 if (req
->f
->op
.forget
)
1094 req
->f
->op
.forget(req
, nodeid
, arg
->nlookup
);
1096 fuse_reply_none(req
);
1099 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
1102 struct fuse_batch_forget_in
*arg
= (void *) inarg
;
1103 struct fuse_forget_one
*param
= (void *) PARAM(arg
);
1108 if (req
->f
->op
.forget_multi
) {
1109 req
->f
->op
.forget_multi(req
, arg
->count
,
1110 (struct fuse_forget_data
*) param
);
1111 } else if (req
->f
->op
.forget
) {
1112 for (i
= 0; i
< arg
->count
; i
++) {
1113 struct fuse_forget_one
*forget
= ¶m
[i
];
1114 struct fuse_req
*dummy_req
;
1116 dummy_req
= fuse_ll_alloc_req(req
->f
);
1117 if (dummy_req
== NULL
)
1120 dummy_req
->unique
= req
->unique
;
1121 dummy_req
->ctx
= req
->ctx
;
1122 dummy_req
->ch
= NULL
;
1124 req
->f
->op
.forget(dummy_req
, forget
->nodeid
,
1127 fuse_reply_none(req
);
1129 fuse_reply_none(req
);
1133 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1135 struct fuse_file_info
*fip
= NULL
;
1136 struct fuse_file_info fi
;
1138 if (req
->f
->conn
.proto_minor
>= 9) {
1139 struct fuse_getattr_in
*arg
= (struct fuse_getattr_in
*) inarg
;
1141 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
1142 memset(&fi
, 0, sizeof(fi
));
1149 if (req
->f
->op
.getattr
)
1150 req
->f
->op
.getattr(req
, nodeid
, fip
);
1152 fuse_reply_err(req
, ENOSYS
);
1155 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1157 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*) inarg
;
1159 if (req
->f
->op
.setattr
) {
1160 struct fuse_file_info
*fi
= NULL
;
1161 struct fuse_file_info fi_store
;
1163 memset(&stbuf
, 0, sizeof(stbuf
));
1164 convert_attr(arg
, &stbuf
);
1165 if (arg
->valid
& FATTR_FH
) {
1166 arg
->valid
&= ~FATTR_FH
;
1167 memset(&fi_store
, 0, sizeof(fi_store
));
1170 fi
->fh_old
= fi
->fh
;
1173 FUSE_SET_ATTR_MODE
|
1176 FUSE_SET_ATTR_SIZE
|
1177 FUSE_SET_ATTR_ATIME
|
1178 FUSE_SET_ATTR_MTIME
|
1179 FUSE_SET_ATTR_ATIME_NOW
|
1180 FUSE_SET_ATTR_MTIME_NOW
;
1182 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
1184 fuse_reply_err(req
, ENOSYS
);
1187 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1189 struct fuse_access_in
*arg
= (struct fuse_access_in
*) inarg
;
1191 if (req
->f
->op
.access
)
1192 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
1194 fuse_reply_err(req
, ENOSYS
);
1197 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1201 if (req
->f
->op
.readlink
)
1202 req
->f
->op
.readlink(req
, nodeid
);
1204 fuse_reply_err(req
, ENOSYS
);
1207 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1209 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*) inarg
;
1210 char *name
= PARAM(arg
);
1212 if (req
->f
->conn
.proto_minor
>= 12)
1213 req
->ctx
.umask
= arg
->umask
;
1215 name
= (char *) inarg
+ FUSE_COMPAT_MKNOD_IN_SIZE
;
1217 if (req
->f
->op
.mknod
)
1218 req
->f
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
1220 fuse_reply_err(req
, ENOSYS
);
1223 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1225 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*) inarg
;
1227 if (req
->f
->conn
.proto_minor
>= 12)
1228 req
->ctx
.umask
= arg
->umask
;
1230 if (req
->f
->op
.mkdir
)
1231 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
1233 fuse_reply_err(req
, ENOSYS
);
1236 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1238 char *name
= (char *) inarg
;
1240 if (req
->f
->op
.unlink
)
1241 req
->f
->op
.unlink(req
, nodeid
, name
);
1243 fuse_reply_err(req
, ENOSYS
);
1246 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1248 char *name
= (char *) inarg
;
1250 if (req
->f
->op
.rmdir
)
1251 req
->f
->op
.rmdir(req
, nodeid
, name
);
1253 fuse_reply_err(req
, ENOSYS
);
1256 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1258 char *name
= (char *) inarg
;
1259 char *linkname
= ((char *) inarg
) + strlen((char *) inarg
) + 1;
1261 if (req
->f
->op
.symlink
)
1262 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
1264 fuse_reply_err(req
, ENOSYS
);
1267 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1269 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*) inarg
;
1270 char *oldname
= PARAM(arg
);
1271 char *newname
= oldname
+ strlen(oldname
) + 1;
1273 if (req
->f
->op
.rename
)
1274 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
1276 fuse_reply_err(req
, ENOSYS
);
1279 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1281 struct fuse_link_in
*arg
= (struct fuse_link_in
*) inarg
;
1283 if (req
->f
->op
.link
)
1284 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
1286 fuse_reply_err(req
, ENOSYS
);
1289 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1291 struct fuse_create_in
*arg
= (struct fuse_create_in
*) inarg
;
1293 if (req
->f
->op
.create
) {
1294 struct fuse_file_info fi
;
1295 char *name
= PARAM(arg
);
1297 memset(&fi
, 0, sizeof(fi
));
1298 fi
.flags
= arg
->flags
;
1300 if (req
->f
->conn
.proto_minor
>= 12)
1301 req
->ctx
.umask
= arg
->umask
;
1303 name
= (char *) inarg
+ sizeof(struct fuse_open_in
);
1305 req
->f
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1307 fuse_reply_err(req
, ENOSYS
);
1310 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1312 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1313 struct fuse_file_info fi
;
1315 memset(&fi
, 0, sizeof(fi
));
1316 fi
.flags
= arg
->flags
;
1318 if (req
->f
->op
.open
)
1319 req
->f
->op
.open(req
, nodeid
, &fi
);
1321 fuse_reply_open(req
, &fi
);
1324 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1326 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1328 if (req
->f
->op
.read
) {
1329 struct fuse_file_info fi
;
1331 memset(&fi
, 0, sizeof(fi
));
1334 if (req
->f
->conn
.proto_minor
>= 9) {
1335 fi
.lock_owner
= arg
->lock_owner
;
1336 fi
.flags
= arg
->flags
;
1338 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1340 fuse_reply_err(req
, ENOSYS
);
1343 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1345 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1346 struct fuse_file_info fi
;
1349 memset(&fi
, 0, sizeof(fi
));
1352 fi
.writepage
= arg
->write_flags
& 1;
1354 if (req
->f
->conn
.proto_minor
< 9) {
1355 param
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1357 fi
.lock_owner
= arg
->lock_owner
;
1358 fi
.flags
= arg
->flags
;
1362 if (req
->f
->op
.write
)
1363 req
->f
->op
.write(req
, nodeid
, param
, arg
->size
,
1366 fuse_reply_err(req
, ENOSYS
);
1369 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
,
1370 const struct fuse_buf
*ibuf
)
1372 struct fuse_ll
*f
= req
->f
;
1373 struct fuse_bufvec bufv
= {
1377 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1378 struct fuse_file_info fi
;
1380 memset(&fi
, 0, sizeof(fi
));
1383 fi
.writepage
= arg
->write_flags
& 1;
1385 if (req
->f
->conn
.proto_minor
< 9) {
1386 bufv
.buf
[0].mem
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1387 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1388 FUSE_COMPAT_WRITE_IN_SIZE
;
1389 assert(!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1391 fi
.lock_owner
= arg
->lock_owner
;
1392 fi
.flags
= arg
->flags
;
1393 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
1394 bufv
.buf
[0].mem
= PARAM(arg
);
1396 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1397 sizeof(struct fuse_write_in
);
1399 if (bufv
.buf
[0].size
< arg
->size
) {
1400 fprintf(stderr
, "fuse: do_write_buf: buffer size too small\n");
1401 fuse_reply_err(req
, EIO
);
1404 bufv
.buf
[0].size
= arg
->size
;
1406 req
->f
->op
.write_buf(req
, nodeid
, &bufv
, arg
->offset
, &fi
);
1409 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1410 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
1411 fuse_ll_clear_pipe(f
);
1414 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1416 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*) inarg
;
1417 struct fuse_file_info fi
;
1419 memset(&fi
, 0, sizeof(fi
));
1423 if (req
->f
->conn
.proto_minor
>= 7)
1424 fi
.lock_owner
= arg
->lock_owner
;
1426 if (req
->f
->op
.flush
)
1427 req
->f
->op
.flush(req
, nodeid
, &fi
);
1429 fuse_reply_err(req
, ENOSYS
);
1432 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1434 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1435 struct fuse_file_info fi
;
1437 memset(&fi
, 0, sizeof(fi
));
1438 fi
.flags
= arg
->flags
;
1441 if (req
->f
->conn
.proto_minor
>= 8) {
1442 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1443 fi
.lock_owner
= arg
->lock_owner
;
1445 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1446 fi
.flock_release
= 1;
1447 fi
.lock_owner
= arg
->lock_owner
;
1450 if (req
->f
->op
.release
)
1451 req
->f
->op
.release(req
, nodeid
, &fi
);
1453 fuse_reply_err(req
, 0);
1456 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1458 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1459 struct fuse_file_info fi
;
1461 memset(&fi
, 0, sizeof(fi
));
1465 if (req
->f
->op
.fsync
)
1466 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1468 fuse_reply_err(req
, ENOSYS
);
1471 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1473 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1474 struct fuse_file_info fi
;
1476 memset(&fi
, 0, sizeof(fi
));
1477 fi
.flags
= arg
->flags
;
1479 if (req
->f
->op
.opendir
)
1480 req
->f
->op
.opendir(req
, nodeid
, &fi
);
1482 fuse_reply_open(req
, &fi
);
1485 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1487 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1488 struct fuse_file_info fi
;
1490 memset(&fi
, 0, sizeof(fi
));
1494 if (req
->f
->op
.readdir
)
1495 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1497 fuse_reply_err(req
, ENOSYS
);
1500 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1502 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1503 struct fuse_file_info fi
;
1505 memset(&fi
, 0, sizeof(fi
));
1509 if (req
->f
->op
.readdirplus
)
1510 req
->f
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1512 fuse_reply_err(req
, ENOSYS
);
1515 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1517 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1518 struct fuse_file_info fi
;
1520 memset(&fi
, 0, sizeof(fi
));
1521 fi
.flags
= arg
->flags
;
1525 if (req
->f
->op
.releasedir
)
1526 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
1528 fuse_reply_err(req
, 0);
1531 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1533 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1534 struct fuse_file_info fi
;
1536 memset(&fi
, 0, sizeof(fi
));
1540 if (req
->f
->op
.fsyncdir
)
1541 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1543 fuse_reply_err(req
, ENOSYS
);
1546 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1551 if (req
->f
->op
.statfs
)
1552 req
->f
->op
.statfs(req
, nodeid
);
1554 struct statvfs buf
= {
1558 fuse_reply_statfs(req
, &buf
);
1562 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1564 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*) inarg
;
1565 char *name
= PARAM(arg
);
1566 char *value
= name
+ strlen(name
) + 1;
1568 if (req
->f
->op
.setxattr
)
1569 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
,
1572 fuse_reply_err(req
, ENOSYS
);
1575 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1577 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1579 if (req
->f
->op
.getxattr
)
1580 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
1582 fuse_reply_err(req
, ENOSYS
);
1585 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1587 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1589 if (req
->f
->op
.listxattr
)
1590 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
1592 fuse_reply_err(req
, ENOSYS
);
1595 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1597 char *name
= (char *) inarg
;
1599 if (req
->f
->op
.removexattr
)
1600 req
->f
->op
.removexattr(req
, nodeid
, name
);
1602 fuse_reply_err(req
, ENOSYS
);
1605 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1606 struct flock
*flock
)
1608 memset(flock
, 0, sizeof(struct flock
));
1609 flock
->l_type
= fl
->type
;
1610 flock
->l_whence
= SEEK_SET
;
1611 flock
->l_start
= fl
->start
;
1612 if (fl
->end
== OFFSET_MAX
)
1615 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1616 flock
->l_pid
= fl
->pid
;
1619 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1621 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1622 struct fuse_file_info fi
;
1625 memset(&fi
, 0, sizeof(fi
));
1627 fi
.lock_owner
= arg
->owner
;
1629 convert_fuse_file_lock(&arg
->lk
, &flock
);
1630 if (req
->f
->op
.getlk
)
1631 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1633 fuse_reply_err(req
, ENOSYS
);
1636 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1637 const void *inarg
, int sleep
)
1639 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1640 struct fuse_file_info fi
;
1643 memset(&fi
, 0, sizeof(fi
));
1645 fi
.lock_owner
= arg
->owner
;
1647 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1650 switch (arg
->lk
.type
) {
1664 if (req
->f
->op
.flock
)
1665 req
->f
->op
.flock(req
, nodeid
, &fi
, op
);
1667 fuse_reply_err(req
, ENOSYS
);
1669 convert_fuse_file_lock(&arg
->lk
, &flock
);
1670 if (req
->f
->op
.setlk
)
1671 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1673 fuse_reply_err(req
, ENOSYS
);
1677 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1679 do_setlk_common(req
, nodeid
, inarg
, 0);
1682 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1684 do_setlk_common(req
, nodeid
, inarg
, 1);
1687 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
1689 struct fuse_req
*curr
;
1691 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
1692 if (curr
->unique
== req
->u
.i
.unique
) {
1693 fuse_interrupt_func_t func
;
1697 pthread_mutex_unlock(&f
->lock
);
1699 /* Ugh, ugly locking */
1700 pthread_mutex_lock(&curr
->lock
);
1701 pthread_mutex_lock(&f
->lock
);
1702 curr
->interrupted
= 1;
1703 func
= curr
->u
.ni
.func
;
1704 data
= curr
->u
.ni
.data
;
1705 pthread_mutex_unlock(&f
->lock
);
1708 pthread_mutex_unlock(&curr
->lock
);
1710 pthread_mutex_lock(&f
->lock
);
1718 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1719 curr
= curr
->next
) {
1720 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
1726 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1728 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*) inarg
;
1729 struct fuse_ll
*f
= req
->f
;
1733 fprintf(stderr
, "INTERRUPT: %llu\n",
1734 (unsigned long long) arg
->unique
);
1736 req
->u
.i
.unique
= arg
->unique
;
1738 pthread_mutex_lock(&f
->lock
);
1739 if (find_interrupted(f
, req
))
1742 list_add_req(req
, &f
->interrupts
);
1743 pthread_mutex_unlock(&f
->lock
);
1746 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
1748 struct fuse_req
*curr
;
1750 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1751 curr
= curr
->next
) {
1752 if (curr
->u
.i
.unique
== req
->unique
) {
1753 req
->interrupted
= 1;
1759 curr
= f
->interrupts
.next
;
1760 if (curr
!= &f
->interrupts
) {
1762 list_init_req(curr
);
1768 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1770 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*) inarg
;
1772 if (req
->f
->op
.bmap
)
1773 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1775 fuse_reply_err(req
, ENOSYS
);
1778 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1780 struct fuse_ioctl_in
*arg
= (struct fuse_ioctl_in
*) inarg
;
1781 unsigned int flags
= arg
->flags
;
1782 void *in_buf
= arg
->in_size
? PARAM(arg
) : NULL
;
1783 struct fuse_file_info fi
;
1785 if (flags
& FUSE_IOCTL_DIR
&&
1786 !(req
->f
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1787 fuse_reply_err(req
, ENOTTY
);
1791 memset(&fi
, 0, sizeof(fi
));
1795 if (sizeof(void *) == 4 && req
->f
->conn
.proto_minor
>= 16 &&
1796 !(flags
& FUSE_IOCTL_32BIT
)) {
1797 req
->ioctl_64bit
= 1;
1800 if (req
->f
->op
.ioctl
)
1801 req
->f
->op
.ioctl(req
, nodeid
, arg
->cmd
,
1802 (void *)(uintptr_t)arg
->arg
, &fi
, flags
,
1803 in_buf
, arg
->in_size
, arg
->out_size
);
1805 fuse_reply_err(req
, ENOSYS
);
1808 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1813 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1815 struct fuse_poll_in
*arg
= (struct fuse_poll_in
*) inarg
;
1816 struct fuse_file_info fi
;
1818 memset(&fi
, 0, sizeof(fi
));
1821 fi
.poll_events
= arg
->events
;
1823 if (req
->f
->op
.poll
) {
1824 struct fuse_pollhandle
*ph
= NULL
;
1826 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1827 ph
= malloc(sizeof(struct fuse_pollhandle
));
1829 fuse_reply_err(req
, ENOMEM
);
1837 req
->f
->op
.poll(req
, nodeid
, &fi
, ph
);
1839 fuse_reply_err(req
, ENOSYS
);
1843 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1845 struct fuse_fallocate_in
*arg
= (struct fuse_fallocate_in
*) inarg
;
1846 struct fuse_file_info fi
;
1848 memset(&fi
, 0, sizeof(fi
));
1851 if (req
->f
->op
.fallocate
)
1852 req
->f
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
, &fi
);
1854 fuse_reply_err(req
, ENOSYS
);
1857 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1859 struct fuse_init_in
*arg
= (struct fuse_init_in
*) inarg
;
1860 struct fuse_init_out outarg
;
1861 struct fuse_ll
*f
= req
->f
;
1862 size_t bufsize
= f
->bufsize
;
1866 fprintf(stderr
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1867 if (arg
->major
== 7 && arg
->minor
>= 6) {
1868 fprintf(stderr
, "flags=0x%08x\n", arg
->flags
);
1869 fprintf(stderr
, "max_readahead=0x%08x\n",
1870 arg
->max_readahead
);
1873 f
->conn
.proto_major
= arg
->major
;
1874 f
->conn
.proto_minor
= arg
->minor
;
1875 f
->conn
.capable
= 0;
1878 memset(&outarg
, 0, sizeof(outarg
));
1879 outarg
.major
= FUSE_KERNEL_VERSION
;
1880 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1882 if (arg
->major
< 7) {
1883 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
1884 arg
->major
, arg
->minor
);
1885 fuse_reply_err(req
, EPROTO
);
1889 if (arg
->major
> 7) {
1890 /* Wait for a second INIT request with a 7.X version */
1891 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1895 if (arg
->minor
>= 6) {
1896 if (f
->conn
.async_read
)
1897 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
1898 if (arg
->max_readahead
< f
->conn
.max_readahead
)
1899 f
->conn
.max_readahead
= arg
->max_readahead
;
1900 if (arg
->flags
& FUSE_ASYNC_READ
)
1901 f
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1902 if (arg
->flags
& FUSE_POSIX_LOCKS
)
1903 f
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1904 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
)
1905 f
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1906 if (arg
->flags
& FUSE_EXPORT_SUPPORT
)
1907 f
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1908 if (arg
->flags
& FUSE_BIG_WRITES
)
1909 f
->conn
.capable
|= FUSE_CAP_BIG_WRITES
;
1910 if (arg
->flags
& FUSE_DONT_MASK
)
1911 f
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1912 if (arg
->flags
& FUSE_FLOCK_LOCKS
)
1913 f
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1914 if (arg
->flags
& FUSE_AUTO_INVAL_DATA
)
1915 f
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
1916 if (arg
->flags
& FUSE_DO_READDIRPLUS
)
1917 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
1918 if (arg
->flags
& FUSE_READDIRPLUS_AUTO
)
1919 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
1921 f
->conn
.async_read
= 0;
1922 f
->conn
.max_readahead
= 0;
1925 if (req
->f
->conn
.proto_minor
>= 14) {
1927 #ifdef HAVE_VMSPLICE
1928 f
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1929 if (f
->splice_write
)
1930 f
->conn
.want
|= FUSE_CAP_SPLICE_WRITE
;
1932 f
->conn
.want
|= FUSE_CAP_SPLICE_MOVE
;
1934 f
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1936 f
->conn
.want
|= FUSE_CAP_SPLICE_READ
;
1939 if (req
->f
->conn
.proto_minor
>= 18)
1940 f
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
1942 if (f
->atomic_o_trunc
)
1943 f
->conn
.want
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1944 if (f
->op
.getlk
&& f
->op
.setlk
&& !f
->no_remote_posix_lock
)
1945 f
->conn
.want
|= FUSE_CAP_POSIX_LOCKS
;
1946 if (f
->op
.flock
&& !f
->no_remote_flock
)
1947 f
->conn
.want
|= FUSE_CAP_FLOCK_LOCKS
;
1949 f
->conn
.want
|= FUSE_CAP_BIG_WRITES
;
1950 if (f
->auto_inval_data
)
1951 f
->conn
.want
|= FUSE_CAP_AUTO_INVAL_DATA
;
1952 if (f
->op
.readdirplus
&& !f
->no_readdirplus
) {
1953 f
->conn
.want
|= FUSE_CAP_READDIRPLUS
;
1954 if (!f
->no_readdirplus_auto
)
1955 f
->conn
.want
|= FUSE_CAP_READDIRPLUS_AUTO
;
1958 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1959 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
1961 bufsize
= FUSE_MIN_READ_BUFFER
;
1965 if (bufsize
< f
->conn
.max_write
)
1966 f
->conn
.max_write
= bufsize
;
1970 f
->op
.init(f
->userdata
, &f
->conn
);
1972 if (f
->no_splice_read
)
1973 f
->conn
.want
&= ~FUSE_CAP_SPLICE_READ
;
1974 if (f
->no_splice_write
)
1975 f
->conn
.want
&= ~FUSE_CAP_SPLICE_WRITE
;
1976 if (f
->no_splice_move
)
1977 f
->conn
.want
&= ~FUSE_CAP_SPLICE_MOVE
;
1978 if (f
->no_auto_inval_data
)
1979 f
->conn
.want
&= ~FUSE_CAP_AUTO_INVAL_DATA
;
1980 if (f
->no_readdirplus
)
1981 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS
;
1982 if (f
->no_readdirplus_auto
)
1983 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS_AUTO
;
1984 if (f
->conn
.async_read
|| (f
->conn
.want
& FUSE_CAP_ASYNC_READ
))
1985 outarg
.flags
|= FUSE_ASYNC_READ
;
1986 if (f
->conn
.want
& FUSE_CAP_POSIX_LOCKS
)
1987 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1988 if (f
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
)
1989 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
1990 if (f
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
)
1991 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
1992 if (f
->conn
.want
& FUSE_CAP_BIG_WRITES
)
1993 outarg
.flags
|= FUSE_BIG_WRITES
;
1994 if (f
->conn
.want
& FUSE_CAP_DONT_MASK
)
1995 outarg
.flags
|= FUSE_DONT_MASK
;
1996 if (f
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
)
1997 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
1998 if (f
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
)
1999 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
2000 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS
)
2001 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
2002 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
)
2003 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
2004 outarg
.max_readahead
= f
->conn
.max_readahead
;
2005 outarg
.max_write
= f
->conn
.max_write
;
2006 if (f
->conn
.proto_minor
>= 13) {
2007 if (f
->conn
.max_background
>= (1 << 16))
2008 f
->conn
.max_background
= (1 << 16) - 1;
2009 if (f
->conn
.congestion_threshold
> f
->conn
.max_background
)
2010 f
->conn
.congestion_threshold
= f
->conn
.max_background
;
2011 if (!f
->conn
.congestion_threshold
) {
2012 f
->conn
.congestion_threshold
=
2013 f
->conn
.max_background
* 3 / 4;
2016 outarg
.max_background
= f
->conn
.max_background
;
2017 outarg
.congestion_threshold
= f
->conn
.congestion_threshold
;
2021 fprintf(stderr
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
2022 fprintf(stderr
, " flags=0x%08x\n", outarg
.flags
);
2023 fprintf(stderr
, " max_readahead=0x%08x\n",
2024 outarg
.max_readahead
);
2025 fprintf(stderr
, " max_write=0x%08x\n", outarg
.max_write
);
2026 fprintf(stderr
, " max_background=%i\n",
2027 outarg
.max_background
);
2028 fprintf(stderr
, " congestion_threshold=%i\n",
2029 outarg
.congestion_threshold
);
2032 send_reply_ok(req
, &outarg
, arg
->minor
< 5 ? 8 : sizeof(outarg
));
2035 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
2037 struct fuse_ll
*f
= req
->f
;
2044 f
->op
.destroy(f
->userdata
);
2046 send_reply_ok(req
, NULL
, 0);
2049 static void list_del_nreq(struct fuse_notify_req
*nreq
)
2051 struct fuse_notify_req
*prev
= nreq
->prev
;
2052 struct fuse_notify_req
*next
= nreq
->next
;
2057 static void list_add_nreq(struct fuse_notify_req
*nreq
,
2058 struct fuse_notify_req
*next
)
2060 struct fuse_notify_req
*prev
= next
->prev
;
2067 static void list_init_nreq(struct fuse_notify_req
*nreq
)
2073 static void do_notify_reply(fuse_req_t req
, fuse_ino_t nodeid
,
2074 const void *inarg
, const struct fuse_buf
*buf
)
2076 struct fuse_ll
*f
= req
->f
;
2077 struct fuse_notify_req
*nreq
;
2078 struct fuse_notify_req
*head
;
2080 pthread_mutex_lock(&f
->lock
);
2081 head
= &f
->notify_list
;
2082 for (nreq
= head
->next
; nreq
!= head
; nreq
= nreq
->next
) {
2083 if (nreq
->unique
== req
->unique
) {
2084 list_del_nreq(nreq
);
2088 pthread_mutex_unlock(&f
->lock
);
2091 nreq
->reply(nreq
, req
, nodeid
, inarg
, buf
);
2094 static int send_notify_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
2095 int notify_code
, struct iovec
*iov
, int count
)
2097 struct fuse_out_header out
;
2103 out
.error
= notify_code
;
2104 iov
[0].iov_base
= &out
;
2105 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
2107 return fuse_send_msg(f
, ch
, iov
, count
);
2110 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
2113 struct fuse_notify_poll_wakeup_out outarg
;
2114 struct iovec iov
[2];
2118 iov
[1].iov_base
= &outarg
;
2119 iov
[1].iov_len
= sizeof(outarg
);
2121 return send_notify_iov(ph
->f
, ph
->ch
, FUSE_NOTIFY_POLL
, iov
, 2);
2127 int fuse_lowlevel_notify_inval_inode(struct fuse_chan
*ch
, fuse_ino_t ino
,
2128 off_t off
, off_t len
)
2130 struct fuse_notify_inval_inode_out outarg
;
2132 struct iovec iov
[2];
2137 f
= fuse_chan_session(ch
)->f
;
2145 iov
[1].iov_base
= &outarg
;
2146 iov
[1].iov_len
= sizeof(outarg
);
2148 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
2151 int fuse_lowlevel_notify_inval_entry(struct fuse_chan
*ch
, fuse_ino_t parent
,
2152 const char *name
, size_t namelen
)
2154 struct fuse_notify_inval_entry_out outarg
;
2156 struct iovec iov
[3];
2161 f
= fuse_chan_session(ch
)->f
;
2165 outarg
.parent
= parent
;
2166 outarg
.namelen
= namelen
;
2169 iov
[1].iov_base
= &outarg
;
2170 iov
[1].iov_len
= sizeof(outarg
);
2171 iov
[2].iov_base
= (void *)name
;
2172 iov
[2].iov_len
= namelen
+ 1;
2174 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
2177 int fuse_lowlevel_notify_delete(struct fuse_chan
*ch
,
2178 fuse_ino_t parent
, fuse_ino_t child
,
2179 const char *name
, size_t namelen
)
2181 struct fuse_notify_delete_out outarg
;
2183 struct iovec iov
[3];
2188 f
= fuse_chan_session(ch
)->f
;
2192 if (f
->conn
.proto_minor
< 18)
2195 outarg
.parent
= parent
;
2196 outarg
.child
= child
;
2197 outarg
.namelen
= namelen
;
2200 iov
[1].iov_base
= &outarg
;
2201 iov
[1].iov_len
= sizeof(outarg
);
2202 iov
[2].iov_base
= (void *)name
;
2203 iov
[2].iov_len
= namelen
+ 1;
2205 return send_notify_iov(f
, ch
, FUSE_NOTIFY_DELETE
, iov
, 3);
2208 int fuse_lowlevel_notify_store(struct fuse_chan
*ch
, fuse_ino_t ino
,
2209 off_t offset
, struct fuse_bufvec
*bufv
,
2210 enum fuse_buf_copy_flags flags
)
2212 struct fuse_out_header out
;
2213 struct fuse_notify_store_out outarg
;
2215 struct iovec iov
[3];
2216 size_t size
= fuse_buf_size(bufv
);
2222 f
= fuse_chan_session(ch
)->f
;
2226 if (f
->conn
.proto_minor
< 15)
2230 out
.error
= FUSE_NOTIFY_STORE
;
2232 outarg
.nodeid
= ino
;
2233 outarg
.offset
= offset
;
2236 iov
[0].iov_base
= &out
;
2237 iov
[0].iov_len
= sizeof(out
);
2238 iov
[1].iov_base
= &outarg
;
2239 iov
[1].iov_len
= sizeof(outarg
);
2241 res
= fuse_send_data_iov(f
, ch
, iov
, 2, bufv
, flags
);
2248 struct fuse_retrieve_req
{
2249 struct fuse_notify_req nreq
;
2253 static void fuse_ll_retrieve_reply(struct fuse_notify_req
*nreq
,
2254 fuse_req_t req
, fuse_ino_t ino
,
2256 const struct fuse_buf
*ibuf
)
2258 struct fuse_ll
*f
= req
->f
;
2259 struct fuse_retrieve_req
*rreq
=
2260 container_of(nreq
, struct fuse_retrieve_req
, nreq
);
2261 const struct fuse_notify_retrieve_in
*arg
= inarg
;
2262 struct fuse_bufvec bufv
= {
2267 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
2268 bufv
.buf
[0].mem
= PARAM(arg
);
2270 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
2271 sizeof(struct fuse_notify_retrieve_in
);
2273 if (bufv
.buf
[0].size
< arg
->size
) {
2274 fprintf(stderr
, "fuse: retrieve reply: buffer size too small\n");
2275 fuse_reply_none(req
);
2278 bufv
.buf
[0].size
= arg
->size
;
2280 if (req
->f
->op
.retrieve_reply
) {
2281 req
->f
->op
.retrieve_reply(req
, rreq
->cookie
, ino
,
2282 arg
->offset
, &bufv
);
2284 fuse_reply_none(req
);
2288 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
2289 fuse_ll_clear_pipe(f
);
2292 int fuse_lowlevel_notify_retrieve(struct fuse_chan
*ch
, fuse_ino_t ino
,
2293 size_t size
, off_t offset
, void *cookie
)
2295 struct fuse_notify_retrieve_out outarg
;
2297 struct iovec iov
[2];
2298 struct fuse_retrieve_req
*rreq
;
2304 f
= fuse_chan_session(ch
)->f
;
2308 if (f
->conn
.proto_minor
< 15)
2311 rreq
= malloc(sizeof(*rreq
));
2315 pthread_mutex_lock(&f
->lock
);
2316 rreq
->cookie
= cookie
;
2317 rreq
->nreq
.unique
= f
->notify_ctr
++;
2318 rreq
->nreq
.reply
= fuse_ll_retrieve_reply
;
2319 list_add_nreq(&rreq
->nreq
, &f
->notify_list
);
2320 pthread_mutex_unlock(&f
->lock
);
2322 outarg
.notify_unique
= rreq
->nreq
.unique
;
2323 outarg
.nodeid
= ino
;
2324 outarg
.offset
= offset
;
2327 iov
[1].iov_base
= &outarg
;
2328 iov
[1].iov_len
= sizeof(outarg
);
2330 err
= send_notify_iov(f
, ch
, FUSE_NOTIFY_RETRIEVE
, iov
, 2);
2332 pthread_mutex_lock(&f
->lock
);
2333 list_del_nreq(&rreq
->nreq
);
2334 pthread_mutex_unlock(&f
->lock
);
2341 void *fuse_req_userdata(fuse_req_t req
)
2343 return req
->f
->userdata
;
2346 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2351 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2354 pthread_mutex_lock(&req
->lock
);
2355 pthread_mutex_lock(&req
->f
->lock
);
2356 req
->u
.ni
.func
= func
;
2357 req
->u
.ni
.data
= data
;
2358 pthread_mutex_unlock(&req
->f
->lock
);
2359 if (req
->interrupted
&& func
)
2361 pthread_mutex_unlock(&req
->lock
);
2364 int fuse_req_interrupted(fuse_req_t req
)
2368 pthread_mutex_lock(&req
->f
->lock
);
2369 interrupted
= req
->interrupted
;
2370 pthread_mutex_unlock(&req
->f
->lock
);
2376 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
2379 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2380 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2381 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2382 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2383 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2384 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2385 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2386 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2387 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2388 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2389 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2390 [FUSE_LINK
] = { do_link
, "LINK" },
2391 [FUSE_OPEN
] = { do_open
, "OPEN" },
2392 [FUSE_READ
] = { do_read
, "READ" },
2393 [FUSE_WRITE
] = { do_write
, "WRITE" },
2394 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2395 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2396 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2397 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2398 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2399 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2400 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2401 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2402 [FUSE_INIT
] = { do_init
, "INIT" },
2403 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2404 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2405 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2406 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2407 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2408 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2409 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2410 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2411 [FUSE_CREATE
] = { do_create
, "CREATE" },
2412 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2413 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2414 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2415 [FUSE_POLL
] = { do_poll
, "POLL" },
2416 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
2417 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2418 [FUSE_NOTIFY_REPLY
] = { (void *) 1, "NOTIFY_REPLY" },
2419 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2420 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS"},
2421 [CUSE_INIT
] = { cuse_lowlevel_init
, "CUSE_INIT" },
2424 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2426 static const char *opname(enum fuse_opcode opcode
)
2428 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
2431 return fuse_ll_ops
[opcode
].name
;
2434 static int fuse_ll_copy_from_pipe(struct fuse_bufvec
*dst
,
2435 struct fuse_bufvec
*src
)
2437 int res
= fuse_buf_copy(dst
, src
, 0);
2439 fprintf(stderr
, "fuse: copy from pipe: %s\n", strerror(-res
));
2442 if (res
< fuse_buf_size(dst
)) {
2443 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2449 void fuse_session_process_buf(struct fuse_session
*se
,
2450 const struct fuse_buf
*buf
, struct fuse_chan
*ch
)
2452 struct fuse_ll
*f
= se
->f
;
2453 const size_t write_header_size
= sizeof(struct fuse_in_header
) +
2454 sizeof(struct fuse_write_in
);
2455 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2456 struct fuse_bufvec tmpbuf
= FUSE_BUFVEC_INIT(write_header_size
);
2457 struct fuse_in_header
*in
;
2459 struct fuse_req
*req
;
2464 if (buf
->flags
& FUSE_BUF_IS_FD
) {
2465 if (buf
->size
< tmpbuf
.buf
[0].size
)
2466 tmpbuf
.buf
[0].size
= buf
->size
;
2468 mbuf
= malloc(tmpbuf
.buf
[0].size
);
2470 fprintf(stderr
, "fuse: failed to allocate header\n");
2473 tmpbuf
.buf
[0].mem
= mbuf
;
2475 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2486 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2487 (unsigned long long) in
->unique
,
2488 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
2489 (unsigned long long) in
->nodeid
, buf
->size
, in
->pid
);
2492 req
= fuse_ll_alloc_req(f
);
2494 struct fuse_out_header out
= {
2495 .unique
= in
->unique
,
2498 struct iovec iov
= {
2500 .iov_len
= sizeof(struct fuse_out_header
),
2503 fuse_send_msg(f
, ch
, &iov
, 1);
2507 req
->unique
= in
->unique
;
2508 req
->ctx
.uid
= in
->uid
;
2509 req
->ctx
.gid
= in
->gid
;
2510 req
->ctx
.pid
= in
->pid
;
2515 enum fuse_opcode expected
;
2517 expected
= f
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2518 if (in
->opcode
!= expected
)
2520 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
)
2524 if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
2525 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2526 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2527 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2528 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2529 in
->opcode
!= FUSE_NOTIFY_REPLY
&&
2530 in
->opcode
!= FUSE_READDIRPLUS
)
2534 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
2536 if (in
->opcode
!= FUSE_INTERRUPT
) {
2537 struct fuse_req
*intr
;
2538 pthread_mutex_lock(&f
->lock
);
2539 intr
= check_interrupt(f
, req
);
2540 list_add_req(req
, &f
->list
);
2541 pthread_mutex_unlock(&f
->lock
);
2543 fuse_reply_err(intr
, EAGAIN
);
2546 if ((buf
->flags
& FUSE_BUF_IS_FD
) && write_header_size
< buf
->size
&&
2547 (in
->opcode
!= FUSE_WRITE
|| !f
->op
.write_buf
) &&
2548 in
->opcode
!= FUSE_NOTIFY_REPLY
) {
2552 newmbuf
= realloc(mbuf
, buf
->size
);
2553 if (newmbuf
== NULL
)
2557 tmpbuf
= FUSE_BUFVEC_INIT(buf
->size
- write_header_size
);
2558 tmpbuf
.buf
[0].mem
= mbuf
+ write_header_size
;
2560 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2568 inarg
= (void *) &in
[1];
2569 if (in
->opcode
== FUSE_WRITE
&& f
->op
.write_buf
)
2570 do_write_buf(req
, in
->nodeid
, inarg
, buf
);
2571 else if (in
->opcode
== FUSE_NOTIFY_REPLY
)
2572 do_notify_reply(req
, in
->nodeid
, inarg
, buf
);
2574 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
2581 fuse_reply_err(req
, err
);
2583 if (buf
->flags
& FUSE_BUF_IS_FD
)
2584 fuse_ll_clear_pipe(f
);
2593 static const struct fuse_opt fuse_ll_opts
[] = {
2594 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
2595 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
2596 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
2597 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
2598 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
2599 { "max_background=%u", offsetof(struct fuse_ll
, conn
.max_background
), 0 },
2600 { "congestion_threshold=%u",
2601 offsetof(struct fuse_ll
, conn
.congestion_threshold
), 0 },
2602 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
2603 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
2604 { "atomic_o_trunc", offsetof(struct fuse_ll
, atomic_o_trunc
), 1},
2605 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2606 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2607 { "no_remote_flock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2608 { "no_remote_posix_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2609 { "big_writes", offsetof(struct fuse_ll
, big_writes
), 1},
2610 { "splice_write", offsetof(struct fuse_ll
, splice_write
), 1},
2611 { "no_splice_write", offsetof(struct fuse_ll
, no_splice_write
), 1},
2612 { "splice_move", offsetof(struct fuse_ll
, splice_move
), 1},
2613 { "no_splice_move", offsetof(struct fuse_ll
, no_splice_move
), 1},
2614 { "splice_read", offsetof(struct fuse_ll
, splice_read
), 1},
2615 { "no_splice_read", offsetof(struct fuse_ll
, no_splice_read
), 1},
2616 { "auto_inval_data", offsetof(struct fuse_ll
, auto_inval_data
), 1},
2617 { "no_auto_inval_data", offsetof(struct fuse_ll
, no_auto_inval_data
), 1},
2618 { "readdirplus=no", offsetof(struct fuse_ll
, no_readdirplus
), 1},
2619 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2620 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus_auto
), 1},
2621 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2622 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus_auto
), 0},
2623 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
2624 FUSE_OPT_KEY("-h", KEY_HELP
),
2625 FUSE_OPT_KEY("--help", KEY_HELP
),
2626 FUSE_OPT_KEY("-V", KEY_VERSION
),
2627 FUSE_OPT_KEY("--version", KEY_VERSION
),
2631 static void fuse_ll_version(void)
2633 fprintf(stderr
, "using FUSE kernel interface version %i.%i\n",
2634 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
2637 static void fuse_ll_help(void)
2640 " -o max_write=N set maximum size of write requests\n"
2641 " -o max_readahead=N set maximum readahead\n"
2642 " -o max_background=N set number of maximum background requests\n"
2643 " -o congestion_threshold=N set kernel's congestion threshold\n"
2644 " -o async_read perform reads asynchronously (default)\n"
2645 " -o sync_read perform reads synchronously\n"
2646 " -o atomic_o_trunc enable atomic open+truncate support\n"
2647 " -o big_writes enable larger than 4kB writes\n"
2648 " -o no_remote_lock disable remote file locking\n"
2649 " -o no_remote_flock disable remote file locking (BSD)\n"
2650 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2651 " -o [no_]splice_write use splice to write to the fuse device\n"
2652 " -o [no_]splice_move move data while splicing to the fuse device\n"
2653 " -o [no_]splice_read use splice to read from the fuse device\n"
2654 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2655 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2659 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
2660 struct fuse_args
*outargs
)
2662 (void) data
; (void) outargs
;
2674 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
2680 static void fuse_ll_destroy(struct fuse_ll
*f
)
2682 struct fuse_ll_pipe
*llp
;
2684 if (f
->got_init
&& !f
->got_destroy
) {
2686 f
->op
.destroy(f
->userdata
);
2688 llp
= pthread_getspecific(f
->pipe_key
);
2690 fuse_ll_pipe_free(llp
);
2691 pthread_key_delete(f
->pipe_key
);
2692 pthread_mutex_destroy(&f
->lock
);
2697 void fuse_session_destroy(struct fuse_session
*se
)
2699 fuse_ll_destroy(se
->f
);
2701 fuse_chan_destroy(se
->ch
);
2706 static void fuse_ll_pipe_destructor(void *data
)
2708 struct fuse_ll_pipe
*llp
= data
;
2709 fuse_ll_pipe_free(llp
);
2713 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2714 struct fuse_chan
*ch
)
2716 struct fuse_ll
*f
= se
->f
;
2717 size_t bufsize
= buf
->size
= f
->bufsize
;
2718 struct fuse_ll_pipe
*llp
;
2719 struct fuse_buf tmpbuf
;
2723 if (f
->conn
.proto_minor
< 14 || !(f
->conn
.want
& FUSE_CAP_SPLICE_READ
))
2726 llp
= fuse_ll_get_pipe(f
);
2730 if (llp
->size
< bufsize
) {
2731 if (llp
->can_grow
) {
2732 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, bufsize
);
2739 if (llp
->size
< bufsize
)
2743 res
= splice(fuse_chan_fd(ch
), NULL
, llp
->pipe
[1], NULL
, bufsize
, 0);
2746 if (fuse_session_exited(se
))
2750 if (err
== ENODEV
) {
2751 fuse_session_exit(se
);
2754 if (err
!= EINTR
&& err
!= EAGAIN
)
2755 perror("fuse: splice from device");
2759 if (res
< sizeof(struct fuse_in_header
)) {
2760 fprintf(stderr
, "short splice from fuse device\n");
2764 tmpbuf
= (struct fuse_buf
) {
2766 .flags
= FUSE_BUF_IS_FD
,
2771 * Don't bother with zero copy for small requests.
2772 * fuse_loop_mt() needs to check for FORGET so this more than
2773 * just an optimization.
2775 if (res
< sizeof(struct fuse_in_header
) +
2776 sizeof(struct fuse_write_in
) + pagesize
) {
2777 struct fuse_bufvec src
= { .buf
[0] = tmpbuf
, .count
= 1 };
2778 struct fuse_bufvec dst
= { .buf
[0] = *buf
, .count
= 1 };
2780 res
= fuse_buf_copy(&dst
, &src
, 0);
2782 fprintf(stderr
, "fuse: copy from pipe: %s\n",
2784 fuse_ll_clear_pipe(f
);
2787 if (res
< tmpbuf
.size
) {
2788 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2789 fuse_ll_clear_pipe(f
);
2792 buf
->size
= tmpbuf
.size
;
2801 return fuse_chan_recv(se
, buf
, ch
);
2804 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2805 struct fuse_chan
*ch
)
2807 return fuse_chan_recv(se
, buf
, ch
);
2811 #define MIN_BUFSIZE 0x21000
2813 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
2814 const struct fuse_lowlevel_ops
*op
,
2815 size_t op_size
, void *userdata
)
2819 struct fuse_session
*se
;
2821 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2822 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
2823 op_size
= sizeof(struct fuse_lowlevel_ops
);
2826 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
2828 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
2832 f
->conn
.async_read
= 1;
2833 f
->conn
.max_write
= UINT_MAX
;
2834 f
->conn
.max_readahead
= UINT_MAX
;
2835 f
->atomic_o_trunc
= 0;
2836 f
->bufsize
= getpagesize() + 0x1000;
2837 f
->bufsize
= f
->bufsize
< MIN_BUFSIZE
? MIN_BUFSIZE
: f
->bufsize
;
2839 list_init_req(&f
->list
);
2840 list_init_req(&f
->interrupts
);
2841 list_init_nreq(&f
->notify_list
);
2843 fuse_mutex_init(&f
->lock
);
2845 err
= pthread_key_create(&f
->pipe_key
, fuse_ll_pipe_destructor
);
2847 fprintf(stderr
, "fuse: failed to create thread specific key: %s\n",
2852 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
2853 goto out_key_destroy
;
2856 fprintf(stderr
, "FUSE library version: %s\n", PACKAGE_VERSION
);
2858 memcpy(&f
->op
, op
, op_size
);
2859 f
->owner
= getuid();
2860 f
->userdata
= userdata
;
2862 se
= fuse_session_new();
2864 goto out_key_destroy
;
2871 pthread_key_delete(f
->pipe_key
);
2873 pthread_mutex_destroy(&f
->lock
);
2880 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2883 size_t bufsize
= 1024;
2887 unsigned long pid
= req
->ctx
.pid
;
2890 sprintf(path
, "/proc/%lu/task/%lu/status", pid
, pid
);
2893 buf
= malloc(bufsize
);
2898 fd
= open(path
, O_RDONLY
);
2902 ret
= read(fd
, buf
, bufsize
);
2909 if (ret
== bufsize
) {
2916 s
= strstr(buf
, "\nGroups:");
2924 unsigned long val
= strtoul(s
, &end
, 0);
2940 * This is currently not implemented on other than Linux...
2942 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])