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.

2035 lines
46 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. uint64_t inargflags;
  933. uint64_t outargflags;
  934. inargflags = 0;
  935. outargflags = 0;
  936. if(f->debug)
  937. debug_fuse_init_in(arg);
  938. f->conn.proto_major = arg->major;
  939. f->conn.proto_minor = arg->minor;
  940. f->conn.capable = 0;
  941. f->conn.want = 0;
  942. outarg.major = FUSE_KERNEL_VERSION;
  943. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  944. outarg.max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  945. if(arg->major < 7)
  946. {
  947. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  948. arg->major, arg->minor);
  949. fuse_reply_err(req, EPROTO);
  950. return;
  951. }
  952. if(arg->major > 7)
  953. {
  954. /* Wait for a second INIT request with a 7.X version */
  955. send_reply_ok(req, &outarg, sizeof(outarg));
  956. return;
  957. }
  958. if(arg->minor >= 6)
  959. {
  960. inargflags = arg->flags;
  961. if(inargflags & FUSE_INIT_EXT)
  962. inargflags |= (((uint64_t)arg->flags2) << 32);
  963. if(arg->max_readahead < f->conn.max_readahead)
  964. f->conn.max_readahead = arg->max_readahead;
  965. if(inargflags & FUSE_ASYNC_READ)
  966. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  967. if(inargflags & FUSE_POSIX_LOCKS)
  968. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  969. if(inargflags & FUSE_ATOMIC_O_TRUNC)
  970. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  971. if(inargflags & FUSE_EXPORT_SUPPORT)
  972. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  973. if(inargflags & FUSE_BIG_WRITES)
  974. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  975. if(inargflags & FUSE_DONT_MASK)
  976. f->conn.capable |= FUSE_CAP_DONT_MASK;
  977. if(inargflags & FUSE_FLOCK_LOCKS)
  978. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  979. if(inargflags & FUSE_POSIX_ACL)
  980. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  981. if(inargflags & FUSE_CACHE_SYMLINKS)
  982. f->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
  983. if(inargflags & FUSE_ASYNC_DIO)
  984. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  985. if(inargflags & FUSE_PARALLEL_DIROPS)
  986. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  987. if(inargflags & FUSE_MAX_PAGES)
  988. f->conn.capable |= FUSE_CAP_MAX_PAGES;
  989. if(inargflags & FUSE_WRITEBACK_CACHE)
  990. f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
  991. if(inargflags & FUSE_DO_READDIRPLUS)
  992. f->conn.capable |= FUSE_CAP_READDIR_PLUS;
  993. if(inargflags & FUSE_READDIRPLUS_AUTO)
  994. f->conn.capable |= FUSE_CAP_READDIR_PLUS_AUTO;
  995. if(inargflags & FUSE_SETXATTR_EXT)
  996. f->conn.capable |= FUSE_CAP_SETXATTR_EXT;
  997. if(inargflags & FUSE_DIRECT_IO_ALLOW_MMAP)
  998. f->conn.capable |= FUSE_CAP_DIRECT_IO_ALLOW_MMAP;
  999. if(inargflags & FUSE_CREATE_SUPP_GROUP)
  1000. f->conn.capable |= FUSE_CAP_CREATE_SUPP_GROUP;
  1001. }
  1002. else
  1003. {
  1004. f->conn.want &= ~FUSE_CAP_ASYNC_READ;
  1005. f->conn.max_readahead = 0;
  1006. }
  1007. if(req->f->conn.proto_minor >= 18)
  1008. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1009. if(f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1010. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1011. if(f->op.flock && !f->no_remote_flock)
  1012. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1013. if(bufsize < FUSE_MIN_READ_BUFFER)
  1014. {
  1015. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1016. bufsize);
  1017. bufsize = FUSE_MIN_READ_BUFFER;
  1018. }
  1019. bufsize -= pagesize;
  1020. if(bufsize < f->conn.max_write)
  1021. f->conn.max_write = bufsize;
  1022. f->got_init = 1;
  1023. if(f->op.init)
  1024. f->op.init(f->userdata, &f->conn);
  1025. outargflags = outarg.flags;
  1026. if((inargflags & FUSE_MAX_PAGES) && (f->conn.want & FUSE_CAP_MAX_PAGES))
  1027. {
  1028. outargflags |= FUSE_MAX_PAGES;
  1029. outarg.max_pages = f->conn.max_pages;
  1030. msgbuf_set_bufsize(outarg.max_pages + 1);
  1031. }
  1032. if(f->conn.want & FUSE_CAP_ASYNC_READ)
  1033. outargflags |= FUSE_ASYNC_READ;
  1034. if(f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1035. outargflags |= FUSE_POSIX_LOCKS;
  1036. if(f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1037. outargflags |= FUSE_ATOMIC_O_TRUNC;
  1038. if(f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1039. outargflags |= FUSE_EXPORT_SUPPORT;
  1040. if(f->conn.want & FUSE_CAP_BIG_WRITES)
  1041. outargflags |= FUSE_BIG_WRITES;
  1042. if(f->conn.want & FUSE_CAP_DONT_MASK)
  1043. outargflags |= FUSE_DONT_MASK;
  1044. if(f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1045. outargflags |= FUSE_FLOCK_LOCKS;
  1046. if(f->conn.want & FUSE_CAP_POSIX_ACL)
  1047. outargflags |= FUSE_POSIX_ACL;
  1048. if(f->conn.want & FUSE_CAP_CACHE_SYMLINKS)
  1049. outargflags |= FUSE_CACHE_SYMLINKS;
  1050. if(f->conn.want & FUSE_CAP_ASYNC_DIO)
  1051. outargflags |= FUSE_ASYNC_DIO;
  1052. if(f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1053. outargflags |= FUSE_PARALLEL_DIROPS;
  1054. if(f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
  1055. outargflags |= FUSE_WRITEBACK_CACHE;
  1056. if(f->conn.want & FUSE_CAP_READDIR_PLUS)
  1057. outargflags |= FUSE_DO_READDIRPLUS;
  1058. if(f->conn.want & FUSE_CAP_READDIR_PLUS_AUTO)
  1059. outargflags |= FUSE_READDIRPLUS_AUTO;
  1060. if(f->conn.want & FUSE_CAP_SETXATTR_EXT)
  1061. outargflags |= FUSE_SETXATTR_EXT;
  1062. if(f->conn.want & FUSE_CAP_CREATE_SUPP_GROUP)
  1063. outargflags |= FUSE_CREATE_SUPP_GROUP;
  1064. if(f->conn.want & FUSE_CAP_DIRECT_IO_ALLOW_MMAP)
  1065. outargflags |= FUSE_DIRECT_IO_ALLOW_MMAP;
  1066. if(inargflags & FUSE_INIT_EXT)
  1067. {
  1068. outargflags |= FUSE_INIT_EXT;
  1069. outarg.flags2 = (outargflags >> 32);
  1070. }
  1071. outarg.flags = outargflags;
  1072. outarg.max_readahead = f->conn.max_readahead;
  1073. outarg.max_write = f->conn.max_write;
  1074. if(f->conn.proto_minor >= 13)
  1075. {
  1076. if(f->conn.max_background >= (1 << 16))
  1077. f->conn.max_background = (1 << 16) - 1;
  1078. if(f->conn.congestion_threshold > f->conn.max_background)
  1079. f->conn.congestion_threshold = f->conn.max_background;
  1080. if(!f->conn.congestion_threshold)
  1081. {
  1082. f->conn.congestion_threshold = f->conn.max_background * 3 / 4;
  1083. }
  1084. outarg.max_background = f->conn.max_background;
  1085. outarg.congestion_threshold = f->conn.congestion_threshold;
  1086. }
  1087. if(f->conn.proto_minor >= 23)
  1088. outarg.time_gran = 1;
  1089. size_t outargsize;
  1090. if(arg->minor < 5)
  1091. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1092. else if(arg->minor < 23)
  1093. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1094. else
  1095. outargsize = sizeof(outarg);
  1096. if(f->debug)
  1097. debug_fuse_init_out(req->unique,&outarg,outargsize);
  1098. send_reply_ok(req, &outarg, outargsize);
  1099. }
  1100. static
  1101. void
  1102. do_destroy(fuse_req_t req,
  1103. struct fuse_in_header *hdr_)
  1104. {
  1105. struct fuse_ll *f = req->f;
  1106. f->got_destroy = 1;
  1107. f->op.destroy(f->userdata);
  1108. send_reply_ok(req,NULL,0);
  1109. }
  1110. static
  1111. void
  1112. list_del_nreq(struct fuse_notify_req *nreq)
  1113. {
  1114. struct fuse_notify_req *prev = nreq->prev;
  1115. struct fuse_notify_req *next = nreq->next;
  1116. prev->next = next;
  1117. next->prev = prev;
  1118. }
  1119. static
  1120. void
  1121. list_add_nreq(struct fuse_notify_req *nreq,
  1122. struct fuse_notify_req *next)
  1123. {
  1124. struct fuse_notify_req *prev = next->prev;
  1125. nreq->next = next;
  1126. nreq->prev = prev;
  1127. prev->next = nreq;
  1128. next->prev = nreq;
  1129. }
  1130. static
  1131. void
  1132. list_init_nreq(struct fuse_notify_req *nreq)
  1133. {
  1134. nreq->next = nreq;
  1135. nreq->prev = nreq;
  1136. }
  1137. static
  1138. void
  1139. do_notify_reply(fuse_req_t req,
  1140. struct fuse_in_header *hdr_)
  1141. {
  1142. struct fuse_ll *f = req->f;
  1143. struct fuse_notify_req *nreq;
  1144. struct fuse_notify_req *head;
  1145. pthread_mutex_lock(&f->lock);
  1146. head = &f->notify_list;
  1147. for(nreq = head->next; nreq != head; nreq = nreq->next)
  1148. {
  1149. if(nreq->unique == req->unique)
  1150. {
  1151. list_del_nreq(nreq);
  1152. break;
  1153. }
  1154. }
  1155. pthread_mutex_unlock(&f->lock);
  1156. if(nreq != head)
  1157. nreq->reply(nreq, req, hdr_->nodeid, &hdr_[1]);
  1158. }
  1159. static
  1160. void
  1161. do_copy_file_range(fuse_req_t req_,
  1162. struct fuse_in_header *hdr_)
  1163. {
  1164. req_->f->op.copy_file_range(req_,hdr_);
  1165. }
  1166. static
  1167. void
  1168. do_setupmapping(fuse_req_t req_,
  1169. struct fuse_in_header *hdr_)
  1170. {
  1171. printf("setupmapping\n");
  1172. req_->f->op.setupmapping(req_,hdr_);
  1173. }
  1174. static
  1175. void
  1176. do_removemapping(fuse_req_t req_,
  1177. struct fuse_in_header *hdr_)
  1178. {
  1179. printf("removemapping\n");
  1180. req_->f->op.removemapping(req_,hdr_);
  1181. }
  1182. static
  1183. void
  1184. do_syncfs(fuse_req_t req_,
  1185. struct fuse_in_header *hdr_)
  1186. {
  1187. printf("syncfs\n");
  1188. req_->f->op.syncfs(req_,hdr_);
  1189. }
  1190. static
  1191. void
  1192. do_tmpfile(fuse_req_t req_,
  1193. struct fuse_in_header *hdr_)
  1194. {
  1195. printf("tmpfile\n");
  1196. req_->f->op.tmpfile(req_,hdr_);
  1197. }
  1198. static
  1199. int
  1200. send_notify_iov(struct fuse_ll *f,
  1201. struct fuse_chan *ch,
  1202. int notify_code,
  1203. struct iovec *iov,
  1204. int count)
  1205. {
  1206. struct fuse_out_header out;
  1207. if(!f->got_init)
  1208. return -ENOTCONN;
  1209. out.unique = 0;
  1210. out.error = notify_code;
  1211. iov[0].iov_base = &out;
  1212. iov[0].iov_len = sizeof(struct fuse_out_header);
  1213. return fuse_send_msg(f, ch, iov, count);
  1214. }
  1215. int
  1216. fuse_lowlevel_notify_poll(fuse_pollhandle_t *ph)
  1217. {
  1218. if(ph != NULL)
  1219. {
  1220. struct fuse_notify_poll_wakeup_out outarg;
  1221. struct iovec iov[2];
  1222. outarg.kh = ph->kh;
  1223. iov[1].iov_base = &outarg;
  1224. iov[1].iov_len = sizeof(outarg);
  1225. return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
  1226. }
  1227. else
  1228. {
  1229. return 0;
  1230. }
  1231. }
  1232. int
  1233. fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch,
  1234. uint64_t ino,
  1235. off_t off,
  1236. off_t len)
  1237. {
  1238. struct fuse_notify_inval_inode_out outarg;
  1239. struct fuse_ll *f;
  1240. struct iovec iov[2];
  1241. if(!ch)
  1242. return -EINVAL;
  1243. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1244. if(!f)
  1245. return -ENODEV;
  1246. outarg.ino = ino;
  1247. outarg.off = off;
  1248. outarg.len = len;
  1249. iov[1].iov_base = &outarg;
  1250. iov[1].iov_len = sizeof(outarg);
  1251. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
  1252. }
  1253. int
  1254. fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch,
  1255. uint64_t parent,
  1256. const char *name,
  1257. size_t namelen)
  1258. {
  1259. struct fuse_notify_inval_entry_out outarg;
  1260. struct fuse_ll *f;
  1261. struct iovec iov[3];
  1262. if(!ch)
  1263. return -EINVAL;
  1264. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1265. if(!f)
  1266. return -ENODEV;
  1267. outarg.parent = parent;
  1268. outarg.namelen = namelen;
  1269. // TODO: Add ability to set `flags`
  1270. outarg.flags = 0;
  1271. iov[1].iov_base = &outarg;
  1272. iov[1].iov_len = sizeof(outarg);
  1273. iov[2].iov_base = (void *)name;
  1274. iov[2].iov_len = namelen + 1;
  1275. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
  1276. }
  1277. int
  1278. fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  1279. uint64_t parent,
  1280. uint64_t child,
  1281. const char *name,
  1282. size_t namelen)
  1283. {
  1284. struct fuse_notify_delete_out outarg;
  1285. struct fuse_ll *f;
  1286. struct iovec iov[3];
  1287. if(!ch)
  1288. return -EINVAL;
  1289. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1290. if(!f)
  1291. return -ENODEV;
  1292. if(f->conn.proto_minor < 18)
  1293. return -ENOSYS;
  1294. outarg.parent = parent;
  1295. outarg.child = child;
  1296. outarg.namelen = namelen;
  1297. outarg.padding = 0;
  1298. iov[1].iov_base = &outarg;
  1299. iov[1].iov_len = sizeof(outarg);
  1300. iov[2].iov_base = (void *)name;
  1301. iov[2].iov_len = namelen + 1;
  1302. return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
  1303. }
  1304. int
  1305. fuse_lowlevel_notify_store(struct fuse_chan *ch,
  1306. uint64_t ino,
  1307. off_t offset,
  1308. struct fuse_bufvec *bufv,
  1309. enum fuse_buf_copy_flags flags)
  1310. {
  1311. struct fuse_out_header out;
  1312. struct fuse_notify_store_out outarg;
  1313. struct fuse_ll *f;
  1314. struct iovec iov[3];
  1315. size_t size = fuse_buf_size(bufv);
  1316. int res;
  1317. if(!ch)
  1318. return -EINVAL;
  1319. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1320. if(!f)
  1321. return -ENODEV;
  1322. if(f->conn.proto_minor < 15)
  1323. return -ENOSYS;
  1324. out.unique = 0;
  1325. out.error = FUSE_NOTIFY_STORE;
  1326. outarg.nodeid = ino;
  1327. outarg.offset = offset;
  1328. outarg.size = size;
  1329. outarg.padding = 0;
  1330. iov[0].iov_base = &out;
  1331. iov[0].iov_len = sizeof(out);
  1332. iov[1].iov_base = &outarg;
  1333. iov[1].iov_len = sizeof(outarg);
  1334. res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
  1335. if(res > 0)
  1336. res = -res;
  1337. return res;
  1338. }
  1339. struct fuse_retrieve_req
  1340. {
  1341. struct fuse_notify_req nreq;
  1342. void *cookie;
  1343. };
  1344. static
  1345. void
  1346. fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
  1347. fuse_req_t req,
  1348. uint64_t ino,
  1349. const void *inarg)
  1350. {
  1351. struct fuse_retrieve_req *rreq =
  1352. container_of(nreq, struct fuse_retrieve_req, nreq);
  1353. fuse_reply_none(req);
  1354. free(rreq);
  1355. }
  1356. int
  1357. fuse_lowlevel_notify_retrieve(struct fuse_chan *ch,
  1358. uint64_t ino,
  1359. size_t size,
  1360. off_t offset,
  1361. void *cookie)
  1362. {
  1363. struct fuse_notify_retrieve_out outarg;
  1364. struct fuse_ll *f;
  1365. struct iovec iov[2];
  1366. struct fuse_retrieve_req *rreq;
  1367. int err;
  1368. if(!ch)
  1369. return -EINVAL;
  1370. f = (struct fuse_ll*)fuse_session_data(fuse_chan_session(ch));
  1371. if(!f)
  1372. return -ENODEV;
  1373. if(f->conn.proto_minor < 15)
  1374. return -ENOSYS;
  1375. rreq = malloc(sizeof(*rreq));
  1376. if(rreq == NULL)
  1377. return -ENOMEM;
  1378. pthread_mutex_lock(&f->lock);
  1379. rreq->cookie = cookie;
  1380. rreq->nreq.unique = f->notify_ctr++;
  1381. rreq->nreq.reply = fuse_ll_retrieve_reply;
  1382. list_add_nreq(&rreq->nreq, &f->notify_list);
  1383. pthread_mutex_unlock(&f->lock);
  1384. outarg.notify_unique = rreq->nreq.unique;
  1385. outarg.nodeid = ino;
  1386. outarg.offset = offset;
  1387. outarg.size = size;
  1388. iov[1].iov_base = &outarg;
  1389. iov[1].iov_len = sizeof(outarg);
  1390. err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
  1391. if(err)
  1392. {
  1393. pthread_mutex_lock(&f->lock);
  1394. list_del_nreq(&rreq->nreq);
  1395. pthread_mutex_unlock(&f->lock);
  1396. free(rreq);
  1397. }
  1398. return err;
  1399. }
  1400. void *
  1401. fuse_req_userdata(fuse_req_t req)
  1402. {
  1403. return req->f->userdata;
  1404. }
  1405. const
  1406. struct fuse_ctx *
  1407. fuse_req_ctx(fuse_req_t req)
  1408. {
  1409. return &req->ctx;
  1410. }
  1411. static struct {
  1412. void (*func)(fuse_req_t, struct fuse_in_header *);
  1413. const char *name;
  1414. } fuse_ll_ops[] =
  1415. {
  1416. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1417. [FUSE_FORGET] = { do_forget, "FORGET" },
  1418. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1419. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1420. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1421. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1422. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1423. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1424. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1425. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1426. [FUSE_RENAME] = { do_rename, "RENAME" },
  1427. [FUSE_LINK] = { do_link, "LINK" },
  1428. [FUSE_OPEN] = { do_open, "OPEN" },
  1429. [FUSE_READ] = { do_read, "READ" },
  1430. [FUSE_WRITE] = { do_write, "WRITE" },
  1431. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1432. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1433. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1434. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1435. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1436. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1437. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1438. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1439. [FUSE_INIT] = { do_init, "INIT" },
  1440. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1441. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1442. [FUSE_READDIRPLUS] = { do_readdir_plus, "READDIR_PLUS" },
  1443. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1444. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1445. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1446. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1447. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1448. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1449. [FUSE_CREATE] = { do_create, "CREATE" },
  1450. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  1451. [FUSE_BMAP] = { do_bmap, "BMAP" },
  1452. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  1453. [FUSE_POLL] = { do_poll, "POLL" },
  1454. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  1455. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  1456. [FUSE_NOTIFY_REPLY] = { do_notify_reply, "NOTIFY_REPLY" },
  1457. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  1458. [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
  1459. [FUSE_SETUPMAPPING] = { do_setupmapping, "SETUPMAPPING" },
  1460. [FUSE_REMOVEMAPPING] = { do_removemapping, "REMOVEMAPPING" },
  1461. [FUSE_SYNCFS] = { do_syncfs, "SYNCFS" },
  1462. [FUSE_TMPFILE] = { do_tmpfile, "TMPFILE" }
  1463. };
  1464. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  1465. enum {
  1466. KEY_HELP,
  1467. KEY_VERSION,
  1468. };
  1469. static const struct fuse_opt fuse_ll_opts[] =
  1470. {
  1471. { "debug", offsetof(struct fuse_ll, debug), 1 },
  1472. { "-d", offsetof(struct fuse_ll, debug), 1 },
  1473. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  1474. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  1475. { "congestion_threshold=%u",
  1476. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  1477. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1478. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1479. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1480. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1481. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  1482. FUSE_OPT_KEY("-h", KEY_HELP),
  1483. FUSE_OPT_KEY("--help", KEY_HELP),
  1484. FUSE_OPT_KEY("-V", KEY_VERSION),
  1485. FUSE_OPT_KEY("--version", KEY_VERSION),
  1486. FUSE_OPT_END
  1487. };
  1488. static
  1489. void
  1490. fuse_ll_version(void)
  1491. {
  1492. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  1493. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1494. }
  1495. static
  1496. void
  1497. fuse_ll_help(void)
  1498. {
  1499. fprintf(stderr,
  1500. " -o max_readahead=N set maximum readahead\n"
  1501. " -o max_background=N set number of maximum background requests\n"
  1502. " -o congestion_threshold=N set kernel's congestion threshold\n"
  1503. " -o no_remote_lock disable remote file locking\n"
  1504. " -o no_remote_flock disable remote file locking (BSD)\n"
  1505. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  1506. );
  1507. }
  1508. static
  1509. int
  1510. fuse_ll_opt_proc(void *data,
  1511. const char *arg,
  1512. int key,
  1513. struct fuse_args *outargs)
  1514. {
  1515. (void) data; (void) outargs;
  1516. switch (key)
  1517. {
  1518. case KEY_HELP:
  1519. fuse_ll_help();
  1520. break;
  1521. case KEY_VERSION:
  1522. fuse_ll_version();
  1523. break;
  1524. default:
  1525. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  1526. }
  1527. return -1;
  1528. }
  1529. int
  1530. fuse_lowlevel_is_lib_option(const char *opt)
  1531. {
  1532. return fuse_opt_match(fuse_ll_opts, opt);
  1533. }
  1534. static
  1535. void
  1536. fuse_ll_destroy(void *data)
  1537. {
  1538. struct fuse_ll *f = (struct fuse_ll *)data;
  1539. struct fuse_ll_pipe *llp;
  1540. if(f->got_init && !f->got_destroy)
  1541. {
  1542. if(f->op.destroy)
  1543. f->op.destroy(f->userdata);
  1544. }
  1545. llp = pthread_getspecific(f->pipe_key);
  1546. if(llp != NULL)
  1547. fuse_ll_pipe_free(llp);
  1548. pthread_key_delete(f->pipe_key);
  1549. pthread_mutex_destroy(&f->lock);
  1550. free(f);
  1551. lfmp_clear(&g_FMP_fuse_req);
  1552. }
  1553. static
  1554. void
  1555. fuse_ll_pipe_destructor(void *data)
  1556. {
  1557. struct fuse_ll_pipe *llp = data;
  1558. fuse_ll_pipe_free(llp);
  1559. }
  1560. static
  1561. void
  1562. fuse_send_errno(struct fuse_ll *f_,
  1563. struct fuse_chan *ch_,
  1564. const int errno_,
  1565. const uint64_t unique_id_)
  1566. {
  1567. struct fuse_out_header out = {0};
  1568. struct iovec iov = {0};
  1569. out.unique = unique_id_;
  1570. out.error = -errno_;
  1571. iov.iov_base = &out;
  1572. iov.iov_len = sizeof(struct fuse_out_header);
  1573. fuse_send_msg(f_,ch_,&iov,1);
  1574. }
  1575. static
  1576. void
  1577. fuse_send_enomem(struct fuse_ll *f_,
  1578. struct fuse_chan *ch_,
  1579. const uint64_t unique_id_)
  1580. {
  1581. fuse_send_errno(f_,ch_,ENOMEM,unique_id_);
  1582. }
  1583. static
  1584. int
  1585. fuse_ll_buf_receive_read(struct fuse_session *se_,
  1586. fuse_msgbuf_t *msgbuf_)
  1587. {
  1588. int rv;
  1589. rv = read(fuse_chan_fd(se_->ch),msgbuf_->mem,msgbuf_->size);
  1590. if(rv == -1)
  1591. return -errno;
  1592. if(rv < sizeof(struct fuse_in_header))
  1593. {
  1594. fprintf(stderr, "short read from fuse device\n");
  1595. return -EIO;
  1596. }
  1597. return rv;
  1598. }
  1599. static
  1600. void
  1601. fuse_ll_buf_process_read(struct fuse_session *se_,
  1602. const fuse_msgbuf_t *msgbuf_)
  1603. {
  1604. int err;
  1605. struct fuse_req *req;
  1606. struct fuse_in_header *in;
  1607. in = (struct fuse_in_header*)msgbuf_->mem;
  1608. // printf("%d\n",in->opcode);
  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 = ENOSYS;
  1618. if(in->opcode >= FUSE_MAXOP)
  1619. goto reply_err;
  1620. if(fuse_ll_ops[in->opcode].func == NULL)
  1621. goto reply_err;
  1622. fuse_ll_ops[in->opcode].func(req, in);
  1623. return;
  1624. reply_err:
  1625. fuse_reply_err(req, err);
  1626. return;
  1627. }
  1628. static
  1629. void
  1630. fuse_ll_buf_process_read_init(struct fuse_session *se_,
  1631. const fuse_msgbuf_t *msgbuf_)
  1632. {
  1633. int err;
  1634. struct fuse_req *req;
  1635. struct fuse_in_header *in;
  1636. in = (struct fuse_in_header*)msgbuf_->mem;
  1637. req = fuse_ll_alloc_req(se_->f);
  1638. if(req == NULL)
  1639. return fuse_send_enomem(se_->f,se_->ch,in->unique);
  1640. req->unique = in->unique;
  1641. req->ctx.uid = in->uid;
  1642. req->ctx.gid = in->gid;
  1643. req->ctx.pid = in->pid;
  1644. req->ch = se_->ch;
  1645. err = EIO;
  1646. if(in->opcode != FUSE_INIT)
  1647. goto reply_err;
  1648. if(fuse_ll_ops[in->opcode].func == NULL)
  1649. goto reply_err;
  1650. se_->process_buf = fuse_ll_buf_process_read;
  1651. fuse_ll_ops[in->opcode].func(req, in);
  1652. return;
  1653. reply_err:
  1654. fuse_reply_err(req, err);
  1655. return;
  1656. }
  1657. /*
  1658. * always call fuse_lowlevel_new_common() internally, to work around a
  1659. * misfeature in the FreeBSD runtime linker, which links the old
  1660. * version of a symbol to internal references.
  1661. */
  1662. struct fuse_session *
  1663. fuse_lowlevel_new_common(struct fuse_args *args,
  1664. const struct fuse_lowlevel_ops *op,
  1665. size_t op_size,
  1666. void *userdata)
  1667. {
  1668. int err;
  1669. struct fuse_ll *f;
  1670. struct fuse_session *se;
  1671. if(sizeof(struct fuse_lowlevel_ops) < op_size)
  1672. {
  1673. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  1674. op_size = sizeof(struct fuse_lowlevel_ops);
  1675. }
  1676. f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
  1677. if(f == NULL)
  1678. {
  1679. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  1680. goto out;
  1681. }
  1682. f->conn.max_write = UINT_MAX;
  1683. f->conn.max_readahead = UINT_MAX;
  1684. list_init_nreq(&f->notify_list);
  1685. f->notify_ctr = 1;
  1686. fuse_mutex_init(&f->lock);
  1687. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  1688. if(err)
  1689. {
  1690. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  1691. strerror(err));
  1692. goto out_free;
  1693. }
  1694. if(fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  1695. goto out_key_destroy;
  1696. memcpy(&f->op, op, op_size);
  1697. f->owner = getuid();
  1698. f->userdata = userdata;
  1699. se = fuse_session_new(f,
  1700. fuse_ll_buf_receive_read,
  1701. fuse_ll_buf_process_read_init,
  1702. fuse_ll_destroy);
  1703. if(!se)
  1704. goto out_key_destroy;
  1705. return se;
  1706. out_key_destroy:
  1707. pthread_key_delete(f->pipe_key);
  1708. out_free:
  1709. pthread_mutex_destroy(&f->lock);
  1710. free(f);
  1711. out:
  1712. return NULL;
  1713. }
  1714. struct fuse_session*
  1715. fuse_lowlevel_new(struct fuse_args *args,
  1716. const struct fuse_lowlevel_ops *op,
  1717. size_t op_size,
  1718. void *userdata)
  1719. {
  1720. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  1721. }