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.

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