ntfs_ihash.c revision 43552
1/*
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)ufs_ihash.c	8.7 (Berkeley) 5/17/95
34 * $Id: ntfs_ihash.c,v 1.2 1999/01/02 01:17:38 semen Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/vnode.h>
42#include <sys/malloc.h>
43#include <sys/proc.h>
44
45#include <ntfs/ntfs.h>
46#include <ntfs/ntfs_inode.h>
47
48MALLOC_DEFINE(M_NTFSIHASH, "NTFS ihash", "NTFS Inode hash tables");
49
50/*
51 * Structures associated with inode cacheing.
52 */
53static LIST_HEAD(ihashhead, ntnode) *ntfs_ihashtbl;
54static u_long	ntfs_ihash;		/* size of hash table - 1 */
55#define	NTNOHASH(device, inum)	(&ntfs_ihashtbl[((device) + (inum)) & ntfs_ihash])
56static struct simplelock ntfs_ihash_slock;
57
58/*
59 * Initialize inode hash table.
60 */
61void
62ntfs_ihashinit()
63{
64
65	ntfs_ihashtbl = hashinit(desiredvnodes, M_NTFSIHASH, &ntfs_ihash);
66	simple_lock_init(&ntfs_ihash_slock);
67}
68
69/*
70 * Use the device/inum pair to find the incore inode, and return a pointer
71 * to it. If it is in core, return it, even if it is locked.
72 */
73struct vnode *
74ntfs_ihashlookup(dev, inum)
75	dev_t dev;
76	ino_t inum;
77{
78	struct ntnode *ip;
79
80	simple_lock(&ntfs_ihash_slock);
81	for (ip = NTNOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next)
82		if (inum == ip->i_number && dev == ip->i_dev)
83			break;
84	simple_unlock(&ntfs_ihash_slock);
85
86	if (ip)
87		return (NTTOV(ip));
88	return (NULLVP);
89}
90
91/*
92 * Use the device/inum pair to find the incore inode, and return a pointer
93 * to it. If it is in core, but locked, wait for it.
94 */
95struct vnode *
96ntfs_ihashget(dev, inum)
97	dev_t dev;
98	ino_t inum;
99{
100	struct proc *p = curproc;	/* XXX */
101	struct ntnode *ip;
102	struct vnode *vp;
103
104loop:
105	simple_lock(&ntfs_ihash_slock);
106	for (ip = NTNOHASH(dev, inum)->lh_first; ip; ip = ip->i_hash.le_next) {
107		if (inum == ip->i_number && dev == ip->i_dev) {
108			vp = NTTOV(ip);
109			simple_lock(&vp->v_interlock);
110			simple_unlock(&ntfs_ihash_slock);
111			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
112				goto loop;
113			return (vp);
114		}
115	}
116	simple_unlock(&ntfs_ihash_slock);
117	return (NULL);
118}
119
120/*
121 * Insert the inode into the hash table, and return it locked.
122 */
123void
124ntfs_ihashins(ip)
125	struct ntnode *ip;
126{
127	struct proc *p = curproc;		/* XXX */
128	struct ihashhead *ipp;
129
130	/* lock the inode, then put it on the appropriate hash list */
131	lockmgr(&ip->i_lock, LK_EXCLUSIVE, (struct simplelock *)0, p);
132
133	simple_lock(&ntfs_ihash_slock);
134	ipp = NTNOHASH(ip->i_dev, ip->i_number);
135	LIST_INSERT_HEAD(ipp, ip, i_hash);
136	ip->i_flag |= IN_HASHED;
137	simple_unlock(&ntfs_ihash_slock);
138}
139
140/*
141 * Remove the inode from the hash table.
142 */
143void
144ntfs_ihashrem(ip)
145	struct ntnode *ip;
146{
147	simple_lock(&ntfs_ihash_slock);
148	if (ip->i_flag & IN_HASHED) {
149		ip->i_flag &= ~IN_HASHED;
150		LIST_REMOVE(ip, i_hash);
151#ifdef DIAGNOSTIC
152		ip->i_hash.le_next = NULL;
153		ip->i_hash.le_prev = NULL;
154#endif
155	}
156	simple_unlock(&ntfs_ihash_slock);
157}
158