00db10
commit 7fe9e2e089f4990b7d18d0798f591ab276b15f2b
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Fri Jun 5 10:50:38 2015 +0200
00db10
00db10
    posix_fallocate: Emulation fixes and documentation [BZ #15661]
00db10
    
00db10
    Handle signed integer overflow correctly.  Detect and reject O_APPEND.
00db10
    Document drawbacks of emulation.
00db10
    
00db10
    This does not completely address bug 15661, but improves the situation
00db10
    somewhat.
00db10
00db10
commit 543ef578c3304661713950b37abd0c916f52ecf0
00db10
Author: Paul Eggert <eggert@cs.ucla.edu>
00db10
Date:   Tue Aug 25 23:42:01 2015 -0700
00db10
00db10
    Fix broken overflow check in posix_fallocate [BZ 18873]
00db10
    
00db10
    * sysdeps/posix/posix_fallocate.c (posix_fallocate):
00db10
    * sysdeps/posix/posix_fallocate64.c (__posix_fallocate64_l64):
00db10
    Fix parenthesization typo.
00db10
00db10
Index: b/manual/filesys.texi
00db10
===================================================================
00db10
--- a/manual/filesys.texi
00db10
+++ b/manual/filesys.texi
00db10
@@ -1723,6 +1723,7 @@ modify the attributes of a file.
00db10
                                  access a file.
00db10
 * File Times::                  About the time attributes of a file.
00db10
 * File Size::			Manually changing the size of a file.
00db10
+* Storage Allocation::          Allocate backing storage for files.
00db10
 @end menu
00db10
 
00db10
 @node Attribute Meanings
00db10
@@ -3232,6 +3233,99 @@ is a requirement of @code{mmap}.  The pr
00db10
 real size, and when it has finished a final @code{ftruncate} call should
00db10
 set the real size of the file.
00db10
 
00db10
+@node Storage Allocation
00db10
+@subsection Storage Allocation
00db10
+@cindex allocating file storage
00db10
+@cindex file allocation
00db10
+@cindex storage allocating
00db10
+
00db10
+@cindex file fragmentation
00db10
+@cindex fragmentation of files
00db10
+@cindex sparse files
00db10
+@cindex files, sparse
00db10
+Most file systems support allocating large files in a non-contiguous
00db10
+fashion: the file is split into @emph{fragments} which are allocated
00db10
+sequentially, but the fragments themselves can be scattered across the
00db10
+disk.  File systems generally try to avoid such fragmentation because it
00db10
+decreases performance, but if a file gradually increases in size, there
00db10
+might be no other option than to fragment it.  In addition, many file
00db10
+systems support @emph{sparse files} with @emph{holes}: regions of null
00db10
+bytes for which no backing storage has been allocated by the file
00db10
+system.  When the holes are finally overwritten with data, fragmentation
00db10
+can occur as well.
00db10
+
00db10
+Explicit allocation of storage for yet-unwritten parts of the file can
00db10
+help the system to avoid fragmentation.  Additionally, if storage
00db10
+pre-allocation fails, it is possible to report the out-of-disk error
00db10
+early, often without filling up the entire disk.  However, due to
00db10
+deduplication, copy-on-write semantics, and file compression, such
00db10
+pre-allocation may not reliably prevent the out-of-disk-space error from
00db10
+occurring later.  Checking for write errors is still required, and
00db10
+writes to memory-mapped regions created with @code{mmap} can still
00db10
+result in @code{SIGBUS}.
00db10
+
00db10
+@deftypefun int posix_fallocate (int @var{fd}, off_t @var{offset}, off_t @var{length})
00db10
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
00db10
+@c If the file system does not support allocation,
00db10
+@c @code{posix_fallocate} has a race with file extension (if
00db10
+@c @var{length} is zero) or with concurrent writes of non-NUL bytes (if
00db10
+@c @var{length} is positive).
00db10
+
00db10
+Allocate backing store for the region of @var{length} bytes starting at
00db10
+byte @var{offset} in the file for the descriptor @var{fd}.  The file
00db10
+length is increased to @samp{@var{length} + @var{offset}} if necessary.
00db10
+
00db10
+@var{fd} must be a regular file opened for writing, or @code{EBADF} is
00db10
+returned.  If there is insufficient disk space to fulfill the allocation
00db10
+request, @code{ENOSPC} is returned.
00db10
+
00db10
+@strong{Note:} If @code{fallocate} is not available (because the file
00db10
+system does not support it), @code{posix_fallocate} is emulated, which
00db10
+has the following drawbacks:
00db10
+
00db10
+@itemize @bullet
00db10
+@item
00db10
+It is very inefficient because all file system blocks in the requested
00db10
+range need to be examined (even if they have been allocated before) and
00db10
+potentially rewritten.  In contrast, with proper @code{fallocate}
00db10
+support (see below), the file system can examine the internal file
00db10
+allocation data structures and eliminate holes directly, maybe even
00db10
+using unwritten extents (which are pre-allocated but uninitialized on
00db10
+disk).
00db10
+
00db10
+@item
00db10
+There is a race condition if another thread or process modifies the
00db10
+underlying file in the to-be-allocated area.  Non-null bytes could be
00db10
+overwritten with null bytes.
00db10
+
00db10
+@item
00db10
+If @var{fd} has been opened with the @code{O_APPEND} flag, the function
00db10
+will fail with an @code{errno} value of @code{EBADF}.
00db10
+
00db10
+@item
00db10
+If @var{length} is zero, @code{ftruncate} is used to increase the file
00db10
+size as requested, without allocating file system blocks.  There is a
00db10
+race condition which means that @code{ftruncate} can accidentally
00db10
+truncate the file if it has been extended concurrently.
00db10
+@end itemize
00db10
+
00db10
+On Linux, if an application does not benefit from emulation or if the
00db10
+emulation is harmful due to its inherent race conditions, the
00db10
+application can use the Linux-specific @code{fallocate} function, with a
00db10
+zero flag argument.  For the @code{fallocate} function, @theglibc{} does
00db10
+not perform allocation emulation if the file system does not support
00db10
+allocation.  Instead, an @code{EOPNOTSUPP} is returned to the caller.
00db10
+
00db10
+@end deftypefun
00db10
+
00db10
+@deftypefun int posix_fallocate64 (int @var{fd}, off64_t @var{length}, off64_t @var{offset})
00db10
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
00db10
+
00db10
+This function is a variant of @code{posix_fallocate64} which accepts
00db10
+64-bit file offsets on all platforms.
00db10
+
00db10
+@end deftypefun
00db10
+
00db10
 @node Making Special Files
00db10
 @section Making Special Files
00db10
 @cindex creating special files
00db10
Index: b/sysdeps/posix/posix_fallocate.c
00db10
===================================================================
00db10
--- a/sysdeps/posix/posix_fallocate.c
00db10
+++ b/sysdeps/posix/posix_fallocate.c
00db10
@@ -18,26 +18,36 @@
00db10
 #include <errno.h>
00db10
 #include <fcntl.h>
00db10
 #include <unistd.h>
00db10
+#include <stdint.h>
00db10
+#include <sys/fcntl.h>
00db10
 #include <sys/stat.h>
00db10
 #include <sys/statfs.h>
00db10
 
00db10
-/* Reserve storage for the data of the file associated with FD.  */
00db10
+/* Reserve storage for the data of the file associated with FD.  This
00db10
+   emulation is far from perfect, but the kernel cannot do not much
00db10
+   better for network file systems, either.  */
00db10
 
00db10
 int
00db10
 posix_fallocate (int fd, __off_t offset, __off_t len)
00db10
 {
00db10
   struct stat64 st;
00db10
-  struct statfs f;
00db10
 
00db10
-  /* `off_t' is a signed type.  Therefore we can determine whether
00db10
-     OFFSET + LEN is too large if it is a negative value.  */
00db10
   if (offset < 0 || len < 0)
00db10
     return EINVAL;
00db10
-  if (offset + len < 0)
00db10
+
00db10
+  /* Perform overflow check.  The outer cast relies on a GCC
00db10
+     extension.  */
00db10
+  if ((__off_t) ((uint64_t) offset + (uint64_t) len) < 0)
00db10
     return EFBIG;
00db10
 
00db10
-  /* First thing we have to make sure is that this is really a regular
00db10
-     file.  */
00db10
+  /* pwrite below will not do the right thing in O_APPEND mode.  */
00db10
+  {
00db10
+    int flags = __fcntl (fd, F_GETFL, 0);
00db10
+    if (flags < 0 || (flags & O_APPEND) != 0)
00db10
+      return EBADF;
00db10
+  }
00db10
+
00db10
+  /* We have to make sure that this is really a regular file.  */
00db10
   if (__fxstat64 (_STAT_VER, fd, &st) != 0)
00db10
     return EBADF;
00db10
   if (S_ISFIFO (st.st_mode))
00db10
@@ -47,6 +57,8 @@ posix_fallocate (int fd, __off_t offset,
00db10
 
00db10
   if (len == 0)
00db10
     {
00db10
+      /* This is racy, but there is no good way to satisfy a
00db10
+	 zero-length allocation request.  */
00db10
       if (st.st_size < offset)
00db10
 	{
00db10
 	  int ret = __ftruncate (fd, offset);
00db10
@@ -58,19 +70,36 @@ posix_fallocate (int fd, __off_t offset,
00db10
       return 0;
00db10
     }
00db10
 
00db10
-  /* We have to know the block size of the filesystem to get at least some
00db10
-     sort of performance.  */
00db10
-  if (__fstatfs (fd, &f) != 0)
00db10
-    return errno;
00db10
-
00db10
-  /* Try to play safe.  */
00db10
-  if (f.f_bsize == 0)
00db10
-    f.f_bsize = 512;
00db10
-
00db10
-  /* Write something to every block.  */
00db10
-  for (offset += (len - 1) % f.f_bsize; len > 0; offset += f.f_bsize)
00db10
+  /* Minimize data transfer for network file systems, by issuing
00db10
+     single-byte write requests spaced by the file system block size.
00db10
+     (Most local file systems have fallocate support, so this fallback
00db10
+     code is not used there.)  */
00db10
+
00db10
+  unsigned increment;
00db10
+  {
00db10
+    struct statfs64 f;
00db10
+
00db10
+    if (__fstatfs64 (fd, &f) != 0)
00db10
+      return errno;
00db10
+    if (f.f_bsize == 0)
00db10
+      increment = 512;
00db10
+    else if (f.f_bsize < 4096)
00db10
+      increment = f.f_bsize;
00db10
+    else
00db10
+      /* NFS does not propagate the block size of the underlying
00db10
+	 storage and may report a much larger value which would still
00db10
+	 leave holes after the loop below, so we cap the increment at
00db10
+	 4096.  */
00db10
+      increment = 4096;
00db10
+  }
00db10
+
00db10
+  /* Write a null byte to every block.  This is racy; we currently
00db10
+     lack a better option.  Compare-and-swap against a file mapping
00db10
+     might additional local races, but requires interposition of a
00db10
+     signal handler to catch SIGBUS.  */
00db10
+  for (offset += (len - 1) % increment; len > 0; offset += increment)
00db10
     {
00db10
-      len -= f.f_bsize;
00db10
+      len -= increment;
00db10
 
00db10
       if (offset < st.st_size)
00db10
 	{
00db10
Index: b/sysdeps/posix/posix_fallocate64.c
00db10
===================================================================
00db10
--- a/sysdeps/posix/posix_fallocate64.c
00db10
+++ b/sysdeps/posix/posix_fallocate64.c
00db10
@@ -18,26 +18,36 @@
00db10
 #include <errno.h>
00db10
 #include <fcntl.h>
00db10
 #include <unistd.h>
00db10
+#include <stdint.h>
00db10
+#include <sys/fcntl.h>
00db10
 #include <sys/stat.h>
00db10
 #include <sys/statfs.h>
00db10
 
00db10
-/* Reserve storage for the data of the file associated with FD.  */
00db10
+/* Reserve storage for the data of the file associated with FD.  This
00db10
+   emulation is far from perfect, but the kernel cannot do not much
00db10
+   better for network file systems, either.  */
00db10
 
00db10
 int
00db10
 __posix_fallocate64_l64 (int fd, __off64_t offset, __off64_t len)
00db10
 {
00db10
   struct stat64 st;
00db10
-  struct statfs64 f;
00db10
 
00db10
-  /* `off64_t' is a signed type.  Therefore we can determine whether
00db10
-     OFFSET + LEN is too large if it is a negative value.  */
00db10
   if (offset < 0 || len < 0)
00db10
     return EINVAL;
00db10
-  if (offset + len < 0)
00db10
+
00db10
+  /* Perform overflow check.  The outer cast relies on a GCC
00db10
+     extension.  */
00db10
+  if ((__off64_t) ((uint64_t) offset + (uint64_t) len) < 0)
00db10
     return EFBIG;
00db10
 
00db10
-  /* First thing we have to make sure is that this is really a regular
00db10
-     file.  */
00db10
+  /* pwrite64 below will not do the right thing in O_APPEND mode.  */
00db10
+  {
00db10
+    int flags = __fcntl (fd, F_GETFL, 0);
00db10
+    if (flags < 0 || (flags & O_APPEND) != 0)
00db10
+      return EBADF;
00db10
+  }
00db10
+
00db10
+  /* We have to make sure that this is really a regular file.  */
00db10
   if (__fxstat64 (_STAT_VER, fd, &st) != 0)
00db10
     return EBADF;
00db10
   if (S_ISFIFO (st.st_mode))
00db10
@@ -47,6 +57,8 @@ __posix_fallocate64_l64 (int fd, __off64
00db10
 
00db10
   if (len == 0)
00db10
     {
00db10
+      /* This is racy, but there is no good way to satisfy a
00db10
+	 zero-length allocation request.  */
00db10
       if (st.st_size < offset)
00db10
 	{
00db10
 	  int ret = __ftruncate64 (fd, offset);
00db10
@@ -58,19 +70,36 @@ __posix_fallocate64_l64 (int fd, __off64
00db10
       return 0;
00db10
     }
00db10
 
00db10
-  /* We have to know the block size of the filesystem to get at least some
00db10
-     sort of performance.  */
00db10
-  if (__fstatfs64 (fd, &f) != 0)
00db10
-    return errno;
00db10
-
00db10
-  /* Try to play safe.  */
00db10
-  if (f.f_bsize == 0)
00db10
-    f.f_bsize = 512;
00db10
-
00db10
-  /* Write something to every block.  */
00db10
-  for (offset += (len - 1) % f.f_bsize; len > 0; offset += f.f_bsize)
00db10
+  /* Minimize data transfer for network file systems, by issuing
00db10
+     single-byte write requests spaced by the file system block size.
00db10
+     (Most local file systems have fallocate support, so this fallback
00db10
+     code is not used there.)  */
00db10
+
00db10
+  unsigned increment;
00db10
+  {
00db10
+    struct statfs64 f;
00db10
+
00db10
+    if (__fstatfs64 (fd, &f) != 0)
00db10
+      return errno;
00db10
+    if (f.f_bsize == 0)
00db10
+      increment = 512;
00db10
+    else if (f.f_bsize < 4096)
00db10
+      increment = f.f_bsize;
00db10
+    else
00db10
+      /* NFS clients do not propagate the block size of the underlying
00db10
+	 storage and may report a much larger value which would still
00db10
+	 leave holes after the loop below, so we cap the increment at
00db10
+	 4096.  */
00db10
+      increment = 4096;
00db10
+  }
00db10
+
00db10
+  /* Write a null byte to every block.  This is racy; we currently
00db10
+     lack a better option.  Compare-and-swap against a file mapping
00db10
+     might address local races, but requires interposition of a signal
00db10
+     handler to catch SIGBUS.  */
00db10
+  for (offset += (len - 1) % increment; len > 0; offset += increment)
00db10
     {
00db10
-      len -= f.f_bsize;
00db10
+      len -= increment;
00db10
 
00db10
       if (offset < st.st_size)
00db10
 	{