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.

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