2 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include "toolcontext.h"
19 #include "dev-cache.h"
23 #include "filter-persistent.h"
26 #include "format-text.h"
27 #include "format_pool.h"
30 static struct dm_hash_table *_pvid_hash = NULL;
31 static struct dm_hash_table *_vgid_hash = NULL;
32 static struct dm_hash_table *_vgname_hash = NULL;
33 static struct dm_hash_table *_lock_hash = NULL;
34 static DM_LIST_INIT(_vginfos);
35 static int _scanning_in_progress = 0;
36 static int _has_scanned = 0;
37 static int _vgs_locked = 0;
38 static int _vg_global_lock_held = 0; /* Global lock held when cache wiped? */
40 int lvmcache_init(void)
43 * FIXME add a proper lvmcache_locking_reset() that
44 * resets the cache so no previous locks are locked
48 dm_list_init(&_vginfos);
50 if (!(_vgname_hash = dm_hash_create(128)))
53 if (!(_vgid_hash = dm_hash_create(128)))
56 if (!(_pvid_hash = dm_hash_create(128)))
59 if (!(_lock_hash = dm_hash_create(128)))
63 * Reinitialising the cache clears the internal record of
64 * which locks are held. The global lock can be held during
65 * this operation so its state must be restored afterwards.
67 if (_vg_global_lock_held) {
68 lvmcache_lock_vgname(VG_GLOBAL, 0);
69 _vg_global_lock_held = 0;
75 /* Volume Group metadata cache functions */
76 static void _free_cached_vgmetadata(struct lvmcache_vginfo *vginfo)
78 if (!vginfo || !vginfo->vgmetadata)
81 dm_free(vginfo->vgmetadata);
83 vginfo->vgmetadata = NULL;
85 log_debug("Metadata cache: VG %s wiped.", vginfo->vgname);
89 * Cache VG metadata against the vginfo with matching vgid.
91 static void _store_metadata(struct volume_group *vg, unsigned precommitted)
93 char uuid[64] __attribute__((aligned(8)));
94 struct lvmcache_vginfo *vginfo;
97 if (!(vginfo = vginfo_from_vgid((const char *)&vg->id))) {
102 if (vginfo->vgmetadata)
103 _free_cached_vgmetadata(vginfo);
105 if (!(size = export_vg_to_buffer(vg, &vginfo->vgmetadata))) {
110 vginfo->precommitted = precommitted;
112 if (!id_write_format((const struct id *)vginfo->vgid, uuid, sizeof(uuid))) {
117 log_debug("Metadata cache: VG %s (%s) stored (%d bytes%s).",
118 vginfo->vgname, uuid, size,
119 precommitted ? ", precommitted" : "");
122 static void _update_cache_info_lock_state(struct lvmcache_info *info,
124 int *cached_vgmetadata_valid)
126 int was_locked = (info->status & CACHE_LOCKED) ? 1 : 0;
129 * Cache becomes invalid whenever lock state changes unless
130 * exclusive VG_GLOBAL is held (i.e. while scanning).
132 if (!vgname_is_locked(VG_GLOBAL) && (was_locked != locked)) {
133 info->status |= CACHE_INVALID;
134 *cached_vgmetadata_valid = 0;
138 info->status |= CACHE_LOCKED;
140 info->status &= ~CACHE_LOCKED;
143 static void _update_cache_vginfo_lock_state(struct lvmcache_vginfo *vginfo,
146 struct lvmcache_info *info;
147 int cached_vgmetadata_valid = 1;
149 dm_list_iterate_items(info, &vginfo->infos)
150 _update_cache_info_lock_state(info, locked,
151 &cached_vgmetadata_valid);
153 if (!cached_vgmetadata_valid)
154 _free_cached_vgmetadata(vginfo);
157 static void _update_cache_lock_state(const char *vgname, int locked)
159 struct lvmcache_vginfo *vginfo;
161 if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
164 _update_cache_vginfo_lock_state(vginfo, locked);
167 static void _drop_metadata(const char *vgname, int drop_precommitted)
169 struct lvmcache_vginfo *vginfo;
170 struct lvmcache_info *info;
172 if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
176 * Invalidate cached PV labels.
177 * If cached precommitted metadata exists that means we
178 * already invalidated the PV labels (before caching it)
179 * and we must not do it again.
181 if (!drop_precommitted && vginfo->precommitted && !vginfo->vgmetadata)
182 log_error(INTERNAL_ERROR "metadata commit (or revert) missing before "
183 "dropping metadata from cache.");
185 if (drop_precommitted || !vginfo->precommitted)
186 dm_list_iterate_items(info, &vginfo->infos)
187 info->status |= CACHE_INVALID;
189 _free_cached_vgmetadata(vginfo);
193 * Remote node uses this to upgrade precommited metadata to commited state
194 * when receives vg_commit notification.
195 * (Note that devices can be suspended here, if so, precommited metadata are already read.)
197 void lvmcache_commit_metadata(const char *vgname)
199 struct lvmcache_vginfo *vginfo;
201 if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
204 if (vginfo->precommitted) {
205 log_debug("Precommitted metadata cache: VG %s upgraded to committed.",
207 vginfo->precommitted = 0;
211 void lvmcache_drop_metadata(const char *vgname, int drop_precommitted)
213 /* For VG_ORPHANS, we need to invalidate all labels on orphan PVs. */
214 if (!strcmp(vgname, VG_ORPHANS)) {
215 _drop_metadata(FMT_TEXT_ORPHAN_VG_NAME, 0);
216 _drop_metadata(FMT_LVM1_ORPHAN_VG_NAME, 0);
217 _drop_metadata(FMT_POOL_ORPHAN_VG_NAME, 0);
219 /* Indicate that PVs could now be missing from the cache */
220 init_full_scan_done(0);
221 } else if (!vgname_is_locked(VG_GLOBAL))
222 _drop_metadata(vgname, drop_precommitted);
226 * Ensure vgname2 comes after vgname1 alphabetically.
227 * Orphan locks come last.
228 * VG_GLOBAL comes first.
230 static int _vgname_order_correct(const char *vgname1, const char *vgname2)
232 if (is_global_vg(vgname1))
235 if (is_global_vg(vgname2))
238 if (is_orphan_vg(vgname1))
241 if (is_orphan_vg(vgname2))
244 if (strcmp(vgname1, vgname2) < 0)
251 * Ensure VG locks are acquired in alphabetical order.
253 int lvmcache_verify_lock_order(const char *vgname)
255 struct dm_hash_node *n;
261 dm_hash_iterate(n, _lock_hash) {
262 if (!dm_hash_get_data(_lock_hash, n))
265 vgname2 = dm_hash_get_key(_lock_hash, n);
267 if (!_vgname_order_correct(vgname2, vgname)) {
268 log_errno(EDEADLK, INTERNAL_ERROR "VG lock %s must "
269 "be requested before %s, not after.",
278 void lvmcache_lock_vgname(const char *vgname, int read_only __attribute__((unused)))
280 if (!_lock_hash && !lvmcache_init()) {
281 log_error("Internal cache initialisation failed");
285 if (dm_hash_lookup(_lock_hash, vgname))
286 log_error(INTERNAL_ERROR "Nested locking attempted on VG %s.",
289 if (!dm_hash_insert(_lock_hash, vgname, (void *) 1))
290 log_error("Cache locking failure for %s", vgname);
292 _update_cache_lock_state(vgname, 1);
294 if (strcmp(vgname, VG_GLOBAL))
298 int vgname_is_locked(const char *vgname)
303 return dm_hash_lookup(_lock_hash, is_orphan_vg(vgname) ? VG_ORPHANS : vgname) ? 1 : 0;
306 void lvmcache_unlock_vgname(const char *vgname)
308 if (!dm_hash_lookup(_lock_hash, vgname))
309 log_error(INTERNAL_ERROR "Attempt to unlock unlocked VG %s.",
312 _update_cache_lock_state(vgname, 0);
314 dm_hash_remove(_lock_hash, vgname);
316 /* FIXME Do this per-VG */
317 if (strcmp(vgname, VG_GLOBAL) && !--_vgs_locked)
326 static void _vginfo_attach_info(struct lvmcache_vginfo *vginfo,
327 struct lvmcache_info *info)
332 info->vginfo = vginfo;
333 dm_list_add(&vginfo->infos, &info->list);
336 static void _vginfo_detach_info(struct lvmcache_info *info)
338 if (!dm_list_empty(&info->list)) {
339 dm_list_del(&info->list);
340 dm_list_init(&info->list);
346 /* If vgid supplied, require a match. */
347 struct lvmcache_vginfo *vginfo_from_vgname(const char *vgname, const char *vgid)
349 struct lvmcache_vginfo *vginfo;
352 return vginfo_from_vgid(vgid);
357 if (!(vginfo = dm_hash_lookup(_vgname_hash, vgname)))
362 if (!strncmp(vgid, vginfo->vgid, ID_LEN))
364 while ((vginfo = vginfo->next));
369 const struct format_type *fmt_from_vgname(const char *vgname, const char *vgid, unsigned revalidate_labels)
371 struct lvmcache_vginfo *vginfo;
372 struct lvmcache_info *info;
374 struct dm_list *devh, *tmp;
376 struct device_list *devl;
377 char vgid_found[ID_LEN + 1] __attribute__((aligned(8)));
379 if (!(vginfo = vginfo_from_vgname(vgname, vgid)))
383 * If this function is called repeatedly, only the first one needs to revalidate.
385 if (!revalidate_labels)
389 * This function is normally called before reading metadata so
390 * we check cached labels here. Unfortunately vginfo is volatile.
393 dm_list_iterate_items(info, &vginfo->infos) {
394 if (!(devl = dm_malloc(sizeof(*devl)))) {
395 log_error("device_list element allocation failed");
398 devl->dev = info->dev;
399 dm_list_add(&devs, &devl->list);
402 memcpy(vgid_found, vginfo->vgid, sizeof(vgid_found));
404 dm_list_iterate_safe(devh, tmp, &devs) {
405 devl = dm_list_item(devh, struct device_list);
406 label_read(devl->dev, &label, UINT64_C(0));
407 dm_list_del(&devl->list);
411 /* If vginfo changed, caller needs to rescan */
412 if (!(vginfo = vginfo_from_vgname(vgname, vgid_found)) ||
413 strncmp(vginfo->vgid, vgid_found, ID_LEN))
420 struct lvmcache_vginfo *vginfo_from_vgid(const char *vgid)
422 struct lvmcache_vginfo *vginfo;
423 char id[ID_LEN + 1] __attribute__((aligned(8)));
425 if (!_vgid_hash || !vgid)
428 /* vgid not necessarily NULL-terminated */
429 strncpy(&id[0], vgid, ID_LEN);
432 if (!(vginfo = dm_hash_lookup(_vgid_hash, id)))
438 const char *vgname_from_vgid(struct dm_pool *mem, const char *vgid)
440 struct lvmcache_vginfo *vginfo;
441 const char *vgname = NULL;
443 if ((vginfo = vginfo_from_vgid(vgid)))
444 vgname = vginfo->vgname;
447 return dm_pool_strdup(mem, vgname);
452 static int _info_is_valid(struct lvmcache_info *info)
454 if (info->status & CACHE_INVALID)
458 * The caller must hold the VG lock to manipulate metadata.
459 * In a cluster, remote nodes sometimes read metadata in the
460 * knowledge that the controlling node is holding the lock.
461 * So if the VG appears to be unlocked here, it should be safe
462 * to use the cached value.
464 if (info->vginfo && !vgname_is_locked(info->vginfo->vgname))
467 if (!(info->status & CACHE_LOCKED))
473 static int _vginfo_is_valid(struct lvmcache_vginfo *vginfo)
475 struct lvmcache_info *info;
477 /* Invalid if any info is invalid */
478 dm_list_iterate_items(info, &vginfo->infos)
479 if (!_info_is_valid(info))
485 /* vginfo is invalid if it does not contain at least one valid info */
486 static int _vginfo_is_invalid(struct lvmcache_vginfo *vginfo)
488 struct lvmcache_info *info;
490 dm_list_iterate_items(info, &vginfo->infos)
491 if (_info_is_valid(info))
498 * If valid_only is set, data will only be returned if the cached data is
499 * known still to be valid.
501 struct lvmcache_info *info_from_pvid(const char *pvid, int valid_only)
503 struct lvmcache_info *info;
504 char id[ID_LEN + 1] __attribute__((aligned(8)));
506 if (!_pvid_hash || !pvid)
509 strncpy(&id[0], pvid, ID_LEN);
512 if (!(info = dm_hash_lookup(_pvid_hash, id)))
515 if (valid_only && !_info_is_valid(info))
521 char *lvmcache_vgname_from_pvid(struct cmd_context *cmd, const char *pvid)
523 struct lvmcache_info *info;
526 if (!device_from_pvid(cmd, (const struct id *)pvid, NULL)) {
527 log_error("Couldn't find device with uuid %s.", pvid);
531 info = info_from_pvid(pvid, 0);
535 if (!(vgname = dm_pool_strdup(cmd->mem, info->vginfo->vgname))) {
536 log_errno(ENOMEM, "vgname allocation failed");
542 static void _rescan_entry(struct lvmcache_info *info)
546 if (info->status & CACHE_INVALID)
547 label_read(info->dev, &label, UINT64_C(0));
550 static int _scan_invalid(void)
552 dm_hash_iter(_pvid_hash, (dm_hash_iterate_fn) _rescan_entry);
557 int lvmcache_label_scan(struct cmd_context *cmd, int full_scan)
560 struct dev_iter *iter;
562 struct format_type *fmt;
566 /* Avoid recursion when a PVID can't be found! */
567 if (_scanning_in_progress)
570 _scanning_in_progress = 1;
572 if (!_vgname_hash && !lvmcache_init()) {
573 log_error("Internal cache initialisation failed");
577 if (_has_scanned && !full_scan) {
582 if (full_scan == 2 && !cmd->filter->use_count && !refresh_filters(cmd)) {
583 log_error("refresh filters failed");
587 if (!(iter = dev_iter_create(cmd->filter, (full_scan == 2) ? 1 : 0))) {
588 log_error("dev_iter creation failed");
592 while ((dev = dev_iter_get(iter)))
593 label_read(dev, &label, UINT64_C(0));
595 dev_iter_destroy(iter);
599 /* Perform any format-specific scanning e.g. text files */
600 if (cmd->independent_metadata_areas)
601 dm_list_iterate_items(fmt, &cmd->formats)
602 if (fmt->ops->scan && !fmt->ops->scan(fmt, NULL))
606 * If we are a long-lived process, write out the updated persistent
607 * device cache for the benefit of short-lived processes.
609 if (full_scan == 2 && cmd->is_long_lived && cmd->dump_filter)
610 persistent_filter_dump(cmd->filter, 0);
615 _scanning_in_progress = 0;
620 struct volume_group *lvmcache_get_vg(const char *vgid, unsigned precommitted)
622 struct lvmcache_vginfo *vginfo;
623 struct volume_group *vg;
624 struct format_instance *fid;
626 if (!vgid || !(vginfo = vginfo_from_vgid(vgid)) || !vginfo->vgmetadata)
629 if (!_vginfo_is_valid(vginfo))
633 * Don't return cached data if either:
634 * (i) precommitted metadata is requested but we don't have it cached
635 * - caller should read it off disk;
636 * (ii) live metadata is requested but we have precommitted metadata cached
637 * and no devices are suspended so caller may read it off disk.
639 * If live metadata is requested but we have precommitted metadata cached
640 * and devices are suspended, we assume this precommitted metadata has
641 * already been preloaded and committed so it's OK to return it as live.
642 * Note that we do not clear the PRECOMMITTED flag.
644 if ((precommitted && !vginfo->precommitted) ||
645 (!precommitted && vginfo->precommitted && !memlock()))
648 if (!(fid = vginfo->fmt->ops->create_instance(vginfo->fmt,
653 if (!(vg = import_vg_from_buffer(vginfo->vgmetadata, fid))) {
654 _free_cached_vgmetadata(vginfo);
659 log_debug("Using cached %smetadata for VG %s.",
660 vginfo->precommitted ? "pre-committed" : "", vginfo->vgname);
665 struct dm_list *lvmcache_get_vgids(struct cmd_context *cmd,
666 int include_internal)
668 struct dm_list *vgids;
669 struct lvmcache_vginfo *vginfo;
671 lvmcache_label_scan(cmd, 0);
673 if (!(vgids = str_list_create(cmd->mem))) {
674 log_error("vgids list allocation failed");
678 dm_list_iterate_items(vginfo, &_vginfos) {
679 if (!include_internal && is_orphan_vg(vginfo->vgname))
682 if (!str_list_add(cmd->mem, vgids,
683 dm_pool_strdup(cmd->mem, vginfo->vgid))) {
684 log_error("strlist allocation failed");
692 struct dm_list *lvmcache_get_vgnames(struct cmd_context *cmd,
693 int include_internal)
695 struct dm_list *vgnames;
696 struct lvmcache_vginfo *vginfo;
698 lvmcache_label_scan(cmd, 0);
700 if (!(vgnames = str_list_create(cmd->mem))) {
701 log_errno(ENOMEM, "vgnames list allocation failed");
705 dm_list_iterate_items(vginfo, &_vginfos) {
706 if (!include_internal && is_orphan_vg(vginfo->vgname))
709 if (!str_list_add(cmd->mem, vgnames,
710 dm_pool_strdup(cmd->mem, vginfo->vgname))) {
711 log_errno(ENOMEM, "strlist allocation failed");
719 struct dm_list *lvmcache_get_pvids(struct cmd_context *cmd, const char *vgname,
722 struct dm_list *pvids;
723 struct lvmcache_vginfo *vginfo;
724 struct lvmcache_info *info;
726 if (!(pvids = str_list_create(cmd->mem))) {
727 log_error("pvids list allocation failed");
731 if (!(vginfo = vginfo_from_vgname(vgname, vgid)))
734 dm_list_iterate_items(info, &vginfo->infos) {
735 if (!str_list_add(cmd->mem, pvids,
736 dm_pool_strdup(cmd->mem, info->dev->pvid))) {
737 log_error("strlist allocation failed");
745 struct device *device_from_pvid(struct cmd_context *cmd, const struct id *pvid,
746 unsigned *scan_done_once)
749 struct lvmcache_info *info;
751 /* Already cached ? */
752 if ((info = info_from_pvid((const char *) pvid, 0))) {
753 if (label_read(info->dev, &label, UINT64_C(0))) {
754 info = (struct lvmcache_info *) label->info;
755 if (id_equal(pvid, (struct id *) &info->dev->pvid))
760 lvmcache_label_scan(cmd, 0);
763 if ((info = info_from_pvid((const char *) pvid, 0))) {
764 if (label_read(info->dev, &label, UINT64_C(0))) {
765 info = (struct lvmcache_info *) label->info;
766 if (id_equal(pvid, (struct id *) &info->dev->pvid))
771 if (memlock() || (scan_done_once && *scan_done_once))
774 lvmcache_label_scan(cmd, 2);
779 if ((info = info_from_pvid((const char *) pvid, 0))) {
780 if (label_read(info->dev, &label, UINT64_C(0))) {
781 info = (struct lvmcache_info *) label->info;
782 if (id_equal(pvid, (struct id *) &info->dev->pvid))
790 const char *pvid_from_devname(struct cmd_context *cmd,
796 if (!(dev = dev_cache_get(devname, cmd->filter))) {
797 log_error("%s: Couldn't find device. Check your filters?",
802 if (!(label_read(dev, &label, UINT64_C(0))))
809 static int _free_vginfo(struct lvmcache_vginfo *vginfo)
811 struct lvmcache_vginfo *primary_vginfo, *vginfo2;
814 _free_cached_vgmetadata(vginfo);
816 vginfo2 = primary_vginfo = vginfo_from_vgname(vginfo->vgname, NULL);
818 if (vginfo == primary_vginfo) {
819 dm_hash_remove(_vgname_hash, vginfo->vgname);
820 if (vginfo->next && !dm_hash_insert(_vgname_hash, vginfo->vgname,
822 log_error("_vgname_hash re-insertion for %s failed",
827 if (vginfo2->next == vginfo) {
828 vginfo2->next = vginfo->next;
831 while ((vginfo2 = primary_vginfo->next));
834 dm_free(vginfo->vgname);
836 if (vginfo->creation_host)
837 dm_free(vginfo->creation_host);
839 if (*vginfo->vgid && _vgid_hash &&
840 vginfo_from_vgid(vginfo->vgid) == vginfo)
841 dm_hash_remove(_vgid_hash, vginfo->vgid);
843 dm_list_del(&vginfo->list);
851 * vginfo must be info->vginfo unless info is NULL
853 static int _drop_vginfo(struct lvmcache_info *info, struct lvmcache_vginfo *vginfo)
856 _vginfo_detach_info(info);
858 /* vginfo still referenced? */
859 if (!vginfo || is_orphan_vg(vginfo->vgname) ||
860 !dm_list_empty(&vginfo->infos))
863 if (!_free_vginfo(vginfo))
870 void lvmcache_del(struct lvmcache_info *info)
872 if (info->dev->pvid[0] && _pvid_hash)
873 dm_hash_remove(_pvid_hash, info->dev->pvid);
875 _drop_vginfo(info, info->vginfo);
877 info->label->labeller->ops->destroy_label(info->label->labeller,
884 static int _lvmcache_update_pvid(struct lvmcache_info *info, const char *pvid)
887 * Nothing to do if already stored with same pvid.
889 if (((dm_hash_lookup(_pvid_hash, pvid)) == info) &&
890 !strcmp(info->dev->pvid, pvid))
892 if (*info->dev->pvid)
893 dm_hash_remove(_pvid_hash, info->dev->pvid);
894 strncpy(info->dev->pvid, pvid, sizeof(info->dev->pvid));
895 if (!dm_hash_insert(_pvid_hash, pvid, info)) {
896 log_error("_lvmcache_update: pvid insertion failed: %s", pvid);
904 * vginfo must be info->vginfo unless info is NULL (orphans)
906 static int _lvmcache_update_vgid(struct lvmcache_info *info,
907 struct lvmcache_vginfo *vginfo,
910 if (!vgid || !vginfo ||
911 !strncmp(vginfo->vgid, vgid, ID_LEN))
914 if (vginfo && *vginfo->vgid)
915 dm_hash_remove(_vgid_hash, vginfo->vgid);
917 log_debug("lvmcache: %s: clearing VGID", info ? dev_name(info->dev) : vginfo->vgname);
921 strncpy(vginfo->vgid, vgid, ID_LEN);
922 vginfo->vgid[ID_LEN] = '\0';
923 if (!dm_hash_insert(_vgid_hash, vginfo->vgid, vginfo)) {
924 log_error("_lvmcache_update: vgid hash insertion failed: %s",
929 if (!is_orphan_vg(vginfo->vgname))
930 log_debug("lvmcache: %s: setting %s VGID to %s",
931 dev_name(info->dev), vginfo->vgname,
937 static int _insert_vginfo(struct lvmcache_vginfo *new_vginfo, const char *vgid,
938 uint32_t vgstatus, const char *creation_host,
939 struct lvmcache_vginfo *primary_vginfo)
941 struct lvmcache_vginfo *last_vginfo = primary_vginfo;
942 char uuid_primary[64] __attribute__((aligned(8)));
943 char uuid_new[64] __attribute__((aligned(8)));
946 /* Pre-existing VG takes precedence. Unexported VG takes precedence. */
947 if (primary_vginfo) {
948 if (!id_write_format((const struct id *)vgid, uuid_new, sizeof(uuid_new)))
951 if (!id_write_format((const struct id *)&primary_vginfo->vgid, uuid_primary,
952 sizeof(uuid_primary)))
956 * If Primary not exported, new exported => keep
957 * Else Primary exported, new not exported => change
958 * Else Primary has hostname for this machine => keep
959 * Else Primary has no hostname, new has one => change
960 * Else New has hostname for this machine => change
963 if (!(primary_vginfo->status & EXPORTED_VG) &&
964 (vgstatus & EXPORTED_VG))
965 log_warn("WARNING: Duplicate VG name %s: "
966 "Existing %s takes precedence over "
967 "exported %s", new_vginfo->vgname,
968 uuid_primary, uuid_new);
969 else if ((primary_vginfo->status & EXPORTED_VG) &&
970 !(vgstatus & EXPORTED_VG)) {
971 log_warn("WARNING: Duplicate VG name %s: "
972 "%s takes precedence over exported %s",
973 new_vginfo->vgname, uuid_new,
976 } else if (primary_vginfo->creation_host &&
977 !strcmp(primary_vginfo->creation_host,
978 primary_vginfo->fmt->cmd->hostname))
979 log_warn("WARNING: Duplicate VG name %s: "
980 "Existing %s (created here) takes precedence "
981 "over %s", new_vginfo->vgname, uuid_primary,
983 else if (!primary_vginfo->creation_host && creation_host) {
984 log_warn("WARNING: Duplicate VG name %s: "
985 "%s (with creation_host) takes precedence over %s",
986 new_vginfo->vgname, uuid_new,
989 } else if (creation_host &&
990 !strcmp(creation_host,
991 primary_vginfo->fmt->cmd->hostname)) {
992 log_warn("WARNING: Duplicate VG name %s: "
993 "%s (created here) takes precedence over %s",
994 new_vginfo->vgname, uuid_new,
1000 while (last_vginfo->next)
1001 last_vginfo = last_vginfo->next;
1002 last_vginfo->next = new_vginfo;
1006 dm_hash_remove(_vgname_hash, primary_vginfo->vgname);
1009 if (!dm_hash_insert(_vgname_hash, new_vginfo->vgname, new_vginfo)) {
1010 log_error("cache_update: vg hash insertion failed: %s",
1011 new_vginfo->vgname);
1016 new_vginfo->next = primary_vginfo;
1021 static int _lvmcache_update_vgname(struct lvmcache_info *info,
1022 const char *vgname, const char *vgid,
1023 uint32_t vgstatus, const char *creation_host,
1024 const struct format_type *fmt)
1026 struct lvmcache_vginfo *vginfo, *primary_vginfo, *orphan_vginfo;
1027 struct lvmcache_info *info2, *info3;
1029 // struct lvmcache_vginfo *old_vginfo, *next;
1031 if (!vgname || (info && info->vginfo && !strcmp(info->vginfo->vgname, vgname)))
1034 /* Remove existing vginfo entry */
1036 _drop_vginfo(info, info->vginfo);
1038 /* Get existing vginfo or create new one */
1039 if (!(vginfo = vginfo_from_vgname(vgname, vgid))) {
1040 /*** FIXME - vginfo ends up duplicated instead of renamed.
1041 // Renaming? This lookup fails.
1042 if ((vginfo = vginfo_from_vgid(vgid))) {
1043 next = vginfo->next;
1044 old_vginfo = vginfo_from_vgname(vginfo->vgname, NULL);
1045 if (old_vginfo == vginfo) {
1046 dm_hash_remove(_vgname_hash, old_vginfo->vgname);
1047 if (old_vginfo->next) {
1048 if (!dm_hash_insert(_vgname_hash, old_vginfo->vgname, old_vginfo->next)) {
1049 log_error("vg hash re-insertion failed: %s",
1050 old_vginfo->vgname);
1055 if (old_vginfo->next == vginfo) {
1056 old_vginfo->next = vginfo->next;
1059 } while ((old_vginfo = old_vginfo->next));
1060 vginfo->next = NULL;
1062 dm_free(vginfo->vgname);
1063 if (!(vginfo->vgname = dm_strdup(vgname))) {
1064 log_error("cache vgname alloc failed for %s", vgname);
1068 // Rename so can assume new name does not already exist
1069 if (!dm_hash_insert(_vgname_hash, vginfo->vgname, vginfo->next)) {
1070 log_error("vg hash re-insertion failed: %s",
1076 if (!(vginfo = dm_zalloc(sizeof(*vginfo)))) {
1077 log_error("lvmcache_update_vgname: list alloc failed");
1080 if (!(vginfo->vgname = dm_strdup(vgname))) {
1082 log_error("cache vgname alloc failed for %s", vgname);
1085 dm_list_init(&vginfo->infos);
1088 * If we're scanning and there's an invalidated entry, remove it.
1089 * Otherwise we risk bogus warnings of duplicate VGs.
1091 while ((primary_vginfo = vginfo_from_vgname(vgname, NULL)) &&
1092 _scanning_in_progress && _vginfo_is_invalid(primary_vginfo))
1093 dm_list_iterate_items_safe(info2, info3, &primary_vginfo->infos) {
1094 orphan_vginfo = vginfo_from_vgname(primary_vginfo->fmt->orphan_vg_name, NULL);
1095 if (!orphan_vginfo) {
1096 log_error(INTERNAL_ERROR "Orphan vginfo %s lost from cache.",
1097 primary_vginfo->fmt->orphan_vg_name);
1098 dm_free(vginfo->vgname);
1102 _drop_vginfo(info2, primary_vginfo);
1103 _vginfo_attach_info(orphan_vginfo, info2);
1105 sprintf(mdabuf, " with %u mdas",
1106 dm_list_size(&info2->mdas));
1109 log_debug("lvmcache: %s: now in VG %s%s%s%s%s",
1110 dev_name(info2->dev),
1111 vgname, orphan_vginfo->vgid[0] ? " (" : "",
1112 orphan_vginfo->vgid[0] ? orphan_vginfo->vgid : "",
1113 orphan_vginfo->vgid[0] ? ")" : "", mdabuf);
1116 if (!_insert_vginfo(vginfo, vgid, vgstatus, creation_host,
1118 dm_free(vginfo->vgname);
1122 /* Ensure orphans appear last on list_iterate */
1123 if (is_orphan_vg(vgname))
1124 dm_list_add(&_vginfos, &vginfo->list);
1126 dm_list_add_h(&_vginfos, &vginfo->list);
1133 _vginfo_attach_info(vginfo, info);
1134 else if (!_lvmcache_update_vgid(NULL, vginfo, vgid)) /* Orphans */
1137 _update_cache_vginfo_lock_state(vginfo, vgname_is_locked(vgname));
1139 /* FIXME Check consistency of list! */
1144 sprintf(mdabuf, " with %u mdas", dm_list_size(&info->mdas));
1147 log_debug("lvmcache: %s: now in VG %s%s%s%s%s",
1148 dev_name(info->dev),
1149 vgname, vginfo->vgid[0] ? " (" : "",
1150 vginfo->vgid[0] ? vginfo->vgid : "",
1151 vginfo->vgid[0] ? ")" : "", mdabuf);
1153 log_debug("lvmcache: initialised VG %s", vgname);
1158 static int _lvmcache_update_vgstatus(struct lvmcache_info *info, uint32_t vgstatus,
1159 const char *creation_host)
1161 if (!info || !info->vginfo)
1164 if ((info->vginfo->status & EXPORTED_VG) != (vgstatus & EXPORTED_VG))
1165 log_debug("lvmcache: %s: VG %s %s exported",
1166 dev_name(info->dev), info->vginfo->vgname,
1167 vgstatus & EXPORTED_VG ? "now" : "no longer");
1169 info->vginfo->status = vgstatus;
1174 if (info->vginfo->creation_host && !strcmp(creation_host,
1175 info->vginfo->creation_host))
1178 if (info->vginfo->creation_host)
1179 dm_free(info->vginfo->creation_host);
1181 if (!(info->vginfo->creation_host = dm_strdup(creation_host))) {
1182 log_error("cache creation host alloc failed for %s",
1187 log_debug("lvmcache: %s: VG %s: Set creation host to %s.",
1188 dev_name(info->dev), info->vginfo->vgname, creation_host);
1193 int lvmcache_add_orphan_vginfo(const char *vgname, struct format_type *fmt)
1195 if (!_lock_hash && !lvmcache_init()) {
1196 log_error("Internal cache initialisation failed");
1200 return _lvmcache_update_vgname(NULL, vgname, vgname, 0, "", fmt);
1203 int lvmcache_update_vgname_and_id(struct lvmcache_info *info,
1204 const char *vgname, const char *vgid,
1205 uint32_t vgstatus, const char *creation_host)
1207 if (!vgname && !info->vginfo) {
1208 log_error(INTERNAL_ERROR "NULL vgname handed to cache");
1209 /* FIXME Remove this */
1210 vgname = info->fmt->orphan_vg_name;
1214 /* If PV without mdas is already in a real VG, don't make it orphan */
1215 if (is_orphan_vg(vgname) && info->vginfo &&
1216 mdas_empty_or_ignored(&info->mdas) &&
1217 !is_orphan_vg(info->vginfo->vgname) && memlock())
1220 /* If moving PV from orphan to real VG, always mark it valid */
1221 if (!is_orphan_vg(vgname))
1222 info->status &= ~CACHE_INVALID;
1224 if (!_lvmcache_update_vgname(info, vgname, vgid, vgstatus,
1225 creation_host, info->fmt) ||
1226 !_lvmcache_update_vgid(info, info->vginfo, vgid) ||
1227 !_lvmcache_update_vgstatus(info, vgstatus, creation_host))
1233 int lvmcache_update_vg(struct volume_group *vg, unsigned precommitted)
1235 struct pv_list *pvl;
1236 struct lvmcache_info *info;
1237 char pvid_s[ID_LEN + 1] __attribute__((aligned(8)));
1239 pvid_s[sizeof(pvid_s) - 1] = '\0';
1241 dm_list_iterate_items(pvl, &vg->pvs) {
1242 strncpy(pvid_s, (char *) &pvl->pv->id, sizeof(pvid_s) - 1);
1243 /* FIXME Could pvl->pv->dev->pvid ever be different? */
1244 if ((info = info_from_pvid(pvid_s, 0)) &&
1245 !lvmcache_update_vgname_and_id(info, vg->name,
1251 /* store text representation of vg to cache */
1252 if (vg->cmd->current_settings.cache_vgmetadata)
1253 _store_metadata(vg, precommitted);
1258 struct lvmcache_info *lvmcache_add(struct labeller *labeller, const char *pvid,
1260 const char *vgname, const char *vgid,
1263 struct label *label;
1264 struct lvmcache_info *existing, *info;
1265 char pvid_s[ID_LEN + 1] __attribute__((aligned(8)));
1267 if (!_vgname_hash && !lvmcache_init()) {
1268 log_error("Internal cache initialisation failed");
1272 strncpy(pvid_s, pvid, sizeof(pvid_s));
1273 pvid_s[sizeof(pvid_s) - 1] = '\0';
1275 if (!(existing = info_from_pvid(pvid_s, 0)) &&
1276 !(existing = info_from_pvid(dev->pvid, 0))) {
1277 if (!(label = label_create(labeller)))
1279 if (!(info = dm_zalloc(sizeof(*info)))) {
1280 log_error("lvmcache_info allocation failed");
1281 label_destroy(label);
1286 info->label = label;
1287 dm_list_init(&info->list);
1290 if (existing->dev != dev) {
1291 /* Is the existing entry a duplicate pvid e.g. md ? */
1292 if (dev_subsystem_part_major(existing->dev) &&
1293 !dev_subsystem_part_major(dev)) {
1294 log_very_verbose("Ignoring duplicate PV %s on "
1296 pvid, dev_name(dev),
1297 dev_subsystem_name(existing->dev),
1298 dev_name(existing->dev));
1300 } else if (dm_is_dm_major(MAJOR(existing->dev->dev)) &&
1301 !dm_is_dm_major(MAJOR(dev->dev))) {
1302 log_very_verbose("Ignoring duplicate PV %s on "
1304 pvid, dev_name(dev),
1305 dev_name(existing->dev));
1307 } else if (!dev_subsystem_part_major(existing->dev) &&
1308 dev_subsystem_part_major(dev))
1309 log_very_verbose("Duplicate PV %s on %s - "
1310 "using %s %s", pvid,
1311 dev_name(existing->dev),
1312 dev_subsystem_name(existing->dev),
1314 else if (!dm_is_dm_major(MAJOR(existing->dev->dev)) &&
1315 dm_is_dm_major(MAJOR(dev->dev)))
1316 log_very_verbose("Duplicate PV %s on %s - "
1317 "using dm %s", pvid,
1318 dev_name(existing->dev),
1320 /* FIXME If both dm, check dependencies */
1321 //else if (dm_is_dm_major(MAJOR(existing->dev->dev)) &&
1322 //dm_is_dm_major(MAJOR(dev->dev)))
1324 else if (!strcmp(pvid_s, existing->dev->pvid))
1325 log_error("Found duplicate PV %s: using %s not "
1326 "%s", pvid, dev_name(dev),
1327 dev_name(existing->dev));
1329 if (strcmp(pvid_s, existing->dev->pvid))
1330 log_debug("Updating pvid cache to %s (%s) from %s (%s)",
1331 pvid_s, dev_name(dev),
1332 existing->dev->pvid, dev_name(existing->dev));
1333 /* Switch over to new preferred device */
1334 existing->dev = dev;
1336 /* Has labeller changed? */
1337 if (info->label->labeller != labeller) {
1338 label_destroy(info->label);
1339 if (!(info->label = label_create(labeller)))
1340 /* FIXME leaves info without label! */
1342 info->label->info = info;
1344 label = info->label;
1347 info->fmt = (const struct format_type *) labeller->private;
1348 info->status |= CACHE_INVALID;
1350 if (!_lvmcache_update_pvid(info, pvid_s)) {
1353 label_destroy(label);
1358 if (!lvmcache_update_vgname_and_id(info, vgname, vgid, vgstatus, NULL)) {
1360 dm_hash_remove(_pvid_hash, pvid_s);
1361 strcpy(info->dev->pvid, "");
1363 label_destroy(label);
1371 static void _lvmcache_destroy_entry(struct lvmcache_info *info)
1373 _vginfo_detach_info(info);
1374 strcpy(info->dev->pvid, "");
1375 label_destroy(info->label);
1379 static void _lvmcache_destroy_vgnamelist(struct lvmcache_vginfo *vginfo)
1381 struct lvmcache_vginfo *next;
1384 next = vginfo->next;
1385 if (!_free_vginfo(vginfo))
1387 } while ((vginfo = next));
1390 static void _lvmcache_destroy_lockname(struct dm_hash_node *n)
1394 if (!dm_hash_get_data(_lock_hash, n))
1397 vgname = dm_hash_get_key(_lock_hash, n);
1399 if (!strcmp(vgname, VG_GLOBAL))
1400 _vg_global_lock_held = 1;
1402 log_error(INTERNAL_ERROR "Volume Group %s was not unlocked",
1403 dm_hash_get_key(_lock_hash, n));
1406 void lvmcache_destroy(struct cmd_context *cmd, int retain_orphans)
1408 struct dm_hash_node *n;
1409 log_verbose("Wiping internal VG cache");
1414 dm_hash_destroy(_vgid_hash);
1419 dm_hash_iter(_pvid_hash, (dm_hash_iterate_fn) _lvmcache_destroy_entry);
1420 dm_hash_destroy(_pvid_hash);
1425 dm_hash_iter(_vgname_hash,
1426 (dm_hash_iterate_fn) _lvmcache_destroy_vgnamelist);
1427 dm_hash_destroy(_vgname_hash);
1428 _vgname_hash = NULL;
1432 dm_hash_iterate(n, _lock_hash)
1433 _lvmcache_destroy_lockname(n);
1434 dm_hash_destroy(_lock_hash);
1438 if (!dm_list_empty(&_vginfos))
1439 log_error(INTERNAL_ERROR "_vginfos list should be empty");
1440 dm_list_init(&_vginfos);
1443 init_lvmcache_orphans(cmd);