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