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

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