summaryrefslogtreecommitdiff
path: root/libfuse-lite/fuse_lowlevel.c (plain)
blob: 2ade0d3ec09949cc5800f2c7ccfb519df9af916b
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7*/
8
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"
16
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>
24
25#define PARAM(inarg) (((const char *)(inarg)) + sizeof(*(inarg)))
26#define OFFSET_MAX 0x7fffffffffffffffLL
27
28struct fuse_ll;
29
30struct 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;
49};
50
51struct 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;
63};
64
65static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
66{
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
84}
85
86static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
87{
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);
96}
97
98static size_t iov_length(const struct iovec *iov, size_t count)
99{
100 size_t seg;
101 size_t ret = 0;
102
103 for (seg = 0; seg < count; seg++)
104 ret += iov[seg].iov_len;
105 return ret;
106}
107
108static void list_init_req(struct fuse_req *req)
109{
110 req->next = req;
111 req->prev = req;
112}
113
114static void list_del_req(struct fuse_req *req)
115{
116 struct fuse_req *prev = req->prev;
117 struct fuse_req *next = req->next;
118 prev->next = next;
119 next->prev = prev;
120}
121
122static void list_add_req(struct fuse_req *req, struct fuse_req *next)
123{
124 struct fuse_req *prev = next->prev;
125 req->next = next;
126 req->prev = prev;
127 prev->next = req;
128 next->prev = req;
129}
130
131static void destroy_req(fuse_req_t req)
132{
133 pthread_mutex_destroy(&req->lock);
134 free(req);
135}
136
137static void free_req(fuse_req_t req)
138{
139 int ctr;
140 struct fuse_ll *f = req->f;
141
142 pthread_mutex_lock(&req->lock);
143 req->u.ni.func = NULL;
144 req->u.ni.data = NULL;
145 pthread_mutex_unlock(&req->lock);
146
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);
153}
154
155static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
156 int count)
157{
158 struct fuse_out_header out;
159 int res;
160
161 if (error <= -1000 || error > 0) {
162 fprintf(stderr, "fuse: bad error value: %i\n", error);
163 error = -ERANGE;
164 }
165
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);
171
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);
178
179 return res;
180}
181
182static int send_reply(fuse_req_t req, int error, const void *arg,
183 size_t argsize)
184{
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++;
191 }
192 return send_reply_iov(req, error, iov, count);
193}
194
195#if 0 /* not used */
196int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
197{
198 int res;
199 struct iovec *padded_iov;
200
201 padded_iov = malloc((count + 1) * sizeof(struct iovec));
202 if (padded_iov == NULL)
203 return fuse_reply_err(req, -ENOMEM);
204
205 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
206 count++;
207
208 res = send_reply_iov(req, 0, padded_iov, count);
209 free(padded_iov);
210
211 return res;
212}
213#endif
214
215size_t fuse_dirent_size(size_t namelen)
216{
217 return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
218}
219
220char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
221 off_t off)
222{
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;
228
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);
236
237 return buf + entsize;
238}
239
240size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
241 const char *name, const struct stat *stbuf, off_t off)
242{
243 size_t entsize;
244
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;
250}
251
252#if HAVE_SYS_STATVFS_H
253static void convert_statfs(const struct statvfs *stbuf,
254 struct fuse_kstatfs *kstatfs)
255{
256 kstatfs->bsize = stbuf->f_bsize;
257 kstatfs->frsize = stbuf->f_frsize;
258 kstatfs->blocks = stbuf->f_blocks;
259 kstatfs->bfree = stbuf->f_bfree;
260 kstatfs->bavail = stbuf->f_bavail;
261 kstatfs->files = stbuf->f_files;
262 kstatfs->ffree = stbuf->f_ffree;
263 kstatfs->namelen = stbuf->f_namemax;
264}
265#endif
266
267static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
268{
269 return send_reply(req, 0, arg, argsize);
270}
271
272int fuse_reply_err(fuse_req_t req, int err)
273{
274 return send_reply(req, -err, NULL, 0);
275}
276
277void fuse_reply_none(fuse_req_t req)
278{
279 fuse_chan_send(req->ch, NULL, 0);
280 free_req(req);
281}
282
283static unsigned long calc_timeout_sec(double t)
284{
285 if (t > (double) ULONG_MAX)
286 return ULONG_MAX;
287 else if (t < 0.0)
288 return 0;
289 else
290 return (unsigned long) t;
291}
292
293static unsigned int calc_timeout_nsec(double t)
294{
295 unsigned long secs = calc_timeout_sec(t);
296 double f = t - (double)secs;
297 if (f < 0.0)
298 return 0;
299 else if (f >= 0.999999999)
300 return 999999999;
301 else
302 return (unsigned int) (f * 1.0e9);
303}
304
305static void fill_entry(struct fuse_entry_out *arg,
306 const struct fuse_entry_param *e)
307{
308 arg->nodeid = e->ino;
309 arg->generation = e->generation;
310 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
311 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
312 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
313 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
314 convert_stat(&e->attr, &arg->attr);
315}
316
317static void fill_open(struct fuse_open_out *arg,
318 const struct fuse_file_info *f)
319{
320 arg->fh = f->fh;
321 if (f->direct_io)
322 arg->open_flags |= FOPEN_DIRECT_IO;
323 if (f->keep_cache)
324 arg->open_flags |= FOPEN_KEEP_CACHE;
325}
326
327int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
328{
329 struct fuse_entry_out arg;
330
331 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
332 negative entry */
333 if (!e->ino && req->f->conn.proto_minor < 4)
334 return fuse_reply_err(req, ENOENT);
335
336 memset(&arg, 0, sizeof(arg));
337 fill_entry(&arg, e);
338#ifdef POSIXACLS
339 return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12
340 ? sizeof(arg) : FUSE_COMPAT_ENTRY_OUT_SIZE));
341#else
342 return send_reply_ok(req, &arg, sizeof(arg));
343#endif
344}
345
346int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
347 const struct fuse_file_info *f)
348{
349 struct {
350 struct fuse_entry_out e;
351 struct fuse_open_out o;
352 } arg;
353
354 memset(&arg, 0, sizeof(arg));
355 fill_entry(&arg.e, e);
356#ifdef POSIXACLS
357 if (req->f->conn.proto_minor < 12) {
358 fill_open((struct fuse_open_out*)
359 ((char*)&arg + FUSE_COMPAT_ENTRY_OUT_SIZE), f);
360 return send_reply_ok(req, &arg,
361 FUSE_COMPAT_ENTRY_OUT_SIZE + sizeof(struct fuse_open_out));
362 } else {
363 fill_open(&arg.o, f);
364 return send_reply_ok(req, &arg, sizeof(arg));
365 }
366#else
367 fill_open(&arg.o, f);
368 return send_reply_ok(req, &arg, sizeof(arg));
369#endif
370}
371
372int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
373 double attr_timeout)
374{
375 struct fuse_attr_out arg;
376
377 memset(&arg, 0, sizeof(arg));
378 arg.attr_valid = calc_timeout_sec(attr_timeout);
379 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
380 convert_stat(attr, &arg.attr);
381
382#ifdef POSIXACLS
383 return send_reply_ok(req, &arg, (req->f->conn.proto_minor >= 12
384 ? sizeof(arg) : FUSE_COMPAT_FUSE_ATTR_OUT_SIZE));
385#else
386 return send_reply_ok(req, &arg, sizeof(arg));
387#endif
388}
389
390int fuse_reply_readlink(fuse_req_t req, const char *linkname)
391{
392 return send_reply_ok(req, linkname, strlen(linkname));
393}
394
395int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
396{
397 struct fuse_open_out arg;
398
399 memset(&arg, 0, sizeof(arg));
400 fill_open(&arg, f);
401 return send_reply_ok(req, &arg, sizeof(arg));
402}
403
404int fuse_reply_write(fuse_req_t req, size_t count)
405{
406 struct fuse_write_out arg;
407
408 memset(&arg, 0, sizeof(arg));
409 arg.size = count;
410
411 return send_reply_ok(req, &arg, sizeof(arg));
412}
413
414int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
415{
416 return send_reply_ok(req, buf, size);
417}
418
419#if HAVE_SYS_STATVFS_H
420int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
421{
422 struct fuse_statfs_out arg;
423 size_t size = req->f->conn.proto_minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
424
425 memset(&arg, 0, sizeof(arg));
426 convert_statfs(stbuf, &arg.st);
427
428 return send_reply_ok(req, &arg, size);
429}
430#endif
431
432int fuse_reply_xattr(fuse_req_t req, size_t count)
433{
434 struct fuse_getxattr_out arg;
435
436 memset(&arg, 0, sizeof(arg));
437 arg.size = count;
438
439 return send_reply_ok(req, &arg, sizeof(arg));
440}
441
442int fuse_reply_lock(fuse_req_t req, struct flock *lock)
443{
444 struct fuse_lk_out arg;
445
446 memset(&arg, 0, sizeof(arg));
447 arg.lk.type = lock->l_type;
448 if (lock->l_type != F_UNLCK) {
449 arg.lk.start = lock->l_start;
450 if (lock->l_len == 0)
451 arg.lk.end = OFFSET_MAX;
452 else
453 arg.lk.end = lock->l_start + lock->l_len - 1;
454 }
455 arg.lk.pid = lock->l_pid;
456 return send_reply_ok(req, &arg, sizeof(arg));
457}
458
459int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
460{
461 struct fuse_bmap_out arg;
462
463 memset(&arg, 0, sizeof(arg));
464 arg.block = idx;
465
466 return send_reply_ok(req, &arg, sizeof(arg));
467}
468
469static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
470{
471 const char *name = (const char *) inarg;
472
473 if (req->f->op.lookup)
474 req->f->op.lookup(req, nodeid, name);
475 else
476 fuse_reply_err(req, ENOSYS);
477}
478
479static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
480{
481 const struct fuse_forget_in *arg = (const struct fuse_forget_in *) inarg;
482
483 if (req->f->op.forget)
484 req->f->op.forget(req, nodeid, arg->nlookup);
485 else
486 fuse_reply_none(req);
487}
488
489static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
490{
491 (void) inarg;
492
493 if (req->f->op.getattr)
494 req->f->op.getattr(req, nodeid, NULL);
495 else
496 fuse_reply_err(req, ENOSYS);
497}
498
499static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
500{
501 const struct fuse_setattr_in *arg = (const struct fuse_setattr_in *) inarg;
502
503 if (req->f->op.setattr) {
504 struct fuse_file_info *fi = NULL;
505 struct fuse_file_info fi_store;
506 struct stat stbuf;
507 memset(&stbuf, 0, sizeof(stbuf));
508 convert_attr(arg, &stbuf);
509 if (arg->valid & FATTR_FH) {
510 memset(&fi_store, 0, sizeof(fi_store));
511 fi = &fi_store;
512 fi->fh = arg->fh;
513 fi->fh_old = fi->fh;
514 }
515 req->f->op.setattr(req, nodeid, &stbuf, arg->valid & ~FATTR_FH, fi);
516 } else
517 fuse_reply_err(req, ENOSYS);
518}
519
520static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
521{
522 const struct fuse_access_in *arg = (const struct fuse_access_in *) inarg;
523
524 if (req->f->op.access)
525 req->f->op.access(req, nodeid, arg->mask);
526 else
527 fuse_reply_err(req, ENOSYS);
528}
529
530static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
531{
532 (void) inarg;
533
534 if (req->f->op.readlink)
535 req->f->op.readlink(req, nodeid);
536 else
537 fuse_reply_err(req, ENOSYS);
538}
539
540static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
541{
542 const struct fuse_mknod_in *arg = (const struct fuse_mknod_in *) inarg;
543 const char *name = PARAM(arg);
544
545#ifdef POSIXACLS
546 if (req->f->conn.proto_minor >= 12)
547 req->ctx.umask = arg->umask;
548 else
549#endif
550 name = (const char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
551
552 if (req->f->op.mknod)
553 req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
554 else
555 fuse_reply_err(req, ENOSYS);
556}
557
558static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
559{
560 const struct fuse_mkdir_in *arg = (const struct fuse_mkdir_in *) inarg;
561
562#ifdef POSIXACLS
563 if (req->f->conn.proto_minor >= 12)
564 req->ctx.umask = arg->umask;
565#endif
566
567 if (req->f->op.mkdir)
568 req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
569 else
570 fuse_reply_err(req, ENOSYS);
571}
572
573static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
574{
575 const char *name = (const char *) inarg;
576
577 if (req->f->op.unlink)
578 req->f->op.unlink(req, nodeid, name);
579 else
580 fuse_reply_err(req, ENOSYS);
581}
582
583static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
584{
585 const char *name = (const char *) inarg;
586
587 if (req->f->op.rmdir)
588 req->f->op.rmdir(req, nodeid, name);
589 else
590 fuse_reply_err(req, ENOSYS);
591}
592
593static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
594{
595 const char *name = (const char *) inarg;
596 const char *linkname = ((const char *) inarg) + strlen((const char *) inarg) + 1;
597
598 if (req->f->op.symlink)
599 req->f->op.symlink(req, linkname, nodeid, name);
600 else
601 fuse_reply_err(req, ENOSYS);
602}
603
604static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
605{
606 const struct fuse_rename_in *arg = (const struct fuse_rename_in *) inarg;
607 const char *oldname = PARAM(arg);
608 const char *newname = oldname + strlen(oldname) + 1;
609
610 if (req->f->op.rename)
611 req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
612 else
613 fuse_reply_err(req, ENOSYS);
614}
615
616static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
617{
618 const struct fuse_link_in *arg = (const struct fuse_link_in *) inarg;
619
620 if (req->f->op.link)
621 req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
622 else
623 fuse_reply_err(req, ENOSYS);
624}
625
626static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
627{
628 const struct fuse_create_in *arg = (const struct fuse_create_in *) inarg;
629
630 if (req->f->op.create) {
631 struct fuse_file_info fi;
632 const char *name = PARAM(arg);
633
634 memset(&fi, 0, sizeof(fi));
635 fi.flags = arg->flags;
636
637#ifdef POSIXACLS
638 if (req->f->conn.proto_minor >= 12)
639 req->ctx.umask = arg->umask;
640 else
641#endif
642 name = (const char *) inarg + sizeof(struct fuse_open_in);
643
644 req->f->op.create(req, nodeid, name, arg->mode, &fi);
645 } else
646 fuse_reply_err(req, ENOSYS);
647}
648
649static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
650{
651 const struct fuse_open_in *arg = (const struct fuse_open_in *) inarg;
652 struct fuse_file_info fi;
653
654 memset(&fi, 0, sizeof(fi));
655 fi.flags = arg->flags;
656
657 if (req->f->op.open)
658 req->f->op.open(req, nodeid, &fi);
659 else
660 fuse_reply_open(req, &fi);
661}
662
663static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
664{
665 const struct fuse_read_in *arg = (const struct fuse_read_in *) inarg;
666
667 if (req->f->op.read) {
668 struct fuse_file_info fi;
669
670 memset(&fi, 0, sizeof(fi));
671 fi.fh = arg->fh;
672 fi.fh_old = fi.fh;
673 req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
674 } else
675 fuse_reply_err(req, ENOSYS);
676}
677
678static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
679{
680 const struct fuse_write_in *arg = (const struct fuse_write_in *) inarg;
681 struct fuse_file_info fi;
682
683 memset(&fi, 0, sizeof(fi));
684 fi.fh = arg->fh;
685 fi.fh_old = fi.fh;
686 fi.writepage = arg->write_flags & 1;
687
688 if (req->f->op.write) {
689#ifdef POSIXACLS
690 const char *buf;
691
692 if (req->f->conn.proto_minor >= 12)
693 buf = PARAM(arg);
694 else
695 buf = ((const char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
696 req->f->op.write(req, nodeid, buf, arg->size, arg->offset, &fi);
697#else
698 req->f->op.write(req, nodeid, PARAM(arg), arg->size, arg->offset, &fi);
699#endif
700 } else
701 fuse_reply_err(req, ENOSYS);
702}
703
704static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
705{
706 const struct fuse_flush_in *arg = (const struct fuse_flush_in *) inarg;
707 struct fuse_file_info fi;
708
709 memset(&fi, 0, sizeof(fi));
710 fi.fh = arg->fh;
711 fi.fh_old = fi.fh;
712 fi.flush = 1;
713 if (req->f->conn.proto_minor >= 7)
714 fi.lock_owner = arg->lock_owner;
715
716 if (req->f->op.flush)
717 req->f->op.flush(req, nodeid, &fi);
718 else
719 fuse_reply_err(req, ENOSYS);
720}
721
722static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
723{
724 const struct fuse_release_in *arg = (const struct fuse_release_in *) inarg;
725 struct fuse_file_info fi;
726
727 memset(&fi, 0, sizeof(fi));
728 fi.flags = arg->flags;
729 fi.fh = arg->fh;
730 fi.fh_old = fi.fh;
731 if (req->f->conn.proto_minor >= 8) {
732 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
733 fi.lock_owner = arg->lock_owner;
734 }
735
736 if (req->f->op.release)
737 req->f->op.release(req, nodeid, &fi);
738 else
739 fuse_reply_err(req, 0);
740}
741
742static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
743{
744 const struct fuse_fsync_in *arg = (const struct fuse_fsync_in *) inarg;
745 struct fuse_file_info fi;
746
747 memset(&fi, 0, sizeof(fi));
748 fi.fh = arg->fh;
749 fi.fh_old = fi.fh;
750
751 if (req->f->op.fsync)
752 req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
753 else
754 fuse_reply_err(req, ENOSYS);
755}
756
757static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
758{
759 const struct fuse_open_in *arg = (const struct fuse_open_in *) inarg;
760 struct fuse_file_info fi;
761
762 memset(&fi, 0, sizeof(fi));
763 fi.flags = arg->flags;
764
765 if (req->f->op.opendir)
766 req->f->op.opendir(req, nodeid, &fi);
767 else
768 fuse_reply_open(req, &fi);
769}
770
771static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
772{
773 const struct fuse_read_in *arg = (const struct fuse_read_in *) inarg;
774 struct fuse_file_info fi;
775
776 memset(&fi, 0, sizeof(fi));
777 fi.fh = arg->fh;
778 fi.fh_old = fi.fh;
779
780 if (req->f->op.readdir)
781 req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
782 else
783 fuse_reply_err(req, ENOSYS);
784}
785
786static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
787{
788 const struct fuse_release_in *arg = (const struct fuse_release_in *) inarg;
789 struct fuse_file_info fi;
790
791 memset(&fi, 0, sizeof(fi));
792 fi.flags = arg->flags;
793 fi.fh = arg->fh;
794 fi.fh_old = fi.fh;
795
796 if (req->f->op.releasedir)
797 req->f->op.releasedir(req, nodeid, &fi);
798 else
799 fuse_reply_err(req, 0);
800}
801
802static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
803{
804 const struct fuse_fsync_in *arg = (const struct fuse_fsync_in *) inarg;
805 struct fuse_file_info fi;
806
807 memset(&fi, 0, sizeof(fi));
808 fi.fh = arg->fh;
809 fi.fh_old = fi.fh;
810
811 if (req->f->op.fsyncdir)
812 req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
813 else
814 fuse_reply_err(req, ENOSYS);
815}
816
817#if HAVE_SYS_STATVFS_H
818static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
819{
820 (void) nodeid;
821 (void) inarg;
822
823 if (req->f->op.statfs)
824 req->f->op.statfs(req, nodeid);
825 else {
826 struct statvfs buf = {
827 .f_namemax = 255,
828 .f_bsize = 512,
829 };
830 fuse_reply_statfs(req, &buf);
831 }
832}
833#endif
834
835static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
836{
837 const struct fuse_setxattr_in *arg = (const struct fuse_setxattr_in *) inarg;
838 const char *name = PARAM(arg);
839 const char *value = name + strlen(name) + 1;
840
841 if (req->f->op.setxattr)
842 req->f->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
843 else
844 fuse_reply_err(req, ENOSYS);
845}
846
847static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
848{
849 const struct fuse_getxattr_in *arg = (const struct fuse_getxattr_in *) inarg;
850
851 if (req->f->op.getxattr)
852 req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
853 else
854 fuse_reply_err(req, ENOSYS);
855}
856
857static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
858{
859 const struct fuse_getxattr_in *arg = (const struct fuse_getxattr_in *) inarg;
860
861 if (req->f->op.listxattr)
862 req->f->op.listxattr(req, nodeid, arg->size);
863 else
864 fuse_reply_err(req, ENOSYS);
865}
866
867static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
868{
869 const char *name = (const char *) inarg;
870
871 if (req->f->op.removexattr)
872 req->f->op.removexattr(req, nodeid, name);
873 else
874 fuse_reply_err(req, ENOSYS);
875}
876
877static void convert_fuse_file_lock(const struct fuse_file_lock *fl,
878 struct flock *flock)
879{
880 memset(flock, 0, sizeof(struct flock));
881 flock->l_type = fl->type;
882 flock->l_whence = SEEK_SET;
883 flock->l_start = fl->start;
884 if (fl->end == OFFSET_MAX)
885 flock->l_len = 0;
886 else
887 flock->l_len = fl->end - fl->start + 1;
888 flock->l_pid = fl->pid;
889}
890
891static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
892{
893 const struct fuse_lk_in *arg = (const struct fuse_lk_in *) inarg;
894 struct fuse_file_info fi;
895 struct flock flock;
896
897 memset(&fi, 0, sizeof(fi));
898 fi.fh = arg->fh;
899 fi.lock_owner = arg->owner;
900
901 convert_fuse_file_lock(&arg->lk, &flock);
902 if (req->f->op.getlk)
903 req->f->op.getlk(req, nodeid, &fi, &flock);
904 else
905 fuse_reply_err(req, ENOSYS);
906}
907
908static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
909 const void *inarg, int should_sleep)
910{
911 const struct fuse_lk_in *arg = (const struct fuse_lk_in *) inarg;
912 struct fuse_file_info fi;
913 struct flock flock;
914
915 memset(&fi, 0, sizeof(fi));
916 fi.fh = arg->fh;
917 fi.lock_owner = arg->owner;
918
919 convert_fuse_file_lock(&arg->lk, &flock);
920 if (req->f->op.setlk)
921 req->f->op.setlk(req, nodeid, &fi, &flock, should_sleep);
922 else
923 fuse_reply_err(req, ENOSYS);
924}
925
926static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
927{
928 do_setlk_common(req, nodeid, inarg, 0);
929}
930
931static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
932{
933 do_setlk_common(req, nodeid, inarg, 1);
934}
935
936static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
937{
938 struct fuse_req *curr;
939
940 for (curr = f->list.next; curr != &f->list; curr = curr->next) {
941 if (curr->unique == req->u.i.unique) {
942 curr->ctr++;
943 pthread_mutex_unlock(&f->lock);
944
945 /* Ugh, ugly locking */
946 pthread_mutex_lock(&curr->lock);
947 pthread_mutex_lock(&f->lock);
948 curr->interrupted = 1;
949 pthread_mutex_unlock(&f->lock);
950 if (curr->u.ni.func)
951 curr->u.ni.func(curr, curr->u.ni.data);
952 pthread_mutex_unlock(&curr->lock);
953
954 pthread_mutex_lock(&f->lock);
955 curr->ctr--;
956 if (!curr->ctr)
957 destroy_req(curr);
958
959 return 1;
960 }
961 }
962 for (curr = f->interrupts.next; curr != &f->interrupts;
963 curr = curr->next) {
964 if (curr->u.i.unique == req->u.i.unique)
965 return 1;
966 }
967 return 0;
968}
969
970static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
971{
972 const struct fuse_interrupt_in *arg = (const struct fuse_interrupt_in *) inarg;
973 struct fuse_ll *f = req->f;
974
975 (void) nodeid;
976 if (f->debug)
977 fprintf(stderr, "INTERRUPT: %llu\n", (unsigned long long) arg->unique);
978
979 req->u.i.unique = arg->unique;
980
981 pthread_mutex_lock(&f->lock);
982 if (find_interrupted(f, req))
983 destroy_req(req);
984 else
985 list_add_req(req, &f->interrupts);
986 pthread_mutex_unlock(&f->lock);
987}
988
989static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
990{
991 struct fuse_req *curr;
992
993 for (curr = f->interrupts.next; curr != &f->interrupts; curr = curr->next) {
994 if (curr->u.i.unique == req->unique) {
995 req->interrupted = 1;
996 list_del_req(curr);
997 free(curr);
998 return NULL;
999 }
1000 }
1001 curr = f->interrupts.next;
1002 if (curr != &f->interrupts) {
1003 list_del_req(curr);
1004 list_init_req(curr);
1005 return curr;
1006 } else
1007 return NULL;
1008}
1009
1010static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1011{
1012 const struct fuse_bmap_in *arg = (const struct fuse_bmap_in *) inarg;
1013
1014 if (req->f->op.bmap)
1015 req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
1016 else
1017 fuse_reply_err(req, ENOSYS);
1018}
1019
1020static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1021{
1022 const struct fuse_init_in *arg = (const struct fuse_init_in *) inarg;
1023 struct fuse_init_out outarg;
1024 struct fuse_ll *f = req->f;
1025 size_t bufsize = fuse_chan_bufsize(req->ch);
1026
1027 (void) nodeid;
1028 if (f->debug) {
1029 fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
1030 if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) {
1031 fprintf(stderr, "flags=0x%08x\n", arg->flags);
1032 fprintf(stderr, "max_readahead=0x%08x\n", arg->max_readahead);
1033 }
1034 }
1035 f->conn.proto_major = arg->major;
1036 f->conn.proto_minor = arg->minor;
1037
1038 if (arg->major < 7) {
1039 fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
1040 arg->major, arg->minor);
1041 fuse_reply_err(req, EPROTO);
1042 return;
1043 }
1044
1045 if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) {
1046 if (f->conn.async_read)
1047 f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
1048 if (arg->max_readahead < f->conn.max_readahead)
1049 f->conn.max_readahead = arg->max_readahead;
1050#ifdef POSIXACLS
1051 if (arg->flags & FUSE_DONT_MASK)
1052 f->conn.capable |= FUSE_CAP_DONT_MASK;
1053#endif
1054 if (arg->flags & FUSE_BIG_WRITES)
1055 f->conn.capable |= FUSE_CAP_BIG_WRITES;
1056 } else {
1057 f->conn.async_read = 0;
1058 f->conn.max_readahead = 0;
1059 }
1060
1061 if (bufsize < FUSE_MIN_READ_BUFFER) {
1062 fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1063 bufsize);
1064 bufsize = FUSE_MIN_READ_BUFFER;
1065 }
1066
1067 bufsize -= 4096;
1068 if (bufsize < f->conn.max_write)
1069 f->conn.max_write = bufsize;
1070
1071 f->got_init = 1;
1072 if (f->op.init)
1073 f->op.init(f->userdata, &f->conn);
1074
1075 memset(&outarg, 0, sizeof(outarg));
1076 outarg.major = FUSE_KERNEL_VERSION;
1077 /*
1078 * if POSIXACLS is not set, protocol 7.8 provides a good
1079 * compatibility with older kernel modules.
1080 * if POSIXACLS is set, we try to use protocol 7.12 supposed
1081 * to have the ability to process the umask conditionnally,
1082 * but, when using an older kernel module, we fallback to 7.8
1083 */
1084#ifdef POSIXACLS
1085 if (arg->major > 7 || (arg->major == 7 && arg->minor >= 12))
1086 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1087 else
1088 outarg.minor = FUSE_KERNEL_MINOR_FALLBACK;
1089#else
1090 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
1091#endif
1092 if (f->conn.async_read)
1093 outarg.flags |= FUSE_ASYNC_READ;
1094 if (f->op.getlk && f->op.setlk)
1095 outarg.flags |= FUSE_POSIX_LOCKS;
1096#ifdef POSIXACLS
1097 if (f->conn.want & FUSE_CAP_DONT_MASK)
1098 outarg.flags |= FUSE_DONT_MASK;
1099#endif
1100 if (f->conn.want & FUSE_CAP_BIG_WRITES)
1101 outarg.flags |= FUSE_BIG_WRITES;
1102 outarg.max_readahead = f->conn.max_readahead;
1103 outarg.max_write = f->conn.max_write;
1104
1105 if (f->debug) {
1106 fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
1107 fprintf(stderr, " flags=0x%08x\n", outarg.flags);
1108 fprintf(stderr, " max_readahead=0x%08x\n", outarg.max_readahead);
1109 fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
1110 }
1111
1112 send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg));
1113}
1114
1115static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1116{
1117 struct fuse_ll *f = req->f;
1118
1119 (void) nodeid;
1120 (void) inarg;
1121
1122 f->got_destroy = 1;
1123 if (f->op.destroy)
1124 f->op.destroy(f->userdata);
1125
1126 send_reply_ok(req, NULL, 0);
1127}
1128
1129void *fuse_req_userdata(fuse_req_t req)
1130{
1131 return req->f->userdata;
1132}
1133
1134const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
1135{
1136 return &req->ctx;
1137}
1138
1139void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
1140 void *data)
1141{
1142 pthread_mutex_lock(&req->lock);
1143 req->u.ni.func = func;
1144 req->u.ni.data = data;
1145 if (req->interrupted && func)
1146 func(req, data);
1147 pthread_mutex_unlock(&req->lock);
1148}
1149
1150int fuse_req_interrupted(fuse_req_t req)
1151{
1152 int interrupted;
1153
1154 pthread_mutex_lock(&req->f->lock);
1155 interrupted = req->interrupted;
1156 pthread_mutex_unlock(&req->f->lock);
1157
1158 return interrupted;
1159}
1160
1161static struct {
1162 void (*func)(fuse_req_t, fuse_ino_t, const void *);
1163 const char *name;
1164} fuse_ll_ops[] = {
1165 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
1166 [FUSE_FORGET] = { do_forget, "FORGET" },
1167 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
1168 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
1169 [FUSE_READLINK] = { do_readlink, "READLINK" },
1170 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
1171 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
1172 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
1173 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
1174 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
1175 [FUSE_RENAME] = { do_rename, "RENAME" },
1176 [FUSE_LINK] = { do_link, "LINK" },
1177 [FUSE_OPEN] = { do_open, "OPEN" },
1178 [FUSE_READ] = { do_read, "READ" },
1179 [FUSE_WRITE] = { do_write, "WRITE" },
1180#if HAVE_SYS_STATVFS_H
1181 [FUSE_STATFS] = { do_statfs, "STATFS" },
1182#endif
1183 [FUSE_RELEASE] = { do_release, "RELEASE" },
1184 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
1185 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
1186 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
1187 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
1188 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
1189 [FUSE_FLUSH] = { do_flush, "FLUSH" },
1190 [FUSE_INIT] = { do_init, "INIT" },
1191 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
1192 [FUSE_READDIR] = { do_readdir, "READDIR" },
1193 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
1194 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
1195 [FUSE_GETLK] = { do_getlk, "GETLK" },
1196 [FUSE_SETLK] = { do_setlk, "SETLK" },
1197 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
1198 [FUSE_ACCESS] = { do_access, "ACCESS" },
1199 [FUSE_CREATE] = { do_create, "CREATE" },
1200 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
1201 [FUSE_BMAP] = { do_bmap, "BMAP" },
1202 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
1203};
1204
1205#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
1206
1207static const char *opname(enum fuse_opcode opcode)
1208{
1209 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
1210 return "???";
1211 else
1212 return fuse_ll_ops[opcode].name;
1213}
1214
1215static void fuse_ll_process(void *data, const char *buf, size_t len,
1216 struct fuse_chan *ch)
1217{
1218 struct fuse_ll *f = (struct fuse_ll *) data;
1219 const struct fuse_in_header *in = (const struct fuse_in_header *) buf;
1220 const void *inarg = buf + sizeof(struct fuse_in_header);
1221 struct fuse_req *req;
1222
1223 if (f->debug)
1224 fprintf(stderr, "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu\n",
1225 (unsigned long long) in->unique,
1226 opname((enum fuse_opcode) in->opcode), in->opcode,
1227 (unsigned long) in->nodeid, len);
1228
1229 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
1230 if (req == NULL) {
1231 fprintf(stderr, "fuse: failed to allocate request\n");
1232 return;
1233 }
1234
1235 req->f = f;
1236 req->unique = in->unique;
1237 req->ctx.uid = in->uid;
1238 req->ctx.gid = in->gid;
1239 req->ctx.pid = in->pid;
1240 req->ch = ch;
1241 req->ctr = 1;
1242 list_init_req(req);
1243 fuse_mutex_init(&req->lock);
1244
1245 if (!f->got_init && in->opcode != FUSE_INIT)
1246 fuse_reply_err(req, EIO);
1247 else if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
1248 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
1249 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
1250 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
1251 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) {
1252 fuse_reply_err(req, EACCES);
1253 } else if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
1254 fuse_reply_err(req, ENOSYS);
1255 else {
1256 if (in->opcode != FUSE_INTERRUPT) {
1257 struct fuse_req *intr;
1258 pthread_mutex_lock(&f->lock);
1259 intr = check_interrupt(f, req);
1260 list_add_req(req, &f->list);
1261 pthread_mutex_unlock(&f->lock);
1262 if (intr)
1263 fuse_reply_err(intr, EAGAIN);
1264 }
1265 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
1266 }
1267}
1268
1269enum {
1270 KEY_HELP,
1271 KEY_VERSION,
1272};
1273
1274static struct fuse_opt fuse_ll_opts[] = {
1275 { "debug", offsetof(struct fuse_ll, debug), 1 },
1276 { "-d", offsetof(struct fuse_ll, debug), 1 },
1277 { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
1278 { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
1279 { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
1280 { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
1281 { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
1282 FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
1283 FUSE_OPT_KEY("-h", KEY_HELP),
1284 FUSE_OPT_KEY("--help", KEY_HELP),
1285 FUSE_OPT_KEY("-V", KEY_VERSION),
1286 FUSE_OPT_KEY("--version", KEY_VERSION),
1287 FUSE_OPT_END
1288};
1289
1290static void fuse_ll_version(void)
1291{
1292 fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
1293 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1294}
1295
1296static void fuse_ll_help(void)
1297{
1298 fprintf(stderr,
1299" -o max_write=N set maximum size of write requests\n"
1300" -o max_readahead=N set maximum readahead\n"
1301" -o async_read perform reads asynchronously (default)\n"
1302" -o sync_read perform reads synchronously\n");
1303}
1304
1305static int fuse_ll_opt_proc(void *data, const char *arg, int key,
1306 struct fuse_args *outargs)
1307{
1308 (void) data; (void) outargs;
1309
1310 switch (key) {
1311 case KEY_HELP:
1312 fuse_ll_help();
1313 break;
1314
1315 case KEY_VERSION:
1316 fuse_ll_version();
1317 break;
1318
1319 default:
1320 fprintf(stderr, "fuse: unknown option `%s'\n", arg);
1321 }
1322
1323 return -1;
1324}
1325
1326#ifdef __SOLARIS__
1327
1328int fuse_lowlevel_is_lib_option(const char *opt)
1329{
1330 return fuse_opt_match(fuse_ll_opts, opt);
1331}
1332
1333#endif /* __SOLARIS__ */
1334
1335static void fuse_ll_destroy(void *data)
1336{
1337 struct fuse_ll *f = (struct fuse_ll *) data;
1338
1339 if (f->got_init && !f->got_destroy) {
1340 if (f->op.destroy)
1341 f->op.destroy(f->userdata);
1342 }
1343
1344 pthread_mutex_destroy(&f->lock);
1345 free(f);
1346}
1347
1348struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
1349 const struct fuse_lowlevel_ops *op,
1350 size_t op_size, void *userdata)
1351{
1352 struct fuse_ll *f;
1353 struct fuse_session *se;
1354 struct fuse_session_ops sop = {
1355 .process = fuse_ll_process,
1356 .destroy = fuse_ll_destroy,
1357 };
1358
1359 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
1360 fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
1361 op_size = sizeof(struct fuse_lowlevel_ops);
1362 }
1363
1364 f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
1365 if (f == NULL) {
1366 fprintf(stderr, "fuse: failed to allocate fuse object\n");
1367 goto out;
1368 }
1369
1370 f->conn.async_read = 1;
1371 f->conn.max_write = UINT_MAX;
1372 f->conn.max_readahead = UINT_MAX;
1373 list_init_req(&f->list);
1374 list_init_req(&f->interrupts);
1375 fuse_mutex_init(&f->lock);
1376
1377 if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
1378 goto out_free;
1379
1380 memcpy(&f->op, op, op_size);
1381 f->owner = getuid();
1382 f->userdata = userdata;
1383
1384 se = fuse_session_new(&sop, f);
1385 if (!se)
1386 goto out_free;
1387
1388 return se;
1389
1390 out_free:
1391 free(f);
1392 out:
1393 return NULL;
1394}
1395