From patchwork Thu Jun 26 19:12:15 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 4430471 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 06769BEEAA for ; Thu, 26 Jun 2014 19:14:42 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0FD41201BB for ; Thu, 26 Jun 2014 19:14:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CD8B8202AE for ; Thu, 26 Jun 2014 19:14:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751771AbaFZTOg (ORCPT ); Thu, 26 Jun 2014 15:14:36 -0400 Received: from mail-qa0-f41.google.com ([209.85.216.41]:60084 "EHLO mail-qa0-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751754AbaFZTOe (ORCPT ); Thu, 26 Jun 2014 15:14:34 -0400 Received: by mail-qa0-f41.google.com with SMTP id cm18so3214704qab.28 for ; Thu, 26 Jun 2014 12:14:34 -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=l6IE0XYBpwPo9j4UKjtsvIpki3Vy4yG6HJp/IpVmHuU=; b=LmosbFXCXiMTS6SxZCA1X2TZbDM1O5NlY6G3rjaQccQ9mxOBiu+izqox0RRR7NYJEJ 5DPNfa7Bn3ZnLNvCcVZmEBe0XRCv08p255+eMp5J4t318c3HFjFsvCa/fPnHLOUDIGOZ Tzc64ENOQ633H+OE/Blog73eICjt88Si8Ow3Xew4mtZ9fGPPSjjLHDyhhhnet6X6RemQ 6om8omr0c+kymoDLVoBI+xn2OWfiT1UHXGzaN3ohWYGaQ16Dsg8kltlSBoZocYi/3GIW 3jXUD5r1yA8B4kfxSvTH8CWs2OgnC0iUU5zKXMoTxrWb4N8842i1JAdd1tmG6/yrpIyP pxoA== X-Gm-Message-State: ALoCoQlfAL23ln89ZkH16xYiuATKaw2qTsAcWxUuIZRSze8xKXpcdMCldR+MDMwvY2vEMrruAzPs X-Received: by 10.140.50.167 with SMTP id s36mr24667964qga.36.1403810073964; Thu, 26 Jun 2014 12:14:33 -0700 (PDT) Received: from tlielax.poochiereds.net ([2001:470:8:d63:3a60:77ff:fe93:a95d]) by mx.google.com with ESMTPSA id 88sm4763039qgh.5.2014.06.26.12.14.32 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Jun 2014 12:14:33 -0700 (PDT) From: Jeff Layton To: bfields@fieldses.org Cc: linux-nfs@vger.kernel.org Subject: [PATCH v2 035/117] locks: add file_has_lease to prevent delegation break races Date: Thu, 26 Jun 2014 15:12:15 -0400 Message-Id: <1403810017-16062-36-git-send-email-jlayton@primarydata.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1403810017-16062-1-git-send-email-jlayton@primarydata.com> References: <1403810017-16062-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 Once we remove the client_mutex, we'll have a potential race between setting a lease on a file and breaking the delegation. We may set the lease, but by the time we go to hash it, it may have already been broken. Currently, we know that this won't happen as the nfs4_laundromat takes the client_mutex, but we want to remove that. As part of that fix, add a function that can tell us whether a particular file has a lease set on it. In a later nfsd patch, we'll use that to close the potential race window. Signed-off-by: Jeff Layton --- fs/locks.c | 26 ++++++++++++++++++++++++++ include/linux/fs.h | 6 ++++++ 2 files changed, 32 insertions(+) diff --git a/fs/locks.c b/fs/locks.c index 717fbc404e6b..005cc86927e3 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -1308,6 +1308,32 @@ static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker) } /** + * file_has_lease - does the given file have a lease set on it? + * @file: struct file on which we want to check the lease + * + * Returns true if a lease was is set on the given file description, + * false otherwise. + */ +bool +file_has_lease(struct file *file) +{ + bool ret = false; + struct inode *inode = file_inode(file); + struct file_lock *fl; + + spin_lock(&inode->i_lock); + for (fl = inode->i_flock; fl && IS_LEASE(fl); fl = fl->fl_next) { + if (fl->fl_file == file) { + ret = true; + break; + } + } + spin_unlock(&inode->i_lock); + return ret; +} +EXPORT_SYMBOL(file_has_lease); + +/** * __break_lease - revoke all outstanding leases on file * @inode: the inode of the file to return * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR: diff --git a/include/linux/fs.h b/include/linux/fs.h index e11d60cc867b..7ae6f4869669 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -953,6 +953,7 @@ extern int vfs_test_lock(struct file *, struct file_lock *); extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *); extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl); extern int flock_lock_file_wait(struct file *filp, struct file_lock *fl); +extern bool file_has_lease(struct file *file); extern int __break_lease(struct inode *inode, unsigned int flags, unsigned int type); extern void lease_get_mtime(struct inode *, struct timespec *time); extern int generic_setlease(struct file *, long, struct file_lock **); @@ -1064,6 +1065,11 @@ static inline int flock_lock_file_wait(struct file *filp, return -ENOLCK; } +static inline bool file_has_lease(struct file *file) +{ + return false; +} + static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) { return 0;