From patchwork Fri Jun 6 13:07:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 4311731 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 3989ABEEAA for ; Fri, 6 Jun 2014 13:07:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 563BE2015A for ; Fri, 6 Jun 2014 13:07:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6A4722020A for ; Fri, 6 Jun 2014 13:07:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751494AbaFFNHV (ORCPT ); Fri, 6 Jun 2014 09:07:21 -0400 Received: from mail-qa0-f47.google.com ([209.85.216.47]:60952 "EHLO mail-qa0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751152AbaFFNHU (ORCPT ); Fri, 6 Jun 2014 09:07:20 -0400 Received: by mail-qa0-f47.google.com with SMTP id s7so3683894qap.34 for ; Fri, 06 Jun 2014 06:07:19 -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=kuCI+MH+HPMJnW7gnd0fNg0BlqDQZtIoA3bv+dZnnq0=; b=Zi/cWK4Z8ox7CR1gDA9VAWQCcH9fBvL6lp9OGmzzwHDFMPog9ytZzQJDzIQFYFfLAh S2CFnjNaMBzHdvrwxTo/uCcf6iEsb/FuVt7qmKSzirXBjw2d7HVoLCZ/YA+qRSjd9a7K z8vwRgjdtw56wnk6edLhvysNDJzWkqAP0SoMrHTpu9y+gqKYf9K8RCL7kYnPe4NUH72p KS/J/FQE1UF6wb84jOP84405odFljLeUplTl+WR1wk1GtznNJ3ldGstqjxTXFIHZ9JAF R6uXQo65ci1rR4Xxai1q2Hu3qkOl8nMV1nTcfsDVlRI+G/ZLZx9pcjaCRZCgW12VdeXR 834Q== X-Gm-Message-State: ALoCoQkJnrGj+yu93r9OdWcqcQiq87oFz350qzOsoSnny4jFmLBY6E+gS+o3f7vLDCFbLXCwQggM X-Received: by 10.224.166.201 with SMTP id n9mr8227030qay.95.1402060038511; Fri, 06 Jun 2014 06:07:18 -0700 (PDT) Received: from tlielax.poochiereds.net ([2001:470:8:d63:3a60:77ff:fe93:a95d]) by mx.google.com with ESMTPSA id 5sm5759391qgi.45.2014.06.06.06.07.16 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Jun 2014 06:07:17 -0700 (PDT) From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org, trond.myklebust@primarydata.com, hch@infradead.org Subject: [PATCH v2 1/2] nfsd: Protect addition to the file_hashtbl Date: Fri, 6 Jun 2014 09:07:05 -0400 Message-Id: <1402060026-26511-2-git-send-email-jlayton@primarydata.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1402060026-26511-1-git-send-email-jlayton@primarydata.com> References: <1402060026-26511-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=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, 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 From: Trond Myklebust Ensure that we only can have a single struct nfs4_file per inode in the file_hashtbl and make addition atomic with respect to lookup. To prevent an i_lock/state_lock inversion, change nfsd4_init_file to use ihold instead if igrab. That's also more efficient anyway as we definitely hold a reference to the inode at that point. Signed-off-by: Trond Myklebust Signed-off-by: Jeff Layton Reviewed-by: Christoph Hellwig --- fs/nfsd/nfs4state.c | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index a500033a2f87..cbec573e9445 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -2519,17 +2519,18 @@ static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino) { unsigned int hashval = file_hashval(ino); + lockdep_assert_held(&state_lock); + atomic_set(&fp->fi_ref, 1); INIT_LIST_HEAD(&fp->fi_stateids); INIT_LIST_HEAD(&fp->fi_delegations); - fp->fi_inode = igrab(ino); + ihold(ino); + fp->fi_inode = ino; fp->fi_had_conflict = false; fp->fi_lease = NULL; memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); memset(fp->fi_access, 0, sizeof(fp->fi_access)); - spin_lock(&state_lock); hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]); - spin_unlock(&state_lock); } void @@ -2695,23 +2696,49 @@ find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, /* search file_hashtbl[] for file */ static struct nfs4_file * -find_file(struct inode *ino) +find_file_locked(struct inode *ino) { unsigned int hashval = file_hashval(ino); struct nfs4_file *fp; - spin_lock(&state_lock); + lockdep_assert_held(&state_lock); + hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) { if (fp->fi_inode == ino) { get_nfs4_file(fp); - spin_unlock(&state_lock); return fp; } } - spin_unlock(&state_lock); return NULL; } +static struct nfs4_file * +find_file(struct inode *ino) +{ + struct nfs4_file *fp; + + spin_lock(&state_lock); + fp = find_file_locked(ino); + spin_unlock(&state_lock); + return fp; +} + +static struct nfs4_file * +find_or_add_file(struct inode *ino, struct nfs4_file *new) +{ + struct nfs4_file *fp; + + spin_lock(&state_lock); + fp = find_file_locked(ino); + if (fp == NULL) { + nfsd4_init_file(new, ino); + fp = new; + } + spin_unlock(&state_lock); + + return fp; +} + /* * Called to check deny when READ with all zero stateid or * WRITE with all zero or all one stateid @@ -3230,21 +3257,19 @@ nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nf * and check for delegations in the process of being recalled. * If not found, create the nfs4_file struct */ - fp = find_file(ino); - if (fp) { + fp = find_or_add_file(ino, open->op_file); + if (fp != open->op_file) { if ((status = nfs4_check_open(fp, open, &stp))) goto out; status = nfs4_check_deleg(cl, open, &dp); if (status) goto out; } else { + open->op_file = NULL; status = nfserr_bad_stateid; if (nfsd4_is_deleg_cur(open)) goto out; status = nfserr_jukebox; - fp = open->op_file; - open->op_file = NULL; - nfsd4_init_file(fp, ino); } /*