blkdeactivate.sh: Add PATH environment variable into script
[platform/upstream/device-mapper.git] / lib / metadata / snapshot_manip.c
1 /*
2  * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
3  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4  *
5  * This file is part of LVM2.
6  *
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.
10  *
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
14  */
15
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"
23
24 #define SNAPSHOT_MIN_CHUNKS     3       /* Minimum number of chunks in snapshot */
25
26 int lv_is_origin(const struct logical_volume *lv)
27 {
28         return lv->origin_count ? 1 : 0;
29 }
30
31 int lv_is_cow(const struct logical_volume *lv)
32 {
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;
35 }
36
37 struct logical_volume *find_cow(const struct logical_volume *snap)
38 {
39         return first_seg(snap)->cow;
40 }
41
42 /*
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.
45  */
46 static uint64_t _cow_extra_chunks(struct cmd_context *cmd, uint64_t n_chunks)
47 {
48         const struct segment_type *segtype;
49         unsigned attrs = 0;
50
51         if (activation() &&
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))
56                 return 0;
57
58         return (n_chunks + 63) / 64;
59 }
60
61 static uint64_t _cow_max_size(struct cmd_context *cmd, uint64_t origin_size, uint32_t chunk_size)
62 {
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
68          */
69
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);
72
73         /*
74          * Note: if origin_chunks is divisible by chunks_per_metadata_area, we
75          * need one extra metadata chunk as a terminator.
76          */
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;
79
80         return (n_chunks + _cow_extra_chunks(cmd, n_chunks)) * chunk_size;
81 }
82
83 uint32_t cow_max_extents(const struct logical_volume *origin, uint32_t chunk_size)
84 {
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;
88
89         if (size % extent_size)
90                 size += extent_size - size % extent_size;
91
92         if (size > max_size)
93                 size = max_size; /* Origin is too big for 100% snapshot anyway */
94
95         return (uint32_t) (size / extent_size);
96 }
97
98 int cow_has_min_chunks(const struct volume_group *vg, uint32_t cow_extents, uint32_t chunk_size)
99 {
100         if (((uint64_t)vg->extent_size * cow_extents) >= (SNAPSHOT_MIN_CHUNKS * chunk_size))
101                 return 1;
102
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));
108
109         return 0;
110 }
111
112 int lv_is_cow_covering_origin(const struct logical_volume *lv)
113 {
114         const struct logical_volume *origin;
115
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)));
120 }
121
122 int lv_is_visible(const struct logical_volume *lv)
123 {
124         const struct logical_volume *origin;
125
126         if (lv_is_historical(lv))
127                 return 1;
128
129         if (lv_is_snapshot(lv))
130                 return 0;
131
132         if (lv_is_cow(lv)) {
133                 if (!(origin = origin_from_cow(lv)))
134                         return_0;
135
136                 if (lv_is_virtual_origin(origin))
137                         return 1;
138
139                 if (lv_is_merging_cow(lv))
140                         return 0;
141
142                 return lv_is_visible(origin);
143         }
144
145         return lv->status & VISIBLE_LV ? 1 : 0;
146 }
147
148 int lv_is_merging_cow(const struct logical_volume *cow)
149 {
150         struct lv_segment *snap_seg;
151
152         if (!lv_is_cow(cow))
153                 return 0;
154
155         snap_seg = find_snapshot(cow);
156
157         /* checks lv_segment's status to see if snapshot is merging */
158         return (snap_seg && (snap_seg->status & MERGING)) ? 1 : 0;
159 }
160
161 struct lv_segment *find_snapshot(const struct logical_volume *lv)
162 {
163         return lv->snapshot;
164 }
165
166 /* Given a cow LV, return its origin */
167 struct logical_volume *origin_from_cow(const struct logical_volume *lv)
168 {
169         if (lv->snapshot)
170                 return lv->snapshot->origin;
171
172         log_debug(INTERNAL_ERROR "Cannot get origin from snapshot %s.",
173                   display_lvname(lv));
174         return NULL;
175 }
176
177 void init_snapshot_seg(struct lv_segment *seg, struct logical_volume *origin,
178                        struct logical_volume *cow, uint32_t chunk_size, int merge)
179 {
180         seg->chunk_size = chunk_size;
181         seg->origin = origin;
182         seg->cow = cow;
183
184         lv_set_hidden(cow);
185
186         cow->snapshot = seg;
187
188         origin->origin_count++;
189
190         /* FIXME Assumes an invisible origin belongs to a sparse device */
191         if (!lv_is_visible(origin))
192                 origin->status |= VIRTUAL_ORIGIN;
193
194         seg->lv->status |= (SNAPSHOT | VIRTUAL);
195         if (merge)
196                 init_snapshot_merge(seg, origin);
197
198         dm_list_add(&origin->snapshot_segs, &seg->origin_list);
199 }
200
201 void init_snapshot_merge(struct lv_segment *snap_seg,
202                          struct logical_volume *origin)
203 {
204         snap_seg->status |= MERGING;
205         origin->snapshot = snap_seg;
206         origin->status |= MERGING;
207
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);
212                 return;
213         }
214
215         /*
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")
224          */
225         snap_seg->lv->status &= ~VISIBLE_LV;
226 }
227
228 void clear_snapshot_merge(struct logical_volume *origin)
229 {
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);
234
235         origin->snapshot->merge_lv = NULL;
236         origin->snapshot->status &= ~MERGING;
237         origin->snapshot = NULL;
238         origin->status &= ~MERGING;
239 }
240
241 static struct lv_segment *_alloc_snapshot_seg(struct logical_volume *lv)
242 {
243         struct lv_segment *seg;
244         const struct segment_type *segtype;
245
246         segtype = get_segtype_from_string(lv->vg->cmd, SEG_TYPE_NAME_SNAPSHOT);
247         if (!segtype) {
248                 log_error("Failed to find snapshot segtype");
249                 return NULL;
250         }
251
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.");
255                 return NULL;
256         }
257
258         dm_list_add(&lv->segments, &seg->list);
259
260         return seg;
261 }
262
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)
266 {
267         struct logical_volume *snap;
268         struct lv_segment *seg;
269
270         /*
271          * Is the cow device already being used ?
272          */
273         if (lv_is_cow(cow)) {
274                 log_error("'%s' is already in use as a snapshot.", cow->name);
275                 return 0;
276         }
277
278         if (cow == origin) {
279                 log_error("Snapshot and origin LVs must differ.");
280                 return 0;
281         }
282
283         if (!(snap = lv_create_empty("snapshot%d",
284                                      lvid, LVM_READ | LVM_WRITE | VISIBLE_LV,
285                                      ALLOC_INHERIT, origin->vg)))
286                 return_0;
287
288         snap->le_count = extent_count;
289
290         if (!(seg = _alloc_snapshot_seg(snap)))
291                 return_0;
292
293         init_snapshot_seg(seg, origin, cow, chunk_size, 0);
294
295         return 1;
296 }
297
298 int vg_remove_snapshot(struct logical_volume *cow)
299 {
300         struct logical_volume *origin;
301         int is_origin_active;
302
303         if (!lv_is_cow(cow))
304                 return_0;
305
306         origin = origin_from_cow(cow);
307
308         is_origin_active = lv_is_active(origin);
309
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));
315                         return 0;
316                 }
317                 if (!deactivate_lv(origin->vg->cmd, origin)) {
318                         log_error("Failed to deactivate logical volume \"%s\"",
319                                   origin->name);
320                         return 0;
321                 }
322                 is_origin_active = 0;
323         }
324
325         dm_list_del(&cow->snapshot->origin_list);
326         origin->origin_count--;
327
328         if (lv_is_merging_origin(origin) &&
329             (find_snapshot(origin) == find_snapshot(cow))) {
330                 clear_snapshot_merge(origin);
331                 /*
332                  * preload origin IFF "snapshot-merge" target is active
333                  * - IMPORTANT: avoids preload if inactivate merge is pending
334                  */
335         }
336
337         if (!lv_remove(cow->snapshot->lv)) {
338                 log_error("Failed to remove internal snapshot LV %s",
339                           cow->snapshot->lv->name);
340                 return 0;
341         }
342
343         cow->snapshot = NULL;
344         lv_set_visible(cow);
345
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));
353                 return 1;
354         }
355
356         if (!vg_write(origin->vg))
357                 return_0;
358
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.",
362                           origin->name);
363                 vg_revert(origin->vg);
364                 return 0;
365         }
366         if (!vg_commit(origin->vg))
367                 return_0;
368
369         if (is_origin_active) {
370                 /*
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.
374                  */
375                 if (!activate_lv(cow->vg->cmd, cow)) {
376                         log_error("Failed to activate %s.", cow->name);
377                         return 0;
378                 }
379
380                 if (!resume_lv(origin->vg->cmd, origin)) {
381                         log_error("Failed to resume %s.", origin->name);
382                         return 0;
383                 }
384         }
385
386         return 1;
387 }
388
389 /* Check if given LV is usable as snapshot origin LV */
390 int validate_snapshot_origin(const struct logical_volume *origin_lv)
391 {
392         const char *err = NULL; /* For error string */
393
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";
398                         goto out;
399                 }
400         }
401
402         if (lv_is_cow(origin_lv))
403                 err = "snapshots";
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";
419                 else {
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.");
423                 }
424         } else if (lv_is_raid_type(origin_lv) && !lv_is_raid(origin_lv)) {
425                 err = "raid subvolumes";
426         }
427
428  out:
429         if (err) {
430                 log_error("Snapshots of %s are not supported.", err);
431                 return 0;
432         }
433
434         return 1;
435 }