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.

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