Skip to content

Commit b57046e

Browse files
committed
ctree: FileSystem: add block_groups function
In the past, I did not really want to add this helper function, since it's not just translating some function parameters to another function call, but, to get all Block Group objects, we needed to search the Chunk tree and get all of them individually. So, to make it more explicit to the user of the library that it was a bit weird inefficient process, I let the user do that little dance. Now, with the new Block Group Tree, we can actually just to a cheap lookup of a Block Group range! So, well, let's add the convenience function now, and let it handle both the old and new case. Note that the difference in behaviour between error handling for looking up a range or a single items stays the same. block_groups(...) will return an iterator which has no objects to produce, and block_group(...) will throw the ItemNotFoundError.
1 parent 79c72e7 commit b57046e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

btrfs/ctree.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,31 @@ def dev_extents(self, min_devid=1, max_devid=ULLONG_MAX):
861861
for header, data in btrfs.ioctl.search_v2(self.fd, tree, min_key, max_key):
862862
yield DevExtent(header, data)
863863

864+
def block_groups(self, min_vaddr=0, max_vaddr=ULLONG_MAX, nr_items=None):
865+
"""
866+
:param int min_vaddr: Lowest virtual address to search for.
867+
:param int max_vaddr: Highest virtual address to search for.
868+
:param int nr_items: Maximum amount of items to return. Defaults to no limit.
869+
:returns: Block Group items from the Extent Tree or Block Group Tree
870+
:rtype: Iterator[:class:`~btrfs.ctree.BlockGroupItem`]
871+
"""
872+
if self.features().compat_ro_flags & btrfs.ioctl.FEATURE_COMPAT_RO_BLOCK_GROUP_TREE == 0:
873+
for chunk in self.chunks(min_vaddr, max_vaddr, nr_items):
874+
try:
875+
yield self.block_group(chunk.vaddr, chunk.length)
876+
except btrfs.ctree.ItemNotFoundError:
877+
# This is simply to prevent the program from aborting when a block
878+
# group is removed in between doing the chunks lookup and the block
879+
# group item lookup.
880+
pass
881+
else:
882+
tree = BLOCK_GROUP_TREE_OBJECTID
883+
min_key = Key(min_vaddr, BLOCK_GROUP_ITEM_KEY, 0)
884+
max_key = Key(max_vaddr, BLOCK_GROUP_ITEM_KEY, ULLONG_MAX)
885+
for header, data in btrfs.ioctl.search_v2(self.fd, tree, min_key, max_key,
886+
nr_items=nr_items):
887+
yield BlockGroupItem(header, data)
888+
864889
def block_group(self, vaddr, length=None):
865890
"""
866891
:param int vaddr: Starting virtual address of the block group.

0 commit comments

Comments
 (0)