F* 文件系统 – 直接读取 SSD 并绕过操作系统内核的文件搜索技术
F* file system – file search that reads SSD directly bypassing OS kernel

原始链接: https://github.com/dmtrKovalenko/ffs

这是一个高性能、实验性的命令行工具,它通过直接读取原始磁盘块来搜索文件,从而绕过了操作系统内核和虚拟文件系统(VFS)。该工具使用约 1500 行 C 语言代码编写,能够避开内核缓存,随着文件数量的增加,其性能会逐渐超过 `ripgrep` 等标准工具。 **主要功能与局限性:** * **直接访问:** 它能够解析二进制数据块,并支持在无需挂载的情况下直接读取未挂载的卷(例如 `.dmg` 或 `.iso` 文件)。 * **高性能:** 利用 OpenMP 进行多核处理,通过避免 VFS 开销,在大规模数据集上的表现优于 `ripgrep`。 * **兼容性:** 支持 EXT4、Btrfs 和 APFS(后者在 macOS 上需要关闭系统完整性保护)。 * **约束:** 由于它忽略了操作系统缓存,除非手动同步磁盘,否则可能会遗漏最近的写入数据。这是一个实验性的“概念验证”项目,旨在展示内核级抽象如何可能成为性能瓶颈。 虽然该工具在技术上非同寻常且有时不太实用,但它为理解底层文件系统交互和原始数据处理提供了一个有趣的视角。

**F* 文件系统**是一个业余项目,它通过绕过操作系统内核直接读取固态硬盘来实现文件搜索。它支持 ext4、btrfs 和 apfs,并且可以在不挂载的情况下搜索 .iso 和 .dmg 等脱离卷文件。尽管该项目支持多线程和压缩,但创建者承认其实用性有限,主要原因是访问原始磁盘数据需要 sudo 权限。 该项目在 Hacker News 上引发了一场幽默的讨论,用户们探讨了基于人工智能的“机器人”是否可以通过无密码 sudo、别名或容器化等各种变通方法有效地绕过权限限制。技术怀疑论者还质疑该工具在验证硬件级磁盘操作(如刷新命令)时的可靠性,因为固态硬盘控制器背后存在许多隐藏的抽象层。
相关文章

原文

This is a cli tool for searching files (like grep) that does not use the OS kernel to read files, but reads your disks directly. It is practically useless, but insanely cool.

this is just ~1.5k lines of C code that:

  • requires sudo only when reading a raw device node (e.g. /dev/rdisk*); searching an image file needs no elevated permissions
  • requires disabling SIP protection to run on the main macOS disk
  • can miss some recent file writes (will require a manual sync call)
  • might not be able to search trees on a highly volatile file system while other system components are writing files in the OS
  • works only for file systems implemented manually in this project

but at the same time

  • directly reads blocks from your disks
  • bypasses the VFS / buffered read() path, instead it preads the block device directly
  • progressively faster than ripgrep (the more files you need to search - the faster it is than ripgrep)
  • can search unmounted volumes - it just parses binary blobs
  • detects and skips binary files
  • spreads the load across all the cores via openmp

on linux mostly any file system is easy to implement

./fs/ext4.c

This is the easiest file system to support: it is a journaling file system that writes in place (no copy-on-write), so most of the time this is the best file system for ffs. Sometimes you might see that ffs can not see some recent updates to the files, this might happen if the kernel is holding recent updates in the cache and deferring writes to disk. You can enforce synchronization using

./fs/btrfs.c

B-tree file system is significantly more complicated, is a more efficient file storage and comes with an additional limitation:

When any file on your file system is updated the whole superblock requires an update as well, which means that if ffs reads the superblock (the high level b-tree) and after that the kernel updates the tree - the whole read becomes invalid.

This is possible to bypass using fsfreeze or by creating a separate detached volume

./fs/apfs.c

APFS is a proprietary file system implemented by Apple that has been reverse-engineered and is also supported here, but Apple has significantly increased its security policies.

You won't be able to run ffs on your main disk without disabling SIP

SIP - system integrity protection is a special security feature that prohibits any access to the main disk superblock even as a root user. You can not bypass it even with sudo; you have to disable this feature (you may already have it disabled if you use projects like yabai).

There is a way to test ffs on the Apple file system without touching your main disk - you can search raw .dmg files without any elevated permissions (yes, the app installers are just detached volumes). With ffs you don't need to mount anything, you can just give it a path to the raw bytes of a volume along with the file system type:

ffs "<QUERY>" /path/to/volume.dmg apfs

Searching in detached volumes

Because ffs reads bytes directly you can use it to search any detached volumes without mounting them to the file system. E.g. reading .iso or .dmg files.

This is the funniest part - ffs doesn't have access to the VFS / kernel file system cache. That's why it is going to be slower on smaller (or already cached) directories, but progressively faster once the cache is exhausted and your kernel has to go and read the actual disk state.

Why? Exactly to prove the point that at some point the kernel VFS becomes an overhead.

This is the search result comparing ffs to ripgrep on a btrfs mounted drive. Note that ripgrep uses a far more advanced SIMD-based matcher and file walker, while ffs is just ~1.8k lines of C code.

[repos — 631k files]
  ffs |####                                              | 5.505s
  rg  |###                                               | 4.813s

[dev — 1.50M files]
  ffs |############                                      | 18.413s
  rg  |#################                                 | 25.673s

[home — 3.25M files]
  ffs |########################                          | 36.205s
  rg  |##################################################| 74.690s

The flags used for ripgrep are -F --no-heading -H -n --no-ignore --hidden --one-file-system --no-messages - which brings it to emit the same results as ffs.

All you need to compile a project is libzstd for btrfs, openmp in your pkg-config then simply

联系我们 contact @ memedata.com