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.

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