2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
10 /* For pthread_rwlock_t */
14 #include "fuse_lowlevel.h"
16 #include "fuse_misc.h"
17 #include "fuse_common_compat.h"
18 #include "fuse_compat.h"
19 #include "fuse_kernel.h"
34 #include <sys/param.h>
39 #define FUSE_NODE_SLAB 1
45 #define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1
47 #define FUSE_UNKNOWN_INO 0xffffffff
48 #define OFFSET_MAX 0x7fffffffffffffffLL
50 #define NODE_TABLE_MIN_SIZE 8192
57 double negative_timeout;
59 double ac_attr_timeout;
60 int ac_attr_timeout_set;
80 struct fuse_operations op;
81 struct fuse_module *m;
92 struct lock_queue_element {
93 struct lock_queue_element *next;
104 #define container_of(ptr, type, member) ({ \
105 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
106 (type *)( (char *)__mptr - offsetof(type,member) );})
108 #define list_entry(ptr, type, member) \
109 container_of(ptr, type, member)
112 struct list_head *next;
113 struct list_head *prev;
117 struct list_head list; /* must be the first member */
118 struct list_head freelist;
123 struct fuse_session *se;
124 struct node_table name_table;
125 struct node_table id_table;
126 struct list_head lru_table;
128 unsigned int generation;
129 unsigned int hidectr;
130 pthread_mutex_t lock;
131 struct fuse_config conf;
137 struct lock_queue_element *lockq;
139 struct list_head partial_slabs;
140 struct list_head full_slabs;
141 pthread_t prune_thread;
154 struct node *name_next;
155 struct node *id_next;
157 unsigned int generation;
163 struct timespec stat_updated;
164 struct timespec mtime;
167 unsigned int is_hidden : 1;
168 unsigned int cache_valid : 1;
171 char inline_name[32];
176 struct list_head lru;
177 struct timespec forget_time;
181 pthread_mutex_t lock;
196 struct fuse_dirhandle {
197 fuse_fill_dir_t filler;
201 struct fuse_context_i {
202 struct fuse_context ctx;
206 static pthread_key_t fuse_context_key;
207 static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER;
208 static int fuse_context_ref;
209 static struct fusemod_so *fuse_current_so;
210 static struct fuse_module *fuse_modules;
212 static int fuse_load_so_name(const char *soname)
214 struct fusemod_so *so;
216 so = calloc(1, sizeof(struct fusemod_so));
218 fprintf(stderr, "fuse: memory allocation failed\n");
222 fuse_current_so = so;
223 so->handle = dlopen(soname, RTLD_NOW);
224 fuse_current_so = NULL;
226 fprintf(stderr, "fuse: %s\n", dlerror());
230 fprintf(stderr, "fuse: %s did not register any modules\n",
243 static int fuse_load_so_module(const char *module)
246 char *soname = malloc(strlen(module) + 64);
248 fprintf(stderr, "fuse: memory allocation failed\n");
251 sprintf(soname, "libfusemod_%s.so", module);
252 res = fuse_load_so_name(soname);
257 static struct fuse_module *fuse_find_module(const char *module)
259 struct fuse_module *m;
260 for (m = fuse_modules; m; m = m->next) {
261 if (strcmp(module, m->name) == 0) {
269 static struct fuse_module *fuse_get_module(const char *module)
271 struct fuse_module *m;
273 pthread_mutex_lock(&fuse_context_lock);
274 m = fuse_find_module(module);
276 int err = fuse_load_so_module(module);
278 m = fuse_find_module(module);
280 pthread_mutex_unlock(&fuse_context_lock);
284 static void fuse_put_module(struct fuse_module *m)
286 pthread_mutex_lock(&fuse_context_lock);
289 if (!m->ctr && m->so) {
290 struct fusemod_so *so = m->so;
294 struct fuse_module **mp;
295 for (mp = &fuse_modules; *mp;) {
305 pthread_mutex_unlock(&fuse_context_lock);
308 static void init_list_head(struct list_head *list)
314 static int list_empty(const struct list_head *head)
316 return head->next == head;
319 static void list_add(struct list_head *new, struct list_head *prev,
320 struct list_head *next)
328 static void list_add_head(struct list_head *new, struct list_head *head)
330 list_add(new, head, head->next);
333 static void list_add_tail(struct list_head *new, struct list_head *head)
335 list_add(new, head->prev, head);
338 static inline void list_del(struct list_head *entry)
340 struct list_head *prev = entry->prev;
341 struct list_head *next = entry->next;
347 static inline int lru_enabled(struct fuse *f)
349 return f->conf.remember > 0;
352 static struct node_lru *node_lru(struct node *node)
354 return (struct node_lru *) node;
357 static size_t get_node_size(struct fuse *f)
360 return sizeof(struct node_lru);
362 return sizeof(struct node);
365 #ifdef FUSE_NODE_SLAB
366 static struct node_slab *list_to_slab(struct list_head *head)
368 return (struct node_slab *) head;
371 static struct node_slab *node_to_slab(struct fuse *f, struct node *node)
373 return (struct node_slab *) (((uintptr_t) node) & ~((uintptr_t) f->pagesize - 1));
376 static int alloc_slab(struct fuse *f)
379 struct node_slab *slab;
383 size_t node_size = get_node_size(f);
385 mem = mmap(NULL, f->pagesize, PROT_READ | PROT_WRITE,
386 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
388 if (mem == MAP_FAILED)
392 init_list_head(&slab->freelist);
394 num = (f->pagesize - sizeof(struct node_slab)) / node_size;
396 start = (char *) mem + f->pagesize - num * node_size;
397 for (i = 0; i < num; i++) {
400 n = (struct list_head *) (start + i * node_size);
401 list_add_tail(n, &slab->freelist);
403 list_add_tail(&slab->list, &f->partial_slabs);
408 static struct node *alloc_node(struct fuse *f)
410 struct node_slab *slab;
411 struct list_head *node;
413 if (list_empty(&f->partial_slabs)) {
414 int res = alloc_slab(f);
418 slab = list_to_slab(f->partial_slabs.next);
420 node = slab->freelist.next;
422 if (list_empty(&slab->freelist)) {
423 list_del(&slab->list);
424 list_add_tail(&slab->list, &f->full_slabs);
427 return (struct node *) node;
430 static void free_slab(struct fuse *f, struct node_slab *slab)
434 list_del(&slab->list);
435 res = munmap(slab, f->pagesize);
437 fprintf(stderr, "fuse warning: munmap(%p) failed\n", slab);
440 static void free_node_mem(struct fuse *f, struct node *node)
442 struct node_slab *slab = node_to_slab(f, node);
443 struct list_head *n = (struct list_head *) node;
447 if (list_empty(&slab->freelist)) {
448 list_del(&slab->list);
449 list_add_tail(&slab->list, &f->partial_slabs);
451 list_add_head(n, &slab->freelist);
457 static struct node *alloc_node(struct fuse *f)
459 return (struct node *) calloc(1, get_node_size(f));
462 static void free_node_mem(struct fuse *f, struct node *node)
469 static size_t id_hash(struct fuse *f, fuse_ino_t ino)
471 uint64_t hash = ((uint32_t) ino * 2654435761U) % f->id_table.size;
472 uint64_t oldhash = hash % (f->id_table.size / 2);
474 if (oldhash >= f->id_table.split)
480 static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid)
482 size_t hash = id_hash(f, nodeid);
485 for (node = f->id_table.array[hash]; node != NULL; node = node->id_next)
486 if (node->nodeid == nodeid)
492 static struct node *get_node(struct fuse *f, fuse_ino_t nodeid)
494 struct node *node = get_node_nocheck(f, nodeid);
496 fprintf(stderr, "fuse internal error: node %llu not found\n",
497 (unsigned long long) nodeid);
503 static void curr_time(struct timespec *now);
504 static double diff_timespec(const struct timespec *t1,
505 const struct timespec *t2);
507 static void remove_node_lru(struct node *node)
509 struct node_lru *lnode = node_lru(node);
510 list_del(&lnode->lru);
511 init_list_head(&lnode->lru);
514 static void set_forget_time(struct fuse *f, struct node *node)
516 struct node_lru *lnode = node_lru(node);
518 list_del(&lnode->lru);
519 list_add_tail(&lnode->lru, &f->lru_table);
520 curr_time(&lnode->forget_time);
523 static void free_node(struct fuse *f, struct node *node)
525 if (node->name != node->inline_name)
527 free_node_mem(f, node);
530 static void node_table_reduce(struct node_table *t)
532 size_t newsize = t->size / 2;
535 if (newsize < NODE_TABLE_MIN_SIZE)
538 newarray = realloc(t->array, sizeof(struct node *) * newsize);
539 if (newarray != NULL)
543 t->split = t->size / 2;
546 static void remerge_id(struct fuse *f)
548 struct node_table *t = &f->id_table;
552 node_table_reduce(t);
554 for (iter = 8; t->split > 0 && iter; iter--) {
558 upper = &t->array[t->split + t->size / 2];
562 for (nodep = &t->array[t->split]; *nodep;
563 nodep = &(*nodep)->id_next);
572 static void unhash_id(struct fuse *f, struct node *node)
574 struct node **nodep = &f->id_table.array[id_hash(f, node->nodeid)];
576 for (; *nodep != NULL; nodep = &(*nodep)->id_next)
577 if (*nodep == node) {
578 *nodep = node->id_next;
581 if(f->id_table.use < f->id_table.size / 4)
587 static int node_table_resize(struct node_table *t)
589 size_t newsize = t->size * 2;
592 newarray = realloc(t->array, sizeof(struct node *) * newsize);
593 if (newarray == NULL)
597 memset(t->array + t->size, 0, t->size * sizeof(struct node *));
604 static void rehash_id(struct fuse *f)
606 struct node_table *t = &f->id_table;
611 if (t->split == t->size / 2)
616 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
617 struct node *node = *nodep;
618 size_t newhash = id_hash(f, node->nodeid);
620 if (newhash != hash) {
622 *nodep = node->id_next;
623 node->id_next = t->array[newhash];
624 t->array[newhash] = node;
626 next = &node->id_next;
629 if (t->split == t->size / 2)
630 node_table_resize(t);
633 static void hash_id(struct fuse *f, struct node *node)
635 size_t hash = id_hash(f, node->nodeid);
636 node->id_next = f->id_table.array[hash];
637 f->id_table.array[hash] = node;
640 if (f->id_table.use >= f->id_table.size / 2)
644 static size_t name_hash(struct fuse *f, fuse_ino_t parent,
647 uint64_t hash = parent;
650 for (; *name; name++)
651 hash = hash * 31 + (unsigned char) *name;
653 hash %= f->name_table.size;
654 oldhash = hash % (f->name_table.size / 2);
655 if (oldhash >= f->name_table.split)
661 static void unref_node(struct fuse *f, struct node *node);
663 static void remerge_name(struct fuse *f)
665 struct node_table *t = &f->name_table;
669 node_table_reduce(t);
671 for (iter = 8; t->split > 0 && iter; iter--) {
675 upper = &t->array[t->split + t->size / 2];
679 for (nodep = &t->array[t->split]; *nodep;
680 nodep = &(*nodep)->name_next);
689 static void unhash_name(struct fuse *f, struct node *node)
692 size_t hash = name_hash(f, node->parent->nodeid, node->name);
693 struct node **nodep = &f->name_table.array[hash];
695 for (; *nodep != NULL; nodep = &(*nodep)->name_next)
696 if (*nodep == node) {
697 *nodep = node->name_next;
698 node->name_next = NULL;
699 unref_node(f, node->parent);
700 if (node->name != node->inline_name)
706 if (f->name_table.use < f->name_table.size / 4)
711 "fuse internal error: unable to unhash node: %llu\n",
712 (unsigned long long) node->nodeid);
717 static void rehash_name(struct fuse *f)
719 struct node_table *t = &f->name_table;
724 if (t->split == t->size / 2)
729 for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) {
730 struct node *node = *nodep;
731 size_t newhash = name_hash(f, node->parent->nodeid, node->name);
733 if (newhash != hash) {
735 *nodep = node->name_next;
736 node->name_next = t->array[newhash];
737 t->array[newhash] = node;
739 next = &node->name_next;
742 if (t->split == t->size / 2)
743 node_table_resize(t);
746 static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parentid,
749 size_t hash = name_hash(f, parentid, name);
750 struct node *parent = get_node(f, parentid);
751 if (strlen(name) < sizeof(node->inline_name)) {
752 strcpy(node->inline_name, name);
753 node->name = node->inline_name;
755 node->name = strdup(name);
756 if (node->name == NULL)
761 node->parent = parent;
762 node->name_next = f->name_table.array[hash];
763 f->name_table.array[hash] = node;
766 if (f->name_table.use >= f->name_table.size / 2)
772 static void delete_node(struct fuse *f, struct node *node)
775 fprintf(stderr, "DELETE: %llu\n",
776 (unsigned long long) node->nodeid);
778 assert(node->treelock == 0);
779 unhash_name(f, node);
781 remove_node_lru(node);
786 static void unref_node(struct fuse *f, struct node *node)
788 assert(node->refctr > 0);
791 delete_node(f, node);
794 static fuse_ino_t next_id(struct fuse *f)
797 f->ctr = (f->ctr + 1) & 0xffffffff;
800 } while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO ||
801 get_node_nocheck(f, f->ctr) != NULL);
805 static struct node *lookup_node(struct fuse *f, fuse_ino_t parent,
808 size_t hash = name_hash(f, parent, name);
811 for (node = f->name_table.array[hash]; node != NULL; node = node->name_next)
812 if (node->parent->nodeid == parent &&
813 strcmp(node->name, name) == 0)
819 static struct node *find_node(struct fuse *f, fuse_ino_t parent,
824 pthread_mutex_lock(&f->lock);
826 node = get_node(f, parent);
828 node = lookup_node(f, parent, name);
830 node = alloc_node(f);
834 if (f->conf.remember)
837 node->nodeid = next_id(f);
838 node->generation = f->generation;
839 node->open_count = 0;
843 if (hash_name(f, node, parent, name) == -1) {
849 if (lru_enabled(f)) {
850 struct node_lru *lnode = node_lru(node);
851 init_list_head(&lnode->lru);
853 } else if (lru_enabled(f) && node->nlookup == 1) {
854 remove_node_lru(node);
858 pthread_mutex_unlock(&f->lock);
862 static char *add_name(char **buf, unsigned *bufsize, char *s, const char *name)
864 size_t len = strlen(name);
866 if (s - len <= *buf) {
867 unsigned pathlen = *bufsize - (s - *buf);
868 unsigned newbufsize = *bufsize;
871 while (newbufsize < pathlen + len + 1) {
872 if (newbufsize >= 0x80000000)
873 newbufsize = 0xffffffff;
878 newbuf = realloc(*buf, newbufsize);
883 s = newbuf + newbufsize - pathlen;
884 memmove(s, newbuf + *bufsize - pathlen, pathlen);
885 *bufsize = newbufsize;
888 strncpy(s, name, len);
895 static void unlock_path(struct fuse *f, fuse_ino_t nodeid, struct node *wnode,
896 struct node *end, int ticket)
901 assert(wnode->treelock == -1);
904 wnode->ticket = ticket;
907 for (node = get_node(f, nodeid);
908 node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent) {
909 assert(node->treelock > 0);
912 node->ticket = ticket;
916 static void release_tickets(struct fuse *f, fuse_ino_t nodeid,
917 struct node *wnode, int ticket)
922 if (wnode->ticket != ticket)
928 for (node = get_node(f, nodeid);
929 node->nodeid != FUSE_ROOT_ID; node = node->parent) {
930 if (node->ticket != ticket)
936 static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name,
937 char **path, struct node **wnodep, int ticket)
939 unsigned bufsize = 256;
943 struct node *wnode = NULL;
949 buf = malloc(bufsize);
953 s = buf + bufsize - 1;
957 s = add_name(&buf, &bufsize, s, name);
965 wnode = lookup_node(f, nodeid, name);
967 if (wnode->treelock != 0 ||
968 (wnode->ticket && wnode->ticket != ticket)) {
970 wnode->ticket = ticket;
974 wnode->treelock = -1;
979 for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID;
980 node = node->parent) {
982 if (node->name == NULL || node->parent == NULL)
986 s = add_name(&buf, &bufsize, s, node->name);
992 if (node->treelock == -1 ||
993 (node->ticket && node->ticket != ticket))
1002 memmove(buf, s, bufsize - (s - buf));
1014 unlock_path(f, nodeid, wnode, node, ticket);
1019 if (ticket && err != -EAGAIN)
1020 release_tickets(f, nodeid, wnode, ticket);
1025 static void wake_up_first(struct fuse *f)
1028 pthread_cond_signal(&f->lockq->cond);
1031 static void wake_up_next(struct lock_queue_element *qe)
1034 pthread_cond_signal(&qe->next->cond);
1037 static int get_ticket(struct fuse *f)
1039 do f->curr_ticket++;
1040 while (f->curr_ticket == 0);
1042 return f->curr_ticket;
1045 static void debug_path(struct fuse *f, const char *msg, fuse_ino_t nodeid,
1046 const char *name, int wr)
1048 if (f->conf.debug) {
1049 struct node *wnode = NULL;
1052 wnode = lookup_node(f, nodeid, name);
1055 fprintf(stderr, "%s %li (w)\n", msg, wnode->nodeid);
1057 fprintf(stderr, "%s %li\n", msg, nodeid);
1061 static void queue_path(struct fuse *f, struct lock_queue_element *qe,
1062 fuse_ino_t nodeid, const char *name, int wr)
1064 struct lock_queue_element **qp;
1066 debug_path(f, "QUEUE PATH", nodeid, name, wr);
1067 pthread_cond_init(&qe->cond, NULL);
1069 for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next);
1073 static void dequeue_path(struct fuse *f, struct lock_queue_element *qe,
1074 fuse_ino_t nodeid, const char *name, int wr)
1076 struct lock_queue_element **qp;
1078 debug_path(f, "DEQUEUE PATH", nodeid, name, wr);
1079 pthread_cond_destroy(&qe->cond);
1080 for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next);
1084 static void wait_on_path(struct fuse *f, struct lock_queue_element *qe,
1085 fuse_ino_t nodeid, const char *name, int wr)
1087 debug_path(f, "WAIT ON PATH", nodeid, name, wr);
1088 pthread_cond_wait(&qe->cond, &f->lock);
1091 static int get_path_common(struct fuse *f, fuse_ino_t nodeid, const char *name,
1092 char **path, struct node **wnode)
1097 pthread_mutex_lock(&f->lock);
1098 ticket = get_ticket(f);
1099 err = try_get_path(f, nodeid, name, path, wnode, ticket);
1100 if (err == -EAGAIN) {
1101 struct lock_queue_element qe;
1103 queue_path(f, &qe, nodeid, name, !!wnode);
1105 wait_on_path(f, &qe, nodeid, name, !!wnode);
1106 err = try_get_path(f, nodeid, name, path, wnode,
1109 } while (err == -EAGAIN);
1110 dequeue_path(f, &qe, nodeid, name, !!wnode);
1112 pthread_mutex_unlock(&f->lock);
1117 static int get_path(struct fuse *f, fuse_ino_t nodeid, char **path)
1119 return get_path_common(f, nodeid, NULL, path, NULL);
1122 static int get_path_nullok(struct fuse *f, fuse_ino_t nodeid, char **path)
1126 if (f->conf.nopath) {
1129 err = get_path_common(f, nodeid, NULL, path, NULL);
1130 if (err == -ENOENT && f->nullpath_ok)
1137 static int get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name,
1140 return get_path_common(f, nodeid, name, path, NULL);
1143 static int get_path_wrlock(struct fuse *f, fuse_ino_t nodeid, const char *name,
1144 char **path, struct node **wnode)
1146 return get_path_common(f, nodeid, name, path, wnode);
1149 static int try_get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1,
1150 fuse_ino_t nodeid2, const char *name2,
1151 char **path1, char **path2,
1152 struct node **wnode1, struct node **wnode2,
1157 /* FIXME: locking two paths needs deadlock checking */
1158 err = try_get_path(f, nodeid1, name1, path1, wnode1, ticket);
1160 err = try_get_path(f, nodeid2, name2, path2, wnode2, ticket);
1162 struct node *wn1 = wnode1 ? *wnode1 : NULL;
1164 unlock_path(f, nodeid1, wn1, NULL, ticket);
1166 if (ticket && err != -EAGAIN)
1167 release_tickets(f, nodeid1, wn1, ticket);
1173 static int get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1,
1174 fuse_ino_t nodeid2, const char *name2,
1175 char **path1, char **path2,
1176 struct node **wnode1, struct node **wnode2)
1181 pthread_mutex_lock(&f->lock);
1182 ticket = get_ticket(f);
1183 err = try_get_path2(f, nodeid1, name1, nodeid2, name2,
1184 path1, path2, wnode1, wnode2, ticket);
1185 if (err == -EAGAIN) {
1186 struct lock_queue_element qe;
1188 queue_path(f, &qe, nodeid1, name1, !!wnode1);
1189 debug_path(f, " path2", nodeid2, name2, !!wnode2);
1191 wait_on_path(f, &qe, nodeid1, name1, !!wnode1);
1192 debug_path(f, " path2", nodeid2, name2, !!wnode2);
1193 err = try_get_path2(f, nodeid1, name1, nodeid2, name2,
1194 path1, path2, wnode1, wnode2,
1197 } while (err == -EAGAIN);
1198 dequeue_path(f, &qe, nodeid1, name1, !!wnode1);
1199 debug_path(f, " path2", nodeid2, name2, !!wnode2);
1201 pthread_mutex_unlock(&f->lock);
1206 static void free_path_wrlock(struct fuse *f, fuse_ino_t nodeid,
1207 struct node *wnode, char *path)
1209 pthread_mutex_lock(&f->lock);
1210 unlock_path(f, nodeid, wnode, NULL, 0);
1212 pthread_mutex_unlock(&f->lock);
1216 static void free_path(struct fuse *f, fuse_ino_t nodeid, char *path)
1219 free_path_wrlock(f, nodeid, NULL, path);
1222 static void free_path2(struct fuse *f, fuse_ino_t nodeid1, fuse_ino_t nodeid2,
1223 struct node *wnode1, struct node *wnode2,
1224 char *path1, char *path2)
1226 pthread_mutex_lock(&f->lock);
1227 unlock_path(f, nodeid1, wnode1, NULL, 0);
1228 unlock_path(f, nodeid2, wnode2, NULL, 0);
1230 pthread_mutex_unlock(&f->lock);
1235 static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup)
1238 if (nodeid == FUSE_ROOT_ID)
1240 pthread_mutex_lock(&f->lock);
1241 node = get_node(f, nodeid);
1244 * Node may still be locked due to interrupt idiocy in open,
1245 * create and opendir
1247 while (node->nlookup == nlookup && node->treelock) {
1248 struct lock_queue_element qe;
1250 queue_path(f, &qe, node->nodeid, NULL, 0);
1252 wait_on_path(f, &qe, node->nodeid, NULL, 0);
1255 } while (node->nlookup == nlookup && node->treelock);
1256 dequeue_path(f, &qe, node->nodeid, NULL, 0);
1259 assert(node->nlookup >= nlookup);
1260 node->nlookup -= nlookup;
1261 if (!node->nlookup) {
1262 unref_node(f, node);
1263 } else if (lru_enabled(f) && node->nlookup == 1) {
1264 set_forget_time(f, node);
1266 pthread_mutex_unlock(&f->lock);
1269 static void unlink_node(struct fuse *f, struct node *node)
1271 if (f->conf.remember) {
1272 assert(node->nlookup > 1);
1275 unhash_name(f, node);
1278 static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name)
1282 pthread_mutex_lock(&f->lock);
1283 node = lookup_node(f, dir, name);
1285 unlink_node(f, node);
1286 pthread_mutex_unlock(&f->lock);
1289 static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname,
1290 fuse_ino_t newdir, const char *newname, int hide)
1293 struct node *newnode;
1296 pthread_mutex_lock(&f->lock);
1297 node = lookup_node(f, olddir, oldname);
1298 newnode = lookup_node(f, newdir, newname);
1302 if (newnode != NULL) {
1304 fprintf(stderr, "fuse: hidden file got created during hiding\n");
1308 unlink_node(f, newnode);
1311 unhash_name(f, node);
1312 if (hash_name(f, node, newdir, newname) == -1) {
1318 node->is_hidden = 1;
1321 pthread_mutex_unlock(&f->lock);
1325 static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf)
1327 if (!f->conf.use_ino)
1328 stbuf->st_ino = nodeid;
1329 if (f->conf.set_mode)
1330 stbuf->st_mode = (stbuf->st_mode & S_IFMT) |
1331 (0777 & ~f->conf.umask);
1332 if (f->conf.set_uid)
1333 stbuf->st_uid = f->conf.uid;
1334 if (f->conf.set_gid)
1335 stbuf->st_gid = f->conf.gid;
1338 static struct fuse *req_fuse(fuse_req_t req)
1340 return (struct fuse *) fuse_req_userdata(req);
1343 static void fuse_intr_sighandler(int sig)
1349 struct fuse_intr_data {
1351 pthread_cond_t cond;
1355 static void fuse_interrupt(fuse_req_t req, void *d_)
1357 struct fuse_intr_data *d = d_;
1358 struct fuse *f = req_fuse(req);
1360 if (d->id == pthread_self())
1363 pthread_mutex_lock(&f->lock);
1364 while (!d->finished) {
1366 struct timespec timeout;
1368 pthread_kill(d->id, f->conf.intr_signal);
1369 gettimeofday(&now, NULL);
1370 timeout.tv_sec = now.tv_sec + 1;
1371 timeout.tv_nsec = now.tv_usec * 1000;
1372 pthread_cond_timedwait(&d->cond, &f->lock, &timeout);
1374 pthread_mutex_unlock(&f->lock);
1377 static void fuse_do_finish_interrupt(struct fuse *f, fuse_req_t req,
1378 struct fuse_intr_data *d)
1380 pthread_mutex_lock(&f->lock);
1382 pthread_cond_broadcast(&d->cond);
1383 pthread_mutex_unlock(&f->lock);
1384 fuse_req_interrupt_func(req, NULL, NULL);
1385 pthread_cond_destroy(&d->cond);
1388 static void fuse_do_prepare_interrupt(fuse_req_t req, struct fuse_intr_data *d)
1390 d->id = pthread_self();
1391 pthread_cond_init(&d->cond, NULL);
1393 fuse_req_interrupt_func(req, fuse_interrupt, d);
1396 static inline void fuse_finish_interrupt(struct fuse *f, fuse_req_t req,
1397 struct fuse_intr_data *d)
1400 fuse_do_finish_interrupt(f, req, d);
1403 static inline void fuse_prepare_interrupt(struct fuse *f, fuse_req_t req,
1404 struct fuse_intr_data *d)
1407 fuse_do_prepare_interrupt(req, d);
1410 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
1412 static int fuse_compat_open(struct fuse_fs *fs, const char *path,
1413 struct fuse_file_info *fi)
1416 if (!fs->compat || fs->compat >= 25)
1417 err = fs->op.open(path, fi);
1418 else if (fs->compat == 22) {
1419 struct fuse_file_info_compat tmp;
1420 memcpy(&tmp, fi, sizeof(tmp));
1421 err = ((struct fuse_operations_compat22 *) &fs->op)->open(path,
1423 memcpy(fi, &tmp, sizeof(tmp));
1426 err = ((struct fuse_operations_compat2 *) &fs->op)
1427 ->open(path, fi->flags);
1431 static int fuse_compat_release(struct fuse_fs *fs, const char *path,
1432 struct fuse_file_info *fi)
1434 if (!fs->compat || fs->compat >= 22)
1435 return fs->op.release(path, fi);
1437 return ((struct fuse_operations_compat2 *) &fs->op)
1438 ->release(path, fi->flags);
1441 static int fuse_compat_opendir(struct fuse_fs *fs, const char *path,
1442 struct fuse_file_info *fi)
1444 if (!fs->compat || fs->compat >= 25)
1445 return fs->op.opendir(path, fi);
1448 struct fuse_file_info_compat tmp;
1449 memcpy(&tmp, fi, sizeof(tmp));
1450 err = ((struct fuse_operations_compat22 *) &fs->op)
1451 ->opendir(path, &tmp);
1452 memcpy(fi, &tmp, sizeof(tmp));
1458 static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf,
1459 struct statvfs *stbuf)
1461 stbuf->f_bsize = compatbuf->block_size;
1462 stbuf->f_blocks = compatbuf->blocks;
1463 stbuf->f_bfree = compatbuf->blocks_free;
1464 stbuf->f_bavail = compatbuf->blocks_free;
1465 stbuf->f_files = compatbuf->files;
1466 stbuf->f_ffree = compatbuf->files_free;
1467 stbuf->f_namemax = compatbuf->namelen;
1470 static void convert_statfs_old(struct statfs *oldbuf, struct statvfs *stbuf)
1472 stbuf->f_bsize = oldbuf->f_bsize;
1473 stbuf->f_blocks = oldbuf->f_blocks;
1474 stbuf->f_bfree = oldbuf->f_bfree;
1475 stbuf->f_bavail = oldbuf->f_bavail;
1476 stbuf->f_files = oldbuf->f_files;
1477 stbuf->f_ffree = oldbuf->f_ffree;
1478 stbuf->f_namemax = oldbuf->f_namelen;
1481 static int fuse_compat_statfs(struct fuse_fs *fs, const char *path,
1482 struct statvfs *buf)
1486 if (!fs->compat || fs->compat >= 25) {
1487 err = fs->op.statfs(fs->compat == 25 ? "/" : path, buf);
1488 } else if (fs->compat > 11) {
1489 struct statfs oldbuf;
1490 err = ((struct fuse_operations_compat22 *) &fs->op)
1491 ->statfs("/", &oldbuf);
1493 convert_statfs_old(&oldbuf, buf);
1495 struct fuse_statfs_compat1 compatbuf;
1496 memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1));
1497 err = ((struct fuse_operations_compat1 *) &fs->op)
1498 ->statfs(&compatbuf);
1500 convert_statfs_compat(&compatbuf, buf);
1505 #else /* __FreeBSD__ || __NetBSD__ */
1507 static inline int fuse_compat_open(struct fuse_fs *fs, char *path,
1508 struct fuse_file_info *fi)
1510 return fs->op.open(path, fi);
1513 static inline int fuse_compat_release(struct fuse_fs *fs, const char *path,
1514 struct fuse_file_info *fi)
1516 return fs->op.release(path, fi);
1519 static inline int fuse_compat_opendir(struct fuse_fs *fs, const char *path,
1520 struct fuse_file_info *fi)
1522 return fs->op.opendir(path, fi);
1525 static inline int fuse_compat_statfs(struct fuse_fs *fs, const char *path,
1526 struct statvfs *buf)
1528 return fs->op.statfs(fs->compat == 25 ? "/" : path, buf);
1531 #endif /* __FreeBSD__ || __NetBSD__ */
1533 int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf)
1535 fuse_get_context()->private_data = fs->user_data;
1536 if (fs->op.getattr) {
1538 fprintf(stderr, "getattr %s\n", path);
1540 return fs->op.getattr(path, buf);
1546 int fuse_fs_fgetattr(struct fuse_fs *fs, const char *path, struct stat *buf,
1547 struct fuse_file_info *fi)
1549 fuse_get_context()->private_data = fs->user_data;
1550 if (fs->op.fgetattr) {
1552 fprintf(stderr, "fgetattr[%llu] %s\n",
1553 (unsigned long long) fi->fh, path);
1555 return fs->op.fgetattr(path, buf, fi);
1556 } else if (path && fs->op.getattr) {
1558 fprintf(stderr, "getattr %s\n", path);
1560 return fs->op.getattr(path, buf);
1566 int fuse_fs_rename(struct fuse_fs *fs, const char *oldpath,
1567 const char *newpath)
1569 fuse_get_context()->private_data = fs->user_data;
1570 if (fs->op.rename) {
1572 fprintf(stderr, "rename %s %s\n", oldpath, newpath);
1574 return fs->op.rename(oldpath, newpath);
1580 int fuse_fs_unlink(struct fuse_fs *fs, const char *path)
1582 fuse_get_context()->private_data = fs->user_data;
1583 if (fs->op.unlink) {
1585 fprintf(stderr, "unlink %s\n", path);
1587 return fs->op.unlink(path);
1593 int fuse_fs_rmdir(struct fuse_fs *fs, const char *path)
1595 fuse_get_context()->private_data = fs->user_data;
1598 fprintf(stderr, "rmdir %s\n", path);
1600 return fs->op.rmdir(path);
1606 int fuse_fs_symlink(struct fuse_fs *fs, const char *linkname, const char *path)
1608 fuse_get_context()->private_data = fs->user_data;
1609 if (fs->op.symlink) {
1611 fprintf(stderr, "symlink %s %s\n", linkname, path);
1613 return fs->op.symlink(linkname, path);
1619 int fuse_fs_link(struct fuse_fs *fs, const char *oldpath, const char *newpath)
1621 fuse_get_context()->private_data = fs->user_data;
1624 fprintf(stderr, "link %s %s\n", oldpath, newpath);
1626 return fs->op.link(oldpath, newpath);
1632 int fuse_fs_release(struct fuse_fs *fs, const char *path,
1633 struct fuse_file_info *fi)
1635 fuse_get_context()->private_data = fs->user_data;
1636 if (fs->op.release) {
1638 fprintf(stderr, "release%s[%llu] flags: 0x%x\n",
1639 fi->flush ? "+flush" : "",
1640 (unsigned long long) fi->fh, fi->flags);
1642 return fuse_compat_release(fs, path, fi);
1648 int fuse_fs_opendir(struct fuse_fs *fs, const char *path,
1649 struct fuse_file_info *fi)
1651 fuse_get_context()->private_data = fs->user_data;
1652 if (fs->op.opendir) {
1656 fprintf(stderr, "opendir flags: 0x%x %s\n", fi->flags,
1659 err = fuse_compat_opendir(fs, path, fi);
1661 if (fs->debug && !err)
1662 fprintf(stderr, " opendir[%lli] flags: 0x%x %s\n",
1663 (unsigned long long) fi->fh, fi->flags, path);
1671 int fuse_fs_open(struct fuse_fs *fs, const char *path,
1672 struct fuse_file_info *fi)
1674 fuse_get_context()->private_data = fs->user_data;
1679 fprintf(stderr, "open flags: 0x%x %s\n", fi->flags,
1682 err = fuse_compat_open(fs, path, fi);
1684 if (fs->debug && !err)
1685 fprintf(stderr, " open[%lli] flags: 0x%x %s\n",
1686 (unsigned long long) fi->fh, fi->flags, path);
1694 static void fuse_free_buf(struct fuse_bufvec *buf)
1699 for (i = 0; i < buf->count; i++)
1700 free(buf->buf[i].mem);
1705 int fuse_fs_read_buf(struct fuse_fs *fs, const char *path,
1706 struct fuse_bufvec **bufp, size_t size, off_t off,
1707 struct fuse_file_info *fi)
1709 fuse_get_context()->private_data = fs->user_data;
1710 if (fs->op.read || fs->op.read_buf) {
1715 "read[%llu] %zu bytes from %llu flags: 0x%x\n",
1716 (unsigned long long) fi->fh,
1717 size, (unsigned long long) off, fi->flags);
1719 if (fs->op.read_buf) {
1720 res = fs->op.read_buf(path, bufp, size, off, fi);
1722 struct fuse_bufvec *buf;
1725 buf = malloc(sizeof(struct fuse_bufvec));
1734 *buf = FUSE_BUFVEC_INIT(size);
1735 buf->buf[0].mem = mem;
1738 res = fs->op.read(path, mem, size, off, fi);
1740 buf->buf[0].size = res;
1743 if (fs->debug && res >= 0)
1744 fprintf(stderr, " read[%llu] %zu bytes from %llu\n",
1745 (unsigned long long) fi->fh,
1746 fuse_buf_size(*bufp),
1747 (unsigned long long) off);
1748 if (res >= 0 && fuse_buf_size(*bufp) > (int) size)
1749 fprintf(stderr, "fuse: read too many bytes\n");
1760 int fuse_fs_read(struct fuse_fs *fs, const char *path, char *mem, size_t size,
1761 off_t off, struct fuse_file_info *fi)
1764 struct fuse_bufvec *buf = NULL;
1766 res = fuse_fs_read_buf(fs, path, &buf, size, off, fi);
1768 struct fuse_bufvec dst = FUSE_BUFVEC_INIT(size);
1770 dst.buf[0].mem = mem;
1771 res = fuse_buf_copy(&dst, buf, 0);
1778 int fuse_fs_write_buf(struct fuse_fs *fs, const char *path,
1779 struct fuse_bufvec *buf, off_t off,
1780 struct fuse_file_info *fi)
1782 fuse_get_context()->private_data = fs->user_data;
1783 if (fs->op.write_buf || fs->op.write) {
1785 size_t size = fuse_buf_size(buf);
1787 assert(buf->idx == 0 && buf->off == 0);
1790 "write%s[%llu] %zu bytes to %llu flags: 0x%x\n",
1791 fi->writepage ? "page" : "",
1792 (unsigned long long) fi->fh,
1794 (unsigned long long) off,
1797 if (fs->op.write_buf) {
1798 res = fs->op.write_buf(path, buf, off, fi);
1801 struct fuse_buf *flatbuf;
1802 struct fuse_bufvec tmp = FUSE_BUFVEC_INIT(size);
1804 if (buf->count == 1 &&
1805 !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
1806 flatbuf = &buf->buf[0];
1813 tmp.buf[0].mem = mem;
1814 res = fuse_buf_copy(&tmp, buf, 0);
1818 tmp.buf[0].size = res;
1819 flatbuf = &tmp.buf[0];
1822 res = fs->op.write(path, flatbuf->mem, flatbuf->size,
1828 if (fs->debug && res >= 0)
1829 fprintf(stderr, " write%s[%llu] %u bytes to %llu\n",
1830 fi->writepage ? "page" : "",
1831 (unsigned long long) fi->fh, res,
1832 (unsigned long long) off);
1833 if (res > (int) size)
1834 fprintf(stderr, "fuse: wrote too many bytes\n");
1842 int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *mem,
1843 size_t size, off_t off, struct fuse_file_info *fi)
1845 struct fuse_bufvec bufv = FUSE_BUFVEC_INIT(size);
1847 bufv.buf[0].mem = (void *) mem;
1849 return fuse_fs_write_buf(fs, path, &bufv, off, fi);
1852 int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync,
1853 struct fuse_file_info *fi)
1855 fuse_get_context()->private_data = fs->user_data;
1858 fprintf(stderr, "fsync[%llu] datasync: %i\n",
1859 (unsigned long long) fi->fh, datasync);
1861 return fs->op.fsync(path, datasync, fi);
1867 int fuse_fs_fsyncdir(struct fuse_fs *fs, const char *path, int datasync,
1868 struct fuse_file_info *fi)
1870 fuse_get_context()->private_data = fs->user_data;
1871 if (fs->op.fsyncdir) {
1873 fprintf(stderr, "fsyncdir[%llu] datasync: %i\n",
1874 (unsigned long long) fi->fh, datasync);
1876 return fs->op.fsyncdir(path, datasync, fi);
1882 int fuse_fs_flush(struct fuse_fs *fs, const char *path,
1883 struct fuse_file_info *fi)
1885 fuse_get_context()->private_data = fs->user_data;
1888 fprintf(stderr, "flush[%llu]\n",
1889 (unsigned long long) fi->fh);
1891 return fs->op.flush(path, fi);
1897 int fuse_fs_statfs(struct fuse_fs *fs, const char *path, struct statvfs *buf)
1899 fuse_get_context()->private_data = fs->user_data;
1900 if (fs->op.statfs) {
1902 fprintf(stderr, "statfs %s\n", path);
1904 return fuse_compat_statfs(fs, path, buf);
1906 buf->f_namemax = 255;
1912 int fuse_fs_releasedir(struct fuse_fs *fs, const char *path,
1913 struct fuse_file_info *fi)
1915 fuse_get_context()->private_data = fs->user_data;
1916 if (fs->op.releasedir) {
1918 fprintf(stderr, "releasedir[%llu] flags: 0x%x\n",
1919 (unsigned long long) fi->fh, fi->flags);
1921 return fs->op.releasedir(path, fi);
1927 static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type,
1933 memset(&stbuf, 0, sizeof(stbuf));
1934 stbuf.st_mode = type << 12;
1937 res = dh->filler(dh->buf, name, &stbuf, 0);
1938 return res ? -ENOMEM : 0;
1941 int fuse_fs_readdir(struct fuse_fs *fs, const char *path, void *buf,
1942 fuse_fill_dir_t filler, off_t off,
1943 struct fuse_file_info *fi)
1945 fuse_get_context()->private_data = fs->user_data;
1946 if (fs->op.readdir) {
1948 fprintf(stderr, "readdir[%llu] from %llu\n",
1949 (unsigned long long) fi->fh,
1950 (unsigned long long) off);
1952 return fs->op.readdir(path, buf, filler, off, fi);
1953 } else if (fs->op.getdir) {
1954 struct fuse_dirhandle dh;
1957 fprintf(stderr, "getdir[%llu]\n",
1958 (unsigned long long) fi->fh);
1962 return fs->op.getdir(path, &dh, fill_dir_old);
1968 int fuse_fs_create(struct fuse_fs *fs, const char *path, mode_t mode,
1969 struct fuse_file_info *fi)
1971 fuse_get_context()->private_data = fs->user_data;
1972 if (fs->op.create) {
1977 "create flags: 0x%x %s 0%o umask=0%03o\n",
1978 fi->flags, path, mode,
1979 fuse_get_context()->umask);
1981 err = fs->op.create(path, mode, fi);
1983 if (fs->debug && !err)
1984 fprintf(stderr, " create[%llu] flags: 0x%x %s\n",
1985 (unsigned long long) fi->fh, fi->flags, path);
1993 int fuse_fs_lock(struct fuse_fs *fs, const char *path,
1994 struct fuse_file_info *fi, int cmd, struct flock *lock)
1996 fuse_get_context()->private_data = fs->user_data;
1999 fprintf(stderr, "lock[%llu] %s %s start: %llu len: %llu pid: %llu\n",
2000 (unsigned long long) fi->fh,
2001 (cmd == F_GETLK ? "F_GETLK" :
2002 (cmd == F_SETLK ? "F_SETLK" :
2003 (cmd == F_SETLKW ? "F_SETLKW" : "???"))),
2004 (lock->l_type == F_RDLCK ? "F_RDLCK" :
2005 (lock->l_type == F_WRLCK ? "F_WRLCK" :
2006 (lock->l_type == F_UNLCK ? "F_UNLCK" :
2008 (unsigned long long) lock->l_start,
2009 (unsigned long long) lock->l_len,
2010 (unsigned long long) lock->l_pid);
2012 return fs->op.lock(path, fi, cmd, lock);
2018 int fuse_fs_flock(struct fuse_fs *fs, const char *path,
2019 struct fuse_file_info *fi, int op)
2021 fuse_get_context()->private_data = fs->user_data;
2024 int xop = op & ~LOCK_NB;
2026 fprintf(stderr, "lock[%llu] %s%s\n",
2027 (unsigned long long) fi->fh,
2028 xop == LOCK_SH ? "LOCK_SH" :
2029 (xop == LOCK_EX ? "LOCK_EX" :
2030 (xop == LOCK_UN ? "LOCK_UN" : "???")),
2031 (op & LOCK_NB) ? "|LOCK_NB" : "");
2033 return fs->op.flock(path, fi, op);
2039 int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid)
2041 fuse_get_context()->private_data = fs->user_data;
2044 fprintf(stderr, "chown %s %lu %lu\n", path,
2045 (unsigned long) uid, (unsigned long) gid);
2047 return fs->op.chown(path, uid, gid);
2053 int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off_t size)
2055 fuse_get_context()->private_data = fs->user_data;
2056 if (fs->op.truncate) {
2058 fprintf(stderr, "truncate %s %llu\n", path,
2059 (unsigned long long) size);
2061 return fs->op.truncate(path, size);
2067 int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off_t size,
2068 struct fuse_file_info *fi)
2070 fuse_get_context()->private_data = fs->user_data;
2071 if (fs->op.ftruncate) {
2073 fprintf(stderr, "ftruncate[%llu] %llu\n",
2074 (unsigned long long) fi->fh,
2075 (unsigned long long) size);
2077 return fs->op.ftruncate(path, size, fi);
2078 } else if (path && fs->op.truncate) {
2080 fprintf(stderr, "truncate %s %llu\n", path,
2081 (unsigned long long) size);
2083 return fs->op.truncate(path, size);
2089 int fuse_fs_utimens(struct fuse_fs *fs, const char *path,
2090 const struct timespec tv[2])
2092 fuse_get_context()->private_data = fs->user_data;
2093 if (fs->op.utimens) {
2095 fprintf(stderr, "utimens %s %li.%09lu %li.%09lu\n",
2096 path, tv[0].tv_sec, tv[0].tv_nsec,
2097 tv[1].tv_sec, tv[1].tv_nsec);
2099 return fs->op.utimens(path, tv);
2100 } else if(fs->op.utime) {
2104 fprintf(stderr, "utime %s %li %li\n", path,
2105 tv[0].tv_sec, tv[1].tv_sec);
2107 buf.actime = tv[0].tv_sec;
2108 buf.modtime = tv[1].tv_sec;
2109 return fs->op.utime(path, &buf);
2115 int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask)
2117 fuse_get_context()->private_data = fs->user_data;
2118 if (fs->op.access) {
2120 fprintf(stderr, "access %s 0%o\n", path, mask);
2122 return fs->op.access(path, mask);
2128 int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf,
2131 fuse_get_context()->private_data = fs->user_data;
2132 if (fs->op.readlink) {
2134 fprintf(stderr, "readlink %s %lu\n", path,
2135 (unsigned long) len);
2137 return fs->op.readlink(path, buf, len);
2143 int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode,
2146 fuse_get_context()->private_data = fs->user_data;
2149 fprintf(stderr, "mknod %s 0%o 0x%llx umask=0%03o\n",
2150 path, mode, (unsigned long long) rdev,
2151 fuse_get_context()->umask);
2153 return fs->op.mknod(path, mode, rdev);
2159 int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode)
2161 fuse_get_context()->private_data = fs->user_data;
2164 fprintf(stderr, "mkdir %s 0%o umask=0%03o\n",
2165 path, mode, fuse_get_context()->umask);
2167 return fs->op.mkdir(path, mode);
2173 int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name,
2174 const char *value, size_t size, int flags)
2176 fuse_get_context()->private_data = fs->user_data;
2177 if (fs->op.setxattr) {
2179 fprintf(stderr, "setxattr %s %s %lu 0x%x\n",
2180 path, name, (unsigned long) size, flags);
2182 return fs->op.setxattr(path, name, value, size, flags);
2188 int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name,
2189 char *value, size_t size)
2191 fuse_get_context()->private_data = fs->user_data;
2192 if (fs->op.getxattr) {
2194 fprintf(stderr, "getxattr %s %s %lu\n",
2195 path, name, (unsigned long) size);
2197 return fs->op.getxattr(path, name, value, size);
2203 int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list,
2206 fuse_get_context()->private_data = fs->user_data;
2207 if (fs->op.listxattr) {
2209 fprintf(stderr, "listxattr %s %lu\n",
2210 path, (unsigned long) size);
2212 return fs->op.listxattr(path, list, size);
2218 int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize,
2221 fuse_get_context()->private_data = fs->user_data;
2224 fprintf(stderr, "bmap %s blocksize: %lu index: %llu\n",
2225 path, (unsigned long) blocksize,
2226 (unsigned long long) *idx);
2228 return fs->op.bmap(path, blocksize, idx);
2234 int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, const char *name)
2236 fuse_get_context()->private_data = fs->user_data;
2237 if (fs->op.removexattr) {
2239 fprintf(stderr, "removexattr %s %s\n", path, name);
2241 return fs->op.removexattr(path, name);
2247 int fuse_fs_ioctl(struct fuse_fs *fs, const char *path, int cmd, void *arg,
2248 struct fuse_file_info *fi, unsigned int flags, void *data)
2250 fuse_get_context()->private_data = fs->user_data;
2253 fprintf(stderr, "ioctl[%llu] 0x%x flags: 0x%x\n",
2254 (unsigned long long) fi->fh, cmd, flags);
2256 return fs->op.ioctl(path, cmd, arg, fi, flags, data);
2261 int fuse_fs_poll(struct fuse_fs *fs, const char *path,
2262 struct fuse_file_info *fi, struct fuse_pollhandle *ph,
2265 fuse_get_context()->private_data = fs->user_data;
2270 fprintf(stderr, "poll[%llu] ph: %p\n",
2271 (unsigned long long) fi->fh, ph);
2273 res = fs->op.poll(path, fi, ph, reventsp);
2275 if (fs->debug && !res)
2276 fprintf(stderr, " poll[%llu] revents: 0x%x\n",
2277 (unsigned long long) fi->fh, *reventsp);
2284 static int is_open(struct fuse *f, fuse_ino_t dir, const char *name)
2288 pthread_mutex_lock(&f->lock);
2289 node = lookup_node(f, dir, name);
2290 if (node && node->open_count > 0)
2292 pthread_mutex_unlock(&f->lock);
2296 static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname,
2297 char *newname, size_t bufsize)
2301 struct node *newnode;
2307 pthread_mutex_lock(&f->lock);
2308 node = lookup_node(f, dir, oldname);
2310 pthread_mutex_unlock(&f->lock);
2315 snprintf(newname, bufsize, ".fuse_hidden%08x%08x",
2316 (unsigned int) node->nodeid, f->hidectr);
2317 newnode = lookup_node(f, dir, newname);
2320 res = try_get_path(f, dir, newname, &newpath, NULL, 0);
2321 pthread_mutex_unlock(&f->lock);
2325 res = fuse_fs_getattr(f->fs, newpath, &buf);
2330 } while(res == 0 && --failctr);
2335 static int hide_node(struct fuse *f, const char *oldpath,
2336 fuse_ino_t dir, const char *oldname)
2342 newpath = hidden_name(f, dir, oldname, newname, sizeof(newname));
2344 err = fuse_fs_rename(f->fs, oldpath, newpath);
2346 err = rename_node(f, dir, oldname, dir, newname, 1);
2352 static int mtime_eq(const struct stat *stbuf, const struct timespec *ts)
2354 return stbuf->st_mtime == ts->tv_sec &&
2355 ST_MTIM_NSEC(stbuf) == ts->tv_nsec;
2358 #ifndef CLOCK_MONOTONIC
2359 #define CLOCK_MONOTONIC CLOCK_REALTIME
2362 static void curr_time(struct timespec *now)
2364 static clockid_t clockid = CLOCK_MONOTONIC;
2365 int res = clock_gettime(clockid, now);
2366 if (res == -1 && errno == EINVAL) {
2367 clockid = CLOCK_REALTIME;
2368 res = clock_gettime(clockid, now);
2371 perror("fuse: clock_gettime");
2376 static void update_stat(struct node *node, const struct stat *stbuf)
2378 if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) ||
2379 stbuf->st_size != node->size))
2380 node->cache_valid = 0;
2381 node->mtime.tv_sec = stbuf->st_mtime;
2382 node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf);
2383 node->size = stbuf->st_size;
2384 curr_time(&node->stat_updated);
2387 static int lookup_path(struct fuse *f, fuse_ino_t nodeid,
2388 const char *name, const char *path,
2389 struct fuse_entry_param *e, struct fuse_file_info *fi)
2393 memset(e, 0, sizeof(struct fuse_entry_param));
2395 res = fuse_fs_fgetattr(f->fs, path, &e->attr, fi);
2397 res = fuse_fs_getattr(f->fs, path, &e->attr);
2401 node = find_node(f, nodeid, name);
2405 e->ino = node->nodeid;
2406 e->generation = node->generation;
2407 e->entry_timeout = f->conf.entry_timeout;
2408 e->attr_timeout = f->conf.attr_timeout;
2409 if (f->conf.auto_cache) {
2410 pthread_mutex_lock(&f->lock);
2411 update_stat(node, &e->attr);
2412 pthread_mutex_unlock(&f->lock);
2414 set_stat(f, e->ino, &e->attr);
2416 fprintf(stderr, " NODEID: %lu\n",
2417 (unsigned long) e->ino);
2423 static struct fuse_context_i *fuse_get_context_internal(void)
2425 struct fuse_context_i *c;
2427 c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key);
2429 c = (struct fuse_context_i *)
2430 calloc(1, sizeof(struct fuse_context_i));
2432 /* This is hard to deal with properly, so just
2433 abort. If memory is so low that the
2434 context cannot be allocated, there's not
2435 much hope for the filesystem anyway */
2436 fprintf(stderr, "fuse: failed to allocate thread specific data\n");
2439 pthread_setspecific(fuse_context_key, c);
2444 static void fuse_freecontext(void *data)
2449 static int fuse_create_context_key(void)
2452 pthread_mutex_lock(&fuse_context_lock);
2453 if (!fuse_context_ref) {
2454 err = pthread_key_create(&fuse_context_key, fuse_freecontext);
2456 fprintf(stderr, "fuse: failed to create thread specific key: %s\n",
2458 pthread_mutex_unlock(&fuse_context_lock);
2463 pthread_mutex_unlock(&fuse_context_lock);
2467 static void fuse_delete_context_key(void)
2469 pthread_mutex_lock(&fuse_context_lock);
2471 if (!fuse_context_ref) {
2472 free(pthread_getspecific(fuse_context_key));
2473 pthread_key_delete(fuse_context_key);
2475 pthread_mutex_unlock(&fuse_context_lock);
2478 static struct fuse *req_fuse_prepare(fuse_req_t req)
2480 struct fuse_context_i *c = fuse_get_context_internal();
2481 const struct fuse_ctx *ctx = fuse_req_ctx(req);
2483 c->ctx.fuse = req_fuse(req);
2484 c->ctx.uid = ctx->uid;
2485 c->ctx.gid = ctx->gid;
2486 c->ctx.pid = ctx->pid;
2487 c->ctx.umask = ctx->umask;
2491 static inline void reply_err(fuse_req_t req, int err)
2493 /* fuse_reply_err() uses non-negated errno values */
2494 fuse_reply_err(req, -err);
2497 static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e,
2501 struct fuse *f = req_fuse(req);
2502 if (fuse_reply_entry(req, e) == -ENOENT) {
2503 /* Skip forget for negative result */
2505 forget_node(f, e->ino, 1);
2508 reply_err(req, err);
2511 void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn)
2513 fuse_get_context()->private_data = fs->user_data;
2514 if (!fs->op.write_buf)
2515 conn->want &= ~FUSE_CAP_SPLICE_READ;
2517 conn->want &= ~FUSE_CAP_POSIX_LOCKS;
2519 conn->want &= ~FUSE_CAP_FLOCK_LOCKS;
2521 fs->user_data = fs->op.init(conn);
2524 static void fuse_lib_init(void *data, struct fuse_conn_info *conn)
2526 struct fuse *f = (struct fuse *) data;
2527 struct fuse_context_i *c = fuse_get_context_internal();
2529 memset(c, 0, sizeof(*c));
2531 conn->want |= FUSE_CAP_EXPORT_SUPPORT;
2532 fuse_fs_init(f->fs, conn);
2535 void fuse_fs_destroy(struct fuse_fs *fs)
2537 fuse_get_context()->private_data = fs->user_data;
2539 fs->op.destroy(fs->user_data);
2541 fuse_put_module(fs->m);
2545 static void fuse_lib_destroy(void *data)
2547 struct fuse *f = (struct fuse *) data;
2548 struct fuse_context_i *c = fuse_get_context_internal();
2550 memset(c, 0, sizeof(*c));
2552 fuse_fs_destroy(f->fs);
2556 static void fuse_lib_lookup(fuse_req_t req, fuse_ino_t parent,
2559 struct fuse *f = req_fuse_prepare(req);
2560 struct fuse_entry_param e;
2563 struct node *dot = NULL;
2565 if (name[0] == '.') {
2566 int len = strlen(name);
2568 if (len == 1 || (name[1] == '.' && len == 2)) {
2569 pthread_mutex_lock(&f->lock);
2572 fprintf(stderr, "LOOKUP-DOT\n");
2573 dot = get_node_nocheck(f, parent);
2575 pthread_mutex_unlock(&f->lock);
2576 reply_entry(req, &e, -ESTALE);
2582 fprintf(stderr, "LOOKUP-DOTDOT\n");
2583 parent = get_node(f, parent)->parent->nodeid;
2585 pthread_mutex_unlock(&f->lock);
2590 err = get_path_name(f, parent, name, &path);
2592 struct fuse_intr_data d;
2594 fprintf(stderr, "LOOKUP %s\n", path);
2595 fuse_prepare_interrupt(f, req, &d);
2596 err = lookup_path(f, parent, name, path, &e, NULL);
2597 if (err == -ENOENT && f->conf.negative_timeout != 0.0) {
2599 e.entry_timeout = f->conf.negative_timeout;
2602 fuse_finish_interrupt(f, req, &d);
2603 free_path(f, parent, path);
2606 pthread_mutex_lock(&f->lock);
2608 pthread_mutex_unlock(&f->lock);
2610 reply_entry(req, &e, err);
2613 static void do_forget(struct fuse *f, fuse_ino_t ino, uint64_t nlookup)
2616 fprintf(stderr, "FORGET %llu/%llu\n", (unsigned long long)ino,
2617 (unsigned long long) nlookup);
2618 forget_node(f, ino, nlookup);
2621 static void fuse_lib_forget(fuse_req_t req, fuse_ino_t ino,
2622 unsigned long nlookup)
2624 do_forget(req_fuse(req), ino, nlookup);
2625 fuse_reply_none(req);
2628 static void fuse_lib_forget_multi(fuse_req_t req, size_t count,
2629 struct fuse_forget_data *forgets)
2631 struct fuse *f = req_fuse(req);
2634 for (i = 0; i < count; i++)
2635 do_forget(f, forgets[i].ino, forgets[i].nlookup);
2637 fuse_reply_none(req);
2641 static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino,
2642 struct fuse_file_info *fi)
2644 struct fuse *f = req_fuse_prepare(req);
2649 memset(&buf, 0, sizeof(buf));
2651 if (fi != NULL && f->fs->op.fgetattr)
2652 err = get_path_nullok(f, ino, &path);
2654 err = get_path(f, ino, &path);
2656 struct fuse_intr_data d;
2657 fuse_prepare_interrupt(f, req, &d);
2659 err = fuse_fs_fgetattr(f->fs, path, &buf, fi);
2661 err = fuse_fs_getattr(f->fs, path, &buf);
2662 fuse_finish_interrupt(f, req, &d);
2663 free_path(f, ino, path);
2668 pthread_mutex_lock(&f->lock);
2669 node = get_node(f, ino);
2670 if (node->is_hidden && buf.st_nlink > 0)
2672 if (f->conf.auto_cache)
2673 update_stat(node, &buf);
2674 pthread_mutex_unlock(&f->lock);
2675 set_stat(f, ino, &buf);
2676 fuse_reply_attr(req, &buf, f->conf.attr_timeout);
2678 reply_err(req, err);
2681 int fuse_fs_chmod(struct fuse_fs *fs, const char *path, mode_t mode)
2683 fuse_get_context()->private_data = fs->user_data;
2685 return fs->op.chmod(path, mode);
2690 static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
2691 int valid, struct fuse_file_info *fi)
2693 struct fuse *f = req_fuse_prepare(req);
2698 if (valid == FUSE_SET_ATTR_SIZE && fi != NULL &&
2699 f->fs->op.ftruncate && f->fs->op.fgetattr)
2700 err = get_path_nullok(f, ino, &path);
2702 err = get_path(f, ino, &path);
2704 struct fuse_intr_data d;
2705 fuse_prepare_interrupt(f, req, &d);
2707 if (!err && (valid & FUSE_SET_ATTR_MODE))
2708 err = fuse_fs_chmod(f->fs, path, attr->st_mode);
2709 if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) {
2710 uid_t uid = (valid & FUSE_SET_ATTR_UID) ?
2711 attr->st_uid : (uid_t) -1;
2712 gid_t gid = (valid & FUSE_SET_ATTR_GID) ?
2713 attr->st_gid : (gid_t) -1;
2714 err = fuse_fs_chown(f->fs, path, uid, gid);
2716 if (!err && (valid & FUSE_SET_ATTR_SIZE)) {
2718 err = fuse_fs_ftruncate(f->fs, path,
2721 err = fuse_fs_truncate(f->fs, path,
2724 #ifdef HAVE_UTIMENSAT
2725 if (!err && f->utime_omit_ok &&
2726 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) {
2727 struct timespec tv[2];
2731 tv[0].tv_nsec = UTIME_OMIT;
2732 tv[1].tv_nsec = UTIME_OMIT;
2734 if (valid & FUSE_SET_ATTR_ATIME_NOW)
2735 tv[0].tv_nsec = UTIME_NOW;
2736 else if (valid & FUSE_SET_ATTR_ATIME)
2737 tv[0] = attr->st_atim;
2739 if (valid & FUSE_SET_ATTR_MTIME_NOW)
2740 tv[1].tv_nsec = UTIME_NOW;
2741 else if (valid & FUSE_SET_ATTR_MTIME)
2742 tv[1] = attr->st_mtim;
2744 err = fuse_fs_utimens(f->fs, path, tv);
2748 (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) ==
2749 (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) {
2750 struct timespec tv[2];
2751 tv[0].tv_sec = attr->st_atime;
2752 tv[0].tv_nsec = ST_ATIM_NSEC(attr);
2753 tv[1].tv_sec = attr->st_mtime;
2754 tv[1].tv_nsec = ST_MTIM_NSEC(attr);
2755 err = fuse_fs_utimens(f->fs, path, tv);
2759 err = fuse_fs_fgetattr(f->fs, path, &buf, fi);
2761 err = fuse_fs_getattr(f->fs, path, &buf);
2763 fuse_finish_interrupt(f, req, &d);
2764 free_path(f, ino, path);
2767 if (f->conf.auto_cache) {
2768 pthread_mutex_lock(&f->lock);
2769 update_stat(get_node(f, ino), &buf);
2770 pthread_mutex_unlock(&f->lock);
2772 set_stat(f, ino, &buf);
2773 fuse_reply_attr(req, &buf, f->conf.attr_timeout);
2775 reply_err(req, err);
2778 static void fuse_lib_access(fuse_req_t req, fuse_ino_t ino, int mask)
2780 struct fuse *f = req_fuse_prepare(req);
2784 err = get_path(f, ino, &path);
2786 struct fuse_intr_data d;
2788 fuse_prepare_interrupt(f, req, &d);
2789 err = fuse_fs_access(f->fs, path, mask);
2790 fuse_finish_interrupt(f, req, &d);
2791 free_path(f, ino, path);
2793 reply_err(req, err);
2796 static void fuse_lib_readlink(fuse_req_t req, fuse_ino_t ino)
2798 struct fuse *f = req_fuse_prepare(req);
2799 char linkname[PATH_MAX + 1];
2803 err = get_path(f, ino, &path);
2805 struct fuse_intr_data d;
2806 fuse_prepare_interrupt(f, req, &d);
2807 err = fuse_fs_readlink(f->fs, path, linkname, sizeof(linkname));
2808 fuse_finish_interrupt(f, req, &d);
2809 free_path(f, ino, path);
2812 linkname[PATH_MAX] = '\0';
2813 fuse_reply_readlink(req, linkname);
2815 reply_err(req, err);
2818 static void fuse_lib_mknod(fuse_req_t req, fuse_ino_t parent, const char *name,
2819 mode_t mode, dev_t rdev)
2821 struct fuse *f = req_fuse_prepare(req);
2822 struct fuse_entry_param e;
2826 err = get_path_name(f, parent, name, &path);
2828 struct fuse_intr_data d;
2830 fuse_prepare_interrupt(f, req, &d);
2832 if (S_ISREG(mode)) {
2833 struct fuse_file_info fi;
2835 memset(&fi, 0, sizeof(fi));
2836 fi.flags = O_CREAT | O_EXCL | O_WRONLY;
2837 err = fuse_fs_create(f->fs, path, mode, &fi);
2839 err = lookup_path(f, parent, name, path, &e,
2841 fuse_fs_release(f->fs, path, &fi);
2844 if (err == -ENOSYS) {
2845 err = fuse_fs_mknod(f->fs, path, mode, rdev);
2847 err = lookup_path(f, parent, name, path, &e,
2850 fuse_finish_interrupt(f, req, &d);
2851 free_path(f, parent, path);
2853 reply_entry(req, &e, err);
2856 static void fuse_lib_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name,
2859 struct fuse *f = req_fuse_prepare(req);
2860 struct fuse_entry_param e;
2864 err = get_path_name(f, parent, name, &path);
2866 struct fuse_intr_data d;
2868 fuse_prepare_interrupt(f, req, &d);
2869 err = fuse_fs_mkdir(f->fs, path, mode);
2871 err = lookup_path(f, parent, name, path, &e, NULL);
2872 fuse_finish_interrupt(f, req, &d);
2873 free_path(f, parent, path);
2875 reply_entry(req, &e, err);
2878 static void fuse_lib_unlink(fuse_req_t req, fuse_ino_t parent,
2881 struct fuse *f = req_fuse_prepare(req);
2886 err = get_path_wrlock(f, parent, name, &path, &wnode);
2888 struct fuse_intr_data d;
2890 fuse_prepare_interrupt(f, req, &d);
2891 if (!f->conf.hard_remove && is_open(f, parent, name)) {
2892 err = hide_node(f, path, parent, name);
2894 err = fuse_fs_unlink(f->fs, path);
2896 remove_node(f, parent, name);
2898 fuse_finish_interrupt(f, req, &d);
2899 free_path_wrlock(f, parent, wnode, path);
2901 reply_err(req, err);
2904 static void fuse_lib_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name)
2906 struct fuse *f = req_fuse_prepare(req);
2911 err = get_path_wrlock(f, parent, name, &path, &wnode);
2913 struct fuse_intr_data d;
2915 fuse_prepare_interrupt(f, req, &d);
2916 err = fuse_fs_rmdir(f->fs, path);
2917 fuse_finish_interrupt(f, req, &d);
2919 remove_node(f, parent, name);
2920 free_path_wrlock(f, parent, wnode, path);
2922 reply_err(req, err);
2925 static void fuse_lib_symlink(fuse_req_t req, const char *linkname,
2926 fuse_ino_t parent, const char *name)
2928 struct fuse *f = req_fuse_prepare(req);
2929 struct fuse_entry_param e;
2933 err = get_path_name(f, parent, name, &path);
2935 struct fuse_intr_data d;
2937 fuse_prepare_interrupt(f, req, &d);
2938 err = fuse_fs_symlink(f->fs, linkname, path);
2940 err = lookup_path(f, parent, name, path, &e, NULL);
2941 fuse_finish_interrupt(f, req, &d);
2942 free_path(f, parent, path);
2944 reply_entry(req, &e, err);
2947 static void fuse_lib_rename(fuse_req_t req, fuse_ino_t olddir,
2948 const char *oldname, fuse_ino_t newdir,
2949 const char *newname)
2951 struct fuse *f = req_fuse_prepare(req);
2954 struct node *wnode1;
2955 struct node *wnode2;
2958 err = get_path2(f, olddir, oldname, newdir, newname,
2959 &oldpath, &newpath, &wnode1, &wnode2);
2961 struct fuse_intr_data d;
2963 fuse_prepare_interrupt(f, req, &d);
2964 if (!f->conf.hard_remove && is_open(f, newdir, newname))
2965 err = hide_node(f, newpath, newdir, newname);
2967 err = fuse_fs_rename(f->fs, oldpath, newpath);
2969 err = rename_node(f, olddir, oldname, newdir,
2972 fuse_finish_interrupt(f, req, &d);
2973 free_path2(f, olddir, newdir, wnode1, wnode2, oldpath, newpath);
2975 reply_err(req, err);
2978 static void fuse_lib_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent,
2979 const char *newname)
2981 struct fuse *f = req_fuse_prepare(req);
2982 struct fuse_entry_param e;
2987 err = get_path2(f, ino, NULL, newparent, newname,
2988 &oldpath, &newpath, NULL, NULL);
2990 struct fuse_intr_data d;
2992 fuse_prepare_interrupt(f, req, &d);
2993 err = fuse_fs_link(f->fs, oldpath, newpath);
2995 err = lookup_path(f, newparent, newname, newpath,
2997 fuse_finish_interrupt(f, req, &d);
2998 free_path2(f, ino, newparent, NULL, NULL, oldpath, newpath);
3000 reply_entry(req, &e, err);
3003 static void fuse_do_release(struct fuse *f, fuse_ino_t ino, const char *path,
3004 struct fuse_file_info *fi)
3007 int unlink_hidden = 0;
3008 const char *compatpath;
3010 if (path != NULL || f->nullpath_ok || f->conf.nopath)
3015 fuse_fs_release(f->fs, compatpath, fi);
3017 pthread_mutex_lock(&f->lock);
3018 node = get_node(f, ino);
3019 assert(node->open_count > 0);
3021 if (node->is_hidden && !node->open_count) {
3023 node->is_hidden = 0;
3025 pthread_mutex_unlock(&f->lock);
3029 fuse_fs_unlink(f->fs, path);
3030 } else if (f->conf.nopath) {
3033 if (get_path(f, ino, &unlinkpath) == 0)
3034 fuse_fs_unlink(f->fs, unlinkpath);
3036 free_path(f, ino, unlinkpath);
3041 static void fuse_lib_create(fuse_req_t req, fuse_ino_t parent,
3042 const char *name, mode_t mode,
3043 struct fuse_file_info *fi)
3045 struct fuse *f = req_fuse_prepare(req);
3046 struct fuse_intr_data d;
3047 struct fuse_entry_param e;
3051 err = get_path_name(f, parent, name, &path);
3053 fuse_prepare_interrupt(f, req, &d);
3054 err = fuse_fs_create(f->fs, path, mode, fi);
3056 err = lookup_path(f, parent, name, path, &e, fi);
3058 fuse_fs_release(f->fs, path, fi);
3059 else if (!S_ISREG(e.attr.st_mode)) {
3061 fuse_fs_release(f->fs, path, fi);
3062 forget_node(f, e.ino, 1);
3064 if (f->conf.direct_io)
3066 if (f->conf.kernel_cache)
3071 fuse_finish_interrupt(f, req, &d);
3074 pthread_mutex_lock(&f->lock);
3075 get_node(f, e.ino)->open_count++;
3076 pthread_mutex_unlock(&f->lock);
3077 if (fuse_reply_create(req, &e, fi) == -ENOENT) {
3078 /* The open syscall was interrupted, so it
3079 must be cancelled */
3080 fuse_do_release(f, e.ino, path, fi);
3081 forget_node(f, e.ino, 1);
3084 reply_err(req, err);
3087 free_path(f, parent, path);
3090 static double diff_timespec(const struct timespec *t1,
3091 const struct timespec *t2)
3093 return (t1->tv_sec - t2->tv_sec) +
3094 ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0;
3097 static void open_auto_cache(struct fuse *f, fuse_ino_t ino, const char *path,
3098 struct fuse_file_info *fi)
3102 pthread_mutex_lock(&f->lock);
3103 node = get_node(f, ino);
3104 if (node->cache_valid) {
3105 struct timespec now;
3108 if (diff_timespec(&now, &node->stat_updated) >
3109 f->conf.ac_attr_timeout) {
3112 pthread_mutex_unlock(&f->lock);
3113 err = fuse_fs_fgetattr(f->fs, path, &stbuf, fi);
3114 pthread_mutex_lock(&f->lock);
3116 update_stat(node, &stbuf);
3118 node->cache_valid = 0;
3121 if (node->cache_valid)
3124 node->cache_valid = 1;
3125 pthread_mutex_unlock(&f->lock);
3128 static void fuse_lib_open(fuse_req_t req, fuse_ino_t ino,
3129 struct fuse_file_info *fi)
3131 struct fuse *f = req_fuse_prepare(req);
3132 struct fuse_intr_data d;
3136 err = get_path(f, ino, &path);
3138 fuse_prepare_interrupt(f, req, &d);
3139 err = fuse_fs_open(f->fs, path, fi);
3141 if (f->conf.direct_io)
3143 if (f->conf.kernel_cache)
3146 if (f->conf.auto_cache)
3147 open_auto_cache(f, ino, path, fi);
3149 fuse_finish_interrupt(f, req, &d);
3152 pthread_mutex_lock(&f->lock);
3153 get_node(f, ino)->open_count++;
3154 pthread_mutex_unlock(&f->lock);
3155 if (fuse_reply_open(req, fi) == -ENOENT) {
3156 /* The open syscall was interrupted, so it
3157 must be cancelled */
3158 fuse_do_release(f, ino, path, fi);
3161 reply_err(req, err);
3163 free_path(f, ino, path);
3166 static void fuse_lib_read(fuse_req_t req, fuse_ino_t ino, size_t size,
3167 off_t off, struct fuse_file_info *fi)
3169 struct fuse *f = req_fuse_prepare(req);
3170 struct fuse_bufvec *buf = NULL;
3174 res = get_path_nullok(f, ino, &path);
3176 struct fuse_intr_data d;
3178 fuse_prepare_interrupt(f, req, &d);
3179 res = fuse_fs_read_buf(f->fs, path, &buf, size, off, fi);
3180 fuse_finish_interrupt(f, req, &d);
3181 free_path(f, ino, path);
3185 fuse_reply_data(req, buf, FUSE_BUF_SPLICE_MOVE);
3187 reply_err(req, res);
3192 static void fuse_lib_write_buf(fuse_req_t req, fuse_ino_t ino,
3193 struct fuse_bufvec *buf, off_t off,
3194 struct fuse_file_info *fi)
3196 struct fuse *f = req_fuse_prepare(req);
3200 res = get_path_nullok(f, ino, &path);
3202 struct fuse_intr_data d;
3204 fuse_prepare_interrupt(f, req, &d);
3205 res = fuse_fs_write_buf(f->fs, path, buf, off, fi);
3206 fuse_finish_interrupt(f, req, &d);
3207 free_path(f, ino, path);
3211 fuse_reply_write(req, res);
3213 reply_err(req, res);
3216 static void fuse_lib_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
3217 struct fuse_file_info *fi)
3219 struct fuse *f = req_fuse_prepare(req);
3223 err = get_path_nullok(f, ino, &path);
3225 struct fuse_intr_data d;
3227 fuse_prepare_interrupt(f, req, &d);
3228 err = fuse_fs_fsync(f->fs, path, datasync, fi);
3229 fuse_finish_interrupt(f, req, &d);
3230 free_path(f, ino, path);
3232 reply_err(req, err);
3235 static struct fuse_dh *get_dirhandle(const struct fuse_file_info *llfi,
3236 struct fuse_file_info *fi)
3238 struct fuse_dh *dh = (struct fuse_dh *) (uintptr_t) llfi->fh;
3239 memset(fi, 0, sizeof(struct fuse_file_info));
3241 fi->fh_old = dh->fh;
3245 static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino,
3246 struct fuse_file_info *llfi)
3248 struct fuse *f = req_fuse_prepare(req);
3249 struct fuse_intr_data d;
3251 struct fuse_file_info fi;
3255 dh = (struct fuse_dh *) malloc(sizeof(struct fuse_dh));
3257 reply_err(req, -ENOMEM);
3260 memset(dh, 0, sizeof(struct fuse_dh));
3262 dh->contents = NULL;
3266 fuse_mutex_init(&dh->lock);
3268 llfi->fh = (uintptr_t) dh;
3270 memset(&fi, 0, sizeof(fi));
3271 fi.flags = llfi->flags;
3273 err = get_path(f, ino, &path);
3275 fuse_prepare_interrupt(f, req, &d);
3276 err = fuse_fs_opendir(f->fs, path, &fi);
3277 fuse_finish_interrupt(f, req, &d);
3281 if (fuse_reply_open(req, llfi) == -ENOENT) {
3282 /* The opendir syscall was interrupted, so it
3283 must be cancelled */
3284 fuse_fs_releasedir(f->fs, path, &fi);
3285 pthread_mutex_destroy(&dh->lock);
3289 reply_err(req, err);
3290 pthread_mutex_destroy(&dh->lock);
3293 free_path(f, ino, path);
3296 static int extend_contents(struct fuse_dh *dh, unsigned minsize)
3298 if (minsize > dh->size) {
3300 unsigned newsize = dh->size;
3303 while (newsize < minsize) {
3304 if (newsize >= 0x80000000)
3305 newsize = 0xffffffff;
3310 newptr = (char *) realloc(dh->contents, newsize);
3312 dh->error = -ENOMEM;
3315 dh->contents = newptr;
3321 static int fill_dir(void *dh_, const char *name, const struct stat *statp,
3324 struct fuse_dh *dh = (struct fuse_dh *) dh_;
3331 memset(&stbuf, 0, sizeof(stbuf));
3332 stbuf.st_ino = FUSE_UNKNOWN_INO;
3335 if (!dh->fuse->conf.use_ino) {
3336 stbuf.st_ino = FUSE_UNKNOWN_INO;
3337 if (dh->fuse->conf.readdir_ino) {
3339 pthread_mutex_lock(&dh->fuse->lock);
3340 node = lookup_node(dh->fuse, dh->nodeid, name);
3342 stbuf.st_ino = (ino_t) node->nodeid;
3343 pthread_mutex_unlock(&dh->fuse->lock);
3348 if (extend_contents(dh, dh->needlen) == -1)
3353 fuse_add_direntry(dh->req, dh->contents + dh->len,
3354 dh->needlen - dh->len, name,
3356 if (newlen > dh->needlen)
3360 fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0);
3361 if (extend_contents(dh, newlen) == -1)
3364 fuse_add_direntry(dh->req, dh->contents + dh->len,
3365 dh->size - dh->len, name, &stbuf, newlen);
3371 static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino,
3372 size_t size, off_t off, struct fuse_dh *dh,
3373 struct fuse_file_info *fi)
3378 if (f->fs->op.readdir)
3379 err = get_path_nullok(f, ino, &path);
3381 err = get_path(f, ino, &path);
3383 struct fuse_intr_data d;
3390 fuse_prepare_interrupt(f, req, &d);
3391 err = fuse_fs_readdir(f->fs, path, dh, fill_dir, off, fi);
3392 fuse_finish_interrupt(f, req, &d);
3398 free_path(f, ino, path);
3403 static void fuse_lib_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
3404 off_t off, struct fuse_file_info *llfi)
3406 struct fuse *f = req_fuse_prepare(req);
3407 struct fuse_file_info fi;
3408 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3410 pthread_mutex_lock(&dh->lock);
3411 /* According to SUS, directory contents need to be refreshed on
3417 int err = readdir_fill(f, req, ino, size, off, dh, &fi);
3419 reply_err(req, err);
3424 if (off < dh->len) {
3425 if (off + size > dh->len)
3426 size = dh->len - off;
3433 fuse_reply_buf(req, dh->contents + off, size);
3435 pthread_mutex_unlock(&dh->lock);
3438 static void fuse_lib_releasedir(fuse_req_t req, fuse_ino_t ino,
3439 struct fuse_file_info *llfi)
3441 struct fuse *f = req_fuse_prepare(req);
3442 struct fuse_intr_data d;
3443 struct fuse_file_info fi;
3444 struct fuse_dh *dh = get_dirhandle(llfi, &fi);
3446 const char *compatpath;
3448 get_path_nullok(f, ino, &path);
3449 if (path != NULL || f->nullpath_ok || f->conf.nopath)
3454 fuse_prepare_interrupt(f, req, &d);
3455 fuse_fs_releasedir(f->fs, compatpath, &fi);
3456 fuse_finish_interrupt(f, req, &d);
3457 free_path(f, ino, path);
3459 pthread_mutex_lock(&dh->lock);
3460 pthread_mutex_unlock(&dh->lock);
3461 pthread_mutex_destroy(&dh->lock);
3467 static void fuse_lib_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync,
3468 struct fuse_file_info *llfi)
3470 struct fuse *f = req_fuse_prepare(req);
3471 struct fuse_file_info fi;
3475 get_dirhandle(llfi, &fi);
3477 err = get_path_nullok(f, ino, &path);
3479 struct fuse_intr_data d;
3480 fuse_prepare_interrupt(f, req, &d);
3481 err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi);
3482 fuse_finish_interrupt(f, req, &d);
3483 free_path(f, ino, path);
3485 reply_err(req, err);
3488 static void fuse_lib_statfs(fuse_req_t req, fuse_ino_t ino)
3490 struct fuse *f = req_fuse_prepare(req);
3495 memset(&buf, 0, sizeof(buf));
3497 err = get_path(f, ino, &path);
3500 struct fuse_intr_data d;
3501 fuse_prepare_interrupt(f, req, &d);
3502 err = fuse_fs_statfs(f->fs, path ? path : "/", &buf);
3503 fuse_finish_interrupt(f, req, &d);
3504 free_path(f, ino, path);
3508 fuse_reply_statfs(req, &buf);
3510 reply_err(req, err);
3513 static void fuse_lib_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
3514 const char *value, size_t size, int flags)
3516 struct fuse *f = req_fuse_prepare(req);
3520 err = get_path(f, ino, &path);
3522 struct fuse_intr_data d;
3523 fuse_prepare_interrupt(f, req, &d);
3524 err = fuse_fs_setxattr(f->fs, path, name, value, size, flags);
3525 fuse_finish_interrupt(f, req, &d);
3526 free_path(f, ino, path);
3528 reply_err(req, err);
3531 static int common_getxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino,
3532 const char *name, char *value, size_t size)
3537 err = get_path(f, ino, &path);
3539 struct fuse_intr_data d;
3540 fuse_prepare_interrupt(f, req, &d);
3541 err = fuse_fs_getxattr(f->fs, path, name, value, size);
3542 fuse_finish_interrupt(f, req, &d);
3543 free_path(f, ino, path);
3548 static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
3551 struct fuse *f = req_fuse_prepare(req);
3555 char *value = (char *) malloc(size);
3556 if (value == NULL) {
3557 reply_err(req, -ENOMEM);
3560 res = common_getxattr(f, req, ino, name, value, size);
3562 fuse_reply_buf(req, value, res);
3564 reply_err(req, res);
3567 res = common_getxattr(f, req, ino, name, NULL, 0);
3569 fuse_reply_xattr(req, res);
3571 reply_err(req, res);
3575 static int common_listxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino,
3576 char *list, size_t size)
3581 err = get_path(f, ino, &path);
3583 struct fuse_intr_data d;
3584 fuse_prepare_interrupt(f, req, &d);
3585 err = fuse_fs_listxattr(f->fs, path, list, size);
3586 fuse_finish_interrupt(f, req, &d);
3587 free_path(f, ino, path);
3592 static void fuse_lib_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size)
3594 struct fuse *f = req_fuse_prepare(req);
3598 char *list = (char *) malloc(size);
3600 reply_err(req, -ENOMEM);
3603 res = common_listxattr(f, req, ino, list, size);
3605 fuse_reply_buf(req, list, res);
3607 reply_err(req, res);
3610 res = common_listxattr(f, req, ino, NULL, 0);
3612 fuse_reply_xattr(req, res);
3614 reply_err(req, res);
3618 static void fuse_lib_removexattr(fuse_req_t req, fuse_ino_t ino,
3621 struct fuse *f = req_fuse_prepare(req);
3625 err = get_path(f, ino, &path);
3627 struct fuse_intr_data d;
3628 fuse_prepare_interrupt(f, req, &d);
3629 err = fuse_fs_removexattr(f->fs, path, name);
3630 fuse_finish_interrupt(f, req, &d);
3631 free_path(f, ino, path);
3633 reply_err(req, err);
3636 static struct lock *locks_conflict(struct node *node, const struct lock *lock)
3640 for (l = node->locks; l; l = l->next)
3641 if (l->owner != lock->owner &&
3642 lock->start <= l->end && l->start <= lock->end &&
3643 (l->type == F_WRLCK || lock->type == F_WRLCK))
3649 static void delete_lock(struct lock **lockp)
3651 struct lock *l = *lockp;
3656 static void insert_lock(struct lock **pos, struct lock *lock)
3662 static int locks_insert(struct node *node, struct lock *lock)
3665 struct lock *newl1 = NULL;
3666 struct lock *newl2 = NULL;
3668 if (lock->type != F_UNLCK || lock->start != 0 ||
3669 lock->end != OFFSET_MAX) {
3670 newl1 = malloc(sizeof(struct lock));
3671 newl2 = malloc(sizeof(struct lock));
3673 if (!newl1 || !newl2) {
3680 for (lp = &node->locks; *lp;) {
3681 struct lock *l = *lp;
3682 if (l->owner != lock->owner)
3685 if (lock->type == l->type) {
3686 if (l->end < lock->start - 1)
3688 if (lock->end < l->start - 1)
3690 if (l->start <= lock->start && lock->end <= l->end)
3692 if (l->start < lock->start)
3693 lock->start = l->start;
3694 if (lock->end < l->end)
3698 if (l->end < lock->start)
3700 if (lock->end < l->start)
3702 if (lock->start <= l->start && l->end <= lock->end)
3704 if (l->end <= lock->end) {
3705 l->end = lock->start - 1;
3708 if (lock->start <= l->start) {
3709 l->start = lock->end + 1;
3713 newl2->start = lock->end + 1;
3714 l->end = lock->start - 1;
3715 insert_lock(&l->next, newl2);
3725 if (lock->type != F_UNLCK) {
3727 insert_lock(lp, newl1);
3736 static void flock_to_lock(struct flock *flock, struct lock *lock)
3738 memset(lock, 0, sizeof(struct lock));
3739 lock->type = flock->l_type;
3740 lock->start = flock->l_start;
3742 flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX;
3743 lock->pid = flock->l_pid;
3746 static void lock_to_flock(struct lock *lock, struct flock *flock)
3748 flock->l_type = lock->type;
3749 flock->l_start = lock->start;
3751 (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1;
3752 flock->l_pid = lock->pid;
3755 static int fuse_flush_common(struct fuse *f, fuse_req_t req, fuse_ino_t ino,
3756 const char *path, struct fuse_file_info *fi)
3758 struct fuse_intr_data d;
3764 fuse_prepare_interrupt(f, req, &d);
3765 memset(&lock, 0, sizeof(lock));
3766 lock.l_type = F_UNLCK;
3767 lock.l_whence = SEEK_SET;
3768 err = fuse_fs_flush(f->fs, path, fi);
3769 errlock = fuse_fs_lock(f->fs, path, fi, F_SETLK, &lock);
3770 fuse_finish_interrupt(f, req, &d);
3772 if (errlock != -ENOSYS) {
3773 flock_to_lock(&lock, &l);
3774 l.owner = fi->lock_owner;
3775 pthread_mutex_lock(&f->lock);
3776 locks_insert(get_node(f, ino), &l);
3777 pthread_mutex_unlock(&f->lock);
3779 /* if op.lock() is defined FLUSH is needed regardless
3787 static void fuse_lib_release(fuse_req_t req, fuse_ino_t ino,
3788 struct fuse_file_info *fi)
3790 struct fuse *f = req_fuse_prepare(req);
3791 struct fuse_intr_data d;
3795 get_path_nullok(f, ino, &path);
3797 err = fuse_flush_common(f, req, ino, path, fi);
3802 fuse_prepare_interrupt(f, req, &d);
3803 fuse_do_release(f, ino, path, fi);
3804 fuse_finish_interrupt(f, req, &d);
3805 free_path(f, ino, path);
3807 reply_err(req, err);
3810 static void fuse_lib_flush(fuse_req_t req, fuse_ino_t ino,
3811 struct fuse_file_info *fi)
3813 struct fuse *f = req_fuse_prepare(req);
3817 get_path_nullok(f, ino, &path);
3818 err = fuse_flush_common(f, req, ino, path, fi);
3819 free_path(f, ino, path);
3821 reply_err(req, err);
3824 static int fuse_lock_common(fuse_req_t req, fuse_ino_t ino,
3825 struct fuse_file_info *fi, struct flock *lock,
3828 struct fuse *f = req_fuse_prepare(req);
3832 err = get_path_nullok(f, ino, &path);
3834 struct fuse_intr_data d;
3835 fuse_prepare_interrupt(f, req, &d);
3836 err = fuse_fs_lock(f->fs, path, fi, cmd, lock);
3837 fuse_finish_interrupt(f, req, &d);
3838 free_path(f, ino, path);
3843 static void fuse_lib_getlk(fuse_req_t req, fuse_ino_t ino,
3844 struct fuse_file_info *fi, struct flock *lock)
3848 struct lock *conflict;
3849 struct fuse *f = req_fuse(req);
3851 flock_to_lock(lock, &l);
3852 l.owner = fi->lock_owner;
3853 pthread_mutex_lock(&f->lock);
3854 conflict = locks_conflict(get_node(f, ino), &l);
3856 lock_to_flock(conflict, lock);
3857 pthread_mutex_unlock(&f->lock);
3859 err = fuse_lock_common(req, ino, fi, lock, F_GETLK);
3864 fuse_reply_lock(req, lock);
3866 reply_err(req, err);
3869 static void fuse_lib_setlk(fuse_req_t req, fuse_ino_t ino,
3870 struct fuse_file_info *fi, struct flock *lock,
3873 int err = fuse_lock_common(req, ino, fi, lock,
3874 sleep ? F_SETLKW : F_SETLK);
3876 struct fuse *f = req_fuse(req);
3878 flock_to_lock(lock, &l);
3879 l.owner = fi->lock_owner;
3880 pthread_mutex_lock(&f->lock);
3881 locks_insert(get_node(f, ino), &l);
3882 pthread_mutex_unlock(&f->lock);
3884 reply_err(req, err);
3887 static void fuse_lib_flock(fuse_req_t req, fuse_ino_t ino,
3888 struct fuse_file_info *fi, int op)
3890 struct fuse *f = req_fuse_prepare(req);
3894 err = get_path_nullok(f, ino, &path);
3896 struct fuse_intr_data d;
3897 fuse_prepare_interrupt(f, req, &d);
3898 err = fuse_fs_flock(f->fs, path, fi, op);
3899 fuse_finish_interrupt(f, req, &d);
3900 free_path(f, ino, path);
3902 reply_err(req, err);
3905 static void fuse_lib_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize,
3908 struct fuse *f = req_fuse_prepare(req);
3909 struct fuse_intr_data d;
3913 err = get_path(f, ino, &path);
3915 fuse_prepare_interrupt(f, req, &d);
3916 err = fuse_fs_bmap(f->fs, path, blocksize, &idx);
3917 fuse_finish_interrupt(f, req, &d);
3918 free_path(f, ino, path);
3921 fuse_reply_bmap(req, idx);
3923 reply_err(req, err);
3926 static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg,
3927 struct fuse_file_info *fi, unsigned int flags,
3928 const void *in_buf, size_t in_bufsz,
3931 struct fuse *f = req_fuse_prepare(req);
3932 struct fuse_intr_data d;
3933 char *path, *out_buf = NULL;
3937 if (flags & FUSE_IOCTL_UNRESTRICTED)
3942 out_buf = malloc(out_bufsz);
3947 assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz);
3949 memcpy(out_buf, in_buf, in_bufsz);
3951 err = get_path_nullok(f, ino, &path);
3955 fuse_prepare_interrupt(f, req, &d);
3957 err = fuse_fs_ioctl(f->fs, path, cmd, arg, fi, flags,
3958 out_buf ?: (void *)in_buf);
3960 fuse_finish_interrupt(f, req, &d);
3961 free_path(f, ino, path);
3963 fuse_reply_ioctl(req, err, out_buf, out_bufsz);
3966 reply_err(req, err);
3971 static void fuse_lib_poll(fuse_req_t req, fuse_ino_t ino,
3972 struct fuse_file_info *fi, struct fuse_pollhandle *ph)
3974 struct fuse *f = req_fuse_prepare(req);
3975 struct fuse_intr_data d;
3978 unsigned revents = 0;
3980 err = get_path_nullok(f, ino, &path);
3982 fuse_prepare_interrupt(f, req, &d);
3983 err = fuse_fs_poll(f->fs, path, fi, ph, &revents);
3984 fuse_finish_interrupt(f, req, &d);
3985 free_path(f, ino, path);
3988 fuse_reply_poll(req, revents);
3990 reply_err(req, err);
3993 static int clean_delay(struct fuse *f)
3996 * This is calculating the delay between clean runs. To
3997 * reduce the number of cleans we are doing them 10 times
3998 * within the remember window.
4001 int max_sleep = 3600;
4002 int sleep_time = f->conf.remember / 10;
4004 if (sleep_time > max_sleep)
4006 if (sleep_time < min_sleep)
4011 int fuse_clean_cache(struct fuse *f)
4013 struct node_lru *lnode;
4014 struct list_head *curr, *next;
4016 struct timespec now;
4018 pthread_mutex_lock(&f->lock);
4022 for (curr = f->lru_table.next; curr != &f->lru_table; curr = next) {
4026 lnode = list_entry(curr, struct node_lru, lru);
4027 node = &lnode->node;
4029 age = diff_timespec(&now, &lnode->forget_time);
4030 if (age <= f->conf.remember)
4033 assert(node->nlookup == 1);
4035 /* Don't forget active directories */
4036 if (node->refctr > 1)
4040 unhash_name(f, node);
4041 unref_node(f, node);
4043 pthread_mutex_unlock(&f->lock);
4045 return clean_delay(f);
4048 static struct fuse_lowlevel_ops fuse_path_ops = {
4049 .init = fuse_lib_init,
4050 .destroy = fuse_lib_destroy,
4051 .lookup = fuse_lib_lookup,
4052 .forget = fuse_lib_forget,
4053 .forget_multi = fuse_lib_forget_multi,
4054 .getattr = fuse_lib_getattr,
4055 .setattr = fuse_lib_setattr,
4056 .access = fuse_lib_access,
4057 .readlink = fuse_lib_readlink,
4058 .mknod = fuse_lib_mknod,
4059 .mkdir = fuse_lib_mkdir,
4060 .unlink = fuse_lib_unlink,
4061 .rmdir = fuse_lib_rmdir,
4062 .symlink = fuse_lib_symlink,
4063 .rename = fuse_lib_rename,
4064 .link = fuse_lib_link,
4065 .create = fuse_lib_create,
4066 .open = fuse_lib_open,
4067 .read = fuse_lib_read,
4068 .write_buf = fuse_lib_write_buf,
4069 .flush = fuse_lib_flush,
4070 .release = fuse_lib_release,
4071 .fsync = fuse_lib_fsync,
4072 .opendir = fuse_lib_opendir,
4073 .readdir = fuse_lib_readdir,
4074 .releasedir = fuse_lib_releasedir,
4075 .fsyncdir = fuse_lib_fsyncdir,
4076 .statfs = fuse_lib_statfs,
4077 .setxattr = fuse_lib_setxattr,
4078 .getxattr = fuse_lib_getxattr,
4079 .listxattr = fuse_lib_listxattr,
4080 .removexattr = fuse_lib_removexattr,
4081 .getlk = fuse_lib_getlk,
4082 .setlk = fuse_lib_setlk,
4083 .flock = fuse_lib_flock,
4084 .bmap = fuse_lib_bmap,
4085 .ioctl = fuse_lib_ioctl,
4086 .poll = fuse_lib_poll,
4089 int fuse_notify_poll(struct fuse_pollhandle *ph)
4091 return fuse_lowlevel_notify_poll(ph);
4094 static void free_cmd(struct fuse_cmd *cmd)
4100 void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
4102 fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch);
4106 int fuse_exited(struct fuse *f)
4108 return fuse_session_exited(f->se);
4111 struct fuse_session *fuse_get_session(struct fuse *f)
4116 static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize)
4118 struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd));
4120 fprintf(stderr, "fuse: failed to allocate cmd\n");
4123 cmd->buf = (char *) malloc(bufsize);
4124 if (cmd->buf == NULL) {
4125 fprintf(stderr, "fuse: failed to allocate read buffer\n");
4132 struct fuse_cmd *fuse_read_cmd(struct fuse *f)
4134 struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL);
4135 size_t bufsize = fuse_chan_bufsize(ch);
4136 struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize);
4138 int res = fuse_chan_recv(&ch, cmd->buf, bufsize);
4141 if (res < 0 && res != -EINTR && res != -EAGAIN)
4151 static int fuse_session_loop_remember(struct fuse *f)
4153 struct fuse_session *se = f->se;
4155 struct timespec now;
4157 struct fuse_chan *ch = fuse_session_next_chan(se, NULL);
4158 size_t bufsize = fuse_chan_bufsize(ch);
4159 char *buf = (char *) malloc(bufsize);
4160 struct pollfd fds = {
4161 .fd = fuse_chan_fd(ch),
4166 fprintf(stderr, "fuse: failed to allocate read buffer\n");
4171 next_clean = now.tv_sec;
4172 while (!fuse_session_exited(se)) {
4173 struct fuse_chan *tmpch = ch;
4174 struct fuse_buf fbuf = {
4181 if (now.tv_sec < next_clean)
4182 timeout = next_clean - now.tv_sec;
4186 res = poll(&fds, 1, timeout * 1000);
4188 if (errno == -EINTR)
4192 } else if (res > 0) {
4193 res = fuse_session_receive_buf(se, &fbuf, &tmpch);
4200 fuse_session_process_buf(se, &fbuf, tmpch);
4202 timeout = fuse_clean_cache(f);
4204 next_clean = now.tv_sec + timeout;
4209 fuse_session_reset(se);
4210 return res < 0 ? -1 : 0;
4213 int fuse_loop(struct fuse *f)
4219 return fuse_session_loop_remember(f);
4221 return fuse_session_loop(f->se);
4224 int fuse_invalidate(struct fuse *f, const char *path)
4231 void fuse_exit(struct fuse *f)
4233 fuse_session_exit(f->se);
4236 struct fuse_context *fuse_get_context(void)
4238 return &fuse_get_context_internal()->ctx;
4242 * The size of fuse_context got extended, so need to be careful about
4243 * incompatibility (i.e. a new binary cannot work with an old
4246 struct fuse_context *fuse_get_context_compat22(void);
4247 struct fuse_context *fuse_get_context_compat22(void)
4249 return &fuse_get_context_internal()->ctx;
4251 FUSE_SYMVER(".symver fuse_get_context_compat22,fuse_get_context@FUSE_2.2");
4253 int fuse_getgroups(int size, gid_t list[])
4255 fuse_req_t req = fuse_get_context_internal()->req;
4256 return fuse_req_getgroups(req, size, list);
4259 int fuse_interrupted(void)
4261 return fuse_req_interrupted(fuse_get_context_internal()->req);
4264 void fuse_set_getcontext_func(struct fuse_context *(*func)(void))
4274 #define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v }
4276 static const struct fuse_opt fuse_lib_opts[] = {
4277 FUSE_OPT_KEY("-h", KEY_HELP),
4278 FUSE_OPT_KEY("--help", KEY_HELP),
4279 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
4280 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
4281 FUSE_LIB_OPT("debug", debug, 1),
4282 FUSE_LIB_OPT("-d", debug, 1),
4283 FUSE_LIB_OPT("hard_remove", hard_remove, 1),
4284 FUSE_LIB_OPT("use_ino", use_ino, 1),
4285 FUSE_LIB_OPT("readdir_ino", readdir_ino, 1),
4286 FUSE_LIB_OPT("direct_io", direct_io, 1),
4287 FUSE_LIB_OPT("kernel_cache", kernel_cache, 1),
4288 FUSE_LIB_OPT("auto_cache", auto_cache, 1),
4289 FUSE_LIB_OPT("noauto_cache", auto_cache, 0),
4290 FUSE_LIB_OPT("umask=", set_mode, 1),
4291 FUSE_LIB_OPT("umask=%o", umask, 0),
4292 FUSE_LIB_OPT("uid=", set_uid, 1),
4293 FUSE_LIB_OPT("uid=%d", uid, 0),
4294 FUSE_LIB_OPT("gid=", set_gid, 1),
4295 FUSE_LIB_OPT("gid=%d", gid, 0),
4296 FUSE_LIB_OPT("entry_timeout=%lf", entry_timeout, 0),
4297 FUSE_LIB_OPT("attr_timeout=%lf", attr_timeout, 0),
4298 FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0),
4299 FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1),
4300 FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0),
4301 FUSE_LIB_OPT("noforget", remember, -1),
4302 FUSE_LIB_OPT("remember=%u", remember, 0),
4303 FUSE_LIB_OPT("nopath", nopath, 1),
4304 FUSE_LIB_OPT("intr", intr, 1),
4305 FUSE_LIB_OPT("intr_signal=%d", intr_signal, 0),
4306 FUSE_LIB_OPT("modules=%s", modules, 0),
4310 static void fuse_lib_help(void)
4313 " -o hard_remove immediate removal (don't hide files)\n"
4314 " -o use_ino let filesystem set inode numbers\n"
4315 " -o readdir_ino try to fill in d_ino in readdir\n"
4316 " -o direct_io use direct I/O\n"
4317 " -o kernel_cache cache files in kernel\n"
4318 " -o [no]auto_cache enable caching based on modification times (off)\n"
4319 " -o umask=M set file permissions (octal)\n"
4320 " -o uid=N set file owner\n"
4321 " -o gid=N set file group\n"
4322 " -o entry_timeout=T cache timeout for names (1.0s)\n"
4323 " -o negative_timeout=T cache timeout for deleted names (0.0s)\n"
4324 " -o attr_timeout=T cache timeout for attributes (1.0s)\n"
4325 " -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n"
4326 " -o noforget never forget cached inodes\n"
4327 " -o remember=T remember cached inodes for T seconds (0s)\n"
4328 " -o intr allow requests to be interrupted\n"
4329 " -o intr_signal=NUM signal to send on interrupt (%i)\n"
4330 " -o modules=M1[:M2...] names of modules to push onto filesystem stack\n"
4331 "\n", FUSE_DEFAULT_INTR_SIGNAL);
4334 static void fuse_lib_help_modules(void)
4336 struct fuse_module *m;
4337 fprintf(stderr, "\nModule options:\n");
4338 pthread_mutex_lock(&fuse_context_lock);
4339 for (m = fuse_modules; m; m = m->next) {
4340 struct fuse_fs *fs = NULL;
4341 struct fuse_fs *newfs;
4342 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
4343 if (fuse_opt_add_arg(&args, "") != -1 &&
4344 fuse_opt_add_arg(&args, "-h") != -1) {
4345 fprintf(stderr, "\n[%s]\n", m->name);
4346 newfs = m->factory(&args, &fs);
4347 assert(newfs == NULL);
4349 fuse_opt_free_args(&args);
4351 pthread_mutex_unlock(&fuse_context_lock);
4354 static int fuse_lib_opt_proc(void *data, const char *arg, int key,
4355 struct fuse_args *outargs)
4357 (void) arg; (void) outargs;
4359 if (key == KEY_HELP) {
4360 struct fuse_config *conf = (struct fuse_config *) data;
4368 int fuse_is_lib_option(const char *opt)
4370 return fuse_lowlevel_is_lib_option(opt) ||
4371 fuse_opt_match(fuse_lib_opts, opt);
4374 static int fuse_init_intr_signal(int signum, int *installed)
4376 struct sigaction old_sa;
4378 if (sigaction(signum, NULL, &old_sa) == -1) {
4379 perror("fuse: cannot get old signal handler");
4383 if (old_sa.sa_handler == SIG_DFL) {
4384 struct sigaction sa;
4386 memset(&sa, 0, sizeof(struct sigaction));
4387 sa.sa_handler = fuse_intr_sighandler;
4388 sigemptyset(&sa.sa_mask);
4390 if (sigaction(signum, &sa, NULL) == -1) {
4391 perror("fuse: cannot set interrupt signal handler");
4399 static void fuse_restore_intr_signal(int signum)
4401 struct sigaction sa;
4403 memset(&sa, 0, sizeof(struct sigaction));
4404 sa.sa_handler = SIG_DFL;
4405 sigaction(signum, &sa, NULL);
4409 static int fuse_push_module(struct fuse *f, const char *module,
4410 struct fuse_args *args)
4412 struct fuse_fs *fs[2] = { f->fs, NULL };
4413 struct fuse_fs *newfs;
4414 struct fuse_module *m = fuse_get_module(module);
4419 newfs = m->factory(args, fs);
4426 f->nullpath_ok = newfs->op.flag_nullpath_ok && f->nullpath_ok;
4427 f->conf.nopath = newfs->op.flag_nopath && f->conf.nopath;
4428 f->utime_omit_ok = newfs->op.flag_utime_omit_ok && f->utime_omit_ok;
4432 struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size,
4437 if (sizeof(struct fuse_operations) < op_size) {
4438 fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n");
4439 op_size = sizeof(struct fuse_operations);
4442 fs = (struct fuse_fs *) calloc(1, sizeof(struct fuse_fs));
4444 fprintf(stderr, "fuse: failed to allocate fuse_fs object\n");
4448 fs->user_data = user_data;
4450 memcpy(&fs->op, op, op_size);
4454 static int node_table_init(struct node_table *t)
4456 t->size = NODE_TABLE_MIN_SIZE;
4457 t->array = (struct node **) calloc(1, sizeof(struct node *) * t->size);
4458 if (t->array == NULL) {
4459 fprintf(stderr, "fuse: memory allocation failed\n");
4468 static void *fuse_prune_nodes(void *fuse)
4470 struct fuse *f = fuse;
4474 sleep_time = fuse_clean_cache(f);
4480 int fuse_start_cleanup_thread(struct fuse *f)
4483 return fuse_start_thread(&f->prune_thread, fuse_prune_nodes, f);
4488 void fuse_stop_cleanup_thread(struct fuse *f)
4490 if (lru_enabled(f)) {
4491 pthread_mutex_lock(&f->lock);
4492 pthread_cancel(f->prune_thread);
4493 pthread_mutex_unlock(&f->lock);
4494 pthread_join(f->prune_thread, NULL);
4498 struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args,
4499 const struct fuse_operations *op,
4500 size_t op_size, void *user_data, int compat)
4505 struct fuse_lowlevel_ops llop = fuse_path_ops;
4507 if (fuse_create_context_key() == -1)
4510 f = (struct fuse *) calloc(1, sizeof(struct fuse));
4512 fprintf(stderr, "fuse: failed to allocate fuse object\n");
4513 goto out_delete_context_key;
4516 fs = fuse_fs_new(op, op_size, user_data);
4520 fs->compat = compat;
4522 f->nullpath_ok = fs->op.flag_nullpath_ok;
4523 f->conf.nopath = fs->op.flag_nopath;
4524 f->utime_omit_ok = fs->op.flag_utime_omit_ok;
4526 /* Oh f**k, this is ugly! */
4532 f->conf.entry_timeout = 1.0;
4533 f->conf.attr_timeout = 1.0;
4534 f->conf.negative_timeout = 0.0;
4535 f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL;
4537 f->pagesize = getpagesize();
4538 init_list_head(&f->partial_slabs);
4539 init_list_head(&f->full_slabs);
4540 init_list_head(&f->lru_table);
4542 if (fuse_opt_parse(args, &f->conf, fuse_lib_opts,
4543 fuse_lib_opt_proc) == -1)
4546 if (f->conf.modules) {
4550 for (module = f->conf.modules; module; module = next) {
4552 for (p = module; *p && *p != ':'; p++);
4553 next = *p ? p + 1 : NULL;
4556 fuse_push_module(f, module, args) == -1)
4561 if (!f->conf.ac_attr_timeout_set)
4562 f->conf.ac_attr_timeout = f->conf.attr_timeout;
4564 #if defined(__FreeBSD__) || defined(__NetBSD__)
4566 * In FreeBSD, we always use these settings as inode numbers
4567 * are needed to make getcwd(3) work.
4569 f->conf.readdir_ino = 1;
4572 if (compat && compat <= 25) {
4573 if (fuse_sync_compat_args(args) == -1)
4577 f->se = fuse_lowlevel_new_common(args, &llop, sizeof(llop), f);
4578 if (f->se == NULL) {
4580 fuse_lib_help_modules();
4584 fuse_session_add_chan(f->se, ch);
4586 if (f->conf.debug) {
4587 fprintf(stderr, "nullpath_ok: %i\n", f->nullpath_ok);
4588 fprintf(stderr, "nopath: %i\n", f->conf.nopath);
4589 fprintf(stderr, "utime_omit_ok: %i\n", f->utime_omit_ok);
4592 /* Trace topmost layer by default */
4593 f->fs->debug = f->conf.debug;
4596 if (node_table_init(&f->name_table) == -1)
4597 goto out_free_session;
4599 if (node_table_init(&f->id_table) == -1)
4600 goto out_free_name_table;
4602 fuse_mutex_init(&f->lock);
4604 root = alloc_node(f);
4606 fprintf(stderr, "fuse: memory allocation failed\n");
4607 goto out_free_id_table;
4610 strcpy(root->inline_name, "/");
4611 root->name = root->inline_name;
4614 fuse_init_intr_signal(f->conf.intr_signal,
4615 &f->intr_installed) == -1)
4618 root->parent = NULL;
4619 root->nodeid = FUSE_ROOT_ID;
4620 root->generation = 0;
4630 free(f->id_table.array);
4631 out_free_name_table:
4632 free(f->name_table.array);
4634 fuse_session_destroy(f->se);
4636 /* Horrible compatibility hack to stop the destructor from being
4637 called on the filesystem without init being called first */
4638 fs->op.destroy = NULL;
4639 fuse_fs_destroy(f->fs);
4640 free(f->conf.modules);
4643 out_delete_context_key:
4644 fuse_delete_context_key();
4649 struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args,
4650 const struct fuse_operations *op, size_t op_size,
4653 return fuse_new_common(ch, args, op, op_size, user_data, 0);
4656 void fuse_destroy(struct fuse *f)
4660 if (f->conf.intr && f->intr_installed)
4661 fuse_restore_intr_signal(f->conf.intr_signal);
4664 struct fuse_context_i *c = fuse_get_context_internal();
4666 memset(c, 0, sizeof(*c));
4669 for (i = 0; i < f->id_table.size; i++) {
4672 for (node = f->id_table.array[i]; node != NULL;
4673 node = node->id_next) {
4674 if (node->is_hidden) {
4676 if (try_get_path(f, node->nodeid, NULL, &path, NULL, 0) == 0) {
4677 fuse_fs_unlink(f->fs, path);
4684 for (i = 0; i < f->id_table.size; i++) {
4688 for (node = f->id_table.array[i]; node != NULL; node = next) {
4689 next = node->id_next;
4694 assert(list_empty(&f->partial_slabs));
4695 assert(list_empty(&f->full_slabs));
4697 free(f->id_table.array);
4698 free(f->name_table.array);
4699 pthread_mutex_destroy(&f->lock);
4700 fuse_session_destroy(f->se);
4701 free(f->conf.modules);
4703 fuse_delete_context_key();
4706 static struct fuse *fuse_new_common_compat25(int fd, struct fuse_args *args,
4707 const struct fuse_operations *op,
4708 size_t op_size, int compat)
4710 struct fuse *f = NULL;
4711 struct fuse_chan *ch = fuse_kern_chan_new(fd);
4714 f = fuse_new_common(ch, args, op, op_size, NULL, compat);
4719 /* called with fuse_context_lock held or during initialization (before
4720 main() has been called) */
4721 void fuse_register_module(struct fuse_module *mod)
4724 mod->so = fuse_current_so;
4727 mod->next = fuse_modules;
4731 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
4733 static struct fuse *fuse_new_common_compat(int fd, const char *opts,
4734 const struct fuse_operations *op,
4735 size_t op_size, int compat)
4738 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
4740 if (fuse_opt_add_arg(&args, "") == -1)
4743 (fuse_opt_add_arg(&args, "-o") == -1 ||
4744 fuse_opt_add_arg(&args, opts) == -1)) {
4745 fuse_opt_free_args(&args);
4748 f = fuse_new_common_compat25(fd, &args, op, op_size, compat);
4749 fuse_opt_free_args(&args);
4754 struct fuse *fuse_new_compat22(int fd, const char *opts,
4755 const struct fuse_operations_compat22 *op,
4758 return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
4762 struct fuse *fuse_new_compat2(int fd, const char *opts,
4763 const struct fuse_operations_compat2 *op)
4765 return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
4766 sizeof(struct fuse_operations_compat2),
4770 struct fuse *fuse_new_compat1(int fd, int flags,
4771 const struct fuse_operations_compat1 *op)
4773 const char *opts = NULL;
4774 if (flags & FUSE_DEBUG_COMPAT1)
4776 return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op,
4777 sizeof(struct fuse_operations_compat1),
4781 FUSE_SYMVER(".symver fuse_exited,__fuse_exited@");
4782 FUSE_SYMVER(".symver fuse_process_cmd,__fuse_process_cmd@");
4783 FUSE_SYMVER(".symver fuse_read_cmd,__fuse_read_cmd@");
4784 FUSE_SYMVER(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@");
4785 FUSE_SYMVER(".symver fuse_new_compat2,fuse_new@");
4786 FUSE_SYMVER(".symver fuse_new_compat22,fuse_new@FUSE_2.2");
4788 #endif /* __FreeBSD__ || __NetBSD__ */
4790 struct fuse *fuse_new_compat25(int fd, struct fuse_args *args,
4791 const struct fuse_operations_compat25 *op,
4794 return fuse_new_common_compat25(fd, args, (struct fuse_operations *) op,
4798 FUSE_SYMVER(".symver fuse_new_compat25,fuse_new@FUSE_2.5");