1 /* SPDX-License-Identifier: GPL-2.0-or-later */
7 * Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008
8 * Phillip Lougher <phillip@squashfs.org.uk>
13 #define SQUASHFS_CACHED_FRAGMENTS CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
14 #define SQUASHFS_MAJOR 4
15 #define SQUASHFS_MINOR 0
16 #define SQUASHFS_START 0
18 /* size of metadata (inode and directory) blocks */
19 #define SQUASHFS_METADATA_SIZE 8192
20 #define SQUASHFS_BLOCK_OFFSET 2
22 /* default size of block device I/O */
23 #ifdef CONFIG_SQUASHFS_4K_DEVBLK_SIZE
24 #define SQUASHFS_DEVBLK_SIZE 4096
26 #define SQUASHFS_DEVBLK_SIZE 1024
29 #define SQUASHFS_FILE_MAX_SIZE 1048576
30 #define SQUASHFS_FILE_MAX_LOG 20
32 /* Max length of filename (not 255) */
33 #define SQUASHFS_NAME_LEN 256
35 /* Max value for directory header count*/
36 #define SQUASHFS_DIR_COUNT 256
38 #define SQUASHFS_INVALID_FRAG (0xffffffffU)
39 #define SQUASHFS_INVALID_XATTR (0xffffffffU)
40 #define SQUASHFS_INVALID_BLK (-1LL)
42 /* Filesystem flags */
43 #define SQUASHFS_NOI 0
44 #define SQUASHFS_NOD 1
45 #define SQUASHFS_NOF 3
46 #define SQUASHFS_NO_FRAG 4
47 #define SQUASHFS_ALWAYS_FRAG 5
48 #define SQUASHFS_DUPLICATE 6
49 #define SQUASHFS_EXPORT 7
50 #define SQUASHFS_COMP_OPT 10
52 #define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
54 #define SQUASHFS_UNCOMPRESSED_INODES(flags) SQUASHFS_BIT(flags, \
57 #define SQUASHFS_UNCOMPRESSED_DATA(flags) SQUASHFS_BIT(flags, \
60 #define SQUASHFS_UNCOMPRESSED_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
63 #define SQUASHFS_NO_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
66 #define SQUASHFS_ALWAYS_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
69 #define SQUASHFS_DUPLICATES(flags) SQUASHFS_BIT(flags, \
72 #define SQUASHFS_EXPORTABLE(flags) SQUASHFS_BIT(flags, \
75 #define SQUASHFS_COMP_OPTS(flags) SQUASHFS_BIT(flags, \
78 /* Inode types including extended types */
79 #define SQUASHFS_DIR_TYPE 1
80 #define SQUASHFS_REG_TYPE 2
81 #define SQUASHFS_SYMLINK_TYPE 3
82 #define SQUASHFS_BLKDEV_TYPE 4
83 #define SQUASHFS_CHRDEV_TYPE 5
84 #define SQUASHFS_FIFO_TYPE 6
85 #define SQUASHFS_SOCKET_TYPE 7
86 #define SQUASHFS_LDIR_TYPE 8
87 #define SQUASHFS_LREG_TYPE 9
88 #define SQUASHFS_LSYMLINK_TYPE 10
89 #define SQUASHFS_LBLKDEV_TYPE 11
90 #define SQUASHFS_LCHRDEV_TYPE 12
91 #define SQUASHFS_LFIFO_TYPE 13
92 #define SQUASHFS_LSOCKET_TYPE 14
94 /* Max type value stored in directory entry */
95 #define SQUASHFS_MAX_DIR_TYPE 7
98 #define SQUASHFS_XATTR_USER 0
99 #define SQUASHFS_XATTR_TRUSTED 1
100 #define SQUASHFS_XATTR_SECURITY 2
101 #define SQUASHFS_XATTR_VALUE_OOL 256
102 #define SQUASHFS_XATTR_PREFIX_MASK 0xff
104 /* Flag whether block is compressed or uncompressed, bit is set if block is
106 #define SQUASHFS_COMPRESSED_BIT (1 << 15)
108 #define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
109 (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT)
111 #define SQUASHFS_COMPRESSED(B) (!((B) & SQUASHFS_COMPRESSED_BIT))
113 #define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24)
115 #define SQUASHFS_COMPRESSED_SIZE_BLOCK(B) ((B) & \
116 ~SQUASHFS_COMPRESSED_BIT_BLOCK)
118 #define SQUASHFS_COMPRESSED_BLOCK(B) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
120 static inline int squashfs_block_size(__le32 raw)
122 u32 size = le32_to_cpu(raw);
123 return (size >> 25) ? -EIO : size;
127 * Inode number ops. Inodes consist of a compressed block number, and an
128 * uncompressed offset within that block
130 #define SQUASHFS_INODE_BLK(A) ((unsigned int) ((A) >> 16))
132 #define SQUASHFS_INODE_OFFSET(A) ((unsigned int) ((A) & 0xffff))
134 #define SQUASHFS_MKINODE(A, B) ((long long)(((long long) (A)\
137 /* fragment and fragment table defines */
138 #define SQUASHFS_FRAGMENT_BYTES(A) \
139 ((A) * sizeof(struct squashfs_fragment_entry))
141 #define SQUASHFS_FRAGMENT_INDEX(A) (SQUASHFS_FRAGMENT_BYTES(A) / \
142 SQUASHFS_METADATA_SIZE)
144 #define SQUASHFS_FRAGMENT_INDEX_OFFSET(A) (SQUASHFS_FRAGMENT_BYTES(A) % \
145 SQUASHFS_METADATA_SIZE)
147 #define SQUASHFS_FRAGMENT_INDEXES(A) ((SQUASHFS_FRAGMENT_BYTES(A) + \
148 SQUASHFS_METADATA_SIZE - 1) / \
149 SQUASHFS_METADATA_SIZE)
151 #define SQUASHFS_FRAGMENT_INDEX_BYTES(A) (SQUASHFS_FRAGMENT_INDEXES(A) *\
154 /* inode lookup table defines */
155 #define SQUASHFS_LOOKUP_BYTES(A) ((A) * sizeof(u64))
157 #define SQUASHFS_LOOKUP_BLOCK(A) (SQUASHFS_LOOKUP_BYTES(A) / \
158 SQUASHFS_METADATA_SIZE)
160 #define SQUASHFS_LOOKUP_BLOCK_OFFSET(A) (SQUASHFS_LOOKUP_BYTES(A) % \
161 SQUASHFS_METADATA_SIZE)
163 #define SQUASHFS_LOOKUP_BLOCKS(A) ((SQUASHFS_LOOKUP_BYTES(A) + \
164 SQUASHFS_METADATA_SIZE - 1) / \
165 SQUASHFS_METADATA_SIZE)
167 #define SQUASHFS_LOOKUP_BLOCK_BYTES(A) (SQUASHFS_LOOKUP_BLOCKS(A) *\
170 /* uid/gid lookup table defines */
171 #define SQUASHFS_ID_BYTES(A) ((A) * sizeof(unsigned int))
173 #define SQUASHFS_ID_BLOCK(A) (SQUASHFS_ID_BYTES(A) / \
174 SQUASHFS_METADATA_SIZE)
176 #define SQUASHFS_ID_BLOCK_OFFSET(A) (SQUASHFS_ID_BYTES(A) % \
177 SQUASHFS_METADATA_SIZE)
179 #define SQUASHFS_ID_BLOCKS(A) ((SQUASHFS_ID_BYTES(A) + \
180 SQUASHFS_METADATA_SIZE - 1) / \
181 SQUASHFS_METADATA_SIZE)
183 #define SQUASHFS_ID_BLOCK_BYTES(A) (SQUASHFS_ID_BLOCKS(A) *\
185 /* xattr id lookup table defines */
186 #define SQUASHFS_XATTR_BYTES(A) ((A) * sizeof(struct squashfs_xattr_id))
188 #define SQUASHFS_XATTR_BLOCK(A) (SQUASHFS_XATTR_BYTES(A) / \
189 SQUASHFS_METADATA_SIZE)
191 #define SQUASHFS_XATTR_BLOCK_OFFSET(A) (SQUASHFS_XATTR_BYTES(A) % \
192 SQUASHFS_METADATA_SIZE)
194 #define SQUASHFS_XATTR_BLOCKS(A) ((SQUASHFS_XATTR_BYTES(A) + \
195 SQUASHFS_METADATA_SIZE - 1) / \
196 SQUASHFS_METADATA_SIZE)
198 #define SQUASHFS_XATTR_BLOCK_BYTES(A) (SQUASHFS_XATTR_BLOCKS(A) *\
200 #define SQUASHFS_XATTR_BLK(A) ((unsigned int) ((A) >> 16))
202 #define SQUASHFS_XATTR_OFFSET(A) ((unsigned int) ((A) & 0xffff))
204 /* cached data constants for filesystem */
205 #define SQUASHFS_CACHED_BLKS 8
207 /* meta index cache */
208 #define SQUASHFS_META_INDEXES (SQUASHFS_METADATA_SIZE / sizeof(unsigned int))
209 #define SQUASHFS_META_ENTRIES 127
210 #define SQUASHFS_META_SLOTS 8
214 unsigned int index_block;
215 unsigned short offset;
220 unsigned int inode_number;
222 unsigned short entries;
224 unsigned short locked;
226 struct meta_entry meta_entry[SQUASHFS_META_ENTRIES];
231 * definitions for structures on disk
233 #define ZLIB_COMPRESSION 1
234 #define LZMA_COMPRESSION 2
235 #define LZO_COMPRESSION 3
236 #define XZ_COMPRESSION 4
237 #define LZ4_COMPRESSION 5
238 #define ZSTD_COMPRESSION 6
240 struct squashfs_super_block {
254 __le64 id_table_start;
255 __le64 xattr_id_table_start;
256 __le64 inode_table_start;
257 __le64 directory_table_start;
258 __le64 fragment_table_start;
259 __le64 lookup_table_start;
262 struct squashfs_dir_index {
266 unsigned char name[];
269 struct squashfs_base_inode {
278 struct squashfs_ipc_inode {
288 struct squashfs_lipc_inode {
299 struct squashfs_dev_inode {
310 struct squashfs_ldev_inode {
322 struct squashfs_symlink_inode {
334 struct squashfs_reg_inode {
348 struct squashfs_lreg_inode {
365 struct squashfs_dir_inode {
379 struct squashfs_ldir_inode {
393 struct squashfs_dir_index index[];
396 union squashfs_inode {
397 struct squashfs_base_inode base;
398 struct squashfs_dev_inode dev;
399 struct squashfs_ldev_inode ldev;
400 struct squashfs_symlink_inode symlink;
401 struct squashfs_reg_inode reg;
402 struct squashfs_lreg_inode lreg;
403 struct squashfs_dir_inode dir;
404 struct squashfs_ldir_inode ldir;
405 struct squashfs_ipc_inode ipc;
406 struct squashfs_lipc_inode lipc;
409 struct squashfs_dir_entry {
417 struct squashfs_dir_header {
423 struct squashfs_fragment_entry {
429 struct squashfs_xattr_entry {
435 struct squashfs_xattr_val {
440 struct squashfs_xattr_id {
446 struct squashfs_xattr_id_table {
447 __le64 xattr_table_start;