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.

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