file_direct.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
  19. int pages, struct page **page, int bytes);
  20. /* Read separately compressed datablock directly into page cache */
  21. int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
  22. int expected)
  23. {
  24. struct inode *inode = target_page->mapping->host;
  25. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  26. int file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  27. int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
  28. int start_index = target_page->index & ~mask;
  29. int end_index = start_index | mask;
  30. int i, n, pages, missing_pages, bytes, res = -ENOMEM;
  31. struct page **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. /*
  41. * Create a "page actor" which will kmap and kunmap the
  42. * page cache pages appropriately within the decompressor
  43. */
  44. actor = squashfs_page_actor_init_special(page, pages, 0);
  45. if (actor == NULL)
  46. goto out;
  47. /* Try to grab all the pages covered by the Squashfs block */
  48. for (missing_pages = 0, i = 0, n = start_index; i < pages; i++, n++) {
  49. page[i] = (n == target_page->index) ? target_page :
  50. grab_cache_page_nowait(target_page->mapping, n);
  51. if (page[i] == NULL) {
  52. missing_pages++;
  53. continue;
  54. }
  55. if (PageUptodate(page[i])) {
  56. unlock_page(page[i]);
  57. put_page(page[i]);
  58. page[i] = NULL;
  59. missing_pages++;
  60. }
  61. }
  62. if (missing_pages) {
  63. /*
  64. * Couldn't get one or more pages, this page has either
  65. * been VM reclaimed, but others are still in the page cache
  66. * and uptodate, or we're racing with another thread in
  67. * squashfs_readpage also trying to grab them. Fall back to
  68. * using an intermediate buffer.
  69. */
  70. res = squashfs_read_cache(target_page, block, bsize, pages,
  71. page, expected);
  72. if (res < 0)
  73. goto mark_errored;
  74. goto out;
  75. }
  76. /* Decompress directly into the page cache buffers */
  77. res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
  78. if (res < 0)
  79. goto mark_errored;
  80. if (res != expected) {
  81. res = -EIO;
  82. goto mark_errored;
  83. }
  84. /* Last page may have trailing bytes not filled */
  85. bytes = res % PAGE_SIZE;
  86. if (bytes) {
  87. pageaddr = kmap_atomic(page[pages - 1]);
  88. memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
  89. kunmap_atomic(pageaddr);
  90. }
  91. /* Mark pages as uptodate, unlock and release */
  92. for (i = 0; i < pages; i++) {
  93. flush_dcache_page(page[i]);
  94. SetPageUptodate(page[i]);
  95. unlock_page(page[i]);
  96. if (page[i] != target_page)
  97. put_page(page[i]);
  98. }
  99. kfree(actor);
  100. kfree(page);
  101. return 0;
  102. mark_errored:
  103. /* Decompression failed, mark pages as errored. Target_page is
  104. * dealt with by the caller
  105. */
  106. for (i = 0; i < pages; i++) {
  107. if (page[i] == NULL || page[i] == target_page)
  108. continue;
  109. flush_dcache_page(page[i]);
  110. SetPageError(page[i]);
  111. unlock_page(page[i]);
  112. put_page(page[i]);
  113. }
  114. out:
  115. kfree(actor);
  116. kfree(page);
  117. return res;
  118. }
  119. static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
  120. int pages, struct page **page, int bytes)
  121. {
  122. struct inode *i = target_page->mapping->host;
  123. struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb,
  124. block, bsize);
  125. int res = buffer->error, n, offset = 0;
  126. if (res) {
  127. ERROR("Unable to read page, block %llx, size %x\n", block,
  128. bsize);
  129. goto out;
  130. }
  131. for (n = 0; n < pages && bytes > 0; n++,
  132. bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
  133. int avail = min_t(int, bytes, PAGE_SIZE);
  134. if (page[n] == NULL)
  135. continue;
  136. squashfs_fill_page(page[n], buffer, offset, avail);
  137. unlock_page(page[n]);
  138. if (page[n] != target_page)
  139. put_page(page[n]);
  140. }
  141. out:
  142. squashfs_cache_put(buffer);
  143. return res;
  144. }