pagecache.c 1.8 KB

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