From patchwork Wed Aug 6 09:34:22 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 4684531 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.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 710D4C0338 for ; Wed, 6 Aug 2014 09:34:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9918E20121 for ; Wed, 6 Aug 2014 09:34:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8657520114 for ; Wed, 6 Aug 2014 09:34:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754823AbaHFJeZ (ORCPT ); Wed, 6 Aug 2014 05:34:25 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:24437 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1754768AbaHFJeY (ORCPT ); Wed, 6 Aug 2014 05:34:24 -0400 X-IronPort-AV: E=Sophos;i="5.04,275,1406563200"; d="scan'208";a="34253862" Received: from unknown (HELO edo.cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 06 Aug 2014 17:31:33 +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 s769YLtf029825 for ; Wed, 6 Aug 2014 17:34:21 +0800 Received: from adam-work.lan (10.167.226.24) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Wed, 6 Aug 2014 17:34:27 +0800 From: Qu Wenruo To: Subject: [PATCH] btrfs: cancel scrub/replace if the user space process receive SIGKILL. Date: Wed, 6 Aug 2014 17:34:22 +0800 Message-ID: <1407317662-9364-1-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.0.4 MIME-Version: 1.0 X-Originating-IP: [10.167.226.24] Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.6 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 impatient sysadmin is tired of waiting background running btrfs scrub/replace and send SIGKILL to btrfs process, unlike SIGINT/SIGTERM which can be caught by user space program and cancel the scrub work, user space program will continue running until ioctl exits. To keep it consistent with the behavior of btrfs-progs, which cancels the work when SIGINT is received, this patch will make scrub routine to check SIGKILL pending of current task and cancel the work if SIGKILL is already pending. Signed-off-by: Qu Wenruo --- fs/btrfs/scrub.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c index b6d198f..0c8047f 100644 --- a/fs/btrfs/scrub.c +++ b/fs/btrfs/scrub.c @@ -2277,6 +2277,29 @@ static int get_raid56_logic_offset(u64 physical, int num, return 1; } +/* + * check whether the scrub is canceled + * if canceled, return -ECANCELED, else return 0 + * + * scrub/replace will be canceled when + * 1) cancel is called manually + * 2) caller user space process(btrfs-progs) receive SIGKILL + * other signals can be caught in btrfs-progs using multi-thread + * and cancel the work. + * but SIGKILL can't be caught and btrfs-progs already fallen into ioctl + * so cancel current scrub to return asap if SIGKILL is received. + */ +static inline int is_scrub_canceled(struct btrfs_fs_info *fs_info, + struct scrub_ctx *sctx) +{ + int ret = 0; + + if (unlikely(atomic_read(&fs_info->scrub_cancel_req) || + atomic_read(&sctx->cancel_req) || + __fatal_signal_pending(current))) + ret = -ECANCELED; + return ret; +} static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, struct map_lookup *map, struct btrfs_device *scrub_dev, @@ -2420,11 +2443,9 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx, /* * canceled? */ - if (atomic_read(&fs_info->scrub_cancel_req) || - atomic_read(&sctx->cancel_req)) { - ret = -ECANCELED; + ret = is_scrub_canceled(fs_info, sctx); + if (ret) goto out; - } /* * check to see if we have to pause */