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.

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