Statistics
| Branch: | Revision:

root / block.c @ 72cf2d4f

History | View | Annotate | Download (50.2 kB)

1
/*
2
 * QEMU System Emulator block driver
3
 *
4
 * Copyright (c) 2003 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 "config-host.h"
25
#include "qemu-common.h"
26
#include "monitor.h"
27
#include "block_int.h"
28
#include "module.h"
29

    
30
#ifdef CONFIG_BSD
31
#include <sys/types.h>
32
#include <sys/stat.h>
33
#include <sys/ioctl.h>
34
#include <sys/queue.h>
35
#ifndef __DragonFly__
36
#include <sys/disk.h>
37
#endif
38
#endif
39

    
40
#ifdef _WIN32
41
#include <windows.h>
42
#endif
43

    
44
#define SECTOR_BITS 9
45
#define SECTOR_SIZE (1 << SECTOR_BITS)
46

    
47
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
48
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
49
        BlockDriverCompletionFunc *cb, void *opaque);
50
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
51
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
52
        BlockDriverCompletionFunc *cb, void *opaque);
53
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
54
        BlockDriverCompletionFunc *cb, void *opaque);
55
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
56
                        uint8_t *buf, int nb_sectors);
57
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58
                         const uint8_t *buf, int nb_sectors);
59

    
60
BlockDriverState *bdrv_first;
61

    
62
static BlockDriver *first_drv;
63

    
64
int path_is_absolute(const char *path)
65
{
66
    const char *p;
67
#ifdef _WIN32
68
    /* specific case for names like: "\\.\d:" */
69
    if (*path == '/' || *path == '\\')
70
        return 1;
71
#endif
72
    p = strchr(path, ':');
73
    if (p)
74
        p++;
75
    else
76
        p = path;
77
#ifdef _WIN32
78
    return (*p == '/' || *p == '\\');
79
#else
80
    return (*p == '/');
81
#endif
82
}
83

    
84
/* if filename is absolute, just copy it to dest. Otherwise, build a
85
   path to it by considering it is relative to base_path. URL are
86
   supported. */
87
void path_combine(char *dest, int dest_size,
88
                  const char *base_path,
89
                  const char *filename)
90
{
91
    const char *p, *p1;
92
    int len;
93

    
94
    if (dest_size <= 0)
95
        return;
96
    if (path_is_absolute(filename)) {
97
        pstrcpy(dest, dest_size, filename);
98
    } else {
99
        p = strchr(base_path, ':');
100
        if (p)
101
            p++;
102
        else
103
            p = base_path;
104
        p1 = strrchr(base_path, '/');
105
#ifdef _WIN32
106
        {
107
            const char *p2;
108
            p2 = strrchr(base_path, '\\');
109
            if (!p1 || p2 > p1)
110
                p1 = p2;
111
        }
112
#endif
113
        if (p1)
114
            p1++;
115
        else
116
            p1 = base_path;
117
        if (p1 > p)
118
            p = p1;
119
        len = p - base_path;
120
        if (len > dest_size - 1)
121
            len = dest_size - 1;
122
        memcpy(dest, base_path, len);
123
        dest[len] = '\0';
124
        pstrcat(dest, dest_size, filename);
125
    }
126
}
127

    
128
void bdrv_register(BlockDriver *bdrv)
129
{
130
    if (!bdrv->bdrv_aio_readv) {
131
        /* add AIO emulation layer */
132
        bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
133
        bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
134
    } else if (!bdrv->bdrv_read) {
135
        /* add synchronous IO emulation layer */
136
        bdrv->bdrv_read = bdrv_read_em;
137
        bdrv->bdrv_write = bdrv_write_em;
138
    }
139

    
140
    if (!bdrv->bdrv_aio_flush)
141
        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
142

    
143
    bdrv->next = first_drv;
144
    first_drv = bdrv;
145
}
146

    
147
/* create a new block device (by default it is empty) */
148
BlockDriverState *bdrv_new(const char *device_name)
149
{
150
    BlockDriverState **pbs, *bs;
151

    
152
    bs = qemu_mallocz(sizeof(BlockDriverState));
153
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
154
    if (device_name[0] != '\0') {
155
        /* insert at the end */
156
        pbs = &bdrv_first;
157
        while (*pbs != NULL)
158
            pbs = &(*pbs)->next;
159
        *pbs = bs;
160
    }
161
    return bs;
162
}
163

    
164
BlockDriver *bdrv_find_format(const char *format_name)
165
{
166
    BlockDriver *drv1;
167
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
168
        if (!strcmp(drv1->format_name, format_name))
169
            return drv1;
170
    }
171
    return NULL;
172
}
173

    
174
int bdrv_create(BlockDriver *drv, const char* filename,
175
    QEMUOptionParameter *options)
176
{
177
    if (!drv->bdrv_create)
178
        return -ENOTSUP;
179

    
180
    return drv->bdrv_create(filename, options);
181
}
182

    
183
#ifdef _WIN32
184
void get_tmp_filename(char *filename, int size)
185
{
186
    char temp_dir[MAX_PATH];
187

    
188
    GetTempPath(MAX_PATH, temp_dir);
189
    GetTempFileName(temp_dir, "qem", 0, filename);
190
}
191
#else
192
void get_tmp_filename(char *filename, int size)
193
{
194
    int fd;
195
    const char *tmpdir;
196
    /* XXX: race condition possible */
197
    tmpdir = getenv("TMPDIR");
198
    if (!tmpdir)
199
        tmpdir = "/tmp";
200
    snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
201
    fd = mkstemp(filename);
202
    close(fd);
203
}
204
#endif
205

    
206
#ifdef _WIN32
207
static int is_windows_drive_prefix(const char *filename)
208
{
209
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
210
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
211
            filename[1] == ':');
212
}
213

    
214
int is_windows_drive(const char *filename)
215
{
216
    if (is_windows_drive_prefix(filename) &&
217
        filename[2] == '\0')
218
        return 1;
219
    if (strstart(filename, "\\\\.\\", NULL) ||
220
        strstart(filename, "//./", NULL))
221
        return 1;
222
    return 0;
223
}
224
#endif
225

    
226
static BlockDriver *find_protocol(const char *filename)
227
{
228
    BlockDriver *drv1;
229
    char protocol[128];
230
    int len;
231
    const char *p;
232

    
233
#ifdef _WIN32
234
    if (is_windows_drive(filename) ||
235
        is_windows_drive_prefix(filename))
236
        return bdrv_find_format("raw");
237
#endif
238
    p = strchr(filename, ':');
239
    if (!p)
240
        return bdrv_find_format("raw");
241
    len = p - filename;
242
    if (len > sizeof(protocol) - 1)
243
        len = sizeof(protocol) - 1;
244
    memcpy(protocol, filename, len);
245
    protocol[len] = '\0';
246
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
247
        if (drv1->protocol_name &&
248
            !strcmp(drv1->protocol_name, protocol))
249
            return drv1;
250
    }
251
    return NULL;
252
}
253

    
254
/*
255
 * Detect host devices. By convention, /dev/cdrom[N] is always
256
 * recognized as a host CDROM.
257
 */
258
static BlockDriver *find_hdev_driver(const char *filename)
259
{
260
    int score_max = 0, score;
261
    BlockDriver *drv = NULL, *d;
262

    
263
    for (d = first_drv; d; d = d->next) {
264
        if (d->bdrv_probe_device) {
265
            score = d->bdrv_probe_device(filename);
266
            if (score > score_max) {
267
                score_max = score;
268
                drv = d;
269
            }
270
        }
271
    }
272

    
273
    return drv;
274
}
275

    
276
static BlockDriver *find_image_format(const char *filename)
277
{
278
    int ret, score, score_max;
279
    BlockDriver *drv1, *drv;
280
    uint8_t buf[2048];
281
    BlockDriverState *bs;
282

    
283
    drv = find_protocol(filename);
284
    /* no need to test disk image formats for vvfat */
285
    if (drv && strcmp(drv->format_name, "vvfat") == 0)
286
        return drv;
287

    
288
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
289
    if (ret < 0)
290
        return NULL;
291
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
292
    bdrv_delete(bs);
293
    if (ret < 0) {
294
        return NULL;
295
    }
296

    
297
    score_max = 0;
298
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
299
        if (drv1->bdrv_probe) {
300
            score = drv1->bdrv_probe(buf, ret, filename);
301
            if (score > score_max) {
302
                score_max = score;
303
                drv = drv1;
304
            }
305
        }
306
    }
307
    return drv;
308
}
309

    
310
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
311
{
312
    BlockDriverState *bs;
313
    int ret;
314

    
315
    bs = bdrv_new("");
316
    ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
317
    if (ret < 0) {
318
        bdrv_delete(bs);
319
        return ret;
320
    }
321
    bs->growable = 1;
322
    *pbs = bs;
323
    return 0;
324
}
325

    
326
int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
327
{
328
    return bdrv_open2(bs, filename, flags, NULL);
329
}
330

    
331
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
332
               BlockDriver *drv)
333
{
334
    int ret, open_flags;
335
    char tmp_filename[PATH_MAX];
336
    char backing_filename[PATH_MAX];
337

    
338
    bs->read_only = 0;
339
    bs->is_temporary = 0;
340
    bs->encrypted = 0;
341
    bs->valid_key = 0;
342
    /* buffer_alignment defaulted to 512, drivers can change this value */
343
    bs->buffer_alignment = 512;
344

    
345
    if (flags & BDRV_O_SNAPSHOT) {
346
        BlockDriverState *bs1;
347
        int64_t total_size;
348
        int is_protocol = 0;
349
        BlockDriver *bdrv_qcow2;
350
        QEMUOptionParameter *options;
351

    
352
        /* if snapshot, we create a temporary backing file and open it
353
           instead of opening 'filename' directly */
354

    
355
        /* if there is a backing file, use it */
356
        bs1 = bdrv_new("");
357
        ret = bdrv_open2(bs1, filename, 0, drv);
358
        if (ret < 0) {
359
            bdrv_delete(bs1);
360
            return ret;
361
        }
362
        total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
363

    
364
        if (bs1->drv && bs1->drv->protocol_name)
365
            is_protocol = 1;
366

    
367
        bdrv_delete(bs1);
368

    
369
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
370

    
371
        /* Real path is meaningless for protocols */
372
        if (is_protocol)
373
            snprintf(backing_filename, sizeof(backing_filename),
374
                     "%s", filename);
375
        else
376
            realpath(filename, backing_filename);
377

    
378
        bdrv_qcow2 = bdrv_find_format("qcow2");
379
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
380

    
381
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
382
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
383
        if (drv) {
384
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
385
                drv->format_name);
386
        }
387

    
388
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
389
        if (ret < 0) {
390
            return ret;
391
        }
392

    
393
        filename = tmp_filename;
394
        drv = bdrv_qcow2;
395
        bs->is_temporary = 1;
396
    }
397

    
398
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
399
    if (flags & BDRV_O_FILE) {
400
        drv = find_protocol(filename);
401
    } else if (!drv) {
402
        drv = find_hdev_driver(filename);
403
        if (!drv) {
404
            drv = find_image_format(filename);
405
        }
406
    }
407
    if (!drv) {
408
        ret = -ENOENT;
409
        goto unlink_and_fail;
410
    }
411
    bs->drv = drv;
412
    bs->opaque = qemu_mallocz(drv->instance_size);
413

    
414
    /*
415
     * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
416
     * write cache to the guest.  We do need the fdatasync to flush
417
     * out transactions for block allocations, and we maybe have a
418
     * volatile write cache in our backing device to deal with.
419
     */
420
    if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
421
        bs->enable_write_cache = 1;
422

    
423
    /* Note: for compatibility, we open disk image files as RDWR, and
424
       RDONLY as fallback */
425
    if (!(flags & BDRV_O_FILE))
426
        open_flags = BDRV_O_RDWR |
427
                (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
428
    else
429
        open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
430
    ret = drv->bdrv_open(bs, filename, open_flags);
431
    if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
432
        ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
433
        bs->read_only = 1;
434
    }
435
    if (ret < 0) {
436
        qemu_free(bs->opaque);
437
        bs->opaque = NULL;
438
        bs->drv = NULL;
439
    unlink_and_fail:
440
        if (bs->is_temporary)
441
            unlink(filename);
442
        return ret;
443
    }
444
    if (drv->bdrv_getlength) {
445
        bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
446
    }
447
#ifndef _WIN32
448
    if (bs->is_temporary) {
449
        unlink(filename);
450
    }
451
#endif
452
    if (bs->backing_file[0] != '\0') {
453
        /* if there is a backing file, use it */
454
        BlockDriver *back_drv = NULL;
455
        bs->backing_hd = bdrv_new("");
456
        path_combine(backing_filename, sizeof(backing_filename),
457
                     filename, bs->backing_file);
458
        if (bs->backing_format[0] != '\0')
459
            back_drv = bdrv_find_format(bs->backing_format);
460
        ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
461
                         back_drv);
462
        if (ret < 0) {
463
            bdrv_close(bs);
464
            return ret;
465
        }
466
    }
467

    
468
    if (!bdrv_key_required(bs)) {
469
        /* call the change callback */
470
        bs->media_changed = 1;
471
        if (bs->change_cb)
472
            bs->change_cb(bs->change_opaque);
473
    }
474
    return 0;
475
}
476

    
477
void bdrv_close(BlockDriverState *bs)
478
{
479
    if (bs->drv) {
480
        if (bs->backing_hd)
481
            bdrv_delete(bs->backing_hd);
482
        bs->drv->bdrv_close(bs);
483
        qemu_free(bs->opaque);
484
#ifdef _WIN32
485
        if (bs->is_temporary) {
486
            unlink(bs->filename);
487
        }
488
#endif
489
        bs->opaque = NULL;
490
        bs->drv = NULL;
491

    
492
        /* call the change callback */
493
        bs->media_changed = 1;
494
        if (bs->change_cb)
495
            bs->change_cb(bs->change_opaque);
496
    }
497
}
498

    
499
void bdrv_delete(BlockDriverState *bs)
500
{
501
    BlockDriverState **pbs;
502

    
503
    pbs = &bdrv_first;
504
    while (*pbs != bs && *pbs != NULL)
505
        pbs = &(*pbs)->next;
506
    if (*pbs == bs)
507
        *pbs = bs->next;
508

    
509
    bdrv_close(bs);
510
    qemu_free(bs);
511
}
512

    
513
/*
514
 * Run consistency checks on an image
515
 *
516
 * Returns the number of errors or -errno when an internal error occurs
517
 */
518
int bdrv_check(BlockDriverState *bs)
519
{
520
    if (bs->drv->bdrv_check == NULL) {
521
        return -ENOTSUP;
522
    }
523

    
524
    return bs->drv->bdrv_check(bs);
525
}
526

    
527
/* commit COW file into the raw image */
528
int bdrv_commit(BlockDriverState *bs)
529
{
530
    BlockDriver *drv = bs->drv;
531
    int64_t i, total_sectors;
532
    int n, j;
533
    unsigned char sector[512];
534

    
535
    if (!drv)
536
        return -ENOMEDIUM;
537

    
538
    if (bs->read_only) {
539
        return -EACCES;
540
    }
541

    
542
    if (!bs->backing_hd) {
543
        return -ENOTSUP;
544
    }
545

    
546
    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
547
    for (i = 0; i < total_sectors;) {
548
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
549
            for(j = 0; j < n; j++) {
550
                if (bdrv_read(bs, i, sector, 1) != 0) {
551
                    return -EIO;
552
                }
553

    
554
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
555
                    return -EIO;
556
                }
557
                i++;
558
            }
559
        } else {
560
            i += n;
561
        }
562
    }
563

    
564
    if (drv->bdrv_make_empty)
565
        return drv->bdrv_make_empty(bs);
566

    
567
    return 0;
568
}
569

    
570
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
571
                                   size_t size)
572
{
573
    int64_t len;
574

    
575
    if (!bdrv_is_inserted(bs))
576
        return -ENOMEDIUM;
577

    
578
    if (bs->growable)
579
        return 0;
580

    
581
    len = bdrv_getlength(bs);
582

    
583
    if (offset < 0)
584
        return -EIO;
585

    
586
    if ((offset > len) || (len - offset < size))
587
        return -EIO;
588

    
589
    return 0;
590
}
591

    
592
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
593
                              int nb_sectors)
594
{
595
    return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
596
}
597

    
598
/* return < 0 if error. See bdrv_write() for the return codes */
599
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
600
              uint8_t *buf, int nb_sectors)
601
{
602
    BlockDriver *drv = bs->drv;
603

    
604
    if (!drv)
605
        return -ENOMEDIUM;
606
    if (bdrv_check_request(bs, sector_num, nb_sectors))
607
        return -EIO;
608

    
609
    return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
610
}
611

    
612
/* Return < 0 if error. Important errors are:
613
  -EIO         generic I/O error (may happen for all errors)
614
  -ENOMEDIUM   No media inserted.
615
  -EINVAL      Invalid sector number or nb_sectors
616
  -EACCES      Trying to write a read-only device
617
*/
618
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
619
               const uint8_t *buf, int nb_sectors)
620
{
621
    BlockDriver *drv = bs->drv;
622
    if (!bs->drv)
623
        return -ENOMEDIUM;
624
    if (bs->read_only)
625
        return -EACCES;
626
    if (bdrv_check_request(bs, sector_num, nb_sectors))
627
        return -EIO;
628

    
629
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
630
}
631

    
632
int bdrv_pread(BlockDriverState *bs, int64_t offset,
633
               void *buf, int count1)
634
{
635
    uint8_t tmp_buf[SECTOR_SIZE];
636
    int len, nb_sectors, count;
637
    int64_t sector_num;
638

    
639
    count = count1;
640
    /* first read to align to sector start */
641
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
642
    if (len > count)
643
        len = count;
644
    sector_num = offset >> SECTOR_BITS;
645
    if (len > 0) {
646
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
647
            return -EIO;
648
        memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
649
        count -= len;
650
        if (count == 0)
651
            return count1;
652
        sector_num++;
653
        buf += len;
654
    }
655

    
656
    /* read the sectors "in place" */
657
    nb_sectors = count >> SECTOR_BITS;
658
    if (nb_sectors > 0) {
659
        if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
660
            return -EIO;
661
        sector_num += nb_sectors;
662
        len = nb_sectors << SECTOR_BITS;
663
        buf += len;
664
        count -= len;
665
    }
666

    
667
    /* add data from the last sector */
668
    if (count > 0) {
669
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
670
            return -EIO;
671
        memcpy(buf, tmp_buf, count);
672
    }
673
    return count1;
674
}
675

    
676
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
677
                const void *buf, int count1)
678
{
679
    uint8_t tmp_buf[SECTOR_SIZE];
680
    int len, nb_sectors, count;
681
    int64_t sector_num;
682

    
683
    count = count1;
684
    /* first write to align to sector start */
685
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
686
    if (len > count)
687
        len = count;
688
    sector_num = offset >> SECTOR_BITS;
689
    if (len > 0) {
690
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
691
            return -EIO;
692
        memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
693
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
694
            return -EIO;
695
        count -= len;
696
        if (count == 0)
697
            return count1;
698
        sector_num++;
699
        buf += len;
700
    }
701

    
702
    /* write the sectors "in place" */
703
    nb_sectors = count >> SECTOR_BITS;
704
    if (nb_sectors > 0) {
705
        if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
706
            return -EIO;
707
        sector_num += nb_sectors;
708
        len = nb_sectors << SECTOR_BITS;
709
        buf += len;
710
        count -= len;
711
    }
712

    
713
    /* add data from the last sector */
714
    if (count > 0) {
715
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
716
            return -EIO;
717
        memcpy(tmp_buf, buf, count);
718
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
719
            return -EIO;
720
    }
721
    return count1;
722
}
723

    
724
/**
725
 * Truncate file to 'offset' bytes (needed only for file protocols)
726
 */
727
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
728
{
729
    BlockDriver *drv = bs->drv;
730
    if (!drv)
731
        return -ENOMEDIUM;
732
    if (!drv->bdrv_truncate)
733
        return -ENOTSUP;
734
    return drv->bdrv_truncate(bs, offset);
735
}
736

    
737
/**
738
 * Length of a file in bytes. Return < 0 if error or unknown.
739
 */
740
int64_t bdrv_getlength(BlockDriverState *bs)
741
{
742
    BlockDriver *drv = bs->drv;
743
    if (!drv)
744
        return -ENOMEDIUM;
745
    if (!drv->bdrv_getlength) {
746
        /* legacy mode */
747
        return bs->total_sectors * SECTOR_SIZE;
748
    }
749
    return drv->bdrv_getlength(bs);
750
}
751

    
752
/* return 0 as number of sectors if no device present or error */
753
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
754
{
755
    int64_t length;
756
    length = bdrv_getlength(bs);
757
    if (length < 0)
758
        length = 0;
759
    else
760
        length = length >> SECTOR_BITS;
761
    *nb_sectors_ptr = length;
762
}
763

    
764
struct partition {
765
        uint8_t boot_ind;           /* 0x80 - active */
766
        uint8_t head;               /* starting head */
767
        uint8_t sector;             /* starting sector */
768
        uint8_t cyl;                /* starting cylinder */
769
        uint8_t sys_ind;            /* What partition type */
770
        uint8_t end_head;           /* end head */
771
        uint8_t end_sector;         /* end sector */
772
        uint8_t end_cyl;            /* end cylinder */
773
        uint32_t start_sect;        /* starting sector counting from 0 */
774
        uint32_t nr_sects;          /* nr of sectors in partition */
775
} __attribute__((packed));
776

    
777
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
778
static int guess_disk_lchs(BlockDriverState *bs,
779
                           int *pcylinders, int *pheads, int *psectors)
780
{
781
    uint8_t buf[512];
782
    int ret, i, heads, sectors, cylinders;
783
    struct partition *p;
784
    uint32_t nr_sects;
785
    uint64_t nb_sectors;
786

    
787
    bdrv_get_geometry(bs, &nb_sectors);
788

    
789
    ret = bdrv_read(bs, 0, buf, 1);
790
    if (ret < 0)
791
        return -1;
792
    /* test msdos magic */
793
    if (buf[510] != 0x55 || buf[511] != 0xaa)
794
        return -1;
795
    for(i = 0; i < 4; i++) {
796
        p = ((struct partition *)(buf + 0x1be)) + i;
797
        nr_sects = le32_to_cpu(p->nr_sects);
798
        if (nr_sects && p->end_head) {
799
            /* We make the assumption that the partition terminates on
800
               a cylinder boundary */
801
            heads = p->end_head + 1;
802
            sectors = p->end_sector & 63;
803
            if (sectors == 0)
804
                continue;
805
            cylinders = nb_sectors / (heads * sectors);
806
            if (cylinders < 1 || cylinders > 16383)
807
                continue;
808
            *pheads = heads;
809
            *psectors = sectors;
810
            *pcylinders = cylinders;
811
#if 0
812
            printf("guessed geometry: LCHS=%d %d %d\n",
813
                   cylinders, heads, sectors);
814
#endif
815
            return 0;
816
        }
817
    }
818
    return -1;
819
}
820

    
821
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
822
{
823
    int translation, lba_detected = 0;
824
    int cylinders, heads, secs;
825
    uint64_t nb_sectors;
826

    
827
    /* if a geometry hint is available, use it */
828
    bdrv_get_geometry(bs, &nb_sectors);
829
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
830
    translation = bdrv_get_translation_hint(bs);
831
    if (cylinders != 0) {
832
        *pcyls = cylinders;
833
        *pheads = heads;
834
        *psecs = secs;
835
    } else {
836
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
837
            if (heads > 16) {
838
                /* if heads > 16, it means that a BIOS LBA
839
                   translation was active, so the default
840
                   hardware geometry is OK */
841
                lba_detected = 1;
842
                goto default_geometry;
843
            } else {
844
                *pcyls = cylinders;
845
                *pheads = heads;
846
                *psecs = secs;
847
                /* disable any translation to be in sync with
848
                   the logical geometry */
849
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
850
                    bdrv_set_translation_hint(bs,
851
                                              BIOS_ATA_TRANSLATION_NONE);
852
                }
853
            }
854
        } else {
855
        default_geometry:
856
            /* if no geometry, use a standard physical disk geometry */
857
            cylinders = nb_sectors / (16 * 63);
858

    
859
            if (cylinders > 16383)
860
                cylinders = 16383;
861
            else if (cylinders < 2)
862
                cylinders = 2;
863
            *pcyls = cylinders;
864
            *pheads = 16;
865
            *psecs = 63;
866
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
867
                if ((*pcyls * *pheads) <= 131072) {
868
                    bdrv_set_translation_hint(bs,
869
                                              BIOS_ATA_TRANSLATION_LARGE);
870
                } else {
871
                    bdrv_set_translation_hint(bs,
872
                                              BIOS_ATA_TRANSLATION_LBA);
873
                }
874
            }
875
        }
876
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
877
    }
878
}
879

    
880
void bdrv_set_geometry_hint(BlockDriverState *bs,
881
                            int cyls, int heads, int secs)
882
{
883
    bs->cyls = cyls;
884
    bs->heads = heads;
885
    bs->secs = secs;
886
}
887

    
888
void bdrv_set_type_hint(BlockDriverState *bs, int type)
889
{
890
    bs->type = type;
891
    bs->removable = ((type == BDRV_TYPE_CDROM ||
892
                      type == BDRV_TYPE_FLOPPY));
893
}
894

    
895
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
896
{
897
    bs->translation = translation;
898
}
899

    
900
void bdrv_get_geometry_hint(BlockDriverState *bs,
901
                            int *pcyls, int *pheads, int *psecs)
902
{
903
    *pcyls = bs->cyls;
904
    *pheads = bs->heads;
905
    *psecs = bs->secs;
906
}
907

    
908
int bdrv_get_type_hint(BlockDriverState *bs)
909
{
910
    return bs->type;
911
}
912

    
913
int bdrv_get_translation_hint(BlockDriverState *bs)
914
{
915
    return bs->translation;
916
}
917

    
918
int bdrv_is_removable(BlockDriverState *bs)
919
{
920
    return bs->removable;
921
}
922

    
923
int bdrv_is_read_only(BlockDriverState *bs)
924
{
925
    return bs->read_only;
926
}
927

    
928
int bdrv_is_sg(BlockDriverState *bs)
929
{
930
    return bs->sg;
931
}
932

    
933
int bdrv_enable_write_cache(BlockDriverState *bs)
934
{
935
    return bs->enable_write_cache;
936
}
937

    
938
/* XXX: no longer used */
939
void bdrv_set_change_cb(BlockDriverState *bs,
940
                        void (*change_cb)(void *opaque), void *opaque)
941
{
942
    bs->change_cb = change_cb;
943
    bs->change_opaque = opaque;
944
}
945

    
946
int bdrv_is_encrypted(BlockDriverState *bs)
947
{
948
    if (bs->backing_hd && bs->backing_hd->encrypted)
949
        return 1;
950
    return bs->encrypted;
951
}
952

    
953
int bdrv_key_required(BlockDriverState *bs)
954
{
955
    BlockDriverState *backing_hd = bs->backing_hd;
956

    
957
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
958
        return 1;
959
    return (bs->encrypted && !bs->valid_key);
960
}
961

    
962
int bdrv_set_key(BlockDriverState *bs, const char *key)
963
{
964
    int ret;
965
    if (bs->backing_hd && bs->backing_hd->encrypted) {
966
        ret = bdrv_set_key(bs->backing_hd, key);
967
        if (ret < 0)
968
            return ret;
969
        if (!bs->encrypted)
970
            return 0;
971
    }
972
    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
973
        return -1;
974
    ret = bs->drv->bdrv_set_key(bs, key);
975
    if (ret < 0) {
976
        bs->valid_key = 0;
977
    } else if (!bs->valid_key) {
978
        bs->valid_key = 1;
979
        /* call the change callback now, we skipped it on open */
980
        bs->media_changed = 1;
981
        if (bs->change_cb)
982
            bs->change_cb(bs->change_opaque);
983
    }
984
    return ret;
985
}
986

    
987
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
988
{
989
    if (!bs->drv) {
990
        buf[0] = '\0';
991
    } else {
992
        pstrcpy(buf, buf_size, bs->drv->format_name);
993
    }
994
}
995

    
996
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
997
                         void *opaque)
998
{
999
    BlockDriver *drv;
1000

    
1001
    for (drv = first_drv; drv != NULL; drv = drv->next) {
1002
        it(opaque, drv->format_name);
1003
    }
1004
}
1005

    
1006
BlockDriverState *bdrv_find(const char *name)
1007
{
1008
    BlockDriverState *bs;
1009

    
1010
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1011
        if (!strcmp(name, bs->device_name))
1012
            return bs;
1013
    }
1014
    return NULL;
1015
}
1016

    
1017
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1018
{
1019
    BlockDriverState *bs;
1020

    
1021
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1022
        it(opaque, bs);
1023
    }
1024
}
1025

    
1026
const char *bdrv_get_device_name(BlockDriverState *bs)
1027
{
1028
    return bs->device_name;
1029
}
1030

    
1031
void bdrv_flush(BlockDriverState *bs)
1032
{
1033
    if (!bs->drv)
1034
        return;
1035
    if (bs->drv->bdrv_flush)
1036
        bs->drv->bdrv_flush(bs);
1037
    if (bs->backing_hd)
1038
        bdrv_flush(bs->backing_hd);
1039
}
1040

    
1041
void bdrv_flush_all(void)
1042
{
1043
    BlockDriverState *bs;
1044

    
1045
    for (bs = bdrv_first; bs != NULL; bs = bs->next)
1046
        if (bs->drv && !bdrv_is_read_only(bs) && 
1047
            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1048
            bdrv_flush(bs);
1049
}
1050

    
1051
/*
1052
 * Returns true iff the specified sector is present in the disk image. Drivers
1053
 * not implementing the functionality are assumed to not support backing files,
1054
 * hence all their sectors are reported as allocated.
1055
 *
1056
 * 'pnum' is set to the number of sectors (including and immediately following
1057
 * the specified sector) that are known to be in the same
1058
 * allocated/unallocated state.
1059
 *
1060
 * 'nb_sectors' is the max value 'pnum' should be set to.
1061
 */
1062
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1063
        int *pnum)
1064
{
1065
    int64_t n;
1066
    if (!bs->drv->bdrv_is_allocated) {
1067
        if (sector_num >= bs->total_sectors) {
1068
            *pnum = 0;
1069
            return 0;
1070
        }
1071
        n = bs->total_sectors - sector_num;
1072
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1073
        return 1;
1074
    }
1075
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1076
}
1077

    
1078
void bdrv_info(Monitor *mon)
1079
{
1080
    BlockDriverState *bs;
1081

    
1082
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1083
        monitor_printf(mon, "%s:", bs->device_name);
1084
        monitor_printf(mon, " type=");
1085
        switch(bs->type) {
1086
        case BDRV_TYPE_HD:
1087
            monitor_printf(mon, "hd");
1088
            break;
1089
        case BDRV_TYPE_CDROM:
1090
            monitor_printf(mon, "cdrom");
1091
            break;
1092
        case BDRV_TYPE_FLOPPY:
1093
            monitor_printf(mon, "floppy");
1094
            break;
1095
        }
1096
        monitor_printf(mon, " removable=%d", bs->removable);
1097
        if (bs->removable) {
1098
            monitor_printf(mon, " locked=%d", bs->locked);
1099
        }
1100
        if (bs->drv) {
1101
            monitor_printf(mon, " file=");
1102
            monitor_print_filename(mon, bs->filename);
1103
            if (bs->backing_file[0] != '\0') {
1104
                monitor_printf(mon, " backing_file=");
1105
                monitor_print_filename(mon, bs->backing_file);
1106
            }
1107
            monitor_printf(mon, " ro=%d", bs->read_only);
1108
            monitor_printf(mon, " drv=%s", bs->drv->format_name);
1109
            monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1110
        } else {
1111
            monitor_printf(mon, " [not inserted]");
1112
        }
1113
        monitor_printf(mon, "\n");
1114
    }
1115
}
1116

    
1117
/* The "info blockstats" command. */
1118
void bdrv_info_stats(Monitor *mon)
1119
{
1120
    BlockDriverState *bs;
1121

    
1122
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1123
        monitor_printf(mon, "%s:"
1124
                       " rd_bytes=%" PRIu64
1125
                       " wr_bytes=%" PRIu64
1126
                       " rd_operations=%" PRIu64
1127
                       " wr_operations=%" PRIu64
1128
                       "\n",
1129
                       bs->device_name,
1130
                       bs->rd_bytes, bs->wr_bytes,
1131
                       bs->rd_ops, bs->wr_ops);
1132
    }
1133
}
1134

    
1135
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1136
{
1137
    if (bs->backing_hd && bs->backing_hd->encrypted)
1138
        return bs->backing_file;
1139
    else if (bs->encrypted)
1140
        return bs->filename;
1141
    else
1142
        return NULL;
1143
}
1144

    
1145
void bdrv_get_backing_filename(BlockDriverState *bs,
1146
                               char *filename, int filename_size)
1147
{
1148
    if (!bs->backing_hd) {
1149
        pstrcpy(filename, filename_size, "");
1150
    } else {
1151
        pstrcpy(filename, filename_size, bs->backing_file);
1152
    }
1153
}
1154

    
1155
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1156
                          const uint8_t *buf, int nb_sectors)
1157
{
1158
    BlockDriver *drv = bs->drv;
1159
    if (!drv)
1160
        return -ENOMEDIUM;
1161
    if (!drv->bdrv_write_compressed)
1162
        return -ENOTSUP;
1163
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1164
        return -EIO;
1165
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1166
}
1167

    
1168
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1169
{
1170
    BlockDriver *drv = bs->drv;
1171
    if (!drv)
1172
        return -ENOMEDIUM;
1173
    if (!drv->bdrv_get_info)
1174
        return -ENOTSUP;
1175
    memset(bdi, 0, sizeof(*bdi));
1176
    return drv->bdrv_get_info(bs, bdi);
1177
}
1178

    
1179
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1180
                      int64_t pos, int size)
1181
{
1182
    BlockDriver *drv = bs->drv;
1183
    if (!drv)
1184
        return -ENOMEDIUM;
1185
    if (!drv->bdrv_save_vmstate)
1186
        return -ENOTSUP;
1187
    return drv->bdrv_save_vmstate(bs, buf, pos, size);
1188
}
1189

    
1190
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1191
                      int64_t pos, int size)
1192
{
1193
    BlockDriver *drv = bs->drv;
1194
    if (!drv)
1195
        return -ENOMEDIUM;
1196
    if (!drv->bdrv_load_vmstate)
1197
        return -ENOTSUP;
1198
    return drv->bdrv_load_vmstate(bs, buf, pos, size);
1199
}
1200

    
1201
/**************************************************************/
1202
/* handling of snapshots */
1203

    
1204
int bdrv_snapshot_create(BlockDriverState *bs,
1205
                         QEMUSnapshotInfo *sn_info)
1206
{
1207
    BlockDriver *drv = bs->drv;
1208
    if (!drv)
1209
        return -ENOMEDIUM;
1210
    if (!drv->bdrv_snapshot_create)
1211
        return -ENOTSUP;
1212
    return drv->bdrv_snapshot_create(bs, sn_info);
1213
}
1214

    
1215
int bdrv_snapshot_goto(BlockDriverState *bs,
1216
                       const char *snapshot_id)
1217
{
1218
    BlockDriver *drv = bs->drv;
1219
    if (!drv)
1220
        return -ENOMEDIUM;
1221
    if (!drv->bdrv_snapshot_goto)
1222
        return -ENOTSUP;
1223
    return drv->bdrv_snapshot_goto(bs, snapshot_id);
1224
}
1225

    
1226
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1227
{
1228
    BlockDriver *drv = bs->drv;
1229
    if (!drv)
1230
        return -ENOMEDIUM;
1231
    if (!drv->bdrv_snapshot_delete)
1232
        return -ENOTSUP;
1233
    return drv->bdrv_snapshot_delete(bs, snapshot_id);
1234
}
1235

    
1236
int bdrv_snapshot_list(BlockDriverState *bs,
1237
                       QEMUSnapshotInfo **psn_info)
1238
{
1239
    BlockDriver *drv = bs->drv;
1240
    if (!drv)
1241
        return -ENOMEDIUM;
1242
    if (!drv->bdrv_snapshot_list)
1243
        return -ENOTSUP;
1244
    return drv->bdrv_snapshot_list(bs, psn_info);
1245
}
1246

    
1247
#define NB_SUFFIXES 4
1248

    
1249
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1250
{
1251
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1252
    int64_t base;
1253
    int i;
1254

    
1255
    if (size <= 999) {
1256
        snprintf(buf, buf_size, "%" PRId64, size);
1257
    } else {
1258
        base = 1024;
1259
        for(i = 0; i < NB_SUFFIXES; i++) {
1260
            if (size < (10 * base)) {
1261
                snprintf(buf, buf_size, "%0.1f%c",
1262
                         (double)size / base,
1263
                         suffixes[i]);
1264
                break;
1265
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1266
                snprintf(buf, buf_size, "%" PRId64 "%c",
1267
                         ((size + (base >> 1)) / base),
1268
                         suffixes[i]);
1269
                break;
1270
            }
1271
            base = base * 1024;
1272
        }
1273
    }
1274
    return buf;
1275
}
1276

    
1277
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1278
{
1279
    char buf1[128], date_buf[128], clock_buf[128];
1280
#ifdef _WIN32
1281
    struct tm *ptm;
1282
#else
1283
    struct tm tm;
1284
#endif
1285
    time_t ti;
1286
    int64_t secs;
1287

    
1288
    if (!sn) {
1289
        snprintf(buf, buf_size,
1290
                 "%-10s%-20s%7s%20s%15s",
1291
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1292
    } else {
1293
        ti = sn->date_sec;
1294
#ifdef _WIN32
1295
        ptm = localtime(&ti);
1296
        strftime(date_buf, sizeof(date_buf),
1297
                 "%Y-%m-%d %H:%M:%S", ptm);
1298
#else
1299
        localtime_r(&ti, &tm);
1300
        strftime(date_buf, sizeof(date_buf),
1301
                 "%Y-%m-%d %H:%M:%S", &tm);
1302
#endif
1303
        secs = sn->vm_clock_nsec / 1000000000;
1304
        snprintf(clock_buf, sizeof(clock_buf),
1305
                 "%02d:%02d:%02d.%03d",
1306
                 (int)(secs / 3600),
1307
                 (int)((secs / 60) % 60),
1308
                 (int)(secs % 60),
1309
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1310
        snprintf(buf, buf_size,
1311
                 "%-10s%-20s%7s%20s%15s",
1312
                 sn->id_str, sn->name,
1313
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1314
                 date_buf,
1315
                 clock_buf);
1316
    }
1317
    return buf;
1318
}
1319

    
1320

    
1321
/**************************************************************/
1322
/* async I/Os */
1323

    
1324
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1325
                                 QEMUIOVector *qiov, int nb_sectors,
1326
                                 BlockDriverCompletionFunc *cb, void *opaque)
1327
{
1328
    BlockDriver *drv = bs->drv;
1329
    BlockDriverAIOCB *ret;
1330

    
1331
    if (!drv)
1332
        return NULL;
1333
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1334
        return NULL;
1335

    
1336
    ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1337
                              cb, opaque);
1338

    
1339
    if (ret) {
1340
        /* Update stats even though technically transfer has not happened. */
1341
        bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1342
        bs->rd_ops ++;
1343
    }
1344

    
1345
    return ret;
1346
}
1347

    
1348
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1349
                                  QEMUIOVector *qiov, int nb_sectors,
1350
                                  BlockDriverCompletionFunc *cb, void *opaque)
1351
{
1352
    BlockDriver *drv = bs->drv;
1353
    BlockDriverAIOCB *ret;
1354

    
1355
    if (!drv)
1356
        return NULL;
1357
    if (bs->read_only)
1358
        return NULL;
1359
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1360
        return NULL;
1361

    
1362
    ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1363
                               cb, opaque);
1364

    
1365
    if (ret) {
1366
        /* Update stats even though technically transfer has not happened. */
1367
        bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1368
        bs->wr_ops ++;
1369
    }
1370

    
1371
    return ret;
1372
}
1373

    
1374

    
1375
typedef struct MultiwriteCB {
1376
    int error;
1377
    int num_requests;
1378
    int num_callbacks;
1379
    struct {
1380
        BlockDriverCompletionFunc *cb;
1381
        void *opaque;
1382
        QEMUIOVector *free_qiov;
1383
        void *free_buf;
1384
    } callbacks[];
1385
} MultiwriteCB;
1386

    
1387
static void multiwrite_user_cb(MultiwriteCB *mcb)
1388
{
1389
    int i;
1390

    
1391
    for (i = 0; i < mcb->num_callbacks; i++) {
1392
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1393
        qemu_free(mcb->callbacks[i].free_qiov);
1394
        qemu_free(mcb->callbacks[i].free_buf);
1395
    }
1396
}
1397

    
1398
static void multiwrite_cb(void *opaque, int ret)
1399
{
1400
    MultiwriteCB *mcb = opaque;
1401

    
1402
    if (ret < 0) {
1403
        mcb->error = ret;
1404
        multiwrite_user_cb(mcb);
1405
    }
1406

    
1407
    mcb->num_requests--;
1408
    if (mcb->num_requests == 0) {
1409
        if (mcb->error == 0) {
1410
            multiwrite_user_cb(mcb);
1411
        }
1412
        qemu_free(mcb);
1413
    }
1414
}
1415

    
1416
static int multiwrite_req_compare(const void *a, const void *b)
1417
{
1418
    return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1419
}
1420

    
1421
/*
1422
 * Takes a bunch of requests and tries to merge them. Returns the number of
1423
 * requests that remain after merging.
1424
 */
1425
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1426
    int num_reqs, MultiwriteCB *mcb)
1427
{
1428
    int i, outidx;
1429

    
1430
    // Sort requests by start sector
1431
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1432

    
1433
    // Check if adjacent requests touch the same clusters. If so, combine them,
1434
    // filling up gaps with zero sectors.
1435
    outidx = 0;
1436
    for (i = 1; i < num_reqs; i++) {
1437
        int merge = 0;
1438
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1439

    
1440
        // This handles the cases that are valid for all block drivers, namely
1441
        // exactly sequential writes and overlapping writes.
1442
        if (reqs[i].sector <= oldreq_last) {
1443
            merge = 1;
1444
        }
1445

    
1446
        // The block driver may decide that it makes sense to combine requests
1447
        // even if there is a gap of some sectors between them. In this case,
1448
        // the gap is filled with zeros (therefore only applicable for yet
1449
        // unused space in format like qcow2).
1450
        if (!merge && bs->drv->bdrv_merge_requests) {
1451
            merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1452
        }
1453

    
1454
        if (merge) {
1455
            size_t size;
1456
            QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1457
            qemu_iovec_init(qiov,
1458
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1459

    
1460
            // Add the first request to the merged one. If the requests are
1461
            // overlapping, drop the last sectors of the first request.
1462
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
1463
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1464

    
1465
            // We might need to add some zeros between the two requests
1466
            if (reqs[i].sector > oldreq_last) {
1467
                size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1468
                uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1469
                memset(buf, 0, zero_bytes);
1470
                qemu_iovec_add(qiov, buf, zero_bytes);
1471
                mcb->callbacks[i].free_buf = buf;
1472
            }
1473

    
1474
            // Add the second request
1475
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1476

    
1477
            reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1478
            reqs[outidx].qiov = qiov;
1479

    
1480
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1481
        } else {
1482
            outidx++;
1483
            reqs[outidx].sector     = reqs[i].sector;
1484
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1485
            reqs[outidx].qiov       = reqs[i].qiov;
1486
        }
1487
    }
1488

    
1489
    return outidx + 1;
1490
}
1491

    
1492
/*
1493
 * Submit multiple AIO write requests at once.
1494
 *
1495
 * On success, the function returns 0 and all requests in the reqs array have
1496
 * been submitted. In error case this function returns -1, and any of the
1497
 * requests may or may not be submitted yet. In particular, this means that the
1498
 * callback will be called for some of the requests, for others it won't. The
1499
 * caller must check the error field of the BlockRequest to wait for the right
1500
 * callbacks (if error != 0, no callback will be called).
1501
 *
1502
 * The implementation may modify the contents of the reqs array, e.g. to merge
1503
 * requests. However, the fields opaque and error are left unmodified as they
1504
 * are used to signal failure for a single request to the caller.
1505
 */
1506
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1507
{
1508
    BlockDriverAIOCB *acb;
1509
    MultiwriteCB *mcb;
1510
    int i;
1511

    
1512
    if (num_reqs == 0) {
1513
        return 0;
1514
    }
1515

    
1516
    // Create MultiwriteCB structure
1517
    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1518
    mcb->num_requests = 0;
1519
    mcb->num_callbacks = num_reqs;
1520

    
1521
    for (i = 0; i < num_reqs; i++) {
1522
        mcb->callbacks[i].cb = reqs[i].cb;
1523
        mcb->callbacks[i].opaque = reqs[i].opaque;
1524
    }
1525

    
1526
    // Check for mergable requests
1527
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1528

    
1529
    // Run the aio requests
1530
    for (i = 0; i < num_reqs; i++) {
1531
        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1532
            reqs[i].nb_sectors, multiwrite_cb, mcb);
1533

    
1534
        if (acb == NULL) {
1535
            // We can only fail the whole thing if no request has been
1536
            // submitted yet. Otherwise we'll wait for the submitted AIOs to
1537
            // complete and report the error in the callback.
1538
            if (mcb->num_requests == 0) {
1539
                reqs[i].error = EIO;
1540
                goto fail;
1541
            } else {
1542
                mcb->error = EIO;
1543
                break;
1544
            }
1545
        } else {
1546
            mcb->num_requests++;
1547
        }
1548
    }
1549

    
1550
    return 0;
1551

    
1552
fail:
1553
    free(mcb);
1554
    return -1;
1555
}
1556

    
1557
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1558
        BlockDriverCompletionFunc *cb, void *opaque)
1559
{
1560
    BlockDriver *drv = bs->drv;
1561

    
1562
    if (!drv)
1563
        return NULL;
1564

    
1565
    /*
1566
     * Note that unlike bdrv_flush the driver is reponsible for flushing a
1567
     * backing image if it exists.
1568
     */
1569
    return drv->bdrv_aio_flush(bs, cb, opaque);
1570
}
1571

    
1572
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1573
{
1574
    acb->pool->cancel(acb);
1575
}
1576

    
1577

    
1578
/**************************************************************/
1579
/* async block device emulation */
1580

    
1581
typedef struct BlockDriverAIOCBSync {
1582
    BlockDriverAIOCB common;
1583
    QEMUBH *bh;
1584
    int ret;
1585
    /* vector translation state */
1586
    QEMUIOVector *qiov;
1587
    uint8_t *bounce;
1588
    int is_write;
1589
} BlockDriverAIOCBSync;
1590

    
1591
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1592
{
1593
    BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1594
    qemu_bh_delete(acb->bh);
1595
    acb->bh = NULL;
1596
    qemu_aio_release(acb);
1597
}
1598

    
1599
static AIOPool bdrv_em_aio_pool = {
1600
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
1601
    .cancel             = bdrv_aio_cancel_em,
1602
};
1603

    
1604
static void bdrv_aio_bh_cb(void *opaque)
1605
{
1606
    BlockDriverAIOCBSync *acb = opaque;
1607

    
1608
    if (!acb->is_write)
1609
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1610
    qemu_vfree(acb->bounce);
1611
    acb->common.cb(acb->common.opaque, acb->ret);
1612
    qemu_bh_delete(acb->bh);
1613
    acb->bh = NULL;
1614
    qemu_aio_release(acb);
1615
}
1616

    
1617
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1618
                                            int64_t sector_num,
1619
                                            QEMUIOVector *qiov,
1620
                                            int nb_sectors,
1621
                                            BlockDriverCompletionFunc *cb,
1622
                                            void *opaque,
1623
                                            int is_write)
1624

    
1625
{
1626
    BlockDriverAIOCBSync *acb;
1627

    
1628
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1629
    acb->is_write = is_write;
1630
    acb->qiov = qiov;
1631
    acb->bounce = qemu_blockalign(bs, qiov->size);
1632

    
1633
    if (!acb->bh)
1634
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1635

    
1636
    if (is_write) {
1637
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1638
        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1639
    } else {
1640
        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1641
    }
1642

    
1643
    qemu_bh_schedule(acb->bh);
1644

    
1645
    return &acb->common;
1646
}
1647

    
1648
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1649
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1650
        BlockDriverCompletionFunc *cb, void *opaque)
1651
{
1652
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1653
}
1654

    
1655
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1656
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1657
        BlockDriverCompletionFunc *cb, void *opaque)
1658
{
1659
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1660
}
1661

    
1662
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1663
        BlockDriverCompletionFunc *cb, void *opaque)
1664
{
1665
    BlockDriverAIOCBSync *acb;
1666

    
1667
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1668
    acb->is_write = 1; /* don't bounce in the completion hadler */
1669
    acb->qiov = NULL;
1670
    acb->bounce = NULL;
1671
    acb->ret = 0;
1672

    
1673
    if (!acb->bh)
1674
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1675

    
1676
    bdrv_flush(bs);
1677
    qemu_bh_schedule(acb->bh);
1678
    return &acb->common;
1679
}
1680

    
1681
/**************************************************************/
1682
/* sync block device emulation */
1683

    
1684
static void bdrv_rw_em_cb(void *opaque, int ret)
1685
{
1686
    *(int *)opaque = ret;
1687
}
1688

    
1689
#define NOT_DONE 0x7fffffff
1690

    
1691
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1692
                        uint8_t *buf, int nb_sectors)
1693
{
1694
    int async_ret;
1695
    BlockDriverAIOCB *acb;
1696
    struct iovec iov;
1697
    QEMUIOVector qiov;
1698

    
1699
    async_ret = NOT_DONE;
1700
    iov.iov_base = (void *)buf;
1701
    iov.iov_len = nb_sectors * 512;
1702
    qemu_iovec_init_external(&qiov, &iov, 1);
1703
    acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1704
        bdrv_rw_em_cb, &async_ret);
1705
    if (acb == NULL)
1706
        return -1;
1707

    
1708
    while (async_ret == NOT_DONE) {
1709
        qemu_aio_wait();
1710
    }
1711

    
1712
    return async_ret;
1713
}
1714

    
1715
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1716
                         const uint8_t *buf, int nb_sectors)
1717
{
1718
    int async_ret;
1719
    BlockDriverAIOCB *acb;
1720
    struct iovec iov;
1721
    QEMUIOVector qiov;
1722

    
1723
    async_ret = NOT_DONE;
1724
    iov.iov_base = (void *)buf;
1725
    iov.iov_len = nb_sectors * 512;
1726
    qemu_iovec_init_external(&qiov, &iov, 1);
1727
    acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1728
        bdrv_rw_em_cb, &async_ret);
1729
    if (acb == NULL)
1730
        return -1;
1731
    while (async_ret == NOT_DONE) {
1732
        qemu_aio_wait();
1733
    }
1734
    return async_ret;
1735
}
1736

    
1737
void bdrv_init(void)
1738
{
1739
    module_call_init(MODULE_INIT_BLOCK);
1740
}
1741

    
1742
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1743
                   BlockDriverCompletionFunc *cb, void *opaque)
1744
{
1745
    BlockDriverAIOCB *acb;
1746

    
1747
    if (pool->free_aiocb) {
1748
        acb = pool->free_aiocb;
1749
        pool->free_aiocb = acb->next;
1750
    } else {
1751
        acb = qemu_mallocz(pool->aiocb_size);
1752
        acb->pool = pool;
1753
    }
1754
    acb->bs = bs;
1755
    acb->cb = cb;
1756
    acb->opaque = opaque;
1757
    return acb;
1758
}
1759

    
1760
void qemu_aio_release(void *p)
1761
{
1762
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1763
    AIOPool *pool = acb->pool;
1764
    acb->next = pool->free_aiocb;
1765
    pool->free_aiocb = acb;
1766
}
1767

    
1768
/**************************************************************/
1769
/* removable device support */
1770

    
1771
/**
1772
 * Return TRUE if the media is present
1773
 */
1774
int bdrv_is_inserted(BlockDriverState *bs)
1775
{
1776
    BlockDriver *drv = bs->drv;
1777
    int ret;
1778
    if (!drv)
1779
        return 0;
1780
    if (!drv->bdrv_is_inserted)
1781
        return 1;
1782
    ret = drv->bdrv_is_inserted(bs);
1783
    return ret;
1784
}
1785

    
1786
/**
1787
 * Return TRUE if the media changed since the last call to this
1788
 * function. It is currently only used for floppy disks
1789
 */
1790
int bdrv_media_changed(BlockDriverState *bs)
1791
{
1792
    BlockDriver *drv = bs->drv;
1793
    int ret;
1794

    
1795
    if (!drv || !drv->bdrv_media_changed)
1796
        ret = -ENOTSUP;
1797
    else
1798
        ret = drv->bdrv_media_changed(bs);
1799
    if (ret == -ENOTSUP)
1800
        ret = bs->media_changed;
1801
    bs->media_changed = 0;
1802
    return ret;
1803
}
1804

    
1805
/**
1806
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1807
 */
1808
int bdrv_eject(BlockDriverState *bs, int eject_flag)
1809
{
1810
    BlockDriver *drv = bs->drv;
1811
    int ret;
1812

    
1813
    if (bs->locked) {
1814
        return -EBUSY;
1815
    }
1816

    
1817
    if (!drv || !drv->bdrv_eject) {
1818
        ret = -ENOTSUP;
1819
    } else {
1820
        ret = drv->bdrv_eject(bs, eject_flag);
1821
    }
1822
    if (ret == -ENOTSUP) {
1823
        if (eject_flag)
1824
            bdrv_close(bs);
1825
        ret = 0;
1826
    }
1827

    
1828
    return ret;
1829
}
1830

    
1831
int bdrv_is_locked(BlockDriverState *bs)
1832
{
1833
    return bs->locked;
1834
}
1835

    
1836
/**
1837
 * Lock or unlock the media (if it is locked, the user won't be able
1838
 * to eject it manually).
1839
 */
1840
void bdrv_set_locked(BlockDriverState *bs, int locked)
1841
{
1842
    BlockDriver *drv = bs->drv;
1843

    
1844
    bs->locked = locked;
1845
    if (drv && drv->bdrv_set_locked) {
1846
        drv->bdrv_set_locked(bs, locked);
1847
    }
1848
}
1849

    
1850
/* needed for generic scsi interface */
1851

    
1852
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1853
{
1854
    BlockDriver *drv = bs->drv;
1855

    
1856
    if (drv && drv->bdrv_ioctl)
1857
        return drv->bdrv_ioctl(bs, req, buf);
1858
    return -ENOTSUP;
1859
}
1860

    
1861
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1862
        unsigned long int req, void *buf,
1863
        BlockDriverCompletionFunc *cb, void *opaque)
1864
{
1865
    BlockDriver *drv = bs->drv;
1866

    
1867
    if (drv && drv->bdrv_aio_ioctl)
1868
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1869
    return NULL;
1870
}
1871

    
1872
void *qemu_blockalign(BlockDriverState *bs, size_t size)
1873
{
1874
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1875
}