ext2.txt 18 KB

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