1 // SPDX-License-Identifier: GPL-2.0-only
4 * Phillip Lougher <phillip@squashfs.org.uk>
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>
15 #include "squashfs_fs.h"
16 #include "squashfs_fs_sb.h"
17 #include "squashfs_fs_i.h"
19 #include "page_actor.h"
21 static int squashfs_read_cache(struct page
*target_page
, u64 block
, int bsize
,
22 int pages
, struct page
**page
, int bytes
);
24 /* Read separately compressed datablock directly into page cache */
25 int squashfs_readpage_block(struct page
*target_page
, u64 block
, int bsize
,
29 struct inode
*inode
= target_page
->mapping
->host
;
30 struct squashfs_sb_info
*msblk
= inode
->i_sb
->s_fs_info
;
32 int file_end
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
33 int mask
= (1 << (msblk
->block_log
- PAGE_SHIFT
)) - 1;
34 int start_index
= target_page
->index
& ~mask
;
35 int end_index
= start_index
| mask
;
36 int i
, n
, pages
, missing_pages
, bytes
, res
= -ENOMEM
;
38 struct squashfs_page_actor
*actor
;
41 if (end_index
> file_end
)
44 pages
= end_index
- start_index
+ 1;
46 page
= kmalloc_array(pages
, sizeof(void *), GFP_KERNEL
);
51 * Create a "page actor" which will kmap and kunmap the
52 * page cache pages appropriately within the decompressor
54 actor
= squashfs_page_actor_init_special(page
, pages
, 0);
58 /* Try to grab all the pages covered by the Squashfs block */
59 for (missing_pages
= 0, i
= 0, n
= start_index
; i
< pages
; i
++, n
++) {
60 page
[i
] = (n
== target_page
->index
) ? target_page
:
61 grab_cache_page_nowait(target_page
->mapping
, n
);
63 if (page
[i
] == NULL
) {
68 if (PageUptodate(page
[i
])) {
78 * Couldn't get one or more pages, this page has either
79 * been VM reclaimed, but others are still in the page cache
80 * and uptodate, or we're racing with another thread in
81 * squashfs_readpage also trying to grab them. Fall back to
82 * using an intermediate buffer.
84 res
= squashfs_read_cache(target_page
, block
, bsize
, pages
,
92 /* Decompress directly into the page cache buffers */
93 res
= squashfs_read_data(inode
->i_sb
, block
, bsize
, NULL
, actor
);
97 if (res
!= expected
) {
102 /* Last page may have trailing bytes not filled */
103 bytes
= res
% PAGE_SIZE
;
105 pageaddr
= kmap_atomic(page
[pages
- 1]);
106 memset(pageaddr
+ bytes
, 0, PAGE_SIZE
- bytes
);
107 kunmap_atomic(pageaddr
);
110 /* Mark pages as uptodate, unlock and release */
111 for (i
= 0; i
< pages
; i
++) {
112 flush_dcache_page(page
[i
]);
113 SetPageUptodate(page
[i
]);
114 unlock_page(page
[i
]);
115 if (page
[i
] != target_page
)
125 /* Decompression failed, mark pages as errored. Target_page is
126 * dealt with by the caller
128 for (i
= 0; i
< pages
; i
++) {
129 if (page
[i
] == NULL
|| page
[i
] == target_page
)
131 flush_dcache_page(page
[i
]);
132 SetPageError(page
[i
]);
133 unlock_page(page
[i
]);
144 static int squashfs_read_cache(struct page
*target_page
, u64 block
, int bsize
,
145 int pages
, struct page
**page
, int bytes
)
147 struct inode
*i
= target_page
->mapping
->host
;
148 struct squashfs_cache_entry
*buffer
= squashfs_get_datablock(i
->i_sb
,
150 int res
= buffer
->error
, n
, offset
= 0;
153 ERROR("Unable to read page, block %llx, size %x\n", block
,
158 for (n
= 0; n
< pages
&& bytes
> 0; n
++,
159 bytes
-= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
160 int avail
= min_t(int, bytes
, PAGE_SIZE
);
165 squashfs_fill_page(page
[n
], buffer
, offset
, avail
);
166 unlock_page(page
[n
]);
167 if (page
[n
] != target_page
)
172 squashfs_cache_put(buffer
);