Inodes and data blocks
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A disk only knows numbered blocks, yet you open files by name, read from the middle of them and make them grow. How? A Unix-style file system splits every file in three: a name in a directory, an inode holding the file's facts and block pointers, and the data blocks themselves. Follow open, pread and pwrite on a small 64-block disk as the kernel walks from the root inode to a directory, to the file's inode, to the right data block, and takes a free block from the bitmap when the file grows. The Linux lessons The file system and Disks, partitions and mounts show the same disk from the user's side.
A file is three things
On disk a file is split in three. Its name sits in a directory. Its inode, a small fixed-size record, holds the facts about it and where its bytes are. The bytes themselves sit in data blocks of 4096 bytes.
name in a directory: "notes" -> inode 20
inode number 20: type, size, owner, times, links,
and pointers to its data blocks
data blocks 9 and 10: the 6000 bytes of the file
the inode number is the file's real identity;
the name is just an entry that points to itاقرأ الدرس خطوة بخطوة
A file is three things
On disk a file is split in three. Its name sits in a directory. Its inode, a small fixed-size record, holds the facts about it and where its bytes are. The bytes themselves sit in data blocks of 4096 bytes.
name in a directory: "notes" -> inode 20 inode number 20: type, size, owner, times, links, and pointers to its data blocks data blocks 9 and 10: the 6000 bytes of the file the inode number is the file's real identity; the name is just an entry that points to itThe disk's map
Our small disk has 64 blocks of 4 KB. Block 0 is the superblock, block 1 the inode bitmap, block 2 the data bitmap. Blocks 3 to 7 hold 80 inodes of 256 bytes. Blocks 8 to 63 hold data.
block what (OSTEP's vsfs) 0 superblock sizes and where each region starts 1 inode bitmap one bit per inode, 1 = used 2 data bitmap one bit per data block 3-7 inode table 80 inodes x 256 B, 16 per block 8-63 data region file and directory contents data bitmap, blocks 8-15: 1110 0000Inside an inode
An inode records the file's type, size, owner and permissions, times, link count, and a list of block pointers. Pointer 0 names the disk block holding bytes 0 to 4095, pointer 1 the next 4096, and so on. It never stores the name.
inode 20 (256 bytes in block 4) type regular file size 6000 bytes mode owner, permissions times accessed, modified, changed links 1 (names pointing here) ptr 0 block 9 bytes 0 - 4095 ptr 1 block 10 bytes 4096 - 8191 ptr 2 - (empty) ... no file name anywhereopen() starts at the root
open() gets a path, but the disk finds files by inode number. Every path starts at the root directory, whose inode number is always known: 2. So the kernel reads inode 2 from block 3.
line 1 fd = open("/notes", O_RDWR); root inode 2 (fixed, known when the disk is mounted) where 2 x 256 = 512 bytes into the table (this toy counts slots from 0; real ext4 counts from 1) -> block 3, byte 512 inode 2 type dir, ptr 0 = block 8Read the directory
A directory is a file whose data is a list of name and inode number pairs. Inode 2's pointer leads to block 8. The kernel scans it for "notes" and finds inode 20.
block 8 the root directory's entries name inode . 2 .. 2 notes 20 <- foundFetch inode 20
Inodes are numbered slots, so finding one is arithmetic: 20 × 256 = 5120 bytes into the table, which is block 4, byte 1024. The kernel reads it, checks the permissions and returns file descriptor 3.
byte = 20 x 256 = 5120 block = 3 + 5120 / 4096 = 3 + 1 = 4 offset = 5120 mod 4096 = 1024 permissions ok -> open returns the lowest free fd fd = 3 (0, 1 and 2 are already taken) reads so far: inode 2, block 8, inode 20 (3 reads)Which block holds byte 5000?
pread asks for 100 bytes starting at byte 5000. 5000 ÷ 4096 is 1 remainder 904, so pointer 1 tells the kernel: block 10. It reads that block and copies bytes 904 to 1003 into the buffer.
line 2 pread(fd, b, 100, 5000); index = 5000 / 4096 = 1 offset = 5000 mod 4096 = 904 ptr 1 -> block 10 copy block 10, bytes 904-1003 -> b returns 100 bitmaps not touched: reading allocates nothingThe file must grow
pwrite puts 4000 bytes at byte 6000, so the last byte is 9999. 9999 ÷ 4096 is 2, but pointer 2 is empty. The kernel scans the data bitmap for the first 0, finds block 11 free, and sets its bit to 1.
line 3 pwrite(fd, b, 4000, 6000); bytes 6000 - 9999 last 9999 / 4096 = 2 ptr 2 empty bitmap 1110 0000 (blocks 8-15) ^ first 0: block 11 bitmap 1111 0000Write data, then the inode
The bytes go into block 10 (2192 bytes) and block 11 (1808). Then the inode changes: pointer 2 = 11, size = 10000. One call wrote four blocks: bitmap, two data blocks, inode. A crash midway leaves them disagreeing.
ptr 1 block 10 bytes 1904-4095 of it (2192 bytes) ptr 2 block 11 bytes 0-1807 of it (1808 bytes) inode 20 ptr 2 = 11 size 6000 -> 10000 returns 4000 blocks written: data bitmap (2), data 10, data 11, inode table block 4. If the power fails between them, the disk is inconsistent: see crash consistency and journaling, later in this module.Big files: indirect blocks
An inode has room for few pointers. ext2 and ext3 keep 15: twelve direct ones cover 48 KB. The 13th points to an indirect block holding 1024 more pointers, about 4 MB. A double indirect block passes 4 GB. ext4 uses extents instead.
ext2/ext3 inode: 15 pointers of 4 bytes, 4 KB blocks ptr 0-11 direct 12 x 4 KB = 48 KB ptr 12 indirect block 1024 x 4 KB = 4 MB ptr 13 double indirect 1024^2 x 4 KB = 4 GB ptr 14 triple indirect 1024^3 x 4 KB = 4 TB (12 + 1024) x 4 KB = 4144 KB with one indirect block ext4: extents, "start block + length" runs insteadWhy split name and inode?
Because names and files are separate, two names can point to one inode (a hard link; the link count says how many). Fixed-size inodes are found by arithmetic, and pointers let a file's blocks sit anywhere on the disk.
the whole walk as a Python simulation: BLOCK, INODE = 4096, 256 dirs = {8: {".": 2, "..": 2, "notes": 20}} # block 8: the root directory inodes = {2: {"size": 4096, "ptr": [8]}, 20: {"size": 6000, "ptr": [9, 10]}} bitmap = [1, 1, 1, 0, 0, 0, 0, 0] # data blocks 8..15 def where(inum): # inode number -> disk block, byte byte = inum * INODE return 3 + byte // BLOCK, byte % BLOCK def block_of(ino, offset): # file offset -> disk block, byte return ino["ptr"][offset // BLOCK], offset % BLOCK root = inodes[2] # the root is always inode 2 inum = dirs[root["ptr"][0]]["notes"] print("notes is inode", inum, "in block %d at byte %d" % where(inum)) ino = inodes[inum] print("byte 5000 is in block %d at byte %d" % block_of(ino, 5000)) end = 6000 + 4000 while len(ino["ptr"]) * BLOCK < end: # grow: first free block i = bitmap.index(0) bitmap[i] = 1 ino["ptr"].append(8 + i) ino["size"] = max(ino["size"], end) print("pointers", ino["ptr"], "size", ino["size"]) prints: notes is inode 20 in block 4 at byte 1024 byte 5000 is in block 10 at byte 904 pointers [9, 10, 11] size 10000
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01Where is a file's name stored?
A directory's data is a list of (name, inode number) pairs. The inode holds the type, size, owner, times, link count and block pointers, but no name, which is why one inode can have several names (hard links).
02In the lesson's file system, inodes are 256 bytes and the inode table starts at block 3 with 4096-byte blocks. Which block holds inode 20?
20 × 256 = 5120 bytes into the table. 5120 ÷ 4096 = 1 remainder 1024, so it is in the table's second block: 3 + 1 = block 4, at byte 1024.
03A file's inode has pointers [9, 10]. With 4096-byte blocks, which disk block holds byte 5000 of the file?
5000 ÷ 4096 = 1 remainder 904, so the byte is in the file's block number 1. Pointer 1 says that is disk block 10, at byte 904.
04Opening and then reading an existing file never needs to touch which of these?
Bitmaps only matter when blocks or inodes are allocated or freed. Opening and reading allocate nothing, so the data bitmap is not consulted. The open does walk the directory entries on the path, and the read uses the inode and the data block.
05With 4 KB blocks and 4-byte pointers, ext2 and ext3 inodes have 12 direct pointers and one single indirect pointer before the double one. How much of a file do they cover?
12 direct pointers cover 12 × 4 KB = 48 KB. The indirect block holds 4096 ÷ 4 = 1024 more pointers, another 4 MB, so (12 + 1024) × 4 KB = 4144 KB. The double indirect pointer takes files past 4 GB.
المسرد
- Block
- The unit a file system reads and writes, here 4096 bytes. The disk is a numbered row of blocks.
- Inode
- A small fixed-size record (here 256 bytes) for one file: its type, size, owner and permissions, times, link count and pointers to its data blocks. It does not hold the name.
- Inode number
- The number that names an inode, also called the i-number. Because inodes are a fixed size in a table, the number tells the kernel exactly where the inode is.
- Directory
- A file whose data is a list of entries, each a name and an inode number.
- Superblock
- The first block of the file system. It records how big each region is and where it starts.
- Bitmap
- One bit per inode (inode bitmap) or per data block (data bitmap): 1 means in use, 0 means free.
- Indirect block
- A data block that holds pointers instead of file bytes, so a file can have more blocks than fit in its inode.
- Extent
- A run of blocks named by its first block and its length. ext4 stores extents in place of one pointer per block.
الملاحظات والمصادر
- The disk is the "very simple file system" from OSTEP chapter 40: 64 blocks of 4 KB with the same regions in the same order. Real file systems are bigger and split the disk into groups, but the parts are the same.
- The bitmaps on the figure show only the eight bits that matter: inodes 16 to 23 and data blocks 8 to 15. Each real bitmap fills a whole 4 KB block.
- ext4 also uses 256-byte inodes by default (ext2 and ext3 used 128). Its 60-byte block field holds either the classic 15 pointers or, by default, an extent tree.
- The counts of reads and writes assume nothing is cached. In practice the kernel keeps recently used blocks in memory (the buffer cache, a later lesson), so a second open of the same file may not touch the disk at all.
- The program is real C99 with POSIX. The card shows the three lines that matter; the full file declares char b[4096] and int fd. Run on a 6000-byte file named /notes, it leaves the file 10000 bytes long.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 40: File System Implementation
- Linux kernel documentation: ext4 index nodes
- Linux kernel documentation: ext4 direct and indirect block addressing
- Linux kernel documentation: ext4 special inodes
- Linux man-pages: inode(7)
- Linux man-pages: open(2)
- Linux man-pages: pread(2)
- MIT 6.1810 (2024), lecture notes: File system
- Berkeley CS162, Lecture 20: file systems (file header and inode)
اختبار يتاح بعد الخطوة الأخيرة.