From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.12.124.146 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795988026; b=M5R9HzvXBlLz7JvjKE2H8btdY4sUFYyD8Fswvbezrwlmb5tig3JNbGwycSWYmLygI4To iUkeeSXd6eGJzHqAfv8JJHfohmwl/yqthj6f95KMT6r5HJnCsCGJ8ObMpEQdB8hKc6xbN uG8b1CtWbnyBtfPNqMco+ALB58a1Kggrss6AbELwLDwiuWi4L653+gsTW86VmzXNi9f6G g44xvkErUgAm16PXArB1wAoth4SOnFNCr+ktrlGkKXEm6nW9cKaBS7S4qErAsgteV07ve BlKJvIisVwFZsqkBXAB+BRzRaI6YtVoavsNmJEbQ4MmtJ1ZAaEv4zvgIHqFn11pVngA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795988026; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Received:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID: Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:Reply-To:MIME-Version:Content-Transfer-Encoding; bh=p5acfDepdOmlOwMWkS1s17Y72kEkQ4gAs/uc6pPx2uE=; b=VIrbtxbJ/Wnbmc353nsR0qFdWb0ZGMFx2GiyICmAFWOv76YgTVKt3PcHs+nDSAZK8dOG AvOrai64m0O2wlK014R9FEZ9bSWoq4WRz112tTeHOpkEIhfnvnU+wSDE2kespALgADKNQ a3EW9tLiX4E8lbBdXmGmyX0V0q/O4qEIYwPf909wMgVCLNZ/vk8RLy2mRzfbbBjOFN1rD 6/peYhwGj+BaADwV+0l447yaXLPrG5119FpOf8uP23tK/bpuVdAcI8+opp/dfwBFhL7qP u80+7RUw3CeoYo6DwYKefEVwfnpeeoCyoDX0Hr0EV9fFXm9MKoiVZs9phfBuwKYkfNQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=ownmail.net; spf=pass smtp.mailfrom=ownmail.net; dkim=pass header.d=ownmail.net header.i=@ownmail.net header.a=rsa-sha256 header.s=fm3 header.b=ByatTDya; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=k9YIWoyD; arc=none smtp.remote-ip=202.12.124.146 Received: from fout-b3-smtp.messagingengine.com (fout-b3-smtp.messagingengine.com [202.12.124.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 229D2606A31C for ; Sun, 29 Nov 2026 21:33:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 229D2606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ownmail.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ownmail.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=ownmail.net header.i=@ownmail.net header.a=rsa-sha256 header.s=fm3 header.b=ByatTDya; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=k9YIWoyD Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfout.stl.internal (Postfix) with ESMTP id 9B6371D00538 for ; Sun, 29 Nov 2026 16:33:45 -0500 (EST) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Sun, 29 Nov 2026 16:33:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ownmail.net; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to; s=fm3; t=1795988025; x=1796074425; bh=p5acfDepdOmlOwMWkS1s17Y72kEkQ4gAs/uc6pPx2uE=; b= ByatTDyan3JLvKGApYX6SU1KkmIPN5bgtnlNt0bAQE5IVrFbnVarrExe4t0bsLCS jZ1e5UWkoIh76xPiZJTNICJ8mH+V9iBMdDlALWMs0CdjCE8Ep0rt8HQiaO/1tpDG RKlvQPdN4Q2hNKeeDvNv6srVnviaSLzgImYxEPi72oOEJQGJLrJyR2sJUxH2uxON afmk8agQT+BOQuQ2we72aewgyhTi/8jQwv+HsODZM9Nnipn5vhp+X+pISjfOWVgo IumPeKfO7cxiGlEpqcSLBnXFUC7dnKodhuz24ZHppITtWp2irMVI/k6U7N1aSjDO tCrQgUgOBzbssGzr3FtAjQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1795988025; x=1796074425; bh=p 5acfDepdOmlOwMWkS1s17Y72kEkQ4gAs/uc6pPx2uE=; b=k9YIWoyDHmS6ycGmg AJhRqfj0yWiGMsWP1z7TbcAmN+SrNQVFEIQ0h8iJp02us68USyUH2Czno2yqImbC zcTxiPbCYHhw+oIsra31ElGt7eWwHBw1Wr+Fh+Mb0RVPAs+0a2jwdJwLLjMutY/z 4j30YH5Us5dSH9z12CX/nx7btTeG1jnj2PhjYJdk4G+nvtNJfmjdw8aLnJmhKAng S7QWuKOnnz295NDZX49Zd0bKRsT0tx59G2zwGr6mjuSqCMpJluGBTKZPzayP+1p+ OlYFmbJPFPxRQlS67YfX21+ATJOODil3GmXQ7k6HJTxsxhAHTWRvSegLPr0fPWDH WsUNw== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=ownmail.net a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1795988025; d=ownmail.net; mf=PG5laWxiQG93bm1haWwubmV0Pg==; rt=PGxpbnV4LWtlcm5lbEB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:ooTUlWee3zBpUCIHFvT/JMYZC8ViqbSyVjjDD26l94L1lac 8/ebPYjb1NsCia5Gv5qQ/o0qniGmus2RSgrMnKCDVUZ7NuO0OeQFql+rPlcfJlO9 U28DkfI3kjMtRKYkF1JwQK0LPZfyLmGeRnEIAAwhEsJt7KLF6CZENjN5+KXLS1RU WnMBUCnHK4xVEoRavhsRnYPjj0mGyVsvz2o3Am3itivsWx1AgLYDHEmC74iVZOBa TRMIUM+5YzBUVJRnI6qIEtXlgm/Zx64xjJpk6di8C+Sm3DFVU70J6450N85FYFkl nGnv/GQ+j+hes4Sy/a/+kLtTadVGqy1dKwJkniQ==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=12; hn=cc,content-transfer-encoding,date,feedback-id,from, in-reply-to,message-id,mime-version,references,reply-to,subject, to; Message-Instance: m=1; h=sha256:cJf4jQUXs2EbEvv6OYT04rqyMDPFrMGVCsD4ne2T76k=:zlZyQ2tubD5jEsZy55dYgbHrzluMbd5f4X7kvMuKiQ4=; X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTGfp+6UBLGiiDzhhvWZmQgOIh1cOq5mJlXMIt9t8f7mgPkw9O54UqDYx8m+G8xM/o Glp6CyspmZznXyFFGTCiv0+krGDh55+K3LA9NTcd2lLQv7PKNzaQhv27TLlWxE8jja9yt/ eWlUFkLA5Mr6taIlfz0dk/M2LK2EBEcXYVypLDPkiTTWLeGvj4LSG0FLNy+RdJxiQyRXZn Wtdviy/lwLA5MUDNqsONoZ0BA3L1qr3jXexL1MpEbyY/vX4FIEohn710ZVd5tExKi7b4rL GDdjuLOQekOyngfsMy6xhvW4PC8qoH2NEz/5pIsvLOSQuF+h6FUmbTSUtapjYLQKLuF1IY k6V5xSnlqPhqRHIEh6PqrDe33mj+Sqm0p2Nn0XCvZHPcmksLj+nXZVxIpbphOEwJdavDqD 3FGNQbWHVbNFYqWXceM/+xjVn+sJB/pBcW3StiJWt5VN0JcyZJlwt+2avzIxyh5cxDsCXi S59eyrPN1RVCllsxZxg/FVd6rO+6JSYJeuz+3cQAxR6NDmnHJddPcSrmmEfw2rqpHMxVu3 3T4gnM7jk1mroDureTfWAg56XQHZSx6QHtoSbENnmAMuffD0AE/I81wUp5oLe88A9XiQOW a5JaKQn9G4NpDP+oU37ow+47KVFijNoqZ0v2QJHe+pVVnUnwvq3lqAjLi9qw X-ME-Proxy: Feedback-ID: i9d664b8f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 29 Nov 2026 16:33:39 -0500 (EST) From: NeilBrown To: Alexander Viro , Christian Brauner Cc: David Howells , Marc Dionne , Jan Harkes , coda@cs.cmu.edu, Jan Kara , Richard Weinberger , Anton Ivanov , Johannes Berg , Jeff Layton , Amir Goldstein , linux-kernel@vger.kernel.org, codalist@coda.cs.cmu.edu, linux-fsdevel@vger.kernel.org, linux-um@lists.infradead.org, linux-afs@lists.infradead.org Subject: [PATCH v4 3/8] afs: don't unhash/rehash dentries during unlink/rename Date: Mon, 30 Nov 2026 08:21:39 +1100 Message-ID: <20261129213259.2837044-4-neilb@ownmail.net> X-Mailer: git-send-email 2.50.0.107.gf914562f5916.dirty In-Reply-To: <20261129213259.2837044-1-neilb@ownmail.net> References: <20261129213259.2837044-1-neilb@ownmail.net> Reply-To: NeilBrown X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: NeilBrown afs needs to block lookup of dentries during unlink and rename. There are two reasons: 1/ If the target is to be removed, not silly-renamed, the subsequent opens cannot be allowed as the file won't exist on the server, but they shouldn't fail until the unlink/rename completes. 2/ If the rename source is being moved between directories a lookup, particularly d_revalidate, might change ->d_time asynchronously with rename changing ->d_time with possible incorrect results. afs currently unhashes the dentry to force a lookup which will wait on the directory lock, and rehashes afterwards. This is incompatible with proposed changed to directory locking which will require a dentry to remain hashed throughout rename/unlink/etc operations. This patch copies a mechanism developed for NFS but uses the new DCACHE_PRIVATE flag rather then ->d_fsdata. DCACHE_PRIVATE is given the private name DCACHE_BLOCKED within afs. The flag is set when lookups and revalidates must be blocked. d_revalidate checks for this flag and waits for it to become be cleared. ->d_lock is used to ensure d_revalidate never updates the time stamp in ->d_fsdata while DCACHE_BLOCKED is set. It also avoids updating the timestamp if the d_parent has changed. For a ref-walk, ->d_lock provides all needed sequencing. It is taken in __d_lookup() to increment the refcount and is held by afs when checking the refcount and setting DCACHE_BLOCKED. d_revalidate() is called after __d_lookup() so it will see DCACHE_BLOCKED if needed without any extra barrier. For RCU-walk, d_revalidate() is called before refcount is incremented, so as well as setting DCACHE_BLOCKED we must write_seqcount_invalidate(&dentry->d_seq). If d_revalidate is called before that write_seqcount_invalidate(), then the RCU-walk will be retried as ref-walk. If it is called after, then DCACHE_BLOCKED will be seen providing we use smp_load_acquire(). The unlocking in afs_rename_edit_dir() is not needed. It was previously needed because it would not be safe to d_rehash() after calling d_move(). But now that we don't use d_rehash() that is not a concern. afs_rename_put() is always called and is sufficient for all rename unlocking. For rename, we can clear DCACHE_BLOCKED in afs_rename_put() as that will certainly be called. For unlink, afs_unlink_put() might not be called if op->ops is updated to afs_fetch_status_operation, so DCACHE_BLOCKED must be cleared in afs_unlink(). Signed-off-by: NeilBrown --- fs/afs/dir.c | 111 +++++++++++++++++++++++++++++++--------------- fs/afs/internal.h | 5 +-- 2 files changed, 77 insertions(+), 39 deletions(-) diff --git a/fs/afs/dir.c b/fs/afs/dir.c index 75f8f70cfeff..e76a610ce89a 100644 --- a/fs/afs/dir.c +++ b/fs/afs/dir.c @@ -49,6 +49,16 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir, static int afs_dir_writepages(struct address_space *mapping, struct writeback_control *wbc); +/* + * This is set to stop d_revalidate looking at, and possibly changing, + * ->d_fsdata (a timestamp) on a dentry which is being moved between + * directories, and to block lookup and possible open() for a dentry + * that is being removed without silly-rename. + * When set to block open()s, we also invalidate ->d_seq. + * When set only to protect ->d_fsdata, we don't need to invalidate ->d_seq. + */ +#define DCACHE_BLOCKED DCACHE_PRIVATE + const struct file_operations afs_dir_file_operations = { .open = afs_dir_open, .release = afs_release, @@ -1031,6 +1041,12 @@ static int afs_d_revalidate_rcu(struct afs_vnode *dvnode, struct dentry *dentry) if (!afs_check_validity(dvnode)) return -ECHILD; + /* Is a rename/unlink is pending? We must load d_flags *after* + * ->d_seq was sampled in path_init() or similar so use "acquire". + */ + if (smp_load_acquire(&dentry->d_flags) & DCACHE_BLOCKED) + return -ECHILD; + /* We only need to invalidate a dentry if the server's copy changed * behind our back. If we made the change, it's no problem. Note that * on a 32-bit system, we only have 32 bits in the dentry to store the @@ -1079,6 +1095,10 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name, if (IS_ERR(key)) key = NULL; + /* Wait for rename/unlink to complete */ +wait_for_rename: + wait_var_event(&dentry->d_flags, !(dentry->d_flags & DCACHE_BLOCKED)); + /* validate the parent directory */ ret = afs_validate(dir, key); if (ret == -ERESTARTSYS) { @@ -1158,7 +1178,14 @@ static int afs_d_revalidate(struct inode *parent_dir, const struct qstr *name, } out_valid: - dentry->d_fsdata = (void *)(unsigned long)dir_version; + spin_lock(&dentry->d_lock); + if (dentry->d_flags & DCACHE_BLOCKED) { + spin_unlock(&dentry->d_lock); + goto wait_for_rename; + } + if (dentry->d_parent->d_inode == parent_dir) + dentry->d_fsdata = (void *)(unsigned long)dir_version; + spin_unlock(&dentry->d_lock); out_valid_noupdate: key_put(key); _leave(" = 1 [valid]"); @@ -1530,20 +1557,12 @@ static void afs_unlink_edit_dir(struct afs_operation *op) fscache_end_operation(&cres); } -static void afs_unlink_put(struct afs_operation *op) -{ - _enter("op=%08x", op->debug_id); - if (op->unlink.need_rehash && afs_op_error(op) < 0 && afs_op_error(op) != -ENOENT) - d_rehash(op->dentry); -} - static const struct afs_operation_ops afs_unlink_operation = { .issue_afs_rpc = afs_fs_remove_file, .issue_yfs_rpc = yfs_fs_remove_file, .success = afs_unlink_success, .aborted = afs_check_for_remote_deletion, .edit_dir = afs_unlink_edit_dir, - .put = afs_unlink_put, }; /* @@ -1588,11 +1607,13 @@ static int afs_unlink(struct inode *dir, struct dentry *dentry) afs_op_set_error(op, afs_sillyrename(dvnode, vnode, dentry, op->key)); goto error; } - if (!d_unhashed(dentry)) { - /* Prevent a race with RCU lookup. */ - __d_drop(dentry); - op->unlink.need_rehash = true; - } + dentry->d_flags |= DCACHE_BLOCKED; + /* + * As RCU-walk calls d_revalidate() before incrementing d_count + * it may have already run. We need to invalidate d_seq so + * legitimize_path() will trigger a retry. + */ + write_seqcount_invalidate(&dentry->d_seq); spin_unlock(&dentry->d_lock); op->file[1].vnode = vnode; @@ -1603,6 +1624,11 @@ static int afs_unlink(struct inode *dir, struct dentry *dentry) afs_begin_vnode_operation(op); afs_wait_for_operation(op); + spin_lock(&dentry->d_lock); + store_release_wake_up(&dentry->d_flags, + dentry->d_flags &~ DCACHE_BLOCKED); + spin_unlock(&dentry->d_lock); + /* If there was a conflict with a third party, check the status of the * unlinked vnode. */ @@ -1899,11 +1925,6 @@ static void afs_rename_edit_dir(struct afs_operation *op) _enter("op=%08x", op->debug_id); - if (op->rename.rehash) { - d_rehash(op->rename.rehash); - op->rename.rehash = NULL; - } - fscache_begin_write_operation(&orig_cres, afs_vnode_cache(orig_dvnode)); if (new_dvnode != orig_dvnode) fscache_begin_write_operation(&new_cres, afs_vnode_cache(new_dvnode)); @@ -2023,11 +2044,18 @@ static void afs_rename_exchange_edit_dir(struct afs_operation *op) static void afs_rename_put(struct afs_operation *op) { _enter("op=%08x", op->debug_id); - if (op->rename.rehash) - d_rehash(op->rename.rehash); + if (op->rename.unblock) { + spin_lock(&op->rename.unblock->d_lock); + store_release_wake_up(&op->rename.unblock->d_flags, + op->rename.unblock->d_flags &~ DCACHE_BLOCKED); + spin_unlock(&op->rename.unblock->d_lock); + op->rename.unblock = NULL; + } + spin_lock(&op->dentry->d_lock); + store_release_wake_up(&op->dentry->d_flags, + op->dentry->d_flags &~ DCACHE_BLOCKED); + spin_unlock(&op->dentry->d_lock); dput(op->rename.tmp); - if (afs_op_error(op)) - d_rehash(op->dentry); } static const struct afs_operation_ops afs_rename_operation = { @@ -2135,7 +2163,11 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir, op->ops = &afs_rename_noreplace_operation; } else if (flags & RENAME_EXCHANGE) { op->ops = &afs_rename_exchange_operation; - d_drop(new_dentry); + /* Block revalidate on new_dentry until rename completes */ + spin_lock(&new_dentry->d_lock); + new_dentry->d_flags |= DCACHE_BLOCKED; + op->rename.unblock = new_dentry; + spin_unlock(&new_dentry->d_lock); } else { /* If we might displace the target, we might need to do silly * rename. @@ -2148,15 +2180,10 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir, * and becomes the new target. */ if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { - /* To prevent any new references to the target during - * the rename, we unhash the dentry in advance. - */ - if (!d_unhashed(new_dentry)) { - d_drop(new_dentry); - op->rename.rehash = new_dentry; - } + spin_lock(&new_dentry->d_lock); if (d_count(new_dentry) > 2) { + spin_unlock(&new_dentry->d_lock); /* copy the target dentry's name */ op->rename.tmp = d_alloc(new_dentry->d_parent, &new_dentry->d_name); @@ -2174,8 +2201,21 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir, } op->dentry_2 = op->rename.tmp; - op->rename.rehash = NULL; op->rename.new_negative = true; + } else { + /* + * To prevent any new references to the + * target during the rename, we set + * DCACHE_BLOCKED which afs_d_revalidate + * will wait for. d_lock ensures + * d_count() and DCACHE_BLOCKED are + * consistent, and d_seq ensure RCU-walk + * is retried. + */ + new_dentry->d_flags |= DCACHE_BLOCKED; + write_seqcount_invalidate(&new_dentry->d_seq); + op->rename.unblock = new_dentry; + spin_unlock(&new_dentry->d_lock); } } } @@ -2186,10 +2226,11 @@ static int afs_rename(const struct mnt_idmap *idmap, struct inode *old_dir, * d_revalidate may see old_dentry between the op having taken place * and the version being updated. * - * So drop the old_dentry for now to make other threads go through - * lookup instead - which we hold a lock against. + * So block revalidate on the old_dentry until the rename completes. */ - d_drop(old_dentry); + spin_lock(&old_dentry->d_lock); + old_dentry->d_flags |= DCACHE_BLOCKED; + spin_unlock(&old_dentry->d_lock); ret = afs_do_sync_operation(op); if (ret == -ENOTSUPP) diff --git a/fs/afs/internal.h b/fs/afs/internal.h index 7cea5ba0d20d..578fe7d35ab4 100644 --- a/fs/afs/internal.h +++ b/fs/afs/internal.h @@ -901,10 +901,7 @@ struct afs_operation { struct afs_symlink *symlink; } create; struct { - bool need_rehash; - } unlink; - struct { - struct dentry *rehash; + struct dentry *unblock; struct dentry *tmp; unsigned int rename_flags; bool new_negative; -- 2.50.0.107.gf914562f5916.dirty