mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-03 21:30:11 -05:00
fix: make the rename-exdev fallback safe and transactional
rename-exdev (opt-in; the default is passthrough) moves a source it cannot rename across branches into a staging tree and publishes a symlink at the destination. This reworks that fallback so a rename either completes or leaves both names as they were. - Staging opens every component relative to a pinned parent with O_NOFOLLOW, requires a daemon-owned staging root, and refuses when the tree can't be trusted. - Staged names are taken without replacement and must be free on every configured branch, not only those holding the source, so a later lookup cannot reach another rename's payload. Over-long names are shortened. Fallback renames are serialized. - Destinations: a compatible existing destination is replaced, kept under a temporary name until every branch is published, and restored if a later branch fails. A destination on a read-only branch, or a no-create one where the link can't be created, is refused before anything moves. Temporary and restore names never replace an entry, and a restore that fails is reported. - An ordinary rename (rename-exdev=passthrough) also refuses with EXDEV when the destination exists on a read-only branch that does not hold the source: that copy could neither be replaced nor removed and would shadow the renamed file there. It is never scheduled for removal either. - The replacement symlink is prepared in a daemon-private directory, its UID and GID are verified through a pinned descriptor, and it is published without replacement, before any source moves. A sticky, world-writable destination directory under Linux protected symlinks is refused. Each prepared link is tracked by identity for cleanup. - Symlink sources are recreated with their original text, owner and times instead of staged, since staging changes where a relative target resolves. Sources of different types on different branches are refused. A hidden source that cannot be removed fails the rename. - libfuse re-parents the kernel's node onto the staged inode, using a name allocated before publication, so open handles and cached dentries keep working and nothing can fail after the rename commits. - Without renameat2 (FreeBSD 14.4 and 15.0, or seccomp), regular files and directories use a portable layout: a fresh daemon-owned wrapper, access gates that mirror the source's parents with the source's GID and its read and execute bits only, and the payload directory inside the last gate. Foreign-owned parents, unsettable group ownership and non-trivial ACLs are refused before the source moves. A namespace the source never entered is removed. A portable directory that can't be restored stays staged with its path logged, and an existing directory destination is refused before a portable source moves: keeping it aside would need the no-replace rename the portable layout lacks. - Document the fallback, its refusals and FreeBSD limits. - TEST_rename_exdev_publication, TEST_rename_exdev_portable and tests.cpp cover publication rollback, shadows, collisions, ownership, sticky directories, mixed source types, a refused portable directory destination and the portable layout through a mounted pool. TEST_rename_exdev_publication keeps its fixture if its tmpfs can't be detached. Performance: an ordinary rename only adds an lstat per read-only branch, and only when one is configured. The fallback adds about two lstats per branch plus the staging work, and fallback renames run one at a time.
This commit is contained in:
parent
036246140b
commit
c9854f236f
16 changed files with 3962 additions and 202 deletions
|
|
@ -406,13 +406,12 @@ remerge_name()
|
|||
}
|
||||
}
|
||||
|
||||
// Detach from the name bucket without changing the use count. The caller
|
||||
// either transfers the node to a new bucket or drops the old reference.
|
||||
static
|
||||
void
|
||||
unhash_name(node_t *node)
|
||||
unlink_name(node_t *node)
|
||||
{
|
||||
if(not (node && node->name && node->parent))
|
||||
return;
|
||||
|
||||
size_t hash = name_hash(node->parent->nodeid,node->name);
|
||||
node_t **nodep = &f.name_table.array[hash];
|
||||
|
||||
|
|
@ -423,14 +422,6 @@ unhash_name(node_t *node)
|
|||
|
||||
*nodep = node->name_next;
|
||||
node->name_next = NULL;
|
||||
unref_node(node->parent);
|
||||
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();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -438,6 +429,34 @@ unhash_name(node_t *node)
|
|||
node->nodeid);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
relink_name(node_t *node)
|
||||
{
|
||||
size_t hash = name_hash(node->parent->nodeid,node->name);
|
||||
|
||||
node->name_next = f.name_table.array[hash];
|
||||
f.name_table.array[hash] = node;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
unhash_name(node_t *node)
|
||||
{
|
||||
if(not (node && node->name && node->parent))
|
||||
return;
|
||||
|
||||
unlink_name(node);
|
||||
unref_node(node->parent);
|
||||
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();
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
rehash_name()
|
||||
|
|
@ -1264,6 +1283,50 @@ rename_node(uint64_t olddir,
|
|||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
The filesystem replaced newname with a different object (a symlink)
|
||||
and moved the source to `relpath_` instead. The kernel has already
|
||||
moved the source's dentry, and with it this node, to newname and
|
||||
keeps using it until it revalidates the dentry. Requests for the node
|
||||
must reach the source, not the object now at newname: if they did,
|
||||
the kernel would see the inode change type and fail the request with
|
||||
EIO, and would mark the inode bad, breaking any handle still open on
|
||||
it. So re-parent the node to `relpath_` under the root. Kernel lookups
|
||||
never contain '/', so that name can't collide with a real entry. Any
|
||||
node at newname is dropped so the next lookup there creates a new
|
||||
one for the new object.
|
||||
*/
|
||||
static
|
||||
void
|
||||
relocate_node(uint64_t olddir,
|
||||
const char *oldname,
|
||||
uint64_t newdir,
|
||||
const char *newname,
|
||||
char *relpath_)
|
||||
{
|
||||
mutex_lockguard(f.lock);
|
||||
node_t *node = lookup_node(olddir,oldname);
|
||||
node_t *newnode = lookup_node(newdir,newname);
|
||||
if((newnode != NULL) && (newnode != node))
|
||||
unlink_node(newnode);
|
||||
if(node == NULL)
|
||||
{
|
||||
free(relpath_);
|
||||
return;
|
||||
}
|
||||
|
||||
// Publication has committed. Adopt the name reserved by the callback:
|
||||
// no strdup, table growth, or other fallible insertion remains here.
|
||||
node_t *root = get_node(FUSE_ROOT_ID);
|
||||
root->refctr++;
|
||||
unlink_name(node);
|
||||
unref_node(node->parent);
|
||||
free(node->name);
|
||||
node->name = relpath_;
|
||||
node->parent = root;
|
||||
relink_name(node);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
set_stat(uint64_t nodeid,
|
||||
|
|
@ -2049,11 +2112,20 @@ fuse_lib_rename(fuse_req_t *req_,
|
|||
|
||||
if(!err)
|
||||
{
|
||||
char *relocated = NULL;
|
||||
|
||||
err = f.ops.rename(&req_->ctx,
|
||||
&oldpath[1],
|
||||
&newpath[1]);
|
||||
if(!err)
|
||||
err = rename_node(hdr_->nodeid,oldname,arg->newdir,newname);
|
||||
&newpath[1],
|
||||
&relocated);
|
||||
if(!err && (relocated != NULL))
|
||||
relocate_node(hdr_->nodeid,oldname,arg->newdir,newname,relocated);
|
||||
else
|
||||
{
|
||||
free(relocated);
|
||||
if(!err)
|
||||
err = rename_node(hdr_->nodeid,oldname,arg->newdir,newname);
|
||||
}
|
||||
|
||||
free_path2(hdr_->nodeid,arg->newdir,wnode1,wnode2,oldpath,newpath);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue