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.

4136 lines
82 KiB

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