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.

4291 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 = -ENOENT;
  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. struct fuse *f = req_fuse_prepare(req);
  1746. struct fuse_entry_param e;
  1747. char *path;
  1748. int err;
  1749. struct node *dot = NULL;
  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. name = NULL;
  1769. pthread_mutex_lock(&f->lock);
  1770. parent = get_node(f,parent)->parent->nodeid;
  1771. pthread_mutex_unlock(&f->lock);
  1772. }
  1773. }
  1774. err = get_path_name(f,parent,name,&path);
  1775. if(!err)
  1776. {
  1777. err = lookup_path(f,parent,name,path,&e,NULL);
  1778. if(err == -ENOENT)
  1779. {
  1780. e.ino = 0;
  1781. err = 0;
  1782. }
  1783. free_path(f,parent,path);
  1784. }
  1785. if(dot)
  1786. {
  1787. pthread_mutex_lock(&f->lock);
  1788. unref_node(f,dot);
  1789. pthread_mutex_unlock(&f->lock);
  1790. }
  1791. reply_entry(req,&e,err);
  1792. }
  1793. static
  1794. void
  1795. do_forget(struct fuse *f,
  1796. const uint64_t ino,
  1797. const uint64_t nlookup)
  1798. {
  1799. forget_node(f,ino,nlookup);
  1800. }
  1801. static
  1802. void
  1803. fuse_lib_forget(fuse_req_t req,
  1804. const uint64_t ino,
  1805. const uint64_t nlookup)
  1806. {
  1807. do_forget(req_fuse(req),ino,nlookup);
  1808. fuse_reply_none(req);
  1809. }
  1810. static
  1811. void
  1812. fuse_lib_forget_multi(fuse_req_t req,
  1813. size_t count,
  1814. struct fuse_forget_data *forgets)
  1815. {
  1816. struct fuse *f = req_fuse(req);
  1817. size_t i;
  1818. for(i = 0; i < count; i++)
  1819. do_forget(f,forgets[i].ino,forgets[i].nlookup);
  1820. fuse_reply_none(req);
  1821. }
  1822. static
  1823. void
  1824. fuse_lib_getattr(fuse_req_t req,
  1825. uint64_t ino,
  1826. fuse_file_info_t *fi)
  1827. {
  1828. int err;
  1829. char *path;
  1830. struct fuse *f;
  1831. struct stat buf;
  1832. struct node *node;
  1833. fuse_timeouts_t timeout;
  1834. fuse_file_info_t ffi = {0};
  1835. f = req_fuse_prepare(req);
  1836. if(fi == NULL)
  1837. {
  1838. pthread_mutex_lock(&f->lock);
  1839. node = get_node(f,ino);
  1840. if(node->is_hidden)
  1841. {
  1842. fi = &ffi;
  1843. fi->fh = node->hidden_fh;
  1844. }
  1845. pthread_mutex_unlock(&f->lock);
  1846. }
  1847. memset(&buf,0,sizeof(buf));
  1848. err = 0;
  1849. path = NULL;
  1850. if(fi == NULL)
  1851. err = get_path(f,ino,&path);
  1852. if(!err)
  1853. {
  1854. err = ((fi == NULL) ?
  1855. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  1856. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  1857. free_path(f,ino,path);
  1858. }
  1859. if(!err)
  1860. {
  1861. pthread_mutex_lock(&f->lock);
  1862. node = get_node(f,ino);
  1863. update_stat(node,&buf);
  1864. pthread_mutex_unlock(&f->lock);
  1865. set_stat(f,ino,&buf);
  1866. fuse_reply_attr(req,&buf,timeout.attr);
  1867. }
  1868. else
  1869. {
  1870. reply_err(req,err);
  1871. }
  1872. }
  1873. int
  1874. fuse_fs_chmod(struct fuse_fs *fs,
  1875. const char *path,
  1876. mode_t mode)
  1877. {
  1878. return fs->op.chmod(path,mode);
  1879. }
  1880. int
  1881. fuse_fs_fchmod(struct fuse_fs *fs_,
  1882. const fuse_file_info_t *ffi_,
  1883. const mode_t mode_)
  1884. {
  1885. return fs_->op.fchmod(ffi_,mode_);
  1886. }
  1887. static
  1888. void
  1889. fuse_lib_setattr(fuse_req_t req,
  1890. uint64_t ino,
  1891. struct stat *attr,
  1892. int valid,
  1893. fuse_file_info_t *fi)
  1894. {
  1895. struct fuse *f = req_fuse_prepare(req);
  1896. struct stat buf;
  1897. char *path;
  1898. int err;
  1899. struct node *node;
  1900. fuse_timeouts_t timeout;
  1901. fuse_file_info_t ffi = {0};
  1902. if(fi == NULL)
  1903. {
  1904. pthread_mutex_lock(&f->lock);
  1905. node = get_node(f,ino);
  1906. if(node->is_hidden)
  1907. {
  1908. fi = &ffi;
  1909. fi->fh = node->hidden_fh;
  1910. }
  1911. pthread_mutex_unlock(&f->lock);
  1912. }
  1913. memset(&buf,0,sizeof(buf));
  1914. err = 0;
  1915. path = NULL;
  1916. if(fi == NULL)
  1917. err = get_path(f,ino,&path);
  1918. if(!err)
  1919. {
  1920. err = 0;
  1921. if(!err && (valid & FATTR_MODE))
  1922. err = ((fi == NULL) ?
  1923. fuse_fs_chmod(f->fs,path,attr->st_mode) :
  1924. fuse_fs_fchmod(f->fs,fi,attr->st_mode));
  1925. if(!err && (valid & (FATTR_UID | FATTR_GID)))
  1926. {
  1927. uid_t uid = ((valid & FATTR_UID) ? attr->st_uid : (uid_t)-1);
  1928. gid_t gid = ((valid & FATTR_GID) ? attr->st_gid : (gid_t)-1);
  1929. err = ((fi == NULL) ?
  1930. fuse_fs_chown(f->fs,path,uid,gid) :
  1931. fuse_fs_fchown(f->fs,fi,uid,gid));
  1932. }
  1933. if(!err && (valid & FATTR_SIZE))
  1934. err = ((fi == NULL) ?
  1935. fuse_fs_truncate(f->fs,path,attr->st_size) :
  1936. fuse_fs_ftruncate(f->fs,attr->st_size,fi));
  1937. #ifdef HAVE_UTIMENSAT
  1938. if(!err && (valid & (FATTR_ATIME | FATTR_MTIME)))
  1939. {
  1940. struct timespec tv[2];
  1941. tv[0].tv_sec = 0;
  1942. tv[1].tv_sec = 0;
  1943. tv[0].tv_nsec = UTIME_OMIT;
  1944. tv[1].tv_nsec = UTIME_OMIT;
  1945. if(valid & FATTR_ATIME_NOW)
  1946. tv[0].tv_nsec = UTIME_NOW;
  1947. else if(valid & FATTR_ATIME)
  1948. tv[0] = attr->st_atim;
  1949. if(valid & FATTR_MTIME_NOW)
  1950. tv[1].tv_nsec = UTIME_NOW;
  1951. else if(valid & FATTR_MTIME)
  1952. tv[1] = attr->st_mtim;
  1953. err = ((fi == NULL) ?
  1954. fuse_fs_utimens(f->fs,path,tv) :
  1955. fuse_fs_futimens(f->fs,fi,tv));
  1956. }
  1957. else
  1958. #endif
  1959. if(!err && ((valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
  1960. {
  1961. struct timespec tv[2];
  1962. tv[0].tv_sec = attr->st_atime;
  1963. tv[0].tv_nsec = ST_ATIM_NSEC(attr);
  1964. tv[1].tv_sec = attr->st_mtime;
  1965. tv[1].tv_nsec = ST_MTIM_NSEC(attr);
  1966. err = ((fi == NULL) ?
  1967. fuse_fs_utimens(f->fs,path,tv) :
  1968. fuse_fs_futimens(f->fs,fi,tv));
  1969. }
  1970. if(!err)
  1971. err = ((fi == NULL) ?
  1972. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  1973. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  1974. free_path(f,ino,path);
  1975. }
  1976. if(!err)
  1977. {
  1978. pthread_mutex_lock(&f->lock);
  1979. update_stat(get_node(f,ino),&buf);
  1980. pthread_mutex_unlock(&f->lock);
  1981. set_stat(f,ino,&buf);
  1982. fuse_reply_attr(req,&buf,timeout.attr);
  1983. }
  1984. else
  1985. {
  1986. reply_err(req,err);
  1987. }
  1988. }
  1989. static
  1990. void
  1991. fuse_lib_access(fuse_req_t req,
  1992. uint64_t ino,
  1993. int mask)
  1994. {
  1995. struct fuse *f = req_fuse_prepare(req);
  1996. char *path;
  1997. int err;
  1998. err = get_path(f,ino,&path);
  1999. if(!err)
  2000. {
  2001. err = fuse_fs_access(f->fs,path,mask);
  2002. free_path(f,ino,path);
  2003. }
  2004. reply_err(req,err);
  2005. }
  2006. static
  2007. void
  2008. fuse_lib_readlink(fuse_req_t req,
  2009. uint64_t ino)
  2010. {
  2011. struct fuse *f = req_fuse_prepare(req);
  2012. char linkname[PATH_MAX + 1];
  2013. char *path;
  2014. int err;
  2015. err = get_path(f,ino,&path);
  2016. if(!err)
  2017. {
  2018. err = fuse_fs_readlink(f->fs,path,linkname,sizeof(linkname));
  2019. free_path(f,ino,path);
  2020. }
  2021. if(!err)
  2022. {
  2023. linkname[PATH_MAX] = '\0';
  2024. fuse_reply_readlink(req,linkname);
  2025. }
  2026. else
  2027. {
  2028. reply_err(req,err);
  2029. }
  2030. }
  2031. static
  2032. void
  2033. fuse_lib_mknod(fuse_req_t req,
  2034. uint64_t parent,
  2035. const char *name,
  2036. mode_t mode,
  2037. dev_t rdev)
  2038. {
  2039. struct fuse *f = req_fuse_prepare(req);
  2040. struct fuse_entry_param e;
  2041. char *path;
  2042. int err;
  2043. err = get_path_name(f,parent,name,&path);
  2044. if(!err)
  2045. {
  2046. err = -ENOSYS;
  2047. if(S_ISREG(mode))
  2048. {
  2049. fuse_file_info_t fi;
  2050. memset(&fi,0,sizeof(fi));
  2051. fi.flags = O_CREAT | O_EXCL | O_WRONLY;
  2052. err = fuse_fs_create(f->fs,path,mode,&fi);
  2053. if(!err)
  2054. {
  2055. err = lookup_path(f,parent,name,path,&e,
  2056. &fi);
  2057. fuse_fs_release(f->fs,&fi);
  2058. }
  2059. }
  2060. if(err == -ENOSYS)
  2061. {
  2062. err = fuse_fs_mknod(f->fs,path,mode,rdev);
  2063. if(!err)
  2064. err = lookup_path(f,parent,name,path,&e,NULL);
  2065. }
  2066. free_path(f,parent,path);
  2067. }
  2068. reply_entry(req,&e,err);
  2069. }
  2070. static
  2071. void
  2072. fuse_lib_mkdir(fuse_req_t req,
  2073. uint64_t parent,
  2074. const char *name,
  2075. mode_t mode)
  2076. {
  2077. struct fuse *f = req_fuse_prepare(req);
  2078. struct fuse_entry_param e;
  2079. char *path;
  2080. int err;
  2081. err = get_path_name(f,parent,name,&path);
  2082. if(!err)
  2083. {
  2084. err = fuse_fs_mkdir(f->fs,path,mode);
  2085. if(!err)
  2086. err = lookup_path(f,parent,name,path,&e,NULL);
  2087. free_path(f,parent,path);
  2088. }
  2089. reply_entry(req,&e,err);
  2090. }
  2091. static
  2092. void
  2093. fuse_lib_unlink(fuse_req_t req,
  2094. uint64_t parent,
  2095. const char *name)
  2096. {
  2097. int err;
  2098. char *path;
  2099. struct fuse *f;
  2100. struct node *wnode;
  2101. f = req_fuse_prepare(req);
  2102. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2103. if(!err)
  2104. {
  2105. pthread_mutex_lock(&f->lock);
  2106. if(node_open(wnode))
  2107. {
  2108. err = fuse_fs_prepare_hide(f->fs,path,&wnode->hidden_fh);
  2109. if(!err)
  2110. wnode->is_hidden = 1;
  2111. }
  2112. pthread_mutex_unlock(&f->lock);
  2113. err = fuse_fs_unlink(f->fs,path);
  2114. if(!err)
  2115. remove_node(f,parent,name);
  2116. free_path_wrlock(f,parent,wnode,path);
  2117. }
  2118. reply_err(req,err);
  2119. }
  2120. static
  2121. void
  2122. fuse_lib_rmdir(fuse_req_t req,
  2123. uint64_t parent,
  2124. const char *name)
  2125. {
  2126. struct fuse *f = req_fuse_prepare(req);
  2127. struct node *wnode;
  2128. char *path;
  2129. int err;
  2130. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2131. if(!err)
  2132. {
  2133. err = fuse_fs_rmdir(f->fs,path);
  2134. if(!err)
  2135. remove_node(f,parent,name);
  2136. free_path_wrlock(f,parent,wnode,path);
  2137. }
  2138. reply_err(req,err);
  2139. }
  2140. static
  2141. void
  2142. fuse_lib_symlink(fuse_req_t req_,
  2143. const char *linkname_,
  2144. uint64_t parent_,
  2145. const char *name_)
  2146. {
  2147. int rv;
  2148. char *path;
  2149. struct fuse *f;
  2150. struct fuse_entry_param e = {0};
  2151. f = req_fuse_prepare(req_);
  2152. rv = get_path_name(f,parent_,name_,&path);
  2153. if(rv == 0)
  2154. {
  2155. rv = fuse_fs_symlink(f->fs,linkname_,path,&e.attr,&e.timeout);
  2156. if(rv == 0)
  2157. rv = set_path_info(f,parent_,name_,&e);
  2158. free_path(f,parent_,path);
  2159. }
  2160. reply_entry(req_,&e,rv);
  2161. }
  2162. static
  2163. void
  2164. fuse_lib_rename(fuse_req_t req,
  2165. uint64_t olddir,
  2166. const char *oldname,
  2167. uint64_t newdir,
  2168. const char *newname)
  2169. {
  2170. int err;
  2171. struct fuse *f;
  2172. char *oldpath;
  2173. char *newpath;
  2174. struct node *wnode1;
  2175. struct node *wnode2;
  2176. f = req_fuse_prepare(req);
  2177. err = get_path2(f,olddir,oldname,newdir,newname,
  2178. &oldpath,&newpath,&wnode1,&wnode2);
  2179. if(!err)
  2180. {
  2181. pthread_mutex_lock(&f->lock);
  2182. if(node_open(wnode2))
  2183. {
  2184. err = fuse_fs_prepare_hide(f->fs,newpath,&wnode2->hidden_fh);
  2185. if(!err)
  2186. wnode2->is_hidden = 1;
  2187. }
  2188. pthread_mutex_unlock(&f->lock);
  2189. err = fuse_fs_rename(f->fs,oldpath,newpath);
  2190. if(!err)
  2191. err = rename_node(f,olddir,oldname,newdir,newname);
  2192. free_path2(f,olddir,newdir,wnode1,wnode2,oldpath,newpath);
  2193. }
  2194. reply_err(req,err);
  2195. }
  2196. static
  2197. void
  2198. fuse_lib_link(fuse_req_t req,
  2199. uint64_t ino,
  2200. uint64_t newparent,
  2201. const char *newname)
  2202. {
  2203. int rv;
  2204. char *oldpath;
  2205. char *newpath;
  2206. struct fuse *f;
  2207. struct fuse_entry_param e = {0};
  2208. f = req_fuse_prepare(req);
  2209. rv = get_path2(f,ino,NULL,newparent,newname,
  2210. &oldpath,&newpath,NULL,NULL);
  2211. if(!rv)
  2212. {
  2213. rv = fuse_fs_link(f->fs,oldpath,newpath,&e.attr,&e.timeout);
  2214. if(rv == 0)
  2215. rv = set_path_info(f,newparent,newname,&e);
  2216. free_path2(f,ino,newparent,NULL,NULL,oldpath,newpath);
  2217. }
  2218. reply_entry(req,&e,rv);
  2219. }
  2220. static
  2221. void
  2222. fuse_do_release(struct fuse *f,
  2223. uint64_t ino,
  2224. fuse_file_info_t *fi)
  2225. {
  2226. struct node *node;
  2227. uint64_t fh;
  2228. int was_hidden;
  2229. fh = 0;
  2230. fuse_fs_release(f->fs,fi);
  2231. pthread_mutex_lock(&f->lock);
  2232. node = get_node(f,ino);
  2233. assert(node->open_count > 0);
  2234. node->open_count--;
  2235. was_hidden = 0;
  2236. if(node->is_hidden && (node->open_count == 0))
  2237. {
  2238. was_hidden = 1;
  2239. node->is_hidden = 0;
  2240. fh = node->hidden_fh;
  2241. }
  2242. pthread_mutex_unlock(&f->lock);
  2243. if(was_hidden)
  2244. fuse_fs_free_hide(f->fs,fh);
  2245. }
  2246. static
  2247. void
  2248. fuse_lib_create(fuse_req_t req,
  2249. uint64_t parent,
  2250. const char *name,
  2251. mode_t mode,
  2252. fuse_file_info_t *fi)
  2253. {
  2254. int err;
  2255. char *path;
  2256. struct fuse *f;
  2257. struct fuse_entry_param e;
  2258. f = req_fuse_prepare(req);
  2259. err = get_path_name(f,parent,name,&path);
  2260. if(!err)
  2261. {
  2262. err = fuse_fs_create(f->fs,path,mode,fi);
  2263. if(!err)
  2264. {
  2265. err = lookup_path(f,parent,name,path,&e,fi);
  2266. if(err)
  2267. {
  2268. fuse_fs_release(f->fs,fi);
  2269. }
  2270. else if(!S_ISREG(e.attr.st_mode))
  2271. {
  2272. err = -EIO;
  2273. fuse_fs_release(f->fs,fi);
  2274. forget_node(f,e.ino,1);
  2275. }
  2276. }
  2277. }
  2278. if(!err)
  2279. {
  2280. pthread_mutex_lock(&f->lock);
  2281. get_node(f,e.ino)->open_count++;
  2282. pthread_mutex_unlock(&f->lock);
  2283. if(fuse_reply_create(req,&e,fi) == -ENOENT)
  2284. {
  2285. /* The open syscall was interrupted,so it
  2286. must be cancelled */
  2287. fuse_do_release(f,e.ino,fi);
  2288. forget_node(f,e.ino,1);
  2289. }
  2290. }
  2291. else
  2292. {
  2293. reply_err(req,err);
  2294. }
  2295. free_path(f,parent,path);
  2296. }
  2297. static
  2298. void
  2299. open_auto_cache(struct fuse *f,
  2300. uint64_t ino,
  2301. const char *path,
  2302. fuse_file_info_t *fi)
  2303. {
  2304. struct node *node;
  2305. fuse_timeouts_t timeout;
  2306. pthread_mutex_lock(&f->lock);
  2307. node = get_node(f,ino);
  2308. if(node->is_stat_cache_valid)
  2309. {
  2310. int err;
  2311. struct stat stbuf;
  2312. pthread_mutex_unlock(&f->lock);
  2313. err = fuse_fs_fgetattr(f->fs,&stbuf,fi,&timeout);
  2314. pthread_mutex_lock(&f->lock);
  2315. if(!err)
  2316. update_stat(node,&stbuf);
  2317. else
  2318. node->is_stat_cache_valid = 0;
  2319. }
  2320. if(node->is_stat_cache_valid)
  2321. fi->keep_cache = 1;
  2322. node->is_stat_cache_valid = 1;
  2323. pthread_mutex_unlock(&f->lock);
  2324. }
  2325. static
  2326. void
  2327. fuse_lib_open(fuse_req_t req,
  2328. uint64_t ino,
  2329. fuse_file_info_t *fi)
  2330. {
  2331. int err;
  2332. char *path;
  2333. struct fuse *f;
  2334. f = req_fuse_prepare(req);
  2335. err = get_path(f,ino,&path);
  2336. if(!err)
  2337. {
  2338. err = fuse_fs_open(f->fs,path,fi);
  2339. if(!err)
  2340. {
  2341. if(fi && fi->auto_cache)
  2342. open_auto_cache(f,ino,path,fi);
  2343. }
  2344. }
  2345. if(!err)
  2346. {
  2347. pthread_mutex_lock(&f->lock);
  2348. get_node(f,ino)->open_count++;
  2349. pthread_mutex_unlock(&f->lock);
  2350. /* The open syscall was interrupted,so it must be cancelled */
  2351. if(fuse_reply_open(req,fi) == -ENOENT)
  2352. fuse_do_release(f,ino,fi);
  2353. }
  2354. else
  2355. {
  2356. reply_err(req,err);
  2357. }
  2358. free_path(f,ino,path);
  2359. }
  2360. static
  2361. void
  2362. fuse_lib_read(fuse_req_t req,
  2363. uint64_t ino,
  2364. size_t size,
  2365. off_t off,
  2366. fuse_file_info_t *fi)
  2367. {
  2368. struct fuse *f = req_fuse_prepare(req);
  2369. struct fuse_bufvec *buf = NULL;
  2370. int res;
  2371. res = fuse_fs_read_buf(f->fs,&buf,size,off,fi);
  2372. if(res == 0)
  2373. fuse_reply_data(req,buf,FUSE_BUF_SPLICE_MOVE);
  2374. else
  2375. reply_err(req,res);
  2376. fuse_free_buf(buf);
  2377. }
  2378. static
  2379. void
  2380. fuse_lib_write_buf(fuse_req_t req,
  2381. uint64_t ino,
  2382. struct fuse_bufvec *buf,
  2383. off_t off,
  2384. fuse_file_info_t *fi)
  2385. {
  2386. int res;
  2387. struct fuse *f = req_fuse_prepare(req);
  2388. res = fuse_fs_write_buf(f->fs,buf,off,fi);
  2389. free_path(f,ino,NULL);
  2390. if(res >= 0)
  2391. fuse_reply_write(req,res);
  2392. else
  2393. reply_err(req,res);
  2394. }
  2395. static
  2396. void
  2397. fuse_lib_fsync(fuse_req_t req,
  2398. uint64_t ino,
  2399. int datasync,
  2400. fuse_file_info_t *fi)
  2401. {
  2402. int err;
  2403. struct fuse *f = req_fuse_prepare(req);
  2404. err = fuse_fs_fsync(f->fs,datasync,fi);
  2405. reply_err(req,err);
  2406. }
  2407. static
  2408. struct fuse_dh*
  2409. get_dirhandle(const fuse_file_info_t *llfi,
  2410. fuse_file_info_t *fi)
  2411. {
  2412. struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
  2413. memset(fi,0,sizeof(fuse_file_info_t));
  2414. fi->fh = dh->fh;
  2415. return dh;
  2416. }
  2417. static
  2418. void
  2419. fuse_lib_opendir(fuse_req_t req,
  2420. uint64_t ino,
  2421. fuse_file_info_t *llfi)
  2422. {
  2423. int err;
  2424. char *path;
  2425. struct fuse_dh *dh;
  2426. fuse_file_info_t fi;
  2427. struct fuse *f = req_fuse_prepare(req);
  2428. dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
  2429. if(dh == NULL)
  2430. {
  2431. reply_err(req,-ENOMEM);
  2432. return;
  2433. }
  2434. fuse_dirents_init(&dh->d);
  2435. fuse_mutex_init(&dh->lock);
  2436. llfi->fh = (uintptr_t)dh;
  2437. memset(&fi,0,sizeof(fi));
  2438. fi.flags = llfi->flags;
  2439. err = get_path(f,ino,&path);
  2440. if(!err)
  2441. {
  2442. err = fuse_fs_opendir(f->fs,path,&fi);
  2443. dh->fh = fi.fh;
  2444. llfi->keep_cache = fi.keep_cache;
  2445. llfi->cache_readdir = fi.cache_readdir;
  2446. }
  2447. if(!err)
  2448. {
  2449. if(fuse_reply_open(req,llfi) == -ENOENT)
  2450. {
  2451. /* The opendir syscall was interrupted,so it
  2452. must be cancelled */
  2453. fuse_fs_releasedir(f->fs,&fi);
  2454. pthread_mutex_destroy(&dh->lock);
  2455. free(dh);
  2456. }
  2457. }
  2458. else
  2459. {
  2460. reply_err(req,err);
  2461. pthread_mutex_destroy(&dh->lock);
  2462. free(dh);
  2463. }
  2464. free_path(f,ino,path);
  2465. }
  2466. static
  2467. size_t
  2468. readdir_buf_size(fuse_dirents_t *d_,
  2469. size_t size_,
  2470. off_t off_)
  2471. {
  2472. if(off_ >= kv_size(d_->offs))
  2473. return 0;
  2474. if((kv_A(d_->offs,off_) + size_) > kv_size(d_->data))
  2475. return (kv_size(d_->data) - kv_A(d_->offs,off_));
  2476. return size_;
  2477. }
  2478. static
  2479. char*
  2480. readdir_buf(fuse_dirents_t *d_,
  2481. off_t off_)
  2482. {
  2483. size_t i;
  2484. i = kv_A(d_->offs,off_);
  2485. return &kv_A(d_->data,i);
  2486. }
  2487. static
  2488. void
  2489. fuse_lib_readdir(fuse_req_t req_,
  2490. uint64_t ino_,
  2491. size_t size_,
  2492. off_t off_,
  2493. fuse_file_info_t *llffi_)
  2494. {
  2495. int rv;
  2496. struct fuse *f;
  2497. fuse_dirents_t *d;
  2498. struct fuse_dh *dh;
  2499. fuse_file_info_t fi;
  2500. f = req_fuse_prepare(req_);
  2501. dh = get_dirhandle(llffi_,&fi);
  2502. d = &dh->d;
  2503. pthread_mutex_lock(&dh->lock);
  2504. rv = 0;
  2505. if((off_ == 0) || (kv_size(d->data) == 0))
  2506. rv = fuse_fs_readdir(f->fs,&fi,d);
  2507. if(rv)
  2508. {
  2509. reply_err(req_,rv);
  2510. goto out;
  2511. }
  2512. size_ = readdir_buf_size(d,size_,off_);
  2513. fuse_reply_buf(req_,
  2514. readdir_buf(d,off_),
  2515. size_);
  2516. out:
  2517. pthread_mutex_unlock(&dh->lock);
  2518. }
  2519. static
  2520. void
  2521. fuse_lib_readdir_plus(fuse_req_t req_,
  2522. uint64_t ino_,
  2523. size_t size_,
  2524. off_t off_,
  2525. fuse_file_info_t *llffi_)
  2526. {
  2527. int rv;
  2528. struct fuse *f;
  2529. fuse_dirents_t *d;
  2530. struct fuse_dh *dh;
  2531. fuse_file_info_t fi;
  2532. f = req_fuse_prepare(req_);
  2533. dh = get_dirhandle(llffi_,&fi);
  2534. d = &dh->d;
  2535. pthread_mutex_lock(&dh->lock);
  2536. rv = 0;
  2537. if((off_ == 0) || (kv_size(d->data) == 0))
  2538. rv = fuse_fs_readdir_plus(f->fs,&fi,d);
  2539. if(rv)
  2540. {
  2541. reply_err(req_,rv);
  2542. goto out;
  2543. }
  2544. size_ = readdir_buf_size(d,size_,off_);
  2545. fuse_reply_buf(req_,
  2546. readdir_buf(d,off_),
  2547. size_);
  2548. out:
  2549. pthread_mutex_unlock(&dh->lock);
  2550. }
  2551. static
  2552. void
  2553. fuse_lib_releasedir(fuse_req_t req_,
  2554. uint64_t ino_,
  2555. fuse_file_info_t *llfi_)
  2556. {
  2557. struct fuse *f;
  2558. struct fuse_dh *dh;
  2559. fuse_file_info_t fi;
  2560. f = req_fuse_prepare(req_);
  2561. dh = get_dirhandle(llfi_,&fi);
  2562. fuse_fs_releasedir(f->fs,&fi);
  2563. /* Done to keep race condition between last readdir reply and the unlock */
  2564. pthread_mutex_lock(&dh->lock);
  2565. pthread_mutex_unlock(&dh->lock);
  2566. pthread_mutex_destroy(&dh->lock);
  2567. fuse_dirents_free(&dh->d);
  2568. free(dh);
  2569. reply_err(req_,0);
  2570. }
  2571. static
  2572. void
  2573. fuse_lib_fsyncdir(fuse_req_t req,
  2574. uint64_t ino,
  2575. int datasync,
  2576. fuse_file_info_t *llfi)
  2577. {
  2578. int err;
  2579. fuse_file_info_t fi;
  2580. struct fuse *f = req_fuse_prepare(req);
  2581. get_dirhandle(llfi,&fi);
  2582. err = fuse_fs_fsyncdir(f->fs,datasync,&fi);
  2583. reply_err(req,err);
  2584. }
  2585. static
  2586. void
  2587. fuse_lib_statfs(fuse_req_t req,
  2588. uint64_t ino)
  2589. {
  2590. struct fuse *f = req_fuse_prepare(req);
  2591. struct statvfs buf;
  2592. char *path = NULL;
  2593. int err = 0;
  2594. memset(&buf,0,sizeof(buf));
  2595. if(ino)
  2596. err = get_path(f,ino,&path);
  2597. if(!err)
  2598. {
  2599. err = fuse_fs_statfs(f->fs,path ? path : "/",&buf);
  2600. free_path(f,ino,path);
  2601. }
  2602. if(!err)
  2603. fuse_reply_statfs(req,&buf);
  2604. else
  2605. reply_err(req,err);
  2606. }
  2607. static
  2608. void
  2609. fuse_lib_setxattr(fuse_req_t req,
  2610. uint64_t ino,
  2611. const char *name,
  2612. const char *value,
  2613. size_t size,
  2614. int flags)
  2615. {
  2616. struct fuse *f = req_fuse_prepare(req);
  2617. char *path;
  2618. int err;
  2619. err = get_path(f,ino,&path);
  2620. if(!err)
  2621. {
  2622. err = fuse_fs_setxattr(f->fs,path,name,value,size,flags);
  2623. free_path(f,ino,path);
  2624. }
  2625. reply_err(req,err);
  2626. }
  2627. static
  2628. int
  2629. common_getxattr(struct fuse *f,
  2630. fuse_req_t req,
  2631. uint64_t ino,
  2632. const char *name,
  2633. char *value,
  2634. size_t size)
  2635. {
  2636. int err;
  2637. char *path;
  2638. err = get_path(f,ino,&path);
  2639. if(!err)
  2640. {
  2641. err = fuse_fs_getxattr(f->fs,path,name,value,size);
  2642. free_path(f,ino,path);
  2643. }
  2644. return err;
  2645. }
  2646. static
  2647. void
  2648. fuse_lib_getxattr(fuse_req_t req,
  2649. uint64_t ino,
  2650. const char *name,
  2651. size_t size)
  2652. {
  2653. struct fuse *f = req_fuse_prepare(req);
  2654. int res;
  2655. if(size)
  2656. {
  2657. char *value = (char *)malloc(size);
  2658. if(value == NULL)
  2659. {
  2660. reply_err(req,-ENOMEM);
  2661. return;
  2662. }
  2663. res = common_getxattr(f,req,ino,name,value,size);
  2664. if(res > 0)
  2665. fuse_reply_buf(req,value,res);
  2666. else
  2667. reply_err(req,res);
  2668. free(value);
  2669. }
  2670. else
  2671. {
  2672. res = common_getxattr(f,req,ino,name,NULL,0);
  2673. if(res >= 0)
  2674. fuse_reply_xattr(req,res);
  2675. else
  2676. reply_err(req,res);
  2677. }
  2678. }
  2679. static
  2680. int
  2681. common_listxattr(struct fuse *f,
  2682. fuse_req_t req,
  2683. uint64_t ino,
  2684. char *list,
  2685. size_t size)
  2686. {
  2687. char *path;
  2688. int err;
  2689. err = get_path(f,ino,&path);
  2690. if(!err)
  2691. {
  2692. err = fuse_fs_listxattr(f->fs,path,list,size);
  2693. free_path(f,ino,path);
  2694. }
  2695. return err;
  2696. }
  2697. static
  2698. void
  2699. fuse_lib_listxattr(fuse_req_t req,
  2700. uint64_t ino,
  2701. size_t size)
  2702. {
  2703. struct fuse *f = req_fuse_prepare(req);
  2704. int res;
  2705. if(size)
  2706. {
  2707. char *list = (char *)malloc(size);
  2708. if(list == NULL)
  2709. {
  2710. reply_err(req,-ENOMEM);
  2711. return;
  2712. }
  2713. res = common_listxattr(f,req,ino,list,size);
  2714. if(res > 0)
  2715. fuse_reply_buf(req,list,res);
  2716. else
  2717. reply_err(req,res);
  2718. free(list);
  2719. }
  2720. else
  2721. {
  2722. res = common_listxattr(f,req,ino,NULL,0);
  2723. if(res >= 0)
  2724. fuse_reply_xattr(req,res);
  2725. else
  2726. reply_err(req,res);
  2727. }
  2728. }
  2729. static
  2730. void
  2731. fuse_lib_removexattr(fuse_req_t req,
  2732. uint64_t ino,
  2733. const char *name)
  2734. {
  2735. struct fuse *f = req_fuse_prepare(req);
  2736. char *path;
  2737. int err;
  2738. err = get_path(f,ino,&path);
  2739. if(!err)
  2740. {
  2741. err = fuse_fs_removexattr(f->fs,path,name);
  2742. free_path(f,ino,path);
  2743. }
  2744. reply_err(req,err);
  2745. }
  2746. static
  2747. void
  2748. fuse_lib_copy_file_range(fuse_req_t req_,
  2749. uint64_t nodeid_in_,
  2750. off_t off_in_,
  2751. fuse_file_info_t *ffi_in_,
  2752. uint64_t nodeid_out_,
  2753. off_t off_out_,
  2754. fuse_file_info_t *ffi_out_,
  2755. size_t len_,
  2756. int flags_)
  2757. {
  2758. ssize_t rv;
  2759. struct fuse *f;
  2760. f = req_fuse_prepare(req_);
  2761. rv = fuse_fs_copy_file_range(f->fs,
  2762. ffi_in_,
  2763. off_in_,
  2764. ffi_out_,
  2765. off_out_,
  2766. len_,
  2767. flags_);
  2768. if(rv >= 0)
  2769. fuse_reply_write(req_,rv);
  2770. else
  2771. reply_err(req_,rv);
  2772. }
  2773. static
  2774. struct lock*
  2775. locks_conflict(struct node *node,
  2776. const struct lock *lock)
  2777. {
  2778. struct lock *l;
  2779. for(l = node->locks; l; l = l->next)
  2780. if(l->owner != lock->owner &&
  2781. lock->start <= l->end && l->start <= lock->end &&
  2782. (l->type == F_WRLCK || lock->type == F_WRLCK))
  2783. break;
  2784. return l;
  2785. }
  2786. static
  2787. void
  2788. delete_lock(struct lock **lockp)
  2789. {
  2790. struct lock *l = *lockp;
  2791. *lockp = l->next;
  2792. free(l);
  2793. }
  2794. static
  2795. void
  2796. insert_lock(struct lock **pos,
  2797. struct lock *lock)
  2798. {
  2799. lock->next = *pos;
  2800. *pos = lock;
  2801. }
  2802. static
  2803. int
  2804. locks_insert(struct node *node,
  2805. struct lock *lock)
  2806. {
  2807. struct lock **lp;
  2808. struct lock *newl1 = NULL;
  2809. struct lock *newl2 = NULL;
  2810. if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
  2811. {
  2812. newl1 = malloc(sizeof(struct lock));
  2813. newl2 = malloc(sizeof(struct lock));
  2814. if(!newl1 || !newl2)
  2815. {
  2816. free(newl1);
  2817. free(newl2);
  2818. return -ENOLCK;
  2819. }
  2820. }
  2821. for(lp = &node->locks; *lp;)
  2822. {
  2823. struct lock *l = *lp;
  2824. if(l->owner != lock->owner)
  2825. goto skip;
  2826. if(lock->type == l->type)
  2827. {
  2828. if(l->end < lock->start - 1)
  2829. goto skip;
  2830. if(lock->end < l->start - 1)
  2831. break;
  2832. if(l->start <= lock->start && lock->end <= l->end)
  2833. goto out;
  2834. if(l->start < lock->start)
  2835. lock->start = l->start;
  2836. if(lock->end < l->end)
  2837. lock->end = l->end;
  2838. goto delete;
  2839. }
  2840. else
  2841. {
  2842. if(l->end < lock->start)
  2843. goto skip;
  2844. if(lock->end < l->start)
  2845. break;
  2846. if(lock->start <= l->start && l->end <= lock->end)
  2847. goto delete;
  2848. if(l->end <= lock->end)
  2849. {
  2850. l->end = lock->start - 1;
  2851. goto skip;
  2852. }
  2853. if(lock->start <= l->start)
  2854. {
  2855. l->start = lock->end + 1;
  2856. break;
  2857. }
  2858. *newl2 = *l;
  2859. newl2->start = lock->end + 1;
  2860. l->end = lock->start - 1;
  2861. insert_lock(&l->next,newl2);
  2862. newl2 = NULL;
  2863. }
  2864. skip:
  2865. lp = &l->next;
  2866. continue;
  2867. delete:
  2868. delete_lock(lp);
  2869. }
  2870. if(lock->type != F_UNLCK)
  2871. {
  2872. *newl1 = *lock;
  2873. insert_lock(lp,newl1);
  2874. newl1 = NULL;
  2875. }
  2876. out:
  2877. free(newl1);
  2878. free(newl2);
  2879. return 0;
  2880. }
  2881. static
  2882. void
  2883. flock_to_lock(struct flock *flock,
  2884. struct lock *lock)
  2885. {
  2886. memset(lock,0,sizeof(struct lock));
  2887. lock->type = flock->l_type;
  2888. lock->start = flock->l_start;
  2889. lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
  2890. lock->pid = flock->l_pid;
  2891. }
  2892. static
  2893. void
  2894. lock_to_flock(struct lock *lock,
  2895. struct flock *flock)
  2896. {
  2897. flock->l_type = lock->type;
  2898. flock->l_start = lock->start;
  2899. flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
  2900. flock->l_pid = lock->pid;
  2901. }
  2902. static
  2903. int
  2904. fuse_flush_common(struct fuse *f,
  2905. fuse_req_t req,
  2906. uint64_t ino,
  2907. fuse_file_info_t *fi)
  2908. {
  2909. struct flock lock;
  2910. struct lock l;
  2911. int err;
  2912. int errlock;
  2913. memset(&lock,0,sizeof(lock));
  2914. lock.l_type = F_UNLCK;
  2915. lock.l_whence = SEEK_SET;
  2916. err = fuse_fs_flush(f->fs,fi);
  2917. errlock = fuse_fs_lock(f->fs,fi,F_SETLK,&lock);
  2918. if(errlock != -ENOSYS)
  2919. {
  2920. flock_to_lock(&lock,&l);
  2921. l.owner = fi->lock_owner;
  2922. pthread_mutex_lock(&f->lock);
  2923. locks_insert(get_node(f,ino),&l);
  2924. pthread_mutex_unlock(&f->lock);
  2925. /* if op.lock() is defined FLUSH is needed regardless
  2926. of op.flush() */
  2927. if(err == -ENOSYS)
  2928. err = 0;
  2929. }
  2930. return err;
  2931. }
  2932. static
  2933. void
  2934. fuse_lib_release(fuse_req_t req,
  2935. uint64_t ino,
  2936. fuse_file_info_t *fi)
  2937. {
  2938. int err = 0;
  2939. struct fuse *f = req_fuse_prepare(req);
  2940. if(fi->flush)
  2941. {
  2942. err = fuse_flush_common(f,req,ino,fi);
  2943. if(err == -ENOSYS)
  2944. err = 0;
  2945. }
  2946. fuse_do_release(f,ino,fi);
  2947. reply_err(req,err);
  2948. }
  2949. static
  2950. void
  2951. fuse_lib_flush(fuse_req_t req,
  2952. uint64_t ino,
  2953. fuse_file_info_t *fi)
  2954. {
  2955. int err;
  2956. struct fuse *f = req_fuse_prepare(req);
  2957. err = fuse_flush_common(f,req,ino,fi);
  2958. reply_err(req,err);
  2959. }
  2960. static
  2961. int
  2962. fuse_lock_common(fuse_req_t req,
  2963. uint64_t ino,
  2964. fuse_file_info_t *fi,
  2965. struct flock *lock,
  2966. int cmd)
  2967. {
  2968. int err;
  2969. struct fuse *f = req_fuse_prepare(req);
  2970. err = fuse_fs_lock(f->fs,fi,cmd,lock);
  2971. return err;
  2972. }
  2973. static
  2974. void
  2975. fuse_lib_getlk(fuse_req_t req,
  2976. uint64_t ino,
  2977. fuse_file_info_t *fi,
  2978. struct flock *lock)
  2979. {
  2980. int err;
  2981. struct lock l;
  2982. struct lock *conflict;
  2983. struct fuse *f = req_fuse(req);
  2984. flock_to_lock(lock,&l);
  2985. l.owner = fi->lock_owner;
  2986. pthread_mutex_lock(&f->lock);
  2987. conflict = locks_conflict(get_node(f,ino),&l);
  2988. if(conflict)
  2989. lock_to_flock(conflict,lock);
  2990. pthread_mutex_unlock(&f->lock);
  2991. if(!conflict)
  2992. err = fuse_lock_common(req,ino,fi,lock,F_GETLK);
  2993. else
  2994. err = 0;
  2995. if(!err)
  2996. fuse_reply_lock(req,lock);
  2997. else
  2998. reply_err(req,err);
  2999. }
  3000. static
  3001. void
  3002. fuse_lib_setlk(fuse_req_t req,
  3003. uint64_t ino,
  3004. fuse_file_info_t *fi,
  3005. struct flock *lock,
  3006. int sleep)
  3007. {
  3008. int err = fuse_lock_common(req,ino,fi,lock,
  3009. sleep ? F_SETLKW : F_SETLK);
  3010. if(!err)
  3011. {
  3012. struct fuse *f = req_fuse(req);
  3013. struct lock l;
  3014. flock_to_lock(lock,&l);
  3015. l.owner = fi->lock_owner;
  3016. pthread_mutex_lock(&f->lock);
  3017. locks_insert(get_node(f,ino),&l);
  3018. pthread_mutex_unlock(&f->lock);
  3019. }
  3020. reply_err(req,err);
  3021. }
  3022. static
  3023. void
  3024. fuse_lib_flock(fuse_req_t req,
  3025. uint64_t ino,
  3026. fuse_file_info_t *fi,
  3027. int op)
  3028. {
  3029. int err;
  3030. struct fuse *f = req_fuse_prepare(req);
  3031. err = fuse_fs_flock(f->fs,fi,op);
  3032. reply_err(req,err);
  3033. }
  3034. static
  3035. void
  3036. fuse_lib_bmap(fuse_req_t req,
  3037. uint64_t ino,
  3038. size_t blocksize,
  3039. uint64_t idx)
  3040. {
  3041. int err;
  3042. char *path;
  3043. struct fuse *f = req_fuse_prepare(req);
  3044. err = get_path(f,ino,&path);
  3045. if(!err)
  3046. {
  3047. err = fuse_fs_bmap(f->fs,path,blocksize,&idx);
  3048. free_path(f,ino,path);
  3049. }
  3050. if(!err)
  3051. fuse_reply_bmap(req,idx);
  3052. else
  3053. reply_err(req,err);
  3054. }
  3055. static
  3056. void
  3057. fuse_lib_ioctl(fuse_req_t req,
  3058. uint64_t ino,
  3059. unsigned long cmd,
  3060. void *arg,
  3061. fuse_file_info_t *llfi,
  3062. unsigned int flags,
  3063. const void *in_buf,
  3064. uint32_t in_bufsz,
  3065. uint32_t out_bufsz_)
  3066. {
  3067. int err;
  3068. char *out_buf = NULL;
  3069. struct fuse *f = req_fuse_prepare(req);
  3070. fuse_file_info_t fi;
  3071. uint32_t out_bufsz = out_bufsz_;
  3072. err = -EPERM;
  3073. if(flags & FUSE_IOCTL_UNRESTRICTED)
  3074. goto err;
  3075. if(flags & FUSE_IOCTL_DIR)
  3076. get_dirhandle(llfi,&fi);
  3077. else
  3078. fi = *llfi;
  3079. if(out_bufsz)
  3080. {
  3081. err = -ENOMEM;
  3082. out_buf = malloc(out_bufsz);
  3083. if(!out_buf)
  3084. goto err;
  3085. }
  3086. assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
  3087. if(out_buf)
  3088. memcpy(out_buf,in_buf,in_bufsz);
  3089. err = fuse_fs_ioctl(f->fs,cmd,arg,&fi,flags,
  3090. out_buf ?: (void *)in_buf,&out_bufsz);
  3091. fuse_reply_ioctl(req,err,out_buf,out_bufsz);
  3092. goto out;
  3093. err:
  3094. reply_err(req,err);
  3095. out:
  3096. free(out_buf);
  3097. }
  3098. static
  3099. void
  3100. fuse_lib_poll(fuse_req_t req,
  3101. uint64_t ino,
  3102. fuse_file_info_t *fi,
  3103. fuse_pollhandle_t *ph)
  3104. {
  3105. int err;
  3106. struct fuse *f = req_fuse_prepare(req);
  3107. unsigned revents = 0;
  3108. err = fuse_fs_poll(f->fs,fi,ph,&revents);
  3109. if(!err)
  3110. fuse_reply_poll(req,revents);
  3111. else
  3112. reply_err(req,err);
  3113. }
  3114. static
  3115. void
  3116. fuse_lib_fallocate(fuse_req_t req,
  3117. uint64_t ino,
  3118. int mode,
  3119. off_t offset,
  3120. off_t length,
  3121. fuse_file_info_t *fi)
  3122. {
  3123. int err;
  3124. struct fuse *f = req_fuse_prepare(req);
  3125. err = fuse_fs_fallocate(f->fs,mode,offset,length,fi);
  3126. reply_err(req,err);
  3127. }
  3128. static
  3129. int
  3130. remembered_node_cmp(const void *a_,
  3131. const void *b_)
  3132. {
  3133. const remembered_node_t *a = a_;
  3134. const remembered_node_t *b = b_;
  3135. return (a->time - b->time);
  3136. }
  3137. static
  3138. void
  3139. remembered_nodes_sort(struct fuse *f_)
  3140. {
  3141. pthread_mutex_lock(&f_->lock);
  3142. qsort(&kv_first(f_->remembered_nodes),
  3143. kv_size(f_->remembered_nodes),
  3144. sizeof(remembered_node_t),
  3145. remembered_node_cmp);
  3146. pthread_mutex_unlock(&f_->lock);
  3147. }
  3148. #define MAX_PRUNE 100
  3149. #define MAX_CHECK 1000
  3150. int
  3151. fuse_prune_some_remembered_nodes(struct fuse *f_,
  3152. int *offset_)
  3153. {
  3154. time_t now;
  3155. int pruned;
  3156. int checked;
  3157. pthread_mutex_lock(&f_->lock);
  3158. pruned = 0;
  3159. checked = 0;
  3160. now = current_time();
  3161. while(*offset_ < kv_size(f_->remembered_nodes))
  3162. {
  3163. time_t age;
  3164. remembered_node_t *fn = &kv_A(f_->remembered_nodes,*offset_);
  3165. if(pruned >= MAX_PRUNE)
  3166. break;
  3167. if(checked >= MAX_CHECK)
  3168. break;
  3169. checked++;
  3170. age = (now - fn->time);
  3171. if(f_->conf.remember > age)
  3172. break;
  3173. assert(fn->node->nlookup == 1);
  3174. /* Don't forget active directories */
  3175. if(fn->node->refctr > 1)
  3176. {
  3177. (*offset_)++;
  3178. continue;
  3179. }
  3180. fn->node->nlookup = 0;
  3181. unref_node(f_,fn->node);
  3182. kv_delete(f_->remembered_nodes,*offset_);
  3183. pruned++;
  3184. }
  3185. pthread_mutex_unlock(&f_->lock);
  3186. if((pruned < MAX_PRUNE) && (checked < MAX_CHECK))
  3187. *offset_ = -1;
  3188. return pruned;
  3189. }
  3190. #undef MAX_PRUNE
  3191. #undef MAX_CHECK
  3192. static
  3193. void
  3194. sleep_100ms(void)
  3195. {
  3196. const struct timespec ms100 = {0,100 * 1000000};
  3197. nanosleep(&ms100,NULL);
  3198. }
  3199. void
  3200. fuse_prune_remembered_nodes(struct fuse *f_)
  3201. {
  3202. int offset;
  3203. int pruned;
  3204. offset = 0;
  3205. pruned = 0;
  3206. for(;;)
  3207. {
  3208. pruned += fuse_prune_some_remembered_nodes(f_,&offset);
  3209. if(offset >= 0)
  3210. {
  3211. sleep_100ms();
  3212. continue;
  3213. }
  3214. break;
  3215. }
  3216. if(pruned > 0)
  3217. remembered_nodes_sort(f_);
  3218. }
  3219. static struct fuse_lowlevel_ops fuse_path_ops =
  3220. {
  3221. .access = fuse_lib_access,
  3222. .bmap = fuse_lib_bmap,
  3223. .copy_file_range = fuse_lib_copy_file_range,
  3224. .create = fuse_lib_create,
  3225. .destroy = fuse_lib_destroy,
  3226. .fallocate = fuse_lib_fallocate,
  3227. .flock = fuse_lib_flock,
  3228. .flush = fuse_lib_flush,
  3229. .forget = fuse_lib_forget,
  3230. .forget_multi = fuse_lib_forget_multi,
  3231. .fsync = fuse_lib_fsync,
  3232. .fsyncdir = fuse_lib_fsyncdir,
  3233. .getattr = fuse_lib_getattr,
  3234. .getlk = fuse_lib_getlk,
  3235. .getxattr = fuse_lib_getxattr,
  3236. .init = fuse_lib_init,
  3237. .ioctl = fuse_lib_ioctl,
  3238. .link = fuse_lib_link,
  3239. .listxattr = fuse_lib_listxattr,
  3240. .lookup = fuse_lib_lookup,
  3241. .mkdir = fuse_lib_mkdir,
  3242. .mknod = fuse_lib_mknod,
  3243. .open = fuse_lib_open,
  3244. .opendir = fuse_lib_opendir,
  3245. .poll = fuse_lib_poll,
  3246. .read = fuse_lib_read,
  3247. .readdir = fuse_lib_readdir,
  3248. .readdir_plus = fuse_lib_readdir_plus,
  3249. .readlink = fuse_lib_readlink,
  3250. .release = fuse_lib_release,
  3251. .releasedir = fuse_lib_releasedir,
  3252. .removexattr = fuse_lib_removexattr,
  3253. .rename = fuse_lib_rename,
  3254. .retrieve_reply = NULL,
  3255. .rmdir = fuse_lib_rmdir,
  3256. .setattr = fuse_lib_setattr,
  3257. .setlk = fuse_lib_setlk,
  3258. .setxattr = fuse_lib_setxattr,
  3259. .statfs = fuse_lib_statfs,
  3260. .symlink = fuse_lib_symlink,
  3261. .unlink = fuse_lib_unlink,
  3262. .write_buf = fuse_lib_write_buf,
  3263. };
  3264. int
  3265. fuse_notify_poll(fuse_pollhandle_t *ph)
  3266. {
  3267. return fuse_lowlevel_notify_poll(ph);
  3268. }
  3269. static
  3270. void
  3271. free_cmd(struct fuse_cmd *cmd)
  3272. {
  3273. free(cmd->buf);
  3274. free(cmd);
  3275. }
  3276. void
  3277. fuse_process_cmd(struct fuse *f,
  3278. struct fuse_cmd *cmd)
  3279. {
  3280. fuse_session_process(f->se,cmd->buf,cmd->buflen,cmd->ch);
  3281. free_cmd(cmd);
  3282. }
  3283. int
  3284. fuse_exited(struct fuse *f)
  3285. {
  3286. return fuse_session_exited(f->se);
  3287. }
  3288. struct fuse_session*
  3289. fuse_get_session(struct fuse *f)
  3290. {
  3291. return f->se;
  3292. }
  3293. static
  3294. struct fuse_cmd*
  3295. fuse_alloc_cmd(size_t bufsize)
  3296. {
  3297. struct fuse_cmd *cmd = (struct fuse_cmd *)malloc(sizeof(*cmd));
  3298. if(cmd == NULL)
  3299. {
  3300. fprintf(stderr,"fuse: failed to allocate cmd\n");
  3301. return NULL;
  3302. }
  3303. cmd->buf = (char *)malloc(bufsize);
  3304. if(cmd->buf == NULL)
  3305. {
  3306. fprintf(stderr,"fuse: failed to allocate read buffer\n");
  3307. free(cmd);
  3308. return NULL;
  3309. }
  3310. return cmd;
  3311. }
  3312. struct fuse_cmd*
  3313. fuse_read_cmd(struct fuse *f)
  3314. {
  3315. struct fuse_chan *ch = fuse_session_next_chan(f->se,NULL);
  3316. size_t bufsize = fuse_chan_bufsize(ch);
  3317. struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize);
  3318. if(cmd != NULL)
  3319. {
  3320. int res = fuse_chan_recv(&ch,cmd->buf,bufsize);
  3321. if(res <= 0)
  3322. {
  3323. free_cmd(cmd);
  3324. if(res < 0 && res != -EINTR && res != -EAGAIN)
  3325. fuse_exit(f);
  3326. return NULL;
  3327. }
  3328. cmd->buflen = res;
  3329. cmd->ch = ch;
  3330. }
  3331. return cmd;
  3332. }
  3333. void
  3334. fuse_exit(struct fuse *f)
  3335. {
  3336. fuse_session_exit(f->se);
  3337. }
  3338. struct fuse_context*
  3339. fuse_get_context(void)
  3340. {
  3341. return &fuse_get_context_internal()->ctx;
  3342. }
  3343. enum {
  3344. KEY_HELP,
  3345. };
  3346. #define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
  3347. static const struct fuse_opt fuse_lib_opts[] =
  3348. {
  3349. FUSE_OPT_KEY("-h", KEY_HELP),
  3350. FUSE_OPT_KEY("--help", KEY_HELP),
  3351. FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
  3352. FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
  3353. FUSE_LIB_OPT("debug", debug,1),
  3354. FUSE_LIB_OPT("-d", debug,1),
  3355. FUSE_LIB_OPT("nogc", nogc,1),
  3356. FUSE_LIB_OPT("umask=", set_mode,1),
  3357. FUSE_LIB_OPT("umask=%o", umask,0),
  3358. FUSE_LIB_OPT("uid=", set_uid,1),
  3359. FUSE_LIB_OPT("uid=%d", uid,0),
  3360. FUSE_LIB_OPT("gid=", set_gid,1),
  3361. FUSE_LIB_OPT("gid=%d", gid,0),
  3362. FUSE_LIB_OPT("noforget", remember,-1),
  3363. FUSE_LIB_OPT("remember=%u", remember,0),
  3364. FUSE_LIB_OPT("threads=%d", threads,0),
  3365. FUSE_LIB_OPT("use_ino", use_ino,1),
  3366. FUSE_OPT_END
  3367. };
  3368. static void fuse_lib_help(void)
  3369. {
  3370. fprintf(stderr,
  3371. " -o umask=M set file permissions (octal)\n"
  3372. " -o uid=N set file owner\n"
  3373. " -o gid=N set file group\n"
  3374. " -o noforget never forget cached inodes\n"
  3375. " -o remember=T remember cached inodes for T seconds (0s)\n"
  3376. " -o threads=NUM number of worker threads. 0 = autodetect.\n"
  3377. " Negative values autodetect then divide by\n"
  3378. " absolute value. default = 0\n"
  3379. "\n");
  3380. }
  3381. static
  3382. int
  3383. fuse_lib_opt_proc(void *data,
  3384. const char *arg,
  3385. int key,
  3386. struct fuse_args *outargs)
  3387. {
  3388. (void)arg; (void)outargs;
  3389. if(key == KEY_HELP)
  3390. {
  3391. struct fuse_config *conf = (struct fuse_config *)data;
  3392. fuse_lib_help();
  3393. conf->help = 1;
  3394. }
  3395. return 1;
  3396. }
  3397. int
  3398. fuse_is_lib_option(const char *opt)
  3399. {
  3400. return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
  3401. }
  3402. struct fuse_fs*
  3403. fuse_fs_new(const struct fuse_operations *op,
  3404. size_t op_size)
  3405. {
  3406. struct fuse_fs *fs;
  3407. if(sizeof(struct fuse_operations) < op_size)
  3408. {
  3409. fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
  3410. op_size = sizeof(struct fuse_operations);
  3411. }
  3412. fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
  3413. if(!fs)
  3414. {
  3415. fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
  3416. return NULL;
  3417. }
  3418. if(op)
  3419. memcpy(&fs->op,op,op_size);
  3420. return fs;
  3421. }
  3422. static
  3423. int
  3424. node_table_init(struct node_table *t)
  3425. {
  3426. t->size = NODE_TABLE_MIN_SIZE;
  3427. t->array = (struct node **)calloc(1,sizeof(struct node *) * t->size);
  3428. if(t->array == NULL)
  3429. {
  3430. fprintf(stderr,"fuse: memory allocation failed\n");
  3431. return -1;
  3432. }
  3433. t->use = 0;
  3434. t->split = 0;
  3435. return 0;
  3436. }
  3437. static
  3438. void
  3439. metrics_log_nodes_info(struct fuse *f_,
  3440. FILE *file_)
  3441. {
  3442. char buf[1024];
  3443. pthread_mutex_lock(&f_->lock);
  3444. snprintf(buf,sizeof(buf),
  3445. "time: %zu\n"
  3446. "sizeof(node): %zu\n"
  3447. "node id_table size: %zu\n"
  3448. "node id_table usage: %zu\n"
  3449. "node id_table total allocated memory: %zu\n"
  3450. "node name_table size: %zu\n"
  3451. "node name_table usage: %zu\n"
  3452. "node name_table total allocated memory: %zu\n"
  3453. "node memory pool slab count: %zu\n"
  3454. "node memory pool usage ratio: %f\n"
  3455. "node memory pool avail objs: %zu\n"
  3456. "node memory pool total allocated memory: %zu\n"
  3457. "\n"
  3458. ,
  3459. time(NULL),
  3460. sizeof(struct node),
  3461. f_->id_table.size,
  3462. f_->id_table.use,
  3463. (f_->id_table.size * sizeof(struct node*)),
  3464. f_->name_table.size,
  3465. f_->name_table.use,
  3466. (f_->name_table.size * sizeof(struct node*)),
  3467. lfmp_slab_count(&f_->node_fmp),
  3468. lfmp_slab_usage_ratio(&f_->node_fmp),
  3469. lfmp_avail_objs(&f_->node_fmp),
  3470. lfmp_total_allocated_memory(&f_->node_fmp)
  3471. );
  3472. pthread_mutex_unlock(&f_->lock);
  3473. fputs(buf,file_);
  3474. }
  3475. static
  3476. void
  3477. metrics_log_nodes_info_to_tmp_dir(struct fuse *f_)
  3478. {
  3479. FILE *file;
  3480. char filepath[256];
  3481. sprintf(filepath,"/tmp/mergerfs.%d.info",getpid());
  3482. file = fopen(filepath,"w");
  3483. if(file == NULL)
  3484. return;
  3485. metrics_log_nodes_info(f_,file);
  3486. fclose(file);
  3487. }
  3488. static
  3489. void
  3490. fuse_malloc_trim(void)
  3491. {
  3492. #ifdef HAVE_MALLOC_TRIM
  3493. malloc_trim(1024 * 1024);
  3494. #endif
  3495. }
  3496. static
  3497. void*
  3498. fuse_maintenance_loop(void *fuse_)
  3499. {
  3500. int gc;
  3501. int loops;
  3502. int sleep_time;
  3503. double slab_usage_ratio;
  3504. struct fuse *f = (struct fuse*)fuse_;
  3505. gc = 0;
  3506. loops = 0;
  3507. sleep_time = 60;
  3508. while(1)
  3509. {
  3510. if(remember_nodes(f))
  3511. fuse_prune_remembered_nodes(f);
  3512. if((loops % 15) == 0)
  3513. {
  3514. fuse_malloc_trim();
  3515. gc = 1;
  3516. }
  3517. // Trigger a followup gc if this gc succeeds
  3518. if(!f->conf.nogc && gc)
  3519. gc = lfmp_gc(&f->node_fmp);
  3520. if(g_LOG_METRICS)
  3521. metrics_log_nodes_info_to_tmp_dir(f);
  3522. loops++;
  3523. sleep(sleep_time);
  3524. }
  3525. return NULL;
  3526. }
  3527. int
  3528. fuse_start_maintenance_thread(struct fuse *f_)
  3529. {
  3530. return fuse_start_thread(&f_->maintenance_thread,fuse_maintenance_loop,f_);
  3531. }
  3532. void
  3533. fuse_stop_maintenance_thread(struct fuse *f_)
  3534. {
  3535. pthread_mutex_lock(&f_->lock);
  3536. pthread_cancel(f_->maintenance_thread);
  3537. pthread_mutex_unlock(&f_->lock);
  3538. pthread_join(f_->maintenance_thread,NULL);
  3539. }
  3540. struct fuse*
  3541. fuse_new_common(struct fuse_chan *ch,
  3542. struct fuse_args *args,
  3543. const struct fuse_operations *op,
  3544. size_t op_size)
  3545. {
  3546. struct fuse *f;
  3547. struct node *root;
  3548. struct fuse_fs *fs;
  3549. struct fuse_lowlevel_ops llop = fuse_path_ops;
  3550. if(fuse_create_context_key() == -1)
  3551. goto out;
  3552. f = (struct fuse *)calloc(1,sizeof(struct fuse));
  3553. if(f == NULL)
  3554. {
  3555. fprintf(stderr,"fuse: failed to allocate fuse object\n");
  3556. goto out_delete_context_key;
  3557. }
  3558. fs = fuse_fs_new(op,op_size);
  3559. if(!fs)
  3560. goto out_free;
  3561. f->fs = fs;
  3562. /* Oh f**k,this is ugly! */
  3563. if(!fs->op.lock)
  3564. {
  3565. llop.getlk = NULL;
  3566. llop.setlk = NULL;
  3567. }
  3568. if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
  3569. goto out_free_fs;
  3570. g_LOG_METRICS = f->conf.debug;
  3571. f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
  3572. if(f->se == NULL)
  3573. goto out_free_fs;
  3574. fuse_session_add_chan(f->se,ch);
  3575. /* Trace topmost layer by default */
  3576. srand(time(NULL));
  3577. f->nodeid_gen.nodeid = FUSE_ROOT_ID;
  3578. f->nodeid_gen.generation = rand64();
  3579. if(node_table_init(&f->name_table) == -1)
  3580. goto out_free_session;
  3581. if(node_table_init(&f->id_table) == -1)
  3582. goto out_free_name_table;
  3583. fuse_mutex_init(&f->lock);
  3584. lfmp_init(&f->node_fmp,sizeof(struct node),256);
  3585. kv_init(f->remembered_nodes);
  3586. root = alloc_node(f);
  3587. if(root == NULL)
  3588. {
  3589. fprintf(stderr,"fuse: memory allocation failed\n");
  3590. goto out_free_id_table;
  3591. }
  3592. root->name = filename_strdup(f,"/");
  3593. root->parent = NULL;
  3594. root->nodeid = FUSE_ROOT_ID;
  3595. inc_nlookup(root);
  3596. hash_id(f,root);
  3597. return f;
  3598. out_free_id_table:
  3599. free(f->id_table.array);
  3600. out_free_name_table:
  3601. free(f->name_table.array);
  3602. out_free_session:
  3603. fuse_session_destroy(f->se);
  3604. out_free_fs:
  3605. /* Horrible compatibility hack to stop the destructor from being
  3606. called on the filesystem without init being called first */
  3607. fs->op.destroy = NULL;
  3608. fuse_fs_destroy(f->fs);
  3609. out_free:
  3610. free(f);
  3611. out_delete_context_key:
  3612. fuse_delete_context_key();
  3613. out:
  3614. return NULL;
  3615. }
  3616. struct fuse*
  3617. fuse_new(struct fuse_chan *ch,
  3618. struct fuse_args *args,
  3619. const struct fuse_operations *op,
  3620. size_t op_size)
  3621. {
  3622. return fuse_new_common(ch,args,op,op_size);
  3623. }
  3624. void
  3625. fuse_destroy(struct fuse *f)
  3626. {
  3627. size_t i;
  3628. if(f->fs)
  3629. {
  3630. struct fuse_context_i *c = fuse_get_context_internal();
  3631. memset(c,0,sizeof(*c));
  3632. c->ctx.fuse = f;
  3633. for(i = 0; i < f->id_table.size; i++)
  3634. {
  3635. struct node *node;
  3636. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  3637. {
  3638. if(node->is_hidden)
  3639. fuse_fs_free_hide(f->fs,node->hidden_fh);
  3640. }
  3641. }
  3642. }
  3643. for(i = 0; i < f->id_table.size; i++)
  3644. {
  3645. struct node *node;
  3646. struct node *next;
  3647. for(node = f->id_table.array[i]; node != NULL; node = next)
  3648. {
  3649. next = node->id_next;
  3650. free_node(f,node);
  3651. f->id_table.use--;
  3652. }
  3653. }
  3654. free(f->id_table.array);
  3655. free(f->name_table.array);
  3656. pthread_mutex_destroy(&f->lock);
  3657. fuse_session_destroy(f->se);
  3658. lfmp_destroy(&f->node_fmp);
  3659. kv_destroy(f->remembered_nodes);
  3660. free(f);
  3661. fuse_delete_context_key();
  3662. }
  3663. int
  3664. fuse_config_num_threads(const struct fuse *fuse_)
  3665. {
  3666. return fuse_->conf.threads;
  3667. }
  3668. void
  3669. fuse_log_metrics_set(int log_)
  3670. {
  3671. g_LOG_METRICS = log_;
  3672. }
  3673. int
  3674. fuse_log_metrics_get(void)
  3675. {
  3676. return g_LOG_METRICS;
  3677. }