【问题标题】:Difference between punch hole and zero range打孔和零范围的区别
【发布时间】:2016-11-26 19:29:43
【问题描述】:

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

归零文件空间 指定 FALLOC_FL_ZERO_RANGE 标志(自 Linux 3.15 起可用) 在模式中,从偏移量开始的字节范围内的空间为零,并且 继续 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) in mode 通过在文件中插入一个洞来增加文件空间 文件大小而不覆盖任何现有数据。洞会开始 在偏移处并继续 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).

【问题讨论】:

    标签: file filesystems fallocate


    【解决方案1】:

    这取决于应用程序想要什么,而文件因使用其中任何一个而消耗的磁盘空间。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多