From patchwork Sat Feb 17 20:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13561582 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67B34C48BC3 for ; Sat, 17 Feb 2024 20:24:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB11D8D0007; Sat, 17 Feb 2024 15:23:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3A158D0001; Sat, 17 Feb 2024 15:23:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDC1E8D0007; Sat, 17 Feb 2024 15:23:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B65F08D0001 for ; Sat, 17 Feb 2024 15:23:59 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 60AEB80198 for ; Sat, 17 Feb 2024 20:23:59 +0000 (UTC) X-FDA: 81802422198.27.37FD7D4 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id B2366C000A for ; Sat, 17 Feb 2024 20:23:57 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hSPSc3YT; spf=pass (imf22.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708201437; a=rsa-sha256; cv=none; b=iqqPrcVTRy9v1J7tvyRxYhLpbSuzNG1UkV+rb73NMK6FVfJOjFGD6oyKVkOLCwd4vhvOiA Gzv+MGCNc0FpYHmElNu9MB6x7aWMdfAcjWRQKpJmdKwRJXJj4LmuOBMkdCTpXdgMDYj9HZ 7n24yIduBP7wtjOJ1NmkTASsXImti4I= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hSPSc3YT; spf=pass (imf22.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708201437; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4qPfTWx2wBbF6c6gCarkXbRmheLFXCu0GVp2UOlPxx0=; b=AMexbdRR1l3G5NzHqm99IbppCZM5FoDQIvFb97FLJQ99JfDr+rhfF3ZCU+lSXHKWLc9SET mL90JIxyouU/cvj+eazyGCHGIBjKNVOyXrweI2Iw4HfDyJA/VFdl412wpy3AqD43TeY/ws Z5U0orBf9x/PZLjxIRTMCBDPBD8VGTE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id E47C4606DC; Sat, 17 Feb 2024 20:23:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9828DC433C7; Sat, 17 Feb 2024 20:23:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708201436; bh=Wl5YZpZIwNdyP0j7hPhqHmMF9/qk/KQwvYrvu9hugUU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=hSPSc3YTTnETZH7tpcODEx1noXHcGxWv4jbcdGMmkKFsEWnfprlfhBAMvQrRX//ay cgIsqNc565w8r+aJyx0o2wv3/D0ExShXnYuhchx1OdQ6esqSVlx4XCBIwsz0wR4Tc+ SLiXgISMOVYmI8HKJURtotqLYVMwdItc1YCaZMJaTNJ9wiCtKOi5mW2hiO9NAnrsL4 eCstP8GCNdyd2EaFgD7YXM37wiYuqB7EwqVdbHXQCbINlch7KtIHb/1vlLp4w3hwYH R9vt1ltVSLWnz79sr+OcRO1RCM8jv6mrvIq9talI2/YedhJ5Oy60DgXlVhj+DFJHq/ p8Xc60B+KN9Pw== Subject: [PATCH v2 3/6] libfs: Add simple_offset_empty() From: Chuck Lever To: viro@zeniv.linux.org.uk, brauner@kernel.org, jack@suse.cz, hughd@google.com, akpm@linux-foundation.org, Liam.Howlett@oracle.com, oliver.sang@intel.com, feng.tang@intel.com Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, maple-tree@lists.infradead.org, linux-mm@kvack.org, lkp@intel.com Date: Sat, 17 Feb 2024 15:23:54 -0500 Message-ID: <170820143463.6328.7872919188371286951.stgit@91.116.238.104.host.secureserver.net> In-Reply-To: <170820083431.6328.16233178852085891453.stgit@91.116.238.104.host.secureserver.net> References: <170820083431.6328.16233178852085891453.stgit@91.116.238.104.host.secureserver.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: B2366C000A X-Stat-Signature: n9gtic3or9wsquixhabtyrgp5eujfi9w X-Rspam-User: X-HE-Tag: 1708201437-923113 X-HE-Meta: U2FsdGVkX19FwZYbyPjmlNiaPB/n3Df2hZOwnxKI26/xJ8yjVD9cJGyu5qYxD3A2O5J27pmQF/SN6iBWd5MIiSvT9N0bxW95GrajCmJtWtJAniMCyEiGV1IiHL0OI5ZfB028TsePDttxoJvrtiAQxCJPBk2DnBQvvx5RhMXiAIIr1CNO4/zXA2iM4RUnoE4aQesqTG4bmB+19fsjSBC+dAGyMsaAu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, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever For simple filesystems that use directory offset mapping, rely strictly on the directory offset map to tell when a directory has no children. After this patch is applied, the emptiness test holds only the RCU read lock when the directory being tested has no children. In addition, this adds another layer of confirmation that simple_offset_add/remove() are working as expected. Reviewed-by: Jan Kara Signed-off-by: Chuck Lever --- fs/libfs.c | 32 ++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + mm/shmem.c | 4 ++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index f0045db739df..f7f92a49a418 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -313,6 +313,38 @@ void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry) offset_set(dentry, 0); } +/** + * simple_offset_empty - Check if a dentry can be unlinked + * @dentry: dentry to be tested + * + * Returns 0 if @dentry is a non-empty directory; otherwise returns 1. + */ +int simple_offset_empty(struct dentry *dentry) +{ + struct inode *inode = d_inode(dentry); + struct offset_ctx *octx; + struct dentry *child; + unsigned long index; + int ret = 1; + + if (!inode || !S_ISDIR(inode->i_mode)) + return ret; + + index = DIR_OFFSET_MIN; + octx = inode->i_op->get_offset_ctx(inode); + xa_for_each(&octx->xa, index, child) { + spin_lock(&child->d_lock); + if (simple_positive(child)) { + spin_unlock(&child->d_lock); + ret = 0; + break; + } + spin_unlock(&child->d_lock); + } + + return ret; +} + /** * simple_offset_rename_exchange - exchange rename with directory offsets * @old_dir: parent of dentry being moved diff --git a/include/linux/fs.h b/include/linux/fs.h index ed5966a70495..03d141809a2c 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3267,6 +3267,7 @@ struct offset_ctx { void simple_offset_init(struct offset_ctx *octx); int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry); void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry); +int simple_offset_empty(struct dentry *dentry); int simple_offset_rename_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, diff --git a/mm/shmem.c b/mm/shmem.c index d7c84ff62186..6fed524343cb 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3374,7 +3374,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) static int shmem_rmdir(struct inode *dir, struct dentry *dentry) { - if (!simple_empty(dentry)) + if (!simple_offset_empty(dentry)) return -ENOTEMPTY; drop_nlink(d_inode(dentry)); @@ -3431,7 +3431,7 @@ static int shmem_rename2(struct mnt_idmap *idmap, return simple_offset_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); - if (!simple_empty(new_dentry)) + if (!simple_offset_empty(new_dentry)) return -ENOTEMPTY; if (flags & RENAME_WHITEOUT) {