Ensuring sufficient disk space

Linux provides the posix_fallocate(3) API; its job is to guarantee that sufficient disk space is available for a given range specific to a given file. What that actually means is that whenever the app writes to that file within that range, the write is guaranteed not to fail due to lack of disk space (if it does fail, errno will be set to ENOSPC; that won't happen). It's signature is as follows:

#include <fcntl.h>
int posix_fallocate(int fd, off_t offset, off_t len);

Here are some quick points to note regarding this API:

  • The file is the one referred to by the descriptor fd.
  • The range is from offset for len bytes; in effect, this is the disk space that will be reserved for the file.
  • If the current file size is less than what the range requests (that is, offset+len), then the file is grown to this size; otherwise, the file's size remains unaltered.
  • posix_fallocate(3) is a portable wrapper over the underlying system call fallocate(2).
  • For this API to succeed, the underlying filesystem must support the fallocate; if not, it's emulated (but with a lot of caveats and issues; refer to the man page for more).
  • Also, a CLI utility called fallocate(1) exists to perform the same task from, say, a shell script.
These APIs and tools may come in very useful for software such as backup, cloud provisioning, digitization, and so on, guaranteeing sufficient disk space is available before a long I/O operation begins.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset