const struct open_flags *op, const char *pathname)
{
struct dentry *dir = nd->path.dentry;
+ int open_flag = op->open_flag;
int will_truncate;
+ int want_write = 0;
+ int skip_perm = 0;
struct file *filp;
struct inode *inode;
int error;
if (error)
goto exit;
audit_inode(pathname, nd->path.dentry);
- if (op->open_flag & O_CREAT) {
+ if (open_flag & O_CREAT) {
error = -EISDIR;
goto exit;
}
goto ok;
}
- if (!(op->open_flag & O_CREAT)) {
+ if (!(open_flag & O_CREAT)) {
if (nd->last.name[nd->last.len])
nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
/* we _can_ be in RCU mode here */
error = mnt_want_write(nd->path.mnt);
if (error)
goto exit_mutex_unlock;
- error = __open_namei_create(nd, path, op->open_flag, op->mode);
- if (error) {
- mnt_drop_write(nd->path.mnt);
+ want_write = 1;
+ will_truncate = 0;
+ error = __open_namei_create(nd, path, open_flag, op->mode);
+ if (error)
goto exit;
- }
/* Don't check for write permission, don't truncate */
- error = may_open(&nd->path, 0, op->open_flag & ~O_TRUNC);
- if (error) {
- mnt_drop_write(nd->path.mnt);
- 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;
+ open_flag &= ~O_TRUNC;
+ skip_perm = 1;
+ goto common;
}
/*
audit_inode(pathname, path->dentry);
error = -EEXIST;
- if (op->open_flag & O_EXCL)
+ if (open_flag & O_EXCL)
goto exit_dput;
error = follow_managed(path, nd->flags);
if (S_ISDIR(nd->inode->i_mode))
goto exit;
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) {
error = mnt_want_write(nd->path.mnt);
if (error)
goto exit;
+ want_write = 1;
}
- error = may_open(&nd->path, op->acc_mode, op->open_flag);
- if (error) {
- if (will_truncate)
- mnt_drop_write(nd->path.mnt);
+common:
+ error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
+ if (error)
goto exit;
- }
filp = nameidata_to_filp(nd);
if (!IS_ERR(filp)) {
error = ima_file_check(filp, op->acc_mode);
}
}
}
- /*
- * It is now safe to drop the mnt write
- * because the filp has had a write taken
- * on its behalf.
- */
- if (will_truncate)
+out:
+ if (want_write)
mnt_drop_write(nd->path.mnt);
path_put(&nd->path);
return filp;
exit_dput:
path_put_conditional(path, nd);
exit:
- path_put(&nd->path);
- return ERR_PTR(error);
+ filp = ERR_PTR(error);
+ goto out;
}
static struct file *path_openat(int dfd, const char *pathname,