2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
10 #include "fuse_lowlevel.h"
11 #include "fuse_kernel.h"
14 #include "fuse_misc.h"
15 #include "fuse_lowlevel_compat.h"
25 #define PARAM(inarg) (((const char *)(inarg)) + sizeof(*(inarg)))
26 #define OFFSET_MAX 0x7fffffffffffffffLL
43 fuse_interrupt_func_t func
;
47 struct fuse_req
*next
;
48 struct fuse_req
*prev
;
54 struct fuse_lowlevel_ops op
;
58 struct fuse_conn_info conn
;
60 struct fuse_req interrupts
;
65 static void convert_stat(const struct stat
*stbuf
, struct fuse_attr
*attr
)
67 attr
->ino
= stbuf
->st_ino
;
68 attr
->mode
= stbuf
->st_mode
;
69 attr
->nlink
= stbuf
->st_nlink
;
70 attr
->uid
= stbuf
->st_uid
;
71 attr
->gid
= stbuf
->st_gid
;
72 attr
->rdev
= stbuf
->st_rdev
;
73 attr
->size
= stbuf
->st_size
;
74 attr
->blocks
= stbuf
->st_blocks
;
75 attr
->atime
= stbuf
->st_atime
;
76 attr
->mtime
= stbuf
->st_mtime
;
77 attr
->ctime
= stbuf
->st_ctime
;
78 attr
->atimensec
= ST_ATIM_NSEC(stbuf
);
79 attr
->mtimensec
= ST_MTIM_NSEC(stbuf
);
80 attr
->ctimensec
= ST_CTIM_NSEC(stbuf
);
82 attr
->filling
= 0; /* JPA trying to be safe */
86 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
88 stbuf
->st_mode
= attr
->mode
;
89 stbuf
->st_uid
= attr
->uid
;
90 stbuf
->st_gid
= attr
->gid
;
91 stbuf
->st_size
= attr
->size
;
92 stbuf
->st_atime
= attr
->atime
;
93 stbuf
->st_mtime
= attr
->mtime
;
94 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
95 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
98 static size_t iov_length(const struct iovec
*iov
, size_t count
)
103 for (seg
= 0; seg
< count
; seg
++)
104 ret
+= iov
[seg
].iov_len
;
108 static void list_init_req(struct fuse_req
*req
)
114 static void list_del_req(struct fuse_req
*req
)
116 struct fuse_req
*prev
= req
->prev
;
117 struct fuse_req
*next
= req
->next
;
122 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
124 struct fuse_req
*prev
= next
->prev
;
131 static void destroy_req(fuse_req_t req
)
133 pthread_mutex_destroy(&req
->lock
);
137 static void free_req(fuse_req_t req
)
140 struct fuse_ll
*f
= req
->f
;
142 pthread_mutex_lock(&req
->lock
);
143 req
->u
.ni
.func
= NULL
;
144 req
->u
.ni
.data
= NULL
;
145 pthread_mutex_unlock(&req
->lock
);
147 pthread_mutex_lock(&f
->lock
);
150 pthread_mutex_unlock(&f
->lock
);
155 static int send_reply_iov(fuse_req_t req
, int error
, struct iovec
*iov
,
158 struct fuse_out_header out
;
161 if (error
<= -1000 || error
> 0) {
162 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
166 out
.unique
= req
->unique
;
168 iov
[0].iov_base
= &out
;
169 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
170 out
.len
= iov_length(iov
, count
);
173 fprintf(stderr
, " unique: %llu, error: %i (%s), outsize: %i\n",
174 (unsigned long long) out
.unique
, out
.error
,
175 strerror(-out
.error
), out
.len
);
176 res
= fuse_chan_send(req
->ch
, iov
, count
);
182 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
188 iov
[1].iov_base
= (void *) arg
;
189 iov
[1].iov_len
= argsize
;
192 return send_reply_iov(req
, error
, iov
, count
);
196 int fuse_reply_iov(fuse_req_t req
, const struct iovec
*iov
, int count
)
199 struct iovec
*padded_iov
;
201 padded_iov
= malloc((count
+ 1) * sizeof(struct iovec
));
202 if (padded_iov
== NULL
)
203 return fuse_reply_err(req
, -ENOMEM
);
205 memcpy(padded_iov
+ 1, iov
, count
* sizeof(struct iovec
));
208 res
= send_reply_iov(req
, 0, padded_iov
, count
);
215 size_t fuse_dirent_size(size_t namelen
)
217 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
220 char *fuse_add_dirent(char *buf
, const char *name
, const struct stat
*stbuf
,
223 unsigned namelen
= strlen(name
);
224 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
225 unsigned entsize
= fuse_dirent_size(namelen
);
226 unsigned padlen
= entsize
- entlen
;
227 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
229 dirent
->ino
= stbuf
->st_ino
;
231 dirent
->namelen
= namelen
;
232 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
233 strncpy(dirent
->name
, name
, namelen
);
235 memset(buf
+ entlen
, 0, padlen
);
237 return buf
+ entsize
;
240 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
241 const char *name
, const struct stat
*stbuf
, off_t off
)
246 entsize
= fuse_dirent_size(strlen(name
));
247 if (entsize
<= bufsize
&& buf
)
248 fuse_add_dirent(buf
, name
, stbuf
, off
);
252 static void convert_statfs(const struct statvfs
*stbuf
,
253 struct fuse_kstatfs
*kstatfs
)
255 kstatfs
->bsize
= stbuf
->f_bsize
;
256 kstatfs
->frsize
= stbuf
->f_frsize
;
257 kstatfs
->blocks
= stbuf
->f_blocks
;
258 kstatfs
->bfree
= stbuf
->f_bfree
;
259 kstatfs
->bavail
= stbuf
->f_bavail
;
260 kstatfs
->files
= stbuf
->f_files
;
261 kstatfs
->ffree
= stbuf
->f_ffree
;
262 kstatfs
->namelen
= stbuf
->f_namemax
;
265 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
267 return send_reply(req
, 0, arg
, argsize
);
270 int fuse_reply_err(fuse_req_t req
, int err
)
272 return send_reply(req
, -err
, NULL
, 0);
275 void fuse_reply_none(fuse_req_t req
)
277 fuse_chan_send(req
->ch
, NULL
, 0);
281 static unsigned long calc_timeout_sec(double t
)
283 if (t
> (double) ULONG_MAX
)
288 return (unsigned long) t
;
291 static unsigned int calc_timeout_nsec(double t
)
293 unsigned long secs
= calc_timeout_sec(t
);
294 double f
= t
- (double)secs
;
297 else if (f
>= 0.999999999)
300 return (unsigned int) (f
* 1.0e9
);
303 static void fill_entry(struct fuse_entry_out
*arg
,
304 const struct fuse_entry_param
*e
)
306 arg
->nodeid
= e
->ino
;
307 arg
->generation
= e
->generation
;
308 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
309 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
310 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
311 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
312 convert_stat(&e
->attr
, &arg
->attr
);
315 static void fill_open(struct fuse_open_out
*arg
,
316 const struct fuse_file_info
*f
)
320 arg
->open_flags
|= FOPEN_DIRECT_IO
;
322 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
325 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
327 struct fuse_entry_out arg
;
329 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
331 if (!e
->ino
&& req
->f
->conn
.proto_minor
< 4)
332 return fuse_reply_err(req
, ENOENT
);
334 memset(&arg
, 0, sizeof(arg
));
337 return send_reply_ok(req
, &arg
, (req
->f
->conn
.proto_minor
>= 12
338 ? sizeof(arg
) : FUSE_COMPAT_ENTRY_OUT_SIZE
));
340 return send_reply_ok(req
, &arg
, sizeof(arg
));
344 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
345 const struct fuse_file_info
*f
)
348 struct fuse_entry_out e
;
349 struct fuse_open_out o
;
352 memset(&arg
, 0, sizeof(arg
));
353 fill_entry(&arg
.e
, e
);
355 if (req
->f
->conn
.proto_minor
< 12) {
356 fill_open((struct fuse_open_out
*)
357 ((char*)&arg
+ FUSE_COMPAT_ENTRY_OUT_SIZE
), f
);
358 return send_reply_ok(req
, &arg
,
359 FUSE_COMPAT_ENTRY_OUT_SIZE
+ sizeof(struct fuse_open_out
));
361 fill_open(&arg
.o
, f
);
362 return send_reply_ok(req
, &arg
, sizeof(arg
));
365 fill_open(&arg
.o
, f
);
366 return send_reply_ok(req
, &arg
, sizeof(arg
));
370 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
373 struct fuse_attr_out arg
;
375 memset(&arg
, 0, sizeof(arg
));
376 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
377 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
378 convert_stat(attr
, &arg
.attr
);
381 return send_reply_ok(req
, &arg
, (req
->f
->conn
.proto_minor
>= 12
382 ? sizeof(arg
) : FUSE_COMPAT_FUSE_ATTR_OUT_SIZE
));
384 return send_reply_ok(req
, &arg
, sizeof(arg
));
388 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
390 return send_reply_ok(req
, linkname
, strlen(linkname
));
393 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
395 struct fuse_open_out arg
;
397 memset(&arg
, 0, sizeof(arg
));
399 return send_reply_ok(req
, &arg
, sizeof(arg
));
402 int fuse_reply_write(fuse_req_t req
, size_t count
)
404 struct fuse_write_out arg
;
406 memset(&arg
, 0, sizeof(arg
));
409 return send_reply_ok(req
, &arg
, sizeof(arg
));
412 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
414 return send_reply_ok(req
, buf
, size
);
417 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
419 struct fuse_statfs_out arg
;
420 size_t size
= req
->f
->conn
.proto_minor
< 4 ? FUSE_COMPAT_STATFS_SIZE
: sizeof(arg
);
422 memset(&arg
, 0, sizeof(arg
));
423 convert_statfs(stbuf
, &arg
.st
);
425 return send_reply_ok(req
, &arg
, size
);
428 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
430 struct fuse_getxattr_out arg
;
432 memset(&arg
, 0, sizeof(arg
));
435 return send_reply_ok(req
, &arg
, sizeof(arg
));
438 int fuse_reply_lock(fuse_req_t req
, struct flock
*lock
)
440 struct fuse_lk_out arg
;
442 memset(&arg
, 0, sizeof(arg
));
443 arg
.lk
.type
= lock
->l_type
;
444 if (lock
->l_type
!= F_UNLCK
) {
445 arg
.lk
.start
= lock
->l_start
;
446 if (lock
->l_len
== 0)
447 arg
.lk
.end
= OFFSET_MAX
;
449 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
451 arg
.lk
.pid
= lock
->l_pid
;
452 return send_reply_ok(req
, &arg
, sizeof(arg
));
455 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
457 struct fuse_bmap_out arg
;
459 memset(&arg
, 0, sizeof(arg
));
462 return send_reply_ok(req
, &arg
, sizeof(arg
));
465 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
467 const char *name
= (const char *) inarg
;
469 if (req
->f
->op
.lookup
)
470 req
->f
->op
.lookup(req
, nodeid
, name
);
472 fuse_reply_err(req
, ENOSYS
);
475 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
477 const struct fuse_forget_in
*arg
= (const struct fuse_forget_in
*) inarg
;
479 if (req
->f
->op
.forget
)
480 req
->f
->op
.forget(req
, nodeid
, arg
->nlookup
);
482 fuse_reply_none(req
);
485 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
489 if (req
->f
->op
.getattr
)
490 req
->f
->op
.getattr(req
, nodeid
, NULL
);
492 fuse_reply_err(req
, ENOSYS
);
495 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
497 const struct fuse_setattr_in
*arg
= (const struct fuse_setattr_in
*) inarg
;
499 if (req
->f
->op
.setattr
) {
500 struct fuse_file_info
*fi
= NULL
;
501 struct fuse_file_info fi_store
;
503 memset(&stbuf
, 0, sizeof(stbuf
));
504 convert_attr(arg
, &stbuf
);
505 if (arg
->valid
& FATTR_FH
) {
506 memset(&fi_store
, 0, sizeof(fi_store
));
511 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
& ~FATTR_FH
, fi
);
513 fuse_reply_err(req
, ENOSYS
);
516 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
518 const struct fuse_access_in
*arg
= (const struct fuse_access_in
*) inarg
;
520 if (req
->f
->op
.access
)
521 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
523 fuse_reply_err(req
, ENOSYS
);
526 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
530 if (req
->f
->op
.readlink
)
531 req
->f
->op
.readlink(req
, nodeid
);
533 fuse_reply_err(req
, ENOSYS
);
536 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
538 const struct fuse_mknod_in
*arg
= (const struct fuse_mknod_in
*) inarg
;
539 const char *name
= PARAM(arg
);
542 if (req
->f
->conn
.proto_minor
>= 12)
543 req
->ctx
.umask
= arg
->umask
;
546 name
= (const char *) inarg
+ FUSE_COMPAT_MKNOD_IN_SIZE
;
548 if (req
->f
->op
.mknod
)
549 req
->f
->op
.mknod(req
, nodeid
, name
, arg
->mode
, arg
->rdev
);
551 fuse_reply_err(req
, ENOSYS
);
554 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
556 const struct fuse_mkdir_in
*arg
= (const struct fuse_mkdir_in
*) inarg
;
559 if (req
->f
->conn
.proto_minor
>= 12)
560 req
->ctx
.umask
= arg
->umask
;
563 if (req
->f
->op
.mkdir
)
564 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
566 fuse_reply_err(req
, ENOSYS
);
569 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
571 const char *name
= (const char *) inarg
;
573 if (req
->f
->op
.unlink
)
574 req
->f
->op
.unlink(req
, nodeid
, name
);
576 fuse_reply_err(req
, ENOSYS
);
579 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
581 const char *name
= (const char *) inarg
;
583 if (req
->f
->op
.rmdir
)
584 req
->f
->op
.rmdir(req
, nodeid
, name
);
586 fuse_reply_err(req
, ENOSYS
);
589 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
591 const char *name
= (const char *) inarg
;
592 const char *linkname
= ((const char *) inarg
) + strlen((const char *) inarg
) + 1;
594 if (req
->f
->op
.symlink
)
595 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
597 fuse_reply_err(req
, ENOSYS
);
600 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
602 const struct fuse_rename_in
*arg
= (const struct fuse_rename_in
*) inarg
;
603 const char *oldname
= PARAM(arg
);
604 const char *newname
= oldname
+ strlen(oldname
) + 1;
606 if (req
->f
->op
.rename
)
607 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
609 fuse_reply_err(req
, ENOSYS
);
612 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
614 const struct fuse_link_in
*arg
= (const struct fuse_link_in
*) inarg
;
617 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
619 fuse_reply_err(req
, ENOSYS
);
622 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
624 const struct fuse_create_in
*arg
= (const struct fuse_create_in
*) inarg
;
626 if (req
->f
->op
.create
) {
627 struct fuse_file_info fi
;
628 const char *name
= PARAM(arg
);
630 memset(&fi
, 0, sizeof(fi
));
631 fi
.flags
= arg
->flags
;
634 if (req
->f
->conn
.proto_minor
>= 12)
635 req
->ctx
.umask
= arg
->umask
;
638 name
= (const char *) inarg
+ sizeof(struct fuse_open_in
);
640 req
->f
->op
.create(req
, nodeid
, name
, arg
->mode
, &fi
);
642 fuse_reply_err(req
, ENOSYS
);
645 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
647 const struct fuse_open_in
*arg
= (const struct fuse_open_in
*) inarg
;
648 struct fuse_file_info fi
;
650 memset(&fi
, 0, sizeof(fi
));
651 fi
.flags
= arg
->flags
;
654 req
->f
->op
.open(req
, nodeid
, &fi
);
656 fuse_reply_open(req
, &fi
);
659 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
661 const struct fuse_read_in
*arg
= (const struct fuse_read_in
*) inarg
;
663 if (req
->f
->op
.read
) {
664 struct fuse_file_info fi
;
666 memset(&fi
, 0, sizeof(fi
));
669 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
671 fuse_reply_err(req
, ENOSYS
);
674 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
676 const struct fuse_write_in
*arg
= (const struct fuse_write_in
*) inarg
;
677 struct fuse_file_info fi
;
679 memset(&fi
, 0, sizeof(fi
));
682 fi
.writepage
= arg
->write_flags
& 1;
684 if (req
->f
->op
.write
) {
688 if (req
->f
->conn
.proto_minor
>= 12)
691 buf
= ((const char*)arg
) + FUSE_COMPAT_WRITE_IN_SIZE
;
692 req
->f
->op
.write(req
, nodeid
, buf
, arg
->size
, arg
->offset
, &fi
);
694 req
->f
->op
.write(req
, nodeid
, PARAM(arg
), arg
->size
, arg
->offset
, &fi
);
697 fuse_reply_err(req
, ENOSYS
);
700 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
702 const struct fuse_flush_in
*arg
= (const struct fuse_flush_in
*) inarg
;
703 struct fuse_file_info fi
;
705 memset(&fi
, 0, sizeof(fi
));
709 if (req
->f
->conn
.proto_minor
>= 7)
710 fi
.lock_owner
= arg
->lock_owner
;
712 if (req
->f
->op
.flush
)
713 req
->f
->op
.flush(req
, nodeid
, &fi
);
715 fuse_reply_err(req
, ENOSYS
);
718 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
720 const struct fuse_release_in
*arg
= (const struct fuse_release_in
*) inarg
;
721 struct fuse_file_info fi
;
723 memset(&fi
, 0, sizeof(fi
));
724 fi
.flags
= arg
->flags
;
727 if (req
->f
->conn
.proto_minor
>= 8) {
728 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
729 fi
.lock_owner
= arg
->lock_owner
;
732 if (req
->f
->op
.release
)
733 req
->f
->op
.release(req
, nodeid
, &fi
);
735 fuse_reply_err(req
, 0);
738 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
740 const struct fuse_fsync_in
*arg
= (const struct fuse_fsync_in
*) inarg
;
741 struct fuse_file_info fi
;
743 memset(&fi
, 0, sizeof(fi
));
747 if (req
->f
->op
.fsync
)
748 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
750 fuse_reply_err(req
, ENOSYS
);
753 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
755 const struct fuse_open_in
*arg
= (const struct fuse_open_in
*) inarg
;
756 struct fuse_file_info fi
;
758 memset(&fi
, 0, sizeof(fi
));
759 fi
.flags
= arg
->flags
;
761 if (req
->f
->op
.opendir
)
762 req
->f
->op
.opendir(req
, nodeid
, &fi
);
764 fuse_reply_open(req
, &fi
);
767 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
769 const struct fuse_read_in
*arg
= (const struct fuse_read_in
*) inarg
;
770 struct fuse_file_info fi
;
772 memset(&fi
, 0, sizeof(fi
));
776 if (req
->f
->op
.readdir
)
777 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
779 fuse_reply_err(req
, ENOSYS
);
782 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
784 const struct fuse_release_in
*arg
= (const struct fuse_release_in
*) inarg
;
785 struct fuse_file_info fi
;
787 memset(&fi
, 0, sizeof(fi
));
788 fi
.flags
= arg
->flags
;
792 if (req
->f
->op
.releasedir
)
793 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
795 fuse_reply_err(req
, 0);
798 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
800 const struct fuse_fsync_in
*arg
= (const struct fuse_fsync_in
*) inarg
;
801 struct fuse_file_info fi
;
803 memset(&fi
, 0, sizeof(fi
));
807 if (req
->f
->op
.fsyncdir
)
808 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
810 fuse_reply_err(req
, ENOSYS
);
813 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
818 if (req
->f
->op
.statfs
)
819 req
->f
->op
.statfs(req
, nodeid
);
821 struct statvfs buf
= {
825 fuse_reply_statfs(req
, &buf
);
829 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
831 const struct fuse_setxattr_in
*arg
= (const struct fuse_setxattr_in
*) inarg
;
832 const char *name
= PARAM(arg
);
833 const char *value
= name
+ strlen(name
) + 1;
835 if (req
->f
->op
.setxattr
)
836 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
, arg
->flags
);
838 fuse_reply_err(req
, ENOSYS
);
841 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
843 const struct fuse_getxattr_in
*arg
= (const struct fuse_getxattr_in
*) inarg
;
845 if (req
->f
->op
.getxattr
)
846 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
848 fuse_reply_err(req
, ENOSYS
);
851 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
853 const struct fuse_getxattr_in
*arg
= (const struct fuse_getxattr_in
*) inarg
;
855 if (req
->f
->op
.listxattr
)
856 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
858 fuse_reply_err(req
, ENOSYS
);
861 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
863 const char *name
= (const char *) inarg
;
865 if (req
->f
->op
.removexattr
)
866 req
->f
->op
.removexattr(req
, nodeid
, name
);
868 fuse_reply_err(req
, ENOSYS
);
871 static void convert_fuse_file_lock(const struct fuse_file_lock
*fl
,
874 memset(flock
, 0, sizeof(struct flock
));
875 flock
->l_type
= fl
->type
;
876 flock
->l_whence
= SEEK_SET
;
877 flock
->l_start
= fl
->start
;
878 if (fl
->end
== OFFSET_MAX
)
881 flock
->l_len
= fl
->end
- fl
->start
+ 1;
882 flock
->l_pid
= fl
->pid
;
885 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
887 const struct fuse_lk_in
*arg
= (const struct fuse_lk_in
*) inarg
;
888 struct fuse_file_info fi
;
891 memset(&fi
, 0, sizeof(fi
));
893 fi
.lock_owner
= arg
->owner
;
895 convert_fuse_file_lock(&arg
->lk
, &flock
);
896 if (req
->f
->op
.getlk
)
897 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
899 fuse_reply_err(req
, ENOSYS
);
902 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
903 const void *inarg
, int should_sleep
)
905 const struct fuse_lk_in
*arg
= (const struct fuse_lk_in
*) inarg
;
906 struct fuse_file_info fi
;
909 memset(&fi
, 0, sizeof(fi
));
911 fi
.lock_owner
= arg
->owner
;
913 convert_fuse_file_lock(&arg
->lk
, &flock
);
914 if (req
->f
->op
.setlk
)
915 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, should_sleep
);
917 fuse_reply_err(req
, ENOSYS
);
920 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
922 do_setlk_common(req
, nodeid
, inarg
, 0);
925 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
927 do_setlk_common(req
, nodeid
, inarg
, 1);
930 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
932 struct fuse_req
*curr
;
934 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
935 if (curr
->unique
== req
->u
.i
.unique
) {
937 pthread_mutex_unlock(&f
->lock
);
939 /* Ugh, ugly locking */
940 pthread_mutex_lock(&curr
->lock
);
941 pthread_mutex_lock(&f
->lock
);
942 curr
->interrupted
= 1;
943 pthread_mutex_unlock(&f
->lock
);
945 curr
->u
.ni
.func(curr
, curr
->u
.ni
.data
);
946 pthread_mutex_unlock(&curr
->lock
);
948 pthread_mutex_lock(&f
->lock
);
956 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
958 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
964 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
966 const struct fuse_interrupt_in
*arg
= (const struct fuse_interrupt_in
*) inarg
;
967 struct fuse_ll
*f
= req
->f
;
971 fprintf(stderr
, "INTERRUPT: %llu\n", (unsigned long long) arg
->unique
);
973 req
->u
.i
.unique
= arg
->unique
;
975 pthread_mutex_lock(&f
->lock
);
976 if (find_interrupted(f
, req
))
979 list_add_req(req
, &f
->interrupts
);
980 pthread_mutex_unlock(&f
->lock
);
983 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
985 struct fuse_req
*curr
;
987 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
; curr
= curr
->next
) {
988 if (curr
->u
.i
.unique
== req
->unique
) {
989 req
->interrupted
= 1;
995 curr
= f
->interrupts
.next
;
996 if (curr
!= &f
->interrupts
) {
1004 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1006 const struct fuse_bmap_in
*arg
= (const struct fuse_bmap_in
*) inarg
;
1008 if (req
->f
->op
.bmap
)
1009 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
1011 fuse_reply_err(req
, ENOSYS
);
1014 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1016 const struct fuse_init_in
*arg
= (const struct fuse_init_in
*) inarg
;
1017 struct fuse_init_out outarg
;
1018 struct fuse_ll
*f
= req
->f
;
1019 size_t bufsize
= fuse_chan_bufsize(req
->ch
);
1023 fprintf(stderr
, "INIT: %u.%u\n", arg
->major
, arg
->minor
);
1024 if (arg
->major
> 7 || (arg
->major
== 7 && arg
->minor
>= 6)) {
1025 fprintf(stderr
, "flags=0x%08x\n", arg
->flags
);
1026 fprintf(stderr
, "max_readahead=0x%08x\n", arg
->max_readahead
);
1029 f
->conn
.proto_major
= arg
->major
;
1030 f
->conn
.proto_minor
= arg
->minor
;
1032 if (arg
->major
< 7) {
1033 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
1034 arg
->major
, arg
->minor
);
1035 fuse_reply_err(req
, EPROTO
);
1039 if (arg
->major
> 7 || (arg
->major
== 7 && arg
->minor
>= 6)) {
1040 if (f
->conn
.async_read
)
1041 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
1042 if (arg
->max_readahead
< f
->conn
.max_readahead
)
1043 f
->conn
.max_readahead
= arg
->max_readahead
;
1045 if (arg
->flags
& FUSE_DONT_MASK
)
1046 f
->conn
.capable
|= FUSE_CAP_DONT_MASK
;
1048 if (arg
->flags
& FUSE_BIG_WRITES
)
1049 f
->conn
.capable
|= FUSE_CAP_BIG_WRITES
;
1051 f
->conn
.async_read
= 0;
1052 f
->conn
.max_readahead
= 0;
1055 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
1056 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
1058 bufsize
= FUSE_MIN_READ_BUFFER
;
1062 if (bufsize
< f
->conn
.max_write
)
1063 f
->conn
.max_write
= bufsize
;
1067 f
->op
.init(f
->userdata
, &f
->conn
);
1069 memset(&outarg
, 0, sizeof(outarg
));
1070 outarg
.major
= FUSE_KERNEL_VERSION
;
1072 * if POSIXACLS is not set, protocol 7.8 provides a good
1073 * compatibility with older kernel modules.
1074 * if POSIXACLS is set, we try to use protocol 7.12 supposed
1075 * to have the ability to process the umask conditionnally,
1076 * but, when using an older kernel module, we fallback to 7.8
1079 if (arg
->major
> 7 || (arg
->major
== 7 && arg
->minor
>= 12))
1080 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1082 outarg
.minor
= FUSE_KERNEL_MINOR_FALLBACK
;
1084 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
1086 if (f
->conn
.async_read
)
1087 outarg
.flags
|= FUSE_ASYNC_READ
;
1088 if (f
->op
.getlk
&& f
->op
.setlk
)
1089 outarg
.flags
|= FUSE_POSIX_LOCKS
;
1091 if (f
->conn
.want
& FUSE_CAP_DONT_MASK
)
1092 outarg
.flags
|= FUSE_DONT_MASK
;
1094 if (f
->conn
.want
& FUSE_CAP_BIG_WRITES
)
1095 outarg
.flags
|= FUSE_BIG_WRITES
;
1096 outarg
.max_readahead
= f
->conn
.max_readahead
;
1097 outarg
.max_write
= f
->conn
.max_write
;
1100 fprintf(stderr
, " INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
1101 fprintf(stderr
, " flags=0x%08x\n", outarg
.flags
);
1102 fprintf(stderr
, " max_readahead=0x%08x\n", outarg
.max_readahead
);
1103 fprintf(stderr
, " max_write=0x%08x\n", outarg
.max_write
);
1106 send_reply_ok(req
, &outarg
, arg
->minor
< 5 ? 8 : sizeof(outarg
));
1109 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1111 struct fuse_ll
*f
= req
->f
;
1118 f
->op
.destroy(f
->userdata
);
1120 send_reply_ok(req
, NULL
, 0);
1123 void *fuse_req_userdata(fuse_req_t req
)
1125 return req
->f
->userdata
;
1128 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
1133 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
1136 pthread_mutex_lock(&req
->lock
);
1137 req
->u
.ni
.func
= func
;
1138 req
->u
.ni
.data
= data
;
1139 if (req
->interrupted
&& func
)
1141 pthread_mutex_unlock(&req
->lock
);
1144 int fuse_req_interrupted(fuse_req_t req
)
1148 pthread_mutex_lock(&req
->f
->lock
);
1149 interrupted
= req
->interrupted
;
1150 pthread_mutex_unlock(&req
->f
->lock
);
1156 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
1159 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
1160 [FUSE_FORGET
] = { do_forget
, "FORGET" },
1161 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
1162 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
1163 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
1164 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
1165 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
1166 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
1167 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
1168 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
1169 [FUSE_RENAME
] = { do_rename
, "RENAME" },
1170 [FUSE_LINK
] = { do_link
, "LINK" },
1171 [FUSE_OPEN
] = { do_open
, "OPEN" },
1172 [FUSE_READ
] = { do_read
, "READ" },
1173 [FUSE_WRITE
] = { do_write
, "WRITE" },
1174 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
1175 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
1176 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
1177 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
1178 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
1179 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
1180 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
1181 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
1182 [FUSE_INIT
] = { do_init
, "INIT" },
1183 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
1184 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
1185 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
1186 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
1187 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
1188 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
1189 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
1190 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
1191 [FUSE_CREATE
] = { do_create
, "CREATE" },
1192 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
1193 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
1194 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
1197 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
1199 static const char *opname(enum fuse_opcode opcode
)
1201 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
1204 return fuse_ll_ops
[opcode
].name
;
1207 static void fuse_ll_process(void *data
, const char *buf
, size_t len
,
1208 struct fuse_chan
*ch
)
1210 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
1211 const struct fuse_in_header
*in
= (const struct fuse_in_header
*) buf
;
1212 const void *inarg
= buf
+ sizeof(struct fuse_in_header
);
1213 struct fuse_req
*req
;
1216 fprintf(stderr
, "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu\n",
1217 (unsigned long long) in
->unique
,
1218 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
1219 (unsigned long) in
->nodeid
, len
);
1221 req
= (struct fuse_req
*) calloc(1, sizeof(struct fuse_req
));
1223 fprintf(stderr
, "fuse: failed to allocate request\n");
1228 req
->unique
= in
->unique
;
1229 req
->ctx
.uid
= in
->uid
;
1230 req
->ctx
.gid
= in
->gid
;
1231 req
->ctx
.pid
= in
->pid
;
1235 fuse_mutex_init(&req
->lock
);
1237 if (!f
->got_init
&& in
->opcode
!= FUSE_INIT
)
1238 fuse_reply_err(req
, EIO
);
1239 else if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
1240 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
1241 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
1242 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
1243 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
) {
1244 fuse_reply_err(req
, EACCES
);
1245 } else if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
1246 fuse_reply_err(req
, ENOSYS
);
1248 if (in
->opcode
!= FUSE_INTERRUPT
) {
1249 struct fuse_req
*intr
;
1250 pthread_mutex_lock(&f
->lock
);
1251 intr
= check_interrupt(f
, req
);
1252 list_add_req(req
, &f
->list
);
1253 pthread_mutex_unlock(&f
->lock
);
1255 fuse_reply_err(intr
, EAGAIN
);
1257 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
1266 static struct fuse_opt fuse_ll_opts
[] = {
1267 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
1268 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
1269 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
1270 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
1271 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
1272 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
1273 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
1274 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
1275 FUSE_OPT_KEY("-h", KEY_HELP
),
1276 FUSE_OPT_KEY("--help", KEY_HELP
),
1277 FUSE_OPT_KEY("-V", KEY_VERSION
),
1278 FUSE_OPT_KEY("--version", KEY_VERSION
),
1282 static void fuse_ll_version(void)
1284 fprintf(stderr
, "using FUSE kernel interface version %i.%i\n",
1285 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
1288 static void fuse_ll_help(void)
1291 " -o max_write=N set maximum size of write requests\n"
1292 " -o max_readahead=N set maximum readahead\n"
1293 " -o async_read perform reads asynchronously (default)\n"
1294 " -o sync_read perform reads synchronously\n");
1297 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
1298 struct fuse_args
*outargs
)
1300 (void) data
; (void) outargs
;
1312 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
1320 int fuse_lowlevel_is_lib_option(const char *opt
)
1322 return fuse_opt_match(fuse_ll_opts
, opt
);
1325 #endif /* __SOLARIS__ */
1327 static void fuse_ll_destroy(void *data
)
1329 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
1331 if (f
->got_init
&& !f
->got_destroy
) {
1333 f
->op
.destroy(f
->userdata
);
1336 pthread_mutex_destroy(&f
->lock
);
1340 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
1341 const struct fuse_lowlevel_ops
*op
,
1342 size_t op_size
, void *userdata
)
1345 struct fuse_session
*se
;
1346 struct fuse_session_ops sop
= {
1347 .process
= fuse_ll_process
,
1348 .destroy
= fuse_ll_destroy
,
1351 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
1352 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
1353 op_size
= sizeof(struct fuse_lowlevel_ops
);
1356 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
1358 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
1362 f
->conn
.async_read
= 1;
1363 f
->conn
.max_write
= UINT_MAX
;
1364 f
->conn
.max_readahead
= UINT_MAX
;
1365 list_init_req(&f
->list
);
1366 list_init_req(&f
->interrupts
);
1367 fuse_mutex_init(&f
->lock
);
1369 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
1372 memcpy(&f
->op
, op
, op_size
);
1373 f
->owner
= getuid();
1374 f
->userdata
= userdata
;
1376 se
= fuse_session_new(&sop
, f
);