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.

4535 lines
102 KiB

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