Skip to content

Commit 6c2d479

Browse files
author
Al Viro
committed
new helper: lookup_positive_unlocked()
Most of the callers of lookup_one_len_unlocked() treat negatives are ERR_PTR(-ENOENT). Provide a helper that would do just that. Note that a pinned positive dentry remains positive - it's ->d_inode is stable, etc.; a pinned _negative_ dentry can become positive at any point as long as you are not holding its parent at least shared. So using lookup_one_len_unlocked() needs to be careful; lookup_positive_unlocked() is safer and that's what the callers end up open-coding anyway. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent d41efb5 commit 6c2d479

9 files changed

Lines changed: 35 additions & 47 deletions

File tree

‎fs/cifs/cifsfs.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -719,11 +719,6 @@ cifs_get_root(struct smb_vol *vol, struct super_block *sb)
719719
struct inode *dir = d_inode(dentry);
720720
struct dentry *child;
721721

722-
if (!dir) {
723-
dput(dentry);
724-
dentry = ERR_PTR(-ENOENT);
725-
break;
726-
}
727722
if (!S_ISDIR(dir->i_mode)) {
728723
dput(dentry);
729724
dentry = ERR_PTR(-ENOTDIR);
@@ -740,7 +735,7 @@ cifs_get_root(struct smb_vol *vol, struct super_block *sb)
740735
while (*s && *s != sep)
741736
s++;
742737

743-
child = lookup_one_len_unlocked(p, dentry, s - p);
738+
child = lookup_positive_unlocked(p, dentry, s - p);
744739
dput(dentry);
745740
dentry = child;
746741
} while (!IS_ERR(dentry));

‎fs/debugfs/inode.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,9 @@ struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
299299
if (!parent)
300300
parent = debugfs_mount->mnt_root;
301301

302-
dentry = lookup_one_len_unlocked(name, parent, strlen(name));
302+
dentry = lookup_positive_unlocked(name, parent, strlen(name));
303303
if (IS_ERR(dentry))
304304
return NULL;
305-
if (!d_really_is_positive(dentry)) {
306-
dput(dentry);
307-
return NULL;
308-
}
309305
return dentry;
310306
}
311307
EXPORT_SYMBOL_GPL(debugfs_lookup);

‎fs/kernfs/mount.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
200200
dput(dentry);
201201
return ERR_PTR(-EINVAL);
202202
}
203-
dtmp = lookup_one_len_unlocked(kntmp->name, dentry,
203+
dtmp = lookup_positive_unlocked(kntmp->name, dentry,
204204
strlen(kntmp->name));
205205
dput(dentry);
206206
if (IS_ERR(dtmp))

‎fs/namei.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,26 @@ struct dentry *lookup_one_len_unlocked(const char *name,
25572557
}
25582558
EXPORT_SYMBOL(lookup_one_len_unlocked);
25592559

2560+
/*
2561+
* Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2562+
* on negatives. Returns known positive or ERR_PTR(); that's what
2563+
* most of the users want. Note that pinned negative with unlocked parent
2564+
* _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2565+
* need to be very careful; pinned positives have ->d_inode stable, so
2566+
* this one avoids such problems.
2567+
*/
2568+
struct dentry *lookup_positive_unlocked(const char *name,
2569+
struct dentry *base, int len)
2570+
{
2571+
struct dentry *ret = lookup_one_len_unlocked(name, base, len);
2572+
if (!IS_ERR(ret) && d_is_negative(ret)) {
2573+
dput(ret);
2574+
ret = ERR_PTR(-ENOENT);
2575+
}
2576+
return ret;
2577+
}
2578+
EXPORT_SYMBOL(lookup_positive_unlocked);
2579+
25602580
#ifdef CONFIG_UNIX98_PTYS
25612581
int path_pts(struct path *path)
25622582
{

‎fs/nfsd/nfs3xdr.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,13 +863,11 @@ compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
863863
} else
864864
dchild = dget(dparent);
865865
} else
866-
dchild = lookup_one_len_unlocked(name, dparent, namlen);
866+
dchild = lookup_positive_unlocked(name, dparent, namlen);
867867
if (IS_ERR(dchild))
868868
return rv;
869869
if (d_mountpoint(dchild))
870870
goto out;
871-
if (d_really_is_negative(dchild))
872-
goto out;
873871
if (dchild->d_inode->i_ino != ino)
874872
goto out;
875873
rv = fh_compose(fhp, exp, dchild, &cd->fh);

‎fs/nfsd/nfs4xdr.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2991,18 +2991,9 @@ nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
29912991
__be32 nfserr;
29922992
int ignore_crossmnt = 0;
29932993

2994-
dentry = lookup_one_len_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
2994+
dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
29952995
if (IS_ERR(dentry))
29962996
return nfserrno(PTR_ERR(dentry));
2997-
if (d_really_is_negative(dentry)) {
2998-
/*
2999-
* we're not holding the i_mutex here, so there's
3000-
* a window where this directory entry could have gone
3001-
* away.
3002-
*/
3003-
dput(dentry);
3004-
return nfserr_noent;
3005-
}
30062997

30072998
exp_get(exp);
30082999
/*

‎fs/overlayfs/namei.c‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,14 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
200200
int err;
201201
bool last_element = !post[0];
202202

203-
this = lookup_one_len_unlocked(name, base, namelen);
203+
this = lookup_positive_unlocked(name, base, namelen);
204204
if (IS_ERR(this)) {
205205
err = PTR_ERR(this);
206206
this = NULL;
207207
if (err == -ENOENT || err == -ENAMETOOLONG)
208208
goto out;
209209
goto out_err;
210210
}
211-
if (!this->d_inode)
212-
goto put_and_out;
213211

214212
if (ovl_dentry_weird(this)) {
215213
/* Don't support traversing automounts and other weirdness */
@@ -651,17 +649,15 @@ struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
651649
if (err)
652650
return ERR_PTR(err);
653651

654-
index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
652+
index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
655653
kfree(name.name);
656654
if (IS_ERR(index)) {
657655
if (PTR_ERR(index) == -ENOENT)
658656
index = NULL;
659657
return index;
660658
}
661659

662-
if (d_is_negative(index))
663-
err = 0;
664-
else if (ovl_is_whiteout(index))
660+
if (ovl_is_whiteout(index))
665661
err = -ESTALE;
666662
else if (ovl_dentry_weird(index))
667663
err = -EIO;
@@ -685,7 +681,7 @@ struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
685681
if (err)
686682
return ERR_PTR(err);
687683

688-
index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
684+
index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
689685
if (IS_ERR(index)) {
690686
err = PTR_ERR(index);
691687
if (err == -ENOENT) {
@@ -700,9 +696,7 @@ struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
700696
}
701697

702698
inode = d_inode(index);
703-
if (d_is_negative(index)) {
704-
goto out_dput;
705-
} else if (ovl_is_whiteout(index) && !verify) {
699+
if (ovl_is_whiteout(index) && !verify) {
706700
/*
707701
* When index lookup is called with !verify for decoding an
708702
* overlay file handle, a whiteout index implies that decode
@@ -1131,7 +1125,7 @@ bool ovl_lower_positive(struct dentry *dentry)
11311125
struct dentry *this;
11321126
struct dentry *lowerdir = poe->lowerstack[i].dentry;
11331127

1134-
this = lookup_one_len_unlocked(name->name, lowerdir,
1128+
this = lookup_positive_unlocked(name->name, lowerdir,
11351129
name->len);
11361130
if (IS_ERR(this)) {
11371131
switch (PTR_ERR(this)) {
@@ -1148,10 +1142,8 @@ bool ovl_lower_positive(struct dentry *dentry)
11481142
break;
11491143
}
11501144
} else {
1151-
if (this->d_inode) {
1152-
positive = !ovl_is_whiteout(this);
1153-
done = true;
1154-
}
1145+
positive = !ovl_is_whiteout(this);
1146+
done = true;
11551147
dput(this);
11561148
}
11571149
}

‎fs/quota/dquot.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,15 +2507,10 @@ int dquot_quota_on_mount(struct super_block *sb, char *qf_name,
25072507
struct dentry *dentry;
25082508
int error;
25092509

2510-
dentry = lookup_one_len_unlocked(qf_name, sb->s_root, strlen(qf_name));
2510+
dentry = lookup_positive_unlocked(qf_name, sb->s_root, strlen(qf_name));
25112511
if (IS_ERR(dentry))
25122512
return PTR_ERR(dentry);
25132513

2514-
if (d_really_is_negative(dentry)) {
2515-
error = -ENOENT;
2516-
goto out;
2517-
}
2518-
25192514
error = security_quota_on(dentry);
25202515
if (!error)
25212516
error = vfs_load_quota_inode(d_inode(dentry), type, format_id,

‎include/linux/namei.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern int kern_path_mountpoint(int, const char *, struct path *, unsigned int);
6060
extern struct dentry *try_lookup_one_len(const char *, struct dentry *, int);
6161
extern struct dentry *lookup_one_len(const char *, struct dentry *, int);
6262
extern struct dentry *lookup_one_len_unlocked(const char *, struct dentry *, int);
63+
extern struct dentry *lookup_positive_unlocked(const char *, struct dentry *, int);
6364

6465
extern int follow_down_one(struct path *);
6566
extern int follow_down(struct path *);

0 commit comments

Comments
 (0)