18 #include "fuse_lowlevel.h" 20 #include "fuse_misc.h" 21 #include "fuse_kernel.h" 37 #include <sys/param.h> 43 #define FUSE_NODE_SLAB 1 49 #ifndef RENAME_EXCHANGE 50 #define RENAME_EXCHANGE (1 << 1) 53 #define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 55 #define FUSE_UNKNOWN_INO 0xffffffff 56 #define OFFSET_MAX 0x7fffffffffffffffLL 58 #define NODE_TABLE_MIN_SIZE 8192 72 struct lock_queue_element {
73 struct lock_queue_element *next;
84 bool first_locked : 1;
85 bool second_locked : 1;
96 #define container_of(ptr, type, member) ({ \ 97 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 98 (type *)( (char *)__mptr - offsetof(type,member) );}) 100 #define list_entry(ptr, type, member) \ 101 container_of(ptr, type, member) 104 struct list_head *next;
105 struct list_head *prev;
109 struct list_head list;
110 struct list_head freelist;
115 struct fuse_session *se;
116 struct node_table name_table;
117 struct node_table id_table;
118 struct list_head lru_table;
120 unsigned int generation;
121 unsigned int hidectr;
122 pthread_mutex_t lock;
126 struct lock_queue_element *lockq;
128 struct list_head partial_slabs;
129 struct list_head full_slabs;
130 pthread_t prune_thread;
143 struct node *name_next;
144 struct node *id_next;
146 unsigned int generation;
152 struct timespec stat_updated;
153 struct timespec mtime;
156 unsigned int is_hidden : 1;
157 unsigned int cache_valid : 1;
159 char inline_name[32];
162 #define TREELOCK_WRITE -1 163 #define TREELOCK_WAIT_OFFSET INT_MIN 167 struct list_head lru;
168 struct timespec forget_time;
171 struct fuse_direntry {
174 struct fuse_direntry *next;
178 pthread_mutex_t lock;
182 struct fuse_direntry *first;
183 struct fuse_direntry **last;
193 struct fuse_context_i {
204 static pthread_key_t fuse_context_key;
205 static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
206 static int fuse_context_ref;
209 static int fuse_register_module(
const char *name,
211 struct fusemod_so *so)
217 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module\n");
220 mod->name = strdup(name);
222 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module name\n");
226 mod->factory = factory;
231 mod->next = fuse_modules;
237 static void fuse_unregister_module(
struct fuse_module *m)
240 for (mp = &fuse_modules; *mp; mp = &(*mp)->next) {
250 static int fuse_load_so_module(
const char *module)
254 struct fusemod_so *so;
257 tmp = malloc(strlen(module) + 64);
259 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
262 sprintf(tmp,
"libfusemod_%s.so", module);
263 so = calloc(1,
sizeof(
struct fusemod_so));
265 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate module so\n");
269 so->handle = dlopen(tmp, RTLD_NOW);
270 if (so->handle == NULL) {
271 fuse_log(FUSE_LOG_ERR,
"fuse: dlopen(%s) failed: %s\n",
276 sprintf(tmp,
"fuse_module_%s_factory", module);
277 *(
void**)(&factory) = dlsym(so->handle, tmp);
278 if (factory == NULL) {
279 fuse_log(FUSE_LOG_ERR,
"fuse: symbol <%s> not found in module: %s\n",
283 ret = fuse_register_module(module, factory, so);
298 static struct fuse_module *fuse_find_module(
const char *module)
301 for (m = fuse_modules; m; m = m->next) {
302 if (strcmp(module, m->name) == 0) {
310 static struct fuse_module *fuse_get_module(
const char *module)
314 pthread_mutex_lock(&fuse_context_lock);
315 m = fuse_find_module(module);
317 int err = fuse_load_so_module(module);
319 m = fuse_find_module(module);
321 pthread_mutex_unlock(&fuse_context_lock);
327 pthread_mutex_lock(&fuse_context_lock);
333 if (!m->ctr && m->so) {
334 struct fusemod_so *so = m->so;
339 for (mp = &fuse_modules; *mp;) {
341 fuse_unregister_module(*mp);
348 }
else if (!m->ctr) {
349 fuse_unregister_module(m);
351 pthread_mutex_unlock(&fuse_context_lock);
354 static void init_list_head(
struct list_head *list)
360 static int list_empty(
const struct list_head *head)
362 return head->next == head;
365 static void list_add(
struct list_head *
new,
struct list_head *prev,
366 struct list_head *next)
374 static inline void list_add_head(
struct list_head *
new,
struct list_head *head)
376 list_add(
new, head, head->next);
379 static inline void list_add_tail(
struct list_head *
new,
struct list_head *head)
381 list_add(
new, head->prev, head);
384 static inline void list_del(
struct list_head *entry)
386 struct list_head *prev = entry->prev;
387 struct list_head *next = entry->next;
393 static inline int lru_enabled(
struct fuse *f)
395 return f->conf.remember > 0;
398 static struct node_lru *node_lru(
struct node *node)
400 return (
struct node_lru *) node;
403 static size_t get_node_size(
struct fuse *f)
406 return sizeof(
struct node_lru);
408 return sizeof(
struct node);
411 #ifdef FUSE_NODE_SLAB 412 static struct node_slab *list_to_slab(
struct list_head *head)
414 return (
struct node_slab *) head;
417 static struct node_slab *node_to_slab(
struct fuse *f,
struct node *node)
419 return (
struct node_slab *) (((uintptr_t) node) & ~((uintptr_t) f->pagesize - 1));
422 static int alloc_slab(
struct fuse *f)
425 struct node_slab *slab;
429 size_t node_size = get_node_size(f);
431 mem = mmap(NULL, f->pagesize, PROT_READ | PROT_WRITE,
432 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
434 if (mem == MAP_FAILED)
438 init_list_head(&slab->freelist);
440 num = (f->pagesize -
sizeof(
struct node_slab)) / node_size;
442 start = (
char *) mem + f->pagesize - num * node_size;
443 for (i = 0; i < num; i++) {
446 n = (
struct list_head *) (start + i * node_size);
447 list_add_tail(n, &slab->freelist);
449 list_add_tail(&slab->list, &f->partial_slabs);
454 static struct node *alloc_node(
struct fuse *f)
456 struct node_slab *slab;
457 struct list_head *node;
459 if (list_empty(&f->partial_slabs)) {
460 int res = alloc_slab(f);
464 slab = list_to_slab(f->partial_slabs.next);
466 node = slab->freelist.next;
468 if (list_empty(&slab->freelist)) {
469 list_del(&slab->list);
470 list_add_tail(&slab->list, &f->full_slabs);
472 memset(node, 0,
sizeof(
struct node));
474 return (
struct node *) node;
477 static void free_slab(
struct fuse *f,
struct node_slab *slab)
481 list_del(&slab->list);
482 res = munmap(slab, f->pagesize);
484 fuse_log(FUSE_LOG_WARNING,
"fuse warning: munmap(%p) failed\n",
488 static void free_node_mem(
struct fuse *f,
struct node *node)
490 struct node_slab *slab = node_to_slab(f, node);
491 struct list_head *n = (
struct list_head *) node;
495 if (list_empty(&slab->freelist)) {
496 list_del(&slab->list);
497 list_add_tail(&slab->list, &f->partial_slabs);
499 list_add_head(n, &slab->freelist);
505 static struct node *alloc_node(
struct fuse *f)
507 return (
struct node *) calloc(1, get_node_size(f));
510 static void free_node_mem(
struct fuse *f,
struct node *node)
517 static size_t id_hash(
struct fuse *f,
fuse_ino_t ino)
519 uint64_t hash = ((uint32_t) ino * 2654435761U) % f->id_table.size;
520 uint64_t oldhash = hash % (f->id_table.size / 2);
522 if (oldhash >= f->id_table.split)
528 static struct node *get_node_nocheck(
struct fuse *f,
fuse_ino_t nodeid)
530 size_t hash = id_hash(f, nodeid);
533 for (node = f->id_table.array[hash]; node != NULL; node = node->id_next)
534 if (node->nodeid == nodeid)
540 static struct node *get_node(
struct fuse *f,
fuse_ino_t nodeid)
542 struct node *node = get_node_nocheck(f, nodeid);
544 fuse_log(FUSE_LOG_ERR,
"fuse internal error: node %llu not found\n",
545 (
unsigned long long) nodeid);
551 static void curr_time(
struct timespec *now);
552 static double diff_timespec(
const struct timespec *t1,
553 const struct timespec *t2);
555 static void remove_node_lru(
struct node *node)
557 struct node_lru *lnode = node_lru(node);
558 list_del(&lnode->lru);
559 init_list_head(&lnode->lru);
562 static void set_forget_time(
struct fuse *f,
struct node *node)
564 struct node_lru *lnode = node_lru(node);
566 list_del(&lnode->lru);
567 list_add_tail(&lnode->lru, &f->lru_table);
568 curr_time(&lnode->forget_time);
571 static void free_node(
struct fuse *f,
struct node *node)
573 if (node->name != node->inline_name)
575 free_node_mem(f, node);
578 static void node_table_reduce(
struct node_table *t)
580 size_t newsize = t->size / 2;
583 if (newsize < NODE_TABLE_MIN_SIZE)
586 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
587 if (newarray != NULL)
591 t->split = t->size / 2;
594 static void remerge_id(
struct fuse *f)
596 struct node_table *t = &f->id_table;
600 node_table_reduce(t);
602 for (iter = 8; t->split > 0 && iter; iter--) {
606 upper = &t->array[t->split + t->size / 2];
610 for (nodep = &t->array[t->split]; *nodep;
611 nodep = &(*nodep)->id_next);
620 static void unhash_id(
struct fuse *f,
struct node *node)
622 struct node **nodep = &f->id_table.array[id_hash(f, node->nodeid)];
624 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
625 if (*nodep == node) {
626 *nodep = node->id_next;
629 if(f->id_table.use < f->id_table.size / 4)
635 static int node_table_resize(
struct node_table *t)
637 size_t newsize = t->size * 2;
640 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
641 if (newarray == NULL)
645 memset(t->array + t->size, 0, t->size *
sizeof(
struct node *));
652 static void rehash_id(
struct fuse *f)
654 struct node_table *t = &f->id_table;
659 if (t->split == t->size / 2)
664 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
665 struct node *node = *nodep;
666 size_t newhash = id_hash(f, node->nodeid);
668 if (newhash != hash) {
670 *nodep = node->id_next;
671 node->id_next = t->array[newhash];
672 t->array[newhash] = node;
674 next = &node->id_next;
677 if (t->split == t->size / 2)
678 node_table_resize(t);
681 static void hash_id(
struct fuse *f,
struct node *node)
683 size_t hash = id_hash(f, node->nodeid);
684 node->id_next = f->id_table.array[hash];
685 f->id_table.array[hash] = node;
688 if (f->id_table.use >= f->id_table.size / 2)
692 static size_t name_hash(
struct fuse *f,
fuse_ino_t parent,
695 uint64_t hash = parent;
698 for (; *name; name++)
699 hash = hash * 31 + (
unsigned char) *name;
701 hash %= f->name_table.size;
702 oldhash = hash % (f->name_table.size / 2);
703 if (oldhash >= f->name_table.split)
709 static void unref_node(
struct fuse *f,
struct node *node);
711 static void remerge_name(
struct fuse *f)
713 struct node_table *t = &f->name_table;
717 node_table_reduce(t);
719 for (iter = 8; t->split > 0 && iter; iter--) {
723 upper = &t->array[t->split + t->size / 2];
727 for (nodep = &t->array[t->split]; *nodep;
728 nodep = &(*nodep)->name_next);
737 static void unhash_name(
struct fuse *f,
struct node *node)
740 size_t hash = name_hash(f, node->parent->nodeid, node->name);
741 struct node **nodep = &f->name_table.array[hash];
743 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
744 if (*nodep == node) {
745 *nodep = node->name_next;
746 node->name_next = NULL;
747 unref_node(f, node->parent);
748 if (node->name != node->inline_name)
754 if (f->name_table.use < f->name_table.size / 4)
759 "fuse internal error: unable to unhash node: %llu\n",
760 (
unsigned long long) node->nodeid);
765 static void rehash_name(
struct fuse *f)
767 struct node_table *t = &f->name_table;
772 if (t->split == t->size / 2)
777 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
778 struct node *node = *nodep;
779 size_t newhash = name_hash(f, node->parent->nodeid, node->name);
781 if (newhash != hash) {
783 *nodep = node->name_next;
784 node->name_next = t->array[newhash];
785 t->array[newhash] = node;
787 next = &node->name_next;
790 if (t->split == t->size / 2)
791 node_table_resize(t);
794 static int hash_name(
struct fuse *f,
struct node *node,
fuse_ino_t parentid,
797 size_t hash = name_hash(f, parentid, name);
798 struct node *parent = get_node(f, parentid);
799 if (strlen(name) <
sizeof(node->inline_name)) {
800 strcpy(node->inline_name, name);
801 node->name = node->inline_name;
803 node->name = strdup(name);
804 if (node->name == NULL)
809 node->parent = parent;
810 node->name_next = f->name_table.array[hash];
811 f->name_table.array[hash] = node;
814 if (f->name_table.use >= f->name_table.size / 2)
820 static void delete_node(
struct fuse *f,
struct node *node)
823 fuse_log(FUSE_LOG_DEBUG,
"DELETE: %llu\n",
824 (
unsigned long long) node->nodeid);
826 assert(node->treelock == 0);
827 unhash_name(f, node);
829 remove_node_lru(node);
834 static void unref_node(
struct fuse *f,
struct node *node)
836 assert(node->refctr > 0);
839 delete_node(f, node);
845 f->ctr = (f->ctr + 1) & 0xffffffff;
848 }
while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO ||
849 get_node_nocheck(f, f->ctr) != NULL);
853 static struct node *lookup_node(
struct fuse *f,
fuse_ino_t parent,
856 size_t hash = name_hash(f, parent, name);
859 for (node = f->name_table.array[hash]; node != NULL; node = node->name_next)
860 if (node->parent->nodeid == parent &&
861 strcmp(node->name, name) == 0)
867 static void inc_nlookup(
struct node *node)
874 static struct node *find_node(
struct fuse *f,
fuse_ino_t parent,
879 pthread_mutex_lock(&f->lock);
881 node = get_node(f, parent);
883 node = lookup_node(f, parent, name);
885 node = alloc_node(f);
889 node->nodeid = next_id(f);
890 node->generation = f->generation;
891 if (f->conf.remember)
894 if (hash_name(f, node, parent, name) == -1) {
900 if (lru_enabled(f)) {
901 struct node_lru *lnode = node_lru(node);
902 init_list_head(&lnode->lru);
904 }
else if (lru_enabled(f) && node->nlookup == 1) {
905 remove_node_lru(node);
909 pthread_mutex_unlock(&f->lock);
913 static int lookup_path_in_cache(
struct fuse *f,
916 char *tmp = strdup(path);
920 pthread_mutex_lock(&f->lock);
925 char *path_element = strtok_r(tmp,
"/", &save_ptr);
926 while (path_element != NULL) {
927 struct node *node = lookup_node(f, ino, path_element);
933 path_element = strtok_r(NULL,
"/", &save_ptr);
935 pthread_mutex_unlock(&f->lock);
943 static char *add_name(
char **buf,
unsigned *bufsize,
char *s,
const char *name)
945 size_t len = strlen(name);
947 if (s - len <= *buf) {
948 unsigned pathlen = *bufsize - (s - *buf);
949 unsigned newbufsize = *bufsize;
952 while (newbufsize < pathlen + len + 1) {
953 if (newbufsize >= 0x80000000)
954 newbufsize = 0xffffffff;
959 newbuf = realloc(*buf, newbufsize);
964 s = newbuf + newbufsize - pathlen;
965 memmove(s, newbuf + *bufsize - pathlen, pathlen);
966 *bufsize = newbufsize;
969 memcpy(s, name, len);
976 static void unlock_path(
struct fuse *f,
fuse_ino_t nodeid,
struct node *wnode,
982 assert(wnode->treelock == TREELOCK_WRITE);
986 for (node = get_node(f, nodeid);
987 node != end && node->nodeid !=
FUSE_ROOT_ID; node = node->parent) {
988 assert(node->treelock != 0);
989 assert(node->treelock != TREELOCK_WAIT_OFFSET);
990 assert(node->treelock != TREELOCK_WRITE);
992 if (node->treelock == TREELOCK_WAIT_OFFSET)
997 static int try_get_path(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
998 char **path,
struct node **wnodep,
bool need_lock)
1000 unsigned bufsize = 256;
1004 struct node *wnode = NULL;
1010 buf = malloc(bufsize);
1014 s = buf + bufsize - 1;
1018 s = add_name(&buf, &bufsize, s, name);
1026 wnode = lookup_node(f, nodeid, name);
1028 if (wnode->treelock != 0) {
1029 if (wnode->treelock > 0)
1030 wnode->treelock += TREELOCK_WAIT_OFFSET;
1034 wnode->treelock = TREELOCK_WRITE;
1038 for (node = get_node(f, nodeid); node->nodeid !=
FUSE_ROOT_ID;
1039 node = node->parent) {
1041 if (node->name == NULL || node->parent == NULL)
1045 s = add_name(&buf, &bufsize, s, node->name);
1051 if (node->treelock < 0)
1059 memmove(buf, s, bufsize - (s - buf));
1071 unlock_path(f, nodeid, wnode, node);
1079 static void queue_element_unlock(
struct fuse *f,
struct lock_queue_element *qe)
1083 if (qe->first_locked) {
1084 wnode = qe->wnode1 ? *qe->wnode1 : NULL;
1085 unlock_path(f, qe->nodeid1, wnode, NULL);
1086 qe->first_locked =
false;
1088 if (qe->second_locked) {
1089 wnode = qe->wnode2 ? *qe->wnode2 : NULL;
1090 unlock_path(f, qe->nodeid2, wnode, NULL);
1091 qe->second_locked =
false;
1095 static void queue_element_wakeup(
struct fuse *f,
struct lock_queue_element *qe)
1098 bool first = (qe == f->lockq);
1102 if (get_node(f, qe->nodeid1)->treelock == 0)
1103 pthread_cond_signal(&qe->cond);
1108 if (!qe->first_locked) {
1109 err = try_get_path(f, qe->nodeid1, qe->name1, qe->path1,
1112 qe->first_locked =
true;
1113 else if (err != -EAGAIN)
1116 if (!qe->second_locked && qe->path2) {
1117 err = try_get_path(f, qe->nodeid2, qe->name2, qe->path2,
1120 qe->second_locked =
true;
1121 else if (err != -EAGAIN)
1125 if (qe->first_locked && (qe->second_locked || !qe->path2)) {
1138 queue_element_unlock(f, qe);
1144 queue_element_unlock(f, qe);
1148 pthread_cond_signal(&qe->cond);
1151 static void wake_up_queued(
struct fuse *f)
1153 struct lock_queue_element *qe;
1155 for (qe = f->lockq; qe != NULL; qe = qe->next)
1156 queue_element_wakeup(f, qe);
1159 static void debug_path(
struct fuse *f,
const char *msg,
fuse_ino_t nodeid,
1160 const char *name,
bool wr)
1162 if (f->conf.debug) {
1163 struct node *wnode = NULL;
1166 wnode = lookup_node(f, nodeid, name);
1169 fuse_log(FUSE_LOG_DEBUG,
"%s %llu (w)\n",
1170 msg, (
unsigned long long) wnode->nodeid);
1172 fuse_log(FUSE_LOG_DEBUG,
"%s %llu\n",
1173 msg, (
unsigned long long) nodeid);
1178 static void queue_path(
struct fuse *f,
struct lock_queue_element *qe)
1180 struct lock_queue_element **qp;
1183 qe->first_locked =
false;
1184 qe->second_locked =
false;
1185 pthread_cond_init(&qe->cond, NULL);
1187 for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
1191 static void dequeue_path(
struct fuse *f,
struct lock_queue_element *qe)
1193 struct lock_queue_element **qp;
1195 pthread_cond_destroy(&qe->cond);
1196 for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
1200 static int wait_path(
struct fuse *f,
struct lock_queue_element *qe)
1205 pthread_cond_wait(&qe->cond, &f->lock);
1206 }
while (!qe->done);
1208 dequeue_path(f, qe);
1213 static int get_path_common(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1214 char **path,
struct node **wnode)
1218 pthread_mutex_lock(&f->lock);
1219 err = try_get_path(f, nodeid, name, path, wnode,
true);
1220 if (err == -EAGAIN) {
1221 struct lock_queue_element qe = {
1227 debug_path(f,
"QUEUE PATH", nodeid, name, !!wnode);
1228 err = wait_path(f, &qe);
1229 debug_path(f,
"DEQUEUE PATH", nodeid, name, !!wnode);
1231 pthread_mutex_unlock(&f->lock);
1236 static int get_path(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1238 return get_path_common(f, nodeid, NULL, path, NULL);
1241 static int get_path_nullok(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1245 if (f->conf.nullpath_ok) {
1248 err = get_path_common(f, nodeid, NULL, path, NULL);
1256 static int get_path_name(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1259 return get_path_common(f, nodeid, name, path, NULL);
1262 static int get_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1263 char **path,
struct node **wnode)
1265 return get_path_common(f, nodeid, name, path, wnode);
1268 #if defined(__FreeBSD__) 1269 #define CHECK_DIR_LOOP 1272 #if defined(CHECK_DIR_LOOP) 1273 static int check_dir_loop(
struct fuse *f,
1277 struct node *node, *node1, *node2;
1280 node1 = lookup_node(f, nodeid1, name1);
1281 id1 = node1 ? node1->nodeid : nodeid1;
1283 node2 = lookup_node(f, nodeid2, name2);
1284 id2 = node2 ? node2->nodeid : nodeid2;
1286 for (node = get_node(f, id2); node->nodeid !=
FUSE_ROOT_ID;
1287 node = node->parent) {
1288 if (node->name == NULL || node->parent == NULL)
1291 if (node->nodeid != id2 && node->nodeid == id1)
1297 for (node = get_node(f, id1); node->nodeid !=
FUSE_ROOT_ID;
1298 node = node->parent) {
1299 if (node->name == NULL || node->parent == NULL)
1302 if (node->nodeid != id1 && node->nodeid == id2)
1311 static int try_get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1313 char **path1,
char **path2,
1314 struct node **wnode1,
struct node **wnode2)
1319 err = try_get_path(f, nodeid1, name1, path1, wnode1,
true);
1321 err = try_get_path(f, nodeid2, name2, path2, wnode2,
true);
1323 struct node *wn1 = wnode1 ? *wnode1 : NULL;
1325 unlock_path(f, nodeid1, wn1, NULL);
1332 static int get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1334 char **path1,
char **path2,
1335 struct node **wnode1,
struct node **wnode2)
1339 pthread_mutex_lock(&f->lock);
1341 #if defined(CHECK_DIR_LOOP) 1345 err = check_dir_loop(f, nodeid1, name1, nodeid2, name2);
1351 err = try_get_path2(f, nodeid1, name1, nodeid2, name2,
1352 path1, path2, wnode1, wnode2);
1353 if (err == -EAGAIN) {
1354 struct lock_queue_element qe = {
1365 debug_path(f,
"QUEUE PATH1", nodeid1, name1, !!wnode1);
1366 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1367 err = wait_path(f, &qe);
1368 debug_path(f,
"DEQUEUE PATH1", nodeid1, name1, !!wnode1);
1369 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1372 #if defined(CHECK_DIR_LOOP) 1375 pthread_mutex_unlock(&f->lock);
1380 static void free_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
1381 struct node *wnode,
char *path)
1383 pthread_mutex_lock(&f->lock);
1384 unlock_path(f, nodeid, wnode, NULL);
1387 pthread_mutex_unlock(&f->lock);
1391 static void free_path(
struct fuse *f,
fuse_ino_t nodeid,
char *path)
1394 free_path_wrlock(f, nodeid, NULL, path);
1398 struct node *wnode1,
struct node *wnode2,
1399 char *path1,
char *path2)
1401 pthread_mutex_lock(&f->lock);
1402 unlock_path(f, nodeid1, wnode1, NULL);
1403 unlock_path(f, nodeid2, wnode2, NULL);
1405 pthread_mutex_unlock(&f->lock);
1410 static void forget_node(
struct fuse *f,
fuse_ino_t nodeid, uint64_t nlookup)
1415 pthread_mutex_lock(&f->lock);
1416 node = get_node(f, nodeid);
1422 while (node->nlookup == nlookup && node->treelock) {
1423 struct lock_queue_element qe = {
1427 debug_path(f,
"QUEUE PATH (forget)", nodeid, NULL,
false);
1431 pthread_cond_wait(&qe.cond, &f->lock);
1432 }
while (node->nlookup == nlookup && node->treelock);
1434 dequeue_path(f, &qe);
1435 debug_path(f,
"DEQUEUE_PATH (forget)", nodeid, NULL,
false);
1438 assert(node->nlookup >= nlookup);
1439 node->nlookup -= nlookup;
1440 if (!node->nlookup) {
1441 unref_node(f, node);
1442 }
else if (lru_enabled(f) && node->nlookup == 1) {
1443 set_forget_time(f, node);
1445 pthread_mutex_unlock(&f->lock);
1448 static void unlink_node(
struct fuse *f,
struct node *node)
1450 if (f->conf.remember) {
1451 assert(node->nlookup > 1);
1454 unhash_name(f, node);
1457 static void remove_node(
struct fuse *f,
fuse_ino_t dir,
const char *name)
1461 pthread_mutex_lock(&f->lock);
1462 node = lookup_node(f, dir, name);
1464 unlink_node(f, node);
1465 pthread_mutex_unlock(&f->lock);
1468 static int rename_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1469 fuse_ino_t newdir,
const char *newname,
int hide)
1472 struct node *newnode;
1475 pthread_mutex_lock(&f->lock);
1476 node = lookup_node(f, olddir, oldname);
1477 newnode = lookup_node(f, newdir, newname);
1481 if (newnode != NULL) {
1483 fuse_log(FUSE_LOG_ERR,
"fuse: hidden file got created during hiding\n");
1487 unlink_node(f, newnode);
1490 unhash_name(f, node);
1491 if (hash_name(f, node, newdir, newname) == -1) {
1497 node->is_hidden = 1;
1500 pthread_mutex_unlock(&f->lock);
1504 static int exchange_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1507 struct node *oldnode;
1508 struct node *newnode;
1511 pthread_mutex_lock(&f->lock);
1512 oldnode = lookup_node(f, olddir, oldname);
1513 newnode = lookup_node(f, newdir, newname);
1516 unhash_name(f, oldnode);
1518 unhash_name(f, newnode);
1522 if (hash_name(f, oldnode, newdir, newname) == -1)
1526 if (hash_name(f, newnode, olddir, oldname) == -1)
1531 pthread_mutex_unlock(&f->lock);
1535 static void set_stat(
struct fuse *f,
fuse_ino_t nodeid,
struct stat *stbuf)
1537 if (!f->conf.use_ino)
1538 stbuf->st_ino = nodeid;
1539 if (f->conf.set_mode)
1540 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1541 (0777 & ~f->conf.umask);
1542 if (f->conf.set_uid)
1543 stbuf->st_uid = f->conf.uid;
1544 if (f->conf.set_gid)
1545 stbuf->st_gid = f->conf.gid;
1553 static void fuse_intr_sighandler(
int sig)
1559 struct fuse_intr_data {
1561 pthread_cond_t cond;
1565 static void fuse_interrupt(
fuse_req_t req,
void *d_)
1567 struct fuse_intr_data *d = d_;
1568 struct fuse *f = req_fuse(req);
1570 if (d->id == pthread_self())
1573 pthread_mutex_lock(&f->lock);
1574 while (!d->finished) {
1576 struct timespec timeout;
1578 pthread_kill(d->id, f->conf.intr_signal);
1579 gettimeofday(&now, NULL);
1580 timeout.tv_sec = now.tv_sec + 1;
1581 timeout.tv_nsec = now.tv_usec * 1000;
1582 pthread_cond_timedwait(&d->cond, &f->lock, &timeout);
1584 pthread_mutex_unlock(&f->lock);
1587 static void fuse_do_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1588 struct fuse_intr_data *d)
1590 pthread_mutex_lock(&f->lock);
1592 pthread_cond_broadcast(&d->cond);
1593 pthread_mutex_unlock(&f->lock);
1595 pthread_cond_destroy(&d->cond);
1598 static void fuse_do_prepare_interrupt(
fuse_req_t req,
struct fuse_intr_data *d)
1600 d->id = pthread_self();
1601 pthread_cond_init(&d->cond, NULL);
1606 static inline void fuse_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1607 struct fuse_intr_data *d)
1610 fuse_do_finish_interrupt(f, req, d);
1613 static inline void fuse_prepare_interrupt(
struct fuse *f,
fuse_req_t req,
1614 struct fuse_intr_data *d)
1617 fuse_do_prepare_interrupt(req, d);
1621 char* buf,
size_t len)
1625 snprintf(buf, len,
"%llu", (
unsigned long long) fi->
fh);
1629 int fuse_fs_getattr(
struct fuse_fs *fs,
const char *path,
struct stat *buf,
1633 if (fs->op.getattr) {
1636 fuse_log(FUSE_LOG_DEBUG,
"getattr[%s] %s\n",
1637 file_info_string(fi, buf,
sizeof(buf)),
1640 return fs->op.getattr(path, buf, fi);
1646 int fuse_fs_rename(
struct fuse_fs *fs,
const char *oldpath,
1647 const char *newpath,
unsigned int flags)
1650 if (fs->op.rename) {
1652 fuse_log(FUSE_LOG_DEBUG,
"rename %s %s 0x%x\n", oldpath, newpath,
1655 return fs->op.rename(oldpath, newpath, flags);
1661 int fuse_fs_unlink(
struct fuse_fs *fs,
const char *path)
1664 if (fs->op.unlink) {
1666 fuse_log(FUSE_LOG_DEBUG,
"unlink %s\n", path);
1668 return fs->op.unlink(path);
1674 int fuse_fs_rmdir(
struct fuse_fs *fs,
const char *path)
1679 fuse_log(FUSE_LOG_DEBUG,
"rmdir %s\n", path);
1681 return fs->op.rmdir(path);
1687 int fuse_fs_symlink(
struct fuse_fs *fs,
const char *linkname,
const char *path)
1690 if (fs->op.symlink) {
1692 fuse_log(FUSE_LOG_DEBUG,
"symlink %s %s\n", linkname, path);
1694 return fs->op.symlink(linkname, path);
1700 int fuse_fs_link(
struct fuse_fs *fs,
const char *oldpath,
const char *newpath)
1705 fuse_log(FUSE_LOG_DEBUG,
"link %s %s\n", oldpath, newpath);
1707 return fs->op.link(oldpath, newpath);
1713 int fuse_fs_release(
struct fuse_fs *fs,
const char *path,
1717 if (fs->op.release) {
1719 fuse_log(FUSE_LOG_DEBUG,
"release%s[%llu] flags: 0x%x\n",
1720 fi->
flush ?
"+flush" :
"",
1721 (
unsigned long long) fi->
fh, fi->
flags);
1723 return fs->op.release(path, fi);
1729 int fuse_fs_opendir(
struct fuse_fs *fs,
const char *path,
1733 if (fs->op.opendir) {
1737 fuse_log(FUSE_LOG_DEBUG,
"opendir flags: 0x%x %s\n", fi->
flags,
1740 err = fs->op.opendir(path, fi);
1742 if (fs->debug && !err)
1743 fuse_log(FUSE_LOG_DEBUG,
" opendir[%llu] flags: 0x%x %s\n",
1744 (
unsigned long long) fi->
fh, fi->
flags, path);
1752 int fuse_fs_open(
struct fuse_fs *fs,
const char *path,
1760 fuse_log(FUSE_LOG_DEBUG,
"open flags: 0x%x %s\n", fi->
flags,
1763 err = fs->op.open(path, fi);
1765 if (fs->debug && !err)
1766 fuse_log(FUSE_LOG_DEBUG,
" open[%llu] flags: 0x%x %s\n",
1767 (
unsigned long long) fi->
fh, fi->
flags, path);
1775 static void fuse_free_buf(
struct fuse_bufvec *buf)
1780 for (i = 0; i < buf->
count; i++)
1787 int fuse_fs_read_buf(
struct fuse_fs *fs,
const char *path,
1788 struct fuse_bufvec **bufp,
size_t size, off_t off,
1792 if (fs->op.read || fs->op.read_buf) {
1797 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1798 (
unsigned long long) fi->
fh,
1799 size, (
unsigned long long) off, fi->
flags);
1801 if (fs->op.read_buf) {
1802 res = fs->op.read_buf(path, bufp, size, off, fi);
1816 *buf = FUSE_BUFVEC_INIT(size);
1820 res = fs->op.read(path, mem, size, off, fi);
1825 if (fs->debug && res >= 0)
1826 fuse_log(FUSE_LOG_DEBUG,
" read[%llu] %zu bytes from %llu\n",
1827 (
unsigned long long) fi->
fh,
1829 (
unsigned long long) off);
1831 fuse_log(FUSE_LOG_ERR,
"fuse: read too many bytes\n");
1842 int fuse_fs_read(
struct fuse_fs *fs,
const char *path,
char *mem,
size_t size,
1846 if (fs->op.read || fs->op.read_buf) {
1851 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1852 (
unsigned long long) fi->
fh,
1853 size, (
unsigned long long) off, fi->
flags);
1855 if (fs->op.read_buf) {
1858 res = fs->op.read_buf(path, &buf, size, off, fi);
1867 res = fs->op.read(path, mem, size, off, fi);
1870 if (fs->debug && res >= 0)
1871 fuse_log(FUSE_LOG_DEBUG,
" read[%llu] %u bytes from %llu\n",
1872 (
unsigned long long) fi->
fh,
1874 (
unsigned long long) off);
1875 if (res >= 0 && res > (
int) size)
1876 fuse_log(FUSE_LOG_ERR,
"fuse: read too many bytes\n");
1884 int fuse_fs_write_buf(
struct fuse_fs *fs,
const char *path,
1889 if (fs->op.write_buf || fs->op.write) {
1893 assert(buf->
idx == 0 && buf->
off == 0);
1896 "write%s[%llu] %zu bytes to %llu flags: 0x%x\n",
1898 (
unsigned long long) fi->
fh,
1900 (
unsigned long long) off,
1903 if (fs->op.write_buf) {
1904 res = fs->op.write_buf(path, buf, off, fi);
1910 if (buf->
count == 1 &&
1912 flatbuf = &buf->
buf[0];
1925 flatbuf = &tmp.
buf[0];
1928 res = fs->op.write(path, flatbuf->
mem, flatbuf->
size,
1934 if (fs->debug && res >= 0)
1935 fuse_log(FUSE_LOG_DEBUG,
" write%s[%llu] %u bytes to %llu\n",
1937 (
unsigned long long) fi->
fh, res,
1938 (
unsigned long long) off);
1939 if (res > (
int) size)
1940 fuse_log(FUSE_LOG_ERR,
"fuse: wrote too many bytes\n");
1948 int fuse_fs_write(
struct fuse_fs *fs,
const char *path,
const char *mem,
1953 bufv.
buf[0].
mem = (
void *) mem;
1955 return fuse_fs_write_buf(fs, path, &bufv, off, fi);
1958 int fuse_fs_fsync(
struct fuse_fs *fs,
const char *path,
int datasync,
1964 fuse_log(FUSE_LOG_DEBUG,
"fsync[%llu] datasync: %i\n",
1965 (
unsigned long long) fi->
fh, datasync);
1967 return fs->op.fsync(path, datasync, fi);
1973 int fuse_fs_fsyncdir(
struct fuse_fs *fs,
const char *path,
int datasync,
1977 if (fs->op.fsyncdir) {
1979 fuse_log(FUSE_LOG_DEBUG,
"fsyncdir[%llu] datasync: %i\n",
1980 (
unsigned long long) fi->
fh, datasync);
1982 return fs->op.fsyncdir(path, datasync, fi);
1988 int fuse_fs_flush(
struct fuse_fs *fs,
const char *path,
1994 fuse_log(FUSE_LOG_DEBUG,
"flush[%llu]\n",
1995 (
unsigned long long) fi->
fh);
1997 return fs->op.flush(path, fi);
2003 int fuse_fs_statfs(
struct fuse_fs *fs,
const char *path,
struct statvfs *buf)
2006 if (fs->op.statfs) {
2008 fuse_log(FUSE_LOG_DEBUG,
"statfs %s\n", path);
2010 return fs->op.statfs(path, buf);
2012 buf->f_namemax = 255;
2018 int fuse_fs_releasedir(
struct fuse_fs *fs,
const char *path,
2022 if (fs->op.releasedir) {
2024 fuse_log(FUSE_LOG_DEBUG,
"releasedir[%llu] flags: 0x%x\n",
2025 (
unsigned long long) fi->
fh, fi->
flags);
2027 return fs->op.releasedir(path, fi);
2033 int fuse_fs_readdir(
struct fuse_fs *fs,
const char *path,
void *buf,
2039 if (fs->op.readdir) {
2041 fuse_log(FUSE_LOG_DEBUG,
"readdir%s[%llu] from %llu\n",
2043 (
unsigned long long) fi->
fh,
2044 (
unsigned long long) off);
2047 return fs->op.readdir(path, buf, filler, off, fi, flags);
2053 int fuse_fs_create(
struct fuse_fs *fs,
const char *path, mode_t mode,
2057 if (fs->op.create) {
2062 "create flags: 0x%x %s 0%o umask=0%03o\n",
2063 fi->
flags, path, mode,
2066 err = fs->op.create(path, mode, fi);
2068 if (fs->debug && !err)
2069 fuse_log(FUSE_LOG_DEBUG,
" create[%llu] flags: 0x%x %s\n",
2070 (
unsigned long long) fi->
fh, fi->
flags, path);
2078 int fuse_fs_lock(
struct fuse_fs *fs,
const char *path,
2084 fuse_log(FUSE_LOG_DEBUG,
"lock[%llu] %s %s start: %llu len: %llu pid: %llu\n",
2085 (
unsigned long long) fi->
fh,
2086 (cmd == F_GETLK ?
"F_GETLK" :
2087 (cmd == F_SETLK ?
"F_SETLK" :
2088 (cmd == F_SETLKW ?
"F_SETLKW" :
"???"))),
2089 (lock->l_type == F_RDLCK ?
"F_RDLCK" :
2090 (lock->l_type == F_WRLCK ?
"F_WRLCK" :
2091 (lock->l_type == F_UNLCK ?
"F_UNLCK" :
2093 (
unsigned long long) lock->l_start,
2094 (
unsigned long long) lock->l_len,
2095 (
unsigned long long) lock->l_pid);
2097 return fs->op.lock(path, fi, cmd, lock);
2103 int fuse_fs_flock(
struct fuse_fs *fs,
const char *path,
2109 int xop = op & ~LOCK_NB;
2111 fuse_log(FUSE_LOG_DEBUG,
"lock[%llu] %s%s\n",
2112 (
unsigned long long) fi->
fh,
2113 xop == LOCK_SH ?
"LOCK_SH" :
2114 (xop == LOCK_EX ?
"LOCK_EX" :
2115 (xop == LOCK_UN ?
"LOCK_UN" :
"???")),
2116 (op & LOCK_NB) ?
"|LOCK_NB" :
"");
2118 return fs->op.flock(path, fi, op);
2124 int fuse_fs_chown(
struct fuse_fs *fs,
const char *path, uid_t uid,
2131 fuse_log(FUSE_LOG_DEBUG,
"chown[%s] %s %lu %lu\n",
2132 file_info_string(fi, buf,
sizeof(buf)),
2133 path, (
unsigned long) uid, (
unsigned long) gid);
2135 return fs->op.chown(path, uid, gid, fi);
2141 int fuse_fs_truncate(
struct fuse_fs *fs,
const char *path, off_t size,
2145 if (fs->op.truncate) {
2148 fuse_log(FUSE_LOG_DEBUG,
"truncate[%s] %llu\n",
2149 file_info_string(fi, buf,
sizeof(buf)),
2150 (
unsigned long long) size);
2152 return fs->op.truncate(path, size, fi);
2158 int fuse_fs_utimens(
struct fuse_fs *fs,
const char *path,
2162 if (fs->op.utimens) {
2165 fuse_log(FUSE_LOG_DEBUG,
"utimens[%s] %s %li.%09lu %li.%09lu\n",
2166 file_info_string(fi, buf,
sizeof(buf)),
2167 path, tv[0].tv_sec, tv[0].tv_nsec,
2168 tv[1].tv_sec, tv[1].tv_nsec);
2170 return fs->op.utimens(path, tv, fi);
2176 int fuse_fs_access(
struct fuse_fs *fs,
const char *path,
int mask)
2179 if (fs->op.access) {
2181 fuse_log(FUSE_LOG_DEBUG,
"access %s 0%o\n", path, mask);
2183 return fs->op.access(path, mask);
2189 int fuse_fs_readlink(
struct fuse_fs *fs,
const char *path,
char *buf,
2193 if (fs->op.readlink) {
2195 fuse_log(FUSE_LOG_DEBUG,
"readlink %s %lu\n", path,
2196 (
unsigned long) len);
2198 return fs->op.readlink(path, buf, len);
2204 int fuse_fs_mknod(
struct fuse_fs *fs,
const char *path, mode_t mode,
2210 fuse_log(FUSE_LOG_DEBUG,
"mknod %s 0%o 0x%llx umask=0%03o\n",
2211 path, mode, (
unsigned long long) rdev,
2214 return fs->op.mknod(path, mode, rdev);
2220 int fuse_fs_mkdir(
struct fuse_fs *fs,
const char *path, mode_t mode)
2225 fuse_log(FUSE_LOG_DEBUG,
"mkdir %s 0%o umask=0%03o\n",
2228 return fs->op.mkdir(path, mode);
2234 int fuse_fs_setxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2235 const char *value,
size_t size,
int flags)
2238 if (fs->op.setxattr) {
2240 fuse_log(FUSE_LOG_DEBUG,
"setxattr %s %s %lu 0x%x\n",
2241 path, name, (
unsigned long) size, flags);
2243 return fs->op.setxattr(path, name, value, size, flags);
2249 int fuse_fs_getxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2250 char *value,
size_t size)
2253 if (fs->op.getxattr) {
2255 fuse_log(FUSE_LOG_DEBUG,
"getxattr %s %s %lu\n",
2256 path, name, (
unsigned long) size);
2258 return fs->op.getxattr(path, name, value, size);
2264 int fuse_fs_listxattr(
struct fuse_fs *fs,
const char *path,
char *list,
2268 if (fs->op.listxattr) {
2270 fuse_log(FUSE_LOG_DEBUG,
"listxattr %s %lu\n",
2271 path, (
unsigned long) size);
2273 return fs->op.listxattr(path, list, size);
2279 int fuse_fs_bmap(
struct fuse_fs *fs,
const char *path,
size_t blocksize,
2285 fuse_log(FUSE_LOG_DEBUG,
"bmap %s blocksize: %lu index: %llu\n",
2286 path, (
unsigned long) blocksize,
2287 (
unsigned long long) *idx);
2289 return fs->op.bmap(path, blocksize, idx);
2295 int fuse_fs_removexattr(
struct fuse_fs *fs,
const char *path,
const char *name)
2298 if (fs->op.removexattr) {
2300 fuse_log(FUSE_LOG_DEBUG,
"removexattr %s %s\n", path, name);
2302 return fs->op.removexattr(path, name);
2308 int fuse_fs_ioctl(
struct fuse_fs *fs,
const char *path,
unsigned int cmd,
2315 fuse_log(FUSE_LOG_DEBUG,
"ioctl[%llu] 0x%x flags: 0x%x\n",
2316 (
unsigned long long) fi->
fh, cmd, flags);
2318 return fs->op.ioctl(path, cmd, arg, fi, flags, data);
2323 int fuse_fs_poll(
struct fuse_fs *fs,
const char *path,
2332 fuse_log(FUSE_LOG_DEBUG,
"poll[%llu] ph: %p, events 0x%x\n",
2333 (
unsigned long long) fi->
fh, ph,
2336 res = fs->op.poll(path, fi, ph, reventsp);
2338 if (fs->debug && !res)
2339 fuse_log(FUSE_LOG_DEBUG,
" poll[%llu] revents: 0x%x\n",
2340 (
unsigned long long) fi->
fh, *reventsp);
2347 int fuse_fs_fallocate(
struct fuse_fs *fs,
const char *path,
int mode,
2351 if (fs->op.fallocate) {
2353 fuse_log(FUSE_LOG_DEBUG,
"fallocate %s mode %x, offset: %llu, length: %llu\n",
2356 (
unsigned long long) offset,
2357 (
unsigned long long) length);
2359 return fs->op.fallocate(path, mode, offset, length, fi);
2364 ssize_t fuse_fs_copy_file_range(
struct fuse_fs *fs,
const char *path_in,
2366 const char *path_out,
2368 size_t len,
int flags)
2371 if (fs->op.copy_file_range) {
2373 fuse_log(FUSE_LOG_DEBUG,
"copy_file_range from %s:%llu to " 2374 "%s:%llu, length: %llu\n",
2376 (
unsigned long long) off_in,
2378 (
unsigned long long) off_out,
2379 (
unsigned long long) len);
2381 return fs->op.copy_file_range(path_in, fi_in, off_in, path_out,
2382 fi_out, off_out, len, flags);
2387 static int is_open(
struct fuse *f,
fuse_ino_t dir,
const char *name)
2391 pthread_mutex_lock(&f->lock);
2392 node = lookup_node(f, dir, name);
2393 if (node && node->open_count > 0)
2395 pthread_mutex_unlock(&f->lock);
2399 static char *hidden_name(
struct fuse *f,
fuse_ino_t dir,
const char *oldname,
2400 char *newname,
size_t bufsize)
2404 struct node *newnode;
2410 pthread_mutex_lock(&f->lock);
2411 node = lookup_node(f, dir, oldname);
2413 pthread_mutex_unlock(&f->lock);
2418 snprintf(newname, bufsize,
".fuse_hidden%08x%08x",
2419 (
unsigned int) node->nodeid, f->hidectr);
2420 newnode = lookup_node(f, dir, newname);
2423 res = try_get_path(f, dir, newname, &newpath, NULL,
false);
2424 pthread_mutex_unlock(&f->lock);
2428 memset(&buf, 0,
sizeof(buf));
2429 res = fuse_fs_getattr(f->fs, newpath, &buf, NULL);
2434 }
while(res == 0 && --failctr);
2439 static int hide_node(
struct fuse *f,
const char *oldpath,
2446 newpath = hidden_name(f, dir, oldname, newname,
sizeof(newname));
2448 err = fuse_fs_rename(f->fs, oldpath, newpath, 0);
2450 err = rename_node(f, dir, oldname, dir, newname, 1);
2456 static int mtime_eq(
const struct stat *stbuf,
const struct timespec *ts)
2458 return stbuf->st_mtime == ts->tv_sec &&
2459 ST_MTIM_NSEC(stbuf) == ts->tv_nsec;
2462 #ifndef CLOCK_MONOTONIC 2463 #define CLOCK_MONOTONIC CLOCK_REALTIME 2466 static void curr_time(
struct timespec *now)
2468 static clockid_t clockid = CLOCK_MONOTONIC;
2469 int res = clock_gettime(clockid, now);
2470 if (res == -1 && errno == EINVAL) {
2471 clockid = CLOCK_REALTIME;
2472 res = clock_gettime(clockid, now);
2475 perror(
"fuse: clock_gettime");
2480 static void update_stat(
struct node *node,
const struct stat *stbuf)
2482 if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) ||
2483 stbuf->st_size != node->size))
2484 node->cache_valid = 0;
2485 node->mtime.tv_sec = stbuf->st_mtime;
2486 node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf);
2487 node->size = stbuf->st_size;
2488 curr_time(&node->stat_updated);
2491 static int do_lookup(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
2496 node = find_node(f, nodeid, name);
2500 e->
ino = node->nodeid;
2504 if (f->conf.auto_cache) {
2505 pthread_mutex_lock(&f->lock);
2506 update_stat(node, &e->
attr);
2507 pthread_mutex_unlock(&f->lock);
2509 set_stat(f, e->
ino, &e->
attr);
2513 static int lookup_path(
struct fuse *f,
fuse_ino_t nodeid,
2514 const char *name,
const char *path,
2520 res = fuse_fs_getattr(f->fs, path, &e->
attr, fi);
2522 res = do_lookup(f, nodeid, name, e);
2523 if (res == 0 && f->conf.debug) {
2524 fuse_log(FUSE_LOG_DEBUG,
" NODEID: %llu\n",
2525 (
unsigned long long) e->
ino);
2531 static struct fuse_context_i *fuse_get_context_internal(
void)
2533 return (
struct fuse_context_i *) pthread_getspecific(fuse_context_key);
2536 static struct fuse_context_i *fuse_create_context(
struct fuse *f)
2538 struct fuse_context_i *c = fuse_get_context_internal();
2540 c = (
struct fuse_context_i *)
2541 calloc(1,
sizeof(
struct fuse_context_i));
2547 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate thread specific data\n");
2550 pthread_setspecific(fuse_context_key, c);
2552 memset(c, 0,
sizeof(*c));
2559 static void fuse_freecontext(
void *data)
2564 static int fuse_create_context_key(
void)
2567 pthread_mutex_lock(&fuse_context_lock);
2568 if (!fuse_context_ref) {
2569 err = pthread_key_create(&fuse_context_key, fuse_freecontext);
2571 fuse_log(FUSE_LOG_ERR,
"fuse: failed to create thread specific key: %s\n",
2573 pthread_mutex_unlock(&fuse_context_lock);
2578 pthread_mutex_unlock(&fuse_context_lock);
2582 static void fuse_delete_context_key(
void)
2584 pthread_mutex_lock(&fuse_context_lock);
2586 if (!fuse_context_ref) {
2587 free(pthread_getspecific(fuse_context_key));
2588 pthread_key_delete(fuse_context_key);
2590 pthread_mutex_unlock(&fuse_context_lock);
2593 static struct fuse *req_fuse_prepare(
fuse_req_t req)
2595 struct fuse_context_i *c = fuse_create_context(req_fuse(req));
2598 c->ctx.uid = ctx->
uid;
2599 c->ctx.gid = ctx->
gid;
2600 c->ctx.pid = ctx->
pid;
2601 c->ctx.umask = ctx->
umask;
2605 static inline void reply_err(
fuse_req_t req,
int err)
2615 struct fuse *f = req_fuse(req);
2619 forget_node(f, e->
ino, 1);
2622 reply_err(req, err);
2625 void fuse_fs_init(
struct fuse_fs *fs,
struct fuse_conn_info *conn,
2629 if (!fs->op.write_buf)
2636 fs->user_data = fs->op.init(conn, cfg);
2639 static void fuse_lib_init(
void *data,
struct fuse_conn_info *conn)
2641 struct fuse *f = (
struct fuse *) data;
2643 fuse_create_context(f);
2646 fuse_fs_init(f->fs, conn, &f->conf);
2649 void fuse_fs_destroy(
struct fuse_fs *fs)
2653 fs->op.destroy(fs->user_data);
2655 fuse_put_module(fs->m);
2659 static void fuse_lib_destroy(
void *data)
2661 struct fuse *f = (
struct fuse *) data;
2663 fuse_create_context(f);
2664 fuse_fs_destroy(f->fs);
2671 struct fuse *f = req_fuse_prepare(req);
2675 struct node *dot = NULL;
2677 if (name[0] ==
'.') {
2678 int len = strlen(name);
2680 if (len == 1 || (name[1] ==
'.' && len == 2)) {
2681 pthread_mutex_lock(&f->lock);
2684 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP-DOT\n");
2685 dot = get_node_nocheck(f, parent);
2687 pthread_mutex_unlock(&f->lock);
2688 reply_entry(req, &e, -ESTALE);
2694 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP-DOTDOT\n");
2695 parent = get_node(f, parent)->parent->nodeid;
2697 pthread_mutex_unlock(&f->lock);
2702 err = get_path_name(f, parent, name, &path);
2704 struct fuse_intr_data d;
2706 fuse_log(FUSE_LOG_DEBUG,
"LOOKUP %s\n", path);
2707 fuse_prepare_interrupt(f, req, &d);
2708 err = lookup_path(f, parent, name, path, &e, NULL);
2709 if (err == -ENOENT && f->conf.negative_timeout != 0.0) {
2714 fuse_finish_interrupt(f, req, &d);
2715 free_path(f, parent, path);
2718 pthread_mutex_lock(&f->lock);
2720 pthread_mutex_unlock(&f->lock);
2722 reply_entry(req, &e, err);
2725 static void do_forget(
struct fuse *f,
fuse_ino_t ino, uint64_t nlookup)
2728 fuse_log(FUSE_LOG_DEBUG,
"FORGET %llu/%llu\n", (
unsigned long long)ino,
2729 (
unsigned long long) nlookup);
2730 forget_node(f, ino, nlookup);
2735 do_forget(req_fuse(req), ino, nlookup);
2739 static void fuse_lib_forget_multi(
fuse_req_t req,
size_t count,
2740 struct fuse_forget_data *forgets)
2742 struct fuse *f = req_fuse(req);
2745 for (i = 0; i < count; i++)
2746 do_forget(f, forgets[i].ino, forgets[i].nlookup);
2755 struct fuse *f = req_fuse_prepare(req);
2760 memset(&buf, 0,
sizeof(buf));
2763 err = get_path_nullok(f, ino, &path);
2765 err = get_path(f, ino, &path);
2767 struct fuse_intr_data d;
2768 fuse_prepare_interrupt(f, req, &d);
2769 err = fuse_fs_getattr(f->fs, path, &buf, fi);
2770 fuse_finish_interrupt(f, req, &d);
2771 free_path(f, ino, path);
2776 pthread_mutex_lock(&f->lock);
2777 node = get_node(f, ino);
2778 if (node->is_hidden && buf.st_nlink > 0)
2780 if (f->conf.auto_cache)
2781 update_stat(node, &buf);
2782 pthread_mutex_unlock(&f->lock);
2783 set_stat(f, ino, &buf);
2786 reply_err(req, err);
2789 int fuse_fs_chmod(
struct fuse_fs *fs,
const char *path, mode_t mode,
2796 fuse_log(FUSE_LOG_DEBUG,
"chmod[%s] %s %llo\n",
2797 file_info_string(fi, buf,
sizeof(buf)),
2798 path, (
unsigned long long) mode);
2800 return fs->op.chmod(path, mode, fi);
2809 struct fuse *f = req_fuse_prepare(req);
2814 memset(&buf, 0,
sizeof(buf));
2816 err = get_path_nullok(f, ino, &path);
2818 err = get_path(f, ino, &path);
2820 struct fuse_intr_data d;
2821 fuse_prepare_interrupt(f, req, &d);
2823 if (!err && (valid & FUSE_SET_ATTR_MODE))
2824 err = fuse_fs_chmod(f->fs, path, attr->st_mode, fi);
2825 if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) {
2826 uid_t uid = (valid & FUSE_SET_ATTR_UID) ?
2827 attr->st_uid : (uid_t) -1;
2828 gid_t gid = (valid & FUSE_SET_ATTR_GID) ?
2829 attr->st_gid : (gid_t) -1;
2830 err = fuse_fs_chown(f->fs, path, uid, gid, fi);
2832 if (!err && (valid & FUSE_SET_ATTR_SIZE)) {
2833 err = fuse_fs_truncate(f->fs, path,
2836 #ifdef HAVE_UTIMENSAT 2838 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) {
2839 struct timespec tv[2];
2843 tv[0].tv_nsec = UTIME_OMIT;
2844 tv[1].tv_nsec = UTIME_OMIT;
2846 if (valid & FUSE_SET_ATTR_ATIME_NOW)
2847 tv[0].tv_nsec = UTIME_NOW;
2848 else if (valid & FUSE_SET_ATTR_ATIME)
2849 tv[0] = attr->st_atim;
2851 if (valid & FUSE_SET_ATTR_MTIME_NOW)
2852 tv[1].tv_nsec = UTIME_NOW;
2853 else if (valid & FUSE_SET_ATTR_MTIME)
2854 tv[1] = attr->st_mtim;
2856 err = fuse_fs_utimens(f->fs, path, tv, fi);
2860 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) ==
2861 (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
2862 struct timespec tv[2];
2863 tv[0].tv_sec = attr->st_atime;
2864 tv[0].tv_nsec = ST_ATIM_NSEC(attr);
2865 tv[1].tv_sec = attr->st_mtime;
2866 tv[1].tv_nsec = ST_MTIM_NSEC(attr);
2867 err = fuse_fs_utimens(f->fs, path, tv, fi);
2870 err = fuse_fs_getattr(f->fs, path, &buf, fi);
2872 fuse_finish_interrupt(f, req, &d);
2873 free_path(f, ino, path);
2876 if (f->conf.auto_cache) {
2877 pthread_mutex_lock(&f->lock);
2878 update_stat(get_node(f, ino), &buf);
2879 pthread_mutex_unlock(&f->lock);
2881 set_stat(f, ino, &buf);
2884 reply_err(req, err);
2889 struct fuse *f = req_fuse_prepare(req);
2893 err = get_path(f, ino, &path);
2895 struct fuse_intr_data d;
2897 fuse_prepare_interrupt(f, req, &d);
2898 err = fuse_fs_access(f->fs, path, mask);
2899 fuse_finish_interrupt(f, req, &d);
2900 free_path(f, ino, path);
2902 reply_err(req, err);
2907 struct fuse *f = req_fuse_prepare(req);
2908 char linkname[PATH_MAX + 1];
2912 err = get_path(f, ino, &path);
2914 struct fuse_intr_data d;
2915 fuse_prepare_interrupt(f, req, &d);
2916 err = fuse_fs_readlink(f->fs, path, linkname,
sizeof(linkname));
2917 fuse_finish_interrupt(f, req, &d);
2918 free_path(f, ino, path);
2921 linkname[PATH_MAX] =
'\0';
2924 reply_err(req, err);
2928 mode_t mode, dev_t rdev)
2930 struct fuse *f = req_fuse_prepare(req);
2935 err = get_path_name(f, parent, name, &path);
2937 struct fuse_intr_data d;
2939 fuse_prepare_interrupt(f, req, &d);
2941 if (S_ISREG(mode)) {
2944 memset(&fi, 0,
sizeof(fi));
2945 fi.
flags = O_CREAT | O_EXCL | O_WRONLY;
2946 err = fuse_fs_create(f->fs, path, mode, &fi);
2948 err = lookup_path(f, parent, name, path, &e,
2950 fuse_fs_release(f->fs, path, &fi);
2953 if (err == -ENOSYS) {
2954 err = fuse_fs_mknod(f->fs, path, mode, rdev);
2956 err = lookup_path(f, parent, name, path, &e,
2959 fuse_finish_interrupt(f, req, &d);
2960 free_path(f, parent, path);
2962 reply_entry(req, &e, err);
2968 struct fuse *f = req_fuse_prepare(req);
2973 err = get_path_name(f, parent, name, &path);
2975 struct fuse_intr_data d;
2977 fuse_prepare_interrupt(f, req, &d);
2978 err = fuse_fs_mkdir(f->fs, path, mode);
2980 err = lookup_path(f, parent, name, path, &e, NULL);
2981 fuse_finish_interrupt(f, req, &d);
2982 free_path(f, parent, path);
2984 reply_entry(req, &e, err);
2990 struct fuse *f = req_fuse_prepare(req);
2995 err = get_path_wrlock(f, parent, name, &path, &wnode);
2997 struct fuse_intr_data d;
2999 fuse_prepare_interrupt(f, req, &d);
3000 if (!f->conf.hard_remove && is_open(f, parent, name)) {
3001 err = hide_node(f, path, parent, name);
3003 err = fuse_fs_unlink(f->fs, path);
3005 remove_node(f, parent, name);
3007 fuse_finish_interrupt(f, req, &d);
3008 free_path_wrlock(f, parent, wnode, path);
3010 reply_err(req, err);
3015 struct fuse *f = req_fuse_prepare(req);
3020 err = get_path_wrlock(f, parent, name, &path, &wnode);
3022 struct fuse_intr_data d;
3024 fuse_prepare_interrupt(f, req, &d);
3025 err = fuse_fs_rmdir(f->fs, path);
3026 fuse_finish_interrupt(f, req, &d);
3028 remove_node(f, parent, name);
3029 free_path_wrlock(f, parent, wnode, path);
3031 reply_err(req, err);
3034 static void fuse_lib_symlink(
fuse_req_t req,
const char *linkname,
3037 struct fuse *f = req_fuse_prepare(req);
3042 err = get_path_name(f, parent, name, &path);
3044 struct fuse_intr_data d;
3046 fuse_prepare_interrupt(f, req, &d);
3047 err = fuse_fs_symlink(f->fs, linkname, path);
3049 err = lookup_path(f, parent, name, path, &e, NULL);
3050 fuse_finish_interrupt(f, req, &d);
3051 free_path(f, parent, path);
3053 reply_entry(req, &e, err);
3058 const char *newname,
unsigned int flags)
3060 struct fuse *f = req_fuse_prepare(req);
3063 struct node *wnode1;
3064 struct node *wnode2;
3067 err = get_path2(f, olddir, oldname, newdir, newname,
3068 &oldpath, &newpath, &wnode1, &wnode2);
3070 struct fuse_intr_data d;
3072 fuse_prepare_interrupt(f, req, &d);
3073 if (!f->conf.hard_remove && !(flags & RENAME_EXCHANGE) &&
3074 is_open(f, newdir, newname))
3075 err = hide_node(f, newpath, newdir, newname);
3077 err = fuse_fs_rename(f->fs, oldpath, newpath, flags);
3079 if (flags & RENAME_EXCHANGE) {
3080 err = exchange_node(f, olddir, oldname,
3083 err = rename_node(f, olddir, oldname,
3084 newdir, newname, 0);
3088 fuse_finish_interrupt(f, req, &d);
3089 free_path2(f, olddir, newdir, wnode1, wnode2, oldpath, newpath);
3091 reply_err(req, err);
3095 const char *newname)
3097 struct fuse *f = req_fuse_prepare(req);
3103 err = get_path2(f, ino, NULL, newparent, newname,
3104 &oldpath, &newpath, NULL, NULL);
3106 struct fuse_intr_data d;
3108 fuse_prepare_interrupt(f, req, &d);
3109 err = fuse_fs_link(f->fs, oldpath, newpath);
3111 err = lookup_path(f, newparent, newname, newpath,
3113 fuse_finish_interrupt(f, req, &d);
3114 free_path2(f, ino, newparent, NULL, NULL, oldpath, newpath);
3116 reply_entry(req, &e, err);
3119 static void fuse_do_release(
struct fuse *f,
fuse_ino_t ino,
const char *path,
3123 int unlink_hidden = 0;
3125 fuse_fs_release(f->fs, path, fi);
3127 pthread_mutex_lock(&f->lock);
3128 node = get_node(f, ino);
3129 assert(node->open_count > 0);
3131 if (node->is_hidden && !node->open_count) {
3133 node->is_hidden = 0;
3135 pthread_mutex_unlock(&f->lock);
3139 fuse_fs_unlink(f->fs, path);
3140 }
else if (f->conf.nullpath_ok) {
3143 if (get_path(f, ino, &unlinkpath) == 0)
3144 fuse_fs_unlink(f->fs, unlinkpath);
3146 free_path(f, ino, unlinkpath);
3152 const char *name, mode_t mode,
3155 struct fuse *f = req_fuse_prepare(req);
3156 struct fuse_intr_data d;
3161 err = get_path_name(f, parent, name, &path);
3163 fuse_prepare_interrupt(f, req, &d);
3164 err = fuse_fs_create(f->fs, path, mode, fi);
3166 err = lookup_path(f, parent, name, path, &e, fi);
3168 fuse_fs_release(f->fs, path, fi);
3169 else if (!S_ISREG(e.
attr.st_mode)) {
3171 fuse_fs_release(f->fs, path, fi);
3172 forget_node(f, e.
ino, 1);
3174 if (f->conf.direct_io)
3176 if (f->conf.kernel_cache)
3181 fuse_finish_interrupt(f, req, &d);
3184 pthread_mutex_lock(&f->lock);
3185 get_node(f, e.
ino)->open_count++;
3186 pthread_mutex_unlock(&f->lock);
3190 fuse_do_release(f, e.
ino, path, fi);
3191 forget_node(f, e.
ino, 1);
3194 reply_err(req, err);
3197 free_path(f, parent, path);
3200 static double diff_timespec(
const struct timespec *t1,
3201 const struct timespec *t2)
3203 return (t1->tv_sec - t2->tv_sec) +
3204 ((double) t1->tv_nsec - (
double) t2->tv_nsec) / 1000000000.0;
3207 static void open_auto_cache(
struct fuse *f,
fuse_ino_t ino,
const char *path,
3212 pthread_mutex_lock(&f->lock);
3213 node = get_node(f, ino);
3214 if (node->cache_valid) {
3215 struct timespec now;
3218 if (diff_timespec(&now, &node->stat_updated) >
3219 f->conf.ac_attr_timeout) {
3222 pthread_mutex_unlock(&f->lock);
3223 err = fuse_fs_getattr(f->fs, path, &stbuf, fi);
3224 pthread_mutex_lock(&f->lock);
3226 update_stat(node, &stbuf);
3228 node->cache_valid = 0;
3231 if (node->cache_valid)
3234 node->cache_valid = 1;
3235 pthread_mutex_unlock(&f->lock);
3241 struct fuse *f = req_fuse_prepare(req);
3242 struct fuse_intr_data d;
3246 err = get_path(f, ino, &path);
3248 fuse_prepare_interrupt(f, req, &d);
3249 err = fuse_fs_open(f->fs, path, fi);
3251 if (f->conf.direct_io)
3253 if (f->conf.kernel_cache)
3256 if (f->conf.auto_cache)
3257 open_auto_cache(f, ino, path, fi);
3259 fuse_finish_interrupt(f, req, &d);
3262 pthread_mutex_lock(&f->lock);
3263 get_node(f, ino)->open_count++;
3264 pthread_mutex_unlock(&f->lock);
3268 fuse_do_release(f, ino, path, fi);
3271 reply_err(req, err);
3273 free_path(f, ino, path);
3279 struct fuse *f = req_fuse_prepare(req);
3284 res = get_path_nullok(f, ino, &path);
3286 struct fuse_intr_data d;
3288 fuse_prepare_interrupt(f, req, &d);
3289 res = fuse_fs_read_buf(f->fs, path, &buf, size, off, fi);
3290 fuse_finish_interrupt(f, req, &d);
3291 free_path(f, ino, path);
3297 reply_err(req, res);
3306 struct fuse *f = req_fuse_prepare(req);
3310 res = get_path_nullok(f, ino, &path);
3312 struct fuse_intr_data d;
3314 fuse_prepare_interrupt(f, req, &d);
3315 res = fuse_fs_write_buf(f->fs, path, buf, off, fi);
3316 fuse_finish_interrupt(f, req, &d);
3317 free_path(f, ino, path);
3323 reply_err(req, res);
3329 struct fuse *f = req_fuse_prepare(req);
3333 err = get_path_nullok(f, ino, &path);
3335 struct fuse_intr_data d;
3337 fuse_prepare_interrupt(f, req, &d);
3338 err = fuse_fs_fsync(f->fs, path, datasync, fi);
3339 fuse_finish_interrupt(f, req, &d);
3340 free_path(f, ino, path);
3342 reply_err(req, err);
3345 static struct fuse_dh *get_dirhandle(
const struct fuse_file_info *llfi,
3348 struct fuse_dh *dh = (
struct fuse_dh *) (uintptr_t) llfi->
fh;
3357 struct fuse *f = req_fuse_prepare(req);
3358 struct fuse_intr_data d;
3364 dh = (
struct fuse_dh *) malloc(
sizeof(
struct fuse_dh));
3366 reply_err(req, -ENOMEM);
3369 memset(dh, 0,
sizeof(
struct fuse_dh));
3371 dh->contents = NULL;
3376 fuse_mutex_init(&dh->lock);
3378 llfi->
fh = (uintptr_t) dh;
3380 memset(&fi, 0,
sizeof(fi));
3383 err = get_path(f, ino, &path);
3385 fuse_prepare_interrupt(f, req, &d);
3386 err = fuse_fs_opendir(f->fs, path, &fi);
3387 fuse_finish_interrupt(f, req, &d);
3394 fuse_fs_releasedir(f->fs, path, &fi);
3395 pthread_mutex_destroy(&dh->lock);
3399 reply_err(req, err);
3400 pthread_mutex_destroy(&dh->lock);
3403 free_path(f, ino, path);
3406 static int extend_contents(
struct fuse_dh *dh,
unsigned minsize)
3408 if (minsize > dh->size) {
3410 unsigned newsize = dh->size;
3413 while (newsize < minsize) {
3414 if (newsize >= 0x80000000)
3415 newsize = 0xffffffff;
3420 newptr = (
char *) realloc(dh->contents, newsize);
3422 dh->error = -ENOMEM;
3425 dh->contents = newptr;
3431 static int fuse_add_direntry_to_dh(
struct fuse_dh *dh,
const char *name,
3434 struct fuse_direntry *de;
3436 de = malloc(
sizeof(
struct fuse_direntry));
3438 dh->error = -ENOMEM;
3441 de->name = strdup(name);
3443 dh->error = -ENOMEM;
3451 dh->last = &de->next;
3462 pthread_mutex_lock(&f->lock);
3463 node = lookup_node(f, parent, name);
3466 pthread_mutex_unlock(&f->lock);
3471 static int fill_dir(
void *dh_,
const char *name,
const struct stat *statp,
3474 struct fuse_dh *dh = (
struct fuse_dh *) dh_;
3485 memset(&stbuf, 0,
sizeof(stbuf));
3486 stbuf.st_ino = FUSE_UNKNOWN_INO;
3489 if (!dh->fuse->conf.use_ino) {
3490 stbuf.st_ino = FUSE_UNKNOWN_INO;
3491 if (dh->fuse->conf.readdir_ino) {
3492 stbuf.st_ino = (ino_t)
3493 lookup_nodeid(dh->fuse, dh->nodeid, name);
3510 if (extend_contents(dh, dh->needlen) == -1)
3515 dh->needlen - dh->len, name,
3517 if (newlen > dh->needlen)
3524 if (fuse_add_direntry_to_dh(dh, name, &stbuf) == -1)
3530 static int is_dot_or_dotdot(
const char *name)
3532 return name[0] ==
'.' && (name[1] ==
'\0' ||
3533 (name[1] ==
'.' && name[2] ==
'\0'));
3536 static int fill_dir_plus(
void *dh_,
const char *name,
const struct stat *statp,
3539 struct fuse_dh *dh = (
struct fuse_dh *) dh_;
3544 struct fuse *f = dh->fuse;
3555 if (!is_dot_or_dotdot(name)) {
3556 res = do_lookup(f, dh->nodeid, name, &e);
3563 e.
attr.st_ino = FUSE_UNKNOWN_INO;
3564 if (!f->conf.use_ino && f->conf.readdir_ino) {
3565 e.
attr.st_ino = (ino_t)
3566 lookup_nodeid(f, dh->nodeid, name);
3582 if (extend_contents(dh, dh->needlen) == -1)
3587 dh->needlen - dh->len, name,
3589 if (newlen > dh->needlen)
3595 if (fuse_add_direntry_to_dh(dh, name, &e.
attr) == -1)
3602 static void free_direntries(
struct fuse_direntry *de)
3605 struct fuse_direntry *next = de->next;
3613 size_t size, off_t off,
struct fuse_dh *dh,
3620 if (f->fs->op.readdir)
3621 err = get_path_nullok(f, ino, &path);
3623 err = get_path(f, ino, &path);
3625 struct fuse_intr_data d;
3629 filler = fill_dir_plus;
3631 free_direntries(dh->first);
3633 dh->last = &dh->first;
3639 fuse_prepare_interrupt(f, req, &d);
3640 err = fuse_fs_readdir(f->fs, path, dh, filler, off, fi, flags);
3641 fuse_finish_interrupt(f, req, &d);
3647 free_path(f, ino, path);
3652 static int readdir_fill_from_list(
fuse_req_t req,
struct fuse_dh *dh,
3656 struct fuse_direntry *de = dh->first;
3660 if (extend_contents(dh, dh->needlen) == -1)
3663 for (pos = 0; pos < off; pos++) {
3670 char *p = dh->contents + dh->len;
3671 unsigned rem = dh->needlen - dh->len;
3676 if (flags & FUSE_READDIR_PLUS) {
3685 de->name, &de->stat, pos);
3687 newlen = dh->len + thislen;
3688 if (newlen > dh->needlen)
3700 struct fuse *f = req_fuse_prepare(req);
3702 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3705 pthread_mutex_lock(&dh->lock);
3712 err = readdir_fill(f, req, ino, size, off, dh, &fi, flags);
3714 reply_err(req, err);
3720 err = readdir_fill_from_list(req, dh, off, flags);
3722 reply_err(req, err);
3728 pthread_mutex_unlock(&dh->lock);
3734 fuse_readdir_common(req, ino, size, off, llfi, 0);
3740 fuse_readdir_common(req, ino, size, off, llfi, FUSE_READDIR_PLUS);
3746 struct fuse *f = req_fuse_prepare(req);
3747 struct fuse_intr_data d;
3749 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3752 get_path_nullok(f, ino, &path);
3754 fuse_prepare_interrupt(f, req, &d);
3755 fuse_fs_releasedir(f->fs, path, &fi);
3756 fuse_finish_interrupt(f, req, &d);
3757 free_path(f, ino, path);
3759 pthread_mutex_lock(&dh->lock);
3760 pthread_mutex_unlock(&dh->lock);
3761 pthread_mutex_destroy(&dh->lock);
3762 free_direntries(dh->first);
3771 struct fuse *f = req_fuse_prepare(req);
3776 get_dirhandle(llfi, &fi);
3778 err = get_path_nullok(f, ino, &path);
3780 struct fuse_intr_data d;
3781 fuse_prepare_interrupt(f, req, &d);
3782 err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi);
3783 fuse_finish_interrupt(f, req, &d);
3784 free_path(f, ino, path);
3786 reply_err(req, err);
3791 struct fuse *f = req_fuse_prepare(req);
3796 memset(&buf, 0,
sizeof(buf));
3798 err = get_path(f, ino, &path);
3801 struct fuse_intr_data d;
3802 fuse_prepare_interrupt(f, req, &d);
3803 err = fuse_fs_statfs(f->fs, path ? path :
"/", &buf);
3804 fuse_finish_interrupt(f, req, &d);
3805 free_path(f, ino, path);
3811 reply_err(req, err);
3815 const char *value,
size_t size,
int flags)
3817 struct fuse *f = req_fuse_prepare(req);
3821 err = get_path(f, ino, &path);
3823 struct fuse_intr_data d;
3824 fuse_prepare_interrupt(f, req, &d);
3825 err = fuse_fs_setxattr(f->fs, path, name, value, size, flags);
3826 fuse_finish_interrupt(f, req, &d);
3827 free_path(f, ino, path);
3829 reply_err(req, err);
3833 const char *name,
char *value,
size_t size)
3838 err = get_path(f, ino, &path);
3840 struct fuse_intr_data d;
3841 fuse_prepare_interrupt(f, req, &d);
3842 err = fuse_fs_getxattr(f->fs, path, name, value, size);
3843 fuse_finish_interrupt(f, req, &d);
3844 free_path(f, ino, path);
3852 struct fuse *f = req_fuse_prepare(req);
3856 char *value = (
char *) malloc(size);
3857 if (value == NULL) {
3858 reply_err(req, -ENOMEM);
3861 res = common_getxattr(f, req, ino, name, value, size);
3865 reply_err(req, res);
3868 res = common_getxattr(f, req, ino, name, NULL, 0);
3872 reply_err(req, res);
3877 char *list,
size_t size)
3882 err = get_path(f, ino, &path);
3884 struct fuse_intr_data d;
3885 fuse_prepare_interrupt(f, req, &d);
3886 err = fuse_fs_listxattr(f->fs, path, list, size);
3887 fuse_finish_interrupt(f, req, &d);
3888 free_path(f, ino, path);
3895 struct fuse *f = req_fuse_prepare(req);
3899 char *list = (
char *) malloc(size);
3901 reply_err(req, -ENOMEM);
3904 res = common_listxattr(f, req, ino, list, size);
3908 reply_err(req, res);
3911 res = common_listxattr(f, req, ino, NULL, 0);
3915 reply_err(req, res);
3922 struct fuse *f = req_fuse_prepare(req);
3926 err = get_path(f, ino, &path);
3928 struct fuse_intr_data d;
3929 fuse_prepare_interrupt(f, req, &d);
3930 err = fuse_fs_removexattr(f->fs, path, name);
3931 fuse_finish_interrupt(f, req, &d);
3932 free_path(f, ino, path);
3934 reply_err(req, err);
3937 static struct lock *locks_conflict(
struct node *node,
const struct lock *lock)
3941 for (l = node->locks; l; l = l->next)
3942 if (l->owner != lock->owner &&
3943 lock->start <= l->end && l->start <= lock->end &&
3944 (l->type == F_WRLCK || lock->type == F_WRLCK))
3950 static void delete_lock(
struct lock **lockp)
3952 struct lock *l = *lockp;
3957 static void insert_lock(
struct lock **pos,
struct lock *lock)
3963 static int locks_insert(
struct node *node,
struct lock *lock)
3966 struct lock *newl1 = NULL;
3967 struct lock *newl2 = NULL;
3969 if (lock->type != F_UNLCK || lock->start != 0 ||
3970 lock->end != OFFSET_MAX) {
3971 newl1 = malloc(
sizeof(
struct lock));
3972 newl2 = malloc(
sizeof(
struct lock));
3974 if (!newl1 || !newl2) {
3981 for (lp = &node->locks; *lp;) {
3982 struct lock *l = *lp;
3983 if (l->owner != lock->owner)
3986 if (lock->type == l->type) {
3987 if (l->end < lock->start - 1)
3989 if (lock->end < l->start - 1)
3991 if (l->start <= lock->start && lock->end <= l->end)
3993 if (l->start < lock->start)
3994 lock->start = l->start;
3995 if (lock->end < l->end)
3999 if (l->end < lock->start)
4001 if (lock->end < l->start)
4003 if (lock->start <= l->start && l->end <= lock->end)
4005 if (l->end <= lock->end) {
4006 l->end = lock->start - 1;
4009 if (lock->start <= l->start) {
4010 l->start = lock->end + 1;
4014 newl2->start = lock->end + 1;
4015 l->end = lock->start - 1;
4016 insert_lock(&l->next, newl2);
4026 if (lock->type != F_UNLCK) {
4028 insert_lock(lp, newl1);
4037 static void flock_to_lock(
struct flock *flock,
struct lock *lock)
4039 memset(lock, 0,
sizeof(
struct lock));
4040 lock->type = flock->l_type;
4041 lock->start = flock->l_start;
4043 flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
4044 lock->pid = flock->l_pid;
4047 static void lock_to_flock(
struct lock *lock,
struct flock *flock)
4049 flock->l_type = lock->type;
4050 flock->l_start = lock->start;
4052 (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
4053 flock->l_pid = lock->pid;
4059 struct fuse_intr_data d;
4065 fuse_prepare_interrupt(f, req, &d);
4066 memset(&lock, 0,
sizeof(lock));
4067 lock.l_type = F_UNLCK;
4068 lock.l_whence = SEEK_SET;
4069 err = fuse_fs_flush(f->fs, path, fi);
4070 errlock = fuse_fs_lock(f->fs, path, fi, F_SETLK, &lock);
4071 fuse_finish_interrupt(f, req, &d);
4073 if (errlock != -ENOSYS) {
4074 flock_to_lock(&lock, &l);
4076 pthread_mutex_lock(&f->lock);
4077 locks_insert(get_node(f, ino), &l);
4078 pthread_mutex_unlock(&f->lock);
4091 struct fuse *f = req_fuse_prepare(req);
4092 struct fuse_intr_data d;
4096 get_path_nullok(f, ino, &path);
4098 err = fuse_flush_common(f, req, ino, path, fi);
4103 fuse_prepare_interrupt(f, req, &d);
4104 fuse_do_release(f, ino, path, fi);
4105 fuse_finish_interrupt(f, req, &d);
4106 free_path(f, ino, path);
4108 reply_err(req, err);
4114 struct fuse *f = req_fuse_prepare(req);
4118 get_path_nullok(f, ino, &path);
4119 err = fuse_flush_common(f, req, ino, path, fi);
4120 free_path(f, ino, path);
4122 reply_err(req, err);
4129 struct fuse *f = req_fuse_prepare(req);
4133 err = get_path_nullok(f, ino, &path);
4135 struct fuse_intr_data d;
4136 fuse_prepare_interrupt(f, req, &d);
4137 err = fuse_fs_lock(f->fs, path, fi, cmd, lock);
4138 fuse_finish_interrupt(f, req, &d);
4139 free_path(f, ino, path);
4149 struct lock *conflict;
4150 struct fuse *f = req_fuse(req);
4152 flock_to_lock(lock, &l);
4154 pthread_mutex_lock(&f->lock);
4155 conflict = locks_conflict(get_node(f, ino), &l);
4157 lock_to_flock(conflict, lock);
4158 pthread_mutex_unlock(&f->lock);
4160 err = fuse_lock_common(req, ino, fi, lock, F_GETLK);
4167 reply_err(req, err);
4174 int err = fuse_lock_common(req, ino, fi, lock,
4175 sleep ? F_SETLKW : F_SETLK);
4177 struct fuse *f = req_fuse(req);
4179 flock_to_lock(lock, &l);
4181 pthread_mutex_lock(&f->lock);
4182 locks_insert(get_node(f, ino), &l);
4183 pthread_mutex_unlock(&f->lock);
4185 reply_err(req, err);
4191 struct fuse *f = req_fuse_prepare(req);
4195 err = get_path_nullok(f, ino, &path);
4197 struct fuse_intr_data d;
4198 fuse_prepare_interrupt(f, req, &d);
4199 err = fuse_fs_flock(f->fs, path, fi, op);
4200 fuse_finish_interrupt(f, req, &d);
4201 free_path(f, ino, path);
4203 reply_err(req, err);
4209 struct fuse *f = req_fuse_prepare(req);
4210 struct fuse_intr_data d;
4214 err = get_path(f, ino, &path);
4216 fuse_prepare_interrupt(f, req, &d);
4217 err = fuse_fs_bmap(f->fs, path, blocksize, &idx);
4218 fuse_finish_interrupt(f, req, &d);
4219 free_path(f, ino, path);
4224 reply_err(req, err);
4229 unsigned int flags,
const void *in_buf,
4230 size_t in_bufsz,
size_t out_bufsz)
4232 struct fuse *f = req_fuse_prepare(req);
4233 struct fuse_intr_data d;
4235 char *path, *out_buf = NULL;
4239 if (flags & FUSE_IOCTL_UNRESTRICTED)
4242 if (flags & FUSE_IOCTL_DIR)
4243 get_dirhandle(llfi, &fi);
4249 out_buf = malloc(out_bufsz);
4254 assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
4255 if (out_buf && in_bufsz)
4256 memcpy(out_buf, in_buf, in_bufsz);
4258 err = get_path_nullok(f, ino, &path);
4262 fuse_prepare_interrupt(f, req, &d);
4264 err = fuse_fs_ioctl(f->fs, path, cmd, arg, &fi, flags,
4265 out_buf ? out_buf : (
void *)in_buf);
4267 fuse_finish_interrupt(f, req, &d);
4268 free_path(f, ino, path);
4273 reply_err(req, err);
4281 struct fuse *f = req_fuse_prepare(req);
4282 struct fuse_intr_data d;
4285 unsigned revents = 0;
4287 err = get_path_nullok(f, ino, &path);
4289 fuse_prepare_interrupt(f, req, &d);
4290 err = fuse_fs_poll(f->fs, path, fi, ph, &revents);
4291 fuse_finish_interrupt(f, req, &d);
4292 free_path(f, ino, path);
4297 reply_err(req, err);
4303 struct fuse *f = req_fuse_prepare(req);
4304 struct fuse_intr_data d;
4308 err = get_path_nullok(f, ino, &path);
4310 fuse_prepare_interrupt(f, req, &d);
4311 err = fuse_fs_fallocate(f->fs, path, mode, offset, length, fi);
4312 fuse_finish_interrupt(f, req, &d);
4313 free_path(f, ino, path);
4315 reply_err(req, err);
4324 struct fuse *f = req_fuse_prepare(req);
4325 struct fuse_intr_data d;
4326 char *path_in, *path_out;
4330 err = get_path_nullok(f, nodeid_in, &path_in);
4332 reply_err(req, err);
4336 err = get_path_nullok(f, nodeid_out, &path_out);
4338 free_path(f, nodeid_in, path_in);
4339 reply_err(req, err);
4343 fuse_prepare_interrupt(f, req, &d);
4344 res = fuse_fs_copy_file_range(f->fs, path_in, fi_in, off_in, path_out,
4345 fi_out, off_out, len, flags);
4346 fuse_finish_interrupt(f, req, &d);
4351 reply_err(req, res);
4353 free_path(f, nodeid_in, path_in);
4354 free_path(f, nodeid_out, path_out);
4357 static int clean_delay(
struct fuse *f)
4365 int max_sleep = 3600;
4366 int sleep_time = f->conf.remember / 10;
4368 if (sleep_time > max_sleep)
4370 if (sleep_time < min_sleep)
4377 struct node_lru *lnode;
4378 struct list_head *curr, *next;
4380 struct timespec now;
4382 pthread_mutex_lock(&f->lock);
4386 for (curr = f->lru_table.next; curr != &f->lru_table; curr = next) {
4390 lnode = list_entry(curr,
struct node_lru, lru);
4391 node = &lnode->node;
4393 age = diff_timespec(&now, &lnode->forget_time);
4394 if (age <= f->conf.remember)
4397 assert(node->nlookup == 1);
4400 if (node->refctr > 1)
4404 unhash_name(f, node);
4405 unref_node(f, node);
4407 pthread_mutex_unlock(&f->lock);
4409 return clean_delay(f);
4413 .
init = fuse_lib_init,
4414 .destroy = fuse_lib_destroy,
4415 .lookup = fuse_lib_lookup,
4416 .forget = fuse_lib_forget,
4417 .forget_multi = fuse_lib_forget_multi,
4418 .getattr = fuse_lib_getattr,
4419 .setattr = fuse_lib_setattr,
4420 .access = fuse_lib_access,
4421 .readlink = fuse_lib_readlink,
4422 .mknod = fuse_lib_mknod,
4423 .mkdir = fuse_lib_mkdir,
4424 .unlink = fuse_lib_unlink,
4425 .rmdir = fuse_lib_rmdir,
4426 .symlink = fuse_lib_symlink,
4427 .rename = fuse_lib_rename,
4428 .link = fuse_lib_link,
4429 .create = fuse_lib_create,
4430 .open = fuse_lib_open,
4431 .read = fuse_lib_read,
4432 .write_buf = fuse_lib_write_buf,
4433 .flush = fuse_lib_flush,
4434 .release = fuse_lib_release,
4435 .fsync = fuse_lib_fsync,
4436 .opendir = fuse_lib_opendir,
4437 .readdir = fuse_lib_readdir,
4438 .readdirplus = fuse_lib_readdirplus,
4439 .releasedir = fuse_lib_releasedir,
4440 .fsyncdir = fuse_lib_fsyncdir,
4441 .statfs = fuse_lib_statfs,
4442 .setxattr = fuse_lib_setxattr,
4443 .getxattr = fuse_lib_getxattr,
4444 .listxattr = fuse_lib_listxattr,
4445 .removexattr = fuse_lib_removexattr,
4446 .getlk = fuse_lib_getlk,
4447 .setlk = fuse_lib_setlk,
4448 .flock = fuse_lib_flock,
4449 .bmap = fuse_lib_bmap,
4450 .ioctl = fuse_lib_ioctl,
4451 .poll = fuse_lib_poll,
4452 .fallocate = fuse_lib_fallocate,
4453 .copy_file_range = fuse_lib_copy_file_range,
4456 int fuse_notify_poll(
struct fuse_pollhandle *ph)
4466 static int fuse_session_loop_remember(
struct fuse *f)
4468 struct fuse_session *se = f->se;
4470 struct timespec now;
4472 struct pollfd fds = {
4481 next_clean = now.tv_sec;
4486 if (now.tv_sec < next_clean)
4487 timeout = next_clean - now.tv_sec;
4491 res = poll(&fds, 1, timeout * 1000);
4493 if (errno == -EINTR)
4497 }
else if (res > 0) {
4498 res = fuse_session_receive_buf_int(se, &fbuf, NULL);
4505 fuse_session_process_buf_int(se, &fbuf, NULL);
4509 next_clean = now.tv_sec + timeout;
4515 return res < 0 ? -1 : 0;
4524 return fuse_session_loop_remember(f);
4529 FUSE_SYMVER(
".symver fuse_loop_mt_32,fuse_loop_mt@@FUSE_3.2");
4545 FUSE_SYMVER(
".symver fuse_loop_mt_31,fuse_loop_mt@FUSE_3.0");
4551 return fuse_loop_mt_32(f, &config);
4561 struct fuse_context_i *c = fuse_get_context_internal();
4571 struct fuse_context_i *c = fuse_get_context_internal();
4580 struct fuse_context_i *c = fuse_get_context_internal();
4590 int err = lookup_path_in_cache(f, path, &ino);
4598 #define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v } 4600 static const struct fuse_opt fuse_lib_opts[] = {
4603 FUSE_LIB_OPT(
"debug", debug, 1),
4604 FUSE_LIB_OPT(
"-d", debug, 1),
4605 FUSE_LIB_OPT(
"kernel_cache", kernel_cache, 1),
4606 FUSE_LIB_OPT(
"auto_cache", auto_cache, 1),
4607 FUSE_LIB_OPT(
"noauto_cache", auto_cache, 0),
4608 FUSE_LIB_OPT(
"umask=", set_mode, 1),
4609 FUSE_LIB_OPT(
"umask=%o", umask, 0),
4610 FUSE_LIB_OPT(
"uid=", set_uid, 1),
4611 FUSE_LIB_OPT(
"uid=%d", uid, 0),
4612 FUSE_LIB_OPT(
"gid=", set_gid, 1),
4613 FUSE_LIB_OPT(
"gid=%d", gid, 0),
4614 FUSE_LIB_OPT(
"entry_timeout=%lf", entry_timeout, 0),
4615 FUSE_LIB_OPT(
"attr_timeout=%lf", attr_timeout, 0),
4616 FUSE_LIB_OPT(
"ac_attr_timeout=%lf", ac_attr_timeout, 0),
4617 FUSE_LIB_OPT(
"ac_attr_timeout=", ac_attr_timeout_set, 1),
4618 FUSE_LIB_OPT(
"negative_timeout=%lf", negative_timeout, 0),
4619 FUSE_LIB_OPT(
"noforget", remember, -1),
4620 FUSE_LIB_OPT(
"remember=%u", remember, 0),
4621 FUSE_LIB_OPT(
"modules=%s", modules, 0),
4625 static int fuse_lib_opt_proc(
void *data,
const char *arg,
int key,
4628 (void) arg; (void) outargs; (void) data; (void) key;
4635 static const struct fuse_opt fuse_help_opts[] = {
4636 FUSE_LIB_OPT(
"modules=%s", modules, 1),
4641 static void print_module_help(
const char *name,
4648 printf(
"\nOptions for %s module:\n", name);
4658 " -o kernel_cache cache files in kernel\n" 4659 " -o [no]auto_cache enable caching based on modification times (off)\n" 4660 " -o umask=M set file permissions (octal)\n" 4661 " -o uid=N set file owner\n" 4662 " -o gid=N set file group\n" 4663 " -o entry_timeout=T cache timeout for names (1.0s)\n" 4664 " -o negative_timeout=T cache timeout for deleted names (0.0s)\n" 4665 " -o attr_timeout=T cache timeout for attributes (1.0s)\n" 4666 " -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n" 4667 " -o noforget never forget cached inodes\n" 4668 " -o remember=T remember cached inodes for T seconds (0s)\n" 4669 " -o modules=M1[:M2...] names of modules to push onto filesystem stack\n");
4676 print_module_help(
"subdir", &fuse_module_subdir_factory);
4678 print_module_help(
"iconv", &fuse_module_iconv_factory);
4685 fuse_lib_opt_proc) == -1
4694 for (module = conf.modules; module; module = next) {
4696 for (p = module; *p && *p !=
':'; p++);
4697 next = *p ? p + 1 : NULL;
4700 m = fuse_get_module(module);
4702 print_module_help(module, &m->factory);
4708 static int fuse_init_intr_signal(
int signum,
int *installed)
4710 struct sigaction old_sa;
4712 if (sigaction(signum, NULL, &old_sa) == -1) {
4713 perror(
"fuse: cannot get old signal handler");
4717 if (old_sa.sa_handler == SIG_DFL) {
4718 struct sigaction sa;
4720 memset(&sa, 0,
sizeof(
struct sigaction));
4721 sa.sa_handler = fuse_intr_sighandler;
4722 sigemptyset(&sa.sa_mask);
4724 if (sigaction(signum, &sa, NULL) == -1) {
4725 perror(
"fuse: cannot set interrupt signal handler");
4733 static void fuse_restore_intr_signal(
int signum)
4735 struct sigaction sa;
4737 memset(&sa, 0,
sizeof(
struct sigaction));
4738 sa.sa_handler = SIG_DFL;
4739 sigaction(signum, &sa, NULL);
4743 static int fuse_push_module(
struct fuse *f,
const char *module,
4746 struct fuse_fs *fs[2] = { f->fs, NULL };
4747 struct fuse_fs *newfs;
4753 newfs = m->factory(args, fs);
4769 fuse_log(FUSE_LOG_ERR,
"fuse: warning: library too old, some operations may not not work\n");
4773 fs = (
struct fuse_fs *) calloc(1,
sizeof(
struct fuse_fs));
4775 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate fuse_fs object\n");
4779 fs->user_data = user_data;
4781 memcpy(&fs->op, op, op_size);
4785 static int node_table_init(
struct node_table *t)
4787 t->size = NODE_TABLE_MIN_SIZE;
4788 t->array = (
struct node **) calloc(1,
sizeof(
struct node *) * t->size);
4789 if (t->array == NULL) {
4790 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
4799 static void *fuse_prune_nodes(
void *fuse)
4801 struct fuse *f = fuse;
4814 return fuse_start_thread(&f->prune_thread, fuse_prune_nodes, f);
4821 if (lru_enabled(f)) {
4822 pthread_mutex_lock(&f->lock);
4823 pthread_cancel(f->prune_thread);
4824 pthread_mutex_unlock(&f->lock);
4825 pthread_join(f->prune_thread, NULL);
4830 FUSE_SYMVER(
".symver fuse_new_31,fuse_new@@FUSE_3.1");
4831 struct fuse *fuse_new_31(
struct fuse_args *args,
4833 size_t op_size,
void *user_data)
4840 f = (
struct fuse *) calloc(1,
sizeof(
struct fuse));
4842 fuse_log(FUSE_LOG_ERR,
"fuse: failed to allocate fuse object\n");
4846 f->conf.entry_timeout = 1.0;
4847 f->conf.attr_timeout = 1.0;
4848 f->conf.negative_timeout = 0.0;
4849 f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL;
4853 fuse_lib_opt_proc) == -1)
4856 pthread_mutex_lock(&fuse_context_lock);
4857 static int builtin_modules_registered = 0;
4859 if (builtin_modules_registered == 0) {
4861 fuse_register_module(
"subdir", fuse_module_subdir_factory, NULL);
4863 fuse_register_module(
"iconv", fuse_module_iconv_factory, NULL);
4865 builtin_modules_registered= 1;
4867 pthread_mutex_unlock(&fuse_context_lock);
4869 if (fuse_create_context_key() == -1)
4874 goto out_delete_context_key;
4884 f->pagesize = getpagesize();
4885 init_list_head(&f->partial_slabs);
4886 init_list_head(&f->full_slabs);
4887 init_list_head(&f->lru_table);
4889 if (f->conf.modules) {
4893 for (module = f->conf.modules; module; module = next) {
4895 for (p = module; *p && *p !=
':'; p++);
4896 next = *p ? p + 1 : NULL;
4899 fuse_push_module(f, module, args) == -1)
4904 if (!f->conf.ac_attr_timeout_set)
4905 f->conf.ac_attr_timeout = f->conf.attr_timeout;
4907 #if defined(__FreeBSD__) || defined(__NetBSD__) 4912 f->conf.readdir_ino = 1;
4919 if (f->conf.debug) {
4920 fuse_log(FUSE_LOG_DEBUG,
"nullpath_ok: %i\n", f->conf.nullpath_ok);
4924 f->fs->debug = f->conf.debug;
4927 if (node_table_init(&f->name_table) == -1)
4928 goto out_free_session;
4930 if (node_table_init(&f->id_table) == -1)
4931 goto out_free_name_table;
4933 fuse_mutex_init(&f->lock);
4935 root = alloc_node(f);
4937 fuse_log(FUSE_LOG_ERR,
"fuse: memory allocation failed\n");
4938 goto out_free_id_table;
4940 if (lru_enabled(f)) {
4941 struct node_lru *lnode = node_lru(root);
4942 init_list_head(&lnode->lru);
4945 strcpy(root->inline_name,
"/");
4946 root->name = root->inline_name;
4949 fuse_init_intr_signal(f->conf.intr_signal,
4950 &f->intr_installed) == -1)
4953 root->parent = NULL;
4963 free(f->id_table.array);
4964 out_free_name_table:
4965 free(f->name_table.array);
4970 fuse_put_module(f->fs->m);
4972 free(f->conf.modules);
4973 out_delete_context_key:
4974 fuse_delete_context_key();
4983 size_t op_size,
void *private_data);
4984 FUSE_SYMVER(
".symver fuse_new_30,fuse_new@FUSE_3.0");
4985 struct fuse *fuse_new_30(
struct fuse_args *args,
4987 size_t op_size,
void *user_data)
4991 memset(&conf, 0,
sizeof(conf));
4994 FUSE_LIB_OPT(
"-h", show_help, 1),
4995 FUSE_LIB_OPT(
"--help", show_help, 1),
5000 fuse_lib_opt_proc) == -1)
5007 return fuse_new_31(args, op, op_size, user_data);
5014 if (f->conf.intr && f->intr_installed)
5015 fuse_restore_intr_signal(f->conf.intr_signal);
5018 fuse_create_context(f);
5020 for (i = 0; i < f->id_table.size; i++) {
5023 for (node = f->id_table.array[i]; node != NULL;
5024 node = node->id_next) {
5025 if (node->is_hidden) {
5027 if (try_get_path(f, node->nodeid, NULL, &path, NULL,
false) == 0) {
5028 fuse_fs_unlink(f->fs, path);
5035 for (i = 0; i < f->id_table.size; i++) {
5039 for (node = f->id_table.array[i]; node != NULL; node = next) {
5040 next = node->id_next;
5045 assert(list_empty(&f->partial_slabs));
5046 assert(list_empty(&f->full_slabs));
5048 while (fuse_modules) {
5049 fuse_put_module(fuse_modules);
5051 free(f->id_table.array);
5052 free(f->name_table.array);
5053 pthread_mutex_destroy(&f->lock);
5055 free(f->conf.modules);
5057 fuse_delete_context_key();
5060 int fuse_mount(
struct fuse *f,
const char *mountpoint) {
5071 return FUSE_VERSION;
5076 return PACKAGE_VERSION;
const struct fuse_ctx * fuse_req_ctx(fuse_req_t req)
#define FUSE_OPT_KEY_KEEP
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
#define FUSE_CAP_EXPORT_SUPPORT
void fuse_session_unmount(struct fuse_session *se)
int fuse_getgroups(int size, gid_t list[])
int fuse_loop_mt_31(struct fuse *f, int clone_fd)
void fuse_stop_cleanup_thread(struct fuse *fuse)
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
unsigned int max_idle_threads
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
void fuse_unmount(struct fuse *f)
int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
int fuse_loop(struct fuse *f)
int fuse_req_interrupted(fuse_req_t req)
int fuse_reply_poll(fuse_req_t req, unsigned revents)
int(* fuse_fill_dir_t)(void *buf, const char *name, const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
const char * fuse_pkgversion(void)
int fuse_reply_err(fuse_req_t req, int err)
void fuse_session_reset(struct fuse_session *se)
struct fuse_req * fuse_req_t
int fuse_clean_cache(struct fuse *fuse)
void * fuse_req_userdata(fuse_req_t req)
#define FUSE_ARGS_INIT(argc, argv)
struct fuse_session * fuse_get_session(struct fuse *f)
size_t fuse_buf_size(const struct fuse_bufvec *bufv)
void fuse_reply_none(fuse_req_t req)
#define FUSE_CAP_FLOCK_LOCKS
#define FUSE_CAP_POSIX_LOCKS
struct fuse_fs *(* fuse_module_factory_t)(struct fuse_args *args, struct fuse_fs *fs[])
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_destroy(struct fuse_session *se)
void fuse_session_exit(struct fuse_session *se)
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_reply_readlink(fuse_req_t req, const char *link)
int fuse_start_cleanup_thread(struct fuse *fuse)
void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, void *data)
int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
enum fuse_buf_flags flags
struct fuse_fs * fuse_fs_new(const struct fuse_operations *op, size_t op_size, void *private_data)
int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, const struct fuse_file_info *fi)
void fuse_lowlevel_help(void)
int fuse_interrupted(void)
void(* getlk)(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, struct flock *lock)
int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
void fuse_destroy(struct fuse *f)
int fuse_mount(struct fuse *f, const char *mountpoint)
void(* setlk)(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, struct flock *lock, int sleep)
int fuse_invalidate_path(struct fuse *f, const char *path)
int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
void fuse_log(enum fuse_log_level level, const char *fmt,...)
int fuse_session_exited(struct fuse_session *se)
int fuse_reply_xattr(fuse_req_t req, size_t count)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
int fuse_reply_write(fuse_req_t req, size_t count)
#define FUSE_CAP_SPLICE_READ
void(* init)(void *userdata, struct fuse_conn_info *conn)
void fuse_exit(struct fuse *f)
int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
struct fuse_context * fuse_get_context(void)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
void fuse_lib_help(struct fuse_args *args)
#define FUSE_OPT_KEY(templ, key)