Skip to content

Commit d41efb5

Browse files
author
Al Viro
committed
fs/namei.c: pull positivity check into follow_managed()
There are 4 callers; two proceed to check if result is positive and fail with ENOENT if it isn't; one (in handle_lookup_down()) is guaranteed to yield positive and one (in lookup_fast()) is _preceded_ by positivity check. However, follow_managed() on a negative dentry is a (fairly cheap) no-op on anything other than autofs. And negative autofs dentries are never hashed, so lookup_fast() is not going to run into one of those. Moreover, successful follow_managed() on a _positive_ dentry never yields a negative one (and we significantly rely upon that in callers of lookup_fast()). In other words, we can easily transpose the positivity check and the call of follow_managed() in lookup_fast(). And that allows to fold the positivity check *into* follow_managed(), simplifying life for the code downstream of its calls. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 3e5aeec commit d41efb5

2 files changed

Lines changed: 16 additions & 23 deletions

File tree

‎fs/namei.c‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,25 +1206,25 @@ static int follow_automount(struct path *path, struct nameidata *nd,
12061206
* - Flagged as automount point
12071207
*
12081208
* This may only be called in refwalk mode.
1209+
* On success path->dentry is known positive.
12091210
*
12101211
* Serialization is taken care of in namespace.c
12111212
*/
12121213
static int follow_managed(struct path *path, struct nameidata *nd)
12131214
{
12141215
struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1215-
unsigned managed;
1216+
unsigned flags;
12161217
bool need_mntput = false;
12171218
int ret = 0;
12181219

12191220
/* Given that we're not holding a lock here, we retain the value in a
12201221
* local variable for each dentry as we look at it so that we don't see
12211222
* the components of that value change under us */
1222-
while (managed = READ_ONCE(path->dentry->d_flags),
1223-
managed &= DCACHE_MANAGED_DENTRY,
1224-
unlikely(managed != 0)) {
1223+
while (flags = READ_ONCE(path->dentry->d_flags),
1224+
unlikely(flags & DCACHE_MANAGED_DENTRY)) {
12251225
/* Allow the filesystem to manage the transit without i_mutex
12261226
* being held. */
1227-
if (managed & DCACHE_MANAGE_TRANSIT) {
1227+
if (flags & DCACHE_MANAGE_TRANSIT) {
12281228
BUG_ON(!path->dentry->d_op);
12291229
BUG_ON(!path->dentry->d_op->d_manage);
12301230
ret = path->dentry->d_op->d_manage(path, false);
@@ -1233,7 +1233,7 @@ static int follow_managed(struct path *path, struct nameidata *nd)
12331233
}
12341234

12351235
/* Transit to a mounted filesystem. */
1236-
if (managed & DCACHE_MOUNTED) {
1236+
if (flags & DCACHE_MOUNTED) {
12371237
struct vfsmount *mounted = lookup_mnt(path);
12381238
if (mounted) {
12391239
dput(path->dentry);
@@ -1252,7 +1252,7 @@ static int follow_managed(struct path *path, struct nameidata *nd)
12521252
}
12531253

12541254
/* Handle an automount point */
1255-
if (managed & DCACHE_NEED_AUTOMOUNT) {
1255+
if (flags & DCACHE_NEED_AUTOMOUNT) {
12561256
ret = follow_automount(path, nd, &need_mntput);
12571257
if (ret < 0)
12581258
break;
@@ -1265,10 +1265,12 @@ static int follow_managed(struct path *path, struct nameidata *nd)
12651265

12661266
if (need_mntput && path->mnt == mnt)
12671267
mntput(path->mnt);
1268-
if (ret == -EISDIR || !ret)
1269-
ret = 1;
12701268
if (need_mntput)
12711269
nd->flags |= LOOKUP_JUMPED;
1270+
if (ret == -EISDIR || !ret)
1271+
ret = 1;
1272+
if (ret > 0 && unlikely(d_flags_negative(flags)))
1273+
ret = -ENOENT;
12721274
if (unlikely(ret < 0))
12731275
path_put_conditional(path, nd);
12741276
return ret;
@@ -1617,10 +1619,6 @@ static int lookup_fast(struct nameidata *nd,
16171619
dput(dentry);
16181620
return status;
16191621
}
1620-
if (unlikely(d_is_negative(dentry))) {
1621-
dput(dentry);
1622-
return -ENOENT;
1623-
}
16241622

16251623
path->mnt = mnt;
16261624
path->dentry = dentry;
@@ -1807,11 +1805,6 @@ static int walk_component(struct nameidata *nd, int flags)
18071805
if (unlikely(err < 0))
18081806
return err;
18091807

1810-
if (unlikely(d_is_negative(path.dentry))) {
1811-
path_to_nameidata(&path, nd);
1812-
return -ENOENT;
1813-
}
1814-
18151808
seq = 0; /* we are already out of RCU mode */
18161809
inode = d_backing_inode(path.dentry);
18171810
}
@@ -3352,11 +3345,6 @@ static int do_last(struct nameidata *nd,
33523345
if (unlikely(error < 0))
33533346
return error;
33543347

3355-
if (unlikely(d_is_negative(path.dentry))) {
3356-
path_to_nameidata(&path, nd);
3357-
return -ENOENT;
3358-
}
3359-
33603348
/*
33613349
* create/update audit record if it already exists.
33623350
*/

‎include/linux/dcache.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,11 @@ static inline bool d_is_negative(const struct dentry *dentry)
440440
return d_is_miss(dentry);
441441
}
442442

443+
static inline bool d_flags_negative(unsigned flags)
444+
{
445+
return (flags & DCACHE_ENTRY_TYPE) == DCACHE_MISS_TYPE;
446+
}
447+
443448
static inline bool d_is_positive(const struct dentry *dentry)
444449
{
445450
return !d_is_negative(dentry);

0 commit comments

Comments
 (0)