xref: /linux/fs/verity/pagecache.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Google LLC
4  */
5 
6 #include <linux/export.h>
7 #include <linux/fsverity.h>
8 #include <linux/pagemap.h>
9 
10 /**
11  * generic_read_merkle_tree_page - generic ->read_merkle_tree_page helper
12  * @inode:	inode containing the Merkle tree
13  * @index:	0-based index of the Merkle tree page in the inode
14  *
15  * The caller needs to adjust @index from the Merkle-tree relative index passed
16  * to ->read_merkle_tree_page to the actual index where the Merkle tree is
17  * stored in the page cache for @inode.
18  */
19 struct page *generic_read_merkle_tree_page(struct inode *inode, pgoff_t index)
20 {
21 	struct folio *folio;
22 
23 	folio = read_mapping_folio(inode->i_mapping, index, NULL);
24 	if (IS_ERR(folio))
25 		return ERR_CAST(folio);
26 	return folio_file_page(folio, index);
27 }
28 EXPORT_SYMBOL_GPL(generic_read_merkle_tree_page);
29 
30 /**
31  * generic_readahead_merkle_tree() - generic ->readahead_merkle_tree helper
32  * @inode:	inode containing the Merkle tree
33  * @index:	0-based index of the first Merkle tree page to read ahead in the
34  *		inode
35  * @nr_pages:	the number of Merkle tree pages that should be read ahead
36  *
37  * The caller needs to adjust @index from the Merkle-tree relative index passed
38  * to ->read_merkle_tree_page to the actual index where the Merkle tree is
39  * stored in the page cache for @inode.
40  */
41 void generic_readahead_merkle_tree(struct inode *inode, pgoff_t index,
42 				   unsigned long nr_pages)
43 {
44 	struct folio *folio;
45 
46 	lockdep_assert_held(&inode->i_mapping->invalidate_lock);
47 
48 	folio = __filemap_get_folio(inode->i_mapping, index, FGP_ACCESSED, 0);
49 	if (folio == ERR_PTR(-ENOENT) ||
50 	    (!IS_ERR(folio) && !folio_test_uptodate(folio))) {
51 		DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, index);
52 
53 		page_cache_ra_unbounded(&ractl, nr_pages, 0);
54 	}
55 	if (!IS_ERR(folio))
56 		folio_put(folio);
57 }
58 EXPORT_SYMBOL_GPL(generic_readahead_merkle_tree);
59