~ubuntu-branches/ubuntu/saucy/lvm2/saucy

« back to all changes in this revision

Viewing changes to lib/activate/activate.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:
1
1
/*
2
2
 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
3
 
 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
 
3
 * Copyright (C) 2004-2012 Red Hat, Inc. All rights reserved.
4
4
 *
5
5
 * This file is part of LVM2.
6
6
 *
38
38
 
39
39
int lvm1_present(struct cmd_context *cmd)
40
40
{
41
 
        char path[PATH_MAX];
 
41
        static char path[PATH_MAX];
42
42
 
43
43
        if (dm_snprintf(path, sizeof(path), "%s/lvm/global", cmd->proc_dir)
44
44
            < 0) {
147
147
{
148
148
        return 0;
149
149
}
150
 
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
 
150
int lvm_dm_prefix_check(int major, int minor, const char *prefix)
 
151
{
 
152
        return 0;
 
153
}
 
154
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int use_layer,
151
155
            struct lvinfo *info, int with_open_count, int with_read_ahead)
152
156
{
153
157
        return 0;
154
158
}
155
 
int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
156
 
                    unsigned origin_only,
 
159
int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, int use_layer,
157
160
                    struct lvinfo *info, int with_open_count, int with_read_ahead)
158
161
{
159
162
        return 0;
160
163
}
 
164
int lv_check_not_in_use(struct cmd_context *cmd __attribute__((unused)),
 
165
                        struct logical_volume *lv, struct lvinfo *info)
 
166
{
 
167
        return 0;
 
168
}
161
169
int lv_snapshot_percent(const struct logical_volume *lv, percent_t *percent)
162
170
{
163
171
        return 0;
167
175
{
168
176
        return 0;
169
177
}
170
 
int lvs_in_vg_activated(struct volume_group *vg)
 
178
int lv_raid_percent(const struct logical_volume *lv, percent_t *percent)
 
179
{
 
180
        return 0;
 
181
}
 
182
int lv_thin_pool_percent(const struct logical_volume *lv, int metadata,
 
183
                         percent_t *percent)
 
184
{
 
185
        return 0;
 
186
}
 
187
int lv_thin_percent(const struct logical_volume *lv, int mapped,
 
188
                    percent_t *percent)
 
189
{
 
190
        return 0;
 
191
}
 
192
int lv_thin_pool_transaction_id(const struct logical_volume *lv,
 
193
                                uint64_t *transaction_id)
 
194
{
 
195
        return 0;
 
196
}
 
197
int lvs_in_vg_activated(const struct volume_group *vg)
171
198
{
172
199
        return 0;
173
200
}
181
208
        return 1;
182
209
}
183
210
*******/
184
 
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
 
211
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only, unsigned exclusive)
185
212
{
186
213
        return 1;
187
214
}
190
217
        return 1;
191
218
}
192
219
int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
193
 
                        unsigned origin_only, unsigned exclusive)
 
220
                        unsigned origin_only, unsigned exclusive, unsigned revert)
194
221
{
195
222
        return 1;
196
223
}
226
253
void activation_exit(void)
227
254
{
228
255
}
229
 
int lv_is_active(struct logical_volume *lv)
230
 
{
231
 
        return 0;
232
 
}
233
 
int lv_is_active_exclusive_locally(struct logical_volume *lv)
234
 
{
235
 
        return 0;
236
 
}
237
 
int lv_is_active_exclusive_remotely(struct logical_volume *lv)
238
 
{
239
 
        return 0;
240
 
}
 
256
 
 
257
int lv_is_active(const struct logical_volume *lv)
 
258
{
 
259
        return 0;
 
260
}
 
261
int lv_is_active_but_not_locally(const struct logical_volume *lv)
 
262
{
 
263
        return 0;
 
264
}
 
265
int lv_is_active_exclusive(const struct logical_volume *lv)
 
266
{
 
267
        return 0;
 
268
}
 
269
int lv_is_active_exclusive_locally(const struct logical_volume *lv)
 
270
{
 
271
        return 0;
 
272
}
 
273
int lv_is_active_exclusive_remotely(const struct logical_volume *lv)
 
274
{
 
275
        return 0;
 
276
}
 
277
 
241
278
int lv_check_transient(struct logical_volume *lv)
242
279
{
243
280
        return 1;
244
281
}
245
282
int monitor_dev_for_events(struct cmd_context *cmd, struct logical_volume *lv,
246
 
                           struct lv_activate_opts *laopts, int monitor)
 
283
                           const struct lv_activate_opts *laopts, int monitor)
247
284
{
248
285
        return 1;
249
286
}
 
287
/* fs.c */
 
288
void fs_unlock(void)
 
289
{
 
290
}
 
291
/* dev_manager.c */
 
292
#include "targets.h"
 
293
int add_areas_line(struct dev_manager *dm, struct lv_segment *seg,
 
294
                   struct dm_tree_node *node, uint32_t start_area,
 
295
                   uint32_t areas)
 
296
{
 
297
        return 0;
 
298
}
 
299
int device_is_usable(struct device *dev)
 
300
{
 
301
        return 0;
 
302
}
 
303
int lv_has_target_type(struct dm_pool *mem, struct logical_volume *lv,
 
304
                       const char *layer, const char *target_type)
 
305
{
 
306
        return 0;
 
307
}
250
308
#else                           /* DEVMAPPER_SUPPORT */
251
309
 
252
310
static int _activation = 1;
270
328
        return _activation;
271
329
}
272
330
 
273
 
static int _passes_activation_filter(struct cmd_context *cmd,
274
 
                                     struct logical_volume *lv)
 
331
static int _passes_volumes_filter(struct cmd_context *cmd,
 
332
                                  struct logical_volume *lv,
 
333
                                  const struct dm_config_node *cn,
 
334
                                  const char *config_path)
275
335
{
276
 
        const struct config_node *cn;
277
 
        const struct config_value *cv;
 
336
        const struct dm_config_value *cv;
278
337
        const char *str;
279
 
        char path[PATH_MAX];
280
 
 
281
 
        if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
282
 
                log_verbose("activation/volume_list configuration setting "
283
 
                            "not defined, checking only host tags for %s/%s",
284
 
                            lv->vg->name, lv->name);
285
 
 
286
 
                /* If no host tags defined, activate */
287
 
                if (dm_list_empty(&cmd->tags))
288
 
                        return 1;
289
 
 
290
 
                /* If any host tag matches any LV or VG tag, activate */
291
 
                if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
292
 
                    str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
293
 
                        return 1;
294
 
 
295
 
                log_verbose("No host tag matches %s/%s",
296
 
                            lv->vg->name, lv->name);
297
 
 
298
 
                /* Don't activate */
299
 
                return 0;
300
 
        }
301
 
        else
302
 
                log_verbose("activation/volume_list configuration setting "
303
 
                            "defined, checking the list to match %s/%s",
304
 
                            lv->vg->name, lv->name);
 
338
        static char path[PATH_MAX];
 
339
 
 
340
        log_verbose("%s configuration setting defined: "
 
341
                    "Checking the list to match %s/%s",
 
342
                    config_path, lv->vg->name, lv->name);
305
343
 
306
344
        for (cv = cn->v; cv; cv = cv->next) {
307
 
                if (cv->type != CFG_STRING) {
308
 
                        log_error("Ignoring invalid string in config file "
309
 
                                  "activation/volume_list");
 
345
                if (cv->type != DM_CFG_STRING) {
 
346
                        log_error("Ignoring invalid string in config file %s",
 
347
                                  config_path);
310
348
                        continue;
311
349
                }
312
350
                str = cv->v.str;
313
351
                if (!*str) {
314
 
                        log_error("Ignoring empty string in config file "
315
 
                                  "activation/volume_list");
 
352
                        log_error("Ignoring empty string in config file %s",
 
353
                                  config_path);
316
354
                        continue;
317
355
                }
318
356
 
322
360
                        str++;
323
361
                        if (!*str) {
324
362
                                log_error("Ignoring empty tag in config file "
325
 
                                          "activation/volume_list");
 
363
                                          "%s", config_path);
326
364
                                continue;
327
365
                        }
328
366
                        /* If any host tag matches any LV or VG tag, activate */
359
397
                        return 1;
360
398
        }
361
399
 
362
 
        log_verbose("No item supplied in activation/volume_list configuration "
363
 
                    "setting matches %s/%s", lv->vg->name, lv->name);
 
400
        log_verbose("No item supplied in %s configuration setting "
 
401
                    "matches %s/%s", config_path, lv->vg->name, lv->name);
364
402
 
365
403
        return 0;
366
404
}
367
405
 
 
406
static int _passes_activation_filter(struct cmd_context *cmd,
 
407
                                     struct logical_volume *lv)
 
408
{
 
409
        const struct dm_config_node *cn;
 
410
 
 
411
        if (!(cn = find_config_tree_node(cmd, "activation/volume_list"))) {
 
412
                log_verbose("activation/volume_list configuration setting "
 
413
                            "not defined: Checking only host tags for %s/%s",
 
414
                            lv->vg->name, lv->name);
 
415
 
 
416
                /* If no host tags defined, activate */
 
417
                if (dm_list_empty(&cmd->tags))
 
418
                        return 1;
 
419
 
 
420
                /* If any host tag matches any LV or VG tag, activate */
 
421
                if (str_list_match_list(&cmd->tags, &lv->tags, NULL) ||
 
422
                    str_list_match_list(&cmd->tags, &lv->vg->tags, NULL))
 
423
                        return 1;
 
424
 
 
425
                log_verbose("No host tag matches %s/%s",
 
426
                            lv->vg->name, lv->name);
 
427
 
 
428
                /* Don't activate */
 
429
                return 0;
 
430
        }
 
431
 
 
432
        return _passes_volumes_filter(cmd, lv, cn, "activation/volume_list");
 
433
}
 
434
 
 
435
static int _passes_readonly_filter(struct cmd_context *cmd,
 
436
                                   struct logical_volume *lv)
 
437
{
 
438
        const struct dm_config_node *cn;
 
439
 
 
440
        if (!(cn = find_config_tree_node(cmd, "activation/read_only_volume_list")))
 
441
                return 0;
 
442
 
 
443
        return _passes_volumes_filter(cmd, lv, cn, "activation/read_only_volume_list");
 
444
}
 
445
 
368
446
int library_version(char *version, size_t size)
369
447
{
370
448
        if (!activation())
400
478
        if (!dm_task_run(dmt)) {
401
479
                log_debug("Failed to get %s target version", target_name);
402
480
                /* Assume this was because LIST_VERSIONS isn't supported */
403
 
                return 1;
 
481
                *maj = 0;
 
482
                *min = 0;
 
483
                *patchlevel = 0;
 
484
                r = 1;
 
485
                goto out;
404
486
        }
405
487
 
406
488
        target = dm_task_get_versions(dmt);
425
507
        return r;
426
508
}
427
509
 
 
510
int lvm_dm_prefix_check(int major, int minor, const char *prefix)
 
511
{
 
512
        struct dm_task *dmt;
 
513
        const char *uuid;
 
514
        int r;
 
515
 
 
516
        if (!(dmt = dm_task_create(DM_DEVICE_STATUS)))
 
517
                return_0;
 
518
 
 
519
        if (!dm_task_set_minor(dmt, minor) ||
 
520
            !dm_task_set_major(dmt, major) ||
 
521
            !dm_task_run(dmt) ||
 
522
            !(uuid = dm_task_get_uuid(dmt))) {
 
523
                dm_task_destroy(dmt);
 
524
                return 0;
 
525
        }
 
526
 
 
527
        r = strncasecmp(uuid, prefix, strlen(prefix));
 
528
        dm_task_destroy(dmt);
 
529
 
 
530
        return r ? 0 : 1;
 
531
}
 
532
 
428
533
int module_present(struct cmd_context *cmd, const char *target_name)
429
534
{
430
535
        int ret = 0;
471
576
/*
472
577
 * Returns 1 if info structure populated, else 0 on failure.
473
578
 */
474
 
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, unsigned origin_only,
 
579
int lv_info(struct cmd_context *cmd, const struct logical_volume *lv, int use_layer,
475
580
            struct lvinfo *info, int with_open_count, int with_read_ahead)
476
581
{
477
582
        struct dm_info dminfo;
 
583
        const char *layer;
478
584
 
479
585
        if (!activation())
480
586
                return 0;
491
597
                        fs_unlock(); /* For non clustered - wait if there are non-delete ops */
492
598
        }
493
599
 
494
 
        if (!dev_manager_info(lv->vg->cmd->mem, lv, origin_only ? "real" : NULL, with_open_count,
 
600
        if (use_layer && lv_is_thin_pool(lv))
 
601
                layer = "tpool";
 
602
        else if (use_layer && lv_is_origin(lv))
 
603
                layer = "real";
 
604
        else
 
605
                layer = NULL;
 
606
 
 
607
        if (!dev_manager_info(lv->vg->cmd->mem, lv, layer, with_open_count,
495
608
                              with_read_ahead, &dminfo, &info->read_ahead))
496
609
                return_0;
497
610
 
507
620
        return 1;
508
621
}
509
622
 
510
 
int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s,
511
 
                    unsigned origin_only,
 
623
int lv_info_by_lvid(struct cmd_context *cmd, const char *lvid_s, int use_layer,
512
624
                    struct lvinfo *info, int with_open_count, int with_read_ahead)
513
625
{
514
626
        int r;
517
629
        if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
518
630
                return 0;
519
631
 
520
 
        if (!lv_is_origin(lv))
521
 
                origin_only = 0;
522
 
 
523
 
        r = lv_info(cmd, lv, origin_only, info, with_open_count, with_read_ahead);
 
632
        r = lv_info(cmd, lv, use_layer, info, with_open_count, with_read_ahead);
524
633
        release_vg(lv->vg);
525
634
 
526
635
        return r;
527
636
}
528
637
 
 
638
int lv_check_not_in_use(struct cmd_context *cmd __attribute__((unused)),
 
639
                        struct logical_volume *lv, struct lvinfo *info)
 
640
{
 
641
        if (!info->exists)
 
642
                return 1;
 
643
 
 
644
        /* If sysfs is not used, use open_count information only. */
 
645
        if (!*dm_sysfs_dir()) {
 
646
                if (info->open_count) {
 
647
                        log_error("Logical volume %s/%s in use.",
 
648
                                  lv->vg->name, lv->name);
 
649
                        return 0;
 
650
                }
 
651
 
 
652
                return 1;
 
653
        }
 
654
 
 
655
        if (dm_device_has_holders(info->major, info->minor)) {
 
656
                log_error("Logical volume %s/%s is used by another device.",
 
657
                          lv->vg->name, lv->name);
 
658
                return 0;
 
659
        }
 
660
 
 
661
        if (dm_device_has_mounted_fs(info->major, info->minor)) {
 
662
                log_error("Logical volume %s/%s contains a filesystem in use.",
 
663
                          lv->vg->name, lv->name);
 
664
                return 0;
 
665
        }
 
666
 
 
667
        return 1;
 
668
}
 
669
 
529
670
/*
530
671
 * Returns 1 if percent set, else 0 on failure.
531
672
 */
616
757
        return lv_mirror_percent(lv->vg->cmd, lv, 0, percent, NULL);
617
758
}
618
759
 
619
 
static int _lv_active(struct cmd_context *cmd, struct logical_volume *lv)
 
760
/*
 
761
 * Returns data or metadata percent usage, depends on metadata 0/1.
 
762
 * Returns 1 if percent set, else 0 on failure.
 
763
 */
 
764
int lv_thin_pool_percent(const struct logical_volume *lv, int metadata,
 
765
                         percent_t *percent)
 
766
{
 
767
        int r;
 
768
        struct dev_manager *dm;
 
769
 
 
770
        if (!activation())
 
771
                return 0;
 
772
 
 
773
        log_debug("Checking thin %sdata percent for LV %s/%s",
 
774
                  (metadata) ? "meta" : "", lv->vg->name, lv->name);
 
775
 
 
776
        if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
 
777
                return_0;
 
778
 
 
779
        if (!(r = dev_manager_thin_pool_percent(dm, lv, metadata, percent)))
 
780
                stack;
 
781
 
 
782
        dev_manager_destroy(dm);
 
783
 
 
784
        return r;
 
785
}
 
786
 
 
787
/*
 
788
 * Returns 1 if percent set, else 0 on failure.
 
789
 */
 
790
int lv_thin_percent(const struct logical_volume *lv,
 
791
                    int mapped, percent_t *percent)
 
792
{
 
793
        int r;
 
794
        struct dev_manager *dm;
 
795
 
 
796
        if (!activation())
 
797
                return 0;
 
798
 
 
799
        log_debug("Checking thin percent for LV %s/%s",
 
800
                  lv->vg->name, lv->name);
 
801
 
 
802
        if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
 
803
                return_0;
 
804
 
 
805
        if (!(r = dev_manager_thin_percent(dm, lv, mapped, percent)))
 
806
                stack;
 
807
 
 
808
        dev_manager_destroy(dm);
 
809
 
 
810
        return r;
 
811
}
 
812
 
 
813
/*
 
814
 * Returns 1 if transaction_id set, else 0 on failure.
 
815
 */
 
816
int lv_thin_pool_transaction_id(const struct logical_volume *lv,
 
817
                                uint64_t *transaction_id)
 
818
{
 
819
        int r;
 
820
        struct dev_manager *dm;
 
821
        struct dm_status_thin_pool *status;
 
822
 
 
823
        if (!activation())
 
824
                return 0;
 
825
 
 
826
        log_debug("Checking thin percent for LV %s/%s",
 
827
                  lv->vg->name, lv->name);
 
828
 
 
829
        if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, 1)))
 
830
                return_0;
 
831
 
 
832
        if (!(r = dev_manager_thin_pool_status(dm, lv, &status)))
 
833
                stack;
 
834
        else
 
835
                *transaction_id = status->transaction_id;
 
836
 
 
837
        dev_manager_destroy(dm);
 
838
 
 
839
        return r;
 
840
}
 
841
 
 
842
static int _lv_active(struct cmd_context *cmd, const struct logical_volume *lv)
620
843
{
621
844
        struct lvinfo info;
622
845
 
658
881
static int _lv_preload(struct logical_volume *lv, struct lv_activate_opts *laopts,
659
882
                       int *flush_required)
660
883
{
661
 
        int r;
 
884
        int r = 0;
662
885
        struct dev_manager *dm;
 
886
        int old_readonly = laopts->read_only;
 
887
 
 
888
        laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
663
889
 
664
890
        if (!(dm = dev_manager_create(lv->vg->cmd, lv->vg->name, (lv->status & PVMOVE) ? 0 : 1)))
665
 
                return_0;
 
891
                goto_out;
666
892
 
667
893
        if (!(r = dev_manager_preload(dm, lv, laopts, flush_required)))
668
894
                stack;
669
895
 
670
896
        dev_manager_destroy(dm);
 
897
 
 
898
        laopts->read_only = old_readonly;
 
899
out:
671
900
        return r;
672
901
}
673
902
 
692
921
        int r;
693
922
        struct dev_manager *dm;
694
923
 
 
924
        laopts->read_only = _passes_readonly_filter(lv->vg->cmd, lv);
 
925
 
695
926
        /*
696
927
         * When we are asked to manipulate (normally suspend/resume) the PVMOVE
697
928
         * device directly, we don't want to touch the devices that use it.
710
941
 * These two functions return the number of visible LVs in the state,
711
942
 * or -1 on error.  FIXME Check this.
712
943
 */
713
 
int lvs_in_vg_activated(struct volume_group *vg)
 
944
int lvs_in_vg_activated(const struct volume_group *vg)
714
945
{
715
946
        struct lv_list *lvl;
716
947
        int count = 0;
765
996
 *
766
997
 * Returns: 0 or 1
767
998
 */
768
 
static int _lv_is_active(struct logical_volume *lv,
 
999
static int _lv_is_active(const struct logical_volume *lv,
769
1000
                         int *locally, int *exclusive)
770
1001
{
771
1002
        int r, l, e; /* remote, local, and exclusive */
776
1007
                l = 1;
777
1008
 
778
1009
        if (!vg_is_clustered(lv->vg)) {
779
 
                e = 1;  /* exclusive by definition */
 
1010
                if (l)
 
1011
                        e = 1;  /* exclusive by definition */
780
1012
                goto out;
781
1013
        }
782
1014
 
796
1028
         * New users of this function who specifically ask for 'exclusive'
797
1029
         * will be given an error message.
798
1030
         */
799
 
        if (l) {
800
 
                if (exclusive)
801
 
                        log_error("Unable to determine exclusivity of %s",
802
 
                                  lv->name);
803
 
                goto out;
804
 
        }
805
 
 
806
 
        /* FIXME: Is this fallback alright? */
807
 
        if (activate_lv_excl(lv->vg->cmd, lv)) {
808
 
                if (!deactivate_lv(lv->vg->cmd, lv))
809
 
                        stack;
810
 
                /* FIXME: locally & exclusive are undefined. */
811
 
                return 0;
812
 
        }
813
 
        /* FIXME: Check exclusive value here. */
 
1031
        log_error("Unable to determine exclusivity of %s", lv->name);
 
1032
 
 
1033
        e = 0;
 
1034
 
 
1035
        /*
 
1036
         * We used to attempt activate_lv_excl_local(lv->vg->cmd, lv) here,
 
1037
         * but it's unreliable.
 
1038
         */
 
1039
 
814
1040
out:
815
1041
        if (locally)
816
1042
                *locally = l;
826
1052
        return r || l;
827
1053
}
828
1054
 
829
 
int lv_is_active(struct logical_volume *lv)
 
1055
int lv_is_active(const struct logical_volume *lv)
830
1056
{
831
1057
        return _lv_is_active(lv, NULL, NULL);
832
1058
}
833
1059
 
834
 
int lv_is_active_but_not_locally(struct logical_volume *lv)
 
1060
int lv_is_active_but_not_locally(const struct logical_volume *lv)
835
1061
{
836
1062
        int l;
837
1063
        return _lv_is_active(lv, &l, NULL) && !l;
838
1064
}
839
1065
 
840
 
int lv_is_active_exclusive_locally(struct logical_volume *lv)
 
1066
int lv_is_active_exclusive(const struct logical_volume *lv)
 
1067
{
 
1068
        int e;
 
1069
 
 
1070
        return _lv_is_active(lv, NULL, &e) && e;
 
1071
}
 
1072
 
 
1073
int lv_is_active_exclusive_locally(const struct logical_volume *lv)
841
1074
{
842
1075
        int l, e;
843
1076
 
844
1077
        return _lv_is_active(lv, &l, &e) && l && e;
845
1078
}
846
1079
 
847
 
int lv_is_active_exclusive_remotely(struct logical_volume *lv)
 
1080
int lv_is_active_exclusive_remotely(const struct logical_volume *lv)
848
1081
{
849
1082
        int l, e;
850
1083
 
893
1126
        return path;
894
1127
}
895
1128
 
 
1129
static char *_build_target_uuid(struct cmd_context *cmd, struct logical_volume *lv)
 
1130
{
 
1131
        const char *layer;
 
1132
 
 
1133
        if (lv_is_thin_pool(lv))
 
1134
                layer = "tpool"; /* Monitor "tpool" for the "thin pool". */
 
1135
        else if (lv_is_origin(lv))
 
1136
                layer = "real"; /* Monitor "real" for "snapshot-origin". */
 
1137
        else
 
1138
                layer = NULL;
 
1139
 
 
1140
        return build_dm_uuid(cmd->mem, lv->lvid.s, layer);
 
1141
}
 
1142
 
896
1143
int target_registered_with_dmeventd(struct cmd_context *cmd, const char *dso,
897
1144
                                    struct logical_volume *lv, int *pending)
898
1145
{
899
1146
        char *uuid;
900
1147
        enum dm_event_mask evmask = 0;
901
1148
        struct dm_event_handler *dmevh;
902
 
 
903
1149
        *pending = 0;
904
1150
 
905
1151
        if (!dso)
906
1152
                return_0;
907
1153
 
908
 
        /* We always monitor the "real" device, never the "snapshot-origin" itself. */
909
 
        if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
 
1154
        if (!(uuid = _build_target_uuid(cmd, lv)))
910
1155
                return_0;
911
1156
 
912
1157
        if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, 0, DM_EVENT_ALL_ERRORS)))
939
1184
                return_0;
940
1185
 
941
1186
        /* We always monitor the "real" device, never the "snapshot-origin" itself. */
942
 
        if (!(uuid = build_dm_uuid(cmd->mem, lv->lvid.s, lv_is_origin(lv) ? "real" : NULL)))
 
1187
        if (!(uuid = _build_target_uuid(cmd, lv)))
943
1188
                return_0;
944
1189
 
945
1190
        if (!(dmevh = _create_dm_event_handler(cmd, uuid, dso, timeout,
1147
1392
                goto_out;
1148
1393
 
1149
1394
        /* Ignore origin_only unless LV is origin in both old and new metadata */
1150
 
        if (!lv_is_origin(lv) || !lv_is_origin(lv_pre))
 
1395
        if (!lv_is_thin_volume(lv) && !(lv_is_origin(lv) && lv_is_origin(lv_pre)))
1151
1396
                laopts->origin_only = 0;
1152
1397
 
1153
1398
        if (test_mode()) {
1281
1526
        return r;
1282
1527
}
1283
1528
 
1284
 
/* Returns success if the device is not active */
1285
 
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only)
 
1529
/*
 
1530
 * In a cluster, set exclusive to indicate that only one node is using the
 
1531
 * device.  Any preloaded tables may then use non-clustered targets.
 
1532
 *
 
1533
 * Returns success if the device is not active
 
1534
 */
 
1535
int lv_suspend_if_active(struct cmd_context *cmd, const char *lvid_s, unsigned origin_only, unsigned exclusive)
1286
1536
{
1287
 
        struct lv_activate_opts laopts = { .origin_only = origin_only };
 
1537
        struct lv_activate_opts laopts = {
 
1538
                .origin_only = origin_only,
 
1539
                .exclusive = exclusive
 
1540
        };
1288
1541
 
1289
1542
        return _lv_suspend(cmd, lvid_s, &laopts, 0);
1290
1543
}
1297
1550
}
1298
1551
***********/
1299
1552
 
1300
 
 /*
1301
 
  * _lv_resume
1302
 
  * @cmd
1303
 
  * @lvid_s
1304
 
  * @origin_only
1305
 
  * @exclusive:  This parameter only has an affect in cluster-context.
1306
 
  *              It forces local target type to be used (instead of
1307
 
  *              cluster-aware type).
1308
 
  * @error_if_not_active
1309
 
  */
1310
1553
static int _lv_resume(struct cmd_context *cmd, const char *lvid_s,
1311
1554
                      struct lv_activate_opts *laopts, int error_if_not_active)
1312
1555
{
1313
1556
        struct logical_volume *lv;
1314
1557
        struct lvinfo info;
1315
1558
        int r = 0;
 
1559
        int messages_only = 0;
1316
1560
 
1317
1561
        if (!activation())
1318
1562
                return 1;
1320
1564
        if (!(lv = lv_from_lvid(cmd, lvid_s, 0)))
1321
1565
                goto_out;
1322
1566
 
 
1567
        if (lv_is_thin_pool(lv) && laopts->origin_only)
 
1568
                messages_only = 1;
 
1569
 
1323
1570
        if (!lv_is_origin(lv))
1324
1571
                laopts->origin_only = 0;
1325
1572
 
1326
1573
        if (test_mode()) {
1327
 
                _skip("Resuming %s%s.", lv->name, laopts->origin_only ? " without snapshots" : "");
 
1574
                _skip("Resuming %s%s%s.", lv->name, laopts->origin_only ? " without snapshots" : "",
 
1575
                      laopts->revert ? " (reverting)" : "");
1328
1576
                r = 1;
1329
1577
                goto out;
1330
1578
        }
1331
1579
 
1332
 
        log_debug("Resuming LV %s/%s%s%s.", lv->vg->name, lv->name,
 
1580
        log_debug("Resuming LV %s/%s%s%s%s.", lv->vg->name, lv->name,
1333
1581
                  error_if_not_active ? "" : " if active",
1334
 
                  laopts->origin_only ? " without snapshots" : "");
 
1582
                  laopts->origin_only ? " without snapshots" : "",
 
1583
                  laopts->revert ? " (reverting)" : "");
1335
1584
 
1336
1585
        if (!lv_info(cmd, lv, laopts->origin_only, &info, 0, 0))
1337
1586
                goto_out;
1338
1587
 
1339
 
        if (!info.exists || !info.suspended) {
 
1588
        if (!info.exists || !(info.suspended || messages_only)) {
1340
1589
                if (error_if_not_active)
1341
1590
                        goto_out;
1342
1591
                r = 1;
1345
1594
                goto out;
1346
1595
        }
1347
1596
 
 
1597
        laopts->read_only = _passes_readonly_filter(cmd, lv);
 
1598
 
1348
1599
        if (!_lv_activate_lv(lv, laopts))
1349
1600
                goto_out;
1350
1601
 
1361
1612
        return r;
1362
1613
}
1363
1614
 
1364
 
/* Returns success if the device is not active */
 
1615
/*
 
1616
 * In a cluster, set exclusive to indicate that only one node is using the
 
1617
 * device.  Any tables loaded may then use non-clustered targets.
 
1618
 *
 
1619
 * @origin_only
 
1620
 * @exclusive   This parameter only has an affect in cluster-context.
 
1621
 *              It forces local target type to be used (instead of
 
1622
 *              cluster-aware type).
 
1623
 * Returns success if the device is not active
 
1624
 */
1365
1625
int lv_resume_if_active(struct cmd_context *cmd, const char *lvid_s,
1366
 
                        unsigned origin_only, unsigned exclusive)
 
1626
                        unsigned origin_only, unsigned exclusive,
 
1627
                        unsigned revert)
1367
1628
{
1368
1629
        struct lv_activate_opts laopts = {
1369
1630
                .origin_only = origin_only,
1370
 
                /*
1371
 
                 * When targets are activated exclusively in a cluster, the
1372
 
                 * non-clustered target should be used.  This only happens
1373
 
                 * if exclusive is set.
1374
 
                 */
1375
 
                .exclusive = exclusive
 
1631
                .exclusive = exclusive,
 
1632
                .revert = revert
1376
1633
        };
1377
1634
 
1378
1635
        return _lv_resume(cmd, lvid_s, &laopts, 0);
1437
1694
        }
1438
1695
 
1439
1696
        if (lv_is_visible(lv)) {
1440
 
                if (info.open_count) {
1441
 
                        log_error("LV %s/%s in use: not deactivating",
1442
 
                                  lv->vg->name, lv->name);
1443
 
                        goto out;
1444
 
                }
 
1697
                if (!lv_check_not_in_use(cmd, lv, &info))
 
1698
                        goto_out;
 
1699
 
1445
1700
                if (lv_is_origin(lv) && _lv_has_open_snapshots(lv))
1446
1701
                        goto_out;
1447
1702
        }
1535
1790
                goto out;
1536
1791
        }
1537
1792
 
1538
 
        log_debug("Activating %s/%s%s.", lv->vg->name, lv->name,
1539
 
                  laopts->exclusive ? " exclusively" : "");
 
1793
        if (filter)
 
1794
                laopts->read_only = _passes_readonly_filter(cmd, lv);
 
1795
 
 
1796
        log_debug("Activating %s/%s%s%s.", lv->vg->name, lv->name,
 
1797
                  laopts->exclusive ? " exclusively" : "",
 
1798
                  laopts->read_only ? " read-only" : "");
1540
1799
 
1541
1800
        if (!lv_info(cmd, lv, 0, &info, 0, 0))
1542
1801
                goto_out;
1543
1802
 
1544
 
        if (info.exists && !info.suspended && info.live_table) {
 
1803
        /*
 
1804
         * Nothing to do?
 
1805
         */
 
1806
        if (info.exists && !info.suspended && info.live_table &&
 
1807
            (info.read_only == read_only_lv(lv, laopts))) {
1545
1808
                r = 1;
1546
1809
                goto out;
1547
1810
        }
1617
1880
int pv_uses_vg(struct physical_volume *pv,
1618
1881
               struct volume_group *vg)
1619
1882
{
1620
 
        if (!activation())
 
1883
        if (!activation() || !pv->dev)
1621
1884
                return 0;
1622
1885
 
1623
1886
        if (!dm_is_dm_major(MAJOR(pv->dev->dev)))