summaryrefslogtreecommitdiffstats
path: root/lib/format_pool/import_export.c
blob: f7f619e844bb20180068e03b3f45d526b2f8bcbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 1997-2004 Sistina Software, Inc. All rights reserved.
 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"
#include "label.h"
#include "metadata.h"
#include "lvmcache.h"
#include "disk_rep.h"
#include "sptype_names.h"
#include "lv_alloc.h"
#include "pv_alloc.h"
#include "str_list.h"
#include "display.h"
#include "segtype.h"
#include "toolcontext.h"

/* This file contains only imports at the moment... */

int import_pool_vg(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
{
	struct pool_list *pl;

	dm_list_iterate_items(pl, pls) {
		vg->extent_count +=
		    ((pl->pd.pl_blocks) / POOL_PE_SIZE);

		vg->free_count = vg->extent_count;

		if (vg->name)
			continue;

		vg->name = dm_pool_strdup(mem, pl->pd.pl_pool_name);
		get_pool_vg_uuid(&vg->id, &pl->pd);
		vg->extent_size = POOL_PE_SIZE;
		vg->status |= LVM_READ | LVM_WRITE | CLUSTERED | SHARED;
		vg->max_lv = 1;
		vg->max_pv = POOL_MAX_DEVICES;
		vg->alloc = ALLOC_NORMAL;
	}

	return 1;
}

int import_pool_lvs(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
{
	struct pool_list *pl;
	struct logical_volume *lv;

	if (!(lv = alloc_lv(mem)))
		return_0;

	lv->status = 0;
	lv->alloc = ALLOC_NORMAL;
	lv->size = 0;
	lv->name = NULL;
	lv->le_count = 0;
	lv->read_ahead = vg->cmd->default_settings.read_ahead;

	dm_list_iterate_items(pl, pls) {
		lv->size += pl->pd.pl_blocks;

		if (lv->name)
			continue;

		if (!(lv->name = dm_pool_strdup(mem, pl->pd.pl_pool_name)))
			return_0;

		get_pool_lv_uuid(lv->lvid.id, &pl->pd);
		log_debug("Calculated lv uuid for lv %s: %s", lv->name,
			  lv->lvid.s);

		lv->status |= VISIBLE_LV | LVM_READ | LVM_WRITE;
		lv->major = POOL_MAJOR;

		/* for pool a minor of 0 is dynamic */
		if (pl->pd.pl_minor) {
			lv->status |= FIXED_MINOR;
			lv->minor = pl->pd.pl_minor + MINOR_OFFSET;
		} else {
			lv->minor = -1;
		}
	}

	lv->le_count = lv->size / POOL_PE_SIZE;

	return link_lv_to_vg(vg, lv);
}

int import_pool_pvs(const struct format_type *fmt, struct volume_group *vg,
		    struct dm_pool *mem, struct dm_list *pls)
{
	struct pv_list *pvl;
	struct pool_list *pl;

	dm_list_iterate_items(pl, pls) {
		if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
			log_error("Unable to allocate pv list structure");
			return 0;
		}
		if (!(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv)))) {
			log_error("Unable to allocate pv structure");
			return 0;
		}
		if (!import_pool_pv(fmt, mem, vg, pvl->pv, pl)) {
			return 0;
		}
		pl->pv = pvl->pv;
		pvl->mdas = NULL;
		pvl->pe_ranges = NULL;
		add_pvl_to_vgs(vg, pvl);
	}

	return 1;
}

int import_pool_pv(const struct format_type *fmt, struct dm_pool *mem,
		   struct volume_group *vg, struct physical_volume *pv,
		   struct pool_list *pl)
{
	struct pool_disk *pd = &pl->pd;

	memset(pv, 0, sizeof(*pv));

	get_pool_pv_uuid(&pv->id, pd);
	pv->fmt = fmt;

	pv->dev = pl->dev;
	if (!(pv->vg_name = dm_pool_strdup(mem, pd->pl_pool_name))) {
		log_error("Unable to duplicate vg_name string");
		return 0;
	}
	if (vg != NULL)
		memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
	pv->status = 0;
	pv->size = pd->pl_blocks;
	pv->pe_size = POOL_PE_SIZE;
	pv->pe_start = POOL_PE_START;
	pv->pe_count = pv->size / POOL_PE_SIZE;
	pv->pe_alloc_count = 0;
	pv->pe_align = 0;

	dm_list_init(&pv->tags);
	dm_list_init(&pv->segments);

	if (!alloc_pv_segment_whole_pv(mem, pv))
		return_0;

	return 1;
}

static const char *_cvt_sptype(uint32_t sptype)
{
	int i;
	for (i = 0; sptype_names[i].name[0]; i++) {
		if (sptype == sptype_names[i].label) {
			break;
		}
	}
	log_debug("Found sptype %X and converted it to %s",
		  sptype, sptype_names[i].name);
	return sptype_names[i].name;
}

static int _add_stripe_seg(struct dm_pool *mem,
			   struct user_subpool *usp, struct logical_volume *lv,
			   uint32_t *le_cur)
{
	struct lv_segment *seg;
	struct segment_type *segtype;
	unsigned j;
	uint32_t area_len;

	if (usp->striping & (usp->striping - 1)) {
		log_error("Stripe size must be a power of 2");
		return 0;
	}

	area_len = (usp->devs[0].blocks) / POOL_PE_SIZE;

	if (!(segtype = get_segtype_from_string(lv->vg->cmd,
						     "striped")))
		return_0;

	if (!(seg = alloc_lv_segment(segtype, lv, *le_cur,
				     area_len * usp->num_devs, 0,
				     usp->striping, NULL, NULL, usp->num_devs,
				     area_len, 0, 0, 0, NULL))) {
		log_error("Unable to allocate striped lv_segment structure");
		return 0;
	}

	for (j = 0; j < usp->num_devs; j++)
		if (!set_lv_segment_area_pv(seg, j, usp->devs[j].pv, 0))
			return_0;

	/* add the subpool type to the segment tag list */
	if (!str_list_add(mem, &seg->tags, _cvt_sptype(usp->type))) {
		log_error("Allocation failed for str_list.");
		return 0;
	}

	dm_list_add(&lv->segments, &seg->list);

	*le_cur += seg->len;

	return 1;
}

static int _add_linear_seg(struct dm_pool *mem,
			   struct user_subpool *usp, struct logical_volume *lv,
			   uint32_t *le_cur)
{
	struct lv_segment *seg;
	struct segment_type *segtype;
	unsigned j;
	uint32_t area_len;

	if (!(segtype = get_segtype_from_string(lv->vg->cmd, "striped")))
		return_0;

	for (j = 0; j < usp->num_devs; j++) {
		area_len = (usp->devs[j].blocks) / POOL_PE_SIZE;

		if (!(seg = alloc_lv_segment(segtype, lv, *le_cur,
					     area_len, 0, usp->striping,
					     NULL, NULL, 1, area_len,
					     POOL_PE_SIZE, 0, 0, NULL))) {
			log_error("Unable to allocate linear lv_segment "
				  "structure");
			return 0;
		}

		/* add the subpool type to the segment tag list */
		if (!str_list_add(mem, &seg->tags, _cvt_sptype(usp->type))) {
			log_error("Allocation failed for str_list.");
			return 0;
		}

		if (!set_lv_segment_area_pv(seg, 0, usp->devs[j].pv, 0))
			return_0;
		dm_list_add(&lv->segments, &seg->list);

		*le_cur += seg->len;
	}

	return 1;
}

int import_pool_segments(struct dm_list *lvs, struct dm_pool *mem,
			 struct user_subpool *usp, int subpools)
{
	struct lv_list *lvl;
	struct logical_volume *lv;
	uint32_t le_cur = 0;
	int i;

	dm_list_iterate_items(lvl, lvs) {
		lv = lvl->lv;

		if (lv->status & SNAPSHOT)
			continue;

		for (i = 0; i < subpools; i++) {
			if (usp[i].striping) {
				if (!_add_stripe_seg(mem, &usp[i], lv, &le_cur))
					return_0;
			} else {
				if (!_add_linear_seg(mem, &usp[i], lv, &le_cur))
					return_0;
			}
		}
	}

	return 1;
}