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"
|
2020-07-19 23:03:47 +02:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-04-01 21:23:32 +02:00
|
|
|
"os"
|
2020-07-19 23:03:47 +02:00
|
|
|
"os/signal"
|
2018-04-01 21:23:32 +02:00
|
|
|
"path/filepath"
|
2018-04-02 16:56:29 +02:00
|
|
|
"strings"
|
2018-04-03 21:19:44 +02:00
|
|
|
"sync"
|
2018-04-01 21:23:32 +02:00
|
|
|
"syscall"
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2018-04-01 21:23:32 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/fusefrontend"
|
2020-07-19 23:03:47 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2018-04-01 21:23:32 +02:00
|
|
|
"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 {
|
2020-07-18 23:41:27 +02:00
|
|
|
rootNode *fusefrontend.RootNode
|
2020-07-19 23:03:47 +02:00
|
|
|
// mnt is the mountpoint of the temporary mount
|
|
|
|
mnt string
|
2018-04-02 18:32:30 +02:00
|
|
|
// List of corrupt files
|
|
|
|
corruptList []string
|
2019-01-04 04:53:55 +01:00
|
|
|
// List of skipped files
|
|
|
|
skippedList []string
|
2018-04-03 21:19:44 +02:00
|
|
|
// Protects corruptList
|
2019-01-04 04:53:55 +01:00
|
|
|
listLock 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{}
|
2020-07-19 23:03:47 +02:00
|
|
|
// abort the running fsck operation? Checked in a few long-running loops.
|
|
|
|
abort bool
|
2018-04-02 18:32:30 +02:00
|
|
|
}
|
|
|
|
|
2019-01-04 04:53:55 +01:00
|
|
|
func runsAsRoot() bool {
|
|
|
|
return syscall.Geteuid() == 0
|
|
|
|
}
|
|
|
|
|
2018-04-02 18:32:30 +02:00
|
|
|
func (ck *fsckObj) markCorrupt(path string) {
|
2019-01-04 04:53:55 +01:00
|
|
|
ck.listLock.Lock()
|
2018-04-02 18:32:30 +02:00
|
|
|
ck.corruptList = append(ck.corruptList, path)
|
2019-01-04 04:53:55 +01:00
|
|
|
ck.listLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ck *fsckObj) markSkipped(path string) {
|
|
|
|
ck.listLock.Lock()
|
|
|
|
ck.skippedList = append(ck.skippedList, path)
|
|
|
|
ck.listLock.Unlock()
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:03:47 +02:00
|
|
|
func (ck *fsckObj) abs(relPath string) (absPath string) {
|
|
|
|
return filepath.Join(ck.mnt, relPath)
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:03:00 +01:00
|
|
|
// Watch for mitigated corruptions that occur during OpenDir()
|
2018-07-01 16:24:02 +02:00
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsOpenDir(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
2020-07-18 23:41:27 +02:00
|
|
|
case item := <-ck.rootNode.MitigatedCorruptions:
|
2018-07-01 16:24:02 +02:00
|
|
|
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
|
2020-07-19 23:03:47 +02:00
|
|
|
func (ck *fsckObj) dir(relPath string) {
|
|
|
|
tlog.Debug.Printf("ck.dir %q\n", relPath)
|
|
|
|
ck.xattrs(relPath)
|
2018-07-01 16:24:02 +02:00
|
|
|
// Run OpenDir and catch transparently mitigated corruptions
|
2020-07-19 23:03:47 +02:00
|
|
|
go ck.watchMitigatedCorruptionsOpenDir(relPath)
|
|
|
|
f, err := os.Open(ck.abs(relPath))
|
2018-07-01 16:24:02 +02:00
|
|
|
ck.watchDone <- struct{}{}
|
2020-07-19 23:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fsck: error opening dir %q: %v\n", relPath, err)
|
|
|
|
if err == os.ErrPermission && !runsAsRoot() {
|
|
|
|
ck.markSkipped(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
} else {
|
2020-07-19 23:03:47 +02:00
|
|
|
ck.markCorrupt(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
}
|
2018-04-01 21:23:32 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
go ck.watchMitigatedCorruptionsOpenDir(relPath)
|
|
|
|
entries, err := f.Readdirnames(0)
|
|
|
|
ck.watchDone <- struct{}{}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fsck: error reading dir %q: %v\n", relPath, err)
|
|
|
|
ck.markCorrupt(relPath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, entry := range entries {
|
|
|
|
if ck.abort {
|
|
|
|
return
|
2020-07-18 23:41:27 +02:00
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
if entry == "." || entry == ".." {
|
2018-04-01 21:23:32 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
nextPath := filepath.Join(relPath, entry)
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err := syscall.Lstat(ck.abs(nextPath), &st)
|
|
|
|
if err != nil {
|
|
|
|
ck.markCorrupt(filepath.Join(relPath, entry))
|
2020-07-18 23:41:27 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
filetype := st.Mode & syscall.S_IFMT
|
2018-04-01 21:23:32 +02:00
|
|
|
//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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 23:03:47 +02:00
|
|
|
func (ck *fsckObj) symlink(relPath string) {
|
|
|
|
_, err := os.Readlink(ck.abs(relPath))
|
|
|
|
if err != nil {
|
|
|
|
ck.markCorrupt(relPath)
|
|
|
|
fmt.Printf("fsck: error reading symlink %q: %v\n", relPath, err)
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:03:00 +01:00
|
|
|
// Watch for mitigated corruptions that occur during Read()
|
2018-07-01 16:24:02 +02:00
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsRead(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
2020-07-18 23:41:27 +02:00
|
|
|
case item := <-ck.rootNode.MitigatedCorruptions:
|
2018-07-01 16:24:02 +02:00
|
|
|
fmt.Printf("fsck: corrupt file %q (inode %s)\n", path, item)
|
|
|
|
ck.markCorrupt(path)
|
|
|
|
case <-ck.watchDone:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check file for corruption
|
2020-07-19 23:03:47 +02:00
|
|
|
func (ck *fsckObj) file(relPath string) {
|
|
|
|
tlog.Debug.Printf("ck.file %q\n", relPath)
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err := syscall.Lstat(ck.abs(relPath), &st)
|
|
|
|
if err != nil {
|
|
|
|
ck.markCorrupt(relPath)
|
|
|
|
fmt.Printf("fsck: error stating file %q: %v\n", relPath, err)
|
2018-08-15 14:02:05 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
if st.Nlink > 1 {
|
2018-08-15 14:02:05 +02:00
|
|
|
// Due to hard links, we may have already checked this file.
|
2020-07-19 23:03:47 +02:00
|
|
|
if _, ok := ck.seenInodes[st.Ino]; ok {
|
|
|
|
tlog.Debug.Printf("ck.file : skipping %q (inode number %d already seen)\n", relPath, st.Ino)
|
2018-08-15 14:02:05 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
ck.seenInodes[st.Ino] = struct{}{}
|
2018-08-15 14:02:05 +02:00
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
ck.xattrs(relPath)
|
|
|
|
f, err := os.Open(ck.abs(relPath))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fsck: error opening file %q: %v\n", relPath, err)
|
|
|
|
if err == os.ErrPermission && !runsAsRoot() {
|
|
|
|
ck.markSkipped(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
} else {
|
2020-07-19 23:03:47 +02:00
|
|
|
ck.markCorrupt(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
}
|
2018-04-01 21:23:32 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
defer f.Close()
|
2019-01-04 20:23:01 +01:00
|
|
|
// 128 kiB of zeros
|
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
|
2020-07-19 23:03:47 +02:00
|
|
|
go ck.watchMitigatedCorruptionsRead(relPath)
|
2018-07-01 16:24:02 +02:00
|
|
|
defer func() { ck.watchDone <- struct{}{} }()
|
2018-04-01 21:23:32 +02:00
|
|
|
for {
|
2020-07-19 23:03:47 +02:00
|
|
|
if ck.abort {
|
|
|
|
return
|
|
|
|
}
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.file: read %d bytes from offset %d\n", len(buf), off)
|
2020-07-19 23:03:47 +02:00
|
|
|
n, err := f.ReadAt(buf, off)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
ck.markCorrupt(relPath)
|
|
|
|
fmt.Printf("fsck: error reading file %q (inum %d): %v\n", relPath, inum(f), err)
|
2018-04-01 21:23:32 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// EOF
|
2020-07-19 23:03:47 +02:00
|
|
|
if err == io.EOF {
|
2018-04-01 21:23:32 +02:00
|
|
|
return
|
|
|
|
}
|
2019-01-04 20:23:01 +01:00
|
|
|
off += int64(n)
|
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.
|
2019-01-04 20:23:01 +01:00
|
|
|
data := buf[:n]
|
|
|
|
if bytes.Equal(data, allZero) {
|
2018-07-15 11:34:30 +02:00
|
|
|
tlog.Debug.Printf("ck.file: trying to skip file hole\n")
|
2020-07-19 23:03:47 +02:00
|
|
|
const SEEK_DATA = 3
|
|
|
|
nextOff, err := syscall.Seek(int(f.Fd()), off, SEEK_DATA)
|
2018-07-01 19:14:00 +02:00
|
|
|
if err == nil {
|
|
|
|
off = nextOff
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:03:00 +01:00
|
|
|
// Watch for mitigated corruptions that occur during ListXAttr()
|
2018-07-01 16:24:02 +02:00
|
|
|
func (ck *fsckObj) watchMitigatedCorruptionsListXAttr(path string) {
|
|
|
|
for {
|
|
|
|
select {
|
2020-07-18 23:41:27 +02:00
|
|
|
case item := <-ck.rootNode.MitigatedCorruptions:
|
2018-07-01 16:24:02 +02:00
|
|
|
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
|
2020-07-19 23:03:47 +02:00
|
|
|
func (ck *fsckObj) xattrs(relPath string) {
|
2018-07-01 16:24:02 +02:00
|
|
|
// Run ListXAttr() and catch transparently mitigated corruptions
|
2020-07-19 23:03:47 +02:00
|
|
|
go ck.watchMitigatedCorruptionsListXAttr(relPath)
|
|
|
|
attrs, err := syscallcompat.Llistxattr(ck.abs(relPath))
|
2018-07-01 16:24:02 +02:00
|
|
|
ck.watchDone <- struct{}{}
|
2020-07-19 23:03:47 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fsck: error listing xattrs on %q: %v\n", relPath, err)
|
|
|
|
ck.markCorrupt(relPath)
|
2020-07-18 23:41:27 +02:00
|
|
|
return
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
// Try to read all xattr values
|
2018-04-02 20:25:59 +02:00
|
|
|
for _, a := range attrs {
|
2020-07-19 23:03:47 +02:00
|
|
|
_, err := syscallcompat.Lgetxattr(ck.abs(relPath), a)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fsck: error reading xattr %q from %q: %v\n", a, relPath, err)
|
|
|
|
if err == syscall.EACCES && !runsAsRoot() {
|
|
|
|
ck.markSkipped(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
} else {
|
2020-07-19 23:03:47 +02:00
|
|
|
ck.markCorrupt(relPath)
|
2019-01-04 04:53:55 +01:00
|
|
|
}
|
2018-04-02 20:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:38:23 +02:00
|
|
|
// entrypoint from main()
|
2020-10-17 23:03:58 +02:00
|
|
|
func fsck(args *argContainer) (exitcode int) {
|
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
|
2021-06-02 19:32:20 +02:00
|
|
|
args.ro = true
|
2020-07-19 23:03:47 +02:00
|
|
|
var err error
|
|
|
|
args.mountpoint, err = ioutil.TempDir("", "gocryptfs.fsck.")
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("fsck: TmpDir: %v", err)
|
|
|
|
os.Exit(exitcodes.MountPoint)
|
2020-07-19 12:48:29 +02:00
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
pfs, wipeKeys := initFuseFrontend(args)
|
2020-07-18 23:41:27 +02:00
|
|
|
rn := pfs.(*fusefrontend.RootNode)
|
|
|
|
rn.MitigatedCorruptions = make(chan string)
|
2018-04-01 21:23:32 +02:00
|
|
|
ck := fsckObj{
|
2020-07-19 23:03:47 +02:00
|
|
|
mnt: args.mountpoint,
|
2020-07-18 23:41:27 +02:00
|
|
|
rootNode: rn,
|
2018-08-15 14:02:05 +02:00
|
|
|
watchDone: make(chan struct{}),
|
|
|
|
seenInodes: make(map[uint64]struct{}),
|
2018-04-01 21:23:32 +02:00
|
|
|
}
|
2021-05-18 18:38:23 +02:00
|
|
|
if args.quiet {
|
|
|
|
// go-fuse throws a lot of these:
|
|
|
|
// writer: Write/Writev failed, err: 2=no such file or directory. opcode: INTERRUPT
|
|
|
|
// This is ugly and causes failures in xfstests. Hide them away in syslog.
|
|
|
|
tlog.SwitchLoggerToSyslog()
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
// Mount
|
|
|
|
srv := initGoFuse(pfs, args)
|
|
|
|
// Handle SIGINT & SIGTERM
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, os.Interrupt)
|
|
|
|
signal.Notify(ch, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
ck.abort = true
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
err = srv.Unmount()
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("failed to unmount %q: %v", ck.mnt, err)
|
2021-06-02 19:28:20 +02:00
|
|
|
} else {
|
|
|
|
if err := syscall.Rmdir(ck.mnt); err != nil {
|
|
|
|
tlog.Warn.Printf("cleaning up %q failed: %v", ck.mnt, err)
|
|
|
|
}
|
2020-07-19 23:03:47 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Recursively check the root dir
|
|
|
|
ck.dir("")
|
|
|
|
// Report results
|
2018-04-02 18:32:30 +02:00
|
|
|
wipeKeys()
|
2020-07-19 23:03:47 +02:00
|
|
|
if ck.abort {
|
|
|
|
tlog.Info.Printf("fsck: aborted")
|
2020-10-17 23:03:58 +02:00
|
|
|
return exitcodes.Other
|
2020-07-19 23:03:47 +02:00
|
|
|
}
|
2019-01-04 04:53:55 +01:00
|
|
|
if len(ck.corruptList) == 0 && len(ck.skippedList) == 0 {
|
2018-06-27 22:17:41 +02:00
|
|
|
tlog.Info.Printf("fsck summary: no problems found\n")
|
2020-10-17 23:03:58 +02:00
|
|
|
return 0
|
2018-04-02 18:32:30 +02:00
|
|
|
}
|
2019-01-04 04:53:55 +01:00
|
|
|
if len(ck.skippedList) > 0 {
|
|
|
|
tlog.Warn.Printf("fsck: re-run this program as root to check all files!\n")
|
|
|
|
}
|
|
|
|
fmt.Printf("fsck summary: %d corrupt files, %d files skipped\n", len(ck.corruptList), len(ck.skippedList))
|
2020-10-17 23:03:58 +02:00
|
|
|
return 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
|
|
|
|
2020-07-19 23:03:47 +02:00
|
|
|
func inum(f *os.File) uint64 {
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err := syscall.Fstat(int(f.Fd()), &st)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("inum: fstat failed: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return st.Ino
|
2018-07-23 22:34:41 +02:00
|
|
|
}
|