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