block.c (9fcb025146676ab376e6159b58f5a5ddb67bf03c) | block.c (4f6fd3491cf0f768b135ed2e242bd1d1d2a2efec) |
---|---|
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 --- 889 unchanged lines hidden (view full) --- 898 *pbs = bs; 899 return 0; 900 901fail: 902 QDECREF(options); 903 if (!bs->drv) { 904 QDECREF(bs->options); 905 } | 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 --- 889 unchanged lines hidden (view full) --- 898 *pbs = bs; 899 return 0; 900 901fail: 902 QDECREF(options); 903 if (!bs->drv) { 904 QDECREF(bs->options); 905 } |
906 bdrv_delete(bs); | 906 bdrv_unref(bs); |
907 return ret; 908} 909 910/* 911 * Opens the backing file for a BlockDriverState if not yet open 912 * 913 * options is a QDict of options to pass to the block drivers, or NULL for an 914 * empty set of options. The reference to the QDict is transferred to this --- 34 unchanged lines hidden (view full) --- 949 950 /* backing files always opened read-only */ 951 back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); 952 953 ret = bdrv_open(bs->backing_hd, 954 *backing_filename ? backing_filename : NULL, options, 955 back_flags, back_drv); 956 if (ret < 0) { | 907 return ret; 908} 909 910/* 911 * Opens the backing file for a BlockDriverState if not yet open 912 * 913 * options is a QDict of options to pass to the block drivers, or NULL for an 914 * empty set of options. The reference to the QDict is transferred to this --- 34 unchanged lines hidden (view full) --- 949 950 /* backing files always opened read-only */ 951 back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); 952 953 ret = bdrv_open(bs->backing_hd, 954 *backing_filename ? backing_filename : NULL, options, 955 back_flags, back_drv); 956 if (ret < 0) { |
957 bdrv_delete(bs->backing_hd); | 957 bdrv_unref(bs->backing_hd); |
958 bs->backing_hd = NULL; 959 bs->open_flags |= BDRV_O_NO_BACKING; 960 return ret; 961 } 962 return 0; 963} 964 965static void extract_subqdict(QDict *src, QDict **dst, const char *start) --- 58 unchanged lines hidden (view full) --- 1024 1025 /* if snapshot, we create a temporary backing file and open it 1026 instead of opening 'filename' directly */ 1027 1028 /* if there is a backing file, use it */ 1029 bs1 = bdrv_new(""); 1030 ret = bdrv_open(bs1, filename, NULL, 0, drv); 1031 if (ret < 0) { | 958 bs->backing_hd = NULL; 959 bs->open_flags |= BDRV_O_NO_BACKING; 960 return ret; 961 } 962 return 0; 963} 964 965static void extract_subqdict(QDict *src, QDict **dst, const char *start) --- 58 unchanged lines hidden (view full) --- 1024 1025 /* if snapshot, we create a temporary backing file and open it 1026 instead of opening 'filename' directly */ 1027 1028 /* if there is a backing file, use it */ 1029 bs1 = bdrv_new(""); 1030 ret = bdrv_open(bs1, filename, NULL, 0, drv); 1031 if (ret < 0) { |
1032 bdrv_delete(bs1); | 1032 bdrv_unref(bs1); |
1033 goto fail; 1034 } 1035 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 1036 | 1033 goto fail; 1034 } 1035 total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 1036 |
1037 bdrv_delete(bs1); | 1037 bdrv_unref(bs1); |
1038 1039 ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1040 if (ret < 0) { 1041 goto fail; 1042 } 1043 1044 /* Real path is meaningless for protocols */ 1045 if (path_has_protocol(filename)) { --- 57 unchanged lines hidden (view full) --- 1103 1104 /* Open the image */ 1105 ret = bdrv_open_common(bs, file, options, flags, drv); 1106 if (ret < 0) { 1107 goto unlink_and_fail; 1108 } 1109 1110 if (bs->file != file) { | 1038 1039 ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1040 if (ret < 0) { 1041 goto fail; 1042 } 1043 1044 /* Real path is meaningless for protocols */ 1045 if (path_has_protocol(filename)) { --- 57 unchanged lines hidden (view full) --- 1103 1104 /* Open the image */ 1105 ret = bdrv_open_common(bs, file, options, flags, drv); 1106 if (ret < 0) { 1107 goto unlink_and_fail; 1108 } 1109 1110 if (bs->file != file) { |
1111 bdrv_delete(file); | 1111 bdrv_unref(file); |
1112 file = NULL; 1113 } 1114 1115 /* If there is a backing file, use it */ 1116 if ((flags & BDRV_O_NO_BACKING) == 0) { 1117 QDict *backing_options; 1118 1119 extract_subqdict(options, &backing_options, "backing."); --- 18 unchanged lines hidden (view full) --- 1138 if (!bdrv_key_required(bs)) { 1139 bdrv_dev_change_media_cb(bs, true); 1140 } 1141 1142 return 0; 1143 1144unlink_and_fail: 1145 if (file != NULL) { | 1112 file = NULL; 1113 } 1114 1115 /* If there is a backing file, use it */ 1116 if ((flags & BDRV_O_NO_BACKING) == 0) { 1117 QDict *backing_options; 1118 1119 extract_subqdict(options, &backing_options, "backing."); --- 18 unchanged lines hidden (view full) --- 1138 if (!bdrv_key_required(bs)) { 1139 bdrv_dev_change_media_cb(bs, true); 1140 } 1141 1142 return 0; 1143 1144unlink_and_fail: 1145 if (file != NULL) { |
1146 bdrv_delete(file); | 1146 bdrv_unref(file); |
1147 } 1148 if (bs->is_temporary) { 1149 unlink(filename); 1150 } 1151fail: 1152 QDECREF(bs->options); 1153 QDECREF(options); 1154 bs->options = NULL; --- 244 unchanged lines hidden (view full) --- 1399 } 1400 bdrv_drain_all(); /* complete I/O */ 1401 bdrv_flush(bs); 1402 bdrv_drain_all(); /* in case flush left pending I/O */ 1403 notifier_list_notify(&bs->close_notifiers, bs); 1404 1405 if (bs->drv) { 1406 if (bs->backing_hd) { | 1147 } 1148 if (bs->is_temporary) { 1149 unlink(filename); 1150 } 1151fail: 1152 QDECREF(bs->options); 1153 QDECREF(options); 1154 bs->options = NULL; --- 244 unchanged lines hidden (view full) --- 1399 } 1400 bdrv_drain_all(); /* complete I/O */ 1401 bdrv_flush(bs); 1402 bdrv_drain_all(); /* in case flush left pending I/O */ 1403 notifier_list_notify(&bs->close_notifiers, bs); 1404 1405 if (bs->drv) { 1406 if (bs->backing_hd) { |
1407 bdrv_delete(bs->backing_hd); | 1407 bdrv_unref(bs->backing_hd); |
1408 bs->backing_hd = NULL; 1409 } 1410 bs->drv->bdrv_close(bs); 1411 g_free(bs->opaque); 1412#ifdef _WIN32 1413 if (bs->is_temporary) { 1414 unlink(bs->filename); 1415 } --- 8 unchanged lines hidden (view full) --- 1424 bs->valid_key = 0; 1425 bs->sg = 0; 1426 bs->growable = 0; 1427 bs->zero_beyond_eof = false; 1428 QDECREF(bs->options); 1429 bs->options = NULL; 1430 1431 if (bs->file != NULL) { | 1408 bs->backing_hd = NULL; 1409 } 1410 bs->drv->bdrv_close(bs); 1411 g_free(bs->opaque); 1412#ifdef _WIN32 1413 if (bs->is_temporary) { 1414 unlink(bs->filename); 1415 } --- 8 unchanged lines hidden (view full) --- 1424 bs->valid_key = 0; 1425 bs->sg = 0; 1426 bs->growable = 0; 1427 bs->zero_beyond_eof = false; 1428 QDECREF(bs->options); 1429 bs->options = NULL; 1430 1431 if (bs->file != NULL) { |
1432 bdrv_delete(bs->file); | 1432 bdrv_unref(bs->file); |
1433 bs->file = NULL; 1434 } 1435 } 1436 1437 bdrv_dev_change_media_cb(bs, false); 1438 1439 /*throttling disk I/O limits*/ 1440 if (bs->io_limits_enabled) { --- 207 unchanged lines hidden (view full) --- 1648 bs_top->backing_hd = bs_new; 1649 bs_top->open_flags &= ~BDRV_O_NO_BACKING; 1650 pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 1651 bs_new->filename); 1652 pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 1653 bs_new->drv ? bs_new->drv->format_name : ""); 1654} 1655 | 1433 bs->file = NULL; 1434 } 1435 } 1436 1437 bdrv_dev_change_media_cb(bs, false); 1438 1439 /*throttling disk I/O limits*/ 1440 if (bs->io_limits_enabled) { --- 207 unchanged lines hidden (view full) --- 1648 bs_top->backing_hd = bs_new; 1649 bs_top->open_flags &= ~BDRV_O_NO_BACKING; 1650 pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 1651 bs_new->filename); 1652 pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 1653 bs_new->drv ? bs_new->drv->format_name : ""); 1654} 1655 |
1656void bdrv_delete(BlockDriverState *bs) | 1656static void bdrv_delete(BlockDriverState *bs) |
1657{ 1658 assert(!bs->dev); 1659 assert(!bs->job); 1660 assert(!bs->in_use); | 1657{ 1658 assert(!bs->dev); 1659 assert(!bs->job); 1660 assert(!bs->in_use); |
1661 assert(!bs->refcnt); |
|
1661 1662 bdrv_close(bs); 1663 1664 /* remove from list, if necessary */ 1665 bdrv_make_anon(bs); 1666 1667 g_free(bs); 1668} --- 499 unchanged lines hidden (view full) --- 2168 goto exit; 2169 } 2170 new_top_bs->backing_hd = base_bs; 2171 2172 2173 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 2174 /* so that bdrv_close() does not recursively close the chain */ 2175 intermediate_state->bs->backing_hd = NULL; | 1662 1663 bdrv_close(bs); 1664 1665 /* remove from list, if necessary */ 1666 bdrv_make_anon(bs); 1667 1668 g_free(bs); 1669} --- 499 unchanged lines hidden (view full) --- 2169 goto exit; 2170 } 2171 new_top_bs->backing_hd = base_bs; 2172 2173 2174 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 2175 /* so that bdrv_close() does not recursively close the chain */ 2176 intermediate_state->bs->backing_hd = NULL; |
2176 bdrv_delete(intermediate_state->bs); | 2177 bdrv_unref(intermediate_state->bs); |
2177 } 2178 ret = 0; 2179 2180exit: 2181 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 2182 g_free(intermediate_state); 2183 } 2184 return ret; --- 2341 unchanged lines hidden (view full) --- 4526 } 4527 } 4528 4529out: 4530 free_option_parameters(create_options); 4531 free_option_parameters(param); 4532 4533 if (bs) { | 2178 } 2179 ret = 0; 2180 2181exit: 2182 QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 2183 g_free(intermediate_state); 2184 } 2185 return ret; --- 2341 unchanged lines hidden (view full) --- 4527 } 4528 } 4529 4530out: 4531 free_option_parameters(create_options); 4532 free_option_parameters(param); 4533 4534 if (bs) { |
4534 bdrv_delete(bs); | 4535 bdrv_unref(bs); |
4535 } 4536} 4537 4538AioContext *bdrv_get_aio_context(BlockDriverState *bs) 4539{ 4540 /* Currently BlockDriverState always uses the main loop AioContext */ 4541 return qemu_get_aio_context(); 4542} 4543 4544void bdrv_add_before_write_notifier(BlockDriverState *bs, 4545 NotifierWithReturn *notifier) 4546{ 4547 notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4548} | 4536 } 4537} 4538 4539AioContext *bdrv_get_aio_context(BlockDriverState *bs) 4540{ 4541 /* Currently BlockDriverState always uses the main loop AioContext */ 4542 return qemu_get_aio_context(); 4543} 4544 4545void bdrv_add_before_write_notifier(BlockDriverState *bs, 4546 NotifierWithReturn *notifier) 4547{ 4548 notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4549} |