Understanding Inodes and File System Internals
1. Understanding Inode Structure
| Field | Description | Size (bytes) |
|---|---|---|
| inode number | Unique identifier for the inode within the filesystem | 4 or 8 bytes |
| file type | Indicates whether it's a regular file, directory, symlink, etc. | 2 bits |
| permissions | Read, write, execute permissions for owner, group, others | 9 bits |
| link count | Number of hard links pointing to this inode | 2 bytes |
| owner ID (UID) | User ID of the file owner | 4 bytes |
| group ID (GID) | Group ID of the file owner | 4 bytes |
| size in bytes | Size of the file in bytes | 8 bytes |
| block count | Number of 512-byte blocks allocated to the file | 8 bytes |
| access time (atime) | Time of last access | 8 bytes |
| modify time (mtime) | Time of last modification | 8 bytes |
| change time (ctime) | Time of last metadata change | 8 bytes |
| birth time (btime) | Time of creation (not available on all systems) | 8 bytes |
| block pointers | Pointers to data blocks (typically 12 direct + 1 indirect + 1 double indirect) | 15 * 4 or 8 bytes |
Note: The inode structure varies by filesystem type (ext2, ext3, ext4, XFS, etc.). Some fields like
btime are not supported on older filesystems.
2. Viewing Inode Information
| Command | Description | Example Output |
|---|---|---|
| ls -i | Lists files with their inode numbers | |
| stat | Displays detailed file or filesystem status including inode info | |
| find -inum | Searches for files by inode number | /home/user/file1.txt |
Note: Inode numbers are unique within a filesystem but not across different filesystems.
3. Checking Inode Usage
| Command | Description | Example Output |
|---|---|---|
| df -i | Display inode usage for all mounted filesystems | |
| ls -li | List files with inode numbers (useful for checking individual inodes) | 123456 -rw-r--r-- 1 user group 1024 Jan 1 12:00 file.txt |
| find / -inum <inode_number> | Find files by inode number | /home/user/file.txt |
Note: Inode usage can be checked with
df -i, which shows the number of inodes used and available on each filesystem.
4. Understanding Hard Links vs Inodes
| Aspect | Hard Links | Inodes |
|---|---|---|
| Definition | A directory entry that points to the same inode as another file | Metadata structure storing file attributes and pointers to data blocks |
| Count | Multiple directory entries can point to the same inode | Each file has one unique inode |
| File Size | Does not affect file size | Represents actual file size and metadata |
| Permissions | Same permissions as original file | Permissions are separate from hard link permissions |
| Deletion | Original file remains accessible until all hard links are removed | Removed only when no more hard links exist |
| Symbolic Links | Cannot be created to directories (unless root) | Not applicable to inodes |
Note: Hard links cannot span across different filesystems.
5. Finding Files by Inode Number
| Command | Description | Example |
|---|---|---|
| find | Searches for files and directories by inode number using the -inum option. |
find /path/to/search -inum 12345 |
| ls -i | Lists files with their inode numbers in the current directory. | ls -i |
| stat | Displays detailed information about a file, including its inode number. | stat filename |
Note: Inode numbers are unique within a filesystem but not across different filesystems.
6. Understanding File Descriptors
| Descriptor Type | Description | Default Value |
|---|---|---|
| stdin | Standard input stream | 0 |
| stdout | Standard output stream | 1 |
| stderr | Standard error stream | 2 |
Note: File descriptors are integers used to reference open files or I/O streams. They are managed by the kernel and can be duplicated, redirected, or closed.
7. Viewing Open File Descriptors
| Command | Description | Notes |
|---|---|---|
| lsof | Lists open files and their associated processes | Most common tool for viewing file descriptors |
| /proc/<pid>/fd/ | Directory containing symbolic links to open files | Each link represents an open file descriptor |
| ls -l /proc/<pid>/fd/ | Lists file descriptors for a specific process | Shows file paths and permissions |
Note: The
/proc/<pid>/fd/ directory provides a snapshot of all open file descriptors for the specified process ID.
8. Understanding Sparse Files
Sparse files are files that contain large sections of null bytes (zeros), but do not actually allocate disk space for these sections. Instead, the filesystem only allocates space for actual data, making sparse files efficient in terms of disk usage.
| Property | Description | Notes |
|---|---|---|
| File Size vs Disk Usage | Actual size of file on disk vs logical file size | Logical size can be much larger than physical space used |
| Creation Method | Using tools like dd or fallocate |
Can be created with zero-filled regions |
| Filesystem Support | Most modern Linux filesystems (ext4, XFS) | Not supported on FAT or older filesystems |
Note: The
ls -l command shows the logical file size, while du -h shows actual disk usage.
Key Points About Sparse Files
- Sparse files do not consume disk space for zero regions.
- They are useful for large files with many empty sections (e.g., databases, virtual machines).
- Operations like
cporddmay not preserve sparseness.
9. Managing Extended Attributes
| Attribute Name | Value Type | Description | Commands |
|---|---|---|---|
| user.comment | String | User-defined comment for a file | setfattr -n user.comment -v "Important file" filename |
| security.selinux | String | SELinux security context | getfattr -n security.selinux filename |
| trusted.* | Binary | Trusted namespace for system use | setfattr -n trusted.user_data -v "secret" filename |
Example: Setting and Retrieving Extended Attributes
setfattr -n user.author -v "John Doe" /path/to/file
getfattr -n user.author /path/to/file
Note: Extended attributes are supported on filesystems like ext2, ext3, ext4, and XFS.
Warning: Writing to
trusted.* attributes requires root privileges.
10. Understanding File System Journaling
| Aspect | Description | Notes |
|---|---|---|
| Journaling | Logging of file system changes before they are committed to the main file system | Protects against data corruption during unexpected shutdowns or crashes |
| Journal Location | Stored in a separate area of the file system or on a dedicated partition | Can be located in a separate area to avoid conflicts with the main file system |
| Journal Types | Transaction-based logging, metadata-only journaling, and data journaling | Transaction-based logging ensures atomicity of operations |
| Journaling Benefits | Reduces recovery time, improves data consistency, and prevents file system corruption | Improves reliability of file systems like ext3, ext4, and XFS |
| Journaling Overhead | Minimal performance impact in most cases | Can cause slight delays during write operations due to logging |
Note: Journaling is enabled by default in ext4 and XFS file systems.
Warning: Disabling journaling can lead to data loss or corruption if the system crashes unexpectedly.