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.

3550 lines
81 KiB

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