diff options
author | Jakob Unterwurzacher | 2018-07-01 19:14:00 +0200 |
---|---|---|
committer | Jakob Unterwurzacher | 2018-07-01 21:48:51 +0200 |
commit | 2d68b06f9dab66b2a0a6c29c8d5450e2b1d43fae (patch) | |
tree | 77f1b55fca2cf58595b33e8d062cf6437f85f283 | |
parent | e951043084937c42421debf457e33b3db3b5bac4 (diff) |
fsck: handle sparse files efficiently, fix xfstests generic/285
If we encounter a 128KB block of zeros, try to skip to the next
data section by calling File.SeekData().
This fixes xfstests generic/285, which creates a 17TB sparse file,
and runs fsck afterwards. Without this optimization, fsck would
take ages.
-rw-r--r-- | fsck.go | 11 | ||||
-rw-r--r-- | tests/fsck/fsck_test.go | 40 |
2 files changed, 51 insertions, 0 deletions
@@ -1,6 +1,7 @@ package main import ( + "bytes" "fmt" "os" "path/filepath" @@ -115,6 +116,7 @@ func (ck *fsckObj) file(path string) { return } defer f.Release() + allZero := make([]byte, fuse.MAX_KERNEL_WRITE) buf := make([]byte, fuse.MAX_KERNEL_WRITE) var off int64 // Read() through the whole file and catch transparently mitigated corruptions @@ -132,6 +134,15 @@ func (ck *fsckObj) file(path string) { return } off += int64(result.Size()) + // If we seem to be in the middle of a file hole, try to skip to the next + // data section. + if bytes.Equal(buf, allZero) { + f2 := f.(*fusefrontend.File) + nextOff, err := f2.SeekData(off) + if err == nil { + off = nextOff + } + } } } diff --git a/tests/fsck/fsck_test.go b/tests/fsck/fsck_test.go index 8e73cf7..5ece6cd 100644 --- a/tests/fsck/fsck_test.go +++ b/tests/fsck/fsck_test.go @@ -4,8 +4,10 @@ import ( "encoding/base64" "os" "os/exec" + "runtime" "strings" "testing" + "time" "github.com/pkg/xattr" @@ -84,3 +86,41 @@ func TestExampleFses(t *testing.T) { } } } + +// TestTerabyteFile verifies that fsck does something intelligent when it hits +// a 1-terabyte sparse file (trying to read the whole file is not intelligent). +func TestTerabyteFile(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skipf("Only linux supports SEEK_DATA") + } + cDir := test_helpers.InitFS(t) + pDir := cDir + ".mnt" + test_helpers.MountOrFatal(t, cDir, pDir, "-extpass", "echo test") + defer test_helpers.UnmountErr(pDir) + exabyteFile := pDir + "/exabyteFile" + fd, err := os.Create(exabyteFile) + if err != nil { + t.Fatal(err) + } + defer fd.Close() + var oneTiB int64 = 1024 * 1024 * 1024 * 1024 + _, err = fd.WriteAt([]byte("foobar"), oneTiB) + if err != nil { + t.Fatal(err) + } + fi, err := fd.Stat() + if err != nil { + t.Fatal(err) + } + t.Logf("size=%d, running fsck", fi.Size()) + cmd := exec.Command(test_helpers.GocryptfsBinary, "-fsck", "-extpass", "echo test", cDir) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Start() + timer := time.AfterFunc(10*time.Second, func() { + cmd.Process.Kill() + t.Fatalf("timeout") + }) + cmd.Wait() + timer.Stop() +} |