|
| 1 | +/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | + |
| 3 | +#include <fcntl.h> |
| 4 | +#include <linux/dm-ioctl.h> |
| 5 | +#include <sys/ioctl.h> |
| 6 | +#include <sys/stat.h> |
| 7 | + |
| 8 | +#include "clone-ioctl.h" |
| 9 | +#include "device-private.h" |
| 10 | +#include "fd-util.h" |
| 11 | +#include "log.h" |
| 12 | +#include "memory-util.h" /* memzero() */ |
| 13 | +#include "sd-device.h" |
| 14 | +#include "stdio-util.h" /* xsprintf() */ |
| 15 | +#include "string-util.h" |
| 16 | + |
| 17 | +static int get_size(const char *dev_path, uint64_t *ret_size) { |
| 18 | + _cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 19 | + uint64_t size; |
| 20 | + int r; |
| 21 | + |
| 22 | + assert(dev_path); |
| 23 | + assert(ret_size); |
| 24 | + |
| 25 | + r = sd_device_new_from_devname(&dev, dev_path); |
| 26 | + if (r < 0) |
| 27 | + return log_error_errno(r, "Failed to create device from '%s': %m", dev_path); |
| 28 | + |
| 29 | + r = device_get_sysattr_u64(dev, "size", &size); |
| 30 | + if (r < 0) |
| 31 | + return log_error_errno(r, "Failed to get device size for '%s': %m", dev_path); |
| 32 | + |
| 33 | + /* sysfs 'size' is in 512-byte sectors */ |
| 34 | + *ret_size = size * 512; |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static int dm_ioctl_run(const char *name, uint32_t cmd, struct dm_ioctl *data, size_t data_size) { |
| 39 | + _cleanup_close_ int fd = -EBADF; |
| 40 | + struct dm_ioctl *dm = data; |
| 41 | + |
| 42 | + assert(name); |
| 43 | + assert(data); |
| 44 | + assert(data_size >= sizeof(struct dm_ioctl)); |
| 45 | + |
| 46 | + dm->version[0] = DM_VERSION_MAJOR; |
| 47 | + dm->version[1] = DM_VERSION_MINOR; |
| 48 | + dm->version[2] = DM_VERSION_PATCHLEVEL; |
| 49 | + dm->data_size = data_size; |
| 50 | + |
| 51 | + assert(strlen(name) < sizeof_field(struct dm_ioctl, name)); |
| 52 | + strncpy_exact(dm->name, name, sizeof(dm->name)); |
| 53 | + |
| 54 | + fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC); |
| 55 | + if (fd < 0) |
| 56 | + return log_error_errno(errno, "Failed to open /dev/mapper/control: %m"); |
| 57 | + |
| 58 | + if (ioctl(fd, cmd, dm) < 0) |
| 59 | + return log_error_errno(errno, "DM ioctl failed: %m"); |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static int dm_clone_create(const char *name) { |
| 65 | + assert(name); |
| 66 | + |
| 67 | + struct dm_ioctl dm = {}; |
| 68 | + return dm_ioctl_run(name, DM_DEV_CREATE, &dm, sizeof(struct dm_ioctl)); |
| 69 | +} |
| 70 | + |
| 71 | +static int dm_clone_load_table(const char *name, uint64_t size_sectors, const char *target_params) { |
| 72 | + char *params_buf; |
| 73 | + size_t params_len, dm_size; |
| 74 | + struct dm_ioctl *dm; |
| 75 | + struct dm_target_spec *tgt; |
| 76 | + |
| 77 | + assert(name); |
| 78 | + assert(target_params); |
| 79 | + |
| 80 | + params_len = strlen(target_params) + 1; |
| 81 | + dm_size = sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec) + params_len; |
| 82 | + dm = alloca0(dm_size); |
| 83 | + |
| 84 | + dm->data_start = sizeof(struct dm_ioctl); |
| 85 | + dm->target_count = 1; |
| 86 | + |
| 87 | + tgt = (struct dm_target_spec *) ((uint8_t *) dm + dm->data_start); |
| 88 | + tgt->sector_start = 0; |
| 89 | + tgt->length = size_sectors; |
| 90 | + strncpy(tgt->target_type, "clone", sizeof(tgt->target_type)); |
| 91 | + tgt->next = 0; |
| 92 | + |
| 93 | + params_buf = (char *) tgt + sizeof(struct dm_target_spec); |
| 94 | + strcpy(params_buf, target_params); |
| 95 | + tgt->status = 0; |
| 96 | + |
| 97 | + return dm_ioctl_run(name, DM_TABLE_LOAD, dm, dm_size); |
| 98 | +} |
| 99 | + |
| 100 | +static int dm_clone_activate(const char *name) { |
| 101 | + assert(name); |
| 102 | + |
| 103 | + struct dm_ioctl dm = {}; |
| 104 | + |
| 105 | + return dm_ioctl_run(name, DM_DEV_SUSPEND, &dm, sizeof(struct dm_ioctl)); |
| 106 | +} |
| 107 | + |
| 108 | +int dm_clone_create_device( |
| 109 | + const char *name, |
| 110 | + const char *source_dev, |
| 111 | + const char *dest_dev, |
| 112 | + const char *metadata_dev) { |
| 113 | + |
| 114 | + uint64_t src_dev_size_sectors, src_dev_size; |
| 115 | + char target_params[256]; |
| 116 | + int r; |
| 117 | + |
| 118 | + assert(name); |
| 119 | + assert(source_dev); |
| 120 | + assert(dest_dev); |
| 121 | + assert(metadata_dev); |
| 122 | + |
| 123 | + r = get_size(source_dev, &src_dev_size); |
| 124 | + if (r < 0) |
| 125 | + return r; |
| 126 | + |
| 127 | + src_dev_size_sectors = src_dev_size / 512; |
| 128 | + |
| 129 | + /* dm-clone target params: <metadata_dev> <dest_dev> <source_dev> <region_size> <hydration_threshold> [options] |
| 130 | + * 8 = region size in sectors (4KB regions with 512-byte sectors) |
| 131 | + * 1 = hydration threshold (regions to hydrate per batch) |
| 132 | + * no_hydration = don't start automatic background hydration */ |
| 133 | + xsprintf(target_params, "%s %s %s 8 1 no_hydration", metadata_dev, dest_dev, source_dev); |
| 134 | + |
| 135 | + r = dm_clone_create(name); |
| 136 | + if (r < 0) |
| 137 | + return r; |
| 138 | + |
| 139 | + r = dm_clone_load_table(name, src_dev_size_sectors, target_params); |
| 140 | + if (r < 0) |
| 141 | + return r; |
| 142 | + |
| 143 | + r = dm_clone_activate(name); |
| 144 | + if (r < 0) |
| 145 | + return r; |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +int dm_clone_send_message(const char *name, const char *message) { |
| 151 | + struct dm_ioctl *dm; |
| 152 | + struct dm_target_msg *msg; |
| 153 | + size_t dm_size, msg_len; |
| 154 | + |
| 155 | + assert(name); |
| 156 | + assert(message); |
| 157 | + |
| 158 | + msg_len = strlen(message) + 1; |
| 159 | + dm_size = sizeof(struct dm_ioctl) + sizeof(struct dm_target_msg) + msg_len; |
| 160 | + dm = alloca0(dm_size); |
| 161 | + |
| 162 | + dm->data_start = sizeof(struct dm_ioctl); |
| 163 | + |
| 164 | + msg = (struct dm_target_msg *) ((char *) dm + dm->data_start); |
| 165 | + strcpy(msg->message, message); |
| 166 | + |
| 167 | + return dm_ioctl_run(name, DM_TARGET_MSG, dm, dm_size); |
| 168 | +} |
0 commit comments