Sub topic 13
Directory Structures
This chapter assumes you know what a file is and what attributes it carries (name, size, type, location, timestamps), and that you have seen basic system calls such as open and unlink. A directory is the structure the operating system uses to map file names to files. Here we compare the four classic organizations that GATE CS asks about: single-level, two-level, tree-structured and acyclic-graph.
What a directory does
A directory is a special file whose contents are a table of entries. Each entry maps a name to the information needed to find the file. Depending on the file system, the entry holds the file's attributes directly (FAT and early MS-DOS) or only a pointer to them, such as an inode number (Unix).
The operations a directory structure must support efficiently are:
- Search for a file by name.
- Create a file.
- Delete a file.
- List the directory.
- Rename a file.
- Traverse the whole file system.
Each structure below is a different trade-off among naming freedom, grouping, sharing and implementation cost.
Single-level directory
All files of all users live in one directory.
- It is simple to implement and fast to understand.
- Every file name must be unique across the whole system. With many users, name collisions are almost certain.
- There is no grouping. Hundreds of files form one flat list, so searching and listing become slow.
Two-level directory
A master file directory (MFD) holds one entry per user. Each entry points to that user's own user file directory (UFD).
- Different users can reuse the same file name, because a name only has to be unique within one UFD.
- A path now has two parts: user and file name, such as
alice/report.txt. - Users are isolated by default, which gives some protection. It also makes sharing awkward. To use another user's file you must name it by its full path.
- A user still cannot group their own files into subcategories.
- System programs (compilers, utilities) are a problem. Either each user gets a copy, or the system defines a search path of directories to look in.
The search path idea is important. It generalizes in the tree structure to the PATH environment variable.
Tree-structured directory
The two-level scheme generalizes into a tree of arbitrary height. There is one root, and every file has exactly one parent directory and therefore exactly one path name.
A directory is just a file with a flag bit marking it as a directory. Users can create subdirectories and group files by project or type.
- An absolute path starts at the root, such as
/home/alice/os/notes.txt. - A relative path starts at the process's current working directory. If the current directory is
/home/alice, thenos/notes.txtnames the same file. .means the current directory and..means its parent.
Deleting a directory raises a design choice. Some systems refuse to delete a non-empty directory, and the user must empty it first (the MS-DOS rmdir behavior). Others delete the whole subtree on request (rm -r). The second is convenient but dangerous.
Acyclic-graph directory
A tree forbids sharing. If Alice and Bob work on the same project, a shared subdirectory should appear in both of their trees. An acyclic graph allows a file or subdirectory to have several names or several parents, but forbids cycles.
There are two common ways to implement sharing in Unix-style systems.
| Property | Hard link | Symbolic (soft) link |
|---|---|---|
| What it stores | Another directory entry pointing to the same inode | A small file containing a path name |
| Inode shared | Yes, same inode number | No, the link has its own inode |
| Survives deletion of the original name | Yes, the data stays until the last link goes | No, it becomes a dangling link |
| Can cross file systems | No, inode numbers are local to one file system | Yes |
| Can link directories | Usually forbidden, to prevent cycles | Allowed |
| Extra cost | Needs a reference count in the inode | Extra path resolution on each access |
Deletion with hard links
With sharing, deleting a name must not destroy data that another name still uses. The inode keeps a link count. unlink removes one directory entry and decrements the count. The file's blocks are freed only when the count reaches zero (and no process still has the file open).
With symbolic links, deleting the target leaves the link pointing at nothing. Following it fails with "no such file".
Why cycles are a problem
If a directory could contain a link back to one of its ancestors, then:
- A traversal such as a search or backup would loop forever unless it tracks visited nodes.
- Reference counts would never reach zero for an isolated cycle, since each directory in the cycle still has a link from another. The space would be leaked, and recovering it needs garbage collection, which is expensive for a disk.
Systems avoid cycles by refusing hard links to directories, or by running a cycle check when a link is added. Symbolic links can create cycles, so path resolution limits the number of links it follows (Linux returns ELOOP after 40).
The diagram shows a file shared through two names, and how the link count changes.
Path resolution and its cost
To open /home/alice/os/notes.txt, the kernel starts at the root directory and, for each component, reads a directory and searches it for the next name.
- Read the root directory, find
home. - Read
home, findalice. - Read
alice, findos. - Read
os, findnotes.txtand its inode.
Each step can need a disk read for the directory data and one for its inode. Longer paths cost more, which is why systems cache directory entries in memory.
Directory search itself depends on the data structure:
- A linear list is simple but takes O(n) per lookup.
- A hash table gives near O(1) lookup, but needs collision handling and a fixed or resizable size.
- A B-tree (used by several modern file systems) gives O(log n) lookup and keeps entries sorted.
Worked example: link counts
Suppose /d1/f is created, then ln /d1/f /d2/g makes a hard link, then ln -s /d1/f /d3/h makes a symbolic link. Then rm /d1/f runs.
- After creation, the link count is 1.
- After the hard link, the link count is 2.
- The symbolic link does not change the count. It is a separate file holding the string
/d1/f. - After
rm /d1/f, the count is 1. The data is intact and reachable through/d2/g. - The symbolic link
/d3/hnow dangles, because the path/d1/fno longer exists. Reading/d2/gstill works.
Common errors
- Saying a hard link is a copy. It is another name for the same file, so a write through one name is visible through the other.
- Forgetting that a symbolic link has its own inode and its own size, equal to the length of the stored path.
- Claiming a tree allows sharing. A tree gives each file one parent. Sharing requires the acyclic-graph structure.
- Assuming deletion frees blocks whenever a name is removed. It only does so when the link count reaches zero.
Comparison
| Structure | Unique names needed | Grouping | Sharing | Main weakness |
|---|---|---|---|---|
| Single-level | Across the entire system | None | Trivial (everything visible) | Collisions, no organization |
| Two-level | Within each user | By user only | Awkward, by full path | No subgrouping |
| Tree | Within each directory | Arbitrary depth | None, one parent per file | Cannot share without copying |
| Acyclic graph | Within each directory | Arbitrary depth | Links | Deletion and dangling links |
Exercise
A file has link count 3 (three hard links in different directories) and one symbolic link pointing to one of those names. You remove that name with unlink. What are the link count, the state of the data, and the state of the symbolic link?
Show answer
The link count drops to 2, and the data stays on disk because two names still reference the inode. The symbolic link now dangles, since it stores the path of the removed name, and following it fails. The other two hard links still work.
Exercise
Why do most systems forbid hard links to directories, and what do they allow instead?
Show answer
A hard link to a directory could create a cycle. Traversals would loop, and reference counts in an unreachable cycle would never reach zero, leaking space. Systems allow symbolic links to directories instead, and limit how many symbolic links path resolution follows.