17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5facf4a8llai * Common Development and Distribution License (the "License").
6facf4a8llai * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
2274ceea2Vikram Hegde * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
2448bbca8Daniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
27facf4a8llai#include "libdevinfo.h"
287c478bdstevel@tonic-gate#include "devinfo_devlink.h"
29ff2aee4cth#include "device_info.h"
307c478bdstevel@tonic-gate
31ff2aee4cth#undef	DEBUG
32ff2aee4cth#ifndef	DEBUG
337c478bdstevel@tonic-gate#define	NDEBUG 1
347c478bdstevel@tonic-gate#else
357c478bdstevel@tonic-gate#undef	NDEBUG
367c478bdstevel@tonic-gate#endif
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#include <assert.h>
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gatestatic mutex_t update_mutex = DEFAULTMUTEX; /* Protects update record lock */
41568e756vikramstatic mutex_t temp_file_mutex = DEFAULTMUTEX; /* for file creation tests */
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gatestatic const size_t elem_sizes[DB_TYPES] = {
447c478bdstevel@tonic-gate	sizeof (struct db_node),
457c478bdstevel@tonic-gate	sizeof (struct db_minor),
467c478bdstevel@tonic-gate	sizeof (struct db_link),
477c478bdstevel@tonic-gate	sizeof (char)
487c478bdstevel@tonic-gate};
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/*
517c478bdstevel@tonic-gate * List of directories/files skipped while physically walking /dev
527c478bdstevel@tonic-gate * Paths are relative to "<root>/dev/"
537c478bdstevel@tonic-gate */
547c478bdstevel@tonic-gatestatic const char *skip_dirs[] = {"fd"};
557c478bdstevel@tonic-gatestatic const char *skip_files[] = {
567c478bdstevel@tonic-gate	"stdout",
577c478bdstevel@tonic-gate	"stdin",
587c478bdstevel@tonic-gate	"stderr"
597c478bdstevel@tonic-gate};
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate#define	N_SKIP_DIRS	(sizeof (skip_dirs) / sizeof (skip_dirs[0]))
627c478bdstevel@tonic-gate#define	N_SKIP_FILES	(sizeof (skip_files) / sizeof (skip_files[0]))
637c478bdstevel@tonic-gate
64568e756vikram#define	DI_TEST_DB	ETCDEV "di_test_db"
65568e756vikram
667c478bdstevel@tonic-gate/*
677c478bdstevel@tonic-gate *
687c478bdstevel@tonic-gate * This file contains two sets of interfaces which operate on the reverse
697c478bdstevel@tonic-gate * links database. One set (which includes di_devlink_open()/_close())
707c478bdstevel@tonic-gate * allows link generators like devfsadm(1M) and ucblinks(1B) (writers) to
717c478bdstevel@tonic-gate * populate the database with /devices -> /dev mappings. Another set
727c478bdstevel@tonic-gate * of interfaces (which includes di_devlink_init()/_fini()) allows
737c478bdstevel@tonic-gate * applications (readers) to lookup the database for /dev links corresponding
747c478bdstevel@tonic-gate * to a given minor.
757c478bdstevel@tonic-gate *
767c478bdstevel@tonic-gate * Writers operate on a cached version of the database. The cache is created
777c478bdstevel@tonic-gate * when di_devlink_open() is called. As links in /dev are created and removed,
787c478bdstevel@tonic-gate * the cache is updated to keep it in synch with /dev. When the /dev updates
797c478bdstevel@tonic-gate * are complete, the link generator calls di_devlink_close() which writes
807c478bdstevel@tonic-gate * out the cache to the database.
817c478bdstevel@tonic-gate *
827c478bdstevel@tonic-gate * Applications which need to lookup the database, call di_devlink_init().
837c478bdstevel@tonic-gate * di_devlink_init() checks the database file (if one exists). If the
847c478bdstevel@tonic-gate * database is valid, it is mapped into the address space of the
857c478bdstevel@tonic-gate * application. The database file consists of several segments. Each
867c478bdstevel@tonic-gate * segment can be mapped in independently and is mapped on demand.
877c478bdstevel@tonic-gate *
887c478bdstevel@tonic-gate *		   Database Layout
897c478bdstevel@tonic-gate *
907c478bdstevel@tonic-gate *		---------------------
917c478bdstevel@tonic-gate *		|	Magic #     |
927c478bdstevel@tonic-gate *		| ----------------- |
937c478bdstevel@tonic-gate *		|       Version	    |	HEADER
947c478bdstevel@tonic-gate *		| ----------------- |
957c478bdstevel@tonic-gate *		|        ...        |
967c478bdstevel@tonic-gate *		---------------------
977c478bdstevel@tonic-gate *		|		    |
987c478bdstevel@tonic-gate *		|		    |	NODES
997c478bdstevel@tonic-gate *		|	            |
1007c478bdstevel@tonic-gate *		|		    |
1017c478bdstevel@tonic-gate *		---------------------
1027c478bdstevel@tonic-gate *		|		    |
1037c478bdstevel@tonic-gate *		|		    |	MINORS
1047c478bdstevel@tonic-gate *		|	            |
1057c478bdstevel@tonic-gate *		|		    |
1067c478bdstevel@tonic-gate *		---------------------
1077c478bdstevel@tonic-gate *		|		    |
1087c478bdstevel@tonic-gate *		|		    |   LINKS
1097c478bdstevel@tonic-gate *		|	            |
1107c478bdstevel@tonic-gate *		|		    |
1117c478bdstevel@tonic-gate *		---------------------
1127c478bdstevel@tonic-gate *		|		    |
1137c478bdstevel@tonic-gate *		|		    |	STRINGS
1147c478bdstevel@tonic-gate *		|	            |
1157c478bdstevel@tonic-gate *		|		    |
1167c478bdstevel@tonic-gate *		---------------------
1177c478bdstevel@tonic-gate *
1187c478bdstevel@tonic-gate * Readers can lookup /dev links for a specific minor or
1197c478bdstevel@tonic-gate * lookup all /dev links. In the latter case, the node
1207c478bdstevel@tonic-gate * and minor segments are not mapped in and the reader
1217c478bdstevel@tonic-gate * walks through every link in the link segment.
1227c478bdstevel@tonic-gate *
1237c478bdstevel@tonic-gate */
1247c478bdstevel@tonic-gatedi_devlink_handle_t
1257c478bdstevel@tonic-gatedi_devlink_open(const char *root_dir, uint_t flags)
1267c478bdstevel@tonic-gate{
1277c478bdstevel@tonic-gate	int err;
1287c478bdstevel@tonic-gate	char path[PATH_MAX];
1297c478bdstevel@tonic-gate	struct di_devlink_handle *hdp;
1307c478bdstevel@tonic-gate	int retried = 0;
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gateretry:
1337c478bdstevel@tonic-gate	/*
1347c478bdstevel@tonic-gate	 * Allocate a read-write handle but open the DB in readonly
1357c478bdstevel@tonic-gate	 * mode. We do writes only to a temporary copy of the database.
1367c478bdstevel@tonic-gate	 */
1377c478bdstevel@tonic-gate	if ((hdp = handle_alloc(root_dir, OPEN_RDWR)) == NULL) {
1387c478bdstevel@tonic-gate		return (NULL);
1397c478bdstevel@tonic-gate	}
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate	err = open_db(hdp, OPEN_RDONLY);
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate	/*
144f7b6b8ccth	 * We don't want to unlink the db at this point - if we did we
145f7b6b8ccth	 * would be creating a window where consumers would take a slow
146f7b6b8ccth	 * code path (and those consumers might also trigger requests for
147f7b6b8ccth	 * db creation, which we are already in the process of doing).
148f7b6b8ccth	 * When we are done with our update, we use rename to install the
149f7b6b8ccth	 * latest version of the db file.
1507c478bdstevel@tonic-gate	 */
1517c478bdstevel@tonic-gate	get_db_path(hdp, DB_FILE, path, sizeof (path));
1527c478bdstevel@tonic-gate
1537c478bdstevel@tonic-gate	/*
1547c478bdstevel@tonic-gate	 * The flags argument is reserved for future use.
1557c478bdstevel@tonic-gate	 */
1567c478bdstevel@tonic-gate	if (flags != 0) {
1577c478bdstevel@tonic-gate		handle_free(&hdp); /* also closes the DB */
1587c478bdstevel@tonic-gate		errno = EINVAL;
1597c478bdstevel@tonic-gate		return (NULL);
1607c478bdstevel@tonic-gate	}
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	if (cache_alloc(hdp) != 0) {
1637c478bdstevel@tonic-gate		handle_free(&hdp);
1647c478bdstevel@tonic-gate		return (NULL);
1657c478bdstevel@tonic-gate	}
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate	if (err) {
1687c478bdstevel@tonic-gate		/*
1697c478bdstevel@tonic-gate		 * Failed to open DB.
1707c478bdstevel@tonic-gate		 * The most likely cause is that DB file did not exist.
1717c478bdstevel@tonic-gate		 * Call di_devlink_close() to recreate the DB file and
1727c478bdstevel@tonic-gate		 * retry di_devlink_open().
1737c478bdstevel@tonic-gate		 */
1747c478bdstevel@tonic-gate		if (retried == 0) {
1757c478bdstevel@tonic-gate			(void) di_devlink_close(&hdp, 0);
1767c478bdstevel@tonic-gate			retried = 1;
1777c478bdstevel@tonic-gate			goto retry;
1787c478bdstevel@tonic-gate		}
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gate		/*
1817c478bdstevel@tonic-gate		 * DB cannot be opened, just return the
1827c478bdstevel@tonic-gate		 * handle. We will recreate the DB later.
1837c478bdstevel@tonic-gate		 */
1847c478bdstevel@tonic-gate		return (hdp);
1857c478bdstevel@tonic-gate	}
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gate	/* Read the database into the cache */
1887c478bdstevel@tonic-gate	CACHE(hdp)->update_count = DB_HDR(hdp)->update_count;
1897c478bdstevel@tonic-gate	(void) read_nodes(hdp, NULL, DB_HDR(hdp)->root_idx);
1907c478bdstevel@tonic-gate	(void) read_links(hdp, NULL, DB_HDR(hdp)->dngl_idx);
1917c478bdstevel@tonic-gate
1927c478bdstevel@tonic-gate	(void) close_db(hdp);
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	return (hdp);
1957c478bdstevel@tonic-gate}
1967c478bdstevel@tonic-gate
1977c478bdstevel@tonic-gatestatic void
1987c478bdstevel@tonic-gateget_db_path(
1997c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
2007c478bdstevel@tonic-gate	const char *fname,
2017c478bdstevel@tonic-gate	char *buf,
2027c478bdstevel@tonic-gate	size_t blen)
2037c478bdstevel@tonic-gate{
2047c478bdstevel@tonic-gate	char *dir = NULL;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate#ifdef	DEBUG
2077c478bdstevel@tonic-gate	if (dir = getenv(ALT_DB_DIR)) {
2087c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "get_db_path: alternate db dir: %s\n",
2097c478bdstevel@tonic-gate		    dir);
2107c478bdstevel@tonic-gate	}
2117c478bdstevel@tonic-gate#endif
2127c478bdstevel@tonic-gate	if (dir == NULL) {
213facf4a8llai		dir = hdp->db_dir;
2147c478bdstevel@tonic-gate	}
2157c478bdstevel@tonic-gate
2167c478bdstevel@tonic-gate	(void) snprintf(buf, blen, "%s/%s", dir, fname);
2177c478bdstevel@tonic-gate}
2187c478bdstevel@tonic-gate
2197c478bdstevel@tonic-gatestatic int
2207c478bdstevel@tonic-gateopen_db(struct di_devlink_handle *hdp, int flags)
2217c478bdstevel@tonic-gate{
2227c478bdstevel@tonic-gate	size_t sz;
2237c478bdstevel@tonic-gate	long page_sz;
2247c478bdstevel@tonic-gate	int fd, rv, flg;
2257c478bdstevel@tonic-gate	struct stat sbuf;
2267c478bdstevel@tonic-gate	uint32_t count[DB_TYPES] = {0};
2277c478bdstevel@tonic-gate	char path[PATH_MAX];
2287c478bdstevel@tonic-gate	void *cp;
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate	assert(!DB_OPEN(hdp));
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gate#ifdef	DEBUG
2337c478bdstevel@tonic-gate	if (getenv(SKIP_DB)) {
2347c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "open_db: skipping database\n");
2357c478bdstevel@tonic-gate		return (-1);
2367c478bdstevel@tonic-gate	}
2377c478bdstevel@tonic-gate#endif
2387c478bdstevel@tonic-gate	if ((page_sz = sysconf(_SC_PAGE_SIZE)) == -1) {
2397c478bdstevel@tonic-gate		return (-1);
2407c478bdstevel@tonic-gate	}
2417c478bdstevel@tonic-gate
2427c478bdstevel@tonic-gate	/*
2437c478bdstevel@tonic-gate	 * Use O_TRUNC flag for write access, so that the subsequent ftruncate()
2447c478bdstevel@tonic-gate	 * call will zero-fill the entire file
2457c478bdstevel@tonic-gate	 */
2467c478bdstevel@tonic-gate	if (IS_RDONLY(flags)) {
2477c478bdstevel@tonic-gate		flg = O_RDONLY;
2487c478bdstevel@tonic-gate		get_db_path(hdp, DB_FILE, path, sizeof (path));
2497c478bdstevel@tonic-gate	} else {
2507c478bdstevel@tonic-gate		flg = O_RDWR|O_CREAT|O_TRUNC;
2517c478bdstevel@tonic-gate		get_db_path(hdp, DB_TMP, path, sizeof (path));
2527c478bdstevel@tonic-gate	}
2537c478bdstevel@tonic-gate
254facf4a8llai	/*
255facf4a8llai	 * Avoid triggering /dev reconfigure for read when not present
256facf4a8llai	 */
257facf4a8llai	if (IS_RDONLY(flags) &&
258facf4a8llai	    (strncmp(path, "/dev/", 5) == 0) && !device_exists(path)) {
259facf4a8llai		return (-1);
260facf4a8llai	}
261facf4a8llai
2627c478bdstevel@tonic-gate	if ((fd = open(path, flg, DB_PERMS)) == -1) {
2637c478bdstevel@tonic-gate		return (-1);
2647c478bdstevel@tonic-gate	}
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	if (IS_RDONLY(flags)) {
2677c478bdstevel@tonic-gate		flg = PROT_READ;
2687c478bdstevel@tonic-gate		rv = fstat(fd, &sbuf);
2697c478bdstevel@tonic-gate		sz = sbuf.st_size;
2707c478bdstevel@tonic-gate	} else {
2717c478bdstevel@tonic-gate		flg = PROT_READ | PROT_WRITE;
2727c478bdstevel@tonic-gate		sz = size_db(hdp, page_sz, count);
2737c478bdstevel@tonic-gate		rv = ftruncate(fd, sz);
2747c478bdstevel@tonic-gate	}
2757c478bdstevel@tonic-gate
2767c478bdstevel@tonic-gate	if (rv == -1 || sz < HDR_LEN) {
2777c478bdstevel@tonic-gate		if (rv != -1)
2787c478bdstevel@tonic-gate			errno = EINVAL;
2797c478bdstevel@tonic-gate		(void) close(fd);
2807c478bdstevel@tonic-gate		return (-1);
2817c478bdstevel@tonic-gate	}
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate	cp = mmap(0, HDR_LEN, flg, MAP_SHARED, fd, 0);
2847c478bdstevel@tonic-gate	if (cp == MAP_FAILED) {
2857c478bdstevel@tonic-gate		(void) close(fd);
2867c478bdstevel@tonic-gate		return (-1);
2877c478bdstevel@tonic-gate	}
2887c478bdstevel@tonic-gate	DB(hdp)->hdr = (struct db_hdr *)cp;
2897c478bdstevel@tonic-gate	DB(hdp)->db_fd = fd;
2907c478bdstevel@tonic-gate	DB(hdp)->flags = flags;
2917c478bdstevel@tonic-gate
2927c478bdstevel@tonic-gate	if (IS_RDONLY(flags)) {
2937c478bdstevel@tonic-gate		rv = invalid_db(hdp, sz, page_sz);
2947c478bdstevel@tonic-gate	} else {
2957c478bdstevel@tonic-gate		rv = init_hdr(hdp, page_sz, count);
2967c478bdstevel@tonic-gate	}
2977c478bdstevel@tonic-gate
2987c478bdstevel@tonic-gate	if (rv) {
2997c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "open_db: invalid DB(%s)\n", path);
3007c478bdstevel@tonic-gate		(void) close_db(hdp);
3017c478bdstevel@tonic-gate		return (-1);
3027c478bdstevel@tonic-gate	} else {
3037c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "open_db: DB(%s): opened\n", path);
3047c478bdstevel@tonic-gate		return (0);
3057c478bdstevel@tonic-gate	}
3067c478bdstevel@tonic-gate}
3077c478bdstevel@tonic-gate
3087c478bdstevel@tonic-gate/*
3097c478bdstevel@tonic-gate * A handle can be allocated for read-only or read-write access
3107c478bdstevel@tonic-gate */
3117c478bdstevel@tonic-gatestatic struct di_devlink_handle *
3127c478bdstevel@tonic-gatehandle_alloc(const char *root_dir, uint_t flags)
3137c478bdstevel@tonic-gate{
314facf4a8llai	char dev_dir[PATH_MAX], path[PATH_MAX], db_dir[PATH_MAX];
3157c478bdstevel@tonic-gate	struct di_devlink_handle *hdp, proto = {0};
316568e756vikram	int install = 0;
317568e756vikram	int isroot = 0;
318568e756vikram	struct stat sb;
319568e756vikram	char can_path[PATH_MAX];
3207c478bdstevel@tonic-gate
3217c478bdstevel@tonic-gate	assert(flags == OPEN_RDWR || flags == OPEN_RDONLY);
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gate	dev_dir[0] = '\0';
324facf4a8llai	db_dir[0] = '\0';
3257c478bdstevel@tonic-gate
3267c478bdstevel@tonic-gate	/*
3277c478bdstevel@tonic-gate	 * NULL and the empty string are equivalent to "/"
3287c478bdstevel@tonic-gate	 */
3297c478bdstevel@tonic-gate	if (root_dir && root_dir[0] != '\0') {
3307c478bdstevel@tonic-gate
3317c478bdstevel@tonic-gate		if (root_dir[0] != '/') {
3327c478bdstevel@tonic-gate			errno = EINVAL;
3337c478bdstevel@tonic-gate			return (NULL);
3347c478bdstevel@tonic-gate		}
3357c478bdstevel@tonic-gate
3367c478bdstevel@tonic-gate#ifdef	DEBUG
3377c478bdstevel@tonic-gate		/*LINTED*/
3387c478bdstevel@tonic-gate		assert(sizeof (dev_dir) >= PATH_MAX);
3397c478bdstevel@tonic-gate#endif
340facf4a8llai		if ((realpath(root_dir, dev_dir) == NULL) ||
341facf4a8llai		    (realpath(root_dir, db_dir) == NULL)) {
3427c478bdstevel@tonic-gate			return (NULL);
3437c478bdstevel@tonic-gate		}
344568e756vikram	} else {
345568e756vikram		/*
346568e756vikram		 * The dev dir is at /dev i.e. we are not doing a -r /altroot
347568e756vikram		 */
348568e756vikram		isroot = 1;
3497c478bdstevel@tonic-gate	}
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	if (strcmp(dev_dir, "/") == 0) {
352facf4a8llai		dev_dir[0] = 0;
353facf4a8llai		db_dir[0] = 0;
3547c478bdstevel@tonic-gate	} else {
355facf4a8llai		(void) strlcpy(db_dir, dev_dir, sizeof (db_dir));
3567c478bdstevel@tonic-gate	}
3577c478bdstevel@tonic-gate
358facf4a8llai	(void) strlcat(dev_dir, DEV, sizeof (dev_dir));
359facf4a8llai	(void) strlcat(db_dir, ETCDEV, sizeof (db_dir));
360facf4a8llai
361568e756vikram	/*
362568e756vikram	 * The following code is for install. Readers and writers need
363568e756vikram	 * to be redirected to /tmp/etc/dev for the database file.
364568e756vikram	 * Note that we test for readonly /etc by actually creating a
365568e756vikram	 * file since statvfs is not a reliable method for determining
366568e756vikram	 * readonly filesystems.
367568e756vikram	 */
368568e756vikram	install = 0;
369568e756vikram	(void) snprintf(can_path, sizeof (can_path), "%s/%s", ETCDEV, DB_FILE);
370568e756vikram	if (flags == OPEN_RDWR && isroot) {
371568e756vikram		char di_test_db[PATH_MAX];
372568e756vikram		int fd;
373568e756vikram		(void) mutex_lock(&temp_file_mutex);
374568e756vikram		(void) snprintf(di_test_db, sizeof (di_test_db), "%s.%d",
375568e756vikram		    DI_TEST_DB, getpid());
376568e756vikram		fd = open(di_test_db, O_CREAT|O_RDWR|O_EXCL, 0644);
377568e756vikram		if (fd == -1 && errno == EROFS && stat(can_path, &sb) == -1)
378568e756vikram			install = 1;
379568e756vikram		if (fd != -1) {
380568e756vikram			(void) close(fd);
381568e756vikram			(void) unlink(di_test_db);
382568e756vikram		}
383568e756vikram		(void) mutex_unlock(&temp_file_mutex);
384568e756vikram	} else if (isroot) {
385568e756vikram		/*
386568e756vikram		 * Readers can be non-privileged so we cannot test by creating
387568e756vikram		 * a file in /etc/dev. Instead we check if the database
388568e756vikram		 * file is missing in /etc/dev and is present in /tmp/etc/dev
389568e756vikram		 * and is owned by root.
390568e756vikram		 */
391568e756vikram		char install_path[PATH_MAX];
392568e756vikram
393568e756vikram		(void) snprintf(install_path, sizeof (install_path),
394568e756vikram		    "/tmp%s/%s", ETCDEV, DB_FILE);
395568e756vikram		if (stat(can_path, &sb) == -1 && stat(install_path, &sb)
396568e756vikram		    != -1 && sb.st_uid == 0) {
397568e756vikram			install = 1;
398568e756vikram		}
399568e756vikram	}
400568e756vikram
401568e756vikram	/*
402568e756vikram	 * Check if we are in install. If we are, the database will be in
403568e756vikram	 * /tmp/etc/dev
404568e756vikram	 */
405568e756vikram	if (install)
406568e756vikram		(void) snprintf(db_dir, sizeof (db_dir), "/tmp%s", ETCDEV);
407568e756vikram
4087c478bdstevel@tonic-gate	proto.dev_dir = dev_dir;
409facf4a8llai	proto.db_dir = db_dir;
4107c478bdstevel@tonic-gate	proto.flags = flags;
4117c478bdstevel@tonic-gate	proto.lock_fd = -1;
4127c478bdstevel@tonic-gate
4137c478bdstevel@tonic-gate	/*
4147c478bdstevel@tonic-gate	 * Lock database if a read-write handle is being allocated.
4157c478bdstevel@tonic-gate	 * Locks are needed to protect against multiple writers.
416f7b6b8ccth	 * Readers don't need locks.
4177c478bdstevel@tonic-gate	 */
4187c478bdstevel@tonic-gate	if (HDL_RDWR(&proto)) {
419ff2aee4cth		if (enter_db_lock(&proto, root_dir) != 1) {
4207c478bdstevel@tonic-gate			return (NULL);
4217c478bdstevel@tonic-gate		}
4227c478bdstevel@tonic-gate	}
4237c478bdstevel@tonic-gate
4247c478bdstevel@tonic-gate	DB(&proto)->db_fd = -1;
4257c478bdstevel@tonic-gate
4267c478bdstevel@tonic-gate	hdp = calloc(1, sizeof (struct di_devlink_handle));
4277c478bdstevel@tonic-gate	if (hdp == NULL) {
4287c478bdstevel@tonic-gate		goto error;
4297c478bdstevel@tonic-gate	}
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	*hdp = proto;
4327c478bdstevel@tonic-gate
4337c478bdstevel@tonic-gate	/*
4347c478bdstevel@tonic-gate	 * The handle hdp now contains a pointer to local storage
4357c478bdstevel@tonic-gate	 * in the dev_dir field (obtained from the proto handle).
4367c478bdstevel@tonic-gate	 * In the following line, a dynamically allocated version
4377c478bdstevel@tonic-gate	 * is substituted.
4387c478bdstevel@tonic-gate	 */
4397c478bdstevel@tonic-gate
4407c478bdstevel@tonic-gate	if ((hdp->dev_dir = strdup(proto.dev_dir)) == NULL) {
4417c478bdstevel@tonic-gate		free(hdp);
4427c478bdstevel@tonic-gate		goto error;
4437c478bdstevel@tonic-gate	}
4447c478bdstevel@tonic-gate
445facf4a8llai	if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) {
446facf4a8llai		free(hdp->dev_dir);
447facf4a8llai		free(hdp);
448facf4a8llai		goto error;
449facf4a8llai	}
4507c478bdstevel@tonic-gate
4517c478bdstevel@tonic-gate	return (hdp);
4527c478bdstevel@tonic-gate
4537c478bdstevel@tonic-gateerror:
4547c478bdstevel@tonic-gate	if (HDL_RDWR(&proto)) {
4557c478bdstevel@tonic-gate		/* Unlink DB file on error */
4567c478bdstevel@tonic-gate		get_db_path(&proto, DB_FILE, path, sizeof (path));
4577c478bdstevel@tonic-gate		(void) unlink(path);
458ff2aee4cth		exit_db_lock(&proto);
4597c478bdstevel@tonic-gate	}
4607c478bdstevel@tonic-gate	return (NULL);
4617c478bdstevel@tonic-gate}
4627c478bdstevel@tonic-gate
4637c478bdstevel@tonic-gate
4647c478bdstevel@tonic-gatestatic int
4657c478bdstevel@tonic-gatecache_alloc(struct di_devlink_handle *hdp)
4667c478bdstevel@tonic-gate{
4677c478bdstevel@tonic-gate	size_t hash_sz = 0;
4687c478bdstevel@tonic-gate
4697c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
4707c478bdstevel@tonic-gate
4717c478bdstevel@tonic-gate	if (DB_OPEN(hdp)) {
4727c478bdstevel@tonic-gate		hash_sz = DB_NUM(hdp, DB_LINK) / AVG_CHAIN_SIZE;
4737c478bdstevel@tonic-gate	}
4747c478bdstevel@tonic-gate	hash_sz = (hash_sz >= MIN_HASH_SIZE) ? hash_sz : MIN_HASH_SIZE;
4757c478bdstevel@tonic-gate
4767c478bdstevel@tonic-gate	CACHE(hdp)->hash = calloc(hash_sz, sizeof (cache_link_t *));
4777c478bdstevel@tonic-gate	if (CACHE(hdp)->hash == NULL) {
4787c478bdstevel@tonic-gate		return (-1);
4797c478bdstevel@tonic-gate	}
4807c478bdstevel@tonic-gate	CACHE(hdp)->hash_sz = hash_sz;
4817c478bdstevel@tonic-gate
4827c478bdstevel@tonic-gate	return (0);
4837c478bdstevel@tonic-gate}
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate
4867c478bdstevel@tonic-gatestatic int
4877c478bdstevel@tonic-gateinvalid_db(struct di_devlink_handle *hdp, size_t fsize, long page_sz)
4887c478bdstevel@tonic-gate{
4897c478bdstevel@tonic-gate	int i;
4907c478bdstevel@tonic-gate	char *cp;
4917c478bdstevel@tonic-gate	size_t sz;
4927c478bdstevel@tonic-gate
4937c478bdstevel@tonic-gate	if (DB_HDR(hdp)->magic != DB_MAGIC || DB_HDR(hdp)->vers != DB_VERSION) {
4947c478bdstevel@tonic-gate		return (1);
4957c478bdstevel@tonic-gate	}
4967c478bdstevel@tonic-gate
4977c478bdstevel@tonic-gate	if (DB_HDR(hdp)->page_sz == 0 || DB_HDR(hdp)->page_sz != page_sz) {
4987c478bdstevel@tonic-gate		return (1);
4997c478bdstevel@tonic-gate	}
5007c478bdstevel@tonic-gate
5017c478bdstevel@tonic-gate	sz = seg_size(hdp, DB_HEADER);
5027c478bdstevel@tonic-gate	for (i = 0; i < DB_TYPES; i++) {
5037c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "N[%u] = %u\n", i, DB_NUM(hdp, i));
5047c478bdstevel@tonic-gate		/* There must be at least 1 element of each type */
5057c478bdstevel@tonic-gate		if (DB_NUM(hdp, i) < 1) {
5067c478bdstevel@tonic-gate			return (1);
5077c478bdstevel@tonic-gate		}
5087c478bdstevel@tonic-gate		sz += seg_size(hdp, i);
5097c478bdstevel@tonic-gate		assert(sz % page_sz == 0);
5107c478bdstevel@tonic-gate	}
5117c478bdstevel@tonic-gate
5127c478bdstevel@tonic-gate	if (sz != fsize) {
5137c478bdstevel@tonic-gate		return (1);
5147c478bdstevel@tonic-gate	}
5157c478bdstevel@tonic-gate
5167c478bdstevel@tonic-gate	if (!VALID_INDEX(hdp, DB_NODE, DB_HDR(hdp)->root_idx)) {
5177c478bdstevel@tonic-gate		return (1);
5187c478bdstevel@tonic-gate	}
5197c478bdstevel@tonic-gate
5207c478bdstevel@tonic-gate	if (!VALID_INDEX(hdp, DB_LINK, DB_HDR(hdp)->dngl_idx)) {
5217c478bdstevel@tonic-gate		return (1);
5227c478bdstevel@tonic-gate	}
5237c478bdstevel@tonic-gate
5247c478bdstevel@tonic-gate	if (DB_EMPTY(hdp)) {
5257c478bdstevel@tonic-gate		return (1);
5267c478bdstevel@tonic-gate	}
5277c478bdstevel@tonic-gate
5287c478bdstevel@tonic-gate	/*
5297c478bdstevel@tonic-gate	 * The last character in the string segment must be a NUL char.
5307c478bdstevel@tonic-gate	 */
5317c478bdstevel@tonic-gate	cp = get_string(hdp, DB_NUM(hdp, DB_STR) - 1);
5327c478bdstevel@tonic-gate	if (cp == NULL || *cp != '\0') {
5337c478bdstevel@tonic-gate		return (1);
5347c478bdstevel@tonic-gate	}
5357c478bdstevel@tonic-gate
5367c478bdstevel@tonic-gate	return (0);
5377c478bdstevel@tonic-gate}
5387c478bdstevel@tonic-gate
5397c478bdstevel@tonic-gatestatic int
5407c478bdstevel@tonic-gateread_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
5417c478bdstevel@tonic-gate{
5427c478bdstevel@tonic-gate	char *path;
5437c478bdstevel@tonic-gate	cache_node_t *cnp;
5447c478bdstevel@tonic-gate	struct db_node *dnp;
5457c478bdstevel@tonic-gate	const char *fcn = "read_nodes";
5467c478bdstevel@tonic-gate
5477c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
5487c478bdstevel@tonic-gate
5497c478bdstevel@tonic-gate	/*
5507c478bdstevel@tonic-gate	 * parent node should be NULL only for the root node
5517c478bdstevel@tonic-gate	 */
5527c478bdstevel@tonic-gate	if ((pcnp == NULL) ^ (nidx == DB_HDR(hdp)->root_idx)) {
5537c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: invalid parent or index(%u)\n",
5547c478bdstevel@tonic-gate		    fcn, nidx);
5557c478bdstevel@tonic-gate		SET_DB_ERR(hdp);
5567c478bdstevel@tonic-gate		return (-1);
5577c478bdstevel@tonic-gate	}
5587c478bdstevel@tonic-gate
5597c478bdstevel@tonic-gate	for (; dnp = get_node(hdp, nidx); nidx = dnp->sib) {
5607c478bdstevel@tonic-gate
5617c478bdstevel@tonic-gate		path = get_string(hdp, dnp->path);
5627c478bdstevel@tonic-gate
5637c478bdstevel@tonic-gate		/*
5647c478bdstevel@tonic-gate		 * Insert at head of list to recreate original order
5657c478bdstevel@tonic-gate		 */
5667c478bdstevel@tonic-gate		cnp = node_insert(hdp, pcnp, path, INSERT_HEAD);
5677c478bdstevel@tonic-gate		if (cnp == NULL) {
5687c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
5697c478bdstevel@tonic-gate			break;
5707c478bdstevel@tonic-gate		}
5717c478bdstevel@tonic-gate
5727c478bdstevel@tonic-gate		assert(strcmp(path, "/") ^ (nidx == DB_HDR(hdp)->root_idx));
5737c478bdstevel@tonic-gate		assert(strcmp(path, "/") != 0 || dnp->sib == DB_NIL);
5747c478bdstevel@tonic-gate
5757c478bdstevel@tonic-gate		if (read_minors(hdp, cnp, dnp->minor) != 0 ||
5767c478bdstevel@tonic-gate		    read_nodes(hdp, cnp, dnp->child) != 0) {
5777c478bdstevel@tonic-gate			break;
5787c478bdstevel@tonic-gate		}
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, nidx,
5817c478bdstevel@tonic-gate		    cnp->path);
5827c478bdstevel@tonic-gate	}
5837c478bdstevel@tonic-gate
5847c478bdstevel@tonic-gate	return (dnp ? -1 : 0);
5857c478bdstevel@tonic-gate}
5867c478bdstevel@tonic-gate
5877c478bdstevel@tonic-gatestatic int
5887c478bdstevel@tonic-gateread_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
5897c478bdstevel@tonic-gate{
5907c478bdstevel@tonic-gate	cache_minor_t *cmnp;
5917c478bdstevel@tonic-gate	struct db_minor *dmp;
5927c478bdstevel@tonic-gate	char *name, *nodetype;
5937c478bdstevel@tonic-gate	const char *fcn = "read_minors";
5947c478bdstevel@tonic-gate
5957c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
5967c478bdstevel@tonic-gate
5977c478bdstevel@tonic-gate	if (pcnp == NULL) {
5987c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: minor[%u]: orphan minor\n", fcn,
5997c478bdstevel@tonic-gate		    nidx);
6007c478bdstevel@tonic-gate		SET_DB_ERR(hdp);
6017c478bdstevel@tonic-gate		return (-1);
6027c478bdstevel@tonic-gate	}
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate	for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
6057c478bdstevel@tonic-gate
6067c478bdstevel@tonic-gate		name = get_string(hdp, dmp->name);
6077c478bdstevel@tonic-gate		nodetype = get_string(hdp, dmp->nodetype);
6087c478bdstevel@tonic-gate
6097c478bdstevel@tonic-gate		cmnp = minor_insert(hdp, pcnp, name, nodetype, NULL);
6107c478bdstevel@tonic-gate		if (cmnp == NULL) {
6117c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
6127c478bdstevel@tonic-gate			break;
6137c478bdstevel@tonic-gate		}
6147c478bdstevel@tonic-gate
6157c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, nidx,
6167c478bdstevel@tonic-gate		    cmnp->name);
6177c478bdstevel@tonic-gate
6187c478bdstevel@tonic-gate		if (read_links(hdp, cmnp, dmp->link) != 0) {
6197c478bdstevel@tonic-gate			break;
6207c478bdstevel@tonic-gate		}
6217c478bdstevel@tonic-gate	}
6227c478bdstevel@tonic-gate
6237c478bdstevel@tonic-gate	return (dmp ? -1 : 0);
6247c478bdstevel@tonic-gate}
6257c478bdstevel@tonic-gate
6267c478bdstevel@tonic-gate/*
6277c478bdstevel@tonic-gate * If the link is dangling the corresponding minor will be absent.
6287c478bdstevel@tonic-gate */
6297c478bdstevel@tonic-gatestatic int
6307c478bdstevel@tonic-gateread_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp, uint32_t nidx)
6317c478bdstevel@tonic-gate{
6327c478bdstevel@tonic-gate	cache_link_t *clp;
6337c478bdstevel@tonic-gate	struct db_link *dlp;
6347c478bdstevel@tonic-gate	char *path, *content;
6357c478bdstevel@tonic-gate
6367c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate	if (nidx != DB_NIL &&
6397c478bdstevel@tonic-gate	    ((pcmp == NULL) ^ (nidx == DB_HDR(hdp)->dngl_idx))) {
6407c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "read_links: invalid minor or"
6417c478bdstevel@tonic-gate		    " index(%u)\n", nidx);
6427c478bdstevel@tonic-gate		SET_DB_ERR(hdp);
6437c478bdstevel@tonic-gate		return (-1);
6447c478bdstevel@tonic-gate	}
6457c478bdstevel@tonic-gate
6467c478bdstevel@tonic-gate	for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate		path = get_string(hdp, dlp->path);
6497c478bdstevel@tonic-gate		content = get_string(hdp, dlp->content);
6507c478bdstevel@tonic-gate
6517c478bdstevel@tonic-gate		clp = link_insert(hdp, pcmp, path, content, dlp->attr);
6527c478bdstevel@tonic-gate		if (clp == NULL) {
6537c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
6547c478bdstevel@tonic-gate			break;
6557c478bdstevel@tonic-gate		}
6567c478bdstevel@tonic-gate
6577c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "read_links: link[%u]: %s%s\n",
6587c478bdstevel@tonic-gate		    nidx, clp->path, pcmp == NULL ? "(DANGLING)" : "");
6597c478bdstevel@tonic-gate	}
6607c478bdstevel@tonic-gate
6617c478bdstevel@tonic-gate	return (dlp ? -1 : 0);
6627c478bdstevel@tonic-gate}
6637c478bdstevel@tonic-gate
6647c478bdstevel@tonic-gateint
6657c478bdstevel@tonic-gatedi_devlink_close(di_devlink_handle_t *pp, int flag)
6667c478bdstevel@tonic-gate{
6677c478bdstevel@tonic-gate	int i, rv;
6687c478bdstevel@tonic-gate	char tmp[PATH_MAX];
6697c478bdstevel@tonic-gate	char file[PATH_MAX];
6707c478bdstevel@tonic-gate	uint32_t next[DB_TYPES] = {0};
6717c478bdstevel@tonic-gate	struct di_devlink_handle *hdp;
6727c478bdstevel@tonic-gate
6737c478bdstevel@tonic-gate	if (pp == NULL || *pp == NULL || !HDL_RDWR(*pp)) {
6747c478bdstevel@tonic-gate		errno = EINVAL;
6757c478bdstevel@tonic-gate		return (-1);
6767c478bdstevel@tonic-gate	}
6777c478bdstevel@tonic-gate
6787c478bdstevel@tonic-gate	hdp = *pp;
6797c478bdstevel@tonic-gate	*pp = NULL;
6807c478bdstevel@tonic-gate
6817c478bdstevel@tonic-gate	/*
6827c478bdstevel@tonic-gate	 * The caller encountered some error in their processing.
6837c478bdstevel@tonic-gate	 * so handle isn't valid. Discard it and return success.
6847c478bdstevel@tonic-gate	 */
6857c478bdstevel@tonic-gate	if (flag == DI_LINK_ERROR) {
6867c478bdstevel@tonic-gate		handle_free(&hdp);
6877c478bdstevel@tonic-gate		return (0);
6887c478bdstevel@tonic-gate	}
6897c478bdstevel@tonic-gate
6907c478bdstevel@tonic-gate	if (DB_ERR(hdp)) {
6917c478bdstevel@tonic-gate		handle_free(&hdp);
6927c478bdstevel@tonic-gate		errno = EINVAL;
6937c478bdstevel@tonic-gate		return (-1);
6947c478bdstevel@tonic-gate	}
6957c478bdstevel@tonic-gate
6967c478bdstevel@tonic-gate	/*
6977c478bdstevel@tonic-gate	 * Extract the DB path before the handle is freed.
6987c478bdstevel@tonic-gate	 */
6997c478bdstevel@tonic-gate	get_db_path(hdp, DB_FILE, file, sizeof (file));
7007c478bdstevel@tonic-gate	get_db_path(hdp, DB_TMP, tmp, sizeof (tmp));
7017c478bdstevel@tonic-gate
7027c478bdstevel@tonic-gate	/*
7037c478bdstevel@tonic-gate	 * update database with actual contents of /dev
7047c478bdstevel@tonic-gate	 */
7057c478bdstevel@tonic-gate	(void) dprintf(DBG_INFO, "di_devlink_close: update_count = %u\n",
7067c478bdstevel@tonic-gate	    CACHE(hdp)->update_count);
7077c478bdstevel@tonic-gate
7087c478bdstevel@tonic-gate	/*
7097c478bdstevel@tonic-gate	 * For performance reasons, synchronization of the database
7107c478bdstevel@tonic-gate	 * with /dev is turned off by default. However, applications
7117c478bdstevel@tonic-gate	 * with appropriate permissions can request a "sync" by
7127c478bdstevel@tonic-gate	 * calling di_devlink_update().
7137c478bdstevel@tonic-gate	 */
7147c478bdstevel@tonic-gate	if (CACHE(hdp)->update_count == 0) {
7157c478bdstevel@tonic-gate		CACHE(hdp)->update_count = 1;
7167c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO,
7177c478bdstevel@tonic-gate		    "di_devlink_close: synchronizing DB\n");
7187c478bdstevel@tonic-gate		(void) synchronize_db(hdp);
7197c478bdstevel@tonic-gate	}
7207c478bdstevel@tonic-gate
7217c478bdstevel@tonic-gate	/*
7227c478bdstevel@tonic-gate	 * Resolve dangling links AFTER synchronizing DB with /dev as the
7237c478bdstevel@tonic-gate	 * synchronization process may create dangling links.
7247c478bdstevel@tonic-gate	 */
7257c478bdstevel@tonic-gate	resolve_dangling_links(hdp);
7267c478bdstevel@tonic-gate
7277c478bdstevel@tonic-gate	/*
7287c478bdstevel@tonic-gate	 * All changes to the cache are complete. Write out the cache
7297c478bdstevel@tonic-gate	 * to the database only if it is not empty.
7307c478bdstevel@tonic-gate	 */
7317c478bdstevel@tonic-gate	if (CACHE_EMPTY(hdp)) {
7327c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "di_devlink_close: skipping write\n");
7337c478bdstevel@tonic-gate		(void) unlink(file);
7347c478bdstevel@tonic-gate		handle_free(&hdp);
7357c478bdstevel@tonic-gate		return (0);
7367c478bdstevel@tonic-gate	}
7377c478bdstevel@tonic-gate
7387c478bdstevel@tonic-gate	if (open_db(hdp, OPEN_RDWR) != 0) {
7397c478bdstevel@tonic-gate		handle_free(&hdp);
7407c478bdstevel@tonic-gate		return (-1);
7417c478bdstevel@tonic-gate	}
7427c478bdstevel@tonic-gate
7437c478bdstevel@tonic-gate	/*
744ff2aee4cth	 * Keep track of array assignments. There is at least
7457c478bdstevel@tonic-gate	 * 1 element (the "NIL" element) per type.
7467c478bdstevel@tonic-gate	 */
7477c478bdstevel@tonic-gate	for (i = 0; i < DB_TYPES; i++) {
7487c478bdstevel@tonic-gate		next[i] = 1;
7497c478bdstevel@tonic-gate	}
7507c478bdstevel@tonic-gate
7517c478bdstevel@tonic-gate	(void) write_nodes(hdp, NULL, CACHE_ROOT(hdp), next);
7527c478bdstevel@tonic-gate	(void) write_links(hdp, NULL, CACHE(hdp)->dngl, next);
7537c478bdstevel@tonic-gate	DB_HDR(hdp)->update_count = CACHE(hdp)->update_count;
7547c478bdstevel@tonic-gate
7557c478bdstevel@tonic-gate	rv = close_db(hdp);
7567c478bdstevel@tonic-gate
7577c478bdstevel@tonic-gate	if (rv != 0 || DB_ERR(hdp) || rename(tmp, file) != 0) {
7587c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "di_devlink_close: %s error: %s\n",
7597c478bdstevel@tonic-gate		    rv ? "close_db" : "DB or rename", strerror(errno));
7607c478bdstevel@tonic-gate		(void) unlink(tmp);
7617c478bdstevel@tonic-gate		(void) unlink(file);
7627c478bdstevel@tonic-gate		handle_free(&hdp);
7637c478bdstevel@tonic-gate		return (-1);
7647c478bdstevel@tonic-gate	}
7657c478bdstevel@tonic-gate
7667c478bdstevel@tonic-gate	handle_free(&hdp);
7677c478bdstevel@tonic-gate
7687c478bdstevel@tonic-gate	(void) dprintf(DBG_INFO, "di_devlink_close: wrote DB(%s)\n", file);
7697c478bdstevel@tonic-gate
7707c478bdstevel@tonic-gate	return (0);
7717c478bdstevel@tonic-gate}
7727c478bdstevel@tonic-gate
7737c478bdstevel@tonic-gate/*
7747c478bdstevel@tonic-gate * Inits the database header.
7757c478bdstevel@tonic-gate */
7767c478bdstevel@tonic-gatestatic int
7777c478bdstevel@tonic-gateinit_hdr(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
7787c478bdstevel@tonic-gate{
7797c478bdstevel@tonic-gate	int i;
7807c478bdstevel@tonic-gate
7817c478bdstevel@tonic-gate	DB_HDR(hdp)->magic = DB_MAGIC;
7827c478bdstevel@tonic-gate	DB_HDR(hdp)->vers = DB_VERSION;
7837c478bdstevel@tonic-gate	DB_HDR(hdp)->root_idx = DB_NIL;
7847c478bdstevel@tonic-gate	DB_HDR(hdp)->dngl_idx = DB_NIL;
7857c478bdstevel@tonic-gate	DB_HDR(hdp)->page_sz = (uint32_t)page_sz;
7867c478bdstevel@tonic-gate
7877c478bdstevel@tonic-gate	for (i = 0; i < DB_TYPES; i++) {
7887c478bdstevel@tonic-gate		assert(count[i] >= 1);
7897c478bdstevel@tonic-gate		DB_NUM(hdp, i) = count[i];
7907c478bdstevel@tonic-gate	}
7917c478bdstevel@tonic-gate
7927c478bdstevel@tonic-gate	return (0);
7937c478bdstevel@tonic-gate}
7947c478bdstevel@tonic-gate
7957c478bdstevel@tonic-gatestatic int
7967c478bdstevel@tonic-gatewrite_nodes(
7977c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
7987c478bdstevel@tonic-gate	struct db_node *pdnp,
7997c478bdstevel@tonic-gate	cache_node_t *cnp,
8007c478bdstevel@tonic-gate	uint32_t *next)
8017c478bdstevel@tonic-gate{
8027c478bdstevel@tonic-gate	uint32_t idx;
8037c478bdstevel@tonic-gate	struct db_node *dnp;
8047c478bdstevel@tonic-gate	const char *fcn = "write_nodes";
8057c478bdstevel@tonic-gate
8067c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
8077c478bdstevel@tonic-gate
8087c478bdstevel@tonic-gate	for (; cnp != NULL; cnp = cnp->sib) {
8097c478bdstevel@tonic-gate
8107c478bdstevel@tonic-gate		assert(cnp->path != NULL);
8117c478bdstevel@tonic-gate
8127c478bdstevel@tonic-gate		/* parent node should only be NULL for root node */
8137c478bdstevel@tonic-gate		if ((pdnp == NULL) ^ (cnp == CACHE_ROOT(hdp))) {
8147c478bdstevel@tonic-gate			(void) dprintf(DBG_ERR, "%s: invalid parent for: %s\n",
8157c478bdstevel@tonic-gate			    fcn, cnp->path);
8167c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
8177c478bdstevel@tonic-gate			break;
8187c478bdstevel@tonic-gate		}
8197c478bdstevel@tonic-gate
8207c478bdstevel@tonic-gate		assert((strcmp(cnp->path, "/") != 0) ^
8217c478bdstevel@tonic-gate		    (cnp == CACHE_ROOT(hdp)));
8227c478bdstevel@tonic-gate
8237c478bdstevel@tonic-gate		idx = next[DB_NODE];
8247c478bdstevel@tonic-gate		if ((dnp = set_node(hdp, idx)) == NULL) {
8257c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
8267c478bdstevel@tonic-gate			break;
8277c478bdstevel@tonic-gate		}
8287c478bdstevel@tonic-gate
8297c478bdstevel@tonic-gate		dnp->path = write_string(hdp, cnp->path, next);
8307c478bdstevel@tonic-gate		if (dnp->path == DB_NIL) {
8317c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
8327c478bdstevel@tonic-gate			break;
8337c478bdstevel@tonic-gate		}
8347c478bdstevel@tonic-gate		/* commit write for this node */
8357c478bdstevel@tonic-gate		next[DB_NODE]++;
8367c478bdstevel@tonic-gate
8377c478bdstevel@tonic-gate		if (pdnp == NULL) {
8387c478bdstevel@tonic-gate			assert(DB_HDR(hdp)->root_idx == DB_NIL);
8397c478bdstevel@tonic-gate			DB_HDR(hdp)->root_idx = idx;
8407c478bdstevel@tonic-gate		} else {
8417c478bdstevel@tonic-gate			dnp->sib = pdnp->child;
8427c478bdstevel@tonic-gate			pdnp->child = idx;
8437c478bdstevel@tonic-gate		}
8447c478bdstevel@tonic-gate
8457c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "%s: node[%u]: %s\n", fcn, idx,
8467c478bdstevel@tonic-gate		    cnp->path);
8477c478bdstevel@tonic-gate
8487c478bdstevel@tonic-gate		if (write_minors(hdp, dnp, cnp->minor, next) != 0 ||
8497c478bdstevel@tonic-gate		    write_nodes(hdp, dnp, cnp->child, next) != 0) {
8507c478bdstevel@tonic-gate			break;
8517c478bdstevel@tonic-gate		}
8527c478bdstevel@tonic-gate	}
8537c478bdstevel@tonic-gate
8547c478bdstevel@tonic-gate	return (cnp ? -1 : 0);
8557c478bdstevel@tonic-gate}
8567c478bdstevel@tonic-gate
8577c478bdstevel@tonic-gatestatic int
8587c478bdstevel@tonic-gatewrite_minors(
8597c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
8607c478bdstevel@tonic-gate	struct db_node *pdnp,
8617c478bdstevel@tonic-gate	cache_minor_t *cmnp,
8627c478bdstevel@tonic-gate	uint32_t *next)
8637c478bdstevel@tonic-gate{
8647c478bdstevel@tonic-gate	uint32_t idx;
8657c478bdstevel@tonic-gate	struct db_minor *dmp;
8667c478bdstevel@tonic-gate	const char *fcn = "write_minors";
8677c478bdstevel@tonic-gate
8687c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
8697c478bdstevel@tonic-gate
8707c478bdstevel@tonic-gate	if (pdnp == NULL) {
8717c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: no node for minor: %s\n", fcn,
8727c478bdstevel@tonic-gate		    cmnp ? cmnp->name : "<NULL>");
8737c478bdstevel@tonic-gate		SET_DB_ERR(hdp);
8747c478bdstevel@tonic-gate		return (-1);
8757c478bdstevel@tonic-gate	}
8767c478bdstevel@tonic-gate
8777c478bdstevel@tonic-gate	for (; cmnp != NULL; cmnp = cmnp->sib) {
8787c478bdstevel@tonic-gate
8797c478bdstevel@tonic-gate		assert(cmnp->name != NULL);
8807c478bdstevel@tonic-gate
8817c478bdstevel@tonic-gate		idx = next[DB_MINOR];
8827c478bdstevel@tonic-gate		if ((dmp = set_minor(hdp, idx)) == NULL) {
8837c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
8847c478bdstevel@tonic-gate			break;
8857c478bdstevel@tonic-gate		}
8867c478bdstevel@tonic-gate
8877c478bdstevel@tonic-gate		dmp->name = write_string(hdp, cmnp->name, next);
8887c478bdstevel@tonic-gate		dmp->nodetype = write_string(hdp, cmnp->nodetype, next);
8897c478bdstevel@tonic-gate		if (dmp->name == DB_NIL || dmp->nodetype == DB_NIL) {
8907c478bdstevel@tonic-gate			dmp->name = dmp->nodetype = DB_NIL;
8917c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
8927c478bdstevel@tonic-gate			break;
8937c478bdstevel@tonic-gate		}
8947c478bdstevel@tonic-gate
8957c478bdstevel@tonic-gate		/* Commit writes to this minor */
8967c478bdstevel@tonic-gate		next[DB_MINOR]++;
8977c478bdstevel@tonic-gate
8987c478bdstevel@tonic-gate		dmp->sib = pdnp->minor;
8997c478bdstevel@tonic-gate		pdnp->minor = idx;
9007c478bdstevel@tonic-gate
9017c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "%s: minor[%u]: %s\n", fcn, idx,
9027c478bdstevel@tonic-gate		    cmnp->name);
9037c478bdstevel@tonic-gate
9047c478bdstevel@tonic-gate		if (write_links(hdp, dmp, cmnp->link, next) != 0) {
9057c478bdstevel@tonic-gate			break;
9067c478bdstevel@tonic-gate		}
9077c478bdstevel@tonic-gate	}
9087c478bdstevel@tonic-gate
9097c478bdstevel@tonic-gate	return (cmnp ? -1 : 0);
9107c478bdstevel@tonic-gate}
9117c478bdstevel@tonic-gate
9127c478bdstevel@tonic-gatestatic int
9137c478bdstevel@tonic-gatewrite_links(
9147c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
9157c478bdstevel@tonic-gate	struct db_minor *pdmp,
9167c478bdstevel@tonic-gate	cache_link_t *clp,
9177c478bdstevel@tonic-gate	uint32_t *next)
9187c478bdstevel@tonic-gate{
9197c478bdstevel@tonic-gate	uint32_t idx;
9207c478bdstevel@tonic-gate	struct db_link *dlp;
9217c478bdstevel@tonic-gate	const char *fcn = "write_links";
9227c478bdstevel@tonic-gate
9237c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
9247c478bdstevel@tonic-gate
9257c478bdstevel@tonic-gate	/* A NULL minor if and only if the links are dangling */
9267c478bdstevel@tonic-gate	if (clp != NULL && ((pdmp == NULL) ^ (clp == CACHE(hdp)->dngl))) {
9277c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: invalid minor for link\n", fcn);
9287c478bdstevel@tonic-gate		SET_DB_ERR(hdp);
9297c478bdstevel@tonic-gate		return (-1);
9307c478bdstevel@tonic-gate	}
9317c478bdstevel@tonic-gate
9327c478bdstevel@tonic-gate	for (; clp != NULL; clp = clp->sib) {
9337c478bdstevel@tonic-gate
9347c478bdstevel@tonic-gate		assert(clp->path != NULL);
9357c478bdstevel@tonic-gate
9367c478bdstevel@tonic-gate		if ((pdmp == NULL) ^ (clp->minor == NULL)) {
9377c478bdstevel@tonic-gate			(void) dprintf(DBG_ERR, "%s: invalid minor for link"
9387c478bdstevel@tonic-gate			    "(%s)\n", fcn, clp->path);
9397c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
9407c478bdstevel@tonic-gate			break;
9417c478bdstevel@tonic-gate		}
9427c478bdstevel@tonic-gate
9437c478bdstevel@tonic-gate		idx = next[DB_LINK];
9447c478bdstevel@tonic-gate		if ((dlp = set_link(hdp, idx)) == NULL) {
9457c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
9467c478bdstevel@tonic-gate			break;
9477c478bdstevel@tonic-gate		}
9487c478bdstevel@tonic-gate
9497c478bdstevel@tonic-gate		dlp->path = write_string(hdp, clp->path, next);
9507c478bdstevel@tonic-gate		dlp->content = write_string(hdp, clp->content, next);
9517c478bdstevel@tonic-gate		if (dlp->path == DB_NIL || dlp->content == DB_NIL) {
9527c478bdstevel@tonic-gate			dlp->path = dlp->content = DB_NIL;
9537c478bdstevel@tonic-gate			SET_DB_ERR(hdp);
9547c478bdstevel@tonic-gate			break;
9557c478bdstevel@tonic-gate		}
9567c478bdstevel@tonic-gate
9577c478bdstevel@tonic-gate		dlp->attr = clp->attr;
9587c478bdstevel@tonic-gate
9597c478bdstevel@tonic-gate		/* Commit writes to this link */
9607c478bdstevel@tonic-gate		next[DB_LINK]++;
9617c478bdstevel@tonic-gate
9627c478bdstevel@tonic-gate		if (pdmp != NULL) {
9637c478bdstevel@tonic-gate			dlp->sib = pdmp->link;
9647c478bdstevel@tonic-gate			pdmp->link = idx;
9657c478bdstevel@tonic-gate		} else {
9667c478bdstevel@tonic-gate			dlp->sib = DB_HDR(hdp)->dngl_idx;
9677c478bdstevel@tonic-gate			DB_HDR(hdp)->dngl_idx = idx;
9687c478bdstevel@tonic-gate		}
9697c478bdstevel@tonic-gate
9707c478bdstevel@tonic-gate		(void) dprintf(DBG_STEP, "%s: link[%u]: %s%s\n", fcn, idx,
9717c478bdstevel@tonic-gate		    clp->path, pdmp == NULL ? "(DANGLING)" : "");
9727c478bdstevel@tonic-gate	}
9737c478bdstevel@tonic-gate
9747c478bdstevel@tonic-gate	return (clp ? -1 : 0);
9757c478bdstevel@tonic-gate}
9767c478bdstevel@tonic-gate
9777c478bdstevel@tonic-gate
9787c478bdstevel@tonic-gatestatic uint32_t
9797c478bdstevel@tonic-gatewrite_string(struct di_devlink_handle *hdp, const char *str, uint32_t *next)
9807c478bdstevel@tonic-gate{
9817c478bdstevel@tonic-gate	char *dstr;
9827c478bdstevel@tonic-gate	uint32_t idx;
9837c478bdstevel@tonic-gate
9847c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp));
9857c478bdstevel@tonic-gate
9867c478bdstevel@tonic-gate	if (str == NULL) {
9877c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "write_string: NULL argument\n");
9887c478bdstevel@tonic-gate		return (DB_NIL);
9897c478bdstevel@tonic-gate	}
9907c478bdstevel@tonic-gate
9917c478bdstevel@tonic-gate	idx = next[DB_STR];
9927c478bdstevel@tonic-gate	if (!VALID_STR(hdp, idx, str)) {
9937c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "write_string: invalid index[%u],"
9947c478bdstevel@tonic-gate		    " string(%s)\n", idx, str);
9957c478bdstevel@tonic-gate		return (DB_NIL);
9967c478bdstevel@tonic-gate	}
9977c478bdstevel@tonic-gate
9987c478bdstevel@tonic-gate	if ((dstr = set_string(hdp, idx)) == NULL) {
9997c478bdstevel@tonic-gate		return (DB_NIL);
10007c478bdstevel@tonic-gate	}
10017c478bdstevel@tonic-gate
10027c478bdstevel@tonic-gate	(void) strcpy(dstr, str);
10037c478bdstevel@tonic-gate
10047c478bdstevel@tonic-gate	next[DB_STR] += strlen(dstr) + 1;
10057c478bdstevel@tonic-gate
10067c478bdstevel@tonic-gate	return (idx);
10077c478bdstevel@tonic-gate}
10087c478bdstevel@tonic-gate
10097c478bdstevel@tonic-gatestatic int
10107c478bdstevel@tonic-gateclose_db(struct di_devlink_handle *hdp)
10117c478bdstevel@tonic-gate{
10127c478bdstevel@tonic-gate	int i, rv = 0;
10137c478bdstevel@tonic-gate	size_t sz;
10147c478bdstevel@tonic-gate
10157c478bdstevel@tonic-gate	if (!DB_OPEN(hdp)) {
10167c478bdstevel@tonic-gate#ifdef	DEBUG
10177c478bdstevel@tonic-gate		assert(DB(hdp)->db_fd == -1);
10187c478bdstevel@tonic-gate		assert(DB(hdp)->flags == 0);
10197c478bdstevel@tonic-gate		for (i = 0; i < DB_TYPES; i++) {
10207c478bdstevel@tonic-gate			assert(DB_SEG(hdp, i) == NULL);
10217c478bdstevel@tonic-gate			assert(DB_SEG_PROT(hdp, i) == 0);
10227c478bdstevel@tonic-gate		}
10237c478bdstevel@tonic-gate#endif
10247c478bdstevel@tonic-gate		return (0);
10257c478bdstevel@tonic-gate	}
10267c478bdstevel@tonic-gate
10277c478bdstevel@tonic-gate	/* Unmap header after unmapping all other mapped segments */
10287c478bdstevel@tonic-gate	for (i = 0; i < DB_TYPES; i++) {
10297c478bdstevel@tonic-gate		if (DB_SEG(hdp, i)) {
10307c478bdstevel@tonic-gate			sz = seg_size(hdp, i);
10317c478bdstevel@tonic-gate			if (DB_RDWR(hdp))
10327c478bdstevel@tonic-gate				rv += msync(DB_SEG(hdp, i), sz, MS_SYNC);
10337c478bdstevel@tonic-gate			(void) munmap(DB_SEG(hdp, i), sz);
10347c478bdstevel@tonic-gate			DB_SEG(hdp, i) = NULL;
10357c478bdstevel@tonic-gate			DB_SEG_PROT(hdp, i) = 0;
10367c478bdstevel@tonic-gate		}
10377c478bdstevel@tonic-gate	}
10387c478bdstevel@tonic-gate
10397c478bdstevel@tonic-gate	if (DB_RDWR(hdp))
10407c478bdstevel@tonic-gate		rv += msync((caddr_t)DB_HDR(hdp), HDR_LEN, MS_SYNC);
10417c478bdstevel@tonic-gate	(void) munmap((caddr_t)DB_HDR(hdp), HDR_LEN);
10427c478bdstevel@tonic-gate	DB(hdp)->hdr = NULL;
10437c478bdstevel@tonic-gate
10447c478bdstevel@tonic-gate	(void) close(DB(hdp)->db_fd);
10457c478bdstevel@tonic-gate	DB(hdp)->db_fd = -1;
10467c478bdstevel@tonic-gate	DB(hdp)->flags = 0;
10477c478bdstevel@tonic-gate
10487c478bdstevel@tonic-gate	return (rv ? -1 : 0);
10497c478bdstevel@tonic-gate}
10507c478bdstevel@tonic-gate
10517c478bdstevel@tonic-gate
10527c478bdstevel@tonic-gatestatic void
10537c478bdstevel@tonic-gatecache_free(struct di_devlink_handle *hdp)
10547c478bdstevel@tonic-gate{
10557c478bdstevel@tonic-gate	cache_link_t *clp;
10567c478bdstevel@tonic-gate
10577c478bdstevel@tonic-gate	subtree_free(hdp, &(CACHE_ROOT(hdp)));
10587c478bdstevel@tonic-gate	assert(CACHE_LAST(hdp) == NULL);
10597c478bdstevel@tonic-gate
10607c478bdstevel@tonic-gate	/*
10617c478bdstevel@tonic-gate	 * Don't bother removing links from hash table chains,
10627c478bdstevel@tonic-gate	 * as we are freeing the hash table itself.
10637c478bdstevel@tonic-gate	 */
10647c478bdstevel@tonic-gate	while (CACHE(hdp)->dngl != NULL) {
10657c478bdstevel@tonic-gate		clp = CACHE(hdp)->dngl;
10667c478bdstevel@tonic-gate		CACHE(hdp)->dngl = clp->sib;
10677c478bdstevel@tonic-gate		assert(clp->minor == NULL);
10687c478bdstevel@tonic-gate		link_free(&clp);
10697c478bdstevel@tonic-gate	}
10707c478bdstevel@tonic-gate
10717c478bdstevel@tonic-gate	assert((CACHE(hdp)->hash == NULL) ^ (CACHE(hdp)->hash_sz != 0));
10727c478bdstevel@tonic-gate
10737c478bdstevel@tonic-gate	free(CACHE(hdp)->hash);
10747c478bdstevel@tonic-gate	CACHE(hdp)->hash = NULL;
10757c478bdstevel@tonic-gate	CACHE(hdp)->hash_sz = 0;
10767c478bdstevel@tonic-gate}
10777c478bdstevel@tonic-gate
10787c478bdstevel@tonic-gatestatic void
10797c478bdstevel@tonic-gatehandle_free(struct di_devlink_handle **pp)
10807c478bdstevel@tonic-gate{
10817c478bdstevel@tonic-gate	struct di_devlink_handle *hdp = *pp;
10827c478bdstevel@tonic-gate
10837c478bdstevel@tonic-gate	*pp = NULL;
10847c478bdstevel@tonic-gate
10857c478bdstevel@tonic-gate	if (hdp == NULL)
10867c478bdstevel@tonic-gate		return;
10877c478bdstevel@tonic-gate
10887c478bdstevel@tonic-gate	(void) close_db(hdp);
10897c478bdstevel@tonic-gate	cache_free(hdp);
10907c478bdstevel@tonic-gate
10917c478bdstevel@tonic-gate	if (HDL_RDWR(hdp))
1092ff2aee4cth		exit_db_lock(hdp);
10937c478bdstevel@tonic-gate	assert(hdp->lock_fd == -1);
10947c478bdstevel@tonic-gate
10957c478bdstevel@tonic-gate	free(hdp->dev_dir);
1096a08731ecth	free(hdp->db_dir);
10977c478bdstevel@tonic-gate	free(hdp);
10987c478bdstevel@tonic-gate}
10997c478bdstevel@tonic-gate
11007c478bdstevel@tonic-gate/*
11017c478bdstevel@tonic-gate * Frees the tree rooted at a node. Siblings of the subtree root
11027c478bdstevel@tonic-gate * have to be handled by the caller.
11037c478bdstevel@tonic-gate */
11047c478bdstevel@tonic-gatestatic void
11057c478bdstevel@tonic-gatesubtree_free(struct di_devlink_handle *hdp, cache_node_t **pp)
11067c478bdstevel@tonic-gate{
11077c478bdstevel@tonic-gate	cache_node_t *np;
11087c478bdstevel@tonic-gate	cache_link_t *clp;
11097c478bdstevel@tonic-gate	cache_minor_t *cmnp;
11107c478bdstevel@tonic-gate
11117c478bdstevel@tonic-gate	if (pp == NULL || *pp == NULL)
11127c478bdstevel@tonic-gate		return;
11137c478bdstevel@tonic-gate
11147c478bdstevel@tonic-gate	while ((*pp)->child != NULL) {
11157c478bdstevel@tonic-gate		np = (*pp)->child;
11167c478bdstevel@tonic-gate		(*pp)->child = np->sib;
11177c478bdstevel@tonic-gate		subtree_free(hdp, &np);
11187c478bdstevel@tonic-gate	}
11197c478bdstevel@tonic-gate
11207c478bdstevel@tonic-gate	while ((*pp)->minor != NULL) {
11217c478bdstevel@tonic-gate		cmnp = (*pp)->minor;
11227c478bdstevel@tonic-gate		(*pp)->minor = cmnp->sib;
11237c478bdstevel@tonic-gate
11247c478bdstevel@tonic-gate		while (cmnp->link != NULL) {
11257c478bdstevel@tonic-gate			clp = cmnp->link;
11267c478bdstevel@tonic-gate			cmnp->link = clp->sib;
11277c478bdstevel@tonic-gate			rm_link_from_hash(hdp, clp);
11287c478bdstevel@tonic-gate			link_free(&clp);
11297c478bdstevel@tonic-gate		}
11307c478bdstevel@tonic-gate		minor_free(hdp, &cmnp);
11317c478bdstevel@tonic-gate	}
11327c478bdstevel@tonic-gate
11337c478bdstevel@tonic-gate	node_free(pp);
11347c478bdstevel@tonic-gate}
11357c478bdstevel@tonic-gate
11367c478bdstevel@tonic-gatestatic void
11377c478bdstevel@tonic-gaterm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp)
11387c478bdstevel@tonic-gate{
11397c478bdstevel@tonic-gate	int hval;
11407c478bdstevel@tonic-gate	cache_link_t **pp;
11417c478bdstevel@tonic-gate
11427c478bdstevel@tonic-gate	if (clp == NULL)
11437c478bdstevel@tonic-gate		return;
11447c478bdstevel@tonic-gate
11457c478bdstevel@tonic-gate	if (clp->path == NULL)
11467c478bdstevel@tonic-gate		return;
11477c478bdstevel@tonic-gate
11487c478bdstevel@tonic-gate	hval = hashfn(hdp, clp->path);
11497c478bdstevel@tonic-gate	pp = &(CACHE_HASH(hdp, hval));
11507c478bdstevel@tonic-gate	for (; *pp != NULL; pp = &(*pp)->hash) {
11517c478bdstevel@tonic-gate		if (*pp == clp) {
11527c478bdstevel@tonic-gate			*pp = clp->hash;
11537c478bdstevel@tonic-gate			clp->hash = NULL;
11547c478bdstevel@tonic-gate			return;
11557c478bdstevel@tonic-gate		}
11567c478bdstevel@tonic-gate	}
11577c478bdstevel@tonic-gate
11587c478bdstevel@tonic-gate	dprintf(DBG_ERR, "rm_link_from_hash: link(%s) not found\n", clp->path);
11597c478bdstevel@tonic-gate}
11607c478bdstevel@tonic-gate
11617c478bdstevel@tonic-gatestatic cache_link_t *
11627c478bdstevel@tonic-gatelink_hash(di_devlink_handle_t hdp, const char *link, uint_t flags)
11637c478bdstevel@tonic-gate{
11647c478bdstevel@tonic-gate	int hval;
11657c478bdstevel@tonic-gate	cache_link_t **pp, *clp;
11667c478bdstevel@tonic-gate
11677c478bdstevel@tonic-gate	if (link == NULL)
11687c478bdstevel@tonic-gate		return (NULL);
11697c478bdstevel@tonic-gate
11707c478bdstevel@tonic-gate	hval = hashfn(hdp, link);
11717c478bdstevel@tonic-gate	pp = &(CACHE_HASH(hdp, hval));
11727c478bdstevel@tonic-gate	for (; (clp = *pp) != NULL; pp = &clp->hash) {
11737c478bdstevel@tonic-gate		if (strcmp(clp->path, link) == 0) {
11747c478bdstevel@tonic-gate			break;
11757c478bdstevel@tonic-gate		}
11767c478bdstevel@tonic-gate	}
11777c478bdstevel@tonic-gate
11787c478bdstevel@tonic-gate	if (clp == NULL)
11797c478bdstevel@tonic-gate		return (NULL);
11807c478bdstevel@tonic-gate
11817c478bdstevel@tonic-gate	if ((flags & UNLINK_FROM_HASH) == UNLINK_FROM_HASH) {
11827c478bdstevel@tonic-gate		*pp = clp->hash;
11837c478bdstevel@tonic-gate		clp->hash = NULL;
11847c478bdstevel@tonic-gate	}
11857c478bdstevel@tonic-gate
11867c478bdstevel@tonic-gate	return (clp);
11877c478bdstevel@tonic-gate}
11887c478bdstevel@tonic-gate
11897c478bdstevel@tonic-gatestatic cache_minor_t *
11907c478bdstevel@tonic-gatelink2minor(struct di_devlink_handle *hdp, cache_link_t *clp)
11917c478bdstevel@tonic-gate{
11927c478bdstevel@tonic-gate	cache_link_t *plp;
11937c478bdstevel@tonic-gate	const char *minor_path;
11947c478bdstevel@tonic-gate	char *cp, buf[PATH_MAX], link[PATH_MAX];
1195facf4a8llai	char abspath[PATH_MAX];
1196facf4a8llai	struct stat st;
11977c478bdstevel@tonic-gate
11987c478bdstevel@tonic-gate	if (TYPE_PRI(attr2type(clp->attr))) {
11997c478bdstevel@tonic-gate		/*
12007c478bdstevel@tonic-gate		 * For primary link, content should point to a /devices node.
12017c478bdstevel@tonic-gate		 */
12027c478bdstevel@tonic-gate		if (!is_minor_node(clp->content, &minor_path)) {
12037c478bdstevel@tonic-gate			return (NULL);
12047c478bdstevel@tonic-gate		}
12057c478bdstevel@tonic-gate
12067c478bdstevel@tonic-gate		return (lookup_minor(hdp, minor_path, NULL,
12077c478bdstevel@tonic-gate		    TYPE_CACHE|CREATE_FLAG));
12087c478bdstevel@tonic-gate
12097c478bdstevel@tonic-gate	}
12107c478bdstevel@tonic-gate
12117c478bdstevel@tonic-gate	/*
12127c478bdstevel@tonic-gate	 * If secondary, the primary link is derived from the secondary
12137c478bdstevel@tonic-gate	 * link contents. Secondary link contents can have two formats:
1214facf4a8llai	 *	audio -> /dev/sound/0
12157c478bdstevel@tonic-gate	 *	fb0 -> fbs/afb0
12167c478bdstevel@tonic-gate	 */
12177c478bdstevel@tonic-gate
12187c478bdstevel@tonic-gate	buf[0] = '\0';
12197c478bdstevel@tonic-gate	if (strncmp(clp->content, DEV"/", strlen(DEV"/")) == 0) {
12207c478bdstevel@tonic-gate		cp = &clp->content[strlen(DEV"/")];
12217c478bdstevel@tonic-gate	} else if (clp->content[0] != '/') {
12227c478bdstevel@tonic-gate		if ((cp = strrchr(clp->path, '/')) != NULL) {
12237c478bdstevel@tonic-gate			char savechar = *(cp + 1);
12247c478bdstevel@tonic-gate			*(cp + 1) = '\0';
12257c478bdstevel@tonic-gate			(void) snprintf(buf, sizeof (buf), "%s", clp->path);
12267c478bdstevel@tonic-gate			*(cp + 1) = savechar;
12277c478bdstevel@tonic-gate		}
12287c478bdstevel@tonic-gate		(void) strlcat(buf, clp->content, sizeof (buf));
12297c478bdstevel@tonic-gate		cp = buf;
12307c478bdstevel@tonic-gate	} else {
12317c478bdstevel@tonic-gate		goto follow_link;
12327c478bdstevel@tonic-gate	}
12337c478bdstevel@tonic-gate
12347c478bdstevel@tonic-gate	/*
12357c478bdstevel@tonic-gate	 * Lookup the primary link if possible and find its minor.
12367c478bdstevel@tonic-gate	 */
12377c478bdstevel@tonic-gate	if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) {
12387c478bdstevel@tonic-gate		return (plp->minor);
12397c478bdstevel@tonic-gate	}
12407c478bdstevel@tonic-gate
12417c478bdstevel@tonic-gate	/* realpath() used only as a last resort because it is expensive */
12427c478bdstevel@tonic-gatefollow_link:
12437c478bdstevel@tonic-gate	(void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path);
12447c478bdstevel@tonic-gate
12457c478bdstevel@tonic-gate#ifdef	DEBUG
12467c478bdstevel@tonic-gate	/*LINTED*/
12477c478bdstevel@tonic-gate	assert(sizeof (buf) >= PATH_MAX);
12487c478bdstevel@tonic-gate#endif
1249facf4a8llai
1250facf4a8llai	/*
1251facf4a8llai	 * A realpath attempt to lookup a dangling link can invoke implicit
1252facf4a8llai	 * reconfig so verify there's an actual device behind the link first.
1253facf4a8llai	 */
1254facf4a8llai	if (lstat(link, &st) == -1)
1255facf4a8llai		return (NULL);
1256facf4a8llai	if (S_ISLNK(st.st_mode)) {
1257facf4a8llai		if (s_readlink(link, buf, sizeof (buf)) < 0)
1258facf4a8llai			return (NULL);
1259facf4a8llai		if (buf[0] != '/') {
1260facf4a8llai			char *p;
1261facf4a8llai			size_t n = sizeof (abspath);
1262facf4a8llai			if (strlcpy(abspath, link, n) >= n)
1263facf4a8llai				return (NULL);
1264facf4a8llai			p = strrchr(abspath, '/') + 1;
1265facf4a8llai			*p = 0;
1266facf4a8llai			n = sizeof (abspath) - strlen(p);
1267facf4a8llai			if (strlcpy(p, buf, n) >= n)
1268facf4a8llai				return (NULL);
1269facf4a8llai		} else {
1270facf4a8llai			if (strlcpy(abspath, buf, sizeof (abspath)) >=
1271facf4a8llai			    sizeof (abspath))
1272facf4a8llai				return (NULL);
1273facf4a8llai		}
1274facf4a8llai		if (!device_exists(abspath))
1275facf4a8llai			return (NULL);
1276facf4a8llai	}
1277facf4a8llai
1278ff2aee4cth	if (s_realpath(link, buf) == NULL || !is_minor_node(buf, &minor_path)) {
12797c478bdstevel@tonic-gate		return (NULL);
12807c478bdstevel@tonic-gate	}
12817c478bdstevel@tonic-gate	return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG));
12827c478bdstevel@tonic-gate}
12837c478bdstevel@tonic-gate
12847c478bdstevel@tonic-gate
12857c478bdstevel@tonic-gatestatic void
12867c478bdstevel@tonic-gateresolve_dangling_links(struct di_devlink_handle *hdp)
12877c478bdstevel@tonic-gate{
12887c478bdstevel@tonic-gate	cache_minor_t *cmnp;
12897c478bdstevel@tonic-gate	cache_link_t *clp, **pp;
12907c478bdstevel@tonic-gate
12917c478bdstevel@tonic-gate	for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) {
12927c478bdstevel@tonic-gate		clp = *pp;
12937c478bdstevel@tonic-gate		if ((cmnp = link2minor(hdp, clp)) != NULL) {
12947c478bdstevel@tonic-gate			*pp = clp->sib;
12957c478bdstevel@tonic-gate			clp->sib = cmnp->link;
12967c478bdstevel@tonic-gate			cmnp->link = clp;
12977c478bdstevel@tonic-gate			assert(clp->minor == NULL);
12987c478bdstevel@tonic-gate			clp->minor = cmnp;
12997c478bdstevel@tonic-gate		} else {
13007c478bdstevel@tonic-gate			dprintf(DBG_INFO, "resolve_dangling_links: link(%s):"
13017c478bdstevel@tonic-gate			    " unresolved\n", clp->path);
13027c478bdstevel@tonic-gate			pp = &clp->sib;
13037c478bdstevel@tonic-gate		}
13047c478bdstevel@tonic-gate	}
13057c478bdstevel@tonic-gate}
13067c478bdstevel@tonic-gate
13077c478bdstevel@tonic-gate
13087c478bdstevel@tonic-gate/*
13097c478bdstevel@tonic-gate * The elements are assumed to be detached from the cache tree.
13107c478bdstevel@tonic-gate */
13117c478bdstevel@tonic-gatestatic void
13127c478bdstevel@tonic-gatenode_free(cache_node_t **pp)
13137c478bdstevel@tonic-gate{
13147c478bdstevel@tonic-gate	cache_node_t *cnp = *pp;
13157c478bdstevel@tonic-gate
13167c478bdstevel@tonic-gate	*pp = NULL;
13177c478bdstevel@tonic-gate
13187c478bdstevel@tonic-gate	if (cnp == NULL)
13197c478bdstevel@tonic-gate		return;
13207c478bdstevel@tonic-gate
13217c478bdstevel@tonic-gate	free(cnp->path);
13227c478bdstevel@tonic-gate	free(cnp);
13237c478bdstevel@tonic-gate}
13247c478bdstevel@tonic-gate
13257c478bdstevel@tonic-gatestatic void
13267c478bdstevel@tonic-gateminor_free(struct di_devlink_handle *hdp, cache_minor_t **pp)
13277c478bdstevel@tonic-gate{
13287c478bdstevel@tonic-gate	cache_minor_t *cmnp = *pp;
13297c478bdstevel@tonic-gate
13307c478bdstevel@tonic-gate	*pp = NULL;
13317c478bdstevel@tonic-gate
13327c478bdstevel@tonic-gate	if (cmnp == NULL)
13337c478bdstevel@tonic-gate		return;
13347c478bdstevel@tonic-gate
13357c478bdstevel@tonic-gate	if (CACHE_LAST(hdp) == cmnp) {
13367c478bdstevel@tonic-gate		dprintf(DBG_STEP, "minor_free: last_minor(%s)\n", cmnp->name);
13377c478bdstevel@tonic-gate		CACHE_LAST(hdp) = NULL;
13387c478bdstevel@tonic-gate	}
13397c478bdstevel@tonic-gate
13407c478bdstevel@tonic-gate	free(cmnp->name);
13417c478bdstevel@tonic-gate	free(cmnp->nodetype);
13427c478bdstevel@tonic-gate	free(cmnp);
13437c478bdstevel@tonic-gate}
13447c478bdstevel@tonic-gate
13457c478bdstevel@tonic-gatestatic void
13467c478bdstevel@tonic-gatelink_free(cache_link_t **pp)
13477c478bdstevel@tonic-gate{
13487c478bdstevel@tonic-gate	cache_link_t *clp = *pp;
13497c478bdstevel@tonic-gate
13507c478bdstevel@tonic-gate	*pp = NULL;
13517c478bdstevel@tonic-gate
13527c478bdstevel@tonic-gate	if (clp == NULL)
13537c478bdstevel@tonic-gate		return;
13547c478bdstevel@tonic-gate
13557c478bdstevel@tonic-gate	free(clp->path);
13567c478bdstevel@tonic-gate	free(clp->content);
13577c478bdstevel@tonic-gate	free(clp);
13587c478bdstevel@tonic-gate}
13597c478bdstevel@tonic-gate
13607c478bdstevel@tonic-gate/*
13617c478bdstevel@tonic-gate * Returns the ':' preceding the minor name
13627c478bdstevel@tonic-gate */
13637c478bdstevel@tonic-gatestatic char *
13647c478bdstevel@tonic-gateminor_colon(const char *path)
13657c478bdstevel@tonic-gate{
13667c478bdstevel@tonic-gate	char *cp;
13677c478bdstevel@tonic-gate
13687c478bdstevel@tonic-gate	if ((cp = strrchr(path, '/')) == NULL) {
13697c478bdstevel@tonic-gate		return (NULL);
13707c478bdstevel@tonic-gate	}
13717c478bdstevel@tonic-gate
13727c478bdstevel@tonic-gate	return (strchr(cp, ':'));
13737c478bdstevel@tonic-gate}
13747c478bdstevel@tonic-gate
13757c478bdstevel@tonic-gatestatic void *
13767c478bdstevel@tonic-gatelookup_minor(
13777c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
13787c478bdstevel@tonic-gate	const char *minor_path,
13797c478bdstevel@tonic-gate	const char *nodetype,
13807c478bdstevel@tonic-gate	const int flags)
13817c478bdstevel@tonic-gate{
13827c478bdstevel@tonic-gate	void *vp;
13837c478bdstevel@tonic-gate	char *colon;
13847c478bdstevel@tonic-gate	char pdup[PATH_MAX];
13857c478bdstevel@tonic-gate	const char *fcn = "lookup_minor";
13867c478bdstevel@tonic-gate
13877c478bdstevel@tonic-gate	if (minor_path == NULL) {
13887c478bdstevel@tonic-gate		errno = EINVAL;
13897c478bdstevel@tonic-gate		return (NULL);
13907c478bdstevel@tonic-gate	}
13917c478bdstevel@tonic-gate
13927c478bdstevel@tonic-gate	(void) snprintf(pdup, sizeof (pdup), "%s", minor_path);
13937c478bdstevel@tonic-gate
13947c478bdstevel@tonic-gate	if ((colon = minor_colon(pdup)) == NULL) {
13957c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: invalid minor path(%s)\n", fcn,
13967c478bdstevel@tonic-gate		    minor_path);
13977c478bdstevel@tonic-gate		errno = EINVAL;
13987c478bdstevel@tonic-gate		return (NULL);
13997c478bdstevel@tonic-gate	}
14007c478bdstevel@tonic-gate	*colon = '\0';
14017c478bdstevel@tonic-gate
14027c478bdstevel@tonic-gate	if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) {
14037c478bdstevel@tonic-gate		return (vp);
14047c478bdstevel@tonic-gate	}
14057c478bdstevel@tonic-gate
14067c478bdstevel@tonic-gate	if ((vp = lookup_node(hdp, pdup, flags)) == NULL) {
14077c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: node(%s) not found\n", fcn, pdup);
14087c478bdstevel@tonic-gate		return (NULL);
14097c478bdstevel@tonic-gate	}
14107c478bdstevel@tonic-gate	*colon = ':';
14117c478bdstevel@tonic-gate
14127c478bdstevel@tonic-gate	if (LOOKUP_CACHE(flags)) {
14137c478bdstevel@tonic-gate		cache_minor_t **pp;
14147c478bdstevel@tonic-gate
14157c478bdstevel@tonic-gate		pp = &((cache_node_t *)vp)->minor;
14167c478bdstevel@tonic-gate		for (; *pp != NULL; pp = &(*pp)->sib) {
14177c478bdstevel@tonic-gate			if (strcmp((*pp)->name, colon + 1) == 0)
14187c478bdstevel@tonic-gate				break;
14197c478bdstevel@tonic-gate		}
14207c478bdstevel@tonic-gate
14217c478bdstevel@tonic-gate		if (*pp == NULL && CREATE_ELEM(flags)) {
14227c478bdstevel@tonic-gate			*pp = minor_insert(hdp, vp, colon + 1, nodetype, pp);
14237c478bdstevel@tonic-gate		}
14247c478bdstevel@tonic-gate		set_last_minor(hdp, *pp, flags);
14257c478bdstevel@tonic-gate
14267c478bdstevel@tonic-gate		return (*pp);
14277c478bdstevel@tonic-gate	} else {
14287c478bdstevel@tonic-gate		char *cp;
14297c478bdstevel@tonic-gate		uint32_t nidx;
14307c478bdstevel@tonic-gate		struct db_minor *dmp;
14317c478bdstevel@tonic-gate
14327c478bdstevel@tonic-gate		nidx = (((struct db_node *)vp)->minor);
14337c478bdstevel@tonic-gate		for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
14347c478bdstevel@tonic-gate			cp = get_string(hdp, dmp->name);
14357c478bdstevel@tonic-gate			if (cp && strcmp(cp, colon + 1) == 0)
14367c478bdstevel@tonic-gate				break;
14377c478bdstevel@tonic-gate		}
14387c478bdstevel@tonic-gate		return (dmp);
14397c478bdstevel@tonic-gate	}
14407c478bdstevel@tonic-gate}
14417c478bdstevel@tonic-gate
14427c478bdstevel@tonic-gatestatic void *
14437c478bdstevel@tonic-gatelookup_node(struct di_devlink_handle *hdp, char *path, const int flags)
14447c478bdstevel@tonic-gate{
14457c478bdstevel@tonic-gate	struct tnode tnd = {NULL};
14467c478bdstevel@tonic-gate
14477c478bdstevel@tonic-gate	if (tnd.node = get_last_node(hdp, path, flags))
14487c478bdstevel@tonic-gate		return (tnd.node);
14497c478bdstevel@tonic-gate
14507c478bdstevel@tonic-gate	tnd.handle = hdp;
14517c478bdstevel@tonic-gate	tnd.flags = flags;
14527c478bdstevel@tonic-gate
14537c478bdstevel@tonic-gate	if (walk_tree(path, &tnd, visit_node) != 0)
14547c478bdstevel@tonic-gate		return (NULL);
14557c478bdstevel@tonic-gate
14567c478bdstevel@tonic-gate	return (tnd.node);
14577c478bdstevel@tonic-gate}
14587c478bdstevel@tonic-gate
14597c478bdstevel@tonic-gate/*
14607c478bdstevel@tonic-gate * last_minor is used for nodes of TYPE_CACHE only.
14617c478bdstevel@tonic-gate */
14627c478bdstevel@tonic-gatestatic void *
14637c478bdstevel@tonic-gateget_last_node(struct di_devlink_handle *hdp, const char *path, int flags)
14647c478bdstevel@tonic-gate{
14657c478bdstevel@tonic-gate	cache_node_t *cnp;
14667c478bdstevel@tonic-gate
14677c478bdstevel@tonic-gate#ifdef	DEBUG
14687c478bdstevel@tonic-gate	if (getenv(SKIP_LAST_CACHE)) {
14697c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "get_last_node: SKIPPING \"last\" "
14707c478bdstevel@tonic-gate		    "node cache\n");
14717c478bdstevel@tonic-gate		return (NULL);
14727c478bdstevel@tonic-gate	}
14737c478bdstevel@tonic-gate#endif
14747c478bdstevel@tonic-gate
14757c478bdstevel@tonic-gate	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL ||
14767c478bdstevel@tonic-gate	    CACHE_LAST(hdp)->node == NULL) {
14777c478bdstevel@tonic-gate		return (NULL);
14787c478bdstevel@tonic-gate	}
14797c478bdstevel@tonic-gate
14807c478bdstevel@tonic-gate	cnp = CACHE_LAST(hdp)->node;
14817c478bdstevel@tonic-gate	if (strcmp(cnp->path, path) == 0) {
14827c478bdstevel@tonic-gate		return (cnp);
14837c478bdstevel@tonic-gate	}
14847c478bdstevel@tonic-gate
14857c478bdstevel@tonic-gate	cnp = cnp->sib;
14867c478bdstevel@tonic-gate	if (cnp && strcmp(cnp->path, path) == 0) {
14877c478bdstevel@tonic-gate		return (cnp);
14887c478bdstevel@tonic-gate	}
14897c478bdstevel@tonic-gate
14907c478bdstevel@tonic-gate	return (NULL);
14917c478bdstevel@tonic-gate}
14927c478bdstevel@tonic-gate
14937c478bdstevel@tonic-gatestatic void *
14947c478bdstevel@tonic-gateget_last_minor(
14957c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
14967c478bdstevel@tonic-gate	const char *devfs_path,
14977c478bdstevel@tonic-gate	const char *minor_name,
14987c478bdstevel@tonic-gate	int flags)
14997c478bdstevel@tonic-gate{
15007c478bdstevel@tonic-gate	cache_minor_t *cmnp;
15017c478bdstevel@tonic-gate
15027c478bdstevel@tonic-gate#ifdef	DEBUG
15037c478bdstevel@tonic-gate	if (getenv(SKIP_LAST_CACHE)) {
15047c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "get_last_minor: SKIPPING \"last\" "
15057c478bdstevel@tonic-gate		    "minor cache\n");
15067c478bdstevel@tonic-gate		return (NULL);
15077c478bdstevel@tonic-gate	}
15087c478bdstevel@tonic-gate#endif
15097c478bdstevel@tonic-gate
15107c478bdstevel@tonic-gate	if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) {
15117c478bdstevel@tonic-gate		return (NULL);
15127c478bdstevel@tonic-gate	}
15137c478bdstevel@tonic-gate
15147c478bdstevel@tonic-gate	cmnp = CACHE_LAST(hdp);
15157c478bdstevel@tonic-gate	if (strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
15167c478bdstevel@tonic-gate	    strcmp(cmnp->node->path, devfs_path) == 0) {
15177c478bdstevel@tonic-gate		return (cmnp);
15187c478bdstevel@tonic-gate	}
15197c478bdstevel@tonic-gate
15207c478bdstevel@tonic-gate	cmnp = cmnp->sib;
15217c478bdstevel@tonic-gate	if (cmnp && strcmp(cmnp->name, minor_name) == 0 && cmnp->node &&
15227c478bdstevel@tonic-gate	    strcmp(cmnp->node->path, devfs_path) == 0) {
15237c478bdstevel@tonic-gate		set_last_minor(hdp, cmnp, TYPE_CACHE);
15247c478bdstevel@tonic-gate		return (cmnp);
15257c478bdstevel@tonic-gate	}
15267c478bdstevel@tonic-gate
15277c478bdstevel@tonic-gate	return (NULL);
15287c478bdstevel@tonic-gate}
15297c478bdstevel@tonic-gate
15307c478bdstevel@tonic-gatestatic void
15317c478bdstevel@tonic-gateset_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags)
15327c478bdstevel@tonic-gate{
15337c478bdstevel@tonic-gate#ifdef	DEBUG
15347c478bdstevel@tonic-gate	if (getenv(SKIP_LAST_CACHE)) {
15357c478bdstevel@tonic-gate		(void) dprintf(DBG_INFO, "set_last_minor: SKIPPING \"last\" "
15367c478bdstevel@tonic-gate		    "minor cache\n");
15377c478bdstevel@tonic-gate		return;
15387c478bdstevel@tonic-gate	}
15397c478bdstevel@tonic-gate#endif
15407c478bdstevel@tonic-gate
15417c478bdstevel@tonic-gate	if (LOOKUP_CACHE(flags) && cmnp) {
15427c478bdstevel@tonic-gate		CACHE_LAST(hdp) = cmnp;
15437c478bdstevel@tonic-gate	}
15447c478bdstevel@tonic-gate}
15457c478bdstevel@tonic-gate
15467c478bdstevel@tonic-gate
15477c478bdstevel@tonic-gate/*
15487c478bdstevel@tonic-gate * Returns 0 if normal return or -1 otherwise.
15497c478bdstevel@tonic-gate */
15507c478bdstevel@tonic-gatestatic int
15517c478bdstevel@tonic-gatewalk_tree(
15527c478bdstevel@tonic-gate	char *cur,
15537c478bdstevel@tonic-gate	void *arg,
15547c478bdstevel@tonic-gate	int (*node_callback)(const char *path, void *arg))
15557c478bdstevel@tonic-gate{
15567c478bdstevel@tonic-gate	char *slash, buf[PATH_MAX];
15577c478bdstevel@tonic-gate
15587c478bdstevel@tonic-gate	if (cur == NULL || cur[0] != '/' || strlen(cur) > sizeof (buf) - 1) {
15597c478bdstevel@tonic-gate		errno = EINVAL;
15607c478bdstevel@tonic-gate		return (-1);
15617c478bdstevel@tonic-gate	}
15627c478bdstevel@tonic-gate
15637c478bdstevel@tonic-gate	(void) strcpy(buf, "/");
15647c478bdstevel@tonic-gate
15657c478bdstevel@tonic-gate	for (;;) {
15667c478bdstevel@tonic-gate
15677c478bdstevel@tonic-gate		if (node_callback(buf, arg) != DI_WALK_CONTINUE)
15687c478bdstevel@tonic-gate			break;
15697c478bdstevel@tonic-gate
15707c478bdstevel@tonic-gate		while (*cur == '/')
15717c478bdstevel@tonic-gate			cur++;
15727c478bdstevel@tonic-gate
15737c478bdstevel@tonic-gate		if (*cur == '\0')
15747c478bdstevel@tonic-gate			break;
15757c478bdstevel@tonic-gate
15767c478bdstevel@tonic-gate		/*
15777c478bdstevel@tonic-gate		 * There is a next component(s). Append a "/" separator for all
15787c478bdstevel@tonic-gate		 * but the first (root) component.
15797c478bdstevel@tonic-gate		 */
15807c478bdstevel@tonic-gate		if (buf[1] != '\0') {
15817c478bdstevel@tonic-gate			(void) strlcat(buf, "/", sizeof (buf));
15827c478bdstevel@tonic-gate		}
15837c478bdstevel@tonic-gate
15847c478bdstevel@tonic-gate		if (slash = strchr(cur, '/')) {
15857c478bdstevel@tonic-gate			*slash = '\0';
15867c478bdstevel@tonic-gate			(void) strlcat(buf, cur, sizeof (buf));
15877c478bdstevel@tonic-gate			*slash = '/';
15887c478bdstevel@tonic-gate			cur = slash;
15897c478bdstevel@tonic-gate		} else {
15907c478bdstevel@tonic-gate			(void) strlcat(buf, cur, sizeof (buf));
15917c478bdstevel@tonic-gate			cur += strlen(cur);
15927c478bdstevel@tonic-gate		}
15937c478bdstevel@tonic-gate
15947c478bdstevel@tonic-gate	}
15957c478bdstevel@tonic-gate
15967c478bdstevel@tonic-gate	return (0);
15977c478bdstevel@tonic-gate}
15987c478bdstevel@tonic-gate
15997c478bdstevel@tonic-gate
16007c478bdstevel@tonic-gatestatic int
16017c478bdstevel@tonic-gatevisit_node(const char *path, void *arg)
16027c478bdstevel@tonic-gate{
16037c478bdstevel@tonic-gate	struct tnode *tnp = arg;
16047c478bdstevel@tonic-gate
16057c478bdstevel@tonic-gate	if (LOOKUP_CACHE(tnp->flags)) {
16067c478bdstevel@tonic-gate
16077c478bdstevel@tonic-gate		cache_node_t *cnp = tnp->node;
16087c478bdstevel@tonic-gate
16097c478bdstevel@tonic-gate		cnp = (cnp) ? cnp->child : CACHE_ROOT(tnp->handle);
16107c478bdstevel@tonic-gate
16117c478bdstevel@tonic-gate		for (; cnp != NULL; cnp = cnp->sib) {
16127c478bdstevel@tonic-gate			if (strcmp(cnp->path, path) == 0)
16137c478bdstevel@tonic-gate				break;
16147c478bdstevel@tonic-gate		}
16157c478bdstevel@tonic-gate		if (cnp == NULL && CREATE_ELEM(tnp->flags)) {
16167c478bdstevel@tonic-gate			cnp = node_insert(tnp->handle, tnp->node, path,
16177c478bdstevel@tonic-gate			    INSERT_TAIL);
16187c478bdstevel@tonic-gate		}
16197c478bdstevel@tonic-gate		tnp->node = cnp;
16207c478bdstevel@tonic-gate	} else {
16217c478bdstevel@tonic-gate		char *cp;
16227c478bdstevel@tonic-gate		struct db_node *dnp = tnp->node;
16237c478bdstevel@tonic-gate
16247c478bdstevel@tonic-gate		dnp = (dnp) ? get_node(tnp->handle, dnp->child)
16257c478bdstevel@tonic-gate		    : get_node(tnp->handle, DB_HDR(tnp->handle)->root_idx);
16267c478bdstevel@tonic-gate
16277c478bdstevel@tonic-gate		for (; dnp != NULL; dnp = get_node(tnp->handle, dnp->sib)) {
16287c478bdstevel@tonic-gate			cp = get_string(tnp->handle, dnp->path);
16297c478bdstevel@tonic-gate			if (cp && strcmp(cp, path) == 0) {
16307c478bdstevel@tonic-gate				break;
16317c478bdstevel@tonic-gate			}
16327c478bdstevel@tonic-gate		}
16337c478bdstevel@tonic-gate		tnp->node = dnp;
16347c478bdstevel@tonic-gate	}
16357c478bdstevel@tonic-gate
16367c478bdstevel@tonic-gate	/*
16377c478bdstevel@tonic-gate	 * Terminate walk if node is not found for a path component.
16387c478bdstevel@tonic-gate	 */
16397c478bdstevel@tonic-gate	return (tnp->node ? DI_WALK_CONTINUE : DI_WALK_TERMINATE);
16407c478bdstevel@tonic-gate}
16417c478bdstevel@tonic-gate
16427c478bdstevel@tonic-gatestatic void
16437c478bdstevel@tonic-gateminor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp)
16447c478bdstevel@tonic-gate{
16457c478bdstevel@tonic-gate	cache_link_t **lpp;
16467c478bdstevel@tonic-gate	cache_minor_t **mpp;
16477c478bdstevel@tonic-gate	const char *fcn = "minor_delete";
16487c478bdstevel@tonic-gate
16497c478bdstevel@tonic-gate	(void) dprintf(DBG_STEP, "%s: removing minor: %s\n", fcn, cmnp->name);
16507c478bdstevel@tonic-gate
16517c478bdstevel@tonic-gate	/* detach minor from node */
16527c478bdstevel@tonic-gate	if (cmnp->node != NULL) {
16537c478bdstevel@tonic-gate		mpp = &cmnp->node->minor;
16547c478bdstevel@tonic-gate		for (; *mpp != NULL; mpp = &(*mpp)->sib) {
16557c478bdstevel@tonic-gate			if (*mpp == cmnp)
16567c478bdstevel@tonic-gate				break;
16577c478bdstevel@tonic-gate		}
16587c478bdstevel@tonic-gate
16597c478bdstevel@tonic-gate		if (*mpp == NULL) {
16607c478bdstevel@tonic-gate			(void) dprintf(DBG_ERR, "%s: dangling minor: %s\n",
16617c478bdstevel@tonic-gate			    fcn, cmnp->name);
16627c478bdstevel@tonic-gate		} else {
16637c478bdstevel@tonic-gate			*mpp = cmnp->sib;
16647c478bdstevel@tonic-gate		}
16657c478bdstevel@tonic-gate	} else {
16667c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: orphan minor(%s)\n", fcn,
16677c478bdstevel@tonic-gate		    cmnp->name);
16687c478bdstevel@tonic-gate	}
16697c478bdstevel@tonic-gate
16707c478bdstevel@tonic-gate	delete_unused_nodes(hdp, cmnp->node);
16717c478bdstevel@tonic-gate
16727c478bdstevel@tonic-gate	cmnp->node = NULL;
16737c478bdstevel@tonic-gate	cmnp->sib = NULL;
16747c478bdstevel@tonic-gate
16757c478bdstevel@tonic-gate	/* Move all remaining links to dangling list */
16767c478bdstevel@tonic-gate	for (lpp = &cmnp->link; *lpp != NULL; lpp = &(*lpp)->sib) {
16777c478bdstevel@tonic-gate		(*lpp)->minor = NULL;
16787c478bdstevel@tonic-gate	}
16797c478bdstevel@tonic-gate	*lpp = CACHE(hdp)->dngl;
16807c478bdstevel@tonic-gate	CACHE(hdp)->dngl = cmnp->link;
16817c478bdstevel@tonic-gate	cmnp->link = NULL;
16827c478bdstevel@tonic-gate
16837c478bdstevel@tonic-gate	minor_free(hdp, &cmnp);
16847c478bdstevel@tonic-gate}
16857c478bdstevel@tonic-gate
16867c478bdstevel@tonic-gatestatic void
16877c478bdstevel@tonic-gatedelete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp)
16887c478bdstevel@tonic-gate{
16897c478bdstevel@tonic-gate	cache_node_t **npp;
16907c478bdstevel@tonic-gate	const char *fcn = "delete_unused_nodes";
16917c478bdstevel@tonic-gate
16927c478bdstevel@tonic-gate	if (cnp == NULL)
16937c478bdstevel@tonic-gate		return;
16947c478bdstevel@tonic-gate
16957c478bdstevel@tonic-gate	if (cnp->minor != NULL || cnp->child != NULL)
16967c478bdstevel@tonic-gate		return;
16977c478bdstevel@tonic-gate
16987c478bdstevel@tonic-gate	(void) dprintf(DBG_INFO, "%s: removing unused node: %s\n", fcn,
16997c478bdstevel@tonic-gate	    cnp->path);
17007c478bdstevel@tonic-gate
17017c478bdstevel@tonic-gate	/* Unlink node from tree */
17027c478bdstevel@tonic-gate	if (cnp->parent != NULL) {
17037c478bdstevel@tonic-gate		npp = &cnp->parent->child;
17047c478bdstevel@tonic-gate		for (; *npp != NULL; npp = &(*npp)->sib) {
17057c478bdstevel@tonic-gate			if (*npp == cnp)
17067c478bdstevel@tonic-gate				break;
17077c478bdstevel@tonic-gate		}
17087c478bdstevel@tonic-gate
17097c478bdstevel@tonic-gate		if (*npp == NULL) {
17107c478bdstevel@tonic-gate			(void) dprintf(DBG_ERR, "%s: dangling node: %s\n", fcn,
17117c478bdstevel@tonic-gate			    cnp->path);
17127c478bdstevel@tonic-gate		} else {
17137c478bdstevel@tonic-gate			*npp = cnp->sib;
17147c478bdstevel@tonic-gate		}
17157c478bdstevel@tonic-gate	} else if (cnp == CACHE_ROOT(hdp)) {
17167c478bdstevel@tonic-gate		CACHE_ROOT(hdp) = NULL;
17177c478bdstevel@tonic-gate	} else {
17187c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: orphan node (%s)\n", fcn,
17197c478bdstevel@tonic-gate		    cnp->path);
17207c478bdstevel@tonic-gate	}
17217c478bdstevel@tonic-gate
17227c478bdstevel@tonic-gate	delete_unused_nodes(hdp, cnp->parent);
17237c478bdstevel@tonic-gate
17247c478bdstevel@tonic-gate	cnp->parent = cnp->sib = NULL;
17257c478bdstevel@tonic-gate
17267c478bdstevel@tonic-gate	node_free(&cnp);
17277c478bdstevel@tonic-gate}
17287c478bdstevel@tonic-gate
17297c478bdstevel@tonic-gatestatic int
17307c478bdstevel@tonic-gaterm_link(di_devlink_handle_t hdp, const char *link)
17317c478bdstevel@tonic-gate{
17327c478bdstevel@tonic-gate	cache_link_t *clp;
17337c478bdstevel@tonic-gate	const char *fcn = "rm_link";
17347c478bdstevel@tonic-gate
17357c478bdstevel@tonic-gate	if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' ||
17367c478bdstevel@tonic-gate	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
17377c478bdstevel@tonic-gate		dprintf(DBG_ERR, "%s: %s: invalid args\n",
17387c478bdstevel@tonic-gate		    fcn, link ? link : "<NULL>");
17397c478bdstevel@tonic-gate		errno = EINVAL;
17407c478bdstevel@tonic-gate		return (-1);
17417c478bdstevel@tonic-gate	}
17427c478bdstevel@tonic-gate
17437c478bdstevel@tonic-gate	dprintf(DBG_STEP, "%s: link(%s)\n", fcn, link);
17447c478bdstevel@tonic-gate
17457c478bdstevel@tonic-gate	if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) {
17467c478bdstevel@tonic-gate		return (0);
17477c478bdstevel@tonic-gate	}
17487c478bdstevel@tonic-gate
17497c478bdstevel@tonic-gate	link_delete(hdp, clp);
17507c478bdstevel@tonic-gate
17517c478bdstevel@tonic-gate	return (0);
17527c478bdstevel@tonic-gate}
17537c478bdstevel@tonic-gate
17547c478bdstevel@tonic-gateint
17557c478bdstevel@tonic-gatedi_devlink_rm_link(di_devlink_handle_t hdp, const char *link)
17567c478bdstevel@tonic-gate{
17577c478bdstevel@tonic-gate	if (hdp == NULL || !HDL_RDWR(hdp)) {
17587c478bdstevel@tonic-gate		errno = EINVAL;
17597c478bdstevel@tonic-gate		return (-1);
17607c478bdstevel@tonic-gate	}
17617c478bdstevel@tonic-gate
17627c478bdstevel@tonic-gate	return (rm_link(hdp, link));
17637c478bdstevel@tonic-gate}
17647c478bdstevel@tonic-gate
17657c478bdstevel@tonic-gatestatic void
17667c478bdstevel@tonic-gatelink_delete(di_devlink_handle_t hdp, cache_link_t *clp)
17677c478bdstevel@tonic-gate{
17687c478bdstevel@tonic-gate	cache_link_t **pp;
17697c478bdstevel@tonic-gate	const char *fcn = "link_delete";
17707c478bdstevel@tonic-gate
17717c478bdstevel@tonic-gate	(void) dprintf(DBG_STEP, "%s: removing link: %s\n", fcn, clp->path);
17727c478bdstevel@tonic-gate
17737c478bdstevel@tonic-gate	if (clp->minor == NULL)
17747c478bdstevel@tonic-gate		pp = &(CACHE(hdp)->dngl);
17757c478bdstevel@tonic-gate	else
17767c478bdstevel@tonic-gate		pp = &clp->minor->link;
17777c478bdstevel@tonic-gate
17787c478bdstevel@tonic-gate	for (; *pp != NULL; pp = &(*pp)->sib) {
17797c478bdstevel@tonic-gate		if (*pp == clp)
17807c478bdstevel@tonic-gate			break;
17817c478bdstevel@tonic-gate	}
17827c478bdstevel@tonic-gate
17837c478bdstevel@tonic-gate	if (*pp == NULL) {
17847c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "%s: link(%s) not on list\n",
17857c478bdstevel@tonic-gate		    fcn, clp->path);
17867c478bdstevel@tonic-gate	} else {
17877c478bdstevel@tonic-gate		*pp = clp->sib;
17887c478bdstevel@tonic-gate	}
17897c478bdstevel@tonic-gate
17907c478bdstevel@tonic-gate	delete_unused_minor(hdp, clp->minor);
17917c478bdstevel@tonic-gate
17927c478bdstevel@tonic-gate	clp->minor = NULL;
17937c478bdstevel@tonic-gate
17947c478bdstevel@tonic-gate	link_free(&clp);
17957c478bdstevel@tonic-gate}
17967c478bdstevel@tonic-gate
17977c478bdstevel@tonic-gatestatic void
17987c478bdstevel@tonic-gatedelete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp)
17997c478bdstevel@tonic-gate{
18007c478bdstevel@tonic-gate	if (cmnp == NULL)
18017c478bdstevel@tonic-gate		return;
18027c478bdstevel@tonic-gate
18037c478bdstevel@tonic-gate	if (cmnp->link != NULL)
18047c478bdstevel@tonic-gate		return;
18057c478bdstevel@tonic-gate
18067c478bdstevel@tonic-gate	dprintf(DBG_STEP, "delete_unused_minor: removing minor(%s)\n",
18077c478bdstevel@tonic-gate	    cmnp->name);
18087c478bdstevel@tonic-gate
18097c478bdstevel@tonic-gate	minor_delete(hdp, cmnp);
18107c478bdstevel@tonic-gate}
18117c478bdstevel@tonic-gate
18127c478bdstevel@tonic-gateint
18137c478bdstevel@tonic-gatedi_devlink_add_link(
18147c478bdstevel@tonic-gate	di_devlink_handle_t hdp,
18157c478bdstevel@tonic-gate	const char *link,
18167c478bdstevel@tonic-gate	const char *content,
18177c478bdstevel@tonic-gate	int flags)
18187c478bdstevel@tonic-gate{
18197c478bdstevel@tonic-gate	return (add_link(hdp, link, content, flags) != NULL ? 0 : -1);
18207c478bdstevel@tonic-gate}
18217c478bdstevel@tonic-gate
18227c478bdstevel@tonic-gatestatic cache_link_t *
18237c478bdstevel@tonic-gateadd_link(
18247c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
18257c478bdstevel@tonic-gate	const char *link,
18267c478bdstevel@tonic-gate	const char *content,
18277c478bdstevel@tonic-gate	int flags)
18287c478bdstevel@tonic-gate{
18297c478bdstevel@tonic-gate	uint32_t attr;
18307c478bdstevel@tonic-gate	cache_link_t *clp;
18317c478bdstevel@tonic-gate	cache_minor_t *cmnp;
18327c478bdstevel@tonic-gate	const char *fcn = "add_link";
18337c478bdstevel@tonic-gate
18347c478bdstevel@tonic-gate	if (hdp == NULL || DB_ERR(hdp) || link == NULL ||
18357c478bdstevel@tonic-gate	    link[0] == '/' || content == NULL || !link_flag(flags) ||
18367c478bdstevel@tonic-gate	    (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
18377c478bdstevel@tonic-gate		dprintf(DBG_ERR, "%s: %s: invalid args\n",
18387c478bdstevel@tonic-gate		    fcn, link ? link : "<NULL>");
18397c478bdstevel@tonic-gate		errno = EINVAL;
18407c478bdstevel@tonic-gate		return (NULL);
18417c478bdstevel@tonic-gate	}
18427c478bdstevel@tonic-gate
18437c478bdstevel@tonic-gate	if ((clp = link_hash(hdp, link, 0)) != NULL) {
18447c478bdstevel@tonic-gate		if (link_cmp(clp, content, LINK_TYPE(flags)) != 0) {
18457c478bdstevel@tonic-gate			(void) rm_link(hdp, link);
18467c478bdstevel@tonic-gate		} else {
18477c478bdstevel@tonic-gate			return (clp);
18487c478bdstevel@tonic-gate		}
18497c478bdstevel@tonic-gate	}
18507c478bdstevel@tonic-gate
18517c478bdstevel@tonic-gate	if (TYPE_PRI(flags)) {
18527c478bdstevel@tonic-gate		const char *minor_path = NULL;
18537c478bdstevel@tonic-gate
18547c478bdstevel@tonic-gate		if (!is_minor_node(content, &minor_path)) {
18557c478bdstevel@tonic-gate			(void) dprintf(DBG_ERR, "%s: invalid content(%s)"
18567c478bdstevel@tonic-gate			    " for primary link\n", fcn, content);
18577c478bdstevel@tonic-gate			errno = EINVAL;
18587c478bdstevel@tonic-gate			return (NULL);
18597c478bdstevel@tonic-gate		}
18607c478bdstevel@tonic-gate		if ((cmnp = lookup_minor(hdp, minor_path, NULL,
18617c478bdstevel@tonic-gate		    TYPE_CACHE|CREATE_FLAG)) == NULL) {
18627c478bdstevel@tonic-gate			return (NULL);
18637c478bdstevel@tonic-gate		}
18647c478bdstevel@tonic-gate		attr = A_PRIMARY;
18657c478bdstevel@tonic-gate	} else {
18667c478bdstevel@tonic-gate		/*
18677c478bdstevel@tonic-gate		 * Defer resolving a secondary link to a minor until the
18687c478bdstevel@tonic-gate		 * database is closed. This ensures that the primary link
18697c478bdstevel@tonic-gate		 * (required for a successful resolve) has also been created.
18707c478bdstevel@tonic-gate		 */
18717c478bdstevel@tonic-gate		cmnp = NULL;
18727c478bdstevel@tonic-gate		attr = A_SECONDARY;
18737c478bdstevel@tonic-gate	}
18747c478bdstevel@tonic-gate
18757c478bdstevel@tonic-gate	return (link_insert(hdp, cmnp, link, content, attr));
18767c478bdstevel@tonic-gate}
18777c478bdstevel@tonic-gate
18787c478bdstevel@tonic-gate/*
18797c478bdstevel@tonic-gate * Returns 0 on match or 1 otherwise.
18807c478bdstevel@tonic-gate */
18817c478bdstevel@tonic-gatestatic int
18827c478bdstevel@tonic-gatelink_cmp(cache_link_t *clp, const char *content, int type)
18837c478bdstevel@tonic-gate{
18847c478bdstevel@tonic-gate	if (strcmp(clp->content, content) != 0)
18857c478bdstevel@tonic-gate		return (1);
18867c478bdstevel@tonic-gate
18877c478bdstevel@tonic-gate	if (attr2type(clp->attr) != type)
18887c478bdstevel@tonic-gate		return (1);
18897c478bdstevel@tonic-gate
18907c478bdstevel@tonic-gate	return (0);
18917c478bdstevel@tonic-gate}
18927c478bdstevel@tonic-gate
18937c478bdstevel@tonic-gateint
18947c478bdstevel@tonic-gatedi_devlink_update(di_devlink_handle_t hdp)
18957c478bdstevel@tonic-gate{
18967c478bdstevel@tonic-gate	if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) {
18977c478bdstevel@tonic-gate		errno = EINVAL;
18987c478bdstevel@tonic-gate		return (-1);
18997c478bdstevel@tonic-gate	}
19007c478bdstevel@tonic-gate
19017c478bdstevel@tonic-gate	/*
19027c478bdstevel@tonic-gate	 * Reset the counter to schedule a synchronization with /dev on the next
19037c478bdstevel@tonic-gate	 * di_devlink_close().
19047c478bdstevel@tonic-gate	 */
19057c478bdstevel@tonic-gate	CACHE(hdp)->update_count = 0;
19067c478bdstevel@tonic-gate
19077c478bdstevel@tonic-gate	return (0);
19087c478bdstevel@tonic-gate}
19097c478bdstevel@tonic-gate
19107c478bdstevel@tonic-gatestatic int
19117c478bdstevel@tonic-gatesynchronize_db(di_devlink_handle_t hdp)
19127c478bdstevel@tonic-gate{
19137c478bdstevel@tonic-gate	int hval;
19147c478bdstevel@tonic-gate	cache_link_t *clp;
19157c478bdstevel@tonic-gate	char pdup[PATH_MAX];
19167c478bdstevel@tonic-gate	recurse_t rec = {NULL};
19177c478bdstevel@tonic-gate	const char *fcn = "synchronize_db";
19187c478bdstevel@tonic-gate
19197c478bdstevel@tonic-gate	rec.data = NULL;
19207c478bdstevel@tonic-gate	rec.fcn = cache_dev_link;
19217c478bdstevel@tonic-gate
19227c478bdstevel@tonic-gate	/*
19237c478bdstevel@tonic-gate	 * Walk through $ROOT/dev, reading every link and marking the
19247c478bdstevel@tonic-gate	 * corresponding cached version as valid(adding new links as needed).
19257c478bdstevel@tonic-gate	 * Then walk through the cache and remove all unmarked links.
19267c478bdstevel@tonic-gate	 */
19277c478bdstevel@tonic-gate	if (recurse_dev(hdp, &rec) != 0) {
19287c478bdstevel@tonic-gate		return (-1);
19297c478bdstevel@tonic-gate	}
19307c478bdstevel@tonic-gate
19317c478bdstevel@tonic-gate	for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) {
19327c478bdstevel@tonic-gate		for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) {
19337c478bdstevel@tonic-gate			if (GET_VALID_ATTR(clp->attr)) {
19347c478bdstevel@tonic-gate				CLR_VALID_ATTR(clp->attr);
19357c478bdstevel@tonic-gate				clp = clp->hash;
19367c478bdstevel@tonic-gate				continue;
19377c478bdstevel@tonic-gate			}
19387c478bdstevel@tonic-gate
19397c478bdstevel@tonic-gate			/*
19407c478bdstevel@tonic-gate			 * The link is stale, so remove it. Since the link
19417c478bdstevel@tonic-gate			 * will be destroyed, use a copy of the link path to
19427c478bdstevel@tonic-gate			 * invoke the remove function.
19437c478bdstevel@tonic-gate			 */
19447c478bdstevel@tonic-gate			(void) snprintf(pdup, sizeof (pdup), "%s", clp->path);
19457c478bdstevel@tonic-gate			clp = clp->hash;
19467c478bdstevel@tonic-gate			(void) dprintf(DBG_STEP, "%s: removing invalid link:"
19477c478bdstevel@tonic-gate			    " %s\n", fcn, pdup);
19487c478bdstevel@tonic-gate			(void) di_devlink_rm_link(hdp, pdup);
19497c478bdstevel@tonic-gate		}
19507c478bdstevel@tonic-gate	}
19517c478bdstevel@tonic-gate
19527c478bdstevel@tonic-gate	(void) dprintf(DBG_STEP, "%s: update completed\n", fcn);
19537c478bdstevel@tonic-gate
19547c478bdstevel@tonic-gate	return (0);
19557c478bdstevel@tonic-gate}
19567c478bdstevel@tonic-gate
19577c478bdstevel@tonic-gatestatic di_devlink_handle_t
19587c478bdstevel@tonic-gatedi_devlink_init_impl(const char *root, const char *name, uint_t flags)
19597c478bdstevel@tonic-gate{
19607c478bdstevel@tonic-gate	int	err = 0;
19617c478bdstevel@tonic-gate
19627c478bdstevel@tonic-gate	if ((flags != 0 && flags != DI_MAKE_LINK) ||
19637c478bdstevel@tonic-gate	    (flags == 0 && name != NULL)) {
19647c478bdstevel@tonic-gate		errno = EINVAL;
19657c478bdstevel@tonic-gate		return (NULL);
19667c478bdstevel@tonic-gate	}
19677c478bdstevel@tonic-gate
1968ff2aee4cth	if ((flags == DI_MAKE_LINK) &&
1969ff2aee4cth	    (err = devlink_create(root, name, DCA_DEVLINK_CACHE))) {
19707c478bdstevel@tonic-gate		errno = err;
19717c478bdstevel@tonic-gate		return (NULL);
19727c478bdstevel@tonic-gate	}
19737c478bdstevel@tonic-gate
19747c478bdstevel@tonic-gate	(void) dprintf(DBG_INFO, "devlink_init_impl: success\n");
19757c478bdstevel@tonic-gate
19767c478bdstevel@tonic-gate	return (devlink_snapshot(root));
19777c478bdstevel@tonic-gate}
19787c478bdstevel@tonic-gate
19797c478bdstevel@tonic-gatedi_devlink_handle_t
19807c478bdstevel@tonic-gatedi_devlink_init(const char *name, uint_t flags)
19817c478bdstevel@tonic-gate{
19827c478bdstevel@tonic-gate	return (di_devlink_init_impl("/", name, flags));
19837c478bdstevel@tonic-gate}
19847c478bdstevel@tonic-gate
19857c478bdstevel@tonic-gatedi_devlink_handle_t
19867c478bdstevel@tonic-gatedi_devlink_init_root(const char *root, const char *name, uint_t flags)
19877c478bdstevel@tonic-gate{
19887c478bdstevel@tonic-gate	return (di_devlink_init_impl(root, name, flags));
19897c478bdstevel@tonic-gate}
19907c478bdstevel@tonic-gate
19917c478bdstevel@tonic-gatestatic di_devlink_handle_t
19927c478bdstevel@tonic-gatedevlink_snapshot(const char *root_dir)
19937c478bdstevel@tonic-gate{
19947c478bdstevel@tonic-gate	struct di_devlink_handle *hdp;
1995f7b6b8ccth	int		err;
1996f7b6b8ccth	static int	retried = 0;
19977c478bdstevel@tonic-gate
19987c478bdstevel@tonic-gate	if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) {
19997c478bdstevel@tonic-gate		return (NULL);
20007c478bdstevel@tonic-gate	}
20017c478bdstevel@tonic-gate
20027c478bdstevel@tonic-gate	/*
2003f7b6b8ccth	 * We don't need to lock.  If a consumer wants the very latest db
200448bbca8Daniel Hoffman	 * then it must perform a di_devlink_init with the DI_MAKE_LINK
2005f7b6b8ccth	 * flag to force a sync with devfsadm first.  Otherwise, the
2006f7b6b8ccth	 * current database file is opened and mmaped on demand: the rename
2007f7b6b8ccth	 * associated with a db update does not change the contents
2008f7b6b8ccth	 * of files already opened.
2009ff2aee4cth	 */
2010f7b6b8ccthagain:	err = open_db(hdp, OPEN_RDONLY);
2011ff2aee4cth
2012ff2aee4cth	/*
2013ff2aee4cth	 * If we failed to open DB the most likely cause is that DB file did
2014ff2aee4cth	 * not exist. If we have not done a retry, signal devfsadmd to
2015f7b6b8ccth	 * recreate the DB file and retry. If we fail to open the DB after
2016f7b6b8ccth	 * retry, we will walk /dev in di_devlink_walk.
20177c478bdstevel@tonic-gate	 */
2018ff2aee4cth	if (err && (retried == 0)) {
2019ff2aee4cth		retried++;
2020ff2aee4cth		(void) devlink_create(root_dir, NULL, DCA_DEVLINK_SYNC);
2021ff2aee4cth		goto again;
2022ff2aee4cth	}
20237c478bdstevel@tonic-gate	return (hdp);
20247c478bdstevel@tonic-gate}
20257c478bdstevel@tonic-gate
20267c478bdstevel@tonic-gateint
20277c478bdstevel@tonic-gatedi_devlink_fini(di_devlink_handle_t *pp)
20287c478bdstevel@tonic-gate{
20297c478bdstevel@tonic-gate	if (pp == NULL || *pp == NULL || !HDL_RDONLY(*pp)) {
20307c478bdstevel@tonic-gate		errno = EINVAL;
20317c478bdstevel@tonic-gate		return (-1);
20327c478bdstevel@tonic-gate	}
20337c478bdstevel@tonic-gate
20347c478bdstevel@tonic-gate	/* Freeing the handle also closes the DB */
20357c478bdstevel@tonic-gate	handle_free(pp);
20367c478bdstevel@tonic-gate
20377c478bdstevel@tonic-gate	return (0);
20387c478bdstevel@tonic-gate}
20397c478bdstevel@tonic-gate
20407c478bdstevel@tonic-gateint
20417c478bdstevel@tonic-gatedi_devlink_walk(
20427c478bdstevel@tonic-gate	di_devlink_handle_t hdp,
20437c478bdstevel@tonic-gate	const char *re,
20447c478bdstevel@tonic-gate	const char *minor_path,
20457c478bdstevel@tonic-gate	uint_t flags,
20467c478bdstevel@tonic-gate	void *arg,
20477c478bdstevel@tonic-gate	int (*devlink_callback)(di_devlink_t, void *))
20487c478bdstevel@tonic-gate{
20497c478bdstevel@tonic-gate	int rv;
20507c478bdstevel@tonic-gate	regex_t reg;
20517c478bdstevel@tonic-gate	link_desc_t linkd = {NULL};
20527c478bdstevel@tonic-gate
20537c478bdstevel@tonic-gate	if (hdp == NULL || !HDL_RDONLY(hdp)) {
20547c478bdstevel@tonic-gate		errno = EINVAL;
20557c478bdstevel@tonic-gate		return (-1);
20567c478bdstevel@tonic-gate	}
20577c478bdstevel@tonic-gate
20587c478bdstevel@tonic-gate	linkd.minor_path = minor_path;
20597c478bdstevel@tonic-gate	linkd.flags = flags;
20607c478bdstevel@tonic-gate	linkd.arg = arg;
20617c478bdstevel@tonic-gate	linkd.fcn = devlink_callback;
20627c478bdstevel@tonic-gate
20637c478bdstevel@tonic-gate	if (re) {
20647c478bdstevel@tonic-gate		if (regcomp(&reg, re, REG_EXTENDED) != 0)
20657c478bdstevel@tonic-gate			return (-1);
20667c478bdstevel@tonic-gate		linkd.regp = &reg;
20677c478bdstevel@tonic-gate	}
20687c478bdstevel@tonic-gate
20697c478bdstevel@tonic-gate	if (check_args(&linkd)) {
20707c478bdstevel@tonic-gate		errno = EINVAL;
20717c478bdstevel@tonic-gate		rv = -1;
20727c478bdstevel@tonic-gate		goto out;
20737c478bdstevel@tonic-gate	}
20747c478bdstevel@tonic-gate
20757c478bdstevel@tonic-gate	if (DB_OPEN(hdp)) {
20767c478bdstevel@tonic-gate		rv = walk_db(hdp, &linkd);
20777c478bdstevel@tonic-gate	} else {
20787c478bdstevel@tonic-gate		rv = walk_dev(hdp, &linkd);
20797c478bdstevel@tonic-gate	}
20807c478bdstevel@tonic-gate
20817c478bdstevel@tonic-gateout:
20827c478bdstevel@tonic-gate	if (re) {
20837c478bdstevel@tonic-gate		regfree(&reg);
20847c478bdstevel@tonic-gate	}
20857c478bdstevel@tonic-gate
20867c478bdstevel@tonic-gate	return (rv ? -1 : 0);
20877c478bdstevel@tonic-gate}
20887c478bdstevel@tonic-gate
20897c478bdstevel@tonic-gatestatic int
20907c478bdstevel@tonic-gatelink_flag(uint_t flags)
20917c478bdstevel@tonic-gate{
20927c478bdstevel@tonic-gate	if (flags != 0 && flags != DI_PRIMARY_LINK &&
20937c478bdstevel@tonic-gate	    flags != DI_SECONDARY_LINK) {
20947c478bdstevel@tonic-gate		return (0);
20957c478bdstevel@tonic-gate	}
20967c478bdstevel@tonic-gate
20977c478bdstevel@tonic-gate	return (1);
20987c478bdstevel@tonic-gate}
20997c478bdstevel@tonic-gate
21007c478bdstevel@tonic-gate/*
21017c478bdstevel@tonic-gate * Currently allowed flags are:
21027c478bdstevel@tonic-gate *	DI_PRIMARY_LINK
21037c478bdstevel@tonic-gate *	DI_SECONDARY_LINK
21047c478bdstevel@tonic-gate */
21057c478bdstevel@tonic-gatestatic int
21067c478bdstevel@tonic-gatecheck_args(link_desc_t *linkp)
21077c478bdstevel@tonic-gate{
21087c478bdstevel@tonic-gate	if (linkp->fcn == NULL)
21097c478bdstevel@tonic-gate		return (-1);
21107c478bdstevel@tonic-gate
21117c478bdstevel@tonic-gate	if (!link_flag(linkp->flags)) {
21127c478bdstevel@tonic-gate		return (-1);
21137c478bdstevel@tonic-gate	}
21147c478bdstevel@tonic-gate
21157c478bdstevel@tonic-gate	/*
21167c478bdstevel@tonic-gate	 * Minor path can be NULL. In that case, all links will be
21177c478bdstevel@tonic-gate	 * selected.
21187c478bdstevel@tonic-gate	 */
21197c478bdstevel@tonic-gate	if (linkp->minor_path) {
21207c478bdstevel@tonic-gate		if (linkp->minor_path[0] != '/' ||
21217c478bdstevel@tonic-gate		    minor_colon(linkp->minor_path) == NULL) {
21227c478bdstevel@tonic-gate			return (-1);
21237c478bdstevel@tonic-gate		}
21247c478bdstevel@tonic-gate	}
21257c478bdstevel@tonic-gate
21267c478bdstevel@tonic-gate	return (0);
21277c478bdstevel@tonic-gate}
21287c478bdstevel@tonic-gate
21297c478bdstevel@tonic-gate
21307c478bdstevel@tonic-gate/*
21317c478bdstevel@tonic-gate * Walk all links in database if no minor path is specified.
21327c478bdstevel@tonic-gate */
21337c478bdstevel@tonic-gatestatic int
21347c478bdstevel@tonic-gatewalk_db(struct di_devlink_handle *hdp, link_desc_t *linkp)
21357c478bdstevel@tonic-gate{
21367c478bdstevel@tonic-gate	assert(DB_OPEN(hdp));
21377c478bdstevel@tonic-gate
21387c478bdstevel@tonic-gate	if (linkp->minor_path == NULL) {
21397c478bdstevel@tonic-gate		return (walk_all_links(hdp, linkp));
21407c478bdstevel@tonic-gate	} else {
21417c478bdstevel@tonic-gate		return (walk_matching_links(hdp, linkp));
21427c478bdstevel@tonic-gate	}
21437c478bdstevel@tonic-gate}
21447c478bdstevel@tonic-gate
21457c478bdstevel@tonic-gatestatic int
21467c478bdstevel@tonic-gatecache_dev(struct di_devlink_handle *hdp)
21477c478bdstevel@tonic-gate{
21487c478bdstevel@tonic-gate	size_t sz;
21497c478bdstevel@tonic-gate	recurse_t rec = {NULL};
21507c478bdstevel@tonic-gate
21517c478bdstevel@tonic-gate	assert(hdp);
21527c478bdstevel@tonic-gate	assert(HDL_RDONLY(hdp));
21537c478bdstevel@tonic-gate
21547c478bdstevel@tonic-gate	if (hdp == NULL || !HDL_RDONLY(hdp)) {
21557c478bdstevel@tonic-gate		dprintf(DBG_ERR, "cache_dev: invalid arg\n");
21567c478bdstevel@tonic-gate		return (-1);
21577c478bdstevel@tonic-gate	}
21587c478bdstevel@tonic-gate
21597c478bdstevel@tonic-gate	sz = MIN_HASH_SIZE;
21607c478bdstevel@tonic-gate
21617c478bdstevel@tonic-gate	CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *));
21627c478bdstevel@tonic-gate	if (CACHE(hdp)->hash == NULL) {
21637c478bdstevel@tonic-gate		return (-1);
21647c478bdstevel@tonic-gate	}
21657c478bdstevel@tonic-gate	CACHE(hdp)->hash_sz = sz;
21667c478bdstevel@tonic-gate
21677c478bdstevel@tonic-gate	rec.data = NULL;
21687c478bdstevel@tonic-gate	rec.fcn = cache_dev_link;
21697c478bdstevel@tonic-gate
21707c478bdstevel@tonic-gate	return (recurse_dev(hdp, &rec));
21717c478bdstevel@tonic-gate}
21727c478bdstevel@tonic-gate
21737c478bdstevel@tonic-gatestatic int
21747c478bdstevel@tonic-gatewalk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp)
21757c478bdstevel@tonic-gate{
21767c478bdstevel@tonic-gate	assert(hdp && linkp);
21777c478bdstevel@tonic-gate	assert(!DB_OPEN(hdp));
21787c478bdstevel@tonic-gate	assert(HDL_RDONLY(hdp));
21797c478bdstevel@tonic-gate
21807c478bdstevel@tonic-gate	if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) {
21817c478bdstevel@tonic-gate		dprintf(DBG_ERR, "walk_dev: invalid args\n");
21827c478bdstevel@tonic-gate		return (-1);
21837c478bdstevel@tonic-gate	}
21847c478bdstevel@tonic-gate
21857c478bdstevel@tonic-gate	if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) {
21867c478bdstevel@tonic-gate		dprintf(DBG_ERR, "walk_dev: /dev caching failed\n");
21877c478bdstevel@tonic-gate		return (-1);
21887c478bdstevel@tonic-gate	}
21897c478bdstevel@tonic-gate
21907c478bdstevel@tonic-gate	if (linkp->minor_path)
21917c478bdstevel@tonic-gate		walk_cache_minor(hdp, linkp->minor_path, linkp);
21927c478bdstevel@tonic-gate	else
21937c478bdstevel@tonic-gate		walk_all_cache(hdp, linkp);
21947c478bdstevel@tonic-gate
21957c478bdstevel@tonic-gate	return (linkp->retval);
21967c478bdstevel@tonic-gate}
21977c478bdstevel@tonic-gate
21987c478bdstevel@tonic-gate/* ARGSUSED */
21997c478bdstevel@tonic-gatestatic int
22007c478bdstevel@tonic-gatecache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link)
22017c478bdstevel@tonic-gate{
22027c478bdstevel@tonic-gate	int flags;
22037c478bdstevel@tonic-gate	cache_link_t *clp;
22047c478bdstevel@tonic-gate	char content[PATH_MAX];
22057c478bdstevel@tonic-gate
22067c478bdstevel@tonic-gate	assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
22077c478bdstevel@tonic-gate
22087c478bdstevel@tonic-gate	if (s_readlink(link, content, sizeof (content)) < 0) {
22097c478bdstevel@tonic-gate		return (DI_WALK_CONTINUE);
22107c478bdstevel@tonic-gate	}
22117c478bdstevel@tonic-gate
22127c478bdstevel@tonic-gate	if (is_minor_node(content, NULL)) {
22137c478bdstevel@tonic-gate		flags = DI_PRIMARY_LINK;
22147c478bdstevel@tonic-gate	} else {
22157c478bdstevel@tonic-gate		flags = DI_SECONDARY_LINK;
22167c478bdstevel@tonic-gate	}
22177c478bdstevel@tonic-gate
22187c478bdstevel@tonic-gate	assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0);
22197c478bdstevel@tonic-gate
22207c478bdstevel@tonic-gate	/*
22217c478bdstevel@tonic-gate	 * Store only the part after <root-dir>/dev/
22227c478bdstevel@tonic-gate	 */
22237c478bdstevel@tonic-gate	link += strlen(hdp->dev_dir) + 1;
22247c478bdstevel@tonic-gate
22257c478bdstevel@tonic-gate	if ((clp = add_link(hdp, link, content, flags)) != NULL) {
22267c478bdstevel@tonic-gate		SET_VALID_ATTR(clp->attr);
22277c478bdstevel@tonic-gate	}
22287c478bdstevel@tonic-gate
22297c478bdstevel@tonic-gate	return (DI_WALK_CONTINUE);
22307c478bdstevel@tonic-gate}
22317c478bdstevel@tonic-gate
22327c478bdstevel@tonic-gate
22337c478bdstevel@tonic-gatestatic int
22347c478bdstevel@tonic-gatewalk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
22357c478bdstevel@tonic-gate{
22367c478bdstevel@tonic-gate	struct db_link *dlp;
22377c478bdstevel@tonic-gate	uint32_t nidx, eidx;
22387c478bdstevel@tonic-gate
22397c478bdstevel@tonic-gate	assert(DB_NUM(hdp, DB_LINK) >= 1);
22407c478bdstevel@tonic-gate
22417c478bdstevel@tonic-gate	eidx = DB_NUM(hdp, DB_LINK);
22427c478bdstevel@tonic-gate
22437c478bdstevel@tonic-gate	/* Skip the "NIL" (index == 0) link. */
22447c478bdstevel@tonic-gate	for (nidx = 1; nidx < eidx; nidx++) {
22457c478bdstevel@tonic-gate		/*
22467c478bdstevel@tonic-gate		 * Declare this local to the block with zero
22477c478bdstevel@tonic-gate		 * initializer so that it gets rezeroed
22487c478bdstevel@tonic-gate		 * for each iteration.
22497c478bdstevel@tonic-gate		 */
22507c478bdstevel@tonic-gate		struct di_devlink vlink = {NULL};
22517c478bdstevel@tonic-gate
22527c478bdstevel@tonic-gate		if ((dlp = get_link(hdp, nidx)) == NULL)
22537c478bdstevel@tonic-gate			continue;
22547c478bdstevel@tonic-gate
22557c478bdstevel@tonic-gate		vlink.rel_path = get_string(hdp, dlp->path);
22567c478bdstevel@tonic-gate		vlink.content = get_string(hdp, dlp->content);
22577c478bdstevel@tonic-gate		vlink.type = attr2type(dlp->attr);
22587c478bdstevel@tonic-gate
22597c478bdstevel@tonic-gate		if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) {
22607c478bdstevel@tonic-gate			break;
22617c478bdstevel@tonic-gate		}
22627c478bdstevel@tonic-gate	}
22637c478bdstevel@tonic-gate
22647c478bdstevel@tonic-gate	return (linkp->retval);
22657c478bdstevel@tonic-gate}
22667c478bdstevel@tonic-gate
22677c478bdstevel@tonic-gatestatic int
22687c478bdstevel@tonic-gatewalk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
22697c478bdstevel@tonic-gate{
22707c478bdstevel@tonic-gate	uint32_t nidx;
22717c478bdstevel@tonic-gate	struct db_link *dlp;
22727c478bdstevel@tonic-gate	struct db_minor *dmp;
22737c478bdstevel@tonic-gate
22747c478bdstevel@tonic-gate	assert(linkp->minor_path != NULL);
22757c478bdstevel@tonic-gate
22767c478bdstevel@tonic-gate	dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB);
22777c478bdstevel@tonic-gate
22787c478bdstevel@tonic-gate	/*
22797c478bdstevel@tonic-gate	 * If a minor matching the path exists, walk that minor's devlinks list.
22807c478bdstevel@tonic-gate	 * Then walk the dangling devlinks list. Non-matching devlinks will be
22817c478bdstevel@tonic-gate	 * filtered out in visit_link.
22827c478bdstevel@tonic-gate	 */
22837c478bdstevel@tonic-gate	for (;;) {
22847c478bdstevel@tonic-gate		nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx;
22857c478bdstevel@tonic-gate		for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
22867c478bdstevel@tonic-gate			struct di_devlink vlink = {NULL};
22877c478bdstevel@tonic-gate
22887c478bdstevel@tonic-gate			vlink.rel_path = get_string(hdp, dlp->path);
22897c478bdstevel@tonic-gate			vlink.content = get_string(hdp, dlp->content);
22907c478bdstevel@tonic-gate			vlink.type = attr2type(dlp->attr);
22917c478bdstevel@tonic-gate
22927c478bdstevel@tonic-gate			if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE)
22937c478bdstevel@tonic-gate				goto out;
22947c478bdstevel@tonic-gate		}
22957c478bdstevel@tonic-gate		if (dmp == NULL) {
22967c478bdstevel@tonic-gate			break;
22977c478bdstevel@tonic-gate		} else {
22987c478bdstevel@tonic-gate			dmp = NULL;
22997c478bdstevel@tonic-gate		}
23007c478bdstevel@tonic-gate	}
23017c478bdstevel@tonic-gate
23027c478bdstevel@tonic-gateout:
23037c478bdstevel@tonic-gate	return (linkp->retval);
23047c478bdstevel@tonic-gate}
23057c478bdstevel@tonic-gate
23067c478bdstevel@tonic-gatestatic int
23077c478bdstevel@tonic-gatevisit_link(
23087c478bdstevel@tonic-gate	struct di_devlink_handle *hdp,
23097c478bdstevel@tonic-gate	link_desc_t *linkp,
23107c478bdstevel@tonic-gate	struct di_devlink *vlp)
23117c478bdstevel@tonic-gate{
23127c478bdstevel@tonic-gate	struct stat sbuf;
23137c478bdstevel@tonic-gate	const char *minor_path = NULL;
23147c478bdstevel@tonic-gate	char abs_path[PATH_MAX], cont[PATH_MAX];
23157c478bdstevel@tonic-gate
23167c478bdstevel@tonic-gate	/*
23177c478bdstevel@tonic-gate	 * It is legal for the link's content and type to be unknown.
23187c478bdstevel@tonic-gate	 * but one of absolute or relative path must be set.
23197c478bdstevel@tonic-gate	 */
23207c478bdstevel@tonic-gate	if (vlp->rel_path == NULL && vlp->abs_path == NULL) {
23217c478bdstevel@tonic-gate		(void) dprintf(DBG_ERR, "visit_link: invalid arguments\n");
23227c478bdstevel@tonic-gate		return (DI_WALK_CONTINUE);
23237c478bdstevel@tonic-gate	}
23247c478bdstevel@tonic-gate
23257c478bdstevel@tonic-gate	if (vlp->rel_path == NULL) {
23267c478bdstevel@tonic-gate		vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path);
23277c478bdstevel@tonic-gate		if (vlp->rel_path == NULL || vlp->rel_path[0] == '\0')
23287c478bdstevel@tonic-gate			return (DI_WALK_CONTINUE);
23297c478bdstevel@tonic-gate	}
23307c478bdstevel@tonic-gate
23317c478bdstevel@tonic-gate	if (linkp->regp) {
23327c478bdstevel@tonic-gate		if (regexec(linkp->regp, vlp->rel_path, 0, NULL, 0) != 0)
23337c478bdstevel@tonic-gate			return (DI_WALK_CONTINUE);
23347c478bdstevel@tonic-gate	}
23357c478bdstevel@tonic-gate
23367c478bdstevel@tonic-gate	if (vlp->abs_path == NULL) {
23377c478bdstevel@tonic-gate		assert(vlp->rel_path[0] != '/');
23387c478bdstevel@tonic-gate		(void) snprintf(abs_path, sizeof (abs_path), "%s/%s",
23397c478bdstevel@tonic-gate		    hdp->dev_dir, vlp->rel_path);
23407c478bdstevel@tonic-gate		vlp->abs_path = abs_path;
23417c478bdstevel@tonic-gate	}
23427c478bdstevel@tonic-gate
23437c478bdstevel@tonic-gate	if (vlp->content == NULL) {
23447c478bdstevel@tonic-gate		if (s_readlink(vlp->abs_path, cont, sizeof (cont)) < 0) {
23457c478bdstevel@tonic-gate			return (DI_WALK_CONTINUE);
23467c478bdstevel@tonic-gate		}
23477c478bdstevel@tonic-gate		vlp->content = cont;
23487c478bdstevel@tonic-gate	}
23497c478bdstevel@tonic-gate
23507c478bdstevel@tonic-gate
23517c478bdstevel@tonic-gate	if (vlp->type == 0) {
23527c478bdstevel@tonic-gate		if (is_minor_node(vlp->content, &minor_path)) {
23537c478bdstevel@tonic-gate			vlp->type = DI_PRIMARY_LINK;