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.

4192 lines
84 KiB

2 years ago
2 years ago
7 months ago
2 years ago
2 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. /* For pthread_rwlock_t */
  8. #define _GNU_SOURCE
  9. #include "crc32b.h"
  10. #include "fuse_node.h"
  11. #include "khash.h"
  12. #include "kvec.h"
  13. #include "node.h"
  14. #include "config.h"
  15. #include "fuse_dirents.h"
  16. #include "fuse_i.h"
  17. #include "fuse_kernel.h"
  18. #include "fuse_lowlevel.h"
  19. #include "fuse_misc.h"
  20. #include "fuse_opt.h"
  21. #include "fuse_pollhandle.h"
  22. #include "fuse_msgbuf.hpp"
  23. #include <assert.h>
  24. #include <dlfcn.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <inttypes.h>
  28. #include <limits.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <stdbool.h>
  32. #include <stddef.h>
  33. #include <stdint.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sys/file.h>
  38. #include <sys/mman.h>
  39. #include <sys/param.h>
  40. #include <sys/time.h>
  41. #include <sys/uio.h>
  42. #include <syslog.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #include <sys/ioctl.h>
  46. #ifdef HAVE_MALLOC_TRIM
  47. #include <malloc.h>
  48. #endif
  49. #define FUSE_UNKNOWN_INO UINT64_MAX
  50. #define OFFSET_MAX 0x7fffffffffffffffLL
  51. #define NODE_TABLE_MIN_SIZE 8192
  52. #define PARAM(inarg) ((void*)(((char*)(inarg)) + sizeof(*(inarg))))
  53. static int g_LOG_METRICS = 0;
  54. struct fuse_config
  55. {
  56. unsigned int uid;
  57. unsigned int gid;
  58. unsigned int umask;
  59. int remember;
  60. int debug;
  61. int nogc;
  62. int set_mode;
  63. int set_uid;
  64. int set_gid;
  65. int help;
  66. };
  67. struct fuse_fs
  68. {
  69. struct fuse_operations op;
  70. };
  71. struct lock_queue_element
  72. {
  73. struct lock_queue_element *next;
  74. pthread_cond_t cond;
  75. uint64_t nodeid1;
  76. const char *name1;
  77. char **path1;
  78. node_t **wnode1;
  79. uint64_t nodeid2;
  80. const char *name2;
  81. char **path2;
  82. node_t **wnode2;
  83. int err;
  84. bool done : 1;
  85. };
  86. struct node_table
  87. {
  88. node_t **array;
  89. size_t use;
  90. size_t size;
  91. size_t split;
  92. };
  93. #define container_of(ptr,type,member) ({ \
  94. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  95. (type *)( (char *)__mptr - offsetof(type,member) );})
  96. #define list_entry(ptr,type,member) \
  97. container_of(ptr,type,member)
  98. struct list_head
  99. {
  100. struct list_head *next;
  101. struct list_head *prev;
  102. };
  103. typedef struct remembered_node_t remembered_node_t;
  104. struct remembered_node_t
  105. {
  106. node_t *node;
  107. time_t time;
  108. };
  109. typedef struct nodeid_gen_t nodeid_gen_t;
  110. struct nodeid_gen_t
  111. {
  112. uint64_t nodeid;
  113. uint64_t generation;
  114. };
  115. struct fuse
  116. {
  117. struct fuse_session *se;
  118. struct node_table name_table;
  119. struct node_table id_table;
  120. nodeid_gen_t nodeid_gen;
  121. unsigned int hidectr;
  122. pthread_mutex_t lock;
  123. struct fuse_config conf;
  124. struct fuse_fs *fs;
  125. struct lock_queue_element *lockq;
  126. pthread_t maintenance_thread;
  127. kvec_t(remembered_node_t) remembered_nodes;
  128. };
  129. struct lock
  130. {
  131. int type;
  132. off_t start;
  133. off_t end;
  134. pid_t pid;
  135. uint64_t owner;
  136. struct lock *next;
  137. };
  138. #define TREELOCK_WRITE -1
  139. #define TREELOCK_WAIT_OFFSET INT_MIN
  140. struct fuse_dh
  141. {
  142. pthread_mutex_t lock;
  143. uint64_t fh;
  144. fuse_dirents_t d;
  145. };
  146. struct fuse_context_i
  147. {
  148. struct fuse_context ctx;
  149. fuse_req_t req;
  150. };
  151. static pthread_key_t fuse_context_key;
  152. static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
  153. static int fuse_context_ref;
  154. /*
  155. Why was the nodeid:generation logic simplified?
  156. nodeid is uint64_t: max value of 18446744073709551616
  157. If nodes were created at a rate of 1048576 per second it would take
  158. over 500 thousand years to roll over. I'm fine with risking that.
  159. */
  160. static
  161. uint64_t
  162. generate_nodeid(nodeid_gen_t *ng_)
  163. {
  164. ng_->nodeid++;
  165. return ng_->nodeid;
  166. }
  167. static
  168. char*
  169. filename_strdup(struct fuse *f_,
  170. const char *fn_)
  171. {
  172. return strdup(fn_);
  173. }
  174. static
  175. void
  176. filename_free(struct fuse *f_,
  177. char *fn_)
  178. {
  179. free(fn_);
  180. }
  181. static
  182. void*
  183. fuse_hdr_arg(const struct fuse_in_header *hdr_)
  184. {
  185. return (void*)&hdr_[1];
  186. }
  187. static
  188. void
  189. list_add(struct list_head *new,
  190. struct list_head *prev,
  191. struct list_head *next)
  192. {
  193. next->prev = new;
  194. new->next = next;
  195. new->prev = prev;
  196. prev->next = new;
  197. }
  198. static
  199. inline
  200. void
  201. list_add_head(struct list_head *new,
  202. struct list_head *head)
  203. {
  204. list_add(new,head,head->next);
  205. }
  206. static
  207. inline
  208. void
  209. list_add_tail(struct list_head *new,
  210. struct list_head *head)
  211. {
  212. list_add(new,head->prev,head);
  213. }
  214. static
  215. inline
  216. void
  217. list_del(struct list_head *entry)
  218. {
  219. struct list_head *prev = entry->prev;
  220. struct list_head *next = entry->next;
  221. next->prev = prev;
  222. prev->next = next;
  223. }
  224. static
  225. size_t
  226. id_hash(struct fuse *f,
  227. uint64_t ino)
  228. {
  229. uint64_t hash = ((uint32_t)ino * 2654435761U) % f->id_table.size;
  230. uint64_t oldhash = hash % (f->id_table.size / 2);
  231. if(oldhash >= f->id_table.split)
  232. return oldhash;
  233. else
  234. return hash;
  235. }
  236. static
  237. node_t*
  238. get_node_nocheck(struct fuse *f,
  239. uint64_t nodeid)
  240. {
  241. size_t hash = id_hash(f,nodeid);
  242. node_t *node;
  243. for(node = f->id_table.array[hash]; node != NULL; node = node->id_next)
  244. if(node->nodeid == nodeid)
  245. return node;
  246. return NULL;
  247. }
  248. static
  249. node_t*
  250. get_node(struct fuse *f,
  251. const uint64_t nodeid)
  252. {
  253. node_t *node = get_node_nocheck(f,nodeid);
  254. if(!node)
  255. {
  256. fprintf(stderr,"fuse internal error: node %llu not found\n",
  257. (unsigned long long)nodeid);
  258. abort();
  259. }
  260. return node;
  261. }
  262. static
  263. void
  264. remove_remembered_node(struct fuse *f_,
  265. node_t *node_)
  266. {
  267. for(size_t i = 0; i < kv_size(f_->remembered_nodes); i++)
  268. {
  269. if(kv_A(f_->remembered_nodes,i).node != node_)
  270. continue;
  271. kv_delete(f_->remembered_nodes,i);
  272. break;
  273. }
  274. }
  275. static
  276. uint32_t
  277. stat_crc32b(const struct stat *st_)
  278. {
  279. uint32_t crc;
  280. crc = crc32b_start();
  281. crc = crc32b_continue(&st_->st_ino,sizeof(st_->st_ino),crc);
  282. crc = crc32b_continue(&st_->st_size,sizeof(st_->st_size),crc);
  283. crc = crc32b_continue(&st_->st_mtim,sizeof(st_->st_mtim),crc);
  284. crc = crc32b_finish(crc);
  285. return crc;
  286. }
  287. #ifndef CLOCK_MONOTONIC
  288. # define CLOCK_MONOTONIC CLOCK_REALTIME
  289. #endif
  290. static
  291. time_t
  292. current_time()
  293. {
  294. int rv;
  295. struct timespec now;
  296. static clockid_t clockid = CLOCK_MONOTONIC;
  297. rv = clock_gettime(clockid,&now);
  298. if((rv == -1) && (errno == EINVAL))
  299. {
  300. clockid = CLOCK_REALTIME;
  301. rv = clock_gettime(clockid,&now);
  302. }
  303. if(rv == -1)
  304. now.tv_sec = time(NULL);
  305. return now.tv_sec;
  306. }
  307. static
  308. void
  309. free_node(struct fuse *f_,
  310. node_t *node_)
  311. {
  312. filename_free(f_,node_->name);
  313. if(node_->hidden_fh)
  314. f_->fs->op.free_hide(node_->hidden_fh);
  315. node_free(node_);
  316. }
  317. static
  318. void
  319. node_table_reduce(struct node_table *t)
  320. {
  321. size_t newsize = t->size / 2;
  322. void *newarray;
  323. if(newsize < NODE_TABLE_MIN_SIZE)
  324. return;
  325. newarray = realloc(t->array,sizeof(node_t*) * newsize);
  326. if(newarray != NULL)
  327. t->array = newarray;
  328. t->size = newsize;
  329. t->split = t->size / 2;
  330. }
  331. static
  332. void
  333. remerge_id(struct fuse *f)
  334. {
  335. struct node_table *t = &f->id_table;
  336. int iter;
  337. if(t->split == 0)
  338. node_table_reduce(t);
  339. for(iter = 8; t->split > 0 && iter; iter--)
  340. {
  341. node_t **upper;
  342. t->split--;
  343. upper = &t->array[t->split + t->size / 2];
  344. if(*upper)
  345. {
  346. node_t **nodep;
  347. for(nodep = &t->array[t->split]; *nodep;
  348. nodep = &(*nodep)->id_next);
  349. *nodep = *upper;
  350. *upper = NULL;
  351. break;
  352. }
  353. }
  354. }
  355. static
  356. void
  357. unhash_id(struct fuse *f,
  358. node_t *node)
  359. {
  360. node_t **nodep = &f->id_table.array[id_hash(f,node->nodeid)];
  361. for(; *nodep != NULL; nodep = &(*nodep)->id_next)
  362. if(*nodep == node)
  363. {
  364. *nodep = node->id_next;
  365. f->id_table.use--;
  366. if(f->id_table.use < f->id_table.size / 4)
  367. remerge_id(f);
  368. return;
  369. }
  370. }
  371. static
  372. int
  373. node_table_resize(struct node_table *t)
  374. {
  375. size_t newsize = t->size * 2;
  376. void *newarray;
  377. newarray = realloc(t->array,sizeof(node_t*) * newsize);
  378. if(newarray == NULL)
  379. return -1;
  380. t->array = newarray;
  381. memset(t->array + t->size,0,t->size * sizeof(node_t*));
  382. t->size = newsize;
  383. t->split = 0;
  384. return 0;
  385. }
  386. static
  387. void
  388. rehash_id(struct fuse *f)
  389. {
  390. struct node_table *t = &f->id_table;
  391. node_t **nodep;
  392. node_t **next;
  393. size_t hash;
  394. if(t->split == t->size / 2)
  395. return;
  396. hash = t->split;
  397. t->split++;
  398. for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
  399. {
  400. node_t *node = *nodep;
  401. size_t newhash = id_hash(f,node->nodeid);
  402. if(newhash != hash)
  403. {
  404. next = nodep;
  405. *nodep = node->id_next;
  406. node->id_next = t->array[newhash];
  407. t->array[newhash] = node;
  408. }
  409. else
  410. {
  411. next = &node->id_next;
  412. }
  413. }
  414. if(t->split == t->size / 2)
  415. node_table_resize(t);
  416. }
  417. static
  418. void
  419. hash_id(struct fuse *f,
  420. node_t *node)
  421. {
  422. size_t hash;
  423. hash = id_hash(f,node->nodeid);
  424. node->id_next = f->id_table.array[hash];
  425. f->id_table.array[hash] = node;
  426. f->id_table.use++;
  427. if(f->id_table.use >= f->id_table.size / 2)
  428. rehash_id(f);
  429. }
  430. static
  431. size_t
  432. name_hash(struct fuse *f,
  433. uint64_t parent,
  434. const char *name)
  435. {
  436. uint64_t hash = parent;
  437. uint64_t oldhash;
  438. for(; *name; name++)
  439. hash = hash * 31 + (unsigned char)*name;
  440. hash %= f->name_table.size;
  441. oldhash = hash % (f->name_table.size / 2);
  442. if(oldhash >= f->name_table.split)
  443. return oldhash;
  444. else
  445. return hash;
  446. }
  447. static
  448. void
  449. unref_node(struct fuse *f,
  450. node_t *node);
  451. static
  452. void
  453. remerge_name(struct fuse *f)
  454. {
  455. int iter;
  456. struct node_table *t = &f->name_table;
  457. if(t->split == 0)
  458. node_table_reduce(t);
  459. for(iter = 8; t->split > 0 && iter; iter--)
  460. {
  461. node_t **upper;
  462. t->split--;
  463. upper = &t->array[t->split + t->size / 2];
  464. if(*upper)
  465. {
  466. node_t **nodep;
  467. for(nodep = &t->array[t->split]; *nodep; nodep = &(*nodep)->name_next);
  468. *nodep = *upper;
  469. *upper = NULL;
  470. break;
  471. }
  472. }
  473. }
  474. static
  475. void
  476. unhash_name(struct fuse *f,
  477. node_t *node)
  478. {
  479. if(node->name)
  480. {
  481. size_t hash = name_hash(f,node->parent->nodeid,node->name);
  482. node_t **nodep = &f->name_table.array[hash];
  483. for(; *nodep != NULL; nodep = &(*nodep)->name_next)
  484. if(*nodep == node)
  485. {
  486. *nodep = node->name_next;
  487. node->name_next = NULL;
  488. unref_node(f,node->parent);
  489. filename_free(f,node->name);
  490. node->name = NULL;
  491. node->parent = NULL;
  492. f->name_table.use--;
  493. if(f->name_table.use < f->name_table.size / 4)
  494. remerge_name(f);
  495. return;
  496. }
  497. fprintf(stderr,
  498. "fuse internal error: unable to unhash node: %llu\n",
  499. (unsigned long long)node->nodeid);
  500. abort();
  501. }
  502. }
  503. static
  504. void
  505. rehash_name(struct fuse *f)
  506. {
  507. struct node_table *t = &f->name_table;
  508. node_t **nodep;
  509. node_t **next;
  510. size_t hash;
  511. if(t->split == t->size / 2)
  512. return;
  513. hash = t->split;
  514. t->split++;
  515. for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
  516. {
  517. node_t *node = *nodep;
  518. size_t newhash = name_hash(f,node->parent->nodeid,node->name);
  519. if(newhash != hash)
  520. {
  521. next = nodep;
  522. *nodep = node->name_next;
  523. node->name_next = t->array[newhash];
  524. t->array[newhash] = node;
  525. }
  526. else
  527. {
  528. next = &node->name_next;
  529. }
  530. }
  531. if(t->split == t->size / 2)
  532. node_table_resize(t);
  533. }
  534. static
  535. int
  536. hash_name(struct fuse *f,
  537. node_t *node,
  538. uint64_t parentid,
  539. const char *name)
  540. {
  541. size_t hash = name_hash(f,parentid,name);
  542. node_t *parent = get_node(f,parentid);
  543. node->name = filename_strdup(f,name);
  544. if(node->name == NULL)
  545. return -1;
  546. parent->refctr++;
  547. node->parent = parent;
  548. node->name_next = f->name_table.array[hash];
  549. f->name_table.array[hash] = node;
  550. f->name_table.use++;
  551. if(f->name_table.use >= f->name_table.size / 2)
  552. rehash_name(f);
  553. return 0;
  554. }
  555. static
  556. inline
  557. int
  558. remember_nodes(struct fuse *f_)
  559. {
  560. return (f_->conf.remember > 0);
  561. }
  562. static
  563. void
  564. delete_node(struct fuse *f,
  565. node_t *node)
  566. {
  567. assert(node->treelock == 0);
  568. unhash_name(f,node);
  569. if(remember_nodes(f))
  570. remove_remembered_node(f,node);
  571. unhash_id(f,node);
  572. node_free(node);
  573. }
  574. static
  575. void
  576. unref_node(struct fuse *f,
  577. node_t *node)
  578. {
  579. assert(node->refctr > 0);
  580. node->refctr--;
  581. if(!node->refctr)
  582. delete_node(f,node);
  583. }
  584. static
  585. uint64_t
  586. rand64(void)
  587. {
  588. uint64_t rv;
  589. rv = rand();
  590. rv <<= 32;
  591. rv |= rand();
  592. return rv;
  593. }
  594. static
  595. node_t*
  596. lookup_node(struct fuse *f,
  597. uint64_t parent,
  598. const char *name)
  599. {
  600. size_t hash;
  601. node_t *node;
  602. hash = name_hash(f,parent,name);
  603. for(node = f->name_table.array[hash]; node != NULL; node = node->name_next)
  604. if(node->parent->nodeid == parent && strcmp(node->name,name) == 0)
  605. return node;
  606. return NULL;
  607. }
  608. static
  609. void
  610. inc_nlookup(node_t *node)
  611. {
  612. if(!node->nlookup)
  613. node->refctr++;
  614. node->nlookup++;
  615. }
  616. static
  617. node_t*
  618. find_node(struct fuse *f,
  619. uint64_t parent,
  620. const char *name)
  621. {
  622. node_t *node;
  623. pthread_mutex_lock(&f->lock);
  624. if(!name)
  625. node = get_node(f,parent);
  626. else
  627. node = lookup_node(f,parent,name);
  628. if(node == NULL)
  629. {
  630. node = node_alloc();
  631. if(node == NULL)
  632. goto out_err;
  633. node->nodeid = generate_nodeid(&f->nodeid_gen);
  634. if(f->conf.remember)
  635. inc_nlookup(node);
  636. if(hash_name(f,node,parent,name) == -1)
  637. {
  638. free_node(f,node);
  639. node = NULL;
  640. goto out_err;
  641. }
  642. hash_id(f,node);
  643. }
  644. else if((node->nlookup == 1) && remember_nodes(f))
  645. {
  646. remove_remembered_node(f,node);
  647. }
  648. inc_nlookup(node);
  649. out_err:
  650. pthread_mutex_unlock(&f->lock);
  651. return node;
  652. }
  653. static
  654. char*
  655. add_name(char **buf,
  656. unsigned *bufsize,
  657. char *s,
  658. const char *name)
  659. {
  660. size_t len = strlen(name);
  661. if(s - len <= *buf)
  662. {
  663. unsigned pathlen = *bufsize - (s - *buf);
  664. unsigned newbufsize = *bufsize;
  665. char *newbuf;
  666. while(newbufsize < pathlen + len + 1)
  667. {
  668. if(newbufsize >= 0x80000000)
  669. newbufsize = 0xffffffff;
  670. else
  671. newbufsize *= 2;
  672. }
  673. newbuf = realloc(*buf,newbufsize);
  674. if(newbuf == NULL)
  675. return NULL;
  676. *buf = newbuf;
  677. s = newbuf + newbufsize - pathlen;
  678. memmove(s,newbuf + *bufsize - pathlen,pathlen);
  679. *bufsize = newbufsize;
  680. }
  681. s -= len;
  682. strncpy(s,name,len);
  683. s--;
  684. *s = '/';
  685. return s;
  686. }
  687. static
  688. void
  689. unlock_path(struct fuse *f,
  690. uint64_t nodeid,
  691. node_t *wnode,
  692. node_t *end)
  693. {
  694. node_t *node;
  695. if(wnode)
  696. {
  697. assert(wnode->treelock == TREELOCK_WRITE);
  698. wnode->treelock = 0;
  699. }
  700. for(node = get_node(f,nodeid); node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent)
  701. {
  702. assert(node->treelock != 0);
  703. assert(node->treelock != TREELOCK_WAIT_OFFSET);
  704. assert(node->treelock != TREELOCK_WRITE);
  705. node->treelock--;
  706. if(node->treelock == TREELOCK_WAIT_OFFSET)
  707. node->treelock = 0;
  708. }
  709. }
  710. static
  711. int
  712. try_get_path(struct fuse *f,
  713. uint64_t nodeid,
  714. const char *name,
  715. char **path,
  716. node_t **wnodep,
  717. bool need_lock)
  718. {
  719. unsigned bufsize = 256;
  720. char *buf;
  721. char *s;
  722. node_t *node;
  723. node_t *wnode = NULL;
  724. int err;
  725. *path = NULL;
  726. err = -ENOMEM;
  727. buf = malloc(bufsize);
  728. if(buf == NULL)
  729. goto out_err;
  730. s = buf + bufsize - 1;
  731. *s = '\0';
  732. if(name != NULL)
  733. {
  734. s = add_name(&buf,&bufsize,s,name);
  735. err = -ENOMEM;
  736. if(s == NULL)
  737. goto out_free;
  738. }
  739. if(wnodep)
  740. {
  741. assert(need_lock);
  742. wnode = lookup_node(f,nodeid,name);
  743. if(wnode)
  744. {
  745. if(wnode->treelock != 0)
  746. {
  747. if(wnode->treelock > 0)
  748. wnode->treelock += TREELOCK_WAIT_OFFSET;
  749. err = -EAGAIN;
  750. goto out_free;
  751. }
  752. wnode->treelock = TREELOCK_WRITE;
  753. }
  754. }
  755. for(node = get_node(f,nodeid); node->nodeid != FUSE_ROOT_ID; node = node->parent)
  756. {
  757. err = -ESTALE;
  758. if(node->name == NULL || node->parent == NULL)
  759. goto out_unlock;
  760. err = -ENOMEM;
  761. s = add_name(&buf,&bufsize,s,node->name);
  762. if(s == NULL)
  763. goto out_unlock;
  764. if(need_lock)
  765. {
  766. err = -EAGAIN;
  767. if(node->treelock < 0)
  768. goto out_unlock;
  769. node->treelock++;
  770. }
  771. }
  772. if(s[0])
  773. memmove(buf,s,bufsize - (s - buf));
  774. else
  775. strcpy(buf,"/");
  776. *path = buf;
  777. if(wnodep)
  778. *wnodep = wnode;
  779. return 0;
  780. out_unlock:
  781. if(need_lock)
  782. unlock_path(f,nodeid,wnode,node);
  783. out_free:
  784. free(buf);
  785. out_err:
  786. return err;
  787. }
  788. static
  789. int
  790. try_get_path2(struct fuse *f,
  791. uint64_t nodeid1,
  792. const char *name1,
  793. uint64_t nodeid2,
  794. const char *name2,
  795. char **path1,
  796. char **path2,
  797. node_t **wnode1,
  798. node_t **wnode2)
  799. {
  800. int err;
  801. err = try_get_path(f,nodeid1,name1,path1,wnode1,true);
  802. if(!err)
  803. {
  804. err = try_get_path(f,nodeid2,name2,path2,wnode2,true);
  805. if(err)
  806. {
  807. node_t *wn1 = wnode1 ? *wnode1 : NULL;
  808. unlock_path(f,nodeid1,wn1,NULL);
  809. free(*path1);
  810. }
  811. }
  812. return err;
  813. }
  814. static
  815. void
  816. queue_element_wakeup(struct fuse *f,
  817. struct lock_queue_element *qe)
  818. {
  819. int err;
  820. if(!qe->path1)
  821. {
  822. /* Just waiting for it to be unlocked */
  823. if(get_node(f,qe->nodeid1)->treelock == 0)
  824. pthread_cond_signal(&qe->cond);
  825. return;
  826. }
  827. if(qe->done)
  828. return;
  829. if(!qe->path2)
  830. {
  831. err = try_get_path(f,
  832. qe->nodeid1,
  833. qe->name1,
  834. qe->path1,
  835. qe->wnode1,
  836. true);
  837. }
  838. else
  839. {
  840. err = try_get_path2(f,
  841. qe->nodeid1,
  842. qe->name1,
  843. qe->nodeid2,
  844. qe->name2,
  845. qe->path1,
  846. qe->path2,
  847. qe->wnode1,
  848. qe->wnode2);
  849. }
  850. if(err == -EAGAIN)
  851. return;
  852. qe->err = err;
  853. qe->done = true;
  854. pthread_cond_signal(&qe->cond);
  855. }
  856. static
  857. void
  858. wake_up_queued(struct fuse *f)
  859. {
  860. struct lock_queue_element *qe;
  861. for(qe = f->lockq; qe != NULL; qe = qe->next)
  862. queue_element_wakeup(f,qe);
  863. }
  864. static
  865. void
  866. queue_path(struct fuse *f,
  867. struct lock_queue_element *qe)
  868. {
  869. struct lock_queue_element **qp;
  870. qe->done = false;
  871. pthread_cond_init(&qe->cond,NULL);
  872. qe->next = NULL;
  873. for(qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
  874. *qp = qe;
  875. }
  876. static
  877. void
  878. dequeue_path(struct fuse *f,
  879. struct lock_queue_element *qe)
  880. {
  881. struct lock_queue_element **qp;
  882. pthread_cond_destroy(&qe->cond);
  883. for(qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
  884. *qp = qe->next;
  885. }
  886. static
  887. int
  888. wait_path(struct fuse *f,
  889. struct lock_queue_element *qe)
  890. {
  891. queue_path(f,qe);
  892. do
  893. {
  894. pthread_cond_wait(&qe->cond,&f->lock);
  895. } while(!qe->done);
  896. dequeue_path(f,qe);
  897. return qe->err;
  898. }
  899. static
  900. int
  901. get_path_common(struct fuse *f,
  902. uint64_t nodeid,
  903. const char *name,
  904. char **path,
  905. node_t **wnode)
  906. {
  907. int err;
  908. pthread_mutex_lock(&f->lock);
  909. err = try_get_path(f,nodeid,name,path,wnode,true);
  910. if(err == -EAGAIN)
  911. {
  912. struct lock_queue_element qe = {0};
  913. qe.nodeid1 = nodeid;
  914. qe.name1 = name;
  915. qe.path1 = path;
  916. qe.wnode1 = wnode;
  917. err = wait_path(f,&qe);
  918. }
  919. pthread_mutex_unlock(&f->lock);
  920. return err;
  921. }
  922. static
  923. int
  924. get_path(struct fuse *f,
  925. uint64_t nodeid,
  926. char **path)
  927. {
  928. return get_path_common(f,nodeid,NULL,path,NULL);
  929. }
  930. static
  931. int
  932. get_path_name(struct fuse *f,
  933. uint64_t nodeid,
  934. const char *name,
  935. char **path)
  936. {
  937. return get_path_common(f,nodeid,name,path,NULL);
  938. }
  939. static
  940. int
  941. get_path_wrlock(struct fuse *f,
  942. uint64_t nodeid,
  943. const char *name,
  944. char **path,
  945. node_t **wnode)
  946. {
  947. return get_path_common(f,nodeid,name,path,wnode);
  948. }
  949. static
  950. int
  951. get_path2(struct fuse *f,
  952. uint64_t nodeid1,
  953. const char *name1,
  954. uint64_t nodeid2,
  955. const char *name2,
  956. char **path1,
  957. char **path2,
  958. node_t **wnode1,
  959. node_t **wnode2)
  960. {
  961. int err;
  962. pthread_mutex_lock(&f->lock);
  963. err = try_get_path2(f,nodeid1,name1,nodeid2,name2,
  964. path1,path2,wnode1,wnode2);
  965. if(err == -EAGAIN)
  966. {
  967. struct lock_queue_element qe = {0};
  968. qe.nodeid1 = nodeid1;
  969. qe.name1 = name1;
  970. qe.path1 = path1;
  971. qe.wnode1 = wnode1;
  972. qe.nodeid2 = nodeid2;
  973. qe.name2 = name2;
  974. qe.path2 = path2;
  975. qe.wnode2 = wnode2;
  976. err = wait_path(f,&qe);
  977. }
  978. pthread_mutex_unlock(&f->lock);
  979. return err;
  980. }
  981. static
  982. void
  983. free_path_wrlock(struct fuse *f,
  984. uint64_t nodeid,
  985. node_t *wnode,
  986. char *path)
  987. {
  988. pthread_mutex_lock(&f->lock);
  989. unlock_path(f,nodeid,wnode,NULL);
  990. if(f->lockq)
  991. wake_up_queued(f);
  992. pthread_mutex_unlock(&f->lock);
  993. free(path);
  994. }
  995. static
  996. void
  997. free_path(struct fuse *f,
  998. uint64_t nodeid,
  999. char *path)
  1000. {
  1001. if(path)
  1002. free_path_wrlock(f,nodeid,NULL,path);
  1003. }
  1004. static
  1005. void
  1006. free_path2(struct fuse *f,
  1007. uint64_t nodeid1,
  1008. uint64_t nodeid2,
  1009. node_t *wnode1,
  1010. node_t *wnode2,
  1011. char *path1,
  1012. char *path2)
  1013. {
  1014. pthread_mutex_lock(&f->lock);
  1015. unlock_path(f,nodeid1,wnode1,NULL);
  1016. unlock_path(f,nodeid2,wnode2,NULL);
  1017. wake_up_queued(f);
  1018. pthread_mutex_unlock(&f->lock);
  1019. free(path1);
  1020. free(path2);
  1021. }
  1022. static
  1023. void
  1024. forget_node(struct fuse *f,
  1025. const uint64_t nodeid,
  1026. const uint64_t nlookup)
  1027. {
  1028. node_t *node;
  1029. if(nodeid == FUSE_ROOT_ID)
  1030. return;
  1031. pthread_mutex_lock(&f->lock);
  1032. node = get_node(f,nodeid);
  1033. /*
  1034. * Node may still be locked due to interrupt idiocy in open,
  1035. * create and opendir
  1036. */
  1037. while(node->nlookup == nlookup && node->treelock)
  1038. {
  1039. struct lock_queue_element qe = {0};
  1040. qe.nodeid1 = nodeid;
  1041. queue_path(f,&qe);
  1042. do
  1043. {
  1044. pthread_cond_wait(&qe.cond,&f->lock);
  1045. }
  1046. while((node->nlookup == nlookup) && node->treelock);
  1047. dequeue_path(f,&qe);
  1048. }
  1049. assert(node->nlookup >= nlookup);
  1050. node->nlookup -= nlookup;
  1051. if(node->nlookup == 0)
  1052. {
  1053. unref_node(f,node);
  1054. }
  1055. else if((node->nlookup == 1) && remember_nodes(f))
  1056. {
  1057. remembered_node_t fn;
  1058. fn.node = node;
  1059. fn.time = current_time();
  1060. kv_push(remembered_node_t,f->remembered_nodes,fn);
  1061. }
  1062. pthread_mutex_unlock(&f->lock);
  1063. }
  1064. static
  1065. void
  1066. unlink_node(struct fuse *f,
  1067. node_t *node)
  1068. {
  1069. if(remember_nodes(f))
  1070. {
  1071. assert(node->nlookup > 1);
  1072. node->nlookup--;
  1073. }
  1074. unhash_name(f,node);
  1075. }
  1076. static
  1077. void
  1078. remove_node(struct fuse *f,
  1079. uint64_t dir,
  1080. const char *name)
  1081. {
  1082. node_t *node;
  1083. pthread_mutex_lock(&f->lock);
  1084. node = lookup_node(f,dir,name);
  1085. if(node != NULL)
  1086. unlink_node(f,node);
  1087. pthread_mutex_unlock(&f->lock);
  1088. }
  1089. static
  1090. int
  1091. rename_node(struct fuse *f,
  1092. uint64_t olddir,
  1093. const char *oldname,
  1094. uint64_t newdir,
  1095. const char *newname)
  1096. {
  1097. node_t *node;
  1098. node_t *newnode;
  1099. int err = 0;
  1100. pthread_mutex_lock(&f->lock);
  1101. node = lookup_node(f,olddir,oldname);
  1102. newnode = lookup_node(f,newdir,newname);
  1103. if(node == NULL)
  1104. goto out;
  1105. if(newnode != NULL)
  1106. unlink_node(f,newnode);
  1107. unhash_name(f,node);
  1108. if(hash_name(f,node,newdir,newname) == -1)
  1109. {
  1110. err = -ENOMEM;
  1111. goto out;
  1112. }
  1113. out:
  1114. pthread_mutex_unlock(&f->lock);
  1115. return err;
  1116. }
  1117. static
  1118. void
  1119. set_stat(struct fuse *f,
  1120. uint64_t nodeid,
  1121. struct stat *stbuf)
  1122. {
  1123. if(f->conf.set_mode)
  1124. stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
  1125. if(f->conf.set_uid)
  1126. stbuf->st_uid = f->conf.uid;
  1127. if(f->conf.set_gid)
  1128. stbuf->st_gid = f->conf.gid;
  1129. }
  1130. static
  1131. struct fuse*
  1132. req_fuse(fuse_req_t req)
  1133. {
  1134. return (struct fuse*)fuse_req_userdata(req);
  1135. }
  1136. static
  1137. int
  1138. node_open(const node_t *node_)
  1139. {
  1140. return ((node_ != NULL) && (node_->open_count > 0));
  1141. }
  1142. static
  1143. void
  1144. update_stat(node_t *node_,
  1145. const struct stat *stnew_)
  1146. {
  1147. uint32_t crc32b;
  1148. crc32b = stat_crc32b(stnew_);
  1149. if(node_->is_stat_cache_valid && (crc32b != node_->stat_crc32b))
  1150. node_->is_stat_cache_valid = 0;
  1151. node_->stat_crc32b = crc32b;
  1152. }
  1153. static
  1154. int
  1155. set_path_info(struct fuse *f,
  1156. uint64_t nodeid,
  1157. const char *name,
  1158. struct fuse_entry_param *e)
  1159. {
  1160. node_t *node;
  1161. node = find_node(f,nodeid,name);
  1162. if(node == NULL)
  1163. return -ENOMEM;
  1164. e->ino = node->nodeid;
  1165. e->generation = ((e->ino == FUSE_ROOT_ID) ? 0 : f->nodeid_gen.generation);
  1166. pthread_mutex_lock(&f->lock);
  1167. update_stat(node,&e->attr);
  1168. pthread_mutex_unlock(&f->lock);
  1169. set_stat(f,e->ino,&e->attr);
  1170. return 0;
  1171. }
  1172. /*
  1173. lookup requests only come in for FUSE_ROOT_ID when a "parent of
  1174. child of root node" request is made. This can happen when using
  1175. EXPORT_SUPPORT=true and a file handle is used to keep a reference to
  1176. a node which has been forgotten. Mostly a NFS concern but not
  1177. excluslively. Root node always has a nodeid of 1 and generation of
  1178. 0. To ensure this set_path_info() explicitly ensures the root id has
  1179. a generation of 0.
  1180. */
  1181. static
  1182. int
  1183. lookup_path(struct fuse *f,
  1184. uint64_t nodeid,
  1185. const char *name,
  1186. const char *path,
  1187. struct fuse_entry_param *e,
  1188. fuse_file_info_t *fi)
  1189. {
  1190. int rv;
  1191. memset(e,0,sizeof(struct fuse_entry_param));
  1192. rv = ((fi == NULL) ?
  1193. f->fs->op.getattr(path,&e->attr,&e->timeout) :
  1194. f->fs->op.fgetattr(fi,&e->attr,&e->timeout));
  1195. if(rv)
  1196. return rv;
  1197. return set_path_info(f,nodeid,name,e);
  1198. }
  1199. static
  1200. struct fuse_context_i*
  1201. fuse_get_context_internal(void)
  1202. {
  1203. struct fuse_context_i *c;
  1204. c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
  1205. if(c == NULL)
  1206. {
  1207. c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
  1208. if(c == NULL)
  1209. {
  1210. /* This is hard to deal with properly,so just
  1211. abort. If memory is so low that the
  1212. context cannot be allocated,there's not
  1213. much hope for the filesystem anyway */
  1214. fprintf(stderr,"fuse: failed to allocate thread specific data\n");
  1215. abort();
  1216. }
  1217. pthread_setspecific(fuse_context_key,c);
  1218. }
  1219. return c;
  1220. }
  1221. static
  1222. void
  1223. fuse_freecontext(void *data)
  1224. {
  1225. free(data);
  1226. }
  1227. static
  1228. int
  1229. fuse_create_context_key(void)
  1230. {
  1231. int err = 0;
  1232. pthread_mutex_lock(&fuse_context_lock);
  1233. if(!fuse_context_ref)
  1234. {
  1235. err = pthread_key_create(&fuse_context_key,fuse_freecontext);
  1236. if(err)
  1237. {
  1238. fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
  1239. strerror(err));
  1240. pthread_mutex_unlock(&fuse_context_lock);
  1241. return -1;
  1242. }
  1243. }
  1244. fuse_context_ref++;
  1245. pthread_mutex_unlock(&fuse_context_lock);
  1246. return 0;
  1247. }
  1248. static
  1249. void
  1250. fuse_delete_context_key(void)
  1251. {
  1252. pthread_mutex_lock(&fuse_context_lock);
  1253. fuse_context_ref--;
  1254. if(!fuse_context_ref)
  1255. {
  1256. free(pthread_getspecific(fuse_context_key));
  1257. pthread_key_delete(fuse_context_key);
  1258. }
  1259. pthread_mutex_unlock(&fuse_context_lock);
  1260. }
  1261. static
  1262. struct fuse*
  1263. req_fuse_prepare(fuse_req_t req)
  1264. {
  1265. struct fuse_context_i *c = fuse_get_context_internal();
  1266. const struct fuse_ctx *ctx = fuse_req_ctx(req);
  1267. c->req = req;
  1268. c->ctx.fuse = req_fuse(req);
  1269. c->ctx.uid = ctx->uid;
  1270. c->ctx.gid = ctx->gid;
  1271. c->ctx.pid = ctx->pid;
  1272. c->ctx.umask = ctx->umask;
  1273. return c->ctx.fuse;
  1274. }
  1275. static
  1276. void
  1277. reply_entry(fuse_req_t req,
  1278. const struct fuse_entry_param *e,
  1279. int err)
  1280. {
  1281. if(!err)
  1282. {
  1283. struct fuse *f = req_fuse(req);
  1284. if(fuse_reply_entry(req,e) == -ENOENT)
  1285. {
  1286. /* Skip forget for negative result */
  1287. if(e->ino != 0)
  1288. forget_node(f,e->ino,1);
  1289. }
  1290. }
  1291. else
  1292. {
  1293. fuse_reply_err(req,err);
  1294. }
  1295. }
  1296. static
  1297. void
  1298. fuse_lib_init(void *data,
  1299. struct fuse_conn_info *conn)
  1300. {
  1301. struct fuse *f = (struct fuse *)data;
  1302. struct fuse_context_i *c = fuse_get_context_internal();
  1303. memset(c,0,sizeof(*c));
  1304. c->ctx.fuse = f;
  1305. f->fs->op.init(conn);
  1306. }
  1307. static
  1308. void
  1309. fuse_lib_destroy(void *data)
  1310. {
  1311. struct fuse *f = (struct fuse *)data;
  1312. struct fuse_context_i *c = fuse_get_context_internal();
  1313. memset(c,0,sizeof(*c));
  1314. c->ctx.fuse = f;
  1315. f->fs->op.destroy();
  1316. free(f->fs);
  1317. f->fs = NULL;
  1318. }
  1319. static
  1320. void
  1321. fuse_lib_lookup(fuse_req_t req,
  1322. struct fuse_in_header *hdr_)
  1323. {
  1324. int err;
  1325. uint64_t nodeid;
  1326. char *path;
  1327. const char *name;
  1328. struct fuse *f;
  1329. node_t *dot = NULL;
  1330. struct fuse_entry_param e = {0};
  1331. name = fuse_hdr_arg(hdr_);
  1332. nodeid = hdr_->nodeid;
  1333. f = req_fuse_prepare(req);
  1334. if(name[0] == '.')
  1335. {
  1336. if(name[1] == '\0')
  1337. {
  1338. name = NULL;
  1339. pthread_mutex_lock(&f->lock);
  1340. dot = get_node_nocheck(f,nodeid);
  1341. if(dot == NULL)
  1342. {
  1343. pthread_mutex_unlock(&f->lock);
  1344. reply_entry(req,&e,-ESTALE);
  1345. return;
  1346. }
  1347. dot->refctr++;
  1348. pthread_mutex_unlock(&f->lock);
  1349. }
  1350. else if((name[1] == '.') && (name[2] == '\0'))
  1351. {
  1352. if(nodeid == 1)
  1353. {
  1354. reply_entry(req,&e,-ENOENT);
  1355. return;
  1356. }
  1357. name = NULL;
  1358. pthread_mutex_lock(&f->lock);
  1359. nodeid = get_node(f,nodeid)->parent->nodeid;
  1360. pthread_mutex_unlock(&f->lock);
  1361. }
  1362. }
  1363. err = get_path_name(f,nodeid,name,&path);
  1364. if(!err)
  1365. {
  1366. err = lookup_path(f,nodeid,name,path,&e,NULL);
  1367. if(err == -ENOENT)
  1368. {
  1369. e.ino = 0;
  1370. err = 0;
  1371. }
  1372. free_path(f,nodeid,path);
  1373. }
  1374. if(dot)
  1375. {
  1376. pthread_mutex_lock(&f->lock);
  1377. unref_node(f,dot);
  1378. pthread_mutex_unlock(&f->lock);
  1379. }
  1380. reply_entry(req,&e,err);
  1381. }
  1382. static
  1383. void
  1384. fuse_lib_forget(fuse_req_t req,
  1385. struct fuse_in_header *hdr_)
  1386. {
  1387. struct fuse *f;
  1388. struct fuse_forget_in *arg;
  1389. f = req_fuse(req);
  1390. arg = fuse_hdr_arg(hdr_);
  1391. forget_node(f,hdr_->nodeid,arg->nlookup);
  1392. fuse_reply_none(req);
  1393. }
  1394. static
  1395. void
  1396. fuse_lib_forget_multi(fuse_req_t req,
  1397. struct fuse_in_header *hdr_)
  1398. {
  1399. struct fuse *f;
  1400. struct fuse_batch_forget_in *arg;
  1401. struct fuse_forget_one *entry;
  1402. f = req_fuse(req);
  1403. arg = fuse_hdr_arg(hdr_);
  1404. entry = PARAM(arg);
  1405. for(uint32_t i = 0; i < arg->count; i++)
  1406. {
  1407. forget_node(f,
  1408. entry[i].nodeid,
  1409. entry[i].nlookup);
  1410. }
  1411. fuse_reply_none(req);
  1412. }
  1413. static
  1414. void
  1415. fuse_lib_getattr(fuse_req_t req,
  1416. struct fuse_in_header *hdr_)
  1417. {
  1418. int err;
  1419. char *path;
  1420. struct fuse *f;
  1421. struct stat buf;
  1422. node_t *node;
  1423. fuse_timeouts_t timeout;
  1424. fuse_file_info_t ffi = {0};
  1425. const struct fuse_getattr_in *arg;
  1426. arg = fuse_hdr_arg(hdr_);
  1427. f = req_fuse_prepare(req);
  1428. if(arg->getattr_flags & FUSE_GETATTR_FH)
  1429. {
  1430. ffi.fh = arg->fh;
  1431. }
  1432. else
  1433. {
  1434. pthread_mutex_lock(&f->lock);
  1435. node = get_node(f,hdr_->nodeid);
  1436. if(node->hidden_fh)
  1437. ffi.fh = node->hidden_fh;
  1438. pthread_mutex_unlock(&f->lock);
  1439. }
  1440. memset(&buf,0,sizeof(buf));
  1441. err = 0;
  1442. path = NULL;
  1443. if(ffi.fh == 0)
  1444. err = get_path(f,hdr_->nodeid,&path);
  1445. if(!err)
  1446. {
  1447. err = ((ffi.fh == 0) ?
  1448. f->fs->op.getattr(path,&buf,&timeout) :
  1449. f->fs->op.fgetattr(&ffi,&buf,&timeout));
  1450. free_path(f,hdr_->nodeid,path);
  1451. }
  1452. if(!err)
  1453. {
  1454. pthread_mutex_lock(&f->lock);
  1455. node = get_node(f,hdr_->nodeid);
  1456. update_stat(node,&buf);
  1457. pthread_mutex_unlock(&f->lock);
  1458. set_stat(f,hdr_->nodeid,&buf);
  1459. fuse_reply_attr(req,&buf,timeout.attr);
  1460. }
  1461. else
  1462. {
  1463. fuse_reply_err(req,err);
  1464. }
  1465. }
  1466. static
  1467. void
  1468. fuse_lib_setattr(fuse_req_t req,
  1469. struct fuse_in_header *hdr_)
  1470. {
  1471. struct fuse *f = req_fuse_prepare(req);
  1472. struct stat stbuf = {0};
  1473. char *path;
  1474. int err;
  1475. node_t *node;
  1476. fuse_timeouts_t timeout;
  1477. fuse_file_info_t *fi;
  1478. fuse_file_info_t ffi = {0};
  1479. struct fuse_setattr_in *arg;
  1480. arg = fuse_hdr_arg(hdr_);
  1481. fi = NULL;
  1482. if(arg->valid & FATTR_FH)
  1483. {
  1484. fi = &ffi;
  1485. fi->fh = arg->fh;
  1486. }
  1487. else
  1488. {
  1489. pthread_mutex_lock(&f->lock);
  1490. node = get_node(f,hdr_->nodeid);
  1491. if(node->hidden_fh)
  1492. {
  1493. fi = &ffi;
  1494. fi->fh = node->hidden_fh;
  1495. }
  1496. pthread_mutex_unlock(&f->lock);
  1497. }
  1498. err = 0;
  1499. path = NULL;
  1500. if(fi == NULL)
  1501. err = get_path(f,hdr_->nodeid,&path);
  1502. if(!err)
  1503. {
  1504. err = 0;
  1505. if(!err && (arg->valid & FATTR_MODE))
  1506. err = ((fi == NULL) ?
  1507. f->fs->op.chmod(path,arg->mode) :
  1508. f->fs->op.fchmod(fi,arg->mode));
  1509. if(!err && (arg->valid & (FATTR_UID | FATTR_GID)))
  1510. {
  1511. uid_t uid = ((arg->valid & FATTR_UID) ? arg->uid : (uid_t)-1);
  1512. gid_t gid = ((arg->valid & FATTR_GID) ? arg->gid : (gid_t)-1);
  1513. err = ((fi == NULL) ?
  1514. f->fs->op.chown(path,uid,gid) :
  1515. f->fs->op.fchown(fi,uid,gid));
  1516. }
  1517. if(!err && (arg->valid & FATTR_SIZE))
  1518. err = ((fi == NULL) ?
  1519. f->fs->op.truncate(path,arg->size) :
  1520. f->fs->op.ftruncate(fi,arg->size));
  1521. #ifdef HAVE_UTIMENSAT
  1522. if(!err && (arg->valid & (FATTR_ATIME | FATTR_MTIME)))
  1523. {
  1524. struct timespec tv[2];
  1525. tv[0].tv_sec = 0;
  1526. tv[1].tv_sec = 0;
  1527. tv[0].tv_nsec = UTIME_OMIT;
  1528. tv[1].tv_nsec = UTIME_OMIT;
  1529. if(arg->valid & FATTR_ATIME_NOW)
  1530. tv[0].tv_nsec = UTIME_NOW;
  1531. else if(arg->valid & FATTR_ATIME)
  1532. tv[0] = (struct timespec){ arg->atime, arg->atimensec };
  1533. if(arg->valid & FATTR_MTIME_NOW)
  1534. tv[1].tv_nsec = UTIME_NOW;
  1535. else if(arg->valid & FATTR_MTIME)
  1536. tv[1] = (struct timespec){ arg->mtime, arg->mtimensec };
  1537. err = ((fi == NULL) ?
  1538. f->fs->op.utimens(path,tv) :
  1539. f->fs->op.futimens(fi,tv));
  1540. }
  1541. else
  1542. #endif
  1543. if(!err && ((arg->valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
  1544. {
  1545. struct timespec tv[2];
  1546. tv[0].tv_sec = arg->atime;
  1547. tv[0].tv_nsec = arg->atimensec;
  1548. tv[1].tv_sec = arg->mtime;
  1549. tv[1].tv_nsec = arg->mtimensec;
  1550. err = ((fi == NULL) ?
  1551. f->fs->op.utimens(path,tv) :
  1552. f->fs->op.futimens(fi,tv));
  1553. }
  1554. if(!err)
  1555. err = ((fi == NULL) ?
  1556. f->fs->op.getattr(path,&stbuf,&timeout) :
  1557. f->fs->op.fgetattr(fi,&stbuf,&timeout));
  1558. free_path(f,hdr_->nodeid,path);
  1559. }
  1560. if(!err)
  1561. {
  1562. pthread_mutex_lock(&f->lock);
  1563. update_stat(get_node(f,hdr_->nodeid),&stbuf);
  1564. pthread_mutex_unlock(&f->lock);
  1565. set_stat(f,hdr_->nodeid,&stbuf);
  1566. fuse_reply_attr(req,&stbuf,timeout.attr);
  1567. }
  1568. else
  1569. {
  1570. fuse_reply_err(req,err);
  1571. }
  1572. }
  1573. static
  1574. void
  1575. fuse_lib_access(fuse_req_t req,
  1576. struct fuse_in_header *hdr_)
  1577. {
  1578. int err;
  1579. char *path;
  1580. struct fuse *f;
  1581. struct fuse_access_in *arg;
  1582. arg = fuse_hdr_arg(hdr_);
  1583. f = req_fuse_prepare(req);
  1584. err = get_path(f,hdr_->nodeid,&path);
  1585. if(!err)
  1586. {
  1587. err = f->fs->op.access(path,arg->mask);
  1588. free_path(f,hdr_->nodeid,path);
  1589. }
  1590. fuse_reply_err(req,err);
  1591. }
  1592. static
  1593. void
  1594. fuse_lib_readlink(fuse_req_t req,
  1595. struct fuse_in_header *hdr_)
  1596. {
  1597. int err;
  1598. char *path;
  1599. struct fuse *f;
  1600. char linkname[PATH_MAX + 1];
  1601. f = req_fuse_prepare(req);
  1602. err = get_path(f,hdr_->nodeid,&path);
  1603. if(!err)
  1604. {
  1605. err = f->fs->op.readlink(path,linkname,sizeof(linkname));
  1606. free_path(f,hdr_->nodeid,path);
  1607. }
  1608. if(!err)
  1609. {
  1610. linkname[PATH_MAX] = '\0';
  1611. fuse_reply_readlink(req,linkname);
  1612. }
  1613. else
  1614. {
  1615. fuse_reply_err(req,err);
  1616. }
  1617. }
  1618. static
  1619. void
  1620. fuse_lib_mknod(fuse_req_t req,
  1621. struct fuse_in_header *hdr_)
  1622. {
  1623. int err;
  1624. char *path;
  1625. struct fuse *f;
  1626. const char* name;
  1627. struct fuse_entry_param e;
  1628. struct fuse_mknod_in *arg;
  1629. arg = fuse_hdr_arg(hdr_);
  1630. name = PARAM(arg);
  1631. if(req->f->conn.proto_minor >= 12)
  1632. req->ctx.umask = arg->umask;
  1633. else
  1634. name = (char*)arg + FUSE_COMPAT_MKNOD_IN_SIZE;
  1635. f = req_fuse_prepare(req);
  1636. err = get_path_name(f,hdr_->nodeid,name,&path);
  1637. if(!err)
  1638. {
  1639. err = -ENOSYS;
  1640. if(S_ISREG(arg->mode))
  1641. {
  1642. fuse_file_info_t fi;
  1643. memset(&fi,0,sizeof(fi));
  1644. fi.flags = O_CREAT | O_EXCL | O_WRONLY;
  1645. err = f->fs->op.create(path,arg->mode,&fi);
  1646. if(!err)
  1647. {
  1648. err = lookup_path(f,hdr_->nodeid,name,path,&e,&fi);
  1649. f->fs->op.release(&fi);
  1650. }
  1651. }
  1652. if(err == -ENOSYS)
  1653. {
  1654. err = f->fs->op.mknod(path,arg->mode,arg->rdev);
  1655. if(!err)
  1656. err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
  1657. }
  1658. free_path(f,hdr_->nodeid,path);
  1659. }
  1660. reply_entry(req,&e,err);
  1661. }
  1662. static
  1663. void
  1664. fuse_lib_mkdir(fuse_req_t req,
  1665. struct fuse_in_header *hdr_)
  1666. {
  1667. int err;
  1668. char *path;
  1669. struct fuse *f;
  1670. const char *name;
  1671. struct fuse_entry_param e;
  1672. struct fuse_mkdir_in *arg;
  1673. arg = fuse_hdr_arg(hdr_);
  1674. name = PARAM(arg);
  1675. if(req->f->conn.proto_minor >= 12)
  1676. req->ctx.umask = arg->umask;
  1677. f = req_fuse_prepare(req);
  1678. err = get_path_name(f,hdr_->nodeid,name,&path);
  1679. if(!err)
  1680. {
  1681. err = f->fs->op.mkdir(path,arg->mode);
  1682. if(!err)
  1683. err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
  1684. free_path(f,hdr_->nodeid,path);
  1685. }
  1686. reply_entry(req,&e,err);
  1687. }
  1688. static
  1689. void
  1690. fuse_lib_unlink(fuse_req_t req,
  1691. struct fuse_in_header *hdr_)
  1692. {
  1693. int err;
  1694. char *path;
  1695. struct fuse *f;
  1696. const char *name;
  1697. node_t *wnode;
  1698. name = PARAM(hdr_);
  1699. f = req_fuse_prepare(req);
  1700. err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
  1701. if(!err)
  1702. {
  1703. pthread_mutex_lock(&f->lock);
  1704. if(node_open(wnode))
  1705. err = f->fs->op.prepare_hide(path,&wnode->hidden_fh);
  1706. pthread_mutex_unlock(&f->lock);
  1707. err = f->fs->op.unlink(path);
  1708. if(!err)
  1709. remove_node(f,hdr_->nodeid,name);
  1710. free_path_wrlock(f,hdr_->nodeid,wnode,path);
  1711. }
  1712. fuse_reply_err(req,err);
  1713. }
  1714. static
  1715. void
  1716. fuse_lib_rmdir(fuse_req_t req,
  1717. struct fuse_in_header *hdr_)
  1718. {
  1719. int err;
  1720. char *path;
  1721. struct fuse *f;
  1722. const char *name;
  1723. node_t *wnode;
  1724. name = PARAM(hdr_);
  1725. f = req_fuse_prepare(req);
  1726. err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
  1727. if(!err)
  1728. {
  1729. err = f->fs->op.rmdir(path);
  1730. if(!err)
  1731. remove_node(f,hdr_->nodeid,name);
  1732. free_path_wrlock(f,hdr_->nodeid,wnode,path);
  1733. }
  1734. fuse_reply_err(req,err);
  1735. }
  1736. static
  1737. void
  1738. fuse_lib_symlink(fuse_req_t req_,
  1739. struct fuse_in_header *hdr_)
  1740. {
  1741. int rv;
  1742. char *path;
  1743. struct fuse *f;
  1744. const char *name;
  1745. const char *linkname;
  1746. struct fuse_entry_param e = {0};
  1747. name = fuse_hdr_arg(hdr_);
  1748. linkname = (name + strlen(name) + 1);
  1749. f = req_fuse_prepare(req_);
  1750. rv = get_path_name(f,hdr_->nodeid,name,&path);
  1751. if(rv == 0)
  1752. {
  1753. rv = f->fs->op.symlink(linkname,path,&e.attr,&e.timeout);
  1754. if(rv == 0)
  1755. rv = set_path_info(f,hdr_->nodeid,name,&e);
  1756. free_path(f,hdr_->nodeid,path);
  1757. }
  1758. reply_entry(req_,&e,rv);
  1759. }
  1760. static
  1761. void
  1762. fuse_lib_rename(fuse_req_t req,
  1763. struct fuse_in_header *hdr_)
  1764. {
  1765. int err;
  1766. struct fuse *f;
  1767. char *oldpath;
  1768. char *newpath;
  1769. const char *oldname;
  1770. const char *newname;
  1771. node_t *wnode1;
  1772. node_t *wnode2;
  1773. struct fuse_rename_in *arg;
  1774. arg = fuse_hdr_arg(hdr_);
  1775. oldname = PARAM(arg);
  1776. newname = (oldname + strlen(oldname) + 1);
  1777. f = req_fuse_prepare(req);
  1778. err = get_path2(f,hdr_->nodeid,oldname,arg->newdir,newname,
  1779. &oldpath,&newpath,&wnode1,&wnode2);
  1780. if(!err)
  1781. {
  1782. pthread_mutex_lock(&f->lock);
  1783. if(node_open(wnode2))
  1784. err = f->fs->op.prepare_hide(newpath,&wnode2->hidden_fh);
  1785. pthread_mutex_unlock(&f->lock);
  1786. err = f->fs->op.rename(oldpath,newpath);
  1787. if(!err)
  1788. err = rename_node(f,hdr_->nodeid,oldname,arg->newdir,newname);
  1789. free_path2(f,hdr_->nodeid,arg->newdir,wnode1,wnode2,oldpath,newpath);
  1790. }
  1791. fuse_reply_err(req,err);
  1792. }
  1793. static
  1794. void
  1795. fuse_lib_link(fuse_req_t req,
  1796. struct fuse_in_header *hdr_)
  1797. {
  1798. int rv;
  1799. char *oldpath;
  1800. char *newpath;
  1801. struct fuse *f;
  1802. const char *newname;
  1803. struct fuse_link_in *arg;
  1804. struct fuse_entry_param e = {0};
  1805. arg = fuse_hdr_arg(hdr_);
  1806. newname = PARAM(arg);
  1807. f = req_fuse_prepare(req);
  1808. rv = get_path2(f,
  1809. arg->oldnodeid,NULL,
  1810. hdr_->nodeid,newname,
  1811. &oldpath,&newpath,NULL,NULL);
  1812. if(!rv)
  1813. {
  1814. rv = f->fs->op.link(oldpath,newpath,&e.attr,&e.timeout);
  1815. if(rv == 0)
  1816. rv = set_path_info(f,hdr_->nodeid,newname,&e);
  1817. free_path2(f,arg->oldnodeid,hdr_->nodeid,NULL,NULL,oldpath,newpath);
  1818. }
  1819. reply_entry(req,&e,rv);
  1820. }
  1821. static
  1822. void
  1823. fuse_do_release(struct fuse *f,
  1824. uint64_t ino,
  1825. fuse_file_info_t *fi)
  1826. {
  1827. uint64_t fh;
  1828. node_t *node;
  1829. fh = 0;
  1830. f->fs->op.release(fi);
  1831. pthread_mutex_lock(&f->lock);
  1832. {
  1833. node = get_node(f,ino);
  1834. assert(node->open_count > 0);
  1835. node->open_count--;
  1836. if(node->hidden_fh && (node->open_count == 0))
  1837. {
  1838. fh = node->hidden_fh;
  1839. node->hidden_fh = 0;
  1840. }
  1841. }
  1842. pthread_mutex_unlock(&f->lock);
  1843. if(fh)
  1844. f->fs->op.free_hide(fh);
  1845. }
  1846. static
  1847. void
  1848. fuse_lib_create(fuse_req_t req,
  1849. struct fuse_in_header *hdr_)
  1850. {
  1851. int err;
  1852. char *path;
  1853. struct fuse *f;
  1854. const char *name;
  1855. fuse_file_info_t ffi = {0};
  1856. struct fuse_entry_param e;
  1857. struct fuse_create_in *arg;
  1858. arg = fuse_hdr_arg(hdr_);
  1859. name = PARAM(arg);
  1860. ffi.flags = arg->flags;
  1861. if(req->f->conn.proto_minor >= 12)
  1862. req->ctx.umask = arg->umask;
  1863. else
  1864. name = (char*)arg + sizeof(struct fuse_open_in);
  1865. f = req_fuse_prepare(req);
  1866. err = get_path_name(f,hdr_->nodeid,name,&path);
  1867. if(!err)
  1868. {
  1869. err = f->fs->op.create(path,arg->mode,&ffi);
  1870. if(!err)
  1871. {
  1872. err = lookup_path(f,hdr_->nodeid,name,path,&e,&ffi);
  1873. if(err)
  1874. {
  1875. f->fs->op.release(&ffi);
  1876. }
  1877. else if(!S_ISREG(e.attr.st_mode))
  1878. {
  1879. err = -EIO;
  1880. f->fs->op.release(&ffi);
  1881. forget_node(f,e.ino,1);
  1882. }
  1883. }
  1884. }
  1885. if(!err)
  1886. {
  1887. pthread_mutex_lock(&f->lock);
  1888. get_node(f,e.ino)->open_count++;
  1889. pthread_mutex_unlock(&f->lock);
  1890. if(fuse_reply_create(req,&e,&ffi) == -ENOENT)
  1891. {
  1892. /* The open syscall was interrupted,so it
  1893. must be cancelled */
  1894. fuse_do_release(f,e.ino,&ffi);
  1895. forget_node(f,e.ino,1);
  1896. }
  1897. }
  1898. else
  1899. {
  1900. fuse_reply_err(req,err);
  1901. }
  1902. free_path(f,hdr_->nodeid,path);
  1903. }
  1904. static
  1905. void
  1906. open_auto_cache(struct fuse *f,
  1907. uint64_t ino,
  1908. const char *path,
  1909. fuse_file_info_t *fi)
  1910. {
  1911. node_t *node;
  1912. fuse_timeouts_t timeout = {1,1};
  1913. pthread_mutex_lock(&f->lock);
  1914. node = get_node(f,ino);
  1915. if(node->is_stat_cache_valid)
  1916. {
  1917. int err;
  1918. struct stat stbuf;
  1919. pthread_mutex_unlock(&f->lock);
  1920. err = f->fs->op.fgetattr(fi,&stbuf,&timeout);
  1921. pthread_mutex_lock(&f->lock);
  1922. if(!err)
  1923. update_stat(node,&stbuf);
  1924. else
  1925. node->is_stat_cache_valid = 0;
  1926. }
  1927. if(node->is_stat_cache_valid)
  1928. fi->keep_cache = 1;
  1929. node->is_stat_cache_valid = 1;
  1930. pthread_mutex_unlock(&f->lock);
  1931. }
  1932. static
  1933. void
  1934. fuse_lib_open(fuse_req_t req,
  1935. struct fuse_in_header *hdr_)
  1936. {
  1937. int err;
  1938. char *path;
  1939. struct fuse *f;
  1940. fuse_file_info_t ffi = {0};
  1941. struct fuse_open_in *arg;
  1942. arg = fuse_hdr_arg(hdr_);
  1943. ffi.flags = arg->flags;
  1944. f = req_fuse_prepare(req);
  1945. err = get_path(f,hdr_->nodeid,&path);
  1946. if(!err)
  1947. {
  1948. err = f->fs->op.open(path,&ffi);
  1949. if(!err)
  1950. {
  1951. if(ffi.auto_cache)
  1952. open_auto_cache(f,hdr_->nodeid,path,&ffi);
  1953. }
  1954. }
  1955. if(!err)
  1956. {
  1957. pthread_mutex_lock(&f->lock);
  1958. get_node(f,hdr_->nodeid)->open_count++;
  1959. pthread_mutex_unlock(&f->lock);
  1960. /* The open syscall was interrupted,so it must be cancelled */
  1961. if(fuse_reply_open(req,&ffi) == -ENOENT)
  1962. fuse_do_release(f,hdr_->nodeid,&ffi);
  1963. }
  1964. else
  1965. {
  1966. fuse_reply_err(req,err);
  1967. }
  1968. free_path(f,hdr_->nodeid,path);
  1969. }
  1970. static
  1971. void
  1972. fuse_lib_read(fuse_req_t req,
  1973. struct fuse_in_header *hdr_)
  1974. {
  1975. int res;
  1976. struct fuse *f;
  1977. fuse_file_info_t ffi = {0};
  1978. struct fuse_read_in *arg;
  1979. fuse_msgbuf_t *msgbuf;
  1980. arg = fuse_hdr_arg(hdr_);
  1981. ffi.fh = arg->fh;
  1982. if(req->f->conn.proto_minor >= 9)
  1983. {
  1984. ffi.flags = arg->flags;
  1985. ffi.lock_owner = arg->lock_owner;
  1986. }
  1987. f = req_fuse_prepare(req);
  1988. msgbuf = msgbuf_alloc_page_aligned();
  1989. res = f->fs->op.read(&ffi,msgbuf->mem,arg->size,arg->offset);
  1990. if(res >= 0)
  1991. fuse_reply_data(req,msgbuf->mem,res);
  1992. else
  1993. fuse_reply_err(req,res);
  1994. msgbuf_free(msgbuf);
  1995. }
  1996. static
  1997. void
  1998. fuse_lib_write(fuse_req_t req,
  1999. struct fuse_in_header *hdr_)
  2000. {
  2001. int res;
  2002. char *data;
  2003. struct fuse *f;
  2004. fuse_file_info_t ffi = {0};
  2005. struct fuse_write_in *arg;
  2006. arg = fuse_hdr_arg(hdr_);
  2007. ffi.fh = arg->fh;
  2008. ffi.writepage = !!(arg->write_flags & 1);
  2009. if(req->f->conn.proto_minor < 9)
  2010. {
  2011. data = ((char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  2012. }
  2013. else
  2014. {
  2015. ffi.flags = arg->flags;
  2016. ffi.lock_owner = arg->lock_owner;
  2017. data = PARAM(arg);
  2018. }
  2019. f = req_fuse_prepare(req);
  2020. res = f->fs->op.write(&ffi,data,arg->size,arg->offset);
  2021. free_path(f,hdr_->nodeid,NULL);
  2022. if(res >= 0)
  2023. fuse_reply_write(req,res);
  2024. else
  2025. fuse_reply_err(req,res);
  2026. }
  2027. static
  2028. void
  2029. fuse_lib_fsync(fuse_req_t req,
  2030. struct fuse_in_header *hdr_)
  2031. {
  2032. int err;
  2033. struct fuse *f;
  2034. struct fuse_fsync_in *arg;
  2035. fuse_file_info_t ffi = {0};
  2036. arg = fuse_hdr_arg(hdr_);
  2037. ffi.fh = arg->fh;
  2038. f = req_fuse_prepare(req);
  2039. err = f->fs->op.fsync(&ffi,
  2040. !!(arg->fsync_flags & 1));
  2041. fuse_reply_err(req,err);
  2042. }
  2043. static
  2044. struct fuse_dh*
  2045. get_dirhandle(const fuse_file_info_t *llfi,
  2046. fuse_file_info_t *fi)
  2047. {
  2048. struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
  2049. memset(fi,0,sizeof(fuse_file_info_t));
  2050. fi->fh = dh->fh;
  2051. return dh;
  2052. }
  2053. static
  2054. void
  2055. fuse_lib_opendir(fuse_req_t req,
  2056. struct fuse_in_header *hdr_)
  2057. {
  2058. int err;
  2059. char *path;
  2060. struct fuse_dh *dh;
  2061. fuse_file_info_t llffi = {0};
  2062. fuse_file_info_t ffi = {0};
  2063. struct fuse *f;
  2064. struct fuse_open_in *arg;
  2065. arg = fuse_hdr_arg(hdr_);
  2066. llffi.flags = arg->flags;
  2067. f = req_fuse_prepare(req);
  2068. dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
  2069. if(dh == NULL)
  2070. {
  2071. fuse_reply_err(req,ENOMEM);
  2072. return;
  2073. }
  2074. fuse_dirents_init(&dh->d);
  2075. fuse_mutex_init(&dh->lock);
  2076. llffi.fh = (uintptr_t)dh;
  2077. ffi.flags = llffi.flags;
  2078. err = get_path(f,hdr_->nodeid,&path);
  2079. if(!err)
  2080. {
  2081. err = f->fs->op.opendir(path,&ffi);
  2082. dh->fh = ffi.fh;
  2083. llffi.keep_cache = ffi.keep_cache;
  2084. llffi.cache_readdir = ffi.cache_readdir;
  2085. }
  2086. if(!err)
  2087. {
  2088. if(fuse_reply_open(req,&llffi) == -ENOENT)
  2089. {
  2090. /* The opendir syscall was interrupted,so it
  2091. must be cancelled */
  2092. f->fs->op.releasedir(&ffi);
  2093. pthread_mutex_destroy(&dh->lock);
  2094. free(dh);
  2095. }
  2096. }
  2097. else
  2098. {
  2099. fuse_reply_err(req,err);
  2100. pthread_mutex_destroy(&dh->lock);
  2101. free(dh);
  2102. }
  2103. free_path(f,hdr_->nodeid,path);
  2104. }
  2105. static
  2106. size_t
  2107. readdir_buf_size(fuse_dirents_t *d_,
  2108. size_t size_,
  2109. off_t off_)
  2110. {
  2111. if(off_ >= kv_size(d_->offs))
  2112. return 0;
  2113. if((kv_A(d_->offs,off_) + size_) > kv_size(d_->data))
  2114. return (kv_size(d_->data) - kv_A(d_->offs,off_));
  2115. return size_;
  2116. }
  2117. static
  2118. char*
  2119. readdir_buf(fuse_dirents_t *d_,
  2120. off_t off_)
  2121. {
  2122. size_t i;
  2123. i = kv_A(d_->offs,off_);
  2124. return &kv_A(d_->data,i);
  2125. }
  2126. static
  2127. void
  2128. fuse_lib_readdir(fuse_req_t req_,
  2129. struct fuse_in_header *hdr_)
  2130. {
  2131. int rv;
  2132. size_t size;
  2133. struct fuse *f;
  2134. fuse_dirents_t *d;
  2135. struct fuse_dh *dh;
  2136. fuse_file_info_t ffi = {0};
  2137. fuse_file_info_t llffi = {0};
  2138. struct fuse_read_in *arg;
  2139. arg = fuse_hdr_arg(hdr_);
  2140. size = arg->size;
  2141. llffi.fh = arg->fh;
  2142. f = req_fuse_prepare(req_);
  2143. dh = get_dirhandle(&llffi,&ffi);
  2144. d = &dh->d;
  2145. pthread_mutex_lock(&dh->lock);
  2146. rv = 0;
  2147. if((arg->offset == 0) || (kv_size(d->data) == 0))
  2148. rv = f->fs->op.readdir(&ffi,d);
  2149. if(rv)
  2150. {
  2151. fuse_reply_err(req_,rv);
  2152. goto out;
  2153. }
  2154. size = readdir_buf_size(d,size,arg->offset);
  2155. fuse_reply_buf(req_,
  2156. readdir_buf(d,arg->offset),
  2157. size);
  2158. out:
  2159. pthread_mutex_unlock(&dh->lock);
  2160. }
  2161. static
  2162. void
  2163. fuse_lib_readdir_plus(fuse_req_t req_,
  2164. struct fuse_in_header *hdr_)
  2165. {
  2166. int rv;
  2167. size_t size;
  2168. struct fuse *f;
  2169. fuse_dirents_t *d;
  2170. struct fuse_dh *dh;
  2171. fuse_file_info_t ffi = {0};
  2172. fuse_file_info_t llffi = {0};
  2173. struct fuse_read_in *arg;
  2174. arg = fuse_hdr_arg(hdr_);
  2175. size = arg->size;
  2176. llffi.fh = arg->fh;
  2177. f = req_fuse_prepare(req_);
  2178. dh = get_dirhandle(&llffi,&ffi);
  2179. d = &dh->d;
  2180. pthread_mutex_lock(&dh->lock);
  2181. rv = 0;
  2182. if((arg->offset == 0) || (kv_size(d->data) == 0))
  2183. rv = f->fs->op.readdir_plus(&ffi,d);
  2184. if(rv)
  2185. {
  2186. fuse_reply_err(req_,rv);
  2187. goto out;
  2188. }
  2189. size = readdir_buf_size(d,size,arg->offset);
  2190. fuse_reply_buf(req_,
  2191. readdir_buf(d,arg->offset),
  2192. size);
  2193. out:
  2194. pthread_mutex_unlock(&dh->lock);
  2195. }
  2196. static
  2197. void
  2198. fuse_lib_releasedir(fuse_req_t req_,
  2199. struct fuse_in_header *hdr_)
  2200. {
  2201. struct fuse *f;
  2202. struct fuse_dh *dh;
  2203. fuse_file_info_t ffi;
  2204. fuse_file_info_t llffi = {0};
  2205. struct fuse_release_in *arg;
  2206. arg = fuse_hdr_arg(hdr_);
  2207. llffi.fh = arg->fh;
  2208. llffi.flags = arg->flags;
  2209. f = req_fuse_prepare(req_);
  2210. dh = get_dirhandle(&llffi,&ffi);
  2211. f->fs->op.releasedir(&ffi);
  2212. /* Done to keep race condition between last readdir reply and the unlock */
  2213. pthread_mutex_lock(&dh->lock);
  2214. pthread_mutex_unlock(&dh->lock);
  2215. pthread_mutex_destroy(&dh->lock);
  2216. fuse_dirents_free(&dh->d);
  2217. free(dh);
  2218. fuse_reply_err(req_,0);
  2219. }
  2220. static
  2221. void
  2222. fuse_lib_fsyncdir(fuse_req_t req,
  2223. struct fuse_in_header *hdr_)
  2224. {
  2225. int err;
  2226. struct fuse *f;
  2227. fuse_file_info_t ffi;
  2228. fuse_file_info_t llffi = {0};
  2229. struct fuse_fsync_in *arg;
  2230. arg = fuse_hdr_arg(hdr_);
  2231. llffi.fh = arg->fh;
  2232. f = req_fuse_prepare(req);
  2233. get_dirhandle(&llffi,&ffi);
  2234. err = f->fs->op.fsyncdir(&ffi,
  2235. !!(arg->fsync_flags & FUSE_FSYNC_FDATASYNC));
  2236. fuse_reply_err(req,err);
  2237. }
  2238. static
  2239. void
  2240. fuse_lib_statfs(fuse_req_t req,
  2241. struct fuse_in_header *hdr_)
  2242. {
  2243. int err = 0;
  2244. char *path = NULL;
  2245. struct fuse *f;
  2246. struct statvfs buf = {0};
  2247. f = req_fuse_prepare(req);
  2248. if(hdr_->nodeid)
  2249. err = get_path(f,hdr_->nodeid,&path);
  2250. if(!err)
  2251. {
  2252. err = f->fs->op.statfs(path ? path : "/",&buf);
  2253. free_path(f,hdr_->nodeid,path);
  2254. }
  2255. if(!err)
  2256. fuse_reply_statfs(req,&buf);
  2257. else
  2258. fuse_reply_err(req,err);
  2259. }
  2260. static
  2261. void
  2262. fuse_lib_setxattr(fuse_req_t req,
  2263. struct fuse_in_header *hdr_)
  2264. {
  2265. int err;
  2266. char *path;
  2267. const char *name;
  2268. const char *value;
  2269. struct fuse *f;
  2270. struct fuse_setxattr_in *arg;
  2271. arg = fuse_hdr_arg(hdr_);
  2272. if((req->f->conn.capable & FUSE_SETXATTR_EXT) && (req->f->conn.want & FUSE_SETXATTR_EXT))
  2273. name = PARAM(arg);
  2274. else
  2275. name = (((char*)arg) + FUSE_COMPAT_SETXATTR_IN_SIZE);
  2276. value = (name + strlen(name) + 1);
  2277. f = req_fuse_prepare(req);
  2278. err = get_path(f,hdr_->nodeid,&path);
  2279. if(!err)
  2280. {
  2281. err = f->fs->op.setxattr(path,name,value,arg->size,arg->flags);
  2282. free_path(f,hdr_->nodeid,path);
  2283. }
  2284. fuse_reply_err(req,err);
  2285. }
  2286. static
  2287. int
  2288. common_getxattr(struct fuse *f,
  2289. fuse_req_t req,
  2290. uint64_t ino,
  2291. const char *name,
  2292. char *value,
  2293. size_t size)
  2294. {
  2295. int err;
  2296. char *path;
  2297. err = get_path(f,ino,&path);
  2298. if(!err)
  2299. {
  2300. err = f->fs->op.getxattr(path,name,value,size);
  2301. free_path(f,ino,path);
  2302. }
  2303. return err;
  2304. }
  2305. static
  2306. void
  2307. fuse_lib_getxattr(fuse_req_t req,
  2308. struct fuse_in_header *hdr_)
  2309. {
  2310. int res;
  2311. struct fuse *f;
  2312. const char* name;
  2313. struct fuse_getxattr_in *arg;
  2314. arg = fuse_hdr_arg(hdr_);
  2315. name = PARAM(arg);
  2316. f = req_fuse_prepare(req);
  2317. if(arg->size)
  2318. {
  2319. char *value = (char*)malloc(arg->size);
  2320. if(value == NULL)
  2321. {
  2322. fuse_reply_err(req,ENOMEM);
  2323. return;
  2324. }
  2325. res = common_getxattr(f,req,hdr_->nodeid,name,value,arg->size);
  2326. if(res > 0)
  2327. fuse_reply_buf(req,value,res);
  2328. else
  2329. fuse_reply_err(req,res);
  2330. free(value);
  2331. }
  2332. else
  2333. {
  2334. res = common_getxattr(f,req,hdr_->nodeid,name,NULL,0);
  2335. if(res >= 0)
  2336. fuse_reply_xattr(req,res);
  2337. else
  2338. fuse_reply_err(req,res);
  2339. }
  2340. }
  2341. static
  2342. int
  2343. common_listxattr(struct fuse *f,
  2344. fuse_req_t req,
  2345. uint64_t ino,
  2346. char *list,
  2347. size_t size)
  2348. {
  2349. char *path;
  2350. int err;
  2351. err = get_path(f,ino,&path);
  2352. if(!err)
  2353. {
  2354. err = f->fs->op.listxattr(path,list,size);
  2355. free_path(f,ino,path);
  2356. }
  2357. return err;
  2358. }
  2359. static
  2360. void
  2361. fuse_lib_listxattr(fuse_req_t req,
  2362. struct fuse_in_header *hdr_)
  2363. {
  2364. int res;
  2365. struct fuse *f;
  2366. struct fuse_getxattr_in *arg;
  2367. arg = fuse_hdr_arg(hdr_);
  2368. f = req_fuse_prepare(req);
  2369. if(arg->size)
  2370. {
  2371. char *list = (char*)malloc(arg->size);
  2372. if(list == NULL)
  2373. {
  2374. fuse_reply_err(req,ENOMEM);
  2375. return;
  2376. }
  2377. res = common_listxattr(f,req,hdr_->nodeid,list,arg->size);
  2378. if(res > 0)
  2379. fuse_reply_buf(req,list,res);
  2380. else
  2381. fuse_reply_err(req,res);
  2382. free(list);
  2383. }
  2384. else
  2385. {
  2386. res = common_listxattr(f,req,hdr_->nodeid,NULL,0);
  2387. if(res >= 0)
  2388. fuse_reply_xattr(req,res);
  2389. else
  2390. fuse_reply_err(req,res);
  2391. }
  2392. }
  2393. static
  2394. void
  2395. fuse_lib_removexattr(fuse_req_t req,
  2396. const struct fuse_in_header *hdr_)
  2397. {
  2398. int err;
  2399. char *path;
  2400. const char *name;
  2401. struct fuse *f;
  2402. name = fuse_hdr_arg(hdr_);
  2403. f = req_fuse_prepare(req);
  2404. err = get_path(f,hdr_->nodeid,&path);
  2405. if(!err)
  2406. {
  2407. err = f->fs->op.removexattr(path,name);
  2408. free_path(f,hdr_->nodeid,path);
  2409. }
  2410. fuse_reply_err(req,err);
  2411. }
  2412. static
  2413. void
  2414. fuse_lib_copy_file_range(fuse_req_t req_,
  2415. const struct fuse_in_header *hdr_)
  2416. {
  2417. ssize_t rv;
  2418. struct fuse *f;
  2419. fuse_file_info_t ffi_in = {0};
  2420. fuse_file_info_t ffi_out = {0};
  2421. const struct fuse_copy_file_range_in *arg;
  2422. arg = fuse_hdr_arg(hdr_);
  2423. ffi_in.fh = arg->fh_in;
  2424. ffi_out.fh = arg->fh_out;
  2425. f = req_fuse_prepare(req_);
  2426. rv = f->fs->op.copy_file_range(&ffi_in,
  2427. arg->off_in,
  2428. &ffi_out,
  2429. arg->off_out,
  2430. arg->len,
  2431. arg->flags);
  2432. if(rv >= 0)
  2433. fuse_reply_write(req_,rv);
  2434. else
  2435. fuse_reply_err(req_,rv);
  2436. }
  2437. static
  2438. void
  2439. fuse_lib_setupmapping(fuse_req_t req_,
  2440. const struct fuse_in_header *hdr_)
  2441. {
  2442. fuse_reply_err(req_,ENOSYS);
  2443. }
  2444. static
  2445. void
  2446. fuse_lib_removemapping(fuse_req_t req_,
  2447. const struct fuse_in_header *hdr_)
  2448. {
  2449. fuse_reply_err(req_,ENOSYS);
  2450. }
  2451. static
  2452. void
  2453. fuse_lib_syncfs(fuse_req_t req_,
  2454. const struct fuse_in_header *hdr_)
  2455. {
  2456. fuse_reply_err(req_,ENOSYS);
  2457. }
  2458. // TODO: This is just a copy of fuse_lib_create. Needs to be rewritten
  2459. // so a nameless node can be setup.
  2460. // name is always '/'
  2461. // nodeid is the base directory
  2462. static
  2463. void
  2464. fuse_lib_tmpfile(fuse_req_t req_,
  2465. const struct fuse_in_header *hdr_)
  2466. {
  2467. int err;
  2468. char *path;
  2469. struct fuse *f;
  2470. const char *name;
  2471. fuse_file_info_t ffi = {0};
  2472. struct fuse_entry_param e;
  2473. struct fuse_create_in *arg;
  2474. arg = fuse_hdr_arg(hdr_);
  2475. name = PARAM(arg);
  2476. ffi.flags = arg->flags;
  2477. if(req_->f->conn.proto_minor >= 12)
  2478. req_->ctx.umask = arg->umask;
  2479. else
  2480. name = (char*)arg + sizeof(struct fuse_open_in);
  2481. f = req_fuse_prepare(req_);
  2482. err = get_path_name(f,hdr_->nodeid,name,&path);
  2483. if(!err)
  2484. {
  2485. err = f->fs->op.tmpfile(path,arg->mode,&ffi);
  2486. if(!err)
  2487. {
  2488. err = lookup_path(f,hdr_->nodeid,name,path,&e,&ffi);
  2489. if(err)
  2490. {
  2491. f->fs->op.release(&ffi);
  2492. }
  2493. else if(!S_ISREG(e.attr.st_mode))
  2494. {
  2495. err = -EIO;
  2496. f->fs->op.release(&ffi);
  2497. forget_node(f,e.ino,1);
  2498. }
  2499. }
  2500. }
  2501. if(!err)
  2502. {
  2503. pthread_mutex_lock(&f->lock);
  2504. get_node(f,e.ino)->open_count++;
  2505. pthread_mutex_unlock(&f->lock);
  2506. if(fuse_reply_create(req_,&e,&ffi) == -ENOENT)
  2507. {
  2508. /* The open syscall was interrupted,so it
  2509. must be cancelled */
  2510. fuse_do_release(f,e.ino,&ffi);
  2511. forget_node(f,e.ino,1);
  2512. }
  2513. }
  2514. else
  2515. {
  2516. fuse_reply_err(req_,err);
  2517. }
  2518. free_path(f,hdr_->nodeid,path);
  2519. }
  2520. static
  2521. lock_t*
  2522. locks_conflict(node_t *node,
  2523. const lock_t *lock)
  2524. {
  2525. lock_t *l;
  2526. for(l = node->locks; l; l = l->next)
  2527. if(l->owner != lock->owner &&
  2528. lock->start <= l->end && l->start <= lock->end &&
  2529. (l->type == F_WRLCK || lock->type == F_WRLCK))
  2530. break;
  2531. return l;
  2532. }
  2533. static
  2534. void
  2535. delete_lock(lock_t **lockp)
  2536. {
  2537. lock_t *l = *lockp;
  2538. *lockp = l->next;
  2539. free(l);
  2540. }
  2541. static
  2542. void
  2543. insert_lock(lock_t **pos,
  2544. lock_t *lock)
  2545. {
  2546. lock->next = *pos;
  2547. *pos = lock;
  2548. }
  2549. static
  2550. int
  2551. locks_insert(node_t *node,
  2552. lock_t *lock)
  2553. {
  2554. lock_t **lp;
  2555. lock_t *newl1 = NULL;
  2556. lock_t *newl2 = NULL;
  2557. if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
  2558. {
  2559. newl1 = malloc(sizeof(lock_t));
  2560. newl2 = malloc(sizeof(lock_t));
  2561. if(!newl1 || !newl2)
  2562. {
  2563. free(newl1);
  2564. free(newl2);
  2565. return -ENOLCK;
  2566. }
  2567. }
  2568. for(lp = &node->locks; *lp;)
  2569. {
  2570. lock_t *l = *lp;
  2571. if(l->owner != lock->owner)
  2572. goto skip;
  2573. if(lock->type == l->type)
  2574. {
  2575. if(l->end < lock->start - 1)
  2576. goto skip;
  2577. if(lock->end < l->start - 1)
  2578. break;
  2579. if(l->start <= lock->start && lock->end <= l->end)
  2580. goto out;
  2581. if(l->start < lock->start)
  2582. lock->start = l->start;
  2583. if(lock->end < l->end)
  2584. lock->end = l->end;
  2585. goto delete;
  2586. }
  2587. else
  2588. {
  2589. if(l->end < lock->start)
  2590. goto skip;
  2591. if(lock->end < l->start)
  2592. break;
  2593. if(lock->start <= l->start && l->end <= lock->end)
  2594. goto delete;
  2595. if(l->end <= lock->end)
  2596. {
  2597. l->end = lock->start - 1;
  2598. goto skip;
  2599. }
  2600. if(lock->start <= l->start)
  2601. {
  2602. l->start = lock->end + 1;
  2603. break;
  2604. }
  2605. *newl2 = *l;
  2606. newl2->start = lock->end + 1;
  2607. l->end = lock->start - 1;
  2608. insert_lock(&l->next,newl2);
  2609. newl2 = NULL;
  2610. }
  2611. skip:
  2612. lp = &l->next;
  2613. continue;
  2614. delete:
  2615. delete_lock(lp);
  2616. }
  2617. if(lock->type != F_UNLCK)
  2618. {
  2619. *newl1 = *lock;
  2620. insert_lock(lp,newl1);
  2621. newl1 = NULL;
  2622. }
  2623. out:
  2624. free(newl1);
  2625. free(newl2);
  2626. return 0;
  2627. }
  2628. static
  2629. void
  2630. flock_to_lock(struct flock *flock,
  2631. lock_t *lock)
  2632. {
  2633. memset(lock,0,sizeof(lock_t));
  2634. lock->type = flock->l_type;
  2635. lock->start = flock->l_start;
  2636. lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
  2637. lock->pid = flock->l_pid;
  2638. }
  2639. static
  2640. void
  2641. lock_to_flock(lock_t *lock,
  2642. struct flock *flock)
  2643. {
  2644. flock->l_type = lock->type;
  2645. flock->l_start = lock->start;
  2646. flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
  2647. flock->l_pid = lock->pid;
  2648. }
  2649. static
  2650. int
  2651. fuse_flush_common(struct fuse *f,
  2652. fuse_req_t req,
  2653. uint64_t ino,
  2654. fuse_file_info_t *fi)
  2655. {
  2656. struct flock lock;
  2657. lock_t l;
  2658. int err;
  2659. int errlock;
  2660. memset(&lock,0,sizeof(lock));
  2661. lock.l_type = F_UNLCK;
  2662. lock.l_whence = SEEK_SET;
  2663. err = f->fs->op.flush(fi);
  2664. errlock = f->fs->op.lock(fi,F_SETLK,&lock);
  2665. if(errlock != -ENOSYS)
  2666. {
  2667. flock_to_lock(&lock,&l);
  2668. l.owner = fi->lock_owner;
  2669. pthread_mutex_lock(&f->lock);
  2670. locks_insert(get_node(f,ino),&l);
  2671. pthread_mutex_unlock(&f->lock);
  2672. /* if op.lock() is defined FLUSH is needed regardless
  2673. of op.flush() */
  2674. if(err == -ENOSYS)
  2675. err = 0;
  2676. }
  2677. return err;
  2678. }
  2679. static
  2680. void
  2681. fuse_lib_release(fuse_req_t req,
  2682. struct fuse_in_header *hdr_)
  2683. {
  2684. int err = 0;
  2685. struct fuse *f;
  2686. fuse_file_info_t ffi = {0};
  2687. struct fuse_release_in *arg;
  2688. arg = fuse_hdr_arg(hdr_);
  2689. ffi.fh = arg->fh;
  2690. ffi.flags = arg->flags;
  2691. if(req->f->conn.proto_minor >= 8)
  2692. {
  2693. ffi.flush = !!(arg->release_flags & FUSE_RELEASE_FLUSH);
  2694. ffi.lock_owner = arg->lock_owner;
  2695. }
  2696. else
  2697. {
  2698. ffi.flock_release = 1;
  2699. ffi.lock_owner = arg->lock_owner;
  2700. }
  2701. f = req_fuse_prepare(req);
  2702. if(ffi.flush)
  2703. {
  2704. err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
  2705. if(err == -ENOSYS)
  2706. err = 0;
  2707. }
  2708. fuse_do_release(f,hdr_->nodeid,&ffi);
  2709. fuse_reply_err(req,err);
  2710. }
  2711. static
  2712. void
  2713. fuse_lib_flush(fuse_req_t req,
  2714. struct fuse_in_header *hdr_)
  2715. {
  2716. int err;
  2717. struct fuse *f;
  2718. fuse_file_info_t ffi = {0};
  2719. struct fuse_flush_in *arg;
  2720. arg = fuse_hdr_arg(hdr_);
  2721. ffi.fh = arg->fh;
  2722. ffi.flush = 1;
  2723. if(req->f->conn.proto_minor >= 7)
  2724. ffi.lock_owner = arg->lock_owner;
  2725. f = req_fuse_prepare(req);
  2726. err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
  2727. fuse_reply_err(req,err);
  2728. }
  2729. static
  2730. int
  2731. fuse_lock_common(fuse_req_t req,
  2732. uint64_t ino,
  2733. fuse_file_info_t *fi,
  2734. struct flock *lock,
  2735. int cmd)
  2736. {
  2737. int err;
  2738. struct fuse *f = req_fuse_prepare(req);
  2739. err = f->fs->op.lock(fi,cmd,lock);
  2740. return err;
  2741. }
  2742. static
  2743. void
  2744. convert_fuse_file_lock(const struct fuse_file_lock *fl,
  2745. struct flock *flock)
  2746. {
  2747. memset(flock, 0, sizeof(struct flock));
  2748. flock->l_type = fl->type;
  2749. flock->l_whence = SEEK_SET;
  2750. flock->l_start = fl->start;
  2751. if (fl->end == OFFSET_MAX)
  2752. flock->l_len = 0;
  2753. else
  2754. flock->l_len = fl->end - fl->start + 1;
  2755. flock->l_pid = fl->pid;
  2756. }
  2757. static
  2758. void
  2759. fuse_lib_getlk(fuse_req_t req,
  2760. const struct fuse_in_header *hdr_)
  2761. {
  2762. int err;
  2763. struct fuse *f;
  2764. lock_t lk;
  2765. struct flock flk;
  2766. lock_t *conflict;
  2767. fuse_file_info_t ffi = {0};
  2768. const struct fuse_lk_in *arg;
  2769. arg = fuse_hdr_arg(hdr_);
  2770. ffi.fh = arg->fh;
  2771. ffi.lock_owner = arg->owner;
  2772. convert_fuse_file_lock(&arg->lk,&flk);
  2773. f = req_fuse(req);
  2774. flock_to_lock(&flk,&lk);
  2775. lk.owner = ffi.lock_owner;
  2776. pthread_mutex_lock(&f->lock);
  2777. conflict = locks_conflict(get_node(f,hdr_->nodeid),&lk);
  2778. if(conflict)
  2779. lock_to_flock(conflict,&flk);
  2780. pthread_mutex_unlock(&f->lock);
  2781. if(!conflict)
  2782. err = fuse_lock_common(req,hdr_->nodeid,&ffi,&flk,F_GETLK);
  2783. else
  2784. err = 0;
  2785. if(!err)
  2786. fuse_reply_lock(req,&flk);
  2787. else
  2788. fuse_reply_err(req,err);
  2789. }
  2790. static
  2791. void
  2792. fuse_lib_setlk(fuse_req_t req,
  2793. uint64_t ino,
  2794. fuse_file_info_t *fi,
  2795. struct flock *lock,
  2796. int sleep)
  2797. {
  2798. int err = fuse_lock_common(req,ino,fi,lock,
  2799. sleep ? F_SETLKW : F_SETLK);
  2800. if(!err)
  2801. {
  2802. struct fuse *f = req_fuse(req);
  2803. lock_t l;
  2804. flock_to_lock(lock,&l);
  2805. l.owner = fi->lock_owner;
  2806. pthread_mutex_lock(&f->lock);
  2807. locks_insert(get_node(f,ino),&l);
  2808. pthread_mutex_unlock(&f->lock);
  2809. }
  2810. fuse_reply_err(req,err);
  2811. }
  2812. static
  2813. void
  2814. fuse_lib_flock(fuse_req_t req,
  2815. uint64_t ino,
  2816. fuse_file_info_t *fi,
  2817. int op)
  2818. {
  2819. int err;
  2820. struct fuse *f = req_fuse_prepare(req);
  2821. err = f->fs->op.flock(fi,op);
  2822. fuse_reply_err(req,err);
  2823. }
  2824. static
  2825. void
  2826. fuse_lib_bmap(fuse_req_t req,
  2827. const struct fuse_in_header *hdr_)
  2828. {
  2829. int err;
  2830. char *path;
  2831. struct fuse *f;
  2832. uint64_t block;
  2833. const struct fuse_bmap_in *arg;
  2834. arg = fuse_hdr_arg(hdr_);
  2835. block = arg->block;
  2836. f = req_fuse_prepare(req);
  2837. err = get_path(f,hdr_->nodeid,&path);
  2838. if(!err)
  2839. {
  2840. err = f->fs->op.bmap(path,arg->blocksize,&block);
  2841. free_path(f,hdr_->nodeid,path);
  2842. }
  2843. if(!err)
  2844. fuse_reply_bmap(req,block);
  2845. else
  2846. fuse_reply_err(req,err);
  2847. }
  2848. static
  2849. void
  2850. fuse_lib_ioctl(fuse_req_t req,
  2851. const struct fuse_in_header *hdr_)
  2852. {
  2853. int err;
  2854. char *out_buf = NULL;
  2855. struct fuse *f = req_fuse_prepare(req);
  2856. fuse_file_info_t ffi;
  2857. fuse_file_info_t llffi = {0};
  2858. const void *in_buf;
  2859. uint32_t out_size;
  2860. const struct fuse_ioctl_in *arg;
  2861. arg = fuse_hdr_arg(hdr_);
  2862. if((arg->flags & FUSE_IOCTL_DIR) && !(req->f->conn.want & FUSE_CAP_IOCTL_DIR))
  2863. {
  2864. fuse_reply_err(req,ENOTTY);
  2865. return;
  2866. }
  2867. if((sizeof(void*) == 4) &&
  2868. (req->f->conn.proto_minor >= 16) &&
  2869. !(arg->flags & FUSE_IOCTL_32BIT))
  2870. {
  2871. req->ioctl_64bit = 1;
  2872. }
  2873. llffi.fh = arg->fh;
  2874. out_size = arg->out_size;
  2875. in_buf = (arg->in_size ? PARAM(arg) : NULL);
  2876. err = -EPERM;
  2877. if(arg->flags & FUSE_IOCTL_UNRESTRICTED)
  2878. goto err;
  2879. if(arg->flags & FUSE_IOCTL_DIR)
  2880. get_dirhandle(&llffi,&ffi);
  2881. else
  2882. ffi = llffi;
  2883. if(out_size)
  2884. {
  2885. err = -ENOMEM;
  2886. out_buf = malloc(out_size);
  2887. if(!out_buf)
  2888. goto err;
  2889. }
  2890. assert(!arg->in_size || !out_size || arg->in_size == out_size);
  2891. if(out_buf)
  2892. memcpy(out_buf,in_buf,arg->in_size);
  2893. err = f->fs->op.ioctl(&ffi,
  2894. arg->cmd,
  2895. (void*)(uintptr_t)arg->arg,
  2896. arg->flags,
  2897. out_buf ?: (void *)in_buf,
  2898. &out_size);
  2899. if(err < 0)
  2900. goto err;
  2901. fuse_reply_ioctl(req,err,out_buf,out_size);
  2902. goto out;
  2903. err:
  2904. fuse_reply_err(req,err);
  2905. out:
  2906. free(out_buf);
  2907. }
  2908. static
  2909. void
  2910. fuse_lib_poll(fuse_req_t req,
  2911. const struct fuse_in_header *hdr_)
  2912. {
  2913. int err;
  2914. struct fuse *f = req_fuse_prepare(req);
  2915. unsigned revents = 0;
  2916. fuse_file_info_t ffi = {0};
  2917. fuse_pollhandle_t *ph = NULL;
  2918. const struct fuse_poll_in *arg;
  2919. arg = fuse_hdr_arg(hdr_);
  2920. ffi.fh = arg->fh;
  2921. if(arg->flags & FUSE_POLL_SCHEDULE_NOTIFY)
  2922. {
  2923. ph = (fuse_pollhandle_t*)malloc(sizeof(fuse_pollhandle_t));
  2924. if(ph == NULL)
  2925. {
  2926. fuse_reply_err(req,ENOMEM);
  2927. return;
  2928. }
  2929. ph->kh = arg->kh;
  2930. ph->ch = req->ch;
  2931. ph->f = req->f;
  2932. }
  2933. err = f->fs->op.poll(&ffi,ph,&revents);
  2934. if(!err)
  2935. fuse_reply_poll(req,revents);
  2936. else
  2937. fuse_reply_err(req,err);
  2938. }
  2939. static
  2940. void
  2941. fuse_lib_fallocate(fuse_req_t req,
  2942. const struct fuse_in_header *hdr_)
  2943. {
  2944. int err;
  2945. struct fuse *f;
  2946. fuse_file_info_t ffi = {0};
  2947. const struct fuse_fallocate_in *arg;
  2948. arg = fuse_hdr_arg(hdr_);
  2949. ffi.fh = arg->fh;
  2950. f = req_fuse_prepare(req);
  2951. err = f->fs->op.fallocate(&ffi,
  2952. arg->mode,
  2953. arg->offset,
  2954. arg->length);
  2955. fuse_reply_err(req,err);
  2956. }
  2957. static
  2958. int
  2959. remembered_node_cmp(const void *a_,
  2960. const void *b_)
  2961. {
  2962. const remembered_node_t *a = a_;
  2963. const remembered_node_t *b = b_;
  2964. return (a->time - b->time);
  2965. }
  2966. static
  2967. void
  2968. remembered_nodes_sort(struct fuse *f_)
  2969. {
  2970. pthread_mutex_lock(&f_->lock);
  2971. qsort(&kv_first(f_->remembered_nodes),
  2972. kv_size(f_->remembered_nodes),
  2973. sizeof(remembered_node_t),
  2974. remembered_node_cmp);
  2975. pthread_mutex_unlock(&f_->lock);
  2976. }
  2977. #define MAX_PRUNE 100
  2978. #define MAX_CHECK 1000
  2979. int
  2980. fuse_prune_some_remembered_nodes(struct fuse *f_,
  2981. int *offset_)
  2982. {
  2983. time_t now;
  2984. int pruned;
  2985. int checked;
  2986. pthread_mutex_lock(&f_->lock);
  2987. pruned = 0;
  2988. checked = 0;
  2989. now = current_time();
  2990. while(*offset_ < kv_size(f_->remembered_nodes))
  2991. {
  2992. time_t age;
  2993. remembered_node_t *fn = &kv_A(f_->remembered_nodes,*offset_);
  2994. if(pruned >= MAX_PRUNE)
  2995. break;
  2996. if(checked >= MAX_CHECK)
  2997. break;
  2998. checked++;
  2999. age = (now - fn->time);
  3000. if(f_->conf.remember > age)
  3001. break;
  3002. assert(fn->node->nlookup == 1);
  3003. /* Don't forget active directories */
  3004. if(fn->node->refctr > 1)
  3005. {
  3006. (*offset_)++;
  3007. continue;
  3008. }
  3009. fn->node->nlookup = 0;
  3010. unref_node(f_,fn->node);
  3011. kv_delete(f_->remembered_nodes,*offset_);
  3012. pruned++;
  3013. }
  3014. pthread_mutex_unlock(&f_->lock);
  3015. if((pruned < MAX_PRUNE) && (checked < MAX_CHECK))
  3016. *offset_ = -1;
  3017. return pruned;
  3018. }
  3019. #undef MAX_PRUNE
  3020. #undef MAX_CHECK
  3021. static
  3022. void
  3023. sleep_100ms(void)
  3024. {
  3025. const struct timespec ms100 = {0,100 * 1000000};
  3026. nanosleep(&ms100,NULL);
  3027. }
  3028. void
  3029. fuse_prune_remembered_nodes(struct fuse *f_)
  3030. {
  3031. int offset;
  3032. int pruned;
  3033. offset = 0;
  3034. pruned = 0;
  3035. for(;;)
  3036. {
  3037. pruned += fuse_prune_some_remembered_nodes(f_,&offset);
  3038. if(offset >= 0)
  3039. {
  3040. sleep_100ms();
  3041. continue;
  3042. }
  3043. break;
  3044. }
  3045. if(pruned > 0)
  3046. remembered_nodes_sort(f_);
  3047. }
  3048. static struct fuse_lowlevel_ops fuse_path_ops =
  3049. {
  3050. .access = fuse_lib_access,
  3051. .bmap = fuse_lib_bmap,
  3052. .copy_file_range = fuse_lib_copy_file_range,
  3053. .create = fuse_lib_create,
  3054. .destroy = fuse_lib_destroy,
  3055. .fallocate = fuse_lib_fallocate,
  3056. .flock = fuse_lib_flock,
  3057. .flush = fuse_lib_flush,
  3058. .forget = fuse_lib_forget,
  3059. .forget_multi = fuse_lib_forget_multi,
  3060. .fsync = fuse_lib_fsync,
  3061. .fsyncdir = fuse_lib_fsyncdir,
  3062. .getattr = fuse_lib_getattr,
  3063. .getlk = fuse_lib_getlk,
  3064. .getxattr = fuse_lib_getxattr,
  3065. .init = fuse_lib_init,
  3066. .ioctl = fuse_lib_ioctl,
  3067. .link = fuse_lib_link,
  3068. .listxattr = fuse_lib_listxattr,
  3069. .lookup = fuse_lib_lookup,
  3070. .mkdir = fuse_lib_mkdir,
  3071. .mknod = fuse_lib_mknod,
  3072. .open = fuse_lib_open,
  3073. .opendir = fuse_lib_opendir,
  3074. .poll = fuse_lib_poll,
  3075. .read = fuse_lib_read,
  3076. .readdir = fuse_lib_readdir,
  3077. .readdir_plus = fuse_lib_readdir_plus,
  3078. .readlink = fuse_lib_readlink,
  3079. .release = fuse_lib_release,
  3080. .releasedir = fuse_lib_releasedir,
  3081. .removemapping = fuse_lib_removemapping,
  3082. .removexattr = fuse_lib_removexattr,
  3083. .rename = fuse_lib_rename,
  3084. .retrieve_reply = NULL,
  3085. .rmdir = fuse_lib_rmdir,
  3086. .setattr = fuse_lib_setattr,
  3087. .setlk = fuse_lib_setlk,
  3088. .setupmapping = fuse_lib_setupmapping,
  3089. .setxattr = fuse_lib_setxattr,
  3090. .statfs = fuse_lib_statfs,
  3091. .symlink = fuse_lib_symlink,
  3092. .syncfs = fuse_lib_syncfs,
  3093. .tmpfile = fuse_lib_tmpfile,
  3094. .unlink = fuse_lib_unlink,
  3095. .write = fuse_lib_write,
  3096. };
  3097. int
  3098. fuse_notify_poll(fuse_pollhandle_t *ph)
  3099. {
  3100. return fuse_lowlevel_notify_poll(ph);
  3101. }
  3102. int
  3103. fuse_exited(struct fuse *f)
  3104. {
  3105. return fuse_session_exited(f->se);
  3106. }
  3107. struct fuse_session*
  3108. fuse_get_session(struct fuse *f)
  3109. {
  3110. return f->se;
  3111. }
  3112. void
  3113. fuse_exit(struct fuse *f)
  3114. {
  3115. f->se->exited = 1;
  3116. }
  3117. struct fuse_context*
  3118. fuse_get_context(void)
  3119. {
  3120. return &fuse_get_context_internal()->ctx;
  3121. }
  3122. int
  3123. fuse_get_dev_fuse_fd(const struct fuse_context *fc_)
  3124. {
  3125. return fuse_chan_fd(fc_->fuse->se->ch);
  3126. }
  3127. enum {
  3128. KEY_HELP,
  3129. };
  3130. #define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
  3131. static const struct fuse_opt fuse_lib_opts[] =
  3132. {
  3133. FUSE_OPT_KEY("-h", KEY_HELP),
  3134. FUSE_OPT_KEY("--help", KEY_HELP),
  3135. FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
  3136. FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
  3137. FUSE_LIB_OPT("debug", debug,1),
  3138. FUSE_LIB_OPT("-d", debug,1),
  3139. FUSE_LIB_OPT("nogc", nogc,1),
  3140. FUSE_LIB_OPT("umask=", set_mode,1),
  3141. FUSE_LIB_OPT("umask=%o", umask,0),
  3142. FUSE_LIB_OPT("uid=", set_uid,1),
  3143. FUSE_LIB_OPT("uid=%d", uid,0),
  3144. FUSE_LIB_OPT("gid=", set_gid,1),
  3145. FUSE_LIB_OPT("gid=%d", gid,0),
  3146. FUSE_LIB_OPT("noforget", remember,-1),
  3147. FUSE_LIB_OPT("remember=%u", remember,0),
  3148. FUSE_OPT_END
  3149. };
  3150. static void fuse_lib_help(void)
  3151. {
  3152. fprintf(stderr,
  3153. " -o umask=M set file permissions (octal)\n"
  3154. " -o uid=N set file owner\n"
  3155. " -o gid=N set file group\n"
  3156. " -o noforget never forget cached inodes\n"
  3157. " -o remember=T remember cached inodes for T seconds (0s)\n"
  3158. " -o threads=NUM number of worker threads. 0 = autodetect.\n"
  3159. " Negative values autodetect then divide by\n"
  3160. " absolute value. default = 0\n"
  3161. "\n");
  3162. }
  3163. static
  3164. int
  3165. fuse_lib_opt_proc(void *data,
  3166. const char *arg,
  3167. int key,
  3168. struct fuse_args *outargs)
  3169. {
  3170. (void)arg; (void)outargs;
  3171. if(key == KEY_HELP)
  3172. {
  3173. struct fuse_config *conf = (struct fuse_config *)data;
  3174. fuse_lib_help();
  3175. conf->help = 1;
  3176. }
  3177. return 1;
  3178. }
  3179. int
  3180. fuse_is_lib_option(const char *opt)
  3181. {
  3182. return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
  3183. }
  3184. struct fuse_fs*
  3185. fuse_fs_new(const struct fuse_operations *op,
  3186. size_t op_size)
  3187. {
  3188. struct fuse_fs *fs;
  3189. if(sizeof(struct fuse_operations) < op_size)
  3190. {
  3191. fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
  3192. op_size = sizeof(struct fuse_operations);
  3193. }
  3194. fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
  3195. if(!fs)
  3196. {
  3197. fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
  3198. return NULL;
  3199. }
  3200. if(op)
  3201. memcpy(&fs->op,op,op_size);
  3202. return fs;
  3203. }
  3204. static
  3205. int
  3206. node_table_init(struct node_table *t)
  3207. {
  3208. t->size = NODE_TABLE_MIN_SIZE;
  3209. t->array = (node_t **)calloc(1,sizeof(node_t *) * t->size);
  3210. if(t->array == NULL)
  3211. {
  3212. fprintf(stderr,"fuse: memory allocation failed\n");
  3213. return -1;
  3214. }
  3215. t->use = 0;
  3216. t->split = 0;
  3217. return 0;
  3218. }
  3219. static
  3220. struct fuse*
  3221. fuse_get_fuse_obj()
  3222. {
  3223. static struct fuse f = {0};
  3224. return &f;
  3225. }
  3226. static
  3227. void
  3228. metrics_log_nodes_info(struct fuse *f_,
  3229. FILE *file_)
  3230. {
  3231. char buf[1024];
  3232. char time_str[64];
  3233. struct tm tm;
  3234. struct timeval tv;
  3235. uint64_t sizeof_node;
  3236. float node_usage_ratio;
  3237. uint64_t node_slab_count;
  3238. uint64_t node_avail_objs;
  3239. uint64_t node_total_alloc_mem;
  3240. gettimeofday(&tv,NULL);
  3241. localtime_r(&tv.tv_sec,&tm);
  3242. strftime(time_str,sizeof(time_str),"%Y-%m-%dT%H:%M:%S.000%z",&tm);
  3243. sizeof_node = sizeof(node_t);
  3244. lfmp_t *lfmp;
  3245. lfmp = node_lfmp();
  3246. lfmp_lock(lfmp);
  3247. node_slab_count = fmp_slab_count(&lfmp->fmp);
  3248. node_usage_ratio = fmp_slab_usage_ratio(&lfmp->fmp);
  3249. node_avail_objs = fmp_avail_objs(&lfmp->fmp);
  3250. node_total_alloc_mem = fmp_total_allocated_memory(&lfmp->fmp);
  3251. lfmp_unlock(lfmp);
  3252. snprintf(buf,sizeof(buf),
  3253. "time: %s\n"
  3254. "sizeof(node): %"PRIu64"\n"
  3255. "node id_table size: %"PRIu64"\n"
  3256. "node id_table usage: %"PRIu64"\n"
  3257. "node id_table total allocated memory: %"PRIu64"\n"
  3258. "node name_table size: %"PRIu64"\n"
  3259. "node name_table usage: %"PRIu64"\n"
  3260. "node name_table total allocated memory: %"PRIu64"\n"
  3261. "node memory pool slab count: %"PRIu64"\n"
  3262. "node memory pool usage ratio: %f\n"
  3263. "node memory pool avail objs: %"PRIu64"\n"
  3264. "node memory pool total allocated memory: %"PRIu64"\n"
  3265. "msgbuf bufsize: %"PRIu64"\n"
  3266. "msgbuf allocation count: %"PRIu64"\n"
  3267. "msgbuf available count: %"PRIu64"\n"
  3268. "msgbuf total allocated memory: %"PRIu64"\n"
  3269. "\n"
  3270. ,
  3271. time_str,
  3272. sizeof_node,
  3273. (uint64_t)f_->id_table.size,
  3274. (uint64_t)f_->id_table.use,
  3275. (uint64_t)(f_->id_table.size * sizeof(node_t*)),
  3276. (uint64_t)f_->name_table.size,
  3277. (uint64_t)f_->name_table.use,
  3278. (uint64_t)(f_->name_table.size * sizeof(node_t*)),
  3279. node_slab_count,
  3280. node_usage_ratio,
  3281. node_avail_objs,
  3282. node_total_alloc_mem,
  3283. msgbuf_get_bufsize(),
  3284. msgbuf_alloc_count(),
  3285. msgbuf_avail_count(),
  3286. msgbuf_alloc_count() * msgbuf_get_bufsize()
  3287. );
  3288. fputs(buf,file_);
  3289. }
  3290. static
  3291. void
  3292. metrics_log_nodes_info_to_tmp_dir(struct fuse *f_)
  3293. {
  3294. int rv;
  3295. FILE *file;
  3296. char filepath[256];
  3297. struct stat st;
  3298. char const *mode = "a";
  3299. off_t const max_size = (1024 * 1024);
  3300. sprintf(filepath,"/tmp/mergerfs.%d.info",getpid());
  3301. rv = lstat(filepath,&st);
  3302. if((rv == 0) && (st.st_size > max_size))
  3303. mode = "w";
  3304. file = fopen(filepath,mode);
  3305. if(file == NULL)
  3306. return;
  3307. metrics_log_nodes_info(f_,file);
  3308. fclose(file);
  3309. }
  3310. static
  3311. void
  3312. fuse_malloc_trim(void)
  3313. {
  3314. #ifdef HAVE_MALLOC_TRIM
  3315. malloc_trim(1024 * 1024);
  3316. #endif
  3317. }
  3318. void
  3319. fuse_invalidate_all_nodes()
  3320. {
  3321. struct fuse *f = fuse_get_fuse_obj();
  3322. syslog(LOG_INFO,"invalidating file entries");
  3323. pthread_mutex_lock(&f->lock);
  3324. for(int i = 0; i < f->id_table.size; i++)
  3325. {
  3326. node_t *node;
  3327. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  3328. {
  3329. if(node->nodeid == FUSE_ROOT_ID)
  3330. continue;
  3331. if(node->parent->nodeid != FUSE_ROOT_ID)
  3332. continue;
  3333. fuse_lowlevel_notify_inval_entry(f->se->ch,
  3334. node->parent->nodeid,
  3335. node->name,
  3336. strlen(node->name));
  3337. }
  3338. }
  3339. pthread_mutex_unlock(&f->lock);
  3340. }
  3341. void
  3342. fuse_gc()
  3343. {
  3344. syslog(LOG_INFO,"running thorough garbage collection");
  3345. node_gc();
  3346. msgbuf_gc();
  3347. fuse_malloc_trim();
  3348. }
  3349. void
  3350. fuse_gc1()
  3351. {
  3352. syslog(LOG_INFO,"running basic garbage collection");
  3353. node_gc1();
  3354. msgbuf_gc_10percent();
  3355. fuse_malloc_trim();
  3356. }
  3357. static
  3358. void*
  3359. fuse_maintenance_loop(void *fuse_)
  3360. {
  3361. int loops;
  3362. int sleep_time;
  3363. struct fuse *f = (struct fuse*)fuse_;
  3364. pthread_setname_np(pthread_self(),"fuse.maint");
  3365. loops = 0;
  3366. sleep_time = 60;
  3367. while(1)
  3368. {
  3369. if(remember_nodes(f))
  3370. fuse_prune_remembered_nodes(f);
  3371. if((loops % 15) == 0)
  3372. fuse_gc1();
  3373. if(g_LOG_METRICS)
  3374. metrics_log_nodes_info_to_tmp_dir(f);
  3375. loops++;
  3376. sleep(sleep_time);
  3377. }
  3378. return NULL;
  3379. }
  3380. int
  3381. fuse_start_maintenance_thread(struct fuse *f_)
  3382. {
  3383. return fuse_start_thread(&f_->maintenance_thread,fuse_maintenance_loop,f_);
  3384. }
  3385. void
  3386. fuse_stop_maintenance_thread(struct fuse *f_)
  3387. {
  3388. pthread_mutex_lock(&f_->lock);
  3389. pthread_cancel(f_->maintenance_thread);
  3390. pthread_mutex_unlock(&f_->lock);
  3391. pthread_join(f_->maintenance_thread,NULL);
  3392. }
  3393. struct fuse*
  3394. fuse_new_common(struct fuse_chan *ch,
  3395. struct fuse_args *args,
  3396. const struct fuse_operations *op,
  3397. size_t op_size)
  3398. {
  3399. struct fuse *f;
  3400. node_t *root;
  3401. struct fuse_fs *fs;
  3402. struct fuse_lowlevel_ops llop = fuse_path_ops;
  3403. if(fuse_create_context_key() == -1)
  3404. goto out;
  3405. f = fuse_get_fuse_obj();
  3406. if(f == NULL)
  3407. {
  3408. fprintf(stderr,"fuse: failed to allocate fuse object\n");
  3409. goto out_delete_context_key;
  3410. }
  3411. fs = fuse_fs_new(op,op_size);
  3412. if(!fs)
  3413. goto out_free;
  3414. f->fs = fs;
  3415. /* Oh f**k,this is ugly! */
  3416. if(!fs->op.lock)
  3417. {
  3418. llop.getlk = NULL;
  3419. llop.setlk = NULL;
  3420. }
  3421. if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
  3422. goto out_free_fs;
  3423. g_LOG_METRICS = f->conf.debug;
  3424. f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
  3425. if(f->se == NULL)
  3426. goto out_free_fs;
  3427. fuse_session_add_chan(f->se,ch);
  3428. /* Trace topmost layer by default */
  3429. srand(time(NULL));
  3430. f->nodeid_gen.nodeid = FUSE_ROOT_ID;
  3431. f->nodeid_gen.generation = rand64();
  3432. if(node_table_init(&f->name_table) == -1)
  3433. goto out_free_session;
  3434. if(node_table_init(&f->id_table) == -1)
  3435. goto out_free_name_table;
  3436. fuse_mutex_init(&f->lock);
  3437. kv_init(f->remembered_nodes);
  3438. root = node_alloc();
  3439. if(root == NULL)
  3440. {
  3441. fprintf(stderr,"fuse: memory allocation failed\n");
  3442. goto out_free_id_table;
  3443. }
  3444. root->name = filename_strdup(f,"/");
  3445. root->parent = NULL;
  3446. root->nodeid = FUSE_ROOT_ID;
  3447. inc_nlookup(root);
  3448. hash_id(f,root);
  3449. return f;
  3450. out_free_id_table:
  3451. free(f->id_table.array);
  3452. out_free_name_table:
  3453. free(f->name_table.array);
  3454. out_free_session:
  3455. fuse_session_destroy(f->se);
  3456. out_free_fs:
  3457. /* Horrible compatibility hack to stop the destructor from being
  3458. called on the filesystem without init being called first */
  3459. fs->op.destroy = NULL;
  3460. free(f->fs);
  3461. out_free:
  3462. // free(f);
  3463. out_delete_context_key:
  3464. fuse_delete_context_key();
  3465. out:
  3466. return NULL;
  3467. }
  3468. struct fuse*
  3469. fuse_new(struct fuse_chan *ch,
  3470. struct fuse_args *args,
  3471. const struct fuse_operations *op,
  3472. size_t op_size)
  3473. {
  3474. return fuse_new_common(ch,args,op,op_size);
  3475. }
  3476. void
  3477. fuse_destroy(struct fuse *f)
  3478. {
  3479. size_t i;
  3480. if(f->fs)
  3481. {
  3482. struct fuse_context_i *c = fuse_get_context_internal();
  3483. memset(c,0,sizeof(*c));
  3484. c->ctx.fuse = f;
  3485. for(i = 0; i < f->id_table.size; i++)
  3486. {
  3487. node_t *node;
  3488. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  3489. {
  3490. if(!node->hidden_fh)
  3491. continue;
  3492. f->fs->op.free_hide(node->hidden_fh);
  3493. node->hidden_fh = 0;
  3494. }
  3495. }
  3496. }
  3497. for(i = 0; i < f->id_table.size; i++)
  3498. {
  3499. node_t *node;
  3500. node_t *next;
  3501. for(node = f->id_table.array[i]; node != NULL; node = next)
  3502. {
  3503. next = node->id_next;
  3504. free_node(f,node);
  3505. f->id_table.use--;
  3506. }
  3507. }
  3508. free(f->id_table.array);
  3509. free(f->name_table.array);
  3510. pthread_mutex_destroy(&f->lock);
  3511. fuse_session_destroy(f->se);
  3512. kv_destroy(f->remembered_nodes);
  3513. fuse_delete_context_key();
  3514. }
  3515. void
  3516. fuse_log_metrics_set(int log_)
  3517. {
  3518. g_LOG_METRICS = log_;
  3519. }
  3520. int
  3521. fuse_log_metrics_get(void)
  3522. {
  3523. return g_LOG_METRICS;
  3524. }
  3525. int
  3526. fuse_passthrough_open(const struct fuse_context *fc_,
  3527. const int fd_)
  3528. {
  3529. int rv;
  3530. int dev_fuse_fd;
  3531. struct fuse_backing_map bm = {0};
  3532. dev_fuse_fd = fuse_get_dev_fuse_fd(fc_);
  3533. bm.fd = fd_;
  3534. rv = ioctl(dev_fuse_fd,FUSE_DEV_IOC_BACKING_OPEN,&bm);
  3535. return rv;
  3536. }
  3537. int
  3538. fuse_passthrough_close(const struct fuse_context *fc_,
  3539. const int backing_id_)
  3540. {
  3541. int dev_fuse_fd;
  3542. dev_fuse_fd = fuse_get_dev_fuse_fd(fc_);
  3543. return ioctl(dev_fuse_fd, FUSE_DEV_IOC_BACKING_CLOSE, &backing_id_);
  3544. }