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.

2000 lines
45 KiB

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