block.c (d7d512f60979681c27597f1b1277e03505c1de08) block.c (b1b1d783eabdb6ac4e4578b2c04b0c24483dce77)
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

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

25#include "qemu-common.h"
26#include "trace.h"
27#include "monitor.h"
28#include "block_int.h"
29#include "blockjob.h"
30#include "module.h"
31#include "qjson.h"
32#include "sysemu.h"
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

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

25#include "qemu-common.h"
26#include "trace.h"
27#include "monitor.h"
28#include "block_int.h"
29#include "blockjob.h"
30#include "module.h"
31#include "qjson.h"
32#include "sysemu.h"
33#include "notify.h"
34#include "qemu-coroutine.h"
35#include "qmp-commands.h"
36#include "qemu-timer.h"
37
38#ifdef CONFIG_BSD
39#include <sys/types.h>
40#include <sys/stat.h>
41#include <sys/ioctl.h>

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

308 BlockDriverState *bs;
309
310 bs = g_malloc0(sizeof(BlockDriverState));
311 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
312 if (device_name[0] != '\0') {
313 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
314 }
315 bdrv_iostatus_disable(bs);
33#include "qemu-coroutine.h"
34#include "qmp-commands.h"
35#include "qemu-timer.h"
36
37#ifdef CONFIG_BSD
38#include <sys/types.h>
39#include <sys/stat.h>
40#include <sys/ioctl.h>

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

307 BlockDriverState *bs;
308
309 bs = g_malloc0(sizeof(BlockDriverState));
310 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
311 if (device_name[0] != '\0') {
312 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
313 }
314 bdrv_iostatus_disable(bs);
316 notifier_list_init(&bs->close_notifiers);
317
318 return bs;
319}
320
315 return bs;
316}
317
321void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
322{
323 notifier_list_add(&bs->close_notifiers, notify);
324}
325
326BlockDriver *bdrv_find_format(const char *format_name)
327{
328 BlockDriver *drv1;
329 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
330 if (!strcmp(drv1->format_name, format_name)) {
331 return drv1;
332 }
333 }

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

1101 drv->bdrv_reopen_abort(reopen_state);
1102 }
1103}
1104
1105
1106void bdrv_close(BlockDriverState *bs)
1107{
1108 bdrv_flush(bs);
318BlockDriver *bdrv_find_format(const char *format_name)
319{
320 BlockDriver *drv1;
321 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
322 if (!strcmp(drv1->format_name, format_name)) {
323 return drv1;
324 }
325 }

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

1093 drv->bdrv_reopen_abort(reopen_state);
1094 }
1095}
1096
1097
1098void bdrv_close(BlockDriverState *bs)
1099{
1100 bdrv_flush(bs);
1109 if (bs->job) {
1110 block_job_cancel_sync(bs->job);
1111 }
1112 bdrv_drain_all();
1113 notifier_list_notify(&bs->close_notifiers, bs);
1114
1115 if (bs->drv) {
1101 if (bs->drv) {
1102 if (bs->job) {
1103 block_job_cancel_sync(bs->job);
1104 }
1105 bdrv_drain_all();
1106
1116 if (bs == bs_snapshots) {
1117 bs_snapshots = NULL;
1118 }
1119 if (bs->backing_hd) {
1120 bdrv_delete(bs->backing_hd);
1121 bs->backing_hd = NULL;
1122 }
1123 bs->drv->bdrv_close(bs);

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

3127 return -EINVAL;
3128 }
3129 if (drv->bdrv_snapshot_load_tmp) {
3130 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
3131 }
3132 return -ENOTSUP;
3133}
3134
1107 if (bs == bs_snapshots) {
1108 bs_snapshots = NULL;
1109 }
1110 if (bs->backing_hd) {
1111 bdrv_delete(bs->backing_hd);
1112 bs->backing_hd = NULL;
1113 }
1114 bs->drv->bdrv_close(bs);

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

3118 return -EINVAL;
3119 }
3120 if (drv->bdrv_snapshot_load_tmp) {
3121 return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
3122 }
3123 return -ENOTSUP;
3124}
3125
3126/* backing_file can either be relative, or absolute, or a protocol. If it is
3127 * relative, it must be relative to the chain. So, passing in bs->filename
3128 * from a BDS as backing_file should not be done, as that may be relative to
3129 * the CWD rather than the chain. */
3135BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3136 const char *backing_file)
3137{
3130BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3131 const char *backing_file)
3132{
3138 if (!bs->drv) {
3133 char *filename_full = NULL;
3134 char *backing_file_full = NULL;
3135 char *filename_tmp = NULL;
3136 int is_protocol = 0;
3137 BlockDriverState *curr_bs = NULL;
3138 BlockDriverState *retval = NULL;
3139
3140 if (!bs || !bs->drv || !backing_file) {
3139 return NULL;
3140 }
3141
3141 return NULL;
3142 }
3143
3142 if (bs->backing_hd) {
3143 if (strcmp(bs->backing_file, backing_file) == 0) {
3144 return bs->backing_hd;
3144 filename_full = g_malloc(PATH_MAX);
3145 backing_file_full = g_malloc(PATH_MAX);
3146 filename_tmp = g_malloc(PATH_MAX);
3147
3148 is_protocol = path_has_protocol(backing_file);
3149
3150 for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
3151
3152 /* If either of the filename paths is actually a protocol, then
3153 * compare unmodified paths; otherwise make paths relative */
3154 if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3155 if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3156 retval = curr_bs->backing_hd;
3157 break;
3158 }
3145 } else {
3159 } else {
3146 return bdrv_find_backing_image(bs->backing_hd, backing_file);
3160 /* If not an absolute filename path, make it relative to the current
3161 * image's filename path */
3162 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3163 backing_file);
3164
3165 /* We are going to compare absolute pathnames */
3166 if (!realpath(filename_tmp, filename_full)) {
3167 continue;
3168 }
3169
3170 /* We need to make sure the backing filename we are comparing against
3171 * is relative to the current image filename (or absolute) */
3172 path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3173 curr_bs->backing_file);
3174
3175 if (!realpath(filename_tmp, backing_file_full)) {
3176 continue;
3177 }
3178
3179 if (strcmp(backing_file_full, filename_full) == 0) {
3180 retval = curr_bs->backing_hd;
3181 break;
3182 }
3147 }
3148 }
3149
3183 }
3184 }
3185
3150 return NULL;
3186 g_free(filename_full);
3187 g_free(backing_file_full);
3188 g_free(filename_tmp);
3189 return retval;
3151}
3152
3153int bdrv_get_backing_file_depth(BlockDriverState *bs)
3154{
3155 if (!bs->drv) {
3156 return 0;
3157 }
3158

--- 1292 unchanged lines hidden ---
3190}
3191
3192int bdrv_get_backing_file_depth(BlockDriverState *bs)
3193{
3194 if (!bs->drv) {
3195 return 0;
3196 }
3197

--- 1292 unchanged lines hidden ---