From nobody Wed Oct 8 23:09:10 2025 Received: from mail.nfschina.com (unknown [42.101.60.213]) by smtp.subspace.kernel.org (Postfix) with SMTP id D224C1A8412; Mon, 23 Jun 2025 12:53:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=42.101.60.213 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750683195; cv=none; b=LqT2OBAOiNQoVYBoxGPandtlWCLpqiPp+H156oSrYX6bEKG2DaCFEh5li088kHOERIZut0YsLBvPXne6WjDByiKpbrSjvj5sUtEbk58VeZj+bwfdqKiQ7bGe5nYSNoMEYdktsYdE+V/kK90U0sCdkvxh8+H5vGQs4EmBHRrB/9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750683195; c=relaxed/simple; bh=0g50SqWc5g3rJvoZPAxV4yQCt2rJxE4bYMCSo9EOwwA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=gkEOJvLeB+Zrfpr02/cN6OtpOK2RHns/DzSaSGfIKF38swlUj+ELBPgaKvfhPO+WZfMJ+CXDNP/dQtYlWa6n0W8uSCghQ2mk/9cVQnAIjVa+JPKCreAfZ4+lpk90634qHJoX+inIPlPZB6kDRBJLOiApf+fIebKPYd+ic8tAWos= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=nfschina.com; spf=pass smtp.mailfrom=nfschina.com; arc=none smtp.client-ip=42.101.60.213 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=nfschina.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=nfschina.com Received: from longsh.shanghai.nfschina.local (unknown [180.167.10.98]) by mail.nfschina.com (MailData Gateway V2.8.8) with ESMTPSA id 81B0F60307155; Mon, 23 Jun 2025 20:52:59 +0800 (CST) X-MD-Sfrom: suhui@nfschina.com X-MD-SrcIP: 180.167.10.98 From: Su Hui To: trondmy@kernel.org, anna@kernel.org Cc: Su Hui , linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org Subject: [PATCH] NFS: Using guard() to simplify lock/unlock code Date: Mon, 23 Jun 2025 20:52:53 +0800 Message-Id: <20250623125253.3797131-1-suhui@nfschina.com> X-Mailer: git-send-email 2.30.2 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Using guard() to replace *unlock* label. guard() is better than goto unlock patterns and is more clear. No functional changes. Signed-off-by: Su Hui --- fs/nfs/callback_proc.c | 169 ++++++++++++++++++++--------------------- 1 file changed, 82 insertions(+), 87 deletions(-) diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c index 8397c43358bd..ae7635f88f35 100644 --- a/fs/nfs/callback_proc.c +++ b/fs/nfs/callback_proc.c @@ -264,47 +264,43 @@ static u32 initiate_file_draining(struct nfs_client *= clp, =20 pnfs_layoutcommit_inode(ino, false); =20 + scoped_guard(spinlock, &ino->i_lock) { + lo =3D NFS_I(ino)->layout; + if (!lo) + goto out; + pnfs_get_layout_hdr(lo); + rv =3D pnfs_check_callback_stateid(lo, &args->cbl_stateid, cps); + if (rv !=3D NFS_OK) + break; =20 - spin_lock(&ino->i_lock); - lo =3D NFS_I(ino)->layout; - if (!lo) { - spin_unlock(&ino->i_lock); - goto out; - } - pnfs_get_layout_hdr(lo); - rv =3D pnfs_check_callback_stateid(lo, &args->cbl_stateid, cps); - if (rv !=3D NFS_OK) - goto unlock; - - /* - * Enforce RFC5661 Section 12.5.5.2.1.5 (Bulk Recall and Return) - */ - if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { - rv =3D NFS4ERR_DELAY; - goto unlock; - } - - pnfs_set_layout_stateid(lo, &args->cbl_stateid, NULL, true); - switch (pnfs_mark_matching_lsegs_return(lo, &free_me_list, - &args->cbl_range, - be32_to_cpu(args->cbl_stateid.seqid))) { - case 0: - case -EBUSY: - /* There are layout segments that need to be returned */ - rv =3D NFS4_OK; - break; - case -ENOENT: - set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags); - /* Embrace your forgetfulness! */ - rv =3D NFS4ERR_NOMATCHING_LAYOUT; + /* + * Enforce RFC5661 Section 12.5.5.2.1.5 (Bulk Recall and Return) + */ + if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) { + rv =3D NFS4ERR_DELAY; + break; + } =20 - if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) { - NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, - &args->cbl_range); + pnfs_set_layout_stateid(lo, &args->cbl_stateid, NULL, true); + switch (pnfs_mark_matching_lsegs_return(lo, &free_me_list, + &args->cbl_range, + be32_to_cpu(args->cbl_stateid.seqid))) { + case 0: + case -EBUSY: + /* There are layout segments that need to be returned */ + rv =3D NFS4_OK; + break; + case -ENOENT: + set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags); + /* Embrace your forgetfulness! */ + rv =3D NFS4ERR_NOMATCHING_LAYOUT; + + if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) { + NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, + &args->cbl_range); + } } } -unlock: - spin_unlock(&ino->i_lock); pnfs_free_lseg_list(&free_me_list); /* Free all lsegs that are attached to commit buckets */ nfs_commit_inode(ino, 0); @@ -524,62 +520,61 @@ __be32 nfs4_callback_sequence(void *argp, void *resp, res->csr_sequenceid =3D args->csa_sequenceid; res->csr_slotid =3D args->csa_slotid; =20 - spin_lock(&tbl->slot_tbl_lock); - /* state manager is resetting the session */ - if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) { - status =3D htonl(NFS4ERR_DELAY); - /* Return NFS4ERR_BADSESSION if we're draining the session - * in order to reset it. - */ - if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) - status =3D htonl(NFS4ERR_BADSESSION); - goto out_unlock; - } + scoped_guard(spinlock, &tbl->slot_tbl_lock) { + /* state manager is resetting the session */ + if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) { + status =3D htonl(NFS4ERR_DELAY); + /* Return NFS4ERR_BADSESSION if we're draining the session + * in order to reset it. + */ + if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) + status =3D htonl(NFS4ERR_BADSESSION); + break; + } =20 - status =3D htonl(NFS4ERR_BADSLOT); - slot =3D nfs4_lookup_slot(tbl, args->csa_slotid); - if (IS_ERR(slot)) - goto out_unlock; + status =3D htonl(NFS4ERR_BADSLOT); + slot =3D nfs4_lookup_slot(tbl, args->csa_slotid); + if (IS_ERR(slot)) + break; =20 - res->csr_highestslotid =3D tbl->server_highest_slotid; - res->csr_target_highestslotid =3D tbl->target_highest_slotid; + res->csr_highestslotid =3D tbl->server_highest_slotid; + res->csr_target_highestslotid =3D tbl->target_highest_slotid; =20 - status =3D validate_seqid(tbl, slot, args); - if (status) - goto out_unlock; - if (!nfs4_try_to_lock_slot(tbl, slot)) { - status =3D htonl(NFS4ERR_DELAY); - goto out_unlock; - } - cps->slot =3D slot; + status =3D validate_seqid(tbl, slot, args); + if (status) + break; + if (!nfs4_try_to_lock_slot(tbl, slot)) { + status =3D htonl(NFS4ERR_DELAY); + break; + } + cps->slot =3D slot; =20 - /* The ca_maxresponsesize_cached is 0 with no DRC */ - if (args->csa_cachethis !=3D 0) { - status =3D htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE); - goto out_unlock; - } + /* The ca_maxresponsesize_cached is 0 with no DRC */ + if (args->csa_cachethis !=3D 0) { + status =3D htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE); + break; + } =20 - /* - * Check for pending referring calls. If a match is found, a - * related callback was received before the response to the original - * call. - */ - ret =3D referring_call_exists(clp, args->csa_nrclists, args->csa_rclists, - &tbl->slot_tbl_lock); - if (ret < 0) { - status =3D htonl(NFS4ERR_DELAY); - goto out_unlock; - } - cps->referring_calls =3D ret; + /* + * Check for pending referring calls. If a match is found, a + * related callback was received before the response to the original + * call. + */ + ret =3D referring_call_exists(clp, args->csa_nrclists, args->csa_rclists, + &tbl->slot_tbl_lock); + if (ret < 0) { + status =3D htonl(NFS4ERR_DELAY); + break; + } + cps->referring_calls =3D ret; =20 - /* - * RFC5661 20.9.3 - * If CB_SEQUENCE returns an error, then the state of the slot - * (sequence ID, cached reply) MUST NOT change. - */ - slot->seq_nr =3D args->csa_sequenceid; -out_unlock: - spin_unlock(&tbl->slot_tbl_lock); + /* + * RFC5661 20.9.3 + * If CB_SEQUENCE returns an error, then the state of the slot + * (sequence ID, cached reply) MUST NOT change. + */ + slot->seq_nr =3D args->csa_sequenceid; + } =20 out: cps->clp =3D clp; /* put in nfs4_callback_compound */ --=20 2.30.2