1 /* $NetBSD: import_export.c,v 1.1.1.2 2009/12/02 00:26:50 haad Exp $ */
4 * Copyright (C) 1997-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
9 * This copyrighted material is made available to anyone wishing to use,
10 * modify, copy, or redistribute it subject to the terms and conditions
11 * of the GNU Lesser General Public License v.2.1.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "sptype_names.h"
29 #include "toolcontext.h"
31 /* This file contains only imports at the moment... */
33 int import_pool_vg(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
37 dm_list_iterate_items(pl, pls) {
39 ((pl->pd.pl_blocks) / POOL_PE_SIZE);
41 vg->free_count = vg->extent_count;
47 vg->name = dm_pool_strdup(mem, pl->pd.pl_pool_name);
48 get_pool_vg_uuid(&vg->id, &pl->pd);
49 vg->extent_size = POOL_PE_SIZE;
50 vg->status |= LVM_READ | LVM_WRITE | CLUSTERED | SHARED;
52 vg->max_pv = POOL_MAX_DEVICES;
53 vg->alloc = ALLOC_NORMAL;
59 int import_pool_lvs(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
62 struct logical_volume *lv;
64 if (!(lv = alloc_lv(mem)))
68 lv->alloc = ALLOC_NORMAL;
72 lv->read_ahead = vg->cmd->default_settings.read_ahead;
74 dm_list_iterate_items(pl, pls) {
75 lv->size += pl->pd.pl_blocks;
80 if (!(lv->name = dm_pool_strdup(mem, pl->pd.pl_pool_name)))
83 get_pool_lv_uuid(lv->lvid.id, &pl->pd);
84 log_debug("Calculated lv uuid for lv %s: %s", lv->name,
87 lv->status |= VISIBLE_LV | LVM_READ | LVM_WRITE;
88 lv->major = POOL_MAJOR;
90 /* for pool a minor of 0 is dynamic */
91 if (pl->pd.pl_minor) {
92 lv->status |= FIXED_MINOR;
93 lv->minor = pl->pd.pl_minor + MINOR_OFFSET;
99 lv->le_count = lv->size / POOL_PE_SIZE;
101 return link_lv_to_vg(vg, lv);
104 int import_pool_pvs(const struct format_type *fmt, struct volume_group *vg,
105 struct dm_list *pvs, struct dm_pool *mem, struct dm_list *pls)
108 struct pool_list *pl;
110 dm_list_iterate_items(pl, pls) {
111 if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
112 log_error("Unable to allocate pv list structure");
115 if (!(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv)))) {
116 log_error("Unable to allocate pv structure");
119 if (!import_pool_pv(fmt, mem, vg, pvl->pv, pl)) {
124 pvl->pe_ranges = NULL;
125 dm_list_add(pvs, &pvl->list);
131 int import_pool_pv(const struct format_type *fmt, struct dm_pool *mem,
132 struct volume_group *vg, struct physical_volume *pv,
133 struct pool_list *pl)
135 struct pool_disk *pd = &pl->pd;
137 memset(pv, 0, sizeof(*pv));
139 get_pool_pv_uuid(&pv->id, pd);
143 if (!(pv->vg_name = dm_pool_strdup(mem, pd->pl_pool_name))) {
144 log_error("Unable to duplicate vg_name string");
148 memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
150 pv->size = pd->pl_blocks;
151 pv->pe_size = POOL_PE_SIZE;
152 pv->pe_start = POOL_PE_START;
153 pv->pe_count = pv->size / POOL_PE_SIZE;
154 pv->pe_alloc_count = 0;
157 dm_list_init(&pv->tags);
158 dm_list_init(&pv->segments);
160 if (!alloc_pv_segment_whole_pv(mem, pv))
166 static const char *_cvt_sptype(uint32_t sptype)
169 for (i = 0; sptype_names[i].name[0]; i++) {
170 if (sptype == sptype_names[i].label) {
174 log_debug("Found sptype %X and converted it to %s",
175 sptype, sptype_names[i].name);
176 return sptype_names[i].name;
179 static int _add_stripe_seg(struct dm_pool *mem,
180 struct user_subpool *usp, struct logical_volume *lv,
183 struct lv_segment *seg;
184 struct segment_type *segtype;
188 if (usp->striping & (usp->striping - 1)) {
189 log_error("Stripe size must be a power of 2");
193 area_len = (usp->devs[0].blocks) / POOL_PE_SIZE;
195 if (!(segtype = get_segtype_from_string(lv->vg->cmd,
199 if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
200 area_len * usp->num_devs, 0,
201 usp->striping, NULL, usp->num_devs,
202 area_len, 0, 0, 0))) {
203 log_error("Unable to allocate striped lv_segment structure");
207 for (j = 0; j < usp->num_devs; j++)
208 if (!set_lv_segment_area_pv(seg, j, usp->devs[j].pv, 0))
211 /* add the subpool type to the segment tag list */
212 str_list_add(mem, &seg->tags, _cvt_sptype(usp->type));
214 dm_list_add(&lv->segments, &seg->list);
221 static int _add_linear_seg(struct dm_pool *mem,
222 struct user_subpool *usp, struct logical_volume *lv,
225 struct lv_segment *seg;
226 struct segment_type *segtype;
230 if (!(segtype = get_segtype_from_string(lv->vg->cmd, "striped")))
233 for (j = 0; j < usp->num_devs; j++) {
234 area_len = (usp->devs[j].blocks) / POOL_PE_SIZE;
236 if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
237 area_len, 0, usp->striping,
239 POOL_PE_SIZE, 0, 0))) {
240 log_error("Unable to allocate linear lv_segment "
245 /* add the subpool type to the segment tag list */
246 str_list_add(mem, &seg->tags, _cvt_sptype(usp->type));
248 if (!set_lv_segment_area_pv(seg, 0, usp->devs[j].pv, 0))
250 dm_list_add(&lv->segments, &seg->list);
258 int import_pool_segments(struct dm_list *lvs, struct dm_pool *mem,
259 struct user_subpool *usp, int subpools)
262 struct logical_volume *lv;
266 dm_list_iterate_items(lvl, lvs) {
269 if (lv->status & SNAPSHOT)
272 for (i = 0; i < subpools; i++) {
273 if (usp[i].striping) {
274 if (!_add_stripe_seg(mem, &usp[i], lv, &le_cur))
277 if (!_add_linear_seg(mem, &usp[i], lv, &le_cur))