qcow2-snapshot.c (5d757b563d59142ca81e1073a8e8396750a0ad1a) qcow2-snapshot.c (66f82ceed6781261c09e65fb440ca76842fd0500)
1/*
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

--- 65 unchanged lines hidden (view full) ---

74 s->snapshots_size = 0;
75 return 0;
76 }
77
78 offset = s->snapshots_offset;
79 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
80 for(i = 0; i < s->nb_snapshots; i++) {
81 offset = align_offset(offset, 8);
1/*
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

--- 65 unchanged lines hidden (view full) ---

74 s->snapshots_size = 0;
75 return 0;
76 }
77
78 offset = s->snapshots_offset;
79 s->snapshots = qemu_mallocz(s->nb_snapshots * sizeof(QCowSnapshot));
80 for(i = 0; i < s->nb_snapshots; i++) {
81 offset = align_offset(offset, 8);
82 if (bdrv_pread(s->hd, offset, &h, sizeof(h)) != sizeof(h))
82 if (bdrv_pread(bs->file, offset, &h, sizeof(h)) != sizeof(h))
83 goto fail;
84 offset += sizeof(h);
85 sn = s->snapshots + i;
86 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
87 sn->l1_size = be32_to_cpu(h.l1_size);
88 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
89 sn->date_sec = be32_to_cpu(h.date_sec);
90 sn->date_nsec = be32_to_cpu(h.date_nsec);
91 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
92 extra_data_size = be32_to_cpu(h.extra_data_size);
93
94 id_str_size = be16_to_cpu(h.id_str_size);
95 name_size = be16_to_cpu(h.name_size);
96
97 offset += extra_data_size;
98
99 sn->id_str = qemu_malloc(id_str_size + 1);
83 goto fail;
84 offset += sizeof(h);
85 sn = s->snapshots + i;
86 sn->l1_table_offset = be64_to_cpu(h.l1_table_offset);
87 sn->l1_size = be32_to_cpu(h.l1_size);
88 sn->vm_state_size = be32_to_cpu(h.vm_state_size);
89 sn->date_sec = be32_to_cpu(h.date_sec);
90 sn->date_nsec = be32_to_cpu(h.date_nsec);
91 sn->vm_clock_nsec = be64_to_cpu(h.vm_clock_nsec);
92 extra_data_size = be32_to_cpu(h.extra_data_size);
93
94 id_str_size = be16_to_cpu(h.id_str_size);
95 name_size = be16_to_cpu(h.name_size);
96
97 offset += extra_data_size;
98
99 sn->id_str = qemu_malloc(id_str_size + 1);
100 if (bdrv_pread(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
100 if (bdrv_pread(bs->file, offset, sn->id_str, id_str_size) != id_str_size)
101 goto fail;
102 offset += id_str_size;
103 sn->id_str[id_str_size] = '\0';
104
105 sn->name = qemu_malloc(name_size + 1);
101 goto fail;
102 offset += id_str_size;
103 sn->id_str[id_str_size] = '\0';
104
105 sn->name = qemu_malloc(name_size + 1);
106 if (bdrv_pread(s->hd, offset, sn->name, name_size) != name_size)
106 if (bdrv_pread(bs->file, offset, sn->name, name_size) != name_size)
107 goto fail;
108 offset += name_size;
109 sn->name[name_size] = '\0';
110 }
111 s->snapshots_size = offset - s->snapshots_offset;
112 return 0;
113 fail:
114 qcow2_free_snapshots(bs);

--- 38 unchanged lines hidden (view full) ---

153 h.date_nsec = cpu_to_be32(sn->date_nsec);
154 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
155
156 id_str_size = strlen(sn->id_str);
157 name_size = strlen(sn->name);
158 h.id_str_size = cpu_to_be16(id_str_size);
159 h.name_size = cpu_to_be16(name_size);
160 offset = align_offset(offset, 8);
107 goto fail;
108 offset += name_size;
109 sn->name[name_size] = '\0';
110 }
111 s->snapshots_size = offset - s->snapshots_offset;
112 return 0;
113 fail:
114 qcow2_free_snapshots(bs);

--- 38 unchanged lines hidden (view full) ---

153 h.date_nsec = cpu_to_be32(sn->date_nsec);
154 h.vm_clock_nsec = cpu_to_be64(sn->vm_clock_nsec);
155
156 id_str_size = strlen(sn->id_str);
157 name_size = strlen(sn->name);
158 h.id_str_size = cpu_to_be16(id_str_size);
159 h.name_size = cpu_to_be16(name_size);
160 offset = align_offset(offset, 8);
161 if (bdrv_pwrite(s->hd, offset, &h, sizeof(h)) != sizeof(h))
161 if (bdrv_pwrite(bs->file, offset, &h, sizeof(h)) != sizeof(h))
162 goto fail;
163 offset += sizeof(h);
162 goto fail;
163 offset += sizeof(h);
164 if (bdrv_pwrite(s->hd, offset, sn->id_str, id_str_size) != id_str_size)
164 if (bdrv_pwrite(bs->file, offset, sn->id_str, id_str_size) != id_str_size)
165 goto fail;
166 offset += id_str_size;
165 goto fail;
166 offset += id_str_size;
167 if (bdrv_pwrite(s->hd, offset, sn->name, name_size) != name_size)
167 if (bdrv_pwrite(bs->file, offset, sn->name, name_size) != name_size)
168 goto fail;
169 offset += name_size;
170 }
171
172 /* update the various header fields */
173 data64 = cpu_to_be64(snapshots_offset);
168 goto fail;
169 offset += name_size;
170 }
171
172 /* update the various header fields */
173 data64 = cpu_to_be64(snapshots_offset);
174 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, snapshots_offset),
174 if (bdrv_pwrite(bs->file, offsetof(QCowHeader, snapshots_offset),
175 &data64, sizeof(data64)) != sizeof(data64))
176 goto fail;
177 data32 = cpu_to_be32(s->nb_snapshots);
175 &data64, sizeof(data64)) != sizeof(data64))
176 goto fail;
177 data32 = cpu_to_be32(s->nb_snapshots);
178 if (bdrv_pwrite(s->hd, offsetof(QCowHeader, nb_snapshots),
178 if (bdrv_pwrite(bs->file, offsetof(QCowHeader, nb_snapshots),
179 &data32, sizeof(data32)) != sizeof(data32))
180 goto fail;
181
182 /* free the old snapshot table */
183 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
184 s->snapshots_offset = snapshots_offset;
185 s->snapshots_size = snapshots_size;
186 return 0;

--- 92 unchanged lines hidden (view full) ---

279 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
280 } else {
281 l1_table = NULL;
282 }
283
284 for(i = 0; i < s->l1_size; i++) {
285 l1_table[i] = cpu_to_be64(s->l1_table[i]);
286 }
179 &data32, sizeof(data32)) != sizeof(data32))
180 goto fail;
181
182 /* free the old snapshot table */
183 qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
184 s->snapshots_offset = snapshots_offset;
185 s->snapshots_size = snapshots_size;
186 return 0;

--- 92 unchanged lines hidden (view full) ---

279 l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
280 } else {
281 l1_table = NULL;
282 }
283
284 for(i = 0; i < s->l1_size; i++) {
285 l1_table[i] = cpu_to_be64(s->l1_table[i]);
286 }
287 if (bdrv_pwrite(s->hd, sn->l1_table_offset,
287 if (bdrv_pwrite(bs->file, sn->l1_table_offset,
288 l1_table, s->l1_size * sizeof(uint64_t)) !=
289 (s->l1_size * sizeof(uint64_t)))
290 goto fail;
291 qemu_free(l1_table);
292 l1_table = NULL;
293
294 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
295 if (s->snapshots) {

--- 31 unchanged lines hidden (view full) ---

327 goto fail;
328
329 if (qcow2_grow_l1_table(bs, sn->l1_size) < 0)
330 goto fail;
331
332 s->l1_size = sn->l1_size;
333 l1_size2 = s->l1_size * sizeof(uint64_t);
334 /* copy the snapshot l1 table to the current l1 table */
288 l1_table, s->l1_size * sizeof(uint64_t)) !=
289 (s->l1_size * sizeof(uint64_t)))
290 goto fail;
291 qemu_free(l1_table);
292 l1_table = NULL;
293
294 snapshots1 = qemu_malloc((s->nb_snapshots + 1) * sizeof(QCowSnapshot));
295 if (s->snapshots) {

--- 31 unchanged lines hidden (view full) ---

327 goto fail;
328
329 if (qcow2_grow_l1_table(bs, sn->l1_size) < 0)
330 goto fail;
331
332 s->l1_size = sn->l1_size;
333 l1_size2 = s->l1_size * sizeof(uint64_t);
334 /* copy the snapshot l1 table to the current l1 table */
335 if (bdrv_pread(s->hd, sn->l1_table_offset,
335 if (bdrv_pread(bs->file, sn->l1_table_offset,
336 s->l1_table, l1_size2) != l1_size2)
337 goto fail;
336 s->l1_table, l1_size2) != l1_size2)
337 goto fail;
338 if (bdrv_pwrite(s->hd, s->l1_table_offset,
338 if (bdrv_pwrite(bs->file, s->l1_table_offset,
339 s->l1_table, l1_size2) != l1_size2)
340 goto fail;
341 for(i = 0;i < s->l1_size; i++) {
342 be64_to_cpus(&s->l1_table[i]);
343 }
344
345 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
346 goto fail;

--- 73 unchanged lines hidden ---
339 s->l1_table, l1_size2) != l1_size2)
340 goto fail;
341 for(i = 0;i < s->l1_size; i++) {
342 be64_to_cpus(&s->l1_table[i]);
343 }
344
345 if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
346 goto fail;

--- 73 unchanged lines hidden ---