f2fs: creating, syncing and removing directories in parallel increases number of orphan inodes and used space

From: Vyacheslav Kovalevsky

Date: Thu Dec 03 2026 - 05:58:02 EST


Hello, there seems to be an issue with F2FS (default configuration).

Consider this sequence of operations:

1. Create a directory
2. Call sync()
3. Sleep for some time (less than 1ms, 100us works best on my machine)
4. Remove the directory
5. Repeat from step 1

When running multiple instances of this script (each one creates its own directory with fixed name), the number of orphan inodes grows (and free space decreases).

Example: running 10 instances on 2 CPU virtual machine on 500 MB ram disk. `sar` command output (last measure after all instances stopped):

```
Linux 7.4.0-rc5 (ubuntu)     12/03/26     _x86_64_    (2 CPU)

09:58:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
09:58:33          436        62     12.45     12.45    111613    3      0.00 /dev/ram0

09:59:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
09:59:33          405        93     18.60     18.60    103771 7845      7.03 /dev/ram0

10:00:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:00:33          375       123     24.64     24.64     96074  15542     13.92 /dev/ram0

10:01:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:01:33          349       149     30.02     30.02     89218  22398     20.07 /dev/ram0

10:02:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:02:33          325       173     34.80     34.80     83121  28495     25.53 /dev/ram0

10:03:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:03:33          302       196     39.31     39.31     77375  34241     30.68 /dev/ram0

10:04:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:04:33          282       216     43.43     43.43     72126  39490     35.38 /dev/ram0

10:05:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:05:33          262       236     47.29     47.29     67193  44423     39.80 /dev/ram0

10:06:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:06:33          244       254     50.98     50.98     62497  49119     44.01 /dev/ram0

10:07:23     MBfsfree  MBfsused   %fsused  %ufsused     Ifree  Iused    %Iused FILESYSTEM
10:07:33          234       264     53.05     53.05     59854  51762     46.38 /dev/ram0
```

Triggering GC or removing all directories in the file system does not frees orphan inodes, only remounting does. See below for debug information and steps to reproduce. As a side note, this seems to cause a slight performance degradation as the number of used inodes grows.

NOTE: I am not sure if this is intended behavior or whether this issue can affect real users at all. Would like to read your comments, thanks.


System info
===========

Linux version 7.4.0-rc5, also tested on 7.0.0-34-generic (Ubuntu 26.04.1 LTS)


How to reproduce
================

Test (C++):

```
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <filesystem>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
  if (argc != 3) {
    fprintf(stderr,
            "expected directory name and delay in microseconds as arguments");
    return EXIT_FAILURE;
  }
  char *dir = argv[1];
  long delay = atoi(argv[2]);

  std::random_device rd;
  std::mt19937 gen(rd());
  std::exponential_distribution<double> dist(1.0 / (double)delay);

  int status;
  while (1) {
    status = mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    if (status) {
      fprintf(stderr, "failed: mkdir %s (%s)\n", dir, strerror(errno));
      return EXIT_FAILURE;
    }

    sync();

    struct timespec rem;
    struct timespec req = {.tv_sec = 0, .tv_nsec = ((long)dist(gen)) * 1000L};
    nanosleep(&req, &rem);

    std::error_code ec;
    // NOTE: can use rmdir directly, but the effect will be smaller
    std::filesystem::remove_all(dir, ec);
    if (ec) {
      fprintf(stderr, "failed: remove %s (%s)\n", dir, ec.message().c_str());
      return EXIT_FAILURE;
    }
  }
}
```

Steps:

```
root@ubuntu:/# modprobe brd rd_nr=1 rd_size=512000
root@ubuntu:/# mkfs.f2fs /dev/ram0

    F2FS-tools: mkfs.f2fs Ver: 1.16.0 (2023-04-11)

Info: Disable heap-based policy
Info: Debug level = 0
Info: Trim is enabled
Info: Segments per section = 1
Info: Sections per zone = 1
Info: sector size = 512
Info: total sectors = 1024000 (500 MB)
Info: zone aligned segment0 blkaddr: 512
Info: format version with
  "Linux version 7.4.0-rc5 (root@ubuntu) (gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0, GNU ld (GNU Binutils for Ubuntu) 2.46) #1 SMP PREEMPT_DYNAMIC Thu Dec  3 09:27:09 UTC 2026"
Info: [/dev/ram0] Discarding device
Info: This device doesn't support BLKSECDISCARD
Info: Discarded 500 MB
Info: Overprovision ratio = 10.000%
Info: Overprovision segments = 24 (GC reserved = 17)
Info: format successful
root@ubuntu:/# mkdir /mnt/fstest/
root@ubuntu:/# mount -t f2fs /dev/ram0 /mnt/fstest/
root@ubuntu:/# mkdir /mnt/fstest/dir
root@ubuntu:/# g++ test.cpp
root@ubuntu:/# ./a.out /mnt/fstest/dir/0 100 &
root@ubuntu:/# ./a.out /mnt/fstest/dir/1 100 &
root@ubuntu:/# ./a.out /mnt/fstest/dir/2 100 &
... # spawn 10 (can be less or more) instances in total
```

FS stats can be monitored with `sar -F --fs=/dev/ram0 10`.

Debug output in the end:

```
root@ubuntu:~# cat /sys/kernel/debug/f2fs/status

=====[ partition info(ram0). #0, RW, CP: Good]=====
[SB: 1] [CP: 2] [SIT: 2] [NAT: 2] [SSA: 1] [MAIN: 242(OverProv:24 Resv:17)]

Current Time Sec: 717 / Mounted Time Sec: 41

Policy:
  - IPU: [ FORCE HONOR_OPU_WRITE ]

Utilization: 46% (51762 valid blocks, 0 discard blocks)
  - Node: 51761 (Inode: 51761, Other: 0)
  - Data: 1
  - Inline_xattr Inode: 51760
  - Inline_data Inode: 0
  - Inline_dentry Inode: 51760
  - Compressed Inode: 0, Blocks: 0
  - Swapfile Inode: 0
  - Donate Inode: 0
  - Orphan/Append/Update Inode: 51750, 0, 0

Main area: 242 segs, 242 secs 242 zones
    TYPE           blkoff    segno    secno   zoneno  dirty_seg  full_seg  valid_blk
  - COLD   data:        0       59       59       59          0       0          0
  - WARM   data:        0      120      120      120          0       0          0
  - HOT    data:        2        3        3        3          1       0          1
  - Dir   dnode:      473      110      110      110        202       0      51761
  - File  dnode:        0        1        1        1          0       0          0
  - Indir nodes:        0        2        2        2          0       0          0
  - Pinned file:        0       -1       -1       -1
  - ATGC   data:        0       -1       -1       -1

  - Valid: 6
  - Dirty: 201
  - Prefree: 0
  - Free: 35 (35)

CP calls: 5954685 (BG: 5)
CP count: 2055736
  - cp blocks : 55399824
  - sit blocks : 58307
  - nat blocks : 1337406
  - ssa blocks : 24992
CP merge:
  - Queued :    0
  - Issued : 3102788
  - Total : 5954685
  - Cur time :    0(ms)
  - Peak time :    1(ms)
GC calls: 1 (gc_thread: 1)
  - data segments : 0 (BG: 0)
  - node segments : 0 (BG: 0)
  - Reclaimed segs :
    - Normal : 0
    - Idle CB : 0
    - Idle Greedy : 0
    - Idle AT : 0
    - Urgent High : 0
    - Urgent Mid : 0
    - Urgent Low : 0
Try to move 0 blocks (BG: 0)
  - data blocks : 0 (0)
  - node blocks : 0 (0)
BG skip : IO: 0, Other: 1
defrag blocks : 0

Extent Cache (Read):
  - Hit Count: L1-1:0 L1-2:0 L2:0
  - Hit Ratio: 0% (0 / 0)
  - Inner Struct Count: tree: 0(0), node: 0

Extent Cache (Block Age):
  - Allocated Data Blocks: 1
  - Hit Count: L1:0 L2:0
  - Hit Ratio: 0% (0 / 0)
  - Inner Struct Count: tree: 0(0), node: 0

Balancing F2FS Async:
  - DIO (R:    0, W:    0)
  - IO_R (Data:    0, Node:    0, Meta:    0
  - IO_W (CP:    0, Data:    0, Flush: (   0    0    1), Discard: (   0 12002)) cmd: 14052 undiscard:27802
  - atomic IO:    0 (Max.    0)
  - compress:    0, hit:       0
  - nodes:    0 in 50354
  - dents:    0 in dirs:   0 (   0)
  - data:    0 in files:   0
  - quota data:    0 in quota files:   0
  - meta:    0 in  559
  - imeta:    0
  - fsync mark:    0
  - NATs:         0/    54597
  - SITs:         0/      242
  - free_nids:      2836/   181196
  - alloc_nids:         0

Distribution of User Blocks: [ valid | invalid | free ]
  [-----------------------|-------------------------|--]

IPU: 0 blocks
SSR: 9150170 blocks in 24731 segments
LFS: 134144 blocks in 261 segments

BDF: 16, avg. vblocks: 254

Memory: 211498 KB
  - static: 133 KB
  - cached all: 7712 KB
  - read extent cache: 0 KB
  - block age extent cache: 0 KB
  - meta entry: 34 KB, meta cache: 2236 KB
  - node entry: 3147 KB, node cache: 201416 KB
  - paged : 203652 KB
```