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.

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