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.

2462 lines
62 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
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
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 "config.h"
  9. #include "fuse_chan.h"
  10. #include "fuse_common_compat.h"
  11. #include "fuse_i.h"
  12. #include "fuse_kernel.h"
  13. #include "fuse_lowlevel_compat.h"
  14. #include "fuse_misc.h"
  15. #include "fuse_opt.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <stddef.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <limits.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <sys/file.h>
  25. #ifndef F_LINUX_SPECIFIC_BASE
  26. #define F_LINUX_SPECIFIC_BASE 1024
  27. #endif
  28. #ifndef F_SETPIPE_SZ
  29. #define F_SETPIPE_SZ (F_LINUX_SPECIFIC_BASE + 7)
  30. #endif
  31. #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
  32. #define OFFSET_MAX 0x7fffffffffffffffLL
  33. #define container_of(ptr, type, member) ({ \
  34. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  35. (type *)( (char *)__mptr - offsetof(type,member) );})
  36. struct fuse_pollhandle
  37. {
  38. uint64_t kh;
  39. fuse_chan_t *ch;
  40. struct fuse_ll *f;
  41. };
  42. static size_t pagesize;
  43. static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
  44. {
  45. pagesize = getpagesize();
  46. }
  47. static
  48. void
  49. convert_stat(const struct stat *stbuf_,
  50. struct fuse_attr *attr_)
  51. {
  52. attr_->ino = stbuf_->st_ino;
  53. attr_->mode = stbuf_->st_mode;
  54. attr_->nlink = stbuf_->st_nlink;
  55. attr_->uid = stbuf_->st_uid;
  56. attr_->gid = stbuf_->st_gid;
  57. attr_->rdev = stbuf_->st_rdev;
  58. attr_->size = stbuf_->st_size;
  59. attr_->blksize = stbuf_->st_blksize;
  60. attr_->blocks = stbuf_->st_blocks;
  61. attr_->atime = stbuf_->st_atime;
  62. attr_->mtime = stbuf_->st_mtime;
  63. attr_->ctime = stbuf_->st_ctime;
  64. attr_->atimensec = ST_ATIM_NSEC(stbuf_);
  65. attr_->mtimensec = ST_MTIM_NSEC(stbuf_);
  66. attr_->ctimensec = ST_CTIM_NSEC(stbuf_);
  67. }
  68. static
  69. void
  70. convert_attr(const struct fuse_setattr_in *attr_,
  71. struct stat *stbuf_)
  72. {
  73. stbuf_->st_mode = attr_->mode;
  74. stbuf_->st_uid = attr_->uid;
  75. stbuf_->st_gid = attr_->gid;
  76. stbuf_->st_size = attr_->size;
  77. stbuf_->st_atime = attr_->atime;
  78. stbuf_->st_mtime = attr_->mtime;
  79. stbuf_->st_ctime = attr_->ctime;
  80. ST_ATIM_NSEC_SET(stbuf_,attr_->atimensec);
  81. ST_MTIM_NSEC_SET(stbuf_,attr_->mtimensec);
  82. ST_CTIM_NSEC_SET(stbuf_,attr_->ctimensec);
  83. }
  84. static
  85. size_t
  86. iov_length(const struct iovec *iov,
  87. size_t count)
  88. {
  89. size_t seg;
  90. size_t ret = 0;
  91. for(seg = 0; seg < count; seg++)
  92. ret += iov[seg].iov_len;
  93. return ret;
  94. }
  95. static
  96. void
  97. list_init_req(struct fuse_req *req)
  98. {
  99. req->next = req;
  100. req->prev = req;
  101. }
  102. static
  103. void
  104. list_del_req(struct fuse_req *req)
  105. {
  106. struct fuse_req *prev = req->prev;
  107. struct fuse_req *next = req->next;
  108. prev->next = next;
  109. next->prev = prev;
  110. }
  111. static
  112. void
  113. list_add_req(struct fuse_req *req,
  114. struct fuse_req *next)
  115. {
  116. struct fuse_req *prev = next->prev;
  117. req->next = next;
  118. req->prev = prev;
  119. prev->next = req;
  120. next->prev = req;
  121. }
  122. static
  123. void
  124. destroy_req(fuse_req_t req)
  125. {
  126. pthread_mutex_destroy(&req->lock);
  127. free(req);
  128. }
  129. void
  130. fuse_free_req(fuse_req_t req)
  131. {
  132. int ctr;
  133. struct fuse_ll *f = req->f;
  134. pthread_mutex_lock(&f->lock);
  135. req->u.ni.func = NULL;
  136. req->u.ni.data = NULL;
  137. list_del_req(req);
  138. ctr = --req->ctr;
  139. pthread_mutex_unlock(&f->lock);
  140. if(!ctr)
  141. destroy_req(req);
  142. }
  143. static
  144. struct fuse_req*
  145. fuse_ll_alloc_req(struct fuse_ll *f)
  146. {
  147. struct fuse_req *req;
  148. req = (struct fuse_req *)calloc(1,sizeof(struct fuse_req));
  149. if(req == NULL)
  150. {
  151. fprintf(stderr, "fuse: failed to allocate request\n");
  152. }
  153. else
  154. {
  155. req->f = f;
  156. req->ctr = 1;
  157. list_init_req(req);
  158. fuse_mutex_init(&req->lock);
  159. }
  160. return req;
  161. }
  162. static
  163. int
  164. fuse_send_msg(struct fuse_ll *f,
  165. fuse_chan_t *ch,
  166. struct iovec *iov,
  167. int count)
  168. {
  169. struct fuse_out_header *out = iov[0].iov_base;
  170. out->len = iov_length(iov, count);
  171. if(f->debug) {
  172. if(out->unique == 0) {
  173. fprintf(stderr, "NOTIFY: code=%d length=%u\n",
  174. out->error, out->len);
  175. } else if(out->error) {
  176. fprintf(stderr,
  177. " unique: %llu, error: %i (%s), outsize: %i\n",
  178. (unsigned long long) out->unique, out->error,
  179. strerror(-out->error), out->len);
  180. } else {
  181. fprintf(stderr,
  182. " unique: %llu, success, outsize: %i\n",
  183. (unsigned long long) out->unique, out->len);
  184. }
  185. }
  186. return fuse_chan_send(ch,iov,count);
  187. }
  188. int
  189. fuse_send_reply_iov_nofree(fuse_req_t req,
  190. int error,
  191. struct iovec *iov,
  192. int count)
  193. {
  194. struct fuse_out_header out;
  195. if(error <= -1000 || error > 0)
  196. {
  197. fprintf(stderr, "fuse: bad error value: %i\n", error);
  198. error = -ERANGE;
  199. }
  200. out.unique = req->unique;
  201. out.error = error;
  202. iov[0].iov_base = &out;
  203. iov[0].iov_len = sizeof(struct fuse_out_header);
  204. return fuse_send_msg(req->f, req->ch, iov, count);
  205. }
  206. static
  207. int
  208. send_reply_iov(fuse_req_t req,
  209. int error,
  210. struct iovec *iov,
  211. int count)
  212. {
  213. int res;
  214. res = fuse_send_reply_iov_nofree(req,error,iov,count);
  215. fuse_free_req(req);
  216. return res;
  217. }
  218. static
  219. int
  220. send_reply(fuse_req_t req,
  221. int error,
  222. const void *arg,
  223. size_t argsize)
  224. {
  225. int count;
  226. struct iovec iov[2];
  227. 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 fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
  278. const char *name, const struct stat *stbuf, off_t off)
  279. {
  280. size_t entsize;
  281. (void) req;
  282. entsize = fuse_dirent_size(strlen(name));
  283. if(entsize <= bufsize && buf)
  284. fuse_add_dirent(buf, name, stbuf, off);
  285. return entsize;
  286. }
  287. static void convert_statfs(const struct statvfs *stbuf,
  288. struct fuse_kstatfs *kstatfs)
  289. {
  290. kstatfs->bsize = stbuf->f_bsize;
  291. kstatfs->frsize = stbuf->f_frsize;
  292. kstatfs->blocks = stbuf->f_blocks;
  293. kstatfs->bfree = stbuf->f_bfree;
  294. kstatfs->bavail = stbuf->f_bavail;
  295. kstatfs->files = stbuf->f_files;
  296. kstatfs->ffree = stbuf->f_ffree;
  297. kstatfs->namelen = stbuf->f_namemax;
  298. }
  299. static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
  300. {
  301. return send_reply(req, 0, arg, argsize);
  302. }
  303. int
  304. fuse_reply_err(fuse_req_t req_,
  305. int err_)
  306. {
  307. return send_reply(req_,-err_,NULL,0);
  308. }
  309. int
  310. fuse_reply_err_ENOSYS(fuse_req_t req_)
  311. {
  312. return fuse_reply_err(req_,ENOSYS);
  313. }
  314. void fuse_reply_none(fuse_req_t req)
  315. {
  316. if(req->ch)
  317. fuse_chan_send(req->ch, NULL, 0);
  318. fuse_free_req(req);
  319. }
  320. static unsigned long calc_timeout_sec(double t)
  321. {
  322. if(t > (double) ULONG_MAX)
  323. return ULONG_MAX;
  324. else if(t < 0.0)
  325. return 0;
  326. else
  327. return (unsigned long) t;
  328. }
  329. static unsigned int calc_timeout_nsec(double t)
  330. {
  331. double f = t - (double) calc_timeout_sec(t);
  332. if(f < 0.0)
  333. return 0;
  334. else if(f >= 0.999999999)
  335. return 999999999;
  336. else
  337. return (unsigned int) (f * 1.0e9);
  338. }
  339. static void fill_entry(struct fuse_entry_out *arg,
  340. const struct fuse_entry_param *e)
  341. {
  342. arg->nodeid = e->ino;
  343. arg->generation = e->generation;
  344. arg->entry_valid = calc_timeout_sec(e->entry_timeout);
  345. arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
  346. arg->attr_valid = calc_timeout_sec(e->attr_timeout);
  347. arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
  348. convert_stat(&e->attr, &arg->attr);
  349. }
  350. static void fill_open(struct fuse_open_out *arg,
  351. const struct fuse_file_info *f)
  352. {
  353. arg->fh = f->fh;
  354. if(f->direct_io)
  355. arg->open_flags |= FOPEN_DIRECT_IO;
  356. if(f->keep_cache)
  357. arg->open_flags |= FOPEN_KEEP_CACHE;
  358. if(f->nonseekable)
  359. arg->open_flags |= FOPEN_NONSEEKABLE;
  360. if(f->cache_readdir)
  361. arg->open_flags |= FOPEN_CACHE_DIR;
  362. }
  363. int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
  364. {
  365. struct fuse_entry_out arg;
  366. size_t size = req->f->conn.proto_minor < 9 ?
  367. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
  368. /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
  369. negative entry */
  370. if(!e->ino && req->f->conn.proto_minor < 4)
  371. return fuse_reply_err(req, ENOENT);
  372. memset(&arg, 0, sizeof(arg));
  373. fill_entry(&arg, e);
  374. return send_reply_ok(req, &arg, size);
  375. }
  376. int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
  377. const struct fuse_file_info *f)
  378. {
  379. char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
  380. size_t entrysize = req->f->conn.proto_minor < 9 ?
  381. FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
  382. struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
  383. struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
  384. memset(buf, 0, sizeof(buf));
  385. fill_entry(earg, e);
  386. fill_open(oarg, f);
  387. return send_reply_ok(req, buf,
  388. entrysize + sizeof(struct fuse_open_out));
  389. }
  390. int
  391. fuse_reply_attr(fuse_req_t req,
  392. const struct stat *attr,
  393. double attr_timeout)
  394. {
  395. struct fuse_attr_out arg;
  396. size_t size = req->f->conn.proto_minor < 9 ?
  397. FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
  398. memset(&arg,0,sizeof(arg));
  399. arg.attr_valid = calc_timeout_sec(attr_timeout);
  400. arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
  401. convert_stat(attr,&arg.attr);
  402. return send_reply_ok(req,&arg,size);
  403. }
  404. int
  405. fuse_reply_readlink(fuse_req_t req,
  406. const char *linkname)
  407. {
  408. return send_reply_ok(req,linkname,strlen(linkname));
  409. }
  410. int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
  411. {
  412. struct fuse_open_out arg;
  413. memset(&arg, 0, sizeof(arg));
  414. fill_open(&arg, f);
  415. return send_reply_ok(req, &arg, sizeof(arg));
  416. }
  417. int fuse_reply_write(fuse_req_t req, size_t count)
  418. {
  419. struct fuse_write_out arg;
  420. memset(&arg, 0, sizeof(arg));
  421. arg.size = count;
  422. return send_reply_ok(req, &arg, sizeof(arg));
  423. }
  424. int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
  425. {
  426. return send_reply_ok(req, buf, size);
  427. }
  428. static int fuse_send_data_iov_fallback(struct fuse_ll *f, fuse_chan_t *ch,
  429. struct iovec *iov, int iov_count,
  430. struct fuse_bufvec *buf,
  431. size_t len)
  432. {
  433. struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
  434. void *mbuf;
  435. int res;
  436. /* Optimize common case */
  437. if(buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
  438. !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
  439. /* FIXME: also avoid memory copy if there are multiple buffers
  440. but none of them contain an fd */
  441. iov[iov_count].iov_base = buf->buf[0].mem;
  442. iov[iov_count].iov_len = len;
  443. iov_count++;
  444. return fuse_send_msg(f, ch, iov, iov_count);
  445. }
  446. res = posix_memalign(&mbuf, pagesize, len);
  447. if(res != 0)
  448. return res;
  449. mem_buf.buf[0].mem = mbuf;
  450. res = fuse_buf_copy(&mem_buf, buf, 0);
  451. if(res < 0) {
  452. free(mbuf);
  453. return -res;
  454. }
  455. len = res;
  456. iov[iov_count].iov_base = mbuf;
  457. iov[iov_count].iov_len = len;
  458. iov_count++;
  459. res = fuse_send_msg(f, ch, iov, iov_count);
  460. free(mbuf);
  461. return res;
  462. }
  463. struct fuse_ll_pipe {
  464. size_t size;
  465. int can_grow;
  466. int pipe[2];
  467. };
  468. static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
  469. {
  470. close(llp->pipe[0]);
  471. close(llp->pipe[1]);
  472. free(llp);
  473. }
  474. static void fuse_ll_clear_pipe(struct fuse_ll *f)
  475. {
  476. struct fuse_ll_pipe *llp = pthread_getspecific(f->pipe_key);
  477. if(llp) {
  478. pthread_setspecific(f->pipe_key, NULL);
  479. fuse_ll_pipe_free(llp);
  480. }
  481. }
  482. static int fuse_send_data_iov(struct fuse_ll *f, fuse_chan_t *ch,
  483. struct iovec *iov, int iov_count,
  484. struct fuse_bufvec *buf, unsigned int flags)
  485. {
  486. size_t len = fuse_buf_size(buf);
  487. (void) flags;
  488. return fuse_send_data_iov_fallback(f, ch, iov, iov_count, buf, len);
  489. }
  490. int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
  491. enum fuse_buf_copy_flags flags)
  492. {
  493. struct iovec iov[2];
  494. struct fuse_out_header out;
  495. int res;
  496. iov[0].iov_base = &out;
  497. iov[0].iov_len = sizeof(struct fuse_out_header);
  498. out.unique = req->unique;
  499. out.error = 0;
  500. res = fuse_send_data_iov(req->f, req->ch, iov, 1, bufv, flags);
  501. if(res <= 0) {
  502. fuse_free_req(req);
  503. return res;
  504. } else {
  505. return fuse_reply_err(req, res);
  506. }
  507. }
  508. int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
  509. {
  510. struct fuse_statfs_out arg;
  511. size_t size = req->f->conn.proto_minor < 4 ?
  512. FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
  513. memset(&arg, 0, sizeof(arg));
  514. convert_statfs(stbuf, &arg.st);
  515. return send_reply_ok(req, &arg, size);
  516. }
  517. int fuse_reply_xattr(fuse_req_t req, size_t count)
  518. {
  519. struct fuse_getxattr_out arg;
  520. memset(&arg, 0, sizeof(arg));
  521. arg.size = count;
  522. return send_reply_ok(req, &arg, sizeof(arg));
  523. }
  524. int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
  525. {
  526. struct fuse_lk_out arg;
  527. memset(&arg, 0, sizeof(arg));
  528. arg.lk.type = lock->l_type;
  529. if(lock->l_type != F_UNLCK) {
  530. arg.lk.start = lock->l_start;
  531. if(lock->l_len == 0)
  532. arg.lk.end = OFFSET_MAX;
  533. else
  534. arg.lk.end = lock->l_start + lock->l_len - 1;
  535. }
  536. arg.lk.pid = lock->l_pid;
  537. return send_reply_ok(req, &arg, sizeof(arg));
  538. }
  539. int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
  540. {
  541. struct fuse_bmap_out arg;
  542. memset(&arg, 0, sizeof(arg));
  543. arg.block = idx;
  544. return send_reply_ok(req, &arg, sizeof(arg));
  545. }
  546. static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
  547. size_t count)
  548. {
  549. struct fuse_ioctl_iovec *fiov;
  550. size_t i;
  551. fiov = malloc(sizeof(fiov[0]) * count);
  552. if(!fiov)
  553. return NULL;
  554. for (i = 0; i < count; i++) {
  555. fiov[i].base = (uintptr_t) iov[i].iov_base;
  556. fiov[i].len = iov[i].iov_len;
  557. }
  558. return fiov;
  559. }
  560. int fuse_reply_ioctl_retry(fuse_req_t req,
  561. const struct iovec *in_iov, size_t in_count,
  562. const struct iovec *out_iov, size_t out_count)
  563. {
  564. struct fuse_ioctl_out arg;
  565. struct fuse_ioctl_iovec *in_fiov = NULL;
  566. struct fuse_ioctl_iovec *out_fiov = NULL;
  567. struct iovec iov[4];
  568. size_t count = 1;
  569. int res;
  570. memset(&arg, 0, sizeof(arg));
  571. arg.flags |= FUSE_IOCTL_RETRY;
  572. arg.in_iovs = in_count;
  573. arg.out_iovs = out_count;
  574. iov[count].iov_base = &arg;
  575. iov[count].iov_len = sizeof(arg);
  576. count++;
  577. if(req->f->conn.proto_minor < 16) {
  578. if(in_count) {
  579. iov[count].iov_base = (void *)in_iov;
  580. iov[count].iov_len = sizeof(in_iov[0]) * in_count;
  581. count++;
  582. }
  583. if(out_count) {
  584. iov[count].iov_base = (void *)out_iov;
  585. iov[count].iov_len = sizeof(out_iov[0]) * out_count;
  586. count++;
  587. }
  588. } else {
  589. /* Can't handle non-compat 64bit ioctls on 32bit */
  590. if(sizeof(void *) == 4 && req->ioctl_64bit) {
  591. res = fuse_reply_err(req, EINVAL);
  592. goto out;
  593. }
  594. if(in_count) {
  595. in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
  596. if(!in_fiov)
  597. goto enomem;
  598. iov[count].iov_base = (void *)in_fiov;
  599. iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
  600. count++;
  601. }
  602. if(out_count) {
  603. out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
  604. if(!out_fiov)
  605. goto enomem;
  606. iov[count].iov_base = (void *)out_fiov;
  607. iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
  608. count++;
  609. }
  610. }
  611. res = send_reply_iov(req, 0, iov, count);
  612. out:
  613. free(in_fiov);
  614. free(out_fiov);
  615. return res;
  616. enomem:
  617. res = fuse_reply_err(req, ENOMEM);
  618. goto out;
  619. }
  620. int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, uint32_t size)
  621. {
  622. int count;
  623. struct iovec iov[3];
  624. struct fuse_ioctl_out arg;
  625. arg.result = result;
  626. arg.flags = 0;
  627. arg.in_iovs = 0;
  628. arg.out_iovs = 0;
  629. count = 1;
  630. iov[count].iov_base = &arg;
  631. iov[count].iov_len = sizeof(arg);
  632. count++;
  633. if(size)
  634. {
  635. iov[count].iov_base = (char*)buf;
  636. iov[count].iov_len = size;
  637. count++;
  638. }
  639. return send_reply_iov(req, 0, iov, count);
  640. }
  641. int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
  642. int count)
  643. {
  644. struct iovec *padded_iov;
  645. struct fuse_ioctl_out arg;
  646. int res;
  647. padded_iov = malloc((count + 2) * sizeof(struct iovec));
  648. if(padded_iov == NULL)
  649. return fuse_reply_err(req, ENOMEM);
  650. memset(&arg, 0, sizeof(arg));
  651. arg.result = result;
  652. padded_iov[1].iov_base = &arg;
  653. padded_iov[1].iov_len = sizeof(arg);
  654. memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
  655. res = send_reply_iov(req, 0, padded_iov, count + 2);
  656. free(padded_iov);
  657. return res;
  658. }
  659. int fuse_reply_poll(fuse_req_t req, unsigned revents)
  660. {
  661. struct fuse_poll_out arg;
  662. memset(&arg, 0, sizeof(arg));
  663. arg.revents = revents;
  664. return send_reply_ok(req, &arg, sizeof(arg));
  665. }
  666. static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  667. {
  668. char *name = (char *) inarg;
  669. if(req->f->op.lookup)
  670. req->f->op.lookup(req, nodeid, name);
  671. else
  672. fuse_reply_err(req, ENOSYS);
  673. }
  674. static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  675. {
  676. struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
  677. if(req->f->op.forget)
  678. req->f->op.forget(req, nodeid, arg->nlookup);
  679. else
  680. fuse_reply_none(req);
  681. }
  682. static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
  683. const void *inarg)
  684. {
  685. struct fuse_batch_forget_in *arg = (void *) inarg;
  686. struct fuse_forget_one *param = (void *) PARAM(arg);
  687. unsigned int i;
  688. (void) nodeid;
  689. if(req->f->op.forget_multi) {
  690. req->f->op.forget_multi(req, arg->count,
  691. (struct fuse_forget_data *) param);
  692. } else if(req->f->op.forget) {
  693. for (i = 0; i < arg->count; i++) {
  694. struct fuse_forget_one *forget = &param[i];
  695. struct fuse_req *dummy_req;
  696. dummy_req = fuse_ll_alloc_req(req->f);
  697. if(dummy_req == NULL)
  698. break;
  699. dummy_req->unique = req->unique;
  700. dummy_req->ctx = req->ctx;
  701. dummy_req->ch = NULL;
  702. req->f->op.forget(dummy_req, forget->nodeid,
  703. forget->nlookup);
  704. }
  705. fuse_reply_none(req);
  706. } else {
  707. fuse_reply_none(req);
  708. }
  709. }
  710. static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  711. {
  712. struct fuse_file_info *fip = NULL;
  713. struct fuse_file_info fi;
  714. if(req->f->conn.proto_minor >= 9) {
  715. struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
  716. if(arg->getattr_flags & FUSE_GETATTR_FH) {
  717. memset(&fi, 0, sizeof(fi));
  718. fi.fh = arg->fh;
  719. fip = &fi;
  720. }
  721. }
  722. if(req->f->op.getattr)
  723. req->f->op.getattr(req, nodeid, fip);
  724. else
  725. fuse_reply_err(req, ENOSYS);
  726. }
  727. static
  728. void
  729. do_setattr(fuse_req_t req_,
  730. fuse_ino_t nodeid_,
  731. const void *inarg_)
  732. {
  733. struct stat stbuf;
  734. struct fuse_file_info *fi;
  735. struct fuse_file_info fi_store;
  736. struct fuse_setattr_in *arg;
  737. if(req_->f->op.setattr == NULL)
  738. return (void)fuse_reply_err(req_,ENOSYS);
  739. fi = NULL;
  740. arg = (struct fuse_setattr_in*)inarg_;
  741. memset(&stbuf,0,sizeof(stbuf));
  742. convert_attr(arg,&stbuf);
  743. if(arg->valid & FATTR_FH)
  744. {
  745. arg->valid &= ~FATTR_FH;
  746. memset(&fi_store,0,sizeof(fi_store));
  747. fi = &fi_store;
  748. fi->fh = arg->fh;
  749. }
  750. arg->valid &=
  751. (FATTR_MODE |
  752. FATTR_UID |
  753. FATTR_GID |
  754. FATTR_SIZE |
  755. FATTR_ATIME |
  756. FATTR_MTIME |
  757. FATTR_CTIME |
  758. FATTR_ATIME_NOW |
  759. FATTR_MTIME_NOW);
  760. req_->f->op.setattr(req_,nodeid_,&stbuf,arg->valid,fi);
  761. }
  762. static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  763. {
  764. struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
  765. if(req->f->op.access)
  766. req->f->op.access(req, nodeid, arg->mask);
  767. else
  768. fuse_reply_err(req, ENOSYS);
  769. }
  770. static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  771. {
  772. (void) inarg;
  773. if(req->f->op.readlink)
  774. req->f->op.readlink(req, nodeid);
  775. else
  776. fuse_reply_err(req, ENOSYS);
  777. }
  778. static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  779. {
  780. struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
  781. char *name = PARAM(arg);
  782. if(req->f->conn.proto_minor >= 12)
  783. req->ctx.umask = arg->umask;
  784. else
  785. name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
  786. if(req->f->op.mknod)
  787. req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
  788. else
  789. fuse_reply_err(req, ENOSYS);
  790. }
  791. static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  792. {
  793. struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
  794. if(req->f->conn.proto_minor >= 12)
  795. req->ctx.umask = arg->umask;
  796. if(req->f->op.mkdir)
  797. req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
  798. else
  799. fuse_reply_err(req, ENOSYS);
  800. }
  801. static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  802. {
  803. char *name = (char *) inarg;
  804. if(req->f->op.unlink)
  805. req->f->op.unlink(req, nodeid, name);
  806. else
  807. fuse_reply_err(req, ENOSYS);
  808. }
  809. static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  810. {
  811. char *name = (char *) inarg;
  812. if(req->f->op.rmdir)
  813. req->f->op.rmdir(req, nodeid, name);
  814. else
  815. fuse_reply_err(req, ENOSYS);
  816. }
  817. static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  818. {
  819. char *name = (char *) inarg;
  820. char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
  821. if(req->f->op.symlink)
  822. req->f->op.symlink(req, linkname, nodeid, name);
  823. else
  824. fuse_reply_err(req, ENOSYS);
  825. }
  826. static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  827. {
  828. struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
  829. char *oldname = PARAM(arg);
  830. char *newname = oldname + strlen(oldname) + 1;
  831. if(req->f->op.rename)
  832. req->f->op.rename(req, nodeid, oldname, arg->newdir, newname);
  833. else
  834. fuse_reply_err(req, ENOSYS);
  835. }
  836. static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  837. {
  838. struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
  839. if(req->f->op.link)
  840. req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
  841. else
  842. fuse_reply_err(req, ENOSYS);
  843. }
  844. static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  845. {
  846. struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
  847. if(req->f->op.create) {
  848. struct fuse_file_info fi;
  849. char *name = PARAM(arg);
  850. memset(&fi, 0, sizeof(fi));
  851. fi.flags = arg->flags;
  852. if(req->f->conn.proto_minor >= 12)
  853. req->ctx.umask = arg->umask;
  854. else
  855. name = (char *) inarg + sizeof(struct fuse_open_in);
  856. req->f->op.create(req, nodeid, name, arg->mode, &fi);
  857. } else
  858. fuse_reply_err(req, ENOSYS);
  859. }
  860. static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  861. {
  862. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  863. struct fuse_file_info fi;
  864. memset(&fi, 0, sizeof(fi));
  865. fi.flags = arg->flags;
  866. if(req->f->op.open)
  867. req->f->op.open(req, nodeid, &fi);
  868. else
  869. fuse_reply_open(req, &fi);
  870. }
  871. static
  872. void
  873. do_read(fuse_req_t req,
  874. fuse_ino_t nodeid,
  875. const void *inarg)
  876. {
  877. struct fuse_file_info fi;
  878. struct fuse_read_in *arg = (struct fuse_read_in *)inarg;
  879. if(req->f->op.read == NULL)
  880. return (void)fuse_reply_err_ENOSYS(req);
  881. memset(&fi,0,sizeof(fi));
  882. fi.fh = arg->fh;
  883. fi.lock_owner = arg->lock_owner;
  884. fi.flags = arg->flags;
  885. req->f->op.read(req,nodeid,arg->size,arg->offset,&fi);
  886. }
  887. static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  888. {
  889. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  890. struct fuse_file_info fi;
  891. char *param;
  892. memset(&fi, 0, sizeof(fi));
  893. fi.fh = arg->fh;
  894. fi.writepage = arg->write_flags & 1;
  895. if(req->f->conn.proto_minor < 9) {
  896. param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  897. } else {
  898. fi.lock_owner = arg->lock_owner;
  899. fi.flags = arg->flags;
  900. param = PARAM(arg);
  901. }
  902. if(req->f->op.write)
  903. req->f->op.write(req, nodeid, param, arg->size,
  904. arg->offset, &fi);
  905. else
  906. fuse_reply_err(req, ENOSYS);
  907. }
  908. static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
  909. const struct fuse_buf *ibuf)
  910. {
  911. struct fuse_ll *f = req->f;
  912. struct fuse_bufvec bufv = {
  913. .buf[0] = *ibuf,
  914. .count = 1,
  915. };
  916. struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
  917. struct fuse_file_info fi;
  918. memset(&fi, 0, sizeof(fi));
  919. fi.fh = arg->fh;
  920. fi.writepage = arg->write_flags & 1;
  921. if(req->f->conn.proto_minor < 9) {
  922. bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  923. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  924. FUSE_COMPAT_WRITE_IN_SIZE;
  925. assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
  926. } else {
  927. fi.lock_owner = arg->lock_owner;
  928. fi.flags = arg->flags;
  929. if(!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
  930. bufv.buf[0].mem = PARAM(arg);
  931. bufv.buf[0].size -= sizeof(struct fuse_in_header) +
  932. sizeof(struct fuse_write_in);
  933. }
  934. if(bufv.buf[0].size < arg->size) {
  935. fprintf(stderr, "fuse: do_write_buf: buffer size too small\n");
  936. fuse_reply_err(req, EIO);
  937. goto out;
  938. }
  939. bufv.buf[0].size = arg->size;
  940. req->f->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
  941. out:
  942. /* Need to reset the pipe if ->write_buf() didn't consume all data */
  943. if((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
  944. fuse_ll_clear_pipe(f);
  945. }
  946. static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  947. {
  948. struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
  949. struct fuse_file_info fi;
  950. memset(&fi, 0, sizeof(fi));
  951. fi.fh = arg->fh;
  952. fi.flush = 1;
  953. if(req->f->conn.proto_minor >= 7)
  954. fi.lock_owner = arg->lock_owner;
  955. if(req->f->op.flush)
  956. req->f->op.flush(req, nodeid, &fi);
  957. else
  958. fuse_reply_err(req, ENOSYS);
  959. }
  960. static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  961. {
  962. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  963. struct fuse_file_info fi;
  964. memset(&fi, 0, sizeof(fi));
  965. fi.flags = arg->flags;
  966. fi.fh = arg->fh;
  967. if(req->f->conn.proto_minor >= 8) {
  968. fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
  969. fi.lock_owner = arg->lock_owner;
  970. }
  971. if(arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
  972. fi.flock_release = 1;
  973. fi.lock_owner = arg->lock_owner;
  974. }
  975. if(req->f->op.release)
  976. req->f->op.release(req, nodeid, &fi);
  977. else
  978. fuse_reply_err(req, 0);
  979. }
  980. static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  981. {
  982. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  983. struct fuse_file_info fi;
  984. memset(&fi, 0, sizeof(fi));
  985. fi.fh = arg->fh;
  986. if(req->f->op.fsync)
  987. req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi);
  988. else
  989. fuse_reply_err(req, ENOSYS);
  990. }
  991. static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  992. {
  993. struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
  994. struct fuse_file_info fi;
  995. memset(&fi, 0, sizeof(fi));
  996. fi.flags = arg->flags;
  997. if(req->f->op.opendir)
  998. req->f->op.opendir(req, nodeid, &fi);
  999. else
  1000. fuse_reply_open(req, &fi);
  1001. }
  1002. static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1003. {
  1004. struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
  1005. struct fuse_file_info fi;
  1006. memset(&fi, 0, sizeof(fi));
  1007. fi.fh = arg->fh;
  1008. if(req->f->op.readdir)
  1009. req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
  1010. else
  1011. fuse_reply_err(req, ENOSYS);
  1012. }
  1013. static
  1014. void
  1015. do_readdir_plus(fuse_req_t req_,
  1016. fuse_ino_t nodeid_,
  1017. const void *inarg_)
  1018. {
  1019. const struct fuse_read_in *arg;
  1020. struct fuse_file_info ffi = {0};
  1021. arg = (struct fuse_read_in*)inarg_;
  1022. ffi.fh = arg->fh;
  1023. if(req_->f->op.readdir_plus)
  1024. req_->f->op.readdir_plus(req_,nodeid_,arg->size,arg->offset,&ffi);
  1025. else
  1026. fuse_reply_err(req_,ENOSYS);
  1027. }
  1028. static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1029. {
  1030. struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
  1031. struct fuse_file_info fi;
  1032. memset(&fi, 0, sizeof(fi));
  1033. fi.flags = arg->flags;
  1034. fi.fh = arg->fh;
  1035. if(req->f->op.releasedir)
  1036. req->f->op.releasedir(req, nodeid, &fi);
  1037. else
  1038. fuse_reply_err(req, 0);
  1039. }
  1040. static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1041. {
  1042. struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
  1043. struct fuse_file_info fi;
  1044. memset(&fi, 0, sizeof(fi));
  1045. fi.fh = arg->fh;
  1046. if(req->f->op.fsyncdir)
  1047. req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi);
  1048. else
  1049. fuse_reply_err(req, ENOSYS);
  1050. }
  1051. static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1052. {
  1053. (void) nodeid;
  1054. (void) inarg;
  1055. if(req->f->op.statfs)
  1056. req->f->op.statfs(req, nodeid);
  1057. else {
  1058. struct statvfs buf = {
  1059. .f_namemax = 255,
  1060. .f_bsize = 512,
  1061. };
  1062. fuse_reply_statfs(req, &buf);
  1063. }
  1064. }
  1065. static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1066. {
  1067. struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
  1068. char *name = PARAM(arg);
  1069. char *value = name + strlen(name) + 1;
  1070. if(req->f->op.setxattr)
  1071. req->f->op.setxattr(req, nodeid, name, value, arg->size,
  1072. arg->flags);
  1073. else
  1074. fuse_reply_err(req, ENOSYS);
  1075. }
  1076. static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1077. {
  1078. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1079. if(req->f->op.getxattr)
  1080. req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size);
  1081. else
  1082. fuse_reply_err(req, ENOSYS);
  1083. }
  1084. static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1085. {
  1086. struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
  1087. if(req->f->op.listxattr)
  1088. req->f->op.listxattr(req, nodeid, arg->size);
  1089. else
  1090. fuse_reply_err(req, ENOSYS);
  1091. }
  1092. static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1093. {
  1094. char *name = (char *) inarg;
  1095. if(req->f->op.removexattr)
  1096. req->f->op.removexattr(req, nodeid, name);
  1097. else
  1098. fuse_reply_err(req, ENOSYS);
  1099. }
  1100. static void convert_fuse_file_lock(struct fuse_file_lock *fl,
  1101. struct flock *flock)
  1102. {
  1103. memset(flock, 0, sizeof(struct flock));
  1104. flock->l_type = fl->type;
  1105. flock->l_whence = SEEK_SET;
  1106. flock->l_start = fl->start;
  1107. if(fl->end == OFFSET_MAX)
  1108. flock->l_len = 0;
  1109. else
  1110. flock->l_len = fl->end - fl->start + 1;
  1111. flock->l_pid = fl->pid;
  1112. }
  1113. static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1114. {
  1115. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1116. struct fuse_file_info fi;
  1117. struct flock flock;
  1118. memset(&fi, 0, sizeof(fi));
  1119. fi.fh = arg->fh;
  1120. fi.lock_owner = arg->owner;
  1121. convert_fuse_file_lock(&arg->lk, &flock);
  1122. if(req->f->op.getlk)
  1123. req->f->op.getlk(req, nodeid, &fi, &flock);
  1124. else
  1125. fuse_reply_err(req, ENOSYS);
  1126. }
  1127. static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
  1128. const void *inarg, int sleep)
  1129. {
  1130. struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
  1131. struct fuse_file_info fi;
  1132. struct flock flock;
  1133. memset(&fi, 0, sizeof(fi));
  1134. fi.fh = arg->fh;
  1135. fi.lock_owner = arg->owner;
  1136. if(arg->lk_flags & FUSE_LK_FLOCK) {
  1137. int op = 0;
  1138. switch (arg->lk.type) {
  1139. case F_RDLCK:
  1140. op = LOCK_SH;
  1141. break;
  1142. case F_WRLCK:
  1143. op = LOCK_EX;
  1144. break;
  1145. case F_UNLCK:
  1146. op = LOCK_UN;
  1147. break;
  1148. }
  1149. if(!sleep)
  1150. op |= LOCK_NB;
  1151. if(req->f->op.flock)
  1152. req->f->op.flock(req, nodeid, &fi, op);
  1153. else
  1154. fuse_reply_err(req, ENOSYS);
  1155. } else {
  1156. convert_fuse_file_lock(&arg->lk, &flock);
  1157. if(req->f->op.setlk)
  1158. req->f->op.setlk(req, nodeid, &fi, &flock, sleep);
  1159. else
  1160. fuse_reply_err(req, ENOSYS);
  1161. }
  1162. }
  1163. static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1164. {
  1165. do_setlk_common(req, nodeid, inarg, 0);
  1166. }
  1167. static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1168. {
  1169. do_setlk_common(req, nodeid, inarg, 1);
  1170. }
  1171. static int find_interrupted(struct fuse_ll *f, struct fuse_req *req)
  1172. {
  1173. struct fuse_req *curr;
  1174. for (curr = f->list.next; curr != &f->list; curr = curr->next) {
  1175. if(curr->unique == req->u.i.unique) {
  1176. fuse_interrupt_func_t func;
  1177. void *data;
  1178. curr->ctr++;
  1179. pthread_mutex_unlock(&f->lock);
  1180. /* Ugh, ugly locking */
  1181. pthread_mutex_lock(&curr->lock);
  1182. pthread_mutex_lock(&f->lock);
  1183. curr->interrupted = 1;
  1184. func = curr->u.ni.func;
  1185. data = curr->u.ni.data;
  1186. pthread_mutex_unlock(&f->lock);
  1187. if(func)
  1188. func(curr, data);
  1189. pthread_mutex_unlock(&curr->lock);
  1190. pthread_mutex_lock(&f->lock);
  1191. curr->ctr--;
  1192. if(!curr->ctr)
  1193. destroy_req(curr);
  1194. return 1;
  1195. }
  1196. }
  1197. for (curr = f->interrupts.next; curr != &f->interrupts;
  1198. curr = curr->next) {
  1199. if(curr->u.i.unique == req->u.i.unique)
  1200. return 1;
  1201. }
  1202. return 0;
  1203. }
  1204. static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1205. {
  1206. struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
  1207. struct fuse_ll *f = req->f;
  1208. (void) nodeid;
  1209. if(f->debug)
  1210. fprintf(stderr, "INTERRUPT: %llu\n",
  1211. (unsigned long long) arg->unique);
  1212. req->u.i.unique = arg->unique;
  1213. pthread_mutex_lock(&f->lock);
  1214. if(find_interrupted(f, req))
  1215. destroy_req(req);
  1216. else
  1217. list_add_req(req, &f->interrupts);
  1218. pthread_mutex_unlock(&f->lock);
  1219. }
  1220. static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req)
  1221. {
  1222. struct fuse_req *curr;
  1223. for (curr = f->interrupts.next; curr != &f->interrupts;
  1224. curr = curr->next) {
  1225. if(curr->u.i.unique == req->unique) {
  1226. req->interrupted = 1;
  1227. list_del_req(curr);
  1228. free(curr);
  1229. return NULL;
  1230. }
  1231. }
  1232. curr = f->interrupts.next;
  1233. if(curr != &f->interrupts) {
  1234. list_del_req(curr);
  1235. list_init_req(curr);
  1236. return curr;
  1237. } else
  1238. return NULL;
  1239. }
  1240. static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1241. {
  1242. struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
  1243. if(req->f->op.bmap)
  1244. req->f->op.bmap(req, nodeid, arg->blocksize, arg->block);
  1245. else
  1246. fuse_reply_err(req, ENOSYS);
  1247. }
  1248. static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1249. {
  1250. struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
  1251. unsigned int flags = arg->flags;
  1252. void *in_buf = arg->in_size ? PARAM(arg) : NULL;
  1253. struct fuse_file_info fi;
  1254. if(flags & FUSE_IOCTL_DIR &&
  1255. !(req->f->conn.want & FUSE_CAP_IOCTL_DIR)) {
  1256. fuse_reply_err(req, ENOTTY);
  1257. return;
  1258. }
  1259. memset(&fi, 0, sizeof(fi));
  1260. fi.fh = arg->fh;
  1261. if(sizeof(void *) == 4 && req->f->conn.proto_minor >= 16 &&
  1262. !(flags & FUSE_IOCTL_32BIT)) {
  1263. req->ioctl_64bit = 1;
  1264. }
  1265. if(req->f->op.ioctl)
  1266. req->f->op.ioctl(req, nodeid, arg->cmd,
  1267. (void *)(uintptr_t)arg->arg, &fi, flags,
  1268. in_buf, arg->in_size, arg->out_size);
  1269. else
  1270. fuse_reply_err(req, ENOSYS);
  1271. }
  1272. void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
  1273. {
  1274. free(ph);
  1275. }
  1276. static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1277. {
  1278. struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
  1279. struct fuse_file_info fi;
  1280. memset(&fi, 0, sizeof(fi));
  1281. fi.fh = arg->fh;
  1282. if(req->f->op.poll) {
  1283. struct fuse_pollhandle *ph = NULL;
  1284. if(arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
  1285. ph = malloc(sizeof(struct fuse_pollhandle));
  1286. if(ph == NULL) {
  1287. fuse_reply_err(req, ENOMEM);
  1288. return;
  1289. }
  1290. ph->kh = arg->kh;
  1291. ph->ch = req->ch;
  1292. ph->f = req->f;
  1293. }
  1294. req->f->op.poll(req, nodeid, &fi, ph);
  1295. } else {
  1296. fuse_reply_err(req, ENOSYS);
  1297. }
  1298. }
  1299. static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1300. {
  1301. struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
  1302. struct fuse_file_info fi;
  1303. memset(&fi, 0, sizeof(fi));
  1304. fi.fh = arg->fh;
  1305. if(req->f->op.fallocate)
  1306. req->f->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
  1307. else
  1308. fuse_reply_err(req, ENOSYS);
  1309. }
  1310. static
  1311. void
  1312. do_init(fuse_req_t req,
  1313. fuse_ino_t nodeid,
  1314. const void *inarg)
  1315. {
  1316. struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
  1317. struct fuse_init_out outarg;
  1318. struct fuse_ll *f = req->f;
  1319. size_t bufsize = req->ch->bufsize;
  1320. (void) nodeid;
  1321. if(f->debug) {
  1322. fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor);
  1323. if(arg->major == 7 && arg->minor >= 6) {
  1324. fprintf(stderr, "flags=0x%08x\n", arg->flags);
  1325. fprintf(stderr, "max_readahead=0x%08x\n",
  1326. arg->max_readahead);
  1327. }
  1328. }
  1329. f->conn.proto_major = arg->major;
  1330. f->conn.proto_minor = arg->minor;
  1331. f->conn.capable = 0;
  1332. f->conn.want = 0;
  1333. memset(&outarg, 0, sizeof(outarg));
  1334. outarg.major = FUSE_KERNEL_VERSION;
  1335. outarg.minor = FUSE_KERNEL_MINOR_VERSION;
  1336. outarg.max_pages = FUSE_MSG_DEFAULT_PAGES;
  1337. if(arg->major < 7) {
  1338. fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n",
  1339. arg->major, arg->minor);
  1340. fuse_reply_err(req, EPROTO);
  1341. return;
  1342. }
  1343. if(arg->major > 7) {
  1344. /* Wait for a second INIT request with a 7.X version */
  1345. send_reply_ok(req, &outarg, sizeof(outarg));
  1346. return;
  1347. }
  1348. if(arg->minor >= 6) {
  1349. if(arg->max_readahead < f->conn.max_readahead)
  1350. f->conn.max_readahead = arg->max_readahead;
  1351. if(arg->flags & FUSE_ASYNC_READ)
  1352. f->conn.capable |= FUSE_CAP_ASYNC_READ;
  1353. if(arg->flags & FUSE_POSIX_LOCKS)
  1354. f->conn.capable |= FUSE_CAP_POSIX_LOCKS;
  1355. if(arg->flags & FUSE_ATOMIC_O_TRUNC)
  1356. f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
  1357. if(arg->flags & FUSE_EXPORT_SUPPORT)
  1358. f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
  1359. if(arg->flags & FUSE_BIG_WRITES)
  1360. f->conn.capable |= FUSE_CAP_BIG_WRITES;
  1361. if(arg->flags & FUSE_DONT_MASK)
  1362. f->conn.capable |= FUSE_CAP_DONT_MASK;
  1363. if(arg->flags & FUSE_FLOCK_LOCKS)
  1364. f->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
  1365. if(arg->flags & FUSE_POSIX_ACL)
  1366. f->conn.capable |= FUSE_CAP_POSIX_ACL;
  1367. if(arg->flags & FUSE_CACHE_SYMLINKS)
  1368. f->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
  1369. if(arg->flags & FUSE_ASYNC_DIO)
  1370. f->conn.capable |= FUSE_CAP_ASYNC_DIO;
  1371. if(arg->flags & FUSE_PARALLEL_DIROPS)
  1372. f->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
  1373. if(arg->flags & FUSE_MAX_PAGES)
  1374. f->conn.capable |= FUSE_CAP_MAX_PAGES;
  1375. if(arg->flags & FUSE_WRITEBACK_CACHE)
  1376. f->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
  1377. if (arg->flags & FUSE_DO_READDIRPLUS)
  1378. f->conn.capable |= FUSE_CAP_READDIR_PLUS;
  1379. if (arg->flags & FUSE_READDIRPLUS_AUTO)
  1380. f->conn.capable |= FUSE_CAP_READDIR_PLUS_AUTO;
  1381. } else {
  1382. f->conn.want &= ~FUSE_CAP_ASYNC_READ;
  1383. f->conn.max_readahead = 0;
  1384. }
  1385. if(req->f->conn.proto_minor >= 14)
  1386. {
  1387. f->conn.capable |= (FUSE_CAP_SPLICE_WRITE|FUSE_CAP_SPLICE_MOVE);
  1388. if(f->splice_write)
  1389. f->conn.want |= FUSE_CAP_SPLICE_WRITE;
  1390. if(f->splice_move)
  1391. f->conn.want |= FUSE_CAP_SPLICE_MOVE;
  1392. f->conn.capable |= FUSE_CAP_SPLICE_READ;
  1393. if(f->splice_read)
  1394. f->conn.want |= FUSE_CAP_SPLICE_READ;
  1395. }
  1396. if(req->f->conn.proto_minor >= 18)
  1397. f->conn.capable |= FUSE_CAP_IOCTL_DIR;
  1398. if(f->op.getlk && f->op.setlk && !f->no_remote_posix_lock)
  1399. f->conn.want |= FUSE_CAP_POSIX_LOCKS;
  1400. if(f->op.flock && !f->no_remote_flock)
  1401. f->conn.want |= FUSE_CAP_FLOCK_LOCKS;
  1402. if(bufsize < FUSE_MIN_READ_BUFFER) {
  1403. fprintf(stderr, "fuse: warning: buffer size too small: %zu\n",
  1404. bufsize);
  1405. bufsize = FUSE_MIN_READ_BUFFER;
  1406. }
  1407. bufsize -= 4096;
  1408. if(bufsize < f->conn.max_write)
  1409. f->conn.max_write = bufsize;
  1410. f->got_init = 1;
  1411. if(f->op.init)
  1412. f->op.init(f->userdata, &f->conn);
  1413. if(f->no_splice_read)
  1414. f->conn.want &= ~FUSE_CAP_SPLICE_READ;
  1415. if(f->no_splice_write)
  1416. f->conn.want &= ~FUSE_CAP_SPLICE_WRITE;
  1417. if(f->no_splice_move)
  1418. f->conn.want &= ~FUSE_CAP_SPLICE_MOVE;
  1419. if((arg->flags & FUSE_MAX_PAGES) && (f->conn.want & FUSE_CAP_MAX_PAGES))
  1420. {
  1421. outarg.flags |= FUSE_MAX_PAGES;
  1422. outarg.max_pages = f->conn.max_pages;
  1423. }
  1424. if(f->conn.want & FUSE_CAP_ASYNC_READ)
  1425. outarg.flags |= FUSE_ASYNC_READ;
  1426. if(f->conn.want & FUSE_CAP_POSIX_LOCKS)
  1427. outarg.flags |= FUSE_POSIX_LOCKS;
  1428. if(f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
  1429. outarg.flags |= FUSE_ATOMIC_O_TRUNC;
  1430. if(f->conn.want & FUSE_CAP_EXPORT_SUPPORT)
  1431. outarg.flags |= FUSE_EXPORT_SUPPORT;
  1432. if(f->conn.want & FUSE_CAP_BIG_WRITES)
  1433. outarg.flags |= FUSE_BIG_WRITES;
  1434. if(f->conn.want & FUSE_CAP_DONT_MASK)
  1435. outarg.flags |= FUSE_DONT_MASK;
  1436. if(f->conn.want & FUSE_CAP_FLOCK_LOCKS)
  1437. outarg.flags |= FUSE_FLOCK_LOCKS;
  1438. if(f->conn.want & FUSE_CAP_POSIX_ACL)
  1439. outarg.flags |= FUSE_POSIX_ACL;
  1440. if(f->conn.want & FUSE_CAP_CACHE_SYMLINKS)
  1441. outarg.flags |= FUSE_CACHE_SYMLINKS;
  1442. if(f->conn.want & FUSE_CAP_ASYNC_DIO)
  1443. outarg.flags |= FUSE_ASYNC_DIO;
  1444. if(f->conn.want & FUSE_CAP_PARALLEL_DIROPS)
  1445. outarg.flags |= FUSE_PARALLEL_DIROPS;
  1446. if(f->conn.want & FUSE_CAP_WRITEBACK_CACHE)
  1447. outarg.flags |= FUSE_WRITEBACK_CACHE;
  1448. if (f->conn.want & FUSE_CAP_READDIR_PLUS)
  1449. outarg.flags |= FUSE_DO_READDIRPLUS;
  1450. if (f->conn.want & FUSE_CAP_READDIR_PLUS_AUTO)
  1451. outarg.flags |= FUSE_READDIRPLUS_AUTO;
  1452. outarg.max_readahead = f->conn.max_readahead;
  1453. outarg.max_write = f->conn.max_write;
  1454. if(f->conn.proto_minor >= 13) {
  1455. if(f->conn.max_background >= (1 << 16))
  1456. f->conn.max_background = (1 << 16) - 1;
  1457. if(f->conn.congestion_threshold > f->conn.max_background)
  1458. f->conn.congestion_threshold = f->conn.max_background;
  1459. if(!f->conn.congestion_threshold) {
  1460. f->conn.congestion_threshold =
  1461. f->conn.max_background * 3 / 4;
  1462. }
  1463. outarg.max_background = f->conn.max_background;
  1464. outarg.congestion_threshold = f->conn.congestion_threshold;
  1465. }
  1466. if(f->debug) {
  1467. fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor);
  1468. fprintf(stderr, " flags=0x%08x\n", outarg.flags);
  1469. fprintf(stderr, " max_readahead=0x%08x\n",
  1470. outarg.max_readahead);
  1471. fprintf(stderr, " max_write=0x%08x\n", outarg.max_write);
  1472. fprintf(stderr, " max_background=%i\n",
  1473. outarg.max_background);
  1474. fprintf(stderr, " congestion_threshold=%i\n",
  1475. outarg.congestion_threshold);
  1476. fprintf(stderr, " max_pages=%d\n",outarg.max_pages);
  1477. }
  1478. size_t outargsize;
  1479. if(arg->minor < 5)
  1480. outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
  1481. else if(arg->minor < 23)
  1482. outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
  1483. else
  1484. outargsize = sizeof(outarg);
  1485. send_reply_ok(req, &outarg, outargsize);
  1486. }
  1487. static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
  1488. {
  1489. struct fuse_ll *f = req->f;
  1490. (void) nodeid;
  1491. (void) inarg;
  1492. f->got_destroy = 1;
  1493. if(f->op.destroy)
  1494. f->op.destroy(f->userdata);
  1495. send_reply_ok(req, NULL, 0);
  1496. }
  1497. static void list_del_nreq(struct fuse_notify_req *nreq)
  1498. {
  1499. struct fuse_notify_req *prev = nreq->prev;
  1500. struct fuse_notify_req *next = nreq->next;
  1501. prev->next = next;
  1502. next->prev = prev;
  1503. }
  1504. static void list_init_nreq(struct fuse_notify_req *nreq)
  1505. {
  1506. nreq->next = nreq;
  1507. nreq->prev = nreq;
  1508. }
  1509. static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
  1510. const void *inarg, const struct fuse_buf *buf)
  1511. {
  1512. struct fuse_ll *f = req->f;
  1513. struct fuse_notify_req *nreq;
  1514. struct fuse_notify_req *head;
  1515. pthread_mutex_lock(&f->lock);
  1516. head = &f->notify_list;
  1517. for (nreq = head->next; nreq != head; nreq = nreq->next) {
  1518. if(nreq->unique == req->unique) {
  1519. list_del_nreq(nreq);
  1520. break;
  1521. }
  1522. }
  1523. pthread_mutex_unlock(&f->lock);
  1524. if(nreq != head)
  1525. nreq->reply(nreq, req, nodeid, inarg, buf);
  1526. }
  1527. static
  1528. void
  1529. do_copy_file_range(fuse_req_t req_,
  1530. fuse_ino_t nodeid_in_,
  1531. const void *arg_)
  1532. {
  1533. struct fuse_file_info ffi_in = {0};
  1534. struct fuse_file_info ffi_out = {0};
  1535. struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in*)arg_;
  1536. ffi_in.fh = arg->fh_in;
  1537. ffi_out.fh = arg->fh_out;
  1538. if(req_->f->op.copy_file_range == NULL)
  1539. fuse_reply_err(req_,ENOSYS);
  1540. else
  1541. req_->f->op.copy_file_range(req_,
  1542. nodeid_in_,
  1543. arg->off_in,
  1544. &ffi_in,
  1545. arg->nodeid_out,
  1546. arg->off_out,
  1547. &ffi_out,
  1548. arg->len,
  1549. arg->flags);
  1550. }
  1551. struct fuse_retrieve_req
  1552. {
  1553. struct fuse_notify_req nreq;
  1554. void *cookie;
  1555. };
  1556. void *fuse_req_userdata(fuse_req_t req)
  1557. {
  1558. return req->f->userdata;
  1559. }
  1560. const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
  1561. {
  1562. return &req->ctx;
  1563. }
  1564. /*
  1565. * The size of fuse_ctx got extended, so need to be careful about
  1566. * incompatibility (i.e. a new binary cannot work with an old
  1567. * library).
  1568. */
  1569. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req);
  1570. const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req)
  1571. {
  1572. return fuse_req_ctx(req);
  1573. }
  1574. #ifndef __NetBSD__
  1575. FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4");
  1576. #endif
  1577. void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
  1578. void *data)
  1579. {
  1580. pthread_mutex_lock(&req->lock);
  1581. pthread_mutex_lock(&req->f->lock);
  1582. req->u.ni.func = func;
  1583. req->u.ni.data = data;
  1584. pthread_mutex_unlock(&req->f->lock);
  1585. if(req->interrupted && func)
  1586. func(req,data);
  1587. pthread_mutex_unlock(&req->lock);
  1588. }
  1589. int fuse_req_interrupted(fuse_req_t req)
  1590. {
  1591. int interrupted;
  1592. pthread_mutex_lock(&req->f->lock);
  1593. interrupted = req->interrupted;
  1594. pthread_mutex_unlock(&req->f->lock);
  1595. return interrupted;
  1596. }
  1597. static struct
  1598. {
  1599. void (*func)(fuse_req_t, fuse_ino_t, const void *);
  1600. const char *name;
  1601. } fuse_ll_ops[] =
  1602. {
  1603. [FUSE_LOOKUP] = { do_lookup, "LOOKUP" },
  1604. [FUSE_FORGET] = { do_forget, "FORGET" },
  1605. [FUSE_GETATTR] = { do_getattr, "GETATTR" },
  1606. [FUSE_SETATTR] = { do_setattr, "SETATTR" },
  1607. [FUSE_READLINK] = { do_readlink, "READLINK" },
  1608. [FUSE_SYMLINK] = { do_symlink, "SYMLINK" },
  1609. [FUSE_MKNOD] = { do_mknod, "MKNOD" },
  1610. [FUSE_MKDIR] = { do_mkdir, "MKDIR" },
  1611. [FUSE_UNLINK] = { do_unlink, "UNLINK" },
  1612. [FUSE_RMDIR] = { do_rmdir, "RMDIR" },
  1613. [FUSE_RENAME] = { do_rename, "RENAME" },
  1614. [FUSE_LINK] = { do_link, "LINK" },
  1615. [FUSE_OPEN] = { do_open, "OPEN" },
  1616. [FUSE_READ] = { do_read, "READ" },
  1617. [FUSE_WRITE] = { do_write, "WRITE" },
  1618. [FUSE_STATFS] = { do_statfs, "STATFS" },
  1619. [FUSE_RELEASE] = { do_release, "RELEASE" },
  1620. [FUSE_FSYNC] = { do_fsync, "FSYNC" },
  1621. [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" },
  1622. [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" },
  1623. [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" },
  1624. [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
  1625. [FUSE_FLUSH] = { do_flush, "FLUSH" },
  1626. [FUSE_INIT] = { do_init, "INIT" },
  1627. [FUSE_OPENDIR] = { do_opendir, "OPENDIR" },
  1628. [FUSE_READDIR] = { do_readdir, "READDIR" },
  1629. [FUSE_READDIRPLUS] = { do_readdir_plus, "READDIR_PLUS" },
  1630. [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" },
  1631. [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" },
  1632. [FUSE_GETLK] = { do_getlk, "GETLK" },
  1633. [FUSE_SETLK] = { do_setlk, "SETLK" },
  1634. [FUSE_SETLKW] = { do_setlkw, "SETLKW" },
  1635. [FUSE_ACCESS] = { do_access, "ACCESS" },
  1636. [FUSE_CREATE] = { do_create, "CREATE" },
  1637. [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" },
  1638. [FUSE_BMAP] = { do_bmap, "BMAP" },
  1639. [FUSE_IOCTL] = { do_ioctl, "IOCTL" },
  1640. [FUSE_POLL] = { do_poll, "POLL" },
  1641. [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" },
  1642. [FUSE_DESTROY] = { do_destroy, "DESTROY" },
  1643. [FUSE_NOTIFY_REPLY] = { (void *) 1, "NOTIFY_REPLY" },
  1644. [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
  1645. [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
  1646. };
  1647. #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
  1648. static
  1649. const
  1650. char*
  1651. opname(enum fuse_opcode opcode)
  1652. {
  1653. if(opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
  1654. return "???";
  1655. else
  1656. return fuse_ll_ops[opcode].name;
  1657. }
  1658. static
  1659. int
  1660. fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
  1661. struct fuse_bufvec *src)
  1662. {
  1663. int res = fuse_buf_copy(dst,src,0);
  1664. if(res < 0)
  1665. {
  1666. fprintf(stderr, "fuse: copy from pipe: %s\n", strerror(-res));
  1667. return res;
  1668. }
  1669. if(res < fuse_buf_size(dst))
  1670. {
  1671. fprintf(stderr, "fuse: copy from pipe: short read\n");
  1672. return -1;
  1673. }
  1674. return 0;
  1675. }
  1676. void
  1677. fuse_ll_process_buf(void *data,
  1678. const struct fuse_buf *buf,
  1679. fuse_chan_t *ch)
  1680. {
  1681. struct fuse_ll *f = (struct fuse_ll *) data;
  1682. const size_t write_header_size = sizeof(struct fuse_in_header) +
  1683. sizeof(struct fuse_write_in);
  1684. struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
  1685. struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
  1686. struct fuse_in_header *in;
  1687. const void *inarg;
  1688. struct fuse_req *req;
  1689. void *mbuf = NULL;
  1690. int err;
  1691. int res;
  1692. if(buf->flags & FUSE_BUF_IS_FD) {
  1693. if(buf->size < tmpbuf.buf[0].size)
  1694. tmpbuf.buf[0].size = buf->size;
  1695. mbuf = malloc(tmpbuf.buf[0].size);
  1696. if(mbuf == NULL) {
  1697. fprintf(stderr, "fuse: failed to allocate header\n");
  1698. goto clear_pipe;
  1699. }
  1700. tmpbuf.buf[0].mem = mbuf;
  1701. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  1702. if(res < 0)
  1703. goto clear_pipe;
  1704. in = mbuf;
  1705. } else {
  1706. in = buf->mem;
  1707. }
  1708. if(f->debug) {
  1709. fprintf(stderr,
  1710. "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu, pid: %u\n",
  1711. (unsigned long long) in->unique,
  1712. opname((enum fuse_opcode) in->opcode), in->opcode,
  1713. (unsigned long) in->nodeid, buf->size, in->pid);
  1714. }
  1715. req = fuse_ll_alloc_req(f);
  1716. if(req == NULL) {
  1717. struct fuse_out_header out = {
  1718. .unique = in->unique,
  1719. .error = -ENOMEM,
  1720. };
  1721. struct iovec iov = {
  1722. .iov_base = &out,
  1723. .iov_len = sizeof(struct fuse_out_header),
  1724. };
  1725. fuse_send_msg(f, ch, &iov, 1);
  1726. goto clear_pipe;
  1727. }
  1728. req->unique = in->unique;
  1729. req->ctx.uid = in->uid;
  1730. req->ctx.gid = in->gid;
  1731. req->ctx.pid = in->pid;
  1732. req->ch = ch;
  1733. err = EIO;
  1734. if(!f->got_init)
  1735. {
  1736. if(in->opcode != FUSE_INIT)
  1737. goto reply_err;
  1738. }
  1739. else if(in->opcode == FUSE_INIT)
  1740. {
  1741. goto reply_err;
  1742. }
  1743. err = EACCES;
  1744. if(f->allow_root && in->uid != f->owner && in->uid != 0 &&
  1745. in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
  1746. in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
  1747. in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
  1748. in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
  1749. in->opcode != FUSE_NOTIFY_REPLY)
  1750. goto reply_err;
  1751. err = ENOSYS;
  1752. if(in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
  1753. goto reply_err;
  1754. if(in->opcode != FUSE_INTERRUPT) {
  1755. struct fuse_req *intr;
  1756. pthread_mutex_lock(&f->lock);
  1757. intr = check_interrupt(f, req);
  1758. list_add_req(req, &f->list);
  1759. pthread_mutex_unlock(&f->lock);
  1760. if(intr)
  1761. fuse_reply_err(intr, EAGAIN);
  1762. }
  1763. if((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
  1764. (in->opcode != FUSE_WRITE || !f->op.write_buf) &&
  1765. in->opcode != FUSE_NOTIFY_REPLY) {
  1766. void *newmbuf;
  1767. err = ENOMEM;
  1768. newmbuf = realloc(mbuf, buf->size);
  1769. if(newmbuf == NULL)
  1770. goto reply_err;
  1771. mbuf = newmbuf;
  1772. tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
  1773. tmpbuf.buf[0].mem = mbuf + write_header_size;
  1774. res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
  1775. err = -res;
  1776. if(res < 0)
  1777. goto reply_err;
  1778. in = mbuf;
  1779. }
  1780. inarg = (void *) &in[1];
  1781. if(in->opcode == FUSE_WRITE && f->op.write_buf)
  1782. do_write_buf(req, in->nodeid, inarg, buf);
  1783. else if(in->opcode == FUSE_NOTIFY_REPLY)
  1784. do_notify_reply(req, in->nodeid, inarg, buf);
  1785. else
  1786. fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
  1787. out_free:
  1788. free(mbuf);
  1789. return;
  1790. reply_err:
  1791. fuse_reply_err(req, err);
  1792. clear_pipe:
  1793. if(buf->flags & FUSE_BUF_IS_FD)
  1794. fuse_ll_clear_pipe(f);
  1795. goto out_free;
  1796. }
  1797. enum {
  1798. KEY_HELP,
  1799. KEY_VERSION,
  1800. };
  1801. static const struct fuse_opt fuse_ll_opts[] =
  1802. {
  1803. { "debug", offsetof(struct fuse_ll, debug), 1 },
  1804. { "-d", offsetof(struct fuse_ll, debug), 1 },
  1805. { "allow_root", offsetof(struct fuse_ll, allow_root), 1 },
  1806. { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 },
  1807. { "max_background=%u", offsetof(struct fuse_ll, conn.max_background), 0 },
  1808. { "congestion_threshold=%u",
  1809. offsetof(struct fuse_ll, conn.congestion_threshold), 0 },
  1810. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1811. { "no_remote_lock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1812. { "no_remote_flock", offsetof(struct fuse_ll, no_remote_flock), 1},
  1813. { "no_remote_posix_lock", offsetof(struct fuse_ll, no_remote_posix_lock), 1},
  1814. { "splice_write", offsetof(struct fuse_ll, splice_write), 1},
  1815. { "no_splice_write", offsetof(struct fuse_ll, no_splice_write), 1},
  1816. { "splice_move", offsetof(struct fuse_ll, splice_move), 1},
  1817. { "no_splice_move", offsetof(struct fuse_ll, no_splice_move), 1},
  1818. { "splice_read", offsetof(struct fuse_ll, splice_read), 1},
  1819. { "no_splice_read", offsetof(struct fuse_ll, no_splice_read), 1},
  1820. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD),
  1821. FUSE_OPT_KEY("-h", KEY_HELP),
  1822. FUSE_OPT_KEY("--help", KEY_HELP),
  1823. FUSE_OPT_KEY("-V", KEY_VERSION),
  1824. FUSE_OPT_KEY("--version", KEY_VERSION),
  1825. FUSE_OPT_END
  1826. };
  1827. static
  1828. void
  1829. fuse_ll_version(void)
  1830. {
  1831. fprintf(stderr, "using FUSE kernel interface version %i.%i\n",
  1832. FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
  1833. }
  1834. static
  1835. void
  1836. fuse_ll_help(void)
  1837. {
  1838. fprintf(stderr,
  1839. " -o max_readahead=N set maximum readahead\n"
  1840. " -o max_background=N set number of maximum background requests\n"
  1841. " -o congestion_threshold=N set kernel's congestion threshold\n"
  1842. " -o no_remote_lock disable remote file locking\n"
  1843. " -o no_remote_flock disable remote file locking (BSD)\n"
  1844. " -o no_remote_posix_lock disable remove file locking (POSIX)\n"
  1845. " -o [no_]splice_write use splice to write to the fuse device\n"
  1846. " -o [no_]splice_move move data while splicing to the fuse device\n"
  1847. " -o [no_]splice_read use splice to read from the fuse device\n"
  1848. );
  1849. }
  1850. static int fuse_ll_opt_proc(void *data, const char *arg, int key,
  1851. struct fuse_args *outargs)
  1852. {
  1853. (void) data; (void) outargs;
  1854. switch (key) {
  1855. case KEY_HELP:
  1856. fuse_ll_help();
  1857. break;
  1858. case KEY_VERSION:
  1859. fuse_ll_version();
  1860. break;
  1861. default:
  1862. fprintf(stderr, "fuse: unknown option `%s'\n", arg);
  1863. }
  1864. return -1;
  1865. }
  1866. int
  1867. fuse_lowlevel_is_lib_option(const char *opt)
  1868. {
  1869. return fuse_opt_match(fuse_ll_opts, opt);
  1870. }
  1871. static
  1872. void
  1873. fuse_ll_destroy(void *data)
  1874. {
  1875. struct fuse_ll *f = (struct fuse_ll*)data;
  1876. struct fuse_ll_pipe *llp;
  1877. if(f->got_init && !f->got_destroy)
  1878. {
  1879. if(f->op.destroy)
  1880. f->op.destroy(f->userdata);
  1881. }
  1882. llp = pthread_getspecific(f->pipe_key);
  1883. if(llp != NULL)
  1884. fuse_ll_pipe_free(llp);
  1885. pthread_key_delete(f->pipe_key);
  1886. pthread_mutex_destroy(&f->lock);
  1887. free(f);
  1888. }
  1889. static void fuse_ll_pipe_destructor(void *data)
  1890. {
  1891. struct fuse_ll_pipe *llp = data;
  1892. fuse_ll_pipe_free(llp);
  1893. }
  1894. int
  1895. fuse_ll_receive_buf(struct fuse_session *se,
  1896. struct fuse_buf *buf,
  1897. fuse_chan_t *ch)
  1898. {
  1899. (void) se;
  1900. int res = fuse_chan_recv(ch, buf->mem, buf->size);
  1901. if(res <= 0)
  1902. return res;
  1903. buf->size = res;
  1904. return res;
  1905. }
  1906. /*
  1907. * always call fuse_lowlevel_new_common() internally, to work around a
  1908. * misfeature in the FreeBSD runtime linker, which links the old
  1909. * version of a symbol to internal references.
  1910. */
  1911. struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args,
  1912. const struct fuse_lowlevel_ops *op,
  1913. size_t op_size, void *userdata)
  1914. {
  1915. int err;
  1916. struct fuse_ll *f;
  1917. struct fuse_session *se;
  1918. struct fuse_session_ops sop = {
  1919. .destroy = fuse_ll_destroy,
  1920. };
  1921. if(sizeof(struct fuse_lowlevel_ops) < op_size) {
  1922. fprintf(stderr, "fuse: warning: library too old, some operations may not work\n");
  1923. op_size = sizeof(struct fuse_lowlevel_ops);
  1924. }
  1925. f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll));
  1926. if(f == NULL) {
  1927. fprintf(stderr, "fuse: failed to allocate fuse object\n");
  1928. goto out;
  1929. }
  1930. f->conn.max_write = UINT_MAX;
  1931. f->conn.max_readahead = UINT_MAX;
  1932. list_init_req(&f->list);
  1933. list_init_req(&f->interrupts);
  1934. list_init_nreq(&f->notify_list);
  1935. f->notify_ctr = 1;
  1936. fuse_mutex_init(&f->lock);
  1937. err = pthread_key_create(&f->pipe_key, fuse_ll_pipe_destructor);
  1938. if(err) {
  1939. fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
  1940. strerror(err));
  1941. goto out_free;
  1942. }
  1943. if(fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1)
  1944. goto out_key_destroy;
  1945. if(f->debug)
  1946. fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION);
  1947. memcpy(&f->op, op, op_size);
  1948. f->owner = getuid();
  1949. f->userdata = userdata;
  1950. se = fuse_session_new(&sop, f);
  1951. if(!se)
  1952. goto out_key_destroy;
  1953. return se;
  1954. out_key_destroy:
  1955. pthread_key_delete(f->pipe_key);
  1956. out_free:
  1957. pthread_mutex_destroy(&f->lock);
  1958. free(f);
  1959. out:
  1960. return NULL;
  1961. }
  1962. struct fuse_session *fuse_lowlevel_new(struct fuse_args *args,
  1963. const struct fuse_lowlevel_ops *op,
  1964. size_t op_size, void *userdata)
  1965. {
  1966. return fuse_lowlevel_new_common(args, op, op_size, userdata);
  1967. }
  1968. #if !defined(__FreeBSD__) && !defined(__NetBSD__)
  1969. static void fill_open_compat(struct fuse_open_out *arg,
  1970. const struct fuse_file_info_compat *f)
  1971. {
  1972. arg->fh = f->fh;
  1973. if(f->direct_io)
  1974. arg->open_flags |= FOPEN_DIRECT_IO;
  1975. if(f->keep_cache)
  1976. arg->open_flags |= FOPEN_KEEP_CACHE;
  1977. }
  1978. static void convert_statfs_compat(const struct statfs *compatbuf,
  1979. struct statvfs *buf)
  1980. {
  1981. buf->f_bsize = compatbuf->f_bsize;
  1982. buf->f_blocks = compatbuf->f_blocks;
  1983. buf->f_bfree = compatbuf->f_bfree;
  1984. buf->f_bavail = compatbuf->f_bavail;
  1985. buf->f_files = compatbuf->f_files;
  1986. buf->f_ffree = compatbuf->f_ffree;
  1987. buf->f_namemax = compatbuf->f_namelen;
  1988. }
  1989. int fuse_reply_open_compat(fuse_req_t req,
  1990. const struct fuse_file_info_compat *f)
  1991. {
  1992. struct fuse_open_out arg;
  1993. memset(&arg, 0, sizeof(arg));
  1994. fill_open_compat(&arg, f);
  1995. return send_reply_ok(req, &arg, sizeof(arg));
  1996. }
  1997. int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf)
  1998. {
  1999. struct statvfs newbuf;
  2000. memset(&newbuf, 0, sizeof(newbuf));
  2001. convert_statfs_compat(stbuf, &newbuf);
  2002. return fuse_reply_statfs(req, &newbuf);
  2003. }
  2004. struct fuse_session *fuse_lowlevel_new_compat(const char *opts,
  2005. const struct fuse_lowlevel_ops_compat *op,
  2006. size_t op_size, void *userdata)
  2007. {
  2008. struct fuse_session *se;
  2009. struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
  2010. if(opts &&
  2011. (fuse_opt_add_arg(&args, "") == -1 ||
  2012. fuse_opt_add_arg(&args, "-o") == -1 ||
  2013. fuse_opt_add_arg(&args, opts) == -1)) {
  2014. fuse_opt_free_args(&args);
  2015. return NULL;
  2016. }
  2017. se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op,
  2018. op_size, userdata);
  2019. fuse_opt_free_args(&args);
  2020. return se;
  2021. }
  2022. struct fuse_ll_compat_conf {
  2023. unsigned max_read;
  2024. int set_max_read;
  2025. };
  2026. static const struct fuse_opt fuse_ll_opts_compat[] = {
  2027. { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 },
  2028. { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 },
  2029. FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP),
  2030. FUSE_OPT_END
  2031. };
  2032. int fuse_sync_compat_args(struct fuse_args *args)
  2033. {
  2034. struct fuse_ll_compat_conf conf;
  2035. memset(&conf, 0, sizeof(conf));
  2036. if(fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1)
  2037. return -1;
  2038. if(fuse_opt_insert_arg(args, 1, "-osync_read"))
  2039. return -1;
  2040. if(conf.set_max_read) {
  2041. char tmpbuf[64];
  2042. sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read);
  2043. if(fuse_opt_insert_arg(args, 1, tmpbuf) == -1)
  2044. return -1;
  2045. }
  2046. return 0;
  2047. }
  2048. FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4");
  2049. FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4");
  2050. FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4");
  2051. #else /* __FreeBSD__ || __NetBSD__ */
  2052. int fuse_sync_compat_args(struct fuse_args *args)
  2053. {
  2054. (void) args;
  2055. return 0;
  2056. }
  2057. #endif /* __FreeBSD__ || __NetBSD__ */
  2058. struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args,
  2059. const struct fuse_lowlevel_ops_compat25 *op,
  2060. size_t op_size, void *userdata)
  2061. {
  2062. if(fuse_sync_compat_args(args) == -1)
  2063. return NULL;
  2064. return fuse_lowlevel_new_common(args,
  2065. (const struct fuse_lowlevel_ops *) op,
  2066. op_size, userdata);
  2067. }
  2068. FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5");