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.

2019 lines
45 KiB

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