1 /* $OpenBSD: ntfs_ihash.c,v 1.22 2025/01/13 13:58:41 claudio 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/rwlock.h>
39 #include <sys/vnode.h>
40 #include <sys/malloc.h>
41 #include <sys/mount.h>
42
43 #include <crypto/siphash.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 caching.
51 */
52 u_int ntfs_hash(dev_t, ntfsino_t);
53 static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
54 static SIPHASH_KEY ntfs_nthashkey;
55 static u_long ntfs_nthash; /* size of hash table - 1 */
56 #define NTNOHASH(device, inum) ntfs_hash((device), (inum))
57
58 /*
59 * Initialize inode hash table.
60 */
61 void
ntfs_nthashinit(void)62 ntfs_nthashinit(void)
63 {
64 u_long nthash;
65 void *nthashtbl;
66
67 if (ntfs_nthashtbl)
68 return;
69
70 nthashtbl = hashinit(initialvnodes, M_NTFSNTHASH, M_WAITOK, &nthash);
71 if (ntfs_nthashtbl) {
72 hashfree(nthashtbl, initialvnodes, M_NTFSNTHASH);
73 return;
74 }
75 ntfs_nthashtbl = nthashtbl;
76 ntfs_nthash = nthash;
77
78 arc4random_buf(&ntfs_nthashkey, sizeof(ntfs_nthashkey));
79 }
80
81 u_int
ntfs_hash(dev_t dev,ntfsino_t inum)82 ntfs_hash(dev_t dev, ntfsino_t inum)
83 {
84 SIPHASH_CTX ctx;
85
86 SipHash24_Init(&ctx, &ntfs_nthashkey);
87 SipHash24_Update(&ctx, &dev, sizeof(dev));
88 SipHash24_Update(&ctx, &inum, sizeof(inum));
89
90 return (SipHash24_End(&ctx) & ntfs_nthash);
91 }
92
93 /*
94 * Use the device/inum pair to find the incore inode, and return a pointer
95 * to it. If it is in core, return it, even if it is locked.
96 */
97 struct ntnode *
ntfs_nthashlookup(dev_t dev,ntfsino_t inum)98 ntfs_nthashlookup(dev_t dev, ntfsino_t inum)
99 {
100 struct ntnode *ip;
101 struct nthashhead *ipp;
102
103 /* XXXLOCKING lock hash list? */
104 ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)];
105 LIST_FOREACH(ip, ipp, i_hash) {
106 if (inum == ip->i_number && dev == ip->i_dev)
107 break;
108 }
109 /* XXXLOCKING unlock hash list? */
110
111 return (ip);
112 }
113
114 /*
115 * Insert the ntnode into the hash table.
116 */
117 int
ntfs_nthashins(struct ntnode * ip)118 ntfs_nthashins(struct ntnode *ip)
119 {
120 struct nthashhead *ipp;
121 struct ntnode *curip;
122
123 /* XXXLOCKING lock hash list? */
124 ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)];
125 LIST_FOREACH(curip, ipp, i_hash) {
126 if (ip->i_number == curip->i_number &&
127 ip->i_dev == curip->i_dev)
128 return (EEXIST);
129 }
130
131 ip->i_flag |= IN_HASHED;
132 LIST_INSERT_HEAD(ipp, ip, i_hash);
133 /* XXXLOCKING unlock hash list? */
134
135 return (0);
136 }
137
138 /*
139 * Remove the inode from the hash table.
140 */
141 void
ntfs_nthashrem(struct ntnode * ip)142 ntfs_nthashrem(struct ntnode *ip)
143 {
144 /* XXXLOCKING lock hash list? */
145 if (ip->i_flag & IN_HASHED) {
146 ip->i_flag &= ~IN_HASHED;
147 LIST_REMOVE(ip, i_hash);
148 }
149 /* XXXLOCKING unlock hash list? */
150 }
151