Home | History | Annotate | Line # | Download | only in metadata
mirror.c revision 1.1
      1  1.1  haad /*	$NetBSD: mirror.c,v 1.1 2008/12/22 00:18:09 haad Exp $	*/
      2  1.1  haad 
      3  1.1  haad /*
      4  1.1  haad  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
      5  1.1  haad  * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
      6  1.1  haad  *
      7  1.1  haad  * This file is part of LVM2.
      8  1.1  haad  *
      9  1.1  haad  * This copyrighted material is made available to anyone wishing to use,
     10  1.1  haad  * modify, copy, or redistribute it subject to the terms and conditions
     11  1.1  haad  * of the GNU Lesser General Public License v.2.1.
     12  1.1  haad  *
     13  1.1  haad  * You should have received a copy of the GNU Lesser General Public License
     14  1.1  haad  * along with this program; if not, write to the Free Software Foundation,
     15  1.1  haad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     16  1.1  haad  */
     17  1.1  haad 
     18  1.1  haad #include "lib.h"
     19  1.1  haad #include "metadata.h"
     20  1.1  haad #include "toolcontext.h"
     21  1.1  haad #include "segtype.h"
     22  1.1  haad #include "display.h"
     23  1.1  haad #include "archiver.h"
     24  1.1  haad #include "activate.h"
     25  1.1  haad #include "lv_alloc.h"
     26  1.1  haad #include "lvm-string.h"
     27  1.1  haad #include "str_list.h"
     28  1.1  haad #include "locking.h"	/* FIXME Should not be used in this file */
     29  1.1  haad 
     30  1.1  haad #include "defaults.h" /* FIXME: should this be defaults.h? */
     31  1.1  haad 
     32  1.1  haad /* These are necessary for _write_log_header() */
     33  1.1  haad #include "xlate.h"
     34  1.1  haad #define MIRROR_MAGIC 0x4D695272
     35  1.1  haad #define MIRROR_DISK_VERSION 2
     36  1.1  haad 
     37  1.1  haad /* These are the flags that represent the mirror failure restoration policies */
     38  1.1  haad #define MIRROR_REMOVE		 0
     39  1.1  haad #define MIRROR_ALLOCATE		 1
     40  1.1  haad #define MIRROR_ALLOCATE_ANYWHERE 2
     41  1.1  haad 
     42  1.1  haad /*
     43  1.1  haad  * Returns true if the lv is temporary mirror layer for resync
     44  1.1  haad  */
     45  1.1  haad int is_temporary_mirror_layer(const struct logical_volume *lv)
     46  1.1  haad {
     47  1.1  haad 	if (lv->status & MIRROR_IMAGE
     48  1.1  haad 	    && lv->status & MIRRORED
     49  1.1  haad 	    && !(lv->status & LOCKED))
     50  1.1  haad 		return 1;
     51  1.1  haad 
     52  1.1  haad 	return 0;
     53  1.1  haad }
     54  1.1  haad 
     55  1.1  haad /*
     56  1.1  haad  * Return a temporary LV for resyncing added mirror image.
     57  1.1  haad  * Add other mirror legs to lvs list.
     58  1.1  haad  */
     59  1.1  haad struct logical_volume *find_temporary_mirror(const struct logical_volume *lv)
     60  1.1  haad {
     61  1.1  haad 	struct lv_segment *seg;
     62  1.1  haad 
     63  1.1  haad 	if (!(lv->status & MIRRORED))
     64  1.1  haad 		return NULL;
     65  1.1  haad 
     66  1.1  haad 	seg = first_seg(lv);
     67  1.1  haad 
     68  1.1  haad 	/* Temporary mirror is always area_num == 0 */
     69  1.1  haad 	if (seg_type(seg, 0) == AREA_LV &&
     70  1.1  haad 	    is_temporary_mirror_layer(seg_lv(seg, 0)))
     71  1.1  haad 		return seg_lv(seg, 0);
     72  1.1  haad 
     73  1.1  haad 	return NULL;
     74  1.1  haad }
     75  1.1  haad 
     76  1.1  haad /*
     77  1.1  haad  * Returns the number of mirrors of the LV
     78  1.1  haad  */
     79  1.1  haad uint32_t lv_mirror_count(const struct logical_volume *lv)
     80  1.1  haad {
     81  1.1  haad 	struct lv_segment *seg;
     82  1.1  haad 	uint32_t s, mirrors;
     83  1.1  haad 
     84  1.1  haad 	if (!(lv->status & MIRRORED))
     85  1.1  haad 		return 1;
     86  1.1  haad 
     87  1.1  haad 	seg = first_seg(lv);
     88  1.1  haad 	mirrors = seg->area_count;
     89  1.1  haad 
     90  1.1  haad 	for (s = 0; s < seg->area_count; s++) {
     91  1.1  haad 		if (seg_type(seg, s) != AREA_LV)
     92  1.1  haad 			continue;
     93  1.1  haad 		if (is_temporary_mirror_layer(seg_lv(seg, s)))
     94  1.1  haad 			mirrors += lv_mirror_count(seg_lv(seg, s)) - 1;
     95  1.1  haad 	}
     96  1.1  haad 
     97  1.1  haad 	return mirrors;
     98  1.1  haad }
     99  1.1  haad 
    100  1.1  haad struct lv_segment *find_mirror_seg(struct lv_segment *seg)
    101  1.1  haad {
    102  1.1  haad 	struct lv_segment *mirror_seg;
    103  1.1  haad 
    104  1.1  haad 	mirror_seg = get_only_segment_using_this_lv(seg->lv);
    105  1.1  haad 
    106  1.1  haad 	if (!mirror_seg) {
    107  1.1  haad 		log_error("Failed to find mirror_seg for %s", seg->lv->name);
    108  1.1  haad 		return NULL;
    109  1.1  haad 	}
    110  1.1  haad 
    111  1.1  haad 	if (!seg_is_mirrored(mirror_seg)) {
    112  1.1  haad 		log_error("%s on %s is not a mirror segments",
    113  1.1  haad 			  mirror_seg->lv->name, seg->lv->name);
    114  1.1  haad 		return NULL;
    115  1.1  haad 	}
    116  1.1  haad 
    117  1.1  haad 	return mirror_seg;
    118  1.1  haad }
    119  1.1  haad 
    120  1.1  haad /*
    121  1.1  haad  * Reduce the region size if necessary to ensure
    122  1.1  haad  * the volume size is a multiple of the region size.
    123  1.1  haad  */
    124  1.1  haad uint32_t adjusted_mirror_region_size(uint32_t extent_size, uint32_t extents,
    125  1.1  haad 				     uint32_t region_size)
    126  1.1  haad {
    127  1.1  haad 	uint64_t region_max;
    128  1.1  haad 
    129  1.1  haad 	region_max = (1 << (ffs((int)extents) - 1)) * (uint64_t) extent_size;
    130  1.1  haad 
    131  1.1  haad 	if (region_max < UINT32_MAX && region_size > region_max) {
    132  1.1  haad 		region_size = (uint32_t) region_max;
    133  1.1  haad 		log_print("Using reduced mirror region size of %" PRIu32
    134  1.1  haad 			  " sectors", region_size);
    135  1.1  haad 	}
    136  1.1  haad 
    137  1.1  haad 	return region_size;
    138  1.1  haad }
    139  1.1  haad 
    140  1.1  haad /*
    141  1.1  haad  * shift_mirror_images
    142  1.1  haad  * @mirrored_seg
    143  1.1  haad  * @mimage:  The position (index) of the image to move to the end
    144  1.1  haad  *
    145  1.1  haad  * When dealing with removal of legs, we often move a 'removable leg'
    146  1.1  haad  * to the back of the 'areas' array.  It is critically important not
    147  1.1  haad  * to simply swap it for the last area in the array.  This would have
    148  1.1  haad  * the affect of reordering the remaining legs - altering position of
    149  1.1  haad  * the primary.  So, we must shuffle all of the areas in the array
    150  1.1  haad  * to maintain their relative position before moving the 'removable
    151  1.1  haad  * leg' to the end.
    152  1.1  haad  *
    153  1.1  haad  * Short illustration of the problem:
    154  1.1  haad  *   - Mirror consists of legs A, B, C and we want to remove A
    155  1.1  haad  *   - We swap A and C and then remove A, leaving C, B
    156  1.1  haad  * This scenario is problematic in failure cases where A dies, because
    157  1.1  haad  * B becomes the primary.  If the above happens, we effectively throw
    158  1.1  haad  * away any changes made between the time of failure and the time of
    159  1.1  haad  * restructuring the mirror.
    160  1.1  haad  *
    161  1.1  haad  * So, any time we want to move areas to the end to be removed, use
    162  1.1  haad  * this function.
    163  1.1  haad  */
    164  1.1  haad int shift_mirror_images(struct lv_segment *mirrored_seg, unsigned mimage)
    165  1.1  haad {
    166  1.1  haad 	int i;
    167  1.1  haad 	struct lv_segment_area area;
    168  1.1  haad 
    169  1.1  haad 	if (mimage >= mirrored_seg->area_count) {
    170  1.1  haad 		log_error("Invalid index (%u) of mirror image supplied "
    171  1.1  haad 			  "to shift_mirror_images()", mimage);
    172  1.1  haad 		return 0;
    173  1.1  haad 	}
    174  1.1  haad 
    175  1.1  haad 	area = mirrored_seg->areas[mimage];
    176  1.1  haad 
    177  1.1  haad 	/* Shift remaining images down to fill the hole */
    178  1.1  haad 	for (i = mimage + 1; i < mirrored_seg->area_count; i++)
    179  1.1  haad 		mirrored_seg->areas[i-1] = mirrored_seg->areas[i];
    180  1.1  haad 
    181  1.1  haad 	/* Place this one at the end */
    182  1.1  haad 	mirrored_seg->areas[i-1] = area;
    183  1.1  haad 
    184  1.1  haad 	return 1;
    185  1.1  haad }
    186  1.1  haad 
    187  1.1  haad /*
    188  1.1  haad  * This function writes a new header to the mirror log header to the lv
    189  1.1  haad  *
    190  1.1  haad  * Returns: 1 on success, 0 on failure
    191  1.1  haad  */
    192  1.1  haad static int _write_log_header(struct cmd_context *cmd, struct logical_volume *lv)
    193  1.1  haad {
    194  1.1  haad 	struct device *dev;
    195  1.1  haad 	char *name;
    196  1.1  haad 	struct { /* The mirror log header */
    197  1.1  haad 		uint32_t magic;
    198  1.1  haad 		uint32_t version;
    199  1.1  haad 		uint64_t nr_regions;
    200  1.1  haad 	} log_header;
    201  1.1  haad 
    202  1.1  haad 	log_header.magic = xlate32(MIRROR_MAGIC);
    203  1.1  haad 	log_header.version = xlate32(MIRROR_DISK_VERSION);
    204  1.1  haad 	log_header.nr_regions = xlate64((uint64_t)-1);
    205  1.1  haad 
    206  1.1  haad 	if (!(name = dm_pool_alloc(cmd->mem, PATH_MAX))) {
    207  1.1  haad 		log_error("Name allocation failed - log header not written (%s)",
    208  1.1  haad 			lv->name);
    209  1.1  haad 		return 0;
    210  1.1  haad 	}
    211  1.1  haad 
    212  1.1  haad 	if (dm_snprintf(name, PATH_MAX, "%s%s/%s", cmd->dev_dir,
    213  1.1  haad 			 lv->vg->name, lv->name) < 0) {
    214  1.1  haad 		log_error("Name too long - log header not written (%s)", lv->name);
    215  1.1  haad 		return 0;
    216  1.1  haad 	}
    217  1.1  haad 
    218  1.1  haad 	log_verbose("Writing log header to device, %s", lv->name);
    219  1.1  haad 
    220  1.1  haad 	if (!(dev = dev_cache_get(name, NULL))) {
    221  1.1  haad 		log_error("%s: not found: log header not written", name);
    222  1.1  haad 		return 0;
    223  1.1  haad 	}
    224  1.1  haad 
    225  1.1  haad 	if (!dev_open_quiet(dev))
    226  1.1  haad 		return 0;
    227  1.1  haad 
    228  1.1  haad 	if (!dev_write(dev, UINT64_C(0), sizeof(log_header), &log_header)) {
    229  1.1  haad 		log_error("Failed to write log header to %s", name);
    230  1.1  haad 		dev_close_immediate(dev);
    231  1.1  haad 		return 0;
    232  1.1  haad 	}
    233  1.1  haad 
    234  1.1  haad 	dev_close_immediate(dev);
    235  1.1  haad 
    236  1.1  haad 	return 1;
    237  1.1  haad }
    238  1.1  haad 
    239  1.1  haad /*
    240  1.1  haad  * Initialize mirror log contents
    241  1.1  haad  */
    242  1.1  haad static int _init_mirror_log(struct cmd_context *cmd,
    243  1.1  haad 			    struct logical_volume *log_lv, int in_sync,
    244  1.1  haad 			    struct dm_list *tags, int remove_on_failure)
    245  1.1  haad {
    246  1.1  haad 	struct str_list *sl;
    247  1.1  haad 	struct lvinfo info;
    248  1.1  haad 	uint32_t orig_status = log_lv->status;
    249  1.1  haad 	int was_active = 0;
    250  1.1  haad 
    251  1.1  haad 	if (!activation() && in_sync) {
    252  1.1  haad 		log_error("Aborting. Unable to create in-sync mirror log "
    253  1.1  haad 			  "while activation is disabled.");
    254  1.1  haad 		return 0;
    255  1.1  haad 	}
    256  1.1  haad 
    257  1.1  haad 	/* If the LV is active, deactivate it first. */
    258  1.1  haad 	if (lv_info(cmd, log_lv, &info, 0, 0) && info.exists) {
    259  1.1  haad 		if (!deactivate_lv(cmd, log_lv))
    260  1.1  haad 			return_0;
    261  1.1  haad 		was_active = 1;
    262  1.1  haad 	}
    263  1.1  haad 
    264  1.1  haad 	/* Temporary make it visible for set_lv() */
    265  1.1  haad 	log_lv->status |= VISIBLE_LV;
    266  1.1  haad 
    267  1.1  haad 	/* Temporary tag mirror log for activation */
    268  1.1  haad 	dm_list_iterate_items(sl, tags)
    269  1.1  haad 		if (!str_list_add(cmd->mem, &log_lv->tags, sl->str)) {
    270  1.1  haad 			log_error("Aborting. Unable to tag mirror log.");
    271  1.1  haad 			goto activate_lv;
    272  1.1  haad 		}
    273  1.1  haad 
    274  1.1  haad 	/* store mirror log on disk(s) */
    275  1.1  haad 	if (!vg_write(log_lv->vg))
    276  1.1  haad 		goto activate_lv;
    277  1.1  haad 
    278  1.1  haad 	backup(log_lv->vg);
    279  1.1  haad 
    280  1.1  haad 	if (!vg_commit(log_lv->vg))
    281  1.1  haad 		goto activate_lv;
    282  1.1  haad 
    283  1.1  haad 	if (!activate_lv(cmd, log_lv)) {
    284  1.1  haad 		log_error("Aborting. Failed to activate mirror log.");
    285  1.1  haad 		goto revert_new_lv;
    286  1.1  haad 	}
    287  1.1  haad 
    288  1.1  haad 	/* Remove the temporary tags */
    289  1.1  haad 	dm_list_iterate_items(sl, tags)
    290  1.1  haad 		if (!str_list_del(&log_lv->tags, sl->str))
    291  1.1  haad 			log_error("Failed to remove tag %s from mirror log.",
    292  1.1  haad 				  sl->str);
    293  1.1  haad 
    294  1.1  haad 	if (activation() && !set_lv(cmd, log_lv, log_lv->size,
    295  1.1  haad 				    in_sync ? -1 : 0)) {
    296  1.1  haad 		log_error("Aborting. Failed to wipe mirror log.");
    297  1.1  haad 		goto deactivate_and_revert_new_lv;
    298  1.1  haad 	}
    299  1.1  haad 
    300  1.1  haad 	if (activation() && !_write_log_header(cmd, log_lv)) {
    301  1.1  haad 		log_error("Aborting. Failed to write mirror log header.");
    302  1.1  haad 		goto deactivate_and_revert_new_lv;
    303  1.1  haad 	}
    304  1.1  haad 
    305  1.1  haad 	if (!deactivate_lv(cmd, log_lv)) {
    306  1.1  haad 		log_error("Aborting. Failed to deactivate mirror log. "
    307  1.1  haad 			  "Manual intervention required.");
    308  1.1  haad 		return 0;
    309  1.1  haad 	}
    310  1.1  haad 
    311  1.1  haad 	log_lv->status &= ~VISIBLE_LV;
    312  1.1  haad 
    313  1.1  haad 	if (was_active && !activate_lv(cmd, log_lv))
    314  1.1  haad 		return_0;
    315  1.1  haad 
    316  1.1  haad 	return 1;
    317  1.1  haad 
    318  1.1  haad deactivate_and_revert_new_lv:
    319  1.1  haad 	if (!deactivate_lv(cmd, log_lv)) {
    320  1.1  haad 		log_error("Unable to deactivate mirror log LV. "
    321  1.1  haad 			  "Manual intervention required.");
    322  1.1  haad 		return 0;
    323  1.1  haad 	}
    324  1.1  haad 
    325  1.1  haad revert_new_lv:
    326  1.1  haad 	log_lv->status = orig_status;
    327  1.1  haad 
    328  1.1  haad 	dm_list_iterate_items(sl, tags)
    329  1.1  haad 		if (!str_list_del(&log_lv->tags, sl->str))
    330  1.1  haad 			log_error("Failed to remove tag %s from mirror log.",
    331  1.1  haad 				  sl->str);
    332  1.1  haad 
    333  1.1  haad 	if (remove_on_failure && !lv_remove(log_lv)) {
    334  1.1  haad 		log_error("Manual intervention may be required to remove "
    335  1.1  haad 			  "abandoned log LV before retrying.");
    336  1.1  haad 		return 0;
    337  1.1  haad 	}
    338  1.1  haad 
    339  1.1  haad 	if (!vg_write(log_lv->vg) ||
    340  1.1  haad 	    (backup(log_lv->vg), !vg_commit(log_lv->vg)))
    341  1.1  haad 		log_error("Manual intervention may be required to "
    342  1.1  haad 			  "remove/restore abandoned log LV before retrying.");
    343  1.1  haad activate_lv:
    344  1.1  haad 	if (was_active && !remove_on_failure && !activate_lv(cmd, log_lv))
    345  1.1  haad 		return_0;
    346  1.1  haad 
    347  1.1  haad 	return 0;
    348  1.1  haad }
    349  1.1  haad 
    350  1.1  haad /*
    351  1.1  haad  * Delete independent/orphan LV, it must acquire lock.
    352  1.1  haad  */
    353  1.1  haad static int _delete_lv(struct logical_volume *mirror_lv, struct logical_volume *lv)
    354  1.1  haad {
    355  1.1  haad 	struct cmd_context *cmd = mirror_lv->vg->cmd;
    356  1.1  haad 	struct str_list *sl;
    357  1.1  haad 
    358  1.1  haad 	/* Inherit tags - maybe needed for activation */
    359  1.1  haad 	if (!str_list_match_list(&mirror_lv->tags, &lv->tags)) {
    360  1.1  haad 		dm_list_iterate_items(sl, &mirror_lv->tags)
    361  1.1  haad 			if (!str_list_add(cmd->mem, &lv->tags, sl->str)) {
    362  1.1  haad 				log_error("Aborting. Unable to tag.");
    363  1.1  haad 				return 0;
    364  1.1  haad 			}
    365  1.1  haad 
    366  1.1  haad 		if (!vg_write(mirror_lv->vg) ||
    367  1.1  haad 		    !vg_commit(mirror_lv->vg)) {
    368  1.1  haad 			log_error("Intermediate VG commit for orphan volume failed.");
    369  1.1  haad 			return 0;
    370  1.1  haad 		}
    371  1.1  haad 	}
    372  1.1  haad 
    373  1.1  haad 	if (!activate_lv(cmd, lv))
    374  1.1  haad 		return_0;
    375  1.1  haad 
    376  1.1  haad 	if (!deactivate_lv(cmd, lv))
    377  1.1  haad 		return_0;
    378  1.1  haad 
    379  1.1  haad 	if (!lv_remove(lv))
    380  1.1  haad 		return_0;
    381  1.1  haad 
    382  1.1  haad 	return 1;
    383  1.1  haad }
    384  1.1  haad 
    385  1.1  haad static int _merge_mirror_images(struct logical_volume *lv,
    386  1.1  haad 				const struct dm_list *mimages)
    387  1.1  haad {
    388  1.1  haad 	uint32_t addition = dm_list_size(mimages);
    389  1.1  haad 	struct logical_volume **img_lvs;
    390  1.1  haad 	struct lv_list *lvl;
    391  1.1  haad 	int i = 0;
    392  1.1  haad 
    393  1.1  haad 	if (!addition)
    394  1.1  haad 		return 1;
    395  1.1  haad 
    396  1.1  haad 	if (!(img_lvs = alloca(sizeof(*img_lvs) * addition)))
    397  1.1  haad 		return_0;
    398  1.1  haad 
    399  1.1  haad 	dm_list_iterate_items(lvl, mimages)
    400  1.1  haad 		img_lvs[i++] = lvl->lv;
    401  1.1  haad 
    402  1.1  haad 	return lv_add_mirror_lvs(lv, img_lvs, addition,
    403  1.1  haad 				 MIRROR_IMAGE, first_seg(lv)->region_size);
    404  1.1  haad }
    405  1.1  haad 
    406  1.1  haad /* Unlink the relationship between the segment and its log_lv */
    407  1.1  haad struct logical_volume *detach_mirror_log(struct lv_segment *mirrored_seg)
    408  1.1  haad {
    409  1.1  haad 	struct logical_volume *log_lv;
    410  1.1  haad 
    411  1.1  haad 	if (!mirrored_seg->log_lv)
    412  1.1  haad 		return NULL;
    413  1.1  haad 
    414  1.1  haad 	log_lv = mirrored_seg->log_lv;
    415  1.1  haad 	mirrored_seg->log_lv = NULL;
    416  1.1  haad 	log_lv->status |= VISIBLE_LV;
    417  1.1  haad 	log_lv->status &= ~MIRROR_LOG;
    418  1.1  haad 	remove_seg_from_segs_using_this_lv(log_lv, mirrored_seg);
    419  1.1  haad 
    420  1.1  haad 	return log_lv;
    421  1.1  haad }
    422  1.1  haad 
    423  1.1  haad /* Check if mirror image LV is removable with regard to given removable_pvs */
    424  1.1  haad static int _is_mirror_image_removable(struct logical_volume *mimage_lv,
    425  1.1  haad 				      struct dm_list *removable_pvs)
    426  1.1  haad {
    427  1.1  haad 	struct physical_volume *pv;
    428  1.1  haad 	struct lv_segment *seg;
    429  1.1  haad 	int pv_found;
    430  1.1  haad 	struct pv_list *pvl;
    431  1.1  haad 	uint32_t s;
    432  1.1  haad 
    433  1.1  haad 	dm_list_iterate_items(seg, &mimage_lv->segments) {
    434  1.1  haad 		for (s = 0; s < seg->area_count; s++) {
    435  1.1  haad 			if (seg_type(seg, s) != AREA_PV) {
    436  1.1  haad 				/* FIXME Recurse for AREA_LV? */
    437  1.1  haad 				/* Structure of seg_lv is unknown.
    438  1.1  haad 				 * Not removing this LV for safety. */
    439  1.1  haad 				return 0;
    440  1.1  haad 			}
    441  1.1  haad 
    442  1.1  haad 			pv = seg_pv(seg, s);
    443  1.1  haad 
    444  1.1  haad 			pv_found = 0;
    445  1.1  haad 			dm_list_iterate_items(pvl, removable_pvs) {
    446  1.1  haad 				if (pv->dev->dev == pvl->pv->dev->dev) {
    447  1.1  haad 					pv_found = 1;
    448  1.1  haad 					break;
    449  1.1  haad 				}
    450  1.1  haad 			}
    451  1.1  haad 			if (!pv_found)
    452  1.1  haad 				return 0;
    453  1.1  haad 		}
    454  1.1  haad 	}
    455  1.1  haad 
    456  1.1  haad 	return 1;
    457  1.1  haad }
    458  1.1  haad 
    459  1.1  haad /*
    460  1.1  haad  * Remove num_removed images from mirrored_seg
    461  1.1  haad  *
    462  1.1  haad  * Arguments:
    463  1.1  haad  *   num_removed:   the requested (maximum) number of mirrors to be removed
    464  1.1  haad  *   removable_pvs: if not NULL, only mirrors using PVs in this list
    465  1.1  haad  *                  will be removed
    466  1.1  haad  *   remove_log:    if non-zero, log_lv will be removed
    467  1.1  haad  *                  (even if it's 0, log_lv will be removed if there is no
    468  1.1  haad  *                   mirror remaining after the removal)
    469  1.1  haad  *   collapse:      if non-zero, instead of removing, remove the temporary
    470  1.1  haad  *                  mirror layer and merge mirrors to the original LV.
    471  1.1  haad  *                  removable_pvs should be NULL and num_removed should be
    472  1.1  haad  *                  seg->area_count - 1.
    473  1.1  haad  *   removed:       if non NULL, the number of removed mirror images is set
    474  1.1  haad  *                  as a result
    475  1.1  haad  *
    476  1.1  haad  * If collapse is non-zero, <removed> is guaranteed to be equal to num_removed.
    477  1.1  haad  *
    478  1.1  haad  * Return values:
    479  1.1  haad  *   Failure (0) means something unexpected has happend and
    480  1.1  haad  *   the caller should abort.
    481  1.1  haad  *   Even if no mirror was removed (e.g. no LV matches to 'removable_pvs'),
    482  1.1  haad  *   returns success (1).
    483  1.1  haad  */
    484  1.1  haad static int _remove_mirror_images(struct logical_volume *lv,
    485  1.1  haad 				 uint32_t num_removed,
    486  1.1  haad 				 struct dm_list *removable_pvs,
    487  1.1  haad 				 unsigned remove_log, unsigned collapse,
    488  1.1  haad 				 uint32_t *removed)
    489  1.1  haad {
    490  1.1  haad 	uint32_t m;
    491  1.1  haad 	uint32_t s;
    492  1.1  haad 	struct logical_volume *sub_lv;
    493  1.1  haad 	struct logical_volume *detached_log_lv = NULL;
    494  1.1  haad 	struct logical_volume *lv1 = NULL;
    495  1.1  haad 	struct lv_segment *mirrored_seg = first_seg(lv);
    496  1.1  haad 	uint32_t old_area_count = mirrored_seg->area_count;
    497  1.1  haad 	uint32_t new_area_count = mirrored_seg->area_count;
    498  1.1  haad 	struct lv_list *lvl;
    499  1.1  haad 	struct dm_list tmp_orphan_lvs;
    500  1.1  haad 
    501  1.1  haad 	if (removed)
    502  1.1  haad 		*removed = 0;
    503  1.1  haad 
    504  1.1  haad 	log_very_verbose("Reducing mirror set from %" PRIu32 " to %"
    505  1.1  haad 			 PRIu32 " image(s)%s.",
    506  1.1  haad 			 old_area_count, old_area_count - num_removed,
    507  1.1  haad 			 remove_log ? " and no log volume" : "");
    508  1.1  haad 
    509  1.1  haad 	if (collapse &&
    510  1.1  haad 	    (removable_pvs || (old_area_count - num_removed != 1))) {
    511  1.1  haad 		log_error("Incompatible parameters to _remove_mirror_images");
    512  1.1  haad 		return 0;
    513  1.1  haad 	}
    514  1.1  haad 
    515  1.1  haad 	/* Move removable_pvs to end of array */
    516  1.1  haad 	if (removable_pvs) {
    517  1.1  haad 		for (s = 0; s < mirrored_seg->area_count &&
    518  1.1  haad 			    old_area_count - new_area_count < num_removed; s++) {
    519  1.1  haad 			sub_lv = seg_lv(mirrored_seg, s);
    520  1.1  haad 
    521  1.1  haad 			if (!is_temporary_mirror_layer(sub_lv) &&
    522  1.1  haad 			    _is_mirror_image_removable(sub_lv, removable_pvs)) {
    523  1.1  haad 				if (!shift_mirror_images(mirrored_seg, s))
    524  1.1  haad 					return_0;
    525  1.1  haad 				new_area_count--;
    526  1.1  haad 			}
    527  1.1  haad 		}
    528  1.1  haad 		if (num_removed && old_area_count == new_area_count)
    529  1.1  haad 			return 1;
    530  1.1  haad 	} else
    531  1.1  haad 		new_area_count = old_area_count - num_removed;
    532  1.1  haad 
    533  1.1  haad 	/* Remove mimage LVs from the segment */
    534  1.1  haad 	dm_list_init(&tmp_orphan_lvs);
    535  1.1  haad 	for (m = new_area_count; m < mirrored_seg->area_count; m++) {
    536  1.1  haad 		seg_lv(mirrored_seg, m)->status &= ~MIRROR_IMAGE;
    537  1.1  haad 		seg_lv(mirrored_seg, m)->status |= VISIBLE_LV;
    538  1.1  haad 		if (!(lvl = dm_pool_alloc(lv->vg->cmd->mem, sizeof(*lvl)))) {
    539  1.1  haad 			log_error("lv_list alloc failed");
    540  1.1  haad 			return 0;
    541  1.1  haad 		}
    542  1.1  haad 		lvl->lv = seg_lv(mirrored_seg, m);
    543  1.1  haad 		dm_list_add(&tmp_orphan_lvs, &lvl->list);
    544  1.1  haad 		release_lv_segment_area(mirrored_seg, m, mirrored_seg->area_len);
    545  1.1  haad 	}
    546  1.1  haad 	mirrored_seg->area_count = new_area_count;
    547  1.1  haad 
    548  1.1  haad 	/* If no more mirrors, remove mirror layer */
    549  1.1  haad 	/* As an exceptional case, if the lv is temporary layer,
    550  1.1  haad 	 * leave the LV as mirrored and let the lvconvert completion
    551  1.1  haad 	 * to remove the layer. */
    552  1.1  haad 	if (new_area_count == 1 && !is_temporary_mirror_layer(lv)) {
    553  1.1  haad 		lv1 = seg_lv(mirrored_seg, 0);
    554  1.1  haad 		lv1->status &= ~MIRROR_IMAGE;
    555  1.1  haad 		lv1->status |= VISIBLE_LV;
    556  1.1  haad 		detached_log_lv = detach_mirror_log(mirrored_seg);
    557  1.1  haad 		if (!remove_layer_from_lv(lv, lv1))
    558  1.1  haad 			return_0;
    559  1.1  haad 		lv->status &= ~MIRRORED;
    560  1.1  haad 		lv->status &= ~MIRROR_NOTSYNCED;
    561  1.1  haad 		if (collapse && !_merge_mirror_images(lv, &tmp_orphan_lvs)) {
    562  1.1  haad 			log_error("Failed to add mirror images");
    563  1.1  haad 			return 0;
    564  1.1  haad 		}
    565  1.1  haad 	} else if (new_area_count == 0) {
    566  1.1  haad 		log_very_verbose("All mimages of %s are gone", lv->name);
    567  1.1  haad 
    568  1.1  haad 		/* All mirror images are gone.
    569  1.1  haad 		 * It can happen for vgreduce --removemissing. */
    570  1.1  haad 		detached_log_lv = detach_mirror_log(mirrored_seg);
    571  1.1  haad 		lv->status &= ~MIRRORED;
    572  1.1  haad 		lv->status &= ~MIRROR_NOTSYNCED;
    573  1.1  haad 		if (!replace_lv_with_error_segment(lv))
    574  1.1  haad 			return_0;
    575  1.1  haad 	} else if (remove_log)
    576  1.1  haad 		detached_log_lv = detach_mirror_log(mirrored_seg);
    577  1.1  haad 
    578  1.1  haad 	/*
    579  1.1  haad 	 * To successfully remove these unwanted LVs we need to
    580  1.1  haad 	 * remove the LVs from the mirror set, commit that metadata
    581  1.1  haad 	 * then deactivate and remove them fully.
    582  1.1  haad 	 */
    583  1.1  haad 
    584  1.1  haad 	if (!vg_write(mirrored_seg->lv->vg)) {
    585  1.1  haad 		log_error("intermediate VG write failed.");
    586  1.1  haad 		return 0;
    587  1.1  haad 	}
    588  1.1  haad 
    589  1.1  haad 	if (!suspend_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv)) {
    590  1.1  haad 		log_error("Failed to lock %s", mirrored_seg->lv->name);
    591  1.1  haad 		vg_revert(mirrored_seg->lv->vg);
    592  1.1  haad 		return 0;
    593  1.1  haad 	}
    594  1.1  haad 
    595  1.1  haad 	if (!vg_commit(mirrored_seg->lv->vg)) {
    596  1.1  haad 		resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv);
    597  1.1  haad 		return 0;
    598  1.1  haad 	}
    599  1.1  haad 
    600  1.1  haad 	log_very_verbose("Updating \"%s\" in kernel", mirrored_seg->lv->name);
    601  1.1  haad 
    602  1.1  haad 	/*
    603  1.1  haad 	 * Avoid having same mirror target loaded twice simultaneously by first
    604  1.1  haad 	 * resuming the removed LV which now contains an error segment.
    605  1.1  haad 	 * As it's now detached from mirrored_seg->lv we must resume it
    606  1.1  haad 	 * explicitly.
    607  1.1  haad 	 */
    608  1.1  haad 	if (lv1 && !resume_lv(lv1->vg->cmd, lv1)) {
    609  1.1  haad 		log_error("Problem resuming temporary LV, %s", lv1->name);
    610  1.1  haad 		return 0;
    611  1.1  haad 	}
    612  1.1  haad 
    613  1.1  haad 	if (!resume_lv(mirrored_seg->lv->vg->cmd, mirrored_seg->lv)) {
    614  1.1  haad 		log_error("Problem reactivating %s", mirrored_seg->lv->name);
    615  1.1  haad 		return 0;
    616  1.1  haad 	}
    617  1.1  haad 
    618  1.1  haad 	/* Save or delete the 'orphan' LVs */
    619  1.1  haad 	if (!collapse) {
    620  1.1  haad 		dm_list_iterate_items(lvl, &tmp_orphan_lvs)
    621  1.1  haad 			if (!_delete_lv(lv, lvl->lv))
    622  1.1  haad 				return_0;
    623  1.1  haad 	}
    624  1.1  haad 
    625  1.1  haad 	if (lv1 && !_delete_lv(lv, lv1))
    626  1.1  haad 		return_0;
    627  1.1  haad 
    628  1.1  haad 	if (detached_log_lv && !_delete_lv(lv, detached_log_lv))
    629  1.1  haad 		return_0;
    630  1.1  haad 
    631  1.1  haad 	/* Mirror with only 1 area is 'in sync'. */
    632  1.1  haad 	if (new_area_count == 1 && is_temporary_mirror_layer(lv)) {
    633  1.1  haad 		if (first_seg(lv)->log_lv &&
    634  1.1  haad 		    !_init_mirror_log(lv->vg->cmd, first_seg(lv)->log_lv,
    635  1.1  haad 				      1, &lv->tags, 0)) {
    636  1.1  haad 			/* As a result, unnecessary sync may run after
    637  1.1  haad 			 * collapsing. But safe.*/
    638  1.1  haad 			log_error("Failed to initialize log device");
    639  1.1  haad 			return_0;
    640  1.1  haad 		}
    641  1.1  haad 	}
    642  1.1  haad 
    643  1.1  haad 	if (removed)
    644  1.1  haad 		*removed = old_area_count - new_area_count;
    645  1.1  haad 
    646  1.1  haad 	log_very_verbose("%" PRIu32 " image(s) removed from %s",
    647  1.1  haad 			 old_area_count - num_removed, lv->name);
    648  1.1  haad 
    649  1.1  haad 	return 1;
    650  1.1  haad }
    651  1.1  haad 
    652  1.1  haad /*
    653  1.1  haad  * Remove the number of mirror images from the LV
    654  1.1  haad  */
    655  1.1  haad int remove_mirror_images(struct logical_volume *lv, uint32_t num_mirrors,
    656  1.1  haad 			 struct dm_list *removable_pvs, unsigned remove_log)
    657  1.1  haad {
    658  1.1  haad 	uint32_t num_removed, removed_once, r;
    659  1.1  haad 	uint32_t existing_mirrors = lv_mirror_count(lv);
    660  1.1  haad 	struct logical_volume *next_lv = lv;
    661  1.1  haad 
    662  1.1  haad 	num_removed = existing_mirrors - num_mirrors;
    663  1.1  haad 
    664  1.1  haad 	/* num_removed can be 0 if the function is called just to remove log */
    665  1.1  haad 	do {
    666  1.1  haad 		if (num_removed < first_seg(next_lv)->area_count)
    667  1.1  haad 			removed_once = num_removed;
    668  1.1  haad 		else
    669  1.1  haad 			removed_once = first_seg(next_lv)->area_count - 1;
    670  1.1  haad 
    671  1.1  haad 		if (!_remove_mirror_images(next_lv, removed_once,
    672  1.1  haad 					   removable_pvs, remove_log, 0, &r))
    673  1.1  haad 			return_0;
    674  1.1  haad 
    675  1.1  haad 		if (r < removed_once) {
    676  1.1  haad 			/* Some mirrors are removed from the temporary mirror,
    677  1.1  haad 			 * but the temporary layer still exists.
    678  1.1  haad 			 * Down the stack and retry for remainder. */
    679  1.1  haad 			next_lv = find_temporary_mirror(next_lv);
    680  1.1  haad 		}
    681  1.1  haad 
    682  1.1  haad 		num_removed -= r;
    683  1.1  haad 	} while (next_lv && num_removed);
    684  1.1  haad 
    685  1.1  haad 	if (num_removed) {
    686  1.1  haad 		if (num_removed == existing_mirrors - num_mirrors)
    687  1.1  haad 			log_error("No mirror images found using specified PVs.");
    688  1.1  haad 		else {
    689  1.1  haad 			log_error("%u images are removed out of requested %u.",
    690  1.1  haad 				  existing_mirrors - lv_mirror_count(lv),
    691  1.1  haad 				  existing_mirrors - num_mirrors);
    692  1.1  haad 		}
    693  1.1  haad 		return 0;
    694  1.1  haad 	}
    695  1.1  haad 
    696  1.1  haad 	return 1;
    697  1.1  haad }
    698  1.1  haad 
    699  1.1  haad static int _mirrored_lv_in_sync(struct logical_volume *lv)
    700  1.1  haad {
    701  1.1  haad 	float sync_percent;
    702  1.1  haad 
    703  1.1  haad 	if (!lv_mirror_percent(lv->vg->cmd, lv, 0, &sync_percent, NULL)) {
    704  1.1  haad 		log_error("Unable to determine mirror sync status of %s/%s.",
    705  1.1  haad 			  lv->vg->name, lv->name);
    706  1.1  haad 		return 0;
    707  1.1  haad 	}
    708  1.1  haad 
    709  1.1  haad 	if (sync_percent >= 100.0)
    710  1.1  haad 		return 1;
    711  1.1  haad 
    712  1.1  haad 	return 0;
    713  1.1  haad }
    714  1.1  haad 
    715  1.1  haad /*
    716  1.1  haad  * Collapsing temporary mirror layers.
    717  1.1  haad  *
    718  1.1  haad  * When mirrors are added to already-mirrored LV, a temporary mirror layer
    719  1.1  haad  * is inserted at the top of the stack to reduce resync work.
    720  1.1  haad  * The function will remove the intermediate layer and collapse the stack
    721  1.1  haad  * as far as mirrors are in-sync.
    722  1.1  haad  *
    723  1.1  haad  * The function is destructive: to remove intermediate mirror layers,
    724  1.1  haad  * VG metadata commits and suspend/resume are necessary.
    725  1.1  haad  */
    726  1.1  haad int collapse_mirrored_lv(struct logical_volume *lv)
    727  1.1  haad {
    728  1.1  haad 	struct logical_volume *tmp_lv;
    729  1.1  haad 	struct lv_segment *mirror_seg;
    730  1.1  haad 
    731  1.1  haad 	while ((tmp_lv = find_temporary_mirror(lv))) {
    732  1.1  haad 		mirror_seg = find_mirror_seg(first_seg(tmp_lv));
    733  1.1  haad 		if (!mirror_seg) {
    734  1.1  haad 			log_error("Failed to find mirrored LV for %s",
    735  1.1  haad 				  tmp_lv->name);
    736  1.1  haad 			return 0;
    737  1.1  haad 		}
    738  1.1  haad 
    739  1.1  haad 		if (!_mirrored_lv_in_sync(mirror_seg->lv)) {
    740  1.1  haad 			log_verbose("Not collapsing %s: out-of-sync",
    741  1.1  haad 				    mirror_seg->lv->name);
    742  1.1  haad 			return 1;
    743  1.1  haad 		}
    744  1.1  haad 
    745  1.1  haad 		if (!_remove_mirror_images(mirror_seg->lv,
    746  1.1  haad 					   mirror_seg->area_count - 1,
    747  1.1  haad 					   NULL, 1, 1, NULL)) {
    748  1.1  haad 			log_error("Failed to release mirror images");
    749  1.1  haad 			return 0;
    750  1.1  haad 		}
    751  1.1  haad 	}
    752  1.1  haad 
    753  1.1  haad 	return 1;
    754  1.1  haad }
    755  1.1  haad 
    756  1.1  haad static int get_mirror_fault_policy(struct cmd_context *cmd __attribute((unused)),
    757  1.1  haad 				   int log_policy)
    758  1.1  haad {
    759  1.1  haad 	const char *policy;
    760  1.1  haad 
    761  1.1  haad 	if (log_policy)
    762  1.1  haad 		policy = find_config_str(NULL, "activation/mirror_log_fault_policy",
    763  1.1  haad 					 DEFAULT_MIRROR_LOG_FAULT_POLICY);
    764  1.1  haad 	else
    765  1.1  haad 		policy = find_config_str(NULL, "activation/mirror_device_fault_policy",
    766  1.1  haad 					 DEFAULT_MIRROR_DEV_FAULT_POLICY);
    767  1.1  haad 
    768  1.1  haad 	if (!strcmp(policy, "remove"))
    769  1.1  haad 		return MIRROR_REMOVE;
    770  1.1  haad 	else if (!strcmp(policy, "allocate"))
    771  1.1  haad 		return MIRROR_ALLOCATE;
    772  1.1  haad 	else if (!strcmp(policy, "allocate_anywhere"))
    773  1.1  haad 		return MIRROR_ALLOCATE_ANYWHERE;
    774  1.1  haad 
    775  1.1  haad 	if (log_policy)
    776  1.1  haad 		log_error("Bad activation/mirror_log_fault_policy");
    777  1.1  haad 	else
    778  1.1  haad 		log_error("Bad activation/mirror_device_fault_policy");
    779  1.1  haad 
    780  1.1  haad 	return MIRROR_REMOVE;
    781  1.1  haad }
    782  1.1  haad 
    783  1.1  haad static int get_mirror_log_fault_policy(struct cmd_context *cmd)
    784  1.1  haad {
    785  1.1  haad 	return get_mirror_fault_policy(cmd, 1);
    786  1.1  haad }
    787  1.1  haad 
    788  1.1  haad static int get_mirror_device_fault_policy(struct cmd_context *cmd)
    789  1.1  haad {
    790  1.1  haad 	return get_mirror_fault_policy(cmd, 0);
    791  1.1  haad }
    792  1.1  haad 
    793  1.1  haad /*
    794  1.1  haad  * replace_mirror_images
    795  1.1  haad  * @mirrored_seg: segment (which may be linear now) to restore
    796  1.1  haad  * @num_mirrors: number of copies we should end up with
    797  1.1  haad  * @replace_log: replace log if not present
    798  1.1  haad  * @in_sync: was the original mirror in-sync?
    799  1.1  haad  *
    800  1.1  haad  * in_sync will be set to 0 if new mirror devices are being added
    801  1.1  haad  * In other words, it is only useful if the log (and only the log)
    802  1.1  haad  * is being restored.
    803  1.1  haad  *
    804  1.1  haad  * Returns: 0 on failure, 1 on reconfig, -1 if no reconfig done
    805  1.1  haad  */
    806  1.1  haad static int replace_mirror_images(struct lv_segment *mirrored_seg,
    807  1.1  haad 				 uint32_t num_mirrors,
    808  1.1  haad 				 int log_policy, int in_sync)
    809  1.1  haad {
    810  1.1  haad 	int r = -1;
    811  1.1  haad 	struct logical_volume *lv = mirrored_seg->lv;
    812  1.1  haad 
    813  1.1  haad 	/* FIXME: Use lvconvert rather than duplicating its code */
    814  1.1  haad 
    815  1.1  haad 	if (mirrored_seg->area_count < num_mirrors) {
    816  1.1  haad 		log_error("WARNING: Failed to replace mirror device in %s/%s",
    817  1.1  haad 			  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    818  1.1  haad 
    819  1.1  haad 		if ((mirrored_seg->area_count > 1) && !mirrored_seg->log_lv)
    820  1.1  haad 			log_error("WARNING: Use 'lvconvert -m %d %s/%s --corelog' to replace failed devices",
    821  1.1  haad 				  num_mirrors - 1, lv->vg->name, lv->name);
    822  1.1  haad 		else
    823  1.1  haad 			log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
    824  1.1  haad 				  num_mirrors - 1, lv->vg->name, lv->name);
    825  1.1  haad 		r = 0;
    826  1.1  haad 
    827  1.1  haad 		/* REMEMBER/FIXME: set in_sync to 0 if a new mirror device was added */
    828  1.1  haad 		in_sync = 0;
    829  1.1  haad 	}
    830  1.1  haad 
    831  1.1  haad 	/*
    832  1.1  haad 	 * FIXME: right now, we ignore the allocation policy specified to
    833  1.1  haad 	 * allocate the new log.
    834  1.1  haad 	 */
    835  1.1  haad 	if ((mirrored_seg->area_count > 1) && !mirrored_seg->log_lv &&
    836  1.1  haad 	    (log_policy != MIRROR_REMOVE)) {
    837  1.1  haad 		log_error("WARNING: Failed to replace mirror log device in %s/%s",
    838  1.1  haad 			  lv->vg->name, lv->name);
    839  1.1  haad 
    840  1.1  haad 		log_error("WARNING: Use 'lvconvert -m %d %s/%s' to replace failed devices",
    841  1.1  haad 			  mirrored_seg->area_count - 1 , lv->vg->name, lv->name);
    842  1.1  haad 		r = 0;
    843  1.1  haad 	}
    844  1.1  haad 
    845  1.1  haad 	return r;
    846  1.1  haad }
    847  1.1  haad 
    848  1.1  haad int reconfigure_mirror_images(struct lv_segment *mirrored_seg, uint32_t num_mirrors,
    849  1.1  haad 			      struct dm_list *removable_pvs, unsigned remove_log)
    850  1.1  haad {
    851  1.1  haad 	int r;
    852  1.1  haad 	int in_sync;
    853  1.1  haad 	int log_policy, dev_policy;
    854  1.1  haad 	uint32_t old_num_mirrors = mirrored_seg->area_count;
    855  1.1  haad 	int had_log = (mirrored_seg->log_lv) ? 1 : 0;
    856  1.1  haad 
    857  1.1  haad 	/* was the mirror in-sync before problems? */
    858  1.1  haad 	in_sync = _mirrored_lv_in_sync(mirrored_seg->lv);
    859  1.1  haad 
    860  1.1  haad 	/*
    861  1.1  haad 	 * While we are only removing devices, we can have sync set.
    862  1.1  haad 	 * Setting this is only useful if we are moving to core log
    863  1.1  haad 	 * otherwise the disk log will contain the sync information
    864  1.1  haad 	 */
    865  1.1  haad 	init_mirror_in_sync(in_sync);
    866  1.1  haad 
    867  1.1  haad 	r = _remove_mirror_images(mirrored_seg->lv, old_num_mirrors - num_mirrors,
    868  1.1  haad 				  removable_pvs, remove_log, 0, NULL);
    869  1.1  haad 	if (!r)
    870  1.1  haad 		/* Unable to remove bad devices */
    871  1.1  haad 		return 0;
    872  1.1  haad 
    873  1.1  haad 	log_warn("WARNING: Bad device removed from mirror volume, %s/%s",
    874  1.1  haad 		  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    875  1.1  haad 
    876  1.1  haad 	log_policy = get_mirror_log_fault_policy(mirrored_seg->lv->vg->cmd);
    877  1.1  haad 	dev_policy = get_mirror_device_fault_policy(mirrored_seg->lv->vg->cmd);
    878  1.1  haad 
    879  1.1  haad 	r = replace_mirror_images(mirrored_seg,
    880  1.1  haad 				  (dev_policy != MIRROR_REMOVE) ?
    881  1.1  haad 				  old_num_mirrors : num_mirrors,
    882  1.1  haad 				  log_policy, in_sync);
    883  1.1  haad 
    884  1.1  haad 	if (!r)
    885  1.1  haad 		/* Failed to replace device(s) */
    886  1.1  haad 		log_error("WARNING: Unable to find substitute device for mirror volume, %s/%s",
    887  1.1  haad 			  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    888  1.1  haad 	else if (r > 0)
    889  1.1  haad 		/* Success in replacing device(s) */
    890  1.1  haad 		log_warn("WARNING: Mirror volume, %s/%s restored - substitute for failed device found.",
    891  1.1  haad 			  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    892  1.1  haad 	else
    893  1.1  haad 		/* Bad device removed, but not replaced because of policy */
    894  1.1  haad 		if (mirrored_seg->area_count == 1) {
    895  1.1  haad 			log_warn("WARNING: Mirror volume, %s/%s converted to linear due to device failure.",
    896  1.1  haad 				  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    897  1.1  haad 		} else if (had_log && !mirrored_seg->log_lv) {
    898  1.1  haad 			log_warn("WARNING: Mirror volume, %s/%s disk log removed due to device failure.",
    899  1.1  haad 				  mirrored_seg->lv->vg->name, mirrored_seg->lv->name);
    900  1.1  haad 		}
    901  1.1  haad 	/*
    902  1.1  haad 	 * If we made it here, we at least removed the bad device.
    903  1.1  haad 	 * Consider this success.
    904  1.1  haad 	 */
    905  1.1  haad 	return 1;
    906  1.1  haad }
    907  1.1  haad 
    908  1.1  haad static int _create_mimage_lvs(struct alloc_handle *ah,
    909  1.1  haad 			      uint32_t num_mirrors,
    910  1.1  haad 			      struct logical_volume *lv,
    911  1.1  haad 			      struct logical_volume **img_lvs)
    912  1.1  haad {
    913  1.1  haad 	uint32_t m;
    914  1.1  haad 	char *img_name;
    915  1.1  haad 	size_t len;
    916  1.1  haad 
    917  1.1  haad 	len = strlen(lv->name) + 32;
    918  1.1  haad 	if (!(img_name = alloca(len))) {
    919  1.1  haad 		log_error("img_name allocation failed. "
    920  1.1  haad 			  "Remove new LV and retry.");
    921  1.1  haad 		return 0;
    922  1.1  haad 	}
    923  1.1  haad 
    924  1.1  haad 	if (dm_snprintf(img_name, len, "%s_mimage_%%d", lv->name) < 0) {
    925  1.1  haad 		log_error("img_name allocation failed. "
    926  1.1  haad 			  "Remove new LV and retry.");
    927  1.1  haad 		return 0;
    928  1.1  haad 	}
    929  1.1  haad 
    930  1.1  haad 	for (m = 0; m < num_mirrors; m++) {
    931  1.1  haad 		if (!(img_lvs[m] = lv_create_empty(img_name,
    932  1.1  haad 					     NULL, LVM_READ | LVM_WRITE,
    933  1.1  haad 					     ALLOC_INHERIT, 0, lv->vg))) {
    934  1.1  haad 			log_error("Aborting. Failed to create mirror image LV. "
    935  1.1  haad 				  "Remove new LV and retry.");
    936  1.1  haad 			return 0;
    937  1.1  haad 		}
    938  1.1  haad 
    939  1.1  haad 		if (!lv_add_segment(ah, m, 1, img_lvs[m],
    940  1.1  haad 				    get_segtype_from_string(lv->vg->cmd,
    941  1.1  haad 							    "striped"),
    942  1.1  haad 				    0, 0, 0, NULL)) {
    943  1.1  haad 			log_error("Aborting. Failed to add mirror image segment "
    944  1.1  haad 				  "to %s. Remove new LV and retry.",
    945  1.1  haad 				  img_lvs[m]->name);
    946  1.1  haad 			return 0;
    947  1.1  haad 		}
    948  1.1  haad 	}
    949  1.1  haad 
    950  1.1  haad 	return 1;
    951  1.1  haad }
    952  1.1  haad 
    953  1.1  haad /*
    954  1.1  haad  * Remove mirrors from each segment.
    955  1.1  haad  * 'new_mirrors' is the number of mirrors after the removal. '0' for linear.
    956  1.1  haad  * If 'status_mask' is non-zero, the removal happens only when all segments
    957  1.1  haad  * has the status bits on.
    958  1.1  haad  */
    959  1.1  haad int remove_mirrors_from_segments(struct logical_volume *lv,
    960  1.1  haad 				 uint32_t new_mirrors, uint32_t status_mask)
    961  1.1  haad {
    962  1.1  haad 	struct lv_segment *seg;
    963  1.1  haad 	uint32_t s;
    964  1.1  haad 
    965  1.1  haad 	/* Check the segment params are compatible */
    966  1.1  haad 	dm_list_iterate_items(seg, &lv->segments) {
    967  1.1  haad 		if (!seg_is_mirrored(seg)) {
    968  1.1  haad 			log_error("Segment is not mirrored: %s:%" PRIu32,
    969  1.1  haad 				  lv->name, seg->le);
    970  1.1  haad 			return 0;
    971  1.1  haad 		} if ((seg->status & status_mask) != status_mask) {
    972  1.1  haad 			log_error("Segment status does not match: %s:%" PRIu32
    973  1.1  haad 				  " status:0x%x/0x%x", lv->name, seg->le,
    974  1.1  haad 				  seg->status, status_mask);
    975  1.1  haad 			return 0;
    976  1.1  haad 		}
    977  1.1  haad 	}
    978  1.1  haad 
    979  1.1  haad 	/* Convert the segments */
    980  1.1  haad 	dm_list_iterate_items(seg, &lv->segments) {
    981  1.1  haad 		if (!new_mirrors && seg->extents_copied == seg->area_len) {
    982  1.1  haad 			if (!move_lv_segment_area(seg, 0, seg, 1))
    983  1.1  haad 				return_0;
    984  1.1  haad 		}
    985  1.1  haad 
    986  1.1  haad 		for (s = new_mirrors + 1; s < seg->area_count; s++)
    987  1.1  haad 			release_lv_segment_area(seg, s, seg->area_len);
    988  1.1  haad 
    989  1.1  haad 		seg->area_count = new_mirrors + 1;
    990  1.1  haad 
    991  1.1  haad 		if (!new_mirrors)
    992  1.1  haad 			seg->segtype = get_segtype_from_string(lv->vg->cmd,
    993  1.1  haad 							       "striped");
    994  1.1  haad 	}
    995  1.1  haad 
    996  1.1  haad 	return 1;
    997  1.1  haad }
    998  1.1  haad 
    999  1.1  haad const char *get_pvmove_pvname_from_lv_mirr(struct logical_volume *lv_mirr)
   1000  1.1  haad {
   1001  1.1  haad 	struct lv_segment *seg;
   1002  1.1  haad 
   1003  1.1  haad 	dm_list_iterate_items(seg, &lv_mirr->segments) {
   1004  1.1  haad 		if (!seg_is_mirrored(seg))
   1005  1.1  haad 			continue;
   1006  1.1  haad 		if (seg_type(seg, 0) != AREA_PV)
   1007  1.1  haad 			continue;
   1008  1.1  haad 		return dev_name(seg_dev(seg, 0));
   1009  1.1  haad 	}
   1010  1.1  haad 
   1011  1.1  haad 	return NULL;
   1012  1.1  haad }
   1013  1.1  haad 
   1014  1.1  haad const char *get_pvmove_pvname_from_lv(struct logical_volume *lv)
   1015  1.1  haad {
   1016  1.1  haad 	struct lv_segment *seg;
   1017  1.1  haad 	uint32_t s;
   1018  1.1  haad 
   1019  1.1  haad 	dm_list_iterate_items(seg, &lv->segments) {
   1020  1.1  haad 		for (s = 0; s < seg->area_count; s++) {
   1021  1.1  haad 			if (seg_type(seg, s) != AREA_LV)
   1022  1.1  haad 				continue;
   1023  1.1  haad 			return get_pvmove_pvname_from_lv_mirr(seg_lv(seg, s));
   1024  1.1  haad 		}
   1025  1.1  haad 	}
   1026  1.1  haad 
   1027  1.1  haad 	return NULL;
   1028  1.1  haad }
   1029  1.1  haad 
   1030  1.1  haad struct logical_volume *find_pvmove_lv(struct volume_group *vg,
   1031  1.1  haad 				      struct device *dev,
   1032  1.1  haad 				      uint32_t lv_type)
   1033  1.1  haad {
   1034  1.1  haad 	struct lv_list *lvl;
   1035  1.1  haad 	struct logical_volume *lv;
   1036  1.1  haad 	struct lv_segment *seg;
   1037  1.1  haad 
   1038  1.1  haad 	/* Loop through all LVs */
   1039  1.1  haad 	dm_list_iterate_items(lvl, &vg->lvs) {
   1040  1.1  haad 		lv = lvl->lv;
   1041  1.1  haad 
   1042  1.1  haad 		if (!(lv->status & lv_type))
   1043  1.1  haad 			continue;
   1044  1.1  haad 
   1045  1.1  haad 		/* Check segment origins point to pvname */
   1046  1.1  haad 		dm_list_iterate_items(seg, &lv->segments) {
   1047  1.1  haad 			if (seg_type(seg, 0) != AREA_PV)
   1048  1.1  haad 				continue;
   1049  1.1  haad 			if (seg_dev(seg, 0) != dev)
   1050  1.1  haad 				continue;
   1051  1.1  haad 			return lv;
   1052  1.1  haad 		}
   1053  1.1  haad 	}
   1054  1.1  haad 
   1055  1.1  haad 	return NULL;
   1056  1.1  haad }
   1057  1.1  haad 
   1058  1.1  haad struct logical_volume *find_pvmove_lv_from_pvname(struct cmd_context *cmd,
   1059  1.1  haad 					 	  struct volume_group *vg,
   1060  1.1  haad 				      		  const char *name,
   1061  1.1  haad 				      		  uint32_t lv_type)
   1062  1.1  haad {
   1063  1.1  haad 	struct physical_volume *pv;
   1064  1.1  haad 
   1065  1.1  haad 	if (!(pv = find_pv_by_name(cmd, name)))
   1066  1.1  haad 		return_NULL;
   1067  1.1  haad 
   1068  1.1  haad 	return find_pvmove_lv(vg, pv->dev, lv_type);
   1069  1.1  haad }
   1070  1.1  haad 
   1071  1.1  haad struct dm_list *lvs_using_lv(struct cmd_context *cmd, struct volume_group *vg,
   1072  1.1  haad 			  struct logical_volume *lv)
   1073  1.1  haad {
   1074  1.1  haad 	struct dm_list *lvs;
   1075  1.1  haad 	struct logical_volume *lv1;
   1076  1.1  haad 	struct lv_list *lvl, *lvl1;
   1077  1.1  haad 	struct lv_segment *seg;
   1078  1.1  haad 	uint32_t s;
   1079  1.1  haad 
   1080  1.1  haad 	if (!(lvs = dm_pool_alloc(cmd->mem, sizeof(*lvs)))) {
   1081  1.1  haad 		log_error("lvs list alloc failed");
   1082  1.1  haad 		return NULL;
   1083  1.1  haad 	}
   1084  1.1  haad 
   1085  1.1  haad 	dm_list_init(lvs);
   1086  1.1  haad 
   1087  1.1  haad 	/* Loop through all LVs except the one supplied */
   1088  1.1  haad 	dm_list_iterate_items(lvl1, &vg->lvs) {
   1089  1.1  haad 		lv1 = lvl1->lv;
   1090  1.1  haad 		if (lv1 == lv)
   1091  1.1  haad 			continue;
   1092  1.1  haad 
   1093  1.1  haad 		/* Find whether any segment points at the supplied LV */
   1094  1.1  haad 		dm_list_iterate_items(seg, &lv1->segments) {
   1095  1.1  haad 			for (s = 0; s < seg->area_count; s++) {
   1096  1.1  haad 				if (seg_type(seg, s) != AREA_LV ||
   1097  1.1  haad 				    seg_lv(seg, s) != lv)
   1098  1.1  haad 					continue;
   1099  1.1  haad 				if (!(lvl = dm_pool_alloc(cmd->mem, sizeof(*lvl)))) {
   1100  1.1  haad 					log_error("lv_list alloc failed");
   1101  1.1  haad 					return NULL;
   1102  1.1  haad 				}
   1103  1.1  haad 				lvl->lv = lv1;
   1104  1.1  haad 				dm_list_add(lvs, &lvl->list);
   1105  1.1  haad 				goto next_lv;
   1106  1.1  haad 			}
   1107  1.1  haad 		}
   1108  1.1  haad 	      next_lv:
   1109  1.1  haad 		;
   1110  1.1  haad 	}
   1111  1.1  haad 
   1112  1.1  haad 	return lvs;
   1113  1.1  haad }
   1114  1.1  haad 
   1115  1.1  haad float copy_percent(struct logical_volume *lv_mirr)
   1116  1.1  haad {
   1117  1.1  haad 	uint32_t numerator = 0u, denominator = 0u;
   1118  1.1  haad 	struct lv_segment *seg;
   1119  1.1  haad 
   1120  1.1  haad 	dm_list_iterate_items(seg, &lv_mirr->segments) {
   1121  1.1  haad 		denominator += seg->area_len;
   1122  1.1  haad 
   1123  1.1  haad 		if (seg_is_mirrored(seg) && seg->area_count > 1)
   1124  1.1  haad 			numerator += seg->extents_copied;
   1125  1.1  haad 		else
   1126  1.1  haad 			numerator += seg->area_len;
   1127  1.1  haad 	}
   1128  1.1  haad 
   1129  1.1  haad 	return denominator ? (float) numerator *100 / denominator : 100.0;
   1130  1.1  haad }
   1131  1.1  haad 
   1132  1.1  haad /*
   1133  1.1  haad  * Fixup mirror pointers after single-pass segment import
   1134  1.1  haad  */
   1135  1.1  haad int fixup_imported_mirrors(struct volume_group *vg)
   1136  1.1  haad {
   1137  1.1  haad 	struct lv_list *lvl;
   1138  1.1  haad 	struct lv_segment *seg;
   1139  1.1  haad 
   1140  1.1  haad 	dm_list_iterate_items(lvl, &vg->lvs) {
   1141  1.1  haad 		dm_list_iterate_items(seg, &lvl->lv->segments) {
   1142  1.1  haad 			if (seg->segtype !=
   1143  1.1  haad 			    get_segtype_from_string(vg->cmd, "mirror"))
   1144  1.1  haad 				continue;
   1145  1.1  haad 
   1146  1.1  haad 			if (seg->log_lv && !add_seg_to_segs_using_this_lv(seg->log_lv, seg))
   1147  1.1  haad 				return_0;
   1148  1.1  haad 		}
   1149  1.1  haad 	}
   1150  1.1  haad 
   1151  1.1  haad 	return 1;
   1152  1.1  haad }
   1153  1.1  haad 
   1154  1.1  haad /*
   1155  1.1  haad  * Add mirrors to "linear" or "mirror" segments
   1156  1.1  haad  */
   1157  1.1  haad int add_mirrors_to_segments(struct cmd_context *cmd, struct logical_volume *lv,
   1158  1.1  haad 			    uint32_t mirrors, uint32_t region_size,
   1159  1.1  haad 			    struct dm_list *allocatable_pvs, alloc_policy_t alloc)
   1160  1.1  haad {
   1161  1.1  haad 	struct alloc_handle *ah;
   1162  1.1  haad 	const struct segment_type *segtype;
   1163  1.1  haad 	struct dm_list *parallel_areas;
   1164  1.1  haad 	uint32_t adjusted_region_size;
   1165  1.1  haad 
   1166  1.1  haad 	if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
   1167  1.1  haad 		return_0;
   1168  1.1  haad 
   1169  1.1  haad 	if (!(segtype = get_segtype_from_string(cmd, "mirror")))
   1170  1.1  haad 		return_0;
   1171  1.1  haad 
   1172  1.1  haad 	adjusted_region_size = adjusted_mirror_region_size(lv->vg->extent_size,
   1173  1.1  haad 							   lv->le_count,
   1174  1.1  haad 							   region_size);
   1175  1.1  haad 
   1176  1.1  haad 	if (!(ah = allocate_extents(lv->vg, NULL, segtype, 1, mirrors, 0,
   1177  1.1  haad 				    lv->le_count, allocatable_pvs, alloc,
   1178  1.1  haad 				    parallel_areas))) {
   1179  1.1  haad 		log_error("Unable to allocate mirror extents for %s.", lv->name);
   1180  1.1  haad 		return 0;
   1181  1.1  haad 	}
   1182  1.1  haad 
   1183  1.1  haad 	if (!lv_add_mirror_areas(ah, lv, 0, adjusted_region_size)) {
   1184  1.1  haad 		log_error("Failed to add mirror areas to %s", lv->name);
   1185  1.1  haad 		return 0;
   1186  1.1  haad 	}
   1187  1.1  haad 
   1188  1.1  haad 	return 1;
   1189  1.1  haad }
   1190  1.1  haad 
   1191  1.1  haad /*
   1192  1.1  haad  * Convert mirror log
   1193  1.1  haad  *
   1194  1.1  haad  * FIXME: Can't handle segment-by-segment mirror (like pvmove)
   1195  1.1  haad  */
   1196  1.1  haad int remove_mirror_log(struct cmd_context *cmd,
   1197  1.1  haad 		      struct logical_volume *lv,
   1198  1.1  haad 		      struct dm_list *removable_pvs)
   1199  1.1  haad {
   1200  1.1  haad 	float sync_percent;
   1201  1.1  haad 	struct lvinfo info;
   1202  1.1  haad 	struct volume_group *vg = lv->vg;
   1203  1.1  haad 
   1204  1.1  haad 	/* Unimplemented features */
   1205  1.1  haad 	if (dm_list_size(&lv->segments) != 1) {
   1206  1.1  haad 		log_error("Multiple-segment mirror is not supported");
   1207  1.1  haad 		return 0;
   1208  1.1  haad 	}
   1209  1.1  haad 
   1210  1.1  haad 	/* Had disk log, switch to core. */
   1211  1.1  haad 	if (lv_info(cmd, lv, &info, 0, 0) && info.exists) {
   1212  1.1  haad 		if (!lv_mirror_percent(cmd, lv, 0, &sync_percent, NULL)) {
   1213  1.1  haad 			log_error("Unable to determine mirror sync status.");
   1214  1.1  haad 			return 0;
   1215  1.1  haad 		}
   1216  1.1  haad 	} else if (vg_is_clustered(vg)) {
   1217  1.1  haad 		log_error("Unable to convert the log of inactive "
   1218  1.1  haad 			  "cluster mirror %s", lv->name);
   1219  1.1  haad 		return 0;
   1220  1.1  haad 	} else if (yes_no_prompt("Full resync required to convert "
   1221  1.1  haad 				 "inactive mirror %s to core log. "
   1222  1.1  haad 				 "Proceed? [y/n]: "))
   1223  1.1  haad 		sync_percent = 0;
   1224  1.1  haad 	else
   1225  1.1  haad 		return 0;
   1226  1.1  haad 
   1227  1.1  haad 	if (sync_percent >= 100.0)
   1228  1.1  haad 		init_mirror_in_sync(1);
   1229  1.1  haad 	else {
   1230  1.1  haad 		/* A full resync will take place */
   1231  1.1  haad 		lv->status &= ~MIRROR_NOTSYNCED;
   1232  1.1  haad 		init_mirror_in_sync(0);
   1233  1.1  haad 	}
   1234  1.1  haad 
   1235  1.1  haad 	if (!remove_mirror_images(lv, lv_mirror_count(lv),
   1236  1.1  haad 				  removable_pvs, 1U))
   1237  1.1  haad 		return_0;
   1238  1.1  haad 
   1239  1.1  haad 	return 1;
   1240  1.1  haad }
   1241  1.1  haad 
   1242  1.1  haad static struct logical_volume *_create_mirror_log(struct logical_volume *lv,
   1243  1.1  haad 						 struct alloc_handle *ah,
   1244  1.1  haad 						 alloc_policy_t alloc,
   1245  1.1  haad 						 const char *lv_name,
   1246  1.1  haad 						 const char *suffix)
   1247  1.1  haad {
   1248  1.1  haad 	struct logical_volume *log_lv;
   1249  1.1  haad 	char *log_name;
   1250  1.1  haad 	size_t len;
   1251  1.1  haad 
   1252  1.1  haad 	len = strlen(lv_name) + 32;
   1253  1.1  haad 	if (!(log_name = alloca(len))) {
   1254  1.1  haad 		log_error("log_name allocation failed.");
   1255  1.1  haad 		return NULL;
   1256  1.1  haad 	}
   1257  1.1  haad 
   1258  1.1  haad 	if (dm_snprintf(log_name, len, "%s%s", lv_name, suffix) < 0) {
   1259  1.1  haad 		log_error("log_name allocation failed.");
   1260  1.1  haad 		return NULL;
   1261  1.1  haad 	}
   1262  1.1  haad 
   1263  1.1  haad 	if (!(log_lv = lv_create_empty(log_name, NULL,
   1264  1.1  haad 				       VISIBLE_LV | LVM_READ | LVM_WRITE,
   1265  1.1  haad 				       alloc, 0, lv->vg)))
   1266  1.1  haad 		return_NULL;
   1267  1.1  haad 
   1268  1.1  haad 	if (!lv_add_log_segment(ah, log_lv))
   1269  1.1  haad 		return_NULL;
   1270  1.1  haad 
   1271  1.1  haad 	return log_lv;
   1272  1.1  haad }
   1273  1.1  haad 
   1274  1.1  haad static struct logical_volume *_set_up_mirror_log(struct cmd_context *cmd,
   1275  1.1  haad 						 struct alloc_handle *ah,
   1276  1.1  haad 						 struct logical_volume *lv,
   1277  1.1  haad 						 uint32_t log_count,
   1278  1.1  haad 						 uint32_t region_size __attribute((unused)),
   1279  1.1  haad 						 alloc_policy_t alloc,
   1280  1.1  haad 						 int in_sync)
   1281  1.1  haad {
   1282  1.1  haad 	struct logical_volume *log_lv;
   1283  1.1  haad 	const char *suffix, *c;
   1284  1.1  haad 	char *lv_name;
   1285  1.1  haad 	size_t len;
   1286  1.1  haad 	struct lv_segment *seg;
   1287  1.1  haad 
   1288  1.1  haad 	init_mirror_in_sync(in_sync);
   1289  1.1  haad 
   1290  1.1  haad 	if (log_count != 1) {
   1291  1.1  haad 		log_error("log_count != 1 is not supported.");
   1292  1.1  haad 		return NULL;
   1293  1.1  haad 	}
   1294  1.1  haad 
   1295  1.1  haad 	/* Mirror log name is lv_name + suffix, determined as the following:
   1296  1.1  haad 	 *   1. suffix is:
   1297  1.1  haad 	 *        o "_mlog" for the original mirror LV.
   1298  1.1  haad 	 *        o "_mlogtmp_%d" for temporary mirror LV,
   1299  1.1  haad 	 *   2. lv_name is:
   1300  1.1  haad 	 *        o lv->name, if the log is temporary
   1301  1.1  haad 	 *        o otherwise, the top-level LV name
   1302  1.1  haad 	 */
   1303  1.1  haad 	seg = first_seg(lv);
   1304  1.1  haad 	if (seg_type(seg, 0) == AREA_LV &&
   1305  1.1  haad 	    strstr(seg_lv(seg, 0)->name, MIRROR_SYNC_LAYER)) {
   1306  1.1  haad 		lv_name = lv->name;
   1307  1.1  haad 		suffix = "_mlogtmp_%d";
   1308  1.1  haad 	} else if ((c = strstr(lv->name, MIRROR_SYNC_LAYER))) {
   1309  1.1  haad 		len = c - lv->name + 1;
   1310  1.1  haad 		if (!(lv_name = alloca(len)) ||
   1311  1.1  haad 		    !dm_snprintf(lv_name, len, "%s", lv->name)) {
   1312  1.1  haad 			log_error("mirror log name allocation failed");
   1313  1.1  haad 			return 0;
   1314  1.1  haad 		}
   1315  1.1  haad 		suffix = "_mlog";
   1316  1.1  haad 	} else {
   1317  1.1  haad 		lv_name = lv->name;
   1318  1.1  haad 		suffix = "_mlog";
   1319  1.1  haad 	}
   1320  1.1  haad 
   1321  1.1  haad 	if (!(log_lv = _create_mirror_log(lv, ah, alloc,
   1322  1.1  haad 					  (const char *) lv_name, suffix))) {
   1323  1.1  haad 		log_error("Failed to create mirror log.");
   1324  1.1  haad 		return NULL;
   1325  1.1  haad 	}
   1326  1.1  haad 
   1327  1.1  haad 	if (!_init_mirror_log(cmd, log_lv, in_sync, &lv->tags, 1)) {
   1328  1.1  haad 		log_error("Failed to create mirror log.");
   1329  1.1  haad 		return NULL;
   1330  1.1  haad 	}
   1331  1.1  haad 
   1332  1.1  haad 	return log_lv;
   1333  1.1  haad }
   1334  1.1  haad 
   1335  1.1  haad int attach_mirror_log(struct lv_segment *seg, struct logical_volume *log_lv)
   1336  1.1  haad {
   1337  1.1  haad 	seg->log_lv = log_lv;
   1338  1.1  haad 	log_lv->status |= MIRROR_LOG;
   1339  1.1  haad 	log_lv->status &= ~VISIBLE_LV;
   1340  1.1  haad 	return add_seg_to_segs_using_this_lv(log_lv, seg);
   1341  1.1  haad }
   1342  1.1  haad 
   1343  1.1  haad int add_mirror_log(struct cmd_context *cmd, struct logical_volume *lv,
   1344  1.1  haad 		   uint32_t log_count, uint32_t region_size,
   1345  1.1  haad 		   struct dm_list *allocatable_pvs, alloc_policy_t alloc)
   1346  1.1  haad {
   1347  1.1  haad 	struct alloc_handle *ah;
   1348  1.1  haad 	const struct segment_type *segtype;
   1349  1.1  haad 	struct dm_list *parallel_areas;
   1350  1.1  haad 	float sync_percent;
   1351  1.1  haad 	int in_sync;
   1352  1.1  haad 	struct logical_volume *log_lv;
   1353  1.1  haad 	struct lvinfo info;
   1354  1.1  haad 
   1355  1.1  haad 	/* Unimplemented features */
   1356  1.1  haad 	if (log_count > 1) {
   1357  1.1  haad 		log_error("log_count > 1 is not supported");
   1358  1.1  haad 		return 0;
   1359  1.1  haad 	}
   1360  1.1  haad 
   1361  1.1  haad 	if (dm_list_size(&lv->segments) != 1) {
   1362  1.1  haad 		log_error("Multiple-segment mirror is not supported");
   1363  1.1  haad 		return 0;
   1364  1.1  haad 	}
   1365  1.1  haad 
   1366  1.1  haad 	/*
   1367  1.1  haad 	 * We are unable to convert the log of inactive cluster mirrors
   1368  1.1  haad 	 * due to the inability to detect whether the mirror is active
   1369  1.1  haad 	 * on remote nodes (even though it is inactive on this node)
   1370  1.1  haad 	 */
   1371  1.1  haad 	if (vg_is_clustered(lv->vg) &&
   1372  1.1  haad 	    !(lv_info(cmd, lv, &info, 0, 0) && info.exists)) {
   1373  1.1  haad 		log_error("Unable to convert the log of inactive "
   1374  1.1  haad 			  "cluster mirror %s", lv->name);
   1375  1.1  haad 		return 0;
   1376  1.1  haad 	}
   1377  1.1  haad 
   1378  1.1  haad 	if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
   1379  1.1  haad 		return_0;
   1380  1.1  haad 
   1381  1.1  haad 	if (!(segtype = get_segtype_from_string(cmd, "mirror")))
   1382  1.1  haad 		return_0;
   1383  1.1  haad 
   1384  1.1  haad 	if (activation() && segtype->ops->target_present &&
   1385  1.1  haad 	    !segtype->ops->target_present(NULL, NULL)) {
   1386  1.1  haad 		log_error("%s: Required device-mapper target(s) not "
   1387  1.1  haad 			  "detected in your kernel", segtype->name);
   1388  1.1  haad 		return 0;
   1389  1.1  haad 	}
   1390  1.1  haad 
   1391  1.1  haad 	/* allocate destination extents */
   1392  1.1  haad 	ah = allocate_extents(lv->vg, NULL, segtype,
   1393  1.1  haad 			      0, 0, log_count, 0,
   1394  1.1  haad 			      allocatable_pvs, alloc, parallel_areas);
   1395  1.1  haad 	if (!ah) {
   1396  1.1  haad 		log_error("Unable to allocate extents for mirror log.");
   1397  1.1  haad 		return 0;
   1398  1.1  haad 	}
   1399  1.1  haad 
   1400  1.1  haad 	/* check sync status */
   1401  1.1  haad 	if (lv_mirror_percent(cmd, lv, 0, &sync_percent, NULL) &&
   1402  1.1  haad 	    sync_percent >= 100.0)
   1403  1.1  haad 		in_sync = 1;
   1404  1.1  haad 	else
   1405  1.1  haad 		in_sync = 0;
   1406  1.1  haad 
   1407  1.1  haad 	if (!(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count,
   1408  1.1  haad 					  region_size, alloc, in_sync)))
   1409  1.1  haad 		return_0;
   1410  1.1  haad 
   1411  1.1  haad 	if (!attach_mirror_log(first_seg(lv), log_lv))
   1412  1.1  haad 		return_0;
   1413  1.1  haad 
   1414  1.1  haad 	alloc_destroy(ah);
   1415  1.1  haad 	return 1;
   1416  1.1  haad }
   1417  1.1  haad 
   1418  1.1  haad /*
   1419  1.1  haad  * Convert "linear" LV to "mirror".
   1420  1.1  haad  */
   1421  1.1  haad int add_mirror_images(struct cmd_context *cmd, struct logical_volume *lv,
   1422  1.1  haad 		      uint32_t mirrors, uint32_t stripes, uint32_t region_size,
   1423  1.1  haad 		      struct dm_list *allocatable_pvs, alloc_policy_t alloc,
   1424  1.1  haad 		      uint32_t log_count)
   1425  1.1  haad {
   1426  1.1  haad 	struct alloc_handle *ah;
   1427  1.1  haad 	const struct segment_type *segtype;
   1428  1.1  haad 	struct dm_list *parallel_areas;
   1429  1.1  haad 	struct logical_volume **img_lvs;
   1430  1.1  haad 	struct logical_volume *log_lv = NULL;
   1431  1.1  haad 
   1432  1.1  haad 	if (stripes > 1) {
   1433  1.1  haad 		log_error("stripes > 1 is not supported");
   1434  1.1  haad 		return 0;
   1435  1.1  haad 	}
   1436  1.1  haad 
   1437  1.1  haad 	/*
   1438  1.1  haad 	 * allocate destination extents
   1439  1.1  haad 	 */
   1440  1.1  haad 
   1441  1.1  haad 	if (!(parallel_areas = build_parallel_areas_from_lv(cmd, lv)))
   1442  1.1  haad 		return_0;
   1443  1.1  haad 
   1444  1.1  haad 	if (!(segtype = get_segtype_from_string(cmd, "mirror")))
   1445  1.1  haad 		return_0;
   1446  1.1  haad 
   1447  1.1  haad 	ah = allocate_extents(lv->vg, NULL, segtype,
   1448  1.1  haad 			      stripes, mirrors, log_count, lv->le_count,
   1449  1.1  haad 			      allocatable_pvs, alloc, parallel_areas);
   1450  1.1  haad 	if (!ah) {
   1451  1.1  haad 		log_error("Unable to allocate extents for mirror(s).");
   1452  1.1  haad 		return 0;
   1453  1.1  haad 	}
   1454  1.1  haad 
   1455  1.1  haad 	/*
   1456  1.1  haad 	 * create and initialize mirror log
   1457  1.1  haad 	 */
   1458  1.1  haad 	if (log_count &&
   1459  1.1  haad 	    !(log_lv = _set_up_mirror_log(cmd, ah, lv, log_count, region_size,
   1460  1.1  haad 					  alloc, mirror_in_sync())))
   1461  1.1  haad 		return_0;
   1462  1.1  haad 
   1463  1.1  haad 	/* The log initialization involves vg metadata commit.
   1464  1.1  haad 	   So from here on, if failure occurs, the log must be explicitly
   1465  1.1  haad 	   removed and the updated vg metadata should be committed. */
   1466  1.1  haad 
   1467  1.1  haad 	/*
   1468  1.1  haad 	 * insert a mirror layer
   1469  1.1  haad 	 */
   1470  1.1  haad 	if (dm_list_size(&lv->segments) != 1 ||
   1471  1.1  haad 	    seg_type(first_seg(lv), 0) != AREA_LV)
   1472  1.1  haad 		if (!insert_layer_for_lv(cmd, lv, 0, "_mimage_%d"))
   1473  1.1  haad 			goto out_remove_log;
   1474  1.1  haad 
   1475  1.1  haad 	/*
   1476  1.1  haad 	 * create mirror image LVs
   1477  1.1  haad 	 */
   1478  1.1  haad 	if (!(img_lvs = alloca(sizeof(*img_lvs) * mirrors))) {
   1479  1.1  haad 		log_error("img_lvs allocation failed. "
   1480  1.1  haad 			  "Remove new LV and retry.");
   1481  1.1  haad 		goto out_remove_log;
   1482  1.1  haad 	}
   1483  1.1  haad 
   1484  1.1  haad 	if (!_create_mimage_lvs(ah, mirrors, lv, img_lvs))
   1485  1.1  haad 		goto out_remove_log;
   1486  1.1  haad 
   1487  1.1  haad 	if (!lv_add_mirror_lvs(lv, img_lvs, mirrors,
   1488  1.1  haad 			       MIRROR_IMAGE | (lv->status & LOCKED),
   1489  1.1  haad 			       region_size)) {
   1490  1.1  haad 		log_error("Aborting. Failed to add mirror segment. "
   1491  1.1  haad 			  "Remove new LV and retry.");
   1492  1.1  haad 		goto out_remove_imgs;
   1493  1.1  haad 	}
   1494  1.1  haad 
   1495  1.1  haad 	if (log_count && !attach_mirror_log(first_seg(lv), log_lv))
   1496  1.1  haad 		stack;
   1497  1.1  haad 
   1498  1.1  haad 	alloc_destroy(ah);
   1499  1.1  haad 	return 1;
   1500  1.1  haad 
   1501  1.1  haad   out_remove_log:
   1502  1.1  haad 	if (log_lv && (!lv_remove(log_lv) || !vg_write(log_lv->vg) ||
   1503  1.1  haad 		       (backup(log_lv->vg), !vg_commit(log_lv->vg))))
   1504  1.1  haad 		log_error("Manual intervention may be required to remove "
   1505  1.1  haad 			  "abandoned log LV before retrying.");
   1506  1.1  haad 
   1507  1.1  haad   out_remove_imgs:
   1508  1.1  haad 	return 0;
   1509  1.1  haad }
   1510  1.1  haad 
   1511  1.1  haad /*
   1512  1.1  haad  * Generic interface for adding mirror and/or mirror log.
   1513  1.1  haad  * 'mirror' is the number of mirrors to be added.
   1514  1.1  haad  * 'pvs' is either allocatable pvs.
   1515  1.1  haad  */
   1516  1.1  haad int lv_add_mirrors(struct cmd_context *cmd, struct logical_volume *lv,
   1517  1.1  haad 		   uint32_t mirrors, uint32_t stripes,
   1518  1.1  haad 		   uint32_t region_size, uint32_t log_count,
   1519  1.1  haad 		   struct dm_list *pvs, alloc_policy_t alloc, uint32_t flags)
   1520  1.1  haad {
   1521  1.1  haad 	if (!mirrors && !log_count) {
   1522  1.1  haad 		log_error("No conversion is requested");
   1523  1.1  haad 		return 0;
   1524  1.1  haad 	}
   1525  1.1  haad 
   1526  1.1  haad 	/* For corelog mirror, activation code depends on
   1527  1.1  haad 	 * the global mirror_in_sync status. As we are adding
   1528  1.1  haad 	 * a new mirror, it should be set as 'out-of-sync'
   1529  1.1  haad 	 * so that the sync starts. */
   1530  1.1  haad 	/* However, MIRROR_SKIP_INIT_SYNC even overrides it. */
   1531  1.1  haad 	if (flags & MIRROR_SKIP_INIT_SYNC)
   1532  1.1  haad 		init_mirror_in_sync(1);
   1533  1.1  haad 	else if (!log_count)
   1534  1.1  haad 		init_mirror_in_sync(0);
   1535  1.1  haad 
   1536  1.1  haad 	if (flags & MIRROR_BY_SEG) {
   1537  1.1  haad 		if (log_count) {
   1538  1.1  haad 			log_error("Persistent log is not supported on "
   1539  1.1  haad 				  "segment-by-segment mirroring");
   1540  1.1  haad 			return 0;
   1541  1.1  haad 		}
   1542  1.1  haad 		if (stripes > 1) {
   1543  1.1  haad 			log_error("Striped-mirroring is not supported on "
   1544  1.1  haad 				  "segment-by-segment mirroring");
   1545  1.1  haad 			return 0;
   1546  1.1  haad 		}
   1547  1.1  haad 
   1548  1.1  haad 		return add_mirrors_to_segments(cmd, lv, mirrors,
   1549  1.1  haad 					       region_size, pvs, alloc);
   1550  1.1  haad 	} else if (flags & MIRROR_BY_LV) {
   1551  1.1  haad 		if (!mirrors)
   1552  1.1  haad 			return add_mirror_log(cmd, lv, log_count,
   1553  1.1  haad 					      region_size, pvs, alloc);
   1554  1.1  haad 		return add_mirror_images(cmd, lv, mirrors,
   1555  1.1  haad 					 stripes, region_size,
   1556  1.1  haad 					 pvs, alloc, log_count);
   1557  1.1  haad 	}
   1558  1.1  haad 
   1559  1.1  haad 	log_error("Unsupported mirror conversion type");
   1560  1.1  haad 	return 0;
   1561  1.1  haad }
   1562  1.1  haad 
   1563  1.1  haad /*
   1564  1.1  haad  * Generic interface for removing mirror and/or mirror log.
   1565  1.1  haad  * 'mirror' is the number of mirrors to be removed.
   1566  1.1  haad  * 'pvs' is removable pvs.
   1567  1.1  haad  */
   1568  1.1  haad int lv_remove_mirrors(struct cmd_context *cmd __attribute((unused)),
   1569  1.1  haad 		      struct logical_volume *lv,
   1570  1.1  haad 		      uint32_t mirrors, uint32_t log_count, struct dm_list *pvs,
   1571  1.1  haad 		      uint32_t status_mask)
   1572  1.1  haad {
   1573  1.1  haad 	uint32_t new_mirrors;
   1574  1.1  haad 	struct lv_segment *seg;
   1575  1.1  haad 
   1576  1.1  haad 	if (!mirrors && !log_count) {
   1577  1.1  haad 		log_error("No conversion is requested");
   1578  1.1  haad 		return 0;
   1579  1.1  haad 	}
   1580  1.1  haad 
   1581  1.1  haad 	seg = first_seg(lv);
   1582  1.1  haad 	if (!seg_is_mirrored(seg)) {
   1583  1.1  haad 		log_error("Not a mirror segment");
   1584  1.1  haad 		return 0;
   1585  1.1  haad 	}
   1586  1.1  haad 
   1587  1.1  haad 	if (lv_mirror_count(lv) <= mirrors) {
   1588  1.1  haad 		log_error("Removing more than existing: %d <= %d",
   1589  1.1  haad 			  seg->area_count, mirrors);
   1590  1.1  haad 		return 0;
   1591  1.1  haad 	}
   1592  1.1  haad 	new_mirrors = lv_mirror_count(lv) - mirrors - 1;
   1593  1.1  haad 
   1594  1.1  haad 	/* MIRROR_BY_LV */
   1595  1.1  haad 	if (seg_type(seg, 0) == AREA_LV &&
   1596  1.1  haad 	    seg_lv(seg, 0)->status & MIRROR_IMAGE)
   1597  1.1  haad 		return remove_mirror_images(lv, new_mirrors + 1,
   1598  1.1  haad 					    pvs, log_count ? 1U : 0);
   1599  1.1  haad 
   1600  1.1  haad 	/* MIRROR_BY_SEG */
   1601  1.1  haad 	if (log_count) {
   1602  1.1  haad 		log_error("Persistent log is not supported on "
   1603  1.1  haad 			  "segment-by-segment mirroring");
   1604  1.1  haad 		return 0;
   1605  1.1  haad 	}
   1606  1.1  haad 	return remove_mirrors_from_segments(lv, new_mirrors, status_mask);
   1607  1.1  haad }
   1608  1.1  haad 
   1609