Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5722723rwb; Tue, 1 Aug 2023 07:07:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlHrUNFPR8HBRem2ecZuHDFY3N7Bpzkf8h9Dxmbru1GOYk/poWBVQPf37bY03UIrVL3XvZDI X-Received: by 2002:a17:906:95:b0:992:7329:fe4f with SMTP id 21-20020a170906009500b009927329fe4fmr2658015ejc.73.1690898821485; Tue, 01 Aug 2023 07:07:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690898821; cv=none; d=google.com; s=arc-20160816; b=DJl/G+chVN0/7smFIx/V4uG2ohGn1lZskZNMf7GNkvFTR7PCjHAi3tVMXnVo1+0Sq6 t0DVmdXG49VXhjHng+J+ONSmhvTNbOrIy+C7ponf4ShjPHC21AD4nAFHK5x6Knvuj7XX MaydS2eQ7zjZemEcgeoTwixzYsGBunOhxHLFbcDSSPxNKYbq2exc2JGDNd6jhdvN7610 BNGICoMIhqt/K6M30nuo6hRCcDDsnKntrNrYCUxr5O7ksJIobAn8IRaQvz7Jo8Bcvwxv gF4NaA6GGHrq5P9BEwlJUOaJE6lLWTX69OyImwCI8sAlmx4nZzXhIyjEnkB1Gz9pLysU sngw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:message-id:content-transfer-encoding :mime-version:subject:date:from:dkim-signature; bh=y2s0dWwckvCsyY8FI8FUqzYaSXPsNF/BPrkveAQpc+M=; fh=7kqWyKiIQpgkLC2sS92h74MY/UjqV8MeZaswkbSb4vE=; b=LKUpt4vFSRQwflAiwMCUJjOHmvKVEW4IiMEdiqcKvZ8ZhZCvk5i8H5A81Ibc7teYhZ jtg6ootJhbttVEzK+X9id7jWDIEg4fp2gaNQcs1ABjmI6cg0LjHlPIgKhNmTjjcCa/Ff 76gYpE2v4bc7pwBh6DvGdRC249xdmObQ5BSGzeBk3APIH+fiAiynBn5S0RjdssNVctl/ 9ljpfLtqqgaNtrY+VgsupVDOfccwmQ0Vdr8HhysMPzbtGGPsqZPFhcZPok+LpxmLDhbl ftZ9eZ37cKS5A8sd2pgmAqt7WdqFT6M+WGBjyu3L/KV5rcecmS1duTot5L78eDLDTus9 9hfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iFu9TKbc; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gl23-20020a170906e0d700b0099bcf69f506si262804ejb.202.2023.08.01.07.05.52; Tue, 01 Aug 2023 07:07:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iFu9TKbc; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234034AbjHANdR (ORCPT + 99 others); Tue, 1 Aug 2023 09:33:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234451AbjHANdM (ORCPT ); Tue, 1 Aug 2023 09:33:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A53E1BDB; Tue, 1 Aug 2023 06:33:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0FEDC615A4; Tue, 1 Aug 2023 13:33:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC329C433C7; Tue, 1 Aug 2023 13:33:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690896790; bh=qW9IP+djYLOwZukIeqaHto6GDgMjZwheXduDcYTrR/c=; h=From:Date:Subject:To:Cc:From; b=iFu9TKbcb3PzeiZVSO6f9KNXrcQ2wDLU8Z/igj3oTts8ZzyTz6CYgAjBYty8yO4mt a9450h9QLAurU11lxSvBWojiK/h6w1U1KprjTILupy8al6VQZ+0dsb5n7Xs2z78y/N /Wyfq1aTY2wHoqd/M3ZaHUpoLm+L75/YM6xWDI6IYEQbxsTDlNj1R+eiCsOrKqEsWA EFUPNU+HmoqMzj6HHe2yjapybiou+ik+zVI2GCyqMTZ04xIWX8Ri9uJT37DMa5lbFf ugNkBMwAWrX4dwxcSdhWTVOvxrsFRSB6V4XnfUik94SRHKbjMWnmCurT5VWUSUa0T/ ArNIhYU9fAD9g== From: Jeff Layton Date: Tue, 01 Aug 2023 09:33:06 -0400 Subject: [PATCH v2] nfsd: don't hand out write delegations on O_WRONLY opens MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230801-wdeleg-v2-1-20c14252bab4@kernel.org> X-B4-Tracking: v=1; b=H4sIAJIJyWQC/2XMQQ7CIBCF4as0sxbTgYjGlfdouigw0IkNNWBQ0 3B3sVuX/8vLt0GmxJTh2m2QqHDmNbaQhw7sPMVAgl1rkL1U/VmheDlaKAhjnNFGniZlNbTzI5H n9w4NY+uZ83NNn90t+Fv/iIIChb94QkuIRunbnVKk5bimAGOt9QtQKItCnQAAAA== To: Chuck Lever , Neil Brown , Olga Kornievskaia , Dai Ngo , Tom Talpey Cc: linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org, Jeff Layton X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3422; i=jlayton@kernel.org; h=from:subject:message-id; bh=qW9IP+djYLOwZukIeqaHto6GDgMjZwheXduDcYTrR/c=; b=owEBbQKS/ZANAwAIAQAOaEEZVoIVAcsmYgBkyQmVvF49eJYxC8Cze43AUKz+wTs72Hw99FIhD H/3ISU/H/2JAjMEAAEIAB0WIQRLwNeyRHGyoYTq9dMADmhBGVaCFQUCZMkJlQAKCRAADmhBGVaC FUDvEACPOE0efmtrAtjPvrN6+mM0+nCautiP4Et5TfR5vMua2dkdW7Jj+uH9C/ortYSO2HZ1cCM OLNoW6ZR5ehyrc+Xo8g8uW2AyrRe6eCravWjeHFQErTf6Mm3RriI6cSHeBJ05uE/AwPbLqUCZJ6 KDPMOU+0Ebr8q9EVvnLMqtkbD0ePEsroFB0UdNmuKKuBH72Ae0CwI+CgZA8s9SGbWXs7mTqmMnE P9co02/XQNX9UgxP1/MopRHJPrpuSFzmVxP6feomQvX242lRmhZrOyjLkiNRbEIhrFuuN1frrBS aFvRTIw9imnVApsTp9xDQz9YQNxh7yzQLHw93SP5GzEaiu3cNAmf8bq4EqBqWKRt2H4S7RtIwMS +FUSu9CXj9GueO/mQOpoIEaCI3PaQRR+HkQMD43dKAGVKpRrYufcv1TLNYa8JeWHkzTAunuc4vc 5iefJdp9dOJAu4hVI++PqwHAYkpwjV/p3OuboJu5J+9V43hHBrwFuJ1Z/UDizkqLyF0q4zci/Zg dsDbuoYTlibqqYIrrBKU10Sy23OUjmesOLK7HfpFRGLwrA9Qq75caQLh/ULQs5Ifw06zHAF9zve 8m10Jz6cMTn4aHucUORJTOZODpNVmGT07lgbKjusM5T0ojGjDg7Or9i9iV2usJtDRczyHJOKfJf TluOvGrnosstehQ== X-Developer-Key: i=jlayton@kernel.org; a=openpgp; fpr=4BC0D7B24471B2A184EAF5D3000E684119568215 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org I noticed that xfstests generic/001 was failing against linux-next nfsd. The client would request a OPEN4_SHARE_ACCESS_WRITE open, and the server would hand out a write delegation. The client would then try to use that write delegation as the source stateid in a COPY or CLONE operation, and the server would respond with NFS4ERR_STALE. The problem is that the struct file associated with the delegation does not necessarily have read permissions. It's handing out a write delegation on what is effectively an O_WRONLY open. RFC 8881 states: "An OPEN_DELEGATE_WRITE delegation allows the client to handle, on its own, all opens." Given that the client didn't request any read permissions, and that nfsd didn't check for any, it seems wrong to give out a write delegation. Only hand out a write delegation if we have a O_RDWR descriptor available. If it fails to find an appropriate write descriptor, go ahead and try for a read delegation if NFS4_SHARE_ACCESS_READ was requested. This fixes xfstest generic/001. Closes: https://bugzilla.linux-nfs.org/show_bug.cgi?id=412 Signed-off-by: Jeff Layton --- Changes in v2: - Rework the logic when finding struct file for the delegation. The earlier patch might still have attached a O_WRONLY file to the deleg in some cases, and could still have handed out a write delegation on an O_WRONLY OPEN request in some cases. --- fs/nfsd/nfs4state.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index ef7118ebee00..e79d82fd05e7 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -5449,7 +5449,7 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, struct nfs4_file *fp = stp->st_stid.sc_file; struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate; struct nfs4_delegation *dp; - struct nfsd_file *nf; + struct nfsd_file *nf = NULL; struct file_lock *fl; u32 dl_type; @@ -5461,21 +5461,28 @@ nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp, if (fp->fi_had_conflict) return ERR_PTR(-EAGAIN); - if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) { - nf = find_writeable_file(fp); + /* + * Try for a write delegation first. We need an O_RDWR file + * since a write delegation allows the client to perform any open + * from its cache. + */ + if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) { + nf = nfsd_file_get(fp->fi_fds[O_RDWR]); dl_type = NFS4_OPEN_DELEGATE_WRITE; - } else { + } + + /* + * If the file is being opened O_RDONLY or we couldn't get a O_RDWR + * file for some reason, then try for a read deleg instead. + */ + if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) { nf = find_readable_file(fp); dl_type = NFS4_OPEN_DELEGATE_READ; } - if (!nf) { - /* - * We probably could attempt another open and get a read - * delegation, but for now, don't bother until the - * client actually sends us one. - */ + + if (!nf) return ERR_PTR(-EAGAIN); - } + spin_lock(&state_lock); spin_lock(&fp->fi_lock); if (nfs4_delegation_exists(clp, fp)) --- base-commit: a734662572708cf062e974f659ae50c24fc1ad17 change-id: 20230731-wdeleg-bbdb6b25a3c6 Best regards, -- Jeff Layton