6 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012,
7 * 2013, 2014, 2017, 2019
8 * Phillip Lougher <phillip@squashfs.org.uk>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2,
13 * or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #define SQUASHFS_CACHED_FRAGMENTS CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
28 #define SQUASHFS_MAJOR 4
29 #define SQUASHFS_MINOR 0
30 #define SQUASHFS_MAGIC 0x73717368
31 #define SQUASHFS_MAGIC_SWAP 0x68737173
32 #define SQUASHFS_START 0
34 /* size of metadata (inode and directory) blocks */
35 #define SQUASHFS_METADATA_SIZE 8192
36 #define SQUASHFS_METADATA_LOG 13
38 /* default size of data blocks */
39 #define SQUASHFS_FILE_SIZE 131072
41 #define SQUASHFS_FILE_MAX_SIZE 1048576
42 #define SQUASHFS_FILE_MAX_LOG 20
44 /* Max number of uids and gids */
45 #define SQUASHFS_IDS 65536
47 /* Max length of filename (not 255) */
48 #define SQUASHFS_NAME_LEN 256
50 /* Max value for directory header count */
51 #define SQUASHFS_DIR_COUNT 256
53 #define SQUASHFS_INVALID ((long long) 0xffffffffffff)
54 #define SQUASHFS_INVALID_FRAG ((unsigned int) 0xffffffff)
55 #define SQUASHFS_INVALID_XATTR ((unsigned int) 0xffffffff)
56 #define SQUASHFS_INVALID_BLK ((long long) -1)
57 #define SQUASHFS_USED_BLK ((long long) -2)
59 /* Filesystem flags */
60 #define SQUASHFS_NOI 0
61 #define SQUASHFS_NOD 1
62 #define SQUASHFS_CHECK 2
63 #define SQUASHFS_NOF 3
64 #define SQUASHFS_NO_FRAG 4
65 #define SQUASHFS_ALWAYS_FRAG 5
66 #define SQUASHFS_DUPLICATE 6
67 #define SQUASHFS_EXPORT 7
68 #define SQUASHFS_NOX 8
69 #define SQUASHFS_NO_XATTR 9
70 #define SQUASHFS_COMP_OPT 10
71 #define SQUASHFS_NOID 11
73 #define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
75 #define SQUASHFS_UNCOMPRESSED_INODES(flags) SQUASHFS_BIT(flags, \
78 #define SQUASHFS_UNCOMPRESSED_DATA(flags) SQUASHFS_BIT(flags, \
81 #define SQUASHFS_UNCOMPRESSED_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
84 #define SQUASHFS_NO_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
87 #define SQUASHFS_ALWAYS_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
90 #define SQUASHFS_DUPLICATES(flags) SQUASHFS_BIT(flags, \
93 #define SQUASHFS_EXPORTABLE(flags) SQUASHFS_BIT(flags, \
96 #define SQUASHFS_UNCOMPRESSED_XATTRS(flags) SQUASHFS_BIT(flags, \
99 #define SQUASHFS_NO_XATTRS(flags) SQUASHFS_BIT(flags, \
102 #define SQUASHFS_COMP_OPTS(flags) SQUASHFS_BIT(flags, \
105 #define SQUASHFS_UNCOMPRESSED_IDS(flags) SQUASHFS_BIT(flags, \
108 #define SQUASHFS_MKFLAGS(noi, nod, nof, nox, noid, no_frag, always_frag, \
109 duplicate_checking, exportable, no_xattr, comp_opt) (noi | \
110 (nod << 1) | (nof << 3) | (no_frag << 4) | \
111 (always_frag << 5) | (duplicate_checking << 6) | \
112 (exportable << 7) | (nox << 8) | (no_xattr << 9) | \
113 (comp_opt << 10) | (noid << 11))
115 /* Max number of types and file types */
116 #define SQUASHFS_DIR_TYPE 1
117 #define SQUASHFS_FILE_TYPE 2
118 #define SQUASHFS_SYMLINK_TYPE 3
119 #define SQUASHFS_BLKDEV_TYPE 4
120 #define SQUASHFS_CHRDEV_TYPE 5
121 #define SQUASHFS_FIFO_TYPE 6
122 #define SQUASHFS_SOCKET_TYPE 7
123 #define SQUASHFS_LDIR_TYPE 8
124 #define SQUASHFS_LREG_TYPE 9
125 #define SQUASHFS_LSYMLINK_TYPE 10
126 #define SQUASHFS_LBLKDEV_TYPE 11
127 #define SQUASHFS_LCHRDEV_TYPE 12
128 #define SQUASHFS_LFIFO_TYPE 13
129 #define SQUASHFS_LSOCKET_TYPE 14
132 #define SQUASHFS_XATTR_USER 0
133 #define SQUASHFS_XATTR_TRUSTED 1
134 #define SQUASHFS_XATTR_SECURITY 2
135 #define SQUASHFS_XATTR_VALUE_OOL 256
136 #define SQUASHFS_XATTR_PREFIX_MASK 0xff
138 /* Flag whether block is compressed or uncompressed, bit is set if block is
140 #define SQUASHFS_COMPRESSED_BIT (1 << 15)
142 #define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
143 (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT)
145 #define SQUASHFS_COMPRESSED(B) (!((B) & SQUASHFS_COMPRESSED_BIT))
147 #define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24)
149 #define SQUASHFS_COMPRESSED_SIZE_BLOCK(B) ((B) & \
150 ~SQUASHFS_COMPRESSED_BIT_BLOCK)
152 #define SQUASHFS_COMPRESSED_BLOCK(B) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
155 * Inode number ops. Inodes consist of a compressed block number, and an
156 * uncompressed offset within that block
158 #define SQUASHFS_INODE_BLK(a) ((unsigned int) ((a) >> 16))
160 #define SQUASHFS_INODE_OFFSET(a) ((unsigned int) ((a) & 0xffff))
162 #define SQUASHFS_MKINODE(A, B) ((squashfs_inode)(((squashfs_inode) (A)\
165 /* Compute 32 bit VFS inode number from squashfs inode number */
166 #define SQUASHFS_MK_VFS_INODE(a, b) ((unsigned int) (((a) << 8) + \
169 /* Translate between VFS mode and squashfs mode */
170 #define SQUASHFS_MODE(a) ((a) & 0xfff)
172 /* fragment and fragment table defines */
173 #define SQUASHFS_FRAGMENT_BYTES(A) ((A) * \
174 sizeof(struct squashfs_fragment_entry))
176 #define SQUASHFS_FRAGMENT_INDEX(A) (SQUASHFS_FRAGMENT_BYTES(A) / \
177 SQUASHFS_METADATA_SIZE)
179 #define SQUASHFS_FRAGMENT_INDEX_OFFSET(A) (SQUASHFS_FRAGMENT_BYTES(A) % \
180 SQUASHFS_METADATA_SIZE)
182 #define SQUASHFS_FRAGMENT_INDEXES(A) ((SQUASHFS_FRAGMENT_BYTES(A) + \
183 SQUASHFS_METADATA_SIZE - 1) / \
184 SQUASHFS_METADATA_SIZE)
186 #define SQUASHFS_FRAGMENT_INDEX_BYTES(A) (SQUASHFS_FRAGMENT_INDEXES(A) *\
189 /* inode lookup table defines */
190 #define SQUASHFS_LOOKUP_BYTES(A) ((A) * sizeof(squashfs_inode))
192 #define SQUASHFS_LOOKUP_BLOCK(A) (SQUASHFS_LOOKUP_BYTES(A) / \
193 SQUASHFS_METADATA_SIZE)
195 #define SQUASHFS_LOOKUP_BLOCK_OFFSET(A) (SQUASHFS_LOOKUP_BYTES(A) % \
196 SQUASHFS_METADATA_SIZE)
198 #define SQUASHFS_LOOKUP_BLOCKS(A) ((SQUASHFS_LOOKUP_BYTES(A) + \
199 SQUASHFS_METADATA_SIZE - 1) / \
200 SQUASHFS_METADATA_SIZE)
202 #define SQUASHFS_LOOKUP_BLOCK_BYTES(A) (SQUASHFS_LOOKUP_BLOCKS(A) *\
205 /* uid lookup table defines */
206 #define SQUASHFS_ID_BYTES(A) ((A) * sizeof(unsigned int))
208 #define SQUASHFS_ID_BLOCK(A) (SQUASHFS_ID_BYTES(A) / \
209 SQUASHFS_METADATA_SIZE)
211 #define SQUASHFS_ID_BLOCK_OFFSET(A) (SQUASHFS_ID_BYTES(A) % \
212 SQUASHFS_METADATA_SIZE)
214 #define SQUASHFS_ID_BLOCKS(A) ((SQUASHFS_ID_BYTES(A) + \
215 SQUASHFS_METADATA_SIZE - 1) / \
216 SQUASHFS_METADATA_SIZE)
218 #define SQUASHFS_ID_BLOCK_BYTES(A) (SQUASHFS_ID_BLOCKS(A) *\
221 /* xattr id lookup table defines */
222 #define SQUASHFS_XATTR_BYTES(A) ((A) * sizeof(struct squashfs_xattr_id))
224 #define SQUASHFS_XATTR_BLOCK(A) (SQUASHFS_XATTR_BYTES(A) / \
225 SQUASHFS_METADATA_SIZE)
227 #define SQUASHFS_XATTR_BLOCK_OFFSET(A) (SQUASHFS_XATTR_BYTES(A) % \
228 SQUASHFS_METADATA_SIZE)
230 #define SQUASHFS_XATTR_BLOCKS(A) ((SQUASHFS_XATTR_BYTES(A) + \
231 SQUASHFS_METADATA_SIZE - 1) / \
232 SQUASHFS_METADATA_SIZE)
234 #define SQUASHFS_XATTR_BLOCK_BYTES(A) (SQUASHFS_XATTR_BLOCKS(A) *\
237 #define SQUASHFS_XATTR_BLK(A) ((unsigned int) ((A) >> 16))
239 #define SQUASHFS_XATTR_OFFSET(A) ((unsigned int) ((A) & 0xffff))
241 /* cached data constants for filesystem */
242 #define SQUASHFS_CACHED_BLKS 8
244 #define SQUASHFS_MAX_FILE_SIZE_LOG 64
246 #define SQUASHFS_MAX_FILE_SIZE ((long long) 1 << \
247 (SQUASHFS_MAX_FILE_SIZE_LOG - 2))
249 #define SQUASHFS_MARKER_BYTE 0xff
251 /* meta index cache */
252 #define SQUASHFS_META_INDEXES (SQUASHFS_METADATA_SIZE / sizeof(unsigned int))
253 #define SQUASHFS_META_ENTRIES 31
254 #define SQUASHFS_META_NUMBER 8
255 #define SQUASHFS_SLOTS 4
258 long long data_block
;
259 unsigned int index_block
;
260 unsigned short offset
;
265 unsigned int inode_number
;
267 unsigned short entries
;
269 unsigned short locked
;
271 struct meta_entry meta_entry
[SQUASHFS_META_ENTRIES
];
276 * definitions for structures on disk
279 typedef long long squashfs_block
;
280 typedef long long squashfs_inode
;
282 #define ZLIB_COMPRESSION 1
283 #define LZMA_COMPRESSION 2
284 #define LZO_COMPRESSION 3
285 #define XZ_COMPRESSION 4
286 #define LZ4_COMPRESSION 5
287 #define ZSTD_COMPRESSION 6
289 struct squashfs_super_block
{
290 unsigned int s_magic
;
292 unsigned int mkfs_time
/* time of filesystem creation */;
293 unsigned int block_size
;
294 unsigned int fragments
;
295 unsigned short compression
;
296 unsigned short block_log
;
297 unsigned short flags
;
298 unsigned short no_ids
;
299 unsigned short s_major
;
300 unsigned short s_minor
;
301 squashfs_inode root_inode
;
302 long long bytes_used
;
303 long long id_table_start
;
304 long long xattr_id_table_start
;
305 long long inode_table_start
;
306 long long directory_table_start
;
307 long long fragment_table_start
;
308 long long lookup_table_start
;
311 struct squashfs_dir_index
{
313 unsigned int start_block
;
315 unsigned char name
[0];
318 struct squashfs_base_inode_header
{
319 unsigned short inode_type
;
324 unsigned int inode_number
;
327 struct squashfs_ipc_inode_header
{
328 unsigned short inode_type
;
333 unsigned int inode_number
;
337 struct squashfs_lipc_inode_header
{
338 unsigned short inode_type
;
343 unsigned int inode_number
;
348 struct squashfs_dev_inode_header
{
349 unsigned short inode_type
;
354 unsigned int inode_number
;
359 struct squashfs_ldev_inode_header
{
360 unsigned short inode_type
;
365 unsigned int inode_number
;
371 struct squashfs_symlink_inode_header
{
372 unsigned short inode_type
;
377 unsigned int inode_number
;
379 unsigned int symlink_size
;
383 struct squashfs_reg_inode_header
{
384 unsigned short inode_type
;
389 unsigned int inode_number
;
390 unsigned int start_block
;
391 unsigned int fragment
;
393 unsigned int file_size
;
394 unsigned int block_list
[0];
397 struct squashfs_lreg_inode_header
{
398 unsigned short inode_type
;
403 unsigned int inode_number
;
404 squashfs_block start_block
;
408 unsigned int fragment
;
411 unsigned int block_list
[0];
414 struct squashfs_dir_inode_header
{
415 unsigned short inode_type
;
420 unsigned int inode_number
;
421 unsigned int start_block
;
423 unsigned short file_size
;
424 unsigned short offset
;
425 unsigned int parent_inode
;
428 struct squashfs_ldir_inode_header
{
429 unsigned short inode_type
;
434 unsigned int inode_number
;
436 unsigned int file_size
;
437 unsigned int start_block
;
438 unsigned int parent_inode
;
439 unsigned short i_count
;
440 unsigned short offset
;
442 struct squashfs_dir_index index
[0];
445 union squashfs_inode_header
{
446 struct squashfs_base_inode_header base
;
447 struct squashfs_dev_inode_header dev
;
448 struct squashfs_ldev_inode_header ldev
;
449 struct squashfs_symlink_inode_header symlink
;
450 struct squashfs_reg_inode_header reg
;
451 struct squashfs_lreg_inode_header lreg
;
452 struct squashfs_dir_inode_header dir
;
453 struct squashfs_ldir_inode_header ldir
;
454 struct squashfs_ipc_inode_header ipc
;
455 struct squashfs_lipc_inode_header lipc
;
458 struct squashfs_dir_entry
{
459 unsigned short offset
;
466 struct squashfs_dir_header
{
468 unsigned int start_block
;
469 unsigned int inode_number
;
472 struct squashfs_fragment_entry
{
473 long long start_block
;
478 struct squashfs_xattr_entry
{
483 struct squashfs_xattr_val
{
487 struct squashfs_xattr_id
{
493 struct squashfs_xattr_table
{
494 long long xattr_table_start
;
495 unsigned int xattr_ids
;