From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zeniv.linux.org.uk ([195.92.253.2]:38110 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754424AbeGIEyU (ORCPT ); Mon, 9 Jul 2018 00:54:20 -0400 From: Al Viro To: Linus Torvalds Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC][PATCH 17/27] __shmem_file_setup(): reorder allocations Date: Mon, 9 Jul 2018 05:54:07 +0100 Message-Id: <20180709045417.13988-17-viro@ZenIV.linux.org.uk> In-Reply-To: <20180709045417.13988-1-viro@ZenIV.linux.org.uk> References: <20180709045320.GF30522@ZenIV.linux.org.uk> <20180709045417.13988-1-viro@ZenIV.linux.org.uk> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: From: Al Viro grab inode and reserve memory first. Signed-off-by: Al Viro --- mm/shmem.c | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index a76adc3802d1..0aa290f849ee 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3918,41 +3918,38 @@ static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, l if (shmem_acct_size(flags, size)) return ERR_PTR(-ENOMEM); - res = ERR_PTR(-ENOMEM); + sb = mnt->mnt_sb; + inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); + if (unlikely(!inode)) { + shmem_unacct_size(flags, size); + return ERR_PTR(-ENOSPC); + } + inode->i_flags |= i_flags; + inode->i_size = size; + clear_nlink(inode); /* It is unlinked */ + res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); + if (IS_ERR(res)) { + iput(inode); + return res; + } + this.name = name; this.len = strlen(name); this.hash = 0; /* will go */ - sb = mnt->mnt_sb; path.mnt = mntget(mnt); path.dentry = d_alloc_pseudo(sb, &this); - if (!path.dentry) - goto put_memory; + if (!path.dentry) { + iput(inode); + return ERR_PTR(-ENOMEM); + } d_set_d_op(path.dentry, &anon_ops); - res = ERR_PTR(-ENOSPC); - inode = shmem_get_inode(sb, NULL, S_IFREG | 0777, 0, flags); - if (!inode) - goto put_memory; - - inode->i_flags |= i_flags; d_instantiate(path.dentry, inode); - inode->i_size = size; - clear_nlink(inode); /* It is unlinked */ - res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); - if (IS_ERR(res)) - goto put_path; res = alloc_file(&path, FMODE_WRITE | FMODE_READ | FMODE_OPENED, &shmem_file_operations); if (IS_ERR(res)) - goto put_path; - - return res; - -put_memory: - shmem_unacct_size(flags, size); -put_path: - path_put(&path); + path_put(&path); return res; } -- 2.11.0