Statistics
| Branch: | Revision:

root / block-cow.c @ 14ce26e7

History | View | Annotate | Download (7.8 kB)

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