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  if (!(arg->flags & FUSE_MAX_PAGES)) {
1913  size_t max_bufsize =
1914  FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
1915  + FUSE_BUFFER_HEADER_SIZE;
1916  if (bufsize > max_bufsize) {
1917  bufsize = max_bufsize;
1918  }
1919  }
1920  } else {
1921  se->conn.max_readahead = 0;
1922  }
1923 
1924  if (se->conn.proto_minor >= 14) {
1925 #ifdef HAVE_SPLICE
1926 #ifdef HAVE_VMSPLICE
1927  se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
1928 #endif
1929  se->conn.capable |= FUSE_CAP_SPLICE_READ;
1930 #endif
1931  }
1932  if (se->conn.proto_minor >= 18)
1933  se->conn.capable |= FUSE_CAP_IOCTL_DIR;
1934 
1935  /* Default settings for modern filesystems.
1936  *
1937  * Most of these capabilities were disabled by default in
1938  * libfuse2 for backwards compatibility reasons. In libfuse3,
1939  * we can finally enable them by default (as long as they're
1940  * supported by the kernel).
1941  */
1942 #define LL_SET_DEFAULT(cond, cap) \
1943  if ((cond) && (se->conn.capable & (cap))) \
1944  se->conn.want |= (cap)
1945  LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
1946  LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
1947  LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
1948  LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
1949  LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
1950  LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
1951  LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
1952  LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
1953  LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
1955  LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
1956  LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
1957  LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
1959  se->conn.time_gran = 1;
1960 
1961  if (bufsize < FUSE_MIN_READ_BUFFER) {
1962  fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
1963  bufsize);
1964  bufsize = FUSE_MIN_READ_BUFFER;
1965  }
1966  se->bufsize = bufsize;
1967 
1968  if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
1969  se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
1970 
1971  se->got_init = 1;
1972  if (se->op.init)
1973  se->op.init(se->userdata, &se->conn);
1974 
1975  if (se->conn.want & (~se->conn.capable)) {
1976  fprintf(stderr, "fuse: error: filesystem requested capabilities "
1977  "0x%x that are not supported by kernel, aborting.\n",
1978  se->conn.want & (~se->conn.capable));
1979  fuse_reply_err(req, EPROTO);
1980  se->error = -EPROTO;
1981  fuse_session_exit(se);
1982  return;
1983  }
1984 
1985  unsigned max_read_mo = get_max_read(se->mo);
1986  if (se->conn.max_read != max_read_mo) {
1987  fprintf(stderr, "fuse: error: init() and fuse_session_new() "
1988  "requested different maximum read size (%u vs %u)\n",
1989  se->conn.max_read, max_read_mo);
1990  fuse_reply_err(req, EPROTO);
1991  se->error = -EPROTO;
1992  fuse_session_exit(se);
1993  return;
1994  }
1995 
1996  if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
1997  se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
1998  }
1999  if (arg->flags & FUSE_MAX_PAGES) {
2000  outarg.flags |= FUSE_MAX_PAGES;
2001  outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2002  }
2003 
2004  /* Always enable big writes, this is superseded
2005  by the max_write option */
2006  outarg.flags |= FUSE_BIG_WRITES;
2007 
2008  if (se->conn.want & FUSE_CAP_ASYNC_READ)
2009  outarg.flags |= FUSE_ASYNC_READ;
2010  if (se->conn.want & FUSE_CAP_POSIX_LOCKS)
2011  outarg.flags |= FUSE_POSIX_LOCKS;
2012  if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2013  outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2014  if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2015  outarg.flags |= FUSE_EXPORT_SUPPORT;
2016  if (se->conn.want & FUSE_CAP_DONT_MASK)
2017  outarg.flags |= FUSE_DONT_MASK;
2018  if (se->conn.want & FUSE_CAP_FLOCK_LOCKS)
2019  outarg.flags |= FUSE_FLOCK_LOCKS;
2020  if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2021  outarg.flags |= FUSE_AUTO_INVAL_DATA;
2022  if (se->conn.want & FUSE_CAP_READDIRPLUS)
2023  outarg.flags |= FUSE_DO_READDIRPLUS;
2024  if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2025  outarg.flags |= FUSE_READDIRPLUS_AUTO;
2026  if (se->conn.want & FUSE_CAP_ASYNC_DIO)
2027  outarg.flags |= FUSE_ASYNC_DIO;
2028  if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2029  outarg.flags |= FUSE_WRITEBACK_CACHE;
2030  if (se->conn.want & FUSE_CAP_POSIX_ACL)
2031  outarg.flags |= FUSE_POSIX_ACL;
2032  outarg.max_readahead = se->conn.max_readahead;
2033  outarg.max_write = se->conn.max_write;
2034  if (se->conn.proto_minor >= 13) {
2035  if (se->conn.max_background >= (1 << 16))
2036  se->conn.max_background = (1 << 16) - 1;
2037  if (se->conn.congestion_threshold > se->conn.max_background)
2038  se->conn.congestion_threshold = se->conn.max_background;
2039  if (!se->conn.congestion_threshold) {
2040  se->conn.congestion_threshold =
2041  se->conn.max_background * 3 / 4;
2042  }
2043 
2044  outarg.max_background = se->conn.max_background;
2045  outarg.congestion_threshold = se->conn.congestion_threshold;
2046  }
2047  if (se->conn.proto_minor >= 23)
2048  outarg.time_gran = se->conn.time_gran;
2049 
2050  if (se->debug) {
2051  fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
2052  fprintf(stderr, " flags=0x%08x\n", outarg.flags);
2053  fprintf(stderr, " max_readahead=0x%08x\n",
2054  outarg.max_readahead);
2055  fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
2056  fprintf(stderr, " max_background=%i\n",
2057  outarg.max_background);
2058  fprintf(stderr, " congestion_threshold=%i\n",
2059  outarg.congestion_threshold);
2060  fprintf(stderr, " time_gran=%u\n",
2061  outarg.time_gran);
2062  }
2063  if (arg->minor < 5)
2064  outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2065  else if (arg->minor < 23)
2066  outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2067 
2068  send_reply_ok(req, &outarg, outargsize);
2069 }
2070 
2071 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2072 {
2073  struct fuse_session *se = req->se;
2074 
2075  (void) nodeid;
2076  (void) inarg;
2077 
2078  se->got_destroy = 1;
2079  if (se->op.destroy)
2080  se->op.destroy(se->userdata);
2081 
2082  send_reply_ok(req, NULL, 0);
2083 }
2084 
2085 static void list_del_nreq(struct fuse_notify_req *nreq)
2086 {
2087  struct fuse_notify_req *prev = nreq->prev;
2088  struct fuse_notify_req *next = nreq->next;
2089  prev->next = next;
2090  next->prev = prev;
2091 }
2092 
2093 static void list_add_nreq(struct fuse_notify_req *nreq,
2094  struct fuse_notify_req *next)
2095 {
2096  struct fuse_notify_req *prev = next->prev;
2097  nreq->next = next;
2098  nreq->prev = prev;
2099  prev->next = nreq;
2100  next->prev = nreq;
2101 }
2102 
2103 static void list_init_nreq(struct fuse_notify_req *nreq)
2104 {
2105  nreq->next = nreq;
2106  nreq->prev = nreq;
2107 }
2108 
2109 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2110  const void *inarg, const struct fuse_buf *buf)
2111 {
2112  struct fuse_session *se = req->se;
2113  struct fuse_notify_req *nreq;
2114  struct fuse_notify_req *head;
2115 
2116  pthread_mutex_lock(&se->lock);
2117  head = &se->notify_list;
2118  for (nreq = head->next; nreq != head; nreq = nreq->next) {
2119  if (nreq->unique == req->unique) {
2120  list_del_nreq(nreq);
2121  break;
2122  }
2123  }
2124  pthread_mutex_unlock(&se->lock);
2125 
2126  if (nreq != head)
2127  nreq->reply(nreq, req, nodeid, inarg, buf);
2128 }
2129 
2130 static int send_notify_iov(struct fuse_session *se, int notify_code,
2131  struct iovec *iov, int count)
2132 {
2133  struct fuse_out_header out;
2134 
2135  if (!se->got_init)
2136  return -ENOTCONN;
2137 
2138  out.unique = 0;
2139  out.error = notify_code;
2140  iov[0].iov_base = &out;
2141  iov[0].iov_len = sizeof(struct fuse_out_header);
2142 
2143  return fuse_send_msg(se, NULL, iov, count);
2144 }
2145 
2146 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2147 {
2148  if (ph != NULL) {
2149  struct fuse_notify_poll_wakeup_out outarg;
2150  struct iovec iov[2];
2151 
2152  outarg.kh = ph->kh;
2153 
2154  iov[1].iov_base = &outarg;
2155  iov[1].iov_len = sizeof(outarg);
2156 
2157  return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2158  } else {
2159  return 0;
2160  }
2161 }
2162 
2163 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2164  off_t off, off_t len)
2165 {
2166  struct fuse_notify_inval_inode_out outarg;
2167  struct iovec iov[2];
2168 
2169  if (!se)
2170  return -EINVAL;
2171 
2172  if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2173  return -ENOSYS;
2174 
2175  outarg.ino = ino;
2176  outarg.off = off;
2177  outarg.len = len;
2178 
2179  iov[1].iov_base = &outarg;
2180  iov[1].iov_len = sizeof(outarg);
2181 
2182  return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2183 }
2184 
2185 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2186  const char *name, size_t namelen)
2187 {
2188  struct fuse_notify_inval_entry_out outarg;
2189  struct iovec iov[3];
2190 
2191  if (!se)
2192  return -EINVAL;
2193 
2194  if (se->conn.proto_major < 6 || se->conn.proto_minor < 12)
2195  return -ENOSYS;
2196 
2197  outarg.parent = parent;
2198  outarg.namelen = namelen;
2199  outarg.padding = 0;
2200 
2201  iov[1].iov_base = &outarg;
2202  iov[1].iov_len = sizeof(outarg);
2203  iov[2].iov_base = (void *)name;
2204  iov[2].iov_len = namelen + 1;
2205 
2206  return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2207 }
2208 
2209 int fuse_lowlevel_notify_delete(struct fuse_session *se,
2210  fuse_ino_t parent, fuse_ino_t child,
2211  const char *name, size_t namelen)
2212 {
2213  struct fuse_notify_delete_out outarg;
2214  struct iovec iov[3];
2215 
2216  if (!se)
2217  return -EINVAL;
2218 
2219  if (se->conn.proto_major < 6 || se->conn.proto_minor < 18)
2220  return -ENOSYS;
2221 
2222  outarg.parent = parent;
2223  outarg.child = child;
2224  outarg.namelen = namelen;
2225  outarg.padding = 0;
2226 
2227  iov[1].iov_base = &outarg;
2228  iov[1].iov_len = sizeof(outarg);
2229  iov[2].iov_base = (void *)name;
2230  iov[2].iov_len = namelen + 1;
2231 
2232  return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2233 }
2234 
2235 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2236  off_t offset, struct fuse_bufvec *bufv,
2237  enum fuse_buf_copy_flags flags)
2238 {
2239  struct fuse_out_header out;
2240  struct fuse_notify_store_out outarg;
2241  struct iovec iov[3];
2242  size_t size = fuse_buf_size(bufv);
2243  int res;
2244 
2245  if (!se)
2246  return -EINVAL;
2247 
2248  if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2249  return -ENOSYS;
2250 
2251  out.unique = 0;
2252  out.error = FUSE_NOTIFY_STORE;
2253 
2254  outarg.nodeid = ino;
2255  outarg.offset = offset;
2256  outarg.size = size;
2257  outarg.padding = 0;
2258 
2259  iov[0].iov_base = &out;
2260  iov[0].iov_len = sizeof(out);
2261  iov[1].iov_base = &outarg;
2262  iov[1].iov_len = sizeof(outarg);
2263 
2264  res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2265  if (res > 0)
2266  res = -res;
2267 
2268  return res;
2269 }
2270 
2271 struct fuse_retrieve_req {
2272  struct fuse_notify_req nreq;
2273  void *cookie;
2274 };
2275 
2276 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2277  fuse_req_t req, fuse_ino_t ino,
2278  const void *inarg,
2279  const struct fuse_buf *ibuf)
2280 {
2281  struct fuse_session *se = req->se;
2282  struct fuse_retrieve_req *rreq =
2283  container_of(nreq, struct fuse_retrieve_req, nreq);
2284  const struct fuse_notify_retrieve_in *arg = inarg;
2285  struct fuse_bufvec bufv = {
2286  .buf[0] = *ibuf,
2287  .count = 1,
2288  };
2289 
2290  if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2291  bufv.buf[0].mem = PARAM(arg);
2292 
2293  bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2294  sizeof(struct fuse_notify_retrieve_in);
2295 
2296  if (bufv.buf[0].size < arg->size) {
2297  fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
2298  fuse_reply_none(req);
2299  goto out;
2300  }
2301  bufv.buf[0].size = arg->size;
2302 
2303  if (se->op.retrieve_reply) {
2304  se->op.retrieve_reply(req, rreq->cookie, ino,
2305  arg->offset, &bufv);
2306  } else {
2307  fuse_reply_none(req);
2308  }
2309 out:
2310  free(rreq);
2311  if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2312  fuse_ll_clear_pipe(se);
2313 }
2314 
2315 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
2316  size_t size, off_t offset, void *cookie)
2317 {
2318  struct fuse_notify_retrieve_out outarg;
2319  struct iovec iov[2];
2320  struct fuse_retrieve_req *rreq;
2321  int err;
2322 
2323  if (!se)
2324  return -EINVAL;
2325 
2326  if (se->conn.proto_major < 6 || se->conn.proto_minor < 15)
2327  return -ENOSYS;
2328 
2329  rreq = malloc(sizeof(*rreq));
2330  if (rreq == NULL)
2331  return -ENOMEM;
2332 
2333  pthread_mutex_lock(&se->lock);
2334  rreq->cookie = cookie;
2335  rreq->nreq.unique = se->notify_ctr++;
2336  rreq->nreq.reply = fuse_ll_retrieve_reply;
2337  list_add_nreq(&rreq->nreq, &se->notify_list);
2338  pthread_mutex_unlock(&se->lock);
2339 
2340  outarg.notify_unique = rreq->nreq.unique;
2341  outarg.nodeid = ino;
2342  outarg.offset = offset;
2343  outarg.size = size;
2344  outarg.padding = 0;
2345 
2346  iov[1].iov_base = &outarg;
2347  iov[1].iov_len = sizeof(outarg);
2348 
2349  err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2350  if (err) {
2351  pthread_mutex_lock(&se->lock);
2352  list_del_nreq(&rreq->nreq);
2353  pthread_mutex_unlock(&se->lock);
2354  free(rreq);
2355  }
2356 
2357  return err;
2358 }
2359 
2361 {
2362  return req->se->userdata;
2363 }
2364 
2366 {
2367  return &req->ctx;
2368 }
2369 
2371  void *data)
2372 {
2373  pthread_mutex_lock(&req->lock);
2374  pthread_mutex_lock(&req->se->lock);
2375  req->u.ni.func = func;
2376  req->u.ni.data = data;
2377  pthread_mutex_unlock(&req->se->lock);
2378  if (req->interrupted && func)
2379  func(req, data);
2380  pthread_mutex_unlock(&req->lock);
2381 }
2382 
2384 {
2385  int interrupted;
2386 
2387  pthread_mutex_lock(&req->se->lock);
2388  interrupted = req->interrupted;
2389  pthread_mutex_unlock(&req->se->lock);
2390 
2391  return interrupted;
2392 }
2393 
2394 static struct {
2395  void (*func)(fuse_req_t, fuse_ino_t, const void *);
2396  const char *name;
2397 } fuse_ll_ops[] = {
2398  [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
2399  [FUSE_FORGET] = { do_forget, "FORGET" },
2400  [FUSE_GETATTR] = { do_getattr, "GETATTR" },
2401  [FUSE_SETATTR] = { do_setattr, "SETATTR" },
2402  [FUSE_READLINK] = { do_readlink, "READLINK" },
2403  [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
2404  [FUSE_MKNOD] = { do_mknod, "MKNOD" },
2405  [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
2406  [FUSE_UNLINK] = { do_unlink, "UNLINK" },
2407  [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
2408  [FUSE_RENAME] = { do_rename, "RENAME" },
2409  [FUSE_LINK] = { do_link, "LINK" },
2410  [FUSE_OPEN] = { do_open, "OPEN" },
2411  [FUSE_READ] = { do_read, "READ" },
2412  [FUSE_WRITE] = { do_write, "WRITE" },
2413  [FUSE_STATFS] = { do_statfs, "STATFS" },
2414  [FUSE_RELEASE] = { do_release, "RELEASE" },
2415  [FUSE_FSYNC] = { do_fsync, "FSYNC" },
2416  [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
2417  [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
2418  [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
2419  [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2420  [FUSE_FLUSH] = { do_flush, "FLUSH" },
2421  [FUSE_INIT] = { do_init, "INIT" },
2422  [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
2423  [FUSE_READDIR] = { do_readdir, "READDIR" },
2424  [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
2425  [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
2426  [FUSE_GETLK] = { do_getlk, "GETLK" },
2427  [FUSE_SETLK] = { do_setlk, "SETLK" },
2428  [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
2429  [FUSE_ACCESS] = { do_access, "ACCESS" },
2430  [FUSE_CREATE] = { do_create, "CREATE" },
2431  [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
2432  [FUSE_BMAP] = { do_bmap, "BMAP" },
2433  [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
2434  [FUSE_POLL] = { do_poll, "POLL" },
2435  [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
2436  [FUSE_DESTROY] = { do_destroy, "DESTROY" },
2437  [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
2438  [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2439  [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS"},
2440  [FUSE_RENAME2] = { do_rename2, "RENAME2" },
2441  [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2442  [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
2443 };
2444 
2445 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2446 
2447 static const char *opname(enum fuse_opcode opcode)
2448 {
2449  if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2450  return "???";
2451  else
2452  return fuse_ll_ops[opcode].name;
2453 }
2454 
2455 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2456  struct fuse_bufvec *src)
2457 {
2458  ssize_t res = fuse_buf_copy(dst, src, 0);
2459  if (res < 0) {
2460  fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
2461  return res;
2462  }
2463  if ((size_t)res < fuse_buf_size(dst)) {
2464  fprintf(stderr, "fuse: copy from pipe: short read\n");
2465  return -1;
2466  }
2467  return 0;
2468 }
2469 
2470 void fuse_session_process_buf(struct fuse_session *se,
2471  const struct fuse_buf *buf)
2472 {
2473  fuse_session_process_buf_int(se, buf, NULL);
2474 }
2475 
2476 void fuse_session_process_buf_int(struct fuse_session *se,
2477  const struct fuse_buf *buf, struct fuse_chan *ch)
2478 {
2479  const size_t write_header_size = sizeof(struct fuse_in_header) +
2480  sizeof(struct fuse_write_in);
2481  struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2482  struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2483  struct fuse_in_header *in;
2484  const void *inarg;
2485  struct fuse_req *req;
2486  void *mbuf = NULL;
2487  int err;
2488  int res;
2489 
2490  if (buf->flags & FUSE_BUF_IS_FD) {
2491  if (buf->size < tmpbuf.buf[0].size)
2492  tmpbuf.buf[0].size = buf->size;
2493 
2494  mbuf = malloc(tmpbuf.buf[0].size);
2495  if (mbuf == NULL) {
2496  fprintf(stderr, "fuse: failed to allocate header\n");
2497  goto clear_pipe;
2498  }
2499  tmpbuf.buf[0].mem = mbuf;
2500 
2501  res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2502  if (res < 0)
2503  goto clear_pipe;
2504 
2505  in = mbuf;
2506  } else {
2507  in = buf->mem;
2508  }
2509 
2510  if (se->debug) {
2511  fprintf(stderr,
2512  "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2513  (unsigned long long) in->unique,
2514  opname((enum fuse_opcode) in->opcode), in->opcode,
2515  (unsigned long long) in->nodeid, buf->size, in->pid);
2516  }
2517 
2518  req = fuse_ll_alloc_req(se);
2519  if (req == NULL) {
2520  struct fuse_out_header out = {
2521  .unique = in->unique,
2522  .error = -ENOMEM,
2523  };
2524  struct iovec iov = {
2525  .iov_base = &out,
2526  .iov_len = sizeof(struct fuse_out_header),
2527  };
2528 
2529  fuse_send_msg(se, ch, &iov, 1);
2530  goto clear_pipe;
2531  }
2532 
2533  req->unique = in->unique;
2534  req->ctx.uid = in->uid;
2535  req->ctx.gid = in->gid;
2536  req->ctx.pid = in->pid;
2537  req->ch = ch ? fuse_chan_get(ch) : NULL;
2538 
2539  err = EIO;
2540  if (!se->got_init) {
2541  enum fuse_opcode expected;
2542 
2543  expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2544  if (in->opcode != expected)
2545  goto reply_err;
2546  } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2547  goto reply_err;
2548 
2549  err = EACCES;
2550  /* Implement -o allow_root */
2551  if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2552  in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2553  in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2554  in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2555  in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2556  in->opcode != FUSE_NOTIFY_REPLY &&
2557  in->opcode != FUSE_READDIRPLUS)
2558  goto reply_err;
2559 
2560  err = ENOSYS;
2561  if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2562  goto reply_err;
2563  if (in->opcode != FUSE_INTERRUPT) {
2564  struct fuse_req *intr;
2565  pthread_mutex_lock(&se->lock);
2566  intr = check_interrupt(se, req);
2567  list_add_req(req, &se->list);
2568  pthread_mutex_unlock(&se->lock);
2569  if (intr)
2570  fuse_reply_err(intr, EAGAIN);
2571  }
2572 
2573  if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2574  (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2575  in->opcode != FUSE_NOTIFY_REPLY) {
2576  void *newmbuf;
2577 
2578  err = ENOMEM;
2579  newmbuf = realloc(mbuf, buf->size);
2580  if (newmbuf == NULL)
2581  goto reply_err;
2582  mbuf = newmbuf;
2583 
2584  tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2585  tmpbuf.buf[0].mem = (char *)mbuf + write_header_size;
2586 
2587  res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2588  err = -res;
2589  if (res < 0)
2590  goto reply_err;
2591 
2592  in = mbuf;
2593  }
2594 
2595  inarg = (void *) &in[1];
2596  if (in->opcode == FUSE_WRITE && se->op.write_buf)
2597  do_write_buf(req, in->nodeid, inarg, buf);
2598  else if (in->opcode == FUSE_NOTIFY_REPLY)
2599  do_notify_reply(req, in->nodeid, inarg, buf);
2600  else
2601  fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2602 
2603 out_free:
2604  free(mbuf);
2605  return;
2606 
2607 reply_err:
2608  fuse_reply_err(req, err);
2609 clear_pipe:
2610  if (buf->flags & FUSE_BUF_IS_FD)
2611  fuse_ll_clear_pipe(se);
2612  goto out_free;
2613 }
2614 
2615 #define LL_OPTION(n,o,v) \
2616  { n, offsetof(struct fuse_session, o), v }
2617 
2618 static const struct fuse_opt fuse_ll_opts[] = {
2619  LL_OPTION("debug", debug, 1),
2620  LL_OPTION("-d", debug, 1),
2621  LL_OPTION("--debug", debug, 1),
2622  LL_OPTION("allow_root", deny_others, 1),
2623  FUSE_OPT_END
2624 };
2625 
2627 {
2628  printf("using FUSE kernel interface version %i.%i\n",
2629  FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2630  fuse_mount_version();
2631 }
2632 
2634 {
2635  /* These are not all options, but the ones that are
2636  potentially of interest to an end-user */
2637  printf(
2638 " -o allow_other allow access by all users\n"
2639 " -o allow_root allow access by root\n"
2640 " -o auto_unmount auto unmount on process termination\n");
2641 }
2642 
2643 void fuse_session_destroy(struct fuse_session *se)
2644 {
2645  struct fuse_ll_pipe *llp;
2646 
2647  if (se->got_init && !se->got_destroy) {
2648  if (se->op.destroy)
2649  se->op.destroy(se->userdata);
2650  }
2651  llp = pthread_getspecific(se->pipe_key);
2652  if (llp != NULL)
2653  fuse_ll_pipe_free(llp);
2654  pthread_key_delete(se->pipe_key);
2655  pthread_mutex_destroy(&se->lock);
2656  free(se->cuse_data);
2657  if (se->fd != -1)
2658  close(se->fd);
2659  destroy_mount_opts(se->mo);
2660  free(se);
2661 }
2662 
2663 
2664 static void fuse_ll_pipe_destructor(void *data)
2665 {
2666  struct fuse_ll_pipe *llp = data;
2667  fuse_ll_pipe_free(llp);
2668 }
2669 
2670 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
2671 {
2672  return fuse_session_receive_buf_int(se, buf, NULL);
2673 }
2674 
2675 int fuse_session_receive_buf_int(struct fuse_session *se, struct fuse_buf *buf,
2676  struct fuse_chan *ch)
2677 {
2678  int err;
2679  ssize_t res;
2680 #ifdef HAVE_SPLICE
2681  size_t bufsize = se->bufsize;
2682  struct fuse_ll_pipe *llp;
2683  struct fuse_buf tmpbuf;
2684 
2685  if (se->conn.proto_minor < 14 || !(se->conn.want & FUSE_CAP_SPLICE_READ))
2686  goto fallback;
2687 
2688  llp = fuse_ll_get_pipe(se);
2689  if (llp == NULL)
2690  goto fallback;
2691 
2692  if (llp->size < bufsize) {
2693  if (llp->can_grow) {
2694  res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2695  if (res == -1) {
2696  llp->can_grow = 0;
2697  goto fallback;
2698  }
2699  llp->size = res;
2700  }
2701  if (llp->size < bufsize)
2702  goto fallback;
2703  }
2704 
2705  res = splice(ch ? ch->fd : se->fd,
2706  NULL, llp->pipe[1], NULL, bufsize, 0);
2707  err = errno;
2708 
2709  if (fuse_session_exited(se))
2710  return 0;
2711 
2712  if (res == -1) {
2713  if (err == ENODEV) {
2714  /* Filesystem was unmounted, or connection was aborted
2715  via /sys/fs/fuse/connections */
2716  fuse_session_exit(se);
2717  return 0;
2718  }
2719  if (err != EINTR && err != EAGAIN)
2720  perror("fuse: splice from device");
2721  return -err;
2722  }
2723 
2724  if (res < sizeof(struct fuse_in_header)) {
2725  fprintf(stderr, "short splice from fuse device\n");
2726  return -EIO;
2727  }
2728 
2729  tmpbuf = (struct fuse_buf) {
2730  .size = res,
2731  .flags = FUSE_BUF_IS_FD,
2732  .fd = llp->pipe[0],
2733  };
2734 
2735  /*
2736  * Don't bother with zero copy for small requests.
2737  * fuse_loop_mt() needs to check for FORGET so this more than
2738  * just an optimization.
2739  */
2740  if (res < sizeof(struct fuse_in_header) +
2741  sizeof(struct fuse_write_in) + pagesize) {
2742  struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2743  struct fuse_bufvec dst = { .count = 1 };
2744 
2745  if (!buf->mem) {
2746  buf->mem = malloc(se->bufsize);
2747  if (!buf->mem) {
2748  fprintf(stderr,
2749  "fuse: failed to allocate read buffer\n");
2750  return -ENOMEM;
2751  }
2752  }
2753  buf->size = se->bufsize;
2754  buf->flags = 0;
2755  dst.buf[0] = *buf;
2756 
2757  res = fuse_buf_copy(&dst, &src, 0);
2758  if (res < 0) {
2759  fprintf(stderr, "fuse: copy from pipe: %s\n",
2760  strerror(-res));
2761  fuse_ll_clear_pipe(se);
2762  return res;
2763  }
2764  if (res < tmpbuf.size) {
2765  fprintf(stderr, "fuse: copy from pipe: short read\n");
2766  fuse_ll_clear_pipe(se);
2767  return -EIO;
2768  }
2769  assert(res == tmpbuf.size);
2770 
2771  } else {
2772  /* Don't overwrite buf->mem, as that would cause a leak */
2773  buf->fd = tmpbuf.fd;
2774  buf->flags = tmpbuf.flags;
2775  }
2776  buf->size = tmpbuf.size;
2777 
2778  return res;
2779 
2780 fallback:
2781 #endif
2782  if (!buf->mem) {
2783  buf->mem = malloc(se->bufsize);
2784  if (!buf->mem) {
2785  fprintf(stderr,
2786  "fuse: failed to allocate read buffer\n");
2787  return -ENOMEM;
2788  }
2789  }
2790 
2791 restart:
2792  res = read(ch ? ch->fd : se->fd, buf->mem, se->bufsize);
2793  err = errno;
2794 
2795  if (fuse_session_exited(se))
2796  return 0;
2797  if (res == -1) {
2798  /* ENOENT means the operation was interrupted, it's safe
2799  to restart */
2800  if (err == ENOENT)
2801  goto restart;
2802 
2803  if (err == ENODEV) {
2804  /* Filesystem was unmounted, or connection was aborted
2805  via /sys/fs/fuse/connections */
2806  fuse_session_exit(se);
2807  return 0;
2808  }
2809  /* Errors occurring during normal operation: EINTR (read
2810  interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
2811  umounted) */
2812  if (err != EINTR && err != EAGAIN)
2813  perror("fuse: reading device");
2814  return -err;
2815  }
2816  if ((size_t) res < sizeof(struct fuse_in_header)) {
2817  fprintf(stderr, "short read on fuse device\n");
2818  return -EIO;
2819  }
2820 
2821  buf->size = res;
2822 
2823  return res;
2824 }
2825 
2826 struct fuse_session *fuse_session_new(struct fuse_args *args,
2827  const struct fuse_lowlevel_ops *op,
2828  size_t op_size, void *userdata)
2829 {
2830  int err;
2831  struct fuse_session *se;
2832  struct mount_opts *mo;
2833 
2834  if (sizeof(struct fuse_lowlevel_ops) < op_size) {
2835  fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
2836  op_size = sizeof(struct fuse_lowlevel_ops);
2837  }
2838 
2839  if (args->argc == 0) {
2840  fprintf(stderr, "fuse: empty argv passed to fuse_session_new().\n");
2841  return NULL;
2842  }
2843 
2844  se = (struct fuse_session *) calloc(1, sizeof(struct fuse_session));
2845  if (se == NULL) {
2846  fprintf(stderr, "fuse: failed to allocate fuse object\n");
2847  goto out1;
2848  }
2849  se->fd = -1;
2850  se->conn.max_write = UINT_MAX;
2851  se->conn.max_readahead = UINT_MAX;
2852 
2853  /* Parse options */
2854  if(fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1)
2855  goto out2;
2856  if(se->deny_others) {
2857  /* Allowing access only by root is done by instructing
2858  * kernel to allow access by everyone, and then restricting
2859  * access to root and mountpoint owner in libfuse.
2860  */
2861  // We may be adding the option a second time, but
2862  // that doesn't hurt.
2863  if(fuse_opt_add_arg(args, "-oallow_other") == -1)
2864  goto out2;
2865  }
2866  mo = parse_mount_opts(args);
2867  if (mo == NULL)
2868  goto out3;
2869 
2870  if(args->argc == 1 &&
2871  args->argv[0][0] == '-') {
2872  fprintf(stderr, "fuse: warning: argv[0] looks like an option, but "
2873  "will be ignored\n");
2874  } else if (args->argc != 1) {
2875  int i;
2876  fprintf(stderr, "fuse: unknown option(s): `");
2877  for(i = 1; i < args->argc-1; i++)
2878  fprintf(stderr, "%s ", args->argv[i]);
2879  fprintf(stderr, "%s'\n", args->argv[i]);
2880  goto out4;
2881  }
2882 
2883  if (se->debug)
2884  fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
2885 
2886  se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
2887  FUSE_BUFFER_HEADER_SIZE;
2888 
2889  list_init_req(&se->list);
2890  list_init_req(&se->interrupts);
2891  list_init_nreq(&se->notify_list);
2892  se->notify_ctr = 1;
2893  fuse_mutex_init(&se->lock);
2894 
2895  err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
2896  if (err) {
2897  fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2898  strerror(err));
2899  goto out5;
2900  }
2901 
2902  memcpy(&se->op, op, op_size);
2903  se->owner = getuid();
2904  se->userdata = userdata;
2905 
2906  se->mo = mo;
2907  return se;
2908 
2909 out5:
2910  pthread_mutex_destroy(&se->lock);
2911 out4:
2912  fuse_opt_free_args(args);
2913 out3:
2914  free(mo);
2915 out2:
2916  free(se);
2917 out1:
2918  return NULL;
2919 }
2920 
2921 int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
2922 {
2923  int fd;
2924 
2925  /*
2926  * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
2927  * would ensue.
2928  */
2929  do {
2930  fd = open("/dev/null", O_RDWR);
2931  if (fd > 2)
2932  close(fd);
2933  } while (fd >= 0 && fd <= 2);
2934 
2935  /*
2936  * To allow FUSE daemons to run without privileges, the caller may open
2937  * /dev/fuse before launching the file system and pass on the file
2938  * descriptor by specifying /dev/fd/N as the mount point. Note that the
2939  * parent process takes care of performing the mount in this case.
2940  */
2941  fd = fuse_mnt_parse_fuse_fd(mountpoint);
2942  if (fd != -1) {
2943  if (fcntl(fd, F_GETFD) == -1) {
2944  fprintf(stderr,
2945  "fuse: Invalid file descriptor /dev/fd/%u\n",
2946  fd);
2947  return -1;
2948  }
2949  se->fd = fd;
2950  return 0;
2951  }
2952 
2953  /* Open channel */
2954  fd = fuse_kern_mount(mountpoint, se->mo);
2955  if (fd == -1)
2956  return -1;
2957  se->fd = fd;
2958 
2959  /* Save mountpoint */
2960  se->mountpoint = strdup(mountpoint);
2961  if (se->mountpoint == NULL)
2962  goto error_out;
2963 
2964  return 0;
2965 
2966 error_out:
2967  fuse_kern_unmount(mountpoint, fd);
2968  return -1;
2969 }
2970 
2971 int fuse_session_fd(struct fuse_session *se)
2972 {
2973  return se->fd;
2974 }
2975 
2976 void fuse_session_unmount(struct fuse_session *se)
2977 {
2978  if (se->mountpoint != NULL) {
2979  fuse_kern_unmount(se->mountpoint, se->fd);
2980  free(se->mountpoint);
2981  se->mountpoint = NULL;
2982  }
2983 }
2984 
2985 #ifdef linux
2986 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
2987 {
2988  char *buf;
2989  size_t bufsize = 1024;
2990  char path[128];
2991  int ret;
2992  int fd;
2993  unsigned long pid = req->ctx.pid;
2994  char *s;
2995 
2996  sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
2997 
2998 retry:
2999  buf = malloc(bufsize);
3000  if (buf == NULL)
3001  return -ENOMEM;
3002 
3003  ret = -EIO;
3004  fd = open(path, O_RDONLY);
3005  if (fd == -1)
3006  goto out_free;
3007 
3008  ret = read(fd, buf, bufsize);
3009  close(fd);
3010  if (ret < 0) {
3011  ret = -EIO;
3012  goto out_free;
3013  }
3014 
3015  if ((size_t)ret == bufsize) {
3016  free(buf);
3017  bufsize *= 4;
3018  goto retry;
3019  }
3020 
3021  ret = -EIO;
3022  s = strstr(buf, "\nGroups:");
3023  if (s == NULL)
3024  goto out_free;
3025 
3026  s += 8;
3027  ret = 0;
3028  while (1) {
3029  char *end;
3030  unsigned long val = strtoul(s, &end, 0);
3031  if (end == s)
3032  break;
3033 
3034  s = end;
3035  if (ret < size)
3036  list[ret] = val;
3037  ret++;
3038  }
3039 
3040 out_free:
3041  free(buf);
3042  return ret;
3043 }
3044 #else /* linux */
3045 /*
3046  * This is currently not implemented on other than Linux...
3047  */
3048 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3049 {
3050  (void) req; (void) size; (void) list;
3051  return -ENOSYS;
3052 }
3053 #endif
3054 
3055 void fuse_session_exit(struct fuse_session *se)
3056 {
3057  se->exited = 1;
3058 }
3059 
3060 void fuse_session_reset(struct fuse_session *se)
3061 {
3062  se->exited = 0;
3063  se->error = 0;
3064 }
3065 
3066 int fuse_session_exited(struct fuse_session *se)
3067 {
3068  return se->exited;
3069 }
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:88
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:79
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:94
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