Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / ntfs-3g / libfuse-lite / fuse_lowlevel.c
blob880cdf01419a0acbcff37714d115e24cd8b935eb
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 LGPLv2.
6 See the file COPYING.LIB
7 */
9 #include "config.h"
10 #include "fuse_lowlevel.h"
11 #include "fuse_kernel.h"
12 #include "fuse_opt.h"
13 #include "fuse_i.h"
14 #include "fuse_misc.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) (((const 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);
81 #ifdef POSIXACLS
82 attr->filling = 0; /* JPA trying to be safe */
83 #endif
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)
100 size_t seg;
101 size_t ret = 0;
103 for (seg = 0; seg < count; seg++)
104 ret += iov[seg].iov_len;
105 return ret;
108 static void list_init_req(struct fuse_req *req)
110 req->next = req;
111 req->prev = 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;
118 prev->next = next;
119 next->prev = prev;
122 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
124 struct fuse_req *prev = next->prev;
125 req->next = next;
126 req->prev = prev;
127 prev->next = req;
128 next->prev = req;
131 static void destroy_req(fuse_req_t req)
133 pthread_mutex_destroy(&req->lock);
134 free(req);
137 static void free_req(fuse_req_t req)
139 int ctr;
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);
148 list_del_req(req);
149 ctr = --req->ctr;
150 pthread_mutex_unlock(&f->lock);
151 if (!ctr)
152 destroy_req(req);
155 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
156 int count)
158 struct fuse_out_header out;
159 int res;
161 if (error <= -1000 || error > 0) {
162 fprintf(stderr, "fuse: bad error value: %i\n", error);
163 error = -ERANGE;
166 out.unique = req->unique;
167 out.error = error;
168 iov[0].iov_base = &out;
169 iov[0].iov_len = sizeof(struct fuse_out_header);
170 out.len = iov_length(iov, count);
172 if (req->f->debug)
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);
177 free_req(req);
179 return res;
182 static int send_reply(fuse_req_t req, int error, const void *arg,
183 size_t argsize)
185 struct iovec iov[2];
186 int count = 1;
187 if (argsize) {
188 iov[1].iov_base = (void *) arg;
189 iov[1].iov_len = argsize;
190 count++;
192 return send_reply_iov(req, error, iov, count);
195 #if 0 /* not used */
196 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
198 int res;
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));
206 count++;
208 res = send_reply_iov(req, 0, padded_iov, count);
209 free(padded_iov);
211 return res;
213 #endif
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,
221 off_t off)
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;
230 dirent->off = off;
231 dirent->namelen = namelen;
232 dirent->type = (stbuf->st_mode & 0170000) >> 12;
233 strncpy(dirent->name, name, namelen);
234 if (padlen)
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)
243 size_t entsize;
245 (void) req;
246 entsize = fuse_dirent_size(strlen(name));
247 if (entsize <= bufsize && buf)
248 fuse_add_dirent(buf, name, stbuf, off);
249 return entsize;
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);
278 free_req(req);
281 static unsigned long calc_timeout_sec(double t)
283 if (t > (double) ULONG_MAX)
284 return ULONG_MAX;
285 else if (t < 0.0)
286 return 0;
287 else
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;
295 if (f < 0.0)
296 return 0;
297 else if (f >= 0.999999999)
298 return 999999999;
299 else
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)
318 arg->fh = f->fh;
319 if (f->direct_io)
320 arg->open_flags |= FOPEN_DIRECT_IO;
321 if (f->keep_cache)
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
330 negative entry */
331 if (!e->ino && req->f->conn.proto_minor < 4)
332 return fuse_reply_err(req, ENOENT);
334 memset(&arg, 0, sizeof(arg));
335 fill_entry(&arg, e);
336 #ifdef POSIXACLS
337 return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12
338 ? sizeof(arg) : FUSE_COMPAT_ENTRY_OUT_SIZE));
339 #else
340 return send_reply_ok(req, &arg, sizeof(arg));
341 #endif
344 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
345 const struct fuse_file_info *f)
347 struct {
348 struct fuse_entry_out e;
349 struct fuse_open_out o;
350 } arg;
352 memset(&arg, 0, sizeof(arg));
353 fill_entry(&arg.e, e);
354 #ifdef POSIXACLS
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));
360 } else {
361 fill_open(&arg.o, f);
362 return send_reply_ok(req, &arg, sizeof(arg));
364 #else
365 fill_open(&arg.o, f);
366 return send_reply_ok(req, &arg, sizeof(arg));
367 #endif
370 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
371 double attr_timeout)
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);
380 #ifdef POSIXACLS
381 return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12
382 ? sizeof(arg) : FUSE_COMPAT_FUSE_ATTR_OUT_SIZE));
383 #else
384 return send_reply_ok(req, &arg, sizeof(arg));
385 #endif
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));
398 fill_open(&arg, f);
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));
407 arg.size = count;
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));
433 arg.size = count;
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;
448 else
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));
460 arg.block = idx;
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);
471 else
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);
481 else
482 fuse_reply_none(req);
485 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
487 (void) inarg;
489 if (req->f->op.getattr)
490 req->f->op.getattr(req, nodeid, NULL);
491 else
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;
502 struct stat stbuf;
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));
507 fi = &fi_store;
508 fi->fh = arg->fh;
509 fi->fh_old = fi->fh;
511 req->f->op.setattr(req, nodeid, &stbuf, arg->valid & ~FATTR_FH, fi);
512 } else
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);
522 else
523 fuse_reply_err(req, ENOSYS);
526 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
528 (void) inarg;
530 if (req->f->op.readlink)
531 req->f->op.readlink(req, nodeid);
532 else
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);
541 #ifdef POSIXACLS
542 if (req->f->conn.proto_minor >= 12)
543 req->ctx.umask = arg->umask;
544 else
545 #endif
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);
550 else
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;
558 #ifdef POSIXACLS
559 if (req->f->conn.proto_minor >= 12)
560 req->ctx.umask = arg->umask;
561 #endif
563 if (req->f->op.mkdir)
564 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
565 else
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);
575 else
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);
585 else
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);
596 else
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);
608 else
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;
616 if (req->f->op.link)
617 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
618 else
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;
633 #ifdef POSIXACLS
634 if (req->f->conn.proto_minor >= 12)
635 req->ctx.umask = arg->umask;
636 else
637 #endif
638 name = (const char *) inarg + sizeof(struct fuse_open_in);
640 req->f->op.create(req, nodeid, name, arg->mode, &fi);
641 } else
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;
653 if (req->f->op.open)
654 req->f->op.open(req, nodeid, &fi);
655 else
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));
667 fi.fh = arg->fh;
668 fi.fh_old = fi.fh;
669 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
670 } else
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));
680 fi.fh = arg->fh;
681 fi.fh_old = fi.fh;
682 fi.writepage = arg->write_flags & 1;
684 if (req->f->op.write) {
685 #ifdef POSIXACLS
686 const char *buf;
688 if (req->f->conn.proto_minor >= 12)
689 buf = PARAM(arg);
690 else
691 buf = ((const char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
692 req->f->op.write(req, nodeid, buf, arg->size, arg->offset, &fi);
693 #else
694 req->f->op.write(req, nodeid, PARAM(arg), arg->size, arg->offset, &fi);
695 #endif
696 } else
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));
706 fi.fh = arg->fh;
707 fi.fh_old = fi.fh;
708 fi.flush = 1;
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);
714 else
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;
725 fi.fh = arg->fh;
726 fi.fh_old = fi.fh;
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);
734 else
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));
744 fi.fh = arg->fh;
745 fi.fh_old = fi.fh;
747 if (req->f->op.fsync)
748 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
749 else
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);
763 else
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));
773 fi.fh = arg->fh;
774 fi.fh_old = fi.fh;
776 if (req->f->op.readdir)
777 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
778 else
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;
789 fi.fh = arg->fh;
790 fi.fh_old = fi.fh;
792 if (req->f->op.releasedir)
793 req->f->op.releasedir(req, nodeid, &fi);
794 else
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));
804 fi.fh = arg->fh;
805 fi.fh_old = fi.fh;
807 if (req->f->op.fsyncdir)
808 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
809 else
810 fuse_reply_err(req, ENOSYS);
813 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
815 (void) nodeid;
816 (void) inarg;
818 if (req->f->op.statfs)
819 req->f->op.statfs(req, nodeid);
820 else {
821 struct statvfs buf = {
822 .f_namemax = 255,
823 .f_bsize = 512,
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);
837 else
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);
847 else
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);
857 else
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);
867 else
868 fuse_reply_err(req, ENOSYS);
871 static void convert_fuse_file_lock(const struct fuse_file_lock *fl,
872 struct flock *flock)
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)
879 flock->l_len = 0;
880 else
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;
889 struct flock flock;
891 memset(&fi, 0, sizeof(fi));
892 fi.fh = arg->fh;
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);
898 else
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;
907 struct flock flock;
909 memset(&fi, 0, sizeof(fi));
910 fi.fh = arg->fh;
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);
916 else
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) {
936 curr->ctr++;
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);
944 if (curr->u.ni.func)
945 curr->u.ni.func(curr, curr->u.ni.data);
946 pthread_mutex_unlock(&curr->lock);
948 pthread_mutex_lock(&f->lock);
949 curr->ctr--;
950 if (!curr->ctr)
951 destroy_req(curr);
953 return 1;
956 for (curr = f->interrupts.next; curr != &f->interrupts;
957 curr = curr->next) {
958 if (curr->u.i.unique == req->u.i.unique)
959 return 1;
961 return 0;
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;
969 (void) nodeid;
970 if (f->debug)
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))
977 destroy_req(req);
978 else
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;
990 list_del_req(curr);
991 free(curr);
992 return NULL;
995 curr = f->interrupts.next;
996 if (curr != &f->interrupts) {
997 list_del_req(curr);
998 list_init_req(curr);
999 return curr;
1000 } else
1001 return NULL;
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);
1010 else
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);
1021 (void) nodeid;
1022 if (f->debug) {
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);
1036 return;
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;
1044 #ifdef POSIXACLS
1045 if (arg->flags & FUSE_DONT_MASK)
1046 f->conn.capable |= FUSE_CAP_DONT_MASK;
1047 #endif
1048 if (arg->flags & FUSE_BIG_WRITES)
1049 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1050 } else {
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",
1057 bufsize);
1058 bufsize = FUSE_MIN_READ_BUFFER;
1061 bufsize -= 4096;
1062 if (bufsize < f->conn.max_write)
1063 f->conn.max_write = bufsize;
1065 f->got_init = 1;
1066 if (f->op.init)
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
1078 #ifdef POSIXACLS
1079 if (arg->major > 7 || (arg->major == 7 && arg->minor >= 12))
1080 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1081 else
1082 outarg.minor = FUSE_KERNEL_MINOR_FALLBACK;
1083 #else
1084 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1085 #endif
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;
1090 #ifdef POSIXACLS
1091 if (f->conn.want & FUSE_CAP_DONT_MASK)
1092 outarg.flags |= FUSE_DONT_MASK;
1093 #endif
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;
1099 if (f->debug) {
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;
1113 (void) nodeid;
1114 (void) inarg;
1116 f->got_destroy = 1;
1117 if (f->op.destroy)
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)
1130 return &req->ctx;
1133 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1134 void *data)
1136 pthread_mutex_lock(&req->lock);
1137 req->u.ni.func = func;
1138 req->u.ni.data = data;
1139 if (req->interrupted && func)
1140 func(req, data);
1141 pthread_mutex_unlock(&req->lock);
1144 int fuse_req_interrupted(fuse_req_t req)
1146 int interrupted;
1148 pthread_mutex_lock(&req->f->lock);
1149 interrupted = req->interrupted;
1150 pthread_mutex_unlock(&req->f->lock);
1152 return interrupted;
1155 static struct {
1156 void (*func)(fuse_req_t, fuse_ino_t, const void *);
1157 const char *name;
1158 } fuse_ll_ops[] = {
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)
1202 return "???";
1203 else
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;
1215 if (f->debug)
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));
1222 if (req == NULL) {
1223 fprintf(stderr, "fuse: failed to allocate request\n");
1224 return;
1227 req->f = f;
1228 req->unique = in->unique;
1229 req->ctx.uid = in->uid;
1230 req->ctx.gid = in->gid;
1231 req->ctx.pid = in->pid;
1232 req->ch = ch;
1233 req->ctr = 1;
1234 list_init_req(req);
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);
1247 else {
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);
1254 if (intr)
1255 fuse_reply_err(intr, EAGAIN);
1257 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
1261 enum {
1262 KEY_HELP,
1263 KEY_VERSION,
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),
1279 FUSE_OPT_END
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)
1290 fprintf(stderr,
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;
1302 switch (key) {
1303 case KEY_HELP:
1304 fuse_ll_help();
1305 break;
1307 case KEY_VERSION:
1308 fuse_ll_version();
1309 break;
1311 default:
1312 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
1315 return -1;
1318 #ifdef __SOLARIS__
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) {
1332 if (f->op.destroy)
1333 f->op.destroy(f->userdata);
1336 pthread_mutex_destroy(&f->lock);
1337 free(f);
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)
1344 struct fuse_ll *f;
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));
1357 if (f == NULL) {
1358 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1359 goto out;
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)
1370 goto out_free;
1372 memcpy(&f->op, op, op_size);
1373 f->owner = getuid();
1374 f->userdata = userdata;
1376 se = fuse_session_new(&sop, f);
1377 if (!se)
1378 goto out_free;
1380 return se;
1382 out_free:
1383 free(f);
1384 out:
1385 return NULL;