block.c (facda5443f5a8676fb635b82ac1046ac6b6a67ce) block.c (fd17146cd93d1704cd96d7c2757b325fc7aac6fd)
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

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

527 }
528 }
529
530out:
531 g_free(cco.filename);
532 return ret;
533}
534
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

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

527 }
528 }
529
530out:
531 g_free(cco.filename);
532 return ret;
533}
534
535int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
535/**
536 * Helper function for bdrv_create_file_fallback(): Resize @blk to at
537 * least the given @minimum_size.
538 *
539 * On success, return @blk's actual length.
540 * Otherwise, return -errno.
541 */
542static int64_t create_file_fallback_truncate(BlockBackend *blk,
543 int64_t minimum_size, Error **errp)
536{
544{
537 BlockDriver *drv;
538 Error *local_err = NULL;
545 Error *local_err = NULL;
546 int64_t size;
539 int ret;
540
547 int ret;
548
549 ret = blk_truncate(blk, minimum_size, false, PREALLOC_MODE_OFF, &local_err);
550 if (ret < 0 && ret != -ENOTSUP) {
551 error_propagate(errp, local_err);
552 return ret;
553 }
554
555 size = blk_getlength(blk);
556 if (size < 0) {
557 error_free(local_err);
558 error_setg_errno(errp, -size,
559 "Failed to inquire the new image file's length");
560 return size;
561 }
562
563 if (size < minimum_size) {
564 /* Need to grow the image, but we failed to do that */
565 error_propagate(errp, local_err);
566 return -ENOTSUP;
567 }
568
569 error_free(local_err);
570 local_err = NULL;
571
572 return size;
573}
574
575/**
576 * Helper function for bdrv_create_file_fallback(): Zero the first
577 * sector to remove any potentially pre-existing image header.
578 */
579static int create_file_fallback_zero_first_sector(BlockBackend *blk,
580 int64_t current_size,
581 Error **errp)
582{
583 int64_t bytes_to_clear;
584 int ret;
585
586 bytes_to_clear = MIN(current_size, BDRV_SECTOR_SIZE);
587 if (bytes_to_clear) {
588 ret = blk_pwrite_zeroes(blk, 0, bytes_to_clear, BDRV_REQ_MAY_UNMAP);
589 if (ret < 0) {
590 error_setg_errno(errp, -ret,
591 "Failed to clear the new image's first sector");
592 return ret;
593 }
594 }
595
596 return 0;
597}
598
599static int bdrv_create_file_fallback(const char *filename, BlockDriver *drv,
600 QemuOpts *opts, Error **errp)
601{
602 BlockBackend *blk;
603 QDict *options = qdict_new();
604 int64_t size = 0;
605 char *buf = NULL;
606 PreallocMode prealloc;
607 Error *local_err = NULL;
608 int ret;
609
610 size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
611 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
612 prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
613 PREALLOC_MODE_OFF, &local_err);
614 g_free(buf);
615 if (local_err) {
616 error_propagate(errp, local_err);
617 return -EINVAL;
618 }
619
620 if (prealloc != PREALLOC_MODE_OFF) {
621 error_setg(errp, "Unsupported preallocation mode '%s'",
622 PreallocMode_str(prealloc));
623 return -ENOTSUP;
624 }
625
626 qdict_put_str(options, "driver", drv->format_name);
627
628 blk = blk_new_open(filename, NULL, options,
629 BDRV_O_RDWR | BDRV_O_RESIZE, errp);
630 if (!blk) {
631 error_prepend(errp, "Protocol driver '%s' does not support image "
632 "creation, and opening the image failed: ",
633 drv->format_name);
634 return -EINVAL;
635 }
636
637 size = create_file_fallback_truncate(blk, size, errp);
638 if (size < 0) {
639 ret = size;
640 goto out;
641 }
642
643 ret = create_file_fallback_zero_first_sector(blk, size, errp);
644 if (ret < 0) {
645 goto out;
646 }
647
648 ret = 0;
649out:
650 blk_unref(blk);
651 return ret;
652}
653
654int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp)
655{
656 BlockDriver *drv;
657
541 drv = bdrv_find_protocol(filename, true, errp);
542 if (drv == NULL) {
543 return -ENOENT;
544 }
545
658 drv = bdrv_find_protocol(filename, true, errp);
659 if (drv == NULL) {
660 return -ENOENT;
661 }
662
546 ret = bdrv_create(drv, filename, opts, &local_err);
547 error_propagate(errp, local_err);
548 return ret;
663 if (drv->bdrv_co_create_opts) {
664 return bdrv_create(drv, filename, opts, errp);
665 } else {
666 return bdrv_create_file_fallback(filename, drv, opts, errp);
667 }
549}
550
551/**
552 * Try to get @bs's logical and physical block size.
553 * On success, store them in @bsz struct and return 0.
554 * On failure return -errno.
555 * @bs must not be empty.
556 */

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

1439 .name = BDRV_OPT_FORCE_SHARE,
1440 .type = QEMU_OPT_BOOL,
1441 .help = "always accept other writers (default: off)",
1442 },
1443 { /* end of list */ }
1444 },
1445};
1446
668}
669
670/**
671 * Try to get @bs's logical and physical block size.
672 * On success, store them in @bsz struct and return 0.
673 * On failure return -errno.
674 * @bs must not be empty.
675 */

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

1558 .name = BDRV_OPT_FORCE_SHARE,
1559 .type = QEMU_OPT_BOOL,
1560 .help = "always accept other writers (default: off)",
1561 },
1562 { /* end of list */ }
1563 },
1564};
1565
1566static QemuOptsList fallback_create_opts = {
1567 .name = "fallback-create-opts",
1568 .head = QTAILQ_HEAD_INITIALIZER(fallback_create_opts.head),
1569 .desc = {
1570 {
1571 .name = BLOCK_OPT_SIZE,
1572 .type = QEMU_OPT_SIZE,
1573 .help = "Virtual disk size"
1574 },
1575 {
1576 .name = BLOCK_OPT_PREALLOC,
1577 .type = QEMU_OPT_STRING,
1578 .help = "Preallocation mode (allowed values: off)"
1579 },
1580 { /* end of list */ }
1581 }
1582};
1583
1447/*
1448 * Common part for opening disk images and files
1449 *
1450 * Removes all processed options from *options.
1451 */
1452static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
1453 QDict *options, Error **errp)
1454{

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

5767 }
5768
5769 if (!drv->create_opts) {
5770 error_setg(errp, "Format driver '%s' does not support image creation",
5771 drv->format_name);
5772 return;
5773 }
5774
1584/*
1585 * Common part for opening disk images and files
1586 *
1587 * Removes all processed options from *options.
1588 */
1589static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
1590 QDict *options, Error **errp)
1591{

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

5904 }
5905
5906 if (!drv->create_opts) {
5907 error_setg(errp, "Format driver '%s' does not support image creation",
5908 drv->format_name);
5909 return;
5910 }
5911
5775 if (!proto_drv->create_opts) {
5776 error_setg(errp, "Protocol driver '%s' does not support image creation",
5777 proto_drv->format_name);
5778 return;
5779 }
5780
5781 /* Create parameter list */
5782 create_opts = qemu_opts_append(create_opts, drv->create_opts);
5912 /* Create parameter list */
5913 create_opts = qemu_opts_append(create_opts, drv->create_opts);
5783 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
5914 if (proto_drv->create_opts) {
5915 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
5916 } else {
5917 create_opts = qemu_opts_append(create_opts, &fallback_create_opts);
5918 }
5784
5785 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5786
5787 /* Parse -o options */
5788 if (options) {
5789 qemu_opts_do_parse(opts, options, NULL, &local_err);
5790 if (local_err) {
5791 goto out;

--- 807 unchanged lines hidden ---
5919
5920 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
5921
5922 /* Parse -o options */
5923 if (options) {
5924 qemu_opts_do_parse(opts, options, NULL, &local_err);
5925 if (local_err) {
5926 goto out;

--- 807 unchanged lines hidden ---