Revision c142442b block/qcow2.c
b/block/qcow2.c | ||
---|---|---|
57 | 57 |
#define QCOW_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA |
58 | 58 |
|
59 | 59 |
|
60 |
typedef struct __attribute__((packed)) QCowSnapshotHeader { |
|
61 |
/* header is 8 byte aligned */ |
|
62 |
uint64_t l1_table_offset; |
|
63 |
|
|
64 |
uint32_t l1_size; |
|
65 |
uint16_t id_str_size; |
|
66 |
uint16_t name_size; |
|
67 |
|
|
68 |
uint32_t date_sec; |
|
69 |
uint32_t date_nsec; |
|
70 |
|
|
71 |
uint64_t vm_clock_nsec; |
|
72 |
|
|
73 |
uint32_t vm_state_size; |
|
74 |
uint32_t extra_data_size; /* for extension */ |
|
75 |
/* extra data follows */ |
|
76 |
/* id_str follows */ |
|
77 |
/* name follows */ |
|
78 |
} QCowSnapshotHeader; |
|
79 |
|
|
80 |
|
|
81 |
static int qcow_read_snapshots(BlockDriverState *bs); |
|
82 |
static void qcow_free_snapshots(BlockDriverState *bs); |
|
83 | 60 |
|
84 | 61 |
static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename) |
85 | 62 |
{ |
... | ... | |
331 | 308 |
return 0; |
332 | 309 |
} |
333 | 310 |
|
334 |
static int64_t align_offset(int64_t offset, int n) |
|
335 |
{ |
|
336 |
offset = (offset + n - 1) & ~(n - 1); |
|
337 |
return offset; |
|
338 |
} |
|
339 |
|
|
340 | 311 |
static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_num, |
341 | 312 |
int nb_sectors, int *pnum) |
342 | 313 |
{ |
... | ... | |
936 | 907 |
return 0; |
937 | 908 |
} |
938 | 909 |
|
939 |
/*********************************************************/ |
|
940 |
/* snapshot support */ |
|
941 |
|
|
942 |
|
|
943 |
static void qcow_free_snapshots(BlockDriverState *bs) |
|
944 |
{ |
|
945 |
BDRVQcowState *s = bs->opaque; |
|
946 |
int i; |
|
947 |
|
|
948 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
949 |
qemu_free(s->snapshots[i].name); |
|
950 |
qemu_free(s->snapshots[i].id_str); |
|
951 |
} |
|
952 |
qemu_free(s->snapshots); |
|
953 |
s->snapshots = NULL; |
|
954 |
s->nb_snapshots = 0; |
|
955 |
} |
|
956 |
|
|
957 |
static int qcow_read_snapshots(BlockDriverState *bs) |
|
958 |
{ |
|
959 |
BDRVQcowState *s = bs->opaque; |
|
960 |
QCowSnapshotHeader h; |
|
961 |
QCowSnapshot *sn; |
|
962 |
int i, id_str_size, name_size; |
|
963 |
int64_t offset; |
|
964 |
uint32_t extra_data_size; |
|
965 |
|
|
966 |
if (!s->nb_snapshots) { |
|
967 |
s->snapshots = NULL; |
|
968 |
s->snapshots_size = 0; |
|
969 |
return 0; |
|
970 |
} |
|
971 |
|
|
972 |
offset = s->snapshots_offset; |
|
973 |
s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot)); |
|
974 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
975 |
offset = align_offset(offset, 8); |
|
976 |
if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h)) |
|
977 |
goto fail; |
|
978 |
offset += sizeof(h); |
|
979 |
sn = s->snapshots + i; |
|
980 |
sn->l1_table_offset = be64_to_cpu(h.l1_table_offset); |
|
981 |
sn->l1_size = be32_to_cpu(h.l1_size); |
|
982 |
sn->vm_state_size = be32_to_cpu(h.vm_state_size); |
|
983 |
sn->date_sec = be32_to_cpu(h.date_sec); |
|
984 |
sn->date_nsec = be32_to_cpu(h.date_nsec); |
|
985 |
sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec); |
|
986 |
extra_data_size = be32_to_cpu(h.extra_data_size); |
|
987 |
|
|
988 |
id_str_size = be16_to_cpu(h.id_str_size); |
|
989 |
name_size = be16_to_cpu(h.name_size); |
|
990 |
|
|
991 |
offset += extra_data_size; |
|
992 |
|
|
993 |
sn->id_str = qemu_malloc(id_str_size + 1); |
|
994 |
if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size) |
|
995 |
goto fail; |
|
996 |
offset += id_str_size; |
|
997 |
sn->id_str[id_str_size] = '\0'; |
|
998 |
|
|
999 |
sn->name = qemu_malloc(name_size + 1); |
|
1000 |
if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size) |
|
1001 |
goto fail; |
|
1002 |
offset += name_size; |
|
1003 |
sn->name[name_size] = '\0'; |
|
1004 |
} |
|
1005 |
s->snapshots_size = offset - s->snapshots_offset; |
|
1006 |
return 0; |
|
1007 |
fail: |
|
1008 |
qcow_free_snapshots(bs); |
|
1009 |
return -1; |
|
1010 |
} |
|
1011 |
|
|
1012 |
/* add at the end of the file a new list of snapshots */ |
|
1013 |
static int qcow_write_snapshots(BlockDriverState *bs) |
|
1014 |
{ |
|
1015 |
BDRVQcowState *s = bs->opaque; |
|
1016 |
QCowSnapshot *sn; |
|
1017 |
QCowSnapshotHeader h; |
|
1018 |
int i, name_size, id_str_size, snapshots_size; |
|
1019 |
uint64_t data64; |
|
1020 |
uint32_t data32; |
|
1021 |
int64_t offset, snapshots_offset; |
|
1022 |
|
|
1023 |
/* compute the size of the snapshots */ |
|
1024 |
offset = 0; |
|
1025 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1026 |
sn = s->snapshots + i; |
|
1027 |
offset = align_offset(offset, 8); |
|
1028 |
offset += sizeof(h); |
|
1029 |
offset += strlen(sn->id_str); |
|
1030 |
offset += strlen(sn->name); |
|
1031 |
} |
|
1032 |
snapshots_size = offset; |
|
1033 |
|
|
1034 |
snapshots_offset = alloc_clusters(bs, snapshots_size); |
|
1035 |
offset = snapshots_offset; |
|
1036 |
|
|
1037 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1038 |
sn = s->snapshots + i; |
|
1039 |
memset(&h, 0, sizeof(h)); |
|
1040 |
h.l1_table_offset = cpu_to_be64(sn->l1_table_offset); |
|
1041 |
h.l1_size = cpu_to_be32(sn->l1_size); |
|
1042 |
h.vm_state_size = cpu_to_be32(sn->vm_state_size); |
|
1043 |
h.date_sec = cpu_to_be32(sn->date_sec); |
|
1044 |
h.date_nsec = cpu_to_be32(sn->date_nsec); |
|
1045 |
h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec); |
|
1046 |
|
|
1047 |
id_str_size = strlen(sn->id_str); |
|
1048 |
name_size = strlen(sn->name); |
|
1049 |
h.id_str_size = cpu_to_be16(id_str_size); |
|
1050 |
h.name_size = cpu_to_be16(name_size); |
|
1051 |
offset = align_offset(offset, 8); |
|
1052 |
if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h)) |
|
1053 |
goto fail; |
|
1054 |
offset += sizeof(h); |
|
1055 |
if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size) |
|
1056 |
goto fail; |
|
1057 |
offset += id_str_size; |
|
1058 |
if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size) |
|
1059 |
goto fail; |
|
1060 |
offset += name_size; |
|
1061 |
} |
|
1062 |
|
|
1063 |
/* update the various header fields */ |
|
1064 |
data64 = cpu_to_be64(snapshots_offset); |
|
1065 |
if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset), |
|
1066 |
&data64, sizeof(data64)) != sizeof(data64)) |
|
1067 |
goto fail; |
|
1068 |
data32 = cpu_to_be32(s->nb_snapshots); |
|
1069 |
if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots), |
|
1070 |
&data32, sizeof(data32)) != sizeof(data32)) |
|
1071 |
goto fail; |
|
1072 |
|
|
1073 |
/* free the old snapshot table */ |
|
1074 |
free_clusters(bs, s->snapshots_offset, s->snapshots_size); |
|
1075 |
s->snapshots_offset = snapshots_offset; |
|
1076 |
s->snapshots_size = snapshots_size; |
|
1077 |
return 0; |
|
1078 |
fail: |
|
1079 |
return -1; |
|
1080 |
} |
|
1081 |
|
|
1082 |
static void find_new_snapshot_id(BlockDriverState *bs, |
|
1083 |
char *id_str, int id_str_size) |
|
1084 |
{ |
|
1085 |
BDRVQcowState *s = bs->opaque; |
|
1086 |
QCowSnapshot *sn; |
|
1087 |
int i, id, id_max = 0; |
|
1088 |
|
|
1089 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1090 |
sn = s->snapshots + i; |
|
1091 |
id = strtoul(sn->id_str, NULL, 10); |
|
1092 |
if (id > id_max) |
|
1093 |
id_max = id; |
|
1094 |
} |
|
1095 |
snprintf(id_str, id_str_size, "%d", id_max + 1); |
|
1096 |
} |
|
1097 |
|
|
1098 |
static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str) |
|
1099 |
{ |
|
1100 |
BDRVQcowState *s = bs->opaque; |
|
1101 |
int i; |
|
1102 |
|
|
1103 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1104 |
if (!strcmp(s->snapshots[i].id_str, id_str)) |
|
1105 |
return i; |
|
1106 |
} |
|
1107 |
return -1; |
|
1108 |
} |
|
1109 |
|
|
1110 |
static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name) |
|
1111 |
{ |
|
1112 |
BDRVQcowState *s = bs->opaque; |
|
1113 |
int i, ret; |
|
1114 |
|
|
1115 |
ret = find_snapshot_by_id(bs, name); |
|
1116 |
if (ret >= 0) |
|
1117 |
return ret; |
|
1118 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1119 |
if (!strcmp(s->snapshots[i].name, name)) |
|
1120 |
return i; |
|
1121 |
} |
|
1122 |
return -1; |
|
1123 |
} |
|
1124 |
|
|
1125 |
/* if no id is provided, a new one is constructed */ |
|
1126 |
static int qcow_snapshot_create(BlockDriverState *bs, |
|
1127 |
QEMUSnapshotInfo *sn_info) |
|
1128 |
{ |
|
1129 |
BDRVQcowState *s = bs->opaque; |
|
1130 |
QCowSnapshot *snapshots1, sn1, *sn = &sn1; |
|
1131 |
int i, ret; |
|
1132 |
uint64_t *l1_table = NULL; |
|
1133 |
|
|
1134 |
memset(sn, 0, sizeof(*sn)); |
|
1135 |
|
|
1136 |
if (sn_info->id_str[0] == '\0') { |
|
1137 |
/* compute a new id */ |
|
1138 |
find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str)); |
|
1139 |
} |
|
1140 |
|
|
1141 |
/* check that the ID is unique */ |
|
1142 |
if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) |
|
1143 |
return -ENOENT; |
|
1144 |
|
|
1145 |
sn->id_str = qemu_strdup(sn_info->id_str); |
|
1146 |
if (!sn->id_str) |
|
1147 |
goto fail; |
|
1148 |
sn->name = qemu_strdup(sn_info->name); |
|
1149 |
if (!sn->name) |
|
1150 |
goto fail; |
|
1151 |
sn->vm_state_size = sn_info->vm_state_size; |
|
1152 |
sn->date_sec = sn_info->date_sec; |
|
1153 |
sn->date_nsec = sn_info->date_nsec; |
|
1154 |
sn->vm_clock_nsec = sn_info->vm_clock_nsec; |
|
1155 |
|
|
1156 |
ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1); |
|
1157 |
if (ret < 0) |
|
1158 |
goto fail; |
|
1159 |
|
|
1160 |
/* create the L1 table of the snapshot */ |
|
1161 |
sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t)); |
|
1162 |
sn->l1_size = s->l1_size; |
|
1163 |
|
|
1164 |
l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t)); |
|
1165 |
for(i = 0; i < s->l1_size; i++) { |
|
1166 |
l1_table[i] = cpu_to_be64(s->l1_table[i]); |
|
1167 |
} |
|
1168 |
if (bdrv_pwrite(s->hd, sn->l1_table_offset, |
|
1169 |
l1_table, s->l1_size * sizeof(uint64_t)) != |
|
1170 |
(s->l1_size * sizeof(uint64_t))) |
|
1171 |
goto fail; |
|
1172 |
qemu_free(l1_table); |
|
1173 |
l1_table = NULL; |
|
1174 |
|
|
1175 |
snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot)); |
|
1176 |
if (s->snapshots) { |
|
1177 |
memcpy(snapshots1, s->snapshots, s->nb_snapshots * sizeof(QCowSnapshot)); |
|
1178 |
qemu_free(s->snapshots); |
|
1179 |
} |
|
1180 |
s->snapshots = snapshots1; |
|
1181 |
s->snapshots[s->nb_snapshots++] = *sn; |
|
1182 |
|
|
1183 |
if (qcow_write_snapshots(bs) < 0) |
|
1184 |
goto fail; |
|
1185 |
#ifdef DEBUG_ALLOC |
|
1186 |
check_refcounts(bs); |
|
1187 |
#endif |
|
1188 |
return 0; |
|
1189 |
fail: |
|
1190 |
qemu_free(sn->name); |
|
1191 |
qemu_free(l1_table); |
|
1192 |
return -1; |
|
1193 |
} |
|
1194 |
|
|
1195 |
/* copy the snapshot 'snapshot_name' into the current disk image */ |
|
1196 |
static int qcow_snapshot_goto(BlockDriverState *bs, |
|
1197 |
const char *snapshot_id) |
|
1198 |
{ |
|
1199 |
BDRVQcowState *s = bs->opaque; |
|
1200 |
QCowSnapshot *sn; |
|
1201 |
int i, snapshot_index, l1_size2; |
|
1202 |
|
|
1203 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
|
1204 |
if (snapshot_index < 0) |
|
1205 |
return -ENOENT; |
|
1206 |
sn = &s->snapshots[snapshot_index]; |
|
1207 |
|
|
1208 |
if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0) |
|
1209 |
goto fail; |
|
1210 |
|
|
1211 |
if (grow_l1_table(bs, sn->l1_size) < 0) |
|
1212 |
goto fail; |
|
1213 |
|
|
1214 |
s->l1_size = sn->l1_size; |
|
1215 |
l1_size2 = s->l1_size * sizeof(uint64_t); |
|
1216 |
/* copy the snapshot l1 table to the current l1 table */ |
|
1217 |
if (bdrv_pread(s->hd, sn->l1_table_offset, |
|
1218 |
s->l1_table, l1_size2) != l1_size2) |
|
1219 |
goto fail; |
|
1220 |
if (bdrv_pwrite(s->hd, s->l1_table_offset, |
|
1221 |
s->l1_table, l1_size2) != l1_size2) |
|
1222 |
goto fail; |
|
1223 |
for(i = 0;i < s->l1_size; i++) { |
|
1224 |
be64_to_cpus(&s->l1_table[i]); |
|
1225 |
} |
|
1226 |
|
|
1227 |
if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0) |
|
1228 |
goto fail; |
|
1229 |
|
|
1230 |
#ifdef DEBUG_ALLOC |
|
1231 |
check_refcounts(bs); |
|
1232 |
#endif |
|
1233 |
return 0; |
|
1234 |
fail: |
|
1235 |
return -EIO; |
|
1236 |
} |
|
1237 |
|
|
1238 |
static int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
|
1239 |
{ |
|
1240 |
BDRVQcowState *s = bs->opaque; |
|
1241 |
QCowSnapshot *sn; |
|
1242 |
int snapshot_index, ret; |
|
1243 |
|
|
1244 |
snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id); |
|
1245 |
if (snapshot_index < 0) |
|
1246 |
return -ENOENT; |
|
1247 |
sn = &s->snapshots[snapshot_index]; |
|
1248 |
|
|
1249 |
ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1); |
|
1250 |
if (ret < 0) |
|
1251 |
return ret; |
|
1252 |
/* must update the copied flag on the current cluster offsets */ |
|
1253 |
ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0); |
|
1254 |
if (ret < 0) |
|
1255 |
return ret; |
|
1256 |
free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t)); |
|
1257 |
|
|
1258 |
qemu_free(sn->id_str); |
|
1259 |
qemu_free(sn->name); |
|
1260 |
memmove(sn, sn + 1, (s->nb_snapshots - snapshot_index - 1) * sizeof(*sn)); |
|
1261 |
s->nb_snapshots--; |
|
1262 |
ret = qcow_write_snapshots(bs); |
|
1263 |
if (ret < 0) { |
|
1264 |
/* XXX: restore snapshot if error ? */ |
|
1265 |
return ret; |
|
1266 |
} |
|
1267 |
#ifdef DEBUG_ALLOC |
|
1268 |
check_refcounts(bs); |
|
1269 |
#endif |
|
1270 |
return 0; |
|
1271 |
} |
|
1272 |
|
|
1273 |
static int qcow_snapshot_list(BlockDriverState *bs, |
|
1274 |
QEMUSnapshotInfo **psn_tab) |
|
1275 |
{ |
|
1276 |
BDRVQcowState *s = bs->opaque; |
|
1277 |
QEMUSnapshotInfo *sn_tab, *sn_info; |
|
1278 |
QCowSnapshot *sn; |
|
1279 |
int i; |
|
1280 |
|
|
1281 |
if (!s->nb_snapshots) { |
|
1282 |
*psn_tab = NULL; |
|
1283 |
return s->nb_snapshots; |
|
1284 |
} |
|
1285 |
|
|
1286 |
sn_tab = qemu_mallocz(s->nb_snapshots * sizeof(QEMUSnapshotInfo)); |
|
1287 |
for(i = 0; i < s->nb_snapshots; i++) { |
|
1288 |
sn_info = sn_tab + i; |
|
1289 |
sn = s->snapshots + i; |
|
1290 |
pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), |
|
1291 |
sn->id_str); |
|
1292 |
pstrcpy(sn_info->name, sizeof(sn_info->name), |
|
1293 |
sn->name); |
|
1294 |
sn_info->vm_state_size = sn->vm_state_size; |
|
1295 |
sn_info->date_sec = sn->date_sec; |
|
1296 |
sn_info->date_nsec = sn->date_nsec; |
|
1297 |
sn_info->vm_clock_nsec = sn->vm_clock_nsec; |
|
1298 |
} |
|
1299 |
*psn_tab = sn_tab; |
|
1300 |
return s->nb_snapshots; |
|
1301 |
} |
|
1302 | 910 |
|
1303 | 911 |
static int qcow_check(BlockDriverState *bs) |
1304 | 912 |
{ |
Also available in: Unified diff