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