written 5.4 years ago by |
The simple access to a file is by its path name, as in the open, chdir (change directory), or link system calls. Because the kernel works internally with i-nodes rather than with path names, so it need to converts the path names to i-nodes to access files. The algorithm namei parses the path name one component at a time, converting each component into an i-node based on its name and the directory being searched, and finally returns the i-node of the input path name figure
Algorithm Namei // Converts the path name to i-node
Input : Pathname
Output : i-node in locked form
{
if (Path name initiates from root)
current i-node = root i-node (algorithm iget);
else
current inode = current directory inode (algorithm iget);
while (there is more path names present)
{
read the next path name from input;
confirm the current i-node is having a directory access permission;
if(current inode is root and it's component is "...")
continue; // back to while //
read current i-node;
read directory (current inode)
if( component matches an entry in current i-node)
{
get i-node number for matched component;
release current inode;
current inode = i-node of matched component;
}
else ”Component not present in directory //
return(inode is not there);
}
return (current inode);
}
Every process is associated with (resides in) a current directory; the u area contains a pointer to the current directory i-node. The current directory of the first process in the system, process 0, is the root directory. The current directory of every other process starts out as the current directory of its parent process. Processes change their current directory by executing the chdir (change directory) system call.
All path name searches start from the current directory of the process unless the path name starts with the slash character, signifying that the search should start from the root directory. In either case, the kernel can easily find the mode where the path name search starts: The current directory is stored in the process u area, and the system root i-node is stored in a global variable.
Namei uses intermediate i-nodes as it parses a path name; call them working i-nodes. The mode where the search starts is the first working mode. During each iteration of the namei loop, the kernel makes sure that the working i-node is indeed that of a directory.
Otherwise, the system would violate the assertion that non-directory files can only be leaf nodes of the file system tree. The process must also have permission to search the directory (read permission is insufficient). The user ID of the process must match the owner or group ID of the file, and execute permission must be granted, or the file must allow search to all users. Otherwise the search fails.
The kernel does a linear search of the directory file associated with the working i-node. It is trying to match the path name component to a directory entry name. Starting at byte offset 0, it converts the byte offset in the directory to the appropriate disk block according to algorithm bmap and reads the block using algorithm bread. It searches the block for the path name component, treating the contents of the block as a sequence of directory entries. If it finds a match, it records the mode number of the matched directory entry, releases the block (algorithm brelse) and the old working i-node (algorithm tput), and allocates then i-node of the matched component (algorithm iget).
The new i-node becomes the working i-node. If the kernel does not match the path name with any names in the block, it releases the block, adjusts the byte offset by the number of bytes in a block, converts the new offset to a disk block number (algorithm bmap), and reads the next block. The kernel repeats the procedure until it matches the path name component with a directory entry name, or until it reaches the end of the directory.