Skip to content

Commit 06ed093

Browse files
Matthew Wilcox (Oracle)kdave
authored andcommitted
btrfs: convert btrfs_read_merkle_tree_page() to use a folio
Remove a number of hidden calls to compound_head() by using a folio throughout. Also follow core kernel coding style by adding the folio to the page cache immediately after allocation instead of doing the read first, then adding it to the page cache. This ordering makes subsequent readers block waiting for the first reader instead of duplicating the work only to throw it away when they find out they lost the race. Reviewed-by: Boris Burkov <boris@bur.io> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 5facccc commit 06ed093

1 file changed

Lines changed: 31 additions & 33 deletions

File tree

fs/btrfs/verity.c

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
715715
pgoff_t index,
716716
unsigned long num_ra_pages)
717717
{
718-
struct page *page;
718+
struct folio *folio;
719719
u64 off = (u64)index << PAGE_SHIFT;
720720
loff_t merkle_pos = merkle_file_pos(inode);
721721
int ret;
@@ -726,58 +726,56 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
726726
return ERR_PTR(-EFBIG);
727727
index += merkle_pos >> PAGE_SHIFT;
728728
again:
729-
page = find_get_page_flags(inode->i_mapping, index, FGP_ACCESSED);
730-
if (page) {
731-
if (PageUptodate(page))
732-
return page;
729+
folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0);
730+
if (!IS_ERR(folio)) {
731+
if (folio_test_uptodate(folio))
732+
goto out;
733733

734-
lock_page(page);
735-
/*
736-
* We only insert uptodate pages, so !Uptodate has to be
737-
* an error
738-
*/
739-
if (!PageUptodate(page)) {
740-
unlock_page(page);
741-
put_page(page);
734+
folio_lock(folio);
735+
/* If it's not uptodate after we have the lock, we got a read error. */
736+
if (!folio_test_uptodate(folio)) {
737+
folio_unlock(folio);
738+
folio_put(folio);
742739
return ERR_PTR(-EIO);
743740
}
744-
unlock_page(page);
745-
return page;
741+
folio_unlock(folio);
742+
goto out;
746743
}
747744

748-
page = __page_cache_alloc(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS));
749-
if (!page)
745+
folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
746+
0);
747+
if (!folio)
750748
return ERR_PTR(-ENOMEM);
751749

750+
ret = filemap_add_folio(inode->i_mapping, folio, index, GFP_NOFS);
751+
if (ret) {
752+
folio_put(folio);
753+
/* Did someone else insert a folio here? */
754+
if (ret == -EEXIST)
755+
goto again;
756+
return ERR_PTR(ret);
757+
}
758+
752759
/*
753760
* Merkle item keys are indexed from byte 0 in the merkle tree.
754761
* They have the form:
755762
*
756763
* [ inode objectid, BTRFS_MERKLE_ITEM_KEY, offset in bytes ]
757764
*/
758765
ret = read_key_bytes(BTRFS_I(inode), BTRFS_VERITY_MERKLE_ITEM_KEY, off,
759-
page_address(page), PAGE_SIZE, page);
766+
folio_address(folio), PAGE_SIZE, &folio->page);
760767
if (ret < 0) {
761-
put_page(page);
768+
folio_put(folio);
762769
return ERR_PTR(ret);
763770
}
764771
if (ret < PAGE_SIZE)
765-
memzero_page(page, ret, PAGE_SIZE - ret);
772+
folio_zero_segment(folio, ret, PAGE_SIZE);
766773

767-
SetPageUptodate(page);
768-
ret = add_to_page_cache_lru(page, inode->i_mapping, index, GFP_NOFS);
774+
folio_mark_uptodate(folio);
775+
folio_unlock(folio);
769776

770-
if (!ret) {
771-
/* Inserted and ready for fsverity */
772-
unlock_page(page);
773-
} else {
774-
put_page(page);
775-
/* Did someone race us into inserting this page? */
776-
if (ret == -EEXIST)
777-
goto again;
778-
page = ERR_PTR(ret);
779-
}
780-
return page;
777+
out:
778+
return folio_file_page(folio, index);
781779
}
782780

783781
/*

0 commit comments

Comments
 (0)