Commit 45566e9c9930ebdfb82822c99a792c5caedb38b0

Authored by Christoph Hellwig
Committed by Anthony Liguori
1 parent e1e8f35a

replace bdrv_{get, put}_buffer with bdrv_{load, save}_vmstate

The VM state offset is a concept internal to the image format.  Replace
the old bdrv_{get,put}_buffer method that require an index into the
image file that is constructed from the VM state offset and an offset
into the vmstate with the bdrv_{load,save}_vmstate that just take an
offset into the VM state.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
@@ -1158,24 +1158,26 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) @@ -1158,24 +1158,26 @@ int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1158 return drv->bdrv_get_info(bs, bdi); 1158 return drv->bdrv_get_info(bs, bdi);
1159 } 1159 }
1160 1160
1161 -int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) 1161 +int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
  1162 + int64_t pos, int size)
1162 { 1163 {
1163 BlockDriver *drv = bs->drv; 1164 BlockDriver *drv = bs->drv;
1164 if (!drv) 1165 if (!drv)
1165 return -ENOMEDIUM; 1166 return -ENOMEDIUM;
1166 - if (!drv->bdrv_put_buffer) 1167 + if (!drv->bdrv_save_vmstate)
1167 return -ENOTSUP; 1168 return -ENOTSUP;
1168 - return drv->bdrv_put_buffer(bs, buf, pos, size); 1169 + return drv->bdrv_save_vmstate(bs, buf, pos, size);
1169 } 1170 }
1170 1171
1171 -int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) 1172 +int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
  1173 + int64_t pos, int size)
1172 { 1174 {
1173 BlockDriver *drv = bs->drv; 1175 BlockDriver *drv = bs->drv;
1174 if (!drv) 1176 if (!drv)
1175 return -ENOMEDIUM; 1177 return -ENOMEDIUM;
1176 - if (!drv->bdrv_get_buffer) 1178 + if (!drv->bdrv_load_vmstate)
1177 return -ENOTSUP; 1179 return -ENOTSUP;
1178 - return drv->bdrv_get_buffer(bs, buf, pos, size); 1180 + return drv->bdrv_load_vmstate(bs, buf, pos, size);
1179 } 1181 }
1180 1182
1181 /**************************************************************/ 1183 /**************************************************************/
@@ -159,9 +159,10 @@ void path_combine(char *dest, int dest_size, @@ -159,9 +159,10 @@ void path_combine(char *dest, int dest_size,
159 const char *base_path, 159 const char *base_path,
160 const char *filename); 160 const char *filename);
161 161
162 -int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf,  
163 - int64_t pos, int size); 162 +int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
  163 + int64_t pos, int size);
164 164
165 -int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size); 165 +int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
  166 + int64_t pos, int size);
166 167
167 #endif 168 #endif
block/qcow2.c
@@ -890,12 +890,16 @@ static void qcow_flush(BlockDriverState *bs) @@ -890,12 +890,16 @@ static void qcow_flush(BlockDriverState *bs)
890 bdrv_flush(s->hd); 890 bdrv_flush(s->hd);
891 } 891 }
892 892
  893 +static int64_t qcow_vm_state_offset(BDRVQcowState *s)
  894 +{
  895 + return (int64_t)s->l1_vm_state_index << (s->cluster_bits + s->l2_bits);
  896 +}
  897 +
893 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 898 static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
894 { 899 {
895 BDRVQcowState *s = bs->opaque; 900 BDRVQcowState *s = bs->opaque;
896 bdi->cluster_size = s->cluster_size; 901 bdi->cluster_size = s->cluster_size;
897 - bdi->vm_state_offset = (int64_t)s->l1_vm_state_index <<  
898 - (s->cluster_bits + s->l2_bits); 902 + bdi->vm_state_offset = qcow_vm_state_offset(s);
899 return 0; 903 return 0;
900 } 904 }
901 905
@@ -925,26 +929,28 @@ static void dump_refcounts(BlockDriverState *bs) @@ -925,26 +929,28 @@ static void dump_refcounts(BlockDriverState *bs)
925 } 929 }
926 #endif 930 #endif
927 931
928 -static int qcow_put_buffer(BlockDriverState *bs, const uint8_t *buf, 932 +static int qcow_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
929 int64_t pos, int size) 933 int64_t pos, int size)
930 { 934 {
  935 + BDRVQcowState *s = bs->opaque;
931 int growable = bs->growable; 936 int growable = bs->growable;
932 937
933 bs->growable = 1; 938 bs->growable = 1;
934 - bdrv_pwrite(bs, pos, buf, size); 939 + bdrv_pwrite(bs, qcow_vm_state_offset(s) + pos, buf, size);
935 bs->growable = growable; 940 bs->growable = growable;
936 941
937 return size; 942 return size;
938 } 943 }
939 944
940 -static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf, 945 +static int qcow_load_vmstate(BlockDriverState *bs, uint8_t *buf,
941 int64_t pos, int size) 946 int64_t pos, int size)
942 { 947 {
  948 + BDRVQcowState *s = bs->opaque;
943 int growable = bs->growable; 949 int growable = bs->growable;
944 int ret; 950 int ret;
945 951
946 bs->growable = 1; 952 bs->growable = 1;
947 - ret = bdrv_pread(bs, pos, buf, size); 953 + ret = bdrv_pread(bs, qcow_vm_state_offset(s) + pos, buf, size);
948 bs->growable = growable; 954 bs->growable = growable;
949 955
950 return ret; 956 return ret;
@@ -1001,8 +1007,8 @@ static BlockDriver bdrv_qcow2 = { @@ -1001,8 +1007,8 @@ static BlockDriver bdrv_qcow2 = {
1001 .bdrv_snapshot_list = qcow2_snapshot_list, 1007 .bdrv_snapshot_list = qcow2_snapshot_list,
1002 .bdrv_get_info = qcow_get_info, 1008 .bdrv_get_info = qcow_get_info,
1003 1009
1004 - .bdrv_put_buffer = qcow_put_buffer,  
1005 - .bdrv_get_buffer = qcow_get_buffer, 1010 + .bdrv_save_vmstate = qcow_save_vmstate,
  1011 + .bdrv_load_vmstate = qcow_load_vmstate,
1006 1012
1007 .create_options = qcow_create_options, 1013 .create_options = qcow_create_options,
1008 .bdrv_check = qcow_check, 1014 .bdrv_check = qcow_check,
block_int.h
@@ -84,10 +84,10 @@ struct BlockDriver { @@ -84,10 +84,10 @@ struct BlockDriver {
84 QEMUSnapshotInfo **psn_info); 84 QEMUSnapshotInfo **psn_info);
85 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); 85 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
86 86
87 - int (*bdrv_put_buffer)(BlockDriverState *bs, const uint8_t *buf,  
88 - int64_t pos, int size);  
89 - int (*bdrv_get_buffer)(BlockDriverState *bs, uint8_t *buf,  
90 - int64_t pos, int size); 87 + int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
  88 + int64_t pos, int size);
  89 + int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
  90 + int64_t pos, int size);
91 91
92 /* removable device specific */ 92 /* removable device specific */
93 int (*bdrv_is_inserted)(BlockDriverState *bs); 93 int (*bdrv_is_inserted)(BlockDriverState *bs);
savevm.c
@@ -337,46 +337,28 @@ fail: @@ -337,46 +337,28 @@ fail:
337 return NULL; 337 return NULL;
338 } 338 }
339 339
340 -typedef struct QEMUFileBdrv  
341 -{  
342 - BlockDriverState *bs;  
343 - int64_t base_offset;  
344 -} QEMUFileBdrv;  
345 -  
346 static int block_put_buffer(void *opaque, const uint8_t *buf, 340 static int block_put_buffer(void *opaque, const uint8_t *buf,
347 int64_t pos, int size) 341 int64_t pos, int size)
348 { 342 {
349 - QEMUFileBdrv *s = opaque;  
350 - bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size); 343 + bdrv_save_vmstate(opaque, buf, pos, size);
351 return size; 344 return size;
352 } 345 }
353 346
354 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) 347 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
355 { 348 {
356 - QEMUFileBdrv *s = opaque;  
357 - return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size); 349 + return bdrv_load_vmstate(opaque, buf, pos, size);
358 } 350 }
359 351
360 static int bdrv_fclose(void *opaque) 352 static int bdrv_fclose(void *opaque)
361 { 353 {
362 - QEMUFileBdrv *s = opaque;  
363 - qemu_free(s);  
364 return 0; 354 return 0;
365 } 355 }
366 356
367 -static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) 357 +static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
368 { 358 {
369 - QEMUFileBdrv *s;  
370 -  
371 - s = qemu_mallocz(sizeof(QEMUFileBdrv));  
372 -  
373 - s->bs = bs;  
374 - s->base_offset = offset;  
375 -  
376 if (is_writable) 359 if (is_writable)
377 - return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);  
378 -  
379 - return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL, NULL); 360 + return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
  361 + return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
380 } 362 }
381 363
382 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer, 364 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
@@ -1069,7 +1051,6 @@ void do_savevm(Monitor *mon, const char *name) @@ -1069,7 +1051,6 @@ void do_savevm(Monitor *mon, const char *name)
1069 BlockDriverState *bs, *bs1; 1051 BlockDriverState *bs, *bs1;
1070 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; 1052 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1071 int must_delete, ret, i; 1053 int must_delete, ret, i;
1072 - BlockDriverInfo bdi1, *bdi = &bdi1;  
1073 QEMUFile *f; 1054 QEMUFile *f;
1074 int saved_vm_running; 1055 int saved_vm_running;
1075 uint32_t vm_state_size; 1056 uint32_t vm_state_size;
@@ -1119,14 +1100,8 @@ void do_savevm(Monitor *mon, const char *name) @@ -1119,14 +1100,8 @@ void do_savevm(Monitor *mon, const char *name)
1119 #endif 1100 #endif
1120 sn->vm_clock_nsec = qemu_get_clock(vm_clock); 1101 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1121 1102
1122 - if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {  
1123 - monitor_printf(mon, "Device %s does not support VM state snapshots\n",  
1124 - bdrv_get_device_name(bs));  
1125 - goto the_end;  
1126 - }  
1127 -  
1128 /* save the VM state */ 1103 /* save the VM state */
1129 - f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1); 1104 + f = qemu_fopen_bdrv(bs, 1);
1130 if (!f) { 1105 if (!f) {
1131 monitor_printf(mon, "Could not open VM state file\n"); 1106 monitor_printf(mon, "Could not open VM state file\n");
1132 goto the_end; 1107 goto the_end;
@@ -1170,7 +1145,6 @@ void do_savevm(Monitor *mon, const char *name) @@ -1170,7 +1145,6 @@ void do_savevm(Monitor *mon, const char *name)
1170 void do_loadvm(Monitor *mon, const char *name) 1145 void do_loadvm(Monitor *mon, const char *name)
1171 { 1146 {
1172 BlockDriverState *bs, *bs1; 1147 BlockDriverState *bs, *bs1;
1173 - BlockDriverInfo bdi1, *bdi = &bdi1;  
1174 QEMUSnapshotInfo sn; 1148 QEMUSnapshotInfo sn;
1175 QEMUFile *f; 1149 QEMUFile *f;
1176 int i, ret; 1150 int i, ret;
@@ -1218,19 +1192,13 @@ void do_loadvm(Monitor *mon, const char *name) @@ -1218,19 +1192,13 @@ void do_loadvm(Monitor *mon, const char *name)
1218 } 1192 }
1219 } 1193 }
1220 1194
1221 - if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {  
1222 - monitor_printf(mon, "Device %s does not support VM state snapshots\n",  
1223 - bdrv_get_device_name(bs));  
1224 - return;  
1225 - }  
1226 -  
1227 /* Don't even try to load empty VM states */ 1195 /* Don't even try to load empty VM states */
1228 ret = bdrv_snapshot_find(bs, &sn, name); 1196 ret = bdrv_snapshot_find(bs, &sn, name);
1229 if ((ret >= 0) && (sn.vm_state_size == 0)) 1197 if ((ret >= 0) && (sn.vm_state_size == 0))
1230 goto the_end; 1198 goto the_end;
1231 1199
1232 /* restore the VM state */ 1200 /* restore the VM state */
1233 - f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0); 1201 + f = qemu_fopen_bdrv(bs, 0);
1234 if (!f) { 1202 if (!f) {
1235 monitor_printf(mon, "Could not open VM state file\n"); 1203 monitor_printf(mon, "Could not open VM state file\n");
1236 goto the_end; 1204 goto the_end;