Skip to content

Commit 4d4fe14

Browse files
committed
PR comments: logical changes
1 parent 4ac3526 commit 4d4fe14

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/clonesetup/clonesetup-generator.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ static int generate_clone_units(const char *clone_name, const char *source_dev,
112112

113113
/* symlink unit file to enable it */
114114
dmname = strjoin("dev-mapper-", e, ".device");
115+
if (!dmname)
116+
return log_oom();
117+
115118
r = generator_add_symlink(arg_dest, dmname, "requires", unit);
116119
if (r < 0)
117120
return r;

src/clonesetup/clonesetup-ioctl.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
#include "fd-util.h"
1111
#include "log.h"
1212
#include "sd-device.h"
13-
#include "stdio-util.h" /* xsprintf() */
1413
#include "string-util.h"
1514

15+
/* According to the Linux kernel sysfs ABI, the size of a block device reported in
16+
* /sys/block/<disk>/size is always expressed in units of 512-byte sectors, regardless
17+
* of the physical block size of the device. */
18+
#define SECTOR_SIZE 512
19+
1620
/* Returns the size in bytes of the block device at dev_path.
1721
* Loading the dm-clone table needs the source device size in sectors; sysfs
1822
* reports size in 512-byte sectors. This reads sysfs and returns bytes so the
@@ -124,8 +128,8 @@ int dm_clone_create_device(
124128
const char *dest_dev,
125129
const char *metadata_dev) {
126130

131+
_cleanup_free_ char *target_params = NULL;
127132
uint64_t src_dev_size_sectors, src_dev_size;
128-
char target_params[256];
129133
int r;
130134

131135
assert(name);
@@ -137,13 +141,14 @@ int dm_clone_create_device(
137141
if (r < 0)
138142
return r;
139143

140-
src_dev_size_sectors = src_dev_size / 512;
144+
src_dev_size_sectors = src_dev_size / SECTOR_SIZE;
141145

142146
/* dm-clone target params: <metadata_dev> <dest_dev> <source_dev> <region_size> <hydration_threshold> [options]
143147
* 8 = region size in sectors (4KB regions with 512-byte sectors)
144148
* 1 = hydration threshold (regions to hydrate per batch)
145149
* no_hydration = don't start automatic background hydration */
146-
xsprintf(target_params, "%s %s %s 8 1 no_hydration", metadata_dev, dest_dev, source_dev);
150+
if (asprintf(&target_params, "%s %s %s 8 1 no_hydration", metadata_dev, dest_dev, source_dev) < 0)
151+
return log_oom();
147152

148153
r = dm_clone_create(name);
149154
if (r < 0)

src/clonesetup/clonesetup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static int clone_device(const char *clone_name, const char *source_dev, const ch
3636
if (!clone_dev_path)
3737
return log_oom();
3838

39+
/* Check before calling the DM ioctl to give a cleaner error message;
40+
* DM_DEV_CREATE would return EEXIST too, but with a less obvious message. */
3941
if (access(clone_dev_path, F_OK) >= 0)
4042
return log_error_errno(SYNTHETIC_ERRNO(EEXIST), "Device '%s' already exists.", clone_dev_path);
4143

0 commit comments

Comments
 (0)