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.

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