Skip to content

Commit da53be1

Browse files
torvaldsAl Viro
authored andcommitted
Don't pass inode to ->d_hash() and ->d_compare()
Instances either don't look at it at all (the majority of cases) or only want it to find the superblock (which can be had as dentry->d_sb). A few cases that want more are actually safe with dentry->d_inode - the only precaution needed is the check that it hadn't been replaced with NULL by rmdir() or by overwriting rename(), which case should be simply treated as cache miss. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 642b704 commit da53be1

23 files changed

Lines changed: 108 additions & 165 deletions

File tree

‎Documentation/filesystems/Locking‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ be able to use diff(1).
1111
prototypes:
1212
int (*d_revalidate)(struct dentry *, unsigned int);
1313
int (*d_weak_revalidate)(struct dentry *, unsigned int);
14-
int (*d_hash)(const struct dentry *, const struct inode *,
15-
struct qstr *);
16-
int (*d_compare)(const struct dentry *, const struct inode *,
17-
const struct dentry *, const struct inode *,
14+
int (*d_hash)(const struct dentry *, struct qstr *);
15+
int (*d_compare)(const struct dentry *, const struct dentry *,
1816
unsigned int, const char *, const struct qstr *);
1917
int (*d_delete)(struct dentry *);
2018
void (*d_release)(struct dentry *);

‎Documentation/filesystems/vfs.txt‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -901,10 +901,8 @@ defined:
901901
struct dentry_operations {
902902
int (*d_revalidate)(struct dentry *, unsigned int);
903903
int (*d_weak_revalidate)(struct dentry *, unsigned int);
904-
int (*d_hash)(const struct dentry *, const struct inode *,
905-
struct qstr *);
906-
int (*d_compare)(const struct dentry *, const struct inode *,
907-
const struct dentry *, const struct inode *,
904+
int (*d_hash)(const struct dentry *, struct qstr *);
905+
int (*d_compare)(const struct dentry *, const struct dentry *,
908906
unsigned int, const char *, const struct qstr *);
909907
int (*d_delete)(const struct dentry *);
910908
void (*d_release)(struct dentry *);
@@ -949,25 +947,24 @@ struct dentry_operations {
949947

950948
d_hash: called when the VFS adds a dentry to the hash table. The first
951949
dentry passed to d_hash is the parent directory that the name is
952-
to be hashed into. The inode is the dentry's inode.
950+
to be hashed into.
953951

954952
Same locking and synchronisation rules as d_compare regarding
955953
what is safe to dereference etc.
956954

957955
d_compare: called to compare a dentry name with a given name. The first
958956
dentry is the parent of the dentry to be compared, the second is
959-
the parent's inode, then the dentry and inode (may be NULL) of the
960-
child dentry. len and name string are properties of the dentry to be
961-
compared. qstr is the name to compare it with.
957+
the child dentry. len and name string are properties of the dentry
958+
to be compared. qstr is the name to compare it with.
962959

963960
Must be constant and idempotent, and should not take locks if
964-
possible, and should not or store into the dentry or inodes.
965-
Should not dereference pointers outside the dentry or inodes without
961+
possible, and should not or store into the dentry.
962+
Should not dereference pointers outside the dentry without
966963
lots of care (eg. d_parent, d_inode, d_name should not be used).
967964

968965
However, our vfsmount is pinned, and RCU held, so the dentries and
969966
inodes won't disappear, neither will our sb or filesystem module.
970-
->i_sb and ->d_sb may be used.
967+
->d_sb may be used.
971968

972969
It is a tricky calling convention because it needs to be called under
973970
"rcu-walk", ie. without any locks or references on things.

‎fs/adfs/dir.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ const struct file_operations adfs_dir_operations = {
191191
};
192192

193193
static int
194-
adfs_hash(const struct dentry *parent, const struct inode *inode,
195-
struct qstr *qstr)
194+
adfs_hash(const struct dentry *parent, struct qstr *qstr)
196195
{
197196
const unsigned int name_len = ADFS_SB(parent->d_sb)->s_namelen;
198197
const unsigned char *name;
@@ -228,8 +227,7 @@ adfs_hash(const struct dentry *parent, const struct inode *inode,
228227
* requirements of the underlying filesystem.
229228
*/
230229
static int
231-
adfs_compare(const struct dentry *parent, const struct inode *pinode,
232-
const struct dentry *dentry, const struct inode *inode,
230+
adfs_compare(const struct dentry *parent, const struct dentry *dentry,
233231
unsigned int len, const char *str, const struct qstr *name)
234232
{
235233
int i;

‎fs/affs/namei.c‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313
typedef int (*toupper_t)(int);
1414

1515
static int affs_toupper(int ch);
16-
static int affs_hash_dentry(const struct dentry *,
17-
const struct inode *, struct qstr *);
18-
static int affs_compare_dentry(const struct dentry *parent,
19-
const struct inode *pinode,
20-
const struct dentry *dentry, const struct inode *inode,
16+
static int affs_hash_dentry(const struct dentry *, struct qstr *);
17+
static int affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
2118
unsigned int len, const char *str, const struct qstr *name);
2219
static int affs_intl_toupper(int ch);
23-
static int affs_intl_hash_dentry(const struct dentry *,
24-
const struct inode *, struct qstr *);
25-
static int affs_intl_compare_dentry(const struct dentry *parent,
26-
const struct inode *pinode,
27-
const struct dentry *dentry, const struct inode *inode,
20+
static int affs_intl_hash_dentry(const struct dentry *, struct qstr *);
21+
static int affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
2822
unsigned int len, const char *str, const struct qstr *name);
2923

3024
const struct dentry_operations affs_dentry_operations = {
@@ -86,14 +80,12 @@ __affs_hash_dentry(struct qstr *qstr, toupper_t toupper)
8680
}
8781

8882
static int
89-
affs_hash_dentry(const struct dentry *dentry, const struct inode *inode,
90-
struct qstr *qstr)
83+
affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
9184
{
9285
return __affs_hash_dentry(qstr, affs_toupper);
9386
}
9487
static int
95-
affs_intl_hash_dentry(const struct dentry *dentry, const struct inode *inode,
96-
struct qstr *qstr)
88+
affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr)
9789
{
9890
return __affs_hash_dentry(qstr, affs_intl_toupper);
9991
}
@@ -131,15 +123,13 @@ static inline int __affs_compare_dentry(unsigned int len,
131123
}
132124

133125
static int
134-
affs_compare_dentry(const struct dentry *parent, const struct inode *pinode,
135-
const struct dentry *dentry, const struct inode *inode,
126+
affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
136127
unsigned int len, const char *str, const struct qstr *name)
137128
{
138129
return __affs_compare_dentry(len, str, name, affs_toupper);
139130
}
140131
static int
141-
affs_intl_compare_dentry(const struct dentry *parent,const struct inode *pinode,
142-
const struct dentry *dentry, const struct inode *inode,
132+
affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry,
143133
unsigned int len, const char *str, const struct qstr *name)
144134
{
145135
return __affs_compare_dentry(len, str, name, affs_intl_toupper);

‎fs/cifs/dir.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,7 @@ const struct dentry_operations cifs_dentry_ops = {
822822
/* d_delete: cifs_d_delete, */ /* not needed except for debugging */
823823
};
824824

825-
static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
826-
struct qstr *q)
825+
static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
827826
{
828827
struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
829828
unsigned long hash;
@@ -838,12 +837,10 @@ static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
838837
return 0;
839838
}
840839

841-
static int cifs_ci_compare(const struct dentry *parent,
842-
const struct inode *pinode,
843-
const struct dentry *dentry, const struct inode *inode,
840+
static int cifs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
844841
unsigned int len, const char *str, const struct qstr *name)
845842
{
846-
struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls;
843+
struct nls_table *codepage = CIFS_SB(parent->d_sb)->local_nls;
847844

848845
if ((name->len == len) &&
849846
(nls_strnicmp(codepage, name->name, str, len) == 0))

‎fs/dcache.c‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ EXPORT_SYMBOL(d_add_ci);
17231723
* Do the slow-case of the dentry name compare.
17241724
*
17251725
* Unlike the dentry_cmp() function, we need to atomically
1726-
* load the name, length and inode information, so that the
1726+
* load the name and length information, so that the
17271727
* filesystem can rely on them, and can use the 'name' and
17281728
* 'len' information without worrying about walking off the
17291729
* end of memory etc.
@@ -1741,22 +1741,18 @@ enum slow_d_compare {
17411741

17421742
static noinline enum slow_d_compare slow_dentry_cmp(
17431743
const struct dentry *parent,
1744-
struct inode *inode,
17451744
struct dentry *dentry,
17461745
unsigned int seq,
17471746
const struct qstr *name)
17481747
{
17491748
int tlen = dentry->d_name.len;
17501749
const char *tname = dentry->d_name.name;
1751-
struct inode *i = dentry->d_inode;
17521750

17531751
if (read_seqcount_retry(&dentry->d_seq, seq)) {
17541752
cpu_relax();
17551753
return D_COMP_SEQRETRY;
17561754
}
1757-
if (parent->d_op->d_compare(parent, inode,
1758-
dentry, i,
1759-
tlen, tname, name))
1755+
if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
17601756
return D_COMP_NOMATCH;
17611757
return D_COMP_OK;
17621758
}
@@ -1766,7 +1762,6 @@ static noinline enum slow_d_compare slow_dentry_cmp(
17661762
* @parent: parent dentry
17671763
* @name: qstr of name we wish to find
17681764
* @seqp: returns d_seq value at the point where the dentry was found
1769-
* @inode: returns dentry->d_inode when the inode was found valid.
17701765
* Returns: dentry, or NULL
17711766
*
17721767
* __d_lookup_rcu is the dcache lookup function for rcu-walk name
@@ -1793,7 +1788,7 @@ static noinline enum slow_d_compare slow_dentry_cmp(
17931788
*/
17941789
struct dentry *__d_lookup_rcu(const struct dentry *parent,
17951790
const struct qstr *name,
1796-
unsigned *seqp, struct inode *inode)
1791+
unsigned *seqp)
17971792
{
17981793
u64 hashlen = name->hash_len;
17991794
const unsigned char *str = name->name;
@@ -1827,11 +1822,10 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
18271822
seqretry:
18281823
/*
18291824
* The dentry sequence count protects us from concurrent
1830-
* renames, and thus protects inode, parent and name fields.
1825+
* renames, and thus protects parent and name fields.
18311826
*
18321827
* The caller must perform a seqcount check in order
1833-
* to do anything useful with the returned dentry,
1834-
* including using the 'd_inode' pointer.
1828+
* to do anything useful with the returned dentry.
18351829
*
18361830
* NOTE! We do a "raw" seqcount_begin here. That means that
18371831
* we don't wait for the sequence count to stabilize if it
@@ -1845,12 +1839,12 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
18451839
continue;
18461840
if (d_unhashed(dentry))
18471841
continue;
1848-
*seqp = seq;
18491842

18501843
if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
18511844
if (dentry->d_name.hash != hashlen_hash(hashlen))
18521845
continue;
1853-
switch (slow_dentry_cmp(parent, inode, dentry, seq, name)) {
1846+
*seqp = seq;
1847+
switch (slow_dentry_cmp(parent, dentry, seq, name)) {
18541848
case D_COMP_OK:
18551849
return dentry;
18561850
case D_COMP_NOMATCH:
@@ -1862,6 +1856,7 @@ struct dentry *__d_lookup_rcu(const struct dentry *parent,
18621856

18631857
if (dentry->d_name.hash_len != hashlen)
18641858
continue;
1859+
*seqp = seq;
18651860
if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
18661861
return dentry;
18671862
}
@@ -1959,9 +1954,7 @@ struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
19591954
if (parent->d_flags & DCACHE_OP_COMPARE) {
19601955
int tlen = dentry->d_name.len;
19611956
const char *tname = dentry->d_name.name;
1962-
if (parent->d_op->d_compare(parent, parent->d_inode,
1963-
dentry, dentry->d_inode,
1964-
tlen, tname, name))
1957+
if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
19651958
goto next;
19661959
} else {
19671960
if (dentry->d_name.len != len)
@@ -1998,7 +1991,7 @@ struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
19981991
*/
19991992
name->hash = full_name_hash(name->name, name->len);
20001993
if (dir->d_flags & DCACHE_OP_HASH) {
2001-
int err = dir->d_op->d_hash(dir, dir->d_inode, name);
1994+
int err = dir->d_op->d_hash(dir, name);
20021995
if (unlikely(err < 0))
20031996
return ERR_PTR(err);
20041997
}

‎fs/efivarfs/super.c‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static struct super_block *efivarfs_sb;
4545
* So we need to perform a case-sensitive match on part 1 and a
4646
* case-insensitive match on part 2.
4747
*/
48-
static int efivarfs_d_compare(const struct dentry *parent, const struct inode *pinode,
49-
const struct dentry *dentry, const struct inode *inode,
48+
static int efivarfs_d_compare(const struct dentry *parent,
49+
const struct dentry *dentry,
5050
unsigned int len, const char *str,
5151
const struct qstr *name)
5252
{
@@ -63,8 +63,7 @@ static int efivarfs_d_compare(const struct dentry *parent, const struct inode *p
6363
return strncasecmp(name->name + guid, str + guid, EFI_VARIABLE_GUID_LEN);
6464
}
6565

66-
static int efivarfs_d_hash(const struct dentry *dentry,
67-
const struct inode *inode, struct qstr *qstr)
66+
static int efivarfs_d_hash(const struct dentry *dentry, struct qstr *qstr)
6867
{
6968
unsigned long hash = init_name_hash();
7069
const unsigned char *s = qstr->name;
@@ -108,7 +107,7 @@ static struct dentry *efivarfs_alloc_dentry(struct dentry *parent, char *name)
108107
q.name = name;
109108
q.len = strlen(name);
110109

111-
err = efivarfs_d_hash(NULL, NULL, &q);
110+
err = efivarfs_d_hash(NULL, &q);
112111
if (err)
113112
return ERR_PTR(err);
114113

‎fs/fat/namei_msdos.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ static int msdos_find(struct inode *dir, const unsigned char *name, int len,
148148
* that the existing dentry can be used. The msdos fs routines will
149149
* return ENOENT or EINVAL as appropriate.
150150
*/
151-
static int msdos_hash(const struct dentry *dentry, const struct inode *inode,
152-
struct qstr *qstr)
151+
static int msdos_hash(const struct dentry *dentry, struct qstr *qstr)
153152
{
154153
struct fat_mount_options *options = &MSDOS_SB(dentry->d_sb)->options;
155154
unsigned char msdos_name[MSDOS_NAME];
@@ -165,8 +164,7 @@ static int msdos_hash(const struct dentry *dentry, const struct inode *inode,
165164
* Compare two msdos names. If either of the names are invalid,
166165
* we fall back to doing the standard name comparison.
167166
*/
168-
static int msdos_cmp(const struct dentry *parent, const struct inode *pinode,
169-
const struct dentry *dentry, const struct inode *inode,
167+
static int msdos_cmp(const struct dentry *parent, const struct dentry *dentry,
170168
unsigned int len, const char *str, const struct qstr *name)
171169
{
172170
struct fat_mount_options *options = &MSDOS_SB(parent->d_sb)->options;

‎fs/fat/namei_vfat.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ static unsigned int vfat_striptail_len(const struct qstr *qstr)
107107
* that the existing dentry can be used. The vfat fs routines will
108108
* return ENOENT or EINVAL as appropriate.
109109
*/
110-
static int vfat_hash(const struct dentry *dentry, const struct inode *inode,
111-
struct qstr *qstr)
110+
static int vfat_hash(const struct dentry *dentry, struct qstr *qstr)
112111
{
113112
qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
114113
return 0;
@@ -120,8 +119,7 @@ static int vfat_hash(const struct dentry *dentry, const struct inode *inode,
120119
* that the existing dentry can be used. The vfat fs routines will
121120
* return ENOENT or EINVAL as appropriate.
122121
*/
123-
static int vfat_hashi(const struct dentry *dentry, const struct inode *inode,
124-
struct qstr *qstr)
122+
static int vfat_hashi(const struct dentry *dentry, struct qstr *qstr)
125123
{
126124
struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io;
127125
const unsigned char *name;
@@ -142,8 +140,7 @@ static int vfat_hashi(const struct dentry *dentry, const struct inode *inode,
142140
/*
143141
* Case insensitive compare of two vfat names.
144142
*/
145-
static int vfat_cmpi(const struct dentry *parent, const struct inode *pinode,
146-
const struct dentry *dentry, const struct inode *inode,
143+
static int vfat_cmpi(const struct dentry *parent, const struct dentry *dentry,
147144
unsigned int len, const char *str, const struct qstr *name)
148145
{
149146
struct nls_table *t = MSDOS_SB(parent->d_sb)->nls_io;
@@ -162,8 +159,7 @@ static int vfat_cmpi(const struct dentry *parent, const struct inode *pinode,
162159
/*
163160
* Case sensitive compare of two vfat names.
164161
*/
165-
static int vfat_cmp(const struct dentry *parent, const struct inode *pinode,
166-
const struct dentry *dentry, const struct inode *inode,
162+
static int vfat_cmp(const struct dentry *parent, const struct dentry *dentry,
167163
unsigned int len, const char *str, const struct qstr *name)
168164
{
169165
unsigned int alen, blen;

‎fs/gfs2/dentry.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ static int gfs2_drevalidate(struct dentry *dentry, unsigned int flags)
109109
return 0;
110110
}
111111

112-
static int gfs2_dhash(const struct dentry *dentry, const struct inode *inode,
113-
struct qstr *str)
112+
static int gfs2_dhash(const struct dentry *dentry, struct qstr *str)
114113
{
115114
str->hash = gfs2_disk_hash(str->name, str->len);
116115
return 0;

0 commit comments

Comments
 (0)