Auto-commit fuse.cpp

This commit is contained in:
Antonio SJ Musumeci 2026-04-13 18:55:56 -05:00
parent d5a2187713
commit 02da8efbff
37 changed files with 374 additions and 267 deletions

View file

@ -18,6 +18,7 @@
#include "kvec.h"
#include "mutex.hpp"
#include "node.hpp"
#include "xalloc.hpp"
#include "fuse_cfg.hpp"
#include "fuse_req.hpp"
@ -290,10 +291,11 @@ node_table_reduce(struct node_table *t)
return;
newarray = realloc(t->array,sizeof(node_t*) * newsize);
if(newarray != NULL)
t->array = (node_t**)newarray;
if(newarray == NULL)
return;
t->size = newsize;
t->array = (node_t**)newarray;
t->size = newsize;
t->split = t->size / 2;
}
@ -542,8 +544,15 @@ hash_name(node_t *node,
uint64_t parentid,
const char *name)
{
size_t hash = name_hash(parentid,name);
node_t *parent = get_node(parentid);
size_t hash;
node_t *parent;
hash = name_hash(parentid,name);
parent = get_node(parentid);
if(parent == NULL)
return -1;
node->name = strdup(name);
if(node->name == NULL)
return -1;
@ -634,12 +643,18 @@ find_node(uint64_t parent,
const char *name)
{
node_t *node;
mutex_lockguard(f.lock);
mutex_lock(f.lock);
if(!name)
node = get_node(parent);
{
node = get_node(parent);
if(node == NULL)
goto out_err;
}
else
node = lookup_node(parent,name);
{
node = lookup_node(parent,name);
}
if(node == NULL)
{
@ -664,8 +679,8 @@ find_node(uint64_t parent,
remove_remembered_node(node);
}
inc_nlookup(node);
out_err:
mutex_unlock(f.lock);
return node;
}
@ -723,8 +738,11 @@ unlock_path(uint64_t nodeid,
wnode->treelock = 0;
}
for(node = get_node(nodeid);
node != end && node->nodeid != FUSE_ROOT_ID;
node = get_node(nodeid);
if(node == NULL)
return;
for(;node != end && node->nodeid != FUSE_ROOT_ID;
node = node->parent)
{
assert(node->treelock != 0);
@ -794,7 +812,14 @@ try_get_path(uint64_t nodeid,
}
}
for(node = get_node(nodeid); node->nodeid != FUSE_ROOT_ID; node = node->parent)
node = get_node(nodeid);
if(node == NULL)
{
err = -ESTALE;
goto out_unlock;
}
for(; node->nodeid != FUSE_ROOT_ID; node = node->parent)
{
err = -ESTALE;
if(node->name == NULL || node->parent == NULL)
@ -874,7 +899,8 @@ queue_element_wakeup(struct lock_queue_element *qe)
if(!qe->path1)
{
/* Just waiting for it to be unlocked */
if(get_node(qe->nodeid1)->treelock == 0)
node_t *qnode = get_node(qe->nodeid1);
if(qnode && (qnode->treelock == 0))
pthread_cond_signal(&qe->cond);
return;
@ -1102,8 +1128,11 @@ forget_node(const uint64_t nodeid,
if(nodeid == FUSE_ROOT_ID)
return;
mutex_lock(f.lock);
mutex_lockguard(f.lock);
node = get_node(nodeid);
if(node == NULL)
return;
/*
* Node may still be locked due to interrupt idiocy in open,
@ -1142,8 +1171,6 @@ forget_node(const uint64_t nodeid,
kv_push(remembered_node_t,f.remembered_nodes,fn);
}
mutex_unlock(f.lock);
}
static
@ -1353,29 +1380,34 @@ fuse_lib_lookup(fuse_req_t *req_,
if(name[1] == '\0')
{
name = NULL;
mutex_lock(f.lock);
mutex_lockguard(f.lock);
dot = get_node_nocheck(nodeid);
if(dot == NULL)
{
mutex_unlock(f.lock);
reply_entry(req_,&e,-ESTALE);
return;
}
dot->refctr++;
mutex_unlock(f.lock);
}
else if((name[1] == '.') && (name[2] == '\0'))
{
if(nodeid == 1)
if(nodeid == FUSE_ROOT_ID)
{
reply_entry(req_,&e,-ENOENT);
return;
}
name = NULL;
mutex_lock(f.lock);
nodeid = get_node(nodeid)->parent->nodeid;
mutex_unlock(f.lock);
mutex_lockguard(f.lock);
{
node_t *pnode = get_node(nodeid);
if((pnode == NULL) || (pnode->parent == NULL))
{
reply_entry(req_,&e,-ESTALE);
return;
}
nodeid = pnode->parent->nodeid;
}
}
}
@ -2187,6 +2219,11 @@ fuse_lib_read(fuse_req_t *req_,
}
msgbuf = msgbuf_alloc_page_aligned();
if(msgbuf == NULL)
{
fuse_reply_err(req_,ENOMEM);
return;
}
res = f.ops.read(&req_->ctx,
&ffi,
@ -2264,7 +2301,12 @@ get_dirhandle(const fuse_file_info_t *llfi,
{
struct fuse_dh *dh = (struct fuse_dh *)(uintptr_t)llfi->fh;
memset(fi,0,sizeof(fuse_file_info_t));
if(dh == NULL)
return NULL;
fi->fh = dh->fh;
return dh;
}
@ -2283,7 +2325,7 @@ fuse_lib_opendir(fuse_req_t *req_,
arg = (fuse_open_in*)fuse_hdr_arg(hdr_);
llffi.flags = arg->flags;
dh = (struct fuse_dh *)calloc(1,sizeof(struct fuse_dh));
dh = (fuse_dh*)calloc(1,sizeof(fuse_dh));
if(dh == NULL)
{
fuse_reply_err(req_,ENOMEM);
@ -2372,7 +2414,13 @@ fuse_lib_readdir(fuse_req_t *req_,
llffi.fh = arg->fh;
dh = get_dirhandle(&llffi,&ffi);
d = &dh->d;
if(not dh)
{
fuse_reply_err(req_,EBADF);
return;
}
d = &dh->d;
mutex_lock(dh->lock);
@ -2416,7 +2464,13 @@ fuse_lib_readdir_plus(fuse_req_t *req_,
llffi.fh = arg->fh;
dh = get_dirhandle(&llffi,&ffi);
d = &dh->d;
if(not dh)
{
fuse_reply_err(req_,EBADF);
return;
}
d = &dh->d;
mutex_lock(dh->lock);
@ -2457,6 +2511,11 @@ fuse_lib_releasedir(fuse_req_t *req_,
llffi.flags = arg->flags;
dh = get_dirhandle(&llffi,&ffi);
if(not dh)
{
fuse_reply_err(req_,EBADF);
return;
}
f.ops.releasedir(&req_->ctx,
&ffi);
@ -2485,7 +2544,11 @@ fuse_lib_fsyncdir(fuse_req_t *req_,
is_datasync = !!(arg->fsync_flags & FUSE_FSYNC_FDATASYNC);
llffi.fh = arg->fh;
get_dirhandle(&llffi,&ffi);
if(not get_dirhandle(&llffi,&ffi))
{
fuse_reply_err(req_,EBADF);
return;
}
err = f.ops.fsyncdir(&req_->ctx,
&ffi,
@ -3008,9 +3071,13 @@ fuse_flush_common(fuse_req_t *req_,
{
flock_to_lock(&lock,&l);
l.owner = ffi_->lock_owner;
mutex_lock(f.lock);
locks_insert(get_node(ino_),&l);
mutex_unlock(f.lock);
{
mutex_lockguard(f.lock);
node_t *n = get_node(ino_);
if(n)
locks_insert(n,&l);
}
/* if op.lock() is defined FLUSH is needed regardless
of op.flush() */
@ -3133,11 +3200,15 @@ fuse_lib_getlk(fuse_req_t *req_,
flock_to_lock(&flk,&lk);
lk.owner = ffi.lock_owner;
mutex_lock(f.lock);
conflict = locks_conflict(get_node(hdr_->nodeid),&lk);
if(conflict)
lock_to_flock(conflict,&flk);
mutex_unlock(f.lock);
{
mutex_lockguard(f.lock);
node_t *no = get_node(hdr_->nodeid);
conflict = (n ? locks_conflict(n,&lk) : NULL);
if(conflict)
lock_to_flock(conflict,&flk);
}
if(!conflict)
err = fuse_lock_common(req_,hdr_->nodeid,&ffi,&flk,F_GETLK);
else
@ -3157,16 +3228,26 @@ fuse_lib_setlk(fuse_req_t *req_,
struct flock *lock,
int sleep)
{
int err = fuse_lock_common(req_,ino,fi,lock,
sleep ? F_SETLKW : F_SETLK);
int err;
err = fuse_lock_common(req_,
ino,
fi,
lock,
sleep ? F_SETLKW : F_SETLK);
if(!err)
{
lock_t l;
flock_to_lock(lock,&l);
l.owner = fi->lock_owner;
mutex_lock(f.lock);
locks_insert(get_node(ino),&l);
mutex_unlock(f.lock);
{
mutex_lockguard(f.lock);
node_t *n = get_node(ino);
if(n)
locks_insert(n,&l);
}
}
fuse_reply_err(req_,err);
@ -3231,13 +3312,12 @@ fuse_lib_ioctl(fuse_req_t *req_,
const struct fuse_ioctl_in *arg;
arg = (fuse_ioctl_in*)fuse_hdr_arg(hdr_);
if((arg->flags & FUSE_IOCTL_DIR) && !(req_->conn.want & FUSE_CAP_IOCTL_DIR))
{
fuse_reply_err(req_,ENOTTY);
return;
}
if((sizeof(void*) == 4) &&
err = -EPERM;
if(arg->flags & FUSE_IOCTL_UNRESTRICTED)
goto err;
if((sizeof(void*) == 4) &&
(req_->conn.proto_minor >= 16) &&
!(arg->flags & FUSE_IOCTL_32BIT))
{
@ -3248,14 +3328,26 @@ fuse_lib_ioctl(fuse_req_t *req_,
out_size = arg->out_size;
in_buf = (arg->in_size ? PARAM(arg) : NULL);
err = -EPERM;
if(arg->flags & FUSE_IOCTL_UNRESTRICTED)
goto err;
if(arg->flags & FUSE_IOCTL_DIR)
get_dirhandle(&llffi,&ffi);
{
if(not get_dirhandle(&llffi,&ffi))
{
err = -EBADF;
goto err;
}
}
else
ffi = llffi;
{
ffi = llffi;
}
if((arg->in_size > 0) &&
(out_size > 0) &&
(arg->in_size != out_size))
{
err = -EINVAL;
goto err;
}
if(out_size)
{
@ -3263,28 +3355,27 @@ fuse_lib_ioctl(fuse_req_t *req_,
out_buf = (char*)malloc(out_size);
if(!out_buf)
goto err;
DEFER { free(out_buf); };
}
assert(!arg->in_size || !out_size || arg->in_size == out_size);
if(out_buf && in_buf)
memcpy(out_buf,in_buf,arg->in_size);
if(in_buf && out_buf)
memcpy(out_buf,in_buf,out_size);
err = f.ops.ioctl(&req_->ctx,
&ffi,
arg->cmd,
(void*)(uintptr_t)arg->arg,
arg->flags,
out_buf ?: (void *)in_buf,
out_buf ? out_buf : (void*)in_buf,
&out_size);
if(err < 0)
goto err;
fuse_reply_ioctl(req_,err,out_buf,out_size);
goto out;
return;
err:
fuse_reply_err(req_,err);
out:
free(out_buf);
}
static
@ -3606,7 +3697,10 @@ metrics_log_nodes_info_to_tmp_dir(struct fuse *f_)
struct stat st;
char const *mode = "a";
sprintf(filepath,"/tmp/mergerfs.%d.info",getpid());
snprintf(filepath,
sizeof(filepath),
"/tmp/mergerfs.%d.info",
getpid());
rv = lstat(filepath,&st);
if((rv == 0) && (st.st_size > INFO_FILE_MAX_SIZE))
@ -3750,11 +3844,11 @@ fuse_new(int fd_,
root = node_alloc();
if(root == NULL)
{
fprintf(stderr,"fuse: memory allocation failed\n");
fprintf(stderr,"mergerfs: root node alloc failed\n");
goto out_free_id_table;
}
root->name = strdup("/");
root->name = xstrdup("/");
root->parent = NULL;
root->nodeid = FUSE_ROOT_ID;