You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3070 lines
74 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB
  6. */
  7. #define _GNU_SOURCE
  8. #include "config.h"
  9. #include "fuse_i.h"
  10. #include "fuse_kernel.h"
  11. #include "fuse_opt.h"
  12. #include "fuse_misc.h"
  13. #include "fuse_common_compat.h"
  14. #include "fuse_lowlevel_compat.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <limits.h>
  21. #include <errno.h>
  22. #include <assert.h>
  23. #include <sys/file.h>
  24. #ifndef F_LINUX_SPECIFIC_BASE
  25. #define F_LINUX_SPECIFIC_BASE 1024
  26. #endif
  27. #ifndef F_SETPIPE_SZ
  28. #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
  29. #endif
  30. #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
  31. #define OFFSET_MAX 0x7fffffffffffffffLL
  32. #define container_of(ptr, type, member) ({ \
  33. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  34. (type *)( (char *)__mptr - offsetof(type,member) );})
  35. struct fuse_pollhandle {
  36. uint64_t kh;
  37. struct fuse_chan *ch;
  38. struct fuse_ll *f;
  39. };
  40. static size_t pagesize;
  41. static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
  42. {
  43. pagesize = getpagesize();
  44. }
  45. static
  46. void
  47. convert_stat(const struct stat *stbuf_,
  48. struct fuse_attr *attr_)
  49. {
  50. attr_->ino = stbuf_->st_ino;
  51. attr_->mode = stbuf_->st_mode;
  52. attr_->nlink = stbuf_->st_nlink;
  53. attr_->uid = stbuf_->st_uid;
  54. attr_->gid = stbuf_->st_gid;
  55. attr_->rdev = stbuf_->st_rdev;
  56. attr_->size = stbuf_->st_size;
  57. attr_->blksize = stbuf_->st_blksize;
  58. attr_->blocks = stbuf_->st_blocks;
  59. attr_->atime = stbuf_->st_atime;
  60. attr_->mtime = stbuf_->st_mtime;
  61. attr_->ctime = stbuf_->st_ctime;
  62. attr_->atimensec = ST_ATIM_NSEC(stbuf_);
  63. attr_->mtimensec = ST_MTIM_NSEC(stbuf_);
  64. attr_->ctimensec = ST_CTIM_NSEC(stbuf_);
  65. }
  66. static
  67. void
  68. convert_attr(const struct fuse_setattr_in *attr_,
  69. struct stat *stbuf_)
  70. {
  71. stbuf_->st_mode = attr_->mode;
  72. stbuf_->st_uid = attr_->uid;
  73. stbuf_->st_gid = attr_->gid;
  74. stbuf_->st_size = attr_->size;
  75. stbuf_->st_atime = attr_->atime;
  76. stbuf_->st_mtime = attr_->mtime;
  77. stbuf_->st_ctime = attr_->ctime;
  78. ST_ATIM_NSEC_SET(stbuf_,attr_->atimensec);
  79. ST_MTIM_NSEC_SET(stbuf_,attr_->mtimensec);
  80. ST_CTIM_NSEC_SET(stbuf_,attr_->ctimensec);
  81. }
  82. static size_t iov_length(const struct iovec *iov, size_t count)
  83. {
  84. size_t seg;
  85. size_t ret = 0;
  86. for (seg = 0; seg < count; seg++)
  87. ret += iov[seg].iov_len;
  88. return ret;
  89. }
  90. static void list_init_req(struct fuse_req *req)
  91. {
  92. req->next = req;
  93. req->prev = req;
  94. }
  95. static void list_del_req(struct fuse_req *req)
  96. {
  97. struct fuse_req *prev = req->prev;
  98. struct fuse_req *next = req->next;
  99. prev->next = next;
  100. next->prev = prev;
  101. }
  102. static void list_add_req(struct fuse_req *req, struct fuse_req *next)
  103. {
  104. struct fuse_req *prev = next->prev;
  105. req->next = next;
  106. req->prev = prev;
  107. prev->next = req;
  108. next->prev = req;
  109. }
  110. static void destroy_req(fuse_req_t req)
  111. {
  112. pthread_mutex_destroy(&req->lock);
  113. free(req);
  114. }
  115. void fuse_free_req(fuse_req_t req)
  116. {
  117. int ctr;
  118. struct fuse_ll *f = req->f;
  119. pthread_mutex_lock(&f->lock);
  120. req->u.ni.func = NULL;
  121. req->u.ni.data = NULL;
  122. list_del_req(req);
  123. ctr = --req->ctr;
  124. pthread_mutex_unlock(&f->lock);
  125. if (!ctr)
  126. destroy_req(req);
  127. }
  128. static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
  129. {
  130. struct fuse_req *req;
  131. req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
  132. if (req == NULL) {
  133. fprintf(stderr, "fuse: failed to allocate request\n");
  134. } else {
  135. req->f = f;
  136. req->ctr = 1;
  137. list_init_req(req);
  138. fuse_mutex_init(&req->lock);
  139. }
  140. return req;
  141. }
  142. static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
  143. struct iovec *iov, int count)
  144. {
  145. struct fuse_out_header *out = iov[0].iov_base;
  146. out->len = iov_length(iov, count);
  147. if (f->debug) {
  148. if (out->unique == 0) {
  149. fprintf(stderr, "NOTIFY: code=%d length=%u\n",
  150. out->error, out->len);
  151. } else if (out->error) {
  152. fprintf(stderr,
  153. " unique: %llu, error: %i (%s), outsize: %i\n",
  154. (unsigned long long) out->unique, out->error,
  155. strerror(-out->error), out->len);
  156. } else {
  157. fprintf(stderr,
  158. " unique: %llu, success, outsize: %i\n",
  159. (unsigned long long) out->unique, out->len);
  160. }
  161. }
  162. return fuse_chan_send(ch, iov, count);
  163. }
  164. int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
  165. int count)
  166. {
  167. struct fuse_out_header out;
  168. if (error <= -1000 || error > 0) {
  169. fprintf(stderr, "fuse: bad error value: %i\n", error);
  170. error = -ERANGE;
  171. }
  172. out.unique = req->unique;
  173. out.error = error;
  174. iov[0].iov_base = &out;
  175. iov[0].iov_len = sizeof(struct fuse_out_header);
  176. return fuse_send_msg(req->f, req->ch, iov, count);
  177. }
  178. static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
  179. int count)
  180. {
  181. int res;
  182. res = fuse_send_reply_iov_nofree(req, error, iov, count);
  183. fuse_free_req(req);
  184. return res;
  185. }
  186. static int send_reply(fuse_req_t req, int error, const void *arg,
  187. size_t argsize)
  188. {
  189. struct iovec iov[2];
  190. int count = 1;
  191. if (argsize) {
  192. iov[1].iov_base = (void *) arg;
  193. iov[1].iov_len = argsize;
  194. count++;
  195. }
  196. return send_reply_iov(req, error, iov, count);
  197. }
  198. int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
  199. {
  200. int res;
  201. struct iovec *padded_iov;
  202. padded_iov = malloc((count + 1) * sizeof(struct iovec));
  203. if (padded_iov == NULL)
  204. return fuse_reply_err(req, ENOMEM);
  205. memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
  206. count++;
  207. res = send_reply_iov(req, 0, padded_iov, count);
  208. free(padded_iov);
  209. return res;
  210. }
  211. size_t fuse_dirent_size(size_t namelen)
  212. {
  213. return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
  214. }
  215. char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
  216. off_t off)
  217. {
  218. unsigned namelen = strlen(name);
  219. unsigned entlen = FUSE_NAME_OFFSET + namelen;
  220. unsigned entsize = fuse_dirent_size(namelen);
  221. unsigned padlen = entsize - entlen;
  222. struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
  223. dirent->ino = stbuf->st_ino;
  224. dirent->off = off;
  225. dirent->namelen = namelen;
  226. dirent->type = (stbuf->st_mode & 0170000) >> 12;
  227. strncpy(dirent->name, name, namelen);
  228. if (padlen)
  229. memset(buf + entlen, 0, padlen);
  230. return buf + entsize;
  231. }
  232. size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
  233. const char *name, const struct stat *stbuf, off_t off)
  234. {
  235. size_t entsize;
  236. (void) req;
  237. entsize = fuse_dirent_size(strlen(name));
  238. if (entsize <= bufsize && buf)
  239. fuse_add_dirent(buf, name, stbuf, off);
  240. return entsize;
  241. }
  242. static void convert_statfs(const struct statvfs *stbuf,
  243. struct fuse_kstatfs *kstatfs)
  244. {
  245. kstatfs->bsize = stbuf->f_bsize;
  246. kstatfs->frsize = stbuf->f_frsize;
  247. kstatfs->blocks = stbuf->f_blocks;
  248. kstatfs->bfree = stbuf->f_bfree;
  249. kstatfs->bavail = stbuf->f_bavail;
  250. kstatfs->files = stbuf->f_files;
  251. kstatfs->ffree = stbuf->f_ffree;
  252. kstatfs->namelen = stbuf->f_namemax;
  253. }
  254. static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
  255. {
  256. return send_reply(req, 0, arg, argsize);
  257. }
  258. int
  259. fuse_reply_err(fuse_req_t req_,
  260. int err_)
  261. {
  262. return send_reply(req_,-err_,NULL,0);
  263. }
  264. void fuse_reply_none(fuse_req_t req)
  265. {
  266. if (req->ch)
  267. fuse_chan_send(req->ch, NULL, 0);
  268. fuse_free_req(req);
  269. }
  270. static unsigned long calc_timeout_sec(double t)
  271. {
  272. if (t > (double) ULONG_MAX)
  273. return ULONG_MAX;
  274. else if (t < 0.0)
  275. return 0;
  276. else
  277. return (unsigned long) t;
  278. }
  279. static unsigned int calc_timeout_nsec(double t)
  280. {
  281. double f = t - (double) calc_timeout_sec(t);
  282. if (f < 0.0)
  283. return 0;
  284. else if (f >= 0.999999999)
  285. return 999999999;
  286. else
  287. return (unsigned int) (f * 1.0e9);
  288. }
  289. static void fill_entry(struct fuse_entry_out *arg,
  290. const struct fuse_entry_param *e)
  291. {
  292. arg->nodeid = e->ino;
  293. arg->generation = e->generation;
  294. arg->entry_valid = calc_timeout_sec(e->entry_timeout);
  295. arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
  296. arg->attr_valid = calc_timeout_sec(e->attr_timeout);
  297. arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
  298. convert_stat(&e->attr, &arg->attr);
  299. }
  300. static void fill_open(struct fuse_open_out *arg,
  301. const struct fuse_file_info *f)
  302. {
  303. arg->fh = f->fh;
  304. if (f->direct_io)
  305. arg->open_flags |= FOPEN_DIRECT_IO;
  306. if (f->keep_cache)
  307. arg->open_flags |= FOPEN_KEEP_CACHE;
  308. if (f->nonseekable)
  309. arg->open_flags |= FOPEN_NONSEEKABLE;
  310. if (f->cache_readdir)
  311. arg->open_flags |= FOPEN_CACHE_DIR;
  312. }
  313. int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
  314. {
  315. struct fuse_entry_out arg;
  316. size_t size = req->f->conn.proto_minor < 9 ?
  317. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
  318. /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
  319. negative entry */
  320. if (!e->ino && req->f->conn.proto_minor < 4)
  321. return fuse_reply_err(req, ENOENT);
  322. memset(&arg, 0, sizeof(arg));
  323. fill_entry(&arg, e);
  324. return send_reply_ok(req, &arg, size);
  325. }
  326. int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
  327. const struct fuse_file_info *f)
  328. {
  329. char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
  330. size_t entrysize = req->f->conn.proto_minor < 9 ?
  331. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
  332. struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
  333. struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
  334. memset(buf, 0, sizeof(buf));
  335. fill_entry(earg, e);
  336. fill_open(oarg, f);
  337. return send_reply_ok(req, buf,
  338. entrysize + sizeof(struct fuse_open_out));
  339. }
  340. int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
  341. double attr_timeout)
  342. {
  343. struct fuse_attr_out arg;
  344. size_t size = req->f->conn.proto_minor < 9 ?
  345. FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
  346. memset(&arg, 0, sizeof(arg));
  347. arg.attr_valid = calc_timeout_sec(attr_timeout);
  348. arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
  349. convert_stat(attr, &arg.attr);
  350. return send_reply_ok(req, &arg, size);
  351. }
  352. int fuse_reply_readlink(fuse_req_t req, const char *linkname)
  353. {
  354. return send_reply_ok(req, linkname, strlen(linkname));
  355. }
  356. int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
  357. {
  358. struct fuse_open_out arg;
  359. memset(&arg, 0, sizeof(arg));
  360. fill_open(&arg, f);
  361. return send_reply_ok(req, &arg, sizeof(arg));
  362. }
  363. int fuse_reply_write(fuse_req_t req, size_t count)
  364. {
  365. struct fuse_write_out arg;
  366. memset(&arg, 0, sizeof(arg));
  367. arg.size = count;
  368. return send_reply_ok(req, &arg, sizeof(arg));
  369. }
  370. int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
  371. {
  372. return send_reply_ok(req, buf, size);
  373. }
  374. static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
  375. struct iovec *iov, int iov_count,
  376. struct fuse_bufvec *buf,
  377. size_t len)
  378. {
  379. struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
  380. void *mbuf;
  381. int res;
  382. /* Optimize common case */
  383. if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
  384. !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
  385. /* FIXME: also avoid memory copy if there are multiple buffers
  386. but none of them contain an fd */
  387. iov[iov_count].iov_base = buf->buf[0].mem;
  388. iov[iov_count].iov_len = len;
  389. iov_count++;
  390. return fuse_send_msg(f, ch, iov, iov_count);
  391. }
  392. res = posix_memalign(&mbuf, pagesize, len);
  393. if (res != 0)
  394. return res;
  395. mem_buf.buf[0].mem = mbuf;
  396. res = fuse_buf_copy(&mem_buf, buf, 0);
  397. if (res < 0) {
  398. free(mbuf);
  399. return -res;
  400. }
  401. len = res;
  402. iov[iov_count].iov_base = mbuf;
  403. iov[iov_count].iov_len = len;
  404. iov_count++;
  405. res = fuse_send_msg(f, ch, iov, iov_count);
  406. free(mbuf);
  407. return res;
  408. }
  409. struct fuse_ll_pipe {
  410. size_t size;
  411. int can_grow;
  412. int pipe[2];
  413. };
  414. static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
  415. {
  416. close(llp->pipe[0]);
  417. close(llp->pipe[1]);
  418. free(llp);
  419. }
  420. #ifdef HAVE_SPLICE
  421. static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
  422. {
  423. struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
  424. if (llp == NULL) {
  425. int res;
  426. llp = malloc(sizeof(struct fuse_ll_pipe));
  427. if (llp == NULL)
  428. return NULL;
  429. res = pipe(llp->pipe);
  430. if (res == -1) {
  431. free(llp);
  432. return NULL;
  433. }
  434. if (fcntl(llp->pipe[0], F_SETFL, O_NONBLOCK) == -1 ||
  435. fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
  436. close(llp->pipe[0]);
  437. close(llp->pipe[1]);
  438. free(llp);
  439. return NULL;
  440. }
  441. /*
  442. *the default size is 16 pages on linux
  443. */
  444. llp->size = pagesize * 16;
  445. llp->can_grow = 1;
  446. pthread_setspecific(f->pipe_key, llp);
  447. }
  448. return llp;
  449. }
  450. #endif
  451. static void fuse_ll_clear_pipe(struct fuse_ll *f)
  452. {
  453. struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
  454. if (llp) {
  455. pthread_setspecific(f->pipe_key, NULL);
  456. fuse_ll_pipe_free(llp);
  457. }
  458. }
  459. #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
  460. static int read_back(int fd, char *buf, size_t len)
  461. {
  462. int res;
  463. res = read(fd, buf, len);
  464. if (res == -1) {
  465. fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
  466. return -EIO;
  467. }
  468. if (res != len) {
  469. fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
  470. return -EIO;
  471. }
  472. return 0;
  473. }
  474. static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
  475. struct iovec *iov, int iov_count,
  476. struct fuse_bufvec *buf, unsigned int flags)
  477. {
  478. int res;
  479. size_t len = fuse_buf_size(buf);
  480. struct fuse_out_header *out = iov[0].iov_base;
  481. struct fuse_ll_pipe *llp;
  482. int splice_flags;
  483. size_t pipesize;
  484. size_t total_fd_size;
  485. size_t idx;
  486. size_t headerlen;
  487. struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
  488. if (f->broken_splice_nonblock)
  489. goto fallback;
  490. if (flags & FUSE_BUF_NO_SPLICE)
  491. goto fallback;
  492. total_fd_size = 0;
  493. for (idx = buf->idx; idx < buf->count; idx++) {
  494. if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
  495. total_fd_size = buf->buf[idx].size;
  496. if (idx == buf->idx)
  497. total_fd_size -= buf->off;
  498. }
  499. }
  500. if (total_fd_size < 2 * pagesize)
  501. goto fallback;
  502. if (f->conn.proto_minor < 14 ||
  503. !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
  504. goto fallback;
  505. llp = fuse_ll_get_pipe(f);
  506. if (llp == NULL)
  507. goto fallback;
  508. headerlen = iov_length(iov, iov_count);
  509. out->len = headerlen + len;
  510. /*
  511. * Heuristic for the required pipe size, does not work if the
  512. * source contains less than page size fragments
  513. */
  514. pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
  515. if (llp->size < pipesize) {
  516. if (llp->can_grow) {
  517. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
  518. if (res == -1) {
  519. llp->can_grow = 0;
  520. goto fallback;
  521. }
  522. llp->size = res;
  523. }
  524. if (llp->size < pipesize)
  525. goto fallback;
  526. }
  527. res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
  528. if (res == -1)
  529. goto fallback;
  530. if (res != headerlen) {
  531. res = -EIO;
  532. fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
  533. headerlen);
  534. goto clear_pipe;
  535. }
  536. pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
  537. pipe_buf.buf[0].fd = llp->pipe[1];
  538. res = fuse_buf_copy(&pipe_buf, buf,
  539. FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
  540. if (res < 0) {
  541. if (res == -EAGAIN || res == -EINVAL) {
  542. /*
  543. * Should only get EAGAIN on kernels with
  544. * broken SPLICE_F_NONBLOCK support (<=
  545. * 2.6.35) where this error or a short read is
  546. * returned even if the pipe itself is not
  547. * full
  548. *
  549. * EINVAL might mean that splice can't handle
  550. * this combination of input and output.
  551. */
  552. if (res == -EAGAIN)
  553. f->broken_splice_nonblock = 1;
  554. pthread_setspecific(f->pipe_key, NULL);
  555. fuse_ll_pipe_free(llp);
  556. goto fallback;
  557. }
  558. res = -res;
  559. goto clear_pipe;
  560. }
  561. if (res != 0 && res < len) {
  562. struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
  563. void *mbuf;
  564. size_t now_len = res;
  565. /*
  566. * For regular files a short count is either
  567. * 1) due to EOF, or
  568. * 2) because of broken SPLICE_F_NONBLOCK (see above)
  569. *
  570. * For other inputs it's possible that we overflowed
  571. * the pipe because of small buffer fragments.
  572. */
  573. res = posix_memalign(&mbuf, pagesize, len);
  574. if (res != 0)
  575. goto clear_pipe;
  576. mem_buf.buf[0].mem = mbuf;
  577. mem_buf.off = now_len;
  578. res = fuse_buf_copy(&mem_buf, buf, 0);
  579. if (res > 0) {
  580. char *tmpbuf;
  581. size_t extra_len = res;
  582. /*
  583. * Trickiest case: got more data. Need to get
  584. * back the data from the pipe and then fall
  585. * back to regular write.
  586. */
  587. tmpbuf = malloc(headerlen);
  588. if (tmpbuf == NULL) {
  589. free(mbuf);
  590. res = ENOMEM;
  591. goto clear_pipe;
  592. }
  593. res = read_back(llp->pipe[0], tmpbuf, headerlen);
  594. free(tmpbuf);
  595. if (res != 0) {
  596. free(mbuf);
  597. goto clear_pipe;
  598. }
  599. res = read_back(llp->pipe[0], mbuf, now_len);
  600. if (res != 0) {
  601. free(mbuf);
  602. goto clear_pipe;
  603. }
  604. len = now_len + extra_len;
  605. iov[iov_count].iov_base = mbuf;
  606. iov[iov_count].iov_len = len;
  607. iov_count++;
  608. res = fuse_send_msg(f, ch, iov, iov_count);
  609. free(mbuf);
  610. return res;
  611. }
  612. free(mbuf);
  613. res = now_len;
  614. }
  615. len = res;
  616. out->len = headerlen + len;
  617. if (f->debug) {
  618. fprintf(stderr,
  619. " unique: %llu, success, outsize: %i (splice)\n",
  620. (unsigned long long) out->unique, out->len);
  621. }
  622. splice_flags = 0;
  623. if ((flags & FUSE_BUF_SPLICE_MOVE) &&
  624. (f->conn.want & FUSE_CAP_SPLICE_MOVE))
  625. splice_flags |= SPLICE_F_MOVE;
  626. res = splice(llp->pipe[0], NULL,
  627. fuse_chan_fd(ch), NULL, out->len, splice_flags);
  628. if (res == -1) {
  629. res = -errno;
  630. perror("fuse: splice from pipe");
  631. goto clear_pipe;
  632. }
  633. if (res != out->len) {
  634. res = -EIO;
  635. fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
  636. res, out->len);
  637. goto clear_pipe;
  638. }
  639. return 0;
  640. clear_pipe:
  641. fuse_ll_clear_pipe(f);
  642. return res;
  643. fallback:
  644. return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
  645. }
  646. #else
  647. static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
  648. struct iovec *iov, int iov_count,
  649. struct fuse_bufvec *buf, unsigned int flags)
  650. {
  651. size_t len = fuse_buf_size(buf);
  652. (void) flags;
  653. return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
  654. }
  655. #endif
  656. int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
  657. enum fuse_buf_copy_flags flags)
  658. {
  659. struct iovec iov[2];
  660. struct fuse_out_header out;
  661. int res;
  662. iov[0].iov_base = &out;
  663. iov[0].iov_len = sizeof(struct fuse_out_header);
  664. out.unique = req->unique;
  665. out.error = 0;
  666. res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
  667. if (res <= 0) {
  668. fuse_free_req(req);
  669. return res;
  670. } else {
  671. return fuse_reply_err(req, res);
  672. }
  673. }
  674. int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
  675. {
  676. struct fuse_statfs_out arg;
  677. size_t size = req->f->conn.proto_minor < 4 ?
  678. FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
  679. memset(&arg, 0, sizeof(arg));
  680. convert_statfs(stbuf, &arg.st);
  681. return send_reply_ok(req, &arg, size);
  682. }
  683. int fuse_reply_xattr(fuse_req_t req, size_t count)
  684. {
  685. struct fuse_getxattr_out arg;
  686. memset(&arg, 0, sizeof(arg));
  687. arg.size = count;
  688. return send_reply_ok(req, &arg, sizeof(arg));
  689. }
  690. int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
  691. {
  692. struct fuse_lk_out arg;
  693. memset(&arg, 0, sizeof(arg));
  694. arg.lk.type = lock->l_type;
  695. if (lock->l_type != F_UNLCK) {
  696. arg.lk.start = lock->l_start;
  697. if (lock->l_len == 0)
  698. arg.lk.end = OFFSET_MAX;
  699. else
  700. arg.lk.end = lock->l_start + lock->l_len - 1;
  701. }
  702. arg.lk.pid = lock->l_pid;
  703. return send_reply_ok(req, &arg, sizeof(arg));
  704. }
  705. int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
  706. {
  707. struct fuse_bmap_out arg;
  708. memset(&arg, 0, sizeof(arg));
  709. arg.block = idx;
  710. return send_reply_ok(req, &arg, sizeof(arg));
  711. }
  712. static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
  713. size_t count)
  714. {
  715. struct fuse_ioctl_iovec *fiov;
  716. size_t i;
  717. fiov = malloc(sizeof(fiov[0]) * count);
  718. if (!fiov)
  719. return NULL;
  720. for (i = 0; i < count; i++) {
  721. fiov[i].base = (uintptr_t) iov[i].iov_base;
  722. fiov[i].len = iov[i].iov_len;
  723. }
  724. return fiov;
  725. }
  726. int fuse_reply_ioctl_retry(fuse_req_t req,
  727. const struct iovec *in_iov, size_t in_count,
  728. const struct iovec *out_iov, size_t out_count)
  729. {
  730. struct fuse_ioctl_out arg;
  731. struct fuse_ioctl_iovec *in_fiov = NULL;
  732. struct fuse_ioctl_iovec *out_fiov = NULL;
  733. struct iovec iov[4];
  734. size_t count = 1;
  735. int res;
  736. memset(&arg, 0, sizeof(arg));
  737. arg.flags |= FUSE_IOCTL_RETRY;
  738. arg.in_iovs = in_count;
  739. arg.out_iovs = out_count;
  740. iov[count].iov_base = &arg;
  741. iov[count].iov_len = sizeof(arg);
  742. count++;
  743. if (req->f->conn.proto_minor < 16) {
  744. if (in_count) {
  745. iov[count].iov_base = (void *)in_iov;
  746. iov[count].iov_len = sizeof(in_iov[0]) * in_count;
  747. count++;
  748. }
  749. if (out_count) {
  750. iov[count].iov_base = (void *)out_iov;
  751. iov[count].iov_len = sizeof(out_iov[0]) * out_count;
  752. count++;
  753. }
  754. } else {
  755. /* Can't handle non-compat 64bit ioctls on 32bit */
  756. if (sizeof(void *) == 4 && req->ioctl_64bit) {
  757. res = fuse_reply_err(req, EINVAL);
  758. goto out;
  759. }
  760. if (in_count) {
  761. in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
  762. if (!in_fiov)
  763. goto enomem;
  764. iov[count].iov_base = (void *)in_fiov;
  765. iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
  766. count++;
  767. }
  768. if (out_count) {
  769. out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
  770. if (!out_fiov)
  771. goto enomem;
  772. iov[count].iov_base = (void *)out_fiov;
  773. iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
  774. count++;
  775. }
  776. }
  777. res = send_reply_iov(req, 0, iov, count);
  778. out:
  779. free(in_fiov);
  780. free(out_fiov);
  781. return res;
  782. enomem:
  783. res = fuse_reply_err(req, ENOMEM);
  784. goto out;
  785. }
  786. int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, uint32_t size)
  787. {
  788. int count;
  789. struct iovec iov[3];
  790. struct fuse_ioctl_out arg;
  791. arg.result = result;
  792. arg.flags = 0;
  793. arg.in_iovs = 0;
  794. arg.out_iovs = 0;
  795. count = 1;
  796. iov[count].iov_base = &arg;
  797. iov[count].iov_len = sizeof(arg);
  798. count++;
  799. if(size)
  800. {
  801. iov[count].iov_base = (char*)buf;
  802. iov[count].iov_len = size;
  803. count++;
  804. }
  805. return send_reply_iov(req, 0, iov, count);
  806. }
  807. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  808. int count)
  809. {
  810. struct iovec *padded_iov;
  811. struct fuse_ioctl_out arg;
  812. int res;
  813. padded_iov = malloc((count + 2) * sizeof(struct iovec));
  814. if (padded_iov == NULL)
  815. return fuse_reply_err(req, ENOMEM);
  816. memset(&arg, 0, sizeof(arg));
  817. arg.result = result;
  818. padded_iov[1].iov_base = &arg;
  819. padded_iov[1].iov_len = sizeof(arg);
  820. memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
  821. res = send_reply_iov(req, 0, padded_iov, count + 2);
  822. free(padded_iov);
  823. return res;
  824. }
  825. int fuse_reply_poll(fuse_req_t req, unsigned revents)
  826. {
  827. struct fuse_poll_out arg;
  828. memset(&arg, 0, sizeof(arg));
  829. arg.revents = revents;
  830. return send_reply_ok(req, &arg, sizeof(arg));
  831. }
  832. static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  833. {
  834. char *name = (char *) inarg;
  835. if (req->f->op.lookup)
  836. req->f->op.lookup(req, nodeid, name);
  837. else
  838. fuse_reply_err(req, ENOSYS);
  839. }
  840. static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  841. {
  842. struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
  843. if (req->f->op.forget)
  844. req->f->op.forget(req, nodeid, arg->nlookup);
  845. else
  846. fuse_reply_none(req);
  847. }
  848. static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
  849. const void *inarg)
  850. {
  851. struct fuse_batch_forget_in *arg = (void *) inarg;
  852. struct fuse_forget_one *param = (void *) PARAM(arg);
  853. unsigned int i;
  854. (void) nodeid;
  855. if (req->f->op.forget_multi) {
  856. req->f->op.forget_multi(req, arg->count,
  857. (struct fuse_forget_data *) param);
  858. } else if (req->f->op.forget) {
  859. for (i = 0; i < arg->count; i++) {
  860. struct fuse_forget_one *forget = &param[i];
  861. struct fuse_req *dummy_req;
  862. dummy_req = fuse_ll_alloc_req(req->f);
  863. if (dummy_req == NULL)
  864. break;
  865. dummy_req->unique = req->unique;
  866. dummy_req->ctx = req->ctx;
  867. dummy_req->ch = NULL;
  868. req->f->op.forget(dummy_req, forget->nodeid,
  869. forget->nlookup);
  870. }
  871. fuse_reply_none(req);
  872. } else {
  873. fuse_reply_none(req);
  874. }
  875. }
  876. static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  877. {
  878. struct fuse_file_info *fip = NULL;
  879. struct fuse_file_info fi;
  880. if (req->f->conn.proto_minor >= 9) {
  881. struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
  882. if (arg->getattr_flags & FUSE_GETATTR_FH) {
  883. memset(&fi, 0, sizeof(fi));
  884. fi.fh = arg->fh;
  885. fip = &fi;
  886. }
  887. }
  888. if (req->f->op.getattr)
  889. req->f->op.getattr(req, nodeid, fip);
  890. else
  891. fuse_reply_err(req, ENOSYS);
  892. }
  893. static
  894. void
  895. do_setattr(fuse_req_t req_,
  896. fuse_ino_t nodeid_,
  897. const void *inarg_)
  898. {
  899. struct stat stbuf;
  900. struct fuse_file_info *fi;
  901. struct fuse_file_info fi_store;
  902. struct fuse_setattr_in *arg;
  903. if(req_->f->op.setattr == NULL)
  904. return (void)fuse_reply_err(req_,ENOSYS);
  905. fi = NULL;
  906. arg = (struct fuse_setattr_in*)inarg_;
  907. memset(&stbuf,0,sizeof(stbuf));
  908. convert_attr(arg,&stbuf);
  909. if(arg->valid & FATTR_FH)
  910. {
  911. arg->valid &= ~FATTR_FH;
  912. memset(&fi_store,0,sizeof(fi_store));
  913. fi = &fi_store;
  914. fi->fh = arg->fh;
  915. }
  916. arg->valid &=
  917. (FATTR_MODE |
  918. FATTR_UID |
  919. FATTR_GID |
  920. FATTR_SIZE |
  921. FATTR_ATIME |
  922. FATTR_MTIME |
  923. FATTR_CTIME |
  924. FATTR_ATIME_NOW |
  925. FATTR_MTIME_NOW);
  926. req_->f->op.setattr(req_,nodeid_,&stbuf,arg->valid,fi);
  927. }
  928. static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  929. {
  930. struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
  931. if (req->f->op.access)
  932. req->f->op.access(req, nodeid, arg->mask);
  933. else
  934. fuse_reply_err(req, ENOSYS);
  935. }
  936. static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  937. {
  938. (void) inarg;
  939. if (req->f->op.readlink)
  940. req->f->op.readlink(req, nodeid);
  941. else
  942. fuse_reply_err(req, ENOSYS);
  943. }
  944. static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  945. {
  946. struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
  947. char *name = PARAM(arg);
  948. if (req->f->conn.proto_minor >= 12)
  949. req->ctx.umask = arg->umask;
  950. else
  951. name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
  952. if (req->f->op.mknod)
  953. req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
  954. else
  955. fuse_reply_err(req, ENOSYS);
  956. }
  957. static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  958. {
  959. struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
  960. if (req->f->conn.proto_minor >= 12)
  961. req->ctx.umask = arg->umask;
  962. if (req->f->op.mkdir)
  963. req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
  964. else
  965. fuse_reply_err(req, ENOSYS);
  966. }
  967. static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  968. {
  969. char *name = (char *) inarg;
  970. if (req->f->op.unlink)
  971. req->f->op.unlink(req, nodeid, name);
  972. else
  973. fuse_reply_err(req, ENOSYS);
  974. }
  975. static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  976. {
  977. char *name = (char *) inarg;
  978. if (req->f->op.rmdir)
  979. req->f->op.rmdir(req, nodeid, name);
  980. else
  981. fuse_reply_err(req, ENOSYS);
  982. }
  983. static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  984. {
  985. char *name = (char *) inarg;
  986. char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
  987. if (req->f->op.symlink)
  988. req->f->op.symlink(req, linkname, nodeid, name);
  989. else
  990. fuse_reply_err(req, ENOSYS);
  991. }
  992. static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  993. {
  994. struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
  995. char *oldname = PARAM(arg);
  996. char *newname = oldname + strlen(oldname) + 1;
  997. if (req->f->op.rename)
  998. req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
  999. else
  1000. fuse_reply_err(req, ENOSYS);
  1001. }
  1002. static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1003. {
  1004. struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
  1005. if (req->f->op.link)
  1006. req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
  1007. else
  1008. fuse_reply_err(req, ENOSYS);
  1009. }
  1010. static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1011. {
  1012. struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
  1013. if (req->f->op.create) {
  1014. struct fuse_file_info fi;
  1015. char *name = PARAM(arg);
  1016. memset(&fi, 0, sizeof(fi));
  1017. fi.flags = arg->flags;
  1018. if (req->f->conn.proto_minor >= 12)
  1019. req->ctx.umask = arg->umask;
  1020. else
  1021. name = (char *) inarg + sizeof(struct fuse_open_in);
  1022. req->f->op.create(req, nodeid, name, arg->mode, &fi);
  1023. } else
  1024. fuse_reply_err(req, ENOSYS);
  1025. }
  1026. static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1027. {
  1028. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  1029. struct fuse_file_info fi;
  1030. memset(&fi, 0, sizeof(fi));
  1031. fi.flags = arg->flags;
  1032. if (req->f->op.open)
  1033. req->f->op.open(req, nodeid, &fi);
  1034. else
  1035. fuse_reply_open(req, &fi);
  1036. }
  1037. static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1038. {
  1039. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1040. if (req->f->op.read) {
  1041. struct fuse_file_info fi;
  1042. memset(&fi, 0, sizeof(fi));
  1043. fi.fh = arg->fh;
  1044. if (req->f->conn.proto_minor >= 9) {
  1045. fi.lock_owner = arg->lock_owner;
  1046. fi.flags = arg->flags;
  1047. }
  1048. req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
  1049. } else
  1050. fuse_reply_err(req, ENOSYS);
  1051. }
  1052. static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1053. {
  1054. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1055. struct fuse_file_info fi;
  1056. char *param;
  1057. memset(&fi, 0, sizeof(fi));
  1058. fi.fh = arg->fh;
  1059. fi.writepage = arg->write_flags & 1;
  1060. if (req->f->conn.proto_minor < 9) {
  1061. param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1062. } else {
  1063. fi.lock_owner = arg->lock_owner;
  1064. fi.flags = arg->flags;
  1065. param = PARAM(arg);
  1066. }
  1067. if (req->f->op.write)
  1068. req->f->op.write(req, nodeid, param, arg->size,
  1069. arg->offset, &fi);
  1070. else
  1071. fuse_reply_err(req, ENOSYS);
  1072. }
  1073. static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
  1074. const struct fuse_buf *ibuf)
  1075. {
  1076. struct fuse_ll *f = req->f;
  1077. struct fuse_bufvec bufv = {
  1078. .buf[0] = *ibuf,
  1079. .count = 1,
  1080. };
  1081. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1082. struct fuse_file_info fi;
  1083. memset(&fi, 0, sizeof(fi));
  1084. fi.fh = arg->fh;
  1085. fi.writepage = arg->write_flags & 1;
  1086. if (req->f->conn.proto_minor < 9) {
  1087. bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1088. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1089. FUSE_COMPAT_WRITE_IN_SIZE;
  1090. assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
  1091. } else {
  1092. fi.lock_owner = arg->lock_owner;
  1093. fi.flags = arg->flags;
  1094. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1095. bufv.buf[0].mem = PARAM(arg);
  1096. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1097. sizeof(struct fuse_write_in);
  1098. }
  1099. if (bufv.buf[0].size < arg->size) {
  1100. fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
  1101. fuse_reply_err(req, EIO);
  1102. goto out;
  1103. }
  1104. bufv.buf[0].size = arg->size;
  1105. req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
  1106. out:
  1107. /* Need to reset the pipe if ->write_buf() didn't consume all data */
  1108. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1109. fuse_ll_clear_pipe(f);
  1110. }
  1111. static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1112. {
  1113. struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
  1114. struct fuse_file_info fi;
  1115. memset(&fi, 0, sizeof(fi));
  1116. fi.fh = arg->fh;
  1117. fi.flush = 1;
  1118. if (req->f->conn.proto_minor >= 7)
  1119. fi.lock_owner = arg->lock_owner;
  1120. if (req->f->op.flush)
  1121. req->f->op.flush(req, nodeid, &fi);
  1122. else
  1123. fuse_reply_err(req, ENOSYS);
  1124. }
  1125. static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1126. {
  1127. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1128. struct fuse_file_info fi;
  1129. memset(&fi, 0, sizeof(fi));
  1130. fi.flags = arg->flags;
  1131. fi.fh = arg->fh;
  1132. if (req->f->conn.proto_minor >= 8) {
  1133. fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
  1134. fi.lock_owner = arg->lock_owner;
  1135. }
  1136. if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
  1137. fi.flock_release = 1;
  1138. fi.lock_owner = arg->lock_owner;
  1139. }
  1140. if (req->f->op.release)
  1141. req->f->op.release(req, nodeid, &fi);
  1142. else
  1143. fuse_reply_err(req, 0);
  1144. }
  1145. static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1146. {
  1147. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1148. struct fuse_file_info fi;
  1149. memset(&fi, 0, sizeof(fi));
  1150. fi.fh = arg->fh;
  1151. if (req->f->op.fsync)
  1152. req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
  1153. else
  1154. fuse_reply_err(req, ENOSYS);
  1155. }
  1156. static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1157. {
  1158. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  1159. struct fuse_file_info fi;
  1160. memset(&fi, 0, sizeof(fi));
  1161. fi.flags = arg->flags;
  1162. if (req->f->op.opendir)
  1163. req->f->op.opendir(req, nodeid, &fi);
  1164. else
  1165. fuse_reply_open(req, &fi);
  1166. }
  1167. static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1168. {
  1169. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1170. struct fuse_file_info fi;
  1171. memset(&fi, 0, sizeof(fi));
  1172. fi.fh = arg->fh;
  1173. if (req->f->op.readdir)
  1174. req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
  1175. else
  1176. fuse_reply_err(req, ENOSYS);
  1177. }
  1178. static
  1179. void
  1180. do_readdir_plus(fuse_req_t req_,
  1181. fuse_ino_t nodeid_,
  1182. const void *inarg_)
  1183. {
  1184. const struct fuse_read_in *arg;
  1185. struct fuse_file_info ffi = {0};
  1186. arg = (struct fuse_read_in*)inarg_;
  1187. ffi.fh = arg->fh;
  1188. if(req_->f->op.readdir_plus)
  1189. req_->f->op.readdir_plus(req_,nodeid_,arg->size,arg->offset,&ffi);
  1190. else
  1191. fuse_reply_err(req_,ENOSYS);
  1192. }
  1193. static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1194. {
  1195. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1196. struct fuse_file_info fi;
  1197. memset(&fi, 0, sizeof(fi));
  1198. fi.flags = arg->flags;
  1199. fi.fh = arg->fh;
  1200. if (req->f->op.releasedir)
  1201. req->f->op.releasedir(req, nodeid, &fi);
  1202. else
  1203. fuse_reply_err(req, 0);
  1204. }
  1205. static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1206. {
  1207. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1208. struct fuse_file_info fi;
  1209. memset(&fi, 0, sizeof(fi));
  1210. fi.fh = arg->fh;
  1211. if (req->f->op.fsyncdir)
  1212. req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
  1213. else
  1214. fuse_reply_err(req, ENOSYS);
  1215. }
  1216. static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1217. {
  1218. (void) nodeid;
  1219. (void) inarg;
  1220. if (req->f->op.statfs)
  1221. req->f->op.statfs(req, nodeid);
  1222. else {
  1223. struct statvfs buf = {
  1224. .f_namemax = 255,
  1225. .f_bsize = 512,
  1226. };
  1227. fuse_reply_statfs(req, &buf);
  1228. }
  1229. }
  1230. static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1231. {
  1232. struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
  1233. char *name = PARAM(arg);
  1234. char *value = name + strlen(name) + 1;
  1235. if (req->f->op.setxattr)
  1236. req->f->op.setxattr(req, nodeid, name, value, arg->size,
  1237. arg->flags);
  1238. else
  1239. fuse_reply_err(req, ENOSYS);
  1240. }
  1241. static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1242. {
  1243. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1244. if (req->f->op.getxattr)
  1245. req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
  1246. else
  1247. fuse_reply_err(req, ENOSYS);
  1248. }
  1249. static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1250. {
  1251. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1252. if (req->f->op.listxattr)
  1253. req->f->op.listxattr(req, nodeid, arg->size);
  1254. else
  1255. fuse_reply_err(req, ENOSYS);
  1256. }
  1257. static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1258. {
  1259. char *name = (char *) inarg;
  1260. if (req->f->op.removexattr)
  1261. req->f->op.removexattr(req, nodeid, name);
  1262. else
  1263. fuse_reply_err(req, ENOSYS);
  1264. }
  1265. static void convert_fuse_file_lock(struct fuse_file_lock *fl,
  1266. struct flock *flock)
  1267. {
  1268. memset(flock, 0, sizeof(struct flock));
  1269. flock->l_type = fl->type;
  1270. flock->l_whence = SEEK_SET;
  1271. flock->l_start = fl->start;
  1272. if (fl->end == OFFSET_MAX)
  1273. flock->l_len = 0;
  1274. else
  1275. flock->l_len = fl->end - fl->start + 1;
  1276. flock->l_pid = fl->pid;
  1277. }
  1278. static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1279. {
  1280. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1281. struct fuse_file_info fi;
  1282. struct flock flock;
  1283. memset(&fi, 0, sizeof(fi));
  1284. fi.fh = arg->fh;
  1285. fi.lock_owner = arg->owner;
  1286. convert_fuse_file_lock(&arg->lk, &flock);
  1287. if (req->f->op.getlk)
  1288. req->f->op.getlk(req, nodeid, &fi, &flock);
  1289. else
  1290. fuse_reply_err(req, ENOSYS);
  1291. }
  1292. static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
  1293. const void *inarg, int sleep)
  1294. {
  1295. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1296. struct fuse_file_info fi;
  1297. struct flock flock;
  1298. memset(&fi, 0, sizeof(fi));
  1299. fi.fh = arg->fh;
  1300. fi.lock_owner = arg->owner;
  1301. if (arg->lk_flags & FUSE_LK_FLOCK) {
  1302. int op = 0;
  1303. switch (arg->lk.type) {
  1304. case F_RDLCK:
  1305. op = LOCK_SH;
  1306. break;
  1307. case F_WRLCK:
  1308. op = LOCK_EX;
  1309. break;
  1310. case F_UNLCK:
  1311. op = LOCK_UN;
  1312. break;
  1313. }
  1314. if (!sleep)
  1315. op |= LOCK_NB;
  1316. if (req->f->op.flock)
  1317. req->f->op.flock(req, nodeid, &fi, op);
  1318. else
  1319. fuse_reply_err(req, ENOSYS);
  1320. } else {
  1321. convert_fuse_file_lock(&arg->lk, &flock);
  1322. if (req->f->op.setlk)
  1323. req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
  1324. else
  1325. fuse_reply_err(req, ENOSYS);
  1326. }
  1327. }
  1328. static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1329. {
  1330. do_setlk_common(req, nodeid, inarg, 0);
  1331. }
  1332. static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1333. {
  1334. do_setlk_common(req, nodeid, inarg, 1);
  1335. }
  1336. static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
  1337. {
  1338. struct fuse_req *curr;
  1339. for (curr = f->list.next; curr != &f->list; curr = curr->next) {
  1340. if (curr->unique == req->u.i.unique) {
  1341. fuse_interrupt_func_t func;
  1342. void *data;
  1343. curr->ctr++;
  1344. pthread_mutex_unlock(&f->lock);
  1345. /* Ugh, ugly locking */
  1346. pthread_mutex_lock(&curr->lock);
  1347. pthread_mutex_lock(&f->lock);
  1348. curr->interrupted = 1;
  1349. func = curr->u.ni.func;
  1350. data = curr->u.ni.data;
  1351. pthread_mutex_unlock(&f->lock);
  1352. if (func)
  1353. func(curr, data);
  1354. pthread_mutex_unlock(&curr->lock);
  1355. pthread_mutex_lock(&f->lock);
  1356. curr->ctr--;
  1357. if (!curr->ctr)
  1358. destroy_req(curr);
  1359. return 1;
  1360. }
  1361. }
  1362. for (curr = f->interrupts.next; curr != &f->interrupts;
  1363. curr = curr->next) {
  1364. if (curr->u.i.unique == req->u.i.unique)
  1365. return 1;
  1366. }
  1367. return 0;
  1368. }
  1369. static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1370. {
  1371. struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
  1372. struct fuse_ll *f = req->f;
  1373. (void) nodeid;
  1374. if (f->debug)
  1375. fprintf(stderr, "INTERRUPT: %llu\n",
  1376. (unsigned long long) arg->unique);
  1377. req->u.i.unique = arg->unique;
  1378. pthread_mutex_lock(&f->lock);
  1379. if (find_interrupted(f, req))
  1380. destroy_req(req);
  1381. else
  1382. list_add_req(req, &f->interrupts);
  1383. pthread_mutex_unlock(&f->lock);
  1384. }
  1385. static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
  1386. {
  1387. struct fuse_req *curr;
  1388. for (curr = f->interrupts.next; curr != &f->interrupts;
  1389. curr = curr->next) {
  1390. if (curr->u.i.unique == req->unique) {
  1391. req->interrupted = 1;
  1392. list_del_req(curr);
  1393. free(curr);
  1394. return NULL;
  1395. }
  1396. }
  1397. curr = f->interrupts.next;
  1398. if (curr != &f->interrupts) {
  1399. list_del_req(curr);
  1400. list_init_req(curr);
  1401. return curr;
  1402. } else
  1403. return NULL;
  1404. }
  1405. static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1406. {
  1407. struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
  1408. if (req->f->op.bmap)
  1409. req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
  1410. else
  1411. fuse_reply_err(req, ENOSYS);
  1412. }
  1413. static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1414. {
  1415. struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
  1416. unsigned int flags = arg->flags;
  1417. void *in_buf = arg->in_size ? PARAM(arg) : NULL;
  1418. struct fuse_file_info fi;
  1419. if (flags & FUSE_IOCTL_DIR &&
  1420. !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
  1421. fuse_reply_err(req, ENOTTY);
  1422. return;
  1423. }
  1424. memset(&fi, 0, sizeof(fi));
  1425. fi.fh = arg->fh;
  1426. if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
  1427. !(flags & FUSE_IOCTL_32BIT)) {
  1428. req->ioctl_64bit = 1;
  1429. }
  1430. if (req->f->op.ioctl)
  1431. req->f->op.ioctl(req, nodeid, arg->cmd,
  1432. (void *)(uintptr_t)arg->arg, &fi, flags,
  1433. in_buf, arg->in_size, arg->out_size);
  1434. else
  1435. fuse_reply_err(req, ENOSYS);
  1436. }
  1437. void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
  1438. {
  1439. free(ph);
  1440. }
  1441. static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1442. {
  1443. struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
  1444. struct fuse_file_info fi;
  1445. memset(&fi, 0, sizeof(fi));
  1446. fi.fh = arg->fh;
  1447. if (req->f->op.poll) {
  1448. struct fuse_pollhandle *ph = NULL;
  1449. if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
  1450. ph = malloc(sizeof(struct fuse_pollhandle));
  1451. if (ph == NULL) {
  1452. fuse_reply_err(req, ENOMEM);
  1453. return;
  1454. }
  1455. ph->kh = arg->kh;
  1456. ph->ch = req->ch;
  1457. ph->f = req->f;
  1458. }
  1459. req->f->op.poll(req, nodeid, &fi, ph);
  1460. } else {
  1461. fuse_reply_err(req, ENOSYS);
  1462. }
  1463. }
  1464. static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1465. {
  1466. struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
  1467. struct fuse_file_info fi;
  1468. memset(&fi, 0, sizeof(fi));
  1469. fi.fh = arg->fh;
  1470. if (req->f->op.fallocate)
  1471. req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
  1472. else
  1473. fuse_reply_err(req, ENOSYS);
  1474. }
  1475. static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1476. {
  1477. struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
  1478. struct fuse_init_out outarg;
  1479. struct fuse_ll *f = req->f;
  1480. size_t bufsize = fuse_chan_bufsize(req->ch);
  1481. (void) nodeid;
  1482. if (f->debug) {
  1483. fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
  1484. if (arg->major == 7 && arg->minor >= 6) {
  1485. fprintf(stderr, "flags=0x%08x\n", arg->flags);
  1486. fprintf(stderr, "max_readahead=0x%08x\n",
  1487. arg->max_readahead);
  1488. }
  1489. }
  1490. f->conn.proto_major = arg->major;
  1491. f->conn.proto_minor = arg->minor;
  1492. f->conn.capable = 0;
  1493. f->conn.want = 0;
  1494. memset(&outarg, 0, sizeof(outarg));
  1495. outarg.major = FUSE_KERNEL_VERSION;
  1496. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  1497. outarg.max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  1498. if (arg->major < 7) {
  1499. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  1500. arg->major, arg->minor);
  1501. fuse_reply_err(req, EPROTO);
  1502. return;
  1503. }
  1504. if (arg->major > 7) {
  1505. /* Wait for a second INIT request with a 7.X version */
  1506. send_reply_ok(req, &outarg, sizeof(outarg));
  1507. return;
  1508. }
  1509. if (arg->minor >= 6) {
  1510. if (arg->max_readahead < f->conn.max_readahead)
  1511. f->conn.max_readahead = arg->max_readahead;
  1512. if (arg->flags & FUSE_ASYNC_READ)
  1513. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  1514. if (arg->flags & FUSE_POSIX_LOCKS)
  1515. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  1516. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  1517. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  1518. if (arg->flags & FUSE_EXPORT_SUPPORT)
  1519. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  1520. if (arg->flags & FUSE_BIG_WRITES)
  1521. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  1522. if (arg->flags & FUSE_DONT_MASK)
  1523. f->conn.capable |= FUSE_CAP_DONT_MASK;
  1524. if (arg->flags & FUSE_FLOCK_LOCKS)
  1525. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  1526. if (arg->flags & FUSE_POSIX_ACL)
  1527. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  1528. if (arg->flags & FUSE_CACHE_SYMLINKS)
  1529. f->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
  1530. if (arg->flags & FUSE_ASYNC_DIO)
  1531. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  1532. if (arg->flags & FUSE_PARALLEL_DIROPS)
  1533. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  1534. if (arg->flags & FUSE_MAX_PAGES)
  1535. f->conn.capable |= FUSE_CAP_MAX_PAGES;
  1536. if (arg->flags & FUSE_WRITEBACK_CACHE)
  1537. f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
  1538. if (arg->flags & FUSE_DO_READDIRPLUS)
  1539. f->conn.capable |= FUSE_CAP_READDIR_PLUS;
  1540. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  1541. f->conn.capable |= FUSE_CAP_READDIR_PLUS_AUTO;
  1542. } else {
  1543. f->conn.want &= ~FUSE_CAP_ASYNC_READ;
  1544. f->conn.max_readahead = 0;
  1545. }
  1546. if (req->f->conn.proto_minor >= 14) {
  1547. #ifdef HAVE_SPLICE
  1548. #ifdef HAVE_VMSPLICE
  1549. f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
  1550. if (f->splice_write)
  1551. f->conn.want |= FUSE_CAP_SPLICE_WRITE;
  1552. if (f->splice_move)
  1553. f->conn.want |= FUSE_CAP_SPLICE_MOVE;
  1554. #endif
  1555. f->conn.capable |= FUSE_CAP_SPLICE_READ;
  1556. if (f->splice_read)
  1557. f->conn.want |= FUSE_CAP_SPLICE_READ;
  1558. #endif
  1559. }
  1560. if (req->f->conn.proto_minor >= 18)
  1561. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1562. if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1563. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1564. if (f->op.flock && !f->no_remote_flock)
  1565. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1566. if (bufsize < FUSE_MIN_READ_BUFFER) {
  1567. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1568. bufsize);
  1569. bufsize = FUSE_MIN_READ_BUFFER;
  1570. }
  1571. bufsize -= 4096;
  1572. if (bufsize < f->conn.max_write)
  1573. f->conn.max_write = bufsize;
  1574. f->got_init = 1;
  1575. if (f->op.init)
  1576. f->op.init(f->userdata, &f->conn);
  1577. if (f->no_splice_read)
  1578. f->conn.want &= ~FUSE_CAP_SPLICE_READ;
  1579. if (f->no_splice_write)
  1580. f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
  1581. if (f->no_splice_move)
  1582. f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
  1583. if ((arg->flags & FUSE_MAX_PAGES) && (f->conn.want & FUSE_CAP_MAX_PAGES))
  1584. {
  1585. outarg.flags |= FUSE_MAX_PAGES;
  1586. outarg.max_pages = f->conn.max_pages;
  1587. }
  1588. if (f->conn.want & FUSE_CAP_ASYNC_READ)
  1589. outarg.flags |= FUSE_ASYNC_READ;
  1590. if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1591. outarg.flags |= FUSE_POSIX_LOCKS;
  1592. if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1593. outarg.flags |= FUSE_ATOMIC_O_TRUNC;
  1594. if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1595. outarg.flags |= FUSE_EXPORT_SUPPORT;
  1596. if (f->conn.want & FUSE_CAP_BIG_WRITES)
  1597. outarg.flags |= FUSE_BIG_WRITES;
  1598. if (f->conn.want & FUSE_CAP_DONT_MASK)
  1599. outarg.flags |= FUSE_DONT_MASK;
  1600. if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1601. outarg.flags |= FUSE_FLOCK_LOCKS;
  1602. if (f->conn.want & FUSE_CAP_POSIX_ACL)
  1603. outarg.flags |= FUSE_POSIX_ACL;
  1604. if (f->conn.want & FUSE_CAP_CACHE_SYMLINKS)
  1605. outarg.flags |= FUSE_CACHE_SYMLINKS;
  1606. if (f->conn.want & FUSE_CAP_ASYNC_DIO)
  1607. outarg.flags |= FUSE_ASYNC_DIO;
  1608. if (f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1609. outarg.flags |= FUSE_PARALLEL_DIROPS;
  1610. if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
  1611. outarg.flags |= FUSE_WRITEBACK_CACHE;
  1612. if (f->conn.want & FUSE_CAP_READDIR_PLUS)
  1613. outarg.flags |= FUSE_DO_READDIRPLUS;
  1614. if (f->conn.want & FUSE_CAP_READDIR_PLUS_AUTO)
  1615. outarg.flags |= FUSE_READDIRPLUS_AUTO;
  1616. outarg.max_readahead = f->conn.max_readahead;
  1617. outarg.max_write = f->conn.max_write;
  1618. if (f->conn.proto_minor >= 13) {
  1619. if (f->conn.max_background >= (1 << 16))
  1620. f->conn.max_background = (1 << 16) - 1;
  1621. if (f->conn.congestion_threshold > f->conn.max_background)
  1622. f->conn.congestion_threshold = f->conn.max_background;
  1623. if (!f->conn.congestion_threshold) {
  1624. f->conn.congestion_threshold =
  1625. f->conn.max_background * 3 / 4;
  1626. }
  1627. outarg.max_background = f->conn.max_background;
  1628. outarg.congestion_threshold = f->conn.congestion_threshold;
  1629. }
  1630. if (f->debug) {
  1631. fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
  1632. fprintf(stderr, " flags=0x%08x\n", outarg.flags);
  1633. fprintf(stderr, " max_readahead=0x%08x\n",
  1634. outarg.max_readahead);
  1635. fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
  1636. fprintf(stderr, " max_background=%i\n",
  1637. outarg.max_background);
  1638. fprintf(stderr, " congestion_threshold=%i\n",
  1639. outarg.congestion_threshold);
  1640. fprintf(stderr, " max_pages=%d\n",outarg.max_pages);
  1641. }
  1642. size_t outargsize;
  1643. if(arg->minor < 5)
  1644. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1645. else if(arg->minor < 23)
  1646. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1647. else
  1648. outargsize = sizeof(outarg);
  1649. send_reply_ok(req, &outarg, outargsize);
  1650. }
  1651. static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1652. {
  1653. struct fuse_ll *f = req->f;
  1654. (void) nodeid;
  1655. (void) inarg;
  1656. f->got_destroy = 1;
  1657. if (f->op.destroy)
  1658. f->op.destroy(f->userdata);
  1659. send_reply_ok(req, NULL, 0);
  1660. }
  1661. static void list_del_nreq(struct fuse_notify_req *nreq)
  1662. {
  1663. struct fuse_notify_req *prev = nreq->prev;
  1664. struct fuse_notify_req *next = nreq->next;
  1665. prev->next = next;
  1666. next->prev = prev;
  1667. }
  1668. static void list_add_nreq(struct fuse_notify_req *nreq,
  1669. struct fuse_notify_req *next)
  1670. {
  1671. struct fuse_notify_req *prev = next->prev;
  1672. nreq->next = next;
  1673. nreq->prev = prev;
  1674. prev->next = nreq;
  1675. next->prev = nreq;
  1676. }
  1677. static void list_init_nreq(struct fuse_notify_req *nreq)
  1678. {
  1679. nreq->next = nreq;
  1680. nreq->prev = nreq;
  1681. }
  1682. static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
  1683. const void *inarg, const struct fuse_buf *buf)
  1684. {
  1685. struct fuse_ll *f = req->f;
  1686. struct fuse_notify_req *nreq;
  1687. struct fuse_notify_req *head;
  1688. pthread_mutex_lock(&f->lock);
  1689. head = &f->notify_list;
  1690. for (nreq = head->next; nreq != head; nreq = nreq->next) {
  1691. if (nreq->unique == req->unique) {
  1692. list_del_nreq(nreq);
  1693. break;
  1694. }
  1695. }
  1696. pthread_mutex_unlock(&f->lock);
  1697. if (nreq != head)
  1698. nreq->reply(nreq, req, nodeid, inarg, buf);
  1699. }
  1700. static
  1701. void
  1702. do_copy_file_range(fuse_req_t req_,
  1703. fuse_ino_t nodeid_in_,
  1704. const void *arg_)
  1705. {
  1706. struct fuse_file_info ffi_in = {0};
  1707. struct fuse_file_info ffi_out = {0};
  1708. struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in*)arg_;
  1709. ffi_in.fh = arg->fh_in;
  1710. ffi_out.fh = arg->fh_out;
  1711. if(req_->f->op.copy_file_range == NULL)
  1712. fuse_reply_err(req_,ENOSYS);
  1713. else
  1714. req_->f->op.copy_file_range(req_,
  1715. nodeid_in_,
  1716. arg->off_in,
  1717. &ffi_in,
  1718. arg->nodeid_out,
  1719. arg->off_out,
  1720. &ffi_out,
  1721. arg->len,
  1722. arg->flags);
  1723. }
  1724. static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
  1725. int notify_code, struct iovec *iov, int count)
  1726. {
  1727. struct fuse_out_header out;
  1728. if (!f->got_init)
  1729. return -ENOTCONN;
  1730. out.unique = 0;
  1731. out.error = notify_code;
  1732. iov[0].iov_base = &out;
  1733. iov[0].iov_len = sizeof(struct fuse_out_header);
  1734. return fuse_send_msg(f, ch, iov, count);
  1735. }
  1736. int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
  1737. {
  1738. if (ph != NULL) {
  1739. struct fuse_notify_poll_wakeup_out outarg;
  1740. struct iovec iov[2];
  1741. outarg.kh = ph->kh;
  1742. iov[1].iov_base = &outarg;
  1743. iov[1].iov_len = sizeof(outarg);
  1744. return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
  1745. } else {
  1746. return 0;
  1747. }
  1748. }
  1749. int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
  1750. off_t off, off_t len)
  1751. {
  1752. struct fuse_notify_inval_inode_out outarg;
  1753. struct fuse_ll *f;
  1754. struct iovec iov[2];
  1755. if (!ch)
  1756. return -EINVAL;
  1757. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1758. if (!f)
  1759. return -ENODEV;
  1760. outarg.ino = ino;
  1761. outarg.off = off;
  1762. outarg.len = len;
  1763. iov[1].iov_base = &outarg;
  1764. iov[1].iov_len = sizeof(outarg);
  1765. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
  1766. }
  1767. int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
  1768. const char *name, size_t namelen)
  1769. {
  1770. struct fuse_notify_inval_entry_out outarg;
  1771. struct fuse_ll *f;
  1772. struct iovec iov[3];
  1773. if (!ch)
  1774. return -EINVAL;
  1775. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1776. if (!f)
  1777. return -ENODEV;
  1778. outarg.parent = parent;
  1779. outarg.namelen = namelen;
  1780. outarg.padding = 0;
  1781. iov[1].iov_base = &outarg;
  1782. iov[1].iov_len = sizeof(outarg);
  1783. iov[2].iov_base = (void *)name;
  1784. iov[2].iov_len = namelen + 1;
  1785. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
  1786. }
  1787. int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  1788. fuse_ino_t parent, fuse_ino_t child,
  1789. const char *name, size_t namelen)
  1790. {
  1791. struct fuse_notify_delete_out outarg;
  1792. struct fuse_ll *f;
  1793. struct iovec iov[3];
  1794. if (!ch)
  1795. return -EINVAL;
  1796. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1797. if (!f)
  1798. return -ENODEV;
  1799. if (f->conn.proto_minor < 18)
  1800. return -ENOSYS;
  1801. outarg.parent = parent;
  1802. outarg.child = child;
  1803. outarg.namelen = namelen;
  1804. outarg.padding = 0;
  1805. iov[1].iov_base = &outarg;
  1806. iov[1].iov_len = sizeof(outarg);
  1807. iov[2].iov_base = (void *)name;
  1808. iov[2].iov_len = namelen + 1;
  1809. return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
  1810. }
  1811. int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
  1812. off_t offset, struct fuse_bufvec *bufv,
  1813. enum fuse_buf_copy_flags flags)
  1814. {
  1815. struct fuse_out_header out;
  1816. struct fuse_notify_store_out outarg;
  1817. struct fuse_ll *f;
  1818. struct iovec iov[3];
  1819. size_t size = fuse_buf_size(bufv);
  1820. int res;
  1821. if (!ch)
  1822. return -EINVAL;
  1823. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1824. if (!f)
  1825. return -ENODEV;
  1826. if (f->conn.proto_minor < 15)
  1827. return -ENOSYS;
  1828. out.unique = 0;
  1829. out.error = FUSE_NOTIFY_STORE;
  1830. outarg.nodeid = ino;
  1831. outarg.offset = offset;
  1832. outarg.size = size;
  1833. outarg.padding = 0;
  1834. iov[0].iov_base = &out;
  1835. iov[0].iov_len = sizeof(out);
  1836. iov[1].iov_base = &outarg;
  1837. iov[1].iov_len = sizeof(outarg);
  1838. res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
  1839. if (res > 0)
  1840. res = -res;
  1841. return res;
  1842. }
  1843. struct fuse_retrieve_req {
  1844. struct fuse_notify_req nreq;
  1845. void *cookie;
  1846. };
  1847. static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
  1848. fuse_req_t req, fuse_ino_t ino,
  1849. const void *inarg,
  1850. const struct fuse_buf *ibuf)
  1851. {
  1852. struct fuse_ll *f = req->f;
  1853. struct fuse_retrieve_req *rreq =
  1854. container_of(nreq, struct fuse_retrieve_req, nreq);
  1855. const struct fuse_notify_retrieve_in *arg = inarg;
  1856. struct fuse_bufvec bufv = {
  1857. .buf[0] = *ibuf,
  1858. .count = 1,
  1859. };
  1860. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1861. bufv.buf[0].mem = PARAM(arg);
  1862. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1863. sizeof(struct fuse_notify_retrieve_in);
  1864. if (bufv.buf[0].size < arg->size) {
  1865. fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
  1866. fuse_reply_none(req);
  1867. goto out;
  1868. }
  1869. bufv.buf[0].size = arg->size;
  1870. if (req->f->op.retrieve_reply) {
  1871. req->f->op.retrieve_reply(req, rreq->cookie, ino,
  1872. arg->offset, &bufv);
  1873. } else {
  1874. fuse_reply_none(req);
  1875. }
  1876. out:
  1877. free(rreq);
  1878. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1879. fuse_ll_clear_pipe(f);
  1880. }
  1881. int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
  1882. size_t size, off_t offset, void *cookie)
  1883. {
  1884. struct fuse_notify_retrieve_out outarg;
  1885. struct fuse_ll *f;
  1886. struct iovec iov[2];
  1887. struct fuse_retrieve_req *rreq;
  1888. int err;
  1889. if (!ch)
  1890. return -EINVAL;
  1891. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1892. if (!f)
  1893. return -ENODEV;
  1894. if (f->conn.proto_minor < 15)
  1895. return -ENOSYS;
  1896. rreq = malloc(sizeof(*rreq));
  1897. if (rreq == NULL)
  1898. return -ENOMEM;
  1899. pthread_mutex_lock(&f->lock);
  1900. rreq->cookie = cookie;
  1901. rreq->nreq.unique = f->notify_ctr++;
  1902. rreq->nreq.reply = fuse_ll_retrieve_reply;
  1903. list_add_nreq(&rreq->nreq, &f->notify_list);
  1904. pthread_mutex_unlock(&f->lock);
  1905. outarg.notify_unique = rreq->nreq.unique;
  1906. outarg.nodeid = ino;
  1907. outarg.offset = offset;
  1908. outarg.size = size;
  1909. iov[1].iov_base = &outarg;
  1910. iov[1].iov_len = sizeof(outarg);
  1911. err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
  1912. if (err) {
  1913. pthread_mutex_lock(&f->lock);
  1914. list_del_nreq(&rreq->nreq);
  1915. pthread_mutex_unlock(&f->lock);
  1916. free(rreq);
  1917. }
  1918. return err;
  1919. }
  1920. void *fuse_req_userdata(fuse_req_t req)
  1921. {
  1922. return req->f->userdata;
  1923. }
  1924. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
  1925. {
  1926. return &req->ctx;
  1927. }
  1928. /*
  1929. * The size of fuse_ctx got extended, so need to be careful about
  1930. * incompatibility (i.e. a new binary cannot work with an old
  1931. * library).
  1932. */
  1933. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
  1934. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
  1935. {
  1936. return fuse_req_ctx(req);
  1937. }
  1938. #ifndef __NetBSD__
  1939. FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
  1940. #endif
  1941. void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
  1942. void *data)
  1943. {
  1944. pthread_mutex_lock(&req->lock);
  1945. pthread_mutex_lock(&req->f->lock);
  1946. req->u.ni.func = func;
  1947. req->u.ni.data = data;
  1948. pthread_mutex_unlock(&req->f->lock);
  1949. if (req->interrupted && func)
  1950. func(req, data);
  1951. pthread_mutex_unlock(&req->lock);
  1952. }
  1953. int fuse_req_interrupted(fuse_req_t req)
  1954. {
  1955. int interrupted;
  1956. pthread_mutex_lock(&req->f->lock);
  1957. interrupted = req->interrupted;
  1958. pthread_mutex_unlock(&req->f->lock);
  1959. return interrupted;
  1960. }
  1961. static struct {
  1962. void (*func)(fuse_req_t, fuse_ino_t, const void *);
  1963. const char *name;
  1964. } fuse_ll_ops[] =
  1965. {
  1966. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1967. [FUSE_FORGET] = { do_forget, "FORGET" },
  1968. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1969. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1970. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1971. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1972. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1973. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1974. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1975. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1976. [FUSE_RENAME] = { do_rename, "RENAME" },
  1977. [FUSE_LINK] = { do_link, "LINK" },
  1978. [FUSE_OPEN] = { do_open, "OPEN" },
  1979. [FUSE_READ] = { do_read, "READ" },
  1980. [FUSE_WRITE] = { do_write, "WRITE" },
  1981. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1982. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1983. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1984. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1985. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1986. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1987. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1988. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1989. [FUSE_INIT] = { do_init, "INIT" },
  1990. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1991. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1992. [FUSE_READDIRPLUS] = { do_readdir_plus, "READDIR_PLUS" },
  1993. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1994. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1995. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1996. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1997. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1998. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1999. [FUSE_CREATE] = { do_create, "CREATE" },
  2000. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  2001. [FUSE_BMAP] = { do_bmap, "BMAP" },
  2002. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  2003. [FUSE_POLL] = { do_poll, "POLL" },
  2004. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  2005. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  2006. [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
  2007. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  2008. [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
  2009. [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
  2010. };
  2011. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  2012. static const char *opname(enum fuse_opcode opcode)
  2013. {
  2014. if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
  2015. return "???";
  2016. else
  2017. return fuse_ll_ops[opcode].name;
  2018. }
  2019. static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
  2020. struct fuse_bufvec *src)
  2021. {
  2022. int res = fuse_buf_copy(dst, src, 0);
  2023. if (res < 0) {
  2024. fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
  2025. return res;
  2026. }
  2027. if (res < fuse_buf_size(dst)) {
  2028. fprintf(stderr, "fuse: copy from pipe: short read\n");
  2029. return -1;
  2030. }
  2031. return 0;
  2032. }
  2033. static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
  2034. struct fuse_chan *ch)
  2035. {
  2036. struct fuse_ll *f = (struct fuse_ll *) data;
  2037. const size_t write_header_size = sizeof(struct fuse_in_header) +
  2038. sizeof(struct fuse_write_in);
  2039. struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
  2040. struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
  2041. struct fuse_in_header *in;
  2042. const void *inarg;
  2043. struct fuse_req *req;
  2044. void *mbuf = NULL;
  2045. int err;
  2046. int res;
  2047. if (buf->flags & FUSE_BUF_IS_FD) {
  2048. if (buf->size < tmpbuf.buf[0].size)
  2049. tmpbuf.buf[0].size = buf->size;
  2050. mbuf = malloc(tmpbuf.buf[0].size);
  2051. if (mbuf == NULL) {
  2052. fprintf(stderr, "fuse: failed to allocate header\n");
  2053. goto clear_pipe;
  2054. }
  2055. tmpbuf.buf[0].mem = mbuf;
  2056. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  2057. if (res < 0)
  2058. goto clear_pipe;
  2059. in = mbuf;
  2060. } else {
  2061. in = buf->mem;
  2062. }
  2063. if (f->debug) {
  2064. fprintf(stderr,
  2065. "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
  2066. (unsigned long long) in->unique,
  2067. opname((enum fuse_opcode) in->opcode), in->opcode,
  2068. (unsigned long) in->nodeid, buf->size, in->pid);
  2069. }
  2070. req = fuse_ll_alloc_req(f);
  2071. if (req == NULL) {
  2072. struct fuse_out_header out = {
  2073. .unique = in->unique,
  2074. .error = -ENOMEM,
  2075. };
  2076. struct iovec iov = {
  2077. .iov_base = &out,
  2078. .iov_len = sizeof(struct fuse_out_header),
  2079. };
  2080. fuse_send_msg(f, ch, &iov, 1);
  2081. goto clear_pipe;
  2082. }
  2083. req->unique = in->unique;
  2084. req->ctx.uid = in->uid;
  2085. req->ctx.gid = in->gid;
  2086. req->ctx.pid = in->pid;
  2087. req->ch = ch;
  2088. err = EIO;
  2089. if (!f->got_init) {
  2090. enum fuse_opcode expected;
  2091. expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
  2092. if (in->opcode != expected)
  2093. goto reply_err;
  2094. } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
  2095. goto reply_err;
  2096. err = EACCES;
  2097. if (f->allow_root &&
  2098. in->uid != f->owner &&
  2099. in->uid != 0 &&
  2100. in->opcode != FUSE_INIT &&
  2101. in->opcode != FUSE_READ &&
  2102. in->opcode != FUSE_WRITE &&
  2103. in->opcode != FUSE_FSYNC &&
  2104. in->opcode != FUSE_RELEASE &&
  2105. in->opcode != FUSE_READDIR &&
  2106. in->opcode != FUSE_READDIRPLUS &&
  2107. in->opcode != FUSE_FSYNCDIR &&
  2108. in->opcode != FUSE_RELEASEDIR &&
  2109. in->opcode != FUSE_NOTIFY_REPLY)
  2110. goto reply_err;
  2111. err = ENOSYS;
  2112. if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
  2113. goto reply_err;
  2114. if (in->opcode != FUSE_INTERRUPT) {
  2115. struct fuse_req *intr;
  2116. pthread_mutex_lock(&f->lock);
  2117. intr = check_interrupt(f, req);
  2118. list_add_req(req, &f->list);
  2119. pthread_mutex_unlock(&f->lock);
  2120. if (intr)
  2121. fuse_reply_err(intr, EAGAIN);
  2122. }
  2123. if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
  2124. (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
  2125. in->opcode != FUSE_NOTIFY_REPLY) {
  2126. void *newmbuf;
  2127. err = ENOMEM;
  2128. newmbuf = realloc(mbuf, buf->size);
  2129. if (newmbuf == NULL)
  2130. goto reply_err;
  2131. mbuf = newmbuf;
  2132. tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
  2133. tmpbuf.buf[0].mem = mbuf + write_header_size;
  2134. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  2135. err = -res;
  2136. if (res < 0)
  2137. goto reply_err;
  2138. in = mbuf;
  2139. }
  2140. inarg = (void *) &in[1];
  2141. if (in->opcode == FUSE_WRITE && f->op.write_buf)
  2142. do_write_buf(req, in->nodeid, inarg, buf);
  2143. else if (in->opcode == FUSE_NOTIFY_REPLY)
  2144. do_notify_reply(req, in->nodeid, inarg, buf);
  2145. else
  2146. fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
  2147. out_free:
  2148. free(mbuf);
  2149. return;
  2150. reply_err:
  2151. fuse_reply_err(req, err);
  2152. clear_pipe:
  2153. if (buf->flags & FUSE_BUF_IS_FD)
  2154. fuse_ll_clear_pipe(f);
  2155. goto out_free;
  2156. }
  2157. static void fuse_ll_process(void *data, const char *buf, size_t len,
  2158. struct fuse_chan *ch)
  2159. {
  2160. struct fuse_buf fbuf = {
  2161. .mem = (void *) buf,
  2162. .size = len,
  2163. };
  2164. fuse_ll_process_buf(data, &fbuf, ch);
  2165. }
  2166. enum {
  2167. KEY_HELP,
  2168. KEY_VERSION,
  2169. };
  2170. static const struct fuse_opt fuse_ll_opts[] = {
  2171. { "debug", offsetof(struct fuse_ll, debug), 1 },
  2172. { "-d", offsetof(struct fuse_ll, debug), 1 },
  2173. { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
  2174. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  2175. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  2176. { "congestion_threshold=%u",
  2177. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  2178. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2179. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2180. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2181. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2182. { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
  2183. { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
  2184. { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
  2185. { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
  2186. { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
  2187. { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
  2188. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  2189. FUSE_OPT_KEY("-h", KEY_HELP),
  2190. FUSE_OPT_KEY("--help", KEY_HELP),
  2191. FUSE_OPT_KEY("-V", KEY_VERSION),
  2192. FUSE_OPT_KEY("--version", KEY_VERSION),
  2193. FUSE_OPT_END
  2194. };
  2195. static void fuse_ll_version(void)
  2196. {
  2197. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  2198. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  2199. }
  2200. static void fuse_ll_help(void)
  2201. {
  2202. fprintf(stderr,
  2203. " -o max_readahead=N set maximum readahead\n"
  2204. " -o max_background=N set number of maximum background requests\n"
  2205. " -o congestion_threshold=N set kernel's congestion threshold\n"
  2206. " -o no_remote_lock disable remote file locking\n"
  2207. " -o no_remote_flock disable remote file locking (BSD)\n"
  2208. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  2209. " -o [no_]splice_write use splice to write to the fuse device\n"
  2210. " -o [no_]splice_move move data while splicing to the fuse device\n"
  2211. " -o [no_]splice_read use splice to read from the fuse device\n"
  2212. );
  2213. }
  2214. static int fuse_ll_opt_proc(void *data, const char *arg, int key,
  2215. struct fuse_args *outargs)
  2216. {
  2217. (void) data; (void) outargs;
  2218. switch (key) {
  2219. case KEY_HELP:
  2220. fuse_ll_help();
  2221. break;
  2222. case KEY_VERSION:
  2223. fuse_ll_version();
  2224. break;
  2225. default:
  2226. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  2227. }
  2228. return -1;
  2229. }
  2230. int fuse_lowlevel_is_lib_option(const char *opt)
  2231. {
  2232. return fuse_opt_match(fuse_ll_opts, opt);
  2233. }
  2234. static void fuse_ll_destroy(void *data)
  2235. {
  2236. struct fuse_ll *f = (struct fuse_ll *) data;
  2237. struct fuse_ll_pipe *llp;
  2238. if (f->got_init && !f->got_destroy) {
  2239. if (f->op.destroy)
  2240. f->op.destroy(f->userdata);
  2241. }
  2242. llp = pthread_getspecific(f->pipe_key);
  2243. if (llp != NULL)
  2244. fuse_ll_pipe_free(llp);
  2245. pthread_key_delete(f->pipe_key);
  2246. pthread_mutex_destroy(&f->lock);
  2247. free(f->cuse_data);
  2248. free(f);
  2249. }
  2250. static void fuse_ll_pipe_destructor(void *data)
  2251. {
  2252. struct fuse_ll_pipe *llp = data;
  2253. fuse_ll_pipe_free(llp);
  2254. }
  2255. #ifdef HAVE_SPLICE
  2256. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2257. struct fuse_chan **chp)
  2258. {
  2259. struct fuse_chan *ch = *chp;
  2260. struct fuse_ll *f = fuse_session_data(se);
  2261. size_t bufsize = buf->size;
  2262. struct fuse_ll_pipe *llp;
  2263. struct fuse_buf tmpbuf;
  2264. int err;
  2265. int res;
  2266. if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
  2267. goto fallback;
  2268. llp = fuse_ll_get_pipe(f);
  2269. if (llp == NULL)
  2270. goto fallback;
  2271. if (llp->size < bufsize) {
  2272. if (llp->can_grow) {
  2273. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
  2274. if (res == -1) {
  2275. llp->can_grow = 0;
  2276. goto fallback;
  2277. }
  2278. llp->size = res;
  2279. }
  2280. if (llp->size < bufsize)
  2281. goto fallback;
  2282. }
  2283. res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
  2284. err = errno;
  2285. if (fuse_session_exited(se))
  2286. return 0;
  2287. if (res == -1) {
  2288. if (err == ENODEV) {
  2289. fuse_session_exit(se);
  2290. return 0;
  2291. }
  2292. if (err != EINTR && err != EAGAIN)
  2293. perror("fuse: splice from device");
  2294. return -err;
  2295. }
  2296. if (res < sizeof(struct fuse_in_header)) {
  2297. fprintf(stderr, "short splice from fuse device\n");
  2298. return -EIO;
  2299. }
  2300. tmpbuf = (struct fuse_buf) {
  2301. .size = res,
  2302. .flags = FUSE_BUF_IS_FD,
  2303. .fd = llp->pipe[0],
  2304. };
  2305. /*
  2306. * Don't bother with zero copy for small requests.
  2307. * fuse_loop_mt() needs to check for FORGET so this more than
  2308. * just an optimization.
  2309. */
  2310. if (res < sizeof(struct fuse_in_header) +
  2311. sizeof(struct fuse_write_in) + pagesize) {
  2312. struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
  2313. struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
  2314. res = fuse_buf_copy(&dst, &src, 0);
  2315. if (res < 0) {
  2316. fprintf(stderr, "fuse: copy from pipe: %s\n",
  2317. strerror(-res));
  2318. fuse_ll_clear_pipe(f);
  2319. return res;
  2320. }
  2321. if (res < tmpbuf.size) {
  2322. fprintf(stderr, "fuse: copy from pipe: short read\n");
  2323. fuse_ll_clear_pipe(f);
  2324. return -EIO;
  2325. }
  2326. buf->size = tmpbuf.size;
  2327. return buf->size;
  2328. }
  2329. *buf = tmpbuf;
  2330. return res;
  2331. fallback:
  2332. res = fuse_chan_recv(chp, buf->mem, bufsize);
  2333. if (res <= 0)
  2334. return res;
  2335. buf->size = res;
  2336. return res;
  2337. }
  2338. #else
  2339. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2340. struct fuse_chan **chp)
  2341. {
  2342. (void) se;
  2343. int res = fuse_chan_recv(chp, buf->mem, buf->size);
  2344. if (res <= 0)
  2345. return res;
  2346. buf->size = res;
  2347. return res;
  2348. }
  2349. #endif
  2350. /*
  2351. * always call fuse_lowlevel_new_common() internally, to work around a
  2352. * misfeature in the FreeBSD runtime linker, which links the old
  2353. * version of a symbol to internal references.
  2354. */
  2355. struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
  2356. const struct fuse_lowlevel_ops *op,
  2357. size_t op_size, void *userdata)
  2358. {
  2359. int err;
  2360. struct fuse_ll *f;
  2361. struct fuse_session *se;
  2362. struct fuse_session_ops sop = {
  2363. .process = fuse_ll_process,
  2364. .destroy = fuse_ll_destroy,
  2365. };
  2366. if (sizeof(struct fuse_lowlevel_ops) < op_size) {
  2367. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  2368. op_size = sizeof(struct fuse_lowlevel_ops);
  2369. }
  2370. f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
  2371. if (f == NULL) {
  2372. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  2373. goto out;
  2374. }
  2375. f->conn.max_write = UINT_MAX;
  2376. f->conn.max_readahead = UINT_MAX;
  2377. list_init_req(&f->list);
  2378. list_init_req(&f->interrupts);
  2379. list_init_nreq(&f->notify_list);
  2380. f->notify_ctr = 1;
  2381. fuse_mutex_init(&f->lock);
  2382. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  2383. if (err) {
  2384. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  2385. strerror(err));
  2386. goto out_free;
  2387. }
  2388. if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  2389. goto out_key_destroy;
  2390. if (f->debug)
  2391. fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
  2392. memcpy(&f->op, op, op_size);
  2393. f->owner = getuid();
  2394. f->userdata = userdata;
  2395. se = fuse_session_new(&sop, f);
  2396. if (!se)
  2397. goto out_key_destroy;
  2398. se->receive_buf = fuse_ll_receive_buf;
  2399. se->process_buf = fuse_ll_process_buf;
  2400. return se;
  2401. out_key_destroy:
  2402. pthread_key_delete(f->pipe_key);
  2403. out_free:
  2404. pthread_mutex_destroy(&f->lock);
  2405. free(f);
  2406. out:
  2407. return NULL;
  2408. }
  2409. struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
  2410. const struct fuse_lowlevel_ops *op,
  2411. size_t op_size, void *userdata)
  2412. {
  2413. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  2414. }
  2415. #ifdef linux
  2416. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
  2417. {
  2418. char *buf;
  2419. size_t bufsize = 1024;
  2420. char path[128];
  2421. int ret;
  2422. int fd;
  2423. unsigned long pid = req->ctx.pid;
  2424. char *s;
  2425. sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
  2426. retry:
  2427. buf = malloc(bufsize);
  2428. if (buf == NULL)
  2429. return -ENOMEM;
  2430. ret = -EIO;
  2431. fd = open(path, O_RDONLY);
  2432. if (fd == -1)
  2433. goto out_free;
  2434. ret = read(fd, buf, bufsize);
  2435. close(fd);
  2436. if (ret == -1) {
  2437. ret = -EIO;
  2438. goto out_free;
  2439. }
  2440. if (ret == bufsize) {
  2441. free(buf);
  2442. bufsize *= 4;
  2443. goto retry;
  2444. }
  2445. ret = -EIO;
  2446. s = strstr(buf, "\nGroups:");
  2447. if (s == NULL)
  2448. goto out_free;
  2449. s += 8;
  2450. ret = 0;
  2451. while (1) {
  2452. char *end;
  2453. unsigned long val = strtoul(s, &end, 0);
  2454. if (end == s)
  2455. break;
  2456. s = end;
  2457. if (ret < size)
  2458. list[ret] = val;
  2459. ret++;
  2460. }
  2461. out_free:
  2462. free(buf);
  2463. return ret;
  2464. }
  2465. #else /* linux */
  2466. /*
  2467. * This is currently not implemented on other than Linux...
  2468. */
  2469. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
  2470. {
  2471. return -ENOSYS;
  2472. }
  2473. #endif
  2474. #if !defined(__FreeBSD__) && !defined(__NetBSD__)
  2475. static void fill_open_compat(struct fuse_open_out *arg,
  2476. const struct fuse_file_info_compat *f)
  2477. {
  2478. arg->fh = f->fh;
  2479. if (f->direct_io)
  2480. arg->open_flags |= FOPEN_DIRECT_IO;
  2481. if (f->keep_cache)
  2482. arg->open_flags |= FOPEN_KEEP_CACHE;
  2483. }
  2484. static void convert_statfs_compat(const struct statfs *compatbuf,
  2485. struct statvfs *buf)
  2486. {
  2487. buf->f_bsize = compatbuf->f_bsize;
  2488. buf->f_blocks = compatbuf->f_blocks;
  2489. buf->f_bfree = compatbuf->f_bfree;
  2490. buf->f_bavail = compatbuf->f_bavail;
  2491. buf->f_files = compatbuf->f_files;
  2492. buf->f_ffree = compatbuf->f_ffree;
  2493. buf->f_namemax = compatbuf->f_namelen;
  2494. }
  2495. int fuse_reply_open_compat(fuse_req_t req,
  2496. const struct fuse_file_info_compat *f)
  2497. {
  2498. struct fuse_open_out arg;
  2499. memset(&arg, 0, sizeof(arg));
  2500. fill_open_compat(&arg, f);
  2501. return send_reply_ok(req, &arg, sizeof(arg));
  2502. }
  2503. int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf)
  2504. {
  2505. struct statvfs newbuf;
  2506. memset(&newbuf, 0, sizeof(newbuf));
  2507. convert_statfs_compat(stbuf, &newbuf);
  2508. return fuse_reply_statfs(req, &newbuf);
  2509. }
  2510. struct fuse_session *fuse_lowlevel_new_compat(const char *opts,
  2511. const struct fuse_lowlevel_ops_compat *op,
  2512. size_t op_size, void *userdata)
  2513. {
  2514. struct fuse_session *se;
  2515. struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
  2516. if (opts &&
  2517. (fuse_opt_add_arg(&args, "") == -1 ||
  2518. fuse_opt_add_arg(&args, "-o") == -1 ||
  2519. fuse_opt_add_arg(&args, opts) == -1)) {
  2520. fuse_opt_free_args(&args);
  2521. return NULL;
  2522. }
  2523. se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
  2524. op_size, userdata);
  2525. fuse_opt_free_args(&args);
  2526. return se;
  2527. }
  2528. struct fuse_ll_compat_conf {
  2529. unsigned max_read;
  2530. int set_max_read;
  2531. };
  2532. static const struct fuse_opt fuse_ll_opts_compat[] = {
  2533. { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 },
  2534. { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 },
  2535. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
  2536. FUSE_OPT_END
  2537. };
  2538. int fuse_sync_compat_args(struct fuse_args *args)
  2539. {
  2540. struct fuse_ll_compat_conf conf;
  2541. memset(&conf, 0, sizeof(conf));
  2542. if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1)
  2543. return -1;
  2544. if (fuse_opt_insert_arg(args, 1, "-osync_read"))
  2545. return -1;
  2546. if (conf.set_max_read) {
  2547. char tmpbuf[64];
  2548. sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
  2549. if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
  2550. return -1;
  2551. }
  2552. return 0;
  2553. }
  2554. FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
  2555. FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
  2556. FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
  2557. #else /* __FreeBSD__ || __NetBSD__ */
  2558. int fuse_sync_compat_args(struct fuse_args *args)
  2559. {
  2560. (void) args;
  2561. return 0;
  2562. }
  2563. #endif /* __FreeBSD__ || __NetBSD__ */
  2564. struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args,
  2565. const struct fuse_lowlevel_ops_compat25 *op,
  2566. size_t op_size, void *userdata)
  2567. {
  2568. if (fuse_sync_compat_args(args) == -1)
  2569. return NULL;
  2570. return fuse_lowlevel_new_common(args,
  2571. (const struct fuse_lowlevel_ops *) op,
  2572. op_size, userdata);
  2573. }
  2574. FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");