@@ -3538,7 +3538,7 @@ static const char *open_last_lookups(struct nameidata *nd,
struct dentry *dir = nd->path.dentry;
int open_flag = op->open_flag;
bool got_write = false;
- struct dentry *dentry;
+ struct dentry *dentry = NULL;
const char *res;
nd->flags |= op->intent;
@@ -3549,28 +3549,57 @@ static const char *open_last_lookups(struct nameidata *nd,
return handle_dots(nd, nd->last_type);
}
- if (!(open_flag & O_CREAT)) {
- if (nd->last.name[nd->last.len])
+ /*
+ * We _can_ be in RCU mode here. For everything but O_EXCL case, do a
+ * fast lookup for the dentry first. For O_CREAT case, we are only
+ * interested in positive dentries. If nothing suitable is found,
+ * fall back to locked codepath.
+ */
+ if ((open_flag & (O_CREAT | O_EXCL)) != (O_CREAT | O_EXCL)) {
+ /* Trailing slashes? */
+ if (unlikely(nd->last.name[nd->last.len]))
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
- /* we _can_ be in RCU mode here */
+
dentry = lookup_fast(nd);
if (IS_ERR(dentry))
return ERR_CAST(dentry);
+ }
+
+ if (!(open_flag & O_CREAT)) {
if (likely(dentry))
goto finish_lookup;
if (WARN_ON_ONCE(nd->flags & LOOKUP_RCU))
return ERR_PTR(-ECHILD);
} else {
- /* create side of things */
+ /* If negative dentry was found earlier,
+ * discard it as we'll need to use the slow path anyway.
+ */
if (nd->flags & LOOKUP_RCU) {
- if (!try_to_unlazy(nd))
+ bool unlazied;
+
+ /* discard negative dentry if one was found */
+ if (dentry && !dentry->d_inode)
+ dentry = NULL;
+
+ unlazied = dentry ? try_to_unlazy_next(nd, dentry) :
+ try_to_unlazy(nd);
+ if (!unlazied)
return ERR_PTR(-ECHILD);
+ } else if (dentry && !dentry->d_inode) {
+ /* discard negative dentry if one was found */
+ dput(dentry);
+ dentry = NULL;
}
audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
+
/* trailing slashes? */
- if (unlikely(nd->last.name[nd->last.len]))
+ if (unlikely(nd->last.name[nd->last.len])) {
+ dput(dentry);
return ERR_PTR(-EISDIR);
+ }
+ if (dentry)
+ goto finish_lookup;
}
if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Today, when opening a file we'll typically do a fast lookup, but if O_CREAT is set, the kernel always takes the exclusive inode lock. I'm sure this was done with the expectation that O_CREAT being set means that we expect to do the create, but that's often not the case. Many programs set O_CREAT even in scenarios where the file already exists. This patch rearranges the pathwalk-for-open code to also attempt a fast_lookup in the O_CREAT case. Have the code always do a fast_lookup (unless O_EXCL is set), and return that without taking the inode_lock when a positive dentry is found in the O_CREAT codepath. Signed-off-by: Jeff Layton <jlayton@kernel.org> --- fs/namei.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-)