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"
27 #ifndef F_LINUX_SPECIFIC_BASE
28 #define F_LINUX_SPECIFIC_BASE 1024
31 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
35 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
36 #define OFFSET_MAX 0x7fffffffffffffffLL
38 #define container_of(ptr, type, member) ({ \
39 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
40 (type *)( (char *)__mptr - offsetof(type,member) );})
42 struct fuse_pollhandle
{
48 static size_t pagesize
;
50 static __attribute__((constructor
)) void fuse_ll_init_pagesize(void)
52 pagesize
= getpagesize();
55 static void convert_stat(const struct stat
*stbuf
, struct fuse_attr
*attr
)
57 attr
->ino
= stbuf
->st_ino
;
58 attr
->mode
= stbuf
->st_mode
;
59 attr
->nlink
= stbuf
->st_nlink
;
60 attr
->uid
= stbuf
->st_uid
;
61 attr
->gid
= stbuf
->st_gid
;
62 attr
->rdev
= stbuf
->st_rdev
;
63 attr
->size
= stbuf
->st_size
;
64 attr
->blksize
= stbuf
->st_blksize
;
65 attr
->blocks
= stbuf
->st_blocks
;
66 attr
->atime
= stbuf
->st_atime
;
67 attr
->mtime
= stbuf
->st_mtime
;
68 attr
->ctime
= stbuf
->st_ctime
;
69 attr
->atimensec
= ST_ATIM_NSEC(stbuf
);
70 attr
->mtimensec
= ST_MTIM_NSEC(stbuf
);
71 attr
->ctimensec
= ST_CTIM_NSEC(stbuf
);
74 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
76 stbuf
->st_mode
= attr
->mode
;
77 stbuf
->st_uid
= attr
->uid
;
78 stbuf
->st_gid
= attr
->gid
;
79 stbuf
->st_size
= attr
->size
;
80 stbuf
->st_atime
= attr
->atime
;
81 stbuf
->st_mtime
= attr
->mtime
;
82 stbuf
->st_ctime
= attr
->ctime
;
83 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
84 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
85 ST_CTIM_NSEC_SET(stbuf
, attr
->ctimensec
);
88 static size_t iov_length(const struct iovec
*iov
, size_t count
)
93 for (seg
= 0; seg
< count
; seg
++)
94 ret
+= iov
[seg
].iov_len
;
98 static void list_init_req(struct fuse_req
*req
)
104 static void list_del_req(struct fuse_req
*req
)
106 struct fuse_req
*prev
= req
->prev
;
107 struct fuse_req
*next
= req
->next
;
112 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
114 struct fuse_req
*prev
= next
->prev
;
121 static void destroy_req(fuse_req_t req
)
123 pthread_mutex_destroy(&req
->lock
);
127 void fuse_free_req(fuse_req_t req
)
130 struct fuse_ll
*f
= req
->f
;
132 pthread_mutex_lock(&f
->lock
);
133 req
->u
.ni
.func
= NULL
;
134 req
->u
.ni
.data
= NULL
;
137 pthread_mutex_unlock(&f
->lock
);
142 static struct fuse_req
*fuse_ll_alloc_req(struct fuse_ll
*f
)
144 struct fuse_req
*req
;
146 req
= (struct fuse_req
*) calloc(1, sizeof(struct fuse_req
));
148 fprintf(stderr
, "fuse: failed to allocate request\n");
153 fuse_mutex_init(&req
->lock
);
159 static int fuse_chan_recv(struct fuse_session
*se
, struct fuse_buf
*buf
,
160 struct fuse_chan
*ch
)
162 struct fuse_ll
*f
= se
->f
;
167 buf
->mem
= malloc(f
->bufsize
);
170 "fuse: failed to allocate read buffer\n");
176 res
= read(fuse_chan_fd(ch
), buf
->mem
, f
->bufsize
);
179 if (fuse_session_exited(se
))
182 /* ENOENT means the operation was interrupted, it's safe
188 fuse_session_exit(se
);
191 /* Errors occurring during normal operation: EINTR (read
192 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
194 if (err
!= EINTR
&& err
!= EAGAIN
)
195 perror("fuse: reading device");
198 if ((size_t) res
< sizeof(struct fuse_in_header
)) {
199 fprintf(stderr
, "short read on fuse device\n");
208 static int fuse_chan_send(struct fuse_chan
*ch
, const struct iovec iov
[],
211 ssize_t res
= writev(fuse_chan_fd(ch
), iov
, count
);
215 struct fuse_session
*se
= fuse_chan_session(ch
);
219 /* ENOENT means the operation was interrupted */
220 if (!fuse_session_exited(se
) && err
!= ENOENT
)
221 perror("fuse: writing device");
228 void fuse_chan_close(struct fuse_chan
*ch
)
230 int fd
= fuse_chan_fd(ch
);
236 static int fuse_send_msg(struct fuse_ll
*f
, struct fuse_chan
*ch
,
237 struct iovec
*iov
, int count
)
239 struct fuse_out_header
*out
= iov
[0].iov_base
;
241 out
->len
= iov_length(iov
, count
);
243 if (out
->unique
== 0) {
244 fprintf(stderr
, "NOTIFY: code=%d length=%u\n",
245 out
->error
, out
->len
);
246 } else if (out
->error
) {
248 " unique: %llu, error: %i (%s), outsize: %i\n",
249 (unsigned long long) out
->unique
, out
->error
,
250 strerror(-out
->error
), out
->len
);
253 " unique: %llu, success, outsize: %i\n",
254 (unsigned long long) out
->unique
, out
->len
);
258 return fuse_chan_send(ch
, iov
, count
);
261 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
264 struct fuse_out_header out
;
266 if (error
<= -1000 || error
> 0) {
267 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
271 out
.unique
= req
->unique
;
274 iov
[0].iov_base
= &out
;
275 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
277 return fuse_send_msg(req
->f
, req
->ch
, iov
, count
);
280 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
285 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
290 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
296 iov
[1].iov_base
= (void *) arg
;
297 iov
[1].iov_len
= argsize
;
300 return send_reply_iov(req
, error
, iov
, count
);
303 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
306 struct iovec
*padded_iov
;
308 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
309 if (padded_iov
== NULL
)
310 return fuse_reply_err(req
, ENOMEM
);
312 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
315 res
= send_reply_iov(req
, 0, padded_iov
, count
);
321 static size_t fuse_dirent_size(size_t namelen
)
323 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
326 static void fuse_add_dirent(struct fuse_dirent
*dirent
, const char *name
,
327 const struct stat
*stbuf
, off_t off
)
329 unsigned namelen
= strlen(name
);
330 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
331 unsigned entsize
= fuse_dirent_size(namelen
);
332 unsigned padlen
= entsize
- entlen
;
334 dirent
->ino
= stbuf
->st_ino
;
336 dirent
->namelen
= namelen
;
337 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
338 strncpy(dirent
->name
, name
, namelen
);
340 memset(dirent
->name
+ namelen
, 0, padlen
);
343 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
344 const char *name
, const struct stat
*stbuf
, off_t off
)
349 entsize
= fuse_dirent_size(strlen(name
));
350 if (entsize
<= bufsize
&& buf
)
351 fuse_add_dirent((struct fuse_dirent
*) buf
, name
, stbuf
, off
);
355 static void convert_statfs(const struct statvfs
*stbuf
,
356 struct fuse_kstatfs
*kstatfs
)
358 kstatfs
->bsize
= stbuf
->f_bsize
;
359 kstatfs
->frsize
= stbuf
->f_frsize
;
360 kstatfs
->blocks
= stbuf
->f_blocks
;
361 kstatfs
->bfree
= stbuf
->f_bfree
;
362 kstatfs
->bavail
= stbuf
->f_bavail
;
363 kstatfs
->files
= stbuf
->f_files
;
364 kstatfs
->ffree
= stbuf
->f_ffree
;
365 kstatfs
->namelen
= stbuf
->f_namemax
;
368 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
370 return send_reply(req
, 0, arg
, argsize
);
373 int fuse_reply_err(fuse_req_t req
, int err
)
375 return send_reply(req
, -err
, NULL
, 0);
378 void fuse_reply_none(fuse_req_t req
)
383 static unsigned long calc_timeout_sec(double t
)
385 if (t
> (double) ULONG_MAX
)
390 return (unsigned long) t
;
393 static unsigned int calc_timeout_nsec(double t
)
395 double f
= t
- (double) calc_timeout_sec(t
);
398 else if (f
>= 0.999999999)
401 return (unsigned int) (f
* 1.0e9
);
404 static void fill_entry(struct fuse_entry_out
*arg
,
405 const struct fuse_entry_param
*e
)
407 arg
->nodeid
= e
->ino
;
408 arg
->generation
= e
->generation
;
409 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
410 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
411 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
412 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
413 convert_stat(&e
->attr
, &arg
->attr
);
416 size_t fuse_add_direntry_plus(fuse_req_t req
, char *buf
, size_t bufsize
,
418 const struct fuse_entry_param
*e
, off_t off
)
423 entsize
= FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS
+ strlen(name
));
424 if (entsize
<= bufsize
&& buf
) {
425 struct fuse_direntplus
*dp
= (struct fuse_direntplus
*) buf
;
426 memset(&dp
->entry_out
, 0, sizeof(dp
->entry_out
));
427 fill_entry(&dp
->entry_out
, e
);
428 fuse_add_dirent(&dp
->dirent
, 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
));
1148 if (req
->f
->op
.getattr
)
1149 req
->f
->op
.getattr(req
, nodeid
, fip
);
1151 fuse_reply_err(req
, ENOSYS
);
1154 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1156 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*) inarg
;
1158 if (req
->f
->op
.setattr
) {
1159 struct fuse_file_info
*fi
= NULL
;
1160 struct fuse_file_info fi_store
;
1162 memset(&stbuf
, 0, sizeof(stbuf
));
1163 convert_attr(arg
, &stbuf
);
1164 if (arg
->valid
& FATTR_FH
) {
1165 arg
->valid
&= ~FATTR_FH
;
1166 memset(&fi_store
, 0, sizeof(fi_store
));
1171 FUSE_SET_ATTR_MODE
|
1174 FUSE_SET_ATTR_SIZE
|
1175 FUSE_SET_ATTR_ATIME
|
1176 FUSE_SET_ATTR_MTIME
|
1177 FUSE_SET_ATTR_ATIME_NOW
|
1178 FUSE_SET_ATTR_MTIME_NOW
|
1179 FUSE_SET_ATTR_CTIME
;
1181 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
1183 fuse_reply_err(req
, ENOSYS
);
1186 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1188 struct fuse_access_in
*arg
= (struct fuse_access_in
*) inarg
;
1190 if (req
->f
->op
.access
)
1191 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
1193 fuse_reply_err(req
, ENOSYS
);
1196 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1200 if (req
->f
->op
.readlink
)
1201 req
->f
->op
.readlink(req
, nodeid
);
1203 fuse_reply_err(req
, ENOSYS
);
1206 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1208 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*) inarg
;
1209 char *name
= PARAM(arg
);
1211 if (req
->f
->conn
.proto_minor
>= 12)
1212 req
->ctx
.umask
= arg
->umask
;
1214 name
= (char *) inarg
+ FUSE_COMPAT_MKNOD_IN_SIZE
;
1216 if (req
->f
->op
.mknod
)
1217 req
->f
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
1219 fuse_reply_err(req
, ENOSYS
);
1222 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1224 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*) inarg
;
1226 if (req
->f
->conn
.proto_minor
>= 12)
1227 req
->ctx
.umask
= arg
->umask
;
1229 if (req
->f
->op
.mkdir
)
1230 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
1232 fuse_reply_err(req
, ENOSYS
);
1235 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1237 char *name
= (char *) inarg
;
1239 if (req
->f
->op
.unlink
)
1240 req
->f
->op
.unlink(req
, nodeid
, name
);
1242 fuse_reply_err(req
, ENOSYS
);
1245 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1247 char *name
= (char *) inarg
;
1249 if (req
->f
->op
.rmdir
)
1250 req
->f
->op
.rmdir(req
, nodeid
, name
);
1252 fuse_reply_err(req
, ENOSYS
);
1255 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1257 char *name
= (char *) inarg
;
1258 char *linkname
= ((char *) inarg
) + strlen((char *) inarg
) + 1;
1260 if (req
->f
->op
.symlink
)
1261 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
1263 fuse_reply_err(req
, ENOSYS
);
1266 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1268 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*) inarg
;
1269 char *oldname
= PARAM(arg
);
1270 char *newname
= oldname
+ strlen(oldname
) + 1;
1272 if (req
->f
->op
.rename
)
1273 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
1275 fuse_reply_err(req
, ENOSYS
);
1278 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1280 struct fuse_link_in
*arg
= (struct fuse_link_in
*) inarg
;
1282 if (req
->f
->op
.link
)
1283 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
1285 fuse_reply_err(req
, ENOSYS
);
1288 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1290 struct fuse_create_in
*arg
= (struct fuse_create_in
*) inarg
;
1292 if (req
->f
->op
.create
) {
1293 struct fuse_file_info fi
;
1294 char *name
= PARAM(arg
);
1296 memset(&fi
, 0, sizeof(fi
));
1297 fi
.flags
= arg
->flags
;
1299 if (req
->f
->conn
.proto_minor
>= 12)
1300 req
->ctx
.umask
= arg
->umask
;
1302 name
= (char *) inarg
+ sizeof(struct fuse_open_in
);
1304 req
->f
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1306 fuse_reply_err(req
, ENOSYS
);
1309 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1311 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1312 struct fuse_file_info fi
;
1314 memset(&fi
, 0, sizeof(fi
));
1315 fi
.flags
= arg
->flags
;
1317 if (req
->f
->op
.open
)
1318 req
->f
->op
.open(req
, nodeid
, &fi
);
1320 fuse_reply_open(req
, &fi
);
1323 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1325 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1327 if (req
->f
->op
.read
) {
1328 struct fuse_file_info fi
;
1330 memset(&fi
, 0, sizeof(fi
));
1332 if (req
->f
->conn
.proto_minor
>= 9) {
1333 fi
.lock_owner
= arg
->lock_owner
;
1334 fi
.flags
= arg
->flags
;
1336 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1338 fuse_reply_err(req
, ENOSYS
);
1341 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1343 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1344 struct fuse_file_info fi
;
1347 memset(&fi
, 0, sizeof(fi
));
1349 fi
.writepage
= (arg
->write_flags
& 1) != 0;
1351 if (req
->f
->conn
.proto_minor
< 9) {
1352 param
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1354 fi
.lock_owner
= arg
->lock_owner
;
1355 fi
.flags
= arg
->flags
;
1359 if (req
->f
->op
.write
)
1360 req
->f
->op
.write(req
, nodeid
, param
, arg
->size
,
1363 fuse_reply_err(req
, ENOSYS
);
1366 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
,
1367 const struct fuse_buf
*ibuf
)
1369 struct fuse_ll
*f
= req
->f
;
1370 struct fuse_bufvec bufv
= {
1374 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1375 struct fuse_file_info fi
;
1377 memset(&fi
, 0, sizeof(fi
));
1379 fi
.writepage
= arg
->write_flags
& 1;
1381 if (req
->f
->conn
.proto_minor
< 9) {
1382 bufv
.buf
[0].mem
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1383 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1384 FUSE_COMPAT_WRITE_IN_SIZE
;
1385 assert(!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1387 fi
.lock_owner
= arg
->lock_owner
;
1388 fi
.flags
= arg
->flags
;
1389 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
1390 bufv
.buf
[0].mem
= PARAM(arg
);
1392 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1393 sizeof(struct fuse_write_in
);
1395 if (bufv
.buf
[0].size
< arg
->size
) {
1396 fprintf(stderr
, "fuse: do_write_buf: buffer size too small\n");
1397 fuse_reply_err(req
, EIO
);
1400 bufv
.buf
[0].size
= arg
->size
;
1402 req
->f
->op
.write_buf(req
, nodeid
, &bufv
, arg
->offset
, &fi
);
1405 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1406 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
1407 fuse_ll_clear_pipe(f
);
1410 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1412 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*) inarg
;
1413 struct fuse_file_info fi
;
1415 memset(&fi
, 0, sizeof(fi
));
1418 if (req
->f
->conn
.proto_minor
>= 7)
1419 fi
.lock_owner
= arg
->lock_owner
;
1421 if (req
->f
->op
.flush
)
1422 req
->f
->op
.flush(req
, nodeid
, &fi
);
1424 fuse_reply_err(req
, ENOSYS
);
1427 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1429 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1430 struct fuse_file_info fi
;
1432 memset(&fi
, 0, sizeof(fi
));
1433 fi
.flags
= arg
->flags
;
1435 if (req
->f
->conn
.proto_minor
>= 8) {
1436 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1437 fi
.lock_owner
= arg
->lock_owner
;
1439 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1440 fi
.flock_release
= 1;
1441 fi
.lock_owner
= arg
->lock_owner
;
1444 if (req
->f
->op
.release
)
1445 req
->f
->op
.release(req
, nodeid
, &fi
);
1447 fuse_reply_err(req
, 0);
1450 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1452 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1453 struct fuse_file_info fi
;
1455 memset(&fi
, 0, sizeof(fi
));
1458 if (req
->f
->op
.fsync
)
1459 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1461 fuse_reply_err(req
, ENOSYS
);
1464 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1466 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1467 struct fuse_file_info fi
;
1469 memset(&fi
, 0, sizeof(fi
));
1470 fi
.flags
= arg
->flags
;
1472 if (req
->f
->op
.opendir
)
1473 req
->f
->op
.opendir(req
, nodeid
, &fi
);
1475 fuse_reply_open(req
, &fi
);
1478 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1480 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1481 struct fuse_file_info fi
;
1483 memset(&fi
, 0, sizeof(fi
));
1486 if (req
->f
->op
.readdir
)
1487 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1489 fuse_reply_err(req
, ENOSYS
);
1492 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1494 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1495 struct fuse_file_info fi
;
1497 memset(&fi
, 0, sizeof(fi
));
1500 if (req
->f
->op
.readdirplus
)
1501 req
->f
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1503 fuse_reply_err(req
, ENOSYS
);
1506 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1508 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1509 struct fuse_file_info fi
;
1511 memset(&fi
, 0, sizeof(fi
));
1512 fi
.flags
= arg
->flags
;
1515 if (req
->f
->op
.releasedir
)
1516 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
1518 fuse_reply_err(req
, 0);
1521 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1523 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1524 struct fuse_file_info fi
;
1526 memset(&fi
, 0, sizeof(fi
));
1529 if (req
->f
->op
.fsyncdir
)
1530 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1532 fuse_reply_err(req
, ENOSYS
);
1535 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1540 if (req
->f
->op
.statfs
)
1541 req
->f
->op
.statfs(req
, nodeid
);
1543 struct statvfs buf
= {
1547 fuse_reply_statfs(req
, &buf
);
1551 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1553 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*) inarg
;
1554 char *name
= PARAM(arg
);
1555 char *value
= name
+ strlen(name
) + 1;
1557 if (req
->f
->op
.setxattr
)
1558 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
,
1561 fuse_reply_err(req
, ENOSYS
);
1564 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1566 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1568 if (req
->f
->op
.getxattr
)
1569 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
1571 fuse_reply_err(req
, ENOSYS
);
1574 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1576 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1578 if (req
->f
->op
.listxattr
)
1579 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
1581 fuse_reply_err(req
, ENOSYS
);
1584 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1586 char *name
= (char *) inarg
;
1588 if (req
->f
->op
.removexattr
)
1589 req
->f
->op
.removexattr(req
, nodeid
, name
);
1591 fuse_reply_err(req
, ENOSYS
);
1594 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1595 struct flock
*flock
)
1597 memset(flock
, 0, sizeof(struct flock
));
1598 flock
->l_type
= fl
->type
;
1599 flock
->l_whence
= SEEK_SET
;
1600 flock
->l_start
= fl
->start
;
1601 if (fl
->end
== OFFSET_MAX
)
1604 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1605 flock
->l_pid
= fl
->pid
;
1608 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1610 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1611 struct fuse_file_info fi
;
1614 memset(&fi
, 0, sizeof(fi
));
1616 fi
.lock_owner
= arg
->owner
;
1618 convert_fuse_file_lock(&arg
->lk
, &flock
);
1619 if (req
->f
->op
.getlk
)
1620 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1622 fuse_reply_err(req
, ENOSYS
);
1625 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1626 const void *inarg
, int sleep
)
1628 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1629 struct fuse_file_info fi
;
1632 memset(&fi
, 0, sizeof(fi
));
1634 fi
.lock_owner
= arg
->owner
;
1636 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1639 switch (arg
->lk
.type
) {
1653 if (req
->f
->op
.flock
)
1654 req
->f
->op
.flock(req
, nodeid
, &fi
, op
);
1656 fuse_reply_err(req
, ENOSYS
);
1658 convert_fuse_file_lock(&arg
->lk
, &flock
);
1659 if (req
->f
->op
.setlk
)
1660 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1662 fuse_reply_err(req
, ENOSYS
);
1666 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1668 do_setlk_common(req
, nodeid
, inarg
, 0);
1671 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1673 do_setlk_common(req
, nodeid
, inarg
, 1);
1676 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
1678 struct fuse_req
*curr
;
1680 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
1681 if (curr
->unique
== req
->u
.i
.unique
) {
1682 fuse_interrupt_func_t func
;
1686 pthread_mutex_unlock(&f
->lock
);
1688 /* Ugh, ugly locking */
1689 pthread_mutex_lock(&curr
->lock
);
1690 pthread_mutex_lock(&f
->lock
);
1691 curr
->interrupted
= 1;
1692 func
= curr
->u
.ni
.func
;
1693 data
= curr
->u
.ni
.data
;
1694 pthread_mutex_unlock(&f
->lock
);
1697 pthread_mutex_unlock(&curr
->lock
);
1699 pthread_mutex_lock(&f
->lock
);
1707 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1708 curr
= curr
->next
) {
1709 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
1715 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1717 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*) inarg
;
1718 struct fuse_ll
*f
= req
->f
;
1722 fprintf(stderr
, "INTERRUPT: %llu\n",
1723 (unsigned long long) arg
->unique
);
1725 req
->u
.i
.unique
= arg
->unique
;
1727 pthread_mutex_lock(&f
->lock
);
1728 if (find_interrupted(f
, req
))
1731 list_add_req(req
, &f
->interrupts
);
1732 pthread_mutex_unlock(&f
->lock
);
1735 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
1737 struct fuse_req
*curr
;
1739 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1740 curr
= curr
->next
) {
1741 if (curr
->u
.i
.unique
== req
->unique
) {
1742 req
->interrupted
= 1;
1748 curr
= f
->interrupts
.next
;
1749 if (curr
!= &f
->interrupts
) {
1751 list_init_req(curr
);
1757 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1759 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*) inarg
;
1761 if (req
->f
->op
.bmap
)
1762 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1764 fuse_reply_err(req
, ENOSYS
);
1767 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1769 struct fuse_ioctl_in
*arg
= (struct fuse_ioctl_in
*) inarg
;
1770 unsigned int flags
= arg
->flags
;
1771 void *in_buf
= arg
->in_size
? PARAM(arg
) : NULL
;
1772 struct fuse_file_info fi
;
1774 if (flags
& FUSE_IOCTL_DIR
&&
1775 !(req
->f
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1776 fuse_reply_err(req
, ENOTTY
);
1780 memset(&fi
, 0, sizeof(fi
));
1783 if (sizeof(void *) == 4 && req
->f
->conn
.proto_minor
>= 16 &&
1784 !(flags
& FUSE_IOCTL_32BIT
)) {
1785 req
->ioctl_64bit
= 1;
1788 if (req
->f
->op
.ioctl
)
1789 req
->f
->op
.ioctl(req
, nodeid
, arg
->cmd
,
1790 (void *)(uintptr_t)arg
->arg
, &fi
, flags
,
1791 in_buf
, arg
->in_size
, arg
->out_size
);
1793 fuse_reply_err(req
, ENOSYS
);
1796 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1801 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1803 struct fuse_poll_in
*arg
= (struct fuse_poll_in
*) inarg
;
1804 struct fuse_file_info fi
;
1806 memset(&fi
, 0, sizeof(fi
));
1808 fi
.poll_events
= arg
->events
;
1810 if (req
->f
->op
.poll
) {
1811 struct fuse_pollhandle
*ph
= NULL
;
1813 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1814 ph
= malloc(sizeof(struct fuse_pollhandle
));
1816 fuse_reply_err(req
, ENOMEM
);
1824 req
->f
->op
.poll(req
, nodeid
, &fi
, ph
);
1826 fuse_reply_err(req
, ENOSYS
);
1830 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1832 struct fuse_fallocate_in
*arg
= (struct fuse_fallocate_in
*) inarg
;
1833 struct fuse_file_info fi
;
1835 memset(&fi
, 0, sizeof(fi
));
1838 if (req
->f
->op
.fallocate
)
1839 req
->f
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
, &fi
);
1841 fuse_reply_err(req
, ENOSYS
);
1844 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1846 struct fuse_init_in
*arg
= (struct fuse_init_in
*) inarg
;
1847 struct fuse_init_out outarg
;
1848 struct fuse_ll
*f
= req
->f
;
1849 size_t bufsize
= f
->bufsize
;
1850 size_t outargsize
= sizeof(outarg
);
1854 fprintf(stderr
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1855 if (arg
->major
== 7 && arg
->minor
>= 6) {
1856 fprintf(stderr
, "flags=0x%08x\n", arg
->flags
);
1857 fprintf(stderr
, "max_readahead=0x%08x\n",
1858 arg
->max_readahead
);
1861 f
->conn
.proto_major
= arg
->major
;
1862 f
->conn
.proto_minor
= arg
->minor
;
1863 f
->conn
.capable
= 0;
1866 memset(&outarg
, 0, sizeof(outarg
));
1867 outarg
.major
= FUSE_KERNEL_VERSION
;
1868 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1870 if (arg
->major
< 7) {
1871 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
1872 arg
->major
, arg
->minor
);
1873 fuse_reply_err(req
, EPROTO
);
1877 if (arg
->major
> 7) {
1878 /* Wait for a second INIT request with a 7.X version */
1879 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1883 if (arg
->minor
>= 6) {
1884 if (f
->conn
.async_read
)
1885 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
1886 if (arg
->max_readahead
< f
->conn
.max_readahead
)
1887 f
->conn
.max_readahead
= arg
->max_readahead
;
1888 if (arg
->flags
& FUSE_ASYNC_READ
)
1889 f
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1890 if (arg
->flags
& FUSE_POSIX_LOCKS
)
1891 f
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1892 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
)
1893 f
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1894 if (arg
->flags
& FUSE_EXPORT_SUPPORT
)
1895 f
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1896 if (arg
->flags
& FUSE_BIG_WRITES
)
1897 f
->conn
.capable
|= FUSE_CAP_BIG_WRITES
;
1898 if (arg
->flags
& FUSE_DONT_MASK
)
1899 f
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1900 if (arg
->flags
& FUSE_FLOCK_LOCKS
)
1901 f
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1902 if (arg
->flags
& FUSE_AUTO_INVAL_DATA
)
1903 f
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
1904 if (arg
->flags
& FUSE_DO_READDIRPLUS
)
1905 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
1906 if (arg
->flags
& FUSE_READDIRPLUS_AUTO
)
1907 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
1908 if (arg
->flags
& FUSE_ASYNC_DIO
)
1909 f
->conn
.capable
|= FUSE_CAP_ASYNC_DIO
;
1910 if (arg
->flags
& FUSE_WRITEBACK_CACHE
)
1911 f
->conn
.capable
|= FUSE_CAP_WRITEBACK_CACHE
;
1913 f
->conn
.async_read
= 0;
1914 f
->conn
.max_readahead
= 0;
1917 if (req
->f
->conn
.proto_minor
>= 14) {
1919 #ifdef HAVE_VMSPLICE
1920 f
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1921 if (f
->splice_write
)
1922 f
->conn
.want
|= FUSE_CAP_SPLICE_WRITE
;
1924 f
->conn
.want
|= FUSE_CAP_SPLICE_MOVE
;
1926 f
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1928 f
->conn
.want
|= FUSE_CAP_SPLICE_READ
;
1931 if (req
->f
->conn
.proto_minor
>= 18)
1932 f
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
1934 if (f
->atomic_o_trunc
)
1935 f
->conn
.want
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1936 if (f
->op
.getlk
&& f
->op
.setlk
&& !f
->no_remote_posix_lock
)
1937 f
->conn
.want
|= FUSE_CAP_POSIX_LOCKS
;
1938 if (f
->op
.flock
&& !f
->no_remote_flock
)
1939 f
->conn
.want
|= FUSE_CAP_FLOCK_LOCKS
;
1941 f
->conn
.want
|= FUSE_CAP_BIG_WRITES
;
1942 if (f
->auto_inval_data
)
1943 f
->conn
.want
|= FUSE_CAP_AUTO_INVAL_DATA
;
1944 if (f
->op
.readdirplus
&& !f
->no_readdirplus
) {
1945 f
->conn
.want
|= FUSE_CAP_READDIRPLUS
;
1946 if (!f
->no_readdirplus_auto
)
1947 f
->conn
.want
|= FUSE_CAP_READDIRPLUS_AUTO
;
1950 f
->conn
.want
|= FUSE_CAP_ASYNC_DIO
;
1951 if (f
->writeback_cache
)
1952 f
->conn
.want
|= FUSE_CAP_WRITEBACK_CACHE
;
1954 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1955 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
1957 bufsize
= FUSE_MIN_READ_BUFFER
;
1961 if (bufsize
< f
->conn
.max_write
)
1962 f
->conn
.max_write
= bufsize
;
1966 f
->op
.init(f
->userdata
, &f
->conn
);
1968 if (f
->no_splice_read
)
1969 f
->conn
.want
&= ~FUSE_CAP_SPLICE_READ
;
1970 if (f
->no_splice_write
)
1971 f
->conn
.want
&= ~FUSE_CAP_SPLICE_WRITE
;
1972 if (f
->no_splice_move
)
1973 f
->conn
.want
&= ~FUSE_CAP_SPLICE_MOVE
;
1974 if (f
->no_auto_inval_data
)
1975 f
->conn
.want
&= ~FUSE_CAP_AUTO_INVAL_DATA
;
1976 if (f
->no_readdirplus
)
1977 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS
;
1978 if (f
->no_readdirplus_auto
)
1979 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS_AUTO
;
1980 if (f
->no_async_dio
)
1981 f
->conn
.want
&= ~FUSE_CAP_ASYNC_DIO
;
1982 if (f
->no_writeback_cache
)
1983 f
->conn
.want
&= ~FUSE_CAP_WRITEBACK_CACHE
;
1985 if (f
->conn
.async_read
|| (f
->conn
.want
& FUSE_CAP_ASYNC_READ
))
1986 outarg
.flags
|= FUSE_ASYNC_READ
;
1987 if (f
->conn
.want
& FUSE_CAP_POSIX_LOCKS
)
1988 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1989 if (f
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
)
1990 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
1991 if (f
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
)
1992 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
1993 if (f
->conn
.want
& FUSE_CAP_BIG_WRITES
)
1994 outarg
.flags
|= FUSE_BIG_WRITES
;
1995 if (f
->conn
.want
& FUSE_CAP_DONT_MASK
)
1996 outarg
.flags
|= FUSE_DONT_MASK
;
1997 if (f
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
)
1998 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
1999 if (f
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
)
2000 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
2001 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS
)
2002 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
2003 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
)
2004 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
2005 if (f
->conn
.want
& FUSE_CAP_ASYNC_DIO
)
2006 outarg
.flags
|= FUSE_ASYNC_DIO
;
2007 if (f
->conn
.want
& FUSE_CAP_WRITEBACK_CACHE
)
2008 outarg
.flags
|= FUSE_WRITEBACK_CACHE
;
2009 outarg
.max_readahead
= f
->conn
.max_readahead
;
2010 outarg
.max_write
= f
->conn
.max_write
;
2011 if (f
->conn
.proto_minor
>= 13) {
2012 if (f
->conn
.max_background
>= (1 << 16))
2013 f
->conn
.max_background
= (1 << 16) - 1;
2014 if (f
->conn
.congestion_threshold
> f
->conn
.max_background
)
2015 f
->conn
.congestion_threshold
= f
->conn
.max_background
;
2016 if (!f
->conn
.congestion_threshold
) {
2017 f
->conn
.congestion_threshold
=
2018 f
->conn
.max_background
* 3 / 4;
2021 outarg
.max_background
= f
->conn
.max_background
;
2022 outarg
.congestion_threshold
= f
->conn
.congestion_threshold
;
2024 if (f
->conn
.proto_minor
>= 23)
2025 outarg
.time_gran
= f
->conn
.time_gran
;
2028 fprintf(stderr
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
2029 fprintf(stderr
, " flags=0x%08x\n", outarg
.flags
);
2030 fprintf(stderr
, " max_readahead=0x%08x\n",
2031 outarg
.max_readahead
);
2032 fprintf(stderr
, " max_write=0x%08x\n", outarg
.max_write
);
2033 fprintf(stderr
, " max_background=%i\n",
2034 outarg
.max_background
);
2035 fprintf(stderr
, " congestion_threshold=%i\n",
2036 outarg
.congestion_threshold
);
2037 fprintf(stderr
, " time_gran=%u\n",
2041 outargsize
= FUSE_COMPAT_INIT_OUT_SIZE
;
2042 else if (arg
->minor
< 23)
2043 outargsize
= FUSE_COMPAT_22_INIT_OUT_SIZE
;
2045 send_reply_ok(req
, &outarg
, outargsize
);
2048 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
2050 struct fuse_ll
*f
= req
->f
;
2057 f
->op
.destroy(f
->userdata
);
2059 send_reply_ok(req
, NULL
, 0);
2062 static void list_del_nreq(struct fuse_notify_req
*nreq
)
2064 struct fuse_notify_req
*prev
= nreq
->prev
;
2065 struct fuse_notify_req
*next
= nreq
->next
;
2070 static void list_add_nreq(struct fuse_notify_req
*nreq
,
2071 struct fuse_notify_req
*next
)
2073 struct fuse_notify_req
*prev
= next
->prev
;
2080 static void list_init_nreq(struct fuse_notify_req
*nreq
)
2086 static void do_notify_reply(fuse_req_t req
, fuse_ino_t nodeid
,
2087 const void *inarg
, const struct fuse_buf
*buf
)
2089 struct fuse_ll
*f
= req
->f
;
2090 struct fuse_notify_req
*nreq
;
2091 struct fuse_notify_req
*head
;
2093 pthread_mutex_lock(&f
->lock
);
2094 head
= &f
->notify_list
;
2095 for (nreq
= head
->next
; nreq
!= head
; nreq
= nreq
->next
) {
2096 if (nreq
->unique
== req
->unique
) {
2097 list_del_nreq(nreq
);
2101 pthread_mutex_unlock(&f
->lock
);
2104 nreq
->reply(nreq
, req
, nodeid
, inarg
, buf
);
2107 static int send_notify_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
2108 int notify_code
, struct iovec
*iov
, int count
)
2110 struct fuse_out_header out
;
2116 out
.error
= notify_code
;
2117 iov
[0].iov_base
= &out
;
2118 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
2120 return fuse_send_msg(f
, ch
, iov
, count
);
2123 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
2126 struct fuse_notify_poll_wakeup_out outarg
;
2127 struct iovec iov
[2];
2131 iov
[1].iov_base
= &outarg
;
2132 iov
[1].iov_len
= sizeof(outarg
);
2134 return send_notify_iov(ph
->f
, ph
->ch
, FUSE_NOTIFY_POLL
, iov
, 2);
2140 int fuse_lowlevel_notify_inval_inode(struct fuse_chan
*ch
, fuse_ino_t ino
,
2141 off_t off
, off_t len
)
2143 struct fuse_notify_inval_inode_out outarg
;
2145 struct iovec iov
[2];
2150 f
= fuse_chan_session(ch
)->f
;
2158 iov
[1].iov_base
= &outarg
;
2159 iov
[1].iov_len
= sizeof(outarg
);
2161 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
2164 int fuse_lowlevel_notify_inval_entry(struct fuse_chan
*ch
, fuse_ino_t parent
,
2165 const char *name
, size_t namelen
)
2167 struct fuse_notify_inval_entry_out outarg
;
2169 struct iovec iov
[3];
2174 f
= fuse_chan_session(ch
)->f
;
2178 outarg
.parent
= parent
;
2179 outarg
.namelen
= namelen
;
2182 iov
[1].iov_base
= &outarg
;
2183 iov
[1].iov_len
= sizeof(outarg
);
2184 iov
[2].iov_base
= (void *)name
;
2185 iov
[2].iov_len
= namelen
+ 1;
2187 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
2190 int fuse_lowlevel_notify_delete(struct fuse_chan
*ch
,
2191 fuse_ino_t parent
, fuse_ino_t child
,
2192 const char *name
, size_t namelen
)
2194 struct fuse_notify_delete_out outarg
;
2196 struct iovec iov
[3];
2201 f
= fuse_chan_session(ch
)->f
;
2205 if (f
->conn
.proto_minor
< 18)
2208 outarg
.parent
= parent
;
2209 outarg
.child
= child
;
2210 outarg
.namelen
= namelen
;
2213 iov
[1].iov_base
= &outarg
;
2214 iov
[1].iov_len
= sizeof(outarg
);
2215 iov
[2].iov_base
= (void *)name
;
2216 iov
[2].iov_len
= namelen
+ 1;
2218 return send_notify_iov(f
, ch
, FUSE_NOTIFY_DELETE
, iov
, 3);
2221 int fuse_lowlevel_notify_store(struct fuse_chan
*ch
, fuse_ino_t ino
,
2222 off_t offset
, struct fuse_bufvec
*bufv
,
2223 enum fuse_buf_copy_flags flags
)
2225 struct fuse_out_header out
;
2226 struct fuse_notify_store_out outarg
;
2228 struct iovec iov
[3];
2229 size_t size
= fuse_buf_size(bufv
);
2235 f
= fuse_chan_session(ch
)->f
;
2239 if (f
->conn
.proto_minor
< 15)
2243 out
.error
= FUSE_NOTIFY_STORE
;
2245 outarg
.nodeid
= ino
;
2246 outarg
.offset
= offset
;
2249 iov
[0].iov_base
= &out
;
2250 iov
[0].iov_len
= sizeof(out
);
2251 iov
[1].iov_base
= &outarg
;
2252 iov
[1].iov_len
= sizeof(outarg
);
2254 res
= fuse_send_data_iov(f
, ch
, iov
, 2, bufv
, flags
);
2261 struct fuse_retrieve_req
{
2262 struct fuse_notify_req nreq
;
2266 static void fuse_ll_retrieve_reply(struct fuse_notify_req
*nreq
,
2267 fuse_req_t req
, fuse_ino_t ino
,
2269 const struct fuse_buf
*ibuf
)
2271 struct fuse_ll
*f
= req
->f
;
2272 struct fuse_retrieve_req
*rreq
=
2273 container_of(nreq
, struct fuse_retrieve_req
, nreq
);
2274 const struct fuse_notify_retrieve_in
*arg
= inarg
;
2275 struct fuse_bufvec bufv
= {
2280 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
2281 bufv
.buf
[0].mem
= PARAM(arg
);
2283 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
2284 sizeof(struct fuse_notify_retrieve_in
);
2286 if (bufv
.buf
[0].size
< arg
->size
) {
2287 fprintf(stderr
, "fuse: retrieve reply: buffer size too small\n");
2288 fuse_reply_none(req
);
2291 bufv
.buf
[0].size
= arg
->size
;
2293 if (req
->f
->op
.retrieve_reply
) {
2294 req
->f
->op
.retrieve_reply(req
, rreq
->cookie
, ino
,
2295 arg
->offset
, &bufv
);
2297 fuse_reply_none(req
);
2301 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
2302 fuse_ll_clear_pipe(f
);
2305 int fuse_lowlevel_notify_retrieve(struct fuse_chan
*ch
, fuse_ino_t ino
,
2306 size_t size
, off_t offset
, void *cookie
)
2308 struct fuse_notify_retrieve_out outarg
;
2310 struct iovec iov
[2];
2311 struct fuse_retrieve_req
*rreq
;
2317 f
= fuse_chan_session(ch
)->f
;
2321 if (f
->conn
.proto_minor
< 15)
2324 rreq
= malloc(sizeof(*rreq
));
2328 pthread_mutex_lock(&f
->lock
);
2329 rreq
->cookie
= cookie
;
2330 rreq
->nreq
.unique
= f
->notify_ctr
++;
2331 rreq
->nreq
.reply
= fuse_ll_retrieve_reply
;
2332 list_add_nreq(&rreq
->nreq
, &f
->notify_list
);
2333 pthread_mutex_unlock(&f
->lock
);
2335 outarg
.notify_unique
= rreq
->nreq
.unique
;
2336 outarg
.nodeid
= ino
;
2337 outarg
.offset
= offset
;
2340 iov
[1].iov_base
= &outarg
;
2341 iov
[1].iov_len
= sizeof(outarg
);
2343 err
= send_notify_iov(f
, ch
, FUSE_NOTIFY_RETRIEVE
, iov
, 2);
2345 pthread_mutex_lock(&f
->lock
);
2346 list_del_nreq(&rreq
->nreq
);
2347 pthread_mutex_unlock(&f
->lock
);
2354 void *fuse_req_userdata(fuse_req_t req
)
2356 return req
->f
->userdata
;
2359 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2364 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2367 pthread_mutex_lock(&req
->lock
);
2368 pthread_mutex_lock(&req
->f
->lock
);
2369 req
->u
.ni
.func
= func
;
2370 req
->u
.ni
.data
= data
;
2371 pthread_mutex_unlock(&req
->f
->lock
);
2372 if (req
->interrupted
&& func
)
2374 pthread_mutex_unlock(&req
->lock
);
2377 int fuse_req_interrupted(fuse_req_t req
)
2381 pthread_mutex_lock(&req
->f
->lock
);
2382 interrupted
= req
->interrupted
;
2383 pthread_mutex_unlock(&req
->f
->lock
);
2389 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
2392 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2393 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2394 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2395 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2396 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2397 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2398 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2399 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2400 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2401 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2402 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2403 [FUSE_LINK
] = { do_link
, "LINK" },
2404 [FUSE_OPEN
] = { do_open
, "OPEN" },
2405 [FUSE_READ
] = { do_read
, "READ" },
2406 [FUSE_WRITE
] = { do_write
, "WRITE" },
2407 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2408 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2409 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2410 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2411 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2412 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2413 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2414 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2415 [FUSE_INIT
] = { do_init
, "INIT" },
2416 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2417 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2418 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2419 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2420 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2421 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2422 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2423 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2424 [FUSE_CREATE
] = { do_create
, "CREATE" },
2425 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2426 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2427 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2428 [FUSE_POLL
] = { do_poll
, "POLL" },
2429 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
2430 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2431 [FUSE_NOTIFY_REPLY
] = { (void *) 1, "NOTIFY_REPLY" },
2432 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2433 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS"},
2434 [CUSE_INIT
] = { cuse_lowlevel_init
, "CUSE_INIT" },
2437 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2439 static const char *opname(enum fuse_opcode opcode
)
2441 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
2444 return fuse_ll_ops
[opcode
].name
;
2447 static int fuse_ll_copy_from_pipe(struct fuse_bufvec
*dst
,
2448 struct fuse_bufvec
*src
)
2450 int res
= fuse_buf_copy(dst
, src
, 0);
2452 fprintf(stderr
, "fuse: copy from pipe: %s\n", strerror(-res
));
2455 if (res
< fuse_buf_size(dst
)) {
2456 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2462 void fuse_session_process_buf(struct fuse_session
*se
,
2463 const struct fuse_buf
*buf
, struct fuse_chan
*ch
)
2465 struct fuse_ll
*f
= se
->f
;
2466 const size_t write_header_size
= sizeof(struct fuse_in_header
) +
2467 sizeof(struct fuse_write_in
);
2468 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2469 struct fuse_bufvec tmpbuf
= FUSE_BUFVEC_INIT(write_header_size
);
2470 struct fuse_in_header
*in
;
2472 struct fuse_req
*req
;
2477 if (buf
->flags
& FUSE_BUF_IS_FD
) {
2478 if (buf
->size
< tmpbuf
.buf
[0].size
)
2479 tmpbuf
.buf
[0].size
= buf
->size
;
2481 mbuf
= malloc(tmpbuf
.buf
[0].size
);
2483 fprintf(stderr
, "fuse: failed to allocate header\n");
2486 tmpbuf
.buf
[0].mem
= mbuf
;
2488 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2499 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2500 (unsigned long long) in
->unique
,
2501 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
2502 (unsigned long long) in
->nodeid
, buf
->size
, in
->pid
);
2505 req
= fuse_ll_alloc_req(f
);
2507 struct fuse_out_header out
= {
2508 .unique
= in
->unique
,
2511 struct iovec iov
= {
2513 .iov_len
= sizeof(struct fuse_out_header
),
2516 fuse_send_msg(f
, ch
, &iov
, 1);
2520 req
->unique
= in
->unique
;
2521 req
->ctx
.uid
= in
->uid
;
2522 req
->ctx
.gid
= in
->gid
;
2523 req
->ctx
.pid
= in
->pid
;
2528 enum fuse_opcode expected
;
2530 expected
= f
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2531 if (in
->opcode
!= expected
)
2533 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
)
2537 if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
2538 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2539 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2540 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2541 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2542 in
->opcode
!= FUSE_NOTIFY_REPLY
&&
2543 in
->opcode
!= FUSE_READDIRPLUS
)
2547 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
2549 if (in
->opcode
!= FUSE_INTERRUPT
) {
2550 struct fuse_req
*intr
;
2551 pthread_mutex_lock(&f
->lock
);
2552 intr
= check_interrupt(f
, req
);
2553 list_add_req(req
, &f
->list
);
2554 pthread_mutex_unlock(&f
->lock
);
2556 fuse_reply_err(intr
, EAGAIN
);
2559 if ((buf
->flags
& FUSE_BUF_IS_FD
) && write_header_size
< buf
->size
&&
2560 (in
->opcode
!= FUSE_WRITE
|| !f
->op
.write_buf
) &&
2561 in
->opcode
!= FUSE_NOTIFY_REPLY
) {
2565 newmbuf
= realloc(mbuf
, buf
->size
);
2566 if (newmbuf
== NULL
)
2570 tmpbuf
= FUSE_BUFVEC_INIT(buf
->size
- write_header_size
);
2571 tmpbuf
.buf
[0].mem
= mbuf
+ write_header_size
;
2573 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2581 inarg
= (void *) &in
[1];
2582 if (in
->opcode
== FUSE_WRITE
&& f
->op
.write_buf
)
2583 do_write_buf(req
, in
->nodeid
, inarg
, buf
);
2584 else if (in
->opcode
== FUSE_NOTIFY_REPLY
)
2585 do_notify_reply(req
, in
->nodeid
, inarg
, buf
);
2587 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
2594 fuse_reply_err(req
, err
);
2596 if (buf
->flags
& FUSE_BUF_IS_FD
)
2597 fuse_ll_clear_pipe(f
);
2606 static const struct fuse_opt fuse_ll_opts
[] = {
2607 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
2608 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
2609 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
2610 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
2611 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
2612 { "max_background=%u", offsetof(struct fuse_ll
, conn
.max_background
), 0 },
2613 { "congestion_threshold=%u",
2614 offsetof(struct fuse_ll
, conn
.congestion_threshold
), 0 },
2615 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
2616 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
2617 { "atomic_o_trunc", offsetof(struct fuse_ll
, atomic_o_trunc
), 1},
2618 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2619 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2620 { "no_remote_flock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2621 { "no_remote_posix_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2622 { "big_writes", offsetof(struct fuse_ll
, big_writes
), 1},
2623 { "splice_write", offsetof(struct fuse_ll
, splice_write
), 1},
2624 { "no_splice_write", offsetof(struct fuse_ll
, no_splice_write
), 1},
2625 { "splice_move", offsetof(struct fuse_ll
, splice_move
), 1},
2626 { "no_splice_move", offsetof(struct fuse_ll
, no_splice_move
), 1},
2627 { "splice_read", offsetof(struct fuse_ll
, splice_read
), 1},
2628 { "no_splice_read", offsetof(struct fuse_ll
, no_splice_read
), 1},
2629 { "auto_inval_data", offsetof(struct fuse_ll
, auto_inval_data
), 1},
2630 { "no_auto_inval_data", offsetof(struct fuse_ll
, no_auto_inval_data
), 1},
2631 { "readdirplus=no", offsetof(struct fuse_ll
, no_readdirplus
), 1},
2632 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2633 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus_auto
), 1},
2634 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2635 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus_auto
), 0},
2636 { "async_dio", offsetof(struct fuse_ll
, async_dio
), 1},
2637 { "no_async_dio", offsetof(struct fuse_ll
, no_async_dio
), 1},
2638 { "writeback_cache", offsetof(struct fuse_ll
, writeback_cache
), 1},
2639 { "no_writeback_cache", offsetof(struct fuse_ll
, no_writeback_cache
), 1},
2640 { "time_gran=%u", offsetof(struct fuse_ll
, conn
.time_gran
), 0 },
2641 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
2642 FUSE_OPT_KEY("-h", KEY_HELP
),
2643 FUSE_OPT_KEY("--help", KEY_HELP
),
2644 FUSE_OPT_KEY("-V", KEY_VERSION
),
2645 FUSE_OPT_KEY("--version", KEY_VERSION
),
2649 static void fuse_ll_version(void)
2651 printf("using FUSE kernel interface version %i.%i\n",
2652 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
2655 static void fuse_ll_help(void)
2658 " -o max_write=N set maximum size of write requests\n"
2659 " -o max_readahead=N set maximum readahead\n"
2660 " -o max_background=N set number of maximum background requests\n"
2661 " -o congestion_threshold=N set kernel's congestion threshold\n"
2662 " -o async_read perform reads asynchronously (default)\n"
2663 " -o sync_read perform reads synchronously\n"
2664 " -o atomic_o_trunc enable atomic open+truncate support\n"
2665 " -o big_writes enable larger than 4kB writes\n"
2666 " -o no_remote_lock disable remote file locking\n"
2667 " -o no_remote_flock disable remote file locking (BSD)\n"
2668 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2669 " -o [no_]splice_write use splice to write to the fuse device\n"
2670 " -o [no_]splice_move move data while splicing to the fuse device\n"
2671 " -o [no_]splice_read use splice to read from the fuse device\n"
2672 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2673 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2674 " -o [no_]async_dio asynchronous direct I/O\n"
2675 " -o [no_]writeback_cache asynchronous, buffered writes\n"
2676 " -o time_gran=N time granularity in nsec\n"
2680 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
2681 struct fuse_args
*outargs
)
2683 (void) data
; (void) outargs
;
2695 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
2701 static void fuse_ll_destroy(struct fuse_ll
*f
)
2703 struct fuse_ll_pipe
*llp
;
2705 if (f
->got_init
&& !f
->got_destroy
) {
2707 f
->op
.destroy(f
->userdata
);
2709 llp
= pthread_getspecific(f
->pipe_key
);
2711 fuse_ll_pipe_free(llp
);
2712 pthread_key_delete(f
->pipe_key
);
2713 pthread_mutex_destroy(&f
->lock
);
2718 void fuse_session_destroy(struct fuse_session
*se
)
2720 fuse_ll_destroy(se
->f
);
2722 fuse_chan_destroy(se
->ch
);
2727 static void fuse_ll_pipe_destructor(void *data
)
2729 struct fuse_ll_pipe
*llp
= data
;
2730 fuse_ll_pipe_free(llp
);
2734 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2735 struct fuse_chan
*ch
)
2737 struct fuse_ll
*f
= se
->f
;
2738 size_t bufsize
= f
->bufsize
;
2739 struct fuse_ll_pipe
*llp
;
2740 struct fuse_buf tmpbuf
;
2744 if (f
->conn
.proto_minor
< 14 || !(f
->conn
.want
& FUSE_CAP_SPLICE_READ
))
2747 llp
= fuse_ll_get_pipe(f
);
2751 if (llp
->size
< bufsize
) {
2752 if (llp
->can_grow
) {
2753 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, bufsize
);
2760 if (llp
->size
< bufsize
)
2764 res
= splice(fuse_chan_fd(ch
), NULL
, llp
->pipe
[1], NULL
, bufsize
, 0);
2767 if (fuse_session_exited(se
))
2771 if (err
== ENODEV
) {
2772 fuse_session_exit(se
);
2775 if (err
!= EINTR
&& err
!= EAGAIN
)
2776 perror("fuse: splice from device");
2780 if (res
< sizeof(struct fuse_in_header
)) {
2781 fprintf(stderr
, "short splice from fuse device\n");
2785 tmpbuf
= (struct fuse_buf
) {
2787 .flags
= FUSE_BUF_IS_FD
,
2792 * Don't bother with zero copy for small requests.
2793 * fuse_loop_mt() needs to check for FORGET so this more than
2794 * just an optimization.
2796 if (res
< sizeof(struct fuse_in_header
) +
2797 sizeof(struct fuse_write_in
) + pagesize
) {
2798 struct fuse_bufvec src
= { .buf
[0] = tmpbuf
, .count
= 1 };
2799 struct fuse_bufvec dst
= { .count
= 1 };
2802 buf
->mem
= malloc(f
->bufsize
);
2805 "fuse: failed to allocate read buffer\n");
2809 buf
->size
= f
->bufsize
;
2813 res
= fuse_buf_copy(&dst
, &src
, 0);
2815 fprintf(stderr
, "fuse: copy from pipe: %s\n",
2817 fuse_ll_clear_pipe(f
);
2820 if (res
< tmpbuf
.size
) {
2821 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2822 fuse_ll_clear_pipe(f
);
2825 assert(res
== tmpbuf
.size
);
2828 /* Don't overwrite buf->mem, as that would cause a leak */
2829 buf
->fd
= tmpbuf
.fd
;
2830 buf
->flags
= tmpbuf
.flags
;
2832 buf
->size
= tmpbuf
.size
;
2837 return fuse_chan_recv(se
, buf
, ch
);
2840 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2841 struct fuse_chan
*ch
)
2843 return fuse_chan_recv(se
, buf
, ch
);
2847 #define MIN_BUFSIZE 0x21000
2849 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
2850 const struct fuse_lowlevel_ops
*op
,
2851 size_t op_size
, void *userdata
)
2855 struct fuse_session
*se
;
2857 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2858 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
2859 op_size
= sizeof(struct fuse_lowlevel_ops
);
2862 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
2864 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
2868 f
->conn
.async_read
= 1;
2869 f
->conn
.max_write
= UINT_MAX
;
2870 f
->conn
.max_readahead
= UINT_MAX
;
2871 f
->atomic_o_trunc
= 0;
2872 f
->bufsize
= getpagesize() + 0x1000;
2873 f
->bufsize
= f
->bufsize
< MIN_BUFSIZE
? MIN_BUFSIZE
: f
->bufsize
;
2875 list_init_req(&f
->list
);
2876 list_init_req(&f
->interrupts
);
2877 list_init_nreq(&f
->notify_list
);
2879 fuse_mutex_init(&f
->lock
);
2881 err
= pthread_key_create(&f
->pipe_key
, fuse_ll_pipe_destructor
);
2883 fprintf(stderr
, "fuse: failed to create thread specific key: %s\n",
2888 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
2889 goto out_key_destroy
;
2892 fprintf(stderr
, "FUSE library version: %s\n", PACKAGE_VERSION
);
2894 memcpy(&f
->op
, op
, op_size
);
2895 f
->owner
= getuid();
2896 f
->userdata
= userdata
;
2898 se
= fuse_session_new();
2900 goto out_key_destroy
;
2907 pthread_key_delete(f
->pipe_key
);
2909 pthread_mutex_destroy(&f
->lock
);
2916 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2919 size_t bufsize
= 1024;
2923 unsigned long pid
= req
->ctx
.pid
;
2926 sprintf(path
, "/proc/%lu/task/%lu/status", pid
, pid
);
2929 buf
= malloc(bufsize
);
2934 fd
= open(path
, O_RDONLY
);
2938 ret
= read(fd
, buf
, bufsize
);
2945 if (ret
== bufsize
) {
2952 s
= strstr(buf
, "\nGroups:");
2960 unsigned long val
= strtoul(s
, &end
, 0);
2976 * This is currently not implemented on other than Linux...
2978 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])