mergerfs/src/fs_copyfile.cpp
Antonio S. Musumeci cd0679b01f fix: leave the source branch out before moveonenospc picks a branch
- The source branch was dropped from the create policy's result. ff,
  mfs, lfs, lus and pfrd return a single branch, so when that branch
  was the source nothing was left and the write kept ENOSPC even with
  room elsewhere. That happens when the source has free space by
  statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
  inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
  inode, as before) and run the policy on that, so it picks another
  branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
  past a source listed first, by its own path or an alias, and that a
  list made only of the source is refused.

Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
2026-11-22 01:24:45 -05:00

227 lines
5.8 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 "fs_copyfile.hpp"
#include "fs_attr.hpp"
#include "fs_close.hpp"
#include "fs_copydata.hpp"
#include "fs_fchmod.hpp"
#include "fs_fchown.hpp"
#include "fs_fcntl.hpp"
#include "fs_file_unchanged.hpp"
#include "fs_fstat.hpp"
#include "fs_futimens.hpp"
#include "fs_mktemp.hpp"
#include "fs_open.hpp"
#include "fs_path.hpp"
#include "fs_rename.hpp"
#include "fs_unlink.hpp"
#include "fs_xattr.hpp"
#include "to_neg_errno.hpp"
#include "scope_guard/scope_guard.hpp"
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#ifdef __linux__
#include <linux/fs.h>
#endif
#if defined(__linux__) || defined(__FreeBSD__)
#include <sys/syscall.h>
#endif
static
bool
_ignorable_error(const s64 err_)
{
switch(err_)
{
case ENOTTY:
case ENOTSUP:
#if ENOTSUP != EOPNOTSUPP
case EOPNOTSUPP:
#endif
return true;
}
return false;
}
static
int
_publish_noreplace(const char *tmppath_,
const char *dstpath_)
{
int rv;
#if defined(SYS_renameat2) && defined(RENAME_NOREPLACE)
rv = ::to_neg_errno(::syscall(SYS_renameat2,AT_FDCWD,tmppath_,
AT_FDCWD,dstpath_,RENAME_NOREPLACE));
#elif defined(__APPLE__) && defined(RENAME_EXCL)
rv = ::to_neg_errno(::renamex_np(tmppath_,dstpath_,RENAME_EXCL));
#else
rv = -ENOSYS;
#endif
if((rv != -ENOSYS) && (rv != -EINVAL) &&
(rv != -ENOTSUP) && (rv != -EOPNOTSUPP))
return rv;
// Some filesystems reject the rename flag. The staging file is a
// regular file in the same directory, so link can publish it atomically
// without replacement. Never fall back to a replacing rename.
rv = ::to_neg_errno(::link(tmppath_,dstpath_));
if(rv < 0)
return rv;
// The destination is now published. A cleanup error cannot undo that.
fs::unlink(tmppath_);
return 0;
}
s64
fs::copyfile(const int src_fd_,
const struct stat &src_st_,
const int dst_fd_)
{
s64 rv;
rv = fs::copydata(src_fd_,dst_fd_,src_st_.st_size);
if(rv < 0)
return rv;
rv = fs::xattr::copy(src_fd_,dst_fd_);
if((rv < 0) && !::_ignorable_error(-rv))
return rv;
rv = fs::attr::copy(src_fd_,dst_fd_,FS_ATTR_CLEAR_IMMUTABLE);
if((rv < 0) && !::_ignorable_error(-rv))
return rv;
rv = fs::fchown_check_on_error(dst_fd_,src_st_);
if(rv < 0)
return rv;
rv = fs::fchmod_check_on_error(dst_fd_,src_st_);
if(rv < 0)
return rv;
rv = fs::futimens(dst_fd_,src_st_);
if(rv < 0)
return rv;
return 0;
}
// Limitations:
// * Doesn't handle immutable files well. Will ignore the flag on attr
// copy.
// * Does not handle non-regular files.
s64
fs::copyfile(const int src_fd_,
const fs::path &dst_filepath_,
const fs::CopyFileFlags &flags_)
{
s64 rv;
int dst_fd;
struct stat src_st = {};
std::string dst_tmppath;
struct sigaction old_act;
struct sigaction new_act;
// Done in case fcntl(F_SETLEASE) works.
new_act.sa_handler = SIG_IGN;
new_act.sa_flags = 0;
sigemptyset(&new_act.sa_mask);
sigaction(SIGIO,&new_act,&old_act);
DEFER { sigaction(SIGIO,&old_act,NULL); };
while(true)
{
// For comparison after the copy to see if the file was
// modified. This could be made more thorough by adding some
// hashing but probably overkill. Also used to provide size and
// other details for data copying.
rv = fs::fstat(src_fd_,&src_st);
if(rv < 0)
return rv;
if(!S_ISREG(src_st.st_mode))
return -EINVAL;
std::tie(dst_fd,dst_tmppath) = fs::mktemp(dst_filepath_,O_RDWR);
if(dst_fd < 0)
return dst_fd;
DEFER { fs::close(dst_fd); };
// If it fails or is unsupported... so be it. This will help
// limit the possibility of the file being modified while the
// copy happens. Opening read-only is fine but open for write or
// truncate will block for others till this finishes or the
// kernel wide timeout (/proc/sys/fs/lease-break-time).
fs::fcntl_setlease_rdlck(src_fd_);
DEFER { fs::fcntl_setlease_unlck(src_fd_); };
rv = fs::copyfile(src_fd_,src_st,dst_fd);
if(rv < 0)
{
if(flags_.cleanup_failure)
fs::unlink(dst_tmppath);
return rv;
}
rv = fs::file_changed(src_fd_,src_st);
if(rv != FS_FILE_UNCHANGED)
{
fs::unlink(dst_tmppath);
continue;
}
if(flags_.no_replace)
rv = ::_publish_noreplace(dst_tmppath.c_str(),dst_filepath_.c_str());
else
rv = fs::rename(dst_tmppath.c_str(),dst_filepath_.c_str());
if((rv < 0) && (flags_.cleanup_failure))
fs::unlink(dst_tmppath);
break;
}
return rv;
}
// For FreeBSD
#ifndef O_NOATIME
#define O_NOATIME 0
#endif
s64
fs::copyfile(const fs::path &src_filepath_,
const fs::path &dst_filepath_,
const fs::CopyFileFlags &flags_)
{
int src_fd;
src_fd = fs::open(src_filepath_,O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_NOATIME);
if(src_fd < 0)
return src_fd;
DEFER { fs::close(src_fd); };
return fs::copyfile(src_fd,dst_filepath_,flags_);
}