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.

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