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.

2994 lines
72 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU LGPLv2.
  5. See the file COPYING.LIB
  6. */
  7. #define _GNU_SOURCE
  8. #include "config.h"
  9. #include "fuse_i.h"
  10. #include "fuse_kernel.h"
  11. #include "fuse_opt.h"
  12. #include "fuse_misc.h"
  13. #include "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, uint32_t size)
  775. {
  776. int count;
  777. struct iovec iov[3];
  778. struct fuse_ioctl_out arg;
  779. arg.result = result;
  780. arg.flags = 0;
  781. arg.in_iovs = 0;
  782. arg.out_iovs = 0;
  783. count = 1;
  784. iov[count].iov_base = &arg;
  785. iov[count].iov_len = sizeof(arg);
  786. count++;
  787. if(size)
  788. {
  789. iov[count].iov_base = (char*)buf;
  790. iov[count].iov_len = size;
  791. count++;
  792. }
  793. return send_reply_iov(req, 0, iov, count);
  794. }
  795. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  796. int count)
  797. {
  798. struct iovec *padded_iov;
  799. struct fuse_ioctl_out arg;
  800. int res;
  801. padded_iov = malloc((count + 2) * sizeof(struct iovec));
  802. if (padded_iov == NULL)
  803. return fuse_reply_err(req, ENOMEM);
  804. memset(&arg, 0, sizeof(arg));
  805. arg.result = result;
  806. padded_iov[1].iov_base = &arg;
  807. padded_iov[1].iov_len = sizeof(arg);
  808. memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
  809. res = send_reply_iov(req, 0, padded_iov, count + 2);
  810. free(padded_iov);
  811. return res;
  812. }
  813. int fuse_reply_poll(fuse_req_t req, unsigned revents)
  814. {
  815. struct fuse_poll_out arg;
  816. memset(&arg, 0, sizeof(arg));
  817. arg.revents = revents;
  818. return send_reply_ok(req, &arg, sizeof(arg));
  819. }
  820. static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  821. {
  822. char *name = (char *) inarg;
  823. if (req->f->op.lookup)
  824. req->f->op.lookup(req, nodeid, name);
  825. else
  826. fuse_reply_err(req, ENOSYS);
  827. }
  828. static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  829. {
  830. struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
  831. if (req->f->op.forget)
  832. req->f->op.forget(req, nodeid, arg->nlookup);
  833. else
  834. fuse_reply_none(req);
  835. }
  836. static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
  837. const void *inarg)
  838. {
  839. struct fuse_batch_forget_in *arg = (void *) inarg;
  840. struct fuse_forget_one *param = (void *) PARAM(arg);
  841. unsigned int i;
  842. (void) nodeid;
  843. if (req->f->op.forget_multi) {
  844. req->f->op.forget_multi(req, arg->count,
  845. (struct fuse_forget_data *) param);
  846. } else if (req->f->op.forget) {
  847. for (i = 0; i < arg->count; i++) {
  848. struct fuse_forget_one *forget = &param[i];
  849. struct fuse_req *dummy_req;
  850. dummy_req = fuse_ll_alloc_req(req->f);
  851. if (dummy_req == NULL)
  852. break;
  853. dummy_req->unique = req->unique;
  854. dummy_req->ctx = req->ctx;
  855. dummy_req->ch = NULL;
  856. req->f->op.forget(dummy_req, forget->nodeid,
  857. forget->nlookup);
  858. }
  859. fuse_reply_none(req);
  860. } else {
  861. fuse_reply_none(req);
  862. }
  863. }
  864. static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  865. {
  866. struct fuse_file_info *fip = NULL;
  867. struct fuse_file_info fi;
  868. if (req->f->conn.proto_minor >= 9) {
  869. struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
  870. if (arg->getattr_flags & FUSE_GETATTR_FH) {
  871. memset(&fi, 0, sizeof(fi));
  872. fi.fh = arg->fh;
  873. fi.fh_old = fi.fh;
  874. fip = &fi;
  875. }
  876. }
  877. if (req->f->op.getattr)
  878. req->f->op.getattr(req, nodeid, fip);
  879. else
  880. fuse_reply_err(req, ENOSYS);
  881. }
  882. static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  883. {
  884. struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
  885. if (req->f->op.setattr) {
  886. struct fuse_file_info *fi = NULL;
  887. struct fuse_file_info fi_store;
  888. struct stat stbuf;
  889. memset(&stbuf, 0, sizeof(stbuf));
  890. convert_attr(arg, &stbuf);
  891. if (arg->valid & FATTR_FH) {
  892. arg->valid &= ~FATTR_FH;
  893. memset(&fi_store, 0, sizeof(fi_store));
  894. fi = &fi_store;
  895. fi->fh = arg->fh;
  896. fi->fh_old = fi->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. fi.fh_old = fi.fh;
  1028. if (req->f->conn.proto_minor >= 9) {
  1029. fi.lock_owner = arg->lock_owner;
  1030. fi.flags = arg->flags;
  1031. }
  1032. req->f->op.read(req, nodeid, arg->size, arg->offset, &fi);
  1033. } else
  1034. fuse_reply_err(req, ENOSYS);
  1035. }
  1036. static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1037. {
  1038. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1039. struct fuse_file_info fi;
  1040. char *param;
  1041. memset(&fi, 0, sizeof(fi));
  1042. fi.fh = arg->fh;
  1043. fi.fh_old = fi.fh;
  1044. fi.writepage = arg->write_flags & 1;
  1045. if (req->f->conn.proto_minor < 9) {
  1046. param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1047. } else {
  1048. fi.lock_owner = arg->lock_owner;
  1049. fi.flags = arg->flags;
  1050. param = PARAM(arg);
  1051. }
  1052. if (req->f->op.write)
  1053. req->f->op.write(req, nodeid, param, arg->size,
  1054. arg->offset, &fi);
  1055. else
  1056. fuse_reply_err(req, ENOSYS);
  1057. }
  1058. static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
  1059. const struct fuse_buf *ibuf)
  1060. {
  1061. struct fuse_ll *f = req->f;
  1062. struct fuse_bufvec bufv = {
  1063. .buf[0] = *ibuf,
  1064. .count = 1,
  1065. };
  1066. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  1067. struct fuse_file_info fi;
  1068. memset(&fi, 0, sizeof(fi));
  1069. fi.fh = arg->fh;
  1070. fi.fh_old = fi.fh;
  1071. fi.writepage = arg->write_flags & 1;
  1072. if (req->f->conn.proto_minor < 9) {
  1073. bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  1074. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1075. FUSE_COMPAT_WRITE_IN_SIZE;
  1076. assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
  1077. } else {
  1078. fi.lock_owner = arg->lock_owner;
  1079. fi.flags = arg->flags;
  1080. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1081. bufv.buf[0].mem = PARAM(arg);
  1082. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1083. sizeof(struct fuse_write_in);
  1084. }
  1085. if (bufv.buf[0].size < arg->size) {
  1086. fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
  1087. fuse_reply_err(req, EIO);
  1088. goto out;
  1089. }
  1090. bufv.buf[0].size = arg->size;
  1091. req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
  1092. out:
  1093. /* Need to reset the pipe if ->write_buf() didn't consume all data */
  1094. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1095. fuse_ll_clear_pipe(f);
  1096. }
  1097. static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1098. {
  1099. struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
  1100. struct fuse_file_info fi;
  1101. memset(&fi, 0, sizeof(fi));
  1102. fi.fh = arg->fh;
  1103. fi.fh_old = fi.fh;
  1104. fi.flush = 1;
  1105. if (req->f->conn.proto_minor >= 7)
  1106. fi.lock_owner = arg->lock_owner;
  1107. if (req->f->op.flush)
  1108. req->f->op.flush(req, nodeid, &fi);
  1109. else
  1110. fuse_reply_err(req, ENOSYS);
  1111. }
  1112. static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1113. {
  1114. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1115. struct fuse_file_info fi;
  1116. memset(&fi, 0, sizeof(fi));
  1117. fi.flags = arg->flags;
  1118. fi.fh = arg->fh;
  1119. fi.fh_old = fi.fh;
  1120. if (req->f->conn.proto_minor >= 8) {
  1121. fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
  1122. fi.lock_owner = arg->lock_owner;
  1123. }
  1124. if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
  1125. fi.flock_release = 1;
  1126. fi.lock_owner = arg->lock_owner;
  1127. }
  1128. if (req->f->op.release)
  1129. req->f->op.release(req, nodeid, &fi);
  1130. else
  1131. fuse_reply_err(req, 0);
  1132. }
  1133. static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1134. {
  1135. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1136. struct fuse_file_info fi;
  1137. memset(&fi, 0, sizeof(fi));
  1138. fi.fh = arg->fh;
  1139. fi.fh_old = fi.fh;
  1140. if (req->f->op.fsync)
  1141. req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
  1142. else
  1143. fuse_reply_err(req, ENOSYS);
  1144. }
  1145. static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1146. {
  1147. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  1148. struct fuse_file_info fi;
  1149. memset(&fi, 0, sizeof(fi));
  1150. fi.flags = arg->flags;
  1151. if (req->f->op.opendir)
  1152. req->f->op.opendir(req, nodeid, &fi);
  1153. else
  1154. fuse_reply_open(req, &fi);
  1155. }
  1156. static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1157. {
  1158. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1159. struct fuse_file_info fi;
  1160. memset(&fi, 0, sizeof(fi));
  1161. fi.fh = arg->fh;
  1162. fi.fh_old = fi.fh;
  1163. if (req->f->op.readdir)
  1164. req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
  1165. else
  1166. fuse_reply_err(req, ENOSYS);
  1167. }
  1168. static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1169. {
  1170. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1171. struct fuse_file_info fi;
  1172. memset(&fi, 0, sizeof(fi));
  1173. fi.flags = arg->flags;
  1174. fi.fh = arg->fh;
  1175. fi.fh_old = fi.fh;
  1176. if (req->f->op.releasedir)
  1177. req->f->op.releasedir(req, nodeid, &fi);
  1178. else
  1179. fuse_reply_err(req, 0);
  1180. }
  1181. static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1182. {
  1183. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1184. struct fuse_file_info fi;
  1185. memset(&fi, 0, sizeof(fi));
  1186. fi.fh = arg->fh;
  1187. fi.fh_old = fi.fh;
  1188. if (req->f->op.fsyncdir)
  1189. req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
  1190. else
  1191. fuse_reply_err(req, ENOSYS);
  1192. }
  1193. static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1194. {
  1195. (void) nodeid;
  1196. (void) inarg;
  1197. if (req->f->op.statfs)
  1198. req->f->op.statfs(req, nodeid);
  1199. else {
  1200. struct statvfs buf = {
  1201. .f_namemax = 255,
  1202. .f_bsize = 512,
  1203. };
  1204. fuse_reply_statfs(req, &buf);
  1205. }
  1206. }
  1207. static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1208. {
  1209. struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
  1210. char *name = PARAM(arg);
  1211. char *value = name + strlen(name) + 1;
  1212. if (req->f->op.setxattr)
  1213. req->f->op.setxattr(req, nodeid, name, value, arg->size,
  1214. arg->flags);
  1215. else
  1216. fuse_reply_err(req, ENOSYS);
  1217. }
  1218. static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1219. {
  1220. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1221. if (req->f->op.getxattr)
  1222. req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
  1223. else
  1224. fuse_reply_err(req, ENOSYS);
  1225. }
  1226. static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1227. {
  1228. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1229. if (req->f->op.listxattr)
  1230. req->f->op.listxattr(req, nodeid, arg->size);
  1231. else
  1232. fuse_reply_err(req, ENOSYS);
  1233. }
  1234. static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1235. {
  1236. char *name = (char *) inarg;
  1237. if (req->f->op.removexattr)
  1238. req->f->op.removexattr(req, nodeid, name);
  1239. else
  1240. fuse_reply_err(req, ENOSYS);
  1241. }
  1242. static void convert_fuse_file_lock(struct fuse_file_lock *fl,
  1243. struct flock *flock)
  1244. {
  1245. memset(flock, 0, sizeof(struct flock));
  1246. flock->l_type = fl->type;
  1247. flock->l_whence = SEEK_SET;
  1248. flock->l_start = fl->start;
  1249. if (fl->end == OFFSET_MAX)
  1250. flock->l_len = 0;
  1251. else
  1252. flock->l_len = fl->end - fl->start + 1;
  1253. flock->l_pid = fl->pid;
  1254. }
  1255. static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1256. {
  1257. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1258. struct fuse_file_info fi;
  1259. struct flock flock;
  1260. memset(&fi, 0, sizeof(fi));
  1261. fi.fh = arg->fh;
  1262. fi.lock_owner = arg->owner;
  1263. convert_fuse_file_lock(&arg->lk, &flock);
  1264. if (req->f->op.getlk)
  1265. req->f->op.getlk(req, nodeid, &fi, &flock);
  1266. else
  1267. fuse_reply_err(req, ENOSYS);
  1268. }
  1269. static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
  1270. const void *inarg, int sleep)
  1271. {
  1272. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1273. struct fuse_file_info fi;
  1274. struct flock flock;
  1275. memset(&fi, 0, sizeof(fi));
  1276. fi.fh = arg->fh;
  1277. fi.lock_owner = arg->owner;
  1278. if (arg->lk_flags & FUSE_LK_FLOCK) {
  1279. int op = 0;
  1280. switch (arg->lk.type) {
  1281. case F_RDLCK:
  1282. op = LOCK_SH;
  1283. break;
  1284. case F_WRLCK:
  1285. op = LOCK_EX;
  1286. break;
  1287. case F_UNLCK:
  1288. op = LOCK_UN;
  1289. break;
  1290. }
  1291. if (!sleep)
  1292. op |= LOCK_NB;
  1293. if (req->f->op.flock)
  1294. req->f->op.flock(req, nodeid, &fi, op);
  1295. else
  1296. fuse_reply_err(req, ENOSYS);
  1297. } else {
  1298. convert_fuse_file_lock(&arg->lk, &flock);
  1299. if (req->f->op.setlk)
  1300. req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
  1301. else
  1302. fuse_reply_err(req, ENOSYS);
  1303. }
  1304. }
  1305. static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1306. {
  1307. do_setlk_common(req, nodeid, inarg, 0);
  1308. }
  1309. static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1310. {
  1311. do_setlk_common(req, nodeid, inarg, 1);
  1312. }
  1313. static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
  1314. {
  1315. struct fuse_req *curr;
  1316. for (curr = f->list.next; curr != &f->list; curr = curr->next) {
  1317. if (curr->unique == req->u.i.unique) {
  1318. fuse_interrupt_func_t func;
  1319. void *data;
  1320. curr->ctr++;
  1321. pthread_mutex_unlock(&f->lock);
  1322. /* Ugh, ugly locking */
  1323. pthread_mutex_lock(&curr->lock);
  1324. pthread_mutex_lock(&f->lock);
  1325. curr->interrupted = 1;
  1326. func = curr->u.ni.func;
  1327. data = curr->u.ni.data;
  1328. pthread_mutex_unlock(&f->lock);
  1329. if (func)
  1330. func(curr, data);
  1331. pthread_mutex_unlock(&curr->lock);
  1332. pthread_mutex_lock(&f->lock);
  1333. curr->ctr--;
  1334. if (!curr->ctr)
  1335. destroy_req(curr);
  1336. return 1;
  1337. }
  1338. }
  1339. for (curr = f->interrupts.next; curr != &f->interrupts;
  1340. curr = curr->next) {
  1341. if (curr->u.i.unique == req->u.i.unique)
  1342. return 1;
  1343. }
  1344. return 0;
  1345. }
  1346. static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1347. {
  1348. struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
  1349. struct fuse_ll *f = req->f;
  1350. (void) nodeid;
  1351. if (f->debug)
  1352. fprintf(stderr, "INTERRUPT: %llu\n",
  1353. (unsigned long long) arg->unique);
  1354. req->u.i.unique = arg->unique;
  1355. pthread_mutex_lock(&f->lock);
  1356. if (find_interrupted(f, req))
  1357. destroy_req(req);
  1358. else
  1359. list_add_req(req, &f->interrupts);
  1360. pthread_mutex_unlock(&f->lock);
  1361. }
  1362. static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
  1363. {
  1364. struct fuse_req *curr;
  1365. for (curr = f->interrupts.next; curr != &f->interrupts;
  1366. curr = curr->next) {
  1367. if (curr->u.i.unique == req->unique) {
  1368. req->interrupted = 1;
  1369. list_del_req(curr);
  1370. free(curr);
  1371. return NULL;
  1372. }
  1373. }
  1374. curr = f->interrupts.next;
  1375. if (curr != &f->interrupts) {
  1376. list_del_req(curr);
  1377. list_init_req(curr);
  1378. return curr;
  1379. } else
  1380. return NULL;
  1381. }
  1382. static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1383. {
  1384. struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
  1385. if (req->f->op.bmap)
  1386. req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
  1387. else
  1388. fuse_reply_err(req, ENOSYS);
  1389. }
  1390. static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1391. {
  1392. struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
  1393. unsigned int flags = arg->flags;
  1394. void *in_buf = arg->in_size ? PARAM(arg) : NULL;
  1395. struct fuse_file_info fi;
  1396. if (flags & FUSE_IOCTL_DIR &&
  1397. !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
  1398. fuse_reply_err(req, ENOTTY);
  1399. return;
  1400. }
  1401. memset(&fi, 0, sizeof(fi));
  1402. fi.fh = arg->fh;
  1403. fi.fh_old = fi.fh;
  1404. if (sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
  1405. !(flags & FUSE_IOCTL_32BIT)) {
  1406. req->ioctl_64bit = 1;
  1407. }
  1408. if (req->f->op.ioctl)
  1409. req->f->op.ioctl(req, nodeid, arg->cmd,
  1410. (void *)(uintptr_t)arg->arg, &fi, flags,
  1411. in_buf, arg->in_size, arg->out_size);
  1412. else
  1413. fuse_reply_err(req, ENOSYS);
  1414. }
  1415. void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
  1416. {
  1417. free(ph);
  1418. }
  1419. static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1420. {
  1421. struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
  1422. struct fuse_file_info fi;
  1423. memset(&fi, 0, sizeof(fi));
  1424. fi.fh = arg->fh;
  1425. fi.fh_old = fi.fh;
  1426. if (req->f->op.poll) {
  1427. struct fuse_pollhandle *ph = NULL;
  1428. if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
  1429. ph = malloc(sizeof(struct fuse_pollhandle));
  1430. if (ph == NULL) {
  1431. fuse_reply_err(req, ENOMEM);
  1432. return;
  1433. }
  1434. ph->kh = arg->kh;
  1435. ph->ch = req->ch;
  1436. ph->f = req->f;
  1437. }
  1438. req->f->op.poll(req, nodeid, &fi, ph);
  1439. } else {
  1440. fuse_reply_err(req, ENOSYS);
  1441. }
  1442. }
  1443. static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1444. {
  1445. struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
  1446. struct fuse_file_info fi;
  1447. memset(&fi, 0, sizeof(fi));
  1448. fi.fh = arg->fh;
  1449. if (req->f->op.fallocate)
  1450. req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
  1451. else
  1452. fuse_reply_err(req, ENOSYS);
  1453. }
  1454. static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1455. {
  1456. struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
  1457. struct fuse_init_out outarg;
  1458. struct fuse_ll *f = req->f;
  1459. size_t bufsize = fuse_chan_bufsize(req->ch);
  1460. (void) nodeid;
  1461. if (f->debug) {
  1462. fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
  1463. if (arg->major == 7 && arg->minor >= 6) {
  1464. fprintf(stderr, "flags=0x%08x\n", arg->flags);
  1465. fprintf(stderr, "max_readahead=0x%08x\n",
  1466. arg->max_readahead);
  1467. }
  1468. }
  1469. f->conn.proto_major = arg->major;
  1470. f->conn.proto_minor = arg->minor;
  1471. f->conn.capable = 0;
  1472. f->conn.want = 0;
  1473. memset(&outarg, 0, sizeof(outarg));
  1474. outarg.major = FUSE_KERNEL_VERSION;
  1475. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  1476. if (arg->major < 7) {
  1477. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  1478. arg->major, arg->minor);
  1479. fuse_reply_err(req, EPROTO);
  1480. return;
  1481. }
  1482. if (arg->major > 7) {
  1483. /* Wait for a second INIT request with a 7.X version */
  1484. send_reply_ok(req, &outarg, sizeof(outarg));
  1485. return;
  1486. }
  1487. if (arg->minor >= 6) {
  1488. if (f->conn.async_read)
  1489. f->conn.async_read = arg->flags & FUSE_ASYNC_READ;
  1490. if (arg->max_readahead < f->conn.max_readahead)
  1491. f->conn.max_readahead = arg->max_readahead;
  1492. if (arg->flags & FUSE_ASYNC_READ)
  1493. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  1494. if (arg->flags & FUSE_POSIX_LOCKS)
  1495. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  1496. if (arg->flags & FUSE_ATOMIC_O_TRUNC)
  1497. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  1498. if (arg->flags & FUSE_EXPORT_SUPPORT)
  1499. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  1500. if (arg->flags & FUSE_BIG_WRITES)
  1501. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  1502. if (arg->flags & FUSE_DONT_MASK)
  1503. f->conn.capable |= FUSE_CAP_DONT_MASK;
  1504. if (arg->flags & FUSE_FLOCK_LOCKS)
  1505. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  1506. if (arg->flags & FUSE_POSIX_ACL)
  1507. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  1508. if (arg->flags & FUSE_ASYNC_DIO)
  1509. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  1510. if (arg->flags & FUSE_PARALLEL_DIROPS)
  1511. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  1512. } else {
  1513. f->conn.async_read = 0;
  1514. f->conn.max_readahead = 0;
  1515. }
  1516. if (req->f->conn.proto_minor >= 14) {
  1517. #ifdef HAVE_SPLICE
  1518. #ifdef HAVE_VMSPLICE
  1519. f->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
  1520. if (f->splice_write)
  1521. f->conn.want |= FUSE_CAP_SPLICE_WRITE;
  1522. if (f->splice_move)
  1523. f->conn.want |= FUSE_CAP_SPLICE_MOVE;
  1524. #endif
  1525. f->conn.capable |= FUSE_CAP_SPLICE_READ;
  1526. if (f->splice_read)
  1527. f->conn.want |= FUSE_CAP_SPLICE_READ;
  1528. #endif
  1529. }
  1530. if (req->f->conn.proto_minor >= 18)
  1531. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1532. if (f->atomic_o_trunc)
  1533. f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC;
  1534. if (f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1535. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1536. if (f->op.flock && !f->no_remote_flock)
  1537. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1538. if (f->big_writes)
  1539. f->conn.want |= FUSE_CAP_BIG_WRITES;
  1540. if (bufsize < FUSE_MIN_READ_BUFFER) {
  1541. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1542. bufsize);
  1543. bufsize = FUSE_MIN_READ_BUFFER;
  1544. }
  1545. bufsize -= 4096;
  1546. if (bufsize < f->conn.max_write)
  1547. f->conn.max_write = bufsize;
  1548. f->got_init = 1;
  1549. if (f->op.init)
  1550. f->op.init(f->userdata, &f->conn);
  1551. if (f->no_splice_read)
  1552. f->conn.want &= ~FUSE_CAP_SPLICE_READ;
  1553. if (f->no_splice_write)
  1554. f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
  1555. if (f->no_splice_move)
  1556. f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
  1557. if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ))
  1558. outarg.flags |= FUSE_ASYNC_READ;
  1559. if (f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1560. outarg.flags |= FUSE_POSIX_LOCKS;
  1561. if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1562. outarg.flags |= FUSE_ATOMIC_O_TRUNC;
  1563. if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1564. outarg.flags |= FUSE_EXPORT_SUPPORT;
  1565. if (f->conn.want & FUSE_CAP_BIG_WRITES)
  1566. outarg.flags |= FUSE_BIG_WRITES;
  1567. if (f->conn.want & FUSE_CAP_DONT_MASK)
  1568. outarg.flags |= FUSE_DONT_MASK;
  1569. if (f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1570. outarg.flags |= FUSE_FLOCK_LOCKS;
  1571. if (f->conn.want & FUSE_CAP_POSIX_ACL)
  1572. outarg.flags |= FUSE_POSIX_ACL;
  1573. if (f->conn.want & FUSE_CAP_ASYNC_DIO)
  1574. outarg.flags |= FUSE_ASYNC_DIO;
  1575. if (f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1576. outarg.flags |= FUSE_PARALLEL_DIROPS;
  1577. outarg.max_readahead = f->conn.max_readahead;
  1578. outarg.max_write = f->conn.max_write;
  1579. if (f->conn.proto_minor >= 13) {
  1580. if (f->conn.max_background >= (1 << 16))
  1581. f->conn.max_background = (1 << 16) - 1;
  1582. if (f->conn.congestion_threshold > f->conn.max_background)
  1583. f->conn.congestion_threshold = f->conn.max_background;
  1584. if (!f->conn.congestion_threshold) {
  1585. f->conn.congestion_threshold =
  1586. f->conn.max_background * 3 / 4;
  1587. }
  1588. outarg.max_background = f->conn.max_background;
  1589. outarg.congestion_threshold = f->conn.congestion_threshold;
  1590. }
  1591. if (f->debug) {
  1592. fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
  1593. fprintf(stderr, " flags=0x%08x\n", outarg.flags);
  1594. fprintf(stderr, " max_readahead=0x%08x\n",
  1595. outarg.max_readahead);
  1596. fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
  1597. fprintf(stderr, " max_background=%i\n",
  1598. outarg.max_background);
  1599. fprintf(stderr, " congestion_threshold=%i\n",
  1600. outarg.congestion_threshold);
  1601. }
  1602. size_t outargsize;
  1603. if(arg->minor < 5)
  1604. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1605. else if(arg->minor < 23)
  1606. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1607. else
  1608. outargsize = sizeof(outarg);
  1609. send_reply_ok(req, &outarg, outargsize);
  1610. }
  1611. static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1612. {
  1613. struct fuse_ll *f = req->f;
  1614. (void) nodeid;
  1615. (void) inarg;
  1616. f->got_destroy = 1;
  1617. if (f->op.destroy)
  1618. f->op.destroy(f->userdata);
  1619. send_reply_ok(req, NULL, 0);
  1620. }
  1621. static void list_del_nreq(struct fuse_notify_req *nreq)
  1622. {
  1623. struct fuse_notify_req *prev = nreq->prev;
  1624. struct fuse_notify_req *next = nreq->next;
  1625. prev->next = next;
  1626. next->prev = prev;
  1627. }
  1628. static void list_add_nreq(struct fuse_notify_req *nreq,
  1629. struct fuse_notify_req *next)
  1630. {
  1631. struct fuse_notify_req *prev = next->prev;
  1632. nreq->next = next;
  1633. nreq->prev = prev;
  1634. prev->next = nreq;
  1635. next->prev = nreq;
  1636. }
  1637. static void list_init_nreq(struct fuse_notify_req *nreq)
  1638. {
  1639. nreq->next = nreq;
  1640. nreq->prev = nreq;
  1641. }
  1642. static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
  1643. const void *inarg, const struct fuse_buf *buf)
  1644. {
  1645. struct fuse_ll *f = req->f;
  1646. struct fuse_notify_req *nreq;
  1647. struct fuse_notify_req *head;
  1648. pthread_mutex_lock(&f->lock);
  1649. head = &f->notify_list;
  1650. for (nreq = head->next; nreq != head; nreq = nreq->next) {
  1651. if (nreq->unique == req->unique) {
  1652. list_del_nreq(nreq);
  1653. break;
  1654. }
  1655. }
  1656. pthread_mutex_unlock(&f->lock);
  1657. if (nreq != head)
  1658. nreq->reply(nreq, req, nodeid, inarg, buf);
  1659. }
  1660. static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch,
  1661. int notify_code, struct iovec *iov, int count)
  1662. {
  1663. struct fuse_out_header out;
  1664. if (!f->got_init)
  1665. return -ENOTCONN;
  1666. out.unique = 0;
  1667. out.error = notify_code;
  1668. iov[0].iov_base = &out;
  1669. iov[0].iov_len = sizeof(struct fuse_out_header);
  1670. return fuse_send_msg(f, ch, iov, count);
  1671. }
  1672. int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
  1673. {
  1674. if (ph != NULL) {
  1675. struct fuse_notify_poll_wakeup_out outarg;
  1676. struct iovec iov[2];
  1677. outarg.kh = ph->kh;
  1678. iov[1].iov_base = &outarg;
  1679. iov[1].iov_len = sizeof(outarg);
  1680. return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2);
  1681. } else {
  1682. return 0;
  1683. }
  1684. }
  1685. int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino,
  1686. off_t off, off_t len)
  1687. {
  1688. struct fuse_notify_inval_inode_out outarg;
  1689. struct fuse_ll *f;
  1690. struct iovec iov[2];
  1691. if (!ch)
  1692. return -EINVAL;
  1693. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1694. if (!f)
  1695. return -ENODEV;
  1696. outarg.ino = ino;
  1697. outarg.off = off;
  1698. outarg.len = len;
  1699. iov[1].iov_base = &outarg;
  1700. iov[1].iov_len = sizeof(outarg);
  1701. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2);
  1702. }
  1703. int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent,
  1704. const char *name, size_t namelen)
  1705. {
  1706. struct fuse_notify_inval_entry_out outarg;
  1707. struct fuse_ll *f;
  1708. struct iovec iov[3];
  1709. if (!ch)
  1710. return -EINVAL;
  1711. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1712. if (!f)
  1713. return -ENODEV;
  1714. outarg.parent = parent;
  1715. outarg.namelen = namelen;
  1716. outarg.padding = 0;
  1717. iov[1].iov_base = &outarg;
  1718. iov[1].iov_len = sizeof(outarg);
  1719. iov[2].iov_base = (void *)name;
  1720. iov[2].iov_len = namelen + 1;
  1721. return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
  1722. }
  1723. int fuse_lowlevel_notify_delete(struct fuse_chan *ch,
  1724. fuse_ino_t parent, fuse_ino_t child,
  1725. const char *name, size_t namelen)
  1726. {
  1727. struct fuse_notify_delete_out outarg;
  1728. struct fuse_ll *f;
  1729. struct iovec iov[3];
  1730. if (!ch)
  1731. return -EINVAL;
  1732. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1733. if (!f)
  1734. return -ENODEV;
  1735. if (f->conn.proto_minor < 18)
  1736. return -ENOSYS;
  1737. outarg.parent = parent;
  1738. outarg.child = child;
  1739. outarg.namelen = namelen;
  1740. outarg.padding = 0;
  1741. iov[1].iov_base = &outarg;
  1742. iov[1].iov_len = sizeof(outarg);
  1743. iov[2].iov_base = (void *)name;
  1744. iov[2].iov_len = namelen + 1;
  1745. return send_notify_iov(f, ch, FUSE_NOTIFY_DELETE, iov, 3);
  1746. }
  1747. int fuse_lowlevel_notify_store(struct fuse_chan *ch, fuse_ino_t ino,
  1748. off_t offset, struct fuse_bufvec *bufv,
  1749. enum fuse_buf_copy_flags flags)
  1750. {
  1751. struct fuse_out_header out;
  1752. struct fuse_notify_store_out outarg;
  1753. struct fuse_ll *f;
  1754. struct iovec iov[3];
  1755. size_t size = fuse_buf_size(bufv);
  1756. int res;
  1757. if (!ch)
  1758. return -EINVAL;
  1759. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1760. if (!f)
  1761. return -ENODEV;
  1762. if (f->conn.proto_minor < 15)
  1763. return -ENOSYS;
  1764. out.unique = 0;
  1765. out.error = FUSE_NOTIFY_STORE;
  1766. outarg.nodeid = ino;
  1767. outarg.offset = offset;
  1768. outarg.size = size;
  1769. outarg.padding = 0;
  1770. iov[0].iov_base = &out;
  1771. iov[0].iov_len = sizeof(out);
  1772. iov[1].iov_base = &outarg;
  1773. iov[1].iov_len = sizeof(outarg);
  1774. res = fuse_send_data_iov(f, ch, iov, 2, bufv, flags);
  1775. if (res > 0)
  1776. res = -res;
  1777. return res;
  1778. }
  1779. struct fuse_retrieve_req {
  1780. struct fuse_notify_req nreq;
  1781. void *cookie;
  1782. };
  1783. static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
  1784. fuse_req_t req, fuse_ino_t ino,
  1785. const void *inarg,
  1786. const struct fuse_buf *ibuf)
  1787. {
  1788. struct fuse_ll *f = req->f;
  1789. struct fuse_retrieve_req *rreq =
  1790. container_of(nreq, struct fuse_retrieve_req, nreq);
  1791. const struct fuse_notify_retrieve_in *arg = inarg;
  1792. struct fuse_bufvec bufv = {
  1793. .buf[0] = *ibuf,
  1794. .count = 1,
  1795. };
  1796. if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  1797. bufv.buf[0].mem = PARAM(arg);
  1798. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  1799. sizeof(struct fuse_notify_retrieve_in);
  1800. if (bufv.buf[0].size < arg->size) {
  1801. fprintf(stderr, "fuse: retrieve reply: buffer size too small\n");
  1802. fuse_reply_none(req);
  1803. goto out;
  1804. }
  1805. bufv.buf[0].size = arg->size;
  1806. if (req->f->op.retrieve_reply) {
  1807. req->f->op.retrieve_reply(req, rreq->cookie, ino,
  1808. arg->offset, &bufv);
  1809. } else {
  1810. fuse_reply_none(req);
  1811. }
  1812. out:
  1813. free(rreq);
  1814. if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  1815. fuse_ll_clear_pipe(f);
  1816. }
  1817. int fuse_lowlevel_notify_retrieve(struct fuse_chan *ch, fuse_ino_t ino,
  1818. size_t size, off_t offset, void *cookie)
  1819. {
  1820. struct fuse_notify_retrieve_out outarg;
  1821. struct fuse_ll *f;
  1822. struct iovec iov[2];
  1823. struct fuse_retrieve_req *rreq;
  1824. int err;
  1825. if (!ch)
  1826. return -EINVAL;
  1827. f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch));
  1828. if (!f)
  1829. return -ENODEV;
  1830. if (f->conn.proto_minor < 15)
  1831. return -ENOSYS;
  1832. rreq = malloc(sizeof(*rreq));
  1833. if (rreq == NULL)
  1834. return -ENOMEM;
  1835. pthread_mutex_lock(&f->lock);
  1836. rreq->cookie = cookie;
  1837. rreq->nreq.unique = f->notify_ctr++;
  1838. rreq->nreq.reply = fuse_ll_retrieve_reply;
  1839. list_add_nreq(&rreq->nreq, &f->notify_list);
  1840. pthread_mutex_unlock(&f->lock);
  1841. outarg.notify_unique = rreq->nreq.unique;
  1842. outarg.nodeid = ino;
  1843. outarg.offset = offset;
  1844. outarg.size = size;
  1845. iov[1].iov_base = &outarg;
  1846. iov[1].iov_len = sizeof(outarg);
  1847. err = send_notify_iov(f, ch, FUSE_NOTIFY_RETRIEVE, iov, 2);
  1848. if (err) {
  1849. pthread_mutex_lock(&f->lock);
  1850. list_del_nreq(&rreq->nreq);
  1851. pthread_mutex_unlock(&f->lock);
  1852. free(rreq);
  1853. }
  1854. return err;
  1855. }
  1856. void *fuse_req_userdata(fuse_req_t req)
  1857. {
  1858. return req->f->userdata;
  1859. }
  1860. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
  1861. {
  1862. return &req->ctx;
  1863. }
  1864. /*
  1865. * The size of fuse_ctx got extended, so need to be careful about
  1866. * incompatibility (i.e. a new binary cannot work with an old
  1867. * library).
  1868. */
  1869. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
  1870. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
  1871. {
  1872. return fuse_req_ctx(req);
  1873. }
  1874. #ifndef __NetBSD__
  1875. FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
  1876. #endif
  1877. void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
  1878. void *data)
  1879. {
  1880. pthread_mutex_lock(&req->lock);
  1881. pthread_mutex_lock(&req->f->lock);
  1882. req->u.ni.func = func;
  1883. req->u.ni.data = data;
  1884. pthread_mutex_unlock(&req->f->lock);
  1885. if (req->interrupted && func)
  1886. func(req, data);
  1887. pthread_mutex_unlock(&req->lock);
  1888. }
  1889. int fuse_req_interrupted(fuse_req_t req)
  1890. {
  1891. int interrupted;
  1892. pthread_mutex_lock(&req->f->lock);
  1893. interrupted = req->interrupted;
  1894. pthread_mutex_unlock(&req->f->lock);
  1895. return interrupted;
  1896. }
  1897. static struct {
  1898. void (*func)(fuse_req_t, fuse_ino_t, const void *);
  1899. const char *name;
  1900. } fuse_ll_ops[] = {
  1901. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1902. [FUSE_FORGET] = { do_forget, "FORGET" },
  1903. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1904. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1905. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1906. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1907. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1908. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1909. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1910. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1911. [FUSE_RENAME] = { do_rename, "RENAME" },
  1912. [FUSE_LINK] = { do_link, "LINK" },
  1913. [FUSE_OPEN] = { do_open, "OPEN" },
  1914. [FUSE_READ] = { do_read, "READ" },
  1915. [FUSE_WRITE] = { do_write, "WRITE" },
  1916. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1917. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1918. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1919. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1920. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1921. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1922. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1923. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1924. [FUSE_INIT] = { do_init, "INIT" },
  1925. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1926. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1927. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1928. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1929. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1930. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1931. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1932. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1933. [FUSE_CREATE] = { do_create, "CREATE" },
  1934. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  1935. [FUSE_BMAP] = { do_bmap, "BMAP" },
  1936. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  1937. [FUSE_POLL] = { do_poll, "POLL" },
  1938. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  1939. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  1940. [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
  1941. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  1942. [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" },
  1943. };
  1944. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  1945. static const char *opname(enum fuse_opcode opcode)
  1946. {
  1947. if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
  1948. return "???";
  1949. else
  1950. return fuse_ll_ops[opcode].name;
  1951. }
  1952. static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
  1953. struct fuse_bufvec *src)
  1954. {
  1955. int res = fuse_buf_copy(dst, src, 0);
  1956. if (res < 0) {
  1957. fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
  1958. return res;
  1959. }
  1960. if (res < fuse_buf_size(dst)) {
  1961. fprintf(stderr, "fuse: copy from pipe: short read\n");
  1962. return -1;
  1963. }
  1964. return 0;
  1965. }
  1966. static void fuse_ll_process_buf(void *data, const struct fuse_buf *buf,
  1967. struct fuse_chan *ch)
  1968. {
  1969. struct fuse_ll *f = (struct fuse_ll *) data;
  1970. const size_t write_header_size = sizeof(struct fuse_in_header) +
  1971. sizeof(struct fuse_write_in);
  1972. struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
  1973. struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
  1974. struct fuse_in_header *in;
  1975. const void *inarg;
  1976. struct fuse_req *req;
  1977. void *mbuf = NULL;
  1978. int err;
  1979. int res;
  1980. if (buf->flags & FUSE_BUF_IS_FD) {
  1981. if (buf->size < tmpbuf.buf[0].size)
  1982. tmpbuf.buf[0].size = buf->size;
  1983. mbuf = malloc(tmpbuf.buf[0].size);
  1984. if (mbuf == NULL) {
  1985. fprintf(stderr, "fuse: failed to allocate header\n");
  1986. goto clear_pipe;
  1987. }
  1988. tmpbuf.buf[0].mem = mbuf;
  1989. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  1990. if (res < 0)
  1991. goto clear_pipe;
  1992. in = mbuf;
  1993. } else {
  1994. in = buf->mem;
  1995. }
  1996. if (f->debug) {
  1997. fprintf(stderr,
  1998. "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
  1999. (unsigned long long) in->unique,
  2000. opname((enum fuse_opcode) in->opcode), in->opcode,
  2001. (unsigned long) in->nodeid, buf->size, in->pid);
  2002. }
  2003. req = fuse_ll_alloc_req(f);
  2004. if (req == NULL) {
  2005. struct fuse_out_header out = {
  2006. .unique = in->unique,
  2007. .error = -ENOMEM,
  2008. };
  2009. struct iovec iov = {
  2010. .iov_base = &out,
  2011. .iov_len = sizeof(struct fuse_out_header),
  2012. };
  2013. fuse_send_msg(f, ch, &iov, 1);
  2014. goto clear_pipe;
  2015. }
  2016. req->unique = in->unique;
  2017. req->ctx.uid = in->uid;
  2018. req->ctx.gid = in->gid;
  2019. req->ctx.pid = in->pid;
  2020. req->ch = ch;
  2021. err = EIO;
  2022. if (!f->got_init) {
  2023. enum fuse_opcode expected;
  2024. expected = f->cuse_data ? CUSE_INIT : FUSE_INIT;
  2025. if (in->opcode != expected)
  2026. goto reply_err;
  2027. } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
  2028. goto reply_err;
  2029. err = EACCES;
  2030. if (f->allow_root && in->uid != f->owner && in->uid != 0 &&
  2031. in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
  2032. in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
  2033. in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
  2034. in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
  2035. in->opcode != FUSE_NOTIFY_REPLY)
  2036. goto reply_err;
  2037. err = ENOSYS;
  2038. if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
  2039. goto reply_err;
  2040. if (in->opcode != FUSE_INTERRUPT) {
  2041. struct fuse_req *intr;
  2042. pthread_mutex_lock(&f->lock);
  2043. intr = check_interrupt(f, req);
  2044. list_add_req(req, &f->list);
  2045. pthread_mutex_unlock(&f->lock);
  2046. if (intr)
  2047. fuse_reply_err(intr, EAGAIN);
  2048. }
  2049. if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
  2050. (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
  2051. in->opcode != FUSE_NOTIFY_REPLY) {
  2052. void *newmbuf;
  2053. err = ENOMEM;
  2054. newmbuf = realloc(mbuf, buf->size);
  2055. if (newmbuf == NULL)
  2056. goto reply_err;
  2057. mbuf = newmbuf;
  2058. tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
  2059. tmpbuf.buf[0].mem = mbuf + write_header_size;
  2060. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  2061. err = -res;
  2062. if (res < 0)
  2063. goto reply_err;
  2064. in = mbuf;
  2065. }
  2066. inarg = (void *) &in[1];
  2067. if (in->opcode == FUSE_WRITE && f->op.write_buf)
  2068. do_write_buf(req, in->nodeid, inarg, buf);
  2069. else if (in->opcode == FUSE_NOTIFY_REPLY)
  2070. do_notify_reply(req, in->nodeid, inarg, buf);
  2071. else
  2072. fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
  2073. out_free:
  2074. free(mbuf);
  2075. return;
  2076. reply_err:
  2077. fuse_reply_err(req, err);
  2078. clear_pipe:
  2079. if (buf->flags & FUSE_BUF_IS_FD)
  2080. fuse_ll_clear_pipe(f);
  2081. goto out_free;
  2082. }
  2083. static void fuse_ll_process(void *data, const char *buf, size_t len,
  2084. struct fuse_chan *ch)
  2085. {
  2086. struct fuse_buf fbuf = {
  2087. .mem = (void *) buf,
  2088. .size = len,
  2089. };
  2090. fuse_ll_process_buf(data, &fbuf, ch);
  2091. }
  2092. enum {
  2093. KEY_HELP,
  2094. KEY_VERSION,
  2095. };
  2096. static const struct fuse_opt fuse_ll_opts[] = {
  2097. { "debug", offsetof(struct fuse_ll, debug), 1 },
  2098. { "-d", offsetof(struct fuse_ll, debug), 1 },
  2099. { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
  2100. { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 },
  2101. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  2102. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  2103. { "congestion_threshold=%u",
  2104. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  2105. { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 },
  2106. { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 },
  2107. { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1},
  2108. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2109. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2110. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  2111. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  2112. { "big_writes", offsetof(struct fuse_ll, big_writes), 1},
  2113. { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
  2114. { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
  2115. { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
  2116. { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
  2117. { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
  2118. { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
  2119. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  2120. FUSE_OPT_KEY("-h", KEY_HELP),
  2121. FUSE_OPT_KEY("--help", KEY_HELP),
  2122. FUSE_OPT_KEY("-V", KEY_VERSION),
  2123. FUSE_OPT_KEY("--version", KEY_VERSION),
  2124. FUSE_OPT_END
  2125. };
  2126. static void fuse_ll_version(void)
  2127. {
  2128. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  2129. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  2130. }
  2131. static void fuse_ll_help(void)
  2132. {
  2133. fprintf(stderr,
  2134. " -o max_write=N set maximum size of write requests\n"
  2135. " -o max_readahead=N set maximum readahead\n"
  2136. " -o max_background=N set number of maximum background requests\n"
  2137. " -o congestion_threshold=N set kernel's congestion threshold\n"
  2138. " -o async_read perform reads asynchronously (default)\n"
  2139. " -o sync_read perform reads synchronously\n"
  2140. " -o atomic_o_trunc enable atomic open+truncate support\n"
  2141. " -o big_writes enable larger than 4kB writes\n"
  2142. " -o no_remote_lock disable remote file locking\n"
  2143. " -o no_remote_flock disable remote file locking (BSD)\n"
  2144. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  2145. " -o [no_]splice_write use splice to write to the fuse device\n"
  2146. " -o [no_]splice_move move data while splicing to the fuse device\n"
  2147. " -o [no_]splice_read use splice to read from the fuse device\n"
  2148. );
  2149. }
  2150. static int fuse_ll_opt_proc(void *data, const char *arg, int key,
  2151. struct fuse_args *outargs)
  2152. {
  2153. (void) data; (void) outargs;
  2154. switch (key) {
  2155. case KEY_HELP:
  2156. fuse_ll_help();
  2157. break;
  2158. case KEY_VERSION:
  2159. fuse_ll_version();
  2160. break;
  2161. default:
  2162. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  2163. }
  2164. return -1;
  2165. }
  2166. int fuse_lowlevel_is_lib_option(const char *opt)
  2167. {
  2168. return fuse_opt_match(fuse_ll_opts, opt);
  2169. }
  2170. static void fuse_ll_destroy(void *data)
  2171. {
  2172. struct fuse_ll *f = (struct fuse_ll *) data;
  2173. struct fuse_ll_pipe *llp;
  2174. if (f->got_init && !f->got_destroy) {
  2175. if (f->op.destroy)
  2176. f->op.destroy(f->userdata);
  2177. }
  2178. llp = pthread_getspecific(f->pipe_key);
  2179. if (llp != NULL)
  2180. fuse_ll_pipe_free(llp);
  2181. pthread_key_delete(f->pipe_key);
  2182. pthread_mutex_destroy(&f->lock);
  2183. free(f->cuse_data);
  2184. free(f);
  2185. }
  2186. static void fuse_ll_pipe_destructor(void *data)
  2187. {
  2188. struct fuse_ll_pipe *llp = data;
  2189. fuse_ll_pipe_free(llp);
  2190. }
  2191. #ifdef HAVE_SPLICE
  2192. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2193. struct fuse_chan **chp)
  2194. {
  2195. struct fuse_chan *ch = *chp;
  2196. struct fuse_ll *f = fuse_session_data(se);
  2197. size_t bufsize = buf->size;
  2198. struct fuse_ll_pipe *llp;
  2199. struct fuse_buf tmpbuf;
  2200. int err;
  2201. int res;
  2202. if (f->conn.proto_minor < 14 || !(f->conn.want & FUSE_CAP_SPLICE_READ))
  2203. goto fallback;
  2204. llp = fuse_ll_get_pipe(f);
  2205. if (llp == NULL)
  2206. goto fallback;
  2207. if (llp->size < bufsize) {
  2208. if (llp->can_grow) {
  2209. res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
  2210. if (res == -1) {
  2211. llp->can_grow = 0;
  2212. goto fallback;
  2213. }
  2214. llp->size = res;
  2215. }
  2216. if (llp->size < bufsize)
  2217. goto fallback;
  2218. }
  2219. res = splice(fuse_chan_fd(ch), NULL, llp->pipe[1], NULL, bufsize, 0);
  2220. err = errno;
  2221. if (fuse_session_exited(se))
  2222. return 0;
  2223. if (res == -1) {
  2224. if (err == ENODEV) {
  2225. fuse_session_exit(se);
  2226. return 0;
  2227. }
  2228. if (err != EINTR && err != EAGAIN)
  2229. perror("fuse: splice from device");
  2230. return -err;
  2231. }
  2232. if (res < sizeof(struct fuse_in_header)) {
  2233. fprintf(stderr, "short splice from fuse device\n");
  2234. return -EIO;
  2235. }
  2236. tmpbuf = (struct fuse_buf) {
  2237. .size = res,
  2238. .flags = FUSE_BUF_IS_FD,
  2239. .fd = llp->pipe[0],
  2240. };
  2241. /*
  2242. * Don't bother with zero copy for small requests.
  2243. * fuse_loop_mt() needs to check for FORGET so this more than
  2244. * just an optimization.
  2245. */
  2246. if (res < sizeof(struct fuse_in_header) +
  2247. sizeof(struct fuse_write_in) + pagesize) {
  2248. struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
  2249. struct fuse_bufvec dst = { .buf[0] = *buf, .count = 1 };
  2250. res = fuse_buf_copy(&dst, &src, 0);
  2251. if (res < 0) {
  2252. fprintf(stderr, "fuse: copy from pipe: %s\n",
  2253. strerror(-res));
  2254. fuse_ll_clear_pipe(f);
  2255. return res;
  2256. }
  2257. if (res < tmpbuf.size) {
  2258. fprintf(stderr, "fuse: copy from pipe: short read\n");
  2259. fuse_ll_clear_pipe(f);
  2260. return -EIO;
  2261. }
  2262. buf->size = tmpbuf.size;
  2263. return buf->size;
  2264. }
  2265. *buf = tmpbuf;
  2266. return res;
  2267. fallback:
  2268. res = fuse_chan_recv(chp, buf->mem, bufsize);
  2269. if (res <= 0)
  2270. return res;
  2271. buf->size = res;
  2272. return res;
  2273. }
  2274. #else
  2275. static int fuse_ll_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  2276. struct fuse_chan **chp)
  2277. {
  2278. (void) se;
  2279. int res = fuse_chan_recv(chp, buf->mem, buf->size);
  2280. if (res <= 0)
  2281. return res;
  2282. buf->size = res;
  2283. return res;
  2284. }
  2285. #endif
  2286. /*
  2287. * always call fuse_lowlevel_new_common() internally, to work around a
  2288. * misfeature in the FreeBSD runtime linker, which links the old
  2289. * version of a symbol to internal references.
  2290. */
  2291. struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
  2292. const struct fuse_lowlevel_ops *op,
  2293. size_t op_size, void *userdata)
  2294. {
  2295. int err;
  2296. struct fuse_ll *f;
  2297. struct fuse_session *se;
  2298. struct fuse_session_ops sop = {
  2299. .process = fuse_ll_process,
  2300. .destroy = fuse_ll_destroy,
  2301. };
  2302. if (sizeof(struct fuse_lowlevel_ops) < op_size) {
  2303. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  2304. op_size = sizeof(struct fuse_lowlevel_ops);
  2305. }
  2306. f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
  2307. if (f == NULL) {
  2308. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  2309. goto out;
  2310. }
  2311. f->conn.async_read = 1;
  2312. f->conn.max_write = UINT_MAX;
  2313. f->conn.max_readahead = UINT_MAX;
  2314. f->atomic_o_trunc = 0;
  2315. list_init_req(&f->list);
  2316. list_init_req(&f->interrupts);
  2317. list_init_nreq(&f->notify_list);
  2318. f->notify_ctr = 1;
  2319. fuse_mutex_init(&f->lock);
  2320. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  2321. if (err) {
  2322. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  2323. strerror(err));
  2324. goto out_free;
  2325. }
  2326. if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  2327. goto out_key_destroy;
  2328. if (f->debug)
  2329. fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
  2330. memcpy(&f->op, op, op_size);
  2331. f->owner = getuid();
  2332. f->userdata = userdata;
  2333. se = fuse_session_new(&sop, f);
  2334. if (!se)
  2335. goto out_key_destroy;
  2336. se->receive_buf = fuse_ll_receive_buf;
  2337. se->process_buf = fuse_ll_process_buf;
  2338. return se;
  2339. out_key_destroy:
  2340. pthread_key_delete(f->pipe_key);
  2341. out_free:
  2342. pthread_mutex_destroy(&f->lock);
  2343. free(f);
  2344. out:
  2345. return NULL;
  2346. }
  2347. struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
  2348. const struct fuse_lowlevel_ops *op,
  2349. size_t op_size, void *userdata)
  2350. {
  2351. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  2352. }
  2353. #ifdef linux
  2354. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
  2355. {
  2356. char *buf;
  2357. size_t bufsize = 1024;
  2358. char path[128];
  2359. int ret;
  2360. int fd;
  2361. unsigned long pid = req->ctx.pid;
  2362. char *s;
  2363. sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
  2364. retry:
  2365. buf = malloc(bufsize);
  2366. if (buf == NULL)
  2367. return -ENOMEM;
  2368. ret = -EIO;
  2369. fd = open(path, O_RDONLY);
  2370. if (fd == -1)
  2371. goto out_free;
  2372. ret = read(fd, buf, bufsize);
  2373. close(fd);
  2374. if (ret == -1) {
  2375. ret = -EIO;
  2376. goto out_free;
  2377. }
  2378. if (ret == bufsize) {
  2379. free(buf);
  2380. bufsize *= 4;
  2381. goto retry;
  2382. }
  2383. ret = -EIO;
  2384. s = strstr(buf, "\nGroups:");
  2385. if (s == NULL)
  2386. goto out_free;
  2387. s += 8;
  2388. ret = 0;
  2389. while (1) {
  2390. char *end;
  2391. unsigned long val = strtoul(s, &end, 0);
  2392. if (end == s)
  2393. break;
  2394. s = end;
  2395. if (ret < size)
  2396. list[ret] = val;
  2397. ret++;
  2398. }
  2399. out_free:
  2400. free(buf);
  2401. return ret;
  2402. }
  2403. #else /* linux */
  2404. /*
  2405. * This is currently not implemented on other than Linux...
  2406. */
  2407. int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
  2408. {
  2409. return -ENOSYS;
  2410. }
  2411. #endif
  2412. #if !defined(__FreeBSD__) && !defined(__NetBSD__)
  2413. static void fill_open_compat(struct fuse_open_out *arg,
  2414. const struct fuse_file_info_compat *f)
  2415. {
  2416. arg->fh = f->fh;
  2417. if (f->direct_io)
  2418. arg->open_flags |= FOPEN_DIRECT_IO;
  2419. if (f->keep_cache)
  2420. arg->open_flags |= FOPEN_KEEP_CACHE;
  2421. }
  2422. static void convert_statfs_compat(const struct statfs *compatbuf,
  2423. struct statvfs *buf)
  2424. {
  2425. buf->f_bsize = compatbuf->f_bsize;
  2426. buf->f_blocks = compatbuf->f_blocks;
  2427. buf->f_bfree = compatbuf->f_bfree;
  2428. buf->f_bavail = compatbuf->f_bavail;
  2429. buf->f_files = compatbuf->f_files;
  2430. buf->f_ffree = compatbuf->f_ffree;
  2431. buf->f_namemax = compatbuf->f_namelen;
  2432. }
  2433. int fuse_reply_open_compat(fuse_req_t req,
  2434. const struct fuse_file_info_compat *f)
  2435. {
  2436. struct fuse_open_out arg;
  2437. memset(&arg, 0, sizeof(arg));
  2438. fill_open_compat(&arg, f);
  2439. return send_reply_ok(req, &arg, sizeof(arg));
  2440. }
  2441. int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf)
  2442. {
  2443. struct statvfs newbuf;
  2444. memset(&newbuf, 0, sizeof(newbuf));
  2445. convert_statfs_compat(stbuf, &newbuf);
  2446. return fuse_reply_statfs(req, &newbuf);
  2447. }
  2448. struct fuse_session *fuse_lowlevel_new_compat(const char *opts,
  2449. const struct fuse_lowlevel_ops_compat *op,
  2450. size_t op_size, void *userdata)
  2451. {
  2452. struct fuse_session *se;
  2453. struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
  2454. if (opts &&
  2455. (fuse_opt_add_arg(&args, "") == -1 ||
  2456. fuse_opt_add_arg(&args, "-o") == -1 ||
  2457. fuse_opt_add_arg(&args, opts) == -1)) {
  2458. fuse_opt_free_args(&args);
  2459. return NULL;
  2460. }
  2461. se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
  2462. op_size, userdata);
  2463. fuse_opt_free_args(&args);
  2464. return se;
  2465. }
  2466. struct fuse_ll_compat_conf {
  2467. unsigned max_read;
  2468. int set_max_read;
  2469. };
  2470. static const struct fuse_opt fuse_ll_opts_compat[] = {
  2471. { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 },
  2472. { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 },
  2473. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
  2474. FUSE_OPT_END
  2475. };
  2476. int fuse_sync_compat_args(struct fuse_args *args)
  2477. {
  2478. struct fuse_ll_compat_conf conf;
  2479. memset(&conf, 0, sizeof(conf));
  2480. if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1)
  2481. return -1;
  2482. if (fuse_opt_insert_arg(args, 1, "-osync_read"))
  2483. return -1;
  2484. if (conf.set_max_read) {
  2485. char tmpbuf[64];
  2486. sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
  2487. if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
  2488. return -1;
  2489. }
  2490. return 0;
  2491. }
  2492. FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
  2493. FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
  2494. FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
  2495. #else /* __FreeBSD__ || __NetBSD__ */
  2496. int fuse_sync_compat_args(struct fuse_args *args)
  2497. {
  2498. (void) args;
  2499. return 0;
  2500. }
  2501. #endif /* __FreeBSD__ || __NetBSD__ */
  2502. struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args,
  2503. const struct fuse_lowlevel_ops_compat25 *op,
  2504. size_t op_size, void *userdata)
  2505. {
  2506. if (fuse_sync_compat_args(args) == -1)
  2507. return NULL;
  2508. return fuse_lowlevel_new_common(args,
  2509. (const struct fuse_lowlevel_ops *) op,
  2510. op_size, userdata);
  2511. }
  2512. FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");