You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4010 lines
80 KiB

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