Commit ca344a89 authored by Al Viro's avatar Al Viro

do_last: unify may_open() call and everyting after it

We have a bunch of diverging codepaths in do_last(); some of
them converge, but the case of having to create a new file
duplicates large part of common tail of the rest and exits
separately.  Massage them so that they could be merged.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 9b44f1b3
...@@ -2114,7 +2114,10 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2114,7 +2114,10 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
const struct open_flags *op, const char *pathname) const struct open_flags *op, const char *pathname)
{ {
struct dentry *dir = nd->path.dentry; struct dentry *dir = nd->path.dentry;
int open_flag = op->open_flag;
int will_truncate; int will_truncate;
int want_write = 0;
int skip_perm = 0;
struct file *filp; struct file *filp;
struct inode *inode; struct inode *inode;
int error; int error;
...@@ -2138,7 +2141,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2138,7 +2141,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
if (error) if (error)
goto exit; goto exit;
audit_inode(pathname, nd->path.dentry); audit_inode(pathname, nd->path.dentry);
if (op->open_flag & O_CREAT) { if (open_flag & O_CREAT) {
error = -EISDIR; error = -EISDIR;
goto exit; goto exit;
} }
...@@ -2152,7 +2155,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2152,7 +2155,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
goto ok; goto ok;
} }
if (!(op->open_flag & O_CREAT)) { if (!(open_flag & O_CREAT)) {
if (nd->last.name[nd->last.len]) if (nd->last.name[nd->last.len])
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
/* we _can_ be in RCU mode here */ /* we _can_ be in RCU mode here */
...@@ -2230,28 +2233,15 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2230,28 +2233,15 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
error = mnt_want_write(nd->path.mnt); error = mnt_want_write(nd->path.mnt);
if (error) if (error)
goto exit_mutex_unlock; goto exit_mutex_unlock;
error = __open_namei_create(nd, path, op->open_flag, op->mode); want_write = 1;
if (error) { will_truncate = 0;
mnt_drop_write(nd->path.mnt); error = __open_namei_create(nd, path, open_flag, op->mode);
if (error)
goto exit; goto exit;
}
/* Don't check for write permission, don't truncate */ /* Don't check for write permission, don't truncate */
error = may_open(&nd->path, 0, op->open_flag & ~O_TRUNC); open_flag &= ~O_TRUNC;
if (error) { skip_perm = 1;
mnt_drop_write(nd->path.mnt); goto common;
goto exit;
}
filp = nameidata_to_filp(nd);
mnt_drop_write(nd->path.mnt);
path_put(&nd->path);
if (!IS_ERR(filp)) {
error = ima_file_check(filp, op->acc_mode);
if (error) {
fput(filp);
filp = ERR_PTR(error);
}
}
return filp;
} }
/* /*
...@@ -2261,7 +2251,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2261,7 +2251,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
audit_inode(pathname, path->dentry); audit_inode(pathname, path->dentry);
error = -EEXIST; error = -EEXIST;
if (op->open_flag & O_EXCL) if (open_flag & O_EXCL)
goto exit_dput; goto exit_dput;
error = follow_managed(path, nd->flags); error = follow_managed(path, nd->flags);
...@@ -2281,18 +2271,17 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2281,18 +2271,17 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
if (S_ISDIR(nd->inode->i_mode)) if (S_ISDIR(nd->inode->i_mode))
goto exit; goto exit;
ok: ok:
will_truncate = open_will_truncate(op->open_flag, nd->path.dentry->d_inode); will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
if (will_truncate) { if (will_truncate) {
error = mnt_want_write(nd->path.mnt); error = mnt_want_write(nd->path.mnt);
if (error) if (error)
goto exit; goto exit;
want_write = 1;
} }
error = may_open(&nd->path, op->acc_mode, op->open_flag); common:
if (error) { error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
if (will_truncate) if (error)
mnt_drop_write(nd->path.mnt);
goto exit; goto exit;
}
filp = nameidata_to_filp(nd); filp = nameidata_to_filp(nd);
if (!IS_ERR(filp)) { if (!IS_ERR(filp)) {
error = ima_file_check(filp, op->acc_mode); error = ima_file_check(filp, op->acc_mode);
...@@ -2310,12 +2299,8 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2310,12 +2299,8 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
} }
} }
} }
/* out:
* It is now safe to drop the mnt write if (want_write)
* because the filp has had a write taken
* on its behalf.
*/
if (will_truncate)
mnt_drop_write(nd->path.mnt); mnt_drop_write(nd->path.mnt);
path_put(&nd->path); path_put(&nd->path);
return filp; return filp;
...@@ -2325,8 +2310,8 @@ static struct file *do_last(struct nameidata *nd, struct path *path, ...@@ -2325,8 +2310,8 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
exit_dput: exit_dput:
path_put_conditional(path, nd); path_put_conditional(path, nd);
exit: exit:
path_put(&nd->path); filp = ERR_PTR(error);
return ERR_PTR(error); goto out;
} }
static struct file *path_openat(int dfd, const char *pathname, static struct file *path_openat(int dfd, const char *pathname,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment