Some Algorithms Working With Inode System File System Calls

This path tells Xclients to be in xinit , xinit to be in X11 , X11

is in etc and etc is in the / root .

File names are often the actual parameters when typing commands, and typing commands becomes cumbersome for users if they have to type a long path in the above form (known as an absolute path ). Therefore, Linux (like many other operating systems) uses the concept of a current directory for each user working in the system. The current directory is the directory in the file system where the user is currently "in".


Linux allows users to specify a file in a much shorter command using the current directory. For example, if the current directory is the xinit directory , to specify the said file, the user can simply write Xclients or ./Xclients where the " . " symbol refers to the current directory. A path specified using the current directory is called a relative path .

When a user logs into the system, Linux always moves the user to his/her home directory, and at that moment the home directory is the user's current home directory. The super user's home directory is /root , the home directory of the user named user1 is

/home/user1 ... Linux allows using the cd command to change to another directory (set another directory as the current directory). Two dots ".." are used to indicate the directory immediately above the current directory (parent of the current directory).

Linux also allows mounting a file system on a storage device (floppy disk, hard disk area not yet included in the file system) as a subdirectory in the system's file system using the mount command . Mounted file systems are of different types.

The next two sections (3.1.2 and 3.1.3.) introduce more in-depth information about the Linux file system.


3.1.2. Preliminary internal architecture of the file system

On a magnetic disk, a file system is considered a sequential array of logical blocks each containing either 512B or 1024B or a multiple of 512B which is fixed in a file system. In a file system, data blocks are addressed by consecutive indexing, each address is contained in 4 bytes (32 bits).

The internal structure of the file system consists of four consecutive components: Boot block (used

to boot the system), Super block, Inode list and Data area.

Below, we briefly review the contents of the internal structural components of a file system.


Super block

The superblock contains a lot of information about the state of the file system. The superblock contains the following fields:

Size of the inode list (the concept of inode will be explained in the section

after): size the space on the File System that manages the inodes.

Size of the file system.

The above two sizes are in units of external memory capacity,

An index list of free blocks (permanent on the superblock) in the file system.

The index of permanent free blocks on the superblock is used to satisfy the new allocation request. Note that the list of free block indices on the superblock is only a part of the set of all free blocks in the file system.

Index of the next free block in the list of free blocks.

The next free block index is used to aid in finding further free blocks: starting from the block with this index onwards. This means that every block with an index no greater than this index is either in the list of permanent free blocks or has been allocated to a file. Many operations such as creating new files, deleting files, changing file contents etc. update this information.

A list of free inodes (permanent on the superblock) in the file system.

This list contains the index of free inodes that can be immediately allocated to a newly created file. Typically, this list contains only a fraction of the free inodes on the file system.

Index of the next free inode in the list of free inodes.

The next free inode index determines where to search for additional free inodes: the search starts from the inode with this index onwards. This means that every inode with an index no greater than this index is either in the list of permanently free inodes or is already associated with a file.

The above two parameters form a pair that identifies the list of free inodes on the file system. The operations to create new files and delete files update this information.

Lock fields for the free block list and free inode list: In some cases, such as when the system is actually working with the magnetic disk to update these lists, the system does not allow updates to the two lists mentioned above.

Flag indicating that the superblock has been modified: Periodically the superblock in internal memory is updated to the superblock on the magnetic disk and therefore information is needed about the superblock in internal memory differing from the content in external memory: if the two copies are not the same, they need to be modified to be consistent.

Flag indicating that the file system is read-only (write prohibited): In some cases, the system is updating information from external memory, then only reading is allowed for the file system,

Total number of free blocks in the file system,

Total number of free inodes in the file system,

Device information,

The block size (data allocation unit) of the file system. Currently the common block size is 1KB.

During machine operation, the kernel periodically pushes the superblock to disk if it has been transformed to fit the data on the file system.

One of the core concepts that appears in the file system is the inode. The objects associated with the inode are

The relevant aspects of this concept will be presented in the following sections.

Inode

Whenever a process creates a new file, the kernel assigns it an unused inode. To better understand inodes, let's briefly review the relationship between data files and external storage in Linux.

The contents of a file are contained in the data area of ​​the file system and are divided into data blocks (containing the file contents) and the image of the distribution of the file contents is contained in a corresponding inode. The link to this set of data blocks is an inode, only through the inode can one work with the data in the data blocks: The inode contains information about the set of data blocks of the file contents. It can be conceived that the combination of an inode and a set of such data blocks is a physical file: the inode has information about the physical file, including the addresses of the memory blocks containing the contents of the physical file. The term inode is a combination of the two words index and node and is commonly used in Linux.

Inodes are identified by their inode index: that is, the inode's number in the inode list on the file system. Typically, the system uses 2 bytes to store the inode index. With this way of storing the index, there are no more than 65535 inodes in a file system.

Thus, a file has only one inode, but a file has one or more file names. The user acts through the file name, and the file name refers to the inode (the file name and the inode index are two fields of an element of a directory). An inode can correspond to one or more file names, each such correspondence is called a link. Inodes are stored in the inode list area.

During operation, Linux uses a memory area, called the inode table (in some cases, it is also explicitly called the in-core inode copy table) with the function corresponding to the list of inodes in the file system, supporting the process of accessing data in the file system. The content of an in-core inode not only contains the information in the corresponding inode but also adds new information to help inode processing.

Let's look at the internal structure of an inode to see the internal representation of a file. An inode consists of the following fields:

File type. In Linux, file types are classified as: regular files, directories, character specifications, block specifications and FIFO pipes. Linux specifies that the file type field has a value of 0, which means that the inode is not in use.

File access rights. In Linux, files are a common resource of the system so file access rights are of special concern to avoid invalid access cases. For an inode, there are 3 levels of access rights related to objects:

file owner level (this object is denoted by u : from the word user),

the user group level of the file owner (this object is denoted by g : from the word group),

other user level (this object is denoted by a : from the word all).

Access permissions are read, write, execute, or some combination of the three. Note that execute permission for a directory corresponds to permission to search for a file name in that directory.

Number of links to inode: This is the number of file names on the directories.

the item associated with this inode,

Identify the owner of the inode,

Owner group identifier: identifies the name of the user group of which the file owner is a member,

File length in bytes,

File access time:

the time the file was last modified,

time the file was last accessed,

time the file was created,

The address table contains the addresses of the memory blocks containing the file contents. This table has 13 address elements, including 10 direct elements, 1 indirect element of level 1, 1 indirect element of level 2 and 1 indirect element of level 3 (details are in the following section).

The contents of a file change when it is written to; the contents of an inode change when the contents of the file change or the owner changes or the permissions change or the link number changes.

An example of the contents of an inode is as follows:


type regular perms rwxr-xr-x links 2

owner 41CT group 41IT size 5703 bytes

accessed Sep 14 1999 7:30 AM

modified Sep 10 1999 1:30 PM

inode Aug 1 1995 10:15 AM Data address elements

The in-core inode copy also adds the in-core inode status field.

The in-core inode status field contains the following information:

inode is locked,

a process is waiting for the inode to unlock,

in-core inode is different from inode due to the change of data in inode,

in-core inode is different from inode due to data changes in the file,

the number of file names associated with the currently opened file,

logical device number of the file system containing the above file

inode index: used to link to inode on disk,

links to other in-core inodes. In internal memory, in-core inodes are linked by a hash row and a free list. In the hash row list, in-core inodes are linked by logical device number and inode number.

During system operation, the concept of active inode arises if there is a process working with that inode (such as opening a file).

An inode belongs to the list of free inodes when there is no physical file corresponding to it.

that inode

Table containing data block addresses of File in UNIX

The table containing the file's data block address consists of 13 elements, with 10 direct elements and 3 indirect elements: Each element is 4 bytes long, containing a number of a memory block on the disk. Each direct element points to a data block that actually contains the file's content. The first-order indirect element (single indirect) points to an external memory block. Unlike the direct element, this external memory block is not used to store the file's data, but contains a list of indexes of external memory blocks, and these external memory blocks actually contain the file's content. Thus, if the block is 1KB long and an external block index is 4 bytes long, the indirect address allows locating space on the disk to store the file's data up to 256KB (The external memory space in the data area that must be used is 257KB). The same goes for higher-level indirect elements. The above file address management mechanism shows that there is a distinction between small files and large files.

Small files have a smaller length and, organized as above, the access method will allow for faster speed and simplicity because it only has to work with direct elements. When processing, the File reading algorithm proceeds in different ways for direct and indirect elements.

The Fle content storage organization mechanism as presented allows file lengths of up to (2 24 +2 16 + 2 8 +10) blocks.

The data area consists of data blocks, each of which is indexed to distinguish it. Blocks in the data area are used to store the contents of files, the contents of directories, and the contents of file address blocks. Note that the index of the data block is stored in 32 bits and this information determines the maximum capacity of the file system.

3.1.3. Some algorithms working with inodes File system calls

When working with files, we often use system calls. Some common system calls are opening a file open, closing a file close, reading the contents of a file read, writing the contents of a file write, etc.

The table below lists the system calls that work with the file system and classifies them according to the function of each system call (a call may be mentioned several times):


Time

use file

Use namei

assign inode

belong

file count

In-out

file

System structure

file system

Management

tree

open

open

statistics

create

chown

read

month

chdir

create

create

link

mknod

chmod

write

umount

chown

dup

chdir

unlink

link

statistics

cseek



pipe

chroot

mknod

unlink





close

chown

mount







chmod

umount






Maybe you are interested!

Some Algorithms Working With Inode System File System Calls

Low-level file system algorithms

Name

iget

input

Ialloc

ifree

alloc

free

bmap

Buffer positioning algorithm

getblk brelse Bread breada bwrite

Figure 3.2. Overview of File system calls

We look at some algorithms that work with inodes.


Inode access algorithm (iget)

Many situations require the iget algorithm , such as, a process opening a new file or creating a new file etc. The iget algorithm allocates an in-core copy of the inode for an inode number. However, in case there is no in-core copy of the inode, to get its contents, it is necessary to read the contents of that inode and locate the data block containing the given inode. The formula relating the magnetic disk block containing the inode to be read into the internal memory is as follows:

Block index containing inode = (inode number - 1) / (number of inodes in a memory block)

+ first block index contains the inode list on disk.

After reading the disk block containing the inode into internal memory, to determine the exact location of the inode, we have the following formula:

First byte = ((inode number - 1) mod (number of inodes in a block of memory))*length of an inode

For example, if each disk inode occupies 64 bytes, and each disk block contains 8 disk inodes, then inode number 8 will start at the 448th byte on the first disk block in the inode list area.

Note that when working with a file system, its super block is always present in internal memory so that the system has information to work with. Note that in the super block there is a list of free inodes (above it) and a list of free blocks.

The iget algorithm takes an inode to make it active and that depends on a few situations:

- If the inode does not exist in the buffer and is not in the list of free inodes on the super block, the system must report an error that has been encountered. This error occurs because the request for an inode no longer has enough buffer space to work with the file (corresponding to the case in MS-DOS reporting: too many files opened),

- the inode is already in the inode buffer on the file system (there is an in-core inode).

In this case, proceed in two steps:

+ the corresponding inode is locked by another process: then it must wait until the previous process unlocks the inode. Once unlocked the inode can become active or idle,

+ If the inode is in the list of free inodes, remove it from this list by setting the inode to active.

- inode does not exist in the buffer pool but the list of free inodes is not empty. When this list of inodes is not empty, it means that there are inodes that have no value: remove it and put a new inode in its place.


The iput algorithm removes inodes.

The iput algorithm is functionally equivalent to the iget algorithm : it is necessary to remove the presence of an inode, for example when the program performs a file closing operation.

Unlike the case of the iget algorithm, the iput algorithm does not generate error situations.

In this algorithm, when a process stops working on a file associated with an inode, several situations occur:

- The system reduces the number of active files by 1,

- If the number of active files is 0 then:

+ If it is a file deletion command, the system has previously performed the operation of reducing the number of links to the inode by 1 and therefore the number of links may become 0, meaning that the physical file no longer exists. At that time, we actually delete the above file by a number of operations: freeing the data blocks, setting the file type of the inode to 0 and freeing the inode.

+ When the link number > 0, it is necessary to update the inode change on the magnetic disk.

- If the number of active files is still positive, do not perform any action.

Note that this algorithm uses the ifree algorithm .


The ialloc algorithm assigns an inode to a new file.

When a new file is created, such as the initialization of the file creat , an inode must be provided for the file, and the ialloc algorithm meets this requirement.

The working of the ialloc algorithm is explained as follows:

- check the free inode list on the super block, one of two cases occurs either the list is empty or not empty,

- If the list is not empty, get the next inode for the file, initialize that inode's values, and decrement the number of free inodes on super on the super block.

- If the list of free inodes on the super block is empty: search the file system for free inodes to load into the list of free inodes on the super block. If that list is full or no more are found, assign an inode to the file. If the list of free inodes on the super block is empty and no free inodes are found on disk, an error message will be displayed.

On the list of free inodes, the kernel keeps an inode called the cache inode, which is the last inode found, for later convenience in searching.


The ifree algorithm loads an idle inode on disk into the list of idle inodes on superblock


The namei algorithm finds the index of an inode by file name.

The namei algorithm is a popular algorithm, many algorithms working with files must use namei . From the name of a file/directory path, the namei algorithm gives the corresponding inode.


Disk data allocation algorithm

When the kernel wants to allocate a block of data, it allocates the next available block recorded in the super block. Once a block of data has been allocated to a file, it is allocated again only when it becomes available. If there are no more free blocks in the file system and a block is requested, the kernel reports an error.


3.1.4. Support for multiple File systems

The early versions of Linux supported only one file system, the minix file system. Later, with the expansion of the kernel, the Linux community added many different types of file systems to it and Linux became a multi-file system. Below are some of the common file systems supported by Linux in different operating systems.

ADFS file system: ADFS stands for Acorn Disc Filing System and is the standard file system on RiscOS operating system. With this support, Linux can access disk partitions formatted according to ADFS file system.

AFFS file system: AFFS (The Amiga Fast File System) is a popular file system of the AmigaOS version 1.3 operating system running on Amiga machines.

CODA file system: CODA is a network file system that allows users to mount remote file systems and access them as local file systems.

DEVPTS file system: File system for Unix98 PTYs.

EFS file system: This is a type of file system used for CDROMs.

EXT2 file system: EXT2 file system (The second extended filesystem) is the system used mainly on versions of Linux operating system. We will return to study this file system in the following sections.

HFS file system: This is the file system that runs on Apple Macintosh computers.

HPFS file system: HPFS is the file system used in OS/2 operating system. Linux supports this file system at read only level.

ISOFS file system: This is the file system used for CDs. The most popular system for CDs today is ISO 9660. With this support, Linux systems can access data on CDs.

MINIX file system: MINIX is the first file system that Linux supports. File system

This is used in Minix operating systems and some older Linux systems.

MSDOS file system: With this support, Linux system can access partitions of MSDOS operating system. Linux can also use MSDOS style to access partitions of Window 95/98, however, the advantages of Window operating system will no longer be valid, for example, file name is only up to 13 characters (including extension).

NFS file system: NFS (Network File System) is a network file system that supports remote data access like the CODA file system. With NFS, Linux machines can share disk partitions on the network to use as their own local partitions.

NTFS file system: With this support, Linux systems can access partitions of Microsoft Window NT operating systems.

PROC file system: This is a special file system supported by Linux. The PROC file system does not occupy any partition of the system and does not manage data stored on the disk. PROC displays the contents of the system kernel itself. Files in the PROC file system store information about the current state of the kernel. Information about each process running in the system is stored in a directory named corresponding to the process ID of that process. Users can use the PROC file system to get information about the kernel as well as modify some kernel values ​​by modifying the contents of the files in this file system. However, direct modification as above is relatively dangerous, easily causing system crashes.

QNX4 file system: This is the file system used in the QNX 4 operating system.

Comment


Agree Privacy Policy *