9 #include <netinet/in.h>
16 #define PATH_LEN_V1 256
18 struct cow_header_v1
{
21 char backing_file
[PATH_LEN_V1
];
27 #define PATH_LEN_V2 MAXPATHLEN
29 struct cow_header_v2
{
32 char backing_file
[PATH_LEN_V2
];
38 /* Define PATH_LEN_V3 as the usual value of MAXPATHLEN, just hard-code it in
39 * case other systems have different values for MAXPATHLEN
41 #define PATH_LEN_V3 4096
44 * PATH_LEN_V3 as described above
45 * Explicitly specify field bit lengths for systems with different
46 * lengths for the usual C types. Not sure whether char or
47 * time_t should be changed, this can be changed later without
48 * breaking compatibility
49 * Add alignment field so that different alignments can be used for the
51 * Add cow_format field to allow for the possibility of different ways
52 * of specifying the COW blocks. For now, the only value is 0,
53 * for the traditional COW bitmap.
54 * Move the backing_file field to the end of the header. This allows
55 * for the possibility of expanding it into the padding required
56 * by the bitmap alignment.
57 * The bitmap and data portions of the file will be aligned as specified
58 * by the alignment field. This is to allow COW files to be
59 * put on devices with restrictions on access alignments, such as
60 * /dev/raw, with a 512 byte alignment restriction. This also
61 * allows the data to be more aligned more strictly than on
62 * sector boundaries. This is needed for ubd-mmap, which needs
63 * the data to be page aligned.
64 * Fixed (finally!) the rounding bug
67 struct cow_header_v3
{
75 char backing_file
[PATH_LEN_V3
];
78 /* COW format definitions - for now, we have only the usual COW bitmap */
82 struct cow_header_v1 v1
;
83 struct cow_header_v2 v2
;
84 struct cow_header_v3 v3
;
87 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
90 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
91 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
93 void cow_sizes(int version
, __u64 size
, int sectorsize
, int align
,
94 int bitmap_offset
, unsigned long *bitmap_len_out
,
98 *bitmap_len_out
= (size
+ sectorsize
- 1) / (8 * sectorsize
);
100 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
101 *data_offset_out
= (*data_offset_out
+ sectorsize
- 1) /
103 *data_offset_out
*= sectorsize
;
106 *bitmap_len_out
= DIV_ROUND(size
, sectorsize
);
107 *bitmap_len_out
= DIV_ROUND(*bitmap_len_out
, 8);
109 *data_offset_out
= bitmap_offset
+ *bitmap_len_out
;
110 *data_offset_out
= ROUND_UP(*data_offset_out
, align
);
114 static int absolutize(char *to
, int size
, char *from
)
116 char save_cwd
[256], *slash
;
119 if(getcwd(save_cwd
, sizeof(save_cwd
)) == NULL
) {
120 cow_printf("absolutize : unable to get cwd - errno = %d\n",
124 slash
= strrchr(from
, '/');
129 cow_printf("absolutize : Can't cd to '%s' - "
130 "errno = %d\n", from
, errno
);
134 if(getcwd(to
, size
) == NULL
){
135 cow_printf("absolutize : unable to get cwd of '%s' - "
136 "errno = %d\n", from
, errno
);
139 remaining
= size
- strlen(to
);
140 if(strlen(slash
) + 1 > remaining
){
141 cow_printf("absolutize : unable to fit '%s' into %d "
142 "chars\n", from
, size
);
148 if(strlen(save_cwd
) + 1 + strlen(from
) + 1 > size
){
149 cow_printf("absolutize : unable to fit '%s' into %d "
150 "chars\n", from
, size
);
153 strcpy(to
, save_cwd
);
161 int write_cow_header(char *cow_file
, int fd
, char *backing_file
,
162 int sectorsize
, int alignment
, long long *size
)
164 struct cow_header_v3
*header
;
165 unsigned long modtime
;
168 err
= cow_seek_file(fd
, 0);
170 cow_printf("write_cow_header - lseek failed, err = %d\n", -err
);
175 header
= cow_malloc(sizeof(*header
));
177 cow_printf("Failed to allocate COW V3 header\n");
180 header
->magic
= htonl(COW_MAGIC
);
181 header
->version
= htonl(COW_VERSION
);
184 if(strlen(backing_file
) > sizeof(header
->backing_file
) - 1){
185 cow_printf("Backing file name \"%s\" is too long - names are "
186 "limited to %d characters\n", backing_file
,
187 sizeof(header
->backing_file
) - 1);
191 if(absolutize(header
->backing_file
, sizeof(header
->backing_file
),
195 err
= os_file_modtime(header
->backing_file
, &modtime
);
197 cow_printf("Backing file '%s' mtime request failed, "
198 "err = %d\n", header
->backing_file
, -err
);
202 err
= cow_file_size(header
->backing_file
, size
);
204 cow_printf("Couldn't get size of backing file '%s', "
205 "err = %d\n", header
->backing_file
, -err
);
209 header
->mtime
= htonl(modtime
);
210 header
->size
= htonll(*size
);
211 header
->sectorsize
= htonl(sectorsize
);
212 header
->alignment
= htonl(alignment
);
213 header
->cow_format
= COW_BITMAP
;
215 err
= os_write_file(fd
, header
, sizeof(*header
));
216 if(err
!= sizeof(*header
)){
217 cow_printf("Write of header to new COW file '%s' failed, "
218 "err = %d\n", cow_file
, -err
);
228 int file_reader(__u64 offset
, char *buf
, int len
, void *arg
)
230 int fd
= *((int *) arg
);
232 return(pread(fd
, buf
, len
, offset
));
235 /* XXX Need to sanity-check the values read from the header */
237 int read_cow_header(int (*reader
)(__u64
, char *, int, void *), void *arg
,
238 __u32
*version_out
, char **backing_file_out
,
239 time_t *mtime_out
, __u64
*size_out
,
240 int *sectorsize_out
, __u32
*align_out
,
241 int *bitmap_offset_out
)
243 union cow_header
*header
;
246 unsigned long version
, magic
;
248 header
= cow_malloc(sizeof(*header
));
250 cow_printf("read_cow_header - Failed to allocate header\n");
254 n
= (*reader
)(0, (char *) header
, sizeof(*header
), arg
);
255 if(n
< offsetof(typeof(header
->v1
), backing_file
)){
256 cow_printf("read_cow_header - short header\n");
260 magic
= header
->v1
.magic
;
261 if(magic
== COW_MAGIC
) {
262 version
= header
->v1
.version
;
264 else if(magic
== ntohl(COW_MAGIC
)){
265 version
= ntohl(header
->v1
.version
);
267 /* No error printed because the non-COW case comes through here */
270 *version_out
= version
;
273 if(n
< sizeof(header
->v1
)){
274 cow_printf("read_cow_header - failed to read V1 "
278 *mtime_out
= header
->v1
.mtime
;
279 *size_out
= header
->v1
.size
;
280 *sectorsize_out
= header
->v1
.sectorsize
;
281 *bitmap_offset_out
= sizeof(header
->v1
);
282 *align_out
= *sectorsize_out
;
283 file
= header
->v1
.backing_file
;
285 else if(version
== 2){
286 if(n
< sizeof(header
->v2
)){
287 cow_printf("read_cow_header - failed to read V2 "
291 *mtime_out
= ntohl(header
->v2
.mtime
);
292 *size_out
= ntohll(header
->v2
.size
);
293 *sectorsize_out
= ntohl(header
->v2
.sectorsize
);
294 *bitmap_offset_out
= sizeof(header
->v2
);
295 *align_out
= *sectorsize_out
;
296 file
= header
->v2
.backing_file
;
298 else if(version
== 3){
299 if(n
< sizeof(header
->v3
)){
300 cow_printf("read_cow_header - failed to read V2 "
304 *mtime_out
= ntohl(header
->v3
.mtime
);
305 *size_out
= ntohll(header
->v3
.size
);
306 *sectorsize_out
= ntohl(header
->v3
.sectorsize
);
307 *align_out
= ntohl(header
->v3
.alignment
);
308 *bitmap_offset_out
= ROUND_UP(sizeof(header
->v3
), *align_out
);
309 file
= header
->v3
.backing_file
;
312 cow_printf("read_cow_header - invalid COW version\n");
316 *backing_file_out
= cow_strdup(file
);
317 if(*backing_file_out
== NULL
){
318 cow_printf("read_cow_header - failed to allocate backing "
328 int init_cow_file(int fd
, char *cow_file
, char *backing_file
, int sectorsize
,
329 int alignment
, int *bitmap_offset_out
,
330 unsigned long *bitmap_len_out
, int *data_offset_out
)
336 err
= write_cow_header(cow_file
, fd
, backing_file
, sectorsize
,
341 *bitmap_offset_out
= ROUND_UP(sizeof(struct cow_header_v3
), alignment
);
342 cow_sizes(COW_VERSION
, size
, sectorsize
, alignment
, *bitmap_offset_out
,
343 bitmap_len_out
, data_offset_out
);
345 offset
= *data_offset_out
+ size
- sizeof(zero
);
346 err
= cow_seek_file(fd
, offset
);
348 cow_printf("cow bitmap lseek failed : err = %d\n", -err
);
352 /* does not really matter how much we write it is just to set EOF
353 * this also sets the entire COW bitmap
354 * to zero without having to allocate it
356 err
= cow_write_file(fd
, &zero
, sizeof(zero
));
357 if(err
!= sizeof(zero
)){
358 cow_printf("Write of bitmap to new COW file '%s' failed, "
359 "err = %d\n", cow_file
, -err
);
371 * ---------------------------------------------------------------------------
373 * c-file-style: "linux"