1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include "lint.h"
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 /*
34 * Return the proper Posix error number for a failed (EINVAL) fcntl() operation.
35 */
36 static int
fallocate_errno(int fd)37 fallocate_errno(int fd)
38 {
39 struct stat64 statb;
40 int error;
41
42 if (fstat64(fd, &statb) != 0) /* can't happen? */
43 error = EBADF;
44 else if (S_ISFIFO(statb.st_mode)) /* pipe or FIFO */
45 error = ESPIPE;
46 else if (!S_ISREG(statb.st_mode)) /* not a regular file */
47 error = ENODEV;
48 else /* the file system doesn't support F_ALLOCSP */
49 error = EINVAL;
50
51 return (error);
52 }
53
54 int
posix_fallocate(int fd,off_t offset,off_t len)55 posix_fallocate(int fd, off_t offset, off_t len)
56 {
57 struct flock lck;
58 int error;
59
60 if (offset < 0 || len <= 0)
61 return (EINVAL);
62
63 lck.l_whence = 0;
64 lck.l_start = offset;
65 lck.l_len = len;
66 lck.l_type = F_WRLCK;
67
68 if (fcntl(fd, F_ALLOCSP, &lck) == -1) {
69 if ((error = errno) == EINVAL)
70 error = fallocate_errno(fd);
71 else if (error == EOVERFLOW)
72 error = EFBIG;
73 return (error);
74 }
75 return (0);
76 }
77
78 #if !defined(_LP64)
79
80 int
posix_fallocate64(int fd,off64_t offset,off64_t len)81 posix_fallocate64(int fd, off64_t offset, off64_t len)
82 {
83 struct flock64 lck;
84 int error;
85
86 if (offset < 0 || len <= 0)
87 return (EINVAL);
88
89 lck.l_whence = 0;
90 lck.l_start = offset;
91 lck.l_len = len;
92 lck.l_type = F_WRLCK;
93
94 if (fcntl(fd, F_ALLOCSP64, &lck) == -1) {
95 if ((error = errno) == EINVAL)
96 error = fallocate_errno(fd);
97 else if (error == EOVERFLOW)
98 error = EFBIG;
99 return (error);
100 }
101 return (0);
102 }
103
104 #endif
105