Home | History | Annotate | Line # | Download | only in metadata
metadata.c revision 1.1
      1 /*	$NetBSD: metadata.c,v 1.1 2008/12/22 00:18:08 haad Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
      5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
      6  *
      7  * This file is part of LVM2.
      8  *
      9  * This copyrighted material is made available to anyone wishing to use,
     10  * modify, copy, or redistribute it subject to the terms and conditions
     11  * of the GNU Lesser General Public License v.2.1.
     12  *
     13  * You should have received a copy of the GNU Lesser General Public License
     14  * along with this program; if not, write to the Free Software Foundation,
     15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     16  */
     17 
     18 #include "lib.h"
     19 #include "device.h"
     20 #include "metadata.h"
     21 #include "toolcontext.h"
     22 #include "lvm-string.h"
     23 #include "lvm-file.h"
     24 #include "lvmcache.h"
     25 #include "memlock.h"
     26 #include "str_list.h"
     27 #include "pv_alloc.h"
     28 #include "activate.h"
     29 #include "display.h"
     30 #include "locking.h"
     31 #include "archiver.h"
     32 #include "defaults.h"
     33 
     34 #include <sys/param.h>
     35 
     36 /*
     37  * FIXME: Check for valid handle before dereferencing field or log error?
     38  */
     39 #define pv_field(handle, field)				\
     40 	(((const struct physical_volume *)(handle))->field)
     41 
     42 static struct physical_volume *_pv_read(struct cmd_context *cmd,
     43 					const char *pv_name,
     44 					struct dm_list *mdas,
     45 					uint64_t *label_sector,
     46 					int warnings);
     47 
     48 static struct physical_volume *_pv_create(const struct format_type *fmt,
     49 				  struct device *dev,
     50 				  struct id *id, uint64_t size,
     51 				  uint64_t pe_start,
     52 				  uint32_t existing_extent_count,
     53 				  uint32_t existing_extent_size,
     54 				  int pvmetadatacopies,
     55 				  uint64_t pvmetadatasize, struct dm_list *mdas);
     56 
     57 static int _pv_write(struct cmd_context *cmd __attribute((unused)),
     58 		     struct physical_volume *pv,
     59 	     	     struct dm_list *mdas, int64_t label_sector);
     60 
     61 static struct physical_volume *_find_pv_by_name(struct cmd_context *cmd,
     62 			 			const char *pv_name);
     63 
     64 static struct pv_list *_find_pv_in_vg(const struct volume_group *vg,
     65 				      const char *pv_name);
     66 
     67 static struct physical_volume *_find_pv_in_vg_by_uuid(const struct volume_group *vg,
     68 						      const struct id *id);
     69 
     70 unsigned long pe_align(struct physical_volume *pv)
     71 {
     72 	if (pv->pe_align)
     73 		goto out;
     74 
     75 	pv->pe_align = MAX(65536UL, lvm_getpagesize()) >> SECTOR_SHIFT;
     76 
     77 	/*
     78 	 * Align to chunk size of underlying md device if present
     79 	 */
     80 	if (!pv->dev)
     81 		goto out;
     82 
     83 	if (find_config_tree_bool(pv->fmt->cmd, "devices/md_chunk_alignment",
     84 				  DEFAULT_MD_CHUNK_ALIGNMENT))
     85 		pv->pe_align = MAX(pv->pe_align,
     86 				   dev_md_chunk_size(pv->fmt->cmd->sysfs_dir,
     87 						     pv->dev));
     88 
     89 	log_very_verbose("%s: Setting PE alignment to %lu sectors.",
     90 			 dev_name(pv->dev), pv->pe_align);
     91 
     92 out:
     93 	return pv->pe_align;
     94 }
     95 
     96 /**
     97  * add_pv_to_vg - Add a physical volume to a volume group
     98  * @vg - volume group to add to
     99  * @pv_name - name of the pv (to be removed)
    100  * @pv - physical volume to add to volume group
    101  *
    102  * Returns:
    103  *  0 - failure
    104  *  1 - success
    105  * FIXME: remove pv_name - obtain safely from pv
    106  */
    107 int add_pv_to_vg(struct volume_group *vg, const char *pv_name,
    108 		 struct physical_volume *pv)
    109 {
    110 	struct pv_list *pvl;
    111 	struct format_instance *fid = vg->fid;
    112 	struct dm_pool *mem = fid->fmt->cmd->mem;
    113 
    114 	log_verbose("Adding physical volume '%s' to volume group '%s'",
    115 		    pv_name, vg->name);
    116 
    117 	if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
    118 		log_error("pv_list allocation for '%s' failed", pv_name);
    119 		return 0;
    120 	}
    121 
    122 	if (!is_orphan_vg(pv->vg_name)) {
    123 		log_error("Physical volume '%s' is already in volume group "
    124 			  "'%s'", pv_name, pv->vg_name);
    125 		return 0;
    126 	}
    127 
    128 	if (pv->fmt != fid->fmt) {
    129 		log_error("Physical volume %s is of different format type (%s)",
    130 			  pv_name, pv->fmt->name);
    131 		return 0;
    132 	}
    133 
    134 	/* Ensure PV doesn't depend on another PV already in the VG */
    135 	if (pv_uses_vg(pv, vg)) {
    136 		log_error("Physical volume %s might be constructed from same "
    137 			  "volume group %s", pv_name, vg->name);
    138 		return 0;
    139 	}
    140 
    141 	if (!(pv->vg_name = dm_pool_strdup(mem, vg->name))) {
    142 		log_error("vg->name allocation failed for '%s'", pv_name);
    143 		return 0;
    144 	}
    145 
    146 	memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
    147 
    148 	/* Units of 512-byte sectors */
    149 	pv->pe_size = vg->extent_size;
    150 
    151 	/* FIXME Do proper rounding-up alignment? */
    152 	/* Reserved space for label; this holds 0 for PVs created by LVM1 */
    153 	if (pv->pe_start < pe_align(pv))
    154 		pv->pe_start = pe_align(pv);
    155 
    156 	/*
    157 	 * pe_count must always be calculated by pv_setup
    158 	 */
    159 	pv->pe_alloc_count = 0;
    160 
    161 	if (!fid->fmt->ops->pv_setup(fid->fmt, UINT64_C(0), 0,
    162 				     vg->extent_size, 0, UINT64_C(0),
    163 				     &fid->metadata_areas, pv, vg)) {
    164 		log_error("Format-specific setup of physical volume '%s' "
    165 			  "failed.", pv_name);
    166 		return 0;
    167 	}
    168 
    169 	if (_find_pv_in_vg(vg, pv_name)) {
    170 		log_error("Physical volume '%s' listed more than once.",
    171 			  pv_name);
    172 		return 0;
    173 	}
    174 
    175 	if (vg->pv_count && (vg->pv_count == vg->max_pv)) {
    176 		log_error("No space for '%s' - volume group '%s' "
    177 			  "holds max %d physical volume(s).", pv_name,
    178 			  vg->name, vg->max_pv);
    179 		return 0;
    180 	}
    181 
    182 	if (!alloc_pv_segment_whole_pv(mem, pv))
    183 		return_0;
    184 
    185 	pvl->pv = pv;
    186 	dm_list_add(&vg->pvs, &pvl->list);
    187 
    188 	if ((uint64_t) vg->extent_count + pv->pe_count > UINT32_MAX) {
    189 		log_error("Unable to add %s to %s: new extent count (%"
    190 			  PRIu64 ") exceeds limit (%" PRIu32 ").",
    191 			  pv_name, vg->name,
    192 			  (uint64_t) vg->extent_count + pv->pe_count,
    193 			  UINT32_MAX);
    194 		return 0;
    195 	}
    196 
    197 	vg->pv_count++;
    198 	vg->extent_count += pv->pe_count;
    199 	vg->free_count += pv->pe_count;
    200 
    201 	return 1;
    202 }
    203 
    204 static int _copy_pv(struct physical_volume *pv_to,
    205 		    struct physical_volume *pv_from)
    206 {
    207 	memcpy(pv_to, pv_from, sizeof(*pv_to));
    208 
    209 	if (!str_list_dup(pv_to->fmt->cmd->mem, &pv_to->tags, &pv_from->tags)) {
    210 		log_error("PV tags duplication failed");
    211 		return 0;
    212 	}
    213 
    214 	if (!peg_dup(pv_to->fmt->cmd->mem, &pv_to->segments,
    215 		     &pv_from->segments))
    216 		return_0;
    217 
    218 	return 1;
    219 }
    220 
    221 int get_pv_from_vg_by_id(const struct format_type *fmt, const char *vg_name,
    222 			 const char *vgid, const char *pvid,
    223 			 struct physical_volume *pv)
    224 {
    225 	struct volume_group *vg;
    226 	struct pv_list *pvl;
    227 	int consistent = 0;
    228 
    229 	if (!(vg = vg_read(fmt->cmd, vg_name, vgid, &consistent))) {
    230 		log_error("get_pv_from_vg_by_id: vg_read failed to read VG %s",
    231 			  vg_name);
    232 		return 0;
    233 	}
    234 
    235 	if (!consistent)
    236 		log_warn("WARNING: Volume group %s is not consistent",
    237 			 vg_name);
    238 
    239 	dm_list_iterate_items(pvl, &vg->pvs) {
    240 		if (id_equal(&pvl->pv->id, (const struct id *) pvid)) {
    241 			if (!_copy_pv(pv, pvl->pv))
    242 				return_0;
    243 			return 1;
    244 		}
    245 	}
    246 
    247 	return 0;
    248 }
    249 
    250 static int validate_new_vg_name(struct cmd_context *cmd, const char *vg_name)
    251 {
    252 	char vg_path[PATH_MAX];
    253 
    254 	if (!validate_name(vg_name))
    255 		return_0;
    256 
    257 	snprintf(vg_path, PATH_MAX, "%s%s", cmd->dev_dir, vg_name);
    258 	if (path_exists(vg_path)) {
    259 		log_error("%s: already exists in filesystem", vg_path);
    260 		return 0;
    261 	}
    262 
    263 	return 1;
    264 }
    265 
    266 int validate_vg_rename_params(struct cmd_context *cmd,
    267 			      const char *vg_name_old,
    268 			      const char *vg_name_new)
    269 {
    270 	unsigned length;
    271 	char *dev_dir;
    272 
    273 	dev_dir = cmd->dev_dir;
    274 	length = strlen(dev_dir);
    275 
    276 	/* Check sanity of new name */
    277 	if (strlen(vg_name_new) > NAME_LEN - length - 2) {
    278 		log_error("New volume group path exceeds maximum length "
    279 			  "of %d!", NAME_LEN - length - 2);
    280 		return 0;
    281 	}
    282 
    283 	if (!validate_new_vg_name(cmd, vg_name_new)) {
    284 		log_error("New volume group name \"%s\" is invalid",
    285 			  vg_name_new);
    286 		return 0;
    287 	}
    288 
    289 	if (!strcmp(vg_name_old, vg_name_new)) {
    290 		log_error("Old and new volume group names must differ");
    291 		return 0;
    292 	}
    293 
    294 	return 1;
    295 }
    296 
    297 int vg_rename(struct cmd_context *cmd, struct volume_group *vg,
    298 	      const char *new_name)
    299 {
    300 	struct dm_pool *mem = cmd->mem;
    301 	struct pv_list *pvl;
    302 
    303 	if (!(vg->name = dm_pool_strdup(mem, new_name))) {
    304 		log_error("vg->name allocation failed for '%s'", new_name);
    305 		return 0;
    306 	}
    307 
    308 	dm_list_iterate_items(pvl, &vg->pvs) {
    309 		if (!(pvl->pv->vg_name = dm_pool_strdup(mem, new_name))) {
    310 			log_error("pv->vg_name allocation failed for '%s'",
    311 				  pv_dev_name(pvl->pv));
    312 			return 0;
    313 		}
    314 	}
    315 
    316 	return 1;
    317 }
    318 
    319 static int remove_lvs_in_vg(struct cmd_context *cmd,
    320 			    struct volume_group *vg,
    321 			    force_t force)
    322 {
    323 	struct dm_list *lst;
    324 	struct lv_list *lvl;
    325 
    326 	while ((lst = dm_list_first(&vg->lvs))) {
    327 		lvl = dm_list_item(lst, struct lv_list);
    328 		if (!lv_remove_with_dependencies(cmd, lvl->lv, force))
    329 		    return 0;
    330 	}
    331 
    332 	return 1;
    333 }
    334 
    335 /* FIXME: remove redundant vg_name */
    336 int vg_remove_single(struct cmd_context *cmd, const char *vg_name,
    337 		     struct volume_group *vg, int consistent,
    338 		     force_t force __attribute((unused)))
    339 {
    340 	struct physical_volume *pv;
    341 	struct pv_list *pvl;
    342 	int ret = 1;
    343 
    344 	if (!vg || !consistent || vg_missing_pv_count(vg)) {
    345 		log_error("Volume group \"%s\" not found, is inconsistent "
    346 			  "or has PVs missing.", vg_name);
    347 		log_error("Consider vgreduce --removemissing if metadata "
    348 			  "is inconsistent.");
    349 		return 0;
    350 	}
    351 
    352 	if (!vg_check_status(vg, EXPORTED_VG))
    353 		return 0;
    354 
    355 	if (vg->lv_count) {
    356 		if ((force == PROMPT) &&
    357 		    (yes_no_prompt("Do you really want to remove volume "
    358 				   "group \"%s\" containing %d "
    359 				   "logical volumes? [y/n]: ",
    360 				   vg_name, vg->lv_count) == 'n')) {
    361 			log_print("Volume group \"%s\" not removed", vg_name);
    362 			return 0;
    363 		}
    364 		if (!remove_lvs_in_vg(cmd, vg, force))
    365 			return 0;
    366 	}
    367 
    368 	if (vg->lv_count) {
    369 		log_error("Volume group \"%s\" still contains %d "
    370 			  "logical volume(s)", vg_name, vg->lv_count);
    371 		return 0;
    372 	}
    373 
    374 	if (!archive(vg))
    375 		return 0;
    376 
    377 	if (!vg_remove(vg)) {
    378 		log_error("vg_remove %s failed", vg_name);
    379 		return 0;
    380 	}
    381 
    382 	/* init physical volumes */
    383 	dm_list_iterate_items(pvl, &vg->pvs) {
    384 		pv = pvl->pv;
    385 		log_verbose("Removing physical volume \"%s\" from "
    386 			    "volume group \"%s\"", pv_dev_name(pv), vg_name);
    387 		pv->vg_name = vg->fid->fmt->orphan_vg_name;
    388 		pv->status = ALLOCATABLE_PV;
    389 
    390 		if (!dev_get_size(pv_dev(pv), &pv->size)) {
    391 			log_error("%s: Couldn't get size.", pv_dev_name(pv));
    392 			ret = 0;
    393 			continue;
    394 		}
    395 
    396 		/* FIXME Write to same sector label was read from */
    397 		if (!pv_write(cmd, pv, NULL, INT64_C(-1))) {
    398 			log_error("Failed to remove physical volume \"%s\""
    399 				  " from volume group \"%s\"",
    400 				  pv_dev_name(pv), vg_name);
    401 			ret = 0;
    402 		}
    403 	}
    404 
    405 	backup_remove(cmd, vg_name);
    406 
    407 	if (ret)
    408 		log_print("Volume group \"%s\" successfully removed", vg_name);
    409 	else
    410 		log_error("Volume group \"%s\" not properly removed", vg_name);
    411 
    412 	return ret;
    413 }
    414 
    415 int vg_extend(struct volume_group *vg, int pv_count, char **pv_names)
    416 {
    417 	int i;
    418 	struct physical_volume *pv;
    419 
    420 	/* attach each pv */
    421 	for (i = 0; i < pv_count; i++) {
    422 		if (!(pv = pv_by_path(vg->fid->fmt->cmd, pv_names[i]))) {
    423 			log_error("%s not identified as an existing "
    424 				  "physical volume", pv_names[i]);
    425 			goto bad;
    426 		}
    427 
    428 		if (!add_pv_to_vg(vg, pv_names[i], pv))
    429 			goto bad;
    430 	}
    431 
    432 /* FIXME Decide whether to initialise and add new mdahs to format instance */
    433 
    434 	return 1;
    435 
    436       bad:
    437 	log_error("Unable to add physical volume '%s' to "
    438 		  "volume group '%s'.", pv_names[i], vg->name);
    439 	return 0;
    440 }
    441 
    442 const char *strip_dir(const char *vg_name, const char *dev_dir)
    443 {
    444 	size_t len = strlen(dev_dir);
    445 	if (!strncmp(vg_name, dev_dir, len))
    446 		vg_name += len;
    447 
    448 	return vg_name;
    449 }
    450 
    451 /*
    452  * Validate parameters to vg_create() before calling.
    453  * FIXME: Move inside vg_create library function.
    454  * FIXME: Change vgcreate_params struct to individual gets/sets
    455  */
    456 int validate_vg_create_params(struct cmd_context *cmd,
    457 			      struct vgcreate_params *vp)
    458 {
    459 	if (!validate_new_vg_name(cmd, vp->vg_name)) {
    460 		log_error("New volume group name \"%s\" is invalid",
    461 			  vp->vg_name);
    462 		return 1;
    463 	}
    464 
    465 	if (vp->alloc == ALLOC_INHERIT) {
    466 		log_error("Volume Group allocation policy cannot inherit "
    467 			  "from anything");
    468 		return 1;
    469 	}
    470 
    471 	if (!vp->extent_size) {
    472 		log_error("Physical extent size may not be zero");
    473 		return 1;
    474 	}
    475 
    476 	if (!(cmd->fmt->features & FMT_UNLIMITED_VOLS)) {
    477 		if (!vp->max_lv)
    478 			vp->max_lv = 255;
    479 		if (!vp->max_pv)
    480 			vp->max_pv = 255;
    481 		if (vp->max_lv > 255 || vp->max_pv > 255) {
    482 			log_error("Number of volumes may not exceed 255");
    483 			return 1;
    484 		}
    485 	}
    486 
    487 	return 0;
    488 }
    489 
    490 struct volume_group *vg_create(struct cmd_context *cmd, const char *vg_name,
    491 			       uint32_t extent_size, uint32_t max_pv,
    492 			       uint32_t max_lv, alloc_policy_t alloc,
    493 			       int pv_count, char **pv_names)
    494 {
    495 	struct volume_group *vg;
    496 	struct dm_pool *mem = cmd->mem;
    497 	int consistent = 0;
    498 
    499 	if (!(vg = dm_pool_zalloc(mem, sizeof(*vg))))
    500 		return_NULL;
    501 
    502 	/* is this vg name already in use ? */
    503 	if (vg_read(cmd, vg_name, NULL, &consistent)) {
    504 		log_err("A volume group called '%s' already exists.", vg_name);
    505 		goto bad;
    506 	}
    507 
    508 	if (!id_create(&vg->id)) {
    509 		log_err("Couldn't create uuid for volume group '%s'.", vg_name);
    510 		goto bad;
    511 	}
    512 
    513 	/* Strip dev_dir if present */
    514 	vg_name = strip_dir(vg_name, cmd->dev_dir);
    515 
    516 	vg->cmd = cmd;
    517 
    518 	if (!(vg->name = dm_pool_strdup(mem, vg_name)))
    519 		goto_bad;
    520 
    521 	vg->seqno = 0;
    522 
    523 	vg->status = (RESIZEABLE_VG | LVM_READ | LVM_WRITE);
    524 	if (!(vg->system_id = dm_pool_alloc(mem, NAME_LEN)))
    525 		goto_bad;
    526 
    527 	*vg->system_id = '\0';
    528 
    529 	vg->extent_size = extent_size;
    530 	vg->extent_count = 0;
    531 	vg->free_count = 0;
    532 
    533 	vg->max_lv = max_lv;
    534 	vg->max_pv = max_pv;
    535 
    536 	vg->alloc = alloc;
    537 
    538 	vg->pv_count = 0;
    539 	dm_list_init(&vg->pvs);
    540 
    541 	vg->lv_count = 0;
    542 	dm_list_init(&vg->lvs);
    543 
    544 	vg->snapshot_count = 0;
    545 
    546 	dm_list_init(&vg->tags);
    547 
    548 	if (!(vg->fid = cmd->fmt->ops->create_instance(cmd->fmt, vg_name,
    549 						       NULL, NULL))) {
    550 		log_error("Failed to create format instance");
    551 		goto bad;
    552 	}
    553 
    554 	if (vg->fid->fmt->ops->vg_setup &&
    555 	    !vg->fid->fmt->ops->vg_setup(vg->fid, vg)) {
    556 		log_error("Format specific setup of volume group '%s' failed.",
    557 			  vg_name);
    558 		goto bad;
    559 	}
    560 
    561 	/* attach the pv's */
    562 	if (!vg_extend(vg, pv_count, pv_names))
    563 		goto_bad;
    564 
    565 	return vg;
    566 
    567       bad:
    568 	dm_pool_free(mem, vg);
    569 	return NULL;
    570 }
    571 
    572 static int _recalc_extents(uint32_t *extents, const char *desc1,
    573 			   const char *desc2, uint32_t old_size,
    574 			   uint32_t new_size)
    575 {
    576 	uint64_t size = (uint64_t) old_size * (*extents);
    577 
    578 	if (size % new_size) {
    579 		log_error("New size %" PRIu64 " for %s%s not an exact number "
    580 			  "of new extents.", size, desc1, desc2);
    581 		return 0;
    582 	}
    583 
    584 	size /= new_size;
    585 
    586 	if (size > UINT32_MAX) {
    587 		log_error("New extent count %" PRIu64 " for %s%s exceeds "
    588 			  "32 bits.", size, desc1, desc2);
    589 		return 0;
    590 	}
    591 
    592 	*extents = (uint32_t) size;
    593 
    594 	return 1;
    595 }
    596 
    597 int vg_change_pesize(struct cmd_context *cmd __attribute((unused)),
    598 		     struct volume_group *vg, uint32_t new_size)
    599 {
    600 	uint32_t old_size = vg->extent_size;
    601 	struct pv_list *pvl;
    602 	struct lv_list *lvl;
    603 	struct physical_volume *pv;
    604 	struct logical_volume *lv;
    605 	struct lv_segment *seg;
    606 	struct pv_segment *pvseg;
    607 	uint32_t s;
    608 
    609 	vg->extent_size = new_size;
    610 
    611 	if (vg->fid->fmt->ops->vg_setup &&
    612 	    !vg->fid->fmt->ops->vg_setup(vg->fid, vg))
    613 		return_0;
    614 
    615 	if (!_recalc_extents(&vg->extent_count, vg->name, "", old_size,
    616 			     new_size))
    617 		return_0;
    618 
    619 	if (!_recalc_extents(&vg->free_count, vg->name, " free space",
    620 			     old_size, new_size))
    621 		return_0;
    622 
    623 	/* foreach PV */
    624 	dm_list_iterate_items(pvl, &vg->pvs) {
    625 		pv = pvl->pv;
    626 
    627 		pv->pe_size = new_size;
    628 		if (!_recalc_extents(&pv->pe_count, pv_dev_name(pv), "",
    629 				     old_size, new_size))
    630 			return_0;
    631 
    632 		if (!_recalc_extents(&pv->pe_alloc_count, pv_dev_name(pv),
    633 				     " allocated space", old_size, new_size))
    634 			return_0;
    635 
    636 		/* foreach free PV Segment */
    637 		dm_list_iterate_items(pvseg, &pv->segments) {
    638 			if (pvseg_is_allocated(pvseg))
    639 				continue;
    640 
    641 			if (!_recalc_extents(&pvseg->pe, pv_dev_name(pv),
    642 					     " PV segment start", old_size,
    643 					     new_size))
    644 				return_0;
    645 			if (!_recalc_extents(&pvseg->len, pv_dev_name(pv),
    646 					     " PV segment length", old_size,
    647 					     new_size))
    648 				return_0;
    649 		}
    650 	}
    651 
    652 	/* foreach LV */
    653 	dm_list_iterate_items(lvl, &vg->lvs) {
    654 		lv = lvl->lv;
    655 
    656 		if (!_recalc_extents(&lv->le_count, lv->name, "", old_size,
    657 				     new_size))
    658 			return_0;
    659 
    660 		dm_list_iterate_items(seg, &lv->segments) {
    661 			if (!_recalc_extents(&seg->le, lv->name,
    662 					     " segment start", old_size,
    663 					     new_size))
    664 				return_0;
    665 
    666 			if (!_recalc_extents(&seg->len, lv->name,
    667 					     " segment length", old_size,
    668 					     new_size))
    669 				return_0;
    670 
    671 			if (!_recalc_extents(&seg->area_len, lv->name,
    672 					     " area length", old_size,
    673 					     new_size))
    674 				return_0;
    675 
    676 			if (!_recalc_extents(&seg->extents_copied, lv->name,
    677 					     " extents moved", old_size,
    678 					     new_size))
    679 				return_0;
    680 
    681 			/* foreach area */
    682 			for (s = 0; s < seg->area_count; s++) {
    683 				switch (seg_type(seg, s)) {
    684 				case AREA_PV:
    685 					if (!_recalc_extents
    686 					    (&seg_pe(seg, s),
    687 					     lv->name,
    688 					     " pvseg start", old_size,
    689 					     new_size))
    690 						return_0;
    691 					if (!_recalc_extents
    692 					    (&seg_pvseg(seg, s)->len,
    693 					     lv->name,
    694 					     " pvseg length", old_size,
    695 					     new_size))
    696 						return_0;
    697 					break;
    698 				case AREA_LV:
    699 					if (!_recalc_extents
    700 					    (&seg_le(seg, s), lv->name,
    701 					     " area start", old_size,
    702 					     new_size))
    703 						return_0;
    704 					break;
    705 				case AREA_UNASSIGNED:
    706 					log_error("Unassigned area %u found in "
    707 						  "segment", s);
    708 					return 0;
    709 				}
    710 			}
    711 		}
    712 
    713 	}
    714 
    715 	return 1;
    716 }
    717 
    718 /*
    719  * Separate metadata areas after splitting a VG.
    720  * Also accepts orphan VG as destination (for vgreduce).
    721  */
    722 int vg_split_mdas(struct cmd_context *cmd __attribute((unused)),
    723 		  struct volume_group *vg_from, struct volume_group *vg_to)
    724 {
    725 	struct metadata_area *mda, *mda2;
    726 	struct dm_list *mdas_from, *mdas_to;
    727 	int common_mda = 0;
    728 
    729 	mdas_from = &vg_from->fid->metadata_areas;
    730 	mdas_to = &vg_to->fid->metadata_areas;
    731 
    732 	dm_list_iterate_items_safe(mda, mda2, mdas_from) {
    733 		if (!mda->ops->mda_in_vg) {
    734 			common_mda = 1;
    735 			continue;
    736 		}
    737 
    738 		if (!mda->ops->mda_in_vg(vg_from->fid, vg_from, mda)) {
    739 			if (is_orphan_vg(vg_to->name))
    740 				dm_list_del(&mda->list);
    741 			else
    742 				dm_list_move(mdas_to, &mda->list);
    743 		}
    744 	}
    745 
    746 	if (dm_list_empty(mdas_from) ||
    747 	    (!is_orphan_vg(vg_to->name) && dm_list_empty(mdas_to)))
    748 		return common_mda;
    749 
    750 	return 1;
    751 }
    752 
    753 /**
    754  * pv_create - initialize a physical volume for use with a volume group
    755  * @fmt: format type
    756  * @dev: PV device to initialize
    757  * @id: PV UUID to use for initialization
    758  * @size: size of the PV in sectors
    759  * @pe_start: physical extent start
    760  * @existing_extent_count
    761  * @existing_extent_size
    762  * @pvmetadatacopies
    763  * @pvmetadatasize
    764  * @mdas
    765  *
    766  * Returns:
    767  *   PV handle - physical volume initialized successfully
    768  *   NULL - invalid parameter or problem initializing the physical volume
    769  *
    770  * Note:
    771  *   FIXME - liblvm todo - tidy up arguments for external use (fmt, mdas, etc)
    772  */
    773 pv_t *pv_create(const struct cmd_context *cmd,
    774 		struct device *dev,
    775 		struct id *id, uint64_t size,
    776 		uint64_t pe_start,
    777 		uint32_t existing_extent_count,
    778 		uint32_t existing_extent_size,
    779 		int pvmetadatacopies,
    780 		uint64_t pvmetadatasize, struct dm_list *mdas)
    781 {
    782 	return _pv_create(cmd->fmt, dev, id, size, pe_start,
    783 			  existing_extent_count,
    784 			  existing_extent_size,
    785 			  pvmetadatacopies,
    786 			  pvmetadatasize, mdas);
    787 }
    788 
    789 static void _free_pv(struct dm_pool *mem, struct physical_volume *pv)
    790 {
    791 	dm_pool_free(mem, pv);
    792 }
    793 
    794 static struct physical_volume *_alloc_pv(struct dm_pool *mem, struct device *dev)
    795 {
    796 	struct physical_volume *pv = dm_pool_zalloc(mem, sizeof(*pv));
    797 
    798 	if (!pv)
    799 		return_NULL;
    800 
    801 	if (!(pv->vg_name = dm_pool_zalloc(mem, NAME_LEN))) {
    802 		dm_pool_free(mem, pv);
    803 		return NULL;
    804 	}
    805 
    806 	pv->pe_size = 0;
    807 	pv->pe_start = 0;
    808 	pv->pe_count = 0;
    809 	pv->pe_alloc_count = 0;
    810 	pv->pe_align = 0;
    811 	pv->fmt = NULL;
    812 	pv->dev = dev;
    813 
    814 	pv->status = ALLOCATABLE_PV;
    815 
    816 	dm_list_init(&pv->tags);
    817 	dm_list_init(&pv->segments);
    818 
    819 	return pv;
    820 }
    821 
    822 /* Sizes in sectors */
    823 static struct physical_volume *_pv_create(const struct format_type *fmt,
    824 				  struct device *dev,
    825 				  struct id *id, uint64_t size,
    826 				  uint64_t pe_start,
    827 				  uint32_t existing_extent_count,
    828 				  uint32_t existing_extent_size,
    829 				  int pvmetadatacopies,
    830 				  uint64_t pvmetadatasize, struct dm_list *mdas)
    831 {
    832 	struct dm_pool *mem = fmt->cmd->mem;
    833 	struct physical_volume *pv = _alloc_pv(mem, dev);
    834 
    835 	if (!pv)
    836 		return NULL;
    837 
    838 	if (id)
    839 		memcpy(&pv->id, id, sizeof(*id));
    840 	else if (!id_create(&pv->id)) {
    841 		log_error("Failed to create random uuid for %s.",
    842 			  dev_name(dev));
    843 		goto bad;
    844 	}
    845 
    846 	if (!dev_get_size(pv->dev, &pv->size)) {
    847 		log_error("%s: Couldn't get size.", pv_dev_name(pv));
    848 		goto bad;
    849 	}
    850 
    851 	if (size) {
    852 		if (size > pv->size)
    853 			log_warn("WARNING: %s: Overriding real size. "
    854 				  "You could lose data.", pv_dev_name(pv));
    855 		log_verbose("%s: Pretending size is %" PRIu64 " sectors.",
    856 			    pv_dev_name(pv), size);
    857 		pv->size = size;
    858 	}
    859 
    860 	if (pv->size < PV_MIN_SIZE) {
    861 		log_error("%s: Size must exceed minimum of %ld sectors.",
    862 			  pv_dev_name(pv), PV_MIN_SIZE);
    863 		goto bad;
    864 	}
    865 
    866 	pv->fmt = fmt;
    867 	pv->vg_name = fmt->orphan_vg_name;
    868 
    869 	if (!fmt->ops->pv_setup(fmt, pe_start, existing_extent_count,
    870 				existing_extent_size,
    871 				pvmetadatacopies, pvmetadatasize, mdas,
    872 				pv, NULL)) {
    873 		log_error("%s: Format-specific setup of physical volume "
    874 			  "failed.", pv_dev_name(pv));
    875 		goto bad;
    876 	}
    877 	return pv;
    878 
    879       bad:
    880 	_free_pv(mem, pv);
    881 	return NULL;
    882 }
    883 
    884 /* FIXME: liblvm todo - make into function that returns handle */
    885 struct pv_list *find_pv_in_vg(const struct volume_group *vg,
    886 			      const char *pv_name)
    887 {
    888 	return _find_pv_in_vg(vg, pv_name);
    889 }
    890 
    891 static struct pv_list *_find_pv_in_vg(const struct volume_group *vg,
    892 				      const char *pv_name)
    893 {
    894 	struct pv_list *pvl;
    895 
    896 	dm_list_iterate_items(pvl, &vg->pvs)
    897 		if (pvl->pv->dev == dev_cache_get(pv_name, vg->cmd->filter))
    898 			return pvl;
    899 
    900 	return NULL;
    901 }
    902 
    903 struct pv_list *find_pv_in_pv_list(const struct dm_list *pl,
    904 				   const struct physical_volume *pv)
    905 {
    906 	struct pv_list *pvl;
    907 
    908 	dm_list_iterate_items(pvl, pl)
    909 		if (pvl->pv == pv)
    910 			return pvl;
    911 
    912 	return NULL;
    913 }
    914 
    915 int pv_is_in_vg(struct volume_group *vg, struct physical_volume *pv)
    916 {
    917 	struct pv_list *pvl;
    918 
    919 	dm_list_iterate_items(pvl, &vg->pvs)
    920 		if (pv == pvl->pv)
    921 			 return 1;
    922 
    923 	return 0;
    924 }
    925 
    926 /**
    927  * find_pv_in_vg_by_uuid - Find PV in VG by PV UUID
    928  * @vg: volume group to search
    929  * @id: UUID of the PV to match
    930  *
    931  * Returns:
    932  *   PV handle - if UUID of PV found in VG
    933  *   NULL - invalid parameter or UUID of PV not found in VG
    934  *
    935  * Note
    936  *   FIXME - liblvm todo - make into function that takes VG handle
    937  */
    938 pv_t *find_pv_in_vg_by_uuid(const struct volume_group *vg,
    939 			    const struct id *id)
    940 {
    941 	return _find_pv_in_vg_by_uuid(vg, id);
    942 }
    943 
    944 
    945 static struct physical_volume *_find_pv_in_vg_by_uuid(const struct volume_group *vg,
    946 						      const struct id *id)
    947 {
    948 	struct pv_list *pvl;
    949 
    950 	dm_list_iterate_items(pvl, &vg->pvs)
    951 		if (id_equal(&pvl->pv->id, id))
    952 			return pvl->pv;
    953 
    954 	return NULL;
    955 }
    956 
    957 struct lv_list *find_lv_in_vg(const struct volume_group *vg,
    958 			      const char *lv_name)
    959 {
    960 	struct lv_list *lvl;
    961 	const char *ptr;
    962 
    963 	/* Use last component */
    964 	if ((ptr = strrchr(lv_name, '/')))
    965 		ptr++;
    966 	else
    967 		ptr = lv_name;
    968 
    969 	dm_list_iterate_items(lvl, &vg->lvs)
    970 		if (!strcmp(lvl->lv->name, ptr))
    971 			return lvl;
    972 
    973 	return NULL;
    974 }
    975 
    976 struct lv_list *find_lv_in_lv_list(const struct dm_list *ll,
    977 				   const struct logical_volume *lv)
    978 {
    979 	struct lv_list *lvl;
    980 
    981 	dm_list_iterate_items(lvl, ll)
    982 		if (lvl->lv == lv)
    983 			return lvl;
    984 
    985 	return NULL;
    986 }
    987 
    988 struct lv_list *find_lv_in_vg_by_lvid(struct volume_group *vg,
    989 				      const union lvid *lvid)
    990 {
    991 	struct lv_list *lvl;
    992 
    993 	dm_list_iterate_items(lvl, &vg->lvs)
    994 		if (!strncmp(lvl->lv->lvid.s, lvid->s, sizeof(*lvid)))
    995 			return lvl;
    996 
    997 	return NULL;
    998 }
    999 
   1000 struct logical_volume *find_lv(const struct volume_group *vg,
   1001 			       const char *lv_name)
   1002 {
   1003 	struct lv_list *lvl = find_lv_in_vg(vg, lv_name);
   1004 	return lvl ? lvl->lv : NULL;
   1005 }
   1006 
   1007 struct physical_volume *find_pv(struct volume_group *vg, struct device *dev)
   1008 {
   1009 	struct pv_list *pvl;
   1010 
   1011 	dm_list_iterate_items(pvl, &vg->pvs)
   1012 		if (dev == pvl->pv->dev)
   1013 			return pvl->pv;
   1014 
   1015 	return NULL;
   1016 }
   1017 
   1018 /* FIXME: liblvm todo - make into function that returns handle */
   1019 struct physical_volume *find_pv_by_name(struct cmd_context *cmd,
   1020 					const char *pv_name)
   1021 {
   1022 	return _find_pv_by_name(cmd, pv_name);
   1023 }
   1024 
   1025 
   1026 static struct physical_volume *_find_pv_by_name(struct cmd_context *cmd,
   1027 			 			const char *pv_name)
   1028 {
   1029 	struct physical_volume *pv;
   1030 
   1031 	if (!(pv = _pv_read(cmd, pv_name, NULL, NULL, 1))) {
   1032 		log_error("Physical volume %s not found", pv_name);
   1033 		return NULL;
   1034 	}
   1035 
   1036 	if (is_orphan_vg(pv->vg_name)) {
   1037 		/* If a PV has no MDAs - need to search all VGs for it */
   1038 		if (!scan_vgs_for_pvs(cmd))
   1039 			return_NULL;
   1040 		if (!(pv = _pv_read(cmd, pv_name, NULL, NULL, 1))) {
   1041 			log_error("Physical volume %s not found", pv_name);
   1042 			return NULL;
   1043 		}
   1044 	}
   1045 
   1046 	if (is_orphan_vg(pv->vg_name)) {
   1047 		log_error("Physical volume %s not in a volume group", pv_name);
   1048 		return NULL;
   1049 	}
   1050 
   1051 	return pv;
   1052 }
   1053 
   1054 /* Find segment at a given logical extent in an LV */
   1055 struct lv_segment *find_seg_by_le(const struct logical_volume *lv, uint32_t le)
   1056 {
   1057 	struct lv_segment *seg;
   1058 
   1059 	dm_list_iterate_items(seg, &lv->segments)
   1060 		if (le >= seg->le && le < seg->le + seg->len)
   1061 			return seg;
   1062 
   1063 	return NULL;
   1064 }
   1065 
   1066 struct lv_segment *first_seg(const struct logical_volume *lv)
   1067 {
   1068 	struct lv_segment *seg = NULL;
   1069 
   1070 	dm_list_iterate_items(seg, &lv->segments)
   1071 		break;
   1072 
   1073 	return seg;
   1074 }
   1075 
   1076 /* Find segment at a given physical extent in a PV */
   1077 struct pv_segment *find_peg_by_pe(const struct physical_volume *pv, uint32_t pe)
   1078 {
   1079 	struct pv_segment *peg;
   1080 
   1081 	dm_list_iterate_items(peg, &pv->segments)
   1082 		if (pe >= peg->pe && pe < peg->pe + peg->len)
   1083 			return peg;
   1084 
   1085 	return NULL;
   1086 }
   1087 
   1088 int vg_remove(struct volume_group *vg)
   1089 {
   1090 	struct metadata_area *mda;
   1091 
   1092 	/* FIXME Improve recovery situation? */
   1093 	/* Remove each copy of the metadata */
   1094 	dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1095 		if (mda->ops->vg_remove &&
   1096 		    !mda->ops->vg_remove(vg->fid, vg, mda))
   1097 			return_0;
   1098 	}
   1099 
   1100 	return 1;
   1101 }
   1102 
   1103 /*
   1104  * Determine whether two vgs are compatible for merging.
   1105  */
   1106 int vgs_are_compatible(struct cmd_context *cmd __attribute((unused)),
   1107 		       struct volume_group *vg_from,
   1108 		       struct volume_group *vg_to)
   1109 {
   1110 	struct lv_list *lvl1, *lvl2;
   1111 	struct pv_list *pvl;
   1112 	char *name1, *name2;
   1113 
   1114 	if (lvs_in_vg_activated(vg_from)) {
   1115 		log_error("Logical volumes in \"%s\" must be inactive",
   1116 			  vg_from->name);
   1117 		return 0;
   1118 	}
   1119 
   1120 	/* Check compatibility */
   1121 	if (vg_to->extent_size != vg_from->extent_size) {
   1122 		log_error("Extent sizes differ: %d (%s) and %d (%s)",
   1123 			  vg_to->extent_size, vg_to->name,
   1124 			  vg_from->extent_size, vg_from->name);
   1125 		return 0;
   1126 	}
   1127 
   1128 	if (vg_to->max_pv &&
   1129 	    (vg_to->max_pv < vg_to->pv_count + vg_from->pv_count)) {
   1130 		log_error("Maximum number of physical volumes (%d) exceeded "
   1131 			  " for \"%s\" and \"%s\"", vg_to->max_pv, vg_to->name,
   1132 			  vg_from->name);
   1133 		return 0;
   1134 	}
   1135 
   1136 	if (vg_to->max_lv &&
   1137 	    (vg_to->max_lv < vg_to->lv_count + vg_from->lv_count)) {
   1138 		log_error("Maximum number of logical volumes (%d) exceeded "
   1139 			  " for \"%s\" and \"%s\"", vg_to->max_lv, vg_to->name,
   1140 			  vg_from->name);
   1141 		return 0;
   1142 	}
   1143 
   1144 	/* Metadata types must be the same */
   1145 	if (vg_to->fid->fmt != vg_from->fid->fmt) {
   1146 		log_error("Metadata types differ for \"%s\" and \"%s\"",
   1147 			  vg_to->name, vg_from->name);
   1148 		return 0;
   1149 	}
   1150 
   1151 	/* Clustering attribute must be the same */
   1152 	if (vg_is_clustered(vg_to) != vg_is_clustered(vg_from)) {
   1153 		log_error("Clustered attribute differs for \"%s\" and \"%s\"",
   1154 			  vg_to->name, vg_from->name);
   1155 		return 0;
   1156 	}
   1157 
   1158 	/* Check no conflicts with LV names */
   1159 	dm_list_iterate_items(lvl1, &vg_to->lvs) {
   1160 		name1 = lvl1->lv->name;
   1161 
   1162 		dm_list_iterate_items(lvl2, &vg_from->lvs) {
   1163 			name2 = lvl2->lv->name;
   1164 
   1165 			if (!strcmp(name1, name2)) {
   1166 				log_error("Duplicate logical volume "
   1167 					  "name \"%s\" "
   1168 					  "in \"%s\" and \"%s\"",
   1169 					  name1, vg_to->name, vg_from->name);
   1170 				return 0;
   1171 			}
   1172 		}
   1173 	}
   1174 
   1175 	/* Check no PVs are constructed from either VG */
   1176 	dm_list_iterate_items(pvl, &vg_to->pvs) {
   1177 		if (pv_uses_vg(pvl->pv, vg_from)) {
   1178 			log_error("Physical volume %s might be constructed "
   1179 				  "from same volume group %s.",
   1180 				  pv_dev_name(pvl->pv), vg_from->name);
   1181 			return 0;
   1182 		}
   1183 	}
   1184 
   1185 	dm_list_iterate_items(pvl, &vg_from->pvs) {
   1186 		if (pv_uses_vg(pvl->pv, vg_to)) {
   1187 			log_error("Physical volume %s might be constructed "
   1188 				  "from same volume group %s.",
   1189 				  pv_dev_name(pvl->pv), vg_to->name);
   1190 			return 0;
   1191 		}
   1192 	}
   1193 
   1194 	return 1;
   1195 }
   1196 
   1197 struct _lv_postorder_baton {
   1198 	int (*fn)(struct logical_volume *lv, void *data);
   1199 	void *data;
   1200 };
   1201 
   1202 static int _lv_postorder_visit(struct logical_volume *,
   1203 			       int (*fn)(struct logical_volume *lv, void *data),
   1204 			       void *data);
   1205 
   1206 static int _lv_postorder_level(struct logical_volume *lv, void *data)
   1207 {
   1208 	struct _lv_postorder_baton *baton = data;
   1209 	if (lv->status & POSTORDER_OPEN_FLAG)
   1210 		return 1; // a data structure loop has closed...
   1211 	lv->status |= POSTORDER_OPEN_FLAG;
   1212 	int r =_lv_postorder_visit(lv, baton->fn, baton->data);
   1213 	lv->status &= ~POSTORDER_OPEN_FLAG;
   1214 	lv->status |= POSTORDER_FLAG;
   1215 	return r;
   1216 };
   1217 
   1218 static int _lv_each_dependency(struct logical_volume *lv,
   1219 			       int (*fn)(struct logical_volume *lv, void *data),
   1220 			       void *data)
   1221 {
   1222 	int i, s;
   1223 	struct lv_segment *lvseg;
   1224 
   1225 	struct logical_volume *deps[] = {
   1226 		lv->snapshot ? lv->snapshot->origin : 0,
   1227 		lv->snapshot ? lv->snapshot->cow : 0 };
   1228 	for (i = 0; i < sizeof(deps) / sizeof(*deps); ++i) {
   1229 		if (deps[i] && !fn(deps[i], data))
   1230 			return_0;
   1231 	}
   1232 
   1233 	dm_list_iterate_items(lvseg, &lv->segments) {
   1234 		if (lvseg->log_lv && !fn(lvseg->log_lv, data))
   1235 			return_0;
   1236 		for (s = 0; s < lvseg->area_count; ++s) {
   1237 			if (seg_type(lvseg, s) == AREA_LV && !fn(seg_lv(lvseg,s), data))
   1238 				return_0;
   1239 		}
   1240 	}
   1241 	return 1;
   1242 }
   1243 
   1244 static int _lv_postorder_cleanup(struct logical_volume *lv, void *data)
   1245 {
   1246 	if (!(lv->status & POSTORDER_FLAG))
   1247 		return 1;
   1248 	lv->status &= ~POSTORDER_FLAG;
   1249 
   1250 	if (!_lv_each_dependency(lv, _lv_postorder_cleanup, data))
   1251 		return_0;
   1252 	return 1;
   1253 }
   1254 
   1255 static int _lv_postorder_visit(struct logical_volume *lv,
   1256 			       int (*fn)(struct logical_volume *lv, void *data),
   1257 			       void *data)
   1258 {
   1259 	struct _lv_postorder_baton baton;
   1260 	int r;
   1261 
   1262 	if (lv->status & POSTORDER_FLAG)
   1263 		return 1;
   1264 
   1265 	baton.fn = fn;
   1266 	baton.data = data;
   1267 	r = _lv_each_dependency(lv, _lv_postorder_level, &baton);
   1268 	if (r) {
   1269 		r = fn(lv, data);
   1270 		log_verbose("visited %s", lv->name);
   1271 	}
   1272 	return r;
   1273 }
   1274 
   1275 /*
   1276  * This will walk the LV dependency graph in depth-first order and in the
   1277  * postorder, call a callback function "fn". The void *data is passed along all
   1278  * the calls. The callback may return zero to indicate an error and terminate
   1279  * the depth-first walk. The error is propagated to return value of
   1280  * _lv_postorder.
   1281  */
   1282 static int _lv_postorder(struct logical_volume *lv,
   1283 			       int (*fn)(struct logical_volume *lv, void *data),
   1284 			       void *data)
   1285 {
   1286 	int r;
   1287 	r = _lv_postorder_visit(lv, fn, data);
   1288 	_lv_postorder_cleanup(lv, 0);
   1289 	return r;
   1290 }
   1291 
   1292 struct _lv_mark_if_partial_baton {
   1293 	int partial;
   1294 };
   1295 
   1296 static int _lv_mark_if_partial_collect(struct logical_volume *lv, void *data)
   1297 {
   1298 	struct _lv_mark_if_partial_baton *baton = data;
   1299 	if (lv->status & PARTIAL_LV)
   1300 		baton->partial = 1;
   1301 
   1302 	return 1;
   1303 }
   1304 
   1305 static int _lv_mark_if_partial_single(struct logical_volume *lv, void *data)
   1306 {
   1307 	int s;
   1308 	struct _lv_mark_if_partial_baton baton;
   1309 	struct lv_segment *lvseg;
   1310 
   1311 	dm_list_iterate_items(lvseg, &lv->segments) {
   1312 		for (s = 0; s < lvseg->area_count; ++s) {
   1313 			if (seg_type(lvseg, s) == AREA_PV) {
   1314 				if (seg_pv(lvseg, s)->status & MISSING_PV)
   1315 					lv->status |= PARTIAL_LV;
   1316 			}
   1317 		}
   1318 	}
   1319 
   1320 	baton.partial = 0;
   1321 	_lv_each_dependency(lv, _lv_mark_if_partial_collect, &baton);
   1322 
   1323 	if (baton.partial)
   1324 		lv->status |= PARTIAL_LV;
   1325 
   1326 	return 1;
   1327 }
   1328 
   1329 static int _lv_mark_if_partial(struct logical_volume *lv)
   1330 {
   1331 	return _lv_postorder(lv, _lv_mark_if_partial_single, NULL);
   1332 }
   1333 
   1334 /*
   1335  * Mark LVs with missing PVs using PARTIAL_LV status flag. The flag is
   1336  * propagated transitively, so LVs referencing other LVs are marked
   1337  * partial as well, if any of their referenced LVs are marked partial.
   1338  */
   1339 static int _vg_mark_partial_lvs(struct volume_group *vg)
   1340 {
   1341 	struct logical_volume *lv;
   1342 	struct lv_list *lvl;
   1343 
   1344 	dm_list_iterate_items(lvl, &vg->lvs) {
   1345 		lv = lvl->lv;
   1346 		if (!_lv_mark_if_partial(lv))
   1347 			return_0;
   1348 	}
   1349 	return 1;
   1350 }
   1351 
   1352 int vg_validate(struct volume_group *vg)
   1353 {
   1354 	struct pv_list *pvl, *pvl2;
   1355 	struct lv_list *lvl, *lvl2;
   1356 	char uuid[64] __attribute((aligned(8)));
   1357 	int r = 1;
   1358 	uint32_t lv_count;
   1359 
   1360 	/* FIXME Also check there's no data/metadata overlap */
   1361 
   1362 	dm_list_iterate_items(pvl, &vg->pvs) {
   1363 		dm_list_iterate_items(pvl2, &vg->pvs) {
   1364 			if (pvl == pvl2)
   1365 				break;
   1366 			if (id_equal(&pvl->pv->id,
   1367 				     &pvl2->pv->id)) {
   1368 				if (!id_write_format(&pvl->pv->id, uuid,
   1369 						     sizeof(uuid)))
   1370 					 stack;
   1371 				log_error("Internal error: Duplicate PV id "
   1372 					  "%s detected for %s in %s.",
   1373 					  uuid, pv_dev_name(pvl->pv),
   1374 					  vg->name);
   1375 				r = 0;
   1376 			}
   1377 		}
   1378 
   1379 		if (strcmp(pvl->pv->vg_name, vg->name)) {
   1380 			log_error("Internal error: VG name for PV %s is corrupted",
   1381 				  pv_dev_name(pvl->pv));
   1382 			r = 0;
   1383 		}
   1384 	}
   1385 
   1386 	if (!check_pv_segments(vg)) {
   1387 		log_error("Internal error: PV segments corrupted in %s.",
   1388 			  vg->name);
   1389 		r = 0;
   1390 	}
   1391 
   1392 	if ((lv_count = (uint32_t) dm_list_size(&vg->lvs)) !=
   1393 	    vg->lv_count + 2 * vg->snapshot_count) {
   1394 		log_error("Internal error: #internal LVs (%u) != #LVs (%"
   1395 			  PRIu32 ") + 2 * #snapshots (%" PRIu32 ") in VG %s",
   1396 			  dm_list_size(&vg->lvs), vg->lv_count,
   1397 			  vg->snapshot_count, vg->name);
   1398 		r = 0;
   1399 	}
   1400 
   1401 	dm_list_iterate_items(lvl, &vg->lvs) {
   1402 		dm_list_iterate_items(lvl2, &vg->lvs) {
   1403 			if (lvl == lvl2)
   1404 				break;
   1405 			if (!strcmp(lvl->lv->name, lvl2->lv->name)) {
   1406 				log_error("Internal error: Duplicate LV name "
   1407 					  "%s detected in %s.", lvl->lv->name,
   1408 					  vg->name);
   1409 				r = 0;
   1410 			}
   1411 			if (id_equal(&lvl->lv->lvid.id[1],
   1412 				     &lvl2->lv->lvid.id[1])) {
   1413 				if (!id_write_format(&lvl->lv->lvid.id[1], uuid,
   1414 						     sizeof(uuid)))
   1415 					 stack;
   1416 				log_error("Internal error: Duplicate LV id "
   1417 					  "%s detected for %s and %s in %s.",
   1418 					  uuid, lvl->lv->name, lvl2->lv->name,
   1419 					  vg->name);
   1420 				r = 0;
   1421 			}
   1422 		}
   1423 	}
   1424 
   1425 	dm_list_iterate_items(lvl, &vg->lvs) {
   1426 		if (!check_lv_segments(lvl->lv, 1)) {
   1427 			log_error("Internal error: LV segments corrupted in %s.",
   1428 				  lvl->lv->name);
   1429 			r = 0;
   1430 		}
   1431 	}
   1432 
   1433 	if (!(vg->fid->fmt->features & FMT_UNLIMITED_VOLS) &&
   1434 	    (!vg->max_lv || !vg->max_pv)) {
   1435 		log_error("Internal error: Volume group %s has limited PV/LV count"
   1436 			  " but limit is not set.", vg->name);
   1437 		r = 0;
   1438 	}
   1439 
   1440 	return r;
   1441 }
   1442 
   1443 /*
   1444  * After vg_write() returns success,
   1445  * caller MUST call either vg_commit() or vg_revert()
   1446  */
   1447 int vg_write(struct volume_group *vg)
   1448 {
   1449 	struct dm_list *mdah;
   1450 	struct metadata_area *mda;
   1451 
   1452 	if (!vg_validate(vg))
   1453 		return_0;
   1454 
   1455 	if (vg->status & PARTIAL_VG) {
   1456 		log_error("Cannot update partial volume group %s.", vg->name);
   1457 		return 0;
   1458 	}
   1459 
   1460 	if (vg_missing_pv_count(vg) && !vg->cmd->handles_missing_pvs) {
   1461 		log_error("Cannot update volume group %s while physical "
   1462 			  "volumes are missing.", vg->name);
   1463 		return 0;
   1464 	}
   1465 
   1466 	if (dm_list_empty(&vg->fid->metadata_areas)) {
   1467 		log_error("Aborting vg_write: No metadata areas to write to!");
   1468 		return 0;
   1469 	}
   1470 
   1471 	if (!drop_cached_metadata(vg)) {
   1472 		log_error("Unable to drop cached metadata for VG %s.", vg->name);
   1473 		return 0;
   1474 	}
   1475 
   1476 	vg->seqno++;
   1477 
   1478 	/* Write to each copy of the metadata area */
   1479 	dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1480 		if (!mda->ops->vg_write) {
   1481 			log_error("Format does not support writing volume"
   1482 				  "group metadata areas");
   1483 			/* Revert */
   1484 			dm_list_uniterate(mdah, &vg->fid->metadata_areas, &mda->list) {
   1485 				mda = dm_list_item(mdah, struct metadata_area);
   1486 
   1487 				if (mda->ops->vg_revert &&
   1488 				    !mda->ops->vg_revert(vg->fid, vg, mda)) {
   1489 					stack;
   1490 				}
   1491 			}
   1492 			return 0;
   1493 		}
   1494 		if (!mda->ops->vg_write(vg->fid, vg, mda)) {
   1495 			stack;
   1496 			/* Revert */
   1497 			dm_list_uniterate(mdah, &vg->fid->metadata_areas, &mda->list) {
   1498 				mda = dm_list_item(mdah, struct metadata_area);
   1499 
   1500 				if (mda->ops->vg_revert &&
   1501 				    !mda->ops->vg_revert(vg->fid, vg, mda)) {
   1502 					stack;
   1503 				}
   1504 			}
   1505 			return 0;
   1506 		}
   1507 	}
   1508 
   1509 	/* Now pre-commit each copy of the new metadata */
   1510 	dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1511 		if (mda->ops->vg_precommit &&
   1512 		    !mda->ops->vg_precommit(vg->fid, vg, mda)) {
   1513 			stack;
   1514 			/* Revert */
   1515 			dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1516 				if (mda->ops->vg_revert &&
   1517 				    !mda->ops->vg_revert(vg->fid, vg, mda)) {
   1518 					stack;
   1519 				}
   1520 			}
   1521 			return 0;
   1522 		}
   1523 	}
   1524 
   1525 	return 1;
   1526 }
   1527 
   1528 /* Commit pending changes */
   1529 int vg_commit(struct volume_group *vg)
   1530 {
   1531 	struct metadata_area *mda;
   1532 	int cache_updated = 0;
   1533 	int failed = 0;
   1534 
   1535 	if (!vgname_is_locked(vg->name)) {
   1536 		log_error("Internal error: Attempt to write new VG metadata "
   1537 			  "without locking %s", vg->name);
   1538 		return cache_updated;
   1539 	}
   1540 
   1541 	/* Commit to each copy of the metadata area */
   1542 	dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1543 		failed = 0;
   1544 		if (mda->ops->vg_commit &&
   1545 		    !mda->ops->vg_commit(vg->fid, vg, mda)) {
   1546 			stack;
   1547 			failed = 1;
   1548 		}
   1549 		/* Update cache first time we succeed */
   1550 		if (!failed && !cache_updated) {
   1551 			lvmcache_update_vg(vg, 0);
   1552 			cache_updated = 1;
   1553 		}
   1554 	}
   1555 
   1556 	/* If update failed, remove any cached precommitted metadata. */
   1557 	if (!cache_updated && !drop_cached_metadata(vg))
   1558 		log_error("Attempt to drop cached metadata failed "
   1559 			  "after commit for VG %s.", vg->name);
   1560 
   1561 	/* If at least one mda commit succeeded, it was committed */
   1562 	return cache_updated;
   1563 }
   1564 
   1565 /* Don't commit any pending changes */
   1566 int vg_revert(struct volume_group *vg)
   1567 {
   1568 	struct metadata_area *mda;
   1569 
   1570 	dm_list_iterate_items(mda, &vg->fid->metadata_areas) {
   1571 		if (mda->ops->vg_revert &&
   1572 		    !mda->ops->vg_revert(vg->fid, vg, mda)) {
   1573 			stack;
   1574 		}
   1575 	}
   1576 
   1577 	if (!drop_cached_metadata(vg))
   1578 		log_error("Attempt to drop cached metadata failed "
   1579 			  "after reverted update for VG %s.", vg->name);
   1580 
   1581 	return 1;
   1582 }
   1583 
   1584 /* Make orphan PVs look like a VG */
   1585 static struct volume_group *_vg_read_orphans(struct cmd_context *cmd,
   1586 					     const char *orphan_vgname)
   1587 {
   1588 	struct lvmcache_vginfo *vginfo;
   1589 	struct lvmcache_info *info;
   1590 	struct pv_list *pvl;
   1591 	struct volume_group *vg;
   1592 	struct physical_volume *pv;
   1593 
   1594 	lvmcache_label_scan(cmd, 0);
   1595 
   1596 	if (!(vginfo = vginfo_from_vgname(orphan_vgname, NULL)))
   1597 		return_NULL;
   1598 
   1599 	if (!(vg = dm_pool_zalloc(cmd->mem, sizeof(*vg)))) {
   1600 		log_error("vg allocation failed");
   1601 		return NULL;
   1602 	}
   1603 	dm_list_init(&vg->pvs);
   1604 	dm_list_init(&vg->lvs);
   1605 	dm_list_init(&vg->tags);
   1606 	vg->cmd = cmd;
   1607 	if (!(vg->name = dm_pool_strdup(cmd->mem, orphan_vgname))) {
   1608 		log_error("vg name allocation failed");
   1609 		return NULL;
   1610 	}
   1611 
   1612 	/* create format instance with appropriate metadata area */
   1613 	if (!(vg->fid = vginfo->fmt->ops->create_instance(vginfo->fmt,
   1614 							  orphan_vgname, NULL,
   1615 							  NULL))) {
   1616 		log_error("Failed to create format instance");
   1617 		dm_pool_free(cmd->mem, vg);
   1618 		return NULL;
   1619 	}
   1620 
   1621 	dm_list_iterate_items(info, &vginfo->infos) {
   1622 		if (!(pv = _pv_read(cmd, dev_name(info->dev), NULL, NULL, 1))) {
   1623 			continue;
   1624 		}
   1625 		if (!(pvl = dm_pool_zalloc(cmd->mem, sizeof(*pvl)))) {
   1626 			log_error("pv_list allocation failed");
   1627 			return NULL;
   1628 		}
   1629 		pvl->pv = pv;
   1630 		dm_list_add(&vg->pvs, &pvl->list);
   1631 		vg->pv_count++;
   1632 	}
   1633 
   1634 	return vg;
   1635 }
   1636 
   1637 static int _update_pv_list(struct dm_list *all_pvs, struct volume_group *vg)
   1638 {
   1639 	struct pv_list *pvl, *pvl2;
   1640 
   1641 	dm_list_iterate_items(pvl, &vg->pvs) {
   1642 		dm_list_iterate_items(pvl2, all_pvs) {
   1643 			if (pvl->pv->dev == pvl2->pv->dev)
   1644 				goto next_pv;
   1645 		}
   1646 		/* PV is not on list so add it.  Note that we don't copy it. */
   1647        		if (!(pvl2 = dm_pool_zalloc(vg->cmd->mem, sizeof(*pvl2)))) {
   1648 			log_error("pv_list allocation for '%s' failed",
   1649 				  pv_dev_name(pvl->pv));
   1650 			return 0;
   1651 		}
   1652 		pvl2->pv = pvl->pv;
   1653 		dm_list_add(all_pvs, &pvl2->list);
   1654   next_pv:
   1655 		;
   1656 	}
   1657 
   1658 	return 1;
   1659 }
   1660 
   1661 int vg_missing_pv_count(const vg_t *vg)
   1662 {
   1663 	int ret = 0;
   1664 	struct pv_list *pvl;
   1665 	dm_list_iterate_items(pvl, &vg->pvs) {
   1666 		if (pvl->pv->status & MISSING_PV)
   1667 			++ ret;
   1668 	}
   1669 	return ret;
   1670 }
   1671 
   1672 /* Caller sets consistent to 1 if it's safe for vg_read to correct
   1673  * inconsistent metadata on disk (i.e. the VG write lock is held).
   1674  * This guarantees only consistent metadata is returned.
   1675  * If consistent is 0, caller must check whether consistent == 1 on return
   1676  * and take appropriate action if it isn't (e.g. abort; get write lock
   1677  * and call vg_read again).
   1678  *
   1679  * If precommitted is set, use precommitted metadata if present.
   1680  *
   1681  * Either of vgname or vgid may be NULL.
   1682  */
   1683 static struct volume_group *_vg_read(struct cmd_context *cmd,
   1684 				     const char *vgname,
   1685 				     const char *vgid,
   1686 				     int *consistent, unsigned precommitted)
   1687 {
   1688 	struct format_instance *fid;
   1689 	const struct format_type *fmt;
   1690 	struct volume_group *vg, *correct_vg = NULL;
   1691 	struct metadata_area *mda;
   1692 	struct lvmcache_info *info;
   1693 	int inconsistent = 0;
   1694 	int inconsistent_vgid = 0;
   1695 	int inconsistent_pvs = 0;
   1696 	unsigned use_precommitted = precommitted;
   1697 	struct dm_list *pvids;
   1698 	struct pv_list *pvl, *pvl2;
   1699 	struct dm_list all_pvs;
   1700 	char uuid[64] __attribute((aligned(8)));
   1701 
   1702 	if (is_orphan_vg(vgname)) {
   1703 		if (use_precommitted) {
   1704 			log_error("Internal error: vg_read requires vgname "
   1705 				  "with pre-commit.");
   1706 			return NULL;
   1707 		}
   1708 		*consistent = 1;
   1709 		return _vg_read_orphans(cmd, vgname);
   1710 	}
   1711 
   1712 	if ((correct_vg = lvmcache_get_vg(vgid, precommitted))) {
   1713 		if (vg_missing_pv_count(correct_vg)) {
   1714 			log_verbose("There are %d physical volumes missing.",
   1715 				    vg_missing_pv_count(correct_vg));
   1716 			_vg_mark_partial_lvs(correct_vg);
   1717 		}
   1718 		*consistent = 1;
   1719 		return correct_vg;
   1720 	}
   1721 
   1722 	/* Find the vgname in the cache */
   1723 	/* If it's not there we must do full scan to be completely sure */
   1724 	if (!(fmt = fmt_from_vgname(vgname, vgid))) {
   1725 		lvmcache_label_scan(cmd, 0);
   1726 		if (!(fmt = fmt_from_vgname(vgname, vgid))) {
   1727 			if (memlock())
   1728 				return_NULL;
   1729 			lvmcache_label_scan(cmd, 2);
   1730 			if (!(fmt = fmt_from_vgname(vgname, vgid)))
   1731 				return_NULL;
   1732 		}
   1733 	}
   1734 
   1735 	/* Now determine the correct vgname if none was supplied */
   1736 	if (!vgname && !(vgname = vgname_from_vgid(cmd->mem, vgid)))
   1737 		return_NULL;
   1738 
   1739 	if (use_precommitted && !(fmt->features & FMT_PRECOMMIT))
   1740 		use_precommitted = 0;
   1741 
   1742 	/* create format instance with appropriate metadata area */
   1743 	if (!(fid = fmt->ops->create_instance(fmt, vgname, vgid, NULL))) {
   1744 		log_error("Failed to create format instance");
   1745 		return NULL;
   1746 	}
   1747 
   1748 	/* Store pvids for later so we can check if any are missing */
   1749 	if (!(pvids = lvmcache_get_pvids(cmd, vgname, vgid)))
   1750 		return_NULL;
   1751 
   1752 	/* Ensure contents of all metadata areas match - else do recovery */
   1753 	dm_list_iterate_items(mda, &fid->metadata_areas) {
   1754 		if ((use_precommitted &&
   1755 		     !(vg = mda->ops->vg_read_precommit(fid, vgname, mda))) ||
   1756 		    (!use_precommitted &&
   1757 		     !(vg = mda->ops->vg_read(fid, vgname, mda)))) {
   1758 			inconsistent = 1;
   1759 			continue;
   1760 		}
   1761 		if (!correct_vg) {
   1762 			correct_vg = vg;
   1763 			continue;
   1764 		}
   1765 		/* FIXME Also ensure contents same - checksum compare? */
   1766 		if (correct_vg->seqno != vg->seqno) {
   1767 			inconsistent = 1;
   1768 			if (vg->seqno > correct_vg->seqno)
   1769 				correct_vg = vg;
   1770 		}
   1771 	}
   1772 
   1773 	/* Ensure every PV in the VG was in the cache */
   1774 	if (correct_vg) {
   1775 		/*
   1776 		 * If the VG has PVs without mdas, they may still be
   1777 		 * orphans in the cache: update the cache state here.
   1778 		 */
   1779 		if (!inconsistent &&
   1780 		    dm_list_size(&correct_vg->pvs) > dm_list_size(pvids)) {
   1781 			dm_list_iterate_items(pvl, &correct_vg->pvs) {
   1782 				if (!pvl->pv->dev) {
   1783 					inconsistent_pvs = 1;
   1784 					break;
   1785 				}
   1786 
   1787 				if (str_list_match_item(pvids, pvl->pv->dev->pvid))
   1788 					continue;
   1789 
   1790 				/*
   1791 				 * PV not marked as belonging to this VG in cache.
   1792 				 * Check it's an orphan without metadata area.
   1793 				 */
   1794 				if (!(info = info_from_pvid(pvl->pv->dev->pvid, 1)) ||
   1795 				   !info->vginfo || !is_orphan_vg(info->vginfo->vgname) ||
   1796 				   dm_list_size(&info->mdas)) {
   1797 					inconsistent_pvs = 1;
   1798 					break;
   1799 				}
   1800 			}
   1801 
   1802 			/* If the check passed, let's update VG and recalculate pvids */
   1803 			if (!inconsistent_pvs) {
   1804 				log_debug("Updating cache for PVs without mdas "
   1805 					  "in VG %s.", vgname);
   1806 				lvmcache_update_vg(correct_vg, use_precommitted);
   1807 
   1808 				if (!(pvids = lvmcache_get_pvids(cmd, vgname, vgid)))
   1809 					return_NULL;
   1810 			}
   1811 		}
   1812 
   1813 		if (dm_list_size(&correct_vg->pvs) != dm_list_size(pvids)
   1814 		    + vg_missing_pv_count(correct_vg)) {
   1815 			log_debug("Cached VG %s had incorrect PV list",
   1816 				  vgname);
   1817 
   1818 			if (memlock())
   1819 				inconsistent = 1;
   1820 			else
   1821 				correct_vg = NULL;
   1822 		} else dm_list_iterate_items(pvl, &correct_vg->pvs) {
   1823 			if (pvl->pv->status & MISSING_PV)
   1824 				continue;
   1825 			if (!str_list_match_item(pvids, pvl->pv->dev->pvid)) {
   1826 				log_debug("Cached VG %s had incorrect PV list",
   1827 					  vgname);
   1828 				correct_vg = NULL;
   1829 				break;
   1830 			}
   1831 		}
   1832 	}
   1833 
   1834 	dm_list_init(&all_pvs);
   1835 
   1836 	/* Failed to find VG where we expected it - full scan and retry */
   1837 	if (!correct_vg) {
   1838 		inconsistent = 0;
   1839 
   1840 		if (memlock())
   1841 			return_NULL;
   1842 		lvmcache_label_scan(cmd, 2);
   1843 		if (!(fmt = fmt_from_vgname(vgname, vgid)))
   1844 			return_NULL;
   1845 
   1846 		if (precommitted && !(fmt->features & FMT_PRECOMMIT))
   1847 			use_precommitted = 0;
   1848 
   1849 		/* create format instance with appropriate metadata area */
   1850 		if (!(fid = fmt->ops->create_instance(fmt, vgname, vgid, NULL))) {
   1851 			log_error("Failed to create format instance");
   1852 			return NULL;
   1853 		}
   1854 
   1855 		/* Ensure contents of all metadata areas match - else recover */
   1856 		dm_list_iterate_items(mda, &fid->metadata_areas) {
   1857 			if ((use_precommitted &&
   1858 			     !(vg = mda->ops->vg_read_precommit(fid, vgname,
   1859 								mda))) ||
   1860 			    (!use_precommitted &&
   1861 			     !(vg = mda->ops->vg_read(fid, vgname, mda)))) {
   1862 				inconsistent = 1;
   1863 				continue;
   1864 			}
   1865 			if (!correct_vg) {
   1866 				correct_vg = vg;
   1867 				if (!_update_pv_list(&all_pvs, correct_vg))
   1868 					return_NULL;
   1869 				continue;
   1870 			}
   1871 
   1872 			if (strncmp((char *)vg->id.uuid,
   1873 			    (char *)correct_vg->id.uuid, ID_LEN)) {
   1874 				inconsistent = 1;
   1875 				inconsistent_vgid = 1;
   1876 			}
   1877 
   1878 			/* FIXME Also ensure contents same - checksums same? */
   1879 			if (correct_vg->seqno != vg->seqno) {
   1880 				inconsistent = 1;
   1881 				if (!_update_pv_list(&all_pvs, vg))
   1882 					return_NULL;
   1883 				if (vg->seqno > correct_vg->seqno)
   1884 					correct_vg = vg;
   1885 			}
   1886 		}
   1887 
   1888 		/* Give up looking */
   1889 		if (!correct_vg)
   1890 			return_NULL;
   1891 	}
   1892 
   1893 	lvmcache_update_vg(correct_vg, use_precommitted);
   1894 
   1895 	if (inconsistent) {
   1896 		/* FIXME Test should be if we're *using* precommitted metadata not if we were searching for it */
   1897 		if (use_precommitted) {
   1898 			log_error("Inconsistent pre-commit metadata copies "
   1899 				  "for volume group %s", vgname);
   1900 			return NULL;
   1901 		}
   1902 
   1903 		if (!*consistent)
   1904 			return correct_vg;
   1905 
   1906 		/* Don't touch if vgids didn't match */
   1907 		if (inconsistent_vgid) {
   1908 			log_error("Inconsistent metadata UUIDs found for "
   1909 				  "volume group %s", vgname);
   1910 			*consistent = 0;
   1911 			return correct_vg;
   1912 		}
   1913 
   1914 		log_warn("WARNING: Inconsistent metadata found for VG %s - updating "
   1915 			 "to use version %u", vgname, correct_vg->seqno);
   1916 
   1917 		if (!vg_write(correct_vg)) {
   1918 			log_error("Automatic metadata correction failed");
   1919 			return NULL;
   1920 		}
   1921 
   1922 		if (!vg_commit(correct_vg)) {
   1923 			log_error("Automatic metadata correction commit "
   1924 				  "failed");
   1925 			return NULL;
   1926 		}
   1927 
   1928 		dm_list_iterate_items(pvl, &all_pvs) {
   1929 			dm_list_iterate_items(pvl2, &correct_vg->pvs) {
   1930 				if (pvl->pv->dev == pvl2->pv->dev)
   1931 					goto next_pv;
   1932 			}
   1933 			if (!id_write_format(&pvl->pv->id, uuid, sizeof(uuid)))
   1934 				return_NULL;
   1935 			log_error("Removing PV %s (%s) that no longer belongs to VG %s",
   1936 				  pv_dev_name(pvl->pv), uuid, correct_vg->name);
   1937 			if (!pv_write_orphan(cmd, pvl->pv))
   1938 				return_NULL;
   1939       next_pv:
   1940 			;
   1941 		}
   1942 	}
   1943 
   1944 	if (vg_missing_pv_count(correct_vg)) {
   1945 		log_verbose("There are %d physical volumes missing.",
   1946 			    vg_missing_pv_count(correct_vg));
   1947 		_vg_mark_partial_lvs(correct_vg);
   1948 	}
   1949 
   1950 	if ((correct_vg->status & PVMOVE) && !pvmove_mode()) {
   1951 		log_error("WARNING: Interrupted pvmove detected in "
   1952 			  "volume group %s", correct_vg->name);
   1953 		log_error("Please restore the metadata by running "
   1954 			  "vgcfgrestore.");
   1955 		return NULL;
   1956 	}
   1957 
   1958 	*consistent = 1;
   1959 	return correct_vg;
   1960 }
   1961 
   1962 struct volume_group *vg_read(struct cmd_context *cmd, const char *vgname,
   1963 			     const char *vgid, int *consistent)
   1964 {
   1965 	struct volume_group *vg;
   1966 	struct lv_list *lvl;
   1967 
   1968 	if (!(vg = _vg_read(cmd, vgname, vgid, consistent, 0)))
   1969 		return NULL;
   1970 
   1971 	if (!check_pv_segments(vg)) {
   1972 		log_error("Internal error: PV segments corrupted in %s.",
   1973 			  vg->name);
   1974 		return NULL;
   1975 	}
   1976 
   1977 	dm_list_iterate_items(lvl, &vg->lvs) {
   1978 		if (!check_lv_segments(lvl->lv, 1)) {
   1979 			log_error("Internal error: LV segments corrupted in %s.",
   1980 				  lvl->lv->name);
   1981 			return NULL;
   1982 		}
   1983 	}
   1984 
   1985 	return vg;
   1986 }
   1987 
   1988 /* This is only called by lv_from_lvid, which is only called from
   1989  * activate.c so we know the appropriate VG lock is already held and
   1990  * the vg_read is therefore safe.
   1991  */
   1992 static struct volume_group *_vg_read_by_vgid(struct cmd_context *cmd,
   1993 					    const char *vgid,
   1994 					    unsigned precommitted)
   1995 {
   1996 	const char *vgname;
   1997 	struct dm_list *vgnames;
   1998 	struct volume_group *vg;
   1999 	struct lvmcache_vginfo *vginfo;
   2000 	struct str_list *strl;
   2001 	int consistent = 0;
   2002 
   2003 	/* Is corresponding vgname already cached? */
   2004 	if ((vginfo = vginfo_from_vgid(vgid)) &&
   2005 	    vginfo->vgname && !is_orphan_vg(vginfo->vgname)) {
   2006 		if ((vg = _vg_read(cmd, NULL, vgid,
   2007 				   &consistent, precommitted)) &&
   2008 		    !strncmp((char *)vg->id.uuid, vgid, ID_LEN)) {
   2009 
   2010 			if (!consistent) {
   2011 				log_error("Volume group %s metadata is "
   2012 					  "inconsistent", vg->name);
   2013 			}
   2014 			return vg;
   2015 		}
   2016 	}
   2017 
   2018 	/* Mustn't scan if memory locked: ensure cache gets pre-populated! */
   2019 	if (memlock())
   2020 		return NULL;
   2021 
   2022 	/* FIXME Need a genuine read by ID here - don't vg_read by name! */
   2023 	/* FIXME Disabled vgrenames while active for now because we aren't
   2024 	 *       allowed to do a full scan here any more. */
   2025 
   2026 	// The slow way - full scan required to cope with vgrename
   2027 	if (!(vgnames = get_vgs(cmd, 2))) {
   2028 		log_error("vg_read_by_vgid: get_vgs failed");
   2029 		return NULL;
   2030 	}
   2031 
   2032 	dm_list_iterate_items(strl, vgnames) {
   2033 		vgname = strl->str;
   2034 		if (!vgname || is_orphan_vg(vgname))
   2035 			continue;	// FIXME Unnecessary?
   2036 		consistent = 0;
   2037 		if ((vg = _vg_read(cmd, vgname, vgid, &consistent,
   2038 				   precommitted)) &&
   2039 		    !strncmp((char *)vg->id.uuid, vgid, ID_LEN)) {
   2040 
   2041 			if (!consistent) {
   2042 				log_error("Volume group %s metadata is "
   2043 					  "inconsistent", vgname);
   2044 				return NULL;
   2045 			}
   2046 			return vg;
   2047 		}
   2048 	}
   2049 
   2050 	return NULL;
   2051 }
   2052 
   2053 /* Only called by activate.c */
   2054 struct logical_volume *lv_from_lvid(struct cmd_context *cmd, const char *lvid_s,
   2055 				    unsigned precommitted)
   2056 {
   2057 	struct lv_list *lvl;
   2058 	struct volume_group *vg;
   2059 	const union lvid *lvid;
   2060 
   2061 	lvid = (const union lvid *) lvid_s;
   2062 
   2063 	log_very_verbose("Finding volume group for uuid %s", lvid_s);
   2064 	if (!(vg = _vg_read_by_vgid(cmd, (char *)lvid->id[0].uuid, precommitted))) {
   2065 		log_error("Volume group for uuid not found: %s", lvid_s);
   2066 		return NULL;
   2067 	}
   2068 
   2069 	log_verbose("Found volume group \"%s\"", vg->name);
   2070 	if (vg->status & EXPORTED_VG) {
   2071 		log_error("Volume group \"%s\" is exported", vg->name);
   2072 		return NULL;
   2073 	}
   2074 	if (!(lvl = find_lv_in_vg_by_lvid(vg, lvid))) {
   2075 		log_very_verbose("Can't find logical volume id %s", lvid_s);
   2076 		return NULL;
   2077 	}
   2078 
   2079 	return lvl->lv;
   2080 }
   2081 
   2082 /**
   2083  * pv_read - read and return a handle to a physical volume
   2084  * @cmd: LVM command initiating the pv_read
   2085  * @pv_name: full device name of the PV, including the path
   2086  * @mdas: list of metadata areas of the PV
   2087  * @label_sector: sector number where the PV label is stored on @pv_name
   2088  * @warnings:
   2089  *
   2090  * Returns:
   2091  *   PV handle - valid pv_name and successful read of the PV, or
   2092  *   NULL - invalid parameter or error in reading the PV
   2093  *
   2094  * Note:
   2095  *   FIXME - liblvm todo - make into function that returns handle
   2096  */
   2097 struct physical_volume *pv_read(struct cmd_context *cmd, const char *pv_name,
   2098 				struct dm_list *mdas, uint64_t *label_sector,
   2099 				int warnings)
   2100 {
   2101 	return _pv_read(cmd, pv_name, mdas, label_sector, warnings);
   2102 }
   2103 
   2104 /* FIXME Use label functions instead of PV functions */
   2105 static struct physical_volume *_pv_read(struct cmd_context *cmd,
   2106 					const char *pv_name,
   2107 					struct dm_list *mdas,
   2108 					uint64_t *label_sector,
   2109 					int warnings)
   2110 {
   2111 	struct physical_volume *pv;
   2112 	struct label *label;
   2113 	struct lvmcache_info *info;
   2114 	struct device *dev;
   2115 
   2116 	if (!(dev = dev_cache_get(pv_name, cmd->filter)))
   2117 		return_NULL;
   2118 
   2119 	if (!(label_read(dev, &label, UINT64_C(0)))) {
   2120 		if (warnings)
   2121 			log_error("No physical volume label read from %s",
   2122 				  pv_name);
   2123 		return NULL;
   2124 	}
   2125 
   2126 	info = (struct lvmcache_info *) label->info;
   2127 	if (label_sector && *label_sector)
   2128 		*label_sector = label->sector;
   2129 
   2130 	if (!(pv = dm_pool_zalloc(cmd->mem, sizeof(*pv)))) {
   2131 		log_error("pv allocation for '%s' failed", pv_name);
   2132 		return NULL;
   2133 	}
   2134 
   2135 	dm_list_init(&pv->tags);
   2136 	dm_list_init(&pv->segments);
   2137 
   2138 	/* FIXME Move more common code up here */
   2139 	if (!(info->fmt->ops->pv_read(info->fmt, pv_name, pv, mdas))) {
   2140 		log_error("Failed to read existing physical volume '%s'",
   2141 			  pv_name);
   2142 		return NULL;
   2143 	}
   2144 
   2145 	if (!pv->size)
   2146 		return NULL;
   2147 
   2148 	if (!alloc_pv_segment_whole_pv(cmd->mem, pv))
   2149 		return_NULL;
   2150 
   2151 	return pv;
   2152 }
   2153 
   2154 /* May return empty list */
   2155 struct dm_list *get_vgs(struct cmd_context *cmd, int full_scan)
   2156 {
   2157 	return lvmcache_get_vgnames(cmd, full_scan);
   2158 }
   2159 
   2160 struct dm_list *get_vgids(struct cmd_context *cmd, int full_scan)
   2161 {
   2162 	return lvmcache_get_vgids(cmd, full_scan);
   2163 }
   2164 
   2165 static int _get_pvs(struct cmd_context *cmd, struct dm_list **pvslist)
   2166 {
   2167 	struct str_list *strl;
   2168 	struct dm_list * uninitialized_var(results);
   2169 	const char *vgname, *vgid;
   2170 	struct dm_list *pvh, *tmp;
   2171 	struct dm_list *vgids;
   2172 	struct volume_group *vg;
   2173 	int consistent = 0;
   2174 	int old_pvmove;
   2175 
   2176 	lvmcache_label_scan(cmd, 0);
   2177 
   2178 	if (pvslist) {
   2179 		if (!(results = dm_pool_alloc(cmd->mem, sizeof(*results)))) {
   2180 			log_error("PV list allocation failed");
   2181 			return 0;
   2182 		}
   2183 
   2184 		dm_list_init(results);
   2185 	}
   2186 
   2187 	/* Get list of VGs */
   2188 	if (!(vgids = get_vgids(cmd, 0))) {
   2189 		log_error("get_pvs: get_vgs failed");
   2190 		return 0;
   2191 	}
   2192 
   2193 	/* Read every VG to ensure cache consistency */
   2194 	/* Orphan VG is last on list */
   2195 	old_pvmove = pvmove_mode();
   2196 	init_pvmove(1);
   2197 	dm_list_iterate_items(strl, vgids) {
   2198 		vgid = strl->str;
   2199 		if (!vgid)
   2200 			continue;	/* FIXME Unnecessary? */
   2201 		consistent = 0;
   2202 		if (!(vgname = vgname_from_vgid(NULL, vgid))) {
   2203 			stack;
   2204 			continue;
   2205 		}
   2206 		if (!(vg = vg_read(cmd, vgname, vgid, &consistent))) {
   2207 			stack;
   2208 			continue;
   2209 		}
   2210 		if (!consistent)
   2211 			log_warn("WARNING: Volume Group %s is not consistent",
   2212 				 vgname);
   2213 
   2214 		/* Move PVs onto results list */
   2215 		if (pvslist)
   2216 			dm_list_iterate_safe(pvh, tmp, &vg->pvs)
   2217 				dm_list_add(results, pvh);
   2218 	}
   2219 	init_pvmove(old_pvmove);
   2220 
   2221 	if (pvslist)
   2222 		*pvslist = results;
   2223 	else
   2224 		dm_pool_free(cmd->mem, vgids);
   2225 
   2226 	return 1;
   2227 }
   2228 
   2229 struct dm_list *get_pvs(struct cmd_context *cmd)
   2230 {
   2231 	struct dm_list *results;
   2232 
   2233 	if (!_get_pvs(cmd, &results))
   2234 		return NULL;
   2235 
   2236 	return results;
   2237 }
   2238 
   2239 int scan_vgs_for_pvs(struct cmd_context *cmd)
   2240 {
   2241 	return _get_pvs(cmd, NULL);
   2242 }
   2243 
   2244 /* FIXME: liblvm todo - make into function that takes handle */
   2245 int pv_write(struct cmd_context *cmd __attribute((unused)),
   2246 	     struct physical_volume *pv,
   2247 	     struct dm_list *mdas, int64_t label_sector)
   2248 {
   2249 	return _pv_write(cmd, pv, mdas, label_sector);
   2250 }
   2251 
   2252 static int _pv_write(struct cmd_context *cmd __attribute((unused)),
   2253 		     struct physical_volume *pv,
   2254 	     	     struct dm_list *mdas, int64_t label_sector)
   2255 {
   2256 	if (!pv->fmt->ops->pv_write) {
   2257 		log_error("Format does not support writing physical volumes");
   2258 		return 0;
   2259 	}
   2260 
   2261 	if (!is_orphan_vg(pv->vg_name) || pv->pe_alloc_count) {
   2262 		log_error("Assertion failed: can't _pv_write non-orphan PV "
   2263 			  "(in VG %s)", pv->vg_name);
   2264 		return 0;
   2265 	}
   2266 
   2267 	if (!pv->fmt->ops->pv_write(pv->fmt, pv, mdas, label_sector))
   2268 		return_0;
   2269 
   2270 	return 1;
   2271 }
   2272 
   2273 int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv)
   2274 {
   2275 	const char *old_vg_name = pv->vg_name;
   2276 
   2277 	pv->vg_name = cmd->fmt->orphan_vg_name;
   2278 	pv->status = ALLOCATABLE_PV;
   2279 	pv->pe_alloc_count = 0;
   2280 
   2281 	if (!dev_get_size(pv->dev, &pv->size)) {
   2282 		log_error("%s: Couldn't get size.", pv_dev_name(pv));
   2283 		return 0;
   2284 	}
   2285 
   2286 	if (!_pv_write(cmd, pv, NULL, INT64_C(-1))) {
   2287 		log_error("Failed to clear metadata from physical "
   2288 			  "volume \"%s\" after removal from \"%s\"",
   2289 			  pv_dev_name(pv), old_vg_name);
   2290 		return 0;
   2291 	}
   2292 
   2293 	return 1;
   2294 }
   2295 
   2296 /**
   2297  * is_orphan_vg - Determine whether a vg_name is an orphan
   2298  * @vg_name: pointer to the vg_name
   2299  */
   2300 int is_orphan_vg(const char *vg_name)
   2301 {
   2302 	return (vg_name && vg_name[0] == ORPHAN_PREFIX[0]) ? 1 : 0;
   2303 }
   2304 
   2305 /**
   2306  * is_orphan - Determine whether a pv is an orphan based on its vg_name
   2307  * @pv: handle to the physical volume
   2308  */
   2309 int is_orphan(const pv_t *pv)
   2310 {
   2311 	return is_orphan_vg(pv_field(pv, vg_name));
   2312 }
   2313 
   2314 /**
   2315  * is_pv - Determine whether a pv is a real pv or dummy one
   2316  * @pv: handle to device
   2317  */
   2318 int is_pv(pv_t *pv)
   2319 {
   2320 	return (pv_field(pv, vg_name) ? 1 : 0);
   2321 }
   2322 
   2323 /*
   2324  * Returns:
   2325  *  0 - fail
   2326  *  1 - success
   2327  */
   2328 int pv_analyze(struct cmd_context *cmd, const char *pv_name,
   2329 	       uint64_t label_sector)
   2330 {
   2331 	struct label *label;
   2332 	struct device *dev;
   2333 	struct metadata_area *mda;
   2334 	struct lvmcache_info *info;
   2335 
   2336 	dev = dev_cache_get(pv_name, cmd->filter);
   2337 	if (!dev) {
   2338 		log_error("Device %s not found (or ignored by filtering).",
   2339 			  pv_name);
   2340 		return 0;
   2341 	}
   2342 
   2343 	/*
   2344 	 * First, scan for LVM labels.
   2345 	 */
   2346 	if (!label_read(dev, &label, label_sector)) {
   2347 		log_error("Could not find LVM label on %s",
   2348 			  pv_name);
   2349 		return 0;
   2350 	}
   2351 
   2352 	log_print("Found label on %s, sector %"PRIu64", type=%s",
   2353 		  pv_name, label->sector, label->type);
   2354 
   2355 	/*
   2356 	 * Next, loop through metadata areas
   2357 	 */
   2358 	info = label->info;
   2359 	dm_list_iterate_items(mda, &info->mdas)
   2360 		mda->ops->pv_analyze_mda(info->fmt, mda);
   2361 
   2362 	return 1;
   2363 }
   2364 
   2365 
   2366 
   2367 /**
   2368  * vg_check_status - check volume group status flags and log error
   2369  * @vg - volume group to check status flags
   2370  * @status - specific status flags to check (e.g. EXPORTED_VG)
   2371  *
   2372  * Returns:
   2373  * 0 - fail
   2374  * 1 - success
   2375  */
   2376 int vg_check_status(const struct volume_group *vg, uint32_t status)
   2377 {
   2378 	if ((status & CLUSTERED) &&
   2379 	    (vg_is_clustered(vg)) && !locking_is_clustered() &&
   2380 	    !lockingfailed()) {
   2381 		log_error("Skipping clustered volume group %s", vg->name);
   2382 		return 0;
   2383 	}
   2384 
   2385 	if ((status & EXPORTED_VG) &&
   2386 	    (vg->status & EXPORTED_VG)) {
   2387 		log_error("Volume group %s is exported", vg->name);
   2388 		return 0;
   2389 	}
   2390 
   2391 	if ((status & LVM_WRITE) &&
   2392 	    !(vg->status & LVM_WRITE)) {
   2393 		log_error("Volume group %s is read-only", vg->name);
   2394 		return 0;
   2395 	}
   2396 	if ((status & RESIZEABLE_VG) &&
   2397 	    !(vg->status & RESIZEABLE_VG)) {
   2398 		log_error("Volume group %s is not resizeable.", vg->name);
   2399 		return 0;
   2400 	}
   2401 
   2402 	return 1;
   2403 }
   2404 
   2405 /*
   2406  * vg_lock_and_read - consolidate vg locking, reading, and status flag checking
   2407  *
   2408  * Returns:
   2409  * NULL - failure
   2410  * non-NULL - success; volume group handle
   2411  */
   2412 vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name,
   2413 		       const char *vgid,
   2414 		       uint32_t lock_flags, uint32_t status_flags,
   2415 		       uint32_t misc_flags)
   2416 {
   2417 	struct volume_group *vg;
   2418 	int consistent = 1;
   2419 
   2420 	if (!(misc_flags & CORRECT_INCONSISTENT))
   2421 		consistent = 0;
   2422 
   2423 	if (!validate_name(vg_name)) {
   2424 		log_error("Volume group name %s has invalid characters",
   2425 			  vg_name);
   2426 		return NULL;
   2427 	}
   2428 
   2429 	if (!lock_vol(cmd, vg_name, lock_flags)) {
   2430 		log_error("Can't get lock for %s", vg_name);
   2431 		return NULL;
   2432 	}
   2433 
   2434 	if (!(vg = vg_read(cmd, vg_name, vgid, &consistent)) ||
   2435 	    ((misc_flags & FAIL_INCONSISTENT) && !consistent)) {
   2436 		log_error("Volume group \"%s\" not found", vg_name);
   2437 		unlock_vg(cmd, vg_name);
   2438 		return NULL;
   2439 	}
   2440 
   2441 	if (!vg_check_status(vg, status_flags)) {
   2442 		unlock_vg(cmd, vg_name);
   2443 		return NULL;
   2444 	}
   2445 
   2446 	return vg;
   2447 }
   2448 
   2449 /*
   2450  * Gets/Sets for external LVM library
   2451  */
   2452 struct id pv_id(const pv_t *pv)
   2453 {
   2454 	return pv_field(pv, id);
   2455 }
   2456 
   2457 const struct format_type *pv_format_type(const pv_t *pv)
   2458 {
   2459 	return pv_field(pv, fmt);
   2460 }
   2461 
   2462 struct id pv_vgid(const pv_t *pv)
   2463 {
   2464 	return pv_field(pv, vgid);
   2465 }
   2466 
   2467 struct device *pv_dev(const pv_t *pv)
   2468 {
   2469 	return pv_field(pv, dev);
   2470 }
   2471 
   2472 const char *pv_vg_name(const pv_t *pv)
   2473 {
   2474 	return pv_field(pv, vg_name);
   2475 }
   2476 
   2477 const char *pv_dev_name(const pv_t *pv)
   2478 {
   2479 	return dev_name(pv_dev(pv));
   2480 }
   2481 
   2482 uint64_t pv_size(const pv_t *pv)
   2483 {
   2484 	return pv_field(pv, size);
   2485 }
   2486 
   2487 uint32_t pv_status(const pv_t *pv)
   2488 {
   2489 	return pv_field(pv, status);
   2490 }
   2491 
   2492 uint32_t pv_pe_size(const pv_t *pv)
   2493 {
   2494 	return pv_field(pv, pe_size);
   2495 }
   2496 
   2497 uint64_t pv_pe_start(const pv_t *pv)
   2498 {
   2499 	return pv_field(pv, pe_start);
   2500 }
   2501 
   2502 uint32_t pv_pe_count(const pv_t *pv)
   2503 {
   2504 	return pv_field(pv, pe_count);
   2505 }
   2506 
   2507 uint32_t pv_pe_alloc_count(const pv_t *pv)
   2508 {
   2509 	return pv_field(pv, pe_alloc_count);
   2510 }
   2511 
   2512 uint32_t vg_status(const vg_t *vg)
   2513 {
   2514 	return vg->status;
   2515 }
   2516 
   2517 /**
   2518  * pv_by_path - Given a device path return a PV handle if it is a PV
   2519  * @cmd - handle to the LVM command instance
   2520  * @pv_name - device path to read for the PV
   2521  *
   2522  * Returns:
   2523  *  NULL - device path does not contain a valid PV
   2524  *  non-NULL - PV handle corresponding to device path
   2525  *
   2526  * FIXME: merge with find_pv_by_name ?
   2527  */
   2528 pv_t *pv_by_path(struct cmd_context *cmd, const char *pv_name)
   2529 {
   2530 	struct dm_list mdas;
   2531 
   2532 	dm_list_init(&mdas);
   2533 	return _pv_read(cmd, pv_name, &mdas, NULL, 1);
   2534 }
   2535