| 1 |
|
| 2 |
|
| 3 |
import btrfs |
| 4 |
import os |
| 5 |
import struct |
| 6 |
import sys |
| 7 |
import textwrap |
| 8 |
|
| 9 |
from btrfs.ctree import Key, EXTENT_DATA_KEY, FILE_EXTENT_REG, COMPRESS_NONE |
| 10 |
from btrfs.ctree import CSUM_TREE_OBJECTID, EXTENT_CSUM_OBJECTID, EXTENT_CSUM_KEY |
| 11 |
from btrfs.ctree import ULLONG_MAX |
| 12 |
|
| 13 |
CSUM_SIZE = 4 |
| 14 |
csum_struct = struct.Struct('<L') |
| 15 |
csum_function = btrfs.crc32c.crc32c_data |
| 16 |
|
| 17 |
if len(sys.argv) < 2: |
| 18 |
print("Usage: {} <file>".format(sys.argv[0])) |
| 19 |
sys.exit(1) |
| 20 |
|
| 21 |
filename = sys.argv[1] |
| 22 |
|
| 23 |
if not os.path.isfile(filename): |
| 24 |
print("{} is not a regular file!".format(filename)) |
| 25 |
sys.exit(1) |
| 26 |
|
| 27 |
|
| 28 |
print("""------------------------------------- 8< -------------------------------------- |
| 29 |
The purpose of this example is to explore how checksums for data are stored |
| 30 |
inside the checksum tree, and how to find a checksum for a given block of data. |
| 31 |
|
| 32 |
The way in which checksums are looked up in here is clumsy and does not reflect |
| 33 |
the way in which it happens in kernel code. E.g. the fact that we can only |
| 34 |
search forward and not backwards in the trees limits the ability to quickly |
| 35 |
look around and find the right csum item. |
| 36 |
|
| 37 |
In order to make sure the example gets things right, we compare the checksum we |
| 38 |
found against a computed checksum of the file data. To prevent the example code |
| 39 |
from getting too complicated, we require the data extent not to be compressed. |
| 40 |
Also, it has to be a regular extent, because inline extents are part of |
| 41 |
metadata, which has the checksum stored inside the metadata block itself and |
| 42 |
not in the checksum tree. |
| 43 |
|
| 44 |
Keep in mind that this way of checking checksums does not make any sense except |
| 45 |
for the purpose of the example here. An online btrfs filesystem would never |
| 46 |
allow us to read corrupted data. Also, the file we're looking at can use part |
| 47 |
of a data block if it's at the end of the file. |
| 48 |
|
| 49 |
Also, the ioctl interface is not able to tell us which checksum type is used |
| 50 |
inside the filesystem. In here's it's hardcoded to crc32c, because that's the |
| 51 |
only available option in btrfs at this time. |
| 52 |
|
| 53 |
------------------------------------- 8< -------------------------------------- |
| 54 |
""") |
| 55 |
|
| 56 |
|
| 57 |
def wraprint(text): |
| 58 |
for line in textwrap.wrap(text, 80): |
| 59 |
print(line) |
| 60 |
print() |
| 61 |
|
| 62 |
|
| 63 |
inum = os.stat(filename).st_ino |
| 64 |
fd = os.open(filename, os.O_RDONLY) |
| 65 |
tree, _ = btrfs.ioctl.ino_lookup(fd, objectid=inum) |
| 66 |
os.close(fd) |
| 67 |
wraprint("File {} has inode number {} in tree {}.".format(filename, inum, tree)) |
| 68 |
|
| 69 |
fs = btrfs.FileSystem(filename) |
| 70 |
|
| 71 |
|
| 72 |
def first_regular_file_extent(inum, tree): |
| 73 |
min_key = Key(inum, EXTENT_DATA_KEY, 0) |
| 74 |
max_key = Key(inum, EXTENT_DATA_KEY + 1, 0) - 1 |
| 75 |
for header, data in btrfs.ioctl.search_v2(fs.fd, tree, min_key, max_key): |
| 76 |
extent = btrfs.ctree.FileExtentItem(header, data) |
| 77 |
if extent.type == FILE_EXTENT_REG and extent.disk_bytenr != 0 \ |
| 78 |
and extent.num_bytes >= fs.sectorsize and extent.compression == COMPRESS_NONE: |
| 79 |
return extent |
| 80 |
|
| 81 |
|
| 82 |
wraprint("Looking for the first reference to a regular data extent that is at least " |
| 83 |
"sectorsize {} big and does not use compression...".format(fs.sectorsize)) |
| 84 |
extent = first_regular_file_extent(inum, tree) |
| 85 |
if extent is None: |
| 86 |
wraprint("No regular extent found, try another file.") |
| 87 |
sys.exit() |
| 88 |
|
| 89 |
wraprint("At offset {} in the file, it's using {} bytes of data which we can find at offset {} " |
| 90 |
"inside a data extent at vaddr {}.".format( |
| 91 |
extent.logical_offset, extent.num_bytes, extent.offset, extent.disk_bytenr)) |
| 92 |
|
| 93 |
vaddr = extent.disk_bytenr + extent.offset |
| 94 |
|
| 95 |
wraprint("Now, we first look up the checksum value for one block ({} bytes) " |
| 96 |
"of data at vaddr {} ({} + {}).".format( |
| 97 |
fs.sectorsize, vaddr, extent.disk_bytenr, extent.offset)) |
| 98 |
wraprint("If we're lucky, the checksum tree has a key at {}. " |
| 99 |
"If not, we have to try searching back a bit to find the csum object that " |
| 100 |
"holds information about our data block. Searching back is done in a very clumsy " |
| 101 |
"way, because we can only search forward when using the search ioctl.".format(vaddr)) |
| 102 |
|
| 103 |
|
| 104 |
def search_extent_csum_after(vaddr, min_vaddr): |
| 105 |
if min_vaddr < 0: |
| 106 |
min_vaddr = 0 |
| 107 |
min_key = Key(EXTENT_CSUM_OBJECTID, EXTENT_CSUM_KEY, min_vaddr) |
| 108 |
max_key = Key(EXTENT_CSUM_OBJECTID, EXTENT_CSUM_KEY, ULLONG_MAX) |
| 109 |
prev_header = None |
| 110 |
prev_data = None |
| 111 |
for header, data in btrfs.ioctl.search_v2(fs.fd, CSUM_TREE_OBJECTID, min_key, max_key): |
| 112 |
if header.offset > vaddr: |
| 113 |
if prev_header is not None: |
| 114 |
return prev_header, prev_data |
| 115 |
return header, data |
| 116 |
prev_header, prev_data = header, data |
| 117 |
return prev_header, prev_data |
| 118 |
|
| 119 |
|
| 120 |
def search_extent_csum_for(vaddr): |
| 121 |
min_vaddr = vaddr |
| 122 |
header, data = search_extent_csum_after(vaddr, min_vaddr) |
| 123 |
look_back = 4096 |
| 124 |
while header is None or header.offset > vaddr: |
| 125 |
print("Next found extent csum at {} is {}.".format(min_vaddr, header.offset)) |
| 126 |
print("Restarting search {} bytes before our target vaddr.".format( |
| 127 |
btrfs.utils.pretty_size(look_back))) |
| 128 |
min_vaddr = vaddr-look_back |
| 129 |
header, data = search_extent_csum_after(vaddr, min_vaddr) |
| 130 |
look_back = look_back * 2 |
| 131 |
print() |
| 132 |
return header, data |
| 133 |
|
| 134 |
|
| 135 |
header, data = search_extent_csum_for(vaddr) |
| 136 |
csum_covers_vaddr_start = header.offset |
| 137 |
nr_csums = header.len // CSUM_SIZE |
| 138 |
csum_covers_vaddr_end = header.offset + nr_csums * fs.sectorsize |
| 139 |
|
| 140 |
if vaddr < csum_covers_vaddr_start or vaddr + fs.sectorsize > csum_covers_vaddr_end: |
| 141 |
wraprint("BUG: we got a csum extent that does not cover our block: " |
| 142 |
"[{},{}). The range covered by the csum object is: [{},{}).".format( |
| 143 |
vaddr, vaddr + fs.sectorsize, csum_covers_vaddr_start, csum_covers_vaddr_end)) |
| 144 |
sys.exit(1) |
| 145 |
|
| 146 |
wraprint("We found an item holding {} bytes of checksums, starting at vaddr {}. " |
| 147 |
"This means it contains {} {}-byte checksums covering a range up to but not including " |
| 148 |
"vaddr {}.".format(header.len, csum_covers_vaddr_start, |
| 149 |
nr_csums, CSUM_SIZE, csum_covers_vaddr_end)) |
| 150 |
|
| 151 |
index_in_csum_data = (vaddr - header.offset) // fs.sectorsize |
| 152 |
offset_in_csum_data = index_in_csum_data * CSUM_SIZE |
| 153 |
stored_csum, = csum_struct.unpack_from(data, offset_in_csum_data) |
| 154 |
|
| 155 |
wraprint("The checksum for our example data block is item nr {} at byte position {}. The checksum " |
| 156 |
"value is {}".format(index_in_csum_data, offset_in_csum_data, stored_csum)) |
| 157 |
|
| 158 |
with open(filename, 'rb') as f: |
| 159 |
f.seek(extent.logical_offset) |
| 160 |
computed_csum = csum_function(f.read(fs.sectorsize)) |
| 161 |
|
| 162 |
wraprint("The checksum computed from reading {} bytes from the file " |
| 163 |
"at position {} is: {}".format(fs.sectorsize, extent.logical_offset, computed_csum)) |
| 164 |
|
| 165 |
print("""------------------------------------- 8< -------------------------------------- |
| 166 |
If the above checksum values match, then Yay. |
| 167 |
|
| 168 |
If not, then either it means this example program has a bug, or it means you've |
| 169 |
hit one of the many possible small and large race conditions involved here if |
| 170 |
you're trying this on an actively used filesystem. |
| 171 |
------------------------------------- 8< -------------------------------------- |
| 172 |
""") |