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.

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