2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 * Implementation of (most of) the low-level FUSE API. The session loop
6 * functions are implemented in separate files.
8 * This program can be distributed under the terms of the GNU LGPLv2.
9 * See the file COPYING.LIB
12 #include "qemu/osdep.h"
14 #include "standard-headers/linux/fuse.h"
15 #include "fuse_misc.h"
17 #include "fuse_virtio.h"
21 #define THREAD_POOL_SIZE 64
23 #define OFFSET_MAX 0x7fffffffffffffffLL
25 struct fuse_pollhandle
{
27 struct fuse_session
*se
;
30 static size_t pagesize
;
32 static __attribute__((constructor
)) void fuse_ll_init_pagesize(void)
34 pagesize
= getpagesize();
37 static void convert_stat(const struct stat
*stbuf
, struct fuse_attr
*attr
)
39 *attr
= (struct fuse_attr
){
41 .mode
= stbuf
->st_mode
,
42 .nlink
= stbuf
->st_nlink
,
45 .rdev
= stbuf
->st_rdev
,
46 .size
= stbuf
->st_size
,
47 .blksize
= stbuf
->st_blksize
,
48 .blocks
= stbuf
->st_blocks
,
49 .atime
= stbuf
->st_atime
,
50 .mtime
= stbuf
->st_mtime
,
51 .ctime
= stbuf
->st_ctime
,
52 .atimensec
= ST_ATIM_NSEC(stbuf
),
53 .mtimensec
= ST_MTIM_NSEC(stbuf
),
54 .ctimensec
= ST_CTIM_NSEC(stbuf
),
58 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
60 stbuf
->st_mode
= attr
->mode
;
61 stbuf
->st_uid
= attr
->uid
;
62 stbuf
->st_gid
= attr
->gid
;
63 stbuf
->st_size
= attr
->size
;
64 stbuf
->st_atime
= attr
->atime
;
65 stbuf
->st_mtime
= attr
->mtime
;
66 stbuf
->st_ctime
= attr
->ctime
;
67 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
68 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
69 ST_CTIM_NSEC_SET(stbuf
, attr
->ctimensec
);
72 static size_t iov_length(const struct iovec
*iov
, size_t count
)
77 for (seg
= 0; seg
< count
; seg
++) {
78 ret
+= iov
[seg
].iov_len
;
83 static void list_init_req(struct fuse_req
*req
)
89 static void list_del_req(struct fuse_req
*req
)
91 struct fuse_req
*prev
= req
->prev
;
92 struct fuse_req
*next
= req
->next
;
97 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
99 struct fuse_req
*prev
= next
->prev
;
106 static void destroy_req(fuse_req_t req
)
108 pthread_mutex_destroy(&req
->lock
);
112 void fuse_free_req(fuse_req_t req
)
115 struct fuse_session
*se
= req
->se
;
117 pthread_mutex_lock(&se
->lock
);
118 req
->u
.ni
.func
= NULL
;
119 req
->u
.ni
.data
= NULL
;
123 pthread_mutex_unlock(&se
->lock
);
129 static struct fuse_req
*fuse_ll_alloc_req(struct fuse_session
*se
)
131 struct fuse_req
*req
;
133 req
= (struct fuse_req
*)calloc(1, sizeof(struct fuse_req
));
135 fuse_log(FUSE_LOG_ERR
, "fuse: failed to allocate request\n");
140 fuse_mutex_init(&req
->lock
);
146 /* Send data. If *ch* is NULL, send via session master fd */
147 static int fuse_send_msg(struct fuse_session
*se
, struct fuse_chan
*ch
,
148 struct iovec
*iov
, int count
)
150 struct fuse_out_header
*out
= iov
[0].iov_base
;
152 out
->len
= iov_length(iov
, count
);
153 if (out
->unique
== 0) {
154 fuse_log(FUSE_LOG_DEBUG
, "NOTIFY: code=%d length=%u\n", out
->error
,
156 } else if (out
->error
) {
157 fuse_log(FUSE_LOG_DEBUG
,
158 " unique: %llu, error: %i (%s), outsize: %i\n",
159 (unsigned long long)out
->unique
, out
->error
,
160 strerror(-out
->error
), out
->len
);
162 fuse_log(FUSE_LOG_DEBUG
, " unique: %llu, success, outsize: %i\n",
163 (unsigned long long)out
->unique
, out
->len
);
166 if (fuse_lowlevel_is_virtio(se
)) {
167 return virtio_send_msg(se
, ch
, iov
, count
);
170 abort(); /* virtio should have taken it before here */
175 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
178 struct fuse_out_header out
= {
179 .unique
= req
->unique
,
183 if (error
<= -1000 || error
> 0) {
184 fuse_log(FUSE_LOG_ERR
, "fuse: bad error value: %i\n", error
);
188 iov
[0].iov_base
= &out
;
189 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
191 return fuse_send_msg(req
->se
, req
->ch
, iov
, count
);
194 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
199 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
204 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
210 iov
[1].iov_base
= (void *)arg
;
211 iov
[1].iov_len
= argsize
;
214 return send_reply_iov(req
, error
, iov
, count
);
217 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
220 struct iovec
*padded_iov
;
222 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
223 if (padded_iov
== NULL
) {
224 return fuse_reply_err(req
, ENOMEM
);
227 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
230 res
= send_reply_iov(req
, 0, padded_iov
, count
);
238 * 'buf` is allowed to be empty so that the proper size may be
239 * allocated by the caller
241 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
242 const char *name
, const struct stat
*stbuf
, off_t off
)
247 size_t entlen_padded
;
248 struct fuse_dirent
*dirent
;
250 namelen
= strlen(name
);
251 entlen
= FUSE_NAME_OFFSET
+ namelen
;
252 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
254 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
255 return entlen_padded
;
258 dirent
= (struct fuse_dirent
*)buf
;
259 dirent
->ino
= stbuf
->st_ino
;
261 dirent
->namelen
= namelen
;
262 dirent
->type
= (stbuf
->st_mode
& S_IFMT
) >> 12;
263 memcpy(dirent
->name
, name
, namelen
);
264 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
266 return entlen_padded
;
269 static void convert_statfs(const struct statvfs
*stbuf
,
270 struct fuse_kstatfs
*kstatfs
)
272 *kstatfs
= (struct fuse_kstatfs
){
273 .bsize
= stbuf
->f_bsize
,
274 .frsize
= stbuf
->f_frsize
,
275 .blocks
= stbuf
->f_blocks
,
276 .bfree
= stbuf
->f_bfree
,
277 .bavail
= stbuf
->f_bavail
,
278 .files
= stbuf
->f_files
,
279 .ffree
= stbuf
->f_ffree
,
280 .namelen
= stbuf
->f_namemax
,
284 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
286 return send_reply(req
, 0, arg
, argsize
);
289 int fuse_reply_err(fuse_req_t req
, int err
)
291 return send_reply(req
, -err
, NULL
, 0);
294 void fuse_reply_none(fuse_req_t req
)
299 static unsigned long calc_timeout_sec(double t
)
301 if (t
> (double)ULONG_MAX
) {
303 } else if (t
< 0.0) {
306 return (unsigned long)t
;
310 static unsigned int calc_timeout_nsec(double t
)
312 double f
= t
- (double)calc_timeout_sec(t
);
315 } else if (f
>= 0.999999999) {
318 return (unsigned int)(f
* 1.0e9
);
322 static void fill_entry(struct fuse_entry_out
*arg
,
323 const struct fuse_entry_param
*e
)
325 *arg
= (struct fuse_entry_out
){
327 .generation
= e
->generation
,
328 .entry_valid
= calc_timeout_sec(e
->entry_timeout
),
329 .entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
),
330 .attr_valid
= calc_timeout_sec(e
->attr_timeout
),
331 .attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
),
333 convert_stat(&e
->attr
, &arg
->attr
);
335 arg
->attr
.flags
= e
->attr_flags
;
339 * `buf` is allowed to be empty so that the proper size may be
340 * allocated by the caller
342 size_t fuse_add_direntry_plus(fuse_req_t req
, char *buf
, size_t bufsize
,
344 const struct fuse_entry_param
*e
, off_t off
)
349 size_t entlen_padded
;
351 namelen
= strlen(name
);
352 entlen
= FUSE_NAME_OFFSET_DIRENTPLUS
+ namelen
;
353 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
354 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
355 return entlen_padded
;
358 struct fuse_direntplus
*dp
= (struct fuse_direntplus
*)buf
;
359 memset(&dp
->entry_out
, 0, sizeof(dp
->entry_out
));
360 fill_entry(&dp
->entry_out
, e
);
362 struct fuse_dirent
*dirent
= &dp
->dirent
;
363 *dirent
= (struct fuse_dirent
){
364 .ino
= e
->attr
.st_ino
,
367 .type
= (e
->attr
.st_mode
& S_IFMT
) >> 12,
369 memcpy(dirent
->name
, name
, namelen
);
370 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
372 return entlen_padded
;
375 static void fill_open(struct fuse_open_out
*arg
, const struct fuse_file_info
*f
)
379 arg
->open_flags
|= FOPEN_DIRECT_IO
;
382 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
384 if (f
->cache_readdir
) {
385 arg
->open_flags
|= FOPEN_CACHE_DIR
;
387 if (f
->nonseekable
) {
388 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
392 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
394 struct fuse_entry_out arg
;
395 size_t size
= sizeof(arg
);
397 memset(&arg
, 0, sizeof(arg
));
399 return send_reply_ok(req
, &arg
, size
);
402 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
403 const struct fuse_file_info
*f
)
405 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
406 size_t entrysize
= sizeof(struct fuse_entry_out
);
407 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*)buf
;
408 struct fuse_open_out
*oarg
= (struct fuse_open_out
*)(buf
+ entrysize
);
410 memset(buf
, 0, sizeof(buf
));
413 return send_reply_ok(req
, buf
, entrysize
+ sizeof(struct fuse_open_out
));
416 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
419 struct fuse_attr_out arg
;
420 size_t size
= sizeof(arg
);
422 memset(&arg
, 0, sizeof(arg
));
423 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
424 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
425 convert_stat(attr
, &arg
.attr
);
427 return send_reply_ok(req
, &arg
, size
);
430 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
432 return send_reply_ok(req
, linkname
, strlen(linkname
));
435 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
437 struct fuse_open_out arg
;
439 memset(&arg
, 0, sizeof(arg
));
441 return send_reply_ok(req
, &arg
, sizeof(arg
));
444 int fuse_reply_write(fuse_req_t req
, size_t count
)
446 struct fuse_write_out arg
;
448 memset(&arg
, 0, sizeof(arg
));
451 return send_reply_ok(req
, &arg
, sizeof(arg
));
454 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
456 return send_reply_ok(req
, buf
, size
);
459 static int fuse_send_data_iov_fallback(struct fuse_session
*se
,
460 struct fuse_chan
*ch
, struct iovec
*iov
,
461 int iov_count
, struct fuse_bufvec
*buf
,
464 /* Optimize common case */
465 if (buf
->count
== 1 && buf
->idx
== 0 && buf
->off
== 0 &&
466 !(buf
->buf
[0].flags
& FUSE_BUF_IS_FD
)) {
468 * FIXME: also avoid memory copy if there are multiple buffers
469 * but none of them contain an fd
472 iov
[iov_count
].iov_base
= buf
->buf
[0].mem
;
473 iov
[iov_count
].iov_len
= len
;
475 return fuse_send_msg(se
, ch
, iov
, iov_count
);
478 if (fuse_lowlevel_is_virtio(se
) && buf
->count
== 1 &&
479 buf
->buf
[0].flags
== (FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
)) {
480 return virtio_send_data_iov(se
, ch
, iov
, iov_count
, buf
, len
);
483 abort(); /* Will have taken vhost path */
487 static int fuse_send_data_iov(struct fuse_session
*se
, struct fuse_chan
*ch
,
488 struct iovec
*iov
, int iov_count
,
489 struct fuse_bufvec
*buf
)
491 size_t len
= fuse_buf_size(buf
);
493 return fuse_send_data_iov_fallback(se
, ch
, iov
, iov_count
, buf
, len
);
496 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
)
499 struct fuse_out_header out
= {
500 .unique
= req
->unique
,
504 iov
[0].iov_base
= &out
;
505 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
507 res
= fuse_send_data_iov(req
->se
, req
->ch
, iov
, 1, bufv
);
512 return fuse_reply_err(req
, res
);
516 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
518 struct fuse_statfs_out arg
;
519 size_t size
= sizeof(arg
);
521 memset(&arg
, 0, sizeof(arg
));
522 convert_statfs(stbuf
, &arg
.st
);
524 return send_reply_ok(req
, &arg
, size
);
527 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
529 struct fuse_getxattr_out arg
;
531 memset(&arg
, 0, sizeof(arg
));
534 return send_reply_ok(req
, &arg
, sizeof(arg
));
537 int fuse_reply_lock(fuse_req_t req
, const struct flock
*lock
)
539 struct fuse_lk_out arg
;
541 memset(&arg
, 0, sizeof(arg
));
542 arg
.lk
.type
= lock
->l_type
;
543 if (lock
->l_type
!= F_UNLCK
) {
544 arg
.lk
.start
= lock
->l_start
;
545 if (lock
->l_len
== 0) {
546 arg
.lk
.end
= OFFSET_MAX
;
548 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
551 arg
.lk
.pid
= lock
->l_pid
;
552 return send_reply_ok(req
, &arg
, sizeof(arg
));
555 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
557 struct fuse_bmap_out arg
;
559 memset(&arg
, 0, sizeof(arg
));
562 return send_reply_ok(req
, &arg
, sizeof(arg
));
565 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
568 struct fuse_ioctl_iovec
*fiov
;
571 fiov
= malloc(sizeof(fiov
[0]) * count
);
576 for (i
= 0; i
< count
; i
++) {
577 fiov
[i
].base
= (uintptr_t)iov
[i
].iov_base
;
578 fiov
[i
].len
= iov
[i
].iov_len
;
584 int fuse_reply_ioctl_retry(fuse_req_t req
, const struct iovec
*in_iov
,
585 size_t in_count
, const struct iovec
*out_iov
,
588 struct fuse_ioctl_out arg
;
589 struct fuse_ioctl_iovec
*in_fiov
= NULL
;
590 struct fuse_ioctl_iovec
*out_fiov
= NULL
;
595 memset(&arg
, 0, sizeof(arg
));
596 arg
.flags
|= FUSE_IOCTL_RETRY
;
597 arg
.in_iovs
= in_count
;
598 arg
.out_iovs
= out_count
;
599 iov
[count
].iov_base
= &arg
;
600 iov
[count
].iov_len
= sizeof(arg
);
603 /* Can't handle non-compat 64bit ioctls on 32bit */
604 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
605 res
= fuse_reply_err(req
, EINVAL
);
610 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
615 iov
[count
].iov_base
= (void *)in_fiov
;
616 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
620 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
625 iov
[count
].iov_base
= (void *)out_fiov
;
626 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
630 res
= send_reply_iov(req
, 0, iov
, count
);
638 res
= fuse_reply_err(req
, ENOMEM
);
642 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
644 struct fuse_ioctl_out arg
;
648 memset(&arg
, 0, sizeof(arg
));
650 iov
[count
].iov_base
= &arg
;
651 iov
[count
].iov_len
= sizeof(arg
);
655 iov
[count
].iov_base
= (char *)buf
;
656 iov
[count
].iov_len
= size
;
660 return send_reply_iov(req
, 0, iov
, count
);
663 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
666 struct iovec
*padded_iov
;
667 struct fuse_ioctl_out arg
;
670 padded_iov
= malloc((count
+ 2) * sizeof(struct iovec
));
671 if (padded_iov
== NULL
) {
672 return fuse_reply_err(req
, ENOMEM
);
675 memset(&arg
, 0, sizeof(arg
));
677 padded_iov
[1].iov_base
= &arg
;
678 padded_iov
[1].iov_len
= sizeof(arg
);
680 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
682 res
= send_reply_iov(req
, 0, padded_iov
, count
+ 2);
688 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
690 struct fuse_poll_out arg
;
692 memset(&arg
, 0, sizeof(arg
));
693 arg
.revents
= revents
;
695 return send_reply_ok(req
, &arg
, sizeof(arg
));
698 int fuse_reply_lseek(fuse_req_t req
, off_t off
)
700 struct fuse_lseek_out arg
;
702 memset(&arg
, 0, sizeof(arg
));
705 return send_reply_ok(req
, &arg
, sizeof(arg
));
708 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
,
709 struct fuse_mbuf_iter
*iter
)
711 const char *name
= fuse_mbuf_iter_advance_str(iter
);
713 fuse_reply_err(req
, EINVAL
);
717 if (req
->se
->op
.lookup
) {
718 req
->se
->op
.lookup(req
, nodeid
, name
);
720 fuse_reply_err(req
, ENOSYS
);
724 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
,
725 struct fuse_mbuf_iter
*iter
)
727 struct fuse_forget_in
*arg
;
729 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
731 fuse_reply_err(req
, EINVAL
);
735 if (req
->se
->op
.forget
) {
736 req
->se
->op
.forget(req
, nodeid
, arg
->nlookup
);
738 fuse_reply_none(req
);
742 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
743 struct fuse_mbuf_iter
*iter
)
745 struct fuse_batch_forget_in
*arg
;
746 struct fuse_forget_data
*forgets
;
751 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
753 fuse_reply_none(req
);
758 * Prevent integer overflow. The compiler emits the following warning
759 * unless we use the scount local variable:
761 * error: comparison is always false due to limited range of data type
762 * [-Werror=type-limits]
764 * This may be true on 64-bit hosts but we need this check for 32-bit
768 if (scount
> SIZE_MAX
/ sizeof(forgets
[0])) {
769 fuse_reply_none(req
);
773 forgets
= fuse_mbuf_iter_advance(iter
, arg
->count
* sizeof(forgets
[0]));
775 fuse_reply_none(req
);
779 if (req
->se
->op
.forget_multi
) {
780 req
->se
->op
.forget_multi(req
, arg
->count
, forgets
);
781 } else if (req
->se
->op
.forget
) {
784 for (i
= 0; i
< arg
->count
; i
++) {
785 struct fuse_req
*dummy_req
;
787 dummy_req
= fuse_ll_alloc_req(req
->se
);
788 if (dummy_req
== NULL
) {
792 dummy_req
->unique
= req
->unique
;
793 dummy_req
->ctx
= req
->ctx
;
794 dummy_req
->ch
= NULL
;
796 req
->se
->op
.forget(dummy_req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
798 fuse_reply_none(req
);
800 fuse_reply_none(req
);
804 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
,
805 struct fuse_mbuf_iter
*iter
)
807 struct fuse_file_info
*fip
= NULL
;
808 struct fuse_file_info fi
;
810 struct fuse_getattr_in
*arg
;
812 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
814 fuse_reply_err(req
, EINVAL
);
818 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
819 memset(&fi
, 0, sizeof(fi
));
824 if (req
->se
->op
.getattr
) {
825 req
->se
->op
.getattr(req
, nodeid
, fip
);
827 fuse_reply_err(req
, ENOSYS
);
831 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
,
832 struct fuse_mbuf_iter
*iter
)
834 if (req
->se
->op
.setattr
) {
835 struct fuse_setattr_in
*arg
;
836 struct fuse_file_info
*fi
= NULL
;
837 struct fuse_file_info fi_store
;
840 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
842 fuse_reply_err(req
, EINVAL
);
846 memset(&stbuf
, 0, sizeof(stbuf
));
847 convert_attr(arg
, &stbuf
);
848 if (arg
->valid
& FATTR_FH
) {
849 arg
->valid
&= ~FATTR_FH
;
850 memset(&fi_store
, 0, sizeof(fi_store
));
854 arg
->valid
&= FUSE_SET_ATTR_MODE
| FUSE_SET_ATTR_UID
|
855 FUSE_SET_ATTR_GID
| FUSE_SET_ATTR_SIZE
|
856 FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
|
857 FUSE_SET_ATTR_ATIME_NOW
| FUSE_SET_ATTR_MTIME_NOW
|
860 req
->se
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
862 fuse_reply_err(req
, ENOSYS
);
866 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
,
867 struct fuse_mbuf_iter
*iter
)
869 struct fuse_access_in
*arg
;
871 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
873 fuse_reply_err(req
, EINVAL
);
877 if (req
->se
->op
.access
) {
878 req
->se
->op
.access(req
, nodeid
, arg
->mask
);
880 fuse_reply_err(req
, ENOSYS
);
884 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
,
885 struct fuse_mbuf_iter
*iter
)
889 if (req
->se
->op
.readlink
) {
890 req
->se
->op
.readlink(req
, nodeid
);
892 fuse_reply_err(req
, ENOSYS
);
896 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
,
897 struct fuse_mbuf_iter
*iter
)
899 struct fuse_mknod_in
*arg
;
902 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
903 name
= fuse_mbuf_iter_advance_str(iter
);
905 fuse_reply_err(req
, EINVAL
);
909 req
->ctx
.umask
= arg
->umask
;
911 if (req
->se
->op
.mknod
) {
912 req
->se
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
914 fuse_reply_err(req
, ENOSYS
);
918 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
,
919 struct fuse_mbuf_iter
*iter
)
921 struct fuse_mkdir_in
*arg
;
924 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
925 name
= fuse_mbuf_iter_advance_str(iter
);
927 fuse_reply_err(req
, EINVAL
);
931 req
->ctx
.umask
= arg
->umask
;
933 if (req
->se
->op
.mkdir
) {
934 req
->se
->op
.mkdir(req
, nodeid
, name
, arg
->mode
);
936 fuse_reply_err(req
, ENOSYS
);
940 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
,
941 struct fuse_mbuf_iter
*iter
)
943 const char *name
= fuse_mbuf_iter_advance_str(iter
);
946 fuse_reply_err(req
, EINVAL
);
950 if (req
->se
->op
.unlink
) {
951 req
->se
->op
.unlink(req
, nodeid
, name
);
953 fuse_reply_err(req
, ENOSYS
);
957 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
,
958 struct fuse_mbuf_iter
*iter
)
960 const char *name
= fuse_mbuf_iter_advance_str(iter
);
963 fuse_reply_err(req
, EINVAL
);
967 if (req
->se
->op
.rmdir
) {
968 req
->se
->op
.rmdir(req
, nodeid
, name
);
970 fuse_reply_err(req
, ENOSYS
);
974 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
,
975 struct fuse_mbuf_iter
*iter
)
977 const char *name
= fuse_mbuf_iter_advance_str(iter
);
978 const char *linkname
= fuse_mbuf_iter_advance_str(iter
);
980 if (!name
|| !linkname
) {
981 fuse_reply_err(req
, EINVAL
);
985 if (req
->se
->op
.symlink
) {
986 req
->se
->op
.symlink(req
, linkname
, nodeid
, name
);
988 fuse_reply_err(req
, ENOSYS
);
992 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
,
993 struct fuse_mbuf_iter
*iter
)
995 struct fuse_rename_in
*arg
;
999 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1000 oldname
= fuse_mbuf_iter_advance_str(iter
);
1001 newname
= fuse_mbuf_iter_advance_str(iter
);
1002 if (!arg
|| !oldname
|| !newname
) {
1003 fuse_reply_err(req
, EINVAL
);
1007 if (req
->se
->op
.rename
) {
1008 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
, 0);
1010 fuse_reply_err(req
, ENOSYS
);
1014 static void do_rename2(fuse_req_t req
, fuse_ino_t nodeid
,
1015 struct fuse_mbuf_iter
*iter
)
1017 struct fuse_rename2_in
*arg
;
1018 const char *oldname
;
1019 const char *newname
;
1021 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1022 oldname
= fuse_mbuf_iter_advance_str(iter
);
1023 newname
= fuse_mbuf_iter_advance_str(iter
);
1024 if (!arg
|| !oldname
|| !newname
) {
1025 fuse_reply_err(req
, EINVAL
);
1029 if (req
->se
->op
.rename
) {
1030 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
,
1033 fuse_reply_err(req
, ENOSYS
);
1037 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
,
1038 struct fuse_mbuf_iter
*iter
)
1040 struct fuse_link_in
*arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1041 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1043 if (!arg
|| !name
) {
1044 fuse_reply_err(req
, EINVAL
);
1048 if (req
->se
->op
.link
) {
1049 req
->se
->op
.link(req
, arg
->oldnodeid
, nodeid
, name
);
1051 fuse_reply_err(req
, ENOSYS
);
1055 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
,
1056 struct fuse_mbuf_iter
*iter
)
1058 if (req
->se
->op
.create
) {
1059 struct fuse_create_in
*arg
;
1060 struct fuse_file_info fi
;
1063 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1064 name
= fuse_mbuf_iter_advance_str(iter
);
1065 if (!arg
|| !name
) {
1066 fuse_reply_err(req
, EINVAL
);
1070 memset(&fi
, 0, sizeof(fi
));
1071 fi
.flags
= arg
->flags
;
1073 req
->ctx
.umask
= arg
->umask
;
1075 req
->se
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1077 fuse_reply_err(req
, ENOSYS
);
1081 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
,
1082 struct fuse_mbuf_iter
*iter
)
1084 struct fuse_open_in
*arg
;
1085 struct fuse_file_info fi
;
1087 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1089 fuse_reply_err(req
, EINVAL
);
1093 memset(&fi
, 0, sizeof(fi
));
1094 fi
.flags
= arg
->flags
;
1096 if (req
->se
->op
.open
) {
1097 req
->se
->op
.open(req
, nodeid
, &fi
);
1099 fuse_reply_open(req
, &fi
);
1103 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
,
1104 struct fuse_mbuf_iter
*iter
)
1106 if (req
->se
->op
.read
) {
1107 struct fuse_read_in
*arg
;
1108 struct fuse_file_info fi
;
1110 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1112 fuse_reply_err(req
, EINVAL
);
1116 memset(&fi
, 0, sizeof(fi
));
1118 fi
.lock_owner
= arg
->lock_owner
;
1119 fi
.flags
= arg
->flags
;
1120 req
->se
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1122 fuse_reply_err(req
, ENOSYS
);
1126 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
,
1127 struct fuse_mbuf_iter
*iter
)
1129 struct fuse_write_in
*arg
;
1130 struct fuse_file_info fi
;
1133 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1135 fuse_reply_err(req
, EINVAL
);
1139 param
= fuse_mbuf_iter_advance(iter
, arg
->size
);
1141 fuse_reply_err(req
, EINVAL
);
1145 memset(&fi
, 0, sizeof(fi
));
1147 fi
.writepage
= (arg
->write_flags
& FUSE_WRITE_CACHE
) != 0;
1148 fi
.kill_priv
= !!(arg
->write_flags
& FUSE_WRITE_KILL_PRIV
);
1150 fi
.lock_owner
= arg
->lock_owner
;
1151 fi
.flags
= arg
->flags
;
1153 if (req
->se
->op
.write
) {
1154 req
->se
->op
.write(req
, nodeid
, param
, arg
->size
, arg
->offset
, &fi
);
1156 fuse_reply_err(req
, ENOSYS
);
1160 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
,
1161 struct fuse_mbuf_iter
*iter
, struct fuse_bufvec
*ibufv
)
1163 struct fuse_session
*se
= req
->se
;
1164 struct fuse_bufvec
*pbufv
= ibufv
;
1165 struct fuse_bufvec tmpbufv
= {
1166 .buf
[0] = ibufv
->buf
[0],
1169 struct fuse_write_in
*arg
;
1170 size_t arg_size
= sizeof(*arg
);
1171 struct fuse_file_info fi
;
1173 memset(&fi
, 0, sizeof(fi
));
1175 arg
= fuse_mbuf_iter_advance(iter
, arg_size
);
1177 fuse_reply_err(req
, EINVAL
);
1181 fi
.lock_owner
= arg
->lock_owner
;
1182 fi
.flags
= arg
->flags
;
1184 fi
.writepage
= !!(arg
->write_flags
& FUSE_WRITE_CACHE
);
1185 fi
.kill_priv
= !!(arg
->write_flags
& FUSE_WRITE_KILL_PRIV
);
1187 if (ibufv
->count
== 1) {
1188 assert(!(tmpbufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1189 tmpbufv
.buf
[0].mem
= ((char *)arg
) + arg_size
;
1190 tmpbufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) + arg_size
;
1194 * Input bufv contains the headers in the first element
1195 * and the data in the rest, we need to skip that first element
1197 ibufv
->buf
[0].size
= 0;
1200 if (fuse_buf_size(pbufv
) != arg
->size
) {
1201 fuse_log(FUSE_LOG_ERR
,
1202 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
1203 fuse_reply_err(req
, EIO
);
1207 se
->op
.write_buf(req
, nodeid
, pbufv
, arg
->offset
, &fi
);
1210 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
,
1211 struct fuse_mbuf_iter
*iter
)
1213 struct fuse_flush_in
*arg
;
1214 struct fuse_file_info fi
;
1216 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1218 fuse_reply_err(req
, EINVAL
);
1222 memset(&fi
, 0, sizeof(fi
));
1225 fi
.lock_owner
= arg
->lock_owner
;
1227 if (req
->se
->op
.flush
) {
1228 req
->se
->op
.flush(req
, nodeid
, &fi
);
1230 fuse_reply_err(req
, ENOSYS
);
1234 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
,
1235 struct fuse_mbuf_iter
*iter
)
1237 struct fuse_release_in
*arg
;
1238 struct fuse_file_info fi
;
1240 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1242 fuse_reply_err(req
, EINVAL
);
1246 memset(&fi
, 0, sizeof(fi
));
1247 fi
.flags
= arg
->flags
;
1249 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1250 fi
.lock_owner
= arg
->lock_owner
;
1252 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1253 fi
.flock_release
= 1;
1256 if (req
->se
->op
.release
) {
1257 req
->se
->op
.release(req
, nodeid
, &fi
);
1259 fuse_reply_err(req
, 0);
1263 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
,
1264 struct fuse_mbuf_iter
*iter
)
1266 struct fuse_fsync_in
*arg
;
1267 struct fuse_file_info fi
;
1270 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1272 fuse_reply_err(req
, EINVAL
);
1275 datasync
= arg
->fsync_flags
& 1;
1277 memset(&fi
, 0, sizeof(fi
));
1280 if (req
->se
->op
.fsync
) {
1281 if (fi
.fh
== (uint64_t)-1) {
1282 req
->se
->op
.fsync(req
, nodeid
, datasync
, NULL
);
1284 req
->se
->op
.fsync(req
, nodeid
, datasync
, &fi
);
1287 fuse_reply_err(req
, ENOSYS
);
1291 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
,
1292 struct fuse_mbuf_iter
*iter
)
1294 struct fuse_open_in
*arg
;
1295 struct fuse_file_info fi
;
1297 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1299 fuse_reply_err(req
, EINVAL
);
1303 memset(&fi
, 0, sizeof(fi
));
1304 fi
.flags
= arg
->flags
;
1306 if (req
->se
->op
.opendir
) {
1307 req
->se
->op
.opendir(req
, nodeid
, &fi
);
1309 fuse_reply_open(req
, &fi
);
1313 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
,
1314 struct fuse_mbuf_iter
*iter
)
1316 struct fuse_read_in
*arg
;
1317 struct fuse_file_info fi
;
1319 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1321 fuse_reply_err(req
, EINVAL
);
1325 memset(&fi
, 0, sizeof(fi
));
1328 if (req
->se
->op
.readdir
) {
1329 req
->se
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1331 fuse_reply_err(req
, ENOSYS
);
1335 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
,
1336 struct fuse_mbuf_iter
*iter
)
1338 struct fuse_read_in
*arg
;
1339 struct fuse_file_info fi
;
1341 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1343 fuse_reply_err(req
, EINVAL
);
1347 memset(&fi
, 0, sizeof(fi
));
1350 if (req
->se
->op
.readdirplus
) {
1351 req
->se
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1353 fuse_reply_err(req
, ENOSYS
);
1357 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
,
1358 struct fuse_mbuf_iter
*iter
)
1360 struct fuse_release_in
*arg
;
1361 struct fuse_file_info fi
;
1363 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1365 fuse_reply_err(req
, EINVAL
);
1369 memset(&fi
, 0, sizeof(fi
));
1370 fi
.flags
= arg
->flags
;
1373 if (req
->se
->op
.releasedir
) {
1374 req
->se
->op
.releasedir(req
, nodeid
, &fi
);
1376 fuse_reply_err(req
, 0);
1380 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
,
1381 struct fuse_mbuf_iter
*iter
)
1383 struct fuse_fsync_in
*arg
;
1384 struct fuse_file_info fi
;
1387 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1389 fuse_reply_err(req
, EINVAL
);
1392 datasync
= arg
->fsync_flags
& 1;
1394 memset(&fi
, 0, sizeof(fi
));
1397 if (req
->se
->op
.fsyncdir
) {
1398 req
->se
->op
.fsyncdir(req
, nodeid
, datasync
, &fi
);
1400 fuse_reply_err(req
, ENOSYS
);
1404 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
,
1405 struct fuse_mbuf_iter
*iter
)
1410 if (req
->se
->op
.statfs
) {
1411 req
->se
->op
.statfs(req
, nodeid
);
1413 struct statvfs buf
= {
1417 fuse_reply_statfs(req
, &buf
);
1421 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1422 struct fuse_mbuf_iter
*iter
)
1424 struct fuse_setxattr_in
*arg
;
1428 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1429 name
= fuse_mbuf_iter_advance_str(iter
);
1430 if (!arg
|| !name
) {
1431 fuse_reply_err(req
, EINVAL
);
1435 value
= fuse_mbuf_iter_advance(iter
, arg
->size
);
1437 fuse_reply_err(req
, EINVAL
);
1441 if (req
->se
->op
.setxattr
) {
1442 req
->se
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
, arg
->flags
);
1444 fuse_reply_err(req
, ENOSYS
);
1448 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1449 struct fuse_mbuf_iter
*iter
)
1451 struct fuse_getxattr_in
*arg
;
1454 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1455 name
= fuse_mbuf_iter_advance_str(iter
);
1456 if (!arg
|| !name
) {
1457 fuse_reply_err(req
, EINVAL
);
1461 if (req
->se
->op
.getxattr
) {
1462 req
->se
->op
.getxattr(req
, nodeid
, name
, arg
->size
);
1464 fuse_reply_err(req
, ENOSYS
);
1468 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1469 struct fuse_mbuf_iter
*iter
)
1471 struct fuse_getxattr_in
*arg
;
1473 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1475 fuse_reply_err(req
, EINVAL
);
1479 if (req
->se
->op
.listxattr
) {
1480 req
->se
->op
.listxattr(req
, nodeid
, arg
->size
);
1482 fuse_reply_err(req
, ENOSYS
);
1486 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
,
1487 struct fuse_mbuf_iter
*iter
)
1489 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1492 fuse_reply_err(req
, EINVAL
);
1496 if (req
->se
->op
.removexattr
) {
1497 req
->se
->op
.removexattr(req
, nodeid
, name
);
1499 fuse_reply_err(req
, ENOSYS
);
1503 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1504 struct flock
*flock
)
1506 memset(flock
, 0, sizeof(struct flock
));
1507 flock
->l_type
= fl
->type
;
1508 flock
->l_whence
= SEEK_SET
;
1509 flock
->l_start
= fl
->start
;
1510 if (fl
->end
== OFFSET_MAX
) {
1513 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1515 flock
->l_pid
= fl
->pid
;
1518 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
,
1519 struct fuse_mbuf_iter
*iter
)
1521 struct fuse_lk_in
*arg
;
1522 struct fuse_file_info fi
;
1525 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1527 fuse_reply_err(req
, EINVAL
);
1531 memset(&fi
, 0, sizeof(fi
));
1533 fi
.lock_owner
= arg
->owner
;
1535 convert_fuse_file_lock(&arg
->lk
, &flock
);
1536 if (req
->se
->op
.getlk
) {
1537 req
->se
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1539 fuse_reply_err(req
, ENOSYS
);
1543 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1544 struct fuse_mbuf_iter
*iter
, int sleep
)
1546 struct fuse_lk_in
*arg
;
1547 struct fuse_file_info fi
;
1550 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1552 fuse_reply_err(req
, EINVAL
);
1556 memset(&fi
, 0, sizeof(fi
));
1558 fi
.lock_owner
= arg
->owner
;
1560 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1563 switch (arg
->lk
.type
) {
1578 if (req
->se
->op
.flock
) {
1579 req
->se
->op
.flock(req
, nodeid
, &fi
, op
);
1581 fuse_reply_err(req
, ENOSYS
);
1584 convert_fuse_file_lock(&arg
->lk
, &flock
);
1585 if (req
->se
->op
.setlk
) {
1586 req
->se
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1588 fuse_reply_err(req
, ENOSYS
);
1593 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
,
1594 struct fuse_mbuf_iter
*iter
)
1596 do_setlk_common(req
, nodeid
, iter
, 0);
1599 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
,
1600 struct fuse_mbuf_iter
*iter
)
1602 do_setlk_common(req
, nodeid
, iter
, 1);
1605 static int find_interrupted(struct fuse_session
*se
, struct fuse_req
*req
)
1607 struct fuse_req
*curr
;
1609 for (curr
= se
->list
.next
; curr
!= &se
->list
; curr
= curr
->next
) {
1610 if (curr
->unique
== req
->u
.i
.unique
) {
1611 fuse_interrupt_func_t func
;
1615 pthread_mutex_unlock(&se
->lock
);
1617 /* Ugh, ugly locking */
1618 pthread_mutex_lock(&curr
->lock
);
1619 pthread_mutex_lock(&se
->lock
);
1620 curr
->interrupted
= 1;
1621 func
= curr
->u
.ni
.func
;
1622 data
= curr
->u
.ni
.data
;
1623 pthread_mutex_unlock(&se
->lock
);
1627 pthread_mutex_unlock(&curr
->lock
);
1629 pthread_mutex_lock(&se
->lock
);
1638 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1639 curr
= curr
->next
) {
1640 if (curr
->u
.i
.unique
== req
->u
.i
.unique
) {
1647 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
,
1648 struct fuse_mbuf_iter
*iter
)
1650 struct fuse_interrupt_in
*arg
;
1651 struct fuse_session
*se
= req
->se
;
1655 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1657 fuse_reply_err(req
, EINVAL
);
1661 fuse_log(FUSE_LOG_DEBUG
, "INTERRUPT: %llu\n",
1662 (unsigned long long)arg
->unique
);
1664 req
->u
.i
.unique
= arg
->unique
;
1666 pthread_mutex_lock(&se
->lock
);
1667 if (find_interrupted(se
, req
)) {
1670 list_add_req(req
, &se
->interrupts
);
1672 pthread_mutex_unlock(&se
->lock
);
1675 static struct fuse_req
*check_interrupt(struct fuse_session
*se
,
1676 struct fuse_req
*req
)
1678 struct fuse_req
*curr
;
1680 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1681 curr
= curr
->next
) {
1682 if (curr
->u
.i
.unique
== req
->unique
) {
1683 req
->interrupted
= 1;
1689 curr
= se
->interrupts
.next
;
1690 if (curr
!= &se
->interrupts
) {
1692 list_init_req(curr
);
1699 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
,
1700 struct fuse_mbuf_iter
*iter
)
1702 struct fuse_bmap_in
*arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1705 fuse_reply_err(req
, EINVAL
);
1709 if (req
->se
->op
.bmap
) {
1710 req
->se
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1712 fuse_reply_err(req
, ENOSYS
);
1716 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
,
1717 struct fuse_mbuf_iter
*iter
)
1719 struct fuse_ioctl_in
*arg
;
1721 void *in_buf
= NULL
;
1722 struct fuse_file_info fi
;
1724 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1726 fuse_reply_err(req
, EINVAL
);
1731 if (flags
& FUSE_IOCTL_DIR
&& !(req
->se
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1732 fuse_reply_err(req
, ENOTTY
);
1737 in_buf
= fuse_mbuf_iter_advance(iter
, arg
->in_size
);
1739 fuse_reply_err(req
, EINVAL
);
1744 memset(&fi
, 0, sizeof(fi
));
1747 if (sizeof(void *) == 4 && !(flags
& FUSE_IOCTL_32BIT
)) {
1748 req
->ioctl_64bit
= 1;
1751 if (req
->se
->op
.ioctl
) {
1752 req
->se
->op
.ioctl(req
, nodeid
, arg
->cmd
, (void *)(uintptr_t)arg
->arg
,
1753 &fi
, flags
, in_buf
, arg
->in_size
, arg
->out_size
);
1755 fuse_reply_err(req
, ENOSYS
);
1759 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1764 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
,
1765 struct fuse_mbuf_iter
*iter
)
1767 struct fuse_poll_in
*arg
;
1768 struct fuse_file_info fi
;
1770 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1772 fuse_reply_err(req
, EINVAL
);
1776 memset(&fi
, 0, sizeof(fi
));
1778 fi
.poll_events
= arg
->events
;
1780 if (req
->se
->op
.poll
) {
1781 struct fuse_pollhandle
*ph
= NULL
;
1783 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1784 ph
= malloc(sizeof(struct fuse_pollhandle
));
1786 fuse_reply_err(req
, ENOMEM
);
1793 req
->se
->op
.poll(req
, nodeid
, &fi
, ph
);
1795 fuse_reply_err(req
, ENOSYS
);
1799 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
,
1800 struct fuse_mbuf_iter
*iter
)
1802 struct fuse_fallocate_in
*arg
;
1803 struct fuse_file_info fi
;
1805 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1807 fuse_reply_err(req
, EINVAL
);
1811 memset(&fi
, 0, sizeof(fi
));
1814 if (req
->se
->op
.fallocate
) {
1815 req
->se
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
,
1818 fuse_reply_err(req
, ENOSYS
);
1822 static void do_copy_file_range(fuse_req_t req
, fuse_ino_t nodeid_in
,
1823 struct fuse_mbuf_iter
*iter
)
1825 struct fuse_copy_file_range_in
*arg
;
1826 struct fuse_file_info fi_in
, fi_out
;
1828 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1830 fuse_reply_err(req
, EINVAL
);
1834 memset(&fi_in
, 0, sizeof(fi_in
));
1835 fi_in
.fh
= arg
->fh_in
;
1837 memset(&fi_out
, 0, sizeof(fi_out
));
1838 fi_out
.fh
= arg
->fh_out
;
1841 if (req
->se
->op
.copy_file_range
) {
1842 req
->se
->op
.copy_file_range(req
, nodeid_in
, arg
->off_in
, &fi_in
,
1843 arg
->nodeid_out
, arg
->off_out
, &fi_out
,
1844 arg
->len
, arg
->flags
);
1846 fuse_reply_err(req
, ENOSYS
);
1850 static void do_lseek(fuse_req_t req
, fuse_ino_t nodeid
,
1851 struct fuse_mbuf_iter
*iter
)
1853 struct fuse_lseek_in
*arg
;
1854 struct fuse_file_info fi
;
1856 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1858 fuse_reply_err(req
, EINVAL
);
1861 memset(&fi
, 0, sizeof(fi
));
1864 if (req
->se
->op
.lseek
) {
1865 req
->se
->op
.lseek(req
, nodeid
, arg
->offset
, arg
->whence
, &fi
);
1867 fuse_reply_err(req
, ENOSYS
);
1871 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
,
1872 struct fuse_mbuf_iter
*iter
)
1874 size_t compat_size
= offsetof(struct fuse_init_in
, max_readahead
);
1875 struct fuse_init_in
*arg
;
1876 struct fuse_init_out outarg
;
1877 struct fuse_session
*se
= req
->se
;
1878 size_t bufsize
= se
->bufsize
;
1879 size_t outargsize
= sizeof(outarg
);
1883 /* First consume the old fields... */
1884 arg
= fuse_mbuf_iter_advance(iter
, compat_size
);
1886 fuse_reply_err(req
, EINVAL
);
1890 /* ...and now consume the new fields. */
1891 if (arg
->major
== 7 && arg
->minor
>= 6) {
1892 if (!fuse_mbuf_iter_advance(iter
, sizeof(*arg
) - compat_size
)) {
1893 fuse_reply_err(req
, EINVAL
);
1898 fuse_log(FUSE_LOG_DEBUG
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1899 if (arg
->major
== 7 && arg
->minor
>= 6) {
1900 fuse_log(FUSE_LOG_DEBUG
, "flags=0x%08x\n", arg
->flags
);
1901 fuse_log(FUSE_LOG_DEBUG
, "max_readahead=0x%08x\n", arg
->max_readahead
);
1903 se
->conn
.proto_major
= arg
->major
;
1904 se
->conn
.proto_minor
= arg
->minor
;
1905 se
->conn
.capable
= 0;
1908 memset(&outarg
, 0, sizeof(outarg
));
1909 outarg
.major
= FUSE_KERNEL_VERSION
;
1910 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1912 if (arg
->major
< 7 || (arg
->major
== 7 && arg
->minor
< 31)) {
1913 fuse_log(FUSE_LOG_ERR
, "fuse: unsupported protocol version: %u.%u\n",
1914 arg
->major
, arg
->minor
);
1915 fuse_reply_err(req
, EPROTO
);
1919 if (arg
->major
> 7) {
1920 /* Wait for a second INIT request with a 7.X version */
1921 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1925 if (arg
->max_readahead
< se
->conn
.max_readahead
) {
1926 se
->conn
.max_readahead
= arg
->max_readahead
;
1928 if (arg
->flags
& FUSE_ASYNC_READ
) {
1929 se
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1931 if (arg
->flags
& FUSE_POSIX_LOCKS
) {
1932 se
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1934 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
) {
1935 se
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1937 if (arg
->flags
& FUSE_EXPORT_SUPPORT
) {
1938 se
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1940 if (arg
->flags
& FUSE_DONT_MASK
) {
1941 se
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1943 if (arg
->flags
& FUSE_FLOCK_LOCKS
) {
1944 se
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1946 if (arg
->flags
& FUSE_AUTO_INVAL_DATA
) {
1947 se
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
1949 if (arg
->flags
& FUSE_DO_READDIRPLUS
) {
1950 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
1952 if (arg
->flags
& FUSE_READDIRPLUS_AUTO
) {
1953 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
1955 if (arg
->flags
& FUSE_ASYNC_DIO
) {
1956 se
->conn
.capable
|= FUSE_CAP_ASYNC_DIO
;
1958 if (arg
->flags
& FUSE_WRITEBACK_CACHE
) {
1959 se
->conn
.capable
|= FUSE_CAP_WRITEBACK_CACHE
;
1961 if (arg
->flags
& FUSE_NO_OPEN_SUPPORT
) {
1962 se
->conn
.capable
|= FUSE_CAP_NO_OPEN_SUPPORT
;
1964 if (arg
->flags
& FUSE_PARALLEL_DIROPS
) {
1965 se
->conn
.capable
|= FUSE_CAP_PARALLEL_DIROPS
;
1967 if (arg
->flags
& FUSE_POSIX_ACL
) {
1968 se
->conn
.capable
|= FUSE_CAP_POSIX_ACL
;
1970 if (arg
->flags
& FUSE_HANDLE_KILLPRIV
) {
1971 se
->conn
.capable
|= FUSE_CAP_HANDLE_KILLPRIV
;
1973 if (arg
->flags
& FUSE_NO_OPENDIR_SUPPORT
) {
1974 se
->conn
.capable
|= FUSE_CAP_NO_OPENDIR_SUPPORT
;
1976 if (!(arg
->flags
& FUSE_MAX_PAGES
)) {
1977 size_t max_bufsize
= FUSE_DEFAULT_MAX_PAGES_PER_REQ
* getpagesize() +
1978 FUSE_BUFFER_HEADER_SIZE
;
1979 if (bufsize
> max_bufsize
) {
1980 bufsize
= max_bufsize
;
1983 if (arg
->flags
& FUSE_SUBMOUNTS
) {
1984 se
->conn
.capable
|= FUSE_CAP_SUBMOUNTS
;
1987 #ifdef HAVE_VMSPLICE
1988 se
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1990 se
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1992 se
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
1995 * Default settings for modern filesystems.
1997 * Most of these capabilities were disabled by default in
1998 * libfuse2 for backwards compatibility reasons. In libfuse3,
1999 * we can finally enable them by default (as long as they're
2000 * supported by the kernel).
2002 #define LL_SET_DEFAULT(cond, cap) \
2003 if ((cond) && (se->conn.capable & (cap))) \
2004 se->conn.want |= (cap)
2005 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ
);
2006 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS
);
2007 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA
);
2008 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV
);
2009 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO
);
2010 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR
);
2011 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC
);
2012 LL_SET_DEFAULT(se
->op
.write_buf
, FUSE_CAP_SPLICE_READ
);
2013 LL_SET_DEFAULT(se
->op
.getlk
&& se
->op
.setlk
, FUSE_CAP_POSIX_LOCKS
);
2014 LL_SET_DEFAULT(se
->op
.flock
, FUSE_CAP_FLOCK_LOCKS
);
2015 LL_SET_DEFAULT(se
->op
.readdirplus
, FUSE_CAP_READDIRPLUS
);
2016 LL_SET_DEFAULT(se
->op
.readdirplus
&& se
->op
.readdir
,
2017 FUSE_CAP_READDIRPLUS_AUTO
);
2018 se
->conn
.time_gran
= 1;
2020 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
2021 fuse_log(FUSE_LOG_ERR
, "fuse: warning: buffer size too small: %zu\n",
2023 bufsize
= FUSE_MIN_READ_BUFFER
;
2025 se
->bufsize
= bufsize
;
2027 if (se
->conn
.max_write
> bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
2028 se
->conn
.max_write
= bufsize
- FUSE_BUFFER_HEADER_SIZE
;
2032 se
->got_destroy
= 0;
2034 se
->op
.init(se
->userdata
, &se
->conn
);
2037 if (se
->conn
.want
& (~se
->conn
.capable
)) {
2038 fuse_log(FUSE_LOG_ERR
,
2039 "fuse: error: filesystem requested capabilities "
2040 "0x%x that are not supported by kernel, aborting.\n",
2041 se
->conn
.want
& (~se
->conn
.capable
));
2042 fuse_reply_err(req
, EPROTO
);
2043 se
->error
= -EPROTO
;
2044 fuse_session_exit(se
);
2048 if (se
->conn
.max_write
< bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
2049 se
->bufsize
= se
->conn
.max_write
+ FUSE_BUFFER_HEADER_SIZE
;
2051 if (arg
->flags
& FUSE_MAX_PAGES
) {
2052 outarg
.flags
|= FUSE_MAX_PAGES
;
2053 outarg
.max_pages
= (se
->conn
.max_write
- 1) / getpagesize() + 1;
2057 * Always enable big writes, this is superseded
2058 * by the max_write option
2060 outarg
.flags
|= FUSE_BIG_WRITES
;
2062 if (se
->conn
.want
& FUSE_CAP_ASYNC_READ
) {
2063 outarg
.flags
|= FUSE_ASYNC_READ
;
2065 if (se
->conn
.want
& FUSE_CAP_PARALLEL_DIROPS
) {
2066 outarg
.flags
|= FUSE_PARALLEL_DIROPS
;
2068 if (se
->conn
.want
& FUSE_CAP_POSIX_LOCKS
) {
2069 outarg
.flags
|= FUSE_POSIX_LOCKS
;
2071 if (se
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
) {
2072 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
2074 if (se
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
) {
2075 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
2077 if (se
->conn
.want
& FUSE_CAP_DONT_MASK
) {
2078 outarg
.flags
|= FUSE_DONT_MASK
;
2080 if (se
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
) {
2081 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
2083 if (se
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
) {
2084 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
2086 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS
) {
2087 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
2089 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
) {
2090 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
2092 if (se
->conn
.want
& FUSE_CAP_ASYNC_DIO
) {
2093 outarg
.flags
|= FUSE_ASYNC_DIO
;
2095 if (se
->conn
.want
& FUSE_CAP_WRITEBACK_CACHE
) {
2096 outarg
.flags
|= FUSE_WRITEBACK_CACHE
;
2098 if (se
->conn
.want
& FUSE_CAP_POSIX_ACL
) {
2099 outarg
.flags
|= FUSE_POSIX_ACL
;
2101 outarg
.max_readahead
= se
->conn
.max_readahead
;
2102 outarg
.max_write
= se
->conn
.max_write
;
2103 if (se
->conn
.max_background
>= (1 << 16)) {
2104 se
->conn
.max_background
= (1 << 16) - 1;
2106 if (se
->conn
.congestion_threshold
> se
->conn
.max_background
) {
2107 se
->conn
.congestion_threshold
= se
->conn
.max_background
;
2109 if (!se
->conn
.congestion_threshold
) {
2110 se
->conn
.congestion_threshold
= se
->conn
.max_background
* 3 / 4;
2113 outarg
.max_background
= se
->conn
.max_background
;
2114 outarg
.congestion_threshold
= se
->conn
.congestion_threshold
;
2115 outarg
.time_gran
= se
->conn
.time_gran
;
2117 fuse_log(FUSE_LOG_DEBUG
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
2118 fuse_log(FUSE_LOG_DEBUG
, " flags=0x%08x\n", outarg
.flags
);
2119 fuse_log(FUSE_LOG_DEBUG
, " max_readahead=0x%08x\n", outarg
.max_readahead
);
2120 fuse_log(FUSE_LOG_DEBUG
, " max_write=0x%08x\n", outarg
.max_write
);
2121 fuse_log(FUSE_LOG_DEBUG
, " max_background=%i\n", outarg
.max_background
);
2122 fuse_log(FUSE_LOG_DEBUG
, " congestion_threshold=%i\n",
2123 outarg
.congestion_threshold
);
2124 fuse_log(FUSE_LOG_DEBUG
, " time_gran=%u\n", outarg
.time_gran
);
2126 send_reply_ok(req
, &outarg
, outargsize
);
2129 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
,
2130 struct fuse_mbuf_iter
*iter
)
2132 struct fuse_session
*se
= req
->se
;
2137 se
->got_destroy
= 1;
2139 if (se
->op
.destroy
) {
2140 se
->op
.destroy(se
->userdata
);
2143 send_reply_ok(req
, NULL
, 0);
2146 static int send_notify_iov(struct fuse_session
*se
, int notify_code
,
2147 struct iovec
*iov
, int count
)
2149 struct fuse_out_header out
= {
2150 .error
= notify_code
,
2153 if (!se
->got_init
) {
2157 iov
[0].iov_base
= &out
;
2158 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
2160 return fuse_send_msg(se
, NULL
, iov
, count
);
2163 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
2166 struct fuse_notify_poll_wakeup_out outarg
= {
2169 struct iovec iov
[2];
2171 iov
[1].iov_base
= &outarg
;
2172 iov
[1].iov_len
= sizeof(outarg
);
2174 return send_notify_iov(ph
->se
, FUSE_NOTIFY_POLL
, iov
, 2);
2180 int fuse_lowlevel_notify_inval_inode(struct fuse_session
*se
, fuse_ino_t ino
,
2181 off_t off
, off_t len
)
2183 struct fuse_notify_inval_inode_out outarg
= {
2188 struct iovec iov
[2];
2194 iov
[1].iov_base
= &outarg
;
2195 iov
[1].iov_len
= sizeof(outarg
);
2197 return send_notify_iov(se
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
2200 int fuse_lowlevel_notify_inval_entry(struct fuse_session
*se
, fuse_ino_t parent
,
2201 const char *name
, size_t namelen
)
2203 struct fuse_notify_inval_entry_out outarg
= {
2207 struct iovec iov
[3];
2213 iov
[1].iov_base
= &outarg
;
2214 iov
[1].iov_len
= sizeof(outarg
);
2215 iov
[2].iov_base
= (void *)name
;
2216 iov
[2].iov_len
= namelen
+ 1;
2218 return send_notify_iov(se
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
2221 int fuse_lowlevel_notify_delete(struct fuse_session
*se
, fuse_ino_t parent
,
2222 fuse_ino_t child
, const char *name
,
2225 struct fuse_notify_delete_out outarg
= {
2230 struct iovec iov
[3];
2236 iov
[1].iov_base
= &outarg
;
2237 iov
[1].iov_len
= sizeof(outarg
);
2238 iov
[2].iov_base
= (void *)name
;
2239 iov
[2].iov_len
= namelen
+ 1;
2241 return send_notify_iov(se
, FUSE_NOTIFY_DELETE
, iov
, 3);
2244 int fuse_lowlevel_notify_store(struct fuse_session
*se
, fuse_ino_t ino
,
2245 off_t offset
, struct fuse_bufvec
*bufv
)
2247 struct fuse_out_header out
= {
2248 .error
= FUSE_NOTIFY_STORE
,
2250 struct fuse_notify_store_out outarg
= {
2253 .size
= fuse_buf_size(bufv
),
2255 struct iovec iov
[3];
2262 iov
[0].iov_base
= &out
;
2263 iov
[0].iov_len
= sizeof(out
);
2264 iov
[1].iov_base
= &outarg
;
2265 iov
[1].iov_len
= sizeof(outarg
);
2267 res
= fuse_send_data_iov(se
, NULL
, iov
, 2, bufv
);
2275 void *fuse_req_userdata(fuse_req_t req
)
2277 return req
->se
->userdata
;
2280 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2285 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2288 pthread_mutex_lock(&req
->lock
);
2289 pthread_mutex_lock(&req
->se
->lock
);
2290 req
->u
.ni
.func
= func
;
2291 req
->u
.ni
.data
= data
;
2292 pthread_mutex_unlock(&req
->se
->lock
);
2293 if (req
->interrupted
&& func
) {
2296 pthread_mutex_unlock(&req
->lock
);
2299 int fuse_req_interrupted(fuse_req_t req
)
2303 pthread_mutex_lock(&req
->se
->lock
);
2304 interrupted
= req
->interrupted
;
2305 pthread_mutex_unlock(&req
->se
->lock
);
2311 void (*func
)(fuse_req_t
, fuse_ino_t
, struct fuse_mbuf_iter
*);
2314 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2315 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2316 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2317 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2318 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2319 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2320 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2321 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2322 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2323 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2324 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2325 [FUSE_LINK
] = { do_link
, "LINK" },
2326 [FUSE_OPEN
] = { do_open
, "OPEN" },
2327 [FUSE_READ
] = { do_read
, "READ" },
2328 [FUSE_WRITE
] = { do_write
, "WRITE" },
2329 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2330 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2331 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2332 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2333 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2334 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2335 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2336 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2337 [FUSE_INIT
] = { do_init
, "INIT" },
2338 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2339 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2340 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2341 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2342 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2343 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2344 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2345 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2346 [FUSE_CREATE
] = { do_create
, "CREATE" },
2347 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2348 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2349 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2350 [FUSE_POLL
] = { do_poll
, "POLL" },
2351 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
2352 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2353 [FUSE_NOTIFY_REPLY
] = { NULL
, "NOTIFY_REPLY" },
2354 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2355 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS" },
2356 [FUSE_RENAME2
] = { do_rename2
, "RENAME2" },
2357 [FUSE_COPY_FILE_RANGE
] = { do_copy_file_range
, "COPY_FILE_RANGE" },
2358 [FUSE_LSEEK
] = { do_lseek
, "LSEEK" },
2361 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2363 static const char *opname(enum fuse_opcode opcode
)
2365 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
) {
2368 return fuse_ll_ops
[opcode
].name
;
2372 void fuse_session_process_buf(struct fuse_session
*se
,
2373 const struct fuse_buf
*buf
)
2375 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2376 fuse_session_process_buf_int(se
, &bufv
, NULL
);
2381 * bufv is normally a single entry buffer, except for a write
2382 * where (if it's in memory) then the bufv may be multiple entries,
2383 * where the first entry contains all headers and subsequent entries
2385 * bufv shall not use any offsets etc to make the data anything
2386 * other than contiguous starting from 0.
2388 void fuse_session_process_buf_int(struct fuse_session
*se
,
2389 struct fuse_bufvec
*bufv
,
2390 struct fuse_chan
*ch
)
2392 const struct fuse_buf
*buf
= bufv
->buf
;
2393 struct fuse_mbuf_iter iter
= FUSE_MBUF_ITER_INIT(buf
);
2394 struct fuse_in_header
*in
;
2395 struct fuse_req
*req
;
2398 /* The first buffer must be a memory buffer */
2399 assert(!(buf
->flags
& FUSE_BUF_IS_FD
));
2401 in
= fuse_mbuf_iter_advance(&iter
, sizeof(*in
));
2402 assert(in
); /* caller guarantees the input buffer is large enough */
2406 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2407 (unsigned long long)in
->unique
, opname((enum fuse_opcode
)in
->opcode
),
2408 in
->opcode
, (unsigned long long)in
->nodeid
, buf
->size
, in
->pid
);
2410 req
= fuse_ll_alloc_req(se
);
2412 struct fuse_out_header out
= {
2413 .unique
= in
->unique
,
2416 struct iovec iov
= {
2418 .iov_len
= sizeof(struct fuse_out_header
),
2421 fuse_send_msg(se
, ch
, &iov
, 1);
2425 req
->unique
= in
->unique
;
2426 req
->ctx
.uid
= in
->uid
;
2427 req
->ctx
.gid
= in
->gid
;
2428 req
->ctx
.pid
= in
->pid
;
2432 * INIT and DESTROY requests are serialized, all other request types
2433 * run in parallel. This prevents races between FUSE_INIT and ordinary
2434 * requests, FUSE_INIT and FUSE_INIT, FUSE_INIT and FUSE_DESTROY, and
2435 * FUSE_DESTROY and FUSE_DESTROY.
2437 if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
||
2438 in
->opcode
== FUSE_DESTROY
) {
2439 pthread_rwlock_wrlock(&se
->init_rwlock
);
2441 pthread_rwlock_rdlock(&se
->init_rwlock
);
2445 if (!se
->got_init
) {
2446 enum fuse_opcode expected
;
2448 expected
= se
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2449 if (in
->opcode
!= expected
) {
2452 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
) {
2453 if (fuse_lowlevel_is_virtio(se
)) {
2455 * TODO: This is after a hard reboot typically, we need to do
2456 * a destroy, but we can't reply to this request yet so
2457 * we can't use do_destroy
2459 fuse_log(FUSE_LOG_DEBUG
, "%s: reinit\n", __func__
);
2460 se
->got_destroy
= 1;
2462 if (se
->op
.destroy
) {
2463 se
->op
.destroy(se
->userdata
);
2471 /* Implement -o allow_root */
2472 if (se
->deny_others
&& in
->uid
!= se
->owner
&& in
->uid
!= 0 &&
2473 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2474 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2475 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2476 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2477 in
->opcode
!= FUSE_NOTIFY_REPLY
&& in
->opcode
!= FUSE_READDIRPLUS
) {
2482 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
) {
2485 if (in
->opcode
!= FUSE_INTERRUPT
) {
2486 struct fuse_req
*intr
;
2487 pthread_mutex_lock(&se
->lock
);
2488 intr
= check_interrupt(se
, req
);
2489 list_add_req(req
, &se
->list
);
2490 pthread_mutex_unlock(&se
->lock
);
2492 fuse_reply_err(intr
, EAGAIN
);
2496 if (in
->opcode
== FUSE_WRITE
&& se
->op
.write_buf
) {
2497 do_write_buf(req
, in
->nodeid
, &iter
, bufv
);
2499 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, &iter
);
2502 pthread_rwlock_unlock(&se
->init_rwlock
);
2506 fuse_reply_err(req
, err
);
2507 pthread_rwlock_unlock(&se
->init_rwlock
);
2510 #define LL_OPTION(n, o, v) \
2512 n, offsetof(struct fuse_session, o), v \
2515 static const struct fuse_opt fuse_ll_opts
[] = {
2516 LL_OPTION("debug", debug
, 1),
2517 LL_OPTION("-d", debug
, 1),
2518 LL_OPTION("--debug", debug
, 1),
2519 LL_OPTION("allow_root", deny_others
, 1),
2520 LL_OPTION("--socket-path=%s", vu_socket_path
, 0),
2521 LL_OPTION("--socket-group=%s", vu_socket_group
, 0),
2522 LL_OPTION("--fd=%d", vu_listen_fd
, 0),
2523 LL_OPTION("--thread-pool-size=%d", thread_pool_size
, 0),
2527 void fuse_lowlevel_version(void)
2529 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION
,
2530 FUSE_KERNEL_MINOR_VERSION
);
2533 void fuse_lowlevel_help(void)
2536 * These are not all options, but the ones that are
2537 * potentially of interest to an end-user
2540 " -o allow_root allow access by root\n"
2541 " --socket-path=PATH path for the vhost-user socket\n"
2542 " --fd=FDNUM fd number of vhost-user socket\n"
2543 " --thread-pool-size=NUM thread pool size limit (default %d)\n",
2547 void fuse_session_destroy(struct fuse_session
*se
)
2549 if (se
->got_init
&& !se
->got_destroy
) {
2550 if (se
->op
.destroy
) {
2551 se
->op
.destroy(se
->userdata
);
2554 pthread_rwlock_destroy(&se
->init_rwlock
);
2555 pthread_mutex_destroy(&se
->lock
);
2556 free(se
->cuse_data
);
2561 if (fuse_lowlevel_is_virtio(se
)) {
2562 virtio_session_close(se
);
2565 free(se
->vu_socket_path
);
2566 se
->vu_socket_path
= NULL
;
2572 struct fuse_session
*fuse_session_new(struct fuse_args
*args
,
2573 const struct fuse_lowlevel_ops
*op
,
2574 size_t op_size
, void *userdata
)
2576 struct fuse_session
*se
;
2578 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2581 "fuse: warning: library too old, some operations may not work\n");
2582 op_size
= sizeof(struct fuse_lowlevel_ops
);
2585 if (args
->argc
== 0) {
2586 fuse_log(FUSE_LOG_ERR
,
2587 "fuse: empty argv passed to fuse_session_new().\n");
2591 se
= (struct fuse_session
*)calloc(1, sizeof(struct fuse_session
));
2593 fuse_log(FUSE_LOG_ERR
, "fuse: failed to allocate fuse object\n");
2597 se
->vu_listen_fd
= -1;
2598 se
->thread_pool_size
= THREAD_POOL_SIZE
;
2599 se
->conn
.max_write
= UINT_MAX
;
2600 se
->conn
.max_readahead
= UINT_MAX
;
2603 if (fuse_opt_parse(args
, se
, fuse_ll_opts
, NULL
) == -1) {
2606 if (args
->argc
== 1 && args
->argv
[0][0] == '-') {
2607 fuse_log(FUSE_LOG_ERR
,
2608 "fuse: warning: argv[0] looks like an option, but "
2609 "will be ignored\n");
2610 } else if (args
->argc
!= 1) {
2612 fuse_log(FUSE_LOG_ERR
, "fuse: unknown option(s): `");
2613 for (i
= 1; i
< args
->argc
- 1; i
++) {
2614 fuse_log(FUSE_LOG_ERR
, "%s ", args
->argv
[i
]);
2616 fuse_log(FUSE_LOG_ERR
, "%s'\n", args
->argv
[i
]);
2620 if (!se
->vu_socket_path
&& se
->vu_listen_fd
< 0) {
2621 fuse_log(FUSE_LOG_ERR
, "fuse: missing --socket-path or --fd option\n");
2624 if (se
->vu_socket_path
&& se
->vu_listen_fd
>= 0) {
2625 fuse_log(FUSE_LOG_ERR
,
2626 "fuse: --socket-path and --fd cannot be given together\n");
2629 if (se
->vu_socket_group
&& !se
->vu_socket_path
) {
2630 fuse_log(FUSE_LOG_ERR
,
2631 "fuse: --socket-group can only be used with --socket-path\n");
2635 se
->bufsize
= FUSE_MAX_MAX_PAGES
* getpagesize() + FUSE_BUFFER_HEADER_SIZE
;
2637 list_init_req(&se
->list
);
2638 list_init_req(&se
->interrupts
);
2639 fuse_mutex_init(&se
->lock
);
2640 pthread_rwlock_init(&se
->init_rwlock
, NULL
);
2642 memcpy(&se
->op
, op
, op_size
);
2643 se
->owner
= getuid();
2644 se
->userdata
= userdata
;
2649 fuse_opt_free_args(args
);
2656 int fuse_session_mount(struct fuse_session
*se
)
2658 return virtio_session_mount(se
);
2661 int fuse_session_fd(struct fuse_session
*se
)
2666 void fuse_session_unmount(struct fuse_session
*se
)
2670 int fuse_lowlevel_is_virtio(struct fuse_session
*se
)
2672 return !!se
->virtio_dev
;
2675 void fuse_session_exit(struct fuse_session
*se
)
2680 void fuse_session_reset(struct fuse_session
*se
)
2686 int fuse_session_exited(struct fuse_session
*se
)