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.

4057 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 <assert.h>
  23. #include <dlfcn.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <inttypes.h>
  27. #include <limits.h>
  28. #include <poll.h>
  29. #include <signal.h>
  30. #include <stdbool.h>
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/file.h>
  37. #include <sys/mman.h>
  38. #include <sys/param.h>
  39. #include <sys/time.h>
  40. #include <sys/uio.h>
  41. #include <time.h>
  42. #include <unistd.h>
  43. #ifdef HAVE_MALLOC_TRIM
  44. #include <malloc.h>
  45. #endif
  46. #define FUSE_UNKNOWN_INO UINT64_MAX
  47. #define OFFSET_MAX 0x7fffffffffffffffLL
  48. #define NODE_TABLE_MIN_SIZE 8192
  49. #define PARAM(inarg) ((void*)(((char*)(inarg)) + sizeof(*(inarg))))
  50. static int g_LOG_METRICS = 0;
  51. struct fuse_config
  52. {
  53. unsigned int uid;
  54. unsigned int gid;
  55. unsigned int umask;
  56. int remember;
  57. int debug;
  58. int nogc;
  59. int set_mode;
  60. int set_uid;
  61. int set_gid;
  62. int help;
  63. int read_thread_count;
  64. int process_thread_count;
  65. char *pin_threads;
  66. };
  67. struct fuse_fs
  68. {
  69. struct fuse_operations op;
  70. };
  71. struct lock_queue_element
  72. {
  73. struct lock_queue_element *next;
  74. pthread_cond_t cond;
  75. uint64_t nodeid1;
  76. const char *name1;
  77. char **path1;
  78. struct node **wnode1;
  79. uint64_t nodeid2;
  80. const char *name2;
  81. char **path2;
  82. struct node **wnode2;
  83. int err;
  84. bool done : 1;
  85. };
  86. struct node_table
  87. {
  88. struct node **array;
  89. size_t use;
  90. size_t size;
  91. size_t split;
  92. };
  93. #define container_of(ptr,type,member) ({ \
  94. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  95. (type *)( (char *)__mptr - offsetof(type,member) );})
  96. #define list_entry(ptr,type,member) \
  97. container_of(ptr,type,member)
  98. struct list_head
  99. {
  100. struct list_head *next;
  101. struct list_head *prev;
  102. };
  103. typedef struct remembered_node_t remembered_node_t;
  104. struct remembered_node_t
  105. {
  106. struct node *node;
  107. time_t time;
  108. };
  109. typedef struct nodeid_gen_t nodeid_gen_t;
  110. struct nodeid_gen_t
  111. {
  112. uint64_t nodeid;
  113. uint64_t generation;
  114. };
  115. struct fuse
  116. {
  117. struct fuse_session *se;
  118. struct node_table name_table;
  119. struct node_table id_table;
  120. nodeid_gen_t nodeid_gen;
  121. unsigned int hidectr;
  122. pthread_mutex_t lock;
  123. struct fuse_config conf;
  124. struct fuse_fs *fs;
  125. struct lock_queue_element *lockq;
  126. pthread_t maintenance_thread;
  127. lfmp_t node_fmp;
  128. kvec_t(remembered_node_t) remembered_nodes;
  129. };
  130. struct lock
  131. {
  132. int type;
  133. off_t start;
  134. off_t end;
  135. pid_t pid;
  136. uint64_t owner;
  137. struct lock *next;
  138. };
  139. struct node
  140. {
  141. struct node *name_next;
  142. struct node *id_next;
  143. uint64_t nodeid;
  144. char *name;
  145. struct node *parent;
  146. uint64_t nlookup;
  147. uint32_t refctr;
  148. uint32_t open_count;
  149. uint64_t hidden_fh;
  150. int32_t treelock;
  151. struct lock *locks;
  152. uint32_t stat_crc32b;
  153. uint8_t is_hidden:1;
  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_->is_hidden)
  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. void
  1169. fuse_free_buf(struct fuse_bufvec *buf)
  1170. {
  1171. if(buf != NULL)
  1172. {
  1173. size_t i;
  1174. for(i = 0; i < buf->count; i++)
  1175. free(buf->buf[i].mem);
  1176. free(buf);
  1177. }
  1178. }
  1179. static
  1180. int
  1181. node_open(const struct node *node_)
  1182. {
  1183. return ((node_ != NULL) && (node_->open_count > 0));
  1184. }
  1185. static
  1186. void
  1187. update_stat(struct node *node_,
  1188. const struct stat *stnew_)
  1189. {
  1190. uint32_t crc32b;
  1191. crc32b = stat_crc32b(stnew_);
  1192. if(node_->is_stat_cache_valid && (crc32b != node_->stat_crc32b))
  1193. node_->is_stat_cache_valid = 0;
  1194. node_->stat_crc32b = crc32b;
  1195. }
  1196. static
  1197. int
  1198. set_path_info(struct fuse *f,
  1199. uint64_t nodeid,
  1200. const char *name,
  1201. struct fuse_entry_param *e)
  1202. {
  1203. struct node *node;
  1204. node = find_node(f,nodeid,name);
  1205. if(node == NULL)
  1206. return -ENOMEM;
  1207. e->ino = node->nodeid;
  1208. e->generation = f->nodeid_gen.generation;
  1209. pthread_mutex_lock(&f->lock);
  1210. update_stat(node,&e->attr);
  1211. pthread_mutex_unlock(&f->lock);
  1212. set_stat(f,e->ino,&e->attr);
  1213. return 0;
  1214. }
  1215. static
  1216. int
  1217. lookup_path(struct fuse *f,
  1218. uint64_t nodeid,
  1219. const char *name,
  1220. const char *path,
  1221. struct fuse_entry_param *e,
  1222. fuse_file_info_t *fi)
  1223. {
  1224. int rv;
  1225. memset(e,0,sizeof(struct fuse_entry_param));
  1226. rv = ((fi == NULL) ?
  1227. f->fs->op.getattr(path,&e->attr,&e->timeout) :
  1228. f->fs->op.fgetattr(fi,&e->attr,&e->timeout));
  1229. if(rv)
  1230. return rv;
  1231. return set_path_info(f,nodeid,name,e);
  1232. }
  1233. static
  1234. struct fuse_context_i*
  1235. fuse_get_context_internal(void)
  1236. {
  1237. struct fuse_context_i *c;
  1238. c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
  1239. if(c == NULL)
  1240. {
  1241. c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
  1242. if(c == NULL)
  1243. {
  1244. /* This is hard to deal with properly,so just
  1245. abort. If memory is so low that the
  1246. context cannot be allocated,there's not
  1247. much hope for the filesystem anyway */
  1248. fprintf(stderr,"fuse: failed to allocate thread specific data\n");
  1249. abort();
  1250. }
  1251. pthread_setspecific(fuse_context_key,c);
  1252. }
  1253. return c;
  1254. }
  1255. static
  1256. void
  1257. fuse_freecontext(void *data)
  1258. {
  1259. free(data);
  1260. }
  1261. static
  1262. int
  1263. fuse_create_context_key(void)
  1264. {
  1265. int err = 0;
  1266. pthread_mutex_lock(&fuse_context_lock);
  1267. if(!fuse_context_ref)
  1268. {
  1269. err = pthread_key_create(&fuse_context_key,fuse_freecontext);
  1270. if(err)
  1271. {
  1272. fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
  1273. strerror(err));
  1274. pthread_mutex_unlock(&fuse_context_lock);
  1275. return -1;
  1276. }
  1277. }
  1278. fuse_context_ref++;
  1279. pthread_mutex_unlock(&fuse_context_lock);
  1280. return 0;
  1281. }
  1282. static
  1283. void
  1284. fuse_delete_context_key(void)
  1285. {
  1286. pthread_mutex_lock(&fuse_context_lock);
  1287. fuse_context_ref--;
  1288. if(!fuse_context_ref)
  1289. {
  1290. free(pthread_getspecific(fuse_context_key));
  1291. pthread_key_delete(fuse_context_key);
  1292. }
  1293. pthread_mutex_unlock(&fuse_context_lock);
  1294. }
  1295. static
  1296. struct fuse*
  1297. req_fuse_prepare(fuse_req_t req)
  1298. {
  1299. struct fuse_context_i *c = fuse_get_context_internal();
  1300. const struct fuse_ctx *ctx = fuse_req_ctx(req);
  1301. c->req = req;
  1302. c->ctx.fuse = req_fuse(req);
  1303. c->ctx.uid = ctx->uid;
  1304. c->ctx.gid = ctx->gid;
  1305. c->ctx.pid = ctx->pid;
  1306. c->ctx.umask = ctx->umask;
  1307. return c->ctx.fuse;
  1308. }
  1309. static
  1310. inline
  1311. void
  1312. reply_err(fuse_req_t req,
  1313. int err)
  1314. {
  1315. /* fuse_reply_err() uses non-negated errno values */
  1316. fuse_reply_err(req,-err);
  1317. }
  1318. static
  1319. void
  1320. reply_entry(fuse_req_t req,
  1321. const struct fuse_entry_param *e,
  1322. int err)
  1323. {
  1324. if(!err)
  1325. {
  1326. struct fuse *f = req_fuse(req);
  1327. if(fuse_reply_entry(req,e) == -ENOENT)
  1328. {
  1329. /* Skip forget for negative result */
  1330. if(e->ino != 0)
  1331. forget_node(f,e->ino,1);
  1332. }
  1333. }
  1334. else
  1335. {
  1336. reply_err(req,err);
  1337. }
  1338. }
  1339. static
  1340. void
  1341. fuse_lib_init(void *data,
  1342. struct fuse_conn_info *conn)
  1343. {
  1344. struct fuse *f = (struct fuse *)data;
  1345. struct fuse_context_i *c = fuse_get_context_internal();
  1346. memset(c,0,sizeof(*c));
  1347. c->ctx.fuse = f;
  1348. conn->want |= FUSE_CAP_EXPORT_SUPPORT;
  1349. f->fs->op.init(conn);
  1350. }
  1351. static
  1352. void
  1353. fuse_lib_destroy(void *data)
  1354. {
  1355. struct fuse *f = (struct fuse *)data;
  1356. struct fuse_context_i *c = fuse_get_context_internal();
  1357. memset(c,0,sizeof(*c));
  1358. c->ctx.fuse = f;
  1359. f->fs->op.destroy();
  1360. free(f->fs);
  1361. f->fs = NULL;
  1362. }
  1363. static
  1364. void
  1365. fuse_lib_lookup(fuse_req_t req,
  1366. struct fuse_in_header *hdr_)
  1367. {
  1368. int err;
  1369. uint64_t nodeid;
  1370. char *path;
  1371. const char *name;
  1372. struct fuse *f;
  1373. struct node *dot = NULL;
  1374. struct fuse_entry_param e = {0};
  1375. name = fuse_hdr_arg(hdr_);
  1376. nodeid = hdr_->nodeid;
  1377. f = req_fuse_prepare(req);
  1378. if(name[0] == '.')
  1379. {
  1380. if(name[1] == '\0')
  1381. {
  1382. name = NULL;
  1383. pthread_mutex_lock(&f->lock);
  1384. dot = get_node_nocheck(f,nodeid);
  1385. if(dot == NULL)
  1386. {
  1387. pthread_mutex_unlock(&f->lock);
  1388. reply_entry(req,&e,-ESTALE);
  1389. return;
  1390. }
  1391. dot->refctr++;
  1392. pthread_mutex_unlock(&f->lock);
  1393. }
  1394. else if((name[1] == '.') && (name[2] == '\0'))
  1395. {
  1396. if(nodeid == 1)
  1397. {
  1398. reply_entry(req,&e,-ENOENT);
  1399. return;
  1400. }
  1401. name = NULL;
  1402. pthread_mutex_lock(&f->lock);
  1403. nodeid = get_node(f,nodeid)->parent->nodeid;
  1404. pthread_mutex_unlock(&f->lock);
  1405. }
  1406. }
  1407. err = get_path_name(f,nodeid,name,&path);
  1408. if(!err)
  1409. {
  1410. err = lookup_path(f,nodeid,name,path,&e,NULL);
  1411. if(err == -ENOENT)
  1412. {
  1413. e.ino = 0;
  1414. err = 0;
  1415. }
  1416. free_path(f,nodeid,path);
  1417. }
  1418. if(dot)
  1419. {
  1420. pthread_mutex_lock(&f->lock);
  1421. unref_node(f,dot);
  1422. pthread_mutex_unlock(&f->lock);
  1423. }
  1424. reply_entry(req,&e,err);
  1425. }
  1426. static
  1427. void
  1428. fuse_lib_forget(fuse_req_t req,
  1429. struct fuse_in_header *hdr_)
  1430. {
  1431. struct fuse *f;
  1432. struct fuse_forget_in *arg;
  1433. f = req_fuse(req);
  1434. arg = fuse_hdr_arg(hdr_);
  1435. forget_node(f,hdr_->nodeid,arg->nlookup);
  1436. fuse_reply_none(req);
  1437. }
  1438. static
  1439. void
  1440. fuse_lib_forget_multi(fuse_req_t req,
  1441. struct fuse_in_header *hdr_)
  1442. {
  1443. struct fuse *f;
  1444. struct fuse_batch_forget_in *arg;
  1445. struct fuse_forget_one *entry;
  1446. f = req_fuse(req);
  1447. arg = fuse_hdr_arg(hdr_);
  1448. entry = PARAM(arg);
  1449. for(uint32_t i = 0; i < arg->count; i++)
  1450. forget_node(f,
  1451. entry[i].nodeid,
  1452. entry[i].nlookup);
  1453. fuse_reply_none(req);
  1454. }
  1455. static
  1456. void
  1457. fuse_lib_getattr(fuse_req_t req,
  1458. struct fuse_in_header *hdr_)
  1459. {
  1460. int err;
  1461. char *path;
  1462. struct fuse *f;
  1463. struct stat buf;
  1464. struct node *node;
  1465. fuse_timeouts_t timeout;
  1466. fuse_file_info_t ffi = {0};
  1467. const struct fuse_getattr_in *arg;
  1468. arg = fuse_hdr_arg(hdr_);
  1469. f = req_fuse_prepare(req);
  1470. if(arg->getattr_flags & FUSE_GETATTR_FH)
  1471. {
  1472. ffi.fh = arg->fh;
  1473. }
  1474. else
  1475. {
  1476. pthread_mutex_lock(&f->lock);
  1477. node = get_node(f,hdr_->nodeid);
  1478. if(node->is_hidden)
  1479. ffi.fh = node->hidden_fh;
  1480. pthread_mutex_unlock(&f->lock);
  1481. }
  1482. memset(&buf,0,sizeof(buf));
  1483. err = 0;
  1484. path = NULL;
  1485. if(ffi.fh == 0)
  1486. err = get_path(f,hdr_->nodeid,&path);
  1487. if(!err)
  1488. {
  1489. err = ((ffi.fh == 0) ?
  1490. f->fs->op.getattr(path,&buf,&timeout) :
  1491. f->fs->op.fgetattr(&ffi,&buf,&timeout));
  1492. free_path(f,hdr_->nodeid,path);
  1493. }
  1494. if(!err)
  1495. {
  1496. pthread_mutex_lock(&f->lock);
  1497. node = get_node(f,hdr_->nodeid);
  1498. update_stat(node,&buf);
  1499. pthread_mutex_unlock(&f->lock);
  1500. set_stat(f,hdr_->nodeid,&buf);
  1501. fuse_reply_attr(req,&buf,timeout.attr);
  1502. }
  1503. else
  1504. {
  1505. reply_err(req,err);
  1506. }
  1507. }
  1508. static
  1509. void
  1510. fuse_lib_setattr(fuse_req_t req,
  1511. struct fuse_in_header *hdr_)
  1512. {
  1513. struct fuse *f = req_fuse_prepare(req);
  1514. struct stat stbuf = {0};
  1515. char *path;
  1516. int err;
  1517. struct node *node;
  1518. fuse_timeouts_t timeout;
  1519. fuse_file_info_t *fi;
  1520. fuse_file_info_t ffi = {0};
  1521. struct fuse_setattr_in *arg;
  1522. arg = fuse_hdr_arg(hdr_);
  1523. fi = NULL;
  1524. if(arg->valid & FATTR_FH)
  1525. {
  1526. fi = &ffi;
  1527. fi->fh = arg->fh;
  1528. }
  1529. else
  1530. {
  1531. pthread_mutex_lock(&f->lock);
  1532. node = get_node(f,hdr_->nodeid);
  1533. if(node->is_hidden)
  1534. {
  1535. fi = &ffi;
  1536. fi->fh = node->hidden_fh;
  1537. }
  1538. pthread_mutex_unlock(&f->lock);
  1539. }
  1540. err = 0;
  1541. path = NULL;
  1542. if(fi == NULL)
  1543. err = get_path(f,hdr_->nodeid,&path);
  1544. if(!err)
  1545. {
  1546. err = 0;
  1547. if(!err && (arg->valid & FATTR_MODE))
  1548. err = ((fi == NULL) ?
  1549. f->fs->op.chmod(path,arg->mode) :
  1550. f->fs->op.fchmod(fi,arg->mode));
  1551. if(!err && (arg->valid & (FATTR_UID | FATTR_GID)))
  1552. {
  1553. uid_t uid = ((arg->valid & FATTR_UID) ? arg->uid : (uid_t)-1);
  1554. gid_t gid = ((arg->valid & FATTR_GID) ? arg->gid : (gid_t)-1);
  1555. err = ((fi == NULL) ?
  1556. f->fs->op.chown(path,uid,gid) :
  1557. f->fs->op.fchown(fi,uid,gid));
  1558. }
  1559. if(!err && (arg->valid & FATTR_SIZE))
  1560. err = ((fi == NULL) ?
  1561. f->fs->op.truncate(path,arg->size) :
  1562. f->fs->op.ftruncate(fi,arg->size));
  1563. #ifdef HAVE_UTIMENSAT
  1564. if(!err && (arg->valid & (FATTR_ATIME | FATTR_MTIME)))
  1565. {
  1566. struct timespec tv[2];
  1567. tv[0].tv_sec = 0;
  1568. tv[1].tv_sec = 0;
  1569. tv[0].tv_nsec = UTIME_OMIT;
  1570. tv[1].tv_nsec = UTIME_OMIT;
  1571. if(arg->valid & FATTR_ATIME_NOW)
  1572. tv[0].tv_nsec = UTIME_NOW;
  1573. else if(arg->valid & FATTR_ATIME)
  1574. tv[0] = (struct timespec){ arg->atime, arg->atimensec };
  1575. if(arg->valid & FATTR_MTIME_NOW)
  1576. tv[1].tv_nsec = UTIME_NOW;
  1577. else if(arg->valid & FATTR_MTIME)
  1578. tv[1] = (struct timespec){ arg->mtime, arg->mtimensec };
  1579. err = ((fi == NULL) ?
  1580. f->fs->op.utimens(path,tv) :
  1581. f->fs->op.futimens(fi,tv));
  1582. }
  1583. else
  1584. #endif
  1585. if(!err && ((arg->valid & (FATTR_ATIME|FATTR_MTIME)) == (FATTR_ATIME|FATTR_MTIME)))
  1586. {
  1587. struct timespec tv[2];
  1588. tv[0].tv_sec = arg->atime;
  1589. tv[0].tv_nsec = arg->atimensec;
  1590. tv[1].tv_sec = arg->mtime;
  1591. tv[1].tv_nsec = arg->mtimensec;
  1592. err = ((fi == NULL) ?
  1593. f->fs->op.utimens(path,tv) :
  1594. f->fs->op.futimens(fi,tv));
  1595. }
  1596. if(!err)
  1597. err = ((fi == NULL) ?
  1598. f->fs->op.getattr(path,&stbuf,&timeout) :
  1599. f->fs->op.fgetattr(fi,&stbuf,&timeout));
  1600. free_path(f,hdr_->nodeid,path);
  1601. }
  1602. if(!err)
  1603. {
  1604. pthread_mutex_lock(&f->lock);
  1605. update_stat(get_node(f,hdr_->nodeid),&stbuf);
  1606. pthread_mutex_unlock(&f->lock);
  1607. set_stat(f,hdr_->nodeid,&stbuf);
  1608. fuse_reply_attr(req,&stbuf,timeout.attr);
  1609. }
  1610. else
  1611. {
  1612. reply_err(req,err);
  1613. }
  1614. }
  1615. static
  1616. void
  1617. fuse_lib_access(fuse_req_t req,
  1618. struct fuse_in_header *hdr_)
  1619. {
  1620. int err;
  1621. char *path;
  1622. struct fuse *f;
  1623. struct fuse_access_in *arg;
  1624. arg = fuse_hdr_arg(hdr_);
  1625. f = req_fuse_prepare(req);
  1626. err = get_path(f,hdr_->nodeid,&path);
  1627. if(!err)
  1628. {
  1629. err = f->fs->op.access(path,arg->mask);
  1630. free_path(f,hdr_->nodeid,path);
  1631. }
  1632. reply_err(req,err);
  1633. }
  1634. static
  1635. void
  1636. fuse_lib_readlink(fuse_req_t req,
  1637. struct fuse_in_header *hdr_)
  1638. {
  1639. int err;
  1640. char *path;
  1641. struct fuse *f;
  1642. char linkname[PATH_MAX + 1];
  1643. f = req_fuse_prepare(req);
  1644. err = get_path(f,hdr_->nodeid,&path);
  1645. if(!err)
  1646. {
  1647. err = f->fs->op.readlink(path,linkname,sizeof(linkname));
  1648. free_path(f,hdr_->nodeid,path);
  1649. }
  1650. if(!err)
  1651. {
  1652. linkname[PATH_MAX] = '\0';
  1653. fuse_reply_readlink(req,linkname);
  1654. }
  1655. else
  1656. {
  1657. reply_err(req,err);
  1658. }
  1659. }
  1660. static
  1661. void
  1662. fuse_lib_mknod(fuse_req_t req,
  1663. struct fuse_in_header *hdr_)
  1664. {
  1665. int err;
  1666. char *path;
  1667. struct fuse *f;
  1668. const char* name;
  1669. struct fuse_entry_param e;
  1670. struct fuse_mknod_in *arg;
  1671. arg = fuse_hdr_arg(hdr_);
  1672. name = PARAM(arg);
  1673. if(req->f->conn.proto_minor >= 12)
  1674. req->ctx.umask = arg->umask;
  1675. else
  1676. name = (char*)arg + FUSE_COMPAT_MKNOD_IN_SIZE;
  1677. f = req_fuse_prepare(req);
  1678. err = get_path_name(f,hdr_->nodeid,name,&path);
  1679. if(!err)
  1680. {
  1681. err = -ENOSYS;
  1682. if(S_ISREG(arg->mode))
  1683. {
  1684. fuse_file_info_t fi;
  1685. memset(&fi,0,sizeof(fi));
  1686. fi.flags = O_CREAT | O_EXCL | O_WRONLY;
  1687. err = f->fs->op.create(path,arg->mode,&fi);
  1688. if(!err)
  1689. {
  1690. err = lookup_path(f,hdr_->nodeid,name,path,&e,&fi);
  1691. f->fs->op.release(&fi);
  1692. }
  1693. }
  1694. if(err == -ENOSYS)
  1695. {
  1696. err = f->fs->op.mknod(path,arg->mode,arg->rdev);
  1697. if(!err)
  1698. err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
  1699. }
  1700. free_path(f,hdr_->nodeid,path);
  1701. }
  1702. reply_entry(req,&e,err);
  1703. }
  1704. static
  1705. void
  1706. fuse_lib_mkdir(fuse_req_t req,
  1707. struct fuse_in_header *hdr_)
  1708. {
  1709. int err;
  1710. char *path;
  1711. struct fuse *f;
  1712. const char *name;
  1713. struct fuse_entry_param e;
  1714. struct fuse_mkdir_in *arg;
  1715. arg = fuse_hdr_arg(hdr_);
  1716. name = PARAM(arg);
  1717. if(req->f->conn.proto_minor >= 12)
  1718. req->ctx.umask = arg->umask;
  1719. f = req_fuse_prepare(req);
  1720. err = get_path_name(f,hdr_->nodeid,name,&path);
  1721. if(!err)
  1722. {
  1723. err = f->fs->op.mkdir(path,arg->mode);
  1724. if(!err)
  1725. err = lookup_path(f,hdr_->nodeid,name,path,&e,NULL);
  1726. free_path(f,hdr_->nodeid,path);
  1727. }
  1728. reply_entry(req,&e,err);
  1729. }
  1730. static
  1731. void
  1732. fuse_lib_unlink(fuse_req_t req,
  1733. struct fuse_in_header *hdr_)
  1734. {
  1735. int err;
  1736. char *path;
  1737. struct fuse *f;
  1738. const char *name;
  1739. struct node *wnode;
  1740. name = PARAM(hdr_);
  1741. f = req_fuse_prepare(req);
  1742. err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
  1743. if(!err)
  1744. {
  1745. pthread_mutex_lock(&f->lock);
  1746. if(node_open(wnode))
  1747. {
  1748. err = f->fs->op.prepare_hide(path,&wnode->hidden_fh);
  1749. if(!err)
  1750. wnode->is_hidden = 1;
  1751. }
  1752. pthread_mutex_unlock(&f->lock);
  1753. err = f->fs->op.unlink(path);
  1754. if(!err)
  1755. remove_node(f,hdr_->nodeid,name);
  1756. free_path_wrlock(f,hdr_->nodeid,wnode,path);
  1757. }
  1758. reply_err(req,err);
  1759. }
  1760. static
  1761. void
  1762. fuse_lib_rmdir(fuse_req_t req,
  1763. struct fuse_in_header *hdr_)
  1764. {
  1765. int err;
  1766. char *path;
  1767. struct fuse *f;
  1768. const char *name;
  1769. struct node *wnode;
  1770. name = PARAM(hdr_);
  1771. f = req_fuse_prepare(req);
  1772. err = get_path_wrlock(f,hdr_->nodeid,name,&path,&wnode);
  1773. if(!err)
  1774. {
  1775. err = f->fs->op.rmdir(path);
  1776. if(!err)
  1777. remove_node(f,hdr_->nodeid,name);
  1778. free_path_wrlock(f,hdr_->nodeid,wnode,path);
  1779. }
  1780. reply_err(req,err);
  1781. }
  1782. static
  1783. void
  1784. fuse_lib_symlink(fuse_req_t req_,
  1785. struct fuse_in_header *hdr_)
  1786. {
  1787. int rv;
  1788. char *path;
  1789. struct fuse *f;
  1790. const char *name;
  1791. const char *linkname;
  1792. struct fuse_entry_param e = {0};
  1793. name = fuse_hdr_arg(hdr_);
  1794. linkname = (name + strlen(name) + 1);
  1795. f = req_fuse_prepare(req_);
  1796. rv = get_path_name(f,hdr_->nodeid,name,&path);
  1797. if(rv == 0)
  1798. {
  1799. rv = f->fs->op.symlink(linkname,path,&e.attr,&e.timeout);
  1800. if(rv == 0)
  1801. rv = set_path_info(f,hdr_->nodeid,name,&e);
  1802. free_path(f,hdr_->nodeid,path);
  1803. }
  1804. reply_entry(req_,&e,rv);
  1805. }
  1806. static
  1807. void
  1808. fuse_lib_rename(fuse_req_t req,
  1809. struct fuse_in_header *hdr_)
  1810. {
  1811. int err;
  1812. struct fuse *f;
  1813. char *oldpath;
  1814. char *newpath;
  1815. const char *oldname;
  1816. const char *newname;
  1817. struct node *wnode1;
  1818. struct node *wnode2;
  1819. struct fuse_rename_in *arg;
  1820. arg = fuse_hdr_arg(hdr_);
  1821. oldname = PARAM(arg);
  1822. newname = (oldname + strlen(oldname) + 1);
  1823. f = req_fuse_prepare(req);
  1824. err = get_path2(f,hdr_->nodeid,oldname,arg->newdir,newname,
  1825. &oldpath,&newpath,&wnode1,&wnode2);
  1826. if(!err)
  1827. {
  1828. pthread_mutex_lock(&f->lock);
  1829. if(node_open(wnode2))
  1830. {
  1831. err = f->fs->op.prepare_hide(newpath,&wnode2->hidden_fh);
  1832. if(!err)
  1833. wnode2->is_hidden = 1;
  1834. }
  1835. pthread_mutex_unlock(&f->lock);
  1836. err = f->fs->op.rename(oldpath,newpath);
  1837. if(!err)
  1838. err = rename_node(f,hdr_->nodeid,oldname,arg->newdir,newname);
  1839. free_path2(f,hdr_->nodeid,arg->newdir,wnode1,wnode2,oldpath,newpath);
  1840. }
  1841. reply_err(req,err);
  1842. }
  1843. static
  1844. void
  1845. fuse_lib_link(fuse_req_t req,
  1846. struct fuse_in_header *hdr_)
  1847. {
  1848. int rv;
  1849. char *oldpath;
  1850. char *newpath;
  1851. struct fuse *f;
  1852. const char *newname;
  1853. struct fuse_link_in *arg;
  1854. struct fuse_entry_param e = {0};
  1855. arg = fuse_hdr_arg(hdr_);
  1856. newname = PARAM(arg);
  1857. f = req_fuse_prepare(req);
  1858. rv = get_path2(f,
  1859. arg->oldnodeid,NULL,
  1860. hdr_->nodeid,newname,
  1861. &oldpath,&newpath,NULL,NULL);
  1862. if(!rv)
  1863. {
  1864. rv = f->fs->op.link(oldpath,newpath,&e.attr,&e.timeout);
  1865. if(rv == 0)
  1866. rv = set_path_info(f,hdr_->nodeid,newname,&e);
  1867. free_path2(f,arg->oldnodeid,hdr_->nodeid,NULL,NULL,oldpath,newpath);
  1868. }
  1869. reply_entry(req,&e,rv);
  1870. }
  1871. static
  1872. void
  1873. fuse_do_release(struct fuse *f,
  1874. uint64_t ino,
  1875. fuse_file_info_t *fi)
  1876. {
  1877. struct node *node;
  1878. uint64_t fh;
  1879. int was_hidden;
  1880. fh = 0;
  1881. f->fs->op.release(fi);
  1882. pthread_mutex_lock(&f->lock);
  1883. node = get_node(f,ino);
  1884. assert(node->open_count > 0);
  1885. node->open_count--;
  1886. was_hidden = 0;
  1887. if(node->is_hidden && (node->open_count == 0))
  1888. {
  1889. was_hidden = 1;
  1890. node->is_hidden = 0;
  1891. fh = node->hidden_fh;
  1892. }
  1893. pthread_mutex_unlock(&f->lock);
  1894. if(was_hidden)
  1895. f->fs->op.free_hide(fh);
  1896. }
  1897. static
  1898. void
  1899. fuse_lib_create(fuse_req_t req,
  1900. struct fuse_in_header *hdr_)
  1901. {
  1902. int err;
  1903. char *path;
  1904. struct fuse *f;
  1905. const char *name;
  1906. fuse_file_info_t ffi = {0};
  1907. struct fuse_entry_param e;
  1908. struct fuse_create_in *arg;
  1909. arg = fuse_hdr_arg(hdr_);
  1910. name = PARAM(arg);
  1911. ffi.flags = arg->flags;
  1912. if(req->f->conn.proto_minor >= 12)
  1913. req->ctx.umask = arg->umask;
  1914. else
  1915. name = (char*)arg + sizeof(struct fuse_open_in);
  1916. f = req_fuse_prepare(req);
  1917. err = get_path_name(f,hdr_->nodeid,name,&path);
  1918. if(!err)
  1919. {
  1920. err = f->fs->op.create(path,arg->mode,&ffi);
  1921. if(!err)
  1922. {
  1923. err = lookup_path(f,hdr_->nodeid,name,path,&e,&ffi);
  1924. if(err)
  1925. {
  1926. f->fs->op.release(&ffi);
  1927. }
  1928. else if(!S_ISREG(e.attr.st_mode))
  1929. {
  1930. err = -EIO;
  1931. f->fs->op.release(&ffi);
  1932. forget_node(f,e.ino,1);
  1933. }
  1934. }
  1935. }
  1936. if(!err)
  1937. {
  1938. pthread_mutex_lock(&f->lock);
  1939. get_node(f,e.ino)->open_count++;
  1940. pthread_mutex_unlock(&f->lock);
  1941. if(fuse_reply_create(req,&e,&ffi) == -ENOENT)
  1942. {
  1943. /* The open syscall was interrupted,so it
  1944. must be cancelled */
  1945. fuse_do_release(f,e.ino,&ffi);
  1946. forget_node(f,e.ino,1);
  1947. }
  1948. }
  1949. else
  1950. {
  1951. reply_err(req,err);
  1952. }
  1953. free_path(f,hdr_->nodeid,path);
  1954. }
  1955. static
  1956. void
  1957. open_auto_cache(struct fuse *f,
  1958. uint64_t ino,
  1959. const char *path,
  1960. fuse_file_info_t *fi)
  1961. {
  1962. struct node *node;
  1963. fuse_timeouts_t timeout;
  1964. pthread_mutex_lock(&f->lock);
  1965. node = get_node(f,ino);
  1966. if(node->is_stat_cache_valid)
  1967. {
  1968. int err;
  1969. struct stat stbuf;
  1970. pthread_mutex_unlock(&f->lock);
  1971. err = f->fs->op.fgetattr(fi,&stbuf,&timeout);
  1972. pthread_mutex_lock(&f->lock);
  1973. if(!err)
  1974. update_stat(node,&stbuf);
  1975. else
  1976. node->is_stat_cache_valid = 0;
  1977. }
  1978. if(node->is_stat_cache_valid)
  1979. fi->keep_cache = 1;
  1980. node->is_stat_cache_valid = 1;
  1981. pthread_mutex_unlock(&f->lock);
  1982. }
  1983. static
  1984. void
  1985. fuse_lib_open(fuse_req_t req,
  1986. struct fuse_in_header *hdr_)
  1987. {
  1988. int err;
  1989. char *path;
  1990. struct fuse *f;
  1991. fuse_file_info_t ffi = {0};
  1992. struct fuse_open_in *arg;
  1993. arg = fuse_hdr_arg(hdr_);
  1994. ffi.flags = arg->flags;
  1995. f = req_fuse_prepare(req);
  1996. err = get_path(f,hdr_->nodeid,&path);
  1997. if(!err)
  1998. {
  1999. err = f->fs->op.open(path,&ffi);
  2000. if(!err)
  2001. {
  2002. if(ffi.auto_cache)
  2003. open_auto_cache(f,hdr_->nodeid,path,&ffi);
  2004. }
  2005. }
  2006. if(!err)
  2007. {
  2008. pthread_mutex_lock(&f->lock);
  2009. get_node(f,hdr_->nodeid)->open_count++;
  2010. pthread_mutex_unlock(&f->lock);
  2011. /* The open syscall was interrupted,so it must be cancelled */
  2012. if(fuse_reply_open(req,&ffi) == -ENOENT)
  2013. fuse_do_release(f,hdr_->nodeid,&ffi);
  2014. }
  2015. else
  2016. {
  2017. reply_err(req,err);
  2018. }
  2019. free_path(f,hdr_->nodeid,path);
  2020. }
  2021. static
  2022. void
  2023. fuse_lib_read(fuse_req_t req,
  2024. struct fuse_in_header *hdr_)
  2025. {
  2026. int res;
  2027. struct fuse *f;
  2028. fuse_file_info_t ffi = {0};
  2029. struct fuse_bufvec *buf = NULL;
  2030. struct fuse_read_in *arg;
  2031. arg = fuse_hdr_arg(hdr_);
  2032. ffi.fh = arg->fh;
  2033. if(req->f->conn.proto_minor >= 9)
  2034. {
  2035. ffi.flags = arg->flags;
  2036. ffi.lock_owner = arg->lock_owner;
  2037. }
  2038. f = req_fuse_prepare(req);
  2039. res = f->fs->op.read_buf(&ffi,&buf,arg->size,arg->offset);
  2040. if(res >= 0)
  2041. fuse_reply_data(req,buf,FUSE_BUF_SPLICE_MOVE);
  2042. else
  2043. reply_err(req,res);
  2044. fuse_free_buf(buf);
  2045. }
  2046. static
  2047. void
  2048. fuse_lib_write(fuse_req_t req,
  2049. struct fuse_in_header *hdr_)
  2050. {
  2051. int res;
  2052. char *data;
  2053. struct fuse *f;
  2054. fuse_file_info_t ffi = {0};
  2055. struct fuse_write_in *arg;
  2056. arg = fuse_hdr_arg(hdr_);
  2057. ffi.fh = arg->fh;
  2058. ffi.writepage = !!(arg->write_flags & 1);
  2059. if(req->f->conn.proto_minor < 9)
  2060. {
  2061. data = ((char*)arg) + FUSE_COMPAT_WRITE_IN_SIZE;
  2062. }
  2063. else
  2064. {
  2065. ffi.flags = arg->flags;
  2066. ffi.lock_owner = arg->lock_owner;
  2067. data = PARAM(arg);
  2068. }
  2069. f = req_fuse_prepare(req);
  2070. res = f->fs->op.write(&ffi,data,arg->size,arg->offset);
  2071. free_path(f,hdr_->nodeid,NULL);
  2072. if(res >= 0)
  2073. fuse_reply_write(req,res);
  2074. else
  2075. reply_err(req,res);
  2076. }
  2077. static
  2078. void
  2079. fuse_lib_fsync(fuse_req_t req,
  2080. struct fuse_in_header *hdr_)
  2081. {
  2082. int err;
  2083. struct fuse *f;
  2084. struct fuse_fsync_in *arg;
  2085. fuse_file_info_t ffi = {0};
  2086. arg = fuse_hdr_arg(hdr_);
  2087. ffi.fh = arg->fh;
  2088. f = req_fuse_prepare(req);
  2089. err = f->fs->op.fsync(&ffi,
  2090. !!(arg->fsync_flags & 1));
  2091. reply_err(req,err);
  2092. }
  2093. static
  2094. struct fuse_dh*
  2095. get_dirhandle(const fuse_file_info_t *llfi,
  2096. fuse_file_info_t *fi)
  2097. {
  2098. struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
  2099. memset(fi,0,sizeof(fuse_file_info_t));
  2100. fi->fh = dh->fh;
  2101. return dh;
  2102. }
  2103. static
  2104. void
  2105. fuse_lib_opendir(fuse_req_t req,
  2106. struct fuse_in_header *hdr_)
  2107. {
  2108. int err;
  2109. char *path;
  2110. struct fuse_dh *dh;
  2111. fuse_file_info_t llffi = {0};
  2112. fuse_file_info_t ffi = {0};
  2113. struct fuse *f;
  2114. struct fuse_open_in *arg;
  2115. arg = fuse_hdr_arg(hdr_);
  2116. llffi.flags = arg->flags;
  2117. f = req_fuse_prepare(req);
  2118. dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
  2119. if(dh == NULL)
  2120. {
  2121. reply_err(req,-ENOMEM);
  2122. return;
  2123. }
  2124. fuse_dirents_init(&dh->d);
  2125. fuse_mutex_init(&dh->lock);
  2126. llffi.fh = (uintptr_t)dh;
  2127. ffi.flags = llffi.flags;
  2128. err = get_path(f,hdr_->nodeid,&path);
  2129. if(!err)
  2130. {
  2131. err = f->fs->op.opendir(path,&ffi);
  2132. dh->fh = ffi.fh;
  2133. llffi.keep_cache = ffi.keep_cache;
  2134. llffi.cache_readdir = ffi.cache_readdir;
  2135. }
  2136. if(!err)
  2137. {
  2138. if(fuse_reply_open(req,&llffi) == -ENOENT)
  2139. {
  2140. /* The opendir syscall was interrupted,so it
  2141. must be cancelled */
  2142. f->fs->op.releasedir(&ffi);
  2143. pthread_mutex_destroy(&dh->lock);
  2144. free(dh);
  2145. }
  2146. }
  2147. else
  2148. {
  2149. reply_err(req,err);
  2150. pthread_mutex_destroy(&dh->lock);
  2151. free(dh);
  2152. }
  2153. free_path(f,hdr_->nodeid,path);
  2154. }
  2155. static
  2156. size_t
  2157. readdir_buf_size(fuse_dirents_t *d_,
  2158. size_t size_,
  2159. off_t off_)
  2160. {
  2161. if(off_ >= kv_size(d_->offs))
  2162. return 0;
  2163. if((kv_A(d_->offs,off_) + size_) > kv_size(d_->data))
  2164. return (kv_size(d_->data) - kv_A(d_->offs,off_));
  2165. return size_;
  2166. }
  2167. static
  2168. char*
  2169. readdir_buf(fuse_dirents_t *d_,
  2170. off_t off_)
  2171. {
  2172. size_t i;
  2173. i = kv_A(d_->offs,off_);
  2174. return &kv_A(d_->data,i);
  2175. }
  2176. static
  2177. void
  2178. fuse_lib_readdir(fuse_req_t req_,
  2179. struct fuse_in_header *hdr_)
  2180. {
  2181. int rv;
  2182. size_t size;
  2183. struct fuse *f;
  2184. fuse_dirents_t *d;
  2185. struct fuse_dh *dh;
  2186. fuse_file_info_t ffi = {0};
  2187. fuse_file_info_t llffi = {0};
  2188. struct fuse_read_in *arg;
  2189. arg = fuse_hdr_arg(hdr_);
  2190. size = arg->size;
  2191. llffi.fh = arg->fh;
  2192. f = req_fuse_prepare(req_);
  2193. dh = get_dirhandle(&llffi,&ffi);
  2194. d = &dh->d;
  2195. pthread_mutex_lock(&dh->lock);
  2196. rv = 0;
  2197. if((arg->offset == 0) || (kv_size(d->data) == 0))
  2198. rv = f->fs->op.readdir(&ffi,d);
  2199. if(rv)
  2200. {
  2201. reply_err(req_,rv);
  2202. goto out;
  2203. }
  2204. size = readdir_buf_size(d,size,arg->offset);
  2205. fuse_reply_buf(req_,
  2206. readdir_buf(d,arg->offset),
  2207. size);
  2208. out:
  2209. pthread_mutex_unlock(&dh->lock);
  2210. }
  2211. static
  2212. void
  2213. fuse_lib_readdir_plus(fuse_req_t req_,
  2214. struct fuse_in_header *hdr_)
  2215. {
  2216. int rv;
  2217. size_t size;
  2218. struct fuse *f;
  2219. fuse_dirents_t *d;
  2220. struct fuse_dh *dh;
  2221. fuse_file_info_t ffi = {0};
  2222. fuse_file_info_t llffi = {0};
  2223. struct fuse_read_in *arg;
  2224. arg = fuse_hdr_arg(hdr_);
  2225. size = arg->size;
  2226. llffi.fh = arg->fh;
  2227. f = req_fuse_prepare(req_);
  2228. dh = get_dirhandle(&llffi,&ffi);
  2229. d = &dh->d;
  2230. pthread_mutex_lock(&dh->lock);
  2231. rv = 0;
  2232. if((arg->offset == 0) || (kv_size(d->data) == 0))
  2233. rv = f->fs->op.readdir_plus(&ffi,d);
  2234. if(rv)
  2235. {
  2236. reply_err(req_,rv);
  2237. goto out;
  2238. }
  2239. size = readdir_buf_size(d,size,arg->offset);
  2240. fuse_reply_buf(req_,
  2241. readdir_buf(d,arg->offset),
  2242. size);
  2243. out:
  2244. pthread_mutex_unlock(&dh->lock);
  2245. }
  2246. static
  2247. void
  2248. fuse_lib_releasedir(fuse_req_t req_,
  2249. struct fuse_in_header *hdr_)
  2250. {
  2251. struct fuse *f;
  2252. struct fuse_dh *dh;
  2253. fuse_file_info_t ffi;
  2254. fuse_file_info_t llffi = {0};
  2255. struct fuse_release_in *arg;
  2256. arg = fuse_hdr_arg(hdr_);
  2257. llffi.fh = arg->fh;
  2258. llffi.flags = arg->flags;
  2259. f = req_fuse_prepare(req_);
  2260. dh = get_dirhandle(&llffi,&ffi);
  2261. f->fs->op.releasedir(&ffi);
  2262. /* Done to keep race condition between last readdir reply and the unlock */
  2263. pthread_mutex_lock(&dh->lock);
  2264. pthread_mutex_unlock(&dh->lock);
  2265. pthread_mutex_destroy(&dh->lock);
  2266. fuse_dirents_free(&dh->d);
  2267. free(dh);
  2268. reply_err(req_,0);
  2269. }
  2270. static
  2271. void
  2272. fuse_lib_fsyncdir(fuse_req_t req,
  2273. struct fuse_in_header *hdr_)
  2274. {
  2275. int err;
  2276. struct fuse *f;
  2277. fuse_file_info_t ffi;
  2278. fuse_file_info_t llffi = {0};
  2279. struct fuse_fsync_in *arg;
  2280. arg = fuse_hdr_arg(hdr_);
  2281. llffi.fh = arg->fh;
  2282. f = req_fuse_prepare(req);
  2283. get_dirhandle(&llffi,&ffi);
  2284. err = f->fs->op.fsyncdir(&ffi,
  2285. !!(arg->fsync_flags & FUSE_FSYNC_FDATASYNC));
  2286. reply_err(req,err);
  2287. }
  2288. static
  2289. void
  2290. fuse_lib_statfs(fuse_req_t req,
  2291. struct fuse_in_header *hdr_)
  2292. {
  2293. int err = 0;
  2294. char *path = NULL;
  2295. struct fuse *f;
  2296. struct statvfs buf = {0};
  2297. f = req_fuse_prepare(req);
  2298. if(hdr_->nodeid)
  2299. err = get_path(f,hdr_->nodeid,&path);
  2300. if(!err)
  2301. {
  2302. err = f->fs->op.statfs(path ? path : "/",&buf);
  2303. free_path(f,hdr_->nodeid,path);
  2304. }
  2305. if(!err)
  2306. fuse_reply_statfs(req,&buf);
  2307. else
  2308. reply_err(req,err);
  2309. }
  2310. static
  2311. void
  2312. fuse_lib_setxattr(fuse_req_t req,
  2313. struct fuse_in_header *hdr_)
  2314. {
  2315. int err;
  2316. char *path;
  2317. const char *name;
  2318. const char *value;
  2319. struct fuse *f;
  2320. struct fuse_setxattr_in *arg;
  2321. arg = fuse_hdr_arg(hdr_);
  2322. if((req->f->conn.capable & FUSE_SETXATTR_EXT) && (req->f->conn.want & FUSE_SETXATTR_EXT))
  2323. name = PARAM(arg);
  2324. else
  2325. name = (((char*)arg) + FUSE_COMPAT_SETXATTR_IN_SIZE);
  2326. value = (name + strlen(name) + 1);
  2327. f = req_fuse_prepare(req);
  2328. err = get_path(f,hdr_->nodeid,&path);
  2329. if(!err)
  2330. {
  2331. err = f->fs->op.setxattr(path,name,value,arg->size,arg->flags);
  2332. free_path(f,hdr_->nodeid,path);
  2333. }
  2334. reply_err(req,err);
  2335. }
  2336. static
  2337. int
  2338. common_getxattr(struct fuse *f,
  2339. fuse_req_t req,
  2340. uint64_t ino,
  2341. const char *name,
  2342. char *value,
  2343. size_t size)
  2344. {
  2345. int err;
  2346. char *path;
  2347. err = get_path(f,ino,&path);
  2348. if(!err)
  2349. {
  2350. err = f->fs->op.getxattr(path,name,value,size);
  2351. free_path(f,ino,path);
  2352. }
  2353. return err;
  2354. }
  2355. static
  2356. void
  2357. fuse_lib_getxattr(fuse_req_t req,
  2358. struct fuse_in_header *hdr_)
  2359. {
  2360. int res;
  2361. struct fuse *f;
  2362. const char* name;
  2363. struct fuse_getxattr_in *arg;
  2364. arg = fuse_hdr_arg(hdr_);
  2365. name = PARAM(arg);
  2366. f = req_fuse_prepare(req);
  2367. if(arg->size)
  2368. {
  2369. char *value = (char*)malloc(arg->size);
  2370. if(value == NULL)
  2371. {
  2372. reply_err(req,-ENOMEM);
  2373. return;
  2374. }
  2375. res = common_getxattr(f,req,hdr_->nodeid,name,value,arg->size);
  2376. if(res > 0)
  2377. fuse_reply_buf(req,value,res);
  2378. else
  2379. reply_err(req,res);
  2380. free(value);
  2381. }
  2382. else
  2383. {
  2384. res = common_getxattr(f,req,hdr_->nodeid,name,NULL,0);
  2385. if(res >= 0)
  2386. fuse_reply_xattr(req,res);
  2387. else
  2388. reply_err(req,res);
  2389. }
  2390. }
  2391. static
  2392. int
  2393. common_listxattr(struct fuse *f,
  2394. fuse_req_t req,
  2395. uint64_t ino,
  2396. char *list,
  2397. size_t size)
  2398. {
  2399. char *path;
  2400. int err;
  2401. err = get_path(f,ino,&path);
  2402. if(!err)
  2403. {
  2404. err = f->fs->op.listxattr(path,list,size);
  2405. free_path(f,ino,path);
  2406. }
  2407. return err;
  2408. }
  2409. static
  2410. void
  2411. fuse_lib_listxattr(fuse_req_t req,
  2412. struct fuse_in_header *hdr_)
  2413. {
  2414. int res;
  2415. struct fuse *f;
  2416. struct fuse_getxattr_in *arg;
  2417. arg = fuse_hdr_arg(hdr_);
  2418. f = req_fuse_prepare(req);
  2419. if(arg->size)
  2420. {
  2421. char *list = (char*)malloc(arg->size);
  2422. if(list == NULL)
  2423. {
  2424. reply_err(req,-ENOMEM);
  2425. return;
  2426. }
  2427. res = common_listxattr(f,req,hdr_->nodeid,list,arg->size);
  2428. if(res > 0)
  2429. fuse_reply_buf(req,list,res);
  2430. else
  2431. reply_err(req,res);
  2432. free(list);
  2433. }
  2434. else
  2435. {
  2436. res = common_listxattr(f,req,hdr_->nodeid,NULL,0);
  2437. if(res >= 0)
  2438. fuse_reply_xattr(req,res);
  2439. else
  2440. reply_err(req,res);
  2441. }
  2442. }
  2443. static
  2444. void
  2445. fuse_lib_removexattr(fuse_req_t req,
  2446. const struct fuse_in_header *hdr_)
  2447. {
  2448. int err;
  2449. char *path;
  2450. const char *name;
  2451. struct fuse *f;
  2452. name = fuse_hdr_arg(hdr_);
  2453. f = req_fuse_prepare(req);
  2454. err = get_path(f,hdr_->nodeid,&path);
  2455. if(!err)
  2456. {
  2457. err = f->fs->op.removexattr(path,name);
  2458. free_path(f,hdr_->nodeid,path);
  2459. }
  2460. reply_err(req,err);
  2461. }
  2462. static
  2463. void
  2464. fuse_lib_copy_file_range(fuse_req_t req_,
  2465. const struct fuse_in_header *hdr_)
  2466. {
  2467. ssize_t rv;
  2468. struct fuse *f;
  2469. fuse_file_info_t ffi_in = {0};
  2470. fuse_file_info_t ffi_out = {0};
  2471. const struct fuse_copy_file_range_in *arg;
  2472. arg = fuse_hdr_arg(hdr_);
  2473. ffi_in.fh = arg->fh_in;
  2474. ffi_out.fh = arg->fh_out;
  2475. f = req_fuse_prepare(req_);
  2476. rv = f->fs->op.copy_file_range(&ffi_in,
  2477. arg->off_in,
  2478. &ffi_out,
  2479. arg->off_out,
  2480. arg->len,
  2481. arg->flags);
  2482. if(rv >= 0)
  2483. fuse_reply_write(req_,rv);
  2484. else
  2485. reply_err(req_,rv);
  2486. }
  2487. static
  2488. struct lock*
  2489. locks_conflict(struct node *node,
  2490. const struct lock *lock)
  2491. {
  2492. struct lock *l;
  2493. for(l = node->locks; l; l = l->next)
  2494. if(l->owner != lock->owner &&
  2495. lock->start <= l->end && l->start <= lock->end &&
  2496. (l->type == F_WRLCK || lock->type == F_WRLCK))
  2497. break;
  2498. return l;
  2499. }
  2500. static
  2501. void
  2502. delete_lock(struct lock **lockp)
  2503. {
  2504. struct lock *l = *lockp;
  2505. *lockp = l->next;
  2506. free(l);
  2507. }
  2508. static
  2509. void
  2510. insert_lock(struct lock **pos,
  2511. struct lock *lock)
  2512. {
  2513. lock->next = *pos;
  2514. *pos = lock;
  2515. }
  2516. static
  2517. int
  2518. locks_insert(struct node *node,
  2519. struct lock *lock)
  2520. {
  2521. struct lock **lp;
  2522. struct lock *newl1 = NULL;
  2523. struct lock *newl2 = NULL;
  2524. if(lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX)
  2525. {
  2526. newl1 = malloc(sizeof(struct lock));
  2527. newl2 = malloc(sizeof(struct lock));
  2528. if(!newl1 || !newl2)
  2529. {
  2530. free(newl1);
  2531. free(newl2);
  2532. return -ENOLCK;
  2533. }
  2534. }
  2535. for(lp = &node->locks; *lp;)
  2536. {
  2537. struct lock *l = *lp;
  2538. if(l->owner != lock->owner)
  2539. goto skip;
  2540. if(lock->type == l->type)
  2541. {
  2542. if(l->end < lock->start - 1)
  2543. goto skip;
  2544. if(lock->end < l->start - 1)
  2545. break;
  2546. if(l->start <= lock->start && lock->end <= l->end)
  2547. goto out;
  2548. if(l->start < lock->start)
  2549. lock->start = l->start;
  2550. if(lock->end < l->end)
  2551. lock->end = l->end;
  2552. goto delete;
  2553. }
  2554. else
  2555. {
  2556. if(l->end < lock->start)
  2557. goto skip;
  2558. if(lock->end < l->start)
  2559. break;
  2560. if(lock->start <= l->start && l->end <= lock->end)
  2561. goto delete;
  2562. if(l->end <= lock->end)
  2563. {
  2564. l->end = lock->start - 1;
  2565. goto skip;
  2566. }
  2567. if(lock->start <= l->start)
  2568. {
  2569. l->start = lock->end + 1;
  2570. break;
  2571. }
  2572. *newl2 = *l;
  2573. newl2->start = lock->end + 1;
  2574. l->end = lock->start - 1;
  2575. insert_lock(&l->next,newl2);
  2576. newl2 = NULL;
  2577. }
  2578. skip:
  2579. lp = &l->next;
  2580. continue;
  2581. delete:
  2582. delete_lock(lp);
  2583. }
  2584. if(lock->type != F_UNLCK)
  2585. {
  2586. *newl1 = *lock;
  2587. insert_lock(lp,newl1);
  2588. newl1 = NULL;
  2589. }
  2590. out:
  2591. free(newl1);
  2592. free(newl2);
  2593. return 0;
  2594. }
  2595. static
  2596. void
  2597. flock_to_lock(struct flock *flock,
  2598. struct lock *lock)
  2599. {
  2600. memset(lock,0,sizeof(struct lock));
  2601. lock->type = flock->l_type;
  2602. lock->start = flock->l_start;
  2603. lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
  2604. lock->pid = flock->l_pid;
  2605. }
  2606. static
  2607. void
  2608. lock_to_flock(struct lock *lock,
  2609. struct flock *flock)
  2610. {
  2611. flock->l_type = lock->type;
  2612. flock->l_start = lock->start;
  2613. flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
  2614. flock->l_pid = lock->pid;
  2615. }
  2616. static
  2617. int
  2618. fuse_flush_common(struct fuse *f,
  2619. fuse_req_t req,
  2620. uint64_t ino,
  2621. fuse_file_info_t *fi)
  2622. {
  2623. struct flock lock;
  2624. struct lock l;
  2625. int err;
  2626. int errlock;
  2627. memset(&lock,0,sizeof(lock));
  2628. lock.l_type = F_UNLCK;
  2629. lock.l_whence = SEEK_SET;
  2630. err = f->fs->op.flush(fi);
  2631. errlock = f->fs->op.lock(fi,F_SETLK,&lock);
  2632. if(errlock != -ENOSYS)
  2633. {
  2634. flock_to_lock(&lock,&l);
  2635. l.owner = fi->lock_owner;
  2636. pthread_mutex_lock(&f->lock);
  2637. locks_insert(get_node(f,ino),&l);
  2638. pthread_mutex_unlock(&f->lock);
  2639. /* if op.lock() is defined FLUSH is needed regardless
  2640. of op.flush() */
  2641. if(err == -ENOSYS)
  2642. err = 0;
  2643. }
  2644. return err;
  2645. }
  2646. static
  2647. void
  2648. fuse_lib_release(fuse_req_t req,
  2649. struct fuse_in_header *hdr_)
  2650. {
  2651. int err = 0;
  2652. struct fuse *f;
  2653. fuse_file_info_t ffi = {0};
  2654. struct fuse_release_in *arg;
  2655. arg = fuse_hdr_arg(hdr_);
  2656. ffi.fh = arg->fh;
  2657. ffi.flags = arg->flags;
  2658. if(req->f->conn.proto_minor >= 8)
  2659. {
  2660. ffi.flush = !!(arg->release_flags & FUSE_RELEASE_FLUSH);
  2661. ffi.lock_owner = arg->lock_owner;
  2662. }
  2663. else
  2664. {
  2665. ffi.flock_release = 1;
  2666. ffi.lock_owner = arg->lock_owner;
  2667. }
  2668. f = req_fuse_prepare(req);
  2669. if(ffi.flush)
  2670. {
  2671. err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
  2672. if(err == -ENOSYS)
  2673. err = 0;
  2674. }
  2675. fuse_do_release(f,hdr_->nodeid,&ffi);
  2676. reply_err(req,err);
  2677. }
  2678. static
  2679. void
  2680. fuse_lib_flush(fuse_req_t req,
  2681. struct fuse_in_header *hdr_)
  2682. {
  2683. int err;
  2684. struct fuse *f;
  2685. fuse_file_info_t ffi = {0};
  2686. struct fuse_flush_in *arg;
  2687. arg = fuse_hdr_arg(hdr_);
  2688. ffi.fh = arg->fh;
  2689. ffi.flush = 1;
  2690. if(req->f->conn.proto_minor >= 7)
  2691. ffi.lock_owner = arg->lock_owner;
  2692. f = req_fuse_prepare(req);
  2693. err = fuse_flush_common(f,req,hdr_->nodeid,&ffi);
  2694. reply_err(req,err);
  2695. }
  2696. static
  2697. int
  2698. fuse_lock_common(fuse_req_t req,
  2699. uint64_t ino,
  2700. fuse_file_info_t *fi,
  2701. struct flock *lock,
  2702. int cmd)
  2703. {
  2704. int err;
  2705. struct fuse *f = req_fuse_prepare(req);
  2706. err = f->fs->op.lock(fi,cmd,lock);
  2707. return err;
  2708. }
  2709. static
  2710. void
  2711. convert_fuse_file_lock(const struct fuse_file_lock *fl,
  2712. struct flock *flock)
  2713. {
  2714. memset(flock, 0, sizeof(struct flock));
  2715. flock->l_type = fl->type;
  2716. flock->l_whence = SEEK_SET;
  2717. flock->l_start = fl->start;
  2718. if (fl->end == OFFSET_MAX)
  2719. flock->l_len = 0;
  2720. else
  2721. flock->l_len = fl->end - fl->start + 1;
  2722. flock->l_pid = fl->pid;
  2723. }
  2724. static
  2725. void
  2726. fuse_lib_getlk(fuse_req_t req,
  2727. const struct fuse_in_header *hdr_)
  2728. {
  2729. int err;
  2730. struct fuse *f;
  2731. struct lock lk;
  2732. struct flock flk;
  2733. struct lock *conflict;
  2734. fuse_file_info_t ffi = {0};
  2735. const struct fuse_lk_in *arg;
  2736. arg = fuse_hdr_arg(hdr_);
  2737. ffi.fh = arg->fh;
  2738. ffi.lock_owner = arg->owner;
  2739. convert_fuse_file_lock(&arg->lk,&flk);
  2740. f = req_fuse(req);
  2741. flock_to_lock(&flk,&lk);
  2742. lk.owner = ffi.lock_owner;
  2743. pthread_mutex_lock(&f->lock);
  2744. conflict = locks_conflict(get_node(f,hdr_->nodeid),&lk);
  2745. if(conflict)
  2746. lock_to_flock(conflict,&flk);
  2747. pthread_mutex_unlock(&f->lock);
  2748. if(!conflict)
  2749. err = fuse_lock_common(req,hdr_->nodeid,&ffi,&flk,F_GETLK);
  2750. else
  2751. err = 0;
  2752. if(!err)
  2753. fuse_reply_lock(req,&flk);
  2754. else
  2755. reply_err(req,err);
  2756. }
  2757. static
  2758. void
  2759. fuse_lib_setlk(fuse_req_t req,
  2760. uint64_t ino,
  2761. fuse_file_info_t *fi,
  2762. struct flock *lock,
  2763. int sleep)
  2764. {
  2765. int err = fuse_lock_common(req,ino,fi,lock,
  2766. sleep ? F_SETLKW : F_SETLK);
  2767. if(!err)
  2768. {
  2769. struct fuse *f = req_fuse(req);
  2770. struct lock l;
  2771. flock_to_lock(lock,&l);
  2772. l.owner = fi->lock_owner;
  2773. pthread_mutex_lock(&f->lock);
  2774. locks_insert(get_node(f,ino),&l);
  2775. pthread_mutex_unlock(&f->lock);
  2776. }
  2777. reply_err(req,err);
  2778. }
  2779. static
  2780. void
  2781. fuse_lib_flock(fuse_req_t req,
  2782. uint64_t ino,
  2783. fuse_file_info_t *fi,
  2784. int op)
  2785. {
  2786. int err;
  2787. struct fuse *f = req_fuse_prepare(req);
  2788. err = f->fs->op.flock(fi,op);
  2789. reply_err(req,err);
  2790. }
  2791. static
  2792. void
  2793. fuse_lib_bmap(fuse_req_t req,
  2794. const struct fuse_in_header *hdr_)
  2795. {
  2796. int err;
  2797. char *path;
  2798. struct fuse *f;
  2799. uint64_t block;
  2800. const struct fuse_bmap_in *arg;
  2801. arg = fuse_hdr_arg(hdr_);
  2802. block = arg->block;
  2803. f = req_fuse_prepare(req);
  2804. err = get_path(f,hdr_->nodeid,&path);
  2805. if(!err)
  2806. {
  2807. err = f->fs->op.bmap(path,arg->blocksize,&block);
  2808. free_path(f,hdr_->nodeid,path);
  2809. }
  2810. if(!err)
  2811. fuse_reply_bmap(req,block);
  2812. else
  2813. reply_err(req,err);
  2814. }
  2815. static
  2816. void
  2817. fuse_lib_ioctl(fuse_req_t req,
  2818. const struct fuse_in_header *hdr_)
  2819. {
  2820. int err;
  2821. char *out_buf = NULL;
  2822. struct fuse *f = req_fuse_prepare(req);
  2823. fuse_file_info_t ffi;
  2824. fuse_file_info_t llffi = {0};
  2825. const void *in_buf;
  2826. uint32_t out_size;
  2827. const struct fuse_ioctl_in *arg;
  2828. arg = fuse_hdr_arg(hdr_);
  2829. if((arg->flags & FUSE_IOCTL_DIR) && !(req->f->conn.want & FUSE_CAP_IOCTL_DIR))
  2830. {
  2831. fuse_reply_err(req,ENOTTY);
  2832. return;
  2833. }
  2834. if((sizeof(void*) == 4) &&
  2835. (req->f->conn.proto_minor >= 16) &&
  2836. !(arg->flags & FUSE_IOCTL_32BIT))
  2837. {
  2838. req->ioctl_64bit = 1;
  2839. }
  2840. llffi.fh = arg->fh;
  2841. out_size = arg->out_size;
  2842. in_buf = (arg->in_size ? PARAM(arg) : NULL);
  2843. err = -EPERM;
  2844. if(arg->flags & FUSE_IOCTL_UNRESTRICTED)
  2845. goto err;
  2846. if(arg->flags & FUSE_IOCTL_DIR)
  2847. get_dirhandle(&llffi,&ffi);
  2848. else
  2849. ffi = llffi;
  2850. if(out_size)
  2851. {
  2852. err = -ENOMEM;
  2853. out_buf = malloc(out_size);
  2854. if(!out_buf)
  2855. goto err;
  2856. }
  2857. assert(!arg->in_size || !out_size || arg->in_size == out_size);
  2858. if(out_buf)
  2859. memcpy(out_buf,in_buf,arg->in_size);
  2860. err = f->fs->op.ioctl(&ffi,
  2861. arg->cmd,
  2862. (void*)(uintptr_t)arg->arg,
  2863. arg->flags,
  2864. out_buf ?: (void *)in_buf,
  2865. &out_size);
  2866. if(err < 0)
  2867. goto err;
  2868. fuse_reply_ioctl(req,err,out_buf,out_size);
  2869. goto out;
  2870. err:
  2871. reply_err(req,err);
  2872. out:
  2873. free(out_buf);
  2874. }
  2875. static
  2876. void
  2877. fuse_lib_poll(fuse_req_t req,
  2878. const struct fuse_in_header *hdr_)
  2879. {
  2880. int err;
  2881. struct fuse *f = req_fuse_prepare(req);
  2882. unsigned revents = 0;
  2883. fuse_file_info_t ffi = {0};
  2884. fuse_pollhandle_t *ph = NULL;
  2885. const struct fuse_poll_in *arg;
  2886. arg = fuse_hdr_arg(hdr_);
  2887. ffi.fh = arg->fh;
  2888. if(arg->flags & FUSE_POLL_SCHEDULE_NOTIFY)
  2889. {
  2890. ph = (fuse_pollhandle_t*)malloc(sizeof(fuse_pollhandle_t));
  2891. if(ph == NULL)
  2892. {
  2893. fuse_reply_err(req,ENOMEM);
  2894. return;
  2895. }
  2896. ph->kh = arg->kh;
  2897. ph->ch = req->ch;
  2898. ph->f = req->f;
  2899. }
  2900. err = f->fs->op.poll(&ffi,ph,&revents);
  2901. if(!err)
  2902. fuse_reply_poll(req,revents);
  2903. else
  2904. reply_err(req,err);
  2905. }
  2906. static
  2907. void
  2908. fuse_lib_fallocate(fuse_req_t req,
  2909. const struct fuse_in_header *hdr_)
  2910. {
  2911. int err;
  2912. struct fuse *f;
  2913. fuse_file_info_t ffi = {0};
  2914. const struct fuse_fallocate_in *arg;
  2915. arg = fuse_hdr_arg(hdr_);
  2916. ffi.fh = arg->fh;
  2917. f = req_fuse_prepare(req);
  2918. err = f->fs->op.fallocate(&ffi,
  2919. arg->mode,
  2920. arg->offset,
  2921. arg->length);
  2922. reply_err(req,err);
  2923. }
  2924. static
  2925. int
  2926. remembered_node_cmp(const void *a_,
  2927. const void *b_)
  2928. {
  2929. const remembered_node_t *a = a_;
  2930. const remembered_node_t *b = b_;
  2931. return (a->time - b->time);
  2932. }
  2933. static
  2934. void
  2935. remembered_nodes_sort(struct fuse *f_)
  2936. {
  2937. pthread_mutex_lock(&f_->lock);
  2938. qsort(&kv_first(f_->remembered_nodes),
  2939. kv_size(f_->remembered_nodes),
  2940. sizeof(remembered_node_t),
  2941. remembered_node_cmp);
  2942. pthread_mutex_unlock(&f_->lock);
  2943. }
  2944. #define MAX_PRUNE 100
  2945. #define MAX_CHECK 1000
  2946. int
  2947. fuse_prune_some_remembered_nodes(struct fuse *f_,
  2948. int *offset_)
  2949. {
  2950. time_t now;
  2951. int pruned;
  2952. int checked;
  2953. pthread_mutex_lock(&f_->lock);
  2954. pruned = 0;
  2955. checked = 0;
  2956. now = current_time();
  2957. while(*offset_ < kv_size(f_->remembered_nodes))
  2958. {
  2959. time_t age;
  2960. remembered_node_t *fn = &kv_A(f_->remembered_nodes,*offset_);
  2961. if(pruned >= MAX_PRUNE)
  2962. break;
  2963. if(checked >= MAX_CHECK)
  2964. break;
  2965. checked++;
  2966. age = (now - fn->time);
  2967. if(f_->conf.remember > age)
  2968. break;
  2969. assert(fn->node->nlookup == 1);
  2970. /* Don't forget active directories */
  2971. if(fn->node->refctr > 1)
  2972. {
  2973. (*offset_)++;
  2974. continue;
  2975. }
  2976. fn->node->nlookup = 0;
  2977. unref_node(f_,fn->node);
  2978. kv_delete(f_->remembered_nodes,*offset_);
  2979. pruned++;
  2980. }
  2981. pthread_mutex_unlock(&f_->lock);
  2982. if((pruned < MAX_PRUNE) && (checked < MAX_CHECK))
  2983. *offset_ = -1;
  2984. return pruned;
  2985. }
  2986. #undef MAX_PRUNE
  2987. #undef MAX_CHECK
  2988. static
  2989. void
  2990. sleep_100ms(void)
  2991. {
  2992. const struct timespec ms100 = {0,100 * 1000000};
  2993. nanosleep(&ms100,NULL);
  2994. }
  2995. void
  2996. fuse_prune_remembered_nodes(struct fuse *f_)
  2997. {
  2998. int offset;
  2999. int pruned;
  3000. offset = 0;
  3001. pruned = 0;
  3002. for(;;)
  3003. {
  3004. pruned += fuse_prune_some_remembered_nodes(f_,&offset);
  3005. if(offset >= 0)
  3006. {
  3007. sleep_100ms();
  3008. continue;
  3009. }
  3010. break;
  3011. }
  3012. if(pruned > 0)
  3013. remembered_nodes_sort(f_);
  3014. }
  3015. static struct fuse_lowlevel_ops fuse_path_ops =
  3016. {
  3017. .access = fuse_lib_access,
  3018. .bmap = fuse_lib_bmap,
  3019. .copy_file_range = fuse_lib_copy_file_range,
  3020. .create = fuse_lib_create,
  3021. .destroy = fuse_lib_destroy,
  3022. .fallocate = fuse_lib_fallocate,
  3023. .flock = fuse_lib_flock,
  3024. .flush = fuse_lib_flush,
  3025. .forget = fuse_lib_forget,
  3026. .forget_multi = fuse_lib_forget_multi,
  3027. .fsync = fuse_lib_fsync,
  3028. .fsyncdir = fuse_lib_fsyncdir,
  3029. .getattr = fuse_lib_getattr,
  3030. .getlk = fuse_lib_getlk,
  3031. .getxattr = fuse_lib_getxattr,
  3032. .init = fuse_lib_init,
  3033. .ioctl = fuse_lib_ioctl,
  3034. .link = fuse_lib_link,
  3035. .listxattr = fuse_lib_listxattr,
  3036. .lookup = fuse_lib_lookup,
  3037. .mkdir = fuse_lib_mkdir,
  3038. .mknod = fuse_lib_mknod,
  3039. .open = fuse_lib_open,
  3040. .opendir = fuse_lib_opendir,
  3041. .poll = fuse_lib_poll,
  3042. .read = fuse_lib_read,
  3043. .readdir = fuse_lib_readdir,
  3044. .readdir_plus = fuse_lib_readdir_plus,
  3045. .readlink = fuse_lib_readlink,
  3046. .release = fuse_lib_release,
  3047. .releasedir = fuse_lib_releasedir,
  3048. .removexattr = fuse_lib_removexattr,
  3049. .rename = fuse_lib_rename,
  3050. .retrieve_reply = NULL,
  3051. .rmdir = fuse_lib_rmdir,
  3052. .setattr = fuse_lib_setattr,
  3053. .setlk = fuse_lib_setlk,
  3054. .setxattr = fuse_lib_setxattr,
  3055. .statfs = fuse_lib_statfs,
  3056. .symlink = fuse_lib_symlink,
  3057. .unlink = fuse_lib_unlink,
  3058. .write = fuse_lib_write,
  3059. };
  3060. int
  3061. fuse_notify_poll(fuse_pollhandle_t *ph)
  3062. {
  3063. return fuse_lowlevel_notify_poll(ph);
  3064. }
  3065. int
  3066. fuse_exited(struct fuse *f)
  3067. {
  3068. return fuse_session_exited(f->se);
  3069. }
  3070. struct fuse_session*
  3071. fuse_get_session(struct fuse *f)
  3072. {
  3073. return f->se;
  3074. }
  3075. void
  3076. fuse_exit(struct fuse *f)
  3077. {
  3078. f->se->exited = 1;
  3079. }
  3080. struct fuse_context*
  3081. fuse_get_context(void)
  3082. {
  3083. return &fuse_get_context_internal()->ctx;
  3084. }
  3085. enum {
  3086. KEY_HELP,
  3087. };
  3088. #define FUSE_LIB_OPT(t,p,v) { t,offsetof(struct fuse_config,p),v }
  3089. static const struct fuse_opt fuse_lib_opts[] =
  3090. {
  3091. FUSE_OPT_KEY("-h", KEY_HELP),
  3092. FUSE_OPT_KEY("--help", KEY_HELP),
  3093. FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
  3094. FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
  3095. FUSE_LIB_OPT("debug", debug,1),
  3096. FUSE_LIB_OPT("-d", debug,1),
  3097. FUSE_LIB_OPT("nogc", nogc,1),
  3098. FUSE_LIB_OPT("umask=", set_mode,1),
  3099. FUSE_LIB_OPT("umask=%o", umask,0),
  3100. FUSE_LIB_OPT("uid=", set_uid,1),
  3101. FUSE_LIB_OPT("uid=%d", uid,0),
  3102. FUSE_LIB_OPT("gid=", set_gid,1),
  3103. FUSE_LIB_OPT("gid=%d", gid,0),
  3104. FUSE_LIB_OPT("noforget", remember,-1),
  3105. FUSE_LIB_OPT("remember=%u", remember,0),
  3106. FUSE_LIB_OPT("threads=%d", read_thread_count,0),
  3107. FUSE_LIB_OPT("read-thread-count=%d", read_thread_count,0),
  3108. FUSE_LIB_OPT("process-thread-count=%d", process_thread_count,-1),
  3109. FUSE_LIB_OPT("pin-threads=%s", pin_threads, 0),
  3110. FUSE_OPT_END
  3111. };
  3112. static void fuse_lib_help(void)
  3113. {
  3114. fprintf(stderr,
  3115. " -o umask=M set file permissions (octal)\n"
  3116. " -o uid=N set file owner\n"
  3117. " -o gid=N set file group\n"
  3118. " -o noforget never forget cached inodes\n"
  3119. " -o remember=T remember cached inodes for T seconds (0s)\n"
  3120. " -o threads=NUM number of worker threads. 0 = autodetect.\n"
  3121. " Negative values autodetect then divide by\n"
  3122. " absolute value. default = 0\n"
  3123. "\n");
  3124. }
  3125. static
  3126. int
  3127. fuse_lib_opt_proc(void *data,
  3128. const char *arg,
  3129. int key,
  3130. struct fuse_args *outargs)
  3131. {
  3132. (void)arg; (void)outargs;
  3133. if(key == KEY_HELP)
  3134. {
  3135. struct fuse_config *conf = (struct fuse_config *)data;
  3136. fuse_lib_help();
  3137. conf->help = 1;
  3138. }
  3139. return 1;
  3140. }
  3141. int
  3142. fuse_is_lib_option(const char *opt)
  3143. {
  3144. return fuse_lowlevel_is_lib_option(opt) || fuse_opt_match(fuse_lib_opts,opt);
  3145. }
  3146. struct fuse_fs*
  3147. fuse_fs_new(const struct fuse_operations *op,
  3148. size_t op_size)
  3149. {
  3150. struct fuse_fs *fs;
  3151. if(sizeof(struct fuse_operations) < op_size)
  3152. {
  3153. fprintf(stderr,"fuse: warning: library too old,some operations may not not work\n");
  3154. op_size = sizeof(struct fuse_operations);
  3155. }
  3156. fs = (struct fuse_fs *)calloc(1,sizeof(struct fuse_fs));
  3157. if(!fs)
  3158. {
  3159. fprintf(stderr,"fuse: failed to allocate fuse_fs object\n");
  3160. return NULL;
  3161. }
  3162. if(op)
  3163. memcpy(&fs->op,op,op_size);
  3164. return fs;
  3165. }
  3166. static
  3167. int
  3168. node_table_init(struct node_table *t)
  3169. {
  3170. t->size = NODE_TABLE_MIN_SIZE;
  3171. t->array = (struct node **)calloc(1,sizeof(struct node *) * t->size);
  3172. if(t->array == NULL)
  3173. {
  3174. fprintf(stderr,"fuse: memory allocation failed\n");
  3175. return -1;
  3176. }
  3177. t->use = 0;
  3178. t->split = 0;
  3179. return 0;
  3180. }
  3181. static
  3182. void
  3183. metrics_log_nodes_info(struct fuse *f_,
  3184. FILE *file_)
  3185. {
  3186. char buf[1024];
  3187. lfmp_lock(&f_->node_fmp);
  3188. snprintf(buf,sizeof(buf),
  3189. "time: %"PRIu64"\n"
  3190. "sizeof(node): %"PRIu64"\n"
  3191. "node id_table size: %"PRIu64"\n"
  3192. "node id_table usage: %"PRIu64"\n"
  3193. "node id_table total allocated memory: %"PRIu64"\n"
  3194. "node name_table size: %"PRIu64"\n"
  3195. "node name_table usage: %"PRIu64"\n"
  3196. "node name_table total allocated memory: %"PRIu64"\n"
  3197. "node memory pool slab count: %"PRIu64"\n"
  3198. "node memory pool usage ratio: %f\n"
  3199. "node memory pool avail objs: %"PRIu64"\n"
  3200. "node memory pool total allocated memory: %"PRIu64"\n"
  3201. "\n"
  3202. ,
  3203. (uint64_t)time(NULL),
  3204. (uint64_t)sizeof(struct node),
  3205. (uint64_t)f_->id_table.size,
  3206. (uint64_t)f_->id_table.use,
  3207. (uint64_t)(f_->id_table.size * sizeof(struct node*)),
  3208. (uint64_t)f_->name_table.size,
  3209. (uint64_t)f_->name_table.use,
  3210. (uint64_t)(f_->name_table.size * sizeof(struct node*)),
  3211. (uint64_t)fmp_slab_count(&f_->node_fmp.fmp),
  3212. fmp_slab_usage_ratio(&f_->node_fmp.fmp),
  3213. (uint64_t)fmp_avail_objs(&f_->node_fmp.fmp),
  3214. (uint64_t)fmp_total_allocated_memory(&f_->node_fmp.fmp)
  3215. );
  3216. lfmp_unlock(&f_->node_fmp);
  3217. fputs(buf,file_);
  3218. }
  3219. static
  3220. void
  3221. metrics_log_nodes_info_to_tmp_dir(struct fuse *f_)
  3222. {
  3223. FILE *file;
  3224. char filepath[256];
  3225. sprintf(filepath,"/tmp/mergerfs.%d.info",getpid());
  3226. file = fopen(filepath,"w");
  3227. if(file == NULL)
  3228. return;
  3229. metrics_log_nodes_info(f_,file);
  3230. fclose(file);
  3231. }
  3232. static
  3233. void
  3234. fuse_malloc_trim(void)
  3235. {
  3236. #ifdef HAVE_MALLOC_TRIM
  3237. malloc_trim(1024 * 1024);
  3238. #endif
  3239. }
  3240. static
  3241. void*
  3242. fuse_maintenance_loop(void *fuse_)
  3243. {
  3244. int gc;
  3245. int loops;
  3246. int sleep_time;
  3247. struct fuse *f = (struct fuse*)fuse_;
  3248. gc = 0;
  3249. loops = 0;
  3250. sleep_time = 60;
  3251. while(1)
  3252. {
  3253. if(remember_nodes(f))
  3254. fuse_prune_remembered_nodes(f);
  3255. if((loops % 15) == 0)
  3256. {
  3257. fuse_malloc_trim();
  3258. gc = 1;
  3259. }
  3260. // Trigger a followup gc if this gc succeeds
  3261. if(!f->conf.nogc && gc)
  3262. gc = lfmp_gc(&f->node_fmp);
  3263. if(g_LOG_METRICS)
  3264. metrics_log_nodes_info_to_tmp_dir(f);
  3265. loops++;
  3266. sleep(sleep_time);
  3267. }
  3268. return NULL;
  3269. }
  3270. int
  3271. fuse_start_maintenance_thread(struct fuse *f_)
  3272. {
  3273. return fuse_start_thread(&f_->maintenance_thread,fuse_maintenance_loop,f_);
  3274. }
  3275. void
  3276. fuse_stop_maintenance_thread(struct fuse *f_)
  3277. {
  3278. pthread_mutex_lock(&f_->lock);
  3279. pthread_cancel(f_->maintenance_thread);
  3280. pthread_mutex_unlock(&f_->lock);
  3281. pthread_join(f_->maintenance_thread,NULL);
  3282. }
  3283. struct fuse*
  3284. fuse_new_common(struct fuse_chan *ch,
  3285. struct fuse_args *args,
  3286. const struct fuse_operations *op,
  3287. size_t op_size)
  3288. {
  3289. struct fuse *f;
  3290. struct node *root;
  3291. struct fuse_fs *fs;
  3292. struct fuse_lowlevel_ops llop = fuse_path_ops;
  3293. if(fuse_create_context_key() == -1)
  3294. goto out;
  3295. f = (struct fuse *)calloc(1,sizeof(struct fuse));
  3296. if(f == NULL)
  3297. {
  3298. fprintf(stderr,"fuse: failed to allocate fuse object\n");
  3299. goto out_delete_context_key;
  3300. }
  3301. fs = fuse_fs_new(op,op_size);
  3302. if(!fs)
  3303. goto out_free;
  3304. f->fs = fs;
  3305. /* Oh f**k,this is ugly! */
  3306. if(!fs->op.lock)
  3307. {
  3308. llop.getlk = NULL;
  3309. llop.setlk = NULL;
  3310. }
  3311. if(fuse_opt_parse(args,&f->conf,fuse_lib_opts,fuse_lib_opt_proc) == -1)
  3312. goto out_free_fs;
  3313. g_LOG_METRICS = f->conf.debug;
  3314. f->se = fuse_lowlevel_new_common(args,&llop,sizeof(llop),f);
  3315. if(f->se == NULL)
  3316. goto out_free_fs;
  3317. fuse_session_add_chan(f->se,ch);
  3318. /* Trace topmost layer by default */
  3319. srand(time(NULL));
  3320. f->nodeid_gen.nodeid = FUSE_ROOT_ID;
  3321. f->nodeid_gen.generation = rand64();
  3322. if(node_table_init(&f->name_table) == -1)
  3323. goto out_free_session;
  3324. if(node_table_init(&f->id_table) == -1)
  3325. goto out_free_name_table;
  3326. fuse_mutex_init(&f->lock);
  3327. lfmp_init(&f->node_fmp,sizeof(struct node),256);
  3328. kv_init(f->remembered_nodes);
  3329. root = alloc_node(f);
  3330. if(root == NULL)
  3331. {
  3332. fprintf(stderr,"fuse: memory allocation failed\n");
  3333. goto out_free_id_table;
  3334. }
  3335. root->name = filename_strdup(f,"/");
  3336. root->parent = NULL;
  3337. root->nodeid = FUSE_ROOT_ID;
  3338. inc_nlookup(root);
  3339. hash_id(f,root);
  3340. return f;
  3341. out_free_id_table:
  3342. free(f->id_table.array);
  3343. out_free_name_table:
  3344. free(f->name_table.array);
  3345. out_free_session:
  3346. fuse_session_destroy(f->se);
  3347. out_free_fs:
  3348. /* Horrible compatibility hack to stop the destructor from being
  3349. called on the filesystem without init being called first */
  3350. fs->op.destroy = NULL;
  3351. free(f->fs);
  3352. out_free:
  3353. free(f);
  3354. out_delete_context_key:
  3355. fuse_delete_context_key();
  3356. out:
  3357. return NULL;
  3358. }
  3359. struct fuse*
  3360. fuse_new(struct fuse_chan *ch,
  3361. struct fuse_args *args,
  3362. const struct fuse_operations *op,
  3363. size_t op_size)
  3364. {
  3365. return fuse_new_common(ch,args,op,op_size);
  3366. }
  3367. void
  3368. fuse_destroy(struct fuse *f)
  3369. {
  3370. size_t i;
  3371. if(f->fs)
  3372. {
  3373. struct fuse_context_i *c = fuse_get_context_internal();
  3374. memset(c,0,sizeof(*c));
  3375. c->ctx.fuse = f;
  3376. for(i = 0; i < f->id_table.size; i++)
  3377. {
  3378. struct node *node;
  3379. for(node = f->id_table.array[i]; node != NULL; node = node->id_next)
  3380. {
  3381. if(node->is_hidden)
  3382. f->fs->op.free_hide(node->hidden_fh);
  3383. }
  3384. }
  3385. }
  3386. for(i = 0; i < f->id_table.size; i++)
  3387. {
  3388. struct node *node;
  3389. struct node *next;
  3390. for(node = f->id_table.array[i]; node != NULL; node = next)
  3391. {
  3392. next = node->id_next;
  3393. free_node(f,node);
  3394. f->id_table.use--;
  3395. }
  3396. }
  3397. free(f->id_table.array);
  3398. free(f->name_table.array);
  3399. pthread_mutex_destroy(&f->lock);
  3400. fuse_session_destroy(f->se);
  3401. lfmp_destroy(&f->node_fmp);
  3402. kv_destroy(f->remembered_nodes);
  3403. free(f);
  3404. fuse_delete_context_key();
  3405. }
  3406. int
  3407. fuse_config_read_thread_count(const struct fuse *f_)
  3408. {
  3409. return f_->conf.read_thread_count;
  3410. }
  3411. int
  3412. fuse_config_process_thread_count(const struct fuse *f_)
  3413. {
  3414. return f_->conf.process_thread_count;
  3415. }
  3416. const
  3417. char*
  3418. fuse_config_pin_threads(const struct fuse *f_)
  3419. {
  3420. return f_->conf.pin_threads;
  3421. }
  3422. void
  3423. fuse_log_metrics_set(int log_)
  3424. {
  3425. g_LOG_METRICS = log_;
  3426. }
  3427. int
  3428. fuse_log_metrics_get(void)
  3429. {
  3430. return g_LOG_METRICS;
  3431. }