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.

2637 lines
60 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_flush(fuse_req_t req,
  1095. uint64_t nodeid,
  1096. const void *inarg)
  1097. {
  1098. fuse_file_info_t fi = {0};
  1099. struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
  1100. fi.fh = arg->fh;
  1101. fi.flush = 1;
  1102. if(req->f->conn.proto_minor >= 7)
  1103. fi.lock_owner = arg->lock_owner;
  1104. req->f->op.flush(req,nodeid,&fi);
  1105. }
  1106. static
  1107. void
  1108. do_release(fuse_req_t req,
  1109. uint64_t nodeid,
  1110. const void *inarg)
  1111. {
  1112. fuse_file_info_t fi = {0};
  1113. struct fuse_release_in *arg = (struct fuse_release_in*)inarg;
  1114. fi.flags = arg->flags;
  1115. fi.fh = arg->fh;
  1116. if(req->f->conn.proto_minor >= 8)
  1117. {
  1118. fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
  1119. fi.lock_owner = arg->lock_owner;
  1120. }
  1121. if(arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK)
  1122. {
  1123. fi.flock_release = 1;
  1124. fi.lock_owner = arg->lock_owner;
  1125. }
  1126. req->f->op.release(req,nodeid,&fi);
  1127. }
  1128. static
  1129. void
  1130. do_fsync(fuse_req_t req,
  1131. uint64_t nodeid,
  1132. const void *inarg)
  1133. {
  1134. fuse_file_info_t fi = {0};
  1135. struct fuse_fsync_in *arg = (struct fuse_fsync_in*)inarg;
  1136. fi.fh = arg->fh;
  1137. req->f->op.fsync(req,nodeid,arg->fsync_flags & 1, &fi);
  1138. }
  1139. static
  1140. void
  1141. do_opendir(fuse_req_t req,
  1142. uint64_t nodeid,
  1143. const void *inarg)
  1144. {
  1145. fuse_file_info_t fi = {0};
  1146. struct fuse_open_in *arg = (struct fuse_open_in*)inarg;
  1147. fi.flags = arg->flags;
  1148. req->f->op.opendir(req,nodeid,&fi);
  1149. }
  1150. static
  1151. void
  1152. do_readdir(fuse_req_t req,
  1153. uint64_t nodeid,
  1154. const void *inarg)
  1155. {
  1156. fuse_file_info_t fi = {0};
  1157. struct fuse_read_in *arg = (struct fuse_read_in*)inarg;
  1158. fi.fh = arg->fh;
  1159. req->f->op.readdir(req,nodeid,arg->size,arg->offset,&fi);
  1160. }
  1161. static
  1162. void
  1163. do_readdir_plus(fuse_req_t req_,
  1164. uint64_t nodeid_,
  1165. const void *inarg_)
  1166. {
  1167. const struct fuse_read_in *arg;
  1168. fuse_file_info_t ffi = {0};
  1169. arg = (struct fuse_read_in*)inarg_;
  1170. ffi.fh = arg->fh;
  1171. req_->f->op.readdir_plus(req_,nodeid_,arg->size,arg->offset,&ffi);
  1172. }
  1173. static
  1174. void
  1175. do_releasedir(fuse_req_t req,
  1176. uint64_t nodeid,
  1177. const void *inarg)
  1178. {
  1179. fuse_file_info_t fi = {0};
  1180. struct fuse_release_in *arg = (struct fuse_release_in*)inarg;
  1181. fi.flags = arg->flags;
  1182. fi.fh = arg->fh;
  1183. req->f->op.releasedir(req,nodeid,&fi);
  1184. }
  1185. static
  1186. void
  1187. do_fsyncdir(fuse_req_t req,
  1188. uint64_t nodeid,
  1189. const void *inarg)
  1190. {
  1191. fuse_file_info_t fi = {0};
  1192. struct fuse_fsync_in *arg = (struct fuse_fsync_in*)inarg;
  1193. fi.fh = arg->fh;
  1194. req->f->op.fsyncdir(req,nodeid,arg->fsync_flags & 1,&fi);
  1195. }
  1196. static
  1197. void
  1198. do_statfs(fuse_req_t req,
  1199. uint64_t nodeid,
  1200. const void *inarg)
  1201. {
  1202. (void)nodeid;
  1203. (void)inarg;
  1204. req->f->op.statfs(req, nodeid);
  1205. }
  1206. static
  1207. void
  1208. do_setxattr(fuse_req_t req,
  1209. uint64_t nodeid,
  1210. const void *inarg)
  1211. {
  1212. struct fuse_setxattr_in *arg = (struct fuse_setxattr_in*)inarg;
  1213. char *name = PARAM(arg);
  1214. char *value = name + strlen(name) + 1;
  1215. req->f->op.setxattr(req, nodeid, name, value, arg->size, arg->flags);
  1216. }
  1217. static
  1218. void
  1219. do_getxattr(fuse_req_t req,
  1220. uint64_t nodeid,
  1221. const void *inarg)
  1222. {
  1223. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in*)inarg;
  1224. req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
  1225. }
  1226. static
  1227. void
  1228. do_listxattr(fuse_req_t req,
  1229. uint64_t nodeid,
  1230. const void *inarg)
  1231. {
  1232. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in*)inarg;
  1233. req->f->op.listxattr(req, nodeid, arg->size);
  1234. }
  1235. static
  1236. void
  1237. do_removexattr(fuse_req_t req,
  1238. uint64_t nodeid,
  1239. const void *inarg)
  1240. {
  1241. char *name = (char *) inarg;
  1242. req->f->op.removexattr(req, nodeid, name);
  1243. }
  1244. static
  1245. void
  1246. convert_fuse_file_lock(struct fuse_file_lock *fl,
  1247. struct flock *flock)
  1248. {
  1249. memset(flock, 0, sizeof(struct flock));
  1250. flock->l_type = fl->type;
  1251. flock->l_whence = SEEK_SET;
  1252. flock->l_start = fl->start;
  1253. if (fl->end == OFFSET_MAX)
  1254. flock->l_len = 0;
  1255. else
  1256. flock->l_len = fl->end - fl->start + 1;
  1257. flock->l_pid = fl->pid;
  1258. }
  1259. static
  1260. void
  1261. do_getlk(fuse_req_t req,
  1262. uint64_t nodeid,
  1263. const void *inarg)
  1264. {
  1265. fuse_file_info_t fi = {0};
  1266. struct flock flock;
  1267. struct fuse_lk_in *arg = (struct fuse_lk_in*)inarg;
  1268. fi.fh = arg->fh;
  1269. fi.lock_owner = arg->owner;
  1270. convert_fuse_file_lock(&arg->lk, &flock);
  1271. req->f->op.getlk(req, nodeid, &fi, &flock);
  1272. }
  1273. static
  1274. void
  1275. do_setlk_common(fuse_req_t req,
  1276. uint64_t nodeid,
  1277. const void *inarg,
  1278. int sleep)
  1279. {
  1280. struct flock flock;
  1281. fuse_file_info_t fi = {0};
  1282. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1283. fi.fh = arg->fh;
  1284. fi.lock_owner = arg->owner;
  1285. if (arg->lk_flags & FUSE_LK_FLOCK)
  1286. {
  1287. int op = 0;
  1288. switch (arg->lk.type)
  1289. {
  1290. case F_RDLCK:
  1291. op = LOCK_SH;
  1292. break;
  1293. case F_WRLCK:
  1294. op = LOCK_EX;
  1295. break;
  1296. case F_UNLCK:
  1297. op = LOCK_UN;
  1298. break;
  1299. }
  1300. if (!sleep)
  1301. op |= LOCK_NB;
  1302. req->f->op.flock(req,nodeid,&fi,op);
  1303. }
  1304. else
  1305. {
  1306. convert_fuse_file_lock(&arg->lk, &flock);
  1307. req->f->op.setlk(req,nodeid,&fi,&flock,sleep);
  1308. }
  1309. }
  1310. static
  1311. void
  1312. do_setlk(fuse_req_t req,
  1313. uint64_t nodeid,
  1314. const void *inarg)
  1315. {
  1316. do_setlk_common(req, nodeid, inarg, 0);
  1317. }
  1318. static
  1319. void
  1320. do_setlkw(fuse_req_t req,
  1321. uint64_t nodeid,
  1322. const void *inarg)
  1323. {
  1324. do_setlk_common(req, nodeid, inarg, 1);
  1325. }
  1326. static
  1327. void
  1328. do_interrupt(fuse_req_t req,
  1329. uint64_t nodeid,
  1330. const void *inarg)
  1331. {
  1332. destroy_req(req);
  1333. }
  1334. static
  1335. void
  1336. do_bmap(fuse_req_t req,
  1337. uint64_t nodeid,
  1338. const void *inarg)
  1339. {
  1340. struct fuse_bmap_in *arg = (struct fuse_bmap_in*)inarg;
  1341. req->f->op.bmap(req,nodeid,arg->blocksize,arg->block);
  1342. }
  1343. static
  1344. void
  1345. do_ioctl(fuse_req_t req,
  1346. uint64_t nodeid,
  1347. const void *inarg)
  1348. {
  1349. fuse_file_info_t fi = {0};
  1350. struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
  1351. unsigned int flags = arg->flags;
  1352. void *in_buf = arg->in_size ? PARAM(arg) : NULL;
  1353. if((flags & FUSE_IOCTL_DIR) && !(req->f->conn.want & FUSE_CAP_IOCTL_DIR))
  1354. {
  1355. fuse_reply_err(req,ENOTTY);
  1356. return;
  1357. }
  1358. fi.fh = arg->fh;
  1359. if((sizeof(void *) == 4) &&
  1360. (req->f->conn.proto_minor >= 16) &&
  1361. !(flags & FUSE_IOCTL_32BIT))
  1362. {
  1363. req->ioctl_64bit = 1;
  1364. }
  1365. req->f->op.ioctl(req, nodeid, (unsigned long)arg->cmd,
  1366. (void *)(uintptr_t)arg->arg, &fi, flags,
  1367. in_buf, arg->in_size, arg->out_size);
  1368. }
  1369. void
  1370. fuse_pollhandle_destroy(fuse_pollhandle_t *ph)
  1371. {
  1372. free(ph);
  1373. }
  1374. static
  1375. void
  1376. do_poll(fuse_req_t req,
  1377. uint64_t nodeid,
  1378. const void *inarg)
  1379. {
  1380. fuse_file_info_t fi = {0};
  1381. fuse_pollhandle_t *ph = NULL;
  1382. struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
  1383. fi.fh = arg->fh;
  1384. if(arg->flags & FUSE_POLL_SCHEDULE_NOTIFY)
  1385. {
  1386. ph = malloc(sizeof(fuse_pollhandle_t));
  1387. if (ph == NULL) {
  1388. fuse_reply_err(req, ENOMEM);
  1389. return;
  1390. }
  1391. ph->kh = arg->kh;
  1392. ph->ch = req->ch;
  1393. ph->f = req->f;
  1394. }
  1395. req->f->op.poll(req,nodeid,&fi,ph);
  1396. }
  1397. static
  1398. void
  1399. do_fallocate(fuse_req_t req,
  1400. uint64_t nodeid,
  1401. const void *inarg)
  1402. {
  1403. fuse_file_info_t fi = {0};
  1404. struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
  1405. fi.fh = arg->fh;
  1406. req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
  1407. }
  1408. static
  1409. void
  1410. do_init(fuse_req_t req,
  1411. uint64_t nodeid,
  1412. const void *inarg)
  1413. {
  1414. struct fuse_init_out outarg = {0};
  1415. struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
  1416. struct fuse_ll *f = req->f;
  1417. size_t bufsize = fuse_chan_bufsize(req->ch);
  1418. (void)nodeid;
  1419. if(f->debug)
  1420. debug_fuse_init_in(arg);
  1421. f->conn.proto_major = arg->major;
  1422. f->conn.proto_minor = arg->minor;
  1423. f->conn.capable = 0;
  1424. f->conn.want = 0;
  1425. outarg.major = FUSE_KERNEL_VERSION;
  1426. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  1427. outarg.max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  1428. if (arg->major < 7)
  1429. {
  1430. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  1431. arg->major, arg->minor);
  1432. fuse_reply_err(req, EPROTO);
  1433. return;
  1434. }
  1435. if (arg->major > 7)
  1436. {
  1437. /* Wait for a second INIT request with a 7.X version */
  1438. send_reply_ok(req, &outarg, sizeof(outarg));
  1439. return;
  1440. }
  1441. if (arg->minor >= 6)
  1442. {
  1443. if (arg->max_readahead < f->conn.max_readahead)
  1444. f->conn.max_readahead = arg->max_readahead;
  1445. if (arg->flags & FUSE_ASYNC_READ)
  1446. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  1447. if (arg->flags & FUSE_POSIX_LOCKS)
  1448. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  1449. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  1450. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  1451. if (arg->flags & FUSE_EXPORT_SUPPORT)
  1452. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  1453. if (arg->flags & FUSE_BIG_WRITES)
  1454. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  1455. if (arg->flags & FUSE_DONT_MASK)
  1456. f->conn.capable |= FUSE_CAP_DONT_MASK;
  1457. if (arg->flags & FUSE_FLOCK_LOCKS)
  1458. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  1459. if (arg->flags & FUSE_POSIX_ACL)
  1460. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  1461. if (arg->flags & FUSE_CACHE_SYMLINKS)
  1462. f->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
  1463. if (arg->flags & FUSE_ASYNC_DIO)
  1464. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  1465. if (arg->flags & FUSE_PARALLEL_DIROPS)
  1466. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  1467. if (arg->flags & FUSE_MAX_PAGES)
  1468. f->conn.capable |= FUSE_CAP_MAX_PAGES;
  1469. if (arg->flags & FUSE_WRITEBACK_CACHE)
  1470. f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
  1471. if (arg->flags & FUSE_DO_READDIRPLUS)
  1472. f->conn.capable |= FUSE_CAP_READDIR_PLUS;
  1473. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  1474. f->conn.capable |= FUSE_CAP_READDIR_PLUS_AUTO;
  1475. }
  1476. else
  1477. {
  1478. f->conn.want &= ~FUSE_CAP_ASYNC_READ;
  1479. f->conn.max_readahead = 0;
  1480. }
  1481. if (req->f->conn.proto_minor >= 14)
  1482. {
  1483. #ifdef HAVE_SPLICE
  1484. #ifdef HAVE_VMSPLICE
  1485. f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
  1486. if (f->splice_write)
  1487. f->conn.want |= FUSE_CAP_SPLICE_WRITE;
  1488. if (f->splice_move)
  1489. f->conn.want |= FUSE_CAP_SPLICE_MOVE;
  1490. #endif
  1491. f->conn.capable |= FUSE_CAP_SPLICE_READ;
  1492. if (f->splice_read)
  1493. f->conn.want |= FUSE_CAP_SPLICE_READ;
  1494. #endif
  1495. }
  1496. if (req->f->conn.proto_minor >= 18)
  1497. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1498. if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1499. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1500. if (f->op.flock && !f->no_remote_flock)
  1501. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1502. if (bufsize < FUSE_MIN_READ_BUFFER)
  1503. {
  1504. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1505. bufsize);
  1506. bufsize = FUSE_MIN_READ_BUFFER;
  1507. }
  1508. bufsize -= 4096;
  1509. if (bufsize < f->conn.max_write)
  1510. f->conn.max_write = bufsize;
  1511. f->got_init = 1;
  1512. if (f->op.init)
  1513. f->op.init(f->userdata, &f->conn);
  1514. if (f->no_splice_read)
  1515. f->conn.want &= ~FUSE_CAP_SPLICE_READ;
  1516. if (f->no_splice_write)
  1517. f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
  1518. if (f->no_splice_move)
  1519. f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
  1520. if ((arg->flags & FUSE_MAX_PAGES) && (f->conn.want & FUSE_CAP_MAX_PAGES))
  1521. {
  1522. outarg.flags |= FUSE_MAX_PAGES;
  1523. outarg.max_pages = f->conn.max_pages;
  1524. }
  1525. if (f->conn.want & FUSE_CAP_ASYNC_READ)
  1526. outarg.flags |= FUSE_ASYNC_READ;
  1527. if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1528. outarg.flags |= FUSE_POSIX_LOCKS;
  1529. if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1530. outarg.flags |= FUSE_ATOMIC_O_TRUNC;
  1531. if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1532. outarg.flags |= FUSE_EXPORT_SUPPORT;
  1533. if (f->conn.want & FUSE_CAP_BIG_WRITES)
  1534. outarg.flags |= FUSE_BIG_WRITES;
  1535. if (f->conn.want & FUSE_CAP_DONT_MASK)
  1536. outarg.flags |= FUSE_DONT_MASK;
  1537. if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1538. outarg.flags |= FUSE_FLOCK_LOCKS;
  1539. if (f->conn.want & FUSE_CAP_POSIX_ACL)
  1540. outarg.flags |= FUSE_POSIX_ACL;
  1541. if (f->conn.want & FUSE_CAP_CACHE_SYMLINKS)
  1542. outarg.flags |= FUSE_CACHE_SYMLINKS;
  1543. if (f->conn.want & FUSE_CAP_ASYNC_DIO)
  1544. outarg.flags |= FUSE_ASYNC_DIO;
  1545. if (f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1546. outarg.flags |= FUSE_PARALLEL_DIROPS;
  1547. if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
  1548. outarg.flags |= FUSE_WRITEBACK_CACHE;
  1549. if (f->conn.want & FUSE_CAP_READDIR_PLUS)
  1550. outarg.flags |= FUSE_DO_READDIRPLUS;
  1551. if (f->conn.want & FUSE_CAP_READDIR_PLUS_AUTO)
  1552. outarg.flags |= FUSE_READDIRPLUS_AUTO;
  1553. outarg.max_readahead = f->conn.max_readahead;
  1554. outarg.max_write = f->conn.max_write;
  1555. if (f->conn.proto_minor >= 13)
  1556. {
  1557. if (f->conn.max_background >= (1 << 16))
  1558. f->conn.max_background = (1 << 16) - 1;
  1559. if (f->conn.congestion_threshold > f->conn.max_background)
  1560. f->conn.congestion_threshold = f->conn.max_background;
  1561. if (!f->conn.congestion_threshold)
  1562. {
  1563. f->conn.congestion_threshold = f->conn.max_background * 3 / 4;
  1564. }
  1565. outarg.max_background = f->conn.max_background;
  1566. outarg.congestion_threshold = f->conn.congestion_threshold;
  1567. }
  1568. size_t outargsize;
  1569. if(arg->minor < 5)
  1570. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1571. else if(arg->minor < 23)
  1572. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1573. else
  1574. outargsize = sizeof(outarg);
  1575. if(f->debug)
  1576. debug_fuse_init_out(req->unique,&outarg,outargsize);
  1577. send_reply_ok(req, &outarg, outargsize);
  1578. }
  1579. static
  1580. void
  1581. do_destroy(fuse_req_t req,
  1582. uint64_t nodeid,
  1583. const void *inarg)
  1584. {
  1585. struct fuse_ll *f = req->f;
  1586. (void) nodeid;
  1587. (void) inarg;
  1588. f->got_destroy = 1;
  1589. f->op.destroy(f->userdata);
  1590. send_reply_ok(req,NULL,0);
  1591. }
  1592. static
  1593. void
  1594. list_del_nreq(struct fuse_notify_req *nreq)
  1595. {
  1596. struct fuse_notify_req *prev = nreq->prev;
  1597. struct fuse_notify_req *next = nreq->next;
  1598. prev->next = next;
  1599. next->prev = prev;
  1600. }
  1601. static
  1602. void
  1603. list_add_nreq(struct fuse_notify_req *nreq,
  1604. struct fuse_notify_req *next)
  1605. {
  1606. struct fuse_notify_req *prev = next->prev;
  1607. nreq->next = next;
  1608. nreq->prev = prev;
  1609. prev->next = nreq;
  1610. next->prev = nreq;
  1611. }
  1612. static
  1613. void
  1614. list_init_nreq(struct fuse_notify_req *nreq)
  1615. {
  1616. nreq->next = nreq;
  1617. nreq->prev = nreq;
  1618. }
  1619. static
  1620. void
  1621. do_notify_reply(fuse_req_t req,
  1622. uint64_t nodeid,
  1623. const void *inarg)
  1624. {
  1625. struct fuse_ll *f = req->f;
  1626. struct fuse_notify_req *nreq;
  1627. struct fuse_notify_req *head;
  1628. pthread_mutex_lock(&f->lock);
  1629. head = &f->notify_list;
  1630. for(nreq = head->next; nreq != head; nreq = nreq->next)
  1631. {
  1632. if(nreq->unique == req->unique)
  1633. {
  1634. list_del_nreq(nreq);
  1635. break;
  1636. }
  1637. }
  1638. pthread_mutex_unlock(&f->lock);
  1639. if(nreq != head)
  1640. nreq->reply(nreq, req, nodeid, inarg);
  1641. }
  1642. static
  1643. void
  1644. do_copy_file_range(fuse_req_t req_,
  1645. uint64_t nodeid_in_,
  1646. const void *arg_)
  1647. {
  1648. fuse_file_info_t ffi_in = {0};
  1649. fuse_file_info_t ffi_out = {0};
  1650. struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in*)arg_;
  1651. ffi_in.fh = arg->fh_in;
  1652. ffi_out.fh = arg->fh_out;
  1653. req_->f->op.copy_file_range(req_,
  1654. nodeid_in_,
  1655. arg->off_in,
  1656. &ffi_in,
  1657. arg->nodeid_out,
  1658. arg->off_out,
  1659. &ffi_out,
  1660. arg->len,
  1661. arg->flags);
  1662. }
  1663. static
  1664. int
  1665. send_notify_iov(struct fuse_ll *f,
  1666. struct fuse_chan *ch,
  1667. int notify_code,
  1668. struct iovec *iov,
  1669. int count)
  1670. {
  1671. struct fuse_out_header out;
  1672. if(!f->got_init)
  1673. return -ENOTCONN;
  1674. out.unique = 0;
  1675. out.error = notify_code;
  1676. iov[0].iov_base = &out;
  1677. iov[0].iov_len = sizeof(struct fuse_out_header);
  1678. return fuse_send_msg(f, ch, iov, count);
  1679. }
  1680. int
  1681. fuse_lowlevel_notify_poll(fuse_pollhandle_t *ph)
  1682. {
  1683. if(ph != NULL)
  1684. {
  1685. struct fuse_notify_poll_wakeup_out outarg;
  1686. struct iovec iov[2];
  1687. outarg.kh = ph->kh;
  1688. iov[1].iov_base = &outarg;
  1689. iov[1].iov_len = sizeof(outarg);
  1690. return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
  1691. }
  1692. else
  1693. {
  1694. return 0;
  1695. }
  1696. }
  1697. int
  1698. fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch,
  1699. uint64_t ino,
  1700. off_t off,
  1701. off_t len)
  1702. {
  1703. struct fuse_notify_inval_inode_out outarg;
  1704. struct fuse_ll *f;
  1705. struct iovec iov[2];
  1706. if (!ch)
  1707. return -EINVAL;
  1708. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1709. if (!f)
  1710. return -ENODEV;
  1711. outarg.ino = ino;
  1712. outarg.off = off;
  1713. outarg.len = len;
  1714. iov[1].iov_base = &outarg;
  1715. iov[1].iov_len = sizeof(outarg);
  1716. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
  1717. }
  1718. int
  1719. fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch,
  1720. uint64_t parent,
  1721. const char *name,
  1722. size_t namelen)
  1723. {
  1724. struct fuse_notify_inval_entry_out outarg;
  1725. struct fuse_ll *f;
  1726. struct iovec iov[3];
  1727. if (!ch)
  1728. return -EINVAL;
  1729. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1730. if (!f)
  1731. return -ENODEV;
  1732. outarg.parent = parent;
  1733. outarg.namelen = namelen;
  1734. outarg.padding = 0;
  1735. iov[1].iov_base = &outarg;
  1736. iov[1].iov_len = sizeof(outarg);
  1737. iov[2].iov_base = (void *)name;
  1738. iov[2].iov_len = namelen + 1;
  1739. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
  1740. }
  1741. int
  1742. fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  1743. uint64_t parent,
  1744. uint64_t child,
  1745. const char *name,
  1746. size_t namelen)
  1747. {
  1748. struct fuse_notify_delete_out outarg;
  1749. struct fuse_ll *f;
  1750. struct iovec iov[3];
  1751. if (!ch)
  1752. return -EINVAL;
  1753. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1754. if (!f)
  1755. return -ENODEV;
  1756. if (f->conn.proto_minor < 18)
  1757. return -ENOSYS;
  1758. outarg.parent = parent;
  1759. outarg.child = child;
  1760. outarg.namelen = namelen;
  1761. outarg.padding = 0;
  1762. iov[1].iov_base = &outarg;
  1763. iov[1].iov_len = sizeof(outarg);
  1764. iov[2].iov_base = (void *)name;
  1765. iov[2].iov_len = namelen + 1;
  1766. return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
  1767. }
  1768. int
  1769. fuse_lowlevel_notify_store(struct fuse_chan *ch,
  1770. uint64_t ino,
  1771. off_t offset,
  1772. struct fuse_bufvec *bufv,
  1773. enum fuse_buf_copy_flags flags)
  1774. {
  1775. struct fuse_out_header out;
  1776. struct fuse_notify_store_out outarg;
  1777. struct fuse_ll *f;
  1778. struct iovec iov[3];
  1779. size_t size = fuse_buf_size(bufv);
  1780. int res;
  1781. if (!ch)
  1782. return -EINVAL;
  1783. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1784. if (!f)
  1785. return -ENODEV;
  1786. if (f->conn.proto_minor < 15)
  1787. return -ENOSYS;
  1788. out.unique = 0;
  1789. out.error = FUSE_NOTIFY_STORE;
  1790. outarg.nodeid = ino;
  1791. outarg.offset = offset;
  1792. outarg.size = size;
  1793. outarg.padding = 0;
  1794. iov[0].iov_base = &out;
  1795. iov[0].iov_len = sizeof(out);
  1796. iov[1].iov_base = &outarg;
  1797. iov[1].iov_len = sizeof(outarg);
  1798. res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
  1799. if (res > 0)
  1800. res = -res;
  1801. return res;
  1802. }
  1803. struct fuse_retrieve_req
  1804. {
  1805. struct fuse_notify_req nreq;
  1806. void *cookie;
  1807. };
  1808. static
  1809. void
  1810. fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
  1811. fuse_req_t req,
  1812. uint64_t ino,
  1813. const void *inarg)
  1814. {
  1815. struct fuse_retrieve_req *rreq =
  1816. container_of(nreq, struct fuse_retrieve_req, nreq);
  1817. fuse_reply_none(req);
  1818. free(rreq);
  1819. }
  1820. int
  1821. fuse_lowlevel_notify_retrieve(struct fuse_chan *ch,
  1822. uint64_t ino,
  1823. size_t size,
  1824. off_t offset,
  1825. void *cookie)
  1826. {
  1827. struct fuse_notify_retrieve_out outarg;
  1828. struct fuse_ll *f;
  1829. struct iovec iov[2];
  1830. struct fuse_retrieve_req *rreq;
  1831. int err;
  1832. if (!ch)
  1833. return -EINVAL;
  1834. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1835. if(!f)
  1836. return -ENODEV;
  1837. if(f->conn.proto_minor < 15)
  1838. return -ENOSYS;
  1839. rreq = malloc(sizeof(*rreq));
  1840. if(rreq == NULL)
  1841. return -ENOMEM;
  1842. pthread_mutex_lock(&f->lock);
  1843. rreq->cookie = cookie;
  1844. rreq->nreq.unique = f->notify_ctr++;
  1845. rreq->nreq.reply = fuse_ll_retrieve_reply;
  1846. list_add_nreq(&rreq->nreq, &f->notify_list);
  1847. pthread_mutex_unlock(&f->lock);
  1848. outarg.notify_unique = rreq->nreq.unique;
  1849. outarg.nodeid = ino;
  1850. outarg.offset = offset;
  1851. outarg.size = size;
  1852. iov[1].iov_base = &outarg;
  1853. iov[1].iov_len = sizeof(outarg);
  1854. err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
  1855. if(err)
  1856. {
  1857. pthread_mutex_lock(&f->lock);
  1858. list_del_nreq(&rreq->nreq);
  1859. pthread_mutex_unlock(&f->lock);
  1860. free(rreq);
  1861. }
  1862. return err;
  1863. }
  1864. void *
  1865. fuse_req_userdata(fuse_req_t req)
  1866. {
  1867. return req->f->userdata;
  1868. }
  1869. const
  1870. struct fuse_ctx *
  1871. fuse_req_ctx(fuse_req_t req)
  1872. {
  1873. return &req->ctx;
  1874. }
  1875. static struct {
  1876. void (*func)(fuse_req_t, uint64_t, const void *);
  1877. const char *name;
  1878. } fuse_ll_ops[] =
  1879. {
  1880. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1881. [FUSE_FORGET] = { do_forget, "FORGET" },
  1882. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1883. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1884. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1885. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1886. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1887. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1888. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1889. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1890. [FUSE_RENAME] = { do_rename, "RENAME" },
  1891. [FUSE_LINK] = { do_link, "LINK" },
  1892. [FUSE_OPEN] = { do_open, "OPEN" },
  1893. [FUSE_READ] = { do_read, "READ" },
  1894. [FUSE_WRITE] = { do_write, "WRITE" },
  1895. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1896. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1897. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1898. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1899. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1900. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1901. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1902. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1903. [FUSE_INIT] = { do_init, "INIT" },
  1904. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1905. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1906. [FUSE_READDIRPLUS] = { do_readdir_plus, "READDIR_PLUS" },
  1907. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1908. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1909. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1910. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1911. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1912. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1913. [FUSE_CREATE] = { do_create, "CREATE" },
  1914. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  1915. [FUSE_BMAP] = { do_bmap, "BMAP" },
  1916. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  1917. [FUSE_POLL] = { do_poll, "POLL" },
  1918. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  1919. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  1920. [FUSE_NOTIFY_REPLY] = { do_notify_reply, "NOTIFY_REPLY" },
  1921. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  1922. [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
  1923. };
  1924. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  1925. static
  1926. void
  1927. fuse_ll_process_buf(void *data,
  1928. const struct fuse_buf *buf,
  1929. struct fuse_chan *ch)
  1930. {
  1931. struct fuse_ll *f = (struct fuse_ll*)data;
  1932. struct fuse_in_header *in;
  1933. const void *inarg;
  1934. struct fuse_req *req;
  1935. int err;
  1936. in = buf->mem;
  1937. req = fuse_ll_alloc_req(f);
  1938. if(req == NULL)
  1939. {
  1940. struct fuse_out_header out = {
  1941. .unique = in->unique,
  1942. .error = -ENOMEM,
  1943. };
  1944. struct iovec iov = {
  1945. .iov_base = &out,
  1946. .iov_len = sizeof(struct fuse_out_header),
  1947. };
  1948. fuse_send_msg(f, ch, &iov, 1);
  1949. return;
  1950. }
  1951. req->unique = in->unique;
  1952. req->ctx.uid = in->uid;
  1953. req->ctx.gid = in->gid;
  1954. req->ctx.pid = in->pid;
  1955. req->ch = ch;
  1956. err = EIO;
  1957. if(!f->got_init)
  1958. {
  1959. enum fuse_opcode expected;
  1960. expected = FUSE_INIT;
  1961. if (in->opcode != expected)
  1962. goto reply_err;
  1963. }
  1964. else if(in->opcode == FUSE_INIT)
  1965. {
  1966. goto reply_err;
  1967. }
  1968. err = ENOSYS;
  1969. if(in->opcode >= FUSE_MAXOP)
  1970. goto reply_err;
  1971. inarg = (void*)&in[1];
  1972. fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
  1973. return;
  1974. reply_err:
  1975. fuse_reply_err(req, err);
  1976. return;
  1977. }
  1978. enum {
  1979. KEY_HELP,
  1980. KEY_VERSION,
  1981. };
  1982. static const struct fuse_opt fuse_ll_opts[] =
  1983. {
  1984. { "debug", offsetof(struct fuse_ll, debug), 1 },
  1985. { "-d", offsetof(struct fuse_ll, debug), 1 },
  1986. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  1987. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  1988. { "congestion_threshold=%u",
  1989. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  1990. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1991. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1992. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1993. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1994. { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
  1995. { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
  1996. { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
  1997. { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
  1998. { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
  1999. { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
  2000. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  2001. FUSE_OPT_KEY("-h", KEY_HELP),
  2002. FUSE_OPT_KEY("--help", KEY_HELP),
  2003. FUSE_OPT_KEY("-V", KEY_VERSION),
  2004. FUSE_OPT_KEY("--version", KEY_VERSION),
  2005. FUSE_OPT_END
  2006. };
  2007. static
  2008. void
  2009. fuse_ll_version(void)
  2010. {
  2011. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  2012. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  2013. }
  2014. static
  2015. void
  2016. fuse_ll_help(void)
  2017. {
  2018. fprintf(stderr,
  2019. " -o max_readahead=N set maximum readahead\n"
  2020. " -o max_background=N set number of maximum background requests\n"
  2021. " -o congestion_threshold=N set kernel's congestion threshold\n"
  2022. " -o no_remote_lock disable remote file locking\n"
  2023. " -o no_remote_flock disable remote file locking (BSD)\n"
  2024. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  2025. " -o [no_]splice_write use splice to write to the fuse device\n"
  2026. " -o [no_]splice_move move data while splicing to the fuse device\n"
  2027. " -o [no_]splice_read use splice to read from the fuse device\n"
  2028. );
  2029. }
  2030. static
  2031. int
  2032. fuse_ll_opt_proc(void *data,
  2033. const char *arg,
  2034. int key,
  2035. struct fuse_args *outargs)
  2036. {
  2037. (void) data; (void) outargs;
  2038. switch (key)
  2039. {
  2040. case KEY_HELP:
  2041. fuse_ll_help();
  2042. break;
  2043. case KEY_VERSION:
  2044. fuse_ll_version();
  2045. break;
  2046. default:
  2047. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  2048. }
  2049. return -1;
  2050. }
  2051. int
  2052. fuse_lowlevel_is_lib_option(const char *opt)
  2053. {
  2054. return fuse_opt_match(fuse_ll_opts, opt);
  2055. }
  2056. static
  2057. void
  2058. fuse_ll_destroy(void *data)
  2059. {
  2060. struct fuse_ll *f = (struct fuse_ll *) data;
  2061. struct fuse_ll_pipe *llp;
  2062. if (f->got_init && !f->got_destroy)
  2063. {
  2064. if (f->op.destroy)
  2065. f->op.destroy(f->userdata);
  2066. }
  2067. llp = pthread_getspecific(f->pipe_key);
  2068. if (llp != NULL)
  2069. fuse_ll_pipe_free(llp);
  2070. pthread_key_delete(f->pipe_key);
  2071. pthread_mutex_destroy(&f->lock);
  2072. free(f);
  2073. lfmp_clear(&g_FMP_fuse_req);
  2074. }
  2075. static
  2076. void
  2077. fuse_ll_pipe_destructor(void *data)
  2078. {
  2079. struct fuse_ll_pipe *llp = data;
  2080. fuse_ll_pipe_free(llp);
  2081. }
  2082. #ifdef HAVE_SPLICE
  2083. static
  2084. int
  2085. fuse_ll_receive_buf(struct fuse_session *se,
  2086. struct fuse_buf *buf)
  2087. {
  2088. struct fuse_ll *f = fuse_session_data(se);
  2089. size_t bufsize = buf->size;
  2090. struct fuse_ll_pipe *llp;
  2091. int err;
  2092. int res;
  2093. if(f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
  2094. goto fallback;
  2095. llp = fuse_ll_get_pipe(f);
  2096. if (llp == NULL)
  2097. goto fallback;
  2098. if(llp->size < bufsize)
  2099. {
  2100. if(llp->can_grow)
  2101. {
  2102. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
  2103. if(res == -1)
  2104. {
  2105. llp->can_grow = 0;
  2106. goto fallback;
  2107. }
  2108. llp->size = res;
  2109. }
  2110. if(llp->size < bufsize)
  2111. goto fallback;
  2112. }
  2113. res = splice(fuse_chan_fd(se->ch), NULL, llp->pipe[1], NULL, bufsize, SPLICE_F_MOVE);
  2114. err = errno;
  2115. if(fuse_session_exited(se))
  2116. return 0;
  2117. if(res == -1)
  2118. {
  2119. if(err == ENODEV)
  2120. {
  2121. fuse_session_exit(se);
  2122. return 0;
  2123. }
  2124. if(err != EINTR && err != EAGAIN)
  2125. perror("fuse: splice from device");
  2126. return -err;
  2127. }
  2128. if(res < sizeof(struct fuse_in_header))
  2129. {
  2130. fprintf(stderr, "short splice from fuse device\n");
  2131. return -EIO;
  2132. }
  2133. struct iovec iov = { buf->mem, res };
  2134. res = vmsplice(llp->pipe[0], &iov, 1, 0);
  2135. return res;
  2136. fallback:
  2137. res = fuse_chan_recv(se->ch, buf->mem, buf->size);
  2138. if(res <= 0)
  2139. return res;
  2140. buf->size = res;
  2141. return res;
  2142. }
  2143. #else
  2144. static
  2145. int
  2146. fuse_ll_receive_buf(struct fuse_session *se,
  2147. struct fuse_buf *buf)
  2148. {
  2149. int res;
  2150. res = fuse_chan_recv(se->ch, buf->mem, buf->size);
  2151. if(res <= 0)
  2152. return res;
  2153. buf->size = res;
  2154. return res;
  2155. }
  2156. #endif
  2157. /*
  2158. * always call fuse_lowlevel_new_common() internally, to work around a
  2159. * misfeature in the FreeBSD runtime linker, which links the old
  2160. * version of a symbol to internal references.
  2161. */
  2162. struct fuse_session *
  2163. fuse_lowlevel_new_common(struct fuse_args *args,
  2164. const struct fuse_lowlevel_ops *op,
  2165. size_t op_size,
  2166. void *userdata)
  2167. {
  2168. int err;
  2169. struct fuse_ll *f;
  2170. struct fuse_session *se;
  2171. if (sizeof(struct fuse_lowlevel_ops) < op_size)
  2172. {
  2173. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  2174. op_size = sizeof(struct fuse_lowlevel_ops);
  2175. }
  2176. f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
  2177. if (f == NULL)
  2178. {
  2179. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  2180. goto out;
  2181. }
  2182. f->conn.max_write = UINT_MAX;
  2183. f->conn.max_readahead = UINT_MAX;
  2184. list_init_nreq(&f->notify_list);
  2185. f->notify_ctr = 1;
  2186. fuse_mutex_init(&f->lock);
  2187. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  2188. if (err)
  2189. {
  2190. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  2191. strerror(err));
  2192. goto out_free;
  2193. }
  2194. if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  2195. goto out_key_destroy;
  2196. memcpy(&f->op, op, op_size);
  2197. f->owner = getuid();
  2198. f->userdata = userdata;
  2199. se = fuse_session_new(f,
  2200. fuse_ll_receive_buf,
  2201. fuse_ll_process_buf,
  2202. fuse_ll_destroy);
  2203. if (!se)
  2204. goto out_key_destroy;
  2205. return se;
  2206. out_key_destroy:
  2207. pthread_key_delete(f->pipe_key);
  2208. out_free:
  2209. pthread_mutex_destroy(&f->lock);
  2210. free(f);
  2211. out:
  2212. return NULL;
  2213. }
  2214. struct fuse_session*
  2215. fuse_lowlevel_new(struct fuse_args *args,
  2216. const struct fuse_lowlevel_ops *op,
  2217. size_t op_size,
  2218. void *userdata)
  2219. {
  2220. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  2221. }