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