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.

2059 lines
47 KiB

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