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.

4937 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,qe->wnode2,true);
  971. if(!err)
  972. qe->second_locked = true;
  973. else if(err != -EAGAIN)
  974. goto err_unlock;
  975. }
  976. if(qe->first_locked && (qe->second_locked || !qe->path2))
  977. {
  978. err = 0;
  979. goto done;
  980. }
  981. /*
  982. * Only let the first element be partially locked otherwise there could
  983. * be a deadlock.
  984. *
  985. * But do allow the first element to be partially locked to prevent
  986. * starvation.
  987. */
  988. if(!first)
  989. queue_element_unlock(f,qe);
  990. /* keep trying */
  991. return;
  992. err_unlock:
  993. queue_element_unlock(f,qe);
  994. done:
  995. qe->err = err;
  996. qe->done = true;
  997. pthread_cond_signal(&qe->cond);
  998. }
  999. static
  1000. void
  1001. wake_up_queued(struct fuse *f)
  1002. {
  1003. struct lock_queue_element *qe;
  1004. for(qe = f->lockq; qe != NULL; qe = qe->next)
  1005. queue_element_wakeup(f,qe);
  1006. }
  1007. static
  1008. void
  1009. debug_path(struct fuse *f,
  1010. const char *msg,
  1011. fuse_ino_t nodeid,
  1012. const char *name,
  1013. bool wr)
  1014. {
  1015. if(f->conf.debug)
  1016. {
  1017. struct node *wnode = NULL;
  1018. if(wr)
  1019. wnode = lookup_node(f,nodeid,name);
  1020. if(wnode)
  1021. fprintf(stderr,"%s %li (w)\n", msg,wnode->nodeid);
  1022. else
  1023. fprintf(stderr,"%s %li\n",msg,nodeid);
  1024. }
  1025. }
  1026. static
  1027. void
  1028. queue_path(struct fuse *f,
  1029. struct lock_queue_element *qe)
  1030. {
  1031. struct lock_queue_element **qp;
  1032. qe->done = false;
  1033. qe->first_locked = false;
  1034. qe->second_locked = false;
  1035. pthread_cond_init(&qe->cond,NULL);
  1036. qe->next = NULL;
  1037. for(qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
  1038. *qp = qe;
  1039. }
  1040. static
  1041. void
  1042. dequeue_path(struct fuse *f,
  1043. struct lock_queue_element *qe)
  1044. {
  1045. struct lock_queue_element **qp;
  1046. pthread_cond_destroy(&qe->cond);
  1047. for(qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
  1048. *qp = qe->next;
  1049. }
  1050. static
  1051. int
  1052. wait_path(struct fuse *f,
  1053. struct lock_queue_element *qe)
  1054. {
  1055. queue_path(f,qe);
  1056. do
  1057. {
  1058. pthread_cond_wait(&qe->cond,&f->lock);
  1059. } while(!qe->done);
  1060. dequeue_path(f,qe);
  1061. return qe->err;
  1062. }
  1063. static
  1064. int
  1065. get_path_common(struct fuse *f,
  1066. fuse_ino_t nodeid,
  1067. const char *name,
  1068. char **path,
  1069. struct node **wnode)
  1070. {
  1071. int err;
  1072. pthread_mutex_lock(&f->lock);
  1073. err = try_get_path(f,nodeid,name,path,wnode,true);
  1074. if(err == -EAGAIN)
  1075. {
  1076. struct lock_queue_element qe = {0};
  1077. qe.nodeid1 = nodeid;
  1078. qe.name1 = name;
  1079. qe.path1 = path;
  1080. qe.wnode1 = wnode;
  1081. debug_path(f,"QUEUE PATH",nodeid,name,!!wnode);
  1082. err = wait_path(f,&qe);
  1083. debug_path(f,"DEQUEUE PATH",nodeid,name,!!wnode);
  1084. }
  1085. pthread_mutex_unlock(&f->lock);
  1086. return err;
  1087. }
  1088. static
  1089. int
  1090. get_path(struct fuse *f,
  1091. fuse_ino_t nodeid,
  1092. char **path)
  1093. {
  1094. return get_path_common(f,nodeid,NULL,path,NULL);
  1095. }
  1096. static
  1097. int
  1098. get_path_name(struct fuse *f,
  1099. fuse_ino_t nodeid,
  1100. const char *name,
  1101. char **path)
  1102. {
  1103. return get_path_common(f,nodeid,name,path,NULL);
  1104. }
  1105. static
  1106. int
  1107. get_path_wrlock(struct fuse *f,
  1108. fuse_ino_t nodeid,
  1109. const char *name,
  1110. char **path,
  1111. struct node **wnode)
  1112. {
  1113. return get_path_common(f,nodeid,name,path,wnode);
  1114. }
  1115. static
  1116. int
  1117. try_get_path2(struct fuse *f,
  1118. fuse_ino_t nodeid1,
  1119. const char *name1,
  1120. fuse_ino_t nodeid2,
  1121. const char *name2,
  1122. char **path1,
  1123. char **path2,
  1124. struct node **wnode1,
  1125. struct node **wnode2)
  1126. {
  1127. int err;
  1128. /* FIXME: locking two paths needs deadlock checking */
  1129. err = try_get_path(f,nodeid1,name1,path1,wnode1,true);
  1130. if(!err)
  1131. {
  1132. err = try_get_path(f,nodeid2,name2,path2,wnode2,true);
  1133. if(err)
  1134. {
  1135. struct node *wn1 = wnode1 ? *wnode1 : NULL;
  1136. unlock_path(f,nodeid1,wn1,NULL);
  1137. free(*path1);
  1138. }
  1139. }
  1140. return err;
  1141. }
  1142. static
  1143. int
  1144. get_path2(struct fuse *f,
  1145. fuse_ino_t nodeid1,
  1146. const char *name1,
  1147. fuse_ino_t nodeid2,
  1148. const char *name2,
  1149. char **path1,
  1150. char **path2,
  1151. struct node **wnode1,
  1152. struct node **wnode2)
  1153. {
  1154. int err;
  1155. pthread_mutex_lock(&f->lock);
  1156. err = try_get_path2(f,nodeid1,name1,nodeid2,name2,
  1157. path1,path2,wnode1,wnode2);
  1158. if(err == -EAGAIN)
  1159. {
  1160. struct lock_queue_element qe = {0};
  1161. qe.nodeid1 = nodeid1;
  1162. qe.name1 = name1;
  1163. qe.path1 = path1;
  1164. qe.wnode1 = wnode1;
  1165. qe.nodeid2 = nodeid2;
  1166. qe.name2 = name2;
  1167. qe.path2 = path2;
  1168. qe.wnode2 = wnode2;
  1169. debug_path(f,"QUEUE PATH1",nodeid1,name1,!!wnode1);
  1170. debug_path(f," PATH2",nodeid2,name2,!!wnode2);
  1171. err = wait_path(f,&qe);
  1172. debug_path(f,"DEQUEUE PATH1",nodeid1,name1,!!wnode1);
  1173. debug_path(f," PATH2",nodeid2,name2,!!wnode2);
  1174. }
  1175. pthread_mutex_unlock(&f->lock);
  1176. return err;
  1177. }
  1178. static
  1179. void
  1180. free_path_wrlock(struct fuse *f,
  1181. fuse_ino_t nodeid,
  1182. struct node *wnode,
  1183. char *path)
  1184. {
  1185. pthread_mutex_lock(&f->lock);
  1186. unlock_path(f,nodeid,wnode,NULL);
  1187. if(f->lockq)
  1188. wake_up_queued(f);
  1189. pthread_mutex_unlock(&f->lock);
  1190. free(path);
  1191. }
  1192. static
  1193. void
  1194. free_path(struct fuse *f,
  1195. fuse_ino_t nodeid,
  1196. char *path)
  1197. {
  1198. if(path)
  1199. free_path_wrlock(f,nodeid,NULL,path);
  1200. }
  1201. static
  1202. void
  1203. free_path2(struct fuse *f,
  1204. fuse_ino_t nodeid1,
  1205. fuse_ino_t nodeid2,
  1206. struct node *wnode1,
  1207. struct node *wnode2,
  1208. char *path1,
  1209. char *path2)
  1210. {
  1211. pthread_mutex_lock(&f->lock);
  1212. unlock_path(f,nodeid1,wnode1,NULL);
  1213. unlock_path(f,nodeid2,wnode2,NULL);
  1214. wake_up_queued(f);
  1215. pthread_mutex_unlock(&f->lock);
  1216. free(path1);
  1217. free(path2);
  1218. }
  1219. static
  1220. void
  1221. forget_node(struct fuse *f,
  1222. const fuse_ino_t nodeid,
  1223. const uint64_t nlookup)
  1224. {
  1225. struct node *node;
  1226. if(nodeid == FUSE_ROOT_ID)
  1227. return;
  1228. pthread_mutex_lock(&f->lock);
  1229. node = get_node(f,nodeid);
  1230. /*
  1231. * Node may still be locked due to interrupt idiocy in open,
  1232. * create and opendir
  1233. */
  1234. while(node->nlookup == nlookup && node->treelock)
  1235. {
  1236. struct lock_queue_element qe = {0};
  1237. qe.nodeid1 = nodeid;
  1238. debug_path(f,"QUEUE PATH (forget)",nodeid,NULL,false);
  1239. queue_path(f,&qe);
  1240. do
  1241. {
  1242. pthread_cond_wait(&qe.cond,&f->lock);
  1243. }
  1244. while((node->nlookup == nlookup) && node->treelock);
  1245. dequeue_path(f,&qe);
  1246. debug_path(f,"DEQUEUE_PATH (forget)",nodeid,NULL,false);
  1247. }
  1248. assert(node->nlookup >= nlookup);
  1249. node->nlookup -= nlookup;
  1250. if(!node->nlookup)
  1251. unref_node(f,node);
  1252. else if(lru_enabled(f) && node->nlookup == 1)
  1253. set_forget_time(f,node);
  1254. pthread_mutex_unlock(&f->lock);
  1255. }
  1256. static
  1257. void
  1258. unlink_node(struct fuse *f,
  1259. struct node *node)
  1260. {
  1261. if(f->conf.remember)
  1262. {
  1263. assert(node->nlookup > 1);
  1264. node->nlookup--;
  1265. }
  1266. unhash_name(f,node);
  1267. }
  1268. static
  1269. void
  1270. remove_node(struct fuse *f,
  1271. fuse_ino_t dir,
  1272. const char *name)
  1273. {
  1274. struct node *node;
  1275. pthread_mutex_lock(&f->lock);
  1276. node = lookup_node(f,dir,name);
  1277. if(node != NULL)
  1278. unlink_node(f,node);
  1279. pthread_mutex_unlock(&f->lock);
  1280. }
  1281. static
  1282. int
  1283. rename_node(struct fuse *f,
  1284. fuse_ino_t olddir,
  1285. const char *oldname,
  1286. fuse_ino_t newdir,
  1287. const char *newname)
  1288. {
  1289. struct node *node;
  1290. struct node *newnode;
  1291. int err = 0;
  1292. pthread_mutex_lock(&f->lock);
  1293. node = lookup_node(f,olddir,oldname);
  1294. newnode = lookup_node(f,newdir,newname);
  1295. if(node == NULL)
  1296. goto out;
  1297. if(newnode != NULL)
  1298. unlink_node(f,newnode);
  1299. unhash_name(f,node);
  1300. if(hash_name(f,node,newdir,newname) == -1)
  1301. {
  1302. err = -ENOMEM;
  1303. goto out;
  1304. }
  1305. out:
  1306. pthread_mutex_unlock(&f->lock);
  1307. return err;
  1308. }
  1309. static
  1310. void
  1311. set_stat(struct fuse *f,
  1312. fuse_ino_t nodeid,
  1313. struct stat *stbuf)
  1314. {
  1315. if(!f->conf.use_ino)
  1316. stbuf->st_ino = nodeid;
  1317. if(f->conf.set_mode)
  1318. stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
  1319. if(f->conf.set_uid)
  1320. stbuf->st_uid = f->conf.uid;
  1321. if(f->conf.set_gid)
  1322. stbuf->st_gid = f->conf.gid;
  1323. }
  1324. static
  1325. struct fuse*
  1326. req_fuse(fuse_req_t req)
  1327. {
  1328. return (struct fuse*)fuse_req_userdata(req);
  1329. }
  1330. int
  1331. fuse_fs_getattr(struct fuse_fs *fs,
  1332. const char *path,
  1333. struct stat *buf,
  1334. fuse_timeouts_t *timeout)
  1335. {
  1336. if(fs->op.getattr == NULL)
  1337. return -ENOSYS;
  1338. if(fs->debug)
  1339. fprintf(stderr,"getattr %s\n",path);
  1340. fuse_get_context()->private_data = fs->user_data;
  1341. return fs->op.getattr(path,buf,timeout);
  1342. }
  1343. int
  1344. fuse_fs_fgetattr(struct fuse_fs *fs,
  1345. struct stat *buf,
  1346. fuse_file_info_t *fi,
  1347. fuse_timeouts_t *timeout)
  1348. {
  1349. if(fs->op.fgetattr == NULL)
  1350. return -ENOSYS;
  1351. fuse_get_context()->private_data = fs->user_data;
  1352. if(fs->debug)
  1353. fprintf(stderr,"fgetattr[%llu]\n",(unsigned long long)fi->fh);
  1354. return fs->op.fgetattr(fi,buf,timeout);
  1355. }
  1356. int
  1357. fuse_fs_rename(struct fuse_fs *fs,
  1358. const char *oldpath,
  1359. const char *newpath)
  1360. {
  1361. if(fs->op.rename == NULL)
  1362. return -ENOSYS;
  1363. fuse_get_context()->private_data = fs->user_data;
  1364. return fs->op.rename(oldpath,newpath);
  1365. }
  1366. int
  1367. fuse_fs_prepare_hide(struct fuse_fs *fs_,
  1368. const char *path_,
  1369. uint64_t *fh_)
  1370. {
  1371. if(fs_->op.prepare_hide == NULL)
  1372. return -ENOSYS;
  1373. fuse_get_context()->private_data = fs_->user_data;
  1374. return fs_->op.prepare_hide(path_,fh_);
  1375. }
  1376. int
  1377. fuse_fs_free_hide(struct fuse_fs *fs_,
  1378. uint64_t fh_)
  1379. {
  1380. if(fs_->op.free_hide == NULL)
  1381. return -ENOSYS;
  1382. fuse_get_context()->private_data = fs_->user_data;
  1383. return fs_->op.free_hide(fh_);
  1384. }
  1385. int
  1386. fuse_fs_unlink(struct fuse_fs *fs,
  1387. const char *path)
  1388. {
  1389. if(fs->op.unlink == NULL)
  1390. return -ENOSYS;
  1391. fuse_get_context()->private_data = fs->user_data;
  1392. if(fs->debug)
  1393. fprintf(stderr,"unlink %s\n",path);
  1394. return fs->op.unlink(path);
  1395. }
  1396. int
  1397. fuse_fs_rmdir(struct fuse_fs *fs,
  1398. const char *path)
  1399. {
  1400. if(fs->op.rmdir == NULL)
  1401. return -ENOSYS;
  1402. fuse_get_context()->private_data = fs->user_data;
  1403. if(fs->debug)
  1404. fprintf(stderr,"rmdir %s\n",path);
  1405. return fs->op.rmdir(path);
  1406. }
  1407. int
  1408. fuse_fs_symlink(struct fuse_fs *fs_,
  1409. const char *linkname_,
  1410. const char *path_,
  1411. struct stat *st_,
  1412. fuse_timeouts_t *timeouts_)
  1413. {
  1414. if(fs_->op.symlink == NULL)
  1415. return -ENOSYS;
  1416. if(fs_->debug)
  1417. fprintf(stderr,"symlink %s %s\n",linkname_,path_);
  1418. fuse_get_context()->private_data = fs_->user_data;
  1419. return fs_->op.symlink(linkname_,path_,st_,timeouts_);
  1420. }
  1421. int
  1422. fuse_fs_link(struct fuse_fs *fs,
  1423. const char *oldpath,
  1424. const char *newpath,
  1425. struct stat *st_,
  1426. fuse_timeouts_t *timeouts_)
  1427. {
  1428. if(fs->op.link == NULL)
  1429. return -ENOSYS;
  1430. fuse_get_context()->private_data = fs->user_data;
  1431. if(fs->debug)
  1432. fprintf(stderr,"link %s %s\n",oldpath,newpath);
  1433. return fs->op.link(oldpath,newpath,st_,timeouts_);
  1434. }
  1435. int
  1436. fuse_fs_release(struct fuse_fs *fs,
  1437. fuse_file_info_t *fi)
  1438. {
  1439. if(fs->op.release == NULL)
  1440. return -ENOSYS;
  1441. fuse_get_context()->private_data = fs->user_data;
  1442. if(fs->debug)
  1443. fprintf(stderr,"release%s[%llu] flags: 0x%x\n",
  1444. fi->flush ? "+flush" : "",
  1445. (unsigned long long)fi->fh,fi->flags);
  1446. return fs->op.release(fi);
  1447. }
  1448. int
  1449. fuse_fs_opendir(struct fuse_fs *fs,
  1450. const char *path,
  1451. fuse_file_info_t *fi)
  1452. {
  1453. int err;
  1454. if(fs->op.opendir == NULL)
  1455. return 0;
  1456. fuse_get_context()->private_data = fs->user_data;
  1457. if(fs->debug)
  1458. fprintf(stderr,"opendir flags: 0x%x %s\n",fi->flags,path);
  1459. err = fs->op.opendir(path,fi);
  1460. if(fs->debug && !err)
  1461. fprintf(stderr," opendir[%lli] flags: 0x%x %s\n",
  1462. (unsigned long long)fi->fh,fi->flags,path);
  1463. return err;
  1464. }
  1465. int
  1466. fuse_fs_open(struct fuse_fs *fs,
  1467. const char *path,
  1468. fuse_file_info_t *fi)
  1469. {
  1470. int err;
  1471. if(fs->op.open == NULL)
  1472. return 0;
  1473. fuse_get_context()->private_data = fs->user_data;
  1474. if(fs->debug)
  1475. fprintf(stderr,"open flags: 0x%x %s\n",fi->flags,path);
  1476. err = fs->op.open(path,fi);
  1477. if(fs->debug && !err)
  1478. fprintf(stderr," open[%lli] flags: 0x%x %s\n",
  1479. (unsigned long long)fi->fh,fi->flags,path);
  1480. return err;
  1481. }
  1482. static
  1483. void
  1484. fuse_free_buf(struct fuse_bufvec *buf)
  1485. {
  1486. if(buf != NULL)
  1487. {
  1488. size_t i;
  1489. for(i = 0; i < buf->count; i++)
  1490. free(buf->buf[i].mem);
  1491. free(buf);
  1492. }
  1493. }
  1494. int
  1495. fuse_fs_read_buf(struct fuse_fs *fs,
  1496. struct fuse_bufvec **bufp,
  1497. size_t size,
  1498. off_t off,
  1499. fuse_file_info_t *fi)
  1500. {
  1501. fuse_get_context()->private_data = fs->user_data;
  1502. if(fs->op.read || fs->op.read_buf)
  1503. {
  1504. int res;
  1505. if(fs->debug)
  1506. fprintf(stderr,
  1507. "read[%llu] %zu bytes from %llu flags: 0x%x\n",
  1508. (unsigned long long)fi->fh,
  1509. size,(unsigned long long)off,fi->flags);
  1510. if(fs->op.read_buf)
  1511. {
  1512. res = fs->op.read_buf(fi,bufp,size,off);
  1513. }
  1514. else
  1515. {
  1516. struct fuse_bufvec *buf;
  1517. void *mem;
  1518. buf = malloc(sizeof(struct fuse_bufvec));
  1519. if(buf == NULL)
  1520. return -ENOMEM;
  1521. mem = malloc(size);
  1522. if(mem == NULL)
  1523. {
  1524. free(buf);
  1525. return -ENOMEM;
  1526. }
  1527. *buf = FUSE_BUFVEC_INIT(size);
  1528. buf->buf[0].mem = mem;
  1529. *bufp = buf;
  1530. res = fs->op.read(fi,mem,size,off);
  1531. if(res >= 0)
  1532. buf->buf[0].size = res;
  1533. }
  1534. if(fs->debug && res >= 0)
  1535. fprintf(stderr," read[%llu] %zu bytes from %llu\n",
  1536. (unsigned long long)fi->fh,
  1537. fuse_buf_size(*bufp),
  1538. (unsigned long long)off);
  1539. if(res >= 0 && fuse_buf_size(*bufp) > (int)size)
  1540. fprintf(stderr,"fuse: read too many bytes\n");
  1541. if(res < 0)
  1542. return res;
  1543. return 0;
  1544. }
  1545. else
  1546. {
  1547. return -ENOSYS;
  1548. }
  1549. }
  1550. int
  1551. fuse_fs_read(struct fuse_fs *fs,
  1552. char *mem,
  1553. size_t size,
  1554. off_t off,
  1555. fuse_file_info_t *fi)
  1556. {
  1557. int res;
  1558. struct fuse_bufvec *buf = NULL;
  1559. res = fuse_fs_read_buf(fs,&buf,size,off,fi);
  1560. if(res == 0)
  1561. {
  1562. struct fuse_bufvec dst = FUSE_BUFVEC_INIT(size);
  1563. dst.buf[0].mem = mem;
  1564. res = fuse_buf_copy(&dst,buf,0);
  1565. }
  1566. fuse_free_buf(buf);
  1567. return res;
  1568. }
  1569. int
  1570. fuse_fs_write_buf(struct fuse_fs *fs,
  1571. struct fuse_bufvec *buf,
  1572. off_t off,
  1573. fuse_file_info_t *fi)
  1574. {
  1575. fuse_get_context()->private_data = fs->user_data;
  1576. if(fs->op.write_buf || fs->op.write)
  1577. {
  1578. int res;
  1579. size_t size = fuse_buf_size(buf);
  1580. assert(buf->idx == 0 && buf->off == 0);
  1581. if(fs->debug)
  1582. fprintf(stderr,
  1583. "write%s[%llu] %zu bytes to %llu flags: 0x%x\n",
  1584. fi->writepage ? "page" : "",
  1585. (unsigned long long)fi->fh,
  1586. size,
  1587. (unsigned long long)off,
  1588. fi->flags);
  1589. if(fs->op.write_buf)
  1590. {
  1591. res = fs->op.write_buf(fi,buf,off);
  1592. }
  1593. else
  1594. {
  1595. void *mem = NULL;
  1596. struct fuse_buf *flatbuf;
  1597. struct fuse_bufvec tmp = FUSE_BUFVEC_INIT(size);
  1598. if(buf->count == 1 && !(buf->buf[0].flags & FUSE_BUF_IS_FD))
  1599. {
  1600. flatbuf = &buf->buf[0];
  1601. }
  1602. else
  1603. {
  1604. res = -ENOMEM;
  1605. mem = malloc(size);
  1606. if(mem == NULL)
  1607. goto out;
  1608. tmp.buf[0].mem = mem;
  1609. res = fuse_buf_copy(&tmp,buf,0);
  1610. if(res <= 0)
  1611. goto out_free;
  1612. tmp.buf[0].size = res;
  1613. flatbuf = &tmp.buf[0];
  1614. }
  1615. res = fs->op.write(fi,flatbuf->mem,flatbuf->size,off);
  1616. out_free:
  1617. free(mem);
  1618. }
  1619. out:
  1620. if(fs->debug && res >= 0)
  1621. fprintf(stderr," write%s[%llu] %u bytes to %llu\n",
  1622. fi->writepage ? "page" : "",
  1623. (unsigned long long)fi->fh,res,
  1624. (unsigned long long)off);
  1625. if(res > (int)size)
  1626. fprintf(stderr,"fuse: wrote too many bytes\n");
  1627. return res;
  1628. }
  1629. else
  1630. {
  1631. return -ENOSYS;
  1632. }
  1633. }
  1634. int
  1635. fuse_fs_write(struct fuse_fs *fs,
  1636. const char *mem,
  1637. size_t size,
  1638. off_t off,
  1639. fuse_file_info_t *fi)
  1640. {
  1641. struct fuse_bufvec bufv = FUSE_BUFVEC_INIT(size);
  1642. bufv.buf[0].mem = (void *)mem;
  1643. return fuse_fs_write_buf(fs,&bufv,off,fi);
  1644. }
  1645. int
  1646. fuse_fs_fsync(struct fuse_fs *fs,
  1647. int datasync,
  1648. fuse_file_info_t *fi)
  1649. {
  1650. if(fs->op.fsync == NULL)
  1651. return -ENOSYS;
  1652. fuse_get_context()->private_data = fs->user_data;
  1653. if(fs->debug)
  1654. fprintf(stderr,"fsync[%llu] datasync: %i\n",
  1655. (unsigned long long)fi->fh,datasync);
  1656. return fs->op.fsync(fi,datasync);
  1657. }
  1658. int
  1659. fuse_fs_fsyncdir(struct fuse_fs *fs,
  1660. int datasync,
  1661. fuse_file_info_t *fi)
  1662. {
  1663. if(fs->op.fsyncdir == NULL)
  1664. return -ENOSYS;
  1665. fuse_get_context()->private_data = fs->user_data;
  1666. if(fs->debug)
  1667. fprintf(stderr,"fsyncdir[%llu] datasync: %i\n",
  1668. (unsigned long long)fi->fh,datasync);
  1669. return fs->op.fsyncdir(fi,datasync);
  1670. }
  1671. int
  1672. fuse_fs_flush(struct fuse_fs *fs,
  1673. fuse_file_info_t *fi)
  1674. {
  1675. if(fs->op.flush == NULL)
  1676. return -ENOSYS;
  1677. fuse_get_context()->private_data = fs->user_data;
  1678. if(fs->debug)
  1679. fprintf(stderr,"flush[%llu]\n",
  1680. (unsigned long long)fi->fh);
  1681. return fs->op.flush(fi);
  1682. }
  1683. int
  1684. fuse_fs_statfs(struct fuse_fs *fs,
  1685. const char *path,
  1686. struct statvfs *buf)
  1687. {
  1688. if(fs->op.statfs == NULL)
  1689. {
  1690. buf->f_namemax = 255;
  1691. buf->f_bsize = 512;
  1692. return 0;
  1693. }
  1694. fuse_get_context()->private_data = fs->user_data;
  1695. if(fs->debug)
  1696. fprintf(stderr,"statfs %s\n",path);
  1697. return fs->op.statfs(path,buf);
  1698. }
  1699. int
  1700. fuse_fs_releasedir(struct fuse_fs *fs,
  1701. fuse_file_info_t *fi)
  1702. {
  1703. if(fs->op.releasedir == NULL)
  1704. return 0;
  1705. fuse_get_context()->private_data = fs->user_data;
  1706. if(fs->debug)
  1707. fprintf(stderr,"releasedir[%llu] flags: 0x%x\n",
  1708. (unsigned long long)fi->fh,fi->flags);
  1709. return fs->op.releasedir(fi);
  1710. }
  1711. int
  1712. fuse_fs_readdir(struct fuse_fs *fs,
  1713. fuse_file_info_t *fi,
  1714. fuse_dirents_t *buf)
  1715. {
  1716. if(fs->op.readdir == NULL)
  1717. return -ENOSYS;
  1718. fuse_get_context()->private_data = fs->user_data;
  1719. return fs->op.readdir(fi,buf);
  1720. }
  1721. int
  1722. fuse_fs_readdir_plus(struct fuse_fs *fs_,
  1723. fuse_file_info_t *ffi_,
  1724. fuse_dirents_t *buf_)
  1725. {
  1726. if(fs_->op.readdir_plus == NULL)
  1727. return -ENOSYS;
  1728. fuse_get_context()->private_data = fs_->user_data;
  1729. return fs_->op.readdir_plus(ffi_,buf_);
  1730. }
  1731. int
  1732. fuse_fs_create(struct fuse_fs *fs,
  1733. const char *path,
  1734. mode_t mode,
  1735. fuse_file_info_t *fi)
  1736. {
  1737. int err;
  1738. if(fs->op.create == NULL)
  1739. return -ENOSYS;
  1740. fuse_get_context()->private_data = fs->user_data;
  1741. if(fs->debug)
  1742. fprintf(stderr,
  1743. "create flags: 0x%x %s 0%o umask=0%03o\n",
  1744. fi->flags,path,mode,
  1745. fuse_get_context()->umask);
  1746. err = fs->op.create(path,mode,fi);
  1747. if(fs->debug && !err)
  1748. fprintf(stderr," create[%llu] flags: 0x%x %s\n",
  1749. (unsigned long long)fi->fh,fi->flags,path);
  1750. return err;
  1751. }
  1752. int
  1753. fuse_fs_lock(struct fuse_fs *fs,
  1754. fuse_file_info_t *fi,
  1755. int cmd,
  1756. struct flock *lock)
  1757. {
  1758. if(fs->op.lock == NULL)
  1759. return -ENOSYS;
  1760. fuse_get_context()->private_data = fs->user_data;
  1761. if(fs->debug)
  1762. fprintf(stderr,"lock[%llu] %s %s start: %llu len: %llu pid: %llu\n",
  1763. (unsigned long long)fi->fh,
  1764. (cmd == F_GETLK ? "F_GETLK" :
  1765. (cmd == F_SETLK ? "F_SETLK" :
  1766. (cmd == F_SETLKW ? "F_SETLKW" : "???"))),
  1767. (lock->l_type == F_RDLCK ? "F_RDLCK" :
  1768. (lock->l_type == F_WRLCK ? "F_WRLCK" :
  1769. (lock->l_type == F_UNLCK ? "F_UNLCK" :
  1770. "???"))),
  1771. (unsigned long long)lock->l_start,
  1772. (unsigned long long)lock->l_len,
  1773. (unsigned long long)lock->l_pid);
  1774. return fs->op.lock(fi,cmd,lock);
  1775. }
  1776. int
  1777. fuse_fs_flock(struct fuse_fs *fs,
  1778. fuse_file_info_t *fi,
  1779. int op)
  1780. {
  1781. if(fs->op.flock == NULL)
  1782. return -ENOSYS;
  1783. fuse_get_context()->private_data = fs->user_data;
  1784. if(fs->debug)
  1785. {
  1786. int xop = op & ~LOCK_NB;
  1787. fprintf(stderr,"lock[%llu] %s%s\n",
  1788. (unsigned long long)fi->fh,
  1789. xop == LOCK_SH ? "LOCK_SH" :
  1790. (xop == LOCK_EX ? "LOCK_EX" :
  1791. (xop == LOCK_UN ? "LOCK_UN" : "???")),
  1792. (op & LOCK_NB) ? "|LOCK_NB" : "");
  1793. }
  1794. return fs->op.flock(fi,op);
  1795. }
  1796. int
  1797. fuse_fs_chown(struct fuse_fs *fs,
  1798. const char *path,
  1799. uid_t uid,
  1800. gid_t gid)
  1801. {
  1802. if(fs->op.chown == NULL)
  1803. return -ENOSYS;
  1804. fuse_get_context()->private_data = fs->user_data;
  1805. if(fs->debug)
  1806. fprintf(stderr,"chown %s %lu %lu\n",path,
  1807. (unsigned long)uid,(unsigned long)gid);
  1808. return fs->op.chown(path,uid,gid);
  1809. }
  1810. int
  1811. fuse_fs_fchown(struct fuse_fs *fs_,
  1812. const fuse_file_info_t *ffi_,
  1813. const uid_t uid_,
  1814. const gid_t gid_)
  1815. {
  1816. if(fs_->op.fchown == NULL)
  1817. return -ENOSYS;
  1818. fuse_get_context()->private_data = fs_->user_data;
  1819. return fs_->op.fchown(ffi_,uid_,gid_);
  1820. }
  1821. int
  1822. fuse_fs_truncate(struct fuse_fs *fs,
  1823. const char *path,
  1824. off_t size)
  1825. {
  1826. if(fs->op.truncate == NULL)
  1827. return -ENOSYS;
  1828. fuse_get_context()->private_data = fs->user_data;
  1829. if(fs->debug)
  1830. fprintf(stderr,"truncate %s %llu\n",path,
  1831. (unsigned long long)size);
  1832. return fs->op.truncate(path,size);
  1833. }
  1834. int
  1835. fuse_fs_ftruncate(struct fuse_fs *fs,
  1836. off_t size,
  1837. fuse_file_info_t *fi)
  1838. {
  1839. if(fs->op.ftruncate == NULL)
  1840. return -ENOSYS;
  1841. fuse_get_context()->private_data = fs->user_data;
  1842. if(fs->debug)
  1843. fprintf(stderr,"ftruncate[%llu] %llu\n",
  1844. (unsigned long long)fi->fh,
  1845. (unsigned long long)size);
  1846. return fs->op.ftruncate(fi,size);
  1847. }
  1848. int
  1849. fuse_fs_utimens(struct fuse_fs *fs,
  1850. const char *path,
  1851. const struct timespec tv[2])
  1852. {
  1853. fuse_get_context()->private_data = fs->user_data;
  1854. if(fs->op.utimens)
  1855. {
  1856. if(fs->debug)
  1857. fprintf(stderr,"utimens %s %li.%09lu %li.%09lu\n",
  1858. path,tv[0].tv_sec,tv[0].tv_nsec,
  1859. tv[1].tv_sec,tv[1].tv_nsec);
  1860. return fs->op.utimens(path,tv);
  1861. }
  1862. else if(fs->op.utime)
  1863. {
  1864. struct utimbuf buf;
  1865. if(fs->debug)
  1866. fprintf(stderr,"utime %s %li %li\n",path,
  1867. tv[0].tv_sec,tv[1].tv_sec);
  1868. buf.actime = tv[0].tv_sec;
  1869. buf.modtime = tv[1].tv_sec;
  1870. return fs->op.utime(path,&buf);
  1871. }
  1872. else
  1873. {
  1874. return -ENOSYS;
  1875. }
  1876. }
  1877. int
  1878. fuse_fs_futimens(struct fuse_fs *fs_,
  1879. const fuse_file_info_t *ffi_,
  1880. const struct timespec tv_[2])
  1881. {
  1882. if(fs_->op.futimens == NULL)
  1883. return -ENOSYS;
  1884. fuse_get_context()->private_data = fs_->user_data;
  1885. return fs_->op.futimens(ffi_,tv_);
  1886. }
  1887. int
  1888. fuse_fs_access(struct fuse_fs *fs,
  1889. const char *path,
  1890. int mask)
  1891. {
  1892. if(fs->op.access == NULL)
  1893. return -ENOSYS;
  1894. fuse_get_context()->private_data = fs->user_data;
  1895. if(fs->debug)
  1896. fprintf(stderr,"access %s 0%o\n",path,mask);
  1897. return fs->op.access(path,mask);
  1898. }
  1899. int
  1900. fuse_fs_readlink(struct fuse_fs *fs,
  1901. const char *path,
  1902. char *buf,
  1903. size_t len)
  1904. {
  1905. if(fs->op.readlink == NULL)
  1906. return -ENOSYS;
  1907. fuse_get_context()->private_data = fs->user_data;
  1908. if(fs->debug)
  1909. fprintf(stderr,"readlink %s %lu\n",path,
  1910. (unsigned long)len);
  1911. return fs->op.readlink(path,buf,len);
  1912. }
  1913. int
  1914. fuse_fs_mknod(struct fuse_fs *fs,
  1915. const char *path,
  1916. mode_t mode,
  1917. dev_t rdev)
  1918. {
  1919. if(fs->op.mknod == NULL)
  1920. return -ENOSYS;
  1921. fuse_get_context()->private_data = fs->user_data;
  1922. if(fs->debug)
  1923. fprintf(stderr,"mknod %s 0%o 0x%llx umask=0%03o\n",
  1924. path,mode,(unsigned long long)rdev,
  1925. fuse_get_context()->umask);
  1926. return fs->op.mknod(path,mode,rdev);
  1927. }
  1928. int
  1929. fuse_fs_mkdir(struct fuse_fs *fs,
  1930. const char *path,
  1931. mode_t mode)
  1932. {
  1933. if(fs->op.mkdir == NULL)
  1934. return -ENOSYS;
  1935. fuse_get_context()->private_data = fs->user_data;
  1936. if(fs->debug)
  1937. fprintf(stderr,"mkdir %s 0%o umask=0%03o\n",
  1938. path,mode,fuse_get_context()->umask);
  1939. return fs->op.mkdir(path,mode);
  1940. }
  1941. int
  1942. fuse_fs_setxattr(struct fuse_fs *fs,
  1943. const char *path,
  1944. const char *name,
  1945. const char *value,
  1946. size_t size,
  1947. int flags)
  1948. {
  1949. if(fs->op.setxattr == NULL)
  1950. return -ENOSYS;
  1951. fuse_get_context()->private_data = fs->user_data;
  1952. if(fs->debug)
  1953. fprintf(stderr,"setxattr %s %s %lu 0x%x\n",
  1954. path,name,(unsigned long)size,flags);
  1955. return fs->op.setxattr(path,name,value,size,flags);
  1956. }
  1957. int
  1958. fuse_fs_getxattr(struct fuse_fs *fs,
  1959. const char *path,
  1960. const char *name,
  1961. char *value,
  1962. size_t size)
  1963. {
  1964. if(fs->op.getxattr == NULL)
  1965. return -ENOSYS;
  1966. fuse_get_context()->private_data = fs->user_data;
  1967. if(fs->debug)
  1968. fprintf(stderr,"getxattr %s %s %lu\n",
  1969. path,name,(unsigned long)size);
  1970. return fs->op.getxattr(path,name,value,size);
  1971. }
  1972. int
  1973. fuse_fs_listxattr(struct fuse_fs *fs,
  1974. const char *path,
  1975. char *list,
  1976. size_t size)
  1977. {
  1978. if(fs->op.listxattr == NULL)
  1979. return -ENOSYS;
  1980. fuse_get_context()->private_data = fs->user_data;
  1981. if(fs->debug)
  1982. fprintf(stderr,"listxattr %s %lu\n",
  1983. path,(unsigned long)size);
  1984. return fs->op.listxattr(path,list,size);
  1985. }
  1986. int
  1987. fuse_fs_bmap(struct fuse_fs *fs,
  1988. const char *path,
  1989. size_t blocksize,
  1990. uint64_t *idx)
  1991. {
  1992. if(fs->op.bmap == NULL)
  1993. return -ENOSYS;
  1994. fuse_get_context()->private_data = fs->user_data;
  1995. if(fs->debug)
  1996. fprintf(stderr,"bmap %s blocksize: %lu index: %llu\n",
  1997. path,(unsigned long)blocksize,
  1998. (unsigned long long)*idx);
  1999. return fs->op.bmap(path,blocksize,idx);
  2000. }
  2001. int
  2002. fuse_fs_removexattr(struct fuse_fs *fs,
  2003. const char *path,
  2004. const char *name)
  2005. {
  2006. if(fs->op.removexattr == NULL)
  2007. return -ENOSYS;
  2008. fuse_get_context()->private_data = fs->user_data;
  2009. if(fs->debug)
  2010. fprintf(stderr,"removexattr %s %s\n",path,name);
  2011. return fs->op.removexattr(path,name);
  2012. }
  2013. int
  2014. fuse_fs_ioctl(struct fuse_fs *fs,
  2015. unsigned long cmd,
  2016. void *arg,
  2017. fuse_file_info_t *fi,
  2018. unsigned int flags,
  2019. void *data,
  2020. uint32_t *out_size)
  2021. {
  2022. if(fs->op.ioctl == NULL)
  2023. return -ENOSYS;
  2024. fuse_get_context()->private_data = fs->user_data;
  2025. if(fs->debug)
  2026. fprintf(stderr,"ioctl[%llu] 0x%lx flags: 0x%x\n",
  2027. (unsigned long long)fi->fh,cmd,flags);
  2028. return fs->op.ioctl(fi,cmd,arg,flags,data,out_size);
  2029. }
  2030. int
  2031. fuse_fs_poll(struct fuse_fs *fs,
  2032. fuse_file_info_t *fi,
  2033. fuse_pollhandle_t *ph,
  2034. unsigned *reventsp)
  2035. {
  2036. int res;
  2037. if(fs->op.poll == NULL)
  2038. return -ENOSYS;
  2039. fuse_get_context()->private_data = fs->user_data;
  2040. if(fs->debug)
  2041. fprintf(stderr,"poll[%llu] ph: %p\n",
  2042. (unsigned long long)fi->fh,ph);
  2043. res = fs->op.poll(fi,ph,reventsp);
  2044. if(fs->debug && !res)
  2045. fprintf(stderr," poll[%llu] revents: 0x%x\n",
  2046. (unsigned long long)fi->fh,*reventsp);
  2047. return res;
  2048. }
  2049. int
  2050. fuse_fs_fallocate(struct fuse_fs *fs,
  2051. int mode,
  2052. off_t offset,
  2053. off_t length,
  2054. fuse_file_info_t *fi)
  2055. {
  2056. if(fs->op.fallocate == NULL)
  2057. return -ENOSYS;
  2058. fuse_get_context()->private_data = fs->user_data;
  2059. if(fs->debug)
  2060. fprintf(stderr,"fallocate mode %x,offset: %llu,length: %llu\n",
  2061. mode,
  2062. (unsigned long long)offset,
  2063. (unsigned long long)length);
  2064. return fs->op.fallocate(fi,mode,offset,length);
  2065. }
  2066. ssize_t
  2067. fuse_fs_copy_file_range(struct fuse_fs *fs_,
  2068. fuse_file_info_t *ffi_in_,
  2069. off_t off_in_,
  2070. fuse_file_info_t *ffi_out_,
  2071. off_t off_out_,
  2072. size_t len_,
  2073. int flags_)
  2074. {
  2075. if(fs_->op.copy_file_range == NULL)
  2076. return -ENOSYS;
  2077. fuse_get_context()->private_data = fs_->user_data;
  2078. return fs_->op.copy_file_range(ffi_in_,
  2079. off_in_,
  2080. ffi_out_,
  2081. off_out_,
  2082. len_,
  2083. flags_);
  2084. }
  2085. static
  2086. int
  2087. node_open(const struct node *node_)
  2088. {
  2089. return ((node_ != NULL) && (node_->open_count > 0));
  2090. }
  2091. #ifndef CLOCK_MONOTONIC
  2092. #define CLOCK_MONOTONIC CLOCK_REALTIME
  2093. #endif
  2094. static
  2095. void
  2096. curr_time(struct timespec *now)
  2097. {
  2098. static clockid_t clockid = CLOCK_MONOTONIC;
  2099. int res = clock_gettime(clockid,now);
  2100. if(res == -1 && errno == EINVAL)
  2101. {
  2102. clockid = CLOCK_REALTIME;
  2103. res = clock_gettime(clockid,now);
  2104. }
  2105. if(res == -1)
  2106. {
  2107. perror("fuse: clock_gettime");
  2108. abort();
  2109. }
  2110. }
  2111. static
  2112. void
  2113. update_stat(struct node *node_,
  2114. const struct stat *stnew_)
  2115. {
  2116. if((node_->stat_cache_valid) &&
  2117. ((node_->mtim.tv_sec != stnew_->st_mtim.tv_sec) ||
  2118. (node_->mtim.tv_nsec != stnew_->st_mtim.tv_nsec) ||
  2119. (node_->ino != stnew_->st_ino) ||
  2120. (node_->size != stnew_->st_size)))
  2121. node_->stat_cache_valid = 0;
  2122. node_->ino = stnew_->st_ino;
  2123. node_->size = stnew_->st_size;
  2124. node_->mtim = stnew_->st_mtim;
  2125. }
  2126. static
  2127. int
  2128. set_path_info(struct fuse *f,
  2129. fuse_ino_t nodeid,
  2130. const char *name,
  2131. struct fuse_entry_param *e)
  2132. {
  2133. struct node *node;
  2134. node = find_node(f,nodeid,name);
  2135. if(node == NULL)
  2136. return -ENOMEM;
  2137. e->ino = node->nodeid;
  2138. e->generation = node->generation;
  2139. pthread_mutex_lock(&f->lock);
  2140. update_stat(node,&e->attr);
  2141. pthread_mutex_unlock(&f->lock);
  2142. set_stat(f,e->ino,&e->attr);
  2143. if(f->conf.debug)
  2144. fprintf(stderr,
  2145. " NODEID: %llu\n"
  2146. " GEN: %llu\n",
  2147. (unsigned long long)e->ino,
  2148. (unsigned long long)e->generation);
  2149. return 0;
  2150. }
  2151. static
  2152. int
  2153. lookup_path(struct fuse *f,
  2154. fuse_ino_t nodeid,
  2155. const char *name,
  2156. const char *path,
  2157. struct fuse_entry_param *e,
  2158. fuse_file_info_t *fi)
  2159. {
  2160. int rv;
  2161. memset(e,0,sizeof(struct fuse_entry_param));
  2162. rv = ((fi == NULL) ?
  2163. fuse_fs_getattr(f->fs,path,&e->attr,&e->timeout) :
  2164. fuse_fs_fgetattr(f->fs,&e->attr,fi,&e->timeout));
  2165. if(rv)
  2166. return rv;
  2167. return set_path_info(f,nodeid,name,e);
  2168. }
  2169. static
  2170. struct fuse_context_i*
  2171. fuse_get_context_internal(void)
  2172. {
  2173. struct fuse_context_i *c;
  2174. c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
  2175. if(c == NULL)
  2176. {
  2177. c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
  2178. if(c == NULL)
  2179. {
  2180. /* This is hard to deal with properly,so just
  2181. abort. If memory is so low that the
  2182. context cannot be allocated,there's not
  2183. much hope for the filesystem anyway */
  2184. fprintf(stderr,"fuse: failed to allocate thread specific data\n");
  2185. abort();
  2186. }
  2187. pthread_setspecific(fuse_context_key,c);
  2188. }
  2189. return c;
  2190. }
  2191. static
  2192. void
  2193. fuse_freecontext(void *data)
  2194. {
  2195. free(data);
  2196. }
  2197. static
  2198. int
  2199. fuse_create_context_key(void)
  2200. {
  2201. int err = 0;
  2202. pthread_mutex_lock(&fuse_context_lock);
  2203. if(!fuse_context_ref)
  2204. {
  2205. err = pthread_key_create(&fuse_context_key,fuse_freecontext);
  2206. if(err)
  2207. {
  2208. fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
  2209. strerror(err));
  2210. pthread_mutex_unlock(&fuse_context_lock);
  2211. return -1;
  2212. }
  2213. }
  2214. fuse_context_ref++;
  2215. pthread_mutex_unlock(&fuse_context_lock);
  2216. return 0;
  2217. }
  2218. static
  2219. void
  2220. fuse_delete_context_key(void)
  2221. {
  2222. pthread_mutex_lock(&fuse_context_lock);
  2223. fuse_context_ref--;
  2224. if(!fuse_context_ref)
  2225. {
  2226. free(pthread_getspecific(fuse_context_key));
  2227. pthread_key_delete(fuse_context_key);
  2228. }
  2229. pthread_mutex_unlock(&fuse_context_lock);
  2230. }
  2231. static
  2232. struct fuse*
  2233. req_fuse_prepare(fuse_req_t req)
  2234. {
  2235. struct fuse_context_i *c = fuse_get_context_internal();
  2236. const struct fuse_ctx *ctx = fuse_req_ctx(req);
  2237. c->req = req;
  2238. c->ctx.fuse = req_fuse(req);
  2239. c->ctx.uid = ctx->uid;
  2240. c->ctx.gid = ctx->gid;
  2241. c->ctx.pid = ctx->pid;
  2242. c->ctx.umask = ctx->umask;
  2243. return c->ctx.fuse;
  2244. }
  2245. static
  2246. inline
  2247. void
  2248. reply_err(fuse_req_t req,
  2249. int err)
  2250. {
  2251. /* fuse_reply_err() uses non-negated errno values */
  2252. fuse_reply_err(req,-err);
  2253. }
  2254. static
  2255. void
  2256. reply_entry(fuse_req_t req,
  2257. const struct fuse_entry_param *e,
  2258. int err)
  2259. {
  2260. if(!err)
  2261. {
  2262. struct fuse *f = req_fuse(req);
  2263. if(fuse_reply_entry(req,e) == -ENOENT)
  2264. {
  2265. /* Skip forget for negative result */
  2266. if(e->ino != 0)
  2267. forget_node(f,e->ino,1);
  2268. }
  2269. }
  2270. else
  2271. {
  2272. reply_err(req,err);
  2273. }
  2274. }
  2275. void
  2276. fuse_fs_init(struct fuse_fs *fs,
  2277. struct fuse_conn_info *conn)
  2278. {
  2279. fuse_get_context()->private_data = fs->user_data;
  2280. if(!fs->op.write_buf)
  2281. conn->want &= ~FUSE_CAP_SPLICE_READ;
  2282. if(!fs->op.lock)
  2283. conn->want &= ~FUSE_CAP_POSIX_LOCKS;
  2284. if(!fs->op.flock)
  2285. conn->want &= ~FUSE_CAP_FLOCK_LOCKS;
  2286. if(fs->op.init)
  2287. fs->user_data = fs->op.init(conn);
  2288. }
  2289. static
  2290. void
  2291. fuse_lib_init(void *data,
  2292. struct fuse_conn_info *conn)
  2293. {
  2294. struct fuse *f = (struct fuse *)data;
  2295. struct fuse_context_i *c = fuse_get_context_internal();
  2296. memset(c,0,sizeof(*c));
  2297. c->ctx.fuse = f;
  2298. conn->want |= FUSE_CAP_EXPORT_SUPPORT;
  2299. fuse_fs_init(f->fs,conn);
  2300. }
  2301. void
  2302. fuse_fs_destroy(struct fuse_fs *fs)
  2303. {
  2304. fuse_get_context()->private_data = fs->user_data;
  2305. if(fs->op.destroy)
  2306. fs->op.destroy(fs->user_data);
  2307. free(fs);
  2308. }
  2309. static
  2310. void
  2311. fuse_lib_destroy(void *data)
  2312. {
  2313. struct fuse *f = (struct fuse *)data;
  2314. struct fuse_context_i *c = fuse_get_context_internal();
  2315. memset(c,0,sizeof(*c));
  2316. c->ctx.fuse = f;
  2317. fuse_fs_destroy(f->fs);
  2318. f->fs = NULL;
  2319. }
  2320. static
  2321. void
  2322. fuse_lib_lookup(fuse_req_t req,
  2323. fuse_ino_t parent,
  2324. const char *name)
  2325. {
  2326. struct fuse *f = req_fuse_prepare(req);
  2327. struct fuse_entry_param e;
  2328. char *path;
  2329. int err;
  2330. struct node *dot = NULL;
  2331. if(name[0] == '.')
  2332. {
  2333. int len = strlen(name);
  2334. if(len == 1 || (name[1] == '.' && len == 2))
  2335. {
  2336. pthread_mutex_lock(&f->lock);
  2337. if(len == 1)
  2338. {
  2339. if(f->conf.debug)
  2340. fprintf(stderr,"LOOKUP-DOT\n");
  2341. dot = get_node_nocheck(f,parent);
  2342. if(dot == NULL)
  2343. {
  2344. pthread_mutex_unlock(&f->lock);
  2345. reply_entry(req,&e,-ESTALE);
  2346. return;
  2347. }
  2348. dot->refctr++;
  2349. }
  2350. else
  2351. {
  2352. if(f->conf.debug)
  2353. fprintf(stderr,"LOOKUP-DOTDOT\n");
  2354. parent = get_node(f,parent)->parent->nodeid;
  2355. }
  2356. pthread_mutex_unlock(&f->lock);
  2357. name = NULL;
  2358. }
  2359. }
  2360. err = get_path_name(f,parent,name,&path);
  2361. if(!err)
  2362. {
  2363. if(f->conf.debug)
  2364. fprintf(stderr,"LOOKUP %s\n",path);
  2365. err = lookup_path(f,parent,name,path,&e,NULL);
  2366. if(err == -ENOENT)
  2367. {
  2368. e.ino = 0;
  2369. err = 0;
  2370. }
  2371. free_path(f,parent,path);
  2372. }
  2373. if(dot)
  2374. {
  2375. pthread_mutex_lock(&f->lock);
  2376. unref_node(f,dot);
  2377. pthread_mutex_unlock(&f->lock);
  2378. }
  2379. reply_entry(req,&e,err);
  2380. }
  2381. static
  2382. void
  2383. do_forget(struct fuse *f,
  2384. const fuse_ino_t ino,
  2385. const uint64_t nlookup)
  2386. {
  2387. if(f->conf.debug)
  2388. fprintf(stderr,
  2389. "FORGET %llu/%llu\n",
  2390. (unsigned long long)ino,
  2391. (unsigned long long)nlookup);
  2392. forget_node(f,ino,nlookup);
  2393. }
  2394. static
  2395. void
  2396. fuse_lib_forget(fuse_req_t req,
  2397. const fuse_ino_t ino,
  2398. const uint64_t nlookup)
  2399. {
  2400. do_forget(req_fuse(req),ino,nlookup);
  2401. fuse_reply_none(req);
  2402. }
  2403. static
  2404. void
  2405. fuse_lib_forget_multi(fuse_req_t req,
  2406. size_t count,
  2407. struct fuse_forget_data *forgets)
  2408. {
  2409. struct fuse *f = req_fuse(req);
  2410. size_t i;
  2411. for(i = 0; i < count; i++)
  2412. do_forget(f,forgets[i].ino,forgets[i].nlookup);
  2413. fuse_reply_none(req);
  2414. }
  2415. static
  2416. void
  2417. fuse_lib_getattr(fuse_req_t req,
  2418. fuse_ino_t ino,
  2419. fuse_file_info_t *fi)
  2420. {
  2421. int err;
  2422. char *path;
  2423. struct fuse *f;
  2424. struct stat buf;
  2425. struct node *node;
  2426. fuse_timeouts_t timeout;
  2427. fuse_file_info_t ffi = {0};
  2428. f = req_fuse_prepare(req);
  2429. if(fi == NULL)
  2430. {
  2431. pthread_mutex_lock(&f->lock);
  2432. node = get_node(f,ino);
  2433. if(node->is_hidden)
  2434. {
  2435. fi = &ffi;
  2436. fi->fh = node->hidden_fh;
  2437. }
  2438. pthread_mutex_unlock(&f->lock);
  2439. }
  2440. memset(&buf,0,sizeof(buf));
  2441. err = 0;
  2442. path = NULL;
  2443. if((fi == NULL) || (f->fs->op.fgetattr == NULL))
  2444. err = get_path(f,ino,&path);
  2445. if(!err)
  2446. {
  2447. err = ((fi == NULL) ?
  2448. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  2449. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  2450. free_path(f,ino,path);
  2451. }
  2452. if(!err)
  2453. {
  2454. pthread_mutex_lock(&f->lock);
  2455. node = get_node(f,ino);
  2456. update_stat(node,&buf);
  2457. pthread_mutex_unlock(&f->lock);
  2458. set_stat(f,ino,&buf);
  2459. fuse_reply_attr(req,&buf,timeout.attr);
  2460. }
  2461. else
  2462. {
  2463. reply_err(req,err);
  2464. }
  2465. }
  2466. int
  2467. fuse_fs_chmod(struct fuse_fs *fs,
  2468. const char *path,
  2469. mode_t mode)
  2470. {
  2471. if(fs->op.chmod == NULL)
  2472. return -ENOSYS;
  2473. fuse_get_context()->private_data = fs->user_data;
  2474. return fs->op.chmod(path,mode);
  2475. }
  2476. int
  2477. fuse_fs_fchmod(struct fuse_fs *fs_,
  2478. const fuse_file_info_t *ffi_,
  2479. const mode_t mode_)
  2480. {
  2481. if(fs_->op.fchmod == NULL)
  2482. return -ENOSYS;
  2483. fuse_get_context()->private_data = fs_->user_data;
  2484. return fs_->op.fchmod(ffi_,mode_);
  2485. }
  2486. static
  2487. void
  2488. fuse_lib_setattr(fuse_req_t req,
  2489. fuse_ino_t ino,
  2490. struct stat *attr,
  2491. int valid,
  2492. fuse_file_info_t *fi)
  2493. {
  2494. struct fuse *f = req_fuse_prepare(req);
  2495. struct stat buf;
  2496. char *path;
  2497. int err;
  2498. struct node *node;
  2499. fuse_timeouts_t timeout;
  2500. fuse_file_info_t ffi = {0};
  2501. if(fi == NULL)
  2502. {
  2503. pthread_mutex_lock(&f->lock);
  2504. node = get_node(f,ino);
  2505. if(node->is_hidden)
  2506. {
  2507. fi = &ffi;
  2508. fi->fh = node->hidden_fh;
  2509. }
  2510. pthread_mutex_unlock(&f->lock);
  2511. }
  2512. memset(&buf,0,sizeof(buf));
  2513. err = 0;
  2514. path = NULL;
  2515. if(fi == NULL)
  2516. err = get_path(f,ino,&path);
  2517. if(!err)
  2518. {
  2519. err = 0;
  2520. if(!err && (valid & FATTR_MODE))
  2521. err = ((fi == NULL) ?
  2522. fuse_fs_chmod(f->fs,path,attr->st_mode) :
  2523. fuse_fs_fchmod(f->fs,fi,attr->st_mode));
  2524. if(!err && (valid & (FATTR_UID | FATTR_GID)))
  2525. {
  2526. uid_t uid = ((valid & FATTR_UID) ? attr->st_uid : (uid_t)-1);
  2527. gid_t gid = ((valid & FATTR_GID) ? attr->st_gid : (gid_t)-1);
  2528. err = ((fi == NULL) ?
  2529. fuse_fs_chown(f->fs,path,uid,gid) :
  2530. fuse_fs_fchown(f->fs,fi,uid,gid));
  2531. }
  2532. if(!err && (valid & FATTR_SIZE))
  2533. err = ((fi == NULL) ?
  2534. fuse_fs_truncate(f->fs,path,attr->st_size) :
  2535. fuse_fs_ftruncate(f->fs,attr->st_size,fi));
  2536. #ifdef HAVE_UTIMENSAT
  2537. if(!err && (valid & (FATTR_ATIME | FATTR_MTIME)))
  2538. {
  2539. struct timespec tv[2];
  2540. tv[0].tv_sec = 0;
  2541. tv[1].tv_sec = 0;
  2542. tv[0].tv_nsec = UTIME_OMIT;
  2543. tv[1].tv_nsec = UTIME_OMIT;
  2544. if(valid & FATTR_ATIME_NOW)
  2545. tv[0].tv_nsec = UTIME_NOW;
  2546. else if(valid & FATTR_ATIME)
  2547. tv[0] = attr->st_atim;
  2548. if(valid & FATTR_MTIME_NOW)
  2549. tv[1].tv_nsec = UTIME_NOW;
  2550. else if(valid & FATTR_MTIME)
  2551. tv[1] = attr->st_mtim;
  2552. err = ((fi == NULL) ?
  2553. fuse_fs_utimens(f->fs,path,tv) :
  2554. fuse_fs_futimens(f->fs,fi,tv));
  2555. }
  2556. else
  2557. #endif
  2558. if(!err && ((valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
  2559. {
  2560. struct timespec tv[2];
  2561. tv[0].tv_sec = attr->st_atime;
  2562. tv[0].tv_nsec = ST_ATIM_NSEC(attr);
  2563. tv[1].tv_sec = attr->st_mtime;
  2564. tv[1].tv_nsec = ST_MTIM_NSEC(attr);
  2565. err = ((fi == NULL) ?
  2566. fuse_fs_utimens(f->fs,path,tv) :
  2567. fuse_fs_futimens(f->fs,fi,tv));
  2568. }
  2569. if(!err)
  2570. err = ((fi == NULL) ?
  2571. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  2572. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  2573. free_path(f,ino,path);
  2574. }
  2575. if(!err)
  2576. {
  2577. pthread_mutex_lock(&f->lock);
  2578. update_stat(get_node(f,ino),&buf);
  2579. pthread_mutex_unlock(&f->lock);
  2580. set_stat(f,ino,&buf);
  2581. fuse_reply_attr(req,&buf,timeout.attr);
  2582. }
  2583. else
  2584. {
  2585. reply_err(req,err);
  2586. }
  2587. }
  2588. static
  2589. void
  2590. fuse_lib_access(fuse_req_t req,
  2591. fuse_ino_t ino,
  2592. int mask)
  2593. {
  2594. struct fuse *f = req_fuse_prepare(req);
  2595. char *path;
  2596. int err;
  2597. err = get_path(f,ino,&path);
  2598. if(!err)
  2599. {
  2600. err = fuse_fs_access(f->fs,path,mask);
  2601. free_path(f,ino,path);
  2602. }
  2603. reply_err(req,err);
  2604. }
  2605. static
  2606. void
  2607. fuse_lib_readlink(fuse_req_t req,
  2608. fuse_ino_t ino)
  2609. {
  2610. struct fuse *f = req_fuse_prepare(req);
  2611. char linkname[PATH_MAX + 1];
  2612. char *path;
  2613. int err;
  2614. err = get_path(f,ino,&path);
  2615. if(!err)
  2616. {
  2617. err = fuse_fs_readlink(f->fs,path,linkname,sizeof(linkname));
  2618. free_path(f,ino,path);
  2619. }
  2620. if(!err)
  2621. {
  2622. linkname[PATH_MAX] = '\0';
  2623. fuse_reply_readlink(req,linkname);
  2624. }
  2625. else
  2626. {
  2627. reply_err(req,err);
  2628. }
  2629. }
  2630. static
  2631. void
  2632. fuse_lib_mknod(fuse_req_t req,
  2633. fuse_ino_t parent,
  2634. const char *name,
  2635. mode_t mode,
  2636. dev_t rdev)
  2637. {
  2638. struct fuse *f = req_fuse_prepare(req);
  2639. struct fuse_entry_param e;
  2640. char *path;
  2641. int err;
  2642. err = get_path_name(f,parent,name,&path);
  2643. if(!err)
  2644. {
  2645. err = -ENOSYS;
  2646. if(S_ISREG(mode))
  2647. {
  2648. fuse_file_info_t fi;
  2649. memset(&fi,0,sizeof(fi));
  2650. fi.flags = O_CREAT | O_EXCL | O_WRONLY;
  2651. err = fuse_fs_create(f->fs,path,mode,&fi);
  2652. if(!err)
  2653. {
  2654. err = lookup_path(f,parent,name,path,&e,
  2655. &fi);
  2656. fuse_fs_release(f->fs,&fi);
  2657. }
  2658. }
  2659. if(err == -ENOSYS)
  2660. {
  2661. err = fuse_fs_mknod(f->fs,path,mode,rdev);
  2662. if(!err)
  2663. err = lookup_path(f,parent,name,path,&e,NULL);
  2664. }
  2665. free_path(f,parent,path);
  2666. }
  2667. reply_entry(req,&e,err);
  2668. }
  2669. static
  2670. void
  2671. fuse_lib_mkdir(fuse_req_t req,
  2672. fuse_ino_t parent,
  2673. const char *name,
  2674. mode_t mode)
  2675. {
  2676. struct fuse *f = req_fuse_prepare(req);
  2677. struct fuse_entry_param e;
  2678. char *path;
  2679. int err;
  2680. err = get_path_name(f,parent,name,&path);
  2681. if(!err)
  2682. {
  2683. err = fuse_fs_mkdir(f->fs,path,mode);
  2684. if(!err)
  2685. err = lookup_path(f,parent,name,path,&e,NULL);
  2686. free_path(f,parent,path);
  2687. }
  2688. reply_entry(req,&e,err);
  2689. }
  2690. static
  2691. void
  2692. fuse_lib_unlink(fuse_req_t req,
  2693. fuse_ino_t parent,
  2694. const char *name)
  2695. {
  2696. int err;
  2697. char *path;
  2698. struct fuse *f;
  2699. struct node *wnode;
  2700. f = req_fuse_prepare(req);
  2701. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2702. if(!err)
  2703. {
  2704. pthread_mutex_lock(&f->lock);
  2705. if(node_open(wnode))
  2706. {
  2707. err = fuse_fs_prepare_hide(f->fs,path,&wnode->hidden_fh);
  2708. if(!err)
  2709. wnode->is_hidden = 1;
  2710. }
  2711. pthread_mutex_unlock(&f->lock);
  2712. err = fuse_fs_unlink(f->fs,path);
  2713. if(!err)
  2714. remove_node(f,parent,name);
  2715. free_path_wrlock(f,parent,wnode,path);
  2716. }
  2717. reply_err(req,err);
  2718. }
  2719. static
  2720. void
  2721. fuse_lib_rmdir(fuse_req_t req,
  2722. fuse_ino_t parent,
  2723. const char *name)
  2724. {
  2725. struct fuse *f = req_fuse_prepare(req);
  2726. struct node *wnode;
  2727. char *path;
  2728. int err;
  2729. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2730. if(!err)
  2731. {
  2732. err = fuse_fs_rmdir(f->fs,path);
  2733. if(!err)
  2734. remove_node(f,parent,name);
  2735. free_path_wrlock(f,parent,wnode,path);
  2736. }
  2737. reply_err(req,err);
  2738. }
  2739. static
  2740. void
  2741. fuse_lib_symlink(fuse_req_t req_,
  2742. const char *linkname_,
  2743. fuse_ino_t parent_,
  2744. const char *name_)
  2745. {
  2746. int rv;
  2747. char *path;
  2748. struct fuse *f;
  2749. struct fuse_entry_param e = {0};
  2750. f = req_fuse_prepare(req_);
  2751. rv = get_path_name(f,parent_,name_,&path);
  2752. if(rv == 0)
  2753. {
  2754. rv = fuse_fs_symlink(f->fs,linkname_,path,&e.attr,&e.timeout);
  2755. if(rv == 0)
  2756. rv = set_path_info(f,parent_,name_,&e);
  2757. free_path(f,parent_,path);
  2758. }
  2759. reply_entry(req_,&e,rv);
  2760. }
  2761. static
  2762. void
  2763. fuse_lib_rename(fuse_req_t req,
  2764. fuse_ino_t olddir,
  2765. const char *oldname,
  2766. fuse_ino_t newdir,
  2767. const char *newname)
  2768. {
  2769. int err;
  2770. struct fuse *f;
  2771. char *oldpath;
  2772. char *newpath;
  2773. struct node *wnode1;
  2774. struct node *wnode2;
  2775. f = req_fuse_prepare(req);
  2776. err = get_path2(f,olddir,oldname,newdir,newname,
  2777. &oldpath,&newpath,&wnode1,&wnode2);
  2778. if(!err)
  2779. {
  2780. pthread_mutex_lock(&f->lock);
  2781. if(node_open(wnode2))
  2782. {
  2783. err = fuse_fs_prepare_hide(f->fs,newpath,&wnode2->hidden_fh);
  2784. if(!err)
  2785. wnode2->is_hidden = 1;
  2786. }
  2787. pthread_mutex_unlock(&f->lock);
  2788. err = fuse_fs_rename(f->fs,oldpath,newpath);
  2789. if(!err)
  2790. err = rename_node(f,olddir,oldname,newdir,newname);
  2791. free_path2(f,olddir,newdir,wnode1,wnode2,oldpath,newpath);
  2792. }
  2793. reply_err(req,err);
  2794. }
  2795. static
  2796. void
  2797. fuse_lib_link(fuse_req_t req,
  2798. fuse_ino_t ino,
  2799. fuse_ino_t newparent,
  2800. const char *newname)
  2801. {
  2802. int rv;
  2803. char *oldpath;
  2804. char *newpath;
  2805. struct fuse *f;
  2806. struct fuse_entry_param e = {0};
  2807. f = req_fuse_prepare(req);
  2808. rv = get_path2(f,ino,NULL,newparent,newname,
  2809. &oldpath,&newpath,NULL,NULL);
  2810. if(!rv)
  2811. {
  2812. rv = fuse_fs_link(f->fs,oldpath,newpath,&e.attr,&e.timeout);
  2813. if(rv == 0)
  2814. rv = set_path_info(f,newparent,newname,&e);
  2815. free_path2(f,ino,newparent,NULL,NULL,oldpath,newpath);
  2816. }
  2817. reply_entry(req,&e,rv);
  2818. }
  2819. static
  2820. void
  2821. fuse_do_release(struct fuse *f,
  2822. fuse_ino_t ino,
  2823. fuse_file_info_t *fi)
  2824. {
  2825. struct node *node;
  2826. uint64_t fh;
  2827. int was_hidden;
  2828. fh = 0;
  2829. fuse_fs_release(f->fs,fi);
  2830. pthread_mutex_lock(&f->lock);
  2831. node = get_node(f,ino);
  2832. assert(node->open_count > 0);
  2833. node->open_count--;
  2834. was_hidden = 0;
  2835. if(node->is_hidden && (node->open_count == 0))
  2836. {
  2837. was_hidden = 1;
  2838. node->is_hidden = 0;
  2839. fh = node->hidden_fh;
  2840. }
  2841. pthread_mutex_unlock(&f->lock);
  2842. if(was_hidden)
  2843. fuse_fs_free_hide(f->fs,fh);
  2844. }
  2845. static
  2846. void
  2847. fuse_lib_create(fuse_req_t req,
  2848. fuse_ino_t parent,
  2849. const char *name,
  2850. mode_t mode,
  2851. fuse_file_info_t *fi)
  2852. {
  2853. int err;
  2854. char *path;
  2855. struct fuse *f;
  2856. struct fuse_entry_param e;
  2857. f = req_fuse_prepare(req);
  2858. err = get_path_name(f,parent,name,&path);
  2859. if(!err)
  2860. {
  2861. err = fuse_fs_create(f->fs,path,mode,fi);
  2862. if(!err)
  2863. {
  2864. err = lookup_path(f,parent,name,path,&e,fi);
  2865. if(err)
  2866. {
  2867. fuse_fs_release(f->fs,fi);
  2868. }
  2869. else if(!S_ISREG(e.attr.st_mode))
  2870. {
  2871. err = -EIO;
  2872. fuse_fs_release(f->fs,fi);
  2873. forget_node(f,e.ino,1);
  2874. }
  2875. }
  2876. }
  2877. if(!err)
  2878. {
  2879. pthread_mutex_lock(&f->lock);
  2880. get_node(f,e.ino)->open_count++;
  2881. pthread_mutex_unlock(&f->lock);
  2882. if(fuse_reply_create(req,&e,fi) == -ENOENT)
  2883. {
  2884. /* The open syscall was interrupted,so it
  2885. must be cancelled */
  2886. fuse_do_release(f,e.ino,fi);
  2887. forget_node(f,e.ino,1);
  2888. }
  2889. }
  2890. else
  2891. {
  2892. reply_err(req,err);
  2893. }
  2894. free_path(f,parent,path);
  2895. }
  2896. static
  2897. double
  2898. diff_timespec(const struct timespec *t1,
  2899. const struct timespec *t2)
  2900. {
  2901. return (t1->tv_sec - t2->tv_sec) +
  2902. ((double)t1->tv_nsec - (double)t2->tv_nsec) / 1000000000.0;
  2903. }
  2904. static
  2905. void
  2906. open_auto_cache(struct fuse *f,
  2907. fuse_ino_t ino,
  2908. const char *path,
  2909. fuse_file_info_t *fi)
  2910. {
  2911. struct node *node;
  2912. fuse_timeouts_t timeout;
  2913. pthread_mutex_lock(&f->lock);
  2914. node = get_node(f,ino);
  2915. if(node->stat_cache_valid)
  2916. {
  2917. int err;
  2918. struct stat stbuf;
  2919. pthread_mutex_unlock(&f->lock);
  2920. err = fuse_fs_fgetattr(f->fs,&stbuf,fi,&timeout);
  2921. pthread_mutex_lock(&f->lock);
  2922. if(!err)
  2923. update_stat(node,&stbuf);
  2924. else
  2925. node->stat_cache_valid = 0;
  2926. }
  2927. if(node->stat_cache_valid)
  2928. fi->keep_cache = 1;
  2929. node->stat_cache_valid = 1;
  2930. pthread_mutex_unlock(&f->lock);
  2931. }
  2932. static
  2933. void
  2934. fuse_lib_open(fuse_req_t req,
  2935. fuse_ino_t ino,
  2936. fuse_file_info_t *fi)
  2937. {
  2938. int err;
  2939. char *path;
  2940. struct fuse *f;
  2941. f = req_fuse_prepare(req);
  2942. err = get_path(f,ino,&path);
  2943. if(!err)
  2944. {
  2945. err = fuse_fs_open(f->fs,path,fi);
  2946. if(!err)
  2947. {
  2948. if(fi && fi->auto_cache)
  2949. open_auto_cache(f,ino,path,fi);
  2950. }
  2951. }
  2952. if(!err)
  2953. {
  2954. pthread_mutex_lock(&f->lock);
  2955. get_node(f,ino)->open_count++;
  2956. pthread_mutex_unlock(&f->lock);
  2957. /* The open syscall was interrupted,so it must be cancelled */
  2958. if(fuse_reply_open(req,fi) == -ENOENT)
  2959. fuse_do_release(f,ino,fi);
  2960. }
  2961. else
  2962. {
  2963. reply_err(req,err);
  2964. }
  2965. free_path(f,ino,path);
  2966. }
  2967. static
  2968. void
  2969. fuse_lib_read(fuse_req_t req,
  2970. fuse_ino_t ino,
  2971. size_t size,
  2972. off_t off,
  2973. fuse_file_info_t *fi)
  2974. {
  2975. struct fuse *f = req_fuse_prepare(req);
  2976. struct fuse_bufvec *buf = NULL;
  2977. int res;
  2978. res = fuse_fs_read_buf(f->fs,&buf,size,off,fi);
  2979. if(res == 0)
  2980. fuse_reply_data(req,buf,FUSE_BUF_SPLICE_MOVE);
  2981. else
  2982. reply_err(req,res);
  2983. fuse_free_buf(buf);
  2984. }
  2985. static
  2986. void
  2987. fuse_lib_write_buf(fuse_req_t req,
  2988. fuse_ino_t ino,
  2989. struct fuse_bufvec *buf,
  2990. off_t off,
  2991. fuse_file_info_t *fi)
  2992. {
  2993. int res;
  2994. struct fuse *f = req_fuse_prepare(req);
  2995. res = fuse_fs_write_buf(f->fs,buf,off,fi);
  2996. free_path(f,ino,NULL);
  2997. if(res >= 0)
  2998. fuse_reply_write(req,res);
  2999. else
  3000. reply_err(req,res);
  3001. }
  3002. static
  3003. void
  3004. fuse_lib_fsync(fuse_req_t req,
  3005. fuse_ino_t ino,
  3006. int datasync,
  3007. fuse_file_info_t *fi)
  3008. {
  3009. int err;
  3010. struct fuse *f = req_fuse_prepare(req);
  3011. err = fuse_fs_fsync(f->fs,datasync,fi);
  3012. reply_err(req,err);
  3013. }
  3014. static
  3015. struct fuse_dh*
  3016. get_dirhandle(const fuse_file_info_t *llfi,
  3017. fuse_file_info_t *fi)
  3018. {
  3019. struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
  3020. memset(fi,0,sizeof(fuse_file_info_t));
  3021. fi->fh = dh->fh;
  3022. return dh;
  3023. }
  3024. static
  3025. void
  3026. fuse_lib_opendir(fuse_req_t req,
  3027. fuse_ino_t ino,
  3028. fuse_file_info_t *llfi)
  3029. {
  3030. int err;
  3031. char *path;
  3032. struct fuse_dh *dh;
  3033. fuse_file_info_t fi;
  3034. struct fuse *f = req_fuse_prepare(req);
  3035. dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
  3036. if(dh == NULL)
  3037. {
  3038. reply_err(req,-ENOMEM);
  3039. return;
  3040. }
  3041. fuse_dirents_init(&dh->d);
  3042. fuse_mutex_init(&dh->lock);
  3043. llfi->fh = (uintptr_t)dh;
  3044. memset(&fi,0,sizeof(fi));
  3045. fi.flags = llfi->flags;
  3046. err = get_path(f,ino,&path);
  3047. if(!err)
  3048. {
  3049. err = fuse_fs_opendir(f->fs,path,&fi);
  3050. dh->fh = fi.fh;
  3051. llfi->keep_cache = fi.keep_cache;
  3052. llfi->cache_readdir = fi.cache_readdir;
  3053. }
  3054. if(!err)
  3055. {
  3056. if(fuse_reply_open(req,llfi) == -ENOENT)
  3057. {
  3058. /* The opendir syscall was interrupted,so it
  3059. must be cancelled */
  3060. fuse_fs_releasedir(f->fs,&fi);
  3061. pthread_mutex_destroy(&dh->lock);
  3062. free(dh);
  3063. }
  3064. }
  3065. else
  3066. {
  3067. reply_err(req,err);
  3068. pthread_mutex_destroy(&dh->lock);
  3069. free(dh);
  3070. }
  3071. free_path(f,ino,path);
  3072. }
  3073. static
  3074. int
  3075. readdir_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(f_->fs,fi_,d_);
  3082. return rv;
  3083. }
  3084. static
  3085. int
  3086. readdir_plus_fill(struct fuse *f_,
  3087. fuse_req_t req_,
  3088. fuse_dirents_t *d_,
  3089. fuse_file_info_t *fi_)
  3090. {
  3091. int rv;
  3092. rv = fuse_fs_readdir_plus(f_->fs,fi_,d_);
  3093. return rv;
  3094. }
  3095. static
  3096. size_t
  3097. readdir_buf_size(fuse_dirents_t *d_,
  3098. size_t size_,
  3099. off_t off_)
  3100. {
  3101. if(off_ >= kv_size(d_->offs))
  3102. return 0;
  3103. if((kv_A(d_->offs,off_) + size_) > d_->data_len)
  3104. return (d_->data_len - kv_A(d_->offs,off_));
  3105. return size_;
  3106. }
  3107. static
  3108. char*
  3109. readdir_buf(fuse_dirents_t *d_,
  3110. off_t off_)
  3111. {
  3112. return &d_->buf[kv_A(d_->offs,off_)];
  3113. }
  3114. static
  3115. void
  3116. fuse_lib_readdir(fuse_req_t req_,
  3117. fuse_ino_t ino_,
  3118. size_t size_,
  3119. off_t off_,
  3120. fuse_file_info_t *llffi_)
  3121. {
  3122. int rv;
  3123. struct fuse *f;
  3124. fuse_dirents_t *d;
  3125. struct fuse_dh *dh;
  3126. fuse_file_info_t fi;
  3127. f = req_fuse_prepare(req_);
  3128. dh = get_dirhandle(llffi_,&fi);
  3129. d = &dh->d;
  3130. pthread_mutex_lock(&dh->lock);
  3131. rv = 0;
  3132. if((off_ == 0) || (d->data_len == 0))
  3133. rv = readdir_fill(f,req_,d,&fi);
  3134. if(rv)
  3135. {
  3136. reply_err(req_,rv);
  3137. goto out;
  3138. }
  3139. size_ = readdir_buf_size(d,size_,off_);
  3140. fuse_reply_buf(req_,
  3141. readdir_buf(d,off_),
  3142. size_);
  3143. out:
  3144. pthread_mutex_unlock(&dh->lock);
  3145. }
  3146. static
  3147. void
  3148. fuse_lib_readdir_plus(fuse_req_t req_,
  3149. fuse_ino_t ino_,
  3150. size_t size_,
  3151. off_t off_,
  3152. fuse_file_info_t *llffi_)
  3153. {
  3154. int rv;
  3155. struct fuse *f;
  3156. fuse_dirents_t *d;
  3157. struct fuse_dh *dh;
  3158. fuse_file_info_t fi;
  3159. f = req_fuse_prepare(req_);
  3160. dh = get_dirhandle(llffi_,&fi);
  3161. d = &dh->d;
  3162. pthread_mutex_lock(&dh->lock);
  3163. rv = 0;
  3164. if((off_ == 0) || (d->data_len == 0))
  3165. rv = readdir_plus_fill(f,req_,d,&fi);
  3166. if(rv)
  3167. {
  3168. reply_err(req_,rv);
  3169. goto out;
  3170. }
  3171. size_ = readdir_buf_size(d,size_,off_);
  3172. fuse_reply_buf(req_,
  3173. readdir_buf(d,off_),
  3174. size_);
  3175. out:
  3176. pthread_mutex_unlock(&dh->lock);
  3177. }
  3178. static
  3179. void
  3180. fuse_lib_releasedir(fuse_req_t req_,
  3181. fuse_ino_t ino_,
  3182. fuse_file_info_t *llfi_)
  3183. {
  3184. struct fuse *f;
  3185. struct fuse_dh *dh;
  3186. fuse_file_info_t fi;
  3187. f = req_fuse_prepare(req_);
  3188. dh = get_dirhandle(llfi_,&fi);
  3189. fuse_fs_releasedir(f->fs,&fi);
  3190. /* Done to keep race condition between last readdir reply and the unlock */
  3191. pthread_mutex_lock(&dh->lock);
  3192. pthread_mutex_unlock(&dh->lock);
  3193. pthread_mutex_destroy(&dh->lock);
  3194. fuse_dirents_free(&dh->d);
  3195. free(dh);
  3196. reply_err(req_,0);
  3197. }
  3198. static
  3199. void
  3200. fuse_lib_fsyncdir(fuse_req_t req,
  3201. fuse_ino_t ino,
  3202. int datasync,
  3203. fuse_file_info_t *llfi)
  3204. {
  3205. int err;
  3206. fuse_file_info_t fi;
  3207. struct fuse *f = req_fuse_prepare(req);
  3208. get_dirhandle(llfi,&fi);
  3209. err = fuse_fs_fsyncdir(f->fs,datasync,&fi);
  3210. reply_err(req,err);
  3211. }
  3212. static
  3213. void
  3214. fuse_lib_statfs(fuse_req_t req,
  3215. fuse_ino_t ino)
  3216. {
  3217. struct fuse *f = req_fuse_prepare(req);
  3218. struct statvfs buf;
  3219. char *path = NULL;
  3220. int err = 0;
  3221. memset(&buf,0,sizeof(buf));
  3222. if(ino)
  3223. err = get_path(f,ino,&path);
  3224. if(!err)
  3225. {
  3226. err = fuse_fs_statfs(f->fs,path ? path : "/",&buf);
  3227. free_path(f,ino,path);
  3228. }
  3229. if(!err)
  3230. fuse_reply_statfs(req,&buf);
  3231. else
  3232. reply_err(req,err);
  3233. }
  3234. static
  3235. void
  3236. fuse_lib_setxattr(fuse_req_t req,
  3237. fuse_ino_t ino,
  3238. const char *name,
  3239. const char *value,
  3240. size_t size,
  3241. int flags)
  3242. {
  3243. struct fuse *f = req_fuse_prepare(req);
  3244. char *path;
  3245. int err;
  3246. err = get_path(f,ino,&path);
  3247. if(!err)
  3248. {
  3249. err = fuse_fs_setxattr(f->fs,path,name,value,size,flags);
  3250. free_path(f,ino,path);
  3251. }
  3252. reply_err(req,err);
  3253. }
  3254. static
  3255. int
  3256. common_getxattr(struct fuse *f,
  3257. fuse_req_t req,
  3258. fuse_ino_t ino,
  3259. const char *name,
  3260. char *value,
  3261. size_t size)
  3262. {
  3263. int err;
  3264. char *path;
  3265. err = get_path(f,ino,&path);
  3266. if(!err)
  3267. {
  3268. err = fuse_fs_getxattr(f->fs,path,name,value,size);
  3269. free_path(f,ino,path);
  3270. }
  3271. return err;
  3272. }
  3273. static
  3274. void
  3275. fuse_lib_getxattr(fuse_req_t req,
  3276. fuse_ino_t ino,
  3277. const char *name,
  3278. size_t size)
  3279. {
  3280. struct fuse *f = req_fuse_prepare(req);
  3281. int res;
  3282. if(size)
  3283. {
  3284. char *value = (char *)malloc(size);
  3285. if(value == NULL)
  3286. {
  3287. reply_err(req,-ENOMEM);
  3288. return;
  3289. }
  3290. res = common_getxattr(f,req,ino,name,value,size);
  3291. if(res > 0)
  3292. fuse_reply_buf(req,value,res);
  3293. else
  3294. reply_err(req,res);
  3295. free(value);
  3296. }
  3297. else
  3298. {
  3299. res = common_getxattr(f,req,ino,name,NULL,0);
  3300. if(res >= 0)
  3301. fuse_reply_xattr(req,res);
  3302. else
  3303. reply_err(req,res);
  3304. }
  3305. }
  3306. static
  3307. int
  3308. common_listxattr(struct fuse *f,
  3309. fuse_req_t req,
  3310. fuse_ino_t ino,
  3311. char *list,
  3312. size_t size)
  3313. {
  3314. char *path;
  3315. int err;
  3316. err = get_path(f,ino,&path);
  3317. if(!err)
  3318. {
  3319. err = fuse_fs_listxattr(f->fs,path,list,size);
  3320. free_path(f,ino,path);
  3321. }
  3322. return err;
  3323. }
  3324. static
  3325. void
  3326. fuse_lib_listxattr(fuse_req_t req,
  3327. fuse_ino_t ino,
  3328. size_t size)
  3329. {
  3330. struct fuse *f = req_fuse_prepare(req);
  3331. int res;
  3332. if(size)
  3333. {
  3334. char *list = (char *)malloc(size);
  3335. if(list == NULL)
  3336. {
  3337. reply_err(req,-ENOMEM);
  3338. return;
  3339. }
  3340. res = common_listxattr(f,req,ino,list,size);
  3341. if(res > 0)
  3342. fuse_reply_buf(req,list,res);
  3343. else
  3344. reply_err(req,res);
  3345. free(list);
  3346. }
  3347. else
  3348. {
  3349. res = common_listxattr(f,req,ino,NULL,0);
  3350. if(res >= 0)
  3351. fuse_reply_xattr(req,res);
  3352. else
  3353. reply_err(req,res);
  3354. }
  3355. }
  3356. static
  3357. void
  3358. fuse_lib_removexattr(fuse_req_t req,
  3359. fuse_ino_t ino,
  3360. const char *name)
  3361. {
  3362. struct fuse *f = req_fuse_prepare(req);
  3363. char *path;
  3364. int err;
  3365. err = get_path(f,ino,&path);
  3366. if(!err)
  3367. {
  3368. err = fuse_fs_removexattr(f->fs,path,name);
  3369. free_path(f,ino,path);
  3370. }
  3371. reply_err(req,err);
  3372. }
  3373. static
  3374. void
  3375. fuse_lib_copy_file_range(fuse_req_t req_,
  3376. fuse_ino_t nodeid_in_,
  3377. off_t off_in_,
  3378. fuse_file_info_t *ffi_in_,
  3379. fuse_ino_t nodeid_out_,
  3380. off_t off_out_,
  3381. fuse_file_info_t *ffi_out_,
  3382. size_t len_,
  3383. int flags_)
  3384. {
  3385. ssize_t rv;
  3386. struct fuse *f;
  3387. f = req_fuse_prepare(req_);
  3388. rv = fuse_fs_copy_file_range(f->fs,
  3389. ffi_in_,
  3390. off_in_,
  3391. ffi_out_,
  3392. off_out_,
  3393. len_,
  3394. flags_);
  3395. if(rv >= 0)
  3396. fuse_reply_write(req_,rv);
  3397. else
  3398. reply_err(req_,rv);
  3399. }
  3400. static
  3401. struct lock*
  3402. locks_conflict(struct node *node,
  3403. const struct lock *lock)
  3404. {
  3405. struct lock *l;
  3406. for(l = node->locks; l; l = l->next)
  3407. if(l->owner != lock->owner &&
  3408. lock->start <= l->end && l->start <= lock->end &&
  3409. (l->type == F_WRLCK || lock->type == F_WRLCK))
  3410. break;
  3411. return l;
  3412. }
  3413. static
  3414. void
  3415. delete_lock(struct lock **lockp)
  3416. {
  3417. struct lock *l = *lockp;
  3418. *lockp = l->next;
  3419. free(l);
  3420. }
  3421. static
  3422. void
  3423. insert_lock(struct lock **pos,
  3424. struct lock *lock)
  3425. {
  3426. lock->next = *pos;
  3427. *pos = lock;
  3428. }
  3429. static
  3430. int
  3431. locks_insert(struct node *node,
  3432. struct lock *lock)
  3433. {
  3434. struct lock **lp;
  3435. struct lock *newl1 = NULL;
  3436. struct lock *newl2 = NULL;
  3437. if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
  3438. {
  3439. newl1 = malloc(sizeof(struct lock));
  3440. newl2 = malloc(sizeof(struct lock));
  3441. if(!newl1 || !newl2)
  3442. {
  3443. free(newl1);
  3444. free(newl2);
  3445. return -ENOLCK;
  3446. }
  3447. }
  3448. for(lp = &node->locks; *lp;)
  3449. {
  3450. struct lock *l = *lp;
  3451. if(l->owner != lock->owner)
  3452. goto skip;
  3453. if(lock->type == l->type)
  3454. {
  3455. if(l->end < lock->start - 1)
  3456. goto skip;
  3457. if(lock->end < l->start - 1)
  3458. break;
  3459. if(l->start <= lock->start && lock->end <= l->end)
  3460. goto out;
  3461. if(l->start < lock->start)
  3462. lock->start = l->start;
  3463. if(lock->end < l->end)
  3464. lock->end = l->end;
  3465. goto delete;
  3466. }
  3467. else
  3468. {
  3469. if(l->end < lock->start)
  3470. goto skip;
  3471. if(lock->end < l->start)
  3472. break;
  3473. if(lock->start <= l->start && l->end <= lock->end)
  3474. goto delete;
  3475. if(l->end <= lock->end)
  3476. {
  3477. l->end = lock->start - 1;
  3478. goto skip;
  3479. }
  3480. if(lock->start <= l->start)
  3481. {
  3482. l->start = lock->end + 1;
  3483. break;
  3484. }
  3485. *newl2 = *l;
  3486. newl2->start = lock->end + 1;
  3487. l->end = lock->start - 1;
  3488. insert_lock(&l->next,newl2);
  3489. newl2 = NULL;
  3490. }
  3491. skip:
  3492. lp = &l->next;
  3493. continue;
  3494. delete:
  3495. delete_lock(lp);
  3496. }
  3497. if(lock->type != F_UNLCK)
  3498. {
  3499. *newl1 = *lock;
  3500. insert_lock(lp,newl1);
  3501. newl1 = NULL;
  3502. }
  3503. out:
  3504. free(newl1);
  3505. free(newl2);
  3506. return 0;
  3507. }
  3508. static
  3509. void
  3510. flock_to_lock(struct flock *flock,
  3511. struct lock *lock)
  3512. {
  3513. memset(lock,0,sizeof(struct lock));
  3514. lock->type = flock->l_type;
  3515. lock->start = flock->l_start;
  3516. lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
  3517. lock->pid = flock->l_pid;
  3518. }
  3519. static
  3520. void
  3521. lock_to_flock(struct lock *lock,
  3522. struct flock *flock)
  3523. {
  3524. flock->l_type = lock->type;
  3525. flock->l_start = lock->start;
  3526. flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
  3527. flock->l_pid = lock->pid;
  3528. }
  3529. static
  3530. int
  3531. fuse_flush_common(struct fuse *f,
  3532. fuse_req_t req,
  3533. fuse_ino_t ino,
  3534. fuse_file_info_t *fi)
  3535. {
  3536. struct flock lock;
  3537. struct lock l;
  3538. int err;
  3539. int errlock;
  3540. memset(&lock,0,sizeof(lock));
  3541. lock.l_type = F_UNLCK;
  3542. lock.l_whence = SEEK_SET;
  3543. err = fuse_fs_flush(f->fs,fi);
  3544. errlock = fuse_fs_lock(f->fs,fi,F_SETLK,&lock);
  3545. if(errlock != -ENOSYS)
  3546. {
  3547. flock_to_lock(&lock,&l);
  3548. l.owner = fi->lock_owner;
  3549. pthread_mutex_lock(&f->lock);
  3550. locks_insert(get_node(f,ino),&l);
  3551. pthread_mutex_unlock(&f->lock);
  3552. /* if op.lock() is defined FLUSH is needed regardless
  3553. of op.flush() */
  3554. if(err == -ENOSYS)
  3555. err = 0;
  3556. }
  3557. return err;
  3558. }
  3559. static
  3560. void
  3561. fuse_lib_release(fuse_req_t req,
  3562. fuse_ino_t ino,
  3563. fuse_file_info_t *fi)
  3564. {
  3565. int err = 0;
  3566. struct fuse *f = req_fuse_prepare(req);
  3567. if(fi->flush)
  3568. {
  3569. err = fuse_flush_common(f,req,ino,fi);
  3570. if(err == -ENOSYS)
  3571. err = 0;
  3572. }
  3573. fuse_do_release(f,ino,fi);
  3574. reply_err(req,err);
  3575. }
  3576. static
  3577. void
  3578. fuse_lib_flush(fuse_req_t req,
  3579. fuse_ino_t ino,
  3580. fuse_file_info_t *fi)
  3581. {
  3582. int err;
  3583. struct fuse *f = req_fuse_prepare(req);
  3584. err = fuse_flush_common(f,req,ino,fi);
  3585. reply_err(req,err);
  3586. }
  3587. static
  3588. int
  3589. fuse_lock_common(fuse_req_t req,
  3590. fuse_ino_t ino,
  3591. fuse_file_info_t *fi,
  3592. struct flock *lock,
  3593. int cmd)
  3594. {
  3595. int err;
  3596. struct fuse *f = req_fuse_prepare(req);
  3597. err = fuse_fs_lock(f->fs,fi,cmd,lock);
  3598. return err;
  3599. }
  3600. static
  3601. void
  3602. fuse_lib_getlk(fuse_req_t req,
  3603. fuse_ino_t ino,
  3604. fuse_file_info_t *fi,
  3605. struct flock *lock)
  3606. {
  3607. int err;
  3608. struct lock l;
  3609. struct lock *conflict;
  3610. struct fuse *f = req_fuse(req);
  3611. flock_to_lock(lock,&l);
  3612. l.owner = fi->lock_owner;
  3613. pthread_mutex_lock(&f->lock);
  3614. conflict = locks_conflict(get_node(f,ino),&l);
  3615. if(conflict)
  3616. lock_to_flock(conflict,lock);
  3617. pthread_mutex_unlock(&f->lock);
  3618. if(!conflict)
  3619. err = fuse_lock_common(req,ino,fi,lock,F_GETLK);
  3620. else
  3621. err = 0;
  3622. if(!err)
  3623. fuse_reply_lock(req,lock);
  3624. else
  3625. reply_err(req,err);
  3626. }
  3627. static
  3628. void
  3629. fuse_lib_setlk(fuse_req_t req,
  3630. fuse_ino_t ino,
  3631. fuse_file_info_t *fi,
  3632. struct flock *lock,
  3633. int sleep)
  3634. {
  3635. int err = fuse_lock_common(req,ino,fi,lock,
  3636. sleep ? F_SETLKW : F_SETLK);
  3637. if(!err)
  3638. {
  3639. struct fuse *f = req_fuse(req);
  3640. struct lock l;
  3641. flock_to_lock(lock,&l);
  3642. l.owner = fi->lock_owner;
  3643. pthread_mutex_lock(&f->lock);
  3644. locks_insert(get_node(f,ino),&l);
  3645. pthread_mutex_unlock(&f->lock);
  3646. }
  3647. reply_err(req,err);
  3648. }
  3649. static
  3650. void
  3651. fuse_lib_flock(fuse_req_t req,
  3652. fuse_ino_t ino,
  3653. fuse_file_info_t *fi,
  3654. int op)
  3655. {
  3656. int err;
  3657. struct fuse *f = req_fuse_prepare(req);
  3658. err = fuse_fs_flock(f->fs,fi,op);
  3659. reply_err(req,err);
  3660. }
  3661. static
  3662. void
  3663. fuse_lib_bmap(fuse_req_t req,
  3664. fuse_ino_t ino,
  3665. size_t blocksize,
  3666. uint64_t idx)
  3667. {
  3668. int err;
  3669. char *path;
  3670. struct fuse *f = req_fuse_prepare(req);
  3671. err = get_path(f,ino,&path);
  3672. if(!err)
  3673. {
  3674. err = fuse_fs_bmap(f->fs,path,blocksize,&idx);
  3675. free_path(f,ino,path);
  3676. }
  3677. if(!err)
  3678. fuse_reply_bmap(req,idx);
  3679. else
  3680. reply_err(req,err);
  3681. }
  3682. static
  3683. void
  3684. fuse_lib_ioctl(fuse_req_t req,
  3685. fuse_ino_t ino,
  3686. unsigned long cmd,
  3687. void *arg,
  3688. fuse_file_info_t *llfi,
  3689. unsigned int flags,
  3690. const void *in_buf,
  3691. uint32_t in_bufsz,
  3692. uint32_t out_bufsz_)
  3693. {
  3694. int err;
  3695. char *out_buf = NULL;
  3696. struct fuse *f = req_fuse_prepare(req);
  3697. fuse_file_info_t fi;
  3698. uint32_t out_bufsz = out_bufsz_;
  3699. err = -EPERM;
  3700. if(flags & FUSE_IOCTL_UNRESTRICTED)
  3701. goto err;
  3702. if(flags & FUSE_IOCTL_DIR)
  3703. get_dirhandle(llfi,&fi);
  3704. else
  3705. fi = *llfi;
  3706. if(out_bufsz)
  3707. {
  3708. err = -ENOMEM;
  3709. out_buf = malloc(out_bufsz);
  3710. if(!out_buf)
  3711. goto err;
  3712. }
  3713. assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
  3714. if(out_buf)
  3715. memcpy(out_buf,in_buf,in_bufsz);
  3716. err = fuse_fs_ioctl(f->fs,cmd,arg,&fi,flags,
  3717. out_buf ?: (void *)in_buf,&out_bufsz);
  3718. fuse_reply_ioctl(req,err,out_buf,out_bufsz);
  3719. goto out;
  3720. err:
  3721. reply_err(req,err);
  3722. out:
  3723. free(out_buf);
  3724. }
  3725. static
  3726. void
  3727. fuse_lib_poll(fuse_req_t req,
  3728. fuse_ino_t ino,
  3729. fuse_file_info_t *fi,
  3730. fuse_pollhandle_t *ph)
  3731. {
  3732. int err;
  3733. struct fuse *f = req_fuse_prepare(req);
  3734. unsigned revents = 0;
  3735. err = fuse_fs_poll(f->fs,fi,ph,&revents);
  3736. if(!err)
  3737. fuse_reply_poll(req,revents);
  3738. else
  3739. reply_err(req,err);
  3740. }
  3741. static
  3742. void
  3743. fuse_lib_fallocate(fuse_req_t req,
  3744. fuse_ino_t ino,
  3745. int mode,
  3746. off_t offset,
  3747. off_t length,
  3748. fuse_file_info_t *fi)
  3749. {
  3750. int err;
  3751. struct fuse *f = req_fuse_prepare(req);
  3752. err = fuse_fs_fallocate(f->fs,mode,offset,length,fi);
  3753. reply_err(req,err);
  3754. }
  3755. static
  3756. int
  3757. clean_delay(struct fuse *f)
  3758. {
  3759. /*
  3760. * This is calculating the delay between clean runs. To
  3761. * reduce the number of cleans we are doing them 10 times
  3762. * within the remember window.
  3763. */
  3764. int min_sleep = 60;
  3765. int max_sleep = 3600;
  3766. int sleep_time = f->conf.remember / 10;
  3767. if(sleep_time > max_sleep)
  3768. return max_sleep;
  3769. if(sleep_time < min_sleep)
  3770. return min_sleep;
  3771. return sleep_time;
  3772. }
  3773. int
  3774. fuse_clean_cache(struct fuse *f)
  3775. {
  3776. struct node_lru *lnode;
  3777. struct list_head *curr,*next;
  3778. struct node *node;
  3779. struct timespec now;
  3780. pthread_mutex_lock(&f->lock);
  3781. curr_time(&now);
  3782. for(curr = f->lru_table.next; curr != &f->lru_table; curr = next)
  3783. {
  3784. double age;
  3785. next = curr->next;
  3786. lnode = list_entry(curr,struct node_lru,lru);
  3787. node = &lnode->node;
  3788. age = diff_timespec(&now,&lnode->forget_time);
  3789. if(age <= f->conf.remember)
  3790. break;
  3791. assert(node->nlookup == 1);
  3792. /* Don't forget active directories */
  3793. if(node->refctr > 1)
  3794. continue;
  3795. node->nlookup = 0;
  3796. unhash_name(f,node);
  3797. unref_node(f,node);
  3798. }
  3799. pthread_mutex_unlock(&f->lock);
  3800. return clean_delay(f);
  3801. }
  3802. static struct fuse_lowlevel_ops fuse_path_ops =
  3803. {
  3804. .init = fuse_lib_init,
  3805. .destroy = fuse_lib_destroy,
  3806. .lookup = fuse_lib_lookup,
  3807. .forget = fuse_lib_forget,
  3808. .forget_multi = fuse_lib_forget_multi,
  3809. .getattr = fuse_lib_getattr,
  3810. .setattr = fuse_lib_setattr,
  3811. .access = fuse_lib_access,
  3812. .readlink = fuse_lib_readlink,
  3813. .mknod = fuse_lib_mknod,
  3814. .mkdir = fuse_lib_mkdir,
  3815. .unlink = fuse_lib_unlink,
  3816. .rmdir = fuse_lib_rmdir,
  3817. .symlink = fuse_lib_symlink,
  3818. .rename = fuse_lib_rename,
  3819. .link = fuse_lib_link,
  3820. .create = fuse_lib_create,
  3821. .open = fuse_lib_open,
  3822. .read = fuse_lib_read,
  3823. .write_buf = fuse_lib_write_buf,
  3824. .flush = fuse_lib_flush,
  3825. .release = fuse_lib_release,
  3826. .fsync = fuse_lib_fsync,
  3827. .opendir = fuse_lib_opendir,
  3828. .readdir = fuse_lib_readdir,
  3829. .readdir_plus = fuse_lib_readdir_plus,
  3830. .releasedir = fuse_lib_releasedir,
  3831. .fsyncdir = fuse_lib_fsyncdir,
  3832. .statfs = fuse_lib_statfs,
  3833. .setxattr = fuse_lib_setxattr,
  3834. .getxattr = fuse_lib_getxattr,
  3835. .listxattr = fuse_lib_listxattr,
  3836. .removexattr = fuse_lib_removexattr,
  3837. .getlk = fuse_lib_getlk,
  3838. .setlk = fuse_lib_setlk,
  3839. .flock = fuse_lib_flock,
  3840. .bmap = fuse_lib_bmap,
  3841. .ioctl = fuse_lib_ioctl,
  3842. .poll = fuse_lib_poll,
  3843. .fallocate = fuse_lib_fallocate,
  3844. .copy_file_range = fuse_lib_copy_file_range,
  3845. };
  3846. int
  3847. fuse_notify_poll(fuse_pollhandle_t *ph)
  3848. {
  3849. return fuse_lowlevel_notify_poll(ph);
  3850. }
  3851. static
  3852. void
  3853. free_cmd(struct fuse_cmd *cmd)
  3854. {
  3855. free(cmd->buf);
  3856. free(cmd);
  3857. }
  3858. void
  3859. fuse_process_cmd(struct fuse *f,
  3860. struct fuse_cmd *cmd)
  3861. {
  3862. fuse_session_process(f->se,cmd->buf,cmd->buflen,cmd->ch);
  3863. free_cmd(cmd);
  3864. }
  3865. int
  3866. fuse_exited(struct fuse *f)
  3867. {
  3868. return fuse_session_exited(f->se);
  3869. }
  3870. struct fuse_session*
  3871. fuse_get_session(struct fuse *f)
  3872. {
  3873. return f->se;
  3874. }
  3875. static
  3876. struct fuse_cmd*
  3877. fuse_alloc_cmd(size_t bufsize)
  3878. {
  3879. struct fuse_cmd *cmd = (struct fuse_cmd *)malloc(sizeof(*cmd));
  3880. if(cmd == NULL)
  3881. {
  3882. fprintf(stderr,"fuse: failed to allocate cmd\n");
  3883. return NULL;
  3884. }
  3885. cmd->buf = (char *)malloc(bufsize);
  3886. if(cmd->buf == NULL)
  3887. {
  3888. fprintf(stderr,"fuse: failed to allocate read buffer\n");
  3889. free(cmd);
  3890. return NULL;
  3891. }
  3892. return cmd;
  3893. }
  3894. struct fuse_cmd*
  3895. fuse_read_cmd(struct fuse *f)
  3896. {
  3897. struct fuse_chan *ch = fuse_session_next_chan(f->se,NULL);
  3898. size_t bufsize = fuse_chan_bufsize(ch);
  3899. struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize);
  3900. if(cmd != NULL)
  3901. {
  3902. int res = fuse_chan_recv(&ch,cmd->buf,bufsize);
  3903. if(res <= 0)
  3904. {
  3905. free_cmd(cmd);
  3906. if(res < 0 && res != -EINTR && res != -EAGAIN)
  3907. fuse_exit(f);
  3908. return NULL;
  3909. }
  3910. cmd->buflen = res;
  3911. cmd->ch = ch;
  3912. }
  3913. return cmd;
  3914. }
  3915. void
  3916. fuse_exit(struct fuse *f)
  3917. {
  3918. fuse_session_exit(f->se);
  3919. }
  3920. struct fuse_context*
  3921. fuse_get_context(void)
  3922. {
  3923. return &fuse_get_context_internal()->ctx;
  3924. }
  3925. enum {
  3926. KEY_HELP,
  3927. };
  3928. #define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
  3929. static const struct fuse_opt fuse_lib_opts[] =
  3930. {
  3931. FUSE_OPT_KEY("-h", KEY_HELP),
  3932. FUSE_OPT_KEY("--help", KEY_HELP),
  3933. FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
  3934. FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
  3935. FUSE_LIB_OPT("debug", debug,1),
  3936. FUSE_LIB_OPT("-d", debug,1),
  3937. FUSE_LIB_OPT("umask=", set_mode,1),
  3938. FUSE_LIB_OPT("umask=%o", umask,0),
  3939. FUSE_LIB_OPT("uid=", set_uid,1),
  3940. FUSE_LIB_OPT("uid=%d", uid,0),
  3941. FUSE_LIB_OPT("gid=", set_gid,1),
  3942. FUSE_LIB_OPT("gid=%d", gid,0),
  3943. FUSE_LIB_OPT("noforget", remember,-1),
  3944. FUSE_LIB_OPT("remember=%u", remember,0),
  3945. FUSE_LIB_OPT("threads=%d", threads,0),
  3946. FUSE_LIB_OPT("use_ino", use_ino,1),
  3947. FUSE_OPT_END
  3948. };
  3949. static void fuse_lib_help(void)
  3950. {
  3951. fprintf(stderr,
  3952. " -o umask=M set file permissions (octal)\n"
  3953. " -o uid=N set file owner\n"
  3954. " -o gid=N set file group\n"
  3955. " -o noforget never forget cached inodes\n"
  3956. " -o remember=T remember cached inodes for T seconds (0s)\n"
  3957. " -o threads=NUM number of worker threads. 0 = autodetect.\n"
  3958. " Negative values autodetect then divide by\n"
  3959. " absolute value. default = 0\n"
  3960. "\n");
  3961. }
  3962. static
  3963. int
  3964. fuse_lib_opt_proc(void *data,
  3965. const char *arg,
  3966. int key,
  3967. struct fuse_args *outargs)
  3968. {
  3969. (void)arg; (void)outargs;
  3970. if(key == KEY_HELP)
  3971. {
  3972. struct fuse_config *conf = (struct fuse_config *)data;
  3973. fuse_lib_help();
  3974. conf->help = 1;
  3975. }
  3976. return 1;
  3977. }
  3978. int
  3979. fuse_is_lib_option(const char *opt)
  3980. {
  3981. return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
  3982. }
  3983. struct fuse_fs*
  3984. fuse_fs_new(const struct fuse_operations *op,
  3985. size_t op_size,
  3986. void *user_data)
  3987. {
  3988. struct fuse_fs *fs;
  3989. if(sizeof(struct fuse_operations) < op_size)
  3990. {
  3991. fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
  3992. op_size = sizeof(struct fuse_operations);
  3993. }
  3994. fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
  3995. if(!fs)
  3996. {
  3997. fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
  3998. return NULL;
  3999. }
  4000. fs->user_data = user_data;
  4001. if(op)
  4002. memcpy(&fs->op,op,op_size);
  4003. return fs;
  4004. }
  4005. static
  4006. int
  4007. node_table_init(struct node_table *t)
  4008. {
  4009. t->size = NODE_TABLE_MIN_SIZE;
  4010. t->array = (struct node **)calloc(1,sizeof(struct node *) * t->size);
  4011. if(t->array == NULL)
  4012. {
  4013. fprintf(stderr,"fuse: memory allocation failed\n");
  4014. return -1;
  4015. }
  4016. t->use = 0;
  4017. t->split = 0;
  4018. return 0;
  4019. }
  4020. static
  4021. void*
  4022. fuse_prune_nodes(void *fuse)
  4023. {
  4024. struct fuse *f = fuse;
  4025. int sleep_time;
  4026. while(1)
  4027. {
  4028. sleep_time = fuse_clean_cache(f);
  4029. sleep(sleep_time);
  4030. }
  4031. return NULL;
  4032. }
  4033. int
  4034. fuse_start_cleanup_thread(struct fuse *f)
  4035. {
  4036. if(lru_enabled(f))
  4037. return fuse_start_thread(&f->prune_thread,fuse_prune_nodes,f);
  4038. return 0;
  4039. }
  4040. void
  4041. fuse_stop_cleanup_thread(struct fuse *f)
  4042. {
  4043. if(lru_enabled(f))
  4044. {
  4045. pthread_mutex_lock(&f->lock);
  4046. pthread_cancel(f->prune_thread);
  4047. pthread_mutex_unlock(&f->lock);
  4048. pthread_join(f->prune_thread,NULL);
  4049. }
  4050. }
  4051. struct fuse*
  4052. fuse_new_common(struct fuse_chan *ch,
  4053. struct fuse_args *args,
  4054. const struct fuse_operations *op,
  4055. size_t op_size,
  4056. void *user_data)
  4057. {
  4058. struct fuse *f;
  4059. struct node *root;
  4060. struct fuse_fs *fs;
  4061. struct fuse_lowlevel_ops llop = fuse_path_ops;
  4062. if(fuse_create_context_key() == -1)
  4063. goto out;
  4064. f = (struct fuse *)calloc(1,sizeof(struct fuse));
  4065. if(f == NULL)
  4066. {
  4067. fprintf(stderr,"fuse: failed to allocate fuse object\n");
  4068. goto out_delete_context_key;
  4069. }
  4070. fs = fuse_fs_new(op,op_size,user_data);
  4071. if(!fs)
  4072. goto out_free;
  4073. f->fs = fs;
  4074. /* Oh f**k,this is ugly! */
  4075. if(!fs->op.lock)
  4076. {
  4077. llop.getlk = NULL;
  4078. llop.setlk = NULL;
  4079. }
  4080. f->pagesize = getpagesize();
  4081. init_list_head(&f->partial_slabs);
  4082. init_list_head(&f->full_slabs);
  4083. init_list_head(&f->lru_table);
  4084. if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
  4085. goto out_free_fs;
  4086. f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
  4087. if(f->se == NULL)
  4088. goto out_free_fs;
  4089. fuse_session_add_chan(f->se,ch);
  4090. /* Trace topmost layer by default */
  4091. srand(time(NULL));
  4092. f->fs->debug = f->conf.debug;
  4093. f->ctr = 0;
  4094. f->generation = rand64();
  4095. if(node_table_init(&f->name_table) == -1)
  4096. goto out_free_session;
  4097. if(node_table_init(&f->id_table) == -1)
  4098. goto out_free_name_table;
  4099. fuse_mutex_init(&f->lock);
  4100. root = alloc_node(f);
  4101. if(root == NULL)
  4102. {
  4103. fprintf(stderr,"fuse: memory allocation failed\n");
  4104. goto out_free_id_table;
  4105. }
  4106. if(lru_enabled(f))
  4107. {
  4108. struct node_lru *lnode = node_lru(root);
  4109. init_list_head(&lnode->lru);
  4110. }
  4111. strcpy(root->inline_name,"/");
  4112. root->name = root->inline_name;
  4113. root->parent = NULL;
  4114. root->nodeid = FUSE_ROOT_ID;
  4115. inc_nlookup(root);
  4116. hash_id(f,root);
  4117. return f;
  4118. out_free_id_table:
  4119. free(f->id_table.array);
  4120. out_free_name_table:
  4121. free(f->name_table.array);
  4122. out_free_session:
  4123. fuse_session_destroy(f->se);
  4124. out_free_fs:
  4125. /* Horrible compatibility hack to stop the destructor from being
  4126. called on the filesystem without init being called first */
  4127. fs->op.destroy = NULL;
  4128. fuse_fs_destroy(f->fs);
  4129. out_free:
  4130. free(f);
  4131. out_delete_context_key:
  4132. fuse_delete_context_key();
  4133. out:
  4134. return NULL;
  4135. }
  4136. struct fuse*
  4137. fuse_new(struct fuse_chan *ch,
  4138. struct fuse_args *args,
  4139. const struct fuse_operations *op,
  4140. size_t op_size,
  4141. void *user_data)
  4142. {
  4143. return fuse_new_common(ch,args,op,op_size,user_data);
  4144. }
  4145. void
  4146. fuse_destroy(struct fuse *f)
  4147. {
  4148. size_t i;
  4149. if(f->fs)
  4150. {
  4151. struct fuse_context_i *c = fuse_get_context_internal();
  4152. memset(c,0,sizeof(*c));
  4153. c->ctx.fuse = f;
  4154. for(i = 0; i < f->id_table.size; i++)
  4155. {
  4156. struct node *node;
  4157. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  4158. {
  4159. if(node->is_hidden)
  4160. fuse_fs_free_hide(f->fs,node->hidden_fh);
  4161. }
  4162. }
  4163. }
  4164. for(i = 0; i < f->id_table.size; i++)
  4165. {
  4166. struct node *node;
  4167. struct node *next;
  4168. for(node = f->id_table.array[i]; node != NULL; node = next)
  4169. {
  4170. next = node->id_next;
  4171. free_node(f,node);
  4172. f->id_table.use--;
  4173. }
  4174. }
  4175. assert(list_empty(&f->partial_slabs));
  4176. assert(list_empty(&f->full_slabs));
  4177. free(f->id_table.array);
  4178. free(f->name_table.array);
  4179. pthread_mutex_destroy(&f->lock);
  4180. fuse_session_destroy(f->se);
  4181. free(f);
  4182. fuse_delete_context_key();
  4183. }
  4184. int
  4185. fuse_config_num_threads(const struct fuse *fuse_)
  4186. {
  4187. return fuse_->conf.threads;
  4188. }