162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci======================= 462306a36Sopenharmony_ciSquashfs 4.0 Filesystem 562306a36Sopenharmony_ci======================= 662306a36Sopenharmony_ci 762306a36Sopenharmony_ciSquashfs is a compressed read-only filesystem for Linux. 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciIt uses zlib, lz4, lzo, or xz compression to compress files, inodes and 1062306a36Sopenharmony_cidirectories. Inodes in the system are very small and all blocks are packed to 1162306a36Sopenharmony_ciminimise data overhead. Block sizes greater than 4K are supported up to a 1262306a36Sopenharmony_cimaximum of 1Mbytes (default block size 128K). 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ciSquashfs is intended for general read-only filesystem use, for archival 1562306a36Sopenharmony_ciuse (i.e. in cases where a .tar.gz file may be used), and in constrained 1662306a36Sopenharmony_ciblock device/memory systems (e.g. embedded systems) where low overhead is 1762306a36Sopenharmony_cineeded. 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ciMailing list: squashfs-devel@lists.sourceforge.net 2062306a36Sopenharmony_ciWeb site: www.squashfs.org 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci1. Filesystem Features 2362306a36Sopenharmony_ci---------------------- 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciSquashfs filesystem features versus Cramfs: 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci============================== ========= ========== 2862306a36Sopenharmony_ci Squashfs Cramfs 2962306a36Sopenharmony_ci============================== ========= ========== 3062306a36Sopenharmony_ciMax filesystem size 2^64 256 MiB 3162306a36Sopenharmony_ciMax file size ~ 2 TiB 16 MiB 3262306a36Sopenharmony_ciMax files unlimited unlimited 3362306a36Sopenharmony_ciMax directories unlimited unlimited 3462306a36Sopenharmony_ciMax entries per directory unlimited unlimited 3562306a36Sopenharmony_ciMax block size 1 MiB 4 KiB 3662306a36Sopenharmony_ciMetadata compression yes no 3762306a36Sopenharmony_ciDirectory indexes yes no 3862306a36Sopenharmony_ciSparse file support yes no 3962306a36Sopenharmony_ciTail-end packing (fragments) yes no 4062306a36Sopenharmony_ciExportable (NFS etc.) yes no 4162306a36Sopenharmony_ciHard link support yes no 4262306a36Sopenharmony_ci"." and ".." in readdir yes no 4362306a36Sopenharmony_ciReal inode numbers yes no 4462306a36Sopenharmony_ci32-bit uids/gids yes no 4562306a36Sopenharmony_ciFile creation time yes no 4662306a36Sopenharmony_ciXattr support yes no 4762306a36Sopenharmony_ciACL support no no 4862306a36Sopenharmony_ci============================== ========= ========== 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ciSquashfs compresses data, inodes and directories. In addition, inode and 5162306a36Sopenharmony_cidirectory data are highly compacted, and packed on byte boundaries. Each 5262306a36Sopenharmony_cicompressed inode is on average 8 bytes in length (the exact length varies on 5362306a36Sopenharmony_cifile type, i.e. regular file, directory, symbolic link, and block/char device 5462306a36Sopenharmony_ciinodes have different sizes). 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci2. Using Squashfs 5762306a36Sopenharmony_ci----------------- 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciAs squashfs is a read-only filesystem, the mksquashfs program must be used to 6062306a36Sopenharmony_cicreate populated squashfs filesystems. This and other squashfs utilities 6162306a36Sopenharmony_cican be obtained from http://www.squashfs.org. Usage instructions can be 6262306a36Sopenharmony_ciobtained from this site also. 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ciThe squashfs-tools development tree is now located on kernel.org 6562306a36Sopenharmony_ci git://git.kernel.org/pub/scm/fs/squashfs/squashfs-tools.git 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci3. Squashfs Filesystem Design 6862306a36Sopenharmony_ci----------------------------- 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ciA squashfs filesystem consists of a maximum of nine parts, packed together on a 7162306a36Sopenharmony_cibyte alignment:: 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci --------------- 7462306a36Sopenharmony_ci | superblock | 7562306a36Sopenharmony_ci |---------------| 7662306a36Sopenharmony_ci | compression | 7762306a36Sopenharmony_ci | options | 7862306a36Sopenharmony_ci |---------------| 7962306a36Sopenharmony_ci | datablocks | 8062306a36Sopenharmony_ci | & fragments | 8162306a36Sopenharmony_ci |---------------| 8262306a36Sopenharmony_ci | inode table | 8362306a36Sopenharmony_ci |---------------| 8462306a36Sopenharmony_ci | directory | 8562306a36Sopenharmony_ci | table | 8662306a36Sopenharmony_ci |---------------| 8762306a36Sopenharmony_ci | fragment | 8862306a36Sopenharmony_ci | table | 8962306a36Sopenharmony_ci |---------------| 9062306a36Sopenharmony_ci | export | 9162306a36Sopenharmony_ci | table | 9262306a36Sopenharmony_ci |---------------| 9362306a36Sopenharmony_ci | uid/gid | 9462306a36Sopenharmony_ci | lookup table | 9562306a36Sopenharmony_ci |---------------| 9662306a36Sopenharmony_ci | xattr | 9762306a36Sopenharmony_ci | table | 9862306a36Sopenharmony_ci --------------- 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ciCompressed data blocks are written to the filesystem as files are read from 10162306a36Sopenharmony_cithe source directory, and checked for duplicates. Once all file data has been 10262306a36Sopenharmony_ciwritten the completed inode, directory, fragment, export, uid/gid lookup and 10362306a36Sopenharmony_cixattr tables are written. 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci3.1 Compression options 10662306a36Sopenharmony_ci----------------------- 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ciCompressors can optionally support compression specific options (e.g. 10962306a36Sopenharmony_cidictionary size). If non-default compression options have been used, then 11062306a36Sopenharmony_cithese are stored here. 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci3.2 Inodes 11362306a36Sopenharmony_ci---------- 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciMetadata (inodes and directories) are compressed in 8Kbyte blocks. Each 11662306a36Sopenharmony_cicompressed block is prefixed by a two byte length, the top bit is set if the 11762306a36Sopenharmony_ciblock is uncompressed. A block will be uncompressed if the -noI option is set, 11862306a36Sopenharmony_cior if the compressed block was larger than the uncompressed block. 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ciInodes are packed into the metadata blocks, and are not aligned to block 12162306a36Sopenharmony_ciboundaries, therefore inodes overlap compressed blocks. Inodes are identified 12262306a36Sopenharmony_ciby a 48-bit number which encodes the location of the compressed metadata block 12362306a36Sopenharmony_cicontaining the inode, and the byte offset into that block where the inode is 12462306a36Sopenharmony_ciplaced (<block, offset>). 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ciTo maximise compression there are different inodes for each file type 12762306a36Sopenharmony_ci(regular file, directory, device, etc.), the inode contents and length 12862306a36Sopenharmony_civarying with the type. 12962306a36Sopenharmony_ci 13062306a36Sopenharmony_ciTo further maximise compression, two types of regular file inode and 13162306a36Sopenharmony_cidirectory inode are defined: inodes optimised for frequently occurring 13262306a36Sopenharmony_ciregular files and directories, and extended types where extra 13362306a36Sopenharmony_ciinformation has to be stored. 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci3.3 Directories 13662306a36Sopenharmony_ci--------------- 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ciLike inodes, directories are packed into compressed metadata blocks, stored 13962306a36Sopenharmony_ciin a directory table. Directories are accessed using the start address of 14062306a36Sopenharmony_cithe metablock containing the directory and the offset into the 14162306a36Sopenharmony_cidecompressed block (<block, offset>). 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ciDirectories are organised in a slightly complex way, and are not simply 14462306a36Sopenharmony_cia list of file names. The organisation takes advantage of the 14562306a36Sopenharmony_cifact that (in most cases) the inodes of the files will be in the same 14662306a36Sopenharmony_cicompressed metadata block, and therefore, can share the start block. 14762306a36Sopenharmony_ciDirectories are therefore organised in a two level list, a directory 14862306a36Sopenharmony_ciheader containing the shared start block value, and a sequence of directory 14962306a36Sopenharmony_cientries, each of which share the shared start block. A new directory header 15062306a36Sopenharmony_ciis written once/if the inode start block changes. The directory 15162306a36Sopenharmony_ciheader/directory entry list is repeated as many times as necessary. 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ciDirectories are sorted, and can contain a directory index to speed up 15462306a36Sopenharmony_cifile lookup. Directory indexes store one entry per metablock, each entry 15562306a36Sopenharmony_cistoring the index/filename mapping to the first directory header 15662306a36Sopenharmony_ciin each metadata block. Directories are sorted in alphabetical order, 15762306a36Sopenharmony_ciand at lookup the index is scanned linearly looking for the first filename 15862306a36Sopenharmony_cialphabetically larger than the filename being looked up. At this point the 15962306a36Sopenharmony_cilocation of the metadata block the filename is in has been found. 16062306a36Sopenharmony_ciThe general idea of the index is to ensure only one metadata block needs to be 16162306a36Sopenharmony_cidecompressed to do a lookup irrespective of the length of the directory. 16262306a36Sopenharmony_ciThis scheme has the advantage that it doesn't require extra memory overhead 16362306a36Sopenharmony_ciand doesn't require much extra storage on disk. 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci3.4 File data 16662306a36Sopenharmony_ci------------- 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ciRegular files consist of a sequence of contiguous compressed blocks, and/or a 16962306a36Sopenharmony_cicompressed fragment block (tail-end packed block). The compressed size 17062306a36Sopenharmony_ciof each datablock is stored in a block list contained within the 17162306a36Sopenharmony_cifile inode. 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ciTo speed up access to datablocks when reading 'large' files (256 Mbytes or 17462306a36Sopenharmony_cilarger), the code implements an index cache that caches the mapping from 17562306a36Sopenharmony_ciblock index to datablock location on disk. 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ciThe index cache allows Squashfs to handle large files (up to 1.75 TiB) while 17862306a36Sopenharmony_ciretaining a simple and space-efficient block list on disk. The cache 17962306a36Sopenharmony_ciis split into slots, caching up to eight 224 GiB files (128 KiB blocks). 18062306a36Sopenharmony_ciLarger files use multiple slots, with 1.75 TiB files using all 8 slots. 18162306a36Sopenharmony_ciThe index cache is designed to be memory efficient, and by default uses 18262306a36Sopenharmony_ci16 KiB. 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci3.5 Fragment lookup table 18562306a36Sopenharmony_ci------------------------- 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ciRegular files can contain a fragment index which is mapped to a fragment 18862306a36Sopenharmony_cilocation on disk and compressed size using a fragment lookup table. This 18962306a36Sopenharmony_cifragment lookup table is itself stored compressed into metadata blocks. 19062306a36Sopenharmony_ciA second index table is used to locate these. This second index table for 19162306a36Sopenharmony_cispeed of access (and because it is small) is read at mount time and cached 19262306a36Sopenharmony_ciin memory. 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci3.6 Uid/gid lookup table 19562306a36Sopenharmony_ci------------------------ 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ciFor space efficiency regular files store uid and gid indexes, which are 19862306a36Sopenharmony_ciconverted to 32-bit uids/gids using an id look up table. This table is 19962306a36Sopenharmony_cistored compressed into metadata blocks. A second index table is used to 20062306a36Sopenharmony_cilocate these. This second index table for speed of access (and because it 20162306a36Sopenharmony_ciis small) is read at mount time and cached in memory. 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci3.7 Export table 20462306a36Sopenharmony_ci---------------- 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ciTo enable Squashfs filesystems to be exportable (via NFS etc.) filesystems 20762306a36Sopenharmony_cican optionally (disabled with the -no-exports Mksquashfs option) contain 20862306a36Sopenharmony_cian inode number to inode disk location lookup table. This is required to 20962306a36Sopenharmony_cienable Squashfs to map inode numbers passed in filehandles to the inode 21062306a36Sopenharmony_cilocation on disk, which is necessary when the export code reinstantiates 21162306a36Sopenharmony_ciexpired/flushed inodes. 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ciThis table is stored compressed into metadata blocks. A second index table is 21462306a36Sopenharmony_ciused to locate these. This second index table for speed of access (and because 21562306a36Sopenharmony_ciit is small) is read at mount time and cached in memory. 21662306a36Sopenharmony_ci 21762306a36Sopenharmony_ci3.8 Xattr table 21862306a36Sopenharmony_ci--------------- 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ciThe xattr table contains extended attributes for each inode. The xattrs 22162306a36Sopenharmony_cifor each inode are stored in a list, each list entry containing a type, 22262306a36Sopenharmony_ciname and value field. The type field encodes the xattr prefix 22362306a36Sopenharmony_ci("user.", "trusted." etc) and it also encodes how the name/value fields 22462306a36Sopenharmony_cishould be interpreted. Currently the type indicates whether the value 22562306a36Sopenharmony_ciis stored inline (in which case the value field contains the xattr value), 22662306a36Sopenharmony_cior if it is stored out of line (in which case the value field stores a 22762306a36Sopenharmony_cireference to where the actual value is stored). This allows large values 22862306a36Sopenharmony_cito be stored out of line improving scanning and lookup performance and it 22962306a36Sopenharmony_cialso allows values to be de-duplicated, the value being stored once, and 23062306a36Sopenharmony_ciall other occurrences holding an out of line reference to that value. 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ciThe xattr lists are packed into compressed 8K metadata blocks. 23362306a36Sopenharmony_ciTo reduce overhead in inodes, rather than storing the on-disk 23462306a36Sopenharmony_cilocation of the xattr list inside each inode, a 32-bit xattr id 23562306a36Sopenharmony_ciis stored. This xattr id is mapped into the location of the xattr 23662306a36Sopenharmony_cilist using a second xattr id lookup table. 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci4. TODOs and Outstanding Issues 23962306a36Sopenharmony_ci------------------------------- 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ci4.1 TODO list 24262306a36Sopenharmony_ci------------- 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciImplement ACL support. 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci4.2 Squashfs Internal Cache 24762306a36Sopenharmony_ci--------------------------- 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ciBlocks in Squashfs are compressed. To avoid repeatedly decompressing 25062306a36Sopenharmony_cirecently accessed data Squashfs uses two small metadata and fragment caches. 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ciThe cache is not used for file datablocks, these are decompressed and cached in 25362306a36Sopenharmony_cithe page-cache in the normal way. The cache is used to temporarily cache 25462306a36Sopenharmony_cifragment and metadata blocks which have been read as a result of a metadata 25562306a36Sopenharmony_ci(i.e. inode or directory) or fragment access. Because metadata and fragments 25662306a36Sopenharmony_ciare packed together into blocks (to gain greater compression) the read of a 25762306a36Sopenharmony_ciparticular piece of metadata or fragment will retrieve other metadata/fragments 25862306a36Sopenharmony_ciwhich have been packed with it, these because of locality-of-reference may be 25962306a36Sopenharmony_ciread in the near future. Temporarily caching them ensures they are available 26062306a36Sopenharmony_cifor near future access without requiring an additional read and decompress. 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ciIn the future this internal cache may be replaced with an implementation which 26362306a36Sopenharmony_ciuses the kernel page cache. Because the page cache operates on page sized 26462306a36Sopenharmony_ciunits this may introduce additional complexity in terms of locking and 26562306a36Sopenharmony_ciassociated race conditions. 266