block.c (2851810223a6f2a3f6c00613477feb6e33c10d34) block.c (8e2160e2c75522554647e197e8b61622d6cf076f)
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

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

690 * like a backing file)
691 */
692static int bdrv_temp_snapshot_flags(int flags)
693{
694 return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
695}
696
697/*
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

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

690 * like a backing file)
691 */
692static int bdrv_temp_snapshot_flags(int flags)
693{
694 return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY;
695}
696
697/*
698 * Returns the flags that bs->file should get if a protocol driver is expected,
699 * based on the given flags for the parent BDS
698 * Returns the options and flags that bs->file should get if a protocol driver
699 * is expected, based on the given options and flags for the parent BDS
700 */
700 */
701static int bdrv_inherited_flags(int flags)
701static void bdrv_inherited_options(int *child_flags, QDict *child_options,
702 int parent_flags, QDict *parent_options)
702{
703{
704 int flags = parent_flags;
705
703 /* Enable protocol handling, disable format probing for bs->file */
704 flags |= BDRV_O_PROTOCOL;
705
706 /* Our block drivers take care to send flushes and respect unmap policy,
707 * so we can enable both unconditionally on lower layers. */
708 flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP;
709
710 /* Clear flags that only apply to the top layer */
711 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
712
706 /* Enable protocol handling, disable format probing for bs->file */
707 flags |= BDRV_O_PROTOCOL;
708
709 /* Our block drivers take care to send flushes and respect unmap policy,
710 * so we can enable both unconditionally on lower layers. */
711 flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP;
712
713 /* Clear flags that only apply to the top layer */
714 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ);
715
713 return flags;
716 *child_flags = flags;
714}
715
716const BdrvChildRole child_file = {
717}
718
719const BdrvChildRole child_file = {
717 .inherit_flags = bdrv_inherited_flags,
720 .inherit_options = bdrv_inherited_options,
718};
719
720/*
721};
722
723/*
721 * Returns the flags that bs->file should get if the use of formats (and not
722 * only protocols) is permitted for it, based on the given flags for the parent
723 * BDS
724 * Returns the options and flags that bs->file should get if the use of formats
725 * (and not only protocols) is permitted for it, based on the given options and
726 * flags for the parent BDS
724 */
727 */
725static int bdrv_inherited_fmt_flags(int parent_flags)
728static void bdrv_inherited_fmt_options(int *child_flags, QDict *child_options,
729 int parent_flags, QDict *parent_options)
726{
730{
727 int flags = child_file.inherit_flags(parent_flags);
728 return flags & ~BDRV_O_PROTOCOL;
731 child_file.inherit_options(child_flags, child_options,
732 parent_flags, parent_options);
733
734 *child_flags &= ~BDRV_O_PROTOCOL;
729}
730
731const BdrvChildRole child_format = {
735}
736
737const BdrvChildRole child_format = {
732 .inherit_flags = bdrv_inherited_fmt_flags,
738 .inherit_options = bdrv_inherited_fmt_options,
733};
734
735/*
739};
740
741/*
736 * Returns the flags that bs->backing should get, based on the given flags
737 * for the parent BDS
742 * Returns the options and flags that bs->backing should get, based on the
743 * given options and flags for the parent BDS
738 */
744 */
739static int bdrv_backing_flags(int flags)
745static void bdrv_backing_options(int *child_flags, QDict *child_options,
746 int parent_flags, QDict *parent_options)
740{
747{
748 int flags = parent_flags;
749
741 /* backing files always opened read-only */
742 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
743
744 /* snapshot=on is handled on the top layer */
745 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
746
750 /* backing files always opened read-only */
751 flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
752
753 /* snapshot=on is handled on the top layer */
754 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
755
747 return flags;
756 *child_flags = flags;
748}
749
750static const BdrvChildRole child_backing = {
757}
758
759static const BdrvChildRole child_backing = {
751 .inherit_flags = bdrv_backing_flags,
760 .inherit_options = bdrv_backing_options,
752};
753
754static int bdrv_open_flags(BlockDriverState *bs, int flags)
755{
756 int open_flags = flags | BDRV_O_CACHE_WB;
757
758 /*
759 * Clear flags that are internal to the block layer before opening the

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

1475
1476 /* NULL means an empty set of options */
1477 if (options == NULL) {
1478 options = qdict_new();
1479 }
1480
1481 if (child_role) {
1482 bs->inherits_from = parent;
761};
762
763static int bdrv_open_flags(BlockDriverState *bs, int flags)
764{
765 int open_flags = flags | BDRV_O_CACHE_WB;
766
767 /*
768 * Clear flags that are internal to the block layer before opening the

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

1484
1485 /* NULL means an empty set of options */
1486 if (options == NULL) {
1487 options = qdict_new();
1488 }
1489
1490 if (child_role) {
1491 bs->inherits_from = parent;
1483 flags = child_role->inherit_flags(parent->open_flags);
1492 child_role->inherit_options(&flags, options,
1493 parent->open_flags, parent->options);
1484 }
1485
1486 ret = bdrv_fill_options(&options, &filename, &flags, &local_err);
1487 if (local_err) {
1488 goto fail;
1489 }
1490
1491 bs->open_flags = flags;

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

1513
1514 /* Open image file without format layer */
1515 if ((flags & BDRV_O_PROTOCOL) == 0) {
1516 if (flags & BDRV_O_RDWR) {
1517 flags |= BDRV_O_ALLOW_RDWR;
1518 }
1519 if (flags & BDRV_O_SNAPSHOT) {
1520 snapshot_flags = bdrv_temp_snapshot_flags(flags);
1494 }
1495
1496 ret = bdrv_fill_options(&options, &filename, &flags, &local_err);
1497 if (local_err) {
1498 goto fail;
1499 }
1500
1501 bs->open_flags = flags;

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

1523
1524 /* Open image file without format layer */
1525 if ((flags & BDRV_O_PROTOCOL) == 0) {
1526 if (flags & BDRV_O_RDWR) {
1527 flags |= BDRV_O_ALLOW_RDWR;
1528 }
1529 if (flags & BDRV_O_SNAPSHOT) {
1530 snapshot_flags = bdrv_temp_snapshot_flags(flags);
1521 flags = bdrv_backing_flags(flags);
1531 bdrv_backing_options(&flags, options, flags, options);
1522 }
1523
1524 bs->open_flags = flags;
1525
1526 file = bdrv_open_child(filename, options, "file", bs,
1527 &child_file, true, &local_err);
1528 if (local_err) {
1529 ret = -EINVAL;

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

1716 options = qdict_new();
1717 }
1718
1719 /*
1720 * Precedence of options:
1721 * 1. Explicitly passed in options (highest)
1722 * 2. TODO Set in flags (only for top level)
1723 * 3. TODO Retained from explicitly set options of bs
1532 }
1533
1534 bs->open_flags = flags;
1535
1536 file = bdrv_open_child(filename, options, "file", bs,
1537 &child_file, true, &local_err);
1538 if (local_err) {
1539 ret = -EINVAL;

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

1726 options = qdict_new();
1727 }
1728
1729 /*
1730 * Precedence of options:
1731 * 1. Explicitly passed in options (highest)
1732 * 2. TODO Set in flags (only for top level)
1733 * 3. TODO Retained from explicitly set options of bs
1724 * 4. TODO Inherited from parent node
1734 * 4. Inherited from parent node
1725 * 5. Retained from effective options of bs
1726 */
1727
1728 /* Inherit from parent node */
1729 if (parent_options) {
1730 assert(!flags);
1735 * 5. Retained from effective options of bs
1736 */
1737
1738 /* Inherit from parent node */
1739 if (parent_options) {
1740 assert(!flags);
1731 flags = role->inherit_flags(parent_flags);
1741 role->inherit_options(&flags, options, parent_flags, parent_options);
1732 }
1733
1734 /* Old values are used for options that aren't set yet */
1735 old_options = qdict_clone_shallow(bs->options);
1736 bdrv_join_options(bs, options, old_options);
1737 QDECREF(old_options);
1738
1739 /* bdrv_open() masks this flag out */

--- 2423 unchanged lines hidden ---
1742 }
1743
1744 /* Old values are used for options that aren't set yet */
1745 old_options = qdict_clone_shallow(bs->options);
1746 bdrv_join_options(bs, options, old_options);
1747 QDECREF(old_options);
1748
1749 /* bdrv_open() masks this flag out */

--- 2423 unchanged lines hidden ---