Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/fsck: add repair option to repair broken directories #2654

Merged
merged 3 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion cmd/fsck.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"fmt"
"path"
"sort"
"strings"
"time"
Expand All @@ -43,7 +44,20 @@ It scans all objects in data storage and slices in metadata, comparing them to s
lost object or broken file.

Examples:
$ juicefs fsck redis://localhost`,
$ juicefs fsck redis://localhost

# Repair broken directories
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be a file

$ juicefs fsck redis://localhost --paths /d1/d2 /d1/d3 --repair`,
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "paths",
Usage: "absolute paths within JuiceFS to check",
},
&cli.BoolFlag{
Name: "repair",
Usage: "repair specified paths if they are broken",
},
},
}
}

Expand All @@ -55,6 +69,18 @@ func fsck(ctx *cli.Context) error {
if err != nil {
logger.Fatalf("load setting: %s", err)
}
if ps := ctx.StringSlice("paths"); len(ps) > 0 {
var needRepair int
for _, p := range ps {
if checkPath(m, p, ctx.Bool("repair")) {
needRepair++
}
}
if needRepair > 0 {
logger.Infof("%d paths can be repaired, please re-run fsck with `--repair` option")
}
return nil
}

chunkConf := chunk.Config{
BlockSize: format.BlockSize * 1024,
Expand Down Expand Up @@ -170,3 +196,41 @@ func fsck(ctx *cli.Context) error {

return nil
}

func checkPath(m meta.Meta, fpath string, repair bool) (needRepair bool) {
if !strings.HasPrefix(fpath, "/") {
logger.Fatalf("File path should be the absolute path within JuiceFS")
}
var attr meta.Attr
var inode meta.Ino
var parent meta.Ino = 1
ps := strings.Split(fpath, "/")
for i, name := range ps {
if len(name) == 0 {
continue
}
if st := m.Lookup(meta.Background, parent, name, &inode, &attr); st != 0 {
logger.Fatalf("Lookup parent %d name %s: %s", parent, name, st)
} else if !attr.Full { // missing attribute
p := "/" + path.Join(ps[:i+1]...)
if attr.Typ == meta.TypeDirectory {
if repair {
if st = m.RepairInode(meta.Background, parent, inode); st == 0 {
logger.Infof("Path %s is successfully repaired: inode %d", p, inode)
} else {
logger.Fatalf("Repair path %s inode %d: %s", p, inode, st)
}
} else {
needRepair = true
logger.Warnf("Path %s is lack of attribute: inode %d", p, inode)
}
} else { // TODO: determine file size?
logger.Warnf("Path %s cannot be auto-repaired: inode %d type %d", p, inode, attr.Typ)
}
return // handle one missing inode at a time
}
parent = inode
}
logger.Infof("Path %s inode %d is valid", fpath, parent)
return
}
2 changes: 2 additions & 0 deletions pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ type Meta interface {
ListSlices(ctx Context, slices map[Ino][]Slice, delete bool, showProgress func()) syscall.Errno
// Remove all files and directories recursively.
Remove(ctx Context, parent Ino, name string, count *uint64) syscall.Errno
// Currently only repairs a directory that is lack of attribute
RepairInode(ctx Context, parent, inode Ino) syscall.Errno

// OnMsg add a callback for the given message type.
OnMsg(mtype uint32, cb MsgCallback)
Expand Down
26 changes: 26 additions & 0 deletions pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2824,6 +2824,32 @@ func (m *redisMeta) ListSlices(ctx Context, slices map[Ino][]Slice, delete bool,
return errno(err)
}

func (m *redisMeta) RepairInode(ctx Context, parent, inode Ino) syscall.Errno {
SandyXSD marked this conversation as resolved.
Show resolved Hide resolved
now := time.Now().Unix()
attr := &Attr{
Typ: TypeDirectory,
Atime: now,
Mtime: now,
Ctime: now,
Length: 4 << 10,
Parent: parent,
}
return errno(m.txn(ctx, func(tx *redis.Tx) error {
attr.Nlink = 2
vals, err := tx.HGetAll(ctx, m.entryKey(inode)).Result()
if err != nil {
return err
}
for _, v := range vals {
typ, _ := m.parseEntry([]byte(v))
if typ == TypeDirectory {
attr.Nlink++
}
}
return tx.Set(ctx, m.inodeKey(inode), m.marshal(attr), 0).Err()
}, m.entryKey(inode), m.inodeKey(inode)))
}

func (m *redisMeta) GetXattr(ctx Context, inode Ino, name string, vbuff *[]byte) syscall.Errno {
defer m.timeit(time.Now())
inode = m.checkRoot(inode)
Expand Down
25 changes: 25 additions & 0 deletions pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,31 @@ func (m *dbMeta) ListSlices(ctx Context, slices map[Ino][]Slice, delete bool, sh
return errno(err)
}

func (m *dbMeta) RepairInode(ctx Context, parent, inode Ino) syscall.Errno {
now := time.Now()
n := &node{
Type: TypeDirectory,
Atime: now.UnixNano() / 1000,
Mtime: now.UnixNano() / 1000,
Ctime: now.UnixNano() / 1000,
Length: 4 << 10,
Parent: parent,
}
return errno(m.txn(func(s *xorm.Session) error {
n.Nlink = 2
var rows []edge
if err := s.Find(&rows, &edge{Parent: inode}); err != nil {
return err
}
for _, row := range rows {
if row.Type == TypeDirectory {
n.Nlink++
}
}
return mustInsert(s, n)
}, inode))
}

func (m *dbMeta) GetXattr(ctx Context, inode Ino, name string, vbuff *[]byte) syscall.Errno {
defer m.timeit(time.Now())
inode = m.checkRoot(inode)
Expand Down
24 changes: 24 additions & 0 deletions pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,30 @@ func (m *kvMeta) ListSlices(ctx Context, slices map[Ino][]Slice, delete bool, sh
return 0
}

func (m *kvMeta) RepairInode(ctx Context, parent, inode Ino) syscall.Errno {
now := time.Now().Unix()
attr := &Attr{
Typ: TypeDirectory,
Atime: now,
Mtime: now,
Ctime: now,
Length: 4 << 10,
Parent: parent,
}
return errno(m.txn(func(tx kvTxn) error {
attr.Nlink = 2
_ = tx.scanValues(m.entryKey(inode, ""), 0, func(k, v []byte) bool {
typ, _ := m.parseEntry(v)
if typ == TypeDirectory {
attr.Nlink++
}
return false
})
tx.set(m.inodeKey(inode), m.marshal(attr))
return nil
}, inode))
}

func (m *kvMeta) GetXattr(ctx Context, inode Ino, name string, vbuff *[]byte) syscall.Errno {
defer m.timeit(time.Now())
inode = m.checkRoot(inode)
Expand Down