file_direct.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Backported from 4.5
  21. #define lru_to_page(head) (list_entry((head)->prev, struct page, lru))
  22. static void release_actor_pages(struct page **page, int pages, int error)
  23. {
  24. int i;
  25. for (i = 0; i < pages; i++) {
  26. if (!page[i])
  27. continue;
  28. flush_dcache_page(page[i]);
  29. if (!error)
  30. SetPageUptodate(page[i]);
  31. else {
  32. SetPageError(page[i]);
  33. zero_user_segment(page[i], 0, PAGE_CACHE_SIZE);
  34. }
  35. unlock_page(page[i]);
  36. put_page(page[i]);
  37. }
  38. kfree(page);
  39. }
  40. /*
  41. * Create a "page actor" which will kmap and kunmap the
  42. * page cache pages appropriately within the decompressor
  43. */
  44. static struct squashfs_page_actor *actor_from_page_cache(
  45. unsigned int actor_pages, struct page *target_page,
  46. struct list_head *rpages, unsigned int *nr_pages, int start_index,
  47. struct address_space *mapping)
  48. {
  49. struct page **page;
  50. struct squashfs_page_actor *actor;
  51. int i, n;
  52. gfp_t gfp = GFP_KERNEL;
  53. page = kmalloc_array(actor_pages, sizeof(void *), GFP_KERNEL);
  54. if (!page)
  55. return NULL;
  56. for (i = 0, n = start_index; i < actor_pages; i++, n++) {
  57. if (target_page == NULL && rpages && !list_empty(rpages)) {
  58. struct page *cur_page = lru_to_page(rpages);
  59. if (cur_page->index < start_index + actor_pages) {
  60. list_del(&cur_page->lru);
  61. --(*nr_pages);
  62. if (add_to_page_cache_lru(cur_page, mapping,
  63. cur_page->index, gfp))
  64. put_page(cur_page);
  65. else
  66. target_page = cur_page;
  67. } else
  68. rpages = NULL;
  69. }
  70. if (target_page && target_page->index == n) {
  71. page[i] = target_page;
  72. target_page = NULL;
  73. } else {
  74. page[i] = grab_cache_page_nowait(mapping, n);
  75. if (page[i] == NULL)
  76. continue;
  77. }
  78. if (PageUptodate(page[i])) {
  79. unlock_page(page[i]);
  80. page_cache_release(page[i]);
  81. page[i] = NULL;
  82. }
  83. }
  84. actor = squashfs_page_actor_init(page, actor_pages, 0,
  85. release_actor_pages);
  86. if (!actor) {
  87. release_actor_pages(page, actor_pages, -ENOMEM);
  88. kfree(page);
  89. return NULL;
  90. }
  91. return actor;
  92. }
  93. int squashfs_readpages_block(struct page *target_page,
  94. struct list_head *readahead_pages,
  95. unsigned int *nr_pages,
  96. struct address_space *mapping,
  97. int page_index, u64 block, int bsize)
  98. {
  99. struct squashfs_page_actor *actor;
  100. struct inode *inode = mapping->host;
  101. struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info;
  102. int start_index, end_index, file_end, actor_pages, res;
  103. int mask = (1 << (msblk->block_log - PAGE_CACHE_SHIFT)) - 1;
  104. /*
  105. * If readpage() is called on an uncompressed datablock, we can just
  106. * read the pages instead of fetching the whole block.
  107. * This greatly improves the performance when a process keep doing
  108. * random reads because we only fetch the necessary data.
  109. * The readahead algorithm will take care of doing speculative reads
  110. * if necessary.
  111. * We can't read more than 1 block even if readahead provides use more
  112. * pages because we don't know yet if the next block is compressed or
  113. * not.
  114. */
  115. if (bsize && !SQUASHFS_COMPRESSED_BLOCK(bsize)) {
  116. u64 block_end = block + msblk->block_size;
  117. block += (page_index & mask) * PAGE_SIZE;
  118. actor_pages = (block_end - block) / PAGE_SIZE;
  119. if (*nr_pages < actor_pages)
  120. actor_pages = *nr_pages;
  121. start_index = page_index;
  122. bsize = min_t(int, bsize, (PAGE_SIZE * actor_pages)
  123. | SQUASHFS_COMPRESSED_BIT_BLOCK);
  124. } else {
  125. file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  126. start_index = page_index & ~mask;
  127. end_index = start_index | mask;
  128. if (end_index > file_end)
  129. end_index = file_end;
  130. actor_pages = end_index - start_index + 1;
  131. }
  132. actor = actor_from_page_cache(actor_pages, target_page,
  133. readahead_pages, nr_pages, start_index,
  134. mapping);
  135. if (!actor)
  136. return -ENOMEM;
  137. res = squashfs_read_data_async(inode->i_sb, block, bsize, NULL,
  138. actor);
  139. return res < 0 ? res : 0;
  140. }