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 <syslog.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #ifdef HAVE_MALLOC_TRIM
  46. #include <malloc.h>
  47. #endif
  48. #define FUSE_UNKNOWN_INO UINT64_MAX
  49. #define OFFSET_MAX 0x7fffffffffffffffLL
  50. #define NODE_TABLE_MIN_SIZE 8192
  51. #define PARAM(inarg) ((void*)(((char*)(inarg)) + sizeof(*(inarg))))
  52. static int g_LOG_METRICS = 0;
  53. struct fuse_config
  54. {
  55. unsigned int uid;
  56. unsigned int gid;
  57. unsigned int umask;
  58. int remember;
  59. int debug;
  60. int nogc;
  61. int set_mode;
  62. int set_uid;
  63. int set_gid;
  64. int help;
  65. };
  66. struct fuse_fs
  67. {
  68. struct fuse_operations op;
  69. };
  70. struct lock_queue_element
  71. {
  72. struct lock_queue_element *next;
  73. pthread_cond_t cond;
  74. uint64_t nodeid1;
  75. const char *name1;
  76. char **path1;
  77. node_t **wnode1;
  78. uint64_t nodeid2;
  79. const char *name2;
  80. char **path2;
  81. node_t **wnode2;
  82. int err;
  83. bool done : 1;
  84. };
  85. struct node_table
  86. {
  87. node_t **array;
  88. size_t use;
  89. size_t size;
  90. size_t split;
  91. };
  92. #define container_of(ptr,type,member) ({ \
  93. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  94. (type *)( (char *)__mptr - offsetof(type,member) );})
  95. #define list_entry(ptr,type,member) \
  96. container_of(ptr,type,member)
  97. struct list_head
  98. {
  99. struct list_head *next;
  100. struct list_head *prev;
  101. };
  102. typedef struct remembered_node_t remembered_node_t;
  103. struct remembered_node_t
  104. {
  105. node_t *node;
  106. time_t time;
  107. };
  108. typedef struct nodeid_gen_t nodeid_gen_t;
  109. struct nodeid_gen_t
  110. {
  111. uint64_t nodeid;
  112. uint64_t generation;
  113. };
  114. struct fuse
  115. {
  116. struct fuse_session *se;
  117. struct node_table name_table;
  118. struct node_table id_table;
  119. nodeid_gen_t nodeid_gen;
  120. unsigned int hidectr;
  121. pthread_mutex_t lock;
  122. struct fuse_config conf;
  123. struct fuse_fs *fs;
  124. struct lock_queue_element *lockq;
  125. pthread_t maintenance_thread;
  126. kvec_t(remembered_node_t) remembered_nodes;
  127. };
  128. struct lock
  129. {
  130. int type;
  131. off_t start;
  132. off_t end;
  133. pid_t pid;
  134. uint64_t owner;
  135. struct lock *next;
  136. };
  137. #define TREELOCK_WRITE -1
  138. #define TREELOCK_WAIT_OFFSET INT_MIN
  139. struct fuse_dh
  140. {
  141. pthread_mutex_t lock;
  142. uint64_t fh;
  143. fuse_dirents_t d;
  144. };
  145. struct fuse_context_i
  146. {
  147. struct fuse_context ctx;
  148. fuse_req_t req;
  149. };
  150. static pthread_key_t fuse_context_key;
  151. static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
  152. static int fuse_context_ref;
  153. /*
  154. Why was the nodeid:generation logic simplified?
  155. nodeid is uint64_t: max value of 18446744073709551616
  156. If nodes were created at a rate of 1048576 per second it would take
  157. over 500 thousand years to roll over. I'm fine with risking that.
  158. */
  159. static
  160. uint64_t
  161. generate_nodeid(nodeid_gen_t *ng_)
  162. {
  163. ng_->nodeid++;
  164. return ng_->nodeid;
  165. }
  166. static
  167. char*
  168. filename_strdup(struct fuse *f_,
  169. const char *fn_)
  170. {
  171. return strdup(fn_);
  172. }
  173. static
  174. void
  175. filename_free(struct fuse *f_,
  176. char *fn_)
  177. {
  178. free(fn_);
  179. }
  180. static
  181. void*
  182. fuse_hdr_arg(const struct fuse_in_header *hdr_)
  183. {
  184. return (void*)&hdr_[1];
  185. }
  186. static
  187. void
  188. list_add(struct list_head *new,
  189. struct list_head *prev,
  190. struct list_head *next)
  191. {
  192. next->prev = new;
  193. new->next = next;
  194. new->prev = prev;
  195. prev->next = new;
  196. }
  197. static
  198. inline
  199. void
  200. list_add_head(struct list_head *new,
  201. struct list_head *head)
  202. {
  203. list_add(new,head,head->next);
  204. }
  205. static
  206. inline
  207. void
  208. list_add_tail(struct list_head *new,
  209. struct list_head *head)
  210. {
  211. list_add(new,head->prev,head);
  212. }
  213. static
  214. inline
  215. void
  216. list_del(struct list_head *entry)
  217. {
  218. struct list_head *prev = entry->prev;
  219. struct list_head *next = entry->next;
  220. next->prev = prev;
  221. prev->next = next;
  222. }
  223. static
  224. size_t
  225. id_hash(struct fuse *f,
  226. uint64_t ino)
  227. {
  228. uint64_t hash = ((uint32_t)ino * 2654435761U) % f->id_table.size;
  229. uint64_t oldhash = hash % (f->id_table.size / 2);
  230. if(oldhash >= f->id_table.split)
  231. return oldhash;
  232. else
  233. return hash;
  234. }
  235. static
  236. node_t*
  237. get_node_nocheck(struct fuse *f,
  238. uint64_t nodeid)
  239. {
  240. size_t hash = id_hash(f,nodeid);
  241. node_t *node;
  242. for(node = f->id_table.array[hash]; node != NULL; node = node->id_next)
  243. if(node->nodeid == nodeid)
  244. return node;
  245. return NULL;
  246. }
  247. static
  248. node_t*
  249. get_node(struct fuse *f,
  250. const uint64_t nodeid)
  251. {
  252. node_t *node = get_node_nocheck(f,nodeid);
  253. if(!node)
  254. {
  255. fprintf(stderr,"fuse internal error: node %llu not found\n",
  256. (unsigned long long)nodeid);
  257. abort();
  258. }
  259. return node;
  260. }
  261. static
  262. void
  263. remove_remembered_node(struct fuse *f_,
  264. node_t *node_)
  265. {
  266. for(size_t i = 0; i < kv_size(f_->remembered_nodes); i++)
  267. {
  268. if(kv_A(f_->remembered_nodes,i).node != node_)
  269. continue;
  270. kv_delete(f_->remembered_nodes,i);
  271. break;
  272. }
  273. }
  274. static
  275. uint32_t
  276. stat_crc32b(const struct stat *st_)
  277. {
  278. uint32_t crc;
  279. crc = crc32b_start();
  280. crc = crc32b_continue(&st_->st_ino,sizeof(st_->st_ino),crc);
  281. crc = crc32b_continue(&st_->st_size,sizeof(st_->st_size),crc);
  282. crc = crc32b_continue(&st_->st_mtim,sizeof(st_->st_mtim),crc);
  283. crc = crc32b_finish(crc);
  284. return crc;
  285. }
  286. #ifndef CLOCK_MONOTONIC
  287. # define CLOCK_MONOTONIC CLOCK_REALTIME
  288. #endif
  289. static
  290. time_t
  291. current_time()
  292. {
  293. int rv;
  294. struct timespec now;
  295. static clockid_t clockid = CLOCK_MONOTONIC;
  296. rv = clock_gettime(clockid,&now);
  297. if((rv == -1) && (errno == EINVAL))
  298. {
  299. clockid = CLOCK_REALTIME;
  300. rv = clock_gettime(clockid,&now);
  301. }
  302. if(rv == -1)
  303. now.tv_sec = time(NULL);
  304. return now.tv_sec;
  305. }
  306. static
  307. void
  308. free_node(struct fuse *f_,
  309. node_t *node_)
  310. {
  311. filename_free(f_,node_->name);
  312. if(node_->hidden_fh)
  313. f_->fs->op.free_hide(node_->hidden_fh);
  314. node_free(node_);
  315. }
  316. static
  317. void
  318. node_table_reduce(struct node_table *t)
  319. {
  320. size_t newsize = t->size / 2;
  321. void *newarray;
  322. if(newsize < NODE_TABLE_MIN_SIZE)
  323. return;
  324. newarray = realloc(t->array,sizeof(node_t*) * newsize);
  325. if(newarray != NULL)
  326. t->array = newarray;
  327. t->size = newsize;
  328. t->split = t->size / 2;
  329. }
  330. static
  331. void
  332. remerge_id(struct fuse *f)
  333. {
  334. struct node_table *t = &f->id_table;
  335. int iter;
  336. if(t->split == 0)
  337. node_table_reduce(t);
  338. for(iter = 8; t->split > 0 && iter; iter--)
  339. {
  340. node_t **upper;
  341. t->split--;
  342. upper = &t->array[t->split + t->size / 2];
  343. if(*upper)
  344. {
  345. node_t **nodep;
  346. for(nodep = &t->array[t->split]; *nodep;
  347. nodep = &(*nodep)->id_next);
  348. *nodep = *upper;
  349. *upper = NULL;
  350. break;
  351. }
  352. }
  353. }
  354. static
  355. void
  356. unhash_id(struct fuse *f,
  357. node_t *node)
  358. {
  359. node_t **nodep = &f->id_table.array[id_hash(f,node->nodeid)];
  360. for(; *nodep != NULL; nodep = &(*nodep)->id_next)
  361. if(*nodep == node)
  362. {
  363. *nodep = node->id_next;
  364. f->id_table.use--;
  365. if(f->id_table.use < f->id_table.size / 4)
  366. remerge_id(f);
  367. return;
  368. }
  369. }
  370. static
  371. int
  372. node_table_resize(struct node_table *t)
  373. {
  374. size_t newsize = t->size * 2;
  375. void *newarray;
  376. newarray = realloc(t->array,sizeof(node_t*) * newsize);
  377. if(newarray == NULL)
  378. return -1;
  379. t->array = newarray;
  380. memset(t->array + t->size,0,t->size * sizeof(node_t*));
  381. t->size = newsize;
  382. t->split = 0;
  383. return 0;
  384. }
  385. static
  386. void
  387. rehash_id(struct fuse *f)
  388. {
  389. struct node_table *t = &f->id_table;
  390. node_t **nodep;
  391. node_t **next;
  392. size_t hash;
  393. if(t->split == t->size / 2)
  394. return;
  395. hash = t->split;
  396. t->split++;
  397. for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
  398. {
  399. node_t *node = *nodep;
  400. size_t newhash = id_hash(f,node->nodeid);
  401. if(newhash != hash)
  402. {
  403. next = nodep;
  404. *nodep = node->id_next;
  405. node->id_next = t->array[newhash];
  406. t->array[newhash] = node;
  407. }
  408. else
  409. {
  410. next = &node->id_next;
  411. }
  412. }
  413. if(t->split == t->size / 2)
  414. node_table_resize(t);
  415. }
  416. static
  417. void
  418. hash_id(struct fuse *f,
  419. node_t *node)
  420. {
  421. size_t hash;
  422. hash = id_hash(f,node->nodeid);
  423. node->id_next = f->id_table.array[hash];
  424. f->id_table.array[hash] = node;
  425. f->id_table.use++;
  426. if(f->id_table.use >= f->id_table.size / 2)
  427. rehash_id(f);
  428. }
  429. static
  430. size_t
  431. name_hash(struct fuse *f,
  432. uint64_t parent,
  433. const char *name)
  434. {
  435. uint64_t hash = parent;
  436. uint64_t oldhash;
  437. for(; *name; name++)
  438. hash = hash * 31 + (unsigned char)*name;
  439. hash %= f->name_table.size;
  440. oldhash = hash % (f->name_table.size / 2);
  441. if(oldhash >= f->name_table.split)
  442. return oldhash;
  443. else
  444. return hash;
  445. }
  446. static
  447. void
  448. unref_node(struct fuse *f,
  449. node_t *node);
  450. static
  451. void
  452. remerge_name(struct fuse *f)
  453. {
  454. int iter;
  455. struct node_table *t = &f->name_table;
  456. if(t->split == 0)
  457. node_table_reduce(t);
  458. for(iter = 8; t->split > 0 && iter; iter--)
  459. {
  460. node_t **upper;
  461. t->split--;
  462. upper = &t->array[t->split + t->size / 2];
  463. if(*upper)
  464. {
  465. node_t **nodep;
  466. for(nodep = &t->array[t->split]; *nodep; nodep = &(*nodep)->name_next);
  467. *nodep = *upper;
  468. *upper = NULL;
  469. break;
  470. }
  471. }
  472. }
  473. static
  474. void
  475. unhash_name(struct fuse *f,
  476. node_t *node)
  477. {
  478. if(node->name)
  479. {
  480. size_t hash = name_hash(f,node->parent->nodeid,node->name);
  481. node_t **nodep = &f->name_table.array[hash];
  482. for(; *nodep != NULL; nodep = &(*nodep)->name_next)
  483. if(*nodep == node)
  484. {
  485. *nodep = node->name_next;
  486. node->name_next = NULL;
  487. unref_node(f,node->parent);
  488. filename_free(f,node->name);
  489. node->name = NULL;
  490. node->parent = NULL;
  491. f->name_table.use--;
  492. if(f->name_table.use < f->name_table.size / 4)
  493. remerge_name(f);
  494. return;
  495. }
  496. fprintf(stderr,
  497. "fuse internal error: unable to unhash node: %llu\n",
  498. (unsigned long long)node->nodeid);
  499. abort();
  500. }
  501. }
  502. static
  503. void
  504. rehash_name(struct fuse *f)
  505. {
  506. struct node_table *t = &f->name_table;
  507. node_t **nodep;
  508. node_t **next;
  509. size_t hash;
  510. if(t->split == t->size / 2)
  511. return;
  512. hash = t->split;
  513. t->split++;
  514. for(nodep = &t->array[hash]; *nodep != NULL; nodep = next)
  515. {
  516. node_t *node = *nodep;
  517. size_t newhash = name_hash(f,node->parent->nodeid,node->name);
  518. if(newhash != hash)
  519. {
  520. next = nodep;
  521. *nodep = node->name_next;
  522. node->name_next = t->array[newhash];
  523. t->array[newhash] = node;
  524. }
  525. else
  526. {
  527. next = &node->name_next;
  528. }
  529. }
  530. if(t->split == t->size / 2)
  531. node_table_resize(t);
  532. }
  533. static
  534. int
  535. hash_name(struct fuse *f,
  536. node_t *node,
  537. uint64_t parentid,
  538. const char *name)
  539. {
  540. size_t hash = name_hash(f,parentid,name);
  541. node_t *parent = get_node(f,parentid);
  542. node->name = filename_strdup(f,name);
  543. if(node->name == NULL)
  544. return -1;
  545. parent->refctr++;
  546. node->parent = parent;
  547. node->name_next = f->name_table.array[hash];
  548. f->name_table.array[hash] = node;
  549. f->name_table.use++;
  550. if(f->name_table.use >= f->name_table.size / 2)
  551. rehash_name(f);
  552. return 0;
  553. }
  554. static
  555. inline
  556. int
  557. remember_nodes(struct fuse *f_)
  558. {
  559. return (f_->conf.remember > 0);
  560. }
  561. static
  562. void
  563. delete_node(struct fuse *f,
  564. node_t *node)
  565. {
  566. assert(node->treelock == 0);
  567. unhash_name(f,node);
  568. if(remember_nodes(f))
  569. remove_remembered_node(f,node);
  570. unhash_id(f,node);
  571. node_free(node);
  572. }
  573. static
  574. void
  575. unref_node(struct fuse *f,
  576. node_t *node)
  577. {
  578. assert(node->refctr > 0);
  579. node->refctr--;
  580. if(!node->refctr)
  581. delete_node(f,node);
  582. }
  583. static
  584. uint64_t
  585. rand64(void)
  586. {
  587. uint64_t rv;
  588. rv = rand();
  589. rv <<= 32;
  590. rv |= rand();
  591. return rv;
  592. }
  593. static
  594. node_t*
  595. lookup_node(struct fuse *f,
  596. uint64_t parent,
  597. const char *name)
  598. {
  599. size_t hash;
  600. node_t *node;
  601. hash = name_hash(f,parent,name);
  602. for(node = f->name_table.array[hash]; node != NULL; node = node->name_next)
  603. if(node->parent->nodeid == parent && strcmp(node->name,name) == 0)
  604. return node;
  605. return NULL;
  606. }
  607. static
  608. void
  609. inc_nlookup(node_t *node)
  610. {
  611. if(!node->nlookup)
  612. node->refctr++;
  613. node->nlookup++;
  614. }
  615. static
  616. node_t*
  617. find_node(struct fuse *f,
  618. uint64_t parent,
  619. const char *name)
  620. {
  621. node_t *node;
  622. pthread_mutex_lock(&f->lock);
  623. if(!name)
  624. node = get_node(f,parent);
  625. else
  626. node = lookup_node(f,parent,name);
  627. if(node == NULL)
  628. {
  629. node = node_alloc();
  630. if(node == NULL)
  631. goto out_err;
  632. node->nodeid = generate_nodeid(&f->nodeid_gen);
  633. if(f->conf.remember)
  634. inc_nlookup(node);
  635. if(hash_name(f,node,parent,name) == -1)
  636. {
  637. free_node(f,node);
  638. node = NULL;
  639. goto out_err;
  640. }
  641. hash_id(f,node);
  642. }
  643. else if((node->nlookup == 1) && remember_nodes(f))
  644. {
  645. remove_remembered_node(f,node);
  646. }
  647. inc_nlookup(node);
  648. out_err:
  649. pthread_mutex_unlock(&f->lock);
  650. return node;
  651. }
  652. static
  653. char*
  654. add_name(char **buf,
  655. unsigned *bufsize,
  656. char *s,
  657. const char *name)
  658. {
  659. size_t len = strlen(name);
  660. if(s - len <= *buf)
  661. {
  662. unsigned pathlen = *bufsize - (s - *buf);
  663. unsigned newbufsize = *bufsize;
  664. char *newbuf;
  665. while(newbufsize < pathlen + len + 1)
  666. {
  667. if(newbufsize >= 0x80000000)
  668. newbufsize = 0xffffffff;
  669. else
  670. newbufsize *= 2;
  671. }
  672. newbuf = realloc(*buf,newbufsize);
  673. if(newbuf == NULL)
  674. return NULL;
  675. *buf = newbuf;
  676. s = newbuf + newbufsize - pathlen;
  677. memmove(s,newbuf + *bufsize - pathlen,pathlen);
  678. *bufsize = newbufsize;
  679. }
  680. s -= len;
  681. strncpy(s,name,len);
  682. s--;
  683. *s = '/';
  684. return s;
  685. }
  686. static
  687. void
  688. unlock_path(struct fuse *f,
  689. uint64_t nodeid,
  690. node_t *wnode,
  691. node_t *end)
  692. {
  693. node_t *node;
  694. if(wnode)
  695. {
  696. assert(wnode->treelock == TREELOCK_WRITE);
  697. wnode->treelock = 0;
  698. }
  699. for(node = get_node(f,nodeid); node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent)
  700. {
  701. assert(node->treelock != 0);
  702. assert(node->treelock != TREELOCK_WAIT_OFFSET);
  703. assert(node->treelock != TREELOCK_WRITE);
  704. node->treelock--;
  705. if(node->treelock == TREELOCK_WAIT_OFFSET)
  706. node->treelock = 0;
  707. }
  708. }
  709. static
  710. int
  711. try_get_path(struct fuse *f,
  712. uint64_t nodeid,
  713. const char *name,
  714. char **path,
  715. node_t **wnodep,
  716. bool need_lock)
  717. {
  718. unsigned bufsize = 256;
  719. char *buf;
  720. char *s;
  721. node_t *node;
  722. node_t *wnode = NULL;
  723. int err;
  724. *path = NULL;
  725. err = -ENOMEM;
  726. buf = malloc(bufsize);
  727. if(buf == NULL)
  728. goto out_err;
  729. s = buf + bufsize - 1;
  730. *s = '\0';
  731. if(name != NULL)
  732. {
  733. s = add_name(&buf,&bufsize,s,name);
  734. err = -ENOMEM;
  735. if(s == NULL)
  736. goto out_free;
  737. }
  738. if(wnodep)
  739. {
  740. assert(need_lock);
  741. wnode = lookup_node(f,nodeid,name);
  742. if(wnode)
  743. {
  744. if(wnode->treelock != 0)
  745. {
  746. if(wnode->treelock > 0)
  747. wnode->treelock += TREELOCK_WAIT_OFFSET;
  748. err = -EAGAIN;
  749. goto out_free;
  750. }
  751. wnode->treelock = TREELOCK_WRITE;
  752. }
  753. }
  754. for(node = get_node(f,nodeid); node->nodeid != FUSE_ROOT_ID; node = node->parent)
  755. {
  756. err = -ESTALE;
  757. if(node->name == NULL || node->parent == NULL)
  758. goto out_unlock;
  759. err = -ENOMEM;
  760. s = add_name(&buf,&bufsize,s,node->name);
  761. if(s == NULL)
  762. goto out_unlock;
  763. if(need_lock)
  764. {
  765. err = -EAGAIN;
  766. if(node->treelock < 0)
  767. goto out_unlock;
  768. node->treelock++;
  769. }
  770. }
  771. if(s[0])
  772. memmove(buf,s,bufsize - (s - buf));
  773. else
  774. strcpy(buf,"/");
  775. *path = buf;
  776. if(wnodep)
  777. *wnodep = wnode;
  778. return 0;
  779. out_unlock:
  780. if(need_lock)
  781. unlock_path(f,nodeid,wnode,node);
  782. out_free:
  783. free(buf);
  784. out_err:
  785. return err;
  786. }
  787. static
  788. int
  789. try_get_path2(struct fuse *f,
  790. uint64_t nodeid1,
  791. const char *name1,
  792. uint64_t nodeid2,
  793. const char *name2,
  794. char **path1,
  795. char **path2,
  796. node_t **wnode1,
  797. node_t **wnode2)
  798. {
  799. int err;
  800. err = try_get_path(f,nodeid1,name1,path1,wnode1,true);
  801. if(!err)
  802. {
  803. err = try_get_path(f,nodeid2,name2,path2,wnode2,true);
  804. if(err)
  805. {
  806. node_t *wn1 = wnode1 ? *wnode1 : NULL;
  807. unlock_path(f,nodeid1,wn1,NULL);
  808. free(*path1);
  809. }
  810. }
  811. return err;
  812. }
  813. static
  814. void
  815. queue_element_wakeup(struct fuse *f,
  816. struct lock_queue_element *qe)
  817. {
  818. int err;
  819. if(!qe->path1)
  820. {
  821. /* Just waiting for it to be unlocked */
  822. if(get_node(f,qe->nodeid1)->treelock == 0)
  823. pthread_cond_signal(&qe->cond);
  824. return;
  825. }
  826. if(qe->done)
  827. return;
  828. if(!qe->path2)
  829. {
  830. err = try_get_path(f,
  831. qe->nodeid1,
  832. qe->name1,
  833. qe->path1,
  834. qe->wnode1,
  835. true);
  836. }
  837. else
  838. {
  839. err = try_get_path2(f,
  840. qe->nodeid1,
  841. qe->name1,
  842. qe->nodeid2,
  843. qe->name2,
  844. qe->path1,
  845. qe->path2,
  846. qe->wnode1,
  847. qe->wnode2);
  848. }
  849. if(err == -EAGAIN)
  850. return;
  851. qe->err = err;
  852. qe->done = true;
  853. pthread_cond_signal(&qe->cond);
  854. }
  855. static
  856. void
  857. wake_up_queued(struct fuse *f)
  858. {
  859. struct lock_queue_element *qe;
  860. for(qe = f->lockq; qe != NULL; qe = qe->next)
  861. queue_element_wakeup(f,qe);
  862. }
  863. static
  864. void
  865. queue_path(struct fuse *f,
  866. struct lock_queue_element *qe)
  867. {
  868. struct lock_queue_element **qp;
  869. qe->done = false;
  870. pthread_cond_init(&qe->cond,NULL);
  871. qe->next = NULL;
  872. for(qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
  873. *qp = qe;
  874. }
  875. static
  876. void
  877. dequeue_path(struct fuse *f,
  878. struct lock_queue_element *qe)
  879. {
  880. struct lock_queue_element **qp;
  881. pthread_cond_destroy(&qe->cond);
  882. for(qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
  883. *qp = qe->next;
  884. }
  885. static
  886. int
  887. wait_path(struct fuse *f,
  888. struct lock_queue_element *qe)
  889. {
  890. queue_path(f,qe);
  891. do
  892. {
  893. pthread_cond_wait(&qe->cond,&f->lock);
  894. } while(!qe->done);
  895. dequeue_path(f,qe);
  896. return qe->err;
  897. }
  898. static
  899. int
  900. get_path_common(struct fuse *f,
  901. uint64_t nodeid,
  902. const char *name,
  903. char **path,
  904. node_t **wnode)
  905. {
  906. int err;
  907. pthread_mutex_lock(&f->lock);
  908. err = try_get_path(f,nodeid,name,path,wnode,true);
  909. if(err == -EAGAIN)
  910. {
  911. struct lock_queue_element qe = {0};
  912. qe.nodeid1 = nodeid;
  913. qe.name1 = name;
  914. qe.path1 = path;
  915. qe.wnode1 = wnode;
  916. err = wait_path(f,&qe);
  917. }
  918. pthread_mutex_unlock(&f->lock);
  919. return err;
  920. }
  921. static
  922. int
  923. get_path(struct fuse *f,
  924. uint64_t nodeid,
  925. char **path)
  926. {
  927. return get_path_common(f,nodeid,NULL,path,NULL);
  928. }
  929. static
  930. int
  931. get_path_name(struct fuse *f,
  932. uint64_t nodeid,
  933. const char *name,
  934. char **path)
  935. {
  936. return get_path_common(f,nodeid,name,path,NULL);
  937. }
  938. static
  939. int
  940. get_path_wrlock(struct fuse *f,
  941. uint64_t nodeid,
  942. const char *name,
  943. char **path,
  944. node_t **wnode)
  945. {
  946. return get_path_common(f,nodeid,name,path,wnode);
  947. }
  948. static
  949. int
  950. get_path2(struct fuse *f,
  951. uint64_t nodeid1,
  952. const char *name1,
  953. uint64_t nodeid2,
  954. const char *name2,
  955. char **path1,
  956. char **path2,
  957. node_t **wnode1,
  958. node_t **wnode2)
  959. {
  960. int err;
  961. pthread_mutex_lock(&f->lock);
  962. err = try_get_path2(f,nodeid1,name1,nodeid2,name2,
  963. path1,path2,wnode1,wnode2);
  964. if(err == -EAGAIN)
  965. {
  966. struct lock_queue_element qe = {0};
  967. qe.nodeid1 = nodeid1;
  968. qe.name1 = name1;
  969. qe.path1 = path1;
  970. qe.wnode1 = wnode1;
  971. qe.nodeid2 = nodeid2;
  972. qe.name2 = name2;
  973. qe.path2 = path2;
  974. qe.wnode2 = wnode2;
  975. err = wait_path(f,&qe);
  976. }
  977. pthread_mutex_unlock(&f->lock);
  978. return err;
  979. }
  980. static
  981. void
  982. free_path_wrlock(struct fuse *f,
  983. uint64_t nodeid,
  984. node_t *wnode,
  985. char *path)
  986. {
  987. pthread_mutex_lock(&f->lock);
  988. unlock_path(f,nodeid,wnode,NULL);
  989. if(f->lockq)
  990. wake_up_queued(f);
  991. pthread_mutex_unlock(&f->lock);
  992. free(path);
  993. }
  994. static
  995. void
  996. free_path(struct fuse *f,
  997. uint64_t nodeid,
  998. char *path)
  999. {
  1000. if(path)
  1001. free_path_wrlock(f,nodeid,NULL,path);
  1002. }
  1003. static
  1004. void
  1005. free_path2(struct fuse *f,
  1006. uint64_t nodeid1,
  1007. uint64_t nodeid2,
  1008. node_t *wnode1,
  1009. node_t *wnode2,
  1010. char *path1,
  1011. char *path2)
  1012. {
  1013. pthread_mutex_lock(&f->lock);
  1014. unlock_path(f,nodeid1,wnode1,NULL);
  1015. unlock_path(f,nodeid2,wnode2,NULL);
  1016. wake_up_queued(f);
  1017. pthread_mutex_unlock(&f->lock);
  1018. free(path1);
  1019. free(path2);
  1020. }
  1021. static
  1022. void
  1023. forget_node(struct fuse *f,
  1024. const uint64_t nodeid,
  1025. const uint64_t nlookup)
  1026. {
  1027. node_t *node;
  1028. if(nodeid == FUSE_ROOT_ID)
  1029. return;
  1030. pthread_mutex_lock(&f->lock);
  1031. node = get_node(f,nodeid);
  1032. /*
  1033. * Node may still be locked due to interrupt idiocy in open,
  1034. * create and opendir
  1035. */
  1036. while(node->nlookup == nlookup && node->treelock)
  1037. {
  1038. struct lock_queue_element qe = {0};
  1039. qe.nodeid1 = nodeid;
  1040. queue_path(f,&qe);
  1041. do
  1042. {
  1043. pthread_cond_wait(&qe.cond,&f->lock);
  1044. }
  1045. while((node->nlookup == nlookup) && node->treelock);
  1046. dequeue_path(f,&qe);
  1047. }
  1048. assert(node->nlookup >= nlookup);
  1049. node->nlookup -= nlookup;
  1050. if(node->nlookup == 0)
  1051. {
  1052. unref_node(f,node);
  1053. }
  1054. else if((node->nlookup == 1) && remember_nodes(f))
  1055. {
  1056. remembered_node_t fn;
  1057. fn.node = node;
  1058. fn.time = current_time();
  1059. kv_push(remembered_node_t,f->remembered_nodes,fn);
  1060. }
  1061. pthread_mutex_unlock(&f->lock);
  1062. }
  1063. static
  1064. void
  1065. unlink_node(struct fuse *f,
  1066. node_t *node)
  1067. {
  1068. if(remember_nodes(f))
  1069. {
  1070. assert(node->nlookup > 1);
  1071. node->nlookup--;
  1072. }
  1073. unhash_name(f,node);
  1074. }
  1075. static
  1076. void
  1077. remove_node(struct fuse *f,
  1078. uint64_t dir,
  1079. const char *name)
  1080. {
  1081. node_t *node;
  1082. pthread_mutex_lock(&f->lock);
  1083. node = lookup_node(f,dir,name);
  1084. if(node != NULL)
  1085. unlink_node(f,node);
  1086. pthread_mutex_unlock(&f->lock);
  1087. }
  1088. static
  1089. int
  1090. rename_node(struct fuse *f,
  1091. uint64_t olddir,
  1092. const char *oldname,
  1093. uint64_t newdir,
  1094. const char *newname)
  1095. {
  1096. node_t *node;
  1097. node_t *newnode;
  1098. int err = 0;
  1099. pthread_mutex_lock(&f->lock);
  1100. node = lookup_node(f,olddir,oldname);
  1101. newnode = lookup_node(f,newdir,newname);
  1102. if(node == NULL)
  1103. goto out;
  1104. if(newnode != NULL)
  1105. unlink_node(f,newnode);
  1106. unhash_name(f,node);
  1107. if(hash_name(f,node,newdir,newname) == -1)
  1108. {
  1109. err = -ENOMEM;
  1110. goto out;
  1111. }
  1112. out:
  1113. pthread_mutex_unlock(&f->lock);
  1114. return err;
  1115. }
  1116. static
  1117. void
  1118. set_stat(struct fuse *f,
  1119. uint64_t nodeid,
  1120. struct stat *stbuf)
  1121. {
  1122. if(f->conf.set_mode)
  1123. stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask);
  1124. if(f->conf.set_uid)
  1125. stbuf->st_uid = f->conf.uid;
  1126. if(f->conf.set_gid)
  1127. stbuf->st_gid = f->conf.gid;
  1128. }
  1129. static
  1130. struct fuse*
  1131. req_fuse(fuse_req_t req)
  1132. {
  1133. return (struct fuse*)fuse_req_userdata(req);
  1134. }
  1135. static
  1136. int
  1137. node_open(const node_t *node_)
  1138. {
  1139. return ((node_ != NULL) && (node_->open_count > 0));
  1140. }
  1141. static
  1142. void
  1143. update_stat(node_t *node_,
  1144. const struct stat *stnew_)
  1145. {
  1146. uint32_t crc32b;
  1147. crc32b = stat_crc32b(stnew_);
  1148. if(node_->is_stat_cache_valid && (crc32b != node_->stat_crc32b))
  1149. node_->is_stat_cache_valid = 0;
  1150. node_->stat_crc32b = crc32b;
  1151. }
  1152. static
  1153. int
  1154. set_path_info(struct fuse *f,
  1155. uint64_t nodeid,
  1156. const char *name,
  1157. struct fuse_entry_param *e)
  1158. {
  1159. node_t *node;
  1160. node = find_node(f,nodeid,name);
  1161. if(node == NULL)
  1162. return -ENOMEM;
  1163. e->ino = node->nodeid;
  1164. e->generation = f->nodeid_gen.generation;
  1165. pthread_mutex_lock(&f->lock);
  1166. update_stat(node,&e->attr);
  1167. pthread_mutex_unlock(&f->lock);
  1168. set_stat(f,e->ino,&e->attr);
  1169. return 0;
  1170. }
  1171. static
  1172. int
  1173. lookup_path(struct fuse *f,
  1174. uint64_t nodeid,
  1175. const char *name,
  1176. const char *path,
  1177. struct fuse_entry_param *e,
  1178. fuse_file_info_t *fi)
  1179. {
  1180. int rv;
  1181. memset(e,0,sizeof(struct fuse_entry_param));
  1182. rv = ((fi == NULL) ?
  1183. f->fs->op.getattr(path,&e->attr,&e->timeout) :
  1184. f->fs->op.fgetattr(fi,&e->attr,&e->timeout));
  1185. if(rv)
  1186. return rv;
  1187. return set_path_info(f,nodeid,name,e);
  1188. }
  1189. static
  1190. struct fuse_context_i*
  1191. fuse_get_context_internal(void)
  1192. {
  1193. struct fuse_context_i *c;
  1194. c = (struct fuse_context_i *)pthread_getspecific(fuse_context_key);
  1195. if(c == NULL)
  1196. {
  1197. c = (struct fuse_context_i*)calloc(1,sizeof(struct fuse_context_i));
  1198. if(c == NULL)
  1199. {
  1200. /* This is hard to deal with properly,so just
  1201. abort. If memory is so low that the
  1202. context cannot be allocated,there's not
  1203. much hope for the filesystem anyway */
  1204. fprintf(stderr,"fuse: failed to allocate thread specific data\n");
  1205. abort();
  1206. }
  1207. pthread_setspecific(fuse_context_key,c);
  1208. }
  1209. return c;
  1210. }
  1211. static
  1212. void
  1213. fuse_freecontext(void *data)
  1214. {
  1215. free(data);
  1216. }
  1217. static
  1218. int
  1219. fuse_create_context_key(void)
  1220. {
  1221. int err = 0;
  1222. pthread_mutex_lock(&fuse_context_lock);
  1223. if(!fuse_context_ref)
  1224. {
  1225. err = pthread_key_create(&fuse_context_key,fuse_freecontext);
  1226. if(err)
  1227. {
  1228. fprintf(stderr,"fuse: failed to create thread specific key: %s\n",
  1229. strerror(err));
  1230. pthread_mutex_unlock(&fuse_context_lock);
  1231. return -1;
  1232. }
  1233. }
  1234. fuse_context_ref++;
  1235. pthread_mutex_unlock(&fuse_context_lock);
  1236. return 0;
  1237. }
  1238. static
  1239. void
  1240. fuse_delete_context_key(void)
  1241. {
  1242. pthread_mutex_lock(&fuse_context_lock);
  1243. fuse_context_ref--;
  1244. if(!fuse_context_ref)
  1245. {
  1246. free(pthread_getspecific(fuse_context_key));
  1247. pthread_key_delete(fuse_context_key);
  1248. }
  1249. pthread_mutex_unlock(&fuse_context_lock);
  1250. }
  1251. static
  1252. struct fuse*
  1253. req_fuse_prepare(fuse_req_t req)
  1254. {
  1255. struct fuse_context_i *c = fuse_get_context_internal();
  1256. const struct fuse_ctx *ctx = fuse_req_ctx(req);
  1257. c->req = req;
  1258. c->ctx.fuse = req_fuse(req);
  1259. c->ctx.uid = ctx->uid;
  1260. c->ctx.gid = ctx->gid;
  1261. c->ctx.pid = ctx->pid;
  1262. c->ctx.umask = ctx->umask;
  1263. return c->ctx.fuse;
  1264. }
  1265. static
  1266. void
  1267. reply_entry(fuse_req_t req,
  1268. const struct fuse_entry_param *e,
  1269. int err)
  1270. {
  1271. if(!err)
  1272. {
  1273. struct fuse *f = req_fuse(req);
  1274. if(fuse_reply_entry(req,e) == -ENOENT)
  1275. {
  1276. /* Skip forget for negative result */
  1277. if(e->ino != 0)
  1278. forget_node(f,e->ino,1);
  1279. }
  1280. }
  1281. else
  1282. {
  1283. fuse_reply_err(req,err);
  1284. }
  1285. }
  1286. static
  1287. void
  1288. fuse_lib_init(void *data,
  1289. struct fuse_conn_info *conn)
  1290. {
  1291. struct fuse *f = (struct fuse *)data;
  1292. struct fuse_context_i *c = fuse_get_context_internal();
  1293. memset(c,0,sizeof(*c));
  1294. c->ctx.fuse = f;
  1295. 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(LOG_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(LOG_INFO,"running thorough garbage collection");
  3330. node_gc();
  3331. msgbuf_gc();
  3332. fuse_malloc_trim();
  3333. }
  3334. void
  3335. fuse_gc1()
  3336. {
  3337. syslog(LOG_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. }