blob: 9cbb426c0b9183e8a5c65afe47483f47e00da0ec [file] [log] [blame]
Jeff Dikecb8fa612007-10-16 01:27:34 -07001/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
3 * Licensed under the GPL
4 */
5
6/*
7 * _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * that.
9 */
10#include <unistd.h>
11#include <byteswap.h>
Jeff Dikecb8fa612007-10-16 01:27:34 -070012#include <errno.h>
13#include <string.h>
14#include <arpa/inet.h>
15#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "cow.h"
17#include "cow_sys.h"
18
19#define PATH_LEN_V1 256
20
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070021typedef __u32 time32_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070023struct cow_header_v1 {
24 __s32 magic;
25 __s32 version;
26 char backing_file[PATH_LEN_V1];
27 time32_t mtime;
28 __u64 size;
29 __s32 sectorsize;
30} __attribute__((packed));
31
Jeff Dikecb8fa612007-10-16 01:27:34 -070032/*
33 * Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070034 * case other systems have different values for MAXPATHLEN.
35 *
36 * The same must hold for V2 - we want file format compatibility, not anything
37 * else.
38 */
39#define PATH_LEN_V3 4096
40#define PATH_LEN_V2 PATH_LEN_V3
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct cow_header_v2 {
43 __u32 magic;
44 __u32 version;
45 char backing_file[PATH_LEN_V2];
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070046 time32_t mtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 __u64 size;
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070048 __s32 sectorsize;
49} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jeff Dikecb8fa612007-10-16 01:27:34 -070051/*
52 * Changes from V2 -
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * 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
59 * bitmap and data
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
74 */
75
Jeff Dikecb8fa612007-10-16 01:27:34 -070076/*
77 * Until Dec2005, __attribute__((packed)) was left out from the below
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070078 * 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).
81 *
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 */
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct cow_header_v3 {
87 __u32 magic;
88 __u32 version;
89 __u32 mtime;
90 __u64 size;
91 __u32 sectorsize;
92 __u32 alignment;
93 __u32 cow_format;
94 char backing_file[PATH_LEN_V3];
Paolo 'Blaisorblade' Giarrussocda402b2006-04-10 22:53:29 -070095} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -070097/* This is the broken layout used by some 64-bit binaries. */
98struct cow_header_v3_broken {
99 __u32 magic;
100 __u32 version;
101 __s64 mtime;
102 __u64 size;
103 __u32 sectorsize;
104 __u32 alignment;
105 __u32 cow_format;
106 char backing_file[PATH_LEN_V3];
Paolo 'Blaisorblade' Giarrussob15fb6b2006-05-01 12:16:01 -0700107};
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* COW format definitions - for now, we have only the usual COW bitmap */
110#define COW_BITMAP 0
111
112union cow_header {
113 struct cow_header_v1 v1;
114 struct cow_header_v2 v2;
115 struct cow_header_v3 v3;
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700116 struct cow_header_v3_broken v3_b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
119#define COW_MAGIC 0x4f4f4f4d /* MOOO */
120#define COW_VERSION 3
121
122#define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
123#define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
124
125void cow_sizes(int version, __u64 size, int sectorsize, int align,
126 int bitmap_offset, unsigned long *bitmap_len_out,
127 int *data_offset_out)
128{
Jeff Dikecb8fa612007-10-16 01:27:34 -0700129 if (version < 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
131
132 *data_offset_out = bitmap_offset + *bitmap_len_out;
133 *data_offset_out = (*data_offset_out + sectorsize - 1) /
134 sectorsize;
135 *data_offset_out *= sectorsize;
136 }
137 else {
138 *bitmap_len_out = DIV_ROUND(size, sectorsize);
139 *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
140
141 *data_offset_out = bitmap_offset + *bitmap_len_out;
142 *data_offset_out = ROUND_UP(*data_offset_out, align);
143 }
144}
145
146static int absolutize(char *to, int size, char *from)
147{
148 char save_cwd[256], *slash;
149 int remaining;
150
Jeff Dikecb8fa612007-10-16 01:27:34 -0700151 if (getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 cow_printf("absolutize : unable to get cwd - errno = %d\n",
153 errno);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700154 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 slash = strrchr(from, '/');
Jeff Dikecb8fa612007-10-16 01:27:34 -0700157 if (slash != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *slash = '\0';
Jeff Dikecb8fa612007-10-16 01:27:34 -0700159 if (chdir(from)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *slash = '/';
161 cow_printf("absolutize : Can't cd to '%s' - "
162 "errno = %d\n", from, errno);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700163 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165 *slash = '/';
Jeff Dikecb8fa612007-10-16 01:27:34 -0700166 if (getcwd(to, size) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 cow_printf("absolutize : unable to get cwd of '%s' - "
168 "errno = %d\n", from, errno);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700169 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 remaining = size - strlen(to);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700172 if (strlen(slash) + 1 > remaining) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 cow_printf("absolutize : unable to fit '%s' into %d "
174 "chars\n", from, size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700175 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 strcat(to, slash);
178 }
179 else {
Jeff Dikecb8fa612007-10-16 01:27:34 -0700180 if (strlen(save_cwd) + 1 + strlen(from) + 1 > size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 cow_printf("absolutize : unable to fit '%s' into %d "
182 "chars\n", from, size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700183 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 strcpy(to, save_cwd);
186 strcat(to, "/");
187 strcat(to, from);
188 }
Vitaliy Ivanov7a99ae72011-07-25 17:12:50 -0700189 if (chdir(save_cwd)) {
190 cow_printf("absolutize : Can't cd to '%s' - "
191 "errno = %d\n", save_cwd, errno);
192 return -1;
193 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197int write_cow_header(char *cow_file, int fd, char *backing_file,
198 int sectorsize, int alignment, unsigned long long *size)
199{
200 struct cow_header_v3 *header;
201 unsigned long modtime;
202 int err;
203
204 err = cow_seek_file(fd, 0);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700205 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
207 goto out;
208 }
209
210 err = -ENOMEM;
211 header = cow_malloc(sizeof(*header));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700212 if (header == NULL) {
213 cow_printf("write_cow_header - failed to allocate COW V3 "
214 "header\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto out;
216 }
217 header->magic = htonl(COW_MAGIC);
218 header->version = htonl(COW_VERSION);
219
220 err = -EINVAL;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700221 if (strlen(backing_file) > sizeof(header->backing_file) - 1) {
Paolo 'Blaisorblade' Giarrusso6dad2d32006-04-10 22:53:31 -0700222 /* Below, %zd is for a size_t value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 cow_printf("Backing file name \"%s\" is too long - names are "
Paolo 'Blaisorblade' Giarrusso6dad2d32006-04-10 22:53:31 -0700224 "limited to %zd characters\n", backing_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 sizeof(header->backing_file) - 1);
226 goto out_free;
227 }
228
Jeff Dikecb8fa612007-10-16 01:27:34 -0700229 if (absolutize(header->backing_file, sizeof(header->backing_file),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 backing_file))
231 goto out_free;
232
233 err = os_file_modtime(header->backing_file, &modtime);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700234 if (err < 0) {
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800235 cow_printf("write_cow_header - backing file '%s' mtime "
236 "request failed, err = %d\n", header->backing_file,
237 -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto out_free;
239 }
240
241 err = cow_file_size(header->backing_file, size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700242 if (err < 0) {
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800243 cow_printf("write_cow_header - couldn't get size of "
244 "backing file '%s', err = %d\n",
245 header->backing_file, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 goto out_free;
247 }
248
249 header->mtime = htonl(modtime);
250 header->size = htonll(*size);
251 header->sectorsize = htonl(sectorsize);
252 header->alignment = htonl(alignment);
253 header->cow_format = COW_BITMAP;
254
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800255 err = cow_write_file(fd, header, sizeof(*header));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700256 if (err != sizeof(*header)) {
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800257 cow_printf("write_cow_header - write of header to "
258 "new COW file '%s' failed, err = %d\n", cow_file,
259 -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out_free;
261 }
262 err = 0;
263 out_free:
264 cow_free(header);
265 out:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269int file_reader(__u64 offset, char *buf, int len, void *arg)
270{
271 int fd = *((int *) arg);
272
Jeff Dikecb8fa612007-10-16 01:27:34 -0700273 return pread(fd, buf, len, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/* XXX Need to sanity-check the values read from the header */
277
278int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
279 __u32 *version_out, char **backing_file_out,
280 time_t *mtime_out, unsigned long long *size_out,
281 int *sectorsize_out, __u32 *align_out,
282 int *bitmap_offset_out)
283{
284 union cow_header *header;
285 char *file;
286 int err, n;
287 unsigned long version, magic;
288
289 header = cow_malloc(sizeof(*header));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700290 if (header == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 cow_printf("read_cow_header - Failed to allocate header\n");
Jeff Dikecb8fa612007-10-16 01:27:34 -0700292 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 err = -EINVAL;
295 n = (*reader)(0, (char *) header, sizeof(*header), arg);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700296 if (n < offsetof(typeof(header->v1), backing_file)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cow_printf("read_cow_header - short header\n");
298 goto out;
299 }
300
301 magic = header->v1.magic;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700302 if (magic == COW_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 version = header->v1.version;
Jeff Dikecb8fa612007-10-16 01:27:34 -0700304 else if (magic == ntohl(COW_MAGIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 version = ntohl(header->v1.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* No error printed because the non-COW case comes through here */
307 else goto out;
308
309 *version_out = version;
310
Jeff Dikecb8fa612007-10-16 01:27:34 -0700311 if (version == 1) {
312 if (n < sizeof(header->v1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 cow_printf("read_cow_header - failed to read V1 "
314 "header\n");
315 goto out;
316 }
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;
323 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700324 else if (version == 2) {
325 if (n < sizeof(header->v2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 cow_printf("read_cow_header - failed to read V2 "
327 "header\n");
328 goto out;
329 }
330 *mtime_out = ntohl(header->v2.mtime);
331 *size_out = ntohll(header->v2.size);
332 *sectorsize_out = ntohl(header->v2.sectorsize);
333 *bitmap_offset_out = sizeof(header->v2);
334 *align_out = *sectorsize_out;
335 file = header->v2.backing_file;
336 }
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700337 /* This is very subtle - see above at union cow_header definition */
Jeff Dikecb8fa612007-10-16 01:27:34 -0700338 else if (version == 3 && (*((int*)header->v3.backing_file) != 0)) {
339 if (n < sizeof(header->v3)) {
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800340 cow_printf("read_cow_header - failed to read V3 "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 "header\n");
342 goto out;
343 }
344 *mtime_out = ntohl(header->v3.mtime);
345 *size_out = ntohll(header->v3.size);
346 *sectorsize_out = ntohl(header->v3.sectorsize);
347 *align_out = ntohl(header->v3.alignment);
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700348 if (*align_out == 0) {
349 cow_printf("read_cow_header - invalid COW header, "
350 "align == 0\n");
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
353 file = header->v3.backing_file;
354 }
Jeff Dikecb8fa612007-10-16 01:27:34 -0700355 else if (version == 3) {
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700356 cow_printf("read_cow_header - broken V3 file with"
357 " 64-bit layout - recovering content.\n");
358
Jeff Dikecb8fa612007-10-16 01:27:34 -0700359 if (n < sizeof(header->v3_b)) {
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700360 cow_printf("read_cow_header - failed to read V3 "
361 "header\n");
362 goto out;
363 }
364
Jeff Dikecb8fa612007-10-16 01:27:34 -0700365 /*
366 * this was used until Dec2005 - 64bits are needed to represent
Paolo 'Blaisorblade' Giarrussof2ea3942006-04-10 22:53:30 -0700367 * 2038+. I.e. we can safely do this truncating cast.
368 *
369 * Additionally, we must use ntohl() instead of ntohll(), since
370 * the program used to use the former (tested - I got mtime
371 * mismatch "0 vs whatever").
372 *
373 * Ever heard about bug-to-bug-compatibility ? ;-) */
374 *mtime_out = (time32_t) ntohl(header->v3_b.mtime);
375
376 *size_out = ntohll(header->v3_b.size);
377 *sectorsize_out = ntohl(header->v3_b.sectorsize);
378 *align_out = ntohl(header->v3_b.alignment);
379 if (*align_out == 0) {
380 cow_printf("read_cow_header - invalid COW header, "
381 "align == 0\n");
382 }
383 *bitmap_offset_out = ROUND_UP(sizeof(header->v3_b), *align_out);
384 file = header->v3_b.backing_file;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 else {
387 cow_printf("read_cow_header - invalid COW version\n");
388 goto out;
389 }
390 err = -ENOMEM;
391 *backing_file_out = cow_strdup(file);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700392 if (*backing_file_out == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 cow_printf("read_cow_header - failed to allocate backing "
394 "file\n");
395 goto out;
396 }
397 err = 0;
398 out:
399 cow_free(header);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
403int 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)
406{
407 unsigned long long size, offset;
408 char zero = 0;
409 int err;
410
411 err = write_cow_header(cow_file, fd, backing_file, sectorsize,
412 alignment, &size);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700413 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto out;
415
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);
419
420 offset = *data_offset_out + size - sizeof(zero);
421 err = cow_seek_file(fd, offset);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700422 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 cow_printf("cow bitmap lseek failed : err = %d\n", -err);
424 goto out;
425 }
426
Jeff Dikecb8fa612007-10-16 01:27:34 -0700427 /*
428 * does not really matter how much we write it is just to set EOF
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * this also sets the entire COW bitmap
430 * to zero without having to allocate it
431 */
432 err = cow_write_file(fd, &zero, sizeof(zero));
Jeff Dikecb8fa612007-10-16 01:27:34 -0700433 if (err != sizeof(zero)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 cow_printf("Write of bitmap to new COW file '%s' failed, "
435 "err = %d\n", cow_file, -err);
Paolo 'Blaisorblade' Giarrussofe1db502006-02-24 13:03:58 -0800436 if (err >= 0)
437 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto out;
439 }
440
Jeff Dikecb8fa612007-10-16 01:27:34 -0700441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 out:
Jeff Dikecb8fa612007-10-16 01:27:34 -0700443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}