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.

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