Skip to content

Commit 4862024

Browse files
committed
CP-40871: return the number of sectors coalesced from vhd-util
Signed-off-by: Mark Syms <[email protected]>
1 parent 69f0873 commit 4862024

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

vhd/lib/vhd-util-coalesce.c

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,21 @@ __raw_io_write(int fd, char* buf, uint64_t sec, uint32_t secs)
6565
return (errno ? -errno : -EIO);
6666
}
6767

68-
/*
69-
* Use 'parent' if the parent is VHD, and 'parent_fd' if the parent is raw
68+
/**
69+
* Coalesce a VHD allocation block
70+
*
71+
* @param[in] vhd the VHD being coalesced
72+
* @param[in] parent the VHD to coalesce to unless raw
73+
* @param[in] parent_fd raw FD to coalesce to unless VHD parent
74+
* @param[in] parent block the allocation block number to coalese
75+
* @return the number of sectors coalesced or negative errno on failure
7076
*/
71-
static int
77+
static int64_t
7278
vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
7379
int parent_fd, uint64_t block)
7480
{
7581
int i, err;
82+
int64_t coalesced_size = 0;
7683
char *buf;
7784
char *map;
7885
uint64_t sec, secs;
@@ -93,6 +100,10 @@ vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
93100
err = vhd_io_write(parent, buf, sec, vhd->spb);
94101
else
95102
err = __raw_io_write(parent_fd, buf, sec, vhd->spb);
103+
104+
if (err == 0)
105+
coalesced_size = vhd->spb;
106+
96107
goto done;
97108
}
98109

@@ -130,6 +141,7 @@ vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
130141
if (err)
131142
goto done;
132143

144+
coalesced_size += secs;
133145
i += secs;
134146
}
135147

@@ -138,15 +150,27 @@ vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
138150
done:
139151
free(buf);
140152
free(map);
141-
return err;
153+
if (err < 0)
154+
return err;
155+
156+
return coalesced_size;
142157
}
143158

144-
static int
159+
/**
160+
* Coalesce VHD to its immediate parent
161+
*
162+
* @param[in] from the VHD to coalesce
163+
* @param[in] to the VHD to coalesce to or NULL if raw
164+
* @param[in] to_fd the raws file to coalesce to or NULL if to is to be used
165+
* @param[in] progess whether to report progress as the operation is being performed
166+
* @return positive number of sectors coalesced or negative errno in the case of failure
167+
*/
168+
static int64_t
145169
vhd_util_coalesce_onto(vhd_context_t *from,
146170
vhd_context_t *to, int to_fd, int progress)
147171
{
148-
int err;
149-
uint64_t i;
172+
int i, err;
173+
int64_t coalesced_size = 0;
150174

151175
err = vhd_get_bat(from);
152176
if (err)
@@ -165,8 +189,10 @@ vhd_util_coalesce_onto(vhd_context_t *from,
165189
fflush(stdout);
166190
}
167191
err = vhd_util_coalesce_block(from, to, to_fd, i);
168-
if (err)
192+
if (err < 0)
169193
goto out;
194+
195+
coalesced_size += err;
170196
}
171197

172198
err = 0;
@@ -175,10 +201,21 @@ vhd_util_coalesce_onto(vhd_context_t *from,
175201
printf("\r100.00%%\n");
176202

177203
out:
178-
return err;
204+
if (err < 0)
205+
return err;
206+
207+
return coalesced_size;
179208
}
180209

181-
static int
210+
/**
211+
* Coalesce the VHD to its immediate parent
212+
*
213+
* @param[in] name the name (path) of the VHD to coalesce
214+
* @param[in] sparse whether the parent VHD should be written sparsely
215+
* @param[in] progess whether to report progress as the operation is being performed
216+
* @return positive number of sectors coalesced or negative errno in the case of failure
217+
*/
218+
static int64_t
182219
vhd_util_coalesce_parent(const char *name, int sparse, int progress)
183220
{
184221
char *pname;
@@ -243,7 +280,8 @@ int
243280
vhd_util_coalesce(int argc, char **argv)
244281
{
245282
char *name;
246-
int err, c, progress, sparse;
283+
int c, progress, sparse;
284+
int64_t result;
247285

248286
name = NULL;
249287
sparse = 0;
@@ -273,12 +311,16 @@ vhd_util_coalesce(int argc, char **argv)
273311
if (!name || optind != argc)
274312
goto usage;
275313

276-
err = vhd_util_coalesce_parent(name, sparse, progress);
314+
result = vhd_util_coalesce_parent(name, sparse, progress);
277315

278-
if (err)
279-
printf("error coalescing: %d\n", err);
316+
if (result < 0) {
317+
/* -ve errors will be in range for int */
318+
printf("error coalescing: %d\n", (int)result);
319+
return result;
320+
}
280321

281-
return err;
322+
printf("Coalesced %" PRId64 " sectors", result);
323+
return 0;
282324

283325
usage:
284326
printf("options: <-n name> "

0 commit comments

Comments
 (0)