~brightbox/ubuntu/raring/lvm2/fix-for-1076304

« back to all changes in this revision

Viewing changes to lib/cache/lvmcache.c

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2012-08-14 14:35:57 UTC
  • mfrom: (3.1.25 sid)
  • Revision ID: package-import@ubuntu.com-20120814143557-93aill2tp3kf3o30
Tags: 2.02.95-4ubuntu1
* Merge from Debian unstable, remaining changes:
  - debian/patches/avoid-dev-block.patch: Prefer any other device name over
    names in /dev/block/ since lvm.conf won't handle this.
  - debian/rules:
    - copy .po file to .pot file for Rosetta (Ubuntu specific).
  - debian/{dmsetup,lvm2}-udeb.install:
    - install initramfs and udev hooks in udebs (Debian bug 504341).
  - auto-start VGs as their PVs are discovered (Ubuntu specific):
    - add debian/tree/lvm2/lib/udev/rules.d/85-lvm2.rules: use watershed plus
      the sledgehammer of vgscan/vgchange to turn on VGs as they come online.
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/hooks/lvm2:
      - add 85-lvm2.rules to the list of udev rules to copy.
      - depend on udev.
    - debian/control:
      - add versioned Depend on watershed in lvm2 for udev rules.
      - add Depends on watershed-udeb in lvm2-udeb for udev rules.
      - add versioned Depend/Breaks on udev in dmsetup for udev rules.
      - add Depend on initramfs-tools in dmsetup so system is not potentially
        rendered unbootable by out-of-order dpkg configuration.
    - debian/rules:
      - do not install local-top scripts since Ubuntu mounts root using udev.
      - do not install init scripts for lvm2, since udev starts LVM.
    - debian/lvm2.postinst: handle missing lvm2 init script.
    - debian/tree/dmsetup/lib/udev/rules.d/60-persistent-storage-dm.rules:
      watch dm devices for changes with inotify
  - add mountroot failure hooks to help fix bad boots (Debian bug 468115):
    - debian/tree/lvm2/usr/share/initramfs-tools/scripts/init-premount/lvm2
  - remaining changes to upstream event manager packages (Debian bug 514706):
    - debian/rules:
      - enable dmeventd during configure.
    - debian/dmeventd.{8,manpages}: install dmeventd files.
  - rename debian/clvm.defaults to debian/clvm.default so it is installed
    correctly.
  - debian/control: add dmsetup-udeb to libdevmapper1.02.1-udeb recommends.
  - debian/rules: make sure dmsetup and lvm2 initramfs-tools scripts are
    executable.  When the Ubuntu-specific ones are added with a patch,
    they may lose their executable bit.
  - Add and install clvmd resource agent
  - Add dependency on libudev-dev to libdevmapper-dev so that the .pc file
    works.
  - debian/{clvmd.ra,clvm.init}:
    - create /run/lvm if it doesn't exist.
  - debian/clvm.init:
    - exit 3 if not running on status action.
  - Call dh_installman so that our dmeventd manpage actually gets installed
  - Install the missing fsadm manpage.

 * libdevmapper-dev:
  - move .so symlinks and pkgconfig files to multiarched locations.
  - mark libdevmapper-dev M-A: same

 * libdevmapper-event1.02.1:
  - Add Breaks: dmeventd (<< 2.02.95-4ubuntu1) due to debian symbol rename

 * debian/lvm2.{preinst,postinst,postrm}:
  - Implement removal of obsolete /etc/init.d/lvm2 conffile, which
    should not have been re-introduced in Quantal.

 * Dropped Changes, included in Debian:
  - Mostly included packages for upstream event manager (Debian bug 514706).
  - debian/patches/rules-subdir.patch: removed as reordering will cause
    build failure with dmeventd.
  - debian/patches/libdm-event-static.patch: removed as other static libs
    aren't being built anymore either.
  - Update symbols for libdevmapper-event.
  - Update libdevmapper-event, dmeventd descriptions to match Debian
    boilerplate.

 * Disappeared Changes:
  - Don't install documentation in udebs. No diff found, but no docs are
    installed into udebs either.

 * Resurected Changes:
  - corrected dropping the wrong init script. Now clvm.init is shipped
    and lvm2.init is dropped in favor of udev rules as per original
    intention (LP: #1037033).

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#include "format1.h"
29
29
#include "config.h"
30
30
 
 
31
#include "lvmetad.h"
 
32
 
 
33
#define CACHE_INVALID   0x00000001
 
34
#define CACHE_LOCKED    0x00000002
 
35
 
 
36
/* One per device */
 
37
struct lvmcache_info {
 
38
        struct dm_list list;    /* Join VG members together */
 
39
        struct dm_list mdas;    /* list head for metadata areas */
 
40
        struct dm_list das;     /* list head for data areas */
 
41
        struct lvmcache_vginfo *vginfo; /* NULL == unknown */
 
42
        struct label *label;
 
43
        const struct format_type *fmt;
 
44
        struct device *dev;
 
45
        uint64_t device_size;   /* Bytes */
 
46
        uint32_t status;
 
47
};
 
48
 
 
49
/* One per VG */
 
50
struct lvmcache_vginfo {
 
51
        struct dm_list list;    /* Join these vginfos together */
 
52
        struct dm_list infos;   /* List head for lvmcache_infos */
 
53
        const struct format_type *fmt;
 
54
        char *vgname;           /* "" == orphan */
 
55
        uint32_t status;
 
56
        char vgid[ID_LEN + 1];
 
57
        char _padding[7];
 
58
        struct lvmcache_vginfo *next; /* Another VG with same name? */
 
59
        char *creation_host;
 
60
        size_t vgmetadata_size;
 
61
        char *vgmetadata;       /* Copy of VG metadata as format_text string */
 
62
        struct dm_config_tree *cft; /* Config tree created from vgmetadata */
 
63
                                    /* Lifetime is directly tied to vgmetadata */
 
64
        struct volume_group *cached_vg;
 
65
        unsigned holders;
 
66
        unsigned vg_use_count;  /* Counter of vg reusage */
 
67
        unsigned precommitted;  /* Is vgmetadata live or precommitted? */
 
68
};
 
69
 
31
70
static struct dm_hash_table *_pvid_hash = NULL;
32
71
static struct dm_hash_table *_vgid_hash = NULL;
33
72
static struct dm_hash_table *_vgname_hash = NULL;
70
109
                _vg_global_lock_held = 0;
71
110
        }
72
111
 
 
112
        lvmetad_init();
 
113
 
73
114
        return 1;
74
115
}
75
116
 
 
117
void lvmcache_seed_infos_from_lvmetad(struct cmd_context *cmd)
 
118
{
 
119
        if (!lvmetad_active() || _has_scanned)
 
120
                return;
 
121
 
 
122
        if (!lvmetad_pv_list_to_lvmcache(cmd)) {
 
123
                stack;
 
124
                return;
 
125
        }
 
126
 
 
127
        _has_scanned = 1;
 
128
};
 
129
 
 
130
 
76
131
/* Volume Group metadata cache functions */
77
132
static void _free_cached_vgmetadata(struct lvmcache_vginfo *vginfo)
78
133
{
85
140
 
86
141
        /* Release also cached config tree */
87
142
        if (vginfo->cft) {
88
 
                destroy_config_tree(vginfo->cft);
 
143
                dm_config_destroy(vginfo->cft);
89
144
                vginfo->cft = NULL;
90
145
        }
91
146
 
102
157
        char uuid[64] __attribute__((aligned(8)));
103
158
        struct lvmcache_vginfo *vginfo;
104
159
        char *data;
105
 
        int size;
 
160
        size_t size;
106
161
 
107
 
        if (!(vginfo = vginfo_from_vgid((const char *)&vg->id))) {
 
162
        if (!(vginfo = lvmcache_vginfo_from_vgid((const char *)&vg->id))) {
108
163
                stack;
109
164
                return;
110
165
        }
132
187
                return;
133
188
        }
134
189
 
135
 
        log_debug("Metadata cache: VG %s (%s) stored (%d bytes%s).",
 
190
        log_debug("Metadata cache: VG %s (%s) stored (%" PRIsize_t " bytes%s).",
136
191
                  vginfo->vgname, uuid, size,
137
192
                  precommitted ? ", precommitted" : "");
138
193
}
147
202
         * Cache becomes invalid whenever lock state changes unless
148
203
         * exclusive VG_GLOBAL is held (i.e. while scanning).
149
204
         */
150
 
        if (!vgname_is_locked(VG_GLOBAL) && (was_locked != locked)) {
 
205
        if (!lvmcache_vgname_is_locked(VG_GLOBAL) && (was_locked != locked)) {
151
206
                info->status |= CACHE_INVALID;
152
207
                *cached_vgmetadata_valid = 0;
153
208
        }
176
231
{
177
232
        struct lvmcache_vginfo *vginfo;
178
233
 
179
 
        if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
 
234
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, NULL)))
180
235
                return;
181
236
 
182
237
        _update_cache_vginfo_lock_state(vginfo, locked);
187
242
        struct lvmcache_vginfo *vginfo;
188
243
        struct lvmcache_info *info;
189
244
 
190
 
        if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
 
245
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, NULL)))
191
246
                return;
192
247
 
193
248
        /*
205
260
                        info->status |= CACHE_INVALID;
206
261
 
207
262
        _free_cached_vgmetadata(vginfo);
 
263
 
 
264
        /* VG revert */
 
265
        if (drop_precommitted)
 
266
                vginfo->precommitted = 0;
208
267
}
209
268
 
210
269
/*
216
275
{
217
276
        struct lvmcache_vginfo *vginfo;
218
277
 
219
 
        if (!(vginfo = vginfo_from_vgname(vgname, NULL)))
 
278
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, NULL)))
220
279
                return;
221
280
 
222
281
        if (vginfo->precommitted) {
236
295
 
237
296
                /* Indicate that PVs could now be missing from the cache */
238
297
                init_full_scan_done(0);
239
 
        } else if (!vgname_is_locked(VG_GLOBAL))
 
298
        } else if (!lvmcache_vgname_is_locked(VG_GLOBAL))
240
299
                _drop_metadata(vgname, drop_precommitted);
241
300
}
242
301
 
280
339
                if (!dm_hash_get_data(_lock_hash, n))
281
340
                        return_0;
282
341
 
283
 
                vgname2 = dm_hash_get_key(_lock_hash, n);
 
342
                if (!(vgname2 = dm_hash_get_key(_lock_hash, n))) {
 
343
                        log_error(INTERNAL_ERROR "VG lock %s hits NULL.",
 
344
                                 vgname);
 
345
                        return 0;
 
346
                }
284
347
 
285
348
                if (!_vgname_order_correct(vgname2, vgname)) {
286
349
                        log_errno(EDEADLK, INTERNAL_ERROR "VG lock %s must "
287
350
                                  "be requested before %s, not after.",
288
351
                                  vgname, vgname2);
289
 
                        return_0;
 
352
                        return 0;
290
353
                }
291
354
        }
292
355
 
313
376
                _vgs_locked++;
314
377
}
315
378
 
316
 
int vgname_is_locked(const char *vgname)
 
379
int lvmcache_vgname_is_locked(const char *vgname)
317
380
{
318
381
        if (!_lock_hash)
319
382
                return 0;
336
399
                dev_close_all();
337
400
}
338
401
 
339
 
int vgs_locked(void)
 
402
int lvmcache_vgs_locked(void)
340
403
{
341
404
        return _vgs_locked;
342
405
}
362
425
}
363
426
 
364
427
/* If vgid supplied, require a match. */
365
 
struct lvmcache_vginfo *vginfo_from_vgname(const char *vgname, const char *vgid)
 
428
struct lvmcache_vginfo *lvmcache_vginfo_from_vgname(const char *vgname, const char *vgid)
366
429
{
367
430
        struct lvmcache_vginfo *vginfo;
368
431
 
369
432
        if (!vgname)
370
 
                return vginfo_from_vgid(vgid);
 
433
                return lvmcache_vginfo_from_vgid(vgid);
371
434
 
372
435
        if (!_vgname_hash)
373
436
                return NULL;
384
447
        return vginfo;
385
448
}
386
449
 
387
 
const struct format_type *fmt_from_vgname(const char *vgname, const char *vgid, unsigned revalidate_labels)
 
450
const struct format_type *lvmcache_fmt_from_vgname(struct cmd_context *cmd,
 
451
                                                   const char *vgname, const char *vgid,
 
452
                                                   unsigned revalidate_labels)
388
453
{
389
454
        struct lvmcache_vginfo *vginfo;
390
455
        struct lvmcache_info *info;
392
457
        struct dm_list *devh, *tmp;
393
458
        struct dm_list devs;
394
459
        struct device_list *devl;
 
460
        struct volume_group *vg;
 
461
        const struct format_type *fmt;
395
462
        char vgid_found[ID_LEN + 1] __attribute__((aligned(8)));
396
463
 
397
 
        if (!(vginfo = vginfo_from_vgname(vgname, vgid)))
 
464
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid))) {
 
465
                if (!lvmetad_active())
 
466
                        return NULL; /* too bad */
 
467
                /* If we don't have the info but we have lvmetad, we can ask
 
468
                 * there before failing. */
 
469
                if ((vg = lvmetad_vg_lookup(cmd, vgname, vgid))) {
 
470
                        fmt = vg->fid->fmt;
 
471
                        release_vg(vg);
 
472
                        return fmt;
 
473
                }
398
474
                return NULL;
 
475
        }
399
476
 
400
477
        /*
401
478
         * If this function is called repeatedly, only the first one needs to revalidate.
421
498
 
422
499
        dm_list_iterate_safe(devh, tmp, &devs) {
423
500
                devl = dm_list_item(devh, struct device_list);
424
 
                label_read(devl->dev, &label, UINT64_C(0));
 
501
                (void) label_read(devl->dev, &label, UINT64_C(0));
425
502
                dm_list_del(&devl->list);
426
503
                dm_free(devl);
427
504
        }
428
505
 
429
506
        /* If vginfo changed, caller needs to rescan */
430
 
        if (!(vginfo = vginfo_from_vgname(vgname, vgid_found)) ||
 
507
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid_found)) ||
431
508
            strncmp(vginfo->vgid, vgid_found, ID_LEN))
432
509
                return NULL;
433
510
 
435
512
        return vginfo->fmt;
436
513
}
437
514
 
438
 
struct lvmcache_vginfo *vginfo_from_vgid(const char *vgid)
 
515
struct lvmcache_vginfo *lvmcache_vginfo_from_vgid(const char *vgid)
439
516
{
440
517
        struct lvmcache_vginfo *vginfo;
441
518
        char id[ID_LEN + 1] __attribute__((aligned(8)));
453
530
        return vginfo;
454
531
}
455
532
 
456
 
const char *vgname_from_vgid(struct dm_pool *mem, const char *vgid)
 
533
const char *lvmcache_vgname_from_vgid(struct dm_pool *mem, const char *vgid)
457
534
{
458
535
        struct lvmcache_vginfo *vginfo;
459
536
        const char *vgname = NULL;
460
537
 
461
 
        if ((vginfo = vginfo_from_vgid(vgid)))
 
538
        if ((vginfo = lvmcache_vginfo_from_vgid(vgid)))
462
539
                vgname = vginfo->vgname;
463
540
 
464
541
        if (mem && vgname)
479
556
         * So if the VG appears to be unlocked here, it should be safe
480
557
         * to use the cached value.
481
558
         */
482
 
        if (info->vginfo && !vgname_is_locked(info->vginfo->vgname))
 
559
        if (info->vginfo && !lvmcache_vgname_is_locked(info->vginfo->vgname))
483
560
                return 1;
484
561
 
485
562
        if (!(info->status & CACHE_LOCKED))
516
593
 * If valid_only is set, data will only be returned if the cached data is
517
594
 * known still to be valid.
518
595
 */
519
 
struct lvmcache_info *info_from_pvid(const char *pvid, int valid_only)
 
596
struct lvmcache_info *lvmcache_info_from_pvid(const char *pvid, int valid_only)
520
597
{
521
598
        struct lvmcache_info *info;
522
599
        char id[ID_LEN + 1] __attribute__((aligned(8)));
536
613
        return info;
537
614
}
538
615
 
 
616
const char *lvmcache_vgname_from_info(struct lvmcache_info *info)
 
617
{
 
618
        if (info->vginfo)
 
619
                return info->vginfo->vgname;
 
620
        return NULL;
 
621
}
 
622
 
539
623
char *lvmcache_vgname_from_pvid(struct cmd_context *cmd, const char *pvid)
540
624
{
541
625
        struct lvmcache_info *info;
542
626
        char *vgname;
543
627
 
544
 
        if (!device_from_pvid(cmd, (const struct id *)pvid, NULL, NULL)) {
 
628
        if (!lvmcache_device_from_pvid(cmd, (const struct id *)pvid, NULL, NULL)) {
545
629
                log_error("Couldn't find device with uuid %s.", pvid);
546
630
                return NULL;
547
631
        }
548
632
 
549
 
        info = info_from_pvid(pvid, 0);
 
633
        info = lvmcache_info_from_pvid(pvid, 0);
550
634
        if (!info)
551
635
                return_NULL;
552
636
 
562
646
        struct label *label;
563
647
 
564
648
        if (info->status & CACHE_INVALID)
565
 
                label_read(info->dev, &label, UINT64_C(0));
 
649
                (void) label_read(info->dev, &label, UINT64_C(0));
566
650
}
567
651
 
568
652
static int _scan_invalid(void)
581
665
 
582
666
        int r = 0;
583
667
 
 
668
        if (lvmetad_active())
 
669
                return 1;
 
670
 
584
671
        /* Avoid recursion when a PVID can't be found! */
585
672
        if (_scanning_in_progress)
586
673
                return 0;
597
684
                goto out;
598
685
        }
599
686
 
600
 
        if (full_scan == 2 && !cmd->filter->use_count && !refresh_filters(cmd)) {
601
 
                log_error("refresh filters failed");
602
 
                goto out;
603
 
        }
 
687
        if (full_scan == 2 && (cmd->filter && !cmd->filter->use_count) && !refresh_filters(cmd))
 
688
                goto_out;
604
689
 
605
 
        if (!(iter = dev_iter_create(cmd->filter, (full_scan == 2) ? 1 : 0))) {
 
690
        if (!cmd->filter || !(iter = dev_iter_create(cmd->filter, (full_scan == 2) ? 1 : 0))) {
606
691
                log_error("dev_iter creation failed");
607
692
                goto out;
608
693
        }
609
694
 
610
695
        while ((dev = dev_iter_get(iter)))
611
 
                label_read(dev, &label, UINT64_C(0));
 
696
                (void) label_read(dev, &label, UINT64_C(0));
612
697
 
613
698
        dev_iter_destroy(iter);
614
699
 
635
720
        return r;
636
721
}
637
722
 
638
 
struct volume_group *lvmcache_get_vg(const char *vgid, unsigned precommitted)
 
723
struct volume_group *lvmcache_get_vg(struct cmd_context *cmd, const char *vgname,
 
724
                                     const char *vgid, unsigned precommitted)
639
725
{
640
726
        struct lvmcache_vginfo *vginfo;
641
727
        struct volume_group *vg = NULL;
642
728
        struct format_instance *fid;
643
729
        struct format_instance_ctx fic;
644
730
 
645
 
        if (!vgid || !(vginfo = vginfo_from_vgid(vgid)) || !vginfo->vgmetadata)
 
731
        /*
 
732
         * We currently do not store precommitted metadata in lvmetad at
 
733
         * all. This means that any request for precommitted metadata is served
 
734
         * using the classic scanning mechanics, and read from disk or from
 
735
         * lvmcache.
 
736
         */
 
737
        if (lvmetad_active() && !precommitted) {
 
738
                /* Still serve the locally cached VG if available */
 
739
                if (vgid && (vginfo = lvmcache_vginfo_from_vgid(vgid)) &&
 
740
                    vginfo->vgmetadata && (vg = vginfo->cached_vg))
 
741
                        goto out;
 
742
                return lvmetad_vg_lookup(cmd, vgname, vgid);
 
743
        }
 
744
 
 
745
        if (!vgid || !(vginfo = lvmcache_vginfo_from_vgid(vgid)) || !vginfo->vgmetadata)
646
746
                return NULL;
647
747
 
648
748
        if (!_vginfo_is_valid(vginfo))
668
768
        if ((vg = vginfo->cached_vg))
669
769
                goto out;
670
770
 
671
 
        fic.type = FMT_INSTANCE_VG | FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS;
 
771
        fic.type = FMT_INSTANCE_MDAS | FMT_INSTANCE_AUX_MDAS;
672
772
        fic.context.vg_ref.vg_name = vginfo->vgname;
673
773
        fic.context.vg_ref.vg_id = vgid;
674
774
        if (!(fid = vginfo->fmt->ops->create_instance(vginfo->fmt, &fic)))
677
777
        /* Build config tree from vgmetadata, if not yet cached */
678
778
        if (!vginfo->cft &&
679
779
            !(vginfo->cft =
680
 
              create_config_tree_from_string(vginfo->vgmetadata)))
 
780
              dm_config_from_string(vginfo->vgmetadata)))
681
781
                goto_bad;
682
782
 
683
783
        if (!(vg = import_vg_from_config_tree(vginfo->cft, fid)))
706
806
        return NULL;
707
807
}
708
808
 
709
 
int vginfo_holders_dec_and_test_for_zero(struct lvmcache_vginfo *vginfo)
 
809
// #if 0
 
810
int lvmcache_vginfo_holders_dec_and_test_for_zero(struct lvmcache_vginfo *vginfo)
710
811
{
711
812
        log_debug("VG %s decrementing %d holder(s) at %p.",
712
813
                  vginfo->cached_vg->name, vginfo->holders, vginfo->cached_vg);
729
830
 
730
831
        return 1;
731
832
}
 
833
// #endif
732
834
 
733
835
struct dm_list *lvmcache_get_vgids(struct cmd_context *cmd,
734
836
                                   int include_internal)
736
838
        struct dm_list *vgids;
737
839
        struct lvmcache_vginfo *vginfo;
738
840
 
 
841
        // TODO plug into lvmetad here automagically?
739
842
        lvmcache_label_scan(cmd, 0);
740
843
 
741
844
        if (!(vgids = str_list_create(cmd->mem))) {
796
899
                return NULL;
797
900
        }
798
901
 
799
 
        if (!(vginfo = vginfo_from_vgname(vgname, vgid)))
 
902
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid)))
800
903
                return pvids;
801
904
 
802
905
        dm_list_iterate_items(info, &vginfo->infos) {
816
919
        struct lvmcache_info *info;
817
920
        struct label *label;
818
921
 
819
 
        if ((info = info_from_pvid((const char *) pvid, 0))) {
 
922
        if ((info = lvmcache_info_from_pvid((const char *) pvid, 0))) {
 
923
                if (lvmetad_active()) {
 
924
                        if (info->label && label_sector)
 
925
                                *label_sector = info->label->sector;
 
926
                        return info->dev;
 
927
                }
 
928
 
820
929
                if (label_read(info->dev, &label, UINT64_C(0))) {
821
930
                        info = (struct lvmcache_info *) label->info;
822
931
                        if (id_equal(pvid, (struct id *) &info->dev->pvid)) {
829
938
        return NULL;
830
939
}
831
940
 
832
 
struct device *device_from_pvid(struct cmd_context *cmd, const struct id *pvid,
 
941
struct device *lvmcache_device_from_pvid(struct cmd_context *cmd, const struct id *pvid,
833
942
                                unsigned *scan_done_once, uint64_t *label_sector)
834
943
{
835
944
        struct device *dev;
861
970
        return NULL;
862
971
}
863
972
 
864
 
const char *pvid_from_devname(struct cmd_context *cmd,
 
973
const char *lvmcache_pvid_from_devname(struct cmd_context *cmd,
865
974
                              const char *devname)
866
975
{
867
976
        struct device *dev;
887
996
 
888
997
        _free_cached_vgmetadata(vginfo);
889
998
 
890
 
        vginfo2 = primary_vginfo = vginfo_from_vgname(vginfo->vgname, NULL);
 
999
        vginfo2 = primary_vginfo = lvmcache_vginfo_from_vgname(vginfo->vgname, NULL);
891
1000
 
892
1001
        if (vginfo == primary_vginfo) {
893
1002
                dm_hash_remove(_vgname_hash, vginfo->vgname);
908
1017
        dm_free(vginfo->creation_host);
909
1018
 
910
1019
        if (*vginfo->vgid && _vgid_hash &&
911
 
            vginfo_from_vgid(vginfo->vgid) == vginfo)
 
1020
            lvmcache_vginfo_from_vgid(vginfo->vgid) == vginfo)
912
1021
                dm_hash_remove(_vgid_hash, vginfo->vgid);
913
1022
 
914
1023
        dm_list_del(&vginfo->list);
957
1066
        /*
958
1067
         * Nothing to do if already stored with same pvid.
959
1068
         */
 
1069
 
960
1070
        if (((dm_hash_lookup(_pvid_hash, pvid)) == info) &&
961
1071
            !strcmp(info->dev->pvid, pvid))
962
1072
                return 1;
985
1095
        if (vginfo && *vginfo->vgid)
986
1096
                dm_hash_remove(_vgid_hash, vginfo->vgid);
987
1097
        if (!vgid) {
 
1098
                /* FIXME: unreachable code path */
988
1099
                log_debug("lvmcache: %s: clearing VGID", info ? dev_name(info->dev) : vginfo->vgname);
989
1100
                return 1;
990
1101
        }
999
1110
 
1000
1111
        if (!is_orphan_vg(vginfo->vgname))
1001
1112
                log_debug("lvmcache: %s: setting %s VGID to %s",
1002
 
                          dev_name(info->dev), vginfo->vgname,
1003
 
                          vginfo->vgid);
 
1113
                          (info) ? dev_name(info->dev) : "",
 
1114
                          vginfo->vgname, vginfo->vgid);
1004
1115
 
1005
1116
        return 1;
1006
1117
}
1107
1218
                _drop_vginfo(info, info->vginfo);
1108
1219
 
1109
1220
        /* Get existing vginfo or create new one */
1110
 
        if (!(vginfo = vginfo_from_vgname(vgname, vgid))) {
 
1221
        if (!(vginfo = lvmcache_vginfo_from_vgname(vgname, vgid))) {
1111
1222
/*** FIXME - vginfo ends up duplicated instead of renamed.
1112
1223
                // Renaming?  This lookup fails.
1113
1224
                if ((vginfo = vginfo_from_vgid(vgid))) {
1159
1270
                 * If we're scanning and there's an invalidated entry, remove it.
1160
1271
                 * Otherwise we risk bogus warnings of duplicate VGs.
1161
1272
                 */
1162
 
                while ((primary_vginfo = vginfo_from_vgname(vgname, NULL)) &&
 
1273
                while ((primary_vginfo = lvmcache_vginfo_from_vgname(vgname, NULL)) &&
1163
1274
                       _scanning_in_progress && _vginfo_is_invalid(primary_vginfo)) {
1164
 
                        orphan_vginfo = vginfo_from_vgname(primary_vginfo->fmt->orphan_vg_name, NULL);
 
1275
                        orphan_vginfo = lvmcache_vginfo_from_vgname(primary_vginfo->fmt->orphan_vg_name, NULL);
1165
1276
                        if (!orphan_vginfo) {
1166
1277
                                log_error(INTERNAL_ERROR "Orphan vginfo %s lost from cache.",
1167
1278
                                          primary_vginfo->fmt->orphan_vg_name);
1209
1320
        else if (!_lvmcache_update_vgid(NULL, vginfo, vgid)) /* Orphans */
1210
1321
                return_0;
1211
1322
 
1212
 
        _update_cache_vginfo_lock_state(vginfo, vgname_is_locked(vgname));
 
1323
        _update_cache_vginfo_lock_state(vginfo, lvmcache_vgname_is_locked(vgname));
1213
1324
 
1214
1325
        /* FIXME Check consistency of list! */
1215
1326
        vginfo->fmt = fmt;
1286
1397
                vgid = vgname;
1287
1398
        }
1288
1399
 
 
1400
        /* When using lvmetad, the PV could not have become orphaned. */
 
1401
        if (lvmetad_active() && is_orphan_vg(vgname) && info->vginfo)
 
1402
                return 1;
 
1403
 
1289
1404
        /* If PV without mdas is already in a real VG, don't make it orphan */
1290
1405
        if (is_orphan_vg(vgname) && info->vginfo &&
1291
1406
            mdas_empty_or_ignored(&info->mdas) &&
1316
1431
        dm_list_iterate_items(pvl, &vg->pvs) {
1317
1432
                strncpy(pvid_s, (char *) &pvl->pv->id, sizeof(pvid_s) - 1);
1318
1433
                /* FIXME Could pvl->pv->dev->pvid ever be different? */
1319
 
                if ((info = info_from_pvid(pvid_s, 0)) &&
 
1434
                if ((info = lvmcache_info_from_pvid(pvid_s, 0)) &&
1320
1435
                    !lvmcache_update_vgname_and_id(info, vg->name,
1321
1436
                                                   (char *) &vg->id,
1322
1437
                                                   vg->status, NULL))
1347
1462
        strncpy(pvid_s, pvid, sizeof(pvid_s) - 1);
1348
1463
        pvid_s[sizeof(pvid_s) - 1] = '\0';
1349
1464
 
1350
 
        if (!(existing = info_from_pvid(pvid_s, 0)) &&
1351
 
            !(existing = info_from_pvid(dev->pvid, 0))) {
 
1465
        if (!(existing = lvmcache_info_from_pvid(pvid_s, 0)) &&
 
1466
            !(existing = lvmcache_info_from_pvid(dev->pvid, 0))) {
1352
1467
                if (!(label = label_create(labeller)))
1353
1468
                        return_NULL;
1354
1469
                if (!(info = dm_zalloc(sizeof(*info)))) {
1361
1476
                info->label = label;
1362
1477
                dm_list_init(&info->list);
1363
1478
                info->dev = dev;
 
1479
 
 
1480
                lvmcache_del_mdas(info);
 
1481
                lvmcache_del_das(info);
1364
1482
        } else {
1365
1483
                if (existing->dev != dev) {
1366
1484
                        /* Is the existing entry a duplicate pvid e.g. md ? */
1515
1633
        dm_list_init(&_vginfos);
1516
1634
 
1517
1635
        if (retain_orphans)
1518
 
                init_lvmcache_orphans(cmd);
 
1636
                if (!init_lvmcache_orphans(cmd))
 
1637
                        stack;
 
1638
}
 
1639
 
 
1640
int lvmcache_pvid_is_locked(const char *pvid) {
 
1641
        struct lvmcache_info *info;
 
1642
        info = lvmcache_info_from_pvid(pvid, 0);
 
1643
        if (!info || !info->vginfo)
 
1644
                return 0;
 
1645
 
 
1646
        return lvmcache_vgname_is_locked(info->vginfo->vgname);
 
1647
}
 
1648
 
 
1649
int lvmcache_fid_add_mdas(struct lvmcache_info *info, struct format_instance *fid,
 
1650
                          const char *id, int id_len)
 
1651
{
 
1652
        return fid_add_mdas(fid, &info->mdas, id, id_len);
 
1653
}
 
1654
 
 
1655
int lvmcache_fid_add_mdas_pv(struct lvmcache_info *info, struct format_instance *fid)
 
1656
{
 
1657
        return lvmcache_fid_add_mdas(info, fid, info->dev->pvid, ID_LEN);
 
1658
}
 
1659
 
 
1660
int lvmcache_fid_add_mdas_vg(struct lvmcache_vginfo *vginfo, struct format_instance *fid)
 
1661
{
 
1662
        struct lvmcache_info *info;
 
1663
        dm_list_iterate_items(info, &vginfo->infos) {
 
1664
                if (!lvmcache_fid_add_mdas_pv(info, fid))
 
1665
                        return_0;
 
1666
        }
 
1667
        return 1;
 
1668
}
 
1669
 
 
1670
static int _get_pv_if_in_vg(struct lvmcache_info *info,
 
1671
                            struct physical_volume *pv)
 
1672
{
 
1673
        char vgname[NAME_LEN + 1];
 
1674
        char vgid[ID_LEN + 1];
 
1675
 
 
1676
        if (info->vginfo && info->vginfo->vgname &&
 
1677
            !is_orphan_vg(info->vginfo->vgname)) {
 
1678
                /*
 
1679
                 * get_pv_from_vg_by_id() may call
 
1680
                 * lvmcache_label_scan() and drop cached
 
1681
                 * vginfo so make a local copy of string.
 
1682
                 */
 
1683
                strcpy(vgname, info->vginfo->vgname);
 
1684
                memcpy(vgid, info->vginfo->vgid, sizeof(vgid));
 
1685
 
 
1686
                if (get_pv_from_vg_by_id(info->fmt, vgname, vgid,
 
1687
                                         info->dev->pvid, pv))
 
1688
                        return 1;
 
1689
        }
 
1690
 
 
1691
        return 0;
 
1692
}
 
1693
 
 
1694
int lvmcache_populate_pv_fields(struct lvmcache_info *info,
 
1695
                                struct physical_volume *pv,
 
1696
                                int scan_label_only)
 
1697
{
 
1698
        struct data_area_list *da;
 
1699
 
 
1700
        /* Have we already cached vgname? */
 
1701
        if (!scan_label_only && _get_pv_if_in_vg(info, pv))
 
1702
                return 1;
 
1703
 
 
1704
        /* Perform full scan (just the first time) and try again */
 
1705
        if (!scan_label_only && !critical_section() && !full_scan_done()) {
 
1706
                lvmcache_label_scan(info->fmt->cmd, 2);
 
1707
 
 
1708
                if (_get_pv_if_in_vg(info, pv))
 
1709
                        return 1;
 
1710
        }
 
1711
 
 
1712
        /* Orphan */
 
1713
        pv->dev = info->dev;
 
1714
        pv->fmt = info->fmt;
 
1715
        pv->size = info->device_size >> SECTOR_SHIFT;
 
1716
        pv->vg_name = FMT_TEXT_ORPHAN_VG_NAME;
 
1717
        memcpy(&pv->id, &info->dev->pvid, sizeof(pv->id));
 
1718
 
 
1719
        /* Currently only support exactly one data area */
 
1720
        if (dm_list_size(&info->das) != 1) {
 
1721
                log_error("Must be exactly one data area (found %d) on PV %s",
 
1722
                          dm_list_size(&info->das), dev_name(info->dev));
 
1723
                return 0;
 
1724
        }
 
1725
 
 
1726
        dm_list_iterate_items(da, &info->das)
 
1727
                pv->pe_start = da->disk_locn.offset >> SECTOR_SHIFT;
 
1728
 
 
1729
        return 1;
 
1730
}
 
1731
 
 
1732
int lvmcache_check_format(struct lvmcache_info *info, const struct format_type *fmt)
 
1733
{
 
1734
        if (info->fmt != fmt) {
 
1735
                log_error("PV %s is a different format (seqno %s)",
 
1736
                          dev_name(info->dev), info->fmt->name);
 
1737
                return 0;
 
1738
        }
 
1739
        return 1;
 
1740
}
 
1741
 
 
1742
void lvmcache_del_mdas(struct lvmcache_info *info)
 
1743
{
 
1744
        if (info->mdas.n)
 
1745
                del_mdas(&info->mdas);
 
1746
        dm_list_init(&info->mdas);
 
1747
}
 
1748
 
 
1749
void lvmcache_del_das(struct lvmcache_info *info)
 
1750
{
 
1751
        if (info->das.n)
 
1752
                del_das(&info->das);
 
1753
        dm_list_init(&info->das);
 
1754
}
 
1755
 
 
1756
int lvmcache_add_mda(struct lvmcache_info *info, struct device *dev,
 
1757
                     uint64_t start, uint64_t size, unsigned ignored)
 
1758
{
 
1759
        return add_mda(info->fmt, NULL, &info->mdas, dev, start, size, ignored);
 
1760
}
 
1761
 
 
1762
int lvmcache_add_da(struct lvmcache_info *info, uint64_t start, uint64_t size)
 
1763
{
 
1764
        return add_da(NULL, &info->das, start, size);
 
1765
}
 
1766
 
 
1767
 
 
1768
void lvmcache_update_pv(struct lvmcache_info *info, struct physical_volume *pv,
 
1769
                        const struct format_type *fmt)
 
1770
{
 
1771
        info->device_size = pv->size << SECTOR_SHIFT;
 
1772
        info->fmt = fmt;
 
1773
}
 
1774
 
 
1775
int lvmcache_update_das(struct lvmcache_info *info, struct physical_volume *pv)
 
1776
{
 
1777
        struct data_area_list *da;
 
1778
        if (info->das.n) {
 
1779
                if (!pv->pe_start)
 
1780
                        dm_list_iterate_items(da, &info->das)
 
1781
                                pv->pe_start = da->disk_locn.offset >> SECTOR_SHIFT;
 
1782
                del_das(&info->das);
 
1783
        } else
 
1784
                dm_list_init(&info->das);
 
1785
 
 
1786
        if (!add_da(NULL, &info->das, pv->pe_start << SECTOR_SHIFT, 0 /*pv->size << SECTOR_SHIFT*/))
 
1787
                return_0;
 
1788
 
 
1789
        return 1;
 
1790
}
 
1791
 
 
1792
int lvmcache_foreach_pv(struct lvmcache_vginfo *vginfo,
 
1793
                        int (*fun)(struct lvmcache_info *, void *),
 
1794
                        void *baton)
 
1795
{
 
1796
        struct lvmcache_info *info;
 
1797
        dm_list_iterate_items(info, &vginfo->infos) {
 
1798
                if (!fun(info, baton))
 
1799
                        return_0;
 
1800
        }
 
1801
 
 
1802
        return 1;
 
1803
}
 
1804
 
 
1805
int lvmcache_foreach_mda(struct lvmcache_info *info,
 
1806
                         int (*fun)(struct metadata_area *, void *),
 
1807
                         void *baton)
 
1808
{
 
1809
        struct metadata_area *mda;
 
1810
        dm_list_iterate_items(mda, &info->mdas) {
 
1811
                if (!fun(mda, baton))
 
1812
                        return_0;
 
1813
        }
 
1814
 
 
1815
        return 1;
 
1816
}
 
1817
 
 
1818
int lvmcache_mda_count(struct lvmcache_info *info)
 
1819
{
 
1820
        return dm_list_size(&info->mdas);
 
1821
}
 
1822
 
 
1823
int lvmcache_foreach_da(struct lvmcache_info *info,
 
1824
                        int (*fun)(struct disk_locn *, void *),
 
1825
                        void *baton)
 
1826
{
 
1827
        struct data_area_list *da;
 
1828
        dm_list_iterate_items(da, &info->das) {
 
1829
                if (!fun(&da->disk_locn, baton))
 
1830
                        return_0;
 
1831
        }
 
1832
 
 
1833
        return 1;
 
1834
}
 
1835
 
 
1836
/*
 
1837
 * The lifetime of the label returned is tied to the lifetime of the
 
1838
 * lvmcache_info which is the same as lvmcache itself.
 
1839
 */
 
1840
struct label *lvmcache_get_label(struct lvmcache_info *info) {
 
1841
        return info->label;
 
1842
}
 
1843
 
 
1844
void lvmcache_make_valid(struct lvmcache_info *info) {
 
1845
        info->status &= ~CACHE_INVALID;
 
1846
}
 
1847
 
 
1848
uint64_t lvmcache_device_size(struct lvmcache_info *info) {
 
1849
        return info->device_size;
 
1850
}
 
1851
 
 
1852
void lvmcache_set_device_size(struct lvmcache_info *info, uint64_t size) {
 
1853
        info->device_size = size;
 
1854
}
 
1855
 
 
1856
struct device *lvmcache_device(struct lvmcache_info *info) {
 
1857
        return info->dev;
 
1858
}
 
1859
 
 
1860
int lvmcache_is_orphan(struct lvmcache_info *info) {
 
1861
        if (!info->vginfo)
 
1862
                return 1; /* FIXME? */
 
1863
        return is_orphan_vg(info->vginfo->vgname);
 
1864
}
 
1865
 
 
1866
int lvmcache_vgid_is_cached(const char *vgid) {
 
1867
        struct lvmcache_vginfo *vginfo;
 
1868
 
 
1869
        if (lvmetad_active())
 
1870
                return 1;
 
1871
 
 
1872
        vginfo = lvmcache_vginfo_from_vgid(vgid);
 
1873
 
 
1874
        if (!vginfo || !vginfo->vgname)
 
1875
                return 0;
 
1876
 
 
1877
        if (is_orphan_vg(vginfo->vgname))
 
1878
                return 0;
 
1879
 
 
1880
        return 1;
 
1881
}
 
1882
 
 
1883
/*
 
1884
 * Return true iff it is impossible to find out from this info alone whether the
 
1885
 * PV in question is or is not an orphan.
 
1886
 */
 
1887
int lvmcache_uncertain_ownership(struct lvmcache_info *info) {
 
1888
        return mdas_empty_or_ignored(&info->mdas);
 
1889
}
 
1890
 
 
1891
int lvmcache_smallest_mda_size(struct lvmcache_info *info)
 
1892
{
 
1893
        return find_min_mda_size(&info->mdas);
 
1894
}
 
1895
 
 
1896
const struct format_type *lvmcache_fmt(struct lvmcache_info *info) {
 
1897
        return info->fmt;
1519
1898
}