4 /* _XOPEN_SOURCE is needed for pread, but we define _GNU_SOURCE, which defines
10 #include <sys/param.h>
12 #include <netinet/in.h>
19 #define PATH_LEN_V1 256
21 struct cow_header_v1
{
24 char backing_file
[PATH_LEN_V1
];
30 #define PATH_LEN_V2 MAXPATHLEN
32 struct cow_header_v2
{
35 char backing_file
[PATH_LEN_V2
];
41 /* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
42 * case other systems have different values for MAXPATHLEN
44 #define PATH_LEN_V3 4096
47 * PATH_LEN_V3 as described above
48 * Explicitly specify field bit lengths for systems with different
49 * lengths for the usual C types. Not sure whether char or
50 * time_t should be changed, this can be changed later without
51 * breaking compatibility
52 * Add alignment field so that different alignments can be used for the
54 * Add cow_format field to allow for the possibility of different ways
55 * of specifying the COW blocks. For now, the only value is 0,
56 * for the traditional COW bitmap.
57 * Move the backing_file field to the end of the header. This allows
58 * for the possibility of expanding it into the padding required
59 * by the bitmap alignment.
60 * The bitmap and data portions of the file will be aligned as specified
61 * by the alignment field. This is to allow COW files to be
62 * put on devices with restrictions on access alignments, such as
63 * /dev/raw, with a 512 byte alignment restriction. This also
64 * allows the data to be more aligned more strictly than on
65 * sector boundaries. This is needed for ubd-mmap, which needs
66 * the data to be page aligned.
67 * Fixed (finally!) the rounding bug
70 struct cow_header_v3
{
78 char backing_file
[PATH_LEN_V3
];
81 /* COW format definitions - for now, we have only the usual COW bitmap */
85 struct cow_header_v1 v1
;
86 struct cow_header_v2 v2
;
87 struct cow_header_v3 v3
;
90 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
93 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
94 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
96 void cow_sizes(int version
, __u64 size
, int sectorsize
, int align
,
97 int bitmap_offset
, unsigned long *bitmap_len_out
,
101 *bitmap_len_out
= (size
+ sectorsize
- 1) / (8 * sectorsize
);
103 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
104 *data_offset_out
= (*data_offset_out
+ sectorsize
- 1) /
106 *data_offset_out
*= sectorsize
;
109 *bitmap_len_out
= DIV_ROUND(size
, sectorsize
);
110 *bitmap_len_out
= DIV_ROUND(*bitmap_len_out
, 8);
112 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
113 *data_offset_out
= ROUND_UP(*data_offset_out
, align
);
117 static int absolutize(char *to
, int size
, char *from
)
119 char save_cwd
[256], *slash
;
122 if(getcwd(save_cwd
, sizeof(save_cwd
)) == NULL
) {
123 cow_printf("absolutize : unable to get cwd - errno = %d\n",
127 slash
= strrchr(from
, '/');
132 cow_printf("absolutize : Can't cd to '%s' - "
133 "errno = %d\n", from
, errno
);
137 if(getcwd(to
, size
) == NULL
){
138 cow_printf("absolutize : unable to get cwd of '%s' - "
139 "errno = %d\n", from
, errno
);
142 remaining
= size
- strlen(to
);
143 if(strlen(slash
) + 1 > remaining
){
144 cow_printf("absolutize : unable to fit '%s' into %d "
145 "chars\n", from
, size
);
151 if(strlen(save_cwd
) + 1 + strlen(from
) + 1 > size
){
152 cow_printf("absolutize : unable to fit '%s' into %d "
153 "chars\n", from
, size
);
156 strcpy(to
, save_cwd
);
164 int write_cow_header(char *cow_file
, int fd
, char *backing_file
,
165 int sectorsize
, int alignment
, unsigned long long *size
)
167 struct cow_header_v3
*header
;
168 unsigned long modtime
;
171 err
= cow_seek_file(fd
, 0);
173 cow_printf("write_cow_header - lseek failed, err = %d\n", -err
);
178 header
= cow_malloc(sizeof(*header
));
180 cow_printf("Failed to allocate COW V3 header\n");
183 header
->magic
= htonl(COW_MAGIC
);
184 header
->version
= htonl(COW_VERSION
);
187 if(strlen(backing_file
) > sizeof(header
->backing_file
) - 1){
188 cow_printf("Backing file name \"%s\" is too long - names are "
189 "limited to %d characters\n", backing_file
,
190 sizeof(header
->backing_file
) - 1);
194 if(absolutize(header
->backing_file
, sizeof(header
->backing_file
),
198 err
= os_file_modtime(header
->backing_file
, &modtime
);
200 cow_printf("Backing file '%s' mtime request failed, "
201 "err = %d\n", header
->backing_file
, -err
);
205 err
= cow_file_size(header
->backing_file
, size
);
207 cow_printf("Couldn't get size of backing file '%s', "
208 "err = %d\n", header
->backing_file
, -err
);
212 header
->mtime
= htonl(modtime
);
213 header
->size
= htonll(*size
);
214 header
->sectorsize
= htonl(sectorsize
);
215 header
->alignment
= htonl(alignment
);
216 header
->cow_format
= COW_BITMAP
;
218 err
= os_write_file(fd
, header
, sizeof(*header
));
219 if(err
!= sizeof(*header
)){
220 cow_printf("Write of header to new COW file '%s' failed, "
221 "err = %d\n", cow_file
, -err
);
231 int file_reader(__u64 offset
, char *buf
, int len
, void *arg
)
233 int fd
= *((int *) arg
);
235 return(pread(fd
, buf
, len
, offset
));
238 /* XXX Need to sanity-check the values read from the header */
240 int read_cow_header(int (*reader
)(__u64
, char *, int, void *), void *arg
,
241 __u32
*version_out
, char **backing_file_out
,
242 time_t *mtime_out
, unsigned long long *size_out
,
243 int *sectorsize_out
, __u32
*align_out
,
244 int *bitmap_offset_out
)
246 union cow_header
*header
;
249 unsigned long version
, magic
;
251 header
= cow_malloc(sizeof(*header
));
253 cow_printf("read_cow_header - Failed to allocate header\n");
257 n
= (*reader
)(0, (char *) header
, sizeof(*header
), arg
);
258 if(n
< offsetof(typeof(header
->v1
), backing_file
)){
259 cow_printf("read_cow_header - short header\n");
263 magic
= header
->v1
.magic
;
264 if(magic
== COW_MAGIC
) {
265 version
= header
->v1
.version
;
267 else if(magic
== ntohl(COW_MAGIC
)){
268 version
= ntohl(header
->v1
.version
);
270 /* No error printed because the non-COW case comes through here */
273 *version_out
= version
;
276 if(n
< sizeof(header
->v1
)){
277 cow_printf("read_cow_header - failed to read V1 "
281 *mtime_out
= header
->v1
.mtime
;
282 *size_out
= header
->v1
.size
;
283 *sectorsize_out
= header
->v1
.sectorsize
;
284 *bitmap_offset_out
= sizeof(header
->v1
);
285 *align_out
= *sectorsize_out
;
286 file
= header
->v1
.backing_file
;
288 else if(version
== 2){
289 if(n
< sizeof(header
->v2
)){
290 cow_printf("read_cow_header - failed to read V2 "
294 *mtime_out
= ntohl(header
->v2
.mtime
);
295 *size_out
= ntohll(header
->v2
.size
);
296 *sectorsize_out
= ntohl(header
->v2
.sectorsize
);
297 *bitmap_offset_out
= sizeof(header
->v2
);
298 *align_out
= *sectorsize_out
;
299 file
= header
->v2
.backing_file
;
301 else if(version
== 3){
302 if(n
< sizeof(header
->v3
)){
303 cow_printf("read_cow_header - failed to read V2 "
307 *mtime_out
= ntohl(header
->v3
.mtime
);
308 *size_out
= ntohll(header
->v3
.size
);
309 *sectorsize_out
= ntohl(header
->v3
.sectorsize
);
310 *align_out
= ntohl(header
->v3
.alignment
);
311 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3
), *align_out
);
312 file
= header
->v3
.backing_file
;
315 cow_printf("read_cow_header - invalid COW version\n");
319 *backing_file_out
= cow_strdup(file
);
320 if(*backing_file_out
== NULL
){
321 cow_printf("read_cow_header - failed to allocate backing "
331 int init_cow_file(int fd
, char *cow_file
, char *backing_file
, int sectorsize
,
332 int alignment
, int *bitmap_offset_out
,
333 unsigned long *bitmap_len_out
, int *data_offset_out
)
335 unsigned long long size
, offset
;
339 err
= write_cow_header(cow_file
, fd
, backing_file
, sectorsize
,
344 *bitmap_offset_out
= ROUND_UP(sizeof(struct cow_header_v3
), alignment
);
345 cow_sizes(COW_VERSION
, size
, sectorsize
, alignment
, *bitmap_offset_out
,
346 bitmap_len_out
, data_offset_out
);
348 offset
= *data_offset_out
+ size
- sizeof(zero
);
349 err
= cow_seek_file(fd
, offset
);
351 cow_printf("cow bitmap lseek failed : err = %d\n", -err
);
355 /* does not really matter how much we write it is just to set EOF
356 * this also sets the entire COW bitmap
357 * to zero without having to allocate it
359 err
= cow_write_file(fd
, &zero
, sizeof(zero
));
360 if(err
!= sizeof(zero
)){
361 cow_printf("Write of bitmap to new COW file '%s' failed, "
362 "err = %d\n", cow_file
, -err
);
374 * ---------------------------------------------------------------------------
376 * c-file-style: "linux"