block.c (35246a6825121f3881b8540beb731d582aa6d697) | block.c (07f076157475e1a8ea454e304e0ea5496d05b539) |
---|---|
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 --- 39 unchanged lines hidden (view full) --- 48 49static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 50static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 51 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 52 BlockDriverCompletionFunc *cb, void *opaque); 53static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 54 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 55 BlockDriverCompletionFunc *cb, void *opaque); | 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 --- 39 unchanged lines hidden (view full) --- 48 49static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 50static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 51 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 52 BlockDriverCompletionFunc *cb, void *opaque); 53static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 54 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 55 BlockDriverCompletionFunc *cb, void *opaque); |
56static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 57 BlockDriverCompletionFunc *cb, void *opaque); 58static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 59 BlockDriverCompletionFunc *cb, void *opaque); | |
60static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 61 int64_t sector_num, int nb_sectors, 62 QEMUIOVector *iov); 63static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 64 int64_t sector_num, int nb_sectors, 65 QEMUIOVector *iov); | 56static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 57 int64_t sector_num, int nb_sectors, 58 QEMUIOVector *iov); 59static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 60 int64_t sector_num, int nb_sectors, 61 QEMUIOVector *iov); |
66static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs); | |
67static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 68 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); 69static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 70 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); 71static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 72 int64_t sector_num, 73 QEMUIOVector *qiov, 74 int nb_sectors, --- 123 unchanged lines hidden (view full) --- 198 */ 199 if (!bdrv->bdrv_aio_readv) { 200 /* add AIO emulation layer */ 201 bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 202 bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 203 } 204 } 205 | 62static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 63 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); 64static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 65 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); 66static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 67 int64_t sector_num, 68 QEMUIOVector *qiov, 69 int nb_sectors, --- 123 unchanged lines hidden (view full) --- 193 */ 194 if (!bdrv->bdrv_aio_readv) { 195 /* add AIO emulation layer */ 196 bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 197 bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 198 } 199 } 200 |
206 if (!bdrv->bdrv_aio_flush) 207 bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 208 | |
209 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 210} 211 212/* create a new block device (by default it is empty) */ 213BlockDriverState *bdrv_new(const char *device_name) 214{ 215 BlockDriverState *bs; 216 --- 805 unchanged lines hidden (view full) --- 1022 1023static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 1024 int nb_sectors) 1025{ 1026 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1027 nb_sectors * BDRV_SECTOR_SIZE); 1028} 1029 | 201 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 202} 203 204/* create a new block device (by default it is empty) */ 205BlockDriverState *bdrv_new(const char *device_name) 206{ 207 BlockDriverState *bs; 208 --- 805 unchanged lines hidden (view full) --- 1014 1015static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 1016 int nb_sectors) 1017{ 1018 return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1019 nb_sectors * BDRV_SECTOR_SIZE); 1020} 1021 |
1030static inline bool bdrv_has_async_flush(BlockDriver *drv) 1031{ 1032 return drv->bdrv_aio_flush != bdrv_aio_flush_em; 1033} 1034 | |
1035typedef struct RwCo { 1036 BlockDriverState *bs; 1037 int64_t sector_num; 1038 int nb_sectors; 1039 QEMUIOVector *qiov; 1040 bool is_write; 1041 int ret; 1042} RwCo; --- 711 unchanged lines hidden (view full) --- 1754 } 1755} 1756 1757const char *bdrv_get_device_name(BlockDriverState *bs) 1758{ 1759 return bs->device_name; 1760} 1761 | 1022typedef struct RwCo { 1023 BlockDriverState *bs; 1024 int64_t sector_num; 1025 int nb_sectors; 1026 QEMUIOVector *qiov; 1027 bool is_write; 1028 int ret; 1029} RwCo; --- 711 unchanged lines hidden (view full) --- 1741 } 1742} 1743 1744const char *bdrv_get_device_name(BlockDriverState *bs) 1745{ 1746 return bs->device_name; 1747} 1748 |
1762int bdrv_flush(BlockDriverState *bs) 1763{ 1764 if (bs->open_flags & BDRV_O_NO_FLUSH) { 1765 return 0; 1766 } 1767 1768 if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) { 1769 return bdrv_co_flush_em(bs); 1770 } 1771 1772 if (bs->drv && bs->drv->bdrv_flush) { 1773 return bs->drv->bdrv_flush(bs); 1774 } 1775 1776 /* 1777 * Some block drivers always operate in either writethrough or unsafe mode 1778 * and don't support bdrv_flush therefore. Usually qemu doesn't know how 1779 * the server works (because the behaviour is hardcoded or depends on 1780 * server-side configuration), so we can't ensure that everything is safe 1781 * on disk. Returning an error doesn't work because that would break guests 1782 * even if the server operates in writethrough mode. 1783 * 1784 * Let's hope the user knows what he's doing. 1785 */ 1786 return 0; 1787} 1788 | |
1789void bdrv_flush_all(void) 1790{ 1791 BlockDriverState *bs; 1792 1793 QTAILQ_FOREACH(bs, &bdrv_states, list) { 1794 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) { 1795 bdrv_flush(bs); 1796 } --- 808 unchanged lines hidden (view full) --- 2605fail: 2606 for (i = 0; i < mcb->num_callbacks; i++) { 2607 reqs[i].error = -EIO; 2608 } 2609 g_free(mcb); 2610 return -1; 2611} 2612 | 1749void bdrv_flush_all(void) 1750{ 1751 BlockDriverState *bs; 1752 1753 QTAILQ_FOREACH(bs, &bdrv_states, list) { 1754 if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) { 1755 bdrv_flush(bs); 1756 } --- 808 unchanged lines hidden (view full) --- 2565fail: 2566 for (i = 0; i < mcb->num_callbacks; i++) { 2567 reqs[i].error = -EIO; 2568 } 2569 g_free(mcb); 2570 return -1; 2571} 2572 |
2613BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2614 BlockDriverCompletionFunc *cb, void *opaque) 2615{ 2616 BlockDriver *drv = bs->drv; 2617 2618 trace_bdrv_aio_flush(bs, opaque); 2619 2620 if (bs->open_flags & BDRV_O_NO_FLUSH) { 2621 return bdrv_aio_noop_em(bs, cb, opaque); 2622 } 2623 2624 if (!drv) 2625 return NULL; 2626 return drv->bdrv_aio_flush(bs, cb, opaque); 2627} 2628 | |
2629void bdrv_aio_cancel(BlockDriverAIOCB *acb) 2630{ 2631 acb->pool->cancel(acb); 2632} 2633 2634 2635/**************************************************************/ 2636/* async block device emulation */ --- 143 unchanged lines hidden (view full) --- 2780 acb->is_write = is_write; 2781 2782 co = qemu_coroutine_create(bdrv_co_do_rw); 2783 qemu_coroutine_enter(co, acb); 2784 2785 return &acb->common; 2786} 2787 | 2573void bdrv_aio_cancel(BlockDriverAIOCB *acb) 2574{ 2575 acb->pool->cancel(acb); 2576} 2577 2578 2579/**************************************************************/ 2580/* async block device emulation */ --- 143 unchanged lines hidden (view full) --- 2724 acb->is_write = is_write; 2725 2726 co = qemu_coroutine_create(bdrv_co_do_rw); 2727 qemu_coroutine_enter(co, acb); 2728 2729 return &acb->common; 2730} 2731 |
2788static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2789 BlockDriverCompletionFunc *cb, void *opaque) | 2732static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
2790{ | 2733{ |
2791 BlockDriverAIOCBSync *acb; | 2734 BlockDriverAIOCBCoroutine *acb = opaque; 2735 BlockDriverState *bs = acb->common.bs; |
2792 | 2736 |
2793 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2794 acb->is_write = 1; /* don't bounce in the completion hadler */ 2795 acb->qiov = NULL; 2796 acb->bounce = NULL; 2797 acb->ret = 0; 2798 2799 if (!acb->bh) 2800 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2801 2802 bdrv_flush(bs); | 2737 acb->req.error = bdrv_co_flush(bs); 2738 acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
2803 qemu_bh_schedule(acb->bh); | 2739 qemu_bh_schedule(acb->bh); |
2804 return &acb->common; | |
2805} 2806 | 2740} 2741 |
2807static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, | 2742BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
2808 BlockDriverCompletionFunc *cb, void *opaque) 2809{ | 2743 BlockDriverCompletionFunc *cb, void *opaque) 2744{ |
2810 BlockDriverAIOCBSync *acb; | 2745 trace_bdrv_aio_flush(bs, opaque); |
2811 | 2746 |
2812 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2813 acb->is_write = 1; /* don't bounce in the completion handler */ 2814 acb->qiov = NULL; 2815 acb->bounce = NULL; 2816 acb->ret = 0; | 2747 Coroutine *co; 2748 BlockDriverAIOCBCoroutine *acb; |
2817 | 2749 |
2818 if (!acb->bh) { 2819 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2820 } | 2750 acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 2751 co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 2752 qemu_coroutine_enter(co, acb); |
2821 | 2753 |
2822 qemu_bh_schedule(acb->bh); | |
2823 return &acb->common; 2824} 2825 2826void bdrv_init(void) 2827{ 2828 module_call_init(MODULE_INIT_BLOCK); 2829} 2830 --- 80 unchanged lines hidden (view full) --- 2911 2912static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 2913 int64_t sector_num, int nb_sectors, 2914 QEMUIOVector *iov) 2915{ 2916 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 2917} 2918 | 2754 return &acb->common; 2755} 2756 2757void bdrv_init(void) 2758{ 2759 module_call_init(MODULE_INIT_BLOCK); 2760} 2761 --- 80 unchanged lines hidden (view full) --- 2842 2843static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 2844 int64_t sector_num, int nb_sectors, 2845 QEMUIOVector *iov) 2846{ 2847 return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 2848} 2849 |
2919static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs) | 2850static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
2920{ | 2851{ |
2921 CoroutineIOCompletion co = { 2922 .coroutine = qemu_coroutine_self(), | 2852 RwCo *rwco = opaque; 2853 2854 rwco->ret = bdrv_co_flush(rwco->bs); 2855} 2856 2857int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 2858{ 2859 if (bs->open_flags & BDRV_O_NO_FLUSH) { 2860 return 0; 2861 } else if (!bs->drv) { 2862 return 0; 2863 } else if (bs->drv->bdrv_co_flush) { 2864 return bs->drv->bdrv_co_flush(bs); 2865 } else if (bs->drv->bdrv_aio_flush) { 2866 BlockDriverAIOCB *acb; 2867 CoroutineIOCompletion co = { 2868 .coroutine = qemu_coroutine_self(), 2869 }; 2870 2871 acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 2872 if (acb == NULL) { 2873 return -EIO; 2874 } else { 2875 qemu_coroutine_yield(); 2876 return co.ret; 2877 } 2878 } else if (bs->drv->bdrv_flush) { 2879 return bs->drv->bdrv_flush(bs); 2880 } else { 2881 /* 2882 * Some block drivers always operate in either writethrough or unsafe 2883 * mode and don't support bdrv_flush therefore. Usually qemu doesn't 2884 * know how the server works (because the behaviour is hardcoded or 2885 * depends on server-side configuration), so we can't ensure that 2886 * everything is safe on disk. Returning an error doesn't work because 2887 * that would break guests even if the server operates in writethrough 2888 * mode. 2889 * 2890 * Let's hope the user knows what he's doing. 2891 */ 2892 return 0; 2893 } 2894} 2895 2896int bdrv_flush(BlockDriverState *bs) 2897{ 2898 Coroutine *co; 2899 RwCo rwco = { 2900 .bs = bs, 2901 .ret = NOT_DONE, |
2923 }; | 2902 }; |
2924 BlockDriverAIOCB *acb; | |
2925 | 2903 |
2926 acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 2927 if (!acb) { 2928 return -EIO; | 2904 if (qemu_in_coroutine()) { 2905 /* Fast-path if already in coroutine context */ 2906 bdrv_flush_co_entry(&rwco); 2907 } else { 2908 co = qemu_coroutine_create(bdrv_flush_co_entry); 2909 qemu_coroutine_enter(co, &rwco); 2910 while (rwco.ret == NOT_DONE) { 2911 qemu_aio_wait(); 2912 } |
2929 } | 2913 } |
2930 qemu_coroutine_yield(); 2931 return co.ret; | 2914 2915 return rwco.ret; |
2932} 2933 2934/**************************************************************/ 2935/* removable device support */ 2936 2937/** 2938 * Return TRUE if the media is present 2939 */ --- 341 unchanged lines hidden --- | 2916} 2917 2918/**************************************************************/ 2919/* removable device support */ 2920 2921/** 2922 * Return TRUE if the media is present 2923 */ --- 341 unchanged lines hidden --- |