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.

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