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
5*2621Sllai1  * Common Development and Distribution License (the "License").
6*2621Sllai1  * 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 /*
22*2621Sllai1  * 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 
28*2621Sllai1 #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) {
208*2621Sllai1 		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 
249*2621Sllai1 	/*
250*2621Sllai1 	 * Avoid triggering /dev reconfigure for read when not present
251*2621Sllai1 	 */
252*2621Sllai1 	if (IS_RDONLY(flags) &&
253*2621Sllai1 	    (strncmp(path, "/dev/", 5) == 0) && !device_exists(path)) {
254*2621Sllai1 		return (-1);
255*2621Sllai1 	}
256*2621Sllai1 
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 {
309*2621Sllai1 	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';
315*2621Sllai1 	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
331*2621Sllai1 		if ((realpath(root_dir, dev_dir) == NULL) ||
332*2621Sllai1 		    (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) {
338*2621Sllai1 		dev_dir[0] = 0;
339*2621Sllai1 		db_dir[0] = 0;
3400Sstevel@tonic-gate 	} else {
341*2621Sllai1 		(void) strlcpy(db_dir, dev_dir, sizeof (db_dir));
3420Sstevel@tonic-gate 	}
3430Sstevel@tonic-gate 
344*2621Sllai1 	(void) strlcat(dev_dir, DEV, sizeof (dev_dir));
345*2621Sllai1 	(void) strlcat(db_dir, ETCDEV, sizeof (db_dir));
346*2621Sllai1 
3470Sstevel@tonic-gate 	proto.dev_dir = dev_dir;
348*2621Sllai1 	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 
384*2621Sllai1 	if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) {
385*2621Sllai1 		free(hdp->dev_dir);
386*2621Sllai1 		free(hdp);
387*2621Sllai1 		goto error;
388*2621Sllai1 	}
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);
10350Sstevel@tonic-gate 	free(hdp);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate  * Frees the tree rooted at a node. Siblings of the subtree root
10400Sstevel@tonic-gate  * have to be handled by the caller.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate static void
10430Sstevel@tonic-gate subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	cache_node_t *np;
10460Sstevel@tonic-gate 	cache_link_t *clp;
10470Sstevel@tonic-gate 	cache_minor_t *cmnp;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	if (pp == NULL || *pp == NULL)
10500Sstevel@tonic-gate 		return;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	while ((*pp)->child != NULL) {
10530Sstevel@tonic-gate 		np = (*pp)->child;
10540Sstevel@tonic-gate 		(*pp)->child = np->sib;
10550Sstevel@tonic-gate 		subtree_free(hdp, &np);
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	while ((*pp)->minor != NULL) {
10590Sstevel@tonic-gate 		cmnp = (*pp)->minor;
10600Sstevel@tonic-gate 		(*pp)->minor = cmnp->sib;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 		while (cmnp->link != NULL) {
10630Sstevel@tonic-gate 			clp = cmnp->link;
10640Sstevel@tonic-gate 			cmnp->link = clp->sib;
10650Sstevel@tonic-gate 			rm_link_from_hash(hdp, clp);
10660Sstevel@tonic-gate 			link_free(&clp);
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 		minor_free(hdp, &cmnp);
10690Sstevel@tonic-gate 	}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	node_free(pp);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate static void
10750Sstevel@tonic-gate rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	int hval;
10780Sstevel@tonic-gate 	cache_link_t **pp;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (clp == NULL)
10810Sstevel@tonic-gate 		return;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (clp->path == NULL)
10840Sstevel@tonic-gate 		return;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	hval = hashfn(hdp, clp->path);
10870Sstevel@tonic-gate 	pp = &(CACHE_HASH(hdp, hval));
10880Sstevel@tonic-gate 	for (; *pp != NULL; pp = &(*pp)->hash) {
10890Sstevel@tonic-gate 		if (*pp == clp) {
10900Sstevel@tonic-gate 			*pp = clp->hash;
10910Sstevel@tonic-gate 			clp->hash = NULL;
10920Sstevel@tonic-gate 			return;
10930Sstevel@tonic-gate 		}
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	dprintf(DBG_ERR, "rm_link_from_hash: link(%s) not found\n", clp->path);
10970Sstevel@tonic-gate }
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate static cache_link_t *
11000Sstevel@tonic-gate link_hash(di_devlink_handle_t hdp, const char *link, uint_t flags)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	int hval;
11030Sstevel@tonic-gate 	cache_link_t **pp, *clp;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	if (link == NULL)
11060Sstevel@tonic-gate 		return (NULL);
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	hval = hashfn(hdp, link);
11090Sstevel@tonic-gate 	pp = &(CACHE_HASH(hdp, hval));
11100Sstevel@tonic-gate 	for (; (clp = *pp) != NULL; pp = &clp->hash) {
11110Sstevel@tonic-gate 		if (strcmp(clp->path, link) == 0) {
11120Sstevel@tonic-gate 			break;
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (clp == NULL)
11170Sstevel@tonic-gate 		return (NULL);
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	if ((flags & UNLINK_FROM_HASH) == UNLINK_FROM_HASH) {
11200Sstevel@tonic-gate 		*pp = clp->hash;
11210Sstevel@tonic-gate 		clp->hash = NULL;
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	return (clp);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate static cache_minor_t *
11280Sstevel@tonic-gate link2minor(struct di_devlink_handle *hdp, cache_link_t *clp)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 	cache_link_t *plp;
11310Sstevel@tonic-gate 	const char *minor_path;
11320Sstevel@tonic-gate 	char *cp, buf[PATH_MAX], link[PATH_MAX];
1133*2621Sllai1 	char abspath[PATH_MAX];
1134*2621Sllai1 	struct stat st;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	if (TYPE_PRI(attr2type(clp->attr))) {
11370Sstevel@tonic-gate 		/*
11380Sstevel@tonic-gate 		 * For primary link, content should point to a /devices node.
11390Sstevel@tonic-gate 		 */
11400Sstevel@tonic-gate 		if (!is_minor_node(clp->content, &minor_path)) {
11410Sstevel@tonic-gate 			return (NULL);
11420Sstevel@tonic-gate 		}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 		return (lookup_minor(hdp, minor_path, NULL,
11450Sstevel@tonic-gate 		    TYPE_CACHE|CREATE_FLAG));
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	/*
11500Sstevel@tonic-gate 	 * If secondary, the primary link is derived from the secondary
11510Sstevel@tonic-gate 	 * link contents. Secondary link contents can have two formats:
1152*2621Sllai1 	 *	audio -> /dev/sound/0
11530Sstevel@tonic-gate 	 *	fb0 -> fbs/afb0
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	buf[0] = '\0';
11570Sstevel@tonic-gate 	if (strncmp(clp->content, DEV"/", strlen(DEV"/")) == 0) {
11580Sstevel@tonic-gate 		cp = &clp->content[strlen(DEV"/")];
11590Sstevel@tonic-gate 	} else if (clp->content[0] != '/') {
11600Sstevel@tonic-gate 		if ((cp = strrchr(clp->path, '/')) != NULL) {
11610Sstevel@tonic-gate 			char savechar = *(cp + 1);
11620Sstevel@tonic-gate 			*(cp + 1) = '\0';
11630Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", clp->path);
11640Sstevel@tonic-gate 			*(cp + 1) = savechar;
11650Sstevel@tonic-gate 		}
11660Sstevel@tonic-gate 		(void) strlcat(buf, clp->content, sizeof (buf));
11670Sstevel@tonic-gate 		cp = buf;
11680Sstevel@tonic-gate 	} else {
11690Sstevel@tonic-gate 		goto follow_link;
11700Sstevel@tonic-gate 	}
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 	/*
11730Sstevel@tonic-gate 	 * Lookup the primary link if possible and find its minor.
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) {
11760Sstevel@tonic-gate 		return (plp->minor);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/* realpath() used only as a last resort because it is expensive */
11800Sstevel@tonic-gate follow_link:
11810Sstevel@tonic-gate 	(void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate #ifdef	DEBUG
11840Sstevel@tonic-gate 	/*LINTED*/
11850Sstevel@tonic-gate 	assert(sizeof (buf) >= PATH_MAX);
11860Sstevel@tonic-gate #endif
1187*2621Sllai1 
1188*2621Sllai1 	/*
1189*2621Sllai1 	 * A realpath attempt to lookup a dangling link can invoke implicit
1190*2621Sllai1 	 * reconfig so verify there's an actual device behind the link first.
1191*2621Sllai1 	 */
1192*2621Sllai1 	if (lstat(link, &st) == -1)
1193*2621Sllai1 		return (NULL);
1194*2621Sllai1 	if (S_ISLNK(st.st_mode)) {
1195*2621Sllai1 		if (s_readlink(link, buf, sizeof (buf)) < 0)
1196*2621Sllai1 			return (NULL);
1197*2621Sllai1 		if (buf[0] != '/') {
1198*2621Sllai1 			char *p;
1199*2621Sllai1 			size_t n = sizeof (abspath);
1200*2621Sllai1 			if (strlcpy(abspath, link, n) >= n)
1201*2621Sllai1 				return (NULL);
1202*2621Sllai1 			p = strrchr(abspath, '/') + 1;
1203*2621Sllai1 			*p = 0;
1204*2621Sllai1 			n = sizeof (abspath) - strlen(p);
1205*2621Sllai1 			if (strlcpy(p, buf, n) >= n)
1206*2621Sllai1 				return (NULL);
1207*2621Sllai1 		} else {
1208*2621Sllai1 			if (strlcpy(abspath, buf, sizeof (abspath)) >=
1209*2621Sllai1 			    sizeof (abspath))
1210*2621Sllai1 				return (NULL);
1211*2621Sllai1 		}
1212*2621Sllai1 		if (!device_exists(abspath))
1213*2621Sllai1 			return (NULL);
1214*2621Sllai1 	}
1215*2621Sllai1 
12160Sstevel@tonic-gate 	if (realpath(link, buf) == NULL || !is_minor_node(buf, &minor_path)) {
12170Sstevel@tonic-gate 		return (NULL);
12180Sstevel@tonic-gate 	}
12190Sstevel@tonic-gate 	return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG));
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate static void
12240Sstevel@tonic-gate resolve_dangling_links(struct di_devlink_handle *hdp)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	cache_minor_t *cmnp;
12270Sstevel@tonic-gate 	cache_link_t *clp, **pp;
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) {
12300Sstevel@tonic-gate 		clp = *pp;
12310Sstevel@tonic-gate 		if ((cmnp = link2minor(hdp, clp)) != NULL) {
12320Sstevel@tonic-gate 			*pp = clp->sib;
12330Sstevel@tonic-gate 			clp->sib = cmnp->link;
12340Sstevel@tonic-gate 			cmnp->link = clp;
12350Sstevel@tonic-gate 			assert(clp->minor == NULL);
12360Sstevel@tonic-gate 			clp->minor = cmnp;
12370Sstevel@tonic-gate 		} else {
12380Sstevel@tonic-gate 			dprintf(DBG_INFO, "resolve_dangling_links: link(%s):"
12390Sstevel@tonic-gate 			    " unresolved\n", clp->path);
12400Sstevel@tonic-gate 			pp = &clp->sib;
12410Sstevel@tonic-gate 		}
12420Sstevel@tonic-gate 	}
12430Sstevel@tonic-gate }
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate /*
12470Sstevel@tonic-gate  * The elements are assumed to be detached from the cache tree.
12480Sstevel@tonic-gate  */
12490Sstevel@tonic-gate static void
12500Sstevel@tonic-gate node_free(cache_node_t **pp)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	cache_node_t *cnp = *pp;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	*pp = NULL;
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	if (cnp == NULL)
12570Sstevel@tonic-gate 		return;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	free(cnp->path);
12600Sstevel@tonic-gate 	free(cnp);
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate static void
12640Sstevel@tonic-gate minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp)
12650Sstevel@tonic-gate {
12660Sstevel@tonic-gate 	cache_minor_t *cmnp = *pp;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	*pp = NULL;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (cmnp == NULL)
12710Sstevel@tonic-gate 		return;
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (CACHE_LAST(hdp) == cmnp) {
12740Sstevel@tonic-gate 		dprintf(DBG_STEP, "minor_free: last_minor(%s)\n", cmnp->name);
12750Sstevel@tonic-gate 		CACHE_LAST(hdp) = NULL;
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	free(cmnp->name);
12790Sstevel@tonic-gate 	free(cmnp->nodetype);
12800Sstevel@tonic-gate 	free(cmnp);
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate static void
12840Sstevel@tonic-gate link_free(cache_link_t **pp)
12850Sstevel@tonic-gate {
12860Sstevel@tonic-gate 	cache_link_t *clp = *pp;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	*pp = NULL;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	if (clp == NULL)
12910Sstevel@tonic-gate 		return;
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	free(clp->path);
12940Sstevel@tonic-gate 	free(clp->content);
12950Sstevel@tonic-gate 	free(clp);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * Returns the ':' preceding the minor name
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate static char *
13020Sstevel@tonic-gate minor_colon(const char *path)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	char *cp;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	if ((cp = strrchr(path, '/')) == NULL) {
13070Sstevel@tonic-gate 		return (NULL);
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	return (strchr(cp, ':'));
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate static void *
13140Sstevel@tonic-gate lookup_minor(
13150Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
13160Sstevel@tonic-gate 	const char *minor_path,
13170Sstevel@tonic-gate 	const char *nodetype,
13180Sstevel@tonic-gate 	const int flags)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	void *vp;
13210Sstevel@tonic-gate 	char *colon;
13220Sstevel@tonic-gate 	char pdup[PATH_MAX];
13230Sstevel@tonic-gate 	const char *fcn = "lookup_minor";
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (minor_path == NULL) {
13260Sstevel@tonic-gate 		errno = EINVAL;
13270Sstevel@tonic-gate 		return (NULL);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	(void) snprintf(pdup, sizeof (pdup), "%s", minor_path);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	if ((colon = minor_colon(pdup)) == NULL) {
13330Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: invalid minor path(%s)\n", fcn,
13340Sstevel@tonic-gate 		    minor_path);
13350Sstevel@tonic-gate 		errno = EINVAL;
13360Sstevel@tonic-gate 		return (NULL);
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 	*colon = '\0';
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) {
13410Sstevel@tonic-gate 		return (vp);
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 	if ((vp = lookup_node(hdp, pdup, flags)) == NULL) {
13450Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: node(%s) not found\n", fcn, pdup);
13460Sstevel@tonic-gate 		return (NULL);
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	*colon = ':';
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if (LOOKUP_CACHE(flags)) {
13510Sstevel@tonic-gate 		cache_minor_t **pp;
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 		pp = &((cache_node_t *)vp)->minor;
13540Sstevel@tonic-gate 		for (; *pp != NULL; pp = &(*pp)->sib) {
13550Sstevel@tonic-gate 			if (strcmp((*pp)->name, colon + 1) == 0)
13560Sstevel@tonic-gate 				break;
13570Sstevel@tonic-gate 		}
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		if (*pp == NULL && CREATE_ELEM(flags)) {
13600Sstevel@tonic-gate 			*pp = minor_insert(hdp, vp, colon + 1, nodetype, pp);
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 		set_last_minor(hdp, *pp, flags);
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		return (*pp);
13650Sstevel@tonic-gate 	} else {
13660Sstevel@tonic-gate 		char *cp;
13670Sstevel@tonic-gate 		uint32_t nidx;
13680Sstevel@tonic-gate 		struct db_minor *dmp;
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 		nidx = (((struct db_node *)vp)->minor);
13710Sstevel@tonic-gate 		for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
13720Sstevel@tonic-gate 			cp = get_string(hdp, dmp->name);
13730Sstevel@tonic-gate 			if (cp && strcmp(cp, colon + 1) == 0)
13740Sstevel@tonic-gate 				break;
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 		return (dmp);
13770Sstevel@tonic-gate 	}
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate static void *
13810Sstevel@tonic-gate lookup_node(struct di_devlink_handle *hdp, char *path, const int flags)
13820Sstevel@tonic-gate {
13830Sstevel@tonic-gate 	struct tnode tnd = {NULL};
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	if (tnd.node = get_last_node(hdp, path, flags))
13860Sstevel@tonic-gate 		return (tnd.node);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	tnd.handle = hdp;
13890Sstevel@tonic-gate 	tnd.flags = flags;
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (walk_tree(path, &tnd, visit_node) != 0)
13920Sstevel@tonic-gate 		return (NULL);
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	return (tnd.node);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate /*
13980Sstevel@tonic-gate  * last_minor is used for nodes of TYPE_CACHE only.
13990Sstevel@tonic-gate  */
14000Sstevel@tonic-gate static void *
14010Sstevel@tonic-gate get_last_node(struct di_devlink_handle *hdp, const char *path, int flags)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate 	cache_node_t *cnp;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate #ifdef	DEBUG
14060Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14070Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "get_last_node: SKIPPING \"last\" "
14080Sstevel@tonic-gate 		    "node cache\n");
14090Sstevel@tonic-gate 		return (NULL);
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate #endif
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL ||
14140Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node == NULL) {
14150Sstevel@tonic-gate 		return (NULL);
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	cnp = CACHE_LAST(hdp)->node;
14190Sstevel@tonic-gate 	if (strcmp(cnp->path, path) == 0) {
14200Sstevel@tonic-gate 		return (cnp);
14210Sstevel@tonic-gate 	}
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	cnp = cnp->sib;
14240Sstevel@tonic-gate 	if (cnp && strcmp(cnp->path, path) == 0) {
14250Sstevel@tonic-gate 		return (cnp);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	return (NULL);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate static void *
14320Sstevel@tonic-gate get_last_minor(
14330Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
14340Sstevel@tonic-gate 	const char *devfs_path,
14350Sstevel@tonic-gate 	const char *minor_name,
14360Sstevel@tonic-gate 	int flags)
14370Sstevel@tonic-gate {
14380Sstevel@tonic-gate 	cache_minor_t *cmnp;
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate #ifdef	DEBUG
14410Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14420Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "get_last_minor: SKIPPING \"last\" "
14430Sstevel@tonic-gate 		    "minor cache\n");
14440Sstevel@tonic-gate 		return (NULL);
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate #endif
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) {
14490Sstevel@tonic-gate 		return (NULL);
14500Sstevel@tonic-gate 	}
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	cmnp = CACHE_LAST(hdp);
14530Sstevel@tonic-gate 	if (strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
14540Sstevel@tonic-gate 	    strcmp(cmnp->node->path, devfs_path) == 0) {
14550Sstevel@tonic-gate 		return (cmnp);
14560Sstevel@tonic-gate 	}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 	cmnp = cmnp->sib;
14590Sstevel@tonic-gate 	if (cmnp && strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
14600Sstevel@tonic-gate 	    strcmp(cmnp->node->path, devfs_path) == 0) {
14610Sstevel@tonic-gate 		set_last_minor(hdp, cmnp, TYPE_CACHE);
14620Sstevel@tonic-gate 		return (cmnp);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	return (NULL);
14660Sstevel@tonic-gate }
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate static void
14690Sstevel@tonic-gate set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate #ifdef	DEBUG
14720Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14730Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "set_last_minor: SKIPPING \"last\" "
14740Sstevel@tonic-gate 		    "minor cache\n");
14750Sstevel@tonic-gate 		return;
14760Sstevel@tonic-gate 	}
14770Sstevel@tonic-gate #endif
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if (LOOKUP_CACHE(flags) && cmnp) {
14800Sstevel@tonic-gate 		CACHE_LAST(hdp) = cmnp;
14810Sstevel@tonic-gate 	}
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate /*
14860Sstevel@tonic-gate  * Returns 0 if normal return or -1 otherwise.
14870Sstevel@tonic-gate  */
14880Sstevel@tonic-gate static int
14890Sstevel@tonic-gate walk_tree(
14900Sstevel@tonic-gate 	char *cur,
14910Sstevel@tonic-gate 	void *arg,
14920Sstevel@tonic-gate 	int (*node_callback)(const char *path, void *arg))
14930Sstevel@tonic-gate {
14940Sstevel@tonic-gate 	char *slash, buf[PATH_MAX];
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	if (cur == NULL || cur[0] != '/' || strlen(cur) > sizeof (buf) - 1) {
14970Sstevel@tonic-gate 		errno = EINVAL;
14980Sstevel@tonic-gate 		return (-1);
14990Sstevel@tonic-gate 	}
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	(void) strcpy(buf, "/");
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	for (;;) {
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 		if (node_callback(buf, arg) != DI_WALK_CONTINUE)
15060Sstevel@tonic-gate 			break;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		while (*cur == '/')
15090Sstevel@tonic-gate 			cur++;
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		if (*cur == '\0')
15120Sstevel@tonic-gate 			break;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 		/*
15150Sstevel@tonic-gate 		 * There is a next component(s). Append a "/" separator for all
15160Sstevel@tonic-gate 		 * but the first (root) component.
15170Sstevel@tonic-gate 		 */
15180Sstevel@tonic-gate 		if (buf[1] != '\0') {
15190Sstevel@tonic-gate 			(void) strlcat(buf, "/", sizeof (buf));
15200Sstevel@tonic-gate 		}
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 		if (slash = strchr(cur, '/')) {
15230Sstevel@tonic-gate 			*slash = '\0';
15240Sstevel@tonic-gate 			(void) strlcat(buf, cur, sizeof (buf));
15250Sstevel@tonic-gate 			*slash = '/';
15260Sstevel@tonic-gate 			cur = slash;
15270Sstevel@tonic-gate 		} else {
15280Sstevel@tonic-gate 			(void) strlcat(buf, cur, sizeof (buf));
15290Sstevel@tonic-gate 			cur += strlen(cur);
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate 	}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 	return (0);
15350Sstevel@tonic-gate }
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate static int
15390Sstevel@tonic-gate visit_node(const char *path, void *arg)
15400Sstevel@tonic-gate {
15410Sstevel@tonic-gate 	struct tnode *tnp = arg;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	if (LOOKUP_CACHE(tnp->flags)) {
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		cache_node_t *cnp = tnp->node;
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 		cnp = (cnp) ? cnp->child : CACHE_ROOT(tnp->handle);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 		for (; cnp != NULL; cnp = cnp->sib) {
15500Sstevel@tonic-gate 			if (strcmp(cnp->path, path) == 0)
15510Sstevel@tonic-gate 				break;
15520Sstevel@tonic-gate 		}
15530Sstevel@tonic-gate 		if (cnp == NULL && CREATE_ELEM(tnp->flags)) {
15540Sstevel@tonic-gate 			cnp = node_insert(tnp->handle, tnp->node, path,
15550Sstevel@tonic-gate 			    INSERT_TAIL);
15560Sstevel@tonic-gate 		}
15570Sstevel@tonic-gate 		tnp->node = cnp;
15580Sstevel@tonic-gate 	} else {
15590Sstevel@tonic-gate 		char *cp;
15600Sstevel@tonic-gate 		struct db_node *dnp = tnp->node;
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 		dnp = (dnp) ? get_node(tnp->handle, dnp->child)
15630Sstevel@tonic-gate 		    : get_node(tnp->handle, DB_HDR(tnp->handle)->root_idx);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 		for (; dnp != NULL; dnp = get_node(tnp->handle, dnp->sib)) {
15660Sstevel@tonic-gate 			cp = get_string(tnp->handle, dnp->path);
15670Sstevel@tonic-gate 			if (cp && strcmp(cp, path) == 0) {
15680Sstevel@tonic-gate 				break;
15690Sstevel@tonic-gate 			}
15700Sstevel@tonic-gate 		}
15710Sstevel@tonic-gate 		tnp->node = dnp;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/*
15750Sstevel@tonic-gate 	 * Terminate walk if node is not found for a path component.
15760Sstevel@tonic-gate 	 */
15770Sstevel@tonic-gate 	return (tnp->node ? DI_WALK_CONTINUE : DI_WALK_TERMINATE);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate static void
15810Sstevel@tonic-gate minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate 	cache_link_t **lpp;
15840Sstevel@tonic-gate 	cache_minor_t **mpp;
15850Sstevel@tonic-gate 	const char *fcn = "minor_delete";
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: removing minor: %s\n", fcn, cmnp->name);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	/* detach minor from node */
15900Sstevel@tonic-gate 	if (cmnp->node != NULL) {
15910Sstevel@tonic-gate 		mpp = &cmnp->node->minor;
15920Sstevel@tonic-gate 		for (; *mpp != NULL; mpp = &(*mpp)->sib) {
15930Sstevel@tonic-gate 			if (*mpp == cmnp)
15940Sstevel@tonic-gate 				break;
15950Sstevel@tonic-gate 		}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		if (*mpp == NULL) {
15980Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: dangling minor: %s\n",
15990Sstevel@tonic-gate 			    fcn, cmnp->name);
16000Sstevel@tonic-gate 		} else {
16010Sstevel@tonic-gate 			*mpp = cmnp->sib;
16020Sstevel@tonic-gate 		}
16030Sstevel@tonic-gate 	} else {
16040Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: orphan minor(%s)\n", fcn,
16050Sstevel@tonic-gate 		    cmnp->name);
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	delete_unused_nodes(hdp, cmnp->node);
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	cmnp->node = NULL;
16110Sstevel@tonic-gate 	cmnp->sib = NULL;
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate 	/* Move all remaining links to dangling list */
16140Sstevel@tonic-gate 	for (lpp = &cmnp->link; *lpp != NULL; lpp = &(*lpp)->sib) {
16150Sstevel@tonic-gate 		(*lpp)->minor = NULL;
16160Sstevel@tonic-gate 	}
16170Sstevel@tonic-gate 	*lpp = CACHE(hdp)->dngl;
16180Sstevel@tonic-gate 	CACHE(hdp)->dngl = cmnp->link;
16190Sstevel@tonic-gate 	cmnp->link = NULL;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	minor_free(hdp, &cmnp);
16220Sstevel@tonic-gate }
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate static void
16250Sstevel@tonic-gate delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp)
16260Sstevel@tonic-gate {
16270Sstevel@tonic-gate 	cache_node_t **npp;
16280Sstevel@tonic-gate 	const char *fcn = "delete_unused_nodes";
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (cnp == NULL)
16310Sstevel@tonic-gate 		return;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if (cnp->minor != NULL || cnp->child != NULL)
16340Sstevel@tonic-gate 		return;
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "%s: removing unused node: %s\n", fcn,
16370Sstevel@tonic-gate 	    cnp->path);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	/* Unlink node from tree */
16400Sstevel@tonic-gate 	if (cnp->parent != NULL) {
16410Sstevel@tonic-gate 		npp = &cnp->parent->child;
16420Sstevel@tonic-gate 		for (; *npp != NULL; npp = &(*npp)->sib) {
16430Sstevel@tonic-gate 			if (*npp == cnp)
16440Sstevel@tonic-gate 				break;
16450Sstevel@tonic-gate 		}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 		if (*npp == NULL) {
16480Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: dangling node: %s\n", fcn,
16490Sstevel@tonic-gate 			    cnp->path);
16500Sstevel@tonic-gate 		} else {
16510Sstevel@tonic-gate 			*npp = cnp->sib;
16520Sstevel@tonic-gate 		}
16530Sstevel@tonic-gate 	} else if (cnp == CACHE_ROOT(hdp)) {
16540Sstevel@tonic-gate 		CACHE_ROOT(hdp) = NULL;
16550Sstevel@tonic-gate 	} else {
16560Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: orphan node (%s)\n", fcn,
16570Sstevel@tonic-gate 		    cnp->path);
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	delete_unused_nodes(hdp, cnp->parent);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	cnp->parent = cnp->sib = NULL;
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	node_free(&cnp);
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate static int
16680Sstevel@tonic-gate rm_link(di_devlink_handle_t hdp, const char *link)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	cache_link_t *clp;
16710Sstevel@tonic-gate 	const char *fcn = "rm_link";
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' ||
16740Sstevel@tonic-gate 	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
16750Sstevel@tonic-gate 		dprintf(DBG_ERR, "%s: %s: invalid args\n",
16760Sstevel@tonic-gate 		    fcn, link ? link : "<NULL>");
16770Sstevel@tonic-gate 		errno = EINVAL;
16780Sstevel@tonic-gate 		return (-1);
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	dprintf(DBG_STEP, "%s: link(%s)\n", fcn, link);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) {
16840Sstevel@tonic-gate 		return (0);
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	link_delete(hdp, clp);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	return (0);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate int
16930Sstevel@tonic-gate di_devlink_rm_link(di_devlink_handle_t hdp, const char *link)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDWR(hdp)) {
16960Sstevel@tonic-gate 		errno = EINVAL;
16970Sstevel@tonic-gate 		return (-1);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	return (rm_link(hdp, link));
17010Sstevel@tonic-gate }
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate static void
17040Sstevel@tonic-gate link_delete(di_devlink_handle_t hdp, cache_link_t *clp)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	cache_link_t **pp;
17070Sstevel@tonic-gate 	const char *fcn = "link_delete";
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: removing link: %s\n", fcn, clp->path);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	if (clp->minor == NULL)
17120Sstevel@tonic-gate 		pp = &(CACHE(hdp)->dngl);
17130Sstevel@tonic-gate 	else
17140Sstevel@tonic-gate 		pp = &clp->minor->link;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	for (; *pp != NULL; pp = &(*pp)->sib) {
17170Sstevel@tonic-gate 		if (*pp == clp)
17180Sstevel@tonic-gate 			break;
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	if (*pp == NULL) {
17220Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: link(%s) not on list\n",
17230Sstevel@tonic-gate 		    fcn, clp->path);
17240Sstevel@tonic-gate 	} else {
17250Sstevel@tonic-gate 		*pp = clp->sib;
17260Sstevel@tonic-gate 	}
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	delete_unused_minor(hdp, clp->minor);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	clp->minor = NULL;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	link_free(&clp);
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate static void
17360Sstevel@tonic-gate delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp)
17370Sstevel@tonic-gate {
17380Sstevel@tonic-gate 	if (cmnp == NULL)
17390Sstevel@tonic-gate 		return;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 	if (cmnp->link != NULL)
17420Sstevel@tonic-gate 		return;
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	dprintf(DBG_STEP, "delete_unused_minor: removing minor(%s)\n",
17450Sstevel@tonic-gate 	    cmnp->name);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	minor_delete(hdp, cmnp);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate int
17510Sstevel@tonic-gate di_devlink_add_link(
17520Sstevel@tonic-gate 	di_devlink_handle_t hdp,
17530Sstevel@tonic-gate 	const char *link,
17540Sstevel@tonic-gate 	const char *content,
17550Sstevel@tonic-gate 	int flags)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate 	return (add_link(hdp, link, content, flags) != NULL ? 0 : -1);
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate static cache_link_t *
17610Sstevel@tonic-gate add_link(
17620Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
17630Sstevel@tonic-gate 	const char *link,
17640Sstevel@tonic-gate 	const char *content,
17650Sstevel@tonic-gate 	int flags)
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	uint32_t attr;
17680Sstevel@tonic-gate 	cache_link_t *clp;
17690Sstevel@tonic-gate 	cache_minor_t *cmnp;
17700Sstevel@tonic-gate 	const char *fcn = "add_link";
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	if (hdp == NULL || DB_ERR(hdp) || link == NULL ||
17730Sstevel@tonic-gate 	    link[0] == '/' || content == NULL || !link_flag(flags) ||
17740Sstevel@tonic-gate 	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
17750Sstevel@tonic-gate 		dprintf(DBG_ERR, "%s: %s: invalid args\n",
17760Sstevel@tonic-gate 		    fcn, link ? link : "<NULL>");
17770Sstevel@tonic-gate 		errno = EINVAL;
17780Sstevel@tonic-gate 		return (NULL);
17790Sstevel@tonic-gate 	}
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	if ((clp = link_hash(hdp, link, 0)) != NULL) {
17820Sstevel@tonic-gate 		if (link_cmp(clp, content, LINK_TYPE(flags)) != 0) {
17830Sstevel@tonic-gate 			(void) rm_link(hdp, link);
17840Sstevel@tonic-gate 		} else {
17850Sstevel@tonic-gate 			return (clp);
17860Sstevel@tonic-gate 		}
17870Sstevel@tonic-gate 	}
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	if (TYPE_PRI(flags)) {
17900Sstevel@tonic-gate 		const char *minor_path = NULL;
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		if (!is_minor_node(content, &minor_path)) {
17930Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: invalid content(%s)"
17940Sstevel@tonic-gate 			    " for primary link\n", fcn, content);
17950Sstevel@tonic-gate 			errno = EINVAL;
17960Sstevel@tonic-gate 			return (NULL);
17970Sstevel@tonic-gate 		}
17980Sstevel@tonic-gate 		if ((cmnp = lookup_minor(hdp, minor_path, NULL,
17990Sstevel@tonic-gate 		    TYPE_CACHE|CREATE_FLAG)) == NULL) {
18000Sstevel@tonic-gate 			return (NULL);
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 		attr = A_PRIMARY;
18030Sstevel@tonic-gate 	} else {
18040Sstevel@tonic-gate 		/*
18050Sstevel@tonic-gate 		 * Defer resolving a secondary link to a minor until the
18060Sstevel@tonic-gate 		 * database is closed. This ensures that the primary link
18070Sstevel@tonic-gate 		 * (required for a successful resolve) has also been created.
18080Sstevel@tonic-gate 		 */
18090Sstevel@tonic-gate 		cmnp = NULL;
18100Sstevel@tonic-gate 		attr = A_SECONDARY;
18110Sstevel@tonic-gate 	}
18120Sstevel@tonic-gate 
18130Sstevel@tonic-gate 	return (link_insert(hdp, cmnp, link, content, attr));
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate /*
18170Sstevel@tonic-gate  * Returns 0 on match or 1 otherwise.
18180Sstevel@tonic-gate  */
18190Sstevel@tonic-gate static int
18200Sstevel@tonic-gate link_cmp(cache_link_t *clp, const char *content, int type)
18210Sstevel@tonic-gate {
18220Sstevel@tonic-gate 	if (strcmp(clp->content, content) != 0)
18230Sstevel@tonic-gate 		return (1);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	if (attr2type(clp->attr) != type)
18260Sstevel@tonic-gate 		return (1);
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 	return (0);
18290Sstevel@tonic-gate }
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate int
18320Sstevel@tonic-gate di_devlink_update(di_devlink_handle_t hdp)
18330Sstevel@tonic-gate {
18340Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) {
18350Sstevel@tonic-gate 		errno = EINVAL;
18360Sstevel@tonic-gate 		return (-1);
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	/*
18400Sstevel@tonic-gate 	 * Reset the counter to schedule a synchronization with /dev on the next
18410Sstevel@tonic-gate 	 * di_devlink_close().
18420Sstevel@tonic-gate 	 */
18430Sstevel@tonic-gate 	CACHE(hdp)->update_count = 0;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	return (0);
18460Sstevel@tonic-gate }
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate static int
18490Sstevel@tonic-gate synchronize_db(di_devlink_handle_t hdp)
18500Sstevel@tonic-gate {
18510Sstevel@tonic-gate 	int hval;
18520Sstevel@tonic-gate 	cache_link_t *clp;
18530Sstevel@tonic-gate 	char pdup[PATH_MAX];
18540Sstevel@tonic-gate 	recurse_t rec = {NULL};
18550Sstevel@tonic-gate 	const char *fcn = "synchronize_db";
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 	rec.data = NULL;
18580Sstevel@tonic-gate 	rec.fcn = cache_dev_link;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	/*
18610Sstevel@tonic-gate 	 * Walk through $ROOT/dev, reading every link and marking the
18620Sstevel@tonic-gate 	 * corresponding cached version as valid(adding new links as needed).
18630Sstevel@tonic-gate 	 * Then walk through the cache and remove all unmarked links.
18640Sstevel@tonic-gate 	 */
18650Sstevel@tonic-gate 	if (recurse_dev(hdp, &rec) != 0) {
18660Sstevel@tonic-gate 		return (-1);
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) {
18700Sstevel@tonic-gate 		for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) {
18710Sstevel@tonic-gate 			if (GET_VALID_ATTR(clp->attr)) {
18720Sstevel@tonic-gate 				CLR_VALID_ATTR(clp->attr);
18730Sstevel@tonic-gate 				clp = clp->hash;
18740Sstevel@tonic-gate 				continue;
18750Sstevel@tonic-gate 			}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 			/*
18780Sstevel@tonic-gate 			 * The link is stale, so remove it. Since the link
18790Sstevel@tonic-gate 			 * will be destroyed, use a copy of the link path to
18800Sstevel@tonic-gate 			 * invoke the remove function.
18810Sstevel@tonic-gate 			 */
18820Sstevel@tonic-gate 			(void) snprintf(pdup, sizeof (pdup), "%s", clp->path);
18830Sstevel@tonic-gate 			clp = clp->hash;
18840Sstevel@tonic-gate 			(void) dprintf(DBG_STEP, "%s: removing invalid link:"
18850Sstevel@tonic-gate 			    " %s\n", fcn, pdup);
18860Sstevel@tonic-gate 			(void) di_devlink_rm_link(hdp, pdup);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 	}
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: update completed\n", fcn);
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	return (0);
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate static di_devlink_handle_t
18960Sstevel@tonic-gate di_devlink_init_impl(const char *root, const char *name, uint_t flags)
18970Sstevel@tonic-gate {
18980Sstevel@tonic-gate 	int	err = 0;
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	if ((flags != 0 && flags != DI_MAKE_LINK) ||
19010Sstevel@tonic-gate 	    (flags == 0 && name != NULL)) {
19020Sstevel@tonic-gate 		errno = EINVAL;
19030Sstevel@tonic-gate 		return (NULL);
19040Sstevel@tonic-gate 	}
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	if (flags == DI_MAKE_LINK && (err = devlink_create(root, name))) {
19070Sstevel@tonic-gate 		errno = err;
19080Sstevel@tonic-gate 		return (NULL);
19090Sstevel@tonic-gate 	}
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "devlink_init_impl: success\n");
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	return (devlink_snapshot(root));
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate di_devlink_handle_t
19170Sstevel@tonic-gate di_devlink_init(const char *name, uint_t flags)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	return (di_devlink_init_impl("/", name, flags));
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate 
19220Sstevel@tonic-gate di_devlink_handle_t
19230Sstevel@tonic-gate di_devlink_init_root(const char *root, const char *name, uint_t flags)
19240Sstevel@tonic-gate {
19250Sstevel@tonic-gate 	return (di_devlink_init_impl(root, name, flags));
19260Sstevel@tonic-gate }
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate static di_devlink_handle_t
19290Sstevel@tonic-gate devlink_snapshot(const char *root_dir)
19300Sstevel@tonic-gate {
19310Sstevel@tonic-gate 	struct di_devlink_handle *hdp;
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) {
19340Sstevel@tonic-gate 		return (NULL);
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	/*
19380Sstevel@tonic-gate 	 * If we cannot open the DB below, we will walk /dev
19390Sstevel@tonic-gate 	 * in di_devlink_walk.
19400Sstevel@tonic-gate 	 */
19410Sstevel@tonic-gate 	(void) open_db(hdp, OPEN_RDONLY);
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 	return (hdp);
19440Sstevel@tonic-gate }
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate int
19470Sstevel@tonic-gate di_devlink_fini(di_devlink_handle_t *pp)
19480Sstevel@tonic-gate {
19490Sstevel@tonic-gate 	if (pp == NULL || *pp == NULL || !HDL_RDONLY(*pp)) {
19500Sstevel@tonic-gate 		errno = EINVAL;
19510Sstevel@tonic-gate 		return (-1);
19520Sstevel@tonic-gate 	}
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	/* Freeing the handle also closes the DB */
19550Sstevel@tonic-gate 	handle_free(pp);
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	return (0);
19580Sstevel@tonic-gate }
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate int
19610Sstevel@tonic-gate di_devlink_walk(
19620Sstevel@tonic-gate 	di_devlink_handle_t hdp,
19630Sstevel@tonic-gate 	const char *re,
19640Sstevel@tonic-gate 	const char *minor_path,
19650Sstevel@tonic-gate 	uint_t flags,
19660Sstevel@tonic-gate 	void *arg,
19670Sstevel@tonic-gate 	int (*devlink_callback)(di_devlink_t, void *))
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	int rv;
19700Sstevel@tonic-gate 	regex_t reg;
19710Sstevel@tonic-gate 	link_desc_t linkd = {NULL};
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp)) {
19740Sstevel@tonic-gate 		errno = EINVAL;
19750Sstevel@tonic-gate 		return (-1);
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	linkd.minor_path = minor_path;
19790Sstevel@tonic-gate 	linkd.flags = flags;
19800Sstevel@tonic-gate 	linkd.arg = arg;
19810Sstevel@tonic-gate 	linkd.fcn = devlink_callback;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 	if (re) {
19840Sstevel@tonic-gate 		if (regcomp(&reg, re, REG_EXTENDED) != 0)
19850Sstevel@tonic-gate 			return (-1);
19860Sstevel@tonic-gate 		linkd.regp = &reg;
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	if (check_args(&linkd)) {
19900Sstevel@tonic-gate 		errno = EINVAL;
19910Sstevel@tonic-gate 		rv = -1;
19920Sstevel@tonic-gate 		goto out;
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	if (DB_OPEN(hdp)) {
19960Sstevel@tonic-gate 		rv = walk_db(hdp, &linkd);
19970Sstevel@tonic-gate 	} else {
19980Sstevel@tonic-gate 		rv = walk_dev(hdp, &linkd);
19990Sstevel@tonic-gate 	}
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate out:
20020Sstevel@tonic-gate 	if (re) {
20030Sstevel@tonic-gate 		regfree(&reg);
20040Sstevel@tonic-gate 	}
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	return (rv ? -1 : 0);
20070Sstevel@tonic-gate }
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate static int
20100Sstevel@tonic-gate link_flag(uint_t flags)
20110Sstevel@tonic-gate {
20120Sstevel@tonic-gate 	if (flags != 0 && flags != DI_PRIMARY_LINK &&
20130Sstevel@tonic-gate 	    flags != DI_SECONDARY_LINK) {
20140Sstevel@tonic-gate 		return (0);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	return (1);
20180Sstevel@tonic-gate }
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate /*
20210Sstevel@tonic-gate  * Currently allowed flags are:
20220Sstevel@tonic-gate  *	DI_PRIMARY_LINK
20230Sstevel@tonic-gate  *	DI_SECONDARY_LINK
20240Sstevel@tonic-gate  */
20250Sstevel@tonic-gate static int
20260Sstevel@tonic-gate check_args(link_desc_t *linkp)
20270Sstevel@tonic-gate {
20280Sstevel@tonic-gate 	if (linkp->fcn == NULL)
20290Sstevel@tonic-gate 		return (-1);
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	if (!link_flag(linkp->flags)) {
20320Sstevel@tonic-gate 		return (-1);
20330Sstevel@tonic-gate 	}
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	/*
20360Sstevel@tonic-gate 	 * Minor path can be NULL. In that case, all links will be
20370Sstevel@tonic-gate 	 * selected.
20380Sstevel@tonic-gate 	 */
20390Sstevel@tonic-gate 	if (linkp->minor_path) {
20400Sstevel@tonic-gate 		if (linkp->minor_path[0] != '/' ||
20410Sstevel@tonic-gate 		    minor_colon(linkp->minor_path) == NULL) {
20420Sstevel@tonic-gate 			return (-1);
20430Sstevel@tonic-gate 		}
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	return (0);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate /*
20510Sstevel@tonic-gate  * Walk all links in database if no minor path is specified.
20520Sstevel@tonic-gate  */
20530Sstevel@tonic-gate static int
20540Sstevel@tonic-gate walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp)
20550Sstevel@tonic-gate {
20560Sstevel@tonic-gate 	assert(DB_OPEN(hdp));
20570Sstevel@tonic-gate 
20580Sstevel@tonic-gate 	if (linkp->minor_path == NULL) {
20590Sstevel@tonic-gate 		return (walk_all_links(hdp, linkp));
20600Sstevel@tonic-gate 	} else {
20610Sstevel@tonic-gate 		return (walk_matching_links(hdp, linkp));
20620Sstevel@tonic-gate 	}
20630Sstevel@tonic-gate }
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate static int
20660Sstevel@tonic-gate cache_dev(struct di_devlink_handle *hdp)
20670Sstevel@tonic-gate {
20680Sstevel@tonic-gate 	size_t sz;
20690Sstevel@tonic-gate 	recurse_t rec = {NULL};
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	assert(hdp);
20720Sstevel@tonic-gate 	assert(HDL_RDONLY(hdp));
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp)) {
20750Sstevel@tonic-gate 		dprintf(DBG_ERR, "cache_dev: invalid arg\n");
20760Sstevel@tonic-gate 		return (-1);
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	sz = MIN_HASH_SIZE;
20800Sstevel@tonic-gate 
20810Sstevel@tonic-gate 	CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *));
20820Sstevel@tonic-gate 	if (CACHE(hdp)->hash == NULL) {
20830Sstevel@tonic-gate 		return (-1);
20840Sstevel@tonic-gate 	}
20850Sstevel@tonic-gate 	CACHE(hdp)->hash_sz = sz;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 	rec.data = NULL;
20880Sstevel@tonic-gate 	rec.fcn = cache_dev_link;
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 	return (recurse_dev(hdp, &rec));
20910Sstevel@tonic-gate }
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate static int
20940Sstevel@tonic-gate walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp)
20950Sstevel@tonic-gate {
20960Sstevel@tonic-gate 	assert(hdp && linkp);
20970Sstevel@tonic-gate 	assert(!DB_OPEN(hdp));
20980Sstevel@tonic-gate 	assert(HDL_RDONLY(hdp));
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) {
21010Sstevel@tonic-gate 		dprintf(DBG_ERR, "walk_dev: invalid args\n");
21020Sstevel@tonic-gate 		return (-1);
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) {
21060Sstevel@tonic-gate 		dprintf(DBG_ERR, "walk_dev: /dev caching failed\n");
21070Sstevel@tonic-gate 		return (-1);
21080Sstevel@tonic-gate 	}
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	if (linkp->minor_path)
21110Sstevel@tonic-gate 		walk_cache_minor(hdp, linkp->minor_path, linkp);
21120Sstevel@tonic-gate 	else
21130Sstevel@tonic-gate 		walk_all_cache(hdp, linkp);
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	return (linkp->retval);
21160Sstevel@tonic-gate }
21170Sstevel@tonic-gate 
21180Sstevel@tonic-gate /* ARGSUSED */
21190Sstevel@tonic-gate static int
21200Sstevel@tonic-gate cache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link)
21210Sstevel@tonic-gate {
21220Sstevel@tonic-gate 	int flags;
21230Sstevel@tonic-gate 	cache_link_t *clp;
21240Sstevel@tonic-gate 	char content[PATH_MAX];
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	if (s_readlink(link, content, sizeof (content)) < 0) {
21290Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
21300Sstevel@tonic-gate 	}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	if (is_minor_node(content, NULL)) {
21330Sstevel@tonic-gate 		flags = DI_PRIMARY_LINK;
21340Sstevel@tonic-gate 	} else {
21350Sstevel@tonic-gate 		flags = DI_SECONDARY_LINK;
21360Sstevel@tonic-gate 	}
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	/*
21410Sstevel@tonic-gate 	 * Store only the part after <root-dir>/dev/
21420Sstevel@tonic-gate 	 */
21430Sstevel@tonic-gate 	link += strlen(hdp->dev_dir) + 1;
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	if ((clp = add_link(hdp, link, content, flags)) != NULL) {
21460Sstevel@tonic-gate 		SET_VALID_ATTR(clp->attr);
21470Sstevel@tonic-gate 	}
21480Sstevel@tonic-gate 
21490Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
21500Sstevel@tonic-gate }
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate static int
21540Sstevel@tonic-gate walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
21550Sstevel@tonic-gate {
21560Sstevel@tonic-gate 	struct db_link *dlp;
21570Sstevel@tonic-gate 	uint32_t nidx, eidx;
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate 	assert(DB_NUM(hdp, DB_LINK) >= 1);
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	eidx = DB_NUM(hdp, DB_LINK);
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	/* Skip the "NIL" (index == 0) link. */
21640Sstevel@tonic-gate 	for (nidx = 1; nidx < eidx; nidx++) {
21650Sstevel@tonic-gate 		/*
21660Sstevel@tonic-gate 		 * Declare this local to the block with zero
21670Sstevel@tonic-gate 		 * initializer so that it gets rezeroed
21680Sstevel@tonic-gate 		 * for each iteration.
21690Sstevel@tonic-gate 		 */
21700Sstevel@tonic-gate 		struct di_devlink vlink = {NULL};
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		if ((dlp = get_link(hdp, nidx)) == NULL)
21730Sstevel@tonic-gate 			continue;
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate 		vlink.rel_path = get_string(hdp, dlp->path);
21760Sstevel@tonic-gate 		vlink.content = get_string(hdp, dlp->content);
21770Sstevel@tonic-gate 		vlink.type = attr2type(dlp->attr);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) {
21800Sstevel@tonic-gate 			break;
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 	}
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 	return (linkp->retval);
21850Sstevel@tonic-gate }
21860Sstevel@tonic-gate 
21870Sstevel@tonic-gate static int
21880Sstevel@tonic-gate walk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
21890Sstevel@tonic-gate {
21900Sstevel@tonic-gate 	uint32_t nidx;
21910Sstevel@tonic-gate 	struct db_link *dlp;
21920Sstevel@tonic-gate 	struct db_minor *dmp;
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 	assert(linkp->minor_path != NULL);
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB);
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 	/*
21990Sstevel@tonic-gate 	 * If a minor matching the path exists, walk that minor's devlinks list.
22000Sstevel@tonic-gate 	 * Then walk the dangling devlinks list. Non-matching devlinks will be
22010Sstevel@tonic-gate 	 * filtered out in visit_link.
22020Sstevel@tonic-gate 	 */
22030Sstevel@tonic-gate 	for (;;) {
22040Sstevel@tonic-gate 		nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx;
22050Sstevel@tonic-gate 		for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
22060Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 			vlink.rel_path = get_string(hdp, dlp->path);
22090Sstevel@tonic-gate 			vlink.content = get_string(hdp, dlp->content);
22100Sstevel@tonic-gate 			vlink.type = attr2type(dlp->attr);
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE)
22130Sstevel@tonic-gate 				goto out;
22140Sstevel@tonic-gate 		}
22150Sstevel@tonic-gate 		if (dmp == NULL) {
22160Sstevel@tonic-gate 			break;
22170Sstevel@tonic-gate 		} else {
22180Sstevel@tonic-gate 			dmp = NULL;
22190Sstevel@tonic-gate 		}
22200Sstevel@tonic-gate 	}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate out:
22230Sstevel@tonic-gate 	return (linkp->retval);
22240Sstevel@tonic-gate }
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate static int
22270Sstevel@tonic-gate visit_link(
22280Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
22290Sstevel@tonic-gate 	link_desc_t *linkp,
22300Sstevel@tonic-gate 	struct di_devlink *vlp)
22310Sstevel@tonic-gate {
22320Sstevel@tonic-gate 	struct stat sbuf;
22330Sstevel@tonic-gate 	const char *minor_path = NULL;
22340Sstevel@tonic-gate 	char abs_path[PATH_MAX], cont[PATH_MAX];
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	/*
22370Sstevel@tonic-gate 	 * It is legal for the link's content and type to be unknown.
22380Sstevel@tonic-gate 	 * but one of absolute or relative path must be set.
22390Sstevel@tonic-gate 	 */
22400Sstevel@tonic-gate 	if (vlp->rel_path == NULL && vlp->abs_path == NULL) {
22410Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "visit_link: invalid arguments\n");
22420Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	if (vlp->rel_path == NULL) {
22460Sstevel@tonic-gate 		vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path);
22470Sstevel@tonic-gate 		if (vlp->rel_path == NULL || vlp->rel_path[0] == '\0')
22480Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22490Sstevel@tonic-gate 	}
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	if (linkp->regp) {
22520Sstevel@tonic-gate 		if (regexec(linkp->regp, vlp->rel_path, 0, NULL, 0) != 0)
22530Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22540Sstevel@tonic-gate 	}
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 	if (vlp->abs_path == NULL) {
22570Sstevel@tonic-gate 		assert(vlp->rel_path[0] != '/');
22580Sstevel@tonic-gate 		(void) snprintf(abs_path, sizeof (abs_path), "%s/%s",
22590Sstevel@tonic-gate 		    hdp->dev_dir, vlp->rel_path);
22600Sstevel@tonic-gate 		vlp->abs_path = abs_path;
22610Sstevel@tonic-gate 	}
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	if (vlp->content == NULL) {
22640Sstevel@tonic-gate 		if (s_readlink(vlp->abs_path, cont, sizeof (cont)) < 0) {
22650Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22660Sstevel@tonic-gate 		}
22670Sstevel@tonic-gate 		vlp->content = cont;
22680Sstevel@tonic-gate 	}
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	if (vlp->type == 0) {
22720Sstevel@tonic-gate 		if (is_minor_node(vlp->content, &minor_path)) {
22730Sstevel@tonic-gate 			vlp->type = DI_PRIMARY_LINK;
22740Sstevel@tonic-gate 		} else {
22750Sstevel@tonic-gate 			vlp->type = DI_SECONDARY_LINK;
22760Sstevel@tonic-gate 		}
22770Sstevel@tonic-gate 	}
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	/*
22800Sstevel@tonic-gate 	 * Filter based on minor path
22810Sstevel@tonic-gate 	 */
22820Sstevel@tonic-gate 	if (linkp->minor_path) {
22830Sstevel@tonic-gate 		char tmp[PATH_MAX];
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		/*
22860Sstevel@tonic-gate 		 * derive minor path
22870Sstevel@tonic-gate 		 */
22880Sstevel@tonic-gate 		if (vlp->type == DI_SECONDARY_LINK) {
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate #ifdef	DEBUG
22910Sstevel@tonic-gate 			/*LINTED*/
22920Sstevel@tonic-gate 			assert(sizeof (tmp) >= PATH_MAX);
22930Sstevel@tonic-gate #endif
22940Sstevel@tonic-gate 			if (realpath(vlp->abs_path, tmp) == NULL)
22950Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 			if (!is_minor_node(tmp, &minor_path))
22980Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 		} else if (minor_path == NULL) {
23010Sstevel@tonic-gate 			if (!is_minor_node(vlp->content, &minor_path))
23020Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
23030Sstevel@tonic-gate 		}
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 		assert(minor_path != NULL);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 		if (strcmp(linkp->minor_path, minor_path) != 0)
23080Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	/*
23120Sstevel@tonic-gate 	 * Filter based on link type
23130Sstevel@tonic-gate 	 */
23140Sstevel@tonic-gate 	if (!TYPE_NONE(linkp->flags) && LINK_TYPE(linkp->flags) != vlp->type) {
23150Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	if (lstat(vlp->abs_path, &sbuf) < 0) {
23190Sstevel@tonic-gate 		dprintf(DBG_ERR, "visit_link: %s: lstat failed: %s\n",
23200Sstevel@tonic-gate 		    vlp->abs_path, strerror(errno));
23210Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	return (linkp->fcn(vlp, linkp->arg));
23250Sstevel@tonic-gate }
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate static int
23280Sstevel@tonic-gate devlink_valid(di_devlink_t devlink)
23290Sstevel@tonic-gate {
23300Sstevel@tonic-gate 	if (devlink == NULL || devlink->rel_path == NULL ||
23310Sstevel@tonic-gate 	    devlink->abs_path == NULL || devlink->content == NULL ||
23320Sstevel@tonic-gate 	    TYPE_NONE(devlink->type)) {
23330Sstevel@tonic-gate 		return (0);
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	return (1);
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate const char *
23400Sstevel@tonic-gate di_devlink_path(di_devlink_t devlink)
23410Sstevel@tonic-gate {
23420Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23430Sstevel@tonic-gate 		errno = EINVAL;
23440Sstevel@tonic-gate 		return (NULL);
23450Sstevel@tonic-gate 	}
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 	return (devlink->abs_path);
23480Sstevel@tonic-gate }
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate const char *
23510Sstevel@tonic-gate di_devlink_content(di_devlink_t devlink)
23520Sstevel@tonic-gate {
23530Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23540Sstevel@tonic-gate 		errno = EINVAL;
23550Sstevel@tonic-gate 		return (NULL);
23560Sstevel@tonic-gate 	}
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	return (devlink->content);
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate int
23620Sstevel@tonic-gate di_devlink_type(di_devlink_t devlink)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23650Sstevel@tonic-gate 		errno = EINVAL;
23660Sstevel@tonic-gate 		return (-1);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	return (devlink->type);
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate di_devlink_t
23730Sstevel@tonic-gate di_devlink_dup(di_devlink_t devlink)
23740Sstevel@tonic-gate {
23750Sstevel@tonic-gate 	struct di_devlink *duplink;
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23780Sstevel@tonic-gate 		errno = EINVAL;
23790Sstevel@tonic-gate 		return (NULL);
23800Sstevel@tonic-gate 	}
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 	if ((duplink = calloc(1, sizeof (struct di_devlink))) == NULL) {
23830Sstevel@tonic-gate 		return (NULL);
23840Sstevel@tonic-gate 	}
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	duplink->rel_path = strdup(devlink->rel_path);
23870Sstevel@tonic-gate 	duplink->abs_path = strdup(devlink->abs_path);
23880Sstevel@tonic-gate 	duplink->content  = strdup(devlink->content);
23890Sstevel@tonic-gate 	duplink->type	  = devlink->type;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	if (!devlink_valid(duplink)) {
23920Sstevel@tonic-gate 		(void) di_devlink_free(duplink);
23930Sstevel@tonic-gate 		errno = ENOMEM;
23940Sstevel@tonic-gate 		return (NULL);
23950Sstevel@tonic-gate 	}
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	return (duplink);
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate int
24010Sstevel@tonic-gate di_devlink_free(di_devlink_t devlink)
24020Sstevel@tonic-gate {
24030Sstevel@tonic-gate 	if (devlink == NULL) {
24040Sstevel@tonic-gate 		errno = EINVAL;
24050Sstevel@tonic-gate 		return (-1);
24060Sstevel@tonic-gate 	}
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	free(devlink->rel_path);
24090Sstevel@tonic-gate 	free(devlink->abs_path);
24100Sstevel@tonic-gate 	free(devlink->content);
24110Sstevel@tonic-gate 	free(devlink);
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	return (0);
24140Sstevel@tonic-gate }
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate /*
24170Sstevel@tonic-gate  * Obtain path relative to dev_dir
24180Sstevel@tonic-gate  */
24190Sstevel@tonic-gate static const char *
24200Sstevel@tonic-gate rel_path(struct di_devlink_handle *hdp, const char *path)
24210Sstevel@tonic-gate {
24220Sstevel@tonic-gate 	const size_t len = strlen(hdp->dev_dir);
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	if (strncmp(path, hdp->dev_dir, len) != 0)
24250Sstevel@tonic-gate 		return (NULL);
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate 	if (path[len] == '\0')
24280Sstevel@tonic-gate 		return (&path[len]);
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	if (path[len] != '/')
24310Sstevel@tonic-gate 		return (NULL);
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate 	return (&path[len+1]);
24340Sstevel@tonic-gate }
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate static int
24370Sstevel@tonic-gate recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp)
24380Sstevel@tonic-gate {
24390Sstevel@tonic-gate 	int ret = 0;
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 	(void) do_recurse(hdp->dev_dir, hdp, rp, &ret);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	return (ret);
24440Sstevel@tonic-gate }
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate static int
24470Sstevel@tonic-gate do_recurse(
24480Sstevel@tonic-gate 	const char *dir,
24490Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
24500Sstevel@tonic-gate 	recurse_t *rp,
24510Sstevel@tonic-gate 	int *retp)
24520Sstevel@tonic-gate {
24530Sstevel@tonic-gate 	size_t len;
24540Sstevel@tonic-gate 	const char *rel;
24550Sstevel@tonic-gate 	struct stat sbuf;
24560Sstevel@tonic-gate 	char cur[PATH_MAX], *cp;
24570Sstevel@tonic-gate 	int i, rv = DI_WALK_CONTINUE;
2458*2621Sllai1 	finddevhdl_t handle;
2459*2621Sllai1 	char *d_name;
24600Sstevel@tonic-gate 
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	if ((rel = rel_path(hdp, dir)) == NULL)
24630Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	/*
24660Sstevel@tonic-gate 	 * Skip directories we are not interested in.
24670Sstevel@tonic-gate 	 */
24680Sstevel@tonic-gate 	for (i = 0; i < N_SKIP_DIRS; i++) {
24690Sstevel@tonic-gate 		if (strcmp(rel, skip_dirs[i]) == 0) {
24700Sstevel@tonic-gate 			(void) dprintf(DBG_STEP, "do_recurse: skipping %s\n",
24710Sstevel@tonic-gate 			    dir);
24720Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
24730Sstevel@tonic-gate 		}
24740Sstevel@tonic-gate 	}
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "do_recurse: dir = %s\n", dir);
24770Sstevel@tonic-gate 
2478*2621Sllai1 	if (finddev_readdir(dir, &handle) != 0)
24790Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	(void) snprintf(cur, sizeof (cur), "%s/", dir);
24820Sstevel@tonic-gate 	len = strlen(cur);
24830Sstevel@tonic-gate 	cp = cur + len;
24840Sstevel@tonic-gate 	len = sizeof (cur) - len;
24850Sstevel@tonic-gate 
2486*2621Sllai1 	for (;;) {
2487*2621Sllai1 		if ((d_name = (char *)finddev_next(handle)) == NULL)
2488*2621Sllai1 			break;
2489*2621Sllai1 
2490*2621Sllai1 		if (strlcpy(cp, d_name, len) >= len)
2491*2621Sllai1 			break;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		/*
24940Sstevel@tonic-gate 		 * Skip files we are not interested in.
24950Sstevel@tonic-gate 		 */
24960Sstevel@tonic-gate 		for (i = 0; i < N_SKIP_FILES; i++) {
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 			rel = rel_path(hdp, cur);
24990Sstevel@tonic-gate 			if (rel == NULL || strcmp(rel, skip_files[i]) == 0) {
25000Sstevel@tonic-gate 				(void) dprintf(DBG_STEP,
25010Sstevel@tonic-gate 				    "do_recurse: skipping %s\n", cur);
25020Sstevel@tonic-gate 				goto next_entry;
25030Sstevel@tonic-gate 			}
25040Sstevel@tonic-gate 		}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 		if (lstat(cur, &sbuf) == 0) {
25070Sstevel@tonic-gate 			if (S_ISDIR(sbuf.st_mode)) {
25080Sstevel@tonic-gate 				rv = do_recurse(cur, hdp, rp, retp);
25090Sstevel@tonic-gate 			} else if (S_ISLNK(sbuf.st_mode)) {
25100Sstevel@tonic-gate 				rv = rp->fcn(hdp, rp->data, cur);
25110Sstevel@tonic-gate 			} else {
25120Sstevel@tonic-gate 				(void) dprintf(DBG_STEP,
25130Sstevel@tonic-gate 				    "do_recurse: Skipping entry: %s\n", cur);
25140Sstevel@tonic-gate 			}
25150Sstevel@tonic-gate 		} else {
25160Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "do_recurse: cur(%s): lstat"
25170Sstevel@tonic-gate 			    " failed: %s\n", cur, strerror(errno));
25180Sstevel@tonic-gate 		}
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate next_entry:
25210Sstevel@tonic-gate 		*cp = '\0';
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 		if (rv != DI_WALK_CONTINUE)
25240Sstevel@tonic-gate 			break;
25250Sstevel@tonic-gate 	}
25260Sstevel@tonic-gate 
2527*2621Sllai1 	finddev_close(handle);
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	return (rv);
25300Sstevel@tonic-gate }
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate static int
25340Sstevel@tonic-gate check_attr(uint32_t attr)
25350Sstevel@tonic-gate {
25360Sstevel@tonic-gate 	switch (attr & A_LINK_TYPES) {
25370Sstevel@tonic-gate 		case A_PRIMARY:
25380Sstevel@tonic-gate 		case A_SECONDARY:
25390Sstevel@tonic-gate 			return (1);
25400Sstevel@tonic-gate 		default:
25410Sstevel@tonic-gate 			dprintf(DBG_ERR, "check_attr: incorrect attr(%u)\n",
25420Sstevel@tonic-gate 			    attr);
25430Sstevel@tonic-gate 			return (0);
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate }
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate static int
25480Sstevel@tonic-gate attr2type(uint32_t attr)
25490Sstevel@tonic-gate {
25500Sstevel@tonic-gate 	switch (attr & A_LINK_TYPES) {
25510Sstevel@tonic-gate 		case A_PRIMARY:
25520Sstevel@tonic-gate 			return (DI_PRIMARY_LINK);
25530Sstevel@tonic-gate 		case A_SECONDARY:
25540Sstevel@tonic-gate 			return (DI_SECONDARY_LINK);
25550Sstevel@tonic-gate 		default:
25560Sstevel@tonic-gate 			dprintf(DBG_ERR, "attr2type: incorrect attr(%u)\n",
25570Sstevel@tonic-gate 			    attr);
25580Sstevel@tonic-gate 			return (0);
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate }
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate /* Allocate new node and link it in */
25630Sstevel@tonic-gate static cache_node_t *
25640Sstevel@tonic-gate node_insert(
25650Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
25660Sstevel@tonic-gate 	cache_node_t *pcnp,
25670Sstevel@tonic-gate 	const char *path,
25680Sstevel@tonic-gate 	int insert)
25690Sstevel@tonic-gate {
25700Sstevel@tonic-gate 	cache_node_t *cnp;
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	if (path == NULL) {
25730Sstevel@tonic-gate 		errno = EINVAL;
25740Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25750Sstevel@tonic-gate 		return (NULL);
25760Sstevel@tonic-gate 	}
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	if ((cnp = calloc(1, sizeof (cache_node_t))) == NULL) {
25790Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25800Sstevel@tonic-gate 		return (NULL);
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 	if ((cnp->path = strdup(path)) == NULL) {
25840Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25850Sstevel@tonic-gate 		free(cnp);
25860Sstevel@tonic-gate 		return (NULL);
25870Sstevel@tonic-gate 	}
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	cnp->parent = pcnp;
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	if (pcnp == NULL) {
25920Sstevel@tonic-gate 		assert(strcmp(path, "/") == 0);
25930Sstevel@tonic-gate 		assert(CACHE(hdp)->root == NULL);
25940Sstevel@tonic-gate 		CACHE(hdp)->root = cnp;
25950Sstevel@tonic-gate 	} else if (insert == INSERT_HEAD) {
25960Sstevel@tonic-gate 		cnp->sib = pcnp->child;
25970Sstevel@tonic-gate 		pcnp->child = cnp;
25980Sstevel@tonic-gate 	} else if (CACHE_LAST(hdp) && CACHE_LAST(hdp)->node &&
25990Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node->parent == pcnp &&
26000Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node->sib == NULL) {
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 		CACHE_LAST(hdp)->node->sib = cnp;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	} else {
26050Sstevel@tonic-gate 		cache_node_t **pp;
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 		for (pp = &pcnp->child; *pp != NULL; pp = &(*pp)->sib)
26080Sstevel@tonic-gate 			;
26090Sstevel@tonic-gate 		*pp = cnp;
26100Sstevel@tonic-gate 	}
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	return (cnp);
26130Sstevel@tonic-gate }
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate /*
26160Sstevel@tonic-gate  * Allocate a new minor and link it in either at the tail or head
26170Sstevel@tonic-gate  * of the minor list depending on the value of "prev".
26180Sstevel@tonic-gate  */
26190Sstevel@tonic-gate static cache_minor_t *
26200Sstevel@tonic-gate minor_insert(
26210Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
26220Sstevel@tonic-gate 	cache_node_t *pcnp,
26230Sstevel@tonic-gate 	const char *name,
26240Sstevel@tonic-gate 	const char *nodetype,
26250Sstevel@tonic-gate 	cache_minor_t **prev)
26260Sstevel@tonic-gate {
26270Sstevel@tonic-gate 	cache_minor_t *cmnp;
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	if (pcnp == NULL || name == NULL) {
26300Sstevel@tonic-gate 		errno = EINVAL;
26310Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26320Sstevel@tonic-gate 		return (NULL);
26330Sstevel@tonic-gate 	}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	/*
26360Sstevel@tonic-gate 	 * Some pseudo drivers don't specify nodetype. Assume pseudo if
26370Sstevel@tonic-gate 	 * nodetype is not specified.
26380Sstevel@tonic-gate 	 */
26390Sstevel@tonic-gate 	if (nodetype == NULL)
26400Sstevel@tonic-gate 		nodetype = DDI_PSEUDO;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	if ((cmnp = calloc(1, sizeof (cache_minor_t))) == NULL) {
26430Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26440Sstevel@tonic-gate 		return (NULL);
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	cmnp->name = strdup(name);
26480Sstevel@tonic-gate 	cmnp->nodetype = strdup(nodetype);
26490Sstevel@tonic-gate 	if (cmnp->name == NULL || cmnp->nodetype == NULL) {
26500Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26510Sstevel@tonic-gate 		free(cmnp->name);
26520Sstevel@tonic-gate 		free(cmnp->nodetype);
26530Sstevel@tonic-gate 		free(cmnp);
26540Sstevel@tonic-gate 		return (NULL);
26550Sstevel@tonic-gate 	}
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 	cmnp->node = pcnp;
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	/* Add to node's minor list */
26600Sstevel@tonic-gate 	if (prev == NULL) {
26610Sstevel@tonic-gate 		cmnp->sib = pcnp->minor;
26620Sstevel@tonic-gate 		pcnp->minor = cmnp;
26630Sstevel@tonic-gate 	} else {
26640Sstevel@tonic-gate 		assert(*prev == NULL);
26650Sstevel@tonic-gate 		*prev = cmnp;
26660Sstevel@tonic-gate 	}
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	return (cmnp);
26690Sstevel@tonic-gate }
26700Sstevel@tonic-gate 
26710Sstevel@tonic-gate static cache_link_t *
26720Sstevel@tonic-gate link_insert(
26730Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
26740Sstevel@tonic-gate 	cache_minor_t *cmnp,
26750Sstevel@tonic-gate 	const char *path,
26760Sstevel@tonic-gate 	const char *content,
26770Sstevel@tonic-gate 	uint32_t attr)
26780Sstevel@tonic-gate {
26790Sstevel@tonic-gate 	cache_link_t *clp;
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	if (path == NULL || content == NULL || !check_attr(attr)) {
26820Sstevel@tonic-gate 		errno = EINVAL;
26830Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26840Sstevel@tonic-gate 		return (NULL);
26850Sstevel@tonic-gate 	}
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	if ((clp = calloc(1, sizeof (cache_link_t))) == NULL) {
26880Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26890Sstevel@tonic-gate 		return (NULL);
26900Sstevel@tonic-gate 	}
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	clp->path = strdup(path);
26930Sstevel@tonic-gate 	clp->content = strdup(content);
26940Sstevel@tonic-gate 	if (clp->path == NULL || clp->content == NULL) {
26950Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26960Sstevel@tonic-gate 		link_free(&clp);
26970Sstevel@tonic-gate 		return (NULL);
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	clp->attr = attr;
27010Sstevel@tonic-gate 	hash_insert(hdp, clp);
27020Sstevel@tonic-gate 	clp->minor = cmnp;
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	/* Add to minor's link list */
27050Sstevel@tonic-gate 	if (cmnp != NULL) {
27060Sstevel@tonic-gate 		clp->sib = cmnp->link;
27070Sstevel@tonic-gate 		cmnp->link = clp;
27080Sstevel@tonic-gate 	} else {
27090Sstevel@tonic-gate 		clp->sib = CACHE(hdp)->dngl;
27100Sstevel@tonic-gate 		CACHE(hdp)->dngl = clp;
27110Sstevel@tonic-gate 	}
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	return (clp);
27140Sstevel@tonic-gate }
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate static void
27170Sstevel@tonic-gate hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp)
27180Sstevel@tonic-gate {
27190Sstevel@tonic-gate 	uint_t hval;
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 	hval = hashfn(hdp, clp->path);
27220Sstevel@tonic-gate 	clp->hash = CACHE_HASH(hdp, hval);
27230Sstevel@tonic-gate 	CACHE_HASH(hdp, hval) = clp;
27240Sstevel@tonic-gate }
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate static struct db_node *
27280Sstevel@tonic-gate get_node(struct di_devlink_handle *hdp, uint32_t idx)
27290Sstevel@tonic-gate {
27300Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_NODE));
27310Sstevel@tonic-gate }
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate static struct db_node *
27340Sstevel@tonic-gate set_node(struct di_devlink_handle *hdp, uint32_t idx)
27350Sstevel@tonic-gate {
27360Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_NODE));
27370Sstevel@tonic-gate }
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate static struct db_minor *
27400Sstevel@tonic-gate get_minor(struct di_devlink_handle *hdp, uint32_t idx)
27410Sstevel@tonic-gate {
27420Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_MINOR));
27430Sstevel@tonic-gate }
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate static struct db_minor *
27460Sstevel@tonic-gate set_minor(struct di_devlink_handle *hdp, uint32_t idx)
27470Sstevel@tonic-gate {
27480Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_MINOR));
27490Sstevel@tonic-gate }
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate static struct db_link *
27520Sstevel@tonic-gate get_link(struct di_devlink_handle *hdp, uint32_t idx)
27530Sstevel@tonic-gate {
27540Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_LINK));
27550Sstevel@tonic-gate }
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate static struct db_link *
27580Sstevel@tonic-gate set_link(struct di_devlink_handle *hdp, uint32_t idx)
27590Sstevel@tonic-gate {
27600Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_LINK));
27610Sstevel@tonic-gate }
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate static char *
27640Sstevel@tonic-gate get_string(struct di_devlink_handle *hdp, uint32_t idx)
27650Sstevel@tonic-gate {
27660Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_STR));
27670Sstevel@tonic-gate }
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate static char *
27700Sstevel@tonic-gate set_string(struct di_devlink_handle *hdp, uint32_t idx)
27710Sstevel@tonic-gate {
27720Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_STR));
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate /*
27770Sstevel@tonic-gate  * Returns the element corresponding to idx. If the portion of file involved
27780Sstevel@tonic-gate  * is not yet mapped, does an mmap() as well. Existing mappings are not changed.
27790Sstevel@tonic-gate  */
27800Sstevel@tonic-gate static void *
27810Sstevel@tonic-gate map_seg(
27820Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
27830Sstevel@tonic-gate 	uint32_t idx,
27840Sstevel@tonic-gate 	int prot,
27850Sstevel@tonic-gate 	db_seg_t seg)
27860Sstevel@tonic-gate {
27870Sstevel@tonic-gate 	int s;
27880Sstevel@tonic-gate 	off_t off;
27890Sstevel@tonic-gate 	size_t slen;
27900Sstevel@tonic-gate 	caddr_t addr;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	if (idx == DB_NIL) {
27930Sstevel@tonic-gate 		return (NULL);
27940Sstevel@tonic-gate 	}
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	if (!VALID_INDEX(hdp, seg, idx)) {
27970Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: seg(%d): invalid idx(%u)\n",
27980Sstevel@tonic-gate 		    seg, idx);
27990Sstevel@tonic-gate 		return (NULL);
28000Sstevel@tonic-gate 	}
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 	/*
28030Sstevel@tonic-gate 	 * If the seg is already mapped in, use it if the access type is
28040Sstevel@tonic-gate 	 * valid.
28050Sstevel@tonic-gate 	 */
28060Sstevel@tonic-gate 	if (DB_SEG(hdp, seg) != NULL) {
28070Sstevel@tonic-gate 		if (DB_SEG_PROT(hdp, seg) != prot) {
28080Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "map_seg: illegal access: "
28090Sstevel@tonic-gate 			    "seg[%d]: idx=%u, seg_prot=%d, access=%d\n",
28100Sstevel@tonic-gate 			    seg, idx, DB_SEG_PROT(hdp, seg), prot);
28110Sstevel@tonic-gate 			return (NULL);
28120Sstevel@tonic-gate 		}
28130Sstevel@tonic-gate 		return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
28140Sstevel@tonic-gate 	}
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	/*
28170Sstevel@tonic-gate 	 * Segment is not mapped. Mmap() the segment.
28180Sstevel@tonic-gate 	 */
28190Sstevel@tonic-gate 	off = seg_size(hdp, DB_HEADER);
28200Sstevel@tonic-gate 	for (s = 0; s < seg; s++) {
28210Sstevel@tonic-gate 		off += seg_size(hdp, s);
28220Sstevel@tonic-gate 	}
28230Sstevel@tonic-gate 	slen = seg_size(hdp, seg);
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	addr = mmap(0, slen, prot, MAP_SHARED, DB(hdp)->db_fd, off);
28260Sstevel@tonic-gate 	if (addr == MAP_FAILED) {
28270Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: seg[%d]: mmap failed: %s\n",
28280Sstevel@tonic-gate 		    seg, strerror(errno));
28290Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: args: len=%lu, prot=%d,"
28300Sstevel@tonic-gate 		    " fd=%d, off=%ld\n", (ulong_t)slen, prot, DB(hdp)->db_fd,
28310Sstevel@tonic-gate 		    off);
28320Sstevel@tonic-gate 		return (NULL);
28330Sstevel@tonic-gate 	}
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	DB_SEG(hdp, seg) = addr;
28360Sstevel@tonic-gate 	DB_SEG_PROT(hdp, seg) = prot;
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "map_seg: seg[%d]: len=%lu, prot=%d, fd=%d, "
28390Sstevel@tonic-gate 	    "off=%ld, seg_base=%p\n", seg, (ulong_t)slen, prot, DB(hdp)->db_fd,
28400Sstevel@tonic-gate 	    off, (void *)addr);
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 	return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
28430Sstevel@tonic-gate }
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate /*
28460Sstevel@tonic-gate  * Computes the size of a segment rounded up to the nearest page boundary.
28470Sstevel@tonic-gate  */
28480Sstevel@tonic-gate static size_t
28490Sstevel@tonic-gate seg_size(struct di_devlink_handle *hdp, int seg)
28500Sstevel@tonic-gate {
28510Sstevel@tonic-gate 	size_t sz;
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate 	assert(DB_HDR(hdp)->page_sz);
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	if (seg == DB_HEADER) {
28560Sstevel@tonic-gate 		sz = HDR_LEN;
28570Sstevel@tonic-gate 	} else {
28580Sstevel@tonic-gate 		assert(DB_NUM(hdp, seg) >= 1);
28590Sstevel@tonic-gate 		sz = DB_NUM(hdp, seg) * elem_sizes[seg];
28600Sstevel@tonic-gate 	}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	sz = (sz / DB_HDR(hdp)->page_sz) + 1;
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	sz *= DB_HDR(hdp)->page_sz;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	return (sz);
28670Sstevel@tonic-gate }
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate static size_t
28700Sstevel@tonic-gate size_db(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
28710Sstevel@tonic-gate {
28720Sstevel@tonic-gate 	int i;
28730Sstevel@tonic-gate 	size_t sz;
28740Sstevel@tonic-gate 	cache_link_t *clp;
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 	assert(page_sz > 0);
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	/* Take "NIL" element into account */
28790Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
28800Sstevel@tonic-gate 		count[i] = 1;
28810Sstevel@tonic-gate 	}
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 	count_node(CACHE(hdp)->root, count);
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 	for (clp = CACHE(hdp)->dngl; clp != NULL; clp = clp->sib) {
28860Sstevel@tonic-gate 		count_link(clp, count);
28870Sstevel@tonic-gate 	}
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	sz = ((HDR_LEN / page_sz) + 1) * page_sz;
28900Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
28910Sstevel@tonic-gate 		assert(count[i] >= 1);
28920Sstevel@tonic-gate 		sz += (((count[i] * elem_sizes[i]) / page_sz) + 1) * page_sz;
28930Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "N[%u]=%u\n", i, count[i]);
28940Sstevel@tonic-gate 	}
28950Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "DB size=%lu\n", (ulong_t)sz);
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 	return (sz);
28980Sstevel@tonic-gate }
28990Sstevel@tonic-gate 
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate static void
29020Sstevel@tonic-gate count_node(cache_node_t *cnp, uint32_t *count)
29030Sstevel@tonic-gate {
29040Sstevel@tonic-gate 	cache_minor_t *cmnp;
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	if (cnp == NULL)
29070Sstevel@tonic-gate 		return;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	count[DB_NODE]++;
29100Sstevel@tonic-gate 	count_string(cnp->path, count);
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) {
29130Sstevel@tonic-gate 		count_minor(cmnp, count);
29140Sstevel@tonic-gate 	}
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	for (cnp = cnp->child; cnp != NULL; cnp = cnp->sib) {
29170Sstevel@tonic-gate 		count_node(cnp, count);
29180Sstevel@tonic-gate 	}
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate }
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate static void
29230Sstevel@tonic-gate count_minor(cache_minor_t *cmnp, uint32_t *count)
29240Sstevel@tonic-gate {
29250Sstevel@tonic-gate 	cache_link_t *clp;
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 	if (cmnp == NULL)
29280Sstevel@tonic-gate 		return;
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 	count[DB_MINOR]++;
29310Sstevel@tonic-gate 	count_string(cmnp->name, count);
29320Sstevel@tonic-gate 	count_string(cmnp->nodetype, count);
29330Sstevel@tonic-gate 
29340Sstevel@tonic-gate 	for (clp = cmnp->link; clp != NULL; clp = clp->sib) {
29350Sstevel@tonic-gate 		count_link(clp, count);
29360Sstevel@tonic-gate 	}
29370Sstevel@tonic-gate }
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate static void
29400Sstevel@tonic-gate count_link(cache_link_t *clp, uint32_t *count)
29410Sstevel@tonic-gate {
29420Sstevel@tonic-gate 	if (clp == NULL)
29430Sstevel@tonic-gate 		return;
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate 	count[DB_LINK]++;
29460Sstevel@tonic-gate 	count_string(clp->path, count);
29470Sstevel@tonic-gate 	count_string(clp->content, count);
29480Sstevel@tonic-gate }
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 
29510Sstevel@tonic-gate static void
29520Sstevel@tonic-gate count_string(const char *str, uint32_t *count)
29530Sstevel@tonic-gate {
29540Sstevel@tonic-gate 	if (str == NULL) {
29550Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "count_string: NULL argument\n");
29560Sstevel@tonic-gate 		return;
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate 
29590Sstevel@tonic-gate 	count[DB_STR] += strlen(str) + 1;
29600Sstevel@tonic-gate }
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate static uint_t
29630Sstevel@tonic-gate hashfn(struct di_devlink_handle *hdp, const char *str)
29640Sstevel@tonic-gate {
29650Sstevel@tonic-gate 	const char *cp;
29660Sstevel@tonic-gate 	ulong_t hval = 0;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	if (str == NULL) {
29690Sstevel@tonic-gate 		return (0);
29700Sstevel@tonic-gate 	}
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	assert(CACHE(hdp)->hash_sz >= MIN_HASH_SIZE);
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 	for (cp = str; *cp != '\0'; cp++) {
29750Sstevel@tonic-gate 		hval += *cp;
29760Sstevel@tonic-gate 	}
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 	return (hval % CACHE(hdp)->hash_sz);
29790Sstevel@tonic-gate }
29800Sstevel@tonic-gate 
29810Sstevel@tonic-gate static int
29820Sstevel@tonic-gate enter_update_lock(struct di_devlink_handle *hdp)
29830Sstevel@tonic-gate {
29840Sstevel@tonic-gate 	int i, fd, rv;
29850Sstevel@tonic-gate 	struct flock lock;
29860Sstevel@tonic-gate 	char lockfile[PATH_MAX];
29870Sstevel@tonic-gate 
29880Sstevel@tonic-gate 	assert(hdp->lock_fd < 0);
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	get_db_path(hdp, DB_LOCK, lockfile, sizeof (lockfile));
29910Sstevel@tonic-gate 
29920Sstevel@tonic-gate 	/*
29930Sstevel@tonic-gate 	 * Record locks are per-process. Protect against multiple threads.
29940Sstevel@tonic-gate 	 */
29950Sstevel@tonic-gate 	(void) mutex_lock(&update_mutex);
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	if ((fd = open(lockfile, O_RDWR|O_CREAT, DB_LOCK_PERMS)) < 0) {
29980Sstevel@tonic-gate 		goto error;
29990Sstevel@tonic-gate 	}
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
30020Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
30030Sstevel@tonic-gate 	lock.l_start = 0;
30040Sstevel@tonic-gate 	lock.l_len = 0;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	i = 1;
30070Sstevel@tonic-gate 	while ((rv = fcntl(fd, F_SETLKW, &lock)) == -1 && errno == EINTR) {
30080Sstevel@tonic-gate 		if (i < MAX_LOCK_RETRY) {
30090Sstevel@tonic-gate 			i++;
30100Sstevel@tonic-gate 		} else {
30110Sstevel@tonic-gate 			break;
30120Sstevel@tonic-gate 		}
30130Sstevel@tonic-gate 	}
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	if (rv == 0) {
30160Sstevel@tonic-gate 		hdp->lock_fd = fd;
30170Sstevel@tonic-gate 		return (0);
30180Sstevel@tonic-gate 	} else {
30190Sstevel@tonic-gate 		(void) close(fd);
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate error:
30230Sstevel@tonic-gate 	(void) mutex_unlock(&update_mutex);
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	dprintf(DBG_ERR, "lockfile(%s): lock failed: %s\n", lockfile,
30260Sstevel@tonic-gate 	    strerror(errno));
30270Sstevel@tonic-gate 	return (-1);
30280Sstevel@tonic-gate }
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate /*
30310Sstevel@tonic-gate  * Close and re-open lock file every time so that it is recreated if deleted.
30320Sstevel@tonic-gate  */
30330Sstevel@tonic-gate static void
30340Sstevel@tonic-gate exit_update_lock(struct di_devlink_handle *hdp)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate 	struct flock unlock;
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	if (hdp->lock_fd < 0) {
30390Sstevel@tonic-gate 		return;
30400Sstevel@tonic-gate 	}
30410Sstevel@tonic-gate 
30420Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
30430Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
30440Sstevel@tonic-gate 	unlock.l_start = 0;
30450Sstevel@tonic-gate 	unlock.l_len = 0;
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	if (fcntl(hdp->lock_fd, F_SETLK, &unlock) == -1) {
30480Sstevel@tonic-gate 		dprintf(DBG_ERR, "update lockfile: unlock failed: %s\n",
30490Sstevel@tonic-gate 		    strerror(errno));
30500Sstevel@tonic-gate 	}
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 	(void) close(hdp->lock_fd);
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	hdp->lock_fd = -1;
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	(void) mutex_unlock(&update_mutex);
30570Sstevel@tonic-gate }
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate /*
30600Sstevel@tonic-gate  * returns 1 if contents is a minor node in /devices.
30610Sstevel@tonic-gate  * If mn_root is not NULL, mn_root is set to:
30620Sstevel@tonic-gate  *	if contents is a /dev node, mn_root = contents
3063*2621Sllai1  *			OR
30640Sstevel@tonic-gate  *	if contents is a /devices node, mn_root set to the '/'
30650Sstevel@tonic-gate  *	following /devices.
30660Sstevel@tonic-gate  */
30670Sstevel@tonic-gate int
30680Sstevel@tonic-gate is_minor_node(const char *contents, const char **mn_root)
30690Sstevel@tonic-gate {
30700Sstevel@tonic-gate 	char *ptr, *prefix;
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	prefix = "../devices/";
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	if ((ptr = strstr(contents, prefix)) != NULL) {
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 		/* mn_root should point to the / following /devices */
30770Sstevel@tonic-gate 		if (mn_root != NULL) {
30780Sstevel@tonic-gate 			*mn_root = ptr += strlen(prefix) - 1;
30790Sstevel@tonic-gate 		}
30800Sstevel@tonic-gate 		return (1);
30810Sstevel@tonic-gate 	}
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	prefix = "/devices/";
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 	if (strncmp(contents, prefix, strlen(prefix)) == 0) {
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate 		/* mn_root should point to the / following /devices/ */
30880Sstevel@tonic-gate 		if (mn_root != NULL) {
30890Sstevel@tonic-gate 			*mn_root = contents + strlen(prefix) - 1;
30900Sstevel@tonic-gate 		}
30910Sstevel@tonic-gate 		return (1);
30920Sstevel@tonic-gate 	}
30930Sstevel@tonic-gate 
30940Sstevel@tonic-gate 	if (mn_root != NULL) {
30950Sstevel@tonic-gate 		*mn_root = contents;
30960Sstevel@tonic-gate 	}
30970Sstevel@tonic-gate 	return (0);
30980Sstevel@tonic-gate }
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate static int
31010Sstevel@tonic-gate s_readlink(const char *link, char *buf, size_t blen)
31020Sstevel@tonic-gate {
31030Sstevel@tonic-gate 	int rv;
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate 	if ((rv = readlink(link, buf, blen)) == -1)
31060Sstevel@tonic-gate 		goto bad;
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate 	if (rv >= blen && buf[blen - 1] != '\0') {
31090Sstevel@tonic-gate 		errno = ENAMETOOLONG;
31100Sstevel@tonic-gate 		goto bad;
31110Sstevel@tonic-gate 	} else if (rv < blen) {
31120Sstevel@tonic-gate 		buf[rv] = '\0';
31130Sstevel@tonic-gate 	}
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	return (0);
31160Sstevel@tonic-gate bad:
31170Sstevel@tonic-gate 	dprintf(DBG_ERR, "s_readlink: %s: failed: %s\n",
31180Sstevel@tonic-gate 	    link, strerror(errno));
31190Sstevel@tonic-gate 	return (-1);
31200Sstevel@tonic-gate }
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate /*
31230Sstevel@tonic-gate  * Synchronous link creation interface routines
31240Sstevel@tonic-gate  * The scope of the operation is determined by the "name" arg.
31250Sstevel@tonic-gate  * "name" can be NULL, a driver name or a devfs pathname (without /devices)
31260Sstevel@tonic-gate  *
31270Sstevel@tonic-gate  *	"name"				creates
31280Sstevel@tonic-gate  *	======				=======
31290Sstevel@tonic-gate  *
31300Sstevel@tonic-gate  *	NULL		=>		All devlinks in system
31310Sstevel@tonic-gate  *	<driver>	=>		devlinks for named driver
31320Sstevel@tonic-gate  *	/pci@1		=>		devlinks for subtree rooted at pci@1
31330Sstevel@tonic-gate  *	/pseudo/foo@0:X	=>		devlinks for minor X
31340Sstevel@tonic-gate  *
31350Sstevel@tonic-gate  * devlink_create() returns 0 on success or an errno value on failure
31360Sstevel@tonic-gate  */
31370Sstevel@tonic-gate 
31380Sstevel@tonic-gate #define	MAX_DAEMON_ATTEMPTS 2
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate static int
31410Sstevel@tonic-gate devlink_create(const char *root, const char *name)
31420Sstevel@tonic-gate {
31430Sstevel@tonic-gate 	int i;
31440Sstevel@tonic-gate 	struct dca_off dca;
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 	assert(root);
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 	/*
31490Sstevel@tonic-gate 	 * Convert name into arg for door_call
31500Sstevel@tonic-gate 	 */
31510Sstevel@tonic-gate 	if (dca_init(name, &dca) != 0)
31520Sstevel@tonic-gate 		return (EINVAL);
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	/*
31550Sstevel@tonic-gate 	 * Attempt to use the daemon first
31560Sstevel@tonic-gate 	 */
31570Sstevel@tonic-gate 	i = 0;
31580Sstevel@tonic-gate 	do {
31590Sstevel@tonic-gate 		daemon_call(root, &dca);
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 		dprintf(DBG_INFO, "daemon_call() retval=%d\n", dca.dca_error);
31620Sstevel@tonic-gate 
31630Sstevel@tonic-gate 		/*
31640Sstevel@tonic-gate 		 * Retry only if door server isn't running
31650Sstevel@tonic-gate 		 */
31660Sstevel@tonic-gate 		if (dca.dca_error != ENOENT && dca.dca_error != EBADF) {
31670Sstevel@tonic-gate 			return (dca.dca_error);
31680Sstevel@tonic-gate 		}
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 		dca.dca_error = 0;
31710Sstevel@tonic-gate 
31720Sstevel@tonic-gate 		/*
31730Sstevel@tonic-gate 		 * To improve performance defer this check until the first
31740Sstevel@tonic-gate 		 * failure. Safe to defer as door server checks perms.
31750Sstevel@tonic-gate 		 */
31760Sstevel@tonic-gate 		if (geteuid() != 0)
31770Sstevel@tonic-gate 			return (EPERM);
31780Sstevel@tonic-gate 	/*
31790Sstevel@tonic-gate 	 * Daemon may not be running. Try to start it.
31800Sstevel@tonic-gate 	 */
31810Sstevel@tonic-gate 	} while ((++i < MAX_DAEMON_ATTEMPTS) && start_daemon(root) == 0);
31820Sstevel@tonic-gate 
31830Sstevel@tonic-gate 	dprintf(DBG_INFO, "devlink_create: can't start daemon\n");
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 	assert(dca.dca_error == 0);
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	/*
31880Sstevel@tonic-gate 	 * If the daemon cannot be started execute the devfsadm command.
31890Sstevel@tonic-gate 	 */
31900Sstevel@tonic-gate 	exec_cmd(root, &dca);
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	return (dca.dca_error);
31930Sstevel@tonic-gate }
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate /*
31960Sstevel@tonic-gate  * The "name" member of "struct dca" contains data in the following order
31970Sstevel@tonic-gate  *	root'\0'minor'\0'driver'\0'
31980Sstevel@tonic-gate  * The root component is always present at offset 0 in the "name" field.
31990Sstevel@tonic-gate  * The driver and minor are optional. If present they have a non-zero
32000Sstevel@tonic-gate  * offset in the "name" member.
32010Sstevel@tonic-gate  */
32020Sstevel@tonic-gate static int
32030Sstevel@tonic-gate dca_init(const char *name, struct dca_off *dcp)
32040Sstevel@tonic-gate {
32050Sstevel@tonic-gate 	char *cp;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	dcp->dca_root = 0;
32080Sstevel@tonic-gate 	dcp->dca_minor = 0;
32090Sstevel@tonic-gate 	dcp->dca_driver = 0;
32100Sstevel@tonic-gate 	dcp->dca_error = 0;
32110Sstevel@tonic-gate 	dcp->dca_flags = 0;
32120Sstevel@tonic-gate 	dcp->dca_name[0] = '\0';
32130Sstevel@tonic-gate 
32140Sstevel@tonic-gate 	name = name ? name : "/";
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	/*
32170Sstevel@tonic-gate 	 *  Check if name is a driver name
32180Sstevel@tonic-gate 	 */
32190Sstevel@tonic-gate 	if (*name != '/') {
32200Sstevel@tonic-gate 		(void) snprintf(dcp->dca_name, sizeof (dcp->dca_name),
32210Sstevel@tonic-gate 		    "/ %s", name);
32220Sstevel@tonic-gate 		dcp->dca_root = 0;
32230Sstevel@tonic-gate 		*(dcp->dca_name + 1) = '\0';
32240Sstevel@tonic-gate 		dcp->dca_driver = 2;
32250Sstevel@tonic-gate 		return (0);
32260Sstevel@tonic-gate 	}
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	(void) snprintf(dcp->dca_name, sizeof (dcp->dca_name), "%s", name);
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	/*
32310Sstevel@tonic-gate 	 * "/devices" not allowed in devfs pathname
32320Sstevel@tonic-gate 	 */
32330Sstevel@tonic-gate 	if (is_minor_node(name, NULL))
32340Sstevel@tonic-gate 		return (-1);
32350Sstevel@tonic-gate 
32360Sstevel@tonic-gate 	dcp->dca_root = 0;
32370Sstevel@tonic-gate 	if (cp = strrchr(dcp->dca_name, ':')) {
32380Sstevel@tonic-gate 		*cp++ = '\0';
32390Sstevel@tonic-gate 		dcp->dca_minor = cp - dcp->dca_name;
32400Sstevel@tonic-gate 	}
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	return (0);
32430Sstevel@tonic-gate }
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate #define	DAEMON_STARTUP_TIME	1 /* 1 second. This may need to be adjusted */
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate static void
32490Sstevel@tonic-gate daemon_call(const char *root, struct dca_off *dcp)
32500Sstevel@tonic-gate {
32510Sstevel@tonic-gate 	door_arg_t	arg;
32520Sstevel@tonic-gate 	int		fd, door_error;
32530Sstevel@tonic-gate 	sigset_t	oset, nset;
32540Sstevel@tonic-gate 	char		synch_door[PATH_MAX];
3255*2621Sllai1 	struct statvfs	svf;
3256*2621Sllai1 	char		*prefix;
3257*2621Sllai1 
3258*2621Sllai1 	/*
3259*2621Sllai1 	 * If readonly root, assume we are in install
3260*2621Sllai1 	 */
3261*2621Sllai1 	prefix =
3262*2621Sllai1 	    (statvfs("/etc/dev", &svf) == 0 && (svf.f_flag & ST_RDONLY)) ?
3263*2621Sllai1 		"/tmp" : (char *)root;
32640Sstevel@tonic-gate 	(void) snprintf(synch_door, sizeof (synch_door),
3265*2621Sllai1 	    "%s/etc/dev/%s", prefix, DEVFSADM_SYNCH_DOOR);
32660Sstevel@tonic-gate 
32670Sstevel@tonic-gate 	if ((fd = open(synch_door, O_RDONLY)) == -1) {
32680Sstevel@tonic-gate 		dcp->dca_error = errno;
32690Sstevel@tonic-gate 		dprintf(DBG_ERR, "open of %s failed: %s\n",
32700Sstevel@tonic-gate 		    synch_door, strerror(errno));
32710Sstevel@tonic-gate 		return;
32720Sstevel@tonic-gate 	}
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 	arg.data_ptr = (char *)dcp;
32750Sstevel@tonic-gate 	arg.data_size = sizeof (*dcp);
32760Sstevel@tonic-gate 	arg.desc_ptr = NULL;
32770Sstevel@tonic-gate 	arg.desc_num = 0;
32780Sstevel@tonic-gate 	arg.rbuf = (char *)dcp;
32790Sstevel@tonic-gate 	arg.rsize = sizeof (*dcp);
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 	/*
32820Sstevel@tonic-gate 	 * Block signals to this thread until door call
32830Sstevel@tonic-gate 	 * completes.
32840Sstevel@tonic-gate 	 */
32850Sstevel@tonic-gate 	(void) sigfillset(&nset);
32860Sstevel@tonic-gate 	(void) sigemptyset(&oset);
32870Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
32880Sstevel@tonic-gate 	if (door_call(fd, &arg)) {
32890Sstevel@tonic-gate 		door_error = 1;
32900Sstevel@tonic-gate 		dcp->dca_error = errno;
32910Sstevel@tonic-gate 	}
32920Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 	(void) close(fd);
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	if (door_error)
32970Sstevel@tonic-gate 		return;
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 	assert(arg.data_ptr);
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 	/*LINTED*/
33020Sstevel@tonic-gate 	dcp->dca_error = ((struct dca_off *)arg.data_ptr)->dca_error;
33030Sstevel@tonic-gate 
33040Sstevel@tonic-gate 	/*
33050Sstevel@tonic-gate 	 * The doors interface may return data in a different buffer
33060Sstevel@tonic-gate 	 * If that happens, deallocate buffer via munmap()
33070Sstevel@tonic-gate 	 */
33080Sstevel@tonic-gate 	if (arg.rbuf != (char *)dcp)
33090Sstevel@tonic-gate 		(void) munmap(arg.rbuf, arg.rsize);
33100Sstevel@tonic-gate }
33110Sstevel@tonic-gate 
33120Sstevel@tonic-gate #define	DEVFSADM_PATH	"/usr/sbin/devfsadm"
33130Sstevel@tonic-gate #define	DEVFSADM	"devfsadm"
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate #define	DEVFSADMD_PATH	"/usr/lib/devfsadm/devfsadmd"
33160Sstevel@tonic-gate #define	DEVFSADM_DAEMON	"devfsadmd"
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate static int
33190Sstevel@tonic-gate start_daemon(const char *root)
33200Sstevel@tonic-gate {
33210Sstevel@tonic-gate 	int rv, i = 0;
33220Sstevel@tonic-gate 	char *argv[20];
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate 	argv[i++] = DEVFSADM_DAEMON;
33250Sstevel@tonic-gate 	if (strcmp(root, "/")) {
33260Sstevel@tonic-gate 		argv[i++] = "-r";
33270Sstevel@tonic-gate 		argv[i++] = (char *)root;
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 	argv[i++] = NULL;
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 	rv = do_exec(DEVFSADMD_PATH, argv);
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate 	(void) sleep(DAEMON_STARTUP_TIME);
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	return (rv);
33360Sstevel@tonic-gate }
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate static void
33390Sstevel@tonic-gate exec_cmd(const char *root, struct dca_off *dcp)
33400Sstevel@tonic-gate {
33410Sstevel@tonic-gate 	int i;
33420Sstevel@tonic-gate 	char *argv[20];
33430Sstevel@tonic-gate 
33440Sstevel@tonic-gate 	i = 0;
33450Sstevel@tonic-gate 	argv[i++] = DEVFSADM;
33460Sstevel@tonic-gate 
33470Sstevel@tonic-gate 	/*
33480Sstevel@tonic-gate 	 * Load drivers only if -i is specified
33490Sstevel@tonic-gate 	 */
33500Sstevel@tonic-gate 	if (dcp->dca_driver) {
33510Sstevel@tonic-gate 		argv[i++] = "-i";
33520Sstevel@tonic-gate 		argv[i++] = &dcp->dca_name[dcp->dca_driver];
33530Sstevel@tonic-gate 	} else {
33540Sstevel@tonic-gate 		argv[i++] = "-n";
33550Sstevel@tonic-gate 	}
33560Sstevel@tonic-gate 
33570Sstevel@tonic-gate 	if (root != NULL && strcmp(root, "/") != 0) {
33580Sstevel@tonic-gate 		argv[i++] = "-r";
33590Sstevel@tonic-gate 		argv[i++] = (char *)root;
33600Sstevel@tonic-gate 	}
33610Sstevel@tonic-gate 
33620Sstevel@tonic-gate 	argv[i] = NULL;
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 	if (do_exec(DEVFSADM_PATH, argv))
33650Sstevel@tonic-gate 		dcp->dca_error = errno;
33660Sstevel@tonic-gate }
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate static int
33690Sstevel@tonic-gate do_exec(const char *path, char *const argv[])
33700Sstevel@tonic-gate {
33710Sstevel@tonic-gate 	int i;
33720Sstevel@tonic-gate 	pid_t cpid;
33730Sstevel@tonic-gate 
33740Sstevel@tonic-gate #ifdef	DEBUG
33750Sstevel@tonic-gate 	dprintf(DBG_INFO, "Executing %s\n\tArgument list:", path);
33760Sstevel@tonic-gate 	for (i = 0; argv[i] != NULL; i++) {
33770Sstevel@tonic-gate 		dprintf(DBG_INFO, " %s", argv[i]);
33780Sstevel@tonic-gate 	}
33790Sstevel@tonic-gate 	dprintf(DBG_INFO, "\n");
33800Sstevel@tonic-gate #endif
33810Sstevel@tonic-gate 
33820Sstevel@tonic-gate 	if ((cpid = fork1()) == -1) {
33830Sstevel@tonic-gate 		dprintf(DBG_ERR, "fork1 failed: %s\n", strerror(errno));
33840Sstevel@tonic-gate 		return (-1);
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	if (cpid == 0) { /* child process */
33880Sstevel@tonic-gate 		int fd;
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 		if ((fd = open("/dev/null", O_RDWR)) >= 0) {
33910Sstevel@tonic-gate 			(void) dup2(fd, fileno(stdout));
33920Sstevel@tonic-gate 			(void) dup2(fd, fileno(stderr));
33930Sstevel@tonic-gate 			(void) close(fd);
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 			(void) execv(path, argv);
33960Sstevel@tonic-gate 		} else {
33970Sstevel@tonic-gate 			dprintf(DBG_ERR, "open of /dev/null failed: %s\n",
33980Sstevel@tonic-gate 			    strerror(errno));
33990Sstevel@tonic-gate 		}
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 		_exit(-1);
34020Sstevel@tonic-gate 	}
34030Sstevel@tonic-gate 
34040Sstevel@tonic-gate 	/* Parent process */
34050Sstevel@tonic-gate 	if (waitpid(cpid, &i, 0) == cpid) {
34060Sstevel@tonic-gate 		if (WIFEXITED(i)) {
34070Sstevel@tonic-gate 			if (WEXITSTATUS(i) == 0) {
34080Sstevel@tonic-gate 				dprintf(DBG_STEP,
34090Sstevel@tonic-gate 				    "do_exec: child exited normally\n");
34100Sstevel@tonic-gate 				return (0);
34110Sstevel@tonic-gate 			} else
34120Sstevel@tonic-gate 				errno = EINVAL;
34130Sstevel@tonic-gate 		} else {
34140Sstevel@tonic-gate 			/*
34150Sstevel@tonic-gate 			 * The child was interrupted by a signal
34160Sstevel@tonic-gate 			 */
34170Sstevel@tonic-gate 			errno = EINTR;
34180Sstevel@tonic-gate 		}
34190Sstevel@tonic-gate 		dprintf(DBG_ERR, "child terminated abnormally: %s\n",
34200Sstevel@tonic-gate 		    strerror(errno));
34210Sstevel@tonic-gate 	} else {
34220Sstevel@tonic-gate 		dprintf(DBG_ERR, "waitpid failed: %s\n", strerror(errno));
34230Sstevel@tonic-gate 	}
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	return (-1);
34260Sstevel@tonic-gate }
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate static int
34290Sstevel@tonic-gate walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp, link_desc_t *linkp)
34300Sstevel@tonic-gate {
34310Sstevel@tonic-gate 	int i;
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate 	assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 	dprintf(DBG_INFO, "walk_cache_links: initial link: %s\n",
34360Sstevel@tonic-gate 	    clp ? clp->path : "<NULL>");
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	/*
34390Sstevel@tonic-gate 	 * First search the links under the specified minor. On the
34400Sstevel@tonic-gate 	 * 2nd pass, search the dangling list - secondary links may
34410Sstevel@tonic-gate 	 * exist on this list since they are not resolved during the
34420Sstevel@tonic-gate 	 * /dev walk.
34430Sstevel@tonic-gate 	 */
34440Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
34450Sstevel@tonic-gate 		for (; clp != NULL; clp = clp->sib) {
34460Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
34470Sstevel@tonic-gate 
34480Sstevel@tonic-gate 			assert(clp->path[0] != '/');
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 			vlink.rel_path = clp->path;
34510Sstevel@tonic-gate 			vlink.content = clp->content;
34520Sstevel@tonic-gate 			vlink.type = attr2type(clp->attr);
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink)
34550Sstevel@tonic-gate 			    != DI_WALK_CONTINUE) {
34560Sstevel@tonic-gate 				dprintf(DBG_INFO, "walk_cache_links: "
34570Sstevel@tonic-gate 				    "terminating at link: %s\n", clp->path);
34580Sstevel@tonic-gate 				goto out;
34590Sstevel@tonic-gate 			}
34600Sstevel@tonic-gate 		}
34610Sstevel@tonic-gate 
34620Sstevel@tonic-gate 		clp = CACHE(hdp)->dngl;
34630Sstevel@tonic-gate 	}
34640Sstevel@tonic-gate 
34650Sstevel@tonic-gate out:
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate 	/* If i < 2, we terminated the walk prematurely */
34680Sstevel@tonic-gate 	return (i < 2 ? DI_WALK_TERMINATE : DI_WALK_CONTINUE);
34690Sstevel@tonic-gate }
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate static void
34720Sstevel@tonic-gate walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp)
34730Sstevel@tonic-gate {
34740Sstevel@tonic-gate 	int i;
34750Sstevel@tonic-gate 	cache_link_t *clp;
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	dprintf(DBG_INFO, "walk_all_cache: entered\n");
34780Sstevel@tonic-gate 
34790Sstevel@tonic-gate 	for (i = 0; i < CACHE(hdp)->hash_sz; i++) {
34800Sstevel@tonic-gate 		clp = CACHE_HASH(hdp, i);
34810Sstevel@tonic-gate 		for (; clp; clp = clp->hash) {
34820Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 			assert(clp->path[0] != '/');
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 			vlink.rel_path = clp->path;
34870Sstevel@tonic-gate 			vlink.content = clp->content;
34880Sstevel@tonic-gate 			vlink.type = attr2type(clp->attr);
34890Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink) !=
34900Sstevel@tonic-gate 			    DI_WALK_CONTINUE) {
34910Sstevel@tonic-gate 				dprintf(DBG_INFO, "walk_all_cache: terminating "
34920Sstevel@tonic-gate 				    "walk at link: %s\n", clp->path);
34930Sstevel@tonic-gate 				return;
34940Sstevel@tonic-gate 			}
34950Sstevel@tonic-gate 		}
34960Sstevel@tonic-gate 	}
34970Sstevel@tonic-gate }
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate static void
35000Sstevel@tonic-gate walk_cache_minor(di_devlink_handle_t hdp, const char *mpath, link_desc_t *linkp)
35010Sstevel@tonic-gate {
35020Sstevel@tonic-gate 	cache_minor_t *cmnp;
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate 	assert(mpath);
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	if ((cmnp = lookup_minor(hdp, mpath, NULL, TYPE_CACHE)) != NULL) {
35070Sstevel@tonic-gate 		(void) walk_cache_links(hdp, cmnp->link, linkp);
35080Sstevel@tonic-gate 	} else {
35090Sstevel@tonic-gate 		dprintf(DBG_ERR, "lookup minor failed: %s\n", mpath);
35100Sstevel@tonic-gate 	}
35110Sstevel@tonic-gate }
35120Sstevel@tonic-gate 
35130Sstevel@tonic-gate static void
35140Sstevel@tonic-gate walk_cache_node(di_devlink_handle_t hdp, const char *path, link_desc_t *linkp)
35150Sstevel@tonic-gate {
35160Sstevel@tonic-gate 	cache_minor_t *cmnp;
35170Sstevel@tonic-gate 	cache_node_t *cnp;
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 	assert(path);
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	if ((cnp = lookup_node(hdp, (char *)path, TYPE_CACHE)) == NULL) {
35220Sstevel@tonic-gate 		dprintf(DBG_ERR, "lookup node failed: %s\n", path);
35230Sstevel@tonic-gate 		return;
35240Sstevel@tonic-gate 	}
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 	for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) {
35270Sstevel@tonic-gate 		if (walk_cache_links(hdp, cmnp->link, linkp)
35280Sstevel@tonic-gate 		    == DI_WALK_TERMINATE)
35290Sstevel@tonic-gate 			break;
35300Sstevel@tonic-gate 	}
35310Sstevel@tonic-gate }
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate /*
35340Sstevel@tonic-gate  * Private function
35350Sstevel@tonic-gate  *
35360Sstevel@tonic-gate  * Walk cached links corresponding to the given path.
35370Sstevel@tonic-gate  *
35380Sstevel@tonic-gate  * path		path to a node or minor node.
35390Sstevel@tonic-gate  *
35400Sstevel@tonic-gate  * flags	specifies the type of devlinks to be selected.
35410Sstevel@tonic-gate  *		If DI_PRIMARY_LINK is used, only primary links are selected.
35420Sstevel@tonic-gate  *		If DI_SECONDARY_LINK is specified, only secondary links
35430Sstevel@tonic-gate  *		are selected.
35440Sstevel@tonic-gate  *		If neither flag is specified, all devlinks are selected.
35450Sstevel@tonic-gate  *
35460Sstevel@tonic-gate  * re		An extended regular expression in regex(5) format which
35470Sstevel@tonic-gate  *		selects the /dev links to be returned. The regular
35480Sstevel@tonic-gate  *		expression should use link pathnames relative to
35490Sstevel@tonic-gate  *		/dev. i.e. without the leading "/dev/" prefix.
35500Sstevel@tonic-gate  *		A NULL value matches all devlinks.
35510Sstevel@tonic-gate  */
35520Sstevel@tonic-gate int
35530Sstevel@tonic-gate di_devlink_cache_walk(di_devlink_handle_t hdp,
35540Sstevel@tonic-gate 	const char *re,
35550Sstevel@tonic-gate 	const char *path,
35560Sstevel@tonic-gate 	uint_t flags,
35570Sstevel@tonic-gate 	void *arg,
35580Sstevel@tonic-gate 	int (*devlink_callback)(di_devlink_t, void *))
35590Sstevel@tonic-gate {
35600Sstevel@tonic-gate 	regex_t reg;
35610Sstevel@tonic-gate 	link_desc_t linkd = {NULL};
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	if (hdp == NULL || path == NULL || !link_flag(flags) ||
35640Sstevel@tonic-gate 	    !HDL_RDWR(hdp) || devlink_callback == NULL) {
35650Sstevel@tonic-gate 		errno = EINVAL;
35660Sstevel@tonic-gate 		return (-1);
35670Sstevel@tonic-gate 	}
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 	linkd.flags = flags;
35700Sstevel@tonic-gate 	linkd.arg = arg;
35710Sstevel@tonic-gate 	linkd.fcn = devlink_callback;
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 	if (re) {
35740Sstevel@tonic-gate 		if (regcomp(&reg, re, REG_EXTENDED) != 0)
35750Sstevel@tonic-gate 			return (-1);
35760Sstevel@tonic-gate 		linkd.regp = &reg;
35770Sstevel@tonic-gate 	}
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	if (minor_colon(path) == NULL) {
35800Sstevel@tonic-gate 		walk_cache_node(hdp, path, &linkd);
35810Sstevel@tonic-gate 	} else {
35820Sstevel@tonic-gate 		walk_cache_minor(hdp, path, &linkd);
35830Sstevel@tonic-gate 	}
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	if (re)
35860Sstevel@tonic-gate 		regfree(&reg);
35870Sstevel@tonic-gate 
35880Sstevel@tonic-gate 	return (0);
35890Sstevel@tonic-gate }
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate #define	DEBUG_ENV_VAR	"_DEVLINK_DEBUG"
35920Sstevel@tonic-gate static int _devlink_debug = -1;
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate /*
35950Sstevel@tonic-gate  * debug level is initialized to -1.
35960Sstevel@tonic-gate  * On first call into this routine, debug level is set.
35970Sstevel@tonic-gate  * If debug level is zero, debugging msgs are disabled.
35980Sstevel@tonic-gate  */
35990Sstevel@tonic-gate static void
36000Sstevel@tonic-gate debug_print(debug_level_t msglevel, const char *fmt, va_list ap)
36010Sstevel@tonic-gate {
36020Sstevel@tonic-gate 	char	*cp;
36030Sstevel@tonic-gate 	int	save;
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	/*
36060Sstevel@tonic-gate 	 * We shouldn't be here if debug is disabled
36070Sstevel@tonic-gate 	 */
36080Sstevel@tonic-gate 	assert(_devlink_debug != 0);
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	/*
36110Sstevel@tonic-gate 	 * Set debug level on first call into this routine
36120Sstevel@tonic-gate 	 */
36130Sstevel@tonic-gate 	if (_devlink_debug < 0) {
36140Sstevel@tonic-gate 		if ((cp = getenv(DEBUG_ENV_VAR)) == NULL) {
36150Sstevel@tonic-gate 			_devlink_debug = 0;
36160Sstevel@tonic-gate 			return;
36170Sstevel@tonic-gate 		}
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 		save = errno;
36200Sstevel@tonic-gate 		errno = 0;
36210Sstevel@tonic-gate 		_devlink_debug = strtol(cp, NULL, 10);
36220Sstevel@tonic-gate 		if (errno != 0 || _devlink_debug < 0)  {
36230Sstevel@tonic-gate 			_devlink_debug = 0;
36240Sstevel@tonic-gate 			errno = save;
36250Sstevel@tonic-gate 			return;
36260Sstevel@tonic-gate 		}
36270Sstevel@tonic-gate 		errno = save;
36280Sstevel@tonic-gate 
36290Sstevel@tonic-gate 		if (!_devlink_debug)
36300Sstevel@tonic-gate 			return;
36310Sstevel@tonic-gate 	}
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	/* debug msgs are enabled */
36340Sstevel@tonic-gate 	assert(_devlink_debug > 0);
36350Sstevel@tonic-gate 
36360Sstevel@tonic-gate 	if (_devlink_debug < msglevel)
36370Sstevel@tonic-gate 		return;
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 	/* Print a distinctive label for error msgs */
36410Sstevel@tonic-gate 	if (msglevel == DBG_ERR) {
36420Sstevel@tonic-gate 		(void) fprintf(stderr, "[ERROR]: ");
36430Sstevel@tonic-gate 	}
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
36460Sstevel@tonic-gate }
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate /* ARGSUSED */
36490Sstevel@tonic-gate /* PRINTFLIKE2 */
36500Sstevel@tonic-gate void
36510Sstevel@tonic-gate dprintf(debug_level_t msglevel, const char *fmt, ...)
36520Sstevel@tonic-gate {
36530Sstevel@tonic-gate 	va_list ap;
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate 	assert(msglevel > 0);
36560Sstevel@tonic-gate 
36570Sstevel@tonic-gate 	if (!_devlink_debug)
36580Sstevel@tonic-gate 		return;
36590Sstevel@tonic-gate 
36600Sstevel@tonic-gate 	va_start(ap, fmt);
36610Sstevel@tonic-gate 	debug_print(msglevel, fmt, ap);
36620Sstevel@tonic-gate 	va_end(ap);
36630Sstevel@tonic-gate }
3664