xref: /openbsd-src/sys/ntfs/ntfs_ihash.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: ntfs_ihash.c,v 1.15 2014/07/12 18:43:52 tedu Exp $	*/
2 /*	$NetBSD: ntfs_ihash.c,v 1.1 2002/12/23 17:38:32 jdolecek Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ufs_ihash.c	8.7 (Berkeley) 5/17/95
33  * Id: ntfs_ihash.c,v 1.5 1999/05/12 09:42:58 semenu Exp
34  */
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/rwlock.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/mount.h>
44 
45 #include <ntfs/ntfs.h>
46 #include <ntfs/ntfs_inode.h>
47 #include <ntfs/ntfs_ihash.h>
48 
49 /*
50  * Structures associated with inode cacheing.
51  */
52 static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
53 static u_long	ntfs_nthash;		/* size of hash table - 1 */
54 #define	NTNOHASH(device, inum)	((minor(device) + (inum)) & ntfs_nthash)
55 struct rwlock ntfs_hashlock = RWLOCK_INITIALIZER("ntfs_nthashlock");
56 
57 /*
58  * Initialize inode hash table.
59  */
60 void
61 ntfs_nthashinit(void)
62 {
63 	u_long nthash;
64 	void *nthashtbl;
65 
66 	if (ntfs_nthashtbl)
67 		return;
68 
69 	nthashtbl = hashinit(desiredvnodes, M_NTFSNTHASH, M_WAITOK, &nthash);
70 	if (ntfs_nthashtbl) {
71 		free(nthashtbl, M_NTFSNTHASH, 0);
72 		return;
73 	}
74 	ntfs_nthashtbl = nthashtbl;
75 	ntfs_nthash = nthash;
76 }
77 
78 /*
79  * Use the device/inum pair to find the incore inode, and return a pointer
80  * to it. If it is in core, return it, even if it is locked.
81  */
82 struct ntnode *
83 ntfs_nthashlookup(dev_t dev, ntfsino_t inum)
84 {
85 	struct ntnode *ip;
86 	struct nthashhead *ipp;
87 
88 	/* XXXLOCKING lock hash list? */
89 	ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)];
90 	LIST_FOREACH(ip, ipp, i_hash) {
91 		if (inum == ip->i_number && dev == ip->i_dev)
92 			break;
93 	}
94 	/* XXXLOCKING unlock hash list? */
95 
96 	return (ip);
97 }
98 
99 /*
100  * Insert the ntnode into the hash table.
101  */
102 void
103 ntfs_nthashins(struct ntnode *ip)
104 {
105 	struct nthashhead *ipp;
106 
107 	/* XXXLOCKING lock hash list? */
108 	ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)];
109 	LIST_INSERT_HEAD(ipp, ip, i_hash);
110 	ip->i_flag |= IN_HASHED;
111 	/* XXXLOCKING unlock hash list? */
112 }
113 
114 /*
115  * Remove the inode from the hash table.
116  */
117 void
118 ntfs_nthashrem(struct ntnode *ip)
119 {
120 	/* XXXLOCKING lock hash list? */
121 	if (ip->i_flag & IN_HASHED) {
122 		ip->i_flag &= ~IN_HASHED;
123 		LIST_REMOVE(ip, i_hash);
124 	}
125 	/* XXXLOCKING unlock hash list? */
126 }
127