1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
7 * _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
13 #include <arpa/inet.h>
18 #define PATH_LEN_V1 256
20 /* unsigned time_t works until year 2106 */
21 typedef __u32 time32_t
;
23 struct cow_header_v1
{
26 char backing_file
[PATH_LEN_V1
];
30 } __attribute__((packed
));
33 * Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
34 * case other systems have different values for MAXPATHLEN.
36 * The same must hold for V2 - we want file format compatibility, not anything
39 #define PATH_LEN_V3 4096
40 #define PATH_LEN_V2 PATH_LEN_V3
42 struct cow_header_v2
{
45 char backing_file
[PATH_LEN_V2
];
49 } __attribute__((packed
));
53 * PATH_LEN_V3 as described above
54 * Explicitly specify field bit lengths for systems with different
55 * lengths for the usual C types. Not sure whether char or
56 * time_t should be changed, this can be changed later without
57 * breaking compatibility
58 * Add alignment field so that different alignments can be used for the
60 * Add cow_format field to allow for the possibility of different ways
61 * of specifying the COW blocks. For now, the only value is 0,
62 * for the traditional COW bitmap.
63 * Move the backing_file field to the end of the header. This allows
64 * for the possibility of expanding it into the padding required
65 * by the bitmap alignment.
66 * The bitmap and data portions of the file will be aligned as specified
67 * by the alignment field. This is to allow COW files to be
68 * put on devices with restrictions on access alignments, such as
69 * /dev/raw, with a 512 byte alignment restriction. This also
70 * allows the data to be more aligned more strictly than on
71 * sector boundaries. This is needed for ubd-mmap, which needs
72 * the data to be page aligned.
73 * Fixed (finally!) the rounding bug
77 * Until Dec2005, __attribute__((packed)) was left out from the below
78 * definition, leading on 64-bit systems to 4 bytes of padding after mtime, to
79 * align size to 8-byte alignment. This shifted all fields above (no padding
80 * was present on 32-bit, no other padding was added).
82 * However, this _can be detected_: it means that cow_format (always 0 until
83 * now) is shifted onto the first 4 bytes of backing_file, where it is otherwise
84 * impossible to find 4 zeros. -bb */
86 struct cow_header_v3
{
94 char backing_file
[PATH_LEN_V3
];
95 } __attribute__((packed
));
97 /* This is the broken layout used by some 64-bit binaries. */
98 struct cow_header_v3_broken
{
106 char backing_file
[PATH_LEN_V3
];
109 /* COW format definitions - for now, we have only the usual COW bitmap */
113 struct cow_header_v1 v1
;
114 struct cow_header_v2 v2
;
115 struct cow_header_v3 v3
;
116 struct cow_header_v3_broken v3_b
;
119 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
120 #define COW_VERSION 3
122 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
123 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
125 void cow_sizes(int version
, __u64 size
, int sectorsize
, int align
,
126 int bitmap_offset
, unsigned long *bitmap_len_out
,
127 int *data_offset_out
)
130 *bitmap_len_out
= (size
+ sectorsize
- 1) / (8 * sectorsize
);
132 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
133 *data_offset_out
= (*data_offset_out
+ sectorsize
- 1) /
135 *data_offset_out
*= sectorsize
;
138 *bitmap_len_out
= DIV_ROUND(size
, sectorsize
);
139 *bitmap_len_out
= DIV_ROUND(*bitmap_len_out
, 8);
141 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
142 *data_offset_out
= ROUND_UP(*data_offset_out
, align
);
146 static int absolutize(char *to
, int size
, char *from
)
148 char save_cwd
[256], *slash
;
151 if (getcwd(save_cwd
, sizeof(save_cwd
)) == NULL
) {
152 cow_printf("absolutize : unable to get cwd - errno = %d\n",
156 slash
= strrchr(from
, '/');
161 cow_printf("absolutize : Can't cd to '%s' - "
162 "errno = %d\n", from
, errno
);
166 if (getcwd(to
, size
) == NULL
) {
167 cow_printf("absolutize : unable to get cwd of '%s' - "
168 "errno = %d\n", from
, errno
);
171 remaining
= size
- strlen(to
);
172 if (strlen(slash
) + 1 > remaining
) {
173 cow_printf("absolutize : unable to fit '%s' into %d "
174 "chars\n", from
, size
);
180 if (strlen(save_cwd
) + 1 + strlen(from
) + 1 > size
) {
181 cow_printf("absolutize : unable to fit '%s' into %d "
182 "chars\n", from
, size
);
185 strcpy(to
, save_cwd
);
189 if (chdir(save_cwd
)) {
190 cow_printf("absolutize : Can't cd to '%s' - "
191 "errno = %d\n", save_cwd
, errno
);
197 int write_cow_header(char *cow_file
, int fd
, char *backing_file
,
198 int sectorsize
, int alignment
, unsigned long long *size
)
200 struct cow_header_v3
*header
;
204 err
= cow_seek_file(fd
, 0);
206 cow_printf("write_cow_header - lseek failed, err = %d\n", -err
);
211 header
= cow_malloc(sizeof(*header
));
212 if (header
== NULL
) {
213 cow_printf("write_cow_header - failed to allocate COW V3 "
217 header
->magic
= htobe32(COW_MAGIC
);
218 header
->version
= htobe32(COW_VERSION
);
221 if (strlen(backing_file
) > sizeof(header
->backing_file
) - 1) {
222 /* Below, %zd is for a size_t value */
223 cow_printf("Backing file name \"%s\" is too long - names are "
224 "limited to %zd characters\n", backing_file
,
225 sizeof(header
->backing_file
) - 1);
229 if (absolutize(header
->backing_file
, sizeof(header
->backing_file
),
233 err
= os_file_modtime(header
->backing_file
, &modtime
);
235 cow_printf("write_cow_header - backing file '%s' mtime "
236 "request failed, err = %d\n", header
->backing_file
,
241 err
= cow_file_size(header
->backing_file
, size
);
243 cow_printf("write_cow_header - couldn't get size of "
244 "backing file '%s', err = %d\n",
245 header
->backing_file
, -err
);
249 header
->mtime
= htobe32(modtime
);
250 header
->size
= htobe64(*size
);
251 header
->sectorsize
= htobe32(sectorsize
);
252 header
->alignment
= htobe32(alignment
);
253 header
->cow_format
= COW_BITMAP
;
255 err
= cow_write_file(fd
, header
, sizeof(*header
));
256 if (err
!= sizeof(*header
)) {
257 cow_printf("write_cow_header - write of header to "
258 "new COW file '%s' failed, err = %d\n", cow_file
,
269 int file_reader(__u64 offset
, char *buf
, int len
, void *arg
)
271 int fd
= *((int *) arg
);
273 return pread(fd
, buf
, len
, offset
);
276 /* XXX Need to sanity-check the values read from the header */
278 int read_cow_header(int (*reader
)(__u64
, char *, int, void *), void *arg
,
279 __u32
*version_out
, char **backing_file_out
,
280 long long *mtime_out
, unsigned long long *size_out
,
281 int *sectorsize_out
, __u32
*align_out
,
282 int *bitmap_offset_out
)
284 union cow_header
*header
;
287 unsigned long version
, magic
;
289 header
= cow_malloc(sizeof(*header
));
290 if (header
== NULL
) {
291 cow_printf("read_cow_header - Failed to allocate header\n");
295 n
= (*reader
)(0, (char *) header
, sizeof(*header
), arg
);
296 if (n
< offsetof(typeof(header
->v1
), backing_file
)) {
297 cow_printf("read_cow_header - short header\n");
301 magic
= header
->v1
.magic
;
302 if (magic
== COW_MAGIC
)
303 version
= header
->v1
.version
;
304 else if (magic
== be32toh(COW_MAGIC
))
305 version
= be32toh(header
->v1
.version
);
306 /* No error printed because the non-COW case comes through here */
309 *version_out
= version
;
312 if (n
< sizeof(header
->v1
)) {
313 cow_printf("read_cow_header - failed to read V1 "
317 *mtime_out
= header
->v1
.mtime
;
318 *size_out
= header
->v1
.size
;
319 *sectorsize_out
= header
->v1
.sectorsize
;
320 *bitmap_offset_out
= sizeof(header
->v1
);
321 *align_out
= *sectorsize_out
;
322 file
= header
->v1
.backing_file
;
324 else if (version
== 2) {
325 if (n
< sizeof(header
->v2
)) {
326 cow_printf("read_cow_header - failed to read V2 "
330 *mtime_out
= be32toh(header
->v2
.mtime
);
331 *size_out
= be64toh(header
->v2
.size
);
332 *sectorsize_out
= be32toh(header
->v2
.sectorsize
);
333 *bitmap_offset_out
= sizeof(header
->v2
);
334 *align_out
= *sectorsize_out
;
335 file
= header
->v2
.backing_file
;
337 /* This is very subtle - see above at union cow_header definition */
338 else if (version
== 3 && (*((int*)header
->v3
.backing_file
) != 0)) {
339 if (n
< sizeof(header
->v3
)) {
340 cow_printf("read_cow_header - failed to read V3 "
344 *mtime_out
= be32toh(header
->v3
.mtime
);
345 *size_out
= be64toh(header
->v3
.size
);
346 *sectorsize_out
= be32toh(header
->v3
.sectorsize
);
347 *align_out
= be32toh(header
->v3
.alignment
);
348 if (*align_out
== 0) {
349 cow_printf("read_cow_header - invalid COW header, "
352 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3
), *align_out
);
353 file
= header
->v3
.backing_file
;
355 else if (version
== 3) {
356 cow_printf("read_cow_header - broken V3 file with"
357 " 64-bit layout - recovering content.\n");
359 if (n
< sizeof(header
->v3_b
)) {
360 cow_printf("read_cow_header - failed to read V3 "
366 * this was used until Dec2005 - 64bits are needed to represent
367 * 2106+. I.e. we can safely do this truncating cast.
369 * Additionally, we must use be32toh() instead of be64toh(), since
370 * the program used to use the former (tested - I got mtime
371 * mismatch "0 vs whatever").
373 * Ever heard about bug-to-bug-compatibility ? ;-) */
374 *mtime_out
= (time32_t
) be32toh(header
->v3_b
.mtime
);
376 *size_out
= be64toh(header
->v3_b
.size
);
377 *sectorsize_out
= be32toh(header
->v3_b
.sectorsize
);
378 *align_out
= be32toh(header
->v3_b
.alignment
);
379 if (*align_out
== 0) {
380 cow_printf("read_cow_header - invalid COW header, "
383 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3_b
), *align_out
);
384 file
= header
->v3_b
.backing_file
;
387 cow_printf("read_cow_header - invalid COW version\n");
391 *backing_file_out
= cow_strdup(file
);
392 if (*backing_file_out
== NULL
) {
393 cow_printf("read_cow_header - failed to allocate backing "
403 int init_cow_file(int fd
, char *cow_file
, char *backing_file
, int sectorsize
,
404 int alignment
, int *bitmap_offset_out
,
405 unsigned long *bitmap_len_out
, int *data_offset_out
)
407 unsigned long long size
, offset
;
411 err
= write_cow_header(cow_file
, fd
, backing_file
, sectorsize
,
416 *bitmap_offset_out
= ROUND_UP(sizeof(struct cow_header_v3
), alignment
);
417 cow_sizes(COW_VERSION
, size
, sectorsize
, alignment
, *bitmap_offset_out
,
418 bitmap_len_out
, data_offset_out
);
420 offset
= *data_offset_out
+ size
- sizeof(zero
);
421 err
= cow_seek_file(fd
, offset
);
423 cow_printf("cow bitmap lseek failed : err = %d\n", -err
);
428 * does not really matter how much we write it is just to set EOF
429 * this also sets the entire COW bitmap
430 * to zero without having to allocate it
432 err
= cow_write_file(fd
, &zero
, sizeof(zero
));
433 if (err
!= sizeof(zero
)) {
434 cow_printf("Write of bitmap to new COW file '%s' failed, "
435 "err = %d\n", cow_file
, -err
);