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("write_cow_header - 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("write_cow_header - backing file '%s' mtime "
200 "request failed, err = %d\n", header->backing_file,
205 err = cow_file_size(header->backing_file, size);
207 cow_printf("write_cow_header - couldn't get size of "
208 "backing file '%s', err = %d\n",
209 header->backing_file, -err);
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;
219 err = cow_write_file(fd, header, sizeof(*header));
220 if(err != sizeof(*header)){
221 cow_printf("write_cow_header - write of header to "
222 "new COW file '%s' failed, err = %d\n", cow_file,
233 int file_reader(__u64 offset, char *buf, int len, void *arg)
235 int fd = *((int *) arg);
237 return(pread(fd, buf, len, offset));
240 /* XXX Need to sanity-check the values read from the header */
242 int 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)
248 union cow_header *header;
251 unsigned long version, magic;
253 header = cow_malloc(sizeof(*header));
255 cow_printf("read_cow_header - Failed to allocate header\n");
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");
265 magic = header->v1.magic;
266 if(magic == COW_MAGIC) {
267 version = header->v1.version;
269 else if(magic == ntohl(COW_MAGIC)){
270 version = ntohl(header->v1.version);
272 /* No error printed because the non-COW case comes through here */
275 *version_out = version;
278 if(n < sizeof(header->v1)){
279 cow_printf("read_cow_header - failed to read V1 "
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;
290 else if(version == 2){
291 if(n < sizeof(header->v2)){
292 cow_printf("read_cow_header - failed to read V2 "
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;
303 else if(version == 3){
304 if(n < sizeof(header->v3)){
305 cow_printf("read_cow_header - failed to read V3 "
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;
317 cow_printf("read_cow_header - invalid COW version\n");
321 *backing_file_out = cow_strdup(file);
322 if(*backing_file_out == NULL){
323 cow_printf("read_cow_header - failed to allocate backing "
333 int 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)
337 unsigned long long size, offset;
341 err = write_cow_header(cow_file, fd, backing_file, sectorsize,
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);
350 offset = *data_offset_out + size - sizeof(zero);
351 err = cow_seek_file(fd, offset);
353 cow_printf("cow bitmap lseek failed : err = %d\n", -err);
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
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);
377 * ---------------------------------------------------------------------------
379 * c-file-style: "linux"