Directories and Links
Optional readings for this topic from Operating Systems: Principles and Practice: Sections 13.1-13.2.
Naming: how do users refer to their files? How does OS find file, given name?
First step: inode has to be stored on disk, so it will persist across system reboots.
Early UNIX versions: all inodes stored in a fixed-size array on disk.
- Originally: entire inode array was at the outer edge of the disk. Result: long seeks between inodes and file data. First improvement: place inode array mid-way across disk. Second improvement: many small inode arrays spread across disk, so inodes can be near to file data.
Space for inodes is fixed when the disk is initialized, and can't be changed.
UNIX/Linux terms:
- Index of inode in the inode array: i-number. Internally the OS uses the i-number as an identifier for the file.
When a file is open, its inode is kept in main memory. When the file is closed, the inode is written back to disk.
File naming: users want to use text names to refer to files. Special disk structures called directories are used to map names to i-numbers.
Early approaches to directory management:
- A single directory for the entire disk:
- Many early personal computers worked this way.
- A single directory for each user (e.g. TOPS-10):
- Avoids problems between users, but still makes it hard to organize information.
Modern systems support hierarchical directory structures. UNIX/Linux approach:
- Directories are stored on disk just like regular files (i.e. inode with 14 pointers, etc.) except inode has special "type" bit set to indicate that it's a directory.
- A simple approach: each directory contains <name, i-number> pairs in no particular order.
- The file referred to by the i-number may be another directory. Hence, get hierarchical tree structure. Names have slashes separating the levels of the tree.
- There is one special directory, called the root. This directory has no name; it has i-number 1 (i-number 0 is not used).
- Only the operating system can read and write directories.
Working directories
Cumbersome to specify the full path name for all files.
OS remembers the i-number for one distinguished directory per process, called the working directory.
If a file name doesn't start with "/" then it is looked up starting in the working directory.
Names starting with "/" are looked up starting in the root directory.
Links
UNIX hard links:
- It is possible for more than one directory entry to refer to a single file.
- UNIX uses reference counts in inodes to keep track of hard links referring to a file.
- Files are deleted when the last directory entry goes away.
- Must prevent circularities (don't allow links to directories).
Symbolic links:
- A file whose contents are another file name.
- Stored on disk like regular files, but with special flag set in inode.
- If a symbolic link is encountered during file lookup,
prepend link value to remaining path, continue lookup.
- If symbolic link starts with "/", resume lookup in root directory
- Otherwise, continue lookup in same directory as the one containing the symbolic link.