Tizen 2.1 base
[external/device-mapper.git] / lib / format1 / import-export.c
1 /*
2  * Copyright (C) 2001-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 /*
17  * Translates between disk and in-core formats.
18  */
19
20 #include "lib.h"
21 #include "disk-rep.h"
22 #include "lvm-string.h"
23 #include "filter.h"
24 #include "toolcontext.h"
25 #include "segtype.h"
26 #include "pv_alloc.h"
27 #include "display.h"
28 #include "lvmcache.h"
29 #include "metadata.h"
30
31 #include <time.h>
32
33 static int _check_vg_name(const char *name)
34 {
35         return strlen(name) < NAME_LEN;
36 }
37
38 /*
39  * Extracts the last part of a path.
40  */
41 static char *_create_lv_name(struct dm_pool *mem, const char *full_name)
42 {
43         const char *ptr = strrchr(full_name, '/');
44
45         if (!ptr)
46                 ptr = full_name;
47         else
48                 ptr++;
49
50         return dm_pool_strdup(mem, ptr);
51 }
52
53 int import_pv(const struct format_type *fmt, struct dm_pool *mem,
54               struct device *dev, struct volume_group *vg,
55               struct physical_volume *pv, struct pv_disk *pvd,
56               struct vg_disk *vgd)
57 {
58         uint64_t size;
59
60         memset(pv, 0, sizeof(*pv));
61         memcpy(&pv->id, pvd->pv_uuid, ID_LEN);
62
63         pv->dev = dev;
64         if (!*pvd->vg_name)
65                 pv->vg_name = fmt->orphan_vg_name;
66         else if (!(pv->vg_name = dm_pool_strdup(mem, (char *)pvd->vg_name))) {
67                 log_error("Volume Group name allocation failed.");
68                 return 0;
69         }
70
71         memcpy(&pv->vgid, vgd->vg_uuid, sizeof(vg->id));
72
73         /* Store system_id from first PV if PV belongs to a VG */
74         if (vg && !*vg->system_id)
75                 strncpy(vg->system_id, (char *)pvd->system_id, NAME_LEN);
76
77         if (vg &&
78             strncmp(vg->system_id, (char *)pvd->system_id, sizeof(pvd->system_id)))
79                     log_very_verbose("System ID %s on %s differs from %s for "
80                                      "volume group", pvd->system_id,
81                                      pv_dev_name(pv), vg->system_id);
82
83         /*
84          * If exported, we still need to flag in pv->status too because
85          * we don't always have a struct volume_group when we need this.
86          */
87         if (pvd->pv_status & VG_EXPORTED)
88                 pv->status |= EXPORTED_VG;
89
90         if (pvd->pv_allocatable)
91                 pv->status |= ALLOCATABLE_PV;
92
93         pv->size = pvd->pv_size;
94         pv->pe_size = pvd->pe_size;
95         pv->pe_start = pvd->pe_start;
96         pv->pe_count = pvd->pe_total;
97         pv->pe_alloc_count = 0;
98         pv->pe_align = 0;
99
100         /* Fix up pv size if missing or impossibly large */
101         if (!pv->size || pv->size > (1ULL << 62)) {
102                 if (!dev_get_size(dev, &pv->size)) {
103                         log_error("%s: Couldn't get size.", pv_dev_name(pv));
104                         return 0;
105                 }
106                 log_verbose("Fixing up missing format1 size (%s) "
107                             "for PV %s", display_size(fmt->cmd, pv->size),
108                             pv_dev_name(pv));
109                 if (vg) {
110                         size = pv->pe_count * (uint64_t) vg->extent_size +
111                                pv->pe_start;
112                         if (size > pv->size)
113                                 log_warn("WARNING: Physical Volume %s is too "
114                                          "large for underlying device",
115                                          pv_dev_name(pv));
116                 }
117         }
118
119         dm_list_init(&pv->tags);
120         dm_list_init(&pv->segments);
121
122         if (!alloc_pv_segment_whole_pv(mem, pv))
123                 return_0;
124
125         return 1;
126 }
127
128 static int _system_id(struct cmd_context *cmd, char *s, const char *prefix)
129 {
130
131         if (dm_snprintf(s, NAME_LEN, "%s%s%lu",
132                          prefix, cmd->hostname, time(NULL)) < 0) {
133                 log_error("Generated system_id too long");
134                 return 0;
135         }
136
137         return 1;
138 }
139
140 int export_pv(struct cmd_context *cmd, struct dm_pool *mem __attribute__((unused)),
141               struct volume_group *vg,
142               struct pv_disk *pvd, struct physical_volume *pv)
143 {
144         memset(pvd, 0, sizeof(*pvd));
145
146         pvd->id[0] = 'H';
147         pvd->id[1] = 'M';
148         pvd->version = 1;
149
150         memcpy(pvd->pv_uuid, pv->id.uuid, ID_LEN);
151
152         if (pv->vg_name && !is_orphan(pv)) {
153                 if (!_check_vg_name(pv->vg_name))
154                         return_0;
155                 strncpy((char *)pvd->vg_name, pv->vg_name, sizeof(pvd->vg_name));
156         }
157
158         /* Preserve existing system_id if it exists */
159         if (vg && *vg->system_id)
160                 strncpy((char *)pvd->system_id, vg->system_id, sizeof(pvd->system_id));
161
162         /* Is VG already exported or being exported? */
163         if (vg && vg_is_exported(vg)) {
164                 /* Does system_id need setting? */
165                 if (!*vg->system_id ||
166                     strncmp(vg->system_id, EXPORTED_TAG,
167                             sizeof(EXPORTED_TAG) - 1)) {
168                         if (!_system_id(cmd, (char *)pvd->system_id, EXPORTED_TAG))
169                                 return_0;
170                 }
171                 if (strlen((char *)pvd->vg_name) + sizeof(EXPORTED_TAG) >
172                     sizeof(pvd->vg_name)) {
173                         log_error("Volume group name %s too long to export",
174                                   pvd->vg_name);
175                         return 0;
176                 }
177                 strcat((char *)pvd->vg_name, EXPORTED_TAG);
178         }
179
180         /* Is VG being imported? */
181         if (vg && !vg_is_exported(vg) && *vg->system_id &&
182             !strncmp(vg->system_id, EXPORTED_TAG, sizeof(EXPORTED_TAG) - 1)) {
183                 if (!_system_id(cmd, (char *)pvd->system_id, IMPORTED_TAG))
184                         return_0;
185         }
186
187         /* Generate system_id if PV is in VG */
188         if (!pvd->system_id[0])
189                 if (!_system_id(cmd, (char *)pvd->system_id, ""))
190                         return_0;
191
192         /* Update internal system_id if we changed it */
193         if (vg &&
194             (!*vg->system_id ||
195              strncmp(vg->system_id, (char *)pvd->system_id, sizeof(pvd->system_id))))
196                     strncpy(vg->system_id, (char *)pvd->system_id, NAME_LEN);
197
198         //pvd->pv_major = MAJOR(pv->dev);
199
200         if (pv->status & ALLOCATABLE_PV)
201                 pvd->pv_allocatable = PV_ALLOCATABLE;
202
203         pvd->pv_size = pv->size;
204         pvd->lv_cur = 0;        /* this is set when exporting the lv list */
205         if (vg)
206                 pvd->pe_size = vg->extent_size;
207         else
208                 pvd->pe_size = pv->pe_size;
209         pvd->pe_total = pv->pe_count;
210         pvd->pe_allocated = pv->pe_alloc_count;
211         pvd->pe_start = pv->pe_start;
212
213         return 1;
214 }
215
216 int import_vg(struct dm_pool *mem,
217               struct volume_group *vg, struct disk_list *dl)
218 {
219         struct vg_disk *vgd = &dl->vgd;
220         memcpy(vg->id.uuid, vgd->vg_uuid, ID_LEN);
221
222         if (!_check_vg_name((char *)dl->pvd.vg_name))
223                 return_0;
224
225         if (!(vg->name = dm_pool_strdup(mem, (char *)dl->pvd.vg_name)))
226                 return_0;
227
228         if (!(vg->system_id = dm_pool_alloc(mem, NAME_LEN)))
229                 return_0;
230
231         *vg->system_id = '\0';
232
233         if (vgd->vg_status & VG_EXPORTED)
234                 vg->status |= EXPORTED_VG;
235
236         if (vgd->vg_status & VG_EXTENDABLE)
237                 vg->status |= RESIZEABLE_VG;
238
239         if (vgd->vg_access & VG_READ)
240                 vg->status |= LVM_READ;
241
242         if (vgd->vg_access & VG_WRITE)
243                 vg->status |= LVM_WRITE;
244
245         if (vgd->vg_access & VG_CLUSTERED)
246                 vg->status |= CLUSTERED;
247
248         if (vgd->vg_access & VG_SHARED)
249                 vg->status |= SHARED;
250
251         vg->extent_size = vgd->pe_size;
252         vg->extent_count = vgd->pe_total;
253         vg->free_count = vgd->pe_total;
254         vg->max_lv = vgd->lv_max;
255         vg->max_pv = vgd->pv_max;
256         vg->alloc = ALLOC_NORMAL;
257
258         return 1;
259 }
260
261 int export_vg(struct vg_disk *vgd, struct volume_group *vg)
262 {
263         memset(vgd, 0, sizeof(*vgd));
264         memcpy(vgd->vg_uuid, vg->id.uuid, ID_LEN);
265
266         if (vg->status & LVM_READ)
267                 vgd->vg_access |= VG_READ;
268
269         if (vg->status & LVM_WRITE)
270                 vgd->vg_access |= VG_WRITE;
271
272         if (vg_is_clustered(vg))
273                 vgd->vg_access |= VG_CLUSTERED;
274
275         if (vg->status & SHARED)
276                 vgd->vg_access |= VG_SHARED;
277
278         if (vg_is_exported(vg))
279                 vgd->vg_status |= VG_EXPORTED;
280
281         if (vg_is_resizeable(vg))
282                 vgd->vg_status |= VG_EXTENDABLE;
283
284         vgd->lv_max = vg->max_lv;
285         vgd->lv_cur = vg_visible_lvs(vg) + snapshot_count(vg);
286
287         vgd->pv_max = vg->max_pv;
288         vgd->pv_cur = vg->pv_count;
289
290         vgd->pe_size = vg->extent_size;
291         vgd->pe_total = vg->extent_count;
292         vgd->pe_allocated = vg->extent_count - vg->free_count;
293
294         return 1;
295 }
296
297 int import_lv(struct cmd_context *cmd, struct dm_pool *mem,
298               struct logical_volume *lv, struct lv_disk *lvd)
299 {
300         if (!(lv->name = _create_lv_name(mem, (char *)lvd->lv_name)))
301                 return_0;
302
303         lv->status |= VISIBLE_LV;
304
305         if (lvd->lv_status & LV_SPINDOWN)
306                 lv->status |= SPINDOWN_LV;
307
308         if (lvd->lv_status & LV_PERSISTENT_MINOR) {
309                 lv->status |= FIXED_MINOR;
310                 lv->minor = MINOR(lvd->lv_dev);
311                 lv->major = MAJOR(lvd->lv_dev);
312         } else {
313                 lv->major = -1;
314                 lv->minor = -1;
315         }
316
317         if (lvd->lv_access & LV_READ)
318                 lv->status |= LVM_READ;
319
320         if (lvd->lv_access & LV_WRITE)
321                 lv->status |= LVM_WRITE;
322
323         if (lvd->lv_badblock)
324                 lv->status |= BADBLOCK_ON;
325
326         /* Drop the unused LV_STRICT here */
327         if (lvd->lv_allocation & LV_CONTIGUOUS)
328                 lv->alloc = ALLOC_CONTIGUOUS;
329         else
330                 lv->alloc = ALLOC_NORMAL;
331
332         if (!lvd->lv_read_ahead)
333                 lv->read_ahead = cmd->default_settings.read_ahead;
334         else
335                 lv->read_ahead = lvd->lv_read_ahead;
336
337         lv->size = lvd->lv_size;
338         lv->le_count = lvd->lv_allocated_le;
339
340         return 1;
341 }
342
343 static void _export_lv(struct lv_disk *lvd, struct volume_group *vg,
344                        struct logical_volume *lv, const char *dev_dir)
345 {
346         memset(lvd, 0, sizeof(*lvd));
347         snprintf((char *)lvd->lv_name, sizeof(lvd->lv_name), "%s%s/%s",
348                  dev_dir, vg->name, lv->name);
349
350         strcpy((char *)lvd->vg_name, vg->name);
351
352         if (lv->status & LVM_READ)
353                 lvd->lv_access |= LV_READ;
354
355         if (lv->status & LVM_WRITE)
356                 lvd->lv_access |= LV_WRITE;
357
358         if (lv->status & SPINDOWN_LV)
359                 lvd->lv_status |= LV_SPINDOWN;
360
361         if (lv->status & FIXED_MINOR) {
362                 lvd->lv_status |= LV_PERSISTENT_MINOR;
363                 lvd->lv_dev = MKDEV(lv->major, lv->minor);
364         } else {
365                 lvd->lv_dev = MKDEV(LVM_BLK_MAJOR, lvnum_from_lvid(&lv->lvid));
366         }
367
368         if (lv->read_ahead == DM_READ_AHEAD_AUTO ||
369             lv->read_ahead == DM_READ_AHEAD_NONE)
370                 lvd->lv_read_ahead = 0;
371         else
372                 lvd->lv_read_ahead = lv->read_ahead;
373
374         lvd->lv_stripes =
375             dm_list_item(lv->segments.n, struct lv_segment)->area_count;
376         lvd->lv_stripesize =
377             dm_list_item(lv->segments.n, struct lv_segment)->stripe_size;
378
379         lvd->lv_size = lv->size;
380         lvd->lv_allocated_le = lv->le_count;
381
382         if (lv->status & BADBLOCK_ON)
383                 lvd->lv_badblock = LV_BADBLOCK_ON;
384
385         if (lv->alloc == ALLOC_CONTIGUOUS)
386                 lvd->lv_allocation |= LV_CONTIGUOUS;
387 }
388
389 int export_extents(struct disk_list *dl, uint32_t lv_num,
390                    struct logical_volume *lv, struct physical_volume *pv)
391 {
392         struct pe_disk *ped;
393         struct lv_segment *seg;
394         uint32_t pe, s;
395
396         dm_list_iterate_items(seg, &lv->segments) {
397                 for (s = 0; s < seg->area_count; s++) {
398                         if (!(seg->segtype->flags & SEG_FORMAT1_SUPPORT)) {
399                                 log_error("Segment type %s in LV %s: "
400                                           "unsupported by format1",
401                                           seg->segtype->name, lv->name);
402                                 return 0;
403                         }
404                         if (seg_type(seg, s) != AREA_PV) {
405                                 log_error("Non-PV stripe found in LV %s: "
406                                           "unsupported by format1", lv->name);
407                                 return 0;
408                         }
409                         if (seg_pv(seg, s) != pv)
410                                 continue;       /* not our pv */
411
412                         for (pe = 0; pe < (seg->len / seg->area_count); pe++) {
413                                 ped = &dl->extents[pe + seg_pe(seg, s)];
414                                 ped->lv_num = lv_num;
415                                 ped->le_num = (seg->le / seg->area_count) + pe +
416                                     s * (lv->le_count / seg->area_count);
417                         }
418                 }
419         }
420
421         return 1;
422 }
423
424 int import_pvs(const struct format_type *fmt, struct dm_pool *mem,
425                struct volume_group *vg, struct dm_list *pvds)
426 {
427         struct disk_list *dl;
428         struct pv_list *pvl;
429
430         vg->pv_count = 0;
431         dm_list_iterate_items(dl, pvds) {
432                 if (!(pvl = dm_pool_zalloc(mem, sizeof(*pvl))) ||
433                     !(pvl->pv = dm_pool_alloc(mem, sizeof(*pvl->pv))))
434                         return_0;
435
436                 if (!import_pv(fmt, mem, dl->dev, vg, pvl->pv, &dl->pvd, &dl->vgd))
437                         return_0;
438
439                 pvl->pv->fmt = fmt;
440                 add_pvl_to_vgs(vg, pvl);
441         }
442
443         return 1;
444 }
445
446 static struct logical_volume *_add_lv(struct dm_pool *mem,
447                                       struct volume_group *vg,
448                                       struct lv_disk *lvd)
449 {
450         struct logical_volume *lv;
451
452         if (!(lv = alloc_lv(mem)))
453                 return_NULL;
454
455         lvid_from_lvnum(&lv->lvid, &vg->id, lvd->lv_number);
456
457         if (!import_lv(vg->cmd, mem, lv, lvd)) 
458                 goto_bad;
459
460         if (!link_lv_to_vg(vg, lv))
461                 goto_bad;
462
463         return lv;
464 bad:
465         dm_pool_free(mem, lv);
466         return NULL;
467 }
468
469 int import_lvs(struct dm_pool *mem, struct volume_group *vg, struct dm_list *pvds)
470 {
471         struct disk_list *dl;
472         struct lvd_list *ll;
473         struct lv_disk *lvd;
474
475         dm_list_iterate_items(dl, pvds) {
476                 dm_list_iterate_items(ll, &dl->lvds) {
477                         lvd = &ll->lvd;
478
479                         if (!find_lv(vg, (char *)lvd->lv_name) &&
480                             !_add_lv(mem, vg, lvd))
481                                 return_0;
482                 }
483         }
484
485         return 1;
486 }
487
488 /* FIXME: tidy */
489 int export_lvs(struct disk_list *dl, struct volume_group *vg,
490                struct physical_volume *pv, const char *dev_dir)
491 {
492         int r = 0;
493         struct lv_list *ll;
494         struct lvd_list *lvdl;
495         size_t len;
496         uint32_t lv_num;
497         struct dm_hash_table *lvd_hash;
498
499         if (!_check_vg_name(vg->name))
500                 return_0;
501
502         if (!(lvd_hash = dm_hash_create(32)))
503                 return_0;
504
505         /*
506          * setup the pv's extents array
507          */
508         len = sizeof(struct pe_disk) * dl->pvd.pe_total;
509         if (!(dl->extents = dm_pool_zalloc(dl->mem, len)))
510                 goto_out;
511
512         dm_list_iterate_items(ll, &vg->lvs) {
513                 if (ll->lv->status & SNAPSHOT)
514                         continue;
515
516                 if (!(lvdl = dm_pool_alloc(dl->mem, sizeof(*lvdl))))
517                         goto_out;
518
519                 _export_lv(&lvdl->lvd, vg, ll->lv, dev_dir);
520
521                 lv_num = lvnum_from_lvid(&ll->lv->lvid);
522                 lvdl->lvd.lv_number = lv_num;
523
524                 if (!dm_hash_insert(lvd_hash, ll->lv->name, &lvdl->lvd))
525                         goto_out;
526
527                 if (!export_extents(dl, lv_num + 1, ll->lv, pv))
528                         goto_out;
529
530                 if (lv_is_origin(ll->lv))
531                         lvdl->lvd.lv_access |= LV_SNAPSHOT_ORG;
532
533                 if (lv_is_cow(ll->lv)) {
534                         lvdl->lvd.lv_access |= LV_SNAPSHOT;
535                         lvdl->lvd.lv_chunk_size = ll->lv->snapshot->chunk_size;
536                         lvdl->lvd.lv_snapshot_minor =
537                             lvnum_from_lvid(&ll->lv->snapshot->origin->lvid);
538                 }
539
540                 dm_list_add(&dl->lvds, &lvdl->list);
541                 dl->pvd.lv_cur++;
542         }
543
544         r = 1;
545
546       out:
547         dm_hash_destroy(lvd_hash);
548         return r;
549 }
550
551 /*
552  * FIXME: More inefficient code.
553  */
554 int import_snapshots(struct dm_pool *mem __attribute__((unused)), struct volume_group *vg,
555                      struct dm_list *pvds)
556 {
557         struct logical_volume *lvs[MAX_LV];
558         struct disk_list *dl;
559         struct lvd_list *ll;
560         struct lv_disk *lvd;
561         int lvnum;
562         struct logical_volume *org, *cow;
563
564         /* build an index of lv numbers */
565         memset(lvs, 0, sizeof(lvs));
566         dm_list_iterate_items(dl, pvds) {
567                 dm_list_iterate_items(ll, &dl->lvds) {
568                         lvd = &ll->lvd;
569
570                         lvnum = lvd->lv_number;
571
572                         if (lvnum >= MAX_LV) {
573                                 log_error("Logical volume number "
574                                           "out of bounds.");
575                                 return 0;
576                         }
577
578                         if (!lvs[lvnum] &&
579                             !(lvs[lvnum] = find_lv(vg, (char *)lvd->lv_name))) {
580                                 log_error("Couldn't find logical volume '%s'.",
581                                           lvd->lv_name);
582                                 return 0;
583                         }
584                 }
585         }
586
587         /*
588          * Now iterate through yet again adding the snapshots.
589          */
590         dm_list_iterate_items(dl, pvds) {
591                 dm_list_iterate_items(ll, &dl->lvds) {
592                         lvd = &ll->lvd;
593
594                         if (!(lvd->lv_access & LV_SNAPSHOT))
595                                 continue;
596
597                         lvnum = lvd->lv_number;
598                         cow = lvs[lvnum];
599                         if (!(org = lvs[lvd->lv_snapshot_minor])) {
600                                 log_error("Couldn't find origin logical volume "
601                                           "for snapshot '%s'.", lvd->lv_name);
602                                 return 0;
603                         }
604
605                         /* we may have already added this snapshot */
606                         if (lv_is_cow(cow))
607                                 continue;
608
609                         /* insert the snapshot */
610                         if (!vg_add_snapshot(org, cow, NULL,
611                                              org->le_count,
612                                              lvd->lv_chunk_size)) {
613                                 log_error("Couldn't add snapshot.");
614                                 return 0;
615                         }
616                 }
617         }
618
619         return 1;
620 }
621
622 int export_uuids(struct disk_list *dl, struct volume_group *vg)
623 {
624         struct uuid_list *ul;
625         struct pv_list *pvl;
626
627         dm_list_iterate_items(pvl, &vg->pvs) {
628                 if (!(ul = dm_pool_alloc(dl->mem, sizeof(*ul))))
629                         return_0;
630
631                 memset(ul->uuid, 0, sizeof(ul->uuid));
632                 memcpy(ul->uuid, pvl->pv->id.uuid, ID_LEN);
633
634                 dm_list_add(&dl->uuids, &ul->list);
635         }
636         return 1;
637 }
638
639 /*
640  * This calculates the nasty pv_number field
641  * used by LVM1.
642  */
643 void export_numbers(struct dm_list *pvds, struct volume_group *vg __attribute__((unused)))
644 {
645         struct disk_list *dl;
646         int pv_num = 1;
647
648         dm_list_iterate_items(dl, pvds)
649                 dl->pvd.pv_number = pv_num++;
650 }
651
652 /*
653  * Calculate vg_disk->pv_act.
654  */
655 void export_pv_act(struct dm_list *pvds)
656 {
657         struct disk_list *dl;
658         int act = 0;
659
660         dm_list_iterate_items(dl, pvds)
661                 if (dl->pvd.pv_status & PV_ACTIVE)
662                         act++;
663
664         dm_list_iterate_items(dl, pvds)
665                 dl->vgd.pv_act = act;
666 }
667
668 int export_vg_number(struct format_instance *fid, struct dm_list *pvds,
669                      const char *vg_name, struct dev_filter *filter)
670 {
671         struct disk_list *dl;
672         int vg_num;
673
674         if (!get_free_vg_number(fid, filter, vg_name, &vg_num))
675                 return_0;
676
677         dm_list_iterate_items(dl, pvds)
678                 dl->vgd.vg_number = vg_num;
679
680         return 1;
681 }