2018-04-01 14:25:10 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-01 19:14:00 +02:00
|
|
|
"bytes"
|
2018-04-01 21:23:32 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-04-02 16:56:29 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-04-03 21:19:44 +02:00
|
|
|
"sync"
|
2018-04-01 21:23:32 +02:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
2018-07-23 22:34:41 +02:00
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
2018-04-01 21:23:32 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/fusefrontend"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2018-04-01 14:25:10 +02:00
|
|
|
)
|
|
|
|
|
2018-04-01 21:23:32 +02:00
|
|
|
type fsckObj struct {
|
2018-04-02 18:32:30 +02:00
|
|
|
fs *fusefrontend.FS
|
|
|
|
// List of corrupt files
|
|
|
|
corruptList []string
|
2018-04-03 21:19:44 +02:00
|
|
|
// Protects corruptList
|
|
|
|
corruptListLock sync.Mutex
|
2018-07-01 16:24:02 +02:00
|
|
|
// stop a running watchMitigatedCorruptions thread
|
|
|
|
watchDone chan struct{}
|
2018-08-15 14:02:05 +02:00
|
|
|
// Inode numbers of hard-linked files (Nlink > 1) that we have already checked
|
|
|
|
seenInodes map[uint64]struct{}
|
2018-04-02 18:32:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ck *fsckObj) markCorrupt(path string) {
|
2018-04-03 21:19:44 +02:00
|
|
|
ck.corruptListLock.Lock()
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.corruptList = append(ck.corruptList, path)
|
2018-04-03 21:19:44 +02:00
|
|
|
ck.corruptListLock.Unlock()
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-01 16:24:02 +02:00
|
|
|
// Watch for mitigated corruptions that occour during OpenDir()
|
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsOpenDir(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-ck.fs.MitigatedCorruptions:
|
|
|
|
fmt.Printf("fsck: corrupt entry in dir %q: %q\n", path, item)
|
|
|
|
ck.markCorrupt(filepath.Join(path, item))
|
|
|
|
case <-ck.watchDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:23:32 +02:00
|
|
|
// Recursively check dir for corruption
|
|
|
|
func (ck *fsckObj) dir(path string) {
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.dir %q\n", path)
|
2018-04-02 20:25:59 +02:00
|
|
|
ck.xattrs(path)
|
2018-07-01 16:24:02 +02:00
|
|
|
// Run OpenDir and catch transparently mitigated corruptions
|
|
|
|
go ck.watchMitigatedCorruptionsOpenDir(path)
|
2018-04-01 21:23:32 +02:00
|
|
|
entries, status := ck.fs.OpenDir(path, nil)
|
2018-07-01 16:24:02 +02:00
|
|
|
ck.watchDone <- struct{}{}
|
|
|
|
// Also catch non-mitigated corruptions
|
2018-04-01 21:23:32 +02:00
|
|
|
if !status.Ok() {
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.markCorrupt(path)
|
2018-04-01 21:23:32 +02:00
|
|
|
fmt.Printf("fsck: error opening dir %q: %v\n", path, status)
|
|
|
|
return
|
|
|
|
}
|
2018-04-02 16:56:29 +02:00
|
|
|
// Sort alphabetically
|
|
|
|
sort.Sort(sortableDirEntries(entries))
|
2018-04-01 21:23:32 +02:00
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.Name == "." || entry.Name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
nextPath := filepath.Join(path, entry.Name)
|
|
|
|
filetype := entry.Mode & syscall.S_IFMT
|
|
|
|
//fmt.Printf(" %q %x\n", entry.Name, entry.Mode)
|
|
|
|
switch filetype {
|
|
|
|
case syscall.S_IFDIR:
|
|
|
|
ck.dir(nextPath)
|
|
|
|
case syscall.S_IFREG:
|
|
|
|
ck.file(nextPath)
|
|
|
|
case syscall.S_IFLNK:
|
|
|
|
ck.symlink(nextPath)
|
|
|
|
case syscall.S_IFIFO, syscall.S_IFSOCK, syscall.S_IFBLK, syscall.S_IFCHR:
|
|
|
|
// nothing to check
|
|
|
|
default:
|
2018-04-02 20:25:59 +02:00
|
|
|
fmt.Printf("fsck: unhandled file type %x\n", filetype)
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ck *fsckObj) symlink(path string) {
|
|
|
|
_, status := ck.fs.Readlink(path, nil)
|
|
|
|
if !status.Ok() {
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.markCorrupt(path)
|
2018-04-01 21:23:32 +02:00
|
|
|
fmt.Printf("fsck: error reading symlink %q: %v\n", path, status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:24:02 +02:00
|
|
|
// Watch for mitigated corruptions that occour during Read()
|
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsRead(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-ck.fs.MitigatedCorruptions:
|
|
|
|
fmt.Printf("fsck: corrupt file %q (inode %s)\n", path, item)
|
|
|
|
ck.markCorrupt(path)
|
|
|
|
case <-ck.watchDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check file for corruption
|
2018-04-01 21:23:32 +02:00
|
|
|
func (ck *fsckObj) file(path string) {
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.file %q\n", path)
|
2018-08-15 14:02:05 +02:00
|
|
|
attr, status := ck.fs.GetAttr(path, nil)
|
|
|
|
if !status.Ok() {
|
|
|
|
ck.markCorrupt(path)
|
|
|
|
fmt.Printf("fsck: error stating file %q: %v\n", path, status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if attr.Nlink > 1 {
|
|
|
|
// Due to hard links, we may have already checked this file.
|
|
|
|
if _, ok := ck.seenInodes[attr.Ino]; ok {
|
|
|
|
tlog.Debug.Printf("ck.file : skipping %q (inode number %d already seen)\n", path, attr.Ino)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ck.seenInodes[attr.Ino] = struct{}{}
|
|
|
|
}
|
2018-04-02 20:25:59 +02:00
|
|
|
ck.xattrs(path)
|
2018-04-01 21:23:32 +02:00
|
|
|
f, status := ck.fs.Open(path, syscall.O_RDONLY, nil)
|
|
|
|
if !status.Ok() {
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.markCorrupt(path)
|
2018-04-01 21:23:32 +02:00
|
|
|
fmt.Printf("fsck: error opening file %q: %v\n", path, status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Release()
|
2018-07-01 19:14:00 +02:00
|
|
|
allZero := make([]byte, fuse.MAX_KERNEL_WRITE)
|
2018-04-01 21:23:32 +02:00
|
|
|
buf := make([]byte, fuse.MAX_KERNEL_WRITE)
|
|
|
|
var off int64
|
2018-07-01 16:24:02 +02:00
|
|
|
// Read() through the whole file and catch transparently mitigated corruptions
|
|
|
|
go ck.watchMitigatedCorruptionsRead(path)
|
|
|
|
defer func() { ck.watchDone <- struct{}{} }()
|
2018-04-01 21:23:32 +02:00
|
|
|
for {
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.file: read %d bytes from offset %d\n", len(buf), off)
|
2018-04-01 21:23:32 +02:00
|
|
|
result, status := f.Read(buf, off)
|
|
|
|
if !status.Ok() {
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.markCorrupt(path)
|
2018-08-15 13:20:40 +02:00
|
|
|
fmt.Printf("fsck: error reading file %q (inum %d): %v\n", path, inum(f), status)
|
2018-04-01 21:23:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// EOF
|
|
|
|
if result.Size() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
off += int64(result.Size())
|
2018-07-01 19:14:00 +02:00
|
|
|
// 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) {
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.file: trying to skip file hole\n")
|
2018-07-01 19:14:00 +02:00
|
|
|
f2 := f.(*fusefrontend.File)
|
|
|
|
nextOff, err := f2.SeekData(off)
|
|
|
|
if err == nil {
|
|
|
|
off = nextOff
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:24:02 +02:00
|
|
|
// Watch for mitigated corruptions that occour during ListXAttr()
|
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsListXAttr(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case item := <-ck.fs.MitigatedCorruptions:
|
|
|
|
fmt.Printf("fsck: corrupt xattr name on file %q: %q\n", path, item)
|
|
|
|
ck.markCorrupt(path + " xattr:" + item)
|
|
|
|
case <-ck.watchDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 20:25:59 +02:00
|
|
|
// Check xattrs on file/dir at path
|
|
|
|
func (ck *fsckObj) xattrs(path string) {
|
2018-07-01 16:24:02 +02:00
|
|
|
// Run ListXAttr() and catch transparently mitigated corruptions
|
|
|
|
go ck.watchMitigatedCorruptionsListXAttr(path)
|
2018-04-02 20:25:59 +02:00
|
|
|
attrs, status := ck.fs.ListXAttr(path, nil)
|
2018-07-01 16:24:02 +02:00
|
|
|
ck.watchDone <- struct{}{}
|
|
|
|
// Also catch non-mitigated corruptions
|
2018-04-02 20:25:59 +02:00
|
|
|
if !status.Ok() {
|
|
|
|
fmt.Printf("fsck: error listing xattrs on %q: %v\n", path, status)
|
|
|
|
ck.markCorrupt(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, a := range attrs {
|
|
|
|
_, status := ck.fs.GetXAttr(path, a, nil)
|
|
|
|
if !status.Ok() {
|
|
|
|
fmt.Printf("fsck: error reading xattr %q from %q: %v\n", a, path, status)
|
|
|
|
ck.markCorrupt(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 14:25:10 +02:00
|
|
|
func fsck(args *argContainer) {
|
2018-04-01 21:23:32 +02:00
|
|
|
if args.reverse {
|
|
|
|
tlog.Fatal.Printf("Running -fsck with -reverse is not supported")
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
args.allow_other = false
|
|
|
|
pfs, wipeKeys := initFuseFrontend(args)
|
|
|
|
fs := pfs.(*fusefrontend.FS)
|
2018-07-01 15:48:53 +02:00
|
|
|
fs.MitigatedCorruptions = make(chan string)
|
2018-04-01 21:23:32 +02:00
|
|
|
ck := fsckObj{
|
2018-08-15 14:02:05 +02:00
|
|
|
fs: fs,
|
|
|
|
watchDone: make(chan struct{}),
|
|
|
|
seenInodes: make(map[uint64]struct{}),
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
ck.dir("")
|
2018-04-02 18:32:30 +02:00
|
|
|
wipeKeys()
|
|
|
|
if len(ck.corruptList) == 0 {
|
2018-06-27 22:17:41 +02:00
|
|
|
tlog.Info.Printf("fsck summary: no problems found\n")
|
2018-04-02 18:32:30 +02:00
|
|
|
return
|
|
|
|
}
|
2018-04-03 21:19:44 +02:00
|
|
|
fmt.Printf("fsck summary: %d corrupt files\n", len(ck.corruptList))
|
2018-04-02 18:32:30 +02:00
|
|
|
os.Exit(exitcodes.FsckErrors)
|
2018-04-01 14:25:10 +02:00
|
|
|
}
|
2018-04-02 16:56:29 +02:00
|
|
|
|
|
|
|
type sortableDirEntries []fuse.DirEntry
|
|
|
|
|
|
|
|
func (s sortableDirEntries) Len() int {
|
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sortableDirEntries) Swap(i, j int) {
|
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sortableDirEntries) Less(i, j int) bool {
|
|
|
|
return strings.Compare(s[i].Name, s[j].Name) < 0
|
|
|
|
}
|
2018-07-23 22:34:41 +02:00
|
|
|
|
|
|
|
func inum(f nodefs.File) uint64 {
|
|
|
|
var a fuse.Attr
|
|
|
|
f.GetAttr(&a)
|
|
|
|
return a.Ino
|
|
|
|
}
|