From patchwork Tue Feb 13 21:37:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13555683 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 65582C4829A for ; Tue, 13 Feb 2024 21:37:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E90BE8D0013; Tue, 13 Feb 2024 16:37:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E19306B0096; Tue, 13 Feb 2024 16:37:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6DAC8D0013; Tue, 13 Feb 2024 16:37:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B1DDD6B0095 for ; Tue, 13 Feb 2024 16:37:44 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 7D0DD401FD for ; Tue, 13 Feb 2024 21:37:44 +0000 (UTC) X-FDA: 81788092848.28.8707B4D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id C887DC0018 for ; Tue, 13 Feb 2024 21:37:42 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Sb+F8d6i; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707860262; 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=6D7+ic2NMajZjJRZ58O+PQEBSPCxwZs8miT0K3jUJcc=; b=HlYxLEIFHP1Se/BHxv7XjvL3OaWC7rnqU5wFF4E6K2Zm86Fyc/nwew7J88xx/mxyrLLYHZ gc7+sAMDraanENVI90XAXPw1PWUSgAyMRPjWgo29OCmXir1uA8U7BFEwulAywYa1yHnejB cOwIleo87Sfrnch85VTQzXVnf2NFa5Q= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Sb+F8d6i; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf28.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707860262; a=rsa-sha256; cv=none; b=2mRQcXiKc/JyMciTu4KnhNViKqG8ylRwSGPtsd7k/JszKC6WGognx0A2Kq1Se9SK36C4fB hZtYluILo0ZJgx2AEmqVpRwl2vubrC5wdJcqOwePhCIr+dlp/5FyO765h9BfMq1zyQxx2j tZfNYtuPGZHvysYXtOngDPnvHyoYmmI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 16DDC616E9; Tue, 13 Feb 2024 21:37:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1874C433C7; Tue, 13 Feb 2024 21:37:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707860261; bh=Mc8iA4R+RvlCogjizmfuBdLaRuEY94aY5Pgh2nLn8ac=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=Sb+F8d6imFeQ45gYeWAN4/BMaT90jG5csQsCSot0PbyMT88i+74CZeUtTPvUxmuUP t7FsY5dTvXbuSKoD8EVHTD8zCxRCTcuApqgMgZ21lhygy+SW4ZxqjBJsTye+y9s532 2VQycVXwzvuXfNBx7QujixZBOFpQ2k+da0/+MZ5FsbBHbiJ8Y7tUguIoLfpsAiomNz 6mFTz8/lMazFoYkJqBJagtYvSSnkHH7k2ksf24CyKW+70KC7jCqYsBGrvQtH3C0jxc G2qH/gokXheDzLuRZiLeolXi1GnEcqa3Y+gPx9AVvs6JbEzWwH8p7cstiyVtjXES19 iDaqW0CwmhJ/A== Subject: [PATCH RFC 3/7] 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: Tue, 13 Feb 2024 16:37:39 -0500 Message-ID: <170786025969.11135.16880338029664682984.stgit@91.116.238.104.host.secureserver.net> In-Reply-To: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> References: <170785993027.11135.8830043889278631735.stgit@91.116.238.104.host.secureserver.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: C887DC0018 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 7sf63g1his75syhwb1j9yjhtmgdnnnau X-HE-Tag: 1707860262-32708 X-HE-Meta: U2FsdGVkX1835ifjHrF2xtoBwI/vDzLm9Riy5IWBVcOgKOv4u3QxjhIMELJklKhZqPOC4aofbcavZ1YjuJaLrMpgzIySYAk+C4d/XSBV3U6hNWmTAhatkkBqGVGcfmjqQ86jH702KVZUnGO72bvuiGxvFdUKI1mEKH7dI1+knx11yKF0JPfBEwzd+oKRWvts7zjyyoH0eca/F2pznLNZSzm6Ivhksur3WLBP2FKvAdWeL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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. Signed-off-by: Chuck Lever Reviewed-by: Jan Kara --- 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 a38af72f4719..3cf773950f93 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 = 2; + 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) {