blob: afdf1ea0f5576a1b1e860d922e05417341e3df45 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <stddef.h>
2#include <string.h>
3#include <errno.h>
4/* _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
5 * that.
6 */
7#include <unistd.h>
8#include <byteswap.h>
9#include <sys/time.h>
10#include <sys/param.h>
11#include <sys/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "os.h"
14
15#include "cow.h"
16#include "cow_sys.h"
17
18#define PATH_LEN_V1 256
19
20struct cow_header_v1 {
21 int magic;
22 int version;
23 char backing_file[PATH_LEN_V1];
24 time_t mtime;
25 __u64 size;
26 int sectorsize;
27};
28
29#define PATH_LEN_V2 MAXPATHLEN
30
31struct cow_header_v2 {
32 __u32 magic;
33 __u32 version;
34 char backing_file[PATH_LEN_V2];
35 time_t mtime;
36 __u64 size;
37 int sectorsize;
38};
39
40/* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
41 * case other systems have different values for MAXPATHLEN
42 */
43#define PATH_LEN_V3 4096
44
45/* Changes from V2 -
46 * PATH_LEN_V3 as described above
47 * Explicitly specify field bit lengths for systems with different
48 * lengths for the usual C types. Not sure whether char or
49 * time_t should be changed, this can be changed later without
50 * breaking compatibility
51 * Add alignment field so that different alignments can be used for the
52 * bitmap and data
53 * Add cow_format field to allow for the possibility of different ways
54 * of specifying the COW blocks. For now, the only value is 0,
55 * for the traditional COW bitmap.
56 * Move the backing_file field to the end of the header. This allows
57 * for the possibility of expanding it into the padding required
58 * by the bitmap alignment.
59 * The bitmap and data portions of the file will be aligned as specified
60 * by the alignment field. This is to allow COW files to be
61 * put on devices with restrictions on access alignments, such as
62 * /dev/raw, with a 512 byte alignment restriction. This also
63 * allows the data to be more aligned more strictly than on
64 * sector boundaries. This is needed for ubd-mmap, which needs
65 * the data to be page aligned.
66 * Fixed (finally!) the rounding bug
67 */
68
69struct cow_header_v3 {
70 __u32 magic;
71 __u32 version;
72 __u32 mtime;
73 __u64 size;
74 __u32 sectorsize;
75 __u32 alignment;
76 __u32 cow_format;
77 char backing_file[PATH_LEN_V3];
Paolo 'Blaisorblade' Giarrussocda402b2006-04-10 22:53:29 -070078} __attribute__((packed));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* COW format definitions - for now, we have only the usual COW bitmap */
81#define COW_BITMAP 0
82
83union cow_header {
84 struct cow_header_v1 v1;
85 struct cow_header_v2 v2;
86 struct cow_header_v3 v3;
87};
88
89#define COW_MAGIC 0x4f4f4f4d /* MOOO */
90#define COW_VERSION 3
91
92#define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
93#define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
94
95void cow_sizes(int version, __u64 size, int sectorsize, int align,
96 int bitmap_offset, unsigned long *bitmap_len_out,
97 int *data_offset_out)
98{
99 if(version < 3){
100 *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
101
102 *data_offset_out = bitmap_offset + *bitmap_len_out;
103 *data_offset_out = (*data_offset_out + sectorsize - 1) /
104 sectorsize;
105 *data_offset_out *= sectorsize;
106 }
107 else {
108 *bitmap_len_out = DIV_ROUND(size, sectorsize);
109 *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
110
111 *data_offset_out = bitmap_offset + *bitmap_len_out;
112 *data_offset_out = ROUND_UP(*data_offset_out, align);
113 }
114}
115
116static int absolutize(char *to, int size, char *from)
117{
118 char save_cwd[256], *slash;
119 int remaining;
120
121 if(getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
122 cow_printf("absolutize : unable to get cwd - errno = %d\n",
123 errno);
124 return(-1);
125 }
126 slash = strrchr(from, '/');
127 if(slash != NULL){
128 *slash = '\0';
129 if(chdir(from)){
130 *slash = '/';
131 cow_printf("absolutize : Can't cd to '%s' - "
132 "errno = %d\n", from, errno);
133 return(-1);
134 }
135 *slash = '/';
136 if(getcwd(to, size) == NULL){
137 cow_printf("absolutize : unable to get cwd of '%s' - "
138 "errno = %d\n", from, errno);
139 return(-1);
140 }
141 remaining = size - strlen(to);
142 if(strlen(slash) + 1 > remaining){
143 cow_printf("absolutize : unable to fit '%s' into %d "
144 "chars\n", from, size);
145 return(-1);
146 }
147 strcat(to, slash);
148 }
149 else {
150 if(strlen(save_cwd) + 1 + strlen(from) + 1 > size){
151 cow_printf("absolutize : unable to fit '%s' into %d "
152 "chars\n", from, size);
153 return(-1);
154 }
155 strcpy(to, save_cwd);
156 strcat(to, "/");
157 strcat(to, from);
158 }
159 chdir(save_cwd);
160 return(0);
161}
162
163int write_cow_header(char *cow_file, int fd, char *backing_file,
164 int sectorsize, int alignment, unsigned long long *size)
165{
166 struct cow_header_v3 *header;
167 unsigned long modtime;
168 int err;
169
170 err = cow_seek_file(fd, 0);
171 if(err < 0){
172 cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
173 goto out;
174 }
175
176 err = -ENOMEM;
177 header = cow_malloc(sizeof(*header));
178 if(header == NULL){
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800179 cow_printf("write_cow_header - failed to allocate COW V3 header\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out;
181 }
182 header->magic = htonl(COW_MAGIC);
183 header->version = htonl(COW_VERSION);
184
185 err = -EINVAL;
186 if(strlen(backing_file) > sizeof(header->backing_file) - 1){
187 cow_printf("Backing file name \"%s\" is too long - names are "
188 "limited to %d characters\n", backing_file,
189 sizeof(header->backing_file) - 1);
190 goto out_free;
191 }
192
193 if(absolutize(header->backing_file, sizeof(header->backing_file),
194 backing_file))
195 goto out_free;
196
197 err = os_file_modtime(header->backing_file, &modtime);
198 if(err < 0){
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800199 cow_printf("write_cow_header - backing file '%s' mtime "
200 "request failed, err = %d\n", header->backing_file,
201 -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto out_free;
203 }
204
205 err = cow_file_size(header->backing_file, size);
206 if(err < 0){
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800207 cow_printf("write_cow_header - couldn't get size of "
208 "backing file '%s', err = %d\n",
209 header->backing_file, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out_free;
211 }
212
213 header->mtime = htonl(modtime);
214 header->size = htonll(*size);
215 header->sectorsize = htonl(sectorsize);
216 header->alignment = htonl(alignment);
217 header->cow_format = COW_BITMAP;
218
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800219 err = cow_write_file(fd, header, sizeof(*header));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if(err != sizeof(*header)){
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800221 cow_printf("write_cow_header - write of header to "
222 "new COW file '%s' failed, err = %d\n", cow_file,
223 -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 goto out_free;
225 }
226 err = 0;
227 out_free:
228 cow_free(header);
229 out:
230 return(err);
231}
232
233int file_reader(__u64 offset, char *buf, int len, void *arg)
234{
235 int fd = *((int *) arg);
236
237 return(pread(fd, buf, len, offset));
238}
239
240/* XXX Need to sanity-check the values read from the header */
241
242int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
243 __u32 *version_out, char **backing_file_out,
244 time_t *mtime_out, unsigned long long *size_out,
245 int *sectorsize_out, __u32 *align_out,
246 int *bitmap_offset_out)
247{
248 union cow_header *header;
249 char *file;
250 int err, n;
251 unsigned long version, magic;
252
253 header = cow_malloc(sizeof(*header));
254 if(header == NULL){
255 cow_printf("read_cow_header - Failed to allocate header\n");
256 return(-ENOMEM);
257 }
258 err = -EINVAL;
259 n = (*reader)(0, (char *) header, sizeof(*header), arg);
260 if(n < offsetof(typeof(header->v1), backing_file)){
261 cow_printf("read_cow_header - short header\n");
262 goto out;
263 }
264
265 magic = header->v1.magic;
266 if(magic == COW_MAGIC) {
267 version = header->v1.version;
268 }
269 else if(magic == ntohl(COW_MAGIC)){
270 version = ntohl(header->v1.version);
271 }
272 /* No error printed because the non-COW case comes through here */
273 else goto out;
274
275 *version_out = version;
276
277 if(version == 1){
278 if(n < sizeof(header->v1)){
279 cow_printf("read_cow_header - failed to read V1 "
280 "header\n");
281 goto out;
282 }
283 *mtime_out = header->v1.mtime;
284 *size_out = header->v1.size;
285 *sectorsize_out = header->v1.sectorsize;
286 *bitmap_offset_out = sizeof(header->v1);
287 *align_out = *sectorsize_out;
288 file = header->v1.backing_file;
289 }
290 else if(version == 2){
291 if(n < sizeof(header->v2)){
292 cow_printf("read_cow_header - failed to read V2 "
293 "header\n");
294 goto out;
295 }
296 *mtime_out = ntohl(header->v2.mtime);
297 *size_out = ntohll(header->v2.size);
298 *sectorsize_out = ntohl(header->v2.sectorsize);
299 *bitmap_offset_out = sizeof(header->v2);
300 *align_out = *sectorsize_out;
301 file = header->v2.backing_file;
302 }
303 else if(version == 3){
304 if(n < sizeof(header->v3)){
Paolo 'Blaisorblade' Giarrusso31bc5a32006-02-24 13:03:53 -0800305 cow_printf("read_cow_header - failed to read V3 "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 "header\n");
307 goto out;
308 }
309 *mtime_out = ntohl(header->v3.mtime);
310 *size_out = ntohll(header->v3.size);
311 *sectorsize_out = ntohl(header->v3.sectorsize);
312 *align_out = ntohl(header->v3.alignment);
313 *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
314 file = header->v3.backing_file;
315 }
316 else {
317 cow_printf("read_cow_header - invalid COW version\n");
318 goto out;
319 }
320 err = -ENOMEM;
321 *backing_file_out = cow_strdup(file);
322 if(*backing_file_out == NULL){
323 cow_printf("read_cow_header - failed to allocate backing "
324 "file\n");
325 goto out;
326 }
327 err = 0;
328 out:
329 cow_free(header);
330 return(err);
331}
332
333int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
334 int alignment, int *bitmap_offset_out,
335 unsigned long *bitmap_len_out, int *data_offset_out)
336{
337 unsigned long long size, offset;
338 char zero = 0;
339 int err;
340
341 err = write_cow_header(cow_file, fd, backing_file, sectorsize,
342 alignment, &size);
343 if(err)
344 goto out;
345
346 *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
347 cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
348 bitmap_len_out, data_offset_out);
349
350 offset = *data_offset_out + size - sizeof(zero);
351 err = cow_seek_file(fd, offset);
352 if(err < 0){
353 cow_printf("cow bitmap lseek failed : err = %d\n", -err);
354 goto out;
355 }
356
357 /* does not really matter how much we write it is just to set EOF
358 * this also sets the entire COW bitmap
359 * to zero without having to allocate it
360 */
361 err = cow_write_file(fd, &zero, sizeof(zero));
362 if(err != sizeof(zero)){
363 cow_printf("Write of bitmap to new COW file '%s' failed, "
364 "err = %d\n", cow_file, -err);
Paolo 'Blaisorblade' Giarrussofe1db502006-02-24 13:03:58 -0800365 if (err >= 0)
366 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto out;
368 }
369
370 return(0);
371
372 out:
373 return(err);
374}
375
376/*
377 * ---------------------------------------------------------------------------
378 * Local variables:
379 * c-file-style: "linux"
380 * End:
381 */