usr/src/lib/libc/port/gen/posix_fallocate.c
author Jon Tibble <meths@btinternet.com>
Thu, 09 Dec 2010 22:32:39 +0100
changeset 13255 4afa820d78b9
parent 7088 87e6b40103da
permissions -rw-r--r--
298 SPARC build fails in smt_pause.o 478 Build needs fixing for pkgdepend flag day Reviewed by: [email protected] Reviewed by: [email protected] Reviewed by: [email protected] Approved by: [email protected]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     1
/*
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     2
 * CDDL HEADER START
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     3
 *
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
2248
4609e8bb25ad 6416832 libaio and librt can and should be folded into libc
raf
parents: 1219
diff changeset
     5
 * Common Development and Distribution License (the "License").
4609e8bb25ad 6416832 libaio and librt can and should be folded into libc
raf
parents: 1219
diff changeset
     6
 * You may not use this file except in compliance with the License.
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     7
 *
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    11
 * and limitations under the License.
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    12
 *
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    18
 *
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    19
 * CDDL HEADER END
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    20
 */
1219
f89f56c2d9ac 6362982 namespace pollution/protection in libc
raf
parents: 923
diff changeset
    21
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    22
/*
6812
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 2248
diff changeset
    23
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    24
 * Use is subject to license terms.
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    25
 */
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    26
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    27
#pragma ident	"%Z%%M%	%I%	%E% SMI"
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    28
6812
febeba71273d PSARC 2008/309 expunge synonyms.h
raf
parents: 2248
diff changeset
    29
#include "lint.h"
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    30
#include <fcntl.h>
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    31
#include <errno.h>
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    32
#include <sys/types.h>
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    33
#include <sys/stat.h>
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    34
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    35
/*
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    36
 * Return the proper Posix error number for a failed (EINVAL) fcntl() operation.
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    37
 */
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    38
static int
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    39
fallocate_errno(int fd)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    40
{
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    41
	struct stat64 statb;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    42
	int error;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    43
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    44
	if (fstat64(fd, &statb) != 0)		/* can't happen? */
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    45
		error = EBADF;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    46
	else if (S_ISFIFO(statb.st_mode))	/* pipe or FIFO */
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    47
		error = ESPIPE;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    48
	else if (!S_ISREG(statb.st_mode))	/* not a regular file */
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    49
		error = ENODEV;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    50
	else			/* the file system doesn't support F_ALLOCSP */
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    51
		error = EINVAL;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    52
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    53
	return (error);
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    54
}
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    55
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    56
int
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    57
posix_fallocate(int fd, off_t offset, off_t len)
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    58
{
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    59
	struct flock lck;
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    60
	int error;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    61
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    62
	if (offset < 0 || len <= 0)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    63
		return (EINVAL);
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    64
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    65
	lck.l_whence = 0;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    66
	lck.l_start = offset;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    67
	lck.l_len = len;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    68
	lck.l_type = F_WRLCK;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    69
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    70
	if (fcntl(fd, F_ALLOCSP, &lck) == -1) {
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    71
		if ((error = errno) == EINVAL)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    72
			error = fallocate_errno(fd);
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    73
		else if (error == EOVERFLOW)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    74
			error = EFBIG;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    75
		return (error);
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    76
	}
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    77
	return (0);
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    78
}
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    79
2248
4609e8bb25ad 6416832 libaio and librt can and should be folded into libc
raf
parents: 1219
diff changeset
    80
#if !defined(_LP64)
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    81
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    82
int
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    83
posix_fallocate64(int fd, off64_t offset, off64_t len)
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    84
{
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    85
	struct flock64 lck;
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    86
	int error;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    87
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    88
	if (offset < 0 || len <= 0)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    89
		return (EINVAL);
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    90
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    91
	lck.l_whence = 0;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    92
	lck.l_start = offset;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    93
	lck.l_len = len;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    94
	lck.l_type = F_WRLCK;
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    95
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
    96
	if (fcntl(fd, F_ALLOCSP64, &lck) == -1) {
7088
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    97
		if ((error = errno) == EINVAL)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    98
			error = fallocate_errno(fd);
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
    99
		else if (error == EOVERFLOW)
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
   100
			error = EFBIG;
87e6b40103da PSARC 2008/421 _POSIX_ADVISORY_INFO
raf
parents: 6812
diff changeset
   101
		return (error);
923
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
   102
	}
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
   103
	return (0);
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
   104
}
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
   105
78f6e60ae914 PSARC 2004/422 posix_fallocate
sdebnath
parents:
diff changeset
   106
#endif