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.

2856 lines
72 KiB

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