this cuts all the hashes in the filesystems over to using siphash
to get keys into their hashes.
i have compiled all of them, and will start on testing them as much
as i can. so far i have done softdep and started on the nfs srv
cache. id like help and review though.
Index: isofs/cd9660/cd9660_node.c
===================================================================
RCS file: /cvs/src/sys/isofs/cd9660/cd9660_node.c,v
retrieving revision 1.25
diff -u -p -r1.25 cd9660_node.c
--- isofs/cd9660/cd9660_node.c 14 Sep 2014 14:17:25 -0000 1.25
+++ isofs/cd9660/cd9660_node.c 18 Nov 2014 01:03:13 -0000
@@ -48,6 +48,9 @@
#include <sys/malloc.h>
#include <sys/stat.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <isofs/cd9660/iso.h>
#include <isofs/cd9660/cd9660_extern.h>
#include <isofs/cd9660/cd9660_node.h>
@@ -56,9 +59,12 @@
/*
* Structures associated with iso_node caching.
*/
+u_int cd9660_isohash(dev_t, cdino_t);
+
struct iso_node **isohashtbl;
u_long isohash;
-#define INOHASH(device, inum) (((device) + ((inum)>>12)) & isohash)
+SIPHASH_KEY isohashkey;
+#define INOHASH(device, inum) cd9660_isohash((device), (inum))
extern int prtactive; /* 1 => print out reclaim of active vnodes */
@@ -73,7 +79,19 @@ cd9660_init(vfsp)
{
isohashtbl = hashinit(desiredvnodes, M_ISOFSMNT, M_WAITOK, &isohash);
+ arc4random_buf(&isohashkey, sizeof(isohashkey));
return (0);
+}
+
+u_int
+cd9660_isohash(dev_t device, cdino_t inum)
+{
+ SIPHASH_CTX ctx;
+
+ SipHash24_Init(&ctx, &isohashkey);
+ SipHash24_Update(&ctx, &device, sizeof(device));
+ SipHash24_Update(&ctx, &inum, sizeof(inum));
+ return (SipHash24_End(&ctx) & isohash);
}
/*
Index: isofs/udf/udf.h
===================================================================
RCS file: /cvs/src/sys/isofs/udf/udf.h,v
retrieving revision 1.19
diff -u -p -r1.19 udf.h
--- isofs/udf/udf.h 17 Sep 2013 04:31:56 -0000 1.19
+++ isofs/udf/udf.h 18 Nov 2014 01:03:13 -0000
@@ -71,6 +71,7 @@ struct umount {
struct unode *um_vat;
struct long_ad um_root_icb;
LIST_HEAD(udf_hash_lh, unode) *um_hashtbl;
+ SIPHASH_KEY um_hashkey;
u_long um_hashsz;
struct mutex um_hashmtx;
int um_psecs;
Index: isofs/udf/udf_subr.c
===================================================================
RCS file: /cvs/src/sys/isofs/udf/udf_subr.c,v
retrieving revision 1.23
diff -u -p -r1.23 udf_subr.c
--- isofs/udf/udf_subr.c 3 Nov 2014 21:28:35 -0000 1.23
+++ isofs/udf/udf_subr.c 18 Nov 2014 01:03:13 -0000
@@ -38,6 +38,9 @@
#include <sys/dirent.h>
#include <sys/disklabel.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <isofs/udf/ecma167-udf.h>
#include <isofs/udf/udf.h>
#include <isofs/udf/udf_extern.h>
Index: isofs/udf/udf_vfsops.c
===================================================================
RCS file: /cvs/src/sys/isofs/udf/udf_vfsops.c,v
retrieving revision 1.42
diff -u -p -r1.42 udf_vfsops.c
--- isofs/udf/udf_vfsops.c 12 Jul 2014 18:50:00 -0000 1.42
+++ isofs/udf/udf_vfsops.c 18 Nov 2014 01:03:13 -0000
@@ -67,6 +67,9 @@
#include <sys/endian.h>
#include <sys/specdev.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <isofs/udf/ecma167-udf.h>
#include <isofs/udf/udf.h>
#include <isofs/udf/udf_extern.h>
@@ -364,6 +367,7 @@ udf_mountfs(struct vnode *devvp, struct
mtx_init(&ump->um_hashmtx, IPL_NONE);
ump->um_hashtbl = hashinit(UDF_HASHTBLSIZE, M_UDFMOUNT, M_WAITOK,
&ump->um_hashsz);
+ arc4random_buf(&ump->um_hashkey, sizeof(ump->um_hashkey));
/* Get the VAT, if needed */
if (ump->um_flags & UDF_MNT_FIND_VAT) {
Index: isofs/udf/udf_vnops.c
===================================================================
RCS file: /cvs/src/sys/isofs/udf/udf_vnops.c,v
retrieving revision 1.56
diff -u -p -r1.56 udf_vnops.c
--- isofs/udf/udf_vnops.c 3 Nov 2014 21:28:35 -0000 1.56
+++ isofs/udf/udf_vnops.c 18 Nov 2014 01:03:13 -0000
@@ -50,6 +50,9 @@
#include <sys/specdev.h>
#include <sys/unistd.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <isofs/udf/ecma167-udf.h>
#include <isofs/udf/udf.h>
#include <isofs/udf/udf_extern.h>
@@ -92,7 +95,8 @@ udf_hashlookup(struct umount *ump, udfin
loop:
mtx_enter(&ump->um_hashmtx);
- lh = &ump->um_hashtbl[id & ump->um_hashsz];
+ lh = &ump->um_hashtbl[SipHash24(&ump->um_hashkey, &id, sizeof(id)) &
+ ump->um_hashsz];
if (lh == NULL) {
mtx_leave(&ump->um_hashmtx);
return (ENOENT);
@@ -127,7 +131,8 @@ udf_hashins(struct unode *up)
vn_lock(up->u_vnode, LK_EXCLUSIVE | LK_RETRY, p);
mtx_enter(&ump->um_hashmtx);
- lh = &ump->um_hashtbl[up->u_ino & ump->um_hashsz];
+ lh = &ump->um_hashtbl[SipHash24(&ump->um_hashkey,
+ &up->u_ino, sizeof(up->u_ino)) & ump->um_hashsz];
if (lh == NULL)
panic("hash entry is NULL, up->u_ino = %d", up->u_ino);
LIST_INSERT_HEAD(lh, up, u_le);
@@ -145,7 +150,8 @@ udf_hashrem(struct unode *up)
ump = up->u_ump;
mtx_enter(&ump->um_hashmtx);
- lh = &ump->um_hashtbl[up->u_ino & ump->um_hashsz];
+ lh = &ump->um_hashtbl[SipHash24(&ump->um_hashkey,
+ &up->u_ino, sizeof(up->u_ino)) & ump->um_hashsz];
if (lh == NULL)
panic("hash entry is NULL, up->u_ino = %d", up->u_ino);
LIST_REMOVE(up, u_le);
Index: msdosfs/msdosfs_denode.c
===================================================================
RCS file: /cvs/src/sys/msdosfs/msdosfs_denode.c,v
retrieving revision 1.50
diff -u -p -r1.50 msdosfs_denode.c
--- msdosfs/msdosfs_denode.c 14 Sep 2014 14:17:26 -0000 1.50
+++ msdosfs/msdosfs_denode.c 18 Nov 2014 01:03:13 -0000
@@ -58,16 +58,21 @@
#include <sys/dirent.h>
#include <sys/namei.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <msdosfs/bpb.h>
#include <msdosfs/msdosfsmount.h>
#include <msdosfs/direntry.h>
#include <msdosfs/denode.h>
#include <msdosfs/fat.h>
+u_int msdosfs_dehash(dev_t, uint32_t, uint32_t);
+
struct denode **dehashtbl;
+SIPHASH_KEY dehashkey;
u_long dehash; /* size of hash table - 1 */
-#define DEHASH(dev, dcl, doff) (((dev) + (dcl) + (doff) /
sizeof(struct direntry)) \
- & dehash)
+#define DEHASH(dev, dcl, doff) msdosfs_dehash((dev), (dcl), (doff))
static struct denode *msdosfs_hashget(dev_t, uint32_t, uint32_t);
static int msdosfs_hashins(struct denode *);
@@ -78,7 +83,21 @@ int
msdosfs_init(struct vfsconf *vfsp)
{
dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, M_WAITOK, &dehash);
+ arc4random_buf(&dehashkey, sizeof(dehashkey));
return (0);
+}
+
+u_int
+msdosfs_dehash(dev_t dev, uint32_t dirclust, uint32_t diroff)
+{
+ SIPHASH_CTX ctx;
+
+ SipHash24_Init(&ctx, &dehashkey);
+ SipHash24_Update(&ctx, &dev, sizeof(dev));
+ SipHash24_Update(&ctx, &dirclust, sizeof(dirclust));
+ SipHash24_Update(&ctx, &diroff, sizeof(diroff));
+
+ return (SipHash24_End(&ctx) & dehash);
}
static struct denode *
Index: nfs/nfs_srvcache.c
===================================================================
RCS file: /cvs/src/sys/nfs/nfs_srvcache.c,v
retrieving revision 1.25
diff -u -p -r1.25 nfs_srvcache.c
--- nfs/nfs_srvcache.c 14 Sep 2014 14:17:26 -0000 1.25
+++ nfs/nfs_srvcache.c 18 Nov 2014 01:03:13 -0000
@@ -49,6 +49,9 @@
#include <sys/socket.h>
#include <sys/queue.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <netinet/in.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsproto.h>
@@ -63,11 +66,12 @@ long numnfsrvcache, desirednfsrvcache =
struct nfsrvcache *nfsrv_lookupcache(struct nfsrv_descript *);
void nfsrv_cleanentry(struct nfsrvcache *);
-#define NFSRCHASH(xid) \
- (&nfsrvhashtbl[((xid) + ((xid) >> 24)) & nfsrvhash])
LIST_HEAD(nfsrvhash, nfsrvcache) *nfsrvhashtbl;
+SIPHASH_KEY nfsrvhashkey;
TAILQ_HEAD(nfsrvlru, nfsrvcache) nfsrvlruhead;
u_long nfsrvhash;
+#define NFSRCHASH(xid) \
+ (&nfsrvhashtbl[SipHash24(&nfsrvhashkey, &(xid), sizeof(xid)) & nfsrvhash])
#define NETFAMILY(rp) \
(((rp)->rc_flag & RC_INETADDR) ? AF_INET : AF_UNSPEC)
@@ -104,6 +108,7 @@ nfsrv_initcache(void)
{
nfsrvhashtbl = hashinit(desirednfsrvcache, M_NFSD, M_WAITOK,
&nfsrvhash);
+ arc4random_buf(&nfsrvhashkey, sizeof(nfsrvhashkey));
TAILQ_INIT(&nfsrvlruhead);
}
@@ -125,6 +130,7 @@ int
nfsrv_getcache(struct nfsrv_descript *nd, struct nfssvc_sock *slp,
struct mbuf **repp)
{
+ struct nfsrvhash *hash;
struct nfsrvcache *rp;
struct mbuf *mb;
struct sockaddr_in *saddr;
@@ -203,7 +209,8 @@ nfsrv_getcache(struct nfsrv_descript *nd
break;
};
rp->rc_proc = nd->nd_procnum;
- LIST_INSERT_HEAD(NFSRCHASH(nd->nd_retxid), rp, rc_hash);
+ hash = NFSRCHASH(nd->nd_retxid);
+ LIST_INSERT_HEAD(hash, rp, rc_hash);
rp->rc_flag &= ~RC_LOCKED;
if (rp->rc_flag & RC_WANTED) {
rp->rc_flag &= ~RC_WANTED;
@@ -269,10 +276,12 @@ nfsrv_cleancache(void)
struct nfsrvcache *
nfsrv_lookupcache(struct nfsrv_descript *nd)
{
+ struct nfsrvhash *hash;
struct nfsrvcache *rp;
+ hash = NFSRCHASH(nd->nd_retxid);
loop:
- LIST_FOREACH(rp, NFSRCHASH(nd->nd_retxid), rc_hash) {
+ LIST_FOREACH(rp, hash, rc_hash) {
if (nd->nd_retxid == rp->rc_xid &&
nd->nd_procnum == rp->rc_proc &&
netaddr_match(NETFAMILY(rp), &rp->rc_haddr, nd->nd_nam)) {
Index: ntfs/ntfs_ihash.c
===================================================================
RCS file: /cvs/src/sys/ntfs/ntfs_ihash.c,v
retrieving revision 1.16
diff -u -p -r1.16 ntfs_ihash.c
--- ntfs/ntfs_ihash.c 14 Sep 2014 14:17:26 -0000 1.16
+++ ntfs/ntfs_ihash.c 18 Nov 2014 01:03:13 -0000
@@ -41,6 +41,9 @@
#include <sys/malloc.h>
#include <sys/mount.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
#include <ntfs/ntfs.h>
#include <ntfs/ntfs_inode.h>
#include <ntfs/ntfs_ihash.h>
@@ -48,9 +51,11 @@
/*
* Structures associated with inode cacheing.
*/
+u_int ntfs_hash(dev_t, ntfsino_t);
static LIST_HEAD(nthashhead, ntnode) *ntfs_nthashtbl;
+static SIPHASH_KEY ntfs_nthashkey;
static u_long ntfs_nthash; /* size of hash table - 1 */
-#define NTNOHASH(device, inum) ((minor(device) + (inum)) & ntfs_nthash)
+#define NTNOHASH(device, inum) ntfs_hash((device), (inum))
struct rwlock ntfs_hashlock = RWLOCK_INITIALIZER("ntfs_nthashlock");
/*
@@ -72,6 +77,20 @@ ntfs_nthashinit(void)
}
ntfs_nthashtbl = nthashtbl;
ntfs_nthash = nthash;
+
+ arc4random_buf(&ntfs_nthashkey, sizeof(ntfs_nthashkey));
+}
+
+u_int
+ntfs_hash(dev_t dev, ntfsino_t inum)
+{
+ SIPHASH_CTX ctx;
+
+ SipHash24_Init(&ctx, &ntfs_nthashkey);
+ SipHash24_Update(&ctx, &dev, sizeof(dev));
+ SipHash24_Update(&ctx, &inum, sizeof(inum));
+
+ return (SipHash24_End(&ctx) & ntfs_nthash);
}
/*
Index: ufs/ffs/ffs_softdep.c
===================================================================
RCS file: /cvs/src/sys/ufs/ffs/ffs_softdep.c,v
retrieving revision 1.128
diff -u -p -r1.128 ffs_softdep.c
--- ufs/ffs/ffs_softdep.c 12 Jul 2014 18:44:01 -0000 1.128
+++ ufs/ffs/ffs_softdep.c 18 Nov 2014 01:03:13 -0000
@@ -52,6 +52,8 @@
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/specdev.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/quota.h>
#include <ufs/ufs/inode.h>
@@ -938,14 +940,13 @@ softdep_flushfiles(struct mount *oldmnt,
#define DEPALLOC 0x0001 /* allocate structure if lookup fails */
#define NODELAY 0x0002 /* cannot do background work */
+SIPHASH_KEY softdep_hashkey;
+
/*
* Structures and routines associated with pagedep caching.
*/
LIST_HEAD(pagedep_hashhead, pagedep) *pagedep_hashtbl;
u_long pagedep_hash; /* size of hash table - 1 */
-#define PAGEDEP_HASH(mp, inum, lbn) \
- (&pagedep_hashtbl[((((register_t)(mp)) >> 13) + (inum) + (lbn)) & \
- pagedep_hash])
STATIC struct sema pagedep_in_progress;
/*
@@ -959,6 +960,7 @@ STATIC int
pagedep_lookup(struct inode *ip, daddr_t lbn, int flags,
struct pagedep **pagedeppp)
{
+ SIPHASH_CTX ctx;
struct pagedep *pagedep;
struct pagedep_hashhead *pagedephd;
struct mount *mp;
@@ -971,7 +973,12 @@ pagedep_lookup(struct inode *ip, daddr_t
panic("pagedep_lookup: lock not held");
#endif
mp = ITOV(ip)->v_mount;
- pagedephd = PAGEDEP_HASH(mp, ip->i_number, lbn);
+
+ SipHash24_Init(&ctx, &softdep_hashkey);
+ SipHash24_Update(&ctx, &mp, sizeof(mp));
+ SipHash24_Update(&ctx, &ip->i_number, sizeof(ip->i_number));
+ SipHash24_Update(&ctx, &lbn, sizeof(lbn));
+ pagedephd = &pagedep_hashtbl[SipHash24_End(&ctx) & pagedep_hash];
top:
LIST_FOREACH(pagedep, pagedephd, pd_hash)
if (ip->i_number == pagedep->pd_ino &&
@@ -1015,8 +1022,6 @@ top:
LIST_HEAD(inodedep_hashhead, inodedep) *inodedep_hashtbl;
STATIC u_long inodedep_hash; /* size of hash table - 1 */
STATIC long num_inodedep; /* number of inodedep allocated */
-#define INODEDEP_HASH(fs, inum) \
- (&inodedep_hashtbl[((((register_t)(fs)) >> 13) + (inum)) &
inodedep_hash])
STATIC struct sema inodedep_in_progress;
/*
@@ -1029,6 +1034,7 @@ STATIC int
inodedep_lookup(struct fs *fs, ufsino_t inum, int flags,
struct inodedep **inodedeppp)
{
+ SIPHASH_CTX ctx;
struct inodedep *inodedep;
struct inodedep_hashhead *inodedephd;
int firsttry;
@@ -1040,7 +1046,10 @@ inodedep_lookup(struct fs *fs, ufsino_t
panic("inodedep_lookup: lock not held");
#endif
firsttry = 1;
- inodedephd = INODEDEP_HASH(fs, inum);
+ SipHash24_Init(&ctx, &softdep_hashkey);
+ SipHash24_Update(&ctx, &fs, sizeof(fs));
+ SipHash24_Update(&ctx, &inum, sizeof(inum));
+ inodedephd = &inodedep_hashtbl[SipHash24_End(&ctx) & inodedep_hash];
top:
LIST_FOREACH(inodedep, inodedephd, id_hash)
if (inum == inodedep->id_ino && fs == inodedep->id_fs)
@@ -1092,8 +1101,6 @@ top:
*/
LIST_HEAD(newblk_hashhead, newblk) *newblk_hashtbl;
u_long newblk_hash; /* size of hash table - 1 */
-#define NEWBLK_HASH(fs, inum) \
- (&newblk_hashtbl[((((register_t)(fs)) >> 13) + (inum)) & newblk_hash])
STATIC struct sema newblk_in_progress;
/*
@@ -1105,10 +1112,14 @@ STATIC int
newblk_lookup(struct fs *fs, daddr_t newblkno, int flags,
struct newblk **newblkpp)
{
+ SIPHASH_CTX ctx;
struct newblk *newblk;
struct newblk_hashhead *newblkhd;
- newblkhd = NEWBLK_HASH(fs, newblkno);
+ SipHash24_Init(&ctx, &softdep_hashkey);
+ SipHash24_Update(&ctx, &fs, sizeof(fs));
+ SipHash24_Update(&ctx, &newblkno, sizeof(newblkno));
+ newblkhd = &newblk_hashtbl[SipHash24_End(&ctx) & newblk_hash];
top:
LIST_FOREACH(newblk, newblkhd, nb_hash)
if (newblkno == newblk->nb_newblkno && fs == newblk->nb_fs)
@@ -1155,6 +1166,7 @@ softdep_initialize(void)
#else
max_softdeps = desiredvnodes * 4;
#endif
+ arc4random_buf(&softdep_hashkey, sizeof(softdep_hashkey));
pagedep_hashtbl = hashinit(desiredvnodes / 5, M_PAGEDEP, M_WAITOK,
&pagedep_hash);
sema_init(&pagedep_in_progress, "pagedep", PRIBIO, 0);
Index: ufs/ufs/ufs_quota.c
===================================================================
RCS file: /cvs/src/sys/ufs/ufs/ufs_quota.c,v
retrieving revision 1.35
diff -u -p -r1.35 ufs_quota.c
--- ufs/ufs/ufs_quota.c 13 Oct 2014 03:46:33 -0000 1.35
+++ ufs/ufs/ufs_quota.c 18 Nov 2014 01:03:13 -0000
@@ -53,6 +53,9 @@
#include <sys/queue.h>
+#include <dev/rndvar.h>
+#include <crypto/siphash.h>
+
/*
* The following structure records disk usage for a user or group on a
* filesystem. There is one allocated for each quota that exists on any
@@ -805,9 +808,8 @@ qsync(struct mount *mp)
/*
* Code pertaining to management of the in-core dquot data structures.
*/
-#define DQHASH(dqvp, id) \
- (&dqhashtbl[((((long)(dqvp)) >> 8) + id) & dqhash])
LIST_HEAD(dqhash, dquot) *dqhashtbl;
+SIPHASH_KEY dqhashkey;
u_long dqhash;
/*
@@ -824,6 +826,7 @@ void
ufs_quota_init(void)
{
dqhashtbl = hashinit(desiredvnodes, M_DQUOT, M_WAITOK, &dqhash);
+ arc4random_buf(&dqhashkey, sizeof(dqhashkey));
TAILQ_INIT(&dqfreelist);
}
@@ -835,6 +838,7 @@ int
dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
struct dquot **dqp)
{
+ SIPHASH_CTX ctx;
struct proc *p = curproc;
struct dquot *dq;
struct dqhash *dqh;
@@ -851,7 +855,11 @@ dqget(struct vnode *vp, u_long id, struc
/*
* Check the cache first.
*/
- dqh = DQHASH(dqvp, id);
+ SipHash24_Init(&ctx, &dqhashkey);
+ SipHash24_Update(&ctx, &dqvp, sizeof(dqvp));
+ SipHash24_Update(&ctx, &id, sizeof(id));
+ dqh = &dqhashtbl[SipHash24_End(&ctx) & dqhash];
+
LIST_FOREACH(dq, dqh, dq_hash) {
if (dq->dq_id != id ||
dq->dq_vp != dqvp)