Skip to content

Commit 9ed6968

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

3 files changed

Lines changed: 13 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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
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

1615
/* Returns the size in bytes of the block device at dev_path.
@@ -124,8 +123,8 @@ int dm_clone_create_device(
124123
const char *dest_dev,
125124
const char *metadata_dev) {
126125

126+
_cleanup_free_ char *target_params = NULL;
127127
uint64_t src_dev_size_sectors, src_dev_size;
128-
char target_params[256];
129128
int r;
130129

131130
assert(name);
@@ -137,13 +136,18 @@ int dm_clone_create_device(
137136
if (r < 0)
138137
return r;
139138

140-
src_dev_size_sectors = src_dev_size / 512;
139+
/* According to the Linux kernel sysfs ABI, the size of a block device reported in
140+
* /sys/block/<disk>/size is always expressed in units of 512-byte sectors, regardless
141+
* of the physical block size of the device. */
142+
const uint64_t SECTOR_SIZE = 512;
143+
src_dev_size_sectors = src_dev_size / SECTOR_SIZE;
141144

142145
/* dm-clone target params: <metadata_dev> <dest_dev> <source_dev> <region_size> <hydration_threshold> [options]
143146
* 8 = region size in sectors (4KB regions with 512-byte sectors)
144147
* 1 = hydration threshold (regions to hydrate per batch)
145148
* 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);
149+
if (asprintf(&target_params, "%s %s %s 8 1 no_hydration", metadata_dev, dest_dev, source_dev) < 0)
150+
return log_oom();
147151

148152
r = dm_clone_create(name);
149153
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)