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.

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