Statistics
| Branch: | Revision:

root / block / cow.c @ d5124c00

History | View | Annotate | Download (10.9 kB)

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
#include "qemu-common.h"
25
#include "block/block_int.h"
26
#include "qemu/module.h"
27

    
28
/**************************************************************/
29
/* COW block driver using file system holes */
30

    
31
/* user mode linux compatible COW file */
32
#define COW_MAGIC 0x4f4f4f4d  /* MOOO */
33
#define COW_VERSION 2
34

    
35
struct cow_header_v2 {
36
    uint32_t magic;
37
    uint32_t version;
38
    char backing_file[1024];
39
    int32_t mtime;
40
    uint64_t size;
41
    uint32_t sectorsize;
42
};
43

    
44
typedef struct BDRVCowState {
45
    CoMutex lock;
46
    int64_t cow_sectors_offset;
47
} BDRVCowState;
48

    
49
static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
50
{
51
    const struct cow_header_v2 *cow_header = (const void *)buf;
52

    
53
    if (buf_size >= sizeof(struct cow_header_v2) &&
54
        be32_to_cpu(cow_header->magic) == COW_MAGIC &&
55
        be32_to_cpu(cow_header->version) == COW_VERSION)
56
        return 100;
57
    else
58
        return 0;
59
}
60

    
61
static int cow_open(BlockDriverState *bs, QDict *options, int flags,
62
                    Error **errp)
63
{
64
    BDRVCowState *s = bs->opaque;
65
    struct cow_header_v2 cow_header;
66
    int bitmap_size;
67
    int64_t size;
68
    int ret;
69

    
70
    /* see if it is a cow image */
71
    ret = bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header));
72
    if (ret < 0) {
73
        goto fail;
74
    }
75

    
76
    if (be32_to_cpu(cow_header.magic) != COW_MAGIC) {
77
        ret = -EMEDIUMTYPE;
78
        goto fail;
79
    }
80

    
81
    if (be32_to_cpu(cow_header.version) != COW_VERSION) {
82
        char version[64];
83
        snprintf(version, sizeof(version),
84
               "COW version %d", cow_header.version);
85
        qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
86
            bs->device_name, "cow", version);
87
        ret = -ENOTSUP;
88
        goto fail;
89
    }
90

    
91
    /* cow image found */
92
    size = be64_to_cpu(cow_header.size);
93
    bs->total_sectors = size / 512;
94

    
95
    pstrcpy(bs->backing_file, sizeof(bs->backing_file),
96
            cow_header.backing_file);
97

    
98
    bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
99
    s->cow_sectors_offset = (bitmap_size + 511) & ~511;
100
    qemu_co_mutex_init(&s->lock);
101
    return 0;
102
 fail:
103
    return ret;
104
}
105

    
106
/*
107
 * XXX(hch): right now these functions are extremely inefficient.
108
 * We should just read the whole bitmap we'll need in one go instead.
109
 */
110
static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum, bool *first)
111
{
112
    uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
113
    uint8_t bitmap;
114
    int ret;
115

    
116
    ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
117
    if (ret < 0) {
118
       return ret;
119
    }
120

    
121
    if (bitmap & (1 << (bitnum % 8))) {
122
        return 0;
123
    }
124

    
125
    if (*first) {
126
        ret = bdrv_flush(bs->file);
127
        if (ret < 0) {
128
            return ret;
129
        }
130
        *first = false;
131
    }
132

    
133
    bitmap |= (1 << (bitnum % 8));
134

    
135
    ret = bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap));
136
    if (ret < 0) {
137
       return ret;
138
    }
139
    return 0;
140
}
141

    
142
#define BITS_PER_BITMAP_SECTOR (512 * 8)
143

    
144
/* Cannot use bitmap.c on big-endian machines.  */
145
static int cow_test_bit(int64_t bitnum, const uint8_t *bitmap)
146
{
147
    return (bitmap[bitnum / 8] & (1 << (bitnum & 7))) != 0;
148
}
149

    
150
static int cow_find_streak(const uint8_t *bitmap, int value, int start, int nb_sectors)
151
{
152
    int streak_value = value ? 0xFF : 0;
153
    int last = MIN(start + nb_sectors, BITS_PER_BITMAP_SECTOR);
154
    int bitnum = start;
155
    while (bitnum < last) {
156
        if ((bitnum & 7) == 0 && bitmap[bitnum / 8] == streak_value) {
157
            bitnum += 8;
158
            continue;
159
        }
160
        if (cow_test_bit(bitnum, bitmap) == value) {
161
            bitnum++;
162
            continue;
163
        }
164
        break;
165
    }
166
    return MIN(bitnum, last) - start;
167
}
168

    
169
/* Return true if first block has been changed (ie. current version is
170
 * in COW file).  Set the number of continuous blocks for which that
171
 * is true. */
172
static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
173
        int64_t sector_num, int nb_sectors, int *num_same)
174
{
175
    int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
176
    uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
177
    uint8_t bitmap[BDRV_SECTOR_SIZE];
178
    int ret;
179
    int changed;
180

    
181
    ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
182
    if (ret < 0) {
183
        return ret;
184
    }
185

    
186
    bitnum &= BITS_PER_BITMAP_SECTOR - 1;
187
    changed = cow_test_bit(bitnum, bitmap);
188
    *num_same = cow_find_streak(bitmap, changed, bitnum, nb_sectors);
189
    return changed;
190
}
191

    
192
static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
193
        int64_t sector_num, int nb_sectors, int *num_same)
194
{
195
    BDRVCowState *s = bs->opaque;
196
    int ret = cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
197
    int64_t offset = s->cow_sectors_offset + (sector_num << BDRV_SECTOR_BITS);
198
    if (ret < 0) {
199
        return ret;
200
    }
201
    return (ret ? BDRV_BLOCK_DATA : 0) | offset | BDRV_BLOCK_OFFSET_VALID;
202
}
203

    
204
static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
205
        int nb_sectors)
206
{
207
    int error = 0;
208
    int i;
209
    bool first = true;
210

    
211
    for (i = 0; i < nb_sectors; i++) {
212
        error = cow_set_bit(bs, sector_num + i, &first);
213
        if (error) {
214
            break;
215
        }
216
    }
217

    
218
    return error;
219
}
220

    
221
static int coroutine_fn cow_read(BlockDriverState *bs, int64_t sector_num,
222
                                 uint8_t *buf, int nb_sectors)
223
{
224
    BDRVCowState *s = bs->opaque;
225
    int ret, n;
226

    
227
    while (nb_sectors > 0) {
228
        ret = cow_co_is_allocated(bs, sector_num, nb_sectors, &n);
229
        if (ret < 0) {
230
            return ret;
231
        }
232
        if (ret) {
233
            ret = bdrv_pread(bs->file,
234
                        s->cow_sectors_offset + sector_num * 512,
235
                        buf, n * 512);
236
            if (ret < 0) {
237
                return ret;
238
            }
239
        } else {
240
            if (bs->backing_hd) {
241
                /* read from the base image */
242
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
243
                if (ret < 0) {
244
                    return ret;
245
                }
246
            } else {
247
                memset(buf, 0, n * 512);
248
            }
249
        }
250
        nb_sectors -= n;
251
        sector_num += n;
252
        buf += n * 512;
253
    }
254
    return 0;
255
}
256

    
257
static coroutine_fn int cow_co_read(BlockDriverState *bs, int64_t sector_num,
258
                                    uint8_t *buf, int nb_sectors)
259
{
260
    int ret;
261
    BDRVCowState *s = bs->opaque;
262
    qemu_co_mutex_lock(&s->lock);
263
    ret = cow_read(bs, sector_num, buf, nb_sectors);
264
    qemu_co_mutex_unlock(&s->lock);
265
    return ret;
266
}
267

    
268
static int cow_write(BlockDriverState *bs, int64_t sector_num,
269
                     const uint8_t *buf, int nb_sectors)
270
{
271
    BDRVCowState *s = bs->opaque;
272
    int ret;
273

    
274
    ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
275
                      buf, nb_sectors * 512);
276
    if (ret < 0) {
277
        return ret;
278
    }
279

    
280
    return cow_update_bitmap(bs, sector_num, nb_sectors);
281
}
282

    
283
static coroutine_fn int cow_co_write(BlockDriverState *bs, int64_t sector_num,
284
                                     const uint8_t *buf, int nb_sectors)
285
{
286
    int ret;
287
    BDRVCowState *s = bs->opaque;
288
    qemu_co_mutex_lock(&s->lock);
289
    ret = cow_write(bs, sector_num, buf, nb_sectors);
290
    qemu_co_mutex_unlock(&s->lock);
291
    return ret;
292
}
293

    
294
static void cow_close(BlockDriverState *bs)
295
{
296
}
297

    
298
static int cow_create(const char *filename, QEMUOptionParameter *options,
299
                      Error **errp)
300
{
301
    struct cow_header_v2 cow_header;
302
    struct stat st;
303
    int64_t image_sectors = 0;
304
    const char *image_filename = NULL;
305
    int ret;
306
    BlockDriverState *cow_bs;
307

    
308
    /* Read out options */
309
    while (options && options->name) {
310
        if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
311
            image_sectors = options->value.n / 512;
312
        } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
313
            image_filename = options->value.s;
314
        }
315
        options++;
316
    }
317

    
318
    ret = bdrv_create_file(filename, options);
319
    if (ret < 0) {
320
        return ret;
321
    }
322

    
323
    ret = bdrv_file_open(&cow_bs, filename, NULL, BDRV_O_RDWR);
324
    if (ret < 0) {
325
        return ret;
326
    }
327

    
328
    memset(&cow_header, 0, sizeof(cow_header));
329
    cow_header.magic = cpu_to_be32(COW_MAGIC);
330
    cow_header.version = cpu_to_be32(COW_VERSION);
331
    if (image_filename) {
332
        /* Note: if no file, we put a dummy mtime */
333
        cow_header.mtime = cpu_to_be32(0);
334

    
335
        if (stat(image_filename, &st) != 0) {
336
            goto mtime_fail;
337
        }
338
        cow_header.mtime = cpu_to_be32(st.st_mtime);
339
    mtime_fail:
340
        pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
341
                image_filename);
342
    }
343
    cow_header.sectorsize = cpu_to_be32(512);
344
    cow_header.size = cpu_to_be64(image_sectors * 512);
345
    ret = bdrv_pwrite(cow_bs, 0, &cow_header, sizeof(cow_header));
346
    if (ret < 0) {
347
        goto exit;
348
    }
349

    
350
    /* resize to include at least all the bitmap */
351
    ret = bdrv_truncate(cow_bs,
352
        sizeof(cow_header) + ((image_sectors + 7) >> 3));
353
    if (ret < 0) {
354
        goto exit;
355
    }
356

    
357
exit:
358
    bdrv_unref(cow_bs);
359
    return ret;
360
}
361

    
362
static QEMUOptionParameter cow_create_options[] = {
363
    {
364
        .name = BLOCK_OPT_SIZE,
365
        .type = OPT_SIZE,
366
        .help = "Virtual disk size"
367
    },
368
    {
369
        .name = BLOCK_OPT_BACKING_FILE,
370
        .type = OPT_STRING,
371
        .help = "File name of a base image"
372
    },
373
    { NULL }
374
};
375

    
376
static BlockDriver bdrv_cow = {
377
    .format_name    = "cow",
378
    .instance_size  = sizeof(BDRVCowState),
379

    
380
    .bdrv_probe     = cow_probe,
381
    .bdrv_open      = cow_open,
382
    .bdrv_close     = cow_close,
383
    .bdrv_create    = cow_create,
384
    .bdrv_has_zero_init     = bdrv_has_zero_init_1,
385

    
386
    .bdrv_read              = cow_co_read,
387
    .bdrv_write             = cow_co_write,
388
    .bdrv_co_get_block_status   = cow_co_get_block_status,
389

    
390
    .create_options = cow_create_options,
391
};
392

    
393
static void bdrv_cow_init(void)
394
{
395
    bdrv_register(&bdrv_cow);
396
}
397

    
398
block_init(bdrv_cow_init);