1 /* $NetBSD: format1.c,v 1.1.1.1 2008/12/22 00:17:59 haad Exp $ */ 2 3 /* 4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. 5 * Copyright (C) 2004-2007 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 #include "lib.h" 19 #include "disk-rep.h" 20 #include "limits.h" 21 #include "display.h" 22 #include "toolcontext.h" 23 #include "lvm1-label.h" 24 #include "format1.h" 25 #include "segtype.h" 26 27 /* VG consistency checks */ 28 static int _check_vgs(struct dm_list *pvs) 29 { 30 struct dm_list *pvh, *t; 31 struct disk_list *dl = NULL; 32 struct disk_list *first = NULL; 33 34 uint32_t pv_count = 0; 35 uint32_t exported = 0; 36 int first_time = 1; 37 38 /* 39 * If there are exported and unexported PVs, ignore exported ones. 40 * This means an active VG won't be affected if disks are inserted 41 * bearing an exported VG with the same name. 42 */ 43 dm_list_iterate_items(dl, pvs) { 44 if (first_time) { 45 exported = dl->pvd.pv_status & VG_EXPORTED; 46 first_time = 0; 47 continue; 48 } 49 50 if (exported != (dl->pvd.pv_status & VG_EXPORTED)) { 51 /* Remove exported PVs */ 52 dm_list_iterate_safe(pvh, t, pvs) { 53 dl = dm_list_item(pvh, struct disk_list); 54 if (dl->pvd.pv_status & VG_EXPORTED) 55 dm_list_del(pvh); 56 } 57 break; 58 } 59 } 60 61 /* Remove any PVs with VG structs that differ from the first */ 62 dm_list_iterate_safe(pvh, t, pvs) { 63 dl = dm_list_item(pvh, struct disk_list); 64 65 if (!first) 66 first = dl; 67 68 else if (memcmp(&first->vgd, &dl->vgd, sizeof(first->vgd))) { 69 log_error("VG data differs between PVs %s and %s", 70 dev_name(first->dev), dev_name(dl->dev)); 71 log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32 72 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" 73 PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 74 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" 75 PRIu32 " %" PRIu32 " %" PRIu32, 76 dev_name(first->dev), first->vgd.vg_uuid, 77 first->vgd.vg_name_dummy, 78 first->vgd.vg_number, first->vgd.vg_access, 79 first->vgd.vg_status, first->vgd.lv_max, 80 first->vgd.lv_cur, first->vgd.lv_open, 81 first->vgd.pv_max, first->vgd.pv_cur, 82 first->vgd.pv_act, first->vgd.dummy, 83 first->vgd.vgda, first->vgd.pe_size, 84 first->vgd.pe_total, first->vgd.pe_allocated, 85 first->vgd.pvg_total); 86 log_debug("VG data on %s: %s %s %" PRIu32 " %" PRIu32 87 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" 88 PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32 89 " %" PRIu32 " %" PRIu32 " %" PRIu32 " %" 90 PRIu32 " %" PRIu32 " %" PRIu32, 91 dev_name(dl->dev), dl->vgd.vg_uuid, 92 dl->vgd.vg_name_dummy, dl->vgd.vg_number, 93 dl->vgd.vg_access, dl->vgd.vg_status, 94 dl->vgd.lv_max, dl->vgd.lv_cur, 95 dl->vgd.lv_open, dl->vgd.pv_max, 96 dl->vgd.pv_cur, dl->vgd.pv_act, dl->vgd.dummy, 97 dl->vgd.vgda, dl->vgd.pe_size, 98 dl->vgd.pe_total, dl->vgd.pe_allocated, 99 dl->vgd.pvg_total); 100 dm_list_del(pvh); 101 return 0; 102 } 103 pv_count++; 104 } 105 106 /* On entry to fn, list known to be non-empty */ 107 if (pv_count != first->vgd.pv_cur) { 108 log_error("%d PV(s) found for VG %s: expected %d", 109 pv_count, first->pvd.vg_name, first->vgd.pv_cur); 110 } 111 112 return 1; 113 } 114 115 static struct volume_group *_build_vg(struct format_instance *fid, 116 struct dm_list *pvs) 117 { 118 struct dm_pool *mem = fid->fmt->cmd->mem; 119 struct volume_group *vg = dm_pool_alloc(mem, sizeof(*vg)); 120 struct disk_list *dl; 121 122 if (!vg) 123 goto_bad; 124 125 if (dm_list_empty(pvs)) 126 goto_bad; 127 128 memset(vg, 0, sizeof(*vg)); 129 130 vg->cmd = fid->fmt->cmd; 131 vg->fid = fid; 132 vg->seqno = 0; 133 dm_list_init(&vg->pvs); 134 dm_list_init(&vg->lvs); 135 dm_list_init(&vg->tags); 136 137 if (!_check_vgs(pvs)) 138 goto_bad; 139 140 dl = dm_list_item(pvs->n, struct disk_list); 141 142 if (!import_vg(mem, vg, dl)) 143 goto_bad; 144 145 if (!import_pvs(fid->fmt, mem, vg, pvs, &vg->pvs, &vg->pv_count)) 146 goto_bad; 147 148 if (!import_lvs(mem, vg, pvs)) 149 goto_bad; 150 151 if (!import_extents(fid->fmt->cmd, vg, pvs)) 152 goto_bad; 153 154 if (!import_snapshots(mem, vg, pvs)) 155 goto_bad; 156 157 return vg; 158 159 bad: 160 dm_pool_free(mem, vg); 161 return NULL; 162 } 163 164 static struct volume_group *_format1_vg_read(struct format_instance *fid, 165 const char *vg_name, 166 struct metadata_area *mda __attribute((unused))) 167 { 168 struct dm_pool *mem = dm_pool_create("lvm1 vg_read", 1024 * 10); 169 struct dm_list pvs; 170 struct volume_group *vg = NULL; 171 dm_list_init(&pvs); 172 173 if (!mem) 174 return_NULL; 175 176 /* Strip dev_dir if present */ 177 vg_name = strip_dir(vg_name, fid->fmt->cmd->dev_dir); 178 179 if (!read_pvs_in_vg 180 (fid->fmt, vg_name, fid->fmt->cmd->filter, mem, &pvs)) 181 goto_bad; 182 183 if (!(vg = _build_vg(fid, &pvs))) 184 goto_bad; 185 186 bad: 187 dm_pool_destroy(mem); 188 return vg; 189 } 190 191 static struct disk_list *_flatten_pv(struct format_instance *fid, 192 struct dm_pool *mem, struct volume_group *vg, 193 struct physical_volume *pv, 194 const char *dev_dir) 195 { 196 struct disk_list *dl = dm_pool_alloc(mem, sizeof(*dl)); 197 198 if (!dl) 199 return_NULL; 200 201 dl->mem = mem; 202 dl->dev = pv->dev; 203 204 dm_list_init(&dl->uuids); 205 dm_list_init(&dl->lvds); 206 207 if (!export_pv(fid->fmt->cmd, mem, vg, &dl->pvd, pv) || 208 !export_vg(&dl->vgd, vg) || 209 !export_uuids(dl, vg) || 210 !export_lvs(dl, vg, pv, dev_dir) || !calculate_layout(dl)) { 211 dm_pool_free(mem, dl); 212 return_NULL; 213 } 214 215 return dl; 216 } 217 218 static int _flatten_vg(struct format_instance *fid, struct dm_pool *mem, 219 struct volume_group *vg, 220 struct dm_list *pvds, const char *dev_dir, 221 struct dev_filter *filter) 222 { 223 struct pv_list *pvl; 224 struct disk_list *data; 225 226 dm_list_iterate_items(pvl, &vg->pvs) { 227 if (!(data = _flatten_pv(fid, mem, vg, pvl->pv, dev_dir))) 228 return_0; 229 230 dm_list_add(pvds, &data->list); 231 } 232 233 export_numbers(pvds, vg); 234 export_pv_act(pvds); 235 236 if (!export_vg_number(fid, pvds, vg->name, filter)) 237 return_0; 238 239 return 1; 240 } 241 242 static int _format1_vg_write(struct format_instance *fid, struct volume_group *vg, 243 struct metadata_area *mda __attribute((unused))) 244 { 245 struct dm_pool *mem = dm_pool_create("lvm1 vg_write", 1024 * 10); 246 struct dm_list pvds; 247 int r = 0; 248 249 if (!mem) 250 return_0; 251 252 dm_list_init(&pvds); 253 254 r = (_flatten_vg(fid, mem, vg, &pvds, fid->fmt->cmd->dev_dir, 255 fid->fmt->cmd->filter) && 256 write_disks(fid->fmt, &pvds)); 257 258 lvmcache_update_vg(vg, 0); 259 dm_pool_destroy(mem); 260 return r; 261 } 262 263 static int _format1_pv_read(const struct format_type *fmt, const char *pv_name, 264 struct physical_volume *pv, struct dm_list *mdas __attribute((unused))) 265 { 266 struct dm_pool *mem = dm_pool_create("lvm1 pv_read", 1024); 267 struct disk_list *dl; 268 struct device *dev; 269 int r = 0; 270 271 log_very_verbose("Reading physical volume data %s from disk", pv_name); 272 273 if (!mem) 274 return_0; 275 276 if (!(dev = dev_cache_get(pv_name, fmt->cmd->filter))) 277 goto_out; 278 279 if (!(dl = read_disk(fmt, dev, mem, NULL))) 280 goto_out; 281 282 if (!import_pv(fmt, fmt->cmd->mem, dl->dev, NULL, pv, &dl->pvd, &dl->vgd)) 283 goto_out; 284 285 pv->fmt = fmt; 286 287 r = 1; 288 289 out: 290 dm_pool_destroy(mem); 291 return r; 292 } 293 294 static int _format1_pv_setup(const struct format_type *fmt, 295 uint64_t pe_start, uint32_t extent_count, 296 uint32_t extent_size, 297 int pvmetadatacopies __attribute((unused)), 298 uint64_t pvmetadatasize __attribute((unused)), struct dm_list *mdas __attribute((unused)), 299 struct physical_volume *pv, struct volume_group *vg __attribute((unused))) 300 { 301 if (pv->size > MAX_PV_SIZE) 302 pv->size--; 303 if (pv->size > MAX_PV_SIZE) { 304 log_error("Physical volumes cannot be bigger than %s", 305 display_size(fmt->cmd, (uint64_t) MAX_PV_SIZE)); 306 return 0; 307 } 308 309 /* Nothing more to do if extent size isn't provided */ 310 if (!extent_size) 311 return 1; 312 313 /* 314 * This works out pe_start and pe_count. 315 */ 316 if (!calculate_extent_count(pv, extent_size, extent_count, pe_start)) 317 return_0; 318 319 /* Retain existing extent locations exactly */ 320 if (((pe_start || extent_count) && (pe_start != pv->pe_start)) || 321 (extent_count && (extent_count != pv->pe_count))) { 322 log_error("Metadata would overwrite physical extents"); 323 return 0; 324 } 325 326 return 1; 327 } 328 329 static int _format1_lv_setup(struct format_instance *fid, struct logical_volume *lv) 330 { 331 uint64_t max_size = UINT_MAX; 332 333 if (!*lv->lvid.s) 334 lvid_from_lvnum(&lv->lvid, &lv->vg->id, find_free_lvnum(lv)); 335 336 if (lv->le_count > MAX_LE_TOTAL) { 337 log_error("logical volumes cannot contain more than " 338 "%d extents.", MAX_LE_TOTAL); 339 return 0; 340 } 341 if (lv->size > max_size) { 342 log_error("logical volumes cannot be larger than %s", 343 display_size(fid->fmt->cmd, max_size)); 344 return 0; 345 } 346 347 return 1; 348 } 349 350 static int _format1_pv_write(const struct format_type *fmt, struct physical_volume *pv, 351 struct dm_list *mdas __attribute((unused)), int64_t sector __attribute((unused))) 352 { 353 struct dm_pool *mem; 354 struct disk_list *dl; 355 struct dm_list pvs; 356 struct label *label; 357 struct lvmcache_info *info; 358 359 if (!(info = lvmcache_add(fmt->labeller, (char *) &pv->id, pv->dev, 360 pv->vg_name, NULL, 0))) 361 return_0; 362 label = info->label; 363 info->device_size = pv->size << SECTOR_SHIFT; 364 info->fmt = fmt; 365 366 dm_list_init(&info->mdas); 367 368 dm_list_init(&pvs); 369 370 /* Ensure any residual PE structure is gone */ 371 pv->pe_size = pv->pe_count = 0; 372 pv->pe_start = LVM1_PE_ALIGN; 373 374 if (!(mem = dm_pool_create("lvm1 pv_write", 1024))) 375 return_0; 376 377 if (!(dl = dm_pool_alloc(mem, sizeof(*dl)))) 378 goto_bad; 379 380 dl->mem = mem; 381 dl->dev = pv->dev; 382 383 if (!export_pv(fmt->cmd, mem, NULL, &dl->pvd, pv)) 384 goto_bad; 385 386 /* must be set to be able to zero gap after PV structure in 387 dev_write in order to make other disk tools happy */ 388 dl->pvd.pv_on_disk.base = METADATA_BASE; 389 dl->pvd.pv_on_disk.size = PV_SIZE; 390 dl->pvd.pe_on_disk.base = LVM1_PE_ALIGN << SECTOR_SHIFT; 391 392 dm_list_add(&pvs, &dl->list); 393 if (!write_disks(fmt, &pvs)) 394 goto_bad; 395 396 dm_pool_destroy(mem); 397 return 1; 398 399 bad: 400 dm_pool_destroy(mem); 401 return 0; 402 } 403 404 static int _format1_vg_setup(struct format_instance *fid, struct volume_group *vg) 405 { 406 /* just check max_pv and max_lv */ 407 if (!vg->max_lv || vg->max_lv >= MAX_LV) 408 vg->max_lv = MAX_LV - 1; 409 410 if (!vg->max_pv || vg->max_pv >= MAX_PV) 411 vg->max_pv = MAX_PV - 1; 412 413 if (vg->extent_size > MAX_PE_SIZE || vg->extent_size < MIN_PE_SIZE) { 414 log_error("Extent size must be between %s and %s", 415 display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE), 416 display_size(fid->fmt->cmd, (uint64_t) MAX_PE_SIZE)); 417 418 return 0; 419 } 420 421 if (vg->extent_size % MIN_PE_SIZE) { 422 log_error("Extent size must be multiple of %s", 423 display_size(fid->fmt->cmd, (uint64_t) MIN_PE_SIZE)); 424 return 0; 425 } 426 427 /* Redundant? */ 428 if (vg->extent_size & (vg->extent_size - 1)) { 429 log_error("Extent size must be power of 2"); 430 return 0; 431 } 432 433 return 1; 434 } 435 436 static int _format1_segtype_supported(struct format_instance *fid __attribute((unused)), 437 const struct segment_type *segtype) 438 { 439 if (!(segtype->flags & SEG_FORMAT1_SUPPORT)) 440 return_0; 441 442 return 1; 443 } 444 445 static struct metadata_area_ops _metadata_format1_ops = { 446 .vg_read = _format1_vg_read, 447 .vg_write = _format1_vg_write, 448 }; 449 450 static struct format_instance *_format1_create_instance(const struct format_type *fmt, 451 const char *vgname __attribute((unused)), 452 const char *vgid __attribute((unused)), 453 void *private __attribute((unused))) 454 { 455 struct format_instance *fid; 456 struct metadata_area *mda; 457 458 if (!(fid = dm_pool_alloc(fmt->cmd->mem, sizeof(*fid)))) 459 return_NULL; 460 461 fid->fmt = fmt; 462 dm_list_init(&fid->metadata_areas); 463 464 /* Define a NULL metadata area */ 465 if (!(mda = dm_pool_alloc(fmt->cmd->mem, sizeof(*mda)))) { 466 dm_pool_free(fmt->cmd->mem, fid); 467 return_NULL; 468 } 469 470 mda->ops = &_metadata_format1_ops; 471 mda->metadata_locn = NULL; 472 dm_list_add(&fid->metadata_areas, &mda->list); 473 474 return fid; 475 } 476 477 static void _format1_destroy_instance(struct format_instance *fid __attribute((unused))) 478 { 479 return; 480 } 481 482 static void _format1_destroy(const struct format_type *fmt) 483 { 484 dm_free((void *) fmt); 485 } 486 487 static struct format_handler _format1_ops = { 488 .pv_read = _format1_pv_read, 489 .pv_setup = _format1_pv_setup, 490 .pv_write = _format1_pv_write, 491 .lv_setup = _format1_lv_setup, 492 .vg_setup = _format1_vg_setup, 493 .segtype_supported = _format1_segtype_supported, 494 .create_instance = _format1_create_instance, 495 .destroy_instance = _format1_destroy_instance, 496 .destroy = _format1_destroy, 497 }; 498 499 #ifdef LVM1_INTERNAL 500 struct format_type *init_lvm1_format(struct cmd_context *cmd) 501 #else /* Shared */ 502 struct format_type *init_format(struct cmd_context *cmd); 503 struct format_type *init_format(struct cmd_context *cmd) 504 #endif 505 { 506 struct format_type *fmt = dm_malloc(sizeof(*fmt)); 507 508 if (!fmt) 509 return_NULL; 510 511 fmt->cmd = cmd; 512 fmt->ops = &_format1_ops; 513 fmt->name = FMT_LVM1_NAME; 514 fmt->alias = NULL; 515 fmt->orphan_vg_name = FMT_LVM1_ORPHAN_VG_NAME; 516 fmt->features = FMT_RESTRICTED_LVIDS | FMT_ORPHAN_ALLOCATABLE | 517 FMT_RESTRICTED_READAHEAD; 518 fmt->private = NULL; 519 520 if (!(fmt->labeller = lvm1_labeller_create(fmt))) { 521 log_error("Couldn't create lvm1 label handler."); 522 return NULL; 523 } 524 525 if (!(label_register_handler(FMT_LVM1_NAME, fmt->labeller))) { 526 log_error("Couldn't register lvm1 label handler."); 527 return NULL; 528 } 529 530 log_very_verbose("Initialised format: %s", fmt->name); 531 532 return fmt; 533 } 534