written 5.7 years ago by |
Structure of files
The i-node has the table of contents which locate a file's data on disk. Since each block on a disk is identifying by number, the table of contents consists of a set of disk block numbers. If the data in a file were stored in a contiguous section of the disk (that is, the file occupied a linear sequence of disk blocks), then storing the block address of start location and the file size in the i-node would sufficient to access all the data in the file.
However, such an allocation policy would not allow for simple expansion and contraction of files in the file system without running the risk of fragmenting free storage area on the disk. Furthermore, the kernel would have to allocate and reserve contiguous space in the file system before allowing operations that would increase the file size.
For example, suppose a user creates three files, A, B and C. Each file is consisting of 10 disk blocks of storage. Suppose the system allocated storage for the three files contiguously. If the user then wishes to add 5 blocks of data to the middle file, B, the kernel would have to copy file B to a place in the file system that had room for 15 blocks of storage. To handle such expense an operation, the disk blocks previously occupied by file B's data would be unusable except for files smaller than 10 blocks (Figure).
The kernel could minimise fragmentation of storage space by periodically running garbage collection procedures to compact available storage, but that would place an added drain on processing power. To keep the i-node structure small yet still allow large files, the table of contents of disk blocks conforms to that shown in Figure 2.4.2(b). The UNIX system runs with 13 entries in the i-node table of contents, but the principles are independent of the number of entries.
The blocks marked "direct" in the figure contain the numbers of disk blocks that contain real data. The block mention as "single indirect" refers to a block that contains a list of direct block numbers. To access the data via the indirect block, the kernel must read the indirect block. It has to find the appropriate direct block entry, and then read the direct block to find the data. The block marked "double indirect" contains a list of indirect block numbers, and the block marked "triple indirect" contains a list of double indirect block numbers.
Direct & Indirect blocks in i-node
DIRECTORIES
Directories are the files that give the file system its hierarchical structure; they play a vital role in conversion of a file name to an i-node number. A directory is a file whose data is a sequence of entries, each entry consisting of an i-node number and the name of a file contained in the directory.
A path name is a null terminated character string divided into separate components by the slash ("/") character. Each component except the last must be the name of a directory, but the last component may be a non-directory file. UNIX System V restricts component names to a maximum of 14 characters; in that a 2 byte entry for the i-node number, and the size of a directory entry is 16 bytes.
Directory layout of /etc
Figure denoted the layout of the directory "etc". Every directory contains the file names dot and dot-dot ("." and "..") whose i-node numbers are those of the directory and its parent directory, respectively. The i-node number of "." in "/etc" is located at offset 0 in the file, and its value is 83. The i-node number of ".." is located at offset 16, and its value is 2, Directory entries may be empty, indicated by an i-node number of 0.
For example, the entry at address 224 in "/etc" is empty, although it once contained an entry for a file named "crash". The program mkfs initializes a file system so that "." and ".." of the root directory have the root i-node number of the file system.
The kernel stores data for a directory same as it stores data for an ordinary file, using the i-node structure and levels of direct and indirect blocks. Directory reading by the processes is same like a regular file reading, but the kernel reserves exclusive right to write a directory, thus insuring its correct structure.
The access permissions of a directory have the following meaning: read permission on a directory allows a process to read a directory; write permission allows a process to create new directory entries or remove old ones (via the create, mknod, link, and unlink system calls), thereby altering the contents of the directory; execute permission allows a process to search the directory for a file name (it is meaningless to execute a directory).