Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

fallocate

Created by : Mr Dk.

2023 / 05 / 02 22:56

Hangzhou, Zhejiang, China


Background

fallocate 用于为文件预分配或收回空间。

当我们需要一个指定长度的文件时,最简单的方法是,通过内存缓冲区不断向文件中写入,直到写到指定的长度。或者将文件指针直接修改为指定长度的位移处,并向文件中写入一次,那么文件长度会自动扩展到指定长度。

使用 fallocate 可以快速分配指定长度的未初始化数据块,达到同样的效果;另外,对于文件中已有的全 0 页,fallocate 也可以回收数据块的物理空间,而上层程序读取这个页中的字节时将直接返回全 0。这样可以实现 文件打洞。

这个 CLI 实际上是由 Linux 上的同名系统调用实现而来。

Usage

$ fallocate --help

Usage:
 fallocate [options] <filename>

Preallocate space to, or deallocate space from a file.

Options:
 -c, --collapse-range remove a range from the file
 -d, --dig-holes      detect zeroes and replace with holes
 -i, --insert-range   insert a hole at range, shifting existing data
 -l, --length <num>   length for range operations, in bytes
 -n, --keep-size      maintain the apparent size of the file
 -o, --offset <num>   offset for range operations, in bytes
 -p, --punch-hole     replace a range with a hole (implies -n)
 -z, --zero-range     zero and ensure allocation of a range
 -x, --posix          use posix_fallocate(3) instead of fallocate(2)
 -v, --verbose        verbose mode

 -h, --help           display this help
 -V, --version        display version

Arguments:
 <num> arguments may be followed by the suffixes for
   GiB, TiB, PiB, EiB, ZiB, and YiB (the "iB" is optional)

For more details see fallocate(1).

Preallocate

预分配一个 1MB 的文件:

$ fallocate -l 1MiB file

$ stat file
  File: file
  Size: 1048576         Blocks: 2048       IO Block: 4096   regular file
Device: 820h/2080d      Inode: 29595       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/mrdrivingduck)   Gid: ( 1000/mrdrivingduck)
Access: 2023-05-02 23:13:36.445550770 +0800
Modify: 2023-05-02 23:13:36.445550770 +0800
Change: 2023-05-02 23:13:36.445550770 +0800
 Birth: 2023-05-02 23:13:36.445550770 +0800

这个文件有 2048 个 512B 的块。由于不需要对这些块进行初始化(写 0),所以 fallocate 很快就返回了。但是文件系统需要保证读取这些未初始化的块时需要返回全 0,否则这些块中可能会出现已经被删除掉的其它文件中的信息。

$ vim file

Deallocate

由于该文件的内容目前全部都是 0,因此这 1MB 的文件数据实际上不需要任何物理块来存储,仅需要在文件的元信息中标记即可。这样在读取全 0 页时,文件系统将直接返回 0。

$ fallocate -d -v file
file: 1 MiB (1048576 bytes) converted to sparse holes.

$ stat file
  File: file
  Size: 1048576         Blocks: 0          IO Block: 4096   regular file
Device: 820h/2080d      Inode: 29595       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/mrdrivingduck)   Gid: ( 1000/mrdrivingduck)
Access: 2023-05-02 23:22:56.385558933 +0800
Modify: 2023-05-02 23:23:06.965559107 +0800
Change: 2023-05-02 23:23:06.965559107 +0800
 Birth: 2023-05-02 23:22:49.705557904 +0800

Keep Zero

也可以通过 -z 参数指定某个范围内的全 0 页一定要分配物理空间。比如对于刚才的文件,我们可以指定第一个 block 必须分配空间,用 -o 和 -l 分别指定范围的起始位置和长度:

$ fallocate -z -o 0 -l 1 file

$ stat file
  File: file
  Size: 1048576         Blocks: 8          IO Block: 4096   regular file
Device: 820h/2080d      Inode: 29595       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/mrdrivingduck)   Gid: ( 1000/mrdrivingduck)
Access: 2023-05-02 23:24:18.705553660 +0800
Modify: 2023-05-02 23:24:40.725548703 +0800
Change: 2023-05-02 23:24:40.725548703 +0800
 Birth: 2023-05-02 23:24:16.525553837 +0800

由于当前文件系统的 I/O 单元大小为 4096 字节(8 个 512B),因此虽然指定的范围只有一个字节,但文件系统将会分配一整个 I/O 单元的物理空间。所以现在文件的前 4096 字节是有物理空间的。

同样,还是可以用刚才的 -d 参数检测文件中具有物理空间的全 0 页,并回收其物理空间:

$ fallocate -d -v file
file: 4 KiB (4096 bytes) converted to sparse holes.

$ stat file
  File: file
  Size: 1048576         Blocks: 0          IO Block: 4096   regular file
Device: 820h/2080d      Inode: 29595       Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/mrdrivingduck)   Gid: ( 1000/mrdrivingduck)
Access: 2023-05-02 23:25:00.975552011 +0800
Modify: 2023-05-02 23:25:10.975551896 +0800
Change: 2023-05-02 23:25:10.975551896 +0800
 Birth: 2023-05-02 23:24:16.525553837 +0800

References

文件打洞 (Hole Punching) 及其应用

stackoverflow - what is file hole and how can it be used? [closed]

fallocate(1) — Linux manual page

LWN.net - Punching holes in files

Edit this page on GitHub
Prev
du
Next
find