13 #include <arpa/inet.h>
18 #define PATH_LEN_V1 256
38 #define PATH_LEN_V3 4096
39 #define PATH_LEN_V2 PATH_LEN_V3
118 #define COW_MAGIC 0x4f4f4f4d
119 #define COW_VERSION 3
121 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
122 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
125 int bitmap_offset,
unsigned long *bitmap_len_out,
126 int *data_offset_out)
129 *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
131 *data_offset_out = bitmap_offset + *bitmap_len_out;
132 *data_offset_out = (*data_offset_out + sectorsize - 1) /
137 *bitmap_len_out =
DIV_ROUND(size, sectorsize);
138 *bitmap_len_out =
DIV_ROUND(*bitmap_len_out, 8);
140 *data_offset_out = bitmap_offset + *bitmap_len_out;
141 *data_offset_out =
ROUND_UP(*data_offset_out, align);
145 static int absolutize(
char *to,
int size,
char *
from)
147 char save_cwd[256], *slash;
150 if (getcwd(save_cwd,
sizeof(save_cwd)) ==
NULL) {
151 cow_printf(
"absolutize : unable to get cwd - errno = %d\n",
161 "errno = %d\n", from, errno);
165 if (getcwd(to, size) ==
NULL) {
166 cow_printf(
"absolutize : unable to get cwd of '%s' - "
167 "errno = %d\n", from, errno);
170 remaining = size -
strlen(to);
171 if (
strlen(slash) + 1 > remaining) {
172 cow_printf(
"absolutize : unable to fit '%s' into %d "
173 "chars\n", from, size);
180 cow_printf(
"absolutize : unable to fit '%s' into %d "
181 "chars\n", from, size);
188 if (chdir(save_cwd)) {
190 "errno = %d\n", save_cwd, errno);
200 unsigned long modtime;
203 err = cow_seek_file(fd, 0);
205 cow_printf(
"write_cow_header - lseek failed, err = %d\n", -err);
210 header = cow_malloc(
sizeof(*header));
211 if (header ==
NULL) {
212 cow_printf(
"write_cow_header - failed to allocate COW V3 "
222 cow_printf(
"Backing file name \"%s\" is too long - names are "
223 "limited to %zd characters\n", backing_file,
234 cow_printf(
"write_cow_header - backing file '%s' mtime "
242 cow_printf(
"write_cow_header - couldn't get size of "
243 "backing file '%s', err = %d\n",
248 header->
mtime = htobe32(modtime);
249 header->
size = htobe64(*size);
254 err = cow_write_file(fd, header,
sizeof(*header));
255 if (err !=
sizeof(*header)) {
256 cow_printf(
"write_cow_header - write of header to "
257 "new COW file '%s' failed, err = %d\n", cow_file,
270 int fd = *((
int *) arg);
272 return pread(fd, buf, len, offset);
278 __u32 *version_out,
char **backing_file_out,
279 time_t *mtime_out,
unsigned long long *size_out,
280 int *sectorsize_out,
__u32 *align_out,
281 int *bitmap_offset_out)
288 header = cow_malloc(
sizeof(*header));
289 if (header ==
NULL) {
290 cow_printf(
"read_cow_header - Failed to allocate header\n");
294 n = (*reader)(0, (
char *) header,
sizeof(*header),
arg);
296 cow_printf(
"read_cow_header - short header\n");
300 magic = header->v1.magic;
302 version = header->v1.version;
304 version = be32toh(header->v1.version);
311 if (n <
sizeof(header->v1)) {
312 cow_printf(
"read_cow_header - failed to read V1 "
316 *mtime_out = header->v1.mtime;
317 *size_out = header->v1.size;
318 *sectorsize_out = header->v1.sectorsize;
319 *bitmap_offset_out =
sizeof(header->v1);
320 *align_out = *sectorsize_out;
321 file = header->v1.backing_file;
323 else if (version == 2) {
324 if (n <
sizeof(header->v2)) {
325 cow_printf(
"read_cow_header - failed to read V2 "
329 *mtime_out = be32toh(header->v2.mtime);
330 *size_out = be64toh(header->v2.size);
331 *sectorsize_out = be32toh(header->v2.sectorsize);
332 *bitmap_offset_out =
sizeof(header->v2);
333 *align_out = *sectorsize_out;
334 file = header->v2.backing_file;
337 else if (version == 3 && (*((
int*)header->v3.backing_file) != 0)) {
338 if (n <
sizeof(header->v3)) {
339 cow_printf(
"read_cow_header - failed to read V3 "
343 *mtime_out = be32toh(header->v3.mtime);
344 *size_out = be64toh(header->v3.size);
345 *sectorsize_out = be32toh(header->v3.sectorsize);
346 *align_out = be32toh(header->v3.alignment);
347 if (*align_out == 0) {
348 cow_printf(
"read_cow_header - invalid COW header, "
351 *bitmap_offset_out =
ROUND_UP(
sizeof(header->v3), *align_out);
352 file = header->v3.backing_file;
354 else if (version == 3) {
355 cow_printf(
"read_cow_header - broken V3 file with"
356 " 64-bit layout - recovering content.\n");
358 if (n <
sizeof(header->v3_b)) {
359 cow_printf(
"read_cow_header - failed to read V3 "
373 *mtime_out = (
time32_t) be32toh(header->v3_b.mtime);
375 *size_out = be64toh(header->v3_b.size);
376 *sectorsize_out = be32toh(header->v3_b.sectorsize);
377 *align_out = be32toh(header->v3_b.alignment);
378 if (*align_out == 0) {
379 cow_printf(
"read_cow_header - invalid COW header, "
382 *bitmap_offset_out =
ROUND_UP(
sizeof(header->v3_b), *align_out);
383 file = header->v3_b.backing_file;
386 cow_printf(
"read_cow_header - invalid COW version\n");
390 *backing_file_out = cow_strdup(file);
391 if (*backing_file_out ==
NULL) {
392 cow_printf(
"read_cow_header - failed to allocate backing "
404 unsigned long *bitmap_len_out,
int *data_offset_out)
417 bitmap_len_out, data_offset_out);
419 offset = *data_offset_out + size -
sizeof(
zero);
420 err = cow_seek_file(fd, offset);
422 cow_printf(
"cow bitmap lseek failed : err = %d\n", -err);
431 err = cow_write_file(fd, &zero,
sizeof(zero));
432 if (err !=
sizeof(zero)) {
433 cow_printf(
"Write of bitmap to new COW file '%s' failed, "
434 "err = %d\n", cow_file, -err);