Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 1997-2004 Sistina Software, Inc. All rights reserved.
3 : : * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
4 : : *
5 : : * This file is part of LVM2.
6 : : *
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.
10 : : *
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
14 : : */
15 : :
16 : : #include "lib.h"
17 : : #include "label.h"
18 : : #include "metadata.h"
19 : : #include "lvmcache.h"
20 : : #include "disk_rep.h"
21 : : #include "sptype_names.h"
22 : : #include "lv_alloc.h"
23 : : #include "pv_alloc.h"
24 : : #include "str_list.h"
25 : : #include "display.h"
26 : : #include "segtype.h"
27 : : #include "toolcontext.h"
28 : :
29 : : /* This file contains only imports at the moment... */
30 : :
31 : 0 : int import_pool_vg(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
32 : : {
33 : : struct pool_list *pl;
34 : :
35 [ # # ]: 0 : dm_list_iterate_items(pl, pls) {
36 : 0 : vg->extent_count +=
37 : 0 : ((pl->pd.pl_blocks) / POOL_PE_SIZE);
38 : :
39 : 0 : vg->free_count = vg->extent_count;
40 : 0 : vg->pv_count++;
41 : :
42 [ # # ]: 0 : if (vg->name)
43 : 0 : continue;
44 : :
45 : 0 : vg->name = dm_pool_strdup(mem, pl->pd.pl_pool_name);
46 : 0 : get_pool_vg_uuid(&vg->id, &pl->pd);
47 : 0 : vg->extent_size = POOL_PE_SIZE;
48 : 0 : vg->status |= LVM_READ | LVM_WRITE | CLUSTERED | SHARED;
49 : 0 : vg->max_lv = 1;
50 : 0 : vg->max_pv = POOL_MAX_DEVICES;
51 : 0 : vg->alloc = ALLOC_NORMAL;
52 : : }
53 : :
54 : 0 : return 1;
55 : : }
56 : :
57 : 0 : int import_pool_lvs(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
58 : : {
59 : : struct pool_list *pl;
60 : : struct logical_volume *lv;
61 : :
62 [ # # ]: 0 : if (!(lv = alloc_lv(mem)))
63 : 0 : return_0;
64 : :
65 : 0 : lv->status = 0;
66 : 0 : lv->alloc = ALLOC_NORMAL;
67 : 0 : lv->size = 0;
68 : 0 : lv->name = NULL;
69 : 0 : lv->le_count = 0;
70 : 0 : lv->read_ahead = vg->cmd->default_settings.read_ahead;
71 : :
72 [ # # ]: 0 : dm_list_iterate_items(pl, pls) {
73 : 0 : lv->size += pl->pd.pl_blocks;
74 : :
75 [ # # ]: 0 : if (lv->name)
76 : 0 : continue;
77 : :
78 [ # # ]: 0 : if (!(lv->name = dm_pool_strdup(mem, pl->pd.pl_pool_name)))
79 : 0 : return_0;
80 : :
81 : 0 : get_pool_lv_uuid(lv->lvid.id, &pl->pd);
82 : 0 : log_debug("Calculated lv uuid for lv %s: %s", lv->name,
83 : : lv->lvid.s);
84 : :
85 : 0 : lv->status |= VISIBLE_LV | LVM_READ | LVM_WRITE;
86 : 0 : lv->major = POOL_MAJOR;
87 : :
88 : : /* for pool a minor of 0 is dynamic */
89 [ # # ]: 0 : if (pl->pd.pl_minor) {
90 : 0 : lv->status |= FIXED_MINOR;
91 : 0 : lv->minor = pl->pd.pl_minor + MINOR_OFFSET;
92 : : } else {
93 : 0 : lv->minor = -1;
94 : : }
95 : : }
96 : :
97 : 0 : lv->le_count = lv->size / POOL_PE_SIZE;
98 : :
99 : 0 : return link_lv_to_vg(vg, lv);
100 : : }
101 : :
102 : 0 : int import_pool_pvs(const struct format_type *fmt, struct volume_group *vg,
103 : : struct dm_list *pvs, struct dm_pool *mem, struct dm_list *pls)
104 : : {
105 : : struct pv_list *pvl;
106 : : struct pool_list *pl;
107 : :
108 [ # # ]: 0 : dm_list_iterate_items(pl, pls) {
109 [ # # ]: 0 : if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl)))) {
110 : 0 : log_error("Unable to allocate pv list structure");
111 : 0 : return 0;
112 : : }
113 [ # # ]: 0 : if (!(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv)))) {
114 : 0 : log_error("Unable to allocate pv structure");
115 : 0 : return 0;
116 : : }
117 [ # # ]: 0 : if (!import_pool_pv(fmt, mem, vg, pvl->pv, pl)) {
118 : 0 : return 0;
119 : : }
120 : 0 : pl->pv = pvl->pv;
121 : 0 : pvl->mdas = NULL;
122 : 0 : pvl->pe_ranges = NULL;
123 : 0 : dm_list_add(pvs, &pvl->list);
124 : : }
125 : :
126 : 0 : return 1;
127 : : }
128 : :
129 : 0 : int import_pool_pv(const struct format_type *fmt, struct dm_pool *mem,
130 : : struct volume_group *vg, struct physical_volume *pv,
131 : : struct pool_list *pl)
132 : : {
133 : 0 : struct pool_disk *pd = &pl->pd;
134 : :
135 : 0 : memset(pv, 0, sizeof(*pv));
136 : :
137 : 0 : get_pool_pv_uuid(&pv->id, pd);
138 : 0 : pv->fmt = fmt;
139 : :
140 : 0 : pv->dev = pl->dev;
141 [ # # ]: 0 : if (!(pv->vg_name = dm_pool_strdup(mem, pd->pl_pool_name))) {
142 : 0 : log_error("Unable to duplicate vg_name string");
143 : 0 : return 0;
144 : : }
145 [ # # ]: 0 : if (vg != NULL)
146 : 0 : memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
147 : 0 : pv->status = 0;
148 : 0 : pv->size = pd->pl_blocks;
149 : 0 : pv->pe_size = POOL_PE_SIZE;
150 : 0 : pv->pe_start = POOL_PE_START;
151 : 0 : pv->pe_count = pv->size / POOL_PE_SIZE;
152 : 0 : pv->pe_alloc_count = 0;
153 : 0 : pv->pe_align = 0;
154 : :
155 : 0 : dm_list_init(&pv->tags);
156 : 0 : dm_list_init(&pv->segments);
157 : :
158 [ # # ]: 0 : if (!alloc_pv_segment_whole_pv(mem, pv))
159 : 0 : return_0;
160 : :
161 : 0 : return 1;
162 : : }
163 : :
164 : 0 : static const char *_cvt_sptype(uint32_t sptype)
165 : : {
166 : : int i;
167 [ # # ]: 0 : for (i = 0; sptype_names[i].name[0]; i++) {
168 [ # # ]: 0 : if (sptype == sptype_names[i].label) {
169 : 0 : break;
170 : : }
171 : : }
172 : 0 : log_debug("Found sptype %X and converted it to %s",
173 : : sptype, sptype_names[i].name);
174 : 0 : return sptype_names[i].name;
175 : : }
176 : :
177 : 0 : static int _add_stripe_seg(struct dm_pool *mem,
178 : : struct user_subpool *usp, struct logical_volume *lv,
179 : : uint32_t *le_cur)
180 : : {
181 : : struct lv_segment *seg;
182 : : struct segment_type *segtype;
183 : : unsigned j;
184 : : uint32_t area_len;
185 : :
186 [ # # ]: 0 : if (usp->striping & (usp->striping - 1)) {
187 : 0 : log_error("Stripe size must be a power of 2");
188 : 0 : return 0;
189 : : }
190 : :
191 : 0 : area_len = (usp->devs[0].blocks) / POOL_PE_SIZE;
192 : :
193 [ # # ]: 0 : if (!(segtype = get_segtype_from_string(lv->vg->cmd,
194 : : "striped")))
195 : 0 : return_0;
196 : :
197 [ # # ]: 0 : if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
198 : : area_len * usp->num_devs, 0,
199 : : usp->striping, NULL, usp->num_devs,
200 : : area_len, 0, 0, 0, NULL))) {
201 : 0 : log_error("Unable to allocate striped lv_segment structure");
202 : 0 : return 0;
203 : : }
204 : :
205 [ # # ]: 0 : for (j = 0; j < usp->num_devs; j++)
206 [ # # ]: 0 : if (!set_lv_segment_area_pv(seg, j, usp->devs[j].pv, 0))
207 : 0 : return_0;
208 : :
209 : : /* add the subpool type to the segment tag list */
210 : 0 : str_list_add(mem, &seg->tags, _cvt_sptype(usp->type));
211 : :
212 : 0 : dm_list_add(&lv->segments, &seg->list);
213 : :
214 : 0 : *le_cur += seg->len;
215 : :
216 : 0 : return 1;
217 : : }
218 : :
219 : 0 : static int _add_linear_seg(struct dm_pool *mem,
220 : : struct user_subpool *usp, struct logical_volume *lv,
221 : : uint32_t *le_cur)
222 : : {
223 : : struct lv_segment *seg;
224 : : struct segment_type *segtype;
225 : : unsigned j;
226 : : uint32_t area_len;
227 : :
228 [ # # ]: 0 : if (!(segtype = get_segtype_from_string(lv->vg->cmd, "striped")))
229 : 0 : return_0;
230 : :
231 [ # # ]: 0 : for (j = 0; j < usp->num_devs; j++) {
232 : 0 : area_len = (usp->devs[j].blocks) / POOL_PE_SIZE;
233 : :
234 [ # # ]: 0 : if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
235 : : area_len, 0, usp->striping,
236 : : NULL, 1, area_len,
237 : : POOL_PE_SIZE, 0, 0, NULL))) {
238 : 0 : log_error("Unable to allocate linear lv_segment "
239 : : "structure");
240 : 0 : return 0;
241 : : }
242 : :
243 : : /* add the subpool type to the segment tag list */
244 : 0 : str_list_add(mem, &seg->tags, _cvt_sptype(usp->type));
245 : :
246 [ # # ]: 0 : if (!set_lv_segment_area_pv(seg, 0, usp->devs[j].pv, 0))
247 : 0 : return_0;
248 : 0 : dm_list_add(&lv->segments, &seg->list);
249 : :
250 : 0 : *le_cur += seg->len;
251 : : }
252 : :
253 : 0 : return 1;
254 : : }
255 : :
256 : 0 : int import_pool_segments(struct dm_list *lvs, struct dm_pool *mem,
257 : : struct user_subpool *usp, int subpools)
258 : : {
259 : : struct lv_list *lvl;
260 : : struct logical_volume *lv;
261 : 0 : uint32_t le_cur = 0;
262 : : int i;
263 : :
264 [ # # ]: 0 : dm_list_iterate_items(lvl, lvs) {
265 : 0 : lv = lvl->lv;
266 : :
267 [ # # ]: 0 : if (lv->status & SNAPSHOT)
268 : 0 : continue;
269 : :
270 [ # # ]: 0 : for (i = 0; i < subpools; i++) {
271 [ # # ]: 0 : if (usp[i].striping) {
272 [ # # ]: 0 : if (!_add_stripe_seg(mem, &usp[i], lv, &le_cur))
273 : 0 : return_0;
274 : : } else {
275 [ # # ]: 0 : if (!_add_linear_seg(mem, &usp[i], lv, &le_cur))
276 : 0 : return_0;
277 : : }
278 : : }
279 : : }
280 : :
281 : 0 : return 1;
282 : : }
|