From patchwork Tue Jan 20 09:05:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 5666451 Return-Path: X-Original-To: patchwork-linux-btrfs@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 B3AB9C058D for ; Tue, 20 Jan 2015 09:10:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D64AE203A5 for ; Tue, 20 Jan 2015 09:10:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E843D20353 for ; Tue, 20 Jan 2015 09:10:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752007AbbATJJP (ORCPT ); Tue, 20 Jan 2015 04:09:15 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:21430 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1752526AbbATJHs (ORCPT ); Tue, 20 Jan 2015 04:07:48 -0500 X-IronPort-AV: E=Sophos;i="5.04,848,1406563200"; d="scan'208";a="56324393" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 20 Jan 2015 17:04:16 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (localhost.localdomain [127.0.0.1]) by edo.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id t0K9773F014151; Tue, 20 Jan 2015 17:07:07 +0800 Received: from localhost.localdomain (10.167.226.33) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Tue, 20 Jan 2015 17:07:48 +0800 From: Qu Wenruo To: CC: Subject: [PATCH RFC v2 3/5] btrfs: Handle pending changes in btrfs_freeze(). Date: Tue, 20 Jan 2015 17:05:35 +0800 Message-ID: <1421744737-16586-4-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.2.2 In-Reply-To: <1421744737-16586-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1421744737-16586-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.33] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@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 Before the patch, btrfs_freeze() does not start a transaction if there is pending changes but no transaction is running. This will lead to deadlock if the fs is frozen but there are pending changes, since sync_fs will start a transaction with s_umount mutex, blocked by frozen fs. And later unfreeze will wait sync_fs to release the s_umount mutex. This patch will ensure the frozen fs from having pending changes, except some one use sysfs interfaces to create pending changes again. Signed-off-by: Qu Wenruo --- fs/btrfs/super.c | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 60f7cbe..5229786 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -975,32 +975,22 @@ fail_close: return err; } -int btrfs_sync_fs(struct super_block *sb, int wait) +static int commit_current_or_new_trans(struct btrfs_fs_info *fs_info, + int freezing) { struct btrfs_trans_handle *trans; - struct btrfs_fs_info *fs_info = btrfs_sb(sb); struct btrfs_root *root = fs_info->tree_root; - trace_btrfs_sync_fs(wait); - - if (!wait) { - filemap_flush(fs_info->btree_inode->i_mapping); - return 0; - } - - btrfs_wait_ordered_roots(fs_info, -1); - trans = btrfs_attach_transaction_barrier(root); if (IS_ERR(trans)) { - /* no transaction, don't bother */ if (PTR_ERR(trans) == -ENOENT) { - /* - * Exit unless we have some pending changes - * that need to go through commit - */ + /* No pending changes, don't bother*/ if (fs_info->pending_changes == 0) return 0; - trans = btrfs_start_transaction(root, 0); + if (freezing) + trans = btrfs_start_transaction_freeze(root, 0); + else + trans = btrfs_start_transaction(root, 0); } else { return PTR_ERR(trans); } @@ -1008,6 +998,22 @@ int btrfs_sync_fs(struct super_block *sb, int wait) return btrfs_commit_transaction(trans, root); } +int btrfs_sync_fs(struct super_block *sb, int wait) +{ + struct btrfs_fs_info *fs_info = btrfs_sb(sb); + + trace_btrfs_sync_fs(wait); + + if (!wait) { + filemap_flush(fs_info->btree_inode->i_mapping); + return 0; + } + + btrfs_wait_ordered_roots(fs_info, -1); + + return commit_current_or_new_trans(fs_info, 0); +} + static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) { struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); @@ -1935,17 +1941,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd, static int btrfs_freeze(struct super_block *sb) { - struct btrfs_trans_handle *trans; - struct btrfs_root *root = btrfs_sb(sb)->tree_root; + struct btrfs_fs_info *fs_info = btrfs_sb(sb); - trans = btrfs_attach_transaction_barrier(root); - if (IS_ERR(trans)) { - /* no transaction, don't bother */ - if (PTR_ERR(trans) == -ENOENT) - return 0; - return PTR_ERR(trans); - } - return btrfs_commit_transaction(trans, root); + return commit_current_or_new_trans(fs_info, 1); } static int btrfs_unfreeze(struct super_block *sb)