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.

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