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 0
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
= g_try_new0(struct fuse_req
, 1);
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
)
219 g_autofree
struct iovec
*padded_iov
= NULL
;
221 padded_iov
= g_try_new(struct iovec
, count
+ 1);
222 if (padded_iov
== NULL
) {
223 return fuse_reply_err(req
, ENOMEM
);
226 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
229 return send_reply_iov(req
, 0, padded_iov
, count
);
234 * 'buf` is allowed to be empty so that the proper size may be
235 * allocated by the caller
237 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
238 const char *name
, const struct stat
*stbuf
, off_t off
)
243 size_t entlen_padded
;
244 struct fuse_dirent
*dirent
;
246 namelen
= strlen(name
);
247 entlen
= FUSE_NAME_OFFSET
+ namelen
;
248 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
250 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
251 return entlen_padded
;
254 dirent
= (struct fuse_dirent
*)buf
;
255 dirent
->ino
= stbuf
->st_ino
;
257 dirent
->namelen
= namelen
;
258 dirent
->type
= (stbuf
->st_mode
& S_IFMT
) >> 12;
259 memcpy(dirent
->name
, name
, namelen
);
260 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
262 return entlen_padded
;
265 static void convert_statfs(const struct statvfs
*stbuf
,
266 struct fuse_kstatfs
*kstatfs
)
268 *kstatfs
= (struct fuse_kstatfs
){
269 .bsize
= stbuf
->f_bsize
,
270 .frsize
= stbuf
->f_frsize
,
271 .blocks
= stbuf
->f_blocks
,
272 .bfree
= stbuf
->f_bfree
,
273 .bavail
= stbuf
->f_bavail
,
274 .files
= stbuf
->f_files
,
275 .ffree
= stbuf
->f_ffree
,
276 .namelen
= stbuf
->f_namemax
,
280 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
282 return send_reply(req
, 0, arg
, argsize
);
285 int fuse_reply_err(fuse_req_t req
, int err
)
287 return send_reply(req
, -err
, NULL
, 0);
290 void fuse_reply_none(fuse_req_t req
)
295 static unsigned long calc_timeout_sec(double t
)
297 if (t
> (double)ULONG_MAX
) {
299 } else if (t
< 0.0) {
302 return (unsigned long)t
;
306 static unsigned int calc_timeout_nsec(double t
)
308 double f
= t
- (double)calc_timeout_sec(t
);
311 } else if (f
>= 0.999999999) {
314 return (unsigned int)(f
* 1.0e9
);
318 static void fill_entry(struct fuse_entry_out
*arg
,
319 const struct fuse_entry_param
*e
)
321 *arg
= (struct fuse_entry_out
){
323 .generation
= e
->generation
,
324 .entry_valid
= calc_timeout_sec(e
->entry_timeout
),
325 .entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
),
326 .attr_valid
= calc_timeout_sec(e
->attr_timeout
),
327 .attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
),
329 convert_stat(&e
->attr
, &arg
->attr
);
331 arg
->attr
.flags
= e
->attr_flags
;
335 * `buf` is allowed to be empty so that the proper size may be
336 * allocated by the caller
338 size_t fuse_add_direntry_plus(fuse_req_t req
, char *buf
, size_t bufsize
,
340 const struct fuse_entry_param
*e
, off_t off
)
345 size_t entlen_padded
;
347 namelen
= strlen(name
);
348 entlen
= FUSE_NAME_OFFSET_DIRENTPLUS
+ namelen
;
349 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
350 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
351 return entlen_padded
;
354 struct fuse_direntplus
*dp
= (struct fuse_direntplus
*)buf
;
355 memset(&dp
->entry_out
, 0, sizeof(dp
->entry_out
));
356 fill_entry(&dp
->entry_out
, e
);
358 struct fuse_dirent
*dirent
= &dp
->dirent
;
359 *dirent
= (struct fuse_dirent
){
360 .ino
= e
->attr
.st_ino
,
363 .type
= (e
->attr
.st_mode
& S_IFMT
) >> 12,
365 memcpy(dirent
->name
, name
, namelen
);
366 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
368 return entlen_padded
;
371 static void fill_open(struct fuse_open_out
*arg
, const struct fuse_file_info
*f
)
375 arg
->open_flags
|= FOPEN_DIRECT_IO
;
378 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
380 if (f
->cache_readdir
) {
381 arg
->open_flags
|= FOPEN_CACHE_DIR
;
383 if (f
->nonseekable
) {
384 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
388 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
390 struct fuse_entry_out arg
;
391 size_t size
= sizeof(arg
);
393 memset(&arg
, 0, sizeof(arg
));
395 return send_reply_ok(req
, &arg
, size
);
398 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
399 const struct fuse_file_info
*f
)
401 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
402 size_t entrysize
= sizeof(struct fuse_entry_out
);
403 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*)buf
;
404 struct fuse_open_out
*oarg
= (struct fuse_open_out
*)(buf
+ entrysize
);
406 memset(buf
, 0, sizeof(buf
));
409 return send_reply_ok(req
, buf
, entrysize
+ sizeof(struct fuse_open_out
));
412 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
415 struct fuse_attr_out arg
;
416 size_t size
= sizeof(arg
);
418 memset(&arg
, 0, sizeof(arg
));
419 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
420 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
421 convert_stat(attr
, &arg
.attr
);
423 return send_reply_ok(req
, &arg
, size
);
426 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
428 return send_reply_ok(req
, linkname
, strlen(linkname
));
431 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
433 struct fuse_open_out arg
;
435 memset(&arg
, 0, sizeof(arg
));
437 return send_reply_ok(req
, &arg
, sizeof(arg
));
440 int fuse_reply_write(fuse_req_t req
, size_t count
)
442 struct fuse_write_out arg
;
444 memset(&arg
, 0, sizeof(arg
));
447 return send_reply_ok(req
, &arg
, sizeof(arg
));
450 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
452 return send_reply_ok(req
, buf
, size
);
455 static int fuse_send_data_iov_fallback(struct fuse_session
*se
,
456 struct fuse_chan
*ch
, struct iovec
*iov
,
457 int iov_count
, struct fuse_bufvec
*buf
,
460 /* Optimize common case */
461 if (buf
->count
== 1 && buf
->idx
== 0 && buf
->off
== 0 &&
462 !(buf
->buf
[0].flags
& FUSE_BUF_IS_FD
)) {
464 * FIXME: also avoid memory copy if there are multiple buffers
465 * but none of them contain an fd
468 iov
[iov_count
].iov_base
= buf
->buf
[0].mem
;
469 iov
[iov_count
].iov_len
= len
;
471 return fuse_send_msg(se
, ch
, iov
, iov_count
);
474 if (fuse_lowlevel_is_virtio(se
) && buf
->count
== 1 &&
475 buf
->buf
[0].flags
== (FUSE_BUF_IS_FD
| FUSE_BUF_FD_SEEK
)) {
476 return virtio_send_data_iov(se
, ch
, iov
, iov_count
, buf
, len
);
479 abort(); /* Will have taken vhost path */
483 static int fuse_send_data_iov(struct fuse_session
*se
, struct fuse_chan
*ch
,
484 struct iovec
*iov
, int iov_count
,
485 struct fuse_bufvec
*buf
)
487 size_t len
= fuse_buf_size(buf
);
489 return fuse_send_data_iov_fallback(se
, ch
, iov
, iov_count
, buf
, len
);
492 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
)
495 struct fuse_out_header out
= {
496 .unique
= req
->unique
,
500 iov
[0].iov_base
= &out
;
501 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
503 res
= fuse_send_data_iov(req
->se
, req
->ch
, iov
, 1, bufv
);
508 return fuse_reply_err(req
, res
);
512 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
514 struct fuse_statfs_out arg
;
515 size_t size
= sizeof(arg
);
517 memset(&arg
, 0, sizeof(arg
));
518 convert_statfs(stbuf
, &arg
.st
);
520 return send_reply_ok(req
, &arg
, size
);
523 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
525 struct fuse_getxattr_out arg
;
527 memset(&arg
, 0, sizeof(arg
));
530 return send_reply_ok(req
, &arg
, sizeof(arg
));
533 int fuse_reply_lock(fuse_req_t req
, const struct flock
*lock
)
535 struct fuse_lk_out arg
;
537 memset(&arg
, 0, sizeof(arg
));
538 arg
.lk
.type
= lock
->l_type
;
539 if (lock
->l_type
!= F_UNLCK
) {
540 arg
.lk
.start
= lock
->l_start
;
541 if (lock
->l_len
== 0) {
542 arg
.lk
.end
= OFFSET_MAX
;
544 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
547 arg
.lk
.pid
= lock
->l_pid
;
548 return send_reply_ok(req
, &arg
, sizeof(arg
));
551 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
553 struct fuse_bmap_out arg
;
555 memset(&arg
, 0, sizeof(arg
));
558 return send_reply_ok(req
, &arg
, sizeof(arg
));
561 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
564 struct fuse_ioctl_iovec
*fiov
;
567 fiov
= g_try_new(struct fuse_ioctl_iovec
, count
);
572 for (i
= 0; i
< count
; i
++) {
573 fiov
[i
].base
= (uintptr_t)iov
[i
].iov_base
;
574 fiov
[i
].len
= iov
[i
].iov_len
;
580 int fuse_reply_ioctl_retry(fuse_req_t req
, const struct iovec
*in_iov
,
581 size_t in_count
, const struct iovec
*out_iov
,
584 struct fuse_ioctl_out arg
;
585 g_autofree
struct fuse_ioctl_iovec
*in_fiov
= NULL
;
586 g_autofree
struct fuse_ioctl_iovec
*out_fiov
= NULL
;
590 memset(&arg
, 0, sizeof(arg
));
591 arg
.flags
|= FUSE_IOCTL_RETRY
;
592 arg
.in_iovs
= in_count
;
593 arg
.out_iovs
= out_count
;
594 iov
[count
].iov_base
= &arg
;
595 iov
[count
].iov_len
= sizeof(arg
);
598 /* Can't handle non-compat 64bit ioctls on 32bit */
599 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
600 return fuse_reply_err(req
, EINVAL
);
604 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
606 return fuse_reply_err(req
, ENOMEM
);
609 iov
[count
].iov_base
= (void *)in_fiov
;
610 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
614 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
616 return fuse_reply_err(req
, ENOMEM
);
619 iov
[count
].iov_base
= (void *)out_fiov
;
620 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
624 return send_reply_iov(req
, 0, iov
, count
);
627 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
629 struct fuse_ioctl_out arg
;
633 memset(&arg
, 0, sizeof(arg
));
635 iov
[count
].iov_base
= &arg
;
636 iov
[count
].iov_len
= sizeof(arg
);
640 iov
[count
].iov_base
= (char *)buf
;
641 iov
[count
].iov_len
= size
;
645 return send_reply_iov(req
, 0, iov
, count
);
648 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
651 g_autofree
struct iovec
*padded_iov
= NULL
;
652 struct fuse_ioctl_out arg
;
654 padded_iov
= g_try_new(struct iovec
, count
+ 2);
655 if (padded_iov
== NULL
) {
656 return fuse_reply_err(req
, ENOMEM
);
659 memset(&arg
, 0, sizeof(arg
));
661 padded_iov
[1].iov_base
= &arg
;
662 padded_iov
[1].iov_len
= sizeof(arg
);
664 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
666 return send_reply_iov(req
, 0, padded_iov
, count
+ 2);
669 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
671 struct fuse_poll_out arg
;
673 memset(&arg
, 0, sizeof(arg
));
674 arg
.revents
= revents
;
676 return send_reply_ok(req
, &arg
, sizeof(arg
));
679 int fuse_reply_lseek(fuse_req_t req
, off_t off
)
681 struct fuse_lseek_out arg
;
683 memset(&arg
, 0, sizeof(arg
));
686 return send_reply_ok(req
, &arg
, sizeof(arg
));
689 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
,
690 struct fuse_mbuf_iter
*iter
)
692 const char *name
= fuse_mbuf_iter_advance_str(iter
);
694 fuse_reply_err(req
, EINVAL
);
698 if (req
->se
->op
.lookup
) {
699 req
->se
->op
.lookup(req
, nodeid
, name
);
701 fuse_reply_err(req
, ENOSYS
);
705 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
,
706 struct fuse_mbuf_iter
*iter
)
708 struct fuse_forget_in
*arg
;
710 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
712 fuse_reply_err(req
, EINVAL
);
716 if (req
->se
->op
.forget
) {
717 req
->se
->op
.forget(req
, nodeid
, arg
->nlookup
);
719 fuse_reply_none(req
);
723 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
724 struct fuse_mbuf_iter
*iter
)
726 struct fuse_batch_forget_in
*arg
;
727 struct fuse_forget_data
*forgets
;
732 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
734 fuse_reply_none(req
);
739 * Prevent integer overflow. The compiler emits the following warning
740 * unless we use the scount local variable:
742 * error: comparison is always false due to limited range of data type
743 * [-Werror=type-limits]
745 * This may be true on 64-bit hosts but we need this check for 32-bit
749 if (scount
> SIZE_MAX
/ sizeof(forgets
[0])) {
750 fuse_reply_none(req
);
754 forgets
= fuse_mbuf_iter_advance(iter
, arg
->count
* sizeof(forgets
[0]));
756 fuse_reply_none(req
);
760 if (req
->se
->op
.forget_multi
) {
761 req
->se
->op
.forget_multi(req
, arg
->count
, forgets
);
762 } else if (req
->se
->op
.forget
) {
765 for (i
= 0; i
< arg
->count
; i
++) {
766 struct fuse_req
*dummy_req
;
768 dummy_req
= fuse_ll_alloc_req(req
->se
);
769 if (dummy_req
== NULL
) {
773 dummy_req
->unique
= req
->unique
;
774 dummy_req
->ctx
= req
->ctx
;
775 dummy_req
->ch
= NULL
;
777 req
->se
->op
.forget(dummy_req
, forgets
[i
].ino
, forgets
[i
].nlookup
);
779 fuse_reply_none(req
);
781 fuse_reply_none(req
);
785 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
,
786 struct fuse_mbuf_iter
*iter
)
788 struct fuse_file_info
*fip
= NULL
;
789 struct fuse_file_info fi
;
791 struct fuse_getattr_in
*arg
;
793 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
795 fuse_reply_err(req
, EINVAL
);
799 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
800 memset(&fi
, 0, sizeof(fi
));
805 if (req
->se
->op
.getattr
) {
806 req
->se
->op
.getattr(req
, nodeid
, fip
);
808 fuse_reply_err(req
, ENOSYS
);
812 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
,
813 struct fuse_mbuf_iter
*iter
)
815 if (req
->se
->op
.setattr
) {
816 struct fuse_setattr_in
*arg
;
817 struct fuse_file_info
*fi
= NULL
;
818 struct fuse_file_info fi_store
;
821 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
823 fuse_reply_err(req
, EINVAL
);
827 memset(&stbuf
, 0, sizeof(stbuf
));
828 convert_attr(arg
, &stbuf
);
829 if (arg
->valid
& FATTR_FH
) {
830 arg
->valid
&= ~FATTR_FH
;
831 memset(&fi_store
, 0, sizeof(fi_store
));
835 arg
->valid
&= FUSE_SET_ATTR_MODE
| FUSE_SET_ATTR_UID
|
836 FUSE_SET_ATTR_GID
| FUSE_SET_ATTR_SIZE
|
837 FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
|
838 FUSE_SET_ATTR_ATIME_NOW
| FUSE_SET_ATTR_MTIME_NOW
|
839 FUSE_SET_ATTR_CTIME
| FUSE_SET_ATTR_KILL_SUIDGID
;
841 req
->se
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
843 fuse_reply_err(req
, ENOSYS
);
847 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
,
848 struct fuse_mbuf_iter
*iter
)
850 struct fuse_access_in
*arg
;
852 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
854 fuse_reply_err(req
, EINVAL
);
858 if (req
->se
->op
.access
) {
859 req
->se
->op
.access(req
, nodeid
, arg
->mask
);
861 fuse_reply_err(req
, ENOSYS
);
865 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
,
866 struct fuse_mbuf_iter
*iter
)
870 if (req
->se
->op
.readlink
) {
871 req
->se
->op
.readlink(req
, nodeid
);
873 fuse_reply_err(req
, ENOSYS
);
877 static int parse_secctx_fill_req(fuse_req_t req
, struct fuse_mbuf_iter
*iter
)
879 struct fuse_secctx_header
*fsecctx_header
;
880 struct fuse_secctx
*fsecctx
;
884 fsecctx_header
= fuse_mbuf_iter_advance(iter
, sizeof(*fsecctx_header
));
885 if (!fsecctx_header
) {
890 * As of now maximum of one security context is supported. It can
891 * change in future though.
893 if (fsecctx_header
->nr_secctx
> 1) {
897 /* No security context sent. Maybe no LSM supports it */
898 if (!fsecctx_header
->nr_secctx
) {
902 fsecctx
= fuse_mbuf_iter_advance(iter
, sizeof(*fsecctx
));
907 /* struct fsecctx with zero sized context is not expected */
908 if (!fsecctx
->size
) {
911 name
= fuse_mbuf_iter_advance_str(iter
);
916 secctx
= fuse_mbuf_iter_advance(iter
, fsecctx
->size
);
921 req
->secctx
.name
= name
;
922 req
->secctx
.ctx
= secctx
;
923 req
->secctx
.ctxlen
= fsecctx
->size
;
927 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
,
928 struct fuse_mbuf_iter
*iter
)
930 struct fuse_mknod_in
*arg
;
932 bool secctx_enabled
= req
->se
->conn
.want
& FUSE_CAP_SECURITY_CTX
;
935 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
936 name
= fuse_mbuf_iter_advance_str(iter
);
938 fuse_reply_err(req
, EINVAL
);
942 req
->ctx
.umask
= arg
->umask
;
944 if (secctx_enabled
) {
945 err
= parse_secctx_fill_req(req
, iter
);
947 fuse_reply_err(req
, -err
);
952 if (req
->se
->op
.mknod
) {
953 req
->se
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
955 fuse_reply_err(req
, ENOSYS
);
959 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
,
960 struct fuse_mbuf_iter
*iter
)
962 struct fuse_mkdir_in
*arg
;
964 bool secctx_enabled
= req
->se
->conn
.want
& FUSE_CAP_SECURITY_CTX
;
967 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
968 name
= fuse_mbuf_iter_advance_str(iter
);
970 fuse_reply_err(req
, EINVAL
);
974 req
->ctx
.umask
= arg
->umask
;
976 if (secctx_enabled
) {
977 err
= parse_secctx_fill_req(req
, iter
);
979 fuse_reply_err(req
, err
);
984 if (req
->se
->op
.mkdir
) {
985 req
->se
->op
.mkdir(req
, nodeid
, name
, arg
->mode
);
987 fuse_reply_err(req
, ENOSYS
);
991 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
,
992 struct fuse_mbuf_iter
*iter
)
994 const char *name
= fuse_mbuf_iter_advance_str(iter
);
997 fuse_reply_err(req
, EINVAL
);
1001 if (req
->se
->op
.unlink
) {
1002 req
->se
->op
.unlink(req
, nodeid
, name
);
1004 fuse_reply_err(req
, ENOSYS
);
1008 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
,
1009 struct fuse_mbuf_iter
*iter
)
1011 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1014 fuse_reply_err(req
, EINVAL
);
1018 if (req
->se
->op
.rmdir
) {
1019 req
->se
->op
.rmdir(req
, nodeid
, name
);
1021 fuse_reply_err(req
, ENOSYS
);
1025 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
,
1026 struct fuse_mbuf_iter
*iter
)
1028 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1029 const char *linkname
= fuse_mbuf_iter_advance_str(iter
);
1030 bool secctx_enabled
= req
->se
->conn
.want
& FUSE_CAP_SECURITY_CTX
;
1033 if (!name
|| !linkname
) {
1034 fuse_reply_err(req
, EINVAL
);
1038 if (secctx_enabled
) {
1039 err
= parse_secctx_fill_req(req
, iter
);
1041 fuse_reply_err(req
, err
);
1046 if (req
->se
->op
.symlink
) {
1047 req
->se
->op
.symlink(req
, linkname
, nodeid
, name
);
1049 fuse_reply_err(req
, ENOSYS
);
1053 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
,
1054 struct fuse_mbuf_iter
*iter
)
1056 struct fuse_rename_in
*arg
;
1057 const char *oldname
;
1058 const char *newname
;
1060 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1061 oldname
= fuse_mbuf_iter_advance_str(iter
);
1062 newname
= fuse_mbuf_iter_advance_str(iter
);
1063 if (!arg
|| !oldname
|| !newname
) {
1064 fuse_reply_err(req
, EINVAL
);
1068 if (req
->se
->op
.rename
) {
1069 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
, 0);
1071 fuse_reply_err(req
, ENOSYS
);
1075 static void do_rename2(fuse_req_t req
, fuse_ino_t nodeid
,
1076 struct fuse_mbuf_iter
*iter
)
1078 struct fuse_rename2_in
*arg
;
1079 const char *oldname
;
1080 const char *newname
;
1082 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1083 oldname
= fuse_mbuf_iter_advance_str(iter
);
1084 newname
= fuse_mbuf_iter_advance_str(iter
);
1085 if (!arg
|| !oldname
|| !newname
) {
1086 fuse_reply_err(req
, EINVAL
);
1090 if (req
->se
->op
.rename
) {
1091 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
,
1094 fuse_reply_err(req
, ENOSYS
);
1098 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
,
1099 struct fuse_mbuf_iter
*iter
)
1101 struct fuse_link_in
*arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1102 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1104 if (!arg
|| !name
) {
1105 fuse_reply_err(req
, EINVAL
);
1109 if (req
->se
->op
.link
) {
1110 req
->se
->op
.link(req
, arg
->oldnodeid
, nodeid
, name
);
1112 fuse_reply_err(req
, ENOSYS
);
1116 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
,
1117 struct fuse_mbuf_iter
*iter
)
1119 bool secctx_enabled
= req
->se
->conn
.want
& FUSE_CAP_SECURITY_CTX
;
1121 if (req
->se
->op
.create
) {
1122 struct fuse_create_in
*arg
;
1123 struct fuse_file_info fi
;
1126 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1127 name
= fuse_mbuf_iter_advance_str(iter
);
1128 if (!arg
|| !name
) {
1129 fuse_reply_err(req
, EINVAL
);
1133 if (secctx_enabled
) {
1135 err
= parse_secctx_fill_req(req
, iter
);
1137 fuse_reply_err(req
, err
);
1142 memset(&fi
, 0, sizeof(fi
));
1143 fi
.flags
= arg
->flags
;
1144 fi
.kill_priv
= arg
->open_flags
& FUSE_OPEN_KILL_SUIDGID
;
1146 req
->ctx
.umask
= arg
->umask
;
1148 req
->se
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
1150 fuse_reply_err(req
, ENOSYS
);
1154 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
,
1155 struct fuse_mbuf_iter
*iter
)
1157 struct fuse_open_in
*arg
;
1158 struct fuse_file_info fi
;
1160 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1162 fuse_reply_err(req
, EINVAL
);
1166 /* File creation is handled by do_create() or do_mknod() */
1167 if (arg
->flags
& (O_CREAT
| O_TMPFILE
)) {
1168 fuse_reply_err(req
, EINVAL
);
1172 memset(&fi
, 0, sizeof(fi
));
1173 fi
.flags
= arg
->flags
;
1174 fi
.kill_priv
= arg
->open_flags
& FUSE_OPEN_KILL_SUIDGID
;
1176 if (req
->se
->op
.open
) {
1177 req
->se
->op
.open(req
, nodeid
, &fi
);
1179 fuse_reply_open(req
, &fi
);
1183 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
,
1184 struct fuse_mbuf_iter
*iter
)
1186 if (req
->se
->op
.read
) {
1187 struct fuse_read_in
*arg
;
1188 struct fuse_file_info fi
;
1190 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1192 fuse_reply_err(req
, EINVAL
);
1196 memset(&fi
, 0, sizeof(fi
));
1198 fi
.lock_owner
= arg
->lock_owner
;
1199 fi
.flags
= arg
->flags
;
1200 req
->se
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1202 fuse_reply_err(req
, ENOSYS
);
1206 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
,
1207 struct fuse_mbuf_iter
*iter
)
1209 struct fuse_write_in
*arg
;
1210 struct fuse_file_info fi
;
1213 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1215 fuse_reply_err(req
, EINVAL
);
1219 param
= fuse_mbuf_iter_advance(iter
, arg
->size
);
1221 fuse_reply_err(req
, EINVAL
);
1225 memset(&fi
, 0, sizeof(fi
));
1227 fi
.writepage
= (arg
->write_flags
& FUSE_WRITE_CACHE
) != 0;
1228 fi
.kill_priv
= !!(arg
->write_flags
& FUSE_WRITE_KILL_PRIV
);
1230 fi
.lock_owner
= arg
->lock_owner
;
1231 fi
.flags
= arg
->flags
;
1233 if (req
->se
->op
.write
) {
1234 req
->se
->op
.write(req
, nodeid
, param
, arg
->size
, arg
->offset
, &fi
);
1236 fuse_reply_err(req
, ENOSYS
);
1240 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
,
1241 struct fuse_mbuf_iter
*iter
, struct fuse_bufvec
*ibufv
)
1243 struct fuse_session
*se
= req
->se
;
1244 struct fuse_bufvec
*pbufv
= ibufv
;
1245 struct fuse_bufvec tmpbufv
= {
1246 .buf
[0] = ibufv
->buf
[0],
1249 struct fuse_write_in
*arg
;
1250 size_t arg_size
= sizeof(*arg
);
1251 struct fuse_file_info fi
;
1253 memset(&fi
, 0, sizeof(fi
));
1255 arg
= fuse_mbuf_iter_advance(iter
, arg_size
);
1257 fuse_reply_err(req
, EINVAL
);
1261 fi
.lock_owner
= arg
->lock_owner
;
1262 fi
.flags
= arg
->flags
;
1264 fi
.writepage
= !!(arg
->write_flags
& FUSE_WRITE_CACHE
);
1265 fi
.kill_priv
= !!(arg
->write_flags
& FUSE_WRITE_KILL_PRIV
);
1267 if (ibufv
->count
== 1) {
1268 assert(!(tmpbufv
.buf
[0].flags
& FUSE_BUF_IS_FD
));
1269 tmpbufv
.buf
[0].mem
= ((char *)arg
) + arg_size
;
1270 tmpbufv
.buf
[0].size
-= sizeof(struct fuse_in_header
) + arg_size
;
1274 * Input bufv contains the headers in the first element
1275 * and the data in the rest, we need to skip that first element
1277 ibufv
->buf
[0].size
= 0;
1280 if (fuse_buf_size(pbufv
) != arg
->size
) {
1281 fuse_log(FUSE_LOG_ERR
,
1282 "fuse: do_write_buf: buffer size doesn't match arg->size\n");
1283 fuse_reply_err(req
, EIO
);
1287 se
->op
.write_buf(req
, nodeid
, pbufv
, arg
->offset
, &fi
);
1290 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
,
1291 struct fuse_mbuf_iter
*iter
)
1293 struct fuse_flush_in
*arg
;
1294 struct fuse_file_info fi
;
1296 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1298 fuse_reply_err(req
, EINVAL
);
1302 memset(&fi
, 0, sizeof(fi
));
1305 fi
.lock_owner
= arg
->lock_owner
;
1307 if (req
->se
->op
.flush
) {
1308 req
->se
->op
.flush(req
, nodeid
, &fi
);
1310 fuse_reply_err(req
, ENOSYS
);
1314 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
,
1315 struct fuse_mbuf_iter
*iter
)
1317 struct fuse_release_in
*arg
;
1318 struct fuse_file_info fi
;
1320 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1322 fuse_reply_err(req
, EINVAL
);
1326 memset(&fi
, 0, sizeof(fi
));
1327 fi
.flags
= arg
->flags
;
1329 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1330 fi
.lock_owner
= arg
->lock_owner
;
1332 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1333 fi
.flock_release
= 1;
1336 if (req
->se
->op
.release
) {
1337 req
->se
->op
.release(req
, nodeid
, &fi
);
1339 fuse_reply_err(req
, 0);
1343 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
,
1344 struct fuse_mbuf_iter
*iter
)
1346 struct fuse_fsync_in
*arg
;
1347 struct fuse_file_info fi
;
1350 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1352 fuse_reply_err(req
, EINVAL
);
1355 datasync
= arg
->fsync_flags
& 1;
1357 memset(&fi
, 0, sizeof(fi
));
1360 if (req
->se
->op
.fsync
) {
1361 if (fi
.fh
== (uint64_t)-1) {
1362 req
->se
->op
.fsync(req
, nodeid
, datasync
, NULL
);
1364 req
->se
->op
.fsync(req
, nodeid
, datasync
, &fi
);
1367 fuse_reply_err(req
, ENOSYS
);
1371 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
,
1372 struct fuse_mbuf_iter
*iter
)
1374 struct fuse_open_in
*arg
;
1375 struct fuse_file_info fi
;
1377 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1379 fuse_reply_err(req
, EINVAL
);
1383 memset(&fi
, 0, sizeof(fi
));
1384 fi
.flags
= arg
->flags
;
1386 if (req
->se
->op
.opendir
) {
1387 req
->se
->op
.opendir(req
, nodeid
, &fi
);
1389 fuse_reply_open(req
, &fi
);
1393 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
,
1394 struct fuse_mbuf_iter
*iter
)
1396 struct fuse_read_in
*arg
;
1397 struct fuse_file_info fi
;
1399 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1401 fuse_reply_err(req
, EINVAL
);
1405 memset(&fi
, 0, sizeof(fi
));
1408 if (req
->se
->op
.readdir
) {
1409 req
->se
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1411 fuse_reply_err(req
, ENOSYS
);
1415 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
,
1416 struct fuse_mbuf_iter
*iter
)
1418 struct fuse_read_in
*arg
;
1419 struct fuse_file_info fi
;
1421 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1423 fuse_reply_err(req
, EINVAL
);
1427 memset(&fi
, 0, sizeof(fi
));
1430 if (req
->se
->op
.readdirplus
) {
1431 req
->se
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1433 fuse_reply_err(req
, ENOSYS
);
1437 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
,
1438 struct fuse_mbuf_iter
*iter
)
1440 struct fuse_release_in
*arg
;
1441 struct fuse_file_info fi
;
1443 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1445 fuse_reply_err(req
, EINVAL
);
1449 memset(&fi
, 0, sizeof(fi
));
1450 fi
.flags
= arg
->flags
;
1453 if (req
->se
->op
.releasedir
) {
1454 req
->se
->op
.releasedir(req
, nodeid
, &fi
);
1456 fuse_reply_err(req
, 0);
1460 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
,
1461 struct fuse_mbuf_iter
*iter
)
1463 struct fuse_fsync_in
*arg
;
1464 struct fuse_file_info fi
;
1467 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1469 fuse_reply_err(req
, EINVAL
);
1472 datasync
= arg
->fsync_flags
& 1;
1474 memset(&fi
, 0, sizeof(fi
));
1477 if (req
->se
->op
.fsyncdir
) {
1478 req
->se
->op
.fsyncdir(req
, nodeid
, datasync
, &fi
);
1480 fuse_reply_err(req
, ENOSYS
);
1484 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
,
1485 struct fuse_mbuf_iter
*iter
)
1490 if (req
->se
->op
.statfs
) {
1491 req
->se
->op
.statfs(req
, nodeid
);
1493 struct statvfs buf
= {
1497 fuse_reply_statfs(req
, &buf
);
1501 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1502 struct fuse_mbuf_iter
*iter
)
1504 struct fuse_setxattr_in
*arg
;
1507 bool setxattr_ext
= req
->se
->conn
.want
& FUSE_CAP_SETXATTR_EXT
;
1510 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1512 arg
= fuse_mbuf_iter_advance(iter
, FUSE_COMPAT_SETXATTR_IN_SIZE
);
1514 name
= fuse_mbuf_iter_advance_str(iter
);
1515 if (!arg
|| !name
) {
1516 fuse_reply_err(req
, EINVAL
);
1520 value
= fuse_mbuf_iter_advance(iter
, arg
->size
);
1522 fuse_reply_err(req
, EINVAL
);
1526 if (req
->se
->op
.setxattr
) {
1527 uint32_t setxattr_flags
= setxattr_ext
? arg
->setxattr_flags
: 0;
1528 req
->se
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
, arg
->flags
,
1531 fuse_reply_err(req
, ENOSYS
);
1535 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1536 struct fuse_mbuf_iter
*iter
)
1538 struct fuse_getxattr_in
*arg
;
1541 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1542 name
= fuse_mbuf_iter_advance_str(iter
);
1543 if (!arg
|| !name
) {
1544 fuse_reply_err(req
, EINVAL
);
1548 if (req
->se
->op
.getxattr
) {
1549 req
->se
->op
.getxattr(req
, nodeid
, name
, arg
->size
);
1551 fuse_reply_err(req
, ENOSYS
);
1555 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
,
1556 struct fuse_mbuf_iter
*iter
)
1558 struct fuse_getxattr_in
*arg
;
1560 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1562 fuse_reply_err(req
, EINVAL
);
1566 if (req
->se
->op
.listxattr
) {
1567 req
->se
->op
.listxattr(req
, nodeid
, arg
->size
);
1569 fuse_reply_err(req
, ENOSYS
);
1573 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
,
1574 struct fuse_mbuf_iter
*iter
)
1576 const char *name
= fuse_mbuf_iter_advance_str(iter
);
1579 fuse_reply_err(req
, EINVAL
);
1583 if (req
->se
->op
.removexattr
) {
1584 req
->se
->op
.removexattr(req
, nodeid
, name
);
1586 fuse_reply_err(req
, ENOSYS
);
1590 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1591 struct flock
*flock
)
1593 memset(flock
, 0, sizeof(struct flock
));
1594 flock
->l_type
= fl
->type
;
1595 flock
->l_whence
= SEEK_SET
;
1596 flock
->l_start
= fl
->start
;
1597 if (fl
->end
== OFFSET_MAX
) {
1600 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1602 flock
->l_pid
= fl
->pid
;
1605 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
,
1606 struct fuse_mbuf_iter
*iter
)
1608 struct fuse_lk_in
*arg
;
1609 struct fuse_file_info fi
;
1612 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1614 fuse_reply_err(req
, EINVAL
);
1618 memset(&fi
, 0, sizeof(fi
));
1620 fi
.lock_owner
= arg
->owner
;
1622 convert_fuse_file_lock(&arg
->lk
, &flock
);
1623 if (req
->se
->op
.getlk
) {
1624 req
->se
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1626 fuse_reply_err(req
, ENOSYS
);
1630 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1631 struct fuse_mbuf_iter
*iter
, int sleep
)
1633 struct fuse_lk_in
*arg
;
1634 struct fuse_file_info fi
;
1637 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1639 fuse_reply_err(req
, EINVAL
);
1643 memset(&fi
, 0, sizeof(fi
));
1645 fi
.lock_owner
= arg
->owner
;
1647 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1650 switch (arg
->lk
.type
) {
1665 if (req
->se
->op
.flock
) {
1666 req
->se
->op
.flock(req
, nodeid
, &fi
, op
);
1668 fuse_reply_err(req
, ENOSYS
);
1671 convert_fuse_file_lock(&arg
->lk
, &flock
);
1672 if (req
->se
->op
.setlk
) {
1673 req
->se
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1675 fuse_reply_err(req
, ENOSYS
);
1680 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
,
1681 struct fuse_mbuf_iter
*iter
)
1683 do_setlk_common(req
, nodeid
, iter
, 0);
1686 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
,
1687 struct fuse_mbuf_iter
*iter
)
1689 do_setlk_common(req
, nodeid
, iter
, 1);
1692 static int find_interrupted(struct fuse_session
*se
, struct fuse_req
*req
)
1694 struct fuse_req
*curr
;
1696 for (curr
= se
->list
.next
; curr
!= &se
->list
; curr
= curr
->next
) {
1697 if (curr
->unique
== req
->u
.i
.unique
) {
1698 fuse_interrupt_func_t func
;
1702 pthread_mutex_unlock(&se
->lock
);
1704 /* Ugh, ugly locking */
1705 pthread_mutex_lock(&curr
->lock
);
1706 pthread_mutex_lock(&se
->lock
);
1707 curr
->interrupted
= 1;
1708 func
= curr
->u
.ni
.func
;
1709 data
= curr
->u
.ni
.data
;
1710 pthread_mutex_unlock(&se
->lock
);
1714 pthread_mutex_unlock(&curr
->lock
);
1716 pthread_mutex_lock(&se
->lock
);
1725 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1726 curr
= curr
->next
) {
1727 if (curr
->u
.i
.unique
== req
->u
.i
.unique
) {
1734 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
,
1735 struct fuse_mbuf_iter
*iter
)
1737 struct fuse_interrupt_in
*arg
;
1738 struct fuse_session
*se
= req
->se
;
1742 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1744 fuse_reply_err(req
, EINVAL
);
1748 fuse_log(FUSE_LOG_DEBUG
, "INTERRUPT: %llu\n",
1749 (unsigned long long)arg
->unique
);
1751 req
->u
.i
.unique
= arg
->unique
;
1753 pthread_mutex_lock(&se
->lock
);
1754 if (find_interrupted(se
, req
)) {
1757 list_add_req(req
, &se
->interrupts
);
1759 pthread_mutex_unlock(&se
->lock
);
1762 static struct fuse_req
*check_interrupt(struct fuse_session
*se
,
1763 struct fuse_req
*req
)
1765 struct fuse_req
*curr
;
1767 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1768 curr
= curr
->next
) {
1769 if (curr
->u
.i
.unique
== req
->unique
) {
1770 req
->interrupted
= 1;
1776 curr
= se
->interrupts
.next
;
1777 if (curr
!= &se
->interrupts
) {
1779 list_init_req(curr
);
1786 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
,
1787 struct fuse_mbuf_iter
*iter
)
1789 struct fuse_bmap_in
*arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1792 fuse_reply_err(req
, EINVAL
);
1796 if (req
->se
->op
.bmap
) {
1797 req
->se
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1799 fuse_reply_err(req
, ENOSYS
);
1803 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
,
1804 struct fuse_mbuf_iter
*iter
)
1806 struct fuse_ioctl_in
*arg
;
1808 void *in_buf
= NULL
;
1809 struct fuse_file_info fi
;
1811 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1813 fuse_reply_err(req
, EINVAL
);
1818 if (flags
& FUSE_IOCTL_DIR
&& !(req
->se
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1819 fuse_reply_err(req
, ENOTTY
);
1824 in_buf
= fuse_mbuf_iter_advance(iter
, arg
->in_size
);
1826 fuse_reply_err(req
, EINVAL
);
1831 memset(&fi
, 0, sizeof(fi
));
1834 if (sizeof(void *) == 4 && !(flags
& FUSE_IOCTL_32BIT
)) {
1835 req
->ioctl_64bit
= 1;
1838 if (req
->se
->op
.ioctl
) {
1839 req
->se
->op
.ioctl(req
, nodeid
, arg
->cmd
, (void *)(uintptr_t)arg
->arg
,
1840 &fi
, flags
, in_buf
, arg
->in_size
, arg
->out_size
);
1842 fuse_reply_err(req
, ENOSYS
);
1846 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1851 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
,
1852 struct fuse_mbuf_iter
*iter
)
1854 struct fuse_poll_in
*arg
;
1855 struct fuse_file_info fi
;
1857 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1859 fuse_reply_err(req
, EINVAL
);
1863 memset(&fi
, 0, sizeof(fi
));
1865 fi
.poll_events
= arg
->events
;
1867 if (req
->se
->op
.poll
) {
1868 struct fuse_pollhandle
*ph
= NULL
;
1870 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1871 ph
= malloc(sizeof(struct fuse_pollhandle
));
1873 fuse_reply_err(req
, ENOMEM
);
1880 req
->se
->op
.poll(req
, nodeid
, &fi
, ph
);
1882 fuse_reply_err(req
, ENOSYS
);
1886 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
,
1887 struct fuse_mbuf_iter
*iter
)
1889 struct fuse_fallocate_in
*arg
;
1890 struct fuse_file_info fi
;
1892 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1894 fuse_reply_err(req
, EINVAL
);
1898 memset(&fi
, 0, sizeof(fi
));
1901 if (req
->se
->op
.fallocate
) {
1902 req
->se
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
,
1905 fuse_reply_err(req
, ENOSYS
);
1909 static void do_copy_file_range(fuse_req_t req
, fuse_ino_t nodeid_in
,
1910 struct fuse_mbuf_iter
*iter
)
1912 struct fuse_copy_file_range_in
*arg
;
1913 struct fuse_file_info fi_in
, fi_out
;
1915 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1917 fuse_reply_err(req
, EINVAL
);
1921 memset(&fi_in
, 0, sizeof(fi_in
));
1922 fi_in
.fh
= arg
->fh_in
;
1924 memset(&fi_out
, 0, sizeof(fi_out
));
1925 fi_out
.fh
= arg
->fh_out
;
1928 if (req
->se
->op
.copy_file_range
) {
1929 req
->se
->op
.copy_file_range(req
, nodeid_in
, arg
->off_in
, &fi_in
,
1930 arg
->nodeid_out
, arg
->off_out
, &fi_out
,
1931 arg
->len
, arg
->flags
);
1933 fuse_reply_err(req
, ENOSYS
);
1937 static void do_lseek(fuse_req_t req
, fuse_ino_t nodeid
,
1938 struct fuse_mbuf_iter
*iter
)
1940 struct fuse_lseek_in
*arg
;
1941 struct fuse_file_info fi
;
1943 arg
= fuse_mbuf_iter_advance(iter
, sizeof(*arg
));
1945 fuse_reply_err(req
, EINVAL
);
1948 memset(&fi
, 0, sizeof(fi
));
1951 if (req
->se
->op
.lseek
) {
1952 req
->se
->op
.lseek(req
, nodeid
, arg
->offset
, arg
->whence
, &fi
);
1954 fuse_reply_err(req
, ENOSYS
);
1958 static void do_syncfs(fuse_req_t req
, fuse_ino_t nodeid
,
1959 struct fuse_mbuf_iter
*iter
)
1961 if (req
->se
->op
.syncfs
) {
1962 req
->se
->op
.syncfs(req
, nodeid
);
1964 fuse_reply_err(req
, ENOSYS
);
1968 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
,
1969 struct fuse_mbuf_iter
*iter
)
1971 size_t compat_size
= offsetof(struct fuse_init_in
, max_readahead
);
1972 size_t compat2_size
= offsetof(struct fuse_init_in
, flags
) +
1974 /* Fuse structure extended with minor version 36 */
1975 size_t compat3_size
= endof(struct fuse_init_in
, unused
);
1976 struct fuse_init_in
*arg
;
1977 struct fuse_init_out outarg
;
1978 struct fuse_session
*se
= req
->se
;
1979 size_t bufsize
= se
->bufsize
;
1980 size_t outargsize
= sizeof(outarg
);
1985 /* First consume the old fields... */
1986 arg
= fuse_mbuf_iter_advance(iter
, compat_size
);
1988 fuse_reply_err(req
, EINVAL
);
1992 /* ...and now consume the new fields. */
1993 if (arg
->major
== 7 && arg
->minor
>= 6) {
1994 if (!fuse_mbuf_iter_advance(iter
, compat2_size
- compat_size
)) {
1995 fuse_reply_err(req
, EINVAL
);
1998 flags
|= arg
->flags
;
2002 * fuse_init_in was extended again with minor version 36. Just read
2003 * current known size of fuse_init so that future extension and
2004 * header rebase does not cause breakage.
2006 if (sizeof(*arg
) > compat2_size
&& (arg
->flags
& FUSE_INIT_EXT
)) {
2007 if (!fuse_mbuf_iter_advance(iter
, compat3_size
- compat2_size
)) {
2008 fuse_reply_err(req
, EINVAL
);
2011 flags
|= (uint64_t) arg
->flags2
<< 32;
2014 fuse_log(FUSE_LOG_DEBUG
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
2015 if (arg
->major
== 7 && arg
->minor
>= 6) {
2016 fuse_log(FUSE_LOG_DEBUG
, "flags=0x%016" PRIx64
"\n", flags
);
2017 fuse_log(FUSE_LOG_DEBUG
, "max_readahead=0x%08x\n", arg
->max_readahead
);
2019 se
->conn
.proto_major
= arg
->major
;
2020 se
->conn
.proto_minor
= arg
->minor
;
2021 se
->conn
.capable
= 0;
2024 memset(&outarg
, 0, sizeof(outarg
));
2025 outarg
.major
= FUSE_KERNEL_VERSION
;
2026 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
2028 if (arg
->major
< 7 || (arg
->major
== 7 && arg
->minor
< 31)) {
2029 fuse_log(FUSE_LOG_ERR
, "fuse: unsupported protocol version: %u.%u\n",
2030 arg
->major
, arg
->minor
);
2031 fuse_reply_err(req
, EPROTO
);
2035 if (arg
->major
> 7) {
2036 /* Wait for a second INIT request with a 7.X version */
2037 send_reply_ok(req
, &outarg
, sizeof(outarg
));
2041 if (arg
->max_readahead
< se
->conn
.max_readahead
) {
2042 se
->conn
.max_readahead
= arg
->max_readahead
;
2044 if (flags
& FUSE_ASYNC_READ
) {
2045 se
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
2047 if (flags
& FUSE_POSIX_LOCKS
) {
2048 se
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
2050 if (flags
& FUSE_ATOMIC_O_TRUNC
) {
2051 se
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
2053 if (flags
& FUSE_EXPORT_SUPPORT
) {
2054 se
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
2056 if (flags
& FUSE_DONT_MASK
) {
2057 se
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
2059 if (flags
& FUSE_FLOCK_LOCKS
) {
2060 se
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
2062 if (flags
& FUSE_AUTO_INVAL_DATA
) {
2063 se
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
2065 if (flags
& FUSE_DO_READDIRPLUS
) {
2066 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
2068 if (flags
& FUSE_READDIRPLUS_AUTO
) {
2069 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
2071 if (flags
& FUSE_ASYNC_DIO
) {
2072 se
->conn
.capable
|= FUSE_CAP_ASYNC_DIO
;
2074 if (flags
& FUSE_WRITEBACK_CACHE
) {
2075 se
->conn
.capable
|= FUSE_CAP_WRITEBACK_CACHE
;
2077 if (flags
& FUSE_NO_OPEN_SUPPORT
) {
2078 se
->conn
.capable
|= FUSE_CAP_NO_OPEN_SUPPORT
;
2080 if (flags
& FUSE_PARALLEL_DIROPS
) {
2081 se
->conn
.capable
|= FUSE_CAP_PARALLEL_DIROPS
;
2083 if (flags
& FUSE_POSIX_ACL
) {
2084 se
->conn
.capable
|= FUSE_CAP_POSIX_ACL
;
2086 if (flags
& FUSE_HANDLE_KILLPRIV
) {
2087 se
->conn
.capable
|= FUSE_CAP_HANDLE_KILLPRIV
;
2089 if (flags
& FUSE_NO_OPENDIR_SUPPORT
) {
2090 se
->conn
.capable
|= FUSE_CAP_NO_OPENDIR_SUPPORT
;
2092 if (!(flags
& FUSE_MAX_PAGES
)) {
2093 size_t max_bufsize
= FUSE_DEFAULT_MAX_PAGES_PER_REQ
* getpagesize() +
2094 FUSE_BUFFER_HEADER_SIZE
;
2095 if (bufsize
> max_bufsize
) {
2096 bufsize
= max_bufsize
;
2099 if (flags
& FUSE_SUBMOUNTS
) {
2100 se
->conn
.capable
|= FUSE_CAP_SUBMOUNTS
;
2102 if (flags
& FUSE_HANDLE_KILLPRIV_V2
) {
2103 se
->conn
.capable
|= FUSE_CAP_HANDLE_KILLPRIV_V2
;
2105 if (flags
& FUSE_SETXATTR_EXT
) {
2106 se
->conn
.capable
|= FUSE_CAP_SETXATTR_EXT
;
2108 if (flags
& FUSE_SECURITY_CTX
) {
2109 se
->conn
.capable
|= FUSE_CAP_SECURITY_CTX
;
2112 #ifdef HAVE_VMSPLICE
2113 se
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
2115 se
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
2117 se
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
2120 * Default settings for modern filesystems.
2122 * Most of these capabilities were disabled by default in
2123 * libfuse2 for backwards compatibility reasons. In libfuse3,
2124 * we can finally enable them by default (as long as they're
2125 * supported by the kernel).
2127 #define LL_SET_DEFAULT(cond, cap) \
2128 if ((cond) && (se->conn.capable & (cap))) \
2129 se->conn.want |= (cap)
2130 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ
);
2131 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS
);
2132 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA
);
2133 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV
);
2134 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO
);
2135 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR
);
2136 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC
);
2137 LL_SET_DEFAULT(se
->op
.write_buf
, FUSE_CAP_SPLICE_READ
);
2138 LL_SET_DEFAULT(se
->op
.getlk
&& se
->op
.setlk
, FUSE_CAP_POSIX_LOCKS
);
2139 LL_SET_DEFAULT(se
->op
.flock
, FUSE_CAP_FLOCK_LOCKS
);
2140 LL_SET_DEFAULT(se
->op
.readdirplus
, FUSE_CAP_READDIRPLUS
);
2141 LL_SET_DEFAULT(se
->op
.readdirplus
&& se
->op
.readdir
,
2142 FUSE_CAP_READDIRPLUS_AUTO
);
2143 se
->conn
.time_gran
= 1;
2145 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
2146 fuse_log(FUSE_LOG_ERR
, "fuse: warning: buffer size too small: %zu\n",
2148 bufsize
= FUSE_MIN_READ_BUFFER
;
2150 se
->bufsize
= bufsize
;
2152 if (se
->conn
.max_write
> bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
2153 se
->conn
.max_write
= bufsize
- FUSE_BUFFER_HEADER_SIZE
;
2157 se
->got_destroy
= 0;
2159 se
->op
.init(se
->userdata
, &se
->conn
);
2162 if (se
->conn
.want
& (~se
->conn
.capable
)) {
2163 fuse_log(FUSE_LOG_ERR
,
2164 "fuse: error: filesystem requested capabilities "
2165 "0x%" PRIx64
" that are not supported by kernel, aborting.\n",
2166 se
->conn
.want
& (~se
->conn
.capable
));
2167 fuse_reply_err(req
, EPROTO
);
2168 se
->error
= -EPROTO
;
2169 fuse_session_exit(se
);
2173 if (se
->conn
.max_write
< bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
2174 se
->bufsize
= se
->conn
.max_write
+ FUSE_BUFFER_HEADER_SIZE
;
2176 if (flags
& FUSE_MAX_PAGES
) {
2177 outarg
.flags
|= FUSE_MAX_PAGES
;
2178 outarg
.max_pages
= (se
->conn
.max_write
- 1) / getpagesize() + 1;
2182 * Always enable big writes, this is superseded
2183 * by the max_write option
2185 outarg
.flags
|= FUSE_BIG_WRITES
;
2187 if (se
->conn
.want
& FUSE_CAP_ASYNC_READ
) {
2188 outarg
.flags
|= FUSE_ASYNC_READ
;
2190 if (se
->conn
.want
& FUSE_CAP_PARALLEL_DIROPS
) {
2191 outarg
.flags
|= FUSE_PARALLEL_DIROPS
;
2193 if (se
->conn
.want
& FUSE_CAP_POSIX_LOCKS
) {
2194 outarg
.flags
|= FUSE_POSIX_LOCKS
;
2196 if (se
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
) {
2197 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
2199 if (se
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
) {
2200 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
2202 if (se
->conn
.want
& FUSE_CAP_DONT_MASK
) {
2203 outarg
.flags
|= FUSE_DONT_MASK
;
2205 if (se
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
) {
2206 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
2208 if (se
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
) {
2209 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
2211 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS
) {
2212 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
2214 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
) {
2215 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
2217 if (se
->conn
.want
& FUSE_CAP_ASYNC_DIO
) {
2218 outarg
.flags
|= FUSE_ASYNC_DIO
;
2220 if (se
->conn
.want
& FUSE_CAP_WRITEBACK_CACHE
) {
2221 outarg
.flags
|= FUSE_WRITEBACK_CACHE
;
2223 if (se
->conn
.want
& FUSE_CAP_POSIX_ACL
) {
2224 outarg
.flags
|= FUSE_POSIX_ACL
;
2226 outarg
.max_readahead
= se
->conn
.max_readahead
;
2227 outarg
.max_write
= se
->conn
.max_write
;
2228 if (se
->conn
.max_background
>= (1 << 16)) {
2229 se
->conn
.max_background
= (1 << 16) - 1;
2231 if (se
->conn
.congestion_threshold
> se
->conn
.max_background
) {
2232 se
->conn
.congestion_threshold
= se
->conn
.max_background
;
2234 if (!se
->conn
.congestion_threshold
) {
2235 se
->conn
.congestion_threshold
= se
->conn
.max_background
* 3 / 4;
2238 outarg
.max_background
= se
->conn
.max_background
;
2239 outarg
.congestion_threshold
= se
->conn
.congestion_threshold
;
2240 outarg
.time_gran
= se
->conn
.time_gran
;
2242 if (se
->conn
.want
& FUSE_CAP_HANDLE_KILLPRIV_V2
) {
2243 outarg
.flags
|= FUSE_HANDLE_KILLPRIV_V2
;
2246 if (se
->conn
.want
& FUSE_CAP_SETXATTR_EXT
) {
2247 outarg
.flags
|= FUSE_SETXATTR_EXT
;
2250 if (se
->conn
.want
& FUSE_CAP_SECURITY_CTX
) {
2251 /* bits 32..63 get shifted down 32 bits into the flags2 field */
2252 outarg
.flags2
|= FUSE_SECURITY_CTX
>> 32;
2255 fuse_log(FUSE_LOG_DEBUG
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
2256 fuse_log(FUSE_LOG_DEBUG
, " flags2=0x%08x flags=0x%08x\n", outarg
.flags2
,
2258 fuse_log(FUSE_LOG_DEBUG
, " max_readahead=0x%08x\n", outarg
.max_readahead
);
2259 fuse_log(FUSE_LOG_DEBUG
, " max_write=0x%08x\n", outarg
.max_write
);
2260 fuse_log(FUSE_LOG_DEBUG
, " max_background=%i\n", outarg
.max_background
);
2261 fuse_log(FUSE_LOG_DEBUG
, " congestion_threshold=%i\n",
2262 outarg
.congestion_threshold
);
2263 fuse_log(FUSE_LOG_DEBUG
, " time_gran=%u\n", outarg
.time_gran
);
2265 send_reply_ok(req
, &outarg
, outargsize
);
2268 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
,
2269 struct fuse_mbuf_iter
*iter
)
2271 struct fuse_session
*se
= req
->se
;
2276 se
->got_destroy
= 1;
2278 if (se
->op
.destroy
) {
2279 se
->op
.destroy(se
->userdata
);
2282 send_reply_ok(req
, NULL
, 0);
2285 int fuse_lowlevel_notify_store(struct fuse_session
*se
, fuse_ino_t ino
,
2286 off_t offset
, struct fuse_bufvec
*bufv
)
2288 struct fuse_out_header out
= {
2289 .error
= FUSE_NOTIFY_STORE
,
2291 struct fuse_notify_store_out outarg
= {
2294 .size
= fuse_buf_size(bufv
),
2296 struct iovec iov
[3];
2303 iov
[0].iov_base
= &out
;
2304 iov
[0].iov_len
= sizeof(out
);
2305 iov
[1].iov_base
= &outarg
;
2306 iov
[1].iov_len
= sizeof(outarg
);
2308 res
= fuse_send_data_iov(se
, NULL
, iov
, 2, bufv
);
2316 void *fuse_req_userdata(fuse_req_t req
)
2318 return req
->se
->userdata
;
2321 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
2326 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
2329 pthread_mutex_lock(&req
->lock
);
2330 pthread_mutex_lock(&req
->se
->lock
);
2331 req
->u
.ni
.func
= func
;
2332 req
->u
.ni
.data
= data
;
2333 pthread_mutex_unlock(&req
->se
->lock
);
2334 if (req
->interrupted
&& func
) {
2337 pthread_mutex_unlock(&req
->lock
);
2340 int fuse_req_interrupted(fuse_req_t req
)
2344 pthread_mutex_lock(&req
->se
->lock
);
2345 interrupted
= req
->interrupted
;
2346 pthread_mutex_unlock(&req
->se
->lock
);
2352 void (*func
)(fuse_req_t
, fuse_ino_t
, struct fuse_mbuf_iter
*);
2355 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
2356 [FUSE_FORGET
] = { do_forget
, "FORGET" },
2357 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
2358 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
2359 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
2360 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
2361 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
2362 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
2363 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
2364 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
2365 [FUSE_RENAME
] = { do_rename
, "RENAME" },
2366 [FUSE_LINK
] = { do_link
, "LINK" },
2367 [FUSE_OPEN
] = { do_open
, "OPEN" },
2368 [FUSE_READ
] = { do_read
, "READ" },
2369 [FUSE_WRITE
] = { do_write
, "WRITE" },
2370 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
2371 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
2372 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
2373 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
2374 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
2375 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
2376 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
2377 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
2378 [FUSE_INIT
] = { do_init
, "INIT" },
2379 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
2380 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
2381 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
2382 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
2383 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
2384 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
2385 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
2386 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
2387 [FUSE_CREATE
] = { do_create
, "CREATE" },
2388 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
2389 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
2390 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
2391 [FUSE_POLL
] = { do_poll
, "POLL" },
2392 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
2393 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
2394 [FUSE_NOTIFY_REPLY
] = { NULL
, "NOTIFY_REPLY" },
2395 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
2396 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS" },
2397 [FUSE_RENAME2
] = { do_rename2
, "RENAME2" },
2398 [FUSE_COPY_FILE_RANGE
] = { do_copy_file_range
, "COPY_FILE_RANGE" },
2399 [FUSE_LSEEK
] = { do_lseek
, "LSEEK" },
2400 [FUSE_SYNCFS
] = { do_syncfs
, "SYNCFS" },
2403 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2405 static const char *opname(enum fuse_opcode opcode
)
2407 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
) {
2410 return fuse_ll_ops
[opcode
].name
;
2414 void fuse_session_process_buf(struct fuse_session
*se
,
2415 const struct fuse_buf
*buf
)
2417 struct fuse_bufvec bufv
= { .buf
[0] = *buf
, .count
= 1 };
2418 fuse_session_process_buf_int(se
, &bufv
, NULL
);
2423 * bufv is normally a single entry buffer, except for a write
2424 * where (if it's in memory) then the bufv may be multiple entries,
2425 * where the first entry contains all headers and subsequent entries
2427 * bufv shall not use any offsets etc to make the data anything
2428 * other than contiguous starting from 0.
2430 void fuse_session_process_buf_int(struct fuse_session
*se
,
2431 struct fuse_bufvec
*bufv
,
2432 struct fuse_chan
*ch
)
2434 const struct fuse_buf
*buf
= bufv
->buf
;
2435 struct fuse_mbuf_iter iter
= FUSE_MBUF_ITER_INIT(buf
);
2436 struct fuse_in_header
*in
;
2437 struct fuse_req
*req
;
2440 /* The first buffer must be a memory buffer */
2441 assert(!(buf
->flags
& FUSE_BUF_IS_FD
));
2443 in
= fuse_mbuf_iter_advance(&iter
, sizeof(*in
));
2444 assert(in
); /* caller guarantees the input buffer is large enough */
2448 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2449 (unsigned long long)in
->unique
, opname((enum fuse_opcode
)in
->opcode
),
2450 in
->opcode
, (unsigned long long)in
->nodeid
, buf
->size
, in
->pid
);
2452 req
= fuse_ll_alloc_req(se
);
2454 struct fuse_out_header out
= {
2455 .unique
= in
->unique
,
2458 struct iovec iov
= {
2460 .iov_len
= sizeof(struct fuse_out_header
),
2463 fuse_send_msg(se
, ch
, &iov
, 1);
2467 req
->unique
= in
->unique
;
2468 req
->ctx
.uid
= in
->uid
;
2469 req
->ctx
.gid
= in
->gid
;
2470 req
->ctx
.pid
= in
->pid
;
2474 * INIT and DESTROY requests are serialized, all other request types
2475 * run in parallel. This prevents races between FUSE_INIT and ordinary
2476 * requests, FUSE_INIT and FUSE_INIT, FUSE_INIT and FUSE_DESTROY, and
2477 * FUSE_DESTROY and FUSE_DESTROY.
2479 if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
||
2480 in
->opcode
== FUSE_DESTROY
) {
2481 pthread_rwlock_wrlock(&se
->init_rwlock
);
2483 pthread_rwlock_rdlock(&se
->init_rwlock
);
2487 if (!se
->got_init
) {
2488 enum fuse_opcode expected
;
2490 expected
= se
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2491 if (in
->opcode
!= expected
) {
2494 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
) {
2495 if (fuse_lowlevel_is_virtio(se
)) {
2497 * TODO: This is after a hard reboot typically, we need to do
2498 * a destroy, but we can't reply to this request yet so
2499 * we can't use do_destroy
2501 fuse_log(FUSE_LOG_DEBUG
, "%s: reinit\n", __func__
);
2502 se
->got_destroy
= 1;
2504 if (se
->op
.destroy
) {
2505 se
->op
.destroy(se
->userdata
);
2513 /* Implement -o allow_root */
2514 if (se
->deny_others
&& in
->uid
!= se
->owner
&& in
->uid
!= 0 &&
2515 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2516 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2517 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2518 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2519 in
->opcode
!= FUSE_NOTIFY_REPLY
&& in
->opcode
!= FUSE_READDIRPLUS
) {
2524 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
) {
2527 if (in
->opcode
!= FUSE_INTERRUPT
) {
2528 struct fuse_req
*intr
;
2529 pthread_mutex_lock(&se
->lock
);
2530 intr
= check_interrupt(se
, req
);
2531 list_add_req(req
, &se
->list
);
2532 pthread_mutex_unlock(&se
->lock
);
2534 fuse_reply_err(intr
, EAGAIN
);
2538 if (in
->opcode
== FUSE_WRITE
&& se
->op
.write_buf
) {
2539 do_write_buf(req
, in
->nodeid
, &iter
, bufv
);
2541 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, &iter
);
2544 pthread_rwlock_unlock(&se
->init_rwlock
);
2548 fuse_reply_err(req
, err
);
2549 pthread_rwlock_unlock(&se
->init_rwlock
);
2552 #define LL_OPTION(n, o, v) \
2554 n, offsetof(struct fuse_session, o), v \
2557 static const struct fuse_opt fuse_ll_opts
[] = {
2558 LL_OPTION("debug", debug
, 1),
2559 LL_OPTION("-d", debug
, 1),
2560 LL_OPTION("--debug", debug
, 1),
2561 LL_OPTION("allow_root", deny_others
, 1),
2562 LL_OPTION("--socket-path=%s", vu_socket_path
, 0),
2563 LL_OPTION("--socket-group=%s", vu_socket_group
, 0),
2564 LL_OPTION("--fd=%d", vu_listen_fd
, 0),
2565 LL_OPTION("--thread-pool-size=%d", thread_pool_size
, 0),
2569 void fuse_lowlevel_version(void)
2571 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION
,
2572 FUSE_KERNEL_MINOR_VERSION
);
2575 void fuse_lowlevel_help(void)
2578 * These are not all options, but the ones that are
2579 * potentially of interest to an end-user
2582 " -o allow_root allow access by root\n"
2583 " --socket-path=PATH path for the vhost-user socket\n"
2584 " --socket-group=GRNAME name of group for the vhost-user socket\n"
2585 " --fd=FDNUM fd number of vhost-user socket\n"
2586 " --thread-pool-size=NUM thread pool size limit (default %d)\n",
2590 void fuse_session_destroy(struct fuse_session
*se
)
2592 if (se
->got_init
&& !se
->got_destroy
) {
2593 if (se
->op
.destroy
) {
2594 se
->op
.destroy(se
->userdata
);
2597 pthread_rwlock_destroy(&se
->init_rwlock
);
2598 pthread_mutex_destroy(&se
->lock
);
2599 free(se
->cuse_data
);
2604 if (fuse_lowlevel_is_virtio(se
)) {
2605 virtio_session_close(se
);
2608 free(se
->vu_socket_path
);
2609 se
->vu_socket_path
= NULL
;
2615 struct fuse_session
*fuse_session_new(struct fuse_args
*args
,
2616 const struct fuse_lowlevel_ops
*op
,
2617 size_t op_size
, void *userdata
)
2619 struct fuse_session
*se
;
2621 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2624 "fuse: warning: library too old, some operations may not work\n");
2625 op_size
= sizeof(struct fuse_lowlevel_ops
);
2628 if (args
->argc
== 0) {
2629 fuse_log(FUSE_LOG_ERR
,
2630 "fuse: empty argv passed to fuse_session_new().\n");
2634 se
= g_try_new0(struct fuse_session
, 1);
2636 fuse_log(FUSE_LOG_ERR
, "fuse: failed to allocate fuse object\n");
2640 se
->vu_listen_fd
= -1;
2641 se
->thread_pool_size
= THREAD_POOL_SIZE
;
2642 se
->conn
.max_write
= UINT_MAX
;
2643 se
->conn
.max_readahead
= UINT_MAX
;
2646 if (fuse_opt_parse(args
, se
, fuse_ll_opts
, NULL
) == -1) {
2649 if (args
->argc
== 1 && args
->argv
[0][0] == '-') {
2650 fuse_log(FUSE_LOG_ERR
,
2651 "fuse: warning: argv[0] looks like an option, but "
2652 "will be ignored\n");
2653 } else if (args
->argc
!= 1) {
2655 fuse_log(FUSE_LOG_ERR
, "fuse: unknown option(s): `");
2656 for (i
= 1; i
< args
->argc
- 1; i
++) {
2657 fuse_log(FUSE_LOG_ERR
, "%s ", args
->argv
[i
]);
2659 fuse_log(FUSE_LOG_ERR
, "%s'\n", args
->argv
[i
]);
2663 if (!se
->vu_socket_path
&& se
->vu_listen_fd
< 0) {
2664 fuse_log(FUSE_LOG_ERR
, "fuse: missing --socket-path or --fd option\n");
2667 if (se
->vu_socket_path
&& se
->vu_listen_fd
>= 0) {
2668 fuse_log(FUSE_LOG_ERR
,
2669 "fuse: --socket-path and --fd cannot be given together\n");
2672 if (se
->vu_socket_group
&& !se
->vu_socket_path
) {
2673 fuse_log(FUSE_LOG_ERR
,
2674 "fuse: --socket-group can only be used with --socket-path\n");
2678 se
->bufsize
= FUSE_MAX_MAX_PAGES
* getpagesize() + FUSE_BUFFER_HEADER_SIZE
;
2680 list_init_req(&se
->list
);
2681 list_init_req(&se
->interrupts
);
2682 fuse_mutex_init(&se
->lock
);
2683 pthread_rwlock_init(&se
->init_rwlock
, NULL
);
2685 memcpy(&se
->op
, op
, op_size
);
2686 se
->owner
= getuid();
2687 se
->userdata
= userdata
;
2692 fuse_opt_free_args(args
);
2699 int fuse_session_mount(struct fuse_session
*se
)
2701 return virtio_session_mount(se
);
2704 int fuse_session_fd(struct fuse_session
*se
)
2709 void fuse_session_unmount(struct fuse_session
*se
)
2713 int fuse_lowlevel_is_virtio(struct fuse_session
*se
)
2715 return !!se
->virtio_dev
;
2718 void fuse_session_exit(struct fuse_session
*se
)
2723 void fuse_session_reset(struct fuse_session
*se
)
2729 int fuse_session_exited(struct fuse_session
*se
)