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.

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