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