Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758917AbcCVNQn (ORCPT ); Tue, 22 Mar 2016 09:16:43 -0400 Received: from mail-wm0-f45.google.com ([74.125.82.45]:35066 "EHLO mail-wm0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758819AbcCVNQY (ORCPT ); Tue, 22 Mar 2016 09:16:24 -0400 Date: Tue, 22 Mar 2016 14:17:42 +0100 From: Miklos Szeredi To: Al Viro Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH] vfs: rename: check backing inode being equal Message-ID: <20160322131742.GC11906@tucsk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1635 Lines: 51 If a file is renamed to a hardlink of itself POSIX specifies that rename(2) should do nothing and return success. This condition is checked in vfs_rename(). However it won't detect hard links on overlayfs where these are given separate inodes on the overlayfs layer. Overlayfs itself detects this condition and returns success without doing anything, but then vfs_rename() will proceed as if this was a successful rename (detach_mounts(), d_move()). The correct thing to do is to detect this condition before even calling into overlayfs. This patch does this by checking for DCACHE_OP_SELECT_INODE and getting the underlying inode in that case. Signed-off-by: Miklos Szeredi Cc: # v4.2+ --- fs/namei.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) --- a/fs/namei.c +++ b/fs/namei.c @@ -4150,6 +4150,17 @@ SYSCALL_DEFINE2(link, const char __user return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); } +static struct inode *backing_inode(struct dentry *dentry) +{ + struct inode *inode = d_inode(dentry); + + if (inode && dentry->d_flags & DCACHE_OP_SELECT_INODE) { + inode = dentry->d_op->d_select_inode(dentry, 0); + WARN_ON(IS_ERR(inode)); + } + return inode; +} + /** * vfs_rename - rename a filesystem object * @old_dir: parent of source @@ -4211,7 +4222,7 @@ int vfs_rename(struct inode *old_dir, st bool new_is_dir = false; unsigned max_links = new_dir->i_sb->s_max_links; - if (source == target) + if (backing_inode(old_dentry) == backing_inode(new_dentry)) return 0; error = may_delete(old_dir, old_dentry, is_dir);