namei: shift nameidata down into filename_parentat()

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
diff --git a/fs/namei.c b/fs/namei.c
index a9c593e..593cf3b 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2146,7 +2146,8 @@
 
 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
 static int path_parentat(int dfd, const struct filename *name,
-				unsigned int flags, struct nameidata *nd)
+				unsigned int flags, struct nameidata *nd,
+				struct path *parent)
 {
 	const char *s = path_init(dfd, name, flags, nd);
 	int err;
@@ -2155,26 +2156,34 @@
 	err = link_path_walk(s, nd);
 	if (!err)
 		err = complete_walk(nd);
-	if (err)
-		terminate_walk(nd);
+	if (!err) {
+		*parent = nd->path;
+		nd->path.mnt = NULL;
+		nd->path.dentry = NULL;
+	}
+	terminate_walk(nd);
 	path_cleanup(nd);
 	return err;
 }
 
 static int filename_parentat(int dfd, struct filename *name,
-				unsigned int flags, struct nameidata *nd)
+				unsigned int flags, struct path *parent,
+				struct qstr *last, int *type)
 {
 	int retval;
-	struct nameidata *saved_nd = set_nameidata(nd);
+	struct nameidata nd, *saved_nd = set_nameidata(&nd);
 
-	retval = path_parentat(dfd, name, flags | LOOKUP_RCU, nd);
+	retval = path_parentat(dfd, name, flags | LOOKUP_RCU, &nd, parent);
 	if (unlikely(retval == -ECHILD))
-		retval = path_parentat(dfd, name, flags, nd);
+		retval = path_parentat(dfd, name, flags, &nd, parent);
 	if (unlikely(retval == -ESTALE))
-		retval = path_parentat(dfd, name, flags | LOOKUP_REVAL, nd);
-
-	if (likely(!retval))
-		audit_inode(name, nd->path.dentry, LOOKUP_PARENT);
+		retval = path_parentat(dfd, name, flags | LOOKUP_REVAL,
+					&nd, parent);
+	if (likely(!retval)) {
+		*last = nd.last;
+		*type = nd.last_type;
+		audit_inode(name, parent->dentry, LOOKUP_PARENT);
+	}
 	restore_nameidata(saved_nd);
 	return retval;
 }
@@ -2183,31 +2192,30 @@
 struct dentry *kern_path_locked(const char *name, struct path *path)
 {
 	struct filename *filename = getname_kernel(name);
-	struct nameidata nd;
+	struct qstr last;
+	int type;
 	struct dentry *d;
 	int err;
 
 	if (IS_ERR(filename))
 		return ERR_CAST(filename);
 
-	err = filename_parentat(AT_FDCWD, filename, 0, &nd);
+	err = filename_parentat(AT_FDCWD, filename, 0, path, &last, &type);
 	if (err) {
 		d = ERR_PTR(err);
 		goto out;
 	}
-	if (nd.last_type != LAST_NORM) {
-		path_put(&nd.path);
+	if (type != LAST_NORM) {
+		path_put(path);
 		d = ERR_PTR(-EINVAL);
 		goto out;
 	}
-	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
-	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
+	mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
+	d = __lookup_hash(&last, path->dentry, 0);
 	if (IS_ERR(d)) {
-		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
-		path_put(&nd.path);
-		goto out;
+		mutex_unlock(&path->dentry->d_inode->i_mutex);
+		path_put(path);
 	}
-	*path = nd.path;
 out:
 	putname(filename);
 	return d;
@@ -2317,7 +2325,6 @@
 		 int *type,
 		 unsigned int flags)
 {
-	struct nameidata nd;
 	struct filename *s = getname(path);
 	int error;
 
@@ -2327,15 +2334,11 @@
 	if (IS_ERR(s))
 		return s;
 
-	error = filename_parentat(dfd, s, flags, &nd);
+	error = filename_parentat(dfd, s, flags, parent, last, type);
 	if (error) {
 		putname(s);
-		return ERR_PTR(error);
+		s = ERR_PTR(error);
 	}
-	*parent = nd.path;
-	*last = nd.last;
-	*type = nd.last_type;
-
 	return s;
 }
 
@@ -3394,7 +3397,8 @@
 				struct path *path, unsigned int lookup_flags)
 {
 	struct dentry *dentry = ERR_PTR(-EEXIST);
-	struct nameidata nd;
+	struct qstr last;
+	int type;
 	int err2;
 	int error;
 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
@@ -3405,7 +3409,7 @@
 	 */
 	lookup_flags &= LOOKUP_REVAL;
 
-	error = filename_parentat(dfd, name, lookup_flags, &nd);
+	error = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
 	if (error)
 		return ERR_PTR(error);
 
@@ -3413,18 +3417,17 @@
 	 * Yucky last component or no last component at all?
 	 * (foo/., foo/.., /////)
 	 */
-	if (nd.last_type != LAST_NORM)
+	if (type != LAST_NORM)
 		goto out;
-	nd.flags &= ~LOOKUP_PARENT;
-	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
 
 	/* don't fail immediately if it's r/o, at least try to report other errors */
-	err2 = mnt_want_write(nd.path.mnt);
+	err2 = mnt_want_write(path->mnt);
 	/*
 	 * Do the final lookup.
 	 */
-	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
-	dentry = __lookup_hash(&nd.last, nd.path.dentry, nd.flags);
+	lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
+	mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT);
+	dentry = __lookup_hash(&last, path->dentry, lookup_flags);
 	if (IS_ERR(dentry))
 		goto unlock;
 
@@ -3438,7 +3441,7 @@
 	 * all is fine. Let's be bastards - you had / on the end, you've
 	 * been asking for (non-existent) directory. -ENOENT for you.
 	 */
-	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
+	if (unlikely(!is_dir && last.name[last.len])) {
 		error = -ENOENT;
 		goto fail;
 	}
@@ -3446,17 +3449,16 @@
 		error = err2;
 		goto fail;
 	}
-	*path = nd.path;
 	return dentry;
 fail:
 	dput(dentry);
 	dentry = ERR_PTR(error);
 unlock:
-	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
+	mutex_unlock(&path->dentry->d_inode->i_mutex);
 	if (!err2)
-		mnt_drop_write(nd.path.mnt);
+		mnt_drop_write(path->mnt);
 out:
-	path_put(&nd.path);
+	path_put(path);
 	return dentry;
 }