void bdrv_close(BlockDriverState *bs)

{

    BdrvAioNotifier *ban, *ban_next;



    if (bs->job) {

        block_job_cancel_sync(bs->job);

    }



    /* Disable I/O limits and drain all pending throttled requests */

    if (bs->io_limits_enabled) {

        bdrv_io_limits_disable(bs);

    }



    bdrv_drain(bs); /* complete I/O */

    bdrv_flush(bs);

    bdrv_drain(bs); /* in case flush left pending I/O */

    notifier_list_notify(&bs->close_notifiers, bs);



    if (bs->drv) {

        BdrvChild *child, *next;



        bs->drv->bdrv_close(bs);

        bs->drv = NULL;



        bdrv_set_backing_hd(bs, NULL);



        if (bs->file != NULL) {

            bdrv_unref_child(bs, bs->file);

            bs->file = NULL;

        }



        QLIST_FOREACH_SAFE(child, &bs->children, next, next) {

            /* TODO Remove bdrv_unref() from drivers' close function and use

             * bdrv_unref_child() here */

            if (child->bs->inherits_from == bs) {

                child->bs->inherits_from = NULL;

            }

            bdrv_detach_child(child);

        }



        g_free(bs->opaque);

        bs->opaque = NULL;

        bs->copy_on_read = 0;

        bs->backing_file[0] = '\0';

        bs->backing_format[0] = '\0';

        bs->total_sectors = 0;

        bs->encrypted = 0;

        bs->valid_key = 0;

        bs->sg = 0;

        bs->zero_beyond_eof = false;

        QDECREF(bs->options);

        bs->options = NULL;

        QDECREF(bs->full_open_options);

        bs->full_open_options = NULL;

    }



    if (bs->blk) {

        blk_dev_change_media_cb(bs->blk, false);

    }



    QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {

        g_free(ban);

    }

    QLIST_INIT(&bs->aio_notifiers);

}

void bdrv_close(BlockDriverState *bs)

{

    BdrvAioNotifier *ban, *ban_next;



    if (bs->job) {

        block_job_cancel_sync(bs->job);

    }



    

    if (bs->io_limits_enabled) {

        bdrv_io_limits_disable(bs);

    }



    bdrv_drain(bs); 

    bdrv_flush(bs);

    bdrv_drain(bs); 

    notifier_list_notify(&bs->close_notifiers, bs);



    if (bs->drv) {

        BdrvChild *child, *next;



        bs->drv->bdrv_close(bs);

        bs->drv = NULL;



        bdrv_set_backing_hd(bs, NULL);



        if (bs->file != NULL) {

            bdrv_unref_child(bs, bs->file);

            bs->file = NULL;

        }



        QLIST_FOREACH_SAFE(child, &bs->children, next, next) {

            



            if (child->bs->inherits_from == bs) {

                child->bs->inherits_from = NULL;

            }

            bdrv_detach_child(child);

        }



        g_free(bs->opaque);

        bs->opaque = NULL;

        bs->copy_on_read = 0;

        bs->backing_file[0] = '\0';

        bs->backing_format[0] = '\0';

        bs->total_sectors = 0;

        bs->encrypted = 0;

        bs->valid_key = 0;

        bs->sg = 0;

        bs->zero_beyond_eof = false;

        QDECREF(bs->options);

        bs->options = NULL;

        QDECREF(bs->full_open_options);

        bs->full_open_options = NULL;

    }



    if (bs->blk) {

        blk_dev_change_media_cb(bs->blk, false);

    }



    QLIST_FOREACH_SAFE(ban, &bs->aio_notifiers, list, ban_next) {

        g_free(ban);

    }

    QLIST_INIT(&bs->aio_notifiers);

}

void FUNC_0(BlockDriverState *VAR_0)

{

    BdrvAioNotifier *ban, *ban_next;



    if (VAR_0->job) {

        block_job_cancel_sync(VAR_0->job);

    }



    

    if (VAR_0->io_limits_enabled) {

        bdrv_io_limits_disable(VAR_0);

    }



    bdrv_drain(VAR_0); 

    bdrv_flush(VAR_0);

    bdrv_drain(VAR_0); 

    notifier_list_notify(&VAR_0->close_notifiers, VAR_0);



    if (VAR_0->drv) {

        BdrvChild *child, *next;



        VAR_0->drv->FUNC_0(VAR_0);

        VAR_0->drv = NULL;



        bdrv_set_backing_hd(VAR_0, NULL);



        if (VAR_0->file != NULL) {

            bdrv_unref_child(VAR_0, VAR_0->file);

            VAR_0->file = NULL;

        }



        QLIST_FOREACH_SAFE(child, &VAR_0->children, next, next) {

            



            if (child->VAR_0->inherits_from == VAR_0) {

                child->VAR_0->inherits_from = NULL;

            }

            bdrv_detach_child(child);

        }



        g_free(VAR_0->opaque);

        VAR_0->opaque = NULL;

        VAR_0->copy_on_read = 0;

        VAR_0->backing_file[0] = '\0';

        VAR_0->backing_format[0] = '\0';

        VAR_0->total_sectors = 0;

        VAR_0->encrypted = 0;

        VAR_0->valid_key = 0;

        VAR_0->sg = 0;

        VAR_0->zero_beyond_eof = false;

        QDECREF(VAR_0->options);

        VAR_0->options = NULL;

        QDECREF(VAR_0->full_open_options);

        VAR_0->full_open_options = NULL;

    }



    if (VAR_0->blk) {

        blk_dev_change_media_cb(VAR_0->blk, false);

    }



    QLIST_FOREACH_SAFE(ban, &VAR_0->aio_notifiers, list, ban_next) {

        g_free(ban);

    }

    QLIST_INIT(&VAR_0->aio_notifiers);

}

void FUNC_0(BlockDriverState *VAR_0)
{ BdrvAioNotifier *ban, *ban_next; if (VAR_0->job) { block_job_cancel_sync(VAR_0->job); } if (VAR_0->io_limits_enabled) { bdrv_io_limits_disable(VAR_0); } bdrv_drain(VAR_0); bdrv_flush(VAR_0); bdrv_drain(VAR_0); notifier_list_notify(&VAR_0->close_notifiers, VAR_0); if (VAR_0->drv) { BdrvChild *child, *next; VAR_0->drv->FUNC_0(VAR_0); VAR_0->drv = NULL; bdrv_set_backing_hd(VAR_0, NULL); if (VAR_0->file != NULL) { bdrv_unref_child(VAR_0, VAR_0->file); VAR_0->file = NULL; } QLIST_FOREACH_SAFE(child, &VAR_0->children, next, next) { if (child->VAR_0->inherits_from == VAR_0) { child->VAR_0->inherits_from = NULL; } bdrv_detach_child(child); } g_free(VAR_0->opaque); VAR_0->opaque = NULL; VAR_0->copy_on_read = 0; VAR_0->backing_file[0] = '\0'; VAR_0->backing_format[0] = '\0'; VAR_0->total_sectors = 0; VAR_0->encrypted = 0; VAR_0->valid_key = 0; VAR_0->sg = 0; VAR_0->zero_beyond_eof = false; QDECREF(VAR_0->options); VAR_0->options = NULL; QDECREF(VAR_0->full_open_options); VAR_0->full_open_options = NULL; } if (VAR_0->blk) { blk_dev_change_media_cb(VAR_0->blk, false); } QLIST_FOREACH_SAFE(ban, &VAR_0->aio_notifiers, list, ban_next) { g_free(ban); } QLIST_INIT(&VAR_0->aio_notifiers); }