From patchwork Mon Sep 7 14:04:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kinglong Mee X-Patchwork-Id: 7135631 Return-Path: X-Original-To: patchwork-linux-fsdevel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 733A3BEEC1 for ; Mon, 7 Sep 2015 14:05:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 88E4C2081B for ; Mon, 7 Sep 2015 14:05:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A35ED2060B for ; Mon, 7 Sep 2015 14:05:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751251AbbIGOFM (ORCPT ); Mon, 7 Sep 2015 10:05:12 -0400 Received: from mail-pa0-f42.google.com ([209.85.220.42]:35005 "EHLO mail-pa0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751143AbbIGOFL (ORCPT ); Mon, 7 Sep 2015 10:05:11 -0400 Received: by pacfv12 with SMTP id fv12so100418035pac.2; Mon, 07 Sep 2015 07:05:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-type:content-transfer-encoding; bh=Sg0FY1c12rY1O0kIYDxSMxJ/zK7+mQ7iMZwZ1dc2R3A=; b=SyRrLf10jFSFlI022f/Y1aKrkcsFly068D6Iesi4NKg9Hufu9MiTnxAd0vs3WCssDt Gbriuzvdbr/ApCykNdU4JO31DcOeo6i2Xjdm+9RE/Fqa/LoCyT9c1IeMTnd54rQIRSNF lEx7HiuqzlyPkEpTsGdEg26tzBVNXFIlT9b6LvPnfCtMZZpb0GR8cexu5zbRHImqIdqK 2kHupRKjXdJLaVsaXF6T9XIH0eBGwcV2/jnrVNEL7dR7/UVDG+1V4fjq1SmRD7lOBQIB M/xoqE35sSYAbrRLAmhmc5RznhqgUsgT+cx7izQ8HC1izUN/R8RtGLQOEgBYhiRUJFMg BVXA== X-Received: by 10.68.244.234 with SMTP id xj10mr46580549pbc.13.1441634710595; Mon, 07 Sep 2015 07:05:10 -0700 (PDT) Received: from [192.168.99.8] ([104.143.41.79]) by smtp.googlemail.com with ESMTPSA id bc1sm12161646pbb.66.2015.09.07.07.05.00 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 Sep 2015 07:05:10 -0700 (PDT) Subject: [PATCH 4/6 v10] fs: New helper legitimize_mntget() for getting a legitimize mnt To: "J. Bruce Fields" , Al Viro References: <55ED9899.9010401@gmail.com> Cc: "linux-nfs@vger.kernel.org" , linux-fsdevel@vger.kernel.org, NeilBrown , Trond Myklebust , kinglongmee@gmail.com From: Kinglong Mee Message-ID: <55ED9986.3040203@gmail.com> Date: Mon, 7 Sep 2015 22:04:54 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: <55ED9899.9010401@gmail.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, 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 New helper legitimize_mntget for getting a mnt without setting MNT_SYNC_UMOUNT | MNT_UMOUNT | MNT_DOOMED, otherwise return NULL. v9, Update using read_seqbegin_or_lock instead read_seqlock_excl v10, fix bad logical of using seqlock Signed-off-by: Kinglong Mee --- fs/namespace.c | 38 ++++++++++++++++++++++++++++++++++++++ include/linux/mount.h | 1 + 2 files changed, 39 insertions(+) diff --git a/fs/namespace.c b/fs/namespace.c index 0570729..6377672 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -1153,6 +1153,44 @@ struct vfsmount *mntget(struct vfsmount *mnt) } EXPORT_SYMBOL(mntget); +struct vfsmount *legitimize_mntget(struct vfsmount *vfsmnt) +{ + struct mount *mnt; + unsigned seq = 0; + + if (vfsmnt == NULL) + return NULL; + + rcu_read_lock(); +retry: + read_seqbegin_or_lock(&mount_lock, &seq); + + if (vfsmnt->mnt_flags & (MNT_SYNC_UMOUNT | MNT_UMOUNT | MNT_DOOMED)) + vfsmnt = NULL; + else { + mnt = real_mount(vfsmnt); + mnt_add_count(mnt, 1); + if (need_seqretry(&mount_lock, seq)) { + /* lost the race, need to try again */ + if (vfsmnt->mnt_flags & MNT_SYNC_UMOUNT) { + /* no point trying... */ + mnt_add_count(mnt, -1); + vfsmnt = NULL; + } else { + mntput(vfsmnt); + seq = 1; + goto retry; + } + } + } + + done_seqretry(&mount_lock, seq); + rcu_read_unlock(); + + return vfsmnt; +} +EXPORT_SYMBOL(legitimize_mntget); + struct vfsmount *mnt_clone_internal(struct path *path) { struct mount *p; diff --git a/include/linux/mount.h b/include/linux/mount.h index f822c3c..8ae9dc0 100644 --- a/include/linux/mount.h +++ b/include/linux/mount.h @@ -79,6 +79,7 @@ extern void mnt_drop_write(struct vfsmount *mnt); extern void mnt_drop_write_file(struct file *file); extern void mntput(struct vfsmount *mnt); extern struct vfsmount *mntget(struct vfsmount *mnt); +extern struct vfsmount *legitimize_mntget(struct vfsmount *vfsmnt); extern struct vfsmount *mnt_clone_internal(struct path *path); extern int __mnt_is_readonly(struct vfsmount *mnt);