1303bf60bSsdebnath /*
2303bf60bSsdebnath  * CDDL HEADER START
3303bf60bSsdebnath  *
4303bf60bSsdebnath  * The contents of this file are subject to the terms of the
5f841f6adSraf  * Common Development and Distribution License (the "License").
6f841f6adSraf  * You may not use this file except in compliance with the License.
7303bf60bSsdebnath  *
8303bf60bSsdebnath  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9303bf60bSsdebnath  * or http://www.opensolaris.org/os/licensing.
10303bf60bSsdebnath  * See the License for the specific language governing permissions
11303bf60bSsdebnath  * and limitations under the License.
12303bf60bSsdebnath  *
13303bf60bSsdebnath  * When distributing Covered Code, include this CDDL HEADER in each
14303bf60bSsdebnath  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15303bf60bSsdebnath  * If applicable, add the following below this CDDL HEADER, with the
16303bf60bSsdebnath  * fields enclosed by brackets "[]" replaced with your own identifying
17303bf60bSsdebnath  * information: Portions Copyright [yyyy] [name of copyright owner]
18303bf60bSsdebnath  *
19303bf60bSsdebnath  * CDDL HEADER END
20303bf60bSsdebnath  */
21e8031f0aSraf 
22303bf60bSsdebnath /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24303bf60bSsdebnath  * Use is subject to license terms.
25303bf60bSsdebnath  */
26303bf60bSsdebnath 
277257d1b4Sraf #include "lint.h"
28303bf60bSsdebnath #include <fcntl.h>
29*019c3c43Sraf #include <errno.h>
30303bf60bSsdebnath #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;
41303bf60bSsdebnath 
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 }
53303bf60bSsdebnath 
54303bf60bSsdebnath int
posix_fallocate(int fd,off_t offset,off_t len)55303bf60bSsdebnath posix_fallocate(int fd, off_t offset, off_t len)
56303bf60bSsdebnath {
57303bf60bSsdebnath 	struct flock lck;
58*019c3c43Sraf 	int error;
59*019c3c43Sraf 
60*019c3c43Sraf 	if (offset < 0 || len <= 0)
61*019c3c43Sraf 		return (EINVAL);
62303bf60bSsdebnath 
63303bf60bSsdebnath 	lck.l_whence = 0;
64303bf60bSsdebnath 	lck.l_start = offset;
65303bf60bSsdebnath 	lck.l_len = len;
66303bf60bSsdebnath 	lck.l_type = F_WRLCK;
67303bf60bSsdebnath 
68303bf60bSsdebnath 	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);
74303bf60bSsdebnath 	}
75303bf60bSsdebnath 	return (0);
76303bf60bSsdebnath }
77303bf60bSsdebnath 
78f841f6adSraf #if !defined(_LP64)
79303bf60bSsdebnath 
80303bf60bSsdebnath int
posix_fallocate64(int fd,off64_t offset,off64_t len)81303bf60bSsdebnath posix_fallocate64(int fd, off64_t offset, off64_t len)
82303bf60bSsdebnath {
83303bf60bSsdebnath 	struct flock64 lck;
84*019c3c43Sraf 	int error;
85*019c3c43Sraf 
86*019c3c43Sraf 	if (offset < 0 || len <= 0)
87*019c3c43Sraf 		return (EINVAL);
88303bf60bSsdebnath 
89303bf60bSsdebnath 	lck.l_whence = 0;
90303bf60bSsdebnath 	lck.l_start = offset;
91303bf60bSsdebnath 	lck.l_len = len;
92303bf60bSsdebnath 	lck.l_type = F_WRLCK;
93303bf60bSsdebnath 
94303bf60bSsdebnath 	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);
100303bf60bSsdebnath 	}
101303bf60bSsdebnath 	return (0);
102303bf60bSsdebnath }
103303bf60bSsdebnath 
104303bf60bSsdebnath #endif
105