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.

4282 lines
86 KiB

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