- Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathfsck.go
335 lines (318 loc) · 8.77 KB
/
fsck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"sort"
"sync"
"syscall"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/rfjakob/gocryptfs/v2/internal/exitcodes"
"github.com/rfjakob/gocryptfs/v2/internal/fusefrontend"
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
)
typefsckObjstruct {
rootNode*fusefrontend.RootNode
// mnt is the mountpoint of the temporary mount
mntstring
// List of corrupt files
corruptList []string
// List of skipped files
skippedList []string
// Protects corruptList
listLock sync.Mutex
// stop a running watchMitigatedCorruptions thread
watchDonechanstruct{}
// Inode numbers of hard-linked files (Nlink > 1) that we have already checked
seenInodesmap[uint64]struct{}
// abort the running fsck operation? Checked in a few long-running loops.
abortbool
}
funcrunsAsRoot() bool {
returnsyscall.Geteuid() ==0
}
func (ck*fsckObj) markCorrupt(pathstring) {
ck.listLock.Lock()
ck.corruptList=append(ck.corruptList, path)
ck.listLock.Unlock()
}
func (ck*fsckObj) markSkipped(pathstring) {
ck.listLock.Lock()
ck.skippedList=append(ck.skippedList, path)
ck.listLock.Unlock()
}
func (ck*fsckObj) abs(relPathstring) (absPathstring) {
returnfilepath.Join(ck.mnt, relPath)
}
// Watch for mitigated corruptions that occur during OpenDir()
func (ck*fsckObj) watchMitigatedCorruptionsOpenDir(pathstring) {
for {
select {
caseitem:=<-ck.rootNode.MitigatedCorruptions:
fmt.Printf("fsck: corrupt entry in dir %q: %q\n", path, item)
ck.markCorrupt(filepath.Join(path, item))
case<-ck.watchDone:
return
}
}
}
// Recursively check dir for corruption
func (ck*fsckObj) dir(relPathstring) {
tlog.Debug.Printf("ck.dir %q\n", relPath)
ck.xattrs(relPath)
// Run OpenDir and catch transparently mitigated corruptions
gock.watchMitigatedCorruptionsOpenDir(relPath)
f, err:=os.Open(ck.abs(relPath))
ck.watchDone<-struct{}{}
iferr!=nil {
fmt.Printf("fsck: error opening dir %q: %v\n", relPath, err)
iferr==os.ErrPermission&&!runsAsRoot() {
ck.markSkipped(relPath)
} else {
ck.markCorrupt(relPath)
}
return
}
gock.watchMitigatedCorruptionsOpenDir(relPath)
entries, err:=f.Readdirnames(0)
ck.watchDone<-struct{}{}
iferr!=nil {
fmt.Printf("fsck: error reading dir %q: %v\n", relPath, err)
ck.markCorrupt(relPath)
return
}
// Sort alphabetically to make fsck runs deterministic
sort.Strings(entries)
for_, entry:=rangeentries {
ifck.abort {
return
}
ifentry=="."||entry==".." {
continue
}
nextPath:=filepath.Join(relPath, entry)
varst syscall.Stat_t
err:=syscall.Lstat(ck.abs(nextPath), &st)
iferr!=nil {
ck.markCorrupt(filepath.Join(relPath, entry))
continue
}
filetype:=st.Mode&syscall.S_IFMT
//fmt.Printf(" %q %x\n", entry.Name, entry.Mode)
switchfiletype {
casesyscall.S_IFDIR:
ck.dir(nextPath)
casesyscall.S_IFREG:
ck.file(nextPath)
casesyscall.S_IFLNK:
ck.symlink(nextPath)
casesyscall.S_IFIFO, syscall.S_IFSOCK, syscall.S_IFBLK, syscall.S_IFCHR:
// nothing to check
default:
fmt.Printf("fsck: unhandled file type %x\n", filetype)
}
}
}
func (ck*fsckObj) symlink(relPathstring) {
_, err:=os.Readlink(ck.abs(relPath))
iferr!=nil {
ck.markCorrupt(relPath)
fmt.Printf("fsck: error reading symlink %q: %v\n", relPath, err)
}
}
// Watch for mitigated corruptions that occur during Read()
func (ck*fsckObj) watchMitigatedCorruptionsRead(pathstring) {
for {
select {
caseitem:=<-ck.rootNode.MitigatedCorruptions:
fmt.Printf("fsck: corrupt file %q (inode %s)\n", path, item)
ck.markCorrupt(path)
case<-ck.watchDone:
return
}
}
}
// Check file for corruption
func (ck*fsckObj) file(relPathstring) {
tlog.Debug.Printf("ck.file %q\n", relPath)
varst syscall.Stat_t
err:=syscall.Lstat(ck.abs(relPath), &st)
iferr!=nil {
ck.markCorrupt(relPath)
fmt.Printf("fsck: error stating file %q: %v\n", relPath, err)
return
}
ifst.Nlink>1 {
// Due to hard links, we may have already checked this file.
if_, ok:=ck.seenInodes[st.Ino]; ok {
tlog.Debug.Printf("ck.file : skipping %q (inode number %d already seen)\n", relPath, st.Ino)
return
}
ck.seenInodes[st.Ino] =struct{}{}
}
ck.xattrs(relPath)
f, err:=os.Open(ck.abs(relPath))
iferr!=nil {
fmt.Printf("fsck: error opening file %q: %v\n", relPath, err)
iferr==os.ErrPermission&&!runsAsRoot() {
ck.markSkipped(relPath)
} else {
ck.markCorrupt(relPath)
}
return
}
deferf.Close()
// 128 kiB of zeros
allZero:=make([]byte, fuse.MAX_KERNEL_WRITE)
buf:=make([]byte, fuse.MAX_KERNEL_WRITE)
varoffint64
// Read() through the whole file and catch transparently mitigated corruptions
gock.watchMitigatedCorruptionsRead(relPath)
deferfunc() { ck.watchDone<-struct{}{} }()
for {
ifck.abort {
return
}
tlog.Debug.Printf("ck.file: read %d bytes from offset %d\n", len(buf), off)
n, err:=f.ReadAt(buf, off)
iferr!=nil&&err!=io.EOF {
ck.markCorrupt(relPath)
fmt.Printf("fsck: error reading file %q (inum %d): %v\n", relPath, inum(f), err)
return
}
// EOF
iferr==io.EOF {
return
}
off+=int64(n)
// If we seem to be in the middle of a file hole, try to skip to the next
// data section.
data:=buf[:n]
ifbytes.Equal(data, allZero) {
tlog.Debug.Printf("ck.file: trying to skip file hole\n")
constSEEK_DATA=3
nextOff, err:=syscall.Seek(int(f.Fd()), off, SEEK_DATA)
iferr==nil {
off=nextOff
}
}
}
}
// Watch for mitigated corruptions that occur during ListXAttr()
func (ck*fsckObj) watchMitigatedCorruptionsListXAttr(pathstring) {
for {
select {
caseitem:=<-ck.rootNode.MitigatedCorruptions:
fmt.Printf("fsck: corrupt xattr name on file %q: %q\n", path, item)
ck.markCorrupt(path+" xattr:"+item)
case<-ck.watchDone:
return
}
}
}
// Check xattrs on file/dir at path
func (ck*fsckObj) xattrs(relPathstring) {
// Run ListXAttr() and catch transparently mitigated corruptions
gock.watchMitigatedCorruptionsListXAttr(relPath)
attrs, err:=syscallcompat.Llistxattr(ck.abs(relPath))
ck.watchDone<-struct{}{}
iferr!=nil {
fmt.Printf("fsck: error listing xattrs on %q: %v\n", relPath, err)
ck.markCorrupt(relPath)
return
}
// Try to read all xattr values
for_, a:=rangeattrs {
_, err:=syscallcompat.Lgetxattr(ck.abs(relPath), a)
iferr!=nil {
fmt.Printf("fsck: error reading xattr %q from %q: %v\n", a, relPath, err)
iferr==syscall.EACCES&&!runsAsRoot() {
ck.markSkipped(relPath)
} else {
ck.markCorrupt(relPath)
}
}
}
}
// entrypoint from main()
funcfsck(args*argContainer) (exitcodeint) {
ifargs.reverse {
tlog.Fatal.Printf("Running -fsck with -reverse is not supported")
os.Exit(exitcodes.Usage)
}
args.allow_other=false
args.ro=true
varerrerror
args.mountpoint, err=ioutil.TempDir("", "gocryptfs.fsck.")
iferr!=nil {
tlog.Fatal.Printf("fsck: TmpDir: %v", err)
os.Exit(exitcodes.MountPoint)
}
pfs, wipeKeys:=initFuseFrontend(args)
rn:=pfs.(*fusefrontend.RootNode)
rn.MitigatedCorruptions=make(chanstring)
ck:=fsckObj{
mnt: args.mountpoint,
rootNode: rn,
watchDone: make(chanstruct{}),
seenInodes: make(map[uint64]struct{}),
}
ifargs.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()
}
// Mount
srv:=initGoFuse(pfs, args)
// Handle SIGINT & SIGTERM
ch:=make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
gofunc() {
<-ch
ck.abort=true
}()
deferfunc() {
err=srv.Unmount()
iferr!=nil {
tlog.Warn.Printf("failed to unmount %q: %v", ck.mnt, err)
} else {
iferr:=syscall.Rmdir(ck.mnt); err!=nil {
tlog.Warn.Printf("cleaning up %q failed: %v", ck.mnt, err)
}
}
}()
// Recursively check the root dir
tlog.Info.Println(tlog.ColorGreen+"Checking filesystem..."+tlog.ColorReset)
ck.dir("")
// Report results
wipeKeys()
ifck.abort {
tlog.Info.Printf("fsck: aborted")
returnexitcodes.Other
}
iflen(ck.corruptList) ==0&&len(ck.skippedList) ==0 {
tlog.Info.Printf("fsck summary: no problems found\n")
return0
}
iflen(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))
returnexitcodes.FsckErrors
}
funcinum(f*os.File) uint64 {
varst syscall.Stat_t
err:=syscall.Fstat(int(f.Fd()), &st)
iferr!=nil {
tlog.Warn.Printf("inum: fstat failed: %v", err)
return0
}
returnst.Ino
}