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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "toolcontext.h"
23 int lv_is_origin(const struct logical_volume *lv)
25 return lv->origin_count ? 1 : 0;
28 int lv_is_cow(const struct logical_volume *lv)
30 return (!lv_is_origin(lv) && lv->snapshot) ? 1 : 0;
33 int lv_is_visible(const struct logical_volume *lv)
35 if (lv->status & SNAPSHOT)
39 if (lv_is_virtual_origin(origin_from_cow(lv)))
42 if (lv_is_merging_cow(lv))
45 return lv_is_visible(origin_from_cow(lv));
48 return lv->status & VISIBLE_LV ? 1 : 0;
51 int lv_is_virtual_origin(const struct logical_volume *lv)
53 return (lv->status & VIRTUAL_ORIGIN) ? 1 : 0;
56 int lv_is_merging_origin(const struct logical_volume *origin)
58 return (origin->status & MERGING) ? 1 : 0;
61 struct lv_segment *find_merging_cow(const struct logical_volume *origin)
63 if (!lv_is_merging_origin(origin))
66 return find_cow(origin);
69 int lv_is_merging_cow(const struct logical_volume *snapshot)
71 /* checks lv_segment's status to see if cow is merging */
72 return (find_cow(snapshot)->status & MERGING) ? 1 : 0;
75 /* Given a cow LV, return the snapshot lv_segment that uses it */
76 struct lv_segment *find_cow(const struct logical_volume *lv)
81 /* Given a cow LV, return its origin */
82 struct logical_volume *origin_from_cow(const struct logical_volume *lv)
84 return lv->snapshot->origin;
87 void init_snapshot_seg(struct lv_segment *seg, struct logical_volume *origin,
88 struct logical_volume *cow, uint32_t chunk_size, int merge)
90 seg->chunk_size = chunk_size;
98 origin->origin_count++;
100 /* FIXME Assumes an invisible origin belongs to a sparse device */
101 if (!lv_is_visible(origin))
102 origin->status |= VIRTUAL_ORIGIN;
104 seg->lv->status |= (SNAPSHOT | VIRTUAL);
106 init_snapshot_merge(seg, origin);
108 dm_list_add(&origin->snapshot_segs, &seg->origin_list);
111 void init_snapshot_merge(struct lv_segment *cow_seg,
112 struct logical_volume *origin)
115 * Even though lv_is_visible(cow_seg->lv) returns 0,
116 * the cow_seg->lv (name: snapshotX) is _not_ hidden;
117 * this is part of the lvm2 snapshot fiction. Must
118 * clear VISIBLE_LV directly (lv_set_visible can't)
119 * - cow_seg->lv->status is used to control whether 'lv'
120 * (with user provided snapshot LV name) is visible
121 * - this also enables vg_validate() to succeed with
122 * merge metadata (cow_seg->lv is now "internal")
124 cow_seg->lv->status &= ~VISIBLE_LV;
125 cow_seg->status |= MERGING;
126 origin->snapshot = cow_seg;
127 origin->status |= MERGING;
130 void clear_snapshot_merge(struct logical_volume *origin)
132 /* clear merge attributes */
133 origin->snapshot->status &= ~MERGING;
134 origin->snapshot = NULL;
135 origin->status &= ~MERGING;
138 int vg_add_snapshot(struct logical_volume *origin,
139 struct logical_volume *cow, union lvid *lvid,
140 uint32_t extent_count, uint32_t chunk_size)
142 struct logical_volume *snap;
143 struct lv_segment *seg;
146 * Is the cow device already being used ?
148 if (lv_is_cow(cow)) {
149 log_error("'%s' is already in use as a snapshot.", cow->name);
154 log_error("Snapshot and origin LVs must differ.");
158 if (!(snap = lv_create_empty("snapshot%d",
159 lvid, LVM_READ | LVM_WRITE | VISIBLE_LV,
160 ALLOC_INHERIT, origin->vg)))
163 snap->le_count = extent_count;
165 if (!(seg = alloc_snapshot_seg(snap, 0, 0)))
168 init_snapshot_seg(seg, origin, cow, chunk_size, 0);
173 int vg_remove_snapshot(struct logical_volume *cow)
175 int preload_origin = 0;
176 struct logical_volume *origin = origin_from_cow(cow);
178 dm_list_del(&cow->snapshot->origin_list);
179 origin->origin_count--;
181 if (find_merging_cow(origin) == find_cow(cow)) {
182 clear_snapshot_merge(origin);
184 * preload origin IFF "snapshot-merge" target is active
185 * - IMPORTANT: avoids preload if onactivate merge is pending
187 if (lv_has_target_type(origin->vg->cmd->mem, origin, NULL,
191 * - allow proper release of -cow
192 * - avoid allocations with other devices suspended
193 * when transitioning from "snapshot-merge" to
194 * "snapshot-origin after a merge completes.
200 if (!lv_remove(cow->snapshot->lv)) {
201 log_error("Failed to remove internal snapshot LV %s",
202 cow->snapshot->lv->name);
206 cow->snapshot = NULL;
209 if (preload_origin) {
210 if (!vg_write(origin->vg))
212 if (!suspend_lv(origin->vg->cmd, origin)) {
213 log_error("Failed to refresh %s without snapshot.",
217 if (!vg_commit(origin->vg))
219 if (!resume_lv(origin->vg->cmd, origin)) {
220 log_error("Failed to resume %s.", origin->name);