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.

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