| 1 | /* Copyright (C) 2007-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <http://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <fcntl.h> |
| 19 | #include <kernel-features.h> |
| 20 | #include <sysdep.h> |
| 21 | |
| 22 | #define posix_fallocate static internal_fallocate |
| 23 | #include <sysdeps/posix/posix_fallocate.c> |
| 24 | #undef posix_fallocate |
| 25 | |
| 26 | /* The alpha architecture introduced the fallocate system call in |
| 27 | 2.6.33-rc1, so we still need the fallback code. */ |
| 28 | #if !defined __ASSUME_FALLOCATE && defined __NR_fallocate |
| 29 | static int __have_fallocate; |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | /* Reserve storage for the data of the file associated with FD. */ |
| 34 | int |
| 35 | posix_fallocate (int fd, __off_t offset, __off_t len) |
| 36 | { |
| 37 | #ifdef __NR_fallocate |
| 38 | # ifndef __ASSUME_FALLOCATE |
| 39 | if (__glibc_likely (__have_fallocate >= 0)) |
| 40 | # endif |
| 41 | { |
| 42 | INTERNAL_SYSCALL_DECL (err); |
| 43 | # ifdef INTERNAL_SYSCALL_TYPES |
| 44 | int res = INTERNAL_SYSCALL_TYPES (fallocate, err, 4, int, fd, |
| 45 | int, 0, off_t, offset, |
| 46 | off_t, len); |
| 47 | # else |
| 48 | int res = INTERNAL_SYSCALL (fallocate, err, 4, fd, 0, offset, len); |
| 49 | # endif |
| 50 | |
| 51 | if (! INTERNAL_SYSCALL_ERROR_P (res, err)) |
| 52 | return 0; |
| 53 | |
| 54 | # ifndef __ASSUME_FALLOCATE |
| 55 | if (__glibc_unlikely (INTERNAL_SYSCALL_ERRNO (res, err) == ENOSYS)) |
| 56 | __have_fallocate = -1; |
| 57 | else |
| 58 | # endif |
| 59 | if (INTERNAL_SYSCALL_ERRNO (res, err) != EOPNOTSUPP) |
| 60 | return INTERNAL_SYSCALL_ERRNO (res, err); |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | return internal_fallocate (fd, offset, len); |
| 65 | } |
| 66 | weak_alias (posix_fallocate, posix_fallocate64) |
| 67 | |