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.

4167 lines
84 KiB

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