xref: /linux/Documentation/filesystems/ext2.rst (revision a460513ed4b6994bfeb7bd86f72853140bc1ac12)
1.. SPDX-License-Identifier: GPL-2.0
2
3
4==============================
5The Second Extended Filesystem
6==============================
7
8ext2 was originally released in January 1993.  Written by R\'emy Card,
9Theodore Ts'o and Stephen Tweedie, it was a major rewrite of the
10Extended Filesystem.  It is currently still (April 2001) the predominant
11filesystem in use by Linux.  There are also implementations available
12for NetBSD, FreeBSD, the GNU HURD, Windows 95/98/NT, OS/2 and RISC OS.
13
14Options
15=======
16
17Most defaults are determined by the filesystem superblock, and can be
18set using tune2fs(8). Kernel-determined defaults are indicated by (*).
19
20====================    ===     ================================================
21bsddf			(*)	Makes ``df`` act like BSD.
22minixdf				Makes ``df`` act like Minix.
23
24check=none, nocheck	(*)	Don't do extra checking of bitmaps on mount
25				(check=normal and check=strict options removed)
26
27dax				Use direct access (no page cache).  See
28				Documentation/filesystems/dax.txt.
29
30debug				Extra debugging information is sent to the
31				kernel syslog.  Useful for developers.
32
33errors=continue			Keep going on a filesystem error.
34errors=remount-ro		Remount the filesystem read-only on an error.
35errors=panic			Panic and halt the machine if an error occurs.
36
37grpid, bsdgroups		Give objects the same group ID as their parent.
38nogrpid, sysvgroups		New objects have the group ID of their creator.
39
40nouid32				Use 16-bit UIDs and GIDs.
41
42oldalloc			Enable the old block allocator. Orlov should
43				have better performance, we'd like to get some
44				feedback if it's the contrary for you.
45orlov			(*)	Use the Orlov block allocator.
46				(See http://lwn.net/Articles/14633/ and
47				http://lwn.net/Articles/14446/.)
48
49resuid=n			The user ID which may use the reserved blocks.
50resgid=n			The group ID which may use the reserved blocks.
51
52sb=n				Use alternate superblock at this location.
53
54user_xattr			Enable "user." POSIX Extended Attributes
55				(requires CONFIG_EXT2_FS_XATTR).
56nouser_xattr			Don't support "user." extended attributes.
57
58acl				Enable POSIX Access Control Lists support
59				(requires CONFIG_EXT2_FS_POSIX_ACL).
60noacl				Don't support POSIX ACLs.
61
62nobh				Do not attach buffer_heads to file pagecache.
63
64quota, usrquota			Enable user disk quota support
65				(requires CONFIG_QUOTA).
66
67grpquota			Enable group disk quota support
68				(requires CONFIG_QUOTA).
69====================    ===     ================================================
70
71noquota option ls silently ignored by ext2.
72
73
74Specification
75=============
76
77ext2 shares many properties with traditional Unix filesystems.  It has
78the concepts of blocks, inodes and directories.  It has space in the
79specification for Access Control Lists (ACLs), fragments, undeletion and
80compression though these are not yet implemented (some are available as
81separate patches).  There is also a versioning mechanism to allow new
82features (such as journalling) to be added in a maximally compatible
83manner.
84
85Blocks
86------
87
88The space in the device or file is split up into blocks.  These are
89a fixed size, of 1024, 2048 or 4096 bytes (8192 bytes on Alpha systems),
90which is decided when the filesystem is created.  Smaller blocks mean
91less wasted space per file, but require slightly more accounting overhead,
92and also impose other limits on the size of files and the filesystem.
93
94Block Groups
95------------
96
97Blocks are clustered into block groups in order to reduce fragmentation
98and minimise the amount of head seeking when reading a large amount
99of consecutive data.  Information about each block group is kept in a
100descriptor table stored in the block(s) immediately after the superblock.
101Two blocks near the start of each group are reserved for the block usage
102bitmap and the inode usage bitmap which show which blocks and inodes
103are in use.  Since each bitmap is limited to a single block, this means
104that the maximum size of a block group is 8 times the size of a block.
105
106The block(s) following the bitmaps in each block group are designated
107as the inode table for that block group and the remainder are the data
108blocks.  The block allocation algorithm attempts to allocate data blocks
109in the same block group as the inode which contains them.
110
111The Superblock
112--------------
113
114The superblock contains all the information about the configuration of
115the filing system.  The primary copy of the superblock is stored at an
116offset of 1024 bytes from the start of the device, and it is essential
117to mounting the filesystem.  Since it is so important, backup copies of
118the superblock are stored in block groups throughout the filesystem.
119The first version of ext2 (revision 0) stores a copy at the start of
120every block group, along with backups of the group descriptor block(s).
121Because this can consume a considerable amount of space for large
122filesystems, later revisions can optionally reduce the number of backup
123copies by only putting backups in specific groups (this is the sparse
124superblock feature).  The groups chosen are 0, 1 and powers of 3, 5 and 7.
125
126The information in the superblock contains fields such as the total
127number of inodes and blocks in the filesystem and how many are free,
128how many inodes and blocks are in each block group, when the filesystem
129was mounted (and if it was cleanly unmounted), when it was modified,
130what version of the filesystem it is (see the Revisions section below)
131and which OS created it.
132
133If the filesystem is revision 1 or higher, then there are extra fields,
134such as a volume name, a unique identification number, the inode size,
135and space for optional filesystem features to store configuration info.
136
137All fields in the superblock (as in all other ext2 structures) are stored
138on the disc in little endian format, so a filesystem is portable between
139machines without having to know what machine it was created on.
140
141Inodes
142------
143
144The inode (index node) is a fundamental concept in the ext2 filesystem.
145Each object in the filesystem is represented by an inode.  The inode
146structure contains pointers to the filesystem blocks which contain the
147data held in the object and all of the metadata about an object except
148its name.  The metadata about an object includes the permissions, owner,
149group, flags, size, number of blocks used, access time, change time,
150modification time, deletion time, number of links, fragments, version
151(for NFS) and extended attributes (EAs) and/or Access Control Lists (ACLs).
152
153There are some reserved fields which are currently unused in the inode
154structure and several which are overloaded.  One field is reserved for the
155directory ACL if the inode is a directory and alternately for the top 32
156bits of the file size if the inode is a regular file (allowing file sizes
157larger than 2GB).  The translator field is unused under Linux, but is used
158by the HURD to reference the inode of a program which will be used to
159interpret this object.  Most of the remaining reserved fields have been
160used up for both Linux and the HURD for larger owner and group fields,
161The HURD also has a larger mode field so it uses another of the remaining
162fields to store the extra more bits.
163
164There are pointers to the first 12 blocks which contain the file's data
165in the inode.  There is a pointer to an indirect block (which contains
166pointers to the next set of blocks), a pointer to a doubly-indirect
167block (which contains pointers to indirect blocks) and a pointer to a
168trebly-indirect block (which contains pointers to doubly-indirect blocks).
169
170The flags field contains some ext2-specific flags which aren't catered
171for by the standard chmod flags.  These flags can be listed with lsattr
172and changed with the chattr command, and allow specific filesystem
173behaviour on a per-file basis.  There are flags for secure deletion,
174undeletable, compression, synchronous updates, immutability, append-only,
175dumpable, no-atime, indexed directories, and data-journaling.  Not all
176of these are supported yet.
177
178Directories
179-----------
180
181A directory is a filesystem object and has an inode just like a file.
182It is a specially formatted file containing records which associate
183each name with an inode number.  Later revisions of the filesystem also
184encode the type of the object (file, directory, symlink, device, fifo,
185socket) to avoid the need to check the inode itself for this information
186(support for taking advantage of this feature does not yet exist in
187Glibc 2.2).
188
189The inode allocation code tries to assign inodes which are in the same
190block group as the directory in which they are first created.
191
192The current implementation of ext2 uses a singly-linked list to store
193the filenames in the directory; a pending enhancement uses hashing of the
194filenames to allow lookup without the need to scan the entire directory.
195
196The current implementation never removes empty directory blocks once they
197have been allocated to hold more files.
198
199Special files
200-------------
201
202Symbolic links are also filesystem objects with inodes.  They deserve
203special mention because the data for them is stored within the inode
204itself if the symlink is less than 60 bytes long.  It uses the fields
205which would normally be used to store the pointers to data blocks.
206This is a worthwhile optimisation as it we avoid allocating a full
207block for the symlink, and most symlinks are less than 60 characters long.
208
209Character and block special devices never have data blocks assigned to
210them.  Instead, their device number is stored in the inode, again reusing
211the fields which would be used to point to the data blocks.
212
213Reserved Space
214--------------
215
216In ext2, there is a mechanism for reserving a certain number of blocks
217for a particular user (normally the super-user).  This is intended to
218allow for the system to continue functioning even if non-privileged users
219fill up all the space available to them (this is independent of filesystem
220quotas).  It also keeps the filesystem from filling up entirely which
221helps combat fragmentation.
222
223Filesystem check
224----------------
225
226At boot time, most systems run a consistency check (e2fsck) on their
227filesystems.  The superblock of the ext2 filesystem contains several
228fields which indicate whether fsck should actually run (since checking
229the filesystem at boot can take a long time if it is large).  fsck will
230run if the filesystem was not cleanly unmounted, if the maximum mount
231count has been exceeded or if the maximum time between checks has been
232exceeded.
233
234Feature Compatibility
235---------------------
236
237The compatibility feature mechanism used in ext2 is sophisticated.
238It safely allows features to be added to the filesystem, without
239unnecessarily sacrificing compatibility with older versions of the
240filesystem code.  The feature compatibility mechanism is not supported by
241the original revision 0 (EXT2_GOOD_OLD_REV) of ext2, but was introduced in
242revision 1.  There are three 32-bit fields, one for compatible features
243(COMPAT), one for read-only compatible (RO_COMPAT) features and one for
244incompatible (INCOMPAT) features.
245
246These feature flags have specific meanings for the kernel as follows:
247
248A COMPAT flag indicates that a feature is present in the filesystem,
249but the on-disk format is 100% compatible with older on-disk formats, so
250a kernel which didn't know anything about this feature could read/write
251the filesystem without any chance of corrupting the filesystem (or even
252making it inconsistent).  This is essentially just a flag which says
253"this filesystem has a (hidden) feature" that the kernel or e2fsck may
254want to be aware of (more on e2fsck and feature flags later).  The ext3
255HAS_JOURNAL feature is a COMPAT flag because the ext3 journal is simply
256a regular file with data blocks in it so the kernel does not need to
257take any special notice of it if it doesn't understand ext3 journaling.
258
259An RO_COMPAT flag indicates that the on-disk format is 100% compatible
260with older on-disk formats for reading (i.e. the feature does not change
261the visible on-disk format).  However, an old kernel writing to such a
262filesystem would/could corrupt the filesystem, so this is prevented. The
263most common such feature, SPARSE_SUPER, is an RO_COMPAT feature because
264sparse groups allow file data blocks where superblock/group descriptor
265backups used to live, and ext2_free_blocks() refuses to free these blocks,
266which would leading to inconsistent bitmaps.  An old kernel would also
267get an error if it tried to free a series of blocks which crossed a group
268boundary, but this is a legitimate layout in a SPARSE_SUPER filesystem.
269
270An INCOMPAT flag indicates the on-disk format has changed in some
271way that makes it unreadable by older kernels, or would otherwise
272cause a problem if an old kernel tried to mount it.  FILETYPE is an
273INCOMPAT flag because older kernels would think a filename was longer
274than 256 characters, which would lead to corrupt directory listings.
275The COMPRESSION flag is an obvious INCOMPAT flag - if the kernel
276doesn't understand compression, you would just get garbage back from
277read() instead of it automatically decompressing your data.  The ext3
278RECOVER flag is needed to prevent a kernel which does not understand the
279ext3 journal from mounting the filesystem without replaying the journal.
280
281For e2fsck, it needs to be more strict with the handling of these
282flags than the kernel.  If it doesn't understand ANY of the COMPAT,
283RO_COMPAT, or INCOMPAT flags it will refuse to check the filesystem,
284because it has no way of verifying whether a given feature is valid
285or not.  Allowing e2fsck to succeed on a filesystem with an unknown
286feature is a false sense of security for the user.  Refusing to check
287a filesystem with unknown features is a good incentive for the user to
288update to the latest e2fsck.  This also means that anyone adding feature
289flags to ext2 also needs to update e2fsck to verify these features.
290
291Metadata
292--------
293
294It is frequently claimed that the ext2 implementation of writing
295asynchronous metadata is faster than the ffs synchronous metadata
296scheme but less reliable.  Both methods are equally resolvable by their
297respective fsck programs.
298
299If you're exceptionally paranoid, there are 3 ways of making metadata
300writes synchronous on ext2:
301
302- per-file if you have the program source: use the O_SYNC flag to open()
303- per-file if you don't have the source: use "chattr +S" on the file
304- per-filesystem: add the "sync" option to mount (or in /etc/fstab)
305
306the first and last are not ext2 specific but do force the metadata to
307be written synchronously.  See also Journaling below.
308
309Limitations
310-----------
311
312There are various limits imposed by the on-disk layout of ext2.  Other
313limits are imposed by the current implementation of the kernel code.
314Many of the limits are determined at the time the filesystem is first
315created, and depend upon the block size chosen.  The ratio of inodes to
316data blocks is fixed at filesystem creation time, so the only way to
317increase the number of inodes is to increase the size of the filesystem.
318No tools currently exist which can change the ratio of inodes to blocks.
319
320Most of these limits could be overcome with slight changes in the on-disk
321format and using a compatibility flag to signal the format change (at
322the expense of some compatibility).
323
324=====================  =======    =======    =======   ========
325Filesystem block size      1kB        2kB        4kB        8kB
326=====================  =======    =======    =======   ========
327File size limit           16GB      256GB     2048GB     2048GB
328Filesystem size limit   2047GB     8192GB    16384GB    32768GB
329=====================  =======    =======    =======   ========
330
331There is a 2.4 kernel limit of 2048GB for a single block device, so no
332filesystem larger than that can be created at this time.  There is also
333an upper limit on the block size imposed by the page size of the kernel,
334so 8kB blocks are only allowed on Alpha systems (and other architectures
335which support larger pages).
336
337There is an upper limit of 32000 subdirectories in a single directory.
338
339There is a "soft" upper limit of about 10-15k files in a single directory
340with the current linear linked-list directory implementation.  This limit
341stems from performance problems when creating and deleting (and also
342finding) files in such large directories.  Using a hashed directory index
343(under development) allows 100k-1M+ files in a single directory without
344performance problems (although RAM size becomes an issue at this point).
345
346The (meaningless) absolute upper limit of files in a single directory
347(imposed by the file size, the realistic limit is obviously much less)
348is over 130 trillion files.  It would be higher except there are not
349enough 4-character names to make up unique directory entries, so they
350have to be 8 character filenames, even then we are fairly close to
351running out of unique filenames.
352
353Journaling
354----------
355
356A journaling extension to the ext2 code has been developed by Stephen
357Tweedie.  It avoids the risks of metadata corruption and the need to
358wait for e2fsck to complete after a crash, without requiring a change
359to the on-disk ext2 layout.  In a nutshell, the journal is a regular
360file which stores whole metadata (and optionally data) blocks that have
361been modified, prior to writing them into the filesystem.  This means
362it is possible to add a journal to an existing ext2 filesystem without
363the need for data conversion.
364
365When changes to the filesystem (e.g. a file is renamed) they are stored in
366a transaction in the journal and can either be complete or incomplete at
367the time of a crash.  If a transaction is complete at the time of a crash
368(or in the normal case where the system does not crash), then any blocks
369in that transaction are guaranteed to represent a valid filesystem state,
370and are copied into the filesystem.  If a transaction is incomplete at
371the time of the crash, then there is no guarantee of consistency for
372the blocks in that transaction so they are discarded (which means any
373filesystem changes they represent are also lost).
374Check Documentation/filesystems/ext4/ if you want to read more about
375ext4 and journaling.
376
377References
378==========
379
380=======================	===============================================
381The kernel source	file:/usr/src/linux/fs/ext2/
382e2fsprogs (e2fsck)	http://e2fsprogs.sourceforge.net/
383Design & Implementation	http://e2fsprogs.sourceforge.net/ext2intro.html
384Journaling (ext3)	ftp://ftp.uk.linux.org/pub/linux/sct/fs/jfs/
385Filesystem Resizing	http://ext2resize.sourceforge.net/
386Compression [1]_	http://e2compr.sourceforge.net/
387=======================	===============================================
388
389Implementations for:
390
391=======================	===========================================================
392Windows 95/98/NT/2000	http://www.chrysocome.net/explore2fs
393Windows 95 [1]_		http://www.yipton.net/content.html#FSDEXT2
394DOS client [1]_		ftp://metalab.unc.edu/pub/Linux/system/filesystems/ext2/
395OS/2 [2]_		ftp://metalab.unc.edu/pub/Linux/system/filesystems/ext2/
396RISC OS client		http://www.esw-heim.tu-clausthal.de/~marco/smorbrod/IscaFS/
397=======================	===========================================================
398
399.. [1] no longer actively developed/supported (as of Apr 2001)
400.. [2] no longer actively developed/supported (as of Mar 2009)
401