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 )
0 commit comments