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