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.

4177 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. assert(node->nlookup > 1);
  1107. node->nlookup--;
  1108. unhash_name(f,node);
  1109. }
  1110. static
  1111. void
  1112. remove_node(struct fuse *f,
  1113. fuse_ino_t dir,
  1114. const char *name)
  1115. {
  1116. struct node *node;
  1117. pthread_mutex_lock(&f->lock);
  1118. node = lookup_node(f,dir,name);
  1119. if(node != NULL)
  1120. unlink_node(f,node);
  1121. pthread_mutex_unlock(&f->lock);
  1122. }
  1123. static
  1124. int
  1125. rename_node(struct fuse *f,
  1126. fuse_ino_t olddir,
  1127. const char *oldname,
  1128. fuse_ino_t newdir,
  1129. const char *newname)
  1130. {
  1131. struct node *node;
  1132. struct node *newnode;
  1133. int err = 0;
  1134. pthread_mutex_lock(&f->lock);
  1135. node = lookup_node(f,olddir,oldname);
  1136. newnode = lookup_node(f,newdir,newname);
  1137. if(node == NULL)
  1138. goto out;
  1139. if(newnode != NULL)
  1140. unlink_node(f,newnode);
  1141. unhash_name(f,node);
  1142. if(hash_name(f,node,newdir,newname) == -1)
  1143. {
  1144. err = -ENOMEM;
  1145. goto out;
  1146. }
  1147. out:
  1148. pthread_mutex_unlock(&f->lock);
  1149. return err;
  1150. }
  1151. static
  1152. void
  1153. set_stat(struct fuse *f,
  1154. fuse_ino_t nodeid,
  1155. struct stat *stbuf)
  1156. {
  1157. if(!f->conf.use_ino)
  1158. stbuf->st_ino = nodeid;
  1159. if(f->conf.set_mode)
  1160. stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
  1161. if(f->conf.set_uid)
  1162. stbuf->st_uid = f->conf.uid;
  1163. if(f->conf.set_gid)
  1164. stbuf->st_gid = f->conf.gid;
  1165. }
  1166. static
  1167. struct fuse*
  1168. req_fuse(fuse_req_t req)
  1169. {
  1170. return (struct fuse*)fuse_req_userdata(req);
  1171. }
  1172. int
  1173. fuse_fs_getattr(struct fuse_fs *fs,
  1174. const char *path,
  1175. struct stat *buf,
  1176. fuse_timeouts_t *timeout)
  1177. {
  1178. return fs->op.getattr(path,buf,timeout);
  1179. }
  1180. int
  1181. fuse_fs_fgetattr(struct fuse_fs *fs,
  1182. struct stat *buf,
  1183. fuse_file_info_t *fi,
  1184. fuse_timeouts_t *timeout)
  1185. {
  1186. return fs->op.fgetattr(fi,buf,timeout);
  1187. }
  1188. int
  1189. fuse_fs_rename(struct fuse_fs *fs,
  1190. const char *oldpath,
  1191. const char *newpath)
  1192. {
  1193. return fs->op.rename(oldpath,newpath);
  1194. }
  1195. int
  1196. fuse_fs_prepare_hide(struct fuse_fs *fs_,
  1197. const char *path_,
  1198. uint64_t *fh_)
  1199. {
  1200. return fs_->op.prepare_hide(path_,fh_);
  1201. }
  1202. int
  1203. fuse_fs_free_hide(struct fuse_fs *fs_,
  1204. uint64_t fh_)
  1205. {
  1206. return fs_->op.free_hide(fh_);
  1207. }
  1208. int
  1209. fuse_fs_unlink(struct fuse_fs *fs,
  1210. const char *path)
  1211. {
  1212. return fs->op.unlink(path);
  1213. }
  1214. int
  1215. fuse_fs_rmdir(struct fuse_fs *fs,
  1216. const char *path)
  1217. {
  1218. return fs->op.rmdir(path);
  1219. }
  1220. int
  1221. fuse_fs_symlink(struct fuse_fs *fs_,
  1222. const char *linkname_,
  1223. const char *path_,
  1224. struct stat *st_,
  1225. fuse_timeouts_t *timeouts_)
  1226. {
  1227. return fs_->op.symlink(linkname_,path_,st_,timeouts_);
  1228. }
  1229. int
  1230. fuse_fs_link(struct fuse_fs *fs,
  1231. const char *oldpath,
  1232. const char *newpath,
  1233. struct stat *st_,
  1234. fuse_timeouts_t *timeouts_)
  1235. {
  1236. return fs->op.link(oldpath,newpath,st_,timeouts_);
  1237. }
  1238. int
  1239. fuse_fs_release(struct fuse_fs *fs,
  1240. fuse_file_info_t *fi)
  1241. {
  1242. return fs->op.release(fi);
  1243. }
  1244. int
  1245. fuse_fs_opendir(struct fuse_fs *fs,
  1246. const char *path,
  1247. fuse_file_info_t *fi)
  1248. {
  1249. return fs->op.opendir(path,fi);
  1250. }
  1251. int
  1252. fuse_fs_open(struct fuse_fs *fs,
  1253. const char *path,
  1254. fuse_file_info_t *fi)
  1255. {
  1256. return fs->op.open(path,fi);
  1257. }
  1258. static
  1259. void
  1260. fuse_free_buf(struct fuse_bufvec *buf)
  1261. {
  1262. if(buf != NULL)
  1263. {
  1264. size_t i;
  1265. for(i = 0; i < buf->count; i++)
  1266. free(buf->buf[i].mem);
  1267. free(buf);
  1268. }
  1269. }
  1270. int
  1271. fuse_fs_read_buf(struct fuse_fs *fs,
  1272. struct fuse_bufvec **bufp,
  1273. size_t size,
  1274. off_t off,
  1275. fuse_file_info_t *fi)
  1276. {
  1277. int res;
  1278. res = fs->op.read_buf(fi,bufp,size,off);
  1279. if(res < 0)
  1280. return res;
  1281. return 0;
  1282. }
  1283. int
  1284. fuse_fs_write_buf(struct fuse_fs *fs,
  1285. struct fuse_bufvec *buf,
  1286. off_t off,
  1287. fuse_file_info_t *fi)
  1288. {
  1289. return fs->op.write_buf(fi,buf,off);
  1290. }
  1291. int
  1292. fuse_fs_fsync(struct fuse_fs *fs,
  1293. int datasync,
  1294. fuse_file_info_t *fi)
  1295. {
  1296. return fs->op.fsync(fi,datasync);
  1297. }
  1298. int
  1299. fuse_fs_fsyncdir(struct fuse_fs *fs,
  1300. int datasync,
  1301. fuse_file_info_t *fi)
  1302. {
  1303. return fs->op.fsyncdir(fi,datasync);
  1304. }
  1305. int
  1306. fuse_fs_flush(struct fuse_fs *fs,
  1307. fuse_file_info_t *fi)
  1308. {
  1309. return fs->op.flush(fi);
  1310. }
  1311. int
  1312. fuse_fs_statfs(struct fuse_fs *fs,
  1313. const char *path,
  1314. struct statvfs *buf)
  1315. {
  1316. return fs->op.statfs(path,buf);
  1317. }
  1318. int
  1319. fuse_fs_releasedir(struct fuse_fs *fs,
  1320. fuse_file_info_t *fi)
  1321. {
  1322. return fs->op.releasedir(fi);
  1323. }
  1324. int
  1325. fuse_fs_readdir(struct fuse_fs *fs,
  1326. fuse_file_info_t *fi,
  1327. fuse_dirents_t *buf)
  1328. {
  1329. return fs->op.readdir(fi,buf);
  1330. }
  1331. int
  1332. fuse_fs_readdir_plus(struct fuse_fs *fs_,
  1333. fuse_file_info_t *ffi_,
  1334. fuse_dirents_t *buf_)
  1335. {
  1336. return fs_->op.readdir_plus(ffi_,buf_);
  1337. }
  1338. int
  1339. fuse_fs_create(struct fuse_fs *fs,
  1340. const char *path,
  1341. mode_t mode,
  1342. fuse_file_info_t *fi)
  1343. {
  1344. return fs->op.create(path,mode,fi);
  1345. }
  1346. int
  1347. fuse_fs_lock(struct fuse_fs *fs,
  1348. fuse_file_info_t *fi,
  1349. int cmd,
  1350. struct flock *lock)
  1351. {
  1352. return fs->op.lock(fi,cmd,lock);
  1353. }
  1354. int
  1355. fuse_fs_flock(struct fuse_fs *fs,
  1356. fuse_file_info_t *fi,
  1357. int op)
  1358. {
  1359. return fs->op.flock(fi,op);
  1360. }
  1361. int
  1362. fuse_fs_chown(struct fuse_fs *fs,
  1363. const char *path,
  1364. uid_t uid,
  1365. gid_t gid)
  1366. {
  1367. return fs->op.chown(path,uid,gid);
  1368. }
  1369. int
  1370. fuse_fs_fchown(struct fuse_fs *fs_,
  1371. const fuse_file_info_t *ffi_,
  1372. const uid_t uid_,
  1373. const gid_t gid_)
  1374. {
  1375. return fs_->op.fchown(ffi_,uid_,gid_);
  1376. }
  1377. int
  1378. fuse_fs_truncate(struct fuse_fs *fs,
  1379. const char *path,
  1380. off_t size)
  1381. {
  1382. return fs->op.truncate(path,size);
  1383. }
  1384. int
  1385. fuse_fs_ftruncate(struct fuse_fs *fs,
  1386. off_t size,
  1387. fuse_file_info_t *fi)
  1388. {
  1389. return fs->op.ftruncate(fi,size);
  1390. }
  1391. int
  1392. fuse_fs_utimens(struct fuse_fs *fs,
  1393. const char *path,
  1394. const struct timespec tv[2])
  1395. {
  1396. return fs->op.utimens(path,tv);
  1397. }
  1398. int
  1399. fuse_fs_futimens(struct fuse_fs *fs_,
  1400. const fuse_file_info_t *ffi_,
  1401. const struct timespec tv_[2])
  1402. {
  1403. return fs_->op.futimens(ffi_,tv_);
  1404. }
  1405. int
  1406. fuse_fs_access(struct fuse_fs *fs,
  1407. const char *path,
  1408. int mask)
  1409. {
  1410. return fs->op.access(path,mask);
  1411. }
  1412. int
  1413. fuse_fs_readlink(struct fuse_fs *fs,
  1414. const char *path,
  1415. char *buf,
  1416. size_t len)
  1417. {
  1418. return fs->op.readlink(path,buf,len);
  1419. }
  1420. int
  1421. fuse_fs_mknod(struct fuse_fs *fs,
  1422. const char *path,
  1423. mode_t mode,
  1424. dev_t rdev)
  1425. {
  1426. return fs->op.mknod(path,mode,rdev);
  1427. }
  1428. int
  1429. fuse_fs_mkdir(struct fuse_fs *fs,
  1430. const char *path,
  1431. mode_t mode)
  1432. {
  1433. return fs->op.mkdir(path,mode);
  1434. }
  1435. int
  1436. fuse_fs_setxattr(struct fuse_fs *fs,
  1437. const char *path,
  1438. const char *name,
  1439. const char *value,
  1440. size_t size,
  1441. int flags)
  1442. {
  1443. return fs->op.setxattr(path,name,value,size,flags);
  1444. }
  1445. int
  1446. fuse_fs_getxattr(struct fuse_fs *fs,
  1447. const char *path,
  1448. const char *name,
  1449. char *value,
  1450. size_t size)
  1451. {
  1452. return fs->op.getxattr(path,name,value,size);
  1453. }
  1454. int
  1455. fuse_fs_listxattr(struct fuse_fs *fs,
  1456. const char *path,
  1457. char *list,
  1458. size_t size)
  1459. {
  1460. return fs->op.listxattr(path,list,size);
  1461. }
  1462. int
  1463. fuse_fs_bmap(struct fuse_fs *fs,
  1464. const char *path,
  1465. size_t blocksize,
  1466. uint64_t *idx)
  1467. {
  1468. return fs->op.bmap(path,blocksize,idx);
  1469. }
  1470. int
  1471. fuse_fs_removexattr(struct fuse_fs *fs,
  1472. const char *path,
  1473. const char *name)
  1474. {
  1475. return fs->op.removexattr(path,name);
  1476. }
  1477. int
  1478. fuse_fs_ioctl(struct fuse_fs *fs,
  1479. unsigned long cmd,
  1480. void *arg,
  1481. fuse_file_info_t *fi,
  1482. unsigned int flags,
  1483. void *data,
  1484. uint32_t *out_size)
  1485. {
  1486. return fs->op.ioctl(fi,cmd,arg,flags,data,out_size);
  1487. }
  1488. int
  1489. fuse_fs_poll(struct fuse_fs *fs,
  1490. fuse_file_info_t *fi,
  1491. fuse_pollhandle_t *ph,
  1492. unsigned *reventsp)
  1493. {
  1494. return fs->op.poll(fi,ph,reventsp);
  1495. }
  1496. int
  1497. fuse_fs_fallocate(struct fuse_fs *fs,
  1498. int mode,
  1499. off_t offset,
  1500. off_t length,
  1501. fuse_file_info_t *fi)
  1502. {
  1503. return fs->op.fallocate(fi,mode,offset,length);
  1504. }
  1505. ssize_t
  1506. fuse_fs_copy_file_range(struct fuse_fs *fs_,
  1507. fuse_file_info_t *ffi_in_,
  1508. off_t off_in_,
  1509. fuse_file_info_t *ffi_out_,
  1510. off_t off_out_,
  1511. size_t len_,
  1512. int flags_)
  1513. {
  1514. return fs_->op.copy_file_range(ffi_in_,
  1515. off_in_,
  1516. ffi_out_,
  1517. off_out_,
  1518. len_,
  1519. flags_);
  1520. }
  1521. static
  1522. int
  1523. node_open(const struct node *node_)
  1524. {
  1525. return ((node_ != NULL) && (node_->open_count > 0));
  1526. }
  1527. static
  1528. void
  1529. update_stat(struct node *node_,
  1530. const struct stat *stnew_)
  1531. {
  1532. if((node_->stat_cache_valid) &&
  1533. ((node_->mtim.tv_sec != stnew_->st_mtim.tv_sec) ||
  1534. (node_->mtim.tv_nsec != stnew_->st_mtim.tv_nsec) ||
  1535. (node_->ino != stnew_->st_ino) ||
  1536. (node_->size != stnew_->st_size)))
  1537. node_->stat_cache_valid = 0;
  1538. node_->ino = stnew_->st_ino;
  1539. node_->size = stnew_->st_size;
  1540. node_->mtim = stnew_->st_mtim;
  1541. }
  1542. static
  1543. int
  1544. set_path_info(struct fuse *f,
  1545. fuse_ino_t nodeid,
  1546. const char *name,
  1547. struct fuse_entry_param *e)
  1548. {
  1549. struct node *node;
  1550. node = find_node(f,nodeid,name);
  1551. if(node == NULL)
  1552. return -ENOMEM;
  1553. e->ino = node->nodeid;
  1554. e->generation = node->generation;
  1555. pthread_mutex_lock(&f->lock);
  1556. update_stat(node,&e->attr);
  1557. pthread_mutex_unlock(&f->lock);
  1558. set_stat(f,e->ino,&e->attr);
  1559. return 0;
  1560. }
  1561. static
  1562. int
  1563. lookup_path(struct fuse *f,
  1564. fuse_ino_t nodeid,
  1565. const char *name,
  1566. const char *path,
  1567. struct fuse_entry_param *e,
  1568. fuse_file_info_t *fi)
  1569. {
  1570. int rv;
  1571. memset(e,0,sizeof(struct fuse_entry_param));
  1572. rv = ((fi == NULL) ?
  1573. fuse_fs_getattr(f->fs,path,&e->attr,&e->timeout) :
  1574. fuse_fs_fgetattr(f->fs,&e->attr,fi,&e->timeout));
  1575. if(rv)
  1576. return rv;
  1577. return set_path_info(f,nodeid,name,e);
  1578. }
  1579. static
  1580. struct fuse_context_i*
  1581. fuse_get_context_internal(void)
  1582. {
  1583. struct fuse_context_i *c;
  1584. c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
  1585. if(c == NULL)
  1586. {
  1587. c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
  1588. if(c == NULL)
  1589. {
  1590. /* This is hard to deal with properly,so just
  1591. abort. If memory is so low that the
  1592. context cannot be allocated,there's not
  1593. much hope for the filesystem anyway */
  1594. fprintf(stderr,"fuse: failed to allocate thread specific data\n");
  1595. abort();
  1596. }
  1597. pthread_setspecific(fuse_context_key,c);
  1598. }
  1599. return c;
  1600. }
  1601. static
  1602. void
  1603. fuse_freecontext(void *data)
  1604. {
  1605. free(data);
  1606. }
  1607. static
  1608. int
  1609. fuse_create_context_key(void)
  1610. {
  1611. int err = 0;
  1612. pthread_mutex_lock(&fuse_context_lock);
  1613. if(!fuse_context_ref)
  1614. {
  1615. err = pthread_key_create(&fuse_context_key,fuse_freecontext);
  1616. if(err)
  1617. {
  1618. fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
  1619. strerror(err));
  1620. pthread_mutex_unlock(&fuse_context_lock);
  1621. return -1;
  1622. }
  1623. }
  1624. fuse_context_ref++;
  1625. pthread_mutex_unlock(&fuse_context_lock);
  1626. return 0;
  1627. }
  1628. static
  1629. void
  1630. fuse_delete_context_key(void)
  1631. {
  1632. pthread_mutex_lock(&fuse_context_lock);
  1633. fuse_context_ref--;
  1634. if(!fuse_context_ref)
  1635. {
  1636. free(pthread_getspecific(fuse_context_key));
  1637. pthread_key_delete(fuse_context_key);
  1638. }
  1639. pthread_mutex_unlock(&fuse_context_lock);
  1640. }
  1641. static
  1642. struct fuse*
  1643. req_fuse_prepare(fuse_req_t req)
  1644. {
  1645. struct fuse_context_i *c = fuse_get_context_internal();
  1646. const struct fuse_ctx *ctx = fuse_req_ctx(req);
  1647. c->req = req;
  1648. c->ctx.fuse = req_fuse(req);
  1649. c->ctx.uid = ctx->uid;
  1650. c->ctx.gid = ctx->gid;
  1651. c->ctx.pid = ctx->pid;
  1652. c->ctx.umask = ctx->umask;
  1653. return c->ctx.fuse;
  1654. }
  1655. static
  1656. inline
  1657. void
  1658. reply_err(fuse_req_t req,
  1659. int err)
  1660. {
  1661. /* fuse_reply_err() uses non-negated errno values */
  1662. fuse_reply_err(req,-err);
  1663. }
  1664. static
  1665. void
  1666. reply_entry(fuse_req_t req,
  1667. const struct fuse_entry_param *e,
  1668. int err)
  1669. {
  1670. if(!err)
  1671. {
  1672. struct fuse *f = req_fuse(req);
  1673. if(fuse_reply_entry(req,e) == -ENOENT)
  1674. {
  1675. /* Skip forget for negative result */
  1676. if(e->ino != 0)
  1677. forget_node(f,e->ino,1);
  1678. }
  1679. }
  1680. else
  1681. {
  1682. reply_err(req,err);
  1683. }
  1684. }
  1685. void
  1686. fuse_fs_init(struct fuse_fs *fs,
  1687. struct fuse_conn_info *conn)
  1688. {
  1689. fs->op.init(conn);
  1690. }
  1691. static
  1692. void
  1693. fuse_lib_init(void *data,
  1694. struct fuse_conn_info *conn)
  1695. {
  1696. struct fuse *f = (struct fuse *)data;
  1697. struct fuse_context_i *c = fuse_get_context_internal();
  1698. memset(c,0,sizeof(*c));
  1699. c->ctx.fuse = f;
  1700. conn->want |= FUSE_CAP_EXPORT_SUPPORT;
  1701. fuse_fs_init(f->fs,conn);
  1702. }
  1703. void
  1704. fuse_fs_destroy(struct fuse_fs *fs)
  1705. {
  1706. if(fs->op.destroy)
  1707. fs->op.destroy();
  1708. free(fs);
  1709. }
  1710. static
  1711. void
  1712. fuse_lib_destroy(void *data)
  1713. {
  1714. struct fuse *f = (struct fuse *)data;
  1715. struct fuse_context_i *c = fuse_get_context_internal();
  1716. memset(c,0,sizeof(*c));
  1717. c->ctx.fuse = f;
  1718. fuse_fs_destroy(f->fs);
  1719. f->fs = NULL;
  1720. }
  1721. static
  1722. void
  1723. fuse_lib_lookup(fuse_req_t req,
  1724. fuse_ino_t parent,
  1725. const char *name)
  1726. {
  1727. struct fuse *f = req_fuse_prepare(req);
  1728. struct fuse_entry_param e;
  1729. char *path;
  1730. int err;
  1731. struct node *dot = NULL;
  1732. if(name[0] == '.')
  1733. {
  1734. if(name[1] == '\0')
  1735. {
  1736. name = NULL;
  1737. pthread_mutex_lock(&f->lock);
  1738. dot = get_node_nocheck(f,parent);
  1739. if(dot == NULL)
  1740. {
  1741. pthread_mutex_unlock(&f->lock);
  1742. reply_entry(req,&e,-ESTALE);
  1743. return;
  1744. }
  1745. dot->refctr++;
  1746. pthread_mutex_unlock(&f->lock);
  1747. }
  1748. else if((name[1] == '.') && (name[2] == '\0'))
  1749. {
  1750. name = NULL;
  1751. pthread_mutex_lock(&f->lock);
  1752. parent = get_node(f,parent)->parent->nodeid;
  1753. pthread_mutex_unlock(&f->lock);
  1754. }
  1755. }
  1756. err = get_path_name(f,parent,name,&path);
  1757. if(!err)
  1758. {
  1759. err = lookup_path(f,parent,name,path,&e,NULL);
  1760. if(err == -ENOENT)
  1761. {
  1762. e.ino = 0;
  1763. err = 0;
  1764. }
  1765. free_path(f,parent,path);
  1766. }
  1767. if(dot)
  1768. {
  1769. pthread_mutex_lock(&f->lock);
  1770. unref_node(f,dot);
  1771. pthread_mutex_unlock(&f->lock);
  1772. }
  1773. reply_entry(req,&e,err);
  1774. }
  1775. static
  1776. void
  1777. do_forget(struct fuse *f,
  1778. const fuse_ino_t ino,
  1779. const uint64_t nlookup)
  1780. {
  1781. forget_node(f,ino,nlookup);
  1782. }
  1783. static
  1784. void
  1785. fuse_lib_forget(fuse_req_t req,
  1786. const fuse_ino_t ino,
  1787. const uint64_t nlookup)
  1788. {
  1789. do_forget(req_fuse(req),ino,nlookup);
  1790. fuse_reply_none(req);
  1791. }
  1792. static
  1793. void
  1794. fuse_lib_forget_multi(fuse_req_t req,
  1795. size_t count,
  1796. struct fuse_forget_data *forgets)
  1797. {
  1798. struct fuse *f = req_fuse(req);
  1799. size_t i;
  1800. for(i = 0; i < count; i++)
  1801. do_forget(f,forgets[i].ino,forgets[i].nlookup);
  1802. fuse_reply_none(req);
  1803. }
  1804. static
  1805. void
  1806. fuse_lib_getattr(fuse_req_t req,
  1807. fuse_ino_t ino,
  1808. fuse_file_info_t *fi)
  1809. {
  1810. int err;
  1811. char *path;
  1812. struct fuse *f;
  1813. struct stat buf;
  1814. struct node *node;
  1815. fuse_timeouts_t timeout;
  1816. fuse_file_info_t ffi = {0};
  1817. f = req_fuse_prepare(req);
  1818. if(fi == NULL)
  1819. {
  1820. pthread_mutex_lock(&f->lock);
  1821. node = get_node(f,ino);
  1822. if(node->is_hidden)
  1823. {
  1824. fi = &ffi;
  1825. fi->fh = node->hidden_fh;
  1826. }
  1827. pthread_mutex_unlock(&f->lock);
  1828. }
  1829. memset(&buf,0,sizeof(buf));
  1830. err = 0;
  1831. path = NULL;
  1832. if(fi == NULL)
  1833. err = get_path(f,ino,&path);
  1834. if(!err)
  1835. {
  1836. err = ((fi == NULL) ?
  1837. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  1838. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  1839. free_path(f,ino,path);
  1840. }
  1841. if(!err)
  1842. {
  1843. pthread_mutex_lock(&f->lock);
  1844. node = get_node(f,ino);
  1845. update_stat(node,&buf);
  1846. pthread_mutex_unlock(&f->lock);
  1847. set_stat(f,ino,&buf);
  1848. fuse_reply_attr(req,&buf,timeout.attr);
  1849. }
  1850. else
  1851. {
  1852. reply_err(req,err);
  1853. }
  1854. }
  1855. int
  1856. fuse_fs_chmod(struct fuse_fs *fs,
  1857. const char *path,
  1858. mode_t mode)
  1859. {
  1860. return fs->op.chmod(path,mode);
  1861. }
  1862. int
  1863. fuse_fs_fchmod(struct fuse_fs *fs_,
  1864. const fuse_file_info_t *ffi_,
  1865. const mode_t mode_)
  1866. {
  1867. return fs_->op.fchmod(ffi_,mode_);
  1868. }
  1869. static
  1870. void
  1871. fuse_lib_setattr(fuse_req_t req,
  1872. fuse_ino_t ino,
  1873. struct stat *attr,
  1874. int valid,
  1875. fuse_file_info_t *fi)
  1876. {
  1877. struct fuse *f = req_fuse_prepare(req);
  1878. struct stat buf;
  1879. char *path;
  1880. int err;
  1881. struct node *node;
  1882. fuse_timeouts_t timeout;
  1883. fuse_file_info_t ffi = {0};
  1884. if(fi == NULL)
  1885. {
  1886. pthread_mutex_lock(&f->lock);
  1887. node = get_node(f,ino);
  1888. if(node->is_hidden)
  1889. {
  1890. fi = &ffi;
  1891. fi->fh = node->hidden_fh;
  1892. }
  1893. pthread_mutex_unlock(&f->lock);
  1894. }
  1895. memset(&buf,0,sizeof(buf));
  1896. err = 0;
  1897. path = NULL;
  1898. if(fi == NULL)
  1899. err = get_path(f,ino,&path);
  1900. if(!err)
  1901. {
  1902. err = 0;
  1903. if(!err && (valid & FATTR_MODE))
  1904. err = ((fi == NULL) ?
  1905. fuse_fs_chmod(f->fs,path,attr->st_mode) :
  1906. fuse_fs_fchmod(f->fs,fi,attr->st_mode));
  1907. if(!err && (valid & (FATTR_UID | FATTR_GID)))
  1908. {
  1909. uid_t uid = ((valid & FATTR_UID) ? attr->st_uid : (uid_t)-1);
  1910. gid_t gid = ((valid & FATTR_GID) ? attr->st_gid : (gid_t)-1);
  1911. err = ((fi == NULL) ?
  1912. fuse_fs_chown(f->fs,path,uid,gid) :
  1913. fuse_fs_fchown(f->fs,fi,uid,gid));
  1914. }
  1915. if(!err && (valid & FATTR_SIZE))
  1916. err = ((fi == NULL) ?
  1917. fuse_fs_truncate(f->fs,path,attr->st_size) :
  1918. fuse_fs_ftruncate(f->fs,attr->st_size,fi));
  1919. #ifdef HAVE_UTIMENSAT
  1920. if(!err && (valid & (FATTR_ATIME | FATTR_MTIME)))
  1921. {
  1922. struct timespec tv[2];
  1923. tv[0].tv_sec = 0;
  1924. tv[1].tv_sec = 0;
  1925. tv[0].tv_nsec = UTIME_OMIT;
  1926. tv[1].tv_nsec = UTIME_OMIT;
  1927. if(valid & FATTR_ATIME_NOW)
  1928. tv[0].tv_nsec = UTIME_NOW;
  1929. else if(valid & FATTR_ATIME)
  1930. tv[0] = attr->st_atim;
  1931. if(valid & FATTR_MTIME_NOW)
  1932. tv[1].tv_nsec = UTIME_NOW;
  1933. else if(valid & FATTR_MTIME)
  1934. tv[1] = attr->st_mtim;
  1935. err = ((fi == NULL) ?
  1936. fuse_fs_utimens(f->fs,path,tv) :
  1937. fuse_fs_futimens(f->fs,fi,tv));
  1938. }
  1939. else
  1940. #endif
  1941. if(!err && ((valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
  1942. {
  1943. struct timespec tv[2];
  1944. tv[0].tv_sec = attr->st_atime;
  1945. tv[0].tv_nsec = ST_ATIM_NSEC(attr);
  1946. tv[1].tv_sec = attr->st_mtime;
  1947. tv[1].tv_nsec = ST_MTIM_NSEC(attr);
  1948. err = ((fi == NULL) ?
  1949. fuse_fs_utimens(f->fs,path,tv) :
  1950. fuse_fs_futimens(f->fs,fi,tv));
  1951. }
  1952. if(!err)
  1953. err = ((fi == NULL) ?
  1954. fuse_fs_getattr(f->fs,path,&buf,&timeout) :
  1955. fuse_fs_fgetattr(f->fs,&buf,fi,&timeout));
  1956. free_path(f,ino,path);
  1957. }
  1958. if(!err)
  1959. {
  1960. pthread_mutex_lock(&f->lock);
  1961. update_stat(get_node(f,ino),&buf);
  1962. pthread_mutex_unlock(&f->lock);
  1963. set_stat(f,ino,&buf);
  1964. fuse_reply_attr(req,&buf,timeout.attr);
  1965. }
  1966. else
  1967. {
  1968. reply_err(req,err);
  1969. }
  1970. }
  1971. static
  1972. void
  1973. fuse_lib_access(fuse_req_t req,
  1974. fuse_ino_t ino,
  1975. int mask)
  1976. {
  1977. struct fuse *f = req_fuse_prepare(req);
  1978. char *path;
  1979. int err;
  1980. err = get_path(f,ino,&path);
  1981. if(!err)
  1982. {
  1983. err = fuse_fs_access(f->fs,path,mask);
  1984. free_path(f,ino,path);
  1985. }
  1986. reply_err(req,err);
  1987. }
  1988. static
  1989. void
  1990. fuse_lib_readlink(fuse_req_t req,
  1991. fuse_ino_t ino)
  1992. {
  1993. struct fuse *f = req_fuse_prepare(req);
  1994. char linkname[PATH_MAX + 1];
  1995. char *path;
  1996. int err;
  1997. err = get_path(f,ino,&path);
  1998. if(!err)
  1999. {
  2000. err = fuse_fs_readlink(f->fs,path,linkname,sizeof(linkname));
  2001. free_path(f,ino,path);
  2002. }
  2003. if(!err)
  2004. {
  2005. linkname[PATH_MAX] = '\0';
  2006. fuse_reply_readlink(req,linkname);
  2007. }
  2008. else
  2009. {
  2010. reply_err(req,err);
  2011. }
  2012. }
  2013. static
  2014. void
  2015. fuse_lib_mknod(fuse_req_t req,
  2016. fuse_ino_t parent,
  2017. const char *name,
  2018. mode_t mode,
  2019. dev_t rdev)
  2020. {
  2021. struct fuse *f = req_fuse_prepare(req);
  2022. struct fuse_entry_param e;
  2023. char *path;
  2024. int err;
  2025. err = get_path_name(f,parent,name,&path);
  2026. if(!err)
  2027. {
  2028. err = -ENOSYS;
  2029. if(S_ISREG(mode))
  2030. {
  2031. fuse_file_info_t fi;
  2032. memset(&fi,0,sizeof(fi));
  2033. fi.flags = O_CREAT | O_EXCL | O_WRONLY;
  2034. err = fuse_fs_create(f->fs,path,mode,&fi);
  2035. if(!err)
  2036. {
  2037. err = lookup_path(f,parent,name,path,&e,
  2038. &fi);
  2039. fuse_fs_release(f->fs,&fi);
  2040. }
  2041. }
  2042. if(err == -ENOSYS)
  2043. {
  2044. err = fuse_fs_mknod(f->fs,path,mode,rdev);
  2045. if(!err)
  2046. err = lookup_path(f,parent,name,path,&e,NULL);
  2047. }
  2048. free_path(f,parent,path);
  2049. }
  2050. reply_entry(req,&e,err);
  2051. }
  2052. static
  2053. void
  2054. fuse_lib_mkdir(fuse_req_t req,
  2055. fuse_ino_t parent,
  2056. const char *name,
  2057. mode_t mode)
  2058. {
  2059. struct fuse *f = req_fuse_prepare(req);
  2060. struct fuse_entry_param e;
  2061. char *path;
  2062. int err;
  2063. err = get_path_name(f,parent,name,&path);
  2064. if(!err)
  2065. {
  2066. err = fuse_fs_mkdir(f->fs,path,mode);
  2067. if(!err)
  2068. err = lookup_path(f,parent,name,path,&e,NULL);
  2069. free_path(f,parent,path);
  2070. }
  2071. reply_entry(req,&e,err);
  2072. }
  2073. static
  2074. void
  2075. fuse_lib_unlink(fuse_req_t req,
  2076. fuse_ino_t parent,
  2077. const char *name)
  2078. {
  2079. int err;
  2080. char *path;
  2081. struct fuse *f;
  2082. struct node *wnode;
  2083. f = req_fuse_prepare(req);
  2084. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2085. if(!err)
  2086. {
  2087. pthread_mutex_lock(&f->lock);
  2088. if(node_open(wnode))
  2089. {
  2090. err = fuse_fs_prepare_hide(f->fs,path,&wnode->hidden_fh);
  2091. if(!err)
  2092. wnode->is_hidden = 1;
  2093. }
  2094. pthread_mutex_unlock(&f->lock);
  2095. err = fuse_fs_unlink(f->fs,path);
  2096. if(!err)
  2097. remove_node(f,parent,name);
  2098. free_path_wrlock(f,parent,wnode,path);
  2099. }
  2100. reply_err(req,err);
  2101. }
  2102. static
  2103. void
  2104. fuse_lib_rmdir(fuse_req_t req,
  2105. fuse_ino_t parent,
  2106. const char *name)
  2107. {
  2108. struct fuse *f = req_fuse_prepare(req);
  2109. struct node *wnode;
  2110. char *path;
  2111. int err;
  2112. err = get_path_wrlock(f,parent,name,&path,&wnode);
  2113. if(!err)
  2114. {
  2115. err = fuse_fs_rmdir(f->fs,path);
  2116. if(!err)
  2117. remove_node(f,parent,name);
  2118. free_path_wrlock(f,parent,wnode,path);
  2119. }
  2120. reply_err(req,err);
  2121. }
  2122. static
  2123. void
  2124. fuse_lib_symlink(fuse_req_t req_,
  2125. const char *linkname_,
  2126. fuse_ino_t parent_,
  2127. const char *name_)
  2128. {
  2129. int rv;
  2130. char *path;
  2131. struct fuse *f;
  2132. struct fuse_entry_param e = {0};
  2133. f = req_fuse_prepare(req_);
  2134. rv = get_path_name(f,parent_,name_,&path);
  2135. if(rv == 0)
  2136. {
  2137. rv = fuse_fs_symlink(f->fs,linkname_,path,&e.attr,&e.timeout);
  2138. if(rv == 0)
  2139. rv = set_path_info(f,parent_,name_,&e);
  2140. free_path(f,parent_,path);
  2141. }
  2142. reply_entry(req_,&e,rv);
  2143. }
  2144. static
  2145. void
  2146. fuse_lib_rename(fuse_req_t req,
  2147. fuse_ino_t olddir,
  2148. const char *oldname,
  2149. fuse_ino_t newdir,
  2150. const char *newname)
  2151. {
  2152. int err;
  2153. struct fuse *f;
  2154. char *oldpath;
  2155. char *newpath;
  2156. struct node *wnode1;
  2157. struct node *wnode2;
  2158. f = req_fuse_prepare(req);
  2159. err = get_path2(f,olddir,oldname,newdir,newname,
  2160. &oldpath,&newpath,&wnode1,&wnode2);
  2161. if(!err)
  2162. {
  2163. pthread_mutex_lock(&f->lock);
  2164. if(node_open(wnode2))
  2165. {
  2166. err = fuse_fs_prepare_hide(f->fs,newpath,&wnode2->hidden_fh);
  2167. if(!err)
  2168. wnode2->is_hidden = 1;
  2169. }
  2170. pthread_mutex_unlock(&f->lock);
  2171. err = fuse_fs_rename(f->fs,oldpath,newpath);
  2172. if(!err)
  2173. err = rename_node(f,olddir,oldname,newdir,newname);
  2174. free_path2(f,olddir,newdir,wnode1,wnode2,oldpath,newpath);
  2175. }
  2176. reply_err(req,err);
  2177. }
  2178. static
  2179. void
  2180. fuse_lib_link(fuse_req_t req,
  2181. fuse_ino_t ino,
  2182. fuse_ino_t newparent,
  2183. const char *newname)
  2184. {
  2185. int rv;
  2186. char *oldpath;
  2187. char *newpath;
  2188. struct fuse *f;
  2189. struct fuse_entry_param e = {0};
  2190. f = req_fuse_prepare(req);
  2191. rv = get_path2(f,ino,NULL,newparent,newname,
  2192. &oldpath,&newpath,NULL,NULL);
  2193. if(!rv)
  2194. {
  2195. rv = fuse_fs_link(f->fs,oldpath,newpath,&e.attr,&e.timeout);
  2196. if(rv == 0)
  2197. rv = set_path_info(f,newparent,newname,&e);
  2198. free_path2(f,ino,newparent,NULL,NULL,oldpath,newpath);
  2199. }
  2200. reply_entry(req,&e,rv);
  2201. }
  2202. static
  2203. void
  2204. fuse_do_release(struct fuse *f,
  2205. fuse_ino_t ino,
  2206. fuse_file_info_t *fi)
  2207. {
  2208. struct node *node;
  2209. uint64_t fh;
  2210. int was_hidden;
  2211. fh = 0;
  2212. fuse_fs_release(f->fs,fi);
  2213. pthread_mutex_lock(&f->lock);
  2214. node = get_node(f,ino);
  2215. assert(node->open_count > 0);
  2216. node->open_count--;
  2217. was_hidden = 0;
  2218. if(node->is_hidden && (node->open_count == 0))
  2219. {
  2220. was_hidden = 1;
  2221. node->is_hidden = 0;
  2222. fh = node->hidden_fh;
  2223. }
  2224. pthread_mutex_unlock(&f->lock);
  2225. if(was_hidden)
  2226. fuse_fs_free_hide(f->fs,fh);
  2227. }
  2228. static
  2229. void
  2230. fuse_lib_create(fuse_req_t req,
  2231. fuse_ino_t parent,
  2232. const char *name,
  2233. mode_t mode,
  2234. fuse_file_info_t *fi)
  2235. {
  2236. int err;
  2237. char *path;
  2238. struct fuse *f;
  2239. struct fuse_entry_param e;
  2240. f = req_fuse_prepare(req);
  2241. err = get_path_name(f,parent,name,&path);
  2242. if(!err)
  2243. {
  2244. err = fuse_fs_create(f->fs,path,mode,fi);
  2245. if(!err)
  2246. {
  2247. err = lookup_path(f,parent,name,path,&e,fi);
  2248. if(err)
  2249. {
  2250. fuse_fs_release(f->fs,fi);
  2251. }
  2252. else if(!S_ISREG(e.attr.st_mode))
  2253. {
  2254. err = -EIO;
  2255. fuse_fs_release(f->fs,fi);
  2256. forget_node(f,e.ino,1);
  2257. }
  2258. }
  2259. }
  2260. if(!err)
  2261. {
  2262. pthread_mutex_lock(&f->lock);
  2263. get_node(f,e.ino)->open_count++;
  2264. pthread_mutex_unlock(&f->lock);
  2265. if(fuse_reply_create(req,&e,fi) == -ENOENT)
  2266. {
  2267. /* The open syscall was interrupted,so it
  2268. must be cancelled */
  2269. fuse_do_release(f,e.ino,fi);
  2270. forget_node(f,e.ino,1);
  2271. }
  2272. }
  2273. else
  2274. {
  2275. reply_err(req,err);
  2276. }
  2277. free_path(f,parent,path);
  2278. }
  2279. static
  2280. void
  2281. open_auto_cache(struct fuse *f,
  2282. fuse_ino_t ino,
  2283. const char *path,
  2284. fuse_file_info_t *fi)
  2285. {
  2286. struct node *node;
  2287. fuse_timeouts_t timeout;
  2288. pthread_mutex_lock(&f->lock);
  2289. node = get_node(f,ino);
  2290. if(node->stat_cache_valid)
  2291. {
  2292. int err;
  2293. struct stat stbuf;
  2294. pthread_mutex_unlock(&f->lock);
  2295. err = fuse_fs_fgetattr(f->fs,&stbuf,fi,&timeout);
  2296. pthread_mutex_lock(&f->lock);
  2297. if(!err)
  2298. update_stat(node,&stbuf);
  2299. else
  2300. node->stat_cache_valid = 0;
  2301. }
  2302. if(node->stat_cache_valid)
  2303. fi->keep_cache = 1;
  2304. node->stat_cache_valid = 1;
  2305. pthread_mutex_unlock(&f->lock);
  2306. }
  2307. static
  2308. void
  2309. fuse_lib_open(fuse_req_t req,
  2310. fuse_ino_t ino,
  2311. fuse_file_info_t *fi)
  2312. {
  2313. int err;
  2314. char *path;
  2315. struct fuse *f;
  2316. f = req_fuse_prepare(req);
  2317. err = get_path(f,ino,&path);
  2318. if(!err)
  2319. {
  2320. err = fuse_fs_open(f->fs,path,fi);
  2321. if(!err)
  2322. {
  2323. if(fi && fi->auto_cache)
  2324. open_auto_cache(f,ino,path,fi);
  2325. }
  2326. }
  2327. if(!err)
  2328. {
  2329. pthread_mutex_lock(&f->lock);
  2330. get_node(f,ino)->open_count++;
  2331. pthread_mutex_unlock(&f->lock);
  2332. /* The open syscall was interrupted,so it must be cancelled */
  2333. if(fuse_reply_open(req,fi) == -ENOENT)
  2334. fuse_do_release(f,ino,fi);
  2335. }
  2336. else
  2337. {
  2338. reply_err(req,err);
  2339. }
  2340. free_path(f,ino,path);
  2341. }
  2342. static
  2343. void
  2344. fuse_lib_read(fuse_req_t req,
  2345. fuse_ino_t ino,
  2346. size_t size,
  2347. off_t off,
  2348. fuse_file_info_t *fi)
  2349. {
  2350. struct fuse *f = req_fuse_prepare(req);
  2351. struct fuse_bufvec *buf = NULL;
  2352. int res;
  2353. res = fuse_fs_read_buf(f->fs,&buf,size,off,fi);
  2354. if(res == 0)
  2355. fuse_reply_data(req,buf,FUSE_BUF_SPLICE_MOVE);
  2356. else
  2357. reply_err(req,res);
  2358. fuse_free_buf(buf);
  2359. }
  2360. static
  2361. void
  2362. fuse_lib_write_buf(fuse_req_t req,
  2363. fuse_ino_t ino,
  2364. struct fuse_bufvec *buf,
  2365. off_t off,
  2366. fuse_file_info_t *fi)
  2367. {
  2368. int res;
  2369. struct fuse *f = req_fuse_prepare(req);
  2370. res = fuse_fs_write_buf(f->fs,buf,off,fi);
  2371. free_path(f,ino,NULL);
  2372. if(res >= 0)
  2373. fuse_reply_write(req,res);
  2374. else
  2375. reply_err(req,res);
  2376. }
  2377. static
  2378. void
  2379. fuse_lib_fsync(fuse_req_t req,
  2380. fuse_ino_t ino,
  2381. int datasync,
  2382. fuse_file_info_t *fi)
  2383. {
  2384. int err;
  2385. struct fuse *f = req_fuse_prepare(req);
  2386. err = fuse_fs_fsync(f->fs,datasync,fi);
  2387. reply_err(req,err);
  2388. }
  2389. static
  2390. struct fuse_dh*
  2391. get_dirhandle(const fuse_file_info_t *llfi,
  2392. fuse_file_info_t *fi)
  2393. {
  2394. struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
  2395. memset(fi,0,sizeof(fuse_file_info_t));
  2396. fi->fh = dh->fh;
  2397. return dh;
  2398. }
  2399. static
  2400. void
  2401. fuse_lib_opendir(fuse_req_t req,
  2402. fuse_ino_t ino,
  2403. fuse_file_info_t *llfi)
  2404. {
  2405. int err;
  2406. char *path;
  2407. struct fuse_dh *dh;
  2408. fuse_file_info_t fi;
  2409. struct fuse *f = req_fuse_prepare(req);
  2410. dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
  2411. if(dh == NULL)
  2412. {
  2413. reply_err(req,-ENOMEM);
  2414. return;
  2415. }
  2416. fuse_dirents_init(&dh->d);
  2417. fuse_mutex_init(&dh->lock);
  2418. llfi->fh = (uintptr_t)dh;
  2419. memset(&fi,0,sizeof(fi));
  2420. fi.flags = llfi->flags;
  2421. err = get_path(f,ino,&path);
  2422. if(!err)
  2423. {
  2424. err = fuse_fs_opendir(f->fs,path,&fi);
  2425. dh->fh = fi.fh;
  2426. llfi->keep_cache = fi.keep_cache;
  2427. llfi->cache_readdir = fi.cache_readdir;
  2428. }
  2429. if(!err)
  2430. {
  2431. if(fuse_reply_open(req,llfi) == -ENOENT)
  2432. {
  2433. /* The opendir syscall was interrupted,so it
  2434. must be cancelled */
  2435. fuse_fs_releasedir(f->fs,&fi);
  2436. pthread_mutex_destroy(&dh->lock);
  2437. free(dh);
  2438. }
  2439. }
  2440. else
  2441. {
  2442. reply_err(req,err);
  2443. pthread_mutex_destroy(&dh->lock);
  2444. free(dh);
  2445. }
  2446. free_path(f,ino,path);
  2447. }
  2448. static
  2449. size_t
  2450. readdir_buf_size(fuse_dirents_t *d_,
  2451. size_t size_,
  2452. off_t off_)
  2453. {
  2454. if(off_ >= kv_size(d_->offs))
  2455. return 0;
  2456. if((kv_A(d_->offs,off_) + size_) > kv_size(d_->data))
  2457. return (kv_size(d_->data) - kv_A(d_->offs,off_));
  2458. return size_;
  2459. }
  2460. static
  2461. char*
  2462. readdir_buf(fuse_dirents_t *d_,
  2463. off_t off_)
  2464. {
  2465. size_t i;
  2466. i = kv_A(d_->offs,off_);
  2467. return &kv_A(d_->data,i);
  2468. }
  2469. static
  2470. void
  2471. fuse_lib_readdir(fuse_req_t req_,
  2472. fuse_ino_t ino_,
  2473. size_t size_,
  2474. off_t off_,
  2475. fuse_file_info_t *llffi_)
  2476. {
  2477. int rv;
  2478. struct fuse *f;
  2479. fuse_dirents_t *d;
  2480. struct fuse_dh *dh;
  2481. fuse_file_info_t fi;
  2482. f = req_fuse_prepare(req_);
  2483. dh = get_dirhandle(llffi_,&fi);
  2484. d = &dh->d;
  2485. pthread_mutex_lock(&dh->lock);
  2486. rv = 0;
  2487. if((off_ == 0) || (kv_size(d->data) == 0))
  2488. rv = fuse_fs_readdir(f->fs,&fi,d);
  2489. if(rv)
  2490. {
  2491. reply_err(req_,rv);
  2492. goto out;
  2493. }
  2494. size_ = readdir_buf_size(d,size_,off_);
  2495. fuse_reply_buf(req_,
  2496. readdir_buf(d,off_),
  2497. size_);
  2498. out:
  2499. pthread_mutex_unlock(&dh->lock);
  2500. }
  2501. static
  2502. void
  2503. fuse_lib_readdir_plus(fuse_req_t req_,
  2504. fuse_ino_t ino_,
  2505. size_t size_,
  2506. off_t off_,
  2507. fuse_file_info_t *llffi_)
  2508. {
  2509. int rv;
  2510. struct fuse *f;
  2511. fuse_dirents_t *d;
  2512. struct fuse_dh *dh;
  2513. fuse_file_info_t fi;
  2514. f = req_fuse_prepare(req_);
  2515. dh = get_dirhandle(llffi_,&fi);
  2516. d = &dh->d;
  2517. pthread_mutex_lock(&dh->lock);
  2518. rv = 0;
  2519. if((off_ == 0) || (kv_size(d->data) == 0))
  2520. rv = fuse_fs_readdir_plus(f->fs,&fi,d);
  2521. if(rv)
  2522. {
  2523. reply_err(req_,rv);
  2524. goto out;
  2525. }
  2526. size_ = readdir_buf_size(d,size_,off_);
  2527. fuse_reply_buf(req_,
  2528. readdir_buf(d,off_),
  2529. size_);
  2530. out:
  2531. pthread_mutex_unlock(&dh->lock);
  2532. }
  2533. static
  2534. void
  2535. fuse_lib_releasedir(fuse_req_t req_,
  2536. fuse_ino_t ino_,
  2537. fuse_file_info_t *llfi_)
  2538. {
  2539. struct fuse *f;
  2540. struct fuse_dh *dh;
  2541. fuse_file_info_t fi;
  2542. f = req_fuse_prepare(req_);
  2543. dh = get_dirhandle(llfi_,&fi);
  2544. fuse_fs_releasedir(f->fs,&fi);
  2545. /* Done to keep race condition between last readdir reply and the unlock */
  2546. pthread_mutex_lock(&dh->lock);
  2547. pthread_mutex_unlock(&dh->lock);
  2548. pthread_mutex_destroy(&dh->lock);
  2549. fuse_dirents_free(&dh->d);
  2550. free(dh);
  2551. reply_err(req_,0);
  2552. }
  2553. static
  2554. void
  2555. fuse_lib_fsyncdir(fuse_req_t req,
  2556. fuse_ino_t ino,
  2557. int datasync,
  2558. fuse_file_info_t *llfi)
  2559. {
  2560. int err;
  2561. fuse_file_info_t fi;
  2562. struct fuse *f = req_fuse_prepare(req);
  2563. get_dirhandle(llfi,&fi);
  2564. err = fuse_fs_fsyncdir(f->fs,datasync,&fi);
  2565. reply_err(req,err);
  2566. }
  2567. static
  2568. void
  2569. fuse_lib_statfs(fuse_req_t req,
  2570. fuse_ino_t ino)
  2571. {
  2572. struct fuse *f = req_fuse_prepare(req);
  2573. struct statvfs buf;
  2574. char *path = NULL;
  2575. int err = 0;
  2576. memset(&buf,0,sizeof(buf));
  2577. if(ino)
  2578. err = get_path(f,ino,&path);
  2579. if(!err)
  2580. {
  2581. err = fuse_fs_statfs(f->fs,path ? path : "/",&buf);
  2582. free_path(f,ino,path);
  2583. }
  2584. if(!err)
  2585. fuse_reply_statfs(req,&buf);
  2586. else
  2587. reply_err(req,err);
  2588. }
  2589. static
  2590. void
  2591. fuse_lib_setxattr(fuse_req_t req,
  2592. fuse_ino_t ino,
  2593. const char *name,
  2594. const char *value,
  2595. size_t size,
  2596. int flags)
  2597. {
  2598. struct fuse *f = req_fuse_prepare(req);
  2599. char *path;
  2600. int err;
  2601. err = get_path(f,ino,&path);
  2602. if(!err)
  2603. {
  2604. err = fuse_fs_setxattr(f->fs,path,name,value,size,flags);
  2605. free_path(f,ino,path);
  2606. }
  2607. reply_err(req,err);
  2608. }
  2609. static
  2610. int
  2611. common_getxattr(struct fuse *f,
  2612. fuse_req_t req,
  2613. fuse_ino_t ino,
  2614. const char *name,
  2615. char *value,
  2616. size_t size)
  2617. {
  2618. int err;
  2619. char *path;
  2620. err = get_path(f,ino,&path);
  2621. if(!err)
  2622. {
  2623. err = fuse_fs_getxattr(f->fs,path,name,value,size);
  2624. free_path(f,ino,path);
  2625. }
  2626. return err;
  2627. }
  2628. static
  2629. void
  2630. fuse_lib_getxattr(fuse_req_t req,
  2631. fuse_ino_t ino,
  2632. const char *name,
  2633. size_t size)
  2634. {
  2635. struct fuse *f = req_fuse_prepare(req);
  2636. int res;
  2637. if(size)
  2638. {
  2639. char *value = (char *)malloc(size);
  2640. if(value == NULL)
  2641. {
  2642. reply_err(req,-ENOMEM);
  2643. return;
  2644. }
  2645. res = common_getxattr(f,req,ino,name,value,size);
  2646. if(res > 0)
  2647. fuse_reply_buf(req,value,res);
  2648. else
  2649. reply_err(req,res);
  2650. free(value);
  2651. }
  2652. else
  2653. {
  2654. res = common_getxattr(f,req,ino,name,NULL,0);
  2655. if(res >= 0)
  2656. fuse_reply_xattr(req,res);
  2657. else
  2658. reply_err(req,res);
  2659. }
  2660. }
  2661. static
  2662. int
  2663. common_listxattr(struct fuse *f,
  2664. fuse_req_t req,
  2665. fuse_ino_t ino,
  2666. char *list,
  2667. size_t size)
  2668. {
  2669. char *path;
  2670. int err;
  2671. err = get_path(f,ino,&path);
  2672. if(!err)
  2673. {
  2674. err = fuse_fs_listxattr(f->fs,path,list,size);
  2675. free_path(f,ino,path);
  2676. }
  2677. return err;
  2678. }
  2679. static
  2680. void
  2681. fuse_lib_listxattr(fuse_req_t req,
  2682. fuse_ino_t ino,
  2683. size_t size)
  2684. {
  2685. struct fuse *f = req_fuse_prepare(req);
  2686. int res;
  2687. if(size)
  2688. {
  2689. char *list = (char *)malloc(size);
  2690. if(list == NULL)
  2691. {
  2692. reply_err(req,-ENOMEM);
  2693. return;
  2694. }
  2695. res = common_listxattr(f,req,ino,list,size);
  2696. if(res > 0)
  2697. fuse_reply_buf(req,list,res);
  2698. else
  2699. reply_err(req,res);
  2700. free(list);
  2701. }
  2702. else
  2703. {
  2704. res = common_listxattr(f,req,ino,NULL,0);
  2705. if(res >= 0)
  2706. fuse_reply_xattr(req,res);
  2707. else
  2708. reply_err(req,res);
  2709. }
  2710. }
  2711. static
  2712. void
  2713. fuse_lib_removexattr(fuse_req_t req,
  2714. fuse_ino_t ino,
  2715. const char *name)
  2716. {
  2717. struct fuse *f = req_fuse_prepare(req);
  2718. char *path;
  2719. int err;
  2720. err = get_path(f,ino,&path);
  2721. if(!err)
  2722. {
  2723. err = fuse_fs_removexattr(f->fs,path,name);
  2724. free_path(f,ino,path);
  2725. }
  2726. reply_err(req,err);
  2727. }
  2728. static
  2729. void
  2730. fuse_lib_copy_file_range(fuse_req_t req_,
  2731. fuse_ino_t nodeid_in_,
  2732. off_t off_in_,
  2733. fuse_file_info_t *ffi_in_,
  2734. fuse_ino_t nodeid_out_,
  2735. off_t off_out_,
  2736. fuse_file_info_t *ffi_out_,
  2737. size_t len_,
  2738. int flags_)
  2739. {
  2740. ssize_t rv;
  2741. struct fuse *f;
  2742. f = req_fuse_prepare(req_);
  2743. rv = fuse_fs_copy_file_range(f->fs,
  2744. ffi_in_,
  2745. off_in_,
  2746. ffi_out_,
  2747. off_out_,
  2748. len_,
  2749. flags_);
  2750. if(rv >= 0)
  2751. fuse_reply_write(req_,rv);
  2752. else
  2753. reply_err(req_,rv);
  2754. }
  2755. static
  2756. struct lock*
  2757. locks_conflict(struct node *node,
  2758. const struct lock *lock)
  2759. {
  2760. struct lock *l;
  2761. for(l = node->locks; l; l = l->next)
  2762. if(l->owner != lock->owner &&
  2763. lock->start <= l->end && l->start <= lock->end &&
  2764. (l->type == F_WRLCK || lock->type == F_WRLCK))
  2765. break;
  2766. return l;
  2767. }
  2768. static
  2769. void
  2770. delete_lock(struct lock **lockp)
  2771. {
  2772. struct lock *l = *lockp;
  2773. *lockp = l->next;
  2774. free(l);
  2775. }
  2776. static
  2777. void
  2778. insert_lock(struct lock **pos,
  2779. struct lock *lock)
  2780. {
  2781. lock->next = *pos;
  2782. *pos = lock;
  2783. }
  2784. static
  2785. int
  2786. locks_insert(struct node *node,
  2787. struct lock *lock)
  2788. {
  2789. struct lock **lp;
  2790. struct lock *newl1 = NULL;
  2791. struct lock *newl2 = NULL;
  2792. if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
  2793. {
  2794. newl1 = malloc(sizeof(struct lock));
  2795. newl2 = malloc(sizeof(struct lock));
  2796. if(!newl1 || !newl2)
  2797. {
  2798. free(newl1);
  2799. free(newl2);
  2800. return -ENOLCK;
  2801. }
  2802. }
  2803. for(lp = &node->locks; *lp;)
  2804. {
  2805. struct lock *l = *lp;
  2806. if(l->owner != lock->owner)
  2807. goto skip;
  2808. if(lock->type == l->type)
  2809. {
  2810. if(l->end < lock->start - 1)
  2811. goto skip;
  2812. if(lock->end < l->start - 1)
  2813. break;
  2814. if(l->start <= lock->start && lock->end <= l->end)
  2815. goto out;
  2816. if(l->start < lock->start)
  2817. lock->start = l->start;
  2818. if(lock->end < l->end)
  2819. lock->end = l->end;
  2820. goto delete;
  2821. }
  2822. else
  2823. {
  2824. if(l->end < lock->start)
  2825. goto skip;
  2826. if(lock->end < l->start)
  2827. break;
  2828. if(lock->start <= l->start && l->end <= lock->end)
  2829. goto delete;
  2830. if(l->end <= lock->end)
  2831. {
  2832. l->end = lock->start - 1;
  2833. goto skip;
  2834. }
  2835. if(lock->start <= l->start)
  2836. {
  2837. l->start = lock->end + 1;
  2838. break;
  2839. }
  2840. *newl2 = *l;
  2841. newl2->start = lock->end + 1;
  2842. l->end = lock->start - 1;
  2843. insert_lock(&l->next,newl2);
  2844. newl2 = NULL;
  2845. }
  2846. skip:
  2847. lp = &l->next;
  2848. continue;
  2849. delete:
  2850. delete_lock(lp);
  2851. }
  2852. if(lock->type != F_UNLCK)
  2853. {
  2854. *newl1 = *lock;
  2855. insert_lock(lp,newl1);
  2856. newl1 = NULL;
  2857. }
  2858. out:
  2859. free(newl1);
  2860. free(newl2);
  2861. return 0;
  2862. }
  2863. static
  2864. void
  2865. flock_to_lock(struct flock *flock,
  2866. struct lock *lock)
  2867. {
  2868. memset(lock,0,sizeof(struct lock));
  2869. lock->type = flock->l_type;
  2870. lock->start = flock->l_start;
  2871. lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
  2872. lock->pid = flock->l_pid;
  2873. }
  2874. static
  2875. void
  2876. lock_to_flock(struct lock *lock,
  2877. struct flock *flock)
  2878. {
  2879. flock->l_type = lock->type;
  2880. flock->l_start = lock->start;
  2881. flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
  2882. flock->l_pid = lock->pid;
  2883. }
  2884. static
  2885. int
  2886. fuse_flush_common(struct fuse *f,
  2887. fuse_req_t req,
  2888. fuse_ino_t ino,
  2889. fuse_file_info_t *fi)
  2890. {
  2891. struct flock lock;
  2892. struct lock l;
  2893. int err;
  2894. int errlock;
  2895. memset(&lock,0,sizeof(lock));
  2896. lock.l_type = F_UNLCK;
  2897. lock.l_whence = SEEK_SET;
  2898. err = fuse_fs_flush(f->fs,fi);
  2899. errlock = fuse_fs_lock(f->fs,fi,F_SETLK,&lock);
  2900. if(errlock != -ENOSYS)
  2901. {
  2902. flock_to_lock(&lock,&l);
  2903. l.owner = fi->lock_owner;
  2904. pthread_mutex_lock(&f->lock);
  2905. locks_insert(get_node(f,ino),&l);
  2906. pthread_mutex_unlock(&f->lock);
  2907. /* if op.lock() is defined FLUSH is needed regardless
  2908. of op.flush() */
  2909. if(err == -ENOSYS)
  2910. err = 0;
  2911. }
  2912. return err;
  2913. }
  2914. static
  2915. void
  2916. fuse_lib_release(fuse_req_t req,
  2917. fuse_ino_t ino,
  2918. fuse_file_info_t *fi)
  2919. {
  2920. int err = 0;
  2921. struct fuse *f = req_fuse_prepare(req);
  2922. if(fi->flush)
  2923. {
  2924. err = fuse_flush_common(f,req,ino,fi);
  2925. if(err == -ENOSYS)
  2926. err = 0;
  2927. }
  2928. fuse_do_release(f,ino,fi);
  2929. reply_err(req,err);
  2930. }
  2931. static
  2932. void
  2933. fuse_lib_flush(fuse_req_t req,
  2934. fuse_ino_t ino,
  2935. fuse_file_info_t *fi)
  2936. {
  2937. int err;
  2938. struct fuse *f = req_fuse_prepare(req);
  2939. err = fuse_flush_common(f,req,ino,fi);
  2940. reply_err(req,err);
  2941. }
  2942. static
  2943. int
  2944. fuse_lock_common(fuse_req_t req,
  2945. fuse_ino_t ino,
  2946. fuse_file_info_t *fi,
  2947. struct flock *lock,
  2948. int cmd)
  2949. {
  2950. int err;
  2951. struct fuse *f = req_fuse_prepare(req);
  2952. err = fuse_fs_lock(f->fs,fi,cmd,lock);
  2953. return err;
  2954. }
  2955. static
  2956. void
  2957. fuse_lib_getlk(fuse_req_t req,
  2958. fuse_ino_t ino,
  2959. fuse_file_info_t *fi,
  2960. struct flock *lock)
  2961. {
  2962. int err;
  2963. struct lock l;
  2964. struct lock *conflict;
  2965. struct fuse *f = req_fuse(req);
  2966. flock_to_lock(lock,&l);
  2967. l.owner = fi->lock_owner;
  2968. pthread_mutex_lock(&f->lock);
  2969. conflict = locks_conflict(get_node(f,ino),&l);
  2970. if(conflict)
  2971. lock_to_flock(conflict,lock);
  2972. pthread_mutex_unlock(&f->lock);
  2973. if(!conflict)
  2974. err = fuse_lock_common(req,ino,fi,lock,F_GETLK);
  2975. else
  2976. err = 0;
  2977. if(!err)
  2978. fuse_reply_lock(req,lock);
  2979. else
  2980. reply_err(req,err);
  2981. }
  2982. static
  2983. void
  2984. fuse_lib_setlk(fuse_req_t req,
  2985. fuse_ino_t ino,
  2986. fuse_file_info_t *fi,
  2987. struct flock *lock,
  2988. int sleep)
  2989. {
  2990. int err = fuse_lock_common(req,ino,fi,lock,
  2991. sleep ? F_SETLKW : F_SETLK);
  2992. if(!err)
  2993. {
  2994. struct fuse *f = req_fuse(req);
  2995. struct lock l;
  2996. flock_to_lock(lock,&l);
  2997. l.owner = fi->lock_owner;
  2998. pthread_mutex_lock(&f->lock);
  2999. locks_insert(get_node(f,ino),&l);
  3000. pthread_mutex_unlock(&f->lock);
  3001. }
  3002. reply_err(req,err);
  3003. }
  3004. static
  3005. void
  3006. fuse_lib_flock(fuse_req_t req,
  3007. fuse_ino_t ino,
  3008. fuse_file_info_t *fi,
  3009. int op)
  3010. {
  3011. int err;
  3012. struct fuse *f = req_fuse_prepare(req);
  3013. err = fuse_fs_flock(f->fs,fi,op);
  3014. reply_err(req,err);
  3015. }
  3016. static
  3017. void
  3018. fuse_lib_bmap(fuse_req_t req,
  3019. fuse_ino_t ino,
  3020. size_t blocksize,
  3021. uint64_t idx)
  3022. {
  3023. int err;
  3024. char *path;
  3025. struct fuse *f = req_fuse_prepare(req);
  3026. err = get_path(f,ino,&path);
  3027. if(!err)
  3028. {
  3029. err = fuse_fs_bmap(f->fs,path,blocksize,&idx);
  3030. free_path(f,ino,path);
  3031. }
  3032. if(!err)
  3033. fuse_reply_bmap(req,idx);
  3034. else
  3035. reply_err(req,err);
  3036. }
  3037. static
  3038. void
  3039. fuse_lib_ioctl(fuse_req_t req,
  3040. fuse_ino_t ino,
  3041. unsigned long cmd,
  3042. void *arg,
  3043. fuse_file_info_t *llfi,
  3044. unsigned int flags,
  3045. const void *in_buf,
  3046. uint32_t in_bufsz,
  3047. uint32_t out_bufsz_)
  3048. {
  3049. int err;
  3050. char *out_buf = NULL;
  3051. struct fuse *f = req_fuse_prepare(req);
  3052. fuse_file_info_t fi;
  3053. uint32_t out_bufsz = out_bufsz_;
  3054. err = -EPERM;
  3055. if(flags & FUSE_IOCTL_UNRESTRICTED)
  3056. goto err;
  3057. if(flags & FUSE_IOCTL_DIR)
  3058. get_dirhandle(llfi,&fi);
  3059. else
  3060. fi = *llfi;
  3061. if(out_bufsz)
  3062. {
  3063. err = -ENOMEM;
  3064. out_buf = malloc(out_bufsz);
  3065. if(!out_buf)
  3066. goto err;
  3067. }
  3068. assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
  3069. if(out_buf)
  3070. memcpy(out_buf,in_buf,in_bufsz);
  3071. err = fuse_fs_ioctl(f->fs,cmd,arg,&fi,flags,
  3072. out_buf ?: (void *)in_buf,&out_bufsz);
  3073. fuse_reply_ioctl(req,err,out_buf,out_bufsz);
  3074. goto out;
  3075. err:
  3076. reply_err(req,err);
  3077. out:
  3078. free(out_buf);
  3079. }
  3080. static
  3081. void
  3082. fuse_lib_poll(fuse_req_t req,
  3083. fuse_ino_t ino,
  3084. fuse_file_info_t *fi,
  3085. fuse_pollhandle_t *ph)
  3086. {
  3087. int err;
  3088. struct fuse *f = req_fuse_prepare(req);
  3089. unsigned revents = 0;
  3090. err = fuse_fs_poll(f->fs,fi,ph,&revents);
  3091. if(!err)
  3092. fuse_reply_poll(req,revents);
  3093. else
  3094. reply_err(req,err);
  3095. }
  3096. static
  3097. void
  3098. fuse_lib_fallocate(fuse_req_t req,
  3099. fuse_ino_t ino,
  3100. int mode,
  3101. off_t offset,
  3102. off_t length,
  3103. fuse_file_info_t *fi)
  3104. {
  3105. int err;
  3106. struct fuse *f = req_fuse_prepare(req);
  3107. err = fuse_fs_fallocate(f->fs,mode,offset,length,fi);
  3108. reply_err(req,err);
  3109. }
  3110. static
  3111. int
  3112. remembered_node_cmp(const void *a_,
  3113. const void *b_)
  3114. {
  3115. const remembered_node_t *a = a_;
  3116. const remembered_node_t *b = b_;
  3117. return (a->time - b->time);
  3118. }
  3119. static
  3120. void
  3121. remembered_nodes_sort(struct fuse *f_)
  3122. {
  3123. pthread_mutex_lock(&f_->lock);
  3124. qsort(&kv_first(f_->remembered_nodes),
  3125. kv_size(f_->remembered_nodes),
  3126. sizeof(remembered_node_t),
  3127. remembered_node_cmp);
  3128. pthread_mutex_unlock(&f_->lock);
  3129. }
  3130. #define MAX_PRUNE 100
  3131. #define MAX_CHECK 1000
  3132. int
  3133. fuse_prune_some_remembered_nodes(struct fuse *f_,
  3134. int *offset_)
  3135. {
  3136. time_t now;
  3137. int pruned;
  3138. int checked;
  3139. pthread_mutex_lock(&f_->lock);
  3140. pruned = 0;
  3141. checked = 0;
  3142. now = current_time();
  3143. while(*offset_ < kv_size(f_->remembered_nodes))
  3144. {
  3145. time_t age;
  3146. remembered_node_t *fn = &kv_A(f_->remembered_nodes,*offset_);
  3147. if(pruned >= MAX_PRUNE)
  3148. break;
  3149. if(checked >= MAX_CHECK)
  3150. break;
  3151. checked++;
  3152. age = (now - fn->time);
  3153. if(f_->conf.remember > age)
  3154. break;
  3155. assert(fn->node->nlookup == 1);
  3156. /* Don't forget active directories */
  3157. if(fn->node->refctr > 1)
  3158. {
  3159. (*offset_)++;
  3160. continue;
  3161. }
  3162. fn->node->nlookup = 0;
  3163. unref_node(f_,fn->node);
  3164. kv_delete(f_->remembered_nodes,*offset_);
  3165. pruned++;
  3166. }
  3167. pthread_mutex_unlock(&f_->lock);
  3168. if((pruned < MAX_PRUNE) && (checked < MAX_CHECK))
  3169. *offset_ = -1;
  3170. return pruned;
  3171. }
  3172. #undef MAX_PRUNE
  3173. #undef MAX_CHECK
  3174. static
  3175. void
  3176. sleep_100ms(void)
  3177. {
  3178. const struct timespec ms100 = {0,100 * 1000000};
  3179. nanosleep(&ms100,NULL);
  3180. }
  3181. void
  3182. fuse_prune_remembered_nodes(struct fuse *f_)
  3183. {
  3184. int offset;
  3185. int pruned;
  3186. offset = 0;
  3187. pruned = 0;
  3188. for(;;)
  3189. {
  3190. pruned += fuse_prune_some_remembered_nodes(f_,&offset);
  3191. if(offset >= 0)
  3192. {
  3193. sleep_100ms();
  3194. continue;
  3195. }
  3196. break;
  3197. }
  3198. if(pruned > 0)
  3199. remembered_nodes_sort(f_);
  3200. }
  3201. static struct fuse_lowlevel_ops fuse_path_ops =
  3202. {
  3203. .access = fuse_lib_access,
  3204. .bmap = fuse_lib_bmap,
  3205. .copy_file_range = fuse_lib_copy_file_range,
  3206. .create = fuse_lib_create,
  3207. .destroy = fuse_lib_destroy,
  3208. .fallocate = fuse_lib_fallocate,
  3209. .flock = fuse_lib_flock,
  3210. .flush = fuse_lib_flush,
  3211. .forget = fuse_lib_forget,
  3212. .forget_multi = fuse_lib_forget_multi,
  3213. .fsync = fuse_lib_fsync,
  3214. .fsyncdir = fuse_lib_fsyncdir,
  3215. .getattr = fuse_lib_getattr,
  3216. .getlk = fuse_lib_getlk,
  3217. .getxattr = fuse_lib_getxattr,
  3218. .init = fuse_lib_init,
  3219. .ioctl = fuse_lib_ioctl,
  3220. .link = fuse_lib_link,
  3221. .listxattr = fuse_lib_listxattr,
  3222. .lookup = fuse_lib_lookup,
  3223. .mkdir = fuse_lib_mkdir,
  3224. .mknod = fuse_lib_mknod,
  3225. .open = fuse_lib_open,
  3226. .opendir = fuse_lib_opendir,
  3227. .poll = fuse_lib_poll,
  3228. .read = fuse_lib_read,
  3229. .readdir = fuse_lib_readdir,
  3230. .readdir_plus = fuse_lib_readdir_plus,
  3231. .readlink = fuse_lib_readlink,
  3232. .release = fuse_lib_release,
  3233. .releasedir = fuse_lib_releasedir,
  3234. .removexattr = fuse_lib_removexattr,
  3235. .rename = fuse_lib_rename,
  3236. .retrieve_reply = NULL,
  3237. .rmdir = fuse_lib_rmdir,
  3238. .setattr = fuse_lib_setattr,
  3239. .setlk = fuse_lib_setlk,
  3240. .setxattr = fuse_lib_setxattr,
  3241. .statfs = fuse_lib_statfs,
  3242. .symlink = fuse_lib_symlink,
  3243. .unlink = fuse_lib_unlink,
  3244. .write_buf = fuse_lib_write_buf,
  3245. };
  3246. int
  3247. fuse_notify_poll(fuse_pollhandle_t *ph)
  3248. {
  3249. return fuse_lowlevel_notify_poll(ph);
  3250. }
  3251. static
  3252. void
  3253. free_cmd(struct fuse_cmd *cmd)
  3254. {
  3255. free(cmd->buf);
  3256. free(cmd);
  3257. }
  3258. void
  3259. fuse_process_cmd(struct fuse *f,
  3260. struct fuse_cmd *cmd)
  3261. {
  3262. fuse_session_process(f->se,cmd->buf,cmd->buflen,cmd->ch);
  3263. free_cmd(cmd);
  3264. }
  3265. int
  3266. fuse_exited(struct fuse *f)
  3267. {
  3268. return fuse_session_exited(f->se);
  3269. }
  3270. struct fuse_session*
  3271. fuse_get_session(struct fuse *f)
  3272. {
  3273. return f->se;
  3274. }
  3275. static
  3276. struct fuse_cmd*
  3277. fuse_alloc_cmd(size_t bufsize)
  3278. {
  3279. struct fuse_cmd *cmd = (struct fuse_cmd *)malloc(sizeof(*cmd));
  3280. if(cmd == NULL)
  3281. {
  3282. fprintf(stderr,"fuse: failed to allocate cmd\n");
  3283. return NULL;
  3284. }
  3285. cmd->buf = (char *)malloc(bufsize);
  3286. if(cmd->buf == NULL)
  3287. {
  3288. fprintf(stderr,"fuse: failed to allocate read buffer\n");
  3289. free(cmd);
  3290. return NULL;
  3291. }
  3292. return cmd;
  3293. }
  3294. struct fuse_cmd*
  3295. fuse_read_cmd(struct fuse *f)
  3296. {
  3297. struct fuse_chan *ch = fuse_session_next_chan(f->se,NULL);
  3298. size_t bufsize = fuse_chan_bufsize(ch);
  3299. struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize);
  3300. if(cmd != NULL)
  3301. {
  3302. int res = fuse_chan_recv(&ch,cmd->buf,bufsize);
  3303. if(res <= 0)
  3304. {
  3305. free_cmd(cmd);
  3306. if(res < 0 && res != -EINTR && res != -EAGAIN)
  3307. fuse_exit(f);
  3308. return NULL;
  3309. }
  3310. cmd->buflen = res;
  3311. cmd->ch = ch;
  3312. }
  3313. return cmd;
  3314. }
  3315. void
  3316. fuse_exit(struct fuse *f)
  3317. {
  3318. fuse_session_exit(f->se);
  3319. }
  3320. struct fuse_context*
  3321. fuse_get_context(void)
  3322. {
  3323. return &fuse_get_context_internal()->ctx;
  3324. }
  3325. enum {
  3326. KEY_HELP,
  3327. };
  3328. #define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
  3329. static const struct fuse_opt fuse_lib_opts[] =
  3330. {
  3331. FUSE_OPT_KEY("-h", KEY_HELP),
  3332. FUSE_OPT_KEY("--help", KEY_HELP),
  3333. FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
  3334. FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
  3335. FUSE_LIB_OPT("debug", debug,1),
  3336. FUSE_LIB_OPT("-d", debug,1),
  3337. FUSE_LIB_OPT("umask=", set_mode,1),
  3338. FUSE_LIB_OPT("umask=%o", umask,0),
  3339. FUSE_LIB_OPT("uid=", set_uid,1),
  3340. FUSE_LIB_OPT("uid=%d", uid,0),
  3341. FUSE_LIB_OPT("gid=", set_gid,1),
  3342. FUSE_LIB_OPT("gid=%d", gid,0),
  3343. FUSE_LIB_OPT("noforget", remember,-1),
  3344. FUSE_LIB_OPT("remember=%u", remember,0),
  3345. FUSE_LIB_OPT("threads=%d", threads,0),
  3346. FUSE_LIB_OPT("use_ino", use_ino,1),
  3347. FUSE_OPT_END
  3348. };
  3349. static void fuse_lib_help(void)
  3350. {
  3351. fprintf(stderr,
  3352. " -o umask=M set file permissions (octal)\n"
  3353. " -o uid=N set file owner\n"
  3354. " -o gid=N set file group\n"
  3355. " -o noforget never forget cached inodes\n"
  3356. " -o remember=T remember cached inodes for T seconds (0s)\n"
  3357. " -o threads=NUM number of worker threads. 0 = autodetect.\n"
  3358. " Negative values autodetect then divide by\n"
  3359. " absolute value. default = 0\n"
  3360. "\n");
  3361. }
  3362. static
  3363. int
  3364. fuse_lib_opt_proc(void *data,
  3365. const char *arg,
  3366. int key,
  3367. struct fuse_args *outargs)
  3368. {
  3369. (void)arg; (void)outargs;
  3370. if(key == KEY_HELP)
  3371. {
  3372. struct fuse_config *conf = (struct fuse_config *)data;
  3373. fuse_lib_help();
  3374. conf->help = 1;
  3375. }
  3376. return 1;
  3377. }
  3378. int
  3379. fuse_is_lib_option(const char *opt)
  3380. {
  3381. return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
  3382. }
  3383. struct fuse_fs*
  3384. fuse_fs_new(const struct fuse_operations *op,
  3385. size_t op_size)
  3386. {
  3387. struct fuse_fs *fs;
  3388. if(sizeof(struct fuse_operations) < op_size)
  3389. {
  3390. fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
  3391. op_size = sizeof(struct fuse_operations);
  3392. }
  3393. fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
  3394. if(!fs)
  3395. {
  3396. fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
  3397. return NULL;
  3398. }
  3399. if(op)
  3400. memcpy(&fs->op,op,op_size);
  3401. return fs;
  3402. }
  3403. static
  3404. int
  3405. node_table_init(struct node_table *t)
  3406. {
  3407. t->size = NODE_TABLE_MIN_SIZE;
  3408. t->array = (struct node **)calloc(1,sizeof(struct node *) * t->size);
  3409. if(t->array == NULL)
  3410. {
  3411. fprintf(stderr,"fuse: memory allocation failed\n");
  3412. return -1;
  3413. }
  3414. t->use = 0;
  3415. t->split = 0;
  3416. return 0;
  3417. }
  3418. static
  3419. void
  3420. fuse_malloc_trim(void)
  3421. {
  3422. #ifdef HAVE_MALLOC_TRIM
  3423. malloc_trim(1024 * 1024);
  3424. #endif
  3425. }
  3426. static
  3427. void*
  3428. fuse_maintenance_loop(void *fuse_)
  3429. {
  3430. int loops;
  3431. int sleep_time;
  3432. double slab_usage_ratio;
  3433. struct fuse *f = (struct fuse*)fuse_;
  3434. loops = 0;
  3435. sleep_time = 60;
  3436. while(1)
  3437. {
  3438. if(remember_nodes(f))
  3439. fuse_prune_remembered_nodes(f);
  3440. slab_usage_ratio = lfmp_slab_usage_ratio(&f->node_fmp);
  3441. if(slab_usage_ratio > 3.0)
  3442. lfmp_gc(&f->node_fmp);
  3443. if(loops % 15)
  3444. fuse_malloc_trim();
  3445. loops++;
  3446. sleep(sleep_time);
  3447. }
  3448. return NULL;
  3449. }
  3450. int
  3451. fuse_start_maintenance_thread(struct fuse *f_)
  3452. {
  3453. return fuse_start_thread(&f_->maintenance_thread,fuse_maintenance_loop,f_);
  3454. }
  3455. void
  3456. fuse_stop_maintenance_thread(struct fuse *f_)
  3457. {
  3458. pthread_mutex_lock(&f_->lock);
  3459. pthread_cancel(f_->maintenance_thread);
  3460. pthread_mutex_unlock(&f_->lock);
  3461. pthread_join(f_->maintenance_thread,NULL);
  3462. }
  3463. struct fuse*
  3464. fuse_new_common(struct fuse_chan *ch,
  3465. struct fuse_args *args,
  3466. const struct fuse_operations *op,
  3467. size_t op_size)
  3468. {
  3469. struct fuse *f;
  3470. struct node *root;
  3471. struct fuse_fs *fs;
  3472. struct fuse_lowlevel_ops llop = fuse_path_ops;
  3473. if(fuse_create_context_key() == -1)
  3474. goto out;
  3475. f = (struct fuse *)calloc(1,sizeof(struct fuse));
  3476. if(f == NULL)
  3477. {
  3478. fprintf(stderr,"fuse: failed to allocate fuse object\n");
  3479. goto out_delete_context_key;
  3480. }
  3481. fs = fuse_fs_new(op,op_size);
  3482. if(!fs)
  3483. goto out_free;
  3484. f->fs = fs;
  3485. /* Oh f**k,this is ugly! */
  3486. if(!fs->op.lock)
  3487. {
  3488. llop.getlk = NULL;
  3489. llop.setlk = NULL;
  3490. }
  3491. if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
  3492. goto out_free_fs;
  3493. f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
  3494. if(f->se == NULL)
  3495. goto out_free_fs;
  3496. fuse_session_add_chan(f->se,ch);
  3497. /* Trace topmost layer by default */
  3498. srand(time(NULL));
  3499. f->ctr = 0;
  3500. f->generation = rand64();
  3501. if(node_table_init(&f->name_table) == -1)
  3502. goto out_free_session;
  3503. if(node_table_init(&f->id_table) == -1)
  3504. goto out_free_name_table;
  3505. fuse_mutex_init(&f->lock);
  3506. lfmp_init(&f->node_fmp,sizeof(struct node),256);
  3507. kv_init(f->remembered_nodes);
  3508. root = alloc_node(f);
  3509. if(root == NULL)
  3510. {
  3511. fprintf(stderr,"fuse: memory allocation failed\n");
  3512. goto out_free_id_table;
  3513. }
  3514. strcpy(root->inline_name,"/");
  3515. root->name = root->inline_name;
  3516. root->parent = NULL;
  3517. root->nodeid = FUSE_ROOT_ID;
  3518. inc_nlookup(root);
  3519. hash_id(f,root);
  3520. return f;
  3521. out_free_id_table:
  3522. free(f->id_table.array);
  3523. out_free_name_table:
  3524. free(f->name_table.array);
  3525. out_free_session:
  3526. fuse_session_destroy(f->se);
  3527. out_free_fs:
  3528. /* Horrible compatibility hack to stop the destructor from being
  3529. called on the filesystem without init being called first */
  3530. fs->op.destroy = NULL;
  3531. fuse_fs_destroy(f->fs);
  3532. out_free:
  3533. free(f);
  3534. out_delete_context_key:
  3535. fuse_delete_context_key();
  3536. out:
  3537. return NULL;
  3538. }
  3539. struct fuse*
  3540. fuse_new(struct fuse_chan *ch,
  3541. struct fuse_args *args,
  3542. const struct fuse_operations *op,
  3543. size_t op_size)
  3544. {
  3545. return fuse_new_common(ch,args,op,op_size);
  3546. }
  3547. void
  3548. fuse_destroy(struct fuse *f)
  3549. {
  3550. size_t i;
  3551. if(f->fs)
  3552. {
  3553. struct fuse_context_i *c = fuse_get_context_internal();
  3554. memset(c,0,sizeof(*c));
  3555. c->ctx.fuse = f;
  3556. for(i = 0; i < f->id_table.size; i++)
  3557. {
  3558. struct node *node;
  3559. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  3560. {
  3561. if(node->is_hidden)
  3562. fuse_fs_free_hide(f->fs,node->hidden_fh);
  3563. }
  3564. }
  3565. }
  3566. for(i = 0; i < f->id_table.size; i++)
  3567. {
  3568. struct node *node;
  3569. struct node *next;
  3570. for(node = f->id_table.array[i]; node != NULL; node = next)
  3571. {
  3572. next = node->id_next;
  3573. free_node(f,node);
  3574. f->id_table.use--;
  3575. }
  3576. }
  3577. free(f->id_table.array);
  3578. free(f->name_table.array);
  3579. pthread_mutex_destroy(&f->lock);
  3580. fuse_session_destroy(f->se);
  3581. lfmp_destroy(&f->node_fmp);
  3582. kv_destroy(f->remembered_nodes);
  3583. free(f);
  3584. fuse_delete_context_key();
  3585. }
  3586. int
  3587. fuse_config_num_threads(const struct fuse *fuse_)
  3588. {
  3589. return fuse_->conf.threads;
  3590. }