From patchwork Tue Oct 29 23:31:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11218807 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 02BE61390 for ; Tue, 29 Oct 2019 23:31:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D3DE920679 for ; Tue, 29 Oct 2019 23:31:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="V2W4JZGX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725974AbfJ2Xb5 (ORCPT ); Tue, 29 Oct 2019 19:31:57 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:42920 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726336AbfJ2Xb5 (ORCPT ); Tue, 29 Oct 2019 19:31:57 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNTJ2b038447 for ; Tue, 29 Oct 2019 23:31:55 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=huokKDzjlnWN600dYv1Bzes61RmlCko7M00qcAoevIM=; b=V2W4JZGXMyAbX+Qh4ltnaT+rdu1wdbSl/OUgOxYG6/X65I4gkCiDKFFF8u1eOxSwac4N ndzO2vh1tnj3dVhP1vX7LKr7iqexJwcJs2UtSyBCEFUTmwEGnntbZbzMszmU/uZKbZK2 h2HTxwTl1UAdT9Yii80DQbOJnBkpRZLs5yJm2dempPu/R2wANr8CANGRSbSWpW4e/rdF zi8fl98BX36+rgRs0hnGBl2Fr3qhRN/jaLHNJCw4vZfTa67B7RgWyNCCyiS7f8CUUvWa ikDJYThrLjcXgvjpEsW0FdBKi/7FLi2Nw53I1FtJV1fs6v0eyawXwXp1HS43wOBjZpYR zA== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by userp2130.oracle.com with ESMTP id 2vxwhfgb50-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:31:55 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNSFDJ039139 for ; Tue, 29 Oct 2019 23:31:54 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserp3030.oracle.com with ESMTP id 2vxwj8v1c9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:31:54 +0000 Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x9TNVrIq024470 for ; Tue, 29 Oct 2019 23:31:54 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Oct 2019 16:31:53 -0700 Subject: [PATCH 1/5] xfs: always rescan allegedly healthy per-ag metadata after repair From: "Darrick J. Wong" To: darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Tue, 29 Oct 2019 16:31:52 -0700 Message-ID: <157239191229.1267304.16820747345916289901.stgit@magnolia> In-Reply-To: <157239190609.1267304.9008396217521176875.stgit@magnolia> References: <157239190609.1267304.9008396217521176875.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=1 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong After an online repair function runs for a per-AG metadata structure, sc->sick_mask is supposed to reflect the per-AG metadata that the repair function fixed. Our next move is to re-check the metadata to assess the completeness of our repair, so we don't want the rebuilt structure to be excluded from the rescan just because the health system previously logged a problem with the data structure. Signed-off-by: Darrick J. Wong --- fs/xfs/scrub/health.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/xfs/scrub/health.c b/fs/xfs/scrub/health.c index b2f602811e9d..4865b2180e22 100644 --- a/fs/xfs/scrub/health.c +++ b/fs/xfs/scrub/health.c @@ -220,6 +220,16 @@ xchk_ag_btree_healthy_enough( return true; } + /* + * If we just repaired some AG metadata, sc->sick_mask will reflect all + * the per-AG metadata types that were repaired. Exclude these from + * the filesystem health query because we have not yet updated the + * health status and we want everything to be scanned. + */ + if ((sc->flags & XREP_ALREADY_FIXED) && + type_to_health_flag[sc->sm->sm_type].group == XHG_AG) + mask &= ~sc->sick_mask; + if (xfs_ag_has_sickness(pag, mask)) { sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL; return false; From patchwork Tue Oct 29 23:31:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11218821 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B296414DB for ; Tue, 29 Oct 2019 23:34:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7158220679 for ; Tue, 29 Oct 2019 23:34:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="QVwNLPZt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726284AbfJ2XeD (ORCPT ); Tue, 29 Oct 2019 19:34:03 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:54198 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725974AbfJ2XeC (ORCPT ); Tue, 29 Oct 2019 19:34:02 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNY1kf020839 for ; Tue, 29 Oct 2019 23:34:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=29JA/cxkssepMpCPmBLCA5MdYZq5GTgGlZ4VgP5Kjek=; b=QVwNLPZtR2ZfLJyglstbjw8XHBCLl2XkupsSmApAnKk2YeadX26Vr/i2F+LNXSlj3kZa FTksP6mLYjmYn5Dm3++5DwAV2OHitKp/NPQ38PYxwXSe5sUJ4LaRVJKHE1+R6AXgMhdC WNkBax3/wREtfp99D05Eu6NXA/Og9drXpGK9cPRt1EuTuei9kumCJlFsfdar1xS2KeA+ QRxyaUK7M80wkZ39HBn1Gf8Cx/nvsaHhrmb1fc6ICBGKGv90kvxlI/5bn4qIiRTCZmpF B3QTuZtRqTgWZwq6Nkncz5aPQmgXZ7ywNqAB8Rys/Mm2T6W+g17XJ6dV2KOYk7+txEY3 LQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 2vxwhf8b9r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:34:01 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNRqhl183447 for ; Tue, 29 Oct 2019 23:32:00 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3020.oracle.com with ESMTP id 2vxwj84arb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:00 +0000 Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x9TNVxJ9012021 for ; Tue, 29 Oct 2019 23:31:59 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Oct 2019 16:31:59 -0700 Subject: [PATCH 2/5] xfs: create a big array data structure From: "Darrick J. Wong" To: darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Tue, 29 Oct 2019 16:31:58 -0700 Message-ID: <157239191842.1267304.17783744609025546276.stgit@magnolia> In-Reply-To: <157239190609.1267304.9008396217521176875.stgit@magnolia> References: <157239190609.1267304.9008396217521176875.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290207 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Create a simple 'big array' data structure for storage of fixed-size metadata records that will be used to reconstruct a btree index. For repair operations, the most important operations are append, iterate, and sort. Earlier implementations of the big array used linked lists and suffered from severe problems -- pinning all records in kernel memory was not a good idea and frequently lead to OOM situations; random access was very inefficient; and record overhead for the lists was unacceptably high at 40-60%. Therefore, the big memory array relies on the 'xfile' abstraction, which creates a memfd file and stores the records in page cache pages. Since the memfd is created in tmpfs, the memory pages can be pushed out to disk if necessary and we have a built-in usage limit of 50% of physical memory. Signed-off-by: Darrick J. Wong --- fs/xfs/Makefile | 2 fs/xfs/scrub/array.c | 630 ++++++++++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/array.h | 44 +++ fs/xfs/scrub/trace.h | 23 ++ fs/xfs/scrub/xfile.c | 82 +++++++ fs/xfs/scrub/xfile.h | 21 ++ 6 files changed, 802 insertions(+) create mode 100644 fs/xfs/scrub/array.c create mode 100644 fs/xfs/scrub/array.h create mode 100644 fs/xfs/scrub/xfile.c create mode 100644 fs/xfs/scrub/xfile.h diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index 06b68b6115bc..d21b59cfc530 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile @@ -160,8 +160,10 @@ xfs-$(CONFIG_XFS_QUOTA) += scrub/quota.o ifeq ($(CONFIG_XFS_ONLINE_REPAIR),y) xfs-y += $(addprefix scrub/, \ agheader_repair.o \ + array.o \ bitmap.o \ repair.o \ + xfile.o \ ) endif endif diff --git a/fs/xfs/scrub/array.c b/fs/xfs/scrub/array.c new file mode 100644 index 000000000000..1b3635a115b2 --- /dev/null +++ b/fs/xfs/scrub/array.c @@ -0,0 +1,630 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "xfs.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "scrub/array.h" +#include "scrub/scrub.h" +#include "scrub/trace.h" +#include "scrub/xfile.h" + +/* + * XFS Fixed-Size Big Memory Array + * =============================== + * The file-backed memory array uses a memfd "file" to store large numbers of + * fixed-size records in memory that can be paged out. This puts less stress + * on the memory reclaim algorithms because memfd file pages are not pinned and + * can be paged out; however, array access is less direct than would be in a + * regular memory array. Access to the array is performed via indexed get and + * put methods, and an append method is provided for convenience. Array + * elements can be set to all zeroes, which means that the entry is NULL and + * will be skipped during iteration. + */ + +#define XFBMA_MAX_TEMP (2) + +/* + * Pointer to temp space. Because we can't access the memfd data directly, we + * allocate a small amount of memory on the end of the xfbma to buffer array + * items when we need space to store values temporarily. + */ +static inline void * +xfbma_temp( + struct xfbma *array, + unsigned int nr) +{ + ASSERT(nr < XFBMA_MAX_TEMP); + + return ((char *)(array + 1)) + (nr * array->obj_size); +} + +/* Initialize a big memory array. */ +struct xfbma * +xfbma_init( + size_t obj_size) +{ + struct xfbma *array; + struct file *filp; + int error; + + filp = xfile_create("big array"); + if (!filp) + return ERR_PTR(-ENOMEM); + if (IS_ERR(filp)) + return ERR_CAST(filp); + + error = -ENOMEM; + array = kmem_alloc(sizeof(struct xfbma) + (XFBMA_MAX_TEMP * obj_size), + KM_NOFS | KM_MAYFAIL); + if (!array) + goto out_filp; + + array->filp = filp; + array->obj_size = obj_size; + array->nr = 0; + return array; +out_filp: + fput(filp); + return ERR_PTR(error); +} + +void +xfbma_destroy( + struct xfbma *array) +{ + xfile_destroy(array->filp); + kmem_free(array); +} + +/* Compute offset of array element. */ +static inline loff_t +xfbma_offset( + struct xfbma *array, + uint64_t nr) +{ + if (nr >= array->nr) + return -1; + return nr * array->obj_size; +} + +/* Get an element from the array. */ +int +xfbma_get( + struct xfbma *array, + uint64_t nr, + void *ptr) +{ + loff_t pos = xfbma_offset(array, nr); + + if (pos < 0) { + ASSERT(0); + return -ENODATA; + } + + return xfile_io(array->filp, XFILE_IO_READ, &pos, ptr, array->obj_size); +} + +/* Put an element in the array. */ +int +xfbma_set( + struct xfbma *array, + uint64_t nr, + void *ptr) +{ + loff_t pos = xfbma_offset(array, nr); + + if (pos < 0) { + ASSERT(0); + return -ENODATA; + } + + return xfile_io(array->filp, XFILE_IO_WRITE, &pos, ptr, + array->obj_size); +} + +/* Is this array element NULL? */ +bool +xfbma_is_null( + struct xfbma *array, + void *ptr) +{ + return !memchr_inv(ptr, 0, array->obj_size); +} + +/* Put an element anywhere in the array that isn't NULL. */ +int +xfbma_insert_anywhere( + struct xfbma *array, + void *ptr) +{ + void *temp = xfbma_temp(array, 0); + uint64_t i; + int error; + + /* Find a null slot to put it in. */ + for (i = 0; i < array->nr; i++) { + error = xfbma_get(array, i, temp); + if (error || !xfbma_is_null(array, temp)) + continue; + return xfbma_set(array, i, ptr); + } + + /* No null slots, just dump it on the end. */ + return xfbma_append(array, ptr); +} + +/* NULL an element in the array. */ +int +xfbma_nullify( + struct xfbma *array, + uint64_t nr) +{ + void *temp = xfbma_temp(array, 0); + loff_t pos = xfbma_offset(array, nr); + + if (pos < 0) { + ASSERT(0); + return -ENODATA; + } + + memset(temp, 0, array->obj_size); + return xfile_io(array->filp, XFILE_IO_WRITE, &pos, temp, + array->obj_size); +} + +/* Append an element to the array. */ +int +xfbma_append( + struct xfbma *array, + void *ptr) +{ + loff_t pos = array->obj_size * array->nr; + int error; + + if (pos < 0) { + ASSERT(0); + return -ENODATA; + } + + error = xfile_io(array->filp, XFILE_IO_WRITE, &pos, ptr, + array->obj_size); + if (error) + return error; + array->nr++; + return 0; +} + +/* + * Iterate every element in this array, freeing each element as we go. + * Array elements will be nulled out. + */ +int +xfbma_iter_del( + struct xfbma *array, + xfbma_iter_fn iter_fn, + void *priv) +{ + void *temp = xfbma_temp(array, 0); + pgoff_t oldpagenr = 0; + uint64_t max_bytes; + uint64_t i; + loff_t pos; + int error = 0; + + max_bytes = array->nr * array->obj_size; + for (pos = 0, i = 0; pos < max_bytes; i++) { + pgoff_t pagenr; + + error = xfile_io(array->filp, XFILE_IO_READ, &pos, temp, + array->obj_size); + if (error) + break; + if (xfbma_is_null(array, temp)) + goto next; + error = iter_fn(temp, priv); + if (error) + break; +next: + /* Release the previous page if possible. */ + pagenr = pos >> PAGE_SHIFT; + if (pagenr != oldpagenr) + xfile_discard(array->filp, oldpagenr << PAGE_SHIFT, + pos - 1); + oldpagenr = pagenr; + } + + return error; +} + +/* Return length of array. */ +uint64_t +xfbma_length( + struct xfbma *array) +{ + return array->nr; +} + +/* + * Select the median value from a[lo], a[mid], and a[hi]. Put the median in + * a[lo], the lowest in a[lo], and the highest in a[hi]. Using the median of + * the three reduces the chances that we pick the worst case pivot value, since + * it's likely that our array values are nearly sorted. + */ +STATIC int +xfbma_qsort_pivot( + struct xfbma *array, + xfbma_cmp_fn cmp_fn, + uint64_t lo, + uint64_t mid, + uint64_t hi) +{ + void *a = xfbma_temp(array, 0); + void *b = xfbma_temp(array, 1); + int error; + + /* if a[mid] < a[lo], swap a[mid] and a[lo]. */ + error = xfbma_get(array, mid, a); + if (error) + return error; + error = xfbma_get(array, lo, b); + if (error) + return error; + if (cmp_fn(a, b) < 0) { + error = xfbma_set(array, lo, a); + if (error) + return error; + error = xfbma_set(array, mid, b); + if (error) + return error; + } + + /* if a[hi] < a[mid], swap a[mid] and a[hi]. */ + error = xfbma_get(array, hi, a); + if (error) + return error; + error = xfbma_get(array, mid, b); + if (error) + return error; + if (cmp_fn(a, b) < 0) { + error = xfbma_set(array, mid, a); + if (error) + return error; + error = xfbma_set(array, hi, b); + if (error) + return error; + } else { + goto move_front; + } + + /* if a[mid] < a[lo], swap a[mid] and a[lo]. */ + error = xfbma_get(array, mid, a); + if (error) + return error; + error = xfbma_get(array, lo, b); + if (error) + return error; + if (cmp_fn(a, b) < 0) { + error = xfbma_set(array, lo, a); + if (error) + return error; + error = xfbma_set(array, mid, b); + if (error) + return error; + } +move_front: + /* move our selected pivot to a[lo] */ + error = xfbma_get(array, lo, b); + if (error) + return error; + error = xfbma_get(array, mid, a); + if (error) + return error; + error = xfbma_set(array, mid, b); + if (error) + return error; + return xfbma_set(array, lo, a); +} + +/* + * Perform an insertion sort on a subset of the array. + * Though insertion sort is an O(n^2) algorithm, for small set sizes it's + * faster than quicksort's stack machine, so we let it take over for that. + */ +STATIC int +xfbma_isort( + struct xfbma *array, + xfbma_cmp_fn cmp_fn, + uint64_t start, + uint64_t end) +{ + void *a = xfbma_temp(array, 0); + void *b = xfbma_temp(array, 1); + uint64_t tmp; + uint64_t i; + uint64_t run; + int error; + + /* + * Move the smallest element in a[start..end] to a[start]. This + * simplifies the loop control logic below. + */ + tmp = start; + error = xfbma_get(array, tmp, b); + if (error) + return error; + for (run = start + 1; run <= end; run++) { + /* if a[run] < a[tmp], tmp = run */ + error = xfbma_get(array, run, a); + if (error) + return error; + if (cmp_fn(a, b) < 0) { + tmp = run; + memcpy(b, a, array->obj_size); + } + } + + /* + * The smallest element is a[tmp]; swap with a[start] if tmp != start. + * Recall that a[tmp] is already in *b. + */ + if (tmp != start) { + error = xfbma_get(array, start, a); + if (error) + return error; + error = xfbma_set(array, tmp, a); + if (error) + return error; + error = xfbma_set(array, start, b); + if (error) + return error; + } + + /* + * Perform an insertion sort on a[start+1..end]. We already made sure + * that the smallest value in the original range is now in a[start], + * so the inner loop should never underflow. + * + * For each a[start+2..end], make sure it's in the correct position + * with respect to the elements that came before it. + */ + for (run = start + 2; run <= end; run++) { + error = xfbma_get(array, run, a); + if (error) + return error; + + /* + * Find the correct place for a[run] by walking leftwards + * towards the start of the range until a[tmp] is no longer + * greater than a[run]. + */ + tmp = run - 1; + error = xfbma_get(array, tmp, b); + if (error) + return error; + while (cmp_fn(a, b) < 0) { + tmp--; + error = xfbma_get(array, tmp, b); + if (error) + return error; + } + tmp++; + + /* + * If tmp != run, then a[tmp..run-1] are all less than a[run], + * so right barrel roll a[tmp..run] to get this range in + * sorted order. + */ + if (tmp == run) + continue; + + for (i = run; i >= tmp; i--) { + error = xfbma_get(array, i - 1, b); + if (error) + return error; + error = xfbma_set(array, i, b); + if (error) + return error; + } + error = xfbma_set(array, tmp, a); + if (error) + return error; + } + + return 0; +} + +/* + * Sort the array elements via quicksort. This implementation incorporates + * four optimizations discussed in Sedgewick: + * + * 1. Use an explicit stack of array indicies to store the next array + * partition to sort. This helps us to avoid recursion in the call stack, + * which is particularly expensive in the kernel. + * + * 2. Choose the pivot element using a median-of-three decision tree. This + * reduces the probability of selecting a bad pivot value which causes + * worst case behavior (i.e. partition sizes of 1). Chance are fairly good + * that the list is nearly sorted, so this is important. + * + * 3. The smaller of the two sub-partitions is pushed onto the stack to start + * the next level of recursion, and the larger sub-partition replaces the + * current stack frame. This guarantees that we won't need more than + * log2(nr) stack space. + * + * 4. Use insertion sort for small sets since since insertion sort is faster + * for small, mostly sorted array segments. In the author's experience, + * substituting insertion sort for arrays smaller than 4 elements yields + * a ~10% reduction in runtime. + */ + +/* + * Due to the use of signed indices, we can only support up to 2^63 records. + * Files can only grow to 2^63 bytes, so this is not much of a limitation. + */ +#define QSORT_MAX_RECS (1ULL << 63) + +/* + * For array subsets smaller than 4 elements, it's slightly faster to use + * insertion sort than quicksort's stack machine. + */ +#define ISORT_THRESHOLD (4) +int +xfbma_sort( + struct xfbma *array, + xfbma_cmp_fn cmp_fn) +{ + int64_t *stack; + int64_t *beg; + int64_t *end; + void *pivot = xfbma_temp(array, 0); + void *temp = xfbma_temp(array, 1); + int64_t lo, mid, hi; + const int max_stack_depth = ilog2(array->nr) + 1; + int stack_depth = 0; + int max_stack_used = 0; + int error = 0; + + if (array->nr == 0) + return 0; + if (array->nr >= QSORT_MAX_RECS) + return -E2BIG; + if (array->nr <= ISORT_THRESHOLD) + return xfbma_isort(array, cmp_fn, 0, array->nr - 1); + + /* Allocate our pointer stacks for sorting. */ + stack = kmem_alloc(sizeof(int64_t) * 2 * max_stack_depth, + KM_NOFS | KM_MAYFAIL); + if (!stack) + return -ENOMEM; + beg = stack; + end = &stack[max_stack_depth]; + + beg[0] = 0; + end[0] = array->nr; + while (stack_depth >= 0) { + lo = beg[stack_depth]; + hi = end[stack_depth] - 1; + + /* Nothing left in this partition to sort; pop stack. */ + if (lo >= hi) { + stack_depth--; + continue; + } + + /* Small enough for insertion sort? */ + if (hi - lo <= ISORT_THRESHOLD) { + error = xfbma_isort(array, cmp_fn, lo, hi); + if (error) + goto out_free; + stack_depth--; + continue; + } + + /* Pick a pivot, move it to a[lo] and stash it. */ + mid = lo + ((hi - lo) / 2); + error = xfbma_qsort_pivot(array, cmp_fn, lo, mid, hi); + if (error) + goto out_free; + + error = xfbma_get(array, lo, pivot); + if (error) + goto out_free; + + /* + * Rearrange a[lo..hi] such that everything smaller than the + * pivot is on the left side of the range and everything larger + * than the pivot is on the right side of the range. + */ + while (lo < hi) { + /* + * Decrement hi until it finds an a[hi] less than the + * pivot value. + */ + error = xfbma_get(array, hi, temp); + if (error) + goto out_free; + while (cmp_fn(temp, pivot) >= 0 && lo < hi) { + hi--; + error = xfbma_get(array, hi, temp); + if (error) + goto out_free; + } + + /* Copy that item (a[hi]) to a[lo]. */ + if (lo < hi) { + error = xfbma_set(array, lo++, temp); + if (error) + goto out_free; + } + + /* + * Increment lo until it finds an a[lo] greater than + * the pivot value. + */ + error = xfbma_get(array, lo, temp); + if (error) + goto out_free; + while (cmp_fn(temp, pivot) <= 0 && lo < hi) { + lo++; + error = xfbma_get(array, lo, temp); + if (error) + goto out_free; + } + + /* Copy that item (a[lo]) to a[hi]. */ + if (lo < hi) { + error = xfbma_set(array, hi--, temp); + if (error) + goto out_free; + } + } + + /* + * Put our pivot value in the correct place at a[lo]. All + * values between a[beg[i]] and a[lo - 1] should be less than + * the pivot; and all values between a[lo + 1] and a[end[i]-1] + * should be greater than the pivot. + */ + error = xfbma_set(array, lo, pivot); + if (error) + goto out_free; + + /* + * Set up the pointers for the next iteration. We push onto + * the stack all of the unsorted values between a[lo + 1] and + * a[end[i]], and we tweak the current stack frame to point to + * the unsorted values between a[beg[i]] and a[lo] so that + * those values will be sorted when we pop the stack. + */ + beg[stack_depth + 1] = lo + 1; + end[stack_depth + 1] = end[stack_depth]; + end[stack_depth++] = lo; + + /* Check our stack usage. */ + max_stack_used = max(max_stack_used, stack_depth); + if (stack_depth >= max_stack_depth) { + ASSERT(0); + return -EFSCORRUPTED; + } + + /* + * Always start with the smaller of the two partitions to keep + * the amount of recursion in check. + */ + if (end[stack_depth] - beg[stack_depth] > + end[stack_depth - 1] - beg[stack_depth - 1]) { + swap(beg[stack_depth], beg[stack_depth - 1]); + swap(end[stack_depth], end[stack_depth - 1]); + } + } + +out_free: + kfree(stack); + trace_xfbma_sort_stats(array->nr, max_stack_depth, max_stack_used, + error); + return error; +} diff --git a/fs/xfs/scrub/array.h b/fs/xfs/scrub/array.h new file mode 100644 index 000000000000..7108d02cfdcd --- /dev/null +++ b/fs/xfs/scrub/array.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#ifndef __XFS_SCRUB_ARRAY_H__ +#define __XFS_SCRUB_ARRAY_H__ + +struct xfbma { + struct file *filp; + size_t obj_size; + uint64_t nr; +}; + +struct xfbma *xfbma_init(size_t obj_size); +void xfbma_destroy(struct xfbma *array); +int xfbma_get(struct xfbma *array, uint64_t nr, void *ptr); +int xfbma_set(struct xfbma *array, uint64_t nr, void *ptr); +int xfbma_insert_anywhere(struct xfbma *array, void *ptr); +bool xfbma_is_null(struct xfbma *array, void *ptr); +int xfbma_nullify(struct xfbma *array, uint64_t nr); +int xfbma_append(struct xfbma *array, void *ptr); +uint64_t xfbma_length(struct xfbma *array); + +/* + * Return codes for the array iterator function are 0 to continue iterating, + * and non-zero to stop iterating. Any non-zero value will be passed up to the + * iteration caller. The special value -ECANCELED can be used to stop + * iteration, because the iterator never generates that error code on its own. + */ +typedef int (*xfbma_iter_fn)(const void *item, void *priv); + +int xfbma_iter_del(struct xfbma *array, xfbma_iter_fn iter_fn, void *priv); + +typedef int (*xfbma_cmp_fn)(const void *a, const void *b); + +int xfbma_sort(struct xfbma *array, xfbma_cmp_fn cmp_fn); + +#define foreach_xfbma_item(array, i, rec) \ + for ((i) = 0; (i) < xfbma_length((array)); (i)++) \ + if (xfbma_get((array), (i), &(rec)) == 0 && \ + !xfbma_is_null((array), &(rec))) + +#endif /* __XFS_SCRUB_ARRAY_H__ */ diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 651d7b8ee09f..7ba12e68ea01 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -940,6 +940,29 @@ DEFINE_NEWBT_EXTENT_EVENT(xrep_newbt_alloc_blocks); DEFINE_NEWBT_EXTENT_EVENT(xrep_newbt_free_blocks); DEFINE_NEWBT_EXTENT_EVENT(xrep_newbt_claim_block); +TRACE_EVENT(xfbma_sort_stats, + TP_PROTO(uint64_t nr, unsigned int max_stack_depth, + unsigned int max_stack_used, int error), + TP_ARGS(nr, max_stack_depth, max_stack_used, error), + TP_STRUCT__entry( + __field(uint64_t, nr) + __field(unsigned int, max_stack_depth) + __field(unsigned int, max_stack_used) + __field(int, error) + ), + TP_fast_assign( + __entry->nr = nr; + __entry->max_stack_depth = max_stack_depth; + __entry->max_stack_used = max_stack_used; + __entry->error = error; + ), + TP_printk("nr %llu max_depth %u max_used %u error %d", + __entry->nr, + __entry->max_stack_depth, + __entry->max_stack_used, + __entry->error) +); + #endif /* IS_ENABLED(CONFIG_XFS_ONLINE_REPAIR) */ #endif /* _TRACE_XFS_SCRUB_TRACE_H */ diff --git a/fs/xfs/scrub/xfile.c b/fs/xfs/scrub/xfile.c new file mode 100644 index 000000000000..2d96e2f9917c --- /dev/null +++ b/fs/xfs/scrub/xfile.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "xfs.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "scrub/array.h" +#include "scrub/scrub.h" +#include "scrub/trace.h" +#include "scrub/xfile.h" +#include + +/* + * Create a memfd to our specifications and return a file pointer. The file + * is not installed in the file description table (because userspace has no + * business accessing our internal data), which means that the caller /must/ + * fput the file when finished. + */ +struct file * +xfile_create( + const char *description) +{ + struct file *filp; + + filp = shmem_file_setup(description, 0, 0); + if (IS_ERR_OR_NULL(filp)) + return filp; + + filp->f_mode |= FMODE_PREAD | FMODE_PWRITE | FMODE_NOCMTIME; + filp->f_flags |= O_RDWR | O_LARGEFILE | O_NOATIME; + return filp; +} + +void +xfile_destroy( + struct file *filp) +{ + fput(filp); +} + +/* + * Perform a read or write IO to the file backing the array. We can defer + * the work to a workqueue if the caller so desires, either to reduce stack + * usage or because the xfs is frozen and we want to avoid deadlocking on the + * page fault that might be about to happen. + */ +int +xfile_io( + struct file *filp, + unsigned int cmd_flags, + loff_t *pos, + void *ptr, + size_t count) +{ + ssize_t ret; + unsigned int pflags = memalloc_nofs_save(); + + if ((cmd_flags & XFILE_IO_MASK) == XFILE_IO_READ) + ret = kernel_read(filp, ptr, count, pos); + else + ret = kernel_write(filp, ptr, count, pos); + memalloc_nofs_restore(pflags); + + /* + * Since we're treating this file as "memory", any IO error should be + * treated as a failure to find any memory. + */ + return ret == count ? 0 : -ENOMEM; +} + +/* Discard pages backing a range of the file. */ +void +xfile_discard( + struct file *filp, + loff_t start, + loff_t end) +{ + shmem_truncate_range(file_inode(filp), start, end); +} diff --git a/fs/xfs/scrub/xfile.h b/fs/xfs/scrub/xfile.h new file mode 100644 index 000000000000..41817bcadc43 --- /dev/null +++ b/fs/xfs/scrub/xfile.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#ifndef __XFS_SCRUB_XFILE_H__ +#define __XFS_SCRUB_XFILE_H__ + +struct file *xfile_create(const char *description); +void xfile_destroy(struct file *filp); + +/* read or write? */ +#define XFILE_IO_READ (0) +#define XFILE_IO_WRITE (1) +#define XFILE_IO_MASK (1 << 0) +int xfile_io(struct file *filp, unsigned int cmd_flags, loff_t *pos, + void *ptr, size_t count); + +void xfile_discard(struct file *filp, loff_t start, loff_t end); + +#endif /* __XFS_SCRUB_XFILE_H__ */ From patchwork Tue Oct 29 23:32:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11218809 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D86971390 for ; Tue, 29 Oct 2019 23:32:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8509B2083E for ; Tue, 29 Oct 2019 23:32:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="ODp3DXde" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726048AbfJ2XcN (ORCPT ); Tue, 29 Oct 2019 19:32:13 -0400 Received: from userp2130.oracle.com ([156.151.31.86]:43046 "EHLO userp2130.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726234AbfJ2XcN (ORCPT ); Tue, 29 Oct 2019 19:32:13 -0400 Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNTvTS038733 for ; Tue, 29 Oct 2019 23:32:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=+2c0lusfdmX60LGQ96fmK5bWtAZUwR4JMcpacX6eliY=; b=ODp3DXdeLWVQzZMHSPMvUn0EFnHysJf8xhr+HdQmTesnU1lqEYuy5CYfSyado+CmG6gA Rg7CR4oTMlq5Kfo5bGzTehnmIPFQkaJjtMrrqZ8P4srOeKA3y3MI51azImTyeeJQexir eMe8EX1htuev31OBCx6THeC8FsdzlK+yzw4xiMFCLsJeEGKa74r5T0+JTNUEjBwfQKPR XHnHGTy8/aOveUgmhaI72CRM8uFDtmDKLf15TnlAHVm8jY435hLSKUQC/XWigDMnV4iE 1+o6Qmf3PJrdFYOPRYxL929ORGWtu0nF7fFCvXVGDFm305B2jzjgFgCdj8Nq/yj0lnx2 1g== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2130.oracle.com with ESMTP id 2vxwhfgb5g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:07 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNRlnL069190 for ; Tue, 29 Oct 2019 23:32:07 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3030.oracle.com with ESMTP id 2vxwhuvgkt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:07 +0000 Received: from abhmp0020.oracle.com (abhmp0020.oracle.com [141.146.116.26]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x9TNW6Ni012138 for ; Tue, 29 Oct 2019 23:32:06 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Oct 2019 16:32:05 -0700 Subject: [PATCH 3/5] xfs: repair free space btrees From: "Darrick J. Wong" To: darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Tue, 29 Oct 2019 16:32:04 -0700 Message-ID: <157239192478.1267304.9946411344337358863.stgit@magnolia> In-Reply-To: <157239190609.1267304.9008396217521176875.stgit@magnolia> References: <157239190609.1267304.9008396217521176875.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Rebuild the free space btrees from the gaps in the rmap btree. Signed-off-by: Darrick J. Wong --- fs/xfs/Makefile | 1 fs/xfs/libxfs/xfs_ag_resv.c | 2 fs/xfs/libxfs/xfs_types.h | 7 fs/xfs/scrub/alloc_repair.c | 718 +++++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/common.c | 8 fs/xfs/scrub/repair.h | 11 + fs/xfs/scrub/scrub.c | 11 - fs/xfs/scrub/scrub.h | 8 fs/xfs/scrub/trace.h | 24 + fs/xfs/xfs_extent_busy.c | 13 + fs/xfs/xfs_extent_busy.h | 2 11 files changed, 800 insertions(+), 5 deletions(-) create mode 100644 fs/xfs/scrub/alloc_repair.c diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index d21b59cfc530..ffcebf428ca9 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile @@ -160,6 +160,7 @@ xfs-$(CONFIG_XFS_QUOTA) += scrub/quota.o ifeq ($(CONFIG_XFS_ONLINE_REPAIR),y) xfs-y += $(addprefix scrub/, \ agheader_repair.o \ + alloc_repair.o \ array.o \ bitmap.o \ repair.o \ diff --git a/fs/xfs/libxfs/xfs_ag_resv.c b/fs/xfs/libxfs/xfs_ag_resv.c index 87a9747f1d36..3f79958ce08e 100644 --- a/fs/xfs/libxfs/xfs_ag_resv.c +++ b/fs/xfs/libxfs/xfs_ag_resv.c @@ -381,6 +381,8 @@ xfs_ag_resv_free_extent( /* fall through */ case XFS_AG_RESV_NONE: xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, (int64_t)len); + /* fall through */ + case XFS_AG_RESV_IGNORE: return; } diff --git a/fs/xfs/libxfs/xfs_types.h b/fs/xfs/libxfs/xfs_types.h index 300b3e91ca3a..6ed204333e51 100644 --- a/fs/xfs/libxfs/xfs_types.h +++ b/fs/xfs/libxfs/xfs_types.h @@ -175,6 +175,13 @@ enum xfs_ag_resv_type { XFS_AG_RESV_AGFL, XFS_AG_RESV_METADATA, XFS_AG_RESV_RMAPBT, + + /* + * Don't increase fdblocks when freeing extent. This is a pony for + * the bnobt repair functions to re-free the free space without + * altering fdblocks. If you think you need this you're wrong. + */ + XFS_AG_RESV_IGNORE, }; /* diff --git a/fs/xfs/scrub/alloc_repair.c b/fs/xfs/scrub/alloc_repair.c new file mode 100644 index 000000000000..d1904e080a7c --- /dev/null +++ b/fs/xfs/scrub/alloc_repair.c @@ -0,0 +1,718 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "xfs.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_defer.h" +#include "xfs_btree.h" +#include "xfs_bit.h" +#include "xfs_log_format.h" +#include "xfs_trans.h" +#include "xfs_sb.h" +#include "xfs_alloc.h" +#include "xfs_alloc_btree.h" +#include "xfs_rmap.h" +#include "xfs_rmap_btree.h" +#include "xfs_inode.h" +#include "xfs_refcount.h" +#include "xfs_extent_busy.h" +#include "xfs_health.h" +#include "xfs_bmap.h" +#include "scrub/xfs_scrub.h" +#include "scrub/scrub.h" +#include "scrub/common.h" +#include "scrub/btree.h" +#include "scrub/trace.h" +#include "scrub/repair.h" +#include "scrub/bitmap.h" +#include "scrub/array.h" + +/* + * Free Space Btree Repair + * ======================= + * + * The reverse mappings are supposed to record all space usage for the entire + * AG. Therefore, we can recalculate the free extents in an AG by looking for + * gaps in the physical extents recorded in the rmapbt. On a reflink + * filesystem this is a little more tricky in that we have to be aware that + * the rmap records are allowed to overlap. + * + * We derive which blocks belonged to the old bnobt/cntbt by recording all the + * OWN_AG extents and subtracting out the blocks owned by all other OWN_AG + * metadata: the rmapbt blocks visited while iterating the reverse mappings + * and the AGFL blocks. + * + * Once we have both of those pieces, we can reconstruct the bnobt and cntbt + * by blowing out the free block state and freeing all the extents that we + * found. This adds the requirement that we can't have any busy extents in + * the AG because the busy code cannot handle duplicate records. + * + * Note that we can only rebuild both free space btrees at the same time + * because the regular extent freeing infrastructure loads both btrees at the + * same time. + * + * We use the prefix 'xrep_abt' here because we regenerate both free space + * allocation btrees at the same time. + */ + +struct xrep_abt { + /* Blocks owned by the rmapbt or the agfl. */ + struct xbitmap not_allocbt_blocks; + + /* All OWN_AG blocks. */ + struct xbitmap old_allocbt_blocks; + + /* + * New bnobt information. All btree block reservations are added to + * the reservation list in new_bnobt_info. + */ + struct xrep_newbt new_bnobt_info; + struct xfs_btree_bload bno_bload; + + /* new cntbt information */ + struct xrep_newbt new_cntbt_info; + struct xfs_btree_bload cnt_bload; + + /* Free space extents. */ + struct xfbma *free_records; + + struct xfs_scrub *sc; + + /* Number of non-null records in @free_records. */ + uint64_t nr_real_records; + + /* get_data()'s position in the free space record array. */ + uint64_t iter; + + /* + * Next block we anticipate seeing in the rmap records. If the next + * rmap record is greater than next_bno, we have found unused space. + */ + xfs_agblock_t next_bno; + + /* Number of free blocks in this AG. */ + xfs_agblock_t nr_blocks; + + /* Longest free extent we found in the AG. */ + xfs_agblock_t longest; +}; + +/* + * Stash a free space record for all the space since the last bno we found + * all the way up to @end. + */ +static int +xrep_abt_stash( + struct xrep_abt *ra, + xfs_agblock_t end) +{ + struct xfs_alloc_rec_incore arec = { + .ar_startblock = ra->next_bno, + .ar_blockcount = end - ra->next_bno, + }; + int error; + + trace_xrep_abt_found(ra->sc->mp, ra->sc->sa.agno, arec.ar_startblock, + arec.ar_blockcount); + + error = xfbma_append(ra->free_records, &arec); + if (error) + return error; + ra->nr_blocks += arec.ar_blockcount; + return 0; +} + +/* Record extents that aren't in use from gaps in the rmap records. */ +STATIC int +xrep_abt_walk_rmap( + struct xfs_btree_cur *cur, + struct xfs_rmap_irec *rec, + void *priv) +{ + struct xrep_abt *ra = priv; + xfs_fsblock_t fsb; + int error; + + /* Record all the OWN_AG blocks... */ + if (rec->rm_owner == XFS_RMAP_OWN_AG) { + fsb = XFS_AGB_TO_FSB(cur->bc_mp, cur->bc_private.a.agno, + rec->rm_startblock); + error = xbitmap_set(&ra->old_allocbt_blocks, fsb, + rec->rm_blockcount); + if (error) + return error; + } + + /* ...and all the rmapbt blocks... */ + error = xbitmap_set_btcur_path(&ra->not_allocbt_blocks, cur); + if (error) + return error; + + /* ...and all the free space. */ + if (rec->rm_startblock > ra->next_bno) { + error = xrep_abt_stash(ra, rec->rm_startblock); + if (error) + return error; + } + + /* + * rmap records can overlap on reflink filesystems, so project next_bno + * as far out into the AG space as we currently know about. + */ + ra->next_bno = max_t(xfs_agblock_t, ra->next_bno, + rec->rm_startblock + rec->rm_blockcount); + return 0; +} + +/* Collect an AGFL block for the not-to-release list. */ +static int +xrep_abt_walk_agfl( + struct xfs_mount *mp, + xfs_agblock_t bno, + void *priv) +{ + struct xrep_abt *ra = priv; + xfs_fsblock_t fsb; + + fsb = XFS_AGB_TO_FSB(mp, ra->sc->sa.agno, bno); + return xbitmap_set(&ra->not_allocbt_blocks, fsb, 1); +} + +/* + * Compare two free space extents by block number. We want to sort by block + * number. + */ +static int +xrep_bnobt_extent_cmp( + const void *a, + const void *b) +{ + const struct xfs_alloc_rec_incore *ap = a; + const struct xfs_alloc_rec_incore *bp = b; + + if (ap->ar_startblock > bp->ar_startblock) + return 1; + else if (ap->ar_startblock < bp->ar_startblock) + return -1; + return 0; +} + +/* + * Compare two free space extents by length and then block number. We want + * to sort first in order of decreasing length and then in increasing block + * number. + */ +static int +xrep_cntbt_extent_cmp( + const void *a, + const void *b) +{ + const struct xfs_alloc_rec_incore *ap = a; + const struct xfs_alloc_rec_incore *bp = b; + + if (ap->ar_blockcount > bp->ar_blockcount) + return 1; + else if (ap->ar_blockcount < bp->ar_blockcount) + return -1; + return xrep_bnobt_extent_cmp(a, b); +} + +/* + * Iterate all reverse mappings to find (1) the gaps between rmap records (all + * unowned space), (2) the OWN_AG extents (which encompass the free space + * btrees, the rmapbt, and the agfl), (3) the rmapbt blocks, and (4) the AGFL + * blocks. The free space is (1) + (2) - (3) - (4). + */ +STATIC int +xrep_abt_find_freespace( + struct xrep_abt *ra) +{ + struct xfs_scrub *sc = ra->sc; + struct xfs_btree_cur *cur; + struct xfs_mount *mp = sc->mp; + xfs_agblock_t agend; + int error; + + xbitmap_init(&ra->not_allocbt_blocks); + + /* + * Iterate all the reverse mappings to find gaps in the physical + * mappings, all the OWN_AG blocks, and all the rmapbt extents. + */ + cur = xfs_rmapbt_init_cursor(mp, sc->tp, sc->sa.agf_bp, sc->sa.agno); + error = xfs_rmap_query_all(cur, xrep_abt_walk_rmap, ra); + xfs_btree_del_cursor(cur, error); + if (error) + goto err; + + /* Insert a record for space between the last rmap and EOAG. */ + agend = be32_to_cpu(XFS_BUF_TO_AGF(sc->sa.agf_bp)->agf_length); + if (ra->next_bno < agend) { + error = xrep_abt_stash(ra, agend); + if (error) + goto err; + } + + /* Collect all the AGFL blocks. */ + error = xfs_agfl_walk(mp, XFS_BUF_TO_AGF(sc->sa.agf_bp), + sc->sa.agfl_bp, xrep_abt_walk_agfl, ra); + if (error) + goto err; + + /* Compute the old bnobt/cntbt blocks. */ + xbitmap_disunion(&ra->old_allocbt_blocks, &ra->not_allocbt_blocks); + + ra->nr_real_records = xfbma_length(ra->free_records); +err: + xbitmap_destroy(&ra->not_allocbt_blocks); + return error; +} + +/* + * We're going to use the observed free space records to reserve blocks for the + * new free space btrees, so we play an iterative game where we try to converge + * on the number of blocks we need: + * + * 1. Estimate how many blocks we'll need to store the records. + * 2. If the first free record has more blocks than we need, we're done. + * We will have to re-sort the records prior to building the cntbt. + * 3. If that record has exactly the number of blocks we need, null out the + * record. We're done. + * 4. Otherwise, we still need more blocks. Null out the record, subtract its + * length from the number of blocks we need, and go back to step 1. + * + * Fortunately, we don't have to do any transaction work to play this game, so + * we don't have to tear down the staging cursors. + */ +STATIC int +xrep_abt_reserve_space( + struct xrep_abt *ra, + struct xfs_btree_cur *bno_cur, + struct xfs_btree_cur *cnt_cur, + bool *need_resort) +{ + struct xfs_scrub *sc = ra->sc; + uint64_t record_nr = xfbma_length(ra->free_records) - 1; + unsigned int allocated = 0; + int error = 0; + + *need_resort = false; + do { + struct xfs_alloc_rec_incore arec; + uint64_t required; + unsigned int desired; + unsigned int found; + + /* Compute how many blocks we'll need. */ + error = xfs_btree_bload_compute_geometry(cnt_cur, + &ra->cnt_bload, ra->nr_real_records); + if (error) + break; + + error = xfs_btree_bload_compute_geometry(bno_cur, + &ra->bno_bload, ra->nr_real_records); + if (error) + break; + + /* How many btree blocks do we need to store all records? */ + required = ra->cnt_bload.nr_blocks + ra->bno_bload.nr_blocks; + ASSERT(required < INT_MAX); + + /* If we've reserved enough blocks, we're done. */ + if (allocated >= required) + break; + + desired = required - allocated; + + /* We need space but there's none left; bye! */ + if (ra->nr_real_records == 0) { + error = -ENOSPC; + break; + } + + /* Grab the first record from the list. */ + error = xfbma_get(ra->free_records, record_nr, &arec); + if (error) + break; + + ASSERT(arec.ar_blockcount <= UINT_MAX); + found = min_t(unsigned int, arec.ar_blockcount, desired); + + error = xrep_newbt_add_blocks(&ra->new_bnobt_info, + XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, + arec.ar_startblock), + found, NULL); + if (error) + break; + allocated += found; + ra->nr_blocks -= found; + + if (arec.ar_blockcount > desired) { + /* + * Record has more space than we need. The number of + * free records doesn't change, so shrink the free + * record and exit the loop. + */ + arec.ar_startblock += desired; + arec.ar_blockcount -= desired; + error = xfbma_set(ra->free_records, record_nr, &arec); + if (error) + break; + *need_resort = true; + break; + } + + /* + * We're going to use up the entire record, so nullify it and + * move on to the next one. This changes the number of free + * records, so we must go around the loop once more to re-run + * _bload_init. + */ + error = xfbma_nullify(ra->free_records, record_nr); + if (error) + break; + ra->nr_real_records--; + record_nr--; + } while (1); + + return error; +} + +/* + * Deal with all the space we reserved. Blocks that were allocated for the + * free space btrees need to have a (deferred) rmap added for the OWN_AG + * allocation, and blocks that didn't get used can be freed via the usual + * (deferred) means. + */ +STATIC void +xrep_abt_dispose_reservations( + struct xrep_abt *ra, + int error) +{ + struct xrep_newbt_resv *resv, *n; + struct xfs_scrub *sc = ra->sc; + + if (error) + goto junkit; + + for_each_xrep_newbt_reservation(&ra->new_bnobt_info, resv, n) { + /* Add a deferred rmap for each extent we used. */ + if (resv->used > 0) + xfs_rmap_alloc_extent(sc->tp, + XFS_FSB_TO_AGNO(sc->mp, resv->fsbno), + XFS_FSB_TO_AGBNO(sc->mp, resv->fsbno), + resv->used, XFS_RMAP_OWN_AG); + + /* + * Add a deferred free for each block we didn't use and now + * have to add to the free space since the new btrees are + * online. + */ + if (resv->used < resv->len) + __xfs_bmap_add_free(sc->tp, resv->fsbno + resv->used, + resv->len - resv->used, NULL, true); + } + +junkit: + for_each_xrep_newbt_reservation(&ra->new_bnobt_info, resv, n) { + list_del(&resv->list); + kmem_free(resv); + } + + xrep_newbt_destroy(&ra->new_bnobt_info, error); + xrep_newbt_destroy(&ra->new_cntbt_info, error); +} + +/* Retrieve free space data for bulk load. */ +STATIC int +xrep_abt_get_data( + struct xfs_btree_cur *cur, + void *priv) +{ + struct xfs_alloc_rec_incore *arec = &cur->bc_rec.a; + struct xrep_abt *ra = priv; + int error; + + do { + error = xfbma_get(ra->free_records, ra->iter++, arec); + } while (error == 0 && xfbma_is_null(ra->free_records, arec)); + + ra->longest = max(ra->longest, arec->ar_blockcount); + return error; +} + +/* Feed one of the new btree blocks to the bulk loader. */ +STATIC int +xrep_abt_alloc_block( + struct xfs_btree_cur *cur, + union xfs_btree_ptr *ptr, + void *priv) +{ + struct xrep_abt *ra = priv; + + return xrep_newbt_claim_block(cur, &ra->new_bnobt_info, ptr); +} + +/* + * Reset the AGF counters to reflect the free space btrees that we just + * rebuilt, then reinitialize the per-AG data. + */ +STATIC int +xrep_abt_reset_counters( + struct xrep_abt *ra, + unsigned int freesp_btreeblks) +{ + struct xfs_scrub *sc = ra->sc; + struct xfs_perag *pag = sc->sa.pag; + struct xfs_agf *agf; + struct xfs_buf *bp; + + agf = XFS_BUF_TO_AGF(sc->sa.agf_bp); + + /* + * Mark the pagf information stale and use the accessor function to + * forcibly reload it from the values we just logged. We still own the + * AGF buffer so we can safely ignore bp. + */ + ASSERT(pag->pagf_init); + pag->pagf_init = 0; + + agf->agf_btreeblks = cpu_to_be32(freesp_btreeblks + + (be32_to_cpu(agf->agf_rmap_blocks) - 1)); + agf->agf_freeblks = cpu_to_be32(ra->nr_blocks); + agf->agf_longest = cpu_to_be32(ra->longest); + xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_BTREEBLKS | + XFS_AGF_LONGEST | + XFS_AGF_FREEBLKS); + + return xfs_alloc_read_agf(sc->mp, sc->tp, sc->sa.agno, 0, &bp); +} + +static void +xrep_abt_init_bload( + struct xrep_abt *ra, + struct xfs_btree_bload *bload) +{ + bload->get_data = xrep_abt_get_data; + bload->alloc_block = xrep_abt_alloc_block; + + xrep_bload_estimate_slack(ra->sc, bload); +} + +/* + * Use the collected free space information to stage new free space btrees. + * If this is successful we'll return with the new btree root + * information logged to the repair transaction but not yet committed. + */ +STATIC int +xrep_abt_build_new_trees( + struct xrep_abt *ra) +{ + struct xfs_scrub *sc = ra->sc; + struct xfs_btree_cur *bno_cur; + struct xfs_btree_cur *cnt_cur; + bool need_resort; + int error; + + xrep_abt_init_bload(ra, &ra->bno_bload); + xrep_abt_init_bload(ra, &ra->cnt_bload); + + /* + * Sort the free extents by length so that we can set up the free space + * btrees in as few extents as possible. This reduces the amount of + * deferred rmap / free work we have to do at the end. + */ + error = xfbma_sort(ra->free_records, xrep_cntbt_extent_cmp); + if (error) + return error; + + /* + * Prepare to construct the new btree by reserving disk space for the + * new btree and setting up all the accounting information we'll need + * to root the new btree while it's under construction and before we + * attach it to the AG header. + */ + xrep_newbt_init_bare(&ra->new_bnobt_info, sc); + xrep_newbt_init_bare(&ra->new_cntbt_info, sc); + + /* Allocate cursors for the staged btrees. */ + bno_cur = xfs_allocbt_stage_cursor(sc->mp, sc->tp, + &ra->new_bnobt_info.afake, sc->sa.agno, XFS_BTNUM_BNO); + cnt_cur = xfs_allocbt_stage_cursor(sc->mp, sc->tp, + &ra->new_cntbt_info.afake, sc->sa.agno, XFS_BTNUM_CNT); + + /* Reserve the space we'll need for the new btrees. */ + error = xrep_abt_reserve_space(ra, bno_cur, cnt_cur, &need_resort); + if (error) + goto out_cur; + + /* + * If we need to re-sort the free extents by length, do so so that we + * can put the records into the cntbt in the correct order. + */ + if (need_resort) { + error = xfbma_sort(ra->free_records, xrep_cntbt_extent_cmp); + if (error) + goto out_cur; + } + + /* Load the free space by length tree. */ + ra->iter = 0; + ra->longest = 0; + error = xfs_btree_bload(cnt_cur, &ra->cnt_bload, ra); + if (error) + goto out_cur; + + /* Re-sort the free extents by block number so so that we can put the + * records into the bnobt in the correct order. + */ + error = xfbma_sort(ra->free_records, xrep_bnobt_extent_cmp); + if (error) + goto out_cur; + + /* Load the free space by block number tree. */ + ra->iter = 0; + error = xfs_btree_bload(bno_cur, &ra->bno_bload, ra); + if (error) + goto out_cur; + + /* + * Install the new btrees in the AG header. After this point the old + * btree is no longer accessible and the new tree is live. + * + * Note: We re-read the AGF here to ensure the buffer type is set + * properly. Since we built a new tree without attaching to the AGF + * buffer, the buffer item may have fallen off the buffer. This ought + * to succeed since the AGF is held across transaction rolls. + */ + error = xfs_read_agf(sc->mp, sc->tp, sc->sa.agno, 0, &sc->sa.agf_bp); + if (error) + goto out_cur; + + /* Commit our new btrees. */ + xfs_allocbt_commit_staged_btree(bno_cur, sc->sa.agf_bp); + xfs_btree_del_cursor(bno_cur, 0); + xfs_allocbt_commit_staged_btree(cnt_cur, sc->sa.agf_bp); + xfs_btree_del_cursor(cnt_cur, 0); + + /* Reset the AGF counters now that we've changed the btree shape. */ + error = xrep_abt_reset_counters(ra, (ra->bno_bload.nr_blocks - 1) + + (ra->cnt_bload.nr_blocks - 1)); + if (error) + goto out_newbt; + + /* Dispose of any unused blocks and the accounting information. */ + xrep_abt_dispose_reservations(ra, error); + + return xrep_roll_ag_trans(sc); + +out_cur: + xfs_btree_del_cursor(cnt_cur, error); + xfs_btree_del_cursor(bno_cur, error); +out_newbt: + xrep_abt_dispose_reservations(ra, error); + return error; +} + +/* + * Now that we've logged the roots of the new btrees, invalidate all of the + * old blocks and free them. + */ +STATIC int +xrep_abt_remove_old_trees( + struct xrep_abt *ra) +{ + /* Free the old inode btree blocks if they're not in use. */ + return xrep_reap_extents(ra->sc, &ra->old_allocbt_blocks, + &XFS_RMAP_OINFO_AG, XFS_AG_RESV_IGNORE); +} + +/* Repair the freespace btrees for some AG. */ +int +xrep_allocbt( + struct xfs_scrub *sc) +{ + struct xrep_abt *ra; + struct xfs_mount *mp = sc->mp; + int error; + + /* We require the rmapbt to rebuild anything. */ + if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) + return -EOPNOTSUPP; + + ra = kmem_zalloc(sizeof(struct xrep_abt), KM_NOFS | KM_MAYFAIL); + if (!ra) + return -ENOMEM; + ra->sc = sc; + + /* We rebuild both data structures. */ + sc->sick_mask = XFS_SICK_AG_BNOBT | XFS_SICK_AG_CNTBT; + + xchk_perag_get(sc->mp, &sc->sa); + + /* + * Make sure the busy extent list is clear because we can't put + * extents on there twice. + */ + if (!xfs_extent_busy_list_empty(sc->sa.pag)) + return -EDEADLOCK; + + /* Set up some storage */ + ra->free_records = xfbma_init(sizeof(struct xfs_alloc_rec_incore)); + if (IS_ERR(ra->free_records)) { + error = PTR_ERR(ra->free_records); + goto out_ra; + } + + /* Collect the free space data and find the old btree blocks. */ + xbitmap_init(&ra->old_allocbt_blocks); + error = xrep_abt_find_freespace(ra); + if (error) + goto out_bitmap; + + /* Rebuild the free space information. */ + error = xrep_abt_build_new_trees(ra); + if (error) + goto out_bitmap; + + /* Kill the old trees. */ + error = xrep_abt_remove_old_trees(ra); + +out_bitmap: + xbitmap_destroy(&ra->old_allocbt_blocks); + xfbma_destroy(ra->free_records); +out_ra: + kmem_free(ra); + return error; +} + +/* Make sure both btrees are ok after we've rebuilt them. */ +int +xrep_revalidate_allocbt( + struct xfs_scrub *sc) +{ + __u32 old_type = sc->sm->sm_type; + int error; + + /* + * We must update sm_type temporarily so that the tree-to-tree cross + * reference checks will work in the correct direction, and also so + * that tracing will report correctly if there are more errors. + */ + sc->sm->sm_type = XFS_SCRUB_TYPE_BNOBT; + error = xchk_bnobt(sc); + if (error) + goto out; + + sc->sm->sm_type = XFS_SCRUB_TYPE_CNTBT; + error = xchk_cntbt(sc); +out: + sc->sm->sm_type = old_type; + return error; +} diff --git a/fs/xfs/scrub/common.c b/fs/xfs/scrub/common.c index 18876056e5e0..4a49a9099477 100644 --- a/fs/xfs/scrub/common.c +++ b/fs/xfs/scrub/common.c @@ -634,8 +634,14 @@ xchk_setup_ag_btree( * expensive operation should be performed infrequently and only * as a last resort. Any caller that sets force_log should * document why they need to do so. + * + * Force everything in memory out to disk if we're repairing. + * This ensures we won't get tripped up by btree blocks sitting + * in memory waiting to have LSNs stamped in. The AGF/AGI repair + * routines use any available rmap data to try to find a btree + * root that also passes the read verifiers. */ - if (force_log) { + if (force_log || (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) { error = xchk_checkpoint_log(mp); if (error) return error; diff --git a/fs/xfs/scrub/repair.h b/fs/xfs/scrub/repair.h index 6ca5dc8dfb2d..725e6d69f003 100644 --- a/fs/xfs/scrub/repair.h +++ b/fs/xfs/scrub/repair.h @@ -55,6 +55,10 @@ int xrep_find_ag_btree_roots(struct xfs_scrub *sc, struct xfs_buf *agf_bp, void xrep_force_quotacheck(struct xfs_scrub *sc, uint dqtype); int xrep_ino_dqattach(struct xfs_scrub *sc); +/* Metadata revalidators */ + +int xrep_revalidate_allocbt(struct xfs_scrub *sc); + /* Metadata repairers */ int xrep_probe(struct xfs_scrub *sc); @@ -62,6 +66,7 @@ int xrep_superblock(struct xfs_scrub *sc); int xrep_agf(struct xfs_scrub *sc); int xrep_agfl(struct xfs_scrub *sc); int xrep_agi(struct xfs_scrub *sc); +int xrep_allocbt(struct xfs_scrub *sc); struct xrep_newbt_resv { /* Link to list of extents that we've reserved. */ @@ -101,6 +106,9 @@ struct xrep_newbt { enum xfs_ag_resv_type resv; }; +#define for_each_xrep_newbt_reservation(xnr, resv, n) \ + list_for_each_entry_safe((resv), (n), &(xnr)->resv_list, list) + void xrep_newbt_init_bare(struct xrep_newbt *xba, struct xfs_scrub *sc); void xrep_newbt_init_ag(struct xrep_newbt *xba, struct xfs_scrub *sc, const struct xfs_owner_info *oinfo, xfs_fsblock_t alloc_hint, @@ -135,11 +143,14 @@ xrep_calc_ag_resblks( return 0; } +#define xrep_revalidate_allocbt (NULL) + #define xrep_probe xrep_notsupported #define xrep_superblock xrep_notsupported #define xrep_agf xrep_notsupported #define xrep_agfl xrep_notsupported #define xrep_agi xrep_notsupported +#define xrep_allocbt xrep_notsupported #endif /* CONFIG_XFS_ONLINE_REPAIR */ diff --git a/fs/xfs/scrub/scrub.c b/fs/xfs/scrub/scrub.c index 15c8c5f3f688..b42ac8ecdb49 100644 --- a/fs/xfs/scrub/scrub.c +++ b/fs/xfs/scrub/scrub.c @@ -217,13 +217,15 @@ static const struct xchk_meta_ops meta_scrub_ops[] = { .type = ST_PERAG, .setup = xchk_setup_ag_allocbt, .scrub = xchk_bnobt, - .repair = xrep_notsupported, + .repair = xrep_allocbt, + .repair_eval = xrep_revalidate_allocbt, }, [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */ .type = ST_PERAG, .setup = xchk_setup_ag_allocbt, .scrub = xchk_cntbt, - .repair = xrep_notsupported, + .repair = xrep_allocbt, + .repair_eval = xrep_revalidate_allocbt, }, [XFS_SCRUB_TYPE_INOBT] = { /* inobt */ .type = ST_PERAG, @@ -495,7 +497,10 @@ xfs_scrub_metadata( goto out_teardown; /* Scrub for errors. */ - error = sc.ops->scrub(&sc); + if ((sc.flags & XREP_ALREADY_FIXED) && sc.ops->repair_eval != NULL) + error = sc.ops->repair_eval(&sc); + else + error = sc.ops->scrub(&sc); if (!(sc.flags & XCHK_TRY_HARDER) && error == -EDEADLOCK) { /* * Scrubbers return -EDEADLOCK to mean 'try harder'. diff --git a/fs/xfs/scrub/scrub.h b/fs/xfs/scrub/scrub.h index ad1ceb44a628..94a30637a127 100644 --- a/fs/xfs/scrub/scrub.h +++ b/fs/xfs/scrub/scrub.h @@ -27,6 +27,14 @@ struct xchk_meta_ops { /* Repair or optimize the metadata. */ int (*repair)(struct xfs_scrub *); + /* + * Re-scrub the metadata we repaired, in case there's extra work that + * we need to do to check our repair work. If this is NULL, we'll use + * the ->scrub function pointer, assuming that the regular scrub is + * sufficient. + */ + int (*repair_eval)(struct xfs_scrub *sc); + /* Decide if we even have this piece of metadata. */ bool (*has)(struct xfs_sb *); diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 7ba12e68ea01..9734aca1a0fd 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -722,11 +722,33 @@ DEFINE_EVENT(xrep_rmap_class, name, \ xfs_agblock_t agbno, xfs_extlen_t len, \ uint64_t owner, uint64_t offset, unsigned int flags), \ TP_ARGS(mp, agno, agbno, len, owner, offset, flags)) -DEFINE_REPAIR_RMAP_EVENT(xrep_alloc_extent_fn); DEFINE_REPAIR_RMAP_EVENT(xrep_ialloc_extent_fn); DEFINE_REPAIR_RMAP_EVENT(xrep_rmap_extent_fn); DEFINE_REPAIR_RMAP_EVENT(xrep_bmap_extent_fn); +TRACE_EVENT(xrep_abt_found, + TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, + xfs_agblock_t startblock, xfs_extlen_t blockcount), + TP_ARGS(mp, agno, startblock, blockcount), + TP_STRUCT__entry( + __field(dev_t, dev) + __field(xfs_agnumber_t, agno) + __field(xfs_agblock_t, startblock) + __field(xfs_extlen_t, blockcount) + ), + TP_fast_assign( + __entry->dev = mp->m_super->s_dev; + __entry->agno = agno; + __entry->startblock = startblock; + __entry->blockcount = blockcount; + ), + TP_printk("dev %d:%d agno %u agbno %u len %u", + MAJOR(__entry->dev), MINOR(__entry->dev), + __entry->agno, + __entry->startblock, + __entry->blockcount) +) + TRACE_EVENT(xrep_refcount_extent_fn, TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, struct xfs_refcount_irec *irec), diff --git a/fs/xfs/xfs_extent_busy.c b/fs/xfs/xfs_extent_busy.c index 2183d87be4cf..3ab163e223aa 100644 --- a/fs/xfs/xfs_extent_busy.c +++ b/fs/xfs/xfs_extent_busy.c @@ -657,3 +657,16 @@ xfs_extent_busy_ag_cmp( diff = b1->bno - b2->bno; return diff; } + +/* Are there any busy extents in this AG? */ +bool +xfs_extent_busy_list_empty( + struct xfs_perag *pag) +{ + bool res; + + spin_lock(&pag->pagb_lock); + res = RB_EMPTY_ROOT(&pag->pagb_tree); + spin_unlock(&pag->pagb_lock); + return res; +} diff --git a/fs/xfs/xfs_extent_busy.h b/fs/xfs/xfs_extent_busy.h index 990ab3891971..2f8c73c712c6 100644 --- a/fs/xfs/xfs_extent_busy.h +++ b/fs/xfs/xfs_extent_busy.h @@ -65,4 +65,6 @@ static inline void xfs_extent_busy_sort(struct list_head *list) list_sort(NULL, list, xfs_extent_busy_ag_cmp); } +bool xfs_extent_busy_list_empty(struct xfs_perag *pag); + #endif /* __XFS_EXTENT_BUSY_H__ */ From patchwork Tue Oct 29 23:32:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11218813 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0086F139A for ; Tue, 29 Oct 2019 23:32:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ACDE62083E for ; Tue, 29 Oct 2019 23:32:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="cN5CApsM" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726089AbfJ2XcV (ORCPT ); Tue, 29 Oct 2019 19:32:21 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:49836 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726053AbfJ2XcV (ORCPT ); Tue, 29 Oct 2019 19:32:21 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNSrMf010509 for ; Tue, 29 Oct 2019 23:32:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=RUYByNKtB1xOkccvCLS0uhiQeR8Qo1ytpbQuaitNPuc=; b=cN5CApsMq0XiAOWw2rIZauYVyHMNPtl9SI8bNwICJnUmufovWEC6H/fcl5UdJdwPBYVB e7pQigw7i0SvBDUZQMKHF6P02NxcfXutJSfSuXYyZRAbaUfJK6tPEcbPdaohMkNbnouY hak/zqMQUQb3pV+JbVjgQhnajWnayEKSmMW5dtsDYpzkYQ3dZACpopN9lx1DsWPJ800a cGOxvN3XkYGaxoAlrf2+oN5jYDl3x765hy5zqjFz9yZQB4fTU76hkhe9D4autrrj12dN RKdNU8ZYMFH83tbIi0RCOi0/Reir6qNE6Hd4oF81FFHq2eStqnEABa6dX6dhKc82eqHo WA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 2vxwhfgb0b-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:14 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNS8uw179260 for ; Tue, 29 Oct 2019 23:32:13 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserp3020.oracle.com with ESMTP id 2vxwj54ypk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:13 +0000 Received: from abhmp0004.oracle.com (abhmp0004.oracle.com [141.146.116.10]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x9TNWCBX012228 for ; Tue, 29 Oct 2019 23:32:12 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Oct 2019 16:32:12 -0700 Subject: [PATCH 4/5] xfs: repair inode btrees From: "Darrick J. Wong" To: darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Tue, 29 Oct 2019 16:32:11 -0700 Message-ID: <157239193139.1267304.16473420534496765201.stgit@magnolia> In-Reply-To: <157239190609.1267304.9008396217521176875.stgit@magnolia> References: <157239190609.1267304.9008396217521176875.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Use the rmapbt to find inode chunks, query the chunks to compute hole and free masks, and with that information rebuild the inobt and finobt. Signed-off-by: Darrick J. Wong --- fs/xfs/Makefile | 1 fs/xfs/libxfs/xfs_ialloc.c | 20 + fs/xfs/libxfs/xfs_ialloc.h | 1 fs/xfs/libxfs/xfs_ialloc_btree.c | 2 fs/xfs/libxfs/xfs_ialloc_btree.h | 2 fs/xfs/scrub/common.c | 1 fs/xfs/scrub/ialloc_repair.c | 786 ++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/repair.c | 23 + fs/xfs/scrub/repair.h | 16 + fs/xfs/scrub/scrub.c | 6 fs/xfs/scrub/scrub.h | 1 fs/xfs/scrub/trace.h | 68 ++- 12 files changed, 883 insertions(+), 44 deletions(-) create mode 100644 fs/xfs/scrub/ialloc_repair.c diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index ffcebf428ca9..6ca9755f031e 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile @@ -163,6 +163,7 @@ xfs-y += $(addprefix scrub/, \ alloc_repair.o \ array.o \ bitmap.o \ + ialloc_repair.o \ repair.o \ xfile.o \ ) diff --git a/fs/xfs/libxfs/xfs_ialloc.c b/fs/xfs/libxfs/xfs_ialloc.c index 588d44613094..d4301d2e9f10 100644 --- a/fs/xfs/libxfs/xfs_ialloc.c +++ b/fs/xfs/libxfs/xfs_ialloc.c @@ -95,6 +95,19 @@ xfs_inobt_btrec_to_irec( irec->ir_free = be64_to_cpu(rec->inobt.ir_free); } +uint8_t +xfs_inobt_rec_freecount( + const struct xfs_inobt_rec_incore *irec) +{ + uint64_t realfree; + + if (!xfs_inobt_issparse(irec->ir_holemask)) + realfree = irec->ir_free; + else + realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec); + return hweight64(realfree); +} + /* * Get the data from the pointed-to record. */ @@ -108,7 +121,6 @@ xfs_inobt_get_rec( xfs_agnumber_t agno = cur->bc_private.a.agno; union xfs_btree_rec *rec; int error; - uint64_t realfree; error = xfs_btree_get_rec(cur, &rec, stat); if (error || *stat == 0) @@ -125,11 +137,7 @@ xfs_inobt_get_rec( goto out_bad_rec; /* if there are no holes, return the first available offset */ - if (!xfs_inobt_issparse(irec->ir_holemask)) - realfree = irec->ir_free; - else - realfree = irec->ir_free & xfs_inobt_irec_to_allocmask(irec); - if (hweight64(realfree) != irec->ir_freecount) + if (xfs_inobt_rec_freecount(irec) != irec->ir_freecount) goto out_bad_rec; return 0; diff --git a/fs/xfs/libxfs/xfs_ialloc.h b/fs/xfs/libxfs/xfs_ialloc.h index 323592d563d5..a6f259ae94ed 100644 --- a/fs/xfs/libxfs/xfs_ialloc.h +++ b/fs/xfs/libxfs/xfs_ialloc.h @@ -125,6 +125,7 @@ int xfs_inobt_lookup(struct xfs_btree_cur *cur, xfs_agino_t ino, */ int xfs_inobt_get_rec(struct xfs_btree_cur *cur, xfs_inobt_rec_incore_t *rec, int *stat); +uint8_t xfs_inobt_rec_freecount(const struct xfs_inobt_rec_incore *irec); /* * Inode chunk initialisation routine diff --git a/fs/xfs/libxfs/xfs_ialloc_btree.c b/fs/xfs/libxfs/xfs_ialloc_btree.c index 15d8ec692a6e..67df4c9f809c 100644 --- a/fs/xfs/libxfs/xfs_ialloc_btree.c +++ b/fs/xfs/libxfs/xfs_ialloc_btree.c @@ -527,7 +527,7 @@ xfs_inobt_maxrecs( */ uint64_t xfs_inobt_irec_to_allocmask( - struct xfs_inobt_rec_incore *rec) + const struct xfs_inobt_rec_incore *rec) { uint64_t bitmap = 0; uint64_t inodespbit; diff --git a/fs/xfs/libxfs/xfs_ialloc_btree.h b/fs/xfs/libxfs/xfs_ialloc_btree.h index 9265b3e08c69..9d14e9cfee21 100644 --- a/fs/xfs/libxfs/xfs_ialloc_btree.h +++ b/fs/xfs/libxfs/xfs_ialloc_btree.h @@ -54,7 +54,7 @@ struct xfs_btree_cur *xfs_inobt_stage_cursor(struct xfs_mount *mp, extern int xfs_inobt_maxrecs(struct xfs_mount *, int, int); /* ir_holemask to inode allocation bitmap conversion */ -uint64_t xfs_inobt_irec_to_allocmask(struct xfs_inobt_rec_incore *); +uint64_t xfs_inobt_irec_to_allocmask(const struct xfs_inobt_rec_incore *i); #if defined(DEBUG) || defined(XFS_WARN) int xfs_inobt_rec_check_count(struct xfs_mount *, diff --git a/fs/xfs/scrub/common.c b/fs/xfs/scrub/common.c index 4a49a9099477..abe88fa756aa 100644 --- a/fs/xfs/scrub/common.c +++ b/fs/xfs/scrub/common.c @@ -527,6 +527,7 @@ xchk_ag_free( struct xchk_ag *sa) { xchk_ag_btcur_free(sa); + xrep_reset_perag_resv(sc); if (sa->agfl_bp) { xfs_trans_brelse(sc->tp, sa->agfl_bp); sa->agfl_bp = NULL; diff --git a/fs/xfs/scrub/ialloc_repair.c b/fs/xfs/scrub/ialloc_repair.c new file mode 100644 index 000000000000..d40f87f7346e --- /dev/null +++ b/fs/xfs/scrub/ialloc_repair.c @@ -0,0 +1,786 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "xfs.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_defer.h" +#include "xfs_btree.h" +#include "xfs_bit.h" +#include "xfs_log_format.h" +#include "xfs_trans.h" +#include "xfs_sb.h" +#include "xfs_inode.h" +#include "xfs_alloc.h" +#include "xfs_ialloc.h" +#include "xfs_ialloc_btree.h" +#include "xfs_icache.h" +#include "xfs_rmap.h" +#include "xfs_rmap_btree.h" +#include "xfs_log.h" +#include "xfs_trans_priv.h" +#include "xfs_error.h" +#include "xfs_health.h" +#include "scrub/xfs_scrub.h" +#include "scrub/scrub.h" +#include "scrub/common.h" +#include "scrub/btree.h" +#include "scrub/trace.h" +#include "scrub/repair.h" +#include "scrub/bitmap.h" +#include "scrub/array.h" + +/* + * Inode Btree Repair + * ================== + * + * A quick refresher of inode btrees on a v5 filesystem: + * + * - Inode records are read into memory in units of 'inode clusters'. However + * many inodes fit in a cluster buffer is the smallest number of inodes that + * can be allocated or freed. Clusters are never smaller than one fs block + * though they can span multiple blocks. The size (in fs blocks) is + * computed with xfs_icluster_size_fsb(). The fs block alignment of a + * cluster is computed with xfs_ialloc_cluster_alignment(). + * + * - Each inode btree record can describe a single 'inode chunk'. The chunk + * size is defined to be 64 inodes. If sparse inodes are enabled, every + * inobt record must be aligned to the chunk size; if not, every record must + * be aligned to the start of a cluster. It is possible to construct an XFS + * geometry where one inobt record maps to multiple inode clusters; it is + * also possible to construct a geometry where multiple inobt records map to + * different parts of one inode cluster. + * + * - If sparse inodes are not enabled, the smallest unit of allocation for + * inode records is enough to contain one inode chunk's worth of inodes. + * + * - If sparse inodes are enabled, the holemask field will be active. Each + * bit of the holemask represents 4 potential inodes; if set, the + * corresponding space does *not* contain inodes and must be left alone. + * Clusters cannot be smaller than 4 inodes. The smallest unit of allocation + * of inode records is one inode cluster. + * + * So what's the rebuild algorithm? + * + * Iterate the reverse mapping records looking for OWN_INODES and OWN_INOBT + * records. The OWN_INOBT records are the old inode btree blocks and will be + * cleared out after we've rebuilt the tree. Each possible inode cluster + * within an OWN_INODES record will be read in; for each possible inobt record + * associated with that cluster, compute the freemask calculated from the + * i_mode data in the inode chunk. For sparse inodes the holemask will be + * calculated by creating the properly aligned inobt record and punching out + * any chunk that's missing. Inode allocations and frees grab the AGI first, + * so repair protects itself from concurrent access by locking the AGI. + * + * Once we've reconstructed all the inode records, we can create new inode + * btree roots and reload the btrees. We rebuild both inode trees at the same + * time because they have the same rmap owner and it would be more complex to + * figure out if the other tree isn't in need of a rebuild and which OWN_INOBT + * blocks it owns. We have all the data we need to build both, so dump + * everything and start over. + * + * We use the prefix 'xrep_ibt' because we rebuild both inode btrees at once. + */ + +struct xrep_ibt { + /* Record under construction. */ + struct xfs_inobt_rec_incore rie; + + /* new inobt information */ + struct xrep_newbt new_inobt_info; + struct xfs_btree_bload ino_bload; + + /* new finobt information */ + struct xrep_newbt new_finobt_info; + struct xfs_btree_bload fino_bload; + + /* Old inode btree blocks we found in the rmap. */ + struct xbitmap old_iallocbt_blocks; + + /* Reconstructed inode records. */ + struct xfbma *inode_records; + + struct xfs_scrub *sc; + + /* Number of inodes assigned disk space. */ + unsigned int icount; + + /* Number of inodes in use. */ + unsigned int iused; + + /* Number of finobt records needed. */ + unsigned int finobt_recs; + + /* get_data()'s position in the inode record array. */ + uint64_t iter; +}; + +/* + * Is this inode in use? If the inode is in memory we can tell from i_mode, + * otherwise we have to check di_mode in the on-disk buffer. We only care + * that the high (i.e. non-permission) bits of _mode are zero. This should be + * safe because repair keeps all AG headers locked until the end, and process + * trying to perform an inode allocation/free must lock the AGI. + * + * @cluster_ag_base is the inode offset of the cluster within the AG. + * @cluster_bp is the cluster buffer. + * @cluster_index is the inode offset within the inode cluster. + */ +STATIC int +xrep_ibt_check_ifree( + struct xrep_ibt *ri, + xfs_agino_t cluster_ag_base, + struct xfs_buf *cluster_bp, + unsigned int cluster_index, + bool *inuse) +{ + struct xfs_scrub *sc = ri->sc; + struct xfs_mount *mp = sc->mp; + struct xfs_dinode *dip; + xfs_ino_t fsino; + xfs_agnumber_t agno = ri->sc->sa.agno; + unsigned int cluster_buf_base; + unsigned int offset; + int error; + + fsino = XFS_AGINO_TO_INO(mp, agno, cluster_ag_base + cluster_index); + + /* Inode uncached or half assembled, read disk buffer */ + cluster_buf_base = XFS_INO_TO_OFFSET(mp, cluster_ag_base); + offset = (cluster_buf_base + cluster_index) * mp->m_sb.sb_inodesize; + if (offset >= BBTOB(cluster_bp->b_length)) + return -EFSCORRUPTED; + dip = xfs_buf_offset(cluster_bp, offset); + if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC) + return -EFSCORRUPTED; + + if (dip->di_version >= 3 && be64_to_cpu(dip->di_ino) != fsino) + return -EFSCORRUPTED; + + /* Will the in-core inode tell us if it's in use? */ + error = xfs_icache_inode_is_allocated(mp, sc->tp, fsino, inuse); + if (!error) + return 0; + + *inuse = dip->di_mode != 0; + return 0; +} + +/* Stash the accumulated inobt record for rebuilding. */ +STATIC int +xrep_ibt_stash_record( + struct xrep_ibt *ri) +{ + int error; + + ri->rie.ir_freecount = xfs_inobt_rec_freecount(&ri->rie); + if (ri->rie.ir_freecount > 0) + ri->finobt_recs++; + + trace_xrep_ibt_found(ri->sc->mp, ri->sc->sa.agno, ri->rie.ir_startino, + ri->rie.ir_holemask, ri->rie.ir_count, + ri->rie.ir_freecount, ri->rie.ir_free); + + error = xfbma_append(ri->inode_records, &ri->rie); + if (error) + return error; + ri->rie.ir_startino = NULLAGINO; + return 0; +} + +/* + * Given an extent of inodes and an inode cluster buffer, calculate the + * location of the corresponding inobt record (creating it if necessary), + * then update the parts of the holemask and freemask of that record that + * correspond to the inode extent we were given. + * + * @cluster_ir_startino is the AG inode number of an inobt record that we're + * proposing to create for this inode cluster. If sparse inodes are enabled, + * we must round down to a chunk boundary to find the actual sparse record. + * @cluster_bp is the buffer of the inode cluster. + * @nr_inodes is the number of inodes to check from the cluster. + */ +STATIC int +xrep_ibt_cluster_record( + struct xrep_ibt *ri, + xfs_agino_t cluster_ir_startino, + struct xfs_buf *cluster_bp, + unsigned int nr_inodes) +{ + struct xfs_scrub *sc = ri->sc; + struct xfs_mount *mp = sc->mp; + xfs_agino_t ir_startino; + unsigned int cluster_base; + unsigned int cluster_index; + bool inuse; + int error = 0; + + ir_startino = cluster_ir_startino; + if (xfs_sb_version_hassparseinodes(&mp->m_sb)) + ir_startino = rounddown(ir_startino, XFS_INODES_PER_CHUNK); + cluster_base = cluster_ir_startino - ir_startino; + + /* + * If the accumulated inobt record doesn't map this cluster, add it to + * the list and reset it. + */ + if (ri->rie.ir_startino != NULLAGINO && + ri->rie.ir_startino + XFS_INODES_PER_CHUNK <= ir_startino) { + error = xrep_ibt_stash_record(ri); + if (error) + return error; + } + + if (ri->rie.ir_startino == NULLAGINO) { + ri->rie.ir_startino = ir_startino; + ri->rie.ir_free = XFS_INOBT_ALL_FREE; + ri->rie.ir_holemask = 0xFFFF; + ri->rie.ir_count = 0; + } + + /* Record the whole cluster. */ + ri->icount += nr_inodes; + ri->rie.ir_count += nr_inodes; + ri->rie.ir_holemask &= ~xfs_inobt_maskn( + cluster_base / XFS_INODES_PER_HOLEMASK_BIT, + nr_inodes / XFS_INODES_PER_HOLEMASK_BIT); + + /* Which inodes within this cluster are free? */ + for (cluster_index = 0; cluster_index < nr_inodes; cluster_index++) { + error = xrep_ibt_check_ifree(ri, cluster_ir_startino, + cluster_bp, cluster_index, &inuse); + if (error) + return error; + if (!inuse) + continue; + ri->iused++; + ri->rie.ir_free &= ~XFS_INOBT_MASK(cluster_base + + cluster_index); + } + return 0; +} + +/* + * For each inode cluster covering the physical extent recorded by the rmapbt, + * we must calculate the properly aligned startino of that cluster, then + * iterate each cluster to fill in used and filled masks appropriately. We + * then use the (startino, used, filled) information to construct the + * appropriate inode records. + */ +STATIC int +xrep_ibt_process_cluster( + struct xrep_ibt *ri, + xfs_agblock_t cluster_bno) +{ + struct xfs_imap imap; + struct xfs_dinode *dip; + struct xfs_buf *cluster_bp; + struct xfs_scrub *sc = ri->sc; + struct xfs_mount *mp = sc->mp; + struct xfs_ino_geometry *igeo = M_IGEO(mp); + xfs_agino_t cluster_ag_base; + xfs_agino_t irec_index; + unsigned int nr_inodes; + int error; + + nr_inodes = min_t(unsigned int, igeo->inodes_per_cluster, + XFS_INODES_PER_CHUNK); + + /* + * Grab the inode cluster buffer. This is safe to do with a broken + * inobt because imap_to_bp directly maps the buffer without touching + * either inode btree. + */ + imap.im_blkno = XFS_AGB_TO_DADDR(mp, sc->sa.agno, cluster_bno); + imap.im_len = XFS_FSB_TO_BB(mp, igeo->blocks_per_cluster); + imap.im_boffset = 0; + error = xfs_imap_to_bp(mp, sc->tp, &imap, &dip, &cluster_bp, 0, 0); + if (error) + return error; + + /* + * Record the contents of each possible inobt record mapping this + * cluster. + */ + cluster_ag_base = XFS_AGB_TO_AGINO(mp, cluster_bno); + for (irec_index = 0; + irec_index < igeo->inodes_per_cluster; + irec_index += XFS_INODES_PER_CHUNK) { + error = xrep_ibt_cluster_record(ri, + cluster_ag_base + irec_index, cluster_bp, + nr_inodes); + if (error) + break; + + } + + xfs_trans_brelse(sc->tp, cluster_bp); + return error; +} + +/* Record extents that belong to inode btrees. */ +STATIC int +xrep_ibt_walk_rmap( + struct xfs_btree_cur *cur, + struct xfs_rmap_irec *rec, + void *priv) +{ + struct xrep_ibt *ri = priv; + struct xfs_mount *mp = cur->bc_mp; + struct xfs_ino_geometry *igeo = M_IGEO(mp); + xfs_fsblock_t fsbno; + xfs_agblock_t agbno = rec->rm_startblock; + xfs_agblock_t cluster_base; + int error = 0; + + if (xchk_should_terminate(ri->sc, &error)) + return error; + + /* Fragment of the old btrees; dispose of them later. */ + if (rec->rm_owner == XFS_RMAP_OWN_INOBT) { + fsbno = XFS_AGB_TO_FSB(mp, ri->sc->sa.agno, agbno); + return xbitmap_set(&ri->old_iallocbt_blocks, fsbno, + rec->rm_blockcount); + } + + /* Skip extents which are not owned by this inode and fork. */ + if (rec->rm_owner != XFS_RMAP_OWN_INODES) + return 0; + + /* The entire record must align to the inode cluster size. */ + if (agbno & (igeo->blocks_per_cluster - 1) || + (agbno + rec->rm_blockcount) & (igeo->blocks_per_cluster - 1)) + return -EFSCORRUPTED; + + /* + * The entire record must also adhere to the inode cluster alignment + * size if sparse inodes are not enabled. + */ + if (!xfs_sb_version_hassparseinodes(&mp->m_sb) && + (agbno & (igeo->cluster_align - 1) || + (agbno + rec->rm_blockcount) & (igeo->cluster_align - 1))) + return -ENAVAIL; + + /* + * On a sparse inode fs, this cluster could be part of a sparse chunk. + * Sparse clusters must be aligned to sparse chunk alignment. + */ + if (xfs_sb_version_hassparseinodes(&mp->m_sb) && + (agbno & (mp->m_sb.sb_spino_align - 1) || + (agbno + rec->rm_blockcount) & (mp->m_sb.sb_spino_align - 1))) + return -EREMOTEIO; + + trace_xrep_ibt_walk_rmap(mp, ri->sc->sa.agno, rec->rm_startblock, + rec->rm_blockcount, rec->rm_owner, rec->rm_offset, + rec->rm_flags); + + /* + * Record the free/hole masks for each inode cluster that could be + * mapped by this rmap record. + */ + for (cluster_base = 0; + cluster_base < rec->rm_blockcount; + cluster_base += igeo->blocks_per_cluster) { + error = xrep_ibt_process_cluster(ri, agbno + cluster_base); + if (error) + return error; + } + + return 0; +} + +/* Compare two ialloc extents. */ +static int +xfs_inobt_rec_incore_cmp( + const void *a, + const void *b) +{ + const struct xfs_inobt_rec_incore *ap = a; + const struct xfs_inobt_rec_incore *bp = b; + + if (ap->ir_startino > bp->ir_startino) + return 1; + else if (ap->ir_startino < bp->ir_startino) + return -1; + return 0; +} + +/* + * Iterate all reverse mappings to find the inodes (OWN_INODES) and the inode + * btrees (OWN_INOBT). Figure out if we have enough free space to reconstruct + * the inode btrees. The caller must clean up the lists if anything goes + * wrong. + */ +STATIC int +xrep_ibt_find_inodes( + struct xrep_ibt *ri) +{ + struct xfs_scrub *sc = ri->sc; + struct xfs_mount *mp = sc->mp; + struct xfs_btree_cur *cur; + int error; + + ri->rie.ir_startino = NULLAGINO; + + /* Collect all reverse mappings for inode blocks. */ + cur = xfs_rmapbt_init_cursor(mp, sc->tp, sc->sa.agf_bp, sc->sa.agno); + error = xfs_rmap_query_all(cur, xrep_ibt_walk_rmap, ri); + xfs_btree_del_cursor(cur, error); + if (error) + return error; + + /* If we have a record ready to go, add it to the array. */ + if (ri->rie.ir_startino != NULLAGINO) { + error = xrep_ibt_stash_record(ri); + if (error) + return error; + } + + return 0; +} + +/* Update the AGI counters. */ +STATIC int +xrep_ibt_reset_counters( + struct xrep_ibt *ri) +{ + struct xfs_scrub *sc = ri->sc; + struct xfs_agi *agi; + struct xfs_perag *pag = sc->sa.pag; + struct xfs_buf *bp; + unsigned int freecount; + + agi = XFS_BUF_TO_AGI(sc->sa.agi_bp); + freecount = ri->icount - ri->iused; + + /* Trigger inode count recalculation */ + xfs_force_summary_recalc(sc->mp); + + /* + * Mark the pagi information stale and use the accessor function to + * forcibly reload it from the values we just logged. We still own + * the AGI bp so we can throw away bp. + */ + ASSERT(pag->pagi_init); + pag->pagi_init = 0; + + agi->agi_count = cpu_to_be32(ri->icount); + agi->agi_freecount = cpu_to_be32(freecount); + xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, + XFS_AGI_COUNT | XFS_AGI_FREECOUNT); + + return xfs_ialloc_read_agi(sc->mp, sc->tp, sc->sa.agno, &bp); +} + +/* Do we even want this record? */ +static inline bool +xrep_ibt_rec_wanted( + struct xrep_ibt *ri, + struct xfs_btree_cur *cur, + struct xfs_inobt_rec_incore *irec) +{ + /* Ignore null records. */ + if (xfbma_is_null(ri->inode_records, irec)) + return false; + + /* finobt only wants inode records with at least 1 free inode. */ + if (cur->bc_btnum == XFS_BTNUM_FINO && + xfs_inobt_rec_freecount(irec) == 0) + return false; + + return true; +} + +/* Retrieve inobt data for bulk load. */ +STATIC int +xrep_ibt_get_data( + struct xfs_btree_cur *cur, + void *priv) +{ + struct xfs_inobt_rec_incore *irec = &cur->bc_rec.i; + struct xrep_ibt *ri = priv; + int error; + + do { + error = xfbma_get(ri->inode_records, ri->iter++, irec); + } while (error == 0 && !xrep_ibt_rec_wanted(ri, cur, irec)); + + return error; +} + +/* Feed one of the new inobt blocks to the bulk loader. */ +STATIC int +xrep_ibt_alloc_block( + struct xfs_btree_cur *cur, + union xfs_btree_ptr *ptr, + void *priv) +{ + struct xrep_ibt *ri = priv; + + if (cur->bc_btnum == XFS_BTNUM_INO) + return xrep_newbt_claim_block(cur, &ri->new_inobt_info, ptr); + return xrep_newbt_claim_block(cur, &ri->new_finobt_info, ptr); +} + +static void +xrep_ibt_init_bload( + struct xrep_ibt *ri, + struct xfs_btree_bload *bload) +{ + bload->get_data = xrep_ibt_get_data; + bload->alloc_block = xrep_ibt_alloc_block; + + xrep_bload_estimate_slack(ri->sc, bload); +} + +/* Build new inode btrees and dispose of the old one. */ +STATIC int +xrep_ibt_build_new_trees( + struct xrep_ibt *ri) +{ + struct xfs_scrub *sc = ri->sc; + struct xfs_btree_cur *ino_cur; + struct xfs_btree_cur *fino_cur = NULL; + bool need_finobt; + int error; + + need_finobt = xfs_sb_version_hasfinobt(&sc->mp->m_sb); + xrep_ibt_init_bload(ri, &ri->ino_bload); + xrep_ibt_init_bload(ri, &ri->fino_bload); + + /* + * Sort the inode extents by startino or else the btree records will + * be in the wrong order. + */ + error = xfbma_sort(ri->inode_records, xfs_inobt_rec_incore_cmp); + if (error) + return error; + + /* + * Create new btrees for staging all the inobt records we collected + * earlier. These btrees will not be rooted in the AGI until we've + * successfully reloaded the tree. + */ + + /* Set up inobt staging cursor. */ + xrep_newbt_init_ag(&ri->new_inobt_info, sc, &XFS_RMAP_OINFO_INOBT, + XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, + XFS_IBT_BLOCK(sc->mp)), + XFS_AG_RESV_NONE); + ino_cur = xfs_inobt_stage_cursor(sc->mp, sc->tp, + &ri->new_inobt_info.afake, sc->sa.agno, XFS_BTNUM_INO); + error = xfs_btree_bload_compute_geometry(ino_cur, &ri->ino_bload, + xfbma_length(ri->inode_records)); + xfs_btree_del_cursor(ino_cur, error); + if (error) + goto err_inobt; + + /* Set up finobt staging cursor. */ + if (need_finobt) { + enum xfs_ag_resv_type resv = XFS_AG_RESV_METADATA; + + if (sc->mp->m_finobt_nores) + resv = XFS_AG_RESV_NONE; + + xrep_newbt_init_ag(&ri->new_finobt_info, sc, + &XFS_RMAP_OINFO_INOBT, + XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, + XFS_FIBT_BLOCK(sc->mp)), + resv); + fino_cur = xfs_inobt_stage_cursor(sc->mp, sc->tp, + &ri->new_finobt_info.afake, sc->sa.agno, + XFS_BTNUM_FINO); + error = xfs_btree_bload_compute_geometry(fino_cur, + &ri->fino_bload, ri->finobt_recs); + xfs_btree_del_cursor(fino_cur, error); + if (error) + goto err_finobt; + } + + /* Reserve all the space we need to build the new btrees. */ + error = xrep_newbt_alloc_blocks(&ri->new_inobt_info, + ri->ino_bload.nr_blocks); + if (error) + goto err_finobt; + + if (need_finobt) { + error = xrep_newbt_alloc_blocks(&ri->new_finobt_info, + ri->fino_bload.nr_blocks); + if (error) + goto err_finobt; + } + + /* Add all inobt records. */ + ri->iter = 0; + ino_cur = xfs_inobt_stage_cursor(sc->mp, sc->tp, + &ri->new_inobt_info.afake, sc->sa.agno, XFS_BTNUM_INO); + error = xfs_btree_bload(ino_cur, &ri->ino_bload, ri); + if (error) + goto err_inocur; + + /* Add all finobt records. */ + if (need_finobt) { + ri->iter = 0; + fino_cur = xfs_inobt_stage_cursor(sc->mp, sc->tp, + &ri->new_finobt_info.afake, sc->sa.agno, + XFS_BTNUM_FINO); + error = xfs_btree_bload(fino_cur, &ri->fino_bload, ri); + if (error) + goto err_finocur; + } + + /* + * Re-read the AGI so that the buffer type is set properly. Since we + * built a new tree without dirtying the AGI, the buffer item may have + * fallen off the buffer. This ought to succeed since the AGI is held + * across transaction rolls. + */ + error = xfs_read_agi(sc->mp, sc->tp, sc->sa.agno, &sc->sa.agi_bp); + if (error) + goto err_finocur; + + /* Install new btree roots. */ + xfs_inobt_commit_staged_btree(ino_cur, sc->sa.agi_bp); + xfs_btree_del_cursor(ino_cur, 0); + + if (fino_cur) { + xfs_inobt_commit_staged_btree(fino_cur, sc->sa.agi_bp); + xfs_btree_del_cursor(fino_cur, 0); + } + + /* Reset the AGI counters now that we've changed the inode roots. */ + error = xrep_ibt_reset_counters(ri); + if (error) + goto err_finobt; + + /* Free unused blocks and bitmap. */ + if (need_finobt) + xrep_newbt_destroy(&ri->new_finobt_info, error); + xrep_newbt_destroy(&ri->new_inobt_info, error); + + return xrep_roll_ag_trans(sc); + +err_finocur: + if (need_finobt) + xfs_btree_del_cursor(fino_cur, error); +err_inocur: + xfs_btree_del_cursor(ino_cur, error); +err_finobt: + if (need_finobt) + xrep_newbt_destroy(&ri->new_finobt_info, error); +err_inobt: + xrep_newbt_destroy(&ri->new_inobt_info, error); + return error; +} + +/* + * Now that we've logged the roots of the new btrees, invalidate all of the + * old blocks and free them. + */ +STATIC int +xrep_ibt_remove_old_trees( + struct xrep_ibt *ri) +{ + struct xfs_scrub *sc = ri->sc; + int error; + + /* Free the old inode btree blocks if they're not in use. */ + error = xrep_reap_extents(sc, &ri->old_iallocbt_blocks, + &XFS_RMAP_OINFO_INOBT, XFS_AG_RESV_NONE); + if (error) + return error; + + /* + * If the finobt is enabled and has a per-AG reservation, make sure we + * reinitialize the per-AG reservations. + */ + if (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->mp->m_finobt_nores) + sc->flags |= XREP_RESET_PERAG_RESV; + + return 0; +} + +/* Repair both inode btrees. */ +int +xrep_iallocbt( + struct xfs_scrub *sc) +{ + struct xrep_ibt *ri; + struct xfs_mount *mp = sc->mp; + int error = 0; + + /* We require the rmapbt to rebuild anything. */ + if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) + return -EOPNOTSUPP; + + ri = kmem_zalloc(sizeof(struct xrep_ibt), KM_NOFS | KM_MAYFAIL); + if (!ri) + return -ENOMEM; + ri->sc = sc; + + xchk_perag_get(sc->mp, &sc->sa); + + /* We rebuild both inode btrees. */ + sc->sick_mask = XFS_SICK_AG_INOBT | XFS_SICK_AG_FINOBT; + + /* Set up some storage */ + ri->inode_records = xfbma_init(sizeof(struct xfs_inobt_rec_incore)); + if (IS_ERR(ri->inode_records)) { + error = PTR_ERR(ri->inode_records); + goto out_ri; + } + + /* Collect the inode data and find the old btree blocks. */ + xbitmap_init(&ri->old_iallocbt_blocks); + error = xrep_ibt_find_inodes(ri); + if (error) + goto out_bitmap; + + /* Rebuild the inode indexes. */ + error = xrep_ibt_build_new_trees(ri); + if (error) + goto out_bitmap; + + /* Kill the old tree. */ + error = xrep_ibt_remove_old_trees(ri); + +out_bitmap: + xbitmap_destroy(&ri->old_iallocbt_blocks); + xfbma_destroy(ri->inode_records); +out_ri: + kmem_free(ri); + return error; +} + +/* Make sure both btrees are ok after we've rebuilt them. */ +int +xrep_revalidate_iallocbt( + struct xfs_scrub *sc) +{ + __u32 old_type = sc->sm->sm_type; + int error; + + /* + * We must update sm_type temporarily so that the tree-to-tree cross + * reference checks will work in the correct direction, and also so + * that tracing will report correctly if there are more errors. + */ + sc->sm->sm_type = XFS_SCRUB_TYPE_INOBT; + error = xchk_inobt(sc); + if (error) + goto out; + + if (xfs_sb_version_hasfinobt(&sc->mp->m_sb)) { + sc->sm->sm_type = XFS_SCRUB_TYPE_FINOBT; + error = xchk_finobt(sc); + } + +out: + sc->sm->sm_type = old_type; + return error; +} diff --git a/fs/xfs/scrub/repair.c b/fs/xfs/scrub/repair.c index e66596d0f21a..a2ec37ad8e3a 100644 --- a/fs/xfs/scrub/repair.c +++ b/fs/xfs/scrub/repair.c @@ -1329,3 +1329,26 @@ xrep_ino_dqattach( return error; } + +/* Reinitialize the per-AG block reservation for the AG we just fixed. */ +int +xrep_reset_perag_resv( + struct xfs_scrub *sc) +{ + int error; + + if (!(sc->flags & XREP_RESET_PERAG_RESV)) + return 0; + + ASSERT(sc->sa.pag != NULL); + ASSERT(sc->ops->type == ST_PERAG); + ASSERT(sc->tp); + + sc->flags &= ~XREP_RESET_PERAG_RESV; + error = xfs_ag_resv_free(sc->sa.pag); + if (error) + goto out; + error = xfs_ag_resv_init(sc->sa.pag, sc->tp); +out: + return error; +} diff --git a/fs/xfs/scrub/repair.h b/fs/xfs/scrub/repair.h index 725e6d69f003..8b320e905e00 100644 --- a/fs/xfs/scrub/repair.h +++ b/fs/xfs/scrub/repair.h @@ -54,10 +54,12 @@ int xrep_find_ag_btree_roots(struct xfs_scrub *sc, struct xfs_buf *agf_bp, struct xrep_find_ag_btree *btree_info, struct xfs_buf *agfl_bp); void xrep_force_quotacheck(struct xfs_scrub *sc, uint dqtype); int xrep_ino_dqattach(struct xfs_scrub *sc); +int xrep_reset_perag_resv(struct xfs_scrub *sc); /* Metadata revalidators */ int xrep_revalidate_allocbt(struct xfs_scrub *sc); +int xrep_revalidate_iallocbt(struct xfs_scrub *sc); /* Metadata repairers */ @@ -67,6 +69,7 @@ int xrep_agf(struct xfs_scrub *sc); int xrep_agfl(struct xfs_scrub *sc); int xrep_agi(struct xfs_scrub *sc); int xrep_allocbt(struct xfs_scrub *sc); +int xrep_iallocbt(struct xfs_scrub *sc); struct xrep_newbt_resv { /* Link to list of extents that we've reserved. */ @@ -143,7 +146,19 @@ xrep_calc_ag_resblks( return 0; } +static inline int +xrep_reset_perag_resv( + struct xfs_scrub *sc) +{ + if (!(sc->flags & XREP_RESET_PERAG_RESV)) + return 0; + + ASSERT(0); + return -EOPNOTSUPP; +} + #define xrep_revalidate_allocbt (NULL) +#define xrep_revalidate_iallocbt (NULL) #define xrep_probe xrep_notsupported #define xrep_superblock xrep_notsupported @@ -151,6 +166,7 @@ xrep_calc_ag_resblks( #define xrep_agfl xrep_notsupported #define xrep_agi xrep_notsupported #define xrep_allocbt xrep_notsupported +#define xrep_iallocbt xrep_notsupported #endif /* CONFIG_XFS_ONLINE_REPAIR */ diff --git a/fs/xfs/scrub/scrub.c b/fs/xfs/scrub/scrub.c index b42ac8ecdb49..6011823d0d40 100644 --- a/fs/xfs/scrub/scrub.c +++ b/fs/xfs/scrub/scrub.c @@ -231,14 +231,16 @@ static const struct xchk_meta_ops meta_scrub_ops[] = { .type = ST_PERAG, .setup = xchk_setup_ag_iallocbt, .scrub = xchk_inobt, - .repair = xrep_notsupported, + .repair = xrep_iallocbt, + .repair_eval = xrep_revalidate_iallocbt, }, [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */ .type = ST_PERAG, .setup = xchk_setup_ag_iallocbt, .scrub = xchk_finobt, .has = xfs_sb_version_hasfinobt, - .repair = xrep_notsupported, + .repair = xrep_iallocbt, + .repair_eval = xrep_revalidate_iallocbt, }, [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */ .type = ST_PERAG, diff --git a/fs/xfs/scrub/scrub.h b/fs/xfs/scrub/scrub.h index 94a30637a127..16ed1d3e1404 100644 --- a/fs/xfs/scrub/scrub.h +++ b/fs/xfs/scrub/scrub.h @@ -89,6 +89,7 @@ struct xfs_scrub { #define XCHK_TRY_HARDER (1 << 0) /* can't get resources, try again */ #define XCHK_HAS_QUOTAOFFLOCK (1 << 1) /* we hold the quotaoff lock */ #define XCHK_REAPING_DISABLED (1 << 2) /* background block reaping paused */ +#define XREP_RESET_PERAG_RESV (1 << 30) /* must reset AG space reservation */ #define XREP_ALREADY_FIXED (1 << 31) /* checking our repair work */ /* Metadata scrubbers */ diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 9734aca1a0fd..9bf75c97fdd1 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -722,7 +722,7 @@ DEFINE_EVENT(xrep_rmap_class, name, \ xfs_agblock_t agbno, xfs_extlen_t len, \ uint64_t owner, uint64_t offset, unsigned int flags), \ TP_ARGS(mp, agno, agbno, len, owner, offset, flags)) -DEFINE_REPAIR_RMAP_EVENT(xrep_ialloc_extent_fn); +DEFINE_REPAIR_RMAP_EVENT(xrep_ibt_walk_rmap); DEFINE_REPAIR_RMAP_EVENT(xrep_rmap_extent_fn); DEFINE_REPAIR_RMAP_EVENT(xrep_bmap_extent_fn); @@ -749,6 +749,39 @@ TRACE_EVENT(xrep_abt_found, __entry->blockcount) ) +TRACE_EVENT(xrep_ibt_found, + TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, + xfs_agino_t startino, uint16_t holemask, uint8_t count, + uint8_t freecount, uint64_t freemask), + TP_ARGS(mp, agno, startino, holemask, count, freecount, freemask), + TP_STRUCT__entry( + __field(dev_t, dev) + __field(xfs_agnumber_t, agno) + __field(xfs_agino_t, startino) + __field(uint16_t, holemask) + __field(uint8_t, count) + __field(uint8_t, freecount) + __field(uint64_t, freemask) + ), + TP_fast_assign( + __entry->dev = mp->m_super->s_dev; + __entry->agno = agno; + __entry->startino = startino; + __entry->holemask = holemask; + __entry->count = count; + __entry->freecount = freecount; + __entry->freemask = freemask; + ), + TP_printk("dev %d:%d agno %d startino %u holemask 0x%x count %u freecount %u freemask 0x%llx", + MAJOR(__entry->dev), MINOR(__entry->dev), + __entry->agno, + __entry->startino, + __entry->holemask, + __entry->count, + __entry->freecount, + __entry->freemask) +) + TRACE_EVENT(xrep_refcount_extent_fn, TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, struct xfs_refcount_irec *irec), @@ -893,39 +926,6 @@ TRACE_EVENT(xrep_reset_counters, MAJOR(__entry->dev), MINOR(__entry->dev)) ) -TRACE_EVENT(xrep_ialloc_insert, - TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, - xfs_agino_t startino, uint16_t holemask, uint8_t count, - uint8_t freecount, uint64_t freemask), - TP_ARGS(mp, agno, startino, holemask, count, freecount, freemask), - TP_STRUCT__entry( - __field(dev_t, dev) - __field(xfs_agnumber_t, agno) - __field(xfs_agino_t, startino) - __field(uint16_t, holemask) - __field(uint8_t, count) - __field(uint8_t, freecount) - __field(uint64_t, freemask) - ), - TP_fast_assign( - __entry->dev = mp->m_super->s_dev; - __entry->agno = agno; - __entry->startino = startino; - __entry->holemask = holemask; - __entry->count = count; - __entry->freecount = freecount; - __entry->freemask = freemask; - ), - TP_printk("dev %d:%d agno %d startino %u holemask 0x%x count %u freecount %u freemask 0x%llx", - MAJOR(__entry->dev), MINOR(__entry->dev), - __entry->agno, - __entry->startino, - __entry->holemask, - __entry->count, - __entry->freecount, - __entry->freemask) -) - DECLARE_EVENT_CLASS(xrep_newbt_extent_class, TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, xfs_agblock_t agbno, xfs_extlen_t len, From patchwork Tue Oct 29 23:32:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11218815 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 647D6139A for ; Tue, 29 Oct 2019 23:32:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2ED1420679 for ; Tue, 29 Oct 2019 23:32:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="LcE40zEP" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726053AbfJ2XcX (ORCPT ); Tue, 29 Oct 2019 19:32:23 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:49918 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726084AbfJ2XcX (ORCPT ); Tue, 29 Oct 2019 19:32:23 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNSrMg010509 for ; Tue, 29 Oct 2019 23:32:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2019-08-05; bh=0Ri4+EVQzC+oqEKwlM9t7r5MwQmXc/Csb9sxFWjZG28=; b=LcE40zEPi2TdWChPlYfru3ZhO76DnoEfMHJOhFf1ttzS5dpoKvTFL+7xnvabBKHeR+Gz Kbx5OrUpy6JJt5XV8THYxiKCGxQel0OVn8ooOD2oILVksqSWfE5fbZPRJSYy1MC3lvnG 0XoU3OqKSloUiIc3J7TY8np6OyTImU8LkZqy0wK/cez/uLIUbS+e8YgOmwan9s8YOrST CsgccNi8+C5Z72U3NzEevRp0c64msycG2xa4fc4pIYtKTwK1i7LzPjRWb0wgMapjAJ/c QA36TfodnpPcRcyG+5Ovu5L4EMxGxUu8yLccuayqxAsU5ffMfJG1rzvWXe04C1+sYDz4 MA== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2120.oracle.com with ESMTP id 2vxwhfgb0g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:20 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x9TNRkW7069172 for ; Tue, 29 Oct 2019 23:32:19 GMT Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userp3030.oracle.com with ESMTP id 2vxwhuvgvk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Tue, 29 Oct 2019 23:32:19 +0000 Received: from abhmp0005.oracle.com (abhmp0005.oracle.com [141.146.116.11]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id x9TNWJ8K012172 for ; Tue, 29 Oct 2019 23:32:19 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 29 Oct 2019 16:32:18 -0700 Subject: [PATCH 5/5] xfs: repair refcount btrees From: "Darrick J. Wong" To: darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Tue, 29 Oct 2019 16:32:17 -0700 Message-ID: <157239193780.1267304.8597220745391732088.stgit@magnolia> In-Reply-To: <157239190609.1267304.9008396217521176875.stgit@magnolia> References: <157239190609.1267304.9008396217521176875.stgit@magnolia> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9425 signatures=668685 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-1910290206 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Reconstruct the refcount data from the rmap btree. Signed-off-by: Darrick J. Wong --- fs/xfs/Makefile | 1 fs/xfs/scrub/refcount_repair.c | 604 ++++++++++++++++++++++++++++++++++++++++ fs/xfs/scrub/repair.h | 2 fs/xfs/scrub/scrub.c | 2 fs/xfs/scrub/trace.h | 13 - 5 files changed, 615 insertions(+), 7 deletions(-) create mode 100644 fs/xfs/scrub/refcount_repair.c diff --git a/fs/xfs/Makefile b/fs/xfs/Makefile index 6ca9755f031e..5ece4a554c9f 100644 --- a/fs/xfs/Makefile +++ b/fs/xfs/Makefile @@ -164,6 +164,7 @@ xfs-y += $(addprefix scrub/, \ array.o \ bitmap.o \ ialloc_repair.o \ + refcount_repair.o \ repair.o \ xfile.o \ ) diff --git a/fs/xfs/scrub/refcount_repair.c b/fs/xfs/scrub/refcount_repair.c new file mode 100644 index 000000000000..a95ee21251d3 --- /dev/null +++ b/fs/xfs/scrub/refcount_repair.c @@ -0,0 +1,604 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2019 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "xfs.h" +#include "xfs_fs.h" +#include "xfs_shared.h" +#include "xfs_format.h" +#include "xfs_trans_resv.h" +#include "xfs_mount.h" +#include "xfs_defer.h" +#include "xfs_btree.h" +#include "xfs_bit.h" +#include "xfs_log_format.h" +#include "xfs_trans.h" +#include "xfs_sb.h" +#include "xfs_alloc.h" +#include "xfs_ialloc.h" +#include "xfs_rmap.h" +#include "xfs_rmap_btree.h" +#include "xfs_refcount.h" +#include "xfs_refcount_btree.h" +#include "xfs_error.h" +#include "scrub/xfs_scrub.h" +#include "scrub/scrub.h" +#include "scrub/common.h" +#include "scrub/btree.h" +#include "scrub/trace.h" +#include "scrub/repair.h" +#include "scrub/bitmap.h" +#include "scrub/array.h" + +/* + * Rebuilding the Reference Count Btree + * ==================================== + * + * This algorithm is "borrowed" from xfs_repair. Imagine the rmap + * entries as rectangles representing extents of physical blocks, and + * that the rectangles can be laid down to allow them to overlap each + * other; then we know that we must emit a refcnt btree entry wherever + * the amount of overlap changes, i.e. the emission stimulus is + * level-triggered: + * + * - --- + * -- ----- ---- --- ------ + * -- ---- ----------- ---- --------- + * -------------------------------- ----------- + * ^ ^ ^^ ^^ ^ ^^ ^^^ ^^^^ ^ ^^ ^ ^ ^ + * 2 1 23 21 3 43 234 2123 1 01 2 3 0 + * + * For our purposes, a rmap is a tuple (startblock, len, fileoff, owner). + * + * Note that in the actual refcnt btree we don't store the refcount < 2 + * cases because the bnobt tells us which blocks are free; single-use + * blocks aren't recorded in the bnobt or the refcntbt. If the rmapbt + * supports storing multiple entries covering a given block we could + * theoretically dispense with the refcntbt and simply count rmaps, but + * that's inefficient in the (hot) write path, so we'll take the cost of + * the extra tree to save time. Also there's no guarantee that rmap + * will be enabled. + * + * Given an array of rmaps sorted by physical block number, a starting + * physical block (sp), a bag to hold rmaps that cover sp, and the next + * physical block where the level changes (np), we can reconstruct the + * refcount btree as follows: + * + * While there are still unprocessed rmaps in the array, + * - Set sp to the physical block (pblk) of the next unprocessed rmap. + * - Add to the bag all rmaps in the array where startblock == sp. + * - Set np to the physical block where the bag size will change. This + * is the minimum of (the pblk of the next unprocessed rmap) and + * (startblock + len of each rmap in the bag). + * - Record the bag size as old_bag_size. + * + * - While the bag isn't empty, + * - Remove from the bag all rmaps where startblock + len == np. + * - Add to the bag all rmaps in the array where startblock == np. + * - If the bag size isn't old_bag_size, store the refcount entry + * (sp, np - sp, bag_size) in the refcnt btree. + * - If the bag is empty, break out of the inner loop. + * - Set old_bag_size to the bag size + * - Set sp = np. + * - Set np to the physical block where the bag size will change. + * This is the minimum of (the pblk of the next unprocessed rmap) + * and (startblock + len of each rmap in the bag). + * + * Like all the other repairers, we make a list of all the refcount + * records we need, then reinitialize the refcount btree root and + * insert all the records. + */ + +/* The only parts of the rmap that we care about for computing refcounts. */ +struct xrep_refc_rmap { + xfs_agblock_t startblock; + xfs_extlen_t blockcount; +} __packed; + +struct xrep_refc { + /* refcount extents */ + struct xfbma *refcount_records; + + /* new refcountbt information */ + struct xrep_newbt new_btree_info; + struct xfs_btree_bload refc_bload; + + /* old refcountbt blocks */ + struct xbitmap old_refcountbt_blocks; + + struct xfs_scrub *sc; + + /* # of refcountbt blocks */ + xfs_extlen_t btblocks; + + /* get_data()'s position in the free space record array. */ + uint64_t iter; +}; + +/* Record a reference count extent. */ +STATIC int +xrep_refc_remember( + struct xrep_refc *rr, + xfs_agblock_t agbno, + xfs_extlen_t len, + xfs_nlink_t refcount) +{ + struct xfs_refcount_irec irec = { + .rc_startblock = agbno, + .rc_blockcount = len, + .rc_refcount = refcount, + }; + + trace_xrep_refc_found(rr->sc->mp, rr->sc->sa.agno, agbno, len, + refcount); + + return xfbma_append(rr->refcount_records, &irec); +} + +/* Record a CoW staging extent. */ +STATIC int +xrep_refc_remember_cow( + struct xrep_refc *rr, + xfs_agblock_t agbno, + xfs_extlen_t len) +{ + return xrep_refc_remember(rr, agbno + XFS_REFC_COW_START, len, 1); +} + +/* Grab the next (abbreviated) rmap record from the rmapbt. */ +STATIC int +xrep_refc_next_rrm( + struct xfs_btree_cur *cur, + struct xrep_refc *rr, + struct xrep_refc_rmap *rrm, + bool *have_rec) +{ + struct xfs_rmap_irec rmap; + struct xfs_mount *mp = cur->bc_mp; + xfs_fsblock_t fsbno; + int have_gt; + int error = 0; + + *have_rec = false; + /* + * Loop through the remaining rmaps. Remember CoW staging + * extents and the refcountbt blocks from the old tree for later + * disposal. We can only share written data fork extents, so + * keep looping until we find an rmap for one. + */ + do { + if (xchk_should_terminate(rr->sc, &error)) + goto out_error; + + error = xfs_btree_increment(cur, 0, &have_gt); + if (error) + goto out_error; + if (!have_gt) + return 0; + + error = xfs_rmap_get_rec(cur, &rmap, &have_gt); + if (error) + goto out_error; + XFS_WANT_CORRUPTED_GOTO(mp, have_gt == 1, out_error); + + if (rmap.rm_owner == XFS_RMAP_OWN_COW) { + error = xrep_refc_remember_cow(rr, rmap.rm_startblock, + rmap.rm_blockcount); + if (error) + goto out_error; + } else if (rmap.rm_owner == XFS_RMAP_OWN_REFC) { + /* refcountbt block, dump it when we're done. */ + rr->btblocks += rmap.rm_blockcount; + fsbno = XFS_AGB_TO_FSB(cur->bc_mp, + cur->bc_private.a.agno, + rmap.rm_startblock); + error = xbitmap_set(&rr->old_refcountbt_blocks, + fsbno, rmap.rm_blockcount); + if (error) + goto out_error; + } + } while (XFS_RMAP_NON_INODE_OWNER(rmap.rm_owner) || + xfs_internal_inum(mp, rmap.rm_owner) || + (rmap.rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK | + XFS_RMAP_UNWRITTEN))); + + rrm->startblock = rmap.rm_startblock; + rrm->blockcount = rmap.rm_blockcount; + *have_rec = true; + return 0; + +out_error: + return error; +} + +/* Compare two btree extents. */ +static int +xrep_refc_extent_cmp( + const void *a, + const void *b) +{ + const struct xfs_refcount_irec *ap = a; + const struct xfs_refcount_irec *bp = b; + + if (ap->rc_startblock > bp->rc_startblock) + return 1; + else if (ap->rc_startblock < bp->rc_startblock) + return -1; + return 0; +} + +#define RRM_NEXT(r) ((r).startblock + (r).blockcount) +/* + * Find the next block where the refcount changes, given the next rmap we + * looked at and the ones we're already tracking. + */ +static inline xfs_agblock_t +xrep_refc_next_edge( + struct xfbma *rmap_bag, + struct xrep_refc_rmap *next_rrm, + bool next_valid) +{ + struct xrep_refc_rmap rrm; + uint64_t i; + xfs_agblock_t nbno; + + nbno = next_valid ? next_rrm->startblock : NULLAGBLOCK; + foreach_xfbma_item(rmap_bag, i, rrm) + nbno = min_t(xfs_agblock_t, nbno, RRM_NEXT(rrm)); + return nbno; +} + +/* Iterate all the rmap records to generate reference count data. */ +STATIC int +xrep_refc_find_refcounts( + struct xrep_refc *rr) +{ + struct xrep_refc_rmap rrm; + struct xfs_scrub *sc = rr->sc; + struct xfbma *rmap_bag; + struct xfs_btree_cur *cur; + xfs_agblock_t sbno; + xfs_agblock_t cbno; + xfs_agblock_t nbno; + size_t old_stack_sz; + size_t stack_sz = 0; + bool have; + int have_gt; + int error; + + /* Start the rmapbt cursor to the left of all records. */ + cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp, + sc->sa.agno); + error = xfs_rmap_lookup_le(cur, 0, 0, 0, 0, 0, &have_gt); + if (error) + goto out_cur; + ASSERT(have_gt == 0); + + /* Set up some storage */ + rmap_bag = xfbma_init(sizeof(struct xrep_refc_rmap)); + if (IS_ERR(rmap_bag)) { + error = PTR_ERR(rmap_bag); + goto out_cur; + } + + /* Process reverse mappings into refcount data. */ + while (xfs_btree_has_more_records(cur)) { + /* Push all rmaps with pblk == sbno onto the stack */ + error = xrep_refc_next_rrm(cur, rr, &rrm, &have); + if (error) + goto out; + if (!have) + break; + sbno = cbno = rrm.startblock; + while (have && rrm.startblock == sbno) { + error = xfbma_insert_anywhere(rmap_bag, &rrm); + if (error) + goto out; + stack_sz++; + error = xrep_refc_next_rrm(cur, rr, &rrm, &have); + if (error) + goto out; + } + error = xfs_btree_decrement(cur, 0, &have_gt); + if (error) + goto out; + XFS_WANT_CORRUPTED_GOTO(sc->mp, have_gt, out); + + /* Set nbno to the bno of the next refcount change */ + nbno = xrep_refc_next_edge(rmap_bag, &rrm, have); + if (nbno == NULLAGBLOCK) { + error = -EFSCORRUPTED; + goto out; + } + + ASSERT(nbno > sbno); + old_stack_sz = stack_sz; + + /* While stack isn't empty... */ + while (stack_sz) { + uint64_t i; + + /* Pop all rmaps that end at nbno */ + foreach_xfbma_item(rmap_bag, i, rrm) { + if (RRM_NEXT(rrm) != nbno) + continue; + error = xfbma_nullify(rmap_bag, i); + if (error) + goto out; + stack_sz--; + } + + /* Push array items that start at nbno */ + error = xrep_refc_next_rrm(cur, rr, &rrm, &have); + if (error) + goto out; + while (have && rrm.startblock == nbno) { + error = xfbma_insert_anywhere(rmap_bag, + &rrm); + if (error) + goto out; + stack_sz++; + error = xrep_refc_next_rrm(cur, rr, &rrm, + &have); + if (error) + goto out; + } + error = xfs_btree_decrement(cur, 0, &have_gt); + if (error) + goto out; + XFS_WANT_CORRUPTED_GOTO(sc->mp, have_gt, out); + + /* Emit refcount if necessary */ + ASSERT(nbno > cbno); + if (stack_sz != old_stack_sz) { + if (old_stack_sz > 1) { + error = xrep_refc_remember(rr, cbno, + nbno - cbno, + old_stack_sz); + if (error) + goto out; + } + cbno = nbno; + } + + /* Stack empty, go find the next rmap */ + if (stack_sz == 0) + break; + old_stack_sz = stack_sz; + sbno = nbno; + + /* Set nbno to the bno of the next refcount change */ + nbno = xrep_refc_next_edge(rmap_bag, &rrm, have); + if (nbno == NULLAGBLOCK) { + error = -EFSCORRUPTED; + goto out; + } + + ASSERT(nbno > sbno); + } + } + + ASSERT(stack_sz == 0); +out: + xfbma_destroy(rmap_bag); +out_cur: + xfs_btree_del_cursor(cur, error); + return error; +} +#undef RRM_NEXT + +/* Retrieve refcountbt data for bulk load. */ +STATIC int +xrep_refc_get_data( + struct xfs_btree_cur *cur, + void *priv) +{ + struct xfs_refcount_irec *refc = &cur->bc_rec.rc; + struct xrep_refc *rr = priv; + int error; + + do { + error = xfbma_get(rr->refcount_records, rr->iter++, refc); + } while (error == 0 && xfbma_is_null(rr->refcount_records, refc)); + + return error; +} + +/* Feed one of the new btree blocks to the bulk loader. */ +STATIC int +xrep_refc_alloc_block( + struct xfs_btree_cur *cur, + union xfs_btree_ptr *ptr, + void *priv) +{ + struct xrep_refc *rr = priv; + + return xrep_newbt_claim_block(cur, &rr->new_btree_info, ptr); +} + +/* Update the AGF counters. */ +STATIC int +xrep_refc_reset_counters( + struct xrep_refc *rr) +{ + struct xfs_scrub *sc = rr->sc; + struct xfs_perag *pag = sc->sa.pag; + struct xfs_buf *bp; + + /* + * Mark the pagf information stale and use the accessor function to + * forcibly reload it from the values we just logged. We still own the + * AGF bp so we can safely ignore bp. + */ + ASSERT(pag->pagf_init); + pag->pagf_init = 0; + + return xfs_alloc_read_agf(sc->mp, sc->tp, sc->sa.agno, 0, &bp); +} + +/* + * Use the collected refcount information to stage a new refcount btree. If + * this is successful we'll return with the new btree root information logged + * to the repair transaction but not yet committed. + */ +STATIC int +xrep_refc_build_new_tree( + struct xrep_refc *rr) +{ + struct xfs_scrub *sc = rr->sc; + struct xfs_btree_cur *refc_cur; + int error; + + rr->refc_bload.get_data = xrep_refc_get_data; + rr->refc_bload.alloc_block = xrep_refc_alloc_block; + xrep_bload_estimate_slack(sc, &rr->refc_bload); + + /* + * Sort the refcount extents by startblock or else the btree records + * will be in the wrong order. + */ + error = xfbma_sort(rr->refcount_records, xrep_refc_extent_cmp); + if (error) + return error; + + /* + * Prepare to construct the new btree by reserving disk space for the + * new btree and setting up all the accounting information we'll need + * to root the new btree while it's under construction and before we + * attach it to the AG header. + */ + xrep_newbt_init_ag(&rr->new_btree_info, sc, &XFS_RMAP_OINFO_REFC, + XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, + xfs_refc_block(sc->mp)), + XFS_AG_RESV_METADATA); + + /* Compute how many blocks we'll need. */ + refc_cur = xfs_refcountbt_stage_cursor(sc->mp, sc->tp, + &rr->new_btree_info.afake, sc->sa.agno); + error = xfs_btree_bload_compute_geometry(refc_cur, &rr->refc_bload, + xfbma_length(rr->refcount_records)); + if (error) + goto err_cur; + xfs_btree_del_cursor(refc_cur, error); + + /* + * Reserve the space we'll need for the new btree. Drop the cursor + * while we do this because that can roll the transaction and cursors + * can't handle that. + */ + error = xrep_newbt_alloc_blocks(&rr->new_btree_info, + rr->refc_bload.nr_blocks); + if (error) + goto err_newbt; + + /* Add all observed refcount records. */ + rr->iter = 0; + refc_cur = xfs_refcountbt_stage_cursor(sc->mp, sc->tp, + &rr->new_btree_info.afake, sc->sa.agno); + error = xfs_btree_bload(refc_cur, &rr->refc_bload, rr); + if (error) + goto err_cur; + + /* + * Install the new btree in the AG header. After this point the old + * btree is no longer accessible and the new tree is live. + * + * Note: We re-read the AGF here to ensure the buffer type is set + * properly. Since we built a new tree without attaching to the AGF + * buffer, the buffer item may have fallen off the buffer. This ought + * to succeed since the AGF is held across transaction rolls. + */ + error = xfs_read_agf(sc->mp, sc->tp, sc->sa.agno, 0, &sc->sa.agf_bp); + if (error) + goto err_cur; + + /* Commit our new btree. */ + xfs_refcountbt_commit_staged_btree(refc_cur, sc->sa.agf_bp); + xfs_btree_del_cursor(refc_cur, 0); + + /* Reset the AGF counters now that we've changed the btree shape. */ + error = xrep_refc_reset_counters(rr); + if (error) + goto err_newbt; + + /* Dispose of any unused blocks and the accounting information. */ + xrep_newbt_destroy(&rr->new_btree_info, error); + + return xrep_roll_ag_trans(sc); +err_cur: + xfs_btree_del_cursor(refc_cur, error); +err_newbt: + xrep_newbt_destroy(&rr->new_btree_info, error); + return error; +} + +/* + * Now that we've logged the roots of the new btrees, invalidate all of the + * old blocks and free them. + */ +STATIC int +xrep_refc_remove_old_tree( + struct xrep_refc *rr) +{ + struct xfs_scrub *sc = rr->sc; + int error; + + /* Free the old refcountbt blocks if they're not in use. */ + error = xrep_reap_extents(sc, &rr->old_refcountbt_blocks, + &XFS_RMAP_OINFO_REFC, XFS_AG_RESV_METADATA); + if (error) + return error; + + sc->flags |= XREP_RESET_PERAG_RESV; + return 0; +} + +/* Rebuild the refcount btree. */ +int +xrep_refcountbt( + struct xfs_scrub *sc) +{ + struct xrep_refc *rr; + struct xfs_mount *mp = sc->mp; + int error; + + /* We require the rmapbt to rebuild anything. */ + if (!xfs_sb_version_hasrmapbt(&mp->m_sb)) + return -EOPNOTSUPP; + + rr = kmem_zalloc(sizeof(struct xrep_refc), KM_NOFS | KM_MAYFAIL); + if (!rr) + return -ENOMEM; + rr->sc = sc; + + xchk_perag_get(sc->mp, &sc->sa); + + /* Set up some storage */ + rr->refcount_records = xfbma_init(sizeof(struct xfs_refcount_irec)); + if (IS_ERR(rr->refcount_records)) { + error = PTR_ERR(rr->refcount_records); + goto out_rr; + } + + /* Collect all reference counts. */ + xbitmap_init(&rr->old_refcountbt_blocks); + error = xrep_refc_find_refcounts(rr); + if (error) + goto out_bitmap; + + /* Rebuild the refcount information. */ + error = xrep_refc_build_new_tree(rr); + if (error) + goto out_bitmap; + + /* Kill the old tree. */ + error = xrep_refc_remove_old_tree(rr); + +out_bitmap: + xbitmap_destroy(&rr->old_refcountbt_blocks); + xfbma_destroy(rr->refcount_records); +out_rr: + kmem_free(rr); + return error; +} diff --git a/fs/xfs/scrub/repair.h b/fs/xfs/scrub/repair.h index 8b320e905e00..c0769aaae9a4 100644 --- a/fs/xfs/scrub/repair.h +++ b/fs/xfs/scrub/repair.h @@ -70,6 +70,7 @@ int xrep_agfl(struct xfs_scrub *sc); int xrep_agi(struct xfs_scrub *sc); int xrep_allocbt(struct xfs_scrub *sc); int xrep_iallocbt(struct xfs_scrub *sc); +int xrep_refcountbt(struct xfs_scrub *sc); struct xrep_newbt_resv { /* Link to list of extents that we've reserved. */ @@ -167,6 +168,7 @@ xrep_reset_perag_resv( #define xrep_agi xrep_notsupported #define xrep_allocbt xrep_notsupported #define xrep_iallocbt xrep_notsupported +#define xrep_refcountbt xrep_notsupported #endif /* CONFIG_XFS_ONLINE_REPAIR */ diff --git a/fs/xfs/scrub/scrub.c b/fs/xfs/scrub/scrub.c index 6011823d0d40..b104231af049 100644 --- a/fs/xfs/scrub/scrub.c +++ b/fs/xfs/scrub/scrub.c @@ -254,7 +254,7 @@ static const struct xchk_meta_ops meta_scrub_ops[] = { .setup = xchk_setup_ag_refcountbt, .scrub = xchk_refcountbt, .has = xfs_sb_version_hasreflink, - .repair = xrep_notsupported, + .repair = xrep_refcountbt, }, [XFS_SCRUB_TYPE_INODE] = { /* inode record */ .type = ST_INODE, diff --git a/fs/xfs/scrub/trace.h b/fs/xfs/scrub/trace.h index 9bf75c97fdd1..ed9484de80fe 100644 --- a/fs/xfs/scrub/trace.h +++ b/fs/xfs/scrub/trace.h @@ -782,10 +782,11 @@ TRACE_EVENT(xrep_ibt_found, __entry->freemask) ) -TRACE_EVENT(xrep_refcount_extent_fn, +TRACE_EVENT(xrep_refc_found, TP_PROTO(struct xfs_mount *mp, xfs_agnumber_t agno, - struct xfs_refcount_irec *irec), - TP_ARGS(mp, agno, irec), + xfs_agblock_t startblock, xfs_extlen_t blockcount, + xfs_nlink_t refcount), + TP_ARGS(mp, agno, startblock, blockcount, refcount), TP_STRUCT__entry( __field(dev_t, dev) __field(xfs_agnumber_t, agno) @@ -796,9 +797,9 @@ TRACE_EVENT(xrep_refcount_extent_fn, TP_fast_assign( __entry->dev = mp->m_super->s_dev; __entry->agno = agno; - __entry->startblock = irec->rc_startblock; - __entry->blockcount = irec->rc_blockcount; - __entry->refcount = irec->rc_refcount; + __entry->startblock = startblock; + __entry->blockcount = blockcount; + __entry->refcount = refcount; ), TP_printk("dev %d:%d agno %u agbno %u len %u refcount %u", MAJOR(__entry->dev), MINOR(__entry->dev),