2

I am looking at man page of fallocate and I do not understand the difference between these two. One seems to allocate blocks but without writing them, other seems to deallocate blocks without overwriting them. Either way, effect seems to be indistinguishable from user perspective. Please explain this me.

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).
ArekBulski
  • 4,520
  • 4
  • 39
  • 61

1 Answers1

1

It depends on what the application wants w.r.t the disk space consumed by a file as a result of using either.

The FALLOC_FL_PUNCH_HOLE flag deallocates blocks. Since it has to be ORed with FALLOC_FL_KEEP_SIZE, what this means is you end up in a sparse file.

FALLOC_FL_ZERO_RANGE on the other hand, allocates blocks for the (offset, length) if not already present and zeroes it out. So in effect you are losing some of its sparseness if the file had holes to begin with. Also, it is a method of zeroing out regions of a file without the application manually having to write(2) zeroes.

All these flags to fallocate(2) are typically used by virtualization software like qemu.

itisravi
  • 3,406
  • 3
  • 23
  • 30