squashfs.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. SQUASHFS 4.0 FILESYSTEM
  2. =======================
  3. Squashfs is a compressed read-only filesystem for Linux.
  4. It uses zlib compression to compress files, inodes and directories.
  5. Inodes in the system are very small and all blocks are packed to minimise
  6. data overhead. Block sizes greater than 4K are supported up to a maximum
  7. of 1Mbytes (default block size 128K).
  8. Squashfs is intended for general read-only filesystem use, for archival
  9. use (i.e. in cases where a .tar.gz file may be used), and in constrained
  10. block device/memory systems (e.g. embedded systems) where low overhead is
  11. needed.
  12. Mailing list: squashfs-devel@lists.sourceforge.net
  13. Web site: www.squashfs.org
  14. 1. FILESYSTEM FEATURES
  15. ----------------------
  16. Squashfs filesystem features versus Cramfs:
  17. Squashfs Cramfs
  18. Max filesystem size: 2^64 256 MiB
  19. Max file size: ~ 2 TiB 16 MiB
  20. Max files: unlimited unlimited
  21. Max directories: unlimited unlimited
  22. Max entries per directory: unlimited unlimited
  23. Max block size: 1 MiB 4 KiB
  24. Metadata compression: yes no
  25. Directory indexes: yes no
  26. Sparse file support: yes no
  27. Tail-end packing (fragments): yes no
  28. Exportable (NFS etc.): yes no
  29. Hard link support: yes no
  30. "." and ".." in readdir: yes no
  31. Real inode numbers: yes no
  32. 32-bit uids/gids: yes no
  33. File creation time: yes no
  34. Xattr and ACL support: no no
  35. Squashfs compresses data, inodes and directories. In addition, inode and
  36. directory data are highly compacted, and packed on byte boundaries. Each
  37. compressed inode is on average 8 bytes in length (the exact length varies on
  38. file type, i.e. regular file, directory, symbolic link, and block/char device
  39. inodes have different sizes).
  40. 2. USING SQUASHFS
  41. -----------------
  42. As squashfs is a read-only filesystem, the mksquashfs program must be used to
  43. create populated squashfs filesystems. This and other squashfs utilities
  44. can be obtained from http://www.squashfs.org. Usage instructions can be
  45. obtained from this site also.
  46. 3. SQUASHFS FILESYSTEM DESIGN
  47. -----------------------------
  48. A squashfs filesystem consists of seven parts, packed together on a byte
  49. alignment:
  50. ---------------
  51. | superblock |
  52. |---------------|
  53. | datablocks |
  54. | & fragments |
  55. |---------------|
  56. | inode table |
  57. |---------------|
  58. | directory |
  59. | table |
  60. |---------------|
  61. | fragment |
  62. | table |
  63. |---------------|
  64. | export |
  65. | table |
  66. |---------------|
  67. | uid/gid |
  68. | lookup table |
  69. ---------------
  70. Compressed data blocks are written to the filesystem as files are read from
  71. the source directory, and checked for duplicates. Once all file data has been
  72. written the completed inode, directory, fragment, export and uid/gid lookup
  73. tables are written.
  74. 3.1 Inodes
  75. ----------
  76. Metadata (inodes and directories) are compressed in 8Kbyte blocks. Each
  77. compressed block is prefixed by a two byte length, the top bit is set if the
  78. block is uncompressed. A block will be uncompressed if the -noI option is set,
  79. or if the compressed block was larger than the uncompressed block.
  80. Inodes are packed into the metadata blocks, and are not aligned to block
  81. boundaries, therefore inodes overlap compressed blocks. Inodes are identified
  82. by a 48-bit number which encodes the location of the compressed metadata block
  83. containing the inode, and the byte offset into that block where the inode is
  84. placed (<block, offset>).
  85. To maximise compression there are different inodes for each file type
  86. (regular file, directory, device, etc.), the inode contents and length
  87. varying with the type.
  88. To further maximise compression, two types of regular file inode and
  89. directory inode are defined: inodes optimised for frequently occurring
  90. regular files and directories, and extended types where extra
  91. information has to be stored.
  92. 3.2 Directories
  93. ---------------
  94. Like inodes, directories are packed into compressed metadata blocks, stored
  95. in a directory table. Directories are accessed using the start address of
  96. the metablock containing the directory and the offset into the
  97. decompressed block (<block, offset>).
  98. Directories are organised in a slightly complex way, and are not simply
  99. a list of file names. The organisation takes advantage of the
  100. fact that (in most cases) the inodes of the files will be in the same
  101. compressed metadata block, and therefore, can share the start block.
  102. Directories are therefore organised in a two level list, a directory
  103. header containing the shared start block value, and a sequence of directory
  104. entries, each of which share the shared start block. A new directory header
  105. is written once/if the inode start block changes. The directory
  106. header/directory entry list is repeated as many times as necessary.
  107. Directories are sorted, and can contain a directory index to speed up
  108. file lookup. Directory indexes store one entry per metablock, each entry
  109. storing the index/filename mapping to the first directory header
  110. in each metadata block. Directories are sorted in alphabetical order,
  111. and at lookup the index is scanned linearly looking for the first filename
  112. alphabetically larger than the filename being looked up. At this point the
  113. location of the metadata block the filename is in has been found.
  114. The general idea of the index is ensure only one metadata block needs to be
  115. decompressed to do a lookup irrespective of the length of the directory.
  116. This scheme has the advantage that it doesn't require extra memory overhead
  117. and doesn't require much extra storage on disk.
  118. 3.3 File data
  119. -------------
  120. Regular files consist of a sequence of contiguous compressed blocks, and/or a
  121. compressed fragment block (tail-end packed block). The compressed size
  122. of each datablock is stored in a block list contained within the
  123. file inode.
  124. To speed up access to datablocks when reading 'large' files (256 Mbytes or
  125. larger), the code implements an index cache that caches the mapping from
  126. block index to datablock location on disk.
  127. The index cache allows Squashfs to handle large files (up to 1.75 TiB) while
  128. retaining a simple and space-efficient block list on disk. The cache
  129. is split into slots, caching up to eight 224 GiB files (128 KiB blocks).
  130. Larger files use multiple slots, with 1.75 TiB files using all 8 slots.
  131. The index cache is designed to be memory efficient, and by default uses
  132. 16 KiB.
  133. 3.4 Fragment lookup table
  134. -------------------------
  135. Regular files can contain a fragment index which is mapped to a fragment
  136. location on disk and compressed size using a fragment lookup table. This
  137. fragment lookup table is itself stored compressed into metadata blocks.
  138. A second index table is used to locate these. This second index table for
  139. speed of access (and because it is small) is read at mount time and cached
  140. in memory.
  141. 3.5 Uid/gid lookup table
  142. ------------------------
  143. For space efficiency regular files store uid and gid indexes, which are
  144. converted to 32-bit uids/gids using an id look up table. This table is
  145. stored compressed into metadata blocks. A second index table is used to
  146. locate these. This second index table for speed of access (and because it
  147. is small) is read at mount time and cached in memory.
  148. 3.6 Export table
  149. ----------------
  150. To enable Squashfs filesystems to be exportable (via NFS etc.) filesystems
  151. can optionally (disabled with the -no-exports Mksquashfs option) contain
  152. an inode number to inode disk location lookup table. This is required to
  153. enable Squashfs to map inode numbers passed in filehandles to the inode
  154. location on disk, which is necessary when the export code reinstantiates
  155. expired/flushed inodes.
  156. This table is stored compressed into metadata blocks. A second index table is
  157. used to locate these. This second index table for speed of access (and because
  158. it is small) is read at mount time and cached in memory.
  159. 4. TODOS AND OUTSTANDING ISSUES
  160. -------------------------------
  161. 4.1 Todo list
  162. -------------
  163. Implement Xattr and ACL support. The Squashfs 4.0 filesystem layout has hooks
  164. for these but the code has not been written. Once the code has been written
  165. the existing layout should not require modification.
  166. 4.2 Squashfs internal cache
  167. ---------------------------
  168. Blocks in Squashfs are compressed. To avoid repeatedly decompressing
  169. recently accessed data Squashfs uses two small metadata and fragment caches.
  170. The cache is not used for file datablocks, these are decompressed and cached in
  171. the page-cache in the normal way. The cache is used to temporarily cache
  172. fragment and metadata blocks which have been read as a result of a metadata
  173. (i.e. inode or directory) or fragment access. Because metadata and fragments
  174. are packed together into blocks (to gain greater compression) the read of a
  175. particular piece of metadata or fragment will retrieve other metadata/fragments
  176. which have been packed with it, these because of locality-of-reference may be
  177. read in the near future. Temporarily caching them ensures they are available
  178. for near future access without requiring an additional read and decompress.
  179. In the future this internal cache may be replaced with an implementation which
  180. uses the kernel page cache. Because the page cache operates on page sized
  181. units this may introduce additional complexity in terms of locking and
  182. associated race conditions.