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.

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