From patchwork Thu Aug 20 11:17:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 7043751 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 1E7569F695 for ; Thu, 20 Aug 2015 11:17:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 02B10205C2 for ; Thu, 20 Aug 2015 11:17:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C647D205C4 for ; Thu, 20 Aug 2015 11:17:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751875AbbHTLRo (ORCPT ); Thu, 20 Aug 2015 07:17:44 -0400 Received: from mail-yk0-f169.google.com ([209.85.160.169]:34859 "EHLO mail-yk0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751863AbbHTLRm (ORCPT ); Thu, 20 Aug 2015 07:17:42 -0400 Received: by ykbi184 with SMTP id i184so33655682ykb.2 for ; Thu, 20 Aug 2015 04:17:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HspWL3G/+3LwXvY43oqTyhcaVDQ90wkGZh60+85hLH4=; b=jkNgU5EgKF1wY+lR8CxCYXgy5ID/65jjPIVRm8ubwHjHUQO7cAteXgp+ZSImZApUYf ow31qspFskPuOOLtQT8/rBnYKKZC7YRe0H3+zKrZr4j3XOE+rBACWdUShypXKvBz8ub3 bcKx0ObF/B4Tf8L9ggqAUG8fpwzoPkYgh4dcFMnY81Cm+zatvcvYxZ1upi7bhN4FLhRU nitJ6eph4jGJObe54p4e/sHsX64UElWRE9R0SFaZ70QE36EawJqTpIzoOvereaIflJft OKuD+e5cfpW7FPTzihAzK8KMTOp9roz2fvUWC8v8+sa0lsbi4x08xrOaqY+t9OjLpj0M pDFg== X-Gm-Message-State: ALoCoQnie5F0H13zQIe7+VkFOS9ntSge2of3UXbrGC1AoiPrcEVpLDmkuFeFkJ0Go2aHelXB569Z X-Received: by 10.170.140.87 with SMTP id h84mr2433530ykc.123.1440069460965; Thu, 20 Aug 2015 04:17:40 -0700 (PDT) Received: from tlielax.poochiereds.net (cpe-45-37-211-243.nc.res.rr.com. [45.37.211.243]) by smtp.googlemail.com with ESMTPSA id k66sm3637319ywe.0.2015.08.20.04.17.40 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Aug 2015 04:17:40 -0700 (PDT) From: Jeff Layton X-Google-Original-From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org, hch@lst.de, kinglongmee@gmail.com Subject: [PATCH v3 15/20] nfsd: call flush_delayed_fput from nfsd_file_close_fh Date: Thu, 20 Aug 2015 07:17:15 -0400 Message-Id: <1440069440-27454-16-git-send-email-jeff.layton@primarydata.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1440069440-27454-1-git-send-email-jeff.layton@primarydata.com> References: <1440069440-27454-1-git-send-email-jeff.layton@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 ...when there are open files to be closed. When knfsd does an fput(), it gets queued to a list and a workqueue job is then scheduled to do the actual __fput work. In the case of knfsd closing down the file prior to a REMOVE or RENAME, we really want to ensure that those files are closed prior to returning. When there are files to be closed, call flush_delayed_fput to ensure this. There are deadlock possibilities if you call flush_delayed_fput while holding locks, however. In the case of nfsd_rename, we don't even do the lookups of the dentries to be renamed until we've locked for rename. Once we've figured out what the target dentry is for a rename, check to see whether there are cached open files associated with it. If there are, then unwind all of the locking, close them all, and then reattempt the rename. Signed-off-by: Jeff Layton --- fs/file_table.c | 1 + fs/nfsd/filecache.c | 33 ++++++++++++++++++++++++++++++++- fs/nfsd/filecache.h | 1 + fs/nfsd/trace.h | 10 +++++++++- fs/nfsd/vfs.c | 47 +++++++++++++++++++++++++++++++++++++++-------- 5 files changed, 82 insertions(+), 10 deletions(-) diff --git a/fs/file_table.c b/fs/file_table.c index 7f9d407c7595..33898e72618c 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -257,6 +257,7 @@ void flush_delayed_fput(void) { delayed_fput(NULL); } +EXPORT_SYMBOL_GPL(flush_delayed_fput); static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput); diff --git a/fs/nfsd/filecache.c b/fs/nfsd/filecache.c index 4bd683f03b6e..b62942ba6e7b 100644 --- a/fs/nfsd/filecache.c +++ b/fs/nfsd/filecache.c @@ -284,6 +284,34 @@ nfsd_file_find_locked(struct inode *inode, unsigned int may_flags, } /** + * nfsd_file_is_cached - are there any cached open files for this fh? + * @inode: inode of the file to check + * + * Scan the hashtable for open files that match this fh. Returns true if there + * are any, and false if not. + */ +bool +nfsd_file_is_cached(struct inode *inode) +{ + bool ret = false; + struct nfsd_file *nf; + unsigned int hashval = (unsigned int)hash_ptr(inode, NFSD_FILE_HASH_BITS); + + rcu_read_lock(); + hlist_for_each_entry_rcu(nf, &nfsd_file_hashtbl[hashval].nfb_head, + nf_node) { + if (inode == nf->nf_inode) { + ret = true; + break; + } + } + rcu_read_unlock(); + trace_nfsd_file_is_cached(hashval, inode, (int)ret); + return ret; +} + + +/** * nfsd_file_close_inode - attempt to forcibly close a nfsd_file * @inode: inode of the file to attempt to remove * @@ -305,7 +333,10 @@ nfsd_file_close_inode(struct inode *inode) } spin_unlock(&nfsd_file_hashtbl[hashval].nfb_lock); trace_nfsd_file_close_inode(hashval, inode, !list_empty(&dispose)); - nfsd_file_dispose_list(&dispose); + if (!list_empty(&dispose)) { + nfsd_file_dispose_list(&dispose); + flush_delayed_fput(); + } } __be32 diff --git a/fs/nfsd/filecache.h b/fs/nfsd/filecache.h index 191cdb25aa66..4a873efb7953 100644 --- a/fs/nfsd/filecache.h +++ b/fs/nfsd/filecache.h @@ -27,6 +27,7 @@ void nfsd_file_cache_shutdown(void); void nfsd_file_put(struct nfsd_file *nf); struct nfsd_file *nfsd_file_get(struct nfsd_file *nf); void nfsd_file_close_inode(struct inode *inode); +bool nfsd_file_is_cached(struct inode *inode); __be32 nfsd_file_acquire(struct svc_rqst *rqstp, struct svc_fh *fhp, unsigned int may_flags, struct nfsd_file **nfp); #endif /* _FS_NFSD_FILECACHE_H */ diff --git a/fs/nfsd/trace.h b/fs/nfsd/trace.h index 95af3b9c7b66..fc6d8ee51a00 100644 --- a/fs/nfsd/trace.h +++ b/fs/nfsd/trace.h @@ -140,7 +140,7 @@ TRACE_EVENT(nfsd_file_acquire, be32_to_cpu(__entry->status)) ); -TRACE_EVENT(nfsd_file_close_inode, +DECLARE_EVENT_CLASS(nfsd_file_search_class, TP_PROTO(unsigned int hash, struct inode *inode, int found), TP_ARGS(hash, inode, found), TP_STRUCT__entry( @@ -156,6 +156,14 @@ TRACE_EVENT(nfsd_file_close_inode, TP_printk("hash=0x%x inode=0x%p found=%d", __entry->hash, __entry->inode, __entry->found) ); + +#define DEFINE_NFSD_FILE_SEARCH_EVENT(name) \ +DEFINE_EVENT(nfsd_file_search_class, name, \ + TP_PROTO(unsigned int hash, struct inode *inode, int found), \ + TP_ARGS(hash, inode, found)) + +DEFINE_NFSD_FILE_SEARCH_EVENT(nfsd_file_close_inode); +DEFINE_NFSD_FILE_SEARCH_EVENT(nfsd_file_is_cached); #endif /* _NFSD_TRACE_H */ #undef TRACE_INCLUDE_PATH diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c index 98d3b9d96480..4cc78a4ec694 100644 --- a/fs/nfsd/vfs.c +++ b/fs/nfsd/vfs.c @@ -1592,6 +1592,17 @@ nfsd_close_cached_files(struct dentry *dentry) nfsd_file_close_inode(inode); } +static bool +nfsd_has_cached_files(struct dentry *dentry) +{ + bool ret = false; + struct inode *inode = d_inode(dentry); + + if (inode && S_ISREG(inode->i_mode)) + ret = nfsd_file_is_cached(inode); + return ret; +} + /* * Rename a file * N.B. After this call _both_ ffhp and tfhp need an fh_put @@ -1604,6 +1615,7 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, struct inode *fdir, *tdir; __be32 err; int host_err; + bool has_cached = false; err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); if (err) @@ -1622,6 +1634,7 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) goto out; +retry: host_err = fh_want_write(ffhp); if (host_err) { err = nfserrno(host_err); @@ -1661,12 +1674,16 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry) goto out_dput_new; - nfsd_close_cached_files(ndentry); - host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0); - if (!host_err) { - host_err = commit_metadata(tfhp); - if (!host_err) - host_err = commit_metadata(ffhp); + if (nfsd_has_cached_files(ndentry)) { + has_cached = true; + goto out_dput_old; + } else { + host_err = vfs_rename(fdir, odentry, tdir, ndentry, NULL, 0); + if (!host_err) { + host_err = commit_metadata(tfhp); + if (!host_err) + host_err = commit_metadata(ffhp); + } } out_dput_new: dput(ndentry); @@ -1679,12 +1696,26 @@ nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, * as that would do the wrong thing if the two directories * were the same, so again we do it by hand. */ - fill_post_wcc(ffhp); - fill_post_wcc(tfhp); + if (!has_cached) { + fill_post_wcc(ffhp); + fill_post_wcc(tfhp); + } unlock_rename(tdentry, fdentry); ffhp->fh_locked = tfhp->fh_locked = 0; fh_drop_write(ffhp); + /* + * If the target dentry has cached open files, then we need to try to + * close them prior to doing the rename. Flushing delayed fput + * shouldn't be done with locks held however, so we delay it until this + * point and then reattempt the whole shebang. + */ + if (has_cached) { + has_cached = false; + nfsd_close_cached_files(ndentry); + dput(ndentry); + goto retry; + } out: return err; }