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