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