Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3469516imu; Sun, 11 Nov 2018 15:49:50 -0800 (PST) X-Google-Smtp-Source: AJdET5fmk/aEFQhbZ3faGEup6uczx4zQAjzV/9fwmbL5JMAamyPHoV7LbDdPPvdPFMMGYMnYy3Ij X-Received: by 2002:a62:11c7:: with SMTP id 68mr384611pfr.21.1541980190072; Sun, 11 Nov 2018 15:49:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541980190; cv=none; d=google.com; s=arc-20160816; b=Z01wCFEF088nLDp+Y3/m42+29H2fU/NRe4AdyiAT5ooMlLSo4u5Qn20AdMXxcjUcmg 8H7vSnRfgDOJRGbP7UsRCBI4WtR1fhKJYS7v8RnAT54j468LRAuCJRsVUc38GkilgCoI JdujPMCwGeqjeRHv8kA17l5I/UeNtwMAimYISwYWii54Kelr4GTqvMrufIX8bxJEmcDu BO51Ueml8NYOCf6bMtU7ruHiSP4+PpihGEmohplJbeb9d/Ffva2aU6OzHflf3ocfJqcc yvT9yvOwWR7u2W8mte0jROC526/5tZfZyBByMx49DctC3eegyQSrxfuiPeXvQxtc7sNq UQ/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=q+BAdaquwXA6E0ifsEG/RHyB2GzgunivGkeSODSZJvo=; b=yRwpK11yjmA3P7fTLVXftn1CkZUirF7Ik1/Aii703Nb4H9B9TZmidehLk8U4Lp43pA V5C06hkionyeYf9rPapqPLM7fZyOI2HcipNXKlrDoAubxC5ClJ5GjKxiFjMDMjSdaQY9 LaTiGxQcLgdPz7pMxMqonWP1c3WQbghQGS8JB564LdAFJccmJp1DZX9d3sCI+mJrybnW Qp7m7ne+5a3RCnWEV24JwQNH3dm1aueQnhRyRQjK1SwdkU+3bla8cKvJONhv/oRbP9NT UCam7XQqB9csQu4UezOIFWjqffJlFm4i69i2AhvyypUXrH9V/oJcsLDxprhKkznN28eF 8MwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SxQ7y8k6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 1-v6si16799771plt.225.2018.11.11.15.49.35; Sun, 11 Nov 2018 15:49:50 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SxQ7y8k6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733126AbeKLISo (ORCPT + 99 others); Mon, 12 Nov 2018 03:18:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:38824 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733012AbeKLISl (ORCPT ); Mon, 12 Nov 2018 03:18:41 -0500 Received: from localhost (unknown [206.108.79.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1338822503; Sun, 11 Nov 2018 22:28:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1541975325; bh=C0KyuaJgMyhpzjYj+YDAXJo1RjuYgY+UfGcFSU6zrzc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SxQ7y8k6C/j7hi+p+oHOm/WDrDXPTXoPLznH/F3zqywz0DVh9IBRyhwMo5zvMBJW0 4iWi57rYQzJ7BcjrhaeqJfoNusiauZDaJD6F18uOzVZFQ/btMhU7fTQ+YKU+foyp/W 2gjD9c81+NcxgLQr288ldmvt9UqOLSr+PoT2vt4U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chris Murphy , Qu Wenruo , David Sterba Subject: [PATCH 4.19 331/361] btrfs: Enhance btrfs_trim_fs function to handle error better Date: Sun, 11 Nov 2018 14:21:18 -0800 Message-Id: <20181111221659.901768008@linuxfoundation.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181111221619.915519183@linuxfoundation.org> References: <20181111221619.915519183@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Qu Wenruo commit 93bba24d4b5ad1e5cd8b43f64e66ff9d6355dd20 upstream. Function btrfs_trim_fs() doesn't handle errors in a consistent way. If error happens when trimming existing block groups, it will skip the remaining blocks and continue to trim unallocated space for each device. The return value will only reflect the final error from device trimming. This patch will fix such behavior by: 1) Recording the last error from block group or device trimming The return value will also reflect the last error during trimming. Make developer more aware of the problem. 2) Continuing trimming if possible If we failed to trim one block group or device, we could still try the next block group or device. 3) Report number of failures during block group and device trimming It would be less noisy, but still gives user a brief summary of what's going wrong. Such behavior can avoid confusion for cases like failure to trim the first block group and then only unallocated space is trimmed. Reported-by: Chris Murphy CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Qu Wenruo Reviewed-by: David Sterba [ add bg_ret and dev_ret to the messages ] Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/extent-tree.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -10850,6 +10850,15 @@ static int btrfs_trim_free_extents(struc return ret; } +/* + * Trim the whole filesystem by: + * 1) trimming the free space in each block group + * 2) trimming the unallocated space on each device + * + * This will also continue trimming even if a block group or device encounters + * an error. The return value will be the last error, or 0 if nothing bad + * happens. + */ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range) { struct btrfs_block_group_cache *cache = NULL; @@ -10860,6 +10869,10 @@ int btrfs_trim_fs(struct btrfs_fs_info * u64 end; u64 trimmed = 0; u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); + u64 bg_failed = 0; + u64 dev_failed = 0; + int bg_ret = 0; + int dev_ret = 0; int ret = 0; /* @@ -10870,7 +10883,7 @@ int btrfs_trim_fs(struct btrfs_fs_info * else cache = btrfs_lookup_block_group(fs_info, range->start); - while (cache) { + for (; cache; cache = next_block_group(fs_info, cache)) { if (cache->key.objectid >= (range->start + range->len)) { btrfs_put_block_group(cache); break; @@ -10884,13 +10897,15 @@ int btrfs_trim_fs(struct btrfs_fs_info * if (!block_group_cache_done(cache)) { ret = cache_block_group(cache, 0); if (ret) { - btrfs_put_block_group(cache); - break; + bg_failed++; + bg_ret = ret; + continue; } ret = wait_block_group_cache_done(cache); if (ret) { - btrfs_put_block_group(cache); - break; + bg_failed++; + bg_ret = ret; + continue; } } ret = btrfs_trim_block_group(cache, @@ -10901,28 +10916,40 @@ int btrfs_trim_fs(struct btrfs_fs_info * trimmed += group_trimmed; if (ret) { - btrfs_put_block_group(cache); - break; + bg_failed++; + bg_ret = ret; + continue; } } - - cache = next_block_group(fs_info, cache); } + if (bg_failed) + btrfs_warn(fs_info, + "failed to trim %llu block group(s), last error %d", + bg_failed, bg_ret); mutex_lock(&fs_info->fs_devices->device_list_mutex); devices = &fs_info->fs_devices->alloc_list; list_for_each_entry(device, devices, dev_alloc_list) { ret = btrfs_trim_free_extents(device, range->minlen, &group_trimmed); - if (ret) + if (ret) { + dev_failed++; + dev_ret = ret; break; + } trimmed += group_trimmed; } mutex_unlock(&fs_info->fs_devices->device_list_mutex); + if (dev_failed) + btrfs_warn(fs_info, + "failed to trim %llu device(s), last error %d", + dev_failed, dev_ret); range->len = trimmed; - return ret; + if (bg_ret) + return bg_ret; + return dev_ret; } /*