1
/* Copyright (c) 2008, XenSource Inc.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
6
* * Redistributions of source code must retain the above copyright
7
* notice, this list of conditions and the following disclaimer.
8
* * Redistributions in binary form must reproduce the above copyright
9
* notice, this list of conditions and the following disclaimer in the
10
* documentation and/or other materials provided with the distribution.
11
* * Neither the name of XenSource Inc. nor the names of its contributors
12
* may be used to endorse or promote products derived from this software
13
* without specific prior written permission.
15
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
__raw_io_write(int fd, char* buf, uint64_t sec, uint32_t secs)
42
off = lseek(fd, vhd_sectors_to_bytes(sec), SEEK_SET);
43
if (off == (off_t)-1) {
44
printf("raw parent: seek(0x%08"PRIx64") failed: %d\n",
45
vhd_sectors_to_bytes(sec), -errno);
49
ret = write(fd, buf, vhd_sectors_to_bytes(secs));
50
if (ret == vhd_sectors_to_bytes(secs))
53
printf("raw parent: write of 0x%"PRIx64" returned %zd, errno: %d\n",
54
vhd_sectors_to_bytes(secs), ret, -errno);
55
return (errno ? -errno : -EIO);
59
* Use 'parent' if the parent is VHD, and 'parent_fd' if the parent is raw
62
vhd_util_coalesce_block(vhd_context_t *vhd, vhd_context_t *parent,
63
int parent_fd, uint64_t block)
71
sec = block * vhd->spb;
73
if (vhd->bat.bat[block] == DD_BLK_UNUSED)
76
err = posix_memalign((void **)&buf, 4096, vhd->header.block_size);
80
err = vhd_io_read(vhd, buf, sec, vhd->spb);
84
if (vhd_has_batmap(vhd) && vhd_batmap_test(vhd, &vhd->batmap, block)) {
86
err = vhd_io_write(parent, buf, sec, vhd->spb);
88
err = __raw_io_write(parent_fd, buf, sec, vhd->spb);
92
err = vhd_read_bitmap(vhd, block, &map);
96
for (i = 0; i < vhd->spb; i++) {
97
if (!vhd_bitmap_test(vhd, map, i))
100
for (secs = 0; i + secs < vhd->spb; secs++)
101
if (!vhd_bitmap_test(vhd, map, i + secs))
105
err = vhd_io_write(parent,
106
buf + vhd_sectors_to_bytes(i),
109
err = __raw_io_write(parent_fd,
110
buf + vhd_sectors_to_bytes(i),
127
vhd_util_coalesce(int argc, char **argv)
132
vhd_context_t vhd, parent;
143
while ((c = getopt(argc, argv, "n:h")) != -1) {
154
if (!name || optind != argc)
157
err = vhd_open(&vhd, name, VHD_OPEN_RDONLY);
159
printf("error opening %s: %d\n", name, err);
163
err = vhd_parent_locator_get(&vhd, &pname);
165
printf("error finding %s parent: %d\n", name, err);
170
if (vhd_parent_raw(&vhd)) {
171
parent_fd = open(pname, O_RDWR | O_DIRECT | O_LARGEFILE, 0644);
172
if (parent_fd == -1) {
174
printf("failed to open parent %s: %d\n", pname, err);
179
err = vhd_open(&parent, pname, VHD_OPEN_RDWR);
181
printf("error opening %s: %d\n", pname, err);
188
err = vhd_get_bat(&vhd);
192
if (vhd_has_batmap(&vhd)) {
193
err = vhd_get_batmap(&vhd);
198
for (i = 0; i < vhd.bat.entries; i++) {
199
err = vhd_util_coalesce_block(&vhd, &parent, parent_fd, i);
216
printf("options: <-n name> [-h help]\n");