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.

2056 lines
46 KiB

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