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.

4036 lines
81 KiB

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