xref: /netbsd-src/external/gpl2/lvm2/dist/lib/format1/import-extents.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: import-extents.c,v 1.1.1.2 2009/12/02 00:26:49 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "metadata.h"
2056a34939Shaad #include "disk-rep.h"
2156a34939Shaad #include "lv_alloc.h"
2256a34939Shaad #include "display.h"
2356a34939Shaad #include "segtype.h"
2456a34939Shaad 
2556a34939Shaad /*
2656a34939Shaad  * After much thought I have decided it is easier,
2756a34939Shaad  * and probably no less efficient, to convert the
2856a34939Shaad  * pe->le map to a full le->pe map, and then
2956a34939Shaad  * process this to get the segments form that
3056a34939Shaad  * we're after.  Any code which goes directly from
3156a34939Shaad  * the pe->le map to segments would be gladly
3256a34939Shaad  * accepted, if it is less complicated than this
3356a34939Shaad  * file.
3456a34939Shaad  */
3556a34939Shaad struct pe_specifier {
3656a34939Shaad 	struct physical_volume *pv;
3756a34939Shaad 	uint32_t pe;
3856a34939Shaad };
3956a34939Shaad 
4056a34939Shaad struct lv_map {
4156a34939Shaad 	struct logical_volume *lv;
4256a34939Shaad 	uint32_t stripes;
4356a34939Shaad 	uint32_t stripe_size;
4456a34939Shaad 	struct pe_specifier *map;
4556a34939Shaad };
4656a34939Shaad 
_create_lv_maps(struct dm_pool * mem,struct volume_group * vg)4756a34939Shaad static struct dm_hash_table *_create_lv_maps(struct dm_pool *mem,
4856a34939Shaad 					  struct volume_group *vg)
4956a34939Shaad {
5056a34939Shaad 	struct dm_hash_table *maps = dm_hash_create(32);
5156a34939Shaad 	struct lv_list *ll;
5256a34939Shaad 	struct lv_map *lvm;
5356a34939Shaad 
5456a34939Shaad 	if (!maps) {
55*7c604eeaShaad 		log_error("Unable to create hash table for holding "
5656a34939Shaad 			  "extent maps.");
5756a34939Shaad 		return NULL;
5856a34939Shaad 	}
5956a34939Shaad 
6056a34939Shaad 	dm_list_iterate_items(ll, &vg->lvs) {
6156a34939Shaad 		if (ll->lv->status & SNAPSHOT)
6256a34939Shaad 			continue;
6356a34939Shaad 
6456a34939Shaad 		if (!(lvm = dm_pool_alloc(mem, sizeof(*lvm))))
6556a34939Shaad 			goto_bad;
6656a34939Shaad 
6756a34939Shaad 		lvm->lv = ll->lv;
6856a34939Shaad 		if (!(lvm->map = dm_pool_zalloc(mem, sizeof(*lvm->map)
6956a34939Shaad 					     * ll->lv->le_count)))
7056a34939Shaad 			goto_bad;
7156a34939Shaad 
7256a34939Shaad 		if (!dm_hash_insert(maps, ll->lv->name, lvm))
7356a34939Shaad 			goto_bad;
7456a34939Shaad 	}
7556a34939Shaad 
7656a34939Shaad 	return maps;
7756a34939Shaad 
7856a34939Shaad       bad:
7956a34939Shaad 	dm_hash_destroy(maps);
8056a34939Shaad 	return NULL;
8156a34939Shaad }
8256a34939Shaad 
_fill_lv_array(struct lv_map ** lvs,struct dm_hash_table * maps,struct disk_list * dl)8356a34939Shaad static int _fill_lv_array(struct lv_map **lvs,
8456a34939Shaad 			  struct dm_hash_table *maps, struct disk_list *dl)
8556a34939Shaad {
8656a34939Shaad 	struct lvd_list *ll;
8756a34939Shaad 	struct lv_map *lvm;
8856a34939Shaad 
8956a34939Shaad 	memset(lvs, 0, sizeof(*lvs) * MAX_LV);
9056a34939Shaad 
9156a34939Shaad 	dm_list_iterate_items(ll, &dl->lvds) {
9256a34939Shaad 		if (!(lvm = dm_hash_lookup(maps, strrchr((char *)ll->lvd.lv_name, '/')
9356a34939Shaad 					+ 1))) {
94*7c604eeaShaad 			log_error("Physical volume (%s) contains an "
9556a34939Shaad 				  "unknown logical volume (%s).",
9656a34939Shaad 				dev_name(dl->dev), ll->lvd.lv_name);
9756a34939Shaad 			return 0;
9856a34939Shaad 		}
9956a34939Shaad 
10056a34939Shaad 		lvm->stripes = ll->lvd.lv_stripes;
10156a34939Shaad 		lvm->stripe_size = ll->lvd.lv_stripesize;
10256a34939Shaad 
10356a34939Shaad 		lvs[ll->lvd.lv_number] = lvm;
10456a34939Shaad 	}
10556a34939Shaad 
10656a34939Shaad 	return 1;
10756a34939Shaad }
10856a34939Shaad 
_fill_maps(struct dm_hash_table * maps,struct volume_group * vg,struct dm_list * pvds)10956a34939Shaad static int _fill_maps(struct dm_hash_table *maps, struct volume_group *vg,
11056a34939Shaad 		      struct dm_list *pvds)
11156a34939Shaad {
11256a34939Shaad 	struct disk_list *dl;
11356a34939Shaad 	struct physical_volume *pv;
11456a34939Shaad 	struct lv_map *lvms[MAX_LV], *lvm;
11556a34939Shaad 	struct pe_disk *e;
11656a34939Shaad 	uint32_t i, lv_num, le;
11756a34939Shaad 
11856a34939Shaad 	dm_list_iterate_items(dl, pvds) {
11956a34939Shaad 		pv = find_pv(vg, dl->dev);
12056a34939Shaad 		e = dl->extents;
12156a34939Shaad 
12256a34939Shaad 		/* build an array of lv's for this pv */
12356a34939Shaad 		if (!_fill_lv_array(lvms, maps, dl))
12456a34939Shaad 			return_0;
12556a34939Shaad 
12656a34939Shaad 		for (i = 0; i < dl->pvd.pe_total; i++) {
12756a34939Shaad 			lv_num = e[i].lv_num;
12856a34939Shaad 
12956a34939Shaad 			if (lv_num == UNMAPPED_EXTENT)
13056a34939Shaad 				continue;
13156a34939Shaad 
13256a34939Shaad 			else {
13356a34939Shaad 				lv_num--;
13456a34939Shaad 				lvm = lvms[lv_num];
13556a34939Shaad 
13656a34939Shaad 				if (!lvm) {
13756a34939Shaad 					log_error("Invalid LV in extent map "
13856a34939Shaad 						  "(PV %s, PE %" PRIu32
13956a34939Shaad 						  ", LV %" PRIu32
14056a34939Shaad 						  ", LE %" PRIu32 ")",
14156a34939Shaad 						  dev_name(pv->dev), i,
14256a34939Shaad 						  lv_num, e[i].le_num);
14356a34939Shaad 					return 0;
14456a34939Shaad 				}
14556a34939Shaad 
14656a34939Shaad 				le = e[i].le_num;
14756a34939Shaad 
14856a34939Shaad 				if (le >= lvm->lv->le_count) {
149*7c604eeaShaad 					log_error("logical extent number "
15056a34939Shaad 						  "out of bounds");
15156a34939Shaad 					return 0;
15256a34939Shaad 				}
15356a34939Shaad 
15456a34939Shaad 				if (lvm->map[le].pv) {
155*7c604eeaShaad 					log_error("logical extent (%u) "
15656a34939Shaad 						  "already mapped.", le);
15756a34939Shaad 					return 0;
15856a34939Shaad 				}
15956a34939Shaad 
16056a34939Shaad 				lvm->map[le].pv = pv;
16156a34939Shaad 				lvm->map[le].pe = i;
16256a34939Shaad 			}
16356a34939Shaad 		}
16456a34939Shaad 	}
16556a34939Shaad 
16656a34939Shaad 	return 1;
16756a34939Shaad }
16856a34939Shaad 
_check_single_map(struct lv_map * lvm)16956a34939Shaad static int _check_single_map(struct lv_map *lvm)
17056a34939Shaad {
17156a34939Shaad 	uint32_t i;
17256a34939Shaad 
17356a34939Shaad 	for (i = 0; i < lvm->lv->le_count; i++) {
17456a34939Shaad 		if (!lvm->map[i].pv) {
175*7c604eeaShaad 			log_error("Logical volume (%s) contains an incomplete "
17656a34939Shaad 				  "mapping table.", lvm->lv->name);
17756a34939Shaad 			return 0;
17856a34939Shaad 		}
17956a34939Shaad 	}
18056a34939Shaad 
18156a34939Shaad 	return 1;
18256a34939Shaad }
18356a34939Shaad 
_check_maps_are_complete(struct dm_hash_table * maps)18456a34939Shaad static int _check_maps_are_complete(struct dm_hash_table *maps)
18556a34939Shaad {
18656a34939Shaad 	struct dm_hash_node *n;
18756a34939Shaad 	struct lv_map *lvm;
18856a34939Shaad 
18956a34939Shaad 	for (n = dm_hash_get_first(maps); n; n = dm_hash_get_next(maps, n)) {
19056a34939Shaad 		lvm = (struct lv_map *) dm_hash_get_data(maps, n);
19156a34939Shaad 
19256a34939Shaad 		if (!_check_single_map(lvm))
19356a34939Shaad 			return_0;
19456a34939Shaad 	}
19556a34939Shaad 	return 1;
19656a34939Shaad }
19756a34939Shaad 
_area_length(struct lv_map * lvm,uint32_t le)19856a34939Shaad static uint32_t _area_length(struct lv_map *lvm, uint32_t le)
19956a34939Shaad {
20056a34939Shaad 	uint32_t len = 0;
20156a34939Shaad 
20256a34939Shaad 	do
20356a34939Shaad 		len++;
20456a34939Shaad 	while ((lvm->map[le + len].pv == lvm->map[le].pv) &&
20556a34939Shaad 		 (lvm->map[le].pv &&
20656a34939Shaad 		  lvm->map[le + len].pe == lvm->map[le].pe + len));
20756a34939Shaad 
20856a34939Shaad 	return len;
20956a34939Shaad }
21056a34939Shaad 
_read_linear(struct cmd_context * cmd,struct lv_map * lvm)21156a34939Shaad static int _read_linear(struct cmd_context *cmd, struct lv_map *lvm)
21256a34939Shaad {
21356a34939Shaad 	uint32_t le = 0, len;
21456a34939Shaad 	struct lv_segment *seg;
21556a34939Shaad 	struct segment_type *segtype;
21656a34939Shaad 
21756a34939Shaad 	if (!(segtype = get_segtype_from_string(cmd, "striped")))
21856a34939Shaad 		return_0;
21956a34939Shaad 
22056a34939Shaad 	while (le < lvm->lv->le_count) {
22156a34939Shaad 		len = _area_length(lvm, le);
22256a34939Shaad 
22356a34939Shaad 		if (!(seg = alloc_lv_segment(cmd->mem, segtype, lvm->lv, le,
22456a34939Shaad 					     len, 0, 0, NULL, 1, len, 0, 0, 0))) {
22556a34939Shaad 			log_error("Failed to allocate linear segment.");
22656a34939Shaad 			return 0;
22756a34939Shaad 		}
22856a34939Shaad 
22956a34939Shaad 		if (!set_lv_segment_area_pv(seg, 0, lvm->map[le].pv,
23056a34939Shaad 					    lvm->map[le].pe))
23156a34939Shaad 			return_0;
23256a34939Shaad 
23356a34939Shaad 		dm_list_add(&lvm->lv->segments, &seg->list);
23456a34939Shaad 
23556a34939Shaad 		le += seg->len;
23656a34939Shaad 	}
23756a34939Shaad 
23856a34939Shaad 	return 1;
23956a34939Shaad }
24056a34939Shaad 
_check_stripe(struct lv_map * lvm,uint32_t area_count,uint32_t area_len,uint32_t base_le,uint32_t total_area_len)24156a34939Shaad static int _check_stripe(struct lv_map *lvm, uint32_t area_count,
24256a34939Shaad 			 uint32_t area_len, uint32_t base_le,
24356a34939Shaad 			 uint32_t total_area_len)
24456a34939Shaad {
24556a34939Shaad 	uint32_t st;
24656a34939Shaad 
24756a34939Shaad 	/*
24856a34939Shaad 	 * Is the next physical extent in every stripe adjacent to the last?
24956a34939Shaad 	 */
25056a34939Shaad 	for (st = 0; st < area_count; st++)
25156a34939Shaad 		if ((lvm->map[base_le + st * total_area_len + area_len].pv !=
25256a34939Shaad 		     lvm->map[base_le + st * total_area_len].pv) ||
25356a34939Shaad 		    (lvm->map[base_le + st * total_area_len].pv &&
25456a34939Shaad 		     lvm->map[base_le + st * total_area_len + area_len].pe !=
25556a34939Shaad 		     lvm->map[base_le + st * total_area_len].pe + area_len))
25656a34939Shaad 			return 0;
25756a34939Shaad 
25856a34939Shaad 	return 1;
25956a34939Shaad }
26056a34939Shaad 
_read_stripes(struct cmd_context * cmd,struct lv_map * lvm)26156a34939Shaad static int _read_stripes(struct cmd_context *cmd, struct lv_map *lvm)
26256a34939Shaad {
26356a34939Shaad 	uint32_t st, first_area_le = 0, total_area_len;
26456a34939Shaad 	uint32_t area_len;
26556a34939Shaad 	struct lv_segment *seg;
26656a34939Shaad 	struct segment_type *segtype;
26756a34939Shaad 
26856a34939Shaad 	/*
26956a34939Shaad 	 * Work out overall striped length
27056a34939Shaad 	 */
27156a34939Shaad 	if (lvm->lv->le_count % lvm->stripes) {
27256a34939Shaad 		log_error("Number of stripes (%u) incompatible "
27356a34939Shaad 			  "with logical extent count (%u) for %s",
27456a34939Shaad 			  lvm->stripes, lvm->lv->le_count, lvm->lv->name);
27556a34939Shaad 	}
27656a34939Shaad 
27756a34939Shaad 	total_area_len = lvm->lv->le_count / lvm->stripes;
27856a34939Shaad 
27956a34939Shaad 	if (!(segtype = get_segtype_from_string(cmd, "striped")))
28056a34939Shaad 		return_0;
28156a34939Shaad 
28256a34939Shaad 	while (first_area_le < total_area_len) {
28356a34939Shaad 		area_len = 1;
28456a34939Shaad 
28556a34939Shaad 		/*
28656a34939Shaad 		 * Find how many extents are contiguous in all stripes
28756a34939Shaad 		 * and so can form part of this segment
28856a34939Shaad 		 */
28956a34939Shaad 		while (_check_stripe(lvm, lvm->stripes,
29056a34939Shaad 				     area_len, first_area_le, total_area_len))
29156a34939Shaad 			area_len++;
29256a34939Shaad 
29356a34939Shaad 		if (!(seg = alloc_lv_segment(cmd->mem, segtype, lvm->lv,
29456a34939Shaad 					     lvm->stripes * first_area_le,
29556a34939Shaad 					     lvm->stripes * area_len,
29656a34939Shaad 					     0, lvm->stripe_size, NULL,
29756a34939Shaad 					     lvm->stripes,
29856a34939Shaad 					     area_len, 0, 0, 0))) {
29956a34939Shaad 			log_error("Failed to allocate striped segment.");
30056a34939Shaad 			return 0;
30156a34939Shaad 		}
30256a34939Shaad 
30356a34939Shaad 		/*
30456a34939Shaad 		 * Set up start positions of each stripe in this segment
30556a34939Shaad 		 */
30656a34939Shaad 		for (st = 0; st < seg->area_count; st++)
30756a34939Shaad 			if (!set_lv_segment_area_pv(seg, st,
30856a34939Shaad 			      lvm->map[first_area_le + st * total_area_len].pv,
30956a34939Shaad 			      lvm->map[first_area_le + st * total_area_len].pe))
31056a34939Shaad 				return_0;
31156a34939Shaad 
31256a34939Shaad 		dm_list_add(&lvm->lv->segments, &seg->list);
31356a34939Shaad 
31456a34939Shaad 		first_area_le += area_len;
31556a34939Shaad 	}
31656a34939Shaad 
31756a34939Shaad 	return 1;
31856a34939Shaad }
31956a34939Shaad 
_build_segments(struct cmd_context * cmd,struct lv_map * lvm)32056a34939Shaad static int _build_segments(struct cmd_context *cmd, struct lv_map *lvm)
32156a34939Shaad {
32256a34939Shaad 	return (lvm->stripes > 1 ? _read_stripes(cmd, lvm) :
32356a34939Shaad 		_read_linear(cmd, lvm));
32456a34939Shaad }
32556a34939Shaad 
_build_all_segments(struct cmd_context * cmd,struct dm_hash_table * maps)32656a34939Shaad static int _build_all_segments(struct cmd_context *cmd, struct dm_hash_table *maps)
32756a34939Shaad {
32856a34939Shaad 	struct dm_hash_node *n;
32956a34939Shaad 	struct lv_map *lvm;
33056a34939Shaad 
33156a34939Shaad 	for (n = dm_hash_get_first(maps); n; n = dm_hash_get_next(maps, n)) {
33256a34939Shaad 		lvm = (struct lv_map *) dm_hash_get_data(maps, n);
33356a34939Shaad 		if (!_build_segments(cmd, lvm))
33456a34939Shaad 			return_0;
33556a34939Shaad 	}
33656a34939Shaad 
33756a34939Shaad 	return 1;
33856a34939Shaad }
33956a34939Shaad 
import_extents(struct cmd_context * cmd,struct volume_group * vg,struct dm_list * pvds)34056a34939Shaad int import_extents(struct cmd_context *cmd, struct volume_group *vg,
34156a34939Shaad 		   struct dm_list *pvds)
34256a34939Shaad {
34356a34939Shaad 	int r = 0;
34456a34939Shaad 	struct dm_pool *scratch = dm_pool_create("lvm1 import_extents", 10 * 1024);
34556a34939Shaad 	struct dm_hash_table *maps;
34656a34939Shaad 
34756a34939Shaad 	if (!scratch)
34856a34939Shaad 		return_0;
34956a34939Shaad 
35056a34939Shaad 	if (!(maps = _create_lv_maps(scratch, vg))) {
351*7c604eeaShaad 		log_error("Couldn't allocate logical volume maps.");
35256a34939Shaad 		goto out;
35356a34939Shaad 	}
35456a34939Shaad 
35556a34939Shaad 	if (!_fill_maps(maps, vg, pvds)) {
356*7c604eeaShaad 		log_error("Couldn't fill logical volume maps.");
35756a34939Shaad 		goto out;
35856a34939Shaad 	}
35956a34939Shaad 
36056a34939Shaad 	if (!_check_maps_are_complete(maps) && !(vg->status & PARTIAL_VG))
36156a34939Shaad 		goto_out;
36256a34939Shaad 
36356a34939Shaad 	if (!_build_all_segments(cmd, maps)) {
364*7c604eeaShaad 		log_error("Couldn't build extent segments.");
36556a34939Shaad 		goto out;
36656a34939Shaad 	}
36756a34939Shaad 	r = 1;
36856a34939Shaad 
36956a34939Shaad       out:
37056a34939Shaad 	if (maps)
37156a34939Shaad 		dm_hash_destroy(maps);
37256a34939Shaad 	dm_pool_destroy(scratch);
37356a34939Shaad 	return r;
37456a34939Shaad }
375