Lines Matching defs:hdp

132 	struct di_devlink_handle *hdp;
140 if ((hdp = handle_alloc(root_dir, OPEN_RDWR)) == NULL) {
144 err = open_db(hdp, OPEN_RDONLY);
154 get_db_path(hdp, DB_FILE, path, sizeof (path));
160 handle_free(&hdp); /* also closes the DB */
165 if (cache_alloc(hdp) != 0) {
166 handle_free(&hdp);
178 (void) di_devlink_close(&hdp, 0);
187 return (hdp);
191 CACHE(hdp)->update_count = DB_HDR(hdp)->update_count;
192 (void) read_nodes(hdp, NULL, DB_HDR(hdp)->root_idx);
193 (void) read_links(hdp, NULL, DB_HDR(hdp)->dngl_idx);
195 (void) close_db(hdp);
197 return (hdp);
202 struct di_devlink_handle *hdp,
216 dir = hdp->db_dir;
223 open_db(struct di_devlink_handle *hdp, int flags)
233 assert(!DB_OPEN(hdp));
251 get_db_path(hdp, DB_FILE, path, sizeof (path));
254 get_db_path(hdp, DB_TMP, path, sizeof (path));
275 sz = size_db(hdp, page_sz, count);
291 DB(hdp)->hdr = (struct db_hdr *)cp;
292 DB(hdp)->db_fd = fd;
293 DB(hdp)->flags = flags;
296 rv = invalid_db(hdp, sz, page_sz);
298 rv = init_hdr(hdp, page_sz, count);
303 (void) close_db(hdp);
318 struct di_devlink_handle *hdp, proto = {0};
429 hdp = calloc(1, sizeof (struct di_devlink_handle));
430 if (hdp == NULL) {
434 *hdp = proto;
437 * The handle hdp now contains a pointer to local storage
443 if ((hdp->dev_dir = strdup(proto.dev_dir)) == NULL) {
444 free(hdp);
448 if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) {
449 free(hdp->dev_dir);
450 free(hdp);
454 return (hdp);
468 cache_alloc(struct di_devlink_handle *hdp)
472 assert(HDL_RDWR(hdp));
474 if (DB_OPEN(hdp)) {
475 hash_sz = DB_NUM(hdp, DB_LINK) / AVG_CHAIN_SIZE;
479 CACHE(hdp)->hash = calloc(hash_sz, sizeof (cache_link_t *));
480 if (CACHE(hdp)->hash == NULL) {
483 CACHE(hdp)->hash_sz = hash_sz;
490 invalid_db(struct di_devlink_handle *hdp, size_t fsize, long page_sz)
496 if (DB_HDR(hdp)->magic != DB_MAGIC || DB_HDR(hdp)->vers != DB_VERSION) {
500 if (DB_HDR(hdp)->page_sz == 0 || DB_HDR(hdp)->page_sz != page_sz) {
504 sz = seg_size(hdp, DB_HEADER);
506 (void) dprintf(DBG_INFO, "N[%u] = %u\n", i, DB_NUM(hdp, i));
508 if (DB_NUM(hdp, i) < 1) {
511 sz += seg_size(hdp, i);
519 if (!VALID_INDEX(hdp, DB_NODE, DB_HDR(hdp)->root_idx)) {
523 if (!VALID_INDEX(hdp, DB_LINK, DB_HDR(hdp)->dngl_idx)) {
527 if (DB_EMPTY(hdp)) {
534 cp = get_string(hdp, DB_NUM(hdp, DB_STR) - 1);
543 read_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
550 assert(HDL_RDWR(hdp));
555 if ((pcnp == NULL) ^ (nidx == DB_HDR(hdp)->root_idx)) {
558 SET_DB_ERR(hdp);
562 for (; dnp = get_node(hdp, nidx); nidx = dnp->sib) {
564 path = get_string(hdp, dnp->path);
569 cnp = node_insert(hdp, pcnp, path, INSERT_HEAD);
571 SET_DB_ERR(hdp);
575 assert(strcmp(path, "/") ^ (nidx == DB_HDR(hdp)->root_idx));
578 if (read_minors(hdp, cnp, dnp->minor) != 0 ||
579 read_nodes(hdp, cnp, dnp->child) != 0) {
591 read_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
598 assert(HDL_RDWR(hdp));
603 SET_DB_ERR(hdp);
607 for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
609 name = get_string(hdp, dmp->name);
610 nodetype = get_string(hdp, dmp->nodetype);
612 cmnp = minor_insert(hdp, pcnp, name, nodetype, NULL);
614 SET_DB_ERR(hdp);
621 if (read_links(hdp, cmnp, dmp->link) != 0) {
633 read_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp, uint32_t nidx)
639 assert(HDL_RDWR(hdp));
642 ((pcmp == NULL) ^ (nidx == DB_HDR(hdp)->dngl_idx))) {
645 SET_DB_ERR(hdp);
649 for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
651 path = get_string(hdp, dlp->path);
652 content = get_string(hdp, dlp->content);
654 clp = link_insert(hdp, pcmp, path, content, dlp->attr);
656 SET_DB_ERR(hdp);
674 struct di_devlink_handle *hdp;
681 hdp = *pp;
689 handle_free(&hdp);
693 if (DB_ERR(hdp)) {
694 handle_free(&hdp);
702 get_db_path(hdp, DB_FILE, file, sizeof (file));
703 get_db_path(hdp, DB_TMP, tmp, sizeof (tmp));
709 CACHE(hdp)->update_count);
717 if (CACHE(hdp)->update_count == 0) {
718 CACHE(hdp)->update_count = 1;
721 (void) synchronize_db(hdp);
728 resolve_dangling_links(hdp);
734 if (CACHE_EMPTY(hdp)) {
737 handle_free(&hdp);
741 if (open_db(hdp, OPEN_RDWR) != 0) {
742 handle_free(&hdp);
754 (void) write_nodes(hdp, NULL, CACHE_ROOT(hdp), next);
755 (void) write_links(hdp, NULL, CACHE(hdp)->dngl, next);
756 DB_HDR(hdp)->update_count = CACHE(hdp)->update_count;
758 rv = close_db(hdp);
760 if (rv != 0 || DB_ERR(hdp) || rename(tmp, file) != 0) {
765 handle_free(&hdp);
769 handle_free(&hdp);
780 init_hdr(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
784 DB_HDR(hdp)->magic = DB_MAGIC;
785 DB_HDR(hdp)->vers = DB_VERSION;
786 DB_HDR(hdp)->root_idx = DB_NIL;
787 DB_HDR(hdp)->dngl_idx = DB_NIL;
788 DB_HDR(hdp)->page_sz = (uint32_t)page_sz;
792 DB_NUM(hdp, i) = count[i];
800 struct di_devlink_handle *hdp,
809 assert(HDL_RDWR(hdp));
816 if ((pdnp == NULL) ^ (cnp == CACHE_ROOT(hdp))) {
819 SET_DB_ERR(hdp);
824 (cnp == CACHE_ROOT(hdp)));
827 if ((dnp = set_node(hdp, idx)) == NULL) {
828 SET_DB_ERR(hdp);
832 dnp->path = write_string(hdp, cnp->path, next);
834 SET_DB_ERR(hdp);
841 assert(DB_HDR(hdp)->root_idx == DB_NIL);
842 DB_HDR(hdp)->root_idx = idx;
851 if (write_minors(hdp, dnp, cnp->minor, next) != 0 ||
852 write_nodes(hdp, dnp, cnp->child, next) != 0) {
862 struct di_devlink_handle *hdp,
871 assert(HDL_RDWR(hdp));
876 SET_DB_ERR(hdp);
885 if ((dmp = set_minor(hdp, idx)) == NULL) {
886 SET_DB_ERR(hdp);
890 dmp->name = write_string(hdp, cmnp->name, next);
891 dmp->nodetype = write_string(hdp, cmnp->nodetype, next);
894 SET_DB_ERR(hdp);
907 if (write_links(hdp, dmp, cmnp->link, next) != 0) {
917 struct di_devlink_handle *hdp,
926 assert(HDL_RDWR(hdp));
929 if (clp != NULL && ((pdmp == NULL) ^ (clp == CACHE(hdp)->dngl))) {
931 SET_DB_ERR(hdp);
942 SET_DB_ERR(hdp);
947 if ((dlp = set_link(hdp, idx)) == NULL) {
948 SET_DB_ERR(hdp);
952 dlp->path = write_string(hdp, clp->path, next);
953 dlp->content = write_string(hdp, clp->content, next);
956 SET_DB_ERR(hdp);
969 dlp->sib = DB_HDR(hdp)->dngl_idx;
970 DB_HDR(hdp)->dngl_idx = idx;
982 write_string(struct di_devlink_handle *hdp, const char *str, uint32_t *next)
987 assert(HDL_RDWR(hdp));
995 if (!VALID_STR(hdp, idx, str)) {
1001 if ((dstr = set_string(hdp, idx)) == NULL) {
1013 close_db(struct di_devlink_handle *hdp)
1018 if (!DB_OPEN(hdp)) {
1020 assert(DB(hdp)->db_fd == -1);
1021 assert(DB(hdp)->flags == 0);
1023 assert(DB_SEG(hdp, i) == NULL);
1024 assert(DB_SEG_PROT(hdp, i) == 0);
1032 if (DB_SEG(hdp, i)) {
1033 sz = seg_size(hdp, i);
1034 if (DB_RDWR(hdp))
1035 rv += msync(DB_SEG(hdp, i), sz, MS_SYNC);
1036 (void) munmap(DB_SEG(hdp, i), sz);
1037 DB_SEG(hdp, i) = NULL;
1038 DB_SEG_PROT(hdp, i) = 0;
1042 if (DB_RDWR(hdp))
1043 rv += msync((caddr_t)DB_HDR(hdp), HDR_LEN, MS_SYNC);
1044 (void) munmap((caddr_t)DB_HDR(hdp), HDR_LEN);
1045 DB(hdp)->hdr = NULL;
1047 (void) close(DB(hdp)->db_fd);
1048 DB(hdp)->db_fd = -1;
1049 DB(hdp)->flags = 0;
1056 cache_free(struct di_devlink_handle *hdp)
1060 subtree_free(hdp, &(CACHE_ROOT(hdp)));
1061 assert(CACHE_LAST(hdp) == NULL);
1067 while (CACHE(hdp)->dngl != NULL) {
1068 clp = CACHE(hdp)->dngl;
1069 CACHE(hdp)->dngl = clp->sib;
1074 assert((CACHE(hdp)->hash == NULL) ^ (CACHE(hdp)->hash_sz != 0));
1076 free(CACHE(hdp)->hash);
1077 CACHE(hdp)->hash = NULL;
1078 CACHE(hdp)->hash_sz = 0;
1084 struct di_devlink_handle *hdp = *pp;
1088 if (hdp == NULL)
1091 (void) close_db(hdp);
1092 cache_free(hdp);
1094 if (HDL_RDWR(hdp))
1095 exit_db_lock(hdp);
1096 assert(hdp->lock_fd == -1);
1098 free(hdp->dev_dir);
1099 free(hdp->db_dir);
1100 free(hdp);
1108 subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp)
1120 subtree_free(hdp, &np);
1130 rm_link_from_hash(hdp, clp);
1133 minor_free(hdp, &cmnp);
1140 rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp)
1151 hval = hashfn(hdp, clp->path);
1152 pp = &(CACHE_HASH(hdp, hval));
1165 link_hash(di_devlink_handle_t hdp, const char *link, uint_t flags)
1173 hval = hashfn(hdp, link);
1174 pp = &(CACHE_HASH(hdp, hval));
1193 link2minor(struct di_devlink_handle *hdp, cache_link_t *clp)
1209 return (lookup_minor(hdp, minor_path, NULL,
1240 if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) {
1246 (void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path);
1284 return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG));
1289 resolve_dangling_links(struct di_devlink_handle *hdp)
1294 for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) {
1296 if ((cmnp = link2minor(hdp, clp)) != NULL) {
1329 minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp)
1338 if (CACHE_LAST(hdp) == cmnp) {
1340 CACHE_LAST(hdp) = NULL;
1380 struct di_devlink_handle *hdp,
1405 if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) {
1409 if ((vp = lookup_node(hdp, pdup, flags)) == NULL) {
1425 *pp = minor_insert(hdp, vp, colon + 1, nodetype, pp);
1427 set_last_minor(hdp, *pp, flags);
1436 for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
1437 cp = get_string(hdp, dmp->name);
1446 lookup_node(struct di_devlink_handle *hdp, char *path, const int flags)
1450 if (tnd.node = get_last_node(hdp, path, flags))
1453 tnd.handle = hdp;
1466 get_last_node(struct di_devlink_handle *hdp, const char *path, int flags)
1478 if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL ||
1479 CACHE_LAST(hdp)->node == NULL) {
1483 cnp = CACHE_LAST(hdp)->node;
1498 struct di_devlink_handle *hdp,
1513 if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) {
1517 cmnp = CACHE_LAST(hdp);
1526 set_last_minor(hdp, cmnp, TYPE_CACHE);
1534 set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags)
1545 CACHE_LAST(hdp) = cmnp;
1646 minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp)
1673 delete_unused_nodes(hdp, cmnp->node);
1682 *lpp = CACHE(hdp)->dngl;
1683 CACHE(hdp)->dngl = cmnp->link;
1686 minor_free(hdp, &cmnp);
1690 delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp)
1718 } else if (cnp == CACHE_ROOT(hdp)) {
1719 CACHE_ROOT(hdp) = NULL;
1725 delete_unused_nodes(hdp, cnp->parent);
1733 rm_link(di_devlink_handle_t hdp, const char *link)
1738 if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' ||
1739 (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
1748 if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) {
1752 link_delete(hdp, clp);
1758 di_devlink_rm_link(di_devlink_handle_t hdp, const char *link)
1760 if (hdp == NULL || !HDL_RDWR(hdp)) {
1765 return (rm_link(hdp, link));
1769 link_delete(di_devlink_handle_t hdp, cache_link_t *clp)
1777 pp = &(CACHE(hdp)->dngl);
1793 delete_unused_minor(hdp, clp->minor);
1801 delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp)
1812 minor_delete(hdp, cmnp);
1817 di_devlink_handle_t hdp,
1822 return (add_link(hdp, link, content, flags) != NULL ? 0 : -1);
1827 struct di_devlink_handle *hdp,
1837 if (hdp == NULL || DB_ERR(hdp) || link == NULL ||
1839 (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
1846 if ((clp = link_hash(hdp, link, 0)) != NULL) {
1848 (void) rm_link(hdp, link);
1863 if ((cmnp = lookup_minor(hdp, minor_path, NULL,
1878 return (link_insert(hdp, cmnp, link, content, attr));
1897 di_devlink_update(di_devlink_handle_t hdp)
1899 if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) {
1908 CACHE(hdp)->update_count = 0;
1914 synchronize_db(di_devlink_handle_t hdp)
1930 if (recurse_dev(hdp, &rec) != 0) {
1934 for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) {
1935 for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) {
1951 (void) di_devlink_rm_link(hdp, pdup);
1997 struct di_devlink_handle *hdp;
2001 if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) {
2013 again: err = open_db(hdp, OPEN_RDONLY);
2026 return (hdp);
2045 di_devlink_handle_t hdp,
2056 if (hdp == NULL || !HDL_RDONLY(hdp)) {
2078 if (DB_OPEN(hdp)) {
2079 rv = walk_db(hdp, &linkd);
2081 rv = walk_dev(hdp, &linkd);
2137 walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp)
2139 assert(DB_OPEN(hdp));
2142 return (walk_all_links(hdp, linkp));
2144 return (walk_matching_links(hdp, linkp));
2149 cache_dev(struct di_devlink_handle *hdp)
2154 assert(hdp);
2155 assert(HDL_RDONLY(hdp));
2157 if (hdp == NULL || !HDL_RDONLY(hdp)) {
2164 CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *));
2165 if (CACHE(hdp)->hash == NULL) {
2168 CACHE(hdp)->hash_sz = sz;
2173 return (recurse_dev(hdp, &rec));
2177 walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp)
2179 assert(hdp && linkp);
2180 assert(!DB_OPEN(hdp));
2181 assert(HDL_RDONLY(hdp));
2183 if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) {
2188 if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) {
2194 walk_cache_minor(hdp, linkp->minor_path, linkp);
2196 walk_all_cache(hdp, linkp);
2203 cache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link)
2209 assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
2221 assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0);
2226 link += strlen(hdp->dev_dir) + 1;
2228 if ((clp = add_link(hdp, link, content, flags)) != NULL) {
2237 walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
2242 assert(DB_NUM(hdp, DB_LINK) >= 1);
2244 eidx = DB_NUM(hdp, DB_LINK);
2255 if ((dlp = get_link(hdp, nidx)) == NULL)
2258 vlink.rel_path = get_string(hdp, dlp->path);
2259 vlink.content = get_string(hdp, dlp->content);
2262 if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) {
2271 walk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
2279 dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB);
2287 nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx;
2288 for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
2291 vlink.rel_path = get_string(hdp, dlp->path);
2292 vlink.content = get_string(hdp, dlp->content);
2295 if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE)
2311 struct di_devlink_handle *hdp,
2329 vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path);
2342 hdp->dev_dir, vlp->rel_path);
2503 rel_path(struct di_devlink_handle *hdp, const char *path)
2505 const size_t len = strlen(hdp->dev_dir);
2507 if (strncmp(path, hdp->dev_dir, len) != 0)
2520 recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp)
2524 (void) do_recurse(hdp->dev_dir, hdp, rp, &ret);
2532 struct di_devlink_handle *hdp,
2545 if ((rel = rel_path(hdp, dir)) == NULL)
2581 rel = rel_path(hdp, cur);
2591 rv = do_recurse(cur, hdp, rp, retp);
2593 rv = rp->fcn(hdp, rp->data, cur);
2648 struct di_devlink_handle *hdp,
2657 SET_DB_ERR(hdp);
2662 SET_DB_ERR(hdp);
2667 SET_DB_ERR(hdp);
2676 assert(CACHE(hdp)->root == NULL);
2677 CACHE(hdp)->root = cnp;
2681 } else if (CACHE_LAST(hdp) && CACHE_LAST(hdp)->node &&
2682 CACHE_LAST(hdp)->node->parent == pcnp &&
2683 CACHE_LAST(hdp)->node->sib == NULL) {
2685 CACHE_LAST(hdp)->node->sib = cnp;
2704 struct di_devlink_handle *hdp,
2714 SET_DB_ERR(hdp);
2726 SET_DB_ERR(hdp);
2733 SET_DB_ERR(hdp);
2756 struct di_devlink_handle *hdp,
2766 SET_DB_ERR(hdp);
2771 SET_DB_ERR(hdp);
2778 SET_DB_ERR(hdp);
2784 hash_insert(hdp, clp);
2792 clp->sib = CACHE(hdp)->dngl;
2793 CACHE(hdp)->dngl = clp;
2800 hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp)
2804 hval = hashfn(hdp, clp->path);
2805 clp->hash = CACHE_HASH(hdp, hval);
2806 CACHE_HASH(hdp, hval) = clp;
2811 get_node(struct di_devlink_handle *hdp, uint32_t idx)
2813 return (map_seg(hdp, idx, PROT_READ, DB_NODE));
2817 set_node(struct di_devlink_handle *hdp, uint32_t idx)
2819 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_NODE));
2823 get_minor(struct di_devlink_handle *hdp, uint32_t idx)
2825 return (map_seg(hdp, idx, PROT_READ, DB_MINOR));
2829 set_minor(struct di_devlink_handle *hdp, uint32_t idx)
2831 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_MINOR));
2835 get_link(struct di_devlink_handle *hdp, uint32_t idx)
2837 return (map_seg(hdp, idx, PROT_READ, DB_LINK));
2841 set_link(struct di_devlink_handle *hdp, uint32_t idx)
2843 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_LINK));
2847 get_string(struct di_devlink_handle *hdp, uint32_t idx)
2849 return (map_seg(hdp, idx, PROT_READ, DB_STR));
2853 set_string(struct di_devlink_handle *hdp, uint32_t idx)
2855 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_STR));
2865 struct di_devlink_handle *hdp,
2879 if (!VALID_INDEX(hdp, seg, idx)) {
2889 if (DB_SEG(hdp, seg) != NULL) {
2890 if (DB_SEG_PROT(hdp, seg) != prot) {
2893 seg, idx, DB_SEG_PROT(hdp, seg), prot);
2896 return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
2902 off = seg_size(hdp, DB_HEADER);
2904 off += seg_size(hdp, s);
2906 slen = seg_size(hdp, seg);
2908 addr = mmap(0, slen, prot, MAP_SHARED, DB(hdp)->db_fd, off);
2913 " fd=%d, off=%ld\n", (ulong_t)slen, prot, DB(hdp)->db_fd,
2918 DB_SEG(hdp, seg) = addr;
2919 DB_SEG_PROT(hdp, seg) = prot;
2922 "off=%ld, seg_base=%p\n", seg, (ulong_t)slen, prot, DB(hdp)->db_fd,
2925 return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
2932 seg_size(struct di_devlink_handle *hdp, int seg)
2936 assert(DB_HDR(hdp)->page_sz);
2941 assert(DB_NUM(hdp, seg) >= 1);
2942 sz = DB_NUM(hdp, seg) * elem_sizes[seg];
2945 sz = (sz / DB_HDR(hdp)->page_sz) + 1;
2947 sz *= DB_HDR(hdp)->page_sz;
2953 size_db(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
2966 count_node(CACHE(hdp)->root, count);
2968 for (clp = CACHE(hdp)->dngl; clp != NULL; clp = clp->sib) {
3046 hashfn(struct di_devlink_handle *hdp, const char *str)
3055 assert(CACHE(hdp)->hash_sz >= MIN_HASH_SIZE);
3061 return (hval % CACHE(hdp)->hash_sz);
3078 enter_db_lock(struct di_devlink_handle *hdp, const char *root_dir)
3084 int writer = HDL_RDWR(hdp);
3088 assert(hdp->lock_fd < 0);
3090 get_db_path(hdp, DB_LOCK, lockfile, sizeof (lockfile));
3147 hdp->lock_fd = fd;
3164 exit_db_lock(struct di_devlink_handle *hdp)
3167 int writer = HDL_RDWR(hdp);
3169 if (hdp->lock_fd < 0) {
3180 if (fcntl(hdp->lock_fd, F_SETLK, &unlock) == -1) {
3185 (void) close(hdp->lock_fd);
3187 hdp->lock_fd = -1;
3552 walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp, link_desc_t *linkp)
3556 assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
3577 if (visit_link(hdp, linkp, &vlink)
3585 clp = CACHE(hdp)->dngl;
3595 walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp)
3602 for (i = 0; i < CACHE(hdp)->hash_sz; i++) {
3603 clp = CACHE_HASH(hdp, i);
3612 if (visit_link(hdp, linkp, &vlink) !=
3623 walk_cache_minor(di_devlink_handle_t hdp, const char *mpath, link_desc_t *linkp)
3629 if ((cmnp = lookup_minor(hdp, mpath, NULL, TYPE_CACHE)) != NULL) {
3630 (void) walk_cache_links(hdp, cmnp->link, linkp);
3637 walk_cache_node(di_devlink_handle_t hdp, const char *path, link_desc_t *linkp)
3644 if ((cnp = lookup_node(hdp, (char *)path, TYPE_CACHE)) == NULL) {
3650 if (walk_cache_links(hdp, cmnp->link, linkp)
3676 di_devlink_cache_walk(di_devlink_handle_t hdp,
3686 if (hdp == NULL || path == NULL || !link_flag(flags) ||
3687 !HDL_RDWR(hdp) || devlink_callback == NULL) {
3703 walk_cache_node(hdp, path, &linkd);
3705 walk_cache_minor(hdp, path, &linkd);