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.

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