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.

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