diff options
Diffstat (limited to '')
-rw-r--r-- | fuse/fuse.c | 1449 |
1 files changed, 1181 insertions, 268 deletions
diff --git a/fuse/fuse.c b/fuse/fuse.c index 98170cf9a..34b11d47a 100644 --- a/fuse/fuse.c +++ b/fuse/fuse.c @@ -10,6 +10,7 @@ /* For pthread_rwlock_t */ #define _GNU_SOURCE +#include "config.h" #include "fuse_i.h" #include "fuse_lowlevel.h" #include "fuse_opt.h" @@ -22,6 +23,7 @@ #include <string.h> #include <stdlib.h> #include <stddef.h> +#include <stdbool.h> #include <unistd.h> #include <time.h> #include <fcntl.h> @@ -30,15 +32,25 @@ #include <signal.h> #include <dlfcn.h> #include <assert.h> +#include <poll.h> #include <sys/param.h> #include <sys/uio.h> #include <sys/time.h> +#include <sys/mman.h> + +#define FUSE_NODE_SLAB 1 + +#ifndef MAP_ANONYMOUS +#undef FUSE_NODE_SLAB +#endif #define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 #define FUSE_UNKNOWN_INO 0xffffffff #define OFFSET_MAX 0x7fffffffffffffffLL +#define NODE_TABLE_MIN_SIZE 8192 + struct fuse_config { unsigned int uid; unsigned int gid; @@ -48,7 +60,8 @@ struct fuse_config { double attr_timeout; double ac_attr_timeout; int ac_attr_timeout_set; - int noforget; + int remember; + int nopath; int debug; int hard_remove; int use_ino; @@ -79,16 +92,52 @@ struct fusemod_so { }; struct lock_queue_element { - struct lock_queue_element *next; - pthread_cond_t cond; + struct lock_queue_element *next; + pthread_cond_t cond; + fuse_ino_t nodeid1; + const char *name1; + char **path1; + struct node **wnode1; + fuse_ino_t nodeid2; + const char *name2; + char **path2; + struct node **wnode2; + int err; + bool first_locked : 1; + bool second_locked : 1; + bool done : 1; +}; + +struct node_table { + struct node **array; + size_t use; + size_t size; + size_t split; +}; + +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +struct list_head { + struct list_head *next; + struct list_head *prev; +}; + +struct node_slab { + struct list_head list; /* must be the first member */ + struct list_head freelist; + int used; }; struct fuse { struct fuse_session *se; - struct node **name_table; - size_t name_table_size; - struct node **id_table; - size_t id_table_size; + struct node_table name_table; + struct node_table id_table; + struct list_head lru_table; fuse_ino_t ctr; unsigned int generation; unsigned int hidectr; @@ -97,8 +146,12 @@ struct fuse { int intr_installed; struct fuse_fs *fs; int nullpath_ok; - int curr_ticket; + int utime_omit_ok; struct lock_queue_element *lockq; + int pagesize; + struct list_head partial_slabs; + struct list_head full_slabs; + pthread_t prune_thread; }; struct lock { @@ -127,7 +180,16 @@ struct node { unsigned int is_hidden : 1; unsigned int cache_valid : 1; int treelock; - int ticket; + char inline_name[32]; +}; + +#define TREELOCK_WRITE -1 +#define TREELOCK_WAIT_OFFSET INT_MIN + +struct node_lru { + struct node node; + struct list_head lru; + struct timespec forget_time; }; struct fuse_dh { @@ -258,12 +320,185 @@ static void fuse_put_module(struct fuse_module *m) pthread_mutex_unlock(&fuse_context_lock); } +static void init_list_head(struct list_head *list) +{ + list->next = list; + list->prev = list; +} + +static int list_empty(const struct list_head *head) +{ + return head->next == head; +} + +static void list_add(struct list_head *new, struct list_head *prev, + struct list_head *next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +static inline void list_add_head(struct list_head *new, struct list_head *head) +{ + list_add(new, head, head->next); +} + +static inline void list_add_tail(struct list_head *new, struct list_head *head) +{ + list_add(new, head->prev, head); +} + +static inline void list_del(struct list_head *entry) +{ + struct list_head *prev = entry->prev; + struct list_head *next = entry->next; + + next->prev = prev; + prev->next = next; +} + +static inline int lru_enabled(struct fuse *f) +{ + return f->conf.remember > 0; +} + +static struct node_lru *node_lru(struct node *node) +{ + return (struct node_lru *) node; +} + +static size_t get_node_size(struct fuse *f) +{ + if (lru_enabled(f)) + return sizeof(struct node_lru); + else + return sizeof(struct node); +} + +#ifdef FUSE_NODE_SLAB +static struct node_slab *list_to_slab(struct list_head *head) +{ + return (struct node_slab *) head; +} + +static struct node_slab *node_to_slab(struct fuse *f, struct node *node) +{ + return (struct node_slab *) (((uintptr_t) node) & ~((uintptr_t) f->pagesize - 1)); +} + +static int alloc_slab(struct fuse *f) +{ + void *mem; + struct node_slab *slab; + char *start; + size_t num; + size_t i; + size_t node_size = get_node_size(f); + + mem = mmap(NULL, f->pagesize, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (mem == MAP_FAILED) + return -1; + + slab = mem; + init_list_head(&slab->freelist); + slab->used = 0; + num = (f->pagesize - sizeof(struct node_slab)) / node_size; + + start = (char *) mem + f->pagesize - num * node_size; + for (i = 0; i < num; i++) { + struct list_head *n; + + n = (struct list_head *) (start + i * node_size); + list_add_tail(n, &slab->freelist); + } + list_add_tail(&slab->list, &f->partial_slabs); + + return 0; +} + +static struct node *alloc_node(struct fuse *f) +{ + struct node_slab *slab; + struct list_head *node; + + if (list_empty(&f->partial_slabs)) { + int res = alloc_slab(f); + if (res != 0) + return NULL; + } + slab = list_to_slab(f->partial_slabs.next); + slab->used++; + node = slab->freelist.next; + list_del(node); + if (list_empty(&slab->freelist)) { + list_del(&slab->list); + list_add_tail(&slab->list, &f->full_slabs); + } + memset(node, 0, sizeof(struct node)); + + return (struct node *) node; +} + +static void free_slab(struct fuse *f, struct node_slab *slab) +{ + int res; + + list_del(&slab->list); + res = munmap(slab, f->pagesize); + if (res == -1) + fprintf(stderr, "fuse warning: munmap(%p) failed\n", slab); +} + +static void free_node_mem(struct fuse *f, struct node *node) +{ + struct node_slab *slab = node_to_slab(f, node); + struct list_head *n = (struct list_head *) node; + + slab->used--; + if (slab->used) { + if (list_empty(&slab->freelist)) { + list_del(&slab->list); + list_add_tail(&slab->list, &f->partial_slabs); + } + list_add_head(n, &slab->freelist); + } else { + free_slab(f, slab); + } +} +#else +static struct node *alloc_node(struct fuse *f) +{ + return (struct node *) calloc(1, get_node_size(f)); +} + +static void free_node_mem(struct fuse *f, struct node *node) +{ + (void) f; + free(node); +} +#endif + +static size_t id_hash(struct fuse *f, fuse_ino_t ino) +{ + uint64_t hash = ((uint32_t) ino * 2654435761U) % f->id_table.size; + uint64_t oldhash = hash % (f->id_table.size / 2); + + if (oldhash >= f->id_table.split) + return oldhash; + else + return hash; +} + static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid) { - size_t hash = nodeid % f->id_table_size; + size_t hash = id_hash(f, nodeid); struct node *node; - for (node = f->id_table[hash]; node != NULL; node = node->id_next) + for (node = f->id_table.array[hash]; node != NULL; node = node->id_next) if (node->nodeid == nodeid) return node; @@ -281,59 +516,211 @@ static struct node *get_node(struct fuse *f, fuse_ino_t nodeid) return node; } -static void free_node(struct node *node) +static void curr_time(struct timespec *now); +static double diff_timespec(const struct timespec *t1, + const struct timespec *t2); + +static void remove_node_lru(struct node *node) { - free(node->name); - free(node); + struct node_lru *lnode = node_lru(node); + list_del(&lnode->lru); + init_list_head(&lnode->lru); +} + +static void set_forget_time(struct fuse *f, struct node *node) +{ + struct node_lru *lnode = node_lru(node); + + list_del(&lnode->lru); + list_add_tail(&lnode->lru, &f->lru_table); + curr_time(&lnode->forget_time); +} + +static void free_node(struct fuse *f, struct node *node) +{ + if (node->name != node->inline_name) + free(node->name); + free_node_mem(f, node); +} + +static void node_table_reduce(struct node_table *t) +{ + size_t newsize = t->size / 2; + void *newarray; + + if (newsize < NODE_TABLE_MIN_SIZE) + return; + + newarray = realloc(t->array, sizeof(struct node *) * newsize); + if (newarray != NULL) + t->array = newarray; + + t->size = newsize; + t->split = t->size / 2; +} + +static void remerge_id(struct fuse *f) +{ + struct node_table *t = &f->id_table; + int iter; + + if (t->split == 0) + node_table_reduce(t); + + for (iter = 8; t->split > 0 && iter; iter--) { + struct node **upper; + + t->split--; + upper = &t->array[t->split + t->size / 2]; + if (*upper) { + struct node **nodep; + + for (nodep = &t->array[t->split]; *nodep; + nodep = &(*nodep)->id_next); + + *nodep = *upper; + *upper = NULL; + break; + } + } } static void unhash_id(struct fuse *f, struct node *node) { - size_t hash = node->nodeid % f->id_table_size; - struct node **nodep = &f->id_table[hash]; + struct node **nodep = &f->id_table.array[id_hash(f, node->nodeid)]; for (; *nodep != NULL; nodep = &(*nodep)->id_next) if (*nodep == node) { *nodep = node->id_next; + f->id_table.use--; + + if(f->id_table.use < f->id_table.size / 4) + remerge_id(f); return; } } +static int node_table_resize(struct node_table *t) +{ + size_t newsize = t->size * 2; + void *newarray; + + newarray = realloc(t->array, sizeof(struct node *) * newsize); + if (newarray == NULL) + return -1; + + t->array = newarray; + memset(t->array + t->size, 0, t->size * sizeof(struct node *)); + t->size = newsize; + t->split = 0; + + return 0; +} + +static void rehash_id(struct fuse *f) +{ + struct node_table *t = &f->id_table; + struct node **nodep; + struct node **next; + size_t hash; + + if (t->split == t->size / 2) + return; + + hash = t->split; + t->split++; + for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) { + struct node *node = *nodep; + size_t newhash = id_hash(f, node->nodeid); + + if (newhash != hash) { + next = nodep; + *nodep = node->id_next; + node->id_next = t->array[newhash]; + t->array[newhash] = node; + } else { + next = &node->id_next; + } + } + if (t->split == t->size / 2) + node_table_resize(t); +} + static void hash_id(struct fuse *f, struct node *node) { - size_t hash = node->nodeid % f->id_table_size; - node->id_next = f->id_table[hash]; - f->id_table[hash] = node; + size_t hash = id_hash(f, node->nodeid); + node->id_next = f->id_table.array[hash]; + f->id_table.array[hash] = node; + f->id_table.use++; + + if (f->id_table.use >= f->id_table.size / 2) + rehash_id(f); } -static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, - const char *name) +static size_t name_hash(struct fuse *f, fuse_ino_t parent, + const char *name) { - unsigned int hash = *name; + uint64_t hash = parent; + uint64_t oldhash; - if (hash) - for (name += 1; *name != '\0'; name++) - hash = (hash << 5) - hash + *name; + for (; *name; name++) + hash = hash * 31 + (unsigned char) *name; - return (hash + parent) % f->name_table_size; + hash %= f->name_table.size; + oldhash = hash % (f->name_table.size / 2); + if (oldhash >= f->name_table.split) + return oldhash; + else + return hash; } static void unref_node(struct fuse *f, struct node *node); +static void remerge_name(struct fuse *f) +{ + struct node_table *t = &f->name_table; + int iter; + + if (t->split == 0) + node_table_reduce(t); + + for (iter = 8; t->split > 0 && iter; iter--) { + struct node **upper; + + t->split--; + upper = &t->array[t->split + t->size / 2]; + if (*upper) { + struct node **nodep; + + for (nodep = &t->array[t->split]; *nodep; + nodep = &(*nodep)->name_next); + + *nodep = *upper; + *upper = NULL; + break; + } + } +} + static void unhash_name(struct fuse *f, struct node *node) { if (node->name) { size_t hash = name_hash(f, node->parent->nodeid, node->name); - struct node **nodep = &f->name_table[hash]; + struct node **nodep = &f->name_table.array[hash]; for (; *nodep != NULL; nodep = &(*nodep)->name_next) if (*nodep == node) { *nodep = node->name_next; node->name_next = NULL; unref_node(f, node->parent); - free(node->name); + if (node->name != node->inline_name) + free(node->name); node->name = NULL; node->parent = NULL; + f->name_table.use--; + + if (f->name_table.use < f->name_table.size / 4) + remerge_name(f); return; } fprintf(stderr, @@ -343,19 +730,58 @@ static void unhash_name(struct fuse *f, struct node *node) } } +static void rehash_name(struct fuse *f) +{ + struct node_table *t = &f->name_table; + struct node **nodep; + struct node **next; + size_t hash; + + if (t->split == t->size / 2) + return; + + hash = t->split; + t->split++; + for (nodep = &t->array[hash]; *nodep != NULL; nodep = next) { + struct node *node = *nodep; + size_t newhash = name_hash(f, node->parent->nodeid, node->name); + + if (newhash != hash) { + next = nodep; + *nodep = node->name_next; + node->name_next = t->array[newhash]; + t->array[newhash] = node; + } else { + next = &node->name_next; + } + } + if (t->split == t->size / 2) + node_table_resize(t); +} + static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parentid, const char *name) { size_t hash = name_hash(f, parentid, name); struct node *parent = get_node(f, parentid); - node->name = strdup(name); - if (node->name == NULL) - return -1; + if (strlen(name) < sizeof(node->inline_name)) { + strcpy(node->inline_name, name); + node->name = node->inline_name; + } else { + node->name = strdup(name); + if (node->name == NULL) + return -1; + } parent->refctr ++; node->parent = parent; - node->name_next = f->name_table[hash]; - f->name_table[hash] = node; + node->name_next = f->name_table.array[hash]; + f->name_table.array[hash] = node; + f->name_table.use++; + + if (f->name_table.use >= f->name_table.size / 2) + rehash_name(f); + return 0; } @@ -366,9 +792,11 @@ static void delete_node(struct fuse *f, struct node *node) (unsigned long long) node->nodeid); assert(node->treelock == 0); - assert(!node->name); + unhash_name(f, node); + if (lru_enabled(f)) + remove_node_lru(node); unhash_id(f, node); - free_node(node); + free_node(f, node); } static void unref_node(struct fuse *f, struct node *node) @@ -396,7 +824,7 @@ static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, size_t hash = name_hash(f, parent, name); struct node *node; - for (node = f->name_table[hash]; node != NULL; node = node->name_next) + for (node = f->name_table.array[hash]; node != NULL; node = node->name_next) if (node->parent->nodeid == parent && strcmp(node->name, name) == 0) return node; @@ -404,6 +832,13 @@ static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, return NULL; } +static void inc_nlookup(struct node *node) +{ + if (!node->nlookup) + node->refctr++; + node->nlookup++; +} + static struct node *find_node(struct fuse *f, fuse_ino_t parent, const char *name) { @@ -415,27 +850,29 @@ static struct node *find_node(struct fuse *f, fuse_ino_t parent, else node = lookup_node(f, parent, name); if (node == NULL) { - node = (struct node *) calloc(1, sizeof(struct node)); + node = alloc_node(f); if (node == NULL) goto out_err; - if (f->conf.noforget) - node->nlookup = 1; - node->refctr = 1; node->nodeid = next_id(f); node->generation = f->generation; - node->open_count = 0; - node->is_hidden = 0; - node->treelock = 0; - node->ticket = 0; + if (f->conf.remember) + inc_nlookup(node); + if (hash_name(f, node, parent, name) == -1) { - free(node); + free_node(f, node); node = NULL; goto out_err; } hash_id(f, node); + if (lru_enabled(f)) { + struct node_lru *lnode = node_lru(node); + init_list_head(&lnode->lru); + } + } else if (lru_enabled(f) && node->nlookup == 1) { + remove_node_lru(node); } - node->nlookup ++; + inc_nlookup(node); out_err: pthread_mutex_unlock(&f->lock); return node; @@ -475,28 +912,28 @@ static char *add_name(char **buf, unsigned *bufsize, char *s, const char *name) } static void unlock_path(struct fuse *f, fuse_ino_t nodeid, struct node *wnode, - struct node *end, int ticket) + struct node *end) { struct node *node; if (wnode) { - assert(wnode->treelock == -1); + assert(wnode->treelock == TREELOCK_WRITE); wnode->treelock = 0; - if (!wnode->ticket) - wnode->ticket = ticket; } for (node = get_node(f, nodeid); node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent) { - assert(node->treelock > 0); + assert(node->treelock != 0); + assert(node->treelock != TREELOCK_WAIT_OFFSET); + assert(node->treelock != TREELOCK_WRITE); node->treelock--; - if (!node->ticket) - node->ticket = ticket; + if (node->treelock == TREELOCK_WAIT_OFFSET) + node->treelock = 0; } } static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, - char **path, struct node **wnodep, int ticket) + char **path, struct node **wnodep, bool need_lock) { unsigned bufsize = 256; char *buf; @@ -507,9 +944,10 @@ static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, *path = NULL; + err = -ENOMEM; buf = malloc(bufsize); if (buf == NULL) - return -ENOMEM; + goto out_err; s = buf + bufsize - 1; *s = '\0'; @@ -517,51 +955,41 @@ static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, if (name != NULL) { s = add_name(&buf, &bufsize, s, name); err = -ENOMEM; - printf("setting err to ENOMEM\n"); if (s == NULL) goto out_free; } if (wnodep) { - assert(ticket); + assert(need_lock); wnode = lookup_node(f, nodeid, name); if (wnode) { - if (wnode->treelock != 0 || - (wnode->ticket && wnode->ticket != ticket)) { - if (!wnode->ticket) - wnode->ticket = ticket; + if (wnode->treelock != 0) { + if (wnode->treelock > 0) + wnode->treelock += TREELOCK_WAIT_OFFSET; err = -EAGAIN; - printf("setting err to EAGAIN\n"); goto out_free; } - wnode->treelock = -1; - wnode->ticket = 0; + wnode->treelock = TREELOCK_WRITE; } } - err = 0; for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID; node = node->parent) { err = -ENOENT; - printf("setting err to ENOENT\n"); if (node->name == NULL || node->parent == NULL) goto out_unlock; err = -ENOMEM; - printf("setting err to ENOMEM\n"); s = add_name(&buf, &bufsize, s, node->name); if (s == NULL) goto out_unlock; - if (ticket) { + if (need_lock) { err = -EAGAIN; - printf("setting err to EAGAIN\n"); - if (node->treelock == -1 || - (node->ticket && node->ticket != ticket)) + if (node->treelock < 0) goto out_unlock; node->treelock++; - node->ticket = 0; } } @@ -577,36 +1005,95 @@ static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, return 0; out_unlock: - if (ticket) - unlock_path(f, nodeid, wnode, node, ticket); + if (need_lock) + unlock_path(f, nodeid, wnode, node); out_free: free(buf); + out_err: return err; } -static void wake_up_first(struct fuse *f) +static void queue_element_unlock(struct fuse *f, struct lock_queue_element *qe) { - if (f->lockq) - pthread_cond_signal(&f->lockq->cond); + struct node *wnode; + + if (qe->first_locked) { + wnode = qe->wnode1 ? *qe->wnode1 : NULL; + unlock_path(f, qe->nodeid1, wnode, NULL); + } + if (qe->second_locked) { + wnode = qe->wnode2 ? *qe->wnode2 : NULL; + unlock_path(f, qe->nodeid2, wnode, NULL); + } } -static void wake_up_next(struct lock_queue_element *qe) +static void queue_element_wakeup(struct fuse *f, struct lock_queue_element *qe) { - if (qe->next) - pthread_cond_signal(&qe->next->cond); + int err; + bool first = (qe == f->lockq); + + if (!qe->path1) { + /* Just waiting for it to be unlocked */ + if (get_node(f, qe->nodeid1)->treelock == 0) + pthread_cond_signal(&qe->cond); + + return; + } + + if (!qe->first_locked) { + err = try_get_path(f, qe->nodeid1, qe->name1, qe->path1, + qe->wnode1, true); + if (!err) + qe->first_locked = true; + else if (err != -EAGAIN) + goto err_unlock; + } + if (!qe->second_locked && qe->path2) { + err = try_get_path(f, qe->nodeid2, qe->name2, qe->path2, + qe->wnode2, true); + if (!err) + qe->second_locked = true; + else if (err != -EAGAIN) + goto err_unlock; + } + + if (qe->first_locked && (qe->second_locked || !qe->path2)) { + err = 0; + goto done; + } + + /* + * Only let the first element be partially locked otherwise there could + * be a deadlock. + * + * But do allow the first element to be partially locked to prevent + * starvation. + */ + if (!first) + queue_element_unlock(f, qe); + + /* keep trying */ + return; + +err_unlock: + queue_element_unlock(f, qe); +done: + qe->err = err; + qe->done = true; + pthread_cond_signal(&qe->cond); } -static int get_ticket(struct fuse *f) +static void wake_up_queued(struct fuse *f) { - do f->curr_ticket++; - while (f->curr_ticket == 0); + struct lock_queue_element *qe; - return f->curr_ticket; + for (qe = f->lockq; qe != NULL; qe = qe->next) + queue_element_wakeup(f, qe); } static void debug_path(struct fuse *f, const char *msg, fuse_ino_t nodeid, - const char *name, int wr) + const char *name, bool wr) { if (f->conf.debug) { struct node *wnode = NULL; @@ -621,56 +1108,58 @@ static void debug_path(struct fuse *f, const char *msg, fuse_ino_t nodeid, } } -static void queue_path(struct fuse *f, struct lock_queue_element *qe, - fuse_ino_t nodeid, const char *name, int wr) +static void queue_path(struct fuse *f, struct lock_queue_element *qe) { struct lock_queue_element **qp; - debug_path(f, "QUEUE PATH", nodeid, name, wr); + qe->done = false; + qe->first_locked = false; + qe->second_locked = false; pthread_cond_init(&qe->cond, NULL); qe->next = NULL; for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next); *qp = qe; } -static void dequeue_path(struct fuse *f, struct lock_queue_element *qe, - fuse_ino_t nodeid, const char *name, int wr) +static void dequeue_path(struct fuse *f, struct lock_queue_element *qe) { struct lock_queue_element **qp; - debug_path(f, "DEQUEUE PATH", nodeid, name, wr); pthread_cond_destroy(&qe->cond); for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next); *qp = qe->next; } -static void wait_on_path(struct fuse *f, struct lock_queue_element *qe, - fuse_ino_t nodeid, const char *name, int wr) +static int wait_path(struct fuse *f, struct lock_queue_element *qe) { - debug_path(f, "WAIT ON PATH", nodeid, name, wr); - pthread_cond_wait(&qe->cond, &f->lock); + queue_path(f, qe); + + do { + pthread_cond_wait(&qe->cond, &f->lock); + } while (!qe->done); + + dequeue_path(f, qe); + + return qe->err; } static int get_path_common(struct fuse *f, fuse_ino_t nodeid, const char *name, char **path, struct node **wnode) { int err; - int ticket; pthread_mutex_lock(&f->lock); - ticket = get_ticket(f); - err = try_get_path(f, nodeid, name, path, wnode, ticket); + err = try_get_path(f, nodeid, name, path, wnode, true); if (err == -EAGAIN) { - struct lock_queue_element qe; - - queue_path(f, &qe, nodeid, name, !!wnode); - do { - wait_on_path(f, &qe, nodeid, name, !!wnode); - err = try_get_path(f, nodeid, name, path, wnode, - ticket); - wake_up_next(&qe); - } while (err == -EAGAIN); - dequeue_path(f, &qe, nodeid, name, !!wnode); + struct lock_queue_element qe = { + .nodeid1 = nodeid, + .name1 = name, + .path1 = path, + .wnode1 = wnode, + }; + debug_path(f, "QUEUE PATH", nodeid, name, !!wnode); + err = wait_path(f, &qe); + debug_path(f, "DEQUEUE PATH", nodeid, name, !!wnode); } pthread_mutex_unlock(&f->lock); @@ -684,10 +1173,15 @@ static int get_path(struct fuse *f, fuse_ino_t nodeid, char **path) static int get_path_nullok(struct fuse *f, fuse_ino_t nodeid, char **path) { - int err = get_path_common(f, nodeid, NULL, path, NULL); + int err = 0; - if (err == -ENOENT && f->nullpath_ok) - err = 0; + if (f->conf.nopath) { + *path = NULL; + } else { + err = get_path_common(f, nodeid, NULL, path, NULL); + if (err == -ENOENT && f->nullpath_ok) + err = 0; + } return err; } @@ -707,18 +1201,20 @@ static int get_path_wrlock(struct fuse *f, fuse_ino_t nodeid, const char *name, static int try_get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1, fuse_ino_t nodeid2, const char *name2, char **path1, char **path2, - struct node **wnode1, struct node **wnode2, - int ticket) + struct node **wnode1, struct node **wnode2) { int err; /* FIXME: locking two paths needs deadlock checking */ - err = try_get_path(f, nodeid1, name1, path1, wnode1, ticket); + err = try_get_path(f, nodeid1, name1, path1, wnode1, true); if (!err) { - err = try_get_path(f, nodeid2, name2, path2, wnode2, ticket); - if (err) - unlock_path(f, nodeid1, wnode1 ? *wnode1 : NULL, NULL, - ticket); + err = try_get_path(f, nodeid2, name2, path2, wnode2, true); + if (err) { + struct node *wn1 = wnode1 ? *wnode1 : NULL; + + unlock_path(f, nodeid1, wn1, NULL); + free(*path1); + } } return err; } @@ -729,27 +1225,27 @@ static int get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1, struct node **wnode1, struct node **wnode2) { int err; - int ticket; pthread_mutex_lock(&f->lock); - ticket = get_ticket(f); err = try_get_path2(f, nodeid1, name1, nodeid2, name2, - path1, path2, wnode1, wnode2, ticket); + path1, path2, wnode1, wnode2); if (err == -EAGAIN) { - struct lock_queue_element qe; - - queue_path(f, &qe, nodeid1, name1, !!wnode1); - debug_path(f, " path2", nodeid2, name2, !!wnode2); - do { - wait_on_path(f, &qe, nodeid1, name1, !!wnode1); - debug_path(f, " path2", nodeid2, name2, !!wnode2); - err = try_get_path2(f, nodeid1, name1, nodeid2, name2, - path1, path2, wnode1, wnode2, - ticket); - wake_up_next(&qe); - } while (err == -EAGAIN); - dequeue_path(f, &qe, nodeid1, name1, !!wnode1); - debug_path(f, " path2", nodeid2, name2, !!wnode2); + struct lock_queue_element qe = { + .nodeid1 = nodeid1, + .name1 = name1, + .path1 = path1, + .wnode1 = wnode1, + .nodeid2 = nodeid2, + .name2 = name2, + .path2 = path2, + .wnode2 = wnode2, + }; + + debug_path(f, "QUEUE PATH1", nodeid1, name1, !!wnode1); + debug_path(f, " PATH2", nodeid2, name2, !!wnode2); + err = wait_path(f, &qe); + debug_path(f, "DEQUEUE PATH1", nodeid1, name1, !!wnode1); + debug_path(f, " PATH2", nodeid2, name2, !!wnode2); } pthread_mutex_unlock(&f->lock); @@ -760,8 +1256,9 @@ static void free_path_wrlock(struct fuse *f, fuse_ino_t nodeid, struct node *wnode, char *path) { pthread_mutex_lock(&f->lock); - unlock_path(f, nodeid, wnode, NULL, 0); - wake_up_first(f); + unlock_path(f, nodeid, wnode, NULL); + if (f->lockq) + wake_up_queued(f); pthread_mutex_unlock(&f->lock); free(path); } @@ -777,9 +1274,9 @@ static void free_path2(struct fuse *f, fuse_ino_t nodeid1, fuse_ino_t nodeid2, char *path1, char *path2) { pthread_mutex_lock(&f->lock); - unlock_path(f, nodeid1, wnode1, NULL, 0); - unlock_path(f, nodeid2, wnode2, NULL, 0); - wake_up_first(f); + unlock_path(f, nodeid1, wnode1, NULL); + unlock_path(f, nodeid2, wnode2, NULL); + wake_up_queued(f); pthread_mutex_unlock(&f->lock); free(path1); free(path2); @@ -798,29 +1295,34 @@ static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup) * create and opendir */ while (node->nlookup == nlookup && node->treelock) { - struct lock_queue_element qe; + struct lock_queue_element qe = { + .nodeid1 = nodeid, + }; - queue_path(f, &qe, node->nodeid, NULL, 0); - do { - wait_on_path(f, &qe, node->nodeid, NULL, 0); - wake_up_next(&qe); + debug_path(f, "QUEUE PATH (forget)", nodeid, NULL, false); + queue_path(f, &qe); + do { + pthread_cond_wait(&qe.cond, &f->lock); } while (node->nlookup == nlookup && node->treelock); - dequeue_path(f, &qe, node->nodeid, NULL, 0); + + dequeue_path(f, &qe); + debug_path(f, "DEQUEUE_PATH (forget)", nodeid, NULL, false); } assert(node->nlookup >= nlookup); node->nlookup -= nlookup; if (!node->nlookup) { - unhash_name(f, node); unref_node(f, node); + } else if (lru_enabled(f) && node->nlookup == 1) { + set_forget_time(f, node); } pthread_mutex_unlock(&f->lock); } static void unlink_node(struct fuse *f, struct node *node) { - if (f->conf.noforget) { + if (f->conf.remember) { assert(node->nlookup > 1); node->nlookup--; } @@ -959,7 +1461,7 @@ static inline void fuse_prepare_interrupt(struct fuse *f, fuse_req_t req, fuse_do_prepare_interrupt(req, d); } -#ifndef __FreeBSD__ +#if !defined(__FreeBSD__) && !defined(__NetBSD__) static int fuse_compat_open(struct fuse_fs *fs, const char *path, struct fuse_file_info *fi) @@ -1054,7 +1556,7 @@ static int fuse_compat_statfs(struct fuse_fs *fs, const char *path, return err; } -#else /* __FreeBSD__ */ +#else /* __FreeBSD__ || __NetBSD__ */ static inline int fuse_compat_open(struct fuse_fs *fs, char *path, struct fuse_file_info *fi) @@ -1080,7 +1582,7 @@ static inline int fuse_compat_statfs(struct fuse_fs *fs, const char *path, return fs->op.statfs(fs->compat == 25 ? "/" : path, buf); } -#endif /* __FreeBSD__ */ +#endif /* __FreeBSD__ || __NetBSD__ */ int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf) { @@ -1243,52 +1745,140 @@ int fuse_fs_open(struct fuse_fs *fs, const char *path, } } -int fuse_fs_read(struct fuse_fs *fs, const char *path, char *buf, size_t size, - off64_t off, struct fuse_file_info *fi) +static void fuse_free_buf(struct fuse_bufvec *buf) +{ + if (buf != NULL) { + size_t i; + + for (i = 0; i < buf->count; i++) + free(buf->buf[i].mem); + free(buf); + } +} + +int fuse_fs_read_buf(struct fuse_fs *fs, const char *path, + struct fuse_bufvec **bufp, size_t size, off64_t off, + struct fuse_file_info *fi) { fuse_get_context()->private_data = fs->user_data; - if (fs->op.read) { + if (fs->op.read || fs->op.read_buf) { int res; if (fs->debug) fprintf(stderr, - "read[%llu] %lu bytes from %llu flags: 0x%x\n", + "read[%llu] %zu bytes from %llu flags: 0x%x\n", (unsigned long long) fi->fh, - (unsigned long) size, (unsigned long long) off, - fi->flags); + size, (unsigned long long) off, fi->flags); + + if (fs->op.read_buf) { + res = fs->op.read_buf(path, bufp, size, off, fi); + } else { + struct fuse_bufvec *buf; + void *mem; + + buf = malloc(sizeof(struct fuse_bufvec)); + if (buf == NULL) + return -ENOMEM; + + mem = malloc(size); + if (mem == NULL) { + free(buf); + return -ENOMEM; + } + *buf = FUSE_BUFVEC_INIT(size); + buf->buf[0].mem = mem; + *bufp = buf; - res = fs->op.read(path, buf, size, off, fi); + res = fs->op.read(path, mem, size, off, fi); + if (res >= 0) + buf->buf[0].size = res; + } if (fs->debug && res >= 0) - fprintf(stderr, " read[%llu] %u bytes from %llu\n", - (unsigned long long) fi->fh, res, + fprintf(stderr, " read[%llu] %zu bytes from %llu\n", + (unsigned long long) fi->fh, + fuse_buf_size(*bufp), (unsigned long long) off); - if (res > (int) size) + if (res >= 0 && fuse_buf_size(*bufp) > (int) size) fprintf(stderr, "fuse: read too many bytes\n"); - return res; + if (res < 0) + return res; + + return 0; } else { return -ENOSYS; } } -int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, - size_t size, off64_t off, struct fuse_file_info *fi) +int fuse_fs_read(struct fuse_fs *fs, const char *path, char *mem, size_t size, + off64_t off, struct fuse_file_info *fi) +{ + int res; + struct fuse_bufvec *buf = NULL; + + res = fuse_fs_read_buf(fs, path, &buf, size, off, fi); + if (res == 0) { + struct fuse_bufvec dst = FUSE_BUFVEC_INIT(size); + + dst.buf[0].mem = mem; + res = fuse_buf_copy(&dst, buf, 0); + } + fuse_free_buf(buf); + + return res; +} + +int fuse_fs_write_buf(struct fuse_fs *fs, const char *path, + struct fuse_bufvec *buf, off64_t off, + struct fuse_file_info *fi) { fuse_get_context()->private_data = fs->user_data; - if (fs->op.write) { + if (fs->op.write_buf || fs->op.write) { int res; + size_t size = fuse_buf_size(buf); + assert(buf->idx == 0 && buf->off == 0); if (fs->debug) fprintf(stderr, - "write%s[%llu] %lu bytes to %llu flags: 0x%x\n", + "write%s[%llu] %zu bytes to %llu flags: 0x%x\n", fi->writepage ? "page" : "", (unsigned long long) fi->fh, - (unsigned long) size, (unsigned long long) off, + size, + (unsigned long long) off, fi->flags); - res = fs->op.write(path, buf, size, off, fi); + if (fs->op.write_buf) { + res = fs->op.write_buf(path, buf, off, fi); + } else { + void *mem = NULL; + struct fuse_buf *flatbuf; + struct fuse_bufvec tmp = FUSE_BUFVEC_INIT(size); + + if (buf->count == 1 && + !(buf->buf[0].flags & FUSE_BUF_IS_FD)) { + flatbuf = &buf->buf[0]; + } else { + res = -ENOMEM; + mem = malloc(size); + if (mem == NULL) + goto out; + + tmp.buf[0].mem = mem; + res = fuse_buf_copy(&tmp, buf, 0); + if (res <= 0) + goto out_free; + + tmp.buf[0].size = res; + flatbuf = &tmp.buf[0]; + } + res = fs->op.write(path, flatbuf->mem, flatbuf->size, + off, fi); +out_free: + free(mem); + } +out: if (fs->debug && res >= 0) fprintf(stderr, " write%s[%llu] %u bytes to %llu\n", fi->writepage ? "page" : "", @@ -1303,6 +1893,16 @@ int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, } } +int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *mem, + size_t size, off64_t off, struct fuse_file_info *fi) +{ + struct fuse_bufvec bufv = FUSE_BUFVEC_INIT(size); + + bufv.buf[0].mem = (void *) mem; + + return fuse_fs_write_buf(fs, path, &bufv, off, fi); +} + int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync, struct fuse_file_info *fi) { @@ -1469,6 +2069,27 @@ int fuse_fs_lock(struct fuse_fs *fs, const char *path, } } +int fuse_fs_flock(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, int op) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.flock) { + if (fs->debug) { + int xop = op & ~LOCK_NB; + + fprintf(stderr, "lock[%llu] %s%s\n", + (unsigned long long) fi->fh, + xop == LOCK_SH ? "LOCK_SH" : + (xop == LOCK_EX ? "LOCK_EX" : + (xop == LOCK_UN ? "LOCK_UN" : "???")), + (op & LOCK_NB) ? "|LOCK_NB" : ""); + } + return fs->op.flock(path, fi, op); + } else { + return -ENOSYS; + } +} + int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid) { fuse_get_context()->private_data = fs->user_data; @@ -1503,8 +2124,8 @@ int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off64_t size, fuse_get_context()->private_data = fs->user_data; if (fs->op.ftruncate) { if (fs->debug) - fprintf(stderr, "ftruncate[%llu] %s %llu\n", - (unsigned long long) fi->fh, path, + fprintf(stderr, "ftruncate[%llu] %llu\n", + (unsigned long long) fi->fh, (unsigned long long) size); return fs->op.ftruncate(path, size, fi); @@ -1714,6 +2335,23 @@ int fuse_fs_poll(struct fuse_fs *fs, const char *path, return -ENOSYS; } +int fuse_fs_fallocate(struct fuse_fs *fs, const char *path, int mode, + off64_t offset, off64_t length, struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fallocate) { + if (fs->debug) + fprintf(stderr, "fallocate %s mode %x, offset: %llu, length: %llu\n", + path, + mode, + (unsigned long long) offset, + (unsigned long long) length); + + return fs->op.fallocate(path, mode, offset, length, fi); + } else + return -ENOSYS; +} + static int is_open(struct fuse *f, fuse_ino_t dir, const char *name) { struct node *node; @@ -1750,10 +2388,9 @@ static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname, newnode = lookup_node(f, dir, newname); } while(newnode); - try_get_path(f, dir, newname, &newpath, NULL, 0); + res = try_get_path(f, dir, newname, &newpath, NULL, false); pthread_mutex_unlock(&f->lock); - - if (!newpath) + if (res) break; res = fuse_fs_getattr(f->fs, newpath, &buf); @@ -1861,7 +2498,7 @@ static struct fuse_context_i *fuse_get_context_internal(void) c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key); if (c == NULL) { c = (struct fuse_context_i *) - malloc(sizeof(struct fuse_context_i)); + calloc(1, sizeof(struct fuse_context_i)); if (c == NULL) { /* This is hard to deal with properly, so just abort. If memory is so low that the @@ -1945,6 +2582,12 @@ static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e, void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn) { fuse_get_context()->private_data = fs->user_data; + if (!fs->op.write_buf) + conn->want &= ~FUSE_CAP_SPLICE_READ; + if (!fs->op.lock) + conn->want &= ~FUSE_CAP_POSIX_LOCKS; + if (!fs->op.flock) + conn->want &= ~FUSE_CAP_FLOCK_LOCKS; if (fs->op.init) fs->user_data = fs->op.init(conn); } @@ -2038,17 +2681,34 @@ static void fuse_lib_lookup(fuse_req_t req, fuse_ino_t parent, reply_entry(req, &e, err); } +static void do_forget(struct fuse *f, fuse_ino_t ino, uint64_t nlookup) +{ + if (f->conf.debug) + fprintf(stderr, "FORGET %llu/%llu\n", (unsigned long long)ino, + (unsigned long long) nlookup); + forget_node(f, ino, nlookup); +} + static void fuse_lib_forget(fuse_req_t req, fuse_ino_t ino, unsigned long nlookup) { + do_forget(req_fuse(req), ino, nlookup); + fuse_reply_none(req); +} + +static void fuse_lib_forget_multi(fuse_req_t req, size_t count, + struct fuse_forget_data *forgets) +{ struct fuse *f = req_fuse(req); - if (f->conf.debug) - fprintf(stderr, "FORGET %llu/%lu\n", (unsigned long long)ino, - nlookup); - forget_node(f, ino, nlookup); + size_t i; + + for (i = 0; i < count; i++) + do_forget(f, forgets[i].ino, forgets[i].nlookup); + fuse_reply_none(req); } + static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) { @@ -2059,7 +2719,7 @@ static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino, memset(&buf, 0, sizeof(buf)); - if (fi != NULL) + if (fi != NULL && f->fs->op.fgetattr) err = get_path_nullok(f, ino, &path); else err = get_path(f, ino, &path); @@ -2074,11 +2734,15 @@ static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino, free_path(f, ino, path); } if (!err) { - if (f->conf.auto_cache) { - pthread_mutex_lock(&f->lock); - update_stat(get_node(f, ino), &buf); - pthread_mutex_unlock(&f->lock); - } + struct node *node; + + pthread_mutex_lock(&f->lock); + node = get_node(f, ino); + if (node->is_hidden && buf.st_nlink > 0) + buf.st_nlink--; + if (f->conf.auto_cache) + update_stat(node, &buf); + pthread_mutex_unlock(&f->lock); set_stat(f, ino, &buf); fuse_reply_attr(req, &buf, f->conf.attr_timeout); } else @@ -2102,7 +2766,11 @@ static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, char *path; int err; - err = get_path(f, ino, &path); + if (valid == FUSE_SET_ATTR_SIZE && fi != NULL && + f->fs->op.ftruncate && f->fs->op.fgetattr) + err = get_path_nullok(f, ino, &path); + else + err = get_path(f, ino, &path); if (!err) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); @@ -2124,6 +2792,29 @@ static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, err = fuse_fs_truncate(f->fs, path, attr->st_size); } +#ifdef HAVE_UTIMENSAT + if (!err && f->utime_omit_ok && + (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME))) { + struct timespec tv[2]; + + tv[0].tv_sec = 0; + tv[1].tv_sec = 0; + tv[0].tv_nsec = UTIME_OMIT; + tv[1].tv_nsec = UTIME_OMIT; + + if (valid & FUSE_SET_ATTR_ATIME_NOW) + tv[0].tv_nsec = UTIME_NOW; + else if (valid & FUSE_SET_ATTR_ATIME) + tv[0] = attr->st_atim; + + if (valid & FUSE_SET_ATTR_MTIME_NOW) + tv[1].tv_nsec = UTIME_NOW; + else if (valid & FUSE_SET_ATTR_MTIME) + tv[1] = attr->st_mtim; + + err = fuse_fs_utimens(f->fs, path, tv); + } else +#endif if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) { @@ -2134,8 +2825,12 @@ static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, tv[1].tv_nsec = ST_MTIM_NSEC(attr); err = fuse_fs_utimens(f->fs, path, tv); } - if (!err) - err = fuse_fs_getattr(f->fs, path, &buf); + if (!err) { + if (fi) + err = fuse_fs_fgetattr(f->fs, path, &buf, fi); + else + err = fuse_fs_getattr(f->fs, path, &buf); + } fuse_finish_interrupt(f, req, &d); free_path(f, ino, path); } @@ -2381,8 +3076,14 @@ static void fuse_do_release(struct fuse *f, fuse_ino_t ino, const char *path, { struct node *node; int unlink_hidden = 0; + const char *compatpath; + + if (path != NULL || f->nullpath_ok || f->conf.nopath) + compatpath = path; + else + compatpath = "-"; - fuse_fs_release(f->fs, (path || f->nullpath_ok) ? path : "-", fi); + fuse_fs_release(f->fs, compatpath, fi); pthread_mutex_lock(&f->lock); node = get_node(f, ino); @@ -2394,8 +3095,18 @@ static void fuse_do_release(struct fuse *f, fuse_ino_t ino, const char *path, } pthread_mutex_unlock(&f->lock); - if(unlink_hidden && path) - fuse_fs_unlink(f->fs, path); + if(unlink_hidden) { + if (path) { + fuse_fs_unlink(f->fs, path); + } else if (f->conf.nopath) { + char *unlinkpath; + + if (get_path(f, ino, &unlinkpath) == 0) + fuse_fs_unlink(f->fs, unlinkpath); + + free_path(f, ino, unlinkpath); + } + } } static void fuse_lib_create(fuse_req_t req, fuse_ino_t parent, @@ -2437,9 +3148,7 @@ static void fuse_lib_create(fuse_req_t req, fuse_ino_t parent, if (fuse_reply_create(req, &e, fi) == -ENOENT) { /* The open syscall was interrupted, so it must be cancelled */ - fuse_prepare_interrupt(f, req, &d); fuse_do_release(f, e.ino, path, fi); - fuse_finish_interrupt(f, req, &d); forget_node(f, e.ino, 1); } } else { @@ -2517,9 +3226,7 @@ static void fuse_lib_open(fuse_req_t req, fuse_ino_t ino, if (fuse_reply_open(req, fi) == -ENOENT) { /* The open syscall was interrupted, so it must be cancelled */ - fuse_prepare_interrupt(f, req, &d); fuse_do_release(f, ino, path, fi); - fuse_finish_interrupt(f, req, &d); } } else reply_err(req, err); @@ -2531,36 +3238,31 @@ static void fuse_lib_read(fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, struct fuse_file_info *fi) { struct fuse *f = req_fuse_prepare(req); + struct fuse_bufvec *buf = NULL; char *path; - char *buf; int res; - buf = (char *) malloc(size); - if (buf == NULL) { - reply_err(req, -ENOMEM); - return; - } - res = get_path_nullok(f, ino, &path); if (res == 0) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); - res = fuse_fs_read(f->fs, path, buf, size, off, fi); + res = fuse_fs_read_buf(f->fs, path, &buf, size, off, fi); fuse_finish_interrupt(f, req, &d); free_path(f, ino, path); } - if (res >= 0) - fuse_reply_buf(req, buf, res); + if (res == 0) + fuse_reply_data(req, buf, FUSE_BUF_SPLICE_MOVE); else reply_err(req, res); - free(buf); + fuse_free_buf(buf); } -static void fuse_lib_write(fuse_req_t req, fuse_ino_t ino, const char *buf, - size_t size, off64_t off, struct fuse_file_info *fi) +static void fuse_lib_write_buf(fuse_req_t req, fuse_ino_t ino, + struct fuse_bufvec *buf, off64_t off, + struct fuse_file_info *fi) { struct fuse *f = req_fuse_prepare(req); char *path; @@ -2571,8 +3273,7 @@ static void fuse_lib_write(fuse_req_t req, fuse_ino_t ino, const char *buf, struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); - res = fuse_fs_write(f->fs, path, buf, size, off, fi); - printf("died\n"); + res = fuse_fs_write_buf(f->fs, path, buf, off, fi); fuse_finish_interrupt(f, req, &d); free_path(f, ino, path); } @@ -2651,9 +3352,7 @@ static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, if (fuse_reply_open(req, llfi) == -ENOENT) { /* The opendir syscall was interrupted, so it must be cancelled */ - fuse_prepare_interrupt(f, req, &d); fuse_fs_releasedir(f->fs, path, &fi); - fuse_finish_interrupt(f, req, &d); pthread_mutex_destroy(&dh->lock); free(dh); } @@ -2747,7 +3446,10 @@ static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, char *path; int err; - err = get_path(f, ino, &path); + if (f->fs->op.readdir) + err = get_path_nullok(f, ino, &path); + else + err = get_path(f, ino, &path); if (!err) { struct fuse_intr_data d; @@ -2812,10 +3514,16 @@ static void fuse_lib_releasedir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info fi; struct fuse_dh *dh = get_dirhandle(llfi, &fi); char *path; + const char *compatpath; + + get_path_nullok(f, ino, &path); + if (path != NULL || f->nullpath_ok || f->conf.nopath) + compatpath = path; + else + compatpath = "-"; - get_path(f, ino, &path); fuse_prepare_interrupt(f, req, &d); - fuse_fs_releasedir(f->fs, (path || f->nullpath_ok) ? path : "-", &fi); + fuse_fs_releasedir(f->fs, compatpath, &fi); fuse_finish_interrupt(f, req, &d); free_path(f, ino, path); @@ -2837,7 +3545,7 @@ static void fuse_lib_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, get_dirhandle(llfi, &fi); - err = get_path(f, ino, &path); + err = get_path_nullok(f, ino, &path); if (!err) { struct fuse_intr_data d; fuse_prepare_interrupt(f, req, &d); @@ -3155,7 +3863,7 @@ static void fuse_lib_release(fuse_req_t req, fuse_ino_t ino, char *path; int err = 0; - get_path(f, ino, &path); + get_path_nullok(f, ino, &path); if (fi->flush) { err = fuse_flush_common(f, req, ino, path, fi); if (err == -ENOSYS) @@ -3177,7 +3885,7 @@ static void fuse_lib_flush(fuse_req_t req, fuse_ino_t ino, char *path; int err; - get_path(f, ino, &path); + get_path_nullok(f, ino, &path); err = fuse_flush_common(f, req, ino, path, fi); free_path(f, ino, path); @@ -3247,6 +3955,24 @@ static void fuse_lib_setlk(fuse_req_t req, fuse_ino_t ino, reply_err(req, err); } +static void fuse_lib_flock(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, int op) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path_nullok(f, ino, &path); + if (err == 0) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_flock(f->fs, path, fi, op); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + static void fuse_lib_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, uint64_t idx) { @@ -3293,7 +4019,7 @@ static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, if (out_buf) memcpy(out_buf, in_buf, in_bufsz); - err = get_path(f, ino, &path); + err = get_path_nullok(f, ino, &path); if (err) goto err; @@ -3319,20 +4045,93 @@ static void fuse_lib_poll(fuse_req_t req, fuse_ino_t ino, struct fuse *f = req_fuse_prepare(req); struct fuse_intr_data d; char *path; - int ret; + int err; unsigned revents = 0; - ret = get_path(f, ino, &path); - if (!ret) { + err = get_path_nullok(f, ino, &path); + if (!err) { fuse_prepare_interrupt(f, req, &d); - ret = fuse_fs_poll(f->fs, path, fi, ph, &revents); + err = fuse_fs_poll(f->fs, path, fi, ph, &revents); fuse_finish_interrupt(f, req, &d); free_path(f, ino, path); } - if (!ret) + if (!err) fuse_reply_poll(req, revents); else - reply_err(req, ret); + reply_err(req, err); +} + +static void fuse_lib_fallocate(fuse_req_t req, fuse_ino_t ino, int mode, + off64_t offset, off64_t length, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err; + + err = get_path_nullok(f, ino, &path); + if (!err) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_fallocate(f->fs, path, mode, offset, length, fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static int clean_delay(struct fuse *f) +{ + /* + * This is calculating the delay between clean runs. To + * reduce the number of cleans we are doing them 10 times + * within the remember window. + */ + int min_sleep = 60; + int max_sleep = 3600; + int sleep_time = f->conf.remember / 10; + + if (sleep_time > max_sleep) + return max_sleep; + if (sleep_time < min_sleep) + return min_sleep; + return sleep_time; +} + +int fuse_clean_cache(struct fuse *f) +{ + struct node_lru *lnode; + struct list_head *curr, *next; + struct node *node; + struct timespec now; + + pthread_mutex_lock(&f->lock); + + curr_time(&now); + + for (curr = f->lru_table.next; curr != &f->lru_table; curr = next) { + double age; + + next = curr->next; + lnode = list_entry(curr, struct node_lru, lru); + node = &lnode->node; + + age = diff_timespec(&now, &lnode->forget_time); + if (age <= f->conf.remember) + break; + + assert(node->nlookup == 1); + + /* Don't forget active directories */ + if (node->refctr > 1) + continue; + + node->nlookup = 0; + unhash_name(f, node); + unref_node(f, node); + } + pthread_mutex_unlock(&f->lock); + + return clean_delay(f); } static struct fuse_lowlevel_ops fuse_path_ops = { @@ -3340,6 +4139,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = { .destroy = fuse_lib_destroy, .lookup = fuse_lib_lookup, .forget = fuse_lib_forget, + .forget_multi = fuse_lib_forget_multi, .getattr = fuse_lib_getattr, .setattr = fuse_lib_setattr, .access = fuse_lib_access, @@ -3354,7 +4154,7 @@ static struct fuse_lowlevel_ops fuse_path_ops = { .create = fuse_lib_create, .open = fuse_lib_open, .read = fuse_lib_read, - .write = fuse_lib_write, + .write_buf = fuse_lib_write_buf, .flush = fuse_lib_flush, .release = fuse_lib_release, .fsync = fuse_lib_fsync, @@ -3369,9 +4169,11 @@ static struct fuse_lowlevel_ops fuse_path_ops = { .removexattr = fuse_lib_removexattr, .getlk = fuse_lib_getlk, .setlk = fuse_lib_setlk, + .flock = fuse_lib_flock, .bmap = fuse_lib_bmap, .ioctl = fuse_lib_ioctl, .poll = fuse_lib_poll, + .fallocate = fuse_lib_fallocate, }; int fuse_notify_poll(struct fuse_pollhandle *ph) @@ -3436,12 +4238,77 @@ struct fuse_cmd *fuse_read_cmd(struct fuse *f) return cmd; } +static int fuse_session_loop_remember(struct fuse *f) +{ + struct fuse_session *se = f->se; + int res = 0; + struct timespec now; + time_t next_clean; + struct fuse_chan *ch = fuse_session_next_chan(se, NULL); + size_t bufsize = fuse_chan_bufsize(ch); + char *buf = (char *) malloc(bufsize); + struct pollfd fds = { + .fd = fuse_chan_fd(ch), + .events = POLLIN + }; + + if (!buf) { + fprintf(stderr, "fuse: failed to allocate read buffer\n"); + return -1; + } + + curr_time(&now); + next_clean = now.tv_sec; + while (!fuse_session_exited(se)) { + struct fuse_chan *tmpch = ch; + struct fuse_buf fbuf = { + .mem = buf, + .size = bufsize, + }; + unsigned timeout; + + curr_time(&now); + if (now.tv_sec < next_clean) + timeout = next_clean - now.tv_sec; + else + timeout = 0; + + res = poll(&fds, 1, timeout * 1000); + if (res == -1) { + if (errno == -EINTR) + continue; + else + break; + } else if (res > 0) { + res = fuse_session_receive_buf(se, &fbuf, &tmpch); + + if (res == -EINTR) + continue; + if (res <= 0) + break; + + fuse_session_process_buf(se, &fbuf, tmpch); + } else { + timeout = fuse_clean_cache(f); + curr_time(&now); + next_clean = now.tv_sec + timeout; + } + } + + free(buf); + fuse_session_reset(se); + return res < 0 ? -1 : 0; +} + int fuse_loop(struct fuse *f) { - if (f) - return fuse_session_loop(f->se); - else + if (!f) return -1; + + if (lru_enabled(f)) + return fuse_session_loop_remember(f); + + return fuse_session_loop(f->se); } int fuse_invalidate(struct fuse *f, const char *path) @@ -3521,7 +4388,9 @@ static const struct fuse_opt fuse_lib_opts[] = { FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0), FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1), FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0), - FUSE_LIB_OPT("noforget", noforget, 1), + FUSE_LIB_OPT("noforget", remember, -1), + FUSE_LIB_OPT("remember=%u", remember, 0), + FUSE_LIB_OPT("nopath", nopath, 1), FUSE_LIB_OPT("intr", intr, 1), FUSE_LIB_OPT("intr_signal=%d", intr_signal, 0), FUSE_LIB_OPT("modules=%s", modules, 0), @@ -3544,6 +4413,8 @@ static void fuse_lib_help(void) " -o negative_timeout=T cache timeout for deleted names (0.0s)\n" " -o attr_timeout=T cache timeout for attributes (1.0s)\n" " -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n" +" -o noforget never forget cached inodes\n" +" -o remember=T remember cached inodes for T seconds (0s)\n" " -o intr allow requests to be interrupted\n" " -o intr_signal=NUM signal to send on interrupt (%i)\n" " -o modules=M1[:M2...] names of modules to push onto filesystem stack\n" @@ -3643,6 +4514,8 @@ static int fuse_push_module(struct fuse *f, const char *module, newfs->m = m; f->fs = newfs; f->nullpath_ok = newfs->op.flag_nullpath_ok && f->nullpath_ok; + f->conf.nopath = newfs->op.flag_nopath && f->conf.nopath; + f->utime_omit_ok = newfs->op.flag_utime_omit_ok && f->utime_omit_ok; return 0; } @@ -3668,6 +4541,50 @@ struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, return fs; } +static int node_table_init(struct node_table *t) +{ + t->size = NODE_TABLE_MIN_SIZE; + t->array = (struct node **) calloc(1, sizeof(struct node *) * t->size); + if (t->array == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + t->use = 0; + t->split = 0; + + return 0; +} + +static void *fuse_prune_nodes(void *fuse) +{ + struct fuse *f = fuse; + int sleep_time; + + while(1) { + sleep_time = fuse_clean_cache(f); + sleep(sleep_time); + } + return NULL; +} + +int fuse_start_cleanup_thread(struct fuse *f) +{ + if (lru_enabled(f)) + return fuse_start_thread(&f->prune_thread, fuse_prune_nodes, f); + + return 0; +} + +void fuse_stop_cleanup_thread(struct fuse *f) +{ + if (lru_enabled(f)) { + pthread_mutex_lock(&f->lock); + pthread_cancel(f->prune_thread); + pthread_mutex_unlock(&f->lock); + pthread_join(f->prune_thread, NULL); + } +} + struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, const struct fuse_operations *op, size_t op_size, void *user_data, int compat) @@ -3693,6 +4610,8 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, fs->compat = compat; f->fs = fs; f->nullpath_ok = fs->op.flag_nullpath_ok; + f->conf.nopath = fs->op.flag_nopath; + f->utime_omit_ok = fs->op.flag_utime_omit_ok; /* Oh f**k, this is ugly! */ if (!fs->op.lock) { @@ -3705,6 +4624,11 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, f->conf.negative_timeout = 0.0; f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL; + f->pagesize = getpagesize(); + init_list_head(&f->partial_slabs); + init_list_head(&f->full_slabs); + init_list_head(&f->lru_table); + if (fuse_opt_parse(args, &f->conf, fuse_lib_opts, fuse_lib_opt_proc) == -1) goto out_free_fs; @@ -3727,7 +4651,7 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, if (!f->conf.ac_attr_timeout_set) f->conf.ac_attr_timeout = f->conf.attr_timeout; -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__NetBSD__) /* * In FreeBSD, we always use these settings as inode numbers * are needed to make getcwd(3) work. @@ -3749,66 +4673,51 @@ struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, fuse_session_add_chan(f->se, ch); - if (f->conf.debug) + if (f->conf.debug) { fprintf(stderr, "nullpath_ok: %i\n", f->nullpath_ok); + fprintf(stderr, "nopath: %i\n", f->conf.nopath); + fprintf(stderr, "utime_omit_ok: %i\n", f->utime_omit_ok); + } /* Trace topmost layer by default */ f->fs->debug = f->conf.debug; f->ctr = 0; f->generation = 0; - /* FIXME: Dynamic hash table */ - f->name_table_size = 14057; - f->name_table = (struct node **) - calloc(1, sizeof(struct node *) * f->name_table_size); - if (f->name_table == NULL) { - fprintf(stderr, "fuse: memory allocation failed\n"); + if (node_table_init(&f->name_table) == -1) goto out_free_session; - } - f->id_table_size = 14057; - f->id_table = (struct node **) - calloc(1, sizeof(struct node *) * f->id_table_size); - if (f->id_table == NULL) { - fprintf(stderr, "fuse: memory allocation failed\n"); + if (node_table_init(&f->id_table) == -1) goto out_free_name_table; - } fuse_mutex_init(&f->lock); - root = (struct node *) calloc(1, sizeof(struct node)); + root = alloc_node(f); if (root == NULL) { fprintf(stderr, "fuse: memory allocation failed\n"); goto out_free_id_table; } - root->name = strdup("/"); - if (root->name == NULL) { - fprintf(stderr, "fuse: memory allocation failed\n"); - goto out_free_root; - } + strcpy(root->inline_name, "/"); + root->name = root->inline_name; if (f->conf.intr && fuse_init_intr_signal(f->conf.intr_signal, &f->intr_installed) == -1) - goto out_free_root_name; + goto out_free_root; root->parent = NULL; root->nodeid = FUSE_ROOT_ID; - root->generation = 0; - root->refctr = 1; - root->nlookup = 1; + inc_nlookup(root); hash_id(f, root); return f; -out_free_root_name: - free(root->name); out_free_root: free(root); out_free_id_table: - free(f->id_table); + free(f->id_table.array); out_free_name_table: - free(f->name_table); + free(f->name_table.array); out_free_session: fuse_session_destroy(f->se); out_free_fs: @@ -3845,14 +4754,14 @@ void fuse_destroy(struct fuse *f) memset(c, 0, sizeof(*c)); c->ctx.fuse = f; - for (i = 0; i < f->id_table_size; i++) { + for (i = 0; i < f->id_table.size; i++) { struct node *node; - for (node = f->id_table[i]; node != NULL; + for (node = f->id_table.array[i]; node != NULL; node = node->id_next) { if (node->is_hidden) { char *path; - if (try_get_path(f, node->nodeid, NULL, &path, NULL, 0) == 0) { + if (try_get_path(f, node->nodeid, NULL, &path, NULL, false) == 0) { fuse_fs_unlink(f->fs, path); free(path); } @@ -3860,17 +4769,21 @@ void fuse_destroy(struct fuse *f) } } } - for (i = 0; i < f->id_table_size; i++) { + for (i = 0; i < f->id_table.size; i++) { struct node *node; struct node *next; - for (node = f->id_table[i]; node != NULL; node = next) { + for (node = f->id_table.array[i]; node != NULL; node = next) { next = node->id_next; - free_node(node); + free_node(f, node); + f->id_table.use--; } } - free(f->id_table); - free(f->name_table); + assert(list_empty(&f->partial_slabs)); + assert(list_empty(&f->full_slabs)); + + free(f->id_table.array); + free(f->name_table.array); pthread_mutex_destroy(&f->lock); fuse_session_destroy(f->se); free(f->conf.modules); @@ -3903,7 +4816,7 @@ void fuse_register_module(struct fuse_module *mod) fuse_modules = mod; } -#ifndef __FreeBSD__ +#if !defined(__FreeBSD__) && !defined(__NetBSD__) static struct fuse *fuse_new_common_compat(int fd, const char *opts, const struct fuse_operations *op, @@ -3960,7 +4873,7 @@ FUSE_SYMVER(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); FUSE_SYMVER(".symver fuse_new_compat2,fuse_new@"); FUSE_SYMVER(".symver fuse_new_compat22,fuse_new@FUSE_2.2"); -#endif /* __FreeBSD__ */ +#endif /* __FreeBSD__ || __NetBSD__ */ struct fuse *fuse_new_compat25(int fd, struct fuse_args *args, const struct fuse_operations_compat25 *op, |