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.

4160 lines
84 KiB

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