block.c (3121fb45b004ea85fb3589368ea699f32e6ef832) block.c (148eb13c84cccd0eedd6e59f90e0151bd7bac9fa)
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

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

234/* Returns whether the image file is opened as read-only. Note that this can
235 * return false and writing to the image file is still not possible because the
236 * image is inactivated. */
237bool bdrv_is_read_only(BlockDriverState *bs)
238{
239 return bs->read_only;
240}
241
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

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

234/* Returns whether the image file is opened as read-only. Note that this can
235 * return false and writing to the image file is still not possible because the
236 * image is inactivated. */
237bool bdrv_is_read_only(BlockDriverState *bs)
238{
239 return bs->read_only;
240}
241
242/* Returns whether the image file can be written to right now */
243bool bdrv_is_writable(BlockDriverState *bs)
244{
245 return !bdrv_is_read_only(bs) && !(bs->open_flags & BDRV_O_INACTIVE);
246}
247
248int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
249 bool ignore_allow_rdw, Error **errp)
250{
251 /* Do not set read_only if copy_on_read is enabled */
252 if (bs->copy_on_read && read_only) {
253 error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled",
254 bdrv_get_device_or_node_name(bs));
255 return -EINVAL;

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

1532}
1533
1534static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q,
1535 uint64_t perm, uint64_t shared,
1536 GSList *ignore_children, Error **errp);
1537static void bdrv_child_abort_perm_update(BdrvChild *c);
1538static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared);
1539
242int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
243 bool ignore_allow_rdw, Error **errp)
244{
245 /* Do not set read_only if copy_on_read is enabled */
246 if (bs->copy_on_read && read_only) {
247 error_setg(errp, "Can't set node '%s' to r/o with copy-on-read enabled",
248 bdrv_get_device_or_node_name(bs));
249 return -EINVAL;

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

1526}
1527
1528static int bdrv_child_check_perm(BdrvChild *c, BlockReopenQueue *q,
1529 uint64_t perm, uint64_t shared,
1530 GSList *ignore_children, Error **errp);
1531static void bdrv_child_abort_perm_update(BdrvChild *c);
1532static void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared);
1533
1534typedef struct BlockReopenQueueEntry {
1535 bool prepared;
1536 BDRVReopenState state;
1537 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1538} BlockReopenQueueEntry;
1539
1540/*
1541 * Return the flags that @bs will have after the reopens in @q have
1542 * successfully completed. If @q is NULL (or @bs is not contained in @q),
1543 * return the current flags.
1544 */
1545static int bdrv_reopen_get_flags(BlockReopenQueue *q, BlockDriverState *bs)
1546{
1547 BlockReopenQueueEntry *entry;
1548
1549 if (q != NULL) {
1550 QSIMPLEQ_FOREACH(entry, q, entry) {
1551 if (entry->state.bs == bs) {
1552 return entry->state.flags;
1553 }
1554 }
1555 }
1556
1557 return bs->open_flags;
1558}
1559
1560/* Returns whether the image file can be written to after the reopen queue @q
1561 * has been successfully applied, or right now if @q is NULL. */
1562static bool bdrv_is_writable(BlockDriverState *bs, BlockReopenQueue *q)
1563{
1564 int flags = bdrv_reopen_get_flags(q, bs);
1565
1566 return (flags & (BDRV_O_RDWR | BDRV_O_INACTIVE)) == BDRV_O_RDWR;
1567}
1568
1540static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
1541 BdrvChild *c, const BdrvChildRole *role,
1542 BlockReopenQueue *reopen_queue,
1543 uint64_t parent_perm, uint64_t parent_shared,
1544 uint64_t *nperm, uint64_t *nshared)
1545{
1546 if (bs->drv && bs->drv->bdrv_child_perm) {
1547 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,

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

1569 GSList *ignore_children, Error **errp)
1570{
1571 BlockDriver *drv = bs->drv;
1572 BdrvChild *c;
1573 int ret;
1574
1575 /* Write permissions never work with read-only images */
1576 if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&
1569static void bdrv_child_perm(BlockDriverState *bs, BlockDriverState *child_bs,
1570 BdrvChild *c, const BdrvChildRole *role,
1571 BlockReopenQueue *reopen_queue,
1572 uint64_t parent_perm, uint64_t parent_shared,
1573 uint64_t *nperm, uint64_t *nshared)
1574{
1575 if (bs->drv && bs->drv->bdrv_child_perm) {
1576 bs->drv->bdrv_child_perm(bs, c, role, reopen_queue,

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

1598 GSList *ignore_children, Error **errp)
1599{
1600 BlockDriver *drv = bs->drv;
1601 BdrvChild *c;
1602 int ret;
1603
1604 /* Write permissions never work with read-only images */
1605 if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&
1577 !bdrv_is_writable(bs))
1606 !bdrv_is_writable(bs, q))
1578 {
1579 error_setg(errp, "Block node is read-only");
1580 return -EPERM;
1581 }
1582
1583 /* Check this node */
1584 if (!drv) {
1585 return 0;

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

1859
1860 if (!backing) {
1861 /* Apart from the modifications below, the same permissions are
1862 * forwarded and left alone as for filters */
1863 bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared,
1864 &perm, &shared);
1865
1866 /* Format drivers may touch metadata even if the guest doesn't write */
1607 {
1608 error_setg(errp, "Block node is read-only");
1609 return -EPERM;
1610 }
1611
1612 /* Check this node */
1613 if (!drv) {
1614 return 0;

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

1888
1889 if (!backing) {
1890 /* Apart from the modifications below, the same permissions are
1891 * forwarded and left alone as for filters */
1892 bdrv_filter_default_perms(bs, c, role, reopen_queue, perm, shared,
1893 &perm, &shared);
1894
1895 /* Format drivers may touch metadata even if the guest doesn't write */
1867 /* TODO Take flags from reopen_queue */
1868 if (bdrv_is_writable(bs)) {
1896 if (bdrv_is_writable(bs, reopen_queue)) {
1869 perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
1870 }
1871
1872 /* bs->file always needs to be consistent because of the metadata. We
1873 * can never allow other users to resize or write to it. */
1874 perm |= BLK_PERM_CONSISTENT_READ;
1875 shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
1876 } else {

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

2637
2638BlockDriverState *bdrv_open(const char *filename, const char *reference,
2639 QDict *options, int flags, Error **errp)
2640{
2641 return bdrv_open_inherit(filename, reference, options, flags, NULL,
2642 NULL, errp);
2643}
2644
1897 perm |= BLK_PERM_WRITE | BLK_PERM_RESIZE;
1898 }
1899
1900 /* bs->file always needs to be consistent because of the metadata. We
1901 * can never allow other users to resize or write to it. */
1902 perm |= BLK_PERM_CONSISTENT_READ;
1903 shared &= ~(BLK_PERM_WRITE | BLK_PERM_RESIZE);
1904 } else {

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

2665
2666BlockDriverState *bdrv_open(const char *filename, const char *reference,
2667 QDict *options, int flags, Error **errp)
2668{
2669 return bdrv_open_inherit(filename, reference, options, flags, NULL,
2670 NULL, errp);
2671}
2672
2645typedef struct BlockReopenQueueEntry {
2646 bool prepared;
2647 BDRVReopenState state;
2648 QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
2649} BlockReopenQueueEntry;
2650
2651/*
2652 * Adds a BlockDriverState to a simple queue for an atomic, transactional
2653 * reopen of multiple devices.
2654 *
2655 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
2656 * already performed, or alternatively may be NULL a new BlockReopenQueue will
2657 * be created and initialized. This newly created BlockReopenQueue should be
2658 * passed back in for subsequent calls that are intended to be of the same

--- 2298 unchanged lines hidden ---
2673/*
2674 * Adds a BlockDriverState to a simple queue for an atomic, transactional
2675 * reopen of multiple devices.
2676 *
2677 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
2678 * already performed, or alternatively may be NULL a new BlockReopenQueue will
2679 * be created and initialized. This newly created BlockReopenQueue should be
2680 * passed back in for subsequent calls that are intended to be of the same

--- 2298 unchanged lines hidden ---