1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/avl.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 31*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 32*7c478bd9Sstevel@tonic-gate #include "libcmdutils.h" 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate /* 35*7c478bd9Sstevel@tonic-gate * The following interfaces complement the interfaces available in 36*7c478bd9Sstevel@tonic-gate * libavl. 37*7c478bd9Sstevel@tonic-gate * tnode_compare() - tree node comparison routine 38*7c478bd9Sstevel@tonic-gate * add_tnode() - adds nodes to a tree 39*7c478bd9Sstevel@tonic-gate * destroy_tree() - destroys a whole tree 40*7c478bd9Sstevel@tonic-gate * 41*7c478bd9Sstevel@tonic-gate * The libavl routines are very generic and don't have any 42*7c478bd9Sstevel@tonic-gate * direct knowledge about the data being stored in the AVL tree, 43*7c478bd9Sstevel@tonic-gate * nor any of the details of the AVL tree representation. 44*7c478bd9Sstevel@tonic-gate * In addition, the libavl routines do not perform any locking 45*7c478bd9Sstevel@tonic-gate * or memory allocation. Appropriate synchronization and memory 46*7c478bd9Sstevel@tonic-gate * allocation are the responsibility of the user of the libavl 47*7c478bd9Sstevel@tonic-gate * routines. 48*7c478bd9Sstevel@tonic-gate * 49*7c478bd9Sstevel@tonic-gate * These routines, and the structures defined in "libcmdutils.h", 50*7c478bd9Sstevel@tonic-gate * provide the necessary details about the data and AVL tree 51*7c478bd9Sstevel@tonic-gate * representation. Currently, the routines available in 52*7c478bd9Sstevel@tonic-gate * libcmdutils perform necessary memory allocations, and do not 53*7c478bd9Sstevel@tonic-gate * perform locking, therefore they are not thread safe and 54*7c478bd9Sstevel@tonic-gate * should not be used by multi-threaded applications. 55*7c478bd9Sstevel@tonic-gate * 56*7c478bd9Sstevel@tonic-gate * For more information on the avl tree routines, see the well 57*7c478bd9Sstevel@tonic-gate * documented source code in avl.c, and the header files in 58*7c478bd9Sstevel@tonic-gate * <sys/avl.h> and <sys/avl_impl.h>. 59*7c478bd9Sstevel@tonic-gate * 60*7c478bd9Sstevel@tonic-gate * Note: The tree must be initialized in the calling application 61*7c478bd9Sstevel@tonic-gate * before calling these routines. An example of how this is done: 62*7c478bd9Sstevel@tonic-gate * static avl_tree_t *tree = NULL; 63*7c478bd9Sstevel@tonic-gate * 64*7c478bd9Sstevel@tonic-gate * tnode_compare() - This function is used by libavl's avl_find() 65*7c478bd9Sstevel@tonic-gate * routine to abstract out how the data structures are ordered, and 66*7c478bd9Sstevel@tonic-gate * must be an argument to libavl's avl_create() function. Therefore, 67*7c478bd9Sstevel@tonic-gate * this routine should not be called directly from the calling 68*7c478bd9Sstevel@tonic-gate * application. 69*7c478bd9Sstevel@tonic-gate * 70*7c478bd9Sstevel@tonic-gate * Input: 71*7c478bd9Sstevel@tonic-gate * const void *p1 (pointer to the 1st node to compare and 72*7c478bd9Sstevel@tonic-gate * is the node which we are try to match 73*7c478bd9Sstevel@tonic-gate * or insert into the search tree) 74*7c478bd9Sstevel@tonic-gate * const void *p2 (pointer to the 2nd node to compare and 75*7c478bd9Sstevel@tonic-gate * is a node which already exists in the 76*7c478bd9Sstevel@tonic-gate * search tree) 77*7c478bd9Sstevel@tonic-gate * 78*7c478bd9Sstevel@tonic-gate * This function returns (as required by the libavl interfaces): 79*7c478bd9Sstevel@tonic-gate * * -1 if the 1st argument node is less than the 2nd 80*7c478bd9Sstevel@tonic-gate * * 0 if the nodes are equal in value 81*7c478bd9Sstevel@tonic-gate * * +1 if the 1st node is greater than the 2nd 82*7c478bd9Sstevel@tonic-gate * 83*7c478bd9Sstevel@tonic-gate * add_tnode() - Builds a height balanced tree of nodes consisting of 84*7c478bd9Sstevel@tonic-gate * a device id and inode number provided by the calling application. 85*7c478bd9Sstevel@tonic-gate * The nodes are stored in the specified search tree by using the 86*7c478bd9Sstevel@tonic-gate * tnode_compare() routine. Duplicate nodes are not stored. 87*7c478bd9Sstevel@tonic-gate * 88*7c478bd9Sstevel@tonic-gate * If the specified search tree does not exist (is NULL), then memory 89*7c478bd9Sstevel@tonic-gate * is allocated for the tree, and libavl's avl_create() routine is 90*7c478bd9Sstevel@tonic-gate * called to initialize the tree with the comparison routine 91*7c478bd9Sstevel@tonic-gate * (tnode_compare()) which will be used to compare the tree nodes 92*7c478bd9Sstevel@tonic-gate * and populate the tree on subsequent calls by add_tnode() to 93*7c478bd9Sstevel@tonic-gate * avl_find(). 94*7c478bd9Sstevel@tonic-gate * 95*7c478bd9Sstevel@tonic-gate * This routine creates a node to be added to the search tree by 96*7c478bd9Sstevel@tonic-gate * allocating memory and setting the nodes device id and inode number 97*7c478bd9Sstevel@tonic-gate * to those specified. If the node does not exist in the search tree, 98*7c478bd9Sstevel@tonic-gate * it is added. If the node already exists in the tree, it is not 99*7c478bd9Sstevel@tonic-gate * added (remember, duplicate nodes are not stored), and the node is 100*7c478bd9Sstevel@tonic-gate * freed. 101*7c478bd9Sstevel@tonic-gate * 102*7c478bd9Sstevel@tonic-gate * Input: 103*7c478bd9Sstevel@tonic-gate * avl_tree_t **stree (search tree the data is to be stored in) 104*7c478bd9Sstevel@tonic-gate * dev_t device (device id of the inode to be stored) 105*7c478bd9Sstevel@tonic-gate * ino_t inode (inode number of inode to be stored) 106*7c478bd9Sstevel@tonic-gate * 107*7c478bd9Sstevel@tonic-gate * This function returns: 108*7c478bd9Sstevel@tonic-gate * * +1 if the node was added 109*7c478bd9Sstevel@tonic-gate * * 0 if the node was not added (node already exists) 110*7c478bd9Sstevel@tonic-gate * * -1 if an error occurred (memory allocation problem) 111*7c478bd9Sstevel@tonic-gate * 112*7c478bd9Sstevel@tonic-gate * destroy_tree() - The specified tree is destroyed by calling 113*7c478bd9Sstevel@tonic-gate * libavl's avl_destroy_nodes() routine to delete a tree without 114*7c478bd9Sstevel@tonic-gate * any rebalancing. Memory is freed that had been previously allocated 115*7c478bd9Sstevel@tonic-gate * by add_tnode() for the tree's nodes and the search tree itself. 116*7c478bd9Sstevel@tonic-gate * 117*7c478bd9Sstevel@tonic-gate * Input: 118*7c478bd9Sstevel@tonic-gate * avl_tree_t *stree (search tree to destroy) 119*7c478bd9Sstevel@tonic-gate * 120*7c478bd9Sstevel@tonic-gate * This function does not return anything. Note: The calling 121*7c478bd9Sstevel@tonic-gate * application is responsible for setting the search tree to NULL upon 122*7c478bd9Sstevel@tonic-gate * return. 123*7c478bd9Sstevel@tonic-gate */ 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate /* 126*7c478bd9Sstevel@tonic-gate * Compare two nodes by first trying to match on the node's device 127*7c478bd9Sstevel@tonic-gate * id, then on the inode number. Return -1 when p1 < p2, 128*7c478bd9Sstevel@tonic-gate * 0 when p1 == p2, and 1 when p1 > p2. This function is invoked 129*7c478bd9Sstevel@tonic-gate * by avl_find. p1 is always the node we are trying to insert or 130*7c478bd9Sstevel@tonic-gate * match in the search database. 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate int 133*7c478bd9Sstevel@tonic-gate tnode_compare(const void *p1, const void *p2) 134*7c478bd9Sstevel@tonic-gate { 135*7c478bd9Sstevel@tonic-gate tree_node_t *n1 = (tree_node_t *)p1; 136*7c478bd9Sstevel@tonic-gate tree_node_t *n2 = (tree_node_t *)p2; 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate /* first match device id */ 139*7c478bd9Sstevel@tonic-gate if (n1->node_dev < n2->node_dev) { 140*7c478bd9Sstevel@tonic-gate return (-1); 141*7c478bd9Sstevel@tonic-gate } else if (n1->node_dev == n2->node_dev) { 142*7c478bd9Sstevel@tonic-gate /* device id match, now check inode */ 143*7c478bd9Sstevel@tonic-gate if (n1->node_ino < n2->node_ino) { 144*7c478bd9Sstevel@tonic-gate return (-1); 145*7c478bd9Sstevel@tonic-gate } else if (n1->node_ino == n2->node_ino) { 146*7c478bd9Sstevel@tonic-gate return (0); 147*7c478bd9Sstevel@tonic-gate } else { 148*7c478bd9Sstevel@tonic-gate return (1); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate } else { 151*7c478bd9Sstevel@tonic-gate return (1); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate /* 156*7c478bd9Sstevel@tonic-gate * Build a height balanced tree of nodes consisting of a device id and 157*7c478bd9Sstevel@tonic-gate * an inode number. Duplicate nodes are not stored. Return 1 if 158*7c478bd9Sstevel@tonic-gate * node was added to the tree, return -1 upon error, otherwise return 0. 159*7c478bd9Sstevel@tonic-gate */ 160*7c478bd9Sstevel@tonic-gate int 161*7c478bd9Sstevel@tonic-gate add_tnode(avl_tree_t **stree, dev_t device, ino_t inode) 162*7c478bd9Sstevel@tonic-gate { 163*7c478bd9Sstevel@tonic-gate tree_node_t *tnode; 164*7c478bd9Sstevel@tonic-gate avl_index_t where; 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate /* 167*7c478bd9Sstevel@tonic-gate * Create an AVL search tree to keep track of inodes 168*7c478bd9Sstevel@tonic-gate * visited/reported. 169*7c478bd9Sstevel@tonic-gate */ 170*7c478bd9Sstevel@tonic-gate if (*stree == NULL) { 171*7c478bd9Sstevel@tonic-gate if ((*stree = calloc(1, sizeof (avl_tree_t))) 172*7c478bd9Sstevel@tonic-gate == NULL) { 173*7c478bd9Sstevel@tonic-gate return (-1); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate avl_create(*stree, 176*7c478bd9Sstevel@tonic-gate tnode_compare, 177*7c478bd9Sstevel@tonic-gate sizeof (tree_node_t), 178*7c478bd9Sstevel@tonic-gate OFFSETOF(tree_node_t, avl_link)); 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate /* Initialize the node */ 182*7c478bd9Sstevel@tonic-gate if ((tnode = calloc(1, sizeof (*tnode))) == NULL) { 183*7c478bd9Sstevel@tonic-gate return (-1); 184*7c478bd9Sstevel@tonic-gate } 185*7c478bd9Sstevel@tonic-gate tnode->node_dev = device; 186*7c478bd9Sstevel@tonic-gate tnode->node_ino = inode; 187*7c478bd9Sstevel@tonic-gate 188*7c478bd9Sstevel@tonic-gate /* If the node is not already in the tree, then insert it */ 189*7c478bd9Sstevel@tonic-gate if (avl_find(*stree, tnode, &where) == NULL) { 190*7c478bd9Sstevel@tonic-gate avl_insert(*stree, tnode, where); 191*7c478bd9Sstevel@tonic-gate return (1); 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate /* The node is already in the tree, so just free it */ 195*7c478bd9Sstevel@tonic-gate free(tnode); 196*7c478bd9Sstevel@tonic-gate return (0); 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate /* 200*7c478bd9Sstevel@tonic-gate * Destroy a search tree. 201*7c478bd9Sstevel@tonic-gate */ 202*7c478bd9Sstevel@tonic-gate void 203*7c478bd9Sstevel@tonic-gate destroy_tree(avl_tree_t *stree) 204*7c478bd9Sstevel@tonic-gate { 205*7c478bd9Sstevel@tonic-gate void *cookie; 206*7c478bd9Sstevel@tonic-gate tree_node_t *tnode; 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate if (stree != NULL) { 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate cookie = NULL; 211*7c478bd9Sstevel@tonic-gate while ((tnode = avl_destroy_nodes(stree, &cookie)) != NULL) { 212*7c478bd9Sstevel@tonic-gate free(tnode); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate avl_destroy(stree); 215*7c478bd9Sstevel@tonic-gate free(stree); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate } 218