mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
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.
2246 lines
68 KiB
C++
2246 lines
68 KiB
C++
/*
|
|
ISC License
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include "fuse_rename.hpp"
|
|
|
|
#include "config.hpp"
|
|
#include "error.hpp"
|
|
#include "errno.hpp"
|
|
#include "fs_attr.hpp"
|
|
#include "fs_clonepath.hpp"
|
|
#include "fs_close.hpp"
|
|
#include "fs_fchmod.hpp"
|
|
#include "fs_fchown.hpp"
|
|
#include "fs_fstat.hpp"
|
|
#include "fs_fstatat.hpp"
|
|
#include "fs_futimens.hpp"
|
|
#include "fs_is_same_file.hpp"
|
|
#include "fs_link.hpp"
|
|
#include "fs_lstat.hpp"
|
|
#include "fs_mkdirat.hpp"
|
|
#include "fs_open.hpp"
|
|
#include "fs_openat.hpp"
|
|
#include "fs_lutimens.hpp"
|
|
#include "fs_path.hpp"
|
|
#include "fs_readlink.hpp"
|
|
#include "fs_remove.hpp"
|
|
#include "fs_rename.hpp"
|
|
#include "fs_renameat.hpp"
|
|
#include "fs_symlink.hpp"
|
|
#include "fs_unlink.hpp"
|
|
#include "fs_xattr.hpp"
|
|
#include "fuse_symlink.hpp"
|
|
#include "rnd.hpp"
|
|
#include "syslog.hpp"
|
|
|
|
#include "rename_exdev_staging.hpp"
|
|
|
|
#include "scope_guard/scope_guard.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include <dirent.h>
|
|
#include <limits.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#if defined(__linux__)
|
|
# include <sys/xattr.h>
|
|
#elif defined(__FreeBSD__)
|
|
# include <sys/acl.h>
|
|
#endif
|
|
|
|
|
|
static
|
|
bool
|
|
_contains(const std::vector<Branch*> &haystack_,
|
|
const char *needle_)
|
|
{
|
|
for(auto &hay : haystack_)
|
|
{
|
|
if(hay->path == needle_)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static
|
|
bool
|
|
_contains(const std::vector<Branch*> &haystack_,
|
|
const std::string &needle_)
|
|
{
|
|
return ::_contains(haystack_,needle_.c_str());
|
|
}
|
|
|
|
static
|
|
void
|
|
_remove(const StrVec &toremove_)
|
|
{
|
|
for(auto &path : toremove_)
|
|
fs::remove(path);
|
|
}
|
|
|
|
// A destination copy on a branch configured read-only can neither be
|
|
// replaced nor removed by the rename: it would outlive it and keep
|
|
// shadowing the new name there, which rename(2) could not have done.
|
|
// Refuse the rename before anything moves; the EXDEV fallback refuses
|
|
// for the same reason. Branches which may be modified, including
|
|
// no-create ones, keep their usual cleanup behavior.
|
|
static
|
|
int
|
|
_rename_check_unselected_ro_destination(const Branches::Ptr &branches_,
|
|
const std::vector<Branch*> &oldbranches_,
|
|
const fs::path &newfusepath_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
|
|
for(const auto &branch : *branches_)
|
|
{
|
|
if(::_contains(oldbranches_,branch.path))
|
|
continue;
|
|
if(!branch.ro())
|
|
continue;
|
|
|
|
rv = fs::lstat(branch.path / newfusepath_,&st);
|
|
if(rv == -ENOENT)
|
|
continue;
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
return -EXDEV;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static
|
|
int
|
|
_rename_create_path(const Policy::Search &searchPolicy_,
|
|
const Policy::Action &actionPolicy_,
|
|
const Branches::Ptr branches_,
|
|
const fs::path &oldfusepath_,
|
|
const fs::path &newfusepath_)
|
|
{
|
|
int rv;
|
|
Err err;
|
|
StrVec toremove;
|
|
std::vector<Branch*> newbranches;
|
|
std::vector<Branch*> oldbranches;
|
|
fs::path oldfullpath;
|
|
fs::path newfullpath;
|
|
|
|
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(oldbranches.empty())
|
|
return -ENOENT;
|
|
|
|
rv = searchPolicy_(branches_,newfusepath_.parent_path(),newbranches);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(newbranches.empty())
|
|
return -ENOENT;
|
|
|
|
rv = ::_rename_check_unselected_ro_destination(branches_,oldbranches,newfusepath_);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
for(auto &branch : *branches_)
|
|
{
|
|
newfullpath = branch.path / newfusepath_;
|
|
|
|
if(!::_contains(oldbranches,branch.path))
|
|
{
|
|
// The preflight refuses when a destination exists on an
|
|
// unselected read-only branch, so scheduling one for removal
|
|
// here could only ever hit a copy which appeared after that
|
|
// check. Such a copy is not scheduled for mutation.
|
|
if(!branch.ro())
|
|
toremove.push_back(newfullpath);
|
|
continue;
|
|
}
|
|
|
|
oldfullpath = branch.path / oldfusepath_;
|
|
|
|
rv = fs::rename(oldfullpath,newfullpath);
|
|
if(rv < 0)
|
|
{
|
|
rv = fs::clonepath(newbranches[0]->path,
|
|
branch.path,
|
|
newfusepath_.parent_path());
|
|
if(rv >= 0)
|
|
rv = fs::rename(oldfullpath,newfullpath);
|
|
}
|
|
|
|
err = rv;
|
|
if(rv < 0)
|
|
toremove.push_back(oldfullpath);
|
|
}
|
|
|
|
if(err == 0)
|
|
::_remove(toremove);
|
|
|
|
return err;
|
|
}
|
|
|
|
static
|
|
int
|
|
_rename_preserve_path(const Policy::Action &actionPolicy_,
|
|
const Branches::Ptr branches_,
|
|
const fs::path &oldfusepath_,
|
|
const fs::path &newfusepath_)
|
|
{
|
|
int rv;
|
|
bool success;
|
|
StrVec toremove;
|
|
std::vector<Branch*> oldbranches;
|
|
fs::path oldfullpath;
|
|
fs::path newfullpath;
|
|
|
|
rv = actionPolicy_(branches_,oldfusepath_,oldbranches);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = ::_rename_check_unselected_ro_destination(branches_,oldbranches,newfusepath_);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
success = false;
|
|
for(auto &branch : *branches_)
|
|
{
|
|
newfullpath = branch.path / newfusepath_;
|
|
|
|
if(!::_contains(oldbranches,branch.path))
|
|
{
|
|
// The preflight refuses when a destination exists on an
|
|
// unselected read-only branch, so scheduling one for removal
|
|
// here could only ever hit a copy which appeared after that
|
|
// check. Such a copy is not scheduled for mutation.
|
|
if(!branch.ro())
|
|
toremove.push_back(newfullpath);
|
|
continue;
|
|
}
|
|
|
|
oldfullpath = branch.path / oldfusepath_;
|
|
|
|
rv = fs::rename(oldfullpath,newfullpath);
|
|
if(rv < 0)
|
|
{
|
|
toremove.push_back(oldfullpath);
|
|
continue;
|
|
}
|
|
|
|
success = true;
|
|
}
|
|
|
|
// TODO: probably should try to be nuanced here.
|
|
if(success == false)
|
|
return -EXDEV;
|
|
|
|
::_remove(toremove);
|
|
|
|
return 0;
|
|
}
|
|
|
|
// The EXDEV fallback moves the source into a staging tree at the root
|
|
// of each branch and then points a symlink at it. The daemon does that
|
|
// work as root while the staging tree is reachable through the pool,
|
|
// so anything already in it may have been planted by a pool user. To
|
|
// avoid following planted symlinks out of the branch every component
|
|
// is opened relative to its already verified parent with O_NOFOLLOW
|
|
// and the final move is done with renameat() on those fds. Nothing in
|
|
// the staging tree is resolved by path. The staging directory name is
|
|
// the shared rename_exdev::STAGING_DIR constant.
|
|
|
|
static constexpr int STAGING_DIR_FLAGS = (O_RDONLY|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
|
|
|
|
namespace
|
|
{
|
|
class FD
|
|
{
|
|
public:
|
|
FD() = default;
|
|
explicit FD(const int fd_) : _fd(fd_) {}
|
|
FD(const FD&) = delete;
|
|
FD& operator=(const FD&) = delete;
|
|
FD(FD &&o_) noexcept : _fd(o_.release()) {}
|
|
FD& operator=(FD &&o_) noexcept
|
|
{
|
|
if(this != &o_)
|
|
reset(o_.release());
|
|
return *this;
|
|
}
|
|
~FD() { reset(); }
|
|
|
|
public:
|
|
int get() const { return _fd; }
|
|
int release() { int fd = _fd; _fd = -1; return fd; }
|
|
void reset(const int fd_ = -1)
|
|
{
|
|
if(_fd >= 0)
|
|
fs::close(_fd);
|
|
_fd = fd_;
|
|
}
|
|
|
|
private:
|
|
int _fd = -1;
|
|
};
|
|
|
|
struct Staged
|
|
{
|
|
FD srcdir;
|
|
FD dstdir;
|
|
bool portable = false;
|
|
bool directory = false;
|
|
bool moved = false;
|
|
std::string name;
|
|
fs::path branch;
|
|
fs::path relocated;
|
|
};
|
|
|
|
struct AccessGate
|
|
{
|
|
FD srcdir;
|
|
FD dir;
|
|
struct stat source;
|
|
std::string name;
|
|
};
|
|
}
|
|
|
|
// The staging root must be a real directory owned by the daemon and
|
|
// not writable by anyone else. Older releases created it 01777 so
|
|
// group/other write is removed from an existing one.
|
|
static
|
|
int
|
|
_open_staging_root(const int branchfd_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
|
|
rv = fs::mkdirat(branchfd_,rename_exdev::STAGING_DIR,0755);
|
|
if((rv < 0) && (rv != -EEXIST))
|
|
return rv;
|
|
|
|
FD fd(fs::openat(branchfd_,rename_exdev::STAGING_DIR,STAGING_DIR_FLAGS));
|
|
if(fd.get() < 0)
|
|
return fd.release();
|
|
|
|
rv = fs::fstat(fd.get(),&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(st.st_uid != ::geteuid())
|
|
return -EPERM;
|
|
|
|
if(st.st_mode & (S_IWGRP|S_IWOTH))
|
|
{
|
|
rv = fs::fchmod(fd.get(),(st.st_mode & 07777 & ~(S_IWGRP|S_IWOTH)));
|
|
if(rv < 0)
|
|
return rv;
|
|
}
|
|
|
|
return fd.release();
|
|
}
|
|
|
|
// Open the source directory `name_` under `srcfd_` and the directory
|
|
// mirroring it under `dstfd_`, creating the latter with the source's
|
|
// metadata if needed. An existing staging directory must be owned by
|
|
// the owner of the source directory it mirrors (as a clone would be)
|
|
// and is not allowed to grant permissions the source does not.
|
|
static
|
|
int
|
|
_open_staging_dir(const int srcfd_,
|
|
const int dstfd_,
|
|
const std::string &name_,
|
|
FD &srcout_,
|
|
FD &dstout_)
|
|
{
|
|
int rv;
|
|
struct stat srcst;
|
|
struct stat dstst;
|
|
|
|
FD srcfd(fs::openat(srcfd_,name_,STAGING_DIR_FLAGS));
|
|
if(srcfd.get() < 0)
|
|
return srcfd.release();
|
|
|
|
rv = fs::fstat(srcfd.get(),&srcst);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = fs::mkdirat(dstfd_,name_,(srcst.st_mode & 07777));
|
|
if((rv < 0) && (rv != -EEXIST))
|
|
return rv;
|
|
const bool created = (rv == 0);
|
|
|
|
FD dstfd(fs::openat(dstfd_,name_,STAGING_DIR_FLAGS));
|
|
if(dstfd.get() < 0)
|
|
return dstfd.release();
|
|
|
|
if(created)
|
|
{
|
|
// Metadata is best effort, as with fs::clonepath().
|
|
fs::attr::copy(srcfd.get(),dstfd.get(),FS_ATTR_CLEAR_IMMUTABLE);
|
|
fs::xattr::copy(srcfd.get(),dstfd.get());
|
|
fs::fchown_check_on_error(dstfd.get(),srcst);
|
|
fs::fchmod_check_on_error(dstfd.get(),srcst);
|
|
fs::futimens(dstfd.get(),srcst);
|
|
}
|
|
else
|
|
{
|
|
rv = fs::fstat(dstfd.get(),&dstst);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(dstst.st_uid != srcst.st_uid)
|
|
return -EPERM;
|
|
|
|
const mode_t extra = (dstst.st_mode & ~srcst.st_mode & 07777);
|
|
if(extra)
|
|
{
|
|
rv = fs::fchmod(dstfd.get(),(dstst.st_mode & 07777 & ~extra));
|
|
if(rv < 0)
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
srcout_ = std::move(srcfd);
|
|
dstout_ = std::move(dstfd);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#if defined(__linux__)
|
|
static
|
|
bool
|
|
_acl_blob_absent(const int rv_)
|
|
{
|
|
return ((rv_ == -ENODATA) || (rv_ == -EOPNOTSUPP) || (rv_ == -ENOTSUP));
|
|
}
|
|
|
|
static
|
|
int
|
|
_require_acl_absent(const int fd_,
|
|
const char *name_)
|
|
{
|
|
const ssize_t rv = ::fgetxattr(fd_,name_,NULL,0);
|
|
if(rv >= 0)
|
|
return -EOPNOTSUPP;
|
|
return (::_acl_blob_absent(-errno) ? 0 : -errno);
|
|
}
|
|
|
|
static
|
|
int
|
|
_clear_acl_blob(const int fd_,
|
|
const char *name_)
|
|
{
|
|
const int rv = ::to_neg_errno(::fremovexattr(fd_,name_));
|
|
return (::_acl_blob_absent(rv) ? 0 : rv);
|
|
}
|
|
|
|
static
|
|
int
|
|
_prepare_fresh_acl(const int fd_)
|
|
{
|
|
int rv = ::_clear_acl_blob(fd_,"system.posix_acl_access");
|
|
if(rv < 0)
|
|
return rv;
|
|
return ::_clear_acl_blob(fd_,"system.posix_acl_default");
|
|
}
|
|
|
|
static
|
|
int
|
|
_verify_mode_only_acl(const int srcfd_,
|
|
const int dstfd_)
|
|
{
|
|
for(const char *name : {"system.posix_acl_access","system.posix_acl_default"})
|
|
{
|
|
int rv = ::_require_acl_absent(srcfd_,name);
|
|
if(rv < 0)
|
|
return rv;
|
|
rv = ::_require_acl_absent(dstfd_,name);
|
|
if(rv < 0)
|
|
return rv;
|
|
}
|
|
return 0;
|
|
}
|
|
#elif defined(__FreeBSD__)
|
|
struct FreeBSDACLTypes
|
|
{
|
|
bool nfs4 = false;
|
|
bool posix1e = false;
|
|
};
|
|
|
|
static
|
|
int
|
|
_freebsd_acl_types(const int fd_,
|
|
FreeBSDACLTypes &types_)
|
|
{
|
|
errno = 0;
|
|
const long nfs4 = ::fpathconf(fd_,_PC_ACL_NFS4);
|
|
if(nfs4 < 0)
|
|
return (errno ? -errno : -EOPNOTSUPP);
|
|
types_.nfs4 = (nfs4 == 1);
|
|
if(types_.nfs4)
|
|
return 0;
|
|
|
|
errno = 0;
|
|
const long posix1e = ::fpathconf(fd_,_PC_ACL_EXTENDED);
|
|
if(posix1e < 0)
|
|
return (errno ? -errno : -EOPNOTSUPP);
|
|
types_.posix1e = (posix1e == 1);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static
|
|
int
|
|
_freebsd_require_trivial_acl(const int fd_,
|
|
const acl_type_t type_)
|
|
{
|
|
acl_t acl = ::acl_get_fd_np(fd_,type_);
|
|
if(acl == NULL)
|
|
return ((errno == ENOENT) ? 0 : -errno);
|
|
|
|
// UFS represents an absent default ACL as a successful zero-entry
|
|
// ACL. It is absent for access purposes, not a nontrivial ACL.
|
|
acl_entry_t entry;
|
|
const int has_entry = ::acl_get_entry(acl,ACL_FIRST_ENTRY,&entry);
|
|
if(has_entry <= 0)
|
|
{
|
|
const int entry_errno = errno;
|
|
::acl_free(acl);
|
|
return ((has_entry == 0) ? 0 : -entry_errno);
|
|
}
|
|
|
|
int trivial = 0;
|
|
const int rv = ::acl_is_trivial_np(acl,&trivial);
|
|
const int saved_errno = errno;
|
|
::acl_free(acl);
|
|
if(rv < 0)
|
|
return -saved_errno;
|
|
|
|
// No ACL copier is needed: native mode-only ACLs are already exactly
|
|
// represented by st_mode. This also leaves ordinary trivial ZFS ACLs
|
|
// alone instead of trying the unsupported NFSv4 delete operation.
|
|
return (trivial ? 0 : -EOPNOTSUPP);
|
|
}
|
|
|
|
static
|
|
int
|
|
_prepare_fresh_acl(const int fd_)
|
|
{
|
|
FreeBSDACLTypes types;
|
|
int rv = ::_freebsd_acl_types(fd_,types);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
if(types.nfs4)
|
|
return ::_freebsd_require_trivial_acl(fd_,ACL_TYPE_NFS4);
|
|
if(types.posix1e)
|
|
{
|
|
rv = ::_freebsd_require_trivial_acl(fd_,ACL_TYPE_ACCESS);
|
|
if(rv < 0)
|
|
return rv;
|
|
return ::_freebsd_require_trivial_acl(fd_,ACL_TYPE_DEFAULT);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static
|
|
int
|
|
_verify_mode_only_acl(const int srcfd_,
|
|
const int dstfd_)
|
|
{
|
|
FreeBSDACLTypes srctypes;
|
|
FreeBSDACLTypes dsttypes;
|
|
int rv = ::_freebsd_acl_types(srcfd_,srctypes);
|
|
if(rv < 0)
|
|
return rv;
|
|
rv = ::_freebsd_acl_types(dstfd_,dsttypes);
|
|
if(rv < 0)
|
|
return rv;
|
|
if((srctypes.nfs4 != dsttypes.nfs4) ||
|
|
(srctypes.posix1e != dsttypes.posix1e))
|
|
return -EOPNOTSUPP;
|
|
|
|
if(srctypes.nfs4)
|
|
{
|
|
rv = ::_freebsd_require_trivial_acl(srcfd_,ACL_TYPE_NFS4);
|
|
if(rv < 0)
|
|
return rv;
|
|
return ::_freebsd_require_trivial_acl(dstfd_,ACL_TYPE_NFS4);
|
|
}
|
|
if(srctypes.posix1e)
|
|
{
|
|
for(const acl_type_t type : {ACL_TYPE_ACCESS,ACL_TYPE_DEFAULT})
|
|
{
|
|
rv = ::_freebsd_require_trivial_acl(srcfd_,type);
|
|
if(rv < 0)
|
|
return rv;
|
|
rv = ::_freebsd_require_trivial_acl(dstfd_,type);
|
|
if(rv < 0)
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
#else
|
|
static
|
|
int
|
|
_prepare_fresh_acl(const int)
|
|
{
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
static
|
|
int
|
|
_verify_mode_only_acl(const int,const int)
|
|
{
|
|
return -EOPNOTSUPP;
|
|
}
|
|
#endif
|
|
|
|
// Create a new directory which only the daemon can enter. The caller
|
|
// opens it to 0555 only after the source has been moved and all access
|
|
// gates have been populated. A plain renameat() into this empty,
|
|
// private directory cannot replace an entry planted by a pool user.
|
|
static
|
|
int
|
|
_open_fresh_staging_dir(const int parentfd_,
|
|
const std::string &name_,
|
|
FD &out_)
|
|
{
|
|
int rv = fs::mkdirat(parentfd_,name_,0700);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
// Only the daemon can modify `parentfd_`, so the name still denotes
|
|
// the directory just created if it turns out to be unusable.
|
|
bool usable = false;
|
|
DEFER { if(!usable) ::unlinkat(parentfd_,name_.c_str(),AT_REMOVEDIR); };
|
|
|
|
FD dir(fs::openat(parentfd_,name_,STAGING_DIR_FLAGS));
|
|
if(dir.get() < 0)
|
|
return dir.release();
|
|
|
|
struct stat st;
|
|
rv = fs::fstat(dir.get(),&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(!S_ISDIR(st.st_mode) ||
|
|
(st.st_uid != ::geteuid()) ||
|
|
(st.st_mode & (S_IWGRP|S_IWOTH)))
|
|
return -EPERM;
|
|
|
|
// mkdirat() succeeded exclusively, and the directory is not publicly
|
|
// traversable. Remove inherited ACL state where possible; otherwise
|
|
// require the inherited ACL to be trivial before using this gate.
|
|
rv = ::_prepare_fresh_acl(dir.get());
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
usable = true;
|
|
out_ = std::move(dir);
|
|
return 0;
|
|
}
|
|
|
|
// Access gates are initially private like the rest of the namespace.
|
|
// Their metadata is applied after the complete hierarchy is created
|
|
// but before the payload moves. The pinned source and payload fds make
|
|
// the later rename independent of the gates' final permissions.
|
|
static
|
|
int
|
|
_prepare_access_gate(const int srcfd_,
|
|
const int dstfd_,
|
|
const std::string &name_,
|
|
std::vector<AccessGate> &gates_)
|
|
{
|
|
int rv;
|
|
struct stat srcst;
|
|
|
|
rv = fs::fstat(srcfd_,&srcst);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(!S_ISDIR(srcst.st_mode))
|
|
return -ENOTDIR;
|
|
|
|
// A gate must be owned by the daemon: a gate owned by anyone else
|
|
// could chmod its own mirror back open and rename the payload out
|
|
// of it. Sources with foreign-owned ancestors are refused before
|
|
// anything moves.
|
|
if(srcst.st_uid != ::geteuid())
|
|
return -EPERM;
|
|
|
|
FD srcdir(fs::openat(srcfd_,".",STAGING_DIR_FLAGS));
|
|
if(srcdir.get() < 0)
|
|
return srcdir.release();
|
|
FD dst;
|
|
rv = ::_open_fresh_staging_dir(dstfd_,name_,dst);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
fs::attr::copy(srcfd_,dst.get(),FS_ATTR_CLEAR_IMMUTABLE);
|
|
|
|
AccessGate gate;
|
|
gate.srcdir = std::move(srcdir);
|
|
gate.dir = std::move(dst);
|
|
gate.source = srcst;
|
|
gate.name = name_;
|
|
gates_.emplace_back(std::move(gate));
|
|
|
|
return 0;
|
|
}
|
|
|
|
static
|
|
int
|
|
_finalize_access_gates(std::vector<AccessGate> &gates_)
|
|
{
|
|
for(auto &gate : gates_)
|
|
{
|
|
int rv = fs::fchown_check_on_error(gate.dir.get(),gate.source);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
// Gates are applied once, after all of them exist but before the
|
|
// payload moves: they mirror the source's read and execute bits
|
|
// and clear every write, setid and sticky bit. There is no
|
|
// intermediate window in which a gate is writable by a non-daemon
|
|
// user, and no gate owner exists who could change it back.
|
|
const mode_t want = gate.source.st_mode & 0555;
|
|
rv = fs::fchmod(gate.dir.get(),want);
|
|
if(rv < 0)
|
|
return rv;
|
|
fs::futimens(gate.dir.get(),gate.source);
|
|
|
|
rv = ::_verify_mode_only_acl(gate.srcdir.get(),gate.dir.get());
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
struct stat st;
|
|
rv = fs::fstat(gate.dir.get(),&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
if((st.st_uid != ::geteuid()) ||
|
|
(st.st_gid != gate.source.st_gid) ||
|
|
((st.st_mode & 07777) != want))
|
|
return -EPERM;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Move `branch_/oldfusepath_` to
|
|
// `branch_/.mergerfs_rename_exdev/<oldfusepath_'s parent>/stagedname_`
|
|
// without replacing anything already staged there.
|
|
static
|
|
int
|
|
_rename_exdev_stage(const Branch *branch_,
|
|
const fs::path &oldfusepath_,
|
|
const std::string &stagedname_,
|
|
Staged &staged_)
|
|
{
|
|
int rv;
|
|
FD srcfd;
|
|
FD dstfd;
|
|
const std::string name = oldfusepath_.filename().string();
|
|
|
|
srcfd.reset(fs::open(branch_->path.string(),(O_RDONLY|O_DIRECTORY|O_CLOEXEC)));
|
|
if(srcfd.get() < 0)
|
|
return srcfd.release();
|
|
|
|
dstfd.reset(::_open_staging_root(srcfd.get()));
|
|
if(dstfd.get() < 0)
|
|
return dstfd.release();
|
|
|
|
for(const auto &component : oldfusepath_.parent_path())
|
|
{
|
|
FD nextsrc;
|
|
FD nextdst;
|
|
|
|
rv = ::_open_staging_dir(srcfd.get(),
|
|
dstfd.get(),
|
|
component.string(),
|
|
nextsrc,
|
|
nextdst);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
srcfd = std::move(nextsrc);
|
|
dstfd = std::move(nextdst);
|
|
}
|
|
|
|
rv = fs::renameat_noreplace(srcfd.get(),name,dstfd.get(),stagedname_);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
staged_.srcdir = std::move(srcfd);
|
|
staged_.dstdir = std::move(dstfd);
|
|
staged_.moved = true;
|
|
staged_.name = stagedname_;
|
|
staged_.branch = branch_->path;
|
|
staged_.relocated = oldfusepath_.parent_path() / stagedname_;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Remove a portable namespace the source never entered. This request
|
|
// created every directory in it and the wrapper keeps them private
|
|
// until publication, so removal by name relative to the pinned parents
|
|
// can't affect anything else. A failed finalization may have applied
|
|
// the source's mode to a gate, so each parent is made writable first.
|
|
static
|
|
void
|
|
_remove_unused_portable_namespace(const int stagingrootfd_,
|
|
const std::string &wrappername_,
|
|
const FD &wrapper_,
|
|
const std::vector<AccessGate> &gates_,
|
|
const FD &payload_)
|
|
{
|
|
if(wrapper_.get() < 0)
|
|
return;
|
|
|
|
// The real payload directory sits inside the last gate, or directly
|
|
// in the wrapper for a root source.
|
|
if(payload_.get() >= 0)
|
|
{
|
|
const int parentfd = (gates_.empty() ?
|
|
wrapper_.get() :
|
|
gates_.back().dir.get());
|
|
fs::fchmod(parentfd,0700);
|
|
::unlinkat(parentfd,".payload",AT_REMOVEDIR);
|
|
}
|
|
|
|
for(size_t i = gates_.size(); i-- > 0;)
|
|
{
|
|
const int parentfd = ((i == 0) ?
|
|
wrapper_.get() :
|
|
gates_[i - 1].dir.get());
|
|
fs::fchmod(parentfd,0700);
|
|
::unlinkat(parentfd,gates_[i].name.c_str(),AT_REMOVEDIR);
|
|
}
|
|
|
|
::unlinkat(stagingrootfd_,wrappername_.c_str(),AT_REMOVEDIR);
|
|
}
|
|
|
|
// Without renameat2, move the payload into a fresh private namespace:
|
|
//
|
|
// .mergerfs_rename_exdev/<random>/<old parent>/.payload/<name>
|
|
//
|
|
// The access gates mirror the old source parents so a pool user meets
|
|
// the same directory permissions on the way to the payload. The real
|
|
// `.payload` directory is the last component of that route, created
|
|
// inside the final gate while the whole namespace is still private
|
|
// (directly in the wrapper for a root source). There is no
|
|
// intermediate link and no path to the payload which does not pass
|
|
// through every gate. Gates are owned by the daemon, so no gate owner
|
|
// exists which could rename the payload out of its mirror; their mode
|
|
// mirrors the source's read and execute bits with every write, setid
|
|
// and sticky bit cleared, applied once before the move. Ancestors
|
|
// owned by anyone else cannot be represented this way and are refused
|
|
// before the source moves. The backing branch root is deliberately
|
|
// not copied: reaching the staging tree already means traversing the
|
|
// branch root itself, so its permissions apply to the staged payload
|
|
// exactly as they do to the source's original path. The wrapper and
|
|
// `.payload` are created exclusively 0700 and verified as real
|
|
// directories through pinned descriptors, and become 0555 only after
|
|
// the move; neither is ever publicly writable.
|
|
static
|
|
int
|
|
_rename_exdev_stage_portable(const Branch *branch_,
|
|
const fs::path &oldfusepath_,
|
|
const std::string &wrappername_,
|
|
Staged &staged_,
|
|
fs::path &stagedpath_)
|
|
{
|
|
int rv;
|
|
const std::string name = oldfusepath_.filename().string();
|
|
|
|
FD srcfd(fs::open(branch_->path.string(),(O_RDONLY|O_DIRECTORY|O_CLOEXEC)));
|
|
if(srcfd.get() < 0)
|
|
return srcfd.release();
|
|
|
|
FD stagingroot(::_open_staging_root(srcfd.get()));
|
|
if(stagingroot.get() < 0)
|
|
return stagingroot.release();
|
|
|
|
FD wrapper;
|
|
FD payload;
|
|
std::vector<AccessGate> gates;
|
|
bool moved = false;
|
|
// Repeated setup failures must not accumulate namespaces.
|
|
DEFER
|
|
{
|
|
if(!moved)
|
|
::_remove_unused_portable_namespace(stagingroot.get(),
|
|
wrappername_,
|
|
wrapper,
|
|
gates,
|
|
payload);
|
|
};
|
|
|
|
rv = ::_open_fresh_staging_dir(stagingroot.get(),wrappername_,wrapper);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
int parentfd = wrapper.get();
|
|
for(const auto &component : oldfusepath_.parent_path())
|
|
{
|
|
FD nextsrc(fs::openat(srcfd.get(),
|
|
component.string(),
|
|
STAGING_DIR_FLAGS));
|
|
if(nextsrc.get() < 0)
|
|
return nextsrc.release();
|
|
|
|
rv = ::_prepare_access_gate(nextsrc.get(),
|
|
parentfd,
|
|
component.string(),
|
|
gates);
|
|
if(rv < 0)
|
|
return rv;
|
|
parentfd = gates.back().dir.get();
|
|
srcfd = std::move(nextsrc);
|
|
}
|
|
|
|
// The real payload directory is created inside the final gate, or
|
|
// directly in the wrapper for a root source, while that parent is
|
|
// still private and writable by the daemon. It is the only `.payload`
|
|
// created by mergerfs: a source component named `.payload` is an
|
|
// ordinary mirrored directory like any other.
|
|
rv = ::_open_fresh_staging_dir(parentfd,".payload",payload);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
// Reject missing ownership, mode, or nontrivial ACLs while the source
|
|
// is still at its original path. Only descriptor and final mode
|
|
// failures after this point require the conservative rollback below.
|
|
rv = ::_finalize_access_gates(gates);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = fs::renameat(srcfd.get(),name,payload.get(),name);
|
|
if(rv < 0)
|
|
return rv;
|
|
moved = true;
|
|
|
|
staged_.portable = true;
|
|
staged_.moved = true;
|
|
staged_.name = name;
|
|
staged_.branch = branch_->path;
|
|
staged_.srcdir = std::move(srcfd);
|
|
staged_.dstdir = std::move(payload);
|
|
staged_.relocated = fs::path(wrappername_) /
|
|
oldfusepath_.parent_path() / ".payload" / name;
|
|
|
|
struct stat st;
|
|
rv = fs::fstatat_nofollow(staged_.dstdir.get(),name.c_str(),&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
staged_.directory = S_ISDIR(st.st_mode);
|
|
|
|
// The payload directory becomes traversable but is never writable.
|
|
rv = fs::fchmod(staged_.dstdir.get(),0555);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
struct stat payloadst;
|
|
rv = fs::fstat(staged_.dstdir.get(),&payloadst);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(payloadst.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH))
|
|
return -EPERM;
|
|
|
|
// The name the replacement symlink publishes must reach this same
|
|
// `.payload` directory through the gate chain (or the wrapper, for a
|
|
// root source).
|
|
rv = fs::fstatat_nofollow(parentfd,".payload",&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(!fs::same_inode(st,payloadst))
|
|
return -EPERM;
|
|
|
|
struct stat wrapperst;
|
|
rv = fs::fstat(wrapper.get(),&wrapperst);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(wrapperst.st_mode & (S_IWGRP|S_IWOTH))
|
|
return -EPERM;
|
|
|
|
// Verify the wrapper is still the directory this request created,
|
|
// then publish the namespace. Until this succeeds no pool user can
|
|
// traverse into its access gates or the payload.
|
|
rv = fs::fstatat_nofollow(stagingroot.get(),wrappername_.c_str(),&st);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(!fs::same_inode(st,wrapperst))
|
|
return -EPERM;
|
|
|
|
rv = fs::fchmod(wrapper.get(),0555);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
stagedpath_ = staged_.relocated;
|
|
return 0;
|
|
}
|
|
|
|
// Undo only moves which actually happened. Native staging has an atomic
|
|
// no-replace rollback. Portable regular files can be restored with an
|
|
// atomic hard-link publication followed by removal of the staged link.
|
|
// Portable directories have no portable no-replace publication
|
|
// operation. Leave them staged and report their recovery path rather
|
|
// than ever replacing a source recreated by another request.
|
|
static
|
|
bool
|
|
_rename_exdev_unstage(std::vector<Staged> &staged_,
|
|
const fs::path &oldfusepath_)
|
|
{
|
|
bool restored = true;
|
|
const std::string sourcename = oldfusepath_.filename().string();
|
|
|
|
for(auto &staged : staged_)
|
|
{
|
|
if(!staged.moved)
|
|
continue;
|
|
|
|
int rv;
|
|
if(!staged.portable)
|
|
{
|
|
rv = fs::renameat_noreplace(staged.dstdir.get(),staged.name,
|
|
staged.srcdir.get(),sourcename);
|
|
}
|
|
else if(staged.directory)
|
|
{
|
|
rv = -EOPNOTSUPP;
|
|
}
|
|
else
|
|
{
|
|
rv = ::to_neg_errno(::linkat(staged.dstdir.get(),staged.name.c_str(),
|
|
staged.srcdir.get(),sourcename.c_str(),0));
|
|
// Only after the old name is restored, reopen write permission
|
|
// on our pinned, daemon-owned directory to remove the spare link.
|
|
if(rv == 0)
|
|
rv = fs::fchmod(staged.dstdir.get(),0700);
|
|
if(rv == 0)
|
|
rv = ::to_neg_errno(::unlinkat(staged.dstdir.get(),
|
|
staged.name.c_str(),0));
|
|
}
|
|
|
|
if(rv < 0)
|
|
{
|
|
restored = false;
|
|
SysLog::error("rename-exdev rollback failed (errno={}); staged data remains at {}",
|
|
-rv,
|
|
(staged.branch / rename_exdev::STAGING_DIR / staged.relocated).string());
|
|
}
|
|
}
|
|
|
|
staged_.clear();
|
|
return restored;
|
|
}
|
|
|
|
// A random leaf short enough to fit `name_max_`. Used when even the
|
|
// shortest suffixed form of the source's name cannot fit: the staged
|
|
// name is internal and opaque, so it only has to be unique within the
|
|
// staging directory, which a truncated random value provides. The
|
|
// smallest name limit POSIX requires is 14 characters, and this fits
|
|
// wherever the staging root itself is representable.
|
|
static
|
|
std::string
|
|
_rename_exdev_opaque_leaf_name(const size_t name_max_)
|
|
{
|
|
char name[32];
|
|
const int len = snprintf(name,sizeof(name),"%016llx",
|
|
(unsigned long long)RND::rand64());
|
|
|
|
if(len <= 0)
|
|
return std::string();
|
|
|
|
return std::string(name,std::min<size_t>((size_t)len,name_max_));
|
|
}
|
|
|
|
// The staged name is the source's leaf name, plus a random suffix from
|
|
// the second attempt on. The suffixed name must fit the staging
|
|
// directory of every branch it is created on, so it is shortened as
|
|
// needed; the shortened leaf is opaque and only has to be unique here.
|
|
static
|
|
std::string
|
|
_rename_exdev_staged_name(const fs::path &oldfusepath_,
|
|
const int attempt_,
|
|
const size_t name_max_)
|
|
{
|
|
char suffix[32];
|
|
std::string name = oldfusepath_.filename().string();
|
|
|
|
if(attempt_ == 0)
|
|
return name;
|
|
|
|
snprintf(suffix,sizeof(suffix),".%016llx",
|
|
(unsigned long long)RND::rand64());
|
|
|
|
const size_t suffixlen = strlen(suffix);
|
|
if(name.size() + suffixlen > name_max_)
|
|
{
|
|
// The suffix is 17 characters and the smallest NAME_MAX POSIX
|
|
// requires is 14, so some filesystems leave no room for the
|
|
// source's name once it is appended. Such a rename still uses a
|
|
// bounded opaque random leaf instead of failing outright.
|
|
if(name_max_ <= suffixlen)
|
|
return ::_rename_exdev_opaque_leaf_name(name_max_);
|
|
name.resize(name_max_ - suffixlen);
|
|
}
|
|
|
|
return name + suffix;
|
|
}
|
|
|
|
static
|
|
std::string
|
|
_rename_exdev_wrapper_name()
|
|
{
|
|
char name[32];
|
|
snprintf(name,sizeof(name),".p-%016llx",
|
|
(unsigned long long)RND::rand64());
|
|
return name;
|
|
}
|
|
|
|
// The staged leaf is created on every source branch, so the retry
|
|
// suffix is fitted to the smallest NAME_MAX among them. Filesystems
|
|
// which do not report a limit use NAME_MAX.
|
|
static
|
|
size_t
|
|
_rename_exdev_min_name_max(const std::vector<Branch*> &obranches_)
|
|
{
|
|
size_t name_max = NAME_MAX;
|
|
|
|
for(const auto &branch : obranches_)
|
|
{
|
|
const long max = ::pathconf(branch->path.c_str(),_PC_NAME_MAX);
|
|
if(max > 0)
|
|
name_max = std::min<size_t>(name_max,(size_t)max);
|
|
}
|
|
|
|
return name_max;
|
|
}
|
|
|
|
// The staged path must be absent from every configured branch: the
|
|
// published symlink resolves through the pool, so a name occupied on
|
|
// any branch, including read-only and no-create ones, could be followed
|
|
// instead of the staged copy. A missing name is free. A name which is
|
|
// there is a collision, and another name is tried. Any other error
|
|
// cannot be verified and fails the rename rather than being mistaken
|
|
// for either.
|
|
static
|
|
int
|
|
_rename_exdev_staged_target_absent(const Branches::Ptr &branches_,
|
|
const fs::path &stagedpath_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
|
|
for(const auto &branch : *branches_)
|
|
{
|
|
rv = fs::lstat(branch.path / rename_exdev::STAGING_DIR / stagedpath_,&st);
|
|
if(rv == -ENOENT)
|
|
continue;
|
|
if(rv < 0)
|
|
return rv;
|
|
return -EEXIST;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// A destination copy pinned by the preflight scan: the branch it sits
|
|
// on and the identity it had there. Publication may replace an object
|
|
// only while it still matches this identity; anything else at the name
|
|
// appeared after the scan and is not ours to replace.
|
|
struct DestinationMember
|
|
{
|
|
fs::path branch;
|
|
dev_t dev;
|
|
ino_t ino;
|
|
bool directory;
|
|
};
|
|
|
|
static
|
|
const DestinationMember *
|
|
_rename_exdev_find_member(const std::vector<DestinationMember> &members_,
|
|
const fs::path &branch_)
|
|
{
|
|
for(const auto &member : members_)
|
|
{
|
|
if(member.branch == branch_)
|
|
return &member;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
// Every pinned destination copy must sit on a branch where the
|
|
// replacement link was created: a copy on any other branch would
|
|
// shadow the published name there and could only be removed behind
|
|
// the back of a rename(2). Refused before anything is moved, because
|
|
// a portable directory cannot always be unstaged once it has been.
|
|
static
|
|
int
|
|
_rename_exdev_check_prepared_covers_members(const std::vector<DestinationMember> &members_,
|
|
const std::vector<FUSE::PreparedSymlink> &prepared_)
|
|
{
|
|
for(const auto &member : members_)
|
|
{
|
|
bool covered = false;
|
|
|
|
for(const auto &record : prepared_)
|
|
{
|
|
if(record.branch == member.branch)
|
|
{
|
|
covered = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(!covered)
|
|
return -EXDEV;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Stage the source on every branch in `obranches_`. Every branch gets
|
|
// the same relative path so a single symlink can point to it. Prefer
|
|
// the original layout when atomic no-replace rename is available. If it
|
|
// is not, all branches instead use the same fresh portable namespace.
|
|
// Each candidate path is first verified absent from every configured
|
|
// branch; a taken name, including one left by an earlier transaction
|
|
// on a branch nothing is staged into, is retried under a fresh name
|
|
// while an unverifiable one fails the rename. Before each attempt
|
|
// `prepare_` creates the real temporary replacement symlink for that
|
|
// path, so ownership, permission and usability failures happen before
|
|
// anything is moved, and the prepared links are checked against the
|
|
// pinned destinations before the first source move: a portable
|
|
// directory cannot always be unstaged once it has been.
|
|
template<typename PrepareSymlink>
|
|
static
|
|
int
|
|
_rename_exdev_rename_target(const Branches::Ptr &branches_,
|
|
const std::vector<Branch*> &obranches_,
|
|
const fs::path &oldfusepath_,
|
|
const std::vector<DestinationMember> &members_,
|
|
std::vector<FUSE::PreparedSymlink> &prepared_,
|
|
std::vector<Staged> &staged_,
|
|
fs::path &stagedpath_,
|
|
PrepareSymlink prepare_)
|
|
{
|
|
int rv = 0;
|
|
const size_t name_max = ::_rename_exdev_min_name_max(obranches_);
|
|
|
|
for(int attempt = 0; attempt < 8; attempt++)
|
|
{
|
|
const std::string stagedname =
|
|
::_rename_exdev_staged_name(oldfusepath_,attempt,name_max);
|
|
if(stagedname.empty())
|
|
return -EXDEV;
|
|
|
|
const fs::path stagedpath = oldfusepath_.parent_path() / stagedname;
|
|
|
|
rv = ::_rename_exdev_staged_target_absent(branches_,stagedpath);
|
|
if(rv < 0)
|
|
{
|
|
if((rv == -EEXIST) || (rv == -ENAMETOOLONG))
|
|
continue;
|
|
return rv;
|
|
}
|
|
|
|
rv = prepare_(stagedpath);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = ::_rename_exdev_check_prepared_covers_members(members_,prepared_);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
for(auto &branch : obranches_)
|
|
{
|
|
Staged staged;
|
|
rv = ::_rename_exdev_stage(branch,oldfusepath_,stagedname,staged);
|
|
if(staged.moved)
|
|
staged_.emplace_back(std::move(staged));
|
|
if(rv < 0)
|
|
break;
|
|
}
|
|
|
|
if(rv >= 0)
|
|
{
|
|
stagedpath_ = stagedpath;
|
|
return 0;
|
|
}
|
|
|
|
const int stage_rv = rv;
|
|
if(!::_rename_exdev_unstage(staged_,oldfusepath_))
|
|
return -EXDEV;
|
|
if((stage_rv != -EEXIST) && (stage_rv != -EOPNOTSUPP))
|
|
return -EXDEV;
|
|
if(stage_rv == -EOPNOTSUPP)
|
|
break;
|
|
}
|
|
|
|
if(rv != -EOPNOTSUPP)
|
|
return -EXDEV;
|
|
|
|
// Keeping a directory destination for rollback needs no-replace
|
|
// rename. Refuse before staging a directory we cannot put back.
|
|
for(const auto &member : members_)
|
|
{
|
|
if(member.directory)
|
|
return -EXDEV;
|
|
}
|
|
|
|
for(int attempt = 0; attempt < 8; attempt++)
|
|
{
|
|
const std::string wrappername = ::_rename_exdev_wrapper_name();
|
|
fs::path candidate = fs::path(wrappername) /
|
|
oldfusepath_.parent_path() / ".payload" /
|
|
oldfusepath_.filename();
|
|
|
|
rv = ::_rename_exdev_staged_target_absent(branches_,candidate);
|
|
if(rv < 0)
|
|
{
|
|
if(rv == -EEXIST)
|
|
continue;
|
|
return rv;
|
|
}
|
|
|
|
// In particular, create the real temporary symlink and confirm
|
|
// it covers every pinned destination before moving a directory.
|
|
// Portable directory rollback cannot safely replace a
|
|
// concurrently recreated source, so common creation, ownership
|
|
// and permission failures must happen before the first move.
|
|
rv = prepare_(candidate);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = ::_rename_exdev_check_prepared_covers_members(members_,prepared_);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
for(auto &branch : obranches_)
|
|
{
|
|
Staged staged;
|
|
fs::path branchpath;
|
|
rv = ::_rename_exdev_stage_portable(branch,
|
|
oldfusepath_,
|
|
wrappername,
|
|
staged,
|
|
branchpath);
|
|
if(rv >= 0)
|
|
candidate = branchpath;
|
|
if(staged.moved)
|
|
staged_.emplace_back(std::move(staged));
|
|
if(rv < 0)
|
|
break;
|
|
}
|
|
|
|
if(rv >= 0)
|
|
{
|
|
stagedpath_ = candidate;
|
|
return 0;
|
|
}
|
|
|
|
const int stage_rv = rv;
|
|
if(!::_rename_exdev_unstage(staged_,oldfusepath_))
|
|
return -EXDEV;
|
|
if(stage_rv != -EEXIST)
|
|
return -EXDEV;
|
|
}
|
|
|
|
return -EXDEV;
|
|
}
|
|
|
|
// Returns 0 when the directory is empty, 1 when it is not, and a
|
|
// negative errno when it could not be opened or read. A readdir error
|
|
// is told apart from the end of the directory by errno, which readdir
|
|
// leaves unchanged there.
|
|
static
|
|
int
|
|
_dir_is_empty(const fs::path &path_)
|
|
{
|
|
int rv;
|
|
DIR *dir;
|
|
struct dirent *d;
|
|
|
|
dir = ::opendir(path_.c_str());
|
|
if(dir == NULL)
|
|
return -errno;
|
|
|
|
rv = 0;
|
|
for(;;)
|
|
{
|
|
errno = 0;
|
|
d = ::readdir(dir);
|
|
if(d == NULL)
|
|
{
|
|
if(errno != 0)
|
|
rv = -errno;
|
|
break;
|
|
}
|
|
if((strcmp(d->d_name,".") == 0) || (strcmp(d->d_name,"..") == 0))
|
|
continue;
|
|
rv = 1;
|
|
break;
|
|
}
|
|
|
|
::closedir(dir);
|
|
|
|
return rv;
|
|
}
|
|
|
|
// The sources the action policy selected must be the same kind of
|
|
// object on every branch: publication points one replacement symlink
|
|
// at the copy staged from the first source, and a later source of a
|
|
// different type would be staged beside it, where a symlink's relative
|
|
// target would resolve from inside the staging directory and dangle.
|
|
// Refused before anything moves, like a symlink which exists on more
|
|
// than one branch. A branch whose copy vanished since selection holds
|
|
// no source to compare and is left for staging to refuse.
|
|
static
|
|
int
|
|
_rename_exdev_check_source_types(const std::vector<Branch*> &obranches_,
|
|
const fs::path &oldfusepath_,
|
|
const struct stat &source_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
|
|
for(size_t i = 1; i < obranches_.size(); i++)
|
|
{
|
|
rv = fs::lstat(obranches_[i]->path / oldfusepath_,&st);
|
|
if(rv == -ENOENT)
|
|
continue;
|
|
if(rv < 0)
|
|
return rv;
|
|
if((st.st_mode & S_IFMT) != (source_.st_mode & S_IFMT))
|
|
return -EXDEV;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// rename(2) replaces an existing destination. Scan every configured
|
|
// branch once, before anything is moved, and pin which of them hold a
|
|
// destination, rejecting the cases where rename(2) would fail rather
|
|
// than discovering them after the source has been staged. A
|
|
// destination on a configured read-only branch cannot be removed, and
|
|
// on a configured no-create branch the replacement link cannot be
|
|
// created, so in both cases the old entry would shadow the published
|
|
// link there and the fallback refuses. So do aliases: two configured
|
|
// branches with the same underlying root would be published to twice
|
|
// and the second would fail. A destination on any other branch is
|
|
// pinned; publication refuses those it does not itself replace.
|
|
static
|
|
int
|
|
_rename_exdev_scan_destination(const Branches::Ptr &branches_,
|
|
const fs::path &newfusepath_,
|
|
const struct stat &source_,
|
|
std::vector<DestinationMember> &members_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
std::vector<dev_t> rootdevs;
|
|
std::vector<ino_t> rootinos;
|
|
|
|
for(const auto &branch : *branches_)
|
|
{
|
|
// An unstattable root (missing mountpoint, unmounted branch)
|
|
// cannot hold the destination either; the loop below treats it
|
|
// as absent, as the pre-scan code did.
|
|
rv = fs::lstat(branch.path,&st);
|
|
if(rv < 0)
|
|
continue;
|
|
for(size_t i = 0; i < rootdevs.size(); i++)
|
|
{
|
|
if((rootdevs[i] == st.st_dev) && (rootinos[i] == st.st_ino))
|
|
return -EXDEV;
|
|
}
|
|
rootdevs.push_back(st.st_dev);
|
|
rootinos.push_back(st.st_ino);
|
|
}
|
|
|
|
for(const auto &branch : *branches_)
|
|
{
|
|
rv = fs::lstat(branch.path / newfusepath_,&st);
|
|
if(rv == -ENOENT)
|
|
continue;
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
if(branch.ro_or_nc())
|
|
return -EXDEV;
|
|
if(S_ISDIR(st.st_mode) && !S_ISDIR(source_.st_mode))
|
|
return -EISDIR;
|
|
if(!S_ISDIR(st.st_mode) && S_ISDIR(source_.st_mode))
|
|
return -ENOTDIR;
|
|
if(S_ISDIR(st.st_mode))
|
|
{
|
|
rv = ::_dir_is_empty(branch.path / newfusepath_);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(rv != 0)
|
|
return -ENOTEMPTY;
|
|
}
|
|
|
|
DestinationMember member;
|
|
member.branch = branch.path;
|
|
member.dev = st.st_dev;
|
|
member.ino = st.st_ino;
|
|
member.directory = S_ISDIR(st.st_mode);
|
|
members_.emplace_back(member);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static
|
|
fs::path
|
|
_rename_exdev_temp_path(const fs::path &dir_)
|
|
{
|
|
char name[64];
|
|
snprintf(name,sizeof(name),"%s_%016llx",
|
|
rename_exdev::STAGING_DIR.c_str(),
|
|
(unsigned long long)RND::rand64());
|
|
|
|
return dir_ / name;
|
|
}
|
|
|
|
// Create the replacement symlink for `newfusepath_` under a temporary
|
|
// name next to it, on whichever branches the strict preparation API
|
|
// creates it, recording each creation together with the identity the
|
|
// fresh link had when it was made. The temporary name is taken, never
|
|
// replaced: a collision, or a preparation which failed partway, is
|
|
// undone through the records - only where the link is still the object
|
|
// this request created - and retried under a fresh name.
|
|
static
|
|
int
|
|
_rename_exdev_prepare_symlink(const Branches::Ptr &branches_,
|
|
const ugid_t owner_,
|
|
const fs::path &target_,
|
|
const fs::path &newfusepath_,
|
|
fs::path &tmpfusepath_,
|
|
std::vector<FUSE::PreparedSymlink> &prepared_)
|
|
{
|
|
int rv;
|
|
|
|
for(int attempt = 0; attempt < 8; attempt++)
|
|
{
|
|
tmpfusepath_ = ::_rename_exdev_temp_path(newfusepath_.parent_path());
|
|
|
|
rv = FUSE::symlink_prepare(branches_,owner_,target_.c_str(),tmpfusepath_,prepared_);
|
|
if(rv >= 0)
|
|
return 0;
|
|
|
|
// The attempt may have created the link on some branches before
|
|
// failing, and the name may simply have been taken. Either way
|
|
// those links are undone before the next attempt, which uses a
|
|
// fresh name.
|
|
FUSE::symlink_unprepare(prepared_);
|
|
if(rv != -EEXIST)
|
|
break;
|
|
}
|
|
|
|
tmpfusepath_.clear();
|
|
return rv;
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// A destination replaced by publication. What was there is kept at
|
|
// `backup` until every branch has been published. The published
|
|
// link's identity (`dev`/`ino`) is the recorded one, so rollback
|
|
// removes it only while the name still refers to that object.
|
|
struct Replaced
|
|
{
|
|
fs::path newpath;
|
|
fs::path backup;
|
|
dev_t dev = 0;
|
|
ino_t ino = 0;
|
|
bool directory = false;
|
|
bool aside = false;
|
|
};
|
|
}
|
|
|
|
// Keep the object at `r_.newpath` under a temporary name. A hard link
|
|
// leaves a non-directory in place so its replacement stays atomic. A
|
|
// directory, or a file which can't be linked, is moved aside. An empty
|
|
// directory must still be empty, as rename(2) would require. Temporary
|
|
// names are taken with no-replace operations and a fresh name on
|
|
// collision, so a temporary name never overwrites an unrelated entry.
|
|
// On failure the backup names what is left; the caller undoes it.
|
|
static
|
|
int
|
|
_rename_exdev_keep_destination(Replaced &r_)
|
|
{
|
|
int rv = -EXDEV;
|
|
|
|
for(int attempt = 0; attempt < 8; attempt++)
|
|
{
|
|
r_.backup = ::_rename_exdev_temp_path(r_.newpath.parent_path());
|
|
|
|
if(!r_.directory)
|
|
{
|
|
rv = ::to_neg_errno(::linkat(AT_FDCWD,r_.newpath.c_str(),
|
|
AT_FDCWD,r_.backup.c_str(),0));
|
|
if(rv == 0)
|
|
return 0;
|
|
if(rv == -EEXIST)
|
|
continue;
|
|
}
|
|
|
|
rv = fs::renameat_noreplace(AT_FDCWD,r_.newpath.string(),
|
|
AT_FDCWD,r_.backup.string());
|
|
if(rv == 0)
|
|
{
|
|
r_.aside = true;
|
|
// Only a directory must still be empty; a file moved aside
|
|
// after a failed link is fine whatever opendir would say.
|
|
if(!r_.directory)
|
|
return 0;
|
|
rv = ::_dir_is_empty(r_.backup);
|
|
if(rv < 0)
|
|
return rv;
|
|
return ((rv != 0) ? -ENOTEMPTY : 0);
|
|
}
|
|
if(rv != -EEXIST)
|
|
break;
|
|
}
|
|
|
|
r_.backup.clear();
|
|
return rv;
|
|
}
|
|
|
|
// Undo a keep whose publication did not happen: the object is either
|
|
// back at its original name already, in which case only the spare hard
|
|
// link is removed, or still at the temporary name, in which case it is
|
|
// put back. Restores never replace a name created in the meantime; a
|
|
// name which can't be restored is logged and its data remains at the
|
|
// temporary name.
|
|
static
|
|
void
|
|
_rename_exdev_unkeep(Replaced &r_)
|
|
{
|
|
int rv;
|
|
|
|
if(r_.backup.empty())
|
|
return;
|
|
|
|
if(r_.aside)
|
|
{
|
|
rv = fs::renameat_noreplace(AT_FDCWD,r_.backup.string(),
|
|
AT_FDCWD,r_.newpath.string());
|
|
if(rv < 0)
|
|
SysLog::error("rename-exdev rollback failed (errno={}); previous {} remains at {}",
|
|
-rv,
|
|
r_.newpath.string(),
|
|
r_.backup.string());
|
|
}
|
|
else
|
|
{
|
|
rv = fs::unlink(r_.backup);
|
|
if(rv < 0)
|
|
SysLog::warning("rename-exdev: could not remove temporary link {} (errno={})",
|
|
r_.backup.string(),
|
|
-rv);
|
|
}
|
|
|
|
r_.backup.clear();
|
|
}
|
|
|
|
// Undo publication on the branches already replaced: put back what was
|
|
// at each destination, or remove the new symlink if nothing was. The
|
|
// name is unlinked only while it still refers to the link this request
|
|
// published; anything else there appeared afterwards and is not ours
|
|
// to remove. The restore itself never replaces a name created in the
|
|
// meantime.
|
|
static
|
|
void
|
|
_rename_exdev_unpublish(const std::vector<Replaced> &replaced_)
|
|
{
|
|
for(auto r = replaced_.rbegin(); r != replaced_.rend(); ++r)
|
|
{
|
|
int rv;
|
|
|
|
rv = fs::is_same_file(r->newpath,r->dev,r->ino);
|
|
if(rv == 0)
|
|
{
|
|
SysLog::warning("rename-exdev: {} was replaced during rollback; leaving it alone",
|
|
r->newpath.string());
|
|
if(!r->backup.empty())
|
|
SysLog::error("rename-exdev rollback failed; previous {} remains at {}",
|
|
r->newpath.string(),
|
|
r->backup.string());
|
|
continue;
|
|
}
|
|
if(rv == 1)
|
|
{
|
|
rv = fs::unlink(r->newpath);
|
|
if((rv < 0) && (rv != -ENOENT))
|
|
{
|
|
SysLog::error("rename-exdev rollback failed (errno={}); could not remove {}",
|
|
-rv,
|
|
r->newpath.string());
|
|
if(!r->backup.empty())
|
|
SysLog::error("rename-exdev rollback failed; previous {} remains at {}",
|
|
r->newpath.string(),
|
|
r->backup.string());
|
|
continue;
|
|
}
|
|
}
|
|
else if(rv != -ENOENT)
|
|
{
|
|
SysLog::error("rename-exdev rollback failed (errno={}); could not inspect {}",
|
|
-rv,
|
|
r->newpath.string());
|
|
continue;
|
|
}
|
|
|
|
if(r->backup.empty())
|
|
continue;
|
|
|
|
rv = fs::renameat_noreplace(AT_FDCWD,r->backup.string(),
|
|
AT_FDCWD,r->newpath.string());
|
|
if(rv == -EOPNOTSUPP)
|
|
{
|
|
rv = ::to_neg_errno(::linkat(AT_FDCWD,r->backup.c_str(),
|
|
AT_FDCWD,r->newpath.c_str(),0));
|
|
if(rv == 0)
|
|
rv = fs::unlink(r->backup);
|
|
}
|
|
if(rv < 0)
|
|
SysLog::error("rename-exdev rollback failed (errno={}); previous {} remains at {}",
|
|
-rv,
|
|
r->newpath.string(),
|
|
r->backup.string());
|
|
}
|
|
}
|
|
|
|
// Rename the prepared symlink over `newfusepath_` on every branch
|
|
// where it was created, as rename(2) would, using only the recorded
|
|
// creations: which branches hold the link is never rediscovered by
|
|
// rescanning the temporary name. Each prepared link is verified to
|
|
// still be the object this request created immediately before it is
|
|
// renamed. Destination copies are only touched on branches of the
|
|
// pinned preflight scan and only while they still match their pinned
|
|
// identity: a copy which appeared after the scan, and one which cannot
|
|
// be verified, refuse the rename instead of being moved or deleted. A
|
|
// destination the scan found absent is never replaced either: if
|
|
// something is there now, the rename is refused. If any branch fails,
|
|
// the destinations already replaced are restored so the caller can
|
|
// move the source back without leaving links to it.
|
|
static
|
|
int
|
|
_rename_exdev_publish_prepared_symlink(const std::vector<FUSE::PreparedSymlink> &prepared_,
|
|
const fs::path &newfusepath_,
|
|
const struct stat *times_,
|
|
const std::vector<DestinationMember> &members_)
|
|
{
|
|
int rv;
|
|
struct stat st;
|
|
std::vector<Replaced> replaced;
|
|
Replaced r;
|
|
|
|
if(prepared_.empty())
|
|
return -ENOENT;
|
|
|
|
for(size_t i = 0; i < prepared_.size(); i++)
|
|
{
|
|
const FUSE::PreparedSymlink &record = prepared_[i];
|
|
const fs::path newpath = record.branch / newfusepath_;
|
|
bool dest_exists;
|
|
|
|
r = Replaced();
|
|
r.newpath = newpath;
|
|
r.dev = record.dev;
|
|
r.ino = record.ino;
|
|
|
|
// The link must still be the object this request created.
|
|
rv = fs::is_same_file(record.path,record.dev,record.ino);
|
|
if(rv < 0)
|
|
break;
|
|
if(rv == 0)
|
|
{
|
|
rv = -EXDEV;
|
|
break;
|
|
}
|
|
|
|
// The times are promised to survive the rename; failing to set
|
|
// them fails the rename. Destinations already replaced by
|
|
// earlier records are put back by the failure path below.
|
|
if(times_ != NULL)
|
|
{
|
|
rv = fs::lutimens(record.path.string(),*times_);
|
|
if(rv < 0)
|
|
break;
|
|
}
|
|
|
|
rv = fs::lstat(newpath,&st);
|
|
if((rv < 0) && (rv != -ENOENT))
|
|
break;
|
|
dest_exists = (rv == 0);
|
|
|
|
if(dest_exists)
|
|
{
|
|
const DestinationMember *member = ::_rename_exdev_find_member(members_,record.branch);
|
|
|
|
if((member == NULL) ||
|
|
(member->dev != st.st_dev) ||
|
|
(member->ino != st.st_ino))
|
|
{
|
|
rv = -EXDEV;
|
|
break;
|
|
}
|
|
|
|
r.directory = member->directory;
|
|
// A directory can't be renamed over. If the last
|
|
// non-directory's rename fails nothing has changed, so it
|
|
// needs no backup copy either.
|
|
if(r.directory || ((i + 1) < prepared_.size()))
|
|
{
|
|
rv = ::_rename_exdev_keep_destination(r);
|
|
if(rv < 0)
|
|
break;
|
|
}
|
|
}
|
|
|
|
// An unpinned destination would have been replaced by a plain
|
|
// rename; refusing it is conservative because it appeared after
|
|
// the scan and was never verified.
|
|
if(dest_exists)
|
|
rv = fs::rename(record.path,newpath);
|
|
else
|
|
rv = fs::renameat_noreplace(AT_FDCWD,record.path.string(),
|
|
AT_FDCWD,newpath.string());
|
|
if(rv == -EOPNOTSUPP)
|
|
{
|
|
// Without renameat2 the name is taken with a hard link,
|
|
// which links the symlink itself and cannot replace an
|
|
// existing destination, and the temporary name is then
|
|
// removed. The inode is unchanged, so the recorded identity
|
|
// still describes the published link.
|
|
rv = ::to_neg_errno(::linkat(AT_FDCWD,record.path.c_str(),
|
|
AT_FDCWD,newpath.c_str(),0));
|
|
if(rv == 0)
|
|
{
|
|
rv = fs::unlink(record.path);
|
|
if(rv < 0)
|
|
{
|
|
fs::unlink(newpath);
|
|
SysLog::error("rename-exdev: could not remove temporary link {} (errno={})",
|
|
record.path.string(),
|
|
-rv);
|
|
}
|
|
}
|
|
}
|
|
if(rv < 0)
|
|
break;
|
|
|
|
replaced.emplace_back(std::move(r));
|
|
}
|
|
|
|
// Every failure after the first record was published must restore
|
|
// the destinations already replaced; otherwise their originals
|
|
// would be stranded at their backup names with nothing logging it.
|
|
if(rv < 0)
|
|
{
|
|
::_rename_exdev_unkeep(r);
|
|
::_rename_exdev_unpublish(replaced);
|
|
return rv;
|
|
}
|
|
|
|
// The rename has succeeded everywhere. The kept copies of the
|
|
// replaced destinations are now removed, as rename(2) would have. A
|
|
// failure leaves the data at the temporary name and is only
|
|
// reported.
|
|
for(const auto &r : replaced)
|
|
{
|
|
if(r.backup.empty())
|
|
continue;
|
|
rv = fs::remove(r.backup);
|
|
if(rv < 0)
|
|
SysLog::warning("rename-exdev: could not remove replaced destination {} (errno={})",
|
|
r.backup.string(),
|
|
-rv);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Put source links that were moved aside back under their original
|
|
// names. A source recreated in the meantime is never replaced.
|
|
static
|
|
void
|
|
_rename_exdev_restore_sources(const std::vector<std::pair<fs::path,fs::path>> &aside_)
|
|
{
|
|
for(const auto &[src,hidden] : aside_)
|
|
{
|
|
int rv = fs::renameat_noreplace(AT_FDCWD,hidden.string(),
|
|
AT_FDCWD,src.string());
|
|
if(rv == -EOPNOTSUPP)
|
|
{
|
|
rv = ::to_neg_errno(::linkat(AT_FDCWD,hidden.c_str(),
|
|
AT_FDCWD,src.c_str(),0));
|
|
if(rv == 0)
|
|
rv = fs::unlink(hidden);
|
|
}
|
|
if(rv < 0)
|
|
SysLog::error("rename-exdev rollback failed (errno={}); source symlink remains at {}",
|
|
-rv,
|
|
hidden.string());
|
|
}
|
|
}
|
|
|
|
// A symlink being renamed is not staged: moving it one directory deeper
|
|
// would break a relative target, including those of the replacement
|
|
// symlinks made by rel-symlink. rename(2) leaves a symlink's content
|
|
// alone, so recreate it at newpath with the same content, owner and
|
|
// times and remove the source. Recreation collapses every branch's
|
|
// copy of the symlink into the one target read from the first source,
|
|
// so a symlink which exists on more than one branch is refused before
|
|
// anything is moved: mirrors whose targets differ would silently lose
|
|
// their contents. Single-source renames keep working.
|
|
static
|
|
int
|
|
_rename_exdev_move_symlink(const Branches::Ptr &branches_,
|
|
const std::vector<Branch*> &obranches_,
|
|
const struct stat &source_,
|
|
const fs::path &oldfusepath_,
|
|
const fs::path &newfusepath_,
|
|
const std::vector<DestinationMember> &destmembers_)
|
|
{
|
|
int rv;
|
|
std::string target;
|
|
fs::path tmpfusepath;
|
|
std::vector<FUSE::PreparedSymlink> prepared;
|
|
std::vector<std::pair<fs::path,fs::path>> aside;
|
|
|
|
if(obranches_.size() > 1)
|
|
return -EXDEV;
|
|
|
|
target.resize(PATH_MAX);
|
|
rv = fs::readlink((obranches_[0]->path / oldfusepath_).string(),
|
|
target.data(),
|
|
target.size());
|
|
if(rv < 0)
|
|
return rv;
|
|
if((size_t)rv >= target.size())
|
|
return -ENAMETOOLONG;
|
|
target.resize(rv);
|
|
|
|
rv = ::_rename_exdev_prepare_symlink(branches_,
|
|
{source_.st_uid,source_.st_gid},
|
|
target,
|
|
newfusepath_,
|
|
tmpfusepath,
|
|
prepared);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
// A pinned destination on a branch which will not publish the link
|
|
// would shadow it there. Refused before any source link moves aside,
|
|
// which is the first thing this path changes.
|
|
rv = ::_rename_exdev_check_prepared_covers_members(destmembers_,prepared);
|
|
if(rv < 0)
|
|
{
|
|
FUSE::symlink_unprepare(prepared);
|
|
return rv;
|
|
}
|
|
|
|
// Move every source aside before publishing. A source which can't be
|
|
// removed then fails the rename while nothing else has changed. Each
|
|
// aside is taken under a fresh no-replace name, so a collision with
|
|
// an unrelated entry takes another name instead of overwriting it.
|
|
// Without renameat2 the name is taken with a hard link, which links
|
|
// the symlink itself, and the original name is then removed; a
|
|
// failure there removes the link and leaves the source alone.
|
|
for(auto &branch : obranches_)
|
|
{
|
|
const fs::path src = branch->path / oldfusepath_;
|
|
fs::path hidden;
|
|
|
|
rv = -EEXIST;
|
|
for(int attempt = 0; attempt < 8; attempt++)
|
|
{
|
|
hidden = ::_rename_exdev_temp_path(src.parent_path());
|
|
rv = fs::renameat_noreplace(AT_FDCWD,src.string(),
|
|
AT_FDCWD,hidden.string());
|
|
if(rv == -EOPNOTSUPP)
|
|
{
|
|
rv = ::to_neg_errno(::linkat(AT_FDCWD,src.c_str(),
|
|
AT_FDCWD,hidden.c_str(),0));
|
|
if(rv == -EEXIST)
|
|
continue;
|
|
if(rv < 0)
|
|
break;
|
|
rv = fs::unlink(src);
|
|
if(rv < 0)
|
|
{
|
|
fs::unlink(hidden);
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
if(rv != -EEXIST)
|
|
break;
|
|
}
|
|
if(rv < 0)
|
|
break;
|
|
aside.emplace_back(src,hidden);
|
|
}
|
|
|
|
if(rv >= 0)
|
|
rv = ::_rename_exdev_publish_prepared_symlink(prepared,
|
|
newfusepath_,
|
|
&source_,
|
|
destmembers_);
|
|
if(rv < 0)
|
|
{
|
|
FUSE::symlink_unprepare(prepared);
|
|
::_rename_exdev_restore_sources(aside);
|
|
return rv;
|
|
}
|
|
|
|
// The published replacement stays: it is the renamed object. A
|
|
// source link that is still linked under its hidden name means the
|
|
// rename did not remove the original, so the call fails. Returning 0
|
|
// here left both names in place.
|
|
int error = 0;
|
|
for(const auto &[src,hidden] : aside)
|
|
{
|
|
rv = fs::unlink(hidden);
|
|
if(rv < 0)
|
|
{
|
|
SysLog::error("rename-exdev: could not remove moved source {} (errno={})",
|
|
hidden.string(),
|
|
-rv);
|
|
if(error == 0)
|
|
error = rv;
|
|
}
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
// Fallback transactions verify a staged target against every
|
|
// configured branch and then publish against the same set. Two of them
|
|
// running at once could interleave those steps, so they are
|
|
// serialized. Renames with the feature disabled never enter the
|
|
// fallback and are not affected.
|
|
static std::mutex _rename_exdev_fallback_mutex;
|
|
|
|
static
|
|
int
|
|
_rename_exdev_symlink(const RenameEXDEV::ENUM mode_,
|
|
const Policy::Action &actionPolicy_,
|
|
const Branches::Ptr branches_,
|
|
const fs::path &mount_,
|
|
const fs::path &oldfusepath_,
|
|
const fs::path &newfusepath_,
|
|
fs::path &relocated_,
|
|
char **relocated_out_)
|
|
{
|
|
int rv;
|
|
struct stat source;
|
|
fs::path target;
|
|
fs::path stagedpath;
|
|
std::vector<FUSE::PreparedSymlink> prepared;
|
|
std::vector<Staged> staged;
|
|
std::vector<Branch*> obranches;
|
|
std::vector<DestinationMember> destmembers;
|
|
const std::lock_guard<std::mutex> guard(_rename_exdev_fallback_mutex);
|
|
|
|
rv = actionPolicy_(branches_,oldfusepath_,obranches);
|
|
if(rv < 0)
|
|
return rv;
|
|
if(obranches.empty())
|
|
return -ENOENT;
|
|
|
|
rv = fs::lstat(obranches[0]->path / oldfusepath_,&source);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = ::_rename_exdev_scan_destination(branches_,newfusepath_,source,destmembers);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
rv = ::_rename_exdev_check_source_types(obranches,oldfusepath_,source);
|
|
if(rv < 0)
|
|
return rv;
|
|
|
|
if(S_ISLNK(source.st_mode))
|
|
return ::_rename_exdev_move_symlink(branches_,
|
|
obranches,
|
|
source,
|
|
oldfusepath_,
|
|
newfusepath_,
|
|
destmembers);
|
|
|
|
auto prepare = [&](const fs::path &candidate_)
|
|
{
|
|
fs::path tmpfusepath;
|
|
|
|
// A retry stages under a different name, so the links of the
|
|
// previous attempt, still pointing at it, go first.
|
|
FUSE::symlink_unprepare(prepared);
|
|
|
|
fs::path candidate = fs::path(rename_exdev::STAGING_DIR) / candidate_;
|
|
if(mode_ == RenameEXDEV::ENUM::ABS_SYMLINK)
|
|
target = mount_ / candidate;
|
|
else
|
|
target = candidate.lexically_relative(newfusepath_.parent_path());
|
|
|
|
return ::_rename_exdev_prepare_symlink(branches_,
|
|
{source.st_uid,source.st_gid},
|
|
target,
|
|
newfusepath_,
|
|
tmpfusepath,
|
|
prepared);
|
|
};
|
|
|
|
rv = ::_rename_exdev_rename_target(branches_,
|
|
obranches,
|
|
oldfusepath_,
|
|
destmembers,
|
|
prepared,
|
|
staged,
|
|
stagedpath,
|
|
prepare);
|
|
if(rv < 0)
|
|
{
|
|
FUSE::symlink_unprepare(prepared);
|
|
return rv;
|
|
}
|
|
|
|
relocated_ = fs::path(rename_exdev::STAGING_DIR) / stagedpath;
|
|
// libfuse has to re-parent the kernel node onto the staged path. Do
|
|
// that allocation before publishing, while unstage can still put the
|
|
// source back. A failure after publication cannot be undone from
|
|
// here: the node update has not run, and a second stage would nest.
|
|
if(relocated_out_ != NULL)
|
|
{
|
|
*relocated_out_ = ::strdup(relocated_.c_str());
|
|
if(*relocated_out_ == NULL)
|
|
{
|
|
FUSE::symlink_unprepare(prepared);
|
|
relocated_.clear();
|
|
::_rename_exdev_unstage(staged,oldfusepath_);
|
|
return -ENOMEM;
|
|
}
|
|
}
|
|
|
|
rv = ::_rename_exdev_publish_prepared_symlink(prepared,
|
|
newfusepath_,
|
|
NULL,
|
|
destmembers);
|
|
|
|
if(rv < 0)
|
|
{
|
|
if((relocated_out_ != NULL) && (*relocated_out_ != NULL))
|
|
{
|
|
::free(*relocated_out_);
|
|
*relocated_out_ = NULL;
|
|
}
|
|
FUSE::symlink_unprepare(prepared);
|
|
relocated_.clear();
|
|
::_rename_exdev_unstage(staged,oldfusepath_);
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
static
|
|
int
|
|
_rename_exdev(const fs::path &oldfusepath_,
|
|
const fs::path &newfusepath_,
|
|
fs::path &relocated_,
|
|
char **relocated_out_)
|
|
{
|
|
const RenameEXDEV::ENUM mode = cfg.rename_exdev;
|
|
|
|
switch(mode)
|
|
{
|
|
case RenameEXDEV::ENUM::PASSTHROUGH:
|
|
return -EXDEV;
|
|
case RenameEXDEV::ENUM::REL_SYMLINK:
|
|
case RenameEXDEV::ENUM::ABS_SYMLINK:
|
|
return ::_rename_exdev_symlink(mode,
|
|
cfg.func.rename.policy,
|
|
cfg.branches,
|
|
cfg.mountpoint,
|
|
oldfusepath_,
|
|
newfusepath_,
|
|
relocated_,
|
|
relocated_out_);
|
|
}
|
|
|
|
return -EXDEV;
|
|
}
|
|
|
|
static
|
|
int
|
|
_rename(const fs::path &oldpath_,
|
|
const fs::path &newpath_)
|
|
{
|
|
if(cfg.func.create.policy.path_preserving() && !cfg.ignorepponrename)
|
|
return ::_rename_preserve_path(cfg.func.rename.policy,
|
|
cfg.branches,
|
|
oldpath_,
|
|
newpath_);
|
|
|
|
return ::_rename_create_path(cfg.func.getattr.policy,
|
|
cfg.func.rename.policy,
|
|
cfg.branches,
|
|
oldpath_,
|
|
newpath_);
|
|
}
|
|
|
|
int
|
|
FUSE::rename(const fuse_req_ctx_t *ctx_,
|
|
const char *oldfusepath_,
|
|
const char *newfusepath_,
|
|
char **relocated_)
|
|
{
|
|
int rv;
|
|
fs::path relocated;
|
|
const fs::path oldfusepath{oldfusepath_};
|
|
const fs::path newfusepath{newfusepath_};
|
|
|
|
rv = ::_rename(oldfusepath,newfusepath);
|
|
if(rv != -EXDEV)
|
|
return rv;
|
|
|
|
// A staged file or directory has its relocation path allocated
|
|
// before publication, so a failed allocation can still unstage.
|
|
// A symlink source is recreated at newpath and leaves the out
|
|
// pointer NULL, and libfuse renames that node normally.
|
|
return ::_rename_exdev(oldfusepath,newfusepath,relocated,relocated_);
|
|
}
|