From patchwork Sun Mar 28 14:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Almeida?= X-Patchwork-Id: 12168987 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ECC24C433E6 for ; Sun, 28 Mar 2021 14:45:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CED1B61972 for ; Sun, 28 Mar 2021 14:45:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231359AbhC1Oo4 (ORCPT ); Sun, 28 Mar 2021 10:44:56 -0400 Received: from bhuna.collabora.co.uk ([46.235.227.227]:33608 "EHLO bhuna.collabora.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230526AbhC1Oo0 (ORCPT ); Sun, 28 Mar 2021 10:44:26 -0400 Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: tonyk) with ESMTPSA id C8BF11F4418B From: =?utf-8?q?Andr=C3=A9_Almeida?= To: Alexander Viro , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Chao Yu Cc: krisman@collabora.com, kernel@collabora.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Daniel Rosenberg , Chao Yu , =?utf-8?q?Andr=C3=A9_Almeida?= Subject: [PATCH 1/3] fs/dcache: Add d_clear_dir_neg_dentries() Date: Sun, 28 Mar 2021 11:43:54 -0300 Message-Id: <20210328144356.12866-2-andrealmeid@collabora.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210328144356.12866-1-andrealmeid@collabora.com> References: <20210328144356.12866-1-andrealmeid@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org For directories with negative dentries that are becoming case-insensitive dirs, we need to remove all those negative dentries, otherwise they will become dangling dentries. During the creation of a new file, if a d_hash collision happens and the names match in a case-insensitive way, the name of the file will be the name defined at the negative dentry, that may be different from the specified by the user. To prevent this from happening, we need to remove all dentries in a directory. Given that the directory must be empty before we call this function we are sure that all dentries there will be negative. Create a function to remove all negative dentries from a directory, to be used as explained above by filesystems that support case-insensitive lookups. Signed-off-by: André Almeida --- fs/dcache.c | 27 +++++++++++++++++++++++++++ include/linux/dcache.h | 1 + 2 files changed, 28 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index 7d24ff7eb206..fafb3016d6fd 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -1723,6 +1723,33 @@ void d_invalidate(struct dentry *dentry) } EXPORT_SYMBOL(d_invalidate); +/** + * d_clear_dir_neg_dentries - Remove negative dentries in an inode + * @dir: Directory to clear negative dentries + * + * For directories with negative dentries that are becoming case-insensitive + * dirs, we need to remove all those negative dentries, otherwise they will + * become dangling dentries. During the creation of a new file, if a d_hash + * collision happens and the names match in a case-insensitive, the name of + * the file will be the name defined at the negative dentry, that can be + * different from the specified by the user. To prevent this from happening, we + * need to remove all dentries in a directory. Given that the directory must be + * empty before we call this function we are sure that all dentries there will + * be negative. + */ +void d_clear_dir_neg_dentries(struct inode *dir) +{ + struct dentry *alias, *dentry; + + hlist_for_each_entry(alias, &dir->i_dentry, d_u.d_alias) { + list_for_each_entry(dentry, &alias->d_subdirs, d_child) { + d_drop(dentry); + dput(dentry); + } + } +} +EXPORT_SYMBOL(d_clear_dir_neg_dentries); + /** * __d_alloc - allocate a dcache entry * @sb: filesystem it will belong to diff --git a/include/linux/dcache.h b/include/linux/dcache.h index c1e48014106f..c43cd0be077f 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -250,6 +250,7 @@ extern void shrink_dcache_sb(struct super_block *); extern void shrink_dcache_parent(struct dentry *); extern void shrink_dcache_for_umount(struct super_block *); extern void d_invalidate(struct dentry *); +extern void d_clear_dir_neg_dentries(struct inode *); /* only used at mount-time */ extern struct dentry * d_make_root(struct inode *); From patchwork Sun Mar 28 14:43:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Almeida?= X-Patchwork-Id: 12168985 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2F3FC433E4 for ; Sun, 28 Mar 2021 14:45:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A938B61972 for ; Sun, 28 Mar 2021 14:45:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231366AbhC1Oo5 (ORCPT ); Sun, 28 Mar 2021 10:44:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48302 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231208AbhC1Ooa (ORCPT ); Sun, 28 Mar 2021 10:44:30 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E58AC061756; Sun, 28 Mar 2021 07:44:30 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: tonyk) with ESMTPSA id CF00F1F42CD7 From: =?utf-8?q?Andr=C3=A9_Almeida?= To: Alexander Viro , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Chao Yu Cc: krisman@collabora.com, kernel@collabora.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Daniel Rosenberg , Chao Yu , =?utf-8?q?Andr=C3=A9_Almeida?= Subject: [PATCH 2/3] ext4: Prevent dangling dentries on casefold directories Date: Sun, 28 Mar 2021 11:43:55 -0300 Message-Id: <20210328144356.12866-3-andrealmeid@collabora.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210328144356.12866-1-andrealmeid@collabora.com> References: <20210328144356.12866-1-andrealmeid@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Before making a folder a case-insensitive one, this folder could have been used before and created some negative dentries (given that the folder needs to be empty before making it case-insensitive, all detries there are negative ones). During a new file creation, if a d_hash() collision happens and the name matches a negative dentry, the new file might have a name different than the specified by user. To prevent this from happening, remove all negative dentries in a directory before making it a case-folded one. Fixes: b886ee3e778e ("ext4: Support case-insensitive file name lookups") Signed-off-by: André Almeida --- fs/ext4/ioctl.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index a2cf35066f46..0eede4c93c22 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -381,6 +381,9 @@ static int ext4_ioctl_setflags(struct inode *inode, err = -ENOTEMPTY; goto flags_out; } + + if (!(oldflags & EXT4_CASEFOLD_FL) && (flags & EXT4_CASEFOLD_FL)) + d_clear_dir_neg_dentries(inode); } /* From patchwork Sun Mar 28 14:43:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andr=C3=A9_Almeida?= X-Patchwork-Id: 12168989 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 248A9C433E8 for ; Sun, 28 Mar 2021 14:45:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F38D6194A for ; Sun, 28 Mar 2021 14:45:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231372AbhC1Oo5 (ORCPT ); Sun, 28 Mar 2021 10:44:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231321AbhC1Ooe (ORCPT ); Sun, 28 Mar 2021 10:44:34 -0400 Received: from bhuna.collabora.co.uk (bhuna.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e3e3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CBFBC061756; Sun, 28 Mar 2021 07:44:34 -0700 (PDT) Received: from [127.0.0.1] (localhost [127.0.0.1]) (Authenticated sender: tonyk) with ESMTPSA id CED981F46335 From: =?utf-8?q?Andr=C3=A9_Almeida?= To: Alexander Viro , Theodore Ts'o , Andreas Dilger , Jaegeuk Kim , Chao Yu Cc: krisman@collabora.com, kernel@collabora.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, Daniel Rosenberg , Chao Yu , =?utf-8?q?Andr=C3=A9_Almeida?= Subject: [PATCH 3/3] f2fs: Prevent dangling dentries on casefold directories Date: Sun, 28 Mar 2021 11:43:56 -0300 Message-Id: <20210328144356.12866-4-andrealmeid@collabora.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210328144356.12866-1-andrealmeid@collabora.com> References: <20210328144356.12866-1-andrealmeid@collabora.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Before making a folder a case-insensitive one, this folder could have been used before and created some negative dentries (given that the folder needs to be empty before making it case-insensitive, all detries there are negative ones). During a new file creation, if a d_hash() collision happens and the name matches a negative dentry, the new file might have a name different than the specified by user. To prevent this from happening, remove all negative dentries in a directory before making it a case-folded one. Fixes: 2c2eb7a300cd ("f2fs: Support case-insensitive file name lookups") Signed-off-by: André Almeida --- fs/f2fs/file.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index d26ff2ae3f5e..616b7eb43795 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1826,6 +1826,10 @@ static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask) return -EOPNOTSUPP; if (!f2fs_empty_dir(inode)) return -ENOTEMPTY; + + if (!(masked_flags & F2FS_CASEFOLD_FL) && + (iflags & F2FS_CASEFOLD_FL)) + d_clear_dir_neg_dentries(inode); } if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {