block.c (b6ce07aa83bdee3cfd2610f270a0ce304e78df95) block.c (579153325158d944be544ced96c6218e7d48802a)
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

--- 28 unchanged lines hidden (view full) ---

37#include <sys/disk.h>
38#endif
39#endif
40
41#ifdef _WIN32
42#include <windows.h>
43#endif
44
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

--- 28 unchanged lines hidden (view full) ---

37#include <sys/disk.h>
38#endif
39#endif
40
41#ifdef _WIN32
42#include <windows.h>
43#endif
44
45static int bdrv_open_common(BlockDriverState *bs, const char *filename,
46 int flags, BlockDriver *drv);
47
48static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
51static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
52 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
53 BlockDriverCompletionFunc *cb, void *opaque);
54static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
55 BlockDriverCompletionFunc *cb, void *opaque);

--- 293 unchanged lines hidden (view full) ---

349 drv = drv1;
350 }
351 }
352 }
353 return drv;
354}
355
356/*
45static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
47 BlockDriverCompletionFunc *cb, void *opaque);
48static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
51static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 BlockDriverCompletionFunc *cb, void *opaque);

--- 293 unchanged lines hidden (view full) ---

346 drv = drv1;
347 }
348 }
349 }
350 return drv;
351}
352
353/*
354 * Common part for opening disk images and files
355 */
356static int bdrv_open_common(BlockDriverState *bs, const char *filename,
357 int flags, BlockDriver *drv)
358{
359 int ret, open_flags;
360
361 assert(drv != NULL);
362
363 bs->is_temporary = 0;
364 bs->encrypted = 0;
365 bs->valid_key = 0;
366 bs->open_flags = flags;
367 /* buffer_alignment defaulted to 512, drivers can change this value */
368 bs->buffer_alignment = 512;
369
370 pstrcpy(bs->filename, sizeof(bs->filename), filename);
371
372 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
373 return -ENOTSUP;
374 }
375
376 bs->drv = drv;
377 bs->opaque = qemu_mallocz(drv->instance_size);
378
379 /*
380 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
381 * write cache to the guest. We do need the fdatasync to flush
382 * out transactions for block allocations, and we maybe have a
383 * volatile write cache in our backing device to deal with.
384 */
385 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
386 bs->enable_write_cache = 1;
387
388 /*
389 * Clear flags that are internal to the block layer before opening the
390 * image.
391 */
392 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
393
394 /*
395 * Snapshots should be writeable.
396 */
397 if (bs->is_temporary) {
398 open_flags |= BDRV_O_RDWR;
399 }
400
401 ret = drv->bdrv_open(bs, filename, open_flags);
402 if (ret < 0) {
403 goto free_and_fail;
404 }
405
406 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
407 if (drv->bdrv_getlength) {
408 bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
409 }
410#ifndef _WIN32
411 if (bs->is_temporary) {
412 unlink(filename);
413 }
414#endif
415 return 0;
416
417free_and_fail:
418 qemu_free(bs->opaque);
419 bs->opaque = NULL;
420 bs->drv = NULL;
421 return ret;
422}
423
424/*
357 * Opens a file using a protocol (file, host_device, nbd, ...)
358 */
359int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
360{
361 BlockDriverState *bs;
362 BlockDriver *drv;
363 int ret;
364

--- 132 unchanged lines hidden (view full) ---

497
498unlink_and_fail:
499 if (bs->is_temporary) {
500 unlink(filename);
501 }
502 return ret;
503}
504
425 * Opens a file using a protocol (file, host_device, nbd, ...)
426 */
427int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
428{
429 BlockDriverState *bs;
430 BlockDriver *drv;
431 int ret;
432

--- 132 unchanged lines hidden (view full) ---

565
566unlink_and_fail:
567 if (bs->is_temporary) {
568 unlink(filename);
569 }
570 return ret;
571}
572
505/*
506 * Common part for opening disk images and files
507 */
508static int bdrv_open_common(BlockDriverState *bs, const char *filename,
509 int flags, BlockDriver *drv)
510{
511 int ret, open_flags;
512
513 assert(drv != NULL);
514
515 bs->is_temporary = 0;
516 bs->encrypted = 0;
517 bs->valid_key = 0;
518 bs->open_flags = flags;
519 /* buffer_alignment defaulted to 512, drivers can change this value */
520 bs->buffer_alignment = 512;
521
522 pstrcpy(bs->filename, sizeof(bs->filename), filename);
523
524 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
525 return -ENOTSUP;
526 }
527
528 bs->drv = drv;
529 bs->opaque = qemu_mallocz(drv->instance_size);
530
531 /*
532 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
533 * write cache to the guest. We do need the fdatasync to flush
534 * out transactions for block allocations, and we maybe have a
535 * volatile write cache in our backing device to deal with.
536 */
537 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
538 bs->enable_write_cache = 1;
539
540 /*
541 * Clear flags that are internal to the block layer before opening the
542 * image.
543 */
544 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
545
546 /*
547 * Snapshots should be writeable.
548 */
549 if (bs->is_temporary) {
550 open_flags |= BDRV_O_RDWR;
551 }
552
553 ret = drv->bdrv_open(bs, filename, open_flags);
554 if (ret < 0) {
555 goto free_and_fail;
556 }
557
558 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
559 if (drv->bdrv_getlength) {
560 bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
561 }
562#ifndef _WIN32
563 if (bs->is_temporary) {
564 unlink(filename);
565 }
566#endif
567 return 0;
568
569free_and_fail:
570 qemu_free(bs->opaque);
571 bs->opaque = NULL;
572 bs->drv = NULL;
573 return ret;
574}
575
576void bdrv_close(BlockDriverState *bs)
577{
578 if (bs->drv) {
579 if (bs->backing_hd)
580 bdrv_delete(bs->backing_hd);
581 bs->drv->bdrv_close(bs);
582 qemu_free(bs->opaque);
583#ifdef _WIN32

--- 1764 unchanged lines hidden ---
573void bdrv_close(BlockDriverState *bs)
574{
575 if (bs->drv) {
576 if (bs->backing_hd)
577 bdrv_delete(bs->backing_hd);
578 bs->drv->bdrv_close(bs);
579 qemu_free(bs->opaque);
580#ifdef _WIN32

--- 1764 unchanged lines hidden ---