Skip to content

Commit 8655786

Browse files
jeffmahoneymasoncl
authored andcommitted
btrfs: skip superblocks during discard
Btrfs doesn't track superblocks with extent records so there is nothing persistent on-disk to indicate that those blocks are in use. We track the superblocks in memory to ensure they don't get used by removing them from the free space cache when we load a block group from disk. Prior to 47ab2a6c6a (Btrfs: remove empty block groups automatically), that was fine since the block group would never be reclaimed so the superblock was always safe. Once we started removing the empty block groups, we were protected by the fact that discards weren't being properly issued for unused space either via FITRIM or -odiscard. The block groups were still being released, but the blocks remained on disk. In order to properly discard unused block groups, we need to filter out the superblocks from the discard range. Superblocks are located at fixed locations on each device, so it makes sense to filter them out in btrfs_issue_discard, which is used by both -odiscard and FITRIM. Signed-off-by: Jeff Mahoney <[email protected]> Reviewed-by: Filipe Manana <[email protected]> Tested-by: Filipe Manana <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 4d89d37 commit 8655786

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

fs/btrfs/extent-tree.c

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,10 +1883,12 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,
18831883
return ret;
18841884
}
18851885

1886+
#define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
18861887
static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
18871888
u64 *discarded_bytes)
18881889
{
1889-
int ret = 0;
1890+
int j, ret = 0;
1891+
u64 bytes_left, end;
18901892
u64 aligned_start = ALIGN(start, 1 << 9);
18911893

18921894
if (WARN_ON(start != aligned_start)) {
@@ -1896,11 +1898,60 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
18961898
}
18971899

18981900
*discarded_bytes = 0;
1899-
if (len) {
1900-
ret = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1901+
1902+
if (!len)
1903+
return 0;
1904+
1905+
end = start + len;
1906+
bytes_left = len;
1907+
1908+
/* Skip any superblocks on this device. */
1909+
for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1910+
u64 sb_start = btrfs_sb_offset(j);
1911+
u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1912+
u64 size = sb_start - start;
1913+
1914+
if (!in_range(sb_start, start, bytes_left) &&
1915+
!in_range(sb_end, start, bytes_left) &&
1916+
!in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1917+
continue;
1918+
1919+
/*
1920+
* Superblock spans beginning of range. Adjust start and
1921+
* try again.
1922+
*/
1923+
if (sb_start <= start) {
1924+
start += sb_end - start;
1925+
if (start > end) {
1926+
bytes_left = 0;
1927+
break;
1928+
}
1929+
bytes_left = end - start;
1930+
continue;
1931+
}
1932+
1933+
if (size) {
1934+
ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1935+
GFP_NOFS, 0);
1936+
if (!ret)
1937+
*discarded_bytes += size;
1938+
else if (ret != -EOPNOTSUPP)
1939+
return ret;
1940+
}
1941+
1942+
start = sb_end;
1943+
if (start > end) {
1944+
bytes_left = 0;
1945+
break;
1946+
}
1947+
bytes_left = end - start;
1948+
}
1949+
1950+
if (bytes_left) {
1951+
ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
19011952
GFP_NOFS, 0);
19021953
if (!ret)
1903-
*discarded_bytes = len;
1954+
*discarded_bytes += bytes_left;
19041955
}
19051956
return ret;
19061957
}

0 commit comments

Comments
 (0)