| 1 |
|
| 2 |
|
| 3 |
import btrfs |
| 4 |
import errno |
| 5 |
import sys |
| 6 |
|
| 7 |
if len(sys.argv) < 3: |
| 8 |
print("Usage: {} <vaddr> <mountpoint>".format(sys.argv[0])) |
| 9 |
sys.exit(1) |
| 10 |
|
| 11 |
|
| 12 |
def using_v1(fd, vaddr): |
| 13 |
return btrfs.ioctl.logical_ino(fd, vaddr) |
| 14 |
|
| 15 |
|
| 16 |
def using_v2(fd, vaddr): |
| 17 |
inodes, bytes_missed = btrfs.ioctl.logical_ino_v2(fd, vaddr, ignore_offset=True) |
| 18 |
if bytes_missed > 0: |
| 19 |
inodes, bytes_missed = \ |
| 20 |
btrfs.ioctl.logical_ino_v2(fd, vaddr, bufsize=65536+bytes_missed, |
| 21 |
ignore_offset=True) |
| 22 |
return inodes, bytes_missed |
| 23 |
|
| 24 |
|
| 25 |
def find_out_about_v1_or_v2(fd, vaddr): |
| 26 |
global logical_ino_fn |
| 27 |
try: |
| 28 |
inodes, bytes_missed = using_v2(fd, vaddr) |
| 29 |
logical_ino_fn = using_v2 |
| 30 |
return inodes, bytes_missed |
| 31 |
except IOError as e: |
| 32 |
if e.errno == errno.ENOTTY: |
| 33 |
inodes, bytes_missed = using_v1(fd, vaddr) |
| 34 |
logical_ino_fn = using_v1 |
| 35 |
return inodes, bytes_missed |
| 36 |
raise |
| 37 |
|
| 38 |
|
| 39 |
vaddr = int(sys.argv[1]) |
| 40 |
path_cache = {} |
| 41 |
|
| 42 |
with btrfs.FileSystem(sys.argv[2]) as fs: |
| 43 |
block_group = fs.block_group(vaddr) |
| 44 |
print(block_group) |
| 45 |
|
| 46 |
logical_ino_fn = find_out_about_v1_or_v2 |
| 47 |
|
| 48 |
for extent in fs.extents(vaddr, vaddr + block_group.length - 1): |
| 49 |
if isinstance(extent, btrfs.ctree.ExtentItem) \ |
| 50 |
and extent.flags & btrfs.ctree.EXTENT_FLAG_DATA: |
| 51 |
print(extent) |
| 52 |
inodes, bytes_missed = logical_ino_fn(fs.fd, extent.vaddr) |
| 53 |
for inode in inodes: |
| 54 |
if inode.root == btrfs.ctree.FS_TREE_OBJECTID or \ |
| 55 |
(inode.root >= btrfs.ctree.FIRST_FREE_OBJECTID and |
| 56 |
inode.root <= btrfs.ctree.LAST_FREE_OBJECTID): |
| 57 |
cache_key = (inode.root, inode.inum) |
| 58 |
if cache_key not in path_cache: |
| 59 |
try: |
| 60 |
path = btrfs.ioctl.ino_lookup(fs.fd, treeid=inode.root, |
| 61 |
objectid=inode.inum)[1][:-1] |
| 62 |
path_cache[cache_key] = btrfs.utils.embedded_text_for_str(path) |
| 63 |
except IOError as e: |
| 64 |
if e.errno == errno.ENOENT: |
| 65 |
path_cache[cache_key] = None |
| 66 |
else: |
| 67 |
raise |
| 68 |
print(" root {} inode {} offset {} path {}".format( |
| 69 |
inode.root, inode.inum, inode.offset, path_cache[cache_key])) |
| 70 |
else: |
| 71 |
print(" root {} inode {} offset {}".format( |
| 72 |
inode.root, inode.inum, inode.offset)) |
| 73 |
if len(inodes) == 0 and logical_ino_fn == using_v1: |
| 74 |
print(" [... no result and no kernel support for LOGICAL_INO_V2]") |
| 75 |
if bytes_missed > 0: |
| 76 |
if logical_ino_fn == using_v1: |
| 77 |
print(" [... remainder can't be shown; no kernel support for LOGICAL_INO_V2]") |
| 78 |
else: |
| 79 |
print(" [... remainder can't be shown; too many items for buffer size]") |