打孔和零范围的区别
Difference between punch hole and zero range
我正在查看 fallocate 的手册页,但我不明白这两者之间的区别。一个似乎分配块但不写入它们,另一个似乎释放块而不覆盖它们。无论哪种方式,从用户的角度来看,效果似乎都无法区分。请给我解释一下。
Zeroing file space
Specifying the FALLOC_FL_ZERO_RANGE flag (available since Linux 3.15)
in mode zeroes space in the byte range starting at offset and
continuing for len bytes. Within the specified range, blocks are
preallocated for the regions that span the holes in the file. After
a successful call, subsequent reads from this range will return
zeroes.
Zeroing is done within the filesystem preferably by converting the
range into unwritten extents. This approach means that the specified
range will not be physically zeroed out on the device (except for
partial blocks at the either end of the range), and I/O is
(otherwise) required only to update metadata.
If the FALLOC_FL_KEEP_SIZE flag is additionally specified in mode,
the behavior of the call is similar, but the file size will not be
changed even if offset+len is greater than the file size. This
behavior is the same as when preallocating space with
FALLOC_FL_KEEP_SIZE specified.
Not all filesystems support FALLOC_FL_ZERO_RANGE; if a filesystem
doesn't support the operation, an error is returned. The operation
is supported on at least the following filesystems:
* XFS (since Linux 3.15)
* ext4, for extent-based files (since Linux 3.15)
* SMB3 (since Linux 3.17)
Increasing file space
Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux
4.1) in mode increases the file space by inserting a hole within the
file size without overwriting any existing data. The hole will start
at offset and continue for len bytes. When inserting the hole inside
file, the contents of the file starting at offset will be shifted
upward (i.e., to a higher file offset) by len bytes. Inserting a
hole inside a file increases the file size by len bytes.
This mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE
regarding the granularity of the operation. If the granularity
requirements are not met, fallocate() will fail with the error
EINVAL. If the offset is equal to or greater than the end of file,
an error is returned. For such operations (i.e., inserting a hole at
the end of file), ftruncate(2) should be used.
No other flags may be specified in mode in conjunction with
FALLOC_FL_INSERT_RANGE.
FALLOC_FL_INSERT_RANGE requires filesystem support. Filesystems that
support this operation include XFS (since Linux 4.1) and ext4 (since
Linux 4.2).
这取决于应用程序需要什么 w.r.t 磁盘 space 由于使用任一文件而消耗的磁盘。
FALLOC_FL_PUNCH_HOLE
标志释放块。由于它必须与 FALLOC_FL_KEEP_SIZE
进行逻辑运算,这意味着您最终会得到一个稀疏文件。
另一方面,FALLOC_FL_ZERO_RANGE
为 (offset, length) 分配块(如果不存在)并将其清零。所以实际上,如果文件开始时有漏洞,你就会失去它的一些稀疏性。此外,它是一种将文件区域清零的方法,无需应用程序手动写入 (2) 个零。
fallocate(2) 的所有这些标志通常由 qemu 等虚拟化软件使用。
我正在查看 fallocate 的手册页,但我不明白这两者之间的区别。一个似乎分配块但不写入它们,另一个似乎释放块而不覆盖它们。无论哪种方式,从用户的角度来看,效果似乎都无法区分。请给我解释一下。
Zeroing file space Specifying the FALLOC_FL_ZERO_RANGE flag (available since Linux 3.15) in mode zeroes space in the byte range starting at offset and continuing for len bytes. Within the specified range, blocks are preallocated for the regions that span the holes in the file. After a successful call, subsequent reads from this range will return zeroes.
Zeroing is done within the filesystem preferably by converting the range into unwritten extents. This approach means that the specified range will not be physically zeroed out on the device (except for partial blocks at the either end of the range), and I/O is (otherwise) required only to update metadata. If the FALLOC_FL_KEEP_SIZE flag is additionally specified in mode, the behavior of the call is similar, but the file size will not be changed even if offset+len is greater than the file size. This behavior is the same as when preallocating space with FALLOC_FL_KEEP_SIZE specified. Not all filesystems support FALLOC_FL_ZERO_RANGE; if a filesystem doesn't support the operation, an error is returned. The operation is supported on at least the following filesystems: * XFS (since Linux 3.15) * ext4, for extent-based files (since Linux 3.15) * SMB3 (since Linux 3.17)
Increasing file space Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1) in mode increases the file space by inserting a hole within the file size without overwriting any existing data. The hole will start at offset and continue for len bytes. When inserting the hole inside file, the contents of the file starting at offset will be shifted upward (i.e., to a higher file offset) by len bytes. Inserting a hole inside a file increases the file size by len bytes.
This mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE regarding the granularity of the operation. If the granularity requirements are not met, fallocate() will fail with the error EINVAL. If the offset is equal to or greater than the end of file, an error is returned. For such operations (i.e., inserting a hole at the end of file), ftruncate(2) should be used. No other flags may be specified in mode in conjunction with FALLOC_FL_INSERT_RANGE. FALLOC_FL_INSERT_RANGE requires filesystem support. Filesystems that support this operation include XFS (since Linux 4.1) and ext4 (since Linux 4.2).
这取决于应用程序需要什么 w.r.t 磁盘 space 由于使用任一文件而消耗的磁盘。
FALLOC_FL_PUNCH_HOLE
标志释放块。由于它必须与 FALLOC_FL_KEEP_SIZE
进行逻辑运算,这意味着您最终会得到一个稀疏文件。
FALLOC_FL_ZERO_RANGE
为 (offset, length) 分配块(如果不存在)并将其清零。所以实际上,如果文件开始时有漏洞,你就会失去它的一些稀疏性。此外,它是一种将文件区域清零的方法,无需应用程序手动写入 (2) 个零。
fallocate(2) 的所有这些标志通常由 qemu 等虚拟化软件使用。