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.

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