2 * Copyright (C) 1997-2004 Sistina Software, Inc. All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This file is part of LVM2.
7 * This copyrighted material is made available to anyone wishing to use,
8 * modify, copy, or redistribute it subject to the terms and conditions
9 * of the GNU Lesser General Public License v.2.1.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, write to the Free Software Foundation,
13 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "sptype_names.h"
27 #include "toolcontext.h"
29 /* This file contains only imports at the moment... */
31 int import_pool_vg(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
35 dm_list_iterate_items(pl, pls) {
37 ((pl->pd.pl_blocks) / POOL_PE_SIZE);
39 vg->free_count = vg->extent_count;
44 vg->name = dm_pool_strdup(mem, pl->pd.pl_pool_name);
45 get_pool_vg_uuid(&vg->id, &pl->pd);
46 vg->extent_size = POOL_PE_SIZE;
47 vg->status |= LVM_READ | LVM_WRITE | CLUSTERED | SHARED;
49 vg->max_pv = POOL_MAX_DEVICES;
50 vg->alloc = ALLOC_NORMAL;
56 int import_pool_lvs(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
59 struct logical_volume *lv;
61 if (!(lv = alloc_lv(mem)))
65 lv->alloc = ALLOC_NORMAL;
69 lv->read_ahead = vg->cmd->default_settings.read_ahead;
71 dm_list_iterate_items(pl, pls) {
72 lv->size += pl->pd.pl_blocks;
77 if (!(lv->name = dm_pool_strdup(mem, pl->pd.pl_pool_name)))
80 get_pool_lv_uuid(lv->lvid.id, &pl->pd);
81 log_debug("Calculated lv uuid for lv %s: %s", lv->name,
84 lv->status |= VISIBLE_LV | LVM_READ | LVM_WRITE;
85 lv->major = POOL_MAJOR;
87 /* for pool a minor of 0 is dynamic */
88 if (pl->pd.pl_minor) {
89 lv->status |= FIXED_MINOR;
90 lv->minor = pl->pd.pl_minor + MINOR_OFFSET;
96 lv->le_count = lv->size / POOL_PE_SIZE;
98 return link_lv_to_vg(vg, lv);
101 int import_pool_pvs(const struct format_type *fmt, struct volume_group *vg,
102 struct dm_pool *mem, struct dm_list *pls)
105 struct pool_list *pl;
107 dm_list_iterate_items(pl, pls) {
108 if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
109 log_error("Unable to allocate pv list structure");
112 if (!(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv)))) {
113 log_error("Unable to allocate pv structure");
116 if (!import_pool_pv(fmt, mem, vg, pvl->pv, pl)) {
121 pvl->pe_ranges = NULL;
122 add_pvl_to_vgs(vg, pvl);
128 int import_pool_pv(const struct format_type *fmt, struct dm_pool *mem,
129 struct volume_group *vg, struct physical_volume *pv,
130 struct pool_list *pl)
132 struct pool_disk *pd = &pl->pd;
134 memset(pv, 0, sizeof(*pv));
136 get_pool_pv_uuid(&pv->id, pd);
140 if (!(pv->vg_name = dm_pool_strdup(mem, pd->pl_pool_name))) {
141 log_error("Unable to duplicate vg_name string");
145 memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
147 pv->size = pd->pl_blocks;
148 pv->pe_size = POOL_PE_SIZE;
149 pv->pe_start = POOL_PE_START;
150 pv->pe_count = pv->size / POOL_PE_SIZE;
151 pv->pe_alloc_count = 0;
154 dm_list_init(&pv->tags);
155 dm_list_init(&pv->segments);
157 if (!alloc_pv_segment_whole_pv(mem, pv))
163 static const char *_cvt_sptype(uint32_t sptype)
166 for (i = 0; sptype_names[i].name[0]; i++) {
167 if (sptype == sptype_names[i].label) {
171 log_debug("Found sptype %X and converted it to %s",
172 sptype, sptype_names[i].name);
173 return sptype_names[i].name;
176 static int _add_stripe_seg(struct dm_pool *mem,
177 struct user_subpool *usp, struct logical_volume *lv,
180 struct lv_segment *seg;
181 struct segment_type *segtype;
185 if (usp->striping & (usp->striping - 1)) {
186 log_error("Stripe size must be a power of 2");
190 area_len = (usp->devs[0].blocks) / POOL_PE_SIZE;
192 if (!(segtype = get_segtype_from_string(lv->vg->cmd,
196 if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
197 area_len * usp->num_devs, 0,
198 usp->striping, NULL, usp->num_devs,
199 area_len, 0, 0, 0, NULL))) {
200 log_error("Unable to allocate striped lv_segment structure");
204 for (j = 0; j < usp->num_devs; j++)
205 if (!set_lv_segment_area_pv(seg, j, usp->devs[j].pv, 0))
208 /* add the subpool type to the segment tag list */
209 if (!str_list_add(mem, &seg->tags, _cvt_sptype(usp->type))) {
210 log_error("Allocation failed for str_list.");
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, NULL))) {
240 log_error("Unable to allocate linear lv_segment "
245 /* add the subpool type to the segment tag list */
246 if (!str_list_add(mem, &seg->tags, _cvt_sptype(usp->type))) {
247 log_error("Allocation failed for str_list.");
251 if (!set_lv_segment_area_pv(seg, 0, usp->devs[j].pv, 0))
253 dm_list_add(&lv->segments, &seg->list);
261 int import_pool_segments(struct dm_list *lvs, struct dm_pool *mem,
262 struct user_subpool *usp, int subpools)
265 struct logical_volume *lv;
269 dm_list_iterate_items(lvl, lvs) {
272 if (lv->status & SNAPSHOT)
275 for (i = 0; i < subpools; i++) {
276 if (usp[i].striping) {
277 if (!_add_stripe_seg(mem, &usp[i], lv, &le_cur))
280 if (!_add_linear_seg(mem, &usp[i], lv, &le_cur))