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.

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