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 LGPL.
6 See the file COPYING.LIB
9 #include "fuse_lowlevel.h"
10 #include "fuse_kernel.h"
13 #include "fuse_misc.h"
14 #include "fuse_common_compat.h"
15 #include "fuse_lowlevel_compat.h"
25 #define PARAM(inarg) (((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
);
83 static void convert_attr(const struct fuse_setattr_in
*attr
, struct stat
*stbuf
)
85 stbuf
->st_mode
= attr
->mode
;
86 stbuf
->st_uid
= attr
->uid
;
87 stbuf
->st_gid
= attr
->gid
;
88 stbuf
->st_size
= attr
->size
;
89 stbuf
->st_atime
= attr
->atime
;
90 stbuf
->st_mtime
= attr
->mtime
;
91 ST_ATIM_NSEC_SET(stbuf
, attr
->atimensec
);
92 ST_MTIM_NSEC_SET(stbuf
, attr
->mtimensec
);
95 static size_t iov_length(const struct iovec
*iov
, size_t count
)
100 for (seg
= 0; seg
< count
; seg
++)
101 ret
+= iov
[seg
].iov_len
;
105 static void list_init_req(struct fuse_req
*req
)
111 static void list_del_req(struct fuse_req
*req
)
113 struct fuse_req
*prev
= req
->prev
;
114 struct fuse_req
*next
= req
->next
;
119 static void list_add_req(struct fuse_req
*req
, struct fuse_req
*next
)
121 struct fuse_req
*prev
= next
->prev
;
128 static void destroy_req(fuse_req_t req
)
130 pthread_mutex_destroy(&req
->lock
);
134 static void free_req(fuse_req_t req
)
137 struct fuse_ll
*f
= req
->f
;
139 pthread_mutex_lock(&req
->lock
);
140 req
->u
.ni
.func
= NULL
;
141 req
->u
.ni
.data
= NULL
;
142 pthread_mutex_unlock(&req
->lock
);
144 pthread_mutex_lock(&f
->lock
);
147 pthread_mutex_unlock(&f
->lock
);
152 static int send_reply(fuse_req_t req
, int error
, const void *arg
,
155 struct fuse_out_header out
;
160 if (error
<= -1000 || error
> 0) {
161 fprintf(stderr
, "fuse: bad error value: %i\n", error
);
165 out
.unique
= req
->unique
;
168 iov
[0].iov_base
= &out
;
169 iov
[0].iov_len
= sizeof(struct fuse_out_header
);
170 if (argsize
&& !error
) {
172 iov
[1].iov_base
= (void *) arg
;
173 iov
[1].iov_len
= argsize
;
175 out
.len
= iov_length(iov
, count
);
178 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
179 out
.unique
, out
.error
, strerror(-out
.error
), out
.len
);
182 res
= fuse_chan_send(req
->ch
, iov
, count
);
188 size_t fuse_dirent_size(size_t namelen
)
190 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET
+ namelen
);
193 char *fuse_add_dirent(char *buf
, const char *name
, const struct stat
*stbuf
,
196 unsigned namelen
= strlen(name
);
197 unsigned entlen
= FUSE_NAME_OFFSET
+ namelen
;
198 unsigned entsize
= fuse_dirent_size(namelen
);
199 unsigned padlen
= entsize
- entlen
;
200 struct fuse_dirent
*dirent
= (struct fuse_dirent
*) buf
;
202 dirent
->ino
= stbuf
->st_ino
;
204 dirent
->namelen
= namelen
;
205 dirent
->type
= (stbuf
->st_mode
& 0170000) >> 12;
206 strncpy(dirent
->name
, name
, namelen
);
208 memset(buf
+ entlen
, 0, padlen
);
210 return buf
+ entsize
;
213 size_t fuse_add_direntry(fuse_req_t req
, char *buf
, size_t bufsize
,
214 const char *name
, const struct stat
*stbuf
, off_t off
)
219 entsize
= fuse_dirent_size(strlen(name
));
220 if (entsize
<= bufsize
&& buf
)
221 fuse_add_dirent(buf
, name
, stbuf
, off
);
225 static void convert_statfs(const struct statvfs
*stbuf
,
226 struct fuse_kstatfs
*kstatfs
)
228 kstatfs
->bsize
= stbuf
->f_bsize
;
229 kstatfs
->frsize
= stbuf
->f_frsize
;
230 kstatfs
->blocks
= stbuf
->f_blocks
;
231 kstatfs
->bfree
= stbuf
->f_bfree
;
232 kstatfs
->bavail
= stbuf
->f_bavail
;
233 kstatfs
->files
= stbuf
->f_files
;
234 kstatfs
->ffree
= stbuf
->f_ffree
;
235 kstatfs
->namelen
= stbuf
->f_namemax
;
238 static int send_reply_ok(fuse_req_t req
, const void *arg
, size_t argsize
)
240 return send_reply(req
, 0, arg
, argsize
);
243 int fuse_reply_err(fuse_req_t req
, int err
)
245 return send_reply(req
, -err
, NULL
, 0);
248 void fuse_reply_none(fuse_req_t req
)
250 fuse_chan_send(req
->ch
, NULL
, 0);
254 static unsigned long calc_timeout_sec(double t
)
256 if (t
> (double) ULONG_MAX
)
261 return (unsigned long) t
;
264 static unsigned int calc_timeout_nsec(double t
)
266 double f
= t
- (double) calc_timeout_sec(t
);
269 else if (f
>= 0.999999999)
272 return (unsigned int) (f
* 1.0e9
);
275 static void fill_entry(struct fuse_entry_out
*arg
,
276 const struct fuse_entry_param
*e
)
278 arg
->nodeid
= e
->ino
;
279 arg
->generation
= e
->generation
;
280 arg
->entry_valid
= calc_timeout_sec(e
->entry_timeout
);
281 arg
->entry_valid_nsec
= calc_timeout_nsec(e
->entry_timeout
);
282 arg
->attr_valid
= calc_timeout_sec(e
->attr_timeout
);
283 arg
->attr_valid_nsec
= calc_timeout_nsec(e
->attr_timeout
);
284 convert_stat(&e
->attr
, &arg
->attr
);
287 static void fill_open(struct fuse_open_out
*arg
,
288 const struct fuse_file_info
*f
)
292 arg
->open_flags
|= FOPEN_DIRECT_IO
;
294 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
297 int fuse_reply_entry(fuse_req_t req
, const struct fuse_entry_param
*e
)
299 struct fuse_entry_out arg
;
301 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
303 if (!e
->ino
&& req
->f
->conn
.proto_minor
< 4)
304 return fuse_reply_err(req
, ENOENT
);
306 memset(&arg
, 0, sizeof(arg
));
308 return send_reply_ok(req
, &arg
, sizeof(arg
));
311 int fuse_reply_create(fuse_req_t req
, const struct fuse_entry_param
*e
,
312 const struct fuse_file_info
*f
)
315 struct fuse_entry_out e
;
316 struct fuse_open_out o
;
319 memset(&arg
, 0, sizeof(arg
));
320 fill_entry(&arg
.e
, e
);
321 fill_open(&arg
.o
, f
);
322 return send_reply_ok(req
, &arg
, sizeof(arg
));
325 int fuse_reply_attr(fuse_req_t req
, const struct stat
*attr
,
328 struct fuse_attr_out arg
;
330 memset(&arg
, 0, sizeof(arg
));
331 arg
.attr_valid
= calc_timeout_sec(attr_timeout
);
332 arg
.attr_valid_nsec
= calc_timeout_nsec(attr_timeout
);
333 convert_stat(attr
, &arg
.attr
);
335 return send_reply_ok(req
, &arg
, sizeof(arg
));
338 int fuse_reply_readlink(fuse_req_t req
, const char *linkname
)
340 return send_reply_ok(req
, linkname
, strlen(linkname
));
343 int fuse_reply_open(fuse_req_t req
, const struct fuse_file_info
*f
)
345 struct fuse_open_out arg
;
347 memset(&arg
, 0, sizeof(arg
));
349 return send_reply_ok(req
, &arg
, sizeof(arg
));
352 int fuse_reply_write(fuse_req_t req
, size_t count
)
354 struct fuse_write_out arg
;
356 memset(&arg
, 0, sizeof(arg
));
359 return send_reply_ok(req
, &arg
, sizeof(arg
));
362 int fuse_reply_buf(fuse_req_t req
, const char *buf
, size_t size
)
364 return send_reply_ok(req
, buf
, size
);
367 int fuse_reply_statfs(fuse_req_t req
, const struct statvfs
*stbuf
)
369 struct fuse_statfs_out arg
;
370 size_t size
= req
->f
->conn
.proto_minor
< 4 ? FUSE_COMPAT_STATFS_SIZE
: sizeof(arg
);
372 memset(&arg
, 0, sizeof(arg
));
373 convert_statfs(stbuf
, &arg
.st
);
375 return send_reply_ok(req
, &arg
, size
);
378 int fuse_reply_xattr(fuse_req_t req
, size_t count
)
380 struct fuse_getxattr_out arg
;
382 memset(&arg
, 0, sizeof(arg
));
385 return send_reply_ok(req
, &arg
, sizeof(arg
));
388 int fuse_reply_lock(fuse_req_t req
, struct flock
*lock
)
390 struct fuse_lk_out arg
;
392 memset(&arg
, 0, sizeof(arg
));
393 arg
.lk
.type
= lock
->l_type
;
394 if (lock
->l_type
!= F_UNLCK
) {
395 arg
.lk
.start
= lock
->l_start
;
396 if (lock
->l_len
== 0)
397 arg
.lk
.end
= OFFSET_MAX
;
399 arg
.lk
.end
= lock
->l_start
+ lock
->l_len
- 1;
401 arg
.lk
.pid
= lock
->l_pid
;
402 return send_reply_ok(req
, &arg
, sizeof(arg
));
405 int fuse_reply_bmap(fuse_req_t req
, uint64_t idx
)
407 struct fuse_bmap_out arg
;
409 memset(&arg
, 0, sizeof(arg
));
412 return send_reply_ok(req
, &arg
, sizeof(arg
));
415 static void do_lookup(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
417 char *name
= (char *) inarg
;
419 if (req
->f
->op
.lookup
)
420 req
->f
->op
.lookup(req
, nodeid
, name
);
422 fuse_reply_err(req
, ENOSYS
);
425 static void do_forget(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
427 struct fuse_forget_in
*arg
= (struct fuse_forget_in
*) inarg
;
429 if (req
->f
->op
.forget
)
430 req
->f
->op
.forget(req
, nodeid
, arg
->nlookup
);
433 static void do_getattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
437 if (req
->f
->op
.getattr
)
438 req
->f
->op
.getattr(req
, nodeid
, NULL
);
440 fuse_reply_err(req
, ENOSYS
);
443 static void do_setattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
445 struct fuse_setattr_in
*arg
= (struct fuse_setattr_in
*) inarg
;
447 if (req
->f
->op
.setattr
) {
448 struct fuse_file_info
*fi
= NULL
;
449 struct fuse_file_info fi_store
;
451 memset(&stbuf
, 0, sizeof(stbuf
));
452 convert_attr(arg
, &stbuf
);
453 if (arg
->valid
& FATTR_FH
) {
454 arg
->valid
&= ~FATTR_FH
;
455 memset(&fi_store
, 0, sizeof(fi_store
));
460 req
->f
->op
.setattr(req
, nodeid
, &stbuf
, arg
->valid
, fi
);
462 fuse_reply_err(req
, ENOSYS
);
465 static void do_access(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
467 struct fuse_access_in
*arg
= (struct fuse_access_in
*) inarg
;
469 if (req
->f
->op
.access
)
470 req
->f
->op
.access(req
, nodeid
, arg
->mask
);
472 fuse_reply_err(req
, ENOSYS
);
475 static void do_readlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
479 if (req
->f
->op
.readlink
)
480 req
->f
->op
.readlink(req
, nodeid
);
482 fuse_reply_err(req
, ENOSYS
);
485 static void do_mknod(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
487 struct fuse_mknod_in
*arg
= (struct fuse_mknod_in
*) inarg
;
489 if (req
->f
->op
.mknod
)
490 req
->f
->op
.mknod(req
, nodeid
, PARAM(arg
), arg
->mode
, arg
->rdev
);
492 fuse_reply_err(req
, ENOSYS
);
495 static void do_mkdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
497 struct fuse_mkdir_in
*arg
= (struct fuse_mkdir_in
*) inarg
;
499 if (req
->f
->op
.mkdir
)
500 req
->f
->op
.mkdir(req
, nodeid
, PARAM(arg
), arg
->mode
);
502 fuse_reply_err(req
, ENOSYS
);
505 static void do_unlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
507 char *name
= (char *) inarg
;
509 if (req
->f
->op
.unlink
)
510 req
->f
->op
.unlink(req
, nodeid
, name
);
512 fuse_reply_err(req
, ENOSYS
);
515 static void do_rmdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
517 char *name
= (char *) inarg
;
519 if (req
->f
->op
.rmdir
)
520 req
->f
->op
.rmdir(req
, nodeid
, name
);
522 fuse_reply_err(req
, ENOSYS
);
525 static void do_symlink(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
527 char *name
= (char *) inarg
;
528 char *linkname
= ((char *) inarg
) + strlen((char *) inarg
) + 1;
530 if (req
->f
->op
.symlink
)
531 req
->f
->op
.symlink(req
, linkname
, nodeid
, name
);
533 fuse_reply_err(req
, ENOSYS
);
536 static void do_rename(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
538 struct fuse_rename_in
*arg
= (struct fuse_rename_in
*) inarg
;
539 char *oldname
= PARAM(arg
);
540 char *newname
= oldname
+ strlen(oldname
) + 1;
542 if (req
->f
->op
.rename
)
543 req
->f
->op
.rename(req
, nodeid
, oldname
, arg
->newdir
, newname
);
545 fuse_reply_err(req
, ENOSYS
);
548 static void do_link(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
550 struct fuse_link_in
*arg
= (struct fuse_link_in
*) inarg
;
553 req
->f
->op
.link(req
, arg
->oldnodeid
, nodeid
, PARAM(arg
));
555 fuse_reply_err(req
, ENOSYS
);
558 static void do_create(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
560 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
562 if (req
->f
->op
.create
) {
563 struct fuse_file_info fi
;
565 memset(&fi
, 0, sizeof(fi
));
566 fi
.flags
= arg
->flags
;
568 req
->f
->op
.create(req
, nodeid
, PARAM(arg
), arg
->mode
, &fi
);
570 fuse_reply_err(req
, ENOSYS
);
573 static void do_open(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
575 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
576 struct fuse_file_info fi
;
578 memset(&fi
, 0, sizeof(fi
));
579 fi
.flags
= arg
->flags
;
582 req
->f
->op
.open(req
, nodeid
, &fi
);
584 fuse_reply_open(req
, &fi
);
587 static void do_read(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
589 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
591 if (req
->f
->op
.read
) {
592 struct fuse_file_info fi
;
594 memset(&fi
, 0, sizeof(fi
));
597 req
->f
->op
.read(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
599 fuse_reply_err(req
, ENOSYS
);
602 static void do_write(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
604 struct fuse_write_in
*arg
= (struct fuse_write_in
*) inarg
;
605 struct fuse_file_info fi
;
607 memset(&fi
, 0, sizeof(fi
));
610 fi
.writepage
= arg
->write_flags
& 1;
612 if (req
->f
->op
.write
)
613 req
->f
->op
.write(req
, nodeid
, PARAM(arg
), arg
->size
, arg
->offset
, &fi
);
615 fuse_reply_err(req
, ENOSYS
);
618 static void do_flush(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
620 struct fuse_flush_in
*arg
= (struct fuse_flush_in
*) inarg
;
621 struct fuse_file_info fi
;
623 memset(&fi
, 0, sizeof(fi
));
627 if (req
->f
->conn
.proto_minor
>= 7)
628 fi
.lock_owner
= arg
->lock_owner
;
630 if (req
->f
->op
.flush
)
631 req
->f
->op
.flush(req
, nodeid
, &fi
);
633 fuse_reply_err(req
, ENOSYS
);
636 static void do_release(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
638 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
639 struct fuse_file_info fi
;
641 memset(&fi
, 0, sizeof(fi
));
642 fi
.flags
= arg
->flags
;
645 if (req
->f
->conn
.proto_minor
>= 8) {
646 fi
.flush
= (arg
->release_flags
& FUSE_RELEASE_FLUSH
) ? 1 : 0;
647 fi
.lock_owner
= arg
->lock_owner
;
650 if (req
->f
->op
.release
)
651 req
->f
->op
.release(req
, nodeid
, &fi
);
653 fuse_reply_err(req
, 0);
656 static void do_fsync(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
658 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
659 struct fuse_file_info fi
;
661 memset(&fi
, 0, sizeof(fi
));
665 if (req
->f
->op
.fsync
)
666 req
->f
->op
.fsync(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
668 fuse_reply_err(req
, ENOSYS
);
671 static void do_opendir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
673 struct fuse_open_in
*arg
= (struct fuse_open_in
*) inarg
;
674 struct fuse_file_info fi
;
676 memset(&fi
, 0, sizeof(fi
));
677 fi
.flags
= arg
->flags
;
679 if (req
->f
->op
.opendir
)
680 req
->f
->op
.opendir(req
, nodeid
, &fi
);
682 fuse_reply_open(req
, &fi
);
685 static void do_readdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
687 struct fuse_read_in
*arg
= (struct fuse_read_in
*) inarg
;
688 struct fuse_file_info fi
;
690 memset(&fi
, 0, sizeof(fi
));
694 if (req
->f
->op
.readdir
)
695 req
->f
->op
.readdir(req
, nodeid
, arg
->size
, arg
->offset
, &fi
);
697 fuse_reply_err(req
, ENOSYS
);
700 static void do_releasedir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
702 struct fuse_release_in
*arg
= (struct fuse_release_in
*) inarg
;
703 struct fuse_file_info fi
;
705 memset(&fi
, 0, sizeof(fi
));
706 fi
.flags
= arg
->flags
;
710 if (req
->f
->op
.releasedir
)
711 req
->f
->op
.releasedir(req
, nodeid
, &fi
);
713 fuse_reply_err(req
, 0);
716 static void do_fsyncdir(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
718 struct fuse_fsync_in
*arg
= (struct fuse_fsync_in
*) inarg
;
719 struct fuse_file_info fi
;
721 memset(&fi
, 0, sizeof(fi
));
725 if (req
->f
->op
.fsyncdir
)
726 req
->f
->op
.fsyncdir(req
, nodeid
, arg
->fsync_flags
& 1, &fi
);
728 fuse_reply_err(req
, ENOSYS
);
731 static void do_statfs(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
736 if (req
->f
->op
.statfs
)
737 req
->f
->op
.statfs(req
, nodeid
);
739 struct statvfs buf
= {
743 fuse_reply_statfs(req
, &buf
);
747 static void do_setxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
749 struct fuse_setxattr_in
*arg
= (struct fuse_setxattr_in
*) inarg
;
750 char *name
= PARAM(arg
);
751 char *value
= name
+ strlen(name
) + 1;
753 if (req
->f
->op
.setxattr
)
754 req
->f
->op
.setxattr(req
, nodeid
, name
, value
, arg
->size
, arg
->flags
);
756 fuse_reply_err(req
, ENOSYS
);
759 static void do_getxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
761 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
763 if (req
->f
->op
.getxattr
)
764 req
->f
->op
.getxattr(req
, nodeid
, PARAM(arg
), arg
->size
);
766 fuse_reply_err(req
, ENOSYS
);
769 static void do_listxattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
771 struct fuse_getxattr_in
*arg
= (struct fuse_getxattr_in
*) inarg
;
773 if (req
->f
->op
.listxattr
)
774 req
->f
->op
.listxattr(req
, nodeid
, arg
->size
);
776 fuse_reply_err(req
, ENOSYS
);
779 static void do_removexattr(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
781 char *name
= (char *) inarg
;
783 if (req
->f
->op
.removexattr
)
784 req
->f
->op
.removexattr(req
, nodeid
, name
);
786 fuse_reply_err(req
, ENOSYS
);
789 static void convert_fuse_file_lock(struct fuse_file_lock
*fl
,
792 memset(flock
, 0, sizeof(struct flock
));
793 flock
->l_type
= fl
->type
;
794 flock
->l_whence
= SEEK_SET
;
795 flock
->l_start
= fl
->start
;
796 if (fl
->end
== OFFSET_MAX
)
799 flock
->l_len
= fl
->end
- fl
->start
+ 1;
800 flock
->l_pid
= fl
->pid
;
803 static void do_getlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
805 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
806 struct fuse_file_info fi
;
809 memset(&fi
, 0, sizeof(fi
));
811 fi
.lock_owner
= arg
->owner
;
813 convert_fuse_file_lock(&arg
->lk
, &flock
);
814 if (req
->f
->op
.getlk
)
815 req
->f
->op
.getlk(req
, nodeid
, &fi
, &flock
);
817 fuse_reply_err(req
, ENOSYS
);
820 static void do_setlk_common(fuse_req_t req
, fuse_ino_t nodeid
,
821 const void *inarg
, int sleep
)
823 struct fuse_lk_in
*arg
= (struct fuse_lk_in
*) inarg
;
824 struct fuse_file_info fi
;
827 memset(&fi
, 0, sizeof(fi
));
829 fi
.lock_owner
= arg
->owner
;
831 convert_fuse_file_lock(&arg
->lk
, &flock
);
832 if (req
->f
->op
.setlk
)
833 req
->f
->op
.setlk(req
, nodeid
, &fi
, &flock
, sleep
);
835 fuse_reply_err(req
, ENOSYS
);
838 static void do_setlk(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
840 do_setlk_common(req
, nodeid
, inarg
, 0);
843 static void do_setlkw(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
845 do_setlk_common(req
, nodeid
, inarg
, 1);
848 static int find_interrupted(struct fuse_ll
*f
, struct fuse_req
*req
)
850 struct fuse_req
*curr
;
852 for (curr
= f
->list
.next
; curr
!= &f
->list
; curr
= curr
->next
) {
853 if (curr
->unique
== req
->u
.i
.unique
) {
855 pthread_mutex_unlock(&f
->lock
);
857 /* Ugh, ugly locking */
858 pthread_mutex_lock(&curr
->lock
);
859 pthread_mutex_lock(&f
->lock
);
860 curr
->interrupted
= 1;
861 pthread_mutex_unlock(&f
->lock
);
863 curr
->u
.ni
.func(curr
, curr
->u
.ni
.data
);
864 pthread_mutex_unlock(&curr
->lock
);
866 pthread_mutex_lock(&f
->lock
);
874 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
;
876 if (curr
->u
.i
.unique
== req
->u
.i
.unique
)
882 static void do_interrupt(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
884 struct fuse_interrupt_in
*arg
= (struct fuse_interrupt_in
*) inarg
;
885 struct fuse_ll
*f
= req
->f
;
889 printf("INTERRUPT: %llu\n", (unsigned long long) arg
->unique
);
893 req
->u
.i
.unique
= arg
->unique
;
895 pthread_mutex_lock(&f
->lock
);
896 if (find_interrupted(f
, req
))
899 list_add_req(req
, &f
->interrupts
);
900 pthread_mutex_unlock(&f
->lock
);
903 static struct fuse_req
*check_interrupt(struct fuse_ll
*f
, struct fuse_req
*req
)
905 struct fuse_req
*curr
;
907 for (curr
= f
->interrupts
.next
; curr
!= &f
->interrupts
; curr
= curr
->next
) {
908 if (curr
->u
.i
.unique
== req
->unique
) {
909 req
->interrupted
= 1;
915 curr
= f
->interrupts
.next
;
916 if (curr
!= &f
->interrupts
) {
924 static void do_bmap(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
926 struct fuse_bmap_in
*arg
= (struct fuse_bmap_in
*) inarg
;
929 req
->f
->op
.bmap(req
, nodeid
, arg
->blocksize
, arg
->block
);
931 fuse_reply_err(req
, ENOSYS
);
934 static void do_init(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
936 struct fuse_init_in
*arg
= (struct fuse_init_in
*) inarg
;
937 struct fuse_init_out outarg
;
938 struct fuse_ll
*f
= req
->f
;
939 size_t bufsize
= fuse_chan_bufsize(req
->ch
);
943 printf("INIT: %u.%u\n", arg
->major
, arg
->minor
);
944 if (arg
->major
> 7 || (arg
->major
== 7 && arg
->minor
>= 6)) {
945 printf("flags=0x%08x\n", arg
->flags
);
946 printf("max_readahead=0x%08x\n", arg
->max_readahead
);
950 f
->conn
.proto_major
= arg
->major
;
951 f
->conn
.proto_minor
= arg
->minor
;
953 if (arg
->major
< 7) {
954 fprintf(stderr
, "fuse: unsupported protocol version: %u.%u\n",
955 arg
->major
, arg
->minor
);
956 fuse_reply_err(req
, EPROTO
);
960 if (arg
->major
> 7 || (arg
->major
== 7 && arg
->minor
>= 6)) {
961 if (f
->conn
.async_read
)
962 f
->conn
.async_read
= arg
->flags
& FUSE_ASYNC_READ
;
963 if (arg
->max_readahead
< f
->conn
.max_readahead
)
964 f
->conn
.max_readahead
= arg
->max_readahead
;
966 f
->conn
.async_read
= 0;
967 f
->conn
.max_readahead
= 0;
970 if (bufsize
< FUSE_MIN_READ_BUFFER
) {
971 fprintf(stderr
, "fuse: warning: buffer size too small: %zu\n",
973 bufsize
= FUSE_MIN_READ_BUFFER
;
977 if (bufsize
< f
->conn
.max_write
)
978 f
->conn
.max_write
= bufsize
;
982 f
->op
.init(f
->userdata
, &f
->conn
);
984 memset(&outarg
, 0, sizeof(outarg
));
985 outarg
.major
= FUSE_KERNEL_VERSION
;
986 outarg
.minor
= FUSE_KERNEL_MINOR_VERSION
;
987 if (f
->conn
.async_read
)
988 outarg
.flags
|= FUSE_ASYNC_READ
;
989 if (f
->op
.getlk
&& f
->op
.setlk
)
990 outarg
.flags
|= FUSE_POSIX_LOCKS
;
991 outarg
.max_readahead
= f
->conn
.max_readahead
;
992 outarg
.max_write
= f
->conn
.max_write
;
995 printf(" INIT: %u.%u\n", outarg
.major
, outarg
.minor
);
996 printf(" flags=0x%08x\n", outarg
.flags
);
997 printf(" max_readahead=0x%08x\n", outarg
.max_readahead
);
998 printf(" max_write=0x%08x\n", outarg
.max_write
);
1002 send_reply_ok(req
, &outarg
, arg
->minor
< 5 ? 8 : sizeof(outarg
));
1005 static void do_destroy(fuse_req_t req
, fuse_ino_t nodeid
, const void *inarg
)
1007 struct fuse_ll
*f
= req
->f
;
1014 f
->op
.destroy(f
->userdata
);
1016 send_reply_ok(req
, NULL
, 0);
1019 void *fuse_req_userdata(fuse_req_t req
)
1021 return req
->f
->userdata
;
1024 const struct fuse_ctx
*fuse_req_ctx(fuse_req_t req
)
1029 void fuse_req_interrupt_func(fuse_req_t req
, fuse_interrupt_func_t func
,
1032 pthread_mutex_lock(&req
->lock
);
1033 req
->u
.ni
.func
= func
;
1034 req
->u
.ni
.data
= data
;
1035 if (req
->interrupted
&& func
)
1037 pthread_mutex_unlock(&req
->lock
);
1040 int fuse_req_interrupted(fuse_req_t req
)
1044 pthread_mutex_lock(&req
->f
->lock
);
1045 interrupted
= req
->interrupted
;
1046 pthread_mutex_unlock(&req
->f
->lock
);
1052 void (*func
)(fuse_req_t
, fuse_ino_t
, const void *);
1055 [FUSE_LOOKUP
] = { do_lookup
, "LOOKUP" },
1056 [FUSE_FORGET
] = { do_forget
, "FORGET" },
1057 [FUSE_GETATTR
] = { do_getattr
, "GETATTR" },
1058 [FUSE_SETATTR
] = { do_setattr
, "SETATTR" },
1059 [FUSE_READLINK
] = { do_readlink
, "READLINK" },
1060 [FUSE_SYMLINK
] = { do_symlink
, "SYMLINK" },
1061 [FUSE_MKNOD
] = { do_mknod
, "MKNOD" },
1062 [FUSE_MKDIR
] = { do_mkdir
, "MKDIR" },
1063 [FUSE_UNLINK
] = { do_unlink
, "UNLINK" },
1064 [FUSE_RMDIR
] = { do_rmdir
, "RMDIR" },
1065 [FUSE_RENAME
] = { do_rename
, "RENAME" },
1066 [FUSE_LINK
] = { do_link
, "LINK" },
1067 [FUSE_OPEN
] = { do_open
, "OPEN" },
1068 [FUSE_READ
] = { do_read
, "READ" },
1069 [FUSE_WRITE
] = { do_write
, "WRITE" },
1070 [FUSE_STATFS
] = { do_statfs
, "STATFS" },
1071 [FUSE_RELEASE
] = { do_release
, "RELEASE" },
1072 [FUSE_FSYNC
] = { do_fsync
, "FSYNC" },
1073 [FUSE_SETXATTR
] = { do_setxattr
, "SETXATTR" },
1074 [FUSE_GETXATTR
] = { do_getxattr
, "GETXATTR" },
1075 [FUSE_LISTXATTR
] = { do_listxattr
, "LISTXATTR" },
1076 [FUSE_REMOVEXATTR
] = { do_removexattr
, "REMOVEXATTR" },
1077 [FUSE_FLUSH
] = { do_flush
, "FLUSH" },
1078 [FUSE_INIT
] = { do_init
, "INIT" },
1079 [FUSE_OPENDIR
] = { do_opendir
, "OPENDIR" },
1080 [FUSE_READDIR
] = { do_readdir
, "READDIR" },
1081 [FUSE_RELEASEDIR
] = { do_releasedir
, "RELEASEDIR" },
1082 [FUSE_FSYNCDIR
] = { do_fsyncdir
, "FSYNCDIR" },
1083 [FUSE_GETLK
] = { do_getlk
, "GETLK" },
1084 [FUSE_SETLK
] = { do_setlk
, "SETLK" },
1085 [FUSE_SETLKW
] = { do_setlkw
, "SETLKW" },
1086 [FUSE_ACCESS
] = { do_access
, "ACCESS" },
1087 [FUSE_CREATE
] = { do_create
, "CREATE" },
1088 [FUSE_INTERRUPT
] = { do_interrupt
, "INTERRUPT" },
1089 [FUSE_BMAP
] = { do_bmap
, "BMAP" },
1090 [FUSE_DESTROY
] = { do_destroy
, "DESTROY" },
1093 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
1095 static const char *opname(enum fuse_opcode opcode
)
1097 if (opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[opcode
].name
)
1100 return fuse_ll_ops
[opcode
].name
;
1103 static void fuse_ll_process(void *data
, const char *buf
, size_t len
,
1104 struct fuse_chan
*ch
)
1106 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
1107 struct fuse_in_header
*in
= (struct fuse_in_header
*) buf
;
1108 const void *inarg
= buf
+ sizeof(struct fuse_in_header
);
1109 struct fuse_req
*req
;
1112 printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu\n",
1113 (unsigned long long) in
->unique
,
1114 opname((enum fuse_opcode
) in
->opcode
), in
->opcode
,
1115 (unsigned long) in
->nodeid
, len
);
1119 req
= (struct fuse_req
*) calloc(1, sizeof(struct fuse_req
));
1121 fprintf(stderr
, "fuse: failed to allocate request\n");
1126 req
->unique
= in
->unique
;
1127 req
->ctx
.uid
= in
->uid
;
1128 req
->ctx
.gid
= in
->gid
;
1129 req
->ctx
.pid
= in
->pid
;
1133 fuse_mutex_init(&req
->lock
);
1135 if (!f
->got_init
&& in
->opcode
!= FUSE_INIT
)
1136 fuse_reply_err(req
, EIO
);
1137 else if (f
->allow_root
&& in
->uid
!= f
->owner
&& in
->uid
!= 0 &&
1138 in
->opcode
!= FUSE_INIT
&& in
->opcode
!= FUSE_READ
&&
1139 in
->opcode
!= FUSE_WRITE
&& in
->opcode
!= FUSE_FSYNC
&&
1140 in
->opcode
!= FUSE_RELEASE
&& in
->opcode
!= FUSE_READDIR
&&
1141 in
->opcode
!= FUSE_FSYNCDIR
&& in
->opcode
!= FUSE_RELEASEDIR
) {
1142 fuse_reply_err(req
, EACCES
);
1143 } else if (in
->opcode
>= FUSE_MAXOP
|| !fuse_ll_ops
[in
->opcode
].func
)
1144 fuse_reply_err(req
, ENOSYS
);
1146 if (in
->opcode
!= FUSE_INTERRUPT
) {
1147 struct fuse_req
*intr
;
1148 pthread_mutex_lock(&f
->lock
);
1149 intr
= check_interrupt(f
, req
);
1150 list_add_req(req
, &f
->list
);
1151 pthread_mutex_unlock(&f
->lock
);
1153 fuse_reply_err(intr
, EAGAIN
);
1155 fuse_ll_ops
[in
->opcode
].func(req
, in
->nodeid
, inarg
);
1164 static struct fuse_opt fuse_ll_opts
[] = {
1165 { "debug", offsetof(struct fuse_ll
, debug
), 1 },
1166 { "-d", offsetof(struct fuse_ll
, debug
), 1 },
1167 { "allow_root", offsetof(struct fuse_ll
, allow_root
), 1 },
1168 { "max_write=%u", offsetof(struct fuse_ll
, conn
.max_write
), 0 },
1169 { "max_readahead=%u", offsetof(struct fuse_ll
, conn
.max_readahead
), 0 },
1170 { "async_read", offsetof(struct fuse_ll
, conn
.async_read
), 1 },
1171 { "sync_read", offsetof(struct fuse_ll
, conn
.async_read
), 0 },
1172 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD
),
1173 FUSE_OPT_KEY("-h", KEY_HELP
),
1174 FUSE_OPT_KEY("--help", KEY_HELP
),
1175 FUSE_OPT_KEY("-V", KEY_VERSION
),
1176 FUSE_OPT_KEY("--version", KEY_VERSION
),
1180 static void fuse_ll_version(void)
1182 fprintf(stderr
, "using FUSE kernel interface version %i.%i\n",
1183 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
1186 static void fuse_ll_help(void)
1189 " -o max_write=N set maximum size of write requests\n"
1190 " -o max_readahead=N set maximum readahead\n"
1191 " -o async_read perform reads asynchronously (default)\n"
1192 " -o sync_read perform reads synchronously\n");
1195 static int fuse_ll_opt_proc(void *data
, const char *arg
, int key
,
1196 struct fuse_args
*outargs
)
1198 (void) data
; (void) outargs
;
1210 fprintf(stderr
, "fuse: unknown option `%s'\n", arg
);
1216 int fuse_lowlevel_is_lib_option(const char *opt
)
1218 return fuse_opt_match(fuse_ll_opts
, opt
);
1221 static void fuse_ll_destroy(void *data
)
1223 struct fuse_ll
*f
= (struct fuse_ll
*) data
;
1225 if (f
->got_init
&& !f
->got_destroy
) {
1227 f
->op
.destroy(f
->userdata
);
1230 pthread_mutex_destroy(&f
->lock
);
1235 * always call fuse_lowlevel_new_common() internally, to work around a
1236 * misfeature in the FreeBSD runtime linker, which links the old
1237 * version of a symbol to internal references.
1239 struct fuse_session
*fuse_lowlevel_new_common(struct fuse_args
*args
,
1240 const struct fuse_lowlevel_ops
*op
,
1241 size_t op_size
, void *userdata
)
1244 struct fuse_session
*se
;
1245 struct fuse_session_ops sop
= {
1246 .process
= fuse_ll_process
,
1247 .destroy
= fuse_ll_destroy
,
1250 if (sizeof(struct fuse_lowlevel_ops
) < op_size
) {
1251 fprintf(stderr
, "fuse: warning: library too old, some operations may not work\n");
1252 op_size
= sizeof(struct fuse_lowlevel_ops
);
1255 f
= (struct fuse_ll
*) calloc(1, sizeof(struct fuse_ll
));
1257 fprintf(stderr
, "fuse: failed to allocate fuse object\n");
1261 f
->conn
.async_read
= 1;
1262 f
->conn
.max_write
= UINT_MAX
;
1263 f
->conn
.max_readahead
= UINT_MAX
;
1264 list_init_req(&f
->list
);
1265 list_init_req(&f
->interrupts
);
1266 fuse_mutex_init(&f
->lock
);
1268 if (fuse_opt_parse(args
, f
, fuse_ll_opts
, fuse_ll_opt_proc
) == -1)
1271 memcpy(&f
->op
, op
, op_size
);
1272 f
->owner
= getuid();
1273 f
->userdata
= userdata
;
1275 se
= fuse_session_new(&sop
, f
);
1288 struct fuse_session
*fuse_lowlevel_new(struct fuse_args
*args
,
1289 const struct fuse_lowlevel_ops
*op
,
1290 size_t op_size
, void *userdata
)
1292 return fuse_lowlevel_new_common(args
, op
, op_size
, userdata
);
1298 static void fill_open_compat(struct fuse_open_out
*arg
,
1299 const struct fuse_file_info_compat
*f
)
1303 arg
->open_flags
|= FOPEN_DIRECT_IO
;
1305 arg
->open_flags
|= FOPEN_KEEP_CACHE
;
1308 static void convert_statfs_compat(const struct statfs
*compatbuf
,
1309 struct statvfs
*buf
)
1311 buf
->f_bsize
= compatbuf
->f_bsize
;
1312 buf
->f_blocks
= compatbuf
->f_blocks
;
1313 buf
->f_bfree
= compatbuf
->f_bfree
;
1314 buf
->f_bavail
= compatbuf
->f_bavail
;
1315 buf
->f_files
= compatbuf
->f_files
;
1316 buf
->f_ffree
= compatbuf
->f_ffree
;
1317 buf
->f_namemax
= compatbuf
->f_namelen
;
1320 int fuse_reply_open_compat(fuse_req_t req
,
1321 const struct fuse_file_info_compat
*f
)
1323 struct fuse_open_out arg
;
1325 memset(&arg
, 0, sizeof(arg
));
1326 fill_open_compat(&arg
, f
);
1327 return send_reply_ok(req
, &arg
, sizeof(arg
));
1330 int fuse_reply_statfs_compat(fuse_req_t req
, const struct statfs
*stbuf
)
1332 struct statvfs newbuf
;
1334 memset(&newbuf
, 0, sizeof(newbuf
));
1335 convert_statfs_compat(stbuf
, &newbuf
);
1337 return fuse_reply_statfs(req
, &newbuf
);
1340 struct fuse_session
*fuse_lowlevel_new_compat(const char *opts
,
1341 const struct fuse_lowlevel_ops_compat
*op
,
1342 size_t op_size
, void *userdata
)
1344 struct fuse_session
*se
;
1345 struct fuse_args args
= FUSE_ARGS_INIT(0, NULL
);
1348 (fuse_opt_add_arg(&args
, "") == -1 ||
1349 fuse_opt_add_arg(&args
, "-o") == -1 ||
1350 fuse_opt_add_arg(&args
, opts
) == -1)) {
1351 fuse_opt_free_args(&args
);
1354 se
= fuse_lowlevel_new(&args
, (const struct fuse_lowlevel_ops
*) op
,
1356 fuse_opt_free_args(&args
);
1361 struct fuse_ll_compat_conf
{
1366 static const struct fuse_opt fuse_ll_opts_compat
[] = {
1367 { "max_read=", offsetof(struct fuse_ll_compat_conf
, set_max_read
), 1 },
1368 { "max_read=%u", offsetof(struct fuse_ll_compat_conf
, max_read
), 0 },
1369 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP
),
1373 int fuse_sync_compat_args(struct fuse_args
*args
)
1375 struct fuse_ll_compat_conf conf
;
1377 memset(&conf
, 0, sizeof(conf
));
1378 if (fuse_opt_parse(args
, &conf
, fuse_ll_opts_compat
, NULL
) == -1)
1381 if (fuse_opt_insert_arg(args
, 1, "-osync_read"))
1384 if (conf
.set_max_read
) {
1387 sprintf(tmpbuf
, "-omax_readahead=%u", conf
.max_read
);
1388 if (fuse_opt_insert_arg(args
, 1, tmpbuf
) == -1)
1394 __asm__(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
1395 __asm__(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
1396 __asm__(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
1398 #else /* __FreeBSD__ */
1400 int fuse_sync_compat_args(struct fuse_args
*args
)
1406 #endif /* __FreeBSD__ */
1408 struct fuse_session
*fuse_lowlevel_new_compat25(struct fuse_args
*args
,
1409 const struct fuse_lowlevel_ops_compat25
*op
,
1410 size_t op_size
, void *userdata
)
1412 if (fuse_sync_compat_args(args
) == -1)
1415 return fuse_lowlevel_new_common(args
,
1416 (const struct fuse_lowlevel_ops
*) op
,
1420 __asm__(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");