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

4048 lines
81 KiB

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