1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Squashfs - a compressed read only filesystem for Linux
5 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
6 * Phillip Lougher <phillip@squashfs.org.uk>
12 * This file implements the low-level routines to read and decompress
13 * datablocks and metadata blocks.
17 #include <linux/vfs.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/buffer_head.h>
21 #include <linux/bio.h>
23 #include "squashfs_fs.h"
24 #include "squashfs_fs_sb.h"
26 #include "decompressor.h"
27 #include "page_actor.h"
30 * Read the metadata block length, this is stored in the first two
31 * bytes of the metadata block.
33 static struct buffer_head
*get_block_length(struct super_block
*sb
,
34 u64
*cur_index
, int *offset
, int *length
)
36 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
37 struct buffer_head
*bh
;
39 bh
= sb_bread(sb
, *cur_index
);
43 if (msblk
->devblksize
- *offset
== 1) {
44 *length
= (unsigned char) bh
->b_data
[*offset
];
46 bh
= sb_bread(sb
, ++(*cur_index
));
49 *length
|= (unsigned char) bh
->b_data
[0] << 8;
52 *length
= (unsigned char) bh
->b_data
[*offset
] |
53 (unsigned char) bh
->b_data
[*offset
+ 1] << 8;
56 if (*offset
== msblk
->devblksize
) {
58 bh
= sb_bread(sb
, ++(*cur_index
));
70 * Read and decompress a metadata block or datablock. Length is non-zero
71 * if a datablock is being read (the size is stored elsewhere in the
72 * filesystem), otherwise the length is obtained from the first two bytes of
73 * the metadata block. A bit in the length field indicates if the block
74 * is stored uncompressed in the filesystem (usually because compression
75 * generated a larger block - this does occasionally happen with compression
78 int squashfs_read_data(struct super_block
*sb
, u64 index
, int length
,
79 u64
*next_index
, struct squashfs_page_actor
*output
)
81 struct squashfs_sb_info
*msblk
= sb
->s_fs_info
;
82 struct buffer_head
**bh
;
83 int offset
= index
& ((1 << msblk
->devblksize_log2
) - 1);
84 u64 cur_index
= index
>> msblk
->devblksize_log2
;
85 int bytes
, compressed
, b
= 0, k
= 0, avail
, i
;
87 bh
= kcalloc(((output
->length
+ msblk
->devblksize
- 1)
88 >> msblk
->devblksize_log2
) + 1, sizeof(*bh
), GFP_KERNEL
);
97 compressed
= SQUASHFS_COMPRESSED_BLOCK(length
);
98 length
= SQUASHFS_COMPRESSED_SIZE_BLOCK(length
);
100 *next_index
= index
+ length
;
102 TRACE("Block @ 0x%llx, %scompressed size %d, src size %d\n",
103 index
, compressed
? "" : "un", length
, output
->length
);
105 if (length
< 0 || length
> output
->length
||
106 (index
+ length
) > msblk
->bytes_used
)
109 for (b
= 0; bytes
< length
; b
++, cur_index
++) {
110 bh
[b
] = sb_getblk(sb
, cur_index
);
113 bytes
+= msblk
->devblksize
;
115 ll_rw_block(REQ_OP_READ
, 0, b
, bh
);
120 if ((index
+ 2) > msblk
->bytes_used
)
123 bh
[0] = get_block_length(sb
, &cur_index
, &offset
, &length
);
128 bytes
= msblk
->devblksize
- offset
;
129 compressed
= SQUASHFS_COMPRESSED(length
);
130 length
= SQUASHFS_COMPRESSED_SIZE(length
);
132 *next_index
= index
+ length
+ 2;
134 TRACE("Block @ 0x%llx, %scompressed size %d\n", index
,
135 compressed
? "" : "un", length
);
137 if (length
< 0 || length
> output
->length
||
138 (index
+ length
) > msblk
->bytes_used
)
141 for (; bytes
< length
; b
++) {
142 bh
[b
] = sb_getblk(sb
, ++cur_index
);
145 bytes
+= msblk
->devblksize
;
147 ll_rw_block(REQ_OP_READ
, 0, b
- 1, bh
+ 1);
150 for (i
= 0; i
< b
; i
++) {
151 wait_on_buffer(bh
[i
]);
152 if (!buffer_uptodate(bh
[i
]))
159 length
= squashfs_decompress(msblk
, bh
, b
, offset
, length
,
165 * Block is uncompressed.
167 int in
, pg_offset
= 0;
168 void *data
= squashfs_first_page(output
);
170 for (bytes
= length
; k
< b
; k
++) {
171 in
= min(bytes
, msblk
->devblksize
- offset
);
174 if (pg_offset
== PAGE_SIZE
) {
175 data
= squashfs_next_page(output
);
178 avail
= min_t(int, in
, PAGE_SIZE
-
180 memcpy(data
+ pg_offset
, bh
[k
]->b_data
+ offset
,
189 squashfs_finish_page(output
);
200 ERROR("squashfs_read_data failed to read block 0x%llx\n",
201 (unsigned long long) index
);