2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A *
2N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A
2N/A#ifndef _DEVINFO_DEVLINK_H
2N/A#define _DEVINFO_DEVLINK_H
2N/A
2N/A#ifdef __cplusplus
2N/Aextern "C" {
2N/A#endif
2N/A
2N/A#define _POSIX_PTHREAD_SEMANTICS /* For readdir_r */
2N/A
2N/A#include <stdio.h>
2N/A#include <unistd.h>
2N/A#include <fcntl.h>
2N/A#include <string.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <libdevinfo.h>
2N/A#include <limits.h>
2N/A#include <stdlib.h>
2N/A#include <dirent.h>
2N/A#include <regex.h>
2N/A#include <errno.h>
2N/A#include <stdarg.h>
2N/A#include <sys/uio.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/time.h>
2N/A#include <sys/mman.h>
2N/A#include <sys/wait.h>
2N/A#include <door.h>
2N/A#include <signal.h>
2N/A#include <sys/statvfs.h>
2N/A
2N/Astruct db_link {
2N/A uint32_t attr; /* primary or secondary */
2N/A uint32_t path; /* link path */
2N/A uint32_t content; /* link content */
2N/A uint32_t sib; /* next link for same minor */
2N/A};
2N/A
2N/Astruct db_minor {
2N/A uint32_t name; /* minor name */
2N/A uint32_t nodetype; /* minor node type */
2N/A uint32_t sib; /* next minor for same node */
2N/A uint32_t link; /* next minor for same node */
2N/A};
2N/A
2N/Astruct db_node {
2N/A uint32_t path; /* node path */
2N/A uint32_t sib; /* node's sibling */
2N/A uint32_t child; /* first child for this node */
2N/A uint32_t minor; /* first minor for node */
2N/A};
2N/A
2N/Atypedef enum db_seg {
2N/A DB_NODE = 0,
2N/A DB_MINOR,
2N/A DB_LINK,
2N/A DB_STR,
2N/A DB_TYPES, /* Number of non-header segments */
2N/A DB_HEADER
2N/A} db_seg_t;
2N/A
2N/Astruct db_hdr {
2N/A uint32_t magic; /* Magic number */
2N/A uint32_t vers; /* database format version */
2N/A uint32_t root_idx; /* index for root node */
2N/A uint32_t dngl_idx; /* head of DB dangling links */
2N/A uint32_t page_sz; /* page size for mmap alignment */
2N/A uint32_t update_count; /* updates since last /dev synch up */
2N/A uint32_t nelems[DB_TYPES]; /* Number of elements of each type */
2N/A};
2N/A
2N/A
2N/Atypedef struct cache_link {
2N/A char *path; /* link path */
2N/A char *content; /* link content */
2N/A uint_t attr; /* link attributes */
2N/A struct cache_link *hash; /* next link on same hash chain */
2N/A struct cache_link *sib; /* next link for same minor */
2N/A struct cache_minor *minor; /* minor for this link */
2N/A} cache_link_t;
2N/A
2N/Atypedef struct cache_minor {
2N/A char *name; /* minor name */
2N/A char *nodetype; /* minor nodetype */
2N/A struct cache_node *node; /* node for this minor */
2N/A struct cache_minor *sib; /* next minor for same node */
2N/A struct cache_link *link; /* first link pointing to minor */
2N/A} cache_minor_t;
2N/A
2N/Atypedef struct cache_node {
2N/A char *path; /* path */
2N/A struct cache_node *parent; /* node's parent */
2N/A struct cache_node *sib; /* node's sibling */
2N/A struct cache_node *child; /* first child for this node */
2N/A struct cache_minor *minor; /* first minor for node */
2N/A} cache_node_t;
2N/A
2N/Astruct cache {
2N/A uint_t flags; /* cache state */
2N/A uint_t update_count; /* updates since /dev synchronization */
2N/A uint_t hash_sz; /* number of hash chains */
2N/A cache_link_t **hash; /* hash table */
2N/A cache_node_t *root; /* root of cache tree */
2N/A cache_link_t *dngl; /* list of dangling links */
2N/A cache_minor_t *last_minor; /* last minor looked up */
2N/A};
2N/A
2N/Astruct db {
2N/A int db_fd; /* database file */
2N/A uint_t flags; /* database open mode */
2N/A struct db_hdr *hdr; /* DB header */
2N/A int seg_prot[DB_TYPES]; /* protection for segments */
2N/A caddr_t seg_base[DB_TYPES]; /* base address for segments */
2N/A};
2N/A
2N/Astruct di_devlink_handle {
2N/A char *dev_dir; /* <root-dir>/dev */
2N/A char *db_dir; /* <root-dir>/etc/dev */
2N/A uint_t flags; /* handle flags */
2N/A uint_t error; /* records errors encountered */
2N/A int lock_fd; /* lock file for updates */
2N/A struct cache cache;
2N/A struct db db;
2N/A};
2N/A
2N/Atypedef struct link_desc {
2N/A regex_t *regp;
2N/A const char *minor_path;
2N/A uint_t flags;
2N/A void *arg;
2N/A int (*fcn)(di_devlink_t, void *);
2N/A int retval;
2N/A} link_desc_t;
2N/A
2N/Astruct tnode {
2N/A void *node;
2N/A int flags;
2N/A struct di_devlink_handle *handle;
2N/A};
2N/A
2N/Astruct di_devlink {
2N/A char *rel_path;
2N/A char *abs_path;
2N/A char *content;
2N/A int type;
2N/A};
2N/A
2N/Atypedef struct recurse {
2N/A void *data;
2N/A int (*fcn)(struct di_devlink_handle *, void *, const char *);
2N/A} recurse_t;
2N/A
2N/A/*
2N/A * Debug levels currently defined.
2N/A */
2N/Atypedef enum {
2N/A DBG_ERR = 1,
2N/A DBG_LCK,
2N/A DBG_INFO,
2N/A DBG_STEP,
2N/A DBG_ALL
2N/A} debug_level_t;
2N/A
2N/A
2N/A#define DB_MAGIC 0xBAC2ACAB
2N/A#define DB_FILE ".devlink_db"
2N/A#define DB_TMP ".devlink_db_tmp"
2N/A#define DB_LOCK ".devlink_db_lock"
2N/A#define DB_PERMS (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR)
2N/A#define DB_LOCK_PERMS DB_PERMS
2N/A#define DB_VERSION 1
2N/A
2N/A#define DB_NIL 0
2N/A
2N/A#define DEV "/dev"
2N/A#define ETCDEV "/etc/dev"
2N/A#define DEVICES_SUFFIX "ices"
2N/A
2N/A#define HDR_LEN sizeof (struct db_hdr)
2N/A
2N/A#define AVG_CHAIN_SIZE 20 /* Average number of links per chain */
2N/A#define MIN_HASH_SIZE 1024 /* Min number of chains in hash table */
2N/A#define MAX_UPDATE_INTERVAL 5 /* Max DB writes before synching with /dev */
2N/A#define MAX_LOCK_RETRY 5 /* Max attempts at locking the update lock */
2N/A
2N/A/*
2N/A * Various flags private to the implementation
2N/A */
2N/A#define A_PRIMARY 0x0001U
2N/A#define A_SECONDARY 0x0002U
2N/A#define A_LINK_TYPES 0x0003U /* Mask */
2N/A#define A_VALID 0x0004U
2N/A
2N/A#define TYPE_DB 0x0008U
2N/A#define TYPE_CACHE 0x0010U
2N/A#define CREATE_FLAG 0x0020U
2N/A
2N/A#define INSERT_HEAD 0x0040U
2N/A#define INSERT_TAIL 0x0080U
2N/A#define OPEN_RDWR 0x0100U
2N/A#define OPEN_RDONLY 0x0200U
2N/A#define OPEN_FLAGS 0x0300U /* Mask */
2N/A#define UNLINK_FROM_HASH 0x0400U
2N/A
2N/A#define SET_VALID_ATTR(a) ((a) |= A_VALID)
2N/A#define CLR_VALID_ATTR(a) ((a) &= ~A_VALID)
2N/A#define GET_VALID_ATTR(a) ((a) & A_VALID)
2N/A
2N/A#define SET_DB_ERR(h) ((h)->error = 1)
2N/A#define DB_ERR(h) ((h)->error)
2N/A
2N/A#define LOOKUP_DB(f) ((f) & TYPE_DB)
2N/A#define LOOKUP_CACHE(f) ((f) & TYPE_CACHE)
2N/A#define CREATE_ELEM(f) ((f) & CREATE_FLAG)
2N/A
2N/A#define IS_RDWR(f) (((f) & OPEN_FLAGS) == OPEN_RDWR)
2N/A#define IS_RDONLY(f) (((f) & OPEN_FLAGS) == OPEN_RDONLY)
2N/A
2N/A#define HDL_RDWR(h) (((h)->flags & OPEN_FLAGS) == OPEN_RDWR)
2N/A#define HDL_RDONLY(h) (((h)->flags & OPEN_FLAGS) == OPEN_RDONLY)
2N/A
2N/A#define CACHE(h) (&(h)->cache)
2N/A#define CACHE_ROOT(h) (CACHE(h)->root)
2N/A#define CACHE_HASH(h, i) (CACHE(h)->hash[i])
2N/A#define CACHE_LAST(h) (CACHE(h)->last_minor)
2N/A#define CACHE_EMPTY(h) (CACHE(h)->root == NULL && CACHE(h)->dngl == NULL)
2N/A
2N/A#define DB(h) (&(h)->db)
2N/A#define DB_HDR(h) (DB(h)->hdr)
2N/A#define DB_NUM(h, t) (DB_HDR(h)->nelems[t])
2N/A#define DB_SEG(h, t) (DB(h)->seg_base[t])
2N/A#define DB_SEG_PROT(h, t) (DB(h)->seg_prot[t])
2N/A
2N/A#define DB_OPEN(h) (DB_HDR(h) != NULL)
2N/A#define DB_RDWR(h) ((DB(h)->flags & OPEN_FLAGS) == OPEN_RDWR)
2N/A#define DB_RDONLY(h) ((DB(h)->flags & OPEN_FLAGS) == OPEN_RDONLY)
2N/A
2N/A#define DB_EMPTY(h) (DB_HDR(h)->root_idx == DB_NIL && \
2N/A DB_HDR(h)->dngl_idx == DB_NIL)
2N/A
2N/A#define TYPE_NONE(f) (((f) & DI_LINK_TYPES) == 0)
2N/A#define TYPE_PRI(f) (((f) & DI_LINK_TYPES) == DI_PRIMARY_LINK)
2N/A#define TYPE_SEC(f) (((f) & DI_LINK_TYPES) == DI_SECONDARY_LINK)
2N/A#define LINK_TYPE(f) ((f) & DI_LINK_TYPES)
2N/A#define VALID_TYPE(f) (TYPE_NONE(f) || TYPE_PRI(f) || TYPE_SEC(f))
2N/A
2N/A#define VALID_STR(h, i, s) ((i) + strlen(s) + 1 <= DB_HDR(h)->nelems[DB_STR])
2N/A#define VALID_INDEX(h, t, i) ((i) < DB_HDR(h)->nelems[t])
2N/A
2N/A/*
2N/A * Environment variables used by DEBUG version of code.
2N/A */
2N/A#define SKIP_DB "DEBUG_SKIP_DB"
2N/A#define SKIP_LAST_CACHE "DEBUG_SKIP_LAST_CACHE"
2N/A#define ALT_DB_DIR "DEBUG_ALT_DB_DIR"
2N/A
2N/A/*
2N/A * Function prototypes
2N/A */
2N/Astatic struct di_devlink_handle *handle_alloc(const char *dev_dir,
2N/A uint_t flags);
2N/Astatic int cache_alloc(struct di_devlink_handle *hdp);
2N/Astatic int open_db(struct di_devlink_handle *hdp, int flags);
2N/Astatic int invalid_db(struct di_devlink_handle *hdp, size_t fsize, long pg_sz);
2N/Astatic int read_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp,
2N/A uint32_t nidx);
2N/Astatic int read_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp,
2N/A uint32_t nidx);
2N/Astatic int read_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp,
2N/A uint32_t nidx);
2N/Astatic int init_hdr(struct di_devlink_handle *hdp, long page_sz,
2N/A uint32_t *count);
2N/Astatic size_t size_db(struct di_devlink_handle *hdp, long page_sz,
2N/A uint32_t *count);
2N/Astatic size_t seg_size(struct di_devlink_handle *hdp, int seg);
2N/A
2N/Astatic cache_node_t *node_insert(struct di_devlink_handle *hdp,
2N/A cache_node_t *pcnp, const char *path, int insert);
2N/Astatic cache_minor_t *minor_insert(struct di_devlink_handle *hdp,
2N/A cache_node_t *pcnp, const char *name, const char *nodetype,
2N/A cache_minor_t **prev);
2N/Astatic cache_link_t *link_insert(struct di_devlink_handle *hdp,
2N/A cache_minor_t *mnp, const char *path, const char *content, uint32_t attr);
2N/A
2N/Astatic void minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp);
2N/Astatic void link_delete(di_devlink_handle_t hdp, cache_link_t *clp);
2N/A
2N/Astatic int write_nodes(struct di_devlink_handle *hdp, struct db_node *pdnp,
2N/A cache_node_t *cnp, uint32_t *next);
2N/Astatic int write_minors(struct di_devlink_handle *hdp, struct db_node *pdnp,
2N/A cache_minor_t *cmnp, uint32_t *next);
2N/Astatic int write_links(struct di_devlink_handle *hdp, struct db_minor *pdmp,
2N/A cache_link_t *clp, uint32_t *next);
2N/Astatic void rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp);
2N/Astatic uint32_t write_string(struct di_devlink_handle *hdp, const char *str,
2N/A uint32_t *next);
2N/Astatic int close_db(struct di_devlink_handle *hdp);
2N/Astatic void cache_free(struct di_devlink_handle *hdp);
2N/Astatic void handle_free(struct di_devlink_handle **pp);
2N/Astatic void resolve_dangling_links(struct di_devlink_handle *hdp);
2N/Astatic void subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp);
2N/Astatic void node_free(cache_node_t **pp);
2N/Astatic void minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp);
2N/Astatic void link_free(cache_link_t **pp);
2N/Astatic void count_node(cache_node_t *cnp, uint32_t *count);
2N/Astatic void count_minor(cache_minor_t *mnp, uint32_t *count);
2N/Astatic void count_link(cache_link_t *clp, uint32_t *count);
2N/Astatic void count_string(const char *str, uint32_t *count);
2N/Astatic int visit_node(const char *path, void *arg);
2N/Astatic int walk_tree(char *cur, void *arg,
2N/A int (*node_callback)(const char *path, void *arg));
2N/Astatic void *lookup_node(struct di_devlink_handle *hdp, char *path,
2N/A const int flags);
2N/Astatic cache_link_t *add_link(struct di_devlink_handle *hdp, const char *link,
2N/A const char *content, int primary);
2N/A
2N/Astatic void *lookup_minor(struct di_devlink_handle *hdp, const char *minor_path,
2N/A const char *nodetype, const int flags);
2N/Astatic cache_link_t *link_hash(di_devlink_handle_t hdp, const char *link,
2N/A uint_t flags);
2N/A
2N/Astatic void hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp);
2N/Astatic uint_t hashfn(struct di_devlink_handle *hdp, const char *str);
2N/Astatic void get_db_path(struct di_devlink_handle *hdp, const char *fname,
2N/A char *buf, size_t blen);
2N/A
2N/Astatic struct db_node *get_node(struct di_devlink_handle *hdp, uint32_t idx);
2N/Astatic struct db_node *set_node(struct di_devlink_handle *hdp, uint32_t idx);
2N/A
2N/Astatic struct db_minor *get_minor(struct di_devlink_handle *hdp, uint32_t idx);
2N/Astatic struct db_minor *set_minor(struct di_devlink_handle *hdp, uint32_t idx);
2N/A
2N/Astatic struct db_link *get_link(struct di_devlink_handle *hdp, uint32_t idx);
2N/Astatic struct db_link *set_link(struct di_devlink_handle *hdp, uint32_t idx);
2N/A
2N/Astatic char *get_string(struct di_devlink_handle *hdp, uint32_t idx);
2N/Astatic char *set_string(struct di_devlink_handle *hdp, uint32_t idx);
2N/A
2N/Astatic void *map_seg(struct di_devlink_handle *hdp, uint32_t idx, int prot,
2N/A db_seg_t seg);
2N/A
2N/Astatic int walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp);
2N/Astatic int walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp);
2N/Astatic int walk_matching_links(struct di_devlink_handle *hdp,
2N/A link_desc_t *linkp);
2N/Astatic int visit_link(struct di_devlink_handle *hdp, link_desc_t *linkp,
2N/A struct di_devlink *vlp);
2N/A
2N/Astatic void walk_cache_minor(di_devlink_handle_t hdp, const char *mpath,
2N/A link_desc_t *linkp);
2N/Astatic int walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp,
2N/A link_desc_t *linkp);
2N/Astatic void walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp);
2N/Astatic int cache_dev_link(struct di_devlink_handle *hdp, void *data,
2N/A const char *link_path);
2N/A
2N/Astatic int walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp);
2N/Astatic int recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp);
2N/Astatic int do_recurse(const char *dir, struct di_devlink_handle *hdp,
2N/A recurse_t *rp, int *retp);
2N/A
2N/Astatic int check_attr(uint32_t attr);
2N/Astatic int attr2type(uint32_t attr);
2N/A
2N/Astatic int check_args(link_desc_t *linkp);
2N/A
2N/Astatic void *get_last_node(struct di_devlink_handle *hdp, const char *path,
2N/A int flags);
2N/Astatic void *get_last_minor(struct di_devlink_handle *hdp,
2N/A const char *devfs_path, const char *minor_name, int flags);
2N/Astatic void set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp,
2N/A int flags);
2N/A
2N/Astatic int enter_db_lock(struct di_devlink_handle *hdp, const char *root_dir);
2N/Astatic void exit_db_lock(struct di_devlink_handle *hdp);
2N/A
2N/Astatic char *minor_colon(const char *path);
2N/Astatic const char *rel_path(struct di_devlink_handle *hdp, const char *path);
2N/Astatic int link_flag(uint_t flags);
2N/Astatic int s_readlink(const char *link, char *buf, size_t blen);
2N/Astatic cache_minor_t *link2minor(struct di_devlink_handle *hdp,
2N/A cache_link_t *clp);
2N/Astatic int link_cmp(cache_link_t *clp, const char *content, int type);
2N/Astatic void delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp);
2N/Astatic void delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp);
2N/Astatic int synchronize_db(di_devlink_handle_t hdp);
2N/Astatic void dprintf(debug_level_t msglevel, const char *fmt, ...);
2N/Astatic di_devlink_handle_t devlink_snapshot(const char *root_dir);
2N/Astatic int devlink_create(const char *root, const char *name, int dca_flags);
2N/Astatic int dca_init(const char *name, struct dca_off *dcp, int dca_flags);
2N/Astatic void exec_devfsadm(const char *root, struct dca_off *dcp);
2N/Astatic int do_exec(const char *path, char *const argv[]);
2N/Astatic int daemon_call(const char *root, struct dca_off *dcp);
2N/A
2N/Aint is_minor_node(const char *contents, const char **mn_root);
2N/Achar *s_realpath(const char *path, char *resolved_path);
2N/A
2N/A#ifdef __cplusplus
2N/A}
2N/A#endif
2N/A
2N/A#endif /* _DEVINFO_DEVLINK_H */