1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * Snapshot Library Interfaces 30 * 31 * Consumers of topology data may use the interfaces in this file to open, 32 * snapshot and close a topology exported by FMRI scheme (hc, mem and cpu) 33 * builtin plugins and their helper modules. A topology handle is obtained 34 * by calling topo_open(). Upon a successful return, the caller may use this 35 * handle to open a new snapshot. Each snapshot is assigned a Universally 36 * Unique Identifier that in a future enchancement to the libtopo API will be 37 * used as the file locator in /var/fm/topo to persist new snapshots or lookup 38 * a previously captured snapshot. topo_snap_hold() will capture the current 39 * system topology. All consumers of the topo_hdl_t argument will be 40 * blocked from accessing the topology trees until the snapshot completes. 41 * 42 * A snapshot may be cleared by calling topo_snap_rele(). As with 43 * topo_snap_hold(), all topology accesses are blocked until the topology 44 * trees have been released and deallocated. 45 * 46 * Walker Library Interfaces 47 * 48 * Once a snapshot has been taken with topo_snap_hold(), topo_hdl_t holders 49 * may initiate topology tree walks on a scheme-tree basis. topo_walk_init() 50 * will initiate the data structures required to walk any one one of the 51 * FMRI scheme trees. The walker data structure, topo_walk_t, is an opaque 52 * handle passed to topo_walk_step to begin the walk. At each node in the 53 * topology tree, a callback function is called with access to the node at 54 * which our current walk falls. The callback function is passed in during 55 * calls to topo_walk_init() and used throughout the walk_step of the 56 * scheme tree. At any time, the callback may terminate the walk by returning 57 * TOPO_WALK_TERMINATE or TOPO_WALK_ERR. TOPO_WALK_NEXT will continue the walk. 58 * 59 * The type of walk through the tree may be breadth first or depth first by 60 * respectively passing in TOPO_WALK_SIBLING or TOPO_WALK_CHILD to 61 * the topo_walk_step() function. Topology nodes 62 * associated with an outstanding walk are held in place and will not be 63 * deallocated until the walk through that node completes. 64 * 65 * Once the walk has terminated, the walking process should call 66 * topo_walk_fini() to clean-up resources created in topo_walk_init() 67 * and release nodes that may be still held. 68 */ 69 70 #include <alloca.h> 71 #include <ctype.h> 72 #include <pthread.h> 73 #include <limits.h> 74 #include <assert.h> 75 #include <fcntl.h> 76 #include <smbios.h> 77 #include <sys/param.h> 78 #include <sys/types.h> 79 #include <sys/stat.h> 80 #include <sys/systeminfo.h> 81 #include <sys/utsname.h> 82 #include <uuid/uuid.h> 83 84 #include <fm/libtopo.h> 85 86 #include <topo_alloc.h> 87 #include <topo_builtin.h> 88 #include <topo_string.h> 89 #include <topo_error.h> 90 #include <topo_subr.h> 91 92 static void topo_snap_destroy(topo_hdl_t *); 93 94 static topo_hdl_t * 95 set_open_errno(topo_hdl_t *thp, int *errp, int err) 96 { 97 if (thp != NULL) { 98 topo_close(thp); 99 } 100 if (errp != NULL) 101 *errp = err; 102 return (NULL); 103 } 104 105 topo_hdl_t * 106 topo_open(int version, const char *rootdir, int *errp) 107 { 108 topo_hdl_t *thp = NULL; 109 topo_alloc_t *tap; 110 111 char platform[MAXNAMELEN]; 112 char isa[MAXNAMELEN]; 113 struct utsname uts; 114 struct stat st; 115 116 smbios_hdl_t *shp; 117 smbios_system_t s1; 118 smbios_info_t s2; 119 id_t id; 120 121 char *dbflags, *dbout; 122 123 if (version != TOPO_VERSION) 124 return (set_open_errno(thp, errp, ETOPO_HDL_ABIVER)); 125 126 if (rootdir != NULL && stat(rootdir, &st) < 0) 127 return (set_open_errno(thp, errp, ETOPO_HDL_INVAL)); 128 129 if ((thp = topo_zalloc(sizeof (topo_hdl_t), 0)) == NULL) 130 return (set_open_errno(thp, errp, ETOPO_NOMEM)); 131 132 (void) pthread_mutex_init(&thp->th_lock, NULL); 133 134 if ((tap = topo_zalloc(sizeof (topo_alloc_t), 0)) == NULL) 135 return (set_open_errno(thp, errp, ETOPO_NOMEM)); 136 137 /* 138 * Install default allocators 139 */ 140 tap->ta_flags = 0; 141 tap->ta_alloc = topo_alloc; 142 tap->ta_zalloc = topo_zalloc; 143 tap->ta_free = topo_free; 144 tap->ta_nvops.nv_ao_alloc = topo_nv_alloc; 145 tap->ta_nvops.nv_ao_free = topo_nv_free; 146 (void) nv_alloc_init(&tap->ta_nva, &tap->ta_nvops); 147 thp->th_alloc = tap; 148 149 if ((thp->th_modhash = topo_modhash_create(thp)) == NULL) 150 return (set_open_errno(thp, errp, ETOPO_NOMEM)); 151 152 /* 153 * Set-up system information and search paths for modules 154 * and topology map files 155 */ 156 if (rootdir == NULL) { 157 rootdir = topo_hdl_strdup(thp, "/"); 158 thp->th_rootdir = (char *)rootdir; 159 } else { 160 int len; 161 char *rpath; 162 163 len = strlen(rootdir); 164 if (len >= PATH_MAX) 165 return (set_open_errno(thp, errp, EINVAL)); 166 167 if (rootdir[len] != '/') { 168 rpath = alloca(len + 1); 169 (void) snprintf(rpath, len + 1, "%s/", rootdir); 170 } else { 171 rpath = (char *)rootdir; 172 } 173 thp->th_rootdir = topo_hdl_strdup(thp, rpath); 174 } 175 176 platform[0] = '\0'; 177 isa[0] = '\0'; 178 (void) sysinfo(SI_PLATFORM, platform, sizeof (platform)); 179 (void) sysinfo(SI_ARCHITECTURE, isa, sizeof (isa)); 180 (void) uname(&uts); 181 thp->th_platform = topo_hdl_strdup(thp, platform); 182 thp->th_isa = topo_hdl_strdup(thp, isa); 183 thp->th_machine = topo_hdl_strdup(thp, uts.machine); 184 if ((shp = smbios_open(NULL, SMB_VERSION, 0, NULL)) != NULL) { 185 if ((id = smbios_info_system(shp, &s1)) != SMB_ERR && 186 smbios_info_common(shp, id, &s2) != SMB_ERR) { 187 188 if (strcmp(s2.smbi_product, SMB_DEFAULT1) != 0 && 189 strcmp(s2.smbi_product, SMB_DEFAULT2) != 0) { 190 thp->th_product = topo_cleanup_auth_str(thp, 191 (char *)s2.smbi_product); 192 } 193 } 194 smbios_close(shp); 195 } else { 196 thp->th_product = topo_hdl_strdup(thp, thp->th_platform); 197 } 198 199 if (thp->th_rootdir == NULL) 200 return (set_open_errno(thp, errp, ETOPO_NOMEM)); 201 202 dbflags = getenv("TOPO_DEBUG"); 203 dbout = getenv("TOPO_DEBUG_OUT"); 204 if (dbflags != NULL) 205 topo_debug_set(thp, dbflags, dbout); 206 207 if (topo_builtin_create(thp, thp->th_rootdir) != 0) { 208 topo_dprintf(thp, TOPO_DBG_ERR, 209 "failed to load builtin modules: %s\n", 210 topo_hdl_errmsg(thp)); 211 topo_close(thp); 212 return (NULL); 213 } 214 215 return (thp); 216 } 217 218 void 219 topo_close(topo_hdl_t *thp) 220 { 221 ttree_t *tp; 222 223 topo_hdl_lock(thp); 224 if (thp->th_platform != NULL) 225 topo_hdl_strfree(thp, thp->th_platform); 226 if (thp->th_isa != NULL) 227 topo_hdl_strfree(thp, thp->th_isa); 228 if (thp->th_machine != NULL) 229 topo_hdl_strfree(thp, thp->th_machine); 230 if (thp->th_product != NULL) 231 topo_hdl_strfree(thp, thp->th_product); 232 if (thp->th_rootdir != NULL) 233 topo_hdl_strfree(thp, thp->th_rootdir); 234 235 /* 236 * Clean-up snapshot 237 */ 238 topo_snap_destroy(thp); 239 240 /* 241 * Clean-up trees 242 */ 243 while ((tp = topo_list_next(&thp->th_trees)) != NULL) { 244 topo_list_delete(&thp->th_trees, tp); 245 topo_tree_destroy(tp); 246 } 247 248 /* 249 * Unload all plugins 250 */ 251 topo_modhash_unload_all(thp); 252 253 if (thp->th_modhash != NULL) 254 topo_modhash_destroy(thp); 255 if (thp->th_alloc != NULL) 256 topo_free(thp->th_alloc, sizeof (topo_alloc_t)); 257 258 topo_hdl_unlock(thp); 259 260 topo_free(thp, sizeof (topo_hdl_t)); 261 } 262 263 static char * 264 topo_snap_create(topo_hdl_t *thp, int *errp) 265 { 266 uuid_t uuid; 267 char *ustr = NULL; 268 269 topo_hdl_lock(thp); 270 if (thp->th_uuid != NULL) { 271 *errp = ETOPO_HDL_UUID; 272 topo_hdl_unlock(thp); 273 return (NULL); 274 } 275 276 if ((thp->th_uuid = topo_hdl_zalloc(thp, TOPO_UUID_SIZE)) == NULL) { 277 *errp = ETOPO_NOMEM; 278 topo_dprintf(thp, TOPO_DBG_ERR, "unable to allocate uuid: %s\n", 279 topo_strerror(*errp)); 280 topo_hdl_unlock(thp); 281 return (NULL); 282 } 283 284 uuid_generate(uuid); 285 uuid_unparse(uuid, thp->th_uuid); 286 287 if (topo_tree_enum_all(thp) < 0) { 288 topo_dprintf(thp, TOPO_DBG_ERR, "enumeration failure: %s\n", 289 topo_hdl_errmsg(thp)); 290 if (topo_hdl_errno(thp) == ETOPO_ENUM_FATAL) { 291 *errp = thp->th_errno; 292 topo_hdl_unlock(thp); 293 return (NULL); 294 } 295 } 296 297 if ((ustr = topo_hdl_strdup(thp, thp->th_uuid)) == NULL) 298 *errp = ETOPO_NOMEM; 299 300 thp->th_di = DI_NODE_NIL; 301 thp->th_pi = DI_PROM_HANDLE_NIL; 302 303 topo_hdl_unlock(thp); 304 305 return (ustr); 306 } 307 308 /*ARGSUSED*/ 309 static char * 310 topo_snap_log_create(topo_hdl_t *thp, const char *uuid, int *errp) 311 { 312 return ((char *)uuid); 313 } 314 315 /* 316 * Return snapshot id 317 */ 318 char * 319 topo_snap_hold(topo_hdl_t *thp, const char *uuid, int *errp) 320 { 321 if (thp == NULL) 322 return (NULL); 323 324 if (uuid == NULL) 325 return (topo_snap_create(thp, errp)); 326 else 327 return (topo_snap_log_create(thp, uuid, errp)); 328 } 329 330 /*ARGSUSED*/ 331 static int 332 topo_walk_destroy(topo_hdl_t *thp, tnode_t *node, void *notused) 333 { 334 tnode_t *cnode; 335 336 cnode = topo_child_first(node); 337 338 if (cnode != NULL) 339 return (TOPO_WALK_NEXT); 340 341 topo_node_unbind(node); 342 343 return (TOPO_WALK_NEXT); 344 } 345 346 static void 347 topo_snap_destroy(topo_hdl_t *thp) 348 { 349 int i; 350 ttree_t *tp; 351 topo_walk_t *twp; 352 tnode_t *root; 353 topo_nodehash_t *nhp; 354 topo_mod_t *mod; 355 356 for (tp = topo_list_next(&thp->th_trees); tp != NULL; 357 tp = topo_list_next(tp)) { 358 359 root = tp->tt_root; 360 twp = tp->tt_walk; 361 /* 362 * Clean-up tree nodes from the bottom-up 363 */ 364 if ((twp->tw_node = topo_child_first(root)) != NULL) { 365 twp->tw_cb = topo_walk_destroy; 366 topo_node_hold(root); 367 topo_node_hold(twp->tw_node); /* released at walk end */ 368 (void) topo_walk_bottomup(twp, TOPO_WALK_CHILD); 369 topo_node_rele(root); 370 } 371 372 /* 373 * Tidy-up the root node 374 */ 375 while ((nhp = topo_list_next(&root->tn_children)) != NULL) { 376 for (i = 0; i < nhp->th_arrlen; i++) { 377 assert(nhp->th_nodearr[i] == NULL); 378 } 379 mod = nhp->th_enum; 380 topo_mod_strfree(mod, nhp->th_name); 381 topo_mod_free(mod, nhp->th_nodearr, 382 nhp->th_arrlen * sizeof (tnode_t *)); 383 topo_list_delete(&root->tn_children, nhp); 384 topo_mod_free(mod, nhp, sizeof (topo_nodehash_t)); 385 topo_mod_rele(mod); 386 } 387 388 } 389 390 if (thp->th_uuid != NULL) { 391 topo_hdl_free(thp, thp->th_uuid, TOPO_UUID_SIZE); 392 thp->th_uuid = NULL; 393 } 394 } 395 396 void 397 topo_snap_release(topo_hdl_t *thp) 398 { 399 if (thp == NULL) 400 return; 401 402 topo_hdl_lock(thp); 403 topo_snap_destroy(thp); 404 topo_hdl_unlock(thp); 405 406 } 407 408 topo_walk_t * 409 topo_walk_init(topo_hdl_t *thp, const char *scheme, topo_walk_cb_t cb_f, 410 void *pdata, int *errp) 411 { 412 ttree_t *tp; 413 topo_walk_t *wp; 414 415 for (tp = topo_list_next(&thp->th_trees); tp != NULL; 416 tp = topo_list_next(tp)) { 417 if (strcmp(scheme, tp->tt_scheme) == 0) { 418 419 /* 420 * Hold the root node and start walk at the first 421 * child node 422 */ 423 assert(tp->tt_root != NULL); 424 425 if ((wp = topo_node_walk_init(thp, NULL, tp->tt_root, 426 cb_f, pdata, errp)) == NULL) /* errp set */ 427 return (NULL); 428 429 return (wp); 430 } 431 } 432 433 *errp = ETOPO_WALK_NOTFOUND; 434 return (NULL); 435 } 436 437 static int 438 step_child(tnode_t *cnp, topo_walk_t *wp, int bottomup) 439 { 440 int status; 441 tnode_t *nnp; 442 443 nnp = topo_child_first(cnp); 444 445 if (nnp == NULL) { 446 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 447 "step_child: TOPO_WALK_TERMINATE for %s=%d\n", 448 cnp->tn_name, cnp->tn_instance); 449 return (TOPO_WALK_TERMINATE); 450 } 451 452 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 453 "step_child: walk through node %s=%d to %s=%d\n", 454 cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance); 455 456 topo_node_hold(nnp); /* released on return from walk_step */ 457 wp->tw_node = nnp; 458 if (bottomup == 1) 459 status = topo_walk_bottomup(wp, TOPO_WALK_CHILD); 460 else 461 status = topo_walk_step(wp, TOPO_WALK_CHILD); 462 463 return (status); 464 } 465 466 static int 467 step_sibling(tnode_t *cnp, topo_walk_t *wp, int bottomup) 468 { 469 int status; 470 tnode_t *nnp; 471 472 nnp = topo_child_next(cnp->tn_parent, cnp); 473 474 if (nnp == NULL) { 475 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 476 "step_sibling: TOPO_WALK_TERMINATE for %s=%d\n", 477 cnp->tn_name, cnp->tn_instance); 478 return (TOPO_WALK_TERMINATE); 479 } 480 481 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 482 "step_sibling: through sibling node %s=%d to %s=%d\n", 483 cnp->tn_name, cnp->tn_instance, nnp->tn_name, nnp->tn_instance); 484 485 topo_node_hold(nnp); /* released on return from walk_step */ 486 wp->tw_node = nnp; 487 if (bottomup == 1) 488 status = topo_walk_bottomup(wp, TOPO_WALK_CHILD); 489 else 490 status = topo_walk_step(wp, TOPO_WALK_CHILD); 491 492 return (status); 493 } 494 495 int 496 topo_walk_byid(topo_walk_t *wp, const char *name, topo_instance_t inst) 497 { 498 int status; 499 tnode_t *nnp, *cnp; 500 501 cnp = wp->tw_node; 502 nnp = topo_node_lookup(cnp, name, inst); 503 if (nnp == NULL) 504 return (TOPO_WALK_TERMINATE); 505 506 topo_node_hold(nnp); 507 wp->tw_node = nnp; 508 if (wp->tw_mod != NULL) 509 status = wp->tw_cb(wp->tw_mod, nnp, wp->tw_pdata); 510 else 511 status = wp->tw_cb(wp->tw_thp, nnp, wp->tw_pdata); 512 topo_node_rele(nnp); 513 wp->tw_node = cnp; 514 515 return (status); 516 } 517 518 int 519 topo_walk_step(topo_walk_t *wp, int flag) 520 { 521 int status; 522 tnode_t *cnp = wp->tw_node; 523 524 if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) { 525 topo_node_rele(cnp); 526 return (TOPO_WALK_ERR); 527 } 528 529 /* 530 * No more nodes to walk 531 */ 532 if (cnp == NULL) { 533 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 534 "walk_step terminated\n"); 535 topo_node_rele(cnp); 536 return (TOPO_WALK_TERMINATE); 537 } 538 539 540 if (wp->tw_mod != NULL) 541 status = wp->tw_cb(wp->tw_mod, cnp, wp->tw_pdata); 542 else 543 status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata); 544 545 /* 546 * Walker callback says we're done 547 */ 548 if (status != TOPO_WALK_NEXT) { 549 topo_node_rele(cnp); 550 return (status); 551 } 552 553 flag == TOPO_WALK_CHILD ? (status = step_child(cnp, wp, 0)) : 554 (status = step_sibling(cnp, wp, 0)); 555 556 /* 557 * No more nodes in this hash, skip to next node hash by stepping 558 * to next sibling (depth-first walk) or next child (breadth-first 559 * walk). 560 */ 561 if (status == TOPO_WALK_TERMINATE) { 562 563 flag == TOPO_WALK_CHILD ? (status = step_sibling(cnp, wp, 0)) : 564 (status = step_child(cnp, wp, 0)); 565 } 566 567 topo_node_rele(cnp); /* done with current node */ 568 569 return (status); 570 } 571 572 void 573 topo_walk_fini(topo_walk_t *wp) 574 { 575 if (wp == NULL) 576 return; 577 578 topo_node_rele(wp->tw_root); 579 580 topo_hdl_free(wp->tw_thp, wp, sizeof (topo_walk_t)); 581 } 582 583 int 584 topo_walk_bottomup(topo_walk_t *wp, int flag) 585 { 586 int status; 587 tnode_t *cnp; 588 589 if (wp == NULL) 590 return (TOPO_WALK_ERR); 591 592 cnp = wp->tw_node; 593 if (flag != TOPO_WALK_CHILD && flag != TOPO_WALK_SIBLING) { 594 topo_node_rele(cnp); 595 return (TOPO_WALK_ERR); 596 } 597 598 /* 599 * End of the line 600 */ 601 if (cnp == NULL) { 602 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 603 "walk_bottomup terminated\n"); 604 topo_node_rele(cnp); 605 return (TOPO_WALK_TERMINATE); 606 } 607 608 topo_dprintf(wp->tw_thp, TOPO_DBG_WALK, 609 "%s walk_bottomup through node %s=%d\n", 610 (flag == TOPO_WALK_CHILD ? "TOPO_WALK_CHILD" : "TOPO_WALK_SIBLING"), 611 cnp->tn_name, cnp->tn_instance); 612 613 if (flag == TOPO_WALK_CHILD) 614 status = step_child(cnp, wp, 1); 615 else 616 status = step_sibling(cnp, wp, 1); 617 618 /* 619 * At a leaf, run the callback 620 */ 621 if (status == TOPO_WALK_TERMINATE) { 622 if ((status = wp->tw_cb(wp->tw_thp, cnp, wp->tw_pdata)) 623 != TOPO_WALK_NEXT) { 624 topo_node_rele(cnp); 625 return (status); 626 } 627 } 628 629 /* 630 * Try next child or sibling 631 */ 632 if (status == TOPO_WALK_NEXT) { 633 if (flag == TOPO_WALK_CHILD) 634 status = step_sibling(cnp, wp, 1); 635 else 636 status = step_child(cnp, wp, 1); 637 } 638 639 topo_node_rele(cnp); /* done with current node */ 640 641 return (status); 642 } 643