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.

2055 lines
46 KiB

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