metadata.c revision 1.1.1.3 1 1.1 haad /* $NetBSD: metadata.c,v 1.1.1.3 2009/12/02 00:26:39 haad Exp $ */
2 1.1 haad
3 1.1 haad /*
4 1.1 haad * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 1.1.1.3 haad * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
6 1.1 haad *
7 1.1 haad * This file is part of LVM2.
8 1.1 haad *
9 1.1 haad * This copyrighted material is made available to anyone wishing to use,
10 1.1 haad * modify, copy, or redistribute it subject to the terms and conditions
11 1.1 haad * of the GNU Lesser General Public License v.2.1.
12 1.1 haad *
13 1.1 haad * You should have received a copy of the GNU Lesser General Public License
14 1.1 haad * along with this program; if not, write to the Free Software Foundation,
15 1.1 haad * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 1.1 haad */
17 1.1 haad
18 1.1 haad #include "lib.h"
19 1.1 haad #include "device.h"
20 1.1 haad #include "metadata.h"
21 1.1 haad #include "toolcontext.h"
22 1.1 haad #include "lvm-string.h"
23 1.1 haad #include "lvm-file.h"
24 1.1 haad #include "lvmcache.h"
25 1.1 haad #include "memlock.h"
26 1.1 haad #include "str_list.h"
27 1.1 haad #include "pv_alloc.h"
28 1.1.1.3 haad #include "segtype.h"
29 1.1 haad #include "activate.h"
30 1.1 haad #include "display.h"
31 1.1 haad #include "locking.h"
32 1.1 haad #include "archiver.h"
33 1.1 haad #include "defaults.h"
34 1.1.1.3 haad #include "filter-persistent.h"
35 1.1 haad
36 1.1 haad #include <sys/param.h>
37 1.1 haad
38 1.1 haad /*
39 1.1 haad * FIXME: Check for valid handle before dereferencing field or log error?
40 1.1 haad */
41 1.1 haad #define pv_field(handle, field) \
42 1.1 haad (((const struct physical_volume *)(handle))->field)
43 1.1 haad
44 1.1 haad static struct physical_volume *_pv_read(struct cmd_context *cmd,
45 1.1.1.3 haad struct dm_pool *pvmem,
46 1.1 haad const char *pv_name,
47 1.1 haad struct dm_list *mdas,
48 1.1 haad uint64_t *label_sector,
49 1.1.1.3 haad int warnings, int scan_label_only);
50 1.1 haad
51 1.1 haad static struct physical_volume *_find_pv_by_name(struct cmd_context *cmd,
52 1.1 haad const char *pv_name);
53 1.1 haad
54 1.1 haad static struct pv_list *_find_pv_in_vg(const struct volume_group *vg,
55 1.1 haad const char *pv_name);
56 1.1 haad
57 1.1 haad static struct physical_volume *_find_pv_in_vg_by_uuid(const struct volume_group *vg,
58 1.1 haad const struct id *id);
59 1.1 haad
60 1.1.1.3 haad static uint32_t _vg_bad_status_bits(const struct volume_group *vg,
61 1.1.1.3 haad uint32_t status);
62 1.1.1.3 haad
63 1.1.1.3 haad const char _really_init[] =
64 1.1.1.3 haad "Really INITIALIZE physical volume \"%s\" of volume group \"%s\" [y/n]? ";
65 1.1.1.3 haad
66 1.1.1.3 haad unsigned long set_pe_align(struct physical_volume *pv, unsigned long data_alignment)
67 1.1 haad {
68 1.1 haad if (pv->pe_align)
69 1.1 haad goto out;
70 1.1 haad
71 1.1.1.3 haad if (data_alignment)
72 1.1.1.3 haad pv->pe_align = data_alignment;
73 1.1.1.3 haad else
74 1.1.1.3 haad pv->pe_align = MAX(65536UL, lvm_getpagesize()) >> SECTOR_SHIFT;
75 1.1 haad
76 1.1 haad if (!pv->dev)
77 1.1 haad goto out;
78 1.1 haad
79 1.1.1.3 haad /*
80 1.1.1.3 haad * Align to stripe-width of underlying md device if present
81 1.1.1.3 haad */
82 1.1 haad if (find_config_tree_bool(pv->fmt->cmd, "devices/md_chunk_alignment",
83 1.1 haad DEFAULT_MD_CHUNK_ALIGNMENT))
84 1.1 haad pv->pe_align = MAX(pv->pe_align,
85 1.1.1.3 haad dev_md_stripe_width(pv->fmt->cmd->sysfs_dir,
86 1.1.1.3 haad pv->dev));
87 1.1.1.3 haad
88 1.1.1.3 haad /*
89 1.1.1.3 haad * Align to topology's minimum_io_size or optimal_io_size if present
90 1.1.1.3 haad * - minimum_io_size - the smallest request the device can perform
91 1.1.1.3 haad * w/o incurring a read-modify-write penalty (e.g. MD's chunk size)
92 1.1.1.3 haad * - optimal_io_size - the device's preferred unit of receiving I/O
93 1.1.1.3 haad * (e.g. MD's stripe width)
94 1.1.1.3 haad */
95 1.1.1.3 haad if (find_config_tree_bool(pv->fmt->cmd,
96 1.1.1.3 haad "devices/data_alignment_detection",
97 1.1.1.3 haad DEFAULT_DATA_ALIGNMENT_DETECTION)) {
98 1.1.1.3 haad pv->pe_align = MAX(pv->pe_align,
99 1.1.1.3 haad dev_minimum_io_size(pv->fmt->cmd->sysfs_dir,
100 1.1.1.3 haad pv->dev));
101 1.1.1.3 haad
102 1.1.1.3 haad pv->pe_align = MAX(pv->pe_align,
103 1.1.1.3 haad dev_optimal_io_size(pv->fmt->cmd->sysfs_dir,
104 1.1.1.3 haad pv->dev));
105 1.1.1.3 haad }
106 1.1 haad
107 1.1 haad log_very_verbose("%s: Setting PE alignment to %lu sectors.",
108 1.1 haad dev_name(pv->dev), pv->pe_align);
109 1.1 haad
110 1.1 haad out:
111 1.1 haad return pv->pe_align;
112 1.1 haad }
113 1.1 haad
114 1.1.1.3 haad unsigned long set_pe_align_offset(struct physical_volume *pv,
115 1.1.1.3 haad unsigned long data_alignment_offset)
116 1.1.1.3 haad {
117 1.1.1.3 haad if (pv->pe_align_offset)
118 1.1.1.3 haad goto out;
119 1.1.1.3 haad
120 1.1.1.3 haad if (data_alignment_offset)
121 1.1.1.3 haad pv->pe_align_offset = data_alignment_offset;
122 1.1.1.3 haad
123 1.1.1.3 haad if (!pv->dev)
124 1.1.1.3 haad goto out;
125 1.1.1.3 haad
126 1.1.1.3 haad if (find_config_tree_bool(pv->fmt->cmd,
127 1.1.1.3 haad "devices/data_alignment_offset_detection",
128 1.1.1.3 haad DEFAULT_DATA_ALIGNMENT_OFFSET_DETECTION))
129 1.1.1.3 haad pv->pe_align_offset =
130 1.1.1.3 haad MAX(pv->pe_align_offset,
131 1.1.1.3 haad dev_alignment_offset(pv->fmt->cmd->sysfs_dir,
132 1.1.1.3 haad pv->dev));
133 1.1.1.3 haad
134 1.1.1.3 haad log_very_verbose("%s: Setting PE alignment offset to %lu sectors.",
135 1.1.1.3 haad dev_name(pv->dev), pv->pe_align_offset);
136 1.1.1.3 haad
137 1.1.1.3 haad out:
138 1.1.1.3 haad return pv->pe_align_offset;
139 1.1.1.3 haad }
140 1.1.1.3 haad
141 1.1 haad /**
142 1.1 haad * add_pv_to_vg - Add a physical volume to a volume group
143 1.1 haad * @vg - volume group to add to
144 1.1 haad * @pv_name - name of the pv (to be removed)
145 1.1 haad * @pv - physical volume to add to volume group
146 1.1 haad *
147 1.1 haad * Returns:
148 1.1 haad * 0 - failure
149 1.1 haad * 1 - success
150 1.1 haad * FIXME: remove pv_name - obtain safely from pv
151 1.1 haad */
152 1.1 haad int add_pv_to_vg(struct volume_group *vg, const char *pv_name,
153 1.1 haad struct physical_volume *pv)
154 1.1 haad {
155 1.1 haad struct pv_list *pvl;
156 1.1 haad struct format_instance *fid = vg->fid;
157 1.1.1.3 haad struct dm_pool *mem = vg->vgmem;
158 1.1 haad
159 1.1 haad log_verbose("Adding physical volume '%s' to volume group '%s'",
160 1.1 haad pv_name, vg->name);
161 1.1 haad
162 1.1 haad if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
163 1.1 haad log_error("pv_list allocation for '%s' failed", pv_name);
164 1.1 haad return 0;
165 1.1 haad }
166 1.1 haad
167 1.1 haad if (!is_orphan_vg(pv->vg_name)) {
168 1.1 haad log_error("Physical volume '%s' is already in volume group "
169 1.1 haad "'%s'", pv_name, pv->vg_name);
170 1.1 haad return 0;
171 1.1 haad }
172 1.1 haad
173 1.1 haad if (pv->fmt != fid->fmt) {
174 1.1 haad log_error("Physical volume %s is of different format type (%s)",
175 1.1 haad pv_name, pv->fmt->name);
176 1.1 haad return 0;
177 1.1 haad }
178 1.1 haad
179 1.1 haad /* Ensure PV doesn't depend on another PV already in the VG */
180 1.1 haad if (pv_uses_vg(pv, vg)) {
181 1.1 haad log_error("Physical volume %s might be constructed from same "
182 1.1 haad "volume group %s", pv_name, vg->name);
183 1.1 haad return 0;
184 1.1 haad }
185 1.1 haad
186 1.1 haad if (!(pv->vg_name = dm_pool_strdup(mem, vg->name))) {
187 1.1 haad log_error("vg->name allocation failed for '%s'", pv_name);
188 1.1 haad return 0;
189 1.1 haad }
190 1.1 haad
191 1.1 haad memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
192 1.1 haad
193 1.1 haad /* Units of 512-byte sectors */
194 1.1 haad pv->pe_size = vg->extent_size;
195 1.1 haad
196 1.1 haad /*
197 1.1 haad * pe_count must always be calculated by pv_setup
198 1.1 haad */
199 1.1 haad pv->pe_alloc_count = 0;
200 1.1 haad
201 1.1 haad if (!fid->fmt->ops->pv_setup(fid->fmt, UINT64_C(0), 0,
202 1.1.1.3 haad vg->extent_size, 0, 0, 0UL, UINT64_C(0),
203 1.1 haad &fid->metadata_areas, pv, vg)) {
204 1.1 haad log_error("Format-specific setup of physical volume '%s' "
205 1.1 haad "failed.", pv_name);
206 1.1 haad return 0;
207 1.1 haad }
208 1.1 haad
209 1.1 haad if (_find_pv_in_vg(vg, pv_name)) {
210 1.1 haad log_error("Physical volume '%s' listed more than once.",
211 1.1 haad pv_name);
212 1.1 haad return 0;
213 1.1 haad }
214 1.1 haad
215 1.1 haad if (vg->pv_count && (vg->pv_count == vg->max_pv)) {
216 1.1 haad log_error("No space for '%s' - volume group '%s' "
217 1.1 haad "holds max %d physical volume(s).", pv_name,
218 1.1 haad vg->name, vg->max_pv);
219 1.1 haad return 0;
220 1.1 haad }
221 1.1 haad
222 1.1 haad if (!alloc_pv_segment_whole_pv(mem, pv))
223 1.1 haad return_0;
224 1.1 haad
225 1.1 haad pvl->pv = pv;
226 1.1 haad dm_list_add(&vg->pvs, &pvl->list);
227 1.1 haad
228 1.1 haad if ((uint64_t) vg->extent_count + pv->pe_count > UINT32_MAX) {
229 1.1 haad log_error("Unable to add %s to %s: new extent count (%"
230 1.1 haad PRIu64 ") exceeds limit (%" PRIu32 ").",
231 1.1 haad pv_name, vg->name,
232 1.1 haad (uint64_t) vg->extent_count + pv->pe_count,
233 1.1 haad UINT32_MAX);
234 1.1 haad return 0;
235 1.1 haad }
236 1.1 haad
237 1.1 haad vg->pv_count++;
238 1.1 haad vg->extent_count += pv->pe_count;
239 1.1 haad vg->free_count += pv->pe_count;
240 1.1 haad
241 1.1 haad return 1;
242 1.1 haad }
243 1.1 haad
244 1.1.1.3 haad static int _copy_pv(struct dm_pool *pvmem,
245 1.1.1.3 haad struct physical_volume *pv_to,
246 1.1 haad struct physical_volume *pv_from)
247 1.1 haad {
248 1.1 haad memcpy(pv_to, pv_from, sizeof(*pv_to));
249 1.1 haad
250 1.1.1.3 haad if (!(pv_to->vg_name = dm_pool_strdup(pvmem, pv_from->vg_name)))
251 1.1.1.3 haad return_0;
252 1.1 haad
253 1.1.1.3 haad if (!str_list_dup(pvmem, &pv_to->tags, &pv_from->tags))
254 1.1.1.3 haad return_0;
255 1.1.1.3 haad
256 1.1.1.3 haad if (!peg_dup(pvmem, &pv_to->segments, &pv_from->segments))
257 1.1 haad return_0;
258 1.1 haad
259 1.1 haad return 1;
260 1.1 haad }
261 1.1 haad
262 1.1.1.3 haad static struct pv_list *_copy_pvl(struct dm_pool *pvmem, struct pv_list *pvl_from)
263 1.1.1.3 haad {
264 1.1.1.3 haad struct pv_list *pvl_to = NULL;
265 1.1.1.3 haad
266 1.1.1.3 haad if (!(pvl_to = dm_pool_zalloc(pvmem, sizeof(*pvl_to))))
267 1.1.1.3 haad return_NULL;
268 1.1.1.3 haad
269 1.1.1.3 haad if (!(pvl_to->pv = dm_pool_alloc(pvmem, sizeof(*pvl_to->pv))))
270 1.1.1.3 haad goto_bad;
271 1.1.1.3 haad
272 1.1.1.3 haad if(!_copy_pv(pvmem, pvl_to->pv, pvl_from->pv))
273 1.1.1.3 haad goto_bad;
274 1.1.1.3 haad
275 1.1.1.3 haad return pvl_to;
276 1.1.1.3 haad bad:
277 1.1.1.3 haad dm_pool_free(pvmem, pvl_to);
278 1.1.1.3 haad return NULL;
279 1.1.1.3 haad }
280 1.1.1.3 haad
281 1.1 haad int get_pv_from_vg_by_id(const struct format_type *fmt, const char *vg_name,
282 1.1 haad const char *vgid, const char *pvid,
283 1.1 haad struct physical_volume *pv)
284 1.1 haad {
285 1.1 haad struct volume_group *vg;
286 1.1 haad struct pv_list *pvl;
287 1.1.1.3 haad int r = 0, consistent = 0;
288 1.1 haad
289 1.1.1.3 haad if (!(vg = vg_read_internal(fmt->cmd, vg_name, vgid, &consistent))) {
290 1.1.1.3 haad log_error("get_pv_from_vg_by_id: vg_read_internal failed to read VG %s",
291 1.1 haad vg_name);
292 1.1 haad return 0;
293 1.1 haad }
294 1.1 haad
295 1.1 haad if (!consistent)
296 1.1 haad log_warn("WARNING: Volume group %s is not consistent",
297 1.1 haad vg_name);
298 1.1 haad
299 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
300 1.1 haad if (id_equal(&pvl->pv->id, (const struct id *) pvid)) {
301 1.1.1.3 haad if (!_copy_pv(fmt->cmd->mem, pv, pvl->pv)) {
302 1.1.1.3 haad log_error("internal PV duplication failed");
303 1.1.1.3 haad r = 0;
304 1.1.1.3 haad goto out;
305 1.1.1.3 haad }
306 1.1.1.3 haad r = 1;
307 1.1.1.3 haad goto out;
308 1.1 haad }
309 1.1 haad }
310 1.1.1.3 haad out:
311 1.1.1.3 haad vg_release(vg);
312 1.1.1.3 haad return r;
313 1.1.1.3 haad }
314 1.1 haad
315 1.1.1.3 haad int move_pv(struct volume_group *vg_from, struct volume_group *vg_to,
316 1.1.1.3 haad const char *pv_name)
317 1.1.1.3 haad {
318 1.1.1.3 haad struct physical_volume *pv;
319 1.1.1.3 haad struct pv_list *pvl;
320 1.1.1.3 haad
321 1.1.1.3 haad /* FIXME: handle tags */
322 1.1.1.3 haad if (!(pvl = find_pv_in_vg(vg_from, pv_name))) {
323 1.1.1.3 haad log_error("Physical volume %s not in volume group %s",
324 1.1.1.3 haad pv_name, vg_from->name);
325 1.1.1.3 haad return 0;
326 1.1.1.3 haad }
327 1.1.1.3 haad
328 1.1.1.3 haad if (_vg_bad_status_bits(vg_from, RESIZEABLE_VG) ||
329 1.1.1.3 haad _vg_bad_status_bits(vg_to, RESIZEABLE_VG))
330 1.1.1.3 haad return 0;
331 1.1.1.3 haad
332 1.1.1.3 haad dm_list_move(&vg_to->pvs, &pvl->list);
333 1.1.1.3 haad
334 1.1.1.3 haad vg_from->pv_count--;
335 1.1.1.3 haad vg_to->pv_count++;
336 1.1.1.3 haad
337 1.1.1.3 haad pv = pvl->pv;
338 1.1.1.3 haad
339 1.1.1.3 haad vg_from->extent_count -= pv_pe_count(pv);
340 1.1.1.3 haad vg_to->extent_count += pv_pe_count(pv);
341 1.1.1.3 haad
342 1.1.1.3 haad vg_from->free_count -= pv_pe_count(pv) - pv_pe_alloc_count(pv);
343 1.1.1.3 haad vg_to->free_count += pv_pe_count(pv) - pv_pe_alloc_count(pv);
344 1.1.1.3 haad
345 1.1.1.3 haad return 1;
346 1.1.1.3 haad }
347 1.1.1.3 haad
348 1.1.1.3 haad int move_pvs_used_by_lv(struct volume_group *vg_from,
349 1.1.1.3 haad struct volume_group *vg_to,
350 1.1.1.3 haad const char *lv_name)
351 1.1.1.3 haad {
352 1.1.1.3 haad struct lv_segment *lvseg;
353 1.1.1.3 haad unsigned s;
354 1.1.1.3 haad struct lv_list *lvl;
355 1.1.1.3 haad struct logical_volume *lv;
356 1.1.1.3 haad
357 1.1.1.3 haad /* FIXME: handle tags */
358 1.1.1.3 haad if (!(lvl = find_lv_in_vg(vg_from, lv_name))) {
359 1.1.1.3 haad log_error("Logical volume %s not in volume group %s",
360 1.1.1.3 haad lv_name, vg_from->name);
361 1.1.1.3 haad return 0;
362 1.1.1.3 haad }
363 1.1.1.3 haad
364 1.1.1.3 haad if (_vg_bad_status_bits(vg_from, RESIZEABLE_VG) ||
365 1.1.1.3 haad _vg_bad_status_bits(vg_to, RESIZEABLE_VG))
366 1.1.1.3 haad return 0;
367 1.1.1.3 haad
368 1.1.1.3 haad dm_list_iterate_items(lvseg, &lvl->lv->segments) {
369 1.1.1.3 haad if (lvseg->log_lv)
370 1.1.1.3 haad if (!move_pvs_used_by_lv(vg_from, vg_to,
371 1.1.1.3 haad lvseg->log_lv->name))
372 1.1.1.3 haad return_0;
373 1.1.1.3 haad for (s = 0; s < lvseg->area_count; s++) {
374 1.1.1.3 haad if (seg_type(lvseg, s) == AREA_PV) {
375 1.1.1.3 haad if (!move_pv(vg_from, vg_to,
376 1.1.1.3 haad pv_dev_name(seg_pv(lvseg, s))))
377 1.1.1.3 haad return_0;
378 1.1.1.3 haad } else if (seg_type(lvseg, s) == AREA_LV) {
379 1.1.1.3 haad lv = seg_lv(lvseg, s);
380 1.1.1.3 haad if (!move_pvs_used_by_lv(vg_from, vg_to,
381 1.1.1.3 haad lv->name))
382 1.1.1.3 haad return_0;
383 1.1.1.3 haad }
384 1.1.1.3 haad }
385 1.1.1.3 haad }
386 1.1.1.3 haad return 1;
387 1.1 haad }
388 1.1 haad
389 1.1 haad static int validate_new_vg_name(struct cmd_context *cmd, const char *vg_name)
390 1.1 haad {
391 1.1 haad char vg_path[PATH_MAX];
392 1.1 haad
393 1.1 haad if (!validate_name(vg_name))
394 1.1 haad return_0;
395 1.1 haad
396 1.1 haad snprintf(vg_path, PATH_MAX, "%s%s", cmd->dev_dir, vg_name);
397 1.1 haad if (path_exists(vg_path)) {
398 1.1 haad log_error("%s: already exists in filesystem", vg_path);
399 1.1 haad return 0;
400 1.1 haad }
401 1.1 haad
402 1.1 haad return 1;
403 1.1 haad }
404 1.1 haad
405 1.1 haad int validate_vg_rename_params(struct cmd_context *cmd,
406 1.1 haad const char *vg_name_old,
407 1.1 haad const char *vg_name_new)
408 1.1 haad {
409 1.1 haad unsigned length;
410 1.1 haad char *dev_dir;
411 1.1 haad
412 1.1 haad dev_dir = cmd->dev_dir;
413 1.1 haad length = strlen(dev_dir);
414 1.1 haad
415 1.1 haad /* Check sanity of new name */
416 1.1 haad if (strlen(vg_name_new) > NAME_LEN - length - 2) {
417 1.1 haad log_error("New volume group path exceeds maximum length "
418 1.1 haad "of %d!", NAME_LEN - length - 2);
419 1.1 haad return 0;
420 1.1 haad }
421 1.1 haad
422 1.1 haad if (!validate_new_vg_name(cmd, vg_name_new)) {
423 1.1 haad log_error("New volume group name \"%s\" is invalid",
424 1.1 haad vg_name_new);
425 1.1 haad return 0;
426 1.1 haad }
427 1.1 haad
428 1.1 haad if (!strcmp(vg_name_old, vg_name_new)) {
429 1.1 haad log_error("Old and new volume group names must differ");
430 1.1 haad return 0;
431 1.1 haad }
432 1.1 haad
433 1.1 haad return 1;
434 1.1 haad }
435 1.1 haad
436 1.1 haad int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
437 1.1 haad const char *new_name)
438 1.1 haad {
439 1.1.1.3 haad struct dm_pool *mem = vg->vgmem;
440 1.1 haad struct pv_list *pvl;
441 1.1 haad
442 1.1 haad if (!(vg->name = dm_pool_strdup(mem, new_name))) {
443 1.1 haad log_error("vg->name allocation failed for '%s'", new_name);
444 1.1 haad return 0;
445 1.1 haad }
446 1.1 haad
447 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
448 1.1 haad if (!(pvl->pv->vg_name = dm_pool_strdup(mem, new_name))) {
449 1.1 haad log_error("pv->vg_name allocation failed for '%s'",
450 1.1 haad pv_dev_name(pvl->pv));
451 1.1 haad return 0;
452 1.1 haad }
453 1.1 haad }
454 1.1 haad
455 1.1 haad return 1;
456 1.1 haad }
457 1.1 haad
458 1.1.1.3 haad int remove_lvs_in_vg(struct cmd_context *cmd,
459 1.1.1.3 haad struct volume_group *vg,
460 1.1.1.3 haad force_t force)
461 1.1 haad {
462 1.1 haad struct dm_list *lst;
463 1.1 haad struct lv_list *lvl;
464 1.1 haad
465 1.1 haad while ((lst = dm_list_first(&vg->lvs))) {
466 1.1 haad lvl = dm_list_item(lst, struct lv_list);
467 1.1 haad if (!lv_remove_with_dependencies(cmd, lvl->lv, force))
468 1.1 haad return 0;
469 1.1 haad }
470 1.1 haad
471 1.1 haad return 1;
472 1.1 haad }
473 1.1 haad
474 1.1.1.3 haad int vg_remove_check(struct volume_group *vg)
475 1.1 haad {
476 1.1.1.2 haad unsigned lv_count;
477 1.1.1.3 haad struct pv_list *pvl, *tpvl;
478 1.1 haad
479 1.1.1.3 haad if (vg_read_error(vg) || vg_missing_pv_count(vg)) {
480 1.1 haad log_error("Volume group \"%s\" not found, is inconsistent "
481 1.1.1.3 haad "or has PVs missing.", vg ? vg->name : "");
482 1.1 haad log_error("Consider vgreduce --removemissing if metadata "
483 1.1 haad "is inconsistent.");
484 1.1 haad return 0;
485 1.1 haad }
486 1.1 haad
487 1.1 haad if (!vg_check_status(vg, EXPORTED_VG))
488 1.1 haad return 0;
489 1.1 haad
490 1.1.1.3 haad lv_count = vg_visible_lvs(vg);
491 1.1.1.2 haad
492 1.1.1.2 haad if (lv_count) {
493 1.1.1.2 haad log_error("Volume group \"%s\" still contains %u "
494 1.1.1.3 haad "logical volume(s)", vg->name, lv_count);
495 1.1 haad return 0;
496 1.1 haad }
497 1.1 haad
498 1.1 haad if (!archive(vg))
499 1.1 haad return 0;
500 1.1 haad
501 1.1.1.3 haad dm_list_iterate_items_safe(pvl, tpvl, &vg->pvs) {
502 1.1.1.3 haad dm_list_del(&pvl->list);
503 1.1.1.3 haad dm_list_add(&vg->removed_pvs, &pvl->list);
504 1.1.1.3 haad }
505 1.1.1.3 haad return 1;
506 1.1.1.3 haad }
507 1.1.1.3 haad
508 1.1.1.3 haad int vg_remove(struct volume_group *vg)
509 1.1.1.3 haad {
510 1.1.1.3 haad struct physical_volume *pv;
511 1.1.1.3 haad struct pv_list *pvl;
512 1.1.1.3 haad int ret = 1;
513 1.1.1.3 haad
514 1.1.1.3 haad if (!lock_vol(vg->cmd, VG_ORPHANS, LCK_VG_WRITE)) {
515 1.1.1.3 haad log_error("Can't get lock for orphan PVs");
516 1.1.1.3 haad return 0;
517 1.1.1.3 haad }
518 1.1.1.3 haad
519 1.1.1.3 haad if (!vg_remove_mdas(vg)) {
520 1.1.1.3 haad log_error("vg_remove_mdas %s failed", vg->name);
521 1.1.1.3 haad unlock_vg(vg->cmd, VG_ORPHANS);
522 1.1 haad return 0;
523 1.1 haad }
524 1.1 haad
525 1.1 haad /* init physical volumes */
526 1.1.1.3 haad dm_list_iterate_items(pvl, &vg->removed_pvs) {
527 1.1 haad pv = pvl->pv;
528 1.1 haad log_verbose("Removing physical volume \"%s\" from "
529 1.1.1.3 haad "volume group \"%s\"", pv_dev_name(pv), vg->name);
530 1.1 haad pv->vg_name = vg->fid->fmt->orphan_vg_name;
531 1.1 haad pv->status = ALLOCATABLE_PV;
532 1.1 haad
533 1.1 haad if (!dev_get_size(pv_dev(pv), &pv->size)) {
534 1.1 haad log_error("%s: Couldn't get size.", pv_dev_name(pv));
535 1.1 haad ret = 0;
536 1.1 haad continue;
537 1.1 haad }
538 1.1 haad
539 1.1 haad /* FIXME Write to same sector label was read from */
540 1.1.1.3 haad if (!pv_write(vg->cmd, pv, NULL, INT64_C(-1))) {
541 1.1 haad log_error("Failed to remove physical volume \"%s\""
542 1.1 haad " from volume group \"%s\"",
543 1.1.1.3 haad pv_dev_name(pv), vg->name);
544 1.1 haad ret = 0;
545 1.1 haad }
546 1.1 haad }
547 1.1 haad
548 1.1.1.3 haad backup_remove(vg->cmd, vg->name);
549 1.1 haad
550 1.1 haad if (ret)
551 1.1.1.3 haad log_print("Volume group \"%s\" successfully removed", vg->name);
552 1.1 haad else
553 1.1.1.3 haad log_error("Volume group \"%s\" not properly removed", vg->name);
554 1.1 haad
555 1.1.1.3 haad unlock_vg(vg->cmd, VG_ORPHANS);
556 1.1 haad return ret;
557 1.1 haad }
558 1.1 haad
559 1.1.1.3 haad /*
560 1.1.1.3 haad * Extend a VG by a single PV / device path
561 1.1.1.3 haad *
562 1.1.1.3 haad * Parameters:
563 1.1.1.3 haad * - vg: handle of volume group to extend by 'pv_name'
564 1.1.1.3 haad * - pv_name: device path of PV to add to VG
565 1.1.1.3 haad * - pp: parameters to pass to implicit pvcreate; if NULL, do not pvcreate
566 1.1.1.3 haad *
567 1.1.1.3 haad */
568 1.1.1.3 haad static int vg_extend_single_pv(struct volume_group *vg, char *pv_name,
569 1.1.1.3 haad struct pvcreate_params *pp)
570 1.1 haad {
571 1.1 haad struct physical_volume *pv;
572 1.1 haad
573 1.1.1.3 haad pv = pv_by_path(vg->fid->fmt->cmd, pv_name);
574 1.1.1.3 haad if (!pv && !pp) {
575 1.1.1.3 haad log_error("%s not identified as an existing "
576 1.1.1.3 haad "physical volume", pv_name);
577 1.1.1.3 haad return 0;
578 1.1.1.3 haad } else if (!pv && pp) {
579 1.1.1.3 haad pv = pvcreate_single(vg->cmd, pv_name, pp);
580 1.1.1.3 haad if (!pv)
581 1.1.1.3 haad return 0;
582 1.1.1.3 haad }
583 1.1.1.3 haad if (!add_pv_to_vg(vg, pv_name, pv))
584 1.1.1.3 haad return 0;
585 1.1.1.3 haad return 1;
586 1.1.1.3 haad }
587 1.1.1.3 haad
588 1.1.1.3 haad /*
589 1.1.1.3 haad * Extend a VG by a single PV / device path
590 1.1.1.3 haad *
591 1.1.1.3 haad * Parameters:
592 1.1.1.3 haad * - vg: handle of volume group to extend by 'pv_name'
593 1.1.1.3 haad * - pv_count: count of device paths of PVs
594 1.1.1.3 haad * - pv_names: device paths of PVs to add to VG
595 1.1.1.3 haad * - pp: parameters to pass to implicit pvcreate; if NULL, do not pvcreate
596 1.1.1.3 haad *
597 1.1.1.3 haad */
598 1.1.1.3 haad int vg_extend(struct volume_group *vg, int pv_count, char **pv_names,
599 1.1.1.3 haad struct pvcreate_params *pp)
600 1.1.1.3 haad {
601 1.1.1.3 haad int i;
602 1.1.1.3 haad
603 1.1.1.3 haad if (_vg_bad_status_bits(vg, RESIZEABLE_VG))
604 1.1.1.3 haad return 0;
605 1.1.1.3 haad
606 1.1 haad /* attach each pv */
607 1.1 haad for (i = 0; i < pv_count; i++) {
608 1.1.1.3 haad if (!vg_extend_single_pv(vg, pv_names[i], pp))
609 1.1 haad goto bad;
610 1.1 haad }
611 1.1 haad
612 1.1 haad /* FIXME Decide whether to initialise and add new mdahs to format instance */
613 1.1 haad
614 1.1 haad return 1;
615 1.1.1.3 haad
616 1.1 haad bad:
617 1.1 haad log_error("Unable to add physical volume '%s' to "
618 1.1 haad "volume group '%s'.", pv_names[i], vg->name);
619 1.1 haad return 0;
620 1.1 haad }
621 1.1 haad
622 1.1.1.3 haad /* FIXME: use this inside vgreduce_single? */
623 1.1.1.3 haad int vg_reduce(struct volume_group *vg, char *pv_name)
624 1.1.1.3 haad {
625 1.1.1.3 haad struct physical_volume *pv;
626 1.1.1.3 haad struct pv_list *pvl;
627 1.1.1.3 haad
628 1.1.1.3 haad if (_vg_bad_status_bits(vg, RESIZEABLE_VG))
629 1.1.1.3 haad return 0;
630 1.1.1.3 haad
631 1.1.1.3 haad if (!archive(vg))
632 1.1.1.3 haad goto bad;
633 1.1.1.3 haad
634 1.1.1.3 haad /* remove each pv */
635 1.1.1.3 haad if (!(pvl = find_pv_in_vg(vg, pv_name))) {
636 1.1.1.3 haad log_error("Physical volume %s not in volume group %s.",
637 1.1.1.3 haad pv_name, vg->name);
638 1.1.1.3 haad goto bad;
639 1.1.1.3 haad }
640 1.1.1.3 haad
641 1.1.1.3 haad pv = pvl->pv;
642 1.1.1.3 haad
643 1.1.1.3 haad if (pv_pe_alloc_count(pv)) {
644 1.1.1.3 haad log_error("Physical volume %s still in use.",
645 1.1.1.3 haad pv_name);
646 1.1.1.3 haad goto bad;
647 1.1.1.3 haad }
648 1.1.1.3 haad
649 1.1.1.3 haad if (!dev_get_size(pv_dev(pv), &pv->size)) {
650 1.1.1.3 haad log_error("%s: Couldn't get size.", pv_name);
651 1.1.1.3 haad goto bad;
652 1.1.1.3 haad }
653 1.1.1.3 haad
654 1.1.1.3 haad vg->pv_count--;
655 1.1.1.3 haad vg->free_count -= pv_pe_count(pv) - pv_pe_alloc_count(pv);
656 1.1.1.3 haad vg->extent_count -= pv_pe_count(pv);
657 1.1.1.3 haad
658 1.1.1.3 haad /* add pv to the remove_pvs list */
659 1.1.1.3 haad dm_list_del(&pvl->list);
660 1.1.1.3 haad dm_list_add(&vg->removed_pvs, &pvl->list);
661 1.1.1.3 haad
662 1.1.1.3 haad return 1;
663 1.1.1.3 haad
664 1.1.1.3 haad bad:
665 1.1.1.3 haad log_error("Unable to remove physical volume '%s' from "
666 1.1.1.3 haad "volume group '%s'.", pv_name, vg->name);
667 1.1.1.3 haad return 0;
668 1.1.1.3 haad }
669 1.1.1.3 haad
670 1.1 haad const char *strip_dir(const char *vg_name, const char *dev_dir)
671 1.1 haad {
672 1.1 haad size_t len = strlen(dev_dir);
673 1.1 haad if (!strncmp(vg_name, dev_dir, len))
674 1.1 haad vg_name += len;
675 1.1 haad
676 1.1 haad return vg_name;
677 1.1 haad }
678 1.1 haad
679 1.1 haad /*
680 1.1 haad * Validate parameters to vg_create() before calling.
681 1.1 haad * FIXME: Move inside vg_create library function.
682 1.1 haad * FIXME: Change vgcreate_params struct to individual gets/sets
683 1.1 haad */
684 1.1.1.3 haad int vgcreate_params_validate(struct cmd_context *cmd,
685 1.1.1.3 haad struct vgcreate_params *vp)
686 1.1 haad {
687 1.1 haad if (!validate_new_vg_name(cmd, vp->vg_name)) {
688 1.1 haad log_error("New volume group name \"%s\" is invalid",
689 1.1 haad vp->vg_name);
690 1.1 haad return 1;
691 1.1 haad }
692 1.1 haad
693 1.1 haad if (vp->alloc == ALLOC_INHERIT) {
694 1.1 haad log_error("Volume Group allocation policy cannot inherit "
695 1.1 haad "from anything");
696 1.1 haad return 1;
697 1.1 haad }
698 1.1 haad
699 1.1 haad if (!vp->extent_size) {
700 1.1 haad log_error("Physical extent size may not be zero");
701 1.1 haad return 1;
702 1.1 haad }
703 1.1 haad
704 1.1 haad if (!(cmd->fmt->features & FMT_UNLIMITED_VOLS)) {
705 1.1 haad if (!vp->max_lv)
706 1.1 haad vp->max_lv = 255;
707 1.1 haad if (!vp->max_pv)
708 1.1 haad vp->max_pv = 255;
709 1.1 haad if (vp->max_lv > 255 || vp->max_pv > 255) {
710 1.1 haad log_error("Number of volumes may not exceed 255");
711 1.1 haad return 1;
712 1.1 haad }
713 1.1 haad }
714 1.1 haad
715 1.1 haad return 0;
716 1.1 haad }
717 1.1 haad
718 1.1.1.3 haad /*
719 1.1.1.3 haad * Create a (struct volume_group) volume group handle from a struct volume_group pointer and a
720 1.1.1.3 haad * possible failure code or zero for success.
721 1.1.1.3 haad */
722 1.1.1.3 haad static struct volume_group *_vg_make_handle(struct cmd_context *cmd,
723 1.1.1.3 haad struct volume_group *vg,
724 1.1.1.3 haad uint32_t failure)
725 1.1.1.3 haad {
726 1.1.1.3 haad struct dm_pool *vgmem;
727 1.1.1.3 haad
728 1.1.1.3 haad if (!vg) {
729 1.1.1.3 haad if (!(vgmem = dm_pool_create("lvm2 vg_handle", VG_MEMPOOL_CHUNK)) ||
730 1.1.1.3 haad !(vg = dm_pool_zalloc(vgmem, sizeof(*vg)))) {
731 1.1.1.3 haad log_error("Error allocating vg handle.");
732 1.1.1.3 haad if (vgmem)
733 1.1.1.3 haad dm_pool_destroy(vgmem);
734 1.1.1.3 haad return_NULL;
735 1.1.1.3 haad }
736 1.1.1.3 haad vg->vgmem = vgmem;
737 1.1.1.3 haad }
738 1.1.1.3 haad
739 1.1.1.3 haad vg->read_status = failure;
740 1.1.1.3 haad
741 1.1.1.3 haad return (struct volume_group *)vg;
742 1.1.1.3 haad }
743 1.1.1.3 haad
744 1.1.1.3 haad int lv_has_unknown_segments(const struct logical_volume *lv)
745 1.1.1.3 haad {
746 1.1.1.3 haad struct lv_segment *seg;
747 1.1.1.3 haad /* foreach segment */
748 1.1.1.3 haad dm_list_iterate_items(seg, &lv->segments)
749 1.1.1.3 haad if (seg_unknown(seg))
750 1.1.1.3 haad return 1;
751 1.1.1.3 haad return 0;
752 1.1.1.3 haad }
753 1.1.1.3 haad
754 1.1.1.3 haad int vg_has_unknown_segments(const struct volume_group *vg)
755 1.1.1.3 haad {
756 1.1.1.3 haad struct lv_list *lvl;
757 1.1.1.3 haad
758 1.1.1.3 haad /* foreach LV */
759 1.1.1.3 haad dm_list_iterate_items(lvl, &vg->lvs)
760 1.1.1.3 haad if (lv_has_unknown_segments(lvl->lv))
761 1.1.1.3 haad return 1;
762 1.1.1.3 haad return 0;
763 1.1.1.3 haad }
764 1.1.1.3 haad
765 1.1.1.3 haad /*
766 1.1.1.3 haad * Create a VG with default parameters.
767 1.1.1.3 haad * Returns:
768 1.1.1.3 haad * - struct volume_group* with SUCCESS code: VG structure created
769 1.1.1.3 haad * - NULL or struct volume_group* with FAILED_* code: error creating VG structure
770 1.1.1.3 haad * Use vg_read_error() to determine success or failure.
771 1.1.1.3 haad * FIXME: cleanup usage of _vg_make_handle()
772 1.1.1.3 haad */
773 1.1.1.3 haad struct volume_group *vg_create(struct cmd_context *cmd, const char *vg_name)
774 1.1 haad {
775 1.1 haad struct volume_group *vg;
776 1.1 haad int consistent = 0;
777 1.1.1.3 haad struct dm_pool *mem;
778 1.1.1.3 haad uint32_t rc;
779 1.1 haad
780 1.1.1.3 haad if (!validate_name(vg_name)) {
781 1.1.1.3 haad log_error("Invalid vg name %s", vg_name);
782 1.1.1.3 haad /* FIXME: use _vg_make_handle() w/proper error code */
783 1.1.1.3 haad return NULL;
784 1.1.1.3 haad }
785 1.1 haad
786 1.1.1.3 haad rc = vg_lock_newname(cmd, vg_name);
787 1.1.1.3 haad if (rc != SUCCESS)
788 1.1.1.3 haad /* NOTE: let caller decide - this may be check for existence */
789 1.1.1.3 haad return _vg_make_handle(cmd, NULL, rc);
790 1.1.1.3 haad
791 1.1.1.3 haad /* FIXME: Is this vg_read_internal necessary? Move it inside
792 1.1.1.3 haad vg_lock_newname? */
793 1.1 haad /* is this vg name already in use ? */
794 1.1.1.3 haad if ((vg = vg_read_internal(cmd, vg_name, NULL, &consistent))) {
795 1.1.1.3 haad log_error("A volume group called '%s' already exists.", vg_name);
796 1.1.1.3 haad unlock_and_release_vg(cmd, vg, vg_name);
797 1.1.1.3 haad return _vg_make_handle(cmd, NULL, FAILED_EXIST);
798 1.1 haad }
799 1.1 haad
800 1.1.1.3 haad if (!(mem = dm_pool_create("lvm2 vg_create", VG_MEMPOOL_CHUNK)))
801 1.1.1.3 haad goto_bad;
802 1.1.1.3 haad
803 1.1.1.3 haad if (!(vg = dm_pool_zalloc(mem, sizeof(*vg))))
804 1.1.1.3 haad goto_bad;
805 1.1.1.3 haad
806 1.1 haad if (!id_create(&vg->id)) {
807 1.1.1.3 haad log_error("Couldn't create uuid for volume group '%s'.",
808 1.1.1.3 haad vg_name);
809 1.1 haad goto bad;
810 1.1 haad }
811 1.1 haad
812 1.1 haad /* Strip dev_dir if present */
813 1.1 haad vg_name = strip_dir(vg_name, cmd->dev_dir);
814 1.1 haad
815 1.1.1.3 haad vg->vgmem = mem;
816 1.1 haad vg->cmd = cmd;
817 1.1 haad
818 1.1 haad if (!(vg->name = dm_pool_strdup(mem, vg_name)))
819 1.1 haad goto_bad;
820 1.1 haad
821 1.1 haad vg->seqno = 0;
822 1.1 haad
823 1.1 haad vg->status = (RESIZEABLE_VG | LVM_READ | LVM_WRITE);
824 1.1 haad if (!(vg->system_id = dm_pool_alloc(mem, NAME_LEN)))
825 1.1 haad goto_bad;
826 1.1 haad
827 1.1 haad *vg->system_id = '\0';
828 1.1 haad
829 1.1.1.3 haad vg->extent_size = DEFAULT_EXTENT_SIZE * 2;
830 1.1 haad vg->extent_count = 0;
831 1.1 haad vg->free_count = 0;
832 1.1 haad
833 1.1.1.3 haad vg->max_lv = DEFAULT_MAX_LV;
834 1.1.1.3 haad vg->max_pv = DEFAULT_MAX_PV;
835 1.1 haad
836 1.1.1.3 haad vg->alloc = DEFAULT_ALLOC_POLICY;
837 1.1 haad
838 1.1 haad vg->pv_count = 0;
839 1.1 haad dm_list_init(&vg->pvs);
840 1.1 haad
841 1.1 haad dm_list_init(&vg->lvs);
842 1.1 haad
843 1.1 haad dm_list_init(&vg->tags);
844 1.1 haad
845 1.1.1.3 haad /* initialize removed_pvs list */
846 1.1.1.3 haad dm_list_init(&vg->removed_pvs);
847 1.1.1.3 haad
848 1.1 haad if (!(vg->fid = cmd->fmt->ops->create_instance(cmd->fmt, vg_name,
849 1.1 haad NULL, NULL))) {
850 1.1 haad log_error("Failed to create format instance");
851 1.1 haad goto bad;
852 1.1 haad }
853 1.1 haad
854 1.1 haad if (vg->fid->fmt->ops->vg_setup &&
855 1.1 haad !vg->fid->fmt->ops->vg_setup(vg->fid, vg)) {
856 1.1 haad log_error("Format specific setup of volume group '%s' failed.",
857 1.1 haad vg_name);
858 1.1 haad goto bad;
859 1.1 haad }
860 1.1.1.3 haad return _vg_make_handle(cmd, vg, SUCCESS);
861 1.1 haad
862 1.1.1.3 haad bad:
863 1.1.1.3 haad unlock_and_release_vg(cmd, vg, vg_name);
864 1.1.1.3 haad /* FIXME: use _vg_make_handle() w/proper error code */
865 1.1.1.3 haad return NULL;
866 1.1.1.3 haad }
867 1.1 haad
868 1.1.1.3 haad uint64_t extents_from_size(struct cmd_context *cmd, uint64_t size,
869 1.1.1.3 haad uint32_t extent_size)
870 1.1.1.3 haad {
871 1.1.1.3 haad if (size % extent_size) {
872 1.1.1.3 haad size += extent_size - size % extent_size;
873 1.1.1.3 haad log_print("Rounding up size to full physical extent %s",
874 1.1.1.3 haad display_size(cmd, size));
875 1.1.1.3 haad }
876 1.1 haad
877 1.1.1.3 haad if (size > (uint64_t) UINT32_MAX * extent_size) {
878 1.1.1.3 haad log_error("Volume too large (%s) for extent size %s. "
879 1.1.1.3 haad "Upper limit is %s.",
880 1.1.1.3 haad display_size(cmd, size),
881 1.1.1.3 haad display_size(cmd, (uint64_t) extent_size),
882 1.1.1.3 haad display_size(cmd, (uint64_t) UINT32_MAX *
883 1.1.1.3 haad extent_size));
884 1.1.1.3 haad return 0;
885 1.1.1.3 haad }
886 1.1.1.3 haad
887 1.1.1.3 haad return (uint64_t) size / extent_size;
888 1.1 haad }
889 1.1 haad
890 1.1 haad static int _recalc_extents(uint32_t *extents, const char *desc1,
891 1.1 haad const char *desc2, uint32_t old_size,
892 1.1 haad uint32_t new_size)
893 1.1 haad {
894 1.1 haad uint64_t size = (uint64_t) old_size * (*extents);
895 1.1 haad
896 1.1 haad if (size % new_size) {
897 1.1 haad log_error("New size %" PRIu64 " for %s%s not an exact number "
898 1.1 haad "of new extents.", size, desc1, desc2);
899 1.1 haad return 0;
900 1.1 haad }
901 1.1 haad
902 1.1 haad size /= new_size;
903 1.1 haad
904 1.1 haad if (size > UINT32_MAX) {
905 1.1 haad log_error("New extent count %" PRIu64 " for %s%s exceeds "
906 1.1 haad "32 bits.", size, desc1, desc2);
907 1.1 haad return 0;
908 1.1 haad }
909 1.1 haad
910 1.1 haad *extents = (uint32_t) size;
911 1.1 haad
912 1.1 haad return 1;
913 1.1 haad }
914 1.1 haad
915 1.1.1.3 haad int vg_set_extent_size(struct volume_group *vg, uint32_t new_size)
916 1.1 haad {
917 1.1 haad uint32_t old_size = vg->extent_size;
918 1.1 haad struct pv_list *pvl;
919 1.1 haad struct lv_list *lvl;
920 1.1 haad struct physical_volume *pv;
921 1.1 haad struct logical_volume *lv;
922 1.1 haad struct lv_segment *seg;
923 1.1 haad struct pv_segment *pvseg;
924 1.1 haad uint32_t s;
925 1.1 haad
926 1.1.1.3 haad if (!vg_is_resizeable(vg)) {
927 1.1.1.3 haad log_error("Volume group \"%s\" must be resizeable "
928 1.1.1.3 haad "to change PE size", vg->name);
929 1.1.1.3 haad return 0;
930 1.1.1.3 haad }
931 1.1.1.3 haad
932 1.1.1.3 haad if (!new_size) {
933 1.1.1.3 haad log_error("Physical extent size may not be zero");
934 1.1.1.3 haad return 0;
935 1.1.1.3 haad }
936 1.1.1.3 haad
937 1.1.1.3 haad if (new_size == vg->extent_size)
938 1.1.1.3 haad return 1;
939 1.1.1.3 haad
940 1.1.1.3 haad if (new_size & (new_size - 1)) {
941 1.1.1.3 haad log_error("Physical extent size must be a power of 2.");
942 1.1.1.3 haad return 0;
943 1.1.1.3 haad }
944 1.1.1.3 haad
945 1.1.1.3 haad if (new_size > vg->extent_size) {
946 1.1.1.3 haad if ((uint64_t) vg_size(vg) % new_size) {
947 1.1.1.3 haad /* FIXME Adjust used PV sizes instead */
948 1.1.1.3 haad log_error("New extent size is not a perfect fit");
949 1.1.1.3 haad return 0;
950 1.1.1.3 haad }
951 1.1.1.3 haad }
952 1.1.1.3 haad
953 1.1 haad vg->extent_size = new_size;
954 1.1 haad
955 1.1 haad if (vg->fid->fmt->ops->vg_setup &&
956 1.1 haad !vg->fid->fmt->ops->vg_setup(vg->fid, vg))
957 1.1 haad return_0;
958 1.1 haad
959 1.1 haad if (!_recalc_extents(&vg->extent_count, vg->name, "", old_size,
960 1.1 haad new_size))
961 1.1 haad return_0;
962 1.1 haad
963 1.1 haad if (!_recalc_extents(&vg->free_count, vg->name, " free space",
964 1.1 haad old_size, new_size))
965 1.1 haad return_0;
966 1.1 haad
967 1.1 haad /* foreach PV */
968 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
969 1.1 haad pv = pvl->pv;
970 1.1 haad
971 1.1 haad pv->pe_size = new_size;
972 1.1 haad if (!_recalc_extents(&pv->pe_count, pv_dev_name(pv), "",
973 1.1 haad old_size, new_size))
974 1.1 haad return_0;
975 1.1 haad
976 1.1 haad if (!_recalc_extents(&pv->pe_alloc_count, pv_dev_name(pv),
977 1.1 haad " allocated space", old_size, new_size))
978 1.1 haad return_0;
979 1.1 haad
980 1.1 haad /* foreach free PV Segment */
981 1.1 haad dm_list_iterate_items(pvseg, &pv->segments) {
982 1.1 haad if (pvseg_is_allocated(pvseg))
983 1.1 haad continue;
984 1.1 haad
985 1.1 haad if (!_recalc_extents(&pvseg->pe, pv_dev_name(pv),
986 1.1 haad " PV segment start", old_size,
987 1.1 haad new_size))
988 1.1 haad return_0;
989 1.1 haad if (!_recalc_extents(&pvseg->len, pv_dev_name(pv),
990 1.1 haad " PV segment length", old_size,
991 1.1 haad new_size))
992 1.1 haad return_0;
993 1.1 haad }
994 1.1 haad }
995 1.1 haad
996 1.1 haad /* foreach LV */
997 1.1 haad dm_list_iterate_items(lvl, &vg->lvs) {
998 1.1 haad lv = lvl->lv;
999 1.1 haad
1000 1.1 haad if (!_recalc_extents(&lv->le_count, lv->name, "", old_size,
1001 1.1 haad new_size))
1002 1.1 haad return_0;
1003 1.1 haad
1004 1.1 haad dm_list_iterate_items(seg, &lv->segments) {
1005 1.1 haad if (!_recalc_extents(&seg->le, lv->name,
1006 1.1 haad " segment start", old_size,
1007 1.1 haad new_size))
1008 1.1 haad return_0;
1009 1.1 haad
1010 1.1 haad if (!_recalc_extents(&seg->len, lv->name,
1011 1.1 haad " segment length", old_size,
1012 1.1 haad new_size))
1013 1.1 haad return_0;
1014 1.1 haad
1015 1.1 haad if (!_recalc_extents(&seg->area_len, lv->name,
1016 1.1 haad " area length", old_size,
1017 1.1 haad new_size))
1018 1.1 haad return_0;
1019 1.1 haad
1020 1.1 haad if (!_recalc_extents(&seg->extents_copied, lv->name,
1021 1.1 haad " extents moved", old_size,
1022 1.1 haad new_size))
1023 1.1 haad return_0;
1024 1.1 haad
1025 1.1 haad /* foreach area */
1026 1.1 haad for (s = 0; s < seg->area_count; s++) {
1027 1.1 haad switch (seg_type(seg, s)) {
1028 1.1 haad case AREA_PV:
1029 1.1 haad if (!_recalc_extents
1030 1.1 haad (&seg_pe(seg, s),
1031 1.1 haad lv->name,
1032 1.1 haad " pvseg start", old_size,
1033 1.1 haad new_size))
1034 1.1 haad return_0;
1035 1.1 haad if (!_recalc_extents
1036 1.1 haad (&seg_pvseg(seg, s)->len,
1037 1.1 haad lv->name,
1038 1.1 haad " pvseg length", old_size,
1039 1.1 haad new_size))
1040 1.1 haad return_0;
1041 1.1 haad break;
1042 1.1 haad case AREA_LV:
1043 1.1 haad if (!_recalc_extents
1044 1.1 haad (&seg_le(seg, s), lv->name,
1045 1.1 haad " area start", old_size,
1046 1.1 haad new_size))
1047 1.1 haad return_0;
1048 1.1 haad break;
1049 1.1 haad case AREA_UNASSIGNED:
1050 1.1 haad log_error("Unassigned area %u found in "
1051 1.1 haad "segment", s);
1052 1.1 haad return 0;
1053 1.1 haad }
1054 1.1 haad }
1055 1.1 haad }
1056 1.1 haad
1057 1.1 haad }
1058 1.1 haad
1059 1.1 haad return 1;
1060 1.1 haad }
1061 1.1 haad
1062 1.1.1.3 haad int vg_set_max_lv(struct volume_group *vg, uint32_t max_lv)
1063 1.1.1.3 haad {
1064 1.1.1.3 haad if (!vg_is_resizeable(vg)) {
1065 1.1.1.3 haad log_error("Volume group \"%s\" must be resizeable "
1066 1.1.1.3 haad "to change MaxLogicalVolume", vg->name);
1067 1.1.1.3 haad return 0;
1068 1.1.1.3 haad }
1069 1.1.1.3 haad
1070 1.1.1.3 haad if (!(vg->fid->fmt->features & FMT_UNLIMITED_VOLS)) {
1071 1.1.1.3 haad if (!max_lv)
1072 1.1.1.3 haad max_lv = 255;
1073 1.1.1.3 haad else if (max_lv > 255) {
1074 1.1.1.3 haad log_error("MaxLogicalVolume limit is 255");
1075 1.1.1.3 haad return 0;
1076 1.1.1.3 haad }
1077 1.1.1.3 haad }
1078 1.1.1.3 haad
1079 1.1.1.3 haad if (max_lv && max_lv < vg_visible_lvs(vg)) {
1080 1.1.1.3 haad log_error("MaxLogicalVolume is less than the current number "
1081 1.1.1.3 haad "%d of LVs for %s", vg_visible_lvs(vg),
1082 1.1.1.3 haad vg->name);
1083 1.1.1.3 haad return 0;
1084 1.1.1.3 haad }
1085 1.1.1.3 haad vg->max_lv = max_lv;
1086 1.1.1.3 haad
1087 1.1.1.3 haad return 1;
1088 1.1.1.3 haad }
1089 1.1.1.3 haad
1090 1.1.1.3 haad int vg_set_max_pv(struct volume_group *vg, uint32_t max_pv)
1091 1.1.1.3 haad {
1092 1.1.1.3 haad if (!vg_is_resizeable(vg)) {
1093 1.1.1.3 haad log_error("Volume group \"%s\" must be resizeable "
1094 1.1.1.3 haad "to change MaxPhysicalVolumes", vg->name);
1095 1.1.1.3 haad return 0;
1096 1.1.1.3 haad }
1097 1.1.1.3 haad
1098 1.1.1.3 haad if (!(vg->fid->fmt->features & FMT_UNLIMITED_VOLS)) {
1099 1.1.1.3 haad if (!max_pv)
1100 1.1.1.3 haad max_pv = 255;
1101 1.1.1.3 haad else if (max_pv > 255) {
1102 1.1.1.3 haad log_error("MaxPhysicalVolume limit is 255");
1103 1.1.1.3 haad return 0;
1104 1.1.1.3 haad }
1105 1.1.1.3 haad }
1106 1.1.1.3 haad
1107 1.1.1.3 haad if (max_pv && max_pv < vg->pv_count) {
1108 1.1.1.3 haad log_error("MaxPhysicalVolumes is less than the current number "
1109 1.1.1.3 haad "%d of PVs for \"%s\"", vg->pv_count,
1110 1.1.1.3 haad vg->name);
1111 1.1.1.3 haad return 0;
1112 1.1.1.3 haad }
1113 1.1.1.3 haad vg->max_pv = max_pv;
1114 1.1.1.3 haad return 1;
1115 1.1.1.3 haad }
1116 1.1.1.3 haad
1117 1.1.1.3 haad int vg_set_alloc_policy(struct volume_group *vg, alloc_policy_t alloc)
1118 1.1.1.3 haad {
1119 1.1.1.3 haad if (alloc == ALLOC_INHERIT) {
1120 1.1.1.3 haad log_error("Volume Group allocation policy cannot inherit "
1121 1.1.1.3 haad "from anything");
1122 1.1.1.3 haad return 0;
1123 1.1.1.3 haad }
1124 1.1.1.3 haad
1125 1.1.1.3 haad if (alloc == vg->alloc)
1126 1.1.1.3 haad return 1;
1127 1.1.1.3 haad
1128 1.1.1.3 haad vg->alloc = alloc;
1129 1.1.1.3 haad return 1;
1130 1.1.1.3 haad }
1131 1.1.1.3 haad
1132 1.1.1.3 haad int vg_set_clustered(struct volume_group *vg, int clustered)
1133 1.1.1.3 haad {
1134 1.1.1.3 haad struct lv_list *lvl;
1135 1.1.1.3 haad if (clustered) {
1136 1.1.1.3 haad dm_list_iterate_items(lvl, &vg->lvs) {
1137 1.1.1.3 haad if (lv_is_origin(lvl->lv) || lv_is_cow(lvl->lv)) {
1138 1.1.1.3 haad log_error("Volume group %s contains snapshots "
1139 1.1.1.3 haad "that are not yet supported.",
1140 1.1.1.3 haad vg->name);
1141 1.1.1.3 haad return 0;
1142 1.1.1.3 haad }
1143 1.1.1.3 haad }
1144 1.1.1.3 haad }
1145 1.1.1.3 haad
1146 1.1.1.3 haad if (clustered)
1147 1.1.1.3 haad vg->status |= CLUSTERED;
1148 1.1.1.3 haad else
1149 1.1.1.3 haad vg->status &= ~CLUSTERED;
1150 1.1.1.3 haad return 1;
1151 1.1.1.3 haad }
1152 1.1.1.3 haad
1153 1.1 haad /*
1154 1.1 haad * Separate metadata areas after splitting a VG.
1155 1.1 haad * Also accepts orphan VG as destination (for vgreduce).
1156 1.1 haad */
1157 1.1 haad int vg_split_mdas(struct cmd_context *cmd __attribute((unused)),
1158 1.1 haad struct volume_group *vg_from, struct volume_group *vg_to)
1159 1.1 haad {
1160 1.1 haad struct metadata_area *mda, *mda2;
1161 1.1 haad struct dm_list *mdas_from, *mdas_to;
1162 1.1 haad int common_mda = 0;
1163 1.1 haad
1164 1.1 haad mdas_from = &vg_from->fid->metadata_areas;
1165 1.1 haad mdas_to = &vg_to->fid->metadata_areas;
1166 1.1 haad
1167 1.1 haad dm_list_iterate_items_safe(mda, mda2, mdas_from) {
1168 1.1 haad if (!mda->ops->mda_in_vg) {
1169 1.1 haad common_mda = 1;
1170 1.1 haad continue;
1171 1.1 haad }
1172 1.1 haad
1173 1.1 haad if (!mda->ops->mda_in_vg(vg_from->fid, vg_from, mda)) {
1174 1.1 haad if (is_orphan_vg(vg_to->name))
1175 1.1 haad dm_list_del(&mda->list);
1176 1.1 haad else
1177 1.1 haad dm_list_move(mdas_to, &mda->list);
1178 1.1 haad }
1179 1.1 haad }
1180 1.1 haad
1181 1.1 haad if (dm_list_empty(mdas_from) ||
1182 1.1 haad (!is_orphan_vg(vg_to->name) && dm_list_empty(mdas_to)))
1183 1.1 haad return common_mda;
1184 1.1 haad
1185 1.1 haad return 1;
1186 1.1 haad }
1187 1.1 haad
1188 1.1.1.3 haad /*
1189 1.1.1.3 haad * See if we may pvcreate on this device.
1190 1.1.1.3 haad * 0 indicates we may not.
1191 1.1.1.3 haad */
1192 1.1.1.3 haad static int pvcreate_check(struct cmd_context *cmd, const char *name,
1193 1.1.1.3 haad struct pvcreate_params *pp)
1194 1.1.1.3 haad {
1195 1.1.1.3 haad struct physical_volume *pv;
1196 1.1.1.3 haad struct device *dev;
1197 1.1.1.3 haad uint64_t md_superblock, swap_signature;
1198 1.1.1.3 haad int wipe_md, wipe_swap;
1199 1.1.1.3 haad
1200 1.1.1.3 haad /* FIXME Check partition type is LVM unless --force is given */
1201 1.1.1.3 haad
1202 1.1.1.3 haad /* Is there a pv here already? */
1203 1.1.1.3 haad pv = pv_read(cmd, name, NULL, NULL, 0, 0);
1204 1.1.1.3 haad
1205 1.1.1.3 haad /*
1206 1.1.1.3 haad * If a PV has no MDAs it may appear to be an orphan until the
1207 1.1.1.3 haad * metadata is read off another PV in the same VG. Detecting
1208 1.1.1.3 haad * this means checking every VG by scanning every PV on the
1209 1.1.1.3 haad * system.
1210 1.1.1.3 haad */
1211 1.1.1.3 haad if (pv && is_orphan(pv)) {
1212 1.1.1.3 haad if (!scan_vgs_for_pvs(cmd))
1213 1.1.1.3 haad return_0;
1214 1.1.1.3 haad pv = pv_read(cmd, name, NULL, NULL, 0, 0);
1215 1.1.1.3 haad }
1216 1.1.1.3 haad
1217 1.1.1.3 haad /* Allow partial & exported VGs to be destroyed. */
1218 1.1.1.3 haad /* We must have -ff to overwrite a non orphan */
1219 1.1.1.3 haad if (pv && !is_orphan(pv) && pp->force != DONT_PROMPT_OVERRIDE) {
1220 1.1.1.3 haad log_error("Can't initialize physical volume \"%s\" of "
1221 1.1.1.3 haad "volume group \"%s\" without -ff", name, pv_vg_name(pv));
1222 1.1.1.3 haad return 0;
1223 1.1.1.3 haad }
1224 1.1.1.3 haad
1225 1.1.1.3 haad /* prompt */
1226 1.1.1.3 haad if (pv && !is_orphan(pv) && !pp->yes &&
1227 1.1.1.3 haad yes_no_prompt(_really_init, name, pv_vg_name(pv)) == 'n') {
1228 1.1.1.3 haad log_print("%s: physical volume not initialized", name);
1229 1.1.1.3 haad return 0;
1230 1.1.1.3 haad }
1231 1.1.1.3 haad
1232 1.1.1.3 haad if (sigint_caught())
1233 1.1.1.3 haad return 0;
1234 1.1.1.3 haad
1235 1.1.1.3 haad dev = dev_cache_get(name, cmd->filter);
1236 1.1.1.3 haad
1237 1.1.1.3 haad /* Is there an md superblock here? */
1238 1.1.1.3 haad if (!dev && md_filtering()) {
1239 1.1.1.3 haad unlock_vg(cmd, VG_ORPHANS);
1240 1.1.1.3 haad
1241 1.1.1.3 haad persistent_filter_wipe(cmd->filter);
1242 1.1.1.3 haad lvmcache_destroy(cmd, 1);
1243 1.1.1.3 haad
1244 1.1.1.3 haad init_md_filtering(0);
1245 1.1.1.3 haad if (!lock_vol(cmd, VG_ORPHANS, LCK_VG_WRITE)) {
1246 1.1.1.3 haad log_error("Can't get lock for orphan PVs");
1247 1.1.1.3 haad init_md_filtering(1);
1248 1.1.1.3 haad return 0;
1249 1.1.1.3 haad }
1250 1.1.1.3 haad dev = dev_cache_get(name, cmd->filter);
1251 1.1.1.3 haad init_md_filtering(1);
1252 1.1.1.3 haad }
1253 1.1.1.3 haad
1254 1.1.1.3 haad if (!dev) {
1255 1.1.1.3 haad log_error("Device %s not found (or ignored by filtering).", name);
1256 1.1.1.3 haad return 0;
1257 1.1.1.3 haad }
1258 1.1.1.3 haad
1259 1.1.1.3 haad /*
1260 1.1.1.3 haad * This test will fail if the device belongs to an MD array.
1261 1.1.1.3 haad */
1262 1.1.1.3 haad if (!dev_test_excl(dev)) {
1263 1.1.1.3 haad /* FIXME Detect whether device-mapper itself is still using it */
1264 1.1.1.3 haad log_error("Can't open %s exclusively. Mounted filesystem?",
1265 1.1.1.3 haad name);
1266 1.1.1.3 haad return 0;
1267 1.1.1.3 haad }
1268 1.1.1.3 haad
1269 1.1.1.3 haad /* Wipe superblock? */
1270 1.1.1.3 haad if ((wipe_md = dev_is_md(dev, &md_superblock)) == 1 &&
1271 1.1.1.3 haad ((!pp->idp && !pp->restorefile) || pp->yes ||
1272 1.1.1.3 haad (yes_no_prompt("Software RAID md superblock "
1273 1.1.1.3 haad "detected on %s. Wipe it? [y/n] ", name) == 'y'))) {
1274 1.1.1.3 haad log_print("Wiping software RAID md superblock on %s", name);
1275 1.1.1.3 haad if (!dev_set(dev, md_superblock, 4, 0)) {
1276 1.1.1.3 haad log_error("Failed to wipe RAID md superblock on %s",
1277 1.1.1.3 haad name);
1278 1.1.1.3 haad return 0;
1279 1.1.1.3 haad }
1280 1.1.1.3 haad }
1281 1.1.1.3 haad
1282 1.1.1.3 haad if (wipe_md == -1) {
1283 1.1.1.3 haad log_error("Fatal error while trying to detect software "
1284 1.1.1.3 haad "RAID md superblock on %s", name);
1285 1.1.1.3 haad return 0;
1286 1.1.1.3 haad }
1287 1.1.1.3 haad
1288 1.1.1.3 haad if ((wipe_swap = dev_is_swap(dev, &swap_signature)) == 1 &&
1289 1.1.1.3 haad ((!pp->idp && !pp->restorefile) || pp->yes ||
1290 1.1.1.3 haad (yes_no_prompt("Swap signature detected on %s. Wipe it? [y/n] ",
1291 1.1.1.3 haad name) == 'y'))) {
1292 1.1.1.3 haad log_print("Wiping swap signature on %s", name);
1293 1.1.1.3 haad if (!dev_set(dev, swap_signature, 10, 0)) {
1294 1.1.1.3 haad log_error("Failed to wipe swap signature on %s", name);
1295 1.1.1.3 haad return 0;
1296 1.1.1.3 haad }
1297 1.1.1.3 haad }
1298 1.1.1.3 haad
1299 1.1.1.3 haad if (wipe_swap == -1) {
1300 1.1.1.3 haad log_error("Fatal error while trying to detect swap "
1301 1.1.1.3 haad "signature on %s", name);
1302 1.1.1.3 haad return 0;
1303 1.1.1.3 haad }
1304 1.1.1.3 haad
1305 1.1.1.3 haad if (sigint_caught())
1306 1.1.1.3 haad return 0;
1307 1.1.1.3 haad
1308 1.1.1.3 haad if (pv && !is_orphan(pv) && pp->force) {
1309 1.1.1.3 haad log_warn("WARNING: Forcing physical volume creation on "
1310 1.1.1.3 haad "%s%s%s%s", name,
1311 1.1.1.3 haad !is_orphan(pv) ? " of volume group \"" : "",
1312 1.1.1.3 haad !is_orphan(pv) ? pv_vg_name(pv) : "",
1313 1.1.1.3 haad !is_orphan(pv) ? "\"" : "");
1314 1.1.1.3 haad }
1315 1.1.1.3 haad
1316 1.1.1.3 haad return 1;
1317 1.1.1.3 haad }
1318 1.1.1.3 haad
1319 1.1.1.3 haad void pvcreate_params_set_defaults(struct pvcreate_params *pp)
1320 1.1.1.3 haad {
1321 1.1.1.3 haad memset(pp, 0, sizeof(*pp));
1322 1.1.1.3 haad pp->zero = 1;
1323 1.1.1.3 haad pp->size = 0;
1324 1.1.1.3 haad pp->data_alignment = UINT64_C(0);
1325 1.1.1.3 haad pp->data_alignment_offset = UINT64_C(0);
1326 1.1.1.3 haad pp->pvmetadatacopies = DEFAULT_PVMETADATACOPIES;
1327 1.1.1.3 haad pp->pvmetadatasize = DEFAULT_PVMETADATASIZE;
1328 1.1.1.3 haad pp->labelsector = DEFAULT_LABELSECTOR;
1329 1.1.1.3 haad pp->idp = 0;
1330 1.1.1.3 haad pp->pe_start = 0;
1331 1.1.1.3 haad pp->extent_count = 0;
1332 1.1.1.3 haad pp->extent_size = 0;
1333 1.1.1.3 haad pp->restorefile = 0;
1334 1.1.1.3 haad pp->force = PROMPT;
1335 1.1.1.3 haad pp->yes = 0;
1336 1.1.1.3 haad }
1337 1.1.1.3 haad
1338 1.1.1.3 haad /*
1339 1.1.1.3 haad * pvcreate_single() - initialize a device with PV label and metadata area
1340 1.1 haad *
1341 1.1.1.3 haad * Parameters:
1342 1.1.1.3 haad * - pv_name: device path to initialize
1343 1.1.1.3 haad * - pp: parameters to pass to pv_create; if NULL, use default values
1344 1.1 haad *
1345 1.1.1.3 haad * Returns:
1346 1.1.1.3 haad * NULL: error
1347 1.1.1.3 haad * struct physical_volume * (non-NULL): handle to physical volume created
1348 1.1 haad */
1349 1.1.1.3 haad struct physical_volume * pvcreate_single(struct cmd_context *cmd,
1350 1.1.1.3 haad const char *pv_name,
1351 1.1.1.3 haad struct pvcreate_params *pp)
1352 1.1.1.3 haad {
1353 1.1.1.3 haad void *pv;
1354 1.1.1.3 haad struct device *dev;
1355 1.1.1.3 haad struct dm_list mdas;
1356 1.1.1.3 haad struct pvcreate_params default_pp;
1357 1.1.1.3 haad char buffer[64] __attribute((aligned(8)));
1358 1.1.1.3 haad
1359 1.1.1.3 haad pvcreate_params_set_defaults(&default_pp);
1360 1.1.1.3 haad if (!pp)
1361 1.1.1.3 haad pp = &default_pp;
1362 1.1.1.3 haad
1363 1.1.1.3 haad if (pp->idp) {
1364 1.1.1.3 haad if ((dev = device_from_pvid(cmd, pp->idp)) &&
1365 1.1.1.3 haad (dev != dev_cache_get(pv_name, cmd->filter))) {
1366 1.1.1.3 haad if (!id_write_format((const struct id*)&pp->idp->uuid,
1367 1.1.1.3 haad buffer, sizeof(buffer)))
1368 1.1.1.3 haad return_NULL;
1369 1.1.1.3 haad log_error("uuid %s already in use on \"%s\"", buffer,
1370 1.1.1.3 haad dev_name(dev));
1371 1.1.1.3 haad return NULL;
1372 1.1.1.3 haad }
1373 1.1.1.3 haad }
1374 1.1.1.3 haad
1375 1.1.1.3 haad if (!pvcreate_check(cmd, pv_name, pp))
1376 1.1.1.3 haad goto error;
1377 1.1.1.3 haad
1378 1.1.1.3 haad if (sigint_caught())
1379 1.1.1.3 haad goto error;
1380 1.1.1.3 haad
1381 1.1.1.3 haad if (!(dev = dev_cache_get(pv_name, cmd->filter))) {
1382 1.1.1.3 haad log_error("%s: Couldn't find device. Check your filters?",
1383 1.1.1.3 haad pv_name);
1384 1.1.1.3 haad goto error;
1385 1.1.1.3 haad }
1386 1.1.1.3 haad
1387 1.1.1.3 haad dm_list_init(&mdas);
1388 1.1.1.3 haad if (!(pv = pv_create(cmd, dev, pp->idp, pp->size,
1389 1.1.1.3 haad pp->data_alignment, pp->data_alignment_offset,
1390 1.1.1.3 haad pp->pe_start, pp->extent_count, pp->extent_size,
1391 1.1.1.3 haad pp->pvmetadatacopies,
1392 1.1.1.3 haad pp->pvmetadatasize,&mdas))) {
1393 1.1.1.3 haad log_error("Failed to setup physical volume \"%s\"", pv_name);
1394 1.1.1.3 haad goto error;
1395 1.1.1.3 haad }
1396 1.1.1.3 haad
1397 1.1.1.3 haad log_verbose("Set up physical volume for \"%s\" with %" PRIu64
1398 1.1.1.3 haad " available sectors", pv_name, pv_size(pv));
1399 1.1.1.3 haad
1400 1.1.1.3 haad /* Wipe existing label first */
1401 1.1.1.3 haad if (!label_remove(pv_dev(pv))) {
1402 1.1.1.3 haad log_error("Failed to wipe existing label on %s", pv_name);
1403 1.1.1.3 haad goto error;
1404 1.1.1.3 haad }
1405 1.1.1.3 haad
1406 1.1.1.3 haad if (pp->zero) {
1407 1.1.1.3 haad log_verbose("Zeroing start of device %s", pv_name);
1408 1.1.1.3 haad if (!dev_open_quiet(dev)) {
1409 1.1.1.3 haad log_error("%s not opened: device not zeroed", pv_name);
1410 1.1.1.3 haad goto error;
1411 1.1.1.3 haad }
1412 1.1.1.3 haad
1413 1.1.1.3 haad if (!dev_set(dev, UINT64_C(0), (size_t) 2048, 0)) {
1414 1.1.1.3 haad log_error("%s not wiped: aborting", pv_name);
1415 1.1.1.3 haad dev_close(dev);
1416 1.1.1.3 haad goto error;
1417 1.1.1.3 haad }
1418 1.1.1.3 haad dev_close(dev);
1419 1.1.1.3 haad }
1420 1.1.1.3 haad
1421 1.1.1.3 haad log_very_verbose("Writing physical volume data to disk \"%s\"",
1422 1.1.1.3 haad pv_name);
1423 1.1.1.3 haad if (!(pv_write(cmd, (struct physical_volume *)pv, &mdas,
1424 1.1.1.3 haad pp->labelsector))) {
1425 1.1.1.3 haad log_error("Failed to write physical volume \"%s\"", pv_name);
1426 1.1.1.3 haad goto error;
1427 1.1.1.3 haad }
1428 1.1.1.3 haad
1429 1.1.1.3 haad log_print("Physical volume \"%s\" successfully created", pv_name);
1430 1.1.1.3 haad
1431 1.1.1.3 haad return pv;
1432 1.1.1.3 haad
1433 1.1.1.3 haad error:
1434 1.1.1.3 haad return NULL;
1435 1.1 haad }
1436 1.1 haad
1437 1.1 haad static void _free_pv(struct dm_pool *mem, struct physical_volume *pv)
1438 1.1 haad {
1439 1.1 haad dm_pool_free(mem, pv);
1440 1.1 haad }
1441 1.1 haad
1442 1.1 haad static struct physical_volume *_alloc_pv(struct dm_pool *mem, struct device *dev)
1443 1.1 haad {
1444 1.1 haad struct physical_volume *pv = dm_pool_zalloc(mem, sizeof(*pv));
1445 1.1 haad
1446 1.1 haad if (!pv)
1447 1.1 haad return_NULL;
1448 1.1 haad
1449 1.1 haad if (!(pv->vg_name = dm_pool_zalloc(mem, NAME_LEN))) {
1450 1.1 haad dm_pool_free(mem, pv);
1451 1.1 haad return NULL;
1452 1.1 haad }
1453 1.1 haad
1454 1.1 haad pv->pe_size = 0;
1455 1.1 haad pv->pe_start = 0;
1456 1.1 haad pv->pe_count = 0;
1457 1.1 haad pv->pe_alloc_count = 0;
1458 1.1 haad pv->pe_align = 0;
1459 1.1.1.3 haad pv->pe_align_offset = 0;
1460 1.1 haad pv->fmt = NULL;
1461 1.1 haad pv->dev = dev;
1462 1.1 haad
1463 1.1 haad pv->status = ALLOCATABLE_PV;
1464 1.1 haad
1465 1.1 haad dm_list_init(&pv->tags);
1466 1.1 haad dm_list_init(&pv->segments);
1467 1.1 haad
1468 1.1 haad return pv;
1469 1.1 haad }
1470 1.1 haad
1471 1.1.1.3 haad /**
1472 1.1.1.3 haad * pv_create - initialize a physical volume for use with a volume group
1473 1.1.1.3 haad *
1474 1.1.1.3 haad * @fmt: format type
1475 1.1.1.3 haad * @dev: PV device to initialize
1476 1.1.1.3 haad * @size: size of the PV in sectors
1477 1.1.1.3 haad * @data_alignment: requested alignment of data
1478 1.1.1.3 haad * @data_alignment_offset: requested offset to aligned data
1479 1.1.1.3 haad * @pe_start: physical extent start
1480 1.1.1.3 haad * @existing_extent_count
1481 1.1.1.3 haad * @existing_extent_size
1482 1.1.1.3 haad * @pvmetadatacopies
1483 1.1.1.3 haad * @pvmetadatasize
1484 1.1.1.3 haad * @mdas
1485 1.1.1.3 haad *
1486 1.1.1.3 haad * Returns:
1487 1.1.1.3 haad * PV handle - physical volume initialized successfully
1488 1.1.1.3 haad * NULL - invalid parameter or problem initializing the physical volume
1489 1.1.1.3 haad *
1490 1.1.1.3 haad * Note:
1491 1.1.1.3 haad * FIXME: shorten argument list and replace with explict 'set' functions
1492 1.1.1.3 haad */
1493 1.1.1.3 haad struct physical_volume *pv_create(const struct cmd_context *cmd,
1494 1.1 haad struct device *dev,
1495 1.1 haad struct id *id, uint64_t size,
1496 1.1.1.3 haad unsigned long data_alignment,
1497 1.1.1.3 haad unsigned long data_alignment_offset,
1498 1.1 haad uint64_t pe_start,
1499 1.1 haad uint32_t existing_extent_count,
1500 1.1 haad uint32_t existing_extent_size,
1501 1.1 haad int pvmetadatacopies,
1502 1.1 haad uint64_t pvmetadatasize, struct dm_list *mdas)
1503 1.1 haad {
1504 1.1.1.3 haad const struct format_type *fmt = cmd->fmt;
1505 1.1 haad struct dm_pool *mem = fmt->cmd->mem;
1506 1.1 haad struct physical_volume *pv = _alloc_pv(mem, dev);
1507 1.1 haad
1508 1.1 haad if (!pv)
1509 1.1 haad return NULL;
1510 1.1 haad
1511 1.1 haad if (id)
1512 1.1 haad memcpy(&pv->id, id, sizeof(*id));
1513 1.1 haad else if (!id_create(&pv->id)) {
1514 1.1 haad log_error("Failed to create random uuid for %s.",
1515 1.1 haad dev_name(dev));
1516 1.1 haad goto bad;
1517 1.1 haad }
1518 1.1 haad
1519 1.1 haad if (!dev_get_size(pv->dev, &pv->size)) {
1520 1.1 haad log_error("%s: Couldn't get size.", pv_dev_name(pv));
1521 1.1 haad goto bad;
1522 1.1 haad }
1523 1.1 haad
1524 1.1 haad if (size) {
1525 1.1 haad if (size > pv->size)
1526 1.1 haad log_warn("WARNING: %s: Overriding real size. "
1527 1.1 haad "You could lose data.", pv_dev_name(pv));
1528 1.1 haad log_verbose("%s: Pretending size is %" PRIu64 " sectors.",
1529 1.1 haad pv_dev_name(pv), size);
1530 1.1 haad pv->size = size;
1531 1.1 haad }
1532 1.1 haad
1533 1.1 haad if (pv->size < PV_MIN_SIZE) {
1534 1.1 haad log_error("%s: Size must exceed minimum of %ld sectors.",
1535 1.1 haad pv_dev_name(pv), PV_MIN_SIZE);
1536 1.1 haad goto bad;
1537 1.1 haad }
1538 1.1 haad
1539 1.1.1.3 haad if (pv->size < data_alignment) {
1540 1.1.1.3 haad log_error("%s: Data alignment must not exceed device size.",
1541 1.1.1.3 haad pv_dev_name(pv));
1542 1.1.1.3 haad goto bad;
1543 1.1.1.3 haad }
1544 1.1.1.3 haad
1545 1.1 haad pv->fmt = fmt;
1546 1.1 haad pv->vg_name = fmt->orphan_vg_name;
1547 1.1 haad
1548 1.1 haad if (!fmt->ops->pv_setup(fmt, pe_start, existing_extent_count,
1549 1.1.1.3 haad existing_extent_size, data_alignment,
1550 1.1.1.3 haad data_alignment_offset,
1551 1.1 haad pvmetadatacopies, pvmetadatasize, mdas,
1552 1.1 haad pv, NULL)) {
1553 1.1 haad log_error("%s: Format-specific setup of physical volume "
1554 1.1 haad "failed.", pv_dev_name(pv));
1555 1.1 haad goto bad;
1556 1.1 haad }
1557 1.1.1.3 haad
1558 1.1 haad return pv;
1559 1.1 haad
1560 1.1 haad bad:
1561 1.1 haad _free_pv(mem, pv);
1562 1.1 haad return NULL;
1563 1.1 haad }
1564 1.1 haad
1565 1.1 haad /* FIXME: liblvm todo - make into function that returns handle */
1566 1.1 haad struct pv_list *find_pv_in_vg(const struct volume_group *vg,
1567 1.1 haad const char *pv_name)
1568 1.1 haad {
1569 1.1 haad return _find_pv_in_vg(vg, pv_name);
1570 1.1 haad }
1571 1.1 haad
1572 1.1 haad static struct pv_list *_find_pv_in_vg(const struct volume_group *vg,
1573 1.1 haad const char *pv_name)
1574 1.1 haad {
1575 1.1 haad struct pv_list *pvl;
1576 1.1 haad
1577 1.1 haad dm_list_iterate_items(pvl, &vg->pvs)
1578 1.1 haad if (pvl->pv->dev == dev_cache_get(pv_name, vg->cmd->filter))
1579 1.1 haad return pvl;
1580 1.1 haad
1581 1.1 haad return NULL;
1582 1.1 haad }
1583 1.1 haad
1584 1.1 haad struct pv_list *find_pv_in_pv_list(const struct dm_list *pl,
1585 1.1 haad const struct physical_volume *pv)
1586 1.1 haad {
1587 1.1 haad struct pv_list *pvl;
1588 1.1 haad
1589 1.1 haad dm_list_iterate_items(pvl, pl)
1590 1.1 haad if (pvl->pv == pv)
1591 1.1 haad return pvl;
1592 1.1 haad
1593 1.1 haad return NULL;
1594 1.1 haad }
1595 1.1 haad
1596 1.1 haad int pv_is_in_vg(struct volume_group *vg, struct physical_volume *pv)
1597 1.1 haad {
1598 1.1 haad struct pv_list *pvl;
1599 1.1 haad
1600 1.1 haad dm_list_iterate_items(pvl, &vg->pvs)
1601 1.1 haad if (pv == pvl->pv)
1602 1.1 haad return 1;
1603 1.1 haad
1604 1.1 haad return 0;
1605 1.1 haad }
1606 1.1 haad
1607 1.1 haad /**
1608 1.1 haad * find_pv_in_vg_by_uuid - Find PV in VG by PV UUID
1609 1.1 haad * @vg: volume group to search
1610 1.1 haad * @id: UUID of the PV to match
1611 1.1 haad *
1612 1.1 haad * Returns:
1613 1.1 haad * PV handle - if UUID of PV found in VG
1614 1.1 haad * NULL - invalid parameter or UUID of PV not found in VG
1615 1.1 haad *
1616 1.1 haad * Note
1617 1.1 haad * FIXME - liblvm todo - make into function that takes VG handle
1618 1.1 haad */
1619 1.1.1.3 haad struct physical_volume *find_pv_in_vg_by_uuid(const struct volume_group *vg,
1620 1.1 haad const struct id *id)
1621 1.1 haad {
1622 1.1 haad return _find_pv_in_vg_by_uuid(vg, id);
1623 1.1 haad }
1624 1.1 haad
1625 1.1 haad
1626 1.1 haad static struct physical_volume *_find_pv_in_vg_by_uuid(const struct volume_group *vg,
1627 1.1 haad const struct id *id)
1628 1.1 haad {
1629 1.1 haad struct pv_list *pvl;
1630 1.1 haad
1631 1.1 haad dm_list_iterate_items(pvl, &vg->pvs)
1632 1.1 haad if (id_equal(&pvl->pv->id, id))
1633 1.1 haad return pvl->pv;
1634 1.1 haad
1635 1.1 haad return NULL;
1636 1.1 haad }
1637 1.1 haad
1638 1.1 haad struct lv_list *find_lv_in_vg(const struct volume_group *vg,
1639 1.1 haad const char *lv_name)
1640 1.1 haad {
1641 1.1 haad struct lv_list *lvl;
1642 1.1 haad const char *ptr;
1643 1.1 haad
1644 1.1 haad /* Use last component */
1645 1.1 haad if ((ptr = strrchr(lv_name, '/')))
1646 1.1 haad ptr++;
1647 1.1 haad else
1648 1.1 haad ptr = lv_name;
1649 1.1 haad
1650 1.1 haad dm_list_iterate_items(lvl, &vg->lvs)
1651 1.1 haad if (!strcmp(lvl->lv->name, ptr))
1652 1.1 haad return lvl;
1653 1.1 haad
1654 1.1 haad return NULL;
1655 1.1 haad }
1656 1.1 haad
1657 1.1 haad struct lv_list *find_lv_in_lv_list(const struct dm_list *ll,
1658 1.1 haad const struct logical_volume *lv)
1659 1.1 haad {
1660 1.1 haad struct lv_list *lvl;
1661 1.1 haad
1662 1.1 haad dm_list_iterate_items(lvl, ll)
1663 1.1 haad if (lvl->lv == lv)
1664 1.1 haad return lvl;
1665 1.1 haad
1666 1.1 haad return NULL;
1667 1.1 haad }
1668 1.1 haad
1669 1.1 haad struct lv_list *find_lv_in_vg_by_lvid(struct volume_group *vg,
1670 1.1 haad const union lvid *lvid)
1671 1.1 haad {
1672 1.1 haad struct lv_list *lvl;
1673 1.1 haad
1674 1.1 haad dm_list_iterate_items(lvl, &vg->lvs)
1675 1.1 haad if (!strncmp(lvl->lv->lvid.s, lvid->s, sizeof(*lvid)))
1676 1.1 haad return lvl;
1677 1.1 haad
1678 1.1 haad return NULL;
1679 1.1 haad }
1680 1.1 haad
1681 1.1 haad struct logical_volume *find_lv(const struct volume_group *vg,
1682 1.1 haad const char *lv_name)
1683 1.1 haad {
1684 1.1 haad struct lv_list *lvl = find_lv_in_vg(vg, lv_name);
1685 1.1 haad return lvl ? lvl->lv : NULL;
1686 1.1 haad }
1687 1.1 haad
1688 1.1 haad struct physical_volume *find_pv(struct volume_group *vg, struct device *dev)
1689 1.1 haad {
1690 1.1 haad struct pv_list *pvl;
1691 1.1 haad
1692 1.1 haad dm_list_iterate_items(pvl, &vg->pvs)
1693 1.1 haad if (dev == pvl->pv->dev)
1694 1.1 haad return pvl->pv;
1695 1.1 haad
1696 1.1 haad return NULL;
1697 1.1 haad }
1698 1.1 haad
1699 1.1 haad /* FIXME: liblvm todo - make into function that returns handle */
1700 1.1 haad struct physical_volume *find_pv_by_name(struct cmd_context *cmd,
1701 1.1 haad const char *pv_name)
1702 1.1 haad {
1703 1.1 haad return _find_pv_by_name(cmd, pv_name);
1704 1.1 haad }
1705 1.1 haad
1706 1.1 haad
1707 1.1 haad static struct physical_volume *_find_pv_by_name(struct cmd_context *cmd,
1708 1.1 haad const char *pv_name)
1709 1.1 haad {
1710 1.1 haad struct physical_volume *pv;
1711 1.1 haad
1712 1.1.1.3 haad if (!(pv = _pv_read(cmd, cmd->mem, pv_name, NULL, NULL, 1, 0))) {
1713 1.1 haad log_error("Physical volume %s not found", pv_name);
1714 1.1 haad return NULL;
1715 1.1 haad }
1716 1.1 haad
1717 1.1 haad if (is_orphan_vg(pv->vg_name)) {
1718 1.1 haad /* If a PV has no MDAs - need to search all VGs for it */
1719 1.1 haad if (!scan_vgs_for_pvs(cmd))
1720 1.1 haad return_NULL;
1721 1.1.1.3 haad if (!(pv = _pv_read(cmd, cmd->mem, pv_name, NULL, NULL, 1, 0))) {
1722 1.1 haad log_error("Physical volume %s not found", pv_name);
1723 1.1 haad return NULL;
1724 1.1 haad }
1725 1.1 haad }
1726 1.1 haad
1727 1.1 haad if (is_orphan_vg(pv->vg_name)) {
1728 1.1 haad log_error("Physical volume %s not in a volume group", pv_name);
1729 1.1 haad return NULL;
1730 1.1 haad }
1731 1.1 haad
1732 1.1 haad return pv;
1733 1.1 haad }
1734 1.1 haad
1735 1.1 haad /* Find segment at a given logical extent in an LV */
1736 1.1 haad struct lv_segment *find_seg_by_le(const struct logical_volume *lv, uint32_t le)
1737 1.1 haad {
1738 1.1 haad struct lv_segment *seg;
1739 1.1 haad
1740 1.1 haad dm_list_iterate_items(seg, &lv->segments)
1741 1.1 haad if (le >= seg->le && le < seg->le + seg->len)
1742 1.1 haad return seg;
1743 1.1 haad
1744 1.1 haad return NULL;
1745 1.1 haad }
1746 1.1 haad
1747 1.1 haad struct lv_segment *first_seg(const struct logical_volume *lv)
1748 1.1 haad {
1749 1.1.1.3 haad struct lv_segment *seg;
1750 1.1 haad
1751 1.1 haad dm_list_iterate_items(seg, &lv->segments)
1752 1.1.1.3 haad return seg;
1753 1.1 haad
1754 1.1.1.3 haad return NULL;
1755 1.1 haad }
1756 1.1 haad
1757 1.1 haad /* Find segment at a given physical extent in a PV */
1758 1.1 haad struct pv_segment *find_peg_by_pe(const struct physical_volume *pv, uint32_t pe)
1759 1.1 haad {
1760 1.1 haad struct pv_segment *peg;
1761 1.1 haad
1762 1.1 haad dm_list_iterate_items(peg, &pv->segments)
1763 1.1 haad if (pe >= peg->pe && pe < peg->pe + peg->len)
1764 1.1 haad return peg;
1765 1.1 haad
1766 1.1 haad return NULL;
1767 1.1 haad }
1768 1.1 haad
1769 1.1.1.3 haad int vg_remove_mdas(struct volume_group *vg)
1770 1.1 haad {
1771 1.1 haad struct metadata_area *mda;
1772 1.1 haad
1773 1.1 haad /* FIXME Improve recovery situation? */
1774 1.1 haad /* Remove each copy of the metadata */
1775 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
1776 1.1 haad if (mda->ops->vg_remove &&
1777 1.1 haad !mda->ops->vg_remove(vg->fid, vg, mda))
1778 1.1 haad return_0;
1779 1.1 haad }
1780 1.1 haad
1781 1.1 haad return 1;
1782 1.1 haad }
1783 1.1 haad
1784 1.1.1.3 haad unsigned snapshot_count(const struct volume_group *vg)
1785 1.1.1.2 haad {
1786 1.1.1.2 haad struct lv_list *lvl;
1787 1.1.1.3 haad unsigned num_snapshots = 0;
1788 1.1.1.2 haad
1789 1.1.1.2 haad dm_list_iterate_items(lvl, &vg->lvs)
1790 1.1.1.3 haad if (lv_is_cow(lvl->lv))
1791 1.1.1.3 haad num_snapshots++;
1792 1.1.1.3 haad
1793 1.1.1.3 haad return num_snapshots;
1794 1.1.1.3 haad }
1795 1.1.1.3 haad
1796 1.1.1.3 haad unsigned vg_visible_lvs(const struct volume_group *vg)
1797 1.1.1.3 haad {
1798 1.1.1.3 haad struct lv_list *lvl;
1799 1.1.1.3 haad unsigned lv_count = 0;
1800 1.1.1.3 haad
1801 1.1.1.3 haad dm_list_iterate_items(lvl, &vg->lvs) {
1802 1.1.1.3 haad if (lv_is_visible(lvl->lv))
1803 1.1.1.2 haad lv_count++;
1804 1.1.1.3 haad }
1805 1.1.1.2 haad
1806 1.1.1.2 haad return lv_count;
1807 1.1.1.2 haad }
1808 1.1.1.2 haad
1809 1.1 haad /*
1810 1.1 haad * Determine whether two vgs are compatible for merging.
1811 1.1 haad */
1812 1.1 haad int vgs_are_compatible(struct cmd_context *cmd __attribute((unused)),
1813 1.1 haad struct volume_group *vg_from,
1814 1.1 haad struct volume_group *vg_to)
1815 1.1 haad {
1816 1.1 haad struct lv_list *lvl1, *lvl2;
1817 1.1 haad struct pv_list *pvl;
1818 1.1 haad char *name1, *name2;
1819 1.1 haad
1820 1.1 haad if (lvs_in_vg_activated(vg_from)) {
1821 1.1 haad log_error("Logical volumes in \"%s\" must be inactive",
1822 1.1 haad vg_from->name);
1823 1.1 haad return 0;
1824 1.1 haad }
1825 1.1 haad
1826 1.1 haad /* Check compatibility */
1827 1.1 haad if (vg_to->extent_size != vg_from->extent_size) {
1828 1.1 haad log_error("Extent sizes differ: %d (%s) and %d (%s)",
1829 1.1 haad vg_to->extent_size, vg_to->name,
1830 1.1 haad vg_from->extent_size, vg_from->name);
1831 1.1 haad return 0;
1832 1.1 haad }
1833 1.1 haad
1834 1.1 haad if (vg_to->max_pv &&
1835 1.1 haad (vg_to->max_pv < vg_to->pv_count + vg_from->pv_count)) {
1836 1.1 haad log_error("Maximum number of physical volumes (%d) exceeded "
1837 1.1 haad " for \"%s\" and \"%s\"", vg_to->max_pv, vg_to->name,
1838 1.1 haad vg_from->name);
1839 1.1 haad return 0;
1840 1.1 haad }
1841 1.1 haad
1842 1.1 haad if (vg_to->max_lv &&
1843 1.1.1.3 haad (vg_to->max_lv < vg_visible_lvs(vg_to) + vg_visible_lvs(vg_from))) {
1844 1.1 haad log_error("Maximum number of logical volumes (%d) exceeded "
1845 1.1 haad " for \"%s\" and \"%s\"", vg_to->max_lv, vg_to->name,
1846 1.1 haad vg_from->name);
1847 1.1 haad return 0;
1848 1.1 haad }
1849 1.1 haad
1850 1.1 haad /* Metadata types must be the same */
1851 1.1 haad if (vg_to->fid->fmt != vg_from->fid->fmt) {
1852 1.1 haad log_error("Metadata types differ for \"%s\" and \"%s\"",
1853 1.1 haad vg_to->name, vg_from->name);
1854 1.1 haad return 0;
1855 1.1 haad }
1856 1.1 haad
1857 1.1 haad /* Clustering attribute must be the same */
1858 1.1 haad if (vg_is_clustered(vg_to) != vg_is_clustered(vg_from)) {
1859 1.1 haad log_error("Clustered attribute differs for \"%s\" and \"%s\"",
1860 1.1 haad vg_to->name, vg_from->name);
1861 1.1 haad return 0;
1862 1.1 haad }
1863 1.1 haad
1864 1.1 haad /* Check no conflicts with LV names */
1865 1.1 haad dm_list_iterate_items(lvl1, &vg_to->lvs) {
1866 1.1 haad name1 = lvl1->lv->name;
1867 1.1 haad
1868 1.1 haad dm_list_iterate_items(lvl2, &vg_from->lvs) {
1869 1.1 haad name2 = lvl2->lv->name;
1870 1.1 haad
1871 1.1 haad if (!strcmp(name1, name2)) {
1872 1.1 haad log_error("Duplicate logical volume "
1873 1.1 haad "name \"%s\" "
1874 1.1 haad "in \"%s\" and \"%s\"",
1875 1.1 haad name1, vg_to->name, vg_from->name);
1876 1.1 haad return 0;
1877 1.1 haad }
1878 1.1 haad }
1879 1.1 haad }
1880 1.1 haad
1881 1.1 haad /* Check no PVs are constructed from either VG */
1882 1.1 haad dm_list_iterate_items(pvl, &vg_to->pvs) {
1883 1.1 haad if (pv_uses_vg(pvl->pv, vg_from)) {
1884 1.1 haad log_error("Physical volume %s might be constructed "
1885 1.1 haad "from same volume group %s.",
1886 1.1 haad pv_dev_name(pvl->pv), vg_from->name);
1887 1.1 haad return 0;
1888 1.1 haad }
1889 1.1 haad }
1890 1.1 haad
1891 1.1 haad dm_list_iterate_items(pvl, &vg_from->pvs) {
1892 1.1 haad if (pv_uses_vg(pvl->pv, vg_to)) {
1893 1.1 haad log_error("Physical volume %s might be constructed "
1894 1.1 haad "from same volume group %s.",
1895 1.1 haad pv_dev_name(pvl->pv), vg_to->name);
1896 1.1 haad return 0;
1897 1.1 haad }
1898 1.1 haad }
1899 1.1 haad
1900 1.1 haad return 1;
1901 1.1 haad }
1902 1.1 haad
1903 1.1 haad struct _lv_postorder_baton {
1904 1.1 haad int (*fn)(struct logical_volume *lv, void *data);
1905 1.1 haad void *data;
1906 1.1 haad };
1907 1.1 haad
1908 1.1 haad static int _lv_postorder_visit(struct logical_volume *,
1909 1.1 haad int (*fn)(struct logical_volume *lv, void *data),
1910 1.1 haad void *data);
1911 1.1 haad
1912 1.1 haad static int _lv_postorder_level(struct logical_volume *lv, void *data)
1913 1.1 haad {
1914 1.1 haad struct _lv_postorder_baton *baton = data;
1915 1.1 haad if (lv->status & POSTORDER_OPEN_FLAG)
1916 1.1 haad return 1; // a data structure loop has closed...
1917 1.1 haad lv->status |= POSTORDER_OPEN_FLAG;
1918 1.1 haad int r =_lv_postorder_visit(lv, baton->fn, baton->data);
1919 1.1 haad lv->status &= ~POSTORDER_OPEN_FLAG;
1920 1.1 haad lv->status |= POSTORDER_FLAG;
1921 1.1 haad return r;
1922 1.1 haad };
1923 1.1 haad
1924 1.1 haad static int _lv_each_dependency(struct logical_volume *lv,
1925 1.1 haad int (*fn)(struct logical_volume *lv, void *data),
1926 1.1 haad void *data)
1927 1.1 haad {
1928 1.1 haad int i, s;
1929 1.1 haad struct lv_segment *lvseg;
1930 1.1 haad
1931 1.1 haad struct logical_volume *deps[] = {
1932 1.1 haad lv->snapshot ? lv->snapshot->origin : 0,
1933 1.1 haad lv->snapshot ? lv->snapshot->cow : 0 };
1934 1.1 haad for (i = 0; i < sizeof(deps) / sizeof(*deps); ++i) {
1935 1.1 haad if (deps[i] && !fn(deps[i], data))
1936 1.1 haad return_0;
1937 1.1 haad }
1938 1.1 haad
1939 1.1 haad dm_list_iterate_items(lvseg, &lv->segments) {
1940 1.1 haad if (lvseg->log_lv && !fn(lvseg->log_lv, data))
1941 1.1 haad return_0;
1942 1.1 haad for (s = 0; s < lvseg->area_count; ++s) {
1943 1.1 haad if (seg_type(lvseg, s) == AREA_LV && !fn(seg_lv(lvseg,s), data))
1944 1.1 haad return_0;
1945 1.1 haad }
1946 1.1 haad }
1947 1.1 haad return 1;
1948 1.1 haad }
1949 1.1 haad
1950 1.1 haad static int _lv_postorder_cleanup(struct logical_volume *lv, void *data)
1951 1.1 haad {
1952 1.1 haad if (!(lv->status & POSTORDER_FLAG))
1953 1.1 haad return 1;
1954 1.1 haad lv->status &= ~POSTORDER_FLAG;
1955 1.1 haad
1956 1.1 haad if (!_lv_each_dependency(lv, _lv_postorder_cleanup, data))
1957 1.1 haad return_0;
1958 1.1 haad return 1;
1959 1.1 haad }
1960 1.1 haad
1961 1.1 haad static int _lv_postorder_visit(struct logical_volume *lv,
1962 1.1 haad int (*fn)(struct logical_volume *lv, void *data),
1963 1.1 haad void *data)
1964 1.1 haad {
1965 1.1 haad struct _lv_postorder_baton baton;
1966 1.1 haad int r;
1967 1.1 haad
1968 1.1 haad if (lv->status & POSTORDER_FLAG)
1969 1.1 haad return 1;
1970 1.1 haad
1971 1.1 haad baton.fn = fn;
1972 1.1 haad baton.data = data;
1973 1.1 haad r = _lv_each_dependency(lv, _lv_postorder_level, &baton);
1974 1.1.1.3 haad if (r)
1975 1.1 haad r = fn(lv, data);
1976 1.1.1.3 haad
1977 1.1 haad return r;
1978 1.1 haad }
1979 1.1 haad
1980 1.1 haad /*
1981 1.1 haad * This will walk the LV dependency graph in depth-first order and in the
1982 1.1 haad * postorder, call a callback function "fn". The void *data is passed along all
1983 1.1 haad * the calls. The callback may return zero to indicate an error and terminate
1984 1.1 haad * the depth-first walk. The error is propagated to return value of
1985 1.1 haad * _lv_postorder.
1986 1.1 haad */
1987 1.1 haad static int _lv_postorder(struct logical_volume *lv,
1988 1.1 haad int (*fn)(struct logical_volume *lv, void *data),
1989 1.1 haad void *data)
1990 1.1 haad {
1991 1.1 haad int r;
1992 1.1 haad r = _lv_postorder_visit(lv, fn, data);
1993 1.1 haad _lv_postorder_cleanup(lv, 0);
1994 1.1 haad return r;
1995 1.1 haad }
1996 1.1 haad
1997 1.1 haad struct _lv_mark_if_partial_baton {
1998 1.1 haad int partial;
1999 1.1 haad };
2000 1.1 haad
2001 1.1 haad static int _lv_mark_if_partial_collect(struct logical_volume *lv, void *data)
2002 1.1 haad {
2003 1.1 haad struct _lv_mark_if_partial_baton *baton = data;
2004 1.1 haad if (lv->status & PARTIAL_LV)
2005 1.1 haad baton->partial = 1;
2006 1.1 haad
2007 1.1 haad return 1;
2008 1.1 haad }
2009 1.1 haad
2010 1.1 haad static int _lv_mark_if_partial_single(struct logical_volume *lv, void *data)
2011 1.1 haad {
2012 1.1 haad int s;
2013 1.1 haad struct _lv_mark_if_partial_baton baton;
2014 1.1 haad struct lv_segment *lvseg;
2015 1.1 haad
2016 1.1 haad dm_list_iterate_items(lvseg, &lv->segments) {
2017 1.1 haad for (s = 0; s < lvseg->area_count; ++s) {
2018 1.1 haad if (seg_type(lvseg, s) == AREA_PV) {
2019 1.1 haad if (seg_pv(lvseg, s)->status & MISSING_PV)
2020 1.1 haad lv->status |= PARTIAL_LV;
2021 1.1 haad }
2022 1.1 haad }
2023 1.1 haad }
2024 1.1 haad
2025 1.1 haad baton.partial = 0;
2026 1.1 haad _lv_each_dependency(lv, _lv_mark_if_partial_collect, &baton);
2027 1.1 haad
2028 1.1 haad if (baton.partial)
2029 1.1 haad lv->status |= PARTIAL_LV;
2030 1.1 haad
2031 1.1 haad return 1;
2032 1.1 haad }
2033 1.1 haad
2034 1.1 haad static int _lv_mark_if_partial(struct logical_volume *lv)
2035 1.1 haad {
2036 1.1 haad return _lv_postorder(lv, _lv_mark_if_partial_single, NULL);
2037 1.1 haad }
2038 1.1 haad
2039 1.1 haad /*
2040 1.1 haad * Mark LVs with missing PVs using PARTIAL_LV status flag. The flag is
2041 1.1 haad * propagated transitively, so LVs referencing other LVs are marked
2042 1.1 haad * partial as well, if any of their referenced LVs are marked partial.
2043 1.1 haad */
2044 1.1 haad static int _vg_mark_partial_lvs(struct volume_group *vg)
2045 1.1 haad {
2046 1.1 haad struct logical_volume *lv;
2047 1.1 haad struct lv_list *lvl;
2048 1.1 haad
2049 1.1 haad dm_list_iterate_items(lvl, &vg->lvs) {
2050 1.1 haad lv = lvl->lv;
2051 1.1 haad if (!_lv_mark_if_partial(lv))
2052 1.1 haad return_0;
2053 1.1 haad }
2054 1.1 haad return 1;
2055 1.1 haad }
2056 1.1 haad
2057 1.1.1.3 haad /*
2058 1.1.1.3 haad * Be sure that all PV devices have cached read ahead in dev-cache
2059 1.1.1.3 haad * Currently it takes read_ahead from first PV segment only
2060 1.1.1.3 haad */
2061 1.1.1.3 haad static int _lv_read_ahead_single(struct logical_volume *lv, void *data)
2062 1.1.1.3 haad {
2063 1.1.1.3 haad struct lv_segment *seg = first_seg(lv);
2064 1.1.1.3 haad uint32_t seg_read_ahead = 0, *read_ahead = data;
2065 1.1.1.3 haad
2066 1.1.1.3 haad if (seg && seg->area_count && seg_type(seg, 0) == AREA_PV)
2067 1.1.1.3 haad dev_get_read_ahead(seg_pv(seg, 0)->dev, &seg_read_ahead);
2068 1.1.1.3 haad
2069 1.1.1.3 haad if (seg_read_ahead > *read_ahead)
2070 1.1.1.3 haad *read_ahead = seg_read_ahead;
2071 1.1.1.3 haad
2072 1.1.1.3 haad return 1;
2073 1.1.1.3 haad }
2074 1.1.1.3 haad
2075 1.1.1.3 haad /*
2076 1.1.1.3 haad * Calculate readahead for logical volume from underlying PV devices.
2077 1.1.1.3 haad * If read_ahead is NULL, only ensure that readahead of PVs are preloaded
2078 1.1.1.3 haad * into PV struct device in dev cache.
2079 1.1.1.3 haad */
2080 1.1.1.3 haad void lv_calculate_readahead(const struct logical_volume *lv, uint32_t *read_ahead)
2081 1.1.1.3 haad {
2082 1.1.1.3 haad uint32_t _read_ahead = 0;
2083 1.1.1.3 haad
2084 1.1.1.3 haad if (lv->read_ahead == DM_READ_AHEAD_AUTO)
2085 1.1.1.3 haad _lv_postorder((struct logical_volume *)lv, _lv_read_ahead_single, &_read_ahead);
2086 1.1.1.3 haad
2087 1.1.1.3 haad if (read_ahead) {
2088 1.1.1.3 haad log_debug("Calculated readahead of LV %s is %u", lv->name, _read_ahead);
2089 1.1.1.3 haad *read_ahead = _read_ahead;
2090 1.1.1.3 haad }
2091 1.1.1.3 haad }
2092 1.1.1.3 haad
2093 1.1 haad int vg_validate(struct volume_group *vg)
2094 1.1 haad {
2095 1.1 haad struct pv_list *pvl, *pvl2;
2096 1.1 haad struct lv_list *lvl, *lvl2;
2097 1.1 haad char uuid[64] __attribute((aligned(8)));
2098 1.1 haad int r = 1;
2099 1.1.1.3 haad uint32_t hidden_lv_count = 0;
2100 1.1 haad
2101 1.1 haad /* FIXME Also check there's no data/metadata overlap */
2102 1.1 haad
2103 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
2104 1.1 haad dm_list_iterate_items(pvl2, &vg->pvs) {
2105 1.1 haad if (pvl == pvl2)
2106 1.1 haad break;
2107 1.1 haad if (id_equal(&pvl->pv->id,
2108 1.1 haad &pvl2->pv->id)) {
2109 1.1 haad if (!id_write_format(&pvl->pv->id, uuid,
2110 1.1 haad sizeof(uuid)))
2111 1.1 haad stack;
2112 1.1 haad log_error("Internal error: Duplicate PV id "
2113 1.1 haad "%s detected for %s in %s.",
2114 1.1 haad uuid, pv_dev_name(pvl->pv),
2115 1.1 haad vg->name);
2116 1.1 haad r = 0;
2117 1.1 haad }
2118 1.1 haad }
2119 1.1 haad
2120 1.1 haad if (strcmp(pvl->pv->vg_name, vg->name)) {
2121 1.1 haad log_error("Internal error: VG name for PV %s is corrupted",
2122 1.1 haad pv_dev_name(pvl->pv));
2123 1.1 haad r = 0;
2124 1.1 haad }
2125 1.1 haad }
2126 1.1 haad
2127 1.1 haad if (!check_pv_segments(vg)) {
2128 1.1 haad log_error("Internal error: PV segments corrupted in %s.",
2129 1.1 haad vg->name);
2130 1.1 haad r = 0;
2131 1.1 haad }
2132 1.1 haad
2133 1.1.1.3 haad /*
2134 1.1.1.3 haad * Count all non-snapshot invisible LVs
2135 1.1.1.3 haad */
2136 1.1.1.3 haad dm_list_iterate_items(lvl, &vg->lvs) {
2137 1.1.1.3 haad if (lvl->lv->status & VISIBLE_LV)
2138 1.1.1.3 haad continue;
2139 1.1.1.3 haad
2140 1.1.1.3 haad /* snapshots */
2141 1.1.1.3 haad if (lv_is_cow(lvl->lv))
2142 1.1.1.3 haad continue;
2143 1.1.1.3 haad
2144 1.1.1.3 haad /* virtual origins are always hidden */
2145 1.1.1.3 haad if (lv_is_origin(lvl->lv) && !lv_is_virtual_origin(lvl->lv))
2146 1.1.1.3 haad continue;
2147 1.1.1.3 haad
2148 1.1.1.3 haad /* count other non-snapshot invisible volumes */
2149 1.1.1.3 haad hidden_lv_count++;
2150 1.1.1.3 haad
2151 1.1.1.3 haad /*
2152 1.1.1.3 haad * FIXME: add check for unreferenced invisible LVs
2153 1.1.1.3 haad * - snapshot cow & origin
2154 1.1.1.3 haad * - mirror log & images
2155 1.1.1.3 haad * - mirror conversion volumes (_mimagetmp*)
2156 1.1.1.3 haad */
2157 1.1.1.3 haad }
2158 1.1.1.3 haad
2159 1.1.1.3 haad /*
2160 1.1.1.3 haad * all volumes = visible LVs + snapshot_cows + invisible LVs
2161 1.1.1.3 haad */
2162 1.1.1.3 haad if (((uint32_t) dm_list_size(&vg->lvs)) !=
2163 1.1.1.3 haad vg_visible_lvs(vg) + snapshot_count(vg) + hidden_lv_count) {
2164 1.1 haad log_error("Internal error: #internal LVs (%u) != #LVs (%"
2165 1.1.1.3 haad PRIu32 ") + #snapshots (%" PRIu32 ") + #internal LVs %u in VG %s",
2166 1.1.1.3 haad dm_list_size(&vg->lvs), vg_visible_lvs(vg),
2167 1.1.1.3 haad snapshot_count(vg), hidden_lv_count, vg->name);
2168 1.1 haad r = 0;
2169 1.1 haad }
2170 1.1 haad
2171 1.1 haad dm_list_iterate_items(lvl, &vg->lvs) {
2172 1.1 haad dm_list_iterate_items(lvl2, &vg->lvs) {
2173 1.1 haad if (lvl == lvl2)
2174 1.1 haad break;
2175 1.1 haad if (!strcmp(lvl->lv->name, lvl2->lv->name)) {
2176 1.1 haad log_error("Internal error: Duplicate LV name "
2177 1.1 haad "%s detected in %s.", lvl->lv->name,
2178 1.1 haad vg->name);
2179 1.1 haad r = 0;
2180 1.1 haad }
2181 1.1 haad if (id_equal(&lvl->lv->lvid.id[1],
2182 1.1 haad &lvl2->lv->lvid.id[1])) {
2183 1.1 haad if (!id_write_format(&lvl->lv->lvid.id[1], uuid,
2184 1.1 haad sizeof(uuid)))
2185 1.1 haad stack;
2186 1.1 haad log_error("Internal error: Duplicate LV id "
2187 1.1 haad "%s detected for %s and %s in %s.",
2188 1.1 haad uuid, lvl->lv->name, lvl2->lv->name,
2189 1.1 haad vg->name);
2190 1.1 haad r = 0;
2191 1.1 haad }
2192 1.1 haad }
2193 1.1 haad }
2194 1.1 haad
2195 1.1 haad dm_list_iterate_items(lvl, &vg->lvs) {
2196 1.1 haad if (!check_lv_segments(lvl->lv, 1)) {
2197 1.1 haad log_error("Internal error: LV segments corrupted in %s.",
2198 1.1 haad lvl->lv->name);
2199 1.1 haad r = 0;
2200 1.1 haad }
2201 1.1 haad }
2202 1.1 haad
2203 1.1 haad if (!(vg->fid->fmt->features & FMT_UNLIMITED_VOLS) &&
2204 1.1 haad (!vg->max_lv || !vg->max_pv)) {
2205 1.1 haad log_error("Internal error: Volume group %s has limited PV/LV count"
2206 1.1 haad " but limit is not set.", vg->name);
2207 1.1 haad r = 0;
2208 1.1 haad }
2209 1.1 haad
2210 1.1.1.3 haad if (vg_max_lv_reached(vg))
2211 1.1.1.3 haad stack;
2212 1.1.1.3 haad
2213 1.1 haad return r;
2214 1.1 haad }
2215 1.1 haad
2216 1.1 haad /*
2217 1.1 haad * After vg_write() returns success,
2218 1.1 haad * caller MUST call either vg_commit() or vg_revert()
2219 1.1 haad */
2220 1.1 haad int vg_write(struct volume_group *vg)
2221 1.1 haad {
2222 1.1 haad struct dm_list *mdah;
2223 1.1 haad struct metadata_area *mda;
2224 1.1 haad
2225 1.1 haad if (!vg_validate(vg))
2226 1.1 haad return_0;
2227 1.1 haad
2228 1.1 haad if (vg->status & PARTIAL_VG) {
2229 1.1 haad log_error("Cannot update partial volume group %s.", vg->name);
2230 1.1 haad return 0;
2231 1.1 haad }
2232 1.1 haad
2233 1.1 haad if (vg_missing_pv_count(vg) && !vg->cmd->handles_missing_pvs) {
2234 1.1 haad log_error("Cannot update volume group %s while physical "
2235 1.1 haad "volumes are missing.", vg->name);
2236 1.1 haad return 0;
2237 1.1 haad }
2238 1.1 haad
2239 1.1.1.3 haad if (vg_has_unknown_segments(vg) && !vg->cmd->handles_unknown_segments) {
2240 1.1.1.3 haad log_error("Cannot update volume group %s with unknown segments in it!",
2241 1.1.1.3 haad vg->name);
2242 1.1.1.3 haad return 0;
2243 1.1.1.3 haad }
2244 1.1.1.3 haad
2245 1.1.1.3 haad
2246 1.1 haad if (dm_list_empty(&vg->fid->metadata_areas)) {
2247 1.1 haad log_error("Aborting vg_write: No metadata areas to write to!");
2248 1.1 haad return 0;
2249 1.1 haad }
2250 1.1 haad
2251 1.1 haad if (!drop_cached_metadata(vg)) {
2252 1.1 haad log_error("Unable to drop cached metadata for VG %s.", vg->name);
2253 1.1 haad return 0;
2254 1.1 haad }
2255 1.1 haad
2256 1.1 haad vg->seqno++;
2257 1.1 haad
2258 1.1 haad /* Write to each copy of the metadata area */
2259 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
2260 1.1 haad if (!mda->ops->vg_write) {
2261 1.1 haad log_error("Format does not support writing volume"
2262 1.1 haad "group metadata areas");
2263 1.1 haad /* Revert */
2264 1.1 haad dm_list_uniterate(mdah, &vg->fid->metadata_areas, &mda->list) {
2265 1.1 haad mda = dm_list_item(mdah, struct metadata_area);
2266 1.1 haad
2267 1.1 haad if (mda->ops->vg_revert &&
2268 1.1 haad !mda->ops->vg_revert(vg->fid, vg, mda)) {
2269 1.1 haad stack;
2270 1.1 haad }
2271 1.1 haad }
2272 1.1 haad return 0;
2273 1.1 haad }
2274 1.1 haad if (!mda->ops->vg_write(vg->fid, vg, mda)) {
2275 1.1 haad stack;
2276 1.1 haad /* Revert */
2277 1.1 haad dm_list_uniterate(mdah, &vg->fid->metadata_areas, &mda->list) {
2278 1.1 haad mda = dm_list_item(mdah, struct metadata_area);
2279 1.1 haad
2280 1.1 haad if (mda->ops->vg_revert &&
2281 1.1 haad !mda->ops->vg_revert(vg->fid, vg, mda)) {
2282 1.1 haad stack;
2283 1.1 haad }
2284 1.1 haad }
2285 1.1 haad return 0;
2286 1.1 haad }
2287 1.1 haad }
2288 1.1 haad
2289 1.1 haad /* Now pre-commit each copy of the new metadata */
2290 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
2291 1.1 haad if (mda->ops->vg_precommit &&
2292 1.1 haad !mda->ops->vg_precommit(vg->fid, vg, mda)) {
2293 1.1 haad stack;
2294 1.1 haad /* Revert */
2295 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
2296 1.1 haad if (mda->ops->vg_revert &&
2297 1.1 haad !mda->ops->vg_revert(vg->fid, vg, mda)) {
2298 1.1 haad stack;
2299 1.1 haad }
2300 1.1 haad }
2301 1.1 haad return 0;
2302 1.1 haad }
2303 1.1 haad }
2304 1.1 haad
2305 1.1 haad return 1;
2306 1.1 haad }
2307 1.1 haad
2308 1.1 haad /* Commit pending changes */
2309 1.1 haad int vg_commit(struct volume_group *vg)
2310 1.1 haad {
2311 1.1 haad struct metadata_area *mda;
2312 1.1 haad int cache_updated = 0;
2313 1.1 haad int failed = 0;
2314 1.1 haad
2315 1.1 haad if (!vgname_is_locked(vg->name)) {
2316 1.1 haad log_error("Internal error: Attempt to write new VG metadata "
2317 1.1 haad "without locking %s", vg->name);
2318 1.1 haad return cache_updated;
2319 1.1 haad }
2320 1.1 haad
2321 1.1 haad /* Commit to each copy of the metadata area */
2322 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
2323 1.1 haad failed = 0;
2324 1.1 haad if (mda->ops->vg_commit &&
2325 1.1 haad !mda->ops->vg_commit(vg->fid, vg, mda)) {
2326 1.1 haad stack;
2327 1.1 haad failed = 1;
2328 1.1 haad }
2329 1.1 haad /* Update cache first time we succeed */
2330 1.1 haad if (!failed && !cache_updated) {
2331 1.1 haad lvmcache_update_vg(vg, 0);
2332 1.1 haad cache_updated = 1;
2333 1.1 haad }
2334 1.1 haad }
2335 1.1 haad
2336 1.1 haad /* If update failed, remove any cached precommitted metadata. */
2337 1.1 haad if (!cache_updated && !drop_cached_metadata(vg))
2338 1.1 haad log_error("Attempt to drop cached metadata failed "
2339 1.1 haad "after commit for VG %s.", vg->name);
2340 1.1 haad
2341 1.1 haad /* If at least one mda commit succeeded, it was committed */
2342 1.1 haad return cache_updated;
2343 1.1 haad }
2344 1.1 haad
2345 1.1 haad /* Don't commit any pending changes */
2346 1.1 haad int vg_revert(struct volume_group *vg)
2347 1.1 haad {
2348 1.1 haad struct metadata_area *mda;
2349 1.1 haad
2350 1.1 haad dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
2351 1.1 haad if (mda->ops->vg_revert &&
2352 1.1 haad !mda->ops->vg_revert(vg->fid, vg, mda)) {
2353 1.1 haad stack;
2354 1.1 haad }
2355 1.1 haad }
2356 1.1 haad
2357 1.1 haad if (!drop_cached_metadata(vg))
2358 1.1 haad log_error("Attempt to drop cached metadata failed "
2359 1.1 haad "after reverted update for VG %s.", vg->name);
2360 1.1 haad
2361 1.1 haad return 1;
2362 1.1 haad }
2363 1.1 haad
2364 1.1 haad /* Make orphan PVs look like a VG */
2365 1.1 haad static struct volume_group *_vg_read_orphans(struct cmd_context *cmd,
2366 1.1 haad const char *orphan_vgname)
2367 1.1 haad {
2368 1.1 haad struct lvmcache_vginfo *vginfo;
2369 1.1 haad struct lvmcache_info *info;
2370 1.1 haad struct pv_list *pvl;
2371 1.1 haad struct volume_group *vg;
2372 1.1 haad struct physical_volume *pv;
2373 1.1.1.3 haad struct dm_pool *mem;
2374 1.1 haad
2375 1.1 haad lvmcache_label_scan(cmd, 0);
2376 1.1 haad
2377 1.1 haad if (!(vginfo = vginfo_from_vgname(orphan_vgname, NULL)))
2378 1.1 haad return_NULL;
2379 1.1 haad
2380 1.1.1.3 haad if (!(mem = dm_pool_create("vg_read orphan", VG_MEMPOOL_CHUNK)))
2381 1.1.1.3 haad return_NULL;
2382 1.1.1.3 haad
2383 1.1.1.3 haad if (!(vg = dm_pool_zalloc(mem, sizeof(*vg)))) {
2384 1.1 haad log_error("vg allocation failed");
2385 1.1 haad return NULL;
2386 1.1 haad }
2387 1.1 haad dm_list_init(&vg->pvs);
2388 1.1 haad dm_list_init(&vg->lvs);
2389 1.1 haad dm_list_init(&vg->tags);
2390 1.1.1.3 haad dm_list_init(&vg->removed_pvs);
2391 1.1.1.3 haad vg->vgmem = mem;
2392 1.1 haad vg->cmd = cmd;
2393 1.1.1.3 haad if (!(vg->name = dm_pool_strdup(mem, orphan_vgname))) {
2394 1.1 haad log_error("vg name allocation failed");
2395 1.1.1.3 haad goto bad;
2396 1.1 haad }
2397 1.1 haad
2398 1.1 haad /* create format instance with appropriate metadata area */
2399 1.1 haad if (!(vg->fid = vginfo->fmt->ops->create_instance(vginfo->fmt,
2400 1.1 haad orphan_vgname, NULL,
2401 1.1 haad NULL))) {
2402 1.1 haad log_error("Failed to create format instance");
2403 1.1.1.3 haad goto bad;
2404 1.1 haad }
2405 1.1 haad
2406 1.1 haad dm_list_iterate_items(info, &vginfo->infos) {
2407 1.1.1.3 haad if (!(pv = _pv_read(cmd, mem, dev_name(info->dev), NULL, NULL, 1, 0))) {
2408 1.1 haad continue;
2409 1.1 haad }
2410 1.1.1.3 haad if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
2411 1.1 haad log_error("pv_list allocation failed");
2412 1.1.1.3 haad goto bad;
2413 1.1 haad }
2414 1.1 haad pvl->pv = pv;
2415 1.1 haad dm_list_add(&vg->pvs, &pvl->list);
2416 1.1 haad vg->pv_count++;
2417 1.1 haad }
2418 1.1 haad
2419 1.1 haad return vg;
2420 1.1.1.3 haad bad:
2421 1.1.1.3 haad dm_pool_destroy(mem);
2422 1.1.1.3 haad return NULL;
2423 1.1 haad }
2424 1.1 haad
2425 1.1.1.3 haad static int _update_pv_list(struct dm_pool *pvmem, struct dm_list *all_pvs, struct volume_group *vg)
2426 1.1 haad {
2427 1.1 haad struct pv_list *pvl, *pvl2;
2428 1.1 haad
2429 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
2430 1.1 haad dm_list_iterate_items(pvl2, all_pvs) {
2431 1.1 haad if (pvl->pv->dev == pvl2->pv->dev)
2432 1.1 haad goto next_pv;
2433 1.1 haad }
2434 1.1.1.3 haad
2435 1.1.1.3 haad /*
2436 1.1.1.3 haad * PV is not on list so add it.
2437 1.1.1.3 haad */
2438 1.1.1.3 haad if (!(pvl2 = _copy_pvl(pvmem, pvl))) {
2439 1.1 haad log_error("pv_list allocation for '%s' failed",
2440 1.1 haad pv_dev_name(pvl->pv));
2441 1.1 haad return 0;
2442 1.1 haad }
2443 1.1 haad dm_list_add(all_pvs, &pvl2->list);
2444 1.1 haad next_pv:
2445 1.1 haad ;
2446 1.1 haad }
2447 1.1 haad
2448 1.1 haad return 1;
2449 1.1 haad }
2450 1.1 haad
2451 1.1.1.3 haad int vg_missing_pv_count(const struct volume_group *vg)
2452 1.1 haad {
2453 1.1 haad int ret = 0;
2454 1.1 haad struct pv_list *pvl;
2455 1.1 haad dm_list_iterate_items(pvl, &vg->pvs) {
2456 1.1 haad if (pvl->pv->status & MISSING_PV)
2457 1.1 haad ++ ret;
2458 1.1 haad }
2459 1.1 haad return ret;
2460 1.1 haad }
2461 1.1 haad
2462 1.1.1.3 haad /* Caller sets consistent to 1 if it's safe for vg_read_internal to correct
2463 1.1 haad * inconsistent metadata on disk (i.e. the VG write lock is held).
2464 1.1 haad * This guarantees only consistent metadata is returned.
2465 1.1 haad * If consistent is 0, caller must check whether consistent == 1 on return
2466 1.1 haad * and take appropriate action if it isn't (e.g. abort; get write lock
2467 1.1.1.3 haad * and call vg_read_internal again).
2468 1.1 haad *
2469 1.1 haad * If precommitted is set, use precommitted metadata if present.
2470 1.1 haad *
2471 1.1 haad * Either of vgname or vgid may be NULL.
2472 1.1 haad */
2473 1.1 haad static struct volume_group *_vg_read(struct cmd_context *cmd,
2474 1.1 haad const char *vgname,
2475 1.1 haad const char *vgid,
2476 1.1 haad int *consistent, unsigned precommitted)
2477 1.1 haad {
2478 1.1 haad struct format_instance *fid;
2479 1.1 haad const struct format_type *fmt;
2480 1.1 haad struct volume_group *vg, *correct_vg = NULL;
2481 1.1 haad struct metadata_area *mda;
2482 1.1 haad struct lvmcache_info *info;
2483 1.1 haad int inconsistent = 0;
2484 1.1 haad int inconsistent_vgid = 0;
2485 1.1 haad int inconsistent_pvs = 0;
2486 1.1 haad unsigned use_precommitted = precommitted;
2487 1.1.1.3 haad unsigned saved_handles_missing_pvs = cmd->handles_missing_pvs;
2488 1.1 haad struct dm_list *pvids;
2489 1.1 haad struct pv_list *pvl, *pvl2;
2490 1.1 haad struct dm_list all_pvs;
2491 1.1 haad char uuid[64] __attribute((aligned(8)));
2492 1.1 haad
2493 1.1 haad if (is_orphan_vg(vgname)) {
2494 1.1 haad if (use_precommitted) {
2495 1.1.1.3 haad log_error("Internal error: vg_read_internal requires vgname "
2496 1.1 haad "with pre-commit.");
2497 1.1 haad return NULL;
2498 1.1 haad }
2499 1.1 haad *consistent = 1;
2500 1.1 haad return _vg_read_orphans(cmd, vgname);
2501 1.1 haad }
2502 1.1 haad
2503 1.1 haad if ((correct_vg = lvmcache_get_vg(vgid, precommitted))) {
2504 1.1 haad if (vg_missing_pv_count(correct_vg)) {
2505 1.1 haad log_verbose("There are %d physical volumes missing.",
2506 1.1 haad vg_missing_pv_count(correct_vg));
2507 1.1 haad _vg_mark_partial_lvs(correct_vg);
2508 1.1 haad }
2509 1.1 haad *consistent = 1;
2510 1.1 haad return correct_vg;
2511 1.1 haad }
2512 1.1 haad
2513 1.1 haad /* Find the vgname in the cache */
2514 1.1 haad /* If it's not there we must do full scan to be completely sure */
2515 1.1 haad if (!(fmt = fmt_from_vgname(vgname, vgid))) {
2516 1.1 haad lvmcache_label_scan(cmd, 0);
2517 1.1 haad if (!(fmt = fmt_from_vgname(vgname, vgid))) {
2518 1.1 haad if (memlock())
2519 1.1 haad return_NULL;
2520 1.1 haad lvmcache_label_scan(cmd, 2);
2521 1.1 haad if (!(fmt = fmt_from_vgname(vgname, vgid)))
2522 1.1 haad return_NULL;
2523 1.1 haad }
2524 1.1 haad }
2525 1.1 haad
2526 1.1 haad /* Now determine the correct vgname if none was supplied */
2527 1.1 haad if (!vgname && !(vgname = vgname_from_vgid(cmd->mem, vgid)))
2528 1.1 haad return_NULL;
2529 1.1 haad
2530 1.1 haad if (use_precommitted && !(fmt->features & FMT_PRECOMMIT))
2531 1.1 haad use_precommitted = 0;
2532 1.1 haad
2533 1.1 haad /* create format instance with appropriate metadata area */
2534 1.1 haad if (!(fid = fmt->ops->create_instance(fmt, vgname, vgid, NULL))) {
2535 1.1 haad log_error("Failed to create format instance");
2536 1.1 haad return NULL;
2537 1.1 haad }
2538 1.1 haad
2539 1.1 haad /* Store pvids for later so we can check if any are missing */
2540 1.1 haad if (!(pvids = lvmcache_get_pvids(cmd, vgname, vgid)))
2541 1.1 haad return_NULL;
2542 1.1 haad
2543 1.1 haad /* Ensure contents of all metadata areas match - else do recovery */
2544 1.1 haad dm_list_iterate_items(mda, &fid->metadata_areas) {
2545 1.1 haad if ((use_precommitted &&
2546 1.1 haad !(vg = mda->ops->vg_read_precommit(fid, vgname, mda))) ||
2547 1.1 haad (!use_precommitted &&
2548 1.1 haad !(vg = mda->ops->vg_read(fid, vgname, mda)))) {
2549 1.1 haad inconsistent = 1;
2550 1.1.1.3 haad vg_release(vg);
2551 1.1 haad continue;
2552 1.1 haad }
2553 1.1 haad if (!correct_vg) {
2554 1.1 haad correct_vg = vg;
2555 1.1 haad continue;
2556 1.1 haad }
2557 1.1.1.3 haad
2558 1.1 haad /* FIXME Also ensure contents same - checksum compare? */
2559 1.1 haad if (correct_vg->seqno != vg->seqno) {
2560 1.1 haad inconsistent = 1;
2561 1.1.1.3 haad if (vg->seqno > correct_vg->seqno) {
2562 1.1.1.3 haad vg_release(correct_vg);
2563 1.1 haad correct_vg = vg;
2564 1.1.1.3 haad }
2565 1.1 haad }
2566 1.1.1.3 haad
2567 1.1.1.3 haad if (vg != correct_vg)
2568 1.1.1.3 haad vg_release(vg);
2569 1.1 haad }
2570 1.1 haad
2571 1.1 haad /* Ensure every PV in the VG was in the cache */
2572 1.1 haad if (correct_vg) {
2573 1.1 haad /*
2574 1.1 haad * If the VG has PVs without mdas, they may still be
2575 1.1 haad * orphans in the cache: update the cache state here.
2576 1.1 haad */
2577 1.1 haad if (!inconsistent &&
2578 1.1 haad dm_list_size(&correct_vg->pvs) > dm_list_size(pvids)) {
2579 1.1 haad dm_list_iterate_items(pvl, &correct_vg->pvs) {
2580 1.1 haad if (!pvl->pv->dev) {
2581 1.1 haad inconsistent_pvs = 1;
2582 1.1 haad break;
2583 1.1 haad }
2584 1.1 haad
2585 1.1 haad if (str_list_match_item(pvids, pvl->pv->dev->pvid))
2586 1.1 haad continue;
2587 1.1 haad
2588 1.1 haad /*
2589 1.1 haad * PV not marked as belonging to this VG in cache.
2590 1.1 haad * Check it's an orphan without metadata area.
2591 1.1 haad */
2592 1.1 haad if (!(info = info_from_pvid(pvl->pv->dev->pvid, 1)) ||
2593 1.1 haad !info->vginfo || !is_orphan_vg(info->vginfo->vgname) ||
2594 1.1 haad dm_list_size(&info->mdas)) {
2595 1.1 haad inconsistent_pvs = 1;
2596 1.1 haad break;
2597 1.1 haad }
2598 1.1 haad }
2599 1.1 haad
2600 1.1 haad /* If the check passed, let's update VG and recalculate pvids */
2601 1.1 haad if (!inconsistent_pvs) {
2602 1.1 haad log_debug("Updating cache for PVs without mdas "
2603 1.1 haad "in VG %s.", vgname);
2604 1.1 haad lvmcache_update_vg(correct_vg, use_precommitted);
2605 1.1 haad
2606 1.1 haad if (!(pvids = lvmcache_get_pvids(cmd, vgname, vgid)))
2607 1.1 haad return_NULL;
2608 1.1 haad }
2609 1.1 haad }
2610 1.1 haad
2611 1.1 haad if (dm_list_size(&correct_vg->pvs) != dm_list_size(pvids)
2612 1.1 haad + vg_missing_pv_count(correct_vg)) {
2613 1.1 haad log_debug("Cached VG %s had incorrect PV list",
2614 1.1 haad vgname);
2615 1.1 haad
2616 1.1 haad if (memlock())
2617 1.1 haad inconsistent = 1;
2618 1.1.1.3 haad else {
2619 1.1.1.3 haad vg_release(correct_vg);
2620 1.1 haad correct_vg = NULL;
2621 1.1.1.3 haad }
2622 1.1 haad } else dm_list_iterate_items(pvl, &correct_vg->pvs) {
2623 1.1 haad if (pvl->pv->status & MISSING_PV)
2624 1.1 haad continue;
2625 1.1 haad if (!str_list_match_item(pvids, pvl->pv->dev->pvid)) {
2626 1.1 haad log_debug("Cached VG %s had incorrect PV list",
2627 1.1 haad vgname);
2628 1.1.1.3 haad vg_release(correct_vg);
2629 1.1 haad correct_vg = NULL;
2630 1.1 haad break;
2631 1.1 haad }
2632 1.1 haad }
2633 1.1 haad }
2634 1.1 haad
2635 1.1 haad dm_list_init(&all_pvs);
2636 1.1 haad
2637 1.1 haad /* Failed to find VG where we expected it - full scan and retry */
2638 1.1 haad if (!correct_vg) {
2639 1.1 haad inconsistent = 0;
2640 1.1 haad
2641 1.1 haad if (memlock())
2642 1.1 haad return_NULL;
2643 1.1 haad lvmcache_label_scan(cmd, 2);
2644 1.1 haad if (!(fmt = fmt_from_vgname(vgname, vgid)))
2645 1.1 haad return_NULL;
2646 1.1 haad
2647 1.1 haad if (precommitted && !(fmt->features & FMT_PRECOMMIT))
2648 1.1 haad use_precommitted = 0;
2649 1.1 haad
2650 1.1 haad /* create format instance with appropriate metadata area */
2651 1.1 haad if (!(fid = fmt->ops->create_instance(fmt, vgname, vgid, NULL))) {
2652 1.1 haad log_error("Failed to create format instance");
2653 1.1 haad return NULL;
2654 1.1 haad }
2655 1.1 haad
2656 1.1 haad /* Ensure contents of all metadata areas match - else recover */
2657 1.1 haad dm_list_iterate_items(mda, &fid->metadata_areas) {
2658 1.1 haad if ((use_precommitted &&
2659 1.1 haad !(vg = mda->ops->vg_read_precommit(fid, vgname,
2660 1.1 haad mda))) ||
2661 1.1 haad (!use_precommitted &&
2662 1.1 haad !(vg = mda->ops->vg_read(fid, vgname, mda)))) {
2663 1.1 haad inconsistent = 1;
2664 1.1 haad continue;
2665 1.1 haad }
2666 1.1 haad if (!correct_vg) {
2667 1.1 haad correct_vg = vg;
2668 1.1.1.3 haad if (!_update_pv_list(cmd->mem, &all_pvs, correct_vg)) {
2669 1.1.1.3 haad vg_release(vg);
2670 1.1 haad return_NULL;
2671 1.1.1.3 haad }
2672 1.1 haad continue;
2673 1.1 haad }
2674 1.1 haad
2675 1.1 haad if (strncmp((char *)vg->id.uuid,
2676 1.1 haad (char *)correct_vg->id.uuid, ID_LEN)) {
2677 1.1 haad inconsistent = 1;
2678 1.1 haad inconsistent_vgid = 1;
2679 1.1 haad }
2680 1.1 haad
2681 1.1 haad /* FIXME Also ensure contents same - checksums same? */
2682 1.1 haad if (correct_vg->seqno != vg->seqno) {
2683 1.1 haad inconsistent = 1;
2684 1.1.1.3 haad if (!_update_pv_list(cmd->mem, &all_pvs, vg)) {
2685 1.1.1.3 haad vg_release(vg);
2686 1.1.1.3 haad vg_release(correct_vg);
2687 1.1 haad return_NULL;
2688 1.1.1.3 haad }
2689 1.1.1.3 haad if (vg->seqno > correct_vg->seqno) {
2690 1.1.1.3 haad vg_release(correct_vg);
2691 1.1 haad correct_vg = vg;
2692 1.1.1.3 haad }
2693 1.1 haad }
2694 1.1.1.3 haad
2695 1.1.1.3 haad if (vg != correct_vg)
2696 1.1.1.3 haad vg_release(vg);
2697 1.1 haad }
2698 1.1 haad
2699 1.1 haad /* Give up looking */
2700 1.1 haad if (!correct_vg)
2701 1.1 haad return_NULL;
2702 1.1 haad }
2703 1.1 haad
2704 1.1 haad lvmcache_update_vg(correct_vg, use_precommitted);
2705 1.1 haad
2706 1.1 haad if (inconsistent) {
2707 1.1 haad /* FIXME Test should be if we're *using* precommitted metadata not if we were searching for it */
2708 1.1 haad if (use_precommitted) {
2709 1.1 haad log_error("Inconsistent pre-commit metadata copies "
2710 1.1 haad "for volume group %s", vgname);
2711 1.1.1.3 haad vg_release(correct_vg);
2712 1.1 haad return NULL;
2713 1.1 haad }
2714 1.1 haad
2715 1.1 haad if (!*consistent)
2716 1.1 haad return correct_vg;
2717 1.1 haad
2718 1.1 haad /* Don't touch if vgids didn't match */
2719 1.1 haad if (inconsistent_vgid) {
2720 1.1 haad log_error("Inconsistent metadata UUIDs found for "
2721 1.1 haad "volume group %s", vgname);
2722 1.1 haad *consistent = 0;
2723 1.1 haad return correct_vg;
2724 1.1 haad }
2725 1.1 haad
2726 1.1 haad log_warn("WARNING: Inconsistent metadata found for VG %s - updating "
2727 1.1 haad "to use version %u", vgname, correct_vg->seqno);
2728 1.1 haad
2729 1.1.1.3 haad cmd->handles_missing_pvs = 1;
2730 1.1 haad if (!vg_write(correct_vg)) {
2731 1.1 haad log_error("Automatic metadata correction failed");
2732 1.1.1.3 haad vg_release(correct_vg);
2733 1.1.1.3 haad cmd->handles_missing_pvs = saved_handles_missing_pvs;
2734 1.1 haad return NULL;
2735 1.1 haad }
2736 1.1.1.3 haad cmd->handles_missing_pvs = saved_handles_missing_pvs;
2737 1.1 haad
2738 1.1 haad if (!vg_commit(correct_vg)) {
2739 1.1 haad log_error("Automatic metadata correction commit "
2740 1.1 haad "failed");
2741 1.1.1.3 haad vg_release(correct_vg);
2742 1.1 haad return NULL;
2743 1.1 haad }
2744 1.1 haad
2745 1.1 haad dm_list_iterate_items(pvl, &all_pvs) {
2746 1.1 haad dm_list_iterate_items(pvl2, &correct_vg->pvs) {
2747 1.1 haad if (pvl->pv->dev == pvl2->pv->dev)
2748 1.1 haad goto next_pv;
2749 1.1 haad }
2750 1.1.1.3 haad if (!id_write_format(&pvl->pv->id, uuid, sizeof(uuid))) {
2751 1.1.1.3 haad vg_release(correct_vg);
2752 1.1 haad return_NULL;
2753 1.1.1.3 haad }
2754 1.1 haad log_error("Removing PV %s (%s) that no longer belongs to VG %s",
2755 1.1 haad pv_dev_name(pvl->pv), uuid, correct_vg->name);
2756 1.1.1.3 haad if (!pv_write_orphan(cmd, pvl->pv)) {
2757 1.1.1.3 haad vg_release(correct_vg);
2758 1.1 haad return_NULL;
2759 1.1.1.3 haad }
2760 1.1 haad next_pv:
2761 1.1 haad ;
2762 1.1 haad }
2763 1.1 haad }
2764 1.1 haad
2765 1.1 haad if (vg_missing_pv_count(correct_vg)) {
2766 1.1 haad log_verbose("There are %d physical volumes missing.",
2767 1.1 haad vg_missing_pv_count(correct_vg));
2768 1.1 haad _vg_mark_partial_lvs(correct_vg);
2769 1.1 haad }
2770 1.1 haad
2771 1.1 haad if ((correct_vg->status & PVMOVE) && !pvmove_mode()) {
2772 1.1 haad log_error("WARNING: Interrupted pvmove detected in "
2773 1.1 haad "volume group %s", correct_vg->name);
2774 1.1 haad log_error("Please restore the metadata by running "
2775 1.1 haad "vgcfgrestore.");
2776 1.1.1.3 haad vg_release(correct_vg);
2777 1.1 haad return NULL;
2778 1.1 haad }
2779 1.1 haad
2780 1.1 haad *consistent = 1;
2781 1.1 haad return correct_vg;
2782 1.1 haad }
2783 1.1 haad
2784 1.1.1.3 haad struct volume_group *vg_read_internal(struct cmd_context *cmd, const char *vgname,
2785 1.1 haad const char *vgid, int *consistent)
2786 1.1 haad {
2787 1.1 haad struct volume_group *vg;
2788 1.1 haad struct lv_list *lvl;
2789 1.1 haad
2790 1.1 haad if (!(vg = _vg_read(cmd, vgname, vgid, consistent, 0)))
2791 1.1 haad return NULL;
2792 1.1 haad
2793 1.1 haad if (!check_pv_segments(vg)) {
2794 1.1 haad log_error("Internal error: PV segments corrupted in %s.",
2795 1.1 haad vg->name);
2796 1.1.1.3 haad vg_release(vg);
2797 1.1 haad return NULL;
2798 1.1 haad }
2799 1.1 haad
2800 1.1 haad dm_list_iterate_items(lvl, &vg->lvs) {
2801 1.1 haad if (!check_lv_segments(lvl->lv, 1)) {
2802 1.1 haad log_error("Internal error: LV segments corrupted in %s.",
2803 1.1 haad lvl->lv->name);
2804 1.1.1.3 haad vg_release(vg);
2805 1.1 haad return NULL;
2806 1.1 haad }
2807 1.1 haad }
2808 1.1 haad
2809 1.1 haad return vg;
2810 1.1 haad }
2811 1.1 haad
2812 1.1.1.3 haad void vg_release(struct volume_group *vg)
2813 1.1.1.3 haad {
2814 1.1.1.3 haad if (!vg || !vg->vgmem)
2815 1.1.1.3 haad return;
2816 1.1.1.3 haad
2817 1.1.1.3 haad if (vg->cmd && vg->vgmem == vg->cmd->mem)
2818 1.1.1.3 haad log_error("Internal error: global memory pool used for VG %s",
2819 1.1.1.3 haad vg->name);
2820 1.1.1.3 haad
2821 1.1.1.3 haad dm_pool_destroy(vg->vgmem);
2822 1.1.1.3 haad }
2823 1.1.1.3 haad
2824 1.1 haad /* This is only called by lv_from_lvid, which is only called from
2825 1.1 haad * activate.c so we know the appropriate VG lock is already held and
2826 1.1.1.3 haad * the vg_read_internal is therefore safe.
2827 1.1 haad */
2828 1.1 haad static struct volume_group *_vg_read_by_vgid(struct cmd_context *cmd,
2829 1.1 haad const char *vgid,
2830 1.1 haad unsigned precommitted)
2831 1.1 haad {
2832 1.1 haad const char *vgname;
2833 1.1 haad struct dm_list *vgnames;
2834 1.1.1.3 haad struct volume_group *vg = NULL;
2835 1.1 haad struct lvmcache_vginfo *vginfo;
2836 1.1 haad struct str_list *strl;
2837 1.1 haad int consistent = 0;
2838 1.1 haad
2839 1.1 haad /* Is corresponding vgname already cached? */
2840 1.1 haad if ((vginfo = vginfo_from_vgid(vgid)) &&
2841 1.1 haad vginfo->vgname && !is_orphan_vg(vginfo->vgname)) {
2842 1.1 haad if ((vg = _vg_read(cmd, NULL, vgid,
2843 1.1 haad &consistent, precommitted)) &&
2844 1.1 haad !strncmp((char *)vg->id.uuid, vgid, ID_LEN)) {
2845 1.1 haad
2846 1.1 haad if (!consistent) {
2847 1.1 haad log_error("Volume group %s metadata is "
2848 1.1 haad "inconsistent", vg->name);
2849 1.1 haad }
2850 1.1 haad return vg;
2851 1.1 haad }
2852 1.1.1.3 haad vg_release(vg);
2853 1.1 haad }
2854 1.1 haad
2855 1.1 haad /* Mustn't scan if memory locked: ensure cache gets pre-populated! */
2856 1.1 haad if (memlock())
2857 1.1.1.3 haad goto out;
2858 1.1 haad
2859 1.1.1.3 haad /* FIXME Need a genuine read by ID here - don't vg_read_internal by name! */
2860 1.1 haad /* FIXME Disabled vgrenames while active for now because we aren't
2861 1.1 haad * allowed to do a full scan here any more. */
2862 1.1 haad
2863 1.1 haad // The slow way - full scan required to cope with vgrename
2864 1.1.1.3 haad if (!(vgnames = get_vgnames(cmd, 2))) {
2865 1.1.1.3 haad log_error("vg_read_by_vgid: get_vgnames failed");
2866 1.1.1.3 haad goto out;
2867 1.1 haad }
2868 1.1 haad
2869 1.1 haad dm_list_iterate_items(strl, vgnames) {
2870 1.1 haad vgname = strl->str;
2871 1.1 haad if (!vgname || is_orphan_vg(vgname))
2872 1.1 haad continue; // FIXME Unnecessary?
2873 1.1 haad consistent = 0;
2874 1.1 haad if ((vg = _vg_read(cmd, vgname, vgid, &consistent,
2875 1.1 haad precommitted)) &&
2876 1.1 haad !strncmp((char *)vg->id.uuid, vgid, ID_LEN)) {
2877 1.1 haad
2878 1.1 haad if (!consistent) {
2879 1.1 haad log_error("Volume group %s metadata is "
2880 1.1 haad "inconsistent", vgname);
2881 1.1.1.3 haad goto out;
2882 1.1 haad }
2883 1.1 haad return vg;
2884 1.1 haad }
2885 1.1 haad }
2886 1.1 haad
2887 1.1.1.3 haad out:
2888 1.1.1.3 haad vg_release(vg);
2889 1.1 haad return NULL;
2890 1.1 haad }
2891 1.1 haad
2892 1.1 haad /* Only called by activate.c */
2893 1.1 haad struct logical_volume *lv_from_lvid(struct cmd_context *cmd, const char *lvid_s,
2894 1.1 haad unsigned precommitted)
2895 1.1 haad {
2896 1.1 haad struct lv_list *lvl;
2897 1.1 haad struct volume_group *vg;
2898 1.1 haad const union lvid *lvid;
2899 1.1 haad
2900 1.1 haad lvid = (const union lvid *) lvid_s;
2901 1.1 haad
2902 1.1 haad log_very_verbose("Finding volume group for uuid %s", lvid_s);
2903 1.1 haad if (!(vg = _vg_read_by_vgid(cmd, (char *)lvid->id[0].uuid, precommitted))) {
2904 1.1 haad log_error("Volume group for uuid not found: %s", lvid_s);
2905 1.1 haad return NULL;
2906 1.1 haad }
2907 1.1 haad
2908 1.1 haad log_verbose("Found volume group \"%s\"", vg->name);
2909 1.1 haad if (vg->status & EXPORTED_VG) {
2910 1.1 haad log_error("Volume group \"%s\" is exported", vg->name);
2911 1.1.1.3 haad goto out;
2912 1.1 haad }
2913 1.1 haad if (!(lvl = find_lv_in_vg_by_lvid(vg, lvid))) {
2914 1.1 haad log_very_verbose("Can't find logical volume id %s", lvid_s);
2915 1.1.1.3 haad goto out;
2916 1.1 haad }
2917 1.1 haad
2918 1.1 haad return lvl->lv;
2919 1.1.1.3 haad out:
2920 1.1.1.3 haad vg_release(vg);
2921 1.1.1.3 haad return NULL;
2922 1.1 haad }
2923 1.1 haad
2924 1.1 haad /**
2925 1.1 haad * pv_read - read and return a handle to a physical volume
2926 1.1 haad * @cmd: LVM command initiating the pv_read
2927 1.1 haad * @pv_name: full device name of the PV, including the path
2928 1.1 haad * @mdas: list of metadata areas of the PV
2929 1.1 haad * @label_sector: sector number where the PV label is stored on @pv_name
2930 1.1 haad * @warnings:
2931 1.1 haad *
2932 1.1 haad * Returns:
2933 1.1 haad * PV handle - valid pv_name and successful read of the PV, or
2934 1.1 haad * NULL - invalid parameter or error in reading the PV
2935 1.1 haad *
2936 1.1 haad * Note:
2937 1.1 haad * FIXME - liblvm todo - make into function that returns handle
2938 1.1 haad */
2939 1.1 haad struct physical_volume *pv_read(struct cmd_context *cmd, const char *pv_name,
2940 1.1 haad struct dm_list *mdas, uint64_t *label_sector,
2941 1.1.1.3 haad int warnings, int scan_label_only)
2942 1.1 haad {
2943 1.1.1.3 haad return _pv_read(cmd, cmd->mem, pv_name, mdas, label_sector, warnings, scan_label_only);
2944 1.1 haad }
2945 1.1 haad
2946 1.1 haad /* FIXME Use label functions instead of PV functions */
2947 1.1 haad static struct physical_volume *_pv_read(struct cmd_context *cmd,
2948 1.1.1.3 haad struct dm_pool *pvmem,
2949 1.1 haad const char *pv_name,
2950 1.1 haad struct dm_list *mdas,
2951 1.1 haad uint64_t *label_sector,
2952 1.1.1.3 haad int warnings, int scan_label_only)
2953 1.1 haad {
2954 1.1 haad struct physical_volume *pv;
2955 1.1 haad struct label *label;
2956 1.1 haad struct lvmcache_info *info;
2957 1.1 haad struct device *dev;
2958 1.1 haad
2959 1.1 haad if (!(dev = dev_cache_get(pv_name, cmd->filter)))
2960 1.1 haad return_NULL;
2961 1.1 haad
2962 1.1 haad if (!(label_read(dev, &label, UINT64_C(0)))) {
2963 1.1 haad if (warnings)
2964 1.1 haad log_error("No physical volume label read from %s",
2965 1.1 haad pv_name);
2966 1.1 haad return NULL;
2967 1.1 haad }
2968 1.1 haad
2969 1.1 haad info = (struct lvmcache_info *) label->info;
2970 1.1 haad if (label_sector && *label_sector)
2971 1.1 haad *label_sector = label->sector;
2972 1.1 haad
2973 1.1.1.3 haad if (!(pv = dm_pool_zalloc(pvmem, sizeof(*pv)))) {
2974 1.1 haad log_error("pv allocation for '%s' failed", pv_name);
2975 1.1 haad return NULL;
2976 1.1 haad }
2977 1.1 haad
2978 1.1 haad dm_list_init(&pv->tags);
2979 1.1 haad dm_list_init(&pv->segments);
2980 1.1 haad
2981 1.1 haad /* FIXME Move more common code up here */
2982 1.1.1.3 haad if (!(info->fmt->ops->pv_read(info->fmt, pv_name, pv, mdas,
2983 1.1.1.3 haad scan_label_only))) {
2984 1.1 haad log_error("Failed to read existing physical volume '%s'",
2985 1.1 haad pv_name);
2986 1.1 haad return NULL;
2987 1.1 haad }
2988 1.1 haad
2989 1.1 haad if (!pv->size)
2990 1.1 haad return NULL;
2991 1.1.1.3 haad
2992 1.1.1.3 haad if (!alloc_pv_segment_whole_pv(pvmem, pv))
2993 1.1 haad return_NULL;
2994 1.1 haad
2995 1.1 haad return pv;
2996 1.1 haad }
2997 1.1 haad
2998 1.1 haad /* May return empty list */
2999 1.1.1.3 haad struct dm_list *get_vgnames(struct cmd_context *cmd, int full_scan)
3000 1.1 haad {
3001 1.1 haad return lvmcache_get_vgnames(cmd, full_scan);
3002 1.1 haad }
3003 1.1 haad
3004 1.1 haad struct dm_list *get_vgids(struct cmd_context *cmd, int full_scan)
3005 1.1 haad {
3006 1.1 haad return lvmcache_get_vgids(cmd, full_scan);
3007 1.1 haad }
3008 1.1 haad
3009 1.1 haad static int _get_pvs(struct cmd_context *cmd, struct dm_list **pvslist)
3010 1.1 haad {
3011 1.1 haad struct str_list *strl;
3012 1.1 haad struct dm_list * uninitialized_var(results);
3013 1.1 haad const char *vgname, *vgid;
3014 1.1.1.3 haad struct pv_list *pvl, *pvl_copy;
3015 1.1 haad struct dm_list *vgids;
3016 1.1 haad struct volume_group *vg;
3017 1.1 haad int consistent = 0;
3018 1.1 haad int old_pvmove;
3019 1.1 haad
3020 1.1 haad lvmcache_label_scan(cmd, 0);
3021 1.1 haad
3022 1.1 haad if (pvslist) {
3023 1.1 haad if (!(results = dm_pool_alloc(cmd->mem, sizeof(*results)))) {
3024 1.1 haad log_error("PV list allocation failed");
3025 1.1 haad return 0;
3026 1.1 haad }
3027 1.1 haad
3028 1.1 haad dm_list_init(results);
3029 1.1 haad }
3030 1.1 haad
3031 1.1 haad /* Get list of VGs */
3032 1.1 haad if (!(vgids = get_vgids(cmd, 0))) {
3033 1.1.1.3 haad log_error("get_pvs: get_vgids failed");
3034 1.1 haad return 0;
3035 1.1 haad }
3036 1.1 haad
3037 1.1 haad /* Read every VG to ensure cache consistency */
3038 1.1 haad /* Orphan VG is last on list */
3039 1.1 haad old_pvmove = pvmove_mode();
3040 1.1 haad init_pvmove(1);
3041 1.1 haad dm_list_iterate_items(strl, vgids) {
3042 1.1 haad vgid = strl->str;
3043 1.1 haad if (!vgid)
3044 1.1 haad continue; /* FIXME Unnecessary? */
3045 1.1 haad consistent = 0;
3046 1.1 haad if (!(vgname = vgname_from_vgid(NULL, vgid))) {
3047 1.1 haad stack;
3048 1.1 haad continue;
3049 1.1 haad }
3050 1.1.1.3 haad if (!(vg = vg_read_internal(cmd, vgname, vgid, &consistent))) {
3051 1.1 haad stack;
3052 1.1 haad continue;
3053 1.1 haad }
3054 1.1 haad if (!consistent)
3055 1.1 haad log_warn("WARNING: Volume Group %s is not consistent",
3056 1.1 haad vgname);
3057 1.1 haad
3058 1.1 haad /* Move PVs onto results list */
3059 1.1 haad if (pvslist)
3060 1.1.1.3 haad dm_list_iterate_items(pvl, &vg->pvs) {
3061 1.1.1.3 haad if (!(pvl_copy = _copy_pvl(cmd->mem, pvl))) {
3062 1.1.1.3 haad log_error("PV list allocation failed");
3063 1.1.1.3 haad vg_release(vg);
3064 1.1.1.3 haad return 0;
3065 1.1.1.3 haad }
3066 1.1.1.3 haad dm_list_add(results, &pvl_copy->list);
3067 1.1.1.3 haad }
3068 1.1.1.3 haad vg_release(vg);
3069 1.1 haad }
3070 1.1 haad init_pvmove(old_pvmove);
3071 1.1 haad
3072 1.1 haad if (pvslist)
3073 1.1 haad *pvslist = results;
3074 1.1 haad else
3075 1.1 haad dm_pool_free(cmd->mem, vgids);
3076 1.1 haad
3077 1.1 haad return 1;
3078 1.1 haad }
3079 1.1 haad
3080 1.1 haad struct dm_list *get_pvs(struct cmd_context *cmd)
3081 1.1 haad {
3082 1.1 haad struct dm_list *results;
3083 1.1 haad
3084 1.1 haad if (!_get_pvs(cmd, &results))
3085 1.1 haad return NULL;
3086 1.1 haad
3087 1.1 haad return results;
3088 1.1 haad }
3089 1.1 haad
3090 1.1 haad int scan_vgs_for_pvs(struct cmd_context *cmd)
3091 1.1 haad {
3092 1.1 haad return _get_pvs(cmd, NULL);
3093 1.1 haad }
3094 1.1 haad
3095 1.1 haad int pv_write(struct cmd_context *cmd __attribute((unused)),
3096 1.1 haad struct physical_volume *pv,
3097 1.1 haad struct dm_list *mdas, int64_t label_sector)
3098 1.1 haad {
3099 1.1 haad if (!pv->fmt->ops->pv_write) {
3100 1.1 haad log_error("Format does not support writing physical volumes");
3101 1.1 haad return 0;
3102 1.1 haad }
3103 1.1 haad
3104 1.1 haad if (!is_orphan_vg(pv->vg_name) || pv->pe_alloc_count) {
3105 1.1 haad log_error("Assertion failed: can't _pv_write non-orphan PV "
3106 1.1 haad "(in VG %s)", pv->vg_name);
3107 1.1 haad return 0;
3108 1.1 haad }
3109 1.1 haad
3110 1.1 haad if (!pv->fmt->ops->pv_write(pv->fmt, pv, mdas, label_sector))
3111 1.1 haad return_0;
3112 1.1 haad
3113 1.1 haad return 1;
3114 1.1 haad }
3115 1.1 haad
3116 1.1 haad int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv)
3117 1.1 haad {
3118 1.1 haad const char *old_vg_name = pv->vg_name;
3119 1.1 haad
3120 1.1 haad pv->vg_name = cmd->fmt->orphan_vg_name;
3121 1.1 haad pv->status = ALLOCATABLE_PV;
3122 1.1 haad pv->pe_alloc_count = 0;
3123 1.1 haad
3124 1.1 haad if (!dev_get_size(pv->dev, &pv->size)) {
3125 1.1 haad log_error("%s: Couldn't get size.", pv_dev_name(pv));
3126 1.1 haad return 0;
3127 1.1 haad }
3128 1.1 haad
3129 1.1.1.3 haad if (!pv_write(cmd, pv, NULL, INT64_C(-1))) {
3130 1.1 haad log_error("Failed to clear metadata from physical "
3131 1.1 haad "volume \"%s\" after removal from \"%s\"",
3132 1.1 haad pv_dev_name(pv), old_vg_name);
3133 1.1 haad return 0;
3134 1.1 haad }
3135 1.1 haad
3136 1.1 haad return 1;
3137 1.1 haad }
3138 1.1 haad
3139 1.1 haad /**
3140 1.1 haad * is_orphan_vg - Determine whether a vg_name is an orphan
3141 1.1 haad * @vg_name: pointer to the vg_name
3142 1.1 haad */
3143 1.1 haad int is_orphan_vg(const char *vg_name)
3144 1.1 haad {
3145 1.1 haad return (vg_name && vg_name[0] == ORPHAN_PREFIX[0]) ? 1 : 0;
3146 1.1 haad }
3147 1.1 haad
3148 1.1 haad /**
3149 1.1 haad * is_orphan - Determine whether a pv is an orphan based on its vg_name
3150 1.1 haad * @pv: handle to the physical volume
3151 1.1 haad */
3152 1.1.1.3 haad int is_orphan(const struct physical_volume *pv)
3153 1.1 haad {
3154 1.1 haad return is_orphan_vg(pv_field(pv, vg_name));
3155 1.1 haad }
3156 1.1 haad
3157 1.1 haad /**
3158 1.1 haad * is_pv - Determine whether a pv is a real pv or dummy one
3159 1.1 haad * @pv: handle to device
3160 1.1 haad */
3161 1.1.1.3 haad int is_pv(struct physical_volume *pv)
3162 1.1 haad {
3163 1.1 haad return (pv_field(pv, vg_name) ? 1 : 0);
3164 1.1 haad }
3165 1.1 haad
3166 1.1 haad /*
3167 1.1 haad * Returns:
3168 1.1 haad * 0 - fail
3169 1.1 haad * 1 - success
3170 1.1 haad */
3171 1.1 haad int pv_analyze(struct cmd_context *cmd, const char *pv_name,
3172 1.1 haad uint64_t label_sector)
3173 1.1 haad {
3174 1.1 haad struct label *label;
3175 1.1 haad struct device *dev;
3176 1.1 haad struct metadata_area *mda;
3177 1.1 haad struct lvmcache_info *info;
3178 1.1 haad
3179 1.1 haad dev = dev_cache_get(pv_name, cmd->filter);
3180 1.1 haad if (!dev) {
3181 1.1 haad log_error("Device %s not found (or ignored by filtering).",
3182 1.1 haad pv_name);
3183 1.1 haad return 0;
3184 1.1 haad }
3185 1.1 haad
3186 1.1 haad /*
3187 1.1 haad * First, scan for LVM labels.
3188 1.1 haad */
3189 1.1 haad if (!label_read(dev, &label, label_sector)) {
3190 1.1 haad log_error("Could not find LVM label on %s",
3191 1.1 haad pv_name);
3192 1.1 haad return 0;
3193 1.1 haad }
3194 1.1 haad
3195 1.1 haad log_print("Found label on %s, sector %"PRIu64", type=%s",
3196 1.1 haad pv_name, label->sector, label->type);
3197 1.1 haad
3198 1.1 haad /*
3199 1.1 haad * Next, loop through metadata areas
3200 1.1 haad */
3201 1.1 haad info = label->info;
3202 1.1 haad dm_list_iterate_items(mda, &info->mdas)
3203 1.1 haad mda->ops->pv_analyze_mda(info->fmt, mda);
3204 1.1 haad
3205 1.1 haad return 1;
3206 1.1 haad }
3207 1.1 haad
3208 1.1.1.3 haad /* FIXME: remove / combine this with locking? */
3209 1.1.1.3 haad int vg_check_write_mode(struct volume_group *vg)
3210 1.1.1.3 haad {
3211 1.1.1.3 haad if (vg->open_mode != 'w') {
3212 1.1.1.3 haad log_errno(EPERM, "Attempt to modify a read-only VG");
3213 1.1.1.3 haad return 0;
3214 1.1.1.3 haad }
3215 1.1.1.3 haad return 1;
3216 1.1.1.3 haad }
3217 1.1 haad
3218 1.1.1.3 haad /*
3219 1.1.1.3 haad * Performs a set of checks against a VG according to bits set in status
3220 1.1.1.3 haad * and returns FAILED_* bits for those that aren't acceptable.
3221 1.1 haad *
3222 1.1.1.3 haad * FIXME Remove the unnecessary duplicate definitions and return bits directly.
3223 1.1 haad */
3224 1.1.1.3 haad static uint32_t _vg_bad_status_bits(const struct volume_group *vg,
3225 1.1.1.3 haad uint32_t status)
3226 1.1 haad {
3227 1.1.1.3 haad uint32_t failure = 0;
3228 1.1.1.3 haad
3229 1.1 haad if ((status & CLUSTERED) &&
3230 1.1.1.3 haad (vg_is_clustered(vg)) && !locking_is_clustered()) {
3231 1.1 haad log_error("Skipping clustered volume group %s", vg->name);
3232 1.1.1.3 haad /* Return because other flags are considered undefined. */
3233 1.1.1.3 haad return FAILED_CLUSTERED;
3234 1.1 haad }
3235 1.1 haad
3236 1.1 haad if ((status & EXPORTED_VG) &&
3237 1.1.1.3 haad vg_is_exported(vg)) {
3238 1.1 haad log_error("Volume group %s is exported", vg->name);
3239 1.1.1.3 haad failure |= FAILED_EXPORTED;
3240 1.1 haad }
3241 1.1 haad
3242 1.1 haad if ((status & LVM_WRITE) &&
3243 1.1 haad !(vg->status & LVM_WRITE)) {
3244 1.1 haad log_error("Volume group %s is read-only", vg->name);
3245 1.1.1.3 haad failure |= FAILED_READ_ONLY;
3246 1.1 haad }
3247 1.1.1.3 haad
3248 1.1 haad if ((status & RESIZEABLE_VG) &&
3249 1.1.1.3 haad !vg_is_resizeable(vg)) {
3250 1.1 haad log_error("Volume group %s is not resizeable.", vg->name);
3251 1.1.1.3 haad failure |= FAILED_RESIZEABLE;
3252 1.1 haad }
3253 1.1 haad
3254 1.1.1.3 haad return failure;
3255 1.1 haad }
3256 1.1 haad
3257 1.1.1.3 haad /**
3258 1.1.1.3 haad * vg_check_status - check volume group status flags and log error
3259 1.1.1.3 haad * @vg - volume group to check status flags
3260 1.1.1.3 haad * @status - specific status flags to check (e.g. EXPORTED_VG)
3261 1.1 haad */
3262 1.1.1.3 haad int vg_check_status(const struct volume_group *vg, uint32_t status)
3263 1.1.1.3 haad {
3264 1.1.1.3 haad return !_vg_bad_status_bits(vg, status);
3265 1.1.1.3 haad }
3266 1.1.1.3 haad
3267 1.1.1.3 haad static struct volume_group *_recover_vg(struct cmd_context *cmd, const char *lock_name,
3268 1.1.1.3 haad const char *vg_name, const char *vgid,
3269 1.1.1.3 haad uint32_t lock_flags)
3270 1.1 haad {
3271 1.1 haad int consistent = 1;
3272 1.1.1.3 haad struct volume_group *vg;
3273 1.1.1.3 haad
3274 1.1.1.3 haad lock_flags &= ~LCK_TYPE_MASK;
3275 1.1.1.3 haad lock_flags |= LCK_WRITE;
3276 1.1.1.3 haad
3277 1.1.1.3 haad unlock_vg(cmd, lock_name);
3278 1.1.1.3 haad
3279 1.1.1.3 haad dev_close_all();
3280 1.1.1.3 haad
3281 1.1.1.3 haad if (!lock_vol(cmd, lock_name, lock_flags))
3282 1.1.1.3 haad return_NULL;
3283 1.1.1.3 haad
3284 1.1.1.3 haad if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent)))
3285 1.1.1.3 haad return_NULL;
3286 1.1.1.3 haad
3287 1.1.1.3 haad if (!consistent) {
3288 1.1.1.3 haad vg_release(vg);
3289 1.1.1.3 haad return_NULL;
3290 1.1.1.3 haad }
3291 1.1.1.3 haad
3292 1.1.1.3 haad return (struct volume_group *)vg;
3293 1.1.1.3 haad }
3294 1.1 haad
3295 1.1.1.3 haad /*
3296 1.1.1.3 haad * Consolidated locking, reading, and status flag checking.
3297 1.1.1.3 haad *
3298 1.1.1.3 haad * If the metadata is inconsistent, setting READ_ALLOW_INCONSISTENT in
3299 1.1.1.3 haad * misc_flags will return it with FAILED_INCONSISTENT set instead of
3300 1.1.1.3 haad * giving you nothing.
3301 1.1.1.3 haad *
3302 1.1.1.3 haad * Use vg_read_error(vg) to determine the result. Nonzero means there were
3303 1.1.1.3 haad * problems reading the volume group.
3304 1.1.1.3 haad * Zero value means that the VG is open and appropriate locks are held.
3305 1.1.1.3 haad */
3306 1.1.1.3 haad static struct volume_group *_vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
3307 1.1.1.3 haad const char *vgid, uint32_t lock_flags,
3308 1.1.1.3 haad uint32_t status_flags, uint32_t misc_flags)
3309 1.1.1.3 haad {
3310 1.1.1.3 haad struct volume_group *vg = NULL;
3311 1.1.1.3 haad const char *lock_name;
3312 1.1.1.3 haad int consistent = 1;
3313 1.1.1.3 haad int consistent_in;
3314 1.1.1.3 haad uint32_t failure = 0;
3315 1.1.1.3 haad int already_locked;
3316 1.1.1.3 haad
3317 1.1.1.3 haad if (misc_flags & READ_ALLOW_INCONSISTENT || !(lock_flags & LCK_WRITE))
3318 1.1 haad consistent = 0;
3319 1.1 haad
3320 1.1.1.3 haad if (!validate_name(vg_name) && !is_orphan_vg(vg_name)) {
3321 1.1 haad log_error("Volume group name %s has invalid characters",
3322 1.1 haad vg_name);
3323 1.1 haad return NULL;
3324 1.1 haad }
3325 1.1 haad
3326 1.1.1.3 haad lock_name = is_orphan_vg(vg_name) ? VG_ORPHANS : vg_name;
3327 1.1.1.3 haad already_locked = vgname_is_locked(lock_name);
3328 1.1.1.3 haad
3329 1.1.1.3 haad if (!already_locked && !(misc_flags & READ_WITHOUT_LOCK) &&
3330 1.1.1.3 haad !lock_vol(cmd, lock_name, lock_flags)) {
3331 1.1 haad log_error("Can't get lock for %s", vg_name);
3332 1.1.1.3 haad return _vg_make_handle(cmd, vg, FAILED_LOCKING);
3333 1.1 haad }
3334 1.1 haad
3335 1.1.1.3 haad if (is_orphan_vg(vg_name))
3336 1.1.1.3 haad status_flags &= ~LVM_WRITE;
3337 1.1.1.3 haad
3338 1.1.1.3 haad consistent_in = consistent;
3339 1.1.1.3 haad
3340 1.1.1.3 haad /* If consistent == 1, we get NULL here if correction fails. */
3341 1.1.1.3 haad if (!(vg = vg_read_internal(cmd, vg_name, vgid, &consistent))) {
3342 1.1.1.3 haad if (consistent_in && !consistent) {
3343 1.1.1.3 haad log_error("Volume group \"%s\" inconsistent.", vg_name);
3344 1.1.1.3 haad failure |= FAILED_INCONSISTENT;
3345 1.1.1.3 haad goto_bad;
3346 1.1.1.3 haad }
3347 1.1.1.3 haad
3348 1.1 haad log_error("Volume group \"%s\" not found", vg_name);
3349 1.1.1.3 haad
3350 1.1.1.3 haad failure |= FAILED_NOTFOUND;
3351 1.1.1.3 haad goto_bad;
3352 1.1 haad }
3353 1.1 haad
3354 1.1.1.3 haad if (vg_is_clustered(vg) && !locking_is_clustered()) {
3355 1.1.1.3 haad log_error("Skipping clustered volume group %s", vg->name);
3356 1.1.1.3 haad failure |= FAILED_CLUSTERED;
3357 1.1.1.3 haad goto_bad;
3358 1.1 haad }
3359 1.1 haad
3360 1.1.1.3 haad /* consistent == 0 when VG is not found, but failed == FAILED_NOTFOUND */
3361 1.1.1.3 haad if (!consistent && !failure) {
3362 1.1.1.3 haad vg_release(vg);
3363 1.1.1.3 haad if (!(vg = _recover_vg(cmd, lock_name, vg_name, vgid, lock_flags))) {
3364 1.1.1.3 haad log_error("Recovery of volume group \"%s\" failed.",
3365 1.1.1.3 haad vg_name);
3366 1.1.1.3 haad failure |= FAILED_INCONSISTENT;
3367 1.1.1.3 haad goto_bad;
3368 1.1.1.3 haad }
3369 1.1.1.3 haad }
3370 1.1.1.3 haad
3371 1.1.1.3 haad /*
3372 1.1.1.3 haad * Check that the tool can handle tricky cases -- missing PVs and
3373 1.1.1.3 haad * unknown segment types.
3374 1.1.1.3 haad */
3375 1.1.1.3 haad
3376 1.1.1.3 haad if (!cmd->handles_missing_pvs && vg_missing_pv_count(vg) &&
3377 1.1.1.3 haad (lock_flags & LCK_WRITE)) {
3378 1.1.1.3 haad log_error("Cannot change VG %s while PVs are missing.", vg->name);
3379 1.1.1.3 haad log_error("Consider vgreduce --removemissing.");
3380 1.1.1.3 haad failure |= FAILED_INCONSISTENT; /* FIXME new failure code here? */
3381 1.1.1.3 haad goto_bad;
3382 1.1.1.3 haad }
3383 1.1.1.3 haad
3384 1.1.1.3 haad if (!cmd->handles_unknown_segments && vg_has_unknown_segments(vg) &&
3385 1.1.1.3 haad (lock_flags & LCK_WRITE)) {
3386 1.1.1.3 haad log_error("Cannot change VG %s with unknown segments in it!",
3387 1.1.1.3 haad vg->name);
3388 1.1.1.3 haad failure |= FAILED_INCONSISTENT; /* FIXME new failure code here? */
3389 1.1.1.3 haad goto_bad;
3390 1.1.1.3 haad }
3391 1.1.1.3 haad
3392 1.1.1.3 haad failure |= _vg_bad_status_bits(vg, status_flags);
3393 1.1.1.3 haad if (failure)
3394 1.1.1.3 haad goto_bad;
3395 1.1.1.3 haad
3396 1.1.1.3 haad return _vg_make_handle(cmd, vg, failure);
3397 1.1.1.3 haad
3398 1.1.1.3 haad bad:
3399 1.1.1.3 haad if (!already_locked && !(misc_flags & READ_WITHOUT_LOCK))
3400 1.1.1.3 haad unlock_vg(cmd, lock_name);
3401 1.1.1.3 haad
3402 1.1.1.3 haad return _vg_make_handle(cmd, vg, failure);
3403 1.1.1.3 haad }
3404 1.1.1.3 haad
3405 1.1.1.3 haad /*
3406 1.1.1.3 haad * vg_read: High-level volume group metadata read function.
3407 1.1.1.3 haad *
3408 1.1.1.3 haad * vg_read_error() must be used on any handle returned to check for errors.
3409 1.1.1.3 haad *
3410 1.1.1.3 haad * - metadata inconsistent and automatic correction failed: FAILED_INCONSISTENT
3411 1.1.1.3 haad * - VG is read-only: FAILED_READ_ONLY
3412 1.1.1.3 haad * - VG is EXPORTED, unless flags has READ_ALLOW_EXPORTED: FAILED_EXPORTED
3413 1.1.1.3 haad * - VG is not RESIZEABLE: FAILED_RESIZEABLE
3414 1.1.1.3 haad * - locking failed: FAILED_LOCKING
3415 1.1.1.3 haad *
3416 1.1.1.3 haad * On failures, all locks are released, unless one of the following applies:
3417 1.1.1.3 haad * - vgname_is_locked(lock_name) is true
3418 1.1.1.3 haad * FIXME: remove the above 2 conditions if possible and make an error always
3419 1.1.1.3 haad * release the lock.
3420 1.1.1.3 haad *
3421 1.1.1.3 haad * Volume groups are opened read-only unless flags contains READ_FOR_UPDATE.
3422 1.1.1.3 haad *
3423 1.1.1.3 haad * Checking for VG existence:
3424 1.1.1.3 haad *
3425 1.1.1.3 haad * FIXME: We want vg_read to attempt automatic recovery after acquiring a
3426 1.1.1.3 haad * temporary write lock: if that fails, we bail out as usual, with failed &
3427 1.1.1.3 haad * FAILED_INCONSISTENT. If it works, we are good to go. Code that's been in
3428 1.1.1.3 haad * toollib just set lock_flags to LCK_VG_WRITE and called vg_read_internal with
3429 1.1.1.3 haad * *consistent = 1.
3430 1.1.1.3 haad */
3431 1.1.1.3 haad struct volume_group *vg_read(struct cmd_context *cmd, const char *vg_name,
3432 1.1.1.3 haad const char *vgid, uint32_t flags)
3433 1.1.1.3 haad {
3434 1.1.1.3 haad uint32_t status = 0;
3435 1.1.1.3 haad uint32_t lock_flags = LCK_VG_READ;
3436 1.1.1.3 haad
3437 1.1.1.3 haad if (flags & READ_FOR_UPDATE) {
3438 1.1.1.3 haad status |= EXPORTED_VG | LVM_WRITE;
3439 1.1.1.3 haad lock_flags = LCK_VG_WRITE;
3440 1.1.1.3 haad }
3441 1.1.1.3 haad
3442 1.1.1.3 haad if (flags & READ_ALLOW_EXPORTED)
3443 1.1.1.3 haad status &= ~EXPORTED_VG;
3444 1.1.1.3 haad
3445 1.1.1.3 haad return _vg_lock_and_read(cmd, vg_name, vgid, lock_flags, status, flags);
3446 1.1.1.3 haad }
3447 1.1.1.3 haad
3448 1.1.1.3 haad /*
3449 1.1.1.3 haad * A high-level volume group metadata reading function. Open a volume group for
3450 1.1.1.3 haad * later update (this means the user code can change the metadata and later
3451 1.1.1.3 haad * request the new metadata to be written and committed).
3452 1.1.1.3 haad */
3453 1.1.1.3 haad struct volume_group *vg_read_for_update(struct cmd_context *cmd, const char *vg_name,
3454 1.1.1.3 haad const char *vgid, uint32_t flags)
3455 1.1.1.3 haad {
3456 1.1.1.3 haad return vg_read(cmd, vg_name, vgid, flags | READ_FOR_UPDATE);
3457 1.1.1.3 haad }
3458 1.1.1.3 haad
3459 1.1.1.3 haad /*
3460 1.1.1.3 haad * Test the validity of a VG handle returned by vg_read() or vg_read_for_update().
3461 1.1.1.3 haad */
3462 1.1.1.3 haad uint32_t vg_read_error(struct volume_group *vg_handle)
3463 1.1.1.3 haad {
3464 1.1.1.3 haad if (!vg_handle)
3465 1.1.1.3 haad return FAILED_ALLOCATION;
3466 1.1.1.3 haad
3467 1.1.1.3 haad return vg_handle->read_status;
3468 1.1.1.3 haad }
3469 1.1.1.3 haad
3470 1.1.1.3 haad /*
3471 1.1.1.3 haad * Lock a vgname and/or check for existence.
3472 1.1.1.3 haad * Takes a WRITE lock on the vgname before scanning.
3473 1.1.1.3 haad * If scanning fails or vgname found, release the lock.
3474 1.1.1.3 haad * NOTE: If you find the return codes confusing, you might think of this
3475 1.1.1.3 haad * function as similar to an open() call with O_CREAT and O_EXCL flags
3476 1.1.1.3 haad * (open returns fail with -EEXIST if file already exists).
3477 1.1.1.3 haad *
3478 1.1.1.3 haad * Returns:
3479 1.1.1.3 haad * FAILED_LOCKING - Cannot lock name
3480 1.1.1.3 haad * FAILED_EXIST - VG name already exists - cannot reserve
3481 1.1.1.3 haad * SUCCESS - VG name does not exist in system and WRITE lock held
3482 1.1.1.3 haad */
3483 1.1.1.3 haad uint32_t vg_lock_newname(struct cmd_context *cmd, const char *vgname)
3484 1.1.1.3 haad {
3485 1.1.1.3 haad if (!lock_vol(cmd, vgname, LCK_VG_WRITE)) {
3486 1.1.1.3 haad return FAILED_LOCKING;
3487 1.1.1.3 haad }
3488 1.1.1.3 haad
3489 1.1.1.3 haad /* Find the vgname in the cache */
3490 1.1.1.3 haad /* If it's not there we must do full scan to be completely sure */
3491 1.1.1.3 haad if (!fmt_from_vgname(vgname, NULL)) {
3492 1.1.1.3 haad lvmcache_label_scan(cmd, 0);
3493 1.1.1.3 haad if (!fmt_from_vgname(vgname, NULL)) {
3494 1.1.1.3 haad if (memlock()) {
3495 1.1.1.3 haad /*
3496 1.1.1.3 haad * FIXME: Disallow calling this function if
3497 1.1.1.3 haad * memlock() is true.
3498 1.1.1.3 haad */
3499 1.1.1.3 haad unlock_vg(cmd, vgname);
3500 1.1.1.3 haad return FAILED_LOCKING;
3501 1.1.1.3 haad }
3502 1.1.1.3 haad lvmcache_label_scan(cmd, 2);
3503 1.1.1.3 haad if (!fmt_from_vgname(vgname, NULL)) {
3504 1.1.1.3 haad /* vgname not found after scanning */
3505 1.1.1.3 haad return SUCCESS;
3506 1.1.1.3 haad }
3507 1.1.1.3 haad }
3508 1.1.1.3 haad }
3509 1.1.1.3 haad
3510 1.1.1.3 haad /* Found vgname so cannot reserve. */
3511 1.1.1.3 haad unlock_vg(cmd, vgname);
3512 1.1.1.3 haad return FAILED_EXIST;
3513 1.1 haad }
3514 1.1 haad
3515 1.1 haad /*
3516 1.1 haad * Gets/Sets for external LVM library
3517 1.1 haad */
3518 1.1.1.3 haad struct id pv_id(const struct physical_volume *pv)
3519 1.1 haad {
3520 1.1 haad return pv_field(pv, id);
3521 1.1 haad }
3522 1.1 haad
3523 1.1.1.3 haad const struct format_type *pv_format_type(const struct physical_volume *pv)
3524 1.1 haad {
3525 1.1 haad return pv_field(pv, fmt);
3526 1.1 haad }
3527 1.1 haad
3528 1.1.1.3 haad struct id pv_vgid(const struct physical_volume *pv)
3529 1.1 haad {
3530 1.1 haad return pv_field(pv, vgid);
3531 1.1 haad }
3532 1.1 haad
3533 1.1.1.3 haad struct device *pv_dev(const struct physical_volume *pv)
3534 1.1 haad {
3535 1.1 haad return pv_field(pv, dev);
3536 1.1 haad }
3537 1.1 haad
3538 1.1.1.3 haad const char *pv_vg_name(const struct physical_volume *pv)
3539 1.1 haad {
3540 1.1 haad return pv_field(pv, vg_name);
3541 1.1 haad }
3542 1.1 haad
3543 1.1.1.3 haad const char *pv_dev_name(const struct physical_volume *pv)
3544 1.1 haad {
3545 1.1 haad return dev_name(pv_dev(pv));
3546 1.1 haad }
3547 1.1 haad
3548 1.1.1.3 haad uint64_t pv_size(const struct physical_volume *pv)
3549 1.1 haad {
3550 1.1 haad return pv_field(pv, size);
3551 1.1 haad }
3552 1.1 haad
3553 1.1.1.3 haad uint32_t pv_status(const struct physical_volume *pv)
3554 1.1 haad {
3555 1.1 haad return pv_field(pv, status);
3556 1.1 haad }
3557 1.1 haad
3558 1.1.1.3 haad uint32_t pv_pe_size(const struct physical_volume *pv)
3559 1.1 haad {
3560 1.1 haad return pv_field(pv, pe_size);
3561 1.1 haad }
3562 1.1 haad
3563 1.1.1.3 haad uint64_t pv_pe_start(const struct physical_volume *pv)
3564 1.1 haad {
3565 1.1 haad return pv_field(pv, pe_start);
3566 1.1 haad }
3567 1.1 haad
3568 1.1.1.3 haad uint32_t pv_pe_count(const struct physical_volume *pv)
3569 1.1 haad {
3570 1.1 haad return pv_field(pv, pe_count);
3571 1.1 haad }
3572 1.1 haad
3573 1.1.1.3 haad uint32_t pv_pe_alloc_count(const struct physical_volume *pv)
3574 1.1 haad {
3575 1.1 haad return pv_field(pv, pe_alloc_count);
3576 1.1 haad }
3577 1.1 haad
3578 1.1.1.3 haad uint32_t pv_mda_count(const struct physical_volume *pv)
3579 1.1.1.3 haad {
3580 1.1.1.3 haad struct lvmcache_info *info;
3581 1.1.1.3 haad
3582 1.1.1.3 haad info = info_from_pvid((const char *)&pv->id.uuid, 0);
3583 1.1.1.3 haad return info ? dm_list_size(&info->mdas) : UINT64_C(0);
3584 1.1.1.3 haad }
3585 1.1.1.3 haad
3586 1.1.1.3 haad uint32_t vg_seqno(const struct volume_group *vg)
3587 1.1.1.3 haad {
3588 1.1.1.3 haad return vg->seqno;
3589 1.1.1.3 haad }
3590 1.1.1.3 haad
3591 1.1.1.3 haad uint32_t vg_status(const struct volume_group *vg)
3592 1.1 haad {
3593 1.1 haad return vg->status;
3594 1.1 haad }
3595 1.1 haad
3596 1.1.1.3 haad uint64_t vg_size(const struct volume_group *vg)
3597 1.1.1.3 haad {
3598 1.1.1.3 haad return (uint64_t) vg->extent_count * vg->extent_size;
3599 1.1.1.3 haad }
3600 1.1.1.3 haad
3601 1.1.1.3 haad uint64_t vg_free(const struct volume_group *vg)
3602 1.1.1.3 haad {
3603 1.1.1.3 haad return (uint64_t) vg->free_count * vg->extent_size;
3604 1.1.1.3 haad }
3605 1.1.1.3 haad
3606 1.1.1.3 haad uint64_t vg_extent_size(const struct volume_group *vg)
3607 1.1.1.3 haad {
3608 1.1.1.3 haad return (uint64_t) vg->extent_size;
3609 1.1.1.3 haad }
3610 1.1.1.3 haad
3611 1.1.1.3 haad uint64_t vg_extent_count(const struct volume_group *vg)
3612 1.1.1.3 haad {
3613 1.1.1.3 haad return (uint64_t) vg->extent_count;
3614 1.1.1.3 haad }
3615 1.1.1.3 haad
3616 1.1.1.3 haad uint64_t vg_free_count(const struct volume_group *vg)
3617 1.1.1.3 haad {
3618 1.1.1.3 haad return (uint64_t) vg->free_count;
3619 1.1.1.3 haad }
3620 1.1.1.3 haad
3621 1.1.1.3 haad uint64_t vg_pv_count(const struct volume_group *vg)
3622 1.1.1.3 haad {
3623 1.1.1.3 haad return (uint64_t) vg->pv_count;
3624 1.1.1.3 haad }
3625 1.1.1.3 haad
3626 1.1.1.3 haad uint64_t vg_max_pv(const struct volume_group *vg)
3627 1.1.1.3 haad {
3628 1.1.1.3 haad return (uint64_t) vg->max_pv;
3629 1.1.1.3 haad }
3630 1.1.1.3 haad
3631 1.1.1.3 haad uint64_t vg_max_lv(const struct volume_group *vg)
3632 1.1.1.3 haad {
3633 1.1.1.3 haad return (uint64_t) vg->max_lv;
3634 1.1.1.3 haad }
3635 1.1.1.3 haad
3636 1.1.1.3 haad uint32_t vg_mda_count(const struct volume_group *vg)
3637 1.1.1.3 haad {
3638 1.1.1.3 haad return dm_list_size(&vg->fid->metadata_areas);
3639 1.1.1.3 haad }
3640 1.1.1.3 haad
3641 1.1.1.3 haad uint64_t lv_size(const struct logical_volume *lv)
3642 1.1.1.3 haad {
3643 1.1.1.3 haad return lv->size;
3644 1.1.1.3 haad }
3645 1.1.1.3 haad
3646 1.1 haad /**
3647 1.1 haad * pv_by_path - Given a device path return a PV handle if it is a PV
3648 1.1 haad * @cmd - handle to the LVM command instance
3649 1.1 haad * @pv_name - device path to read for the PV
3650 1.1 haad *
3651 1.1 haad * Returns:
3652 1.1 haad * NULL - device path does not contain a valid PV
3653 1.1 haad * non-NULL - PV handle corresponding to device path
3654 1.1 haad *
3655 1.1 haad * FIXME: merge with find_pv_by_name ?
3656 1.1 haad */
3657 1.1.1.3 haad struct physical_volume *pv_by_path(struct cmd_context *cmd, const char *pv_name)
3658 1.1 haad {
3659 1.1 haad struct dm_list mdas;
3660 1.1.1.3 haad
3661 1.1 haad dm_list_init(&mdas);
3662 1.1.1.3 haad return _pv_read(cmd, cmd->mem, pv_name, &mdas, NULL, 1, 0);
3663 1.1 haad }
3664