17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5facf4a8dSllai  * Common Development and Distribution License (the "License").
6facf4a8dSllai  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22facf4a8dSllai  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
28facf4a8dSllai #include "libdevinfo.h"
297c478bd9Sstevel@tonic-gate #include "devinfo_devlink.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #undef DEBUG
327c478bd9Sstevel@tonic-gate #ifndef DEBUG
337c478bd9Sstevel@tonic-gate #define	NDEBUG 1
347c478bd9Sstevel@tonic-gate #else
357c478bd9Sstevel@tonic-gate #undef	NDEBUG
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <assert.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static mutex_t update_mutex = DEFAULTMUTEX; /* Protects update record lock */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static const size_t elem_sizes[DB_TYPES] = {
437c478bd9Sstevel@tonic-gate 	sizeof (struct db_node),
447c478bd9Sstevel@tonic-gate 	sizeof (struct db_minor),
457c478bd9Sstevel@tonic-gate 	sizeof (struct db_link),
467c478bd9Sstevel@tonic-gate 	sizeof (char)
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * List of directories/files skipped while physically walking /dev
517c478bd9Sstevel@tonic-gate  * Paths are relative to "<root>/dev/"
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static const char *skip_dirs[] = {"fd"};
547c478bd9Sstevel@tonic-gate static const char *skip_files[] = {
557c478bd9Sstevel@tonic-gate 	"stdout",
567c478bd9Sstevel@tonic-gate 	"stdin",
577c478bd9Sstevel@tonic-gate 	"stderr"
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	N_SKIP_DIRS	(sizeof (skip_dirs) / sizeof (skip_dirs[0]))
617c478bd9Sstevel@tonic-gate #define	N_SKIP_FILES	(sizeof (skip_files) / sizeof (skip_files[0]))
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * This file contains two sets of interfaces which operate on the reverse
667c478bd9Sstevel@tonic-gate  * links database. One set (which includes di_devlink_open()/_close())
677c478bd9Sstevel@tonic-gate  * allows link generators like devfsadm(1M) and ucblinks(1B) (writers) to
687c478bd9Sstevel@tonic-gate  * populate the database with /devices -> /dev mappings. Another set
697c478bd9Sstevel@tonic-gate  * of interfaces (which includes di_devlink_init()/_fini()) allows
707c478bd9Sstevel@tonic-gate  * applications (readers) to lookup the database for /dev links corresponding
717c478bd9Sstevel@tonic-gate  * to a given minor.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * Writers operate on a cached version of the database. The cache is created
747c478bd9Sstevel@tonic-gate  * when di_devlink_open() is called. As links in /dev are created and removed,
757c478bd9Sstevel@tonic-gate  * the cache is updated to keep it in synch with /dev. When the /dev updates
767c478bd9Sstevel@tonic-gate  * are complete, the link generator calls di_devlink_close() which writes
777c478bd9Sstevel@tonic-gate  * out the cache to the database.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * Applications which need to lookup the database, call di_devlink_init().
807c478bd9Sstevel@tonic-gate  * di_devlink_init() checks the database file (if one exists). If the
817c478bd9Sstevel@tonic-gate  * database is valid, it is mapped into the address space of the
827c478bd9Sstevel@tonic-gate  * application. The database file consists of several segments. Each
837c478bd9Sstevel@tonic-gate  * segment can be mapped in independently and is mapped on demand.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  *		   Database Layout
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *		---------------------
887c478bd9Sstevel@tonic-gate  *		|	Magic #     |
897c478bd9Sstevel@tonic-gate  *		| ----------------- |
907c478bd9Sstevel@tonic-gate  *		|       Version	    |	HEADER
917c478bd9Sstevel@tonic-gate  *		| ----------------- |
927c478bd9Sstevel@tonic-gate  *		|        ...        |
937c478bd9Sstevel@tonic-gate  *		---------------------
947c478bd9Sstevel@tonic-gate  *		|		    |
957c478bd9Sstevel@tonic-gate  *		|		    |	NODES
967c478bd9Sstevel@tonic-gate  *		|	            |
977c478bd9Sstevel@tonic-gate  *		|		    |
987c478bd9Sstevel@tonic-gate  *		---------------------
997c478bd9Sstevel@tonic-gate  *		|		    |
1007c478bd9Sstevel@tonic-gate  *		|		    |	MINORS
1017c478bd9Sstevel@tonic-gate  *		|	            |
1027c478bd9Sstevel@tonic-gate  *		|		    |
1037c478bd9Sstevel@tonic-gate  *		---------------------
1047c478bd9Sstevel@tonic-gate  *		|		    |
1057c478bd9Sstevel@tonic-gate  *		|		    |   LINKS
1067c478bd9Sstevel@tonic-gate  *		|	            |
1077c478bd9Sstevel@tonic-gate  *		|		    |
1087c478bd9Sstevel@tonic-gate  *		---------------------
1097c478bd9Sstevel@tonic-gate  *		|		    |
1107c478bd9Sstevel@tonic-gate  *		|		    |	STRINGS
1117c478bd9Sstevel@tonic-gate  *		|	            |
1127c478bd9Sstevel@tonic-gate  *		|		    |
1137c478bd9Sstevel@tonic-gate  *		---------------------
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * Readers can lookup /dev links for a specific minor or
1167c478bd9Sstevel@tonic-gate  * lookup all /dev links. In the latter case, the node
1177c478bd9Sstevel@tonic-gate  * and minor segments are not mapped in and the reader
1187c478bd9Sstevel@tonic-gate  * walks through every link in the link segment.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate di_devlink_handle_t
1237c478bd9Sstevel@tonic-gate di_devlink_open(const char *root_dir, uint_t flags)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	int err;
1267c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
1277c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp;
1287c478bd9Sstevel@tonic-gate 	int retried = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate retry:
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Allocate a read-write handle but open the DB in readonly
1337c478bd9Sstevel@tonic-gate 	 * mode. We do writes only to a temporary copy of the database.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if ((hdp = handle_alloc(root_dir, OPEN_RDWR)) == NULL) {
1367c478bd9Sstevel@tonic-gate 		return (NULL);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	err = open_db(hdp, OPEN_RDONLY);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * Unlink the database, so that consumers don't get
1437c478bd9Sstevel@tonic-gate 	 * out of date information as the database is being updated.
1447c478bd9Sstevel@tonic-gate 	 */
1457c478bd9Sstevel@tonic-gate 	get_db_path(hdp, DB_FILE, path, sizeof (path));
1467c478bd9Sstevel@tonic-gate 	(void) unlink(path);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * The flags argument is reserved for future use.
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 	if (flags != 0) {
1527c478bd9Sstevel@tonic-gate 		handle_free(&hdp); /* also closes the DB */
1537c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1547c478bd9Sstevel@tonic-gate 		return (NULL);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (cache_alloc(hdp) != 0) {
1587c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
1597c478bd9Sstevel@tonic-gate 		return (NULL);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (err) {
1637c478bd9Sstevel@tonic-gate 		/*
1647c478bd9Sstevel@tonic-gate 		 * Failed to open DB.
1657c478bd9Sstevel@tonic-gate 		 * The most likely cause is that DB file did not exist.
1667c478bd9Sstevel@tonic-gate 		 * Call di_devlink_close() to recreate the DB file and
1677c478bd9Sstevel@tonic-gate 		 * retry di_devlink_open().
1687c478bd9Sstevel@tonic-gate 		 */
1697c478bd9Sstevel@tonic-gate 		if (retried == 0) {
1707c478bd9Sstevel@tonic-gate 			(void) di_devlink_close(&hdp, 0);
1717c478bd9Sstevel@tonic-gate 			retried = 1;
1727c478bd9Sstevel@tonic-gate 			goto retry;
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		/*
1767c478bd9Sstevel@tonic-gate 		 * DB cannot be opened, just return the
1777c478bd9Sstevel@tonic-gate 		 * handle. We will recreate the DB later.
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		return (hdp);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Read the database into the cache */
1837c478bd9Sstevel@tonic-gate 	CACHE(hdp)->update_count = DB_HDR(hdp)->update_count;
1847c478bd9Sstevel@tonic-gate 	(void) read_nodes(hdp, NULL, DB_HDR(hdp)->root_idx);
1857c478bd9Sstevel@tonic-gate 	(void) read_links(hdp, NULL, DB_HDR(hdp)->dngl_idx);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	(void) close_db(hdp);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (hdp);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static void
1937c478bd9Sstevel@tonic-gate get_db_path(
1947c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
1957c478bd9Sstevel@tonic-gate 	const char *fname,
1967c478bd9Sstevel@tonic-gate 	char *buf,
1977c478bd9Sstevel@tonic-gate 	size_t blen)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	char *dir = NULL;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2027c478bd9Sstevel@tonic-gate 	if (dir = getenv(ALT_DB_DIR)) {
2037c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "get_db_path: alternate db dir: %s\n",
2047c478bd9Sstevel@tonic-gate 		    dir);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate 	if (dir == NULL) {
208facf4a8dSllai 		dir = hdp->db_dir;
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, blen, "%s/%s", dir, fname);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static int
2157c478bd9Sstevel@tonic-gate open_db(struct di_devlink_handle *hdp, int flags)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	size_t sz;
2187c478bd9Sstevel@tonic-gate 	long page_sz;
2197c478bd9Sstevel@tonic-gate 	int fd, rv, flg;
2207c478bd9Sstevel@tonic-gate 	struct stat sbuf;
2217c478bd9Sstevel@tonic-gate 	uint32_t count[DB_TYPES] = {0};
2227c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
2237c478bd9Sstevel@tonic-gate 	void *cp;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	assert(!DB_OPEN(hdp));
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2287c478bd9Sstevel@tonic-gate 	if (getenv(SKIP_DB)) {
2297c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "open_db: skipping database\n");
2307c478bd9Sstevel@tonic-gate 		return (-1);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate #endif
2337c478bd9Sstevel@tonic-gate 	if ((page_sz = sysconf(_SC_PAGE_SIZE)) == -1) {
2347c478bd9Sstevel@tonic-gate 		return (-1);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Use O_TRUNC flag for write access, so that the subsequent ftruncate()
2397c478bd9Sstevel@tonic-gate 	 * call will zero-fill the entire file
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	if (IS_RDONLY(flags)) {
2427c478bd9Sstevel@tonic-gate 		flg = O_RDONLY;
2437c478bd9Sstevel@tonic-gate 		get_db_path(hdp, DB_FILE, path, sizeof (path));
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		flg = O_RDWR|O_CREAT|O_TRUNC;
2467c478bd9Sstevel@tonic-gate 		get_db_path(hdp, DB_TMP, path, sizeof (path));
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
249facf4a8dSllai 	/*
250facf4a8dSllai 	 * Avoid triggering /dev reconfigure for read when not present
251facf4a8dSllai 	 */
252facf4a8dSllai 	if (IS_RDONLY(flags) &&
253facf4a8dSllai 	    (strncmp(path, "/dev/", 5) == 0) && !device_exists(path)) {
254facf4a8dSllai 		return (-1);
255facf4a8dSllai 	}
256facf4a8dSllai 
2577c478bd9Sstevel@tonic-gate 	if ((fd = open(path, flg, DB_PERMS)) == -1) {
2587c478bd9Sstevel@tonic-gate 		return (-1);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if (IS_RDONLY(flags)) {
2627c478bd9Sstevel@tonic-gate 		flg = PROT_READ;
2637c478bd9Sstevel@tonic-gate 		rv = fstat(fd, &sbuf);
2647c478bd9Sstevel@tonic-gate 		sz = sbuf.st_size;
2657c478bd9Sstevel@tonic-gate 	} else {
2667c478bd9Sstevel@tonic-gate 		flg = PROT_READ | PROT_WRITE;
2677c478bd9Sstevel@tonic-gate 		sz = size_db(hdp, page_sz, count);
2687c478bd9Sstevel@tonic-gate 		rv = ftruncate(fd, sz);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	if (rv == -1 || sz < HDR_LEN) {
2727c478bd9Sstevel@tonic-gate 		if (rv != -1)
2737c478bd9Sstevel@tonic-gate 			errno = EINVAL;
2747c478bd9Sstevel@tonic-gate 		(void) close(fd);
2757c478bd9Sstevel@tonic-gate 		return (-1);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	cp = mmap(0, HDR_LEN, flg, MAP_SHARED, fd, 0);
2797c478bd9Sstevel@tonic-gate 	if (cp == MAP_FAILED) {
2807c478bd9Sstevel@tonic-gate 		(void) close(fd);
2817c478bd9Sstevel@tonic-gate 		return (-1);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	DB(hdp)->hdr = (struct db_hdr *)cp;
2847c478bd9Sstevel@tonic-gate 	DB(hdp)->db_fd = fd;
2857c478bd9Sstevel@tonic-gate 	DB(hdp)->flags = flags;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (IS_RDONLY(flags)) {
2887c478bd9Sstevel@tonic-gate 		rv = invalid_db(hdp, sz, page_sz);
2897c478bd9Sstevel@tonic-gate 	} else {
2907c478bd9Sstevel@tonic-gate 		rv = init_hdr(hdp, page_sz, count);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	if (rv) {
2947c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "open_db: invalid DB(%s)\n", path);
2957c478bd9Sstevel@tonic-gate 		(void) close_db(hdp);
2967c478bd9Sstevel@tonic-gate 		return (-1);
2977c478bd9Sstevel@tonic-gate 	} else {
2987c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "open_db: DB(%s): opened\n", path);
2997c478bd9Sstevel@tonic-gate 		return (0);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * A handle can be allocated for read-only or read-write access
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate static struct di_devlink_handle *
3077c478bd9Sstevel@tonic-gate handle_alloc(const char *root_dir, uint_t flags)
3087c478bd9Sstevel@tonic-gate {
309facf4a8dSllai 	char dev_dir[PATH_MAX], path[PATH_MAX], db_dir[PATH_MAX];
3107c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp, proto = {0};
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	assert(flags == OPEN_RDWR || flags == OPEN_RDONLY);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	dev_dir[0] = '\0';
315facf4a8dSllai 	db_dir[0] = '\0';
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * NULL and the empty string are equivalent to "/"
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 	if (root_dir && root_dir[0] != '\0') {
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		if (root_dir[0] != '/') {
3237c478bd9Sstevel@tonic-gate 			errno = EINVAL;
3247c478bd9Sstevel@tonic-gate 			return (NULL);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3287c478bd9Sstevel@tonic-gate 		/*LINTED*/
3297c478bd9Sstevel@tonic-gate 		assert(sizeof (dev_dir) >= PATH_MAX);
3307c478bd9Sstevel@tonic-gate #endif
331facf4a8dSllai 		if ((realpath(root_dir, dev_dir) == NULL) ||
332facf4a8dSllai 		    (realpath(root_dir, db_dir) == NULL)) {
3337c478bd9Sstevel@tonic-gate 			return (NULL);
3347c478bd9Sstevel@tonic-gate 		}
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (strcmp(dev_dir, "/") == 0) {
338facf4a8dSllai 		dev_dir[0] = 0;
339facf4a8dSllai 		db_dir[0] = 0;
3407c478bd9Sstevel@tonic-gate 	} else {
341facf4a8dSllai 		(void) strlcpy(db_dir, dev_dir, sizeof (db_dir));
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
344facf4a8dSllai 	(void) strlcat(dev_dir, DEV, sizeof (dev_dir));
345facf4a8dSllai 	(void) strlcat(db_dir, ETCDEV, sizeof (db_dir));
346facf4a8dSllai 
3477c478bd9Sstevel@tonic-gate 	proto.dev_dir = dev_dir;
348facf4a8dSllai 	proto.db_dir = db_dir;
3497c478bd9Sstevel@tonic-gate 	proto.flags = flags;
3507c478bd9Sstevel@tonic-gate 	proto.lock_fd = -1;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/*
3537c478bd9Sstevel@tonic-gate 	 * Lock database if a read-write handle is being allocated.
3547c478bd9Sstevel@tonic-gate 	 * Locks are needed to protect against multiple writers.
3557c478bd9Sstevel@tonic-gate 	 * Readers don't need locks.
3567c478bd9Sstevel@tonic-gate 	 */
3577c478bd9Sstevel@tonic-gate 	if (HDL_RDWR(&proto)) {
3587c478bd9Sstevel@tonic-gate 		if (enter_update_lock(&proto) != 0) {
3597c478bd9Sstevel@tonic-gate 			return (NULL);
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	DB(&proto)->db_fd = -1;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	hdp = calloc(1, sizeof (struct di_devlink_handle));
3667c478bd9Sstevel@tonic-gate 	if (hdp == NULL) {
3677c478bd9Sstevel@tonic-gate 		goto error;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	*hdp = proto;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * The handle hdp now contains a pointer to local storage
3747c478bd9Sstevel@tonic-gate 	 * in the dev_dir field (obtained from the proto handle).
3757c478bd9Sstevel@tonic-gate 	 * In the following line, a dynamically allocated version
3767c478bd9Sstevel@tonic-gate 	 * is substituted.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if ((hdp->dev_dir = strdup(proto.dev_dir)) == NULL) {
3807c478bd9Sstevel@tonic-gate 		free(hdp);
3817c478bd9Sstevel@tonic-gate 		goto error;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
384facf4a8dSllai 	if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) {
385facf4a8dSllai 		free(hdp->dev_dir);
386facf4a8dSllai 		free(hdp);
387facf4a8dSllai 		goto error;
388facf4a8dSllai 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (hdp);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate error:
3937c478bd9Sstevel@tonic-gate 	if (HDL_RDWR(&proto)) {
3947c478bd9Sstevel@tonic-gate 		/* Unlink DB file on error */
3957c478bd9Sstevel@tonic-gate 		get_db_path(&proto, DB_FILE, path, sizeof (path));
3967c478bd9Sstevel@tonic-gate 		(void) unlink(path);
3977c478bd9Sstevel@tonic-gate 		exit_update_lock(&proto);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 	return (NULL);
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static int
4047c478bd9Sstevel@tonic-gate cache_alloc(struct di_devlink_handle *hdp)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	size_t hash_sz = 0;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (DB_OPEN(hdp)) {
4117c478bd9Sstevel@tonic-gate 		hash_sz = DB_NUM(hdp, DB_LINK) / AVG_CHAIN_SIZE;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	hash_sz = (hash_sz >= MIN_HASH_SIZE) ? hash_sz : MIN_HASH_SIZE;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash = calloc(hash_sz, sizeof (cache_link_t *));
4167c478bd9Sstevel@tonic-gate 	if (CACHE(hdp)->hash == NULL) {
4177c478bd9Sstevel@tonic-gate 		return (-1);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash_sz = hash_sz;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (0);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate static int
4267c478bd9Sstevel@tonic-gate invalid_db(struct di_devlink_handle *hdp, size_t fsize, long page_sz)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	int i;
4297c478bd9Sstevel@tonic-gate 	char *cp;
4307c478bd9Sstevel@tonic-gate 	size_t sz;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	if (DB_HDR(hdp)->magic != DB_MAGIC || DB_HDR(hdp)->vers != DB_VERSION) {
4337c478bd9Sstevel@tonic-gate 		return (1);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (DB_HDR(hdp)->page_sz == 0 || DB_HDR(hdp)->page_sz != page_sz) {
4377c478bd9Sstevel@tonic-gate 		return (1);
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	sz = seg_size(hdp, DB_HEADER);
4417c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
4427c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "N[%u] = %u\n", i, DB_NUM(hdp, i));
4437c478bd9Sstevel@tonic-gate 		/* There must be at least 1 element of each type */
4447c478bd9Sstevel@tonic-gate 		if (DB_NUM(hdp, i) < 1) {
4457c478bd9Sstevel@tonic-gate 			return (1);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		sz += seg_size(hdp, i);
4487c478bd9Sstevel@tonic-gate 		assert(sz % page_sz == 0);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (sz != fsize) {
4527c478bd9Sstevel@tonic-gate 		return (1);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (!VALID_INDEX(hdp, DB_NODE, DB_HDR(hdp)->root_idx)) {
4567c478bd9Sstevel@tonic-gate 		return (1);
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (!VALID_INDEX(hdp, DB_LINK, DB_HDR(hdp)->dngl_idx)) {
4607c478bd9Sstevel@tonic-gate 		return (1);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (DB_EMPTY(hdp)) {
4647c478bd9Sstevel@tonic-gate 		return (1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/*
4687c478bd9Sstevel@tonic-gate 	 * The last character in the string segment must be a NUL char.
4697c478bd9Sstevel@tonic-gate 	 */
4707c478bd9Sstevel@tonic-gate 	cp = get_string(hdp, DB_NUM(hdp, DB_STR) - 1);
4717c478bd9Sstevel@tonic-gate 	if (cp == NULL || *cp != '\0') {
4727c478bd9Sstevel@tonic-gate 		return (1);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	return (0);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static int
4797c478bd9Sstevel@tonic-gate read_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	char *path;
4827c478bd9Sstevel@tonic-gate 	cache_node_t *cnp;
4837c478bd9Sstevel@tonic-gate 	struct db_node *dnp;
4847c478bd9Sstevel@tonic-gate 	const char *fcn = "read_nodes";
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * parent node should be NULL only for the root node
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	if ((pcnp == NULL) ^ (nidx == DB_HDR(hdp)->root_idx)) {
4927c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: invalid parent or index(%u)\n",
4937c478bd9Sstevel@tonic-gate 		    fcn, nidx);
4947c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
4957c478bd9Sstevel@tonic-gate 		return (-1);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	for (; dnp = get_node(hdp, nidx); nidx = dnp->sib) {
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		path = get_string(hdp, dnp->path);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/*
5037c478bd9Sstevel@tonic-gate 		 * Insert at head of list to recreate original order
5047c478bd9Sstevel@tonic-gate 		 */
5057c478bd9Sstevel@tonic-gate 		cnp = node_insert(hdp, pcnp, path, INSERT_HEAD);
5067c478bd9Sstevel@tonic-gate 		if (cnp == NULL) {
5077c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		assert(strcmp(path, "/") ^ (nidx == DB_HDR(hdp)->root_idx));
5127c478bd9Sstevel@tonic-gate 		assert(strcmp(path, "/") != 0 || dnp->sib == DB_NIL);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		if (read_minors(hdp, cnp, dnp->minor) != 0 ||
5157c478bd9Sstevel@tonic-gate 		    read_nodes(hdp, cnp, dnp->child) != 0) {
5167c478bd9Sstevel@tonic-gate 			break;
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, nidx,
5207c478bd9Sstevel@tonic-gate 		    cnp->path);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	return (dnp ? -1 : 0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static int
5277c478bd9Sstevel@tonic-gate read_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
5307c478bd9Sstevel@tonic-gate 	struct db_minor *dmp;
5317c478bd9Sstevel@tonic-gate 	char *name, *nodetype;
5327c478bd9Sstevel@tonic-gate 	const char *fcn = "read_minors";
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (pcnp == NULL) {
5377c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: minor[%u]: orphan minor\n", fcn,
5387c478bd9Sstevel@tonic-gate 		    nidx);
5397c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
5407c478bd9Sstevel@tonic-gate 		return (-1);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		name = get_string(hdp, dmp->name);
5467c478bd9Sstevel@tonic-gate 		nodetype = get_string(hdp, dmp->nodetype);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		cmnp = minor_insert(hdp, pcnp, name, nodetype, NULL);
5497c478bd9Sstevel@tonic-gate 		if (cmnp == NULL) {
5507c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
5517c478bd9Sstevel@tonic-gate 			break;
5527c478bd9Sstevel@tonic-gate 		}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, nidx,
5557c478bd9Sstevel@tonic-gate 		    cmnp->name);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		if (read_links(hdp, cmnp, dmp->link) != 0) {
5587c478bd9Sstevel@tonic-gate 			break;
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	return (dmp ? -1 : 0);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * If the link is dangling the corresponding minor will be absent.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static int
5697c478bd9Sstevel@tonic-gate read_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp, uint32_t nidx)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
5727c478bd9Sstevel@tonic-gate 	struct db_link *dlp;
5737c478bd9Sstevel@tonic-gate 	char *path, *content;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (nidx != DB_NIL &&
5787c478bd9Sstevel@tonic-gate 	    ((pcmp == NULL) ^ (nidx == DB_HDR(hdp)->dngl_idx))) {
5797c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "read_links: invalid minor or"
5807c478bd9Sstevel@tonic-gate 		    " index(%u)\n", nidx);
5817c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
5827c478bd9Sstevel@tonic-gate 		return (-1);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		path = get_string(hdp, dlp->path);
5887c478bd9Sstevel@tonic-gate 		content = get_string(hdp, dlp->content);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		clp = link_insert(hdp, pcmp, path, content, dlp->attr);
5917c478bd9Sstevel@tonic-gate 		if (clp == NULL) {
5927c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
5937c478bd9Sstevel@tonic-gate 			break;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "read_links: link[%u]: %s%s\n",
5977c478bd9Sstevel@tonic-gate 		    nidx, clp->path, pcmp == NULL ? "(DANGLING)" : "");
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	return (dlp ? -1 : 0);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate int
6047c478bd9Sstevel@tonic-gate di_devlink_close(di_devlink_handle_t *pp, int flag)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	int i, rv;
6077c478bd9Sstevel@tonic-gate 	char tmp[PATH_MAX];
6087c478bd9Sstevel@tonic-gate 	char file[PATH_MAX];
6097c478bd9Sstevel@tonic-gate 	uint32_t next[DB_TYPES] = {0};
6107c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (pp == NULL || *pp == NULL || !HDL_RDWR(*pp)) {
6137c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6147c478bd9Sstevel@tonic-gate 		return (-1);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	hdp = *pp;
6187c478bd9Sstevel@tonic-gate 	*pp = NULL;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * The caller encountered some error in their processing.
6227c478bd9Sstevel@tonic-gate 	 * so handle isn't valid. Discard it and return success.
6237c478bd9Sstevel@tonic-gate 	 */
6247c478bd9Sstevel@tonic-gate 	if (flag == DI_LINK_ERROR) {
6257c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
6267c478bd9Sstevel@tonic-gate 		return (0);
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	if (DB_ERR(hdp)) {
6307c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
6317c478bd9Sstevel@tonic-gate 		errno = EINVAL;
6327c478bd9Sstevel@tonic-gate 		return (-1);
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Extract the DB path before the handle is freed.
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	get_db_path(hdp, DB_FILE, file, sizeof (file));
6397c478bd9Sstevel@tonic-gate 	get_db_path(hdp, DB_TMP, tmp, sizeof (tmp));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * update database with actual contents of /dev
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "di_devlink_close: update_count = %u\n",
6457c478bd9Sstevel@tonic-gate 	    CACHE(hdp)->update_count);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * For performance reasons, synchronization of the database
6497c478bd9Sstevel@tonic-gate 	 * with /dev is turned off by default. However, applications
6507c478bd9Sstevel@tonic-gate 	 * with appropriate permissions can request a "sync" by
6517c478bd9Sstevel@tonic-gate 	 * calling di_devlink_update().
6527c478bd9Sstevel@tonic-gate 	 */
6537c478bd9Sstevel@tonic-gate 	if (CACHE(hdp)->update_count == 0) {
6547c478bd9Sstevel@tonic-gate 		CACHE(hdp)->update_count = 1;
6557c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO,
6567c478bd9Sstevel@tonic-gate 		    "di_devlink_close: synchronizing DB\n");
6577c478bd9Sstevel@tonic-gate 		(void) synchronize_db(hdp);
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Resolve dangling links AFTER synchronizing DB with /dev as the
6627c478bd9Sstevel@tonic-gate 	 * synchronization process may create dangling links.
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	resolve_dangling_links(hdp);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * All changes to the cache are complete. Write out the cache
6687c478bd9Sstevel@tonic-gate 	 * to the database only if it is not empty.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	if (CACHE_EMPTY(hdp)) {
6717c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "di_devlink_close: skipping write\n");
6727c478bd9Sstevel@tonic-gate 		(void) unlink(file);
6737c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
6747c478bd9Sstevel@tonic-gate 		return (0);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	if (open_db(hdp, OPEN_RDWR) != 0) {
6787c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
6797c478bd9Sstevel@tonic-gate 		return (-1);
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/*
6837c478bd9Sstevel@tonic-gate 	 * Keep track of array assignments. There is atleast
6847c478bd9Sstevel@tonic-gate 	 * 1 element (the "NIL" element) per type.
6857c478bd9Sstevel@tonic-gate 	 */
6867c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
6877c478bd9Sstevel@tonic-gate 		next[i] = 1;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	(void) write_nodes(hdp, NULL, CACHE_ROOT(hdp), next);
6917c478bd9Sstevel@tonic-gate 	(void) write_links(hdp, NULL, CACHE(hdp)->dngl, next);
6927c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->update_count = CACHE(hdp)->update_count;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	rv = close_db(hdp);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (rv != 0 || DB_ERR(hdp) || rename(tmp, file) != 0) {
6977c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "di_devlink_close: %s error: %s\n",
6987c478bd9Sstevel@tonic-gate 		    rv ? "close_db" : "DB or rename", strerror(errno));
6997c478bd9Sstevel@tonic-gate 		(void) unlink(tmp);
7007c478bd9Sstevel@tonic-gate 		(void) unlink(file);
7017c478bd9Sstevel@tonic-gate 		handle_free(&hdp);
7027c478bd9Sstevel@tonic-gate 		return (-1);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	handle_free(&hdp);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "di_devlink_close: wrote DB(%s)\n", file);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	return (0);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * Inits the database header.
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate static int
7167c478bd9Sstevel@tonic-gate init_hdr(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	int i;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->magic = DB_MAGIC;
7217c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->vers = DB_VERSION;
7227c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->root_idx = DB_NIL;
7237c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->dngl_idx = DB_NIL;
7247c478bd9Sstevel@tonic-gate 	DB_HDR(hdp)->page_sz = (uint32_t)page_sz;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
7277c478bd9Sstevel@tonic-gate 		assert(count[i] >= 1);
7287c478bd9Sstevel@tonic-gate 		DB_NUM(hdp, i) = count[i];
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	return (0);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate static int
7357c478bd9Sstevel@tonic-gate write_nodes(
7367c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
7377c478bd9Sstevel@tonic-gate 	struct db_node *pdnp,
7387c478bd9Sstevel@tonic-gate 	cache_node_t *cnp,
7397c478bd9Sstevel@tonic-gate 	uint32_t *next)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	uint32_t idx;
7427c478bd9Sstevel@tonic-gate 	struct db_node *dnp;
7437c478bd9Sstevel@tonic-gate 	const char *fcn = "write_nodes";
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	for (; cnp != NULL; cnp = cnp->sib) {
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 		assert(cnp->path != NULL);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		/* parent node should only be NULL for root node */
7527c478bd9Sstevel@tonic-gate 		if ((pdnp == NULL) ^ (cnp == CACHE_ROOT(hdp))) {
7537c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: invalid parent for: %s\n",
7547c478bd9Sstevel@tonic-gate 			    fcn, cnp->path);
7557c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
7567c478bd9Sstevel@tonic-gate 			break;
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		assert((strcmp(cnp->path, "/") != 0) ^
7607c478bd9Sstevel@tonic-gate 		    (cnp == CACHE_ROOT(hdp)));
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		idx = next[DB_NODE];
7637c478bd9Sstevel@tonic-gate 		if ((dnp = set_node(hdp, idx)) == NULL) {
7647c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
7657c478bd9Sstevel@tonic-gate 			break;
7667c478bd9Sstevel@tonic-gate 		}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		dnp->path = write_string(hdp, cnp->path, next);
7697c478bd9Sstevel@tonic-gate 		if (dnp->path == DB_NIL) {
7707c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
7717c478bd9Sstevel@tonic-gate 			break;
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		/* commit write for this node */
7747c478bd9Sstevel@tonic-gate 		next[DB_NODE]++;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if (pdnp == NULL) {
7777c478bd9Sstevel@tonic-gate 			assert(DB_HDR(hdp)->root_idx == DB_NIL);
7787c478bd9Sstevel@tonic-gate 			DB_HDR(hdp)->root_idx = idx;
7797c478bd9Sstevel@tonic-gate 		} else {
7807c478bd9Sstevel@tonic-gate 			dnp->sib = pdnp->child;
7817c478bd9Sstevel@tonic-gate 			pdnp->child = idx;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, idx,
7857c478bd9Sstevel@tonic-gate 		    cnp->path);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		if (write_minors(hdp, dnp, cnp->minor, next) != 0 ||
7887c478bd9Sstevel@tonic-gate 		    write_nodes(hdp, dnp, cnp->child, next) != 0) {
7897c478bd9Sstevel@tonic-gate 			break;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	return (cnp ? -1 : 0);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate static int
7977c478bd9Sstevel@tonic-gate write_minors(
7987c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
7997c478bd9Sstevel@tonic-gate 	struct db_node *pdnp,
8007c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp,
8017c478bd9Sstevel@tonic-gate 	uint32_t *next)
8027c478bd9Sstevel@tonic-gate {
8037c478bd9Sstevel@tonic-gate 	uint32_t idx;
8047c478bd9Sstevel@tonic-gate 	struct db_minor *dmp;
8057c478bd9Sstevel@tonic-gate 	const char *fcn = "write_minors";
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (pdnp == NULL) {
8107c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: no node for minor: %s\n", fcn,
8117c478bd9Sstevel@tonic-gate 		    cmnp ? cmnp->name : "<NULL>");
8127c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
8137c478bd9Sstevel@tonic-gate 		return (-1);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	for (; cmnp != NULL; cmnp = cmnp->sib) {
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 		assert(cmnp->name != NULL);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		idx = next[DB_MINOR];
8217c478bd9Sstevel@tonic-gate 		if ((dmp = set_minor(hdp, idx)) == NULL) {
8227c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
8237c478bd9Sstevel@tonic-gate 			break;
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		dmp->name = write_string(hdp, cmnp->name, next);
8277c478bd9Sstevel@tonic-gate 		dmp->nodetype = write_string(hdp, cmnp->nodetype, next);
8287c478bd9Sstevel@tonic-gate 		if (dmp->name == DB_NIL || dmp->nodetype == DB_NIL) {
8297c478bd9Sstevel@tonic-gate 			dmp->name = dmp->nodetype = DB_NIL;
8307c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		/* Commit writes to this minor */
8357c478bd9Sstevel@tonic-gate 		next[DB_MINOR]++;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		dmp->sib = pdnp->minor;
8387c478bd9Sstevel@tonic-gate 		pdnp->minor = idx;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, idx,
8417c478bd9Sstevel@tonic-gate 		    cmnp->name);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 		if (write_links(hdp, dmp, cmnp->link, next) != 0) {
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	return (cmnp ? -1 : 0);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate static int
8527c478bd9Sstevel@tonic-gate write_links(
8537c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
8547c478bd9Sstevel@tonic-gate 	struct db_minor *pdmp,
8557c478bd9Sstevel@tonic-gate 	cache_link_t *clp,
8567c478bd9Sstevel@tonic-gate 	uint32_t *next)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	uint32_t idx;
8597c478bd9Sstevel@tonic-gate 	struct db_link *dlp;
8607c478bd9Sstevel@tonic-gate 	const char *fcn = "write_links";
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* A NULL minor if and only if the links are dangling */
8657c478bd9Sstevel@tonic-gate 	if (clp != NULL && ((pdmp == NULL) ^ (clp == CACHE(hdp)->dngl))) {
8667c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: invalid minor for link\n", fcn);
8677c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
8687c478bd9Sstevel@tonic-gate 		return (-1);
8697c478bd9Sstevel@tonic-gate 	}
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	for (; clp != NULL; clp = clp->sib) {
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		assert(clp->path != NULL);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 		if ((pdmp == NULL) ^ (clp->minor == NULL)) {
8767c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: invalid minor for link"
8777c478bd9Sstevel@tonic-gate 			    "(%s)\n", fcn, clp->path);
8787c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		idx = next[DB_LINK];
8837c478bd9Sstevel@tonic-gate 		if ((dlp = set_link(hdp, idx)) == NULL) {
8847c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		dlp->path = write_string(hdp, clp->path, next);
8897c478bd9Sstevel@tonic-gate 		dlp->content = write_string(hdp, clp->content, next);
8907c478bd9Sstevel@tonic-gate 		if (dlp->path == DB_NIL || dlp->content == DB_NIL) {
8917c478bd9Sstevel@tonic-gate 			dlp->path = dlp->content = DB_NIL;
8927c478bd9Sstevel@tonic-gate 			SET_DB_ERR(hdp);
8937c478bd9Sstevel@tonic-gate 			break;
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		dlp->attr = clp->attr;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		/* Commit writes to this link */
8997c478bd9Sstevel@tonic-gate 		next[DB_LINK]++;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		if (pdmp != NULL) {
9027c478bd9Sstevel@tonic-gate 			dlp->sib = pdmp->link;
9037c478bd9Sstevel@tonic-gate 			pdmp->link = idx;
9047c478bd9Sstevel@tonic-gate 		} else {
9057c478bd9Sstevel@tonic-gate 			dlp->sib = DB_HDR(hdp)->dngl_idx;
9067c478bd9Sstevel@tonic-gate 			DB_HDR(hdp)->dngl_idx = idx;
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_STEP, "%s: link[%u]: %s%s\n", fcn, idx,
9107c478bd9Sstevel@tonic-gate 		    clp->path, pdmp == NULL ? "(DANGLING)" : "");
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	return (clp ? -1 : 0);
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate static uint32_t
9187c478bd9Sstevel@tonic-gate write_string(struct di_devlink_handle *hdp, const char *str, uint32_t *next)
9197c478bd9Sstevel@tonic-gate {
9207c478bd9Sstevel@tonic-gate 	char *dstr;
9217c478bd9Sstevel@tonic-gate 	uint32_t idx;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp));
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if (str == NULL) {
9267c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "write_string: NULL argument\n");
9277c478bd9Sstevel@tonic-gate 		return (DB_NIL);
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	idx = next[DB_STR];
9317c478bd9Sstevel@tonic-gate 	if (!VALID_STR(hdp, idx, str)) {
9327c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "write_string: invalid index[%u],"
9337c478bd9Sstevel@tonic-gate 		    " string(%s)\n", idx, str);
9347c478bd9Sstevel@tonic-gate 		return (DB_NIL);
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if ((dstr = set_string(hdp, idx)) == NULL) {
9387c478bd9Sstevel@tonic-gate 		return (DB_NIL);
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	(void) strcpy(dstr, str);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	next[DB_STR] += strlen(dstr) + 1;
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (idx);
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate static int
9497c478bd9Sstevel@tonic-gate close_db(struct di_devlink_handle *hdp)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	int i, rv = 0;
9527c478bd9Sstevel@tonic-gate 	size_t sz;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (!DB_OPEN(hdp)) {
9557c478bd9Sstevel@tonic-gate #ifdef	DEBUG
9567c478bd9Sstevel@tonic-gate 		assert(DB(hdp)->db_fd == -1);
9577c478bd9Sstevel@tonic-gate 		assert(DB(hdp)->flags == 0);
9587c478bd9Sstevel@tonic-gate 		for (i = 0; i < DB_TYPES; i++) {
9597c478bd9Sstevel@tonic-gate 			assert(DB_SEG(hdp, i) == NULL);
9607c478bd9Sstevel@tonic-gate 			assert(DB_SEG_PROT(hdp, i) == 0);
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate #endif
9637c478bd9Sstevel@tonic-gate 		return (0);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/* Unmap header after unmapping all other mapped segments */
9677c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
9687c478bd9Sstevel@tonic-gate 		if (DB_SEG(hdp, i)) {
9697c478bd9Sstevel@tonic-gate 			sz = seg_size(hdp, i);
9707c478bd9Sstevel@tonic-gate 			if (DB_RDWR(hdp))
9717c478bd9Sstevel@tonic-gate 				rv += msync(DB_SEG(hdp, i), sz, MS_SYNC);
9727c478bd9Sstevel@tonic-gate 			(void) munmap(DB_SEG(hdp, i), sz);
9737c478bd9Sstevel@tonic-gate 			DB_SEG(hdp, i) = NULL;
9747c478bd9Sstevel@tonic-gate 			DB_SEG_PROT(hdp, i) = 0;
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (DB_RDWR(hdp))
9797c478bd9Sstevel@tonic-gate 		rv += msync((caddr_t)DB_HDR(hdp), HDR_LEN, MS_SYNC);
9807c478bd9Sstevel@tonic-gate 	(void) munmap((caddr_t)DB_HDR(hdp), HDR_LEN);
9817c478bd9Sstevel@tonic-gate 	DB(hdp)->hdr = NULL;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	(void) close(DB(hdp)->db_fd);
9847c478bd9Sstevel@tonic-gate 	DB(hdp)->db_fd = -1;
9857c478bd9Sstevel@tonic-gate 	DB(hdp)->flags = 0;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	return (rv ? -1 : 0);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate static void
9927c478bd9Sstevel@tonic-gate cache_free(struct di_devlink_handle *hdp)
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	subtree_free(hdp, &(CACHE_ROOT(hdp)));
9977c478bd9Sstevel@tonic-gate 	assert(CACHE_LAST(hdp) == NULL);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/*
10007c478bd9Sstevel@tonic-gate 	 * Don't bother removing links from hash table chains,
10017c478bd9Sstevel@tonic-gate 	 * as we are freeing the hash table itself.
10027c478bd9Sstevel@tonic-gate 	 */
10037c478bd9Sstevel@tonic-gate 	while (CACHE(hdp)->dngl != NULL) {
10047c478bd9Sstevel@tonic-gate 		clp = CACHE(hdp)->dngl;
10057c478bd9Sstevel@tonic-gate 		CACHE(hdp)->dngl = clp->sib;
10067c478bd9Sstevel@tonic-gate 		assert(clp->minor == NULL);
10077c478bd9Sstevel@tonic-gate 		link_free(&clp);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	assert((CACHE(hdp)->hash == NULL) ^ (CACHE(hdp)->hash_sz != 0));
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	free(CACHE(hdp)->hash);
10137c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash = NULL;
10147c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash_sz = 0;
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate static void
10187c478bd9Sstevel@tonic-gate handle_free(struct di_devlink_handle **pp)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp = *pp;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	*pp = NULL;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if (hdp == NULL)
10257c478bd9Sstevel@tonic-gate 		return;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	(void) close_db(hdp);
10287c478bd9Sstevel@tonic-gate 	cache_free(hdp);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (HDL_RDWR(hdp))
10317c478bd9Sstevel@tonic-gate 		exit_update_lock(hdp);
10327c478bd9Sstevel@tonic-gate 	assert(hdp->lock_fd == -1);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	free(hdp->dev_dir);
1035*a08731ecScth 	free(hdp->db_dir);
10367c478bd9Sstevel@tonic-gate 	free(hdp);
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate /*
10407c478bd9Sstevel@tonic-gate  * Frees the tree rooted at a node. Siblings of the subtree root
10417c478bd9Sstevel@tonic-gate  * have to be handled by the caller.
10427c478bd9Sstevel@tonic-gate  */
10437c478bd9Sstevel@tonic-gate static void
10447c478bd9Sstevel@tonic-gate subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	cache_node_t *np;
10477c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
10487c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (pp == NULL || *pp == NULL)
10517c478bd9Sstevel@tonic-gate 		return;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	while ((*pp)->child != NULL) {
10547c478bd9Sstevel@tonic-gate 		np = (*pp)->child;
10557c478bd9Sstevel@tonic-gate 		(*pp)->child = np->sib;
10567c478bd9Sstevel@tonic-gate 		subtree_free(hdp, &np);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	while ((*pp)->minor != NULL) {
10607c478bd9Sstevel@tonic-gate 		cmnp = (*pp)->minor;
10617c478bd9Sstevel@tonic-gate 		(*pp)->minor = cmnp->sib;
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		while (cmnp->link != NULL) {
10647c478bd9Sstevel@tonic-gate 			clp = cmnp->link;
10657c478bd9Sstevel@tonic-gate 			cmnp->link = clp->sib;
10667c478bd9Sstevel@tonic-gate 			rm_link_from_hash(hdp, clp);
10677c478bd9Sstevel@tonic-gate 			link_free(&clp);
10687c478bd9Sstevel@tonic-gate 		}
10697c478bd9Sstevel@tonic-gate 		minor_free(hdp, &cmnp);
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	node_free(pp);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate static void
10767c478bd9Sstevel@tonic-gate rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	int hval;
10797c478bd9Sstevel@tonic-gate 	cache_link_t **pp;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (clp == NULL)
10827c478bd9Sstevel@tonic-gate 		return;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (clp->path == NULL)
10857c478bd9Sstevel@tonic-gate 		return;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	hval = hashfn(hdp, clp->path);
10887c478bd9Sstevel@tonic-gate 	pp = &(CACHE_HASH(hdp, hval));
10897c478bd9Sstevel@tonic-gate 	for (; *pp != NULL; pp = &(*pp)->hash) {
10907c478bd9Sstevel@tonic-gate 		if (*pp == clp) {
10917c478bd9Sstevel@tonic-gate 			*pp = clp->hash;
10927c478bd9Sstevel@tonic-gate 			clp->hash = NULL;
10937c478bd9Sstevel@tonic-gate 			return;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	dprintf(DBG_ERR, "rm_link_from_hash: link(%s) not found\n", clp->path);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate static cache_link_t *
11017c478bd9Sstevel@tonic-gate link_hash(di_devlink_handle_t hdp, const char *link, uint_t flags)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	int hval;
11047c478bd9Sstevel@tonic-gate 	cache_link_t **pp, *clp;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	if (link == NULL)
11077c478bd9Sstevel@tonic-gate 		return (NULL);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	hval = hashfn(hdp, link);
11107c478bd9Sstevel@tonic-gate 	pp = &(CACHE_HASH(hdp, hval));
11117c478bd9Sstevel@tonic-gate 	for (; (clp = *pp) != NULL; pp = &clp->hash) {
11127c478bd9Sstevel@tonic-gate 		if (strcmp(clp->path, link) == 0) {
11137c478bd9Sstevel@tonic-gate 			break;
11147c478bd9Sstevel@tonic-gate 		}
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if (clp == NULL)
11187c478bd9Sstevel@tonic-gate 		return (NULL);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	if ((flags & UNLINK_FROM_HASH) == UNLINK_FROM_HASH) {
11217c478bd9Sstevel@tonic-gate 		*pp = clp->hash;
11227c478bd9Sstevel@tonic-gate 		clp->hash = NULL;
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	return (clp);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate static cache_minor_t *
11297c478bd9Sstevel@tonic-gate link2minor(struct di_devlink_handle *hdp, cache_link_t *clp)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	cache_link_t *plp;
11327c478bd9Sstevel@tonic-gate 	const char *minor_path;
11337c478bd9Sstevel@tonic-gate 	char *cp, buf[PATH_MAX], link[PATH_MAX];
1134facf4a8dSllai 	char abspath[PATH_MAX];
1135facf4a8dSllai 	struct stat st;
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (TYPE_PRI(attr2type(clp->attr))) {
11387c478bd9Sstevel@tonic-gate 		/*
11397c478bd9Sstevel@tonic-gate 		 * For primary link, content should point to a /devices node.
11407c478bd9Sstevel@tonic-gate 		 */
11417c478bd9Sstevel@tonic-gate 		if (!is_minor_node(clp->content, &minor_path)) {
11427c478bd9Sstevel@tonic-gate 			return (NULL);
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		return (lookup_minor(hdp, minor_path, NULL,
11467c478bd9Sstevel@tonic-gate 		    TYPE_CACHE|CREATE_FLAG));
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * If secondary, the primary link is derived from the secondary
11527c478bd9Sstevel@tonic-gate 	 * link contents. Secondary link contents can have two formats:
1153facf4a8dSllai 	 *	audio -> /dev/sound/0
11547c478bd9Sstevel@tonic-gate 	 *	fb0 -> fbs/afb0
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
11587c478bd9Sstevel@tonic-gate 	if (strncmp(clp->content, DEV"/", strlen(DEV"/")) == 0) {
11597c478bd9Sstevel@tonic-gate 		cp = &clp->content[strlen(DEV"/")];
11607c478bd9Sstevel@tonic-gate 	} else if (clp->content[0] != '/') {
11617c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(clp->path, '/')) != NULL) {
11627c478bd9Sstevel@tonic-gate 			char savechar = *(cp + 1);
11637c478bd9Sstevel@tonic-gate 			*(cp + 1) = '\0';
11647c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", clp->path);
11657c478bd9Sstevel@tonic-gate 			*(cp + 1) = savechar;
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 		(void) strlcat(buf, clp->content, sizeof (buf));
11687c478bd9Sstevel@tonic-gate 		cp = buf;
11697c478bd9Sstevel@tonic-gate 	} else {
11707c478bd9Sstevel@tonic-gate 		goto follow_link;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	/*
11747c478bd9Sstevel@tonic-gate 	 * Lookup the primary link if possible and find its minor.
11757c478bd9Sstevel@tonic-gate 	 */
11767c478bd9Sstevel@tonic-gate 	if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) {
11777c478bd9Sstevel@tonic-gate 		return (plp->minor);
11787c478bd9Sstevel@tonic-gate 	}
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	/* realpath() used only as a last resort because it is expensive */
11817c478bd9Sstevel@tonic-gate follow_link:
11827c478bd9Sstevel@tonic-gate 	(void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate #ifdef	DEBUG
11857c478bd9Sstevel@tonic-gate 	/*LINTED*/
11867c478bd9Sstevel@tonic-gate 	assert(sizeof (buf) >= PATH_MAX);
11877c478bd9Sstevel@tonic-gate #endif
1188facf4a8dSllai 
1189facf4a8dSllai 	/*
1190facf4a8dSllai 	 * A realpath attempt to lookup a dangling link can invoke implicit
1191facf4a8dSllai 	 * reconfig so verify there's an actual device behind the link first.
1192facf4a8dSllai 	 */
1193facf4a8dSllai 	if (lstat(link, &st) == -1)
1194facf4a8dSllai 		return (NULL);
1195facf4a8dSllai 	if (S_ISLNK(st.st_mode)) {
1196facf4a8dSllai 		if (s_readlink(link, buf, sizeof (buf)) < 0)
1197facf4a8dSllai 			return (NULL);
1198facf4a8dSllai 		if (buf[0] != '/') {
1199facf4a8dSllai 			char *p;
1200facf4a8dSllai 			size_t n = sizeof (abspath);
1201facf4a8dSllai 			if (strlcpy(abspath, link, n) >= n)
1202facf4a8dSllai 				return (NULL);
1203facf4a8dSllai 			p = strrchr(abspath, '/') + 1;
1204facf4a8dSllai 			*p = 0;
1205facf4a8dSllai 			n = sizeof (abspath) - strlen(p);
1206facf4a8dSllai 			if (strlcpy(p, buf, n) >= n)
1207facf4a8dSllai 				return (NULL);
1208facf4a8dSllai 		} else {
1209facf4a8dSllai 			if (strlcpy(abspath, buf, sizeof (abspath)) >=
1210facf4a8dSllai 			    sizeof (abspath))
1211facf4a8dSllai 				return (NULL);
1212facf4a8dSllai 		}
1213facf4a8dSllai 		if (!device_exists(abspath))
1214facf4a8dSllai 			return (NULL);
1215facf4a8dSllai 	}
1216facf4a8dSllai 
12177c478bd9Sstevel@tonic-gate 	if (realpath(link, buf) == NULL || !is_minor_node(buf, &minor_path)) {
12187c478bd9Sstevel@tonic-gate 		return (NULL);
12197c478bd9Sstevel@tonic-gate 	}
12207c478bd9Sstevel@tonic-gate 	return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG));
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate static void
12257c478bd9Sstevel@tonic-gate resolve_dangling_links(struct di_devlink_handle *hdp)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
12287c478bd9Sstevel@tonic-gate 	cache_link_t *clp, **pp;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) {
12317c478bd9Sstevel@tonic-gate 		clp = *pp;
12327c478bd9Sstevel@tonic-gate 		if ((cmnp = link2minor(hdp, clp)) != NULL) {
12337c478bd9Sstevel@tonic-gate 			*pp = clp->sib;
12347c478bd9Sstevel@tonic-gate 			clp->sib = cmnp->link;
12357c478bd9Sstevel@tonic-gate 			cmnp->link = clp;
12367c478bd9Sstevel@tonic-gate 			assert(clp->minor == NULL);
12377c478bd9Sstevel@tonic-gate 			clp->minor = cmnp;
12387c478bd9Sstevel@tonic-gate 		} else {
12397c478bd9Sstevel@tonic-gate 			dprintf(DBG_INFO, "resolve_dangling_links: link(%s):"
12407c478bd9Sstevel@tonic-gate 			    " unresolved\n", clp->path);
12417c478bd9Sstevel@tonic-gate 			pp = &clp->sib;
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate }
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * The elements are assumed to be detached from the cache tree.
12497c478bd9Sstevel@tonic-gate  */
12507c478bd9Sstevel@tonic-gate static void
12517c478bd9Sstevel@tonic-gate node_free(cache_node_t **pp)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	cache_node_t *cnp = *pp;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	*pp = NULL;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	if (cnp == NULL)
12587c478bd9Sstevel@tonic-gate 		return;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	free(cnp->path);
12617c478bd9Sstevel@tonic-gate 	free(cnp);
12627c478bd9Sstevel@tonic-gate }
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate static void
12657c478bd9Sstevel@tonic-gate minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp = *pp;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	*pp = NULL;
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if (cmnp == NULL)
12727c478bd9Sstevel@tonic-gate 		return;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if (CACHE_LAST(hdp) == cmnp) {
12757c478bd9Sstevel@tonic-gate 		dprintf(DBG_STEP, "minor_free: last_minor(%s)\n", cmnp->name);
12767c478bd9Sstevel@tonic-gate 		CACHE_LAST(hdp) = NULL;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	free(cmnp->name);
12807c478bd9Sstevel@tonic-gate 	free(cmnp->nodetype);
12817c478bd9Sstevel@tonic-gate 	free(cmnp);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate static void
12857c478bd9Sstevel@tonic-gate link_free(cache_link_t **pp)
12867c478bd9Sstevel@tonic-gate {
12877c478bd9Sstevel@tonic-gate 	cache_link_t *clp = *pp;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	*pp = NULL;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if (clp == NULL)
12927c478bd9Sstevel@tonic-gate 		return;
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate 	free(clp->path);
12957c478bd9Sstevel@tonic-gate 	free(clp->content);
12967c478bd9Sstevel@tonic-gate 	free(clp);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * Returns the ':' preceding the minor name
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate static char *
13037c478bd9Sstevel@tonic-gate minor_colon(const char *path)
13047c478bd9Sstevel@tonic-gate {
13057c478bd9Sstevel@tonic-gate 	char *cp;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(path, '/')) == NULL) {
13087c478bd9Sstevel@tonic-gate 		return (NULL);
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	return (strchr(cp, ':'));
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate static void *
13157c478bd9Sstevel@tonic-gate lookup_minor(
13167c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
13177c478bd9Sstevel@tonic-gate 	const char *minor_path,
13187c478bd9Sstevel@tonic-gate 	const char *nodetype,
13197c478bd9Sstevel@tonic-gate 	const int flags)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	void *vp;
13227c478bd9Sstevel@tonic-gate 	char *colon;
13237c478bd9Sstevel@tonic-gate 	char pdup[PATH_MAX];
13247c478bd9Sstevel@tonic-gate 	const char *fcn = "lookup_minor";
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	if (minor_path == NULL) {
13277c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13287c478bd9Sstevel@tonic-gate 		return (NULL);
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	(void) snprintf(pdup, sizeof (pdup), "%s", minor_path);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if ((colon = minor_colon(pdup)) == NULL) {
13347c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: invalid minor path(%s)\n", fcn,
13357c478bd9Sstevel@tonic-gate 		    minor_path);
13367c478bd9Sstevel@tonic-gate 		errno = EINVAL;
13377c478bd9Sstevel@tonic-gate 		return (NULL);
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	*colon = '\0';
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) {
13427c478bd9Sstevel@tonic-gate 		return (vp);
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	if ((vp = lookup_node(hdp, pdup, flags)) == NULL) {
13467c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: node(%s) not found\n", fcn, pdup);
13477c478bd9Sstevel@tonic-gate 		return (NULL);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 	*colon = ':';
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	if (LOOKUP_CACHE(flags)) {
13527c478bd9Sstevel@tonic-gate 		cache_minor_t **pp;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		pp = &((cache_node_t *)vp)->minor;
13557c478bd9Sstevel@tonic-gate 		for (; *pp != NULL; pp = &(*pp)->sib) {
13567c478bd9Sstevel@tonic-gate 			if (strcmp((*pp)->name, colon + 1) == 0)
13577c478bd9Sstevel@tonic-gate 				break;
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		if (*pp == NULL && CREATE_ELEM(flags)) {
13617c478bd9Sstevel@tonic-gate 			*pp = minor_insert(hdp, vp, colon + 1, nodetype, pp);
13627c478bd9Sstevel@tonic-gate 		}
13637c478bd9Sstevel@tonic-gate 		set_last_minor(hdp, *pp, flags);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 		return (*pp);
13667c478bd9Sstevel@tonic-gate 	} else {
13677c478bd9Sstevel@tonic-gate 		char *cp;
13687c478bd9Sstevel@tonic-gate 		uint32_t nidx;
13697c478bd9Sstevel@tonic-gate 		struct db_minor *dmp;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 		nidx = (((struct db_node *)vp)->minor);
13727c478bd9Sstevel@tonic-gate 		for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
13737c478bd9Sstevel@tonic-gate 			cp = get_string(hdp, dmp->name);
13747c478bd9Sstevel@tonic-gate 			if (cp && strcmp(cp, colon + 1) == 0)
13757c478bd9Sstevel@tonic-gate 				break;
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 		return (dmp);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate }
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate static void *
13827c478bd9Sstevel@tonic-gate lookup_node(struct di_devlink_handle *hdp, char *path, const int flags)
13837c478bd9Sstevel@tonic-gate {
13847c478bd9Sstevel@tonic-gate 	struct tnode tnd = {NULL};
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	if (tnd.node = get_last_node(hdp, path, flags))
13877c478bd9Sstevel@tonic-gate 		return (tnd.node);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	tnd.handle = hdp;
13907c478bd9Sstevel@tonic-gate 	tnd.flags = flags;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	if (walk_tree(path, &tnd, visit_node) != 0)
13937c478bd9Sstevel@tonic-gate 		return (NULL);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	return (tnd.node);
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate  * last_minor is used for nodes of TYPE_CACHE only.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate static void *
14027c478bd9Sstevel@tonic-gate get_last_node(struct di_devlink_handle *hdp, const char *path, int flags)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	cache_node_t *cnp;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate #ifdef	DEBUG
14077c478bd9Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14087c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "get_last_node: SKIPPING \"last\" "
14097c478bd9Sstevel@tonic-gate 		    "node cache\n");
14107c478bd9Sstevel@tonic-gate 		return (NULL);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate #endif
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL ||
14157c478bd9Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node == NULL) {
14167c478bd9Sstevel@tonic-gate 		return (NULL);
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	cnp = CACHE_LAST(hdp)->node;
14207c478bd9Sstevel@tonic-gate 	if (strcmp(cnp->path, path) == 0) {
14217c478bd9Sstevel@tonic-gate 		return (cnp);
14227c478bd9Sstevel@tonic-gate 	}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	cnp = cnp->sib;
14257c478bd9Sstevel@tonic-gate 	if (cnp && strcmp(cnp->path, path) == 0) {
14267c478bd9Sstevel@tonic-gate 		return (cnp);
14277c478bd9Sstevel@tonic-gate 	}
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	return (NULL);
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate static void *
14337c478bd9Sstevel@tonic-gate get_last_minor(
14347c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
14357c478bd9Sstevel@tonic-gate 	const char *devfs_path,
14367c478bd9Sstevel@tonic-gate 	const char *minor_name,
14377c478bd9Sstevel@tonic-gate 	int flags)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate #ifdef	DEBUG
14427c478bd9Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14437c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "get_last_minor: SKIPPING \"last\" "
14447c478bd9Sstevel@tonic-gate 		    "minor cache\n");
14457c478bd9Sstevel@tonic-gate 		return (NULL);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate #endif
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) {
14507c478bd9Sstevel@tonic-gate 		return (NULL);
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	cmnp = CACHE_LAST(hdp);
14547c478bd9Sstevel@tonic-gate 	if (strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
14557c478bd9Sstevel@tonic-gate 	    strcmp(cmnp->node->path, devfs_path) == 0) {
14567c478bd9Sstevel@tonic-gate 		return (cmnp);
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	cmnp = cmnp->sib;
14607c478bd9Sstevel@tonic-gate 	if (cmnp && strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
14617c478bd9Sstevel@tonic-gate 	    strcmp(cmnp->node->path, devfs_path) == 0) {
14627c478bd9Sstevel@tonic-gate 		set_last_minor(hdp, cmnp, TYPE_CACHE);
14637c478bd9Sstevel@tonic-gate 		return (cmnp);
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	return (NULL);
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate static void
14707c478bd9Sstevel@tonic-gate set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate #ifdef	DEBUG
14737c478bd9Sstevel@tonic-gate 	if (getenv(SKIP_LAST_CACHE)) {
14747c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "set_last_minor: SKIPPING \"last\" "
14757c478bd9Sstevel@tonic-gate 		    "minor cache\n");
14767c478bd9Sstevel@tonic-gate 		return;
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate #endif
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (LOOKUP_CACHE(flags) && cmnp) {
14817c478bd9Sstevel@tonic-gate 		CACHE_LAST(hdp) = cmnp;
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate /*
14877c478bd9Sstevel@tonic-gate  * Returns 0 if normal return or -1 otherwise.
14887c478bd9Sstevel@tonic-gate  */
14897c478bd9Sstevel@tonic-gate static int
14907c478bd9Sstevel@tonic-gate walk_tree(
14917c478bd9Sstevel@tonic-gate 	char *cur,
14927c478bd9Sstevel@tonic-gate 	void *arg,
14937c478bd9Sstevel@tonic-gate 	int (*node_callback)(const char *path, void *arg))
14947c478bd9Sstevel@tonic-gate {
14957c478bd9Sstevel@tonic-gate 	char *slash, buf[PATH_MAX];
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if (cur == NULL || cur[0] != '/' || strlen(cur) > sizeof (buf) - 1) {
14987c478bd9Sstevel@tonic-gate 		errno = EINVAL;
14997c478bd9Sstevel@tonic-gate 		return (-1);
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	(void) strcpy(buf, "/");
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 	for (;;) {
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 		if (node_callback(buf, arg) != DI_WALK_CONTINUE)
15077c478bd9Sstevel@tonic-gate 			break;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 		while (*cur == '/')
15107c478bd9Sstevel@tonic-gate 			cur++;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 		if (*cur == '\0')
15137c478bd9Sstevel@tonic-gate 			break;
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 		/*
15167c478bd9Sstevel@tonic-gate 		 * There is a next component(s). Append a "/" separator for all
15177c478bd9Sstevel@tonic-gate 		 * but the first (root) component.
15187c478bd9Sstevel@tonic-gate 		 */
15197c478bd9Sstevel@tonic-gate 		if (buf[1] != '\0') {
15207c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, "/", sizeof (buf));
15217c478bd9Sstevel@tonic-gate 		}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		if (slash = strchr(cur, '/')) {
15247c478bd9Sstevel@tonic-gate 			*slash = '\0';
15257c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, cur, sizeof (buf));
15267c478bd9Sstevel@tonic-gate 			*slash = '/';
15277c478bd9Sstevel@tonic-gate 			cur = slash;
15287c478bd9Sstevel@tonic-gate 		} else {
15297c478bd9Sstevel@tonic-gate 			(void) strlcat(buf, cur, sizeof (buf));
15307c478bd9Sstevel@tonic-gate 			cur += strlen(cur);
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	return (0);
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate static int
15407c478bd9Sstevel@tonic-gate visit_node(const char *path, void *arg)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	struct tnode *tnp = arg;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if (LOOKUP_CACHE(tnp->flags)) {
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 		cache_node_t *cnp = tnp->node;
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 		cnp = (cnp) ? cnp->child : CACHE_ROOT(tnp->handle);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 		for (; cnp != NULL; cnp = cnp->sib) {
15517c478bd9Sstevel@tonic-gate 			if (strcmp(cnp->path, path) == 0)
15527c478bd9Sstevel@tonic-gate 				break;
15537c478bd9Sstevel@tonic-gate 		}
15547c478bd9Sstevel@tonic-gate 		if (cnp == NULL && CREATE_ELEM(tnp->flags)) {
15557c478bd9Sstevel@tonic-gate 			cnp = node_insert(tnp->handle, tnp->node, path,
15567c478bd9Sstevel@tonic-gate 			    INSERT_TAIL);
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 		tnp->node = cnp;
15597c478bd9Sstevel@tonic-gate 	} else {
15607c478bd9Sstevel@tonic-gate 		char *cp;
15617c478bd9Sstevel@tonic-gate 		struct db_node *dnp = tnp->node;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 		dnp = (dnp) ? get_node(tnp->handle, dnp->child)
15647c478bd9Sstevel@tonic-gate 		    : get_node(tnp->handle, DB_HDR(tnp->handle)->root_idx);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		for (; dnp != NULL; dnp = get_node(tnp->handle, dnp->sib)) {
15677c478bd9Sstevel@tonic-gate 			cp = get_string(tnp->handle, dnp->path);
15687c478bd9Sstevel@tonic-gate 			if (cp && strcmp(cp, path) == 0) {
15697c478bd9Sstevel@tonic-gate 				break;
15707c478bd9Sstevel@tonic-gate 			}
15717c478bd9Sstevel@tonic-gate 		}
15727c478bd9Sstevel@tonic-gate 		tnp->node = dnp;
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * Terminate walk if node is not found for a path component.
15777c478bd9Sstevel@tonic-gate 	 */
15787c478bd9Sstevel@tonic-gate 	return (tnp->node ? DI_WALK_CONTINUE : DI_WALK_TERMINATE);
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate static void
15827c478bd9Sstevel@tonic-gate minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	cache_link_t **lpp;
15857c478bd9Sstevel@tonic-gate 	cache_minor_t **mpp;
15867c478bd9Sstevel@tonic-gate 	const char *fcn = "minor_delete";
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: removing minor: %s\n", fcn, cmnp->name);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/* detach minor from node */
15917c478bd9Sstevel@tonic-gate 	if (cmnp->node != NULL) {
15927c478bd9Sstevel@tonic-gate 		mpp = &cmnp->node->minor;
15937c478bd9Sstevel@tonic-gate 		for (; *mpp != NULL; mpp = &(*mpp)->sib) {
15947c478bd9Sstevel@tonic-gate 			if (*mpp == cmnp)
15957c478bd9Sstevel@tonic-gate 				break;
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 		if (*mpp == NULL) {
15997c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: dangling minor: %s\n",
16007c478bd9Sstevel@tonic-gate 			    fcn, cmnp->name);
16017c478bd9Sstevel@tonic-gate 		} else {
16027c478bd9Sstevel@tonic-gate 			*mpp = cmnp->sib;
16037c478bd9Sstevel@tonic-gate 		}
16047c478bd9Sstevel@tonic-gate 	} else {
16057c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: orphan minor(%s)\n", fcn,
16067c478bd9Sstevel@tonic-gate 		    cmnp->name);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	delete_unused_nodes(hdp, cmnp->node);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	cmnp->node = NULL;
16127c478bd9Sstevel@tonic-gate 	cmnp->sib = NULL;
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	/* Move all remaining links to dangling list */
16157c478bd9Sstevel@tonic-gate 	for (lpp = &cmnp->link; *lpp != NULL; lpp = &(*lpp)->sib) {
16167c478bd9Sstevel@tonic-gate 		(*lpp)->minor = NULL;
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 	*lpp = CACHE(hdp)->dngl;
16197c478bd9Sstevel@tonic-gate 	CACHE(hdp)->dngl = cmnp->link;
16207c478bd9Sstevel@tonic-gate 	cmnp->link = NULL;
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	minor_free(hdp, &cmnp);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate static void
16267c478bd9Sstevel@tonic-gate delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	cache_node_t **npp;
16297c478bd9Sstevel@tonic-gate 	const char *fcn = "delete_unused_nodes";
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	if (cnp == NULL)
16327c478bd9Sstevel@tonic-gate 		return;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 	if (cnp->minor != NULL || cnp->child != NULL)
16357c478bd9Sstevel@tonic-gate 		return;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "%s: removing unused node: %s\n", fcn,
16387c478bd9Sstevel@tonic-gate 	    cnp->path);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	/* Unlink node from tree */
16417c478bd9Sstevel@tonic-gate 	if (cnp->parent != NULL) {
16427c478bd9Sstevel@tonic-gate 		npp = &cnp->parent->child;
16437c478bd9Sstevel@tonic-gate 		for (; *npp != NULL; npp = &(*npp)->sib) {
16447c478bd9Sstevel@tonic-gate 			if (*npp == cnp)
16457c478bd9Sstevel@tonic-gate 				break;
16467c478bd9Sstevel@tonic-gate 		}
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 		if (*npp == NULL) {
16497c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: dangling node: %s\n", fcn,
16507c478bd9Sstevel@tonic-gate 			    cnp->path);
16517c478bd9Sstevel@tonic-gate 		} else {
16527c478bd9Sstevel@tonic-gate 			*npp = cnp->sib;
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 	} else if (cnp == CACHE_ROOT(hdp)) {
16557c478bd9Sstevel@tonic-gate 		CACHE_ROOT(hdp) = NULL;
16567c478bd9Sstevel@tonic-gate 	} else {
16577c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: orphan node (%s)\n", fcn,
16587c478bd9Sstevel@tonic-gate 		    cnp->path);
16597c478bd9Sstevel@tonic-gate 	}
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	delete_unused_nodes(hdp, cnp->parent);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	cnp->parent = cnp->sib = NULL;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	node_free(&cnp);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate static int
16697c478bd9Sstevel@tonic-gate rm_link(di_devlink_handle_t hdp, const char *link)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
16727c478bd9Sstevel@tonic-gate 	const char *fcn = "rm_link";
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' ||
16757c478bd9Sstevel@tonic-gate 	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
16767c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "%s: %s: invalid args\n",
16777c478bd9Sstevel@tonic-gate 		    fcn, link ? link : "<NULL>");
16787c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16797c478bd9Sstevel@tonic-gate 		return (-1);
16807c478bd9Sstevel@tonic-gate 	}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	dprintf(DBG_STEP, "%s: link(%s)\n", fcn, link);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) {
16857c478bd9Sstevel@tonic-gate 		return (0);
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	link_delete(hdp, clp);
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 	return (0);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate int
16947c478bd9Sstevel@tonic-gate di_devlink_rm_link(di_devlink_handle_t hdp, const char *link)
16957c478bd9Sstevel@tonic-gate {
16967c478bd9Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDWR(hdp)) {
16977c478bd9Sstevel@tonic-gate 		errno = EINVAL;
16987c478bd9Sstevel@tonic-gate 		return (-1);
16997c478bd9Sstevel@tonic-gate 	}
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	return (rm_link(hdp, link));
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate static void
17057c478bd9Sstevel@tonic-gate link_delete(di_devlink_handle_t hdp, cache_link_t *clp)
17067c478bd9Sstevel@tonic-gate {
17077c478bd9Sstevel@tonic-gate 	cache_link_t **pp;
17087c478bd9Sstevel@tonic-gate 	const char *fcn = "link_delete";
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: removing link: %s\n", fcn, clp->path);
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (clp->minor == NULL)
17137c478bd9Sstevel@tonic-gate 		pp = &(CACHE(hdp)->dngl);
17147c478bd9Sstevel@tonic-gate 	else
17157c478bd9Sstevel@tonic-gate 		pp = &clp->minor->link;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	for (; *pp != NULL; pp = &(*pp)->sib) {
17187c478bd9Sstevel@tonic-gate 		if (*pp == clp)
17197c478bd9Sstevel@tonic-gate 			break;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (*pp == NULL) {
17237c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "%s: link(%s) not on list\n",
17247c478bd9Sstevel@tonic-gate 		    fcn, clp->path);
17257c478bd9Sstevel@tonic-gate 	} else {
17267c478bd9Sstevel@tonic-gate 		*pp = clp->sib;
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	delete_unused_minor(hdp, clp->minor);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	clp->minor = NULL;
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	link_free(&clp);
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate static void
17377c478bd9Sstevel@tonic-gate delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	if (cmnp == NULL)
17407c478bd9Sstevel@tonic-gate 		return;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	if (cmnp->link != NULL)
17437c478bd9Sstevel@tonic-gate 		return;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	dprintf(DBG_STEP, "delete_unused_minor: removing minor(%s)\n",
17467c478bd9Sstevel@tonic-gate 	    cmnp->name);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	minor_delete(hdp, cmnp);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate int
17527c478bd9Sstevel@tonic-gate di_devlink_add_link(
17537c478bd9Sstevel@tonic-gate 	di_devlink_handle_t hdp,
17547c478bd9Sstevel@tonic-gate 	const char *link,
17557c478bd9Sstevel@tonic-gate 	const char *content,
17567c478bd9Sstevel@tonic-gate 	int flags)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	return (add_link(hdp, link, content, flags) != NULL ? 0 : -1);
17597c478bd9Sstevel@tonic-gate }
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate static cache_link_t *
17627c478bd9Sstevel@tonic-gate add_link(
17637c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
17647c478bd9Sstevel@tonic-gate 	const char *link,
17657c478bd9Sstevel@tonic-gate 	const char *content,
17667c478bd9Sstevel@tonic-gate 	int flags)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	uint32_t attr;
17697c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
17707c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
17717c478bd9Sstevel@tonic-gate 	const char *fcn = "add_link";
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	if (hdp == NULL || DB_ERR(hdp) || link == NULL ||
17747c478bd9Sstevel@tonic-gate 	    link[0] == '/' || content == NULL || !link_flag(flags) ||
17757c478bd9Sstevel@tonic-gate 	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
17767c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "%s: %s: invalid args\n",
17777c478bd9Sstevel@tonic-gate 		    fcn, link ? link : "<NULL>");
17787c478bd9Sstevel@tonic-gate 		errno = EINVAL;
17797c478bd9Sstevel@tonic-gate 		return (NULL);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	if ((clp = link_hash(hdp, link, 0)) != NULL) {
17837c478bd9Sstevel@tonic-gate 		if (link_cmp(clp, content, LINK_TYPE(flags)) != 0) {
17847c478bd9Sstevel@tonic-gate 			(void) rm_link(hdp, link);
17857c478bd9Sstevel@tonic-gate 		} else {
17867c478bd9Sstevel@tonic-gate 			return (clp);
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (TYPE_PRI(flags)) {
17917c478bd9Sstevel@tonic-gate 		const char *minor_path = NULL;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 		if (!is_minor_node(content, &minor_path)) {
17947c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "%s: invalid content(%s)"
17957c478bd9Sstevel@tonic-gate 			    " for primary link\n", fcn, content);
17967c478bd9Sstevel@tonic-gate 			errno = EINVAL;
17977c478bd9Sstevel@tonic-gate 			return (NULL);
17987c478bd9Sstevel@tonic-gate 		}
17997c478bd9Sstevel@tonic-gate 		if ((cmnp = lookup_minor(hdp, minor_path, NULL,
18007c478bd9Sstevel@tonic-gate 		    TYPE_CACHE|CREATE_FLAG)) == NULL) {
18017c478bd9Sstevel@tonic-gate 			return (NULL);
18027c478bd9Sstevel@tonic-gate 		}
18037c478bd9Sstevel@tonic-gate 		attr = A_PRIMARY;
18047c478bd9Sstevel@tonic-gate 	} else {
18057c478bd9Sstevel@tonic-gate 		/*
18067c478bd9Sstevel@tonic-gate 		 * Defer resolving a secondary link to a minor until the
18077c478bd9Sstevel@tonic-gate 		 * database is closed. This ensures that the primary link
18087c478bd9Sstevel@tonic-gate 		 * (required for a successful resolve) has also been created.
18097c478bd9Sstevel@tonic-gate 		 */
18107c478bd9Sstevel@tonic-gate 		cmnp = NULL;
18117c478bd9Sstevel@tonic-gate 		attr = A_SECONDARY;
18127c478bd9Sstevel@tonic-gate 	}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	return (link_insert(hdp, cmnp, link, content, attr));
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate /*
18187c478bd9Sstevel@tonic-gate  * Returns 0 on match or 1 otherwise.
18197c478bd9Sstevel@tonic-gate  */
18207c478bd9Sstevel@tonic-gate static int
18217c478bd9Sstevel@tonic-gate link_cmp(cache_link_t *clp, const char *content, int type)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	if (strcmp(clp->content, content) != 0)
18247c478bd9Sstevel@tonic-gate 		return (1);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (attr2type(clp->attr) != type)
18277c478bd9Sstevel@tonic-gate 		return (1);
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	return (0);
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate int
18337c478bd9Sstevel@tonic-gate di_devlink_update(di_devlink_handle_t hdp)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) {
18367c478bd9Sstevel@tonic-gate 		errno = EINVAL;
18377c478bd9Sstevel@tonic-gate 		return (-1);
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * Reset the counter to schedule a synchronization with /dev on the next
18427c478bd9Sstevel@tonic-gate 	 * di_devlink_close().
18437c478bd9Sstevel@tonic-gate 	 */
18447c478bd9Sstevel@tonic-gate 	CACHE(hdp)->update_count = 0;
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	return (0);
18477c478bd9Sstevel@tonic-gate }
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate static int
18507c478bd9Sstevel@tonic-gate synchronize_db(di_devlink_handle_t hdp)
18517c478bd9Sstevel@tonic-gate {
18527c478bd9Sstevel@tonic-gate 	int hval;
18537c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
18547c478bd9Sstevel@tonic-gate 	char pdup[PATH_MAX];
18557c478bd9Sstevel@tonic-gate 	recurse_t rec = {NULL};
18567c478bd9Sstevel@tonic-gate 	const char *fcn = "synchronize_db";
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	rec.data = NULL;
18597c478bd9Sstevel@tonic-gate 	rec.fcn = cache_dev_link;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	/*
18627c478bd9Sstevel@tonic-gate 	 * Walk through $ROOT/dev, reading every link and marking the
18637c478bd9Sstevel@tonic-gate 	 * corresponding cached version as valid(adding new links as needed).
18647c478bd9Sstevel@tonic-gate 	 * Then walk through the cache and remove all unmarked links.
18657c478bd9Sstevel@tonic-gate 	 */
18667c478bd9Sstevel@tonic-gate 	if (recurse_dev(hdp, &rec) != 0) {
18677c478bd9Sstevel@tonic-gate 		return (-1);
18687c478bd9Sstevel@tonic-gate 	}
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) {
18717c478bd9Sstevel@tonic-gate 		for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) {
18727c478bd9Sstevel@tonic-gate 			if (GET_VALID_ATTR(clp->attr)) {
18737c478bd9Sstevel@tonic-gate 				CLR_VALID_ATTR(clp->attr);
18747c478bd9Sstevel@tonic-gate 				clp = clp->hash;
18757c478bd9Sstevel@tonic-gate 				continue;
18767c478bd9Sstevel@tonic-gate 			}
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 			/*
18797c478bd9Sstevel@tonic-gate 			 * The link is stale, so remove it. Since the link
18807c478bd9Sstevel@tonic-gate 			 * will be destroyed, use a copy of the link path to
18817c478bd9Sstevel@tonic-gate 			 * invoke the remove function.
18827c478bd9Sstevel@tonic-gate 			 */
18837c478bd9Sstevel@tonic-gate 			(void) snprintf(pdup, sizeof (pdup), "%s", clp->path);
18847c478bd9Sstevel@tonic-gate 			clp = clp->hash;
18857c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_STEP, "%s: removing invalid link:"
18867c478bd9Sstevel@tonic-gate 			    " %s\n", fcn, pdup);
18877c478bd9Sstevel@tonic-gate 			(void) di_devlink_rm_link(hdp, pdup);
18887c478bd9Sstevel@tonic-gate 		}
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "%s: update completed\n", fcn);
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	return (0);
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate static di_devlink_handle_t
18977c478bd9Sstevel@tonic-gate di_devlink_init_impl(const char *root, const char *name, uint_t flags)
18987c478bd9Sstevel@tonic-gate {
18997c478bd9Sstevel@tonic-gate 	int	err = 0;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	if ((flags != 0 && flags != DI_MAKE_LINK) ||
19027c478bd9Sstevel@tonic-gate 	    (flags == 0 && name != NULL)) {
19037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19047c478bd9Sstevel@tonic-gate 		return (NULL);
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	if (flags == DI_MAKE_LINK && (err = devlink_create(root, name))) {
19087c478bd9Sstevel@tonic-gate 		errno = err;
19097c478bd9Sstevel@tonic-gate 		return (NULL);
19107c478bd9Sstevel@tonic-gate 	}
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "devlink_init_impl: success\n");
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	return (devlink_snapshot(root));
19157c478bd9Sstevel@tonic-gate }
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate di_devlink_handle_t
19187c478bd9Sstevel@tonic-gate di_devlink_init(const char *name, uint_t flags)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	return (di_devlink_init_impl("/", name, flags));
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate di_devlink_handle_t
19247c478bd9Sstevel@tonic-gate di_devlink_init_root(const char *root, const char *name, uint_t flags)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate 	return (di_devlink_init_impl(root, name, flags));
19277c478bd9Sstevel@tonic-gate }
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate static di_devlink_handle_t
19307c478bd9Sstevel@tonic-gate devlink_snapshot(const char *root_dir)
19317c478bd9Sstevel@tonic-gate {
19327c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp;
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) {
19357c478bd9Sstevel@tonic-gate 		return (NULL);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	/*
19397c478bd9Sstevel@tonic-gate 	 * If we cannot open the DB below, we will walk /dev
19407c478bd9Sstevel@tonic-gate 	 * in di_devlink_walk.
19417c478bd9Sstevel@tonic-gate 	 */
19427c478bd9Sstevel@tonic-gate 	(void) open_db(hdp, OPEN_RDONLY);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	return (hdp);
19457c478bd9Sstevel@tonic-gate }
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate int
19487c478bd9Sstevel@tonic-gate di_devlink_fini(di_devlink_handle_t *pp)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	if (pp == NULL || *pp == NULL || !HDL_RDONLY(*pp)) {
19517c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19527c478bd9Sstevel@tonic-gate 		return (-1);
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/* Freeing the handle also closes the DB */
19567c478bd9Sstevel@tonic-gate 	handle_free(pp);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	return (0);
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate int
19627c478bd9Sstevel@tonic-gate di_devlink_walk(
19637c478bd9Sstevel@tonic-gate 	di_devlink_handle_t hdp,
19647c478bd9Sstevel@tonic-gate 	const char *re,
19657c478bd9Sstevel@tonic-gate 	const char *minor_path,
19667c478bd9Sstevel@tonic-gate 	uint_t flags,
19677c478bd9Sstevel@tonic-gate 	void *arg,
19687c478bd9Sstevel@tonic-gate 	int (*devlink_callback)(di_devlink_t, void *))
19697c478bd9Sstevel@tonic-gate {
19707c478bd9Sstevel@tonic-gate 	int rv;
19717c478bd9Sstevel@tonic-gate 	regex_t reg;
19727c478bd9Sstevel@tonic-gate 	link_desc_t linkd = {NULL};
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp)) {
19757c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19767c478bd9Sstevel@tonic-gate 		return (-1);
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	linkd.minor_path = minor_path;
19807c478bd9Sstevel@tonic-gate 	linkd.flags = flags;
19817c478bd9Sstevel@tonic-gate 	linkd.arg = arg;
19827c478bd9Sstevel@tonic-gate 	linkd.fcn = devlink_callback;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	if (re) {
19857c478bd9Sstevel@tonic-gate 		if (regcomp(&reg, re, REG_EXTENDED) != 0)
19867c478bd9Sstevel@tonic-gate 			return (-1);
19877c478bd9Sstevel@tonic-gate 		linkd.regp = &reg;
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if (check_args(&linkd)) {
19917c478bd9Sstevel@tonic-gate 		errno = EINVAL;
19927c478bd9Sstevel@tonic-gate 		rv = -1;
19937c478bd9Sstevel@tonic-gate 		goto out;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	if (DB_OPEN(hdp)) {
19977c478bd9Sstevel@tonic-gate 		rv = walk_db(hdp, &linkd);
19987c478bd9Sstevel@tonic-gate 	} else {
19997c478bd9Sstevel@tonic-gate 		rv = walk_dev(hdp, &linkd);
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate out:
20037c478bd9Sstevel@tonic-gate 	if (re) {
20047c478bd9Sstevel@tonic-gate 		regfree(&reg);
20057c478bd9Sstevel@tonic-gate 	}
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	return (rv ? -1 : 0);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate static int
20117c478bd9Sstevel@tonic-gate link_flag(uint_t flags)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	if (flags != 0 && flags != DI_PRIMARY_LINK &&
20147c478bd9Sstevel@tonic-gate 	    flags != DI_SECONDARY_LINK) {
20157c478bd9Sstevel@tonic-gate 		return (0);
20167c478bd9Sstevel@tonic-gate 	}
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	return (1);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate /*
20227c478bd9Sstevel@tonic-gate  * Currently allowed flags are:
20237c478bd9Sstevel@tonic-gate  *	DI_PRIMARY_LINK
20247c478bd9Sstevel@tonic-gate  *	DI_SECONDARY_LINK
20257c478bd9Sstevel@tonic-gate  */
20267c478bd9Sstevel@tonic-gate static int
20277c478bd9Sstevel@tonic-gate check_args(link_desc_t *linkp)
20287c478bd9Sstevel@tonic-gate {
20297c478bd9Sstevel@tonic-gate 	if (linkp->fcn == NULL)
20307c478bd9Sstevel@tonic-gate 		return (-1);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	if (!link_flag(linkp->flags)) {
20337c478bd9Sstevel@tonic-gate 		return (-1);
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	/*
20377c478bd9Sstevel@tonic-gate 	 * Minor path can be NULL. In that case, all links will be
20387c478bd9Sstevel@tonic-gate 	 * selected.
20397c478bd9Sstevel@tonic-gate 	 */
20407c478bd9Sstevel@tonic-gate 	if (linkp->minor_path) {
20417c478bd9Sstevel@tonic-gate 		if (linkp->minor_path[0] != '/' ||
20427c478bd9Sstevel@tonic-gate 		    minor_colon(linkp->minor_path) == NULL) {
20437c478bd9Sstevel@tonic-gate 			return (-1);
20447c478bd9Sstevel@tonic-gate 		}
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	return (0);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate /*
20527c478bd9Sstevel@tonic-gate  * Walk all links in database if no minor path is specified.
20537c478bd9Sstevel@tonic-gate  */
20547c478bd9Sstevel@tonic-gate static int
20557c478bd9Sstevel@tonic-gate walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp)
20567c478bd9Sstevel@tonic-gate {
20577c478bd9Sstevel@tonic-gate 	assert(DB_OPEN(hdp));
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if (linkp->minor_path == NULL) {
20607c478bd9Sstevel@tonic-gate 		return (walk_all_links(hdp, linkp));
20617c478bd9Sstevel@tonic-gate 	} else {
20627c478bd9Sstevel@tonic-gate 		return (walk_matching_links(hdp, linkp));
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate static int
20677c478bd9Sstevel@tonic-gate cache_dev(struct di_devlink_handle *hdp)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	size_t sz;
20707c478bd9Sstevel@tonic-gate 	recurse_t rec = {NULL};
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	assert(hdp);
20737c478bd9Sstevel@tonic-gate 	assert(HDL_RDONLY(hdp));
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp)) {
20767c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "cache_dev: invalid arg\n");
20777c478bd9Sstevel@tonic-gate 		return (-1);
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	sz = MIN_HASH_SIZE;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *));
20837c478bd9Sstevel@tonic-gate 	if (CACHE(hdp)->hash == NULL) {
20847c478bd9Sstevel@tonic-gate 		return (-1);
20857c478bd9Sstevel@tonic-gate 	}
20867c478bd9Sstevel@tonic-gate 	CACHE(hdp)->hash_sz = sz;
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	rec.data = NULL;
20897c478bd9Sstevel@tonic-gate 	rec.fcn = cache_dev_link;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	return (recurse_dev(hdp, &rec));
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate static int
20957c478bd9Sstevel@tonic-gate walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp)
20967c478bd9Sstevel@tonic-gate {
20977c478bd9Sstevel@tonic-gate 	assert(hdp && linkp);
20987c478bd9Sstevel@tonic-gate 	assert(!DB_OPEN(hdp));
20997c478bd9Sstevel@tonic-gate 	assert(HDL_RDONLY(hdp));
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) {
21027c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "walk_dev: invalid args\n");
21037c478bd9Sstevel@tonic-gate 		return (-1);
21047c478bd9Sstevel@tonic-gate 	}
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 	if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) {
21077c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "walk_dev: /dev caching failed\n");
21087c478bd9Sstevel@tonic-gate 		return (-1);
21097c478bd9Sstevel@tonic-gate 	}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	if (linkp->minor_path)
21127c478bd9Sstevel@tonic-gate 		walk_cache_minor(hdp, linkp->minor_path, linkp);
21137c478bd9Sstevel@tonic-gate 	else
21147c478bd9Sstevel@tonic-gate 		walk_all_cache(hdp, linkp);
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	return (linkp->retval);
21177c478bd9Sstevel@tonic-gate }
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate /* ARGSUSED */
21207c478bd9Sstevel@tonic-gate static int
21217c478bd9Sstevel@tonic-gate cache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link)
21227c478bd9Sstevel@tonic-gate {
21237c478bd9Sstevel@tonic-gate 	int flags;
21247c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
21257c478bd9Sstevel@tonic-gate 	char content[PATH_MAX];
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	if (s_readlink(link, content, sizeof (content)) < 0) {
21307c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
21317c478bd9Sstevel@tonic-gate 	}
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	if (is_minor_node(content, NULL)) {
21347c478bd9Sstevel@tonic-gate 		flags = DI_PRIMARY_LINK;
21357c478bd9Sstevel@tonic-gate 	} else {
21367c478bd9Sstevel@tonic-gate 		flags = DI_SECONDARY_LINK;
21377c478bd9Sstevel@tonic-gate 	}
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0);
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 	/*
21427c478bd9Sstevel@tonic-gate 	 * Store only the part after <root-dir>/dev/
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	link += strlen(hdp->dev_dir) + 1;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	if ((clp = add_link(hdp, link, content, flags)) != NULL) {
21477c478bd9Sstevel@tonic-gate 		SET_VALID_ATTR(clp->attr);
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
21517c478bd9Sstevel@tonic-gate }
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate static int
21557c478bd9Sstevel@tonic-gate walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	struct db_link *dlp;
21587c478bd9Sstevel@tonic-gate 	uint32_t nidx, eidx;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	assert(DB_NUM(hdp, DB_LINK) >= 1);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	eidx = DB_NUM(hdp, DB_LINK);
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 	/* Skip the "NIL" (index == 0) link. */
21657c478bd9Sstevel@tonic-gate 	for (nidx = 1; nidx < eidx; nidx++) {
21667c478bd9Sstevel@tonic-gate 		/*
21677c478bd9Sstevel@tonic-gate 		 * Declare this local to the block with zero
21687c478bd9Sstevel@tonic-gate 		 * initializer so that it gets rezeroed
21697c478bd9Sstevel@tonic-gate 		 * for each iteration.
21707c478bd9Sstevel@tonic-gate 		 */
21717c478bd9Sstevel@tonic-gate 		struct di_devlink vlink = {NULL};
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 		if ((dlp = get_link(hdp, nidx)) == NULL)
21747c478bd9Sstevel@tonic-gate 			continue;
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate 		vlink.rel_path = get_string(hdp, dlp->path);
21777c478bd9Sstevel@tonic-gate 		vlink.content = get_string(hdp, dlp->content);
21787c478bd9Sstevel@tonic-gate 		vlink.type = attr2type(dlp->attr);
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) {
21817c478bd9Sstevel@tonic-gate 			break;
21827c478bd9Sstevel@tonic-gate 		}
21837c478bd9Sstevel@tonic-gate 	}
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	return (linkp->retval);
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate static int
21897c478bd9Sstevel@tonic-gate walk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
21907c478bd9Sstevel@tonic-gate {
21917c478bd9Sstevel@tonic-gate 	uint32_t nidx;
21927c478bd9Sstevel@tonic-gate 	struct db_link *dlp;
21937c478bd9Sstevel@tonic-gate 	struct db_minor *dmp;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	assert(linkp->minor_path != NULL);
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/*
22007c478bd9Sstevel@tonic-gate 	 * If a minor matching the path exists, walk that minor's devlinks list.
22017c478bd9Sstevel@tonic-gate 	 * Then walk the dangling devlinks list. Non-matching devlinks will be
22027c478bd9Sstevel@tonic-gate 	 * filtered out in visit_link.
22037c478bd9Sstevel@tonic-gate 	 */
22047c478bd9Sstevel@tonic-gate 	for (;;) {
22057c478bd9Sstevel@tonic-gate 		nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx;
22067c478bd9Sstevel@tonic-gate 		for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
22077c478bd9Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 			vlink.rel_path = get_string(hdp, dlp->path);
22107c478bd9Sstevel@tonic-gate 			vlink.content = get_string(hdp, dlp->content);
22117c478bd9Sstevel@tonic-gate 			vlink.type = attr2type(dlp->attr);
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE)
22147c478bd9Sstevel@tonic-gate 				goto out;
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 		if (dmp == NULL) {
22177c478bd9Sstevel@tonic-gate 			break;
22187c478bd9Sstevel@tonic-gate 		} else {
22197c478bd9Sstevel@tonic-gate 			dmp = NULL;
22207c478bd9Sstevel@tonic-gate 		}
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate out:
22247c478bd9Sstevel@tonic-gate 	return (linkp->retval);
22257c478bd9Sstevel@tonic-gate }
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate static int
22287c478bd9Sstevel@tonic-gate visit_link(
22297c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
22307c478bd9Sstevel@tonic-gate 	link_desc_t *linkp,
22317c478bd9Sstevel@tonic-gate 	struct di_devlink *vlp)
22327c478bd9Sstevel@tonic-gate {
22337c478bd9Sstevel@tonic-gate 	struct stat sbuf;
22347c478bd9Sstevel@tonic-gate 	const char *minor_path = NULL;
22357c478bd9Sstevel@tonic-gate 	char abs_path[PATH_MAX], cont[PATH_MAX];
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	/*
22387c478bd9Sstevel@tonic-gate 	 * It is legal for the link's content and type to be unknown.
22397c478bd9Sstevel@tonic-gate 	 * but one of absolute or relative path must be set.
22407c478bd9Sstevel@tonic-gate 	 */
22417c478bd9Sstevel@tonic-gate 	if (vlp->rel_path == NULL && vlp->abs_path == NULL) {
22427c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "visit_link: invalid arguments\n");
22437c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22447c478bd9Sstevel@tonic-gate 	}
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 	if (vlp->rel_path == NULL) {
22477c478bd9Sstevel@tonic-gate 		vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path);
22487c478bd9Sstevel@tonic-gate 		if (vlp->rel_path == NULL || vlp->rel_path[0] == '\0')
22497c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	if (linkp->regp) {
22537c478bd9Sstevel@tonic-gate 		if (regexec(linkp->regp, vlp->rel_path, 0, NULL, 0) != 0)
22547c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	if (vlp->abs_path == NULL) {
22587c478bd9Sstevel@tonic-gate 		assert(vlp->rel_path[0] != '/');
22597c478bd9Sstevel@tonic-gate 		(void) snprintf(abs_path, sizeof (abs_path), "%s/%s",
22607c478bd9Sstevel@tonic-gate 		    hdp->dev_dir, vlp->rel_path);
22617c478bd9Sstevel@tonic-gate 		vlp->abs_path = abs_path;
22627c478bd9Sstevel@tonic-gate 	}
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	if (vlp->content == NULL) {
22657c478bd9Sstevel@tonic-gate 		if (s_readlink(vlp->abs_path, cont, sizeof (cont)) < 0) {
22667c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
22677c478bd9Sstevel@tonic-gate 		}
22687c478bd9Sstevel@tonic-gate 		vlp->content = cont;
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	if (vlp->type == 0) {
22737c478bd9Sstevel@tonic-gate 		if (is_minor_node(vlp->content, &minor_path)) {
22747c478bd9Sstevel@tonic-gate 			vlp->type = DI_PRIMARY_LINK;
22757c478bd9Sstevel@tonic-gate 		} else {
22767c478bd9Sstevel@tonic-gate 			vlp->type = DI_SECONDARY_LINK;
22777c478bd9Sstevel@tonic-gate 		}
22787c478bd9Sstevel@tonic-gate 	}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * Filter based on minor path
22827c478bd9Sstevel@tonic-gate 	 */
22837c478bd9Sstevel@tonic-gate 	if (linkp->minor_path) {
22847c478bd9Sstevel@tonic-gate 		char tmp[PATH_MAX];
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 		/*
22877c478bd9Sstevel@tonic-gate 		 * derive minor path
22887c478bd9Sstevel@tonic-gate 		 */
22897c478bd9Sstevel@tonic-gate 		if (vlp->type == DI_SECONDARY_LINK) {
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate #ifdef	DEBUG
22927c478bd9Sstevel@tonic-gate 			/*LINTED*/
22937c478bd9Sstevel@tonic-gate 			assert(sizeof (tmp) >= PATH_MAX);
22947c478bd9Sstevel@tonic-gate #endif
22957c478bd9Sstevel@tonic-gate 			if (realpath(vlp->abs_path, tmp) == NULL)
22967c478bd9Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 			if (!is_minor_node(tmp, &minor_path))
22997c478bd9Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 		} else if (minor_path == NULL) {
23027c478bd9Sstevel@tonic-gate 			if (!is_minor_node(vlp->content, &minor_path))
23037c478bd9Sstevel@tonic-gate 				return (DI_WALK_CONTINUE);
23047c478bd9Sstevel@tonic-gate 		}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 		assert(minor_path != NULL);
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 		if (strcmp(linkp->minor_path, minor_path) != 0)
23097c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
23107c478bd9Sstevel@tonic-gate 	}
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	/*
23137c478bd9Sstevel@tonic-gate 	 * Filter based on link type
23147c478bd9Sstevel@tonic-gate 	 */
23157c478bd9Sstevel@tonic-gate 	if (!TYPE_NONE(linkp->flags) && LINK_TYPE(linkp->flags) != vlp->type) {
23167c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
23177c478bd9Sstevel@tonic-gate 	}
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	if (lstat(vlp->abs_path, &sbuf) < 0) {
23207c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "visit_link: %s: lstat failed: %s\n",
23217c478bd9Sstevel@tonic-gate 		    vlp->abs_path, strerror(errno));
23227c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	return (linkp->fcn(vlp, linkp->arg));
23267c478bd9Sstevel@tonic-gate }
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate static int
23297c478bd9Sstevel@tonic-gate devlink_valid(di_devlink_t devlink)
23307c478bd9Sstevel@tonic-gate {
23317c478bd9Sstevel@tonic-gate 	if (devlink == NULL || devlink->rel_path == NULL ||
23327c478bd9Sstevel@tonic-gate 	    devlink->abs_path == NULL || devlink->content == NULL ||
23337c478bd9Sstevel@tonic-gate 	    TYPE_NONE(devlink->type)) {
23347c478bd9Sstevel@tonic-gate 		return (0);
23357c478bd9Sstevel@tonic-gate 	}
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	return (1);
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate const char *
23417c478bd9Sstevel@tonic-gate di_devlink_path(di_devlink_t devlink)
23427c478bd9Sstevel@tonic-gate {
23437c478bd9Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23447c478bd9Sstevel@tonic-gate 		errno = EINVAL;
23457c478bd9Sstevel@tonic-gate 		return (NULL);
23467c478bd9Sstevel@tonic-gate 	}
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	return (devlink->abs_path);
23497c478bd9Sstevel@tonic-gate }
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate const char *
23527c478bd9Sstevel@tonic-gate di_devlink_content(di_devlink_t devlink)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23557c478bd9Sstevel@tonic-gate 		errno = EINVAL;
23567c478bd9Sstevel@tonic-gate 		return (NULL);
23577c478bd9Sstevel@tonic-gate 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	return (devlink->content);
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate int
23637c478bd9Sstevel@tonic-gate di_devlink_type(di_devlink_t devlink)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23667c478bd9Sstevel@tonic-gate 		errno = EINVAL;
23677c478bd9Sstevel@tonic-gate 		return (-1);
23687c478bd9Sstevel@tonic-gate 	}
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	return (devlink->type);
23717c478bd9Sstevel@tonic-gate }
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate di_devlink_t
23747c478bd9Sstevel@tonic-gate di_devlink_dup(di_devlink_t devlink)
23757c478bd9Sstevel@tonic-gate {
23767c478bd9Sstevel@tonic-gate 	struct di_devlink *duplink;
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	if (!devlink_valid(devlink)) {
23797c478bd9Sstevel@tonic-gate 		errno = EINVAL;
23807c478bd9Sstevel@tonic-gate 		return (NULL);
23817c478bd9Sstevel@tonic-gate 	}
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	if ((duplink = calloc(1, sizeof (struct di_devlink))) == NULL) {
23847c478bd9Sstevel@tonic-gate 		return (NULL);
23857c478bd9Sstevel@tonic-gate 	}
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	duplink->rel_path = strdup(devlink->rel_path);
23887c478bd9Sstevel@tonic-gate 	duplink->abs_path = strdup(devlink->abs_path);
23897c478bd9Sstevel@tonic-gate 	duplink->content  = strdup(devlink->content);
23907c478bd9Sstevel@tonic-gate 	duplink->type	  = devlink->type;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	if (!devlink_valid(duplink)) {
23937c478bd9Sstevel@tonic-gate 		(void) di_devlink_free(duplink);
23947c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
23957c478bd9Sstevel@tonic-gate 		return (NULL);
23967c478bd9Sstevel@tonic-gate 	}
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	return (duplink);
23997c478bd9Sstevel@tonic-gate }
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate int
24027c478bd9Sstevel@tonic-gate di_devlink_free(di_devlink_t devlink)
24037c478bd9Sstevel@tonic-gate {
24047c478bd9Sstevel@tonic-gate 	if (devlink == NULL) {
24057c478bd9Sstevel@tonic-gate 		errno = EINVAL;
24067c478bd9Sstevel@tonic-gate 		return (-1);
24077c478bd9Sstevel@tonic-gate 	}
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	free(devlink->rel_path);
24107c478bd9Sstevel@tonic-gate 	free(devlink->abs_path);
24117c478bd9Sstevel@tonic-gate 	free(devlink->content);
24127c478bd9Sstevel@tonic-gate 	free(devlink);
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	return (0);
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate /*
24187c478bd9Sstevel@tonic-gate  * Obtain path relative to dev_dir
24197c478bd9Sstevel@tonic-gate  */
24207c478bd9Sstevel@tonic-gate static const char *
24217c478bd9Sstevel@tonic-gate rel_path(struct di_devlink_handle *hdp, const char *path)
24227c478bd9Sstevel@tonic-gate {
24237c478bd9Sstevel@tonic-gate 	const size_t len = strlen(hdp->dev_dir);
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	if (strncmp(path, hdp->dev_dir, len) != 0)
24267c478bd9Sstevel@tonic-gate 		return (NULL);
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	if (path[len] == '\0')
24297c478bd9Sstevel@tonic-gate 		return (&path[len]);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	if (path[len] != '/')
24327c478bd9Sstevel@tonic-gate 		return (NULL);
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	return (&path[len+1]);
24357c478bd9Sstevel@tonic-gate }
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate static int
24387c478bd9Sstevel@tonic-gate recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp)
24397c478bd9Sstevel@tonic-gate {
24407c478bd9Sstevel@tonic-gate 	int ret = 0;
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	(void) do_recurse(hdp->dev_dir, hdp, rp, &ret);
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	return (ret);
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate static int
24487c478bd9Sstevel@tonic-gate do_recurse(
24497c478bd9Sstevel@tonic-gate 	const char *dir,
24507c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
24517c478bd9Sstevel@tonic-gate 	recurse_t *rp,
24527c478bd9Sstevel@tonic-gate 	int *retp)
24537c478bd9Sstevel@tonic-gate {
24547c478bd9Sstevel@tonic-gate 	size_t len;
24557c478bd9Sstevel@tonic-gate 	const char *rel;
24567c478bd9Sstevel@tonic-gate 	struct stat sbuf;
24577c478bd9Sstevel@tonic-gate 	char cur[PATH_MAX], *cp;
24587c478bd9Sstevel@tonic-gate 	int i, rv = DI_WALK_CONTINUE;
2459facf4a8dSllai 	finddevhdl_t handle;
2460facf4a8dSllai 	char *d_name;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	if ((rel = rel_path(hdp, dir)) == NULL)
24647c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	/*
24677c478bd9Sstevel@tonic-gate 	 * Skip directories we are not interested in.
24687c478bd9Sstevel@tonic-gate 	 */
24697c478bd9Sstevel@tonic-gate 	for (i = 0; i < N_SKIP_DIRS; i++) {
24707c478bd9Sstevel@tonic-gate 		if (strcmp(rel, skip_dirs[i]) == 0) {
24717c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_STEP, "do_recurse: skipping %s\n",
24727c478bd9Sstevel@tonic-gate 			    dir);
24737c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
24747c478bd9Sstevel@tonic-gate 		}
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "do_recurse: dir = %s\n", dir);
24787c478bd9Sstevel@tonic-gate 
2479facf4a8dSllai 	if (finddev_readdir(dir, &handle) != 0)
24807c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	(void) snprintf(cur, sizeof (cur), "%s/", dir);
24837c478bd9Sstevel@tonic-gate 	len = strlen(cur);
24847c478bd9Sstevel@tonic-gate 	cp = cur + len;
24857c478bd9Sstevel@tonic-gate 	len = sizeof (cur) - len;
24867c478bd9Sstevel@tonic-gate 
2487facf4a8dSllai 	for (;;) {
2488facf4a8dSllai 		if ((d_name = (char *)finddev_next(handle)) == NULL)
2489facf4a8dSllai 			break;
24907c478bd9Sstevel@tonic-gate 
2491facf4a8dSllai 		if (strlcpy(cp, d_name, len) >= len)
2492facf4a8dSllai 			break;
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate 		/*
24957c478bd9Sstevel@tonic-gate 		 * Skip files we are not interested in.
24967c478bd9Sstevel@tonic-gate 		 */
24977c478bd9Sstevel@tonic-gate 		for (i = 0; i < N_SKIP_FILES; i++) {
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 			rel = rel_path(hdp, cur);
25007c478bd9Sstevel@tonic-gate 			if (rel == NULL || strcmp(rel, skip_files[i]) == 0) {
25017c478bd9Sstevel@tonic-gate 				(void) dprintf(DBG_STEP,
25027c478bd9Sstevel@tonic-gate 				    "do_recurse: skipping %s\n", cur);
25037c478bd9Sstevel@tonic-gate 				goto next_entry;
25047c478bd9Sstevel@tonic-gate 			}
25057c478bd9Sstevel@tonic-gate 		}
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		if (lstat(cur, &sbuf) == 0) {
25087c478bd9Sstevel@tonic-gate 			if (S_ISDIR(sbuf.st_mode)) {
25097c478bd9Sstevel@tonic-gate 				rv = do_recurse(cur, hdp, rp, retp);
25107c478bd9Sstevel@tonic-gate 			} else if (S_ISLNK(sbuf.st_mode)) {
25117c478bd9Sstevel@tonic-gate 				rv = rp->fcn(hdp, rp->data, cur);
25127c478bd9Sstevel@tonic-gate 			} else {
25137c478bd9Sstevel@tonic-gate 				(void) dprintf(DBG_STEP,
25147c478bd9Sstevel@tonic-gate 				    "do_recurse: Skipping entry: %s\n", cur);
25157c478bd9Sstevel@tonic-gate 			}
25167c478bd9Sstevel@tonic-gate 		} else {
25177c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "do_recurse: cur(%s): lstat"
25187c478bd9Sstevel@tonic-gate 			    " failed: %s\n", cur, strerror(errno));
25197c478bd9Sstevel@tonic-gate 		}
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate next_entry:
25227c478bd9Sstevel@tonic-gate 		*cp = '\0';
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 		if (rv != DI_WALK_CONTINUE)
25257c478bd9Sstevel@tonic-gate 			break;
25267c478bd9Sstevel@tonic-gate 	}
25277c478bd9Sstevel@tonic-gate 
2528facf4a8dSllai 	finddev_close(handle);
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	return (rv);
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate static int
25357c478bd9Sstevel@tonic-gate check_attr(uint32_t attr)
25367c478bd9Sstevel@tonic-gate {
25377c478bd9Sstevel@tonic-gate 	switch (attr & A_LINK_TYPES) {
25387c478bd9Sstevel@tonic-gate 		case A_PRIMARY:
25397c478bd9Sstevel@tonic-gate 		case A_SECONDARY:
25407c478bd9Sstevel@tonic-gate 			return (1);
25417c478bd9Sstevel@tonic-gate 		default:
25427c478bd9Sstevel@tonic-gate 			dprintf(DBG_ERR, "check_attr: incorrect attr(%u)\n",
25437c478bd9Sstevel@tonic-gate 			    attr);
25447c478bd9Sstevel@tonic-gate 			return (0);
25457c478bd9Sstevel@tonic-gate 	}
25467c478bd9Sstevel@tonic-gate }
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate static int
25497c478bd9Sstevel@tonic-gate attr2type(uint32_t attr)
25507c478bd9Sstevel@tonic-gate {
25517c478bd9Sstevel@tonic-gate 	switch (attr & A_LINK_TYPES) {
25527c478bd9Sstevel@tonic-gate 		case A_PRIMARY:
25537c478bd9Sstevel@tonic-gate 			return (DI_PRIMARY_LINK);
25547c478bd9Sstevel@tonic-gate 		case A_SECONDARY:
25557c478bd9Sstevel@tonic-gate 			return (DI_SECONDARY_LINK);
25567c478bd9Sstevel@tonic-gate 		default:
25577c478bd9Sstevel@tonic-gate 			dprintf(DBG_ERR, "attr2type: incorrect attr(%u)\n",
25587c478bd9Sstevel@tonic-gate 			    attr);
25597c478bd9Sstevel@tonic-gate 			return (0);
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate }
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate /* Allocate new node and link it in */
25647c478bd9Sstevel@tonic-gate static cache_node_t *
25657c478bd9Sstevel@tonic-gate node_insert(
25667c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
25677c478bd9Sstevel@tonic-gate 	cache_node_t *pcnp,
25687c478bd9Sstevel@tonic-gate 	const char *path,
25697c478bd9Sstevel@tonic-gate 	int insert)
25707c478bd9Sstevel@tonic-gate {
25717c478bd9Sstevel@tonic-gate 	cache_node_t *cnp;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	if (path == NULL) {
25747c478bd9Sstevel@tonic-gate 		errno = EINVAL;
25757c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25767c478bd9Sstevel@tonic-gate 		return (NULL);
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	if ((cnp = calloc(1, sizeof (cache_node_t))) == NULL) {
25807c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25817c478bd9Sstevel@tonic-gate 		return (NULL);
25827c478bd9Sstevel@tonic-gate 	}
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	if ((cnp->path = strdup(path)) == NULL) {
25857c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
25867c478bd9Sstevel@tonic-gate 		free(cnp);
25877c478bd9Sstevel@tonic-gate 		return (NULL);
25887c478bd9Sstevel@tonic-gate 	}
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	cnp->parent = pcnp;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	if (pcnp == NULL) {
25937c478bd9Sstevel@tonic-gate 		assert(strcmp(path, "/") == 0);
25947c478bd9Sstevel@tonic-gate 		assert(CACHE(hdp)->root == NULL);
25957c478bd9Sstevel@tonic-gate 		CACHE(hdp)->root = cnp;
25967c478bd9Sstevel@tonic-gate 	} else if (insert == INSERT_HEAD) {
25977c478bd9Sstevel@tonic-gate 		cnp->sib = pcnp->child;
25987c478bd9Sstevel@tonic-gate 		pcnp->child = cnp;
25997c478bd9Sstevel@tonic-gate 	} else if (CACHE_LAST(hdp) && CACHE_LAST(hdp)->node &&
26007c478bd9Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node->parent == pcnp &&
26017c478bd9Sstevel@tonic-gate 	    CACHE_LAST(hdp)->node->sib == NULL) {
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 		CACHE_LAST(hdp)->node->sib = cnp;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	} else {
26067c478bd9Sstevel@tonic-gate 		cache_node_t **pp;
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 		for (pp = &pcnp->child; *pp != NULL; pp = &(*pp)->sib)
26097c478bd9Sstevel@tonic-gate 			;
26107c478bd9Sstevel@tonic-gate 		*pp = cnp;
26117c478bd9Sstevel@tonic-gate 	}
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 	return (cnp);
26147c478bd9Sstevel@tonic-gate }
26157c478bd9Sstevel@tonic-gate 
26167c478bd9Sstevel@tonic-gate /*
26177c478bd9Sstevel@tonic-gate  * Allocate a new minor and link it in either at the tail or head
26187c478bd9Sstevel@tonic-gate  * of the minor list depending on the value of "prev".
26197c478bd9Sstevel@tonic-gate  */
26207c478bd9Sstevel@tonic-gate static cache_minor_t *
26217c478bd9Sstevel@tonic-gate minor_insert(
26227c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
26237c478bd9Sstevel@tonic-gate 	cache_node_t *pcnp,
26247c478bd9Sstevel@tonic-gate 	const char *name,
26257c478bd9Sstevel@tonic-gate 	const char *nodetype,
26267c478bd9Sstevel@tonic-gate 	cache_minor_t **prev)
26277c478bd9Sstevel@tonic-gate {
26287c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	if (pcnp == NULL || name == NULL) {
26317c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26327c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26337c478bd9Sstevel@tonic-gate 		return (NULL);
26347c478bd9Sstevel@tonic-gate 	}
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	/*
26377c478bd9Sstevel@tonic-gate 	 * Some pseudo drivers don't specify nodetype. Assume pseudo if
26387c478bd9Sstevel@tonic-gate 	 * nodetype is not specified.
26397c478bd9Sstevel@tonic-gate 	 */
26407c478bd9Sstevel@tonic-gate 	if (nodetype == NULL)
26417c478bd9Sstevel@tonic-gate 		nodetype = DDI_PSEUDO;
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate 	if ((cmnp = calloc(1, sizeof (cache_minor_t))) == NULL) {
26447c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26457c478bd9Sstevel@tonic-gate 		return (NULL);
26467c478bd9Sstevel@tonic-gate 	}
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	cmnp->name = strdup(name);
26497c478bd9Sstevel@tonic-gate 	cmnp->nodetype = strdup(nodetype);
26507c478bd9Sstevel@tonic-gate 	if (cmnp->name == NULL || cmnp->nodetype == NULL) {
26517c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26527c478bd9Sstevel@tonic-gate 		free(cmnp->name);
26537c478bd9Sstevel@tonic-gate 		free(cmnp->nodetype);
26547c478bd9Sstevel@tonic-gate 		free(cmnp);
26557c478bd9Sstevel@tonic-gate 		return (NULL);
26567c478bd9Sstevel@tonic-gate 	}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	cmnp->node = pcnp;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/* Add to node's minor list */
26617c478bd9Sstevel@tonic-gate 	if (prev == NULL) {
26627c478bd9Sstevel@tonic-gate 		cmnp->sib = pcnp->minor;
26637c478bd9Sstevel@tonic-gate 		pcnp->minor = cmnp;
26647c478bd9Sstevel@tonic-gate 	} else {
26657c478bd9Sstevel@tonic-gate 		assert(*prev == NULL);
26667c478bd9Sstevel@tonic-gate 		*prev = cmnp;
26677c478bd9Sstevel@tonic-gate 	}
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate 	return (cmnp);
26707c478bd9Sstevel@tonic-gate }
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate static cache_link_t *
26737c478bd9Sstevel@tonic-gate link_insert(
26747c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
26757c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp,
26767c478bd9Sstevel@tonic-gate 	const char *path,
26777c478bd9Sstevel@tonic-gate 	const char *content,
26787c478bd9Sstevel@tonic-gate 	uint32_t attr)
26797c478bd9Sstevel@tonic-gate {
26807c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	if (path == NULL || content == NULL || !check_attr(attr)) {
26837c478bd9Sstevel@tonic-gate 		errno = EINVAL;
26847c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26857c478bd9Sstevel@tonic-gate 		return (NULL);
26867c478bd9Sstevel@tonic-gate 	}
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	if ((clp = calloc(1, sizeof (cache_link_t))) == NULL) {
26897c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26907c478bd9Sstevel@tonic-gate 		return (NULL);
26917c478bd9Sstevel@tonic-gate 	}
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	clp->path = strdup(path);
26947c478bd9Sstevel@tonic-gate 	clp->content = strdup(content);
26957c478bd9Sstevel@tonic-gate 	if (clp->path == NULL || clp->content == NULL) {
26967c478bd9Sstevel@tonic-gate 		SET_DB_ERR(hdp);
26977c478bd9Sstevel@tonic-gate 		link_free(&clp);
26987c478bd9Sstevel@tonic-gate 		return (NULL);
26997c478bd9Sstevel@tonic-gate 	}
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	clp->attr = attr;
27027c478bd9Sstevel@tonic-gate 	hash_insert(hdp, clp);
27037c478bd9Sstevel@tonic-gate 	clp->minor = cmnp;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	/* Add to minor's link list */
27067c478bd9Sstevel@tonic-gate 	if (cmnp != NULL) {
27077c478bd9Sstevel@tonic-gate 		clp->sib = cmnp->link;
27087c478bd9Sstevel@tonic-gate 		cmnp->link = clp;
27097c478bd9Sstevel@tonic-gate 	} else {
27107c478bd9Sstevel@tonic-gate 		clp->sib = CACHE(hdp)->dngl;
27117c478bd9Sstevel@tonic-gate 		CACHE(hdp)->dngl = clp;
27127c478bd9Sstevel@tonic-gate 	}
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	return (clp);
27157c478bd9Sstevel@tonic-gate }
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate static void
27187c478bd9Sstevel@tonic-gate hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp)
27197c478bd9Sstevel@tonic-gate {
27207c478bd9Sstevel@tonic-gate 	uint_t hval;
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	hval = hashfn(hdp, clp->path);
27237c478bd9Sstevel@tonic-gate 	clp->hash = CACHE_HASH(hdp, hval);
27247c478bd9Sstevel@tonic-gate 	CACHE_HASH(hdp, hval) = clp;
27257c478bd9Sstevel@tonic-gate }
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate static struct db_node *
27297c478bd9Sstevel@tonic-gate get_node(struct di_devlink_handle *hdp, uint32_t idx)
27307c478bd9Sstevel@tonic-gate {
27317c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_NODE));
27327c478bd9Sstevel@tonic-gate }
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate static struct db_node *
27357c478bd9Sstevel@tonic-gate set_node(struct di_devlink_handle *hdp, uint32_t idx)
27367c478bd9Sstevel@tonic-gate {
27377c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_NODE));
27387c478bd9Sstevel@tonic-gate }
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate static struct db_minor *
27417c478bd9Sstevel@tonic-gate get_minor(struct di_devlink_handle *hdp, uint32_t idx)
27427c478bd9Sstevel@tonic-gate {
27437c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_MINOR));
27447c478bd9Sstevel@tonic-gate }
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate static struct db_minor *
27477c478bd9Sstevel@tonic-gate set_minor(struct di_devlink_handle *hdp, uint32_t idx)
27487c478bd9Sstevel@tonic-gate {
27497c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_MINOR));
27507c478bd9Sstevel@tonic-gate }
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate static struct db_link *
27537c478bd9Sstevel@tonic-gate get_link(struct di_devlink_handle *hdp, uint32_t idx)
27547c478bd9Sstevel@tonic-gate {
27557c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_LINK));
27567c478bd9Sstevel@tonic-gate }
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate static struct db_link *
27597c478bd9Sstevel@tonic-gate set_link(struct di_devlink_handle *hdp, uint32_t idx)
27607c478bd9Sstevel@tonic-gate {
27617c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_LINK));
27627c478bd9Sstevel@tonic-gate }
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate static char *
27657c478bd9Sstevel@tonic-gate get_string(struct di_devlink_handle *hdp, uint32_t idx)
27667c478bd9Sstevel@tonic-gate {
27677c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ, DB_STR));
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate static char *
27717c478bd9Sstevel@tonic-gate set_string(struct di_devlink_handle *hdp, uint32_t idx)
27727c478bd9Sstevel@tonic-gate {
27737c478bd9Sstevel@tonic-gate 	return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_STR));
27747c478bd9Sstevel@tonic-gate }
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate /*
27787c478bd9Sstevel@tonic-gate  * Returns the element corresponding to idx. If the portion of file involved
27797c478bd9Sstevel@tonic-gate  * is not yet mapped, does an mmap() as well. Existing mappings are not changed.
27807c478bd9Sstevel@tonic-gate  */
27817c478bd9Sstevel@tonic-gate static void *
27827c478bd9Sstevel@tonic-gate map_seg(
27837c478bd9Sstevel@tonic-gate 	struct di_devlink_handle *hdp,
27847c478bd9Sstevel@tonic-gate 	uint32_t idx,
27857c478bd9Sstevel@tonic-gate 	int prot,
27867c478bd9Sstevel@tonic-gate 	db_seg_t seg)
27877c478bd9Sstevel@tonic-gate {
27887c478bd9Sstevel@tonic-gate 	int s;
27897c478bd9Sstevel@tonic-gate 	off_t off;
27907c478bd9Sstevel@tonic-gate 	size_t slen;
27917c478bd9Sstevel@tonic-gate 	caddr_t addr;
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	if (idx == DB_NIL) {
27947c478bd9Sstevel@tonic-gate 		return (NULL);
27957c478bd9Sstevel@tonic-gate 	}
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate 	if (!VALID_INDEX(hdp, seg, idx)) {
27987c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: seg(%d): invalid idx(%u)\n",
27997c478bd9Sstevel@tonic-gate 		    seg, idx);
28007c478bd9Sstevel@tonic-gate 		return (NULL);
28017c478bd9Sstevel@tonic-gate 	}
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	/*
28047c478bd9Sstevel@tonic-gate 	 * If the seg is already mapped in, use it if the access type is
28057c478bd9Sstevel@tonic-gate 	 * valid.
28067c478bd9Sstevel@tonic-gate 	 */
28077c478bd9Sstevel@tonic-gate 	if (DB_SEG(hdp, seg) != NULL) {
28087c478bd9Sstevel@tonic-gate 		if (DB_SEG_PROT(hdp, seg) != prot) {
28097c478bd9Sstevel@tonic-gate 			(void) dprintf(DBG_ERR, "map_seg: illegal access: "
28107c478bd9Sstevel@tonic-gate 			    "seg[%d]: idx=%u, seg_prot=%d, access=%d\n",
28117c478bd9Sstevel@tonic-gate 			    seg, idx, DB_SEG_PROT(hdp, seg), prot);
28127c478bd9Sstevel@tonic-gate 			return (NULL);
28137c478bd9Sstevel@tonic-gate 		}
28147c478bd9Sstevel@tonic-gate 		return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
28157c478bd9Sstevel@tonic-gate 	}
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	/*
28187c478bd9Sstevel@tonic-gate 	 * Segment is not mapped. Mmap() the segment.
28197c478bd9Sstevel@tonic-gate 	 */
28207c478bd9Sstevel@tonic-gate 	off = seg_size(hdp, DB_HEADER);
28217c478bd9Sstevel@tonic-gate 	for (s = 0; s < seg; s++) {
28227c478bd9Sstevel@tonic-gate 		off += seg_size(hdp, s);
28237c478bd9Sstevel@tonic-gate 	}
28247c478bd9Sstevel@tonic-gate 	slen = seg_size(hdp, seg);
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	addr = mmap(0, slen, prot, MAP_SHARED, DB(hdp)->db_fd, off);
28277c478bd9Sstevel@tonic-gate 	if (addr == MAP_FAILED) {
28287c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: seg[%d]: mmap failed: %s\n",
28297c478bd9Sstevel@tonic-gate 		    seg, strerror(errno));
28307c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "map_seg: args: len=%lu, prot=%d,"
28317c478bd9Sstevel@tonic-gate 		    " fd=%d, off=%ld\n", (ulong_t)slen, prot, DB(hdp)->db_fd,
28327c478bd9Sstevel@tonic-gate 		    off);
28337c478bd9Sstevel@tonic-gate 		return (NULL);
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	DB_SEG(hdp, seg) = addr;
28377c478bd9Sstevel@tonic-gate 	DB_SEG_PROT(hdp, seg) = prot;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_STEP, "map_seg: seg[%d]: len=%lu, prot=%d, fd=%d, "
28407c478bd9Sstevel@tonic-gate 	    "off=%ld, seg_base=%p\n", seg, (ulong_t)slen, prot, DB(hdp)->db_fd,
28417c478bd9Sstevel@tonic-gate 	    off, (void *)addr);
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 	return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
28447c478bd9Sstevel@tonic-gate }
28457c478bd9Sstevel@tonic-gate 
28467c478bd9Sstevel@tonic-gate /*
28477c478bd9Sstevel@tonic-gate  * Computes the size of a segment rounded up to the nearest page boundary.
28487c478bd9Sstevel@tonic-gate  */
28497c478bd9Sstevel@tonic-gate static size_t
28507c478bd9Sstevel@tonic-gate seg_size(struct di_devlink_handle *hdp, int seg)
28517c478bd9Sstevel@tonic-gate {
28527c478bd9Sstevel@tonic-gate 	size_t sz;
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	assert(DB_HDR(hdp)->page_sz);
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	if (seg == DB_HEADER) {
28577c478bd9Sstevel@tonic-gate 		sz = HDR_LEN;
28587c478bd9Sstevel@tonic-gate 	} else {
28597c478bd9Sstevel@tonic-gate 		assert(DB_NUM(hdp, seg) >= 1);
28607c478bd9Sstevel@tonic-gate 		sz = DB_NUM(hdp, seg) * elem_sizes[seg];
28617c478bd9Sstevel@tonic-gate 	}
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	sz = (sz / DB_HDR(hdp)->page_sz) + 1;
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	sz *= DB_HDR(hdp)->page_sz;
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	return (sz);
28687c478bd9Sstevel@tonic-gate }
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate static size_t
28717c478bd9Sstevel@tonic-gate size_db(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
28727c478bd9Sstevel@tonic-gate {
28737c478bd9Sstevel@tonic-gate 	int i;
28747c478bd9Sstevel@tonic-gate 	size_t sz;
28757c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	assert(page_sz > 0);
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 	/* Take "NIL" element into account */
28807c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
28817c478bd9Sstevel@tonic-gate 		count[i] = 1;
28827c478bd9Sstevel@tonic-gate 	}
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	count_node(CACHE(hdp)->root, count);
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	for (clp = CACHE(hdp)->dngl; clp != NULL; clp = clp->sib) {
28877c478bd9Sstevel@tonic-gate 		count_link(clp, count);
28887c478bd9Sstevel@tonic-gate 	}
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	sz = ((HDR_LEN / page_sz) + 1) * page_sz;
28917c478bd9Sstevel@tonic-gate 	for (i = 0; i < DB_TYPES; i++) {
28927c478bd9Sstevel@tonic-gate 		assert(count[i] >= 1);
28937c478bd9Sstevel@tonic-gate 		sz += (((count[i] * elem_sizes[i]) / page_sz) + 1) * page_sz;
28947c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_INFO, "N[%u]=%u\n", i, count[i]);
28957c478bd9Sstevel@tonic-gate 	}
28967c478bd9Sstevel@tonic-gate 	(void) dprintf(DBG_INFO, "DB size=%lu\n", (ulong_t)sz);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	return (sz);
28997c478bd9Sstevel@tonic-gate }
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate static void
29037c478bd9Sstevel@tonic-gate count_node(cache_node_t *cnp, uint32_t *count)
29047c478bd9Sstevel@tonic-gate {
29057c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	if (cnp == NULL)
29087c478bd9Sstevel@tonic-gate 		return;
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	count[DB_NODE]++;
29117c478bd9Sstevel@tonic-gate 	count_string(cnp->path, count);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) {
29147c478bd9Sstevel@tonic-gate 		count_minor(cmnp, count);
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate 	for (cnp = cnp->child; cnp != NULL; cnp = cnp->sib) {
29187c478bd9Sstevel@tonic-gate 		count_node(cnp, count);
29197c478bd9Sstevel@tonic-gate 	}
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate }
29227c478bd9Sstevel@tonic-gate 
29237c478bd9Sstevel@tonic-gate static void
29247c478bd9Sstevel@tonic-gate count_minor(cache_minor_t *cmnp, uint32_t *count)
29257c478bd9Sstevel@tonic-gate {
29267c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	if (cmnp == NULL)
29297c478bd9Sstevel@tonic-gate 		return;
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 	count[DB_MINOR]++;
29327c478bd9Sstevel@tonic-gate 	count_string(cmnp->name, count);
29337c478bd9Sstevel@tonic-gate 	count_string(cmnp->nodetype, count);
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	for (clp = cmnp->link; clp != NULL; clp = clp->sib) {
29367c478bd9Sstevel@tonic-gate 		count_link(clp, count);
29377c478bd9Sstevel@tonic-gate 	}
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate static void
29417c478bd9Sstevel@tonic-gate count_link(cache_link_t *clp, uint32_t *count)
29427c478bd9Sstevel@tonic-gate {
29437c478bd9Sstevel@tonic-gate 	if (clp == NULL)
29447c478bd9Sstevel@tonic-gate 		return;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	count[DB_LINK]++;
29477c478bd9Sstevel@tonic-gate 	count_string(clp->path, count);
29487c478bd9Sstevel@tonic-gate 	count_string(clp->content, count);
29497c478bd9Sstevel@tonic-gate }
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate static void
29537c478bd9Sstevel@tonic-gate count_string(const char *str, uint32_t *count)
29547c478bd9Sstevel@tonic-gate {
29557c478bd9Sstevel@tonic-gate 	if (str == NULL) {
29567c478bd9Sstevel@tonic-gate 		(void) dprintf(DBG_ERR, "count_string: NULL argument\n");
29577c478bd9Sstevel@tonic-gate 		return;
29587c478bd9Sstevel@tonic-gate 	}
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	count[DB_STR] += strlen(str) + 1;
29617c478bd9Sstevel@tonic-gate }
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate static uint_t
29647c478bd9Sstevel@tonic-gate hashfn(struct di_devlink_handle *hdp, const char *str)
29657c478bd9Sstevel@tonic-gate {
29667c478bd9Sstevel@tonic-gate 	const char *cp;
29677c478bd9Sstevel@tonic-gate 	ulong_t hval = 0;
29687c478bd9Sstevel@tonic-gate 
29697c478bd9Sstevel@tonic-gate 	if (str == NULL) {
29707c478bd9Sstevel@tonic-gate 		return (0);
29717c478bd9Sstevel@tonic-gate 	}
29727c478bd9Sstevel@tonic-gate 
29737c478bd9Sstevel@tonic-gate 	assert(CACHE(hdp)->hash_sz >= MIN_HASH_SIZE);
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	for (cp = str; *cp != '\0'; cp++) {
29767c478bd9Sstevel@tonic-gate 		hval += *cp;
29777c478bd9Sstevel@tonic-gate 	}
29787c478bd9Sstevel@tonic-gate 
29797c478bd9Sstevel@tonic-gate 	return (hval % CACHE(hdp)->hash_sz);
29807c478bd9Sstevel@tonic-gate }
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate static int
29837c478bd9Sstevel@tonic-gate enter_update_lock(struct di_devlink_handle *hdp)
29847c478bd9Sstevel@tonic-gate {
29857c478bd9Sstevel@tonic-gate 	int i, fd, rv;
29867c478bd9Sstevel@tonic-gate 	struct flock lock;
29877c478bd9Sstevel@tonic-gate 	char lockfile[PATH_MAX];
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	assert(hdp->lock_fd < 0);
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 	get_db_path(hdp, DB_LOCK, lockfile, sizeof (lockfile));
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/*
29947c478bd9Sstevel@tonic-gate 	 * Record locks are per-process. Protect against multiple threads.
29957c478bd9Sstevel@tonic-gate 	 */
29967c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&update_mutex);
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	if ((fd = open(lockfile, O_RDWR|O_CREAT, DB_LOCK_PERMS)) < 0) {
29997c478bd9Sstevel@tonic-gate 		goto error;
30007c478bd9Sstevel@tonic-gate 	}
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
30037c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
30047c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
30057c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	i = 1;
30087c478bd9Sstevel@tonic-gate 	while ((rv = fcntl(fd, F_SETLKW, &lock)) == -1 && errno == EINTR) {
30097c478bd9Sstevel@tonic-gate 		if (i < MAX_LOCK_RETRY) {
30107c478bd9Sstevel@tonic-gate 			i++;
30117c478bd9Sstevel@tonic-gate 		} else {
30127c478bd9Sstevel@tonic-gate 			break;
30137c478bd9Sstevel@tonic-gate 		}
30147c478bd9Sstevel@tonic-gate 	}
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	if (rv == 0) {
30177c478bd9Sstevel@tonic-gate 		hdp->lock_fd = fd;
30187c478bd9Sstevel@tonic-gate 		return (0);
30197c478bd9Sstevel@tonic-gate 	} else {
30207c478bd9Sstevel@tonic-gate 		(void) close(fd);
30217c478bd9Sstevel@tonic-gate 	}
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate error:
30247c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&update_mutex);
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	dprintf(DBG_ERR, "lockfile(%s): lock failed: %s\n", lockfile,
30277c478bd9Sstevel@tonic-gate 	    strerror(errno));
30287c478bd9Sstevel@tonic-gate 	return (-1);
30297c478bd9Sstevel@tonic-gate }
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate /*
30327c478bd9Sstevel@tonic-gate  * Close and re-open lock file every time so that it is recreated if deleted.
30337c478bd9Sstevel@tonic-gate  */
30347c478bd9Sstevel@tonic-gate static void
30357c478bd9Sstevel@tonic-gate exit_update_lock(struct di_devlink_handle *hdp)
30367c478bd9Sstevel@tonic-gate {
30377c478bd9Sstevel@tonic-gate 	struct flock unlock;
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 	if (hdp->lock_fd < 0) {
30407c478bd9Sstevel@tonic-gate 		return;
30417c478bd9Sstevel@tonic-gate 	}
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
30447c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
30457c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
30467c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	if (fcntl(hdp->lock_fd, F_SETLK, &unlock) == -1) {
30497c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "update lockfile: unlock failed: %s\n",
30507c478bd9Sstevel@tonic-gate 		    strerror(errno));
30517c478bd9Sstevel@tonic-gate 	}
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	(void) close(hdp->lock_fd);
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	hdp->lock_fd = -1;
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&update_mutex);
30587c478bd9Sstevel@tonic-gate }
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate /*
30617c478bd9Sstevel@tonic-gate  * returns 1 if contents is a minor node in /devices.
30627c478bd9Sstevel@tonic-gate  * If mn_root is not NULL, mn_root is set to:
30637c478bd9Sstevel@tonic-gate  *	if contents is a /dev node, mn_root = contents
3064facf4a8dSllai  *			OR
30657c478bd9Sstevel@tonic-gate  *	if contents is a /devices node, mn_root set to the '/'
30667c478bd9Sstevel@tonic-gate  *	following /devices.
30677c478bd9Sstevel@tonic-gate  */
30687c478bd9Sstevel@tonic-gate int
30697c478bd9Sstevel@tonic-gate is_minor_node(const char *contents, const char **mn_root)
30707c478bd9Sstevel@tonic-gate {
30717c478bd9Sstevel@tonic-gate 	char *ptr, *prefix;
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	prefix = "../devices/";
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 	if ((ptr = strstr(contents, prefix)) != NULL) {
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate 		/* mn_root should point to the / following /devices */
30787c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
30797c478bd9Sstevel@tonic-gate 			*mn_root = ptr += strlen(prefix) - 1;
30807c478bd9Sstevel@tonic-gate 		}
30817c478bd9Sstevel@tonic-gate 		return (1);
30827c478bd9Sstevel@tonic-gate 	}
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 	prefix = "/devices/";
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	if (strncmp(contents, prefix, strlen(prefix)) == 0) {
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 		/* mn_root should point to the / following /devices/ */
30897c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
30907c478bd9Sstevel@tonic-gate 			*mn_root = contents + strlen(prefix) - 1;
30917c478bd9Sstevel@tonic-gate 		}
30927c478bd9Sstevel@tonic-gate 		return (1);
30937c478bd9Sstevel@tonic-gate 	}
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	if (mn_root != NULL) {
30967c478bd9Sstevel@tonic-gate 		*mn_root = contents;
30977c478bd9Sstevel@tonic-gate 	}
30987c478bd9Sstevel@tonic-gate 	return (0);
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate static int
31027c478bd9Sstevel@tonic-gate s_readlink(const char *link, char *buf, size_t blen)
31037c478bd9Sstevel@tonic-gate {
31047c478bd9Sstevel@tonic-gate 	int rv;
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	if ((rv = readlink(link, buf, blen)) == -1)
31077c478bd9Sstevel@tonic-gate 		goto bad;
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	if (rv >= blen && buf[blen - 1] != '\0') {
31107c478bd9Sstevel@tonic-gate 		errno = ENAMETOOLONG;
31117c478bd9Sstevel@tonic-gate 		goto bad;
31127c478bd9Sstevel@tonic-gate 	} else if (rv < blen) {
31137c478bd9Sstevel@tonic-gate 		buf[rv] = '\0';
31147c478bd9Sstevel@tonic-gate 	}
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	return (0);
31177c478bd9Sstevel@tonic-gate bad:
31187c478bd9Sstevel@tonic-gate 	dprintf(DBG_ERR, "s_readlink: %s: failed: %s\n",
31197c478bd9Sstevel@tonic-gate 	    link, strerror(errno));
31207c478bd9Sstevel@tonic-gate 	return (-1);
31217c478bd9Sstevel@tonic-gate }
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate /*
31247c478bd9Sstevel@tonic-gate  * Synchronous link creation interface routines
31257c478bd9Sstevel@tonic-gate  * The scope of the operation is determined by the "name" arg.
31267c478bd9Sstevel@tonic-gate  * "name" can be NULL, a driver name or a devfs pathname (without /devices)
31277c478bd9Sstevel@tonic-gate  *
31287c478bd9Sstevel@tonic-gate  *	"name"				creates
31297c478bd9Sstevel@tonic-gate  *	======				=======
31307c478bd9Sstevel@tonic-gate  *
31317c478bd9Sstevel@tonic-gate  *	NULL		=>		All devlinks in system
31327c478bd9Sstevel@tonic-gate  *	<driver>	=>		devlinks for named driver
31337c478bd9Sstevel@tonic-gate  *	/pci@1		=>		devlinks for subtree rooted at pci@1
31347c478bd9Sstevel@tonic-gate  *	/pseudo/foo@0:X	=>		devlinks for minor X
31357c478bd9Sstevel@tonic-gate  *
31367c478bd9Sstevel@tonic-gate  * devlink_create() returns 0 on success or an errno value on failure
31377c478bd9Sstevel@tonic-gate  */
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate #define	MAX_DAEMON_ATTEMPTS 2
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate static int
31427c478bd9Sstevel@tonic-gate devlink_create(const char *root, const char *name)
31437c478bd9Sstevel@tonic-gate {
31447c478bd9Sstevel@tonic-gate 	int i;
31457c478bd9Sstevel@tonic-gate 	struct dca_off dca;
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	assert(root);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	/*
31507c478bd9Sstevel@tonic-gate 	 * Convert name into arg for door_call
31517c478bd9Sstevel@tonic-gate 	 */
31527c478bd9Sstevel@tonic-gate 	if (dca_init(name, &dca) != 0)
31537c478bd9Sstevel@tonic-gate 		return (EINVAL);
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/*
31567c478bd9Sstevel@tonic-gate 	 * Attempt to use the daemon first
31577c478bd9Sstevel@tonic-gate 	 */
31587c478bd9Sstevel@tonic-gate 	i = 0;
31597c478bd9Sstevel@tonic-gate 	do {
31607c478bd9Sstevel@tonic-gate 		daemon_call(root, &dca);
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 		dprintf(DBG_INFO, "daemon_call() retval=%d\n", dca.dca_error);
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 		/*
31657c478bd9Sstevel@tonic-gate 		 * Retry only if door server isn't running
31667c478bd9Sstevel@tonic-gate 		 */
31677c478bd9Sstevel@tonic-gate 		if (dca.dca_error != ENOENT && dca.dca_error != EBADF) {
31687c478bd9Sstevel@tonic-gate 			return (dca.dca_error);
31697c478bd9Sstevel@tonic-gate 		}
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 		dca.dca_error = 0;
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 		/*
31747c478bd9Sstevel@tonic-gate 		 * To improve performance defer this check until the first
31757c478bd9Sstevel@tonic-gate 		 * failure. Safe to defer as door server checks perms.
31767c478bd9Sstevel@tonic-gate 		 */
31777c478bd9Sstevel@tonic-gate 		if (geteuid() != 0)
31787c478bd9Sstevel@tonic-gate 			return (EPERM);
31797c478bd9Sstevel@tonic-gate 	/*
31807c478bd9Sstevel@tonic-gate 	 * Daemon may not be running. Try to start it.
31817c478bd9Sstevel@tonic-gate 	 */
31827c478bd9Sstevel@tonic-gate 	} while ((++i < MAX_DAEMON_ATTEMPTS) && start_daemon(root) == 0);
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	dprintf(DBG_INFO, "devlink_create: can't start daemon\n");
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	assert(dca.dca_error == 0);
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 	/*
31897c478bd9Sstevel@tonic-gate 	 * If the daemon cannot be started execute the devfsadm command.
31907c478bd9Sstevel@tonic-gate 	 */
31917c478bd9Sstevel@tonic-gate 	exec_cmd(root, &dca);
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	return (dca.dca_error);
31947c478bd9Sstevel@tonic-gate }
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate /*
31977c478bd9Sstevel@tonic-gate  * The "name" member of "struct dca" contains data in the following order
31987c478bd9Sstevel@tonic-gate  *	root'\0'minor'\0'driver'\0'
31997c478bd9Sstevel@tonic-gate  * The root component is always present at offset 0 in the "name" field.
32007c478bd9Sstevel@tonic-gate  * The driver and minor are optional. If present they have a non-zero
32017c478bd9Sstevel@tonic-gate  * offset in the "name" member.
32027c478bd9Sstevel@tonic-gate  */
32037c478bd9Sstevel@tonic-gate static int
32047c478bd9Sstevel@tonic-gate dca_init(const char *name, struct dca_off *dcp)
32057c478bd9Sstevel@tonic-gate {
32067c478bd9Sstevel@tonic-gate 	char *cp;
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	dcp->dca_root = 0;
32097c478bd9Sstevel@tonic-gate 	dcp->dca_minor = 0;
32107c478bd9Sstevel@tonic-gate 	dcp->dca_driver = 0;
32117c478bd9Sstevel@tonic-gate 	dcp->dca_error = 0;
32127c478bd9Sstevel@tonic-gate 	dcp->dca_flags = 0;
32137c478bd9Sstevel@tonic-gate 	dcp->dca_name[0] = '\0';
32147c478bd9Sstevel@tonic-gate 
32157c478bd9Sstevel@tonic-gate 	name = name ? name : "/";
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	/*
32187c478bd9Sstevel@tonic-gate 	 *  Check if name is a driver name
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 	if (*name != '/') {
32217c478bd9Sstevel@tonic-gate 		(void) snprintf(dcp->dca_name, sizeof (dcp->dca_name),
32227c478bd9Sstevel@tonic-gate 		    "/ %s", name);
32237c478bd9Sstevel@tonic-gate 		dcp->dca_root = 0;
32247c478bd9Sstevel@tonic-gate 		*(dcp->dca_name + 1) = '\0';
32257c478bd9Sstevel@tonic-gate 		dcp->dca_driver = 2;
32267c478bd9Sstevel@tonic-gate 		return (0);
32277c478bd9Sstevel@tonic-gate 	}
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	(void) snprintf(dcp->dca_name, sizeof (dcp->dca_name), "%s", name);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	/*
32327c478bd9Sstevel@tonic-gate 	 * "/devices" not allowed in devfs pathname
32337c478bd9Sstevel@tonic-gate 	 */
32347c478bd9Sstevel@tonic-gate 	if (is_minor_node(name, NULL))
32357c478bd9Sstevel@tonic-gate 		return (-1);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	dcp->dca_root = 0;
32387c478bd9Sstevel@tonic-gate 	if (cp = strrchr(dcp->dca_name, ':')) {
32397c478bd9Sstevel@tonic-gate 		*cp++ = '\0';
32407c478bd9Sstevel@tonic-gate 		dcp->dca_minor = cp - dcp->dca_name;
32417c478bd9Sstevel@tonic-gate 	}
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	return (0);
32447c478bd9Sstevel@tonic-gate }
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 
32477c478bd9Sstevel@tonic-gate #define	DAEMON_STARTUP_TIME	1 /* 1 second. This may need to be adjusted */
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate static void
32507c478bd9Sstevel@tonic-gate daemon_call(const char *root, struct dca_off *dcp)
32517c478bd9Sstevel@tonic-gate {
32527c478bd9Sstevel@tonic-gate 	door_arg_t	arg;
32537c478bd9Sstevel@tonic-gate 	int		fd, door_error;
32547c478bd9Sstevel@tonic-gate 	sigset_t	oset, nset;
32557c478bd9Sstevel@tonic-gate 	char		synch_door[PATH_MAX];
3256facf4a8dSllai 	struct statvfs	svf;
3257facf4a8dSllai 	char		*prefix;
32587c478bd9Sstevel@tonic-gate 
3259facf4a8dSllai 	/*
3260facf4a8dSllai 	 * If readonly root, assume we are in install
3261facf4a8dSllai 	 */
3262facf4a8dSllai 	prefix =
3263facf4a8dSllai 	    (statvfs("/etc/dev", &svf) == 0 && (svf.f_flag & ST_RDONLY)) ?
3264facf4a8dSllai 		"/tmp" : (char *)root;
32657c478bd9Sstevel@tonic-gate 	(void) snprintf(synch_door, sizeof (synch_door),
3266facf4a8dSllai 	    "%s/etc/dev/%s", prefix, DEVFSADM_SYNCH_DOOR);
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate 	if ((fd = open(synch_door, O_RDONLY)) == -1) {
32697c478bd9Sstevel@tonic-gate 		dcp->dca_error = errno;
32707c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "open of %s failed: %s\n",
32717c478bd9Sstevel@tonic-gate 		    synch_door, strerror(errno));
32727c478bd9Sstevel@tonic-gate 		return;
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	arg.data_ptr = (char *)dcp;
32767c478bd9Sstevel@tonic-gate 	arg.data_size = sizeof (*dcp);
32777c478bd9Sstevel@tonic-gate 	arg.desc_ptr = NULL;
32787c478bd9Sstevel@tonic-gate 	arg.desc_num = 0;
32797c478bd9Sstevel@tonic-gate 	arg.rbuf = (char *)dcp;
32807c478bd9Sstevel@tonic-gate 	arg.rsize = sizeof (*dcp);
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate 	/*
32837c478bd9Sstevel@tonic-gate 	 * Block signals to this thread until door call
32847c478bd9Sstevel@tonic-gate 	 * completes.
32857c478bd9Sstevel@tonic-gate 	 */
32867c478bd9Sstevel@tonic-gate 	(void) sigfillset(&nset);
32877c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&oset);
32887c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &nset, &oset);
32897c478bd9Sstevel@tonic-gate 	if (door_call(fd, &arg)) {
32907c478bd9Sstevel@tonic-gate 		door_error = 1;
32917c478bd9Sstevel@tonic-gate 		dcp->dca_error = errno;
32927c478bd9Sstevel@tonic-gate 	}
32937c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_SETMASK, &oset, NULL);
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	(void) close(fd);
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	if (door_error)
32987c478bd9Sstevel@tonic-gate 		return;
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	assert(arg.data_ptr);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	/*LINTED*/
33037c478bd9Sstevel@tonic-gate 	dcp->dca_error = ((struct dca_off *)arg.data_ptr)->dca_error;
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 	/*
33067c478bd9Sstevel@tonic-gate 	 * The doors interface may return data in a different buffer
33077c478bd9Sstevel@tonic-gate 	 * If that happens, deallocate buffer via munmap()
33087c478bd9Sstevel@tonic-gate 	 */
33097c478bd9Sstevel@tonic-gate 	if (arg.rbuf != (char *)dcp)
33107c478bd9Sstevel@tonic-gate 		(void) munmap(arg.rbuf, arg.rsize);
33117c478bd9Sstevel@tonic-gate }
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate #define	DEVFSADM_PATH	"/usr/sbin/devfsadm"
33147c478bd9Sstevel@tonic-gate #define	DEVFSADM	"devfsadm"
33157c478bd9Sstevel@tonic-gate 
33167c478bd9Sstevel@tonic-gate #define	DEVFSADMD_PATH	"/usr/lib/devfsadm/devfsadmd"
33177c478bd9Sstevel@tonic-gate #define	DEVFSADM_DAEMON	"devfsadmd"
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate static int
33207c478bd9Sstevel@tonic-gate start_daemon(const char *root)
33217c478bd9Sstevel@tonic-gate {
33227c478bd9Sstevel@tonic-gate 	int rv, i = 0;
33237c478bd9Sstevel@tonic-gate 	char *argv[20];
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 	argv[i++] = DEVFSADM_DAEMON;
33267c478bd9Sstevel@tonic-gate 	if (strcmp(root, "/")) {
33277c478bd9Sstevel@tonic-gate 		argv[i++] = "-r";
33287c478bd9Sstevel@tonic-gate 		argv[i++] = (char *)root;
33297c478bd9Sstevel@tonic-gate 	}
33307c478bd9Sstevel@tonic-gate 	argv[i++] = NULL;
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	rv = do_exec(DEVFSADMD_PATH, argv);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	(void) sleep(DAEMON_STARTUP_TIME);
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	return (rv);
33377c478bd9Sstevel@tonic-gate }
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate static void
33407c478bd9Sstevel@tonic-gate exec_cmd(const char *root, struct dca_off *dcp)
33417c478bd9Sstevel@tonic-gate {
33427c478bd9Sstevel@tonic-gate 	int i;
33437c478bd9Sstevel@tonic-gate 	char *argv[20];
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	i = 0;
33467c478bd9Sstevel@tonic-gate 	argv[i++] = DEVFSADM;
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	/*
33497c478bd9Sstevel@tonic-gate 	 * Load drivers only if -i is specified
33507c478bd9Sstevel@tonic-gate 	 */
33517c478bd9Sstevel@tonic-gate 	if (dcp->dca_driver) {
33527c478bd9Sstevel@tonic-gate 		argv[i++] = "-i";
33537c478bd9Sstevel@tonic-gate 		argv[i++] = &dcp->dca_name[dcp->dca_driver];
33547c478bd9Sstevel@tonic-gate 	} else {
33557c478bd9Sstevel@tonic-gate 		argv[i++] = "-n";
33567c478bd9Sstevel@tonic-gate 	}
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	if (root != NULL && strcmp(root, "/") != 0) {
33597c478bd9Sstevel@tonic-gate 		argv[i++] = "-r";
33607c478bd9Sstevel@tonic-gate 		argv[i++] = (char *)root;
33617c478bd9Sstevel@tonic-gate 	}
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	argv[i] = NULL;
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	if (do_exec(DEVFSADM_PATH, argv))
33667c478bd9Sstevel@tonic-gate 		dcp->dca_error = errno;
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate static int
33707c478bd9Sstevel@tonic-gate do_exec(const char *path, char *const argv[])
33717c478bd9Sstevel@tonic-gate {
33727c478bd9Sstevel@tonic-gate 	int i;
33737c478bd9Sstevel@tonic-gate 	pid_t cpid;
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate #ifdef	DEBUG
33767c478bd9Sstevel@tonic-gate 	dprintf(DBG_INFO, "Executing %s\n\tArgument list:", path);
33777c478bd9Sstevel@tonic-gate 	for (i = 0; argv[i] != NULL; i++) {
33787c478bd9Sstevel@tonic-gate 		dprintf(DBG_INFO, " %s", argv[i]);
33797c478bd9Sstevel@tonic-gate 	}
33807c478bd9Sstevel@tonic-gate 	dprintf(DBG_INFO, "\n");
33817c478bd9Sstevel@tonic-gate #endif
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	if ((cpid = fork1()) == -1) {
33847c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "fork1 failed: %s\n", strerror(errno));
33857c478bd9Sstevel@tonic-gate 		return (-1);
33867c478bd9Sstevel@tonic-gate 	}
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if (cpid == 0) { /* child process */
33897c478bd9Sstevel@tonic-gate 		int fd;
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 		if ((fd = open("/dev/null", O_RDWR)) >= 0) {
33927c478bd9Sstevel@tonic-gate 			(void) dup2(fd, fileno(stdout));
33937c478bd9Sstevel@tonic-gate 			(void) dup2(fd, fileno(stderr));
33947c478bd9Sstevel@tonic-gate 			(void) close(fd);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 			(void) execv(path, argv);
33977c478bd9Sstevel@tonic-gate 		} else {
33987c478bd9Sstevel@tonic-gate 			dprintf(DBG_ERR, "open of /dev/null failed: %s\n",
33997c478bd9Sstevel@tonic-gate 			    strerror(errno));
34007c478bd9Sstevel@tonic-gate 		}
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate 		_exit(-1);
34037c478bd9Sstevel@tonic-gate 	}
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	/* Parent process */
34067c478bd9Sstevel@tonic-gate 	if (waitpid(cpid, &i, 0) == cpid) {
34077c478bd9Sstevel@tonic-gate 		if (WIFEXITED(i)) {
34087c478bd9Sstevel@tonic-gate 			if (WEXITSTATUS(i) == 0) {
34097c478bd9Sstevel@tonic-gate 				dprintf(DBG_STEP,
34107c478bd9Sstevel@tonic-gate 				    "do_exec: child exited normally\n");
34117c478bd9Sstevel@tonic-gate 				return (0);
34127c478bd9Sstevel@tonic-gate 			} else
34137c478bd9Sstevel@tonic-gate 				errno = EINVAL;
34147c478bd9Sstevel@tonic-gate 		} else {
34157c478bd9Sstevel@tonic-gate 			/*
34167c478bd9Sstevel@tonic-gate 			 * The child was interrupted by a signal
34177c478bd9Sstevel@tonic-gate 			 */
34187c478bd9Sstevel@tonic-gate 			errno = EINTR;
34197c478bd9Sstevel@tonic-gate 		}
34207c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "child terminated abnormally: %s\n",
34217c478bd9Sstevel@tonic-gate 		    strerror(errno));
34227c478bd9Sstevel@tonic-gate 	} else {
34237c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "waitpid failed: %s\n", strerror(errno));
34247c478bd9Sstevel@tonic-gate 	}
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 	return (-1);
34277c478bd9Sstevel@tonic-gate }
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate static int
34307c478bd9Sstevel@tonic-gate walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp, link_desc_t *linkp)
34317c478bd9Sstevel@tonic-gate {
34327c478bd9Sstevel@tonic-gate 	int i;
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	dprintf(DBG_INFO, "walk_cache_links: initial link: %s\n",
34377c478bd9Sstevel@tonic-gate 	    clp ? clp->path : "<NULL>");
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	/*
34407c478bd9Sstevel@tonic-gate 	 * First search the links under the specified minor. On the
34417c478bd9Sstevel@tonic-gate 	 * 2nd pass, search the dangling list - secondary links may
34427c478bd9Sstevel@tonic-gate 	 * exist on this list since they are not resolved during the
34437c478bd9Sstevel@tonic-gate 	 * /dev walk.
34447c478bd9Sstevel@tonic-gate 	 */
34457c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++) {
34467c478bd9Sstevel@tonic-gate 		for (; clp != NULL; clp = clp->sib) {
34477c478bd9Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 			assert(clp->path[0] != '/');
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 			vlink.rel_path = clp->path;
34527c478bd9Sstevel@tonic-gate 			vlink.content = clp->content;
34537c478bd9Sstevel@tonic-gate 			vlink.type = attr2type(clp->attr);
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink)
34567c478bd9Sstevel@tonic-gate 			    != DI_WALK_CONTINUE) {
34577c478bd9Sstevel@tonic-gate 				dprintf(DBG_INFO, "walk_cache_links: "
34587c478bd9Sstevel@tonic-gate 				    "terminating at link: %s\n", clp->path);
34597c478bd9Sstevel@tonic-gate 				goto out;
34607c478bd9Sstevel@tonic-gate 			}
34617c478bd9Sstevel@tonic-gate 		}
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 		clp = CACHE(hdp)->dngl;
34647c478bd9Sstevel@tonic-gate 	}
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate out:
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	/* If i < 2, we terminated the walk prematurely */
34697c478bd9Sstevel@tonic-gate 	return (i < 2 ? DI_WALK_TERMINATE : DI_WALK_CONTINUE);
34707c478bd9Sstevel@tonic-gate }
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate static void
34737c478bd9Sstevel@tonic-gate walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp)
34747c478bd9Sstevel@tonic-gate {
34757c478bd9Sstevel@tonic-gate 	int i;
34767c478bd9Sstevel@tonic-gate 	cache_link_t *clp;
34777c478bd9Sstevel@tonic-gate 
34787c478bd9Sstevel@tonic-gate 	dprintf(DBG_INFO, "walk_all_cache: entered\n");
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	for (i = 0; i < CACHE(hdp)->hash_sz; i++) {
34817c478bd9Sstevel@tonic-gate 		clp = CACHE_HASH(hdp, i);
34827c478bd9Sstevel@tonic-gate 		for (; clp; clp = clp->hash) {
34837c478bd9Sstevel@tonic-gate 			struct di_devlink vlink = {NULL};
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 			assert(clp->path[0] != '/');
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 			vlink.rel_path = clp->path;
34887c478bd9Sstevel@tonic-gate 			vlink.content = clp->content;
34897c478bd9Sstevel@tonic-gate 			vlink.type = attr2type(clp->attr);
34907c478bd9Sstevel@tonic-gate 			if (visit_link(hdp, linkp, &vlink) !=
34917c478bd9Sstevel@tonic-gate 			    DI_WALK_CONTINUE) {
34927c478bd9Sstevel@tonic-gate 				dprintf(DBG_INFO, "walk_all_cache: terminating "
34937c478bd9Sstevel@tonic-gate 				    "walk at link: %s\n", clp->path);
34947c478bd9Sstevel@tonic-gate 				return;
34957c478bd9Sstevel@tonic-gate 			}
34967c478bd9Sstevel@tonic-gate 		}
34977c478bd9Sstevel@tonic-gate 	}
34987c478bd9Sstevel@tonic-gate }
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate static void
35017c478bd9Sstevel@tonic-gate walk_cache_minor(di_devlink_handle_t hdp, const char *mpath, link_desc_t *linkp)
35027c478bd9Sstevel@tonic-gate {
35037c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	assert(mpath);
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	if ((cmnp = lookup_minor(hdp, mpath, NULL, TYPE_CACHE)) != NULL) {
35087c478bd9Sstevel@tonic-gate 		(void) walk_cache_links(hdp, cmnp->link, linkp);
35097c478bd9Sstevel@tonic-gate 	} else {
35107c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "lookup minor failed: %s\n", mpath);
35117c478bd9Sstevel@tonic-gate 	}
35127c478bd9Sstevel@tonic-gate }
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate static void
35157c478bd9Sstevel@tonic-gate walk_cache_node(di_devlink_handle_t hdp, const char *path, link_desc_t *linkp)
35167c478bd9Sstevel@tonic-gate {
35177c478bd9Sstevel@tonic-gate 	cache_minor_t *cmnp;
35187c478bd9Sstevel@tonic-gate 	cache_node_t *cnp;
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 	assert(path);
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	if ((cnp = lookup_node(hdp, (char *)path, TYPE_CACHE)) == NULL) {
35237c478bd9Sstevel@tonic-gate 		dprintf(DBG_ERR, "lookup node failed: %s\n", path);
35247c478bd9Sstevel@tonic-gate 		return;
35257c478bd9Sstevel@tonic-gate 	}
35267c478bd9Sstevel@tonic-gate 
35277c478bd9Sstevel@tonic-gate 	for (cmnp = cnp->minor; cmnp != NULL; cmnp = cmnp->sib) {
35287c478bd9Sstevel@tonic-gate 		if (walk_cache_links(hdp, cmnp->link, linkp)
35297c478bd9Sstevel@tonic-gate 		    == DI_WALK_TERMINATE)
35307c478bd9Sstevel@tonic-gate 			break;
35317c478bd9Sstevel@tonic-gate 	}
35327c478bd9Sstevel@tonic-gate }
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate /*
35357c478bd9Sstevel@tonic-gate  * Private function
35367c478bd9Sstevel@tonic-gate  *
35377c478bd9Sstevel@tonic-gate  * Walk cached links corresponding to the given path.
35387c478bd9Sstevel@tonic-gate  *
35397c478bd9Sstevel@tonic-gate  * path		path to a node or minor node.
35407c478bd9Sstevel@tonic-gate  *
35417c478bd9Sstevel@tonic-gate  * flags	specifies the type of devlinks to be selected.
35427c478bd9Sstevel@tonic-gate  *		If DI_PRIMARY_LINK is used, only primary links are selected.
35437c478bd9Sstevel@tonic-gate  *		If DI_SECONDARY_LINK is specified, only secondary links
35447c478bd9Sstevel@tonic-gate  *		are selected.
35457c478bd9Sstevel@tonic-gate  *		If neither flag is specified, all devlinks are selected.
35467c478bd9Sstevel@tonic-gate  *
35477c478bd9Sstevel@tonic-gate  * re		An extended regular expression in regex(5) format which
35487c478bd9Sstevel@tonic-gate  *		selects the /dev links to be returned. The regular
35497c478bd9Sstevel@tonic-gate  *		expression should use link pathnames relative to
35507c478bd9Sstevel@tonic-gate  *		/dev. i.e. without the leading "/dev/" prefix.
35517c478bd9Sstevel@tonic-gate  *		A NULL value matches all devlinks.
35527c478bd9Sstevel@tonic-gate  */
35537c478bd9Sstevel@tonic-gate int
35547c478bd9Sstevel@tonic-gate di_devlink_cache_walk(di_devlink_handle_t hdp,
35557c478bd9Sstevel@tonic-gate 	const char *re,
35567c478bd9Sstevel@tonic-gate 	const char *path,
35577c478bd9Sstevel@tonic-gate 	uint_t flags,
35587c478bd9Sstevel@tonic-gate 	void *arg,
35597c478bd9Sstevel@tonic-gate 	int (*devlink_callback)(di_devlink_t, void *))
35607c478bd9Sstevel@tonic-gate {
35617c478bd9Sstevel@tonic-gate 	regex_t reg;
35627c478bd9Sstevel@tonic-gate 	link_desc_t linkd = {NULL};
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	if (hdp == NULL || path == NULL || !link_flag(flags) ||
35657c478bd9Sstevel@tonic-gate 	    !HDL_RDWR(hdp) || devlink_callback == NULL) {
35667c478bd9Sstevel@tonic-gate 		errno = EINVAL;
35677c478bd9Sstevel@tonic-gate 		return (-1);
35687c478bd9Sstevel@tonic-gate 	}
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 	linkd.flags = flags;
35717c478bd9Sstevel@tonic-gate 	linkd.arg = arg;
35727c478bd9Sstevel@tonic-gate 	linkd.fcn = devlink_callback;
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	if (re) {
35757c478bd9Sstevel@tonic-gate 		if (regcomp(&reg, re, REG_EXTENDED) != 0)
35767c478bd9Sstevel@tonic-gate 			return (-1);
35777c478bd9Sstevel@tonic-gate 		linkd.regp = &reg;
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	if (minor_colon(path) == NULL) {
35817c478bd9Sstevel@tonic-gate 		walk_cache_node(hdp, path, &linkd);
35827c478bd9Sstevel@tonic-gate 	} else {
35837c478bd9Sstevel@tonic-gate 		walk_cache_minor(hdp, path, &linkd);
35847c478bd9Sstevel@tonic-gate 	}
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 	if (re)
35877c478bd9Sstevel@tonic-gate 		regfree(&reg);
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	return (0);
35907c478bd9Sstevel@tonic-gate }
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate #define	DEBUG_ENV_VAR	"_DEVLINK_DEBUG"
35937c478bd9Sstevel@tonic-gate static int _devlink_debug = -1;
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate /*
35967c478bd9Sstevel@tonic-gate  * debug level is initialized to -1.
35977c478bd9Sstevel@tonic-gate  * On first call into this routine, debug level is set.
35987c478bd9Sstevel@tonic-gate  * If debug level is zero, debugging msgs are disabled.
35997c478bd9Sstevel@tonic-gate  */
36007c478bd9Sstevel@tonic-gate static void
36017c478bd9Sstevel@tonic-gate debug_print(debug_level_t msglevel, const char *fmt, va_list ap)
36027c478bd9Sstevel@tonic-gate {
36037c478bd9Sstevel@tonic-gate 	char	*cp;
36047c478bd9Sstevel@tonic-gate 	int	save;
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	/*
36077c478bd9Sstevel@tonic-gate 	 * We shouldn't be here if debug is disabled
36087c478bd9Sstevel@tonic-gate 	 */
36097c478bd9Sstevel@tonic-gate 	assert(_devlink_debug != 0);
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	/*
36127c478bd9Sstevel@tonic-gate 	 * Set debug level on first call into this routine
36137c478bd9Sstevel@tonic-gate 	 */
36147c478bd9Sstevel@tonic-gate 	if (_devlink_debug < 0) {
36157c478bd9Sstevel@tonic-gate 		if ((cp = getenv(DEBUG_ENV_VAR)) == NULL) {
36167c478bd9Sstevel@tonic-gate 			_devlink_debug = 0;
36177c478bd9Sstevel@tonic-gate 			return;
36187c478bd9Sstevel@tonic-gate 		}
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 		save = errno;
36217c478bd9Sstevel@tonic-gate 		errno = 0;
36227c478bd9Sstevel@tonic-gate 		_devlink_debug = strtol(cp, NULL, 10);
36237c478bd9Sstevel@tonic-gate 		if (errno != 0 || _devlink_debug < 0)  {
36247c478bd9Sstevel@tonic-gate 			_devlink_debug = 0;
36257c478bd9Sstevel@tonic-gate 			errno = save;
36267c478bd9Sstevel@tonic-gate 			return;
36277c478bd9Sstevel@tonic-gate 		}
36287c478bd9Sstevel@tonic-gate 		errno = save;
36297c478bd9Sstevel@tonic-gate 
36307c478bd9Sstevel@tonic-gate 		if (!_devlink_debug)
36317c478bd9Sstevel@tonic-gate 			return;
36327c478bd9Sstevel@tonic-gate 	}
36337c478bd9Sstevel@tonic-gate 
36347c478bd9Sstevel@tonic-gate 	/* debug msgs are enabled */
36357c478bd9Sstevel@tonic-gate 	assert(_devlink_debug > 0);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	if (_devlink_debug < msglevel)
36387c478bd9Sstevel@tonic-gate 		return;
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	/* Print a distinctive label for error msgs */
36427c478bd9Sstevel@tonic-gate 	if (msglevel == DBG_ERR) {
36437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "[ERROR]: ");
36447c478bd9Sstevel@tonic-gate 	}
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
36477c478bd9Sstevel@tonic-gate }
36487c478bd9Sstevel@tonic-gate 
36497c478bd9Sstevel@tonic-gate /* ARGSUSED */
36507c478bd9Sstevel@tonic-gate /* PRINTFLIKE2 */
36517c478bd9Sstevel@tonic-gate void
36527c478bd9Sstevel@tonic-gate dprintf(debug_level_t msglevel, const char *fmt, ...)
36537c478bd9Sstevel@tonic-gate {
36547c478bd9Sstevel@tonic-gate 	va_list ap;
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	assert(msglevel > 0);
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	if (!_devlink_debug)
36597c478bd9Sstevel@tonic-gate 		return;
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
36627c478bd9Sstevel@tonic-gate 	debug_print(msglevel, fmt, ap);
36637c478bd9Sstevel@tonic-gate 	va_end(ap);
36647c478bd9Sstevel@tonic-gate }
3665