2 * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 #include "lib/misc/lib.h"
17 #include "lib/metadata/metadata.h"
18 #include "lib/metadata/segtype.h"
19 #include "lib/locking/locking.h"
20 #include "lib/commands/toolcontext.h"
21 #include "lib/metadata/lv_alloc.h"
22 #include "lib/activate/activate.h"
24 #define SNAPSHOT_MIN_CHUNKS 3 /* Minimum number of chunks in snapshot */
26 int lv_is_origin(const struct logical_volume *lv)
28 return lv->origin_count ? 1 : 0;
31 int lv_is_cow(const struct logical_volume *lv)
33 /* Make sure a merging thin origin isn't confused as a cow LV */
34 return (lv->snapshot && !lv_is_thin_volume(lv) && !lv_is_origin(lv)) ? 1 : 0;
37 struct logical_volume *find_cow(const struct logical_volume *snap)
39 return first_seg(snap)->cow;
43 * Some kernels have a bug that they may leak space in the snapshot on crash.
44 * If the kernel is buggy, we add some extra space.
46 static uint64_t _cow_extra_chunks(struct cmd_context *cmd, uint64_t n_chunks)
48 const struct segment_type *segtype;
52 (segtype = get_segtype_from_string(cmd, SEG_TYPE_NAME_SNAPSHOT)) &&
53 segtype->ops->target_present &&
54 segtype->ops->target_present(cmd, NULL, &attrs) &&
55 (attrs & SNAPSHOT_FEATURE_FIXED_LEAK))
58 return (n_chunks + 63) / 64;
61 static uint64_t _cow_max_size(struct cmd_context *cmd, uint64_t origin_size, uint32_t chunk_size)
63 /* Snapshot disk layout:
64 * COW is divided into chunks
65 * 1st. chunk is reserved for header
66 * 2nd. chunk is the 1st. metadata chunk
67 * 3rd. chunk is the 1st. data chunk
70 uint64_t origin_chunks = (origin_size + chunk_size - 1) / chunk_size;
71 uint64_t chunks_per_metadata_area = (uint64_t)chunk_size << (SECTOR_SHIFT - 4);
74 * Note: if origin_chunks is divisible by chunks_per_metadata_area, we
75 * need one extra metadata chunk as a terminator.
77 uint64_t metadata_chunks = (origin_chunks + chunks_per_metadata_area) / chunks_per_metadata_area;
78 uint64_t n_chunks = 1 + origin_chunks + metadata_chunks;
80 return (n_chunks + _cow_extra_chunks(cmd, n_chunks)) * chunk_size;
83 uint32_t cow_max_extents(const struct logical_volume *origin, uint32_t chunk_size)
85 uint64_t size = _cow_max_size(origin->vg->cmd, origin->size, chunk_size);
86 uint32_t extent_size = origin->vg->extent_size;
87 uint64_t max_size = (uint64_t) MAX_EXTENT_COUNT * extent_size;
89 if (size % extent_size)
90 size += extent_size - size % extent_size;
93 size = max_size; /* Origin is too big for 100% snapshot anyway */
95 return (uint32_t) (size / extent_size);
98 int cow_has_min_chunks(const struct volume_group *vg, uint32_t cow_extents, uint32_t chunk_size)
100 if (((uint64_t)vg->extent_size * cow_extents) >= (SNAPSHOT_MIN_CHUNKS * chunk_size))
103 log_error("Snapshot volume cannot be smaller than " DM_TO_STRING(SNAPSHOT_MIN_CHUNKS)
104 " chunks (%u extents, %s).", (unsigned)
105 (((uint64_t) SNAPSHOT_MIN_CHUNKS * chunk_size +
106 vg->extent_size - 1) / vg->extent_size),
107 display_size(vg->cmd, (uint64_t) SNAPSHOT_MIN_CHUNKS * chunk_size));
112 int lv_is_cow_covering_origin(const struct logical_volume *lv)
114 const struct logical_volume *origin;
116 return (lv_is_cow(lv) &&
117 (origin = origin_from_cow(lv)) &&
118 (lv->size >= _cow_max_size(lv->vg->cmd, origin->size,
119 find_snapshot(lv)->chunk_size)));
122 int lv_is_visible(const struct logical_volume *lv)
124 const struct logical_volume *origin;
126 if (lv_is_historical(lv))
129 if (lv_is_snapshot(lv))
133 if (!(origin = origin_from_cow(lv)))
136 if (lv_is_virtual_origin(origin))
139 if (lv_is_merging_cow(lv))
142 return lv_is_visible(origin);
145 return lv->status & VISIBLE_LV ? 1 : 0;
148 int lv_is_merging_cow(const struct logical_volume *cow)
150 struct lv_segment *snap_seg;
155 snap_seg = find_snapshot(cow);
157 /* checks lv_segment's status to see if snapshot is merging */
158 return (snap_seg && (snap_seg->status & MERGING)) ? 1 : 0;
161 struct lv_segment *find_snapshot(const struct logical_volume *lv)
166 /* Given a cow LV, return its origin */
167 struct logical_volume *origin_from_cow(const struct logical_volume *lv)
170 return lv->snapshot->origin;
172 log_debug(INTERNAL_ERROR "Cannot get origin from snapshot %s.",
177 void init_snapshot_seg(struct lv_segment *seg, struct logical_volume *origin,
178 struct logical_volume *cow, uint32_t chunk_size, int merge)
180 seg->chunk_size = chunk_size;
181 seg->origin = origin;
188 origin->origin_count++;
190 /* FIXME Assumes an invisible origin belongs to a sparse device */
191 if (!lv_is_visible(origin))
192 origin->status |= VIRTUAL_ORIGIN;
194 seg->lv->status |= (SNAPSHOT | VIRTUAL);
196 init_snapshot_merge(seg, origin);
198 dm_list_add(&origin->snapshot_segs, &seg->origin_list);
201 void init_snapshot_merge(struct lv_segment *snap_seg,
202 struct logical_volume *origin)
204 snap_seg->status |= MERGING;
205 origin->snapshot = snap_seg;
206 origin->status |= MERGING;
208 if (seg_is_thin_volume(snap_seg)) {
209 snap_seg->merge_lv = origin;
210 /* Making thin LV invisible with regular log */
211 lv_set_hidden(snap_seg->lv);
216 * Even though lv_is_visible(snap_seg->lv) returns 0,
217 * the snap_seg->lv (name: snapshotX) is _not_ hidden;
218 * this is part of the lvm2 snapshot fiction. Must
219 * clear VISIBLE_LV directly (lv_set_visible can't)
220 * - snap_seg->lv->status is used to control whether 'lv'
221 * (with user provided snapshot LV name) is visible
222 * - this also enables vg_validate() to succeed with
223 * merge metadata (snap_seg->lv is now "internal")
225 snap_seg->lv->status &= ~VISIBLE_LV;
228 void clear_snapshot_merge(struct logical_volume *origin)
230 /* clear merge attributes */
231 if (origin->snapshot->merge_lv)
232 /* Removed thin volume has to be visible */
233 lv_set_visible(origin->snapshot->lv);
235 origin->snapshot->merge_lv = NULL;
236 origin->snapshot->status &= ~MERGING;
237 origin->snapshot = NULL;
238 origin->status &= ~MERGING;
241 static struct lv_segment *_alloc_snapshot_seg(struct logical_volume *lv)
243 struct lv_segment *seg;
244 const struct segment_type *segtype;
246 segtype = get_segtype_from_string(lv->vg->cmd, SEG_TYPE_NAME_SNAPSHOT);
248 log_error("Failed to find snapshot segtype");
252 if (!(seg = alloc_lv_segment(segtype, lv, 0, lv->le_count, 0, 0, 0,
253 NULL, 0, lv->le_count, 0, 0, 0, 0, NULL))) {
254 log_error("Couldn't allocate new snapshot segment.");
258 dm_list_add(&lv->segments, &seg->list);
263 int vg_add_snapshot(struct logical_volume *origin,
264 struct logical_volume *cow, union lvid *lvid,
265 uint32_t extent_count, uint32_t chunk_size)
267 struct logical_volume *snap;
268 struct lv_segment *seg;
271 * Is the cow device already being used ?
273 if (lv_is_cow(cow)) {
274 log_error("'%s' is already in use as a snapshot.", cow->name);
279 log_error("Snapshot and origin LVs must differ.");
283 if (!(snap = lv_create_empty("snapshot%d",
284 lvid, LVM_READ | LVM_WRITE | VISIBLE_LV,
285 ALLOC_INHERIT, origin->vg)))
288 snap->le_count = extent_count;
290 if (!(seg = _alloc_snapshot_seg(snap)))
293 init_snapshot_seg(seg, origin, cow, chunk_size, 0);
298 int vg_remove_snapshot(struct logical_volume *cow)
300 struct logical_volume *origin;
301 int is_origin_active;
306 origin = origin_from_cow(cow);
308 is_origin_active = lv_is_active(origin);
310 if (is_origin_active &&
311 lv_is_virtual_origin(origin)) {
312 if (!sync_local_dev_names(origin->vg->cmd)) {
313 log_error("Failed to sync local devices before deactivating origin LV %s.",
314 display_lvname(origin));
317 if (!deactivate_lv(origin->vg->cmd, origin)) {
318 log_error("Failed to deactivate logical volume \"%s\"",
322 is_origin_active = 0;
325 dm_list_del(&cow->snapshot->origin_list);
326 origin->origin_count--;
328 if (lv_is_merging_origin(origin) &&
329 (find_snapshot(origin) == find_snapshot(cow))) {
330 clear_snapshot_merge(origin);
332 * preload origin IFF "snapshot-merge" target is active
333 * - IMPORTANT: avoids preload if inactivate merge is pending
337 if (!lv_remove(cow->snapshot->lv)) {
338 log_error("Failed to remove internal snapshot LV %s",
339 cow->snapshot->lv->name);
343 cow->snapshot = NULL;
346 /* When origin with all its snapshots is going to be remove
347 * don't bother with individual manipulation with COWs
348 * Note: removal proceeds only when origin is inactive */
349 if (is_origin_active && origin->to_remove) {
350 origin->vg->needs_write_and_commit = 1;
351 log_debug_metadata("Postponing write and commit for remove of snapshot %s.",
352 display_lvname(cow));
356 if (!vg_write(origin->vg))
359 /* Skip call suspend, if device is not active */
360 if (is_origin_active && !suspend_lv(origin->vg->cmd, origin)) {
361 log_error("Failed to refresh %s without snapshot.",
363 vg_revert(origin->vg);
366 if (!vg_commit(origin->vg))
369 if (is_origin_active) {
371 * If the snapshot was active and the COW LV is taken away
372 * the LV lock on cluster has to be grabbed, so use
373 * activate_lv() which resumes suspend cow device.
375 if (!activate_lv(cow->vg->cmd, cow)) {
376 log_error("Failed to activate %s.", cow->name);
380 if (!resume_lv(origin->vg->cmd, origin)) {
381 log_error("Failed to resume %s.", origin->name);
389 /* Check if given LV is usable as snapshot origin LV */
390 int validate_snapshot_origin(const struct logical_volume *origin_lv)
392 const char *err = NULL; /* For error string */
394 if (lv_is_cache(origin_lv) || lv_is_writecache(origin_lv)) {
395 struct logical_volume *lv = seg_lv(first_seg(origin_lv), 0);
396 if (lv_is_raid(lv) && lv_raid_has_integrity(lv)) {
397 err = "raid with integrity";
402 if (lv_is_cow(origin_lv))
404 else if (lv_is_locked(origin_lv))
405 err = "locked volumes";
406 else if (lv_is_pvmove(origin_lv))
407 err = "pvmoved volumes";
408 else if (!lv_is_visible(origin_lv))
409 err = "hidden volumes";
410 else if (lv_is_merging_origin(origin_lv))
411 err = "an origin that has a merging snapshot";
412 else if (lv_is_cache_type(origin_lv) && !lv_is_cache(origin_lv))
413 err = "cache type volumes";
414 else if (lv_is_thin_type(origin_lv) && !lv_is_thin_volume(origin_lv))
415 err = "thin pool type volumes";
416 else if (lv_is_mirror_type(origin_lv)) {
417 if (!lv_is_mirror(origin_lv))
418 err = "mirror subvolumes";
420 log_warn("WARNING: Snapshots of mirrors can deadlock under rare device failures.");
421 log_warn("WARNING: Consider using the raid1 mirror type to avoid this.");
422 log_warn("WARNING: See global/mirror_segtype_default in lvm.conf.");
424 } else if (lv_is_raid_type(origin_lv) && !lv_is_raid(origin_lv)) {
425 err = "raid subvolumes";
430 log_error("Snapshots of %s are not supported.", err);