1 /* $OpenBSD: ntfs_ihash.c,v 1.21 2021/03/11 13:31:35 jsg 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 struct rwlock ntfs_hashlock = RWLOCK_INITIALIZER("ntfs_nthashlock"); 58 59 /* 60 * Initialize inode hash table. 61 */ 62 void 63 ntfs_nthashinit(void) 64 { 65 u_long nthash; 66 void *nthashtbl; 67 68 if (ntfs_nthashtbl) 69 return; 70 71 nthashtbl = hashinit(initialvnodes, M_NTFSNTHASH, M_WAITOK, &nthash); 72 if (ntfs_nthashtbl) { 73 hashfree(nthashtbl, initialvnodes, M_NTFSNTHASH); 74 return; 75 } 76 ntfs_nthashtbl = nthashtbl; 77 ntfs_nthash = nthash; 78 79 arc4random_buf(&ntfs_nthashkey, sizeof(ntfs_nthashkey)); 80 } 81 82 u_int 83 ntfs_hash(dev_t dev, ntfsino_t inum) 84 { 85 SIPHASH_CTX ctx; 86 87 SipHash24_Init(&ctx, &ntfs_nthashkey); 88 SipHash24_Update(&ctx, &dev, sizeof(dev)); 89 SipHash24_Update(&ctx, &inum, sizeof(inum)); 90 91 return (SipHash24_End(&ctx) & ntfs_nthash); 92 } 93 94 /* 95 * Use the device/inum pair to find the incore inode, and return a pointer 96 * to it. If it is in core, return it, even if it is locked. 97 */ 98 struct ntnode * 99 ntfs_nthashlookup(dev_t dev, ntfsino_t inum) 100 { 101 struct ntnode *ip; 102 struct nthashhead *ipp; 103 104 /* XXXLOCKING lock hash list? */ 105 ipp = &ntfs_nthashtbl[NTNOHASH(dev, inum)]; 106 LIST_FOREACH(ip, ipp, i_hash) { 107 if (inum == ip->i_number && dev == ip->i_dev) 108 break; 109 } 110 /* XXXLOCKING unlock hash list? */ 111 112 return (ip); 113 } 114 115 /* 116 * Insert the ntnode into the hash table. 117 */ 118 void 119 ntfs_nthashins(struct ntnode *ip) 120 { 121 struct nthashhead *ipp; 122 123 /* XXXLOCKING lock hash list? */ 124 ipp = &ntfs_nthashtbl[NTNOHASH(ip->i_dev, ip->i_number)]; 125 LIST_INSERT_HEAD(ipp, ip, i_hash); 126 ip->i_flag |= IN_HASHED; 127 /* XXXLOCKING unlock hash list? */ 128 } 129 130 /* 131 * Remove the inode from the hash table. 132 */ 133 void 134 ntfs_nthashrem(struct ntnode *ip) 135 { 136 /* XXXLOCKING lock hash list? */ 137 if (ip->i_flag & IN_HASHED) { 138 ip->i_flag &= ~IN_HASHED; 139 LIST_REMOVE(ip, i_hash); 140 } 141 /* XXXLOCKING unlock hash list? */ 142 } 143