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.

3053 lines
74 KiB

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