f3b5df
diff --git a/libscrub/Makefile.am b/libscrub/Makefile.am
f3b5df
index 477c866..d88cd48 100644
f3b5df
--- a/libscrub/Makefile.am
f3b5df
+++ b/libscrub/Makefile.am
f3b5df
@@ -13,6 +13,7 @@ libscrub_la_SOURCES = \
f3b5df
 	libscrub.c \
f3b5df
 	scrub.h \
f3b5df
 	../src/aes.c \
f3b5df
+	../src/fextent_apply.c \
f3b5df
 	../src/filldentry.c \
f3b5df
 	../src/fillfile.c \
f3b5df
 	../src/genrand.c \
f3b5df
diff --git a/man/scrub.1.in b/man/scrub.1.in
f3b5df
index a1c260a..72b114f 100644
f3b5df
--- a/man/scrub.1.in
f3b5df
+++ b/man/scrub.1.in
f3b5df
@@ -106,6 +106,13 @@ Don't generate random data in parallel with I/O.
f3b5df
 .TP
f3b5df
 \fI-h\fR, \fI--help\fR
f3b5df
 Print a summary of command line options on stderr.
f3b5df
+.TP
f3b5df
+\fI-E\fR, \fI--extent-only\fR
f3b5df
+When scrubbing regular files, scrub only the file extents. This option is
f3b5df
+useful in combination with large sparse files. If used, scrub will skip
f3b5df
+the holes in the sparse file. Use this option with caution, the result may not
f3b5df
+be compliant with cited standards and information about the actual on-disk
f3b5df
+data allocation may leak since only the allocated parts will be scrubbed.
f3b5df
 .SH SCRUB METHODS
f3b5df
 .TP
f3b5df
 .I "nnsa"
f3b5df
diff --git a/src/Makefile.am b/src/Makefile.am
f3b5df
index 0cbd8f7..5de0b68 100644
f3b5df
--- a/src/Makefile.am
f3b5df
+++ b/src/Makefile.am
f3b5df
@@ -3,6 +3,8 @@ bin_PROGRAMS = scrub
f3b5df
 scrub_SOURCES = \
f3b5df
 	aes.c \
f3b5df
 	aes.h \
f3b5df
+	fextent_apply.c \
f3b5df
+	fextent_apply.h \
f3b5df
 	filldentry.c \
f3b5df
 	filldentry.h \
f3b5df
 	fillfile.c \
f3b5df
diff --git a/src/fextent_apply.c b/src/fextent_apply.c
f3b5df
new file mode 100644
f3b5df
index 0000000..31d3210
f3b5df
--- /dev/null
f3b5df
+++ b/src/fextent_apply.c
f3b5df
@@ -0,0 +1,142 @@
f3b5df
+/*
f3b5df
+ * Copyright 2012 Red Hat Inc., Durham, North Carolina.
f3b5df
+ * All Rights Reserved.
f3b5df
+ *
f3b5df
+ * This library is free software; you can redistribute it and/or
f3b5df
+ * modify it under the terms of the GNU Lesser General Public
f3b5df
+ * License as published by the Free Software Foundation; either
f3b5df
+ * version 2.1 of the License, or (at your option) any later version.
f3b5df
+ *
f3b5df
+ * This library is distributed in the hope that it will be useful, 
f3b5df
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
f3b5df
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f3b5df
+ * Lesser General Public License for more details.
f3b5df
+ *
f3b5df
+ * You should have received a copy of the GNU Lesser General Public
f3b5df
+ * License along with this library; if not, write to the Free Software 
f3b5df
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f3b5df
+ *
f3b5df
+ * Authors:
f3b5df
+ *      Daniel Kopecek <dkopecek@redhat.com>
f3b5df
+ */
f3b5df
+#include <stdio.h>
f3b5df
+#include <stdint.h>
f3b5df
+#include <stdlib.h>
f3b5df
+#include <string.h>
f3b5df
+#include <unistd.h>
f3b5df
+#include <errno.h>
f3b5df
+
f3b5df
+#include <sys/types.h>
f3b5df
+#include <sys/stat.h>
f3b5df
+#include <sys/ioctl.h>
f3b5df
+#include <sys/file.h>
f3b5df
+
f3b5df
+#include <linux/fs.h>
f3b5df
+#include <linux/fiemap.h>
f3b5df
+
f3b5df
+#ifndef NDEBUG
f3b5df
+# define dP(...)				\
f3b5df
+    do { int  __tmp_errno = errno;		\
f3b5df
+	fprintf(stderr, "DEBUG: "__VA_ARGS__);	\
f3b5df
+	errno = __tmp_errno;			\
f3b5df
+    } while(0)
f3b5df
+#else
f3b5df
+# define dP(...) while(0)
f3b5df
+#endif
f3b5df
+
f3b5df
+int fextent_apply(int fd, int (*function)(int, struct fiemap_extent *, void *), void *arg)
f3b5df
+{
f3b5df
+    int ret = -1;
f3b5df
+    struct stat st;
f3b5df
+    struct fiemap *em;
f3b5df
+    uint32_t extent_count, i;
f3b5df
+
f3b5df
+    // lock, sync, stat
f3b5df
+    if (flock(fd, LOCK_EX) != 0) {
f3b5df
+	dP("flock(%d, LOCK_EX) failed: %s, %d.\n", fd, strerror(errno), errno);
f3b5df
+	return -1;
f3b5df
+    }
f3b5df
+    if (fsync(fd) != 0) {
f3b5df
+	dP("fsync(%d) failed: %s, %d.\n", fd, strerror(errno), errno);
f3b5df
+	goto exit_1;
f3b5df
+    }
f3b5df
+    if (fstat(fd, &st) != 0) {
f3b5df
+	dP("fstat(%d) failed: %s, %d.\n", fd, strerror(errno), errno);
f3b5df
+	goto exit_1;
f3b5df
+    }
f3b5df
+    
f3b5df
+    /*
f3b5df
+     * fiemap => get extent count
f3b5df
+     */
f3b5df
+    em = malloc(sizeof(struct fiemap));
f3b5df
+
f3b5df
+    if (em == NULL) {
f3b5df
+	dP("malloc(%zu) returned NULL!\n", sizeof(struct fiemap));
f3b5df
+	goto exit_1;
f3b5df
+    }
f3b5df
+
f3b5df
+    memset(em, 0, sizeof(struct fiemap));
f3b5df
+
f3b5df
+    em->fm_start = 0;
f3b5df
+    em->fm_length = st.st_size;
f3b5df
+    em->fm_extent_count = 0;
f3b5df
+    em->fm_mapped_extents = 0;
f3b5df
+    em->fm_flags = 0;
f3b5df
+
f3b5df
+    if (ioctl(fd, FS_IOC_FIEMAP, em) != 0) {
f3b5df
+	dP("FS_IOC_FIEMAP: %s, %d.\n", strerror(errno), errno);
f3b5df
+	goto exit_0;
f3b5df
+    }
f3b5df
+
f3b5df
+    extent_count = em->fm_mapped_extents;
f3b5df
+    free(em);
f3b5df
+
f3b5df
+    /*
f3b5df
+     * fiemap => get extents
f3b5df
+     */
f3b5df
+    em = malloc (sizeof(struct fiemap)
f3b5df
+		 + (sizeof(struct fiemap_extent) * extent_count));
f3b5df
+
f3b5df
+    if (em == NULL) {
f3b5df
+	dP("malloc(%zu) returned NULL!\n", sizeof(struct fiemap)
f3b5df
+	   + (sizeof (struct fiemap_extent) * extent_count));
f3b5df
+	goto exit_0;
f3b5df
+    }
f3b5df
+
f3b5df
+    memset(em, 0, sizeof(struct fiemap)
f3b5df
+	   + (sizeof(struct fiemap_extent) * extent_count));
f3b5df
+
f3b5df
+    em[0].fm_start = 0;
f3b5df
+    em[0].fm_length = st.st_size;
f3b5df
+    em[0].fm_extent_count = extent_count;
f3b5df
+    em[0].fm_flags = 0;
f3b5df
+
f3b5df
+    if (ioctl(fd, FS_IOC_FIEMAP, em) != 0) {
f3b5df
+	dP("FS_IOC_FIEMAP: %s, %d.\n", strerror(errno), errno);
f3b5df
+	goto exit_0;
f3b5df
+    }
f3b5df
+   
f3b5df
+    for (i = 0; i < extent_count; ++i) {
f3b5df
+	// seek to extent start
f3b5df
+	if (lseek(fd, em->fm_extents[i].fe_logical, SEEK_SET) == (off_t)-1) {
f3b5df
+	    dP("lseek(%d, %llu, SET) failed: %s, %d.\n",
f3b5df
+	       fd, em->fm_extents[i].fe_logical, strerror(errno), errno);
f3b5df
+	    goto exit_0;
f3b5df
+	}
f3b5df
+
f3b5df
+	ret = function(fd, em->fm_extents + i, arg);
f3b5df
+	if (ret != 0)
f3b5df
+	    goto exit_0;
f3b5df
+    }
f3b5df
+
f3b5df
+    ret = 0;
f3b5df
+  exit_0:
f3b5df
+    // release resources
f3b5df
+    free (em);
f3b5df
+  exit_1:
f3b5df
+    // unlock
f3b5df
+    if (flock(fd, LOCK_UN) != 0)
f3b5df
+	ret = -1;
f3b5df
+
f3b5df
+    return ret;
f3b5df
+}
f3b5df
diff --git a/src/fextent_apply.h b/src/fextent_apply.h
f3b5df
new file mode 100644
f3b5df
index 0000000..40a54ec
f3b5df
--- /dev/null
f3b5df
+++ b/src/fextent_apply.h
f3b5df
@@ -0,0 +1,30 @@
f3b5df
+/*
f3b5df
+ * Copyright 2012 Red Hat Inc., Durham, North Carolina.
f3b5df
+ * All Rights Reserved.
f3b5df
+ *
f3b5df
+ * This library is free software; you can redistribute it and/or
f3b5df
+ * modify it under the terms of the GNU Lesser General Public
f3b5df
+ * License as published by the Free Software Foundation; either
f3b5df
+ * version 2.1 of the License, or (at your option) any later version.
f3b5df
+ *
f3b5df
+ * This library is distributed in the hope that it will be useful, 
f3b5df
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
f3b5df
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f3b5df
+ * Lesser General Public License for more details.
f3b5df
+ *
f3b5df
+ * You should have received a copy of the GNU Lesser General Public
f3b5df
+ * License along with this library; if not, write to the Free Software 
f3b5df
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f3b5df
+ *
f3b5df
+ * Authors:
f3b5df
+ *      Daniel Kopecek <dkopecek@redhat.com>
f3b5df
+ */
f3b5df
+#ifndef FEXTENT_APPLY_H
f3b5df
+#define FEXTENT_APPLY_H
f3b5df
+
f3b5df
+#include <linux/fs.h>
f3b5df
+#include <linux/fiemap.h>
f3b5df
+
f3b5df
+int fextent_apply(int fd, int (*function)(int, struct fiemap_extent *, void *), void *arg);
f3b5df
+
f3b5df
+#endif /* FEXTENT_APPLY_H */
f3b5df
diff --git a/src/fillfile.c b/src/fillfile.c
f3b5df
index e0f67b6..a77367f 100644
f3b5df
--- a/src/fillfile.c
f3b5df
+++ b/src/fillfile.c
f3b5df
@@ -42,6 +42,7 @@
f3b5df
 
f3b5df
 #include "util.h"
f3b5df
 #include "fillfile.h"
f3b5df
+#include "fextent_apply.h"
f3b5df
 
f3b5df
 static int no_threads = 0;
f3b5df
 
f3b5df
@@ -57,6 +58,20 @@ struct memstruct {
f3b5df
 
f3b5df
 extern char *prog;
f3b5df
 
f3b5df
+struct fillfile_args {
f3b5df
+    char *path;
f3b5df
+    off_t filesize;
f3b5df
+    unsigned char *mem;
f3b5df
+    int memsize;
f3b5df
+    progress_t progress;
f3b5df
+    void *arg;
f3b5df
+    refill_t refill;
f3b5df
+    unsigned char *buf;
f3b5df
+};
f3b5df
+
f3b5df
+int fillextent(int fd, struct fiemap_extent *extent, void *pa);
f3b5df
+int checkextent(int fd, struct fiemap_extent *extent, void *pa);
f3b5df
+
f3b5df
 #if defined(O_DIRECT) && (defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN))
f3b5df
 # define MY_O_DIRECT O_DIRECT
f3b5df
 #else
f3b5df
@@ -155,11 +170,12 @@ refill_fini(struct memstruct *mp)
f3b5df
  * If 'sparse' is true, only scrub first and last blocks (for testing).
f3b5df
  * The number of bytes written is returned.
f3b5df
  * If 'creat' is true, open with O_CREAT and allow ENOSPC to be non-fatal.
f3b5df
+ * IF 'extentonly' is true, fill only file extents with the given pattern
f3b5df
  */
f3b5df
 off_t
f3b5df
 fillfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
          progress_t progress, void *arg, refill_t refill, 
f3b5df
-         bool sparse, bool creat)
f3b5df
+         bool sparse, bool creat, bool extentonly)
f3b5df
 {
f3b5df
     int fd = -1;
f3b5df
     off_t n;
f3b5df
@@ -179,34 +195,58 @@ fillfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
     }
f3b5df
     if (fd < 0)
f3b5df
         goto error;
f3b5df
-    do {
f3b5df
-        if (written + memsize > filesize)
f3b5df
-            memsize = filesize - written;
f3b5df
-        if (refill && !sparse) {
f3b5df
-            if (!mp)
f3b5df
-                if (refill_init(&mp, refill, memsize) < 0)
f3b5df
-                    goto error;
f3b5df
-            if (refill_memcpy(mp, mem, memsize, filesize, written) < 0)
f3b5df
-                goto error;
f3b5df
-        }
f3b5df
-        if (sparse && !(written == 0) && !(written + memsize == filesize)) {
f3b5df
-            if (lseek(fd, memsize, SEEK_CUR) < 0)
f3b5df
-                goto error;
f3b5df
-            written += memsize;
f3b5df
-        } else {
f3b5df
-            n = write_all(fd, mem, memsize);
f3b5df
-            if (creat && n < 0 && errno == ENOSPC)
f3b5df
-                break;
f3b5df
-            if (n == 0) {
f3b5df
-                errno = EINVAL; /* write past end of device? */
f3b5df
-                goto error;
f3b5df
-            } else if (n < 0)
f3b5df
-                goto error;
f3b5df
-            written += n;
f3b5df
+
f3b5df
+    if (extentonly) {
f3b5df
+        struct fillfile_args fa;
f3b5df
+
f3b5df
+        fa.path = path;
f3b5df
+        fa.filesize = filesize;
f3b5df
+        fa.mem = mem;
f3b5df
+        fa.memsize = memsize;
f3b5df
+        fa.progress = progress;
f3b5df
+        fa.refill = refill;
f3b5df
+        fa.arg = arg;
f3b5df
+
f3b5df
+        if (fextent_apply(fd, fillextent, &fa) == 0) {
f3b5df
+            written = filesize;
f3b5df
         }
f3b5df
-        if (progress)
f3b5df
-            progress(arg, (double)written/filesize);
f3b5df
-    } while (written < filesize);
f3b5df
+    } else {
f3b5df
+        do {
f3b5df
+            if (written + memsize > filesize)
f3b5df
+                memsize = filesize - written;
f3b5df
+            if (refill && !sparse) {
f3b5df
+                if (!mp) {
f3b5df
+                    if (refill_init(&mp, refill, memsize) < 0) {
f3b5df
+                        goto error;
f3b5df
+                    }
f3b5df
+                }
f3b5df
+                if (refill_memcpy(mp, mem, memsize, filesize, written) < 0) {
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+            }
f3b5df
+            if (sparse && !(written == 0) && !(written + memsize == filesize)) {
f3b5df
+                if (lseek(fd, memsize, SEEK_CUR) < 0) {
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                written += memsize;
f3b5df
+            } else {
f3b5df
+                n = write_all(fd, mem, memsize);
f3b5df
+                if (creat && n < 0 && errno == ENOSPC)
f3b5df
+                    break;
f3b5df
+                if (n == 0) {
f3b5df
+                    errno = EINVAL;
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                else if (n < 0) {
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                written += n;
f3b5df
+            }
f3b5df
+            if (progress)
f3b5df
+                progress(arg, (double)written/filesize);
f3b5df
+        } while (written < filesize);
f3b5df
+    }
f3b5df
+
f3b5df
     if (fsync(fd) < 0)
f3b5df
         goto error;
f3b5df
 #if defined(HAVE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
f3b5df
@@ -230,7 +270,7 @@ error:
f3b5df
  */
f3b5df
 off_t
f3b5df
 checkfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
-          progress_t progress, void *arg, bool sparse)
f3b5df
+          progress_t progress, void *arg, bool sparse, bool extentonly)
f3b5df
 {
f3b5df
     int fd = -1;
f3b5df
     off_t n;
f3b5df
@@ -238,8 +278,6 @@ checkfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
     unsigned char *buf = NULL;
f3b5df
     int openflags = O_RDONLY;
f3b5df
 
f3b5df
-    if (!(buf = alloc_buffer(memsize)))
f3b5df
-        goto nomem;
f3b5df
     if (filetype(path) != FILE_CHAR)
f3b5df
         openflags |= MY_O_DIRECT;
f3b5df
     fd = open(path, openflags);
f3b5df
@@ -250,32 +288,60 @@ checkfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
     }
f3b5df
     if (fd < 0)
f3b5df
         goto error;
f3b5df
-    do {
f3b5df
-        if (verified + memsize > filesize)
f3b5df
-            memsize = filesize - verified;
f3b5df
-        if (sparse && !(verified == 0) && !(verified + memsize == filesize)) {
f3b5df
-            if (lseek(fd, memsize, SEEK_CUR) < 0)
f3b5df
-                goto error;
f3b5df
-            verified += memsize;
f3b5df
-        } else {
f3b5df
-            n = read_all(fd, buf, memsize);
f3b5df
-            if (n < 0)
f3b5df
-                goto error;
f3b5df
-            if (n == 0) {
f3b5df
-                errno = EINVAL; /* early EOF */
f3b5df
-                goto error;
f3b5df
-            }
f3b5df
-            if (memcmp(mem, buf, memsize) != 0) {
f3b5df
-                break; /* return < filesize means verification failure */
f3b5df
-            }
f3b5df
-            verified += n;
f3b5df
+    if (extentonly) {
f3b5df
+        struct fillfile_args fa;
f3b5df
+
f3b5df
+        fa.path = path;
f3b5df
+        fa.filesize = filesize;
f3b5df
+        fa.mem = mem;
f3b5df
+        fa.memsize = memsize;
f3b5df
+        fa.progress = progress;
f3b5df
+        fa.arg = arg;
f3b5df
+        fa.buf = alloc_buffer(memsize);
f3b5df
+
f3b5df
+        if (fa.buf == NULL) {
f3b5df
+            goto nomem;
f3b5df
         }
f3b5df
-        if (progress)
f3b5df
-            progress(arg, (double)verified/filesize);
f3b5df
-    } while (verified < filesize);
f3b5df
+
f3b5df
+        if (fextent_apply(fd, checkextent, &fa) == 0)
f3b5df
+            verified = filesize;
f3b5df
+
f3b5df
+        free(fa.buf);
f3b5df
+    } else {
f3b5df
+        if (!(buf = alloc_buffer(memsize)))
f3b5df
+            goto nomem;
f3b5df
+        do {
f3b5df
+            if (verified + memsize > filesize)
f3b5df
+                memsize = filesize - verified;
f3b5df
+            if (sparse && !(verified == 0) && !(verified + memsize == filesize)) {
f3b5df
+                if (lseek(fd, memsize, SEEK_CUR) < 0) {
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                verified += memsize;
f3b5df
+            } else {
f3b5df
+                n = read_all(fd, buf, memsize);
f3b5df
+                if (n < 0) {
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                if (n == 0) {
f3b5df
+                    errno = EINVAL; /* early EOF */
f3b5df
+                    goto error;
f3b5df
+                }
f3b5df
+                if (memcmp(mem, buf, memsize) != 0) {
f3b5df
+                    break;
f3b5df
+                }
f3b5df
+                verified += n;
f3b5df
+            }
f3b5df
+            if (progress)
f3b5df
+                progress(arg, (double)verified/filesize);
f3b5df
+        } while (verified < filesize);
f3b5df
+    }
f3b5df
+ 
f3b5df
     if (close(fd) < 0)
f3b5df
         goto error;
f3b5df
-    free(buf);
f3b5df
+    if (buf != NULL) {
f3b5df
+        free(buf);
f3b5df
+    }
f3b5df
     return verified;
f3b5df
 nomem:
f3b5df
     errno = ENOMEM;
f3b5df
@@ -293,6 +359,63 @@ disable_threads(void)
f3b5df
     no_threads = 1;
f3b5df
 }
f3b5df
 
f3b5df
+int fillextent(int fd, struct fiemap_extent *extent, void *pa)
f3b5df
+{
f3b5df
+    off_t n;
f3b5df
+    off_t written = 0LL;
f3b5df
+    struct fillfile_args args = *(struct fillfile_args *)(pa);
f3b5df
+    
f3b5df
+    do {
f3b5df
+        if (args.refill)
f3b5df
+            args.refill(args.mem, args.memsize);
f3b5df
+
f3b5df
+        if (written + args.memsize > extent->fe_length)
f3b5df
+            args.memsize = extent->fe_length - written;
f3b5df
+
f3b5df
+        n = write_all(fd, args.mem, args.memsize);
f3b5df
+
f3b5df
+        if (n < 0) {
f3b5df
+            fprintf(stderr, "%s: write %s: %s\n", prog, args.path, strerror(errno));
f3b5df
+            exit(1);
f3b5df
+        }
f3b5df
+        written += n;
f3b5df
+
f3b5df
+        if (args.progress)
f3b5df
+            args.progress(args.arg, (double)(extent->fe_logical + written)/args.filesize);
f3b5df
+    } while (written < extent->fe_length);
f3b5df
+
f3b5df
+    return 0;
f3b5df
+}
f3b5df
+
f3b5df
+int checkextent(int fd, struct fiemap_extent *extent, void *pa)
f3b5df
+{
f3b5df
+    off_t n;
f3b5df
+    off_t verified = 0LL;
f3b5df
+    struct fillfile_args args = *(struct fillfile_args *)(pa);
f3b5df
+
f3b5df
+    do {
f3b5df
+        if (verified + args.memsize > extent->fe_length)
f3b5df
+            args.memsize = extent->fe_length - verified;
f3b5df
+
f3b5df
+        n = read_all(fd, args.buf, args.memsize);
f3b5df
+        if (n < 0) {
f3b5df
+            return -1;
f3b5df
+        }
f3b5df
+        if (n == 0) {
f3b5df
+            errno = EINVAL;
f3b5df
+            return -1;
f3b5df
+        }
f3b5df
+        if (memcmp(args.mem, args.buf, args.memsize) != 0) {
f3b5df
+            break;
f3b5df
+        }
f3b5df
+        verified += n;
f3b5df
+        if (args.progress)
f3b5df
+            args.progress(args.arg, (double)(extent->fe_logical+verified)/args.filesize);
f3b5df
+    } while (verified < extent->fe_length);
f3b5df
+
f3b5df
+    return 0;
f3b5df
+}
f3b5df
+
f3b5df
 /*
f3b5df
  * vi:tabstop=4 shiftwidth=4 expandtab
f3b5df
  */
f3b5df
diff --git a/src/fillfile.h b/src/fillfile.h
f3b5df
index b9ef951..2fc917d 100644
f3b5df
--- a/src/fillfile.h
f3b5df
+++ b/src/fillfile.h
f3b5df
@@ -29,7 +29,7 @@ typedef void (*refill_t) (unsigned char *mem, int memsize);
f3b5df
 
f3b5df
 off_t fillfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
         progress_t progress, void *arg, refill_t refill, 
f3b5df
-        bool sparse, bool creat);
f3b5df
+        bool sparse, bool creat, bool extentonly);
f3b5df
 off_t checkfile(char *path, off_t filesize, unsigned char *mem, int memsize,
f3b5df
-        progress_t progress, void *arg, bool sparse);
f3b5df
+        progress_t progress, void *arg, bool sparse, bool extentonly);
f3b5df
 void  disable_threads(void);
f3b5df
diff --git a/src/genrand.c b/src/genrand.c
f3b5df
index 820c898..ecfd382 100644
f3b5df
--- a/src/genrand.c
f3b5df
+++ b/src/genrand.c
f3b5df
@@ -106,7 +106,7 @@ genrandraw(unsigned char *buf, int buflen)
f3b5df
                 buf[n] = result;
f3b5df
             }
f3b5df
 #endif
f3b5df
-            return;
f3b5df
+            return 0;
f3b5df
         }
f3b5df
     }
f3b5df
 
f3b5df
diff --git a/src/scrub.c b/src/scrub.c
f3b5df
index dec71f3..b0eb1f7 100644
f3b5df
--- a/src/scrub.c
f3b5df
+++ b/src/scrub.c
f3b5df
@@ -58,12 +58,12 @@
f3b5df
 #define BUFSIZE (4*1024*1024) /* default blocksize */
f3b5df
 
f3b5df
 static bool       scrub(char *path, off_t size, const sequence_t *seq,
f3b5df
-                      int bufsize, bool Sopt, bool sparse, bool enospc);
f3b5df
+                      int bufsize, bool Sopt, bool sparse, bool enospc, bool extentonly);
f3b5df
 static void       scrub_free(char *path, off_t size, const sequence_t *seq,
f3b5df
                       int bufsize, bool Sopt);
f3b5df
 static void       scrub_dirent(char *path, char *newpath);
f3b5df
 static void       scrub_file(char *path, off_t size, const sequence_t *seq,
f3b5df
-                      int bufsize, bool Sopt, bool sparse);
f3b5df
+                      int bufsize, bool Sopt, bool sparse, bool extentonly);
f3b5df
 #if __APPLE__
f3b5df
 static void       scrub_resfork(char *path, const sequence_t *seq,
f3b5df
                       int bufsize);
f3b5df
@@ -71,7 +71,7 @@ static void       scrub_resfork(char *path, const sequence_t *seq,
f3b5df
 static void       scrub_disk(char *path, off_t size, const sequence_t *seq,
f3b5df
                       int bufsize, bool Sopt, bool sparse);
f3b5df
 
f3b5df
-#define OPTIONS "p:D:Xb:s:fSrvTLRth"
f3b5df
+#define OPTIONS "p:D:Xb:s:fSrvTELRth"
f3b5df
 #if HAVE_GETOPT_LONG
f3b5df
 #define GETOPT(ac,av,opt,lopt) getopt_long(ac,av,opt,lopt,NULL)
f3b5df
 static struct option longopts[] = {
f3b5df
@@ -85,6 +85,7 @@ static struct option longopts[] = {
f3b5df
     {"remove",           no_argument,        0, 'r'},
f3b5df
     {"version",          no_argument,        0, 'v'},
f3b5df
     {"test-sparse",      no_argument,        0, 'T'},
f3b5df
+    {"extent-only",      no_argument,        0, 'E'},
f3b5df
     {"no-link",          no_argument,        0, 'L'},
f3b5df
     {"no-hwrand",        no_argument,        0, 'R'},
f3b5df
     {"no-threads",       no_argument,        0, 't'},
f3b5df
@@ -111,6 +112,7 @@ usage(void)
f3b5df
 "  -f, --force             scrub despite signature from previous scrub\n"
f3b5df
 "  -S, --no-signature      do not write scrub signature after scrub\n"
f3b5df
 "  -r, --remove            remove file after scrub\n"
f3b5df
+"  -E, --extent-only       scrub only file extents\n"
f3b5df
 "  -L, --no-link           do not scrub link target\n"
f3b5df
 "  -R, --no-hwrand         do not use a hardware random number generator\n"
f3b5df
 "  -t, --no-threads        do not compute random data in a parallel thread\n"
f3b5df
@@ -139,6 +141,7 @@ main(int argc, char *argv[])
f3b5df
     bool Lopt = false;
f3b5df
     bool Ropt = false;
f3b5df
     bool topt = false;
f3b5df
+    bool Eopt = false;
f3b5df
     extern int optind;
f3b5df
     extern char *optarg;
f3b5df
     int c;
f3b5df
@@ -207,6 +210,9 @@ main(int argc, char *argv[])
f3b5df
         case 'T':   /* --test-sparse */
f3b5df
             Topt = true;
f3b5df
             break;
f3b5df
+        case 'E':   /* --extent-only */
f3b5df
+            Eopt = true;
f3b5df
+            break;
f3b5df
         case 'L':   /* --no-link */
f3b5df
             Lopt = true;
f3b5df
             break;
f3b5df
@@ -315,7 +321,7 @@ main(int argc, char *argv[])
f3b5df
                         prog, Dopt, filename);
f3b5df
                 exit(1);
f3b5df
             }
f3b5df
-            scrub_file(filename, sopt, seq, bopt, Sopt, Topt);
f3b5df
+            scrub_file(filename, sopt, seq, bopt, Sopt, Topt, Eopt);
f3b5df
 #if __APPLE__
f3b5df
             scrub_resfork(filename, seq, bopt);
f3b5df
 #endif
f3b5df
@@ -346,14 +352,14 @@ done:
f3b5df
  */
f3b5df
 static bool
f3b5df
 scrub(char *path, off_t size, const sequence_t *seq, int bufsize, 
f3b5df
-      bool Sopt, bool sparse, bool enospc)
f3b5df
+      bool Sopt, bool sparse, bool enospc, bool extentonly)
f3b5df
 {
f3b5df
     unsigned char *buf;
f3b5df
     int i;
f3b5df
     prog_t p;
f3b5df
     char sizestr[80];
f3b5df
     bool isfull = false;
f3b5df
-    off_t written, checked;
f3b5df
+    off_t written = (off_t)-1, checked = (off_t)-1;
f3b5df
 
f3b5df
     if (!(buf = alloc_buffer(bufsize))) {
f3b5df
         fprintf(stderr, "%s: out of memory\n", prog);
f3b5df
@@ -381,7 +387,7 @@ scrub(char *path, off_t size, const sequence_t *seq, int bufsize,
f3b5df
                 }
f3b5df
                 written = fillfile(path, size, buf, bufsize, 
f3b5df
                                    (progress_t)progress_update, p, 
f3b5df
-                                   (refill_t)genrand, sparse, enospc);
f3b5df
+                                   (refill_t)genrand, sparse, enospc, extentonly);
f3b5df
                 if (written == (off_t)-1) {
f3b5df
                     fprintf(stderr, "%s: %s: %s\n", prog, path,
f3b5df
                              strerror(errno));
f3b5df
@@ -395,7 +401,7 @@ scrub(char *path, off_t size, const sequence_t *seq, int bufsize,
f3b5df
                 memset_pat(buf, seq->pat[i], bufsize);
f3b5df
                 written = fillfile(path, size, buf, bufsize, 
f3b5df
                                    (progress_t)progress_update, p, 
f3b5df
-                                   NULL, sparse, enospc);
f3b5df
+                                   NULL, sparse, enospc, extentonly);
f3b5df
                 if (written == (off_t)-1) {
f3b5df
                     fprintf(stderr, "%s: %s: %s\n", prog, path,
f3b5df
                              strerror(errno));
f3b5df
@@ -409,7 +415,7 @@ scrub(char *path, off_t size, const sequence_t *seq, int bufsize,
f3b5df
                 memset_pat(buf, seq->pat[i], bufsize);
f3b5df
                 written = fillfile(path, size, buf, bufsize, 
f3b5df
                                    (progress_t)progress_update, p, 
f3b5df
-                                   NULL, sparse, enospc);
f3b5df
+                                   NULL, sparse, enospc, extentonly);
f3b5df
                 if (written == (off_t)-1) {
f3b5df
                     fprintf(stderr, "%s: %s: %s\n", prog, path,
f3b5df
                              strerror(errno));
f3b5df
@@ -419,7 +425,7 @@ scrub(char *path, off_t size, const sequence_t *seq, int bufsize,
f3b5df
                 printf("%s: %-8s", prog, "verify");
f3b5df
                 progress_create(&p, 50);
f3b5df
                 checked = checkfile(path, written, buf, bufsize, 
f3b5df
-                                    (progress_t)progress_update, p, sparse);
f3b5df
+                                    (progress_t)progress_update, p, sparse, extentonly);
f3b5df
                 if (checked == (off_t)-1) {
f3b5df
                     fprintf(stderr, "%s: %s: %s\n", prog, path,
f3b5df
                              strerror(errno));
f3b5df
@@ -513,7 +519,7 @@ scrub_free(char *dirpath, off_t size, const sequence_t *seq,
f3b5df
     size = blkalign(size, sb.st_blksize, DOWN);
f3b5df
     do {
f3b5df
         snprintf(path, sizeof(path), "%s/scrub.%.3d", dirpath, fileno++);
f3b5df
-        isfull = scrub(path, size, seq, bufsize, Sopt, false, true);
f3b5df
+        isfull = scrub(path, size, seq, bufsize, Sopt, false, true, false);
f3b5df
     } while (!isfull);
f3b5df
     while (--fileno >= 0) {
f3b5df
         snprintf(path, sizeof(path), "%s/scrub.%.3d", dirpath, fileno);
f3b5df
@@ -565,7 +571,7 @@ scrub_dirent(char *path, char *newpath)
f3b5df
  */
f3b5df
 static void 
f3b5df
 scrub_file(char *path, off_t size, const sequence_t *seq, 
f3b5df
-           int bufsize, bool Sopt, bool sparse)
f3b5df
+           int bufsize, bool Sopt, bool sparse, bool extentonly)
f3b5df
 {
f3b5df
     struct stat sb;
f3b5df
     filetype_t ftype = filetype(path);
f3b5df
@@ -590,7 +596,7 @@ scrub_file(char *path, off_t size, const sequence_t *seq,
f3b5df
                     prog, path, (int)(size - sb.st_size)); 
f3b5df
         }
f3b5df
     }
f3b5df
-    scrub(path, size, seq, bufsize, Sopt, sparse, false);
f3b5df
+    scrub(path, size, seq, bufsize, Sopt, sparse, false, extentonly);
f3b5df
 }
f3b5df
 
f3b5df
 /* Scrub apple resource fork component of file.
f3b5df
@@ -618,7 +624,7 @@ scrub_resfork(char *path, const sequence_t *seq, int bufsize)
f3b5df
         printf("%s: padding %s with %d bytes to fill last fs block\n", 
f3b5df
                         prog, rpath, (int)(rsize - rsb.st_size)); 
f3b5df
     }
f3b5df
-    scrub(rpath, rsize, seq, bufsize, false, false, false);
f3b5df
+    scrub(rpath, rsize, seq, bufsize, false, false, false, false);
f3b5df
 }
f3b5df
 #endif
f3b5df
 
f3b5df
@@ -639,7 +645,7 @@ scrub_disk(char *path, off_t size, const sequence_t *seq, int bufsize,
f3b5df
         }
f3b5df
         printf("%s: please verify that device size below is correct!\n", prog);
f3b5df
     }
f3b5df
-    scrub(path, size, seq, bufsize, Sopt, sparse, false);
f3b5df
+    scrub(path, size, seq, bufsize, Sopt, sparse, false, false);
f3b5df
 }
f3b5df
 
f3b5df
 /*