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