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.

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