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