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.

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