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.

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