2

我正在查看fallocate的手册页,但我不明白这两者之间的区别。一种似乎分配了块但没有写入它们,另一种似乎释放了块而不覆盖它们。无论哪种方式,从用户的角度来看,效果似乎都无法区分。请给我解释一下。

清零文件空间在模式中指定 FALLOC_FL_ZERO_RANGE 标志(自 Linux 3.15 起可用)会将字节范围内的空间清零,从 offset 开始并持续到 len 个字节。在指定范围内,为跨越文件中空洞的区域预先分配块。成功调用后,从此范围内的后续读取将返回零。

   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)

增加文件空间 在模式中指定 FALLOC_FL_INSERT_RANGE 标志(自 Linux 4.1 起可用)通过在文件大小内插入一个洞而不覆盖任何现有数据来增加文件空间。该孔将从偏移量开始并持续 len 个字节。当在文件中插入孔时,从偏移量开始的文件内容将向上移动(即到更高的文件偏移量) len 个字节。在文件中插入一个洞会使文件大小增加 len 个字节。

   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).
4

1 回答 1

1

这取决于应用程序想要什么文件占用的磁盘空间作为使用任何一个的结果。

FALLOC_FL_PUNCH_HOLE标志释放块。由于它必须与 进行 ORed FALLOC_FL_KEEP_SIZE,这意味着您最终会得到一个稀疏文件。

FALLOC_FL_ZERO_RANGE另一方面,如果 (offset, length) 不存在,则为 (offset, length) 分配块并将其清零。因此,如果文件一开始就有漏洞,那么实际上你会失去一些稀疏性。此外,它是一种将文件区域清零的方法,而无需应用程序手动写入 (2) 个零。

fallocate(2) 的所有这些标志通常由 qemu 等虚拟化软件使用。

于 2016-07-28T09:02:59.850 回答