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