4 /* _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
10 #include <sys/param.h>
18 #define PATH_LEN_V1 256
20 typedef __u32 time32_t
;
22 struct cow_header_v1
{
25 char backing_file
[PATH_LEN_V1
];
29 } __attribute__((packed
));
31 /* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
32 * case other systems have different values for MAXPATHLEN.
34 * The same must hold for V2 - we want file format compatibility, not anything
37 #define PATH_LEN_V3 4096
38 #define PATH_LEN_V2 PATH_LEN_V3
40 struct cow_header_v2
{
43 char backing_file
[PATH_LEN_V2
];
47 } __attribute__((packed
));
50 * PATH_LEN_V3 as described above
51 * Explicitly specify field bit lengths for systems with different
52 * lengths for the usual C types. Not sure whether char or
53 * time_t should be changed, this can be changed later without
54 * breaking compatibility
55 * Add alignment field so that different alignments can be used for the
57 * Add cow_format field to allow for the possibility of different ways
58 * of specifying the COW blocks. For now, the only value is 0,
59 * for the traditional COW bitmap.
60 * Move the backing_file field to the end of the header. This allows
61 * for the possibility of expanding it into the padding required
62 * by the bitmap alignment.
63 * The bitmap and data portions of the file will be aligned as specified
64 * by the alignment field. This is to allow COW files to be
65 * put on devices with restrictions on access alignments, such as
66 * /dev/raw, with a 512 byte alignment restriction. This also
67 * allows the data to be more aligned more strictly than on
68 * sector boundaries. This is needed for ubd-mmap, which needs
69 * the data to be page aligned.
70 * Fixed (finally!) the rounding bug
73 /* Until Dec2005, __attribute__((packed)) was left out from the below
74 * definition, leading on 64-bit systems to 4 bytes of padding after mtime, to
75 * align size to 8-byte alignment. This shifted all fields above (no padding
76 * was present on 32-bit, no other padding was added).
78 * However, this _can be detected_: it means that cow_format (always 0 until
79 * now) is shifted onto the first 4 bytes of backing_file, where it is otherwise
80 * impossible to find 4 zeros. -bb */
82 struct cow_header_v3
{
90 char backing_file
[PATH_LEN_V3
];
91 } __attribute__((packed
));
93 /* This is the broken layout used by some 64-bit binaries. */
94 struct cow_header_v3_broken
{
102 char backing_file
[PATH_LEN_V3
];
105 /* COW format definitions - for now, we have only the usual COW bitmap */
109 struct cow_header_v1 v1
;
110 struct cow_header_v2 v2
;
111 struct cow_header_v3 v3
;
112 struct cow_header_v3_broken v3_b
;
115 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
116 #define COW_VERSION 3
118 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
119 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
121 void cow_sizes(int version
, __u64 size
, int sectorsize
, int align
,
122 int bitmap_offset
, unsigned long *bitmap_len_out
,
123 int *data_offset_out
)
126 *bitmap_len_out
= (size
+ sectorsize
- 1) / (8 * sectorsize
);
128 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
129 *data_offset_out
= (*data_offset_out
+ sectorsize
- 1) /
131 *data_offset_out
*= sectorsize
;
134 *bitmap_len_out
= DIV_ROUND(size
, sectorsize
);
135 *bitmap_len_out
= DIV_ROUND(*bitmap_len_out
, 8);
137 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
138 *data_offset_out
= ROUND_UP(*data_offset_out
, align
);
142 static int absolutize(char *to
, int size
, char *from
)
144 char save_cwd
[256], *slash
;
147 if(getcwd(save_cwd
, sizeof(save_cwd
)) == NULL
) {
148 cow_printf("absolutize : unable to get cwd - errno = %d\n",
152 slash
= strrchr(from
, '/');
157 cow_printf("absolutize : Can't cd to '%s' - "
158 "errno = %d\n", from
, errno
);
162 if(getcwd(to
, size
) == NULL
){
163 cow_printf("absolutize : unable to get cwd of '%s' - "
164 "errno = %d\n", from
, errno
);
167 remaining
= size
- strlen(to
);
168 if(strlen(slash
) + 1 > remaining
){
169 cow_printf("absolutize : unable to fit '%s' into %d "
170 "chars\n", from
, size
);
176 if(strlen(save_cwd
) + 1 + strlen(from
) + 1 > size
){
177 cow_printf("absolutize : unable to fit '%s' into %d "
178 "chars\n", from
, size
);
181 strcpy(to
, save_cwd
);
189 int write_cow_header(char *cow_file
, int fd
, char *backing_file
,
190 int sectorsize
, int alignment
, unsigned long long *size
)
192 struct cow_header_v3
*header
;
193 unsigned long modtime
;
196 err
= cow_seek_file(fd
, 0);
198 cow_printf("write_cow_header - lseek failed, err = %d\n", -err
);
203 header
= cow_malloc(sizeof(*header
));
205 cow_printf("write_cow_header - failed to allocate COW V3 header\n");
208 header
->magic
= htonl(COW_MAGIC
);
209 header
->version
= htonl(COW_VERSION
);
212 if(strlen(backing_file
) > sizeof(header
->backing_file
) - 1){
213 cow_printf("Backing file name \"%s\" is too long - names are "
214 "limited to %d characters\n", backing_file
,
215 sizeof(header
->backing_file
) - 1);
219 if(absolutize(header
->backing_file
, sizeof(header
->backing_file
),
223 err
= os_file_modtime(header
->backing_file
, &modtime
);
225 cow_printf("write_cow_header - backing file '%s' mtime "
226 "request failed, err = %d\n", header
->backing_file
,
231 err
= cow_file_size(header
->backing_file
, size
);
233 cow_printf("write_cow_header - couldn't get size of "
234 "backing file '%s', err = %d\n",
235 header
->backing_file
, -err
);
239 header
->mtime
= htonl(modtime
);
240 header
->size
= htonll(*size
);
241 header
->sectorsize
= htonl(sectorsize
);
242 header
->alignment
= htonl(alignment
);
243 header
->cow_format
= COW_BITMAP
;
245 err
= cow_write_file(fd
, header
, sizeof(*header
));
246 if(err
!= sizeof(*header
)){
247 cow_printf("write_cow_header - write of header to "
248 "new COW file '%s' failed, err = %d\n", cow_file
,
259 int file_reader(__u64 offset
, char *buf
, int len
, void *arg
)
261 int fd
= *((int *) arg
);
263 return(pread(fd
, buf
, len
, offset
));
266 /* XXX Need to sanity-check the values read from the header */
268 int read_cow_header(int (*reader
)(__u64
, char *, int, void *), void *arg
,
269 __u32
*version_out
, char **backing_file_out
,
270 time_t *mtime_out
, unsigned long long *size_out
,
271 int *sectorsize_out
, __u32
*align_out
,
272 int *bitmap_offset_out
)
274 union cow_header
*header
;
277 unsigned long version
, magic
;
279 header
= cow_malloc(sizeof(*header
));
281 cow_printf("read_cow_header - Failed to allocate header\n");
285 n
= (*reader
)(0, (char *) header
, sizeof(*header
), arg
);
286 if(n
< offsetof(typeof(header
->v1
), backing_file
)){
287 cow_printf("read_cow_header - short header\n");
291 magic
= header
->v1
.magic
;
292 if(magic
== COW_MAGIC
) {
293 version
= header
->v1
.version
;
295 else if(magic
== ntohl(COW_MAGIC
)){
296 version
= ntohl(header
->v1
.version
);
298 /* No error printed because the non-COW case comes through here */
301 *version_out
= version
;
304 if(n
< sizeof(header
->v1
)){
305 cow_printf("read_cow_header - failed to read V1 "
309 *mtime_out
= header
->v1
.mtime
;
310 *size_out
= header
->v1
.size
;
311 *sectorsize_out
= header
->v1
.sectorsize
;
312 *bitmap_offset_out
= sizeof(header
->v1
);
313 *align_out
= *sectorsize_out
;
314 file
= header
->v1
.backing_file
;
316 else if(version
== 2){
317 if(n
< sizeof(header
->v2
)){
318 cow_printf("read_cow_header - failed to read V2 "
322 *mtime_out
= ntohl(header
->v2
.mtime
);
323 *size_out
= ntohll(header
->v2
.size
);
324 *sectorsize_out
= ntohl(header
->v2
.sectorsize
);
325 *bitmap_offset_out
= sizeof(header
->v2
);
326 *align_out
= *sectorsize_out
;
327 file
= header
->v2
.backing_file
;
329 /* This is very subtle - see above at union cow_header definition */
330 else if(version
== 3 && (*((int*)header
->v3
.backing_file
) != 0)){
331 if(n
< sizeof(header
->v3
)){
332 cow_printf("read_cow_header - failed to read V3 "
336 *mtime_out
= ntohl(header
->v3
.mtime
);
337 *size_out
= ntohll(header
->v3
.size
);
338 *sectorsize_out
= ntohl(header
->v3
.sectorsize
);
339 *align_out
= ntohl(header
->v3
.alignment
);
340 if (*align_out
== 0) {
341 cow_printf("read_cow_header - invalid COW header, "
344 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3
), *align_out
);
345 file
= header
->v3
.backing_file
;
347 else if(version
== 3){
348 cow_printf("read_cow_header - broken V3 file with"
349 " 64-bit layout - recovering content.\n");
351 if(n
< sizeof(header
->v3_b
)){
352 cow_printf("read_cow_header - failed to read V3 "
357 /* this was used until Dec2005 - 64bits are needed to represent
358 * 2038+. I.e. we can safely do this truncating cast.
360 * Additionally, we must use ntohl() instead of ntohll(), since
361 * the program used to use the former (tested - I got mtime
362 * mismatch "0 vs whatever").
364 * Ever heard about bug-to-bug-compatibility ? ;-) */
365 *mtime_out
= (time32_t
) ntohl(header
->v3_b
.mtime
);
367 *size_out
= ntohll(header
->v3_b
.size
);
368 *sectorsize_out
= ntohl(header
->v3_b
.sectorsize
);
369 *align_out
= ntohl(header
->v3_b
.alignment
);
370 if (*align_out
== 0) {
371 cow_printf("read_cow_header - invalid COW header, "
374 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3_b
), *align_out
);
375 file
= header
->v3_b
.backing_file
;
378 cow_printf("read_cow_header - invalid COW version\n");
382 *backing_file_out
= cow_strdup(file
);
383 if(*backing_file_out
== NULL
){
384 cow_printf("read_cow_header - failed to allocate backing "
394 int init_cow_file(int fd
, char *cow_file
, char *backing_file
, int sectorsize
,
395 int alignment
, int *bitmap_offset_out
,
396 unsigned long *bitmap_len_out
, int *data_offset_out
)
398 unsigned long long size
, offset
;
402 err
= write_cow_header(cow_file
, fd
, backing_file
, sectorsize
,
407 *bitmap_offset_out
= ROUND_UP(sizeof(struct cow_header_v3
), alignment
);
408 cow_sizes(COW_VERSION
, size
, sectorsize
, alignment
, *bitmap_offset_out
,
409 bitmap_len_out
, data_offset_out
);
411 offset
= *data_offset_out
+ size
- sizeof(zero
);
412 err
= cow_seek_file(fd
, offset
);
414 cow_printf("cow bitmap lseek failed : err = %d\n", -err
);
418 /* does not really matter how much we write it is just to set EOF
419 * this also sets the entire COW bitmap
420 * to zero without having to allocate it
422 err
= cow_write_file(fd
, &zero
, sizeof(zero
));
423 if(err
!= sizeof(zero
)){
424 cow_printf("Write of bitmap to new COW file '%s' failed, "
425 "err = %d\n", cow_file
, -err
);
438 * ---------------------------------------------------------------------------
440 * c-file-style: "linux"