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