feat(native): preserve ctime of files, directories and symlinks

The MDS takes an inode's ctime from the time stamp the client sends with
each request. The native backend overrides clock_gettime per thread and
per libcephfs call: modifying calls run at 1970-01-01T00:00:01Z, and the
last change to each entry creates and removes the xattr
user.cephmig.ctime stamped with the source ctime. Link, unlink, rename
and xattr requests set ctime = max(stamp, mtime), also backwards.

- write xattrs and quotas before SetAttr, which must come last
- always restamp directories after their children and hardlinks after
  linking or pruning, since the writer's cache may be stale
- verify through a separate, freshly mounted target client
- name the differing attributes in metadata mismatch errors
- compare ctime of every entry, the root included, in the lab fixture

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-12-03 19:33:19 +01:00
1 parent 02ebc08c88
commit cc2dff46a0
11 files changed
+491 -96

No files matched your search

+119 -41
View File
@@ -9,6 +9,7 @@ import (
"path"
"strings"
"syscall"
"time"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
)
@@ -186,6 +187,8 @@ func nativeAttrs(fs storage.FS, p string) (map[string][]byte, error) {
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 {
@@ -195,87 +198,162 @@ func syncNativeMetadata(src, dst storage.FS, from, to string, n storage.Node, ve
return syncNativeMetadataKnown(src, dst, from, to, n, target, verify)
}
// Reuse a no-follow stat just read by the caller. No other writer is permitted
// in the target while a migration owns it.
// 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)
}
different := n.Mode != target.Mode || n.UID != target.UID || n.GID != target.GID ||
!n.Atime.Equal(target.Atime) || !n.Mtime.Equal(target.Mtime) || !n.Btime.Equal(target.Btime)
if different {
if verify {
return fmt.Errorf("native metadata mismatch at %s", to)
}
diff := metadataDiff(n, target)
different := diff != ""
if different && verify {
return fmt.Errorf("native metadata mismatch at %s:%s", to, diff)
}
if err := dst.SetAttr(to, n); err != nil {
return fmt.Errorf("native setattr %s: %w", to, err)
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 err
return false, err
}
old, err := nativeAttrs(dst, to)
if err != nil {
return err
return false, err
}
changed := false
for k, v := range old {
value, exists := attrs[k]
if verify {
if !exists || !bytes.Equal(value, v) {
return fmt.Errorf("native xattr mismatch at %s: %s", to, k)
return false, fmt.Errorf("native xattr mismatch at %s: %s", to, k)
}
} else if !exists {
if err = dst.RemoveXattr(to, k); err != nil {
return err
return false, err
}
changed = true
}
}
for k, v := range attrs {
if verify {
if actual, ok := old[k]; !ok || !bytes.Equal(v, actual) {
return fmt.Errorf("native xattr mismatch at %s: %s", to, k)
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 fmt.Errorf("native setxattr %s: %w", k, err)
return false, fmt.Errorf("native setxattr %s: %w", k, err)
}
changed = true
}
}
if n.Kind() == storage.Directory {
for _, key := range quotaKeys {
value, err := nativeQuota(src, from, key)
if err != nil {
return err
}
actual, err := nativeQuota(dst, to, key)
if err != nil {
return err
}
if bytes.Equal(value, actual) {
continue
}
if verify {
return fmt.Errorf("native quota mismatch at %s: %s", to, key)
}
if err = dst.SetXattr(to, key, value); err != nil {
return err
}
}
if n.Kind() != storage.Directory {
return changed, nil
}
return 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 {