Blame sysdeps/posix/posix_fallocate64.c

Packit 6c4009
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <sys/fcntl.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
#include <sys/statfs.h>
Packit 6c4009
Packit 6c4009
/* Reserve storage for the data of the file associated with FD.  This
Packit 6c4009
   emulation is far from perfect, but the kernel cannot do not much
Packit 6c4009
   better for network file systems, either.  */
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__posix_fallocate64_l64 (int fd, __off64_t offset, __off64_t len)
Packit 6c4009
{
Packit 6c4009
  struct stat64 st;
Packit 6c4009
Packit 6c4009
  if (offset < 0 || len < 0)
Packit 6c4009
    return EINVAL;
Packit 6c4009
Packit 6c4009
  /* Perform overflow check.  The outer cast relies on a GCC
Packit 6c4009
     extension.  */
Packit 6c4009
  if ((__off64_t) ((uint64_t) offset + (uint64_t) len) < 0)
Packit 6c4009
    return EFBIG;
Packit 6c4009
Packit 6c4009
  /* pwrite64 below will not do the right thing in O_APPEND mode.  */
Packit 6c4009
  {
Packit 6c4009
    int flags = __fcntl (fd, F_GETFL, 0);
Packit 6c4009
    if (flags < 0 || (flags & O_APPEND) != 0)
Packit 6c4009
      return EBADF;
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* We have to make sure that this is really a regular file.  */
Packit 6c4009
  if (__fxstat64 (_STAT_VER, fd, &st) != 0)
Packit 6c4009
    return EBADF;
Packit 6c4009
  if (S_ISFIFO (st.st_mode))
Packit 6c4009
    return ESPIPE;
Packit 6c4009
  if (! S_ISREG (st.st_mode))
Packit 6c4009
    return ENODEV;
Packit 6c4009
Packit 6c4009
  if (len == 0)
Packit 6c4009
    {
Packit 6c4009
      /* This is racy, but there is no good way to satisfy a
Packit 6c4009
	 zero-length allocation request.  */
Packit 6c4009
      if (st.st_size < offset)
Packit 6c4009
	{
Packit 6c4009
	  int ret = __ftruncate64 (fd, offset);
Packit 6c4009
Packit 6c4009
	  if (ret != 0)
Packit 6c4009
	    ret = errno;
Packit 6c4009
	  return ret;
Packit 6c4009
	}
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Minimize data transfer for network file systems, by issuing
Packit 6c4009
     single-byte write requests spaced by the file system block size.
Packit 6c4009
     (Most local file systems have fallocate support, so this fallback
Packit 6c4009
     code is not used there.)  */
Packit 6c4009
Packit 6c4009
  unsigned increment;
Packit 6c4009
  {
Packit 6c4009
    struct statfs64 f;
Packit 6c4009
Packit 6c4009
    if (__fstatfs64 (fd, &f) != 0)
Packit 6c4009
      return errno;
Packit 6c4009
    if (f.f_bsize == 0)
Packit 6c4009
      increment = 512;
Packit 6c4009
    else if (f.f_bsize < 4096)
Packit 6c4009
      increment = f.f_bsize;
Packit 6c4009
    else
Packit 6c4009
      /* NFS clients do not propagate the block size of the underlying
Packit 6c4009
	 storage and may report a much larger value which would still
Packit 6c4009
	 leave holes after the loop below, so we cap the increment at
Packit 6c4009
	 4096.  */
Packit 6c4009
      increment = 4096;
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Write a null byte to every block.  This is racy; we currently
Packit 6c4009
     lack a better option.  Compare-and-swap against a file mapping
Packit 6c4009
     might address local races, but requires interposition of a signal
Packit 6c4009
     handler to catch SIGBUS.  */
Packit 6c4009
  for (offset += (len - 1) % increment; len > 0; offset += increment)
Packit 6c4009
    {
Packit 6c4009
      len -= increment;
Packit 6c4009
Packit 6c4009
      if (offset < st.st_size)
Packit 6c4009
	{
Packit 6c4009
	  unsigned char c;
Packit 6c4009
	  ssize_t rsize = __libc_pread64 (fd, &c, 1, offset);
Packit 6c4009
Packit 6c4009
	  if (rsize < 0)
Packit 6c4009
	    return errno;
Packit 6c4009
	  /* If there is a non-zero byte, the block must have been
Packit 6c4009
	     allocated already.  */
Packit 6c4009
	  else if (rsize == 1 && c != 0)
Packit 6c4009
	    continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (__libc_pwrite64 (fd, "", 1, offset) != 1)
Packit 6c4009
	return errno;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#undef __posix_fallocate64_l64
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
#include <bits/wordsize.h>
Packit 6c4009
Packit 6c4009
#if __WORDSIZE == 32 && SHLIB_COMPAT(libc, GLIBC_2_2, GLIBC_2_3_3)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
attribute_compat_text_section
Packit 6c4009
__posix_fallocate64_l32 (int fd, off64_t offset, size_t len)
Packit 6c4009
{
Packit 6c4009
  return __posix_fallocate64_l64 (fd, offset, len);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
versioned_symbol (libc, __posix_fallocate64_l64, posix_fallocate64,
Packit 6c4009
		  GLIBC_2_3_3);
Packit 6c4009
compat_symbol (libc, __posix_fallocate64_l32, posix_fallocate64, GLIBC_2_2);
Packit 6c4009
#else
Packit 6c4009
strong_alias (__posix_fallocate64_l64, posix_fallocate64);
Packit 6c4009
#endif