Tizen 2.1 base
[external/device-mapper.git] / lib / format_pool / import_export.c
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 int import_pool_vg(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
32 {
33         struct pool_list *pl;
34
35         dm_list_iterate_items(pl, pls) {
36                 vg->extent_count +=
37                     ((pl->pd.pl_blocks) / POOL_PE_SIZE);
38
39                 vg->free_count = vg->extent_count;
40
41                 if (vg->name)
42                         continue;
43
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;
48                 vg->max_lv = 1;
49                 vg->max_pv = POOL_MAX_DEVICES;
50                 vg->alloc = ALLOC_NORMAL;
51         }
52
53         return 1;
54 }
55
56 int import_pool_lvs(struct volume_group *vg, struct dm_pool *mem, struct dm_list *pls)
57 {
58         struct pool_list *pl;
59         struct logical_volume *lv;
60
61         if (!(lv = alloc_lv(mem)))
62                 return_0;
63
64         lv->status = 0;
65         lv->alloc = ALLOC_NORMAL;
66         lv->size = 0;
67         lv->name = NULL;
68         lv->le_count = 0;
69         lv->read_ahead = vg->cmd->default_settings.read_ahead;
70
71         dm_list_iterate_items(pl, pls) {
72                 lv->size += pl->pd.pl_blocks;
73
74                 if (lv->name)
75                         continue;
76
77                 if (!(lv->name = dm_pool_strdup(mem, pl->pd.pl_pool_name)))
78                         return_0;
79
80                 get_pool_lv_uuid(lv->lvid.id, &pl->pd);
81                 log_debug("Calculated lv uuid for lv %s: %s", lv->name,
82                           lv->lvid.s);
83
84                 lv->status |= VISIBLE_LV | LVM_READ | LVM_WRITE;
85                 lv->major = POOL_MAJOR;
86
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;
91                 } else {
92                         lv->minor = -1;
93                 }
94         }
95
96         lv->le_count = lv->size / POOL_PE_SIZE;
97
98         return link_lv_to_vg(vg, lv);
99 }
100
101 int import_pool_pvs(const struct format_type *fmt, struct volume_group *vg,
102                     struct dm_pool *mem, struct dm_list *pls)
103 {
104         struct pv_list *pvl;
105         struct pool_list *pl;
106
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");
110                         return 0;
111                 }
112                 if (!(pvl->pv = dm_pool_zalloc(mem, sizeof(*pvl->pv)))) {
113                         log_error("Unable to allocate pv structure");
114                         return 0;
115                 }
116                 if (!import_pool_pv(fmt, mem, vg, pvl->pv, pl)) {
117                         return 0;
118                 }
119                 pl->pv = pvl->pv;
120                 pvl->mdas = NULL;
121                 pvl->pe_ranges = NULL;
122                 add_pvl_to_vgs(vg, pvl);
123         }
124
125         return 1;
126 }
127
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)
131 {
132         struct pool_disk *pd = &pl->pd;
133
134         memset(pv, 0, sizeof(*pv));
135
136         get_pool_pv_uuid(&pv->id, pd);
137         pv->fmt = fmt;
138
139         pv->dev = pl->dev;
140         if (!(pv->vg_name = dm_pool_strdup(mem, pd->pl_pool_name))) {
141                 log_error("Unable to duplicate vg_name string");
142                 return 0;
143         }
144         if (vg != NULL)
145                 memcpy(&pv->vgid, &vg->id, sizeof(vg->id));
146         pv->status = 0;
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;
152         pv->pe_align = 0;
153
154         dm_list_init(&pv->tags);
155         dm_list_init(&pv->segments);
156
157         if (!alloc_pv_segment_whole_pv(mem, pv))
158                 return_0;
159
160         return 1;
161 }
162
163 static const char *_cvt_sptype(uint32_t sptype)
164 {
165         int i;
166         for (i = 0; sptype_names[i].name[0]; i++) {
167                 if (sptype == sptype_names[i].label) {
168                         break;
169                 }
170         }
171         log_debug("Found sptype %X and converted it to %s",
172                   sptype, sptype_names[i].name);
173         return sptype_names[i].name;
174 }
175
176 static int _add_stripe_seg(struct dm_pool *mem,
177                            struct user_subpool *usp, struct logical_volume *lv,
178                            uint32_t *le_cur)
179 {
180         struct lv_segment *seg;
181         struct segment_type *segtype;
182         unsigned j;
183         uint32_t area_len;
184
185         if (usp->striping & (usp->striping - 1)) {
186                 log_error("Stripe size must be a power of 2");
187                 return 0;
188         }
189
190         area_len = (usp->devs[0].blocks) / POOL_PE_SIZE;
191
192         if (!(segtype = get_segtype_from_string(lv->vg->cmd,
193                                                      "striped")))
194                 return_0;
195
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");
201                 return 0;
202         }
203
204         for (j = 0; j < usp->num_devs; j++)
205                 if (!set_lv_segment_area_pv(seg, j, usp->devs[j].pv, 0))
206                         return_0;
207
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.");
211                 return 0;
212         }
213
214         dm_list_add(&lv->segments, &seg->list);
215
216         *le_cur += seg->len;
217
218         return 1;
219 }
220
221 static int _add_linear_seg(struct dm_pool *mem,
222                            struct user_subpool *usp, struct logical_volume *lv,
223                            uint32_t *le_cur)
224 {
225         struct lv_segment *seg;
226         struct segment_type *segtype;
227         unsigned j;
228         uint32_t area_len;
229
230         if (!(segtype = get_segtype_from_string(lv->vg->cmd, "striped")))
231                 return_0;
232
233         for (j = 0; j < usp->num_devs; j++) {
234                 area_len = (usp->devs[j].blocks) / POOL_PE_SIZE;
235
236                 if (!(seg = alloc_lv_segment(mem, segtype, lv, *le_cur,
237                                              area_len, 0, usp->striping,
238                                              NULL, 1, area_len,
239                                              POOL_PE_SIZE, 0, 0, NULL))) {
240                         log_error("Unable to allocate linear lv_segment "
241                                   "structure");
242                         return 0;
243                 }
244
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.");
248                         return 0;
249                 }
250
251                 if (!set_lv_segment_area_pv(seg, 0, usp->devs[j].pv, 0))
252                         return_0;
253                 dm_list_add(&lv->segments, &seg->list);
254
255                 *le_cur += seg->len;
256         }
257
258         return 1;
259 }
260
261 int import_pool_segments(struct dm_list *lvs, struct dm_pool *mem,
262                          struct user_subpool *usp, int subpools)
263 {
264         struct lv_list *lvl;
265         struct logical_volume *lv;
266         uint32_t le_cur = 0;
267         int i;
268
269         dm_list_iterate_items(lvl, lvs) {
270                 lv = lvl->lv;
271
272                 if (lv->status & SNAPSHOT)
273                         continue;
274
275                 for (i = 0; i < subpools; i++) {
276                         if (usp[i].striping) {
277                                 if (!_add_stripe_seg(mem, &usp[i], lv, &le_cur))
278                                         return_0;
279                         } else {
280                                 if (!_add_linear_seg(mem, &usp[i], lv, &le_cur))
281                                         return_0;
282                         }
283                 }
284         }
285
286         return 1;
287 }