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 struct cow_header_v1 {
23 char backing_file[PATH_LEN_V1];
29 #define PATH_LEN_V2 MAXPATHLEN
31 struct cow_header_v2 {
34 char backing_file[PATH_LEN_V2];
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
43 #define PATH_LEN_V3 4096
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
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
69 struct cow_header_v3 {
77 char backing_file[PATH_LEN_V3];
80 /* COW format definitions - for now, we have only the usual COW bitmap */
84 struct cow_header_v1 v1;
85 struct cow_header_v2 v2;
86 struct cow_header_v3 v3;
89 #define COW_MAGIC 0x4f4f4f4d /* MOOO */
92 #define DIV_ROUND(x, len) (((x) + (len) - 1) / (len))
93 #define ROUND_UP(x, align) DIV_ROUND(x, align) * (align)
95 void cow_sizes(int version, __u64 size, int sectorsize, int align,
96 int bitmap_offset, unsigned long *bitmap_len_out,
100 *bitmap_len_out = (size + sectorsize - 1) / (8 * sectorsize);
102 *data_offset_out = bitmap_offset + *bitmap_len_out;
103 *data_offset_out = (*data_offset_out + sectorsize - 1) /
105 *data_offset_out *= sectorsize;
108 *bitmap_len_out = DIV_ROUND(size, sectorsize);
109 *bitmap_len_out = DIV_ROUND(*bitmap_len_out, 8);
111 *data_offset_out = bitmap_offset + *bitmap_len_out;
112 *data_offset_out = ROUND_UP(*data_offset_out, align);
116 static int absolutize(char *to, int size, char *from)
118 char save_cwd[256], *slash;
121 if(getcwd(save_cwd, sizeof(save_cwd)) == NULL) {
122 cow_printf("absolutize : unable to get cwd - errno = %d\n",
126 slash = strrchr(from, '/');
131 cow_printf("absolutize : Can't cd to '%s' - "
132 "errno = %d\n", from, errno);
136 if(getcwd(to, size) == NULL){
137 cow_printf("absolutize : unable to get cwd of '%s' - "
138 "errno = %d\n", from, errno);
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);
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);
155 strcpy(to, save_cwd);
163 int write_cow_header(char *cow_file, int fd, char *backing_file,
164 int sectorsize, int alignment, unsigned long long *size)
166 struct cow_header_v3 *header;
167 unsigned long modtime;
170 err = cow_seek_file(fd, 0);
172 cow_printf("write_cow_header - lseek failed, err = %d\n", -err);
177 header = cow_malloc(sizeof(*header));
179 cow_printf("Failed to allocate COW V3 header\n");
182 header->magic = htonl(COW_MAGIC);
183 header->version = htonl(COW_VERSION);
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);
193 if(absolutize(header->backing_file, sizeof(header->backing_file),
197 err = os_file_modtime(header->backing_file, &modtime);
199 cow_printf("Backing file '%s' mtime request failed, "
200 "err = %d\n", header->backing_file, -err);
204 err = cow_file_size(header->backing_file, size);
206 cow_printf("Couldn't get size of backing file '%s', "
207 "err = %d\n", header->backing_file, -err);
211 header->mtime = htonl(modtime);
212 header->size = htonll(*size);
213 header->sectorsize = htonl(sectorsize);
214 header->alignment = htonl(alignment);
215 header->cow_format = COW_BITMAP;
217 err = os_write_file(fd, header, sizeof(*header));
218 if(err != sizeof(*header)){
219 cow_printf("Write of header to new COW file '%s' failed, "
220 "err = %d\n", cow_file, -err);
230 int file_reader(__u64 offset, char *buf, int len, void *arg)
232 int fd = *((int *) arg);
234 return(pread(fd, buf, len, offset));
237 /* XXX Need to sanity-check the values read from the header */
239 int read_cow_header(int (*reader)(__u64, char *, int, void *), void *arg,
240 __u32 *version_out, char **backing_file_out,
241 time_t *mtime_out, unsigned long long *size_out,
242 int *sectorsize_out, __u32 *align_out,
243 int *bitmap_offset_out)
245 union cow_header *header;
248 unsigned long version, magic;
250 header = cow_malloc(sizeof(*header));
252 cow_printf("read_cow_header - Failed to allocate header\n");
256 n = (*reader)(0, (char *) header, sizeof(*header), arg);
257 if(n < offsetof(typeof(header->v1), backing_file)){
258 cow_printf("read_cow_header - short header\n");
262 magic = header->v1.magic;
263 if(magic == COW_MAGIC) {
264 version = header->v1.version;
266 else if(magic == ntohl(COW_MAGIC)){
267 version = ntohl(header->v1.version);
269 /* No error printed because the non-COW case comes through here */
272 *version_out = version;
275 if(n < sizeof(header->v1)){
276 cow_printf("read_cow_header - failed to read V1 "
280 *mtime_out = header->v1.mtime;
281 *size_out = header->v1.size;
282 *sectorsize_out = header->v1.sectorsize;
283 *bitmap_offset_out = sizeof(header->v1);
284 *align_out = *sectorsize_out;
285 file = header->v1.backing_file;
287 else if(version == 2){
288 if(n < sizeof(header->v2)){
289 cow_printf("read_cow_header - failed to read V2 "
293 *mtime_out = ntohl(header->v2.mtime);
294 *size_out = ntohll(header->v2.size);
295 *sectorsize_out = ntohl(header->v2.sectorsize);
296 *bitmap_offset_out = sizeof(header->v2);
297 *align_out = *sectorsize_out;
298 file = header->v2.backing_file;
300 else if(version == 3){
301 if(n < sizeof(header->v3)){
302 cow_printf("read_cow_header - failed to read V2 "
306 *mtime_out = ntohl(header->v3.mtime);
307 *size_out = ntohll(header->v3.size);
308 *sectorsize_out = ntohl(header->v3.sectorsize);
309 *align_out = ntohl(header->v3.alignment);
310 *bitmap_offset_out = ROUND_UP(sizeof(header->v3), *align_out);
311 file = header->v3.backing_file;
314 cow_printf("read_cow_header - invalid COW version\n");
318 *backing_file_out = cow_strdup(file);
319 if(*backing_file_out == NULL){
320 cow_printf("read_cow_header - failed to allocate backing "
330 int init_cow_file(int fd, char *cow_file, char *backing_file, int sectorsize,
331 int alignment, int *bitmap_offset_out,
332 unsigned long *bitmap_len_out, int *data_offset_out)
334 unsigned long long size, offset;
338 err = write_cow_header(cow_file, fd, backing_file, sectorsize,
343 *bitmap_offset_out = ROUND_UP(sizeof(struct cow_header_v3), alignment);
344 cow_sizes(COW_VERSION, size, sectorsize, alignment, *bitmap_offset_out,
345 bitmap_len_out, data_offset_out);
347 offset = *data_offset_out + size - sizeof(zero);
348 err = cow_seek_file(fd, offset);
350 cow_printf("cow bitmap lseek failed : err = %d\n", -err);
354 /* does not really matter how much we write it is just to set EOF
355 * this also sets the entire COW bitmap
356 * to zero without having to allocate it
358 err = cow_write_file(fd, &zero, sizeof(zero));
359 if(err != sizeof(zero)){
360 cow_printf("Write of bitmap to new COW file '%s' failed, "
361 "err = %d\n", cow_file, -err);
373 * ---------------------------------------------------------------------------
375 * c-file-style: "linux"