On Thu, Apr 03, 2025 at 06:44:22PM +0100, Matthew Wilcox wrote: > > On the other hand, I'm having some internal code to convert a bio_vec > > into a folio and offset inside the folio already. > > Thus I'm wondering can we provide something like bio_for_each_folio()? > > Or is it too niche that only certain fs can benefit from? > > I don't understand your requirements. but doing something different that > fills in a folio_iter along the lines of bio_for_each_folio_all() > would make sense. > Looking at btrfs_decompress_buf2page it uses bio_iter_iovec, which is the building block use by bio_for_each_segment to build segments, that is bvecs that are constrained to PAGE_SIZE boundaries. In this for a good reason as it then wants to copy into them with a single kmap mapping. This means that bv_offset is always less than PAGE_SIZE for these generated bio_vecs. In the short run I'd suggest to just keep using the existing open-coded bio_for_each_segment-like loop so that each iteration covers a PAGE_SIZE granularity chunk. Just stop messing th the bvec fields directly and use bvec_kmap_local to map the destination and do a plain memcpy into that instead of using memcpy_to_page. In the long run it would be great to just rewrite the low-level decompressors to work on an iov_iter as output and remove the need for the extra data copy entirely, but if that can't happen we'll just need for figure out how we can do useful kmaps larger than PAGE_SIZE or stop allowing highmem pages for btrfs and then switch to a folio based iterator.