Hard disk partitions and file systems

A new hard disk cannot be used directly. The hard drive must be divided into several (or possibly one) partitions, and each partition formatted as a file system on which Linux can store data and perform file management and maintenance.

A partition on a Linux or UNIX system is the equivalent of a logical disk on a Windows system.

To format a partition as a file system is to divide the partition into small, equal-sized units and number them sequentially.

These small units are called blocks, and the default Block size on Linux is 4KB.

Blocks are the smallest unit for storing data. Each Block can store a maximum of one file. If a file is larger than 4KB, multiple blocks are used.

inode

The inode concept

An inode (I node) is a list of information related to a particular object, such as a file or directory.

The I node is actually a data structure that holds basic information about a common file, directory, or other file system object.

When a disk is formatted as a file system (such as ext2 or ext3), the system automatically generates an I-node (inode) table.

This table contains the metadata of all files. Each file and directory corresponds to a unique I node, which is identified by an I node number (inode number).

The number of inodes determines how many files can be stored in the file system, and how many I nodes in a partition can store only as many files and directories.

In most types of file systems, the number of I nodes is fixed and is generated when the file system is created.

In a typical UNIX or Linux file system, the I node takes up about 1% of the total file system size.

The inode interpretation

All attributes in the I node describe the file, not the contents of the file.

The node I is similar to the book catalog in the library. In the book catalog of each book, there is a brief introduction, author information, publication date, page number and other abstract information.

Usually each I node consists of two parts, the first part is the basic information about the file, and the second part is a pointer to the data block where the file information is stored:

  1. inode-no: I Node ID. In a file system, each I node has a unique number.
  2. File type: Indicates the type of the file.
    • -Is a common file
    • dFor a directory
  3. permission: the permissions. Use numerical representation in the I node to indicate the permissions of the file.
  4. Link count: Indicates the number of hard links.
  5. UID: UID of the file owner.
  6. GID: owner GID of the owning group.
  7. size: File size.
  8. Time stamp: Timestamp. The timestamp contains three times:
    • (1) Access time (A time) : Time of last access.
    • (2) the Modify time (M time) : Time of last edit.
    • (3) Change the time,C time) : The time when any unary data in the file inode changed.
    • ifM timeTo be updated, usuallyA timeC timeWill be updated along with it:
      • A file must be opened before it can be updated.
      • The file size changes after editing, so C time is also updated.
  9. Other information
  10. pointer: pointer to blocks where the file is saved.

Query the inode number of a file:

You can use the ls command with the -i option to display the file’s I-node number at the beginning of each line of records.

Common filedirectory

  • Regular files: Stores only data and can store various types of data.

  • Directories: a special type of file. Directories store the file name and the information about the I node number associated with the file name. Only one type of data can be stored in a directory.

struct file_list{
    char name[MAX];
    unsign inode;
    struct file_list next;
} * pBarFiles;

char * make_the_list(struct file_list *);

fooDirectoty.content = make_the_list(pBarFiles);
Copy the code

Inode operations behind cp, rm, and MV

  • Cp:

    1. Find a free inode number, write meta data from the new file to the free I node, and place the new record in the inode table.
    2. Generates a directory record that maps the new file name to the empty inode number.
    3. Copy the contents (data) of a file to a new file.
  • Rm:

    1. First, reduce the number of hard links in this file by 1. (If the original file link count is 3, after running rm, its link count will be 2)
    2. If the link count of the file is less than 1, the I node is freed for reuse.
    3. Release the block of data storing the contents of this file; (Mark these blocks for use)
    4. Delete the directory record for this file name and node number I.
  • Mv:

    • If the original location and destination location are in the same file system:

      1. First, a new directory record is generated, and the new file name corresponds to the I node of the source file.
      2. Deletes the old directory record with the old file name.

      In this case it is a logical move: except for updating the timestamp, the action of moving the file does not affect the data in the inode table, nor does it move the data to another file, i.e. there is no actual movement of the data.

    • If the source location and destination location are on different file systems:

      Cp (source, destination); Rm (source);Copy the code

Note: If necessary, cp, rm rather than MV should be used to ensure that data actually moves.

conclusion

  • A few terms about documents:

    (1) File names are most commonly used when accessing and maintaining files.

    (2) Inodes are objects used by the system to record information about files.

    (3) A data block is a unit of disk space used to store data.

  • Among them:

    Each file must have a name and be associated with an I node.

    Usually the system can identify the I node by the file name,

    The data block where the data is stored can then be located by a pointer in the I node