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 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
83 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
86 static size_t iov_length(const struct iovec
*iov
, size_t count
)
91 for (seg
= 0; seg
< count
; seg
++)
92 ret
+= iov
[seg
].iov_len
;
96 static void list_init_req(struct fuse_req
*req
)
102 static void list_del_req(struct fuse_req
*req
)
104 struct fuse_req
*prev
= req
->prev
;
105 struct fuse_req
*next
= req
->next
;
110 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
112 struct fuse_req
*prev
= next
->prev
;
119 static void destroy_req(fuse_req_t req
)
121 pthread_mutex_destroy(&req
->lock
);
125 void fuse_free_req(fuse_req_t req
)
128 struct fuse_ll
*f
= req
->f
;
130 pthread_mutex_lock(&f
->lock
);
131 req
->u
.ni
.func
= NULL
;
132 req
->u
.ni
.data
= NULL
;
135 pthread_mutex_unlock(&f
->lock
);
140 static struct fuse_req
*fuse_ll_alloc_req(struct fuse_ll
*f
)
142 struct fuse_req
*req
;
144 req
= (struct fuse_req
*) calloc(1, sizeof(struct fuse_req
));
146 fprintf(stderr
, "fuse: failed to allocate request\n");
151 fuse_mutex_init(&req
->lock
);
157 static int fuse_chan_recv(struct fuse_session
*se
, struct fuse_buf
*buf
,
158 struct fuse_chan
*ch
)
160 struct fuse_ll
*f
= se
->f
;
165 buf
->mem
= malloc(f
->bufsize
);
168 "fuse: failed to allocate read buffer\n");
174 res
= read(fuse_chan_fd(ch
), buf
->mem
, f
->bufsize
);
177 if (fuse_session_exited(se
))
180 /* ENOENT means the operation was interrupted, it's safe
186 fuse_session_exit(se
);
189 /* Errors occurring during normal operation: EINTR (read
190 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
192 if (err
!= EINTR
&& err
!= EAGAIN
)
193 perror("fuse: reading device");
196 if ((size_t) res
< sizeof(struct fuse_in_header
)) {
197 fprintf(stderr
, "short read on fuse device\n");
206 static int fuse_chan_send(struct fuse_chan
*ch
, const struct iovec iov
[],
209 ssize_t res
= writev(fuse_chan_fd(ch
), iov
, count
);
213 struct fuse_session
*se
= fuse_chan_session(ch
);
217 /* ENOENT means the operation was interrupted */
218 if (!fuse_session_exited(se
) && err
!= ENOENT
)
219 perror("fuse: writing device");
226 void fuse_chan_close(struct fuse_chan
*ch
)
228 int fd
= fuse_chan_fd(ch
);
234 static int fuse_send_msg(struct fuse_ll
*f
, struct fuse_chan
*ch
,
235 struct iovec
*iov
, int count
)
237 struct fuse_out_header
*out
= iov
[0].iov_base
;
239 out
->len
= iov_length(iov
, count
);
241 if (out
->unique
== 0) {
242 fprintf(stderr
, "NOTIFY: code=%d length=%u\n",
243 out
->error
, out
->len
);
244 } else if (out
->error
) {
246 " unique: %llu, error: %i (%s), outsize: %i\n",
247 (unsigned long long) out
->unique
, out
->error
,
248 strerror(-out
->error
), out
->len
);
251 " unique: %llu, success, outsize: %i\n",
252 (unsigned long long) out
->unique
, out
->len
);
256 return fuse_chan_send(ch
, iov
, count
);
259 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
262 struct fuse_out_header out
;
264 if (error
<= -1000 || error
> 0) {
265 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
269 out
.unique
= req
->unique
;
272 iov
[0].iov_base
= &out
;
273 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
275 return fuse_send_msg(req
->f
, req
->ch
, iov
, count
);
278 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
283 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
288 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
294 iov
[1].iov_base
= (void *) arg
;
295 iov
[1].iov_len
= argsize
;
298 return send_reply_iov(req
, error
, iov
, count
);
301 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
304 struct iovec
*padded_iov
;
306 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
307 if (padded_iov
== NULL
)
308 return fuse_reply_err(req
, ENOMEM
);
310 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
313 res
= send_reply_iov(req
, 0, padded_iov
, count
);
319 static size_t fuse_dirent_size(size_t namelen
)
321 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
324 static void fuse_add_dirent(struct fuse_dirent
*dirent
, const char *name
,
325 const struct stat
*stbuf
, off_t off
)
327 unsigned namelen
= strlen(name
);
328 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
329 unsigned entsize
= fuse_dirent_size(namelen
);
330 unsigned padlen
= entsize
- entlen
;
332 dirent
->ino
= stbuf
->st_ino
;
334 dirent
->namelen
= namelen
;
335 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
336 strncpy(dirent
->name
, name
, namelen
);
338 memset(dirent
->name
+ namelen
, 0, padlen
);
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((struct fuse_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
)
421 entsize
= FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET_DIRENTPLUS
+ strlen(name
));
422 if (entsize
<= bufsize
&& buf
) {
423 struct fuse_direntplus
*dp
= (struct fuse_direntplus
*) buf
;
424 memset(&dp
->entry_out
, 0, sizeof(dp
->entry_out
));
425 fill_entry(&dp
->entry_out
, e
);
426 fuse_add_dirent(&dp
->dirent
, name
, &e
->attr
, off
);
431 static void fill_open(struct fuse_open_out
*arg
,
432 const struct fuse_file_info
*f
)
436 arg
->open_flags
|= FOPEN_DIRECT_IO
;
438 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
440 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
443 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
445 struct fuse_entry_out arg
;
446 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
447 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(arg
);
449 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
451 if (!e
->ino
&& req
->f
->conn
.proto_minor
< 4)
452 return fuse_reply_err(req
, ENOENT
);
454 memset(&arg
, 0, sizeof(arg
));
456 return send_reply_ok(req
, &arg
, size
);
459 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
460 const struct fuse_file_info
*f
)
462 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
463 size_t entrysize
= req
->f
->conn
.proto_minor
< 9 ?
464 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(struct fuse_entry_out
);
465 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*) buf
;
466 struct fuse_open_out
*oarg
= (struct fuse_open_out
*) (buf
+ entrysize
);
468 memset(buf
, 0, sizeof(buf
));
471 return send_reply_ok(req
, buf
,
472 entrysize
+ sizeof(struct fuse_open_out
));
475 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
478 struct fuse_attr_out arg
;
479 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
480 FUSE_COMPAT_ATTR_OUT_SIZE
: sizeof(arg
);
482 memset(&arg
, 0, sizeof(arg
));
483 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
484 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
485 convert_stat(attr
, &arg
.attr
);
487 return send_reply_ok(req
, &arg
, size
);
490 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
492 return send_reply_ok(req
, linkname
, strlen(linkname
));
495 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
497 struct fuse_open_out arg
;
499 memset(&arg
, 0, sizeof(arg
));
501 return send_reply_ok(req
, &arg
, sizeof(arg
));
504 int fuse_reply_write(fuse_req_t req
, size_t count
)
506 struct fuse_write_out arg
;
508 memset(&arg
, 0, sizeof(arg
));
511 return send_reply_ok(req
, &arg
, sizeof(arg
));
514 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
516 return send_reply_ok(req
, buf
, size
);
519 static int fuse_send_data_iov_fallback(struct fuse_ll
*f
, struct fuse_chan
*ch
,
520 struct iovec
*iov
, int iov_count
,
521 struct fuse_bufvec
*buf
,
524 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
528 /* Optimize common case */
529 if (buf
->count
== 1 && buf
->idx
== 0 && buf
->off
== 0 &&
530 !(buf
->buf
[0].flags
& FUSE_BUF_IS_FD
)) {
531 /* FIXME: also avoid memory copy if there are multiple buffers
532 but none of them contain an fd */
534 iov
[iov_count
].iov_base
= buf
->buf
[0].mem
;
535 iov
[iov_count
].iov_len
= len
;
537 return fuse_send_msg(f
, ch
, iov
, iov_count
);
540 res
= posix_memalign(&mbuf
, pagesize
, len
);
544 mem_buf
.buf
[0].mem
= mbuf
;
545 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
552 iov
[iov_count
].iov_base
= mbuf
;
553 iov
[iov_count
].iov_len
= len
;
555 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
561 struct fuse_ll_pipe
{
567 static void fuse_ll_pipe_free(struct fuse_ll_pipe
*llp
)
575 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
576 static int fuse_pipe(int fds
[2])
583 if (fcntl(fds
[0], F_SETFL
, O_NONBLOCK
) == -1 ||
584 fcntl(fds
[1], F_SETFL
, O_NONBLOCK
) == -1 ||
585 fcntl(fds
[0], F_SETFD
, FD_CLOEXEC
) == -1 ||
586 fcntl(fds
[1], F_SETFD
, FD_CLOEXEC
) == -1) {
594 static int fuse_pipe(int fds
[2])
596 return pipe2(fds
, O_CLOEXEC
| O_NONBLOCK
);
600 static struct fuse_ll_pipe
*fuse_ll_get_pipe(struct fuse_ll
*f
)
602 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
606 llp
= malloc(sizeof(struct fuse_ll_pipe
));
610 res
= fuse_pipe(llp
->pipe
);
617 *the default size is 16 pages on linux
619 llp
->size
= pagesize
* 16;
622 pthread_setspecific(f
->pipe_key
, llp
);
629 static void fuse_ll_clear_pipe(struct fuse_ll
*f
)
631 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
633 pthread_setspecific(f
->pipe_key
, NULL
);
634 fuse_ll_pipe_free(llp
);
638 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
639 static int read_back(int fd
, char *buf
, size_t len
)
643 res
= read(fd
, buf
, len
);
645 fprintf(stderr
, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno
));
649 fprintf(stderr
, "fuse: internal error: short read back from pipe: %i from %zi\n", res
, len
);
655 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
656 struct iovec
*iov
, int iov_count
,
657 struct fuse_bufvec
*buf
, unsigned int flags
)
660 size_t len
= fuse_buf_size(buf
);
661 struct fuse_out_header
*out
= iov
[0].iov_base
;
662 struct fuse_ll_pipe
*llp
;
665 size_t total_fd_size
;
668 struct fuse_bufvec pipe_buf
= FUSE_BUFVEC_INIT(len
);
670 if (f
->broken_splice_nonblock
)
673 if (flags
& FUSE_BUF_NO_SPLICE
)
677 for (idx
= buf
->idx
; idx
< buf
->count
; idx
++) {
678 if (buf
->buf
[idx
].flags
& FUSE_BUF_IS_FD
) {
679 total_fd_size
= buf
->buf
[idx
].size
;
681 total_fd_size
-= buf
->off
;
684 if (total_fd_size
< 2 * pagesize
)
687 if (f
->conn
.proto_minor
< 14 ||
688 !(f
->conn
.want
& FUSE_CAP_SPLICE_WRITE
))
691 llp
= fuse_ll_get_pipe(f
);
696 headerlen
= iov_length(iov
, iov_count
);
698 out
->len
= headerlen
+ len
;
701 * Heuristic for the required pipe size, does not work if the
702 * source contains less than page size fragments
704 pipesize
= pagesize
* (iov_count
+ buf
->count
+ 1) + out
->len
;
706 if (llp
->size
< pipesize
) {
708 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, pipesize
);
715 if (llp
->size
< pipesize
)
720 res
= vmsplice(llp
->pipe
[1], iov
, iov_count
, SPLICE_F_NONBLOCK
);
724 if (res
!= headerlen
) {
726 fprintf(stderr
, "fuse: short vmsplice to pipe: %u/%zu\n", res
,
731 pipe_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
;
732 pipe_buf
.buf
[0].fd
= llp
->pipe
[1];
734 res
= fuse_buf_copy(&pipe_buf
, buf
,
735 FUSE_BUF_FORCE_SPLICE
| FUSE_BUF_SPLICE_NONBLOCK
);
737 if (res
== -EAGAIN
|| res
== -EINVAL
) {
739 * Should only get EAGAIN on kernels with
740 * broken SPLICE_F_NONBLOCK support (<=
741 * 2.6.35) where this error or a short read is
742 * returned even if the pipe itself is not
745 * EINVAL might mean that splice can't handle
746 * this combination of input and output.
749 f
->broken_splice_nonblock
= 1;
751 pthread_setspecific(f
->pipe_key
, NULL
);
752 fuse_ll_pipe_free(llp
);
759 if (res
!= 0 && res
< len
) {
760 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
762 size_t now_len
= res
;
764 * For regular files a short count is either
766 * 2) because of broken SPLICE_F_NONBLOCK (see above)
768 * For other inputs it's possible that we overflowed
769 * the pipe because of small buffer fragments.
772 res
= posix_memalign(&mbuf
, pagesize
, len
);
776 mem_buf
.buf
[0].mem
= mbuf
;
777 mem_buf
.off
= now_len
;
778 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
781 size_t extra_len
= res
;
783 * Trickiest case: got more data. Need to get
784 * back the data from the pipe and then fall
785 * back to regular write.
787 tmpbuf
= malloc(headerlen
);
788 if (tmpbuf
== NULL
) {
793 res
= read_back(llp
->pipe
[0], tmpbuf
, headerlen
);
799 res
= read_back(llp
->pipe
[0], mbuf
, now_len
);
804 len
= now_len
+ extra_len
;
805 iov
[iov_count
].iov_base
= mbuf
;
806 iov
[iov_count
].iov_len
= len
;
808 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
816 out
->len
= headerlen
+ len
;
820 " unique: %llu, success, outsize: %i (splice)\n",
821 (unsigned long long) out
->unique
, out
->len
);
825 if ((flags
& FUSE_BUF_SPLICE_MOVE
) &&
826 (f
->conn
.want
& FUSE_CAP_SPLICE_MOVE
))
827 splice_flags
|= SPLICE_F_MOVE
;
829 res
= splice(llp
->pipe
[0], NULL
,
830 fuse_chan_fd(ch
), NULL
, out
->len
, splice_flags
);
833 perror("fuse: splice from pipe");
836 if (res
!= out
->len
) {
838 fprintf(stderr
, "fuse: short splice from pipe: %u/%u\n",
845 fuse_ll_clear_pipe(f
);
849 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
852 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
853 struct iovec
*iov
, int iov_count
,
854 struct fuse_bufvec
*buf
, unsigned int flags
)
856 size_t len
= fuse_buf_size(buf
);
859 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
863 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
,
864 enum fuse_buf_copy_flags flags
)
867 struct fuse_out_header out
;
870 iov
[0].iov_base
= &out
;
871 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
873 out
.unique
= req
->unique
;
876 res
= fuse_send_data_iov(req
->f
, req
->ch
, iov
, 1, bufv
, flags
);
881 return fuse_reply_err(req
, res
);
885 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
887 struct fuse_statfs_out arg
;
888 size_t size
= req
->f
->conn
.proto_minor
< 4 ?
889 FUSE_COMPAT_STATFS_SIZE
: sizeof(arg
);
891 memset(&arg
, 0, sizeof(arg
));
892 convert_statfs(stbuf
, &arg
.st
);
894 return send_reply_ok(req
, &arg
, size
);
897 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
899 struct fuse_getxattr_out arg
;
901 memset(&arg
, 0, sizeof(arg
));
904 return send_reply_ok(req
, &arg
, sizeof(arg
));
907 int fuse_reply_lock(fuse_req_t req
, const struct flock
*lock
)
909 struct fuse_lk_out arg
;
911 memset(&arg
, 0, sizeof(arg
));
912 arg
.lk
.type
= lock
->l_type
;
913 if (lock
->l_type
!= F_UNLCK
) {
914 arg
.lk
.start
= lock
->l_start
;
915 if (lock
->l_len
== 0)
916 arg
.lk
.end
= OFFSET_MAX
;
918 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
920 arg
.lk
.pid
= lock
->l_pid
;
921 return send_reply_ok(req
, &arg
, sizeof(arg
));
924 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
926 struct fuse_bmap_out arg
;
928 memset(&arg
, 0, sizeof(arg
));
931 return send_reply_ok(req
, &arg
, sizeof(arg
));
934 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
937 struct fuse_ioctl_iovec
*fiov
;
940 fiov
= malloc(sizeof(fiov
[0]) * count
);
944 for (i
= 0; i
< count
; i
++) {
945 fiov
[i
].base
= (uintptr_t) iov
[i
].iov_base
;
946 fiov
[i
].len
= iov
[i
].iov_len
;
952 int fuse_reply_ioctl_retry(fuse_req_t req
,
953 const struct iovec
*in_iov
, size_t in_count
,
954 const struct iovec
*out_iov
, size_t out_count
)
956 struct fuse_ioctl_out arg
;
957 struct fuse_ioctl_iovec
*in_fiov
= NULL
;
958 struct fuse_ioctl_iovec
*out_fiov
= NULL
;
963 memset(&arg
, 0, sizeof(arg
));
964 arg
.flags
|= FUSE_IOCTL_RETRY
;
965 arg
.in_iovs
= in_count
;
966 arg
.out_iovs
= out_count
;
967 iov
[count
].iov_base
= &arg
;
968 iov
[count
].iov_len
= sizeof(arg
);
971 if (req
->f
->conn
.proto_minor
< 16) {
973 iov
[count
].iov_base
= (void *)in_iov
;
974 iov
[count
].iov_len
= sizeof(in_iov
[0]) * in_count
;
979 iov
[count
].iov_base
= (void *)out_iov
;
980 iov
[count
].iov_len
= sizeof(out_iov
[0]) * out_count
;
984 /* Can't handle non-compat 64bit ioctls on 32bit */
985 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
986 res
= fuse_reply_err(req
, EINVAL
);
991 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
995 iov
[count
].iov_base
= (void *)in_fiov
;
996 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
1000 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
1004 iov
[count
].iov_base
= (void *)out_fiov
;
1005 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
1010 res
= send_reply_iov(req
, 0, iov
, count
);
1018 res
= fuse_reply_err(req
, ENOMEM
);
1022 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
1024 struct fuse_ioctl_out arg
;
1025 struct iovec iov
[3];
1028 memset(&arg
, 0, sizeof(arg
));
1029 arg
.result
= result
;
1030 iov
[count
].iov_base
= &arg
;
1031 iov
[count
].iov_len
= sizeof(arg
);
1035 iov
[count
].iov_base
= (char *) buf
;
1036 iov
[count
].iov_len
= size
;
1040 return send_reply_iov(req
, 0, iov
, count
);
1043 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
1046 struct iovec
*padded_iov
;
1047 struct fuse_ioctl_out arg
;
1050 padded_iov
= malloc((count
+ 2) * sizeof(struct iovec
));
1051 if (padded_iov
== NULL
)
1052 return fuse_reply_err(req
, ENOMEM
);
1054 memset(&arg
, 0, sizeof(arg
));
1055 arg
.result
= result
;
1056 padded_iov
[1].iov_base
= &arg
;
1057 padded_iov
[1].iov_len
= sizeof(arg
);
1059 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
1061 res
= send_reply_iov(req
, 0, padded_iov
, count
+ 2);
1067 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
1069 struct fuse_poll_out arg
;
1071 memset(&arg
, 0, sizeof(arg
));
1072 arg
.revents
= revents
;
1074 return send_reply_ok(req
, &arg
, sizeof(arg
));
1077 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1079 char *name
= (char *) inarg
;
1081 if (req
->f
->op
.lookup
)
1082 req
->f
->op
.lookup(req
, nodeid
, name
);
1084 fuse_reply_err(req
, ENOSYS
);
1087 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1089 struct fuse_forget_in
*arg
= (struct fuse_forget_in
*) inarg
;
1091 if (req
->f
->op
.forget
)
1092 req
->f
->op
.forget(req
, nodeid
, arg
->nlookup
);
1094 fuse_reply_none(req
);
1097 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
1100 struct fuse_batch_forget_in
*arg
= (void *) inarg
;
1101 struct fuse_forget_one
*param
= (void *) PARAM(arg
);
1106 if (req
->f
->op
.forget_multi
) {
1107 req
->f
->op
.forget_multi(req
, arg
->count
,
1108 (struct fuse_forget_data
*) param
);
1109 } else if (req
->f
->op
.forget
) {
1110 for (i
= 0; i
< arg
->count
; i
++) {
1111 struct fuse_forget_one
*forget
= ¶m
[i
];
1112 struct fuse_req
*dummy_req
;
1114 dummy_req
= fuse_ll_alloc_req(req
->f
);
1115 if (dummy_req
== NULL
)
1118 dummy_req
->unique
= req
->unique
;
1119 dummy_req
->ctx
= req
->ctx
;
1120 dummy_req
->ch
= NULL
;
1122 req
->f
->op
.forget(dummy_req
, forget
->nodeid
,
1125 fuse_reply_none(req
);
1127 fuse_reply_none(req
);
1131 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1133 struct fuse_file_info
*fip
= NULL
;
1134 struct fuse_file_info fi
;
1136 if (req
->f
->conn
.proto_minor
>= 9) {
1137 struct fuse_getattr_in
*arg
= (struct fuse_getattr_in
*) inarg
;
1139 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
1140 memset(&fi
, 0, sizeof(fi
));
1146 if (req
->f
->op
.getattr
)
1147 req
->f
->op
.getattr(req
, nodeid
, fip
);
1149 fuse_reply_err(req
, ENOSYS
);
1152 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1154 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*) inarg
;
1156 if (req
->f
->op
.setattr
) {
1157 struct fuse_file_info
*fi
= NULL
;
1158 struct fuse_file_info fi_store
;
1160 memset(&stbuf
, 0, sizeof(stbuf
));
1161 convert_attr(arg
, &stbuf
);
1162 if (arg
->valid
& FATTR_FH
) {
1163 arg
->valid
&= ~FATTR_FH
;
1164 memset(&fi_store
, 0, sizeof(fi_store
));
1169 FUSE_SET_ATTR_MODE
|
1172 FUSE_SET_ATTR_SIZE
|
1173 FUSE_SET_ATTR_ATIME
|
1174 FUSE_SET_ATTR_MTIME
|
1175 FUSE_SET_ATTR_ATIME_NOW
|
1176 FUSE_SET_ATTR_MTIME_NOW
;
1178 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
1180 fuse_reply_err(req
, ENOSYS
);
1183 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1185 struct fuse_access_in
*arg
= (struct fuse_access_in
*) inarg
;
1187 if (req
->f
->op
.access
)
1188 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
1190 fuse_reply_err(req
, ENOSYS
);
1193 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1197 if (req
->f
->op
.readlink
)
1198 req
->f
->op
.readlink(req
, nodeid
);
1200 fuse_reply_err(req
, ENOSYS
);
1203 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1205 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*) inarg
;
1206 char *name
= PARAM(arg
);
1208 if (req
->f
->conn
.proto_minor
>= 12)
1209 req
->ctx
.umask
= arg
->umask
;
1211 name
= (char *) inarg
+ FUSE_COMPAT_MKNOD_IN_SIZE
;
1213 if (req
->f
->op
.mknod
)
1214 req
->f
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
1216 fuse_reply_err(req
, ENOSYS
);
1219 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1221 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*) inarg
;
1223 if (req
->f
->conn
.proto_minor
>= 12)
1224 req
->ctx
.umask
= arg
->umask
;
1226 if (req
->f
->op
.mkdir
)
1227 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
1229 fuse_reply_err(req
, ENOSYS
);
1232 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1234 char *name
= (char *) inarg
;
1236 if (req
->f
->op
.unlink
)
1237 req
->f
->op
.unlink(req
, nodeid
, name
);
1239 fuse_reply_err(req
, ENOSYS
);
1242 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1244 char *name
= (char *) inarg
;
1246 if (req
->f
->op
.rmdir
)
1247 req
->f
->op
.rmdir(req
, nodeid
, name
);
1249 fuse_reply_err(req
, ENOSYS
);
1252 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1254 char *name
= (char *) inarg
;
1255 char *linkname
= ((char *) inarg
) + strlen((char *) inarg
) + 1;
1257 if (req
->f
->op
.symlink
)
1258 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
1260 fuse_reply_err(req
, ENOSYS
);
1263 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1265 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*) inarg
;
1266 char *oldname
= PARAM(arg
);
1267 char *newname
= oldname
+ strlen(oldname
) + 1;
1269 if (req
->f
->op
.rename
)
1270 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
1272 fuse_reply_err(req
, ENOSYS
);
1275 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1277 struct fuse_link_in
*arg
= (struct fuse_link_in
*) inarg
;
1279 if (req
->f
->op
.link
)
1280 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
1282 fuse_reply_err(req
, ENOSYS
);
1285 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1287 struct fuse_create_in
*arg
= (struct fuse_create_in
*) inarg
;
1289 if (req
->f
->op
.create
) {
1290 struct fuse_file_info fi
;
1291 char *name
= PARAM(arg
);
1293 memset(&fi
, 0, sizeof(fi
));
1294 fi
.flags
= arg
->flags
;
1296 if (req
->f
->conn
.proto_minor
>= 12)
1297 req
->ctx
.umask
= arg
->umask
;
1299 name
= (char *) inarg
+ sizeof(struct fuse_open_in
);
1301 req
->f
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1303 fuse_reply_err(req
, ENOSYS
);
1306 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1308 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1309 struct fuse_file_info fi
;
1311 memset(&fi
, 0, sizeof(fi
));
1312 fi
.flags
= arg
->flags
;
1314 if (req
->f
->op
.open
)
1315 req
->f
->op
.open(req
, nodeid
, &fi
);
1317 fuse_reply_open(req
, &fi
);
1320 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1322 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1324 if (req
->f
->op
.read
) {
1325 struct fuse_file_info fi
;
1327 memset(&fi
, 0, sizeof(fi
));
1329 if (req
->f
->conn
.proto_minor
>= 9) {
1330 fi
.lock_owner
= arg
->lock_owner
;
1331 fi
.flags
= arg
->flags
;
1333 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1335 fuse_reply_err(req
, ENOSYS
);
1338 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1340 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1341 struct fuse_file_info fi
;
1344 memset(&fi
, 0, sizeof(fi
));
1346 fi
.writepage
= (arg
->write_flags
& 1) != 0;
1348 if (req
->f
->conn
.proto_minor
< 9) {
1349 param
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1351 fi
.lock_owner
= arg
->lock_owner
;
1352 fi
.flags
= arg
->flags
;
1356 if (req
->f
->op
.write
)
1357 req
->f
->op
.write(req
, nodeid
, param
, arg
->size
,
1360 fuse_reply_err(req
, ENOSYS
);
1363 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
,
1364 const struct fuse_buf
*ibuf
)
1366 struct fuse_ll
*f
= req
->f
;
1367 struct fuse_bufvec bufv
= {
1371 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1372 struct fuse_file_info fi
;
1374 memset(&fi
, 0, sizeof(fi
));
1376 fi
.writepage
= arg
->write_flags
& 1;
1378 if (req
->f
->conn
.proto_minor
< 9) {
1379 bufv
.buf
[0].mem
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1380 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1381 FUSE_COMPAT_WRITE_IN_SIZE
;
1382 assert(!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1384 fi
.lock_owner
= arg
->lock_owner
;
1385 fi
.flags
= arg
->flags
;
1386 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
1387 bufv
.buf
[0].mem
= PARAM(arg
);
1389 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1390 sizeof(struct fuse_write_in
);
1392 if (bufv
.buf
[0].size
< arg
->size
) {
1393 fprintf(stderr
, "fuse: do_write_buf: buffer size too small\n");
1394 fuse_reply_err(req
, EIO
);
1397 bufv
.buf
[0].size
= arg
->size
;
1399 req
->f
->op
.write_buf(req
, nodeid
, &bufv
, arg
->offset
, &fi
);
1402 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1403 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
1404 fuse_ll_clear_pipe(f
);
1407 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1409 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*) inarg
;
1410 struct fuse_file_info fi
;
1412 memset(&fi
, 0, sizeof(fi
));
1415 if (req
->f
->conn
.proto_minor
>= 7)
1416 fi
.lock_owner
= arg
->lock_owner
;
1418 if (req
->f
->op
.flush
)
1419 req
->f
->op
.flush(req
, nodeid
, &fi
);
1421 fuse_reply_err(req
, ENOSYS
);
1424 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1426 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1427 struct fuse_file_info fi
;
1429 memset(&fi
, 0, sizeof(fi
));
1430 fi
.flags
= arg
->flags
;
1432 if (req
->f
->conn
.proto_minor
>= 8) {
1433 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1434 fi
.lock_owner
= arg
->lock_owner
;
1436 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1437 fi
.flock_release
= 1;
1438 fi
.lock_owner
= arg
->lock_owner
;
1441 if (req
->f
->op
.release
)
1442 req
->f
->op
.release(req
, nodeid
, &fi
);
1444 fuse_reply_err(req
, 0);
1447 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1449 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1450 struct fuse_file_info fi
;
1452 memset(&fi
, 0, sizeof(fi
));
1455 if (req
->f
->op
.fsync
)
1456 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1458 fuse_reply_err(req
, ENOSYS
);
1461 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1463 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1464 struct fuse_file_info fi
;
1466 memset(&fi
, 0, sizeof(fi
));
1467 fi
.flags
= arg
->flags
;
1469 if (req
->f
->op
.opendir
)
1470 req
->f
->op
.opendir(req
, nodeid
, &fi
);
1472 fuse_reply_open(req
, &fi
);
1475 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1477 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1478 struct fuse_file_info fi
;
1480 memset(&fi
, 0, sizeof(fi
));
1483 if (req
->f
->op
.readdir
)
1484 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1486 fuse_reply_err(req
, ENOSYS
);
1489 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1491 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1492 struct fuse_file_info fi
;
1494 memset(&fi
, 0, sizeof(fi
));
1497 if (req
->f
->op
.readdirplus
)
1498 req
->f
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1500 fuse_reply_err(req
, ENOSYS
);
1503 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1505 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1506 struct fuse_file_info fi
;
1508 memset(&fi
, 0, sizeof(fi
));
1509 fi
.flags
= arg
->flags
;
1512 if (req
->f
->op
.releasedir
)
1513 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
1515 fuse_reply_err(req
, 0);
1518 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1520 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1521 struct fuse_file_info fi
;
1523 memset(&fi
, 0, sizeof(fi
));
1526 if (req
->f
->op
.fsyncdir
)
1527 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1529 fuse_reply_err(req
, ENOSYS
);
1532 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1537 if (req
->f
->op
.statfs
)
1538 req
->f
->op
.statfs(req
, nodeid
);
1540 struct statvfs buf
= {
1544 fuse_reply_statfs(req
, &buf
);
1548 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1550 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*) inarg
;
1551 char *name
= PARAM(arg
);
1552 char *value
= name
+ strlen(name
) + 1;
1554 if (req
->f
->op
.setxattr
)
1555 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
,
1558 fuse_reply_err(req
, ENOSYS
);
1561 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1563 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1565 if (req
->f
->op
.getxattr
)
1566 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
1568 fuse_reply_err(req
, ENOSYS
);
1571 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1573 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1575 if (req
->f
->op
.listxattr
)
1576 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
1578 fuse_reply_err(req
, ENOSYS
);
1581 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1583 char *name
= (char *) inarg
;
1585 if (req
->f
->op
.removexattr
)
1586 req
->f
->op
.removexattr(req
, nodeid
, name
);
1588 fuse_reply_err(req
, ENOSYS
);
1591 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1592 struct flock
*flock
)
1594 memset(flock
, 0, sizeof(struct flock
));
1595 flock
->l_type
= fl
->type
;
1596 flock
->l_whence
= SEEK_SET
;
1597 flock
->l_start
= fl
->start
;
1598 if (fl
->end
== OFFSET_MAX
)
1601 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1602 flock
->l_pid
= fl
->pid
;
1605 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1607 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1608 struct fuse_file_info fi
;
1611 memset(&fi
, 0, sizeof(fi
));
1613 fi
.lock_owner
= arg
->owner
;
1615 convert_fuse_file_lock(&arg
->lk
, &flock
);
1616 if (req
->f
->op
.getlk
)
1617 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1619 fuse_reply_err(req
, ENOSYS
);
1622 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1623 const void *inarg
, int sleep
)
1625 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1626 struct fuse_file_info fi
;
1629 memset(&fi
, 0, sizeof(fi
));
1631 fi
.lock_owner
= arg
->owner
;
1633 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1636 switch (arg
->lk
.type
) {
1650 if (req
->f
->op
.flock
)
1651 req
->f
->op
.flock(req
, nodeid
, &fi
, op
);
1653 fuse_reply_err(req
, ENOSYS
);
1655 convert_fuse_file_lock(&arg
->lk
, &flock
);
1656 if (req
->f
->op
.setlk
)
1657 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1659 fuse_reply_err(req
, ENOSYS
);
1663 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1665 do_setlk_common(req
, nodeid
, inarg
, 0);
1668 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1670 do_setlk_common(req
, nodeid
, inarg
, 1);
1673 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
1675 struct fuse_req
*curr
;
1677 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
1678 if (curr
->unique
== req
->u
.i
.unique
) {
1679 fuse_interrupt_func_t func
;
1683 pthread_mutex_unlock(&f
->lock
);
1685 /* Ugh, ugly locking */
1686 pthread_mutex_lock(&curr
->lock
);
1687 pthread_mutex_lock(&f
->lock
);
1688 curr
->interrupted
= 1;
1689 func
= curr
->u
.ni
.func
;
1690 data
= curr
->u
.ni
.data
;
1691 pthread_mutex_unlock(&f
->lock
);
1694 pthread_mutex_unlock(&curr
->lock
);
1696 pthread_mutex_lock(&f
->lock
);
1704 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1705 curr
= curr
->next
) {
1706 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
1712 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1714 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*) inarg
;
1715 struct fuse_ll
*f
= req
->f
;
1719 fprintf(stderr
, "INTERRUPT: %llu\n",
1720 (unsigned long long) arg
->unique
);
1722 req
->u
.i
.unique
= arg
->unique
;
1724 pthread_mutex_lock(&f
->lock
);
1725 if (find_interrupted(f
, req
))
1728 list_add_req(req
, &f
->interrupts
);
1729 pthread_mutex_unlock(&f
->lock
);
1732 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
1734 struct fuse_req
*curr
;
1736 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1737 curr
= curr
->next
) {
1738 if (curr
->u
.i
.unique
== req
->unique
) {
1739 req
->interrupted
= 1;
1745 curr
= f
->interrupts
.next
;
1746 if (curr
!= &f
->interrupts
) {
1748 list_init_req(curr
);
1754 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1756 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*) inarg
;
1758 if (req
->f
->op
.bmap
)
1759 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1761 fuse_reply_err(req
, ENOSYS
);
1764 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1766 struct fuse_ioctl_in
*arg
= (struct fuse_ioctl_in
*) inarg
;
1767 unsigned int flags
= arg
->flags
;
1768 void *in_buf
= arg
->in_size
? PARAM(arg
) : NULL
;
1769 struct fuse_file_info fi
;
1771 if (flags
& FUSE_IOCTL_DIR
&&
1772 !(req
->f
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1773 fuse_reply_err(req
, ENOTTY
);
1777 memset(&fi
, 0, sizeof(fi
));
1780 if (sizeof(void *) == 4 && req
->f
->conn
.proto_minor
>= 16 &&
1781 !(flags
& FUSE_IOCTL_32BIT
)) {
1782 req
->ioctl_64bit
= 1;
1785 if (req
->f
->op
.ioctl
)
1786 req
->f
->op
.ioctl(req
, nodeid
, arg
->cmd
,
1787 (void *)(uintptr_t)arg
->arg
, &fi
, flags
,
1788 in_buf
, arg
->in_size
, arg
->out_size
);
1790 fuse_reply_err(req
, ENOSYS
);
1793 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1798 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1800 struct fuse_poll_in
*arg
= (struct fuse_poll_in
*) inarg
;
1801 struct fuse_file_info fi
;
1803 memset(&fi
, 0, sizeof(fi
));
1805 fi
.poll_events
= arg
->events
;
1807 if (req
->f
->op
.poll
) {
1808 struct fuse_pollhandle
*ph
= NULL
;
1810 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1811 ph
= malloc(sizeof(struct fuse_pollhandle
));
1813 fuse_reply_err(req
, ENOMEM
);
1821 req
->f
->op
.poll(req
, nodeid
, &fi
, ph
);
1823 fuse_reply_err(req
, ENOSYS
);
1827 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1829 struct fuse_fallocate_in
*arg
= (struct fuse_fallocate_in
*) inarg
;
1830 struct fuse_file_info fi
;
1832 memset(&fi
, 0, sizeof(fi
));
1835 if (req
->f
->op
.fallocate
)
1836 req
->f
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
, &fi
);
1838 fuse_reply_err(req
, ENOSYS
);
1841 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1843 struct fuse_init_in
*arg
= (struct fuse_init_in
*) inarg
;
1844 struct fuse_init_out outarg
;
1845 struct fuse_ll
*f
= req
->f
;
1846 size_t bufsize
= f
->bufsize
;
1847 size_t outargsize
= sizeof(outarg
);
1851 fprintf(stderr
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1852 if (arg
->major
== 7 && arg
->minor
>= 6) {
1853 fprintf(stderr
, "flags=0x%08x\n", arg
->flags
);
1854 fprintf(stderr
, "max_readahead=0x%08x\n",
1855 arg
->max_readahead
);
1858 f
->conn
.proto_major
= arg
->major
;
1859 f
->conn
.proto_minor
= arg
->minor
;
1860 f
->conn
.capable
= 0;
1863 memset(&outarg
, 0, sizeof(outarg
));
1864 outarg
.major
= FUSE_KERNEL_VERSION
;
1865 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1867 if (arg
->major
< 7) {
1868 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
1869 arg
->major
, arg
->minor
);
1870 fuse_reply_err(req
, EPROTO
);
1874 if (arg
->major
> 7) {
1875 /* Wait for a second INIT request with a 7.X version */
1876 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1880 if (arg
->minor
>= 6) {
1881 if (f
->conn
.async_read
)
1882 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
1883 if (arg
->max_readahead
< f
->conn
.max_readahead
)
1884 f
->conn
.max_readahead
= arg
->max_readahead
;
1885 if (arg
->flags
& FUSE_ASYNC_READ
)
1886 f
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1887 if (arg
->flags
& FUSE_POSIX_LOCKS
)
1888 f
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1889 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
)
1890 f
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1891 if (arg
->flags
& FUSE_EXPORT_SUPPORT
)
1892 f
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1893 if (arg
->flags
& FUSE_BIG_WRITES
)
1894 f
->conn
.capable
|= FUSE_CAP_BIG_WRITES
;
1895 if (arg
->flags
& FUSE_DONT_MASK
)
1896 f
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1897 if (arg
->flags
& FUSE_FLOCK_LOCKS
)
1898 f
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1899 if (arg
->flags
& FUSE_AUTO_INVAL_DATA
)
1900 f
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
1901 if (arg
->flags
& FUSE_DO_READDIRPLUS
)
1902 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
1903 if (arg
->flags
& FUSE_READDIRPLUS_AUTO
)
1904 f
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
1905 if (arg
->flags
& FUSE_ASYNC_DIO
)
1906 f
->conn
.capable
|= FUSE_CAP_ASYNC_DIO
;
1907 if (arg
->flags
& FUSE_WRITEBACK_CACHE
)
1908 f
->conn
.capable
|= FUSE_CAP_WRITEBACK_CACHE
;
1910 f
->conn
.async_read
= 0;
1911 f
->conn
.max_readahead
= 0;
1914 if (req
->f
->conn
.proto_minor
>= 14) {
1916 #ifdef HAVE_VMSPLICE
1917 f
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1918 if (f
->splice_write
)
1919 f
->conn
.want
|= FUSE_CAP_SPLICE_WRITE
;
1921 f
->conn
.want
|= FUSE_CAP_SPLICE_MOVE
;
1923 f
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1925 f
->conn
.want
|= FUSE_CAP_SPLICE_READ
;
1928 if (req
->f
->conn
.proto_minor
>= 18)
1929 f
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
1931 if (f
->atomic_o_trunc
)
1932 f
->conn
.want
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1933 if (f
->op
.getlk
&& f
->op
.setlk
&& !f
->no_remote_posix_lock
)
1934 f
->conn
.want
|= FUSE_CAP_POSIX_LOCKS
;
1935 if (f
->op
.flock
&& !f
->no_remote_flock
)
1936 f
->conn
.want
|= FUSE_CAP_FLOCK_LOCKS
;
1938 f
->conn
.want
|= FUSE_CAP_BIG_WRITES
;
1939 if (f
->auto_inval_data
)
1940 f
->conn
.want
|= FUSE_CAP_AUTO_INVAL_DATA
;
1941 if (f
->op
.readdirplus
&& !f
->no_readdirplus
) {
1942 f
->conn
.want
|= FUSE_CAP_READDIRPLUS
;
1943 if (!f
->no_readdirplus_auto
)
1944 f
->conn
.want
|= FUSE_CAP_READDIRPLUS_AUTO
;
1947 f
->conn
.want
|= FUSE_CAP_ASYNC_DIO
;
1948 if (f
->writeback_cache
)
1949 f
->conn
.want
|= FUSE_CAP_WRITEBACK_CACHE
;
1951 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1952 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
1954 bufsize
= FUSE_MIN_READ_BUFFER
;
1958 if (bufsize
< f
->conn
.max_write
)
1959 f
->conn
.max_write
= bufsize
;
1963 f
->op
.init(f
->userdata
, &f
->conn
);
1965 if (f
->no_splice_read
)
1966 f
->conn
.want
&= ~FUSE_CAP_SPLICE_READ
;
1967 if (f
->no_splice_write
)
1968 f
->conn
.want
&= ~FUSE_CAP_SPLICE_WRITE
;
1969 if (f
->no_splice_move
)
1970 f
->conn
.want
&= ~FUSE_CAP_SPLICE_MOVE
;
1971 if (f
->no_auto_inval_data
)
1972 f
->conn
.want
&= ~FUSE_CAP_AUTO_INVAL_DATA
;
1973 if (f
->no_readdirplus
)
1974 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS
;
1975 if (f
->no_readdirplus_auto
)
1976 f
->conn
.want
&= ~FUSE_CAP_READDIRPLUS_AUTO
;
1977 if (f
->no_async_dio
)
1978 f
->conn
.want
&= ~FUSE_CAP_ASYNC_DIO
;
1979 if (f
->no_writeback_cache
)
1980 f
->conn
.want
&= ~FUSE_CAP_WRITEBACK_CACHE
;
1982 if (f
->conn
.async_read
|| (f
->conn
.want
& FUSE_CAP_ASYNC_READ
))
1983 outarg
.flags
|= FUSE_ASYNC_READ
;
1984 if (f
->conn
.want
& FUSE_CAP_POSIX_LOCKS
)
1985 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1986 if (f
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
)
1987 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
1988 if (f
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
)
1989 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
1990 if (f
->conn
.want
& FUSE_CAP_BIG_WRITES
)
1991 outarg
.flags
|= FUSE_BIG_WRITES
;
1992 if (f
->conn
.want
& FUSE_CAP_DONT_MASK
)
1993 outarg
.flags
|= FUSE_DONT_MASK
;
1994 if (f
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
)
1995 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
1996 if (f
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
)
1997 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
1998 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS
)
1999 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
2000 if (f
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
)
2001 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
2002 if (f
->conn
.want
& FUSE_CAP_ASYNC_DIO
)
2003 outarg
.flags
|= FUSE_ASYNC_DIO
;
2004 if (f
->conn
.want
& FUSE_CAP_WRITEBACK_CACHE
)
2005 outarg
.flags
|= FUSE_WRITEBACK_CACHE
;
2006 outarg
.max_readahead
= f
->conn
.max_readahead
;
2007 outarg
.max_write
= f
->conn
.max_write
;
2008 if (f
->conn
.proto_minor
>= 13) {
2009 if (f
->conn
.max_background
>= (1 << 16))
2010 f
->conn
.max_background
= (1 << 16) - 1;
2011 if (f
->conn
.congestion_threshold
> f
->conn
.max_background
)
2012 f
->conn
.congestion_threshold
= f
->conn
.max_background
;
2013 if (!f
->conn
.congestion_threshold
) {
2014 f
->conn
.congestion_threshold
=
2015 f
->conn
.max_background
* 3 / 4;
2018 outarg
.max_background
= f
->conn
.max_background
;
2019 outarg
.congestion_threshold
= f
->conn
.congestion_threshold
;
2021 if (f
->conn
.proto_minor
>= 23)
2022 outarg
.time_gran
= f
->conn
.time_gran
;
2025 fprintf(stderr
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
2026 fprintf(stderr
, " flags=0x%08x\n", outarg
.flags
);
2027 fprintf(stderr
, " max_readahead=0x%08x\n",
2028 outarg
.max_readahead
);
2029 fprintf(stderr
, " max_write=0x%08x\n", outarg
.max_write
);
2030 fprintf(stderr
, " max_background=%i\n",
2031 outarg
.max_background
);
2032 fprintf(stderr
, " congestion_threshold=%i\n",
2033 outarg
.congestion_threshold
);
2034 fprintf(stderr
, " time_gran=%u\n",
2038 outargsize
= FUSE_COMPAT_INIT_OUT_SIZE
;
2039 else if (arg
->minor
< 23)
2040 outargsize
= FUSE_COMPAT_22_INIT_OUT_SIZE
;
2042 send_reply_ok(req
, &outarg
, outargsize
);
2045 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
2047 struct fuse_ll
*f
= req
->f
;
2054 f
->op
.destroy(f
->userdata
);
2056 send_reply_ok(req
, NULL
, 0);
2059 static void list_del_nreq(struct fuse_notify_req
*nreq
)
2061 struct fuse_notify_req
*prev
= nreq
->prev
;
2062 struct fuse_notify_req
*next
= nreq
->next
;
2067 static void list_add_nreq(struct fuse_notify_req
*nreq
,
2068 struct fuse_notify_req
*next
)
2070 struct fuse_notify_req
*prev
= next
->prev
;
2077 static void list_init_nreq(struct fuse_notify_req
*nreq
)
2083 static void do_notify_reply(fuse_req_t req
, fuse_ino_t nodeid
,
2084 const void *inarg
, const struct fuse_buf
*buf
)
2086 struct fuse_ll
*f
= req
->f
;
2087 struct fuse_notify_req
*nreq
;
2088 struct fuse_notify_req
*head
;
2090 pthread_mutex_lock(&f
->lock
);
2091 head
= &f
->notify_list
;
2092 for (nreq
= head
->next
; nreq
!= head
; nreq
= nreq
->next
) {
2093 if (nreq
->unique
== req
->unique
) {
2094 list_del_nreq(nreq
);
2098 pthread_mutex_unlock(&f
->lock
);
2101 nreq
->reply(nreq
, req
, nodeid
, inarg
, buf
);
2104 static int send_notify_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
2105 int notify_code
, struct iovec
*iov
, int count
)
2107 struct fuse_out_header out
;
2113 out
.error
= notify_code
;
2114 iov
[0].iov_base
= &out
;
2115 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
2117 return fuse_send_msg(f
, ch
, iov
, count
);
2120 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
2123 struct fuse_notify_poll_wakeup_out outarg
;
2124 struct iovec iov
[2];
2128 iov
[1].iov_base
= &outarg
;
2129 iov
[1].iov_len
= sizeof(outarg
);
2131 return send_notify_iov(ph
->f
, ph
->ch
, FUSE_NOTIFY_POLL
, iov
, 2);
2137 int fuse_lowlevel_notify_inval_inode(struct fuse_chan
*ch
, fuse_ino_t ino
,
2138 off_t off
, off_t len
)
2140 struct fuse_notify_inval_inode_out outarg
;
2142 struct iovec iov
[2];
2147 f
= fuse_chan_session(ch
)->f
;
2155 iov
[1].iov_base
= &outarg
;
2156 iov
[1].iov_len
= sizeof(outarg
);
2158 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
2161 int fuse_lowlevel_notify_inval_entry(struct fuse_chan
*ch
, fuse_ino_t parent
,
2162 const char *name
, size_t namelen
)
2164 struct fuse_notify_inval_entry_out outarg
;
2166 struct iovec iov
[3];
2171 f
= fuse_chan_session(ch
)->f
;
2175 outarg
.parent
= parent
;
2176 outarg
.namelen
= namelen
;
2179 iov
[1].iov_base
= &outarg
;
2180 iov
[1].iov_len
= sizeof(outarg
);
2181 iov
[2].iov_base
= (void *)name
;
2182 iov
[2].iov_len
= namelen
+ 1;
2184 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
2187 int fuse_lowlevel_notify_delete(struct fuse_chan
*ch
,
2188 fuse_ino_t parent
, fuse_ino_t child
,
2189 const char *name
, size_t namelen
)
2191 struct fuse_notify_delete_out outarg
;
2193 struct iovec iov
[3];
2198 f
= fuse_chan_session(ch
)->f
;
2202 if (f
->conn
.proto_minor
< 18)
2205 outarg
.parent
= parent
;
2206 outarg
.child
= child
;
2207 outarg
.namelen
= namelen
;
2210 iov
[1].iov_base
= &outarg
;
2211 iov
[1].iov_len
= sizeof(outarg
);
2212 iov
[2].iov_base
= (void *)name
;
2213 iov
[2].iov_len
= namelen
+ 1;
2215 return send_notify_iov(f
, ch
, FUSE_NOTIFY_DELETE
, iov
, 3);
2218 int fuse_lowlevel_notify_store(struct fuse_chan
*ch
, fuse_ino_t ino
,
2219 off_t offset
, struct fuse_bufvec
*bufv
,
2220 enum fuse_buf_copy_flags flags
)
2222 struct fuse_out_header out
;
2223 struct fuse_notify_store_out outarg
;
2225 struct iovec iov
[3];
2226 size_t size
= fuse_buf_size(bufv
);
2232 f
= fuse_chan_session(ch
)->f
;
2236 if (f
->conn
.proto_minor
< 15)
2240 out
.error
= FUSE_NOTIFY_STORE
;
2242 outarg
.nodeid
= ino
;
2243 outarg
.offset
= offset
;
2246 iov
[0].iov_base
= &out
;
2247 iov
[0].iov_len
= sizeof(out
);
2248 iov
[1].iov_base
= &outarg
;
2249 iov
[1].iov_len
= sizeof(outarg
);
2251 res
= fuse_send_data_iov(f
, ch
, iov
, 2, bufv
, flags
);
2258 struct fuse_retrieve_req
{
2259 struct fuse_notify_req nreq
;
2263 static void fuse_ll_retrieve_reply(struct fuse_notify_req
*nreq
,
2264 fuse_req_t req
, fuse_ino_t ino
,
2266 const struct fuse_buf
*ibuf
)
2268 struct fuse_ll
*f
= req
->f
;
2269 struct fuse_retrieve_req
*rreq
=
2270 container_of(nreq
, struct fuse_retrieve_req
, nreq
);
2271 const struct fuse_notify_retrieve_in
*arg
= inarg
;
2272 struct fuse_bufvec bufv
= {
2277 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
2278 bufv
.buf
[0].mem
= PARAM(arg
);
2280 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
2281 sizeof(struct fuse_notify_retrieve_in
);
2283 if (bufv
.buf
[0].size
< arg
->size
) {
2284 fprintf(stderr
, "fuse: retrieve reply: buffer size too small\n");
2285 fuse_reply_none(req
);
2288 bufv
.buf
[0].size
= arg
->size
;
2290 if (req
->f
->op
.retrieve_reply
) {
2291 req
->f
->op
.retrieve_reply(req
, rreq
->cookie
, ino
,
2292 arg
->offset
, &bufv
);
2294 fuse_reply_none(req
);
2298 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
2299 fuse_ll_clear_pipe(f
);
2302 int fuse_lowlevel_notify_retrieve(struct fuse_chan
*ch
, fuse_ino_t ino
,
2303 size_t size
, off_t offset
, void *cookie
)
2305 struct fuse_notify_retrieve_out outarg
;
2307 struct iovec iov
[2];
2308 struct fuse_retrieve_req
*rreq
;
2314 f
= fuse_chan_session(ch
)->f
;
2318 if (f
->conn
.proto_minor
< 15)
2321 rreq
= malloc(sizeof(*rreq
));
2325 pthread_mutex_lock(&f
->lock
);
2326 rreq
->cookie
= cookie
;
2327 rreq
->nreq
.unique
= f
->notify_ctr
++;
2328 rreq
->nreq
.reply
= fuse_ll_retrieve_reply
;
2329 list_add_nreq(&rreq
->nreq
, &f
->notify_list
);
2330 pthread_mutex_unlock(&f
->lock
);
2332 outarg
.notify_unique
= rreq
->nreq
.unique
;
2333 outarg
.nodeid
= ino
;
2334 outarg
.offset
= offset
;
2337 iov
[1].iov_base
= &outarg
;
2338 iov
[1].iov_len
= sizeof(outarg
);
2340 err
= send_notify_iov(f
, ch
, FUSE_NOTIFY_RETRIEVE
, iov
, 2);
2342 pthread_mutex_lock(&f
->lock
);
2343 list_del_nreq(&rreq
->nreq
);
2344 pthread_mutex_unlock(&f
->lock
);
2351 void *fuse_req_userdata(fuse_req_t req
)
2353 return req
->f
->userdata
;
2356 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2361 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2364 pthread_mutex_lock(&req
->lock
);
2365 pthread_mutex_lock(&req
->f
->lock
);
2366 req
->u
.ni
.func
= func
;
2367 req
->u
.ni
.data
= data
;
2368 pthread_mutex_unlock(&req
->f
->lock
);
2369 if (req
->interrupted
&& func
)
2371 pthread_mutex_unlock(&req
->lock
);
2374 int fuse_req_interrupted(fuse_req_t req
)
2378 pthread_mutex_lock(&req
->f
->lock
);
2379 interrupted
= req
->interrupted
;
2380 pthread_mutex_unlock(&req
->f
->lock
);
2386 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
2389 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2390 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2391 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2392 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2393 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2394 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2395 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2396 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2397 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2398 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2399 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2400 [FUSE_LINK
] = { do_link
, "LINK" },
2401 [FUSE_OPEN
] = { do_open
, "OPEN" },
2402 [FUSE_READ
] = { do_read
, "READ" },
2403 [FUSE_WRITE
] = { do_write
, "WRITE" },
2404 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2405 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2406 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2407 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2408 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2409 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2410 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2411 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2412 [FUSE_INIT
] = { do_init
, "INIT" },
2413 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2414 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2415 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2416 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2417 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2418 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2419 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2420 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2421 [FUSE_CREATE
] = { do_create
, "CREATE" },
2422 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2423 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2424 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2425 [FUSE_POLL
] = { do_poll
, "POLL" },
2426 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
2427 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2428 [FUSE_NOTIFY_REPLY
] = { (void *) 1, "NOTIFY_REPLY" },
2429 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2430 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS"},
2431 [CUSE_INIT
] = { cuse_lowlevel_init
, "CUSE_INIT" },
2434 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2436 static const char *opname(enum fuse_opcode opcode
)
2438 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
2441 return fuse_ll_ops
[opcode
].name
;
2444 static int fuse_ll_copy_from_pipe(struct fuse_bufvec
*dst
,
2445 struct fuse_bufvec
*src
)
2447 int res
= fuse_buf_copy(dst
, src
, 0);
2449 fprintf(stderr
, "fuse: copy from pipe: %s\n", strerror(-res
));
2452 if (res
< fuse_buf_size(dst
)) {
2453 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2459 void fuse_session_process_buf(struct fuse_session
*se
,
2460 const struct fuse_buf
*buf
, struct fuse_chan
*ch
)
2462 struct fuse_ll
*f
= se
->f
;
2463 const size_t write_header_size
= sizeof(struct fuse_in_header
) +
2464 sizeof(struct fuse_write_in
);
2465 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2466 struct fuse_bufvec tmpbuf
= FUSE_BUFVEC_INIT(write_header_size
);
2467 struct fuse_in_header
*in
;
2469 struct fuse_req
*req
;
2474 if (buf
->flags
& FUSE_BUF_IS_FD
) {
2475 if (buf
->size
< tmpbuf
.buf
[0].size
)
2476 tmpbuf
.buf
[0].size
= buf
->size
;
2478 mbuf
= malloc(tmpbuf
.buf
[0].size
);
2480 fprintf(stderr
, "fuse: failed to allocate header\n");
2483 tmpbuf
.buf
[0].mem
= mbuf
;
2485 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2496 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2497 (unsigned long long) in
->unique
,
2498 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
2499 (unsigned long long) in
->nodeid
, buf
->size
, in
->pid
);
2502 req
= fuse_ll_alloc_req(f
);
2504 struct fuse_out_header out
= {
2505 .unique
= in
->unique
,
2508 struct iovec iov
= {
2510 .iov_len
= sizeof(struct fuse_out_header
),
2513 fuse_send_msg(f
, ch
, &iov
, 1);
2517 req
->unique
= in
->unique
;
2518 req
->ctx
.uid
= in
->uid
;
2519 req
->ctx
.gid
= in
->gid
;
2520 req
->ctx
.pid
= in
->pid
;
2525 enum fuse_opcode expected
;
2527 expected
= f
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2528 if (in
->opcode
!= expected
)
2530 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
)
2534 if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
2535 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2536 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2537 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2538 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2539 in
->opcode
!= FUSE_NOTIFY_REPLY
&&
2540 in
->opcode
!= FUSE_READDIRPLUS
)
2544 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
2546 if (in
->opcode
!= FUSE_INTERRUPT
) {
2547 struct fuse_req
*intr
;
2548 pthread_mutex_lock(&f
->lock
);
2549 intr
= check_interrupt(f
, req
);
2550 list_add_req(req
, &f
->list
);
2551 pthread_mutex_unlock(&f
->lock
);
2553 fuse_reply_err(intr
, EAGAIN
);
2556 if ((buf
->flags
& FUSE_BUF_IS_FD
) && write_header_size
< buf
->size
&&
2557 (in
->opcode
!= FUSE_WRITE
|| !f
->op
.write_buf
) &&
2558 in
->opcode
!= FUSE_NOTIFY_REPLY
) {
2562 newmbuf
= realloc(mbuf
, buf
->size
);
2563 if (newmbuf
== NULL
)
2567 tmpbuf
= FUSE_BUFVEC_INIT(buf
->size
- write_header_size
);
2568 tmpbuf
.buf
[0].mem
= mbuf
+ write_header_size
;
2570 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2578 inarg
= (void *) &in
[1];
2579 if (in
->opcode
== FUSE_WRITE
&& f
->op
.write_buf
)
2580 do_write_buf(req
, in
->nodeid
, inarg
, buf
);
2581 else if (in
->opcode
== FUSE_NOTIFY_REPLY
)
2582 do_notify_reply(req
, in
->nodeid
, inarg
, buf
);
2584 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
2591 fuse_reply_err(req
, err
);
2593 if (buf
->flags
& FUSE_BUF_IS_FD
)
2594 fuse_ll_clear_pipe(f
);
2603 static const struct fuse_opt fuse_ll_opts
[] = {
2604 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
2605 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
2606 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
2607 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
2608 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
2609 { "max_background=%u", offsetof(struct fuse_ll
, conn
.max_background
), 0 },
2610 { "congestion_threshold=%u",
2611 offsetof(struct fuse_ll
, conn
.congestion_threshold
), 0 },
2612 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
2613 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
2614 { "atomic_o_trunc", offsetof(struct fuse_ll
, atomic_o_trunc
), 1},
2615 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2616 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2617 { "no_remote_flock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2618 { "no_remote_posix_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2619 { "big_writes", offsetof(struct fuse_ll
, big_writes
), 1},
2620 { "splice_write", offsetof(struct fuse_ll
, splice_write
), 1},
2621 { "no_splice_write", offsetof(struct fuse_ll
, no_splice_write
), 1},
2622 { "splice_move", offsetof(struct fuse_ll
, splice_move
), 1},
2623 { "no_splice_move", offsetof(struct fuse_ll
, no_splice_move
), 1},
2624 { "splice_read", offsetof(struct fuse_ll
, splice_read
), 1},
2625 { "no_splice_read", offsetof(struct fuse_ll
, no_splice_read
), 1},
2626 { "auto_inval_data", offsetof(struct fuse_ll
, auto_inval_data
), 1},
2627 { "no_auto_inval_data", offsetof(struct fuse_ll
, no_auto_inval_data
), 1},
2628 { "readdirplus=no", offsetof(struct fuse_ll
, no_readdirplus
), 1},
2629 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2630 { "readdirplus=yes", offsetof(struct fuse_ll
, no_readdirplus_auto
), 1},
2631 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus
), 0},
2632 { "readdirplus=auto", offsetof(struct fuse_ll
, no_readdirplus_auto
), 0},
2633 { "async_dio", offsetof(struct fuse_ll
, async_dio
), 1},
2634 { "no_async_dio", offsetof(struct fuse_ll
, no_async_dio
), 1},
2635 { "writeback_cache", offsetof(struct fuse_ll
, writeback_cache
), 1},
2636 { "no_writeback_cache", offsetof(struct fuse_ll
, no_writeback_cache
), 1},
2637 { "time_gran=%u", offsetof(struct fuse_ll
, conn
.time_gran
), 0 },
2638 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
2639 FUSE_OPT_KEY("-h", KEY_HELP
),
2640 FUSE_OPT_KEY("--help", KEY_HELP
),
2641 FUSE_OPT_KEY("-V", KEY_VERSION
),
2642 FUSE_OPT_KEY("--version", KEY_VERSION
),
2646 static void fuse_ll_version(void)
2648 printf("using FUSE kernel interface version %i.%i\n",
2649 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
2652 static void fuse_ll_help(void)
2655 " -o max_write=N set maximum size of write requests\n"
2656 " -o max_readahead=N set maximum readahead\n"
2657 " -o max_background=N set number of maximum background requests\n"
2658 " -o congestion_threshold=N set kernel's congestion threshold\n"
2659 " -o async_read perform reads asynchronously (default)\n"
2660 " -o sync_read perform reads synchronously\n"
2661 " -o atomic_o_trunc enable atomic open+truncate support\n"
2662 " -o big_writes enable larger than 4kB writes\n"
2663 " -o no_remote_lock disable remote file locking\n"
2664 " -o no_remote_flock disable remote file locking (BSD)\n"
2665 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2666 " -o [no_]splice_write use splice to write to the fuse device\n"
2667 " -o [no_]splice_move move data while splicing to the fuse device\n"
2668 " -o [no_]splice_read use splice to read from the fuse device\n"
2669 " -o [no_]auto_inval_data use automatic kernel cache invalidation logic\n"
2670 " -o readdirplus=S control readdirplus use (yes|no|auto)\n"
2671 " -o [no_]async_dio asynchronous direct I/O\n"
2672 " -o [no_]writeback_cache asynchronous, buffered writes\n"
2673 " -o time_gran=N time granularity in nsec\n"
2677 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
2678 struct fuse_args
*outargs
)
2680 (void) data
; (void) outargs
;
2692 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
2698 static void fuse_ll_destroy(struct fuse_ll
*f
)
2700 struct fuse_ll_pipe
*llp
;
2702 if (f
->got_init
&& !f
->got_destroy
) {
2704 f
->op
.destroy(f
->userdata
);
2706 llp
= pthread_getspecific(f
->pipe_key
);
2708 fuse_ll_pipe_free(llp
);
2709 pthread_key_delete(f
->pipe_key
);
2710 pthread_mutex_destroy(&f
->lock
);
2715 void fuse_session_destroy(struct fuse_session
*se
)
2717 fuse_ll_destroy(se
->f
);
2719 fuse_chan_destroy(se
->ch
);
2724 static void fuse_ll_pipe_destructor(void *data
)
2726 struct fuse_ll_pipe
*llp
= data
;
2727 fuse_ll_pipe_free(llp
);
2731 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2732 struct fuse_chan
*ch
)
2734 struct fuse_ll
*f
= se
->f
;
2735 size_t bufsize
= f
->bufsize
;
2736 struct fuse_ll_pipe
*llp
;
2737 struct fuse_buf tmpbuf
;
2741 if (f
->conn
.proto_minor
< 14 || !(f
->conn
.want
& FUSE_CAP_SPLICE_READ
))
2744 llp
= fuse_ll_get_pipe(f
);
2748 if (llp
->size
< bufsize
) {
2749 if (llp
->can_grow
) {
2750 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, bufsize
);
2757 if (llp
->size
< bufsize
)
2761 res
= splice(fuse_chan_fd(ch
), NULL
, llp
->pipe
[1], NULL
, bufsize
, 0);
2764 if (fuse_session_exited(se
))
2768 if (err
== ENODEV
) {
2769 fuse_session_exit(se
);
2772 if (err
!= EINTR
&& err
!= EAGAIN
)
2773 perror("fuse: splice from device");
2777 if (res
< sizeof(struct fuse_in_header
)) {
2778 fprintf(stderr
, "short splice from fuse device\n");
2782 tmpbuf
= (struct fuse_buf
) {
2784 .flags
= FUSE_BUF_IS_FD
,
2789 * Don't bother with zero copy for small requests.
2790 * fuse_loop_mt() needs to check for FORGET so this more than
2791 * just an optimization.
2793 if (res
< sizeof(struct fuse_in_header
) +
2794 sizeof(struct fuse_write_in
) + pagesize
) {
2795 struct fuse_bufvec src
= { .buf
[0] = tmpbuf
, .count
= 1 };
2796 struct fuse_bufvec dst
= { .count
= 1 };
2799 buf
->mem
= malloc(f
->bufsize
);
2802 "fuse: failed to allocate read buffer\n");
2806 buf
->size
= f
->bufsize
;
2810 res
= fuse_buf_copy(&dst
, &src
, 0);
2812 fprintf(stderr
, "fuse: copy from pipe: %s\n",
2814 fuse_ll_clear_pipe(f
);
2817 if (res
< tmpbuf
.size
) {
2818 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2819 fuse_ll_clear_pipe(f
);
2822 assert(res
== tmpbuf
.size
);
2825 /* Don't overwrite buf->mem, as that would cause a leak */
2826 buf
->fd
= tmpbuf
.fd
;
2827 buf
->flags
= tmpbuf
.flags
;
2829 buf
->size
= tmpbuf
.size
;
2834 return fuse_chan_recv(se
, buf
, ch
);
2837 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2838 struct fuse_chan
*ch
)
2840 return fuse_chan_recv(se
, buf
, ch
);
2844 #define MIN_BUFSIZE 0x21000
2846 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
2847 const struct fuse_lowlevel_ops
*op
,
2848 size_t op_size
, void *userdata
)
2852 struct fuse_session
*se
;
2854 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2855 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
2856 op_size
= sizeof(struct fuse_lowlevel_ops
);
2859 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
2861 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
2865 f
->conn
.async_read
= 1;
2866 f
->conn
.max_write
= UINT_MAX
;
2867 f
->conn
.max_readahead
= UINT_MAX
;
2868 f
->atomic_o_trunc
= 0;
2869 f
->bufsize
= getpagesize() + 0x1000;
2870 f
->bufsize
= f
->bufsize
< MIN_BUFSIZE
? MIN_BUFSIZE
: f
->bufsize
;
2872 list_init_req(&f
->list
);
2873 list_init_req(&f
->interrupts
);
2874 list_init_nreq(&f
->notify_list
);
2876 fuse_mutex_init(&f
->lock
);
2878 err
= pthread_key_create(&f
->pipe_key
, fuse_ll_pipe_destructor
);
2880 fprintf(stderr
, "fuse: failed to create thread specific key: %s\n",
2885 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
2886 goto out_key_destroy
;
2889 fprintf(stderr
, "FUSE library version: %s\n", PACKAGE_VERSION
);
2891 memcpy(&f
->op
, op
, op_size
);
2892 f
->owner
= getuid();
2893 f
->userdata
= userdata
;
2895 se
= fuse_session_new();
2897 goto out_key_destroy
;
2904 pthread_key_delete(f
->pipe_key
);
2906 pthread_mutex_destroy(&f
->lock
);
2913 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2916 size_t bufsize
= 1024;
2920 unsigned long pid
= req
->ctx
.pid
;
2923 sprintf(path
, "/proc/%lu/task/%lu/status", pid
, pid
);
2926 buf
= malloc(bufsize
);
2931 fd
= open(path
, O_RDONLY
);
2935 ret
= read(fd
, buf
, bufsize
);
2942 if (ret
== bufsize
) {
2949 s
= strstr(buf
, "\nGroups:");
2957 unsigned long val
= strtoul(s
, &end
, 0);
2973 * This is currently not implemented on other than Linux...
2975 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])