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.

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