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.

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