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