file_direct.c 4.4 KB

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