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.

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