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.

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