GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.kernel.org — Flash-Friendly File System (F2FS) — The Linux Kernel documentation Flash-Friendly File System (F2FS)¶Overview¶NAND flash memory-based storage devices, such as SSD, eMMC, and SD cards, have been equipped on a variety systems ranging from mobile to server systems. Since they are known to have different characteristics from the conventional rotating disks, a file system, an upper layer to the storage device, should adapt to the changes from the sketch in the design level. F2FS is a file system exploiting NAND flash memory-based storage devices, which is based on Log-structured File System (LFS). The design has been focused on addressing the fundamental issues in LFS, which are snowball effect of wandering tree and high cleaning overhead. Since a NAND flash memory-based storage device shows different characteristic according to its internal geometry or flash memory management scheme, namely FTL, F2FS and its tools support various parameters not only for configuring on-disk layout, but also for selecting allocation and cleaning algorithms. The following git tree provides the file system formatting tool (mkfs.f2fs), a consistency checking tool (fsck.f2fs), and a debugging tool (dump.f2fs).
For sending patches, please use the following mailing list: For reporting bugs, please use the following f2fs bug tracker link: Background and Design issues¶Log-structured File System (LFS)¶“A log-structured file system writes all modifications to disk sequentially in a log-like structure, thereby speeding up both file writing and crash recovery. The log is the only structure on disk; it contains indexing information so that files can be read back from the log efficiently. In order to maintain large free areas on disk for fast writing, we divide the log into segments and use a segment cleaner to compress the live information from heavily fragmented segments.” from Rosenblum, M. and Ousterhout, J. K., 1992, “The design and implementation of a log-structured file system”, ACM Trans. Computer Systems 10, 1, 26–52. Wandering Tree Problem¶In LFS, when a file data is updated and written to the end of log, its direct pointer block is updated due to the changed location. Then the indirect pointer block is also updated due to the direct pointer block update. In this manner, the upper index structures such as inode, inode map, and checkpoint block are also updated recursively. This problem is called as wandering tree problem [1], and in order to enhance the performance, it should eliminate or relax the update propagation as much as possible. [1] Bityutskiy, A. 2005. JFFS3 design issues. http://www.linux-mtd.infradead.org/ Cleaning Overhead¶Since LFS is based on out-of-place writes, it produces so many obsolete blocks scattered across the whole storage. In order to serve new empty log space, it needs to reclaim these obsolete blocks seamlessly to users. This job is called as a cleaning process. The process consists of three operations as follows.
This cleaning job may cause unexpected long delays, so the most important goal is to hide the latencies to users. And also definitely, it should reduce the amount of valid data to be moved, and move them quickly as well. Key Features¶Flash Awareness¶
Wandering Tree Problem¶
Cleaning Overhead¶
Mount Options¶
Debugfs Entries¶/sys/kernel/debug/f2fs/ contains information about all the partitions mounted as f2fs. Each file shows the whole f2fs information. /sys/kernel/debug/f2fs/status includes:
Sysfs Entries¶Information about mounted f2fs file systems can be found in /sys/fs/f2fs. Each mounted filesystem will have a directory in /sys/fs/f2fs based on its device name (i.e., /sys/fs/f2fs/sda). The files in each per-device directory are shown in table below. Files in /sys/fs/f2fs/<devname> (see also ABI file testing/sysfs-fs-f2fs) Usage¶
mkfs.f2fs¶The mkfs.f2fs is for the use of formatting a partition as the f2fs filesystem, which builds a basic on-disk layout. The quick options consist of:
Note: please refer to the manpage of mkfs.f2fs(8) to get full option list. fsck.f2fs¶The fsck.f2fs is a tool to check the consistency of an f2fs-formatted partition, which examines whether the filesystem metadata and user-made data are cross-referenced correctly or not. Note that, initial version of the tool does not fix any inconsistency. The quick options consist of: -d debug level [default:0] Note: please refer to the manpage of fsck.f2fs(8) to get full option list. dump.f2fs¶The dump.f2fs shows the information of specific inode and dumps SSA and SIT to file. Each file is dump_ssa and dump_sit. The dump.f2fs is used to debug on-disk data structures of the f2fs filesystem. It shows on-disk inode information recognized by a given inode number, and is able to dump all the SSA and SIT entries into predefined files, ./dump_ssa and ./dump_sit respectively. The options consist of: -d debug level [default:0] -i inode no (hex) -s [SIT dump segno from #1~#2 (decimal), for all 0~-1] -a [SSA dump segno from #1~#2 (decimal), for all 0~-1] Examples: # dump.f2fs -i [ino] /dev/sdx # dump.f2fs -s 0~-1 /dev/sdx (SIT dump) # dump.f2fs -a 0~-1 /dev/sdx (SSA dump) Note: please refer to the manpage of dump.f2fs(8) to get full option list. sload.f2fs¶The sload.f2fs gives a way to insert files and directories in the existing disk image. This tool is useful when building f2fs images given compiled files. Note: please refer to the manpage of sload.f2fs(8) to get full option list. resize.f2fs¶The resize.f2fs lets a user resize the f2fs-formatted disk image, while preserving all the files and directories stored in the image. Note: please refer to the manpage of resize.f2fs(8) to get full option list. defrag.f2fs¶The defrag.f2fs can be used to defragment scattered written data as well as filesystem metadata across the disk. This can improve the write speed by giving more free consecutive space. Note: please refer to the manpage of defrag.f2fs(8) to get full option list. f2fs_io¶The f2fs_io is a simple tool to issue various filesystem APIs as well as f2fs-specific ones, which is very useful for QA tests. Note: please refer to the manpage of f2fs_io(8) to get full option list. Design¶On-disk Layout¶F2FS divides the whole volume into a number of segments, each of which is fixed to 2MB in size. A section is composed of consecutive segments, and a zone consists of a set of sections. By default, section and zone sizes are set to one segment size identically, but users can easily modify the sizes by mkfs. F2FS splits the entire volume into six areas, and all the areas except superblock consist of multiple segments as described below: align with the zone size <-|
|-> align with the segment size
_________________________________________________________________________
| | | Segment | Node | Segment | |
| Superblock | Checkpoint | Info. | Address | Summary | Main |
| (SB) | (CP) | Table (SIT) | Table (NAT) | Area (SSA) | |
|____________|_____2______|______N______|______N______|______N_____|__N___|
. .
. .
. .
._________________________________________.
|_Segment_|_..._|_Segment_|_..._|_Segment_|
. .
._________._________
|_section_|__...__|_
. .
.________.
|__zone__|
In order to avoid misalignment between file system and flash-based storage, F2FS aligns the start block address of CP with the segment size. Also, it aligns the start block address of Main area with the zone size by reserving some segments in SSA area. Reference the following survey for additional technical details. https://wiki.linaro.org/WorkingGroups/Kernel/Projects/FlashCardSurvey File System Metadata Structure¶F2FS adopts the checkpointing scheme to maintain file system consistency. At mount time, F2FS first tries to find the last valid checkpoint data by scanning CP area. In order to reduce the scanning time, F2FS uses only two copies of CP. One of them always indicates the last valid data, which is called as shadow copy mechanism. In addition to CP, NAT and SIT also adopt the shadow copy mechanism. For file system consistency, each CP points to which NAT and SIT copies are valid, as shown as below: +--------+----------+---------+ | CP | SIT | NAT | +--------+----------+---------+ . . . . . . . . . . . . +-------+-------+--------+--------+--------+--------+ | CP #0 | CP #1 | SIT #0 | SIT #1 | NAT #0 | NAT #1 | +-------+-------+--------+--------+--------+--------+ | ^ ^ | | | `----------------------------------------' Index Structure¶The key data structure to manage the data locations is a “node”. Similar to traditional file structures, F2FS has three types of node: inode, direct node, indirect node. F2FS assigns 4KB to an inode block which contains 923 data block indices, two direct node pointers, two indirect node pointers, and one double indirect node pointer as described below. One direct node block contains 1018 data blocks, and one indirect node block contains also 1018 node blocks. Thus, one inode block (i.e., a file) covers: 4KB * (923 + 2 * 1018 + 2 * 1018 * 1018 + 1018 * 1018 * 1018) := 3.94TB.
Inode block (4KB)
|- data (923)
|- direct node (2)
| `- data (1018)
|- indirect node (2)
| `- direct node (1018)
| `- data (1018)
`- double indirect node (1)
`- indirect node (1018)
`- direct node (1018)
`- data (1018)
Note that all the node blocks are mapped by NAT which means the location of each node is translated by the NAT table. In the consideration of the wandering tree problem, F2FS is able to cut off the propagation of node updates caused by leaf data writes. Directory Structure¶A directory entry occupies 11 bytes, which consists of the following attributes.
A dentry block consists of 214 dentry slots and file names. Therein a bitmap is used to represent whether each dentry is valid or not. A dentry block occupies 4KB with the following composition. Dentry Block(4 K) = bitmap (27 bytes) + reserved (3 bytes) +
dentries(11 * 214 bytes) + file name (8 * 214 bytes)
[Bucket]
+--------------------------------+
|dentry block 1 | dentry block 2 |
+--------------------------------+
. .
. .
. [Dentry Block Structure: 4KB] .
+--------+----------+----------+------------+
| bitmap | reserved | dentries | file names |
+--------+----------+----------+------------+
[Dentry Block: 4KB] . .
. .
. .
+------+------+-----+------+
| hash | ino | len | type |
+------+------+-----+------+
[Dentry Structure: 11 bytes]
F2FS implements multi-level hash tables for directory structure. Each level has a hash table with dedicated number of hash buckets as shown below. Note that “A(2B)” means a bucket includes 2 data blocks. ----------------------
A : bucket
B : block
N : MAX_DIR_HASH_DEPTH
----------------------
level #0 | A(2B)
|
level #1 | A(2B) - A(2B)
|
level #2 | A(2B) - A(2B) - A(2B) - A(2B)
. | . . . .
level #N/2 | A(2B) - A(2B) - A(2B) - A(2B) - A(2B) - ... - A(2B)
. | . . . .
level #N | A(4B) - A(4B) - A(4B) - A(4B) - A(4B) - ... - A(4B)
The number of blocks and buckets are determined by: ,- 2, if n < MAX_DIR_HASH_DEPTH / 2,
# of blocks in level #n = |
`- 4, Otherwise
,- 2^(n + dir_level),
| if n + dir_level < MAX_DIR_HASH_DEPTH / 2,
# of buckets in level #n = |
`- 2^((MAX_DIR_HASH_DEPTH / 2) - 1),
Otherwise
When F2FS finds a file name in a directory, at first a hash value of the file name is calculated. Then, F2FS scans the hash table in level #0 to find the dentry consisting of the file name and its inode number. If not found, F2FS scans the next hash table in level #1. In this way, F2FS scans hash tables in each levels incrementally from 1 to N. In each level F2FS needs to scan only one bucket determined by the following equation, which shows O(log(# of files)) complexity: bucket number to scan in level #n = (hash value) % (# of buckets in level #n) In the case of file creation, F2FS finds empty consecutive slots that cover the file name. F2FS searches the empty slots in the hash tables of whole levels from 1 to N in the same way as the lookup operation. The following figure shows an example of two cases holding children: --------------> Dir <--------------
| |
child child
child - child [hole] - child
child - child - child [hole] - [hole] - child
Case 1: Case 2:
Number of children = 6, Number of children = 3,
File size = 7 File size = 7
Default Block Allocation¶At runtime, F2FS manages six active logs inside “Main” area: Hot/Warm/Cold node and Hot/Warm/Cold data.
LFS has two schemes for free space management: threaded log and copy-and-compac- tion. The copy-and-compaction scheme which is known as cleaning, is well-suited for devices showing very good sequential write performance, since free segments are served all the time for writing new data. However, it suffers from cleaning overhead under high utilization. Contrarily, the threaded log scheme suffers from random writes, but no cleaning process is needed. F2FS adopts a hybrid scheme where the copy-and-compaction scheme is adopted by default, but the policy is dynamically changed to the threaded log scheme according to the file system status. In order to align F2FS with underlying flash-based storage, F2FS allocates a segment in a unit of section. F2FS expects that the section size would be the same as the unit size of garbage collection in FTL. Furthermore, with respect to the mapping granularity in FTL, F2FS allocates each section of the active logs from different zones as much as possible, since FTL can write the data in the active logs into one allocation unit according to its mapping granularity. Cleaning process¶F2FS does cleaning both on demand and in the background. On-demand cleaning is triggered when there are not enough free segments to serve VFS calls. Background cleaner is operated by a kernel thread, and triggers the cleaning job when the system is idle. F2FS supports two victim selection policies: greedy and cost-benefit algorithms. In the greedy algorithm, F2FS selects a victim segment having the smallest number of valid blocks. In the cost-benefit algorithm, F2FS selects a victim segment according to the segment age and the number of valid blocks in order to address log block thrashing problem in the greedy algorithm. F2FS adopts the greedy algorithm for on-demand cleaner, while background cleaner adopts cost-benefit algorithm. In order to identify whether the data in the victim segment are valid or not, F2FS manages a bitmap. Each bit represents the validity of a block, and the bitmap is composed of a bit stream covering whole blocks in main area. Write-hint Policy¶F2FS sets the whint all the time with the below policy.
Fallocate(2) Policy¶The default policy follows the below POSIX rule.
However, once F2FS receives ioctl(fd, F2FS_IOC_SET_PIN_FILE) in prior to fallocate(fd, DEFAULT_MODE), it allocates on-disk block addresses having zero or random data, which is useful to the below scenario where:
Compression implementation¶
Compress metadata layout: [Dnode Structure]
+-----------------------------------------------+
| cluster 1 | cluster 2 | ......... | cluster N |
+-----------------------------------------------+
. . . .
. . . .
. Compressed Cluster . . Normal Cluster .
+----------+---------+---------+---------+ +---------+---------+---------+---------+
|compr flag| block 1 | block 2 | block 3 | | block 1 | block 2 | block 3 | block 4 |
+----------+---------+---------+---------+ +---------+---------+---------+---------+
. .
. .
. .
+-------------+-------------+----------+----------------------------+
| data length | data chksum | reserved | compressed data |
+-------------+-------------+----------+----------------------------+
Compression mode¶f2fs supports “fs” and “user” compression modes with “compression_mode” mount option. With this option, f2fs provides a choice to select the way how to compress the compression enabled files (refer to “Compression implementation” section for how to enable compression on a regular inode).
To decompress a file: fd = open(filename, O_WRONLY, 0); ret = ioctl(fd, F2FS_IOC_DECOMPRESS_FILE); To compress a file: fd = open(filename, O_WRONLY, 0); ret = ioctl(fd, F2FS_IOC_COMPRESS_FILE); NVMe Zoned Namespace devices¶
Device aliasing feature¶f2fs can utilize a special file called a “device aliasing file.” This file allows the entire storage device to be mapped with a single, large extent, not using the usual f2fs node structures. This mapped area is pinned and primarily intended for holding the space. Essentially, this mechanism allows a portion of the f2fs area to be temporarily reserved and used by another filesystem or for different purposes. Once that external usage is complete, the device aliasing file can be deleted, releasing the reserved space back to F2FS for its own use. # ls /dev/vd* /dev/vdb (32GB) /dev/vdc (32GB) # mkfs.ext4 /dev/vdc # mkfs.f2fs -c /dev/[email protected] /dev/vdb # mount /dev/vdb /mnt/f2fs # ls -l /mnt/f2fs vdc.file # df -h /dev/vdb 64G 33G 32G 52% /mnt/f2fs # mount -o loop /dev/vdc /mnt/ext4 # df -h /dev/vdb 64G 33G 32G 52% /mnt/f2fs /dev/loop7 32G 24K 30G 1% /mnt/ext4 # umount /mnt/ext4 # f2fs_io getflags /mnt/f2fs/vdc.file get a flag on /mnt/f2fs/vdc.file ret=0, flags=nocow(pinned),immutable # f2fs_io setflags noimmutable /mnt/f2fs/vdc.file get a flag on noimmutable ret=0, flags=800010 set a flag on /mnt/f2fs/vdc.file ret=0, flags=noimmutable # rm /mnt/f2fs/vdc.file # df -h /dev/vdb 64G 753M 64G 2% /mnt/f2fs So, the key idea is, user can do any file operations on /dev/vdc, and reclaim the space after the use, while the space is counted as /data. That doesn’t require modifying partition size and filesystem format. Dynamic Device Aliasing Management¶In addition to static device aliasing by deleting the aliasing file, F2FS supports dynamic management of device aliasing. This mechanism allows the system to dynamically transition partition ownership between F2FS userdata and external entities (e.g., zRAM, raw partition) based on system requirements without deleting the master aliasing file or requiring unmount/remount. The master aliasing file is created during the initial format of the file system and remains as a persistent control entity (ioctl gateway) in the root directory.
# f2fs_io dev_alias release /mnt/f2fs/vdc.file # df -h /dev/vdb 64G 753M 64G 2% /mnt/f2fs # f2fs_io dev_alias reserve /mnt/f2fs/vdc.file # df -h /dev/vdb 64G 33G 32G 52% /mnt/f2fs Per-file Read-Only Large Folio Support¶F2FS implements large folio support on the read path to leverage high-order page allocation for significant performance gains. To minimize code complexity, this support is currently excluded from the write path, which requires handling complex optimizations such as compression and block allocation modes. This optional feature is triggered only when a file’s immutable bit is set. Consequently, F2FS will return EOPNOTSUPP if a user attempts to open a cached file with write permissions, even immediately after clearing the bit. Write access is only restored once the cached inode is dropped. The usage flow is demonstrated below: # f2fs_io setflags immutable /data/testfile_read_seq /* flush and reload the inode to enable the large folio */ # sync && echo 3 > /proc/sys/vm/drop_caches /* mmap(MAP_POPULATE) + mlock() */ # f2fs_io read 128 0 1024 mmap 1 0 /data/testfile_read_seq /* mmap() + fadvise(POSIX_FADV_WILLNEED) + mlock() */ # f2fs_io read 128 0 1024 fadvise 1 0 /data/testfile_read_seq /* mmap() + mlock2(MLOCK_ONFAULT) + madvise(MADV_POPULATE_READ) */ # f2fs_io read 128 0 1024 madvise 1 0 /data/testfile_read_seq # f2fs_io clearflags immutable /data/testfile_read_seq # f2fs_io write 1 0 1 zero buffered /data/testfile_read_seq Failed to open /mnt/test/test: Operation not supported /* flush and reload the inode to disable the large folio */ # sync && echo 3 > /proc/sys/vm/drop_caches # f2fs_io write 1 0 1 zero buffered /data/testfile_read_seq Written 4096 bytes with pattern = zero, total_time = 29 us, max_latency = 28 us # rm /data/testfile_read_seq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||