Skip to content

Commit baa0389

Browse files
Nick PigginAl Viro
authored andcommitted
fs: dentry allocation consolidation
fs: dentry allocation consolidation There are 2 duplicate copies of code in dentry allocation in path lookup. Consolidate them into a single function. Signed-off-by: Nick Piggin <npiggin@kernel.dk> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 2e2e88e commit baa0389

1 file changed

Lines changed: 33 additions & 37 deletions

File tree

‎fs/namei.c‎

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,35 @@ static __always_inline void follow_dotdot(struct nameidata *nd)
685685
follow_mount(&nd->path);
686686
}
687687

688+
/*
689+
* Allocate a dentry with name and parent, and perform a parent
690+
* directory ->lookup on it. Returns the new dentry, or ERR_PTR
691+
* on error. parent->d_inode->i_mutex must be held. d_lookup must
692+
* have verified that no child exists while under i_mutex.
693+
*/
694+
static struct dentry *d_alloc_and_lookup(struct dentry *parent,
695+
struct qstr *name, struct nameidata *nd)
696+
{
697+
struct inode *inode = parent->d_inode;
698+
struct dentry *dentry;
699+
struct dentry *old;
700+
701+
/* Don't create child dentry for a dead directory. */
702+
if (unlikely(IS_DEADDIR(inode)))
703+
return ERR_PTR(-ENOENT);
704+
705+
dentry = d_alloc(parent, name);
706+
if (unlikely(!dentry))
707+
return ERR_PTR(-ENOMEM);
708+
709+
old = inode->i_op->lookup(inode, dentry, nd);
710+
if (unlikely(old)) {
711+
dput(dentry);
712+
dentry = old;
713+
}
714+
return dentry;
715+
}
716+
688717
/*
689718
* It's more convoluted than I'd like it to be, but... it's still fairly
690719
* small and for now I'd prefer to have fast path as straight as possible.
@@ -738,30 +767,13 @@ static int do_lookup(struct nameidata *nd, struct qstr *name,
738767
* so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
739768
*/
740769
dentry = d_lookup(parent, name);
741-
if (!dentry) {
742-
struct dentry *new;
743-
744-
/* Don't create child dentry for a dead directory. */
745-
dentry = ERR_PTR(-ENOENT);
746-
if (IS_DEADDIR(dir))
747-
goto out_unlock;
748-
749-
new = d_alloc(parent, name);
750-
dentry = ERR_PTR(-ENOMEM);
751-
if (new) {
752-
dentry = dir->i_op->lookup(dir, new, nd);
753-
if (dentry)
754-
dput(new);
755-
else
756-
dentry = new;
757-
}
758-
out_unlock:
770+
if (likely(!dentry)) {
771+
dentry = d_alloc_and_lookup(parent, name, nd);
759772
mutex_unlock(&dir->i_mutex);
760773
if (IS_ERR(dentry))
761774
goto fail;
762775
goto done;
763776
}
764-
765777
/*
766778
* Uhhuh! Nasty case: the cache was re-populated while
767779
* we waited on the semaphore. Need to revalidate.
@@ -1135,24 +1147,8 @@ static struct dentry *__lookup_hash(struct qstr *name,
11351147
if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11361148
dentry = do_revalidate(dentry, nd);
11371149

1138-
if (!dentry) {
1139-
struct dentry *new;
1140-
1141-
/* Don't create child dentry for a dead directory. */
1142-
dentry = ERR_PTR(-ENOENT);
1143-
if (IS_DEADDIR(inode))
1144-
goto out;
1145-
1146-
new = d_alloc(base, name);
1147-
dentry = ERR_PTR(-ENOMEM);
1148-
if (!new)
1149-
goto out;
1150-
dentry = inode->i_op->lookup(inode, new, nd);
1151-
if (!dentry)
1152-
dentry = new;
1153-
else
1154-
dput(new);
1155-
}
1150+
if (!dentry)
1151+
dentry = d_alloc_and_lookup(base, name, nd);
11561152
out:
11571153
return dentry;
11581154
}

0 commit comments

Comments
 (0)