10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52621Sllai1 * Common Development and Distribution License (the "License"). 62621Sllai1 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 222621Sllai1 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 282621Sllai1 #include "libdevinfo.h" 290Sstevel@tonic-gate #include "devinfo_devlink.h" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #undef DEBUG 320Sstevel@tonic-gate #ifndef DEBUG 330Sstevel@tonic-gate #define NDEBUG 1 340Sstevel@tonic-gate #else 350Sstevel@tonic-gate #undef NDEBUG 360Sstevel@tonic-gate #endif 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <assert.h> 390Sstevel@tonic-gate 400Sstevel@tonic-gate static mutex_t update_mutex = DEFAULTMUTEX; /* Protects update record lock */ 410Sstevel@tonic-gate 420Sstevel@tonic-gate static const size_t elem_sizes[DB_TYPES] = { 430Sstevel@tonic-gate sizeof (struct db_node), 440Sstevel@tonic-gate sizeof (struct db_minor), 450Sstevel@tonic-gate sizeof (struct db_link), 460Sstevel@tonic-gate sizeof (char) 470Sstevel@tonic-gate }; 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * List of directories/files skipped while physically walking /dev 510Sstevel@tonic-gate * Paths are relative to "<root>/dev/" 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate static const char *skip_dirs[] = {"fd"}; 540Sstevel@tonic-gate static const char *skip_files[] = { 550Sstevel@tonic-gate "stdout", 560Sstevel@tonic-gate "stdin", 570Sstevel@tonic-gate "stderr" 580Sstevel@tonic-gate }; 590Sstevel@tonic-gate 600Sstevel@tonic-gate #define N_SKIP_DIRS (sizeof (skip_dirs) / sizeof (skip_dirs[0])) 610Sstevel@tonic-gate #define N_SKIP_FILES (sizeof (skip_files) / sizeof (skip_files[0])) 620Sstevel@tonic-gate 630Sstevel@tonic-gate /* 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * This file contains two sets of interfaces which operate on the reverse 660Sstevel@tonic-gate * links database. One set (which includes di_devlink_open()/_close()) 670Sstevel@tonic-gate * allows link generators like devfsadm(1M) and ucblinks(1B) (writers) to 680Sstevel@tonic-gate * populate the database with /devices -> /dev mappings. Another set 690Sstevel@tonic-gate * of interfaces (which includes di_devlink_init()/_fini()) allows 700Sstevel@tonic-gate * applications (readers) to lookup the database for /dev links corresponding 710Sstevel@tonic-gate * to a given minor. 720Sstevel@tonic-gate * 730Sstevel@tonic-gate * Writers operate on a cached version of the database. The cache is created 740Sstevel@tonic-gate * when di_devlink_open() is called. As links in /dev are created and removed, 750Sstevel@tonic-gate * the cache is updated to keep it in synch with /dev. When the /dev updates 760Sstevel@tonic-gate * are complete, the link generator calls di_devlink_close() which writes 770Sstevel@tonic-gate * out the cache to the database. 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * Applications which need to lookup the database, call di_devlink_init(). 800Sstevel@tonic-gate * di_devlink_init() checks the database file (if one exists). If the 810Sstevel@tonic-gate * database is valid, it is mapped into the address space of the 820Sstevel@tonic-gate * application. The database file consists of several segments. Each 830Sstevel@tonic-gate * segment can be mapped in independently and is mapped on demand. 840Sstevel@tonic-gate * 850Sstevel@tonic-gate * Database Layout 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * --------------------- 880Sstevel@tonic-gate * | Magic # | 890Sstevel@tonic-gate * | ----------------- | 900Sstevel@tonic-gate * | Version | HEADER 910Sstevel@tonic-gate * | ----------------- | 920Sstevel@tonic-gate * | ... | 930Sstevel@tonic-gate * --------------------- 940Sstevel@tonic-gate * | | 950Sstevel@tonic-gate * | | NODES 960Sstevel@tonic-gate * | | 970Sstevel@tonic-gate * | | 980Sstevel@tonic-gate * --------------------- 990Sstevel@tonic-gate * | | 1000Sstevel@tonic-gate * | | MINORS 1010Sstevel@tonic-gate * | | 1020Sstevel@tonic-gate * | | 1030Sstevel@tonic-gate * --------------------- 1040Sstevel@tonic-gate * | | 1050Sstevel@tonic-gate * | | LINKS 1060Sstevel@tonic-gate * | | 1070Sstevel@tonic-gate * | | 1080Sstevel@tonic-gate * --------------------- 1090Sstevel@tonic-gate * | | 1100Sstevel@tonic-gate * | | STRINGS 1110Sstevel@tonic-gate * | | 1120Sstevel@tonic-gate * | | 1130Sstevel@tonic-gate * --------------------- 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * Readers can lookup /dev links for a specific minor or 1160Sstevel@tonic-gate * lookup all /dev links. In the latter case, the node 1170Sstevel@tonic-gate * and minor segments are not mapped in and the reader 1180Sstevel@tonic-gate * walks through every link in the link segment. 1190Sstevel@tonic-gate * 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate di_devlink_handle_t 1230Sstevel@tonic-gate di_devlink_open(const char *root_dir, uint_t flags) 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate int err; 1260Sstevel@tonic-gate char path[PATH_MAX]; 1270Sstevel@tonic-gate struct di_devlink_handle *hdp; 1280Sstevel@tonic-gate int retried = 0; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate retry: 1310Sstevel@tonic-gate /* 1320Sstevel@tonic-gate * Allocate a read-write handle but open the DB in readonly 1330Sstevel@tonic-gate * mode. We do writes only to a temporary copy of the database. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate if ((hdp = handle_alloc(root_dir, OPEN_RDWR)) == NULL) { 1360Sstevel@tonic-gate return (NULL); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate err = open_db(hdp, OPEN_RDONLY); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Unlink the database, so that consumers don't get 1430Sstevel@tonic-gate * out of date information as the database is being updated. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate get_db_path(hdp, DB_FILE, path, sizeof (path)); 1460Sstevel@tonic-gate (void) unlink(path); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* 1490Sstevel@tonic-gate * The flags argument is reserved for future use. 1500Sstevel@tonic-gate */ 1510Sstevel@tonic-gate if (flags != 0) { 1520Sstevel@tonic-gate handle_free(&hdp); /* also closes the DB */ 1530Sstevel@tonic-gate errno = EINVAL; 1540Sstevel@tonic-gate return (NULL); 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate if (cache_alloc(hdp) != 0) { 1580Sstevel@tonic-gate handle_free(&hdp); 1590Sstevel@tonic-gate return (NULL); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate if (err) { 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Failed to open DB. 1650Sstevel@tonic-gate * The most likely cause is that DB file did not exist. 1660Sstevel@tonic-gate * Call di_devlink_close() to recreate the DB file and 1670Sstevel@tonic-gate * retry di_devlink_open(). 1680Sstevel@tonic-gate */ 1690Sstevel@tonic-gate if (retried == 0) { 1700Sstevel@tonic-gate (void) di_devlink_close(&hdp, 0); 1710Sstevel@tonic-gate retried = 1; 1720Sstevel@tonic-gate goto retry; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * DB cannot be opened, just return the 1770Sstevel@tonic-gate * handle. We will recreate the DB later. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate return (hdp); 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate /* Read the database into the cache */ 1830Sstevel@tonic-gate CACHE(hdp)->update_count = DB_HDR(hdp)->update_count; 1840Sstevel@tonic-gate (void) read_nodes(hdp, NULL, DB_HDR(hdp)->root_idx); 1850Sstevel@tonic-gate (void) read_links(hdp, NULL, DB_HDR(hdp)->dngl_idx); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate (void) close_db(hdp); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate return (hdp); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate static void 1930Sstevel@tonic-gate get_db_path( 1940Sstevel@tonic-gate struct di_devlink_handle *hdp, 1950Sstevel@tonic-gate const char *fname, 1960Sstevel@tonic-gate char *buf, 1970Sstevel@tonic-gate size_t blen) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate char *dir = NULL; 2000Sstevel@tonic-gate 2010Sstevel@tonic-gate #ifdef DEBUG 2020Sstevel@tonic-gate if (dir = getenv(ALT_DB_DIR)) { 2030Sstevel@tonic-gate (void) dprintf(DBG_INFO, "get_db_path: alternate db dir: %s\n", 2040Sstevel@tonic-gate dir); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate #endif 2070Sstevel@tonic-gate if (dir == NULL) { 2082621Sllai1 dir = hdp->db_dir; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate (void) snprintf(buf, blen, "%s/%s", dir, fname); 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate static int 2150Sstevel@tonic-gate open_db(struct di_devlink_handle *hdp, int flags) 2160Sstevel@tonic-gate { 2170Sstevel@tonic-gate size_t sz; 2180Sstevel@tonic-gate long page_sz; 2190Sstevel@tonic-gate int fd, rv, flg; 2200Sstevel@tonic-gate struct stat sbuf; 2210Sstevel@tonic-gate uint32_t count[DB_TYPES] = {0}; 2220Sstevel@tonic-gate char path[PATH_MAX]; 2230Sstevel@tonic-gate void *cp; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate assert(!DB_OPEN(hdp)); 2260Sstevel@tonic-gate 2270Sstevel@tonic-gate #ifdef DEBUG 2280Sstevel@tonic-gate if (getenv(SKIP_DB)) { 2290Sstevel@tonic-gate (void) dprintf(DBG_INFO, "open_db: skipping database\n"); 2300Sstevel@tonic-gate return (-1); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate #endif 2330Sstevel@tonic-gate if ((page_sz = sysconf(_SC_PAGE_SIZE)) == -1) { 2340Sstevel@tonic-gate return (-1); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Use O_TRUNC flag for write access, so that the subsequent ftruncate() 2390Sstevel@tonic-gate * call will zero-fill the entire file 2400Sstevel@tonic-gate */ 2410Sstevel@tonic-gate if (IS_RDONLY(flags)) { 2420Sstevel@tonic-gate flg = O_RDONLY; 2430Sstevel@tonic-gate get_db_path(hdp, DB_FILE, path, sizeof (path)); 2440Sstevel@tonic-gate } else { 2450Sstevel@tonic-gate flg = O_RDWR|O_CREAT|O_TRUNC; 2460Sstevel@tonic-gate get_db_path(hdp, DB_TMP, path, sizeof (path)); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate 2492621Sllai1 /* 2502621Sllai1 * Avoid triggering /dev reconfigure for read when not present 2512621Sllai1 */ 2522621Sllai1 if (IS_RDONLY(flags) && 2532621Sllai1 (strncmp(path, "/dev/", 5) == 0) && !device_exists(path)) { 2542621Sllai1 return (-1); 2552621Sllai1 } 2562621Sllai1 2570Sstevel@tonic-gate if ((fd = open(path, flg, DB_PERMS)) == -1) { 2580Sstevel@tonic-gate return (-1); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate if (IS_RDONLY(flags)) { 2620Sstevel@tonic-gate flg = PROT_READ; 2630Sstevel@tonic-gate rv = fstat(fd, &sbuf); 2640Sstevel@tonic-gate sz = sbuf.st_size; 2650Sstevel@tonic-gate } else { 2660Sstevel@tonic-gate flg = PROT_READ | PROT_WRITE; 2670Sstevel@tonic-gate sz = size_db(hdp, page_sz, count); 2680Sstevel@tonic-gate rv = ftruncate(fd, sz); 2690Sstevel@tonic-gate } 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate if (rv == -1 || sz < HDR_LEN) { 2720Sstevel@tonic-gate if (rv != -1) 2730Sstevel@tonic-gate errno = EINVAL; 2740Sstevel@tonic-gate (void) close(fd); 2750Sstevel@tonic-gate return (-1); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate cp = mmap(0, HDR_LEN, flg, MAP_SHARED, fd, 0); 2790Sstevel@tonic-gate if (cp == MAP_FAILED) { 2800Sstevel@tonic-gate (void) close(fd); 2810Sstevel@tonic-gate return (-1); 2820Sstevel@tonic-gate } 2830Sstevel@tonic-gate DB(hdp)->hdr = (struct db_hdr *)cp; 2840Sstevel@tonic-gate DB(hdp)->db_fd = fd; 2850Sstevel@tonic-gate DB(hdp)->flags = flags; 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate if (IS_RDONLY(flags)) { 2880Sstevel@tonic-gate rv = invalid_db(hdp, sz, page_sz); 2890Sstevel@tonic-gate } else { 2900Sstevel@tonic-gate rv = init_hdr(hdp, page_sz, count); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate if (rv) { 2940Sstevel@tonic-gate (void) dprintf(DBG_ERR, "open_db: invalid DB(%s)\n", path); 2950Sstevel@tonic-gate (void) close_db(hdp); 2960Sstevel@tonic-gate return (-1); 2970Sstevel@tonic-gate } else { 2980Sstevel@tonic-gate (void) dprintf(DBG_STEP, "open_db: DB(%s): opened\n", path); 2990Sstevel@tonic-gate return (0); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * A handle can be allocated for read-only or read-write access 3050Sstevel@tonic-gate */ 3060Sstevel@tonic-gate static struct di_devlink_handle * 3070Sstevel@tonic-gate handle_alloc(const char *root_dir, uint_t flags) 3080Sstevel@tonic-gate { 3092621Sllai1 char dev_dir[PATH_MAX], path[PATH_MAX], db_dir[PATH_MAX]; 3100Sstevel@tonic-gate struct di_devlink_handle *hdp, proto = {0}; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate assert(flags == OPEN_RDWR || flags == OPEN_RDONLY); 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate dev_dir[0] = '\0'; 3152621Sllai1 db_dir[0] = '\0'; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate /* 3180Sstevel@tonic-gate * NULL and the empty string are equivalent to "/" 3190Sstevel@tonic-gate */ 3200Sstevel@tonic-gate if (root_dir && root_dir[0] != '\0') { 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate if (root_dir[0] != '/') { 3230Sstevel@tonic-gate errno = EINVAL; 3240Sstevel@tonic-gate return (NULL); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate 3270Sstevel@tonic-gate #ifdef DEBUG 3280Sstevel@tonic-gate /*LINTED*/ 3290Sstevel@tonic-gate assert(sizeof (dev_dir) >= PATH_MAX); 3300Sstevel@tonic-gate #endif 3312621Sllai1 if ((realpath(root_dir, dev_dir) == NULL) || 3322621Sllai1 (realpath(root_dir, db_dir) == NULL)) { 3330Sstevel@tonic-gate return (NULL); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate if (strcmp(dev_dir, "/") == 0) { 3382621Sllai1 dev_dir[0] = 0; 3392621Sllai1 db_dir[0] = 0; 3400Sstevel@tonic-gate } else { 3412621Sllai1 (void) strlcpy(db_dir, dev_dir, sizeof (db_dir)); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3442621Sllai1 (void) strlcat(dev_dir, DEV, sizeof (dev_dir)); 3452621Sllai1 (void) strlcat(db_dir, ETCDEV, sizeof (db_dir)); 3462621Sllai1 3470Sstevel@tonic-gate proto.dev_dir = dev_dir; 3482621Sllai1 proto.db_dir = db_dir; 3490Sstevel@tonic-gate proto.flags = flags; 3500Sstevel@tonic-gate proto.lock_fd = -1; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * Lock database if a read-write handle is being allocated. 3540Sstevel@tonic-gate * Locks are needed to protect against multiple writers. 3550Sstevel@tonic-gate * Readers don't need locks. 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate if (HDL_RDWR(&proto)) { 3580Sstevel@tonic-gate if (enter_update_lock(&proto) != 0) { 3590Sstevel@tonic-gate return (NULL); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate DB(&proto)->db_fd = -1; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate hdp = calloc(1, sizeof (struct di_devlink_handle)); 3660Sstevel@tonic-gate if (hdp == NULL) { 3670Sstevel@tonic-gate goto error; 3680Sstevel@tonic-gate } 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate *hdp = proto; 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate /* 3730Sstevel@tonic-gate * The handle hdp now contains a pointer to local storage 3740Sstevel@tonic-gate * in the dev_dir field (obtained from the proto handle). 3750Sstevel@tonic-gate * In the following line, a dynamically allocated version 3760Sstevel@tonic-gate * is substituted. 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate if ((hdp->dev_dir = strdup(proto.dev_dir)) == NULL) { 3800Sstevel@tonic-gate free(hdp); 3810Sstevel@tonic-gate goto error; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3842621Sllai1 if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) { 3852621Sllai1 free(hdp->dev_dir); 3862621Sllai1 free(hdp); 3872621Sllai1 goto error; 3882621Sllai1 } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate return (hdp); 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate error: 3930Sstevel@tonic-gate if (HDL_RDWR(&proto)) { 3940Sstevel@tonic-gate /* Unlink DB file on error */ 3950Sstevel@tonic-gate get_db_path(&proto, DB_FILE, path, sizeof (path)); 3960Sstevel@tonic-gate (void) unlink(path); 3970Sstevel@tonic-gate exit_update_lock(&proto); 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate return (NULL); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate static int 4040Sstevel@tonic-gate cache_alloc(struct di_devlink_handle *hdp) 4050Sstevel@tonic-gate { 4060Sstevel@tonic-gate size_t hash_sz = 0; 4070Sstevel@tonic-gate 4080Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate if (DB_OPEN(hdp)) { 4110Sstevel@tonic-gate hash_sz = DB_NUM(hdp, DB_LINK) / AVG_CHAIN_SIZE; 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate hash_sz = (hash_sz >= MIN_HASH_SIZE) ? hash_sz : MIN_HASH_SIZE; 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate CACHE(hdp)->hash = calloc(hash_sz, sizeof (cache_link_t *)); 4160Sstevel@tonic-gate if (CACHE(hdp)->hash == NULL) { 4170Sstevel@tonic-gate return (-1); 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate CACHE(hdp)->hash_sz = hash_sz; 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate return (0); 4220Sstevel@tonic-gate } 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate static int 4260Sstevel@tonic-gate invalid_db(struct di_devlink_handle *hdp, size_t fsize, long page_sz) 4270Sstevel@tonic-gate { 4280Sstevel@tonic-gate int i; 4290Sstevel@tonic-gate char *cp; 4300Sstevel@tonic-gate size_t sz; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (DB_HDR(hdp)->magic != DB_MAGIC || DB_HDR(hdp)->vers != DB_VERSION) { 4330Sstevel@tonic-gate return (1); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate if (DB_HDR(hdp)->page_sz == 0 || DB_HDR(hdp)->page_sz != page_sz) { 4370Sstevel@tonic-gate return (1); 4380Sstevel@tonic-gate } 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate sz = seg_size(hdp, DB_HEADER); 4410Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 4420Sstevel@tonic-gate (void) dprintf(DBG_INFO, "N[%u] = %u\n", i, DB_NUM(hdp, i)); 4430Sstevel@tonic-gate /* There must be at least 1 element of each type */ 4440Sstevel@tonic-gate if (DB_NUM(hdp, i) < 1) { 4450Sstevel@tonic-gate return (1); 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate sz += seg_size(hdp, i); 4480Sstevel@tonic-gate assert(sz % page_sz == 0); 4490Sstevel@tonic-gate } 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate if (sz != fsize) { 4520Sstevel@tonic-gate return (1); 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate if (!VALID_INDEX(hdp, DB_NODE, DB_HDR(hdp)->root_idx)) { 4560Sstevel@tonic-gate return (1); 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if (!VALID_INDEX(hdp, DB_LINK, DB_HDR(hdp)->dngl_idx)) { 4600Sstevel@tonic-gate return (1); 4610Sstevel@tonic-gate } 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (DB_EMPTY(hdp)) { 4640Sstevel@tonic-gate return (1); 4650Sstevel@tonic-gate } 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * The last character in the string segment must be a NUL char. 4690Sstevel@tonic-gate */ 4700Sstevel@tonic-gate cp = get_string(hdp, DB_NUM(hdp, DB_STR) - 1); 4710Sstevel@tonic-gate if (cp == NULL || *cp != '\0') { 4720Sstevel@tonic-gate return (1); 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate return (0); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate static int 4790Sstevel@tonic-gate read_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx) 4800Sstevel@tonic-gate { 4810Sstevel@tonic-gate char *path; 4820Sstevel@tonic-gate cache_node_t *cnp; 4830Sstevel@tonic-gate struct db_node *dnp; 4840Sstevel@tonic-gate const char *fcn = "read_nodes"; 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * parent node should be NULL only for the root node 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate if ((pcnp == NULL) ^ (nidx == DB_HDR(hdp)->root_idx)) { 4920Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid parent or index(%u)\n", 4930Sstevel@tonic-gate fcn, nidx); 4940Sstevel@tonic-gate SET_DB_ERR(hdp); 4950Sstevel@tonic-gate return (-1); 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate 4980Sstevel@tonic-gate for (; dnp = get_node(hdp, nidx); nidx = dnp->sib) { 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate path = get_string(hdp, dnp->path); 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * Insert at head of list to recreate original order 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate cnp = node_insert(hdp, pcnp, path, INSERT_HEAD); 5060Sstevel@tonic-gate if (cnp == NULL) { 5070Sstevel@tonic-gate SET_DB_ERR(hdp); 5080Sstevel@tonic-gate break; 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate assert(strcmp(path, "/") ^ (nidx == DB_HDR(hdp)->root_idx)); 5120Sstevel@tonic-gate assert(strcmp(path, "/") != 0 || dnp->sib == DB_NIL); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if (read_minors(hdp, cnp, dnp->minor) != 0 || 5150Sstevel@tonic-gate read_nodes(hdp, cnp, dnp->child) != 0) { 5160Sstevel@tonic-gate break; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, nidx, 5200Sstevel@tonic-gate cnp->path); 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate return (dnp ? -1 : 0); 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate static int 5270Sstevel@tonic-gate read_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate cache_minor_t *cmnp; 5300Sstevel@tonic-gate struct db_minor *dmp; 5310Sstevel@tonic-gate char *name, *nodetype; 5320Sstevel@tonic-gate const char *fcn = "read_minors"; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (pcnp == NULL) { 5370Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: minor[%u]: orphan minor\n", fcn, 5380Sstevel@tonic-gate nidx); 5390Sstevel@tonic-gate SET_DB_ERR(hdp); 5400Sstevel@tonic-gate return (-1); 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) { 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate name = get_string(hdp, dmp->name); 5460Sstevel@tonic-gate nodetype = get_string(hdp, dmp->nodetype); 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate cmnp = minor_insert(hdp, pcnp, name, nodetype, NULL); 5490Sstevel@tonic-gate if (cmnp == NULL) { 5500Sstevel@tonic-gate SET_DB_ERR(hdp); 5510Sstevel@tonic-gate break; 5520Sstevel@tonic-gate } 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, nidx, 5550Sstevel@tonic-gate cmnp->name); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate if (read_links(hdp, cmnp, dmp->link) != 0) { 5580Sstevel@tonic-gate break; 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate return (dmp ? -1 : 0); 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* 5660Sstevel@tonic-gate * If the link is dangling the corresponding minor will be absent. 5670Sstevel@tonic-gate */ 5680Sstevel@tonic-gate static int 5690Sstevel@tonic-gate read_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp, uint32_t nidx) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate cache_link_t *clp; 5720Sstevel@tonic-gate struct db_link *dlp; 5730Sstevel@tonic-gate char *path, *content; 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate if (nidx != DB_NIL && 5780Sstevel@tonic-gate ((pcmp == NULL) ^ (nidx == DB_HDR(hdp)->dngl_idx))) { 5790Sstevel@tonic-gate (void) dprintf(DBG_ERR, "read_links: invalid minor or" 5800Sstevel@tonic-gate " index(%u)\n", nidx); 5810Sstevel@tonic-gate SET_DB_ERR(hdp); 5820Sstevel@tonic-gate return (-1); 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate 5850Sstevel@tonic-gate for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) { 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate path = get_string(hdp, dlp->path); 5880Sstevel@tonic-gate content = get_string(hdp, dlp->content); 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate clp = link_insert(hdp, pcmp, path, content, dlp->attr); 5910Sstevel@tonic-gate if (clp == NULL) { 5920Sstevel@tonic-gate SET_DB_ERR(hdp); 5930Sstevel@tonic-gate break; 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate (void) dprintf(DBG_STEP, "read_links: link[%u]: %s%s\n", 5970Sstevel@tonic-gate nidx, clp->path, pcmp == NULL ? "(DANGLING)" : ""); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate return (dlp ? -1 : 0); 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate int 6040Sstevel@tonic-gate di_devlink_close(di_devlink_handle_t *pp, int flag) 6050Sstevel@tonic-gate { 6060Sstevel@tonic-gate int i, rv; 6070Sstevel@tonic-gate char tmp[PATH_MAX]; 6080Sstevel@tonic-gate char file[PATH_MAX]; 6090Sstevel@tonic-gate uint32_t next[DB_TYPES] = {0}; 6100Sstevel@tonic-gate struct di_devlink_handle *hdp; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate if (pp == NULL || *pp == NULL || !HDL_RDWR(*pp)) { 6130Sstevel@tonic-gate errno = EINVAL; 6140Sstevel@tonic-gate return (-1); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate hdp = *pp; 6180Sstevel@tonic-gate *pp = NULL; 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* 6210Sstevel@tonic-gate * The caller encountered some error in their processing. 6220Sstevel@tonic-gate * so handle isn't valid. Discard it and return success. 6230Sstevel@tonic-gate */ 6240Sstevel@tonic-gate if (flag == DI_LINK_ERROR) { 6250Sstevel@tonic-gate handle_free(&hdp); 6260Sstevel@tonic-gate return (0); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate if (DB_ERR(hdp)) { 6300Sstevel@tonic-gate handle_free(&hdp); 6310Sstevel@tonic-gate errno = EINVAL; 6320Sstevel@tonic-gate return (-1); 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate /* 6360Sstevel@tonic-gate * Extract the DB path before the handle is freed. 6370Sstevel@tonic-gate */ 6380Sstevel@tonic-gate get_db_path(hdp, DB_FILE, file, sizeof (file)); 6390Sstevel@tonic-gate get_db_path(hdp, DB_TMP, tmp, sizeof (tmp)); 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate /* 6420Sstevel@tonic-gate * update database with actual contents of /dev 6430Sstevel@tonic-gate */ 6440Sstevel@tonic-gate (void) dprintf(DBG_INFO, "di_devlink_close: update_count = %u\n", 6450Sstevel@tonic-gate CACHE(hdp)->update_count); 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate /* 6480Sstevel@tonic-gate * For performance reasons, synchronization of the database 6490Sstevel@tonic-gate * with /dev is turned off by default. However, applications 6500Sstevel@tonic-gate * with appropriate permissions can request a "sync" by 6510Sstevel@tonic-gate * calling di_devlink_update(). 6520Sstevel@tonic-gate */ 6530Sstevel@tonic-gate if (CACHE(hdp)->update_count == 0) { 6540Sstevel@tonic-gate CACHE(hdp)->update_count = 1; 6550Sstevel@tonic-gate (void) dprintf(DBG_INFO, 6560Sstevel@tonic-gate "di_devlink_close: synchronizing DB\n"); 6570Sstevel@tonic-gate (void) synchronize_db(hdp); 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* 6610Sstevel@tonic-gate * Resolve dangling links AFTER synchronizing DB with /dev as the 6620Sstevel@tonic-gate * synchronization process may create dangling links. 6630Sstevel@tonic-gate */ 6640Sstevel@tonic-gate resolve_dangling_links(hdp); 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate /* 6670Sstevel@tonic-gate * All changes to the cache are complete. Write out the cache 6680Sstevel@tonic-gate * to the database only if it is not empty. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate if (CACHE_EMPTY(hdp)) { 6710Sstevel@tonic-gate (void) dprintf(DBG_INFO, "di_devlink_close: skipping write\n"); 6720Sstevel@tonic-gate (void) unlink(file); 6730Sstevel@tonic-gate handle_free(&hdp); 6740Sstevel@tonic-gate return (0); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate if (open_db(hdp, OPEN_RDWR) != 0) { 6780Sstevel@tonic-gate handle_free(&hdp); 6790Sstevel@tonic-gate return (-1); 6800Sstevel@tonic-gate } 6810Sstevel@tonic-gate 6820Sstevel@tonic-gate /* 6830Sstevel@tonic-gate * Keep track of array assignments. There is atleast 6840Sstevel@tonic-gate * 1 element (the "NIL" element) per type. 6850Sstevel@tonic-gate */ 6860Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 6870Sstevel@tonic-gate next[i] = 1; 6880Sstevel@tonic-gate } 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate (void) write_nodes(hdp, NULL, CACHE_ROOT(hdp), next); 6910Sstevel@tonic-gate (void) write_links(hdp, NULL, CACHE(hdp)->dngl, next); 6920Sstevel@tonic-gate DB_HDR(hdp)->update_count = CACHE(hdp)->update_count; 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate rv = close_db(hdp); 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if (rv != 0 || DB_ERR(hdp) || rename(tmp, file) != 0) { 6970Sstevel@tonic-gate (void) dprintf(DBG_ERR, "di_devlink_close: %s error: %s\n", 6980Sstevel@tonic-gate rv ? "close_db" : "DB or rename", strerror(errno)); 6990Sstevel@tonic-gate (void) unlink(tmp); 7000Sstevel@tonic-gate (void) unlink(file); 7010Sstevel@tonic-gate handle_free(&hdp); 7020Sstevel@tonic-gate return (-1); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate handle_free(&hdp); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate (void) dprintf(DBG_INFO, "di_devlink_close: wrote DB(%s)\n", file); 7080Sstevel@tonic-gate 7090Sstevel@tonic-gate return (0); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate /* 7130Sstevel@tonic-gate * Inits the database header. 7140Sstevel@tonic-gate */ 7150Sstevel@tonic-gate static int 7160Sstevel@tonic-gate init_hdr(struct di_devlink_handle *hdp, long page_sz, uint32_t *count) 7170Sstevel@tonic-gate { 7180Sstevel@tonic-gate int i; 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate DB_HDR(hdp)->magic = DB_MAGIC; 7210Sstevel@tonic-gate DB_HDR(hdp)->vers = DB_VERSION; 7220Sstevel@tonic-gate DB_HDR(hdp)->root_idx = DB_NIL; 7230Sstevel@tonic-gate DB_HDR(hdp)->dngl_idx = DB_NIL; 7240Sstevel@tonic-gate DB_HDR(hdp)->page_sz = (uint32_t)page_sz; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 7270Sstevel@tonic-gate assert(count[i] >= 1); 7280Sstevel@tonic-gate DB_NUM(hdp, i) = count[i]; 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate return (0); 7320Sstevel@tonic-gate } 7330Sstevel@tonic-gate 7340Sstevel@tonic-gate static int 7350Sstevel@tonic-gate write_nodes( 7360Sstevel@tonic-gate struct di_devlink_handle *hdp, 7370Sstevel@tonic-gate struct db_node *pdnp, 7380Sstevel@tonic-gate cache_node_t *cnp, 7390Sstevel@tonic-gate uint32_t *next) 7400Sstevel@tonic-gate { 7410Sstevel@tonic-gate uint32_t idx; 7420Sstevel@tonic-gate struct db_node *dnp; 7430Sstevel@tonic-gate const char *fcn = "write_nodes"; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 7460Sstevel@tonic-gate 7470Sstevel@tonic-gate for (; cnp != NULL; cnp = cnp->sib) { 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate assert(cnp->path != NULL); 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate /* parent node should only be NULL for root node */ 7520Sstevel@tonic-gate if ((pdnp == NULL) ^ (cnp == CACHE_ROOT(hdp))) { 7530Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid parent for: %s\n", 7540Sstevel@tonic-gate fcn, cnp->path); 7550Sstevel@tonic-gate SET_DB_ERR(hdp); 7560Sstevel@tonic-gate break; 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate assert((strcmp(cnp->path, "/") != 0) ^ 7600Sstevel@tonic-gate (cnp == CACHE_ROOT(hdp))); 7610Sstevel@tonic-gate 7620Sstevel@tonic-gate idx = next[DB_NODE]; 7630Sstevel@tonic-gate if ((dnp = set_node(hdp, idx)) == NULL) { 7640Sstevel@tonic-gate SET_DB_ERR(hdp); 7650Sstevel@tonic-gate break; 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate dnp->path = write_string(hdp, cnp->path, next); 7690Sstevel@tonic-gate if (dnp->path == DB_NIL) { 7700Sstevel@tonic-gate SET_DB_ERR(hdp); 7710Sstevel@tonic-gate break; 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate /* commit write for this node */ 7740Sstevel@tonic-gate next[DB_NODE]++; 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate if (pdnp == NULL) { 7770Sstevel@tonic-gate assert(DB_HDR(hdp)->root_idx == DB_NIL); 7780Sstevel@tonic-gate DB_HDR(hdp)->root_idx = idx; 7790Sstevel@tonic-gate } else { 7800Sstevel@tonic-gate dnp->sib = pdnp->child; 7810Sstevel@tonic-gate pdnp->child = idx; 7820Sstevel@tonic-gate } 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, idx, 7850Sstevel@tonic-gate cnp->path); 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate if (write_minors(hdp, dnp, cnp->minor, next) != 0 || 7880Sstevel@tonic-gate write_nodes(hdp, dnp, cnp->child, next) != 0) { 7890Sstevel@tonic-gate break; 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate } 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate return (cnp ? -1 : 0); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate static int 7970Sstevel@tonic-gate write_minors( 7980Sstevel@tonic-gate struct di_devlink_handle *hdp, 7990Sstevel@tonic-gate struct db_node *pdnp, 8000Sstevel@tonic-gate cache_minor_t *cmnp, 8010Sstevel@tonic-gate uint32_t *next) 8020Sstevel@tonic-gate { 8030Sstevel@tonic-gate uint32_t idx; 8040Sstevel@tonic-gate struct db_minor *dmp; 8050Sstevel@tonic-gate const char *fcn = "write_minors"; 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate if (pdnp == NULL) { 8100Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: no node for minor: %s\n", fcn, 8110Sstevel@tonic-gate cmnp ? cmnp->name : "<NULL>"); 8120Sstevel@tonic-gate SET_DB_ERR(hdp); 8130Sstevel@tonic-gate return (-1); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate for (; cmnp != NULL; cmnp = cmnp->sib) { 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate assert(cmnp->name != NULL); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate idx = next[DB_MINOR]; 8210Sstevel@tonic-gate if ((dmp = set_minor(hdp, idx)) == NULL) { 8220Sstevel@tonic-gate SET_DB_ERR(hdp); 8230Sstevel@tonic-gate break; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate dmp->name = write_string(hdp, cmnp->name, next); 8270Sstevel@tonic-gate dmp->nodetype = write_string(hdp, cmnp->nodetype, next); 8280Sstevel@tonic-gate if (dmp->name == DB_NIL || dmp->nodetype == DB_NIL) { 8290Sstevel@tonic-gate dmp->name = dmp->nodetype = DB_NIL; 8300Sstevel@tonic-gate SET_DB_ERR(hdp); 8310Sstevel@tonic-gate break; 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate /* Commit writes to this minor */ 8350Sstevel@tonic-gate next[DB_MINOR]++; 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate dmp->sib = pdnp->minor; 8380Sstevel@tonic-gate pdnp->minor = idx; 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, idx, 8410Sstevel@tonic-gate cmnp->name); 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate if (write_links(hdp, dmp, cmnp->link, next) != 0) { 8440Sstevel@tonic-gate break; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate } 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate return (cmnp ? -1 : 0); 8490Sstevel@tonic-gate } 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate static int 8520Sstevel@tonic-gate write_links( 8530Sstevel@tonic-gate struct di_devlink_handle *hdp, 8540Sstevel@tonic-gate struct db_minor *pdmp, 8550Sstevel@tonic-gate cache_link_t *clp, 8560Sstevel@tonic-gate uint32_t *next) 8570Sstevel@tonic-gate { 8580Sstevel@tonic-gate uint32_t idx; 8590Sstevel@tonic-gate struct db_link *dlp; 8600Sstevel@tonic-gate const char *fcn = "write_links"; 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* A NULL minor if and only if the links are dangling */ 8650Sstevel@tonic-gate if (clp != NULL && ((pdmp == NULL) ^ (clp == CACHE(hdp)->dngl))) { 8660Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid minor for link\n", fcn); 8670Sstevel@tonic-gate SET_DB_ERR(hdp); 8680Sstevel@tonic-gate return (-1); 8690Sstevel@tonic-gate } 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate for (; clp != NULL; clp = clp->sib) { 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate assert(clp->path != NULL); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate if ((pdmp == NULL) ^ (clp->minor == NULL)) { 8760Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid minor for link" 8770Sstevel@tonic-gate "(%s)\n", fcn, clp->path); 8780Sstevel@tonic-gate SET_DB_ERR(hdp); 8790Sstevel@tonic-gate break; 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate idx = next[DB_LINK]; 8830Sstevel@tonic-gate if ((dlp = set_link(hdp, idx)) == NULL) { 8840Sstevel@tonic-gate SET_DB_ERR(hdp); 8850Sstevel@tonic-gate break; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate dlp->path = write_string(hdp, clp->path, next); 8890Sstevel@tonic-gate dlp->content = write_string(hdp, clp->content, next); 8900Sstevel@tonic-gate if (dlp->path == DB_NIL || dlp->content == DB_NIL) { 8910Sstevel@tonic-gate dlp->path = dlp->content = DB_NIL; 8920Sstevel@tonic-gate SET_DB_ERR(hdp); 8930Sstevel@tonic-gate break; 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate dlp->attr = clp->attr; 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* Commit writes to this link */ 8990Sstevel@tonic-gate next[DB_LINK]++; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate if (pdmp != NULL) { 9020Sstevel@tonic-gate dlp->sib = pdmp->link; 9030Sstevel@tonic-gate pdmp->link = idx; 9040Sstevel@tonic-gate } else { 9050Sstevel@tonic-gate dlp->sib = DB_HDR(hdp)->dngl_idx; 9060Sstevel@tonic-gate DB_HDR(hdp)->dngl_idx = idx; 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: link[%u]: %s%s\n", fcn, idx, 9100Sstevel@tonic-gate clp->path, pdmp == NULL ? "(DANGLING)" : ""); 9110Sstevel@tonic-gate } 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate return (clp ? -1 : 0); 9140Sstevel@tonic-gate } 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate static uint32_t 9180Sstevel@tonic-gate write_string(struct di_devlink_handle *hdp, const char *str, uint32_t *next) 9190Sstevel@tonic-gate { 9200Sstevel@tonic-gate char *dstr; 9210Sstevel@tonic-gate uint32_t idx; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate assert(HDL_RDWR(hdp)); 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate if (str == NULL) { 9260Sstevel@tonic-gate (void) dprintf(DBG_ERR, "write_string: NULL argument\n"); 9270Sstevel@tonic-gate return (DB_NIL); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate idx = next[DB_STR]; 9310Sstevel@tonic-gate if (!VALID_STR(hdp, idx, str)) { 9320Sstevel@tonic-gate (void) dprintf(DBG_ERR, "write_string: invalid index[%u]," 9330Sstevel@tonic-gate " string(%s)\n", idx, str); 9340Sstevel@tonic-gate return (DB_NIL); 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate if ((dstr = set_string(hdp, idx)) == NULL) { 9380Sstevel@tonic-gate return (DB_NIL); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate (void) strcpy(dstr, str); 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate next[DB_STR] += strlen(dstr) + 1; 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate return (idx); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 9480Sstevel@tonic-gate static int 9490Sstevel@tonic-gate close_db(struct di_devlink_handle *hdp) 9500Sstevel@tonic-gate { 9510Sstevel@tonic-gate int i, rv = 0; 9520Sstevel@tonic-gate size_t sz; 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (!DB_OPEN(hdp)) { 9550Sstevel@tonic-gate #ifdef DEBUG 9560Sstevel@tonic-gate assert(DB(hdp)->db_fd == -1); 9570Sstevel@tonic-gate assert(DB(hdp)->flags == 0); 9580Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 9590Sstevel@tonic-gate assert(DB_SEG(hdp, i) == NULL); 9600Sstevel@tonic-gate assert(DB_SEG_PROT(hdp, i) == 0); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate #endif 9630Sstevel@tonic-gate return (0); 9640Sstevel@tonic-gate } 9650Sstevel@tonic-gate 9660Sstevel@tonic-gate /* Unmap header after unmapping all other mapped segments */ 9670Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 9680Sstevel@tonic-gate if (DB_SEG(hdp, i)) { 9690Sstevel@tonic-gate sz = seg_size(hdp, i); 9700Sstevel@tonic-gate if (DB_RDWR(hdp)) 9710Sstevel@tonic-gate rv += msync(DB_SEG(hdp, i), sz, MS_SYNC); 9720Sstevel@tonic-gate (void) munmap(DB_SEG(hdp, i), sz); 9730Sstevel@tonic-gate DB_SEG(hdp, i) = NULL; 9740Sstevel@tonic-gate DB_SEG_PROT(hdp, i) = 0; 9750Sstevel@tonic-gate } 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate if (DB_RDWR(hdp)) 9790Sstevel@tonic-gate rv += msync((caddr_t)DB_HDR(hdp), HDR_LEN, MS_SYNC); 9800Sstevel@tonic-gate (void) munmap((caddr_t)DB_HDR(hdp), HDR_LEN); 9810Sstevel@tonic-gate DB(hdp)->hdr = NULL; 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate (void) close(DB(hdp)->db_fd); 9840Sstevel@tonic-gate DB(hdp)->db_fd = -1; 9850Sstevel@tonic-gate DB(hdp)->flags = 0; 9860Sstevel@tonic-gate 9870Sstevel@tonic-gate return (rv ? -1 : 0); 9880Sstevel@tonic-gate } 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate static void 9920Sstevel@tonic-gate cache_free(struct di_devlink_handle *hdp) 9930Sstevel@tonic-gate { 9940Sstevel@tonic-gate cache_link_t *clp; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate subtree_free(hdp, &(CACHE_ROOT(hdp))); 9970Sstevel@tonic-gate assert(CACHE_LAST(hdp) == NULL); 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate /* 10000Sstevel@tonic-gate * Don't bother removing links from hash table chains, 10010Sstevel@tonic-gate * as we are freeing the hash table itself. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate while (CACHE(hdp)->dngl != NULL) { 10040Sstevel@tonic-gate clp = CACHE(hdp)->dngl; 10050Sstevel@tonic-gate CACHE(hdp)->dngl = clp->sib; 10060Sstevel@tonic-gate assert(clp->minor == NULL); 10070Sstevel@tonic-gate link_free(&clp); 10080Sstevel@tonic-gate } 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate assert((CACHE(hdp)->hash == NULL) ^ (CACHE(hdp)->hash_sz != 0)); 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate free(CACHE(hdp)->hash); 10130Sstevel@tonic-gate CACHE(hdp)->hash = NULL; 10140Sstevel@tonic-gate CACHE(hdp)->hash_sz = 0; 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate static void 10180Sstevel@tonic-gate handle_free(struct di_devlink_handle **pp) 10190Sstevel@tonic-gate { 10200Sstevel@tonic-gate struct di_devlink_handle *hdp = *pp; 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate *pp = NULL; 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate if (hdp == NULL) 10250Sstevel@tonic-gate return; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate (void) close_db(hdp); 10280Sstevel@tonic-gate cache_free(hdp); 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate if (HDL_RDWR(hdp)) 10310Sstevel@tonic-gate exit_update_lock(hdp); 10320Sstevel@tonic-gate assert(hdp->lock_fd == -1); 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate free(hdp->dev_dir); 1035*2723Scth free(hdp->db_dir); 10360Sstevel@tonic-gate free(hdp); 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate 10390Sstevel@tonic-gate /* 10400Sstevel@tonic-gate * Frees the tree rooted at a node. Siblings of the subtree root 10410Sstevel@tonic-gate * have to be handled by the caller. 10420Sstevel@tonic-gate */ 10430Sstevel@tonic-gate static void 10440Sstevel@tonic-gate subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp) 10450Sstevel@tonic-gate { 10460Sstevel@tonic-gate cache_node_t *np; 10470Sstevel@tonic-gate cache_link_t *clp; 10480Sstevel@tonic-gate cache_minor_t *cmnp; 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate if (pp == NULL || *pp == NULL) 10510Sstevel@tonic-gate return; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate while ((*pp)->child != NULL) { 10540Sstevel@tonic-gate np = (*pp)->child; 10550Sstevel@tonic-gate (*pp)->child = np->sib; 10560Sstevel@tonic-gate subtree_free(hdp, &np); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate while ((*pp)->minor != NULL) { 10600Sstevel@tonic-gate cmnp = (*pp)->minor; 10610Sstevel@tonic-gate (*pp)->minor = cmnp->sib; 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate while (cmnp->link != NULL) { 10640Sstevel@tonic-gate clp = cmnp->link; 10650Sstevel@tonic-gate cmnp->link = clp->sib; 10660Sstevel@tonic-gate rm_link_from_hash(hdp, clp); 10670Sstevel@tonic-gate link_free(&clp); 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate minor_free(hdp, &cmnp); 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate node_free(pp); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate static void 10760Sstevel@tonic-gate rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp) 10770Sstevel@tonic-gate { 10780Sstevel@tonic-gate int hval; 10790Sstevel@tonic-gate cache_link_t **pp; 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate if (clp == NULL) 10820Sstevel@tonic-gate return; 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate if (clp->path == NULL) 10850Sstevel@tonic-gate return; 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate hval = hashfn(hdp, clp->path); 10880Sstevel@tonic-gate pp = &(CACHE_HASH(hdp, hval)); 10890Sstevel@tonic-gate for (; *pp != NULL; pp = &(*pp)->hash) { 10900Sstevel@tonic-gate if (*pp == clp) { 10910Sstevel@tonic-gate *pp = clp->hash; 10920Sstevel@tonic-gate clp->hash = NULL; 10930Sstevel@tonic-gate return; 10940Sstevel@tonic-gate } 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate dprintf(DBG_ERR, "rm_link_from_hash: link(%s) not found\n", clp->path); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate static cache_link_t * 11010Sstevel@tonic-gate link_hash(di_devlink_handle_t hdp, const char *link, uint_t flags) 11020Sstevel@tonic-gate { 11030Sstevel@tonic-gate int hval; 11040Sstevel@tonic-gate cache_link_t **pp, *clp; 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate if (link == NULL) 11070Sstevel@tonic-gate return (NULL); 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate hval = hashfn(hdp, link); 11100Sstevel@tonic-gate pp = &(CACHE_HASH(hdp, hval)); 11110Sstevel@tonic-gate for (; (clp = *pp) != NULL; pp = &clp->hash) { 11120Sstevel@tonic-gate if (strcmp(clp->path, link) == 0) { 11130Sstevel@tonic-gate break; 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate } 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate if (clp == NULL) 11180Sstevel@tonic-gate return (NULL); 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate if ((flags & UNLINK_FROM_HASH) == UNLINK_FROM_HASH) { 11210Sstevel@tonic-gate *pp = clp->hash; 11220Sstevel@tonic-gate clp->hash = NULL; 11230Sstevel@tonic-gate } 11240Sstevel@tonic-gate 11250Sstevel@tonic-gate return (clp); 11260Sstevel@tonic-gate } 11270Sstevel@tonic-gate 11280Sstevel@tonic-gate static cache_minor_t * 11290Sstevel@tonic-gate link2minor(struct di_devlink_handle *hdp, cache_link_t *clp) 11300Sstevel@tonic-gate { 11310Sstevel@tonic-gate cache_link_t *plp; 11320Sstevel@tonic-gate const char *minor_path; 11330Sstevel@tonic-gate char *cp, buf[PATH_MAX], link[PATH_MAX]; 11342621Sllai1 char abspath[PATH_MAX]; 11352621Sllai1 struct stat st; 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate if (TYPE_PRI(attr2type(clp->attr))) { 11380Sstevel@tonic-gate /* 11390Sstevel@tonic-gate * For primary link, content should point to a /devices node. 11400Sstevel@tonic-gate */ 11410Sstevel@tonic-gate if (!is_minor_node(clp->content, &minor_path)) { 11420Sstevel@tonic-gate return (NULL); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate return (lookup_minor(hdp, minor_path, NULL, 11460Sstevel@tonic-gate TYPE_CACHE|CREATE_FLAG)); 11470Sstevel@tonic-gate 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate /* 11510Sstevel@tonic-gate * If secondary, the primary link is derived from the secondary 11520Sstevel@tonic-gate * link contents. Secondary link contents can have two formats: 11532621Sllai1 * audio -> /dev/sound/0 11540Sstevel@tonic-gate * fb0 -> fbs/afb0 11550Sstevel@tonic-gate */ 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate buf[0] = '\0'; 11580Sstevel@tonic-gate if (strncmp(clp->content, DEV"/", strlen(DEV"/")) == 0) { 11590Sstevel@tonic-gate cp = &clp->content[strlen(DEV"/")]; 11600Sstevel@tonic-gate } else if (clp->content[0] != '/') { 11610Sstevel@tonic-gate if ((cp = strrchr(clp->path, '/')) != NULL) { 11620Sstevel@tonic-gate char savechar = *(cp + 1); 11630Sstevel@tonic-gate *(cp + 1) = '\0'; 11640Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s", clp->path); 11650Sstevel@tonic-gate *(cp + 1) = savechar; 11660Sstevel@tonic-gate } 11670Sstevel@tonic-gate (void) strlcat(buf, clp->content, sizeof (buf)); 11680Sstevel@tonic-gate cp = buf; 11690Sstevel@tonic-gate } else { 11700Sstevel@tonic-gate goto follow_link; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate /* 11740Sstevel@tonic-gate * Lookup the primary link if possible and find its minor. 11750Sstevel@tonic-gate */ 11760Sstevel@tonic-gate if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) { 11770Sstevel@tonic-gate return (plp->minor); 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate /* realpath() used only as a last resort because it is expensive */ 11810Sstevel@tonic-gate follow_link: 11820Sstevel@tonic-gate (void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path); 11830Sstevel@tonic-gate 11840Sstevel@tonic-gate #ifdef DEBUG 11850Sstevel@tonic-gate /*LINTED*/ 11860Sstevel@tonic-gate assert(sizeof (buf) >= PATH_MAX); 11870Sstevel@tonic-gate #endif 11882621Sllai1 11892621Sllai1 /* 11902621Sllai1 * A realpath attempt to lookup a dangling link can invoke implicit 11912621Sllai1 * reconfig so verify there's an actual device behind the link first. 11922621Sllai1 */ 11932621Sllai1 if (lstat(link, &st) == -1) 11942621Sllai1 return (NULL); 11952621Sllai1 if (S_ISLNK(st.st_mode)) { 11962621Sllai1 if (s_readlink(link, buf, sizeof (buf)) < 0) 11972621Sllai1 return (NULL); 11982621Sllai1 if (buf[0] != '/') { 11992621Sllai1 char *p; 12002621Sllai1 size_t n = sizeof (abspath); 12012621Sllai1 if (strlcpy(abspath, link, n) >= n) 12022621Sllai1 return (NULL); 12032621Sllai1 p = strrchr(abspath, '/') + 1; 12042621Sllai1 *p = 0; 12052621Sllai1 n = sizeof (abspath) - strlen(p); 12062621Sllai1 if (strlcpy(p, buf, n) >= n) 12072621Sllai1 return (NULL); 12082621Sllai1 } else { 12092621Sllai1 if (strlcpy(abspath, buf, sizeof (abspath)) >= 12102621Sllai1 sizeof (abspath)) 12112621Sllai1 return (NULL); 12122621Sllai1 } 12132621Sllai1 if (!device_exists(abspath)) 12142621Sllai1 return (NULL); 12152621Sllai1 } 12162621Sllai1 12170Sstevel@tonic-gate if (realpath(link, buf) == NULL || !is_minor_node(buf, &minor_path)) { 12180Sstevel@tonic-gate return (NULL); 12190Sstevel@tonic-gate } 12200Sstevel@tonic-gate return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG)); 12210Sstevel@tonic-gate } 12220Sstevel@tonic-gate 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate static void 12250Sstevel@tonic-gate resolve_dangling_links(struct di_devlink_handle *hdp) 12260Sstevel@tonic-gate { 12270Sstevel@tonic-gate cache_minor_t *cmnp; 12280Sstevel@tonic-gate cache_link_t *clp, **pp; 12290Sstevel@tonic-gate 12300Sstevel@tonic-gate for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) { 12310Sstevel@tonic-gate clp = *pp; 12320Sstevel@tonic-gate if ((cmnp = link2minor(hdp, clp)) != NULL) { 12330Sstevel@tonic-gate *pp = clp->sib; 12340Sstevel@tonic-gate clp->sib = cmnp->link; 12350Sstevel@tonic-gate cmnp->link = clp; 12360Sstevel@tonic-gate assert(clp->minor == NULL); 12370Sstevel@tonic-gate clp->minor = cmnp; 12380Sstevel@tonic-gate } else { 12390Sstevel@tonic-gate dprintf(DBG_INFO, "resolve_dangling_links: link(%s):" 12400Sstevel@tonic-gate " unresolved\n", clp->path); 12410Sstevel@tonic-gate pp = &clp->sib; 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate } 12440Sstevel@tonic-gate } 12450Sstevel@tonic-gate 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate /* 12480Sstevel@tonic-gate * The elements are assumed to be detached from the cache tree. 12490Sstevel@tonic-gate */ 12500Sstevel@tonic-gate static void 12510Sstevel@tonic-gate node_free(cache_node_t **pp) 12520Sstevel@tonic-gate { 12530Sstevel@tonic-gate cache_node_t *cnp = *pp; 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate *pp = NULL; 12560Sstevel@tonic-gate 12570Sstevel@tonic-gate if (cnp == NULL) 12580Sstevel@tonic-gate return; 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate free(cnp->path); 12610Sstevel@tonic-gate free(cnp); 12620Sstevel@tonic-gate } 12630Sstevel@tonic-gate 12640Sstevel@tonic-gate static void 12650Sstevel@tonic-gate minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp) 12660Sstevel@tonic-gate { 12670Sstevel@tonic-gate cache_minor_t *cmnp = *pp; 12680Sstevel@tonic-gate 12690Sstevel@tonic-gate *pp = NULL; 12700Sstevel@tonic-gate 12710Sstevel@tonic-gate if (cmnp == NULL) 12720Sstevel@tonic-gate return; 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate if (CACHE_LAST(hdp) == cmnp) { 12750Sstevel@tonic-gate dprintf(DBG_STEP, "minor_free: last_minor(%s)\n", cmnp->name); 12760Sstevel@tonic-gate CACHE_LAST(hdp) = NULL; 12770Sstevel@tonic-gate } 12780Sstevel@tonic-gate 12790Sstevel@tonic-gate free(cmnp->name); 12800Sstevel@tonic-gate free(cmnp->nodetype); 12810Sstevel@tonic-gate free(cmnp); 12820Sstevel@tonic-gate } 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate static void 12850Sstevel@tonic-gate link_free(cache_link_t **pp) 12860Sstevel@tonic-gate { 12870Sstevel@tonic-gate cache_link_t *clp = *pp; 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate *pp = NULL; 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate if (clp == NULL) 12920Sstevel@tonic-gate return; 12930Sstevel@tonic-gate 12940Sstevel@tonic-gate free(clp->path); 12950Sstevel@tonic-gate free(clp->content); 12960Sstevel@tonic-gate free(clp); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate /* 13000Sstevel@tonic-gate * Returns the ':' preceding the minor name 13010Sstevel@tonic-gate */ 13020Sstevel@tonic-gate static char * 13030Sstevel@tonic-gate minor_colon(const char *path) 13040Sstevel@tonic-gate { 13050Sstevel@tonic-gate char *cp; 13060Sstevel@tonic-gate 13070Sstevel@tonic-gate if ((cp = strrchr(path, '/')) == NULL) { 13080Sstevel@tonic-gate return (NULL); 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate return (strchr(cp, ':')); 13120Sstevel@tonic-gate } 13130Sstevel@tonic-gate 13140Sstevel@tonic-gate static void * 13150Sstevel@tonic-gate lookup_minor( 13160Sstevel@tonic-gate struct di_devlink_handle *hdp, 13170Sstevel@tonic-gate const char *minor_path, 13180Sstevel@tonic-gate const char *nodetype, 13190Sstevel@tonic-gate const int flags) 13200Sstevel@tonic-gate { 13210Sstevel@tonic-gate void *vp; 13220Sstevel@tonic-gate char *colon; 13230Sstevel@tonic-gate char pdup[PATH_MAX]; 13240Sstevel@tonic-gate const char *fcn = "lookup_minor"; 13250Sstevel@tonic-gate 13260Sstevel@tonic-gate if (minor_path == NULL) { 13270Sstevel@tonic-gate errno = EINVAL; 13280Sstevel@tonic-gate return (NULL); 13290Sstevel@tonic-gate } 13300Sstevel@tonic-gate 13310Sstevel@tonic-gate (void) snprintf(pdup, sizeof (pdup), "%s", minor_path); 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate if ((colon = minor_colon(pdup)) == NULL) { 13340Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid minor path(%s)\n", fcn, 13350Sstevel@tonic-gate minor_path); 13360Sstevel@tonic-gate errno = EINVAL; 13370Sstevel@tonic-gate return (NULL); 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate *colon = '\0'; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) { 13420Sstevel@tonic-gate return (vp); 13430Sstevel@tonic-gate } 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate if ((vp = lookup_node(hdp, pdup, flags)) == NULL) { 13460Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: node(%s) not found\n", fcn, pdup); 13470Sstevel@tonic-gate return (NULL); 13480Sstevel@tonic-gate } 13490Sstevel@tonic-gate *colon = ':'; 13500Sstevel@tonic-gate 13510Sstevel@tonic-gate if (LOOKUP_CACHE(flags)) { 13520Sstevel@tonic-gate cache_minor_t **pp; 13530Sstevel@tonic-gate 13540Sstevel@tonic-gate pp = &((cache_node_t *)vp)->minor; 13550Sstevel@tonic-gate for (; *pp != NULL; pp = &(*pp)->sib) { 13560Sstevel@tonic-gate if (strcmp((*pp)->name, colon + 1) == 0) 13570Sstevel@tonic-gate break; 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate if (*pp == NULL && CREATE_ELEM(flags)) { 13610Sstevel@tonic-gate *pp = minor_insert(hdp, vp, colon + 1, nodetype, pp); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate set_last_minor(hdp, *pp, flags); 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate return (*pp); 13660Sstevel@tonic-gate } else { 13670Sstevel@tonic-gate char *cp; 13680Sstevel@tonic-gate uint32_t nidx; 13690Sstevel@tonic-gate struct db_minor *dmp; 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate nidx = (((struct db_node *)vp)->minor); 13720Sstevel@tonic-gate for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) { 13730Sstevel@tonic-gate cp = get_string(hdp, dmp->name); 13740Sstevel@tonic-gate if (cp && strcmp(cp, colon + 1) == 0) 13750Sstevel@tonic-gate break; 13760Sstevel@tonic-gate } 13770Sstevel@tonic-gate return (dmp); 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate } 13800Sstevel@tonic-gate 13810Sstevel@tonic-gate static void * 13820Sstevel@tonic-gate lookup_node(struct di_devlink_handle *hdp, char *path, const int flags) 13830Sstevel@tonic-gate { 13840Sstevel@tonic-gate struct tnode tnd = {NULL}; 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate if (tnd.node = get_last_node(hdp, path, flags)) 13870Sstevel@tonic-gate return (tnd.node); 13880Sstevel@tonic-gate 13890Sstevel@tonic-gate tnd.handle = hdp; 13900Sstevel@tonic-gate tnd.flags = flags; 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate if (walk_tree(path, &tnd, visit_node) != 0) 13930Sstevel@tonic-gate return (NULL); 13940Sstevel@tonic-gate 13950Sstevel@tonic-gate return (tnd.node); 13960Sstevel@tonic-gate } 13970Sstevel@tonic-gate 13980Sstevel@tonic-gate /* 13990Sstevel@tonic-gate * last_minor is used for nodes of TYPE_CACHE only. 14000Sstevel@tonic-gate */ 14010Sstevel@tonic-gate static void * 14020Sstevel@tonic-gate get_last_node(struct di_devlink_handle *hdp, const char *path, int flags) 14030Sstevel@tonic-gate { 14040Sstevel@tonic-gate cache_node_t *cnp; 14050Sstevel@tonic-gate 14060Sstevel@tonic-gate #ifdef DEBUG 14070Sstevel@tonic-gate if (getenv(SKIP_LAST_CACHE)) { 14080Sstevel@tonic-gate (void) dprintf(DBG_INFO, "get_last_node: SKIPPING \"last\" " 14090Sstevel@tonic-gate "node cache\n"); 14100Sstevel@tonic-gate return (NULL); 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate #endif 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL || 14150Sstevel@tonic-gate CACHE_LAST(hdp)->node == NULL) { 14160Sstevel@tonic-gate return (NULL); 14170Sstevel@tonic-gate } 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate cnp = CACHE_LAST(hdp)->node; 14200Sstevel@tonic-gate if (strcmp(cnp->path, path) == 0) { 14210Sstevel@tonic-gate return (cnp); 14220Sstevel@tonic-gate } 14230Sstevel@tonic-gate 14240Sstevel@tonic-gate cnp = cnp->sib; 14250Sstevel@tonic-gate if (cnp && strcmp(cnp->path, path) == 0) { 14260Sstevel@tonic-gate return (cnp); 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate return (NULL); 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate static void * 14330Sstevel@tonic-gate get_last_minor( 14340Sstevel@tonic-gate struct di_devlink_handle *hdp, 14350Sstevel@tonic-gate const char *devfs_path, 14360Sstevel@tonic-gate const char *minor_name, 14370Sstevel@tonic-gate int flags) 14380Sstevel@tonic-gate { 14390Sstevel@tonic-gate cache_minor_t *cmnp; 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate #ifdef DEBUG 14420Sstevel@tonic-gate if (getenv(SKIP_LAST_CACHE)) { 14430Sstevel@tonic-gate (void) dprintf(DBG_INFO, "get_last_minor: SKIPPING \"last\" " 14440Sstevel@tonic-gate "minor cache\n"); 14450Sstevel@tonic-gate return (NULL); 14460Sstevel@tonic-gate } 14470Sstevel@tonic-gate #endif 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) { 14500Sstevel@tonic-gate return (NULL); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate 14530Sstevel@tonic-gate cmnp = CACHE_LAST(hdp); 14540Sstevel@tonic-gate if (strcmp(cmnp->name, minor_name) == 0 && cmnp->node && 14550Sstevel@tonic-gate strcmp(cmnp->node->path, devfs_path) == 0) { 14560Sstevel@tonic-gate return (cmnp); 14570Sstevel@tonic-gate } 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate cmnp = cmnp->sib; 14600Sstevel@tonic-gate if (cmnp && strcmp(cmnp->name, minor_name) == 0 && cmnp->node && 14610Sstevel@tonic-gate strcmp(cmnp->node->path, devfs_path) == 0) { 14620Sstevel@tonic-gate set_last_minor(hdp, cmnp, TYPE_CACHE); 14630Sstevel@tonic-gate return (cmnp); 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate return (NULL); 14670Sstevel@tonic-gate } 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate static void 14700Sstevel@tonic-gate set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags) 14710Sstevel@tonic-gate { 14720Sstevel@tonic-gate #ifdef DEBUG 14730Sstevel@tonic-gate if (getenv(SKIP_LAST_CACHE)) { 14740Sstevel@tonic-gate (void) dprintf(DBG_INFO, "set_last_minor: SKIPPING \"last\" " 14750Sstevel@tonic-gate "minor cache\n"); 14760Sstevel@tonic-gate return; 14770Sstevel@tonic-gate } 14780Sstevel@tonic-gate #endif 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate if (LOOKUP_CACHE(flags) && cmnp) { 14810Sstevel@tonic-gate CACHE_LAST(hdp) = cmnp; 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate } 14840Sstevel@tonic-gate 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate /* 14870Sstevel@tonic-gate * Returns 0 if normal return or -1 otherwise. 14880Sstevel@tonic-gate */ 14890Sstevel@tonic-gate static int 14900Sstevel@tonic-gate walk_tree( 14910Sstevel@tonic-gate char *cur, 14920Sstevel@tonic-gate void *arg, 14930Sstevel@tonic-gate int (*node_callback)(const char *path, void *arg)) 14940Sstevel@tonic-gate { 14950Sstevel@tonic-gate char *slash, buf[PATH_MAX]; 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate if (cur == NULL || cur[0] != '/' || strlen(cur) > sizeof (buf) - 1) { 14980Sstevel@tonic-gate errno = EINVAL; 14990Sstevel@tonic-gate return (-1); 15000Sstevel@tonic-gate } 15010Sstevel@tonic-gate 15020Sstevel@tonic-gate (void) strcpy(buf, "/"); 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate for (;;) { 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate if (node_callback(buf, arg) != DI_WALK_CONTINUE) 15070Sstevel@tonic-gate break; 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate while (*cur == '/') 15100Sstevel@tonic-gate cur++; 15110Sstevel@tonic-gate 15120Sstevel@tonic-gate if (*cur == '\0') 15130Sstevel@tonic-gate break; 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate /* 15160Sstevel@tonic-gate * There is a next component(s). Append a "/" separator for all 15170Sstevel@tonic-gate * but the first (root) component. 15180Sstevel@tonic-gate */ 15190Sstevel@tonic-gate if (buf[1] != '\0') { 15200Sstevel@tonic-gate (void) strlcat(buf, "/", sizeof (buf)); 15210Sstevel@tonic-gate } 15220Sstevel@tonic-gate 15230Sstevel@tonic-gate if (slash = strchr(cur, '/')) { 15240Sstevel@tonic-gate *slash = '\0'; 15250Sstevel@tonic-gate (void) strlcat(buf, cur, sizeof (buf)); 15260Sstevel@tonic-gate *slash = '/'; 15270Sstevel@tonic-gate cur = slash; 15280Sstevel@tonic-gate } else { 15290Sstevel@tonic-gate (void) strlcat(buf, cur, sizeof (buf)); 15300Sstevel@tonic-gate cur += strlen(cur); 15310Sstevel@tonic-gate } 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate return (0); 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate static int 15400Sstevel@tonic-gate visit_node(const char *path, void *arg) 15410Sstevel@tonic-gate { 15420Sstevel@tonic-gate struct tnode *tnp = arg; 15430Sstevel@tonic-gate 15440Sstevel@tonic-gate if (LOOKUP_CACHE(tnp->flags)) { 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate cache_node_t *cnp = tnp->node; 15470Sstevel@tonic-gate 15480Sstevel@tonic-gate cnp = (cnp) ? cnp->child : CACHE_ROOT(tnp->handle); 15490Sstevel@tonic-gate 15500Sstevel@tonic-gate for (; cnp != NULL; cnp = cnp->sib) { 15510Sstevel@tonic-gate if (strcmp(cnp->path, path) == 0) 15520Sstevel@tonic-gate break; 15530Sstevel@tonic-gate } 15540Sstevel@tonic-gate if (cnp == NULL && CREATE_ELEM(tnp->flags)) { 15550Sstevel@tonic-gate cnp = node_insert(tnp->handle, tnp->node, path, 15560Sstevel@tonic-gate INSERT_TAIL); 15570Sstevel@tonic-gate } 15580Sstevel@tonic-gate tnp->node = cnp; 15590Sstevel@tonic-gate } else { 15600Sstevel@tonic-gate char *cp; 15610Sstevel@tonic-gate struct db_node *dnp = tnp->node; 15620Sstevel@tonic-gate 15630Sstevel@tonic-gate dnp = (dnp) ? get_node(tnp->handle, dnp->child) 15640Sstevel@tonic-gate : get_node(tnp->handle, DB_HDR(tnp->handle)->root_idx); 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate for (; dnp != NULL; dnp = get_node(tnp->handle, dnp->sib)) { 15670Sstevel@tonic-gate cp = get_string(tnp->handle, dnp->path); 15680Sstevel@tonic-gate if (cp && strcmp(cp, path) == 0) { 15690Sstevel@tonic-gate break; 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate tnp->node = dnp; 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate /* 15760Sstevel@tonic-gate * Terminate walk if node is not found for a path component. 15770Sstevel@tonic-gate */ 15780Sstevel@tonic-gate return (tnp->node ? DI_WALK_CONTINUE : DI_WALK_TERMINATE); 15790Sstevel@tonic-gate } 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate static void 15820Sstevel@tonic-gate minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp) 15830Sstevel@tonic-gate { 15840Sstevel@tonic-gate cache_link_t **lpp; 15850Sstevel@tonic-gate cache_minor_t **mpp; 15860Sstevel@tonic-gate const char *fcn = "minor_delete"; 15870Sstevel@tonic-gate 15880Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: removing minor: %s\n", fcn, cmnp->name); 15890Sstevel@tonic-gate 15900Sstevel@tonic-gate /* detach minor from node */ 15910Sstevel@tonic-gate if (cmnp->node != NULL) { 15920Sstevel@tonic-gate mpp = &cmnp->node->minor; 15930Sstevel@tonic-gate for (; *mpp != NULL; mpp = &(*mpp)->sib) { 15940Sstevel@tonic-gate if (*mpp == cmnp) 15950Sstevel@tonic-gate break; 15960Sstevel@tonic-gate } 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate if (*mpp == NULL) { 15990Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: dangling minor: %s\n", 16000Sstevel@tonic-gate fcn, cmnp->name); 16010Sstevel@tonic-gate } else { 16020Sstevel@tonic-gate *mpp = cmnp->sib; 16030Sstevel@tonic-gate } 16040Sstevel@tonic-gate } else { 16050Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: orphan minor(%s)\n", fcn, 16060Sstevel@tonic-gate cmnp->name); 16070Sstevel@tonic-gate } 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate delete_unused_nodes(hdp, cmnp->node); 16100Sstevel@tonic-gate 16110Sstevel@tonic-gate cmnp->node = NULL; 16120Sstevel@tonic-gate cmnp->sib = NULL; 16130Sstevel@tonic-gate 16140Sstevel@tonic-gate /* Move all remaining links to dangling list */ 16150Sstevel@tonic-gate for (lpp = &cmnp->link; *lpp != NULL; lpp = &(*lpp)->sib) { 16160Sstevel@tonic-gate (*lpp)->minor = NULL; 16170Sstevel@tonic-gate } 16180Sstevel@tonic-gate *lpp = CACHE(hdp)->dngl; 16190Sstevel@tonic-gate CACHE(hdp)->dngl = cmnp->link; 16200Sstevel@tonic-gate cmnp->link = NULL; 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate minor_free(hdp, &cmnp); 16230Sstevel@tonic-gate } 16240Sstevel@tonic-gate 16250Sstevel@tonic-gate static void 16260Sstevel@tonic-gate delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp) 16270Sstevel@tonic-gate { 16280Sstevel@tonic-gate cache_node_t **npp; 16290Sstevel@tonic-gate const char *fcn = "delete_unused_nodes"; 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate if (cnp == NULL) 16320Sstevel@tonic-gate return; 16330Sstevel@tonic-gate 16340Sstevel@tonic-gate if (cnp->minor != NULL || cnp->child != NULL) 16350Sstevel@tonic-gate return; 16360Sstevel@tonic-gate 16370Sstevel@tonic-gate (void) dprintf(DBG_INFO, "%s: removing unused node: %s\n", fcn, 16380Sstevel@tonic-gate cnp->path); 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate /* Unlink node from tree */ 16410Sstevel@tonic-gate if (cnp->parent != NULL) { 16420Sstevel@tonic-gate npp = &cnp->parent->child; 16430Sstevel@tonic-gate for (; *npp != NULL; npp = &(*npp)->sib) { 16440Sstevel@tonic-gate if (*npp == cnp) 16450Sstevel@tonic-gate break; 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate if (*npp == NULL) { 16490Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: dangling node: %s\n", fcn, 16500Sstevel@tonic-gate cnp->path); 16510Sstevel@tonic-gate } else { 16520Sstevel@tonic-gate *npp = cnp->sib; 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate } else if (cnp == CACHE_ROOT(hdp)) { 16550Sstevel@tonic-gate CACHE_ROOT(hdp) = NULL; 16560Sstevel@tonic-gate } else { 16570Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: orphan node (%s)\n", fcn, 16580Sstevel@tonic-gate cnp->path); 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate delete_unused_nodes(hdp, cnp->parent); 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate cnp->parent = cnp->sib = NULL; 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate node_free(&cnp); 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate 16680Sstevel@tonic-gate static int 16690Sstevel@tonic-gate rm_link(di_devlink_handle_t hdp, const char *link) 16700Sstevel@tonic-gate { 16710Sstevel@tonic-gate cache_link_t *clp; 16720Sstevel@tonic-gate const char *fcn = "rm_link"; 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' || 16750Sstevel@tonic-gate (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) { 16760Sstevel@tonic-gate dprintf(DBG_ERR, "%s: %s: invalid args\n", 16770Sstevel@tonic-gate fcn, link ? link : "<NULL>"); 16780Sstevel@tonic-gate errno = EINVAL; 16790Sstevel@tonic-gate return (-1); 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate dprintf(DBG_STEP, "%s: link(%s)\n", fcn, link); 16830Sstevel@tonic-gate 16840Sstevel@tonic-gate if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) { 16850Sstevel@tonic-gate return (0); 16860Sstevel@tonic-gate } 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate link_delete(hdp, clp); 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate return (0); 16910Sstevel@tonic-gate } 16920Sstevel@tonic-gate 16930Sstevel@tonic-gate int 16940Sstevel@tonic-gate di_devlink_rm_link(di_devlink_handle_t hdp, const char *link) 16950Sstevel@tonic-gate { 16960Sstevel@tonic-gate if (hdp == NULL || !HDL_RDWR(hdp)) { 16970Sstevel@tonic-gate errno = EINVAL; 16980Sstevel@tonic-gate return (-1); 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate return (rm_link(hdp, link)); 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate static void 17050Sstevel@tonic-gate link_delete(di_devlink_handle_t hdp, cache_link_t *clp) 17060Sstevel@tonic-gate { 17070Sstevel@tonic-gate cache_link_t **pp; 17080Sstevel@tonic-gate const char *fcn = "link_delete"; 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: removing link: %s\n", fcn, clp->path); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate if (clp->minor == NULL) 17130Sstevel@tonic-gate pp = &(CACHE(hdp)->dngl); 17140Sstevel@tonic-gate else 17150Sstevel@tonic-gate pp = &clp->minor->link; 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate for (; *pp != NULL; pp = &(*pp)->sib) { 17180Sstevel@tonic-gate if (*pp == clp) 17190Sstevel@tonic-gate break; 17200Sstevel@tonic-gate } 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate if (*pp == NULL) { 17230Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: link(%s) not on list\n", 17240Sstevel@tonic-gate fcn, clp->path); 17250Sstevel@tonic-gate } else { 17260Sstevel@tonic-gate *pp = clp->sib; 17270Sstevel@tonic-gate } 17280Sstevel@tonic-gate 17290Sstevel@tonic-gate delete_unused_minor(hdp, clp->minor); 17300Sstevel@tonic-gate 17310Sstevel@tonic-gate clp->minor = NULL; 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate link_free(&clp); 17340Sstevel@tonic-gate } 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate static void 17370Sstevel@tonic-gate delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp) 17380Sstevel@tonic-gate { 17390Sstevel@tonic-gate if (cmnp == NULL) 17400Sstevel@tonic-gate return; 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate if (cmnp->link != NULL) 17430Sstevel@tonic-gate return; 17440Sstevel@tonic-gate 17450Sstevel@tonic-gate dprintf(DBG_STEP, "delete_unused_minor: removing minor(%s)\n", 17460Sstevel@tonic-gate cmnp->name); 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate minor_delete(hdp, cmnp); 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate int 17520Sstevel@tonic-gate di_devlink_add_link( 17530Sstevel@tonic-gate di_devlink_handle_t hdp, 17540Sstevel@tonic-gate const char *link, 17550Sstevel@tonic-gate const char *content, 17560Sstevel@tonic-gate int flags) 17570Sstevel@tonic-gate { 17580Sstevel@tonic-gate return (add_link(hdp, link, content, flags) != NULL ? 0 : -1); 17590Sstevel@tonic-gate } 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate static cache_link_t * 17620Sstevel@tonic-gate add_link( 17630Sstevel@tonic-gate struct di_devlink_handle *hdp, 17640Sstevel@tonic-gate const char *link, 17650Sstevel@tonic-gate const char *content, 17660Sstevel@tonic-gate int flags) 17670Sstevel@tonic-gate { 17680Sstevel@tonic-gate uint32_t attr; 17690Sstevel@tonic-gate cache_link_t *clp; 17700Sstevel@tonic-gate cache_minor_t *cmnp; 17710Sstevel@tonic-gate const char *fcn = "add_link"; 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate if (hdp == NULL || DB_ERR(hdp) || link == NULL || 17740Sstevel@tonic-gate link[0] == '/' || content == NULL || !link_flag(flags) || 17750Sstevel@tonic-gate (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) { 17760Sstevel@tonic-gate dprintf(DBG_ERR, "%s: %s: invalid args\n", 17770Sstevel@tonic-gate fcn, link ? link : "<NULL>"); 17780Sstevel@tonic-gate errno = EINVAL; 17790Sstevel@tonic-gate return (NULL); 17800Sstevel@tonic-gate } 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate if ((clp = link_hash(hdp, link, 0)) != NULL) { 17830Sstevel@tonic-gate if (link_cmp(clp, content, LINK_TYPE(flags)) != 0) { 17840Sstevel@tonic-gate (void) rm_link(hdp, link); 17850Sstevel@tonic-gate } else { 17860Sstevel@tonic-gate return (clp); 17870Sstevel@tonic-gate } 17880Sstevel@tonic-gate } 17890Sstevel@tonic-gate 17900Sstevel@tonic-gate if (TYPE_PRI(flags)) { 17910Sstevel@tonic-gate const char *minor_path = NULL; 17920Sstevel@tonic-gate 17930Sstevel@tonic-gate if (!is_minor_node(content, &minor_path)) { 17940Sstevel@tonic-gate (void) dprintf(DBG_ERR, "%s: invalid content(%s)" 17950Sstevel@tonic-gate " for primary link\n", fcn, content); 17960Sstevel@tonic-gate errno = EINVAL; 17970Sstevel@tonic-gate return (NULL); 17980Sstevel@tonic-gate } 17990Sstevel@tonic-gate if ((cmnp = lookup_minor(hdp, minor_path, NULL, 18000Sstevel@tonic-gate TYPE_CACHE|CREATE_FLAG)) == NULL) { 18010Sstevel@tonic-gate return (NULL); 18020Sstevel@tonic-gate } 18030Sstevel@tonic-gate attr = A_PRIMARY; 18040Sstevel@tonic-gate } else { 18050Sstevel@tonic-gate /* 18060Sstevel@tonic-gate * Defer resolving a secondary link to a minor until the 18070Sstevel@tonic-gate * database is closed. This ensures that the primary link 18080Sstevel@tonic-gate * (required for a successful resolve) has also been created. 18090Sstevel@tonic-gate */ 18100Sstevel@tonic-gate cmnp = NULL; 18110Sstevel@tonic-gate attr = A_SECONDARY; 18120Sstevel@tonic-gate } 18130Sstevel@tonic-gate 18140Sstevel@tonic-gate return (link_insert(hdp, cmnp, link, content, attr)); 18150Sstevel@tonic-gate } 18160Sstevel@tonic-gate 18170Sstevel@tonic-gate /* 18180Sstevel@tonic-gate * Returns 0 on match or 1 otherwise. 18190Sstevel@tonic-gate */ 18200Sstevel@tonic-gate static int 18210Sstevel@tonic-gate link_cmp(cache_link_t *clp, const char *content, int type) 18220Sstevel@tonic-gate { 18230Sstevel@tonic-gate if (strcmp(clp->content, content) != 0) 18240Sstevel@tonic-gate return (1); 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate if (attr2type(clp->attr) != type) 18270Sstevel@tonic-gate return (1); 18280Sstevel@tonic-gate 18290Sstevel@tonic-gate return (0); 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate int 18330Sstevel@tonic-gate di_devlink_update(di_devlink_handle_t hdp) 18340Sstevel@tonic-gate { 18350Sstevel@tonic-gate if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) { 18360Sstevel@tonic-gate errno = EINVAL; 18370Sstevel@tonic-gate return (-1); 18380Sstevel@tonic-gate } 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate /* 18410Sstevel@tonic-gate * Reset the counter to schedule a synchronization with /dev on the next 18420Sstevel@tonic-gate * di_devlink_close(). 18430Sstevel@tonic-gate */ 18440Sstevel@tonic-gate CACHE(hdp)->update_count = 0; 18450Sstevel@tonic-gate 18460Sstevel@tonic-gate return (0); 18470Sstevel@tonic-gate } 18480Sstevel@tonic-gate 18490Sstevel@tonic-gate static int 18500Sstevel@tonic-gate synchronize_db(di_devlink_handle_t hdp) 18510Sstevel@tonic-gate { 18520Sstevel@tonic-gate int hval; 18530Sstevel@tonic-gate cache_link_t *clp; 18540Sstevel@tonic-gate char pdup[PATH_MAX]; 18550Sstevel@tonic-gate recurse_t rec = {NULL}; 18560Sstevel@tonic-gate const char *fcn = "synchronize_db"; 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate rec.data = NULL; 18590Sstevel@tonic-gate rec.fcn = cache_dev_link; 18600Sstevel@tonic-gate 18610Sstevel@tonic-gate /* 18620Sstevel@tonic-gate * Walk through $ROOT/dev, reading every link and marking the 18630Sstevel@tonic-gate * corresponding cached version as valid(adding new links as needed). 18640Sstevel@tonic-gate * Then walk through the cache and remove all unmarked links. 18650Sstevel@tonic-gate */ 18660Sstevel@tonic-gate if (recurse_dev(hdp, &rec) != 0) { 18670Sstevel@tonic-gate return (-1); 18680Sstevel@tonic-gate } 18690Sstevel@tonic-gate 18700Sstevel@tonic-gate for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) { 18710Sstevel@tonic-gate for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) { 18720Sstevel@tonic-gate if (GET_VALID_ATTR(clp->attr)) { 18730Sstevel@tonic-gate CLR_VALID_ATTR(clp->attr); 18740Sstevel@tonic-gate clp = clp->hash; 18750Sstevel@tonic-gate continue; 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate /* 18790Sstevel@tonic-gate * The link is stale, so remove it. Since the link 18800Sstevel@tonic-gate * will be destroyed, use a copy of the link path to 18810Sstevel@tonic-gate * invoke the remove function. 18820Sstevel@tonic-gate */ 18830Sstevel@tonic-gate (void) snprintf(pdup, sizeof (pdup), "%s", clp->path); 18840Sstevel@tonic-gate clp = clp->hash; 18850Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: removing invalid link:" 18860Sstevel@tonic-gate " %s\n", fcn, pdup); 18870Sstevel@tonic-gate (void) di_devlink_rm_link(hdp, pdup); 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate } 18900Sstevel@tonic-gate 18910Sstevel@tonic-gate (void) dprintf(DBG_STEP, "%s: update completed\n", fcn); 18920Sstevel@tonic-gate 18930Sstevel@tonic-gate return (0); 18940Sstevel@tonic-gate } 18950Sstevel@tonic-gate 18960Sstevel@tonic-gate static di_devlink_handle_t 18970Sstevel@tonic-gate di_devlink_init_impl(const char *root, const char *name, uint_t flags) 18980Sstevel@tonic-gate { 18990Sstevel@tonic-gate int err = 0; 19000Sstevel@tonic-gate 19010Sstevel@tonic-gate if ((flags != 0 && flags != DI_MAKE_LINK) || 19020Sstevel@tonic-gate (flags == 0 && name != NULL)) { 19030Sstevel@tonic-gate errno = EINVAL; 19040Sstevel@tonic-gate return (NULL); 19050Sstevel@tonic-gate } 19060Sstevel@tonic-gate 19070Sstevel@tonic-gate if (flags == DI_MAKE_LINK && (err = devlink_create(root, name))) { 19080Sstevel@tonic-gate errno = err; 19090Sstevel@tonic-gate return (NULL); 19100Sstevel@tonic-gate } 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate (void) dprintf(DBG_INFO, "devlink_init_impl: success\n"); 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate return (devlink_snapshot(root)); 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate di_devlink_handle_t 19180Sstevel@tonic-gate di_devlink_init(const char *name, uint_t flags) 19190Sstevel@tonic-gate { 19200Sstevel@tonic-gate return (di_devlink_init_impl("/", name, flags)); 19210Sstevel@tonic-gate } 19220Sstevel@tonic-gate 19230Sstevel@tonic-gate di_devlink_handle_t 19240Sstevel@tonic-gate di_devlink_init_root(const char *root, const char *name, uint_t flags) 19250Sstevel@tonic-gate { 19260Sstevel@tonic-gate return (di_devlink_init_impl(root, name, flags)); 19270Sstevel@tonic-gate } 19280Sstevel@tonic-gate 19290Sstevel@tonic-gate static di_devlink_handle_t 19300Sstevel@tonic-gate devlink_snapshot(const char *root_dir) 19310Sstevel@tonic-gate { 19320Sstevel@tonic-gate struct di_devlink_handle *hdp; 19330Sstevel@tonic-gate 19340Sstevel@tonic-gate if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) { 19350Sstevel@tonic-gate return (NULL); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate /* 19390Sstevel@tonic-gate * If we cannot open the DB below, we will walk /dev 19400Sstevel@tonic-gate * in di_devlink_walk. 19410Sstevel@tonic-gate */ 19420Sstevel@tonic-gate (void) open_db(hdp, OPEN_RDONLY); 19430Sstevel@tonic-gate 19440Sstevel@tonic-gate return (hdp); 19450Sstevel@tonic-gate } 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate int 19480Sstevel@tonic-gate di_devlink_fini(di_devlink_handle_t *pp) 19490Sstevel@tonic-gate { 19500Sstevel@tonic-gate if (pp == NULL || *pp == NULL || !HDL_RDONLY(*pp)) { 19510Sstevel@tonic-gate errno = EINVAL; 19520Sstevel@tonic-gate return (-1); 19530Sstevel@tonic-gate } 19540Sstevel@tonic-gate 19550Sstevel@tonic-gate /* Freeing the handle also closes the DB */ 19560Sstevel@tonic-gate handle_free(pp); 19570Sstevel@tonic-gate 19580Sstevel@tonic-gate return (0); 19590Sstevel@tonic-gate } 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate int 19620Sstevel@tonic-gate di_devlink_walk( 19630Sstevel@tonic-gate di_devlink_handle_t hdp, 19640Sstevel@tonic-gate const char *re, 19650Sstevel@tonic-gate const char *minor_path, 19660Sstevel@tonic-gate uint_t flags, 19670Sstevel@tonic-gate void *arg, 19680Sstevel@tonic-gate int (*devlink_callback)(di_devlink_t, void *)) 19690Sstevel@tonic-gate { 19700Sstevel@tonic-gate int rv; 19710Sstevel@tonic-gate regex_t reg; 19720Sstevel@tonic-gate link_desc_t linkd = {NULL}; 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate if (hdp == NULL || !HDL_RDONLY(hdp)) { 19750Sstevel@tonic-gate errno = EINVAL; 19760Sstevel@tonic-gate return (-1); 19770Sstevel@tonic-gate } 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate linkd.minor_path = minor_path; 19800Sstevel@tonic-gate linkd.flags = flags; 19810Sstevel@tonic-gate linkd.arg = arg; 19820Sstevel@tonic-gate linkd.fcn = devlink_callback; 19830Sstevel@tonic-gate 19840Sstevel@tonic-gate if (re) { 19850Sstevel@tonic-gate if (regcomp(®, re, REG_EXTENDED) != 0) 19860Sstevel@tonic-gate return (-1); 19870Sstevel@tonic-gate linkd.regp = ® 19880Sstevel@tonic-gate } 19890Sstevel@tonic-gate 19900Sstevel@tonic-gate if (check_args(&linkd)) { 19910Sstevel@tonic-gate errno = EINVAL; 19920Sstevel@tonic-gate rv = -1; 19930Sstevel@tonic-gate goto out; 19940Sstevel@tonic-gate } 19950Sstevel@tonic-gate 19960Sstevel@tonic-gate if (DB_OPEN(hdp)) { 19970Sstevel@tonic-gate rv = walk_db(hdp, &linkd); 19980Sstevel@tonic-gate } else { 19990Sstevel@tonic-gate rv = walk_dev(hdp, &linkd); 20000Sstevel@tonic-gate } 20010Sstevel@tonic-gate 20020Sstevel@tonic-gate out: 20030Sstevel@tonic-gate if (re) { 20040Sstevel@tonic-gate regfree(®); 20050Sstevel@tonic-gate } 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate return (rv ? -1 : 0); 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate static int 20110Sstevel@tonic-gate link_flag(uint_t flags) 20120Sstevel@tonic-gate { 20130Sstevel@tonic-gate if (flags != 0 && flags != DI_PRIMARY_LINK && 20140Sstevel@tonic-gate flags != DI_SECONDARY_LINK) { 20150Sstevel@tonic-gate return (0); 20160Sstevel@tonic-gate } 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate return (1); 20190Sstevel@tonic-gate } 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate /* 20220Sstevel@tonic-gate * Currently allowed flags are: 20230Sstevel@tonic-gate * DI_PRIMARY_LINK 20240Sstevel@tonic-gate * DI_SECONDARY_LINK 20250Sstevel@tonic-gate */ 20260Sstevel@tonic-gate static int 20270Sstevel@tonic-gate check_args(link_desc_t *linkp) 20280Sstevel@tonic-gate { 20290Sstevel@tonic-gate if (linkp->fcn == NULL) 20300Sstevel@tonic-gate return (-1); 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate if (!link_flag(linkp->flags)) { 20330Sstevel@tonic-gate return (-1); 20340Sstevel@tonic-gate } 20350Sstevel@tonic-gate 20360Sstevel@tonic-gate /* 20370Sstevel@tonic-gate * Minor path can be NULL. In that case, all links will be 20380Sstevel@tonic-gate * selected. 20390Sstevel@tonic-gate */ 20400Sstevel@tonic-gate if (linkp->minor_path) { 20410Sstevel@tonic-gate if (linkp->minor_path[0] != '/' || 20420Sstevel@tonic-gate minor_colon(linkp->minor_path) == NULL) { 20430Sstevel@tonic-gate return (-1); 20440Sstevel@tonic-gate } 20450Sstevel@tonic-gate } 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate return (0); 20480Sstevel@tonic-gate } 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate 20510Sstevel@tonic-gate /* 20520Sstevel@tonic-gate * Walk all links in database if no minor path is specified. 20530Sstevel@tonic-gate */ 20540Sstevel@tonic-gate static int 20550Sstevel@tonic-gate walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp) 20560Sstevel@tonic-gate { 20570Sstevel@tonic-gate assert(DB_OPEN(hdp)); 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate if (linkp->minor_path == NULL) { 20600Sstevel@tonic-gate return (walk_all_links(hdp, linkp)); 20610Sstevel@tonic-gate } else { 20620Sstevel@tonic-gate return (walk_matching_links(hdp, linkp)); 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate static int 20670Sstevel@tonic-gate cache_dev(struct di_devlink_handle *hdp) 20680Sstevel@tonic-gate { 20690Sstevel@tonic-gate size_t sz; 20700Sstevel@tonic-gate recurse_t rec = {NULL}; 20710Sstevel@tonic-gate 20720Sstevel@tonic-gate assert(hdp); 20730Sstevel@tonic-gate assert(HDL_RDONLY(hdp)); 20740Sstevel@tonic-gate 20750Sstevel@tonic-gate if (hdp == NULL || !HDL_RDONLY(hdp)) { 20760Sstevel@tonic-gate dprintf(DBG_ERR, "cache_dev: invalid arg\n"); 20770Sstevel@tonic-gate return (-1); 20780Sstevel@tonic-gate } 20790Sstevel@tonic-gate 20800Sstevel@tonic-gate sz = MIN_HASH_SIZE; 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *)); 20830Sstevel@tonic-gate if (CACHE(hdp)->hash == NULL) { 20840Sstevel@tonic-gate return (-1); 20850Sstevel@tonic-gate } 20860Sstevel@tonic-gate CACHE(hdp)->hash_sz = sz; 20870Sstevel@tonic-gate 20880Sstevel@tonic-gate rec.data = NULL; 20890Sstevel@tonic-gate rec.fcn = cache_dev_link; 20900Sstevel@tonic-gate 20910Sstevel@tonic-gate return (recurse_dev(hdp, &rec)); 20920Sstevel@tonic-gate } 20930Sstevel@tonic-gate 20940Sstevel@tonic-gate static int 20950Sstevel@tonic-gate walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp) 20960Sstevel@tonic-gate { 20970Sstevel@tonic-gate assert(hdp && linkp); 20980Sstevel@tonic-gate assert(!DB_OPEN(hdp)); 20990Sstevel@tonic-gate assert(HDL_RDONLY(hdp)); 21000Sstevel@tonic-gate 21010Sstevel@tonic-gate if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) { 21020Sstevel@tonic-gate dprintf(DBG_ERR, "walk_dev: invalid args\n"); 21030Sstevel@tonic-gate return (-1); 21040Sstevel@tonic-gate } 21050Sstevel@tonic-gate 21060Sstevel@tonic-gate if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) { 21070Sstevel@tonic-gate dprintf(DBG_ERR, "walk_dev: /dev caching failed\n"); 21080Sstevel@tonic-gate return (-1); 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate if (linkp->minor_path) 21120Sstevel@tonic-gate walk_cache_minor(hdp, linkp->minor_path, linkp); 21130Sstevel@tonic-gate else 21140Sstevel@tonic-gate walk_all_cache(hdp, linkp); 21150Sstevel@tonic-gate 21160Sstevel@tonic-gate return (linkp->retval); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate 21190Sstevel@tonic-gate /* ARGSUSED */ 21200Sstevel@tonic-gate static int 21210Sstevel@tonic-gate cache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link) 21220Sstevel@tonic-gate { 21230Sstevel@tonic-gate int flags; 21240Sstevel@tonic-gate cache_link_t *clp; 21250Sstevel@tonic-gate char content[PATH_MAX]; 21260Sstevel@tonic-gate 21270Sstevel@tonic-gate assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp)); 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate if (s_readlink(link, content, sizeof (content)) < 0) { 21300Sstevel@tonic-gate return (DI_WALK_CONTINUE); 21310Sstevel@tonic-gate } 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate if (is_minor_node(content, NULL)) { 21340Sstevel@tonic-gate flags = DI_PRIMARY_LINK; 21350Sstevel@tonic-gate } else { 21360Sstevel@tonic-gate flags = DI_SECONDARY_LINK; 21370Sstevel@tonic-gate } 21380Sstevel@tonic-gate 21390Sstevel@tonic-gate assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0); 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate /* 21420Sstevel@tonic-gate * Store only the part after <root-dir>/dev/ 21430Sstevel@tonic-gate */ 21440Sstevel@tonic-gate link += strlen(hdp->dev_dir) + 1; 21450Sstevel@tonic-gate 21460Sstevel@tonic-gate if ((clp = add_link(hdp, link, content, flags)) != NULL) { 21470Sstevel@tonic-gate SET_VALID_ATTR(clp->attr); 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate return (DI_WALK_CONTINUE); 21510Sstevel@tonic-gate } 21520Sstevel@tonic-gate 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate static int 21550Sstevel@tonic-gate walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp) 21560Sstevel@tonic-gate { 21570Sstevel@tonic-gate struct db_link *dlp; 21580Sstevel@tonic-gate uint32_t nidx, eidx; 21590Sstevel@tonic-gate 21600Sstevel@tonic-gate assert(DB_NUM(hdp, DB_LINK) >= 1); 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate eidx = DB_NUM(hdp, DB_LINK); 21630Sstevel@tonic-gate 21640Sstevel@tonic-gate /* Skip the "NIL" (index == 0) link. */ 21650Sstevel@tonic-gate for (nidx = 1; nidx < eidx; nidx++) { 21660Sstevel@tonic-gate /* 21670Sstevel@tonic-gate * Declare this local to the block with zero 21680Sstevel@tonic-gate * initializer so that it gets rezeroed 21690Sstevel@tonic-gate * for each iteration. 21700Sstevel@tonic-gate */ 21710Sstevel@tonic-gate struct di_devlink vlink = {NULL}; 21720Sstevel@tonic-gate 21730Sstevel@tonic-gate if ((dlp = get_link(hdp, nidx)) == NULL) 21740Sstevel@tonic-gate continue; 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate vlink.rel_path = get_string(hdp, dlp->path); 21770Sstevel@tonic-gate vlink.content = get_string(hdp, dlp->content); 21780Sstevel@tonic-gate vlink.type = attr2type(dlp->attr); 21790Sstevel@tonic-gate 21800Sstevel@tonic-gate if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) { 21810Sstevel@tonic-gate break; 21820Sstevel@tonic-gate } 21830Sstevel@tonic-gate } 21840Sstevel@tonic-gate 21850Sstevel@tonic-gate return (linkp->retval); 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate static int 21890Sstevel@tonic-gate walk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp) 21900Sstevel@tonic-gate { 21910Sstevel@tonic-gate uint32_t nidx; 21920Sstevel@tonic-gate struct db_link *dlp; 21930Sstevel@tonic-gate struct db_minor *dmp; 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate assert(linkp->minor_path != NULL); 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB); 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate /* 22000Sstevel@tonic-gate * If a minor matching the path exists, walk that minor's devlinks list. 22010Sstevel@tonic-gate * Then walk the dangling devlinks list. Non-matching devlinks will be 22020Sstevel@tonic-gate * filtered out in visit_link. 22030Sstevel@tonic-gate */ 22040Sstevel@tonic-gate for (;;) { 22050Sstevel@tonic-gate nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx; 22060Sstevel@tonic-gate for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) { 22070Sstevel@tonic-gate struct di_devlink vlink = {NULL}; 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate vlink.rel_path = get_string(hdp, dlp->path); 22100Sstevel@tonic-gate vlink.content = get_string(hdp, dlp->content); 22110Sstevel@tonic-gate vlink.type = attr2type(dlp->attr); 22120Sstevel@tonic-gate 22130Sstevel@tonic-gate if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) 22140Sstevel@tonic-gate goto out; 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate if (dmp == NULL) { 22170Sstevel@tonic-gate break; 22180Sstevel@tonic-gate } else { 22190Sstevel@tonic-gate dmp = NULL; 22200Sstevel@tonic-gate } 22210Sstevel@tonic-gate } 22220Sstevel@tonic-gate 22230Sstevel@tonic-gate out: 22240Sstevel@tonic-gate return (linkp->retval); 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate static int 22280Sstevel@tonic-gate visit_link( 22290Sstevel@tonic-gate struct di_devlink_handle *hdp, 22300Sstevel@tonic-gate link_desc_t *linkp, 22310Sstevel@tonic-gate struct di_devlink *vlp) 22320Sstevel@tonic-gate { 22330Sstevel@tonic-gate struct stat sbuf; 22340Sstevel@tonic-gate const char *minor_path = NULL; 22350Sstevel@tonic-gate char abs_path[PATH_MAX], cont[PATH_MAX]; 22360Sstevel@tonic-gate 22370Sstevel@tonic-gate /* 22380Sstevel@tonic-gate * It is legal for the link's content and type to be unknown. 22390Sstevel@tonic-gate * but one of absolute or relative path must be set. 22400Sstevel@tonic-gate */ 22410Sstevel@tonic-gate if (vlp->rel_path == NULL && vlp->abs_path == NULL) { 22420Sstevel@tonic-gate (void) dprintf(DBG_ERR, "visit_link: invalid arguments\n"); 22430Sstevel@tonic-gate return (DI_WALK_CONTINUE); 22440Sstevel@tonic-gate } 22450Sstevel@tonic-gate 22460Sstevel@tonic-gate if (vlp->rel_path == NULL) { 22470Sstevel@tonic-gate vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path); 22480Sstevel@tonic-gate if (vlp->rel_path == NULL || vlp->rel_path[0] == '\0') 22490Sstevel@tonic-gate return (DI_WALK_CONTINUE); 22500Sstevel@tonic-gate } 22510Sstevel@tonic-gate 22520Sstevel@tonic-gate if (linkp->regp) { 22530Sstevel@tonic-gate if (regexec(linkp->regp, vlp->rel_path, 0, NULL, 0) != 0) 22540Sstevel@tonic-gate return (DI_WALK_CONTINUE); 22550Sstevel@tonic-gate } 22560Sstevel@tonic-gate 22570Sstevel@tonic-gate if (vlp->abs_path == NULL) { 22580Sstevel@tonic-gate assert(vlp->rel_path[0] != '/'); 22590Sstevel@tonic-gate (void) snprintf(abs_path, sizeof (abs_path), "%s/%s", 22600Sstevel@tonic-gate hdp->dev_dir, vlp->rel_path); 22610Sstevel@tonic-gate vlp->abs_path = abs_path; 22620Sstevel@tonic-gate } 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate if (vlp->content == NULL) { 22650Sstevel@tonic-gate if (s_readlink(vlp->abs_path, cont, sizeof (cont)) < 0) { 22660Sstevel@tonic-gate return (DI_WALK_CONTINUE); 22670Sstevel@tonic-gate } 22680Sstevel@tonic-gate vlp->content = cont; 22690Sstevel@tonic-gate } 22700Sstevel@tonic-gate 22710Sstevel@tonic-gate 22720Sstevel@tonic-gate if (vlp->type == 0) { 22730Sstevel@tonic-gate if (is_minor_node(vlp->content, &minor_path)) { 22740Sstevel@tonic-gate vlp->type = DI_PRIMARY_LINK; 22750Sstevel@tonic-gate } else { 22760Sstevel@tonic-gate vlp->type = DI_SECONDARY_LINK; 22770Sstevel@tonic-gate } 22780Sstevel@tonic-gate } 22790Sstevel@tonic-gate 22800Sstevel@tonic-gate /* 22810Sstevel@tonic-gate * Filter based on minor path 22820Sstevel@tonic-gate */ 22830Sstevel@tonic-gate if (linkp->minor_path) { 22840Sstevel@tonic-gate char tmp[PATH_MAX]; 22850Sstevel@tonic-gate 22860Sstevel@tonic-gate /* 22870Sstevel@tonic-gate * derive minor path 22880Sstevel@tonic-gate */ 22890Sstevel@tonic-gate if (vlp->type == DI_SECONDARY_LINK) { 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate #ifdef DEBUG 22920Sstevel@tonic-gate /*LINTED*/ 22930Sstevel@tonic-gate assert(sizeof (tmp) >= PATH_MAX); 22940Sstevel@tonic-gate #endif 22950Sstevel@tonic-gate if (realpath(vlp->abs_path, tmp) == NULL) 22960Sstevel@tonic-gate return (DI_WALK_CONTINUE); 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate if (!is_minor_node(tmp, &minor_path)) 22990Sstevel@tonic-gate return (DI_WALK_CONTINUE); 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate } else if (minor_path == NULL) { 23020Sstevel@tonic-gate if (!is_minor_node(vlp->content, &minor_path)) 23030Sstevel@tonic-gate return (DI_WALK_CONTINUE); 23040Sstevel@tonic-gate } 23050Sstevel@tonic-gate 23060Sstevel@tonic-gate assert(minor_path != NULL); 23070Sstevel@tonic-gate 23080Sstevel@tonic-gate if (strcmp(linkp->minor_path, minor_path) != 0) 23090Sstevel@tonic-gate return (DI_WALK_CONTINUE); 23100Sstevel@tonic-gate } 23110Sstevel@tonic-gate 23120Sstevel@tonic-gate /* 23130Sstevel@tonic-gate * Filter based on link type 23140Sstevel@tonic-gate */ 23150Sstevel@tonic-gate if (!TYPE_NONE(linkp->flags) && LINK_TYPE(linkp->flags) != vlp->type) { 23160Sstevel@tonic-gate return (DI_WALK_CONTINUE); 23170Sstevel@tonic-gate } 23180Sstevel@tonic-gate 23190Sstevel@tonic-gate if (lstat(vlp->abs_path, &sbuf) < 0) { 23200Sstevel@tonic-gate dprintf(DBG_ERR, "visit_link: %s: lstat failed: %s\n", 23210Sstevel@tonic-gate vlp->abs_path, strerror(errno)); 23220Sstevel@tonic-gate return (DI_WALK_CONTINUE); 23230Sstevel@tonic-gate } 23240Sstevel@tonic-gate 23250Sstevel@tonic-gate return (linkp->fcn(vlp, linkp->arg)); 23260Sstevel@tonic-gate } 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate static int 23290Sstevel@tonic-gate devlink_valid(di_devlink_t devlink) 23300Sstevel@tonic-gate { 23310Sstevel@tonic-gate if (devlink == NULL || devlink->rel_path == NULL || 23320Sstevel@tonic-gate devlink->abs_path == NULL || devlink->content == NULL || 23330Sstevel@tonic-gate TYPE_NONE(devlink->type)) { 23340Sstevel@tonic-gate return (0); 23350Sstevel@tonic-gate } 23360Sstevel@tonic-gate 23370Sstevel@tonic-gate return (1); 23380Sstevel@tonic-gate } 23390Sstevel@tonic-gate 23400Sstevel@tonic-gate const char * 23410Sstevel@tonic-gate di_devlink_path(di_devlink_t devlink) 23420Sstevel@tonic-gate { 23430Sstevel@tonic-gate if (!devlink_valid(devlink)) { 23440Sstevel@tonic-gate errno = EINVAL; 23450Sstevel@tonic-gate return (NULL); 23460Sstevel@tonic-gate } 23470Sstevel@tonic-gate 23480Sstevel@tonic-gate return (devlink->abs_path); 23490Sstevel@tonic-gate } 23500Sstevel@tonic-gate 23510Sstevel@tonic-gate const char * 23520Sstevel@tonic-gate di_devlink_content(di_devlink_t devlink) 23530Sstevel@tonic-gate { 23540Sstevel@tonic-gate if (!devlink_valid(devlink)) { 23550Sstevel@tonic-gate errno = EINVAL; 23560Sstevel@tonic-gate return (NULL); 23570Sstevel@tonic-gate } 23580Sstevel@tonic-gate 23590Sstevel@tonic-gate return (devlink->content); 23600Sstevel@tonic-gate } 23610Sstevel@tonic-gate 23620Sstevel@tonic-gate int 23630Sstevel@tonic-gate di_devlink_type(di_devlink_t devlink) 23640Sstevel@tonic-gate { 23650Sstevel@tonic-gate if (!devlink_valid(devlink)) { 23660Sstevel@tonic-gate errno = EINVAL; 23670Sstevel@tonic-gate return (-1); 23680Sstevel@tonic-gate } 23690Sstevel@tonic-gate 23700Sstevel@tonic-gate return (devlink->type); 23710Sstevel@tonic-gate } 23720Sstevel@tonic-gate 23730Sstevel@tonic-gate di_devlink_t 23740Sstevel@tonic-gate di_devlink_dup(di_devlink_t devlink) 23750Sstevel@tonic-gate { 23760Sstevel@tonic-gate struct di_devlink *duplink; 23770Sstevel@tonic-gate 23780Sstevel@tonic-gate if (!devlink_valid(devlink)) { 23790Sstevel@tonic-gate errno = EINVAL; 23800Sstevel@tonic-gate return (NULL); 23810Sstevel@tonic-gate } 23820Sstevel@tonic-gate 23830Sstevel@tonic-gate if ((duplink = calloc(1, sizeof (struct di_devlink))) == NULL) { 23840Sstevel@tonic-gate return (NULL); 23850Sstevel@tonic-gate } 23860Sstevel@tonic-gate 23870Sstevel@tonic-gate duplink->rel_path = strdup(devlink->rel_path); 23880Sstevel@tonic-gate duplink->abs_path = strdup(devlink->abs_path); 23890Sstevel@tonic-gate duplink->content = strdup(devlink->content); 23900Sstevel@tonic-gate duplink->type = devlink->type; 23910Sstevel@tonic-gate 23920Sstevel@tonic-gate if (!devlink_valid(duplink)) { 23930Sstevel@tonic-gate (void) di_devlink_free(duplink); 23940Sstevel@tonic-gate errno = ENOMEM; 23950Sstevel@tonic-gate return (NULL); 23960Sstevel@tonic-gate } 23970Sstevel@tonic-gate 23980Sstevel@tonic-gate return (duplink); 23990Sstevel@tonic-gate } 24000Sstevel@tonic-gate 24010Sstevel@tonic-gate int 24020Sstevel@tonic-gate di_devlink_free(di_devlink_t devlink) 24030Sstevel@tonic-gate { 24040Sstevel@tonic-gate if (devlink == NULL) { 24050Sstevel@tonic-gate errno = EINVAL; 24060Sstevel@tonic-gate return (-1); 24070Sstevel@tonic-gate } 24080Sstevel@tonic-gate 24090Sstevel@tonic-gate free(devlink->rel_path); 24100Sstevel@tonic-gate free(devlink->abs_path); 24110Sstevel@tonic-gate free(devlink->content); 24120Sstevel@tonic-gate free(devlink); 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate return (0); 24150Sstevel@tonic-gate } 24160Sstevel@tonic-gate 24170Sstevel@tonic-gate /* 24180Sstevel@tonic-gate * Obtain path relative to dev_dir 24190Sstevel@tonic-gate */ 24200Sstevel@tonic-gate static const char * 24210Sstevel@tonic-gate rel_path(struct di_devlink_handle *hdp, const char *path) 24220Sstevel@tonic-gate { 24230Sstevel@tonic-gate const size_t len = strlen(hdp->dev_dir); 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate if (strncmp(path, hdp->dev_dir, len) != 0) 24260Sstevel@tonic-gate return (NULL); 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate if (path[len] == '\0') 24290Sstevel@tonic-gate return (&path[len]); 24300Sstevel@tonic-gate 24310Sstevel@tonic-gate if (path[len] != '/') 24320Sstevel@tonic-gate return (NULL); 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate return (&path[len+1]); 24350Sstevel@tonic-gate } 24360Sstevel@tonic-gate 24370Sstevel@tonic-gate static int 24380Sstevel@tonic-gate recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp) 24390Sstevel@tonic-gate { 24400Sstevel@tonic-gate int ret = 0; 24410Sstevel@tonic-gate 24420Sstevel@tonic-gate (void) do_recurse(hdp->dev_dir, hdp, rp, &ret); 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate return (ret); 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate 24470Sstevel@tonic-gate static int 24480Sstevel@tonic-gate do_recurse( 24490Sstevel@tonic-gate const char *dir, 24500Sstevel@tonic-gate struct di_devlink_handle *hdp, 24510Sstevel@tonic-gate recurse_t *rp, 24520Sstevel@tonic-gate int *retp) 24530Sstevel@tonic-gate { 24540Sstevel@tonic-gate size_t len; 24550Sstevel@tonic-gate const char *rel; 24560Sstevel@tonic-gate struct stat sbuf; 24570Sstevel@tonic-gate char cur[PATH_MAX], *cp; 24580Sstevel@tonic-gate int i, rv = DI_WALK_CONTINUE; 24592621Sllai1 finddevhdl_t handle; 24602621Sllai1 char *d_name; 24610Sstevel@tonic-gate 24620Sstevel@tonic-gate 24630Sstevel@tonic-gate if ((rel = rel_path(hdp, dir)) == NULL) 24640Sstevel@tonic-gate return (DI_WALK_CONTINUE); 24650Sstevel@tonic-gate 24660Sstevel@tonic-gate /* 24670Sstevel@tonic-gate * Skip directories we are not interested in. 24680Sstevel@tonic-gate */ 24690Sstevel@tonic-gate for (i = 0; i < N_SKIP_DIRS; i++) { 24700Sstevel@tonic-gate if (strcmp(rel, skip_dirs[i]) == 0) { 24710Sstevel@tonic-gate (void) dprintf(DBG_STEP, "do_recurse: skipping %s\n", 24720Sstevel@tonic-gate dir); 24730Sstevel@tonic-gate return (DI_WALK_CONTINUE); 24740Sstevel@tonic-gate } 24750Sstevel@tonic-gate } 24760Sstevel@tonic-gate 24770Sstevel@tonic-gate (void) dprintf(DBG_STEP, "do_recurse: dir = %s\n", dir); 24780Sstevel@tonic-gate 24792621Sllai1 if (finddev_readdir(dir, &handle) != 0) 24800Sstevel@tonic-gate return (DI_WALK_CONTINUE); 24810Sstevel@tonic-gate 24820Sstevel@tonic-gate (void) snprintf(cur, sizeof (cur), "%s/", dir); 24830Sstevel@tonic-gate len = strlen(cur); 24840Sstevel@tonic-gate cp = cur + len; 24850Sstevel@tonic-gate len = sizeof (cur) - len; 24860Sstevel@tonic-gate 24872621Sllai1 for (;;) { 24882621Sllai1 if ((d_name = (char *)finddev_next(handle)) == NULL) 24892621Sllai1 break; 24902621Sllai1 24912621Sllai1 if (strlcpy(cp, d_name, len) >= len) 24922621Sllai1 break; 24930Sstevel@tonic-gate 24940Sstevel@tonic-gate /* 24950Sstevel@tonic-gate * Skip files we are not interested in. 24960Sstevel@tonic-gate */ 24970Sstevel@tonic-gate for (i = 0; i < N_SKIP_FILES; i++) { 24980Sstevel@tonic-gate 24990Sstevel@tonic-gate rel = rel_path(hdp, cur); 25000Sstevel@tonic-gate if (rel == NULL || strcmp(rel, skip_files[i]) == 0) { 25010Sstevel@tonic-gate (void) dprintf(DBG_STEP, 25020Sstevel@tonic-gate "do_recurse: skipping %s\n", cur); 25030Sstevel@tonic-gate goto next_entry; 25040Sstevel@tonic-gate } 25050Sstevel@tonic-gate } 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate if (lstat(cur, &sbuf) == 0) { 25080Sstevel@tonic-gate if (S_ISDIR(sbuf.st_mode)) { 25090Sstevel@tonic-gate rv = do_recurse(cur, hdp, rp, retp); 25100Sstevel@tonic-gate } else if (S_ISLNK(sbuf.st_mode)) { 25110Sstevel@tonic-gate rv = rp->fcn(hdp, rp->data, cur); 25120Sstevel@tonic-gate } else { 25130Sstevel@tonic-gate (void) dprintf(DBG_STEP, 25140Sstevel@tonic-gate "do_recurse: Skipping entry: %s\n", cur); 25150Sstevel@tonic-gate } 25160Sstevel@tonic-gate } else { 25170Sstevel@tonic-gate (void) dprintf(DBG_ERR, "do_recurse: cur(%s): lstat" 25180Sstevel@tonic-gate " failed: %s\n", cur, strerror(errno)); 25190Sstevel@tonic-gate } 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate next_entry: 25220Sstevel@tonic-gate *cp = '\0'; 25230Sstevel@tonic-gate 25240Sstevel@tonic-gate if (rv != DI_WALK_CONTINUE) 25250Sstevel@tonic-gate break; 25260Sstevel@tonic-gate } 25270Sstevel@tonic-gate 25282621Sllai1 finddev_close(handle); 25290Sstevel@tonic-gate 25300Sstevel@tonic-gate return (rv); 25310Sstevel@tonic-gate } 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate 25340Sstevel@tonic-gate static int 25350Sstevel@tonic-gate check_attr(uint32_t attr) 25360Sstevel@tonic-gate { 25370Sstevel@tonic-gate switch (attr & A_LINK_TYPES) { 25380Sstevel@tonic-gate case A_PRIMARY: 25390Sstevel@tonic-gate case A_SECONDARY: 25400Sstevel@tonic-gate return (1); 25410Sstevel@tonic-gate default: 25420Sstevel@tonic-gate dprintf(DBG_ERR, "check_attr: incorrect attr(%u)\n", 25430Sstevel@tonic-gate attr); 25440Sstevel@tonic-gate return (0); 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate static int 25490Sstevel@tonic-gate attr2type(uint32_t attr) 25500Sstevel@tonic-gate { 25510Sstevel@tonic-gate switch (attr & A_LINK_TYPES) { 25520Sstevel@tonic-gate case A_PRIMARY: 25530Sstevel@tonic-gate return (DI_PRIMARY_LINK); 25540Sstevel@tonic-gate case A_SECONDARY: 25550Sstevel@tonic-gate return (DI_SECONDARY_LINK); 25560Sstevel@tonic-gate default: 25570Sstevel@tonic-gate dprintf(DBG_ERR, "attr2type: incorrect attr(%u)\n", 25580Sstevel@tonic-gate attr); 25590Sstevel@tonic-gate return (0); 25600Sstevel@tonic-gate } 25610Sstevel@tonic-gate } 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate /* Allocate new node and link it in */ 25640Sstevel@tonic-gate static cache_node_t * 25650Sstevel@tonic-gate node_insert( 25660Sstevel@tonic-gate struct di_devlink_handle *hdp, 25670Sstevel@tonic-gate cache_node_t *pcnp, 25680Sstevel@tonic-gate const char *path, 25690Sstevel@tonic-gate int insert) 25700Sstevel@tonic-gate { 25710Sstevel@tonic-gate cache_node_t *cnp; 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate if (path == NULL) { 25740Sstevel@tonic-gate errno = EINVAL; 25750Sstevel@tonic-gate SET_DB_ERR(hdp); 25760Sstevel@tonic-gate return (NULL); 25770Sstevel@tonic-gate } 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate if ((cnp = calloc(1, sizeof (cache_node_t))) == NULL) { 25800Sstevel@tonic-gate SET_DB_ERR(hdp); 25810Sstevel@tonic-gate return (NULL); 25820Sstevel@tonic-gate } 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate if ((cnp->path = strdup(path)) == NULL) { 25850Sstevel@tonic-gate SET_DB_ERR(hdp); 25860Sstevel@tonic-gate free(cnp); 25870Sstevel@tonic-gate return (NULL); 25880Sstevel@tonic-gate } 25890Sstevel@tonic-gate 25900Sstevel@tonic-gate cnp->parent = pcnp; 25910Sstevel@tonic-gate 25920Sstevel@tonic-gate if (pcnp == NULL) { 25930Sstevel@tonic-gate assert(strcmp(path, "/") == 0); 25940Sstevel@tonic-gate assert(CACHE(hdp)->root == NULL); 25950Sstevel@tonic-gate CACHE(hdp)->root = cnp; 25960Sstevel@tonic-gate } else if (insert == INSERT_HEAD) { 25970Sstevel@tonic-gate cnp->sib = pcnp->child; 25980Sstevel@tonic-gate pcnp->child = cnp; 25990Sstevel@tonic-gate } else if (CACHE_LAST(hdp) && CACHE_LAST(hdp)->node && 26000Sstevel@tonic-gate CACHE_LAST(hdp)->node->parent == pcnp && 26010Sstevel@tonic-gate CACHE_LAST(hdp)->node->sib == NULL) { 26020Sstevel@tonic-gate 26030Sstevel@tonic-gate CACHE_LAST(hdp)->node->sib = cnp; 26040Sstevel@tonic-gate 26050Sstevel@tonic-gate } else { 26060Sstevel@tonic-gate cache_node_t **pp; 26070Sstevel@tonic-gate 26080Sstevel@tonic-gate for (pp = &pcnp->child; *pp != NULL; pp = &(*pp)->sib) 26090Sstevel@tonic-gate ; 26100Sstevel@tonic-gate *pp = cnp; 26110Sstevel@tonic-gate } 26120Sstevel@tonic-gate 26130Sstevel@tonic-gate return (cnp); 26140Sstevel@tonic-gate } 26150Sstevel@tonic-gate 26160Sstevel@tonic-gate /* 26170Sstevel@tonic-gate * Allocate a new minor and link it in either at the tail or head 26180Sstevel@tonic-gate * of the minor list depending on the value of "prev". 26190Sstevel@tonic-gate */ 26200Sstevel@tonic-gate static cache_minor_t * 26210Sstevel@tonic-gate minor_insert( 26220Sstevel@tonic-gate struct di_devlink_handle *hdp, 26230Sstevel@tonic-gate cache_node_t *pcnp, 26240Sstevel@tonic-gate const char *name, 26250Sstevel@tonic-gate const char *nodetype, 26260Sstevel@tonic-gate cache_minor_t **prev) 26270Sstevel@tonic-gate { 26280Sstevel@tonic-gate cache_minor_t *cmnp; 26290Sstevel@tonic-gate 26300Sstevel@tonic-gate if (pcnp == NULL || name == NULL) { 26310Sstevel@tonic-gate errno = EINVAL; 26320Sstevel@tonic-gate SET_DB_ERR(hdp); 26330Sstevel@tonic-gate return (NULL); 26340Sstevel@tonic-gate } 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate /* 26370Sstevel@tonic-gate * Some pseudo drivers don't specify nodetype. Assume pseudo if 26380Sstevel@tonic-gate * nodetype is not specified. 26390Sstevel@tonic-gate */ 26400Sstevel@tonic-gate if (nodetype == NULL) 26410Sstevel@tonic-gate nodetype = DDI_PSEUDO; 26420Sstevel@tonic-gate 26430Sstevel@tonic-gate if ((cmnp = calloc(1, sizeof (cache_minor_t))) == NULL) { 26440Sstevel@tonic-gate SET_DB_ERR(hdp); 26450Sstevel@tonic-gate return (NULL); 26460Sstevel@tonic-gate } 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate cmnp->name = strdup(name); 26490Sstevel@tonic-gate cmnp->nodetype = strdup(nodetype); 26500Sstevel@tonic-gate if (cmnp->name == NULL || cmnp->nodetype == NULL) { 26510Sstevel@tonic-gate SET_DB_ERR(hdp); 26520Sstevel@tonic-gate free(cmnp->name); 26530Sstevel@tonic-gate free(cmnp->nodetype); 26540Sstevel@tonic-gate free(cmnp); 26550Sstevel@tonic-gate return (NULL); 26560Sstevel@tonic-gate } 26570Sstevel@tonic-gate 26580Sstevel@tonic-gate cmnp->node = pcnp; 26590Sstevel@tonic-gate 26600Sstevel@tonic-gate /* Add to node's minor list */ 26610Sstevel@tonic-gate if (prev == NULL) { 26620Sstevel@tonic-gate cmnp->sib = pcnp->minor; 26630Sstevel@tonic-gate pcnp->minor = cmnp; 26640Sstevel@tonic-gate } else { 26650Sstevel@tonic-gate assert(*prev == NULL); 26660Sstevel@tonic-gate *prev = cmnp; 26670Sstevel@tonic-gate } 26680Sstevel@tonic-gate 26690Sstevel@tonic-gate return (cmnp); 26700Sstevel@tonic-gate } 26710Sstevel@tonic-gate 26720Sstevel@tonic-gate static cache_link_t * 26730Sstevel@tonic-gate link_insert( 26740Sstevel@tonic-gate struct di_devlink_handle *hdp, 26750Sstevel@tonic-gate cache_minor_t *cmnp, 26760Sstevel@tonic-gate const char *path, 26770Sstevel@tonic-gate const char *content, 26780Sstevel@tonic-gate uint32_t attr) 26790Sstevel@tonic-gate { 26800Sstevel@tonic-gate cache_link_t *clp; 26810Sstevel@tonic-gate 26820Sstevel@tonic-gate if (path == NULL || content == NULL || !check_attr(attr)) { 26830Sstevel@tonic-gate errno = EINVAL; 26840Sstevel@tonic-gate SET_DB_ERR(hdp); 26850Sstevel@tonic-gate return (NULL); 26860Sstevel@tonic-gate } 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate if ((clp = calloc(1, sizeof (cache_link_t))) == NULL) { 26890Sstevel@tonic-gate SET_DB_ERR(hdp); 26900Sstevel@tonic-gate return (NULL); 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate clp->path = strdup(path); 26940Sstevel@tonic-gate clp->content = strdup(content); 26950Sstevel@tonic-gate if (clp->path == NULL || clp->content == NULL) { 26960Sstevel@tonic-gate SET_DB_ERR(hdp); 26970Sstevel@tonic-gate link_free(&clp); 26980Sstevel@tonic-gate return (NULL); 26990Sstevel@tonic-gate } 27000Sstevel@tonic-gate 27010Sstevel@tonic-gate clp->attr = attr; 27020Sstevel@tonic-gate hash_insert(hdp, clp); 27030Sstevel@tonic-gate clp->minor = cmnp; 27040Sstevel@tonic-gate 27050Sstevel@tonic-gate /* Add to minor's link list */ 27060Sstevel@tonic-gate if (cmnp != NULL) { 27070Sstevel@tonic-gate clp->sib = cmnp->link; 27080Sstevel@tonic-gate cmnp->link = clp; 27090Sstevel@tonic-gate } else { 27100Sstevel@tonic-gate clp->sib = CACHE(hdp)->dngl; 27110Sstevel@tonic-gate CACHE(hdp)->dngl = clp; 27120Sstevel@tonic-gate } 27130Sstevel@tonic-gate 27140Sstevel@tonic-gate return (clp); 27150Sstevel@tonic-gate } 27160Sstevel@tonic-gate 27170Sstevel@tonic-gate static void 27180Sstevel@tonic-gate hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp) 27190Sstevel@tonic-gate { 27200Sstevel@tonic-gate uint_t hval; 27210Sstevel@tonic-gate 27220Sstevel@tonic-gate hval = hashfn(hdp, clp->path); 27230Sstevel@tonic-gate clp->hash = CACHE_HASH(hdp, hval); 27240Sstevel@tonic-gate CACHE_HASH(hdp, hval) = clp; 27250Sstevel@tonic-gate } 27260Sstevel@tonic-gate 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate static struct db_node * 27290Sstevel@tonic-gate get_node(struct di_devlink_handle *hdp, uint32_t idx) 27300Sstevel@tonic-gate { 27310Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ, DB_NODE)); 27320Sstevel@tonic-gate } 27330Sstevel@tonic-gate 27340Sstevel@tonic-gate static struct db_node * 27350Sstevel@tonic-gate set_node(struct di_devlink_handle *hdp, uint32_t idx) 27360Sstevel@tonic-gate { 27370Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_NODE)); 27380Sstevel@tonic-gate } 27390Sstevel@tonic-gate 27400Sstevel@tonic-gate static struct db_minor * 27410Sstevel@tonic-gate get_minor(struct di_devlink_handle *hdp, uint32_t idx) 27420Sstevel@tonic-gate { 27430Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ, DB_MINOR)); 27440Sstevel@tonic-gate } 27450Sstevel@tonic-gate 27460Sstevel@tonic-gate static struct db_minor * 27470Sstevel@tonic-gate set_minor(struct di_devlink_handle *hdp, uint32_t idx) 27480Sstevel@tonic-gate { 27490Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_MINOR)); 27500Sstevel@tonic-gate } 27510Sstevel@tonic-gate 27520Sstevel@tonic-gate static struct db_link * 27530Sstevel@tonic-gate get_link(struct di_devlink_handle *hdp, uint32_t idx) 27540Sstevel@tonic-gate { 27550Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ, DB_LINK)); 27560Sstevel@tonic-gate } 27570Sstevel@tonic-gate 27580Sstevel@tonic-gate static struct db_link * 27590Sstevel@tonic-gate set_link(struct di_devlink_handle *hdp, uint32_t idx) 27600Sstevel@tonic-gate { 27610Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_LINK)); 27620Sstevel@tonic-gate } 27630Sstevel@tonic-gate 27640Sstevel@tonic-gate static char * 27650Sstevel@tonic-gate get_string(struct di_devlink_handle *hdp, uint32_t idx) 27660Sstevel@tonic-gate { 27670Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ, DB_STR)); 27680Sstevel@tonic-gate } 27690Sstevel@tonic-gate 27700Sstevel@tonic-gate static char * 27710Sstevel@tonic-gate set_string(struct di_devlink_handle *hdp, uint32_t idx) 27720Sstevel@tonic-gate { 27730Sstevel@tonic-gate return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_STR)); 27740Sstevel@tonic-gate } 27750Sstevel@tonic-gate 27760Sstevel@tonic-gate 27770Sstevel@tonic-gate /* 27780Sstevel@tonic-gate * Returns the element corresponding to idx. If the portion of file involved 27790Sstevel@tonic-gate * is not yet mapped, does an mmap() as well. Existing mappings are not changed. 27800Sstevel@tonic-gate */ 27810Sstevel@tonic-gate static void * 27820Sstevel@tonic-gate map_seg( 27830Sstevel@tonic-gate struct di_devlink_handle *hdp, 27840Sstevel@tonic-gate uint32_t idx, 27850Sstevel@tonic-gate int prot, 27860Sstevel@tonic-gate db_seg_t seg) 27870Sstevel@tonic-gate { 27880Sstevel@tonic-gate int s; 27890Sstevel@tonic-gate off_t off; 27900Sstevel@tonic-gate size_t slen; 27910Sstevel@tonic-gate caddr_t addr; 27920Sstevel@tonic-gate 27930Sstevel@tonic-gate if (idx == DB_NIL) { 27940Sstevel@tonic-gate return (NULL); 27950Sstevel@tonic-gate } 27960Sstevel@tonic-gate 27970Sstevel@tonic-gate if (!VALID_INDEX(hdp, seg, idx)) { 27980Sstevel@tonic-gate (void) dprintf(DBG_ERR, "map_seg: seg(%d): invalid idx(%u)\n", 27990Sstevel@tonic-gate seg, idx); 28000Sstevel@tonic-gate return (NULL); 28010Sstevel@tonic-gate } 28020Sstevel@tonic-gate 28030Sstevel@tonic-gate /* 28040Sstevel@tonic-gate * If the seg is already mapped in, use it if the access type is 28050Sstevel@tonic-gate * valid. 28060Sstevel@tonic-gate */ 28070Sstevel@tonic-gate if (DB_SEG(hdp, seg) != NULL) { 28080Sstevel@tonic-gate if (DB_SEG_PROT(hdp, seg) != prot) { 28090Sstevel@tonic-gate (void) dprintf(DBG_ERR, "map_seg: illegal access: " 28100Sstevel@tonic-gate "seg[%d]: idx=%u, seg_prot=%d, access=%d\n", 28110Sstevel@tonic-gate seg, idx, DB_SEG_PROT(hdp, seg), prot); 28120Sstevel@tonic-gate return (NULL); 28130Sstevel@tonic-gate } 28140Sstevel@tonic-gate return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]); 28150Sstevel@tonic-gate } 28160Sstevel@tonic-gate 28170Sstevel@tonic-gate /* 28180Sstevel@tonic-gate * Segment is not mapped. Mmap() the segment. 28190Sstevel@tonic-gate */ 28200Sstevel@tonic-gate off = seg_size(hdp, DB_HEADER); 28210Sstevel@tonic-gate for (s = 0; s < seg; s++) { 28220Sstevel@tonic-gate off += seg_size(hdp, s); 28230Sstevel@tonic-gate } 28240Sstevel@tonic-gate slen = seg_size(hdp, seg); 28250Sstevel@tonic-gate 28260Sstevel@tonic-gate addr = mmap(0, slen, prot, MAP_SHARED, DB(hdp)->db_fd, off); 28270Sstevel@tonic-gate if (addr == MAP_FAILED) { 28280Sstevel@tonic-gate (void) dprintf(DBG_ERR, "map_seg: seg[%d]: mmap failed: %s\n", 28290Sstevel@tonic-gate seg, strerror(errno)); 28300Sstevel@tonic-gate (void) dprintf(DBG_ERR, "map_seg: args: len=%lu, prot=%d," 28310Sstevel@tonic-gate " fd=%d, off=%ld\n", (ulong_t)slen, prot, DB(hdp)->db_fd, 28320Sstevel@tonic-gate off); 28330Sstevel@tonic-gate return (NULL); 28340Sstevel@tonic-gate } 28350Sstevel@tonic-gate 28360Sstevel@tonic-gate DB_SEG(hdp, seg) = addr; 28370Sstevel@tonic-gate DB_SEG_PROT(hdp, seg) = prot; 28380Sstevel@tonic-gate 28390Sstevel@tonic-gate (void) dprintf(DBG_STEP, "map_seg: seg[%d]: len=%lu, prot=%d, fd=%d, " 28400Sstevel@tonic-gate "off=%ld, seg_base=%p\n", seg, (ulong_t)slen, prot, DB(hdp)->db_fd, 28410Sstevel@tonic-gate off, (void *)addr); 28420Sstevel@tonic-gate 28430Sstevel@tonic-gate return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]); 28440Sstevel@tonic-gate } 28450Sstevel@tonic-gate 28460Sstevel@tonic-gate /* 28470Sstevel@tonic-gate * Computes the size of a segment rounded up to the nearest page boundary. 28480Sstevel@tonic-gate */ 28490Sstevel@tonic-gate static size_t 28500Sstevel@tonic-gate seg_size(struct di_devlink_handle *hdp, int seg) 28510Sstevel@tonic-gate { 28520Sstevel@tonic-gate size_t sz; 28530Sstevel@tonic-gate 28540Sstevel@tonic-gate assert(DB_HDR(hdp)->page_sz); 28550Sstevel@tonic-gate 28560Sstevel@tonic-gate if (seg == DB_HEADER) { 28570Sstevel@tonic-gate sz = HDR_LEN; 28580Sstevel@tonic-gate } else { 28590Sstevel@tonic-gate assert(DB_NUM(hdp, seg) >= 1); 28600Sstevel@tonic-gate sz = DB_NUM(hdp, seg) * elem_sizes[seg]; 28610Sstevel@tonic-gate } 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate sz = (sz / DB_HDR(hdp)->page_sz) + 1; 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate sz *= DB_HDR(hdp)->page_sz; 28660Sstevel@tonic-gate 28670Sstevel@tonic-gate return (sz); 28680Sstevel@tonic-gate } 28690Sstevel@tonic-gate 28700Sstevel@tonic-gate static size_t 28710Sstevel@tonic-gate size_db(struct di_devlink_handle *hdp, long page_sz, uint32_t *count) 28720Sstevel@tonic-gate { 28730Sstevel@tonic-gate int i; 28740Sstevel@tonic-gate size_t sz; 28750Sstevel@tonic-gate cache_link_t *clp; 28760Sstevel@tonic-gate 28770Sstevel@tonic-gate assert(page_sz > 0); 28780Sstevel@tonic-gate 28790Sstevel@tonic-gate /* Take "NIL" element into account */ 28800Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 28810Sstevel@tonic-gate count[i] = 1; 28820Sstevel@tonic-gate } 28830Sstevel@tonic-gate 28840Sstevel@tonic-gate count_node(CACHE(hdp)->root, count); 28850Sstevel@tonic-gate 28860Sstevel@tonic-gate for (clp = CACHE(hdp)->dngl; clp != NULL; clp = clp->sib) { 28870Sstevel@tonic-gate count_link(clp, count); 28880Sstevel@tonic-gate } 28890Sstevel@tonic-gate 28900Sstevel@tonic-gate sz = ((HDR_LEN / page_sz) + 1) * page_sz; 28910Sstevel@tonic-gate for (i = 0; i < DB_TYPES; i++) { 28920Sstevel@tonic-gate assert(count[i] >= 1); 28930Sstevel@tonic-gate sz += (((count[i] * elem_sizes[i]) / page_sz) + 1) * page_sz; 28940Sstevel@tonic-gate (void) dprintf(DBG_INFO, "N[%u]=%u\n", i, count[i]); 28950Sstevel@tonic-gate } 28960Sstevel@tonic-gate (void) dprintf(DBG_INFO, "DB size=%lu\n", (ulong_t)sz); 28970Sstevel@tonic-gate 28980Sstevel@tonic-gate return (sz); 28990Sstevel@tonic-gate } 29000Sstevel@tonic-gate 29010Sstevel@tonic-gate 29020Sstevel@tonic-gate static void 29030Sstevel@tonic-gate count_node(cache_node_t *cnp, uint32_t *count) 29040Sstevel@tonic-gate { 29050Sstevel@tonic-gate cache_minor_t *cmnp; 29060Sstevel@tonic-gate 29070Sstevel@tonic-gate if (cnp == NULL) 29080Sstevel@tonic-gate return; 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate count[DB_NODE]++; 29110Sstevel@tonic-gate count_string(cnp->path, count); 29120Sstevel@tonic-gate 29130Sstevel@tonic-gate for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) { 29140Sstevel@tonic-gate count_minor(cmnp, count); 29150Sstevel@tonic-gate } 29160Sstevel@tonic-gate 29170Sstevel@tonic-gate for (cnp = cnp->child; cnp != NULL; cnp = cnp->sib) { 29180Sstevel@tonic-gate count_node(cnp, count); 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate 29210Sstevel@tonic-gate } 29220Sstevel@tonic-gate 29230Sstevel@tonic-gate static void 29240Sstevel@tonic-gate count_minor(cache_minor_t *cmnp, uint32_t *count) 29250Sstevel@tonic-gate { 29260Sstevel@tonic-gate cache_link_t *clp; 29270Sstevel@tonic-gate 29280Sstevel@tonic-gate if (cmnp == NULL) 29290Sstevel@tonic-gate return; 29300Sstevel@tonic-gate 29310Sstevel@tonic-gate count[DB_MINOR]++; 29320Sstevel@tonic-gate count_string(cmnp->name, count); 29330Sstevel@tonic-gate count_string(cmnp->nodetype, count); 29340Sstevel@tonic-gate 29350Sstevel@tonic-gate for (clp = cmnp->link; clp != NULL; clp = clp->sib) { 29360Sstevel@tonic-gate count_link(clp, count); 29370Sstevel@tonic-gate } 29380Sstevel@tonic-gate } 29390Sstevel@tonic-gate 29400Sstevel@tonic-gate static void 29410Sstevel@tonic-gate count_link(cache_link_t *clp, uint32_t *count) 29420Sstevel@tonic-gate { 29430Sstevel@tonic-gate if (clp == NULL) 29440Sstevel@tonic-gate return; 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate count[DB_LINK]++; 29470Sstevel@tonic-gate count_string(clp->path, count); 29480Sstevel@tonic-gate count_string(clp->content, count); 29490Sstevel@tonic-gate } 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate 29520Sstevel@tonic-gate static void 29530Sstevel@tonic-gate count_string(const char *str, uint32_t *count) 29540Sstevel@tonic-gate { 29550Sstevel@tonic-gate if (str == NULL) { 29560Sstevel@tonic-gate (void) dprintf(DBG_ERR, "count_string: NULL argument\n"); 29570Sstevel@tonic-gate return; 29580Sstevel@tonic-gate } 29590Sstevel@tonic-gate 29600Sstevel@tonic-gate count[DB_STR] += strlen(str) + 1; 29610Sstevel@tonic-gate } 29620Sstevel@tonic-gate 29630Sstevel@tonic-gate static uint_t 29640Sstevel@tonic-gate hashfn(struct di_devlink_handle *hdp, const char *str) 29650Sstevel@tonic-gate { 29660Sstevel@tonic-gate const char *cp; 29670Sstevel@tonic-gate ulong_t hval = 0; 29680Sstevel@tonic-gate 29690Sstevel@tonic-gate if (str == NULL) { 29700Sstevel@tonic-gate return (0); 29710Sstevel@tonic-gate } 29720Sstevel@tonic-gate 29730Sstevel@tonic-gate assert(CACHE(hdp)->hash_sz >= MIN_HASH_SIZE); 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate for (cp = str; *cp != '\0'; cp++) { 29760Sstevel@tonic-gate hval += *cp; 29770Sstevel@tonic-gate } 29780Sstevel@tonic-gate 29790Sstevel@tonic-gate return (hval % CACHE(hdp)->hash_sz); 29800Sstevel@tonic-gate } 29810Sstevel@tonic-gate 29820Sstevel@tonic-gate static int 29830Sstevel@tonic-gate enter_update_lock(struct di_devlink_handle *hdp) 29840Sstevel@tonic-gate { 29850Sstevel@tonic-gate int i, fd, rv; 29860Sstevel@tonic-gate struct flock lock; 29870Sstevel@tonic-gate char lockfile[PATH_MAX]; 29880Sstevel@tonic-gate 29890Sstevel@tonic-gate assert(hdp->lock_fd < 0); 29900Sstevel@tonic-gate 29910Sstevel@tonic-gate get_db_path(hdp, DB_LOCK, lockfile, sizeof (lockfile)); 29920Sstevel@tonic-gate 29930Sstevel@tonic-gate /* 29940Sstevel@tonic-gate * Record locks are per-process. Protect against multiple threads. 29950Sstevel@tonic-gate */ 29960Sstevel@tonic-gate (void) mutex_lock(&update_mutex); 29970Sstevel@tonic-gate 29980Sstevel@tonic-gate if ((fd = open(lockfile, O_RDWR|O_CREAT, DB_LOCK_PERMS)) < 0) { 29990Sstevel@tonic-gate goto error; 30000Sstevel@tonic-gate } 30010Sstevel@tonic-gate 30020Sstevel@tonic-gate lock.l_type = F_WRLCK; 30030Sstevel@tonic-gate lock.l_whence = SEEK_SET; 30040Sstevel@tonic-gate lock.l_start = 0; 30050Sstevel@tonic-gate lock.l_len = 0; 30060Sstevel@tonic-gate 30070Sstevel@tonic-gate i = 1; 30080Sstevel@tonic-gate while ((rv = fcntl(fd, F_SETLKW, &lock)) == -1 && errno == EINTR) { 30090Sstevel@tonic-gate if (i < MAX_LOCK_RETRY) { 30100Sstevel@tonic-gate i++; 30110Sstevel@tonic-gate } else { 30120Sstevel@tonic-gate break; 30130Sstevel@tonic-gate } 30140Sstevel@tonic-gate } 30150Sstevel@tonic-gate 30160Sstevel@tonic-gate if (rv == 0) { 30170Sstevel@tonic-gate hdp->lock_fd = fd; 30180Sstevel@tonic-gate return (0); 30190Sstevel@tonic-gate } else { 30200Sstevel@tonic-gate (void) close(fd); 30210Sstevel@tonic-gate } 30220Sstevel@tonic-gate 30230Sstevel@tonic-gate error: 30240Sstevel@tonic-gate (void) mutex_unlock(&update_mutex); 30250Sstevel@tonic-gate 30260Sstevel@tonic-gate dprintf(DBG_ERR, "lockfile(%s): lock failed: %s\n", lockfile, 30270Sstevel@tonic-gate strerror(errno)); 30280Sstevel@tonic-gate return (-1); 30290Sstevel@tonic-gate } 30300Sstevel@tonic-gate 30310Sstevel@tonic-gate /* 30320Sstevel@tonic-gate * Close and re-open lock file every time so that it is recreated if deleted. 30330Sstevel@tonic-gate */ 30340Sstevel@tonic-gate static void 30350Sstevel@tonic-gate exit_update_lock(struct di_devlink_handle *hdp) 30360Sstevel@tonic-gate { 30370Sstevel@tonic-gate struct flock unlock; 30380Sstevel@tonic-gate 30390Sstevel@tonic-gate if (hdp->lock_fd < 0) { 30400Sstevel@tonic-gate return; 30410Sstevel@tonic-gate } 30420Sstevel@tonic-gate 30430Sstevel@tonic-gate unlock.l_type = F_UNLCK; 30440Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 30450Sstevel@tonic-gate unlock.l_start = 0; 30460Sstevel@tonic-gate unlock.l_len = 0; 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate if (fcntl(hdp->lock_fd, F_SETLK, &unlock) == -1) { 30490Sstevel@tonic-gate dprintf(DBG_ERR, "update lockfile: unlock failed: %s\n", 30500Sstevel@tonic-gate strerror(errno)); 30510Sstevel@tonic-gate } 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate (void) close(hdp->lock_fd); 30540Sstevel@tonic-gate 30550Sstevel@tonic-gate hdp->lock_fd = -1; 30560Sstevel@tonic-gate 30570Sstevel@tonic-gate (void) mutex_unlock(&update_mutex); 30580Sstevel@tonic-gate } 30590Sstevel@tonic-gate 30600Sstevel@tonic-gate /* 30610Sstevel@tonic-gate * returns 1 if contents is a minor node in /devices. 30620Sstevel@tonic-gate * If mn_root is not NULL, mn_root is set to: 30630Sstevel@tonic-gate * if contents is a /dev node, mn_root = contents 30642621Sllai1 * OR 30650Sstevel@tonic-gate * if contents is a /devices node, mn_root set to the '/' 30660Sstevel@tonic-gate * following /devices. 30670Sstevel@tonic-gate */ 30680Sstevel@tonic-gate int 30690Sstevel@tonic-gate is_minor_node(const char *contents, const char **mn_root) 30700Sstevel@tonic-gate { 30710Sstevel@tonic-gate char *ptr, *prefix; 30720Sstevel@tonic-gate 30730Sstevel@tonic-gate prefix = "../devices/"; 30740Sstevel@tonic-gate 30750Sstevel@tonic-gate if ((ptr = strstr(contents, prefix)) != NULL) { 30760Sstevel@tonic-gate 30770Sstevel@tonic-gate /* mn_root should point to the / following /devices */ 30780Sstevel@tonic-gate if (mn_root != NULL) { 30790Sstevel@tonic-gate *mn_root = ptr += strlen(prefix) - 1; 30800Sstevel@tonic-gate } 30810Sstevel@tonic-gate return (1); 30820Sstevel@tonic-gate } 30830Sstevel@tonic-gate 30840Sstevel@tonic-gate prefix = "/devices/"; 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate if (strncmp(contents, prefix, strlen(prefix)) == 0) { 30870Sstevel@tonic-gate 30880Sstevel@tonic-gate /* mn_root should point to the / following /devices/ */ 30890Sstevel@tonic-gate if (mn_root != NULL) { 30900Sstevel@tonic-gate *mn_root = contents + strlen(prefix) - 1; 30910Sstevel@tonic-gate } 30920Sstevel@tonic-gate return (1); 30930Sstevel@tonic-gate } 30940Sstevel@tonic-gate 30950Sstevel@tonic-gate if (mn_root != NULL) { 30960Sstevel@tonic-gate *mn_root = contents; 30970Sstevel@tonic-gate } 30980Sstevel@tonic-gate return (0); 30990Sstevel@tonic-gate } 31000Sstevel@tonic-gate 31010Sstevel@tonic-gate static int 31020Sstevel@tonic-gate s_readlink(const char *link, char *buf, size_t blen) 31030Sstevel@tonic-gate { 31040Sstevel@tonic-gate int rv; 31050Sstevel@tonic-gate 31060Sstevel@tonic-gate if ((rv = readlink(link, buf, blen)) == -1) 31070Sstevel@tonic-gate goto bad; 31080Sstevel@tonic-gate 31090Sstevel@tonic-gate if (rv >= blen && buf[blen - 1] != '\0') { 31100Sstevel@tonic-gate errno = ENAMETOOLONG; 31110Sstevel@tonic-gate goto bad; 31120Sstevel@tonic-gate } else if (rv < blen) { 31130Sstevel@tonic-gate buf[rv] = '\0'; 31140Sstevel@tonic-gate } 31150Sstevel@tonic-gate 31160Sstevel@tonic-gate return (0); 31170Sstevel@tonic-gate bad: 31180Sstevel@tonic-gate dprintf(DBG_ERR, "s_readlink: %s: failed: %s\n", 31190Sstevel@tonic-gate link, strerror(errno)); 31200Sstevel@tonic-gate return (-1); 31210Sstevel@tonic-gate } 31220Sstevel@tonic-gate 31230Sstevel@tonic-gate /* 31240Sstevel@tonic-gate * Synchronous link creation interface routines 31250Sstevel@tonic-gate * The scope of the operation is determined by the "name" arg. 31260Sstevel@tonic-gate * "name" can be NULL, a driver name or a devfs pathname (without /devices) 31270Sstevel@tonic-gate * 31280Sstevel@tonic-gate * "name" creates 31290Sstevel@tonic-gate * ====== ======= 31300Sstevel@tonic-gate * 31310Sstevel@tonic-gate * NULL => All devlinks in system 31320Sstevel@tonic-gate * <driver> => devlinks for named driver 31330Sstevel@tonic-gate * /pci@1 => devlinks for subtree rooted at pci@1 31340Sstevel@tonic-gate * /pseudo/foo@0:X => devlinks for minor X 31350Sstevel@tonic-gate * 31360Sstevel@tonic-gate * devlink_create() returns 0 on success or an errno value on failure 31370Sstevel@tonic-gate */ 31380Sstevel@tonic-gate 31390Sstevel@tonic-gate #define MAX_DAEMON_ATTEMPTS 2 31400Sstevel@tonic-gate 31410Sstevel@tonic-gate static int 31420Sstevel@tonic-gate devlink_create(const char *root, const char *name) 31430Sstevel@tonic-gate { 31440Sstevel@tonic-gate int i; 31450Sstevel@tonic-gate struct dca_off dca; 31460Sstevel@tonic-gate 31470Sstevel@tonic-gate assert(root); 31480Sstevel@tonic-gate 31490Sstevel@tonic-gate /* 31500Sstevel@tonic-gate * Convert name into arg for door_call 31510Sstevel@tonic-gate */ 31520Sstevel@tonic-gate if (dca_init(name, &dca) != 0) 31530Sstevel@tonic-gate return (EINVAL); 31540Sstevel@tonic-gate 31550Sstevel@tonic-gate /* 31560Sstevel@tonic-gate * Attempt to use the daemon first 31570Sstevel@tonic-gate */ 31580Sstevel@tonic-gate i = 0; 31590Sstevel@tonic-gate do { 31600Sstevel@tonic-gate daemon_call(root, &dca); 31610Sstevel@tonic-gate 31620Sstevel@tonic-gate dprintf(DBG_INFO, "daemon_call() retval=%d\n", dca.dca_error); 31630Sstevel@tonic-gate 31640Sstevel@tonic-gate /* 31650Sstevel@tonic-gate * Retry only if door server isn't running 31660Sstevel@tonic-gate */ 31670Sstevel@tonic-gate if (dca.dca_error != ENOENT && dca.dca_error != EBADF) { 31680Sstevel@tonic-gate return (dca.dca_error); 31690Sstevel@tonic-gate } 31700Sstevel@tonic-gate 31710Sstevel@tonic-gate dca.dca_error = 0; 31720Sstevel@tonic-gate 31730Sstevel@tonic-gate /* 31740Sstevel@tonic-gate * To improve performance defer this check until the first 31750Sstevel@tonic-gate * failure. Safe to defer as door server checks perms. 31760Sstevel@tonic-gate */ 31770Sstevel@tonic-gate if (geteuid() != 0) 31780Sstevel@tonic-gate return (EPERM); 31790Sstevel@tonic-gate /* 31800Sstevel@tonic-gate * Daemon may not be running. Try to start it. 31810Sstevel@tonic-gate */ 31820Sstevel@tonic-gate } while ((++i < MAX_DAEMON_ATTEMPTS) && start_daemon(root) == 0); 31830Sstevel@tonic-gate 31840Sstevel@tonic-gate dprintf(DBG_INFO, "devlink_create: can't start daemon\n"); 31850Sstevel@tonic-gate 31860Sstevel@tonic-gate assert(dca.dca_error == 0); 31870Sstevel@tonic-gate 31880Sstevel@tonic-gate /* 31890Sstevel@tonic-gate * If the daemon cannot be started execute the devfsadm command. 31900Sstevel@tonic-gate */ 31910Sstevel@tonic-gate exec_cmd(root, &dca); 31920Sstevel@tonic-gate 31930Sstevel@tonic-gate return (dca.dca_error); 31940Sstevel@tonic-gate } 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate /* 31970Sstevel@tonic-gate * The "name" member of "struct dca" contains data in the following order 31980Sstevel@tonic-gate * root'\0'minor'\0'driver'\0' 31990Sstevel@tonic-gate * The root component is always present at offset 0 in the "name" field. 32000Sstevel@tonic-gate * The driver and minor are optional. If present they have a non-zero 32010Sstevel@tonic-gate * offset in the "name" member. 32020Sstevel@tonic-gate */ 32030Sstevel@tonic-gate static int 32040Sstevel@tonic-gate dca_init(const char *name, struct dca_off *dcp) 32050Sstevel@tonic-gate { 32060Sstevel@tonic-gate char *cp; 32070Sstevel@tonic-gate 32080Sstevel@tonic-gate dcp->dca_root = 0; 32090Sstevel@tonic-gate dcp->dca_minor = 0; 32100Sstevel@tonic-gate dcp->dca_driver = 0; 32110Sstevel@tonic-gate dcp->dca_error = 0; 32120Sstevel@tonic-gate dcp->dca_flags = 0; 32130Sstevel@tonic-gate dcp->dca_name[0] = '\0'; 32140Sstevel@tonic-gate 32150Sstevel@tonic-gate name = name ? name : "/"; 32160Sstevel@tonic-gate 32170Sstevel@tonic-gate /* 32180Sstevel@tonic-gate * Check if name is a driver name 32190Sstevel@tonic-gate */ 32200Sstevel@tonic-gate if (*name != '/') { 32210Sstevel@tonic-gate (void) snprintf(dcp->dca_name, sizeof (dcp->dca_name), 32220Sstevel@tonic-gate "/ %s", name); 32230Sstevel@tonic-gate dcp->dca_root = 0; 32240Sstevel@tonic-gate *(dcp->dca_name + 1) = '\0'; 32250Sstevel@tonic-gate dcp->dca_driver = 2; 32260Sstevel@tonic-gate return (0); 32270Sstevel@tonic-gate } 32280Sstevel@tonic-gate 32290Sstevel@tonic-gate (void) snprintf(dcp->dca_name, sizeof (dcp->dca_name), "%s", name); 32300Sstevel@tonic-gate 32310Sstevel@tonic-gate /* 32320Sstevel@tonic-gate * "/devices" not allowed in devfs pathname 32330Sstevel@tonic-gate */ 32340Sstevel@tonic-gate if (is_minor_node(name, NULL)) 32350Sstevel@tonic-gate return (-1); 32360Sstevel@tonic-gate 32370Sstevel@tonic-gate dcp->dca_root = 0; 32380Sstevel@tonic-gate if (cp = strrchr(dcp->dca_name, ':')) { 32390Sstevel@tonic-gate *cp++ = '\0'; 32400Sstevel@tonic-gate dcp->dca_minor = cp - dcp->dca_name; 32410Sstevel@tonic-gate } 32420Sstevel@tonic-gate 32430Sstevel@tonic-gate return (0); 32440Sstevel@tonic-gate } 32450Sstevel@tonic-gate 32460Sstevel@tonic-gate 32470Sstevel@tonic-gate #define DAEMON_STARTUP_TIME 1 /* 1 second. This may need to be adjusted */ 32480Sstevel@tonic-gate 32490Sstevel@tonic-gate static void 32500Sstevel@tonic-gate daemon_call(const char *root, struct dca_off *dcp) 32510Sstevel@tonic-gate { 32520Sstevel@tonic-gate door_arg_t arg; 32530Sstevel@tonic-gate int fd, door_error; 32540Sstevel@tonic-gate sigset_t oset, nset; 32550Sstevel@tonic-gate char synch_door[PATH_MAX]; 32562621Sllai1 struct statvfs svf; 32572621Sllai1 char *prefix; 32582621Sllai1 32592621Sllai1 /* 32602621Sllai1 * If readonly root, assume we are in install 32612621Sllai1 */ 32622621Sllai1 prefix = 32632621Sllai1 (statvfs("/etc/dev", &svf) == 0 && (svf.f_flag & ST_RDONLY)) ? 32642621Sllai1 "/tmp" : (char *)root; 32650Sstevel@tonic-gate (void) snprintf(synch_door, sizeof (synch_door), 32662621Sllai1 "%s/etc/dev/%s", prefix, DEVFSADM_SYNCH_DOOR); 32670Sstevel@tonic-gate 32680Sstevel@tonic-gate if ((fd = open(synch_door, O_RDONLY)) == -1) { 32690Sstevel@tonic-gate dcp->dca_error = errno; 32700Sstevel@tonic-gate dprintf(DBG_ERR, "open of %s failed: %s\n", 32710Sstevel@tonic-gate synch_door, strerror(errno)); 32720Sstevel@tonic-gate return; 32730Sstevel@tonic-gate } 32740Sstevel@tonic-gate 32750Sstevel@tonic-gate arg.data_ptr = (char *)dcp; 32760Sstevel@tonic-gate arg.data_size = sizeof (*dcp); 32770Sstevel@tonic-gate arg.desc_ptr = NULL; 32780Sstevel@tonic-gate arg.desc_num = 0; 32790Sstevel@tonic-gate arg.rbuf = (char *)dcp; 32800Sstevel@tonic-gate arg.rsize = sizeof (*dcp); 32810Sstevel@tonic-gate 32820Sstevel@tonic-gate /* 32830Sstevel@tonic-gate * Block signals to this thread until door call 32840Sstevel@tonic-gate * completes. 32850Sstevel@tonic-gate */ 32860Sstevel@tonic-gate (void) sigfillset(&nset); 32870Sstevel@tonic-gate (void) sigemptyset(&oset); 32880Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &nset, &oset); 32890Sstevel@tonic-gate if (door_call(fd, &arg)) { 32900Sstevel@tonic-gate door_error = 1; 32910Sstevel@tonic-gate dcp->dca_error = errno; 32920Sstevel@tonic-gate } 32930Sstevel@tonic-gate (void) sigprocmask(SIG_SETMASK, &oset, NULL); 32940Sstevel@tonic-gate 32950Sstevel@tonic-gate (void) close(fd); 32960Sstevel@tonic-gate 32970Sstevel@tonic-gate if (door_error) 32980Sstevel@tonic-gate return; 32990Sstevel@tonic-gate 33000Sstevel@tonic-gate assert(arg.data_ptr); 33010Sstevel@tonic-gate 33020Sstevel@tonic-gate /*LINTED*/ 33030Sstevel@tonic-gate dcp->dca_error = ((struct dca_off *)arg.data_ptr)->dca_error; 33040Sstevel@tonic-gate 33050Sstevel@tonic-gate /* 33060Sstevel@tonic-gate * The doors interface may return data in a different buffer 33070Sstevel@tonic-gate * If that happens, deallocate buffer via munmap() 33080Sstevel@tonic-gate */ 33090Sstevel@tonic-gate if (arg.rbuf != (char *)dcp) 33100Sstevel@tonic-gate (void) munmap(arg.rbuf, arg.rsize); 33110Sstevel@tonic-gate } 33120Sstevel@tonic-gate 33130Sstevel@tonic-gate #define DEVFSADM_PATH "/usr/sbin/devfsadm" 33140Sstevel@tonic-gate #define DEVFSADM "devfsadm" 33150Sstevel@tonic-gate 33160Sstevel@tonic-gate #define DEVFSADMD_PATH "/usr/lib/devfsadm/devfsadmd" 33170Sstevel@tonic-gate #define DEVFSADM_DAEMON "devfsadmd" 33180Sstevel@tonic-gate 33190Sstevel@tonic-gate static int 33200Sstevel@tonic-gate start_daemon(const char *root) 33210Sstevel@tonic-gate { 33220Sstevel@tonic-gate int rv, i = 0; 33230Sstevel@tonic-gate char *argv[20]; 33240Sstevel@tonic-gate 33250Sstevel@tonic-gate argv[i++] = DEVFSADM_DAEMON; 33260Sstevel@tonic-gate if (strcmp(root, "/")) { 33270Sstevel@tonic-gate argv[i++] = "-r"; 33280Sstevel@tonic-gate argv[i++] = (char *)root; 33290Sstevel@tonic-gate } 33300Sstevel@tonic-gate argv[i++] = NULL; 33310Sstevel@tonic-gate 33320Sstevel@tonic-gate rv = do_exec(DEVFSADMD_PATH, argv); 33330Sstevel@tonic-gate 33340Sstevel@tonic-gate (void) sleep(DAEMON_STARTUP_TIME); 33350Sstevel@tonic-gate 33360Sstevel@tonic-gate return (rv); 33370Sstevel@tonic-gate } 33380Sstevel@tonic-gate 33390Sstevel@tonic-gate static void 33400Sstevel@tonic-gate exec_cmd(const char *root, struct dca_off *dcp) 33410Sstevel@tonic-gate { 33420Sstevel@tonic-gate int i; 33430Sstevel@tonic-gate char *argv[20]; 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate i = 0; 33460Sstevel@tonic-gate argv[i++] = DEVFSADM; 33470Sstevel@tonic-gate 33480Sstevel@tonic-gate /* 33490Sstevel@tonic-gate * Load drivers only if -i is specified 33500Sstevel@tonic-gate */ 33510Sstevel@tonic-gate if (dcp->dca_driver) { 33520Sstevel@tonic-gate argv[i++] = "-i"; 33530Sstevel@tonic-gate argv[i++] = &dcp->dca_name[dcp->dca_driver]; 33540Sstevel@tonic-gate } else { 33550Sstevel@tonic-gate argv[i++] = "-n"; 33560Sstevel@tonic-gate } 33570Sstevel@tonic-gate 33580Sstevel@tonic-gate if (root != NULL && strcmp(root, "/") != 0) { 33590Sstevel@tonic-gate argv[i++] = "-r"; 33600Sstevel@tonic-gate argv[i++] = (char *)root; 33610Sstevel@tonic-gate } 33620Sstevel@tonic-gate 33630Sstevel@tonic-gate argv[i] = NULL; 33640Sstevel@tonic-gate 33650Sstevel@tonic-gate if (do_exec(DEVFSADM_PATH, argv)) 33660Sstevel@tonic-gate dcp->dca_error = errno; 33670Sstevel@tonic-gate } 33680Sstevel@tonic-gate 33690Sstevel@tonic-gate static int 33700Sstevel@tonic-gate do_exec(const char *path, char *const argv[]) 33710Sstevel@tonic-gate { 33720Sstevel@tonic-gate int i; 33730Sstevel@tonic-gate pid_t cpid; 33740Sstevel@tonic-gate 33750Sstevel@tonic-gate #ifdef DEBUG 33760Sstevel@tonic-gate dprintf(DBG_INFO, "Executing %s\n\tArgument list:", path); 33770Sstevel@tonic-gate for (i = 0; argv[i] != NULL; i++) { 33780Sstevel@tonic-gate dprintf(DBG_INFO, " %s", argv[i]); 33790Sstevel@tonic-gate } 33800Sstevel@tonic-gate dprintf(DBG_INFO, "\n"); 33810Sstevel@tonic-gate #endif 33820Sstevel@tonic-gate 33830Sstevel@tonic-gate if ((cpid = fork1()) == -1) { 33840Sstevel@tonic-gate dprintf(DBG_ERR, "fork1 failed: %s\n", strerror(errno)); 33850Sstevel@tonic-gate return (-1); 33860Sstevel@tonic-gate } 33870Sstevel@tonic-gate 33880Sstevel@tonic-gate if (cpid == 0) { /* child process */ 33890Sstevel@tonic-gate int fd; 33900Sstevel@tonic-gate 33910Sstevel@tonic-gate if ((fd = open("/dev/null", O_RDWR)) >= 0) { 33920Sstevel@tonic-gate (void) dup2(fd, fileno(stdout)); 33930Sstevel@tonic-gate (void) dup2(fd, fileno(stderr)); 33940Sstevel@tonic-gate (void) close(fd); 33950Sstevel@tonic-gate 33960Sstevel@tonic-gate (void) execv(path, argv); 33970Sstevel@tonic-gate } else { 33980Sstevel@tonic-gate dprintf(DBG_ERR, "open of /dev/null failed: %s\n", 33990Sstevel@tonic-gate strerror(errno)); 34000Sstevel@tonic-gate } 34010Sstevel@tonic-gate 34020Sstevel@tonic-gate _exit(-1); 34030Sstevel@tonic-gate } 34040Sstevel@tonic-gate 34050Sstevel@tonic-gate /* Parent process */ 34060Sstevel@tonic-gate if (waitpid(cpid, &i, 0) == cpid) { 34070Sstevel@tonic-gate if (WIFEXITED(i)) { 34080Sstevel@tonic-gate if (WEXITSTATUS(i) == 0) { 34090Sstevel@tonic-gate dprintf(DBG_STEP, 34100Sstevel@tonic-gate "do_exec: child exited normally\n"); 34110Sstevel@tonic-gate return (0); 34120Sstevel@tonic-gate } else 34130Sstevel@tonic-gate errno = EINVAL; 34140Sstevel@tonic-gate } else { 34150Sstevel@tonic-gate /* 34160Sstevel@tonic-gate * The child was interrupted by a signal 34170Sstevel@tonic-gate */ 34180Sstevel@tonic-gate errno = EINTR; 34190Sstevel@tonic-gate } 34200Sstevel@tonic-gate dprintf(DBG_ERR, "child terminated abnormally: %s\n", 34210Sstevel@tonic-gate strerror(errno)); 34220Sstevel@tonic-gate } else { 34230Sstevel@tonic-gate dprintf(DBG_ERR, "waitpid failed: %s\n", strerror(errno)); 34240Sstevel@tonic-gate } 34250Sstevel@tonic-gate 34260Sstevel@tonic-gate return (-1); 34270Sstevel@tonic-gate } 34280Sstevel@tonic-gate 34290Sstevel@tonic-gate static int 34300Sstevel@tonic-gate walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp, link_desc_t *linkp) 34310Sstevel@tonic-gate { 34320Sstevel@tonic-gate int i; 34330Sstevel@tonic-gate 34340Sstevel@tonic-gate assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp)); 34350Sstevel@tonic-gate 34360Sstevel@tonic-gate dprintf(DBG_INFO, "walk_cache_links: initial link: %s\n", 34370Sstevel@tonic-gate clp ? clp->path : "<NULL>"); 34380Sstevel@tonic-gate 34390Sstevel@tonic-gate /* 34400Sstevel@tonic-gate * First search the links under the specified minor. On the 34410Sstevel@tonic-gate * 2nd pass, search the dangling list - secondary links may 34420Sstevel@tonic-gate * exist on this list since they are not resolved during the 34430Sstevel@tonic-gate * /dev walk. 34440Sstevel@tonic-gate */ 34450Sstevel@tonic-gate for (i = 0; i < 2; i++) { 34460Sstevel@tonic-gate for (; clp != NULL; clp = clp->sib) { 34470Sstevel@tonic-gate struct di_devlink vlink = {NULL}; 34480Sstevel@tonic-gate 34490Sstevel@tonic-gate assert(clp->path[0] != '/'); 34500Sstevel@tonic-gate 34510Sstevel@tonic-gate vlink.rel_path = clp->path; 34520Sstevel@tonic-gate vlink.content = clp->content; 34530Sstevel@tonic-gate vlink.type = attr2type(clp->attr); 34540Sstevel@tonic-gate 34550Sstevel@tonic-gate if (visit_link(hdp, linkp, &vlink) 34560Sstevel@tonic-gate != DI_WALK_CONTINUE) { 34570Sstevel@tonic-gate dprintf(DBG_INFO, "walk_cache_links: " 34580Sstevel@tonic-gate "terminating at link: %s\n", clp->path); 34590Sstevel@tonic-gate goto out; 34600Sstevel@tonic-gate } 34610Sstevel@tonic-gate } 34620Sstevel@tonic-gate 34630Sstevel@tonic-gate clp = CACHE(hdp)->dngl; 34640Sstevel@tonic-gate } 34650Sstevel@tonic-gate 34660Sstevel@tonic-gate out: 34670Sstevel@tonic-gate 34680Sstevel@tonic-gate /* If i < 2, we terminated the walk prematurely */ 34690Sstevel@tonic-gate return (i < 2 ? DI_WALK_TERMINATE : DI_WALK_CONTINUE); 34700Sstevel@tonic-gate } 34710Sstevel@tonic-gate 34720Sstevel@tonic-gate static void 34730Sstevel@tonic-gate walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp) 34740Sstevel@tonic-gate { 34750Sstevel@tonic-gate int i; 34760Sstevel@tonic-gate cache_link_t *clp; 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate dprintf(DBG_INFO, "walk_all_cache: entered\n"); 34790Sstevel@tonic-gate 34800Sstevel@tonic-gate for (i = 0; i < CACHE(hdp)->hash_sz; i++) { 34810Sstevel@tonic-gate clp = CACHE_HASH(hdp, i); 34820Sstevel@tonic-gate for (; clp; clp = clp->hash) { 34830Sstevel@tonic-gate struct di_devlink vlink = {NULL}; 34840Sstevel@tonic-gate 34850Sstevel@tonic-gate assert(clp->path[0] != '/'); 34860Sstevel@tonic-gate 34870Sstevel@tonic-gate vlink.rel_path = clp->path; 34880Sstevel@tonic-gate vlink.content = clp->content; 34890Sstevel@tonic-gate vlink.type = attr2type(clp->attr); 34900Sstevel@tonic-gate if (visit_link(hdp, linkp, &vlink) != 34910Sstevel@tonic-gate DI_WALK_CONTINUE) { 34920Sstevel@tonic-gate dprintf(DBG_INFO, "walk_all_cache: terminating " 34930Sstevel@tonic-gate "walk at link: %s\n", clp->path); 34940Sstevel@tonic-gate return; 34950Sstevel@tonic-gate } 34960Sstevel@tonic-gate } 34970Sstevel@tonic-gate } 34980Sstevel@tonic-gate } 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate static void 35010Sstevel@tonic-gate walk_cache_minor(di_devlink_handle_t hdp, const char *mpath, link_desc_t *linkp) 35020Sstevel@tonic-gate { 35030Sstevel@tonic-gate cache_minor_t *cmnp; 35040Sstevel@tonic-gate 35050Sstevel@tonic-gate assert(mpath); 35060Sstevel@tonic-gate 35070Sstevel@tonic-gate if ((cmnp = lookup_minor(hdp, mpath, NULL, TYPE_CACHE)) != NULL) { 35080Sstevel@tonic-gate (void) walk_cache_links(hdp, cmnp->link, linkp); 35090Sstevel@tonic-gate } else { 35100Sstevel@tonic-gate dprintf(DBG_ERR, "lookup minor failed: %s\n", mpath); 35110Sstevel@tonic-gate } 35120Sstevel@tonic-gate } 35130Sstevel@tonic-gate 35140Sstevel@tonic-gate static void 35150Sstevel@tonic-gate walk_cache_node(di_devlink_handle_t hdp, const char *path, link_desc_t *linkp) 35160Sstevel@tonic-gate { 35170Sstevel@tonic-gate cache_minor_t *cmnp; 35180Sstevel@tonic-gate cache_node_t *cnp; 35190Sstevel@tonic-gate 35200Sstevel@tonic-gate assert(path); 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate if ((cnp = lookup_node(hdp, (char *)path, TYPE_CACHE)) == NULL) { 35230Sstevel@tonic-gate dprintf(DBG_ERR, "lookup node failed: %s\n", path); 35240Sstevel@tonic-gate return; 35250Sstevel@tonic-gate } 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) { 35280Sstevel@tonic-gate if (walk_cache_links(hdp, cmnp->link, linkp) 35290Sstevel@tonic-gate == DI_WALK_TERMINATE) 35300Sstevel@tonic-gate break; 35310Sstevel@tonic-gate } 35320Sstevel@tonic-gate } 35330Sstevel@tonic-gate 35340Sstevel@tonic-gate /* 35350Sstevel@tonic-gate * Private function 35360Sstevel@tonic-gate * 35370Sstevel@tonic-gate * Walk cached links corresponding to the given path. 35380Sstevel@tonic-gate * 35390Sstevel@tonic-gate * path path to a node or minor node. 35400Sstevel@tonic-gate * 35410Sstevel@tonic-gate * flags specifies the type of devlinks to be selected. 35420Sstevel@tonic-gate * If DI_PRIMARY_LINK is used, only primary links are selected. 35430Sstevel@tonic-gate * If DI_SECONDARY_LINK is specified, only secondary links 35440Sstevel@tonic-gate * are selected. 35450Sstevel@tonic-gate * If neither flag is specified, all devlinks are selected. 35460Sstevel@tonic-gate * 35470Sstevel@tonic-gate * re An extended regular expression in regex(5) format which 35480Sstevel@tonic-gate * selects the /dev links to be returned. The regular 35490Sstevel@tonic-gate * expression should use link pathnames relative to 35500Sstevel@tonic-gate * /dev. i.e. without the leading "/dev/" prefix. 35510Sstevel@tonic-gate * A NULL value matches all devlinks. 35520Sstevel@tonic-gate */ 35530Sstevel@tonic-gate int 35540Sstevel@tonic-gate di_devlink_cache_walk(di_devlink_handle_t hdp, 35550Sstevel@tonic-gate const char *re, 35560Sstevel@tonic-gate const char *path, 35570Sstevel@tonic-gate uint_t flags, 35580Sstevel@tonic-gate void *arg, 35590Sstevel@tonic-gate int (*devlink_callback)(di_devlink_t, void *)) 35600Sstevel@tonic-gate { 35610Sstevel@tonic-gate regex_t reg; 35620Sstevel@tonic-gate link_desc_t linkd = {NULL}; 35630Sstevel@tonic-gate 35640Sstevel@tonic-gate if (hdp == NULL || path == NULL || !link_flag(flags) || 35650Sstevel@tonic-gate !HDL_RDWR(hdp) || devlink_callback == NULL) { 35660Sstevel@tonic-gate errno = EINVAL; 35670Sstevel@tonic-gate return (-1); 35680Sstevel@tonic-gate } 35690Sstevel@tonic-gate 35700Sstevel@tonic-gate linkd.flags = flags; 35710Sstevel@tonic-gate linkd.arg = arg; 35720Sstevel@tonic-gate linkd.fcn = devlink_callback; 35730Sstevel@tonic-gate 35740Sstevel@tonic-gate if (re) { 35750Sstevel@tonic-gate if (regcomp(®, re, REG_EXTENDED) != 0) 35760Sstevel@tonic-gate return (-1); 35770Sstevel@tonic-gate linkd.regp = ® 35780Sstevel@tonic-gate } 35790Sstevel@tonic-gate 35800Sstevel@tonic-gate if (minor_colon(path) == NULL) { 35810Sstevel@tonic-gate walk_cache_node(hdp, path, &linkd); 35820Sstevel@tonic-gate } else { 35830Sstevel@tonic-gate walk_cache_minor(hdp, path, &linkd); 35840Sstevel@tonic-gate } 35850Sstevel@tonic-gate 35860Sstevel@tonic-gate if (re) 35870Sstevel@tonic-gate regfree(®); 35880Sstevel@tonic-gate 35890Sstevel@tonic-gate return (0); 35900Sstevel@tonic-gate } 35910Sstevel@tonic-gate 35920Sstevel@tonic-gate #define DEBUG_ENV_VAR "_DEVLINK_DEBUG" 35930Sstevel@tonic-gate static int _devlink_debug = -1; 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate /* 35960Sstevel@tonic-gate * debug level is initialized to -1. 35970Sstevel@tonic-gate * On first call into this routine, debug level is set. 35980Sstevel@tonic-gate * If debug level is zero, debugging msgs are disabled. 35990Sstevel@tonic-gate */ 36000Sstevel@tonic-gate static void 36010Sstevel@tonic-gate debug_print(debug_level_t msglevel, const char *fmt, va_list ap) 36020Sstevel@tonic-gate { 36030Sstevel@tonic-gate char *cp; 36040Sstevel@tonic-gate int save; 36050Sstevel@tonic-gate 36060Sstevel@tonic-gate /* 36070Sstevel@tonic-gate * We shouldn't be here if debug is disabled 36080Sstevel@tonic-gate */ 36090Sstevel@tonic-gate assert(_devlink_debug != 0); 36100Sstevel@tonic-gate 36110Sstevel@tonic-gate /* 36120Sstevel@tonic-gate * Set debug level on first call into this routine 36130Sstevel@tonic-gate */ 36140Sstevel@tonic-gate if (_devlink_debug < 0) { 36150Sstevel@tonic-gate if ((cp = getenv(DEBUG_ENV_VAR)) == NULL) { 36160Sstevel@tonic-gate _devlink_debug = 0; 36170Sstevel@tonic-gate return; 36180Sstevel@tonic-gate } 36190Sstevel@tonic-gate 36200Sstevel@tonic-gate save = errno; 36210Sstevel@tonic-gate errno = 0; 36220Sstevel@tonic-gate _devlink_debug = strtol(cp, NULL, 10); 36230Sstevel@tonic-gate if (errno != 0 || _devlink_debug < 0) { 36240Sstevel@tonic-gate _devlink_debug = 0; 36250Sstevel@tonic-gate errno = save; 36260Sstevel@tonic-gate return; 36270Sstevel@tonic-gate } 36280Sstevel@tonic-gate errno = save; 36290Sstevel@tonic-gate 36300Sstevel@tonic-gate if (!_devlink_debug) 36310Sstevel@tonic-gate return; 36320Sstevel@tonic-gate } 36330Sstevel@tonic-gate 36340Sstevel@tonic-gate /* debug msgs are enabled */ 36350Sstevel@tonic-gate assert(_devlink_debug > 0); 36360Sstevel@tonic-gate 36370Sstevel@tonic-gate if (_devlink_debug < msglevel) 36380Sstevel@tonic-gate return; 36390Sstevel@tonic-gate 36400Sstevel@tonic-gate 36410Sstevel@tonic-gate /* Print a distinctive label for error msgs */ 36420Sstevel@tonic-gate if (msglevel == DBG_ERR) { 36430Sstevel@tonic-gate (void) fprintf(stderr, "[ERROR]: "); 36440Sstevel@tonic-gate } 36450Sstevel@tonic-gate 36460Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 36470Sstevel@tonic-gate } 36480Sstevel@tonic-gate 36490Sstevel@tonic-gate /* ARGSUSED */ 36500Sstevel@tonic-gate /* PRINTFLIKE2 */ 36510Sstevel@tonic-gate void 36520Sstevel@tonic-gate dprintf(debug_level_t msglevel, const char *fmt, ...) 36530Sstevel@tonic-gate { 36540Sstevel@tonic-gate va_list ap; 36550Sstevel@tonic-gate 36560Sstevel@tonic-gate assert(msglevel > 0); 36570Sstevel@tonic-gate 36580Sstevel@tonic-gate if (!_devlink_debug) 36590Sstevel@tonic-gate return; 36600Sstevel@tonic-gate 36610Sstevel@tonic-gate va_start(ap, fmt); 36620Sstevel@tonic-gate debug_print(msglevel, fmt, ap); 36630Sstevel@tonic-gate va_end(ap); 36640Sstevel@tonic-gate } 3665