1*0a6a1f1dSLionel Sambuc.\" $NetBSD: fdiscard.2,v 1.3 2015/02/01 15:24:15 christos Exp $ 2*0a6a1f1dSLionel Sambuc.\" 3*0a6a1f1dSLionel Sambuc.\" Copyright (c) 2014 The NetBSD Foundation, Inc. 4*0a6a1f1dSLionel Sambuc.\" All rights reserved. 5*0a6a1f1dSLionel Sambuc.\" 6*0a6a1f1dSLionel Sambuc.\" This code is derived from software contributed to The NetBSD Foundation 7*0a6a1f1dSLionel Sambuc.\" by David A. Holland. 8*0a6a1f1dSLionel Sambuc.\" 9*0a6a1f1dSLionel Sambuc.\" Redistribution and use in source and binary forms, with or without 10*0a6a1f1dSLionel Sambuc.\" modification, are permitted provided that the following conditions 11*0a6a1f1dSLionel Sambuc.\" are met: 12*0a6a1f1dSLionel Sambuc.\" 1. Redistributions of source code must retain the above copyright 13*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer. 14*0a6a1f1dSLionel Sambuc.\" 2. Redistributions in binary form must reproduce the above copyright 15*0a6a1f1dSLionel Sambuc.\" notice, this list of conditions and the following disclaimer in the 16*0a6a1f1dSLionel Sambuc.\" documentation and/or other materials provided with the distribution. 17*0a6a1f1dSLionel Sambuc.\" 18*0a6a1f1dSLionel Sambuc.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19*0a6a1f1dSLionel Sambuc.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20*0a6a1f1dSLionel Sambuc.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21*0a6a1f1dSLionel Sambuc.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22*0a6a1f1dSLionel Sambuc.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*0a6a1f1dSLionel Sambuc.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*0a6a1f1dSLionel Sambuc.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*0a6a1f1dSLionel Sambuc.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*0a6a1f1dSLionel Sambuc.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*0a6a1f1dSLionel Sambuc.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*0a6a1f1dSLionel Sambuc.\" POSSIBILITY OF SUCH DAMAGE. 29*0a6a1f1dSLionel Sambuc.\" 30*0a6a1f1dSLionel Sambuc.Dd February 1, 2015 31*0a6a1f1dSLionel Sambuc.Dt FDISCARD 2 32*0a6a1f1dSLionel Sambuc.Os 33*0a6a1f1dSLionel Sambuc.Sh NAME 34*0a6a1f1dSLionel Sambuc.Nm posix_fallocate , 35*0a6a1f1dSLionel Sambuc.Nm fdiscard 36*0a6a1f1dSLionel Sambuc.Nd allocate or discard backing store for files 37*0a6a1f1dSLionel Sambuc.Sh LIBRARY 38*0a6a1f1dSLionel Sambuc.Lb libc 39*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 40*0a6a1f1dSLionel Sambuc.In unistd.h 41*0a6a1f1dSLionel Sambuc.Ft int 42*0a6a1f1dSLionel Sambuc.Fn posix_fallocate "int fd" "off_t pos" "off_t length" 43*0a6a1f1dSLionel Sambuc.Ft int 44*0a6a1f1dSLionel Sambuc.Fn fdiscard "int fd" "off_t pos" "off_t length" 45*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 46*0a6a1f1dSLionel SambucThe 47*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 48*0a6a1f1dSLionel Sambuccall allocates backing store for the file referenced by 49*0a6a1f1dSLionel Sambuc.Fa fd 50*0a6a1f1dSLionel Sambucin the region starting at 51*0a6a1f1dSLionel Sambuc.Fa pos 52*0a6a1f1dSLionel Sambucbytes from the start of the file and continuing for 53*0a6a1f1dSLionel Sambuc.Fa length 54*0a6a1f1dSLionel Sambucbytes more. 55*0a6a1f1dSLionel SambucIf the region extends past the current end of file, the file size is 56*0a6a1f1dSLionel Sambucincreased to cover the region. 57*0a6a1f1dSLionel Sambuc.Pp 58*0a6a1f1dSLionel SambucThe 59*0a6a1f1dSLionel Sambuc.Fn fdiscard 60*0a6a1f1dSLionel Sambuccall discards backing store for the file referenced by 61*0a6a1f1dSLionel Sambuc.Fa fd 62*0a6a1f1dSLionel Sambucin the region starting at 63*0a6a1f1dSLionel Sambuc.Fa pos 64*0a6a1f1dSLionel Sambucbytes from the start of the file and continuing for 65*0a6a1f1dSLionel Sambuc.Fa length 66*0a6a1f1dSLionel Sambucbytes more. 67*0a6a1f1dSLionel SambucThe file size is not affected. 68*0a6a1f1dSLionel Sambuc.Pp 69*0a6a1f1dSLionel SambucBoth calls operate on the basis of file system blocks, so 70*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 71*0a6a1f1dSLionel Sambucmay allocate more physical space than requested and 72*0a6a1f1dSLionel Sambuc.Fn fdiscard 73*0a6a1f1dSLionel Sambucmay discard less physical space than requested. 74*0a6a1f1dSLionel Sambuc.Pp 75*0a6a1f1dSLionel SambucWhen 76*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 77*0a6a1f1dSLionel Sambucis applied to an unallocated region in a regular file (a 78*0a6a1f1dSLionel Sambuc.Dq hole ) , 79*0a6a1f1dSLionel Sambucthe hole is filled and the visible contents are unaffected; both holes 80*0a6a1f1dSLionel Sambucand newly allocated regions read as all zeros. 81*0a6a1f1dSLionel SambucIf 82*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 83*0a6a1f1dSLionel Sambucis applied to an already-allocated region in a regular file, 84*0a6a1f1dSLionel Sambucit has no effect. 85*0a6a1f1dSLionel Sambuc.Pp 86*0a6a1f1dSLionel SambucWhen 87*0a6a1f1dSLionel Sambuc.Fn fdiscard 88*0a6a1f1dSLionel Sambucis applied to a regular file, a hole is created and any data in the 89*0a6a1f1dSLionel Sambucaffected region is thrown away. 90*0a6a1f1dSLionel SambucSubsequent reads of the region return zeros. 91*0a6a1f1dSLionel Sambuc.Pp 92*0a6a1f1dSLionel SambucIf 93*0a6a1f1dSLionel Sambuc.Fn fdiscard 94*0a6a1f1dSLionel Sambucis applied to a device, and the device supports an underlying discard 95*0a6a1f1dSLionel Sambucoperation, that operation is invoked. 96*0a6a1f1dSLionel SambucFor example, ATA flash devices and solid-state disks support an 97*0a6a1f1dSLionel Sambucoperation called TRIM that discards blocks at the device level. 98*0a6a1f1dSLionel SambucThe behavior of blocks discarded at this level is 99*0a6a1f1dSLionel Sambucimplementation-defined; as devices vary, specific behavior should not 100*0a6a1f1dSLionel Sambucbe relied upon. 101*0a6a1f1dSLionel SambucSubsequent reads of the same block may return zeros; such reads may 102*0a6a1f1dSLionel Sambucalso, however, continue to return the previously written data, or 103*0a6a1f1dSLionel Sambucreturn other data, or return indeterminate garbage; or may switch 104*0a6a1f1dSLionel Sambucbetween any of these behaviors at unpredictable points later on. 105*0a6a1f1dSLionel Sambuc.Pp 106*0a6a1f1dSLionel SambucFor both calls, the file 107*0a6a1f1dSLionel Sambuc.Fa fd 108*0a6a1f1dSLionel Sambucmust be open for writing and may not be a directory or socket. 109*0a6a1f1dSLionel Sambuc.Sh RESTRICTIONS 110*0a6a1f1dSLionel SambucBecause there is no way for 111*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 112*0a6a1f1dSLionel Sambucto report a partial failure, errors may require some or all of the 113*0a6a1f1dSLionel Sambucwork it has already done to be unwound, which may be expensive. 114*0a6a1f1dSLionel SambucIt is recommended to set the file length first with 115*0a6a1f1dSLionel Sambuc.Xr ftruncate 2 116*0a6a1f1dSLionel Sambucand only then allocate space within the file using 117*0a6a1f1dSLionel Sambuc.Fn posix_fallocate . 118*0a6a1f1dSLionel Sambuc.Pp 119*0a6a1f1dSLionel SambucDepending on the implementation, even a failing call to 120*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 121*0a6a1f1dSLionel Sambucmay allocate some space to the target file. 122*0a6a1f1dSLionel SambucSuch a call will not, however, change the file size. 123*0a6a1f1dSLionel Sambuc.Pp 124*0a6a1f1dSLionel SambucFurthermore, in some implementations, the space reservations created 125*0a6a1f1dSLionel Sambucby 126*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 127*0a6a1f1dSLionel Sambucmay not be persistent after a crash or reboot if the space reserved 128*0a6a1f1dSLionel Sambuchas not yet been written to. 129*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 130*0a6a1f1dSLionel SambucIf successful, the 131*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 132*0a6a1f1dSLionel Sambucfunction will return zero. 133*0a6a1f1dSLionel SambucOtherwise an error number will be returned, without setting 134*0a6a1f1dSLionel Sambuc.Va errno . 135*0a6a1f1dSLionel Sambuc.Pp 136*0a6a1f1dSLionel SambucIf successful, the 137*0a6a1f1dSLionel Sambuc.Fn fdiscard 138*0a6a1f1dSLionel Sambucfunction will return zero. 139*0a6a1f1dSLionel SambucOtherwise the value \-1 is returned and the global variable 140*0a6a1f1dSLionel Sambuc.Va errno 141*0a6a1f1dSLionel Sambucis set to indicate the error. 142*0a6a1f1dSLionel Sambuc.Sh ERRORS 143*0a6a1f1dSLionel Sambuc.Bl -tag -width Er 144*0a6a1f1dSLionel Sambuc.It Bq Er EBADF 145*0a6a1f1dSLionel SambucThe file handle 146*0a6a1f1dSLionel Sambuc.Fa fd 147*0a6a1f1dSLionel Sambucis invalid or not open for writing. 148*0a6a1f1dSLionel Sambuc.It Bq Er EDQUOT 149*0a6a1f1dSLionel SambucAllocating the requested blocks would exceed the user's quota. 150*0a6a1f1dSLionel Sambuc.It Bq Er EINVAL 151*0a6a1f1dSLionel SambucThe position and/or length values are negative. 152*0a6a1f1dSLionel Sambuc.It Bq Er EIO 153*0a6a1f1dSLionel SambucA hardware-level I/O error occurred. 154*0a6a1f1dSLionel Sambuc.It Bq Er EISDIR 155*0a6a1f1dSLionel SambucThe selected file is a directory. 156*0a6a1f1dSLionel Sambuc.It Bq Er ENOSPC 157*0a6a1f1dSLionel SambucThere was no space in the file system to complete the operation. 158*0a6a1f1dSLionel Sambuc.El 159*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 160*0a6a1f1dSLionel Sambuc.Xr ftruncate 2 161*0a6a1f1dSLionel Sambuc.Sh HISTORY 162*0a6a1f1dSLionel SambucThe 163*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 164*0a6a1f1dSLionel Sambucand 165*0a6a1f1dSLionel Sambuc.Fn fdiscard 166*0a6a1f1dSLionel Sambucfunction calls appeared in 167*0a6a1f1dSLionel Sambuc.Nx 7.0 . 168*0a6a1f1dSLionel SambucSimilar functions appeared previously in Linux. 169*0a6a1f1dSLionel SambucThe 170*0a6a1f1dSLionel Sambuc.Fn posix_fallocate 171*0a6a1f1dSLionel Sambucfunction is expected to conform to 172*0a6a1f1dSLionel Sambuc.St -p1003.1-2004 . 173