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.

2300 lines
52 KiB

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