1 /* $NetBSD: dev_manager.c,v 1.1.1.1 2008/12/22 00:18:01 haad Exp $ */ 2 3 /* 4 * Copyright (C) 2002-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 "str_list.h" 20 #include "dev_manager.h" 21 #include "lvm-string.h" 22 #include "fs.h" 23 #include "defaults.h" 24 #include "segtype.h" 25 #include "display.h" 26 #include "toolcontext.h" 27 #include "targets.h" 28 #include "config.h" 29 #include "filter.h" 30 #include "activate.h" 31 32 #include <limits.h> 33 #include <dirent.h> 34 35 #define MAX_TARGET_PARAMSIZE 50000 36 #define UUID_PREFIX "LVM-" 37 38 typedef enum { 39 PRELOAD, 40 ACTIVATE, 41 DEACTIVATE, 42 SUSPEND, 43 SUSPEND_WITH_LOCKFS, 44 CLEAN 45 } action_t; 46 47 struct dev_manager { 48 struct dm_pool *mem; 49 50 struct cmd_context *cmd; 51 52 void *target_state; 53 uint32_t pvmove_mirror_count; 54 55 char *vg_name; 56 }; 57 58 struct lv_layer { 59 struct logical_volume *lv; 60 const char *old_name; 61 }; 62 63 static char *_build_dlid(struct dm_pool *mem, const char *lvid, const char *layer) 64 { 65 char *dlid; 66 size_t len; 67 68 if (!layer) 69 layer = ""; 70 71 len = sizeof(UUID_PREFIX) + sizeof(union lvid) + strlen(layer); 72 73 if (!(dlid = dm_pool_alloc(mem, len))) { 74 log_error("_build_dlid: pool allocation failed for %" PRIsize_t 75 " %s %s.", len, lvid, layer); 76 return NULL; 77 } 78 79 sprintf(dlid, UUID_PREFIX "%s%s%s", lvid, (*layer) ? "-" : "", layer); 80 81 return dlid; 82 } 83 84 char *build_dlid(struct dev_manager *dm, const char *lvid, const char *layer) 85 { 86 return _build_dlid(dm->mem, lvid, layer); 87 } 88 89 static int _read_only_lv(struct logical_volume *lv) 90 { 91 return (!(lv->vg->status & LVM_WRITE) || !(lv->status & LVM_WRITE)); 92 } 93 94 /* 95 * Low level device-layer operations. 96 */ 97 static struct dm_task *_setup_task(const char *name, const char *uuid, 98 uint32_t *event_nr, int task) 99 { 100 struct dm_task *dmt; 101 102 if (!(dmt = dm_task_create(task))) 103 return_NULL; 104 105 if (name) 106 dm_task_set_name(dmt, name); 107 108 if (uuid && *uuid) 109 dm_task_set_uuid(dmt, uuid); 110 111 if (event_nr) 112 dm_task_set_event_nr(dmt, *event_nr); 113 114 return dmt; 115 } 116 117 static int _info_run(const char *name, const char *dlid, struct dm_info *info, 118 uint32_t *read_ahead, int mknodes, int with_open_count, 119 int with_read_ahead) 120 { 121 int r = 0; 122 struct dm_task *dmt; 123 int dmtask; 124 125 dmtask = mknodes ? DM_DEVICE_MKNODES : DM_DEVICE_INFO; 126 127 if (!(dmt = _setup_task(name, dlid, 0, dmtask))) 128 return_0; 129 130 if (!with_open_count) 131 if (!dm_task_no_open_count(dmt)) 132 log_error("Failed to disable open_count"); 133 134 if (!dm_task_run(dmt)) 135 goto_out; 136 137 if (!dm_task_get_info(dmt, info)) 138 goto_out; 139 140 if (with_read_ahead) { 141 if (!dm_task_get_read_ahead(dmt, read_ahead)) 142 goto_out; 143 } else if (read_ahead) 144 *read_ahead = DM_READ_AHEAD_NONE; 145 146 r = 1; 147 148 out: 149 dm_task_destroy(dmt); 150 return r; 151 } 152 153 int device_is_usable(dev_t dev) 154 { 155 struct dm_task *dmt; 156 struct dm_info info; 157 const char *name; 158 uint64_t start, length; 159 char *target_type = NULL; 160 char *params; 161 void *next = NULL; 162 int r = 0; 163 164 if (!(dmt = dm_task_create(DM_DEVICE_STATUS))) { 165 log_error("Failed to allocate dm_task struct to check dev status"); 166 return 0; 167 } 168 169 if (!dm_task_set_major(dmt, MAJOR(dev)) || !dm_task_set_minor(dmt, MINOR(dev))) 170 goto_out; 171 172 if (!dm_task_run(dmt)) { 173 log_error("Failed to get state of mapped device"); 174 goto out; 175 } 176 177 if (!dm_task_get_info(dmt, &info)) 178 goto_out; 179 180 if (!info.exists || info.suspended) 181 goto out; 182 183 name = dm_task_get_name(dmt); 184 185 /* FIXME Also check for mirror block_on_error and mpath no paths */ 186 /* For now, we exclude all mirrors */ 187 188 do { 189 next = dm_get_next_target(dmt, next, &start, &length, 190 &target_type, ¶ms); 191 /* Skip if target type doesn't match */ 192 if (target_type && !strcmp(target_type, "mirror")) 193 goto out; 194 } while (next); 195 196 /* FIXME Also check dependencies? */ 197 198 r = 1; 199 200 out: 201 dm_task_destroy(dmt); 202 return r; 203 } 204 205 static int _info(const char *name, const char *dlid, int mknodes, 206 int with_open_count, int with_read_ahead, 207 struct dm_info *info, uint32_t *read_ahead) 208 { 209 if (!mknodes && dlid && *dlid) { 210 if (_info_run(NULL, dlid, info, read_ahead, 0, with_open_count, 211 with_read_ahead) && 212 info->exists) 213 return 1; 214 else if (_info_run(NULL, dlid + sizeof(UUID_PREFIX) - 1, info, 215 read_ahead, 0, with_open_count, 216 with_read_ahead) && 217 info->exists) 218 return 1; 219 } 220 221 if (name) 222 return _info_run(name, NULL, info, read_ahead, mknodes, 223 with_open_count, with_read_ahead); 224 225 return 0; 226 } 227 228 int dev_manager_info(struct dm_pool *mem, const char *name, 229 const struct logical_volume *lv, int with_mknodes, 230 int with_open_count, int with_read_ahead, 231 struct dm_info *info, uint32_t *read_ahead) 232 { 233 const char *dlid; 234 235 if (!(dlid = _build_dlid(mem, lv->lvid.s, NULL))) { 236 log_error("dlid build failed for %s", lv->name); 237 return 0; 238 } 239 240 return _info(name, dlid, with_mknodes, with_open_count, with_read_ahead, 241 info, read_ahead); 242 } 243 244 /* FIXME Interface must cope with multiple targets */ 245 static int _status_run(const char *name, const char *uuid, 246 unsigned long long *s, unsigned long long *l, 247 char **t, uint32_t t_size, char **p, uint32_t p_size) 248 { 249 int r = 0; 250 struct dm_task *dmt; 251 struct dm_info info; 252 void *next = NULL; 253 uint64_t start, length; 254 char *type = NULL; 255 char *params = NULL; 256 257 if (!(dmt = _setup_task(name, uuid, 0, DM_DEVICE_STATUS))) 258 return_0; 259 260 if (!dm_task_no_open_count(dmt)) 261 log_error("Failed to disable open_count"); 262 263 if (!dm_task_run(dmt)) 264 goto_out; 265 266 if (!dm_task_get_info(dmt, &info) || !info.exists) 267 goto_out; 268 269 do { 270 next = dm_get_next_target(dmt, next, &start, &length, 271 &type, ¶ms); 272 if (type) { 273 *s = start; 274 *l = length; 275 /* Make sure things are null terminated */ 276 strncpy(*t, type, t_size); 277 (*t)[t_size - 1] = '\0'; 278 strncpy(*p, params, p_size); 279 (*p)[p_size - 1] = '\0'; 280 281 r = 1; 282 /* FIXME Cope with multiple targets! */ 283 break; 284 } 285 286 } while (next); 287 288 out: 289 dm_task_destroy(dmt); 290 return r; 291 } 292 293 static int _status(const char *name, const char *uuid, 294 unsigned long long *start, unsigned long long *length, 295 char **type, uint32_t type_size, char **params, 296 uint32_t param_size) __attribute__ ((unused)); 297 298 static int _status(const char *name, const char *uuid, 299 unsigned long long *start, unsigned long long *length, 300 char **type, uint32_t type_size, char **params, 301 uint32_t param_size) 302 { 303 if (uuid && *uuid) { 304 if (_status_run(NULL, uuid, start, length, type, 305 type_size, params, param_size) && 306 *params) 307 return 1; 308 else if (_status_run(NULL, uuid + sizeof(UUID_PREFIX) - 1, start, 309 length, type, type_size, params, 310 param_size) && 311 *params) 312 return 1; 313 } 314 315 if (name && _status_run(name, NULL, start, length, type, type_size, 316 params, param_size)) 317 return 1; 318 319 return 0; 320 } 321 322 static int _percent_run(struct dev_manager *dm, const char *name, 323 const char *dlid, 324 const char *target_type, int wait, 325 struct logical_volume *lv, float *percent, 326 uint32_t *event_nr) 327 { 328 int r = 0; 329 struct dm_task *dmt; 330 struct dm_info info; 331 void *next = NULL; 332 uint64_t start, length; 333 char *type = NULL; 334 char *params = NULL; 335 struct dm_list *segh = &lv->segments; 336 struct lv_segment *seg = NULL; 337 struct segment_type *segtype; 338 339 uint64_t total_numerator = 0, total_denominator = 0; 340 341 *percent = -1; 342 343 if (!(dmt = _setup_task(name, dlid, event_nr, 344 wait ? DM_DEVICE_WAITEVENT : DM_DEVICE_STATUS))) 345 return_0; 346 347 if (!dm_task_no_open_count(dmt)) 348 log_error("Failed to disable open_count"); 349 350 if (!dm_task_run(dmt)) 351 goto_out; 352 353 if (!dm_task_get_info(dmt, &info) || !info.exists) 354 goto_out; 355 356 if (event_nr) 357 *event_nr = info.event_nr; 358 359 do { 360 next = dm_get_next_target(dmt, next, &start, &length, &type, 361 ¶ms); 362 if (lv) { 363 if (!(segh = dm_list_next(&lv->segments, segh))) { 364 log_error("Number of segments in active LV %s " 365 "does not match metadata", lv->name); 366 goto out; 367 } 368 seg = dm_list_item(segh, struct lv_segment); 369 } 370 371 if (!type || !params || strcmp(type, target_type)) 372 continue; 373 374 if (!(segtype = get_segtype_from_string(dm->cmd, type))) 375 continue; 376 377 if (segtype->ops->target_percent && 378 !segtype->ops->target_percent(&dm->target_state, dm->mem, 379 dm->cmd, seg, params, 380 &total_numerator, 381 &total_denominator)) 382 goto_out; 383 384 } while (next); 385 386 if (lv && (segh = dm_list_next(&lv->segments, segh))) { 387 log_error("Number of segments in active LV %s does not " 388 "match metadata", lv->name); 389 goto out; 390 } 391 392 if (total_denominator) 393 *percent = (float) total_numerator *100 / total_denominator; 394 else 395 *percent = 100; 396 397 log_debug("LV percent: %f", *percent); 398 r = 1; 399 400 out: 401 dm_task_destroy(dmt); 402 return r; 403 } 404 405 static int _percent(struct dev_manager *dm, const char *name, const char *dlid, 406 const char *target_type, int wait, 407 struct logical_volume *lv, float *percent, 408 uint32_t *event_nr) 409 { 410 if (dlid && *dlid) { 411 if (_percent_run(dm, NULL, dlid, target_type, wait, lv, percent, 412 event_nr)) 413 return 1; 414 else if (_percent_run(dm, NULL, dlid + sizeof(UUID_PREFIX) - 1, 415 target_type, wait, lv, percent, 416 event_nr)) 417 return 1; 418 } 419 420 if (name && _percent_run(dm, name, NULL, target_type, wait, lv, percent, 421 event_nr)) 422 return 1; 423 424 return 0; 425 } 426 427 /* 428 * dev_manager implementation. 429 */ 430 struct dev_manager *dev_manager_create(struct cmd_context *cmd, 431 const char *vg_name) 432 { 433 struct dm_pool *mem; 434 struct dev_manager *dm; 435 436 if (!(mem = dm_pool_create("dev_manager", 16 * 1024))) 437 return_NULL; 438 439 if (!(dm = dm_pool_alloc(mem, sizeof(*dm)))) 440 goto_bad; 441 442 dm->cmd = cmd; 443 dm->mem = mem; 444 445 if (!(dm->vg_name = dm_pool_strdup(dm->mem, vg_name))) 446 goto_bad; 447 448 dm->target_state = NULL; 449 450 return dm; 451 452 bad: 453 dm_pool_destroy(mem); 454 return NULL; 455 } 456 457 void dev_manager_destroy(struct dev_manager *dm) 458 { 459 dm_pool_destroy(dm->mem); 460 } 461 462 void dev_manager_release(void) 463 { 464 dm_lib_release(); 465 } 466 467 void dev_manager_exit(void) 468 { 469 dm_lib_exit(); 470 } 471 472 int dev_manager_snapshot_percent(struct dev_manager *dm, 473 const struct logical_volume *lv, 474 float *percent) 475 { 476 char *name; 477 const char *dlid; 478 479 /* 480 * Build a name for the top layer. 481 */ 482 if (!(name = build_dm_name(dm->mem, lv->vg->name, lv->name, NULL))) 483 return_0; 484 485 if (!(dlid = build_dlid(dm, lv->lvid.s, NULL))) 486 return_0; 487 488 /* 489 * Try and get some info on this device. 490 */ 491 log_debug("Getting device status percentage for %s", name); 492 if (!(_percent(dm, name, dlid, "snapshot", 0, NULL, percent, 493 NULL))) 494 return_0; 495 496 /* FIXME dm_pool_free ? */ 497 498 /* If the snapshot isn't available, percent will be -1 */ 499 return 1; 500 } 501 502 /* FIXME Merge with snapshot_percent, auto-detecting target type */ 503 /* FIXME Cope with more than one target */ 504 int dev_manager_mirror_percent(struct dev_manager *dm, 505 struct logical_volume *lv, int wait, 506 float *percent, uint32_t *event_nr) 507 { 508 char *name; 509 const char *dlid; 510 511 /* 512 * Build a name for the top layer. 513 */ 514 if (!(name = build_dm_name(dm->mem, lv->vg->name, lv->name, NULL))) 515 return_0; 516 517 /* FIXME dm_pool_free ? */ 518 519 if (!(dlid = build_dlid(dm, lv->lvid.s, NULL))) { 520 log_error("dlid build failed for %s", lv->name); 521 return 0; 522 } 523 524 log_debug("Getting device mirror status percentage for %s", name); 525 if (!(_percent(dm, name, dlid, "mirror", wait, lv, percent, 526 event_nr))) 527 return_0; 528 529 return 1; 530 } 531 532 #if 0 533 log_very_verbose("%s %s", sus ? "Suspending" : "Resuming", name); 534 535 log_verbose("Loading %s", dl->name); 536 log_very_verbose("Activating %s read-only", dl->name); 537 log_very_verbose("Activated %s %s %03u:%03u", dl->name, 538 dl->dlid, dl->info.major, dl->info.minor); 539 540 if (_get_flag(dl, VISIBLE)) 541 log_verbose("Removing %s", dl->name); 542 else 543 log_very_verbose("Removing %s", dl->name); 544 545 log_debug("Adding target: %" PRIu64 " %" PRIu64 " %s %s", 546 extent_size * seg->le, extent_size * seg->len, target, params); 547 548 log_debug("Adding target: 0 %" PRIu64 " snapshot-origin %s", 549 dl->lv->size, params); 550 log_debug("Adding target: 0 %" PRIu64 " snapshot %s", size, params); 551 log_debug("Getting device info for %s", dl->name); 552 553 /* Rename? */ 554 if ((suffix = strrchr(dl->dlid + sizeof(UUID_PREFIX) - 1, '-'))) 555 suffix++; 556 new_name = build_dm_name(dm->mem, dm->vg_name, dl->lv->name, 557 suffix); 558 559 static int _belong_to_vg(const char *vgname, const char *name) 560 { 561 const char *v = vgname, *n = name; 562 563 while (*v) { 564 if ((*v != *n) || (*v == '-' && *(++n) != '-')) 565 return 0; 566 v++, n++; 567 } 568 569 if (*n == '-' && *(n + 1) != '-') 570 return 1; 571 else 572 return 0; 573 } 574 575 if (!(snap_seg = find_cow(lv))) 576 return 1; 577 578 old_origin = snap_seg->origin; 579 580 /* Was this the last active snapshot with this origin? */ 581 dm_list_iterate_items(lvl, active_head) { 582 active = lvl->lv; 583 if ((snap_seg = find_cow(active)) && 584 snap_seg->origin == old_origin) { 585 return 1; 586 } 587 } 588 589 #endif 590 591 /*************************/ 592 /* NEW CODE STARTS HERE */ 593 /*************************/ 594 595 int dev_manager_lv_mknodes(const struct logical_volume *lv) 596 { 597 char *name; 598 599 if (!(name = build_dm_name(lv->vg->cmd->mem, lv->vg->name, 600 lv->name, NULL))) 601 return_0; 602 603 return fs_add_lv(lv, name); 604 } 605 606 int dev_manager_lv_rmnodes(const struct logical_volume *lv) 607 { 608 return fs_del_lv(lv); 609 } 610 611 static int _add_dev_to_dtree(struct dev_manager *dm, struct dm_tree *dtree, 612 struct logical_volume *lv, const char *layer) 613 { 614 char *dlid, *name; 615 struct dm_info info; 616 617 if (!(name = build_dm_name(dm->mem, lv->vg->name, lv->name, layer))) 618 return_0; 619 620 if (!(dlid = build_dlid(dm, lv->lvid.s, layer))) 621 return_0; 622 623 log_debug("Getting device info for %s [%s]", name, dlid); 624 if (!_info(name, dlid, 0, 1, 0, &info, NULL)) { 625 log_error("Failed to get info for %s [%s].", name, dlid); 626 return 0; 627 } 628 629 if (info.exists && !dm_tree_add_dev(dtree, info.major, info.minor)) { 630 log_error("Failed to add device (%" PRIu32 ":%" PRIu32") to dtree", 631 info.major, info.minor); 632 return 0; 633 } 634 635 return 1; 636 } 637 638 /* 639 * Add LV and any known dependencies 640 */ 641 static int _add_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree, struct logical_volume *lv) 642 { 643 if (!_add_dev_to_dtree(dm, dtree, lv, NULL)) 644 return_0; 645 646 /* FIXME Can we avoid doing this every time? */ 647 if (!_add_dev_to_dtree(dm, dtree, lv, "real")) 648 return_0; 649 650 if (!_add_dev_to_dtree(dm, dtree, lv, "cow")) 651 return_0; 652 653 if (!_add_dev_to_dtree(dm, dtree, lv, "_mlog")) 654 return_0; 655 656 return 1; 657 } 658 659 static struct dm_tree *_create_partial_dtree(struct dev_manager *dm, struct logical_volume *lv) 660 { 661 struct dm_tree *dtree; 662 struct dm_list *snh, *snht; 663 struct lv_segment *seg; 664 uint32_t s; 665 666 if (!(dtree = dm_tree_create())) { 667 log_error("Partial dtree creation failed for %s.", lv->name); 668 return NULL; 669 } 670 671 if (!_add_lv_to_dtree(dm, dtree, lv)) 672 goto_bad; 673 674 /* Add any snapshots of this LV */ 675 dm_list_iterate_safe(snh, snht, &lv->snapshot_segs) 676 if (!_add_lv_to_dtree(dm, dtree, dm_list_struct_base(snh, struct lv_segment, origin_list)->cow)) 677 goto_bad; 678 679 /* Add any LVs used by segments in this LV */ 680 dm_list_iterate_items(seg, &lv->segments) 681 for (s = 0; s < seg->area_count; s++) 682 if (seg_type(seg, s) == AREA_LV && seg_lv(seg, s)) { 683 if (!_add_lv_to_dtree(dm, dtree, seg_lv(seg, s))) 684 goto_bad; 685 } 686 687 return dtree; 688 689 bad: 690 dm_tree_free(dtree); 691 return NULL; 692 } 693 694 static char *_add_error_device(struct dev_manager *dm, struct dm_tree *dtree, 695 struct lv_segment *seg, int s) 696 { 697 char *id, *name; 698 char errid[32]; 699 struct dm_tree_node *node; 700 struct lv_segment *seg_i; 701 int segno = -1, i = 0;; 702 uint64_t size = seg->len * seg->lv->vg->extent_size; 703 704 dm_list_iterate_items(seg_i, &seg->lv->segments) { 705 if (seg == seg_i) 706 segno = i; 707 ++i; 708 } 709 710 if (segno < 0) { 711 log_error("_add_error_device called with bad segment"); 712 return_NULL; 713 } 714 715 sprintf(errid, "missing_%d_%d", segno, s); 716 717 if (!(id = build_dlid(dm, seg->lv->lvid.s, errid))) 718 return_NULL; 719 720 if (!(name = build_dm_name(dm->mem, seg->lv->vg->name, 721 seg->lv->name, errid))) 722 return_NULL; 723 if (!(node = dm_tree_add_new_dev(dtree, name, id, 0, 0, 0, 0, 0))) 724 return_NULL; 725 if (!dm_tree_node_add_error_target(node, size)) 726 return_NULL; 727 728 return id; 729 } 730 731 static int _add_error_area(struct dev_manager *dm, struct dm_tree_node *node, 732 struct lv_segment *seg, int s) 733 { 734 char *dlid; 735 uint64_t extent_size = seg->lv->vg->extent_size; 736 737 if (!strcmp(dm->cmd->stripe_filler, "error")) { 738 /* 739 * FIXME, the tree pointer is first field of dm_tree_node, but 740 * we don't have the struct definition available. 741 */ 742 struct dm_tree **tree = (struct dm_tree **) node; 743 dlid = _add_error_device(dm, *tree, seg, s); 744 if (!dlid) 745 return_0; 746 dm_tree_node_add_target_area(node, NULL, dlid, 747 extent_size * seg_le(seg, s)); 748 } else 749 dm_tree_node_add_target_area(node, 750 dm->cmd->stripe_filler, 751 NULL, UINT64_C(0)); 752 753 return 1; 754 } 755 756 int add_areas_line(struct dev_manager *dm, struct lv_segment *seg, 757 struct dm_tree_node *node, uint32_t start_area, 758 uint32_t areas) 759 { 760 uint64_t extent_size = seg->lv->vg->extent_size; 761 uint32_t s; 762 char *dlid; 763 764 for (s = start_area; s < areas; s++) { 765 if ((seg_type(seg, s) == AREA_PV && 766 (!seg_pvseg(seg, s) || 767 !seg_pv(seg, s) || 768 !seg_dev(seg, s))) || 769 (seg_type(seg, s) == AREA_LV && !seg_lv(seg, s))) { 770 if (!_add_error_area(dm, node, seg, s)) 771 return_0; 772 } else if (seg_type(seg, s) == AREA_PV) 773 dm_tree_node_add_target_area(node, 774 dev_name(seg_dev(seg, s)), 775 NULL, 776 (seg_pv(seg, s)->pe_start + 777 (extent_size * seg_pe(seg, s)))); 778 else if (seg_type(seg, s) == AREA_LV) { 779 if (!(dlid = build_dlid(dm, 780 seg_lv(seg, s)->lvid.s, 781 NULL))) 782 return_0; 783 dm_tree_node_add_target_area(node, NULL, dlid, 784 extent_size * seg_le(seg, s)); 785 } else { 786 log_error("Internal error: Unassigned area found in LV %s.", 787 seg->lv->name); 788 return 0; 789 } 790 } 791 792 return 1; 793 } 794 795 static int _add_origin_target_to_dtree(struct dev_manager *dm, 796 struct dm_tree_node *dnode, 797 struct logical_volume *lv) 798 { 799 const char *real_dlid; 800 801 if (!(real_dlid = build_dlid(dm, lv->lvid.s, "real"))) 802 return_0; 803 804 if (!dm_tree_node_add_snapshot_origin_target(dnode, lv->size, real_dlid)) 805 return_0; 806 807 return 1; 808 } 809 810 static int _add_snapshot_target_to_dtree(struct dev_manager *dm, 811 struct dm_tree_node *dnode, 812 struct logical_volume *lv) 813 { 814 const char *origin_dlid; 815 const char *cow_dlid; 816 struct lv_segment *snap_seg; 817 uint64_t size; 818 819 if (!(snap_seg = find_cow(lv))) { 820 log_error("Couldn't find snapshot for '%s'.", lv->name); 821 return 0; 822 } 823 824 if (!(origin_dlid = build_dlid(dm, snap_seg->origin->lvid.s, "real"))) 825 return_0; 826 827 if (!(cow_dlid = build_dlid(dm, snap_seg->cow->lvid.s, "cow"))) 828 return_0; 829 830 size = (uint64_t) snap_seg->len * snap_seg->origin->vg->extent_size; 831 832 if (!dm_tree_node_add_snapshot_target(dnode, size, origin_dlid, cow_dlid, 1, snap_seg->chunk_size)) 833 return_0; 834 835 return 1; 836 } 837 838 static int _add_target_to_dtree(struct dev_manager *dm, 839 struct dm_tree_node *dnode, 840 struct lv_segment *seg) 841 { 842 uint64_t extent_size = seg->lv->vg->extent_size; 843 844 if (!seg->segtype->ops->add_target_line) { 845 log_error("_emit_target: Internal error: Can't handle " 846 "segment type %s", seg->segtype->name); 847 return 0; 848 } 849 850 return seg->segtype->ops->add_target_line(dm, dm->mem, dm->cmd, 851 &dm->target_state, seg, 852 dnode, 853 extent_size * seg->len, 854 &dm-> pvmove_mirror_count); 855 } 856 857 static int _add_new_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree, 858 struct logical_volume *lv, const char *layer); 859 860 static int _add_segment_to_dtree(struct dev_manager *dm, 861 struct dm_tree *dtree, 862 struct dm_tree_node *dnode, 863 struct lv_segment *seg, 864 const char *layer) 865 { 866 uint32_t s; 867 struct dm_list *snh; 868 struct lv_segment *seg_present; 869 870 /* Ensure required device-mapper targets are loaded */ 871 seg_present = find_cow(seg->lv) ? : seg; 872 873 log_debug("Checking kernel supports %s segment type for %s%s%s", 874 seg_present->segtype->name, seg->lv->name, 875 layer ? "-" : "", layer ? : ""); 876 877 if (seg_present->segtype->ops->target_present && 878 !seg_present->segtype->ops->target_present(seg_present, NULL)) { 879 log_error("Can't expand LV %s: %s target support missing " 880 "from kernel?", seg->lv->name, seg_present->segtype->name); 881 return 0; 882 } 883 884 /* Add mirror log */ 885 if (seg->log_lv && 886 !_add_new_lv_to_dtree(dm, dtree, seg->log_lv, NULL)) 887 return_0; 888 889 /* If this is a snapshot origin, add real LV */ 890 if (lv_is_origin(seg->lv) && !layer) { 891 if (vg_is_clustered(seg->lv->vg)) { 892 log_error("Clustered snapshots are not yet supported"); 893 return 0; 894 } 895 if (!_add_new_lv_to_dtree(dm, dtree, seg->lv, "real")) 896 return_0; 897 } else if (lv_is_cow(seg->lv) && !layer) { 898 if (!_add_new_lv_to_dtree(dm, dtree, seg->lv, "cow")) 899 return_0; 900 } else { 901 /* Add any LVs used by this segment */ 902 for (s = 0; s < seg->area_count; s++) 903 if ((seg_type(seg, s) == AREA_LV) && 904 (!_add_new_lv_to_dtree(dm, dtree, seg_lv(seg, s), NULL))) 905 return_0; 906 } 907 908 /* Now we've added its dependencies, we can add the target itself */ 909 if (lv_is_origin(seg->lv) && !layer) { 910 if (!_add_origin_target_to_dtree(dm, dnode, seg->lv)) 911 return_0; 912 } else if (lv_is_cow(seg->lv) && !layer) { 913 if (!_add_snapshot_target_to_dtree(dm, dnode, seg->lv)) 914 return_0; 915 } else if (!_add_target_to_dtree(dm, dnode, seg)) 916 return_0; 917 918 if (lv_is_origin(seg->lv) && !layer) 919 /* Add any snapshots of this LV */ 920 dm_list_iterate(snh, &seg->lv->snapshot_segs) 921 if (!_add_new_lv_to_dtree(dm, dtree, dm_list_struct_base(snh, struct lv_segment, origin_list)->cow, NULL)) 922 return_0; 923 924 return 1; 925 } 926 927 static int _add_new_lv_to_dtree(struct dev_manager *dm, struct dm_tree *dtree, 928 struct logical_volume *lv, const char *layer) 929 { 930 struct lv_segment *seg; 931 struct lv_layer *lvlayer; 932 struct dm_tree_node *dnode; 933 char *name, *dlid; 934 uint32_t max_stripe_size = UINT32_C(0); 935 uint32_t read_ahead = lv->read_ahead; 936 uint32_t read_ahead_flags = UINT32_C(0); 937 938 if (!(name = build_dm_name(dm->mem, lv->vg->name, lv->name, layer))) 939 return_0; 940 941 if (!(dlid = build_dlid(dm, lv->lvid.s, layer))) 942 return_0; 943 944 /* We've already processed this node if it already has a context ptr */ 945 if ((dnode = dm_tree_find_node_by_uuid(dtree, dlid)) && 946 dm_tree_node_get_context(dnode)) 947 return 1; 948 949 if (!(lvlayer = dm_pool_alloc(dm->mem, sizeof(*lvlayer)))) { 950 log_error("_add_new_lv_to_dtree: pool alloc failed for %s %s.", lv->name, layer); 951 return 0; 952 } 953 954 lvlayer->lv = lv; 955 956 /* 957 * Add LV to dtree. 958 * If we're working with precommitted metadata, clear any 959 * existing inactive table left behind. 960 * Major/minor settings only apply to the visible layer. 961 */ 962 if (!(dnode = dm_tree_add_new_dev(dtree, name, dlid, 963 layer ? UINT32_C(0) : (uint32_t) lv->major, 964 layer ? UINT32_C(0) : (uint32_t) lv->minor, 965 _read_only_lv(lv), 966 (lv->vg->status & PRECOMMITTED) ? 1 : 0, 967 lvlayer))) 968 return_0; 969 970 /* Store existing name so we can do rename later */ 971 lvlayer->old_name = dm_tree_node_get_name(dnode); 972 973 /* Create table */ 974 dm->pvmove_mirror_count = 0u; 975 dm_list_iterate_items(seg, &lv->segments) { 976 if (!_add_segment_to_dtree(dm, dtree, dnode, seg, layer)) 977 return_0; 978 /* These aren't real segments in the LVM2 metadata */ 979 if (lv_is_origin(lv) && !layer) 980 break; 981 if (lv_is_cow(lv) && !layer) 982 break; 983 if (max_stripe_size < seg->stripe_size * seg->area_count) 984 max_stripe_size = seg->stripe_size * seg->area_count; 985 } 986 987 if (read_ahead == DM_READ_AHEAD_AUTO) { 988 /* we need RA at least twice a whole stripe - see the comment in md/raid0.c */ 989 read_ahead = max_stripe_size * 2; 990 read_ahead_flags = DM_READ_AHEAD_MINIMUM_FLAG; 991 } 992 993 dm_tree_node_set_read_ahead(dnode, read_ahead, read_ahead_flags); 994 995 return 1; 996 } 997 998 /* FIXME: symlinks should be created/destroyed at the same time 999 * as the kernel devices but we can't do that from within libdevmapper 1000 * at present so we must walk the tree twice instead. */ 1001 1002 /* 1003 * Create LV symlinks for children of supplied root node. 1004 */ 1005 static int _create_lv_symlinks(struct dev_manager *dm, struct dm_tree_node *root) 1006 { 1007 void *handle = NULL; 1008 struct dm_tree_node *child; 1009 struct lv_layer *lvlayer; 1010 char *vgname, *lvname, *layer; 1011 const char *name; 1012 int r = 1; 1013 1014 while ((child = dm_tree_next_child(&handle, root, 0))) { 1015 if (!(lvlayer = (struct lv_layer *) dm_tree_node_get_context(child))) 1016 continue; 1017 1018 /* Detect rename */ 1019 name = dm_tree_node_get_name(child); 1020 1021 if (name && lvlayer->old_name && *lvlayer->old_name && strcmp(name, lvlayer->old_name)) { 1022 if (!dm_split_lvm_name(dm->mem, lvlayer->old_name, &vgname, &lvname, &layer)) { 1023 log_error("_create_lv_symlinks: Couldn't split up old device name %s", lvlayer->old_name); 1024 return 0; 1025 } 1026 fs_rename_lv(lvlayer->lv, name, lvname); 1027 } else if (!dev_manager_lv_mknodes(lvlayer->lv)) 1028 r = 0; 1029 } 1030 1031 return r; 1032 } 1033 1034 /* 1035 * Remove LV symlinks for children of supplied root node. 1036 */ 1037 static int _remove_lv_symlinks(struct dev_manager *dm, struct dm_tree_node *root) 1038 { 1039 void *handle = NULL; 1040 struct dm_tree_node *child; 1041 char *vgname, *lvname, *layer; 1042 int r = 1; 1043 1044 while ((child = dm_tree_next_child(&handle, root, 0))) { 1045 if (!dm_split_lvm_name(dm->mem, dm_tree_node_get_name(child), &vgname, &lvname, &layer)) { 1046 r = 0; 1047 continue; 1048 } 1049 1050 if (!*vgname) 1051 continue; 1052 1053 /* only top level layer has symlinks */ 1054 if (*layer) 1055 continue; 1056 1057 fs_del_lv_byname(dm->cmd->dev_dir, vgname, lvname); 1058 } 1059 1060 return r; 1061 } 1062 1063 static int _clean_tree(struct dev_manager *dm, struct dm_tree_node *root) 1064 { 1065 void *handle = NULL; 1066 struct dm_tree_node *child; 1067 char *vgname, *lvname, *layer; 1068 const char *name, *uuid; 1069 1070 while ((child = dm_tree_next_child(&handle, root, 0))) { 1071 if (!(name = dm_tree_node_get_name(child))) 1072 continue; 1073 1074 if (!(uuid = dm_tree_node_get_uuid(child))) 1075 continue; 1076 1077 if (!dm_split_lvm_name(dm->mem, name, &vgname, &lvname, &layer)) { 1078 log_error("_clean_tree: Couldn't split up device name %s.", name); 1079 return 0; 1080 } 1081 1082 /* Not meant to be top level? */ 1083 if (!*layer) 1084 continue; 1085 1086 if (!dm_tree_deactivate_children(root, uuid, strlen(uuid))) 1087 return_0; 1088 } 1089 1090 return 1; 1091 } 1092 1093 static int _tree_action(struct dev_manager *dm, struct logical_volume *lv, action_t action) 1094 { 1095 struct dm_tree *dtree; 1096 struct dm_tree_node *root; 1097 char *dlid; 1098 int r = 0; 1099 1100 if (!(dtree = _create_partial_dtree(dm, lv))) 1101 return_0; 1102 1103 if (!(root = dm_tree_find_node(dtree, 0, 0))) { 1104 log_error("Lost dependency tree root node"); 1105 goto out; 1106 } 1107 1108 if (!(dlid = build_dlid(dm, lv->lvid.s, NULL))) 1109 goto_out; 1110 1111 /* Only process nodes with uuid of "LVM-" plus VG id. */ 1112 switch(action) { 1113 case CLEAN: 1114 /* Deactivate any unused non-toplevel nodes */ 1115 if (!_clean_tree(dm, root)) 1116 goto_out; 1117 break; 1118 case DEACTIVATE: 1119 /* Deactivate LV and all devices it references that nothing else has open. */ 1120 if (!dm_tree_deactivate_children(root, dlid, ID_LEN + sizeof(UUID_PREFIX) - 1)) 1121 goto_out; 1122 if (!_remove_lv_symlinks(dm, root)) 1123 log_error("Failed to remove all device symlinks associated with %s.", lv->name); 1124 break; 1125 case SUSPEND: 1126 dm_tree_skip_lockfs(root); 1127 if ((lv->status & MIRRORED) && !(lv->status & PVMOVE)) 1128 dm_tree_use_no_flush_suspend(root); 1129 case SUSPEND_WITH_LOCKFS: 1130 if (!dm_tree_suspend_children(root, dlid, ID_LEN + sizeof(UUID_PREFIX) - 1)) 1131 goto_out; 1132 break; 1133 case PRELOAD: 1134 case ACTIVATE: 1135 /* Add all required new devices to tree */ 1136 if (!_add_new_lv_to_dtree(dm, dtree, lv, NULL)) 1137 goto_out; 1138 1139 /* Preload any devices required before any suspensions */ 1140 if (!dm_tree_preload_children(root, dlid, ID_LEN + sizeof(UUID_PREFIX) - 1)) 1141 goto_out; 1142 1143 if ((action == ACTIVATE) && 1144 !dm_tree_activate_children(root, dlid, ID_LEN + sizeof(UUID_PREFIX) - 1)) 1145 goto_out; 1146 1147 if (!_create_lv_symlinks(dm, root)) { 1148 log_error("Failed to create symlinks for %s.", lv->name); 1149 goto out; 1150 } 1151 break; 1152 default: 1153 log_error("_tree_action: Action %u not supported.", action); 1154 goto out; 1155 } 1156 1157 r = 1; 1158 1159 out: 1160 dm_tree_free(dtree); 1161 1162 return r; 1163 } 1164 1165 int dev_manager_activate(struct dev_manager *dm, struct logical_volume *lv) 1166 { 1167 if (!_tree_action(dm, lv, ACTIVATE)) 1168 return_0; 1169 1170 return _tree_action(dm, lv, CLEAN); 1171 } 1172 1173 int dev_manager_preload(struct dev_manager *dm, struct logical_volume *lv) 1174 { 1175 /* FIXME Update the pvmove implementation! */ 1176 if ((lv->status & PVMOVE) || (lv->status & LOCKED)) 1177 return 1; 1178 1179 return _tree_action(dm, lv, PRELOAD); 1180 } 1181 1182 int dev_manager_deactivate(struct dev_manager *dm, struct logical_volume *lv) 1183 { 1184 int r; 1185 1186 r = _tree_action(dm, lv, DEACTIVATE); 1187 1188 fs_del_lv(lv); 1189 1190 return r; 1191 } 1192 1193 int dev_manager_suspend(struct dev_manager *dm, struct logical_volume *lv, 1194 int lockfs) 1195 { 1196 return _tree_action(dm, lv, lockfs ? SUSPEND_WITH_LOCKFS : SUSPEND); 1197 } 1198 1199 /* 1200 * Does device use VG somewhere in its construction? 1201 * Returns 1 if uncertain. 1202 */ 1203 int dev_manager_device_uses_vg(struct device *dev, 1204 struct volume_group *vg) 1205 { 1206 struct dm_tree *dtree; 1207 struct dm_tree_node *root; 1208 char dlid[sizeof(UUID_PREFIX) + sizeof(struct id) - 1] __attribute((aligned(8))); 1209 int r = 1; 1210 1211 if (!(dtree = dm_tree_create())) { 1212 log_error("partial dtree creation failed"); 1213 return r; 1214 } 1215 1216 if (!dm_tree_add_dev(dtree, (uint32_t) MAJOR(dev->dev), (uint32_t) MINOR(dev->dev))) { 1217 log_error("Failed to add device %s (%" PRIu32 ":%" PRIu32") to dtree", 1218 dev_name(dev), (uint32_t) MAJOR(dev->dev), (uint32_t) MINOR(dev->dev)); 1219 goto out; 1220 } 1221 1222 memcpy(dlid, UUID_PREFIX, sizeof(UUID_PREFIX) - 1); 1223 memcpy(dlid + sizeof(UUID_PREFIX) - 1, &vg->id.uuid[0], sizeof(vg->id)); 1224 1225 if (!(root = dm_tree_find_node(dtree, 0, 0))) { 1226 log_error("Lost dependency tree root node"); 1227 goto out; 1228 } 1229 1230 if (dm_tree_children_use_uuid(root, dlid, sizeof(UUID_PREFIX) + sizeof(vg->id) - 1)) 1231 goto_out; 1232 1233 r = 0; 1234 1235 out: 1236 dm_tree_free(dtree); 1237 return r; 1238 } 1239