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.

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