1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright (c) 2019 Nikolay Borisov, SUSE LLC. All Rights Reserved.
4 # Parses btrfs' device tree for holes. For example given the followin device extents:
5 # item 3 key (1 DEV_EXTENT 38797312) itemoff 16091 itemsize 48
6 # dev extent chunk_tree 3
7 # chunk_objectid 256 chunk_offset 30408704 length 1073741824
8 # chunk_tree_uuid b8c8f022-452b-4fc1-bf5c-b42d9fba613e
9 # item 4 key (1 DEV_EXTENT 1112539136) itemoff 16043 itemsize 48
10 # dev extent chunk_tree 3
11 # chunk_objectid 256 chunk_offset 30408704 length 1073741824
12 # chunk_tree_uuid b8c8f022-452b-4fc1-bf5c-b42d9fba613e
14 # The scripts will find out if there is a hole between 38797312+1073741824 and
15 # the start offset of the next extent, in this case 1112539136 so no hole. But
16 # if there was it would have printed the size of the hole.
18 # Following paramters must be set:
19 # * sectorsize - how many bytes per sector, used to convert script's output
21 # * devsize - size of the device in bytes, used to output the final
24 # Given a 'chunk_objectid 256 chunk_offset 22020096 length 8388608' line this
25 # function returns 8388608
26 function get_extent_size(line, tmp) {
31 # Given a ' item 3 key (1 DEV_EXTENT 38797312) itemoff 16091 itemsize 48' line
32 # this function returns 38797312
33 function get_extent_offset(line, tmp) {
40 dev_extent_match="^.item [0-9]* key \\([0-9]* DEV_EXTENT [0-9]*\\).*"
41 dev_extent_len_match="^\\s*chunk_objectid [0-9]* chunk_offset [0-9]* length [0-9]*$"
45 if (match($0, dev_extent_match)) {
46 extent_offset = get_extent_offset($0)
47 if (prev_extent_end) {
48 hole_size = extent_offset - prev_extent_end
50 print prev_extent_end / sectorsize, int((extent_offset - 1) / sectorsize)
53 } else if (match($0, dev_extent_len_match)) {
54 extent_size = get_extent_size($0)
55 prev_extent_end = extent_offset + extent_size
60 if (prev_extent_end) {
61 print prev_extent_end / sectorsize, int((devsize - 1) / sectorsize)