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