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 fprintf(stderr,
"fuse: failed to allocate module\n");
220 mod->name = strdup(name);
222 fprintf(stderr,
"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 fprintf(stderr,
"fuse: memory allocation failed\n");
262 sprintf(tmp,
"libfusemod_%s.so", module);
263 so = calloc(1,
sizeof(
struct fusemod_so));
265 fprintf(stderr,
"fuse: failed to allocate module so\n");
269 so->handle = dlopen(tmp, RTLD_NOW);
270 if (so->handle == NULL) {
271 fprintf(stderr,
"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 fprintf(stderr,
"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 fprintf(stderr,
"fuse warning: munmap(%p) failed\n", slab);
487 static void free_node_mem(
struct fuse *f,
struct node *node)
489 struct node_slab *slab = node_to_slab(f, node);
490 struct list_head *n = (
struct list_head *) node;
494 if (list_empty(&slab->freelist)) {
495 list_del(&slab->list);
496 list_add_tail(&slab->list, &f->partial_slabs);
498 list_add_head(n, &slab->freelist);
504 static struct node *alloc_node(
struct fuse *f)
506 return (
struct node *) calloc(1, get_node_size(f));
509 static void free_node_mem(
struct fuse *f,
struct node *node)
516 static size_t id_hash(
struct fuse *f,
fuse_ino_t ino)
518 uint64_t hash = ((uint32_t) ino * 2654435761U) % f->id_table.size;
519 uint64_t oldhash = hash % (f->id_table.size / 2);
521 if (oldhash >= f->id_table.split)
527 static struct node *get_node_nocheck(
struct fuse *f,
fuse_ino_t nodeid)
529 size_t hash = id_hash(f, nodeid);
532 for (node = f->id_table.array[hash]; node != NULL; node = node->id_next)
533 if (node->nodeid == nodeid)
539 static struct node *get_node(
struct fuse *f,
fuse_ino_t nodeid)
541 struct node *node = get_node_nocheck(f, nodeid);
543 fprintf(stderr,
"fuse internal error: node %llu not found\n",
544 (
unsigned long long) nodeid);
550 static void curr_time(
struct timespec *now);
551 static double diff_timespec(
const struct timespec *t1,
552 const struct timespec *t2);
554 static void remove_node_lru(
struct node *node)
556 struct node_lru *lnode = node_lru(node);
557 list_del(&lnode->lru);
558 init_list_head(&lnode->lru);
561 static void set_forget_time(
struct fuse *f,
struct node *node)
563 struct node_lru *lnode = node_lru(node);
565 list_del(&lnode->lru);
566 list_add_tail(&lnode->lru, &f->lru_table);
567 curr_time(&lnode->forget_time);
570 static void free_node(
struct fuse *f,
struct node *node)
572 if (node->name != node->inline_name)
574 free_node_mem(f, node);
577 static void node_table_reduce(
struct node_table *t)
579 size_t newsize = t->size / 2;
582 if (newsize < NODE_TABLE_MIN_SIZE)
585 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
586 if (newarray != NULL)
590 t->split = t->size / 2;
593 static void remerge_id(
struct fuse *f)
595 struct node_table *t = &f->id_table;
599 node_table_reduce(t);
601 for (iter = 8; t->split > 0 && iter; iter--) {
605 upper = &t->array[t->split + t->size / 2];
609 for (nodep = &t->array[t->split]; *nodep;
610 nodep = &(*nodep)->id_next);
619 static void unhash_id(
struct fuse *f,
struct node *node)
621 struct node **nodep = &f->id_table.array[id_hash(f, node->nodeid)];
623 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
624 if (*nodep == node) {
625 *nodep = node->id_next;
628 if(f->id_table.use < f->id_table.size / 4)
634 static int node_table_resize(
struct node_table *t)
636 size_t newsize = t->size * 2;
639 newarray = realloc(t->array,
sizeof(
struct node *) * newsize);
640 if (newarray == NULL)
644 memset(t->array + t->size, 0, t->size *
sizeof(
struct node *));
651 static void rehash_id(
struct fuse *f)
653 struct node_table *t = &f->id_table;
658 if (t->split == t->size / 2)
663 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
664 struct node *node = *nodep;
665 size_t newhash = id_hash(f, node->nodeid);
667 if (newhash != hash) {
669 *nodep = node->id_next;
670 node->id_next = t->array[newhash];
671 t->array[newhash] = node;
673 next = &node->id_next;
676 if (t->split == t->size / 2)
677 node_table_resize(t);
680 static void hash_id(
struct fuse *f,
struct node *node)
682 size_t hash = id_hash(f, node->nodeid);
683 node->id_next = f->id_table.array[hash];
684 f->id_table.array[hash] = node;
687 if (f->id_table.use >= f->id_table.size / 2)
691 static size_t name_hash(
struct fuse *f,
fuse_ino_t parent,
694 uint64_t hash = parent;
697 for (; *name; name++)
698 hash = hash * 31 + (
unsigned char) *name;
700 hash %= f->name_table.size;
701 oldhash = hash % (f->name_table.size / 2);
702 if (oldhash >= f->name_table.split)
708 static void unref_node(
struct fuse *f,
struct node *node);
710 static void remerge_name(
struct fuse *f)
712 struct node_table *t = &f->name_table;
716 node_table_reduce(t);
718 for (iter = 8; t->split > 0 && iter; iter--) {
722 upper = &t->array[t->split + t->size / 2];
726 for (nodep = &t->array[t->split]; *nodep;
727 nodep = &(*nodep)->name_next);
736 static void unhash_name(
struct fuse *f,
struct node *node)
739 size_t hash = name_hash(f, node->parent->nodeid, node->name);
740 struct node **nodep = &f->name_table.array[hash];
742 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
743 if (*nodep == node) {
744 *nodep = node->name_next;
745 node->name_next = NULL;
746 unref_node(f, node->parent);
747 if (node->name != node->inline_name)
753 if (f->name_table.use < f->name_table.size / 4)
758 "fuse internal error: unable to unhash node: %llu\n",
759 (
unsigned long long) node->nodeid);
764 static void rehash_name(
struct fuse *f)
766 struct node_table *t = &f->name_table;
771 if (t->split == t->size / 2)
776 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
777 struct node *node = *nodep;
778 size_t newhash = name_hash(f, node->parent->nodeid, node->name);
780 if (newhash != hash) {
782 *nodep = node->name_next;
783 node->name_next = t->array[newhash];
784 t->array[newhash] = node;
786 next = &node->name_next;
789 if (t->split == t->size / 2)
790 node_table_resize(t);
793 static int hash_name(
struct fuse *f,
struct node *node,
fuse_ino_t parentid,
796 size_t hash = name_hash(f, parentid, name);
797 struct node *parent = get_node(f, parentid);
798 if (strlen(name) <
sizeof(node->inline_name)) {
799 strcpy(node->inline_name, name);
800 node->name = node->inline_name;
802 node->name = strdup(name);
803 if (node->name == NULL)
808 node->parent = parent;
809 node->name_next = f->name_table.array[hash];
810 f->name_table.array[hash] = node;
813 if (f->name_table.use >= f->name_table.size / 2)
819 static void delete_node(
struct fuse *f,
struct node *node)
822 fprintf(stderr,
"DELETE: %llu\n",
823 (
unsigned long long) node->nodeid);
825 assert(node->treelock == 0);
826 unhash_name(f, node);
828 remove_node_lru(node);
833 static void unref_node(
struct fuse *f,
struct node *node)
835 assert(node->refctr > 0);
838 delete_node(f, node);
844 f->ctr = (f->ctr + 1) & 0xffffffff;
847 }
while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO ||
848 get_node_nocheck(f, f->ctr) != NULL);
852 static struct node *lookup_node(
struct fuse *f,
fuse_ino_t parent,
855 size_t hash = name_hash(f, parent, name);
858 for (node = f->name_table.array[hash]; node != NULL; node = node->name_next)
859 if (node->parent->nodeid == parent &&
860 strcmp(node->name, name) == 0)
866 static void inc_nlookup(
struct node *node)
873 static struct node *find_node(
struct fuse *f,
fuse_ino_t parent,
878 pthread_mutex_lock(&f->lock);
880 node = get_node(f, parent);
882 node = lookup_node(f, parent, name);
884 node = alloc_node(f);
888 node->nodeid = next_id(f);
889 node->generation = f->generation;
890 if (f->conf.remember)
893 if (hash_name(f, node, parent, name) == -1) {
899 if (lru_enabled(f)) {
900 struct node_lru *lnode = node_lru(node);
901 init_list_head(&lnode->lru);
903 }
else if (lru_enabled(f) && node->nlookup == 1) {
904 remove_node_lru(node);
908 pthread_mutex_unlock(&f->lock);
912 static int lookup_path_in_cache(
struct fuse *f,
915 char *tmp = strdup(path);
919 pthread_mutex_lock(&f->lock);
924 char *path_element = strtok_r(tmp,
"/", &save_ptr);
925 while (path_element != NULL) {
926 struct node *node = lookup_node(f, ino, path_element);
932 path_element = strtok_r(NULL,
"/", &save_ptr);
934 pthread_mutex_unlock(&f->lock);
942 static char *add_name(
char **buf,
unsigned *bufsize,
char *s,
const char *name)
944 size_t len = strlen(name);
946 if (s - len <= *buf) {
947 unsigned pathlen = *bufsize - (s - *buf);
948 unsigned newbufsize = *bufsize;
951 while (newbufsize < pathlen + len + 1) {
952 if (newbufsize >= 0x80000000)
953 newbufsize = 0xffffffff;
958 newbuf = realloc(*buf, newbufsize);
963 s = newbuf + newbufsize - pathlen;
964 memmove(s, newbuf + *bufsize - pathlen, pathlen);
965 *bufsize = newbufsize;
968 strncpy(s, name, len);
975 static void unlock_path(
struct fuse *f,
fuse_ino_t nodeid,
struct node *wnode,
981 assert(wnode->treelock == TREELOCK_WRITE);
985 for (node = get_node(f, nodeid);
986 node != end && node->nodeid !=
FUSE_ROOT_ID; node = node->parent) {
987 assert(node->treelock != 0);
988 assert(node->treelock != TREELOCK_WAIT_OFFSET);
989 assert(node->treelock != TREELOCK_WRITE);
991 if (node->treelock == TREELOCK_WAIT_OFFSET)
996 static int try_get_path(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
997 char **path,
struct node **wnodep,
bool need_lock)
999 unsigned bufsize = 256;
1003 struct node *wnode = NULL;
1009 buf = malloc(bufsize);
1013 s = buf + bufsize - 1;
1017 s = add_name(&buf, &bufsize, s, name);
1025 wnode = lookup_node(f, nodeid, name);
1027 if (wnode->treelock != 0) {
1028 if (wnode->treelock > 0)
1029 wnode->treelock += TREELOCK_WAIT_OFFSET;
1033 wnode->treelock = TREELOCK_WRITE;
1037 for (node = get_node(f, nodeid); node->nodeid !=
FUSE_ROOT_ID;
1038 node = node->parent) {
1040 if (node->name == NULL || node->parent == NULL)
1044 s = add_name(&buf, &bufsize, s, node->name);
1050 if (node->treelock < 0)
1058 memmove(buf, s, bufsize - (s - buf));
1070 unlock_path(f, nodeid, wnode, node);
1078 static void queue_element_unlock(
struct fuse *f,
struct lock_queue_element *qe)
1082 if (qe->first_locked) {
1083 wnode = qe->wnode1 ? *qe->wnode1 : NULL;
1084 unlock_path(f, qe->nodeid1, wnode, NULL);
1085 qe->first_locked =
false;
1087 if (qe->second_locked) {
1088 wnode = qe->wnode2 ? *qe->wnode2 : NULL;
1089 unlock_path(f, qe->nodeid2, wnode, NULL);
1090 qe->second_locked =
false;
1094 static void queue_element_wakeup(
struct fuse *f,
struct lock_queue_element *qe)
1097 bool first = (qe == f->lockq);
1101 if (get_node(f, qe->nodeid1)->treelock == 0)
1102 pthread_cond_signal(&qe->cond);
1107 if (!qe->first_locked) {
1108 err = try_get_path(f, qe->nodeid1, qe->name1, qe->path1,
1111 qe->first_locked =
true;
1112 else if (err != -EAGAIN)
1115 if (!qe->second_locked && qe->path2) {
1116 err = try_get_path(f, qe->nodeid2, qe->name2, qe->path2,
1119 qe->second_locked =
true;
1120 else if (err != -EAGAIN)
1124 if (qe->first_locked && (qe->second_locked || !qe->path2)) {
1137 queue_element_unlock(f, qe);
1143 queue_element_unlock(f, qe);
1147 pthread_cond_signal(&qe->cond);
1150 static void wake_up_queued(
struct fuse *f)
1152 struct lock_queue_element *qe;
1154 for (qe = f->lockq; qe != NULL; qe = qe->next)
1155 queue_element_wakeup(f, qe);
1158 static void debug_path(
struct fuse *f,
const char *msg,
fuse_ino_t nodeid,
1159 const char *name,
bool wr)
1161 if (f->conf.debug) {
1162 struct node *wnode = NULL;
1165 wnode = lookup_node(f, nodeid, name);
1168 fprintf(stderr,
"%s %llu (w)\n",
1169 msg, (
unsigned long long) wnode->nodeid);
1171 fprintf(stderr,
"%s %llu\n",
1172 msg, (
unsigned long long) nodeid);
1177 static void queue_path(
struct fuse *f,
struct lock_queue_element *qe)
1179 struct lock_queue_element **qp;
1182 qe->first_locked =
false;
1183 qe->second_locked =
false;
1184 pthread_cond_init(&qe->cond, NULL);
1186 for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
1190 static void dequeue_path(
struct fuse *f,
struct lock_queue_element *qe)
1192 struct lock_queue_element **qp;
1194 pthread_cond_destroy(&qe->cond);
1195 for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
1199 static int wait_path(
struct fuse *f,
struct lock_queue_element *qe)
1204 pthread_cond_wait(&qe->cond, &f->lock);
1205 }
while (!qe->done);
1207 dequeue_path(f, qe);
1212 static int get_path_common(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1213 char **path,
struct node **wnode)
1217 pthread_mutex_lock(&f->lock);
1218 err = try_get_path(f, nodeid, name, path, wnode,
true);
1219 if (err == -EAGAIN) {
1220 struct lock_queue_element qe = {
1226 debug_path(f,
"QUEUE PATH", nodeid, name, !!wnode);
1227 err = wait_path(f, &qe);
1228 debug_path(f,
"DEQUEUE PATH", nodeid, name, !!wnode);
1230 pthread_mutex_unlock(&f->lock);
1235 static int get_path(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1237 return get_path_common(f, nodeid, NULL, path, NULL);
1240 static int get_path_nullok(
struct fuse *f,
fuse_ino_t nodeid,
char **path)
1244 if (f->conf.nullpath_ok) {
1247 err = get_path_common(f, nodeid, NULL, path, NULL);
1255 static int get_path_name(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1258 return get_path_common(f, nodeid, name, path, NULL);
1261 static int get_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
const char *name,
1262 char **path,
struct node **wnode)
1264 return get_path_common(f, nodeid, name, path, wnode);
1267 #if defined(__FreeBSD__) 1268 #define CHECK_DIR_LOOP 1271 #if defined(CHECK_DIR_LOOP) 1272 static int check_dir_loop(
struct fuse *f,
1276 struct node *node, *node1, *node2;
1279 node1 = lookup_node(f, nodeid1, name1);
1280 id1 = node1 ? node1->nodeid : nodeid1;
1282 node2 = lookup_node(f, nodeid2, name2);
1283 id2 = node2 ? node2->nodeid : nodeid2;
1285 for (node = get_node(f, id2); node->nodeid !=
FUSE_ROOT_ID;
1286 node = node->parent) {
1287 if (node->name == NULL || node->parent == NULL)
1290 if (node->nodeid != id2 && node->nodeid == id1)
1296 for (node = get_node(f, id1); node->nodeid !=
FUSE_ROOT_ID;
1297 node = node->parent) {
1298 if (node->name == NULL || node->parent == NULL)
1301 if (node->nodeid != id1 && node->nodeid == id2)
1310 static int try_get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1312 char **path1,
char **path2,
1313 struct node **wnode1,
struct node **wnode2)
1318 err = try_get_path(f, nodeid1, name1, path1, wnode1,
true);
1320 err = try_get_path(f, nodeid2, name2, path2, wnode2,
true);
1322 struct node *wn1 = wnode1 ? *wnode1 : NULL;
1324 unlock_path(f, nodeid1, wn1, NULL);
1331 static int get_path2(
struct fuse *f,
fuse_ino_t nodeid1,
const char *name1,
1333 char **path1,
char **path2,
1334 struct node **wnode1,
struct node **wnode2)
1338 pthread_mutex_lock(&f->lock);
1340 #if defined(CHECK_DIR_LOOP) 1344 err = check_dir_loop(f, nodeid1, name1, nodeid2, name2);
1350 err = try_get_path2(f, nodeid1, name1, nodeid2, name2,
1351 path1, path2, wnode1, wnode2);
1352 if (err == -EAGAIN) {
1353 struct lock_queue_element qe = {
1364 debug_path(f,
"QUEUE PATH1", nodeid1, name1, !!wnode1);
1365 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1366 err = wait_path(f, &qe);
1367 debug_path(f,
"DEQUEUE PATH1", nodeid1, name1, !!wnode1);
1368 debug_path(f,
" PATH2", nodeid2, name2, !!wnode2);
1371 #if defined(CHECK_DIR_LOOP) 1374 pthread_mutex_unlock(&f->lock);
1379 static void free_path_wrlock(
struct fuse *f,
fuse_ino_t nodeid,
1380 struct node *wnode,
char *path)
1382 pthread_mutex_lock(&f->lock);
1383 unlock_path(f, nodeid, wnode, NULL);
1386 pthread_mutex_unlock(&f->lock);
1390 static void free_path(
struct fuse *f,
fuse_ino_t nodeid,
char *path)
1393 free_path_wrlock(f, nodeid, NULL, path);
1397 struct node *wnode1,
struct node *wnode2,
1398 char *path1,
char *path2)
1400 pthread_mutex_lock(&f->lock);
1401 unlock_path(f, nodeid1, wnode1, NULL);
1402 unlock_path(f, nodeid2, wnode2, NULL);
1404 pthread_mutex_unlock(&f->lock);
1409 static void forget_node(
struct fuse *f,
fuse_ino_t nodeid, uint64_t nlookup)
1414 pthread_mutex_lock(&f->lock);
1415 node = get_node(f, nodeid);
1421 while (node->nlookup == nlookup && node->treelock) {
1422 struct lock_queue_element qe = {
1426 debug_path(f,
"QUEUE PATH (forget)", nodeid, NULL,
false);
1430 pthread_cond_wait(&qe.cond, &f->lock);
1431 }
while (node->nlookup == nlookup && node->treelock);
1433 dequeue_path(f, &qe);
1434 debug_path(f,
"DEQUEUE_PATH (forget)", nodeid, NULL,
false);
1437 assert(node->nlookup >= nlookup);
1438 node->nlookup -= nlookup;
1439 if (!node->nlookup) {
1440 unref_node(f, node);
1441 }
else if (lru_enabled(f) && node->nlookup == 1) {
1442 set_forget_time(f, node);
1444 pthread_mutex_unlock(&f->lock);
1447 static void unlink_node(
struct fuse *f,
struct node *node)
1449 if (f->conf.remember) {
1450 assert(node->nlookup > 1);
1453 unhash_name(f, node);
1456 static void remove_node(
struct fuse *f,
fuse_ino_t dir,
const char *name)
1460 pthread_mutex_lock(&f->lock);
1461 node = lookup_node(f, dir, name);
1463 unlink_node(f, node);
1464 pthread_mutex_unlock(&f->lock);
1467 static int rename_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1468 fuse_ino_t newdir,
const char *newname,
int hide)
1471 struct node *newnode;
1474 pthread_mutex_lock(&f->lock);
1475 node = lookup_node(f, olddir, oldname);
1476 newnode = lookup_node(f, newdir, newname);
1480 if (newnode != NULL) {
1482 fprintf(stderr,
"fuse: hidden file got created during hiding\n");
1486 unlink_node(f, newnode);
1489 unhash_name(f, node);
1490 if (hash_name(f, node, newdir, newname) == -1) {
1496 node->is_hidden = 1;
1499 pthread_mutex_unlock(&f->lock);
1503 static int exchange_node(
struct fuse *f,
fuse_ino_t olddir,
const char *oldname,
1506 struct node *oldnode;
1507 struct node *newnode;
1510 pthread_mutex_lock(&f->lock);
1511 oldnode = lookup_node(f, olddir, oldname);
1512 newnode = lookup_node(f, newdir, newname);
1515 unhash_name(f, oldnode);
1517 unhash_name(f, newnode);
1521 if (hash_name(f, oldnode, newdir, newname) == -1)
1525 if (hash_name(f, newnode, olddir, oldname) == -1)
1530 pthread_mutex_unlock(&f->lock);
1534 static void set_stat(
struct fuse *f,
fuse_ino_t nodeid,
struct stat *stbuf)
1536 if (!f->conf.use_ino)
1537 stbuf->st_ino = nodeid;
1538 if (f->conf.set_mode)
1539 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1540 (0777 & ~f->conf.umask);
1541 if (f->conf.set_uid)
1542 stbuf->st_uid = f->conf.uid;
1543 if (f->conf.set_gid)
1544 stbuf->st_gid = f->conf.gid;
1552 static void fuse_intr_sighandler(
int sig)
1558 struct fuse_intr_data {
1560 pthread_cond_t cond;
1564 static void fuse_interrupt(
fuse_req_t req,
void *d_)
1566 struct fuse_intr_data *d = d_;
1567 struct fuse *f = req_fuse(req);
1569 if (d->id == pthread_self())
1572 pthread_mutex_lock(&f->lock);
1573 while (!d->finished) {
1575 struct timespec timeout;
1577 pthread_kill(d->id, f->conf.intr_signal);
1578 gettimeofday(&now, NULL);
1579 timeout.tv_sec = now.tv_sec + 1;
1580 timeout.tv_nsec = now.tv_usec * 1000;
1581 pthread_cond_timedwait(&d->cond, &f->lock, &timeout);
1583 pthread_mutex_unlock(&f->lock);
1586 static void fuse_do_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1587 struct fuse_intr_data *d)
1589 pthread_mutex_lock(&f->lock);
1591 pthread_cond_broadcast(&d->cond);
1592 pthread_mutex_unlock(&f->lock);
1594 pthread_cond_destroy(&d->cond);
1597 static void fuse_do_prepare_interrupt(
fuse_req_t req,
struct fuse_intr_data *d)
1599 d->id = pthread_self();
1600 pthread_cond_init(&d->cond, NULL);
1605 static inline void fuse_finish_interrupt(
struct fuse *f,
fuse_req_t req,
1606 struct fuse_intr_data *d)
1609 fuse_do_finish_interrupt(f, req, d);
1612 static inline void fuse_prepare_interrupt(
struct fuse *f,
fuse_req_t req,
1613 struct fuse_intr_data *d)
1616 fuse_do_prepare_interrupt(req, d);
1620 char* buf,
size_t len)
1624 snprintf(buf, len,
"%llu", (
unsigned long long) fi->
fh);
1628 int fuse_fs_getattr(
struct fuse_fs *fs,
const char *path,
struct stat *buf,
1632 if (fs->op.getattr) {
1635 fprintf(stderr,
"getattr[%s] %s\n",
1636 file_info_string(fi, buf,
sizeof(buf)),
1639 return fs->op.getattr(path, buf, fi);
1645 int fuse_fs_rename(
struct fuse_fs *fs,
const char *oldpath,
1646 const char *newpath,
unsigned int flags)
1649 if (fs->op.rename) {
1651 fprintf(stderr,
"rename %s %s 0x%x\n", oldpath, newpath,
1654 return fs->op.rename(oldpath, newpath, flags);
1660 int fuse_fs_unlink(
struct fuse_fs *fs,
const char *path)
1663 if (fs->op.unlink) {
1665 fprintf(stderr,
"unlink %s\n", path);
1667 return fs->op.unlink(path);
1673 int fuse_fs_rmdir(
struct fuse_fs *fs,
const char *path)
1678 fprintf(stderr,
"rmdir %s\n", path);
1680 return fs->op.rmdir(path);
1686 int fuse_fs_symlink(
struct fuse_fs *fs,
const char *linkname,
const char *path)
1689 if (fs->op.symlink) {
1691 fprintf(stderr,
"symlink %s %s\n", linkname, path);
1693 return fs->op.symlink(linkname, path);
1699 int fuse_fs_link(
struct fuse_fs *fs,
const char *oldpath,
const char *newpath)
1704 fprintf(stderr,
"link %s %s\n", oldpath, newpath);
1706 return fs->op.link(oldpath, newpath);
1712 int fuse_fs_release(
struct fuse_fs *fs,
const char *path,
1716 if (fs->op.release) {
1718 fprintf(stderr,
"release%s[%llu] flags: 0x%x\n",
1719 fi->
flush ?
"+flush" :
"",
1720 (
unsigned long long) fi->
fh, fi->
flags);
1722 return fs->op.release(path, fi);
1728 int fuse_fs_opendir(
struct fuse_fs *fs,
const char *path,
1732 if (fs->op.opendir) {
1736 fprintf(stderr,
"opendir flags: 0x%x %s\n", fi->
flags,
1739 err = fs->op.opendir(path, fi);
1741 if (fs->debug && !err)
1742 fprintf(stderr,
" opendir[%llu] flags: 0x%x %s\n",
1743 (
unsigned long long) fi->
fh, fi->
flags, path);
1751 int fuse_fs_open(
struct fuse_fs *fs,
const char *path,
1759 fprintf(stderr,
"open flags: 0x%x %s\n", fi->
flags,
1762 err = fs->op.open(path, fi);
1764 if (fs->debug && !err)
1765 fprintf(stderr,
" open[%llu] flags: 0x%x %s\n",
1766 (
unsigned long long) fi->
fh, fi->
flags, path);
1774 static void fuse_free_buf(
struct fuse_bufvec *buf)
1779 for (i = 0; i < buf->
count; i++)
1786 int fuse_fs_read_buf(
struct fuse_fs *fs,
const char *path,
1787 struct fuse_bufvec **bufp,
size_t size, off_t off,
1791 if (fs->op.read || fs->op.read_buf) {
1796 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1797 (
unsigned long long) fi->
fh,
1798 size, (
unsigned long long) off, fi->
flags);
1800 if (fs->op.read_buf) {
1801 res = fs->op.read_buf(path, bufp, size, off, fi);
1815 *buf = FUSE_BUFVEC_INIT(size);
1819 res = fs->op.read(path, mem, size, off, fi);
1824 if (fs->debug && res >= 0)
1825 fprintf(stderr,
" read[%llu] %zu bytes from %llu\n",
1826 (
unsigned long long) fi->
fh,
1828 (
unsigned long long) off);
1830 fprintf(stderr,
"fuse: read too many bytes\n");
1841 int fuse_fs_read(
struct fuse_fs *fs,
const char *path,
char *mem,
size_t size,
1845 if (fs->op.read || fs->op.read_buf) {
1850 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1851 (
unsigned long long) fi->
fh,
1852 size, (
unsigned long long) off, fi->
flags);
1854 if (fs->op.read_buf) {
1857 res = fs->op.read_buf(path, &buf, size, off, fi);
1866 res = fs->op.read(path, mem, size, off, fi);
1869 if (fs->debug && res >= 0)
1870 fprintf(stderr,
" read[%llu] %u bytes from %llu\n",
1871 (
unsigned long long) fi->
fh,
1873 (
unsigned long long) off);
1874 if (res >= 0 && res > (
int) size)
1875 fprintf(stderr,
"fuse: read too many bytes\n");
1883 int fuse_fs_write_buf(
struct fuse_fs *fs,
const char *path,
1888 if (fs->op.write_buf || fs->op.write) {
1892 assert(buf->
idx == 0 && buf->
off == 0);
1895 "write%s[%llu] %zu bytes to %llu flags: 0x%x\n",
1897 (
unsigned long long) fi->
fh,
1899 (
unsigned long long) off,
1902 if (fs->op.write_buf) {
1903 res = fs->op.write_buf(path, buf, off, fi);
1909 if (buf->
count == 1 &&
1911 flatbuf = &buf->
buf[0];
1924 flatbuf = &tmp.
buf[0];
1927 res = fs->op.write(path, flatbuf->
mem, flatbuf->
size,
1933 if (fs->debug && res >= 0)
1934 fprintf(stderr,
" write%s[%llu] %u bytes to %llu\n",
1936 (
unsigned long long) fi->
fh, res,
1937 (
unsigned long long) off);
1938 if (res > (
int) size)
1939 fprintf(stderr,
"fuse: wrote too many bytes\n");
1947 int fuse_fs_write(
struct fuse_fs *fs,
const char *path,
const char *mem,
1952 bufv.
buf[0].
mem = (
void *) mem;
1954 return fuse_fs_write_buf(fs, path, &bufv, off, fi);
1957 int fuse_fs_fsync(
struct fuse_fs *fs,
const char *path,
int datasync,
1963 fprintf(stderr,
"fsync[%llu] datasync: %i\n",
1964 (
unsigned long long) fi->
fh, datasync);
1966 return fs->op.fsync(path, datasync, fi);
1972 int fuse_fs_fsyncdir(
struct fuse_fs *fs,
const char *path,
int datasync,
1976 if (fs->op.fsyncdir) {
1978 fprintf(stderr,
"fsyncdir[%llu] datasync: %i\n",
1979 (
unsigned long long) fi->
fh, datasync);
1981 return fs->op.fsyncdir(path, datasync, fi);
1987 int fuse_fs_flush(
struct fuse_fs *fs,
const char *path,
1993 fprintf(stderr,
"flush[%llu]\n",
1994 (
unsigned long long) fi->
fh);
1996 return fs->op.flush(path, fi);
2002 int fuse_fs_statfs(
struct fuse_fs *fs,
const char *path,
struct statvfs *buf)
2005 if (fs->op.statfs) {
2007 fprintf(stderr,
"statfs %s\n", path);
2009 return fs->op.statfs(path, buf);
2011 buf->f_namemax = 255;
2017 int fuse_fs_releasedir(
struct fuse_fs *fs,
const char *path,
2021 if (fs->op.releasedir) {
2023 fprintf(stderr,
"releasedir[%llu] flags: 0x%x\n",
2024 (
unsigned long long) fi->
fh, fi->
flags);
2026 return fs->op.releasedir(path, fi);
2032 int fuse_fs_readdir(
struct fuse_fs *fs,
const char *path,
void *buf,
2038 if (fs->op.readdir) {
2040 fprintf(stderr,
"readdir%s[%llu] from %llu\n",
2042 (
unsigned long long) fi->
fh,
2043 (
unsigned long long) off);
2046 return fs->op.readdir(path, buf, filler, off, fi, flags);
2052 int fuse_fs_create(
struct fuse_fs *fs,
const char *path, mode_t mode,
2056 if (fs->op.create) {
2061 "create flags: 0x%x %s 0%o umask=0%03o\n",
2062 fi->
flags, path, mode,
2065 err = fs->op.create(path, mode, fi);
2067 if (fs->debug && !err)
2068 fprintf(stderr,
" create[%llu] flags: 0x%x %s\n",
2069 (
unsigned long long) fi->
fh, fi->
flags, path);
2077 int fuse_fs_lock(
struct fuse_fs *fs,
const char *path,
2083 fprintf(stderr,
"lock[%llu] %s %s start: %llu len: %llu pid: %llu\n",
2084 (
unsigned long long) fi->
fh,
2085 (cmd == F_GETLK ?
"F_GETLK" :
2086 (cmd == F_SETLK ?
"F_SETLK" :
2087 (cmd == F_SETLKW ?
"F_SETLKW" :
"???"))),
2088 (lock->l_type == F_RDLCK ?
"F_RDLCK" :
2089 (lock->l_type == F_WRLCK ?
"F_WRLCK" :
2090 (lock->l_type == F_UNLCK ?
"F_UNLCK" :
2092 (
unsigned long long) lock->l_start,
2093 (
unsigned long long) lock->l_len,
2094 (
unsigned long long) lock->l_pid);
2096 return fs->op.lock(path, fi, cmd, lock);
2102 int fuse_fs_flock(
struct fuse_fs *fs,
const char *path,
2108 int xop = op & ~LOCK_NB;
2110 fprintf(stderr,
"lock[%llu] %s%s\n",
2111 (
unsigned long long) fi->
fh,
2112 xop == LOCK_SH ?
"LOCK_SH" :
2113 (xop == LOCK_EX ?
"LOCK_EX" :
2114 (xop == LOCK_UN ?
"LOCK_UN" :
"???")),
2115 (op & LOCK_NB) ?
"|LOCK_NB" :
"");
2117 return fs->op.flock(path, fi, op);
2123 int fuse_fs_chown(
struct fuse_fs *fs,
const char *path, uid_t uid,
2130 fprintf(stderr,
"chown[%s] %s %lu %lu\n",
2131 file_info_string(fi, buf,
sizeof(buf)),
2132 path, (
unsigned long) uid, (
unsigned long) gid);
2134 return fs->op.chown(path, uid, gid, fi);
2140 int fuse_fs_truncate(
struct fuse_fs *fs,
const char *path, off_t size,
2144 if (fs->op.truncate) {
2147 fprintf(stderr,
"truncate[%s] %llu\n",
2148 file_info_string(fi, buf,
sizeof(buf)),
2149 (
unsigned long long) size);
2151 return fs->op.truncate(path, size, fi);
2157 int fuse_fs_utimens(
struct fuse_fs *fs,
const char *path,
2161 if (fs->op.utimens) {
2164 fprintf(stderr,
"utimens[%s] %s %li.%09lu %li.%09lu\n",
2165 file_info_string(fi, buf,
sizeof(buf)),
2166 path, tv[0].tv_sec, tv[0].tv_nsec,
2167 tv[1].tv_sec, tv[1].tv_nsec);
2169 return fs->op.utimens(path, tv, fi);
2175 int fuse_fs_access(
struct fuse_fs *fs,
const char *path,
int mask)
2178 if (fs->op.access) {
2180 fprintf(stderr,
"access %s 0%o\n", path, mask);
2182 return fs->op.access(path, mask);
2188 int fuse_fs_readlink(
struct fuse_fs *fs,
const char *path,
char *buf,
2192 if (fs->op.readlink) {
2194 fprintf(stderr,
"readlink %s %lu\n", path,
2195 (
unsigned long) len);
2197 return fs->op.readlink(path, buf, len);
2203 int fuse_fs_mknod(
struct fuse_fs *fs,
const char *path, mode_t mode,
2209 fprintf(stderr,
"mknod %s 0%o 0x%llx umask=0%03o\n",
2210 path, mode, (
unsigned long long) rdev,
2213 return fs->op.mknod(path, mode, rdev);
2219 int fuse_fs_mkdir(
struct fuse_fs *fs,
const char *path, mode_t mode)
2224 fprintf(stderr,
"mkdir %s 0%o umask=0%03o\n",
2227 return fs->op.mkdir(path, mode);
2233 int fuse_fs_setxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2234 const char *value,
size_t size,
int flags)
2237 if (fs->op.setxattr) {
2239 fprintf(stderr,
"setxattr %s %s %lu 0x%x\n",
2240 path, name, (
unsigned long) size, flags);
2242 return fs->op.setxattr(path, name, value, size, flags);
2248 int fuse_fs_getxattr(
struct fuse_fs *fs,
const char *path,
const char *name,
2249 char *value,
size_t size)
2252 if (fs->op.getxattr) {
2254 fprintf(stderr,
"getxattr %s %s %lu\n",
2255 path, name, (
unsigned long) size);
2257 return fs->op.getxattr(path, name, value, size);
2263 int fuse_fs_listxattr(
struct fuse_fs *fs,
const char *path,
char *list,
2267 if (fs->op.listxattr) {
2269 fprintf(stderr,
"listxattr %s %lu\n",
2270 path, (
unsigned long) size);
2272 return fs->op.listxattr(path, list, size);
2278 int fuse_fs_bmap(
struct fuse_fs *fs,
const char *path,
size_t blocksize,
2284 fprintf(stderr,
"bmap %s blocksize: %lu index: %llu\n",
2285 path, (
unsigned long) blocksize,
2286 (
unsigned long long) *idx);
2288 return fs->op.bmap(path, blocksize, idx);
2294 int fuse_fs_removexattr(
struct fuse_fs *fs,
const char *path,
const char *name)
2297 if (fs->op.removexattr) {
2299 fprintf(stderr,
"removexattr %s %s\n", path, name);
2301 return fs->op.removexattr(path, name);
2307 int fuse_fs_ioctl(
struct fuse_fs *fs,
const char *path,
int cmd,
void *arg,
2313 fprintf(stderr,
"ioctl[%llu] 0x%x flags: 0x%x\n",
2314 (
unsigned long long) fi->
fh, cmd, flags);
2316 return fs->op.ioctl(path, cmd, arg, fi, flags, data);
2321 int fuse_fs_poll(
struct fuse_fs *fs,
const char *path,
2330 fprintf(stderr,
"poll[%llu] ph: %p, events 0x%x\n",
2331 (
unsigned long long) fi->
fh, ph,
2334 res = fs->op.poll(path, fi, ph, reventsp);
2336 if (fs->debug && !res)
2337 fprintf(stderr,
" poll[%llu] revents: 0x%x\n",
2338 (
unsigned long long) fi->
fh, *reventsp);
2345 int fuse_fs_fallocate(
struct fuse_fs *fs,
const char *path,
int mode,
2349 if (fs->op.fallocate) {
2351 fprintf(stderr,
"fallocate %s mode %x, offset: %llu, length: %llu\n",
2354 (
unsigned long long) offset,
2355 (
unsigned long long) length);
2357 return fs->op.fallocate(path, mode, offset, length, fi);
2362 ssize_t fuse_fs_copy_file_range(
struct fuse_fs *fs,
const char *path_in,
2364 const char *path_out,
2366 size_t len,
int flags)
2369 if (fs->op.copy_file_range) {
2371 fprintf(stderr,
"copy_file_range from %s:%llu to " 2372 "%s:%llu, length: %llu\n",
2374 (
unsigned long long) off_in,
2376 (
unsigned long long) off_out,
2377 (
unsigned long long) len);
2379 return fs->op.copy_file_range(path_in, fi_in, off_in, path_out,
2380 fi_out, off_out, len, flags);
2385 static int is_open(
struct fuse *f,
fuse_ino_t dir,
const char *name)
2389 pthread_mutex_lock(&f->lock);
2390 node = lookup_node(f, dir, name);
2391 if (node && node->open_count > 0)
2393 pthread_mutex_unlock(&f->lock);
2397 static char *hidden_name(
struct fuse *f,
fuse_ino_t dir,
const char *oldname,
2398 char *newname,
size_t bufsize)
2402 struct node *newnode;
2408 pthread_mutex_lock(&f->lock);
2409 node = lookup_node(f, dir, oldname);
2411 pthread_mutex_unlock(&f->lock);
2416 snprintf(newname, bufsize,
".fuse_hidden%08x%08x",
2417 (
unsigned int) node->nodeid, f->hidectr);
2418 newnode = lookup_node(f, dir, newname);
2421 res = try_get_path(f, dir, newname, &newpath, NULL,
false);
2422 pthread_mutex_unlock(&f->lock);
2426 memset(&buf, 0,
sizeof(buf));
2427 res = fuse_fs_getattr(f->fs, newpath, &buf, NULL);
2432 }
while(res == 0 && --failctr);
2437 static int hide_node(
struct fuse *f,
const char *oldpath,
2444 newpath = hidden_name(f, dir, oldname, newname,
sizeof(newname));
2446 err = fuse_fs_rename(f->fs, oldpath, newpath, 0);
2448 err = rename_node(f, dir, oldname, dir, newname, 1);
2454 static int mtime_eq(
const struct stat *stbuf,
const