16#include "fuse_config.h"
21#include "fuse_kernel.h"
45#define FUSE_NODE_SLAB 1
51#ifndef RENAME_EXCHANGE
52#define RENAME_EXCHANGE (1 << 1)
55#define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1
57#define FUSE_UNKNOWN_INO 0xffffffff
58#define OFFSET_MAX 0x7fffffffffffffffLL
60#define NODE_TABLE_MIN_SIZE 8192
63 struct fuse_operations op;
73struct lock_queue_element {
74 struct lock_queue_element *next;
95#define list_entry(ptr, type, member) \
96 container_of(ptr, type, member)
99 struct list_head *next;
100 struct list_head *prev;
104 struct list_head list;
105 struct list_head freelist;
110 struct fuse_session *se;
111 struct node_table name_table;
112 struct node_table id_table;
113 struct list_head lru_table;
115 unsigned int generation;
116 unsigned int hidectr;
117 pthread_mutex_t lock;
118 struct fuse_config conf;
121 struct lock_queue_element *lockq;
123 struct list_head partial_slabs;
124 struct list_head full_slabs;
125 pthread_t prune_thread;
138 struct node *name_next;
139 struct node *id_next;
141 unsigned int generation;
147 struct timespec stat_updated;
148 struct timespec mtime;
151 unsigned int is_hidden : 1;
152 unsigned int cache_valid : 1;
154 char inline_name[32];
157#define TREELOCK_WRITE -1
158#define TREELOCK_WAIT_OFFSET INT_MIN
162 struct list_head lru;
163 struct timespec forget_time;
166struct fuse_direntry {
170 struct fuse_direntry *next;
174 pthread_mutex_t lock;
178 struct fuse_direntry *first;
179 struct fuse_direntry **last;
189struct fuse_context_i {
190 struct fuse_context ctx;
200static pthread_key_t fuse_context_key;
201static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
202static int fuse_context_ref;
205static int fuse_register_module(
const char *name,
207 struct fusemod_so *so)
213 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module\n");
216 mod->name = strdup(name);
218 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module name\n");
222 mod->factory = factory;
227 mod->next = fuse_modules;
233static void fuse_unregister_module(
struct fuse_module *m)
236 for (mp = &fuse_modules; *mp; mp = &(*mp)->next) {
246static int fuse_load_so_module(
const char *module)
250 struct fusemod_so *so;
253 tmp = malloc(strlen(module) + 64);
255 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
258 sprintf(tmp,
"libfusemod_%s.so", module);
259 so = calloc(1,
sizeof(
struct fusemod_so));
261 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module so\n");
265 so->handle = dlopen(tmp, RTLD_NOW);
266 if (so->handle == NULL) {
267 fuse_log(FUSE_LOG_ERR,
"fuse: dlopen(%s) failed: %s\n",
272 sprintf(tmp,
"fuse_module_%s_factory", module);
274 if (factory == NULL) {
275 fuse_log(FUSE_LOG_ERR,
"fuse: symbol <%s> not found in module: %s\n",
279 ret = fuse_register_module(module, *factory, so);
294static struct fuse_module *fuse_find_module(
const char *module)
297 for (m = fuse_modules; m; m = m->next) {
298 if (strcmp(module, m->name) == 0) {
306static struct fuse_module *fuse_get_module(
const char *module)
310 pthread_mutex_lock(&fuse_context_lock);
311 m = fuse_find_module(module);
313 int err = fuse_load_so_module(module);
315 m = fuse_find_module(module);
317 pthread_mutex_unlock(&fuse_context_lock);
323 pthread_mutex_lock(&fuse_context_lock);
329 if (!m->ctr && m->so) {
330 struct fusemod_so *so = m->so;
335 for (mp = &fuse_modules; *mp;) {
337 fuse_unregister_module(*mp);
344 }
else if (!m->ctr) {
345 fuse_unregister_module(m);
347 pthread_mutex_unlock(&fuse_context_lock);
350static void init_list_head(
struct list_head *list)
356static int list_empty(
const struct list_head *head)
358 return head->next == head;
361static void list_add(
struct list_head *
new,
struct list_head *prev,
362 struct list_head *next)
370static inline void list_add_head(
struct list_head *
new,
struct list_head *head)
372 list_add(
new, head, head->next);
375static inline void list_add_tail(
struct list_head *
new,
struct list_head *head)
377 list_add(
new, head->prev, head);
380static inline void list_del(
struct list_head *entry)
382 struct list_head *prev = entry->prev;
383 struct list_head *next = entry->next;
389static inline int lru_enabled(
struct fuse *f)
394static struct node_lru *node_lru(
struct node *node)
396 return (
struct node_lru *) node;
399static size_t get_node_size(
struct fuse *f)
402 return sizeof(
struct node_lru);
404 return sizeof(
struct node);
408static struct node_slab *list_to_slab(
struct list_head *head)
410 return (
struct node_slab *) head;
413static struct node_slab *node_to_slab(
struct fuse *f,
struct node *node)
415 return (
struct node_slab *) (((uintptr_t) node) & ~((uintptr_t) f->pagesize - 1));
418static int alloc_slab(
struct fuse *f)
421 struct node_slab *slab;
425 size_t node_size = get_node_size(f);
427 mem = mmap(NULL, f->pagesize, PROT_READ | PROT_WRITE,
428 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
430 if (mem == MAP_FAILED)
434 init_list_head(&slab->freelist);
436 num = (f->pagesize -
sizeof(
struct node_slab)) / node_size;
438 start = (
char *) mem + f->pagesize - num * node_size;
439 for (i = 0; i < num; i++) {
442 n = (
struct list_head *) (start + i * node_size);
443 list_add_tail(n, &slab->freelist);
445 list_add_tail(&slab->list, &f->partial_slabs);
450static struct node *alloc_node(
struct fuse *f)
452 struct node_slab *slab;
453 struct list_head *node;
455 if (list_empty(&f->partial_slabs)) {
456 int res = alloc_slab(f);
460 slab = list_to_slab(f->partial_slabs.next);
462 node = slab->freelist.next;
464 if (list_empty(&slab->freelist)) {
465 list_del(&slab->list);
466 list_add_tail(&slab->list, &f->full_slabs);
468 memset(node, 0,
sizeof(
struct node));
470 return (
struct node *) node;
473static void free_slab(
struct fuse *f,
struct node_slab *slab)
477 list_del(&slab->list);
478 res = munmap(slab, f->pagesize);
480 fuse_log(FUSE_LOG_WARNING,
"fuse warning: munmap(%p) failed\n",
484static void free_node_mem(
struct fuse *f,
struct node *node)
486 struct node_slab *slab = node_to_slab(f, node);
487 struct list_head *n = (
struct list_head *) node;
491 if (list_empty(&slab->freelist)) {
492 list_del(&slab->list);
493 list_add_tail(&slab->list, &f->partial_slabs);
495 list_add_head(n, &slab->freelist);
501static struct node *alloc_node(
struct fuse *f)
503 return (
struct node *) calloc(1, get_node_size(f));
506static void free_node_mem(
struct fuse *f,
struct node *node)
513static size_t id_hash(
struct fuse *f,
fuse_ino_t ino)
515 uint64_t hash = ((uint32_t) ino * 2654435761U) % f->id_table.size;
516 uint64_t oldhash = hash % (f->id_table.size / 2);
518 if (oldhash >= f->id_table.split)
524static struct node *get_node_nocheck(
struct fuse *f,
fuse_ino_t nodeid)
526 size_t hash = id_hash(f, nodeid);
529 for (node = f->id_table.array[hash]; node != NULL; node = node->id_next)
530 if (node->nodeid == nodeid)
536static struct node *get_node(
struct fuse *f,
fuse_ino_t nodeid)
538 struct node *node = get_node_nocheck(f, nodeid);
540 fuse_log(FUSE_LOG_ERR,
"fuse internal error: node %llu not found\n",
541 (
unsigned long long) nodeid);
547static void curr_time(
struct timespec *now);
548static double diff_timespec(
const struct timespec *t1,
549 const struct timespec *t2);
551static void remove_node_lru(
struct node *node)
553 struct node_lru *lnode = node_lru(node);
554 list_del(&lnode->lru);
555 init_list_head(&lnode->lru);
558static void set_forget_time(
struct fuse *f,
struct node *node)
560 struct node_lru *lnode = node_lru(node);
562 list_del(&lnode->lru);
563 list_add_tail(&lnode->lru, &f->lru_table);
564 curr_time(&lnode->forget_time);
567static void free_node(
struct fuse *f,
struct node *node)
569 if (node->name != node->inline_name)
571 free_node_mem(f, node);
574static void node_table_reduce(
struct node_table *t)
576 size_t newsize = t->size / 2;
579 if (newsize < NODE_TABLE_MIN_SIZE)
582 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
583 if (newarray != NULL)
587 t->split = t->size / 2;
590static void remerge_id(
struct fuse *f)
592 struct node_table *t = &f->id_table;
596 node_table_reduce(t);
598 for (iter = 8; t->split > 0 && iter; iter--) {
602 upper = &t->array[t->split + t->size / 2];
606 for (nodep = &t->array[t->split]; *nodep;
607 nodep = &(*nodep)->id_next);
616static void unhash_id(
struct fuse *f,
struct node *node)
618 struct node **nodep = &f->id_table.array[id_hash(f, node->nodeid)];
620 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
621 if (*nodep == node) {
622 *nodep = node->id_next;
625 if(f->id_table.use < f->id_table.size / 4)
631static int node_table_resize(
struct node_table *t)
633 size_t newsize = t->size * 2;
636 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
637 if (newarray == NULL)
641 memset(t->array + t->size, 0, t->size *
sizeof(
struct node *));
648static void rehash_id(
struct fuse *f)
650 struct node_table *t = &f->id_table;
655 if (t->split == t->size / 2)
660 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
661 struct node *node = *nodep;
662 size_t newhash = id_hash(f, node->nodeid);
664 if (newhash != hash) {
666 *nodep = node->id_next;
667 node->id_next = t->array[newhash];
668 t->array[newhash] = node;
670 next = &node->id_next;
673 if (t->split == t->size / 2)
674 node_table_resize(t);
677static void hash_id(
struct fuse *f,
struct node *node)
679 size_t hash = id_hash(f, node->nodeid);
680 node->id_next = f->id_table.array[hash];
681 f->id_table.array[hash] = node;
684 if (f->id_table.use >= f->id_table.size / 2)
688static size_t name_hash(
struct fuse *f,
fuse_ino_t parent,
691 uint64_t hash = parent;
694 for (; *name; name++)
695 hash = hash * 31 + (
unsigned char) *name;
697 hash %= f->name_table.size;
698 oldhash = hash % (f->name_table.size / 2);
699 if (oldhash >= f->name_table.split)
705static void unref_node(
struct fuse *f,
struct node *node);
707static void remerge_name(
struct fuse *f)
709 struct node_table *t = &f->name_table;
713 node_table_reduce(t);
715 for (iter = 8; t->split > 0 && iter; iter--) {
719 upper = &t->array[t->split + t->size / 2];
723 for (nodep = &t->array[t->split]; *nodep;
724 nodep = &(*nodep)->name_next);
733static void unhash_name(
struct fuse *f,
struct node *node)
736 size_t hash = name_hash(f, node->parent->nodeid, node->name);
737 struct node **nodep = &f->name_table.array[hash];
739 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
740 if (*nodep == node) {
741 *nodep = node->name_next;
742 node->name_next = NULL;
743 unref_node(f, node->parent);
744 if (node->name != node->inline_name)
750 if (f->name_table.use < f->name_table.size / 4)
755 "fuse internal error: unable to unhash node: %llu\n",
756 (
unsigned long long) node->nodeid);
761static void rehash_name(
struct fuse *f)
763 struct node_table *t = &f->name_table;
768 if (t->split == t->size / 2)
773 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
774 struct node *node = *nodep;
775 size_t newhash = name_hash(f, node->parent->nodeid, node->name);
777 if (newhash != hash) {
779 *nodep = node->name_next;
780 node->name_next = t->array[newhash];
781 t->array[newhash] = node;
783 next = &node->name_next;
786 if (t->split == t->size / 2)
787 node_table_resize(t);
790static int hash_name(
struct fuse *f,
struct node *node,
fuse_ino_t parentid,
793 size_t hash = name_hash(f, parentid, name);
794 struct node *parent = get_node(f, parentid);
795 if (strlen(name) <
sizeof(node->inline_name)) {
796 strcpy(node->inline_name, name);
797 node->name = node->inline_name;
799 node->name = strdup(name);
800 if (node->name == NULL)
805 node->parent = parent;
806 node->name_next = f->name_table.array[hash];
807 f->name_table.array[hash] = node;
810 if (f->name_table.use >= f->name_table.size / 2)
816static void delete_node(
struct fuse *f,
struct node *node)
819 fuse_log(FUSE_LOG_DEBUG,
"DELETE: %llu\n",
820 (
unsigned long long) node->nodeid);
822 assert(node->treelock == 0);
823 unhash_name(f, node);
825 remove_node_lru(node);
830static void unref_node(
struct fuse *f,
struct node *node)
832 assert(node->refctr > 0);
835 delete_node(f, node);
841 f->ctr = (f->ctr + 1) & 0xffffffff;
844 }
while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO ||
845 get_node_nocheck(f, f->ctr) != NULL);
849static struct node *lookup_node(
struct fuse *f,
fuse_ino_t parent,
852 size_t hash = name_hash(f, parent, name);
855 for (node = f->name_table.array[hash]; node != NULL; node = node->name_next)
856 if (node->parent->nodeid == parent &&
857 strcmp(node->name, name) == 0)
863static void inc_nlookup(
struct node *node)
870static struct node *find_node(
struct fuse *f,
fuse_ino_t parent,
875 pthread_mutex_lock(&f->lock);
877 node = get_node(f, parent);
879 node = lookup_node(f, parent, name);
881 node = alloc_node(f);
885 node->nodeid = next_id(f);
886 node->generation = f->generation;
890 if (hash_name(f, node, parent, name) == -1) {
896 if (lru_enabled(f)) {
897 struct node_lru *lnode = node_lru(node);
898 init_list_head(&lnode->lru);
900 }
else if (lru_enabled(f) && node->nlookup == 1) {
901 remove_node_lru(node);
905 pthread_mutex_unlock(&f->lock);
909static int lookup_path_in_cache(
struct fuse *f,
912 char *tmp = strdup(path);
916 pthread_mutex_lock(&f->lock);
921 char *path_element = strtok_r(tmp,
"/", &save_ptr);
922 while (path_element != NULL) {
923 struct node *node = lookup_node(f, ino, path_element);
929 path_element = strtok_r(NULL,
"/", &save_ptr);
931 pthread_mutex_unlock(&f->lock);
939static char *add_name(
char **buf,
unsigned *bufsize,
char *s,
const char *name)
941 size_t len = strlen(name);
943 if (s - len <= *buf) {
944 unsigned pathlen = *bufsize - (s - *buf);
945 unsigned newbufsize = *bufsize;
948 while (newbufsize < pathlen + len + 1) {
949 if (newbufsize >= 0x80000000)
950 newbufsize = 0xffffffff;
955 newbuf = realloc(*buf, newbufsize);
960 s = newbuf + newbufsize - pathlen;
961 memmove(s, newbuf + *bufsize - pathlen, pathlen);
962 *bufsize = newbufsize;
965 memcpy(s, name, len);
972static void unlock_path(
struct fuse *f,
fuse_ino_t nodeid,
struct node *wnode,
978 assert(wnode->treelock == TREELOCK_WRITE);
982 for (node = get_node(f, nodeid);
983 node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent) {
984 assert(node->treelock != 0);
985 assert(node->treelock != TREELOCK_WAIT_OFFSET);
986 assert(node->treelock != TREELOCK_WRITE);
988 if (node->treelock == TREELOCK_WAIT_OFFSET)
993static int try_get_path(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
994 char **path,
struct node **wnodep,
bool need_lock)
996 unsigned bufsize = 256;
1000 struct node *wnode = NULL;
1006 buf = malloc(bufsize);
1010 s = buf + bufsize - 1;
1014 s = add_name(&buf, &bufsize, s, name);
1022 assert(name != NULL);
1023 wnode = lookup_node(f, nodeid, name);
1025 if (wnode->treelock != 0) {
1026 if (wnode->treelock > 0)
1027 wnode->treelock += TREELOCK_WAIT_OFFSET;
1031 wnode->treelock = TREELOCK_WRITE;
1035 for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID;
1036 node = node->parent) {
1038 if (node->name == NULL || node->parent == NULL)
1042 s = add_name(&buf, &bufsize, s, node->name);
1048 if (node->treelock < 0)
1056 memmove(buf, s, bufsize - (s - buf));
1068 unlock_path(f, nodeid, wnode, node);
1076static int try_get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1078 char **path1,
char **path2,
1079 struct node **wnode1,
struct node **wnode2)
1084 err = try_get_path(f, nodeid1, name1, path1, wnode1,
true);
1086 err = try_get_path(f, nodeid2, name2, path2, wnode2,
true);
1088 struct node *wn1 = wnode1 ? *wnode1 : NULL;
1090 unlock_path(f, nodeid1, wn1, NULL);
1097static void queue_element_wakeup(
struct fuse *f,
struct lock_queue_element *qe)
1103 if (get_node(f, qe->nodeid1)->treelock == 0)
1104 pthread_cond_signal(&qe->cond);
1113 err = try_get_path(f, qe->nodeid1, qe->name1, qe->path1,
1116 err = try_get_path2(f, qe->nodeid1, qe->name1, qe->nodeid2,
1117 qe->name2, qe->path1, qe->path2, qe->wnode1,
1126 pthread_cond_signal(&qe->cond);
1129static void wake_up_queued(
struct fuse *f)
1131 struct lock_queue_element *qe;
1133 for (qe = f->lockq; qe != NULL; qe = qe->next)
1134 queue_element_wakeup(f, qe);
1137static void debug_path(
struct fuse *f,
const char *msg,
fuse_ino_t nodeid,
1138 const char *name,
bool wr)
1140 if (f->conf.debug) {
1141 struct node *wnode = NULL;
1144 wnode = lookup_node(f, nodeid, name);
1147 fuse_log(FUSE_LOG_DEBUG,
"%s %llu (w)\n",
1148 msg, (
unsigned long long) wnode->nodeid);
1150 fuse_log(FUSE_LOG_DEBUG,
"%s %llu\n",
1151 msg, (
unsigned long long) nodeid);
1156static void queue_path(
struct fuse *f,
struct lock_queue_element *qe)
1158 struct lock_queue_element **qp;
1161 pthread_cond_init(&qe->cond, NULL);
1163 for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
1167static void dequeue_path(
struct fuse *f,
struct lock_queue_element *qe)
1169 struct lock_queue_element **qp;
1171 pthread_cond_destroy(&qe->cond);
1172 for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
1176static int wait_path(
struct fuse *f,
struct lock_queue_element *qe)
1181 pthread_cond_wait(&qe->cond, &f->lock);
1182 }
while (!qe->done);
1184 dequeue_path(f, qe);
1189static int get_path_common(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1190 char **path,
struct node **wnode)
1194 pthread_mutex_lock(&f->lock);
1195 err = try_get_path(f, nodeid, name, path, wnode,
true);
1196 if (err == -EAGAIN) {
1197 struct lock_queue_element qe = {
1203 debug_path(f,
"QUEUE PATH", nodeid, name, !!wnode);
1204 err = wait_path(f, &qe);
1205 debug_path(f,
"DEQUEUE PATH", nodeid, name, !!wnode);
1207 pthread_mutex_unlock(&f->lock);
1212static int get_path(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1214 return get_path_common(f, nodeid, NULL, path, NULL);
1217static int get_path_nullok(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1224 err = get_path_common(f, nodeid, NULL, path, NULL);
1232static int get_path_name(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1235 return get_path_common(f, nodeid, name, path, NULL);
1238static int get_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1239 char **path,
struct node **wnode)
1241 return get_path_common(f, nodeid, name, path, wnode);
1244#if defined(__FreeBSD__)
1245#define CHECK_DIR_LOOP
1248#if defined(CHECK_DIR_LOOP)
1249static int check_dir_loop(
struct fuse *f,
1253 struct node *node, *node1, *node2;
1256 node1 = lookup_node(f, nodeid1, name1);
1257 id1 = node1 ? node1->nodeid : nodeid1;
1259 node2 = lookup_node(f, nodeid2, name2);
1260 id2 = node2 ? node2->nodeid : nodeid2;
1262 for (node = get_node(f, id2); node->nodeid != FUSE_ROOT_ID;
1263 node = node->parent) {
1264 if (node->name == NULL || node->parent == NULL)
1267 if (node->nodeid != id2 && node->nodeid == id1)
1273 for (node = get_node(f, id1); node->nodeid != FUSE_ROOT_ID;
1274 node = node->parent) {
1275 if (node->name == NULL || node->parent == NULL)
1278 if (node->nodeid != id1 && node->nodeid == id2)
1287static int get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1289 char **path1,
char **path2,
1290 struct node **wnode1,
struct node **wnode2)
1294 pthread_mutex_lock(&f->lock);
1296#if defined(CHECK_DIR_LOOP)
1300 err = check_dir_loop(f, nodeid1, name1, nodeid2, name2);
1306 err = try_get_path2(f, nodeid1, name1, nodeid2, name2,
1307 path1, path2, wnode1, wnode2);
1308 if (err == -EAGAIN) {
1309 struct lock_queue_element qe = {
1320 debug_path(f,
"QUEUE PATH1", nodeid1, name1, !!wnode1);
1321 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1322 err = wait_path(f, &qe);
1323 debug_path(f,
"DEQUEUE PATH1", nodeid1, name1, !!wnode1);
1324 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1327#if defined(CHECK_DIR_LOOP)
1330 pthread_mutex_unlock(&f->lock);
1335static void free_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
1336 struct node *wnode,
char *path)
1338 pthread_mutex_lock(&f->lock);
1339 unlock_path(f, nodeid, wnode, NULL);
1342 pthread_mutex_unlock(&f->lock);
1346static void free_path(
struct fuse *f,
fuse_ino_t nodeid,
char *path)
1349 free_path_wrlock(f, nodeid, NULL, path);
1353 struct node *wnode1,
struct node *wnode2,
1354 char *path1,
char *path2)
1356 pthread_mutex_lock(&f->lock);
1357 unlock_path(f, nodeid1, wnode1, NULL);
1358 unlock_path(f, nodeid2, wnode2, NULL);
1360 pthread_mutex_unlock(&f->lock);
1365static void forget_node(
struct fuse *f,
fuse_ino_t nodeid, uint64_t nlookup)
1368 if (nodeid == FUSE_ROOT_ID)
1370 pthread_mutex_lock(&f->lock);
1371 node = get_node(f, nodeid);
1377 while (node->nlookup == nlookup && node->treelock) {
1378 struct lock_queue_element qe = {
1382 debug_path(f,
"QUEUE PATH (forget)", nodeid, NULL,
false);
1386 pthread_cond_wait(&qe.cond, &f->lock);
1387 }
while (node->nlookup == nlookup && node->treelock);
1389 dequeue_path(f, &qe);
1390 debug_path(f,
"DEQUEUE_PATH (forget)", nodeid, NULL,
false);
1393 assert(node->nlookup >= nlookup);
1394 node->nlookup -= nlookup;
1395 if (!node->nlookup) {
1396 unref_node(f, node);
1397 }
else if (lru_enabled(f) && node->nlookup == 1) {
1398 set_forget_time(f, node);
1400 pthread_mutex_unlock(&f->lock);
1403static void unlink_node(
struct fuse *f,
struct node *node)
1406 assert(node->nlookup > 1);
1409 unhash_name(f, node);
1412static void remove_node(
struct fuse *f,
fuse_ino_t dir,
const char *name)
1416 pthread_mutex_lock(&f->lock);
1417 node = lookup_node(f, dir, name);
1419 unlink_node(f, node);
1420 pthread_mutex_unlock(&f->lock);
1423static int rename_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1424 fuse_ino_t newdir,
const char *newname,
int hide)
1427 struct node *newnode;
1430 pthread_mutex_lock(&f->lock);
1431 node = lookup_node(f, olddir, oldname);
1432 newnode = lookup_node(f, newdir, newname);
1436 if (newnode != NULL) {
1438 fuse_log(FUSE_LOG_ERR,
"fuse: hidden file got created during hiding\n");
1442 unlink_node(f, newnode);
1445 unhash_name(f, node);
1446 if (hash_name(f, node, newdir, newname) == -1) {
1452 node->is_hidden = 1;
1455 pthread_mutex_unlock(&f->lock);
1459static int exchange_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1462 struct node *oldnode;
1463 struct node *newnode;
1466 pthread_mutex_lock(&f->lock);
1467 oldnode = lookup_node(f, olddir, oldname);
1468 newnode = lookup_node(f, newdir, newname);
1471 unhash_name(f, oldnode);
1473 unhash_name(f, newnode);
1477 if (hash_name(f, oldnode, newdir, newname) == -1)
1481 if (hash_name(f, newnode, olddir, oldname) == -1)
1486 pthread_mutex_unlock(&f->lock);
1490static void set_stat(
struct fuse *f,
fuse_ino_t nodeid,
struct stat *stbuf)
1493 stbuf->st_ino = nodeid;
1495 if (f->conf.dmask && S_ISDIR(stbuf->st_mode))
1496 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1497 (0777 & ~f->conf.dmask);
1498 else if (f->conf.
fmask)
1499 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1500 (0777 & ~f->conf.
fmask);
1502 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1503 (0777 & ~f->conf.umask);
1506 stbuf->st_uid = f->conf.uid;
1508 stbuf->st_gid = f->conf.gid;
1512static void set_statx(
struct fuse *f,
fuse_ino_t nodeid,
struct statx *stxbuf)
1515 stxbuf->stx_ino = nodeid;
1517 if (f->conf.dmask && S_ISDIR(stxbuf->stx_mode))
1518 stxbuf->stx_mode = (stxbuf->stx_mode & S_IFMT) |
1519 (0777 & ~f->conf.dmask);
1520 else if (f->conf.
fmask)
1521 stxbuf->stx_mode = (stxbuf->stx_mode & S_IFMT) |
1522 (0777 & ~f->conf.
fmask);
1524 stxbuf->stx_mode = (stxbuf->stx_mode & S_IFMT) |
1525 (0777 & ~f->conf.umask);
1528 stxbuf->stx_uid = f->conf.uid;
1530 stxbuf->stx_gid = f->conf.gid;
1539static void fuse_intr_sighandler(
int sig)
1545struct fuse_intr_data {
1547 pthread_cond_t cond;
1551static void fuse_interrupt(
fuse_req_t req,
void *d_)
1553 struct fuse_intr_data *d = d_;
1554 struct fuse *f = req_fuse(req);
1556 if (d->id == pthread_self())
1559 pthread_mutex_lock(&f->lock);
1560 while (!d->finished) {
1562 struct timespec timeout;
1565 gettimeofday(&now, NULL);
1566 timeout.tv_sec = now.tv_sec + 1;
1567 timeout.tv_nsec = now.tv_usec * 1000;
1568 pthread_cond_timedwait(&d->cond, &f->lock, &timeout);
1570 pthread_mutex_unlock(&f->lock);
1573static void fuse_do_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1574 struct fuse_intr_data *d)
1576 pthread_mutex_lock(&f->lock);
1578 pthread_cond_broadcast(&d->cond);
1579 pthread_mutex_unlock(&f->lock);
1581 pthread_cond_destroy(&d->cond);
1584static void fuse_do_prepare_interrupt(
fuse_req_t req,
struct fuse_intr_data *d)
1586 d->id = pthread_self();
1587 pthread_cond_init(&d->cond, NULL);
1592static inline void fuse_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1593 struct fuse_intr_data *d)
1596 fuse_do_finish_interrupt(f, req, d);
1599static inline void fuse_prepare_interrupt(
struct fuse *f,
fuse_req_t req,
1600 struct fuse_intr_data *d)
1603 fuse_do_prepare_interrupt(req, d);
1607 char* buf,
size_t len)
1611 snprintf(buf, len,
"%llu", (
unsigned long long) fi->
fh);
1615int fuse_fs_getattr(
struct fuse_fs *fs,
const char *path,
struct stat *buf,
1625 fuse_log(FUSE_LOG_DEBUG,
"getattr[%s] %s\n",
1626 file_info_string(fi, buf,
sizeof(buf)),
1629 return fs->op.
getattr(path, buf, fi);
1632int fuse_fs_rename(
struct fuse_fs *fs,
const char *oldpath,
1633 const char *newpath,
unsigned int flags)
1639 fuse_log(FUSE_LOG_DEBUG,
"rename %s %s 0x%x\n", oldpath, newpath,
1642 return fs->op.
rename(oldpath, newpath, flags);
1645int fuse_fs_unlink(
struct fuse_fs *fs,
const char *path)
1651 fuse_log(FUSE_LOG_DEBUG,
"unlink %s\n", path);
1653 return fs->op.
unlink(path);
1656int fuse_fs_rmdir(
struct fuse_fs *fs,
const char *path)
1662 fuse_log(FUSE_LOG_DEBUG,
"rmdir %s\n", path);
1664 return fs->op.
rmdir(path);
1667int fuse_fs_symlink(
struct fuse_fs *fs,
const char *linkname,
const char *path)
1673 fuse_log(FUSE_LOG_DEBUG,
"symlink %s %s\n", linkname, path);
1675 return fs->op.
symlink(linkname, path);
1678int fuse_fs_link(
struct fuse_fs *fs,
const char *oldpath,
const char *newpath)
1684 fuse_log(FUSE_LOG_DEBUG,
"link %s %s\n", oldpath, newpath);
1686 return fs->op.
link(oldpath, newpath);
1689int fuse_fs_release(
struct fuse_fs *fs,
const char *path,
1697 fuse_log(FUSE_LOG_DEBUG,
"release%s[%llu] flags: 0x%x\n",
1698 fi->
flush ?
"+flush" :
"",
1699 (
unsigned long long) fi->
fh, fi->
flags);
1701 return fs->op.
release(path, fi);
1704int fuse_fs_opendir(
struct fuse_fs *fs,
const char *path,
1714 fuse_log(FUSE_LOG_DEBUG,
"opendir flags: 0x%x %s\n", fi->
flags,
1717 err = fs->op.
opendir(path, fi);
1719 if (fs->debug && !err)
1720 fuse_log(FUSE_LOG_DEBUG,
" opendir[%llu] flags: 0x%x %s\n",
1721 (
unsigned long long) fi->
fh, fi->
flags, path);
1726int fuse_fs_open(
struct fuse_fs *fs,
const char *path,
1736 fuse_log(FUSE_LOG_DEBUG,
"open flags: 0x%x %s\n", fi->
flags,
1739 err = fs->op.
open(path, fi);
1741 if (fs->debug && !err)
1742 fuse_log(FUSE_LOG_DEBUG,
" open[%llu] flags: 0x%x %s\n",
1743 (
unsigned long long) fi->
fh, fi->
flags, path);
1753 for (i = 0; i < buf->
count; i++)
1760int fuse_fs_read_buf(
struct fuse_fs *fs,
const char *path,
1761 struct fuse_bufvec **bufp,
size_t size, off_t off,
1772 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1773 (
unsigned long long) fi->
fh,
1774 size, (
unsigned long long) off, fi->
flags);
1777 res = fs->op.
read_buf(path, bufp, size, off, fi);
1791 *
buf = FUSE_BUFVEC_INIT(size);
1795 res = fs->op.
read(path, mem, size,
off, fi);
1800 if (fs->debug && res >= 0)
1801 fuse_log(FUSE_LOG_DEBUG,
" read[%llu] %zu bytes from %llu\n",
1802 (
unsigned long long) fi->
fh,
1804 (
unsigned long long)
off);
1806 fuse_log(FUSE_LOG_ERR,
"fuse: read too many bytes\n");
1814int fuse_fs_read(
struct fuse_fs *fs,
const char *path,
char *mem,
size_t size,
1825 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1826 (
unsigned long long) fi->
fh,
1827 size, (
unsigned long long)
off, fi->
flags);
1841 res = fs->op.
read(path, mem, size,
off, fi);
1844 if (fs->debug && res >= 0)
1845 fuse_log(FUSE_LOG_DEBUG,
" read[%llu] %u bytes from %llu\n",
1846 (
unsigned long long) fi->
fh,
1848 (
unsigned long long)
off);
1849 if (res >= 0 && res > (
int) size)
1850 fuse_log(FUSE_LOG_ERR,
"fuse: read too many bytes\n");
1855int fuse_fs_write_buf(
struct fuse_fs *fs,
const char *path,
1867 assert(
buf->idx == 0 &&
buf->off == 0);
1870 "write%s[%llu] %zu bytes to %llu flags: 0x%x\n",
1872 (
unsigned long long) fi->
fh,
1874 (
unsigned long long)
off,
1884 if (
buf->count == 1 &&
1886 flatbuf = &
buf->buf[0];
1899 flatbuf = &tmp.
buf[0];
1908 if (fs->debug && res >= 0)
1909 fuse_log(FUSE_LOG_DEBUG,
" write%s[%llu] %u bytes to %llu\n",
1911 (
unsigned long long) fi->
fh, res,
1912 (
unsigned long long)
off);
1913 if (res > (
int) size)
1914 fuse_log(FUSE_LOG_ERR,
"fuse: wrote too many bytes\n");
1919int fuse_fs_write(
struct fuse_fs *fs,
const char *path,
const char *mem,
1924 bufv.
buf[0].
mem = (
void *) mem;
1926 return fuse_fs_write_buf(fs, path, &bufv,
off, fi);
1929int fuse_fs_fsync(
struct fuse_fs *fs,
const char *path,
int datasync,
1937 fuse_log(FUSE_LOG_DEBUG,
"fsync[%llu] datasync: %i\n",
1938 (
unsigned long long) fi->
fh, datasync);
1940 return fs->op.
fsync(path, datasync, fi);
1943int fuse_fs_fsyncdir(
struct fuse_fs *fs,
const char *path,
int datasync,
1951 fuse_log(FUSE_LOG_DEBUG,
"fsyncdir[%llu] datasync: %i\n",
1952 (
unsigned long long) fi->
fh, datasync);
1954 return fs->op.
fsyncdir(path, datasync, fi);
1957int fuse_fs_flush(
struct fuse_fs *fs,
const char *path,
1964 fuse_log(FUSE_LOG_DEBUG,
"flush[%llu]\n",
1965 (
unsigned long long) fi->
fh);
1967 return fs->op.
flush(path, fi);
1970int fuse_fs_statfs(
struct fuse_fs *fs,
const char *path,
struct statvfs *
buf)
1975 fuse_log(FUSE_LOG_DEBUG,
"statfs %s\n", path);
1979 buf->f_namemax = 255;
1985int fuse_fs_releasedir(
struct fuse_fs *fs,
const char *path,
1993 fuse_log(FUSE_LOG_DEBUG,
"releasedir[%llu] flags: 0x%x\n",
1994 (
unsigned long long) fi->
fh, fi->
flags);
1999int fuse_fs_readdir(
struct fuse_fs *fs,
const char *path,
void *
buf,
2008 fuse_log(FUSE_LOG_DEBUG,
"readdir%s[%llu] from %llu\n",
2009 (flags & FUSE_READDIR_PLUS) ?
"plus" :
"",
2010 (
unsigned long long) fi->
fh,
2011 (
unsigned long long)
off);
2017int fuse_fs_create(
struct fuse_fs *fs,
const char *path, mode_t mode,
2028 "create flags: 0x%x %s 0%o umask=0%03o\n",
2029 fi->
flags, path, mode,
2032 err = fs->op.
create(path, mode, fi);
2034 if (fs->debug && !err)
2035 fuse_log(FUSE_LOG_DEBUG,
" create[%llu] flags: 0x%x %s\n",
2036 (
unsigned long long) fi->
fh, fi->
flags, path);
2041int fuse_fs_lock(
struct fuse_fs *fs,
const char *path,
2049 fuse_log(FUSE_LOG_DEBUG,
"lock[%llu] %s %s start: %llu len: %llu pid: %llu\n",
2050 (
unsigned long long) fi->
fh,
2051 (cmd == F_GETLK ?
"F_GETLK" :
2052 (cmd == F_SETLK ?
"F_SETLK" :
2053 (cmd == F_SETLKW ?
"F_SETLKW" :
"???"))),
2054 (lock->l_type == F_RDLCK ?
"F_RDLCK" :
2055 (lock->l_type == F_WRLCK ?
"F_WRLCK" :
2056 (lock->l_type == F_UNLCK ?
"F_UNLCK" :
2058 (
unsigned long long) lock->l_start,
2059 (
unsigned long long) lock->l_len,
2060 (
unsigned long long) lock->l_pid);
2062 return fs->op.
lock(path, fi, cmd, lock);
2065int fuse_fs_flock(
struct fuse_fs *fs,
const char *path,
2073 int xop = op & ~LOCK_NB;
2075 fuse_log(FUSE_LOG_DEBUG,
"lock[%llu] %s%s\n",
2076 (
unsigned long long) fi->
fh,
2077 xop == LOCK_SH ?
"LOCK_SH" :
2078 (xop == LOCK_EX ?
"LOCK_EX" :
2079 (xop == LOCK_UN ?
"LOCK_UN" :
"???")),
2080 (op & LOCK_NB) ?
"|LOCK_NB" :
"");
2082 return fs->op.
flock(path, fi, op);
2085int fuse_fs_chown(
struct fuse_fs *fs,
const char *path, uid_t uid,
2094 fuse_log(FUSE_LOG_DEBUG,
"chown[%s] %s %lu %lu\n",
2095 file_info_string(fi,
buf,
sizeof(
buf)),
2096 path, (
unsigned long) uid, (
unsigned long) gid);
2098 return fs->op.
chown(path, uid, gid, fi);
2101int fuse_fs_truncate(
struct fuse_fs *fs,
const char *path, off_t size,
2110 fuse_log(FUSE_LOG_DEBUG,
"truncate[%s] %llu\n",
2111 file_info_string(fi,
buf,
sizeof(
buf)),
2112 (
unsigned long long) size);
2114 return fs->op.
truncate(path, size, fi);
2117int fuse_fs_utimens(
struct fuse_fs *fs,
const char *path,
2126 fuse_log(FUSE_LOG_DEBUG,
"utimens[%s] %s %jd.%09ld %jd.%09ld\n",
2127 file_info_string(fi,
buf,
sizeof(
buf)),
2128 path, (intmax_t)tv[0].tv_sec, tv[0].tv_nsec,
2129 (intmax_t)tv[1].tv_sec, tv[1].tv_nsec);
2131 return fs->op.
utimens(path, tv, fi);
2134int fuse_fs_access(
struct fuse_fs *fs,
const char *path,
int mask)
2140 fuse_log(FUSE_LOG_DEBUG,
"access %s 0%o\n", path, mask);
2142 return fs->op.
access(path, mask);
2145int fuse_fs_readlink(
struct fuse_fs *fs,
const char *path,
char *
buf,
2152 fuse_log(FUSE_LOG_DEBUG,
"readlink %s %lu\n", path,
2153 (
unsigned long) len);
2158int fuse_fs_mknod(
struct fuse_fs *fs,
const char *path, mode_t mode,
2165 fuse_log(FUSE_LOG_DEBUG,
"mknod %s 0%o 0x%llx umask=0%03o\n",
2166 path, mode, (
unsigned long long) rdev,
2169 return fs->op.
mknod(path, mode, rdev);
2172int fuse_fs_mkdir(
struct fuse_fs *fs,
const char *path, mode_t mode)
2178 fuse_log(FUSE_LOG_DEBUG,
"mkdir %s 0%o umask=0%03o\n",
2181 return fs->op.
mkdir(path, mode);
2184int fuse_fs_setxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2185 const char *value,
size_t size,
int flags)
2191 fuse_log(FUSE_LOG_DEBUG,
"setxattr %s %s %lu 0x%x\n",
2192 path, name, (
unsigned long) size, flags);
2194 return fs->op.
setxattr(path, name, value, size, flags);
2197int fuse_fs_getxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2198 char *value,
size_t size)
2204 fuse_log(FUSE_LOG_DEBUG,
"getxattr %s %s %lu\n",
2205 path, name, (
unsigned long) size);
2207 return fs->op.
getxattr(path, name, value, size);
2210int fuse_fs_listxattr(
struct fuse_fs *fs,
const char *path,
char *list,
2217 fuse_log(FUSE_LOG_DEBUG,
"listxattr %s %lu\n",
2218 path, (
unsigned long) size);
2220 return fs->op.
listxattr(path, list, size);
2223int fuse_fs_bmap(
struct fuse_fs *fs,
const char *path,
size_t blocksize,
2230 fuse_log(FUSE_LOG_DEBUG,
"bmap %s blocksize: %lu index: %llu\n",
2231 path, (
unsigned long) blocksize,
2232 (
unsigned long long) *
idx);
2234 return fs->op.
bmap(path, blocksize,
idx);
2237int fuse_fs_removexattr(
struct fuse_fs *fs,
const char *path,
const char *name)
2243 fuse_log(FUSE_LOG_DEBUG,
"removexattr %s %s\n", path, name);
2248int fuse_fs_ioctl(
struct fuse_fs *fs,
const char *path,
unsigned int cmd,
2256 fuse_log(FUSE_LOG_DEBUG,
"ioctl[%llu] 0x%x flags: 0x%x\n",
2257 (
unsigned long long) fi->
fh, cmd, flags);
2259 return fs->op.
ioctl(path, cmd, arg, fi, flags, data);
2262int fuse_fs_poll(
struct fuse_fs *fs,
const char *path,
2276 fuse_log(FUSE_LOG_DEBUG,
"poll[%llu] ph: %p, events 0x%x\n",
2277 (
unsigned long long) fi->
fh, ph,
2280 res = fs->op.
poll(path, fi, ph, reventsp);
2282 if (fs->debug && !res)
2283 fuse_log(FUSE_LOG_DEBUG,
" poll[%llu] revents: 0x%x\n",
2284 (
unsigned long long) fi->
fh, *reventsp);
2289int fuse_fs_fallocate(
struct fuse_fs *fs,
const char *path,
int mode,
2296 fuse_log(FUSE_LOG_DEBUG,
"fallocate %s mode %x, offset: %llu, length: %llu\n",
2299 (
unsigned long long) offset,
2300 (
unsigned long long) length);
2302 return fs->op.
fallocate(path, mode, offset, length, fi);
2305ssize_t fuse_fs_copy_file_range(
struct fuse_fs *fs,
const char *path_in,
2307 const char *path_out,
2309 size_t len,
int flags)
2315 fuse_log(FUSE_LOG_DEBUG,
"copy_file_range from %s:%llu to "
2316 "%s:%llu, length: %llu\n",
2318 (
unsigned long long) off_in,
2320 (
unsigned long long) off_out,
2321 (
unsigned long long) len);
2324 fi_out, off_out, len, flags);
2327off_t fuse_fs_lseek(
struct fuse_fs *fs,
const char *path, off_t
off,
int whence,
2336 fuse_log(FUSE_LOG_DEBUG,
"lseek[%s] %llu %d\n",
2337 file_info_string(fi,
buf,
sizeof(
buf)),
2338 (
unsigned long long)
off, whence);
2340 return fs->op.
lseek(path,
off, whence, fi);
2344int fuse_fs_statx(
struct fuse_fs *fs,
const char *path,
int flags,
int mask,
2352 fuse_log(FUSE_LOG_DEBUG,
"statx[%s] %s %d %d\n",
2353 file_info_string(fi,
buf,
sizeof(
buf)), path,
2356 return fs->op.
statx(path, flags, mask, stxbuf, fi);
2362int fuse_fs_statx(
struct fuse_fs *fs,
const char *path,
int flags,
int mask,
2376static int is_open(
struct fuse *f,
fuse_ino_t dir,
const char *name)
2380 pthread_mutex_lock(&f->lock);
2381 node = lookup_node(f, dir, name);
2382 if (node && node->open_count > 0)
2384 pthread_mutex_unlock(&f->lock);
2388static char *hidden_name(
struct fuse *f,
fuse_ino_t dir,
const char *oldname,
2389 char *newname,
size_t bufsize)
2393 struct node *newnode;
2399 pthread_mutex_lock(&f->lock);
2400 node = lookup_node(f, dir, oldname);
2402 pthread_mutex_unlock(&f->lock);
2407 snprintf(newname, bufsize,
".fuse_hidden%08x%08x",
2408 (
unsigned int) node->nodeid, f->hidectr);
2409 newnode = lookup_node(f, dir, newname);
2412 res = try_get_path(f, dir, newname, &newpath, NULL,
false);
2413 pthread_mutex_unlock(&f->lock);
2417 memset(&buf, 0,
sizeof(buf));
2418 res = fuse_fs_getattr(f->fs, newpath, &buf, NULL);
2423 }
while(res == 0 && --failctr);
2428static int hide_node(
struct fuse *f,
const char *oldpath,
2435 newpath = hidden_name(f, dir, oldname, newname,
sizeof(newname));
2437 err = fuse_fs_rename(f->fs, oldpath, newpath, 0);
2439 err = rename_node(f, dir, oldname, dir, newname, 1);
2445static int mtime_eq(
const struct stat *stbuf,
const struct timespec *ts)
2447 return stbuf->st_mtime == ts->tv_sec &&
2448 ST_MTIM_NSEC(stbuf) == ts->tv_nsec;
2451#ifndef CLOCK_MONOTONIC
2452#define CLOCK_MONOTONIC CLOCK_REALTIME
2455static void curr_time(
struct timespec *now)
2457 static clockid_t clockid = CLOCK_MONOTONIC;
2458 int res = clock_gettime(clockid, now);
2459 if (res == -1 && errno == EINVAL) {
2460 clockid = CLOCK_REALTIME;
2461 res = clock_gettime(clockid, now);
2464 perror(
"fuse: clock_gettime");
2469static void update_stat(
struct node *node,
const struct stat *stbuf)
2471 if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) ||
2472 stbuf->st_size != node->size))
2473 node->cache_valid = 0;
2474 node->mtime.tv_sec = stbuf->st_mtime;
2475 node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf);
2476 node->size = stbuf->st_size;
2477 curr_time(&node->stat_updated);
2480static int do_lookup(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
2485 node = find_node(f, nodeid, name);
2489 e->
ino = node->nodeid;
2494 pthread_mutex_lock(&f->lock);
2495 update_stat(node, &e->
attr);
2496 pthread_mutex_unlock(&f->lock);
2498 set_stat(f, e->
ino, &e->
attr);
2502static int lookup_path(
struct fuse *f,
fuse_ino_t nodeid,
2503 const char *name,
const char *path,
2509 res = fuse_fs_getattr(f->fs, path, &e->
attr, fi);
2511 res = do_lookup(f, nodeid, name, e);
2512 if (res == 0 && f->conf.debug) {
2513 fuse_log(FUSE_LOG_DEBUG,
" NODEID: %llu\n",
2514 (
unsigned long long) e->
ino);
2520static struct fuse_context_i *fuse_get_context_internal(
void)
2522 return (
struct fuse_context_i *) pthread_getspecific(fuse_context_key);
2525static struct fuse_context_i *fuse_create_context(
struct fuse *f)
2527 struct fuse_context_i *c = fuse_get_context_internal();
2529 c = (
struct fuse_context_i *)
2530 calloc(1,
sizeof(
struct fuse_context_i));
2536 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate thread specific data\n");
2539 if (pthread_setspecific(fuse_context_key, c) != 0) {
2540 fuse_log(FUSE_LOG_ERR,
"fuse: failed to set thread specific data\n");
2545 memset(c, 0,
sizeof(*c));
2552static void fuse_freecontext(
void *data)
2557static int fuse_create_context_key(
void)
2560 pthread_mutex_lock(&fuse_context_lock);
2561 if (!fuse_context_ref) {
2562 err = pthread_key_create(&fuse_context_key, fuse_freecontext);
2564 fuse_log(FUSE_LOG_ERR,
"fuse: failed to create thread specific key: %s\n",
2566 pthread_mutex_unlock(&fuse_context_lock);
2571 pthread_mutex_unlock(&fuse_context_lock);
2575static void fuse_delete_context_key(
void)
2577 pthread_mutex_lock(&fuse_context_lock);
2579 if (!fuse_context_ref) {
2580 free(pthread_getspecific(fuse_context_key));
2581 pthread_key_delete(fuse_context_key);
2583 pthread_mutex_unlock(&fuse_context_lock);
2586static struct fuse *req_fuse_prepare(
fuse_req_t req)
2588 struct fuse_context_i *c = fuse_create_context(req_fuse(req));
2598static inline void reply_err(
fuse_req_t req,
int err)
2608 struct fuse *f = req_fuse(req);
2612 forget_node(f, e->
ino, 1);
2615 reply_err(req, err);
2618void fuse_fs_init(
struct fuse_fs *fs,
struct fuse_conn_info *conn,
2629 fs->user_data = fs->op.
init(conn, cfg);
2632static int fuse_init_intr_signal(
int signum,
int *installed);
2634static void fuse_lib_init(
void *data,
struct fuse_conn_info *conn)
2636 struct fuse *f = (
struct fuse *) data;
2638 fuse_create_context(f);
2640 fuse_fs_init(f->fs, conn, &f->conf);
2644 &f->intr_installed) == -1)
2645 fuse_log(FUSE_LOG_ERR,
"fuse: failed to init interrupt signal\n");
2652void fuse_fs_destroy(
struct fuse_fs *fs)
2656 fs->op.
destroy(fs->user_data);
2659static void fuse_lib_destroy(
void *data)
2661 struct fuse *f = (
struct fuse *) data;
2663 fuse_create_context(f);
2664 fuse_fs_destroy(f->fs);
2670 struct fuse *f = req_fuse_prepare(req);
2674 struct node *dot = NULL;
2676 if (name[0] ==
'.') {
2677 int len = strlen(name);
2679 if (len == 1 || (name[1] ==
'.' && len == 2)) {
2680 pthread_mutex_lock(&f->lock);
2683 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP-DOT\n");
2684 dot = get_node_nocheck(f, parent);
2686 pthread_mutex_unlock(&f->lock);
2687 reply_entry(req, &e, -ESTALE);
2693 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP-DOTDOT\n");
2694 parent = get_node(f, parent)->parent->nodeid;
2696 pthread_mutex_unlock(&f->lock);
2701 err = get_path_name(f, parent, name, &path);
2703 struct fuse_intr_data d;
2705 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP %s\n", path);
2706 fuse_prepare_interrupt(f, req, &d);
2707 err = lookup_path(f, parent, name, path, &e, NULL);
2713 fuse_finish_interrupt(f, req, &d);
2714 free_path(f, parent, path);
2717 pthread_mutex_lock(&f->lock);
2719 pthread_mutex_unlock(&f->lock);
2721 reply_entry(req, &e, err);
2724static void do_forget(
struct fuse *f,
fuse_ino_t ino, uint64_t nlookup)
2727 fuse_log(FUSE_LOG_DEBUG,
"FORGET %llu/%llu\n", (
unsigned long long)ino,
2728 (
unsigned long long) nlookup);
2729 forget_node(f, ino, nlookup);
2734 do_forget(req_fuse(req), ino, nlookup);
2738static void fuse_lib_forget_multi(
fuse_req_t req,
size_t count,
2739 struct fuse_forget_data *forgets)
2741 struct fuse *f = req_fuse(req);
2744 for (i = 0; i < count; i++)
2745 do_forget(f, forgets[i].ino, forgets[i].nlookup);
2754 struct fuse *f = req_fuse_prepare(req);
2759 memset(&buf, 0,
sizeof(buf));
2762 err = get_path_nullok(f, ino, &path);
2764 err = get_path(f, ino, &path);
2766 struct fuse_intr_data d;
2767 fuse_prepare_interrupt(f, req, &d);
2768 err = fuse_fs_getattr(f->fs, path, &buf, fi);
2769 fuse_finish_interrupt(f, req, &d);
2770 free_path(f, ino, path);
2775 pthread_mutex_lock(&f->lock);
2776 node = get_node(f, ino);
2777 if (node->is_hidden && buf.st_nlink > 0)
2780 update_stat(node, &buf);
2781 pthread_mutex_unlock(&f->lock);
2782 set_stat(f, ino, &buf);
2785 reply_err(req, err);
2788int fuse_fs_chmod(
struct fuse_fs *fs,
const char *path, mode_t mode,
2798 fuse_log(FUSE_LOG_DEBUG,
"chmod[%s] %s %llo\n",
2799 file_info_string(fi, buf,
sizeof(buf)),
2800 path, (
unsigned long long) mode);
2802 return fs->op.
chmod(path, mode, fi);
2808 struct fuse *f = req_fuse_prepare(req);
2813 memset(&buf, 0,
sizeof(buf));
2815 err = get_path_nullok(f, ino, &path);
2817 err = get_path(f, ino, &path);
2819 struct fuse_intr_data d;
2820 fuse_prepare_interrupt(f, req, &d);
2822 if (!err && (valid & FUSE_SET_ATTR_MODE))
2823 err = fuse_fs_chmod(f->fs, path, attr->st_mode, fi);
2824 if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) {
2825 uid_t uid = (valid & FUSE_SET_ATTR_UID) ?
2826 attr->st_uid : (uid_t) -1;
2827 gid_t gid = (valid & FUSE_SET_ATTR_GID) ?
2828 attr->st_gid : (gid_t) -1;
2829 err = fuse_fs_chown(f->fs, path, uid, gid, fi);
2831 if (!err && (valid & FUSE_SET_ATTR_SIZE)) {
2832 err = fuse_fs_truncate(f->fs, path,
2835#ifdef HAVE_UTIMENSAT
2837 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) {
2838 struct timespec tv[2];
2842 tv[0].tv_nsec = UTIME_OMIT;
2843 tv[1].tv_nsec = UTIME_OMIT;
2845 if (valid & FUSE_SET_ATTR_ATIME_NOW)
2846 tv[0].tv_nsec = UTIME_NOW;
2847 else if (valid & FUSE_SET_ATTR_ATIME)
2848 tv[0] = attr->st_atim;
2850 if (valid & FUSE_SET_ATTR_MTIME_NOW)
2851 tv[1].tv_nsec = UTIME_NOW;
2852 else if (valid & FUSE_SET_ATTR_MTIME)
2853 tv[1] = attr->st_mtim;
2855 err = fuse_fs_utimens(f->fs, path, tv, fi);
2859 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) ==
2860 (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
2861 struct timespec tv[2];
2862 tv[0].tv_sec = attr->st_atime;
2863 tv[0].tv_nsec = ST_ATIM_NSEC(attr);
2864 tv[1].tv_sec = attr->st_mtime;
2865 tv[1].tv_nsec = ST_MTIM_NSEC(attr);
2866 err = fuse_fs_utimens(f->fs, path, tv, fi);
2869 err = fuse_fs_getattr(f->fs, path, &buf, fi);
2871 fuse_finish_interrupt(f, req, &d);
2872 free_path(f, ino, path);
2876 pthread_mutex_lock(&f->lock);
2877 update_stat(get_node(f, ino), &buf);
2878 pthread_mutex_unlock(&f->lock);
2880 set_stat(f, ino, &buf);
2883 reply_err(req, err);
2888 struct fuse *f = req_fuse_prepare(req);
2892 err = get_path(f, ino, &path);
2894 struct fuse_intr_data d;
2896 fuse_prepare_interrupt(f, req, &d);
2897 err = fuse_fs_access(f->fs, path, mask);
2898 fuse_finish_interrupt(f, req, &d);
2899 free_path(f, ino, path);
2901 reply_err(req, err);
2906 struct fuse *f = req_fuse_prepare(req);
2907 char linkname[PATH_MAX + 1];
2911 err = get_path(f, ino, &path);
2913 struct fuse_intr_data d;
2914 fuse_prepare_interrupt(f, req, &d);
2915 err = fuse_fs_readlink(f->fs, path, linkname,
sizeof(linkname));
2916 fuse_finish_interrupt(f, req, &d);
2917 free_path(f, ino, path);
2920 linkname[PATH_MAX] =
'\0';
2923 reply_err(req, err);
2927 mode_t mode, dev_t rdev)
2929 struct fuse *f = req_fuse_prepare(req);
2934 err = get_path_name(f, parent, name, &path);
2936 struct fuse_intr_data d;
2938 fuse_prepare_interrupt(f, req, &d);
2940 if (S_ISREG(mode)) {
2943 memset(&fi, 0,
sizeof(fi));
2944 fi.
flags = O_CREAT | O_EXCL | O_WRONLY;
2945 err = fuse_fs_create(f->fs, path, mode, &fi);
2947 err = lookup_path(f, parent, name, path, &e,
2949 fuse_fs_release(f->fs, path, &fi);
2952 if (err == -ENOSYS) {
2953 err = fuse_fs_mknod(f->fs, path, mode, rdev);
2955 err = lookup_path(f, parent, name, path, &e,
2958 fuse_finish_interrupt(f, req, &d);
2959 free_path(f, parent, path);
2961 reply_entry(req, &e, err);
2967 struct fuse *f = req_fuse_prepare(req);
2972 err = get_path_name(f, parent, name, &path);
2974 struct fuse_intr_data d;
2976 fuse_prepare_interrupt(f, req, &d);
2977 err = fuse_fs_mkdir(f->fs, path, mode);
2979 err = lookup_path(f, parent, name, path, &e, NULL);
2980 fuse_finish_interrupt(f, req, &d);
2981 free_path(f, parent, path);
2983 reply_entry(req, &e, err);
2989 struct fuse *f = req_fuse_prepare(req);
2994 err = get_path_wrlock(f, parent, name, &path, &wnode);
2996 struct fuse_intr_data d;
2998 fuse_prepare_interrupt(f, req, &d);
2999 if (!f->conf.
hard_remove && is_open(f, parent, name)) {
3000 err = hide_node(f, path, parent, name);
3003 if (!is_open(f, parent, wnode->name)) {
3007 if (try_get_path(f, wnode->nodeid, NULL, &unlinkpath, NULL,
false) == 0) {
3008 err = fuse_fs_unlink(f->fs, unlinkpath);
3010 remove_node(f, parent, wnode->name);
3016 err = fuse_fs_unlink(f->fs, path);
3018 remove_node(f, parent, name);
3020 fuse_finish_interrupt(f, req, &d);
3021 free_path_wrlock(f, parent, wnode, path);
3023 reply_err(req, err);
3028 struct fuse *f = req_fuse_prepare(req);
3033 err = get_path_wrlock(f, parent, name, &path, &wnode);
3035 struct fuse_intr_data d;
3037 fuse_prepare_interrupt(f, req, &d);
3038 err = fuse_fs_rmdir(f->fs, path);
3039 fuse_finish_interrupt(f, req, &d);
3041 remove_node(f, parent, name);
3042 free_path_wrlock(f, parent, wnode, path);
3044 reply_err(req, err);
3047static void fuse_lib_symlink(
fuse_req_t req,
const char *linkname,
3050 struct fuse *f = req_fuse_prepare(req);
3055 err = get_path_name(f, parent, name, &path);
3057 struct fuse_intr_data d;
3059 fuse_prepare_interrupt(f, req, &d);
3060 err = fuse_fs_symlink(f->fs, linkname, path);
3062 err = lookup_path(f, parent, name, path, &e, NULL);
3063 fuse_finish_interrupt(f, req, &d);
3064 free_path(f, parent, path);
3066 reply_entry(req, &e, err);
3071 const char *newname,
unsigned int flags)
3073 struct fuse *f = req_fuse_prepare(req);
3076 struct node *wnode1;
3077 struct node *wnode2;
3080 err = get_path2(f, olddir, oldname, newdir, newname,
3081 &oldpath, &newpath, &wnode1, &wnode2);
3083 struct fuse_intr_data d;
3085 fuse_prepare_interrupt(f, req, &d);
3086 if (!f->conf.
hard_remove && !(flags & RENAME_EXCHANGE) &&
3087 is_open(f, newdir, newname))
3088 err = hide_node(f, newpath, newdir, newname);
3090 err = fuse_fs_rename(f->fs, oldpath, newpath, flags);
3092 if (flags & RENAME_EXCHANGE) {
3093 err = exchange_node(f, olddir, oldname,
3096 err = rename_node(f, olddir, oldname,
3097 newdir, newname, 0);
3101 fuse_finish_interrupt(f, req, &d);
3102 free_path2(f, olddir, newdir, wnode1, wnode2, oldpath, newpath);
3104 reply_err(req, err);
3108 const char *newname)
3110 struct fuse *f = req_fuse_prepare(req);
3116 err = get_path2(f,
ino, NULL, newparent, newname,
3117 &oldpath, &newpath, NULL, NULL);
3119 struct fuse_intr_data d;
3121 fuse_prepare_interrupt(f, req, &d);
3122 err = fuse_fs_link(f->fs, oldpath, newpath);
3124 err = lookup_path(f, newparent, newname, newpath,
3126 fuse_finish_interrupt(f, req, &d);
3127 free_path2(f, ino, newparent, NULL, NULL, oldpath, newpath);
3129 reply_entry(req, &e, err);
3132static void fuse_do_release(
struct fuse *f,
fuse_ino_t ino,
const char *path,
3136 int unlink_hidden = 0;
3138 fuse_fs_release(f->fs, path, fi);
3140 pthread_mutex_lock(&f->lock);
3141 node = get_node(f, ino);
3142 assert(node->open_count > 0);
3144 if (node->is_hidden && !node->open_count) {
3146 node->is_hidden = 0;
3148 pthread_mutex_unlock(&f->lock);
3152 fuse_fs_unlink(f->fs, path);
3156 if (get_path(f, ino, &unlinkpath) == 0)
3157 fuse_fs_unlink(f->fs, unlinkpath);
3159 free_path(f, ino, unlinkpath);
3165 const char *name, mode_t mode,
3168 struct fuse *f = req_fuse_prepare(req);
3169 struct fuse_intr_data d;
3174 err = get_path_name(f, parent, name, &path);
3176 fuse_prepare_interrupt(f, req, &d);
3177 err = fuse_fs_create(f->fs, path, mode, fi);
3179 err = lookup_path(f, parent, name, path, &e, fi);
3181 fuse_fs_release(f->fs, path, fi);
3182 else if (!S_ISREG(e.
attr.st_mode)) {
3184 fuse_fs_release(f->fs, path, fi);
3185 forget_node(f, e.
ino, 1);
3196 fuse_finish_interrupt(f, req, &d);
3199 pthread_mutex_lock(&f->lock);
3200 get_node(f, e.
ino)->open_count++;
3201 pthread_mutex_unlock(&f->lock);
3205 fuse_do_release(f, e.
ino, path, fi);
3206 forget_node(f, e.
ino, 1);
3209 reply_err(req, err);
3212 free_path(f, parent, path);
3215static double diff_timespec(
const struct timespec *t1,
3216 const struct timespec *t2)
3218 return (t1->tv_sec - t2->tv_sec) +
3219 ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0;
3222static void open_auto_cache(
struct fuse *f,
fuse_ino_t ino,
const char *path,
3227 pthread_mutex_lock(&f->lock);
3228 node = get_node(f, ino);
3229 if (node->cache_valid) {
3230 struct timespec now;
3233 if (diff_timespec(&now, &node->stat_updated) >
3234 f->conf.ac_attr_timeout) {
3237 pthread_mutex_unlock(&f->lock);
3238 err = fuse_fs_getattr(f->fs, path, &stbuf, fi);
3239 pthread_mutex_lock(&f->lock);
3241 update_stat(node, &stbuf);
3243 node->cache_valid = 0;
3246 if (node->cache_valid)
3249 node->cache_valid = 1;
3250 pthread_mutex_unlock(&f->lock);
3256 struct fuse *f = req_fuse_prepare(req);
3257 struct fuse_intr_data d;
3261 err = get_path(f, ino, &path);
3263 fuse_prepare_interrupt(f, req, &d);
3264 err = fuse_fs_open(f->fs, path, fi);
3272 open_auto_cache(f, ino, path, fi);
3275 (fi->
flags & O_ACCMODE) == O_RDONLY)
3282 fuse_finish_interrupt(f, req, &d);
3285 pthread_mutex_lock(&f->lock);
3286 get_node(f, ino)->open_count++;
3287 pthread_mutex_unlock(&f->lock);
3291 fuse_do_release(f, ino, path, fi);
3294 reply_err(req, err);
3296 free_path(f, ino, path);
3302 struct fuse *f = req_fuse_prepare(req);
3307 res = get_path_nullok(f, ino, &path);
3309 struct fuse_intr_data d;
3311 fuse_prepare_interrupt(f, req, &d);
3312 res = fuse_fs_read_buf(f->fs, path, &buf, size, off, fi);
3313 fuse_finish_interrupt(f, req, &d);
3314 free_path(f, ino, path);
3320 reply_err(req, res);
3329 struct fuse *f = req_fuse_prepare(req);
3333 res = get_path_nullok(f, ino, &path);
3335 struct fuse_intr_data d;
3337 fuse_prepare_interrupt(f, req, &d);
3338 res = fuse_fs_write_buf(f->fs, path, buf, off, fi);
3339 fuse_finish_interrupt(f, req, &d);
3340 free_path(f, ino, path);
3346 reply_err(req, res);
3352 struct fuse *f = req_fuse_prepare(req);
3356 err = get_path_nullok(f, ino, &path);
3358 struct fuse_intr_data d;
3360 fuse_prepare_interrupt(f, req, &d);
3361 err = fuse_fs_fsync(f->fs, path, datasync, fi);
3362 fuse_finish_interrupt(f, req, &d);
3363 free_path(f, ino, path);
3365 reply_err(req, err);
3368static struct fuse_dh *get_dirhandle(
const struct fuse_file_info *llfi,
3371 struct fuse_dh *dh = (
struct fuse_dh *) (uintptr_t) llfi->
fh;
3380 struct fuse *f = req_fuse_prepare(req);
3381 struct fuse_intr_data d;
3387 dh = (
struct fuse_dh *) malloc(
sizeof(
struct fuse_dh));
3389 reply_err(req, -ENOMEM);
3392 memset(dh, 0,
sizeof(
struct fuse_dh));
3394 dh->contents = NULL;
3399 pthread_mutex_init(&dh->lock, NULL);
3401 llfi->
fh = (uintptr_t) dh;
3403 memset(&fi, 0,
sizeof(fi));
3406 err = get_path(f, ino, &path);
3408 fuse_prepare_interrupt(f, req, &d);
3409 err = fuse_fs_opendir(f->fs, path, &fi);
3410 fuse_finish_interrupt(f, req, &d);
3419 fuse_fs_releasedir(f->fs, path, &fi);
3420 pthread_mutex_destroy(&dh->lock);
3424 reply_err(req, err);
3425 pthread_mutex_destroy(&dh->lock);
3428 free_path(f, ino, path);
3431static int extend_contents(
struct fuse_dh *dh,
unsigned minsize)
3433 if (minsize > dh->size) {
3435 unsigned newsize = dh->size;
3438 while (newsize < minsize) {
3439 if (newsize >= 0x80000000)
3440 newsize = 0xffffffff;
3445 newptr = (
char *) realloc(dh->contents, newsize);
3447 dh->error = -ENOMEM;
3450 dh->contents = newptr;
3456static int fuse_add_direntry_to_dh(
struct fuse_dh *dh,
const char *name,
3459 struct fuse_direntry *de;
3461 de = malloc(
sizeof(
struct fuse_direntry));
3463 dh->error = -ENOMEM;
3466 de->name = strdup(name);
3468 dh->error = -ENOMEM;
3477 dh->last = &de->next;
3488 pthread_mutex_lock(&f->lock);
3489 node = lookup_node(f, parent, name);
3492 pthread_mutex_unlock(&f->lock);
3497static int fill_dir(
void *dh_,
const char *name,
const struct stat *statp,
3500 struct fuse_dh *dh = (
struct fuse_dh *) dh_;
3503 if ((flags & ~FUSE_FILL_DIR_PLUS) != 0) {
3511 memset(&stbuf, 0,
sizeof(stbuf));
3512 stbuf.st_ino = FUSE_UNKNOWN_INO;
3515 if (!dh->fuse->conf.
use_ino) {
3516 stbuf.st_ino = FUSE_UNKNOWN_INO;
3518 stbuf.st_ino = (ino_t)
3519 lookup_nodeid(dh->fuse, dh->nodeid, name);
3536 if (extend_contents(dh, dh->needlen) == -1)
3541 dh->needlen - dh->len, name,
3543 if (newlen > dh->needlen)
3550 if (fuse_add_direntry_to_dh(dh, name, &stbuf, flags) == -1)
3556static int is_dot_or_dotdot(
const char *name)
3558 return name[0] ==
'.' && (name[1] ==
'\0' ||
3559 (name[1] ==
'.' && name[2] ==
'\0'));
3562static int fill_dir_plus(
void *dh_,
const char *name,
const struct stat *statp,
3565 struct fuse_dh *dh = (
struct fuse_dh *) dh_;
3570 struct fuse *f = dh->fuse;
3573 if ((flags & ~FUSE_FILL_DIR_PLUS) != 0) {
3578 if (statp && (flags & FUSE_FILL_DIR_PLUS)) {
3582 e.
attr.st_ino = FUSE_UNKNOWN_INO;
3584 e.
attr.st_mode = statp->st_mode;
3586 e.
attr.st_ino = statp->st_ino;
3589 e.
attr.st_ino = (ino_t)
3590 lookup_nodeid(f, dh->nodeid, name);
3605 if (extend_contents(dh, dh->needlen) == -1)
3608 if (statp && (flags & FUSE_FILL_DIR_PLUS)) {
3609 if (!is_dot_or_dotdot(name)) {
3610 res = do_lookup(f, dh->nodeid, name, &e);
3620 dh->needlen - dh->len, name,
3622 if (newlen > dh->needlen)
3628 if (fuse_add_direntry_to_dh(dh, name, &e.
attr, flags) == -1)
3635static void free_direntries(
struct fuse_direntry *de)
3638 struct fuse_direntry *next = de->next;
3646 size_t size, off_t off,
struct fuse_dh *dh,
3654 err = get_path_nullok(f, ino, &path);
3656 err = get_path(f, ino, &path);
3658 struct fuse_intr_data d;
3661 if (flags & FUSE_READDIR_PLUS)
3662 filler = fill_dir_plus;
3664 free_direntries(dh->first);
3666 dh->last = &dh->first;
3672 fuse_prepare_interrupt(f, req, &d);
3673 err = fuse_fs_readdir(f->fs, path, dh, filler, off, fi, flags);
3674 fuse_finish_interrupt(f, req, &d);
3680 free_path(f, ino, path);
3685static int readdir_fill_from_list(
fuse_req_t req,
struct fuse_dh *dh,
3689 struct fuse_direntry *de = dh->first;
3694 if (extend_contents(dh, dh->needlen) == -1)
3697 for (pos = 0; pos < off; pos++) {
3704 char *p = dh->contents + dh->len;
3705 unsigned rem = dh->needlen - dh->len;
3710 if (flags & FUSE_READDIR_PLUS) {
3716 if (de->flags & FUSE_FILL_DIR_PLUS &&
3717 !is_dot_or_dotdot(de->name)) {
3718 res = do_lookup(dh->fuse, dh->nodeid,
3730 de->name, &de->stat, pos);
3732 newlen = dh->len + thislen;
3733 if (newlen > dh->needlen)
3745 struct fuse *f = req_fuse_prepare(req);
3747 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3750 pthread_mutex_lock(&dh->lock);
3757 err = readdir_fill(f, req, ino, size, off, dh, &fi, flags);
3759 reply_err(req, err);
3765 err = readdir_fill_from_list(req, dh, off, flags);
3767 reply_err(req, err);
3773 pthread_mutex_unlock(&dh->lock);
3779 fuse_readdir_common(req, ino, size, off, llfi, 0);
3785 fuse_readdir_common(req, ino, size, off, llfi, FUSE_READDIR_PLUS);
3791 struct fuse *f = req_fuse_prepare(req);
3792 struct fuse_intr_data d;
3794 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3797 get_path_nullok(f, ino, &path);
3799 fuse_prepare_interrupt(f, req, &d);
3800 fuse_fs_releasedir(f->fs, path, &fi);
3801 fuse_finish_interrupt(f, req, &d);
3802 free_path(f, ino, path);
3804 pthread_mutex_lock(&dh->lock);
3805 pthread_mutex_unlock(&dh->lock);
3806 pthread_mutex_destroy(&dh->lock);
3807 free_direntries(dh->first);
3816 struct fuse *f = req_fuse_prepare(req);
3821 get_dirhandle(llfi, &fi);
3823 err = get_path_nullok(f, ino, &path);
3825 struct fuse_intr_data d;
3826 fuse_prepare_interrupt(f, req, &d);
3827 err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi);
3828 fuse_finish_interrupt(f, req, &d);
3829 free_path(f, ino, path);
3831 reply_err(req, err);
3836 struct fuse *f = req_fuse_prepare(req);
3841 memset(&buf, 0,
sizeof(buf));
3843 err = get_path(f, ino, &path);
3846 struct fuse_intr_data d;
3847 fuse_prepare_interrupt(f, req, &d);
3848 err = fuse_fs_statfs(f->fs, path ? path :
"/", &buf);
3849 fuse_finish_interrupt(f, req, &d);
3850 free_path(f, ino, path);
3856 reply_err(req, err);
3860 const char *value,
size_t size,
int flags)
3862 struct fuse *f = req_fuse_prepare(req);
3866 err = get_path(f, ino, &path);
3868 struct fuse_intr_data d;
3869 fuse_prepare_interrupt(f, req, &d);
3870 err = fuse_fs_setxattr(f->fs, path, name, value, size, flags);
3871 fuse_finish_interrupt(f, req, &d);
3872 free_path(f, ino, path);
3874 reply_err(req, err);
3878 const char *name,
char *value,
size_t size)
3883 err = get_path(f, ino, &path);
3885 struct fuse_intr_data d;
3886 fuse_prepare_interrupt(f, req, &d);
3887 err = fuse_fs_getxattr(f->fs, path, name, value, size);
3888 fuse_finish_interrupt(f, req, &d);
3889 free_path(f, ino, path);
3897 struct fuse *f = req_fuse_prepare(req);
3901 char *value = (
char *) malloc(size);
3902 if (value == NULL) {
3903 reply_err(req, -ENOMEM);
3906 res = common_getxattr(f, req, ino, name, value, size);
3910 reply_err(req, res);
3913 res = common_getxattr(f, req, ino, name, NULL, 0);
3917 reply_err(req, res);
3922 char *list,
size_t size)
3927 err = get_path(f, ino, &path);
3929 struct fuse_intr_data d;
3930 fuse_prepare_interrupt(f, req, &d);
3931 err = fuse_fs_listxattr(f->fs, path, list, size);
3932 fuse_finish_interrupt(f, req, &d);
3933 free_path(f, ino, path);
3940 struct fuse *f = req_fuse_prepare(req);
3944 char *list = (
char *) malloc(size);
3946 reply_err(req, -ENOMEM);
3949 res = common_listxattr(f, req, ino, list, size);
3953 reply_err(req, res);
3956 res = common_listxattr(f, req, ino, NULL, 0);
3960 reply_err(req, res);
3967 struct fuse *f = req_fuse_prepare(req);
3971 err = get_path(f, ino, &path);
3973 struct fuse_intr_data d;
3974 fuse_prepare_interrupt(f, req, &d);
3975 err = fuse_fs_removexattr(f->fs, path, name);
3976 fuse_finish_interrupt(f, req, &d);
3977 free_path(f, ino, path);
3979 reply_err(req, err);
3982static struct lock *locks_conflict(
struct node *node,
const struct lock *lock)
3986 for (l = node->locks; l; l = l->next)
3987 if (l->owner != lock->owner &&
3988 lock->start <= l->end && l->start <= lock->end &&
3989 (l->type == F_WRLCK || lock->type == F_WRLCK))
3995static void delete_lock(
struct lock **lockp)
3997 struct lock *l = *lockp;
4002static void insert_lock(
struct lock **pos,
struct lock *lock)
4008static int locks_insert(
struct node *node,
struct lock *lock)
4011 struct lock *newl1 = NULL;
4012 struct lock *newl2 = NULL;
4014 if (lock->type != F_UNLCK || lock->start != 0 ||
4015 lock->end != OFFSET_MAX) {
4016 newl1 = malloc(
sizeof(
struct lock));
4017 newl2 = malloc(
sizeof(
struct lock));
4019 if (!newl1 || !newl2) {
4026 for (lp = &node->locks; *lp;) {
4027 struct lock *l = *lp;
4028 if (l->owner != lock->owner)
4031 if (lock->type == l->type) {
4032 if (l->end < lock->start - 1)
4034 if (lock->end < l->start - 1)
4036 if (l->start <= lock->start && lock->end <= l->end)
4038 if (l->start < lock->start)
4039 lock->start = l->start;
4040 if (lock->end < l->end)
4044 if (l->end < lock->start)
4046 if (lock->end < l->start)
4048 if (lock->start <= l->start && l->end <= lock->end)
4050 if (l->end <= lock->end) {
4051 l->end = lock->start - 1;
4054 if (lock->start <= l->start) {
4055 l->start = lock->end + 1;
4059 newl2->start = lock->end + 1;
4060 l->end = lock->start - 1;
4061 insert_lock(&l->next, newl2);
4071 if (lock->type != F_UNLCK) {
4073 insert_lock(lp, newl1);
4082static void flock_to_lock(
struct flock *flock,
struct lock *lock)
4084 memset(lock, 0,
sizeof(
struct lock));
4085 lock->type = flock->l_type;
4086 lock->start = flock->l_start;
4088 flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
4089 lock->pid = flock->l_pid;
4092static void lock_to_flock(
struct lock *lock,
struct flock *flock)
4094 flock->l_type = lock->type;
4095 flock->l_start = lock->start;
4097 (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
4098 flock->l_pid = lock->pid;
4104 struct fuse_intr_data d;
4110 fuse_prepare_interrupt(f, req, &d);
4111 memset(&lock, 0,
sizeof(lock));
4112 lock.l_type = F_UNLCK;
4113 lock.l_whence = SEEK_SET;
4114 err = fuse_fs_flush(f->fs, path, fi);
4115 errlock = fuse_fs_lock(f->fs, path, fi, F_SETLK, &lock);
4116 fuse_finish_interrupt(f, req, &d);
4118 if (errlock != -ENOSYS) {
4119 flock_to_lock(&lock, &l);
4121 pthread_mutex_lock(&f->lock);
4122 locks_insert(get_node(f, ino), &l);
4123 pthread_mutex_unlock(&f->lock);
4136 struct fuse *f = req_fuse_prepare(req);
4137 struct fuse_intr_data d;
4141 get_path_nullok(f, ino, &path);
4143 err = fuse_flush_common(f, req, ino, path, fi);
4148 fuse_prepare_interrupt(f, req, &d);
4149 fuse_do_release(f, ino, path, fi);
4150 fuse_finish_interrupt(f, req, &d);
4151 free_path(f, ino, path);
4153 reply_err(req, err);
4159 struct fuse *f = req_fuse_prepare(req);
4163 get_path_nullok(f, ino, &path);
4164 err = fuse_flush_common(f, req, ino, path, fi);
4165 free_path(f, ino, path);
4167 reply_err(req, err);
4174 struct fuse *f = req_fuse_prepare(req);
4178 err = get_path_nullok(f, ino, &path);
4180 struct fuse_intr_data d;
4181 fuse_prepare_interrupt(f, req, &d);
4182 err = fuse_fs_lock(f->fs, path, fi, cmd, lock);
4183 fuse_finish_interrupt(f, req, &d);
4184 free_path(f, ino, path);
4194 struct lock *conflict;
4195 struct fuse *f = req_fuse(req);
4197 flock_to_lock(lock, &l);
4199 pthread_mutex_lock(&f->lock);
4200 conflict = locks_conflict(get_node(f, ino), &l);
4202 lock_to_flock(conflict, lock);
4203 pthread_mutex_unlock(&f->lock);
4205 err = fuse_lock_common(req, ino, fi, lock, F_GETLK);
4212 reply_err(req, err);
4219 int err = fuse_lock_common(req, ino, fi, lock,
4220 sleep ? F_SETLKW : F_SETLK);
4222 struct fuse *f = req_fuse(req);
4224 flock_to_lock(lock, &l);
4226 pthread_mutex_lock(&f->lock);
4227 locks_insert(get_node(f, ino), &l);
4228 pthread_mutex_unlock(&f->lock);
4230 reply_err(req, err);
4236 struct fuse *f = req_fuse_prepare(req);
4240 err = get_path_nullok(f, ino, &path);
4242 struct fuse_intr_data d;
4243 fuse_prepare_interrupt(f, req, &d);
4244 err = fuse_fs_flock(f->fs, path, fi, op);
4245 fuse_finish_interrupt(f, req, &d);
4246 free_path(f, ino, path);
4248 reply_err(req, err);
4254 struct fuse *f = req_fuse_prepare(req);
4255 struct fuse_intr_data d;
4259 err = get_path(f, ino, &path);
4261 fuse_prepare_interrupt(f, req, &d);
4262 err = fuse_fs_bmap(f->fs, path, blocksize, &idx);
4263 fuse_finish_interrupt(f, req, &d);
4264 free_path(f, ino, path);
4269 reply_err(req, err);
4274 unsigned int flags,
const void *in_buf,
4275 size_t in_bufsz,
size_t out_bufsz)
4277 struct fuse *f = req_fuse_prepare(req);
4278 struct fuse_intr_data d;
4280 char *path, *out_buf = NULL;
4284 if (
flags & FUSE_IOCTL_UNRESTRICTED)
4287 if (
flags & FUSE_IOCTL_DIR)
4288 get_dirhandle(llfi, &fi);
4294 out_buf = malloc(out_bufsz);
4299 assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
4300 if (out_buf && in_bufsz)
4301 memcpy(out_buf, in_buf, in_bufsz);
4303 err = get_path_nullok(f, ino, &path);
4307 fuse_prepare_interrupt(f, req, &d);
4309 err = fuse_fs_ioctl(f->fs, path, cmd, arg, &fi,
flags,
4310 out_buf ? out_buf : (
void *)in_buf);
4312 fuse_finish_interrupt(f, req, &d);
4313 free_path(f, ino, path);
4320 reply_err(req, err);
4328 struct fuse *f = req_fuse_prepare(req);
4329 struct fuse_intr_data d;
4332 unsigned revents = 0;
4334 err = get_path_nullok(f, ino, &path);
4336 fuse_prepare_interrupt(f, req, &d);
4337 err = fuse_fs_poll(f->fs, path, fi, ph, &revents);
4338 fuse_finish_interrupt(f, req, &d);
4339 free_path(f, ino, path);
4346 reply_err(req, err);
4352 struct fuse *f = req_fuse_prepare(req);
4353 struct fuse_intr_data d;
4357 err = get_path_nullok(f, ino, &path);
4359 fuse_prepare_interrupt(f, req, &d);
4360 err = fuse_fs_fallocate(f->fs, path, mode, offset, length, fi);
4361 fuse_finish_interrupt(f, req, &d);
4362 free_path(f, ino, path);
4364 reply_err(req, err);
4373 struct fuse *f = req_fuse_prepare(req);
4374 struct fuse_intr_data d;
4375 char *path_in, *path_out;
4379 err = get_path_nullok(f, nodeid_in, &path_in);
4381 reply_err(req, err);
4385 err = get_path_nullok(f, nodeid_out, &path_out);
4387 free_path(f, nodeid_in, path_in);
4388 reply_err(req, err);
4392 fuse_prepare_interrupt(f, req, &d);
4393 res = fuse_fs_copy_file_range(f->fs, path_in, fi_in, off_in, path_out,
4394 fi_out, off_out, len, flags);
4395 fuse_finish_interrupt(f, req, &d);
4400 reply_err(req, res);
4402 free_path(f, nodeid_in, path_in);
4403 free_path(f, nodeid_out, path_out);
4409 struct fuse *f = req_fuse_prepare(req);
4410 struct fuse_intr_data d;
4415 err = get_path(f, ino, &path);
4417 reply_err(req, err);
4421 fuse_prepare_interrupt(f, req, &d);
4422 res = fuse_fs_lseek(f->fs, path, off, whence, fi);
4423 fuse_finish_interrupt(f, req, &d);
4424 free_path(f, ino, path);
4428 reply_err(req, res);
4435 struct fuse *f = req_fuse_prepare(req);
4436 struct statx stxbuf;
4440 memset(&stxbuf, 0,
sizeof(stxbuf));
4443 err = get_path_nullok(f, ino, &path);
4445 err = get_path(f, ino, &path);
4448 struct fuse_intr_data d;
4451 flags |= AT_EMPTY_PATH;
4452 fuse_prepare_interrupt(f, req, &d);
4453 err = fuse_fs_statx(f->fs, path, flags, mask, &stxbuf, fi);
4454 fuse_finish_interrupt(f, req, &d);
4455 free_path(f, ino, path);
4460 pthread_mutex_lock(&f->lock);
4461 node = get_node(f, ino);
4462 if (node->is_hidden && stxbuf.stx_nlink > 0)
4467 stbuf.st_mtime = stxbuf.stx_mtime.tv_sec;
4468 ST_MTIM_NSEC_SET(&stbuf, stxbuf.stx_mtime.tv_nsec);
4469 stbuf.st_size = stxbuf.stx_size;
4470 update_stat(node, &stbuf);
4472 pthread_mutex_unlock(&f->lock);
4473 set_statx(f, ino, &stxbuf);
4476 reply_err(req, err);
4480static int clean_delay(
struct fuse *f)
4488 int max_sleep = 3600;
4489 int sleep_time = f->conf.
remember / 10;
4491 if (sleep_time > max_sleep)
4493 if (sleep_time < min_sleep)
4500 struct node_lru *lnode;
4501 struct list_head *curr, *next;
4503 struct timespec now;
4505 pthread_mutex_lock(&f->lock);
4509 for (curr = f->lru_table.next; curr != &f->lru_table; curr = next) {
4513 lnode = list_entry(curr,
struct node_lru, lru);
4514 node = &lnode->node;
4516 age = diff_timespec(&now, &lnode->forget_time);
4517 if (age <= f->conf.remember)
4520 assert(node->nlookup == 1);
4523 if (node->refctr > 1)
4527 unhash_name(f, node);
4528 unref_node(f, node);
4530 pthread_mutex_unlock(&f->lock);
4532 return clean_delay(f);
4536 .init = fuse_lib_init,
4537 .destroy = fuse_lib_destroy,
4538 .lookup = fuse_lib_lookup,
4539 .forget = fuse_lib_forget,
4540 .forget_multi = fuse_lib_forget_multi,
4541 .getattr = fuse_lib_getattr,
4542 .setattr = fuse_lib_setattr,
4543 .access = fuse_lib_access,
4544 .readlink = fuse_lib_readlink,
4545 .mknod = fuse_lib_mknod,
4546 .mkdir = fuse_lib_mkdir,
4547 .unlink = fuse_lib_unlink,
4548 .rmdir = fuse_lib_rmdir,
4549 .symlink = fuse_lib_symlink,
4550 .rename = fuse_lib_rename,
4551 .link = fuse_lib_link,
4552 .create = fuse_lib_create,
4553 .open = fuse_lib_open,
4554 .read = fuse_lib_read,
4555 .write_buf = fuse_lib_write_buf,
4556 .flush = fuse_lib_flush,
4557 .release = fuse_lib_release,
4558 .fsync = fuse_lib_fsync,
4559 .opendir = fuse_lib_opendir,
4560 .readdir = fuse_lib_readdir,
4561 .readdirplus = fuse_lib_readdirplus,
4562 .releasedir = fuse_lib_releasedir,
4563 .fsyncdir = fuse_lib_fsyncdir,
4564 .statfs = fuse_lib_statfs,
4565 .setxattr = fuse_lib_setxattr,
4566 .getxattr = fuse_lib_getxattr,
4567 .listxattr = fuse_lib_listxattr,
4568 .removexattr = fuse_lib_removexattr,
4569 .getlk = fuse_lib_getlk,
4570 .setlk = fuse_lib_setlk,
4571 .flock = fuse_lib_flock,
4572 .bmap = fuse_lib_bmap,
4573 .ioctl = fuse_lib_ioctl,
4574 .poll = fuse_lib_poll,
4575 .fallocate = fuse_lib_fallocate,
4576 .copy_file_range = fuse_lib_copy_file_range,
4577 .lseek = fuse_lib_lseek,
4579 .statx = fuse_lib_statx,
4583int fuse_notify_poll(
struct fuse_pollhandle *ph)
4593static int fuse_session_loop_remember(
struct fuse *f)
4595 struct fuse_session *se = f->se;
4597 struct timespec now;
4599 struct pollfd fds = {
4608 next_clean = now.tv_sec;
4613 if (now.tv_sec < next_clean)
4614 timeout = next_clean - now.tv_sec;
4618 res = poll(&fds, 1, timeout * 1000);
4624 }
else if (res > 0) {
4625 res = fuse_session_receive_buf_internal(se, &fbuf,
4632 fuse_session_process_buf_internal(se, &fbuf, NULL);
4636 next_clean = now.tv_sec + timeout;
4640 fuse_buf_free(&fbuf);
4641 return res < 0 ? -1 : 0;
4650 return fuse_session_loop_remember(f);
4655FUSE_SYMVER(
"fuse_loop_mt_312",
"fuse_loop_mt@@FUSE_3.12")
4670int fuse_loop_mt_32(
struct fuse *f,
struct fuse_loop_config_v1 *config_v1);
4671FUSE_SYMVER(
"fuse_loop_mt_32",
"fuse_loop_mt@FUSE_3.2")
4672int fuse_loop_mt_32(struct fuse *f, struct fuse_loop_config_v1 *config_v1)
4678 fuse_loop_cfg_convert(config, config_v1);
4680 int res = fuse_loop_mt_312(f, config);
4682 fuse_loop_cfg_destroy(config);
4687int fuse_loop_mt_31(
struct fuse *f,
int clone_fd);
4688FUSE_SYMVER(
"fuse_loop_mt_31",
"fuse_loop_mt@FUSE_3.0")
4689int fuse_loop_mt_31(struct fuse *f,
int clone_fd)
4697 fuse_loop_cfg_set_clone_fd(config,
clone_fd);
4699 err = fuse_loop_mt_312(f, config);
4701 fuse_loop_cfg_destroy(config);
4713 struct fuse_context_i *c = fuse_get_context_internal();
4723 struct fuse_context_i *c = fuse_get_context_internal();
4732 struct fuse_context_i *c = fuse_get_context_internal();
4742 int err = lookup_path_in_cache(f, path, &ino);
4750#define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v }
4752static const struct fuse_opt fuse_lib_opts[] = {
4755 FUSE_LIB_OPT(
"debug", debug, 1),
4756 FUSE_LIB_OPT(
"-d", debug, 1),
4757 FUSE_LIB_OPT(
"kernel_cache", kernel_cache, 1),
4758 FUSE_LIB_OPT(
"auto_cache", auto_cache, 1),
4759 FUSE_LIB_OPT(
"noauto_cache", auto_cache, 0),
4760 FUSE_LIB_OPT(
"no_rofd_flush", no_rofd_flush, 1),
4761 FUSE_LIB_OPT(
"umask=", set_mode, 1),
4762 FUSE_LIB_OPT(
"umask=%o", umask, 0),
4763 FUSE_LIB_OPT(
"fmask=", set_mode, 1),
4764 FUSE_LIB_OPT(
"fmask=%o", fmask, 0),
4765 FUSE_LIB_OPT(
"dmask=", set_mode, 1),
4766 FUSE_LIB_OPT(
"dmask=%o", dmask, 0),
4767 FUSE_LIB_OPT(
"uid=", set_uid, 1),
4768 FUSE_LIB_OPT(
"uid=%d", uid, 0),
4769 FUSE_LIB_OPT(
"gid=", set_gid, 1),
4770 FUSE_LIB_OPT(
"gid=%d", gid, 0),
4771 FUSE_LIB_OPT(
"entry_timeout=%lf", entry_timeout, 0),
4772 FUSE_LIB_OPT(
"attr_timeout=%lf", attr_timeout, 0),
4773 FUSE_LIB_OPT(
"ac_attr_timeout=%lf", ac_attr_timeout, 0),
4774 FUSE_LIB_OPT(
"ac_attr_timeout=", ac_attr_timeout_set, 1),
4775 FUSE_LIB_OPT(
"negative_timeout=%lf", negative_timeout, 0),
4776 FUSE_LIB_OPT(
"noforget", remember, -1),
4777 FUSE_LIB_OPT(
"remember=%u", remember, 0),
4778 FUSE_LIB_OPT(
"modules=%s", modules, 0),
4779 FUSE_LIB_OPT(
"parallel_direct_write=%d", parallel_direct_writes, 0),
4783static int fuse_lib_opt_proc(
void *data,
const char *arg,
int key,
4786 (void) arg; (void) outargs; (void) data; (void) key;
4793static const struct fuse_opt fuse_help_opts[] = {
4794 FUSE_LIB_OPT(
"modules=%s", modules, 1),
4799static void print_module_help(
const char *name,
4808 printf(
"\nOptions for %s module:\n", name);
4818" -o kernel_cache cache files in kernel\n"
4819" -o [no]auto_cache enable caching based on modification times (off)\n"
4820" -o no_rofd_flush disable flushing of read-only fd on close (off)\n"
4821" -o umask=M set file permissions (octal)\n"
4822" -o fmask=M set file permissions (octal)\n"
4823" -o dmask=M set dir permissions (octal)\n"
4824" -o uid=N set file owner\n"
4825" -o gid=N set file group\n"
4826" -o entry_timeout=T cache timeout for names (1.0s)\n"
4827" -o negative_timeout=T cache timeout for deleted names (0.0s)\n"
4828" -o attr_timeout=T cache timeout for attributes (1.0s)\n"
4829" -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n"
4830" -o noforget never forget cached inodes\n"
4831" -o remember=T remember cached inodes for T seconds (0s)\n"
4832" -o modules=M1[:M2...] names of modules to push onto filesystem stack\n");
4839 print_module_help(
"subdir", &fuse_module_subdir_factory);
4841 print_module_help(
"iconv", &fuse_module_iconv_factory);
4848 fuse_lib_opt_proc) == -1
4857 for (module = conf.modules; module; module = next) {
4859 for (p = module; *p && *p !=
':'; p++);
4860 next = *p ? p + 1 : NULL;
4863 m = fuse_get_module(module);
4865 print_module_help(module, &m->factory);
4869static int fuse_init_intr_signal(
int signum,
int *installed)
4871 struct sigaction old_sa;
4873 if (sigaction(signum, NULL, &old_sa) == -1) {
4874 perror(
"fuse: cannot get old signal handler");
4878 if (old_sa.sa_handler == SIG_DFL) {
4879 struct sigaction sa;
4881 memset(&sa, 0,
sizeof(
struct sigaction));
4882 sa.sa_handler = fuse_intr_sighandler;
4883 sigemptyset(&sa.sa_mask);
4885 if (sigaction(signum, &sa, NULL) == -1) {
4886 perror(
"fuse: cannot set interrupt signal handler");
4894static void fuse_restore_intr_signal(
int signum)
4896 struct sigaction sa;
4898 memset(&sa, 0,
sizeof(
struct sigaction));
4899 sa.sa_handler = SIG_DFL;
4900 sigaction(signum, &sa, NULL);
4904static int fuse_push_module(
struct fuse *f,
const char *module,
4907 struct fuse_fs *fs[2] = { f->fs, NULL };
4908 struct fuse_fs *newfs;
4914 newfs = m->factory(args, fs);
4929 fuse_log(FUSE_LOG_ERR,
"fuse: warning: library too old, some operations may not not work\n");
4933 fs = (
struct fuse_fs *) calloc(1,
sizeof(
struct fuse_fs));
4935 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate fuse_fs object\n");
4939 fs->user_data = user_data;
4941 memcpy(&fs->op, op, op_size);
4945static int node_table_init(
struct node_table *t)
4947 t->size = NODE_TABLE_MIN_SIZE;
4948 t->array = (
struct node **) calloc(1,
sizeof(
struct node *) * t->size);
4949 if (t->array == NULL) {
4950 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
4959static void *fuse_prune_nodes(
void *fuse)
4961 struct fuse *f = fuse;
4964 fuse_set_thread_name(
"fuse_prune_nodes");
4976 return fuse_start_thread(&f->prune_thread, fuse_prune_nodes, f);
4983 if (lru_enabled(f)) {
4984 pthread_mutex_lock(&f->lock);
4985 pthread_cancel(f->prune_thread);
4986 pthread_mutex_unlock(&f->lock);
4987 pthread_join(f->prune_thread, NULL);
4995struct fuse *_fuse_new_31(
struct fuse_args *args,
5004 f = (
struct fuse *) calloc(1,
sizeof(
struct fuse));
5006 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate fuse object\n");
5017 fuse_lib_opt_proc) == -1)
5020 pthread_mutex_lock(&fuse_context_lock);
5021 static int builtin_modules_registered = 0;
5023 if (builtin_modules_registered == 0) {
5025 fuse_register_module(
"subdir", fuse_module_subdir_factory, NULL);
5027 fuse_register_module(
"iconv", fuse_module_iconv_factory, NULL);
5029 builtin_modules_registered= 1;
5031 pthread_mutex_unlock(&fuse_context_lock);
5033 if (fuse_create_context_key() == -1)
5038 goto out_delete_context_key;
5048 f->pagesize = getpagesize();
5049 init_list_head(&f->partial_slabs);
5050 init_list_head(&f->full_slabs);
5051 init_list_head(&f->lru_table);
5053 if (f->conf.modules) {
5057 for (module = f->conf.modules; module; module = next) {
5059 for (p = module; *p && *p !=
':'; p++);
5060 next = *p ? p + 1 : NULL;
5063 fuse_push_module(f, module, args) == -1)
5068 if (!f->conf.ac_attr_timeout_set)
5071#if defined(__FreeBSD__) || defined(__NetBSD__)
5080 struct fuse_session *fuse_session_new_versioned(
5084 f->se = fuse_session_new_versioned(args, &llop,
sizeof(llop), version,
5090 f->fs->debug = f->conf.debug;
5093 if (node_table_init(&f->name_table) == -1)
5094 goto out_free_session;
5096 if (node_table_init(&f->id_table) == -1)
5097 goto out_free_name_table;
5099 pthread_mutex_init(&f->lock, NULL);
5101 root = alloc_node(f);
5103 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
5104 goto out_free_id_table;
5106 if (lru_enabled(f)) {
5107 struct node_lru *lnode = node_lru(root);
5108 init_list_head(&lnode->lru);
5111 strcpy(root->inline_name,
"/");
5112 root->name = root->inline_name;
5113 root->parent = NULL;
5114 root->nodeid = FUSE_ROOT_ID;
5121 free(f->id_table.array);
5123 free(f->name_table.array);
5128 free(f->conf.modules);
5129out_delete_context_key:
5130 fuse_delete_context_key();
5138FUSE_SYMVER(
"_fuse_new_30",
"_fuse_new@FUSE_3.0")
5139struct fuse *_fuse_new_30(struct
fuse_args *args,
5148 FUSE_LIB_OPT(
"-h", show_help, 1),
5149 FUSE_LIB_OPT(
"--help", show_help, 1),
5154 fuse_lib_opt_proc) == -1)
5158 fuse_lib_help(args);
5161 return _fuse_new_31(args, op, op_size, version, user_data);
5166 size_t op_size,
void *user_data);
5167FUSE_SYMVER(
"fuse_new_31",
"fuse_new@FUSE_3.1")
5168struct fuse *fuse_new_31(struct
fuse_args *args,
5170 size_t op_size,
void *user_data)
5175 return _fuse_new_31(args, op, op_size, &version, user_data);
5183 size_t op_size,
void *user_data);
5184FUSE_SYMVER(
"fuse_new_30",
"fuse_new@FUSE_3.0")
5185struct fuse *fuse_new_30(struct
fuse_args *args,
5187 size_t op_size,
void *user_data)
5192 FUSE_LIB_OPT(
"-h", show_help, 1),
5193 FUSE_LIB_OPT(
"--help", show_help, 1),
5198 fuse_lib_opt_proc) == -1)
5202 fuse_lib_help(args);
5205 return fuse_new_31(args, op, op_size, user_data);
5213 if (f->conf.
intr && f->intr_installed)
5217 fuse_create_context(f);
5219 for (i = 0; i < f->id_table.size; i++) {
5222 for (node = f->id_table.array[i]; node != NULL;
5223 node = node->id_next) {
5224 if (node->is_hidden) {
5226 if (try_get_path(f, node->nodeid, NULL, &path, NULL,
false) == 0) {
5227 fuse_fs_unlink(f->fs, path);
5234 for (i = 0; i < f->id_table.size; i++) {
5238 for (node = f->id_table.array[i]; node != NULL; node = next) {
5239 next = node->id_next;
5244 assert(list_empty(&f->partial_slabs));
5245 assert(list_empty(&f->full_slabs));
5247 while (fuse_modules) {
5248 fuse_put_module(fuse_modules);
5250 free(f->id_table.array);
5251 free(f->name_table.array);
5252 pthread_mutex_destroy(&f->lock);
5255 free(f->conf.modules);
5257 fuse_delete_context_key();
5271 return FUSE_VERSION;
5276 return PACKAGE_VERSION;
int fuse_getgroups(int size, gid_t list[])
int fuse_mount(struct fuse *f, const char *mountpoint)
int fuse_interrupted(void)
void fuse_destroy(struct fuse *f)
int fuse_start_cleanup_thread(struct fuse *fuse)
int fuse_invalidate_path(struct fuse *f, const char *path)
struct fuse_fs *(* fuse_module_factory_t)(struct fuse_args *args, struct fuse_fs *fs[])
struct fuse_context * fuse_get_context(void)
int fuse_loop(struct fuse *f)
int(* fuse_fill_dir_t)(void *buf, const char *name, const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
void fuse_exit(struct fuse *f)
int fuse_clean_cache(struct fuse *fuse)
void fuse_lib_help(struct fuse_args *args)
struct fuse_session * fuse_get_session(struct fuse *f)
void fuse_unmount(struct fuse *f)
struct fuse_fs * fuse_fs_new(const struct fuse_operations *op, size_t op_size, void *private_data)
void fuse_stop_cleanup_thread(struct fuse *fuse)
void fuse_unset_feature_flag(struct fuse_conn_info *conn, uint64_t flag)
bool fuse_set_feature_flag(struct fuse_conn_info *conn, uint64_t flag)
#define FUSE_CAP_SPLICE_READ
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
#define FUSE_CAP_EXPORT_SUPPORT
#define FUSE_CAP_POSIX_LOCKS
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
const char * fuse_pkgversion(void)
void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
#define FUSE_CAP_FLOCK_LOCKS
void fuse_log(enum fuse_log_level level, const char *fmt,...) __attribute__((format(printf
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_exit(struct fuse_session *se)
int fuse_reply_poll(fuse_req_t req, unsigned revents)
int fuse_reply_err(fuse_req_t req, int err)
const struct fuse_ctx * fuse_req_ctx(fuse_req_t req)
void * fuse_req_userdata(fuse_req_t req)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
struct fuse_req * fuse_req_t
size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct fuse_entry_param *e, off_t off)
int fuse_session_exited(struct fuse_session *se)
int fuse_req_interrupted(fuse_req_t req)
int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
int fuse_reply_readlink(fuse_req_t req, const char *link)
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
void fuse_session_unmount(struct fuse_session *se)
void fuse_reply_none(fuse_req_t req)
void fuse_lowlevel_help(void)
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
int fuse_reply_write(fuse_req_t req, size_t count)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, void *data)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
int fuse_reply_lseek(fuse_req_t req, off_t off)
int fuse_reply_statx(fuse_req_t req, int flags, struct statx *statx, double attr_timeout)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
int fuse_reply_xattr(fuse_req_t req, size_t count)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
void fuse_opt_free_args(struct fuse_args *args)
#define FUSE_OPT_KEY(templ, key)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_OPT_KEY_KEEP
#define FUSE_ARGS_INIT(argc, argv)
enum fuse_buf_flags flags
int32_t parallel_direct_writes
uint32_t parallel_direct_writes
void(* setlk)(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, struct flock *lock, int sleep)
void(* getlk)(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, struct flock *lock)
int(* mknod)(const char *, mode_t, dev_t)
int(* open)(const char *, struct fuse_file_info *)
int(* readlink)(const char *, char *, size_t)
int(* write)(const char *, const char *, size_t, off_t, struct fuse_file_info *)
int(* fallocate)(const char *, int, off_t, off_t, struct fuse_file_info *)
int(* read)(const char *, char *, size_t, off_t, struct fuse_file_info *)
int(* symlink)(const char *, const char *)
int(* write_buf)(const char *, struct fuse_bufvec *buf, off_t off, struct fuse_file_info *)
int(* release)(const char *, struct fuse_file_info *)
int(* access)(const char *, int)
int(* lock)(const char *, struct fuse_file_info *, int cmd, struct flock *)
int(* fsyncdir)(const char *, int, struct fuse_file_info *)
int(* mkdir)(const char *, mode_t)
int(* chown)(const char *, uid_t, gid_t, struct fuse_file_info *fi)
int(* unlink)(const char *)
int(* flush)(const char *, struct fuse_file_info *)
int(* listxattr)(const char *, char *, size_t)
int(* truncate)(const char *, off_t, struct fuse_file_info *fi)
int(* statfs)(const char *, struct statvfs *)
ssize_t(* copy_file_range)(const char *path_in, struct fuse_file_info *fi_in, off_t offset_in, const char *path_out, struct fuse_file_info *fi_out, off_t offset_out, size_t size, int flags)
int(* create)(const char *, mode_t, struct fuse_file_info *)
int(* utimens)(const char *, const struct timespec tv[2], struct fuse_file_info *fi)
off_t(* lseek)(const char *, off_t off, int whence, struct fuse_file_info *)
int(* getxattr)(const char *, const char *, char *, size_t)
int(* setxattr)(const char *, const char *, const char *, size_t, int)
int(* poll)(const char *, struct fuse_file_info *, struct fuse_pollhandle *ph, unsigned *reventsp)
int(* fsync)(const char *, int, struct fuse_file_info *)
int(* chmod)(const char *, mode_t, struct fuse_file_info *fi)
int(* opendir)(const char *, struct fuse_file_info *)
int(* rmdir)(const char *)
int(* statx)(const char *path, int flags, int mask, struct statx *stxbuf, struct fuse_file_info *fi)
int(* releasedir)(const char *, struct fuse_file_info *)
int(* getattr)(const char *, struct stat *, struct fuse_file_info *fi)
int(* read_buf)(const char *, struct fuse_bufvec **bufp, size_t size, off_t off, struct fuse_file_info *)
int(* link)(const char *, const char *)
int(* flock)(const char *, struct fuse_file_info *, int op)
int(* removexattr)(const char *, const char *)
int(* ioctl)(const char *, unsigned int cmd, void *arg, struct fuse_file_info *, unsigned int flags, void *data)
int(* rename)(const char *, const char *, unsigned int flags)
int(* readdir)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *, enum fuse_readdir_flags)
void *(* init)(struct fuse_conn_info *conn, struct fuse_config *cfg)
void(* destroy)(void *private_data)
int(* bmap)(const char *, size_t blocksize, uint64_t *idx)