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.

4177 lines
84 KiB

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