From patchwork Mon Jun 30 15:49:16 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 4451561 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 7F381BEECB for ; Mon, 30 Jun 2014 15:51:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 86F3120225 for ; Mon, 30 Jun 2014 15:51:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8EC4C20375 for ; Mon, 30 Jun 2014 15:51:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754706AbaF3Pvo (ORCPT ); Mon, 30 Jun 2014 11:51:44 -0400 Received: from mail-qa0-f46.google.com ([209.85.216.46]:60647 "EHLO mail-qa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754699AbaF3Pvm (ORCPT ); Mon, 30 Jun 2014 11:51:42 -0400 Received: by mail-qa0-f46.google.com with SMTP id i13so6428915qae.5 for ; Mon, 30 Jun 2014 08:51:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=C/jOtjX7LRr8bzvNm+RSse6znitkiUb8UGNt2e+aK+M=; b=EZOFNpjdhZkig4xMkmmmeFIr/NQZ1YJvviTKFkv/0+7uB0VDaJyTMLABH7UFyYJx8u Clojsho5dTAsjQTtDEp7h0l8N9ZMeqJq9OFBpMPnBE2s1niu6LyE1wPdurspFhvjIo9F 77F2UA+cGBYD3EsktbxdCGzGuaJ3ntgj/EOrKKadgYCfh63aw53ipTufGd4ByHDNJ0OX ms4jFacDoB9jLfcDDmpk8gpVw+mwHbePQugEVTJbDoWuuAFmn+NUkr9RmfhT+MN3kolK WH/mXKsmLA9Ef/WGtKeGJF5NJeD2nEe/rrgxHQGtWtdJ2PSOxHj9jFa3KGzlj6jdgcWO tR0A== X-Gm-Message-State: ALoCoQm3i7sexqnVDK54SYJEoxKu/vPjByaxGHiaOfvupg4cKY21aSv6X6ZuFxkzGd2+r3ebZlIT X-Received: by 10.140.44.101 with SMTP id f92mr56364525qga.89.1404143498811; Mon, 30 Jun 2014 08:51:38 -0700 (PDT) Received: from tlielax.poochiereds.net ([2001:470:8:d63:3a60:77ff:fe93:a95d]) by mx.google.com with ESMTPSA id m1sm32584105qaz.27.2014.06.30.08.51.37 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Jun 2014 08:51:38 -0700 (PDT) From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org Subject: [PATCH v3 047/114] nfsd: ensure atomicity in nfsd4_free_stateid and nfsd4_validate_stateid Date: Mon, 30 Jun 2014 11:49:16 -0400 Message-Id: <1404143423-24381-48-git-send-email-jlayton@primarydata.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1404143423-24381-1-git-send-email-jlayton@primarydata.com> References: <1404143423-24381-1-git-send-email-jlayton@primarydata.com> Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Hold the cl_lock over the bulk of these functions. In addition to ensuring that they aren't freed prematurely, this will also help prevent a potential race that could be introduced later. Once we remove the client_mutex, it'll be possible for FREE_STATEID and CLOSE to race and for both to try to put the "persistent" reference to the stateid. Signed-off-by: Trond Myklebust --- fs/nfsd/nfs4state.c | 67 ++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index 54c9631a602c..e56071ef945a 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1671,16 +1671,6 @@ find_stateid_locked(struct nfs4_client *cl, stateid_t *t) return ret; } -static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t) -{ - struct nfs4_stid *ret; - - spin_lock(&cl->cl_lock); - ret = find_stateid_locked(cl, t); - spin_unlock(&cl->cl_lock); - return ret; -} - static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) { struct nfs4_stid *s; @@ -4051,10 +4041,10 @@ static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) { struct nfs4_stid *s; struct nfs4_ol_stateid *ols; - __be32 status; + __be32 status = nfserr_bad_stateid; if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) - return nfserr_bad_stateid; + return status; /* Client debugging aid. */ if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { char addr_str[INET6_ADDRSTRLEN]; @@ -4062,34 +4052,42 @@ static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) sizeof(addr_str)); pr_warn_ratelimited("NFSD: client %s testing state ID " "with incorrect client ID\n", addr_str); - return nfserr_bad_stateid; + return status; } - s = find_stateid(cl, stateid); + spin_lock(&cl->cl_lock); + s = find_stateid_locked(cl, stateid); if (!s) - return nfserr_bad_stateid; + goto out_unlock; status = check_stateid_generation(stateid, &s->sc_stateid, 1); if (status) - return status; + goto out_unlock; switch (s->sc_type) { case NFS4_DELEG_STID: - return nfs_ok; + status = nfs_ok; + break; case NFS4_REVOKED_DELEG_STID: - return nfserr_deleg_revoked; + status = nfserr_deleg_revoked; + break; case NFS4_OPEN_STID: case NFS4_LOCK_STID: ols = openlockstateid(s); if (ols->st_stateowner->so_is_open_owner && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) - return nfserr_bad_stateid; - return nfs_ok; + status = nfserr_bad_stateid; + else + status = nfs_ok; + break; default: printk("unknown stateid type %x\n", s->sc_type); /* Fallthrough */ case NFS4_CLOSED_STID: case NFS4_CLOSED_DELEG_STID: - return nfserr_bad_stateid; + status = nfserr_bad_stateid; } +out_unlock: + spin_unlock(&cl->cl_lock); + return status; } static __be32 @@ -4240,31 +4238,36 @@ nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, __be32 ret = nfserr_bad_stateid; nfs4_lock_state(); - s = find_stateid(cl, stateid); + spin_lock(&cl->cl_lock); + s = find_stateid_locked(cl, stateid); if (!s) - goto out; + goto out_unlock; switch (s->sc_type) { case NFS4_DELEG_STID: ret = nfserr_locks_held; - goto out; + break; case NFS4_OPEN_STID: case NFS4_LOCK_STID: ret = check_stateid_generation(stateid, &s->sc_stateid, 1); if (ret) - goto out; - if (s->sc_type == NFS4_LOCK_STID) - ret = nfsd4_free_lock_stateid(openlockstateid(s)); - else + break; + if (s->sc_type != NFS4_LOCK_STID) { ret = nfserr_locks_held; - break; + break; + } + spin_unlock(&cl->cl_lock); + ret = nfsd4_free_lock_stateid(openlockstateid(s)); + goto out; case NFS4_REVOKED_DELEG_STID: + spin_unlock(&cl->cl_lock); dp = delegstateid(s); destroy_revoked_delegation(dp); ret = nfs_ok; - break; - default: - ret = nfserr_bad_stateid; + goto out; + /* Default falls through and returns nfserr_bad_stateid */ } +out_unlock: + spin_unlock(&cl->cl_lock); out: nfs4_unlock_state(); return ret;