Working with JFS2 directories

Directories provide a hierarchical structure to the file system, link files, and i-node subdirectory names. There is no limit on the depth of nested directories.

Disk space is allocated for directories in file system blocks.

Processes can read directories as regular files. However, the kernel can write directories. For this reason, directories are created and maintained by a set of subroutines unique to them.

JFS2 directory structures

A directory contains entries that describe the objects contained in the directory. A directory entry has a fixed length and contains the following:
  • The i-node number
  • The name (up to 22 bytes long)
  • A name length field
  • A field to continue the entry if the name is longer than 22 bytes

The directory entries are stored in a B+ tree sorted by name. The self (.) and parent (..) information is contained in the i-node instead of in a directory entry.

Directories have the following access modes:
Mode Description
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

Working with JFS2 directories (programming)

The following is a list of subroutines available for working with directories:
closedir
Closes a directory stream and frees the structure associated with the DirectoryPointer parameter
mkdir
Creates directories
opendir
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.
readdir
Returns a pointer to the next directory entry
rewinddir
Resets the position of the specified directory stream to the beginning of the directory
rmdir
Removes directories
seekdir
Returns to a position previously obtained with the telldir subroutine
telldir
Returns the current location associated with the specified directory stream

Do not use the open, read, lseek, and close subroutines to access directories.

Changing current directory of a process

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).

Changing the root directory of a process

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.

Processes use the global file system root directory for all path names starting with a / (slash).All path name searches beginning with a / (slash) begin at this new root directory.

Subroutines that control JFS2 directories

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
Perform various actions on directories
getcwd or getwd
Gets path to current directory
mkdir
Creates a directory
rename
Renames a directory
rmdir
Removes a directory