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"
29 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
30 #define OFFSET_MAX 0x7fffffffffffffffLL
32 struct fuse_pollhandle
{
34 struct fuse_session
*se
;
37 static size_t pagesize
;
39 static __attribute__((constructor
)) void fuse_ll_init_pagesize(void)
41 pagesize
= getpagesize();
44 static void convert_stat(const struct stat
*stbuf
, struct fuse_attr
*attr
)
46 attr
->ino
= stbuf
->st_ino
;
47 attr
->mode
= stbuf
->st_mode
;
48 attr
->nlink
= stbuf
->st_nlink
;
49 attr
->uid
= stbuf
->st_uid
;
50 attr
->gid
= stbuf
->st_gid
;
51 attr
->rdev
= stbuf
->st_rdev
;
52 attr
->size
= stbuf
->st_size
;
53 attr
->blksize
= stbuf
->st_blksize
;
54 attr
->blocks
= stbuf
->st_blocks
;
55 attr
->atime
= stbuf
->st_atime
;
56 attr
->mtime
= stbuf
->st_mtime
;
57 attr
->ctime
= stbuf
->st_ctime
;
58 attr
->atimensec
= ST_ATIM_NSEC(stbuf
);
59 attr
->mtimensec
= ST_MTIM_NSEC(stbuf
);
60 attr
->ctimensec
= ST_CTIM_NSEC(stbuf
);
63 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
65 stbuf
->st_mode
= attr
->mode
;
66 stbuf
->st_uid
= attr
->uid
;
67 stbuf
->st_gid
= attr
->gid
;
68 stbuf
->st_size
= attr
->size
;
69 stbuf
->st_atime
= attr
->atime
;
70 stbuf
->st_mtime
= attr
->mtime
;
71 stbuf
->st_ctime
= attr
->ctime
;
72 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
73 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
74 ST_CTIM_NSEC_SET(stbuf
, attr
->ctimensec
);
77 static size_t iov_length(const struct iovec
*iov
, size_t count
)
82 for (seg
= 0; seg
< count
; seg
++) {
83 ret
+= iov
[seg
].iov_len
;
88 static void list_init_req(struct fuse_req
*req
)
94 static void list_del_req(struct fuse_req
*req
)
96 struct fuse_req
*prev
= req
->prev
;
97 struct fuse_req
*next
= req
->next
;
102 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
104 struct fuse_req
*prev
= next
->prev
;
111 static void destroy_req(fuse_req_t req
)
113 pthread_mutex_destroy(&req
->lock
);
117 void fuse_free_req(fuse_req_t req
)
120 struct fuse_session
*se
= req
->se
;
122 pthread_mutex_lock(&se
->lock
);
123 req
->u
.ni
.func
= NULL
;
124 req
->u
.ni
.data
= NULL
;
128 pthread_mutex_unlock(&se
->lock
);
134 static struct fuse_req
*fuse_ll_alloc_req(struct fuse_session
*se
)
136 struct fuse_req
*req
;
138 req
= (struct fuse_req
*)calloc(1, sizeof(struct fuse_req
));
140 fuse_log(FUSE_LOG_ERR
, "fuse: failed to allocate request\n");
145 fuse_mutex_init(&req
->lock
);
151 /* Send data. If *ch* is NULL, send via session master fd */
152 static int fuse_send_msg(struct fuse_session
*se
, struct fuse_chan
*ch
,
153 struct iovec
*iov
, int count
)
155 struct fuse_out_header
*out
= iov
[0].iov_base
;
157 out
->len
= iov_length(iov
, count
);
159 if (out
->unique
== 0) {
160 fuse_log(FUSE_LOG_DEBUG
, "NOTIFY: code=%d length=%u\n", out
->error
,
162 } else if (out
->error
) {
163 fuse_log(FUSE_LOG_DEBUG
,
164 " unique: %llu, error: %i (%s), outsize: %i\n",
165 (unsigned long long)out
->unique
, out
->error
,
166 strerror(-out
->error
), out
->len
);
168 fuse_log(FUSE_LOG_DEBUG
, " unique: %llu, success, outsize: %i\n",
169 (unsigned long long)out
->unique
, out
->len
);
173 abort(); /* virtio should have taken it before here */
178 int fuse_send_reply_iov_nofree(fuse_req_t req
, int error
, struct iovec
*iov
,
181 struct fuse_out_header out
;
183 if (error
<= -1000 || error
> 0) {
184 fuse_log(FUSE_LOG_ERR
, "fuse: bad error value: %i\n", error
);
188 out
.unique
= req
->unique
;
191 iov
[0].iov_base
= &out
;
192 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
194 return fuse_send_msg(req
->se
, req
->ch
, iov
, count
);
197 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
202 res
= fuse_send_reply_iov_nofree(req
, error
, iov
, count
);
207 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
213 iov
[1].iov_base
= (void *)arg
;
214 iov
[1].iov_len
= argsize
;
217 return send_reply_iov(req
, error
, iov
, count
);
220 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
223 struct iovec
*padded_iov
;
225 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
226 if (padded_iov
== NULL
) {
227 return fuse_reply_err(req
, ENOMEM
);
230 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
233 res
= send_reply_iov(req
, 0, padded_iov
, count
);
241 * 'buf` is allowed to be empty so that the proper size may be
242 * allocated by the caller
244 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
245 const char *name
, const struct stat
*stbuf
, off_t off
)
250 size_t entlen_padded
;
251 struct fuse_dirent
*dirent
;
253 namelen
= strlen(name
);
254 entlen
= FUSE_NAME_OFFSET
+ namelen
;
255 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
257 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
258 return entlen_padded
;
261 dirent
= (struct fuse_dirent
*)buf
;
262 dirent
->ino
= stbuf
->st_ino
;
264 dirent
->namelen
= namelen
;
265 dirent
->type
= (stbuf
->st_mode
& S_IFMT
) >> 12;
266 memcpy(dirent
->name
, name
, namelen
);
267 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
269 return entlen_padded
;
272 static void convert_statfs(const struct statvfs
*stbuf
,
273 struct fuse_kstatfs
*kstatfs
)
275 kstatfs
->bsize
= stbuf
->f_bsize
;
276 kstatfs
->frsize
= stbuf
->f_frsize
;
277 kstatfs
->blocks
= stbuf
->f_blocks
;
278 kstatfs
->bfree
= stbuf
->f_bfree
;
279 kstatfs
->bavail
= stbuf
->f_bavail
;
280 kstatfs
->files
= stbuf
->f_files
;
281 kstatfs
->ffree
= stbuf
->f_ffree
;
282 kstatfs
->namelen
= stbuf
->f_namemax
;
285 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
287 return send_reply(req
, 0, arg
, argsize
);
290 int fuse_reply_err(fuse_req_t req
, int err
)
292 return send_reply(req
, -err
, NULL
, 0);
295 void fuse_reply_none(fuse_req_t req
)
300 static unsigned long calc_timeout_sec(double t
)
302 if (t
> (double)ULONG_MAX
) {
304 } else if (t
< 0.0) {
307 return (unsigned long)t
;
311 static unsigned int calc_timeout_nsec(double t
)
313 double f
= t
- (double)calc_timeout_sec(t
);
316 } else if (f
>= 0.999999999) {
319 return (unsigned int)(f
* 1.0e9
);
323 static void fill_entry(struct fuse_entry_out
*arg
,
324 const struct fuse_entry_param
*e
)
326 arg
->nodeid
= e
->ino
;
327 arg
->generation
= e
->generation
;
328 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
329 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
330 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
331 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
332 convert_stat(&e
->attr
, &arg
->attr
);
336 * `buf` is allowed to be empty so that the proper size may be
337 * allocated by the caller
339 size_t fuse_add_direntry_plus(fuse_req_t req
, char *buf
, size_t bufsize
,
341 const struct fuse_entry_param
*e
, off_t off
)
346 size_t entlen_padded
;
348 namelen
= strlen(name
);
349 entlen
= FUSE_NAME_OFFSET_DIRENTPLUS
+ namelen
;
350 entlen_padded
= FUSE_DIRENT_ALIGN(entlen
);
351 if ((buf
== NULL
) || (entlen_padded
> bufsize
)) {
352 return entlen_padded
;
355 struct fuse_direntplus
*dp
= (struct fuse_direntplus
*)buf
;
356 memset(&dp
->entry_out
, 0, sizeof(dp
->entry_out
));
357 fill_entry(&dp
->entry_out
, e
);
359 struct fuse_dirent
*dirent
= &dp
->dirent
;
360 dirent
->ino
= e
->attr
.st_ino
;
362 dirent
->namelen
= namelen
;
363 dirent
->type
= (e
->attr
.st_mode
& S_IFMT
) >> 12;
364 memcpy(dirent
->name
, name
, namelen
);
365 memset(dirent
->name
+ namelen
, 0, entlen_padded
- entlen
);
367 return entlen_padded
;
370 static void fill_open(struct fuse_open_out
*arg
, const struct fuse_file_info
*f
)
374 arg
->open_flags
|= FOPEN_DIRECT_IO
;
377 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
379 if (f
->cache_readdir
) {
380 arg
->open_flags
|= FOPEN_CACHE_DIR
;
382 if (f
->nonseekable
) {
383 arg
->open_flags
|= FOPEN_NONSEEKABLE
;
387 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
389 struct fuse_entry_out arg
;
390 size_t size
= sizeof(arg
);
392 memset(&arg
, 0, sizeof(arg
));
394 return send_reply_ok(req
, &arg
, size
);
397 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
398 const struct fuse_file_info
*f
)
400 char buf
[sizeof(struct fuse_entry_out
) + sizeof(struct fuse_open_out
)];
401 size_t entrysize
= sizeof(struct fuse_entry_out
);
402 struct fuse_entry_out
*earg
= (struct fuse_entry_out
*)buf
;
403 struct fuse_open_out
*oarg
= (struct fuse_open_out
*)(buf
+ entrysize
);
405 memset(buf
, 0, sizeof(buf
));
408 return send_reply_ok(req
, buf
, entrysize
+ sizeof(struct fuse_open_out
));
411 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
414 struct fuse_attr_out arg
;
415 size_t size
= sizeof(arg
);
417 memset(&arg
, 0, sizeof(arg
));
418 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
419 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
420 convert_stat(attr
, &arg
.attr
);
422 return send_reply_ok(req
, &arg
, size
);
425 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
427 return send_reply_ok(req
, linkname
, strlen(linkname
));
430 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
432 struct fuse_open_out arg
;
434 memset(&arg
, 0, sizeof(arg
));
436 return send_reply_ok(req
, &arg
, sizeof(arg
));
439 int fuse_reply_write(fuse_req_t req
, size_t count
)
441 struct fuse_write_out arg
;
443 memset(&arg
, 0, sizeof(arg
));
446 return send_reply_ok(req
, &arg
, sizeof(arg
));
449 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
451 return send_reply_ok(req
, buf
, size
);
454 static int fuse_send_data_iov_fallback(struct fuse_session
*se
,
455 struct fuse_chan
*ch
, struct iovec
*iov
,
456 int iov_count
, struct fuse_bufvec
*buf
,
459 /* Optimize common case */
460 if (buf
->count
== 1 && buf
->idx
== 0 && buf
->off
== 0 &&
461 !(buf
->buf
[0].flags
& FUSE_BUF_IS_FD
)) {
463 * FIXME: also avoid memory copy if there are multiple buffers
464 * but none of them contain an fd
467 iov
[iov_count
].iov_base
= buf
->buf
[0].mem
;
468 iov
[iov_count
].iov_len
= len
;
470 return fuse_send_msg(se
, ch
, iov
, iov_count
);
473 abort(); /* Will have taken vhost path */
477 static int fuse_send_data_iov(struct fuse_session
*se
, struct fuse_chan
*ch
,
478 struct iovec
*iov
, int iov_count
,
479 struct fuse_bufvec
*buf
)
481 size_t len
= fuse_buf_size(buf
);
483 return fuse_send_data_iov_fallback(se
, ch
, iov
, iov_count
, buf
, len
);
486 int fuse_reply_data(fuse_req_t req
, struct fuse_bufvec
*bufv
)
489 struct fuse_out_header out
;
492 iov
[0].iov_base
= &out
;
493 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
495 out
.unique
= req
->unique
;
498 res
= fuse_send_data_iov(req
->se
, req
->ch
, iov
, 1, bufv
);
503 return fuse_reply_err(req
, res
);
507 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
509 struct fuse_statfs_out arg
;
510 size_t size
= sizeof(arg
);
512 memset(&arg
, 0, sizeof(arg
));
513 convert_statfs(stbuf
, &arg
.st
);
515 return send_reply_ok(req
, &arg
, size
);
518 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
520 struct fuse_getxattr_out arg
;
522 memset(&arg
, 0, sizeof(arg
));
525 return send_reply_ok(req
, &arg
, sizeof(arg
));
528 int fuse_reply_lock(fuse_req_t req
, const struct flock
*lock
)
530 struct fuse_lk_out arg
;
532 memset(&arg
, 0, sizeof(arg
));
533 arg
.lk
.type
= lock
->l_type
;
534 if (lock
->l_type
!= F_UNLCK
) {
535 arg
.lk
.start
= lock
->l_start
;
536 if (lock
->l_len
== 0) {
537 arg
.lk
.end
= OFFSET_MAX
;
539 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
542 arg
.lk
.pid
= lock
->l_pid
;
543 return send_reply_ok(req
, &arg
, sizeof(arg
));
546 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
548 struct fuse_bmap_out arg
;
550 memset(&arg
, 0, sizeof(arg
));
553 return send_reply_ok(req
, &arg
, sizeof(arg
));
556 static struct fuse_ioctl_iovec
*fuse_ioctl_iovec_copy(const struct iovec
*iov
,
559 struct fuse_ioctl_iovec
*fiov
;
562 fiov
= malloc(sizeof(fiov
[0]) * count
);
567 for (i
= 0; i
< count
; i
++) {
568 fiov
[i
].base
= (uintptr_t)iov
[i
].iov_base
;
569 fiov
[i
].len
= iov
[i
].iov_len
;
575 int fuse_reply_ioctl_retry(fuse_req_t req
, const struct iovec
*in_iov
,
576 size_t in_count
, const struct iovec
*out_iov
,
579 struct fuse_ioctl_out arg
;
580 struct fuse_ioctl_iovec
*in_fiov
= NULL
;
581 struct fuse_ioctl_iovec
*out_fiov
= NULL
;
586 memset(&arg
, 0, sizeof(arg
));
587 arg
.flags
|= FUSE_IOCTL_RETRY
;
588 arg
.in_iovs
= in_count
;
589 arg
.out_iovs
= out_count
;
590 iov
[count
].iov_base
= &arg
;
591 iov
[count
].iov_len
= sizeof(arg
);
594 /* Can't handle non-compat 64bit ioctls on 32bit */
595 if (sizeof(void *) == 4 && req
->ioctl_64bit
) {
596 res
= fuse_reply_err(req
, EINVAL
);
601 in_fiov
= fuse_ioctl_iovec_copy(in_iov
, in_count
);
606 iov
[count
].iov_base
= (void *)in_fiov
;
607 iov
[count
].iov_len
= sizeof(in_fiov
[0]) * in_count
;
611 out_fiov
= fuse_ioctl_iovec_copy(out_iov
, out_count
);
616 iov
[count
].iov_base
= (void *)out_fiov
;
617 iov
[count
].iov_len
= sizeof(out_fiov
[0]) * out_count
;
621 res
= send_reply_iov(req
, 0, iov
, count
);
629 res
= fuse_reply_err(req
, ENOMEM
);
633 int fuse_reply_ioctl(fuse_req_t req
, int result
, const void *buf
, size_t size
)
635 struct fuse_ioctl_out arg
;
639 memset(&arg
, 0, sizeof(arg
));
641 iov
[count
].iov_base
= &arg
;
642 iov
[count
].iov_len
= sizeof(arg
);
646 iov
[count
].iov_base
= (char *)buf
;
647 iov
[count
].iov_len
= size
;
651 return send_reply_iov(req
, 0, iov
, count
);
654 int fuse_reply_ioctl_iov(fuse_req_t req
, int result
, const struct iovec
*iov
,
657 struct iovec
*padded_iov
;
658 struct fuse_ioctl_out arg
;
661 padded_iov
= malloc((count
+ 2) * sizeof(struct iovec
));
662 if (padded_iov
== NULL
) {
663 return fuse_reply_err(req
, ENOMEM
);
666 memset(&arg
, 0, sizeof(arg
));
668 padded_iov
[1].iov_base
= &arg
;
669 padded_iov
[1].iov_len
= sizeof(arg
);
671 memcpy(&padded_iov
[2], iov
, count
* sizeof(struct iovec
));
673 res
= send_reply_iov(req
, 0, padded_iov
, count
+ 2);
679 int fuse_reply_poll(fuse_req_t req
, unsigned revents
)
681 struct fuse_poll_out arg
;
683 memset(&arg
, 0, sizeof(arg
));
684 arg
.revents
= revents
;
686 return send_reply_ok(req
, &arg
, sizeof(arg
));
689 int fuse_reply_lseek(fuse_req_t req
, off_t off
)
691 struct fuse_lseek_out arg
;
693 memset(&arg
, 0, sizeof(arg
));
696 return send_reply_ok(req
, &arg
, sizeof(arg
));
699 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
701 char *name
= (char *)inarg
;
703 if (req
->se
->op
.lookup
) {
704 req
->se
->op
.lookup(req
, nodeid
, name
);
706 fuse_reply_err(req
, ENOSYS
);
710 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
712 struct fuse_forget_in
*arg
= (struct fuse_forget_in
*)inarg
;
714 if (req
->se
->op
.forget
) {
715 req
->se
->op
.forget(req
, nodeid
, arg
->nlookup
);
717 fuse_reply_none(req
);
721 static void do_batch_forget(fuse_req_t req
, fuse_ino_t nodeid
,
724 struct fuse_batch_forget_in
*arg
= (void *)inarg
;
725 struct fuse_forget_one
*param
= (void *)PARAM(arg
);
730 if (req
->se
->op
.forget_multi
) {
731 req
->se
->op
.forget_multi(req
, arg
->count
,
732 (struct fuse_forget_data
*)param
);
733 } else if (req
->se
->op
.forget
) {
734 for (i
= 0; i
< arg
->count
; i
++) {
735 struct fuse_forget_one
*forget
= ¶m
[i
];
736 struct fuse_req
*dummy_req
;
738 dummy_req
= fuse_ll_alloc_req(req
->se
);
739 if (dummy_req
== NULL
) {
743 dummy_req
->unique
= req
->unique
;
744 dummy_req
->ctx
= req
->ctx
;
745 dummy_req
->ch
= NULL
;
747 req
->se
->op
.forget(dummy_req
, forget
->nodeid
, forget
->nlookup
);
749 fuse_reply_none(req
);
751 fuse_reply_none(req
);
755 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
757 struct fuse_file_info
*fip
= NULL
;
758 struct fuse_file_info fi
;
760 struct fuse_getattr_in
*arg
= (struct fuse_getattr_in
*)inarg
;
762 if (arg
->getattr_flags
& FUSE_GETATTR_FH
) {
763 memset(&fi
, 0, sizeof(fi
));
768 if (req
->se
->op
.getattr
) {
769 req
->se
->op
.getattr(req
, nodeid
, fip
);
771 fuse_reply_err(req
, ENOSYS
);
775 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
777 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*)inarg
;
779 if (req
->se
->op
.setattr
) {
780 struct fuse_file_info
*fi
= NULL
;
781 struct fuse_file_info fi_store
;
783 memset(&stbuf
, 0, sizeof(stbuf
));
784 convert_attr(arg
, &stbuf
);
785 if (arg
->valid
& FATTR_FH
) {
786 arg
->valid
&= ~FATTR_FH
;
787 memset(&fi_store
, 0, sizeof(fi_store
));
791 arg
->valid
&= FUSE_SET_ATTR_MODE
| FUSE_SET_ATTR_UID
|
792 FUSE_SET_ATTR_GID
| FUSE_SET_ATTR_SIZE
|
793 FUSE_SET_ATTR_ATIME
| FUSE_SET_ATTR_MTIME
|
794 FUSE_SET_ATTR_ATIME_NOW
| FUSE_SET_ATTR_MTIME_NOW
|
797 req
->se
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
799 fuse_reply_err(req
, ENOSYS
);
803 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
805 struct fuse_access_in
*arg
= (struct fuse_access_in
*)inarg
;
807 if (req
->se
->op
.access
) {
808 req
->se
->op
.access(req
, nodeid
, arg
->mask
);
810 fuse_reply_err(req
, ENOSYS
);
814 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
818 if (req
->se
->op
.readlink
) {
819 req
->se
->op
.readlink(req
, nodeid
);
821 fuse_reply_err(req
, ENOSYS
);
825 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
827 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*)inarg
;
828 char *name
= PARAM(arg
);
830 req
->ctx
.umask
= arg
->umask
;
832 if (req
->se
->op
.mknod
) {
833 req
->se
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
835 fuse_reply_err(req
, ENOSYS
);
839 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
841 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*)inarg
;
843 req
->ctx
.umask
= arg
->umask
;
845 if (req
->se
->op
.mkdir
) {
846 req
->se
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
848 fuse_reply_err(req
, ENOSYS
);
852 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
854 char *name
= (char *)inarg
;
856 if (req
->se
->op
.unlink
) {
857 req
->se
->op
.unlink(req
, nodeid
, name
);
859 fuse_reply_err(req
, ENOSYS
);
863 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
865 char *name
= (char *)inarg
;
867 if (req
->se
->op
.rmdir
) {
868 req
->se
->op
.rmdir(req
, nodeid
, name
);
870 fuse_reply_err(req
, ENOSYS
);
874 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
876 char *name
= (char *)inarg
;
877 char *linkname
= ((char *)inarg
) + strlen((char *)inarg
) + 1;
879 if (req
->se
->op
.symlink
) {
880 req
->se
->op
.symlink(req
, linkname
, nodeid
, name
);
882 fuse_reply_err(req
, ENOSYS
);
886 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
888 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*)inarg
;
889 char *oldname
= PARAM(arg
);
890 char *newname
= oldname
+ strlen(oldname
) + 1;
892 if (req
->se
->op
.rename
) {
893 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
, 0);
895 fuse_reply_err(req
, ENOSYS
);
899 static void do_rename2(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
901 struct fuse_rename2_in
*arg
= (struct fuse_rename2_in
*)inarg
;
902 char *oldname
= PARAM(arg
);
903 char *newname
= oldname
+ strlen(oldname
) + 1;
905 if (req
->se
->op
.rename
) {
906 req
->se
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
,
909 fuse_reply_err(req
, ENOSYS
);
913 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
915 struct fuse_link_in
*arg
= (struct fuse_link_in
*)inarg
;
917 if (req
->se
->op
.link
) {
918 req
->se
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
920 fuse_reply_err(req
, ENOSYS
);
924 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
926 struct fuse_create_in
*arg
= (struct fuse_create_in
*)inarg
;
928 if (req
->se
->op
.create
) {
929 struct fuse_file_info fi
;
930 char *name
= PARAM(arg
);
932 memset(&fi
, 0, sizeof(fi
));
933 fi
.flags
= arg
->flags
;
935 req
->ctx
.umask
= arg
->umask
;
937 req
->se
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
939 fuse_reply_err(req
, ENOSYS
);
943 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
945 struct fuse_open_in
*arg
= (struct fuse_open_in
*)inarg
;
946 struct fuse_file_info fi
;
948 memset(&fi
, 0, sizeof(fi
));
949 fi
.flags
= arg
->flags
;
951 if (req
->se
->op
.open
) {
952 req
->se
->op
.open(req
, nodeid
, &fi
);
954 fuse_reply_open(req
, &fi
);
958 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
960 struct fuse_read_in
*arg
= (struct fuse_read_in
*)inarg
;
962 if (req
->se
->op
.read
) {
963 struct fuse_file_info fi
;
965 memset(&fi
, 0, sizeof(fi
));
967 fi
.lock_owner
= arg
->lock_owner
;
968 fi
.flags
= arg
->flags
;
969 req
->se
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
971 fuse_reply_err(req
, ENOSYS
);
975 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
977 struct fuse_write_in
*arg
= (struct fuse_write_in
*)inarg
;
978 struct fuse_file_info fi
;
981 memset(&fi
, 0, sizeof(fi
));
983 fi
.writepage
= (arg
->write_flags
& FUSE_WRITE_CACHE
) != 0;
985 fi
.lock_owner
= arg
->lock_owner
;
986 fi
.flags
= arg
->flags
;
989 if (req
->se
->op
.write
) {
990 req
->se
->op
.write(req
, nodeid
, param
, arg
->size
, arg
->offset
, &fi
);
992 fuse_reply_err(req
, ENOSYS
);
996 static void do_write_buf(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
,
997 const struct fuse_buf
*ibuf
)
999 struct fuse_session
*se
= req
->se
;
1000 struct fuse_bufvec bufv
= {
1004 struct fuse_write_in
*arg
= (struct fuse_write_in
*)inarg
;
1005 struct fuse_file_info fi
;
1007 memset(&fi
, 0, sizeof(fi
));
1009 fi
.writepage
= arg
->write_flags
& FUSE_WRITE_CACHE
;
1011 fi
.lock_owner
= arg
->lock_owner
;
1012 fi
.flags
= arg
->flags
;
1013 if (!(bufv
.buf
[0].flags
& FUSE_BUF_IS_FD
)) {
1014 bufv
.buf
[0].mem
= PARAM(arg
);
1018 sizeof(struct fuse_in_header
) + sizeof(struct fuse_write_in
);
1019 if (bufv
.buf
[0].size
< arg
->size
) {
1020 fuse_log(FUSE_LOG_ERR
, "fuse: do_write_buf: buffer size too small\n");
1021 fuse_reply_err(req
, EIO
);
1024 bufv
.buf
[0].size
= arg
->size
;
1026 se
->op
.write_buf(req
, nodeid
, &bufv
, arg
->offset
, &fi
);
1029 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1031 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*)inarg
;
1032 struct fuse_file_info fi
;
1034 memset(&fi
, 0, sizeof(fi
));
1037 fi
.lock_owner
= arg
->lock_owner
;
1039 if (req
->se
->op
.flush
) {
1040 req
->se
->op
.flush(req
, nodeid
, &fi
);
1042 fuse_reply_err(req
, ENOSYS
);
1046 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1048 struct fuse_release_in
*arg
= (struct fuse_release_in
*)inarg
;
1049 struct fuse_file_info fi
;
1051 memset(&fi
, 0, sizeof(fi
));
1052 fi
.flags
= arg
->flags
;
1054 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
1055 fi
.lock_owner
= arg
->lock_owner
;
1056 if (arg
->release_flags
& FUSE_RELEASE_FLOCK_UNLOCK
) {
1057 fi
.flock_release
= 1;
1058 fi
.lock_owner
= arg
->lock_owner
;
1061 if (req
->se
->op
.release
) {
1062 req
->se
->op
.release(req
, nodeid
, &fi
);
1064 fuse_reply_err(req
, 0);
1068 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1070 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*)inarg
;
1071 struct fuse_file_info fi
;
1072 int datasync
= arg
->fsync_flags
& 1;
1074 memset(&fi
, 0, sizeof(fi
));
1077 if (req
->se
->op
.fsync
) {
1078 if (fi
.fh
== (uint64_t)-1) {
1079 req
->se
->op
.fsync(req
, nodeid
, datasync
, NULL
);
1081 req
->se
->op
.fsync(req
, nodeid
, datasync
, &fi
);
1084 fuse_reply_err(req
, ENOSYS
);
1088 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1090 struct fuse_open_in
*arg
= (struct fuse_open_in
*)inarg
;
1091 struct fuse_file_info fi
;
1093 memset(&fi
, 0, sizeof(fi
));
1094 fi
.flags
= arg
->flags
;
1096 if (req
->se
->op
.opendir
) {
1097 req
->se
->op
.opendir(req
, nodeid
, &fi
);
1099 fuse_reply_open(req
, &fi
);
1103 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1105 struct fuse_read_in
*arg
= (struct fuse_read_in
*)inarg
;
1106 struct fuse_file_info fi
;
1108 memset(&fi
, 0, sizeof(fi
));
1111 if (req
->se
->op
.readdir
) {
1112 req
->se
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1114 fuse_reply_err(req
, ENOSYS
);
1118 static void do_readdirplus(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1120 struct fuse_read_in
*arg
= (struct fuse_read_in
*)inarg
;
1121 struct fuse_file_info fi
;
1123 memset(&fi
, 0, sizeof(fi
));
1126 if (req
->se
->op
.readdirplus
) {
1127 req
->se
->op
.readdirplus(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
1129 fuse_reply_err(req
, ENOSYS
);
1133 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1135 struct fuse_release_in
*arg
= (struct fuse_release_in
*)inarg
;
1136 struct fuse_file_info fi
;
1138 memset(&fi
, 0, sizeof(fi
));
1139 fi
.flags
= arg
->flags
;
1142 if (req
->se
->op
.releasedir
) {
1143 req
->se
->op
.releasedir(req
, nodeid
, &fi
);
1145 fuse_reply_err(req
, 0);
1149 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1151 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*)inarg
;
1152 struct fuse_file_info fi
;
1153 int datasync
= arg
->fsync_flags
& 1;
1155 memset(&fi
, 0, sizeof(fi
));
1158 if (req
->se
->op
.fsyncdir
) {
1159 req
->se
->op
.fsyncdir(req
, nodeid
, datasync
, &fi
);
1161 fuse_reply_err(req
, ENOSYS
);
1165 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1170 if (req
->se
->op
.statfs
) {
1171 req
->se
->op
.statfs(req
, nodeid
);
1173 struct statvfs buf
= {
1177 fuse_reply_statfs(req
, &buf
);
1181 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1183 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*)inarg
;
1184 char *name
= PARAM(arg
);
1185 char *value
= name
+ strlen(name
) + 1;
1187 if (req
->se
->op
.setxattr
) {
1188 req
->se
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
, arg
->flags
);
1190 fuse_reply_err(req
, ENOSYS
);
1194 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1196 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*)inarg
;
1198 if (req
->se
->op
.getxattr
) {
1199 req
->se
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
1201 fuse_reply_err(req
, ENOSYS
);
1205 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1207 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*)inarg
;
1209 if (req
->se
->op
.listxattr
) {
1210 req
->se
->op
.listxattr(req
, nodeid
, arg
->size
);
1212 fuse_reply_err(req
, ENOSYS
);
1216 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1218 char *name
= (char *)inarg
;
1220 if (req
->se
->op
.removexattr
) {
1221 req
->se
->op
.removexattr(req
, nodeid
, name
);
1223 fuse_reply_err(req
, ENOSYS
);
1227 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
1228 struct flock
*flock
)
1230 memset(flock
, 0, sizeof(struct flock
));
1231 flock
->l_type
= fl
->type
;
1232 flock
->l_whence
= SEEK_SET
;
1233 flock
->l_start
= fl
->start
;
1234 if (fl
->end
== OFFSET_MAX
) {
1237 flock
->l_len
= fl
->end
- fl
->start
+ 1;
1239 flock
->l_pid
= fl
->pid
;
1242 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1244 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*)inarg
;
1245 struct fuse_file_info fi
;
1248 memset(&fi
, 0, sizeof(fi
));
1250 fi
.lock_owner
= arg
->owner
;
1252 convert_fuse_file_lock(&arg
->lk
, &flock
);
1253 if (req
->se
->op
.getlk
) {
1254 req
->se
->op
.getlk(req
, nodeid
, &fi
, &flock
);
1256 fuse_reply_err(req
, ENOSYS
);
1260 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
1261 const void *inarg
, int sleep
)
1263 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*)inarg
;
1264 struct fuse_file_info fi
;
1267 memset(&fi
, 0, sizeof(fi
));
1269 fi
.lock_owner
= arg
->owner
;
1271 if (arg
->lk_flags
& FUSE_LK_FLOCK
) {
1274 switch (arg
->lk
.type
) {
1289 if (req
->se
->op
.flock
) {
1290 req
->se
->op
.flock(req
, nodeid
, &fi
, op
);
1292 fuse_reply_err(req
, ENOSYS
);
1295 convert_fuse_file_lock(&arg
->lk
, &flock
);
1296 if (req
->se
->op
.setlk
) {
1297 req
->se
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
1299 fuse_reply_err(req
, ENOSYS
);
1304 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1306 do_setlk_common(req
, nodeid
, inarg
, 0);
1309 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1311 do_setlk_common(req
, nodeid
, inarg
, 1);
1314 static int find_interrupted(struct fuse_session
*se
, struct fuse_req
*req
)
1316 struct fuse_req
*curr
;
1318 for (curr
= se
->list
.next
; curr
!= &se
->list
; curr
= curr
->next
) {
1319 if (curr
->unique
== req
->u
.i
.unique
) {
1320 fuse_interrupt_func_t func
;
1324 pthread_mutex_unlock(&se
->lock
);
1326 /* Ugh, ugly locking */
1327 pthread_mutex_lock(&curr
->lock
);
1328 pthread_mutex_lock(&se
->lock
);
1329 curr
->interrupted
= 1;
1330 func
= curr
->u
.ni
.func
;
1331 data
= curr
->u
.ni
.data
;
1332 pthread_mutex_unlock(&se
->lock
);
1336 pthread_mutex_unlock(&curr
->lock
);
1338 pthread_mutex_lock(&se
->lock
);
1347 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1348 curr
= curr
->next
) {
1349 if (curr
->u
.i
.unique
== req
->u
.i
.unique
) {
1356 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1358 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*)inarg
;
1359 struct fuse_session
*se
= req
->se
;
1363 fuse_log(FUSE_LOG_DEBUG
, "INTERRUPT: %llu\n",
1364 (unsigned long long)arg
->unique
);
1367 req
->u
.i
.unique
= arg
->unique
;
1369 pthread_mutex_lock(&se
->lock
);
1370 if (find_interrupted(se
, req
)) {
1373 list_add_req(req
, &se
->interrupts
);
1375 pthread_mutex_unlock(&se
->lock
);
1378 static struct fuse_req
*check_interrupt(struct fuse_session
*se
,
1379 struct fuse_req
*req
)
1381 struct fuse_req
*curr
;
1383 for (curr
= se
->interrupts
.next
; curr
!= &se
->interrupts
;
1384 curr
= curr
->next
) {
1385 if (curr
->u
.i
.unique
== req
->unique
) {
1386 req
->interrupted
= 1;
1392 curr
= se
->interrupts
.next
;
1393 if (curr
!= &se
->interrupts
) {
1395 list_init_req(curr
);
1402 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1404 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*)inarg
;
1406 if (req
->se
->op
.bmap
) {
1407 req
->se
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1409 fuse_reply_err(req
, ENOSYS
);
1413 static void do_ioctl(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1415 struct fuse_ioctl_in
*arg
= (struct fuse_ioctl_in
*)inarg
;
1416 unsigned int flags
= arg
->flags
;
1417 void *in_buf
= arg
->in_size
? PARAM(arg
) : NULL
;
1418 struct fuse_file_info fi
;
1420 if (flags
& FUSE_IOCTL_DIR
&& !(req
->se
->conn
.want
& FUSE_CAP_IOCTL_DIR
)) {
1421 fuse_reply_err(req
, ENOTTY
);
1425 memset(&fi
, 0, sizeof(fi
));
1428 if (sizeof(void *) == 4 && !(flags
& FUSE_IOCTL_32BIT
)) {
1429 req
->ioctl_64bit
= 1;
1432 if (req
->se
->op
.ioctl
) {
1433 req
->se
->op
.ioctl(req
, nodeid
, arg
->cmd
, (void *)(uintptr_t)arg
->arg
,
1434 &fi
, flags
, in_buf
, arg
->in_size
, arg
->out_size
);
1436 fuse_reply_err(req
, ENOSYS
);
1440 void fuse_pollhandle_destroy(struct fuse_pollhandle
*ph
)
1445 static void do_poll(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1447 struct fuse_poll_in
*arg
= (struct fuse_poll_in
*)inarg
;
1448 struct fuse_file_info fi
;
1450 memset(&fi
, 0, sizeof(fi
));
1452 fi
.poll_events
= arg
->events
;
1454 if (req
->se
->op
.poll
) {
1455 struct fuse_pollhandle
*ph
= NULL
;
1457 if (arg
->flags
& FUSE_POLL_SCHEDULE_NOTIFY
) {
1458 ph
= malloc(sizeof(struct fuse_pollhandle
));
1460 fuse_reply_err(req
, ENOMEM
);
1467 req
->se
->op
.poll(req
, nodeid
, &fi
, ph
);
1469 fuse_reply_err(req
, ENOSYS
);
1473 static void do_fallocate(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1475 struct fuse_fallocate_in
*arg
= (struct fuse_fallocate_in
*)inarg
;
1476 struct fuse_file_info fi
;
1478 memset(&fi
, 0, sizeof(fi
));
1481 if (req
->se
->op
.fallocate
) {
1482 req
->se
->op
.fallocate(req
, nodeid
, arg
->mode
, arg
->offset
, arg
->length
,
1485 fuse_reply_err(req
, ENOSYS
);
1489 static void do_copy_file_range(fuse_req_t req
, fuse_ino_t nodeid_in
,
1492 struct fuse_copy_file_range_in
*arg
=
1493 (struct fuse_copy_file_range_in
*)inarg
;
1494 struct fuse_file_info fi_in
, fi_out
;
1496 memset(&fi_in
, 0, sizeof(fi_in
));
1497 fi_in
.fh
= arg
->fh_in
;
1499 memset(&fi_out
, 0, sizeof(fi_out
));
1500 fi_out
.fh
= arg
->fh_out
;
1503 if (req
->se
->op
.copy_file_range
) {
1504 req
->se
->op
.copy_file_range(req
, nodeid_in
, arg
->off_in
, &fi_in
,
1505 arg
->nodeid_out
, arg
->off_out
, &fi_out
,
1506 arg
->len
, arg
->flags
);
1508 fuse_reply_err(req
, ENOSYS
);
1512 static void do_lseek(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1514 struct fuse_lseek_in
*arg
= (struct fuse_lseek_in
*)inarg
;
1515 struct fuse_file_info fi
;
1517 memset(&fi
, 0, sizeof(fi
));
1520 if (req
->se
->op
.lseek
) {
1521 req
->se
->op
.lseek(req
, nodeid
, arg
->offset
, arg
->whence
, &fi
);
1523 fuse_reply_err(req
, ENOSYS
);
1527 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1529 struct fuse_init_in
*arg
= (struct fuse_init_in
*)inarg
;
1530 struct fuse_init_out outarg
;
1531 struct fuse_session
*se
= req
->se
;
1532 size_t bufsize
= se
->bufsize
;
1533 size_t outargsize
= sizeof(outarg
);
1537 fuse_log(FUSE_LOG_DEBUG
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1538 if (arg
->major
== 7 && arg
->minor
>= 6) {
1539 fuse_log(FUSE_LOG_DEBUG
, "flags=0x%08x\n", arg
->flags
);
1540 fuse_log(FUSE_LOG_DEBUG
, "max_readahead=0x%08x\n",
1541 arg
->max_readahead
);
1544 se
->conn
.proto_major
= arg
->major
;
1545 se
->conn
.proto_minor
= arg
->minor
;
1546 se
->conn
.capable
= 0;
1549 memset(&outarg
, 0, sizeof(outarg
));
1550 outarg
.major
= FUSE_KERNEL_VERSION
;
1551 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1553 if (arg
->major
< 7 || (arg
->major
== 7 && arg
->minor
< 31)) {
1554 fuse_log(FUSE_LOG_ERR
, "fuse: unsupported protocol version: %u.%u\n",
1555 arg
->major
, arg
->minor
);
1556 fuse_reply_err(req
, EPROTO
);
1560 if (arg
->major
> 7) {
1561 /* Wait for a second INIT request with a 7.X version */
1562 send_reply_ok(req
, &outarg
, sizeof(outarg
));
1566 if (arg
->max_readahead
< se
->conn
.max_readahead
) {
1567 se
->conn
.max_readahead
= arg
->max_readahead
;
1569 if (arg
->flags
& FUSE_ASYNC_READ
) {
1570 se
->conn
.capable
|= FUSE_CAP_ASYNC_READ
;
1572 if (arg
->flags
& FUSE_POSIX_LOCKS
) {
1573 se
->conn
.capable
|= FUSE_CAP_POSIX_LOCKS
;
1575 if (arg
->flags
& FUSE_ATOMIC_O_TRUNC
) {
1576 se
->conn
.capable
|= FUSE_CAP_ATOMIC_O_TRUNC
;
1578 if (arg
->flags
& FUSE_EXPORT_SUPPORT
) {
1579 se
->conn
.capable
|= FUSE_CAP_EXPORT_SUPPORT
;
1581 if (arg
->flags
& FUSE_DONT_MASK
) {
1582 se
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1584 if (arg
->flags
& FUSE_FLOCK_LOCKS
) {
1585 se
->conn
.capable
|= FUSE_CAP_FLOCK_LOCKS
;
1587 if (arg
->flags
& FUSE_AUTO_INVAL_DATA
) {
1588 se
->conn
.capable
|= FUSE_CAP_AUTO_INVAL_DATA
;
1590 if (arg
->flags
& FUSE_DO_READDIRPLUS
) {
1591 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS
;
1593 if (arg
->flags
& FUSE_READDIRPLUS_AUTO
) {
1594 se
->conn
.capable
|= FUSE_CAP_READDIRPLUS_AUTO
;
1596 if (arg
->flags
& FUSE_ASYNC_DIO
) {
1597 se
->conn
.capable
|= FUSE_CAP_ASYNC_DIO
;
1599 if (arg
->flags
& FUSE_WRITEBACK_CACHE
) {
1600 se
->conn
.capable
|= FUSE_CAP_WRITEBACK_CACHE
;
1602 if (arg
->flags
& FUSE_NO_OPEN_SUPPORT
) {
1603 se
->conn
.capable
|= FUSE_CAP_NO_OPEN_SUPPORT
;
1605 if (arg
->flags
& FUSE_PARALLEL_DIROPS
) {
1606 se
->conn
.capable
|= FUSE_CAP_PARALLEL_DIROPS
;
1608 if (arg
->flags
& FUSE_POSIX_ACL
) {
1609 se
->conn
.capable
|= FUSE_CAP_POSIX_ACL
;
1611 if (arg
->flags
& FUSE_HANDLE_KILLPRIV
) {
1612 se
->conn
.capable
|= FUSE_CAP_HANDLE_KILLPRIV
;
1614 if (arg
->flags
& FUSE_NO_OPENDIR_SUPPORT
) {
1615 se
->conn
.capable
|= FUSE_CAP_NO_OPENDIR_SUPPORT
;
1617 if (!(arg
->flags
& FUSE_MAX_PAGES
)) {
1618 size_t max_bufsize
= FUSE_DEFAULT_MAX_PAGES_PER_REQ
* getpagesize() +
1619 FUSE_BUFFER_HEADER_SIZE
;
1620 if (bufsize
> max_bufsize
) {
1621 bufsize
= max_bufsize
;
1625 #ifdef HAVE_VMSPLICE
1626 se
->conn
.capable
|= FUSE_CAP_SPLICE_WRITE
| FUSE_CAP_SPLICE_MOVE
;
1628 se
->conn
.capable
|= FUSE_CAP_SPLICE_READ
;
1630 se
->conn
.capable
|= FUSE_CAP_IOCTL_DIR
;
1633 * Default settings for modern filesystems.
1635 * Most of these capabilities were disabled by default in
1636 * libfuse2 for backwards compatibility reasons. In libfuse3,
1637 * we can finally enable them by default (as long as they're
1638 * supported by the kernel).
1640 #define LL_SET_DEFAULT(cond, cap) \
1641 if ((cond) && (se->conn.capable & (cap))) \
1642 se->conn.want |= (cap)
1643 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ
);
1644 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS
);
1645 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA
);
1646 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV
);
1647 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO
);
1648 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR
);
1649 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC
);
1650 LL_SET_DEFAULT(se
->op
.write_buf
, FUSE_CAP_SPLICE_READ
);
1651 LL_SET_DEFAULT(se
->op
.getlk
&& se
->op
.setlk
, FUSE_CAP_POSIX_LOCKS
);
1652 LL_SET_DEFAULT(se
->op
.flock
, FUSE_CAP_FLOCK_LOCKS
);
1653 LL_SET_DEFAULT(se
->op
.readdirplus
, FUSE_CAP_READDIRPLUS
);
1654 LL_SET_DEFAULT(se
->op
.readdirplus
&& se
->op
.readdir
,
1655 FUSE_CAP_READDIRPLUS_AUTO
);
1656 se
->conn
.time_gran
= 1;
1658 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1659 fuse_log(FUSE_LOG_ERR
, "fuse: warning: buffer size too small: %zu\n",
1661 bufsize
= FUSE_MIN_READ_BUFFER
;
1663 se
->bufsize
= bufsize
;
1665 if (se
->conn
.max_write
> bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
1666 se
->conn
.max_write
= bufsize
- FUSE_BUFFER_HEADER_SIZE
;
1671 se
->op
.init(se
->userdata
, &se
->conn
);
1674 if (se
->conn
.want
& (~se
->conn
.capable
)) {
1675 fuse_log(FUSE_LOG_ERR
,
1676 "fuse: error: filesystem requested capabilities "
1677 "0x%x that are not supported by kernel, aborting.\n",
1678 se
->conn
.want
& (~se
->conn
.capable
));
1679 fuse_reply_err(req
, EPROTO
);
1680 se
->error
= -EPROTO
;
1681 fuse_session_exit(se
);
1685 if (se
->conn
.max_write
< bufsize
- FUSE_BUFFER_HEADER_SIZE
) {
1686 se
->bufsize
= se
->conn
.max_write
+ FUSE_BUFFER_HEADER_SIZE
;
1688 if (arg
->flags
& FUSE_MAX_PAGES
) {
1689 outarg
.flags
|= FUSE_MAX_PAGES
;
1690 outarg
.max_pages
= (se
->conn
.max_write
- 1) / getpagesize() + 1;
1694 * Always enable big writes, this is superseded
1695 * by the max_write option
1697 outarg
.flags
|= FUSE_BIG_WRITES
;
1699 if (se
->conn
.want
& FUSE_CAP_ASYNC_READ
) {
1700 outarg
.flags
|= FUSE_ASYNC_READ
;
1702 if (se
->conn
.want
& FUSE_CAP_POSIX_LOCKS
) {
1703 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1705 if (se
->conn
.want
& FUSE_CAP_ATOMIC_O_TRUNC
) {
1706 outarg
.flags
|= FUSE_ATOMIC_O_TRUNC
;
1708 if (se
->conn
.want
& FUSE_CAP_EXPORT_SUPPORT
) {
1709 outarg
.flags
|= FUSE_EXPORT_SUPPORT
;
1711 if (se
->conn
.want
& FUSE_CAP_DONT_MASK
) {
1712 outarg
.flags
|= FUSE_DONT_MASK
;
1714 if (se
->conn
.want
& FUSE_CAP_FLOCK_LOCKS
) {
1715 outarg
.flags
|= FUSE_FLOCK_LOCKS
;
1717 if (se
->conn
.want
& FUSE_CAP_AUTO_INVAL_DATA
) {
1718 outarg
.flags
|= FUSE_AUTO_INVAL_DATA
;
1720 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS
) {
1721 outarg
.flags
|= FUSE_DO_READDIRPLUS
;
1723 if (se
->conn
.want
& FUSE_CAP_READDIRPLUS_AUTO
) {
1724 outarg
.flags
|= FUSE_READDIRPLUS_AUTO
;
1726 if (se
->conn
.want
& FUSE_CAP_ASYNC_DIO
) {
1727 outarg
.flags
|= FUSE_ASYNC_DIO
;
1729 if (se
->conn
.want
& FUSE_CAP_WRITEBACK_CACHE
) {
1730 outarg
.flags
|= FUSE_WRITEBACK_CACHE
;
1732 if (se
->conn
.want
& FUSE_CAP_POSIX_ACL
) {
1733 outarg
.flags
|= FUSE_POSIX_ACL
;
1735 outarg
.max_readahead
= se
->conn
.max_readahead
;
1736 outarg
.max_write
= se
->conn
.max_write
;
1737 if (se
->conn
.max_background
>= (1 << 16)) {
1738 se
->conn
.max_background
= (1 << 16) - 1;
1740 if (se
->conn
.congestion_threshold
> se
->conn
.max_background
) {
1741 se
->conn
.congestion_threshold
= se
->conn
.max_background
;
1743 if (!se
->conn
.congestion_threshold
) {
1744 se
->conn
.congestion_threshold
= se
->conn
.max_background
* 3 / 4;
1747 outarg
.max_background
= se
->conn
.max_background
;
1748 outarg
.congestion_threshold
= se
->conn
.congestion_threshold
;
1749 outarg
.time_gran
= se
->conn
.time_gran
;
1752 fuse_log(FUSE_LOG_DEBUG
, " INIT: %u.%u\n", outarg
.major
,
1754 fuse_log(FUSE_LOG_DEBUG
, " flags=0x%08x\n", outarg
.flags
);
1755 fuse_log(FUSE_LOG_DEBUG
, " max_readahead=0x%08x\n",
1756 outarg
.max_readahead
);
1757 fuse_log(FUSE_LOG_DEBUG
, " max_write=0x%08x\n", outarg
.max_write
);
1758 fuse_log(FUSE_LOG_DEBUG
, " max_background=%i\n",
1759 outarg
.max_background
);
1760 fuse_log(FUSE_LOG_DEBUG
, " congestion_threshold=%i\n",
1761 outarg
.congestion_threshold
);
1762 fuse_log(FUSE_LOG_DEBUG
, " time_gran=%u\n", outarg
.time_gran
);
1765 send_reply_ok(req
, &outarg
, outargsize
);
1768 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1770 struct fuse_session
*se
= req
->se
;
1775 se
->got_destroy
= 1;
1776 if (se
->op
.destroy
) {
1777 se
->op
.destroy(se
->userdata
);
1780 send_reply_ok(req
, NULL
, 0);
1783 static int send_notify_iov(struct fuse_session
*se
, int notify_code
,
1784 struct iovec
*iov
, int count
)
1786 struct fuse_out_header out
;
1788 if (!se
->got_init
) {
1793 out
.error
= notify_code
;
1794 iov
[0].iov_base
= &out
;
1795 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
1797 return fuse_send_msg(se
, NULL
, iov
, count
);
1800 int fuse_lowlevel_notify_poll(struct fuse_pollhandle
*ph
)
1803 struct fuse_notify_poll_wakeup_out outarg
;
1804 struct iovec iov
[2];
1808 iov
[1].iov_base
= &outarg
;
1809 iov
[1].iov_len
= sizeof(outarg
);
1811 return send_notify_iov(ph
->se
, FUSE_NOTIFY_POLL
, iov
, 2);
1817 int fuse_lowlevel_notify_inval_inode(struct fuse_session
*se
, fuse_ino_t ino
,
1818 off_t off
, off_t len
)
1820 struct fuse_notify_inval_inode_out outarg
;
1821 struct iovec iov
[2];
1831 iov
[1].iov_base
= &outarg
;
1832 iov
[1].iov_len
= sizeof(outarg
);
1834 return send_notify_iov(se
, FUSE_NOTIFY_INVAL_INODE
, iov
, 2);
1837 int fuse_lowlevel_notify_inval_entry(struct fuse_session
*se
, fuse_ino_t parent
,
1838 const char *name
, size_t namelen
)
1840 struct fuse_notify_inval_entry_out outarg
;
1841 struct iovec iov
[3];
1847 outarg
.parent
= parent
;
1848 outarg
.namelen
= namelen
;
1851 iov
[1].iov_base
= &outarg
;
1852 iov
[1].iov_len
= sizeof(outarg
);
1853 iov
[2].iov_base
= (void *)name
;
1854 iov
[2].iov_len
= namelen
+ 1;
1856 return send_notify_iov(se
, FUSE_NOTIFY_INVAL_ENTRY
, iov
, 3);
1859 int fuse_lowlevel_notify_delete(struct fuse_session
*se
, fuse_ino_t parent
,
1860 fuse_ino_t child
, const char *name
,
1863 struct fuse_notify_delete_out outarg
;
1864 struct iovec iov
[3];
1870 outarg
.parent
= parent
;
1871 outarg
.child
= child
;
1872 outarg
.namelen
= namelen
;
1875 iov
[1].iov_base
= &outarg
;
1876 iov
[1].iov_len
= sizeof(outarg
);
1877 iov
[2].iov_base
= (void *)name
;
1878 iov
[2].iov_len
= namelen
+ 1;
1880 return send_notify_iov(se
, FUSE_NOTIFY_DELETE
, iov
, 3);
1883 int fuse_lowlevel_notify_store(struct fuse_session
*se
, fuse_ino_t ino
,
1884 off_t offset
, struct fuse_bufvec
*bufv
)
1886 struct fuse_out_header out
;
1887 struct fuse_notify_store_out outarg
;
1888 struct iovec iov
[3];
1889 size_t size
= fuse_buf_size(bufv
);
1897 out
.error
= FUSE_NOTIFY_STORE
;
1899 outarg
.nodeid
= ino
;
1900 outarg
.offset
= offset
;
1904 iov
[0].iov_base
= &out
;
1905 iov
[0].iov_len
= sizeof(out
);
1906 iov
[1].iov_base
= &outarg
;
1907 iov
[1].iov_len
= sizeof(outarg
);
1909 res
= fuse_send_data_iov(se
, NULL
, iov
, 2, bufv
);
1917 void *fuse_req_userdata(fuse_req_t req
)
1919 return req
->se
->userdata
;
1922 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
1927 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
1930 pthread_mutex_lock(&req
->lock
);
1931 pthread_mutex_lock(&req
->se
->lock
);
1932 req
->u
.ni
.func
= func
;
1933 req
->u
.ni
.data
= data
;
1934 pthread_mutex_unlock(&req
->se
->lock
);
1935 if (req
->interrupted
&& func
) {
1938 pthread_mutex_unlock(&req
->lock
);
1941 int fuse_req_interrupted(fuse_req_t req
)
1945 pthread_mutex_lock(&req
->se
->lock
);
1946 interrupted
= req
->interrupted
;
1947 pthread_mutex_unlock(&req
->se
->lock
);
1953 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
1956 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
1957 [FUSE_FORGET
] = { do_forget
, "FORGET" },
1958 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
1959 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
1960 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
1961 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
1962 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
1963 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
1964 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
1965 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
1966 [FUSE_RENAME
] = { do_rename
, "RENAME" },
1967 [FUSE_LINK
] = { do_link
, "LINK" },
1968 [FUSE_OPEN
] = { do_open
, "OPEN" },
1969 [FUSE_READ
] = { do_read
, "READ" },
1970 [FUSE_WRITE
] = { do_write
, "WRITE" },
1971 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
1972 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
1973 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
1974 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
1975 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
1976 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
1977 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
1978 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
1979 [FUSE_INIT
] = { do_init
, "INIT" },
1980 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
1981 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
1982 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
1983 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
1984 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
1985 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
1986 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
1987 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
1988 [FUSE_CREATE
] = { do_create
, "CREATE" },
1989 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
1990 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
1991 [FUSE_IOCTL
] = { do_ioctl
, "IOCTL" },
1992 [FUSE_POLL
] = { do_poll
, "POLL" },
1993 [FUSE_FALLOCATE
] = { do_fallocate
, "FALLOCATE" },
1994 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
1995 [FUSE_NOTIFY_REPLY
] = { NULL
, "NOTIFY_REPLY" },
1996 [FUSE_BATCH_FORGET
] = { do_batch_forget
, "BATCH_FORGET" },
1997 [FUSE_READDIRPLUS
] = { do_readdirplus
, "READDIRPLUS" },
1998 [FUSE_RENAME2
] = { do_rename2
, "RENAME2" },
1999 [FUSE_COPY_FILE_RANGE
] = { do_copy_file_range
, "COPY_FILE_RANGE" },
2000 [FUSE_LSEEK
] = { do_lseek
, "LSEEK" },
2003 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2005 static const char *opname(enum fuse_opcode opcode
)
2007 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
) {
2010 return fuse_ll_ops
[opcode
].name
;
2014 void fuse_session_process_buf(struct fuse_session
*se
,
2015 const struct fuse_buf
*buf
)
2017 fuse_session_process_buf_int(se
, buf
, NULL
);
2020 void fuse_session_process_buf_int(struct fuse_session
*se
,
2021 const struct fuse_buf
*buf
,
2022 struct fuse_chan
*ch
)
2024 struct fuse_in_header
*in
;
2026 struct fuse_req
*req
;
2032 fuse_log(FUSE_LOG_DEBUG
,
2033 "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, "
2035 (unsigned long long)in
->unique
,
2036 opname((enum fuse_opcode
)in
->opcode
), in
->opcode
,
2037 (unsigned long long)in
->nodeid
, buf
->size
, in
->pid
);
2040 req
= fuse_ll_alloc_req(se
);
2042 struct fuse_out_header out
= {
2043 .unique
= in
->unique
,
2046 struct iovec iov
= {
2048 .iov_len
= sizeof(struct fuse_out_header
),
2051 fuse_send_msg(se
, ch
, &iov
, 1);
2055 req
->unique
= in
->unique
;
2056 req
->ctx
.uid
= in
->uid
;
2057 req
->ctx
.gid
= in
->gid
;
2058 req
->ctx
.pid
= in
->pid
;
2062 if (!se
->got_init
) {
2063 enum fuse_opcode expected
;
2065 expected
= se
->cuse_data
? CUSE_INIT
: FUSE_INIT
;
2066 if (in
->opcode
!= expected
) {
2069 } else if (in
->opcode
== FUSE_INIT
|| in
->opcode
== CUSE_INIT
) {
2074 /* Implement -o allow_root */
2075 if (se
->deny_others
&& in
->uid
!= se
->owner
&& in
->uid
!= 0 &&
2076 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
2077 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
2078 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
2079 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
&&
2080 in
->opcode
!= FUSE_NOTIFY_REPLY
&& in
->opcode
!= FUSE_READDIRPLUS
) {
2085 if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
) {
2088 if (in
->opcode
!= FUSE_INTERRUPT
) {
2089 struct fuse_req
*intr
;
2090 pthread_mutex_lock(&se
->lock
);
2091 intr
= check_interrupt(se
, req
);
2092 list_add_req(req
, &se
->list
);
2093 pthread_mutex_unlock(&se
->lock
);
2095 fuse_reply_err(intr
, EAGAIN
);
2099 inarg
= (void *)&in
[1];
2100 if (in
->opcode
== FUSE_WRITE
&& se
->op
.write_buf
) {
2101 do_write_buf(req
, in
->nodeid
, inarg
, buf
);
2103 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
2109 fuse_reply_err(req
, err
);
2112 #define LL_OPTION(n, o, v) \
2114 n, offsetof(struct fuse_session, o), v \
2117 static const struct fuse_opt fuse_ll_opts
[] = {
2118 LL_OPTION("debug", debug
, 1),
2119 LL_OPTION("-d", debug
, 1),
2120 LL_OPTION("--debug", debug
, 1),
2121 LL_OPTION("allow_root", deny_others
, 1),
2122 LL_OPTION("--socket-path=%s", vu_socket_path
, 0),
2126 void fuse_lowlevel_version(void)
2128 printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION
,
2129 FUSE_KERNEL_MINOR_VERSION
);
2132 void fuse_lowlevel_help(void)
2135 * These are not all options, but the ones that are
2136 * potentially of interest to an end-user
2139 " -o allow_root allow access by root\n"
2140 " --socket-path=PATH path for the vhost-user socket\n");
2143 void fuse_session_destroy(struct fuse_session
*se
)
2145 if (se
->got_init
&& !se
->got_destroy
) {
2146 if (se
->op
.destroy
) {
2147 se
->op
.destroy(se
->userdata
);
2150 pthread_mutex_destroy(&se
->lock
);
2151 free(se
->cuse_data
);
2159 struct fuse_session
*fuse_session_new(struct fuse_args
*args
,
2160 const struct fuse_lowlevel_ops
*op
,
2161 size_t op_size
, void *userdata
)
2163 struct fuse_session
*se
;
2165 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
2168 "fuse: warning: library too old, some operations may not work\n");
2169 op_size
= sizeof(struct fuse_lowlevel_ops
);
2172 if (args
->argc
== 0) {
2173 fuse_log(FUSE_LOG_ERR
,
2174 "fuse: empty argv passed to fuse_session_new().\n");
2178 se
= (struct fuse_session
*)calloc(1, sizeof(struct fuse_session
));
2180 fuse_log(FUSE_LOG_ERR
, "fuse: failed to allocate fuse object\n");
2184 se
->conn
.max_write
= UINT_MAX
;
2185 se
->conn
.max_readahead
= UINT_MAX
;
2188 if (fuse_opt_parse(args
, se
, fuse_ll_opts
, NULL
) == -1) {
2191 if (args
->argc
== 1 && args
->argv
[0][0] == '-') {
2192 fuse_log(FUSE_LOG_ERR
,
2193 "fuse: warning: argv[0] looks like an option, but "
2194 "will be ignored\n");
2195 } else if (args
->argc
!= 1) {
2197 fuse_log(FUSE_LOG_ERR
, "fuse: unknown option(s): `");
2198 for (i
= 1; i
< args
->argc
- 1; i
++) {
2199 fuse_log(FUSE_LOG_ERR
, "%s ", args
->argv
[i
]);
2201 fuse_log(FUSE_LOG_ERR
, "%s'\n", args
->argv
[i
]);
2205 se
->bufsize
= FUSE_MAX_MAX_PAGES
* getpagesize() + FUSE_BUFFER_HEADER_SIZE
;
2207 list_init_req(&se
->list
);
2208 list_init_req(&se
->interrupts
);
2209 fuse_mutex_init(&se
->lock
);
2211 memcpy(&se
->op
, op
, op_size
);
2212 se
->owner
= getuid();
2213 se
->userdata
= userdata
;
2218 fuse_opt_free_args(args
);
2225 int fuse_session_mount(struct fuse_session
*se
)
2230 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
2234 fd
= open("/dev/null", O_RDWR
);
2238 } while (fd
>= 0 && fd
<= 2);
2241 * To allow FUSE daemons to run without privileges, the caller may open
2242 * /dev/fuse before launching the file system and pass on the file
2243 * descriptor by specifying /dev/fd/N as the mount point. Note that the
2244 * parent process takes care of performing the mount in this case.
2246 fd
= fuse_mnt_parse_fuse_fd(mountpoint
);
2248 if (fcntl(fd
, F_GETFD
) == -1) {
2249 fuse_log(FUSE_LOG_ERR
, "fuse: Invalid file descriptor /dev/fd/%u\n",
2258 fd
= fuse_kern_mount(mountpoint
, se
->mo
);
2264 /* Save mountpoint */
2265 se
->mountpoint
= strdup(mountpoint
);
2266 if (se
->mountpoint
== NULL
) {
2273 fuse_kern_unmount(mountpoint
, fd
);
2277 int fuse_session_fd(struct fuse_session
*se
)
2282 void fuse_session_unmount(struct fuse_session
*se
)
2287 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2290 size_t bufsize
= 1024;
2294 unsigned long pid
= req
->ctx
.pid
;
2297 sprintf(path
, "/proc/%lu/task/%lu/status", pid
, pid
);
2300 buf
= malloc(bufsize
);
2306 fd
= open(path
, O_RDONLY
);
2311 ret
= read(fd
, buf
, bufsize
);
2318 if ((size_t)ret
== bufsize
) {
2325 s
= strstr(buf
, "\nGroups:");
2334 unsigned long val
= strtoul(s
, &end
, 0);
2352 * This is currently not implemented on other than Linux...
2354 int fuse_req_getgroups(fuse_req_t req
, int size
, gid_t list
[])
2363 void fuse_session_exit(struct fuse_session
*se
)
2368 void fuse_session_reset(struct fuse_session
*se
)
2374 int fuse_session_exited(struct fuse_session
*se
)