summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/stdio.h6
-rw-r--r--lib/libc/sys/lseek.254
-rw-r--r--lib/libc/sys/pathconf.212
-rw-r--r--sys/kern/vfs_syscalls.c7
-rw-r--r--sys/sys/filio.h3
-rw-r--r--sys/sys/unistd.h5
6 files changed, 86 insertions, 1 deletions
diff --git a/include/stdio.h b/include/stdio.h
index 348db22..bf01429 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -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
diff --git a/lib/libc/sys/lseek.2 b/lib/libc/sys/lseek.2
index df225f3..6fdf79b 100644
--- a/lib/libc/sys/lseek.2
+++ b/lib/libc/sys/lseek.2
@@ -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
diff --git a/lib/libc/sys/pathconf.2 b/lib/libc/sys/pathconf.2
index e3c17d6..968f307 100644
--- a/lib/libc/sys/pathconf.2
+++ b/lib/libc/sys/pathconf.2
@@ -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
diff --git a/sys/kern/vfs_syscalls.c b/sys/kern/vfs_syscalls.c
index c7b278e..72a3f4e 100644
--- a/sys/kern/vfs_syscalls.c
+++ b/sys/kern/vfs_syscalls.c
@@ -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;
}
diff --git a/sys/sys/filio.h b/sys/sys/filio.h
index 8f2731a..5f13288 100644
--- a/sys/sys/filio.h
+++ b/sys/sys/filio.h
@@ -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_ */
diff --git a/sys/sys/unistd.h b/sys/sys/unistd.h
index 1fcd384..9f3eac9 100644
--- a/sys/sys/unistd.h
+++ b/sys/sys/unistd.h
@@ -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.
OpenPOWER on IntegriCloud