Skip to content

Commit

Permalink
vfs: make the string hashes salt the hash
Browse files Browse the repository at this point in the history
We always mixed in the parent pointer into the dentry name hash, but we
did it late at lookup time.  It turns out that we can simplify that
lookup-time action by salting the hash with the parent pointer early
instead of late.

A few other users of our string hashes also wanted to mix in their own
pointers into the hash, and those are updated to use the same mechanism.

Hash users that don't have any particular initial salt can just use the
NULL pointer as a no-salt.

Cc: Vegard Nossum <vegard.nossum@oracle.com>
Cc: George Spelvin <linux@sciencehorizons.net>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
  • Loading branch information
Linus Torvalds committed Jun 11, 2016
1 parent 147d9e7 commit 8387ff2
Showing 38 changed files with 99 additions and 89 deletions.
8 changes: 5 additions & 3 deletions drivers/staging/lustre/lustre/llite/statahead.c
Original file line number Diff line number Diff line change
@@ -174,7 +174,8 @@ static inline int is_omitted_entry(struct ll_statahead_info *sai, __u64 index)
* Insert it into sai_entries tail when init.
*/
static struct ll_sa_entry *
ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
ll_sa_entry_alloc(struct dentry *parent,
struct ll_statahead_info *sai, __u64 index,
const char *name, int len)
{
struct ll_inode_info *lli;
@@ -221,7 +222,8 @@ ll_sa_entry_alloc(struct ll_statahead_info *sai, __u64 index,
dname = (char *)entry + sizeof(struct ll_sa_entry);
memcpy(dname, name, len);
dname[len] = 0;
entry->se_qstr.hash = full_name_hash(name, len);

entry->se_qstr.hash = full_name_hash(parent, name, len);
entry->se_qstr.len = len;
entry->se_qstr.name = dname;

@@ -902,7 +904,7 @@ static void ll_statahead_one(struct dentry *parent, const char *entry_name,
int rc;
int rc1;

entry = ll_sa_entry_alloc(sai, sai->sai_index, entry_name,
entry = ll_sa_entry_alloc(parent, sai, sai->sai_index, entry_name,
entry_name_len);
if (IS_ERR(entry))
return;
2 changes: 1 addition & 1 deletion fs/adfs/dir.c
Original file line number Diff line number Diff line change
@@ -207,7 +207,7 @@ adfs_hash(const struct dentry *parent, struct qstr *qstr)
*/
qstr->len = i = name_len;
name = qstr->name;
hash = init_name_hash();
hash = init_name_hash(parent);
while (i--) {
char c;

8 changes: 4 additions & 4 deletions fs/affs/namei.c
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ affs_get_toupper(struct super_block *sb)
* Note: the dentry argument is the parent dentry.
*/
static inline int
__affs_hash_dentry(struct qstr *qstr, toupper_t toupper, bool notruncate)
__affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr, toupper_t toupper, bool notruncate)
{
const u8 *name = qstr->name;
unsigned long hash;
@@ -72,7 +72,7 @@ __affs_hash_dentry(struct qstr *qstr, toupper_t toupper, bool notruncate)
if (retval)
return retval;

hash = init_name_hash();
hash = init_name_hash(dentry);
len = min(qstr->len, AFFSNAMEMAX);
for (; len > 0; name++, len--)
hash = partial_name_hash(toupper(*name), hash);
@@ -84,15 +84,15 @@ __affs_hash_dentry(struct qstr *qstr, toupper_t toupper, bool notruncate)
static int
affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
{
return __affs_hash_dentry(qstr, affs_toupper,
return __affs_hash_dentry(dentry, qstr, affs_toupper,
affs_nofilenametruncate(dentry));

}

static int
affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
{
return __affs_hash_dentry(qstr, affs_intl_toupper,
return __affs_hash_dentry(dentry, qstr, affs_intl_toupper,
affs_nofilenametruncate(dentry));

}
2 changes: 1 addition & 1 deletion fs/autofs4/waitq.c
Original file line number Diff line number Diff line change
@@ -397,7 +397,7 @@ int autofs4_wait(struct autofs_sb_info *sbi,
}
}
qstr.name = name;
qstr.hash = full_name_hash(name, qstr.len);
qstr.hash = full_name_hash(dentry, name, qstr.len);

if (mutex_lock_interruptible(&sbi->wq_mutex)) {
kfree(qstr.name);
4 changes: 2 additions & 2 deletions fs/ceph/inode.c
Original file line number Diff line number Diff line change
@@ -1164,7 +1164,7 @@ int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req,

dname.name = rinfo->dname;
dname.len = rinfo->dname_len;
dname.hash = full_name_hash(dname.name, dname.len);
dname.hash = full_name_hash(parent, dname.name, dname.len);
vino.ino = le64_to_cpu(rinfo->targeti.in->ino);
vino.snap = le64_to_cpu(rinfo->targeti.in->snapid);
retry_lookup:
@@ -1508,7 +1508,7 @@ int ceph_readdir_prepopulate(struct ceph_mds_request *req,

dname.name = rde->name;
dname.len = rde->name_len;
dname.hash = full_name_hash(dname.name, dname.len);
dname.hash = full_name_hash(parent, dname.name, dname.len);

vino.ino = le64_to_cpu(rde->inode.in->ino);
vino.snap = le64_to_cpu(rde->inode.in->snapid);
2 changes: 1 addition & 1 deletion fs/ceph/mds_client.c
Original file line number Diff line number Diff line change
@@ -3204,7 +3204,7 @@ static void handle_lease(struct ceph_mds_client *mdsc,
WARN_ON(1);
goto release; /* hrm... */
}
dname.hash = full_name_hash(dname.name, dname.len);
dname.hash = full_name_hash(parent, dname.name, dname.len);
dentry = d_lookup(parent, &dname);
dput(parent);
if (!dentry)
2 changes: 1 addition & 1 deletion fs/cifs/dir.c
Original file line number Diff line number Diff line change
@@ -856,7 +856,7 @@ static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
wchar_t c;
int i, charlen;

hash = init_name_hash();
hash = init_name_hash(dentry);
for (i = 0; i < q->len; i += charlen) {
charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
/* error out if we can't convert the character */
21 changes: 9 additions & 12 deletions fs/dcache.c
Original file line number Diff line number Diff line change
@@ -104,10 +104,8 @@ static unsigned int d_hash_shift __read_mostly;

static struct hlist_bl_head *dentry_hashtable __read_mostly;

static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
unsigned int hash)
static inline struct hlist_bl_head *d_hash(unsigned int hash)
{
hash += (unsigned long) parent / L1_CACHE_BYTES;
return dentry_hashtable + hash_32(hash, d_hash_shift);
}

@@ -488,7 +486,7 @@ void __d_drop(struct dentry *dentry)
if (unlikely(IS_ROOT(dentry)))
b = &dentry->d_sb->s_anon;
else
b = d_hash(dentry->d_parent, dentry->d_name.hash);
b = d_hash(dentry->d_name.hash);

hlist_bl_lock(b);
__hlist_bl_del(&dentry->d_hash);
@@ -1670,7 +1668,7 @@ struct dentry *d_alloc_name(struct dentry *parent, const char *name)
struct qstr q;

q.name = name;
q.hash_len = hashlen_string(name);
q.hash_len = hashlen_string(parent, name);
return d_alloc(parent, &q);
}
EXPORT_SYMBOL(d_alloc_name);
@@ -2094,7 +2092,7 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
{
u64 hashlen = name->hash_len;
const unsigned char *str = name->name;
struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
struct hlist_bl_node *node;
struct dentry *dentry;

@@ -2211,7 +2209,7 @@ struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
unsigned int len = name->len;
unsigned int hash = name->hash;
const unsigned char *str = name->name;
struct hlist_bl_head *b = d_hash(parent, hash);
struct hlist_bl_head *b = d_hash(hash);
struct hlist_bl_node *node;
struct dentry *found = NULL;
struct dentry *dentry;
@@ -2291,7 +2289,7 @@ struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
* calculate the standard hash first, as the d_op->d_hash()
* routine may choose to leave the hash value unchanged.
*/
name->hash = full_name_hash(name->name, name->len);
name->hash = full_name_hash(dir, name->name, name->len);
if (dir->d_flags & DCACHE_OP_HASH) {
int err = dir->d_op->d_hash(dir, name);
if (unlikely(err < 0))
@@ -2364,7 +2362,7 @@ static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)

static void _d_rehash(struct dentry * entry)
{
__d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
__d_rehash(entry, d_hash(entry->d_name.hash));
}

/**
@@ -2821,16 +2819,15 @@ static void __d_move(struct dentry *dentry, struct dentry *target,
* for the same hash queue because of how unlikely it is.
*/
__d_drop(dentry);
__d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
__d_rehash(dentry, d_hash(target->d_name.hash));

/*
* Unhash the target (d_delete() is not usable here). If exchanging
* the two dentries, then rehash onto the other's hash queue.
*/
__d_drop(target);
if (exchange) {
__d_rehash(target,
d_hash(dentry->d_parent, dentry->d_name.hash));
__d_rehash(target, d_hash(dentry->d_name.hash));
}

/* Switch the names.. */
4 changes: 2 additions & 2 deletions fs/efivarfs/super.c
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@ static int efivarfs_d_compare(const struct dentry *parent,

static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
{
unsigned long hash = init_name_hash();
unsigned long hash = init_name_hash(dentry);
const unsigned char *s = qstr->name;
unsigned int len = qstr->len;

@@ -98,7 +98,7 @@ static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
q.name = name;
q.len = strlen(name);

err = efivarfs_d_hash(NULL, &q);
err = efivarfs_d_hash(parent, &q);
if (err)
return ERR_PTR(err);

2 changes: 1 addition & 1 deletion fs/fat/namei_msdos.c
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ static int msdos_hash(const struct dentry *dentry, struct qstr *qstr)

error = msdos_format_name(qstr->name, qstr->len, msdos_name, options);
if (!error)
qstr->hash = full_name_hash(msdos_name, MSDOS_NAME);
qstr->hash = full_name_hash(dentry, msdos_name, MSDOS_NAME);
return 0;
}

4 changes: 2 additions & 2 deletions fs/fat/namei_vfat.c
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ static unsigned int vfat_striptail_len(const struct qstr *qstr)
*/
static int vfat_hash(const struct dentry *dentry, struct qstr *qstr)
{
qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
qstr->hash = full_name_hash(dentry, qstr->name, vfat_striptail_len(qstr));
return 0;
}

@@ -127,7 +127,7 @@ static int vfat_hashi(const struct dentry *dentry, struct qstr *qstr)
name = qstr->name;
len = vfat_striptail_len(qstr);

hash = init_name_hash();
hash = init_name_hash(dentry);
while (len--)
hash = partial_name_hash(nls_tolower(t, *name++), hash);
qstr->hash = end_name_hash(hash);
2 changes: 0 additions & 2 deletions fs/fuse/dev.c
Original file line number Diff line number Diff line change
@@ -1525,7 +1525,6 @@ static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
goto err;
fuse_copy_finish(cs);
buf[outarg.namelen] = 0;
name.hash = full_name_hash(name.name, name.len);

down_read(&fc->killsb);
err = -ENOENT;
@@ -1576,7 +1575,6 @@ static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
goto err;
fuse_copy_finish(cs);
buf[outarg.namelen] = 0;
name.hash = full_name_hash(name.name, name.len);

down_read(&fc->killsb);
err = -ENOENT;
3 changes: 2 additions & 1 deletion fs/fuse/dir.c
Original file line number Diff line number Diff line change
@@ -953,6 +953,7 @@ int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
if (!dir)
goto unlock;

name->hash = full_name_hash(dir, name->name, name->len);
entry = d_lookup(dir, name);
dput(dir);
if (!entry)
@@ -1202,7 +1203,7 @@ static int fuse_direntplus_link(struct file *file,

fc = get_fuse_conn(dir);

name.hash = full_name_hash(name.name, name.len);
name.hash = full_name_hash(parent, name.name, name.len);
dentry = d_lookup(parent, &name);
if (!dentry) {
retry:
2 changes: 1 addition & 1 deletion fs/hfs/string.c
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ int hfs_hash_dentry(const struct dentry *dentry, struct qstr *this)
if (len > HFS_NAMELEN)
len = HFS_NAMELEN;

hash = init_name_hash();
hash = init_name_hash(dentry);
for (; len; len--)
hash = partial_name_hash(caseorder[*name++], hash);
this->hash = end_name_hash(hash);
2 changes: 1 addition & 1 deletion fs/hfsplus/unicode.c
Original file line number Diff line number Diff line change
@@ -346,7 +346,7 @@ int hfsplus_hash_dentry(const struct dentry *dentry, struct qstr *str)

casefold = test_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
decompose = !test_bit(HFSPLUS_SB_NODECOMPOSE, &HFSPLUS_SB(sb)->flags);
hash = init_name_hash();
hash = init_name_hash(dentry);
astr = str->name;
len = str->len;
while (len > 0) {
2 changes: 1 addition & 1 deletion fs/hpfs/dentry.c
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ static int hpfs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
/*return -ENOENT;*/
x:

hash = init_name_hash();
hash = init_name_hash(dentry);
for (i = 0; i < l; i++)
hash = partial_name_hash(hpfs_upcase(hpfs_sb(dentry->d_sb)->sb_cp_table,qstr->name[i]), hash);
qstr->hash = end_name_hash(hash);
14 changes: 7 additions & 7 deletions fs/isofs/inode.c
Original file line number Diff line number Diff line change
@@ -174,7 +174,7 @@ struct iso9660_options{
* Compute the hash for the isofs name corresponding to the dentry.
*/
static int
isofs_hashi_common(struct qstr *qstr, int ms)
isofs_hashi_common(const struct dentry *dentry, struct qstr *qstr, int ms)
{
const char *name;
int len;
@@ -188,7 +188,7 @@ isofs_hashi_common(struct qstr *qstr, int ms)
len--;
}

hash = init_name_hash();
hash = init_name_hash(dentry);
while (len--) {
c = tolower(*name++);
hash = partial_name_hash(c, hash);
@@ -231,7 +231,7 @@ static int isofs_dentry_cmp_common(
static int
isofs_hashi(const struct dentry *dentry, struct qstr *qstr)
{
return isofs_hashi_common(qstr, 0);
return isofs_hashi_common(dentry, qstr, 0);
}

static int
@@ -246,7 +246,7 @@ isofs_dentry_cmpi(const struct dentry *parent, const struct dentry *dentry,
* Compute the hash for the isofs name corresponding to the dentry.
*/
static int
isofs_hash_common(struct qstr *qstr, int ms)
isofs_hash_common(const struct dentry *dentry, struct qstr *qstr, int ms)
{
const char *name;
int len;
@@ -258,21 +258,21 @@ isofs_hash_common(struct qstr *qstr, int ms)
len--;
}

qstr->hash = full_name_hash(name, len);
qstr->hash = full_name_hash(dentry, name, len);

return 0;
}

static int
isofs_hash_ms(const struct dentry *dentry, struct qstr *qstr)
{
return isofs_hash_common(qstr, 1);
return isofs_hash_common(dentry, qstr, 1);
}

static int
isofs_hashi_ms(const struct dentry *dentry, struct qstr *qstr)
{
return isofs_hashi_common(qstr, 1);
return isofs_hashi_common(dentry, qstr, 1);
}

static int
8 changes: 6 additions & 2 deletions fs/jffs2/dir.c
Original file line number Diff line number Diff line change
@@ -81,6 +81,7 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
struct jffs2_full_dirent *fd = NULL, *fd_list;
uint32_t ino = 0;
struct inode *inode = NULL;
unsigned int nhash;

jffs2_dbg(1, "jffs2_lookup()\n");

@@ -89,11 +90,14 @@ static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,

dir_f = JFFS2_INODE_INFO(dir_i);

/* The 'nhash' on the fd_list is not the same as the dentry hash */
nhash = full_name_hash(NULL, target->d_name.name, target->d_name.len);

mutex_lock(&dir_f->sem);

/* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
if (fd_list->nhash == target->d_name.hash &&
for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= nhash; fd_list = fd_list->next) {
if (fd_list->nhash == nhash &&
(!fd || fd_list->version > fd->version) &&
strlen(fd_list->name) == target->d_name.len &&
!strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
Loading

0 comments on commit 8387ff2

Please sign in to comment.