2018-07-30 13:37:09 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Oracle. All Rights Reserved.
|
|
|
|
* Author: Darrick J. Wong <darrick.wong@oracle.com>
|
|
|
|
*/
|
|
|
|
#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 "scrub/xfs_scrub.h"
|
|
|
|
#include "scrub/scrub.h"
|
|
|
|
#include "scrub/common.h"
|
|
|
|
#include "scrub/trace.h"
|
|
|
|
#include "scrub/repair.h"
|
|
|
|
#include "scrub/bitmap.h"
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
/*
|
|
|
|
* Set a range of this bitmap. Caller must ensure the range is not set.
|
|
|
|
*
|
|
|
|
* This is the logical equivalent of bitmap |= mask(start, len).
|
|
|
|
*/
|
2018-07-30 13:37:09 +08:00
|
|
|
int
|
2018-07-31 02:18:13 +08:00
|
|
|
xfs_bitmap_set(
|
|
|
|
struct xfs_bitmap *bitmap,
|
|
|
|
uint64_t start,
|
|
|
|
uint64_t len)
|
2018-07-30 13:37:09 +08:00
|
|
|
{
|
2018-07-31 02:18:13 +08:00
|
|
|
struct xfs_bitmap_range *bmr;
|
2018-07-30 13:37:09 +08:00
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
bmr = kmem_alloc(sizeof(struct xfs_bitmap_range), KM_MAYFAIL);
|
|
|
|
if (!bmr)
|
2018-07-30 13:37:09 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
INIT_LIST_HEAD(&bmr->list);
|
|
|
|
bmr->start = start;
|
|
|
|
bmr->len = len;
|
|
|
|
list_add_tail(&bmr->list, &bitmap->list);
|
2018-07-30 13:37:09 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
/* Free everything related to this bitmap. */
|
2018-07-30 13:37:09 +08:00
|
|
|
void
|
2018-07-31 02:18:13 +08:00
|
|
|
xfs_bitmap_destroy(
|
|
|
|
struct xfs_bitmap *bitmap)
|
2018-07-30 13:37:09 +08:00
|
|
|
{
|
2018-07-31 02:18:13 +08:00
|
|
|
struct xfs_bitmap_range *bmr;
|
|
|
|
struct xfs_bitmap_range *n;
|
2018-07-30 13:37:09 +08:00
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
for_each_xfs_bitmap_extent(bmr, n, bitmap) {
|
|
|
|
list_del(&bmr->list);
|
|
|
|
kmem_free(bmr);
|
2018-07-30 13:37:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
/* Set up a per-AG block bitmap. */
|
|
|
|
void
|
|
|
|
xfs_bitmap_init(
|
|
|
|
struct xfs_bitmap *bitmap)
|
|
|
|
{
|
|
|
|
INIT_LIST_HEAD(&bitmap->list);
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:37:09 +08:00
|
|
|
/* Compare two btree extents. */
|
|
|
|
static int
|
2018-07-31 02:18:13 +08:00
|
|
|
xfs_bitmap_range_cmp(
|
2018-07-30 13:37:09 +08:00
|
|
|
void *priv,
|
|
|
|
struct list_head *a,
|
|
|
|
struct list_head *b)
|
|
|
|
{
|
2018-07-31 02:18:13 +08:00
|
|
|
struct xfs_bitmap_range *ap;
|
|
|
|
struct xfs_bitmap_range *bp;
|
2018-07-30 13:37:09 +08:00
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
ap = container_of(a, struct xfs_bitmap_range, list);
|
|
|
|
bp = container_of(b, struct xfs_bitmap_range, list);
|
2018-07-30 13:37:09 +08:00
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
if (ap->start > bp->start)
|
2018-07-30 13:37:09 +08:00
|
|
|
return 1;
|
2018-07-31 02:18:13 +08:00
|
|
|
if (ap->start < bp->start)
|
2018-07-30 13:37:09 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-07-31 02:18:13 +08:00
|
|
|
* Remove all the blocks mentioned in @sub from the extents in @bitmap.
|
2018-07-30 13:37:09 +08:00
|
|
|
*
|
|
|
|
* The intent is that callers will iterate the rmapbt for all of its records
|
2018-07-31 02:18:13 +08:00
|
|
|
* for a given owner to generate @bitmap; and iterate all the blocks of the
|
2018-07-30 13:37:09 +08:00
|
|
|
* metadata structures that are not being rebuilt and have the same rmapbt
|
2018-07-31 02:18:13 +08:00
|
|
|
* owner to generate @sub. This routine subtracts all the extents
|
|
|
|
* mentioned in sub from all the extents linked in @bitmap, which leaves
|
|
|
|
* @bitmap as the list of blocks that are not accounted for, which we assume
|
2018-07-30 13:37:09 +08:00
|
|
|
* are the dead blocks of the old metadata structure. The blocks mentioned in
|
2018-07-31 02:18:13 +08:00
|
|
|
* @bitmap can be reaped.
|
|
|
|
*
|
|
|
|
* This is the logical equivalent of bitmap &= ~sub.
|
2018-07-30 13:37:09 +08:00
|
|
|
*/
|
|
|
|
#define LEFT_ALIGNED (1 << 0)
|
|
|
|
#define RIGHT_ALIGNED (1 << 1)
|
|
|
|
int
|
2018-07-31 02:18:13 +08:00
|
|
|
xfs_bitmap_disunion(
|
|
|
|
struct xfs_bitmap *bitmap,
|
|
|
|
struct xfs_bitmap *sub)
|
2018-07-30 13:37:09 +08:00
|
|
|
{
|
|
|
|
struct list_head *lp;
|
2018-07-31 02:18:13 +08:00
|
|
|
struct xfs_bitmap_range *br;
|
|
|
|
struct xfs_bitmap_range *new_br;
|
|
|
|
struct xfs_bitmap_range *sub_br;
|
|
|
|
uint64_t sub_start;
|
|
|
|
uint64_t sub_len;
|
2018-07-30 13:37:09 +08:00
|
|
|
int state;
|
|
|
|
int error = 0;
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
if (list_empty(&bitmap->list) || list_empty(&sub->list))
|
2018-07-30 13:37:09 +08:00
|
|
|
return 0;
|
2018-07-31 02:18:13 +08:00
|
|
|
ASSERT(!list_empty(&sub->list));
|
2018-07-30 13:37:09 +08:00
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
list_sort(NULL, &bitmap->list, xfs_bitmap_range_cmp);
|
|
|
|
list_sort(NULL, &sub->list, xfs_bitmap_range_cmp);
|
2018-07-30 13:37:09 +08:00
|
|
|
|
|
|
|
/*
|
2018-07-31 02:18:13 +08:00
|
|
|
* Now that we've sorted both lists, we iterate bitmap once, rolling
|
|
|
|
* forward through sub and/or bitmap as necessary until we find an
|
2018-07-30 13:37:09 +08:00
|
|
|
* overlap or reach the end of either list. We do not reset lp to the
|
2018-07-31 02:18:13 +08:00
|
|
|
* head of bitmap nor do we reset sub_br to the head of sub. The
|
2018-07-30 13:37:09 +08:00
|
|
|
* list traversal is similar to merge sort, but we're deleting
|
|
|
|
* instead. In this manner we avoid O(n^2) operations.
|
|
|
|
*/
|
2018-07-31 02:18:13 +08:00
|
|
|
sub_br = list_first_entry(&sub->list, struct xfs_bitmap_range,
|
2018-07-30 13:37:09 +08:00
|
|
|
list);
|
2018-07-31 02:18:13 +08:00
|
|
|
lp = bitmap->list.next;
|
|
|
|
while (lp != &bitmap->list) {
|
|
|
|
br = list_entry(lp, struct xfs_bitmap_range, list);
|
2018-07-30 13:37:09 +08:00
|
|
|
|
|
|
|
/*
|
2018-07-31 02:18:13 +08:00
|
|
|
* Advance sub_br and/or br until we find a pair that
|
2018-07-30 13:37:09 +08:00
|
|
|
* intersect or we run out of extents.
|
|
|
|
*/
|
2018-07-31 02:18:13 +08:00
|
|
|
while (sub_br->start + sub_br->len <= br->start) {
|
|
|
|
if (list_is_last(&sub_br->list, &sub->list))
|
2018-07-30 13:37:09 +08:00
|
|
|
goto out;
|
2018-07-31 02:18:13 +08:00
|
|
|
sub_br = list_next_entry(sub_br, list);
|
2018-07-30 13:37:09 +08:00
|
|
|
}
|
2018-07-31 02:18:13 +08:00
|
|
|
if (sub_br->start >= br->start + br->len) {
|
2018-07-30 13:37:09 +08:00
|
|
|
lp = lp->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-31 02:18:13 +08:00
|
|
|
/* trim sub_br to fit the extent we have */
|
|
|
|
sub_start = sub_br->start;
|
|
|
|
sub_len = sub_br->len;
|
|
|
|
if (sub_br->start < br->start) {
|
|
|
|
sub_len -= br->start - sub_br->start;
|
|
|
|
sub_start = br->start;
|
2018-07-30 13:37:09 +08:00
|
|
|
}
|
2018-07-31 02:18:13 +08:00
|
|
|
if (sub_len > br->len)
|
|
|
|
sub_len = br->len;
|
2018-07-30 13:37:09 +08:00
|
|
|
|
|
|
|
state = 0;
|
2018-07-31 02:18:13 +08:00
|
|
|
if (sub_start == br->start)
|
2018-07-30 13:37:09 +08:00
|
|
|
state |= LEFT_ALIGNED;
|
2018-07-31 02:18:13 +08:00
|
|
|
if (sub_start + sub_len == br->start + br->len)
|
2018-07-30 13:37:09 +08:00
|
|
|
state |= RIGHT_ALIGNED;
|
|
|
|
switch (state) {
|
|
|
|
case LEFT_ALIGNED:
|
|
|
|
/* Coincides with only the left. */
|
2018-07-31 02:18:13 +08:00
|
|
|
br->start += sub_len;
|
|
|
|
br->len -= sub_len;
|
2018-07-30 13:37:09 +08:00
|
|
|
break;
|
|
|
|
case RIGHT_ALIGNED:
|
|
|
|
/* Coincides with only the right. */
|
2018-07-31 02:18:13 +08:00
|
|
|
br->len -= sub_len;
|
2018-07-30 13:37:09 +08:00
|
|
|
lp = lp->next;
|
|
|
|
break;
|
|
|
|
case LEFT_ALIGNED | RIGHT_ALIGNED:
|
|
|
|
/* Total overlap, just delete ex. */
|
|
|
|
lp = lp->next;
|
2018-07-31 02:18:13 +08:00
|
|
|
list_del(&br->list);
|
|
|
|
kmem_free(br);
|
2018-07-30 13:37:09 +08:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
/*
|
|
|
|
* Deleting from the middle: add the new right extent
|
|
|
|
* and then shrink the left extent.
|
|
|
|
*/
|
2018-07-31 02:18:13 +08:00
|
|
|
new_br = kmem_alloc(sizeof(struct xfs_bitmap_range),
|
2018-07-30 13:37:09 +08:00
|
|
|
KM_MAYFAIL);
|
2018-07-31 02:18:13 +08:00
|
|
|
if (!new_br) {
|
2018-07-30 13:37:09 +08:00
|
|
|
error = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-07-31 02:18:13 +08:00
|
|
|
INIT_LIST_HEAD(&new_br->list);
|
|
|
|
new_br->start = sub_start + sub_len;
|
|
|
|
new_br->len = br->start + br->len - new_br->start;
|
|
|
|
list_add(&new_br->list, &br->list);
|
|
|
|
br->len = sub_start - br->start;
|
2018-07-30 13:37:09 +08:00
|
|
|
lp = lp->next;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
#undef LEFT_ALIGNED
|
|
|
|
#undef RIGHT_ALIGNED
|