打孔和零范围之间的差异

时间:2016-07-22 19:40:36

标签: file filesystems fallocate

我正在查看fallocate的手册页,我不明白这两者的区别。一个似乎分配块但没有写它们,其他似乎解除分配块而不覆盖它们。无论哪种方式,效果似乎与用户的观点无法区分。请解释一下我。

  

归零文件空间          指定FALLOC_FL_ZERO_RANGE标志(自Linux 3.15起可用)          in mode在字节范围内将空格归零,从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).

1 个答案:

答案 0 :(得分:1)

这取决于应用程序想要w.r.t由于使用文件而导致的文件占用的磁盘空间。

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

另一方面,

FALLOC_FL_ZERO_RANGE为(偏移量,长度)分配块(如果尚未存在)并将其清零。因此,如果文件有漏洞,那么实际上你会丢失一些稀疏性。此外,它是一种将文件区域清零而无需手动编写(2)零的应用程序的方法。

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