Commit | Line | Data |
---|---|---|
20c8ccb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
0d455c12 PL |
2 | /* |
3 | * Copyright (c) 2013 | |
4 | * Phillip Lougher <phillip@squashfs.org.uk> | |
0d455c12 PL |
5 | */ |
6 | ||
7 | #include <linux/fs.h> | |
8 | #include <linux/vfs.h> | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/string.h> | |
12 | #include <linux/pagemap.h> | |
13 | #include <linux/mutex.h> | |
14 | ||
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 | ||
0d455c12 | 21 | /* Read separately compressed datablock directly into page cache */ |
a3f94cb9 PL |
22 | int squashfs_readpage_block(struct page *target_page, u64 block, int bsize, |
23 | int expected) | |
0d455c12 PL |
24 | |
25 | { | |
7f73fcde | 26 | struct folio *folio = page_folio(target_page); |
0d455c12 PL |
27 | struct inode *inode = target_page->mapping->host; |
28 | struct squashfs_sb_info *msblk = inode->i_sb->s_fs_info; | |
12427de9 | 29 | loff_t file_end = (i_size_read(inode) - 1) >> PAGE_SHIFT; |
09cbfeaf | 30 | int mask = (1 << (msblk->block_log - PAGE_SHIFT)) - 1; |
7f73fcde | 31 | loff_t start_index = folio->index & ~mask; |
12427de9 | 32 | loff_t end_index = start_index | mask; |
1bb1a07a | 33 | int i, n, pages, bytes, res = -ENOMEM; |
7f73fcde | 34 | struct page **page, *last_page; |
0d455c12 PL |
35 | struct squashfs_page_actor *actor; |
36 | void *pageaddr; | |
37 | ||
38 | if (end_index > file_end) | |
39 | end_index = file_end; | |
40 | ||
41 | pages = end_index - start_index + 1; | |
42 | ||
14694888 | 43 | page = kmalloc_array(pages, sizeof(void *), GFP_KERNEL); |
0d455c12 PL |
44 | if (page == NULL) |
45 | return res; | |
46 | ||
0d455c12 | 47 | /* Try to grab all the pages covered by the Squashfs block */ |
1bb1a07a | 48 | for (i = 0, n = start_index; n <= end_index; n++) { |
7f73fcde | 49 | page[i] = (n == folio->index) ? target_page : |
0d455c12 PL |
50 | grab_cache_page_nowait(target_page->mapping, n); |
51 | ||
1bb1a07a | 52 | if (page[i] == NULL) |
0d455c12 | 53 | continue; |
0d455c12 PL |
54 | |
55 | if (PageUptodate(page[i])) { | |
56 | unlock_page(page[i]); | |
09cbfeaf | 57 | put_page(page[i]); |
1bb1a07a | 58 | continue; |
0d455c12 | 59 | } |
6d565409 | 60 | |
1bb1a07a | 61 | i++; |
0d455c12 PL |
62 | } |
63 | ||
1bb1a07a PL |
64 | pages = i; |
65 | ||
f268eedd PL |
66 | /* |
67 | * Create a "page actor" which will kmap and kunmap the | |
68 | * page cache pages appropriately within the decompressor | |
69 | */ | |
2258e22f PL |
70 | actor = squashfs_page_actor_init_special(msblk, page, pages, expected, |
71 | start_index << PAGE_SHIFT); | |
f268eedd PL |
72 | if (actor == NULL) |
73 | goto out; | |
74 | ||
0d455c12 PL |
75 | /* Decompress directly into the page cache buffers */ |
76 | res = squashfs_read_data(inode->i_sb, block, bsize, NULL, actor); | |
f268eedd | 77 | |
7f73fcde | 78 | last_page = squashfs_page_actor_free(actor); |
f268eedd | 79 | |
0d455c12 PL |
80 | if (res < 0) |
81 | goto mark_errored; | |
82 | ||
84e0e03b | 83 | if (res != expected || IS_ERR(last_page)) { |
a3f94cb9 PL |
84 | res = -EIO; |
85 | goto mark_errored; | |
86 | } | |
87 | ||
1bb1a07a | 88 | /* Last page (if present) may have trailing bytes not filled */ |
09cbfeaf | 89 | bytes = res % PAGE_SIZE; |
7f73fcde PL |
90 | if (end_index == file_end && last_page && bytes) { |
91 | pageaddr = kmap_local_page(last_page); | |
09cbfeaf | 92 | memset(pageaddr + bytes, 0, PAGE_SIZE - bytes); |
1bb1a07a | 93 | kunmap_local(pageaddr); |
0d455c12 PL |
94 | } |
95 | ||
96 | /* Mark pages as uptodate, unlock and release */ | |
97 | for (i = 0; i < pages; i++) { | |
98 | flush_dcache_page(page[i]); | |
99 | SetPageUptodate(page[i]); | |
100 | unlock_page(page[i]); | |
101 | if (page[i] != target_page) | |
09cbfeaf | 102 | put_page(page[i]); |
0d455c12 PL |
103 | } |
104 | ||
0d455c12 PL |
105 | kfree(page); |
106 | ||
107 | return 0; | |
108 | ||
109 | mark_errored: | |
bbf45b7e | 110 | /* Decompression failed. Target_page is |
0d455c12 PL |
111 | * dealt with by the caller |
112 | */ | |
113 | for (i = 0; i < pages; i++) { | |
6d565409 | 114 | if (page[i] == NULL || page[i] == target_page) |
0d455c12 PL |
115 | continue; |
116 | flush_dcache_page(page[i]); | |
0d455c12 | 117 | unlock_page(page[i]); |
09cbfeaf | 118 | put_page(page[i]); |
0d455c12 PL |
119 | } |
120 | ||
121 | out: | |
0d455c12 PL |
122 | kfree(page); |
123 | return res; | |
124 | } |