Directories provide a hierarchical structure to the file system and link file and subdirectory names to i-nodes. There is no limit on the depth of nested directories. Disk space is allocated for directories in 4096 byte blocks, but the operating system allocates directory space in 512 byte records.
Processes can read directories as regular files. However, the kernel reserves the right to write directories. For this reason, directories are created and maintained by a set of subroutines unique to them.
See the following sections for more information about directories:
Directories contain a sequence of directory entries. Each directory entry contains three fixed-length fields (the index number associated with the file's i-node, the length of the file name, and the number of bytes for the entry) and one variable length field for the file name. The file name field is null-terminated and padded to 4 bytes. File names can be up to 255 bytes long.
Directory entries are variable-length to allow file names the greatest flexibility. However, all directory space is allocated at all times. See the JFS Directory Record Space Allocation figure for an illustration.
No directory entry is allowed to span 512-byte sections of a directory. When a directory requires more than 512 bytes, another 512-byte record is appended to the original record. If all of the 512-byte records in the allocated data block are filled, an additional data block (4096 bytes) is allotted.
When a file is removed, the space the file occupied in the directory structure is added to the preceding directory entry. The information about the directory remains until a new entry fits into the space vacated.
Every well-formed directory contains the entries . (dot) and .. (dot, dot). The . (dot) directory entry points to the i-node for the directory itself. The .. (dot, dot) directory entry points to the i-node for the parent directory. The mkfs program initializes a file system so that the . (dot) and .. (dot, dot) entries in the new root directory point to the root i-node of the file system.
Access modes for directories have the following meanings:
read | Allows a process to read directory entries. |
write | Allows a process to create new directory entries or remove old ones by using the creat, mknod, link, and unlink subroutines. |
execute | Allows a process to use the directory as a current working directory or to search below the directory in the file tree. |
The mkdir and rmdir subroutines create and remove directories, respectively.
The opendir, readdir, telldir, seekdir, rewinddir and closedir subroutines manipulate directories. The opendir subroutine returns a structure pointer that is used by the readdir subroutine to obtain the next directory entry, by rewinddir to reset the read position to the beginning, and by closedir to close the directory. The seekdir subroutine returns to a position previously obtained with the telldir subroutine. In earlier versions, programs treated directories as regular files and used the open, read, lseek, and close subroutines to access them. This is no longer recommended.
When the system is booted, the first process uses the root directory of the root file system as its current directory. New processes created with the fork subroutine inherit the current directory used by the parent process. The chdir subroutine changes the current directory of a process.
The chdir subroutine parses the path name to ensure that the target file is a directory and that the process owner has permissions to the directory. After the chdir subroutine is run, the process uses the new current directory to search all path names that do not begin with a / (slash).
Processes use the global file system root directory for all path names starting with a / (slash). Processes can change their understanding of the root directory through the chroot subroutine.
Child processes of the calling process consider the directory indicated by the chroot subroutine as the logical root directory of the file system. All path name searches beginning with a / (slash) begin at this new root directory.
Due to the unique nature of directory files, directories are controlled by a special set of subroutines. The following subroutines are designed to control directories:
chdir | Changes the current working directory. | |
chroot | Changes the effective root directory. | |
opendir, readdir, telldir, seekdir, rewinddir, or closedir | ||
Performs various actions on directories. | ||
getcwd or getwd | Gets path to current directory. | |
mkdir | Creates a directory. | |
rename | Renames a directory. | |
rmdir | Removes a directory. |
Files, Directories, and File Systems Programmers Overview
Working with JFS i-nodes describes the internal representation of files and lists the contents of disk i-nodes and in-core i-nodes.
Linking Files and Directories.
Linking for Programmers.