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.

4288 lines
86 KiB

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