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.

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