block.c 15.7 KB
Newer Older
B
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * QEMU System Emulator block driver
 * 
 * Copyright (c) 2003 Fabrice Bellard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"
B
bellard 已提交
25
#include "block_int.h"
B
bellard 已提交
26

B
bellard 已提交
27 28 29 30 31 32 33 34
#ifdef _BSD
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/disk.h>
#endif

B
bellard 已提交
35
static BlockDriverState *bdrv_first;
B
bellard 已提交
36 37 38 39 40 41 42
static BlockDriver *first_drv;

void bdrv_register(BlockDriver *bdrv)
{
    bdrv->next = first_drv;
    first_drv = bdrv;
}
B
bellard 已提交
43 44 45 46 47 48 49 50 51 52

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

    bs = qemu_mallocz(sizeof(BlockDriverState));
    if(!bs)
        return NULL;
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
B
bellard 已提交
53 54 55 56 57 58 59
    if (device_name[0] != '\0') {
        /* insert at the end */
        pbs = &bdrv_first;
        while (*pbs != NULL)
            pbs = &(*pbs)->next;
        *pbs = bs;
    }
B
bellard 已提交
60 61 62
    return bs;
}

B
bellard 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
BlockDriver *bdrv_find_format(const char *format_name)
{
    BlockDriver *drv1;
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
        if (!strcmp(drv1->format_name, format_name))
            return drv1;
    }
    return NULL;
}

int bdrv_create(BlockDriver *drv, 
                const char *filename, int64_t size_in_sectors,
                const char *backing_file, int flags)
{
    if (!drv->bdrv_create)
        return -ENOTSUP;
    return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
}

B
bellard 已提交
82 83 84 85 86 87 88
#ifdef _WIN32
static void get_tmp_filename(char *filename, int size)
{
    /* XXX: find a better function */
    tmpnam(filename);
}
#else
B
bellard 已提交
89
static void get_tmp_filename(char *filename, int size)
B
bellard 已提交
90
{
B
bellard 已提交
91
    int fd;
B
bellard 已提交
92
    /* XXX: race condition possible */
B
bellard 已提交
93 94 95 96
    pstrcpy(filename, size, "/tmp/vl.XXXXXX");
    fd = mkstemp(filename);
    close(fd);
}
B
bellard 已提交
97
#endif
B
bellard 已提交
98

B
bellard 已提交
99 100
/* XXX: force raw format if block or character device ? It would
   simplify the BSD case */
B
bellard 已提交
101 102 103 104
static BlockDriver *find_image_format(const char *filename)
{
    int fd, ret, score, score_max;
    BlockDriver *drv1, *drv;
B
bellard 已提交
105 106
    uint8_t *buf;
    size_t bufsize = 1024;
107

B
bellard 已提交
108 109 110
    fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
    if (fd < 0)
        return NULL;
B
bellard 已提交
111 112 113 114 115 116 117 118 119 120 121 122
#ifdef DIOCGSECTORSIZE
    {
        unsigned int sectorsize = 512;
        if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
            sectorsize > bufsize)
            bufsize = sectorsize;
    }
#endif
    buf = malloc(bufsize);
    if (!buf)
        return NULL;
    ret = read(fd, buf, bufsize);
B
bellard 已提交
123 124
    if (ret < 0) {
        close(fd);
B
bellard 已提交
125
        free(buf);
B
bellard 已提交
126 127 128 129 130 131 132 133 134 135 136
        return NULL;
    }
    close(fd);
    
    drv = NULL;
    score_max = 0;
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
        score = drv1->bdrv_probe(buf, ret, filename);
        if (score > score_max) {
            score_max = score;
            drv = drv1;
B
bellard 已提交
137
        }
B
bellard 已提交
138
    }
B
bellard 已提交
139
    free(buf);
B
bellard 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    return drv;
}

int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
{
    return bdrv_open2(bs, filename, snapshot, NULL);
}

int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
               BlockDriver *drv)
{
    int ret;
    char tmp_filename[1024];
    
    bs->read_only = 0;
    bs->is_temporary = 0;
    bs->encrypted = 0;
    
    if (snapshot) {
        BlockDriverState *bs1;
        int64_t total_size;
        
        /* if snapshot, we create a temporary backing file and open it
           instead of opening 'filename' directly */
164

B
bellard 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        /* if there is a backing file, use it */
        bs1 = bdrv_new("");
        if (!bs1) {
            return -1;
        }
        if (bdrv_open(bs1, filename, 0) < 0) {
            bdrv_delete(bs1);
            return -1;
        }
        total_size = bs1->total_sectors;
        bdrv_delete(bs1);
        
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
        /* XXX: use cow for linux as it is more efficient ? */
        if (bdrv_create(&bdrv_qcow, tmp_filename, 
                        total_size, filename, 0) < 0) {
            return -1;
        }
        filename = tmp_filename;
        bs->is_temporary = 1;
    }
    
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
    if (!drv) {
        drv = find_image_format(filename);
        if (!drv)
            return -1;
    }
    bs->drv = drv;
    bs->opaque = qemu_mallocz(drv->instance_size);
    if (bs->opaque == NULL && drv->instance_size > 0)
        return -1;
    
    ret = drv->bdrv_open(bs, filename);
    if (ret < 0) {
        qemu_free(bs->opaque);
        return -1;
202
    }
B
bellard 已提交
203
#ifndef _WIN32
B
bellard 已提交
204 205 206 207 208 209 210 211 212 213 214
    if (bs->is_temporary) {
        unlink(filename);
    }
#endif
    if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
        /* if there is a backing file, use it */
        bs->backing_hd = bdrv_new("");
        if (!bs->backing_hd) {
        fail:
            bdrv_close(bs);
            return -1;
215
        }
B
bellard 已提交
216
        if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
217 218 219
            goto fail;
    }

B
bellard 已提交
220 221 222 223 224 225 226
    bs->inserted = 1;

    /* call the change callback */
    if (bs->change_cb)
        bs->change_cb(bs->change_opaque);

    return 0;
B
bellard 已提交
227 228 229 230
}

void bdrv_close(BlockDriverState *bs)
{
B
bellard 已提交
231
    if (bs->inserted) {
B
bellard 已提交
232 233 234 235 236 237 238 239
        if (bs->backing_hd)
            bdrv_delete(bs->backing_hd);
        bs->drv->bdrv_close(bs);
        qemu_free(bs->opaque);
#ifdef _WIN32
        if (bs->is_temporary) {
            unlink(bs->filename);
        }
B
bellard 已提交
240
#endif
B
bellard 已提交
241 242
        bs->opaque = NULL;
        bs->drv = NULL;
B
bellard 已提交
243 244 245 246 247 248 249 250 251 252
        bs->inserted = 0;

        /* call the change callback */
        if (bs->change_cb)
            bs->change_cb(bs->change_opaque);
    }
}

void bdrv_delete(BlockDriverState *bs)
{
B
bellard 已提交
253
    /* XXX: remove the driver list */
B
bellard 已提交
254 255
    bdrv_close(bs);
    qemu_free(bs);
B
bellard 已提交
256 257
}

258 259 260 261
/* commit COW file into the raw image */
int bdrv_commit(BlockDriverState *bs)
{
    int64_t i;
B
bellard 已提交
262 263
    int n, j;
    unsigned char sector[512];
264

B
bellard 已提交
265
    if (!bs->inserted)
B
bellard 已提交
266
        return -ENOENT;
267 268

    if (bs->read_only) {
B
bellard 已提交
269
	return -EACCES;
270 271
    }

B
bellard 已提交
272 273 274
    if (!bs->backing_hd) {
	return -ENOTSUP;
    }
275

B
bellard 已提交
276 277 278 279 280 281 282 283 284 285 286
    for (i = 0; i < bs->total_sectors;) {
        if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
            for(j = 0; j < n; j++) {
                if (bdrv_read(bs, i, sector, 1) != 0) {
                    return -EIO;
                }

                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
                    return -EIO;
                }
                i++;
287
	    }
B
bellard 已提交
288 289 290
	} else {
            i += n;
        }
291 292 293 294
    }
    return 0;
}

B
bellard 已提交
295 296 297 298
/* return -1 if error */
int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
              uint8_t *buf, int nb_sectors)
{
B
bellard 已提交
299 300 301
    int ret, n;
    BlockDriver *drv = bs->drv;

B
bellard 已提交
302 303 304
    if (!bs->inserted)
        return -1;

305
    while (nb_sectors > 0) {
B
bellard 已提交
306
        if (sector_num == 0 && bs->boot_sector_enabled) {
B
bellard 已提交
307 308
            memcpy(buf, bs->boot_sector_data, 512);
            n = 1;
B
bellard 已提交
309 310 311 312 313 314 315 316 317 318 319
        } else if (bs->backing_hd) {
            if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
                ret = drv->bdrv_read(bs, sector_num, buf, n);
                if (ret < 0)
                    return -1;
            } else {
                /* read from the base image */
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
                if (ret < 0)
                    return -1;
            }
320
        } else {
B
bellard 已提交
321 322
            ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
            if (ret < 0)
323
                return -1;
B
bellard 已提交
324 325
            /* no need to loop */
            break;
326 327 328 329 330 331
        }
        nb_sectors -= n;
        sector_num += n;
        buf += n * 512;
    }
    return 0;
B
bellard 已提交
332 333 334 335 336 337
}

/* return -1 if error */
int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
               const uint8_t *buf, int nb_sectors)
{
B
bellard 已提交
338 339
    if (!bs->inserted)
        return -1;
B
bellard 已提交
340 341
    if (bs->read_only)
        return -1;
B
bellard 已提交
342
    return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
B
bellard 已提交
343 344 345 346 347 348
}

void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
{
    *nb_sectors_ptr = bs->total_sectors;
}
B
bellard 已提交
349 350 351 352 353 354 355 356 357 358

/* force a given boot sector. */
void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
{
    bs->boot_sector_enabled = 1;
    if (size > 512)
        size = 512;
    memcpy(bs->boot_sector_data, data, size);
    memset(bs->boot_sector_data + size, 0, 512 - size);
}
B
bellard 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

void bdrv_set_geometry_hint(BlockDriverState *bs, 
                            int cyls, int heads, int secs)
{
    bs->cyls = cyls;
    bs->heads = heads;
    bs->secs = secs;
}

void bdrv_set_type_hint(BlockDriverState *bs, int type)
{
    bs->type = type;
    bs->removable = ((type == BDRV_TYPE_CDROM ||
                      type == BDRV_TYPE_FLOPPY));
}

B
bellard 已提交
375 376 377 378 379
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
{
    bs->translation = translation;
}

B
bellard 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392
void bdrv_get_geometry_hint(BlockDriverState *bs, 
                            int *pcyls, int *pheads, int *psecs)
{
    *pcyls = bs->cyls;
    *pheads = bs->heads;
    *psecs = bs->secs;
}

int bdrv_get_type_hint(BlockDriverState *bs)
{
    return bs->type;
}

B
bellard 已提交
393 394 395 396 397
int bdrv_get_translation_hint(BlockDriverState *bs)
{
    return bs->translation;
}

B
bellard 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
int bdrv_is_removable(BlockDriverState *bs)
{
    return bs->removable;
}

int bdrv_is_read_only(BlockDriverState *bs)
{
    return bs->read_only;
}

int bdrv_is_inserted(BlockDriverState *bs)
{
    return bs->inserted;
}

int bdrv_is_locked(BlockDriverState *bs)
{
    return bs->locked;
}

void bdrv_set_locked(BlockDriverState *bs, int locked)
{
    bs->locked = locked;
}

void bdrv_set_change_cb(BlockDriverState *bs, 
                        void (*change_cb)(void *opaque), void *opaque)
{
    bs->change_cb = change_cb;
    bs->change_opaque = opaque;
}

B
bellard 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
int bdrv_is_encrypted(BlockDriverState *bs)
{
    if (bs->backing_hd && bs->backing_hd->encrypted)
        return 1;
    return bs->encrypted;
}

int bdrv_set_key(BlockDriverState *bs, const char *key)
{
    int ret;
    if (bs->backing_hd && bs->backing_hd->encrypted) {
        ret = bdrv_set_key(bs->backing_hd, key);
        if (ret < 0)
            return ret;
        if (!bs->encrypted)
            return 0;
    }
    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
        return -1;
    return bs->drv->bdrv_set_key(bs, key);
}

void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
{
    if (!bs->inserted || !bs->drv) {
        buf[0] = '\0';
    } else {
        pstrcpy(buf, buf_size, bs->drv->format_name);
    }
}

void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 
                         void *opaque)
{
    BlockDriver *drv;

    for (drv = first_drv; drv != NULL; drv = drv->next) {
        it(opaque, drv->format_name);
    }
}

B
bellard 已提交
471 472 473 474 475 476 477 478 479 480 481
BlockDriverState *bdrv_find(const char *name)
{
    BlockDriverState *bs;

    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
        if (!strcmp(name, bs->device_name))
            return bs;
    }
    return NULL;
}

B
bellard 已提交
482 483 484 485 486 487 488 489 490
void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
{
    BlockDriverState *bs;

    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
        it(opaque, bs->device_name);
    }
}

B
bellard 已提交
491 492 493 494 495
const char *bdrv_get_device_name(BlockDriverState *bs)
{
    return bs->device_name;
}

B
bellard 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
void bdrv_info(void)
{
    BlockDriverState *bs;

    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
        term_printf("%s:", bs->device_name);
        term_printf(" type=");
        switch(bs->type) {
        case BDRV_TYPE_HD:
            term_printf("hd");
            break;
        case BDRV_TYPE_CDROM:
            term_printf("cdrom");
            break;
        case BDRV_TYPE_FLOPPY:
            term_printf("floppy");
            break;
        }
        term_printf(" removable=%d", bs->removable);
        if (bs->removable) {
            term_printf(" locked=%d", bs->locked);
        }
        if (bs->inserted) {
            term_printf(" file=%s", bs->filename);
B
bellard 已提交
520 521
            if (bs->backing_file[0] != '\0')
                term_printf(" backing_file=%s", bs->backing_file);
B
bellard 已提交
522
            term_printf(" ro=%d", bs->read_only);
B
bellard 已提交
523 524 525
            term_printf(" drv=%s", bs->drv->format_name);
            if (bs->encrypted)
                term_printf(" encrypted");
B
bellard 已提交
526 527 528 529 530 531
        } else {
            term_printf(" [not inserted]");
        }
        term_printf("\n");
    }
}
B
bellard 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550


/**************************************************************/
/* RAW block driver */

typedef struct BDRVRawState {
    int fd;
} BDRVRawState;

static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
{
    return 1; /* maybe */
}

static int raw_open(BlockDriverState *bs, const char *filename)
{
    BDRVRawState *s = bs->opaque;
    int fd;
    int64_t size;
B
BSD fix  
bellard 已提交
551 552 553
#ifdef _BSD
    struct stat sb;
#endif
B
bellard 已提交
554 555 556 557 558 559 560 561

    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
    if (fd < 0) {
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
        if (fd < 0)
            return -1;
        bs->read_only = 1;
    }
B
bellard 已提交
562
#ifdef _BSD
B
BSD fix  
bellard 已提交
563
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
B
bellard 已提交
564
#ifdef DIOCGMEDIASIZE
B
BSD fix  
bellard 已提交
565
	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
B
bellard 已提交
566
#endif
B
BSD fix  
bellard 已提交
567
	    size = lseek(fd, 0LL, SEEK_END);
B
bellard 已提交
568 569 570 571 572
    } else
#endif
    {
        size = lseek(fd, 0, SEEK_END);
    }
573 574 575 576 577 578
#ifdef _WIN32
    /* On Windows hosts it can happen that we're unable to get file size
       for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
    if (size == -1)
        size = LONG_LONG_MAX;
#endif
B
bellard 已提交
579 580 581 582 583 584 585 586 587 588 589
    bs->total_sectors = size / 512;
    s->fd = fd;
    return 0;
}

static int raw_read(BlockDriverState *bs, int64_t sector_num, 
                    uint8_t *buf, int nb_sectors)
{
    BDRVRawState *s = bs->opaque;
    int ret;
    
B
bellard 已提交
590
    lseek(s->fd, sector_num * 512, SEEK_SET);
B
bellard 已提交
591 592 593 594 595 596 597 598 599 600 601 602
    ret = read(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;
}

static int raw_write(BlockDriverState *bs, int64_t sector_num, 
                     const uint8_t *buf, int nb_sectors)
{
    BDRVRawState *s = bs->opaque;
    int ret;
    
B
bellard 已提交
603
    lseek(s->fd, sector_num * 512, SEEK_SET);
B
bellard 已提交
604 605 606 607 608 609
    ret = write(s->fd, buf, nb_sectors * 512);
    if (ret != nb_sectors * 512) 
        return -1;
    return 0;
}

B
bellard 已提交
610
static void raw_close(BlockDriverState *bs)
B
bellard 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
{
    BDRVRawState *s = bs->opaque;
    close(s->fd);
}

static int raw_create(const char *filename, int64_t total_size,
                      const char *backing_file, int flags)
{
    int fd;

    if (flags || backing_file)
        return -ENOTSUP;

    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
              0644);
    if (fd < 0)
        return -EIO;
B
bellard 已提交
628
    ftruncate(fd, total_size * 512);
B
bellard 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    close(fd);
    return 0;
}

BlockDriver bdrv_raw = {
    "raw",
    sizeof(BDRVRawState),
    raw_probe,
    raw_open,
    raw_read,
    raw_write,
    raw_close,
    raw_create,
};

void bdrv_init(void)
{
    bdrv_register(&bdrv_raw);
#ifndef _WIN32
    bdrv_register(&bdrv_cow);
#endif
    bdrv_register(&bdrv_qcow);
    bdrv_register(&bdrv_vmdk);
B
bellard 已提交
652
    bdrv_register(&bdrv_cloop);
653
    bdrv_register(&bdrv_dmg);
654
    bdrv_register(&bdrv_bochs);
B
bellard 已提交
655
}