2 * Block driver for vmstate format
4 * Copyright (c) 2009 Nokia Corporation
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
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "block_int.h"
27 //#define VMSTATE_DEBUG
30 #define TRACE(fmt,...) fprintf(stderr, "%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
35 #define VMSTATE_MAGIC 0x564D53544154451ALL
36 #define VMSTATE_VERSION 1
38 typedef struct VMStateHeader {
41 uint64_t state_offset;
45 typedef struct BDRVVmState {
47 uint64_t state_offset;
49 uint64_t write_offset;
52 static int vmstate_probe(const uint8_t *buf, int buf_size, const char *filename)
54 const VMStateHeader *header = (const VMStateHeader *)buf;
55 if (buf_size >= sizeof(VMStateHeader) &&
56 be64_to_cpu(header->magic) == VMSTATE_MAGIC &&
57 be32_to_cpu(header->version) == VMSTATE_VERSION)
62 static int vmstate_open(BlockDriverState *bs, const char *filename, int flags)
64 BDRVVMState *s = bs->opaque;
67 TRACE("open \"%s\"...", filename);
68 s->fd = open(filename, O_RDWR | O_BINARY);
71 if (read(s->fd, &header, sizeof(header)) == sizeof(header) &&
72 be64_to_cpu(header.magic) == VMSTATE_MAGIC &&
73 be32_to_cpu(header.version) == VMSTATE_VERSION) {
74 s->state_offset = be64_to_cpu(header.state_offset);
75 s->state_size = be64_to_cpu(header.state_size);
77 s->write_offset = s->state_offset;
78 TRACE("open ok, state_offset=%lld", s->state_offset);
85 static void vmstate_flush(BlockDriverState *bs)
90 static void vmstate_close(BlockDriverState *bs)
92 BDRVVMState *s = bs->opaque;
99 static int vmstate_create(const char *filename, int64_t total_size,
100 const char *backing_file, int flags)
102 VMStateHeader header;
103 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
106 memset(&header, 0, sizeof(header));
107 header.magic = cpu_to_be64(VMSTATE_MAGIC);
108 header.version = cpu_to_be32(VMSTATE_VERSION);
109 header.state_offset = cpu_to_be64(sizeof(header));
110 write(fd, &header, sizeof(header));
115 static int vmstate_refresh_header(BDRVVMState *s)
117 VMStateHeader header;
119 TRACE("state_size = %lld", s->state_size);
120 if (!lseek(s->fd, 0, SEEK_SET) &&
121 read(s->fd, &header, sizeof(header)) == sizeof(header)) {
122 header.state_size = cpu_to_be64(s->state_size);
123 if (!lseek(s->fd, 0, SEEK_SET) &&
124 write(s->fd, &header, sizeof(header)) == sizeof(header))
130 static int vmstate_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
132 BDRVVMState *s = bs->opaque;
134 TRACE("vm_state_offset = %llx", s->state_offset);
135 bdi->cluster_size = 0;//VMSTATE_BLOCK_SIZE;
136 bdi->vm_state_offset = s->state_offset;
140 static int vmstate_get_buffer(BlockDriverState *bs, uint8_t *buf,
141 int64_t pos, int size)
143 BDRVVMState *s = bs->opaque;
145 TRACE("pos=%lld, size=%d", pos, size);
146 if (lseek(s->fd, pos, SEEK_SET) != pos)
148 return read(s->fd, buf, size);
151 static int vmstate_read(BlockDriverState *bs, int64_t sector_num,
152 uint8_t *buf, int nb_sectors)
154 BDRVVMState *s = bs->opaque;
156 TRACE("sector_num=%lld, nb_sectors=%d", sector_num, nb_sectors);
157 if (lseek(s->fd, sector_num * 512, SEEK_SET) != sector_num * 512)
159 return read(s->fd, buf, nb_sectors * 512);
162 static int vmstate_put_buffer(BlockDriverState *bs, const uint8_t *buf,
163 int64_t pos, int size)
165 BDRVVMState *s = bs->opaque;
167 TRACE("pos=%lld, size=%d", pos, size);
168 if (lseek(s->fd, pos, SEEK_SET) != pos)
170 return write(s->fd, buf, size);
173 static int vmstate_write(BlockDriverState *bs, int64_t sector_num,
174 const uint8_t *buf, int nb_sectors)
176 BDRVVMState *s = bs->opaque;
178 TRACE("sector_num=%lld, nb_sectors=%d", sector_num, nb_sectors);
179 if (lseek(s->fd, sector_num * 512, SEEK_SET) != sector_num * 512)
181 return write(s->fd, buf, nb_sectors * 512);
184 static int vmstate_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
186 BDRVVMState *s = bs->opaque;
188 TRACE("state_size = %lld", s->state_size);
189 return s->state_size ? 0 : -ENOENT;
192 static int vmstate_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
194 BDRVVMState *s = bs->opaque;
196 TRACE("delete snapshot \"%s\"...", snapshot_id);
199 vmstate_refresh_header(s);
200 if (!lseek(s->fd, 0, SEEK_SET))
201 return ftruncate(s->fd, sizeof(VMStateHeader));
206 static int vmstate_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
208 BDRVVMState *s = bs->opaque;
210 TRACE("create snapshot size %u", sn_info->vm_state_size);
212 vmstate_snapshot_delete(bs, NULL);
213 s->state_size = sn_info->vm_state_size;
214 s->write_offset = s->state_offset;
215 return vmstate_refresh_header(s);
218 static int vmstate_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
220 BDRVVMState *s = bs->opaque;
221 QEMUSnapshotInfo *sn_info;
223 TRACE("state_size = %lld", s->state_size);
224 sn_info = qemu_mallocz(sizeof(QEMUSnapshotInfo));
226 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), "vmstate");
227 pstrcpy(sn_info->name, sizeof(sn_info->name), "vmstate");
228 sn_info->vm_state_size = s->state_size;
231 return s->state_size ? 1 : 0;
234 static int64_t vmstate_getlength(BlockDriverState *bs)
236 TRACE("returning big enough number...");
237 return 1LL << 63; /* big enough? */
240 BlockDriver bdrv_vmstate = {
241 .format_name = "vmstate",
242 .instance_size = sizeof(BDRVVMState),
243 .bdrv_probe = vmstate_probe,
244 .bdrv_open = vmstate_open,
245 .bdrv_read = vmstate_read,
246 .bdrv_write = vmstate_write,
247 .bdrv_close = vmstate_close,
248 .bdrv_create = vmstate_create,
249 .bdrv_flush = vmstate_flush,
250 .bdrv_getlength = vmstate_getlength,
251 .bdrv_snapshot_create = vmstate_snapshot_create,
252 .bdrv_snapshot_goto = vmstate_snapshot_goto,
253 .bdrv_snapshot_delete = vmstate_snapshot_delete,
254 .bdrv_snapshot_list = vmstate_snapshot_list,
255 .bdrv_get_info = vmstate_get_info,
256 .bdrv_put_buffer = vmstate_put_buffer,
257 .bdrv_get_buffer = vmstate_get_buffer,