HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/repart/repart.c

483,181 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: b40c7a91acf99c5ac898f4de16e45b142c19fa8c

Lines 6001–7000 of 11,995

                        return log_error_errno(errno, "Failed to open LUKS volume '%s': %m", vol);
 
                if (flock(dev_fd, LOCK_EX) < 0)
                        return log_error_errno(errno, "Failed to lock '%s': %m", vol);
 
                t = new(DecryptedPartitionTarget, 1);
                if (!t)
                        return log_oom();
 
                *t = (DecryptedPartitionTarget) {
                        .fd = TAKE_FD(dev_fd),
                        .dm_name = TAKE_PTR(dm_name),
                        .volume = TAKE_PTR(vol),
                        .device = TAKE_PTR(cd),
                };
 
                target->decrypted = TAKE_PTR(t);
        }
 
        log_info("Successfully encrypted future partition %" PRIu64 ".", p->partno);
 
        return 0;
#else
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
                               "libcryptsetup is not supported, cannot encrypt.");
#endif
}
 
static int partition_format_verity_hash(
                Context *context,
                Partition *p,
                const char *node,
                const char *data_node) {
 
#if HAVE_LIBCRYPTSETUP
        Partition *dp;
        _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
        _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
        _cleanup_free_ char *hint = NULL;
        int r;
 
        assert(context);
        assert(p);
        assert(p->verity == VERITY_HASH);
        assert(data_node);
 
        if (p->dropped)
                return 0;
 
        if (PARTITION_EXISTS(p)) /* Never format existing partitions */
                return 0;
 
        /* Either we are minimizing the partition or we were instructed to copy an existing hash block directly. */
        if (p->copy_blocks_fd >= 0)
                return 0;
 
        assert_se(dp = p->siblings[VERITY_DATA]);
        assert(!dp->dropped);
 
        (void) partition_hint(p, node, &hint);
 
        r = dlopen_cryptsetup(LOG_ERR);
        if (r < 0)
                return r;
 
        if (p->partno != UINT64_MAX)
                log_info("Calculating Verity protection data for future partition %" PRIu64 "...", p->partno);
 
        if (!node) {
                r = partition_target_prepare(context, p, p->new_size, /* need_path= */ true, &t);
                if (r < 0)
                        return r;
 
                node = partition_target_path(t);
        }
 
        r = sym_crypt_init(&cd, node);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate libcryptsetup context for %s: %m", node);
 
        cryptsetup_enable_logging(cd);
 
        r = sym_crypt_format(
                        cd, CRYPT_VERITY, NULL, NULL, SD_ID128_TO_UUID_STRING(p->verity_uuid), NULL, 0,
                        &(struct crypt_params_verity){
                                .data_device = data_node,
                                .flags = CRYPT_VERITY_CREATE_HASH,
                                .hash_name = "sha256",
                                .hash_type = 1,
                                .data_block_size = p->verity_data_block_size,
                                .hash_block_size = p->verity_hash_block_size,
                                .salt_size = sizeof(p->verity_salt),
                                .salt = (const char*)p->verity_salt,
                        });
        if (r < 0) {
                /* libcryptsetup reports non-descriptive EIO errors for every I/O failure. Luckily, it
                 * doesn't clobber errno so let's check for ENOSPC so we can report a better error if the
                 * partition is too small. */
                if (r == -EIO && errno == ENOSPC)
                        return log_error_errno(errno,
                                               "Verity hash data does not fit in partition %s with size %s",
                                               strna(hint), FORMAT_BYTES(p->new_size));
 
                return log_error_errno(r, "Failed to setup verity hash data of partition %s: %m", strna(hint));
        }
 
        if (t) {
                r = partition_target_sync(context, p, t);
                if (r < 0)
                        return r;
        }
 
        r = sym_crypt_get_volume_key_size(cd);
        if (r < 0)
                return log_error_errno(r, "Failed to determine verity root hash size of partition %s: %m", strna(hint));
 
        _cleanup_(iovec_done) struct iovec rh = {
                .iov_base = malloc(r),
                .iov_len = r,
        };
        if (!rh.iov_base)
                return log_oom();
 
        r = sym_crypt_volume_key_get(cd, CRYPT_ANY_SLOT, (char *) rh.iov_base, &rh.iov_len, NULL, 0);
        if (r < 0)
                return log_error_errno(r, "Failed to get verity root hash of partition %s: %m", strna(hint));
 
        assert(rh.iov_len >= sizeof(sd_id128_t) * 2);
 
        if (!dp->new_uuid_is_set) {
                memcpy_safe(dp->new_uuid.bytes, rh.iov_base, sizeof(sd_id128_t));
                dp->new_uuid_is_set = true;
        }
 
        if (!p->new_uuid_is_set) {
                memcpy_safe(p->new_uuid.bytes, (uint8_t*) rh.iov_base + (rh.iov_len - sizeof(sd_id128_t)), sizeof(sd_id128_t));
                p->new_uuid_is_set = true;
        }
 
        p->roothash = TAKE_STRUCT(rh);
 
        return 0;
#else
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libcryptsetup is not supported, cannot setup verity hashes.");
#endif
}
 
static int sign_verity_roothash(
                Context *context,
                const struct iovec *roothash,
                struct iovec *ret_signature) {
 
#if HAVE_OPENSSL
        _cleanup_(BIO_freep) BIO *rb = NULL;
        _cleanup_(PKCS7_freep) PKCS7 *p7 = NULL;
        _cleanup_free_ char *hex = NULL;
        _cleanup_free_ uint8_t *sig = NULL;
        int sigsz, r;
 
        assert(context);
        assert(context->certificate);
        assert(context->private_key);
        assert(roothash);
        assert(iovec_is_set(roothash));
        assert(ret_signature);
 
        r = dlopen_libcrypto(LOG_ERR);
        if (r < 0)
                return r;
 
        hex = hexmem(roothash->iov_base, roothash->iov_len);
        if (!hex)
                return log_oom();
 
        rb = sym_BIO_new_mem_buf(hex, -1);
        if (!rb)
                return log_oom();
 
        p7 = sym_PKCS7_sign(context->certificate, context->private_key, NULL, rb, PKCS7_DETACHED|PKCS7_NOATTR|PKCS7_BINARY);
        if (!p7)
                return log_openssl_errors(LOG_ERR, "Failed to calculate PKCS7 signature");
 
        sigsz = sym_i2d_PKCS7(p7, &sig);
        if (sigsz < 0)
                return log_openssl_errors(LOG_ERR, "Failed to convert PKCS7 signature to DER");
 
        *ret_signature = IOVEC_MAKE(TAKE_PTR(sig), sigsz);
 
        return 0;
#else
        return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "OpenSSL is not supported, cannot setup verity signature.");
#endif
}
 
static int iovec_roothash_from_uuid_pair(
                sd_id128_t data_uuid,
                sd_id128_t hash_uuid,
                struct iovec *ret_roothash) {
 
        uint8_t roothash_bytes[sizeof(sd_id128_t) * 2];
 
        assert(ret_roothash);
 
        if (sd_id128_is_null(data_uuid) || sd_id128_is_null(hash_uuid))
                return -EINVAL;
 
        /* As per the https://uapi-group.org/specifications/specs/discoverable_partitions_specification/ the
         * UUIDs of the data and verity partitions are respectively the first and second halves of the
         * dm-verity roothash, so we can use them to match the signature to the right partition. */
 
        memcpy(roothash_bytes, data_uuid.bytes, sizeof(sd_id128_t));
        memcpy(roothash_bytes + sizeof(sd_id128_t), hash_uuid.bytes, sizeof(sd_id128_t));
 
        if (!iovec_memdup(&IOVEC_MAKE(roothash_bytes, sizeof(roothash_bytes)), ret_roothash))
                return -ENOMEM;
 
        return 0;
}
 
static const VeritySettings *lookup_verity_settings_by_uuid_pair(sd_id128_t data_uuid, sd_id128_t hash_uuid) {
        _cleanup_(iovec_done) struct iovec roothash = {};
        int r;
 
        r = iovec_roothash_from_uuid_pair(data_uuid, hash_uuid, &roothash);
        if (r < 0)
                return NULL;
 
        VeritySettings key = {
                .root_hash = roothash,
        };
 
        return set_get(arg_verity_settings, &key);
}
 
static int partition_format_verity_sig(Context *context, Partition *p) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
        _cleanup_free_ char *text = NULL, *hint = NULL;
        const VeritySettings *verity_settings;
        Partition *hp, *rp;
        uint8_t fp[X509_FINGERPRINT_SIZE];
        int whole_fd, r;
        bool has_fp = false;
 
        assert(p->verity == VERITY_SIG);
 
        if (p->dropped)
                return 0;
 
        if (PARTITION_EXISTS(p))
                return 0;
 
        /* We were instructed to copy an existing signature block directly */
        if (p->copy_blocks_fd >= 0)
                return 0;
 
        assert_se(hp = p->siblings[VERITY_HASH]);
        assert(!hp->dropped);
        assert_se(rp = p->siblings[VERITY_DATA]);
        assert(!rp->dropped);
 
        /* Currently only set while formatting the hash partition. But if this is skipped via CopyBlocks=
         * we just derive the roothash from the UUIDs from the data + hash partition. */
        if (!iovec_is_set(&hp->roothash)) {
                r = iovec_roothash_from_uuid_pair(rp->new_uuid, hp->new_uuid, &hp->roothash);
                if (r < 0)
                        return log_error_errno(r, "Unable to derive roothash: %m");
        }
 
        verity_settings = lookup_verity_settings_by_uuid_pair(rp->current_uuid, hp->current_uuid);
 
        if (!verity_settings) {
#if HAVE_OPENSSL
                if (!context->private_key)
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Verity signature partition signing requested but no private key provided (--private-key=).");
 
                if (!context->certificate)
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                               "Verity signature partition signing requested but no PEM certificate provided (--certificate=).");
#else
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Verity signature partition signing requested but OpenSSL support is disabled.");
#endif
        }
 
        (void) partition_hint(p, context->node, &hint);
 
        assert_se((whole_fd = sym_fdisk_get_devfd(context->fdisk_context)) >= 0);
 
        _cleanup_(iovec_done) struct iovec sig_free = {};
        const struct iovec *roothash, *sig;
        if (verity_settings) {
                sig = &verity_settings->root_hash_sig;
                roothash = &verity_settings->root_hash;
        } else {
                r = sign_verity_roothash(context, &hp->roothash, &sig_free);
                if (r < 0)
                        return r;
 
                sig = &sig_free;
                roothash = &hp->roothash;
        }
 
#if HAVE_OPENSSL
        r = x509_fingerprint(context->certificate, fp);
        if (r < 0)
                return log_error_errno(r, "Unable to calculate X509 certificate fingerprint: %m");
        has_fp = true;
#endif
 
        r = sd_json_buildo(
                        &v,
                        SD_JSON_BUILD_PAIR("rootHash", SD_JSON_BUILD_HEX(roothash->iov_base, roothash->iov_len)),
                        SD_JSON_BUILD_PAIR_CONDITION(has_fp, "certificateFingerprint", SD_JSON_BUILD_HEX(fp, sizeof(fp))),
                        SD_JSON_BUILD_PAIR("signature", JSON_BUILD_IOVEC_BASE64(sig)));
        if (r < 0)
                return log_error_errno(r, "Failed to build verity signature JSON object: %m");
 
        r = sd_json_variant_format(v, 0, &text);
        if (r < 0)
                return log_error_errno(r, "Failed to format verity signature JSON object: %m");
 
        if (strlen(text)+1 > p->new_size)
                return log_error_errno(SYNTHETIC_ERRNO(E2BIG), "Verity signature too long for partition.");
 
        r = strgrowpad0(&text, p->new_size);
        if (r < 0)
                return log_error_errno(r, "Failed to pad string to %s", FORMAT_BYTES(p->new_size));
 
        r = pwrite_full(whole_fd, text, p->new_size, p->offset);
        if (r < 0)
                return log_error_errno(r, "Failed to write verity signature to partition %s: %m", strna(hint));
 
        if (fsync(whole_fd) < 0)
                return log_error_errno(errno, "Failed to synchronize partition %s: %m", strna(hint));
 
        return 0;
}
 
static int progress_bytes(uint64_t n_bytes, uint64_t bps, void *userdata) {
        Partition *p = ASSERT_PTR(userdata);
        unsigned percent;
 
        p->copy_blocks_done += n_bytes;
 
        /* Catch division by zero. */
        if (p->copy_blocks_done >= p->copy_blocks_size)
                percent = 100;
        else
                percent = (unsigned) (100.0 * (double) p->copy_blocks_done / (double) p->copy_blocks_size);
 
        if (percent == p->last_percent)
                return 0;
 
        if (!ratelimit_below(&p->progress_ratelimit))
                return 0;
 
        if (bps != UINT64_MAX)
                (void) draw_progress_barf(
                                percent,
                                "%s %s %s %s/%s %s/s",
                                strna(p->copy_blocks_path),
                                glyph(GLYPH_ARROW_RIGHT),
                                strna(p->definition_path),
                                FORMAT_BYTES_WITH_POINT(p->copy_blocks_done),
                                FORMAT_BYTES_WITH_POINT(p->copy_blocks_size),
                                FORMAT_BYTES_WITH_POINT(bps));
        else
                (void) draw_progress_barf(
                                percent,
                                "%s %s %s %s/%s",
                                strna(p->copy_blocks_path),
                                glyph(GLYPH_ARROW_RIGHT),
                                strna(p->definition_path),
                                FORMAT_BYTES_WITH_POINT(p->copy_blocks_done),
                                FORMAT_BYTES_WITH_POINT(p->copy_blocks_size));
 
        p->last_percent = percent;
 
        (void) context_notify(p->context, PROGRESS_COPYING_PARTITION, p->definition_path, percent);
 
        return 0;
}
 
static int context_copy_blocks(Context *context) {
        int r;
 
        assert(context);
 
        /* Copy in file systems on the block level */
 
        LIST_FOREACH(partitions, p, context->partitions) {
                _cleanup_(partition_target_freep) PartitionTarget *t = NULL;
 
                if (p->dropped)
                        continue;
 
                if (PARTITION_EXISTS(p)) /* Never copy over existing partitions */
                        continue;
 
                if (partition_defer(context, p))
                        continue;
 
                /* For offline signing case */
                if (!set_isempty(arg_verity_settings) && partition_designator_is_verity_sig(p->type.designator))
                        return partition_format_verity_sig(context, p);
 
                if (p->copy_blocks_fd < 0)
                        continue;
 
                (void) context_notify(context, PROGRESS_COPYING_PARTITION, p->definition_path, UINT_MAX);
 
                assert(p->new_size != UINT64_MAX);
 
                size_t extra = p->encrypt != ENCRYPT_OFF ? LUKS2_METADATA_KEEP_FREE : 0;
 
                if (p->copy_blocks_size == UINT64_MAX)
                        p->copy_blocks_size = LESS_BY(p->new_size, extra);
 
                assert(p->new_size >= p->copy_blocks_size + extra);
 
                usec_t start_timestamp = now(CLOCK_MONOTONIC);
 
                r = partition_target_prepare(context, p, p->new_size,
                                             /* need_path= */ p->encrypt != ENCRYPT_OFF || p->siblings[VERITY_HASH],
                                             &t);
                if (r < 0)
                        return r;
 
                if (p->encrypt != ENCRYPT_OFF && (t->loop || t->block_partition)) {
                        r = partition_encrypt(context, p, t, /* offline= */ false, /* temporary= */ true);
                        if (r < 0)
                                return r;
                }
 
                if (p->copy_blocks_offset == UINT64_MAX)
                        log_info("Copying in '%s' (%s) on block level into future partition %" PRIu64 ".",
                                 p->copy_blocks_path, FORMAT_BYTES(p->copy_blocks_size), p->partno);
                else {
                        log_info("Copying in '%s' @ %" PRIu64 " (%s) on block level into future partition %" PRIu64 ".",
                                 p->copy_blocks_path, p->copy_blocks_offset, FORMAT_BYTES(p->copy_blocks_size), p->partno);
 
                        if (lseek(p->copy_blocks_fd, p->copy_blocks_offset, SEEK_SET) < 0)
                                return log_error_errno(errno, "Failed to seek to copy blocks offset in %s: %m", p->copy_blocks_path);
                }
 
                /* We call copy_bytes_full() instead of copy_file_range() directly, because copy_file_range()
                 * needs to be called with a size limit to allow for progress updates. But we don't want that
                 * for cloning, we want one big massive reflink if possible, and unfortunately we can't know
                 * if copy_file_range() will do reflink or not, so we can't call it without the size limit.
                 * Hence, call copy_bytes_full(), which tries FICLONE/BTRFS_IOC_CLONE first to do one big
                 * clone, and then falls back to copying in chunks. */
                r = copy_bytes_full(
                                p->copy_blocks_fd,
                                partition_target_fd(t),
                                p->copy_blocks_size,
                                /* copy_flags= */ 0,
                                /* ret_remains= */ NULL,
                                /* ret_remains_size= */ NULL,
                                progress_bytes,
                                p);
                clear_progress_bar(/* prefix= */ NULL);
                if (r < 0)
                        return log_error_errno(r, "Failed to copy in data from '%s': %m", p->copy_blocks_path);
 
                log_info("Copying in of '%s' on block level completed.", p->copy_blocks_path);
 
                if (p->encrypt != ENCRYPT_OFF && !t->loop && !t->block_partition) {
                        r = partition_encrypt(context, p, t, /* offline= */ true, /* temporary= */ true);
                        if (r < 0)
                                return r;
                }
 
                r = partition_target_sync(context, p, t);
                if (r < 0)
                        return r;
 
                usec_t time_spent = usec_sub_unsigned(now(CLOCK_MONOTONIC), start_timestamp);
                if (time_spent > 250 * USEC_PER_MSEC) /* Show throughput, but not if we spent too little time on it, since it's just noise then */
                        log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s (%s/s).",
                                 p->partno, FORMAT_TIMESPAN(time_spent, 0), FORMAT_BYTES((uint64_t) ((double) p->copy_blocks_size / time_spent * USEC_PER_SEC)));
                else
                        log_info("Block level copying and synchronization of partition %" PRIu64 " complete in %s.",
                                 p->partno, FORMAT_TIMESPAN(time_spent, 0));
 
                if (p->siblings[VERITY_HASH] && !partition_defer(context, p->siblings[VERITY_HASH])) {
                        /* The verity hash must cover the partition contents as stored on disk, i.e. the
                         * ciphertext if the partition is encrypted, hence tear down the dm-crypt device
                         * first. */
                        partition_target_drop_decrypted(t);
 
                        r = partition_format_verity_hash(context, p->siblings[VERITY_HASH],
                                                         /* node= */ NULL, partition_target_path(t));
                        if (r < 0)
                                return r;
                }
 
                if (p->siblings[VERITY_SIG] && !partition_defer(context, p->siblings[VERITY_SIG])) {
                        r = partition_format_verity_sig(context, p->siblings[VERITY_SIG]);
                        if (r < 0)
                                return r;
                }
        }
 
        return 0;
}
 
static int add_exclude_path(const char *path, Hashmap **denylist, DenyType type) {
        _cleanup_free_ struct stat *st = NULL;
        int r;
 
        assert(path);
        assert(denylist);
 
        st = new(struct stat, 1);
        if (!st)
                return log_oom();
 
        r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st);
        if (r == -ENOENT)
                return 0;
        if (r < 0)
                return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path);
 
        r = hashmap_ensure_put(denylist, &inode_hash_ops, st, INT_TO_PTR(type));
        if (r == -EEXIST)
                return 0;
        if (r < 0)
                return log_oom();
        if (r > 0)
                TAKE_PTR(st);
 
        return 0;
}
 
static int shallow_join_strv(char ***ret, char **a, char **b) {
        _cleanup_free_ char **joined = NULL;
        char **iter;
 
        assert(ret);
 
        joined = new(char*, strv_length(a) + strv_length(b) + 1);
        if (!joined)
                return log_oom();
 
        iter = joined;
 
        STRV_FOREACH(i, a)
                *(iter++) = *i;
        *iter = NULL;
 
        STRV_FOREACH(i, b)
                if (!strv_contains(joined, *i)) {
                        *(iter++) = *i;
                        *iter = NULL;
                }
 
        *ret = TAKE_PTR(joined);
        return 0;
}
 
static int make_copy_files_denylist(
                Context *context,
                const Partition *p,
                const char *source,
                const char *target,
                Hashmap **ret) {
 
        _cleanup_hashmap_free_ Hashmap *denylist = NULL;
        _cleanup_free_ char **override_exclude_src = NULL, **override_exclude_tgt = NULL;
        int r;
 
        assert(context);
        assert(p);
        assert(source);
        assert(target);
        assert(ret);
 
        /* Always exclude the top level APIVFS and temporary directories since the contents of these
         * directories are almost certainly not intended to end up in an image. */
 
        NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) {
                r = add_exclude_path(s, &denylist, DENY_CONTENTS);
                if (r < 0)
                        return r;
        }
 
        /* Add the user configured excludes. */
 
        if (p->suppressing_supplement) {
                r = shallow_join_strv(&override_exclude_src,
                                      p->exclude_files_source,
                                      p->supplemented_by->exclude_files_source);
                if (r < 0)
                        return r;
                r = shallow_join_strv(&override_exclude_tgt,
                                      p->exclude_files_target,
                                      p->supplemented_by->exclude_files_target);
                if (r < 0)
                        return r;
        }
 
        STRV_FOREACH(e, override_exclude_src ?: p->exclude_files_source) {
                if (path_startswith(source, *e))
                        return 1;
 
                r = add_exclude_path(*e, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE);
                if (r < 0)
                        return r;
        }
 
        STRV_FOREACH(e, override_exclude_tgt ?: p->exclude_files_target) {
                _cleanup_free_ char *path = NULL;
 
                if (path_startswith(target, *e))
                        return 1;
 
                const char *s = path_startswith(*e, target);
                if (!s)
                        continue;
 
                path = path_join(source, s);
                if (!path)
                        return log_oom();
 
                r = add_exclude_path(path, &denylist, endswith(*e, "/") ? DENY_CONTENTS : DENY_INODE);
                if (r < 0)
                        return r;
        }
 
        /* If we're populating a root partition, we don't want any files to end up under the APIVFS mount
         * points. While we already exclude <source>/proc, users could still do something such as
         * "CopyFiles=/abc:/". Now, if /abc has a proc subdirectory with files in it, those will end up in
         * the top level proc directory in the root partition, which we want to avoid. To deal with these
         * cases, whenever we're populating a root partition and the target of CopyFiles= is the root
         * directory of the root partition, we exclude all directories under the source that are named after
         * APIVFS directories or named after mount points of other partitions that are also going to be part
         * of the image. */
 
        if (p->type.designator == PARTITION_ROOT && empty_or_root(target)) {
                LIST_FOREACH(partitions, q, context->partitions) {
                        if (q->type.designator == PARTITION_ROOT)
                                continue;
 
                        const char *sources = gpt_partition_type_mountpoint_nulstr(q->type);
                        if (!sources)
                                continue;
 
                        NULSTR_FOREACH(s, sources) {
                                _cleanup_free_ char *path = NULL;
 
                                /* Exclude only the children of partition mount points so that the nested
                                 * partition mount point itself still ends up in the upper partition. */
 
                                path = path_join(source, s);
                                if (!path)
                                        return -ENOMEM;
 
                                r = add_exclude_path(path, &denylist, DENY_CONTENTS);
                                if (r < 0)
                                        return r;
                        }
                }
 
                NULSTR_FOREACH(s, APIVFS_TMP_DIRS_NULSTR) {
                        _cleanup_free_ char *path = NULL;
 
                        path = path_join(source, s);
                        if (!path)
                                return -ENOMEM;
 
                        r = add_exclude_path(path, &denylist, DENY_CONTENTS);
                        if (r < 0)
                                return r;
                }
        }
 
        *ret = TAKE_PTR(denylist);
        return 0;
}
 
static int add_subvolume_path(const char *path, BtrfsSubvolFlags flags, Hashmap **subvolumes) {
        _cleanup_free_ struct stat *st = NULL;
        int r;
 
        assert(path);
        assert(subvolumes);
 
        st = new(struct stat, 1);
        if (!st)
                return log_oom();
 
        r = chase_and_stat(path, arg_copy_source, CHASE_PREFIX_ROOT, NULL, st);
        if (r == -ENOENT)
                return 0;
        if (r < 0)
                return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), path);
 
        r = hashmap_ensure_put(subvolumes, &inode_hash_ops, st, INT_TO_PTR(flags));
        if (r < 0)
                return log_oom();
 
        TAKE_PTR(st);
 
        return 0;
}
 
static int make_subvolumes_hashmap(const Partition *p, Hashmap **ret) {
        _cleanup_hashmap_free_ Hashmap *hashmap = NULL;
        Subvolume *subvolume;
        int r;
 
        assert(p);
        assert(ret);
 
        ORDERED_HASHMAP_FOREACH(subvolume, p->subvolumes) {
                _cleanup_free_ char *path = NULL;
 
                path = strdup(subvolume->path);
                if (!path)
                        return log_oom();
 
                r = hashmap_ensure_put(&hashmap, &path_hash_ops_free, path, INT_TO_PTR(subvolume->flags));
                if (r < 0)
                        return log_oom();
 
                TAKE_PTR(path);
        }
 
        if (p->suppressing_supplement) {
                Hashmap *supplemented_by;
 
                r = make_subvolumes_hashmap(p->supplemented_by, &supplemented_by);
                if (r < 0)
                        return r;
 
                r = hashmap_merge(hashmap, supplemented_by);
                if (r < 0)
                        return log_oom();
        }
 
        *ret = TAKE_PTR(hashmap);
        return 0;
}
 
static int make_subvolumes_by_source_inode_hashmap(
                const Partition *p,
                const char *source,
                const char *target,
                Hashmap **ret) {
 
        _cleanup_hashmap_free_ Hashmap *hashmap = NULL;
        Subvolume *subvolume;
        int r;
 
        assert(p);
        assert(target);
        assert(ret);
 
        ORDERED_HASHMAP_FOREACH(subvolume, p->subvolumes) {
                _cleanup_free_ char *path = NULL;
 
                const char *s = path_startswith(subvolume->path, target);
                if (!s)
                        continue;
 
                path = path_join(source, s);
                if (!path)
                        return log_oom();
 
                r = add_subvolume_path(path, subvolume->flags, &hashmap);
                if (r < 0)
                        return r;
        }
 
        if (p->suppressing_supplement) {
                Hashmap *supplemented_by;
 
                r = make_subvolumes_by_source_inode_hashmap(p->supplemented_by, source, target, &supplemented_by);
                if (r < 0)
                        return r;
 
                r = hashmap_merge(hashmap, supplemented_by);
                if (r < 0)
                        return log_oom();
        }
 
        *ret = TAKE_PTR(hashmap);
        return 0;
}
 
static int file_is_denylisted(const char *source, Hashmap *denylist) {
        _cleanup_close_ int pfd = -EBADF;
        struct stat st, rst;
        int r;
 
        r = chase_and_stat(source, arg_copy_source, CHASE_PREFIX_ROOT, /* ret_path= */ NULL, &st);
        if (r < 0)
                return log_error_errno(r, "Failed to stat source file '%s/%s': %m", strempty(arg_copy_source), source);
 
        if (PTR_TO_INT(hashmap_get(denylist, &st)) == DENY_INODE)
                return 1;
 
        if (stat(empty_to_root(arg_copy_source), &rst) < 0)
                return log_error_errno(errno, "Failed to stat '%s': %m", empty_to_root(arg_copy_source));
 
        pfd = chase_and_open_parent(source, arg_copy_source, CHASE_PREFIX_ROOT, /* ret_filename= */ NULL);
        if (pfd < 0)
                return log_error_errno(pfd, "Failed to chase '%s/%s': %m", strempty(arg_copy_source), source);
 
        for (;;) {
                if (fstat(pfd, &st) < 0)
                        return log_error_errno(errno, "Failed to stat parent: %m");
 
                if (PTR_TO_INT(hashmap_get(denylist, &st)) != DENY_DONT)
                        return 1;
 
                if (stat_inode_same(&st, &rst))
                        break;
 
                _cleanup_close_ int new_pfd = openat(pfd, "..", O_DIRECTORY|O_RDONLY);
                if (new_pfd < 0)
                        return log_error_errno(errno, "Failed to open parent directory: %m");
 
                close_and_replace(pfd, new_pfd);
        }
 
        return 0;
}
 
/* Adding an entry to a directory bumps its mtime. Clamp it back to ts. */
static int clamp_directory_mtime(int dir_fd, usec_t ts) {
        assert(dir_fd >= 0);
 
        if (ts == USEC_INFINITY)
                return 0;
 
        /* chase() hands out O_PATH descriptors. */
        return futimens_opath(dir_fd, (const struct timespec[2]) { TIMESPEC_OMIT, *TIMESPEC_STORE(ts) });
}
 
static int do_copy_files(Context *context, Partition *p, const char *root) {
        _cleanup_hashmap_free_ Hashmap *subvolumes = NULL;
        int r;
 
        assert(p);
        assert(root);
 
        r = make_subvolumes_hashmap(p, &subvolumes);
        if (r < 0)
                return r;
 
        _cleanup_free_ CopyFiles *copy_files = newdup(CopyFiles, p->copy_files, p->n_copy_files);
        if (!copy_files)
                return log_oom();
 
        size_t n_copy_files = p->n_copy_files;
        if (p->suppressing_supplement) {
                if (!GREEDY_REALLOC_APPEND(copy_files, n_copy_files,
                                           p->supplemented_by->copy_files, p->supplemented_by->n_copy_files))
                        return log_oom();
        }
 
        bool copy_ownership = fstype_can_ownership(p->format);
        usec_t ts = parse_source_date_epoch();
 
        /* copy_tree_at() automatically copies the permissions of source directories to target directories if
         * it created them. However, the root directory is created by us, so we have to manually take care
         * that it is initialized. We use the first source directory targeting "/" as the metadata source for
         * the root directory. */
        FOREACH_ARRAY(line, copy_files, n_copy_files) {
                _cleanup_close_ int rfd = -EBADF, sfd = -EBADF;
 
                if (!path_equal(line->target, "/"))
                        continue;
 
                rfd = open(root, O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
                if (rfd < 0)
                        return -errno;
 
                sfd = chase_and_open(line->source, arg_copy_source, CHASE_PREFIX_ROOT, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOCTTY, NULL);
                if (sfd == -ENOTDIR)
                        continue;
                if (sfd < 0)
                        return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), line->source);
 
                (void) copy_xattr(sfd, NULL, rfd, NULL, COPY_ALL_XATTRS);
                if (copy_ownership)
                        (void) copy_access(sfd, rfd);
                (void) copy_times_full(sfd, rfd, /* flags= */ 0, ts);
 
                break;
        }
 
        uid_t uid = copy_ownership ? UID_INVALID : getuid();
        gid_t gid = copy_ownership ? GID_INVALID : getgid();
 
        FOREACH_ARRAY(line, copy_files, n_copy_files) {
                _cleanup_hashmap_free_ Hashmap *denylist = NULL;
                _cleanup_hashmap_free_ Hashmap *subvolumes_by_source_inode = NULL;
                _cleanup_close_ int sfd = -EBADF, pfd = -EBADF, tfd = -EBADF;
 
                r = make_copy_files_denylist(context, p, line->source, line->target, &denylist);
                if (r < 0)
                        return r;
                if (r > 0)
                        continue;
 
                r = make_subvolumes_by_source_inode_hashmap(p, line->source, line->target, &subvolumes_by_source_inode);
                if (r < 0)
                        return r;
 
                sfd = chase_and_open(line->source, arg_copy_source, CHASE_PREFIX_ROOT, O_CLOEXEC|O_NOCTTY, NULL);
                if (sfd == -ENOENT) {
                        log_notice_errno(sfd, "Failed to open source file '%s%s', skipping: %m", strempty(arg_copy_source), line->source);
                        continue;
                }
                if (sfd < 0)
                        return log_error_errno(sfd, "Failed to open source file '%s%s': %m", strempty(arg_copy_source), line->source);
 
                r = fd_verify_regular(sfd);
                if (r < 0) {
                        if (r != -EISDIR)
                                return log_error_errno(r, "Failed to check type of source file '%s': %m", line->source);
 
                        /* We are looking at a directory */
                        tfd = chase_and_open(line->target, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
                        if (tfd < 0) {
                                _cleanup_free_ char *dn = NULL, *fn = NULL;
 
                                if (tfd != -ENOENT)
                                        return log_error_errno(tfd, "Failed to open target directory '%s': %m", line->target);
 
                                r = path_extract_filename(line->target, &fn);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to extract filename from '%s': %m", line->target);
 
                                r = path_extract_directory(line->target, &dn);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to extract directory from '%s': %m", line->target);
 
                                r = mkdir_p_root_full(root, dn, UID_INVALID, GID_INVALID, 0755, ts, subvolumes);
                                if (r < 0)
                                        return log_error_errno(r, "Failed to create parent directory '%s': %m", dn);
 
                                pfd = chase_and_open(dn, root, CHASE_PREFIX_ROOT, O_RDONLY|O_DIRECTORY|O_CLOEXEC, NULL);
                                if (pfd < 0)
                                        return log_error_errno(pfd, "Failed to open parent directory of target: %m");
 
                                r = copy_tree_at_full(
                                                sfd, ".",
                                                pfd, fn,
                                                uid, gid,
                                                line->flags,
                                                ts,
                                                denylist, subvolumes_by_source_inode,
                                                /* progress_path= */ NULL,
                                                /* progress_bytes= */ NULL,
                                                /* userdata= */ NULL);
                                if (r >= 0) {
                                        /* Creating the target bumped the parent directory's mtime,
                                         * just like in the regular file case below. */
                                        r = clamp_directory_mtime(pfd, ts);
                                        if (r < 0)
                                                return log_error_errno(
                                                        r, "Failed to set timestamp of '%s': %m", dn);
                                }
                        } else
                                r = copy_tree_at_full(
                                                sfd, ".",
                                                tfd, ".",
                                                uid, gid,
                                                line->flags,
                                                ts,
                                                denylist, subvolumes_by_source_inode,
                                                /* progress_path= */ NULL,
                                                /* progress_bytes= */ NULL,
                                                /* userdata= */ NULL);
                        if (r < 0)
                                return log_error_errno(r, "Failed to copy '%s%s' to '%s%s': %m",
                                                       strempty(arg_copy_source), line->source, strempty(root), line->target);
                } else {
                        _cleanup_free_ char *dn = NULL, *fn = NULL;
 
                        /* We are looking at a regular file */
 
                        r = file_is_denylisted(line->source, denylist);
                        if (r < 0)
                                return r;
                        if (r > 0) {
                                log_debug("%s is in the denylist, ignoring", line->source);
                                continue;
                        }
 
                        r = path_extract_filename(line->target, &fn);
                        if (r == -EADDRNOTAVAIL || r == O_DIRECTORY)
                                return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
                                                       "Target path '%s' refers to a directory, but source path '%s' refers to regular file, can't copy.", line->target, line->source);
                        if (r < 0)
                                return log_error_errno(r, "Failed to extract filename from '%s': %m", line->target);