xref: /linux/Documentation/filesystems/erofs.rst (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1.. SPDX-License-Identifier: GPL-2.0
2
3======================================
4EROFS - Enhanced Read-Only File System
5======================================
6
7Overview
8========
9
10EROFS (Enhanced Read-Only File System) is a modern, efficient, and secure
11read-only kernel filesystem designed for various use cases including immutable
12system images, container images, application sandbox images, and dataset
13distribution.
14
15An immutable image filesystem can be regarded as an enhanced archive format
16which allows golden images to be built once and mounted everywhere -- images are
17bit-for-bit identical across all deployments and can be verified, audited, or
18shared without concerns about runtime modifications (in this model, all user
19writes should be redirected into another trusted filesystem, for example, via
20overlayfs for copy-on-write-style redirection, by design).
21
22EROFS is a dedicated implementation of the image filesystem idea above, with a
23flexible, hierarchical on-disk design so that needed features can be enabled on
24demand. Filesystem data in the core format is strictly block-aligned in order
25to perform optimally on all kinds of storage media, including block devices and
26memory-backed devices. The on-disk format is easy to parse and purposely avoids
27the unnecessary metadata redundancy found in generic writable filesystems, which
28can suffer from extra inconsistency issues -- making it ideal for security
29auditing and untrusted remote access. In addition, designs such as inline data,
30inline/shared extended attributes, and optimized (de)compression provide better
31space efficiency while maintaining high performance.
32
33In short, EROFS aims to be a better fit for the following scenarios:
34
35 - As part of a secure immutable storage solution, where it needs to be
36   immutable and bit-for-bit identical to the official golden image for
37   each individual copy, in order to meet security, data sharing, and/or
38   other requirements;
39
40 - Minimizing storage overhead with guaranteed end-to-end performance
41   by using compact (meta)data layout, optimized transparent data compression,
42   deduplication and direct access, especially for those embedded devices with
43   limited memory and high-density hosts with numerous containers.
44
45Here is the list of highlights:
46
47 - Little endian on-disk design with 48-bit block addressing, supporting up
48   to 1 EiB filesystem capacity with 4 KiB block size;
49
50 - Two compact inode metadata layouts for space and performance efficiency:
51
52   ========================  ========  ======================================
53                             compact   extended
54   ========================  ========  ======================================
55   Inode core metadata size  32 bytes  64 bytes
56   Max file size             4 GiB     16 EiB (also limited by max. vol size)
57   Max uids/gids             65536     4294967296
58   Nanosecond timestamps     no        yes
59   Max hardlinks             65536     4294967296
60   ========================  ========  ======================================
61
62 - Support tailpacking inline data for better space efficiency and reduce
63   unneeded I/O amplification;
64
65 - Block-based and file-backed distribution are both supported;
66
67 - Multiple devices to reference external data blobs: inode data can be
68   optionally placed into external blobs, which enables image layering and data
69   sharing among different filesystems;
70
71 - Inline and shared extended attributes with an optional bloom filter that
72   speeds up negative extended attribute lookups;
73
74 - POSIX.1e ACLs by using extended attributes;
75
76 - Transparent data compression as an option: Supported algorithms (LZ4,
77   MicroLZMA, DEFLATE and Zstandard) can be selected on a per-inode basis.
78   Both the on-disk metadata and decompression runtime have been heavily
79   optimized to minimize the overhead for better performance.
80
81 - Merging tail-end data into a special inode as fragments;
82
83 - Chunk-based deduplication and rolling-hash compressed data deduplication;
84
85 - Direct I/O and FSDAX support on uncompressed inodes for use cases such as
86   secure containers, loop devices, and ramdisks that do not need page caching;
87
88 - Page cache sharing among inodes with identical content fingerprints on
89   the same machine.
90
91For more detailed information, please refer to our documentation site:
92
93- https://erofs.docs.kernel.org
94
95The following git tree provides the file system user-space tools under
96development, such as a formatting tool (mkfs.erofs), an on-disk consistency &
97compatibility checking tool (fsck.erofs), and a debugging tool (dump.erofs):
98
99- git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs-utils.git
100
101Bugs and patches are welcome, please kindly help us and send to the following
102linux-erofs mailing list:
103
104- linux-erofs mailing list   <linux-erofs@lists.ozlabs.org>
105
106Mount options
107=============
108
109===================    =========================================================
110(no)user_xattr         Setup Extended User Attributes. Note: xattr is enabled
111                       by default if CONFIG_EROFS_FS_XATTR is selected.
112(no)acl                Setup POSIX Access Control List. Note: acl is enabled
113                       by default if CONFIG_EROFS_FS_POSIX_ACL is selected.
114cache_strategy=%s      Select a strategy for cached decompression from now on:
115
116		       ==========  =============================================
117                         disabled  In-place I/O decompression only;
118                        readahead  Cache the last incomplete compressed physical
119                                   cluster for further reading. It still does
120                                   in-place I/O decompression for the rest
121                                   compressed physical clusters;
122                       readaround  Cache both ends of incomplete compressed
123                                   physical clusters for further reading.
124                                   It still does in-place I/O decompression
125                                   for the rest compressed physical clusters.
126		       ==========  =============================================
127dax={always,never}     Use direct access (no page cache).  See
128                       Documentation/filesystems/dax.rst.
129dax                    A legacy option which is an alias for ``dax=always``.
130device=%s              Specify a path to an extra device to be used together.
131directio               (For file-backed mounts) Use direct I/O to access backing
132                       files, and asynchronous I/O will be enabled if supported.
133domain_id=%s           Specify a trusted domain ID. Filesystems sharing the same
134                       domain ID can share page cache across mounts when inode
135                       page sharing is enabled. (not shown in mountinfo output)
136fsoffset=%llu          Specify block-aligned filesystem offset for the primary device.
137inode_share            Enable inode page sharing for this filesystem.  Inodes with
138                       identical content within the same domain ID can share the
139                       page cache.
140source=%s              (For file-backed mounts) Specify the backing image as a path
141                       or as an already-opened file descriptor.
142===================    =========================================================
143
144File-backed mounts
145==================
146
147When CONFIG_EROFS_FS_BACKED_BY_FILE is enabled, EROFS file-backed images
148can be mounted directly without a loopback block device.  The backing file
149can be given either as a path, or as an already-opened file descriptor.
150
151When a file descriptor is used, the kernel resolves its path and records it
152so that /proc/mounts and similar interfaces can still report the mount
153source.
154
155Only regular files are accepted as backing files; to mount an image that
156resides on a block device, use the traditional block device mount path
157instead.
158
159Sysfs Entries
160=============
161
162Information about mounted erofs file systems can be found in /sys/fs/erofs.
163Each mounted filesystem will have a directory in /sys/fs/erofs based on its
164device name (i.e., /sys/fs/erofs/sda).
165(see also Documentation/ABI/testing/sysfs-fs-erofs)
166
167On-disk details
168===============
169
170Summary
171-------
172Different from other read-only file systems, an EROFS volume is designed
173to be as simple as possible::
174
175                                |-> aligned with the block size
176   ____________________________________________________________
177  | |SB| | ... | Metadata | ... | Data | Metadata | ... | Data |
178  |_|__|_|_____|__________|_____|______|__________|_____|______|
179  0 +1K
180
181All data areas should be aligned with the block size, but metadata areas
182may not. All metadata can be now observed in two different spaces (views):
183
184 1. Inode metadata space
185
186    Each valid inode should be aligned with an inode slot, which is a fixed
187    value (32 bytes) and designed to be kept in line with compact inode size.
188
189    Each inode can be directly found with the following formula:
190         inode offset = meta_blkaddr * block_size + 32 * nid
191
192    ::
193
194                                 |-> aligned with 8B
195                                            |-> followed closely
196     + meta_blkaddr blocks                                      |-> another slot
197       _____________________________________________________________________
198     |  ...   | inode |  xattrs  | extents  | data inline | ... | inode ...
199     |________|_______|(optional)|(optional)|__(optional)_|_____|__________
200              |-> aligned with the inode slot size
201                   .                   .
202                 .                         .
203               .                              .
204             .                                    .
205           .                                         .
206         .                                              .
207       .____________________________________________________|-> aligned with 4B
208       | xattr_ibody_header | shared xattrs | inline xattrs |
209       |____________________|_______________|_______________|
210       |->    12 bytes    <-|->x * 4 bytes<-|               .
211                           .                .                 .
212                     .                      .                   .
213                .                           .                     .
214            ._______________________________.______________________.
215            | id | id | id | id |  ... | id | ent | ... | ent| ... |
216            |____|____|____|____|______|____|_____|_____|____|_____|
217                                            |-> aligned with 4B
218                                                        |-> aligned with 4B
219
220    Inode could be 32 or 64 bytes, which can be distinguished from a common
221    field which all inode versions have -- i_format::
222
223        __________________               __________________
224       |     i_format     |             |     i_format     |
225       |__________________|             |__________________|
226       |        ...       |             |        ...       |
227       |                  |             |                  |
228       |__________________| 32 bytes    |                  |
229                                        |                  |
230                                        |__________________| 64 bytes
231
232    Xattrs, extents, data inline are placed after the corresponding inode with
233    proper alignment, and they could be optional for different data mappings.
234    _currently_ total 5 data layouts are supported:
235
236    ==  ====================================================================
237     0  flat file data without data inline (no extent);
238     1  fixed-sized output data compression (with non-compacted indexes);
239     2  flat file data with tail packing data inline (no extent);
240     3  fixed-sized output data compression (with compacted indexes, v5.3+);
241     4  chunk-based file (v5.15+).
242    ==  ====================================================================
243
244    The size of the optional xattrs is indicated by i_xattr_count in inode
245    header. Large xattrs or xattrs shared by many different files can be
246    stored in shared xattrs metadata rather than inlined right after inode.
247
248 2. Shared xattrs metadata space
249
250    Shared xattrs space is similar to the above inode space, started with
251    a specific block indicated by xattr_blkaddr, organized one by one with
252    proper align.
253
254    Each share xattr can also be directly found by the following formula:
255         xattr offset = xattr_blkaddr * block_size + 4 * xattr_id
256
257::
258
259                           |-> aligned by  4 bytes
260    + xattr_blkaddr blocks                     |-> aligned with 4 bytes
261     _________________________________________________________________________
262    |  ...   | xattr_entry |  xattr data | ... |  xattr_entry | xattr data  ...
263    |________|_____________|_____________|_____|______________|_______________
264
265Directories
266-----------
267All directories are now organized in a compact on-disk format. Note that
268each directory block is divided into index and name areas in order to support
269random file lookup, and all directory entries are _strictly_ recorded in
270alphabetical order in order to support improved prefix binary search
271algorithm (could refer to the related source code).
272
273::
274
275                  ___________________________
276                 /                           |
277                /              ______________|________________
278               /              /              | nameoff1       | nameoffN-1
279  ____________.______________._______________v________________v__________
280 | dirent | dirent | ... | dirent | filename | filename | ... | filename |
281 |___.0___|____1___|_____|___N-1__|____0_____|____1_____|_____|___N-1____|
282      \                           ^
283       \                          |                           * could have
284        \                         |                             trailing '\0'
285         \________________________| nameoff0
286                             Directory block
287
288Note that apart from the offset of the first filename, nameoff0 also indicates
289the total number of directory entries in this block since it is no need to
290introduce another on-disk field at all.
291
292Chunk-based files
293-----------------
294In order to support chunk-based data deduplication, a new inode data layout has
295been supported since Linux v5.15: Files are split in equal-sized data chunks
296with ``extents`` area of the inode metadata indicating how to get the chunk
297data: these can be simply as a 4-byte block address array or in the 8-byte
298chunk index form (see struct erofs_inode_chunk_index in erofs_fs.h for more
299details.)
300
301By the way, chunk-based files are all uncompressed for now.
302
303Long extended attribute name prefixes
304-------------------------------------
305There are use cases where extended attributes with different values can have
306only a few common prefixes (such as overlayfs xattrs).  The predefined prefixes
307work inefficiently in both image size and runtime performance in such cases.
308
309The long xattr name prefixes feature is introduced to address this issue.  The
310overall idea is that, apart from the existing predefined prefixes, the xattr
311entry could also refer to user-specified long xattr name prefixes, e.g.
312"trusted.overlay.".
313
314When referring to a long xattr name prefix, the highest bit (bit 7) of
315erofs_xattr_entry.e_name_index is set, while the lower bits (bit 0-6) as a whole
316represent the index of the referred long name prefix among all long name
317prefixes.  Therefore, only the trailing part of the name apart from the long
318xattr name prefix is stored in erofs_xattr_entry.e_name, which could be empty if
319the full xattr name matches exactly as its long xattr name prefix.
320
321All long xattr prefixes are stored one by one in the packed inode as long as
322the packed inode is valid, or in the meta inode otherwise.  The
323xattr_prefix_count (of the on-disk superblock) indicates the total number of
324long xattr name prefixes, while (xattr_prefix_start * 4) indicates the start
325offset of long name prefixes in the packed/meta inode.  Note that, long extended
326attribute name prefixes are disabled if xattr_prefix_count is 0.
327
328Each long name prefix is stored in the format: ALIGN({__le16 len, data}, 4),
329where len represents the total size of the data part.  The data part is actually
330represented by 'struct erofs_xattr_long_prefix', where base_index represents the
331index of the predefined xattr name prefix, e.g. EROFS_XATTR_INDEX_TRUSTED for
332"trusted.overlay." long name prefix, while the infix string keeps the string
333after stripping the short prefix, e.g. "overlay." for the example above.
334
335Data compression
336----------------
337EROFS implements fixed-sized output compression which generates fixed-sized
338compressed data blocks from variable-sized input in contrast to other existing
339fixed-sized input solutions. Relatively higher compression ratios can be gotten
340by using fixed-sized output compression since nowadays popular data compression
341algorithms are mostly LZ77-based and such fixed-sized output approach can be
342benefited from the historical dictionary (aka. sliding window).
343
344In details, original (uncompressed) data is turned into several variable-sized
345extents and in the meanwhile, compressed into physical clusters (pclusters).
346In order to record each variable-sized extent, logical clusters (lclusters) are
347introduced as the basic unit of compress indexes to indicate whether a new
348extent is generated within the range (HEAD) or not (NONHEAD). Lclusters are now
349fixed in block size, as illustrated below::
350
351          |<-    variable-sized extent    ->|<-       VLE         ->|
352        clusterofs                        clusterofs              clusterofs
353          |                                 |                       |
354 _________v_________________________________v_______________________v________
355 ... |    .         |              |        .     |              |  .   ...
356 ____|____._________|______________|________.___ _|______________|__.________
357     |-> lcluster <-|-> lcluster <-|-> lcluster <-|-> lcluster <-|
358          (HEAD)        (NONHEAD)       (HEAD)        (NONHEAD)    .
359           .             CBLKCNT            .                    .
360            .                               .                  .
361             .                              .                .
362       _______._____________________________.______________._________________
363          ... |              |              |              | ...
364       _______|______________|______________|______________|_________________
365              |->      big pcluster       <-|-> pcluster <-|
366
367A physical cluster can be seen as a container of physical compressed blocks
368which contains compressed data. Previously, only lcluster-sized (4KB) pclusters
369were supported. After big pcluster feature is introduced (available since
370Linux v5.13), pcluster can be a multiple of lcluster size.
371
372For each HEAD lcluster, clusterofs is recorded to indicate where a new extent
373starts and blkaddr is used to seek the compressed data. For each NONHEAD
374lcluster, delta0 and delta1 are available instead of blkaddr to indicate the
375distance to its HEAD lcluster and the next HEAD lcluster. A PLAIN lcluster is
376also a HEAD lcluster except that its data is uncompressed. See the comments
377around "struct z_erofs_vle_decompressed_index" in erofs_fs.h for more details.
378
379If big pcluster is enabled, pcluster size in lclusters needs to be recorded as
380well. Let the delta0 of the first NONHEAD lcluster store the compressed block
381count with a special flag as a new called CBLKCNT NONHEAD lcluster. It's easy
382to understand its delta0 is constantly 1, as illustrated below::
383
384   __________________________________________________________
385  | HEAD |  NONHEAD  | NONHEAD | ... | NONHEAD | HEAD | HEAD |
386  |__:___|_(CBLKCNT)_|_________|_____|_________|__:___|____:_|
387     |<----- a big pcluster (with CBLKCNT) ------>|<--  -->|
388           a lcluster-sized pcluster (without CBLKCNT) ^
389
390If another HEAD follows a HEAD lcluster, there is no room to record CBLKCNT,
391but it's easy to know the size of such pcluster is 1 lcluster as well.
392
393Since Linux v6.1, each pcluster can be used for multiple variable-sized extents,
394therefore it can be used for compressed data deduplication.
395