1.. SPDX-License-Identifier: GPL-2.0
2
3======================================
4EROFS - Enhanced Read-Only File System
5======================================
6
7Overview
8========
9
10EROFS filesystem stands for Enhanced Read-Only File System.  It aims to form a
11generic read-only filesystem solution for various read-only use cases instead
12of just focusing on storage space saving without considering any side effects
13of runtime performance.
14
15It is designed to meet the needs of flexibility, feature extendability and user
16payload friendly, etc.  Apart from those, it is still kept as a simple
17random-access friendly high-performance filesystem to get rid of unneeded I/O
18amplification and memory-resident overhead compared to similar approaches.
19
20It is implemented to be a better choice for the following scenarios:
21
22 - read-only storage media or
23
24 - part of a fully trusted read-only solution, which means it needs to be
25   immutable and bit-for-bit identical to the official golden image for
26   their releases due to security or other considerations and
27
28 - hope to minimize extra storage space with guaranteed end-to-end performance
29   by using compact layout, transparent file compression and direct access,
30   especially for those embedded devices with limited memory and high-density
31   hosts with numerous containers.
32
33Here is the main features of EROFS:
34
35 - Little endian on-disk design;
36
37 - 4KiB block size and 32-bit block addresses, therefore 16TiB address space
38   at most for now;
39
40 - Two inode layouts for different requirements:
41
42   =====================  ============  ======================================
43                          compact (v1)  extended (v2)
44   =====================  ============  ======================================
45   Inode metadata size    32 bytes      64 bytes
46   Max file size          4 GiB         16 EiB (also limited by max. vol size)
47   Max uids/gids          65536         4294967296
48   Per-inode timestamp    no            yes (64 + 32-bit timestamp)
49   Max hardlinks          65536         4294967296
50   Metadata reserved      8 bytes       18 bytes
51   =====================  ============  ======================================
52
53 - Metadata and data could be mixed as an option;
54
55 - Support extended attributes (xattrs) as an option;
56
57 - Support tailpacking data and xattr inline compared to byte-addressed
58   unaligned metadata or smaller block size alternatives;
59
60 - Support POSIX.1e ACLs by using xattrs;
61
62 - Support transparent data compression as an option:
63   LZ4 and MicroLZMA algorithms can be used on a per-file basis; In addition,
64   inplace decompression is also supported to avoid bounce compressed buffers
65   and page cache thrashing.
66
67 - Support direct I/O on uncompressed files to avoid double caching for loop
68   devices;
69
70 - Support FSDAX on uncompressed images for secure containers and ramdisks in
71   order to get rid of unnecessary page cache.
72
73 - Support multiple devices for multi blob container images;
74
75 - Support file-based on-demand loading with the Fscache infrastructure.
76
77The following git tree provides the file system user-space tools under
78development, such as a formatting tool (mkfs.erofs), an on-disk consistency &
79compatibility checking tool (fsck.erofs), and a debugging tool (dump.erofs):
80
81- git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git
82
83Bugs and patches are welcome, please kindly help us and send to the following
84linux-erofs mailing list:
85
86- linux-erofs mailing list   <linux-erofs@lists.ozlabs.org>
87
88Mount options
89=============
90
91===================    =========================================================
92(no)user_xattr         Setup Extended User Attributes. Note: xattr is enabled
93                       by default if CONFIG_EROFS_FS_XATTR is selected.
94(no)acl                Setup POSIX Access Control List. Note: acl is enabled
95                       by default if CONFIG_EROFS_FS_POSIX_ACL is selected.
96cache_strategy=%s      Select a strategy for cached decompression from now on:
97
98		       ==========  =============================================
99                         disabled  In-place I/O decompression only;
100                        readahead  Cache the last incomplete compressed physical
101                                   cluster for further reading. It still does
102                                   in-place I/O decompression for the rest
103                                   compressed physical clusters;
104                       readaround  Cache the both ends of incomplete compressed
105                                   physical clusters for further reading.
106                                   It still does in-place I/O decompression
107                                   for the rest compressed physical clusters.
108		       ==========  =============================================
109dax={always,never}     Use direct access (no page cache).  See
110                       Documentation/filesystems/dax.rst.
111dax                    A legacy option which is an alias for ``dax=always``.
112device=%s              Specify a path to an extra device to be used together.
113fsid=%s                Specify a filesystem image ID for Fscache back-end.
114===================    =========================================================
115
116Sysfs Entries
117=============
118
119Information about mounted erofs file systems can be found in /sys/fs/erofs.
120Each mounted filesystem will have a directory in /sys/fs/erofs based on its
121device name (i.e., /sys/fs/erofs/sda).
122(see also Documentation/ABI/testing/sysfs-fs-erofs)
123
124On-disk details
125===============
126
127Summary
128-------
129Different from other read-only file systems, an EROFS volume is designed
130to be as simple as possible::
131
132                                |-> aligned with the block size
133   ____________________________________________________________
134  | |SB| | ... | Metadata | ... | Data | Metadata | ... | Data |
135  |_|__|_|_____|__________|_____|______|__________|_____|______|
136  0 +1K
137
138All data areas should be aligned with the block size, but metadata areas
139may not. All metadatas can be now observed in two different spaces (views):
140
141 1. Inode metadata space
142
143    Each valid inode should be aligned with an inode slot, which is a fixed
144    value (32 bytes) and designed to be kept in line with compact inode size.
145
146    Each inode can be directly found with the following formula:
147         inode offset = meta_blkaddr * block_size + 32 * nid
148
149    ::
150
151                                 |-> aligned with 8B
152                                            |-> followed closely
153     + meta_blkaddr blocks                                      |-> another slot
154       _____________________________________________________________________
155     |  ...   | inode |  xattrs  | extents  | data inline | ... | inode ...
156     |________|_______|(optional)|(optional)|__(optional)_|_____|__________
157              |-> aligned with the inode slot size
158                   .                   .
159                 .                         .
160               .                              .
161             .                                    .
162           .                                         .
163         .                                              .
164       .____________________________________________________|-> aligned with 4B
165       | xattr_ibody_header | shared xattrs | inline xattrs |
166       |____________________|_______________|_______________|
167       |->    12 bytes    <-|->x * 4 bytes<-|               .
168                           .                .                 .
169                     .                      .                   .
170                .                           .                     .
171            ._______________________________.______________________.
172            | id | id | id | id |  ... | id | ent | ... | ent| ... |
173            |____|____|____|____|______|____|_____|_____|____|_____|
174                                            |-> aligned with 4B
175                                                        |-> aligned with 4B
176
177    Inode could be 32 or 64 bytes, which can be distinguished from a common
178    field which all inode versions have -- i_format::
179
180        __________________               __________________
181       |     i_format     |             |     i_format     |
182       |__________________|             |__________________|
183       |        ...       |             |        ...       |
184       |                  |             |                  |
185       |__________________| 32 bytes    |                  |
186                                        |                  |
187                                        |__________________| 64 bytes
188
189    Xattrs, extents, data inline are followed by the corresponding inode with
190    proper alignment, and they could be optional for different data mappings.
191    _currently_ total 5 data layouts are supported:
192
193    ==  ====================================================================
194     0  flat file data without data inline (no extent);
195     1  fixed-sized output data compression (with non-compacted indexes);
196     2  flat file data with tail packing data inline (no extent);
197     3  fixed-sized output data compression (with compacted indexes, v5.3+);
198     4  chunk-based file (v5.15+).
199    ==  ====================================================================
200
201    The size of the optional xattrs is indicated by i_xattr_count in inode
202    header. Large xattrs or xattrs shared by many different files can be
203    stored in shared xattrs metadata rather than inlined right after inode.
204
205 2. Shared xattrs metadata space
206
207    Shared xattrs space is similar to the above inode space, started with
208    a specific block indicated by xattr_blkaddr, organized one by one with
209    proper align.
210
211    Each share xattr can also be directly found by the following formula:
212         xattr offset = xattr_blkaddr * block_size + 4 * xattr_id
213
214::
215
216                           |-> aligned by  4 bytes
217    + xattr_blkaddr blocks                     |-> aligned with 4 bytes
218     _________________________________________________________________________
219    |  ...   | xattr_entry |  xattr data | ... |  xattr_entry | xattr data  ...
220    |________|_____________|_____________|_____|______________|_______________
221
222Directories
223-----------
224All directories are now organized in a compact on-disk format. Note that
225each directory block is divided into index and name areas in order to support
226random file lookup, and all directory entries are _strictly_ recorded in
227alphabetical order in order to support improved prefix binary search
228algorithm (could refer to the related source code).
229
230::
231
232                  ___________________________
233                 /                           |
234                /              ______________|________________
235               /              /              | nameoff1       | nameoffN-1
236  ____________.______________._______________v________________v__________
237 | dirent | dirent | ... | dirent | filename | filename | ... | filename |
238 |___.0___|____1___|_____|___N-1__|____0_____|____1_____|_____|___N-1____|
239      \                           ^
240       \                          |                           * could have
241        \                         |                             trailing '\0'
242         \________________________| nameoff0
243                             Directory block
244
245Note that apart from the offset of the first filename, nameoff0 also indicates
246the total number of directory entries in this block since it is no need to
247introduce another on-disk field at all.
248
249Chunk-based files
250-----------------
251In order to support chunk-based data deduplication, a new inode data layout has
252been supported since Linux v5.15: Files are split in equal-sized data chunks
253with ``extents`` area of the inode metadata indicating how to get the chunk
254data: these can be simply as a 4-byte block address array or in the 8-byte
255chunk index form (see struct erofs_inode_chunk_index in erofs_fs.h for more
256details.)
257
258By the way, chunk-based files are all uncompressed for now.
259
260Data compression
261----------------
262EROFS implements LZ4 fixed-sized output compression which generates fixed-sized
263compressed data blocks from variable-sized input in contrast to other existing
264fixed-sized input solutions. Relatively higher compression ratios can be gotten
265by using fixed-sized output compression since nowadays popular data compression
266algorithms are mostly LZ77-based and such fixed-sized output approach can be
267benefited from the historical dictionary (aka. sliding window).
268
269In details, original (uncompressed) data is turned into several variable-sized
270extents and in the meanwhile, compressed into physical clusters (pclusters).
271In order to record each variable-sized extent, logical clusters (lclusters) are
272introduced as the basic unit of compress indexes to indicate whether a new
273extent is generated within the range (HEAD) or not (NONHEAD). Lclusters are now
274fixed in block size, as illustrated below::
275
276          |<-    variable-sized extent    ->|<-       VLE         ->|
277        clusterofs                        clusterofs              clusterofs
278          |                                 |                       |
279 _________v_________________________________v_______________________v________
280 ... |    .         |              |        .     |              |  .   ...
281 ____|____._________|______________|________.___ _|______________|__.________
282     |-> lcluster <-|-> lcluster <-|-> lcluster <-|-> lcluster <-|
283          (HEAD)        (NONHEAD)       (HEAD)        (NONHEAD)    .
284           .             CBLKCNT            .                    .
285            .                               .                  .
286             .                              .                .
287       _______._____________________________.______________._________________
288          ... |              |              |              | ...
289       _______|______________|______________|______________|_________________
290              |->      big pcluster       <-|-> pcluster <-|
291
292A physical cluster can be seen as a container of physical compressed blocks
293which contains compressed data. Previously, only lcluster-sized (4KB) pclusters
294were supported. After big pcluster feature is introduced (available since
295Linux v5.13), pcluster can be a multiple of lcluster size.
296
297For each HEAD lcluster, clusterofs is recorded to indicate where a new extent
298starts and blkaddr is used to seek the compressed data. For each NONHEAD
299lcluster, delta0 and delta1 are available instead of blkaddr to indicate the
300distance to its HEAD lcluster and the next HEAD lcluster. A PLAIN lcluster is
301also a HEAD lcluster except that its data is uncompressed. See the comments
302around "struct z_erofs_vle_decompressed_index" in erofs_fs.h for more details.
303
304If big pcluster is enabled, pcluster size in lclusters needs to be recorded as
305well. Let the delta0 of the first NONHEAD lcluster store the compressed block
306count with a special flag as a new called CBLKCNT NONHEAD lcluster. It's easy
307to understand its delta0 is constantly 1, as illustrated below::
308
309   __________________________________________________________
310  | HEAD |  NONHEAD  | NONHEAD | ... | NONHEAD | HEAD | HEAD |
311  |__:___|_(CBLKCNT)_|_________|_____|_________|__:___|____:_|
312     |<----- a big pcluster (with CBLKCNT) ------>|<--  -->|
313           a lcluster-sized pcluster (without CBLKCNT) ^
314
315If another HEAD follows a HEAD lcluster, there is no room to record CBLKCNT,
316but it's easy to know the size of such pcluster is 1 lcluster as well.
317