Implement SEEK_DATA and SEEK_HOLE extensions to lseek(2) as found in

OpenSolaris. For more information please refer to:

	http://blogs.sun.com/bonwick/entry/seek_hole_and_seek_data
This commit is contained in:
Pawel Jakub Dawidek 2007-04-05 21:10:53 +00:00
parent f3a8d2f93c
commit f6521d1c31
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=168397
6 changed files with 86 additions and 1 deletions

View File

@ -200,6 +200,12 @@ __END_DECLS
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
#ifndef SEEK_DATA
#define SEEK_DATA 3 /* set file offset to next data past offset */
#endif
#ifndef SEEK_HOLE
#define SEEK_HOLE 4 /* set file offset to next hole past offset */
#endif
#define stdin __stdinp
#define stdout __stdoutp

View File

@ -87,6 +87,23 @@ the offset is set to the size of the
file plus
.Fa offset
bytes.
.It
If
.Fa whence
is
.Dv SEEK_HOLE ,
the offset of the start of the next hole greater than or equal to the supplied
.Fa offset
is returned.
The definition of a hole is provided below.
.It
If
.Fa whence
is
.Dv SEEK_DATA ,
the offset is set to the start of the next non-hole file region greater
than or equal to the supplied
.Fa offset .
.El
.Pp
The
@ -100,6 +117,33 @@ bytes of zeros (until data is actually written into the gap).
Some devices are incapable of seeking.
The value of the pointer
associated with such a device is undefined.
.Pp
A
.Qq hole
is defined as a contiguous range of bytes in a file, all having the value of
zero, but not all zeros in a file are guaranteed to be represented as holes
returned with
.Dv SEEK_HOLE .
File systems are allowed to expose ranges of zeros with
.Dv SEEK_HOLE ,
but not required to.
Applications can use
.Dv SEEK_HOLE
to optimise their behavior for ranges of zeros, but must not depend on it to
find all such ranges in a file.
The existence of a hole at the end of every data region allows for easy
programming and implies that a virtual hole exists at the end of the file.
Applications should use
.Fn fpathconf _PC_MIN_HOLE_SIZE
or
.Fn pathconf _PC_MIN_HOLE_SIZE
to determine if a file system supports
.Dv SEEK_HOLE .
See
.Xr pathconf 2 .
.Pp
For file systems that do not supply information about holes, the file will be
represented as one entire data region.
.Sh RETURN VALUES
Upon successful completion,
.Fn lseek
@ -128,6 +172,13 @@ argument
is not a proper value
or the resulting file offset would
be negative for a non-character special file.
.It Bq Er ENXIO
For
.Dv SEEK_DATA ,
there are no more data regions past the supplied offset.
For
.Dv SEEK_HOLE ,
there are no more holes past the supplied offset.
.It Bq Er EOVERFLOW
The resulting file offset would be a value which cannot be represented
correctly in an object of type
@ -140,7 +191,8 @@ is associated with a pipe, socket, or FIFO.
.El
.Sh SEE ALSO
.Xr dup 2 ,
.Xr open 2
.Xr open 2 ,
.Xr pathconf 2
.Sh STANDARDS
The
.Fn lseek

View File

@ -144,6 +144,17 @@ otherwise 0.
.It Li _PC_MAC_PRESENT
Returns 1 if a Mandatory Access Control (MAC) label can be set on the
specified file, otherwise 0.
.It Li _PC_MIN_HOLE_SIZE
If a file system supports the reporting of holes (see
.Xr lseek 2 ,
.Fn pathconf
and
.Fn fpathconf
return a positive number that represents the minimum hole size returned in
bytes.
The offsets of holes returned will be aligned to this same value.
A special value of 1 is returned if the file system does not specify the minimum
hole size but still reports holes.
.El
.Sh RETURN VALUES
If the call to
@ -216,6 +227,7 @@ is not a valid open file descriptor.
An I/O error occurred while reading from or writing to the file system.
.El
.Sh SEE ALSO
.Xr lseek 2 ,
.Xr sysctl 3
.Sh HISTORY
The

View File

@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$");
#include <sys/kernel.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/filio.h>
#include <sys/limits.h>
#include <sys/linker.h>
#include <sys/stat.h>
@ -1740,6 +1741,12 @@ lseek(td, uap)
break;
case L_SET:
break;
case SEEK_DATA:
error = fo_ioctl(fp, FIOSEEKDATA, &offset, cred, td);
break;
case SEEK_HOLE:
error = fo_ioctl(fp, FIOSEEKHOLE, &offset, cred, td);
break;
default:
error = EINVAL;
}

View File

@ -55,5 +55,8 @@ struct fiodgname_arg {
void *buf;
};
#define FIODGNAME _IOW('f', 120, struct fiodgname_arg) /* get dev. name */
/* Handle lseek SEEK_DATA and SEEK_HOLE for holey file knowledge. */
#define FIOSEEKDATA _IOWR('f', 97, off_t) /* SEEK_DATA */
#define FIOSEEKHOLE _IOWR('f', 98, off_t) /* SEEK_HOLE */
#endif /* !_SYS_FILIO_H_ */

View File

@ -107,6 +107,8 @@
#define SEEK_SET 0 /* set file offset to offset */
#define SEEK_CUR 1 /* set file offset to current plus offset */
#define SEEK_END 2 /* set file offset to EOF plus offset */
#define SEEK_DATA 3 /* set file offset to next data past offset */
#define SEEK_HOLE 4 /* set file offset to next hole past offset */
#endif
#ifndef _POSIX_SOURCE
@ -151,6 +153,9 @@
#define _PC_MAC_PRESENT 63
#endif
/* From OpenSolaris, used by SEEK_DATA/SEEK_HOLE. */
#define _PC_MIN_HOLE_SIZE 21
#if __BSD_VISIBLE
/*
* rfork() options.