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.

4920 lines
102 KiB

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