From patchwork Tue Dec 18 13:46:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 10735691 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CAD36924 for ; Tue, 18 Dec 2018 13:46:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAC732A46F for ; Tue, 18 Dec 2018 13:46:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AC3832A478; Tue, 18 Dec 2018 13:46:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 481E42A46F for ; Tue, 18 Dec 2018 13:46:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726691AbeLRNq5 (ORCPT ); Tue, 18 Dec 2018 08:46:57 -0500 Received: from mx2.suse.de ([195.135.220.15]:44700 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726422AbeLRNqz (ORCPT ); Tue, 18 Dec 2018 08:46:55 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id DB801AE6B; Tue, 18 Dec 2018 13:46:53 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id 35DDE1E1484; Tue, 18 Dec 2018 14:46:53 +0100 (CET) From: Jan Kara To: Al Viro Cc: , Jan Kara Subject: [PATCH 1/2] vfs: Provide function to grab superblock reference Date: Tue, 18 Dec 2018 14:46:41 +0100 Message-Id: <20181218134642.21219-2-jack@suse.cz> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20181218134642.21219-1-jack@suse.cz> References: <20181218134642.21219-1-jack@suse.cz> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Provide function to hold superblock reference when we are sure the superblock is active. This function will get used by code reading /proc/mounts and friends. Signed-off-by: Jan Kara --- fs/internal.h | 2 ++ fs/super.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/fs/internal.h b/fs/internal.h index d410186bc369..c2c92be1e7df 100644 --- a/fs/internal.h +++ b/fs/internal.h @@ -104,6 +104,8 @@ extern bool trylock_super(struct super_block *sb); extern struct dentry *mount_fs(struct file_system_type *, int, const char *, void *); extern struct super_block *user_get_super(dev_t); +extern void hold_sb(struct super_block *sb); +extern bool mount_trylock_super(struct super_block *sb); /* * open.c diff --git a/fs/super.c b/fs/super.c index ca53a08497ed..552d6247053b 100644 --- a/fs/super.c +++ b/fs/super.c @@ -775,6 +775,41 @@ struct super_block *get_super_exclusive_thawed(struct block_device *bdev) } EXPORT_SYMBOL(get_super_exclusive_thawed); +/** + * hold_sb - get superblock reference for active superblock + * @sb: superblock to get reference for + * + * Get reference to superblock. The caller must make sure the superblock + * is currently active by other means (e.g. holding an active reference + * itself or holding namespace_sem preventing unmount). + */ +void hold_sb(struct super_block *sb) +{ + spin_lock(&sb_lock); + WARN_ON_ONCE(!atomic_read(&sb->s_active)); + sb->s_count++; + spin_unlock(&sb_lock); +} + +/** + * mount_trylock_super - get superblock and lock it against remount + * @sb: superblock to get reference for + * + * Get reference to superblock and protect superblock against racing + * remount or umount. The caller must make sure the superblock + * is currently active by other means (e.g. holding an active reference + * itself or holding namespace_sem preventing unmount). + */ +bool mount_trylock_super(struct super_block *sb) +{ + hold_sb(sb); + if (!down_read_trylock(&sb->s_umount)) { + put_super(sb); + return false; + } + return true; +} + /** * get_active_super - get an active reference to the superblock of a device * @bdev: device to get the superblock for