Skip to content

Commit

Permalink
Retry defragmentation for next file if previous one became sparse
Browse files Browse the repository at this point in the history
  • Loading branch information
bertbaron committed Dec 27, 2019
1 parent 2166b4e commit 6dcb80f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ func allowedFragcount(file *storage.FileInformation, minBpf int) int {
// Note that when we will do the deduplication more clever (comparing all blocks of all files), we may also need to do
// the defragmentation in a more clever way.
func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, minBpf int, noact bool) (copy []*storage.FileInformation) {
if len(files) == 0 {
return files
}
copy = make([]*storage.FileInformation, len(files), len(files))
for idx, file := range files {
copy[idx] = file
Expand All @@ -198,6 +201,7 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min
}
fragcount := len(copy[0].Fragments)
allowedFragcount := allowedFragcount(copy[0], minBpf)
allowedFragcount = 1
if fragcount <= allowedFragcount {
return
}
Expand Down Expand Up @@ -247,9 +251,10 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min

if newFile, err := readFileMeta(file.Path, path); err != nil {
log.Printf("Error while reading the fragmentation table again: %v", err)
return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact)
} else if newFile == nil {
log.Printf("File can not be deduplicated after defragmentation")
copy = copy[1:]
return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact)
} else {
copy[0] = newFile
log.Printf("Number of fragments was %d and is now %d for file %s", fragcount, len(newFile.Fragments), path)
Expand Down
2 changes: 1 addition & 1 deletion sys/frags.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func Fragments(file *os.File) ([]Fragment, error) {
return nil, err
}
if data.fm_mapped_extents == 0 {
return nil, errors.New("No (more) extends found")
return nil, errors.New("No (more) extends found, file may be sparse")
}
for _, extend := range data.fm_extents[0:data.fm_mapped_extents] {
last = last || extend.fe_flags&FIEMAP_EXTENT_LAST != 0
Expand Down

0 comments on commit 6dcb80f

Please sign in to comment.