file_direct.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2013
  3. * Phillip Lougher <phillip@squashfs.org.uk>
  4. *
  5. * This work is licensed under the terms of the GNU GPL, version 2. See
  6. * the COPYING file in the top-level directory.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/vfs.h>
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/string.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mutex.h>
  15. #include "squashfs_fs.h"
  16. #include "squashfs_fs_sb.h"
  17. #include "squashfs_fs_i.h"
  18. #include "squashfs.h"
  19. #include "page_actor.h"
  20. static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
  21. int pages, struct page **page, int bytes);
  22. /* Read separately compressed datablock directly into page cache */
  23. int squashfs_readpage_block(struct page *target_page, u64 block, int bsize,
  24. int expected)
  25. {
  26. struct inode *inode = target_page->mapping->host;
  27. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  28. int file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  29. int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1;
  30. int start_index = target_page->index & ~mask;
  31. int end_index = start_index | mask;
  32. int i, n, pages, missing_pages, bytes, res = -ENOMEM;
  33. struct page **page;
  34. struct squashfs_page_actor *actor;
  35. void *pageaddr;
  36. if (end_index > file_end)
  37. end_index = file_end;
  38. pages = end_index - start_index + 1;
  39. page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL);
  40. if (page == NULL)
  41. return res;
  42. /*
  43. * Create a "page actor" which will kmap and kunmap the
  44. * page cache pages appropriately within the decompressor
  45. */
  46. actor = squashfs_page_actor_init_special(page, pages, 0);
  47. if (actor == NULL)
  48. goto out;
  49. /* Try to grab all the pages covered by the Squashfs block */
  50. for (missing_pages = 0, i = 0, n = start_index; i < pages; i++, n++) {
  51. page[i] = (n == target_page->index) ? target_page :
  52. grab_cache_page_nowait(target_page->mapping, n);
  53. if (page[i] == NULL) {
  54. missing_pages++;
  55. continue;
  56. }
  57. if (PageUptodate(page[i])) {
  58. unlock_page(page[i]);
  59. put_page(page[i]);
  60. page[i] = NULL;
  61. missing_pages++;
  62. }
  63. }
  64. if (missing_pages) {
  65. /*
  66. * Couldn't get one or more pages, this page has either
  67. * been VM reclaimed, but others are still in the page cache
  68. * and uptodate, or we're racing with another thread in
  69. * squashfs_readpage also trying to grab them. Fall back to
  70. * using an intermediate buffer.
  71. */
  72. res = squashfs_read_cache(target_page, block, bsize, pages,
  73. page, expected);
  74. if (res < 0)
  75. goto mark_errored;
  76. goto out;
  77. }
  78. /* Decompress directly into the page cache buffers */
  79. res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor);
  80. if (res < 0)
  81. goto mark_errored;
  82. if (res != expected) {
  83. res = -EIO;
  84. goto mark_errored;
  85. }
  86. /* Last page may have trailing bytes not filled */
  87. bytes = res % PAGE_SIZE;
  88. if (bytes) {
  89. pageaddr = kmap_atomic(page[pages - 1]);
  90. memset(pageaddr + bytes, 0, PAGE_SIZE - bytes);
  91. kunmap_atomic(pageaddr);
  92. }
  93. /* Mark pages as uptodate, unlock and release */
  94. for (i = 0; i < pages; i++) {
  95. flush_dcache_page(page[i]);
  96. SetPageUptodate(page[i]);
  97. unlock_page(page[i]);
  98. if (page[i] != target_page)
  99. put_page(page[i]);
  100. }
  101. kfree(actor);
  102. kfree(page);
  103. return 0;
  104. mark_errored:
  105. /* Decompression failed, mark pages as errored. Target_page is
  106. * dealt with by the caller
  107. */
  108. for (i = 0; i < pages; i++) {
  109. if (page[i] == NULL || page[i] == target_page)
  110. continue;
  111. flush_dcache_page(page[i]);
  112. SetPageError(page[i]);
  113. unlock_page(page[i]);
  114. put_page(page[i]);
  115. }
  116. out:
  117. kfree(actor);
  118. kfree(page);
  119. return res;
  120. }
  121. static int squashfs_read_cache(struct page *target_page, u64 block, int bsize,
  122. int pages, struct page **page, int bytes)
  123. {
  124. struct inode *i = target_page->mapping->host;
  125. struct squashfs_cache_entry *buffer = squashfs_get_datablock(i->i_sb,
  126. block, bsize);
  127. int res = buffer->error, n, offset = 0;
  128. if (res) {
  129. ERROR("Unable to read page, block %llx, size %x\n", block,
  130. bsize);
  131. goto out;
  132. }
  133. for (n = 0; n < pages && bytes > 0; n++,
  134. bytes -= PAGE_SIZE, offset += PAGE_SIZE) {
  135. int avail = min_t(int, bytes, PAGE_SIZE);
  136. if (page[n] == NULL)
  137. continue;
  138. squashfs_fill_page(page[n], buffer, offset, avail);
  139. unlock_page(page[n]);
  140. if (page[n] != target_page)
  141. put_page(page[n]);
  142. }
  143. out:
  144. squashfs_cache_put(buffer);
  145. return res;
  146. }