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.

2842 lines
72 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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 "config.h"
  9. #include "fuse_i.h"
  10. #include "fuse_kernel.h"
  11. #include "fuse_opt.h"
  12. #include "fuse_misc.h"
  13. #include "xmem.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stddef.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include <limits.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <sys/file.h>
  23. #ifndef F_LINUX_SPECIFIC_BASE
  24. #define F_LINUX_SPECIFIC_BASE 1024
  25. #endif
  26. #ifndef F_SETPIPE_SZ
  27. #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
  28. #endif
  29. #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
  30. #define OFFSET_MAX 0x7fffffffffffffffLL
  31. #define container_of(ptr, type, member) ({ \
  32. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  33. (type *)( (char *)__mptr - offsetof(type,member) );})
  34. struct fuse_pollhandle {
  35. uint64_t kh;
  36. struct fuse_chan *ch;
  37. struct fuse_ll *f;
  38. };
  39. static size_t pagesize;
  40. static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
  41. {
  42. pagesize = getpagesize();
  43. }
  44. static
  45. void
  46. convert_stat(const struct stat *stbuf_,
  47. struct fuse_attr *attr_)
  48. {
  49. attr_->ino = stbuf_->st_ino;
  50. attr_->mode = stbuf_->st_mode;
  51. attr_->nlink = stbuf_->st_nlink;
  52. attr_->uid = stbuf_->st_uid;
  53. attr_->gid = stbuf_->st_gid;
  54. attr_->rdev = stbuf_->st_rdev;
  55. attr_->size = stbuf_->st_size;
  56. attr_->blksize = stbuf_->st_blksize;
  57. attr_->blocks = stbuf_->st_blocks;
  58. attr_->atime = stbuf_->st_atime;
  59. attr_->mtime = stbuf_->st_mtime;
  60. attr_->ctime = stbuf_->st_ctime;
  61. attr_->atimensec = ST_ATIM_NSEC(stbuf_);
  62. attr_->mtimensec = ST_MTIM_NSEC(stbuf_);
  63. attr_->ctimensec = ST_CTIM_NSEC(stbuf_);
  64. }
  65. static
  66. void
  67. convert_attr(const struct fuse_setattr_in *attr_,
  68. struct stat *stbuf_)
  69. {
  70. stbuf_->st_mode = attr_->mode;
  71. stbuf_->st_uid = attr_->uid;
  72. stbuf_->st_gid = attr_->gid;
  73. stbuf_->st_size = attr_->size;
  74. stbuf_->st_atime = attr_->atime;
  75. stbuf_->st_mtime = attr_->mtime;
  76. stbuf_->st_ctime = attr_->ctime;
  77. ST_ATIM_NSEC_SET(stbuf_,attr_->atimensec);
  78. ST_MTIM_NSEC_SET(stbuf_,attr_->mtimensec);
  79. ST_CTIM_NSEC_SET(stbuf_,attr_->ctimensec);
  80. }
  81. static size_t iov_length(const struct iovec *iov, size_t count)
  82. {
  83. size_t seg;
  84. size_t ret = 0;
  85. for (seg = 0; seg < count; seg++)
  86. ret += iov[seg].iov_len;
  87. return ret;
  88. }
  89. static void list_init_req(struct fuse_req *req)
  90. {
  91. req->next = req;
  92. req->prev = req;
  93. }
  94. static void list_del_req(struct fuse_req *req)
  95. {
  96. struct fuse_req *prev = req->prev;
  97. struct fuse_req *next = req->next;
  98. prev->next = next;
  99. next->prev = prev;
  100. }
  101. static void list_add_req(struct fuse_req *req, struct fuse_req *next)
  102. {
  103. struct fuse_req *prev = next->prev;
  104. req->next = next;
  105. req->prev = prev;
  106. prev->next = req;
  107. next->prev = req;
  108. }
  109. static void destroy_req(fuse_req_t req)
  110. {
  111. pthread_mutex_destroy(&req->lock);
  112. xmem_free(req);
  113. }
  114. void fuse_free_req(fuse_req_t req)
  115. {
  116. int ctr;
  117. struct fuse_ll *f = req->f;
  118. pthread_mutex_lock(&f->lock);
  119. req->u.ni.func = NULL;
  120. req->u.ni.data = NULL;
  121. list_del_req(req);
  122. ctr = --req->ctr;
  123. pthread_mutex_unlock(&f->lock);
  124. if (!ctr)
  125. destroy_req(req);
  126. }
  127. static struct fuse_req *fuse_ll_alloc_req(struct fuse_ll *f)
  128. {
  129. struct fuse_req *req;
  130. req = (struct fuse_req *)xmem_calloc(1, sizeof(struct fuse_req));
  131. if (req == NULL) {
  132. fprintf(stderr, "fuse: failed to allocate request\n");
  133. } else {
  134. req->f = f;
  135. req->ctr = 1;
  136. list_init_req(req);
  137. fuse_mutex_init(&req->lock);
  138. }
  139. return req;
  140. }
  141. static int fuse_send_msg(struct fuse_ll *f, struct fuse_chan *ch,
  142. struct iovec *iov, int count)
  143. {
  144. struct fuse_out_header *out = iov[0].iov_base;
  145. out->len = iov_length(iov, count);
  146. if (f->debug) {
  147. if (out->unique == 0) {
  148. fprintf(stderr, "NOTIFY: code=%d length=%u\n",
  149. out->error, out->len);
  150. } else if (out->error) {
  151. fprintf(stderr,
  152. " unique: %llu, error: %i (%s), outsize: %i\n",
  153. (unsigned long long) out->unique, out->error,
  154. strerror(-out->error), out->len);
  155. } else {
  156. fprintf(stderr,
  157. " unique: %llu, success, outsize: %i\n",
  158. (unsigned long long) out->unique, out->len);
  159. }
  160. }
  161. return fuse_chan_send(ch, iov, count);
  162. }
  163. int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
  164. int count)
  165. {
  166. struct fuse_out_header out;
  167. if (error <= -1000 || error > 0) {
  168. fprintf(stderr, "fuse: bad error value: %i\n", error);
  169. error = -ERANGE;
  170. }
  171. out.unique = req->unique;
  172. out.error = error;
  173. iov[0].iov_base = &out;
  174. iov[0].iov_len = sizeof(struct fuse_out_header);
  175. return fuse_send_msg(req->f, req->ch, iov, count);
  176. }
  177. static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
  178. int count)
  179. {
  180. int res;
  181. res = fuse_send_reply_iov_nofree(req, error, iov, count);
  182. fuse_free_req(req);
  183. return res;
  184. }
  185. static int send_reply(fuse_req_t req, int error, const void *arg,
  186. size_t argsize)
  187. {
  188. struct iovec iov[2];
  189. int count = 1;
  190. if (argsize) {
  191. iov[1].iov_base = (void *) arg;
  192. iov[1].iov_len = argsize;
  193. count++;
  194. }
  195. return send_reply_iov(req, error, iov, count);
  196. }
  197. int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
  198. {
  199. int res;
  200. struct iovec *padded_iov;
  201. padded_iov = xmem_malloc((count + 1) * sizeof(struct iovec));
  202. if (padded_iov == NULL)
  203. return fuse_reply_err(req, ENOMEM);
  204. memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
  205. count++;
  206. res = send_reply_iov(req, 0, padded_iov, count);
  207. xmem_free(padded_iov);
  208. return res;
  209. }
  210. size_t fuse_dirent_size(size_t namelen)
  211. {
  212. return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen);
  213. }
  214. char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf,
  215. off_t off)
  216. {
  217. unsigned namelen = strlen(name);
  218. unsigned entlen = FUSE_NAME_OFFSET + namelen;
  219. unsigned entsize = fuse_dirent_size(namelen);
  220. unsigned padlen = entsize - entlen;
  221. struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
  222. dirent->ino = stbuf->st_ino;
  223. dirent->off = off;
  224. dirent->namelen = namelen;
  225. dirent->type = (stbuf->st_mode & 0170000) >> 12;
  226. strncpy(dirent->name, name, namelen);
  227. if (padlen)
  228. memset(buf + entlen, 0, padlen);
  229. return buf + entsize;
  230. }
  231. size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
  232. const char *name, const struct stat *stbuf, off_t off)
  233. {
  234. size_t entsize;
  235. (void) req;
  236. entsize = fuse_dirent_size(strlen(name));
  237. if (entsize <= bufsize && buf)
  238. fuse_add_dirent(buf, name, stbuf, off);
  239. return entsize;
  240. }
  241. static void convert_statfs(const struct statvfs *stbuf,
  242. struct fuse_kstatfs *kstatfs)
  243. {
  244. kstatfs->bsize = stbuf->f_bsize;
  245. kstatfs->frsize = stbuf->f_frsize;
  246. kstatfs->blocks = stbuf->f_blocks;
  247. kstatfs->bfree = stbuf->f_bfree;
  248. kstatfs->bavail = stbuf->f_bavail;
  249. kstatfs->files = stbuf->f_files;
  250. kstatfs->ffree = stbuf->f_ffree;
  251. kstatfs->namelen = stbuf->f_namemax;
  252. }
  253. static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
  254. {
  255. return send_reply(req, 0, arg, argsize);
  256. }
  257. int
  258. fuse_reply_err(fuse_req_t req_,
  259. int err_)
  260. {
  261. return send_reply(req_,-err_,NULL,0);
  262. }
  263. void fuse_reply_none(fuse_req_t req)
  264. {
  265. if (req->ch)
  266. fuse_chan_send(req->ch, NULL, 0);
  267. fuse_free_req(req);
  268. }
  269. static
  270. void
  271. fill_entry(struct fuse_entry_out *arg,
  272. const struct fuse_entry_param *e)
  273. {
  274. arg->nodeid = e->ino;
  275. arg->generation = e->generation;
  276. arg->entry_valid = e->timeout.entry;
  277. arg->entry_valid_nsec = 0;
  278. arg->attr_valid = e->timeout.attr;
  279. arg->attr_valid_nsec = 0;
  280. convert_stat(&e->attr,&arg->attr);
  281. }
  282. static void fill_open(struct fuse_open_out *arg,
  283. const struct fuse_file_info *f)
  284. {
  285. arg->fh = f->fh;
  286. if (f->direct_io)
  287. arg->open_flags |= FOPEN_DIRECT_IO;
  288. if (f->keep_cache)
  289. arg->open_flags |= FOPEN_KEEP_CACHE;
  290. if (f->nonseekable)
  291. arg->open_flags |= FOPEN_NONSEEKABLE;
  292. if (f->cache_readdir)
  293. arg->open_flags |= FOPEN_CACHE_DIR;
  294. }
  295. int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
  296. {
  297. struct fuse_entry_out arg;
  298. size_t size = req->f->conn.proto_minor < 9 ?
  299. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
  300. /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
  301. negative entry */
  302. if (!e->ino && req->f->conn.proto_minor < 4)
  303. return fuse_reply_err(req, ENOENT);
  304. memset(&arg, 0, sizeof(arg));
  305. fill_entry(&arg, e);
  306. return send_reply_ok(req, &arg, size);
  307. }
  308. int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
  309. const struct fuse_file_info *f)
  310. {
  311. char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
  312. size_t entrysize = req->f->conn.proto_minor < 9 ?
  313. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
  314. struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
  315. struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
  316. memset(buf, 0, sizeof(buf));
  317. fill_entry(earg, e);
  318. fill_open(oarg, f);
  319. return send_reply_ok(req, buf,
  320. entrysize + sizeof(struct fuse_open_out));
  321. }
  322. int
  323. fuse_reply_attr(fuse_req_t req,
  324. const struct stat *attr,
  325. const uint64_t timeout)
  326. {
  327. struct fuse_attr_out arg;
  328. size_t size = req->f->conn.proto_minor < 9 ?
  329. FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
  330. memset(&arg,0,sizeof(arg));
  331. arg.attr_valid = timeout;
  332. arg.attr_valid_nsec = 0;
  333. convert_stat(attr,&arg.attr);
  334. return send_reply_ok(req,&arg,size);
  335. }
  336. int fuse_reply_readlink(fuse_req_t req, const char *linkname)
  337. {
  338. return send_reply_ok(req, linkname, strlen(linkname));
  339. }
  340. int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
  341. {
  342. struct fuse_open_out arg;
  343. memset(&arg, 0, sizeof(arg));
  344. fill_open(&arg, f);
  345. return send_reply_ok(req, &arg, sizeof(arg));
  346. }
  347. int fuse_reply_write(fuse_req_t req, size_t count)
  348. {
  349. struct fuse_write_out arg;
  350. memset(&arg, 0, sizeof(arg));
  351. arg.size = count;
  352. return send_reply_ok(req, &arg, sizeof(arg));
  353. }
  354. int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
  355. {
  356. return send_reply_ok(req, buf, size);
  357. }
  358. static int fuse_send_data_iov_fallback(struct fuse_ll *f, struct fuse_chan *ch,
  359. struct iovec *iov, int iov_count,
  360. struct fuse_bufvec *buf,
  361. size_t len)
  362. {
  363. struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
  364. void *mbuf;
  365. int res;
  366. /* Optimize common case */
  367. if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
  368. !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
  369. /* FIXME: also avoid memory copy if there are multiple buffers
  370. but none of them contain an fd */
  371. iov[iov_count].iov_base = buf->buf[0].mem;
  372. iov[iov_count].iov_len = len;
  373. iov_count++;
  374. return fuse_send_msg(f, ch, iov, iov_count);
  375. }
  376. res = posix_memalign(&mbuf, pagesize, len);
  377. if (res != 0)
  378. return res;
  379. mem_buf.buf[0].mem = mbuf;
  380. res = fuse_buf_copy(&mem_buf, buf, 0);
  381. if (res < 0) {
  382. xmem_free(mbuf);
  383. return -res;
  384. }
  385. len = res;
  386. iov[iov_count].iov_base = mbuf;
  387. iov[iov_count].iov_len = len;
  388. iov_count++;
  389. res = fuse_send_msg(f, ch, iov, iov_count);
  390. xmem_free(mbuf);
  391. return res;
  392. }
  393. struct fuse_ll_pipe {
  394. size_t size;
  395. int can_grow;
  396. int pipe[2];
  397. };
  398. static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
  399. {
  400. close(llp->pipe[0]);
  401. close(llp->pipe[1]);
  402. xmem_free(llp);
  403. }
  404. #ifdef HAVE_SPLICE
  405. static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_ll *f)
  406. {
  407. struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
  408. if (llp == NULL) {
  409. int res;
  410. llp = xmem_malloc(sizeof(struct fuse_ll_pipe));
  411. if (llp == NULL)
  412. return NULL;
  413. res = pipe(llp->pipe);
  414. if (res == -1) {
  415. xmem_free(llp);
  416. return NULL;
  417. }
  418. if (fcntl(llp->pipe[0], F_SETFL, O_NONBLOCK) == -1 ||
  419. fcntl(llp->pipe[1], F_SETFL, O_NONBLOCK) == -1) {
  420. close(llp->pipe[0]);
  421. close(llp->pipe[1]);
  422. xmem_free(llp);
  423. return NULL;
  424. }
  425. /*
  426. *the default size is 16 pages on linux
  427. */
  428. llp->size = pagesize * 16;
  429. llp->can_grow = 1;
  430. pthread_setspecific(f->pipe_key, llp);
  431. }
  432. return llp;
  433. }
  434. #endif
  435. static void fuse_ll_clear_pipe(struct fuse_ll *f)
  436. {
  437. struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
  438. if (llp) {
  439. pthread_setspecific(f->pipe_key, NULL);
  440. fuse_ll_pipe_free(llp);
  441. }
  442. }
  443. #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
  444. static int read_back(int fd, char *buf, size_t len)
  445. {
  446. int res;
  447. res = read(fd, buf, len);
  448. if (res == -1) {
  449. fprintf(stderr, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
  450. return -EIO;
  451. }
  452. if (res != len) {
  453. fprintf(stderr, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
  454. return -EIO;
  455. }
  456. return 0;
  457. }
  458. static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
  459. struct iovec *iov, int iov_count,
  460. struct fuse_bufvec *buf, unsigned int flags)
  461. {
  462. int res;
  463. size_t len = fuse_buf_size(buf);
  464. struct fuse_out_header *out = iov[0].iov_base;
  465. struct fuse_ll_pipe *llp;
  466. int splice_flags;
  467. size_t pipesize;
  468. size_t total_fd_size;
  469. size_t idx;
  470. size_t headerlen;
  471. struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
  472. if (f->broken_splice_nonblock)
  473. goto fallback;
  474. if (flags & FUSE_BUF_NO_SPLICE)
  475. goto fallback;
  476. total_fd_size = 0;
  477. for (idx = buf->idx; idx < buf->count; idx++) {
  478. if (buf->buf[idx].flags & FUSE_BUF_IS_FD) {
  479. total_fd_size = buf->buf[idx].size;
  480. if (idx == buf->idx)
  481. total_fd_size -= buf->off;
  482. }
  483. }
  484. if (total_fd_size < 2 * pagesize)
  485. goto fallback;
  486. if (f->conn.proto_minor < 14 ||
  487. !(f->conn.want & FUSE_CAP_SPLICE_WRITE))
  488. goto fallback;
  489. llp = fuse_ll_get_pipe(f);
  490. if (llp == NULL)
  491. goto fallback;
  492. headerlen = iov_length(iov, iov_count);
  493. out->len = headerlen + len;
  494. /*
  495. * Heuristic for the required pipe size, does not work if the
  496. * source contains less than page size fragments
  497. */
  498. pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
  499. if (llp->size < pipesize) {
  500. if (llp->can_grow) {
  501. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
  502. if (res == -1) {
  503. llp->can_grow = 0;
  504. goto fallback;
  505. }
  506. llp->size = res;
  507. }
  508. if (llp->size < pipesize)
  509. goto fallback;
  510. }
  511. res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
  512. if (res == -1)
  513. goto fallback;
  514. if (res != headerlen) {
  515. res = -EIO;
  516. fprintf(stderr, "fuse: short vmsplice to pipe: %u/%zu\n", res,
  517. headerlen);
  518. goto clear_pipe;
  519. }
  520. pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
  521. pipe_buf.buf[0].fd = llp->pipe[1];
  522. res = fuse_buf_copy(&pipe_buf, buf,
  523. FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
  524. if (res < 0) {
  525. if (res == -EAGAIN || res == -EINVAL) {
  526. /*
  527. * Should only get EAGAIN on kernels with
  528. * broken SPLICE_F_NONBLOCK support (<=
  529. * 2.6.35) where this error or a short read is
  530. * returned even if the pipe itself is not
  531. * full
  532. *
  533. * EINVAL might mean that splice can't handle
  534. * this combination of input and output.
  535. */
  536. if (res == -EAGAIN)
  537. f->broken_splice_nonblock = 1;
  538. pthread_setspecific(f->pipe_key, NULL);
  539. fuse_ll_pipe_free(llp);
  540. goto fallback;
  541. }
  542. res = -res;
  543. goto clear_pipe;
  544. }
  545. if (res != 0 && res < len) {
  546. struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
  547. void *mbuf;
  548. size_t now_len = res;
  549. /*
  550. * For regular files a short count is either
  551. * 1) due to EOF, or
  552. * 2) because of broken SPLICE_F_NONBLOCK (see above)
  553. *
  554. * For other inputs it's possible that we overflowed
  555. * the pipe because of small buffer fragments.
  556. */
  557. res = posix_memalign(&mbuf, pagesize, len);
  558. if (res != 0)
  559. goto clear_pipe;
  560. mem_buf.buf[0].mem = mbuf;
  561. mem_buf.off = now_len;
  562. res = fuse_buf_copy(&mem_buf, buf, 0);
  563. if (res > 0) {
  564. char *tmpbuf;
  565. size_t extra_len = res;
  566. /*
  567. * Trickiest case: got more data. Need to get
  568. * back the data from the pipe and then fall
  569. * back to regular write.
  570. */
  571. tmpbuf = xmem_malloc(headerlen);
  572. if (tmpbuf == NULL) {
  573. xmem_free(mbuf);
  574. res = ENOMEM;
  575. goto clear_pipe;
  576. }
  577. res = read_back(llp->pipe[0], tmpbuf, headerlen);
  578. xmem_free(tmpbuf);
  579. if (res != 0) {
  580. xmem_free(mbuf);
  581. goto clear_pipe;
  582. }
  583. res = read_back(llp->pipe[0], mbuf, now_len);
  584. if (res != 0) {
  585. xmem_free(mbuf);
  586. goto clear_pipe;
  587. }
  588. len = now_len + extra_len;
  589. iov[iov_count].iov_base = mbuf;
  590. iov[iov_count].iov_len = len;
  591. iov_count++;
  592. res = fuse_send_msg(f, ch, iov, iov_count);
  593. xmem_free(mbuf);
  594. return res;
  595. }
  596. xmem_free(mbuf);
  597. res = now_len;
  598. }
  599. len = res;
  600. out->len = headerlen + len;
  601. if (f->debug) {
  602. fprintf(stderr,
  603. " unique: %llu, success, outsize: %i (splice)\n",
  604. (unsigned long long) out->unique, out->len);
  605. }
  606. splice_flags = 0;
  607. if ((flags & FUSE_BUF_SPLICE_MOVE) &&
  608. (f->conn.want & FUSE_CAP_SPLICE_MOVE))
  609. splice_flags |= SPLICE_F_MOVE;
  610. res = splice(llp->pipe[0], NULL,
  611. fuse_chan_fd(ch), NULL, out->len, splice_flags);
  612. if (res == -1) {
  613. res = -errno;
  614. perror("fuse: splice from pipe");
  615. goto clear_pipe;
  616. }
  617. if (res != out->len) {
  618. res = -EIO;
  619. fprintf(stderr, "fuse: short splice from pipe: %u/%u\n",
  620. res, out->len);
  621. goto clear_pipe;
  622. }
  623. return 0;
  624. clear_pipe:
  625. fuse_ll_clear_pipe(f);
  626. return res;
  627. fallback:
  628. return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
  629. }
  630. #else
  631. static int fuse_send_data_iov(struct fuse_ll *f, struct fuse_chan *ch,
  632. struct iovec *iov, int iov_count,
  633. struct fuse_bufvec *buf, unsigned int flags)
  634. {
  635. size_t len = fuse_buf_size(buf);
  636. (void) flags;
  637. return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
  638. }
  639. #endif
  640. int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
  641. enum fuse_buf_copy_flags flags)
  642. {
  643. struct iovec iov[2];
  644. struct fuse_out_header out;
  645. int res;
  646. iov[0].iov_base = &out;
  647. iov[0].iov_len = sizeof(struct fuse_out_header);
  648. out.unique = req->unique;
  649. out.error = 0;
  650. res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
  651. if (res <= 0) {
  652. fuse_free_req(req);
  653. return res;
  654. } else {
  655. return fuse_reply_err(req, res);
  656. }
  657. }
  658. int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
  659. {
  660. struct fuse_statfs_out arg;
  661. size_t size = req->f->conn.proto_minor < 4 ?
  662. FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
  663. memset(&arg, 0, sizeof(arg));
  664. convert_statfs(stbuf, &arg.st);
  665. return send_reply_ok(req, &arg, size);
  666. }
  667. int fuse_reply_xattr(fuse_req_t req, size_t count)
  668. {
  669. struct fuse_getxattr_out arg;
  670. memset(&arg, 0, sizeof(arg));
  671. arg.size = count;
  672. return send_reply_ok(req, &arg, sizeof(arg));
  673. }
  674. int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
  675. {
  676. struct fuse_lk_out arg;
  677. memset(&arg, 0, sizeof(arg));
  678. arg.lk.type = lock->l_type;
  679. if (lock->l_type != F_UNLCK) {
  680. arg.lk.start = lock->l_start;
  681. if (lock->l_len == 0)
  682. arg.lk.end = OFFSET_MAX;
  683. else
  684. arg.lk.end = lock->l_start + lock->l_len - 1;
  685. }
  686. arg.lk.pid = lock->l_pid;
  687. return send_reply_ok(req, &arg, sizeof(arg));
  688. }
  689. int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
  690. {
  691. struct fuse_bmap_out arg;
  692. memset(&arg, 0, sizeof(arg));
  693. arg.block = idx;
  694. return send_reply_ok(req, &arg, sizeof(arg));
  695. }
  696. static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
  697. size_t count)
  698. {
  699. struct fuse_ioctl_iovec *fiov;
  700. size_t i;
  701. fiov = xmem_malloc(sizeof(fiov[0]) * count);
  702. if (!fiov)
  703. return NULL;
  704. for (i = 0; i < count; i++) {
  705. fiov[i].base = (uintptr_t) iov[i].iov_base;
  706. fiov[i].len = iov[i].iov_len;
  707. }
  708. return fiov;
  709. }
  710. int fuse_reply_ioctl_retry(fuse_req_t req,
  711. const struct iovec *in_iov, size_t in_count,
  712. const struct iovec *out_iov, size_t out_count)
  713. {
  714. struct fuse_ioctl_out arg;
  715. struct fuse_ioctl_iovec *in_fiov = NULL;
  716. struct fuse_ioctl_iovec *out_fiov = NULL;
  717. struct iovec iov[4];
  718. size_t count = 1;
  719. int res;
  720. memset(&arg, 0, sizeof(arg));
  721. arg.flags |= FUSE_IOCTL_RETRY;
  722. arg.in_iovs = in_count;
  723. arg.out_iovs = out_count;
  724. iov[count].iov_base = &arg;
  725. iov[count].iov_len = sizeof(arg);
  726. count++;
  727. if (req->f->conn.proto_minor < 16) {
  728. if (in_count) {
  729. iov[count].iov_base = (void *)in_iov;
  730. iov[count].iov_len = sizeof(in_iov[0]) * in_count;
  731. count++;
  732. }
  733. if (out_count) {
  734. iov[count].iov_base = (void *)out_iov;
  735. iov[count].iov_len = sizeof(out_iov[0]) * out_count;
  736. count++;
  737. }
  738. } else {
  739. /* Can't handle non-compat 64bit ioctls on 32bit */
  740. if (sizeof(void *) == 4 && req->ioctl_64bit) {
  741. res = fuse_reply_err(req, EINVAL);
  742. goto out;
  743. }
  744. if (in_count) {
  745. in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
  746. if (!in_fiov)
  747. goto enomem;
  748. iov[count].iov_base = (void *)in_fiov;
  749. iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
  750. count++;
  751. }
  752. if (out_count) {
  753. out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
  754. if (!out_fiov)
  755. goto enomem;
  756. iov[count].iov_base = (void *)out_fiov;
  757. iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
  758. count++;
  759. }
  760. }
  761. res = send_reply_iov(req, 0, iov, count);
  762. out:
  763. xmem_free(in_fiov);
  764. xmem_free(out_fiov);
  765. return res;
  766. enomem:
  767. res = fuse_reply_err(req, ENOMEM);
  768. goto out;
  769. }
  770. int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, uint32_t size)
  771. {
  772. int count;
  773. struct iovec iov[3];
  774. struct fuse_ioctl_out arg;
  775. arg.result = result;
  776. arg.flags = 0;
  777. arg.in_iovs = 0;
  778. arg.out_iovs = 0;
  779. count = 1;
  780. iov[count].iov_base = &arg;
  781. iov[count].iov_len = sizeof(arg);
  782. count++;
  783. if(size)
  784. {
  785. iov[count].iov_base = (char*)buf;
  786. iov[count].iov_len = size;
  787. count++;
  788. }
  789. return send_reply_iov(req, 0, iov, count);
  790. }
  791. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  792. int count)
  793. {
  794. struct iovec *padded_iov;
  795. struct fuse_ioctl_out arg;
  796. int res;
  797. padded_iov = xmem_malloc((count + 2) * sizeof(struct iovec));
  798. if (padded_iov == NULL)
  799. return fuse_reply_err(req, ENOMEM);
  800. memset(&arg, 0, sizeof(arg));
  801. arg.result = result;
  802. padded_iov[1].iov_base = &arg;
  803. padded_iov[1].iov_len = sizeof(arg);
  804. memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
  805. res = send_reply_iov(req, 0, padded_iov, count + 2);
  806. xmem_free(padded_iov);
  807. return res;
  808. }
  809. int fuse_reply_poll(fuse_req_t req, unsigned revents)
  810. {
  811. struct fuse_poll_out arg;
  812. memset(&arg, 0, sizeof(arg));
  813. arg.revents = revents;
  814. return send_reply_ok(req, &arg, sizeof(arg));
  815. }
  816. static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  817. {
  818. char *name = (char *) inarg;
  819. if (req->f->op.lookup)
  820. req->f->op.lookup(req, nodeid, name);
  821. else
  822. fuse_reply_err(req, ENOSYS);
  823. }
  824. static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  825. {
  826. struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
  827. if (req->f->op.forget)
  828. req->f->op.forget(req, nodeid, arg->nlookup);
  829. else
  830. fuse_reply_none(req);
  831. }
  832. static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
  833. const void *inarg)
  834. {
  835. struct fuse_batch_forget_in *arg = (void *) inarg;
  836. struct fuse_forget_one *param = (void *) PARAM(arg);
  837. unsigned int i;
  838. (void) nodeid;
  839. if (req->f->op.forget_multi) {
  840. req->f->op.forget_multi(req, arg->count,
  841. (struct fuse_forget_data *) param);
  842. } else if (req->f->op.forget) {
  843. for (i = 0; i < arg->count; i++) {
  844. struct fuse_forget_one *forget = &param[i];
  845. struct fuse_req *dummy_req;
  846. dummy_req = fuse_ll_alloc_req(req->f);
  847. if (dummy_req == NULL)
  848. break;
  849. dummy_req->unique = req->unique;
  850. dummy_req->ctx = req->ctx;
  851. dummy_req->ch = NULL;
  852. req->f->op.forget(dummy_req, forget->nodeid,
  853. forget->nlookup);
  854. }
  855. fuse_reply_none(req);
  856. } else {
  857. fuse_reply_none(req);
  858. }
  859. }
  860. static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  861. {
  862. struct fuse_file_info *fip = NULL;
  863. struct fuse_file_info fi;
  864. if (req->f->conn.proto_minor >= 9) {
  865. struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
  866. if (arg->getattr_flags & FUSE_GETATTR_FH) {
  867. memset(&fi, 0, sizeof(fi));
  868. fi.fh = arg->fh;
  869. fip = &fi;
  870. }
  871. }
  872. if (req->f->op.getattr)
  873. req->f->op.getattr(req, nodeid, fip);
  874. else
  875. fuse_reply_err(req, ENOSYS);
  876. }
  877. static
  878. void
  879. do_setattr(fuse_req_t req_,
  880. fuse_ino_t nodeid_,
  881. const void *inarg_)
  882. {
  883. struct stat stbuf;
  884. struct fuse_file_info *fi;
  885. struct fuse_file_info fi_store;
  886. struct fuse_setattr_in *arg;
  887. if(req_->f->op.setattr == NULL)
  888. return (void)fuse_reply_err(req_,ENOSYS);
  889. fi = NULL;
  890. arg = (struct fuse_setattr_in*)inarg_;
  891. memset(&stbuf,0,sizeof(stbuf));
  892. convert_attr(arg,&stbuf);
  893. if(arg->valid & FATTR_FH)
  894. {
  895. arg->valid &= ~FATTR_FH;
  896. memset(&fi_store,0,sizeof(fi_store));
  897. fi = &fi_store;
  898. fi->fh = arg->fh;
  899. }
  900. arg->valid &=
  901. (FATTR_MODE |
  902. FATTR_UID |
  903. FATTR_GID |
  904. FATTR_SIZE |
  905. FATTR_ATIME |
  906. FATTR_MTIME |
  907. FATTR_CTIME |
  908. FATTR_ATIME_NOW |
  909. FATTR_MTIME_NOW);
  910. req_->f->op.setattr(req_,nodeid_,&stbuf,arg->valid,fi);
  911. }
  912. static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  913. {
  914. struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
  915. if (req->f->op.access)
  916. req->f->op.access(req, nodeid, arg->mask);
  917. else
  918. fuse_reply_err(req, ENOSYS);
  919. }
  920. static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  921. {
  922. (void) inarg;
  923. if (req->f->op.readlink)
  924. req->f->op.readlink(req, nodeid);
  925. else
  926. fuse_reply_err(req, ENOSYS);
  927. }
  928. static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  929. {
  930. struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
  931. char *name = PARAM(arg);
  932. if (req->f->conn.proto_minor >= 12)
  933. req->ctx.umask = arg->umask;
  934. else
  935. name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
  936. if (req->f->op.mknod)
  937. req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
  938. else
  939. fuse_reply_err(req, ENOSYS);
  940. }
  941. static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  942. {
  943. struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
  944. if (req->f->conn.proto_minor >= 12)
  945. req->ctx.umask = arg->umask;
  946. if (req->f->op.mkdir)
  947. req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
  948. else
  949. fuse_reply_err(req, ENOSYS);
  950. }
  951. static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  952. {
  953. char *name = (char *) inarg;
  954. if (req->f->op.unlink)
  955. req->f->op.unlink(req, nodeid, name);
  956. else
  957. fuse_reply_err(req, ENOSYS);
  958. }
  959. static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  960. {
  961. char *name = (char *) inarg;
  962. if (req->f->op.rmdir)
  963. req->f->op.rmdir(req, nodeid, name);
  964. else
  965. fuse_reply_err(req, ENOSYS);
  966. }
  967. static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  968. {
  969. char *name = (char *) inarg;
  970. char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
  971. if (req->f->op.symlink)
  972. req->f->op.symlink(req, linkname, nodeid, name);
  973. else
  974. fuse_reply_err(req, ENOSYS);
  975. }
  976. static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  977. {
  978. struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
  979. char *oldname = PARAM(arg);
  980. char *newname = oldname + strlen(oldname) + 1;
  981. if (req->f->op.rename)
  982. req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
  983. else
  984. fuse_reply_err(req, ENOSYS);
  985. }
  986. static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  987. {
  988. struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
  989. if (req->f->op.link)
  990. req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
  991. else
  992. fuse_reply_err(req, ENOSYS);
  993. }
  994. static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  995. {
  996. struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
  997. if (req->f->op.create) {
  998. struct fuse_file_info fi;
  999. char *name = PARAM(arg);
  1000. memset(&fi, 0, sizeof(fi));
  1001. fi.flags = arg->flags;
  1002. if (req->f->conn.proto_minor >= 12)
  1003. req->ctx.umask = arg->umask;
  1004. else
  1005. name = (char *) inarg + sizeof(struct fuse_open_in);
  1006. req->f->op.create(req, nodeid, name, arg->mode, &fi);
  1007. } else
  1008. fuse_reply_err(req, ENOSYS);
  1009. }
  1010. static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1011. {
  1012. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  1013. struct fuse_file_info fi;
  1014. memset(&fi, 0, sizeof(fi));
  1015. fi.flags = arg->flags;
  1016. if (req->f->op.open)
  1017. req->f->op.open(req, nodeid, &fi);
  1018. else
  1019. fuse_reply_open(req, &fi);
  1020. }
  1021. static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1022. {
  1023. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1024. if (req->f->op.read) {
  1025. struct fuse_file_info fi;
  1026. memset(&fi, 0, sizeof(fi));
  1027. fi.fh = arg->fh;
  1028. if (req->f->conn.proto_minor >= 9) {
  1029. fi.lock_owner = arg->lock_owner;
  1030. fi.flags = arg->flags;
  1031. }
  1032. req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
  1033. } else
  1034. fuse_reply_err(req, ENOSYS);
  1035. }
  1036. static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1037. {
  1038. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1039. struct fuse_file_info fi;
  1040. char *param;
  1041. memset(&fi, 0, sizeof(fi));
  1042. fi.fh = arg->fh;
  1043. fi.writepage = arg->write_flags & 1;
  1044. if (req->f->conn.proto_minor < 9) {
  1045. param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1046. } else {
  1047. fi.lock_owner = arg->lock_owner;
  1048. fi.flags = arg->flags;
  1049. param = PARAM(arg);
  1050. }
  1051. if (req->f->op.write)
  1052. req->f->op.write(req, nodeid, param, arg->size,
  1053. arg->offset, &fi);
  1054. else
  1055. fuse_reply_err(req, ENOSYS);
  1056. }
  1057. static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
  1058. const struct fuse_buf *ibuf)
  1059. {
  1060. struct fuse_ll *f = req->f;
  1061. struct fuse_bufvec bufv = {
  1062. .buf[0] = *ibuf,
  1063. .count = 1,
  1064. };
  1065. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1066. struct fuse_file_info fi;
  1067. memset(&fi, 0, sizeof(fi));
  1068. fi.fh = arg->fh;
  1069. fi.writepage = arg->write_flags & 1;
  1070. if (req->f->conn.proto_minor < 9) {
  1071. bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1072. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1073. FUSE_COMPAT_WRITE_IN_SIZE;
  1074. assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
  1075. } else {
  1076. fi.lock_owner = arg->lock_owner;
  1077. fi.flags = arg->flags;
  1078. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1079. bufv.buf[0].mem = PARAM(arg);
  1080. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1081. sizeof(struct fuse_write_in);
  1082. }
  1083. if (bufv.buf[0].size < arg->size) {
  1084. fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
  1085. fuse_reply_err(req, EIO);
  1086. goto out;
  1087. }
  1088. bufv.buf[0].size = arg->size;
  1089. req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
  1090. out:
  1091. /* Need to reset the pipe if ->write_buf() didn't consume all data */
  1092. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1093. fuse_ll_clear_pipe(f);
  1094. }
  1095. static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1096. {
  1097. struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
  1098. struct fuse_file_info fi;
  1099. memset(&fi, 0, sizeof(fi));
  1100. fi.fh = arg->fh;
  1101. fi.flush = 1;
  1102. if (req->f->conn.proto_minor >= 7)
  1103. fi.lock_owner = arg->lock_owner;
  1104. if (req->f->op.flush)
  1105. req->f->op.flush(req, nodeid, &fi);
  1106. else
  1107. fuse_reply_err(req, ENOSYS);
  1108. }
  1109. static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1110. {
  1111. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1112. struct fuse_file_info fi;
  1113. memset(&fi, 0, sizeof(fi));
  1114. fi.flags = arg->flags;
  1115. fi.fh = arg->fh;
  1116. if (req->f->conn.proto_minor >= 8) {
  1117. fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
  1118. fi.lock_owner = arg->lock_owner;
  1119. }
  1120. if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
  1121. fi.flock_release = 1;
  1122. fi.lock_owner = arg->lock_owner;
  1123. }
  1124. if (req->f->op.release)
  1125. req->f->op.release(req, nodeid, &fi);
  1126. else
  1127. fuse_reply_err(req, 0);
  1128. }
  1129. static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1130. {
  1131. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1132. struct fuse_file_info fi;
  1133. memset(&fi, 0, sizeof(fi));
  1134. fi.fh = arg->fh;
  1135. if (req->f->op.fsync)
  1136. req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
  1137. else
  1138. fuse_reply_err(req, ENOSYS);
  1139. }
  1140. static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1141. {
  1142. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  1143. struct fuse_file_info fi;
  1144. memset(&fi, 0, sizeof(fi));
  1145. fi.flags = arg->flags;
  1146. if (req->f->op.opendir)
  1147. req->f->op.opendir(req, nodeid, &fi);
  1148. else
  1149. fuse_reply_open(req, &fi);
  1150. }
  1151. static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1152. {
  1153. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1154. struct fuse_file_info fi;
  1155. memset(&fi, 0, sizeof(fi));
  1156. fi.fh = arg->fh;
  1157. if (req->f->op.readdir)
  1158. req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
  1159. else
  1160. fuse_reply_err(req, ENOSYS);
  1161. }
  1162. static
  1163. void
  1164. do_readdir_plus(fuse_req_t req_,
  1165. fuse_ino_t nodeid_,
  1166. const void *inarg_)
  1167. {
  1168. const struct fuse_read_in *arg;
  1169. struct fuse_file_info ffi = {0};
  1170. arg = (struct fuse_read_in*)inarg_;
  1171. ffi.fh = arg->fh;
  1172. if(req_->f->op.readdir_plus)
  1173. req_->f->op.readdir_plus(req_,nodeid_,arg->size,arg->offset,&ffi);
  1174. else
  1175. fuse_reply_err(req_,ENOSYS);
  1176. }
  1177. static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1178. {
  1179. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1180. struct fuse_file_info fi;
  1181. memset(&fi, 0, sizeof(fi));
  1182. fi.flags = arg->flags;
  1183. fi.fh = arg->fh;
  1184. if (req->f->op.releasedir)
  1185. req->f->op.releasedir(req, nodeid, &fi);
  1186. else
  1187. fuse_reply_err(req, 0);
  1188. }
  1189. static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1190. {
  1191. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1192. struct fuse_file_info fi;
  1193. memset(&fi, 0, sizeof(fi));
  1194. fi.fh = arg->fh;
  1195. if (req->f->op.fsyncdir)
  1196. req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
  1197. else
  1198. fuse_reply_err(req, ENOSYS);
  1199. }
  1200. static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1201. {
  1202. (void) nodeid;
  1203. (void) inarg;
  1204. if (req->f->op.statfs)
  1205. req->f->op.statfs(req, nodeid);
  1206. else {
  1207. struct statvfs buf = {
  1208. .f_namemax = 255,
  1209. .f_bsize = 512,
  1210. };
  1211. fuse_reply_statfs(req, &buf);
  1212. }
  1213. }
  1214. static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1215. {
  1216. struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
  1217. char *name = PARAM(arg);
  1218. char *value = name + strlen(name) + 1;
  1219. if (req->f->op.setxattr)
  1220. req->f->op.setxattr(req, nodeid, name, value, arg->size,
  1221. arg->flags);
  1222. else
  1223. fuse_reply_err(req, ENOSYS);
  1224. }
  1225. static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1226. {
  1227. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1228. if (req->f->op.getxattr)
  1229. req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
  1230. else
  1231. fuse_reply_err(req, ENOSYS);
  1232. }
  1233. static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1234. {
  1235. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1236. if (req->f->op.listxattr)
  1237. req->f->op.listxattr(req, nodeid, arg->size);
  1238. else
  1239. fuse_reply_err(req, ENOSYS);
  1240. }
  1241. static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1242. {
  1243. char *name = (char *) inarg;
  1244. if (req->f->op.removexattr)
  1245. req->f->op.removexattr(req, nodeid, name);
  1246. else
  1247. fuse_reply_err(req, ENOSYS);
  1248. }
  1249. static void convert_fuse_file_lock(struct fuse_file_lock *fl,
  1250. struct flock *flock)
  1251. {
  1252. memset(flock, 0, sizeof(struct flock));
  1253. flock->l_type = fl->type;
  1254. flock->l_whence = SEEK_SET;
  1255. flock->l_start = fl->start;
  1256. if (fl->end == OFFSET_MAX)
  1257. flock->l_len = 0;
  1258. else
  1259. flock->l_len = fl->end - fl->start + 1;
  1260. flock->l_pid = fl->pid;
  1261. }
  1262. static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1263. {
  1264. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1265. struct fuse_file_info fi;
  1266. struct flock flock;
  1267. memset(&fi, 0, sizeof(fi));
  1268. fi.fh = arg->fh;
  1269. fi.lock_owner = arg->owner;
  1270. convert_fuse_file_lock(&arg->lk, &flock);
  1271. if (req->f->op.getlk)
  1272. req->f->op.getlk(req, nodeid, &fi, &flock);
  1273. else
  1274. fuse_reply_err(req, ENOSYS);
  1275. }
  1276. static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
  1277. const void *inarg, int sleep)
  1278. {
  1279. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1280. struct fuse_file_info fi;
  1281. struct flock flock;
  1282. memset(&fi, 0, sizeof(fi));
  1283. fi.fh = arg->fh;
  1284. fi.lock_owner = arg->owner;
  1285. if (arg->lk_flags & FUSE_LK_FLOCK) {
  1286. int op = 0;
  1287. switch (arg->lk.type) {
  1288. case F_RDLCK:
  1289. op = LOCK_SH;
  1290. break;
  1291. case F_WRLCK:
  1292. op = LOCK_EX;
  1293. break;
  1294. case F_UNLCK:
  1295. op = LOCK_UN;
  1296. break;
  1297. }
  1298. if (!sleep)
  1299. op |= LOCK_NB;
  1300. if (req->f->op.flock)
  1301. req->f->op.flock(req, nodeid, &fi, op);
  1302. else
  1303. fuse_reply_err(req, ENOSYS);
  1304. } else {
  1305. convert_fuse_file_lock(&arg->lk, &flock);
  1306. if (req->f->op.setlk)
  1307. req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
  1308. else
  1309. fuse_reply_err(req, ENOSYS);
  1310. }
  1311. }
  1312. static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1313. {
  1314. do_setlk_common(req, nodeid, inarg, 0);
  1315. }
  1316. static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1317. {
  1318. do_setlk_common(req, nodeid, inarg, 1);
  1319. }
  1320. static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
  1321. {
  1322. struct fuse_req *curr;
  1323. for (curr = f->list.next; curr != &f->list; curr = curr->next) {
  1324. if (curr->unique == req->u.i.unique) {
  1325. fuse_interrupt_func_t func;
  1326. void *data;
  1327. curr->ctr++;
  1328. pthread_mutex_unlock(&f->lock);
  1329. /* Ugh, ugly locking */
  1330. pthread_mutex_lock(&curr->lock);
  1331. pthread_mutex_lock(&f->lock);
  1332. curr->interrupted = 1;
  1333. func = curr->u.ni.func;
  1334. data = curr->u.ni.data;
  1335. pthread_mutex_unlock(&f->lock);
  1336. if (func)
  1337. func(curr, data);
  1338. pthread_mutex_unlock(&curr->lock);
  1339. pthread_mutex_lock(&f->lock);
  1340. curr->ctr--;
  1341. if (!curr->ctr)
  1342. destroy_req(curr);
  1343. return 1;
  1344. }
  1345. }
  1346. for (curr = f->interrupts.next; curr != &f->interrupts;
  1347. curr = curr->next) {
  1348. if (curr->u.i.unique == req->u.i.unique)
  1349. return 1;
  1350. }
  1351. return 0;
  1352. }
  1353. static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1354. {
  1355. struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
  1356. struct fuse_ll *f = req->f;
  1357. (void) nodeid;
  1358. if (f->debug)
  1359. fprintf(stderr, "INTERRUPT: %llu\n",
  1360. (unsigned long long) arg->unique);
  1361. req->u.i.unique = arg->unique;
  1362. pthread_mutex_lock(&f->lock);
  1363. if (find_interrupted(f, req))
  1364. destroy_req(req);
  1365. else
  1366. list_add_req(req, &f->interrupts);
  1367. pthread_mutex_unlock(&f->lock);
  1368. }
  1369. static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
  1370. {
  1371. struct fuse_req *curr;
  1372. for (curr = f->interrupts.next; curr != &f->interrupts;
  1373. curr = curr->next) {
  1374. if (curr->u.i.unique == req->unique) {
  1375. req->interrupted = 1;
  1376. list_del_req(curr);
  1377. xmem_free(curr);
  1378. return NULL;
  1379. }
  1380. }
  1381. curr = f->interrupts.next;
  1382. if (curr != &f->interrupts) {
  1383. list_del_req(curr);
  1384. list_init_req(curr);
  1385. return curr;
  1386. } else
  1387. return NULL;
  1388. }
  1389. static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1390. {
  1391. struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
  1392. if (req->f->op.bmap)
  1393. req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
  1394. else
  1395. fuse_reply_err(req, ENOSYS);
  1396. }
  1397. static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1398. {
  1399. struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
  1400. unsigned int flags = arg->flags;
  1401. void *in_buf = arg->in_size ? PARAM(arg) : NULL;
  1402. struct fuse_file_info fi;
  1403. if (flags & FUSE_IOCTL_DIR &&
  1404. !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
  1405. fuse_reply_err(req, ENOTTY);
  1406. return;
  1407. }
  1408. memset(&fi, 0, sizeof(fi));
  1409. fi.fh = arg->fh;
  1410. if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
  1411. !(flags & FUSE_IOCTL_32BIT)) {
  1412. req->ioctl_64bit = 1;
  1413. }
  1414. if (req->f->op.ioctl)
  1415. req->f->op.ioctl(req, nodeid, (unsigned long)arg->cmd,
  1416. (void *)(uintptr_t)arg->arg, &fi, flags,
  1417. in_buf, arg->in_size, arg->out_size);
  1418. else
  1419. fuse_reply_err(req, ENOSYS);
  1420. }
  1421. void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
  1422. {
  1423. xmem_free(ph);
  1424. }
  1425. static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1426. {
  1427. struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
  1428. struct fuse_file_info fi;
  1429. memset(&fi, 0, sizeof(fi));
  1430. fi.fh = arg->fh;
  1431. if (req->f->op.poll) {
  1432. struct fuse_pollhandle *ph = NULL;
  1433. if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
  1434. ph = xmem_malloc(sizeof(struct fuse_pollhandle));
  1435. if (ph == NULL) {
  1436. fuse_reply_err(req, ENOMEM);
  1437. return;
  1438. }
  1439. ph->kh = arg->kh;
  1440. ph->ch = req->ch;
  1441. ph->f = req->f;
  1442. }
  1443. req->f->op.poll(req, nodeid, &fi, ph);
  1444. } else {
  1445. fuse_reply_err(req, ENOSYS);
  1446. }
  1447. }
  1448. static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1449. {
  1450. struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
  1451. struct fuse_file_info fi;
  1452. memset(&fi, 0, sizeof(fi));
  1453. fi.fh = arg->fh;
  1454. if (req->f->op.fallocate)
  1455. req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
  1456. else
  1457. fuse_reply_err(req, ENOSYS);
  1458. }
  1459. static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1460. {
  1461. struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
  1462. struct fuse_init_out outarg;
  1463. struct fuse_ll *f = req->f;
  1464. size_t bufsize = fuse_chan_bufsize(req->ch);
  1465. (void) nodeid;
  1466. if (f->debug) {
  1467. fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
  1468. if (arg->major == 7 && arg->minor >= 6) {
  1469. fprintf(stderr, "flags=0x%08x\n", arg->flags);
  1470. fprintf(stderr, "max_readahead=0x%08x\n",
  1471. arg->max_readahead);
  1472. }
  1473. }
  1474. f->conn.proto_major = arg->major;
  1475. f->conn.proto_minor = arg->minor;
  1476. f->conn.capable = 0;
  1477. f->conn.want = 0;
  1478. memset(&outarg, 0, sizeof(outarg));
  1479. outarg.major = FUSE_KERNEL_VERSION;
  1480. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  1481. outarg.max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  1482. if (arg->major < 7) {
  1483. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  1484. arg->major, arg->minor);
  1485. fuse_reply_err(req, EPROTO);
  1486. return;
  1487. }
  1488. if (arg->major > 7) {
  1489. /* Wait for a second INIT request with a 7.X version */
  1490. send_reply_ok(req, &outarg, sizeof(outarg));
  1491. return;
  1492. }
  1493. if (arg->minor >= 6) {
  1494. if (arg->max_readahead < f->conn.max_readahead)
  1495. f->conn.max_readahead = arg->max_readahead;
  1496. if (arg->flags & FUSE_ASYNC_READ)
  1497. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  1498. if (arg->flags & FUSE_POSIX_LOCKS)
  1499. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  1500. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  1501. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  1502. if (arg->flags & FUSE_EXPORT_SUPPORT)
  1503. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  1504. if (arg->flags & FUSE_BIG_WRITES)
  1505. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  1506. if (arg->flags & FUSE_DONT_MASK)
  1507. f->conn.capable |= FUSE_CAP_DONT_MASK;
  1508. if (arg->flags & FUSE_FLOCK_LOCKS)
  1509. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  1510. if (arg->flags & FUSE_POSIX_ACL)
  1511. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  1512. if (arg->flags & FUSE_CACHE_SYMLINKS)
  1513. f->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
  1514. if (arg->flags & FUSE_ASYNC_DIO)
  1515. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  1516. if (arg->flags & FUSE_PARALLEL_DIROPS)
  1517. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  1518. if (arg->flags & FUSE_MAX_PAGES)
  1519. f->conn.capable |= FUSE_CAP_MAX_PAGES;
  1520. if (arg->flags & FUSE_WRITEBACK_CACHE)
  1521. f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
  1522. if (arg->flags & FUSE_DO_READDIRPLUS)
  1523. f->conn.capable |= FUSE_CAP_READDIR_PLUS;
  1524. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  1525. f->conn.capable |= FUSE_CAP_READDIR_PLUS_AUTO;
  1526. } else {
  1527. f->conn.want &= ~FUSE_CAP_ASYNC_READ;
  1528. f->conn.max_readahead = 0;
  1529. }
  1530. if (req->f->conn.proto_minor >= 14) {
  1531. #ifdef HAVE_SPLICE
  1532. #ifdef HAVE_VMSPLICE
  1533. f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
  1534. if (f->splice_write)
  1535. f->conn.want |= FUSE_CAP_SPLICE_WRITE;
  1536. if (f->splice_move)
  1537. f->conn.want |= FUSE_CAP_SPLICE_MOVE;
  1538. #endif
  1539. f->conn.capable |= FUSE_CAP_SPLICE_READ;
  1540. if (f->splice_read)
  1541. f->conn.want |= FUSE_CAP_SPLICE_READ;
  1542. #endif
  1543. }
  1544. if (req->f->conn.proto_minor >= 18)
  1545. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1546. if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1547. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1548. if (f->op.flock && !f->no_remote_flock)
  1549. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1550. if (bufsize < FUSE_MIN_READ_BUFFER) {
  1551. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1552. bufsize);
  1553. bufsize = FUSE_MIN_READ_BUFFER;
  1554. }
  1555. bufsize -= 4096;
  1556. if (bufsize < f->conn.max_write)
  1557. f->conn.max_write = bufsize;
  1558. f->got_init = 1;
  1559. if (f->op.init)
  1560. f->op.init(f->userdata, &f->conn);
  1561. if (f->no_splice_read)
  1562. f->conn.want &= ~FUSE_CAP_SPLICE_READ;
  1563. if (f->no_splice_write)
  1564. f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
  1565. if (f->no_splice_move)
  1566. f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
  1567. if ((arg->flags & FUSE_MAX_PAGES) && (f->conn.want & FUSE_CAP_MAX_PAGES))
  1568. {
  1569. outarg.flags |= FUSE_MAX_PAGES;
  1570. outarg.max_pages = f->conn.max_pages;
  1571. }
  1572. if (f->conn.want & FUSE_CAP_ASYNC_READ)
  1573. outarg.flags |= FUSE_ASYNC_READ;
  1574. if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1575. outarg.flags |= FUSE_POSIX_LOCKS;
  1576. if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1577. outarg.flags |= FUSE_ATOMIC_O_TRUNC;
  1578. if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1579. outarg.flags |= FUSE_EXPORT_SUPPORT;
  1580. if (f->conn.want & FUSE_CAP_BIG_WRITES)
  1581. outarg.flags |= FUSE_BIG_WRITES;
  1582. if (f->conn.want & FUSE_CAP_DONT_MASK)
  1583. outarg.flags |= FUSE_DONT_MASK;
  1584. if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1585. outarg.flags |= FUSE_FLOCK_LOCKS;
  1586. if (f->conn.want & FUSE_CAP_POSIX_ACL)
  1587. outarg.flags |= FUSE_POSIX_ACL;
  1588. if (f->conn.want & FUSE_CAP_CACHE_SYMLINKS)
  1589. outarg.flags |= FUSE_CACHE_SYMLINKS;
  1590. if (f->conn.want & FUSE_CAP_ASYNC_DIO)
  1591. outarg.flags |= FUSE_ASYNC_DIO;
  1592. if (f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1593. outarg.flags |= FUSE_PARALLEL_DIROPS;
  1594. if (f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
  1595. outarg.flags |= FUSE_WRITEBACK_CACHE;
  1596. if (f->conn.want & FUSE_CAP_READDIR_PLUS)
  1597. outarg.flags |= FUSE_DO_READDIRPLUS;
  1598. if (f->conn.want & FUSE_CAP_READDIR_PLUS_AUTO)
  1599. outarg.flags |= FUSE_READDIRPLUS_AUTO;
  1600. outarg.max_readahead = f->conn.max_readahead;
  1601. outarg.max_write = f->conn.max_write;
  1602. if (f->conn.proto_minor >= 13) {
  1603. if (f->conn.max_background >= (1 << 16))
  1604. f->conn.max_background = (1 << 16) - 1;
  1605. if (f->conn.congestion_threshold > f->conn.max_background)
  1606. f->conn.congestion_threshold = f->conn.max_background;
  1607. if (!f->conn.congestion_threshold) {
  1608. f->conn.congestion_threshold =
  1609. f->conn.max_background * 3 / 4;
  1610. }
  1611. outarg.max_background = f->conn.max_background;
  1612. outarg.congestion_threshold = f->conn.congestion_threshold;
  1613. }
  1614. if (f->debug) {
  1615. fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
  1616. fprintf(stderr, " flags=0x%08x\n", outarg.flags);
  1617. fprintf(stderr, " max_readahead=0x%08x\n",
  1618. outarg.max_readahead);
  1619. fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
  1620. fprintf(stderr, " max_background=%i\n",
  1621. outarg.max_background);
  1622. fprintf(stderr, " congestion_threshold=%i\n",
  1623. outarg.congestion_threshold);
  1624. fprintf(stderr, " max_pages=%d\n",outarg.max_pages);
  1625. }
  1626. size_t outargsize;
  1627. if(arg->minor < 5)
  1628. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1629. else if(arg->minor < 23)
  1630. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1631. else
  1632. outargsize = sizeof(outarg);
  1633. send_reply_ok(req, &outarg, outargsize);
  1634. }
  1635. static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1636. {
  1637. struct fuse_ll *f = req->f;
  1638. (void) nodeid;
  1639. (void) inarg;
  1640. f->got_destroy = 1;
  1641. if (f->op.destroy)
  1642. f->op.destroy(f->userdata);
  1643. send_reply_ok(req, NULL, 0);
  1644. }
  1645. static void list_del_nreq(struct fuse_notify_req *nreq)
  1646. {
  1647. struct fuse_notify_req *prev = nreq->prev;
  1648. struct fuse_notify_req *next = nreq->next;
  1649. prev->next = next;
  1650. next->prev = prev;
  1651. }
  1652. static void list_add_nreq(struct fuse_notify_req *nreq,
  1653. struct fuse_notify_req *next)
  1654. {
  1655. struct fuse_notify_req *prev = next->prev;
  1656. nreq->next = next;
  1657. nreq->prev = prev;
  1658. prev->next = nreq;
  1659. next->prev = nreq;
  1660. }
  1661. static void list_init_nreq(struct fuse_notify_req *nreq)
  1662. {
  1663. nreq->next = nreq;
  1664. nreq->prev = nreq;
  1665. }
  1666. static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
  1667. const void *inarg, const struct fuse_buf *buf)
  1668. {
  1669. struct fuse_ll *f = req->f;
  1670. struct fuse_notify_req *nreq;
  1671. struct fuse_notify_req *head;
  1672. pthread_mutex_lock(&f->lock);
  1673. head = &f->notify_list;
  1674. for (nreq = head->next; nreq != head; nreq = nreq->next) {
  1675. if (nreq->unique == req->unique) {
  1676. list_del_nreq(nreq);
  1677. break;
  1678. }
  1679. }
  1680. pthread_mutex_unlock(&f->lock);
  1681. if (nreq != head)
  1682. nreq->reply(nreq, req, nodeid, inarg, buf);
  1683. }
  1684. static
  1685. void
  1686. do_copy_file_range(fuse_req_t req_,
  1687. fuse_ino_t nodeid_in_,
  1688. const void *arg_)
  1689. {
  1690. struct fuse_file_info ffi_in = {0};
  1691. struct fuse_file_info ffi_out = {0};
  1692. struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in*)arg_;
  1693. ffi_in.fh = arg->fh_in;
  1694. ffi_out.fh = arg->fh_out;
  1695. if(req_->f->op.copy_file_range == NULL)
  1696. fuse_reply_err(req_,ENOSYS);
  1697. else
  1698. req_->f->op.copy_file_range(req_,
  1699. nodeid_in_,
  1700. arg->off_in,
  1701. &ffi_in,
  1702. arg->nodeid_out,
  1703. arg->off_out,
  1704. &ffi_out,
  1705. arg->len,
  1706. arg->flags);
  1707. }
  1708. static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
  1709. int notify_code, struct iovec *iov, int count)
  1710. {
  1711. struct fuse_out_header out;
  1712. if (!f->got_init)
  1713. return -ENOTCONN;
  1714. out.unique = 0;
  1715. out.error = notify_code;
  1716. iov[0].iov_base = &out;
  1717. iov[0].iov_len = sizeof(struct fuse_out_header);
  1718. return fuse_send_msg(f, ch, iov, count);
  1719. }
  1720. int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
  1721. {
  1722. if (ph != NULL) {
  1723. struct fuse_notify_poll_wakeup_out outarg;
  1724. struct iovec iov[2];
  1725. outarg.kh = ph->kh;
  1726. iov[1].iov_base = &outarg;
  1727. iov[1].iov_len = sizeof(outarg);
  1728. return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
  1729. } else {
  1730. return 0;
  1731. }
  1732. }
  1733. int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
  1734. off_t off, off_t len)
  1735. {
  1736. struct fuse_notify_inval_inode_out outarg;
  1737. struct fuse_ll *f;
  1738. struct iovec iov[2];
  1739. if (!ch)
  1740. return -EINVAL;
  1741. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1742. if (!f)
  1743. return -ENODEV;
  1744. outarg.ino = ino;
  1745. outarg.off = off;
  1746. outarg.len = len;
  1747. iov[1].iov_base = &outarg;
  1748. iov[1].iov_len = sizeof(outarg);
  1749. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
  1750. }
  1751. int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
  1752. const char *name, size_t namelen)
  1753. {
  1754. struct fuse_notify_inval_entry_out outarg;
  1755. struct fuse_ll *f;
  1756. struct iovec iov[3];
  1757. if (!ch)
  1758. return -EINVAL;
  1759. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1760. if (!f)
  1761. return -ENODEV;
  1762. outarg.parent = parent;
  1763. outarg.namelen = namelen;
  1764. outarg.padding = 0;
  1765. iov[1].iov_base = &outarg;
  1766. iov[1].iov_len = sizeof(outarg);
  1767. iov[2].iov_base = (void *)name;
  1768. iov[2].iov_len = namelen + 1;
  1769. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
  1770. }
  1771. int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  1772. fuse_ino_t parent, fuse_ino_t child,
  1773. const char *name, size_t namelen)
  1774. {
  1775. struct fuse_notify_delete_out outarg;
  1776. struct fuse_ll *f;
  1777. struct iovec iov[3];
  1778. if (!ch)
  1779. return -EINVAL;
  1780. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1781. if (!f)
  1782. return -ENODEV;
  1783. if (f->conn.proto_minor < 18)
  1784. return -ENOSYS;
  1785. outarg.parent = parent;
  1786. outarg.child = child;
  1787. outarg.namelen = namelen;
  1788. outarg.padding = 0;
  1789. iov[1].iov_base = &outarg;
  1790. iov[1].iov_len = sizeof(outarg);
  1791. iov[2].iov_base = (void *)name;
  1792. iov[2].iov_len = namelen + 1;
  1793. return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
  1794. }
  1795. int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
  1796. off_t offset, struct fuse_bufvec *bufv,
  1797. enum fuse_buf_copy_flags flags)
  1798. {
  1799. struct fuse_out_header out;
  1800. struct fuse_notify_store_out outarg;
  1801. struct fuse_ll *f;
  1802. struct iovec iov[3];
  1803. size_t size = fuse_buf_size(bufv);
  1804. int res;
  1805. if (!ch)
  1806. return -EINVAL;
  1807. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1808. if (!f)
  1809. return -ENODEV;
  1810. if (f->conn.proto_minor < 15)
  1811. return -ENOSYS;
  1812. out.unique = 0;
  1813. out.error = FUSE_NOTIFY_STORE;
  1814. outarg.nodeid = ino;
  1815. outarg.offset = offset;
  1816. outarg.size = size;
  1817. outarg.padding = 0;
  1818. iov[0].iov_base = &out;
  1819. iov[0].iov_len = sizeof(out);
  1820. iov[1].iov_base = &outarg;
  1821. iov[1].iov_len = sizeof(outarg);
  1822. res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
  1823. if (res > 0)
  1824. res = -res;
  1825. return res;
  1826. }
  1827. struct fuse_retrieve_req {
  1828. struct fuse_notify_req nreq;
  1829. void *cookie;
  1830. };
  1831. static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
  1832. fuse_req_t req, fuse_ino_t ino,
  1833. const void *inarg,
  1834. const struct fuse_buf *ibuf)
  1835. {
  1836. struct fuse_ll *f = req->f;
  1837. struct fuse_retrieve_req *rreq =
  1838. container_of(nreq, struct fuse_retrieve_req, nreq);
  1839. const struct fuse_notify_retrieve_in *arg = inarg;
  1840. struct fuse_bufvec bufv = {
  1841. .buf[0] = *ibuf,
  1842. .count = 1,
  1843. };
  1844. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1845. bufv.buf[0].mem = PARAM(arg);
  1846. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1847. sizeof(struct fuse_notify_retrieve_in);
  1848. if (bufv.buf[0].size < arg->size) {
  1849. fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
  1850. fuse_reply_none(req);
  1851. goto out;
  1852. }
  1853. bufv.buf[0].size = arg->size;
  1854. if (req->f->op.retrieve_reply) {
  1855. req->f->op.retrieve_reply(req, rreq->cookie, ino,
  1856. arg->offset, &bufv);
  1857. } else {
  1858. fuse_reply_none(req);
  1859. }
  1860. out:
  1861. xmem_free(rreq);
  1862. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1863. fuse_ll_clear_pipe(f);
  1864. }
  1865. int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
  1866. size_t size, off_t offset, void *cookie)
  1867. {
  1868. struct fuse_notify_retrieve_out outarg;
  1869. struct fuse_ll *f;
  1870. struct iovec iov[2];
  1871. struct fuse_retrieve_req *rreq;
  1872. int err;
  1873. if (!ch)
  1874. return -EINVAL;
  1875. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1876. if (!f)
  1877. return -ENODEV;
  1878. if (f->conn.proto_minor < 15)
  1879. return -ENOSYS;
  1880. rreq = xmem_malloc(sizeof(*rreq));
  1881. if (rreq == NULL)
  1882. return -ENOMEM;
  1883. pthread_mutex_lock(&f->lock);
  1884. rreq->cookie = cookie;
  1885. rreq->nreq.unique = f->notify_ctr++;
  1886. rreq->nreq.reply = fuse_ll_retrieve_reply;
  1887. list_add_nreq(&rreq->nreq, &f->notify_list);
  1888. pthread_mutex_unlock(&f->lock);
  1889. outarg.notify_unique = rreq->nreq.unique;
  1890. outarg.nodeid = ino;
  1891. outarg.offset = offset;
  1892. outarg.size = size;
  1893. iov[1].iov_base = &outarg;
  1894. iov[1].iov_len = sizeof(outarg);
  1895. err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
  1896. if (err) {
  1897. pthread_mutex_lock(&f->lock);
  1898. list_del_nreq(&rreq->nreq);
  1899. pthread_mutex_unlock(&f->lock);
  1900. xmem_free(rreq);
  1901. }
  1902. return err;
  1903. }
  1904. void *fuse_req_userdata(fuse_req_t req)
  1905. {
  1906. return req->f->userdata;
  1907. }
  1908. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
  1909. {
  1910. return &req->ctx;
  1911. }
  1912. void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
  1913. void *data)
  1914. {
  1915. pthread_mutex_lock(&req->lock);
  1916. pthread_mutex_lock(&req->f->lock);
  1917. req->u.ni.func = func;
  1918. req->u.ni.data = data;
  1919. pthread_mutex_unlock(&req->f->lock);
  1920. if (req->interrupted && func)
  1921. func(req, data);
  1922. pthread_mutex_unlock(&req->lock);
  1923. }
  1924. int fuse_req_interrupted(fuse_req_t req)
  1925. {
  1926. int interrupted;
  1927. pthread_mutex_lock(&req->f->lock);
  1928. interrupted = req->interrupted;
  1929. pthread_mutex_unlock(&req->f->lock);
  1930. return interrupted;
  1931. }
  1932. static struct {
  1933. void (*func)(fuse_req_t, fuse_ino_t, const void *);
  1934. const char *name;
  1935. } fuse_ll_ops[] =
  1936. {
  1937. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1938. [FUSE_FORGET] = { do_forget, "FORGET" },
  1939. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1940. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1941. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1942. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1943. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1944. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1945. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1946. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1947. [FUSE_RENAME] = { do_rename, "RENAME" },
  1948. [FUSE_LINK] = { do_link, "LINK" },
  1949. [FUSE_OPEN] = { do_open, "OPEN" },
  1950. [FUSE_READ] = { do_read, "READ" },
  1951. [FUSE_WRITE] = { do_write, "WRITE" },
  1952. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1953. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1954. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1955. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1956. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1957. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1958. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1959. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1960. [FUSE_INIT] = { do_init, "INIT" },
  1961. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1962. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1963. [FUSE_READDIRPLUS] = { do_readdir_plus, "READDIR_PLUS" },
  1964. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1965. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1966. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1967. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1968. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1969. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1970. [FUSE_CREATE] = { do_create, "CREATE" },
  1971. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  1972. [FUSE_BMAP] = { do_bmap, "BMAP" },
  1973. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  1974. [FUSE_POLL] = { do_poll, "POLL" },
  1975. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  1976. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  1977. [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
  1978. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  1979. [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
  1980. };
  1981. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  1982. static const char *opname(enum fuse_opcode opcode)
  1983. {
  1984. if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
  1985. return "???";
  1986. else
  1987. return fuse_ll_ops[opcode].name;
  1988. }
  1989. static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
  1990. struct fuse_bufvec *src)
  1991. {
  1992. int res = fuse_buf_copy(dst, src, 0);
  1993. if (res < 0) {
  1994. fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
  1995. return res;
  1996. }
  1997. if (res < fuse_buf_size(dst)) {
  1998. fprintf(stderr, "fuse: copy from pipe: short read\n");
  1999. return -1;
  2000. }
  2001. return 0;
  2002. }
  2003. static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
  2004. struct fuse_chan *ch)
  2005. {
  2006. struct fuse_ll *f = (struct fuse_ll *) data;
  2007. const size_t write_header_size = sizeof(struct fuse_in_header) +
  2008. sizeof(struct fuse_write_in);
  2009. struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
  2010. struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
  2011. struct fuse_in_header *in;
  2012. const void *inarg;
  2013. struct fuse_req *req;
  2014. void *mbuf = NULL;
  2015. int err;
  2016. int res;
  2017. if (buf->flags & FUSE_BUF_IS_FD) {
  2018. if (buf->size < tmpbuf.buf[0].size)
  2019. tmpbuf.buf[0].size = buf->size;
  2020. mbuf = xmem_malloc(tmpbuf.buf[0].size);
  2021. if (mbuf == NULL) {
  2022. fprintf(stderr, "fuse: failed to allocate header\n");
  2023. goto clear_pipe;
  2024. }
  2025. tmpbuf.buf[0].mem = mbuf;
  2026. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  2027. if (res < 0)
  2028. goto clear_pipe;
  2029. in = mbuf;
  2030. } else {
  2031. in = buf->mem;
  2032. }
  2033. if (f->debug) {
  2034. fprintf(stderr,
  2035. "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
  2036. (unsigned long long) in->unique,
  2037. opname((enum fuse_opcode) in->opcode), in->opcode,
  2038. (unsigned long) in->nodeid, buf->size, in->pid);
  2039. }
  2040. req = fuse_ll_alloc_req(f);
  2041. if (req == NULL) {
  2042. struct fuse_out_header out = {
  2043. .unique = in->unique,
  2044. .error = -ENOMEM,
  2045. };
  2046. struct iovec iov = {
  2047. .iov_base = &out,
  2048. .iov_len = sizeof(struct fuse_out_header),
  2049. };
  2050. fuse_send_msg(f, ch, &iov, 1);
  2051. goto clear_pipe;
  2052. }
  2053. req->unique = in->unique;
  2054. req->ctx.uid = in->uid;
  2055. req->ctx.gid = in->gid;
  2056. req->ctx.pid = in->pid;
  2057. req->ch = ch;
  2058. err = EIO;
  2059. if(!f->got_init)
  2060. {
  2061. enum fuse_opcode expected;
  2062. expected = FUSE_INIT;
  2063. if (in->opcode != expected)
  2064. goto reply_err;
  2065. }
  2066. else if(in->opcode == FUSE_INIT)
  2067. goto reply_err;
  2068. err = EACCES;
  2069. if (f->allow_root &&
  2070. in->uid != f->owner &&
  2071. in->uid != 0 &&
  2072. in->opcode != FUSE_INIT &&
  2073. in->opcode != FUSE_READ &&
  2074. in->opcode != FUSE_WRITE &&
  2075. in->opcode != FUSE_FSYNC &&
  2076. in->opcode != FUSE_RELEASE &&
  2077. in->opcode != FUSE_READDIR &&
  2078. in->opcode != FUSE_READDIRPLUS &&
  2079. in->opcode != FUSE_FSYNCDIR &&
  2080. in->opcode != FUSE_RELEASEDIR &&
  2081. in->opcode != FUSE_NOTIFY_REPLY)
  2082. goto reply_err;
  2083. err = ENOSYS;
  2084. if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
  2085. goto reply_err;
  2086. if (in->opcode != FUSE_INTERRUPT) {
  2087. struct fuse_req *intr;
  2088. pthread_mutex_lock(&f->lock);
  2089. intr = check_interrupt(f, req);
  2090. list_add_req(req, &f->list);
  2091. pthread_mutex_unlock(&f->lock);
  2092. if (intr)
  2093. fuse_reply_err(intr, EAGAIN);
  2094. }
  2095. if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
  2096. (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
  2097. in->opcode != FUSE_NOTIFY_REPLY) {
  2098. void *newmbuf;
  2099. err = ENOMEM;
  2100. newmbuf = xmem_realloc(mbuf, buf->size);
  2101. if (newmbuf == NULL)
  2102. goto reply_err;
  2103. mbuf = newmbuf;
  2104. tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
  2105. tmpbuf.buf[0].mem = mbuf + write_header_size;
  2106. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  2107. err = -res;
  2108. if (res < 0)
  2109. goto reply_err;
  2110. in = mbuf;
  2111. }
  2112. inarg = (void *) &in[1];
  2113. if (in->opcode == FUSE_WRITE && f->op.write_buf)
  2114. do_write_buf(req, in->nodeid, inarg, buf);
  2115. else if (in->opcode == FUSE_NOTIFY_REPLY)
  2116. do_notify_reply(req, in->nodeid, inarg, buf);
  2117. else
  2118. fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
  2119. out_free:
  2120. xmem_free(mbuf);
  2121. return;
  2122. reply_err:
  2123. fuse_reply_err(req, err);
  2124. clear_pipe:
  2125. if (buf->flags & FUSE_BUF_IS_FD)
  2126. fuse_ll_clear_pipe(f);
  2127. goto out_free;
  2128. }
  2129. static void fuse_ll_process(void *data, const char *buf, size_t len,
  2130. struct fuse_chan *ch)
  2131. {
  2132. struct fuse_buf fbuf = {
  2133. .mem = (void *) buf,
  2134. .size = len,
  2135. };
  2136. fuse_ll_process_buf(data, &fbuf, ch);
  2137. }
  2138. enum {
  2139. KEY_HELP,
  2140. KEY_VERSION,
  2141. };
  2142. static const struct fuse_opt fuse_ll_opts[] = {
  2143. { "debug", offsetof(struct fuse_ll, debug), 1 },
  2144. { "-d", offsetof(struct fuse_ll, debug), 1 },
  2145. { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
  2146. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  2147. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  2148. { "congestion_threshold=%u",
  2149. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  2150. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2151. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2152. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2153. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2154. { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
  2155. { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
  2156. { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
  2157. { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
  2158. { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
  2159. { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
  2160. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  2161. FUSE_OPT_KEY("-h", KEY_HELP),
  2162. FUSE_OPT_KEY("--help", KEY_HELP),
  2163. FUSE_OPT_KEY("-V", KEY_VERSION),
  2164. FUSE_OPT_KEY("--version", KEY_VERSION),
  2165. FUSE_OPT_END
  2166. };
  2167. static void fuse_ll_version(void)
  2168. {
  2169. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  2170. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  2171. }
  2172. static void fuse_ll_help(void)
  2173. {
  2174. fprintf(stderr,
  2175. " -o max_readahead=N set maximum readahead\n"
  2176. " -o max_background=N set number of maximum background requests\n"
  2177. " -o congestion_threshold=N set kernel's congestion threshold\n"
  2178. " -o no_remote_lock disable remote file locking\n"
  2179. " -o no_remote_flock disable remote file locking (BSD)\n"
  2180. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  2181. " -o [no_]splice_write use splice to write to the fuse device\n"
  2182. " -o [no_]splice_move move data while splicing to the fuse device\n"
  2183. " -o [no_]splice_read use splice to read from the fuse device\n"
  2184. );
  2185. }
  2186. static int fuse_ll_opt_proc(void *data, const char *arg, int key,
  2187. struct fuse_args *outargs)
  2188. {
  2189. (void) data; (void) outargs;
  2190. switch (key) {
  2191. case KEY_HELP:
  2192. fuse_ll_help();
  2193. break;
  2194. case KEY_VERSION:
  2195. fuse_ll_version();
  2196. break;
  2197. default:
  2198. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  2199. }
  2200. return -1;
  2201. }
  2202. int fuse_lowlevel_is_lib_option(const char *opt)
  2203. {
  2204. return fuse_opt_match(fuse_ll_opts, opt);
  2205. }
  2206. static void fuse_ll_destroy(void *data)
  2207. {
  2208. struct fuse_ll *f = (struct fuse_ll *) data;
  2209. struct fuse_ll_pipe *llp;
  2210. if (f->got_init && !f->got_destroy) {
  2211. if (f->op.destroy)
  2212. f->op.destroy(f->userdata);
  2213. }
  2214. llp = pthread_getspecific(f->pipe_key);
  2215. if (llp != NULL)
  2216. fuse_ll_pipe_free(llp);
  2217. pthread_key_delete(f->pipe_key);
  2218. pthread_mutex_destroy(&f->lock);
  2219. xmem_free(f);
  2220. }
  2221. static void fuse_ll_pipe_destructor(void *data)
  2222. {
  2223. struct fuse_ll_pipe *llp = data;
  2224. fuse_ll_pipe_free(llp);
  2225. }
  2226. #ifdef HAVE_SPLICE
  2227. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2228. struct fuse_chan **chp)
  2229. {
  2230. struct fuse_chan *ch = *chp;
  2231. struct fuse_ll *f = fuse_session_data(se);
  2232. size_t bufsize = buf->size;
  2233. struct fuse_ll_pipe *llp;
  2234. struct fuse_buf tmpbuf;
  2235. int err;
  2236. int res;
  2237. if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
  2238. goto fallback;
  2239. llp = fuse_ll_get_pipe(f);
  2240. if (llp == NULL)
  2241. goto fallback;
  2242. if (llp->size < bufsize) {
  2243. if (llp->can_grow) {
  2244. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
  2245. if (res == -1) {
  2246. llp->can_grow = 0;
  2247. goto fallback;
  2248. }
  2249. llp->size = res;
  2250. }
  2251. if (llp->size < bufsize)
  2252. goto fallback;
  2253. }
  2254. res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
  2255. err = errno;
  2256. if (fuse_session_exited(se))
  2257. return 0;
  2258. if (res == -1) {
  2259. if (err == ENODEV) {
  2260. fuse_session_exit(se);
  2261. return 0;
  2262. }
  2263. if (err != EINTR && err != EAGAIN)
  2264. perror("fuse: splice from device");
  2265. return -err;
  2266. }
  2267. if (res < sizeof(struct fuse_in_header)) {
  2268. fprintf(stderr, "short splice from fuse device\n");
  2269. return -EIO;
  2270. }
  2271. tmpbuf = (struct fuse_buf) {
  2272. .size = res,
  2273. .flags = FUSE_BUF_IS_FD,
  2274. .fd = llp->pipe[0],
  2275. };
  2276. /*
  2277. * Don't bother with zero copy for small requests.
  2278. * fuse_loop_mt() needs to check for FORGET so this more than
  2279. * just an optimization.
  2280. */
  2281. if (res < sizeof(struct fuse_in_header) +
  2282. sizeof(struct fuse_write_in) + pagesize) {
  2283. struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
  2284. struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
  2285. res = fuse_buf_copy(&dst, &src, 0);
  2286. if (res < 0) {
  2287. fprintf(stderr, "fuse: copy from pipe: %s\n",
  2288. strerror(-res));
  2289. fuse_ll_clear_pipe(f);
  2290. return res;
  2291. }
  2292. if (res < tmpbuf.size) {
  2293. fprintf(stderr, "fuse: copy from pipe: short read\n");
  2294. fuse_ll_clear_pipe(f);
  2295. return -EIO;
  2296. }
  2297. buf->size = tmpbuf.size;
  2298. return buf->size;
  2299. }
  2300. *buf = tmpbuf;
  2301. return res;
  2302. fallback:
  2303. res = fuse_chan_recv(chp, buf->mem, bufsize);
  2304. if (res <= 0)
  2305. return res;
  2306. buf->size = res;
  2307. return res;
  2308. }
  2309. #else
  2310. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2311. struct fuse_chan **chp)
  2312. {
  2313. (void) se;
  2314. int res = fuse_chan_recv(chp, buf->mem, buf->size);
  2315. if (res <= 0)
  2316. return res;
  2317. buf->size = res;
  2318. return res;
  2319. }
  2320. #endif
  2321. /*
  2322. * always call fuse_lowlevel_new_common() internally, to work around a
  2323. * misfeature in the FreeBSD runtime linker, which links the old
  2324. * version of a symbol to internal references.
  2325. */
  2326. struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
  2327. const struct fuse_lowlevel_ops *op,
  2328. size_t op_size, void *userdata)
  2329. {
  2330. int err;
  2331. struct fuse_ll *f;
  2332. struct fuse_session *se;
  2333. struct fuse_session_ops sop = {
  2334. .process = fuse_ll_process,
  2335. .destroy = fuse_ll_destroy,
  2336. };
  2337. if (sizeof(struct fuse_lowlevel_ops) < op_size) {
  2338. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  2339. op_size = sizeof(struct fuse_lowlevel_ops);
  2340. }
  2341. f = (struct fuse_ll *)xmem_calloc(1, sizeof(struct fuse_ll));
  2342. if (f == NULL) {
  2343. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  2344. goto out;
  2345. }
  2346. f->conn.max_write = UINT_MAX;
  2347. f->conn.max_readahead = UINT_MAX;
  2348. list_init_req(&f->list);
  2349. list_init_req(&f->interrupts);
  2350. list_init_nreq(&f->notify_list);
  2351. f->notify_ctr = 1;
  2352. fuse_mutex_init(&f->lock);
  2353. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  2354. if (err) {
  2355. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  2356. strerror(err));
  2357. goto out_free;
  2358. }
  2359. if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  2360. goto out_key_destroy;
  2361. if (f->debug)
  2362. fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
  2363. memcpy(&f->op, op, op_size);
  2364. f->owner = getuid();
  2365. f->userdata = userdata;
  2366. se = fuse_session_new(&sop, f);
  2367. if (!se)
  2368. goto out_key_destroy;
  2369. se->receive_buf = fuse_ll_receive_buf;
  2370. se->process_buf = fuse_ll_process_buf;
  2371. return se;
  2372. out_key_destroy:
  2373. pthread_key_delete(f->pipe_key);
  2374. out_free:
  2375. pthread_mutex_destroy(&f->lock);
  2376. xmem_free(f);
  2377. out:
  2378. return NULL;
  2379. }
  2380. struct fuse_session*
  2381. fuse_lowlevel_new(struct fuse_args *args,
  2382. const struct fuse_lowlevel_ops *op,
  2383. size_t op_size,
  2384. void *userdata)
  2385. {
  2386. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  2387. }