Files, directories and file descriptors
About this lesson
You type a path like /home/ada/todo, and a program gets back a small number, 3, that it uses to read the file. What happened in between? Follow one open() as the kernel walks the path one directory at a time, builds an open file record with an offset, and hands back a file descriptor. Then watch two identical read() calls return different bytes, because the offset moved. The Linux lessons The file system and Disks, partitions and mounts show the same files from the user's side; here we look at what the kernel keeps for them. System calls: crossing into the kernel shows how open() and read() enter the kernel.
A file is just bytes
A file is a numbered row of bytes. Our file holds 10: milk, a newline, eggs, a newline. The disk knows it by a number, inode 42, not by its name. An inode is the record that says where the bytes are.
byte 0 1 2 3 4 5 6 7 8 9
value m i l k \n e g g s \n (10 bytes)
low-level name inode 42
the inode size, owner, permissions,
where the bytes sit on the disk
(the next lesson opens it up)Read the lesson step by step
A file is just bytes
A file is a numbered row of bytes. Our file holds 10: milk, a newline, eggs, a newline. The disk knows it by a number, inode 42, not by its name. An inode is the record that says where the bytes are.
byte 0 1 2 3 4 5 6 7 8 9 value m i l k \n e g g s \n (10 bytes) low-level name inode 42 the inode size, owner, permissions, where the bytes sit on the disk (the next lesson opens it up)A directory is a list
A directory is a file too, but its bytes are a list of pairs: a name and an inode number. Directories hold directories, so they form a tree that starts at the root, written /, which is inode 2 on ext4.
directory entries (name -> inode number) / (2) etc -> 3 home -> 7 home (7) ada -> 19 bob -> 20 ada (19) notes -> 31 todo -> 42 every directory also holds . (itself) and .. (its parent) the name lives in the directory, not in the fileopen() starts at the root
Line 1 asks the kernel, through a system call, to open /home/ada/todo for reading. The path starts with /, so the kernel starts at the root directory, inode 2, and looks up the first name, home. It is inode 7.
path /home/ada/todo names home ada todo start / (inode 2, the root directory) look up home -> inode 7 check may this process search / ? yesOne name at a time
The kernel repeats the same move: read directory 7, find ada, inode 19; read directory 19, find todo, inode 42. One lookup per directory. A missing name stops the walk with the error ENOENT.
directory 7 ada -> inode 19 directory 19 todo -> inode 42 walk 2 -> 7 -> 19 -> 42 missing name ENOENT (no such file) no search permission EACCESThe kernel opens the file
Inode 42 says Ada may read it, so the kernel makes an open file record: which file (inode 42), how it was opened (read only) and the offset, the place the next read starts. The offset begins at 0.
open file record (Linux: an open file description) inode 42 mode read only (O_RDONLY) offset 0 it lives in kernel memory, in a table shared by all processesThe file descriptor is 3
Each process has its own descriptor table. Slots 0, 1 and 2 are already open: standard input, output and error. The kernel takes the lowest free slot, 3, points it at the open file record, and open() returns 3.
this process's descriptor table fd 0 standard input (the terminal) fd 1 standard output (the terminal) fd 2 standard error (the terminal) fd 3 -> open file record (inode 42, offset 0) open("/home/ada/todo", O_RDONLY) returns 3 fd is just an index: only the kernel can follow itread() follows the chain
read(3, buf, 5) follows slot 3 to the open file record. The offset is 0, so the kernel copies bytes 0 to 4, milk and a newline, into buf, adds 5 to the offset and returns 5.
read(3, buf, 5) fd 3 -> open file record -> inode 42 -> bytes offset 0: copy bytes 0..4 "milk\n" offset 0 -> 5 returns 5 (bytes read)Same call, next bytes
Line 4 is the very same call, but the offset is now 5, so it gets bytes 5 to 9: eggs and a newline. The offset moves to 10, the end. A third read would return 0, which means end of file.
read(3, buf, 5) offset 5: copy bytes 5..9 "eggs\n" offset 5 -> 10 (the file has 10 bytes) returns 5 read(3, buf, 5) again returns 0 = end of fileclose() frees the slot
close(3) empties slot 3, so a later open() can reuse the number. No other descriptor points at the open file record, so the kernel frees it too. The file and its directories on the disk are unchanged.
close(3) fd 3 free (the next open may return 3 again) open file record freed: no descriptor refers to it inode 42, bytes unchanged on disk returns 0Why three layers?
Names live in directories, bytes in the file, and the offset in the open file record. So two open() calls on one file get separate offsets, while fork() and dup() share one record and its offset.
the same machinery as a Python simulation: disk = { # inode number -> contents 2: {"etc": 3, "home": 7}, # the root directory 7: {"ada": 19, "bob": 20}, 19: {"notes": 31, "todo": 42}, 42: b"milk\neggs\n", # a plain file: 10 bytes } fds = ["tty", "tty", "tty"] # this process: 0, 1, 2 open def open_(path): ino = 2 # start at the root for name in path.strip("/").split("/"): ino = disk[ino][name] # one lookup per directory entry = {"inode": ino, "offset": 0} # the open file fd = fds.index(None) if None in fds else len(fds) fds[fd:fd + 1] = [entry] # lowest free slot return fd def read(fd, n): f = fds[fd] data = disk[f["inode"]][f["offset"]:f["offset"] + n] f["offset"] += len(data) # the offset moves on return data fd = open_("/home/ada/todo") print(fd, read(fd, 5), read(fd, 5), read(fd, 5)) prints: 3 b'milk\n' b'eggs\n' b''
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What does a directory entry hold?
A directory is a file whose contents are a list of (name, inode number) pairs. The bytes live with the file itself, the offset lives in the open file record, and descriptors belong to processes.
02A process has fds 0, 1 and 2 open and nothing else. It calls open() successfully. Which number does it get?
open() returns the lowest-numbered descriptor not currently open for the process. 0, 1 and 2 are taken, so the answer is 3.
03A file holds the 10 bytes milk\neggs\n. A program opens it and calls read(fd, buf, 5) three times. What do the calls return?
Each read starts at the offset and moves it on by the bytes read: 0 to 5, then 5 to 10. At offset 10 there is nothing left, so the third read returns 0, which means end of file.
04Where does the kernel keep the offset that says where the next read starts?
The open file record holds the offset and the mode. That is why two separate open() calls on one file have separate offsets, while dup() and fork() share one record and so share its offset.
05While resolving /home/ada/todo, the directory home has no entry called ada. What happens?
The walk looks up one name in one directory at a time. A missing name stops it, and open() fails with ENOENT (no such file or directory). Lacking search permission on a directory gives EACCES instead.
Glossary
- File
- A named sequence of bytes that you can read or write. The disk knows it by a number, its inode number.
- Inode
- The record the file system keeps for each file: its size, owner, permissions and where its bytes are. Its number is the file's low-level name.
- Directory
- A file whose contents are a list of entries, each a name and an inode number. Directories inside directories form a tree.
- Root directory
- The top of the tree, written /. On ext2, ext3 and ext4 it is inode 2. An absolute path is resolved starting here.
- Path resolution
- How the kernel turns a path into an inode: one name at a time, looking each one up in the directory found so far.
- File descriptor
- A small integer a process uses to name an open file. It indexes that process's own descriptor table.
- Open file record
- The kernel's record of one open(): which file, how it was opened, and the offset. Linux calls it an open file description.
- Offset
- The position in the file where the next read or write starts. Each read moves it on by the number of bytes read.
Notes and sources
- Inode 2 is the root directory on ext2, ext3 and ext4. The other inode numbers (7, 19, 42 and so on) are made up for the example; on a real disk, ls -i shows them.
- The card shows the lines of main() that matter. The whole program (in the step details) also prints each result; compiled and run on a file holding milk and eggs, it prints fd = 3, read 5: milk, read 5: eggs and read 0.
- The ↵ mark stands for the newline byte (10 in ASCII) that ends each line of the file.
- Every directory also holds the entries . (itself) and .. (its parent). The figure leaves them out to keep the lists short.
- Linux calls the open file record an open file description; textbooks call the table of them the open file table. The descriptor table is private to each process, while the open file records live in one table for the whole system.
- Slots 0, 1 and 2 are open because the shell that started the program handed it the terminal for input, output and errors. A program started another way may get something else in those slots, and then open() can return a different number.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 39: Interlude: Files and Directories
- Linux man-pages: open(2)
- Linux man-pages: read(2)
- Linux man-pages: close(2)
- Linux man-pages: dup(2)
- Linux man-pages: path_resolution(7)
- Linux kernel documentation: ext4 special inodes
- MIT 6.1810 (2024), lecture notes: File systems
- Berkeley CS162, Lecture 4: Abstractions 2: Files and I/O
Quiz Unlocks after the last step.