update copyright dates
[fuse.git] / lib / fuse_lowlevel.c
blobf6342b2a7e38d421f4bae83ea12ca079af1057af
1 /*
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
7 */
9 #include "fuse_lowlevel.h"
10 #include "fuse_kernel.h"
11 #include "fuse_opt.h"
12 #include "fuse_i.h"
13 #include "fuse_misc.h"
14 #include "fuse_common_compat.h"
15 #include "fuse_lowlevel_compat.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <limits.h>
23 #include <errno.h>
25 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
26 #define OFFSET_MAX 0x7fffffffffffffffLL
28 struct fuse_ll;
30 struct fuse_req {
31 struct fuse_ll *f;
32 uint64_t unique;
33 int ctr;
34 pthread_mutex_t lock;
35 struct fuse_ctx ctx;
36 struct fuse_chan *ch;
37 int interrupted;
38 union {
39 struct {
40 uint64_t unique;
41 } i;
42 struct {
43 fuse_interrupt_func_t func;
44 void *data;
45 } ni;
46 } u;
47 struct fuse_req *next;
48 struct fuse_req *prev;
51 struct fuse_ll {
52 int debug;
53 int allow_root;
54 struct fuse_lowlevel_ops op;
55 int got_init;
56 void *userdata;
57 uid_t owner;
58 struct fuse_conn_info conn;
59 struct fuse_req list;
60 struct fuse_req interrupts;
61 pthread_mutex_t lock;
62 int got_destroy;
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)
97 size_t seg;
98 size_t ret = 0;
100 for (seg = 0; seg < count; seg++)
101 ret += iov[seg].iov_len;
102 return ret;
105 static void list_init_req(struct fuse_req *req)
107 req->next = req;
108 req->prev = 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;
115 prev->next = next;
116 next->prev = prev;
119 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
121 struct fuse_req *prev = next->prev;
122 req->next = next;
123 req->prev = prev;
124 prev->next = req;
125 next->prev = req;
128 static void destroy_req(fuse_req_t req)
130 pthread_mutex_destroy(&req->lock);
131 free(req);
134 static void free_req(fuse_req_t req)
136 int ctr;
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);
145 list_del_req(req);
146 ctr = --req->ctr;
147 pthread_mutex_unlock(&f->lock);
148 if (!ctr)
149 destroy_req(req);
152 static int send_reply(fuse_req_t req, int error, const void *arg,
153 size_t argsize)
155 struct fuse_out_header out;
156 struct iovec iov[2];
157 size_t count;
158 int res;
160 if (error <= -1000 || error > 0) {
161 fprintf(stderr, "fuse: bad error value: %i\n", error);
162 error = -ERANGE;
165 out.unique = req->unique;
166 out.error = error;
167 count = 1;
168 iov[0].iov_base = &out;
169 iov[0].iov_len = sizeof(struct fuse_out_header);
170 if (argsize && !error) {
171 count++;
172 iov[1].iov_base = (void *) arg;
173 iov[1].iov_len = argsize;
175 out.len = iov_length(iov, count);
177 if (req->f->debug) {
178 printf(" unique: %llu, error: %i (%s), outsize: %i\n",
179 out.unique, out.error, strerror(-out.error), out.len);
180 fflush(stdout);
182 res = fuse_chan_send(req->ch, iov, count);
183 free_req(req);
185 return res;
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,
194 off_t off)
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;
203 dirent->off = off;
204 dirent->namelen = namelen;
205 dirent->type = (stbuf->st_mode & 0170000) >> 12;
206 strncpy(dirent->name, name, namelen);
207 if (padlen)
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)
216 size_t entsize;
218 (void) req;
219 entsize = fuse_dirent_size(strlen(name));
220 if (entsize <= bufsize && buf)
221 fuse_add_dirent(buf, name, stbuf, off);
222 return entsize;
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);
251 free_req(req);
254 static unsigned long calc_timeout_sec(double t)
256 if (t > (double) ULONG_MAX)
257 return ULONG_MAX;
258 else if (t < 0.0)
259 return 0;
260 else
261 return (unsigned long) t;
264 static unsigned int calc_timeout_nsec(double t)
266 double f = t - (double) calc_timeout_sec(t);
267 if (f < 0.0)
268 return 0;
269 else if (f >= 0.999999999)
270 return 999999999;
271 else
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)
290 arg->fh = f->fh;
291 if (f->direct_io)
292 arg->open_flags |= FOPEN_DIRECT_IO;
293 if (f->keep_cache)
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
302 negative entry */
303 if (!e->ino && req->f->conn.proto_minor < 4)
304 return fuse_reply_err(req, ENOENT);
306 memset(&arg, 0, sizeof(arg));
307 fill_entry(&arg, e);
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)
314 struct {
315 struct fuse_entry_out e;
316 struct fuse_open_out o;
317 } arg;
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,
326 double attr_timeout)
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));
348 fill_open(&arg, f);
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));
357 arg.size = count;
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));
383 arg.size = count;
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;
398 else
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));
410 arg.block = idx;
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);
421 else
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)
435 (void) inarg;
437 if (req->f->op.getattr)
438 req->f->op.getattr(req, nodeid, NULL);
439 else
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;
450 struct stat stbuf;
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));
456 fi = &fi_store;
457 fi->fh = arg->fh;
458 fi->fh_old = fi->fh;
460 req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
461 } else
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);
471 else
472 fuse_reply_err(req, ENOSYS);
475 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
477 (void) inarg;
479 if (req->f->op.readlink)
480 req->f->op.readlink(req, nodeid);
481 else
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);
491 else
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);
501 else
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);
511 else
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);
521 else
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);
532 else
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);
544 else
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;
552 if (req->f->op.link)
553 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
554 else
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);
569 } else
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;
581 if (req->f->op.open)
582 req->f->op.open(req, nodeid, &fi);
583 else
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));
595 fi.fh = arg->fh;
596 fi.fh_old = fi.fh;
597 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
598 } else
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));
608 fi.fh = arg->fh;
609 fi.fh_old = fi.fh;
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);
614 else
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));
624 fi.fh = arg->fh;
625 fi.fh_old = fi.fh;
626 fi.flush = 1;
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);
632 else
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;
643 fi.fh = arg->fh;
644 fi.fh_old = fi.fh;
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);
652 else
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));
662 fi.fh = arg->fh;
663 fi.fh_old = fi.fh;
665 if (req->f->op.fsync)
666 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
667 else
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);
681 else
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));
691 fi.fh = arg->fh;
692 fi.fh_old = fi.fh;
694 if (req->f->op.readdir)
695 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
696 else
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;
707 fi.fh = arg->fh;
708 fi.fh_old = fi.fh;
710 if (req->f->op.releasedir)
711 req->f->op.releasedir(req, nodeid, &fi);
712 else
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));
722 fi.fh = arg->fh;
723 fi.fh_old = fi.fh;
725 if (req->f->op.fsyncdir)
726 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
727 else
728 fuse_reply_err(req, ENOSYS);
731 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
733 (void) nodeid;
734 (void) inarg;
736 if (req->f->op.statfs)
737 req->f->op.statfs(req, nodeid);
738 else {
739 struct statvfs buf = {
740 .f_namemax = 255,
741 .f_bsize = 512,
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);
755 else
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);
765 else
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);
775 else
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);
785 else
786 fuse_reply_err(req, ENOSYS);
789 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
790 struct flock *flock)
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)
797 flock->l_len = 0;
798 else
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;
807 struct flock flock;
809 memset(&fi, 0, sizeof(fi));
810 fi.fh = arg->fh;
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);
816 else
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;
825 struct flock flock;
827 memset(&fi, 0, sizeof(fi));
828 fi.fh = arg->fh;
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);
834 else
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) {
854 curr->ctr++;
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);
862 if (curr->u.ni.func)
863 curr->u.ni.func(curr, curr->u.ni.data);
864 pthread_mutex_unlock(&curr->lock);
866 pthread_mutex_lock(&f->lock);
867 curr->ctr--;
868 if (!curr->ctr)
869 destroy_req(curr);
871 return 1;
874 for (curr = f->interrupts.next; curr != &f->interrupts;
875 curr = curr->next) {
876 if (curr->u.i.unique == req->u.i.unique)
877 return 1;
879 return 0;
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;
887 (void) nodeid;
888 if (f->debug) {
889 printf("INTERRUPT: %llu\n", (unsigned long long) arg->unique);
890 fflush(stdout);
893 req->u.i.unique = arg->unique;
895 pthread_mutex_lock(&f->lock);
896 if (find_interrupted(f, req))
897 destroy_req(req);
898 else
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;
910 list_del_req(curr);
911 free(curr);
912 return NULL;
915 curr = f->interrupts.next;
916 if (curr != &f->interrupts) {
917 list_del_req(curr);
918 list_init_req(curr);
919 return curr;
920 } else
921 return NULL;
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;
928 if (req->f->op.bmap)
929 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
930 else
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);
941 (void) nodeid;
942 if (f->debug) {
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);
948 fflush(stdout);
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);
957 return;
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;
965 } else {
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",
972 bufsize);
973 bufsize = FUSE_MIN_READ_BUFFER;
976 bufsize -= 4096;
977 if (bufsize < f->conn.max_write)
978 f->conn.max_write = bufsize;
980 f->got_init = 1;
981 if (f->op.init)
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;
994 if (f->debug) {
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);
999 fflush(stdout);
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;
1009 (void) nodeid;
1010 (void) inarg;
1012 f->got_destroy = 1;
1013 if (f->op.destroy)
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)
1026 return &req->ctx;
1029 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1030 void *data)
1032 pthread_mutex_lock(&req->lock);
1033 req->u.ni.func = func;
1034 req->u.ni.data = data;
1035 if (req->interrupted && func)
1036 func(req, data);
1037 pthread_mutex_unlock(&req->lock);
1040 int fuse_req_interrupted(fuse_req_t req)
1042 int interrupted;
1044 pthread_mutex_lock(&req->f->lock);
1045 interrupted = req->interrupted;
1046 pthread_mutex_unlock(&req->f->lock);
1048 return interrupted;
1051 static struct {
1052 void (*func)(fuse_req_t, fuse_ino_t, const void *);
1053 const char *name;
1054 } fuse_ll_ops[] = {
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)
1098 return "???";
1099 else
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;
1111 if (f->debug) {
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);
1116 fflush(stdout);
1119 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
1120 if (req == NULL) {
1121 fprintf(stderr, "fuse: failed to allocate request\n");
1122 return;
1125 req->f = f;
1126 req->unique = in->unique;
1127 req->ctx.uid = in->uid;
1128 req->ctx.gid = in->gid;
1129 req->ctx.pid = in->pid;
1130 req->ch = ch;
1131 req->ctr = 1;
1132 list_init_req(req);
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);
1145 else {
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);
1152 if (intr)
1153 fuse_reply_err(intr, EAGAIN);
1155 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
1159 enum {
1160 KEY_HELP,
1161 KEY_VERSION,
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),
1177 FUSE_OPT_END
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)
1188 fprintf(stderr,
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;
1200 switch (key) {
1201 case KEY_HELP:
1202 fuse_ll_help();
1203 break;
1205 case KEY_VERSION:
1206 fuse_ll_version();
1207 break;
1209 default:
1210 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
1213 return -1;
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) {
1226 if (f->op.destroy)
1227 f->op.destroy(f->userdata);
1230 pthread_mutex_destroy(&f->lock);
1231 free(f);
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)
1243 struct fuse_ll *f;
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));
1256 if (f == NULL) {
1257 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1258 goto out;
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)
1269 goto out_free;
1271 memcpy(&f->op, op, op_size);
1272 f->owner = getuid();
1273 f->userdata = userdata;
1275 se = fuse_session_new(&sop, f);
1276 if (!se)
1277 goto out_free;
1279 return se;
1281 out_free:
1282 free(f);
1283 out:
1284 return NULL;
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);
1296 #ifndef __FreeBSD__
1298 static void fill_open_compat(struct fuse_open_out *arg,
1299 const struct fuse_file_info_compat *f)
1301 arg->fh = f->fh;
1302 if (f->direct_io)
1303 arg->open_flags |= FOPEN_DIRECT_IO;
1304 if (f->keep_cache)
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);
1347 if (opts &&
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);
1352 return NULL;
1354 se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
1355 op_size, userdata);
1356 fuse_opt_free_args(&args);
1358 return se;
1361 struct fuse_ll_compat_conf {
1362 unsigned max_read;
1363 int set_max_read;
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),
1370 FUSE_OPT_END
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)
1379 return -1;
1381 if (fuse_opt_insert_arg(args, 1, "-osync_read"))
1382 return -1;
1384 if (conf.set_max_read) {
1385 char tmpbuf[64];
1387 sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
1388 if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
1389 return -1;
1391 return 0;
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)
1402 (void) args;
1403 return 0;
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)
1413 return NULL;
1415 return fuse_lowlevel_new_common(args,
1416 (const struct fuse_lowlevel_ops *) op,
1417 op_size, userdata);
1420 __asm__(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");