Ext2: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
Line 263: Line 263:
Like blocks, each inode has a numerical address. It is extremely important to note that unlike block addresses, '''inode addresses start at 1'''.
Like blocks, each inode has a numerical address. It is extremely important to note that unlike block addresses, '''inode addresses start at 1'''.


With Ext2 versions prior to Major version 1, inodes 1 to 10 are reserved and should be in an allocated state. Starting with version 1, the first non-reserved inode is indicated via a field in the Superblock. Of the reserved inodes, number 2 has subjectively has the most significance as it is used for the root directory. Inode 1 keeps track of bad blocks, but it does
With Ext2 versions prior to Major version 1, inodes 1 to 10 are reserved and should be in an allocated state. Starting with version 1, the first non-reserved inode is indicated via a field in the Superblock. Of the reserved inodes, number 2 has subjectively has the most significance as it is used for the root directory.

not have any special status in the Linux kernel.
Inodes have a fixed size of either 128 for version 0 Ext2 file systems, or as dictated by the field in the Superblock for version 1 file systems. All inodes reside in inode tables that belong to block groups. Therefore, looking up an inode is simply a matter of determining which block group it belongs to and indexing that block group's inode table.


=== Determining which Block Group contains an Inode ===
=== Determining which Block Group contains an Inode ===
Line 285: Line 286:


where INODE_SIZE is either fixed at 128 if VERSION < 1 or defined by a field in the Superblock if VERSION >= 1.0, and BLOCK_SIZE is defined by a field in the Superblock.
where INODE_SIZE is either fixed at 128 if VERSION < 1 or defined by a field in the Superblock if VERSION >= 1.0, and BLOCK_SIZE is defined by a field in the Superblock.

Finally, mask and shift as necessary to extract only the inode data from the containing block.

=== Reading the contents of an inode ===
Each inode contains 12 direct pointers, one singly indirect pointer, one doubly indirect block pointer, and one triply indirect pointer. The direct space "overflows" into the singly indirect space, which overflows into the doubly indirect space, which overflows into the triply indirect space.

'''Direct Block Pointers''': There are 12 direct block pointers. If valid, the value is non-zero. Each pointer is the block address of a block containing data for this inode.

'''Singly Indirect Block Pointer''': If a file needs more than 12 blocks, a separate block is allocated to store the block addresses of the remaining data blocks needed to store its contents. This separate block is called an indirect block because it adds an extra step (a level of indirection) between an inode and its data. The block addresses stored in the block are all 32-bit, and the capacity of stored addresses in this block is a function of the block size. The address of this indirect block is stored in the inode in the "Singly Indirect Block Pointer" field.

'''Doubly Indirect Block Pointer''': If a file has more blocks than can fit in the 12 direct pointers and the indirect block, a double indirect block is used. A double indirect block is an extension of the indirect block described above only now we have two intermediate blocks between the inode and data blocks. The inode structure has a "Doubly Indirect Block Pointer" field that points to this block if necessary.

'''Triply Indirect Block Pointer''': Lastly, if a file needs still more space, it can use a triple indirect block. Again, this is an extension of the double indirect block. So, a triple indirect block contains addresses of double indirect blocks, which contain addresses of single indirect blocks, which contain address of data blocks. The inode structure has a "Triply Indirect Block Pointer" field that points to this block if present.

[http://en.wikipedia.org/wiki/File:Ext2-inode.gif This image from Wikipedia] illustrates what is described above pretty well


=== Inode Data Structure ===
=== Inode Data Structure ===


{| {{Wikitable}}
{| {{Wikitable}}
! Starting
Byte
! Ending
Byte
! Size
in Bytes
! Field Description
|-
|-
| 0 || 1 || 2 || Type and Permissions ([[#Inode_Type_and_Permissions|see below]])
! Byte Range
! Description
|-
|-
| 0–1 || File mode (type and permissions)
| 2 || 3 || 2 || User ID
|-
|-
| 2–3 || Lower 16 bits of user ID
| 4 || 7 || 4 || Lower 32 bits of size in bytes
|-
|-
| 8 || 11 || 4 || Last Access Time (in [http://en.wikipedia.org/wiki/Unix_time POSIX time])
| 4–7 || Lower 32 bits of size in bytes
|-
|-
| 12 || 15 || 4 || Last Change Time (in [http://en.wikipedia.org/wiki/Unix_time POSIX time])
| 8–11 || Access Time
|-
|-
| 16 || 19 || 4 || Last Modification time (in [http://en.wikipedia.org/wiki/Unix_time POSIX time])
| 12–15 || Change Time
|-
|-
| 20 || 23 || 4 || Deletion time (in [http://en.wikipedia.org/wiki/Unix_time POSIX time])
| 16–19 || Modification time
|-
|-
| 24 || 25 || 2 || Group ID
| 20–23 || Deletion time
|-
|-
| 26 || 27 || 2 || Count of hard links (directory entries) to this inode. When this reaches 0, the data blocks are marked as unallocated.
| 24–25 || Lower 16 bits of group ID
|-
|-
| 28 || 31 || 4 || Count of disk sectors (not Ext2 blocks) in use by this inode, not counting the actual inode structure nor directory entries linking to the inode.
| 26–27 || Link count
|-
|-
| 32 || 35 || 4 || Flags ([[#Inode_Flags|see below]])
| 28–31 || Sector count
|-
|-
| 36 || 39 || 4 || (Unused)
| 32–35 || Flags
|-
|-
| 40 || 43 || 4 || Direct Block Pointer 0
| 36–39 || Unused
|-
|-
| 44 || 47 || 4 || Direct Block Pointer 1
| 40–87 || 12 direct block pointers
|-
|-
| 48 || 51 || 4 || Direct Block Pointer 2
| 88–91 || 1 single indirect block pointer
|-
|-
| 52 || 55 || 4 || Direct Block Pointer 3
| 92–95 || 1 double indirect block pointer
|-
|-
| 56 || 59 || 4 || Direct Block Pointer 4
| 96–99 || 1 triple indirect block pointer
|-
|-
| 60 || 63 || 4 || Direct Block Pointer 5
| 100–103 || Generation number (NFS)
|-
|-
| 64 || 67 || 4 || Direct Block Pointer 6
| 104–107 || Extended attribute block (File ACL)
|-
|-
| 108–111 || Upper 32 bits of size / Directory ACL Yes /
| 68 || 71 || 4 || Direct Block Pointer 7
|-
|-
| 72 || 75 || 4 || Direct Block Pointer 8
| 112–115 || Block address of fragment
|-
|-
| 76 || 79 || 4 || Direct Block Pointer 9
| 116–116 || Fragment index in block
|-
|-
| 80 || 83 || 4 || Direct Block Pointer 10
| 117–117 || Fragment size
|-
|-
| 84 || 87 || 4 || Direct Block Pointer 11
| 118–119 || Unused
|-
|-
| 88 || 91 || 4 || Singly Indirect Block Pointer (Points to a block that is a list of block pointers to data)
| 120–121 || Upper 16 bits of user ID
|-
|-
| 92 || 95 || 4 || Doubly Indirect Block Pointer (Points to a block that is a list of block pointers to Singly Indirect Blocks)
| 122–123 || Upper 16 bits of group ID
|-
|-
| 96 || 99 || 4 || Triply Indirect Block Pointer (Points to a block that is a list of block poitners to Doubly Indirect Blocks)
| 124–127 || Unused
|}

Each inode has a static number of fields, and additional information might be stored in extended attributes and indirect block pointers. The allocation status of an inode is determined using the inode bitmap, whose location is given in the group descriptor.

Ext2, like UFS, was designed for efficiency of small files. Therefore, each inode can store the addresses of the first 12 blocks that a file has allocated. These are called direct pointers. If
a file needs more than 12 blocks, a block is allocated to store the remaining addresses. The pointer to the block is called an indirect block pointer. The addresses in the block are all four
bytes, and the total number in each block is based on the block size. The indirect block pointer is stored in the inode.

If a file has more blocks than can fit in the 12 direct pointers and the indirect block, a double indirect block is used. A double indirect block is when the inode points to a block that
contains a list of single indirect block pointers, each of which point to blocks that contain a
list of direct pointers. Lastly, if a file needs still more space, it can use a triple indirect block
pointer. A triple indirect block contains addresses of double indirect blocks, which contain
addresses of single indirect blocks. Each inode contains 12 direct pointers, one single
indirect pointer, one double indirect block pointer, and one triple indirect pointer.

An inode also contains the file's size, ownership, and temporal information. The size value in
newer versions of ExtX is 64 bits, but older versions had only 32 bits and therefore could not
handle files over 4GB. Newer versions utilize an unused field for the upper 32 bits of the size
value and set a read-only compatible feature flag when a large file exists.

"Ownership" information is stored using the user and group ID.

=== Directories ===
Directories are files which contains information needed to find files within the filesystem. The root directory is Inode 2.





==== Filemode flags ====
{| {{Wikitable}}
|+ Bits 0-8
|-
|-
| 100 || 103 || 4 || Generation number (Primarily used for NFS)
! Permission Flag
! In Octal
! Description
|-
|-
| 104 || 107 || 4 || Extended attribute block (File ACL)
| 0x001 || 0001 || Other—execute permission
|-
|-
| 108 || 111 || 4 || Upper 32 bits of size / Directory ACL Yes /
| 0x002 || 0002 || Other—write permission
|-
|-
| 112 || 115 || 4 || Block address of fragment
| 0x004 || 0004 || Other—read permission
|-
|-
| 116 || 116 || 1 || Fragment index in block
| 0x008 || 0010 || Group—execute permission
|-
|-
| 0x010 || 0020 || Group—write permission
| 117 || 117 || 1 || Fragment size
|-
|-
| 0x020 || 0040 || Group—read permission
| 118 || 119 || 2 || (Unused)
|-
|-
| 120 || 121 || 2 || Upper 16 bits of user ID
| 0x040 || 0100 || User—execute permission
|-
|-
| 122 || 123 || 2 || Upper 16 bits of group ID
| 0x080 || 0200 || User—write permission
|-
|-
| 0x100 || 0400 || User—read permission
| 124 || 127 || 4 || (Unused)
|}
|}


==== Inode Type and Permissions ====
{| {{Wikitable}}
{| {{Wikitable}}
|+The type indicator occupies the top hex digit (bits 15 to 12) of this 16-bit field
|+ Bits 9-11
! Type value
|-
in hex
! Flag Value
! Type Description
! In Octal
! Description
|-
| 0x200 || 01000 || Sticky bit
|-
| 0x400 || 02000 || Set group ID
|-
| 0x800 || 04000 || Set user ID
|}

Bits 12-15

{| {{Wikitable}}
|-
! Type Value
! Description
|-
|-
| 0x1000 || FIFO
| 0x1000 || FIFO
Line 435: Line 409:
| 0xC000 || Unix socket
| 0xC000 || Unix socket
|}
|}
{| {{Wikitable}}

|+Permissions occupy the bottom 12 bits of this 16-bit field
==== Inode flags ====
! Permission
value in hex
! Permission
value in octal
! Permission Description
|-
| 0x001 || 00001 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Other—execute permission]
|-
| 0x002 || 00002 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Other—write permission]
|-
| 0x004 || 00004 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Other—read permission]
|-
| 0x008 || 00010 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Group—execute permission]
|-
| 0x010 || 00020 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Group—write permission]
|-
| 0x020 || 00040 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions Group—read permission]
|-
| 0x040 || 00100 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions User—execute permission]
|-
| 0x080 || 00200 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions User—write permission]
|-
| 0x100 || 00400 || [http://en.wikipedia.org/wiki/Filesystem_permissions#Traditional_Unix_permissions User—read permission]
|-
| 0x200 || 01000 || [http://en.wikipedia.org/wiki/Sticky_bit Stick Bit]
|-
| 0x400 || 02000 || Set group ID
|-
| 0x800 || 04000 || Set user ID
|}
==== Inode Flags ====
{| {{Wikitable}}
{| {{Wikitable}}
|-
|-
Line 456: Line 461:
| 0x00000040 || File is not included in 'dump' command
| 0x00000040 || File is not included in 'dump' command
|-
|-
| 0x00000080 || A-time is not updated
| 0x00000080 || Last accessed time should not updated
|-
|-
| ... || (Reserved)
| 0x00001000 || Hash indexed directory
|-
|-
| 0x00010000 || Hash indexed directory
| 0x00002000 || File data is journaled with Ext3
|-
| 0x00020000 || AFS directory
|-
| 0x00040000 || Journal file data
|}
|}
=== Directories ===
Directories are inodes which contain some number of "entries" as their contents. These entries are nothing more than a name/inode pair. For instance the inode corresponding to the root directory might have an entry with the name of "etc" and an inode value of 50. A directory inode stores these entries in a linked-list fashion in its contents blocks.


The root directory is Inode 2.

=== Directory Information ===
=== Directory Entry ===


{| {{Wikitable}}
{| {{Wikitable}}
! Starting
Byte
! Ending
Byte
! Size
in Bytes
! Field Description
|-
|-
| 0 || 3 || 4 || Inode
! Byte Range
! Description
|-
| 0–3 || Inode
|-
| 4-5 || Record Length (Will get you to the next record)
|-
|-
| 6 || Name Length
| 4 || 5 || 2 || Record Length (Will get you to the next record)
|-
|-
| 6 || 6 || 1 || Name Length least-significant 8 bits
| 7 || File Type
|-
|-
| 7 || 7 || 1 || Type (only if the feature bit for "directory entries have file type byte" is set, else this is the most-significant 8 bits of the Name Length)
| 8-X || File name data
|-
|-
| 8 || 8+N-1 || N || Name characters
|}
|}