[Lenovo A6000/Plus Questions & Answers] ShivaY:
F2FS (Flash-Friendly File System) is a flash file system initially developed by Samsung Electronics for the Linux kernel.

The motive for F2FS was to build a file system that, from the start, takes into account the characteristics of NAND flash memory-based storage devices (such as solid-state disks, eMMC, and SD cards), which are widely used in computer systems ranging from mobile devices to servers.

Features

---------------------------

Multi-head logging

Multi-level hash table for directory entries

Static/Dynamic hot and cold data separation

Adaptive logging scheme

Configurable operational units

Dual checkpoint

Roll-back and roll-forward recovery

Heap-style block allocation

TRIM/FITRIM support

Online fs defragmentation/file defragmentation

Inline xattrs/data/dir

Offline filesystem check (Check and fix inconsistency)

Atomic operations

Filesystem-level encryption

Offline resizing

Inner periodically data flush

extent cache

Design

------------

On-disk layout

F2FS divides the whole volume into a number of segments, each of which is fixed at 2 MB. 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 the same size, but users can easily modify the size with mkfs.

F2FS splits the entire volume into six areas, and all except the superblock area consist of multiple segments as described below.

Superblock (SB)

-------------------------

The SB is located at the beginning of the partition. There are two copies to avoid file-system corruption. It contains basic partition information and some default F2FS parameters.

Checkpoint (CP)

-------------------------

The CP contains file system information, bitmaps for valid NAT/SIT sets, orphan inode lists, and summary entries of current active segments.

Segment Information Table (SIT)

--------------------------------------------

The SIT contains the valid block count and validity bitmap of all the Main Area blocks.

Node Address Table (NAT)

--------------------------------------

The NAT is an address table for the Main Area node blocks.

Segment Summary Area (SSA)

-----------------------------------------

The SSA contains entries which contain the owner information of the Main Area data and node blocks.

Main Area

-------------------

The main area contains file and directory data and their indices.

In order to avoid misalignment between file system and flash storage, F2FS aligns the start block address of the CP with the segment size. It also aligns the Main Area start block address with the zone size by reserving some segments in the SSA area.

Metadata structure

-----------------------------

F2FS uses the checkpoint scheme to maintain file system integrity. At mount time, F2FS first tries to find the last valid checkpoint data by scanning the CP area. In order to reduce the scanning time, F2FS uses only two copies of the CP. One of them always indicates the last valid data, which is called a shadow copy mechanism. In addition to the CP, the NAT and SIT also use the shadow copy mechanism. For file system consistency, each CP points to which NAT and SIT copies are valid.

Default block allocation

-----------------------------------

At runtime, F2FS manages six active logs inside the "Main Area:" Hot/Warm/Cold node and Hot/Warm/Cold data.

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. The background cleaner is executed 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 the log block thrashing problem present in the greedy algorithm. F2FS uses the greedy algorithm for on-demand cleaning, the background cleaner uses the 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 the Main Area.