From ee79277774c4165b67c06a95af1bb6175333f141 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Fri, 2 Nov 2007 13:06:42 +0000 Subject: [PATCH] Add is_orphan_vg() and change all hardcoded checks to use it. --- WHATS_NEW | 1 + lib/cache/lvmcache.c | 4 ++-- lib/format1/disk-rep.c | 2 +- lib/format_text/format-text.c | 5 +++-- lib/locking/locking.c | 2 +- lib/metadata/metadata-exported.h | 1 + lib/metadata/metadata.c | 24 ++++++++++++++++-------- lib/metadata/metadata.h | 2 +- lib/metadata/pv_manip.c | 4 ++-- tools/toollib.c | 9 ++++++--- tools/vgrename.c | 3 ++- 11 files changed, 36 insertions(+), 21 deletions(-) diff --git a/WHATS_NEW b/WHATS_NEW index e2b064a59..4320c4072 100644 --- a/WHATS_NEW +++ b/WHATS_NEW @@ -1,5 +1,6 @@ Version 2.02.29 - ================================== + Add is_orphan_vg() and change all hardcoded checks to use it. Detect md superblocks version 1.0, 1.1 and 1.2. Add _alloc_pv() and _free_pv() from _pv_create() code and fix error paths. Add pv_dev_name() to access PV device name. diff --git a/lib/cache/lvmcache.c b/lib/cache/lvmcache.c index 80b82747c..fe32b6fae 100644 --- a/lib/cache/lvmcache.c +++ b/lib/cache/lvmcache.c @@ -633,7 +633,7 @@ static int _lvmcache_update_vgname(struct lvmcache_info *info, return 0; } /* Ensure orphans appear last on list_iterate */ - if (!*vgname) + if (is_orphan_vg(vgname)) list_add(&_vginfos, &vginfo->list); else list_add_h(&_vginfos, &vginfo->list); @@ -649,7 +649,7 @@ static int _lvmcache_update_vgname(struct lvmcache_info *info, vginfo->fmt = info->fmt; log_debug("lvmcache: %s: now %s%s%s%s%s", dev_name(info->dev), - *vgname ? "in VG " : "orphaned", vgname, + is_orphan_vg(vgname) ? "in VG " : "orphaned", vgname, vginfo->vgid[0] ? " (" : "", vginfo->vgid[0] ? vginfo->vgid : "", vginfo->vgid[0] ? ")" : ""); diff --git a/lib/format1/disk-rep.c b/lib/format1/disk-rep.c index 7d63b7906..c2f880dc4 100644 --- a/lib/format1/disk-rep.c +++ b/lib/format1/disk-rep.c @@ -480,7 +480,7 @@ int read_pvs_in_vg(const struct format_type *fmt, const char *vg_name, } /* Did we find the whole VG? */ - if (!vg_name || !*vg_name || + if (!vg_name || is_orphan_vg(vg_name) || (data && *data->pvd.vg_name && list_size(head) == data->vgd.pv_cur)) return 1; diff --git a/lib/format_text/format-text.c b/lib/format_text/format-text.c index f6511348b..e2fd9faf3 100644 --- a/lib/format_text/format-text.c +++ b/lib/format_text/format-text.c @@ -1438,7 +1438,8 @@ static int _text_pv_read(const struct format_type *fmt, const char *pv_name, info = (struct lvmcache_info *) label->info; /* Have we already cached vgname? */ - if (info->vginfo && info->vginfo->vgname && *info->vginfo->vgname && + if (info->vginfo && info->vginfo->vgname && + !is_orphan_vg(info->vginfo->vgname) && get_pv_from_vg_by_id(info->fmt, info->vginfo->vgname, info->vginfo->vgid, info->dev->pvid, pv)) { return 1; @@ -1449,7 +1450,7 @@ static int _text_pv_read(const struct format_type *fmt, const char *pv_name, lvmcache_label_scan(fmt->cmd, 2); if (info->vginfo && info->vginfo->vgname && - *info->vginfo->vgname && + !is_orphan_vg(info->vginfo->vgname) && get_pv_from_vg_by_id(info->fmt, info->vginfo->vgname, info->vginfo->vgid, info->dev->pvid, pv)) { diff --git a/lib/locking/locking.c b/lib/locking/locking.c index 6b57b1289..70ea58e36 100644 --- a/lib/locking/locking.c +++ b/lib/locking/locking.c @@ -288,7 +288,7 @@ int check_lvm1_vg_inactive(struct cmd_context *cmd, const char *vgname) char path[PATH_MAX]; /* We'll allow operations on orphans */ - if (!*vgname) + if (is_orphan_vg(vgname)) return 1; if (dm_snprintf(path, sizeof(path), "%s/lvm/VGs/%s", cmd->proc_dir, diff --git a/lib/metadata/metadata-exported.h b/lib/metadata/metadata-exported.h index 8893d3510..d45e3411d 100644 --- a/lib/metadata/metadata-exported.h +++ b/lib/metadata/metadata-exported.h @@ -304,6 +304,7 @@ struct list *get_vgids(struct cmd_context *cmd, int full_scan); int pv_write(struct cmd_context *cmd, struct physical_volume *pv, struct list *mdas, int64_t label_sector); +int is_orphan_vg(const char *vg_name); int is_orphan(pv_t *pv); vg_t *vg_lock_and_read(struct cmd_context *cmd, const char *vg_name, uint32_t lock_flags, uint32_t status_flags, diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c index bedb74729..7f61b7119 100644 --- a/lib/metadata/metadata.c +++ b/lib/metadata/metadata.c @@ -93,7 +93,7 @@ int add_pv_to_vg(struct volume_group *vg, const char *pv_name, return 0; } - if (*pv->vg_name) { + if (!is_orphan_vg(pv->vg_name)) { log_error("Physical volume '%s' is already in volume group " "'%s'", pv_name, pv->vg_name); return 0; @@ -928,7 +928,7 @@ static struct physical_volume *_find_pv_by_name(struct cmd_context *cmd, } /* FIXME Can fail when no PV mda */ - if (!pv->vg_name[0]) { + if (is_orphan_vg(pv->vg_name)) { log_error("Physical volume %s not in a volume group", pv_name); return NULL; } @@ -1272,7 +1272,7 @@ static struct volume_group *_vg_read(struct cmd_context *cmd, struct list all_pvs; char uuid[64] __attribute((aligned(8))); - if (!*vgname) { + if (is_orphan_vg(vgname)) { if (use_precommitted) { log_error("Internal error: vg_read requires vgname " "with pre-commit."); @@ -1536,7 +1536,7 @@ static struct volume_group *_vg_read_by_vgid(struct cmd_context *cmd, /* Is corresponding vgname already cached? */ if ((vginfo = vginfo_from_vgid(vgid)) && - vginfo->vgname && *vginfo->vgname) { + vginfo->vgname && !is_orphan_vg(vginfo->vgname)) { if ((vg = _vg_read(cmd, vginfo->vgname, vgid, &consistent, precommitted)) && !strncmp((char *)vg->id.uuid, vgid, ID_LEN)) { @@ -1566,7 +1566,7 @@ static struct volume_group *_vg_read_by_vgid(struct cmd_context *cmd, list_iterate_items(strl, vgnames) { vgname = strl->str; - if (!vgname || !*vgname) + if (!vgname || is_orphan_vg(vgname)) continue; // FIXME Unnecessary? consistent = 0; if ((vg = _vg_read(cmd, vgname, vgid, &consistent, @@ -1778,7 +1778,7 @@ static int _pv_write(struct cmd_context *cmd __attribute((unused)), return 0; } - if (*pv->vg_name || pv->pe_alloc_count) { + if (!is_orphan_vg(pv->vg_name) || pv->pe_alloc_count) { log_error("Assertion failed: can't _pv_write non-orphan PV " "(in VG %s)", pv->vg_name); return 0; @@ -1814,16 +1814,24 @@ int pv_write_orphan(struct cmd_context *cmd, struct physical_volume *pv) return 1; } +/** + * is_orphan_vg - Determine whether a vg_name is an orphan + * @vg_name: pointer to the vg_name + */ +int is_orphan_vg(const char *vg_name) +{ + return (vg_name[0] ? 0 : 1); +} + /** * is_orphan - Determine whether a pv is an orphan based on its vg_name * @pv: handle to the physical volume */ int is_orphan(pv_t *pv) { - return (pv_field(pv, vg_name)[0] ? 0 : 1); + return is_orphan_vg(pv_field(pv, vg_name)); } - /* * Returns: * 0 - fail diff --git a/lib/metadata/metadata.h b/lib/metadata/metadata.h index 277832cb0..78ec97812 100644 --- a/lib/metadata/metadata.h +++ b/lib/metadata/metadata.h @@ -180,7 +180,7 @@ struct format_handler { /* * Write a PV structure to disk. Fails if the PV is in a VG ie - * pv->vg_name must be null. + * pv->vg_name must be a valid orphan VG name */ int (*pv_write) (const struct format_type * fmt, struct physical_volume * pv, struct list * mdas, diff --git a/lib/metadata/pv_manip.c b/lib/metadata/pv_manip.c index ce2d0361f..cf2dc2c2b 100644 --- a/lib/metadata/pv_manip.c +++ b/lib/metadata/pv_manip.c @@ -455,7 +455,7 @@ int pv_resize_single(struct cmd_context *cmd, list_init(&mdas); - if (!*pv_vg_name(pv)) { + if (is_orphan_vg(pv_vg_name(pv))) { vg_name = ORPHAN; if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) { @@ -572,7 +572,7 @@ int pv_resize_single(struct cmd_context *cmd, pv_name, pv_size(pv)); log_verbose("Updating physical volume \"%s\"", pv_name); - if (*pv_vg_name(pv)) { + if (!is_orphan_vg(pv_vg_name(pv))) { if (!vg_write(vg) || !vg_commit(vg)) { unlock_vg(cmd, pv_vg_name(pv)); log_error("Failed to store physical volume \"%s\" in " diff --git a/tools/toollib.c b/tools/toollib.c index b74238526..9b9d94fe9 100644 --- a/tools/toollib.c +++ b/tools/toollib.c @@ -341,7 +341,7 @@ int process_each_lv(struct cmd_context *cmd, int argc, char **argv, list_iterate_items(strl, vgnames) { vgname = strl->str; - if (!vgname || !*vgname) + if (!vgname || is_orphan_vg(vgname)) continue; /* FIXME Unnecessary? */ if (!lock_vol(cmd, vgname, lock_type)) { log_error("Can't lock %s: skipping", vgname); @@ -576,7 +576,7 @@ int process_each_vg(struct cmd_context *cmd, int argc, char **argv, list_iterate_items(sl, vgids) { vgid = sl->str; if (!vgid || !(vg_name = vgname_from_vgid(cmd->mem, vgid)) || - !*vg_name) + is_orphan_vg(vg_name)) continue; ret_max = _process_one_vg(cmd, vg_name, vgid, &tags, &arg_vgnames, @@ -588,7 +588,7 @@ int process_each_vg(struct cmd_context *cmd, int argc, char **argv, } else { list_iterate_items(sl, vgnames) { vg_name = sl->str; - if (!vg_name || !*vg_name) + if (!vg_name || is_orphan_vg(vg_name)) continue; /* FIXME Unnecessary? */ ret_max = _process_one_vg(cmd, vg_name, NULL, &tags, &arg_vgnames, @@ -1199,6 +1199,9 @@ int validate_vg_name(struct cmd_context *cmd, const char *vg_name) if (!validate_name(vg_name)) return 0; + if (is_orphan_vg(vg_name)) + return 0; + snprintf(vg_path, PATH_MAX, "%s%s", cmd->dev_dir, vg_name); if (path_exists(vg_path)) { log_error("%s: already exists in filesystem", vg_path); diff --git a/tools/vgrename.c b/tools/vgrename.c index b31d7deff..f5de9d78b 100644 --- a/tools/vgrename.c +++ b/tools/vgrename.c @@ -65,7 +65,8 @@ static int vg_rename_path(struct cmd_context *cmd, const char *old_vg_path, list_iterate_items(sl, vgids) { vgid = sl->str; - if (!vgid || !(vg_name = vgname_from_vgid(NULL, vgid)) || !*vg_name) + if (!vgid || !(vg_name = vgname_from_vgid(NULL, vgid)) + || is_orphan_vg(vg_name)) continue; if (!strcmp(vg_name, vg_name_old)) { if (match) {