file_direct.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013
  4. * Phillip Lougher <phillip@squashfs.org.uk>
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/vfs.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/string.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/mutex.h>
  13. #include "squashfs_fs.h"
  14. #include "squashfs_fs_sb.h"
  15. #include "squashfs_fs_i.h"
  16. #include "squashfs.h"
  17. #include "page_actor.h"
  18. /* Read separately compressed datablock directly into page cache */
  19. int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
  20. int expected)
  21. {
  22. struct folio *folio = page_folio(target_page);
  23. struct inode *inode = target_page->mapping->host;
  24. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  25. loff_t file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  26. int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
  27. loff_t start_index = folio->index & ~mask;
  28. loff_t end_index = start_index | mask;
  29. loff_t index;
  30. int i, pages, bytes, res = -ENOMEM;
  31. struct page **page, *last_page;
  32. struct squashfs_page_actor *actor;
  33. void *pageaddr;
  34. if (end_index > file_end)
  35. end_index = file_end;
  36. pages = end_index - start_index + 1;
  37. page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
  38. if (page == NULL)
  39. return res;
  40. /* Try to grab all the pages covered by the Squashfs block */
  41. for (i = 0, index = start_index; index <= end_index; index++) {
  42. page[i] = (index == folio->index) ? target_page :
  43. grab_cache_page_nowait(target_page->mapping, index);
  44. if (page[i] == NULL)
  45. continue;
  46. if (PageUptodate(page[i])) {
  47. unlock_page(page[i]);
  48. put_page(page[i]);
  49. continue;
  50. }
  51. i++;
  52. }
  53. pages = i;
  54. /*
  55. * Create a "page actor" which will kmap and kunmap the
  56. * page cache pages appropriately within the decompressor
  57. */
  58. actor = squashfs_page_actor_init_special(msblk, page, pages, expected,
  59. start_index << PAGE_SHIFT);
  60. if (actor == NULL)
  61. goto out;
  62. /* Decompress directly into the page cache buffers */
  63. res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
  64. last_page = squashfs_page_actor_free(actor);
  65. if (res < 0)
  66. goto mark_errored;
  67. if (res != expected || IS_ERR(last_page)) {
  68. res = -EIO;
  69. goto mark_errored;
  70. }
  71. /* Last page (if present) may have trailing bytes not filled */
  72. bytes = res % PAGE_SIZE;
  73. if (end_index == file_end && last_page && bytes) {
  74. pageaddr = kmap_local_page(last_page);
  75. memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
  76. kunmap_local(pageaddr);
  77. }
  78. /* Mark pages as uptodate, unlock and release */
  79. for (i = 0; i < pages; i++) {
  80. flush_dcache_page(page[i]);
  81. SetPageUptodate(page[i]);
  82. unlock_page(page[i]);
  83. if (page[i] != target_page)
  84. put_page(page[i]);
  85. }
  86. kfree(page);
  87. return 0;
  88. mark_errored:
  89. /* Decompression failed. Target_page is
  90. * dealt with by the caller
  91. */
  92. for (i = 0; i < pages; i++) {
  93. if (page[i] == NULL || page[i] == target_page)
  94. continue;
  95. flush_dcache_page(page[i]);
  96. unlock_page(page[i]);
  97. put_page(page[i]);
  98. }
  99. out:
  100. kfree(page);
  101. return res;
  102. }