1
0
SWNote/Linux/Linux_FS.md
2021-08-08 23:15:06 +08:00

131 lines
5.3 KiB
Markdown
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 1. Introduction
[File System](https://zhuanlan.zhihu.com/p/291300196) is used to control how data is stored and retrieved , consists of the following three parts: file system API, logical part (data + information about data management structure), and the physical storage of data.
![fs](../img/linux-file-system-2.png)
A FS provides a generalized structure over persistent storage, allowing the low-level structure of the devices (e.g., disk, tape, flash memory storage) to be abstracted away. Generally speaking, the goal of a filesystem is allow logical groups of data to be organized into *files*, which can be manipulated as a unit. In order to do this, the filesystem must provide some sort of index of the locations of files in the actual secondary storage. The fundamental operations of any filesystem are:
- Tracking the available storage space
- Tracking which block or blocks of data belong to which files
- Creating new files
- Reading data from existing files into memory
- Updating the data in the files
- Deleting existing files
# 2. File System Theory
**Metadata is data about data.** For example a file named 'A.txt' , so 'A.txt' is data , and the owner , write/read rules , stored path of this file all called metadata.
## 2.1 Physical Storage of data
Whatever NAND or SSD , all the file system stored on physical storage are serial structure , such like memory address.
![fs_2](../img/linux_fs2.png)
## 2.2 File System Structure
![meta_data](../img/fs_matedata.png)
- 超级块superblock
超级块记录了关于文件系统本身的关键信息metadata of metadata包括文件系统的类型大小状态以及inode的结构metadata。由于超级块非常的重要因此在文件系统中超级块存在着一定的冗余。
- i-节点区i-node
i代表indexi-node也叫索引节点。一个i-node存储一个文件的元数据i-node实际上存储了关于所有权用户、组访问权限可读可写可执行数据的存储位置等等元数据所以i-node就是一个文件的化身一个文件对应一个i-node。
值得注意的是i-node中并不存储文件名这一信息关于这一点原因将在后面解释。
还有很重要的一点是所有的i-node组成一个线性结构并且通过下标来标识下标也叫做i-node号这个和磁盘盘块号不一样inode是建立在盘块上的。
- 数据区data
最后一个部分是数据区文件的内容数据就保存在这个区域。一个磁盘上的块大小是固定的如果一个文件的大小大于一个磁盘块的话数据会分布在多个磁盘块之中文件系统i-node负责追踪这些数据块。
![linux_fs](../img/linux_fs.png)
## 2.3 i-node
**i-node** stores metadata for every file on your system in a table like structure usually located near the beginning of a partition. They store all the information except the file name and the data.
**i-node** numbers are unique at the partition level. Every partition as its own **i-node** table. When a new file is created, it is assigned an **i-node** number and a file name. The **i-node** number is a unique number within that file system. Both name and **i-node** number are stored as entry in a directory. If you run out of **i-node**, you cannot create new files even if you have space left on the given partition.
( Note : Linux和Unix将文件的数据和元数据分开存放具体来讲inode存储元数据数据区存储数据。在 i-node 中不必存储真正的数据但是需要存储数据被放在了数据区的哪个位置。inode保存着文件在数据区的盘块列表这也是索引结点名称 i-node 的由来。)
![fs3](../img/fs3.jpg)
# 3. JFFS2
# 4. Squashfs
**Squashfs** is a compressed read-only file system for Linux. It uses ``zlib/lzo/xz`` compression to compress files, inodes and directories.
A squashfs filesystem consists of a maximum of nine parts, packed together on a byte alignment:
```shell
---------------
| superblock |
|---------------|
| compression |
| options |
|---------------|
| datablocks |
| & fragments |
|---------------|
| inode table |
|---------------|
| directory |
| table |
|---------------|
| fragment |
| table |
|---------------|
| export |
| table |
|---------------|
| uid/gid |
| lookup table |
|---------------|
| xattr |
| table |
---------------
```
Compressed data blocks are written to the filesystem as files are read from the source directory, and checked for duplicates. Once all file data has been written the completed inode, directory, fragment, export, uid/gid lookup and xattr tables are written.
# 5. YAFFS
[Yaffs](<https://yaffs.net/>) (Yet Another Flash File System) is a NAND-flash specific file system.( ref: [How Yaffs Works](<https://yaffs.net/documents/how-yaffs-works>) . )
![yaffs_arch](../img/yaffs_arch.png)
The Yaffs Direct Interface (YDI) which allows Yaffs to be simply integrated with embedded systems, with or without an RTOS.