Files
CephfsMig/internal/migration/native_helpers.go
T
Sebastian UnterschützandClaude Opus 5.5 6dcb7b676f fix!: remove skipping of unchanged subtrees
Intermediate rounds skipped a directory when its inode version and its
ceph.dir.rctime matched the previous round. In the 10,000-file lab test
a round snapshot taken seconds after changes skipped tree/ with about
250 changed files in its subdirectories: the inode version of tree/
changes only with its own entries, and Ceph had not yet propagated the
rctime of the grandchildren. The target snapshot of that round was
incomplete, and its verification skipped the same subtrees; only
finalize closed the gap.

Every round now scans all metadata again. Base index entries that the
old version marked carried are never trusted, so such files are copied
in full in the next round.

The smallfiles verification now checks only snapshots that sync did not
remove.

BREAKING CHANGE: the configuration field "full_scan" and the transfer
statistic "skipped_subtrees" are removed; a configuration with
"full_scan" is rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 22:17:47 +01:00

401 lines
8.7 KiB
Go

package migration
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"path"
"strings"
"syscall"
"time"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
)
// quotaKeys are the directory quota xattrs that the migration copies.
var quotaKeys = []string{"ceph.quota.max_bytes", "ceph.quota.max_files"}
func walkNative(ctx context.Context, fs storage.FS, root string, visit func(string, storage.Node, int) error) error {
var walk func(string, storage.Node, int) error
walk = func(p string, n storage.Node, depth int) error {
if err := ctx.Err(); err != nil {
return err
}
if depth > 128 {
return errors.New("native directory depth exceeds 128")
}
if err := visit(p, n, depth); err != nil {
return err
}
if n.Kind() != storage.Directory {
return nil
}
dir, err := fs.OpenDir(p)
if err != nil {
return err
}
defer dir.Close()
for {
e, err := dir.Next()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
if e == nil || e.Name == "" || strings.ContainsAny(e.Name, "/\x00") || e.Name == "." || e.Name == ".." || e.Name == ".snap" {
return errors.New("invalid native directory entry")
}
if err = walk(path.Join(p, e.Name), e.Node, depth+1); err != nil {
return err
}
}
}
n, err := fs.Stat(root)
if err != nil {
return err
}
return walk(root, n, 0)
}
func removeNative(ctx context.Context, fs storage.FS, p string) error {
if err := ctx.Err(); err != nil {
return err
}
n, err := fs.Stat(p)
if errors.Is(err, syscall.ENOENT) {
return nil
}
if err != nil {
return err
}
if n.Kind() != storage.Directory {
return fs.Unlink(p)
}
d, err := fs.OpenDir(p)
if err != nil {
return err
}
// Re-open after each bounded batch, so deleting while iterating does not skip
// entries on directory streams whose offsets change after removal.
for {
var names []string
for len(names) < 128 {
e, err := d.Next()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
d.Close()
return err
}
names = append(names, e.Name)
}
if err = d.Close(); err != nil {
return err
}
if len(names) == 0 {
break
}
for _, s := range names {
if err = removeNative(ctx, fs, path.Join(p, s)); err != nil {
return err
}
}
d, err = fs.OpenDir(p)
if err != nil {
return err
}
}
return fs.Rmdir(p)
}
func nativeQuota(fs storage.FS, p, key string) ([]byte, error) {
b, err := fs.GetXattr(p, key)
if errors.Is(err, syscall.ENODATA) {
return []byte("0"), nil
}
return b, err
}
func clearNativeQuotas(ctx context.Context, fs storage.FS, root string) error {
return walkNative(ctx, fs, root, func(p string, n storage.Node, _ int) error {
if n.Kind() != storage.Directory {
return nil
}
for _, key := range quotaKeys {
b, err := nativeQuota(fs, p, key)
if err != nil {
return err
}
if string(b) != "0" {
if err = fs.SetXattr(p, key, []byte("0")); err != nil {
return err
}
}
}
return nil
})
}
func portableXattr(s string) bool {
return !strings.HasPrefix(s, "ceph.") && (!strings.HasPrefix(s, "system.") || s == "system.posix_acl_access" || s == "system.posix_acl_default")
}
func nativeAttrs(fs storage.FS, p string) (map[string][]byte, error) {
names, err := fs.ListXattr(p)
if err != nil {
return nil, err
}
attrs := make(map[string][]byte, len(names))
for _, key := range names {
if !portableXattr(key) {
continue
}
value, err := fs.GetXattr(p, key)
if err != nil {
return nil, err
}
attrs[key] = value
}
return attrs, nil
}
// syncNativeMetadata makes the metadata of the target path to equal the source
// node n, or with verify set only checks it.
func syncNativeMetadata(src, dst storage.FS, from, to string, n storage.Node, verify bool) error {
target, err := dst.Stat(to)
if err != nil {
return err
}
return syncNativeMetadataKnown(src, dst, from, to, n, target, verify)
}
// wantedCtime is the change time the target can reach: the MDS keeps the
// ctime at or above the mtime.
func wantedCtime(n storage.Node) time.Time {
if n.Mtime.After(n.Ctime) {
return n.Mtime
}
return n.Ctime
}
// syncNativeMetadataKnown reuses a no-follow stat just read by the caller.
// No other writer is permitted in the target while a migration owns it.
//
// Extended attributes and quotas are written first and SetAttr last, because
// SetAttr sets the ctime and every later change would reset it.
func syncNativeMetadataKnown(src, dst storage.FS, from, to string, n, target storage.Node, verify bool) error {
if n.Kind() != target.Kind() {
return fmt.Errorf("native type mismatch at %s", to)
}
diff := metadataDiff(n, target)
different := diff != ""
if different && verify {
return fmt.Errorf("native metadata mismatch at %s:%s", to, diff)
}
changed, err := syncNativeXattrs(src, dst, from, to, n, verify)
if err != nil || verify || !(different || changed) {
return err
}
if err := dst.SetAttr(to, n); err != nil {
return fmt.Errorf("native setattr %s: %w", to, err)
}
return nil
}
// restoreNativeMetadata copies xattrs and quotas and then always calls
// SetAttr. Use it where the target was changed after its last SetAttr, since
// the cached stat of this client may not show that.
func restoreNativeMetadata(src, dst storage.FS, from, to string, n storage.Node) error {
if _, err := syncNativeXattrs(src, dst, from, to, n, false); err != nil {
return err
}
if err := dst.SetAttr(to, n); err != nil {
return fmt.Errorf("native setattr %s: %w", to, err)
}
return nil
}
// metadataDiff lists the attributes in which target differs from the source
// node n, or returns "" if none does.
func metadataDiff(n, target storage.Node) string {
var b strings.Builder
if n.Mode != target.Mode {
fmt.Fprintf(&b, " mode %o != %o", n.Mode, target.Mode)
}
if n.UID != target.UID || n.GID != target.GID {
fmt.Fprintf(&b, " owner %d:%d != %d:%d", n.UID, n.GID, target.UID, target.GID)
}
for _, t := range []struct {
name string
want, actual time.Time
}{
{"atime", n.Atime, target.Atime},
{"mtime", n.Mtime, target.Mtime},
{"btime", n.Btime, target.Btime},
{"ctime", wantedCtime(n), target.Ctime},
} {
if !t.want.Equal(t.actual) {
fmt.Fprintf(&b, " %s %s != %s", t.name, t.want.UTC().Format(time.RFC3339Nano), t.actual.UTC().Format(time.RFC3339Nano))
}
}
return b.String()
}
// syncNativeXattrs copies portable xattrs and, for directories, quotas. It
// reports whether it changed anything.
func syncNativeXattrs(src, dst storage.FS, from, to string, n storage.Node, verify bool) (bool, error) {
attrs, err := nativeAttrs(src, from)
if err != nil {
return false, err
}
old, err := nativeAttrs(dst, to)
if err != nil {
return false, err
}
changed := false
for k, v := range old {
value, exists := attrs[k]
if verify {
if !exists || !bytes.Equal(value, v) {
return false, fmt.Errorf("native xattr mismatch at %s: %s", to, k)
}
} else if !exists {
if err = dst.RemoveXattr(to, k); err != nil {
return false, err
}
changed = true
}
}
for k, v := range attrs {
if verify {
if actual, ok := old[k]; !ok || !bytes.Equal(v, actual) {
return false, fmt.Errorf("native xattr mismatch at %s: %s", to, k)
}
} else if actual, exists := old[k]; !exists || !bytes.Equal(actual, v) {
if err = dst.SetXattr(to, k, v); err != nil {
return false, fmt.Errorf("native setxattr %s: %w", k, err)
}
changed = true
}
}
if n.Kind() != storage.Directory {
return changed, nil
}
for _, key := range quotaKeys {
value, err := nativeQuota(src, from, key)
if err != nil {
return false, err
}
actual, err := nativeQuota(dst, to, key)
if err != nil {
return false, err
}
if bytes.Equal(value, actual) {
continue
}
if verify {
return false, fmt.Errorf("native quota mismatch at %s: %s", to, key)
}
if err = dst.SetXattr(to, key, value); err != nil {
return false, err
}
changed = true
}
return changed, nil
}
func ensureNativeDirectory(ctx context.Context, fs storage.FS, p string) error {
st, err := fs.Stat(p)
if err == nil && st.Kind() == storage.Directory {
return nil
}
if err != nil && !errors.Is(err, syscall.ENOENT) {
return err
}
if err == nil {
if err = removeNative(ctx, fs, p); err != nil {
return err
}
}
return fs.Mkdir(p, 0700)
}
func validateNativeDirectory(fs storage.FS, p string) error {
current := "/"
for _, part := range strings.Split(strings.TrimPrefix(p, "/"), "/") {
if part == "" || part == "." || part == ".." {
return fmt.Errorf("invalid native path %q", p)
}
current = path.Join(current, part)
n, err := fs.Stat(current)
if err != nil {
return err
}
if n.Kind() != storage.Directory {
return fmt.Errorf("native path contains non-directory %s", current)
}
}
return nil
}