HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/shared/copy.c

67,947 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 51dbf080ad63edd1efcb9689115bcb62f2e24468

Lines 1–1000 of 1,834

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <linux/btrfs.h>
#include <linux/fsverity.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/sendfile.h>
#include <sys/sysmacros.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "btrfs-util.h"
#include "chattr-util.h"
#include "copy.h"
#include "dirent-util.h"
#include "errno-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "hashmap.h"
#include "log.h"
#include "mountpoint-util.h"
#include "nulstr-util.h"
#include "path-util.h"
#include "recurse-dir.h"
#include "rm-rf.h"
#include "selinux-util.h"
#include "signal-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "sync-util.h"
#include "time-util.h"
#include "tmpfile-util.h"
#include "umask-util.h"
#include "user-util.h"
#include "xattr-util.h"
 
/* If we copy via a userspace buffer, size it to 64K */
#define COPY_BUFFER_SIZE (64U*U64_KB)
 
/* If a byte progress function is specified during copying, never try to copy more than 1M, so that we can
 * reasonably call the progress function still */
#define PROGRESS_STEP_SIZE (1U*U64_MB)
 
/* A safety net for descending recursively into file system trees to copy. On Linux PATH_MAX is 4096, which means the
 * deepest valid path one can build is around 2048, which we hence use as a safety net here, to not spin endlessly in
 * case of bind mount cycles and suchlike. */
#define COPY_DEPTH_MAX 2048U
 
static ssize_t try_copy_file_range(
                int fd_in, loff_t *off_in,
                int fd_out, loff_t *off_out,
                size_t len,
                unsigned flags) {
 
        static int have = -1;
        ssize_t r;
 
        if (have == 0)
                return -ENOSYS;
 
        r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
        if (have < 0)
                have = r >= 0 || errno != ENOSYS;
        if (r < 0)
                return -errno;
 
        return r;
}
 
static int look_for_signals(CopyFlags copy_flags) {
        int r;
 
        if ((copy_flags & (COPY_SIGINT|COPY_SIGTERM)) == 0)
                return 0;
 
        r = pop_pending_signal(copy_flags & COPY_SIGINT ? SIGINT : 0,
                               copy_flags & COPY_SIGTERM ? SIGTERM : 0);
        if (r < 0)
                return r;
        if (r != 0)
                return log_debug_errno(SYNTHETIC_ERRNO(EINTR),
                                       "Got %s, cancelling copy operation.", signal_to_string(r));
 
        return 0;
}
 
static int create_hole(int fd, off_t size) {
        off_t offset;
        off_t end;
 
        offset = lseek(fd, 0, SEEK_CUR);
        if (offset < 0)
                return -errno;
 
        end = lseek(fd, 0, SEEK_END);
        if (end < 0)
                return -errno;
 
        /* If we're not at the end of the target file, try to punch a hole in the existing space using fallocate(). */
 
        if (offset < end &&
            fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, MIN(size, end - offset)) < 0 &&
            !ERRNO_IS_NOT_SUPPORTED(errno))
                return -errno;
 
        if (end - offset >= size) {
                /* If we've created the full hole, set the file pointer to the end of the hole we created and exit. */
                if (lseek(fd, offset + size, SEEK_SET) < 0)
                        return -errno;
 
                return 0;
        }
 
        /* If we haven't created the full hole, use ftruncate() to grow the file (and the hole) to the
         * required size and move the file pointer to the end of the file. */
 
        size -= end - offset;
 
        if (ftruncate(fd, end + size) < 0)
                return -errno;
 
        if (lseek(fd, 0, SEEK_END) < 0)
                return -errno;
 
        return 0;
}
 
static int try_reflink_copy_bytes(
                int fdf,
                int fdt,
                uint64_t max_bytes,
                CopyFlags copy_flags) {
 
        int r;
 
        assert(fdf >= 0);
        assert(fdt >= 0);
 
        if (max_bytes == 0)
                return -EOPNOTSUPP;
 
        off_t foffset = FLAGS_SET(copy_flags, COPY_SEEK0_SOURCE) ? 0 : lseek(fdf, 0, SEEK_CUR);
        if (foffset < 0)
                return -EOPNOTSUPP;
 
        off_t toffset = FLAGS_SET(copy_flags, COPY_SEEK0_TARGET) ? 0 : lseek(fdt, 0, SEEK_CUR);
        if (toffset < 0)
                return -EOPNOTSUPP;
 
        r = reflink_range(fdf, foffset, fdt, toffset, max_bytes);
        if (r < 0)
                return -EOPNOTSUPP;
 
        if (max_bytes == UINT64_MAX) {
                off_t end;
 
                end = lseek(fdf, 0, SEEK_END);
                if (end < 0)
                        return -errno;
                if (end < foffset)
                        return -ESPIPE;
 
                if (lseek(fdt, toffset + (end - foffset), SEEK_SET) < 0)
                        return -errno;
        } else {
                if (lseek(fdf, foffset + max_bytes, SEEK_SET) < 0)
                        return -errno;
                if (lseek(fdt, toffset + max_bytes, SEEK_SET) < 0)
                        return -errno;
        }
 
        if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) {
                r = fd_verify_linked(fdf);
                if (r < 0)
                        return r;
        }
 
        return max_bytes == UINT64_MAX ? 0 /* we copied until EOF */
                                       : 1 /* we copied the requested range */;
}
 
int copy_bytes_full(
                int fdf, int fdt,
                uint64_t max_bytes,
                CopyFlags copy_flags,
                void **ret_remains,
                size_t *ret_remains_size,
                copy_progress_bytes_t progress,
                void *userdata) {
 
        _cleanup_close_ int fdf_opened = -EBADF, fdt_opened = -EBADF;
        bool try_cfr = true, try_sendfile = true;
        uint64_t copied_total = 0;
        int r;
 
        assert(fdf >= 0);
        assert(fdt >= 0);
        assert(!FLAGS_SET(copy_flags, COPY_LOCK_BSD));
 
        /* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a
         * maximum of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied.
         * Returns negative on error, zero if EOF is hit before the bytes limit is hit and positive
         * otherwise. If the copy fails for some reason but we read but didn't yet write some data and
         * ret_remains/ret_remains_size is not NULL, then it will be initialized with an allocated buffer
         * containing this "remaining" data. Note that these two parameters are initialized with a valid
         * buffer only on failure and only if there's actually data already read. Otherwise these parameters
         * if non-NULL are set to NULL. */
 
        if (ret_remains)
                *ret_remains = NULL;
        if (ret_remains_size)
                *ret_remains_size = 0;
 
        fdf = fd_reopen_condition(fdf, O_CLOEXEC | O_NOCTTY | O_RDONLY, O_PATH, &fdf_opened);
        if (fdf < 0)
                return fdf;
        fdt = fd_reopen_condition(fdt, O_CLOEXEC | O_NOCTTY | O_RDWR, O_PATH, &fdt_opened);
        if (fdt < 0)
                return fdt;
 
        if (FLAGS_SET(copy_flags, COPY_SEEK0_SOURCE) &&
            lseek(fdf, 0, SEEK_SET) < 0)
                return -errno;
 
        if (FLAGS_SET(copy_flags, COPY_SEEK0_TARGET) &&
            lseek(fdt, 0, SEEK_SET) < 0)
                return -errno;
 
        r = try_reflink_copy_bytes(fdf, fdt, max_bytes, copy_flags);
        if (r != -EOPNOTSUPP)
                return r;
 
        usec_t start_timestamp = USEC_INFINITY;
        if (progress)
                start_timestamp = now(CLOCK_MONOTONIC);
 
        for (;;) {
                ssize_t n;
                size_t m;
 
                if (max_bytes <= 0)
                        break;
 
                r = look_for_signals(copy_flags);
                if (r < 0)
                        return r;
 
                /* sendfile() accepts at most SSIZE_MAX-offset bytes to copy, hence let's subtract how much
                 * copied so far from SSIZE_MAX as maximum of what we want to copy. */
                if (try_sendfile) {
                        assert(copied_total < SSIZE_MAX);
                        m = (uint64_t) SSIZE_MAX - copied_total;
                } else
                        m = SSIZE_MAX;
 
                if (max_bytes != UINT64_MAX && m > max_bytes)
                        m = max_bytes;
 
                if (progress && m > PROGRESS_STEP_SIZE)
                        m = PROGRESS_STEP_SIZE;
 
                if (copy_flags & COPY_HOLES) {
                        off_t c, e;
 
                        c = lseek(fdf, 0, SEEK_CUR);
                        if (c < 0)
                                return -errno;
 
                        /* To see if we're in a hole, we search for the next data offset. */
                        e = lseek(fdf, c, SEEK_DATA);
                        if (e < 0 && errno == ENXIO)
                                /* If errno == ENXIO, that means we've reached the final hole of the file and
                                * that hole isn't followed by more data. */
                                e = lseek(fdf, 0, SEEK_END);
                        if (e < 0)
                                return -errno;
 
                        /* If we're in a hole (current offset is not a data offset), create a hole of the
                         * same size in the target file. */
                        if (e > c) {
                                /* Make sure our new hole doesn't go over the maximum size we're allowed to copy. */
                                n = MIN(max_bytes, (uint64_t) e - c);
                                r = create_hole(fdt, n);
                                if (r < 0)
                                        return r;
 
                                /* Make sure holes are taken into account in the maximum size we're supposed to copy. */
                                if (max_bytes != UINT64_MAX) {
                                        max_bytes -= n;
                                        if (max_bytes <= 0)
                                                break;
                                }
 
                                /* Update the size we're supposed to copy in this iteration if needed. */
                                if (m > max_bytes)
                                        m = max_bytes;
                        }
 
                        c = e; /* Set c to the start of the data segment. */
 
                        /* After copying a potential hole, find the end of the data segment by looking for
                         * the next hole. If we get ENXIO, we're at EOF. */
                        e = lseek(fdf, c, SEEK_HOLE);
                        if (e < 0) {
                                if (errno == ENXIO)
                                        break;
                                return -errno;
                        }
                        if (e == c)
                                /* Empty data segment? Maybe concurrent hole punching taking place? Or EOF?
                                 * Let's figure this out by copying the smallest amount possible */
                                m = 1;
                        else {
                                assert(e > c);
 
                                /* SEEK_HOLE modifies the file offset so we need to move back to the initial offset. */
                                if (lseek(fdf, c, SEEK_SET) < 0)
                                        return -errno;
 
                                /* Make sure we're not copying more than the current data segment. */
                                m = MIN(m, (size_t) e - c);
                                assert(m > 0);
                        }
                }
 
                /* First, try copy_file_range(), unless we already tried */
                if (try_cfr) {
                        n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
                        if (n < 0) {
                                if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF, -EOPNOTSUPP))
                                        return n;
 
                                try_cfr = false;
                                /* use fallback below */
                        } else if (n == 0) { /* likely EOF */
 
                                if (copied_total > 0)
                                        break;
 
                                /* So, we hit EOF immediately, without having copied a single byte. This
                                 * could indicate two things: the file is actually empty, or we are on some
                                 * virtual file system such as procfs/sysfs where the syscall actually
                                 * doesn't work but doesn't return an error. Try to handle that, by falling
                                 * back to simple read()s in case we encounter empty files.
                                 *
                                 * See: https://lwn.net/Articles/846403/ */
                                try_cfr = try_sendfile = false;
                        } else
                                /* Success! */
                                goto next;
                }
 
                /* Second, try sendfile(), unless we already tried */
                if (try_sendfile) {
                        n = sendfile(fdt, fdf, NULL, m);
                        if (n < 0) {
                                if (!IN_SET(errno, EINVAL, ENOSYS))
                                        return -errno;
 
                                try_sendfile = false;
                                /* use fallback below */
                        } else if (n == 0) { /* likely EOF */
 
                                if (copied_total > 0)
                                        break;
 
                                try_sendfile = false; /* same logic as above for copy_file_range() */
                        } else
                                /* Success! */
                                goto next;
                }
 
                /* Third, as a fallback just copy bits by hand */
                {
                        uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
                        ssize_t z;
 
                        n = read(fdf, buf, sizeof buf);
                        if (n < 0)
                                return -errno;
                        if (n == 0) /* EOF */
                                break;
 
                        z = (size_t) n;
                        do {
                                ssize_t k;
 
                                k = write(fdt, p, z);
                                if (k < 0) {
                                        r = -errno;
 
                                        if (ret_remains) {
                                                void *copy;
 
                                                copy = memdup(p, z);
                                                if (!copy)
                                                        return -ENOMEM;
 
                                                *ret_remains = copy;
                                        }
 
                                        if (ret_remains_size)
                                                *ret_remains_size = z;
 
                                        return r;
                                }
 
                                assert(k <= z);
                                z -= k;
                                p += k;
                        } while (z > 0);
                }
 
        next:
                copied_total += n;
 
                /* Disable sendfile() in case we are getting too close to it's SSIZE_MAX-offset limit */
                if (copied_total > SSIZE_MAX - COPY_BUFFER_SIZE)
                        try_sendfile = false;
 
                if (progress) {
                        usec_t t = now(CLOCK_MONOTONIC);
                        usec_t d = usec_sub_unsigned(t, start_timestamp);
                        uint64_t bps = UINT64_MAX;
                        if (d > USEC_PER_SEC * 3U)
                                bps = (uint64_t) (copied_total / ((double) d / USEC_PER_SEC));
 
                        r = progress(n, bps, userdata);
                        if (r < 0)
                                return r;
                }
 
                if (max_bytes != UINT64_MAX) {
                        assert(max_bytes >= (uint64_t) n);
                        max_bytes -= n;
                }
        }
 
        if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) {
                r = fd_verify_linked(fdf);
                if (r < 0)
                        return r;
        }
 
        if (copy_flags & COPY_TRUNCATE) {
                off_t off = lseek(fdt, 0, SEEK_CUR);
                if (off < 0)
                        return -errno;
 
                if (ftruncate(fdt, off) < 0)
                        return -errno;
        }
 
        return max_bytes <= 0; /* return 0 if we hit EOF earlier than the size limit */
}
 
/* Check whether *ts is within [epoch; clamp] */
static bool timespec_within(const struct timespec *ts, usec_t ts_clamp) {
        assert(ts);
 
        return ts->tv_sec < 0 ||              /* Date pre-1970, cannot clamp these */
               timespec_load(ts) <= ts_clamp; /* Date in [epoch; clamp] */
}
 
/* See ts_clamp in copy.h. */
static void clamp_inode_times(const struct stat *st, usec_t ts_clamp, struct timespec ret[static 2]) {
        assert(st);
        assert(ret);
 
        ret[0] = timespec_within(&st->st_atim, ts_clamp) ? st->st_atim : *TIMESPEC_STORE(ts_clamp);
        ret[1] = timespec_within(&st->st_mtim, ts_clamp) ? st->st_mtim : *TIMESPEC_STORE(ts_clamp);
}
 
/* Put *st's clamped times on the inode referred to by fdt */
static int clamp_futimens(int fdt, const struct stat *st, usec_t ts_clamp) {
        struct timespec times[2];
 
        assert(fdt >= 0);
        assert(st);
 
        clamp_inode_times(st, ts_clamp, times);
        return RET_NERRNO(futimens(fdt, times));
}
 
/* Same for (dt, to) */
static int clamp_utimensat(int dt, const char *to, const struct stat *st, usec_t ts_clamp) {
        struct timespec times[2];
 
        assert(dt >= 0 || dt == AT_FDCWD);
        assert(to);
        assert(st);
 
        clamp_inode_times(st, ts_clamp, times);
        return RET_NERRNO(utimensat(dt, to, times, AT_SYMLINK_NOFOLLOW));
}
 
static int fd_copy_symlink(
                int df,
                const char *from,
                const struct stat *st,
                int dt,
                const char *to,
                uid_t override_uid,
                gid_t override_gid,
                CopyFlags copy_flags,
                usec_t ts_clamp) {
 
        _cleanup_free_ char *target = NULL;
        int r;
 
        assert(st);
        assert(to);
 
        r = readlinkat_malloc(df, from, &target);
        if (r < 0)
                return r;
 
        if (copy_flags & COPY_MAC_CREATE) {
                r = mac_selinux_create_file_prepare_at(dt, to, S_IFLNK, /* label_context= */ NULL);
                if (r < 0)
                        return r;
        }
        r = RET_NERRNO(symlinkat(target, dt, to));
        if (copy_flags & COPY_MAC_CREATE)
                mac_selinux_create_file_clear();
        if (r < 0) {
                if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))) {
                        log_notice_errno(r, "Failed to copy symlink%s%s%s, ignoring: %m",
                                         isempty(from) ? "" : " '",
                                         strempty(from),
                                         isempty(from) ? "" : "'");
                        return 0;
                }
 
                return r;
        }
 
        if (fchownat(dt, to,
                     uid_is_valid(override_uid) ? override_uid : st->st_uid,
                     gid_is_valid(override_gid) ? override_gid : st->st_gid,
                     AT_SYMLINK_NOFOLLOW) < 0)
                r = -errno;
 
        (void) copy_xattr(df, from, dt, to, copy_flags);
 
        (void) clamp_utimensat(dt, to, st, ts_clamp);
        return r;
}
 
/* Encapsulates the database we store potential hardlink targets in */
typedef struct HardlinkContext {
        int dir_fd;    /* An fd to the directory we use as lookup table. Never AT_FDCWD. Lazily created, when
                        * we add the first entry. */
 
        /* These two fields are used to create the hardlink repository directory above — via
         * mkdirat(parent_fd, subdir) — and are kept so that we can automatically remove the directory again
         * when we are done. */
        int parent_fd; /* Possibly AT_FDCWD */
        char *subdir;
} HardlinkContext;
 
static int hardlink_context_setup(
                HardlinkContext *c,
                int dt,
                const char *to,
                CopyFlags copy_flags) {
 
        _cleanup_close_ int dt_copy = -EBADF;
        int r;
 
        assert(c);
        assert(c->dir_fd < 0 && c->dir_fd != AT_FDCWD);
        assert(c->parent_fd < 0);
        assert(!c->subdir);
 
        /* If hardlink recreation is requested we have to maintain a database of inodes that are potential
         * hardlink sources. Given that generally disk sizes have to be assumed to be larger than what fits
         * into physical RAM we cannot maintain that database in dynamic memory alone. Here we opt to
         * maintain it on disk, to simplify things: inside the destination directory we'll maintain a
         * temporary directory consisting of hardlinks of every inode we copied that might be subject of
         * hardlinks. We can then use that as hardlink source later on. Yes, this means additional disk IO
         * but thankfully Linux is optimized for this kind of thing. If this ever becomes a performance
         * bottleneck we can certainly place an in-memory hash table in front of this, but for the beginning,
         * let's keep things simple, and just use the disk as lookup table for inodes.
         *
         * Note that this should have zero performance impact as long as .n_link of all files copied remains
         * <= 0, because in that case we will not actually allocate the hardlink inode lookup table directory
         * on disk (we do so lazily, when the first candidate with .n_link > 1 is seen). This means, in the
         * common case where hardlinks are not used at all or only for few files the fact that we store the
         * table on disk shouldn't matter performance-wise. */
 
        if (!FLAGS_SET(copy_flags, COPY_HARDLINKS))
                return 0;
 
        if (dt == AT_FDCWD)
                dt_copy = AT_FDCWD;
        else if (dt < 0)
                return -EBADF;
        else {
                dt_copy = fcntl(dt, F_DUPFD_CLOEXEC, 3);
                if (dt_copy < 0)
                        return -errno;
        }
 
        if (to)
                r = tempfn_random_child(to, "hardlink", &c->subdir);
        else
                r = tempfn_random("hardlink", /* extra= */ NULL, &c->subdir);
        if (r < 0)
                return r;
 
        c->parent_fd = TAKE_FD(dt_copy);
 
        /* We don't actually create the directory we keep the table in here, that's done on-demand when the
         * first entry is added, using hardlink_context_realize() below. */
        return 1;
}
 
static int hardlink_context_realize(HardlinkContext *c) {
        if (!c)
                return 0;
 
        if (c->dir_fd >= 0) /* Already realized */
                return 1;
 
        if (c->parent_fd < 0 && c->parent_fd != AT_FDCWD) /* Not configured */
                return 0;
 
        assert(c->subdir);
 
        c->dir_fd = open_mkdir_at(c->parent_fd, c->subdir, O_EXCL|O_CLOEXEC, 0700);
        if (c->dir_fd < 0)
                return c->dir_fd;
 
        return 1;
}
 
static void hardlink_context_destroy(HardlinkContext *c) {
        int r;
 
        assert(c);
 
        /* Automatically remove the hardlink lookup table directory again after we are done. This is used via
         * _cleanup_() so that we really delete this, even on failure. */
 
        if (c->dir_fd >= 0) {
                /* <dir_fd> might be have already been used for reading, so we need to rewind it. */
                if (lseek(c->dir_fd, 0, SEEK_SET) < 0)
                        log_debug_errno(errno, "Failed to lseek on file descriptor, ignoring: %m");
 
                r = rm_rf_children(TAKE_FD(c->dir_fd), REMOVE_PHYSICAL, NULL); /* consumes dir_fd in all cases, even on failure */
                if (r < 0)
                        log_debug_errno(r, "Failed to remove hardlink store (%s) contents, ignoring: %m", c->subdir);
 
                assert(c->parent_fd >= 0 || c->parent_fd == AT_FDCWD);
                assert(c->subdir);
 
                if (unlinkat(c->parent_fd, c->subdir, AT_REMOVEDIR) < 0)
                        log_debug_errno(errno, "Failed to remove hardlink store (%s) directory, ignoring: %m", c->subdir);
        }
 
        assert_cc(AT_FDCWD < 0);
        c->parent_fd = safe_close(c->parent_fd);
 
        c->subdir = mfree(c->subdir);
}
 
static int try_hardlink(
                HardlinkContext *c,
                const struct stat *st,
                int dt,
                const char *to) {
 
        char dev_ino[DECIMAL_STR_MAX(dev_t)*2 + DECIMAL_STR_MAX(uint64_t) + 4];
 
        assert(st);
        assert(dt >= 0 || dt == AT_FDCWD);
        assert(to);
 
        if (!c) /* No temporary hardlink directory, don't bother */
                return 0;
 
        if (st->st_nlink <= 1) /* Source not hardlinked, don't bother */
                return 0;
 
        if (c->dir_fd < 0) /* not yet realized, hence empty */
                return 0;
 
        xsprintf(dev_ino, "%u:%u:%" PRIu64, major(st->st_dev), minor(st->st_dev), (uint64_t) st->st_ino);
        if (linkat(c->dir_fd, dev_ino, dt, to, 0) < 0)  {
                if (errno != ENOENT) /* doesn't exist in store yet */
                        log_debug_errno(errno, "Failed to hardlink %s to %s, ignoring: %m", dev_ino, to);
                return 0;
        }
 
        return 1;
}
 
static int memorize_hardlink(
                HardlinkContext *c,
                const struct stat *st,
                int dt,
                const char *to) {
 
        char dev_ino[DECIMAL_STR_MAX(dev_t)*2 + DECIMAL_STR_MAX(uint64_t) + 4];
        int r;
 
        assert(st);
        assert(dt >= 0 || dt == AT_FDCWD);
        assert(to);
 
        if (!c) /* No temporary hardlink directory, don't bother */
                return 0;
 
        if (st->st_nlink <= 1) /* Source not hardlinked, don't bother */
                return 0;
 
        r = hardlink_context_realize(c); /* Create the hardlink store lazily */
        if (r < 0)
                return r;
 
        xsprintf(dev_ino, "%u:%u:%" PRIu64, major(st->st_dev), minor(st->st_dev), (uint64_t) st->st_ino);
        if (linkat(dt, to, c->dir_fd, dev_ino, 0) < 0) {
                log_debug_errno(errno, "Failed to hardlink %s to %s, ignoring: %m", to, dev_ino);
                return 0;
        }
 
        return 1;
}
 
static int prepare_nocow(int fdf, const char *from, int fdt, unsigned *chattr_mask, unsigned *chattr_flags) {
        unsigned attrs = 0;
        int r;
 
        assert(fdf >= 0 || fdf == AT_FDCWD);
        assert(fdt >= 0);
        assert(!!chattr_mask == !!chattr_flags);
 
        /* If caller explicitly requested NOCOW to be set or unset, let's not interfere. */
        if (chattr_mask && FLAGS_SET(*chattr_mask, FS_NOCOW_FL))
                return 0;
 
        r = read_attr_at(fdf, from, &attrs);
        if (r < 0 && !ERRNO_IS_IOCTL_NOT_SUPPORTED(r) && r != -ELOOP) /* If the source is a symlink we get ELOOP */
                return r;
 
        if (FLAGS_SET(attrs, FS_NOCOW_FL)) {
                if (chattr_mask && chattr_flags) {
                        *chattr_mask |= FS_NOCOW_FL;
                        *chattr_flags |= FS_NOCOW_FL;
                } else
                        /* If the NOCOW flag is set on the source, make the copy NOCOW as well. If the source
                         * is not NOCOW, don't do anything in particular with the copy. */
                        (void) chattr_fd(fdt, FS_NOCOW_FL, FS_NOCOW_FL);
        }
 
        return 0;
}
 
/* Copies fs-verity status.  May re-open fdt to do its job. */
static int copy_fs_verity(int fdf, int *fdt) {
        int r;
 
        assert(fdf >= 0);
        assert(fdt);
        assert(*fdt >= 0);
 
        r = fd_verify_regular(fdf);
        if (r < 0)
                return r;
 
        struct fsverity_descriptor desc = {};
        struct fsverity_read_metadata_arg read_arg = {
                .metadata_type = FS_VERITY_METADATA_TYPE_DESCRIPTOR,
                .buf_ptr = (uintptr_t) &desc,
                .length = sizeof(desc),
        };
 
        r = ioctl(fdf, FS_IOC_READ_VERITY_METADATA, &read_arg);
        if (r < 0) {
                /* ENODATA means that the file doesn't have fs-verity,
                 * so the correct thing to do is to do nothing at all. */
                if (errno == ENODATA)
                        return 0;
                log_error_errno(errno, "Failed to read fs-verity metadata from source file: %m");
                /* For cases where fs-verity is unsupported we return a special error code */
                if (ERRNO_IS_NOT_SUPPORTED(errno))
                        return -ESOCKTNOSUPPORT;
                return -errno;
        }
 
        /* Make sure that the descriptor is completely initialized */
        assert(r == (int) sizeof desc);
 
        r = fd_verify_regular(*fdt);
        if (r < 0)
                return r;
 
        /* Okay. We're doing this now. We need to re-open fdt as read-only because
         * we can't enable fs-verity while writable file descriptors are outstanding. */
        _cleanup_close_ int reopened_fd = -EBADF;
        r = fd_reopen_condition(*fdt, O_RDONLY|O_CLOEXEC|O_NOCTTY, O_ACCMODE_STRICT|O_PATH, &reopened_fd);
        if (r < 0)
                return r;
        if (reopened_fd >= 0)
                close_and_replace(*fdt, reopened_fd);
 
        struct fsverity_enable_arg enable_arg = {
                .version = desc.version,
                .hash_algorithm = desc.hash_algorithm,
                .block_size = UINT32_C(1) << desc.log_blocksize,
                .salt_size = desc.salt_size,
                .salt_ptr = (uintptr_t) &desc.salt,
        };
 
        if (ioctl(*fdt, FS_IOC_ENABLE_VERITY, &enable_arg) < 0) {
                log_error_errno(errno, "Failed to set fs-verity metadata: %m");
                /* For cases where fs-verity is unsupported we return a special error code */
                if (ERRNO_IS_NOT_SUPPORTED(errno))
                        return -ESOCKTNOSUPPORT;
                return -errno;
        }
 
        return 0;
}
 
static int fd_copy_tree_generic(
                int df,
                const char *from,
                const struct stat *st,
                int dt,
                const char *to,
                dev_t original_device,
                unsigned depth_left,
                uid_t override_uid,
                gid_t override_gid,
                CopyFlags copy_flags,
                usec_t ts_clamp,
                Hashmap *denylist,
                Hashmap *subvolumes,
                HardlinkContext *hardlink_context,
                const char *display_path,
                copy_progress_path_t progress_path,
                copy_progress_bytes_t progress_bytes,
                void *userdata);
 
static int fd_copy_regular(
                int df,
                const char *from,
                const struct stat *st,
                int dt,
                const char *to,
                uid_t override_uid,
                gid_t override_gid,
                CopyFlags copy_flags,
                usec_t ts_clamp,
                HardlinkContext *hardlink_context,
                copy_progress_bytes_t progress,
                void *userdata) {
 
        _cleanup_close_ int fdf = -EBADF, fdt = -EBADF;
        int r, q;
 
        assert(st);
        assert(to);
 
        r = try_hardlink(hardlink_context, st, dt, to);
        if (r < 0)
                return r;
        if (r > 0) /* worked! */
                return 0;
 
        fdf = xopenat_full(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, XO_REGULAR, 0);
        if (fdf < 0)
                return fdf;
 
        if (copy_flags & COPY_MAC_CREATE) {
                r = mac_selinux_create_file_prepare_at(dt, to, S_IFREG, /* label_context= */ NULL);
                if (r < 0)
                        return r;
        }
        fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
        if (copy_flags & COPY_MAC_CREATE)
                mac_selinux_create_file_clear();
        if (fdt < 0)
                return -errno;
 
        r = prepare_nocow(fdf, /* from= */ NULL, fdt, /* chattr_mask= */ NULL, /* chattr_flags= */ NULL);
        if (r < 0)
                return r;
 
        r = copy_bytes_full(fdf, fdt, UINT64_MAX, copy_flags, NULL, NULL, progress, userdata);
        if (r < 0)
                goto fail;
 
        if (fchown(fdt,
                   uid_is_valid(override_uid) ? override_uid : st->st_uid,
                   gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
                r = -errno;
 
        if (fchmod(fdt, st->st_mode & 07777) < 0)
                r = -errno;
 
        (void) clamp_futimens(fdt, st, ts_clamp);
        (void) copy_xattr(fdf, NULL, fdt, NULL, copy_flags);
 
        if (FLAGS_SET(copy_flags, COPY_VERIFY_LINKED)) {
                r = fd_verify_linked(fdf);
                if (r < 0)
                        return r;
        }
 
        /* NB: fs-verity cannot be enabled when a writable file descriptor is outstanding.
         * copy_fs_verity() may well re-open 'fdt' as O_RDONLY. All code below this point
         * needs to be able to work with a read-only file descriptor. */
        if (FLAGS_SET(copy_flags, COPY_PRESERVE_FS_VERITY)) {
                r = copy_fs_verity(fdf, &fdt);
                if (r < 0)
                        goto fail;
        }
 
        if (copy_flags & COPY_FSYNC) {
                if (fsync(fdt) < 0) {
                        r = -errno;
                        goto fail;
                }
        }
 
        q = close_nointr(TAKE_FD(fdt)); /* even if this fails, the fd is now invalidated */
        if (q < 0) {
                r = q;
                goto fail;
        }
 
        (void) memorize_hardlink(hardlink_context, st, dt, to);
        return r;
 
fail:
        (void) unlinkat(dt, to, 0);
        return r;
}
 
static int fd_copy_fifo(
                int df,
                const char *from,
                const struct stat *st,
                int dt,
                const char *to,
                uid_t override_uid,
                gid_t override_gid,
                CopyFlags copy_flags,
                usec_t ts_clamp,
                HardlinkContext *hardlink_context) {
        int r;
 
        assert(st);
        assert(to);
 
        r = try_hardlink(hardlink_context, st, dt, to);
        if (r < 0)
                return r;
        if (r > 0) /* worked! */
                return 0;
 
        if (copy_flags & COPY_MAC_CREATE) {
                r = mac_selinux_create_file_prepare_at(dt, to, S_IFIFO, /* label_context= */ NULL);
                if (r < 0)
                        return r;
        }
        r = RET_NERRNO(mkfifoat(dt, to, st->st_mode & 07777));
        if (copy_flags & COPY_MAC_CREATE)
                mac_selinux_create_file_clear();
        if (FLAGS_SET(copy_flags, COPY_GRACEFUL_WARN) && (ERRNO_IS_NEG_PRIVILEGE(r) || ERRNO_IS_NEG_NOT_SUPPORTED(r))) {
                log_notice_errno(r, "Failed to copy fifo%s%s%s, ignoring: %m",
                                 isempty(from) ? "" : " '",
                                 strempty(from),
                                 isempty(from) ? "" : "'");
                return 0;
        } else if (r < 0)
                return r;
 
        if (fchownat(dt, to,
                     uid_is_valid(override_uid) ? override_uid : st->st_uid,
                     gid_is_valid(override_gid) ? override_gid : st->st_gid,
                     AT_SYMLINK_NOFOLLOW) < 0)
                r = -errno;
 
        if (fchmodat(dt, to, st->st_mode & 07777, AT_SYMLINK_NOFOLLOW) < 0)
                r = -errno;
 
        (void) clamp_utimensat(dt, to, st, ts_clamp);
 
        (void) memorize_hardlink(hardlink_context, st, dt, to);
        return r;
}