From patchwork Fri Jan 24 19:19:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13949885 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 620D4C02181 for ; Fri, 24 Jan 2025 19:20:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8D3A5280098; Fri, 24 Jan 2025 14:19:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80D8D280092; Fri, 24 Jan 2025 14:19:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4A1280098; Fri, 24 Jan 2025 14:19:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 4F5E6280092 for ; Fri, 24 Jan 2025 14:19:57 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D8FD2A0621 for ; Fri, 24 Jan 2025 19:19:56 +0000 (UTC) X-FDA: 83043310392.20.C97D5C0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 35F971C000B for ; Fri, 24 Jan 2025 19:19:55 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kzuTtwb5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.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=1737746395; a=rsa-sha256; cv=none; b=Sx3JDNSX3ZXOC0lFBwq+Th192LOdL3EvcjwU5KfKQ6R7joDpoqsjPhwuuRgWgobgglqFYJ LohXltN2OrMERiRGG7nIFfVzBpmjxrM+0gPP5BV4Cqel3LgleHOrXV7F8N0Gs/bHfJ6jR2 ShRE9WqtCqHFuUV0MKdFIvmQf5B71TM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=kzuTtwb5; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf18.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=1737746395; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=L1nfY3R39KZuQs0KcQdEhLOvZwTICwR85EsrnGFqJV8=; b=0+GgEPosdnFpSuiXr/2JWe+okYXcCN+kOO0XnQr9ZvpZPwe1guLloxj9+3C4CAcY8szGVs 72fZ95nTAVtAKbxgCxZtp+41W0W2fS4a+1Rwv2E9CD7i00MBb/44sd5C6vds0POD4KxhNi tKRY7gcYKyFOzL5p8W+SkUttU21/fL0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 07DBD5C619A; Fri, 24 Jan 2025 19:19:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F03A9C4CEE5; Fri, 24 Jan 2025 19:19:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737746394; bh=kzHPpa2KwO/4QYlO3ISbItr5ch0/fuaXPOLFlhxSI4Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kzuTtwb5nFfTsInS53xDa6b64DCNsdRWaMcj6hDJUkWKBlrZXHEIYasBzIi5+sp/5 fmAWr5c1weaSlZ/q1je+J9GixE8pbr/1FxZbOTsA6hAEK3lVBPi7SdrYOpEzo/R0Vw 1FLASw+xfWjztHLBBsn3VGYvCAfh0sRdBni7A2mN9INCkcCDuo1HjjsFAYVuO40T9J GRwbxR48W5ymurrPq6KoNOpua101EBRDD3GaNGpo11XzXkG6zQZHTJUayLHa+ialwG DgaroVl03FtpTzP7mgPmDvki+KMdGcc9xnnzjJbz2XbTve+BLXiOR4AS9pyDT8mxRS cR/RyT1qdB5AQ== From: cel@kernel.org To: Hugh Dickins , Andrew Morten , Christian Brauner , Al Viro , Greg Kroah-Hartman , Sasha Levin Cc: , , , yukuai3@huawei.com, yangerkun@huawei.com, Chuck Lever , Jan Kara Subject: [RFC PATCH v6.6 03/10] libfs: Add simple_offset_empty() Date: Fri, 24 Jan 2025 14:19:38 -0500 Message-ID: <20250124191946.22308-4-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20250124191946.22308-1-cel@kernel.org> References: <20250124191946.22308-1-cel@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 35F971C000B X-Rspamd-Server: rspam10 X-Stat-Signature: oqfpnwumwen3xe4eowth5qbrjzsi7q98 X-HE-Tag: 1737746395-750191 X-HE-Meta: U2FsdGVkX1+WHy5UWScVOT1mYWpnMyVhB5fmA0BHop0D83Ap8/Fkw5gjlCiX86a98vtnJkSKDq2IL/ZjuO3Q8MfncV6QRT1sK/OxhjzKguj6cozIgea4japL1aJGH31DVWP2CB9xbfnTr+4822Azu1gMpNvKI7BcKxaA4C5bn9sRag4X3+myn6m+gF2YvcJ/Np1D4Jl8yIhikD33ZYOxBa9+UPPEXQq8U7lPQ47iWBmkvWjnm2bzNYytJ/RvyiF5oAmWSvXCPgxuqHfhtgcSpTGsVEyk66EU8BdmRSUPWdqguyDNMriXw1aQ8/cxikXrxSV4mlE6yKep1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ack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever [ Upstream commit ecba88a3b32d733d41e27973e25b2bc580f64281 ] 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 Link: https://lore.kernel.org/r/170820143463.6328.7872919188371286951.stgit@91.116.238.104.host.secureserver.net Signed-off-by: Christian Brauner Stable-dep-of: 5a1a25be995e ("libfs: Add simple_offset_rename() API") 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 c3dc58e776f9..d7b901cb9af4 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -312,6 +312,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 6c3d86532e3f..5104405ce3e6 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3197,6 +3197,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 db7dd45c9181..aaf679976f3b 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3368,7 +3368,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)); @@ -3425,7 +3425,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) {