Skip to content

Commit dafcfa1

Browse files
adam900710kdave
authored andcommitted
btrfs: get rid of compressed_folios[] usage for compressed read
Currently btrfs_submit_compressed_read() still uses compressed_bio::compressed_folios[] array. Change it to allocate each folio and queue them into the compressed bio so that we do not need to allocate that array. Considering how small each compressed read bio is (less than 128KiB), we do not benefit that much from btrfs_alloc_folio_array() anyway, while we may benefit more from btrfs_alloc_compr_folio() by using the global folio pool. So changing from btrfs_alloc_folio_array() to btrfs_alloc_compr_folio() in a loop should still be fine. This removes one error path, and paves the way to completely remove compressed_folios[] array. Reviewed-by: Boris Burkov <boris@bur.io> Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 26902be commit dafcfa1

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

fs/btrfs/compression.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static void end_bbio_compressed_read(struct btrfs_bio *bbio)
239239

240240
btrfs_bio_end_io(cb->orig_bbio, status);
241241
bio_for_each_folio_all(fi, &bbio->bio)
242-
folio_put(fi.folio);
242+
btrfs_free_compr_folio(fi.folio);
243243
bio_put(&bbio->bio);
244244
}
245245

@@ -537,21 +537,21 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
537537
struct extent_map_tree *em_tree = &inode->extent_tree;
538538
struct compressed_bio *cb;
539539
unsigned int compressed_len;
540+
const u32 min_folio_size = btrfs_min_folio_size(fs_info);
540541
u64 file_offset = bbio->file_offset;
541542
u64 em_len;
542543
u64 em_start;
543544
struct extent_map *em;
544545
unsigned long pflags;
545546
int memstall = 0;
546-
blk_status_t status;
547547
int ret;
548548

549549
/* we need the actual starting offset of this extent in the file */
550550
read_lock(&em_tree->lock);
551551
em = btrfs_lookup_extent_mapping(em_tree, file_offset, fs_info->sectorsize);
552552
read_unlock(&em_tree->lock);
553553
if (!em) {
554-
status = BLK_STS_IOERR;
554+
ret = -EIO;
555555
goto out;
556556
}
557557

@@ -573,40 +573,41 @@ void btrfs_submit_compressed_read(struct btrfs_bio *bbio)
573573

574574
btrfs_free_extent_map(em);
575575

576-
cb->nr_folios = DIV_ROUND_UP(compressed_len, btrfs_min_folio_size(fs_info));
577-
cb->compressed_folios = kcalloc(cb->nr_folios, sizeof(struct folio *), GFP_NOFS);
578-
if (!cb->compressed_folios) {
579-
status = BLK_STS_RESOURCE;
580-
goto out_free_bio;
581-
}
576+
for (int i = 0; i * min_folio_size < compressed_len; i++) {
577+
struct folio *folio;
578+
u32 cur_len = min(compressed_len - i * min_folio_size, min_folio_size);
582579

583-
ret = btrfs_alloc_folio_array(cb->nr_folios, fs_info->block_min_order,
584-
cb->compressed_folios);
585-
if (ret) {
586-
status = BLK_STS_RESOURCE;
587-
goto out_free_compressed_pages;
580+
folio = btrfs_alloc_compr_folio(fs_info);
581+
if (!folio) {
582+
ret = -ENOMEM;
583+
goto out_free_bio;
584+
}
585+
586+
ret = bio_add_folio(&cb->bbio.bio, folio, cur_len, 0);
587+
if (unlikely(!ret)) {
588+
folio_put(folio);
589+
ret = -EINVAL;
590+
goto out_free_bio;
591+
}
588592
}
593+
ASSERT(cb->bbio.bio.bi_iter.bi_size == compressed_len);
589594

590595
add_ra_bio_pages(&inode->vfs_inode, em_start + em_len, cb, &memstall,
591596
&pflags);
592597

593-
/* include any pages we added in add_ra-bio_pages */
594598
cb->len = bbio->bio.bi_iter.bi_size;
595599
cb->bbio.bio.bi_iter.bi_sector = bbio->bio.bi_iter.bi_sector;
596-
btrfs_add_compressed_bio_folios(cb);
597600

598601
if (memstall)
599602
psi_memstall_leave(&pflags);
600603

601604
btrfs_submit_bbio(&cb->bbio, 0);
602605
return;
603606

604-
out_free_compressed_pages:
605-
kfree(cb->compressed_folios);
606607
out_free_bio:
607-
bio_put(&cb->bbio.bio);
608+
cleanup_compressed_bio(cb);
608609
out:
609-
btrfs_bio_end_io(bbio, status);
610+
btrfs_bio_end_io(bbio, errno_to_blk_status(ret));
610611
}
611612

612613
/*

0 commit comments

Comments
 (0)