blob: 44243e0e95bfbfb432bb315a94d4dab36d197c73 [file] [log] [blame]
Kevin Wolfc1424422009-05-28 16:07:06 +02001/*
2 * Block driver for the QCOW version 2 format
3 *
4 * Copyright (c) 2004-2006 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
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
Peter Maydell80c71a22016-01-18 18:01:42 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010027#include "block/block_int.h"
Kevin Wolfc1424422009-05-28 16:07:06 +020028#include "block/qcow2.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010029#include "qemu/bswap.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010030#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Kevin Wolfc1424422009-05-28 16:07:06 +020032
Kevin Wolfed6ccf02009-05-28 16:07:07 +020033void qcow2_free_snapshots(BlockDriverState *bs)
Kevin Wolfc1424422009-05-28 16:07:06 +020034{
Kevin Wolfff991292015-09-07 17:12:56 +020035 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +020036 int i;
37
38 for(i = 0; i < s->nb_snapshots; i++) {
Anthony Liguori7267c092011-08-20 22:09:37 -050039 g_free(s->snapshots[i].name);
40 g_free(s->snapshots[i].id_str);
Kevin Wolfc1424422009-05-28 16:07:06 +020041 }
Anthony Liguori7267c092011-08-20 22:09:37 -050042 g_free(s->snapshots);
Kevin Wolfc1424422009-05-28 16:07:06 +020043 s->snapshots = NULL;
44 s->nb_snapshots = 0;
45}
46
Kevin Wolfed6ccf02009-05-28 16:07:07 +020047int qcow2_read_snapshots(BlockDriverState *bs)
Kevin Wolfc1424422009-05-28 16:07:06 +020048{
Kevin Wolfff991292015-09-07 17:12:56 +020049 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +020050 QCowSnapshotHeader h;
Kevin Wolfc2c9a462011-11-16 11:35:54 +010051 QCowSnapshotExtraData extra;
Kevin Wolfc1424422009-05-28 16:07:06 +020052 QCowSnapshot *sn;
53 int i, id_str_size, name_size;
54 int64_t offset;
55 uint32_t extra_data_size;
Kevin Wolf42deb292011-11-16 11:43:28 +010056 int ret;
Kevin Wolfc1424422009-05-28 16:07:06 +020057
58 if (!s->nb_snapshots) {
59 s->snapshots = NULL;
60 s->snapshots_size = 0;
61 return 0;
62 }
63
64 offset = s->snapshots_offset;
Markus Armbruster5839e532014-08-19 10:31:08 +020065 s->snapshots = g_new0(QCowSnapshot, s->nb_snapshots);
Kevin Wolf42deb292011-11-16 11:43:28 +010066
Kevin Wolfc1424422009-05-28 16:07:06 +020067 for(i = 0; i < s->nb_snapshots; i++) {
Kevin Wolf42deb292011-11-16 11:43:28 +010068 /* Read statically sized part of the snapshot header */
Kevin Wolfc1424422009-05-28 16:07:06 +020069 offset = align_offset(offset, 8);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +020070 ret = bdrv_pread(bs->file, offset, &h, sizeof(h));
Kevin Wolf42deb292011-11-16 11:43:28 +010071 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +020072 goto fail;
Kevin Wolf42deb292011-11-16 11:43:28 +010073 }
74
Kevin Wolfc1424422009-05-28 16:07:06 +020075 offset += sizeof(h);
76 sn = s->snapshots + i;
77 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
78 sn->l1_size = be32_to_cpu(h.l1_size);
79 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
80 sn->date_sec = be32_to_cpu(h.date_sec);
81 sn->date_nsec = be32_to_cpu(h.date_nsec);
82 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
83 extra_data_size = be32_to_cpu(h.extra_data_size);
84
85 id_str_size = be16_to_cpu(h.id_str_size);
86 name_size = be16_to_cpu(h.name_size);
87
Kevin Wolfc2c9a462011-11-16 11:35:54 +010088 /* Read extra data */
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +020089 ret = bdrv_pread(bs->file, offset, &extra,
Kevin Wolfc2c9a462011-11-16 11:35:54 +010090 MIN(sizeof(extra), extra_data_size));
91 if (ret < 0) {
92 goto fail;
93 }
Kevin Wolfc1424422009-05-28 16:07:06 +020094 offset += extra_data_size;
95
Kevin Wolfc2c9a462011-11-16 11:35:54 +010096 if (extra_data_size >= 8) {
97 sn->vm_state_size = be64_to_cpu(extra.vm_state_size_large);
98 }
99
Kevin Wolf90b27752012-04-11 16:33:50 +0200100 if (extra_data_size >= 16) {
101 sn->disk_size = be64_to_cpu(extra.disk_size);
102 } else {
103 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
104 }
105
Kevin Wolf42deb292011-11-16 11:43:28 +0100106 /* Read snapshot ID */
Anthony Liguori7267c092011-08-20 22:09:37 -0500107 sn->id_str = g_malloc(id_str_size + 1);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200108 ret = bdrv_pread(bs->file, offset, sn->id_str, id_str_size);
Kevin Wolf42deb292011-11-16 11:43:28 +0100109 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200110 goto fail;
Kevin Wolf42deb292011-11-16 11:43:28 +0100111 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200112 offset += id_str_size;
113 sn->id_str[id_str_size] = '\0';
114
Kevin Wolf42deb292011-11-16 11:43:28 +0100115 /* Read snapshot name */
Anthony Liguori7267c092011-08-20 22:09:37 -0500116 sn->name = g_malloc(name_size + 1);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200117 ret = bdrv_pread(bs->file, offset, sn->name, name_size);
Kevin Wolf42deb292011-11-16 11:43:28 +0100118 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200119 goto fail;
Kevin Wolf42deb292011-11-16 11:43:28 +0100120 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200121 offset += name_size;
122 sn->name[name_size] = '\0';
Kevin Wolf5dae6e32014-03-26 13:06:07 +0100123
124 if (offset - s->snapshots_offset > QCOW_MAX_SNAPSHOTS_SIZE) {
125 ret = -EFBIG;
126 goto fail;
127 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200128 }
Kevin Wolf42deb292011-11-16 11:43:28 +0100129
Kevin Wolf5dae6e32014-03-26 13:06:07 +0100130 assert(offset - s->snapshots_offset <= INT_MAX);
Kevin Wolfc1424422009-05-28 16:07:06 +0200131 s->snapshots_size = offset - s->snapshots_offset;
132 return 0;
Kevin Wolf42deb292011-11-16 11:43:28 +0100133
134fail:
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200135 qcow2_free_snapshots(bs);
Kevin Wolf42deb292011-11-16 11:43:28 +0100136 return ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200137}
138
139/* add at the end of the file a new list of snapshots */
Jes Sorensen7c80ab32010-12-17 16:02:39 +0100140static int qcow2_write_snapshots(BlockDriverState *bs)
Kevin Wolfc1424422009-05-28 16:07:06 +0200141{
Kevin Wolfff991292015-09-07 17:12:56 +0200142 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +0200143 QCowSnapshot *sn;
144 QCowSnapshotHeader h;
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100145 QCowSnapshotExtraData extra;
Kevin Wolfc1424422009-05-28 16:07:06 +0200146 int i, name_size, id_str_size, snapshots_size;
Kevin Wolfd69969c2011-11-18 18:27:00 +0100147 struct {
148 uint32_t nb_snapshots;
149 uint64_t snapshots_offset;
150 } QEMU_PACKED header_data;
Kevin Wolf5dae6e32014-03-26 13:06:07 +0100151 int64_t offset, snapshots_offset = 0;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100152 int ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200153
154 /* compute the size of the snapshots */
155 offset = 0;
156 for(i = 0; i < s->nb_snapshots; i++) {
157 sn = s->snapshots + i;
158 offset = align_offset(offset, 8);
159 offset += sizeof(h);
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100160 offset += sizeof(extra);
Kevin Wolfc1424422009-05-28 16:07:06 +0200161 offset += strlen(sn->id_str);
162 offset += strlen(sn->name);
Kevin Wolf5dae6e32014-03-26 13:06:07 +0100163
164 if (offset > QCOW_MAX_SNAPSHOTS_SIZE) {
165 ret = -EFBIG;
166 goto fail;
167 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200168 }
Kevin Wolf5dae6e32014-03-26 13:06:07 +0100169
170 assert(offset <= INT_MAX);
Kevin Wolfc1424422009-05-28 16:07:06 +0200171 snapshots_size = offset;
172
Kevin Wolf07fd8772011-11-16 12:00:59 +0100173 /* Allocate space for the new snapshot list */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200174 snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
Kevin Wolfc1424422009-05-28 16:07:06 +0200175 offset = snapshots_offset;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100176 if (offset < 0) {
Max Reitz37d41f02013-10-09 10:51:04 +0200177 ret = offset;
178 goto fail;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100179 }
Stefan Hajnoczif6977f12013-03-04 15:02:31 +0100180 ret = bdrv_flush(bs);
181 if (ret < 0) {
Max Reitz37d41f02013-10-09 10:51:04 +0200182 goto fail;
Stefan Hajnoczif6977f12013-03-04 15:02:31 +0100183 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200184
Max Reitzcf939802013-08-30 14:34:26 +0200185 /* The snapshot list position has not yet been updated, so these clusters
186 * must indeed be completely free */
Max Reitz231bb262013-10-10 11:09:23 +0200187 ret = qcow2_pre_write_overlap_check(bs, 0, offset, snapshots_size);
Max Reitzcf939802013-08-30 14:34:26 +0200188 if (ret < 0) {
Max Reitz37d41f02013-10-09 10:51:04 +0200189 goto fail;
Max Reitzcf939802013-08-30 14:34:26 +0200190 }
191
192
Kevin Wolf07fd8772011-11-16 12:00:59 +0100193 /* Write all snapshots to the new list */
Kevin Wolfc1424422009-05-28 16:07:06 +0200194 for(i = 0; i < s->nb_snapshots; i++) {
195 sn = s->snapshots + i;
196 memset(&h, 0, sizeof(h));
197 h.l1_table_offset = cpu_to_be64(sn->l1_table_offset);
198 h.l1_size = cpu_to_be32(sn->l1_size);
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100199 /* If it doesn't fit in 32 bit, older implementations should treat it
200 * as a disk-only snapshot rather than truncate the VM state */
201 if (sn->vm_state_size <= 0xffffffff) {
202 h.vm_state_size = cpu_to_be32(sn->vm_state_size);
203 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200204 h.date_sec = cpu_to_be32(sn->date_sec);
205 h.date_nsec = cpu_to_be32(sn->date_nsec);
206 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100207 h.extra_data_size = cpu_to_be32(sizeof(extra));
208
209 memset(&extra, 0, sizeof(extra));
210 extra.vm_state_size_large = cpu_to_be64(sn->vm_state_size);
Kevin Wolf90b27752012-04-11 16:33:50 +0200211 extra.disk_size = cpu_to_be64(sn->disk_size);
Kevin Wolfc1424422009-05-28 16:07:06 +0200212
213 id_str_size = strlen(sn->id_str);
214 name_size = strlen(sn->name);
Max Reitz88fb1532013-10-09 10:51:06 +0200215 assert(id_str_size <= UINT16_MAX && name_size <= UINT16_MAX);
Kevin Wolfc1424422009-05-28 16:07:06 +0200216 h.id_str_size = cpu_to_be16(id_str_size);
217 h.name_size = cpu_to_be16(name_size);
218 offset = align_offset(offset, 8);
Kevin Wolf07fd8772011-11-16 12:00:59 +0100219
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200220 ret = bdrv_pwrite(bs->file, offset, &h, sizeof(h));
Kevin Wolf07fd8772011-11-16 12:00:59 +0100221 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200222 goto fail;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100223 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200224 offset += sizeof(h);
Kevin Wolf07fd8772011-11-16 12:00:59 +0100225
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200226 ret = bdrv_pwrite(bs->file, offset, &extra, sizeof(extra));
Kevin Wolfc2c9a462011-11-16 11:35:54 +0100227 if (ret < 0) {
228 goto fail;
229 }
230 offset += sizeof(extra);
231
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200232 ret = bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size);
Kevin Wolf07fd8772011-11-16 12:00:59 +0100233 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200234 goto fail;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100235 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200236 offset += id_str_size;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100237
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200238 ret = bdrv_pwrite(bs->file, offset, sn->name, name_size);
Kevin Wolf07fd8772011-11-16 12:00:59 +0100239 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200240 goto fail;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100241 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200242 offset += name_size;
243 }
244
Kevin Wolf07fd8772011-11-16 12:00:59 +0100245 /*
246 * Update the header to point to the new snapshot table. This requires the
247 * new table and its refcounts to be stable on disk.
Kevin Wolf07fd8772011-11-16 12:00:59 +0100248 */
249 ret = bdrv_flush(bs);
250 if (ret < 0) {
251 goto fail;
252 }
253
Kevin Wolfd69969c2011-11-18 18:27:00 +0100254 QEMU_BUILD_BUG_ON(offsetof(QCowHeader, snapshots_offset) !=
255 offsetof(QCowHeader, nb_snapshots) + sizeof(header_data.nb_snapshots));
Kevin Wolf07fd8772011-11-16 12:00:59 +0100256
Kevin Wolfd69969c2011-11-18 18:27:00 +0100257 header_data.nb_snapshots = cpu_to_be32(s->nb_snapshots);
258 header_data.snapshots_offset = cpu_to_be64(snapshots_offset);
259
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200260 ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, nb_snapshots),
Kevin Wolfd69969c2011-11-18 18:27:00 +0100261 &header_data, sizeof(header_data));
Kevin Wolf07fd8772011-11-16 12:00:59 +0100262 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200263 goto fail;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100264 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200265
266 /* free the old snapshot table */
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200267 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size,
268 QCOW2_DISCARD_SNAPSHOT);
Kevin Wolfc1424422009-05-28 16:07:06 +0200269 s->snapshots_offset = snapshots_offset;
270 s->snapshots_size = snapshots_size;
271 return 0;
Kevin Wolf07fd8772011-11-16 12:00:59 +0100272
273fail:
Max Reitz9186ad92013-10-09 10:51:05 +0200274 if (snapshots_offset > 0) {
275 qcow2_free_clusters(bs, snapshots_offset, snapshots_size,
276 QCOW2_DISCARD_ALWAYS);
277 }
Kevin Wolf07fd8772011-11-16 12:00:59 +0100278 return ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200279}
280
281static void find_new_snapshot_id(BlockDriverState *bs,
282 char *id_str, int id_str_size)
283{
Kevin Wolfff991292015-09-07 17:12:56 +0200284 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +0200285 QCowSnapshot *sn;
Max Reitz00c49b22013-10-09 14:42:47 +0200286 int i;
287 unsigned long id, id_max = 0;
Kevin Wolfc1424422009-05-28 16:07:06 +0200288
289 for(i = 0; i < s->nb_snapshots; i++) {
290 sn = s->snapshots + i;
291 id = strtoul(sn->id_str, NULL, 10);
292 if (id > id_max)
293 id_max = id;
294 }
Max Reitz00c49b22013-10-09 14:42:47 +0200295 snprintf(id_str, id_str_size, "%lu", id_max + 1);
Kevin Wolfc1424422009-05-28 16:07:06 +0200296}
297
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800298static int find_snapshot_by_id_and_name(BlockDriverState *bs,
299 const char *id,
300 const char *name)
Kevin Wolfc1424422009-05-28 16:07:06 +0200301{
Kevin Wolfff991292015-09-07 17:12:56 +0200302 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +0200303 int i;
304
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800305 if (id && name) {
306 for (i = 0; i < s->nb_snapshots; i++) {
307 if (!strcmp(s->snapshots[i].id_str, id) &&
308 !strcmp(s->snapshots[i].name, name)) {
309 return i;
310 }
311 }
312 } else if (id) {
313 for (i = 0; i < s->nb_snapshots; i++) {
314 if (!strcmp(s->snapshots[i].id_str, id)) {
315 return i;
316 }
317 }
318 } else if (name) {
319 for (i = 0; i < s->nb_snapshots; i++) {
320 if (!strcmp(s->snapshots[i].name, name)) {
321 return i;
322 }
323 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200324 }
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800325
Kevin Wolfc1424422009-05-28 16:07:06 +0200326 return -1;
327}
328
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800329static int find_snapshot_by_id_or_name(BlockDriverState *bs,
330 const char *id_or_name)
Kevin Wolfc1424422009-05-28 16:07:06 +0200331{
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800332 int ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200333
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800334 ret = find_snapshot_by_id_and_name(bs, id_or_name, NULL);
335 if (ret >= 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200336 return ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200337 }
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800338 return find_snapshot_by_id_and_name(bs, NULL, id_or_name);
Kevin Wolfc1424422009-05-28 16:07:06 +0200339}
340
341/* if no id is provided, a new one is constructed */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200342int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
Kevin Wolfc1424422009-05-28 16:07:06 +0200343{
Kevin Wolfff991292015-09-07 17:12:56 +0200344 BDRVQcow2State *s = bs->opaque;
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100345 QCowSnapshot *new_snapshot_list = NULL;
346 QCowSnapshot *old_snapshot_list = NULL;
347 QCowSnapshot sn1, *sn = &sn1;
Kevin Wolfc1424422009-05-28 16:07:06 +0200348 int i, ret;
349 uint64_t *l1_table = NULL;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100350 int64_t l1_table_offset;
Kevin Wolfc1424422009-05-28 16:07:06 +0200351
Kevin Wolfce48f2f42014-03-26 13:05:45 +0100352 if (s->nb_snapshots >= QCOW_MAX_SNAPSHOTS) {
353 return -EFBIG;
354 }
355
Kevin Wolfc1424422009-05-28 16:07:06 +0200356 memset(sn, 0, sizeof(*sn));
357
Yi Wang407bc152015-03-12 22:54:42 +0800358 /* Generate an ID */
359 find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str));
Kevin Wolfc1424422009-05-28 16:07:06 +0200360
Kevin Wolf03343162011-11-16 17:46:29 +0100361 /* Check that the ID is unique */
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800362 if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) {
Zhi Yong Wu647cc472012-04-26 16:11:37 +0800363 return -EEXIST;
Kevin Wolf03343162011-11-16 17:46:29 +0100364 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200365
Kevin Wolf03343162011-11-16 17:46:29 +0100366 /* Populate sn with passed data */
Anthony Liguori7267c092011-08-20 22:09:37 -0500367 sn->id_str = g_strdup(sn_info->id_str);
Anthony Liguori7267c092011-08-20 22:09:37 -0500368 sn->name = g_strdup(sn_info->name);
Kevin Wolf03343162011-11-16 17:46:29 +0100369
Kevin Wolf90b27752012-04-11 16:33:50 +0200370 sn->disk_size = bs->total_sectors * BDRV_SECTOR_SIZE;
Kevin Wolfc1424422009-05-28 16:07:06 +0200371 sn->vm_state_size = sn_info->vm_state_size;
372 sn->date_sec = sn_info->date_sec;
373 sn->date_nsec = sn_info->date_nsec;
374 sn->vm_clock_nsec = sn_info->vm_clock_nsec;
375
Kevin Wolf03343162011-11-16 17:46:29 +0100376 /* Allocate the L1 table of the snapshot and copy the current one there. */
Kevin Wolf5d757b52010-01-20 15:04:01 +0100377 l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
378 if (l1_table_offset < 0) {
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100379 ret = l1_table_offset;
Kevin Wolf5d757b52010-01-20 15:04:01 +0100380 goto fail;
381 }
382
383 sn->l1_table_offset = l1_table_offset;
Kevin Wolfc1424422009-05-28 16:07:06 +0200384 sn->l1_size = s->l1_size;
385
Markus Armbruster5839e532014-08-19 10:31:08 +0200386 l1_table = g_try_new(uint64_t, s->l1_size);
Kevin Wolfde828152014-05-20 17:12:47 +0200387 if (s->l1_size && l1_table == NULL) {
388 ret = -ENOMEM;
389 goto fail;
390 }
391
Kevin Wolfc1424422009-05-28 16:07:06 +0200392 for(i = 0; i < s->l1_size; i++) {
393 l1_table[i] = cpu_to_be64(s->l1_table[i]);
394 }
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100395
Max Reitz231bb262013-10-10 11:09:23 +0200396 ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
397 s->l1_size * sizeof(uint64_t));
Max Reitzcf939802013-08-30 14:34:26 +0200398 if (ret < 0) {
399 goto fail;
400 }
401
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200402 ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100403 s->l1_size * sizeof(uint64_t));
404 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200405 goto fail;
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100406 }
407
Anthony Liguori7267c092011-08-20 22:09:37 -0500408 g_free(l1_table);
Kevin Wolfc1424422009-05-28 16:07:06 +0200409 l1_table = NULL;
410
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100411 /*
412 * Increase the refcounts of all clusters and make sure everything is
413 * stable on disk before updating the snapshot table to contain a pointer
414 * to the new L1 table.
415 */
416 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
417 if (ret < 0) {
418 goto fail;
Kevin Wolfc1424422009-05-28 16:07:06 +0200419 }
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100420
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100421 /* Append the new snapshot to the snapshot list */
Markus Armbruster5839e532014-08-19 10:31:08 +0200422 new_snapshot_list = g_new(QCowSnapshot, s->nb_snapshots + 1);
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100423 if (s->snapshots) {
424 memcpy(new_snapshot_list, s->snapshots,
425 s->nb_snapshots * sizeof(QCowSnapshot));
426 old_snapshot_list = s->snapshots;
427 }
428 s->snapshots = new_snapshot_list;
Kevin Wolfc1424422009-05-28 16:07:06 +0200429 s->snapshots[s->nb_snapshots++] = *sn;
430
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100431 ret = qcow2_write_snapshots(bs);
432 if (ret < 0) {
433 g_free(s->snapshots);
434 s->snapshots = old_snapshot_list;
Max Reitz84757f72013-10-09 14:42:00 +0200435 s->nb_snapshots--;
Kevin Wolfc1424422009-05-28 16:07:06 +0200436 goto fail;
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100437 }
438
439 g_free(old_snapshot_list);
440
Kevin Wolf1ebf5612013-09-06 12:20:08 +0200441 /* The VM state isn't needed any more in the active L1 table; in fact, it
442 * hurts by causing expensive COW for the next snapshot. */
Eric Blaked2cb36a2017-05-06 19:05:52 -0500443 qcow2_cluster_discard(bs, qcow2_vm_state_offset(s),
444 align_offset(sn->vm_state_size, s->cluster_size),
445 QCOW2_DISCARD_NEVER, false);
Kevin Wolf1ebf5612013-09-06 12:20:08 +0200446
Kevin Wolfc1424422009-05-28 16:07:06 +0200447#ifdef DEBUG_ALLOC
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200448 {
449 BdrvCheckResult result = {0};
Stefan Hajnoczib35278f2012-06-15 16:41:07 +0100450 qcow2_check_refcounts(bs, &result, 0);
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200451 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200452#endif
453 return 0;
Kevin Wolf03343162011-11-16 17:46:29 +0100454
455fail:
456 g_free(sn->id_str);
Anthony Liguori7267c092011-08-20 22:09:37 -0500457 g_free(sn->name);
458 g_free(l1_table);
Kevin Wolfd1ea98d2011-11-16 12:43:59 +0100459
460 return ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200461}
462
463/* copy the snapshot 'snapshot_name' into the current disk image */
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200464int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
Kevin Wolfc1424422009-05-28 16:07:06 +0200465{
Kevin Wolfff991292015-09-07 17:12:56 +0200466 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +0200467 QCowSnapshot *sn;
Kevin Wolf35d7ace2011-08-05 12:06:11 +0200468 int i, snapshot_index;
469 int cur_l1_bytes, sn_l1_bytes;
Kevin Wolf589f2842011-11-16 15:04:11 +0100470 int ret;
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100471 uint64_t *sn_l1_table = NULL;
Kevin Wolfc1424422009-05-28 16:07:06 +0200472
Kevin Wolf589f2842011-11-16 15:04:11 +0100473 /* Search the snapshot */
Kevin Wolfc1424422009-05-28 16:07:06 +0200474 snapshot_index = find_snapshot_by_id_or_name(bs, snapshot_id);
Kevin Wolf589f2842011-11-16 15:04:11 +0100475 if (snapshot_index < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200476 return -ENOENT;
Kevin Wolf589f2842011-11-16 15:04:11 +0100477 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200478 sn = &s->snapshots[snapshot_index];
479
Kevin Wolf90b27752012-04-11 16:33:50 +0200480 if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) {
481 error_report("qcow2: Loading snapshots with different disk "
482 "size is not implemented");
483 ret = -ENOTSUP;
484 goto fail;
485 }
486
Kevin Wolf589f2842011-11-16 15:04:11 +0100487 /*
488 * Make sure that the current L1 table is big enough to contain the whole
489 * L1 table of the snapshot. If the snapshot L1 table is smaller, the
490 * current one must be padded with zeros.
491 */
492 ret = qcow2_grow_l1_table(bs, sn->l1_size, true);
493 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200494 goto fail;
Kevin Wolf589f2842011-11-16 15:04:11 +0100495 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200496
Kevin Wolf35d7ace2011-08-05 12:06:11 +0200497 cur_l1_bytes = s->l1_size * sizeof(uint64_t);
498 sn_l1_bytes = sn->l1_size * sizeof(uint64_t);
499
Kevin Wolf589f2842011-11-16 15:04:11 +0100500 /*
501 * Copy the snapshot L1 table to the current L1 table.
502 *
503 * Before overwriting the old current L1 table on disk, make sure to
504 * increase all refcounts for the clusters referenced by the new one.
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100505 * Decrease the refcount referenced by the old one only when the L1
506 * table is overwritten.
Kevin Wolf589f2842011-11-16 15:04:11 +0100507 */
Kevin Wolfde828152014-05-20 17:12:47 +0200508 sn_l1_table = g_try_malloc0(cur_l1_bytes);
509 if (cur_l1_bytes && sn_l1_table == NULL) {
510 ret = -ENOMEM;
511 goto fail;
512 }
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100513
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200514 ret = bdrv_pread(bs->file, sn->l1_table_offset,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200515 sn_l1_table, sn_l1_bytes);
Kevin Wolf589f2842011-11-16 15:04:11 +0100516 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200517 goto fail;
Kevin Wolf589f2842011-11-16 15:04:11 +0100518 }
519
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100520 ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset,
521 sn->l1_size, 1);
522 if (ret < 0) {
523 goto fail;
524 }
525
Max Reitz231bb262013-10-10 11:09:23 +0200526 ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1,
527 s->l1_table_offset, cur_l1_bytes);
Max Reitzcf939802013-08-30 14:34:26 +0200528 if (ret < 0) {
529 goto fail;
530 }
531
Kevin Wolfd9ca2ea2016-06-20 20:09:15 +0200532 ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset, sn_l1_table,
Kevin Wolf589f2842011-11-16 15:04:11 +0100533 cur_l1_bytes);
534 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200535 goto fail;
Kevin Wolf589f2842011-11-16 15:04:11 +0100536 }
537
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100538 /*
539 * Decrease refcount of clusters of current L1 table.
540 *
541 * At this point, the in-memory s->l1_table points to the old L1 table,
542 * whereas on disk we already have the new one.
543 *
544 * qcow2_update_snapshot_refcount special cases the current L1 table to use
545 * the in-memory data instead of really using the offset to load a new one,
546 * which is why this works.
547 */
548 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
549 s->l1_size, -1);
550
551 /*
552 * Now update the in-memory L1 table to be in sync with the on-disk one. We
553 * need to do this even if updating refcounts failed.
554 */
Kevin Wolfc1424422009-05-28 16:07:06 +0200555 for(i = 0;i < s->l1_size; i++) {
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100556 s->l1_table[i] = be64_to_cpu(sn_l1_table[i]);
Kevin Wolfc1424422009-05-28 16:07:06 +0200557 }
558
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100559 if (ret < 0) {
560 goto fail;
561 }
562
563 g_free(sn_l1_table);
564 sn_l1_table = NULL;
565
566 /*
567 * Update QCOW_OFLAG_COPIED in the active L1 table (it may have changed
568 * when we decreased the refcount of the old snapshot.
569 */
570 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
Kevin Wolf589f2842011-11-16 15:04:11 +0100571 if (ret < 0) {
Kevin Wolfc1424422009-05-28 16:07:06 +0200572 goto fail;
Kevin Wolf589f2842011-11-16 15:04:11 +0100573 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200574
575#ifdef DEBUG_ALLOC
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200576 {
577 BdrvCheckResult result = {0};
Stefan Hajnoczib35278f2012-06-15 16:41:07 +0100578 qcow2_check_refcounts(bs, &result, 0);
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200579 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200580#endif
581 return 0;
Kevin Wolf589f2842011-11-16 15:04:11 +0100582
583fail:
Kevin Wolf43a0cac2011-11-16 15:20:45 +0100584 g_free(sn_l1_table);
Kevin Wolf589f2842011-11-16 15:04:11 +0100585 return ret;
Kevin Wolfc1424422009-05-28 16:07:06 +0200586}
587
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800588int qcow2_snapshot_delete(BlockDriverState *bs,
589 const char *snapshot_id,
590 const char *name,
591 Error **errp)
Kevin Wolfc1424422009-05-28 16:07:06 +0200592{
Kevin Wolfff991292015-09-07 17:12:56 +0200593 BDRVQcow2State *s = bs->opaque;
Kevin Wolf9a476782011-11-16 17:22:10 +0100594 QCowSnapshot sn;
Kevin Wolfc1424422009-05-28 16:07:06 +0200595 int snapshot_index, ret;
596
Kevin Wolf9a476782011-11-16 17:22:10 +0100597 /* Search the snapshot */
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800598 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
Kevin Wolf9a476782011-11-16 17:22:10 +0100599 if (snapshot_index < 0) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800600 error_setg(errp, "Can't find the snapshot");
Kevin Wolfc1424422009-05-28 16:07:06 +0200601 return -ENOENT;
Kevin Wolf9a476782011-11-16 17:22:10 +0100602 }
603 sn = s->snapshots[snapshot_index];
Kevin Wolfc1424422009-05-28 16:07:06 +0200604
Kevin Wolf9a476782011-11-16 17:22:10 +0100605 /* Remove it from the snapshot list */
606 memmove(s->snapshots + snapshot_index,
607 s->snapshots + snapshot_index + 1,
608 (s->nb_snapshots - snapshot_index - 1) * sizeof(sn));
Kevin Wolfc1424422009-05-28 16:07:06 +0200609 s->nb_snapshots--;
Jes Sorensen7c80ab32010-12-17 16:02:39 +0100610 ret = qcow2_write_snapshots(bs);
Kevin Wolfc1424422009-05-28 16:07:06 +0200611 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -0500612 error_setg_errno(errp, -ret,
613 "Failed to remove snapshot from snapshot list");
Kevin Wolfc1424422009-05-28 16:07:06 +0200614 return ret;
615 }
Kevin Wolf9a476782011-11-16 17:22:10 +0100616
617 /*
618 * The snapshot is now unused, clean up. If we fail after this point, we
619 * won't recover but just leak clusters.
620 */
621 g_free(sn.id_str);
622 g_free(sn.name);
623
624 /*
625 * Now decrease the refcounts of clusters referenced by the snapshot and
626 * free the L1 table.
627 */
628 ret = qcow2_update_snapshot_refcount(bs, sn.l1_table_offset,
629 sn.l1_size, -1);
630 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -0500631 error_setg_errno(errp, -ret, "Failed to free the cluster and L1 table");
Kevin Wolf9a476782011-11-16 17:22:10 +0100632 return ret;
633 }
Kevin Wolf6cfcb9b2013-06-19 13:44:18 +0200634 qcow2_free_clusters(bs, sn.l1_table_offset, sn.l1_size * sizeof(uint64_t),
635 QCOW2_DISCARD_SNAPSHOT);
Kevin Wolf9a476782011-11-16 17:22:10 +0100636
637 /* must update the copied flag on the current cluster offsets */
638 ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
639 if (ret < 0) {
Jeff Cody39a611a2014-02-12 14:46:24 -0500640 error_setg_errno(errp, -ret,
641 "Failed to update snapshot status in disk");
Kevin Wolf9a476782011-11-16 17:22:10 +0100642 return ret;
643 }
644
Kevin Wolfc1424422009-05-28 16:07:06 +0200645#ifdef DEBUG_ALLOC
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200646 {
647 BdrvCheckResult result = {0};
Stefan Hajnoczib35278f2012-06-15 16:41:07 +0100648 qcow2_check_refcounts(bs, &result, 0);
Philipp Hahn6cbc3032011-08-04 19:22:10 +0200649 }
Kevin Wolfc1424422009-05-28 16:07:06 +0200650#endif
651 return 0;
652}
653
Kevin Wolfed6ccf02009-05-28 16:07:07 +0200654int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
Kevin Wolfc1424422009-05-28 16:07:06 +0200655{
Kevin Wolfff991292015-09-07 17:12:56 +0200656 BDRVQcow2State *s = bs->opaque;
Kevin Wolfc1424422009-05-28 16:07:06 +0200657 QEMUSnapshotInfo *sn_tab, *sn_info;
658 QCowSnapshot *sn;
659 int i;
660
661 if (!s->nb_snapshots) {
662 *psn_tab = NULL;
663 return s->nb_snapshots;
664 }
665
Markus Armbruster5839e532014-08-19 10:31:08 +0200666 sn_tab = g_new0(QEMUSnapshotInfo, s->nb_snapshots);
Kevin Wolfc1424422009-05-28 16:07:06 +0200667 for(i = 0; i < s->nb_snapshots; i++) {
668 sn_info = sn_tab + i;
669 sn = s->snapshots + i;
670 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str),
671 sn->id_str);
672 pstrcpy(sn_info->name, sizeof(sn_info->name),
673 sn->name);
674 sn_info->vm_state_size = sn->vm_state_size;
675 sn_info->date_sec = sn->date_sec;
676 sn_info->date_nsec = sn->date_nsec;
677 sn_info->vm_clock_nsec = sn->vm_clock_nsec;
678 }
679 *psn_tab = sn_tab;
680 return s->nb_snapshots;
681}
682
Wenchao Xia7b4c4782013-12-04 17:10:54 +0800683int qcow2_snapshot_load_tmp(BlockDriverState *bs,
684 const char *snapshot_id,
685 const char *name,
686 Error **errp)
edison51ef6722010-09-21 19:58:41 -0700687{
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100688 int i, snapshot_index;
Kevin Wolfff991292015-09-07 17:12:56 +0200689 BDRVQcow2State *s = bs->opaque;
edison51ef6722010-09-21 19:58:41 -0700690 QCowSnapshot *sn;
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100691 uint64_t *new_l1_table;
692 int new_l1_bytes;
693 int ret;
edison51ef6722010-09-21 19:58:41 -0700694
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100695 assert(bs->read_only);
696
697 /* Search the snapshot */
Wenchao Xia7b4c4782013-12-04 17:10:54 +0800698 snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
edison51ef6722010-09-21 19:58:41 -0700699 if (snapshot_index < 0) {
Wenchao Xia7b4c4782013-12-04 17:10:54 +0800700 error_setg(errp,
701 "Can't find snapshot");
edison51ef6722010-09-21 19:58:41 -0700702 return -ENOENT;
703 }
edison51ef6722010-09-21 19:58:41 -0700704 sn = &s->snapshots[snapshot_index];
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100705
706 /* Allocate and read in the snapshot's L1 table */
Wen Congyang87b86e72015-03-11 11:05:21 +0800707 if (sn->l1_size > QCOW_MAX_L1_SIZE / sizeof(uint64_t)) {
Kevin Wolf6a83f8b2014-03-26 13:06:06 +0100708 error_setg(errp, "Snapshot L1 table too large");
709 return -EFBIG;
710 }
Kevin Wolfc05e4662014-03-26 13:06:05 +0100711 new_l1_bytes = sn->l1_size * sizeof(uint64_t);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200712 new_l1_table = qemu_try_blockalign(bs->file->bs,
Kevin Wolfde828152014-05-20 17:12:47 +0200713 align_offset(new_l1_bytes, 512));
714 if (new_l1_table == NULL) {
715 return -ENOMEM;
716 }
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100717
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200718 ret = bdrv_pread(bs->file, sn->l1_table_offset,
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200719 new_l1_table, new_l1_bytes);
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100720 if (ret < 0) {
Wenchao Xia7b4c4782013-12-04 17:10:54 +0800721 error_setg(errp, "Failed to read l1 table for snapshot");
Kevin Wolfde828152014-05-20 17:12:47 +0200722 qemu_vfree(new_l1_table);
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100723 return ret;
724 }
725
726 /* Switch the L1 table */
Kevin Wolfde828152014-05-20 17:12:47 +0200727 qemu_vfree(s->l1_table);
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100728
edison51ef6722010-09-21 19:58:41 -0700729 s->l1_size = sn->l1_size;
edison51ef6722010-09-21 19:58:41 -0700730 s->l1_table_offset = sn->l1_table_offset;
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100731 s->l1_table = new_l1_table;
edison51ef6722010-09-21 19:58:41 -0700732
733 for(i = 0;i < s->l1_size; i++) {
734 be64_to_cpus(&s->l1_table[i]);
735 }
Kevin Wolfe3f652b2011-11-16 17:30:33 +0100736
edison51ef6722010-09-21 19:58:41 -0700737 return 0;
738}