64 bit file I/O by default
[qemu] / block-cow.c
1 /*
2  * Block driver for the COW format
3  * 
4  * Copyright (c) 2004 Fabrice Bellard
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #ifndef _WIN32
25 #include "vl.h"
26 #include "block_int.h"
27 #include <sys/mman.h>
28
29 /**************************************************************/
30 /* COW block driver using file system holes */
31
32 /* user mode linux compatible COW file */
33 #define COW_MAGIC 0x4f4f4f4d  /* MOOO */
34 #define COW_VERSION 2
35
36 struct cow_header_v2 {
37     uint32_t magic;
38     uint32_t version;
39     char backing_file[1024];
40     int32_t mtime;
41     uint64_t size;
42     uint32_t sectorsize;
43 };
44
45 typedef struct BDRVCowState {
46     int fd;
47     uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */
48     uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */
49     int cow_bitmap_size;
50     int64_t cow_sectors_offset;
51 } BDRVCowState;
52
53 static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
54 {
55     const struct cow_header_v2 *cow_header = (const void *)buf;
56
57     if (be32_to_cpu(cow_header->magic) == COW_MAGIC &&
58         be32_to_cpu(cow_header->version) == COW_VERSION) 
59         return 100;
60     else
61         return 0;
62 }
63
64 static int cow_open(BlockDriverState *bs, const char *filename)
65 {
66     BDRVCowState *s = bs->opaque;
67     int fd;
68     struct cow_header_v2 cow_header;
69     int64_t size;
70
71     fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
72     if (fd < 0) {
73         fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
74         if (fd < 0)
75             return -1;
76     }
77     s->fd = fd;
78     /* see if it is a cow image */
79     if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) {
80         goto fail;
81     }
82
83     if (be32_to_cpu(cow_header.magic) != COW_MAGIC ||
84         be32_to_cpu(cow_header.version) != COW_VERSION) {
85         goto fail;
86     }
87         
88     /* cow image found */
89     size = be64_to_cpu(cow_header.size);
90     bs->total_sectors = size / 512;
91
92     pstrcpy(bs->backing_file, sizeof(bs->backing_file), 
93             cow_header.backing_file);
94     
95 #if 0
96     if (cow_header.backing_file[0] != '\0') {
97         if (stat(cow_header.backing_file, &st) != 0) {
98             fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file);
99             goto fail;
100         }
101         if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
102             fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file);
103             goto fail;
104             }
105         fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE);
106         if (fd < 0)
107             goto fail;
108         bs->fd = fd;
109     }
110 #endif
111     /* mmap the bitmap */
112     s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
113     s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), 
114                               s->cow_bitmap_size, 
115                               PROT_READ | PROT_WRITE,
116                               MAP_SHARED, s->fd, 0);
117     if (s->cow_bitmap_addr == MAP_FAILED)
118         goto fail;
119     s->cow_bitmap = s->cow_bitmap_addr + sizeof(cow_header);
120     s->cow_sectors_offset = (s->cow_bitmap_size + 511) & ~511;
121     return 0;
122  fail:
123     close(fd);
124     return -1;
125 }
126
127 static inline void set_bit(uint8_t *bitmap, int64_t bitnum)
128 {
129     bitmap[bitnum / 8] |= (1 << (bitnum%8));
130 }
131
132 static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum)
133 {
134     return !!(bitmap[bitnum / 8] & (1 << (bitnum%8)));
135 }
136
137
138 /* Return true if first block has been changed (ie. current version is
139  * in COW file).  Set the number of continuous blocks for which that
140  * is true. */
141 static inline int is_changed(uint8_t *bitmap,
142                              int64_t sector_num, int nb_sectors,
143                              int *num_same)
144 {
145     int changed;
146
147     if (!bitmap || nb_sectors == 0) {
148         *num_same = nb_sectors;
149         return 0;
150     }
151
152     changed = is_bit_set(bitmap, sector_num);
153     for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
154         if (is_bit_set(bitmap, sector_num + *num_same) != changed)
155             break;
156     }
157
158     return changed;
159 }
160
161 static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num, 
162                             int nb_sectors, int *pnum)
163 {
164     BDRVCowState *s = bs->opaque;
165     return is_changed(s->cow_bitmap, sector_num, nb_sectors, pnum);
166 }
167
168 static int cow_read(BlockDriverState *bs, int64_t sector_num, 
169                     uint8_t *buf, int nb_sectors)
170 {
171     BDRVCowState *s = bs->opaque;
172     int ret, n;
173     
174     while (nb_sectors > 0) {
175         if (is_changed(s->cow_bitmap, sector_num, nb_sectors, &n)) {
176             lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
177             ret = read(s->fd, buf, n * 512);
178             if (ret != n * 512) 
179                 return -1;
180         } else {
181             memset(buf, 0, n * 512);
182         }
183         nb_sectors -= n;
184         sector_num += n;
185         buf += n * 512;
186     }
187     return 0;
188 }
189
190 static int cow_write(BlockDriverState *bs, int64_t sector_num, 
191                      const uint8_t *buf, int nb_sectors)
192 {
193     BDRVCowState *s = bs->opaque;
194     int ret, i;
195     
196     lseek(s->fd, s->cow_sectors_offset + sector_num * 512, SEEK_SET);
197     ret = write(s->fd, buf, nb_sectors * 512);
198     if (ret != nb_sectors * 512) 
199         return -1;
200     for (i = 0; i < nb_sectors; i++)
201         set_bit(s->cow_bitmap, sector_num + i);
202     return 0;
203 }
204
205 static int cow_close(BlockDriverState *bs)
206 {
207     BDRVCowState *s = bs->opaque;
208     munmap(s->cow_bitmap_addr, s->cow_bitmap_size);
209     close(s->fd);
210 }
211
212 static int cow_create(const char *filename, int64_t image_sectors,
213                       const char *image_filename, int flags)
214 {
215     int fd, cow_fd;
216     struct cow_header_v2 cow_header;
217     struct stat st;
218
219     if (flags)
220         return -ENOTSUP;
221
222     cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
223               0644);
224     if (cow_fd < 0)
225         return -1;
226     memset(&cow_header, 0, sizeof(cow_header));
227     cow_header.magic = cpu_to_be32(COW_MAGIC);
228     cow_header.version = cpu_to_be32(COW_VERSION);
229     if (image_filename) {
230         fd = open(image_filename, O_RDONLY | O_BINARY);
231         if (fd < 0) {
232             close(cow_fd);
233             return -1;
234         }
235         if (fstat(fd, &st) != 0) {
236             close(fd);
237             return -1;
238         }
239         close(fd);
240         cow_header.mtime = cpu_to_be32(st.st_mtime);
241         realpath(image_filename, cow_header.backing_file);
242     }
243     cow_header.sectorsize = cpu_to_be32(512);
244     cow_header.size = cpu_to_be64(image_sectors * 512);
245     write(cow_fd, &cow_header, sizeof(cow_header));
246     /* resize to include at least all the bitmap */
247     ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3));
248     close(cow_fd);
249     return 0;
250 }
251
252 BlockDriver bdrv_cow = {
253     "cow",
254     sizeof(BDRVCowState),
255     cow_probe,
256     cow_open,
257     cow_read,
258     cow_write,
259     cow_close,
260     cow_create,
261     cow_is_allocated,
262 };
263 #endif