1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 Implementation of (most of) the low-level FUSE API. The session loop
6 functions are implemented in separate files.
7
8 This program can be distributed under the terms of the GNU LGPLv2.
9 See the file COPYING.LIB
10 */
11
12 #define _GNU_SOURCE
13
14 #include "config.h"
15 #include "fuse_i.h"
16 #include "fuse_kernel.h"
17 #include "fuse_opt.h"
18 #include "fuse_misc.h"
19 #include "mount_util.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <sys/file.h>
30 #include <sys/ioctl.h>
31
32 #ifndef F_LINUX_SPECIFIC_BASE
33 #define F_LINUX_SPECIFIC_BASE 1024
34 #endif
35 #ifndef F_SETPIPE_SZ
36 #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
37 #endif
38
39
40 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
41 #define OFFSET_MAX 0x7fffffffffffffffLL
42
43 #define container_of(ptr, type, member) ({ \
44 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
45 (type *)( (char *)__mptr - offsetof(type,member) );})
46
47 struct fuse_pollhandle {
48 uint64_t kh;
49 struct fuse_session *se;
50 };
51
52 static size_t pagesize;
53
fuse_ll_init_pagesize(void)54 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
55 {
56 pagesize = getpagesize();
57 }
58
convert_stat(const struct stat * stbuf,struct fuse_attr * attr)59 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
60 {
61 attr->ino = stbuf->st_ino;
62 attr->mode = stbuf->st_mode;
63 attr->nlink = stbuf->st_nlink;
64 attr->uid = stbuf->st_uid;
65 attr->gid = stbuf->st_gid;
66 attr->rdev = stbuf->st_rdev;
67 attr->size = stbuf->st_size;
68 attr->blksize = stbuf->st_blksize;
69 attr->blocks = stbuf->st_blocks;
70 attr->atime = stbuf->st_atime;
71 attr->mtime = stbuf->st_mtime;
72 attr->ctime = stbuf->st_ctime;
73 attr->atimensec = ST_ATIM_NSEC(stbuf);
74 attr->mtimensec = ST_MTIM_NSEC(stbuf);
75 attr->ctimensec = ST_CTIM_NSEC(stbuf);
76 }
77
convert_attr(const struct fuse_setattr_in * attr,struct stat * stbuf)78 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
79 {
80 stbuf->st_mode = attr->mode;
81 stbuf->st_uid = attr->uid;
82 stbuf->st_gid = attr->gid;
83 stbuf->st_size = attr->size;
84 stbuf->st_atime = attr->atime;
85 stbuf->st_mtime = attr->mtime;
86 stbuf->st_ctime = attr->ctime;
87 ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
88 ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
89 ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
90 }
91
iov_length(const struct iovec * iov,size_t count)92 static size_t iov_length(const struct iovec *iov, size_t count)
93 {
94 size_t seg;
95 size_t ret = 0;
96
97 for (seg = 0; seg < count; seg++)
98 ret += iov[seg].iov_len;
99 return ret;
100 }
101
list_init_req(struct fuse_req * req)102 static void list_init_req(struct fuse_req *req)
103 {
104 req->next = req;
105 req->prev = req;
106 }
107
list_del_req(struct fuse_req * req)108 static void list_del_req(struct fuse_req *req)
109 {
110 struct fuse_req *prev = req->prev;
111 struct fuse_req *next = req->next;
112 prev->next = next;
113 next->prev = prev;
114 }
115
list_add_req(struct fuse_req * req,struct fuse_req * next)116 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
117 {
118 struct fuse_req *prev = next->prev;
119 req->next = next;
120 req->prev = prev;
121 prev->next = req;
122 next->prev = req;
123 }
124
destroy_req(fuse_req_t req)125 static void destroy_req(fuse_req_t req)
126 {
127 pthread_mutex_destroy(&req->lock);
128 free(req);
129 }
130
fuse_free_req(fuse_req_t req)131 void fuse_free_req(fuse_req_t req)
132 {
133 int ctr;
134 struct fuse_session *se = req->se;
135
136 pthread_mutex_lock(&se->lock);
137 req->u.ni.func = NULL;
138 req->u.ni.data = NULL;
139 list_del_req(req);
140 ctr = --req->ctr;
141 fuse_chan_put(req->ch);
142 req->ch = NULL;
143 pthread_mutex_unlock(&se->lock);
144 if (!ctr)
145 destroy_req(req);
146 }
147
fuse_ll_alloc_req(struct fuse_session * se)148 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
149 {
150 struct fuse_req *req;
151
152 req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
153 if (req == NULL) {
154 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
155 } else {
156 req->se = se;
157 req->ctr = 1;
158 list_init_req(req);
159 pthread_mutex_init(&req->lock, NULL);
160 }
161
162 return req;
163 }
164
165 /* Send data. If *ch* is NULL, send via session master fd */
fuse_send_msg(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int count)166 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
167 struct iovec *iov, int count)
168 {
169 struct fuse_out_header *out = iov[0].iov_base;
170
171 assert(se != NULL);
172 out->len = iov_length(iov, count);
173 if (se->debug) {
174 if (out->unique == 0) {
175 fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n",
176 out->error, out->len);
177 } else if (out->error) {
178 fuse_log(FUSE_LOG_DEBUG,
179 " unique: %llu, error: %i (%s), outsize: %i\n",
180 (unsigned long long) out->unique, out->error,
181 strerror(-out->error), out->len);
182 } else {
183 fuse_log(FUSE_LOG_DEBUG,
184 " unique: %llu, success, outsize: %i\n",
185 (unsigned long long) out->unique, out->len);
186 }
187 }
188
189 ssize_t res = writev(ch ? ch->fd : se->fd,
190 iov, count);
191 int err = errno;
192
193 if (res == -1) {
194 /* ENOENT means the operation was interrupted */
195 if (!fuse_session_exited(se) && err != ENOENT)
196 perror("fuse: writing device");
197 return -err;
198 }
199
200 return 0;
201 }
202
203
fuse_send_reply_iov_nofree(fuse_req_t req,int error,struct iovec * iov,int count)204 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
205 int count)
206 {
207 struct fuse_out_header out;
208
209 if (error <= -1000 || error > 0) {
210 fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error);
211 error = -ERANGE;
212 }
213
214 out.unique = req->unique;
215 out.error = error;
216
217 iov[0].iov_base = &out;
218 iov[0].iov_len = sizeof(struct fuse_out_header);
219
220 return fuse_send_msg(req->se, req->ch, iov, count);
221 }
222
send_reply_iov(fuse_req_t req,int error,struct iovec * iov,int count)223 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
224 int count)
225 {
226 int res;
227
228 res = fuse_send_reply_iov_nofree(req, error, iov, count);
229 fuse_free_req(req);
230 return res;
231 }
232
send_reply(fuse_req_t req,int error,const void * arg,size_t argsize)233 static int send_reply(fuse_req_t req, int error, const void *arg,
234 size_t argsize)
235 {
236 struct iovec iov[2];
237 int count = 1;
238 if (argsize) {
239 iov[1].iov_base = (void *) arg;
240 iov[1].iov_len = argsize;
241 count++;
242 }
243 return send_reply_iov(req, error, iov, count);
244 }
245
fuse_reply_iov(fuse_req_t req,const struct iovec * iov,int count)246 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
247 {
248 int res;
249 struct iovec *padded_iov;
250
251 padded_iov = malloc((count + 1) * sizeof(struct iovec));
252 if (padded_iov == NULL)
253 return fuse_reply_err(req, ENOMEM);
254
255 memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
256 count++;
257
258 res = send_reply_iov(req, 0, padded_iov, count);
259 free(padded_iov);
260
261 return res;
262 }
263
264
265 /* `buf` is allowed to be empty so that the proper size may be
266 allocated by the caller */
fuse_add_direntry(fuse_req_t req,char * buf,size_t bufsize,const char * name,const struct stat * stbuf,off_t off)267 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
268 const char *name, const struct stat *stbuf, off_t off)
269 {
270 (void)req;
271 size_t namelen;
272 size_t entlen;
273 size_t entlen_padded;
274 struct fuse_dirent *dirent;
275
276 namelen = strlen(name);
277 entlen = FUSE_NAME_OFFSET + namelen;
278 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
279
280 if ((buf == NULL) || (entlen_padded > bufsize))
281 return entlen_padded;
282
283 dirent = (struct fuse_dirent*) buf;
284 dirent->ino = stbuf->st_ino;
285 dirent->off = off;
286 dirent->namelen = namelen;
287 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
288 memcpy(dirent->name, name, namelen);
289 memset(dirent->name + namelen, 0, entlen_padded - entlen);
290
291 return entlen_padded;
292 }
293
convert_statfs(const struct statvfs * stbuf,struct fuse_kstatfs * kstatfs)294 static void convert_statfs(const struct statvfs *stbuf,
295 struct fuse_kstatfs *kstatfs)
296 {
297 kstatfs->bsize = stbuf->f_bsize;
298 kstatfs->frsize = stbuf->f_frsize;
299 kstatfs->blocks = stbuf->f_blocks;
300 kstatfs->bfree = stbuf->f_bfree;
301 kstatfs->bavail = stbuf->f_bavail;
302 kstatfs->files = stbuf->f_files;
303 kstatfs->ffree = stbuf->f_ffree;
304 kstatfs->namelen = stbuf->f_namemax;
305 }
306
send_reply_ok(fuse_req_t req,const void * arg,size_t argsize)307 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
308 {
309 return send_reply(req, 0, arg, argsize);
310 }
311
fuse_reply_err(fuse_req_t req,int err)312 int fuse_reply_err(fuse_req_t req, int err)
313 {
314 return send_reply(req, -err, NULL, 0);
315 }
316
fuse_reply_none(fuse_req_t req)317 void fuse_reply_none(fuse_req_t req)
318 {
319 fuse_free_req(req);
320 }
321
calc_timeout_sec(double t)322 static unsigned long calc_timeout_sec(double t)
323 {
324 if (t > (double) ULONG_MAX)
325 return ULONG_MAX;
326 else if (t < 0.0)
327 return 0;
328 else
329 return (unsigned long) t;
330 }
331
calc_timeout_nsec(double t)332 static unsigned int calc_timeout_nsec(double t)
333 {
334 double f = t - (double) calc_timeout_sec(t);
335 if (f < 0.0)
336 return 0;
337 else if (f >= 0.999999999)
338 return 999999999;
339 else
340 return (unsigned int) (f * 1.0e9);
341 }
342
fill_entry(struct fuse_entry_out * arg,const struct fuse_entry_param * e)343 static void fill_entry(struct fuse_entry_out *arg,
344 const struct fuse_entry_param *e)
345 {
346 arg->nodeid = e->ino;
347 arg->generation = e->generation;
348 arg->entry_valid = calc_timeout_sec(e->entry_timeout);
349 arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
350 arg->attr_valid = calc_timeout_sec(e->attr_timeout);
351 arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
352 convert_stat(&e->attr, &arg->attr);
353 }
354
355 /* `buf` is allowed to be empty so that the proper size may be
356 allocated by the caller */
fuse_add_direntry_plus(fuse_req_t req,char * buf,size_t bufsize,const char * name,const struct fuse_entry_param * e,off_t off)357 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
358 const char *name,
359 const struct fuse_entry_param *e, off_t off)
360 {
361 (void)req;
362 size_t namelen;
363 size_t entlen;
364 size_t entlen_padded;
365
366 namelen = strlen(name);
367 entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
368 entlen_padded = FUSE_DIRENT_ALIGN(entlen);
369 if ((buf == NULL) || (entlen_padded > bufsize))
370 return entlen_padded;
371
372 struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
373 memset(&dp->entry_out, 0, sizeof(dp->entry_out));
374 fill_entry(&dp->entry_out, e);
375
376 struct fuse_dirent *dirent = &dp->dirent;
377 dirent->ino = e->attr.st_ino;
378 dirent->off = off;
379 dirent->namelen = namelen;
380 dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
381 memcpy(dirent->name, name, namelen);
382 memset(dirent->name + namelen, 0, entlen_padded - entlen);
383
384 return entlen_padded;
385 }
386
fill_open(struct fuse_open_out * arg,const struct fuse_file_info * f)387 static void fill_open(struct fuse_open_out *arg,
388 const struct fuse_file_info *f)
389 {
390 arg->fh = f->fh;
391 arg->passthrough_fh = f->passthrough_fh;
392 if (f->direct_io)
393 arg->open_flags |= FOPEN_DIRECT_IO;
394 if (f->keep_cache)
395 arg->open_flags |= FOPEN_KEEP_CACHE;
396 if (f->cache_readdir)
397 arg->open_flags |= FOPEN_CACHE_DIR;
398 if (f->nonseekable)
399 arg->open_flags |= FOPEN_NONSEEKABLE;
400 }
401
fuse_reply_entry(fuse_req_t req,const struct fuse_entry_param * e)402 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param* e) {
403 struct {
404 struct fuse_entry_out arg;
405 struct fuse_entry_bpf_out bpf_arg;
406 } __attribute__((packed)) arg_ext = {0};
407
408 struct fuse_entry_out arg;
409 struct fuse_entry_bpf_out bpf_arg;
410 size_t size;
411 int extended_args = e->bpf_action || bpf_arg.bpf_fd || e->backing_action || e->backing_fd;
412
413 if (extended_args) {
414 size = req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg_ext);
415 } else {
416 size = req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
417 }
418
419 /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
420 negative entry */
421 if (!e->ino && req->se->conn.proto_minor < 4) return fuse_reply_err(req, ENOENT);
422
423 memset(&arg, 0, sizeof(arg));
424 fill_entry(&arg, e);
425
426 if (extended_args) {
427 memset(&bpf_arg, 0, sizeof(bpf_arg));
428
429 bpf_arg.bpf_action = e->bpf_action;
430 bpf_arg.bpf_fd = e->bpf_fd;
431 bpf_arg.backing_action = e->backing_action;
432 bpf_arg.backing_fd = e->backing_fd;
433
434 arg_ext.arg = arg;
435 arg_ext.bpf_arg = bpf_arg;
436
437 return send_reply_ok(req, &arg_ext, size);
438 } else {
439 return send_reply_ok(req, &arg, size);
440 }
441 }
442
fuse_reply_create(fuse_req_t req,const struct fuse_entry_param * e,const struct fuse_file_info * f)443 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
444 const struct fuse_file_info *f)
445 {
446 char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
447 size_t entrysize = req->se->conn.proto_minor < 9 ?
448 FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
449 struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
450 struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
451
452 memset(buf, 0, sizeof(buf));
453 fill_entry(earg, e);
454 fill_open(oarg, f);
455 return send_reply_ok(req, buf,
456 entrysize + sizeof(struct fuse_open_out));
457 }
458
fuse_reply_attr(fuse_req_t req,const struct stat * attr,double attr_timeout)459 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
460 double attr_timeout)
461 {
462 struct fuse_attr_out arg;
463 size_t size = req->se->conn.proto_minor < 9 ?
464 FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
465
466 memset(&arg, 0, sizeof(arg));
467 arg.attr_valid = calc_timeout_sec(attr_timeout);
468 arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
469 convert_stat(attr, &arg.attr);
470
471 return send_reply_ok(req, &arg, size);
472 }
473
fuse_reply_readlink(fuse_req_t req,const char * linkname)474 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
475 {
476 return send_reply_ok(req, linkname, strlen(linkname));
477 }
478
fuse_reply_canonical_path(fuse_req_t req,const char * path)479 int fuse_reply_canonical_path(fuse_req_t req, const char *path)
480 {
481 // The kernel expects a buffer containing the null terminator for this op
482 // So we add the null terminator size to strlen
483 return send_reply_ok(req, path, strlen(path) + 1);
484 }
485
486 enum {
487 FUSE_PASSTHROUGH_API_UNAVAILABLE,
488 FUSE_PASSTHROUGH_API_V0,
489 FUSE_PASSTHROUGH_API_V1,
490 FUSE_PASSTHROUGH_API_V2,
491 FUSE_PASSTHROUGH_API_STABLE,
492 };
493
494 /*
495 * Requests the FUSE passthrough feature to be enabled on a specific file
496 * through the passed fd.
497 * This function returns an identifier that must be used as passthrough_fh
498 * when the open/create_open request reply is sent back to /dev/fuse.
499 * As for the current FUSE passthrough implementation, passthrough_fh values
500 * are only valid if > 0, so in case the FUSE passthrough open ioctl returns
501 * a value <= 0, this must be considered an error and is returned as-is by
502 * this function.
503 */
fuse_passthrough_enable(fuse_req_t req,unsigned int fd)504 int fuse_passthrough_enable(fuse_req_t req, unsigned int fd) {
505 static sig_atomic_t passthrough_version = FUSE_PASSTHROUGH_API_STABLE;
506 int ret = 0; /* values <= 0 represent errors in FUSE passthrough */
507
508 /*
509 * The interface of FUSE passthrough is still unstable in the kernel,
510 * so the following solution is to search for the most updated API
511 * version and, if not found, fall back to an older one.
512 * This happens when ioctl() returns -1 and errno is set to ENOTTY,
513 * an error code that corresponds to the lack of a specific ioctl.
514 */
515 switch (passthrough_version) {
516 case FUSE_PASSTHROUGH_API_STABLE:
517 /* There is not a stable API yet */
518 passthrough_version = FUSE_PASSTHROUGH_API_V2;
519 case FUSE_PASSTHROUGH_API_V2: {
520 ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V2, &fd);
521 if (ret == -1 && errno == ENOTTY)
522 passthrough_version = FUSE_PASSTHROUGH_API_V1;
523 else
524 break;
525 }
526 case FUSE_PASSTHROUGH_API_V1: {
527 struct fuse_passthrough_out_v0 out = {};
528 out.fd = fd;
529
530 ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V1, &out);
531 if (ret == -1 && errno == ENOTTY)
532 passthrough_version = FUSE_PASSTHROUGH_API_V0;
533 else
534 break;
535 }
536 case FUSE_PASSTHROUGH_API_V0: {
537 struct fuse_passthrough_out_v0 out = {};
538 out.fd = fd;
539
540 ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V0, &out);
541 if (ret == -1 && errno == ENOTTY)
542 passthrough_version = FUSE_PASSTHROUGH_API_UNAVAILABLE;
543 else
544 break;
545 }
546 default:
547 fuse_log(FUSE_LOG_ERR, "fuse: passthrough_enable no valid API\n");
548 return -ENOTTY;
549 }
550
551 if (ret <= 0)
552 fuse_log(FUSE_LOG_ERR, "fuse: passthrough_enable: %s\n", strerror(errno));
553
554 return ret;
555 }
556
fuse_reply_open(fuse_req_t req,const struct fuse_file_info * f)557 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
558 {
559 struct fuse_open_out arg;
560
561 memset(&arg, 0, sizeof(arg));
562 fill_open(&arg, f);
563 return send_reply_ok(req, &arg, sizeof(arg));
564 }
565
fuse_reply_write(fuse_req_t req,size_t count)566 int fuse_reply_write(fuse_req_t req, size_t count)
567 {
568 struct fuse_write_out arg;
569
570 memset(&arg, 0, sizeof(arg));
571 arg.size = count;
572
573 return send_reply_ok(req, &arg, sizeof(arg));
574 }
575
fuse_reply_buf(fuse_req_t req,const char * buf,size_t size)576 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
577 {
578 return send_reply_ok(req, buf, size);
579 }
580
fuse_send_data_iov_fallback(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,size_t len)581 static int fuse_send_data_iov_fallback(struct fuse_session *se,
582 struct fuse_chan *ch,
583 struct iovec *iov, int iov_count,
584 struct fuse_bufvec *buf,
585 size_t len)
586 {
587 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
588 void *mbuf;
589 int res;
590
591 /* Optimize common case */
592 if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
593 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
594 /* FIXME: also avoid memory copy if there are multiple buffers
595 but none of them contain an fd */
596
597 iov[iov_count].iov_base = buf->buf[0].mem;
598 iov[iov_count].iov_len = len;
599 iov_count++;
600 return fuse_send_msg(se, ch, iov, iov_count);
601 }
602
603 res = posix_memalign(&mbuf, pagesize, len);
604 if (res != 0)
605 return res;
606
607 mem_buf.buf[0].mem = mbuf;
608 res = fuse_buf_copy(&mem_buf, buf, 0);
609 if (res < 0) {
610 free(mbuf);
611 return -res;
612 }
613 len = res;
614
615 iov[iov_count].iov_base = mbuf;
616 iov[iov_count].iov_len = len;
617 iov_count++;
618 res = fuse_send_msg(se, ch, iov, iov_count);
619 free(mbuf);
620
621 return res;
622 }
623
624 struct fuse_ll_pipe {
625 size_t size;
626 int can_grow;
627 int pipe[2];
628 };
629
fuse_ll_pipe_free(struct fuse_ll_pipe * llp)630 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
631 {
632 close(llp->pipe[0]);
633 close(llp->pipe[1]);
634 free(llp);
635 }
636
637 #ifdef HAVE_SPLICE
638 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
fuse_pipe(int fds[2])639 static int fuse_pipe(int fds[2])
640 {
641 int rv = pipe(fds);
642
643 if (rv == -1)
644 return rv;
645
646 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
647 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
648 fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
649 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
650 close(fds[0]);
651 close(fds[1]);
652 rv = -1;
653 }
654 return rv;
655 }
656 #else
fuse_pipe(int fds[2])657 static int fuse_pipe(int fds[2])
658 {
659 return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
660 }
661 #endif
662
fuse_ll_get_pipe(struct fuse_session * se)663 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_session *se)
664 {
665 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
666 if (llp == NULL) {
667 int res;
668
669 llp = malloc(sizeof(struct fuse_ll_pipe));
670 if (llp == NULL)
671 return NULL;
672
673 res = fuse_pipe(llp->pipe);
674 if (res == -1) {
675 free(llp);
676 return NULL;
677 }
678
679 /*
680 *the default size is 16 pages on linux
681 */
682 llp->size = pagesize * 16;
683 llp->can_grow = 1;
684
685 pthread_setspecific(se->pipe_key, llp);
686 }
687
688 return llp;
689 }
690 #endif
691
fuse_ll_clear_pipe(struct fuse_session * se)692 static void fuse_ll_clear_pipe(struct fuse_session *se)
693 {
694 struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
695 if (llp) {
696 pthread_setspecific(se->pipe_key, NULL);
697 fuse_ll_pipe_free(llp);
698 }
699 }
700
701 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
read_back(int fd,char * buf,size_t len)702 static int read_back(int fd, char *buf, size_t len)
703 {
704 int res;
705
706 res = read(fd, buf, len);
707 if (res == -1) {
708 fuse_log(FUSE_LOG_ERR, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
709 return -EIO;
710 }
711 if (res != len) {
712 fuse_log(FUSE_LOG_ERR, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
713 return -EIO;
714 }
715 return 0;
716 }
717
grow_pipe_to_max(int pipefd)718 static int grow_pipe_to_max(int pipefd)
719 {
720 int max;
721 int res;
722 int maxfd;
723 char buf[32];
724
725 maxfd = open("/proc/sys/fs/pipe-max-size", O_RDONLY);
726 if (maxfd < 0)
727 return -errno;
728
729 res = read(maxfd, buf, sizeof(buf) - 1);
730 if (res < 0) {
731 int saved_errno;
732
733 saved_errno = errno;
734 close(maxfd);
735 return -saved_errno;
736 }
737 close(maxfd);
738 buf[res] = '\0';
739
740 max = atoi(buf);
741 res = fcntl(pipefd, F_SETPIPE_SZ, max);
742 if (res < 0)
743 return -errno;
744 return max;
745 }
746
fuse_send_data_iov(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,unsigned int flags)747 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
748 struct iovec *iov, int iov_count,
749 struct fuse_bufvec *buf, unsigned int flags)
750 {
751 int res;
752 size_t len = fuse_buf_size(buf);
753 struct fuse_out_header *out = iov[0].iov_base;
754 struct fuse_ll_pipe *llp;
755 int splice_flags;
756 size_t pipesize;
757 size_t total_buf_size;
758 size_t idx;
759 size_t headerlen;
760 struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
761
762 if (se->broken_splice_nonblock)
763 goto fallback;
764
765 if (flags & FUSE_BUF_NO_SPLICE)
766 goto fallback;
767
768 total_buf_size = 0;
769 for (idx = buf->idx; idx < buf->count; idx++) {
770 total_buf_size += buf->buf[idx].size;
771 if (idx == buf->idx)
772 total_buf_size -= buf->off;
773 }
774 if (total_buf_size < 2 * pagesize)
775 goto fallback;
776
777 if (se->conn.proto_minor < 14 ||
778 !(se->conn.want & FUSE_CAP_SPLICE_WRITE))
779 goto fallback;
780
781 llp = fuse_ll_get_pipe(se);
782 if (llp == NULL)
783 goto fallback;
784
785
786 headerlen = iov_length(iov, iov_count);
787
788 out->len = headerlen + len;
789
790 /*
791 * Heuristic for the required pipe size, does not work if the
792 * source contains less than page size fragments
793 */
794 pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
795
796 if (llp->size < pipesize) {
797 if (llp->can_grow) {
798 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
799 if (res == -1) {
800 res = grow_pipe_to_max(llp->pipe[0]);
801 if (res > 0)
802 llp->size = res;
803 llp->can_grow = 0;
804 goto fallback;
805 }
806 llp->size = res;
807 }
808 if (llp->size < pipesize)
809 goto fallback;
810 }
811
812
813 res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
814 if (res == -1)
815 goto fallback;
816
817 if (res != headerlen) {
818 res = -EIO;
819 fuse_log(FUSE_LOG_ERR, "fuse: short vmsplice to pipe: %u/%zu\n", res,
820 headerlen);
821 goto clear_pipe;
822 }
823
824 pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
825 pipe_buf.buf[0].fd = llp->pipe[1];
826
827 res = fuse_buf_copy(&pipe_buf, buf,
828 FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
829 if (res < 0) {
830 if (res == -EAGAIN || res == -EINVAL) {
831 /*
832 * Should only get EAGAIN on kernels with
833 * broken SPLICE_F_NONBLOCK support (<=
834 * 2.6.35) where this error or a short read is
835 * returned even if the pipe itself is not
836 * full
837 *
838 * EINVAL might mean that splice can't handle
839 * this combination of input and output.
840 */
841 if (res == -EAGAIN)
842 se->broken_splice_nonblock = 1;
843
844 pthread_setspecific(se->pipe_key, NULL);
845 fuse_ll_pipe_free(llp);
846 goto fallback;
847 }
848 res = -res;
849 goto clear_pipe;
850 }
851
852 if (res != 0 && res < len) {
853 struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
854 void *mbuf;
855 size_t now_len = res;
856 /*
857 * For regular files a short count is either
858 * 1) due to EOF, or
859 * 2) because of broken SPLICE_F_NONBLOCK (see above)
860 *
861 * For other inputs it's possible that we overflowed
862 * the pipe because of small buffer fragments.
863 */
864
865 res = posix_memalign(&mbuf, pagesize, len);
866 if (res != 0)
867 goto clear_pipe;
868
869 mem_buf.buf[0].mem = mbuf;
870 mem_buf.off = now_len;
871 res = fuse_buf_copy(&mem_buf, buf, 0);
872 if (res > 0) {
873 char *tmpbuf;
874 size_t extra_len = res;
875 /*
876 * Trickiest case: got more data. Need to get
877 * back the data from the pipe and then fall
878 * back to regular write.
879 */
880 tmpbuf = malloc(headerlen);
881 if (tmpbuf == NULL) {
882 free(mbuf);
883 res = ENOMEM;
884 goto clear_pipe;
885 }
886 res = read_back(llp->pipe[0], tmpbuf, headerlen);
887 free(tmpbuf);
888 if (res != 0) {
889 free(mbuf);
890 goto clear_pipe;
891 }
892 res = read_back(llp->pipe[0], mbuf, now_len);
893 if (res != 0) {
894 free(mbuf);
895 goto clear_pipe;
896 }
897 len = now_len + extra_len;
898 iov[iov_count].iov_base = mbuf;
899 iov[iov_count].iov_len = len;
900 iov_count++;
901 res = fuse_send_msg(se, ch, iov, iov_count);
902 free(mbuf);
903 return res;
904 }
905 free(mbuf);
906 res = now_len;
907 }
908 len = res;
909 out->len = headerlen + len;
910
911 if (se->debug) {
912 fuse_log(FUSE_LOG_DEBUG,
913 " unique: %llu, success, outsize: %i (splice)\n",
914 (unsigned long long) out->unique, out->len);
915 }
916
917 splice_flags = 0;
918 if ((flags & FUSE_BUF_SPLICE_MOVE) &&
919 (se->conn.want & FUSE_CAP_SPLICE_MOVE))
920 splice_flags |= SPLICE_F_MOVE;
921
922 res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
923 NULL, out->len, splice_flags);
924 if (res == -1) {
925 res = -errno;
926 perror("fuse: splice from pipe");
927 goto clear_pipe;
928 }
929 if (res != out->len) {
930 res = -EIO;
931 fuse_log(FUSE_LOG_ERR, "fuse: short splice from pipe: %u/%u\n",
932 res, out->len);
933 goto clear_pipe;
934 }
935 return 0;
936
937 clear_pipe:
938 fuse_ll_clear_pipe(se);
939 return res;
940
941 fallback:
942 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
943 }
944 #else
fuse_send_data_iov(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,unsigned int flags)945 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
946 struct iovec *iov, int iov_count,
947 struct fuse_bufvec *buf, unsigned int flags)
948 {
949 size_t len = fuse_buf_size(buf);
950 (void) flags;
951
952 return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
953 }
954 #endif
955
fuse_reply_data(fuse_req_t req,struct fuse_bufvec * bufv,enum fuse_buf_copy_flags flags)956 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
957 enum fuse_buf_copy_flags flags)
958 {
959 struct iovec iov[2];
960 struct fuse_out_header out;
961 int res;
962
963 iov[0].iov_base = &out;
964 iov[0].iov_len = sizeof(struct fuse_out_header);
965
966 out.unique = req->unique;
967 out.error = 0;
968
969 res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
970 if (res <= 0) {
971 fuse_free_req(req);
972 return res;
973 } else {
974 return fuse_reply_err(req, res);
975 }
976 }
977
fuse_reply_statfs(fuse_req_t req,const struct statvfs * stbuf)978 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
979 {
980 struct fuse_statfs_out arg;
981 size_t size = req->se->conn.proto_minor < 4 ?
982 FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
983
984 memset(&arg, 0, sizeof(arg));
985 convert_statfs(stbuf, &arg.st);
986
987 return send_reply_ok(req, &arg, size);
988 }
989
fuse_reply_xattr(fuse_req_t req,size_t count)990 int fuse_reply_xattr(fuse_req_t req, size_t count)
991 {
992 struct fuse_getxattr_out arg;
993
994 memset(&arg, 0, sizeof(arg));
995 arg.size = count;
996
997 return send_reply_ok(req, &arg, sizeof(arg));
998 }
999
fuse_reply_lock(fuse_req_t req,const struct flock * lock)1000 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
1001 {
1002 struct fuse_lk_out arg;
1003
1004 memset(&arg, 0, sizeof(arg));
1005 arg.lk.type = lock->l_type;
1006 if (lock->l_type != F_UNLCK) {
1007 arg.lk.start = lock->l_start;
1008 if (lock->l_len == 0)
1009 arg.lk.end = OFFSET_MAX;
1010 else
1011 arg.lk.end = lock->l_start + lock->l_len - 1;
1012 }
1013 arg.lk.pid = lock->l_pid;
1014 return send_reply_ok(req, &arg, sizeof(arg));
1015 }
1016
fuse_reply_bmap(fuse_req_t req,uint64_t idx)1017 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
1018 {
1019 struct fuse_bmap_out arg;
1020
1021 memset(&arg, 0, sizeof(arg));
1022 arg.block = idx;
1023
1024 return send_reply_ok(req, &arg, sizeof(arg));
1025 }
1026
fuse_ioctl_iovec_copy(const struct iovec * iov,size_t count)1027 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
1028 size_t count)
1029 {
1030 struct fuse_ioctl_iovec *fiov;
1031 size_t i;
1032
1033 fiov = malloc(sizeof(fiov[0]) * count);
1034 if (!fiov)
1035 return NULL;
1036
1037 for (i = 0; i < count; i++) {
1038 fiov[i].base = (uintptr_t) iov[i].iov_base;
1039 fiov[i].len = iov[i].iov_len;
1040 }
1041
1042 return fiov;
1043 }
1044
fuse_reply_ioctl_retry(fuse_req_t req,const struct iovec * in_iov,size_t in_count,const struct iovec * out_iov,size_t out_count)1045 int fuse_reply_ioctl_retry(fuse_req_t req,
1046 const struct iovec *in_iov, size_t in_count,
1047 const struct iovec *out_iov, size_t out_count)
1048 {
1049 struct fuse_ioctl_out arg;
1050 struct fuse_ioctl_iovec *in_fiov = NULL;
1051 struct fuse_ioctl_iovec *out_fiov = NULL;
1052 struct iovec iov[4];
1053 size_t count = 1;
1054 int res;
1055
1056 memset(&arg, 0, sizeof(arg));
1057 arg.flags |= FUSE_IOCTL_RETRY;
1058 arg.in_iovs = in_count;
1059 arg.out_iovs = out_count;
1060 iov[count].iov_base = &arg;
1061 iov[count].iov_len = sizeof(arg);
1062 count++;
1063
1064 if (req->se->conn.proto_minor < 16) {
1065 if (in_count) {
1066 iov[count].iov_base = (void *)in_iov;
1067 iov[count].iov_len = sizeof(in_iov[0]) * in_count;
1068 count++;
1069 }
1070
1071 if (out_count) {
1072 iov[count].iov_base = (void *)out_iov;
1073 iov[count].iov_len = sizeof(out_iov[0]) * out_count;
1074 count++;
1075 }
1076 } else {
1077 /* Can't handle non-compat 64bit ioctls on 32bit */
1078 if (sizeof(void *) == 4 && req->ioctl_64bit) {
1079 res = fuse_reply_err(req, EINVAL);
1080 goto out;
1081 }
1082
1083 if (in_count) {
1084 in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
1085 if (!in_fiov)
1086 goto enomem;
1087
1088 iov[count].iov_base = (void *)in_fiov;
1089 iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
1090 count++;
1091 }
1092 if (out_count) {
1093 out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1094 if (!out_fiov)
1095 goto enomem;
1096
1097 iov[count].iov_base = (void *)out_fiov;
1098 iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1099 count++;
1100 }
1101 }
1102
1103 res = send_reply_iov(req, 0, iov, count);
1104 out:
1105 free(in_fiov);
1106 free(out_fiov);
1107
1108 return res;
1109
1110 enomem:
1111 res = fuse_reply_err(req, ENOMEM);
1112 goto out;
1113 }
1114
fuse_reply_ioctl(fuse_req_t req,int result,const void * buf,size_t size)1115 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1116 {
1117 struct fuse_ioctl_out arg;
1118 struct iovec iov[3];
1119 size_t count = 1;
1120
1121 memset(&arg, 0, sizeof(arg));
1122 arg.result = result;
1123 iov[count].iov_base = &arg;
1124 iov[count].iov_len = sizeof(arg);
1125 count++;
1126
1127 if (size) {
1128 iov[count].iov_base = (char *) buf;
1129 iov[count].iov_len = size;
1130 count++;
1131 }
1132
1133 return send_reply_iov(req, 0, iov, count);
1134 }
1135
fuse_reply_ioctl_iov(fuse_req_t req,int result,const struct iovec * iov,int count)1136 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1137 int count)
1138 {
1139 struct iovec *padded_iov;
1140 struct fuse_ioctl_out arg;
1141 int res;
1142
1143 padded_iov = malloc((count + 2) * sizeof(struct iovec));
1144 if (padded_iov == NULL)
1145 return fuse_reply_err(req, ENOMEM);
1146
1147 memset(&arg, 0, sizeof(arg));
1148 arg.result = result;
1149 padded_iov[1].iov_base = &arg;
1150 padded_iov[1].iov_len = sizeof(arg);
1151
1152 memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1153
1154 res = send_reply_iov(req, 0, padded_iov, count + 2);
1155 free(padded_iov);
1156
1157 return res;
1158 }
1159
fuse_reply_poll(fuse_req_t req,unsigned revents)1160 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1161 {
1162 struct fuse_poll_out arg;
1163
1164 memset(&arg, 0, sizeof(arg));
1165 arg.revents = revents;
1166
1167 return send_reply_ok(req, &arg, sizeof(arg));
1168 }
1169
fuse_reply_lseek(fuse_req_t req,off_t off)1170 int fuse_reply_lseek(fuse_req_t req, off_t off)
1171 {
1172 struct fuse_lseek_out arg;
1173
1174 memset(&arg, 0, sizeof(arg));
1175 arg.offset = off;
1176
1177 return send_reply_ok(req, &arg, sizeof(arg));
1178 }
1179
do_lookup(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1180 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1181 {
1182 char *name = (char *) inarg;
1183
1184 if (req->se->op.lookup)
1185 req->se->op.lookup(req, nodeid, name);
1186 else
1187 fuse_reply_err(req, ENOSYS);
1188 }
1189
do_lookup_postfilter(fuse_req_t req,fuse_ino_t nodeid,uint32_t error_in,const void * inarg,size_t size)1190 static void do_lookup_postfilter(fuse_req_t req, fuse_ino_t nodeid, uint32_t error_in,
1191 const void *inarg, size_t size)
1192 {
1193 if (req->se->op.lookup_postfilter) {
1194 char *name = (char *) inarg;
1195 size_t namelen = strlen(name);
1196
1197 if (size != namelen + 1 + sizeof(struct fuse_entry_out)
1198 + sizeof(struct fuse_entry_bpf_out)) {
1199 fuse_log(FUSE_LOG_ERR, "%s: Bad size", __func__);
1200 fuse_reply_err(req, EIO);
1201 } else {
1202 struct fuse_entry_out *feo = (void *) (name + namelen + 1);
1203 struct fuse_entry_bpf_out *febo = (char *) feo + sizeof(*feo);
1204
1205 req->se->op.lookup_postfilter(req, nodeid, error_in, name, feo,
1206 febo);
1207 }
1208 } else
1209 fuse_reply_err(req, ENOSYS);
1210 }
1211
do_forget(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1212 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1213 {
1214 struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1215
1216 if (req->se->op.forget)
1217 req->se->op.forget(req, nodeid, arg->nlookup);
1218 else
1219 fuse_reply_none(req);
1220 }
1221
do_batch_forget(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1222 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1223 const void *inarg)
1224 {
1225 struct fuse_batch_forget_in *arg = (void *) inarg;
1226 struct fuse_forget_one *param = (void *) PARAM(arg);
1227 unsigned int i;
1228
1229 (void) nodeid;
1230
1231 if (req->se->op.forget_multi) {
1232 req->se->op.forget_multi(req, arg->count,
1233 (struct fuse_forget_data *) param);
1234 } else if (req->se->op.forget) {
1235 for (i = 0; i < arg->count; i++) {
1236 struct fuse_forget_one *forget = ¶m[i];
1237 struct fuse_req *dummy_req;
1238
1239 dummy_req = fuse_ll_alloc_req(req->se);
1240 if (dummy_req == NULL)
1241 break;
1242
1243 dummy_req->unique = req->unique;
1244 dummy_req->ctx = req->ctx;
1245 dummy_req->ch = NULL;
1246
1247 req->se->op.forget(dummy_req, forget->nodeid,
1248 forget->nlookup);
1249 }
1250 fuse_reply_none(req);
1251 } else {
1252 fuse_reply_none(req);
1253 }
1254 }
1255
do_getattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1256 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1257 {
1258 struct fuse_file_info *fip = NULL;
1259 struct fuse_file_info fi;
1260
1261 if (req->se->conn.proto_minor >= 9) {
1262 struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1263
1264 if (arg->getattr_flags & FUSE_GETATTR_FH) {
1265 memset(&fi, 0, sizeof(fi));
1266 fi.fh = arg->fh;
1267 fip = &fi;
1268 }
1269 }
1270
1271 if (req->se->op.getattr)
1272 req->se->op.getattr(req, nodeid, fip);
1273 else
1274 fuse_reply_err(req, ENOSYS);
1275 }
1276
do_setattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1277 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1278 {
1279 struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1280
1281 if (req->se->op.setattr) {
1282 struct fuse_file_info *fi = NULL;
1283 struct fuse_file_info fi_store;
1284 struct stat stbuf;
1285 memset(&stbuf, 0, sizeof(stbuf));
1286 convert_attr(arg, &stbuf);
1287 if (arg->valid & FATTR_FH) {
1288 arg->valid &= ~FATTR_FH;
1289 memset(&fi_store, 0, sizeof(fi_store));
1290 fi = &fi_store;
1291 fi->fh = arg->fh;
1292 }
1293 arg->valid &=
1294 FUSE_SET_ATTR_MODE |
1295 FUSE_SET_ATTR_UID |
1296 FUSE_SET_ATTR_GID |
1297 FUSE_SET_ATTR_SIZE |
1298 FUSE_SET_ATTR_ATIME |
1299 FUSE_SET_ATTR_MTIME |
1300 FUSE_SET_ATTR_ATIME_NOW |
1301 FUSE_SET_ATTR_MTIME_NOW |
1302 FUSE_SET_ATTR_CTIME;
1303
1304 req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1305 } else
1306 fuse_reply_err(req, ENOSYS);
1307 }
1308
do_access(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1309 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1310 {
1311 struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1312
1313 if (req->se->op.access)
1314 req->se->op.access(req, nodeid, arg->mask);
1315 else
1316 fuse_reply_err(req, ENOSYS);
1317 }
1318
do_readlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1319 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1320 {
1321 (void) inarg;
1322
1323 if (req->se->op.readlink)
1324 req->se->op.readlink(req, nodeid);
1325 else
1326 fuse_reply_err(req, ENOSYS);
1327 }
1328
do_canonical_path(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1329 static void do_canonical_path(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1330 {
1331 (void) inarg;
1332
1333 if (req->se->op.canonical_path)
1334 req->se->op.canonical_path(req, nodeid);
1335 else
1336 fuse_reply_err(req, ENOSYS);
1337 }
1338
do_mknod(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1339 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1340 {
1341 struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1342 char *name = PARAM(arg);
1343
1344 if (req->se->conn.proto_minor >= 12)
1345 req->ctx.umask = arg->umask;
1346 else
1347 name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1348
1349 if (req->se->op.mknod)
1350 req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1351 else
1352 fuse_reply_err(req, ENOSYS);
1353 }
1354
do_mkdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1355 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1356 {
1357 struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1358
1359 if (req->se->conn.proto_minor >= 12)
1360 req->ctx.umask = arg->umask;
1361
1362 if (req->se->op.mkdir)
1363 req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1364 else
1365 fuse_reply_err(req, ENOSYS);
1366 }
1367
do_unlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1368 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1369 {
1370 char *name = (char *) inarg;
1371
1372 if (req->se->op.unlink)
1373 req->se->op.unlink(req, nodeid, name);
1374 else
1375 fuse_reply_err(req, ENOSYS);
1376 }
1377
do_rmdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1378 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1379 {
1380 char *name = (char *) inarg;
1381
1382 if (req->se->op.rmdir)
1383 req->se->op.rmdir(req, nodeid, name);
1384 else
1385 fuse_reply_err(req, ENOSYS);
1386 }
1387
do_symlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1388 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1389 {
1390 char *name = (char *) inarg;
1391 char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1392
1393 if (req->se->op.symlink)
1394 req->se->op.symlink(req, linkname, nodeid, name);
1395 else
1396 fuse_reply_err(req, ENOSYS);
1397 }
1398
do_rename(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1399 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1400 {
1401 struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1402 char *oldname = PARAM(arg);
1403 char *newname = oldname + strlen(oldname) + 1;
1404
1405 if (req->se->op.rename)
1406 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1407 0);
1408 else
1409 fuse_reply_err(req, ENOSYS);
1410 }
1411
do_rename2(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1412 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1413 {
1414 struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1415 char *oldname = PARAM(arg);
1416 char *newname = oldname + strlen(oldname) + 1;
1417
1418 if (req->se->op.rename)
1419 req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1420 arg->flags);
1421 else
1422 fuse_reply_err(req, ENOSYS);
1423 }
1424
do_link(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1425 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1426 {
1427 struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1428
1429 if (req->se->op.link)
1430 req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1431 else
1432 fuse_reply_err(req, ENOSYS);
1433 }
1434
do_create(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1435 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1436 {
1437 struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1438
1439 if (req->se->op.create) {
1440 struct fuse_file_info fi;
1441 char *name = PARAM(arg);
1442
1443 memset(&fi, 0, sizeof(fi));
1444 fi.flags = arg->flags;
1445
1446 if (req->se->conn.proto_minor >= 12)
1447 req->ctx.umask = arg->umask;
1448 else
1449 name = (char *) inarg + sizeof(struct fuse_open_in);
1450
1451 req->se->op.create(req, nodeid, name, arg->mode, &fi);
1452 } else
1453 fuse_reply_err(req, ENOSYS);
1454 }
1455
do_open(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1456 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1457 {
1458 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1459 struct fuse_file_info fi;
1460
1461 memset(&fi, 0, sizeof(fi));
1462 fi.flags = arg->flags;
1463
1464 if (req->se->op.open)
1465 req->se->op.open(req, nodeid, &fi);
1466 else
1467 fuse_reply_open(req, &fi);
1468 }
1469
do_read(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1470 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1471 {
1472 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1473
1474 if (req->se->op.read) {
1475 struct fuse_file_info fi;
1476
1477 memset(&fi, 0, sizeof(fi));
1478 fi.fh = arg->fh;
1479 if (req->se->conn.proto_minor >= 9) {
1480 fi.lock_owner = arg->lock_owner;
1481 fi.flags = arg->flags;
1482 }
1483 req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1484 } else
1485 fuse_reply_err(req, ENOSYS);
1486 }
1487
do_write(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1488 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1489 {
1490 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1491 struct fuse_file_info fi;
1492 char *param;
1493
1494 memset(&fi, 0, sizeof(fi));
1495 fi.fh = arg->fh;
1496 fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1497
1498 if (req->se->conn.proto_minor < 9) {
1499 param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1500 } else {
1501 fi.lock_owner = arg->lock_owner;
1502 fi.flags = arg->flags;
1503 param = PARAM(arg);
1504 }
1505
1506 if (req->se->op.write)
1507 req->se->op.write(req, nodeid, param, arg->size,
1508 arg->offset, &fi);
1509 else
1510 fuse_reply_err(req, ENOSYS);
1511 }
1512
do_write_buf(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,const struct fuse_buf * ibuf)1513 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1514 const struct fuse_buf *ibuf)
1515 {
1516 struct fuse_session *se = req->se;
1517 struct fuse_bufvec bufv = {
1518 .buf[0] = *ibuf,
1519 .count = 1,
1520 };
1521 struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1522 struct fuse_file_info fi;
1523
1524 memset(&fi, 0, sizeof(fi));
1525 fi.fh = arg->fh;
1526 fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1527
1528 if (se->conn.proto_minor < 9) {
1529 bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1530 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1531 FUSE_COMPAT_WRITE_IN_SIZE;
1532 assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1533 } else {
1534 fi.lock_owner = arg->lock_owner;
1535 fi.flags = arg->flags;
1536 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1537 bufv.buf[0].mem = PARAM(arg);
1538
1539 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1540 sizeof(struct fuse_write_in);
1541 }
1542 if (bufv.buf[0].size < arg->size) {
1543 fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1544 fuse_reply_err(req, EIO);
1545 goto out;
1546 }
1547 bufv.buf[0].size = arg->size;
1548
1549 se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1550
1551 out:
1552 /* Need to reset the pipe if ->write_buf() didn't consume all data */
1553 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1554 fuse_ll_clear_pipe(se);
1555 }
1556
do_flush(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1557 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1558 {
1559 struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1560 struct fuse_file_info fi;
1561
1562 memset(&fi, 0, sizeof(fi));
1563 fi.fh = arg->fh;
1564 fi.flush = 1;
1565 if (req->se->conn.proto_minor >= 7)
1566 fi.lock_owner = arg->lock_owner;
1567
1568 if (req->se->op.flush)
1569 req->se->op.flush(req, nodeid, &fi);
1570 else
1571 fuse_reply_err(req, ENOSYS);
1572 }
1573
do_release(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1574 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1575 {
1576 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1577 struct fuse_file_info fi;
1578
1579 memset(&fi, 0, sizeof(fi));
1580 fi.flags = arg->flags;
1581 fi.fh = arg->fh;
1582 if (req->se->conn.proto_minor >= 8) {
1583 fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1584 fi.lock_owner = arg->lock_owner;
1585 }
1586 if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1587 fi.flock_release = 1;
1588 fi.lock_owner = arg->lock_owner;
1589 }
1590
1591 if (req->se->op.release)
1592 req->se->op.release(req, nodeid, &fi);
1593 else
1594 fuse_reply_err(req, 0);
1595 }
1596
do_fsync(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1597 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1598 {
1599 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1600 struct fuse_file_info fi;
1601 int datasync = arg->fsync_flags & 1;
1602
1603 memset(&fi, 0, sizeof(fi));
1604 fi.fh = arg->fh;
1605
1606 if (req->se->op.fsync)
1607 req->se->op.fsync(req, nodeid, datasync, &fi);
1608 else
1609 fuse_reply_err(req, ENOSYS);
1610 }
1611
do_opendir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1612 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1613 {
1614 struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1615 struct fuse_file_info fi;
1616
1617 memset(&fi, 0, sizeof(fi));
1618 fi.flags = arg->flags;
1619
1620 if (req->se->op.opendir)
1621 req->se->op.opendir(req, nodeid, &fi);
1622 else
1623 fuse_reply_open(req, &fi);
1624 }
1625
do_readdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1626 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1627 {
1628 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1629 struct fuse_file_info fi;
1630
1631 memset(&fi, 0, sizeof(fi));
1632 fi.fh = arg->fh;
1633
1634 if (req->se->op.readdir)
1635 req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1636 else
1637 fuse_reply_err(req, ENOSYS);
1638 }
1639
do_readdirplus(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1640 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1641 {
1642 struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1643 struct fuse_file_info fi;
1644
1645 memset(&fi, 0, sizeof(fi));
1646 fi.fh = arg->fh;
1647
1648 if (req->se->op.readdirplus)
1649 req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1650 else
1651 fuse_reply_err(req, ENOSYS);
1652 }
1653
do_readdir_postfilter(fuse_req_t req,fuse_ino_t nodeid,uint32_t error_in,const void * inarg,size_t size)1654 static void do_readdir_postfilter(fuse_req_t req, fuse_ino_t nodeid,
1655 uint32_t error_in, const void *inarg,
1656 size_t size) {
1657 struct fuse_read_in *fri = (struct fuse_read_in *) inarg;
1658 struct fuse_read_out *fro = (struct fuse_read_out *) (fri + 1);
1659 struct fuse_dirent *dirents = (struct fuse_dirent *) (fro + 1);
1660 struct fuse_file_info fi;
1661
1662 memset(&fi, 0, sizeof(fi));
1663 fi.fh = fri->fh;
1664
1665 if (req->se->op.readdirpostfilter)
1666 req->se->op.readdirpostfilter(req, nodeid, error_in, fri->offset,
1667 fro->offset,
1668 size - sizeof(*fri) - sizeof(*fro),
1669 dirents, &fi);
1670 else
1671 fuse_reply_err(req, ENOSYS);
1672 }
1673
do_releasedir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1674 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1675 {
1676 struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1677 struct fuse_file_info fi;
1678
1679 memset(&fi, 0, sizeof(fi));
1680 fi.flags = arg->flags;
1681 fi.fh = arg->fh;
1682
1683 if (req->se->op.releasedir)
1684 req->se->op.releasedir(req, nodeid, &fi);
1685 else
1686 fuse_reply_err(req, 0);
1687 }
1688
do_fsyncdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1689 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1690 {
1691 struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1692 struct fuse_file_info fi;
1693 int datasync = arg->fsync_flags & 1;
1694
1695 memset(&fi, 0, sizeof(fi));
1696 fi.fh = arg->fh;
1697
1698 if (req->se->op.fsyncdir)
1699 req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1700 else
1701 fuse_reply_err(req, ENOSYS);
1702 }
1703
do_statfs(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1704 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1705 {
1706 (void) nodeid;
1707 (void) inarg;
1708
1709 if (req->se->op.statfs)
1710 req->se->op.statfs(req, nodeid);
1711 else {
1712 struct statvfs buf = {
1713 .f_namemax = 255,
1714 .f_bsize = 512,
1715 };
1716 fuse_reply_statfs(req, &buf);
1717 }
1718 }
1719
do_setxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1720 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1721 {
1722 struct fuse_session *se = req->se;
1723 unsigned int xattr_ext = !!(se->conn.want & FUSE_CAP_SETXATTR_EXT);
1724 struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1725 char *name = xattr_ext ? PARAM(arg) :
1726 (char *)arg + FUSE_COMPAT_SETXATTR_IN_SIZE;
1727 char *value = name + strlen(name) + 1;
1728
1729 /* XXX:The API should be extended to support extra_flags/setxattr_flags */
1730 if (req->se->op.setxattr)
1731 req->se->op.setxattr(req, nodeid, name, value, arg->size,
1732 arg->flags);
1733 else
1734 fuse_reply_err(req, ENOSYS);
1735 }
1736
do_getxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1737 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1738 {
1739 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1740
1741 if (req->se->op.getxattr)
1742 req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1743 else
1744 fuse_reply_err(req, ENOSYS);
1745 }
1746
do_listxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1747 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1748 {
1749 struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1750
1751 if (req->se->op.listxattr)
1752 req->se->op.listxattr(req, nodeid, arg->size);
1753 else
1754 fuse_reply_err(req, ENOSYS);
1755 }
1756
do_removexattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1757 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1758 {
1759 char *name = (char *) inarg;
1760
1761 if (req->se->op.removexattr)
1762 req->se->op.removexattr(req, nodeid, name);
1763 else
1764 fuse_reply_err(req, ENOSYS);
1765 }
1766
convert_fuse_file_lock(struct fuse_file_lock * fl,struct flock * flock)1767 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1768 struct flock *flock)
1769 {
1770 memset(flock, 0, sizeof(struct flock));
1771 flock->l_type = fl->type;
1772 flock->l_whence = SEEK_SET;
1773 flock->l_start = fl->start;
1774 if (fl->end == OFFSET_MAX)
1775 flock->l_len = 0;
1776 else
1777 flock->l_len = fl->end - fl->start + 1;
1778 flock->l_pid = fl->pid;
1779 }
1780
do_getlk(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1781 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1782 {
1783 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1784 struct fuse_file_info fi;
1785 struct flock flock;
1786
1787 memset(&fi, 0, sizeof(fi));
1788 fi.fh = arg->fh;
1789 fi.lock_owner = arg->owner;
1790
1791 convert_fuse_file_lock(&arg->lk, &flock);
1792 if (req->se->op.getlk)
1793 req->se->op.getlk(req, nodeid, &fi, &flock);
1794 else
1795 fuse_reply_err(req, ENOSYS);
1796 }
1797
do_setlk_common(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,int sleep)1798 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1799 const void *inarg, int sleep)
1800 {
1801 struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1802 struct fuse_file_info fi;
1803 struct flock flock;
1804
1805 memset(&fi, 0, sizeof(fi));
1806 fi.fh = arg->fh;
1807 fi.lock_owner = arg->owner;
1808
1809 if (arg->lk_flags & FUSE_LK_FLOCK) {
1810 int op = 0;
1811
1812 switch (arg->lk.type) {
1813 case F_RDLCK:
1814 op = LOCK_SH;
1815 break;
1816 case F_WRLCK:
1817 op = LOCK_EX;
1818 break;
1819 case F_UNLCK:
1820 op = LOCK_UN;
1821 break;
1822 }
1823 if (!sleep)
1824 op |= LOCK_NB;
1825
1826 if (req->se->op.flock)
1827 req->se->op.flock(req, nodeid, &fi, op);
1828 else
1829 fuse_reply_err(req, ENOSYS);
1830 } else {
1831 convert_fuse_file_lock(&arg->lk, &flock);
1832 if (req->se->op.setlk)
1833 req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1834 else
1835 fuse_reply_err(req, ENOSYS);
1836 }
1837 }
1838
do_setlk(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1839 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1840 {
1841 do_setlk_common(req, nodeid, inarg, 0);
1842 }
1843
do_setlkw(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1844 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1845 {
1846 do_setlk_common(req, nodeid, inarg, 1);
1847 }
1848
find_interrupted(struct fuse_session * se,struct fuse_req * req)1849 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1850 {
1851 struct fuse_req *curr;
1852
1853 for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1854 if (curr->unique == req->u.i.unique) {
1855 fuse_interrupt_func_t func;
1856 void *data;
1857
1858 curr->ctr++;
1859 pthread_mutex_unlock(&se->lock);
1860
1861 /* Ugh, ugly locking */
1862 pthread_mutex_lock(&curr->lock);
1863 pthread_mutex_lock(&se->lock);
1864 curr->interrupted = 1;
1865 func = curr->u.ni.func;
1866 data = curr->u.ni.data;
1867 pthread_mutex_unlock(&se->lock);
1868 if (func)
1869 func(curr, data);
1870 pthread_mutex_unlock(&curr->lock);
1871
1872 pthread_mutex_lock(&se->lock);
1873 curr->ctr--;
1874 if (!curr->ctr)
1875 destroy_req(curr);
1876
1877 return 1;
1878 }
1879 }
1880 for (curr = se->interrupts.next; curr != &se->interrupts;
1881 curr = curr->next) {
1882 if (curr->u.i.unique == req->u.i.unique)
1883 return 1;
1884 }
1885 return 0;
1886 }
1887
do_interrupt(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1888 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1889 {
1890 struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1891 struct fuse_session *se = req->se;
1892
1893 (void) nodeid;
1894 if (se->debug)
1895 fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1896 (unsigned long long) arg->unique);
1897
1898 req->u.i.unique = arg->unique;
1899
1900 pthread_mutex_lock(&se->lock);
1901 if (find_interrupted(se, req))
1902 destroy_req(req);
1903 else
1904 list_add_req(req, &se->interrupts);
1905 pthread_mutex_unlock(&se->lock);
1906 }
1907
check_interrupt(struct fuse_session * se,struct fuse_req * req)1908 static struct fuse_req *check_interrupt(struct fuse_session *se,
1909 struct fuse_req *req)
1910 {
1911 struct fuse_req *curr;
1912
1913 for (curr = se->interrupts.next; curr != &se->interrupts;
1914 curr = curr->next) {
1915 if (curr->u.i.unique == req->unique) {
1916 req->interrupted = 1;
1917 list_del_req(curr);
1918 free(curr);
1919 return NULL;
1920 }
1921 }
1922 curr = se->interrupts.next;
1923 if (curr != &se->interrupts) {
1924 list_del_req(curr);
1925 list_init_req(curr);
1926 return curr;
1927 } else
1928 return NULL;
1929 }
1930
do_bmap(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1931 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1932 {
1933 struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1934
1935 if (req->se->op.bmap)
1936 req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1937 else
1938 fuse_reply_err(req, ENOSYS);
1939 }
1940
do_ioctl(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1941 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1942 {
1943 struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1944 unsigned int flags = arg->flags;
1945 void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1946 struct fuse_file_info fi;
1947
1948 if (flags & FUSE_IOCTL_DIR &&
1949 !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1950 fuse_reply_err(req, ENOTTY);
1951 return;
1952 }
1953
1954 memset(&fi, 0, sizeof(fi));
1955 fi.fh = arg->fh;
1956
1957 if (sizeof(void *) == 4 && req->se->conn.proto_minor >= 16 &&
1958 !(flags & FUSE_IOCTL_32BIT)) {
1959 req->ioctl_64bit = 1;
1960 }
1961
1962 if (req->se->op.ioctl)
1963 req->se->op.ioctl(req, nodeid, arg->cmd,
1964 (void *)(uintptr_t)arg->arg, &fi, flags,
1965 in_buf, arg->in_size, arg->out_size);
1966 else
1967 fuse_reply_err(req, ENOSYS);
1968 }
1969
fuse_pollhandle_destroy(struct fuse_pollhandle * ph)1970 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1971 {
1972 free(ph);
1973 }
1974
do_poll(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1975 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1976 {
1977 struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1978 struct fuse_file_info fi;
1979
1980 memset(&fi, 0, sizeof(fi));
1981 fi.fh = arg->fh;
1982 fi.poll_events = arg->events;
1983
1984 if (req->se->op.poll) {
1985 struct fuse_pollhandle *ph = NULL;
1986
1987 if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1988 ph = malloc(sizeof(struct fuse_pollhandle));
1989 if (ph == NULL) {
1990 fuse_reply_err(req, ENOMEM);
1991 return;
1992 }
1993 ph->kh = arg->kh;
1994 ph->se = req->se;
1995 }
1996
1997 req->se->op.poll(req, nodeid, &fi, ph);
1998 } else {
1999 fuse_reply_err(req, ENOSYS);
2000 }
2001 }
2002
do_fallocate(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2003 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2004 {
2005 struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
2006 struct fuse_file_info fi;
2007
2008 memset(&fi, 0, sizeof(fi));
2009 fi.fh = arg->fh;
2010
2011 if (req->se->op.fallocate)
2012 req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
2013 else
2014 fuse_reply_err(req, ENOSYS);
2015 }
2016
do_copy_file_range(fuse_req_t req,fuse_ino_t nodeid_in,const void * inarg)2017 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, const void *inarg)
2018 {
2019 struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in *) inarg;
2020 struct fuse_file_info fi_in, fi_out;
2021
2022 memset(&fi_in, 0, sizeof(fi_in));
2023 fi_in.fh = arg->fh_in;
2024
2025 memset(&fi_out, 0, sizeof(fi_out));
2026 fi_out.fh = arg->fh_out;
2027
2028
2029 if (req->se->op.copy_file_range)
2030 req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
2031 &fi_in, arg->nodeid_out,
2032 arg->off_out, &fi_out, arg->len,
2033 arg->flags);
2034 else
2035 fuse_reply_err(req, ENOSYS);
2036 }
2037
do_lseek(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2038 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2039 {
2040 struct fuse_lseek_in *arg = (struct fuse_lseek_in *) inarg;
2041 struct fuse_file_info fi;
2042
2043 memset(&fi, 0, sizeof(fi));
2044 fi.fh = arg->fh;
2045
2046 if (req->se->op.lseek)
2047 req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
2048 else
2049 fuse_reply_err(req, ENOSYS);
2050 }
2051
2052 /* Prevent bogus data races (bogus since "init" is called before
2053 * multi-threading becomes relevant */
2054 static __attribute__((no_sanitize("thread")))
do_init(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2055 void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2056 {
2057 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
2058 struct fuse_init_out outarg;
2059 struct fuse_session *se = req->se;
2060 size_t bufsize = se->bufsize;
2061 size_t outargsize = sizeof(outarg);
2062 int extended_flags;
2063
2064 (void) nodeid;
2065 if (se->debug) {
2066 fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
2067 if (arg->major == 7 && arg->minor >= 6) {
2068 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
2069 fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
2070 arg->max_readahead);
2071 }
2072 }
2073 se->conn.proto_major = arg->major;
2074 se->conn.proto_minor = arg->minor;
2075 se->conn.capable = 0;
2076 se->conn.want = 0;
2077
2078 memset(&outarg, 0, sizeof(outarg));
2079 outarg.major = FUSE_KERNEL_VERSION;
2080 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
2081
2082 extended_flags = arg->major > 7 || (arg->major == 7 && arg->minor >= 36);
2083 fuse_log(FUSE_LOG_DEBUG, "fuse: protocol version: %u.%u, extended flags: %d\n",
2084 arg->major, arg->minor, extended_flags);
2085
2086 if (arg->major < 7) {
2087 fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
2088 arg->major, arg->minor);
2089 fuse_reply_err(req, EPROTO);
2090 return;
2091 }
2092
2093 if (arg->major > 7) {
2094 /* Wait for a second INIT request with a 7.X version */
2095 send_reply_ok(req, &outarg, sizeof(outarg));
2096 return;
2097 }
2098
2099 if (arg->minor >= 6) {
2100 if (arg->max_readahead < se->conn.max_readahead)
2101 se->conn.max_readahead = arg->max_readahead;
2102 if (arg->flags & FUSE_ASYNC_READ)
2103 se->conn.capable |= FUSE_CAP_ASYNC_READ;
2104 if (arg->flags & FUSE_POSIX_LOCKS)
2105 se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
2106 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
2107 se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
2108 if (arg->flags & FUSE_EXPORT_SUPPORT)
2109 se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
2110 if (arg->flags & FUSE_DONT_MASK)
2111 se->conn.capable |= FUSE_CAP_DONT_MASK;
2112 if (arg->flags & FUSE_FLOCK_LOCKS)
2113 se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
2114 if (arg->flags & FUSE_AUTO_INVAL_DATA)
2115 se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
2116 if (arg->flags & FUSE_DO_READDIRPLUS)
2117 se->conn.capable |= FUSE_CAP_READDIRPLUS;
2118 if (arg->flags & FUSE_READDIRPLUS_AUTO)
2119 se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
2120 if (arg->flags & FUSE_ASYNC_DIO)
2121 se->conn.capable |= FUSE_CAP_ASYNC_DIO;
2122 if (arg->flags & FUSE_WRITEBACK_CACHE)
2123 se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
2124 if (arg->flags & FUSE_NO_OPEN_SUPPORT)
2125 se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
2126 if (arg->flags & FUSE_PARALLEL_DIROPS)
2127 se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
2128 if (arg->flags & FUSE_POSIX_ACL)
2129 se->conn.capable |= FUSE_CAP_POSIX_ACL;
2130 if (arg->flags & FUSE_HANDLE_KILLPRIV)
2131 se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
2132 if (arg->flags & FUSE_CACHE_SYMLINKS)
2133 se->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
2134 if (arg->flags & FUSE_NO_OPENDIR_SUPPORT)
2135 se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
2136 if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
2137 se->conn.capable |= FUSE_CAP_EXPLICIT_INVAL_DATA;
2138 if (arg->flags & FUSE_SETXATTR_EXT)
2139 se->conn.capable |= FUSE_CAP_SETXATTR_EXT;
2140 if (!(arg->flags & FUSE_MAX_PAGES)) {
2141 size_t max_bufsize =
2142 FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
2143 + FUSE_BUFFER_HEADER_SIZE;
2144 if (bufsize > max_bufsize) {
2145 bufsize = max_bufsize;
2146 }
2147 }
2148 if (extended_flags) {
2149 if (arg->flags2 & (1 << 31))
2150 se->conn.capable |= FUSE_CAP_PASSTHROUGH;
2151 } else {
2152 if (arg->flags & (1 << 31))
2153 se->conn.capable |= FUSE_CAP_PASSTHROUGH;
2154 }
2155 } else {
2156 se->conn.max_readahead = 0;
2157 }
2158
2159 if (se->conn.proto_minor >= 14) {
2160 #ifdef HAVE_SPLICE
2161 #ifdef HAVE_VMSPLICE
2162 se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
2163 #endif
2164 se->conn.capable |= FUSE_CAP_SPLICE_READ;
2165 #endif
2166 }
2167 if (se->conn.proto_minor >= 18)
2168 se->conn.capable |= FUSE_CAP_IOCTL_DIR;
2169
2170 /* Default settings for modern filesystems.
2171 *
2172 * Most of these capabilities were disabled by default in
2173 * libfuse2 for backwards compatibility reasons. In libfuse3,
2174 * we can finally enable them by default (as long as they're
2175 * supported by the kernel).
2176 */
2177 #define LL_SET_DEFAULT(cond, cap) \
2178 if ((cond) && (se->conn.capable & (cap))) \
2179 se->conn.want |= (cap)
2180 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2181 LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2182 LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2183 LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2184 LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2185 LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2186 LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2187 LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2188 LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
2189 FUSE_CAP_POSIX_LOCKS);
2190 LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2191 LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2192 LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2193 FUSE_CAP_READDIRPLUS_AUTO);
2194 se->conn.time_gran = 1;
2195
2196 if (bufsize < FUSE_MIN_READ_BUFFER) {
2197 fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2198 bufsize);
2199 bufsize = FUSE_MIN_READ_BUFFER;
2200 }
2201 se->bufsize = bufsize;
2202
2203 if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
2204 se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2205
2206 se->got_init = 1;
2207 if (se->op.init)
2208 se->op.init(se->userdata, &se->conn);
2209
2210 if (se->conn.want & (~se->conn.capable)) {
2211 fuse_log(FUSE_LOG_ERR, "fuse: error: filesystem requested capabilities "
2212 "0x%x that are not supported by kernel, aborting.\n",
2213 se->conn.want & (~se->conn.capable));
2214 fuse_reply_err(req, EPROTO);
2215 se->error = -EPROTO;
2216 fuse_session_exit(se);
2217 return;
2218 }
2219
2220 unsigned max_read_mo = get_max_read(se->mo);
2221 if (se->conn.max_read != max_read_mo) {
2222 fuse_log(FUSE_LOG_ERR, "fuse: error: init() and fuse_session_new() "
2223 "requested different maximum read size (%u vs %u)\n",
2224 se->conn.max_read, max_read_mo);
2225 fuse_reply_err(req, EPROTO);
2226 se->error = -EPROTO;
2227 fuse_session_exit(se);
2228 return;
2229 }
2230
2231 if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2232 se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2233 }
2234 if (arg->flags & FUSE_MAX_PAGES) {
2235 outarg.flags |= FUSE_MAX_PAGES;
2236 outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2237 }
2238
2239 /* Always enable big writes, this is superseded
2240 by the max_write option */
2241 outarg.flags |= FUSE_BIG_WRITES;
2242
2243 if (se->conn.want & FUSE_CAP_ASYNC_READ)
2244 outarg.flags |= FUSE_ASYNC_READ;
2245 if (se->conn.want & FUSE_CAP_POSIX_LOCKS)
2246 outarg.flags |= FUSE_POSIX_LOCKS;
2247 if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2248 outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2249 if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2250 outarg.flags |= FUSE_EXPORT_SUPPORT;
2251 if (se->conn.want & FUSE_CAP_DONT_MASK)
2252 outarg.flags |= FUSE_DONT_MASK;
2253 if (se->conn.want & FUSE_CAP_FLOCK_LOCKS)
2254 outarg.flags |= FUSE_FLOCK_LOCKS;
2255 if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2256 outarg.flags |= FUSE_AUTO_INVAL_DATA;
2257 if (se->conn.want & FUSE_CAP_READDIRPLUS)
2258 outarg.flags |= FUSE_DO_READDIRPLUS;
2259 if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2260 outarg.flags |= FUSE_READDIRPLUS_AUTO;
2261 if (se->conn.want & FUSE_CAP_ASYNC_DIO)
2262 outarg.flags |= FUSE_ASYNC_DIO;
2263 if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2264 outarg.flags |= FUSE_WRITEBACK_CACHE;
2265 if (se->conn.want & FUSE_CAP_POSIX_ACL)
2266 outarg.flags |= FUSE_POSIX_ACL;
2267 if (se->conn.want & FUSE_CAP_PASSTHROUGH) {
2268 if (extended_flags)
2269 outarg.flags2 |= (1 << 31);
2270 else
2271 outarg.flags |= (1 << 31);
2272 }
2273 if (se->conn.want & FUSE_CAP_CACHE_SYMLINKS)
2274 outarg.flags |= FUSE_CACHE_SYMLINKS;
2275 if (se->conn.want & FUSE_CAP_EXPLICIT_INVAL_DATA)
2276 outarg.flags |= FUSE_EXPLICIT_INVAL_DATA;
2277 if (se->conn.want & FUSE_CAP_SETXATTR_EXT)
2278 outarg.flags |= FUSE_SETXATTR_EXT;
2279 outarg.max_readahead = se->conn.max_readahead;
2280 outarg.max_write = se->conn.max_write;
2281 if (se->conn.proto_minor >= 13) {
2282 if (se->conn.max_background >= (1 << 16))
2283 se->conn.max_background = (1 << 16) - 1;
2284 if (se->conn.congestion_threshold > se->conn.max_background)
2285 se->conn.congestion_threshold = se->conn.max_background;
2286 if (!se->conn.congestion_threshold) {
2287 se->conn.congestion_threshold =
2288 se->conn.max_background * 3 / 4;
2289 }
2290
2291 outarg.max_background = se->conn.max_background;
2292 outarg.congestion_threshold = se->conn.congestion_threshold;
2293 }
2294 if (se->conn.proto_minor >= 23)
2295 outarg.time_gran = se->conn.time_gran;
2296
2297 if (se->debug) {
2298 fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor);
2299 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
2300 fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n",
2301 outarg.max_readahead);
2302 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
2303 fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n",
2304 outarg.max_background);
2305 fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n",
2306 outarg.congestion_threshold);
2307 fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n",
2308 outarg.time_gran);
2309 }
2310 if (arg->minor < 5)
2311 outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2312 else if (arg->minor < 23)
2313 outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2314
2315 send_reply_ok(req, &outarg, outargsize);
2316 }
2317
do_destroy(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2318 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2319 {
2320 struct fuse_session *se = req->se;
2321
2322 (void) nodeid;
2323 (void) inarg;
2324
2325 se->got_destroy = 1;
2326 if (se->op.destroy)
2327 se->op.destroy(se->userdata);
2328
2329 send_reply_ok(req, NULL, 0);
2330 }
2331
list_del_nreq(struct fuse_notify_req * nreq)2332 static void list_del_nreq(struct fuse_notify_req *nreq)
2333 {
2334 struct fuse_notify_req *prev = nreq->prev;
2335 struct fuse_notify_req *next = nreq->next;
2336 prev->next = next;
2337 next->prev = prev;
2338 }
2339
list_add_nreq(struct fuse_notify_req * nreq,struct fuse_notify_req * next)2340 static void list_add_nreq(struct fuse_notify_req *nreq,
2341 struct fuse_notify_req *next)
2342 {
2343 struct fuse_notify_req *prev = next->prev;
2344 nreq->next = next;
2345 nreq->prev = prev;
2346 prev->next = nreq;
2347 next->prev = nreq;
2348 }
2349
list_init_nreq(struct fuse_notify_req * nreq)2350 static void list_init_nreq(struct fuse_notify_req *nreq)
2351 {
2352 nreq->next = nreq;
2353 nreq->prev = nreq;
2354 }
2355
do_notify_reply(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,const struct fuse_buf * buf)2356 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2357 const void *inarg, const struct fuse_buf *buf)
2358 {
2359 struct fuse_session *se = req->se;
2360 struct fuse_notify_req *nreq;
2361 struct fuse_notify_req *head;
2362
2363 pthread_mutex_lock(&se->lock);
2364 head = &se->notify_list;
2365 for (nreq = head->next; nreq != head; nreq = nreq->next) {
2366 if (nreq->unique == req->unique) {
2367 list_del_nreq(nreq);
2368 break;
2369 }
2370 }
2371 pthread_mutex_unlock(&se->lock);
2372
2373 if (nreq != head)
2374 nreq->reply(nreq, req, nodeid, inarg, buf);
2375 }
2376
send_notify_iov(struct fuse_session * se,int notify_code,struct iovec * iov,int count)2377 static int send_notify_iov(struct fuse_session *se, int notify_code,
2378 struct iovec *iov, int count)
2379 {
2380 struct fuse_out_header out;
2381
2382 if (!se->got_init)
2383 return -ENOTCONN;
2384
2385 out.unique = 0;
2386 out.error = notify_code;
2387 iov[0].iov_base = &out;
2388 iov[0].iov_len = sizeof(struct fuse_out_header);
2389
2390 return fuse_send_msg(se, NULL, iov, count);
2391 }
2392
fuse_lowlevel_notify_poll(struct fuse_pollhandle * ph)2393 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2394 {
2395 if (ph != NULL) {
2396 struct fuse_notify_poll_wakeup_out outarg;
2397 struct iovec iov[2];
2398
2399 outarg.kh = ph->kh;
2400
2401 iov[1].iov_base = &outarg;
2402 iov[1].iov_len = sizeof(outarg);
2403
2404 return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2405 } else {
2406 return 0;
2407 }
2408 }
2409
fuse_lowlevel_notify_inval_inode(struct fuse_session * se,fuse_ino_t ino,off_t off,off_t len)2410 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2411 off_t off, off_t len)
2412 {
2413 struct fuse_notify_inval_inode_out outarg;
2414 struct iovec iov[2];
2415
2416 if (!se)
2417 return -EINVAL;
2418
2419 if (se->conn.proto_minor < 12)
2420 return -ENOSYS;
2421
2422 outarg.ino = ino;
2423 outarg.off = off;
2424 outarg.len = len;
2425
2426 iov[1].iov_base = &outarg;
2427 iov[1].iov_len = sizeof(outarg);
2428
2429 return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2430 }
2431
fuse_lowlevel_notify_inval_entry(struct fuse_session * se,fuse_ino_t parent,const char * name,size_t namelen)2432 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2433 const char *name, size_t namelen)
2434 {
2435 struct fuse_notify_inval_entry_out outarg;
2436 struct iovec iov[3];
2437
2438 if (!se)
2439 return -EINVAL;
2440
2441 if (se->conn.proto_minor < 12)
2442 return -ENOSYS;
2443
2444 outarg.parent = parent;
2445 outarg.namelen = namelen;
2446 outarg.padding = 0;
2447
2448 iov[1].iov_base = &outarg;
2449 iov[1].iov_len = sizeof(outarg);
2450 iov[2].iov_base = (void *)name;
2451 iov[2].iov_len = namelen + 1;
2452
2453 return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2454 }
2455
fuse_lowlevel_notify_delete(struct fuse_session * se,fuse_ino_t parent,fuse_ino_t child,const char * name,size_t namelen)2456 int fuse_lowlevel_notify_delete(struct fuse_session *se,
2457 fuse_ino_t parent, fuse_ino_t child,
2458 const char *name, size_t namelen)
2459 {
2460 struct fuse_notify_delete_out outarg;
2461 struct iovec iov[3];
2462
2463 if (!se)
2464 return -EINVAL;
2465
2466 if (se->conn.proto_minor < 18)
2467 return -ENOSYS;
2468
2469 outarg.parent = parent;
2470 outarg.child = child;
2471 outarg.namelen = namelen;
2472 outarg.padding = 0;
2473
2474 iov[1].iov_base = &outarg;
2475 iov[1].iov_len = sizeof(outarg);
2476 iov[2].iov_base = (void *)name;
2477 iov[2].iov_len = namelen + 1;
2478
2479 return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2480 }
2481
fuse_lowlevel_notify_store(struct fuse_session * se,fuse_ino_t ino,off_t offset,struct fuse_bufvec * bufv,enum fuse_buf_copy_flags flags)2482 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2483 off_t offset, struct fuse_bufvec *bufv,
2484 enum fuse_buf_copy_flags flags)
2485 {
2486 struct fuse_out_header out;
2487 struct fuse_notify_store_out outarg;
2488 struct iovec iov[3];
2489 size_t size = fuse_buf_size(bufv);
2490 int res;
2491
2492 if (!se)
2493 return -EINVAL;
2494
2495 if (se->conn.proto_minor < 15)
2496 return -ENOSYS;
2497
2498 out.unique = 0;
2499 out.error = FUSE_NOTIFY_STORE;
2500
2501 outarg.nodeid = ino;
2502 outarg.offset = offset;
2503 outarg.size = size;
2504 outarg.padding = 0;
2505
2506 iov[0].iov_base = &out;
2507 iov[0].iov_len = sizeof(out);
2508 iov[1].iov_base = &outarg;
2509 iov[1].iov_len = sizeof(outarg);
2510
2511 res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2512 if (res > 0)
2513 res = -res;
2514
2515 return res;
2516 }
2517
2518 struct fuse_retrieve_req {
2519 struct fuse_notify_req nreq;
2520 void *cookie;
2521 };
2522
fuse_ll_retrieve_reply(struct fuse_notify_req * nreq,fuse_req_t req,fuse_ino_t ino,const void * inarg,const struct fuse_buf * ibuf)2523 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2524 fuse_req_t req, fuse_ino_t ino,
2525 const void *inarg,
2526 const struct fuse_buf *ibuf)
2527 {
2528 struct fuse_session *se = req->se;
2529 struct fuse_retrieve_req *rreq =
2530 container_of(nreq, struct fuse_retrieve_req, nreq);
2531 const struct fuse_notify_retrieve_in *arg = inarg;
2532 struct fuse_bufvec bufv = {
2533 .buf[0] = *ibuf,
2534 .count = 1,
2535 };
2536
2537 if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2538 bufv.buf[0].mem = PARAM(arg);
2539
2540 bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2541 sizeof(struct fuse_notify_retrieve_in);
2542
2543 if (bufv.buf[0].size < arg->size) {
2544 fuse_log(FUSE_LOG_ERR, "fuse: retrieve reply: buffer size too small\n");
2545 fuse_reply_none(req);
2546 goto out;
2547 }
2548 bufv.buf[0].size = arg->size;
2549
2550 if (se->op.retrieve_reply) {
2551 se->op.retrieve_reply(req, rreq->cookie, ino,
2552 arg->offset, &bufv);
2553 } else {
2554 fuse_reply_none(req);
2555 }
2556 out:
2557 free(rreq);
2558 if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2559 fuse_ll_clear_pipe(se);
2560 }
2561
fuse_lowlevel_notify_retrieve(struct fuse_session * se,fuse_ino_t ino,size_t size,off_t offset,void * cookie)2562 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
2563 size_t size, off_t offset, void *cookie)
2564 {
2565 struct fuse_notify_retrieve_out outarg;
2566 struct iovec iov[2];
2567 struct fuse_retrieve_req *rreq;
2568 int err;
2569
2570 if (!se)
2571 return -EINVAL;
2572
2573 if (se->conn.proto_minor < 15)
2574 return -ENOSYS;
2575
2576 rreq = malloc(sizeof(*rreq));
2577 if (rreq == NULL)
2578 return -ENOMEM;
2579
2580 pthread_mutex_lock(&se->lock);
2581 rreq->cookie = cookie;
2582 rreq->nreq.unique = se->notify_ctr++;
2583 rreq->nreq.reply = fuse_ll_retrieve_reply;
2584 list_add_nreq(&rreq->nreq, &se->notify_list);
2585 pthread_mutex_unlock(&se->lock);
2586
2587 outarg.notify_unique = rreq->nreq.unique;
2588 outarg.nodeid = ino;
2589 outarg.offset = offset;
2590 outarg.size = size;
2591 outarg.padding = 0;
2592
2593 iov[1].iov_base = &outarg;
2594 iov[1].iov_len = sizeof(outarg);
2595
2596 err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2597 if (err) {
2598 pthread_mutex_lock(&se->lock);
2599 list_del_nreq(&rreq->nreq);
2600 pthread_mutex_unlock(&se->lock);
2601 free(rreq);
2602 }
2603
2604 return err;
2605 }
2606
fuse_req_userdata(fuse_req_t req)2607 void *fuse_req_userdata(fuse_req_t req)
2608 {
2609 return req->se->userdata;
2610 }
2611
fuse_req_ctx(fuse_req_t req)2612 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2613 {
2614 return &req->ctx;
2615 }
2616
fuse_req_interrupt_func(fuse_req_t req,fuse_interrupt_func_t func,void * data)2617 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2618 void *data)
2619 {
2620 pthread_mutex_lock(&req->lock);
2621 pthread_mutex_lock(&req->se->lock);
2622 req->u.ni.func = func;
2623 req->u.ni.data = data;
2624 pthread_mutex_unlock(&req->se->lock);
2625 if (req->interrupted && func)
2626 func(req, data);
2627 pthread_mutex_unlock(&req->lock);
2628 }
2629
fuse_req_interrupted(fuse_req_t req)2630 int fuse_req_interrupted(fuse_req_t req)
2631 {
2632 int interrupted;
2633
2634 pthread_mutex_lock(&req->se->lock);
2635 interrupted = req->interrupted;
2636 pthread_mutex_unlock(&req->se->lock);
2637
2638 return interrupted;
2639 }
2640
2641 static struct {
2642 void (*func)(fuse_req_t, fuse_ino_t, const void *);
2643 const char *name;
2644 } fuse_ll_ops[] = {
2645 [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2646 [FUSE_FORGET] = { do_forget, "FORGET" },
2647 [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2648 [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2649 [FUSE_READLINK] = { do_readlink, "READLINK" },
2650 [FUSE_CANONICAL_PATH] = { do_canonical_path, "CANONICAL_PATH" },
2651 [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2652 [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2653 [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2654 [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2655 [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2656 [FUSE_RENAME] = { do_rename, "RENAME" },
2657 [FUSE_LINK] = { do_link, "LINK" },
2658 [FUSE_OPEN] = { do_open, "OPEN" },
2659 [FUSE_READ] = { do_read, "READ" },
2660 [FUSE_WRITE] = { do_write, "WRITE" },
2661 [FUSE_STATFS] = { do_statfs, "STATFS" },
2662 [FUSE_RELEASE] = { do_release, "RELEASE" },
2663 [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2664 [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2665 [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2666 [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2667 [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2668 [FUSE_FLUSH] = { do_flush, "FLUSH" },
2669 [FUSE_INIT] = { do_init, "INIT" },
2670 [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2671 [FUSE_READDIR] = { do_readdir, "READDIR" },
2672 [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2673 [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2674 [FUSE_GETLK] = { do_getlk, "GETLK" },
2675 [FUSE_SETLK] = { do_setlk, "SETLK" },
2676 [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2677 [FUSE_ACCESS] = { do_access, "ACCESS" },
2678 [FUSE_CREATE] = { do_create, "CREATE" },
2679 [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2680 [FUSE_BMAP] = { do_bmap, "BMAP" },
2681 [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2682 [FUSE_POLL] = { do_poll, "POLL" },
2683 [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2684 [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2685 [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2686 [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2687 [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2688 [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2689 [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2690 [FUSE_LSEEK] = { do_lseek, "LSEEK" },
2691 [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2692 };
2693
2694 static struct {
2695 void (*func)( fuse_req_t, fuse_ino_t, const void *);
2696 const char *name;
2697 } fuse_ll_prefilter_ops[] = {};
2698
2699 static struct {
2700 void (*func)( fuse_req_t, fuse_ino_t, uint32_t, const void *, size_t size);
2701 } fuse_ll_postfilter_ops[] = {
2702 [FUSE_LOOKUP] = {do_lookup_postfilter},
2703 [FUSE_READDIR] = {do_readdir_postfilter},
2704 };
2705
2706 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2707
opname(enum fuse_opcode opcode)2708 static const char *opname(enum fuse_opcode opcode)
2709 {
2710 if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2711 return "???";
2712 else
2713 return fuse_ll_ops[opcode].name;
2714 }
2715
opfiltername(int filter)2716 static const char *opfiltername(int filter)
2717 {
2718 switch (filter) {
2719 case 0:
2720 return "NONE";
2721 case FUSE_PREFILTER:
2722 return "FUSE_PREFILTER";
2723 case FUSE_POSTFILTER:
2724 return "FUSE_POSTFILTER";
2725 case FUSE_PREFILTER | FUSE_POSTFILTER:
2726 return "FUSE_PREFILTER | FUSE_POSTFILTER";
2727 default:
2728 return "???";
2729 }
2730 }
2731
fuse_ll_copy_from_pipe(struct fuse_bufvec * dst,struct fuse_bufvec * src)2732 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2733 struct fuse_bufvec *src)
2734 {
2735 ssize_t res = fuse_buf_copy(dst, src, 0);
2736 if (res < 0) {
2737 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n", strerror(-res));
2738 return res;
2739 }
2740 if ((size_t)res < fuse_buf_size(dst)) {
2741 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
2742 return -1;
2743 }
2744 return 0;
2745 }
2746
fuse_session_process_buf(struct fuse_session * se,const struct fuse_buf * buf)2747 void fuse_session_process_buf(struct fuse_session *se,
2748 const struct fuse_buf *buf)
2749 {
2750 fuse_session_process_buf_int(se, buf, NULL);
2751 }
2752
fuse_session_process_buf_int(struct fuse_session * se,const struct fuse_buf * buf,struct fuse_chan * ch)2753 void fuse_session_process_buf_int(struct fuse_session *se,
2754 const struct fuse_buf *buf, struct fuse_chan *ch)
2755 {
2756 const size_t write_header_size = sizeof(struct fuse_in_header) +
2757 sizeof(struct fuse_write_in);
2758 struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2759 struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2760 struct fuse_in_header *in;
2761 const void *inarg;
2762 struct fuse_req *req;
2763 void *mbuf = NULL;
2764 int err;
2765 int res;
2766 int opcode_filter;
2767
2768 if (buf->flags & FUSE_BUF_IS_FD) {
2769 if (buf->size < tmpbuf.buf[0].size)
2770 tmpbuf.buf[0].size = buf->size;
2771
2772 mbuf = malloc(tmpbuf.buf[0].size);
2773 if (mbuf == NULL) {
2774 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate header\n");
2775 goto clear_pipe;
2776 }
2777 tmpbuf.buf[0].mem = mbuf;
2778
2779 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2780 if (res < 0)
2781 goto clear_pipe;
2782
2783 in = mbuf;
2784 } else {
2785 in = buf->mem;
2786 }
2787
2788 /* Cleanup opcode most significant bits used by FUSE BPF */
2789 opcode_filter = in->opcode & ~FUSE_OPCODE_FILTER;
2790 in->opcode &= FUSE_OPCODE_FILTER;
2791
2792 if (se->debug) {
2793 fuse_log(FUSE_LOG_DEBUG,
2794 "unique: %llu, opcode: %s (%i), opcode filter: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2795 (unsigned long long) in->unique,
2796 opname((enum fuse_opcode) in->opcode), in->opcode,
2797 opfiltername((enum fuse_opcode) opcode_filter), opcode_filter,
2798 (unsigned long long) in->nodeid, buf->size, in->pid);
2799 }
2800
2801 req = fuse_ll_alloc_req(se);
2802 if (req == NULL) {
2803 struct fuse_out_header out = {
2804 .unique = in->unique,
2805 .error = -ENOMEM,
2806 };
2807 struct iovec iov = {
2808 .iov_base = &out,
2809 .iov_len = sizeof(struct fuse_out_header),
2810 };
2811
2812 fuse_send_msg(se, ch, &iov, 1);
2813 goto clear_pipe;
2814 }
2815
2816 req->unique = in->unique;
2817 req->ctx.uid = in->uid;
2818 req->ctx.gid = in->gid;
2819 req->ctx.pid = in->pid;
2820 req->ch = ch ? fuse_chan_get(ch) : NULL;
2821
2822 err = EIO;
2823 if (!se->got_init) {
2824 enum fuse_opcode expected;
2825
2826 expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2827 if (in->opcode != expected)
2828 goto reply_err;
2829 } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2830 goto reply_err;
2831
2832 err = EACCES;
2833 /* Implement -o allow_root */
2834 if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2835 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2836 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2837 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2838 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2839 in->opcode != FUSE_NOTIFY_REPLY &&
2840 in->opcode != FUSE_READDIRPLUS)
2841 goto reply_err;
2842
2843 err = ENOSYS;
2844 if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2845 goto reply_err;
2846 if (in->opcode != FUSE_INTERRUPT) {
2847 struct fuse_req *intr;
2848 pthread_mutex_lock(&se->lock);
2849 intr = check_interrupt(se, req);
2850 list_add_req(req, &se->list);
2851 pthread_mutex_unlock(&se->lock);
2852 if (intr)
2853 fuse_reply_err(intr, EAGAIN);
2854 }
2855
2856 if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2857 (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2858 in->opcode != FUSE_NOTIFY_REPLY) {
2859 void *newmbuf;
2860
2861 err = ENOMEM;
2862 newmbuf = realloc(mbuf, buf->size);
2863 if (newmbuf == NULL)
2864 goto reply_err;
2865 mbuf = newmbuf;
2866
2867 tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2868 tmpbuf.buf[0].mem = (char *)mbuf + write_header_size;
2869
2870 res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2871 err = -res;
2872 if (res < 0)
2873 goto reply_err;
2874
2875 in = mbuf;
2876 }
2877
2878 inarg = (void *) &in[1];
2879 if (in->opcode == FUSE_WRITE && se->op.write_buf)
2880 do_write_buf(req, in->nodeid, inarg, buf);
2881 else if (in->opcode == FUSE_NOTIFY_REPLY)
2882 do_notify_reply(req, in->nodeid, inarg, buf);
2883 else if (!opcode_filter)
2884 fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2885 else if (opcode_filter == FUSE_PREFILTER && fuse_ll_prefilter_ops[in->opcode].func)
2886 fuse_ll_prefilter_ops[in->opcode].func(req, in->nodeid, inarg);
2887 else if (opcode_filter == FUSE_POSTFILTER
2888 && fuse_ll_postfilter_ops[in->opcode].func)
2889 fuse_ll_postfilter_ops[in->opcode].func(
2890 req, in->nodeid, in->error_in, inarg,
2891 buf->size - sizeof(struct fuse_in_header));
2892 else {
2893 fuse_log(FUSE_LOG_ERR, "Bad opcode");
2894 err = ENOSYS;
2895 goto reply_err;
2896 }
2897
2898 out_free:
2899 free(mbuf);
2900 return;
2901
2902 reply_err:
2903 fuse_reply_err(req, err);
2904 clear_pipe:
2905 if (buf->flags & FUSE_BUF_IS_FD)
2906 fuse_ll_clear_pipe(se);
2907 goto out_free;
2908 }
2909
2910 #define LL_OPTION(n,o,v) \
2911 { n, offsetof(struct fuse_session, o), v }
2912
2913 static const struct fuse_opt fuse_ll_opts[] = {
2914 LL_OPTION("debug", debug, 1),
2915 LL_OPTION("-d", debug, 1),
2916 LL_OPTION("--debug", debug, 1),
2917 LL_OPTION("allow_root", deny_others, 1),
2918 FUSE_OPT_END
2919 };
2920
fuse_lowlevel_version(void)2921 void fuse_lowlevel_version(void)
2922 {
2923 printf("using FUSE kernel interface version %i.%i\n",
2924 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2925 fuse_mount_version();
2926 }
2927
fuse_lowlevel_help(void)2928 void fuse_lowlevel_help(void)
2929 {
2930 /* These are not all options, but the ones that are
2931 potentially of interest to an end-user */
2932 printf(
2933 " -o allow_other allow access by all users\n"
2934 " -o allow_root allow access by root\n"
2935 " -o auto_unmount auto unmount on process termination\n");
2936 }
2937
fuse_session_destroy(struct fuse_session * se)2938 void fuse_session_destroy(struct fuse_session *se)
2939 {
2940 struct fuse_ll_pipe *llp;
2941
2942 if (se->got_init && !se->got_destroy) {
2943 if (se->op.destroy)
2944 se->op.destroy(se->userdata);
2945 }
2946 llp = pthread_getspecific(se->pipe_key);
2947 if (llp != NULL)
2948 fuse_ll_pipe_free(llp);
2949 pthread_key_delete(se->pipe_key);
2950 pthread_mutex_destroy(&se->lock);
2951 free(se->cuse_data);
2952 if (se->fd != -1)
2953 close(se->fd);
2954 destroy_mount_opts(se->mo);
2955 free(se);
2956 }
2957
2958
fuse_ll_pipe_destructor(void * data)2959 static void fuse_ll_pipe_destructor(void *data)
2960 {
2961 struct fuse_ll_pipe *llp = data;
2962 fuse_ll_pipe_free(llp);
2963 }
2964
fuse_session_receive_buf(struct fuse_session * se,struct fuse_buf * buf)2965 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
2966 {
2967 return fuse_session_receive_buf_int(se, buf, NULL);
2968 }
2969
fuse_session_receive_buf_int(struct fuse_session * se,struct fuse_buf * buf,struct fuse_chan * ch)2970 int fuse_session_receive_buf_int(struct fuse_session *se, struct fuse_buf *buf,
2971 struct fuse_chan *ch)
2972 {
2973 int err;
2974 ssize_t res;
2975 #ifdef HAVE_SPLICE
2976 size_t bufsize = se->bufsize;
2977 struct fuse_ll_pipe *llp;
2978 struct fuse_buf tmpbuf;
2979
2980 if (se->conn.proto_minor < 14 || !(se->conn.want & FUSE_CAP_SPLICE_READ))
2981 goto fallback;
2982
2983 llp = fuse_ll_get_pipe(se);
2984 if (llp == NULL)
2985 goto fallback;
2986
2987 if (llp->size < bufsize) {
2988 if (llp->can_grow) {
2989 res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2990 if (res == -1) {
2991 llp->can_grow = 0;
2992 res = grow_pipe_to_max(llp->pipe[0]);
2993 if (res > 0)
2994 llp->size = res;
2995 goto fallback;
2996 }
2997 llp->size = res;
2998 }
2999 if (llp->size < bufsize)
3000 goto fallback;
3001 }
3002
3003 res = splice(ch ? ch->fd : se->fd,
3004 NULL, llp->pipe[1], NULL, bufsize, 0);
3005 err = errno;
3006
3007 if (fuse_session_exited(se))
3008 return 0;
3009
3010 if (res == -1) {
3011 if (err == ENODEV) {
3012 /* Filesystem was unmounted, or connection was aborted
3013 via /sys/fs/fuse/connections */
3014 fuse_session_exit(se);
3015 return 0;
3016 }
3017 if (err != EINTR && err != EAGAIN)
3018 perror("fuse: splice from device");
3019 return -err;
3020 }
3021
3022 if (res < sizeof(struct fuse_in_header)) {
3023 fuse_log(FUSE_LOG_ERR, "short splice from fuse device\n");
3024 return -EIO;
3025 }
3026
3027 tmpbuf = (struct fuse_buf) {
3028 .size = res,
3029 .flags = FUSE_BUF_IS_FD,
3030 .fd = llp->pipe[0],
3031 };
3032
3033 /*
3034 * Don't bother with zero copy for small requests.
3035 * fuse_loop_mt() needs to check for FORGET so this more than
3036 * just an optimization.
3037 */
3038 if (res < sizeof(struct fuse_in_header) +
3039 sizeof(struct fuse_write_in) + pagesize) {
3040 struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
3041 struct fuse_bufvec dst = { .count = 1 };
3042
3043 if (!buf->mem) {
3044 buf->mem = malloc(se->bufsize);
3045 if (!buf->mem) {
3046 fuse_log(FUSE_LOG_ERR,
3047 "fuse: failed to allocate read buffer\n");
3048 return -ENOMEM;
3049 }
3050 }
3051 buf->size = se->bufsize;
3052 buf->flags = 0;
3053 dst.buf[0] = *buf;
3054
3055 res = fuse_buf_copy(&dst, &src, 0);
3056 if (res < 0) {
3057 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n",
3058 strerror(-res));
3059 fuse_ll_clear_pipe(se);
3060 return res;
3061 }
3062 if (res < tmpbuf.size) {
3063 fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
3064 fuse_ll_clear_pipe(se);
3065 return -EIO;
3066 }
3067 assert(res == tmpbuf.size);
3068
3069 } else {
3070 /* Don't overwrite buf->mem, as that would cause a leak */
3071 buf->fd = tmpbuf.fd;
3072 buf->flags = tmpbuf.flags;
3073 }
3074 buf->size = tmpbuf.size;
3075
3076 return res;
3077
3078 fallback:
3079 #endif
3080 if (!buf->mem) {
3081 buf->mem = malloc(se->bufsize);
3082 if (!buf->mem) {
3083 fuse_log(FUSE_LOG_ERR,
3084 "fuse: failed to allocate read buffer\n");
3085 return -ENOMEM;
3086 }
3087 }
3088
3089 restart:
3090 res = read(ch ? ch->fd : se->fd, buf->mem, se->bufsize);
3091 err = errno;
3092
3093 if (fuse_session_exited(se))
3094 return 0;
3095 if (res == -1) {
3096 /* ENOENT means the operation was interrupted, it's safe
3097 to restart */
3098 if (err == ENOENT)
3099 goto restart;
3100
3101 if (err == ENODEV) {
3102 /* Filesystem was unmounted, or connection was aborted
3103 via /sys/fs/fuse/connections */
3104 fuse_session_exit(se);
3105 return 0;
3106 }
3107 /* Errors occurring during normal operation: EINTR (read
3108 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
3109 umounted) */
3110 if (err != EINTR && err != EAGAIN)
3111 perror("fuse: reading device");
3112 return -err;
3113 }
3114 if ((size_t) res < sizeof(struct fuse_in_header)) {
3115 fuse_log(FUSE_LOG_ERR, "short read on fuse device\n");
3116 return -EIO;
3117 }
3118
3119 buf->size = res;
3120
3121 return res;
3122 }
3123
fuse_session_new(struct fuse_args * args,const struct fuse_lowlevel_ops * op,size_t op_size,void * userdata)3124 struct fuse_session *fuse_session_new(struct fuse_args *args,
3125 const struct fuse_lowlevel_ops *op,
3126 size_t op_size, void *userdata)
3127 {
3128 int err;
3129 struct fuse_session *se;
3130 struct mount_opts *mo;
3131
3132 if (sizeof(struct fuse_lowlevel_ops) < op_size) {
3133 fuse_log(FUSE_LOG_ERR, "fuse: warning: library too old, some operations may not work\n");
3134 op_size = sizeof(struct fuse_lowlevel_ops);
3135 }
3136
3137 if (args->argc == 0) {
3138 fuse_log(FUSE_LOG_ERR, "fuse: empty argv passed to fuse_session_new().\n");
3139 return NULL;
3140 }
3141
3142 se = (struct fuse_session *) calloc(1, sizeof(struct fuse_session));
3143 if (se == NULL) {
3144 fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
3145 goto out1;
3146 }
3147 se->fd = -1;
3148 se->conn.max_write = UINT_MAX;
3149 se->conn.max_readahead = UINT_MAX;
3150
3151 /* Parse options */
3152 if(fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1)
3153 goto out2;
3154 if(se->deny_others) {
3155 /* Allowing access only by root is done by instructing
3156 * kernel to allow access by everyone, and then restricting
3157 * access to root and mountpoint owner in libfuse.
3158 */
3159 // We may be adding the option a second time, but
3160 // that doesn't hurt.
3161 if(fuse_opt_add_arg(args, "-oallow_other") == -1)
3162 goto out2;
3163 }
3164 mo = parse_mount_opts(args);
3165 if (mo == NULL)
3166 goto out3;
3167
3168 if(args->argc == 1 &&
3169 args->argv[0][0] == '-') {
3170 fuse_log(FUSE_LOG_ERR, "fuse: warning: argv[0] looks like an option, but "
3171 "will be ignored\n");
3172 } else if (args->argc != 1) {
3173 int i;
3174 fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
3175 for(i = 1; i < args->argc-1; i++)
3176 fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
3177 fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
3178 goto out4;
3179 }
3180
3181 if (se->debug)
3182 fuse_log(FUSE_LOG_DEBUG, "FUSE library version: %s\n", PACKAGE_VERSION);
3183
3184 se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
3185 FUSE_BUFFER_HEADER_SIZE;
3186
3187 list_init_req(&se->list);
3188 list_init_req(&se->interrupts);
3189 list_init_nreq(&se->notify_list);
3190 se->notify_ctr = 1;
3191 pthread_mutex_init(&se->lock, NULL);
3192
3193 err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
3194 if (err) {
3195 fuse_log(FUSE_LOG_ERR, "fuse: failed to create thread specific key: %s\n",
3196 strerror(err));
3197 goto out5;
3198 }
3199
3200 memcpy(&se->op, op, op_size);
3201 se->owner = getuid();
3202 se->userdata = userdata;
3203
3204 se->mo = mo;
3205 return se;
3206
3207 out5:
3208 pthread_mutex_destroy(&se->lock);
3209 out4:
3210 fuse_opt_free_args(args);
3211 out3:
3212 if (mo != NULL)
3213 destroy_mount_opts(mo);
3214 out2:
3215 free(se);
3216 out1:
3217 return NULL;
3218 }
3219
fuse_session_mount(struct fuse_session * se,const char * mountpoint)3220 int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
3221 {
3222 int fd;
3223
3224 /*
3225 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
3226 * would ensue.
3227 */
3228 do {
3229 fd = open("/dev/null", O_RDWR);
3230 if (fd > 2)
3231 close(fd);
3232 } while (fd >= 0 && fd <= 2);
3233
3234 /*
3235 * To allow FUSE daemons to run without privileges, the caller may open
3236 * /dev/fuse before launching the file system and pass on the file
3237 * descriptor by specifying /dev/fd/N as the mount point. Note that the
3238 * parent process takes care of performing the mount in this case.
3239 */
3240 fd = fuse_mnt_parse_fuse_fd(mountpoint);
3241 if (fd != -1) {
3242 if (fcntl(fd, F_GETFD) == -1) {
3243 fuse_log(FUSE_LOG_ERR,
3244 "fuse: Invalid file descriptor /dev/fd/%u\n",
3245 fd);
3246 return -1;
3247 }
3248 se->fd = fd;
3249 return 0;
3250 }
3251
3252 /* Open channel */
3253 fd = fuse_kern_mount(mountpoint, se->mo);
3254 if (fd == -1)
3255 return -1;
3256 se->fd = fd;
3257
3258 /* Save mountpoint */
3259 se->mountpoint = strdup(mountpoint);
3260 if (se->mountpoint == NULL)
3261 goto error_out;
3262
3263 return 0;
3264
3265 error_out:
3266 fuse_kern_unmount(mountpoint, fd);
3267 return -1;
3268 }
3269
fuse_session_fd(struct fuse_session * se)3270 int fuse_session_fd(struct fuse_session *se)
3271 {
3272 return se->fd;
3273 }
3274
fuse_session_unmount(struct fuse_session * se)3275 void fuse_session_unmount(struct fuse_session *se)
3276 {
3277 if (se->mountpoint != NULL) {
3278 fuse_kern_unmount(se->mountpoint, se->fd);
3279 se->fd = -1;
3280 free(se->mountpoint);
3281 se->mountpoint = NULL;
3282 }
3283 }
3284
3285 #ifdef linux
fuse_req_getgroups(fuse_req_t req,int size,gid_t list[])3286 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3287 {
3288 char *buf;
3289 size_t bufsize = 1024;
3290 char path[128];
3291 int ret;
3292 int fd;
3293 unsigned long pid = req->ctx.pid;
3294 char *s;
3295
3296 sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
3297
3298 retry:
3299 buf = malloc(bufsize);
3300 if (buf == NULL)
3301 return -ENOMEM;
3302
3303 ret = -EIO;
3304 fd = open(path, O_RDONLY);
3305 if (fd == -1)
3306 goto out_free;
3307
3308 ret = read(fd, buf, bufsize);
3309 close(fd);
3310 if (ret < 0) {
3311 ret = -EIO;
3312 goto out_free;
3313 }
3314
3315 if ((size_t)ret == bufsize) {
3316 free(buf);
3317 bufsize *= 4;
3318 goto retry;
3319 }
3320
3321 ret = -EIO;
3322 s = strstr(buf, "\nGroups:");
3323 if (s == NULL)
3324 goto out_free;
3325
3326 s += 8;
3327 ret = 0;
3328 while (1) {
3329 char *end;
3330 unsigned long val = strtoul(s, &end, 0);
3331 if (end == s)
3332 break;
3333
3334 s = end;
3335 if (ret < size)
3336 list[ret] = val;
3337 ret++;
3338 }
3339
3340 out_free:
3341 free(buf);
3342 return ret;
3343 }
3344 #else /* linux */
3345 /*
3346 * This is currently not implemented on other than Linux...
3347 */
fuse_req_getgroups(fuse_req_t req,int size,gid_t list[])3348 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3349 {
3350 (void) req; (void) size; (void) list;
3351 return -ENOSYS;
3352 }
3353 #endif
3354
3355 /* Prevent spurious data race warning - we don't care
3356 * about races for this flag */
3357 __attribute__((no_sanitize_thread))
fuse_session_exit(struct fuse_session * se)3358 void fuse_session_exit(struct fuse_session *se)
3359 {
3360 se->exited = 1;
3361 }
3362
3363 __attribute__((no_sanitize_thread))
fuse_session_reset(struct fuse_session * se)3364 void fuse_session_reset(struct fuse_session *se)
3365 {
3366 se->exited = 0;
3367 se->error = 0;
3368 }
3369
3370 __attribute__((no_sanitize_thread))
fuse_session_exited(struct fuse_session * se)3371 int fuse_session_exited(struct fuse_session *se)
3372 {
3373 return se->exited;
3374 }
3375