17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*d1151f9bSJason King  *
26*d1151f9bSJason King  * Copyright 2017 Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/avl.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*d1151f9bSJason King #include <stddef.h>
337c478bd9Sstevel@tonic-gate #include "libcmdutils.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * The following interfaces complement the interfaces available in
377c478bd9Sstevel@tonic-gate  * libavl.
387c478bd9Sstevel@tonic-gate  * 	tnode_compare() - tree node comparison routine
397c478bd9Sstevel@tonic-gate  *	add_tnode() - adds nodes to a tree
407c478bd9Sstevel@tonic-gate  *	destroy_tree() - destroys a whole tree
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * The libavl routines are very generic and don't have any
437c478bd9Sstevel@tonic-gate  * direct knowledge about the data being stored in the AVL tree,
447c478bd9Sstevel@tonic-gate  * nor any of the details of the AVL tree representation.
457c478bd9Sstevel@tonic-gate  * In addition, the libavl routines do not perform any locking
467c478bd9Sstevel@tonic-gate  * or memory allocation.  Appropriate synchronization and memory
477c478bd9Sstevel@tonic-gate  * allocation are the responsibility of the user of the libavl
487c478bd9Sstevel@tonic-gate  * routines.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * These routines, and the structures defined in "libcmdutils.h",
517c478bd9Sstevel@tonic-gate  * provide the necessary details about the data and AVL tree
527c478bd9Sstevel@tonic-gate  * representation.  Currently, the routines available in
537c478bd9Sstevel@tonic-gate  * libcmdutils perform necessary memory allocations, and do not
547c478bd9Sstevel@tonic-gate  * perform locking, therefore they are not thread safe and
557c478bd9Sstevel@tonic-gate  * should not be used by multi-threaded applications.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * For more information on the avl tree routines, see the well
587c478bd9Sstevel@tonic-gate  * documented source code in avl.c, and the header files in
597c478bd9Sstevel@tonic-gate  * <sys/avl.h> and <sys/avl_impl.h>.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * Note: The tree must be initialized in the calling application
627c478bd9Sstevel@tonic-gate  * before calling these routines. An example of how this is done:
637c478bd9Sstevel@tonic-gate  *	static avl_tree_t       *tree = NULL;
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * tnode_compare() - This function is used by libavl's avl_find()
667c478bd9Sstevel@tonic-gate  * routine to abstract out how the data structures are ordered, and
677c478bd9Sstevel@tonic-gate  * must be an argument to libavl's avl_create() function.  Therefore,
687c478bd9Sstevel@tonic-gate  * this routine should not be called directly from the calling
697c478bd9Sstevel@tonic-gate  * application.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * Input:
727c478bd9Sstevel@tonic-gate  *	const void *p1	(pointer to the 1st node to compare and
737c478bd9Sstevel@tonic-gate  *			 is the node which we are try to match
747c478bd9Sstevel@tonic-gate  *			 or insert into the search tree)
757c478bd9Sstevel@tonic-gate  *	const void *p2	(pointer to the 2nd node to compare and
767c478bd9Sstevel@tonic-gate  *			 is a node which already exists in the
777c478bd9Sstevel@tonic-gate  *			 search tree)
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * This function returns (as required by the libavl interfaces):
807c478bd9Sstevel@tonic-gate  * 	* -1 if the 1st argument node is less than the 2nd
817c478bd9Sstevel@tonic-gate  * 	* 0 if the nodes are equal in value
827c478bd9Sstevel@tonic-gate  * 	* +1 if the 1st node is greater than the 2nd
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * add_tnode() - Builds a height balanced tree of nodes consisting of
857c478bd9Sstevel@tonic-gate  * a device id and inode number provided by the calling application.
867c478bd9Sstevel@tonic-gate  * The nodes are stored in the specified search tree by using the
877c478bd9Sstevel@tonic-gate  * tnode_compare() routine. Duplicate nodes are not stored.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * If the specified search tree does not exist (is NULL), then memory
907c478bd9Sstevel@tonic-gate  * is allocated for the tree, and libavl's avl_create() routine is
917c478bd9Sstevel@tonic-gate  * called to initialize the tree with the comparison routine
927c478bd9Sstevel@tonic-gate  * (tnode_compare()) which will be used to compare the tree nodes
937c478bd9Sstevel@tonic-gate  * and populate the tree on subsequent calls by add_tnode() to
947c478bd9Sstevel@tonic-gate  * avl_find().
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * This routine creates a node to be added to the search tree by
977c478bd9Sstevel@tonic-gate  * allocating memory and setting the nodes device id and inode number
987c478bd9Sstevel@tonic-gate  * to those specified.  If the node does not exist in the search tree,
997c478bd9Sstevel@tonic-gate  * it is added.  If the node already exists in the tree, it is not
1007c478bd9Sstevel@tonic-gate  * added (remember, duplicate nodes are not stored), and the node is
1017c478bd9Sstevel@tonic-gate  * freed.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * Input:
1047c478bd9Sstevel@tonic-gate  *	avl_tree_t **stree 	(search tree the data is to be stored in)
1057c478bd9Sstevel@tonic-gate  *	dev_t device		(device id of the inode to be stored)
1067c478bd9Sstevel@tonic-gate  *	ino_t inode		(inode number of inode to be stored)
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * This function returns:
1097c478bd9Sstevel@tonic-gate  * 	* +1 if the node was added
1107c478bd9Sstevel@tonic-gate  * 	* 0 if the node was not added (node already exists)
1117c478bd9Sstevel@tonic-gate  * 	* -1 if an error occurred (memory allocation problem)
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * destroy_tree() - The specified tree is destroyed by calling
1147c478bd9Sstevel@tonic-gate  * libavl's avl_destroy_nodes() routine to delete a tree without
1157c478bd9Sstevel@tonic-gate  * any rebalancing.  Memory is freed that had been previously allocated
1167c478bd9Sstevel@tonic-gate  * by add_tnode() for the tree's nodes and the search tree itself.
1177c478bd9Sstevel@tonic-gate  *
1187c478bd9Sstevel@tonic-gate  * Input:
1197c478bd9Sstevel@tonic-gate  *	avl_tree_t *stree	(search tree to destroy)
1207c478bd9Sstevel@tonic-gate  *
1217c478bd9Sstevel@tonic-gate  * This function does not return anything.  Note:  The calling
1227c478bd9Sstevel@tonic-gate  * application is responsible for setting the search tree to NULL upon
1237c478bd9Sstevel@tonic-gate  * return.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Compare two nodes by first trying to match on the node's device
1287c478bd9Sstevel@tonic-gate  * id, then on the inode number.  Return -1 when p1 < p2,
1297c478bd9Sstevel@tonic-gate  * 0 when p1 == p2, and 1 when p1 > p2.  This function is invoked
1307c478bd9Sstevel@tonic-gate  * by avl_find.  p1 is always the node we are trying to insert or
1317c478bd9Sstevel@tonic-gate  * match in the search database.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate int
tnode_compare(const void * p1,const void * p2)1347c478bd9Sstevel@tonic-gate tnode_compare(const void *p1, const void *p2)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	tree_node_t *n1 = (tree_node_t *)p1;
1377c478bd9Sstevel@tonic-gate 	tree_node_t *n2 = (tree_node_t *)p2;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* first match device id */
1407c478bd9Sstevel@tonic-gate 	if (n1->node_dev < n2->node_dev) {
1417c478bd9Sstevel@tonic-gate 		return (-1);
1427c478bd9Sstevel@tonic-gate 	} else if (n1->node_dev == n2->node_dev) {
1437c478bd9Sstevel@tonic-gate 		/* device id match, now check inode */
1447c478bd9Sstevel@tonic-gate 		if (n1->node_ino < n2->node_ino) {
1457c478bd9Sstevel@tonic-gate 			return (-1);
1467c478bd9Sstevel@tonic-gate 		} else if (n1->node_ino == n2->node_ino) {
1477c478bd9Sstevel@tonic-gate 			return (0);
1487c478bd9Sstevel@tonic-gate 		} else {
1497c478bd9Sstevel@tonic-gate 			return (1);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	} else {
1527c478bd9Sstevel@tonic-gate 		return (1);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * Build a height balanced tree of nodes consisting of a device id and
1587c478bd9Sstevel@tonic-gate  * an inode number.  Duplicate nodes are not stored.  Return 1 if
1597c478bd9Sstevel@tonic-gate  * node was added to the tree, return -1 upon error, otherwise return 0.
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate int
add_tnode(avl_tree_t ** stree,dev_t device,ino_t inode)1627c478bd9Sstevel@tonic-gate add_tnode(avl_tree_t **stree, dev_t device, ino_t inode)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	tree_node_t	*tnode;
1657c478bd9Sstevel@tonic-gate 	avl_index_t	where;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Create an AVL search tree to keep track of inodes
1697c478bd9Sstevel@tonic-gate 	 * visited/reported.
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	if (*stree == NULL) {
1727c478bd9Sstevel@tonic-gate 		if ((*stree = calloc(1, sizeof (avl_tree_t)))
1737c478bd9Sstevel@tonic-gate 		    == NULL) {
1747c478bd9Sstevel@tonic-gate 			return (-1);
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 		avl_create(*stree,
1777c478bd9Sstevel@tonic-gate 		    tnode_compare,
1787c478bd9Sstevel@tonic-gate 		    sizeof (tree_node_t),
179*d1151f9bSJason King 		    offsetof(tree_node_t, avl_link));
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Initialize the node */
1837c478bd9Sstevel@tonic-gate 	if ((tnode = calloc(1, sizeof (*tnode))) == NULL) {
1847c478bd9Sstevel@tonic-gate 		return (-1);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	tnode->node_dev = device;
1877c478bd9Sstevel@tonic-gate 	tnode->node_ino = inode;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* If the node is not already in the tree, then insert it */
1907c478bd9Sstevel@tonic-gate 	if (avl_find(*stree, tnode, &where) == NULL) {
1917c478bd9Sstevel@tonic-gate 		avl_insert(*stree, tnode, where);
1927c478bd9Sstevel@tonic-gate 		return (1);
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/* The node is already in the tree, so just free it */
1967c478bd9Sstevel@tonic-gate 	free(tnode);
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * Destroy a search tree.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate void
destroy_tree(avl_tree_t * stree)2047c478bd9Sstevel@tonic-gate destroy_tree(avl_tree_t *stree)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	void *cookie;
2077c478bd9Sstevel@tonic-gate 	tree_node_t	*tnode;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (stree != NULL) {
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		cookie = NULL;
2127c478bd9Sstevel@tonic-gate 		while ((tnode = avl_destroy_nodes(stree, &cookie)) != NULL) {
2137c478bd9Sstevel@tonic-gate 			free(tnode);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		avl_destroy(stree);
2167c478bd9Sstevel@tonic-gate 		free(stree);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate }
219