Character
Type
-
Regular file
d
Directory
b
Block device
c
Character device
l
Symbolic link
p
Named pipe
s
Domain socket
The Linux File System
◾
399
the inode number dedicated to that file. The symbolic link is a pointer to a hard link stored
elsewhere. As the user modifies the contents of the directory, this list is modified.
New files require new hard links pointing to newly allocated inodes. The deletion of a
file causes the hard link to be removed and the numeric entry of hard links to a file to be
decremented (e.g., in Figure 10.3, deleting the item disk in /dev would result in the hard
link count being reduced to 4). The inode itself remains allocated to the given file unless
the hard link count becomes 0.
10.3.5 inode
We now turn to the inode. When the file system is first established, it comes with a set
number of inodes. The inode is a data structure used to store file information. The informa-
tion that every inode will store consists of
• The file type
• The file’s permissions
• The file’s owner and group
• The file’s size
• The inode number
• A timestamp indicating when the inode was last modified, when the file was created,
and when the file was last accessed
• A link count (the number of hard links that point at this file)
• The location of the file (i.e., the device storing the file) and pointers to the individual
file blocks (if this is a regular file), these pointers break down into
• A set number of pointers that point directly to blocks
FIGURE 10.3
Long listings illustrating file types.
400
◾
Linux with Operating System Concepts
• A set number of pointers that point to indirect blocks; each indirect block con-
tains pointers that point directly to blocks
• A set number of pointers that point to doubly indirect blocks, which are blocks
that have pointers that point to additional indirect blocks
• A set number of pointers that point to triply indirect blocks, which are blocks that
have pointers that point to additional doubly indirect blocks
Typically, an inode will contain 15 pointers broken down as follows:
• 12 direct pointers
• 1 indirect pointer
• 1 double indirect pointer
• 1 triply indirect pointer
An inode is illustrated in Figure 10.4 (which contains 1 indirect pointer and 1 doubly
indirect pointer but no triply indirect pointer because of a lack of space).
Let us take a look at how to access a Linux file through the inode. We will make a few
assumptions. First, our Linux inode will store 12 direct pointers, 1 indirect pointer, 1 dou-
bly indirect pointer, and 1 triply indirect pointer. Blocks of pointers will store 12 pointers
no matter whether they are indirect, doubly indirect, or triply indirect blocks. We will
assume that our file consists of 500 blocks, numbered 0 to 499, each block storing 8 KB (the
File inode
File size (bytes)
Indirect block
Doubly indirect block
Pointers to
additional
disk blocks
Pointers to
indirect
blocks
Pointers to
additional
disk blocks
Block 0
Block 1
Block 2
Block 3
Block 4
Block 5
Indirect block
Storage device ID
Owner UID
Group UID
Mode
Flags
Timestamp
Hard link count
File block pointers
FIGURE 10.4
inode structure with pointers to disk blocks. (Adapted from Fox, R.
Information
Technology: An Introduction for Today’s Digital World
,
FL: CRC Press, 2013.)
The Linux File System
◾
401
typical disk block stores between 1 KB and 8 KB depending on the file system utilized).
Our example file then stores 500 * 8 KB
=
4000 KB or 4 MB. Here is the breakdown of how
we access the various blocks.
• Blocks 0–11: direct pointers from the inode.
• Blocks 12–23: pointers from an indirect block pointed to by the inode’s indirect
pointer.
• For the rest of the file, access is more complicated.
• We follow the inode’s doubly indirect pointer to a doubly indirect block. This
block contains 12 pointers to indirect blocks. Each indirect block contains 12
pointers to disk blocks.
− The doubly indirect block’s first pointer points to an indirect block of 12
pointers, which point to blocks 24–35.
− The doubly indirect block’s second pointer points to another indirect block of
12 pointers, which point to blocks 36–47.
− …
− The doubly indirect block’s last pointer points to an indirect block of 12 point-
ers, which point to blocks 156–167.
• We follow the inode’s triply indirect pointer to a triply indirect block. This block
contains 12 pointers to doubly indirect blocks, each of which contains 12 point-
ers to indirect blocks, each of which contain 12 pointers to disk blocks. From the
triply indirect block, we can reach blocks 168 through 499 (with room to increase
the file to block 1895).
Earlier, we noted that the disk drive supports random access. The idea is that to track
down a block, block i, we have a mechanism to locate it. This is done through the inode
pointers as just described.
The above example is far from accurate. A disk block used to store an indirect, doubly
indirect, or triply indirect block of pointers would be 8 KB in size. Such a sized block would
store far more than 12 pointers. A pointer is usually 32 or 64 bits long (4 or 8 bytes). If we
assume an 8 byte pointer and an 8 KB disk block, then an indirect, doubly indirect, or
triply indirect block would store 8 KB/8 B pointers
=
1 K or 1024 pointers rather than 12.
When a file system is created, it comes with a set number of inodes. The actual number
depends on the size of the file system and the size of a disk block. Typically, there is 1 inode
for every 2–8 KB of file system space. If we have a 1 TB file system (a common size for a
hard disk today), we might have as many as 128 K (approximately 128 thousand) inodes.
The remainder of the file system is made up of disk blocks dedicated to file storage and
pointers. Unless nearly all of the files in the file system are very small, the number of inodes
should be more than sufficient for any file system usage.
402
◾
Linux with Operating System Concepts
The system administrator is in charge of administering the file system. Rarely, if ever,
will the system administrator have to worry about inodes or pointers. The Linux file system
commands instead operate at a higher level of abstraction, allowing administrators and
users alike to operate on the file entities (files, directories, links, etc.). It is the device driv-
ers, implemented by system programmers, which must deal with inodes and the location
of disk blocks.
Let us consider, as an example, file creation. First, a new inode must be allocated from
the available inodes. The next inode in order is used. The inode’s information is filled in,
consisting of the file type, initial permissions (defaulting from the user’s umask value),
owner and group of the user, the file system’s device number, and a timestamp for file
creation. If the file is to store some initial contents, disk blocks are allocated and the direct
pointers are modified in the inode to point at these blocks. Finally, the content can be saved
to those blocks. Additionally, the directory is modified to store the new hard link.
If we were instead to move a file within the same partition, then all we have to do is trans-
fer the hard link from one directory to another. Copying a file requires a new inode and
disk blocks with the original copy’s blocks copied into the new copy’s blocks. Deleting a file
requires the removal of the hard link from the directory along with a decrement to the hard
link count. If this reaches zero, the inode is returned to the collection of free inodes. Disk
blocks themselves are not modified (erased) but instead are returned to free blocks to be
reused in the future. If a new file is created, the returned inode might be reused at that time.
10.3.6 Linux Commands to Inspect inodes and Files
There are several tools available to inspect inodes. The following commands provide such
information:
•
stat
—provides details on specific file usage, the option
–c %i
displays the file’s
inode number
•
ls
—the option –i displays the inodes of all entries in the directory
•
df –i
—we explore df in more detail in the next section but this command provides
information on the utilization of the file system, partition by partition. The -i option
includes details on the number of inodes used.
We wrap up this section by examining the information obtainable from stat. The stat
command itself will respond with the name of the file, the size of the file, the blocks used
to store the file, the device storing the file (specified as a device number), the inode of the
file, the number of hard links to the file, the file’s permissions, UID, GID in both name and
number, and the last access, modification, and change date and time for the file. The stat
command has many options. The most significant are listed here:
• -L—follow links to obtain inode information of files stored in other directories (with-
out this, symbolic links in the given directory are ignored)
• -f—used to obtain statistics on an entire file system rather than a file
The Linux File System
◾
403
• -c
Do'stlaringiz bilan baham: |