Merge branch 'master' of /home/nchip/public_html/qemu into garage-push
[qemu] / block-vmstate.c
1 /*
2  * Block driver for vmstate format
3  *
4  * Copyright (c) 2009 Nokia Corporation
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
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include "qemu-common.h"
25 #include "block_int.h"
26
27 //#define VMSTATE_DEBUG
28
29 #ifdef VMSTATE_DEBUG
30 #define TRACE(fmt,...) fprintf(stderr, "%s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__)
31 #else
32 #define TRACE(...)
33 #endif
34
35 #define VMSTATE_MAGIC 0x564D53544154451ALL
36 #define VMSTATE_VERSION 1
37
38 typedef struct VMStateHeader {
39     uint64_t magic;
40     uint32_t version;
41     uint64_t state_offset;
42     uint64_t state_size;
43 } VMStateHeader;
44
45 typedef struct BDRVVmState {
46     int fd;
47     uint64_t state_offset;
48     uint64_t state_size;
49     uint64_t write_offset;
50 } BDRVVMState;
51
52 static int vmstate_probe(const uint8_t *buf, int buf_size, const char *filename)
53 {
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)
58         return 100;
59     return 0;
60 }
61
62 static int vmstate_open(BlockDriverState *bs, const char *filename, int flags)
63 {
64     BDRVVMState *s = bs->opaque;
65     VMStateHeader header;
66     
67     TRACE("open \"%s\"...", filename);
68     s->fd = open(filename, O_RDWR | O_BINARY);
69     if (s->fd < 0)
70         return -errno;
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);
76         
77         s->write_offset = s->state_offset;
78         TRACE("open ok, state_offset=%lld", s->state_offset);
79         return 0;
80     }
81     close(s->fd);
82     return -EIO;
83 }
84
85 static void vmstate_flush(BlockDriverState *bs)
86 {
87     TRACE("flushing");
88 }
89
90 static void vmstate_close(BlockDriverState *bs)
91 {
92     BDRVVMState *s = bs->opaque;
93     
94     TRACE("closing");
95     vmstate_flush(bs);
96     close(s->fd);
97 }
98
99 static int vmstate_create(const char *filename, int64_t total_size,
100                           const char *backing_file, int flags)
101 {
102     VMStateHeader header;
103     int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
104     if (fd < 0)
105         return -EIO;
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));
111     close(fd);
112     return 0;
113 }
114
115 static int vmstate_refresh_header(BDRVVMState *s)
116 {
117     VMStateHeader header;
118     
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))
125             return 0;
126     }
127     return -EIO;
128 }
129
130 static int vmstate_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
131 {
132     BDRVVMState *s = bs->opaque;
133     
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;
137     return 0;
138 }
139
140 static int vmstate_get_buffer(BlockDriverState *bs, uint8_t *buf,
141                               int64_t pos, int size)
142 {
143     BDRVVMState *s = bs->opaque;
144     
145     TRACE("pos=%lld, size=%d", pos, size);
146     if (lseek(s->fd, pos, SEEK_SET) != pos)
147         return -EIO;
148     return read(s->fd, buf, size);
149 }
150
151 static int vmstate_read(BlockDriverState *bs, int64_t sector_num,
152                         uint8_t *buf, int nb_sectors)
153 {
154     BDRVVMState *s = bs->opaque;
155     
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)
158         return -EIO;
159     return read(s->fd, buf, nb_sectors * 512);
160 }
161
162 static int vmstate_put_buffer(BlockDriverState *bs, const uint8_t *buf,
163                               int64_t pos, int size)
164 {
165     BDRVVMState *s = bs->opaque;
166     
167     TRACE("pos=%lld, size=%d", pos, size);
168     if (lseek(s->fd, pos, SEEK_SET) != pos)
169         return -EIO;
170     return write(s->fd, buf, size);
171 }
172
173 static int vmstate_write(BlockDriverState *bs, int64_t sector_num,
174                          const uint8_t *buf, int nb_sectors)
175 {
176     BDRVVMState *s = bs->opaque;
177     
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)
180         return -EIO;
181     return write(s->fd, buf, nb_sectors * 512);
182 }
183
184 static int vmstate_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
185 {
186     BDRVVMState *s = bs->opaque;
187
188     TRACE("state_size = %lld", s->state_size);
189     return s->state_size ? 0 : -ENOENT;
190 }
191
192 static int vmstate_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
193 {
194     BDRVVMState *s = bs->opaque;
195     
196     TRACE("delete snapshot \"%s\"...", snapshot_id);
197     if (s->state_size) {
198         s->state_size = 0;
199         vmstate_refresh_header(s);
200         if (!lseek(s->fd, 0, SEEK_SET))
201             return ftruncate(s->fd, sizeof(VMStateHeader));
202     }
203     return -ENOENT;
204 }
205
206 static int vmstate_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
207 {
208     BDRVVMState *s = bs->opaque;
209     
210     TRACE("create snapshot size %u", sn_info->vm_state_size);
211     if (s->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);
216 }
217
218 static int vmstate_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
219 {
220     BDRVVMState *s = bs->opaque;
221     QEMUSnapshotInfo *sn_info;
222     
223     TRACE("state_size = %lld", s->state_size);
224     sn_info = qemu_mallocz(sizeof(QEMUSnapshotInfo));
225     if (s->state_size) {
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;
229     }
230     *psn_tab = sn_info;
231     return s->state_size ? 1 : 0;
232 }
233
234 static int64_t vmstate_getlength(BlockDriverState *bs)
235 {
236     TRACE("returning big enough number...");
237     return 1LL << 63; /* big enough? */
238 }
239
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,
258 };