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
12 #include "fuse_kernel.h"
14 #include "fuse_misc.h"
15 #include "fuse_common_compat.h"
16 #include "fuse_lowlevel_compat.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
);
158 static int fuse_send_msg(struct fuse_ll
*f
, struct fuse_chan
*ch
,
159 struct iovec
*iov
, int count
)
161 struct fuse_out_header
*out
= iov
[0].iov_base
;
163 out
->len
= iov_length(iov
, count
);
165 if (out
->unique
== 0) {
166 fprintf(stderr
, "NOTIFY: code=%d length=%u\n",
167 out
->error
, out
->len
);
168 } else if (out
->error
) {
170 " unique: %llu, error: %i (%s), outsize: %i\n",
171 (unsigned long long) out
->unique
, out
->error
,
172 strerror(-out
->error
), out
->len
);
175 " unique: %llu, success, outsize: %i\n",
176 (unsigned long long) out
->unique
, out
->len
);
180 return fuse_chan_send(ch
, iov
, count
);
183 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
186 struct fuse_out_header out
;
188 if (error
<= -1000 || error
> 0) {
189 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
193 out
.unique
= req
->unique
;
196 iov
[0].iov_base
= &out
;
197 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
199 return fuse_send_msg(req
->f
, req
->ch
, iov
, count
);
202 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
207 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
212 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
218 iov
[1].iov_base
= (void *) arg
;
219 iov
[1].iov_len
= argsize
;
222 return send_reply_iov(req
, error
, iov
, count
);
225 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
228 struct iovec
*padded_iov
;
230 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
231 if (padded_iov
== NULL
)
232 return fuse_reply_err(req
, ENOMEM
);
234 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
237 res
= send_reply_iov(req
, 0, padded_iov
, count
);
243 size_t fuse_dirent_size(size_t namelen
)
245 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
248 char *fuse_add_dirent(char *buf
, const char *name
, const struct stat
*stbuf
,
251 unsigned namelen
= strlen(name
);
252 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
253 unsigned entsize
= fuse_dirent_size(namelen
);
254 unsigned padlen
= entsize
- entlen
;
255 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
257 dirent
->ino
= stbuf
->st_ino
;
259 dirent
->namelen
= namelen
;
260 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
261 strncpy(dirent
->name
, name
, namelen
);
263 memset(buf
+ entlen
, 0, padlen
);
265 return buf
+ entsize
;
268 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
269 const char *name
, const struct stat
*stbuf
, off_t off
)
274 entsize
= fuse_dirent_size(strlen(name
));
275 if (entsize
<= bufsize
&& buf
)
276 fuse_add_dirent(buf
, name
, stbuf
, off
);
280 static void convert_statfs(const struct statvfs
*stbuf
,
281 struct fuse_kstatfs
*kstatfs
)
283 kstatfs
->bsize
= stbuf
->f_bsize
;
284 kstatfs
->frsize
= stbuf
->f_frsize
;
285 kstatfs
->blocks
= stbuf
->f_blocks
;
286 kstatfs
->bfree
= stbuf
->f_bfree
;
287 kstatfs
->bavail
= stbuf
->f_bavail
;
288 kstatfs
->files
= stbuf
->f_files
;
289 kstatfs
->ffree
= stbuf
->f_ffree
;
290 kstatfs
->namelen
= stbuf
->f_namemax
;
293 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
295 return send_reply(req
, 0, arg
, argsize
);
298 int fuse_reply_err(fuse_req_t req
, int err
)
300 return send_reply(req
, -err
, NULL
, 0);
303 void fuse_reply_none(fuse_req_t req
)
306 fuse_chan_send(req
->ch
, NULL
, 0);
310 static unsigned long calc_timeout_sec(double t
)
312 if (t
> (double) ULONG_MAX
)
317 return (unsigned long) t
;
320 static unsigned int calc_timeout_nsec(double t
)
322 double f
= t
- (double) calc_timeout_sec(t
);
325 else if (f
>= 0.999999999)
328 return (unsigned int) (f
* 1.0e9
);
331 static void fill_entry(struct fuse_entry_out
*arg
,
332 const struct fuse_entry_param
*e
)
334 arg
->nodeid
= e
->ino
;
335 arg
->generation
= e
->generation
;
336 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
337 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
338 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
339 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
340 convert_stat(&e
->attr
, &arg
->attr
);
343 static void fill_open(struct fuse_open_out
*arg
,
344 const struct fuse_file_info
*f
)
348 arg
->open_flags
|= FOPEN_DIRECT_IO
;
350 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
352 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
355 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
357 struct fuse_entry_out arg
;
358 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
359 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(arg
);
361 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
363 if (!e
->ino
&& req
->f
->conn
.proto_minor
< 4)
364 return fuse_reply_err(req
, ENOENT
);
366 memset(&arg
, 0, sizeof(arg
));
368 return send_reply_ok(req
, &arg
, size
);
371 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
372 const struct fuse_file_info
*f
)
374 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
375 size_t entrysize
= req
->f
->conn
.proto_minor
< 9 ?
376 FUSE_COMPAT_ENTRY_OUT_SIZE
: sizeof(struct fuse_entry_out
);
377 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*) buf
;
378 struct fuse_open_out
*oarg
= (struct fuse_open_out
*) (buf
+ entrysize
);
380 memset(buf
, 0, sizeof(buf
));
383 return send_reply_ok(req
, buf
,
384 entrysize
+ sizeof(struct fuse_open_out
));
387 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
390 struct fuse_attr_out arg
;
391 size_t size
= req
->f
->conn
.proto_minor
< 9 ?
392 FUSE_COMPAT_ATTR_OUT_SIZE
: sizeof(arg
);
394 memset(&arg
, 0, sizeof(arg
));
395 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
396 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
397 convert_stat(attr
, &arg
.attr
);
399 return send_reply_ok(req
, &arg
, size
);
402 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
404 return send_reply_ok(req
, linkname
, strlen(linkname
));
407 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
409 struct fuse_open_out arg
;
411 memset(&arg
, 0, sizeof(arg
));
413 return send_reply_ok(req
, &arg
, sizeof(arg
));
416 int fuse_reply_write(fuse_req_t req
, size_t count
)
418 struct fuse_write_out arg
;
420 memset(&arg
, 0, sizeof(arg
));
423 return send_reply_ok(req
, &arg
, sizeof(arg
));
426 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
428 return send_reply_ok(req
, buf
, size
);
431 static int fuse_send_data_iov_fallback(struct fuse_ll
*f
, struct fuse_chan
*ch
,
432 struct iovec
*iov
, int iov_count
,
433 struct fuse_bufvec
*buf
,
436 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
440 /* FIXME: Avoid memory copy if none of the buffers contain an fd */
441 res
= posix_memalign(&mbuf
, pagesize
, len
);
445 mem_buf
.buf
[0].mem
= mbuf
;
446 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
453 iov
[iov_count
].iov_base
= mbuf
;
454 iov
[iov_count
].iov_len
= len
;
456 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
462 struct fuse_ll_pipe
{
468 static void fuse_ll_pipe_free(struct fuse_ll_pipe
*llp
)
476 static struct fuse_ll_pipe
*fuse_ll_get_pipe(struct fuse_ll
*f
)
478 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
482 llp
= malloc(sizeof(struct fuse_ll_pipe
));
486 res
= pipe(llp
->pipe
);
492 if (fcntl(llp
->pipe
[0], F_SETFL
, O_NONBLOCK
) == -1 ||
493 fcntl(llp
->pipe
[1], F_SETFL
, O_NONBLOCK
) == -1) {
501 *the default size is 16 pages on linux
503 llp
->size
= pagesize
* 16;
506 pthread_setspecific(f
->pipe_key
, llp
);
513 static void fuse_ll_clear_pipe(struct fuse_ll
*f
)
515 struct fuse_ll_pipe
*llp
= pthread_getspecific(f
->pipe_key
);
517 pthread_setspecific(f
->pipe_key
, NULL
);
518 fuse_ll_pipe_free(llp
);
522 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
523 static int read_back(int fd
, char *buf
, size_t len
)
527 res
= read(fd
, buf
, len
);
529 fprintf(stderr
, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno
));
533 fprintf(stderr
, "fuse: internal error: short read back from pipe: %i from %zi\n", res
, len
);
539 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
540 struct iovec
*iov
, int iov_count
,
541 struct fuse_bufvec
*buf
, unsigned int flags
)
544 size_t len
= fuse_buf_size(buf
);
545 struct fuse_out_header
*out
= iov
[0].iov_base
;
546 struct fuse_ll_pipe
*llp
;
549 size_t total_fd_size
;
552 struct fuse_bufvec pipe_buf
= FUSE_BUFVEC_INIT(len
);
554 if (f
->broken_splice_nonblock
)
557 if (flags
& FUSE_BUF_NO_SPLICE
)
561 for (idx
= buf
->idx
; idx
< buf
->count
; idx
++) {
562 if (buf
->buf
[idx
].flags
& FUSE_BUF_IS_FD
) {
563 total_fd_size
= buf
->buf
[idx
].size
;
565 total_fd_size
-= buf
->off
;
568 if (total_fd_size
< 2 * pagesize
)
571 if (f
->conn
.proto_minor
< 14 ||
572 !(f
->conn
.want
& FUSE_CAP_SPLICE_WRITE
))
575 llp
= fuse_ll_get_pipe(f
);
580 headerlen
= iov_length(iov
, iov_count
);
582 out
->len
= headerlen
+ len
;
585 * Heuristic for the required pipe size, does not work if the
586 * source contains less than page size fragments
588 pipesize
= pagesize
* (iov_count
+ buf
->count
+ 1) + out
->len
;
590 if (llp
->size
< pipesize
) {
592 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, pipesize
);
599 if (llp
->size
< pipesize
)
604 res
= vmsplice(llp
->pipe
[1], iov
, iov_count
, SPLICE_F_NONBLOCK
);
608 if (res
!= headerlen
) {
610 fprintf(stderr
, "fuse: short vmsplice to pipe: %u/%zu\n", res
,
615 pipe_buf
.buf
[0].flags
= FUSE_BUF_IS_FD
;
616 pipe_buf
.buf
[0].fd
= llp
->pipe
[1];
618 res
= fuse_buf_copy(&pipe_buf
, buf
,
619 FUSE_BUF_FORCE_SPLICE
| FUSE_BUF_SPLICE_NONBLOCK
);
621 if (res
== -EAGAIN
|| res
== -EINVAL
) {
623 * Should only get EAGAIN on kernels with
624 * broken SPLICE_F_NONBLOCK support (<=
625 * 2.6.35) where this error or a short read is
626 * returned even if the pipe itself is not
629 * EINVAL might mean that splice can't handle
630 * this combination of input and output.
633 f
->broken_splice_nonblock
= 1;
635 pthread_setspecific(f
->pipe_key
, NULL
);
636 fuse_ll_pipe_free(llp
);
643 if (res
!= 0 && res
< len
) {
644 struct fuse_bufvec mem_buf
= FUSE_BUFVEC_INIT(len
);
646 size_t now_len
= res
;
648 * For regular files a short count is either
650 * 2) because of broken SPLICE_F_NONBLOCK (see above)
652 * For other inputs it's possible that we overflowed
653 * the pipe because of small buffer fragments.
656 res
= posix_memalign(&mbuf
, pagesize
, len
);
660 mem_buf
.buf
[0].mem
= mbuf
;
661 mem_buf
.off
= now_len
;
662 res
= fuse_buf_copy(&mem_buf
, buf
, 0);
665 size_t extra_len
= res
;
667 * Trickiest case: got more data. Need to get
668 * back the data from the pipe and then fall
669 * back to regular write.
671 tmpbuf
= malloc(headerlen
);
672 if (tmpbuf
== NULL
) {
677 res
= read_back(llp
->pipe
[0], tmpbuf
, headerlen
);
683 res
= read_back(llp
->pipe
[0], mbuf
, now_len
);
688 len
= now_len
+ extra_len
;
689 iov
[iov_count
].iov_base
= mbuf
;
690 iov
[iov_count
].iov_len
= len
;
692 res
= fuse_send_msg(f
, ch
, iov
, iov_count
);
700 out
->len
= headerlen
+ len
;
704 " unique: %llu, success, outsize: %i (splice)\n",
705 (unsigned long long) out
->unique
, out
->len
);
709 if ((flags
& FUSE_BUF_SPLICE_MOVE
) &&
710 (f
->conn
.want
& FUSE_CAP_SPLICE_MOVE
))
711 splice_flags
|= SPLICE_F_MOVE
;
713 res
= splice(llp
->pipe
[0], NULL
,
714 fuse_chan_fd(ch
), NULL
, out
->len
, splice_flags
);
717 perror("fuse: splice from pipe");
720 if (res
!= out
->len
) {
722 fprintf(stderr
, "fuse: short splice from pipe: %u/%u\n",
729 fuse_ll_clear_pipe(f
);
733 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
736 static int fuse_send_data_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
737 struct iovec
*iov
, int iov_count
,
738 struct fuse_bufvec
*buf
, unsigned int flags
)
740 size_t len
= fuse_buf_size(buf
);
743 return fuse_send_data_iov_fallback(f
, ch
, iov
, iov_count
, buf
, len
);
747 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
,
748 enum fuse_buf_copy_flags flags
)
751 struct fuse_out_header out
;
754 iov
[0].iov_base
= &out
;
755 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
757 out
.unique
= req
->unique
;
760 res
= fuse_send_data_iov(req
->f
, req
->ch
, iov
, 1, bufv
, flags
);
764 return fuse_reply_err(req
, res
);
767 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
769 struct fuse_statfs_out arg
;
770 size_t size
= req
->f
->conn
.proto_minor
< 4 ?
771 FUSE_COMPAT_STATFS_SIZE
: sizeof(arg
);
773 memset(&arg
, 0, sizeof(arg
));
774 convert_statfs(stbuf
, &arg
.st
);
776 return send_reply_ok(req
, &arg
, size
);
779 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
781 struct fuse_getxattr_out arg
;
783 memset(&arg
, 0, sizeof(arg
));
786 return send_reply_ok(req
, &arg
, sizeof(arg
));
789 int fuse_reply_lock(fuse_req_t req
, struct flock
*lock
)
791 struct fuse_lk_out arg
;
793 memset(&arg
, 0, sizeof(arg
));
794 arg
.lk
.type
= lock
->l_type
;
795 if (lock
->l_type
!= F_UNLCK
) {
796 arg
.lk
.start
= lock
->l_start
;
797 if (lock
->l_len
== 0)
798 arg
.lk
.end
= OFFSET_MAX
;
800 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
802 arg
.lk
.pid
= lock
->l_pid
;
803 return send_reply_ok(req
, &arg
, sizeof(arg
));
806 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
808 struct fuse_bmap_out arg
;
810 memset(&arg
, 0, sizeof(arg
));
813 return send_reply_ok(req
, &arg
, sizeof(arg
));
816 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
819 struct fuse_ioctl_iovec
*fiov
;
822 fiov
= malloc(sizeof(fiov
[0]) * count
);
826 for (i
= 0; i
< count
; i
++) {
827 fiov
[i
].base
= (uintptr_t) iov
[i
].iov_base
;
828 fiov
[i
].len
= iov
[i
].iov_len
;
834 int fuse_reply_ioctl_retry(fuse_req_t req
,
835 const struct iovec
*in_iov
, size_t in_count
,
836 const struct iovec
*out_iov
, size_t out_count
)
838 struct fuse_ioctl_out arg
;
839 struct fuse_ioctl_iovec
*in_fiov
= NULL
;
840 struct fuse_ioctl_iovec
*out_fiov
= NULL
;
845 memset(&arg
, 0, sizeof(arg
));
846 arg
.flags
|= FUSE_IOCTL_RETRY
;
847 arg
.in_iovs
= in_count
;
848 arg
.out_iovs
= out_count
;
849 iov
[count
].iov_base
= &arg
;
850 iov
[count
].iov_len
= sizeof(arg
);
853 if (req
->f
->conn
.proto_minor
< 16) {
855 iov
[count
].iov_base
= (void *)in_iov
;
856 iov
[count
].iov_len
= sizeof(in_iov
[0]) * in_count
;
861 iov
[count
].iov_base
= (void *)out_iov
;
862 iov
[count
].iov_len
= sizeof(out_iov
[0]) * out_count
;
866 /* Can't handle non-compat 64bit ioctls on 32bit */
867 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
868 res
= fuse_reply_err(req
, EINVAL
);
873 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
877 iov
[count
].iov_base
= (void *)in_fiov
;
878 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
882 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
886 iov
[count
].iov_base
= (void *)out_fiov
;
887 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
892 res
= send_reply_iov(req
, 0, iov
, count
);
900 res
= fuse_reply_err(req
, ENOMEM
);
904 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
906 struct fuse_ioctl_out arg
;
910 memset(&arg
, 0, sizeof(arg
));
912 iov
[count
].iov_base
= &arg
;
913 iov
[count
].iov_len
= sizeof(arg
);
917 iov
[count
].iov_base
= (char *) buf
;
918 iov
[count
].iov_len
= size
;
922 return send_reply_iov(req
, 0, iov
, count
);
925 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
928 struct iovec
*padded_iov
;
929 struct fuse_ioctl_out arg
;
932 padded_iov
= malloc((count
+ 2) * sizeof(struct iovec
));
933 if (padded_iov
== NULL
)
934 return fuse_reply_err(req
, ENOMEM
);
936 memset(&arg
, 0, sizeof(arg
));
938 padded_iov
[1].iov_base
= &arg
;
939 padded_iov
[1].iov_len
= sizeof(arg
);
941 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
943 res
= send_reply_iov(req
, 0, padded_iov
, count
+ 2);
949 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
951 struct fuse_poll_out arg
;
953 memset(&arg
, 0, sizeof(arg
));
954 arg
.revents
= revents
;
956 return send_reply_ok(req
, &arg
, sizeof(arg
));
959 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
961 char *name
= (char *) inarg
;
963 if (req
->f
->op
.lookup
)
964 req
->f
->op
.lookup(req
, nodeid
, name
);
966 fuse_reply_err(req
, ENOSYS
);
969 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
971 struct fuse_forget_in
*arg
= (struct fuse_forget_in
*) inarg
;
973 if (req
->f
->op
.forget
)
974 req
->f
->op
.forget(req
, nodeid
, arg
->nlookup
);
976 fuse_reply_none(req
);
979 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
982 struct fuse_batch_forget_in
*arg
= (void *) inarg
;
983 struct fuse_forget_one
*param
= (void *) PARAM(arg
);
988 if (req
->f
->op
.forget_multi
) {
989 req
->f
->op
.forget_multi(req
, arg
->count
,
990 (struct fuse_forget_data
*) param
);
991 } else if (req
->f
->op
.forget
) {
992 for (i
= 0; i
< arg
->count
; i
++) {
993 struct fuse_forget_one
*forget
= ¶m
[i
];
994 struct fuse_req
*dummy_req
;
996 dummy_req
= fuse_ll_alloc_req(req
->f
);
997 if (dummy_req
== NULL
)
1000 dummy_req
->unique
= req
->unique
;
1001 dummy_req
->ctx
= req
->ctx
;
1002 dummy_req
->ch
= NULL
;
1004 req
->f
->op
.forget(dummy_req
, forget
->nodeid
,
1007 fuse_reply_none(req
);
1009 fuse_reply_none(req
);
1013 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1015 struct fuse_file_info
*fip
= NULL
;
1016 struct fuse_file_info fi
;
1018 if (req
->f
->conn
.proto_minor
>= 9) {
1019 struct fuse_getattr_in
*arg
= (struct fuse_getattr_in
*) inarg
;
1021 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
1022 memset(&fi
, 0, sizeof(fi
));
1029 if (req
->f
->op
.getattr
)
1030 req
->f
->op
.getattr(req
, nodeid
, fip
);
1032 fuse_reply_err(req
, ENOSYS
);
1035 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1037 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*) inarg
;
1039 if (req
->f
->op
.setattr
) {
1040 struct fuse_file_info
*fi
= NULL
;
1041 struct fuse_file_info fi_store
;
1043 memset(&stbuf
, 0, sizeof(stbuf
));
1044 convert_attr(arg
, &stbuf
);
1045 if (arg
->valid
& FATTR_FH
) {
1046 arg
->valid
&= ~FATTR_FH
;
1047 memset(&fi_store
, 0, sizeof(fi_store
));
1050 fi
->fh_old
= fi
->fh
;
1053 FUSE_SET_ATTR_MODE
|
1056 FUSE_SET_ATTR_SIZE
|
1057 FUSE_SET_ATTR_ATIME
|
1058 FUSE_SET_ATTR_MTIME
|
1059 FUSE_SET_ATTR_ATIME_NOW
|
1060 FUSE_SET_ATTR_MTIME_NOW
;
1062 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
1064 fuse_reply_err(req
, ENOSYS
);
1067 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1069 struct fuse_access_in
*arg
= (struct fuse_access_in
*) inarg
;
1071 if (req
->f
->op
.access
)
1072 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
1074 fuse_reply_err(req
, ENOSYS
);
1077 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1081 if (req
->f
->op
.readlink
)
1082 req
->f
->op
.readlink(req
, nodeid
);
1084 fuse_reply_err(req
, ENOSYS
);
1087 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1089 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*) inarg
;
1090 char *name
= PARAM(arg
);
1092 if (req
->f
->conn
.proto_minor
>= 12)
1093 req
->ctx
.umask
= arg
->umask
;
1095 name
= (char *) inarg
+ FUSE_COMPAT_MKNOD_IN_SIZE
;
1097 if (req
->f
->op
.mknod
)
1098 req
->f
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
1100 fuse_reply_err(req
, ENOSYS
);
1103 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1105 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*) inarg
;
1107 if (req
->f
->conn
.proto_minor
>= 12)
1108 req
->ctx
.umask
= arg
->umask
;
1110 if (req
->f
->op
.mkdir
)
1111 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
1113 fuse_reply_err(req
, ENOSYS
);
1116 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1118 char *name
= (char *) inarg
;
1120 if (req
->f
->op
.unlink
)
1121 req
->f
->op
.unlink(req
, nodeid
, name
);
1123 fuse_reply_err(req
, ENOSYS
);
1126 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1128 char *name
= (char *) inarg
;
1130 if (req
->f
->op
.rmdir
)
1131 req
->f
->op
.rmdir(req
, nodeid
, name
);
1133 fuse_reply_err(req
, ENOSYS
);
1136 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1138 char *name
= (char *) inarg
;
1139 char *linkname
= ((char *) inarg
) + strlen((char *) inarg
) + 1;
1141 if (req
->f
->op
.symlink
)
1142 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
1144 fuse_reply_err(req
, ENOSYS
);
1147 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1149 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*) inarg
;
1150 char *oldname
= PARAM(arg
);
1151 char *newname
= oldname
+ strlen(oldname
) + 1;
1153 if (req
->f
->op
.rename
)
1154 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
1156 fuse_reply_err(req
, ENOSYS
);
1159 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1161 struct fuse_link_in
*arg
= (struct fuse_link_in
*) inarg
;
1163 if (req
->f
->op
.link
)
1164 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
1166 fuse_reply_err(req
, ENOSYS
);
1169 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1171 struct fuse_create_in
*arg
= (struct fuse_create_in
*) inarg
;
1173 if (req
->f
->op
.create
) {
1174 struct fuse_file_info fi
;
1175 char *name
= PARAM(arg
);
1177 memset(&fi
, 0, sizeof(fi
));
1178 fi
.flags
= arg
->flags
;
1180 if (req
->f
->conn
.proto_minor
>= 12)
1181 req
->ctx
.umask
= arg
->umask
;
1183 name
= (char *) inarg
+ sizeof(struct fuse_open_in
);
1185 req
->f
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1187 fuse_reply_err(req
, ENOSYS
);
1190 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1192 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1193 struct fuse_file_info fi
;
1195 memset(&fi
, 0, sizeof(fi
));
1196 fi
.flags
= arg
->flags
;
1198 if (req
->f
->op
.open
)
1199 req
->f
->op
.open(req
, nodeid
, &fi
);
1201 fuse_reply_open(req
, &fi
);
1204 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1206 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1208 if (req
->f
->op
.read
) {
1209 struct fuse_file_info fi
;
1211 memset(&fi
, 0, sizeof(fi
));
1214 if (req
->f
->conn
.proto_minor
>= 9) {
1215 fi
.lock_owner
= arg
->lock_owner
;
1216 fi
.flags
= arg
->flags
;
1218 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1220 fuse_reply_err(req
, ENOSYS
);
1223 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1225 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1226 struct fuse_file_info fi
;
1229 memset(&fi
, 0, sizeof(fi
));
1232 fi
.writepage
= arg
->write_flags
& 1;
1234 if (req
->f
->conn
.proto_minor
< 9) {
1235 param
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1237 fi
.lock_owner
= arg
->lock_owner
;
1238 fi
.flags
= arg
->flags
;
1242 if (req
->f
->op
.write
)
1243 req
->f
->op
.write(req
, nodeid
, param
, arg
->size
,
1246 fuse_reply_err(req
, ENOSYS
);
1249 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
,
1250 const struct fuse_buf
*ibuf
)
1252 struct fuse_ll
*f
= req
->f
;
1253 struct fuse_bufvec bufv
= {
1257 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
1258 struct fuse_file_info fi
;
1260 memset(&fi
, 0, sizeof(fi
));
1263 fi
.writepage
= arg
->write_flags
& 1;
1265 if (req
->f
->conn
.proto_minor
< 9) {
1266 bufv
.buf
[0].mem
= ((char *) arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
1267 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1268 FUSE_COMPAT_WRITE_IN_SIZE
;
1269 assert(!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1271 fi
.lock_owner
= arg
->lock_owner
;
1272 fi
.flags
= arg
->flags
;
1273 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
1274 bufv
.buf
[0].mem
= PARAM(arg
);
1276 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
1277 sizeof(struct fuse_write_in
);
1279 if (bufv
.buf
[0].size
< arg
->size
) {
1280 fprintf(stderr
, "fuse: do_write_buf: buffer size too small\n");
1281 fuse_reply_err(req
, EIO
);
1284 bufv
.buf
[0].size
= arg
->size
;
1286 req
->f
->op
.write_buf(req
, nodeid
, &bufv
, arg
->offset
, &fi
);
1289 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1290 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
1291 fuse_ll_clear_pipe(f
);
1294 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1296 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*) inarg
;
1297 struct fuse_file_info fi
;
1299 memset(&fi
, 0, sizeof(fi
));
1303 if (req
->f
->conn
.proto_minor
>= 7)
1304 fi
.lock_owner
= arg
->lock_owner
;
1306 if (req
->f
->op
.flush
)
1307 req
->f
->op
.flush(req
, nodeid
, &fi
);
1309 fuse_reply_err(req
, ENOSYS
);
1312 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1314 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1315 struct fuse_file_info fi
;
1317 memset(&fi
, 0, sizeof(fi
));
1318 fi
.flags
= arg
->flags
;
1321 if (req
->f
->conn
.proto_minor
>= 8) {
1322 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1323 fi
.lock_owner
= arg
->lock_owner
;
1325 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1326 fi
.flock_release
= 1;
1327 fi
.lock_owner
= arg
->lock_owner
;
1330 if (req
->f
->op
.release
)
1331 req
->f
->op
.release(req
, nodeid
, &fi
);
1333 fuse_reply_err(req
, 0);
1336 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1338 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1339 struct fuse_file_info fi
;
1341 memset(&fi
, 0, sizeof(fi
));
1345 if (req
->f
->op
.fsync
)
1346 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1348 fuse_reply_err(req
, ENOSYS
);
1351 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1353 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
1354 struct fuse_file_info fi
;
1356 memset(&fi
, 0, sizeof(fi
));
1357 fi
.flags
= arg
->flags
;
1359 if (req
->f
->op
.opendir
)
1360 req
->f
->op
.opendir(req
, nodeid
, &fi
);
1362 fuse_reply_open(req
, &fi
);
1365 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1367 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
1368 struct fuse_file_info fi
;
1370 memset(&fi
, 0, sizeof(fi
));
1374 if (req
->f
->op
.readdir
)
1375 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1377 fuse_reply_err(req
, ENOSYS
);
1380 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1382 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
1383 struct fuse_file_info fi
;
1385 memset(&fi
, 0, sizeof(fi
));
1386 fi
.flags
= arg
->flags
;
1390 if (req
->f
->op
.releasedir
)
1391 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
1393 fuse_reply_err(req
, 0);
1396 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1398 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
1399 struct fuse_file_info fi
;
1401 memset(&fi
, 0, sizeof(fi
));
1405 if (req
->f
->op
.fsyncdir
)
1406 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
1408 fuse_reply_err(req
, ENOSYS
);
1411 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1416 if (req
->f
->op
.statfs
)
1417 req
->f
->op
.statfs(req
, nodeid
);
1419 struct statvfs buf
= {
1423 fuse_reply_statfs(req
, &buf
);
1427 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1429 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*) inarg
;
1430 char *name
= PARAM(arg
);
1431 char *value
= name
+ strlen(name
) + 1;
1433 if (req
->f
->op
.setxattr
)
1434 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
,
1437 fuse_reply_err(req
, ENOSYS
);
1440 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1442 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1444 if (req
->f
->op
.getxattr
)
1445 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
1447 fuse_reply_err(req
, ENOSYS
);
1450 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1452 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
1454 if (req
->f
->op
.listxattr
)
1455 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
1457 fuse_reply_err(req
, ENOSYS
);
1460 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1462 char *name
= (char *) inarg
;
1464 if (req
->f
->op
.removexattr
)
1465 req
->f
->op
.removexattr(req
, nodeid
, name
);
1467 fuse_reply_err(req
, ENOSYS
);
1470 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1471 struct flock
*flock
)
1473 memset(flock
, 0, sizeof(struct flock
));
1474 flock
->l_type
= fl
->type
;
1475 flock
->l_whence
= SEEK_SET
;
1476 flock
->l_start
= fl
->start
;
1477 if (fl
->end
== OFFSET_MAX
)
1480 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1481 flock
->l_pid
= fl
->pid
;
1484 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1486 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1487 struct fuse_file_info fi
;
1490 memset(&fi
, 0, sizeof(fi
));
1492 fi
.lock_owner
= arg
->owner
;
1494 convert_fuse_file_lock(&arg
->lk
, &flock
);
1495 if (req
->f
->op
.getlk
)
1496 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1498 fuse_reply_err(req
, ENOSYS
);
1501 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1502 const void *inarg
, int sleep
)
1504 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
1505 struct fuse_file_info fi
;
1508 memset(&fi
, 0, sizeof(fi
));
1510 fi
.lock_owner
= arg
->owner
;
1512 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1515 switch (arg
->lk
.type
) {
1529 if (req
->f
->op
.flock
)
1530 req
->f
->op
.flock(req
, nodeid
, &fi
, op
);
1532 fuse_reply_err(req
, ENOSYS
);
1534 convert_fuse_file_lock(&arg
->lk
, &flock
);
1535 if (req
->f
->op
.setlk
)
1536 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1538 fuse_reply_err(req
, ENOSYS
);
1542 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1544 do_setlk_common(req
, nodeid
, inarg
, 0);
1547 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1549 do_setlk_common(req
, nodeid
, inarg
, 1);
1552 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
1554 struct fuse_req
*curr
;
1556 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
1557 if (curr
->unique
== req
->u
.i
.unique
) {
1558 fuse_interrupt_func_t func
;
1562 pthread_mutex_unlock(&f
->lock
);
1564 /* Ugh, ugly locking */
1565 pthread_mutex_lock(&curr
->lock
);
1566 pthread_mutex_lock(&f
->lock
);
1567 curr
->interrupted
= 1;
1568 func
= curr
->u
.ni
.func
;
1569 data
= curr
->u
.ni
.data
;
1570 pthread_mutex_unlock(&f
->lock
);
1573 pthread_mutex_unlock(&curr
->lock
);
1575 pthread_mutex_lock(&f
->lock
);
1583 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1584 curr
= curr
->next
) {
1585 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
1591 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1593 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*) inarg
;
1594 struct fuse_ll
*f
= req
->f
;
1598 fprintf(stderr
, "INTERRUPT: %llu\n",
1599 (unsigned long long) arg
->unique
);
1601 req
->u
.i
.unique
= arg
->unique
;
1603 pthread_mutex_lock(&f
->lock
);
1604 if (find_interrupted(f
, req
))
1607 list_add_req(req
, &f
->interrupts
);
1608 pthread_mutex_unlock(&f
->lock
);
1611 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
1613 struct fuse_req
*curr
;
1615 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
1616 curr
= curr
->next
) {
1617 if (curr
->u
.i
.unique
== req
->unique
) {
1618 req
->interrupted
= 1;
1624 curr
= f
->interrupts
.next
;
1625 if (curr
!= &f
->interrupts
) {
1627 list_init_req(curr
);
1633 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1635 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*) inarg
;
1637 if (req
->f
->op
.bmap
)
1638 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1640 fuse_reply_err(req
, ENOSYS
);
1643 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1645 struct fuse_ioctl_in
*arg
= (struct fuse_ioctl_in
*) inarg
;
1646 unsigned int flags
= arg
->flags
;
1647 void *in_buf
= arg
->in_size
? PARAM(arg
) : NULL
;
1648 struct fuse_file_info fi
;
1650 memset(&fi
, 0, sizeof(fi
));
1654 if (sizeof(void *) == 4 && req
->f
->conn
.proto_minor
>= 16 &&
1655 !(flags
& FUSE_IOCTL_32BIT
)) {
1656 req
->ioctl_64bit
= 1;
1659 if (req
->f
->op
.ioctl
)
1660 req
->f
->op
.ioctl(req
, nodeid
, arg
->cmd
,
1661 (void *)(uintptr_t)arg
->arg
, &fi
, flags
,
1662 in_buf
, arg
->in_size
, arg
->out_size
);
1664 fuse_reply_err(req
, ENOSYS
);
1667 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1672 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1674 struct fuse_poll_in
*arg
= (struct fuse_poll_in
*) inarg
;
1675 struct fuse_file_info fi
;
1677 memset(&fi
, 0, sizeof(fi
));
1681 if (req
->f
->op
.poll
) {
1682 struct fuse_pollhandle
*ph
= NULL
;
1684 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1685 ph
= malloc(sizeof(struct fuse_pollhandle
));
1687 fuse_reply_err(req
, ENOMEM
);
1695 req
->f
->op
.poll(req
, nodeid
, &fi
, ph
);
1697 fuse_reply_err(req
, ENOSYS
);
1701 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1703 struct fuse_init_in
*arg
= (struct fuse_init_in
*) inarg
;
1704 struct fuse_init_out outarg
;
1705 struct fuse_ll
*f
= req
->f
;
1706 size_t bufsize
= fuse_chan_bufsize(req
->ch
);
1710 fprintf(stderr
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1711 if (arg
->major
== 7 && arg
->minor
>= 6) {
1712 fprintf(stderr
, "flags=0x%08x\n", arg
->flags
);
1713 fprintf(stderr
, "max_readahead=0x%08x\n",
1714 arg
->max_readahead
);
1717 f
->conn
.proto_major
= arg
->major
;
1718 f
->conn
.proto_minor
= arg
->minor
;
1719 f
->conn
.capable
= 0;
1722 memset(&outarg
, 0, sizeof(outarg
));
1723 outarg
.major
= FUSE_KERNEL_VERSION
;
1724 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1726 if (arg
->major
< 7) {
1727 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
1728 arg
->major
, arg
->minor
);
1729 fuse_reply_err(req
, EPROTO
);
1733 if (arg
->major
> 7) {
1734 /* Wait for a second INIT request with a 7.X version */
1735 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1739 if (arg
->minor
>= 6) {
1740 if (f
->conn
.async_read
)
1741 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
1742 if (arg
->max_readahead
< f
->conn
.max_readahead
)
1743 f
->conn
.max_readahead
= arg
->max_readahead
;
1744 if (arg
->flags
& FUSE_ASYNC_READ
)
1745 f
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1746 if (arg
->flags
& FUSE_POSIX_LOCKS
)
1747 f
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1748 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
)
1749 f
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1750 if (arg
->flags
& FUSE_EXPORT_SUPPORT
)
1751 f
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1752 if (arg
->flags
& FUSE_BIG_WRITES
)
1753 f
->conn
.capable
|= FUSE_CAP_BIG_WRITES
;
1754 if (arg
->flags
& FUSE_DONT_MASK
)
1755 f
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1756 if (arg
->flags
& FUSE_FLOCK_LOCKS
)
1757 f
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1759 f
->conn
.async_read
= 0;
1760 f
->conn
.max_readahead
= 0;
1763 if (req
->f
->conn
.proto_minor
>= 14) {
1765 #ifdef HAVE_VMSPLICE
1766 f
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1767 if (f
->splice_write
)
1768 f
->conn
.want
|= FUSE_CAP_SPLICE_WRITE
;
1770 f
->conn
.want
|= FUSE_CAP_SPLICE_MOVE
;
1772 f
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1774 f
->conn
.want
|= FUSE_CAP_SPLICE_READ
;
1778 if (f
->atomic_o_trunc
)
1779 f
->conn
.want
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1780 if (f
->op
.getlk
&& f
->op
.setlk
&& !f
->no_remote_posix_lock
)
1781 f
->conn
.want
|= FUSE_CAP_POSIX_LOCKS
;
1782 if (f
->op
.flock
&& !f
->no_remote_flock
)
1783 f
->conn
.want
|= FUSE_CAP_FLOCK_LOCKS
;
1785 f
->conn
.want
|= FUSE_CAP_BIG_WRITES
;
1787 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1788 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
1790 bufsize
= FUSE_MIN_READ_BUFFER
;
1794 if (bufsize
< f
->conn
.max_write
)
1795 f
->conn
.max_write
= bufsize
;
1799 f
->op
.init(f
->userdata
, &f
->conn
);
1801 if (f
->no_splice_read
)
1802 f
->conn
.want
&= ~FUSE_CAP_SPLICE_READ
;
1803 if (f
->no_splice_write
)
1804 f
->conn
.want
&= ~FUSE_CAP_SPLICE_WRITE
;
1805 if (f
->no_splice_move
)
1806 f
->conn
.want
&= ~FUSE_CAP_SPLICE_MOVE
;
1808 if (f
->conn
.async_read
|| (f
->conn
.want
& FUSE_CAP_ASYNC_READ
))
1809 outarg
.flags
|= FUSE_ASYNC_READ
;
1810 if (f
->conn
.want
& FUSE_CAP_POSIX_LOCKS
)
1811 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1812 if (f
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
)
1813 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
1814 if (f
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
)
1815 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
1816 if (f
->conn
.want
& FUSE_CAP_BIG_WRITES
)
1817 outarg
.flags
|= FUSE_BIG_WRITES
;
1818 if (f
->conn
.want
& FUSE_CAP_DONT_MASK
)
1819 outarg
.flags
|= FUSE_DONT_MASK
;
1820 if (f
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
)
1821 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
1822 outarg
.max_readahead
= f
->conn
.max_readahead
;
1823 outarg
.max_write
= f
->conn
.max_write
;
1824 if (f
->conn
.proto_minor
>= 13) {
1825 if (f
->conn
.max_background
>= (1 << 16))
1826 f
->conn
.max_background
= (1 << 16) - 1;
1827 if (f
->conn
.congestion_threshold
> f
->conn
.max_background
)
1828 f
->conn
.congestion_threshold
= f
->conn
.max_background
;
1829 if (!f
->conn
.congestion_threshold
) {
1830 f
->conn
.congestion_threshold
=
1831 f
->conn
.max_background
* 3 / 4;
1834 outarg
.max_background
= f
->conn
.max_background
;
1835 outarg
.congestion_threshold
= f
->conn
.congestion_threshold
;
1839 fprintf(stderr
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
1840 fprintf(stderr
, " flags=0x%08x\n", outarg
.flags
);
1841 fprintf(stderr
, " max_readahead=0x%08x\n",
1842 outarg
.max_readahead
);
1843 fprintf(stderr
, " max_write=0x%08x\n", outarg
.max_write
);
1844 fprintf(stderr
, " max_background=%i\n",
1845 outarg
.max_background
);
1846 fprintf(stderr
, " congestion_threshold=%i\n",
1847 outarg
.congestion_threshold
);
1850 send_reply_ok(req
, &outarg
, arg
->minor
< 5 ? 8 : sizeof(outarg
));
1853 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1855 struct fuse_ll
*f
= req
->f
;
1862 f
->op
.destroy(f
->userdata
);
1864 send_reply_ok(req
, NULL
, 0);
1867 static void list_del_nreq(struct fuse_notify_req
*nreq
)
1869 struct fuse_notify_req
*prev
= nreq
->prev
;
1870 struct fuse_notify_req
*next
= nreq
->next
;
1875 static void list_add_nreq(struct fuse_notify_req
*nreq
,
1876 struct fuse_notify_req
*next
)
1878 struct fuse_notify_req
*prev
= next
->prev
;
1885 static void list_init_nreq(struct fuse_notify_req
*nreq
)
1891 static void do_notify_reply(fuse_req_t req
, fuse_ino_t nodeid
,
1892 const void *inarg
, const struct fuse_buf
*buf
)
1894 struct fuse_ll
*f
= req
->f
;
1895 struct fuse_notify_req
*nreq
;
1896 struct fuse_notify_req
*head
;
1898 pthread_mutex_lock(&f
->lock
);
1899 head
= &f
->notify_list
;
1900 for (nreq
= head
->next
; nreq
!= head
; nreq
= nreq
->next
) {
1901 if (nreq
->unique
== req
->unique
) {
1902 list_del_nreq(nreq
);
1906 pthread_mutex_unlock(&f
->lock
);
1909 nreq
->reply(nreq
, req
, nodeid
, inarg
, buf
);
1912 static int send_notify_iov(struct fuse_ll
*f
, struct fuse_chan
*ch
,
1913 int notify_code
, struct iovec
*iov
, int count
)
1915 struct fuse_out_header out
;
1921 out
.error
= notify_code
;
1922 iov
[0].iov_base
= &out
;
1923 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
1925 return fuse_send_msg(f
, ch
, iov
, count
);
1928 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
1931 struct fuse_notify_poll_wakeup_out outarg
;
1932 struct iovec iov
[2];
1936 iov
[1].iov_base
= &outarg
;
1937 iov
[1].iov_len
= sizeof(outarg
);
1939 return send_notify_iov(ph
->f
, ph
->ch
, FUSE_NOTIFY_POLL
, iov
, 2);
1945 int fuse_lowlevel_notify_inval_inode(struct fuse_chan
*ch
, fuse_ino_t ino
,
1946 off_t off
, off_t len
)
1948 struct fuse_notify_inval_inode_out outarg
;
1950 struct iovec iov
[2];
1955 f
= (struct fuse_ll
*)fuse_session_data(fuse_chan_session(ch
));
1963 iov
[1].iov_base
= &outarg
;
1964 iov
[1].iov_len
= sizeof(outarg
);
1966 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
1969 int fuse_lowlevel_notify_inval_entry(struct fuse_chan
*ch
, fuse_ino_t parent
,
1970 const char *name
, size_t namelen
)
1972 struct fuse_notify_inval_entry_out outarg
;
1974 struct iovec iov
[3];
1979 f
= (struct fuse_ll
*)fuse_session_data(fuse_chan_session(ch
));
1983 outarg
.parent
= parent
;
1984 outarg
.namelen
= namelen
;
1987 iov
[1].iov_base
= &outarg
;
1988 iov
[1].iov_len
= sizeof(outarg
);
1989 iov
[2].iov_base
= (void *)name
;
1990 iov
[2].iov_len
= namelen
+ 1;
1992 return send_notify_iov(f
, ch
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
1995 int fuse_lowlevel_notify_store(struct fuse_chan
*ch
, fuse_ino_t ino
,
1996 off_t offset
, struct fuse_bufvec
*bufv
,
1997 enum fuse_buf_copy_flags flags
)
1999 struct fuse_out_header out
;
2000 struct fuse_notify_store_out outarg
;
2002 struct iovec iov
[3];
2003 size_t size
= fuse_buf_size(bufv
);
2009 f
= (struct fuse_ll
*)fuse_session_data(fuse_chan_session(ch
));
2014 out
.error
= FUSE_NOTIFY_STORE
;
2016 outarg
.nodeid
= ino
;
2017 outarg
.offset
= offset
;
2020 iov
[0].iov_base
= &out
;
2021 iov
[0].iov_len
= sizeof(out
);
2022 iov
[1].iov_base
= &outarg
;
2023 iov
[1].iov_len
= sizeof(outarg
);
2025 res
= fuse_send_data_iov(f
, ch
, iov
, 2, bufv
, flags
);
2032 struct fuse_retrieve_req
{
2033 struct fuse_notify_req nreq
;
2037 static void fuse_ll_retrieve_reply(struct fuse_notify_req
*nreq
,
2038 fuse_req_t req
, fuse_ino_t ino
,
2040 const struct fuse_buf
*ibuf
)
2042 struct fuse_ll
*f
= req
->f
;
2043 struct fuse_retrieve_req
*rreq
=
2044 container_of(nreq
, struct fuse_retrieve_req
, nreq
);
2045 const struct fuse_notify_retrieve_in
*arg
= inarg
;
2046 struct fuse_bufvec bufv
= {
2051 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
))
2052 bufv
.buf
[0].mem
= PARAM(arg
);
2054 bufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) +
2055 sizeof(struct fuse_notify_retrieve_in
);
2057 if (bufv
.buf
[0].size
< arg
->size
) {
2058 fprintf(stderr
, "fuse: retrieve reply: buffer size too small\n");
2059 fuse_reply_none(req
);
2062 bufv
.buf
[0].size
= arg
->size
;
2064 if (req
->f
->op
.retrieve_reply
)
2065 req
->f
->op
.retrieve_reply(rreq
->cookie
, ino
, arg
->offset
, &bufv
);
2066 fuse_reply_none(req
);
2070 if ((ibuf
->flags
& FUSE_BUF_IS_FD
) && bufv
.idx
< bufv
.count
)
2071 fuse_ll_clear_pipe(f
);
2074 int fuse_lowlevel_notify_retrieve(struct fuse_chan
*ch
, fuse_ino_t ino
,
2075 size_t size
, off_t offset
, void *cookie
)
2077 struct fuse_notify_retrieve_out outarg
;
2079 struct iovec iov
[2];
2080 struct fuse_retrieve_req
*rreq
;
2086 f
= (struct fuse_ll
*)fuse_session_data(fuse_chan_session(ch
));
2090 rreq
= malloc(sizeof(*rreq
));
2094 pthread_mutex_lock(&f
->lock
);
2095 rreq
->cookie
= cookie
;
2096 rreq
->nreq
.unique
= f
->notify_ctr
++;
2097 rreq
->nreq
.reply
= fuse_ll_retrieve_reply
;
2098 list_add_nreq(&rreq
->nreq
, &f
->notify_list
);
2099 pthread_mutex_unlock(&f
->lock
);
2101 outarg
.notify_unique
= rreq
->nreq
.unique
;
2102 outarg
.nodeid
= ino
;
2103 outarg
.offset
= offset
;
2106 iov
[1].iov_base
= &outarg
;
2107 iov
[1].iov_len
= sizeof(outarg
);
2109 err
= send_notify_iov(f
, ch
, FUSE_NOTIFY_RETRIEVE
, iov
, 2);
2111 pthread_mutex_lock(&f
->lock
);
2112 list_del_nreq(&rreq
->nreq
);
2113 pthread_mutex_unlock(&f
->lock
);
2120 void *fuse_req_userdata(fuse_req_t req
)
2122 return req
->f
->userdata
;
2125 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2131 * The size of fuse_ctx got extended, so need to be careful about
2132 * incompatibility (i.e. a new binary cannot work with an old
2135 const struct fuse_ctx
*fuse_req_ctx_compat24(fuse_req_t req
);
2136 const struct fuse_ctx
*fuse_req_ctx_compat24(fuse_req_t req
)
2138 return fuse_req_ctx(req
);
2141 FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
2145 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2148 pthread_mutex_lock(&req
->lock
);
2149 pthread_mutex_lock(&req
->f
->lock
);
2150 req
->u
.ni
.func
= func
;
2151 req
->u
.ni
.data
= data
;
2152 pthread_mutex_unlock(&req
->f
->lock
);
2153 if (req
->interrupted
&& func
)
2155 pthread_mutex_unlock(&req
->lock
);
2158 int fuse_req_interrupted(fuse_req_t req
)
2162 pthread_mutex_lock(&req
->f
->lock
);
2163 interrupted
= req
->interrupted
;
2164 pthread_mutex_unlock(&req
->f
->lock
);
2170 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
2173 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2174 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2175 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2176 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2177 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2178 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2179 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2180 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2181 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2182 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2183 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2184 [FUSE_LINK
] = { do_link
, "LINK" },
2185 [FUSE_OPEN
] = { do_open
, "OPEN" },
2186 [FUSE_READ
] = { do_read
, "READ" },
2187 [FUSE_WRITE
] = { do_write
, "WRITE" },
2188 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2189 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2190 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2191 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2192 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2193 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2194 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2195 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2196 [FUSE_INIT
] = { do_init
, "INIT" },
2197 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2198 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2199 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2200 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2201 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2202 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2203 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2204 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2205 [FUSE_CREATE
] = { do_create
, "CREATE" },
2206 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2207 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2208 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2209 [FUSE_POLL
] = { do_poll
, "POLL" },
2210 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2211 [FUSE_NOTIFY_REPLY
] = { (void *) 1, "NOTIFY_REPLY" },
2212 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2213 [CUSE_INIT
] = { cuse_lowlevel_init
, "CUSE_INIT" },
2216 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2218 static const char *opname(enum fuse_opcode opcode
)
2220 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
2223 return fuse_ll_ops
[opcode
].name
;
2226 static int fuse_ll_copy_from_pipe(struct fuse_bufvec
*dst
,
2227 struct fuse_bufvec
*src
)
2229 int res
= fuse_buf_copy(dst
, src
, 0);
2231 fprintf(stderr
, "fuse: copy from pipe: %s\n", strerror(-res
));
2234 if (res
< fuse_buf_size(dst
)) {
2235 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2241 static void fuse_ll_process_buf(void *data
, const struct fuse_buf
*buf
,
2242 struct fuse_chan
*ch
)
2244 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
2245 const size_t write_header_size
= sizeof(struct fuse_in_header
) +
2246 sizeof(struct fuse_write_in
);
2247 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2248 struct fuse_bufvec tmpbuf
= FUSE_BUFVEC_INIT(write_header_size
);
2249 struct fuse_in_header
*in
;
2251 struct fuse_req
*req
;
2256 if (buf
->flags
& FUSE_BUF_IS_FD
) {
2257 if (buf
->size
< tmpbuf
.buf
[0].size
)
2258 tmpbuf
.buf
[0].size
= buf
->size
;
2260 mbuf
= malloc(tmpbuf
.buf
[0].size
);
2262 fprintf(stderr
, "fuse: failed to allocate header\n");
2265 tmpbuf
.buf
[0].mem
= mbuf
;
2267 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2276 req
= fuse_ll_alloc_req(f
);
2280 req
->unique
= in
->unique
;
2281 req
->ctx
.uid
= in
->uid
;
2282 req
->ctx
.gid
= in
->gid
;
2283 req
->ctx
.pid
= in
->pid
;
2288 "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
2289 (unsigned long long) in
->unique
,
2290 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
2291 (unsigned long) in
->nodeid
, buf
->size
, in
->pid
);
2296 enum fuse_opcode expected
;
2298 expected
= f
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2299 if (in
->opcode
!= expected
)
2301 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
)
2305 if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
2306 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2307 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2308 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2309 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2310 in
->opcode
!= FUSE_NOTIFY_REPLY
)
2314 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
2316 if (in
->opcode
!= FUSE_INTERRUPT
) {
2317 struct fuse_req
*intr
;
2318 pthread_mutex_lock(&f
->lock
);
2319 intr
= check_interrupt(f
, req
);
2320 list_add_req(req
, &f
->list
);
2321 pthread_mutex_unlock(&f
->lock
);
2323 fuse_reply_err(intr
, EAGAIN
);
2326 if ((buf
->flags
& FUSE_BUF_IS_FD
) && write_header_size
< buf
->size
&&
2327 (in
->opcode
!= FUSE_WRITE
|| !f
->op
.write_buf
) &&
2328 in
->opcode
!= FUSE_NOTIFY_REPLY
) {
2332 newmbuf
= realloc(mbuf
, buf
->size
);
2333 if (newmbuf
== NULL
)
2337 tmpbuf
= FUSE_BUFVEC_INIT(buf
->size
- write_header_size
);
2338 tmpbuf
.buf
[0].mem
= mbuf
+ write_header_size
;
2340 res
= fuse_ll_copy_from_pipe(&tmpbuf
, &bufv
);
2348 inarg
= (void *) &in
[1];
2349 if (in
->opcode
== FUSE_WRITE
&& f
->op
.write_buf
)
2350 do_write_buf(req
, in
->nodeid
, inarg
, buf
);
2351 else if (in
->opcode
== FUSE_NOTIFY_REPLY
)
2352 do_notify_reply(req
, in
->nodeid
, inarg
, buf
);
2354 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
2361 fuse_reply_err(req
, err
);
2363 if (buf
->flags
& FUSE_BUF_IS_FD
)
2364 fuse_ll_clear_pipe(f
);
2368 static void fuse_ll_process(void *data
, const char *buf
, size_t len
,
2369 struct fuse_chan
*ch
)
2371 struct fuse_buf fbuf
= {
2372 .mem
= (void *) buf
,
2376 fuse_ll_process_buf(data
, &fbuf
, ch
);
2384 static struct fuse_opt fuse_ll_opts
[] = {
2385 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
2386 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
2387 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
2388 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
2389 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
2390 { "max_background=%u", offsetof(struct fuse_ll
, conn
.max_background
), 0 },
2391 { "congestion_threshold=%u",
2392 offsetof(struct fuse_ll
, conn
.congestion_threshold
), 0 },
2393 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
2394 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
2395 { "atomic_o_trunc", offsetof(struct fuse_ll
, atomic_o_trunc
), 1},
2396 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2397 { "no_remote_lock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2398 { "no_remote_flock", offsetof(struct fuse_ll
, no_remote_flock
), 1},
2399 { "no_remote_posix_lock", offsetof(struct fuse_ll
, no_remote_posix_lock
), 1},
2400 { "big_writes", offsetof(struct fuse_ll
, big_writes
), 1},
2401 { "splice_write", offsetof(struct fuse_ll
, splice_write
), 1},
2402 { "no_splice_write", offsetof(struct fuse_ll
, no_splice_write
), 1},
2403 { "splice_move", offsetof(struct fuse_ll
, splice_move
), 1},
2404 { "no_splice_move", offsetof(struct fuse_ll
, no_splice_move
), 1},
2405 { "splice_read", offsetof(struct fuse_ll
, splice_read
), 1},
2406 { "no_splice_read", offsetof(struct fuse_ll
, no_splice_read
), 1},
2407 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
2408 FUSE_OPT_KEY("-h", KEY_HELP
),
2409 FUSE_OPT_KEY("--help", KEY_HELP
),
2410 FUSE_OPT_KEY("-V", KEY_VERSION
),
2411 FUSE_OPT_KEY("--version", KEY_VERSION
),
2415 static void fuse_ll_version(void)
2417 fprintf(stderr
, "using FUSE kernel interface version %i.%i\n",
2418 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
2421 static void fuse_ll_help(void)
2424 " -o max_write=N set maximum size of write requests\n"
2425 " -o max_readahead=N set maximum readahead\n"
2426 " -o max_background=N set number of maximum background requests\n"
2427 " -o congestion_threshold=N set kernel's congestion threshold\n"
2428 " -o async_read perform reads asynchronously (default)\n"
2429 " -o sync_read perform reads synchronously\n"
2430 " -o atomic_o_trunc enable atomic open+truncate support\n"
2431 " -o big_writes enable larger than 4kB writes\n"
2432 " -o no_remote_lock disable remote file locking\n"
2433 " -o no_remote_flock disable remote file locking (BSD)\n"
2434 " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
2435 " -o [no_]splice_write use splice to write to the fuse device\n"
2436 " -o [no_]splice_move move data while splicing to the fuse device\n"
2437 " -o [no_]splice_read use splice to read from the fuse device\n"
2441 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
2442 struct fuse_args
*outargs
)
2444 (void) data
; (void) outargs
;
2456 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
2462 int fuse_lowlevel_is_lib_option(const char *opt
)
2464 return fuse_opt_match(fuse_ll_opts
, opt
);
2467 static void fuse_ll_destroy(void *data
)
2469 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
2470 struct fuse_ll_pipe
*llp
;
2472 if (f
->got_init
&& !f
->got_destroy
) {
2474 f
->op
.destroy(f
->userdata
);
2476 llp
= pthread_getspecific(f
->pipe_key
);
2478 fuse_ll_pipe_free(llp
);
2479 pthread_key_delete(f
->pipe_key
);
2480 pthread_mutex_destroy(&f
->lock
);
2485 static void fuse_ll_pipe_destructor(void *data
)
2487 struct fuse_ll_pipe
*llp
= data
;
2488 fuse_ll_pipe_free(llp
);
2492 static int fuse_ll_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2493 struct fuse_chan
**chp
)
2495 struct fuse_chan
*ch
= *chp
;
2496 struct fuse_ll
*f
= fuse_session_data(se
);
2497 size_t bufsize
= buf
->size
;
2498 struct fuse_ll_pipe
*llp
;
2499 struct fuse_buf tmpbuf
;
2503 if (f
->conn
.proto_minor
< 14 || !(f
->conn
.want
& FUSE_CAP_SPLICE_READ
))
2506 llp
= fuse_ll_get_pipe(f
);
2510 if (llp
->size
< bufsize
) {
2511 if (llp
->can_grow
) {
2512 res
= fcntl(llp
->pipe
[0], F_SETPIPE_SZ
, bufsize
);
2519 if (llp
->size
< bufsize
)
2523 res
= splice(fuse_chan_fd(ch
), NULL
, llp
->pipe
[1], NULL
, bufsize
, 0);
2526 if (fuse_session_exited(se
))
2530 if (err
== ENODEV
) {
2531 fuse_session_exit(se
);
2534 if (err
!= EINTR
&& err
!= EAGAIN
)
2535 perror("fuse: splice from device");
2539 if (res
< sizeof(struct fuse_in_header
)) {
2540 fprintf(stderr
, "short splice from fuse device\n");
2544 tmpbuf
= (struct fuse_buf
) {
2546 .flags
= FUSE_BUF_IS_FD
,
2551 * Don't bother with zero copy for small requests.
2552 * fuse_loop_mt() needs to check for FORGET so this more than
2553 * just an optimization.
2555 if (res
< sizeof(struct fuse_in_header
) +
2556 sizeof(struct fuse_write_in
) + pagesize
) {
2557 struct fuse_bufvec src
= { .buf
[0] = tmpbuf
, .count
= 1 };
2558 struct fuse_bufvec dst
= { .buf
[0] = *buf
, .count
= 1 };
2560 res
= fuse_buf_copy(&dst
, &src
, 0);
2562 fprintf(stderr
, "fuse: copy from pipe: %s\n",
2564 fuse_ll_clear_pipe(f
);
2567 if (res
< tmpbuf
.size
) {
2568 fprintf(stderr
, "fuse: copy from pipe: short read\n");
2569 fuse_ll_clear_pipe(f
);
2572 buf
->size
= tmpbuf
.size
;
2581 res
= fuse_chan_recv(chp
, buf
->mem
, bufsize
);
2590 static int fuse_ll_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
2591 struct fuse_chan
**chp
)
2595 int res
= fuse_chan_recv(chp
, buf
->mem
, buf
->size
);
2607 * always call fuse_lowlevel_new_common() internally, to work around a
2608 * misfeature in the FreeBSD runtime linker, which links the old
2609 * version of a symbol to internal references.
2611 struct fuse_session
*fuse_lowlevel_new_common(struct fuse_args
*args
,
2612 const struct fuse_lowlevel_ops
*op
,
2613 size_t op_size
, void *userdata
)
2617 struct fuse_session
*se
;
2618 struct fuse_session_ops sop
= {
2619 .process
= fuse_ll_process
,
2620 .destroy
= fuse_ll_destroy
,
2623 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2624 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
2625 op_size
= sizeof(struct fuse_lowlevel_ops
);
2628 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
2630 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
2634 f
->conn
.async_read
= 1;
2635 f
->conn
.max_write
= UINT_MAX
;
2636 f
->conn
.max_readahead
= UINT_MAX
;
2637 f
->atomic_o_trunc
= 0;
2638 list_init_req(&f
->list
);
2639 list_init_req(&f
->interrupts
);
2640 list_init_nreq(&f
->notify_list
);
2642 fuse_mutex_init(&f
->lock
);
2644 err
= pthread_key_create(&f
->pipe_key
, fuse_ll_pipe_destructor
);
2646 fprintf(stderr
, "fuse: failed to create thread specific key: %s\n",
2651 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
2652 goto out_key_destroy
;
2655 fprintf(stderr
, "FUSE library version: %s\n", PACKAGE_VERSION
);
2657 memcpy(&f
->op
, op
, op_size
);
2658 f
->owner
= getuid();
2659 f
->userdata
= userdata
;
2661 se
= fuse_session_new(&sop
, f
);
2663 goto out_key_destroy
;
2665 se
->receive_buf
= fuse_ll_receive_buf
;
2666 se
->process_buf
= fuse_ll_process_buf
;
2671 pthread_key_delete(f
->pipe_key
);
2673 pthread_mutex_destroy(&f
->lock
);
2680 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
2681 const struct fuse_lowlevel_ops
*op
,
2682 size_t op_size
, void *userdata
)
2684 return fuse_lowlevel_new_common(args
, op
, op_size
, userdata
);
2688 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2691 size_t bufsize
= 1024;
2695 unsigned long pid
= req
->ctx
.pid
;
2698 sprintf(path
, "/proc/%lu/task/%lu/status", pid
, pid
);
2701 buf
= malloc(bufsize
);
2706 fd
= open(path
, O_RDONLY
);
2710 ret
= read(fd
, buf
, bufsize
);
2717 if (ret
== bufsize
) {
2724 s
= strstr(buf
, "\nGroups:");
2732 unsigned long val
= strtoul(s
, &end
, 0);
2748 * This is currently not implemented on other than Linux...
2750 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2756 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
2758 static void fill_open_compat(struct fuse_open_out
*arg
,
2759 const struct fuse_file_info_compat
*f
)
2763 arg
->open_flags
|= FOPEN_DIRECT_IO
;
2765 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
2768 static void convert_statfs_compat(const struct statfs
*compatbuf
,
2769 struct statvfs
*buf
)
2771 buf
->f_bsize
= compatbuf
->f_bsize
;
2772 buf
->f_blocks
= compatbuf
->f_blocks
;
2773 buf
->f_bfree
= compatbuf
->f_bfree
;
2774 buf
->f_bavail
= compatbuf
->f_bavail
;
2775 buf
->f_files
= compatbuf
->f_files
;
2776 buf
->f_ffree
= compatbuf
->f_ffree
;
2777 buf
->f_namemax
= compatbuf
->f_namelen
;
2780 int fuse_reply_open_compat(fuse_req_t req
,
2781 const struct fuse_file_info_compat
*f
)
2783 struct fuse_open_out arg
;
2785 memset(&arg
, 0, sizeof(arg
));
2786 fill_open_compat(&arg
, f
);
2787 return send_reply_ok(req
, &arg
, sizeof(arg
));
2790 int fuse_reply_statfs_compat(fuse_req_t req
, const struct statfs
*stbuf
)
2792 struct statvfs newbuf
;
2794 memset(&newbuf
, 0, sizeof(newbuf
));
2795 convert_statfs_compat(stbuf
, &newbuf
);
2797 return fuse_reply_statfs(req
, &newbuf
);
2800 struct fuse_session
*fuse_lowlevel_new_compat(const char *opts
,
2801 const struct fuse_lowlevel_ops_compat
*op
,
2802 size_t op_size
, void *userdata
)
2804 struct fuse_session
*se
;
2805 struct fuse_args args
= FUSE_ARGS_INIT(0, NULL
);
2808 (fuse_opt_add_arg(&args
, "") == -1 ||
2809 fuse_opt_add_arg(&args
, "-o") == -1 ||
2810 fuse_opt_add_arg(&args
, opts
) == -1)) {
2811 fuse_opt_free_args(&args
);
2814 se
= fuse_lowlevel_new(&args
, (const struct fuse_lowlevel_ops
*) op
,
2816 fuse_opt_free_args(&args
);
2821 struct fuse_ll_compat_conf
{
2826 static const struct fuse_opt fuse_ll_opts_compat
[] = {
2827 { "max_read=", offsetof(struct fuse_ll_compat_conf
, set_max_read
), 1 },
2828 { "max_read=%u", offsetof(struct fuse_ll_compat_conf
, max_read
), 0 },
2829 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP
),
2833 int fuse_sync_compat_args(struct fuse_args
*args
)
2835 struct fuse_ll_compat_conf conf
;
2837 memset(&conf
, 0, sizeof(conf
));
2838 if (fuse_opt_parse(args
, &conf
, fuse_ll_opts_compat
, NULL
) == -1)
2841 if (fuse_opt_insert_arg(args
, 1, "-osync_read"))
2844 if (conf
.set_max_read
) {
2847 sprintf(tmpbuf
, "-omax_readahead=%u", conf
.max_read
);
2848 if (fuse_opt_insert_arg(args
, 1, tmpbuf
) == -1)
2854 FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
2855 FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
2856 FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
2858 #else /* __FreeBSD__ || __NetBSD__ */
2860 int fuse_sync_compat_args(struct fuse_args
*args
)
2866 #endif /* __FreeBSD__ || __NetBSD__ */
2868 struct fuse_session
*fuse_lowlevel_new_compat25(struct fuse_args
*args
,
2869 const struct fuse_lowlevel_ops_compat25
*op
,
2870 size_t op_size
, void *userdata
)
2872 if (fuse_sync_compat_args(args
) == -1)
2875 return fuse_lowlevel_new_common(args
,
2876 (const struct fuse_lowlevel_ops
*) op
,
2880 FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");