Working with JFS 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 4096-byte blocks, but the operating system allocates directory space in 512-byte records.

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.

JFS directory structures

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 of variable length to allow file names the greatest flexibility. However, all directory space is allocated at all times.

No directory entry can 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 that the file occupied in the directory structure is added to the preceding directory entry. The information about the removed directory remains until a new entry fits into the space vacated.

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

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 JFS 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
Opens the directory designated by the DirectoryName parameter and associates a directory stream with it
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
Sets the position of the next readdir subroutine operation on the directory stream
telldir
Returns the current location associated with the specified directory stream

Changing the 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

You can cause the directory named by a process Path parameter to become the effective root directory by using 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 JFS 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
getcwd or getwd
Gets path to current directory
mkdir
Creates a directory
opendir, readdir, telldir, seekdir, rewinddir, or closedir
Perform various actions on directories
rename
Renames a directory
rmdir
Removes a directory