Lines Matching refs:hdp

128 	struct di_devlink_handle *hdp;
136 if ((hdp = handle_alloc(root_dir, OPEN_RDWR)) == NULL) {
140 err = open_db(hdp, OPEN_RDONLY);
150 get_db_path(hdp, DB_FILE, path, sizeof (path));
156 handle_free(&hdp); /* also closes the DB */
161 if (cache_alloc(hdp) != 0) {
162 handle_free(&hdp);
174 (void) di_devlink_close(&hdp, 0);
183 return (hdp);
187 CACHE(hdp)->update_count = DB_HDR(hdp)->update_count;
188 (void) read_nodes(hdp, NULL, DB_HDR(hdp)->root_idx);
189 (void) read_links(hdp, NULL, DB_HDR(hdp)->dngl_idx);
191 (void) close_db(hdp);
193 return (hdp);
198 struct di_devlink_handle *hdp,
212 dir = hdp->db_dir;
219 open_db(struct di_devlink_handle *hdp, int flags)
229 assert(!DB_OPEN(hdp));
247 get_db_path(hdp, DB_FILE, path, sizeof (path));
250 get_db_path(hdp, DB_TMP, path, sizeof (path));
271 sz = size_db(hdp, page_sz, count);
287 DB(hdp)->hdr = (struct db_hdr *)cp;
288 DB(hdp)->db_fd = fd;
289 DB(hdp)->flags = flags;
292 rv = invalid_db(hdp, sz, page_sz);
294 rv = init_hdr(hdp, page_sz, count);
299 (void) close_db(hdp);
314 struct di_devlink_handle *hdp, proto = {0};
425 hdp = calloc(1, sizeof (struct di_devlink_handle));
426 if (hdp == NULL) {
430 *hdp = proto;
433 * The handle hdp now contains a pointer to local storage
439 if ((hdp->dev_dir = strdup(proto.dev_dir)) == NULL) {
440 free(hdp);
444 if ((hdp->db_dir = strdup(proto.db_dir)) == NULL) {
445 free(hdp->dev_dir);
446 free(hdp);
450 return (hdp);
464 cache_alloc(struct di_devlink_handle *hdp)
468 assert(HDL_RDWR(hdp));
470 if (DB_OPEN(hdp)) {
471 hash_sz = DB_NUM(hdp, DB_LINK) / AVG_CHAIN_SIZE;
475 CACHE(hdp)->hash = calloc(hash_sz, sizeof (cache_link_t *));
476 if (CACHE(hdp)->hash == NULL) {
479 CACHE(hdp)->hash_sz = hash_sz;
486 invalid_db(struct di_devlink_handle *hdp, size_t fsize, long page_sz)
492 if (DB_HDR(hdp)->magic != DB_MAGIC || DB_HDR(hdp)->vers != DB_VERSION) {
496 if (DB_HDR(hdp)->page_sz == 0 || DB_HDR(hdp)->page_sz != page_sz) {
500 sz = seg_size(hdp, DB_HEADER);
502 (void) dprintf(DBG_INFO, "N[%u] = %u\n", i, DB_NUM(hdp, i));
504 if (DB_NUM(hdp, i) < 1) {
507 sz += seg_size(hdp, i);
515 if (!VALID_INDEX(hdp, DB_NODE, DB_HDR(hdp)->root_idx)) {
519 if (!VALID_INDEX(hdp, DB_LINK, DB_HDR(hdp)->dngl_idx)) {
523 if (DB_EMPTY(hdp)) {
530 cp = get_string(hdp, DB_NUM(hdp, DB_STR) - 1);
539 read_nodes(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
546 assert(HDL_RDWR(hdp));
551 if ((pcnp == NULL) ^ (nidx == DB_HDR(hdp)->root_idx)) {
554 SET_DB_ERR(hdp);
558 for (; dnp = get_node(hdp, nidx); nidx = dnp->sib) {
560 path = get_string(hdp, dnp->path);
565 cnp = node_insert(hdp, pcnp, path, INSERT_HEAD);
567 SET_DB_ERR(hdp);
571 assert(strcmp(path, "/") ^ (nidx == DB_HDR(hdp)->root_idx));
574 if (read_minors(hdp, cnp, dnp->minor) != 0 ||
575 read_nodes(hdp, cnp, dnp->child) != 0) {
587 read_minors(struct di_devlink_handle *hdp, cache_node_t *pcnp, uint32_t nidx)
594 assert(HDL_RDWR(hdp));
599 SET_DB_ERR(hdp);
603 for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
605 name = get_string(hdp, dmp->name);
606 nodetype = get_string(hdp, dmp->nodetype);
608 cmnp = minor_insert(hdp, pcnp, name, nodetype, NULL);
610 SET_DB_ERR(hdp);
617 if (read_links(hdp, cmnp, dmp->link) != 0) {
629 read_links(struct di_devlink_handle *hdp, cache_minor_t *pcmp, uint32_t nidx)
635 assert(HDL_RDWR(hdp));
638 ((pcmp == NULL) ^ (nidx == DB_HDR(hdp)->dngl_idx))) {
641 SET_DB_ERR(hdp);
645 for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
647 path = get_string(hdp, dlp->path);
648 content = get_string(hdp, dlp->content);
650 clp = link_insert(hdp, pcmp, path, content, dlp->attr);
652 SET_DB_ERR(hdp);
670 struct di_devlink_handle *hdp;
677 hdp = *pp;
685 handle_free(&hdp);
689 if (DB_ERR(hdp)) {
690 handle_free(&hdp);
698 get_db_path(hdp, DB_FILE, file, sizeof (file));
699 get_db_path(hdp, DB_TMP, tmp, sizeof (tmp));
705 CACHE(hdp)->update_count);
713 if (CACHE(hdp)->update_count == 0) {
714 CACHE(hdp)->update_count = 1;
717 (void) synchronize_db(hdp);
724 resolve_dangling_links(hdp);
730 if (CACHE_EMPTY(hdp)) {
733 handle_free(&hdp);
737 if (open_db(hdp, OPEN_RDWR) != 0) {
738 handle_free(&hdp);
750 (void) write_nodes(hdp, NULL, CACHE_ROOT(hdp), next);
751 (void) write_links(hdp, NULL, CACHE(hdp)->dngl, next);
752 DB_HDR(hdp)->update_count = CACHE(hdp)->update_count;
754 rv = close_db(hdp);
756 if (rv != 0 || DB_ERR(hdp) || rename(tmp, file) != 0) {
761 handle_free(&hdp);
765 handle_free(&hdp);
776 init_hdr(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
780 DB_HDR(hdp)->magic = DB_MAGIC;
781 DB_HDR(hdp)->vers = DB_VERSION;
782 DB_HDR(hdp)->root_idx = DB_NIL;
783 DB_HDR(hdp)->dngl_idx = DB_NIL;
784 DB_HDR(hdp)->page_sz = (uint32_t)page_sz;
788 DB_NUM(hdp, i) = count[i];
796 struct di_devlink_handle *hdp,
805 assert(HDL_RDWR(hdp));
812 if ((pdnp == NULL) ^ (cnp == CACHE_ROOT(hdp))) {
815 SET_DB_ERR(hdp);
820 (cnp == CACHE_ROOT(hdp)));
823 if ((dnp = set_node(hdp, idx)) == NULL) {
824 SET_DB_ERR(hdp);
828 dnp->path = write_string(hdp, cnp->path, next);
830 SET_DB_ERR(hdp);
837 assert(DB_HDR(hdp)->root_idx == DB_NIL);
838 DB_HDR(hdp)->root_idx = idx;
847 if (write_minors(hdp, dnp, cnp->minor, next) != 0 ||
848 write_nodes(hdp, dnp, cnp->child, next) != 0) {
858 struct di_devlink_handle *hdp,
867 assert(HDL_RDWR(hdp));
872 SET_DB_ERR(hdp);
881 if ((dmp = set_minor(hdp, idx)) == NULL) {
882 SET_DB_ERR(hdp);
886 dmp->name = write_string(hdp, cmnp->name, next);
887 dmp->nodetype = write_string(hdp, cmnp->nodetype, next);
890 SET_DB_ERR(hdp);
903 if (write_links(hdp, dmp, cmnp->link, next) != 0) {
913 struct di_devlink_handle *hdp,
922 assert(HDL_RDWR(hdp));
925 if (clp != NULL && ((pdmp == NULL) ^ (clp == CACHE(hdp)->dngl))) {
927 SET_DB_ERR(hdp);
938 SET_DB_ERR(hdp);
943 if ((dlp = set_link(hdp, idx)) == NULL) {
944 SET_DB_ERR(hdp);
948 dlp->path = write_string(hdp, clp->path, next);
949 dlp->content = write_string(hdp, clp->content, next);
952 SET_DB_ERR(hdp);
965 dlp->sib = DB_HDR(hdp)->dngl_idx;
966 DB_HDR(hdp)->dngl_idx = idx;
978 write_string(struct di_devlink_handle *hdp, const char *str, uint32_t *next)
983 assert(HDL_RDWR(hdp));
991 if (!VALID_STR(hdp, idx, str)) {
997 if ((dstr = set_string(hdp, idx)) == NULL) {
1009 close_db(struct di_devlink_handle *hdp)
1014 if (!DB_OPEN(hdp)) {
1016 assert(DB(hdp)->db_fd == -1);
1017 assert(DB(hdp)->flags == 0);
1019 assert(DB_SEG(hdp, i) == NULL);
1020 assert(DB_SEG_PROT(hdp, i) == 0);
1028 if (DB_SEG(hdp, i)) {
1029 sz = seg_size(hdp, i);
1030 if (DB_RDWR(hdp))
1031 rv += msync(DB_SEG(hdp, i), sz, MS_SYNC);
1032 (void) munmap(DB_SEG(hdp, i), sz);
1033 DB_SEG(hdp, i) = NULL;
1034 DB_SEG_PROT(hdp, i) = 0;
1038 if (DB_RDWR(hdp))
1039 rv += msync((caddr_t)DB_HDR(hdp), HDR_LEN, MS_SYNC);
1040 (void) munmap((caddr_t)DB_HDR(hdp), HDR_LEN);
1041 DB(hdp)->hdr = NULL;
1043 (void) close(DB(hdp)->db_fd);
1044 DB(hdp)->db_fd = -1;
1045 DB(hdp)->flags = 0;
1052 cache_free(struct di_devlink_handle *hdp)
1056 subtree_free(hdp, &(CACHE_ROOT(hdp)));
1057 assert(CACHE_LAST(hdp) == NULL);
1063 while (CACHE(hdp)->dngl != NULL) {
1064 clp = CACHE(hdp)->dngl;
1065 CACHE(hdp)->dngl = clp->sib;
1070 assert((CACHE(hdp)->hash == NULL) ^ (CACHE(hdp)->hash_sz != 0));
1072 free(CACHE(hdp)->hash);
1073 CACHE(hdp)->hash = NULL;
1074 CACHE(hdp)->hash_sz = 0;
1080 struct di_devlink_handle *hdp = *pp;
1084 if (hdp == NULL)
1087 (void) close_db(hdp);
1088 cache_free(hdp);
1090 if (HDL_RDWR(hdp))
1091 exit_db_lock(hdp);
1092 assert(hdp->lock_fd == -1);
1094 free(hdp->dev_dir);
1095 free(hdp->db_dir);
1096 free(hdp);
1104 subtree_free(struct di_devlink_handle *hdp, cache_node_t **pp)
1116 subtree_free(hdp, &np);
1126 rm_link_from_hash(hdp, clp);
1129 minor_free(hdp, &cmnp);
1136 rm_link_from_hash(struct di_devlink_handle *hdp, cache_link_t *clp)
1147 hval = hashfn(hdp, clp->path);
1148 pp = &(CACHE_HASH(hdp, hval));
1161 link_hash(di_devlink_handle_t hdp, const char *link, uint_t flags)
1169 hval = hashfn(hdp, link);
1170 pp = &(CACHE_HASH(hdp, hval));
1189 link2minor(struct di_devlink_handle *hdp, cache_link_t *clp)
1205 return (lookup_minor(hdp, minor_path, NULL,
1236 if ((plp = link_hash(hdp, cp, 0)) != NULL && plp->minor != NULL) {
1242 (void) snprintf(link, sizeof (link), "%s/%s", hdp->dev_dir, clp->path);
1280 return (lookup_minor(hdp, minor_path, NULL, TYPE_CACHE|CREATE_FLAG));
1285 resolve_dangling_links(struct di_devlink_handle *hdp)
1290 for (pp = &(CACHE(hdp)->dngl); *pp != NULL; ) {
1292 if ((cmnp = link2minor(hdp, clp)) != NULL) {
1325 minor_free(struct di_devlink_handle *hdp, cache_minor_t **pp)
1334 if (CACHE_LAST(hdp) == cmnp) {
1336 CACHE_LAST(hdp) = NULL;
1376 struct di_devlink_handle *hdp,
1401 if ((vp = get_last_minor(hdp, pdup, colon + 1, flags)) != NULL) {
1405 if ((vp = lookup_node(hdp, pdup, flags)) == NULL) {
1421 *pp = minor_insert(hdp, vp, colon + 1, nodetype, pp);
1423 set_last_minor(hdp, *pp, flags);
1432 for (; dmp = get_minor(hdp, nidx); nidx = dmp->sib) {
1433 cp = get_string(hdp, dmp->name);
1442 lookup_node(struct di_devlink_handle *hdp, char *path, const int flags)
1446 if (tnd.node = get_last_node(hdp, path, flags))
1449 tnd.handle = hdp;
1462 get_last_node(struct di_devlink_handle *hdp, const char *path, int flags)
1474 if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL ||
1475 CACHE_LAST(hdp)->node == NULL) {
1479 cnp = CACHE_LAST(hdp)->node;
1494 struct di_devlink_handle *hdp,
1509 if (!LOOKUP_CACHE(flags) || CACHE_LAST(hdp) == NULL) {
1513 cmnp = CACHE_LAST(hdp);
1522 set_last_minor(hdp, cmnp, TYPE_CACHE);
1530 set_last_minor(struct di_devlink_handle *hdp, cache_minor_t *cmnp, int flags)
1541 CACHE_LAST(hdp) = cmnp;
1642 minor_delete(di_devlink_handle_t hdp, cache_minor_t *cmnp)
1669 delete_unused_nodes(hdp, cmnp->node);
1678 *lpp = CACHE(hdp)->dngl;
1679 CACHE(hdp)->dngl = cmnp->link;
1682 minor_free(hdp, &cmnp);
1686 delete_unused_nodes(di_devlink_handle_t hdp, cache_node_t *cnp)
1714 } else if (cnp == CACHE_ROOT(hdp)) {
1715 CACHE_ROOT(hdp) = NULL;
1721 delete_unused_nodes(hdp, cnp->parent);
1729 rm_link(di_devlink_handle_t hdp, const char *link)
1734 if (hdp == NULL || DB_ERR(hdp) || link == NULL || link[0] == '/' ||
1735 (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
1744 if ((clp = link_hash(hdp, link, UNLINK_FROM_HASH)) == NULL) {
1748 link_delete(hdp, clp);
1754 di_devlink_rm_link(di_devlink_handle_t hdp, const char *link)
1756 if (hdp == NULL || !HDL_RDWR(hdp)) {
1761 return (rm_link(hdp, link));
1765 link_delete(di_devlink_handle_t hdp, cache_link_t *clp)
1773 pp = &(CACHE(hdp)->dngl);
1789 delete_unused_minor(hdp, clp->minor);
1797 delete_unused_minor(di_devlink_handle_t hdp, cache_minor_t *cmnp)
1808 minor_delete(hdp, cmnp);
1813 di_devlink_handle_t hdp,
1818 return (add_link(hdp, link, content, flags) != NULL ? 0 : -1);
1823 struct di_devlink_handle *hdp,
1833 if (hdp == NULL || DB_ERR(hdp) || link == NULL ||
1835 (!HDL_RDWR(hdp) && !HDL_RDONLY(hdp))) {
1842 if ((clp = link_hash(hdp, link, 0)) != NULL) {
1844 (void) rm_link(hdp, link);
1859 if ((cmnp = lookup_minor(hdp, minor_path, NULL,
1874 return (link_insert(hdp, cmnp, link, content, attr));
1893 di_devlink_update(di_devlink_handle_t hdp)
1895 if (hdp == NULL || !HDL_RDWR(hdp) || DB_ERR(hdp)) {
1904 CACHE(hdp)->update_count = 0;
1910 synchronize_db(di_devlink_handle_t hdp)
1926 if (recurse_dev(hdp, &rec) != 0) {
1930 for (hval = 0; hval < CACHE(hdp)->hash_sz; hval++) {
1931 for (clp = CACHE_HASH(hdp, hval); clp != NULL; ) {
1947 (void) di_devlink_rm_link(hdp, pdup);
1993 struct di_devlink_handle *hdp;
1997 if ((hdp = handle_alloc(root_dir, OPEN_RDONLY)) == NULL) {
2009 again: err = open_db(hdp, OPEN_RDONLY);
2022 return (hdp);
2041 di_devlink_handle_t hdp,
2052 if (hdp == NULL || !HDL_RDONLY(hdp)) {
2074 if (DB_OPEN(hdp)) {
2075 rv = walk_db(hdp, &linkd);
2077 rv = walk_dev(hdp, &linkd);
2133 walk_db(struct di_devlink_handle *hdp, link_desc_t *linkp)
2135 assert(DB_OPEN(hdp));
2138 return (walk_all_links(hdp, linkp));
2140 return (walk_matching_links(hdp, linkp));
2145 cache_dev(struct di_devlink_handle *hdp)
2150 assert(hdp);
2151 assert(HDL_RDONLY(hdp));
2153 if (hdp == NULL || !HDL_RDONLY(hdp)) {
2160 CACHE(hdp)->hash = calloc(sz, sizeof (cache_link_t *));
2161 if (CACHE(hdp)->hash == NULL) {
2164 CACHE(hdp)->hash_sz = sz;
2169 return (recurse_dev(hdp, &rec));
2173 walk_dev(struct di_devlink_handle *hdp, link_desc_t *linkp)
2175 assert(hdp && linkp);
2176 assert(!DB_OPEN(hdp));
2177 assert(HDL_RDONLY(hdp));
2179 if (hdp == NULL || !HDL_RDONLY(hdp) || DB_OPEN(hdp)) {
2184 if (CACHE_EMPTY(hdp) && cache_dev(hdp) != 0) {
2190 walk_cache_minor(hdp, linkp->minor_path, linkp);
2192 walk_all_cache(hdp, linkp);
2199 cache_dev_link(struct di_devlink_handle *hdp, void *data, const char *link)
2205 assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
2217 assert(strncmp(link, hdp->dev_dir, strlen(hdp->dev_dir)) == 0);
2222 link += strlen(hdp->dev_dir) + 1;
2224 if ((clp = add_link(hdp, link, content, flags)) != NULL) {
2233 walk_all_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
2238 assert(DB_NUM(hdp, DB_LINK) >= 1);
2240 eidx = DB_NUM(hdp, DB_LINK);
2251 if ((dlp = get_link(hdp, nidx)) == NULL)
2254 vlink.rel_path = get_string(hdp, dlp->path);
2255 vlink.content = get_string(hdp, dlp->content);
2258 if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE) {
2267 walk_matching_links(struct di_devlink_handle *hdp, link_desc_t *linkp)
2275 dmp = lookup_minor(hdp, linkp->minor_path, NULL, TYPE_DB);
2283 nidx = dmp ? dmp->link : DB_HDR(hdp)->dngl_idx;
2284 for (; dlp = get_link(hdp, nidx); nidx = dlp->sib) {
2287 vlink.rel_path = get_string(hdp, dlp->path);
2288 vlink.content = get_string(hdp, dlp->content);
2291 if (visit_link(hdp, linkp, &vlink) != DI_WALK_CONTINUE)
2307 struct di_devlink_handle *hdp,
2325 vlp->rel_path = (char *)rel_path(hdp, vlp->abs_path);
2338 hdp->dev_dir, vlp->rel_path);
2499 rel_path(struct di_devlink_handle *hdp, const char *path)
2501 const size_t len = strlen(hdp->dev_dir);
2503 if (strncmp(path, hdp->dev_dir, len) != 0)
2516 recurse_dev(struct di_devlink_handle *hdp, recurse_t *rp)
2520 (void) do_recurse(hdp->dev_dir, hdp, rp, &ret);
2528 struct di_devlink_handle *hdp,
2541 if ((rel = rel_path(hdp, dir)) == NULL)
2577 rel = rel_path(hdp, cur);
2587 rv = do_recurse(cur, hdp, rp, retp);
2589 rv = rp->fcn(hdp, rp->data, cur);
2644 struct di_devlink_handle *hdp,
2653 SET_DB_ERR(hdp);
2658 SET_DB_ERR(hdp);
2663 SET_DB_ERR(hdp);
2672 assert(CACHE(hdp)->root == NULL);
2673 CACHE(hdp)->root = cnp;
2677 } else if (CACHE_LAST(hdp) && CACHE_LAST(hdp)->node &&
2678 CACHE_LAST(hdp)->node->parent == pcnp &&
2679 CACHE_LAST(hdp)->node->sib == NULL) {
2681 CACHE_LAST(hdp)->node->sib = cnp;
2700 struct di_devlink_handle *hdp,
2710 SET_DB_ERR(hdp);
2722 SET_DB_ERR(hdp);
2729 SET_DB_ERR(hdp);
2752 struct di_devlink_handle *hdp,
2762 SET_DB_ERR(hdp);
2767 SET_DB_ERR(hdp);
2774 SET_DB_ERR(hdp);
2780 hash_insert(hdp, clp);
2788 clp->sib = CACHE(hdp)->dngl;
2789 CACHE(hdp)->dngl = clp;
2796 hash_insert(struct di_devlink_handle *hdp, cache_link_t *clp)
2800 hval = hashfn(hdp, clp->path);
2801 clp->hash = CACHE_HASH(hdp, hval);
2802 CACHE_HASH(hdp, hval) = clp;
2807 get_node(struct di_devlink_handle *hdp, uint32_t idx)
2809 return (map_seg(hdp, idx, PROT_READ, DB_NODE));
2813 set_node(struct di_devlink_handle *hdp, uint32_t idx)
2815 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_NODE));
2819 get_minor(struct di_devlink_handle *hdp, uint32_t idx)
2821 return (map_seg(hdp, idx, PROT_READ, DB_MINOR));
2825 set_minor(struct di_devlink_handle *hdp, uint32_t idx)
2827 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_MINOR));
2831 get_link(struct di_devlink_handle *hdp, uint32_t idx)
2833 return (map_seg(hdp, idx, PROT_READ, DB_LINK));
2837 set_link(struct di_devlink_handle *hdp, uint32_t idx)
2839 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_LINK));
2843 get_string(struct di_devlink_handle *hdp, uint32_t idx)
2845 return (map_seg(hdp, idx, PROT_READ, DB_STR));
2849 set_string(struct di_devlink_handle *hdp, uint32_t idx)
2851 return (map_seg(hdp, idx, PROT_READ | PROT_WRITE, DB_STR));
2861 struct di_devlink_handle *hdp,
2875 if (!VALID_INDEX(hdp, seg, idx)) {
2885 if (DB_SEG(hdp, seg) != NULL) {
2886 if (DB_SEG_PROT(hdp, seg) != prot) {
2889 seg, idx, DB_SEG_PROT(hdp, seg), prot);
2892 return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
2898 off = seg_size(hdp, DB_HEADER);
2900 off += seg_size(hdp, s);
2902 slen = seg_size(hdp, seg);
2904 addr = mmap(0, slen, prot, MAP_SHARED, DB(hdp)->db_fd, off);
2909 " fd=%d, off=%ld\n", (ulong_t)slen, prot, DB(hdp)->db_fd,
2914 DB_SEG(hdp, seg) = addr;
2915 DB_SEG_PROT(hdp, seg) = prot;
2918 "off=%ld, seg_base=%p\n", seg, (ulong_t)slen, prot, DB(hdp)->db_fd,
2921 return (DB_SEG(hdp, seg) + idx * elem_sizes[seg]);
2928 seg_size(struct di_devlink_handle *hdp, int seg)
2932 assert(DB_HDR(hdp)->page_sz);
2937 assert(DB_NUM(hdp, seg) >= 1);
2938 sz = DB_NUM(hdp, seg) * elem_sizes[seg];
2941 sz = (sz / DB_HDR(hdp)->page_sz) + 1;
2943 sz *= DB_HDR(hdp)->page_sz;
2949 size_db(struct di_devlink_handle *hdp, long page_sz, uint32_t *count)
2962 count_node(CACHE(hdp)->root, count);
2964 for (clp = CACHE(hdp)->dngl; clp != NULL; clp = clp->sib) {
3042 hashfn(struct di_devlink_handle *hdp, const char *str)
3051 assert(CACHE(hdp)->hash_sz >= MIN_HASH_SIZE);
3057 return (hval % CACHE(hdp)->hash_sz);
3074 enter_db_lock(struct di_devlink_handle *hdp, const char *root_dir)
3080 int writer = HDL_RDWR(hdp);
3084 assert(hdp->lock_fd < 0);
3086 get_db_path(hdp, DB_LOCK, lockfile, sizeof (lockfile));
3143 hdp->lock_fd = fd;
3160 exit_db_lock(struct di_devlink_handle *hdp)
3163 int writer = HDL_RDWR(hdp);
3165 if (hdp->lock_fd < 0) {
3176 if (fcntl(hdp->lock_fd, F_SETLK, &unlock) == -1) {
3181 (void) close(hdp->lock_fd);
3183 hdp->lock_fd = -1;
3616 walk_cache_links(di_devlink_handle_t hdp, cache_link_t *clp, link_desc_t *linkp)
3620 assert(HDL_RDWR(hdp) || HDL_RDONLY(hdp));
3641 if (visit_link(hdp, linkp, &vlink)
3649 clp = CACHE(hdp)->dngl;
3659 walk_all_cache(di_devlink_handle_t hdp, link_desc_t *linkp)
3666 for (i = 0; i < CACHE(hdp)->hash_sz; i++) {
3667 clp = CACHE_HASH(hdp, i);
3676 if (visit_link(hdp, linkp, &vlink) !=
3687 walk_cache_minor(di_devlink_handle_t hdp, const char *mpath, link_desc_t *linkp)
3693 if ((cmnp = lookup_minor(hdp, mpath, NULL, TYPE_CACHE)) != NULL) {
3694 (void) walk_cache_links(hdp, cmnp->link, linkp);
3701 walk_cache_node(di_devlink_handle_t hdp, const char *path, link_desc_t *linkp)
3708 if ((cnp = lookup_node(hdp, (char *)path, TYPE_CACHE)) == NULL) {
3714 if (walk_cache_links(hdp, cmnp->link, linkp)
3740 di_devlink_cache_walk(di_devlink_handle_t hdp,
3750 if (hdp == NULL || path == NULL || !link_flag(flags) ||
3751 !HDL_RDWR(hdp) || devlink_callback == NULL) {
3767 walk_cache_node(hdp, path, &linkd);
3769 walk_cache_minor(hdp, path, &linkd);