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

+78 -6
View File
@@ -125,9 +125,13 @@ type attrsTestFS struct {
func (f *attrsTestFS) Stat(string) (storage.Node, error) { return f.n, nil }
// SetAttr, SetXattr and RemoveXattr follow the change-time rules measured on
// Ceph 21.2.0: SetAttr sets the ctime to max(Ctime, Mtime), and an xattr
// change resets it to the mtime.
func (f *attrsTestFS) SetAttr(_ string, n storage.Node) error {
f.setCalls++
f.n = n
f.n.Ctime = wantedCtime(n)
return nil
}
@@ -150,11 +154,13 @@ func (f *attrsTestFS) GetXattr(_, key string) ([]byte, error) {
func (f *attrsTestFS) SetXattr(_, key string, value []byte) error {
f.attrs[key] = bytes.Clone(value)
f.n.Ctime = f.n.Mtime
return nil
}
func (f *attrsTestFS) RemoveXattr(_, key string) error {
delete(f.attrs, key)
f.n.Ctime = f.n.Mtime
return nil
}
@@ -212,19 +218,29 @@ func TestNativeCancellationAndPaths(t *testing.T) {
}
func TestNativeMetadataOnlyWritesChangedAttributes(t *testing.T) {
n := storage.Node{Mode: storage.Regular | 0640, UID: 12, GID: 34, Mtime: time.Unix(123, 0)}
n := storage.Node{Mode: storage.Regular | 0640, UID: 12, GID: 34, Mtime: time.Unix(123, 0), Ctime: time.Unix(124, 0)}
src := &attrsTestFS{n: n, attrs: map[string][]byte{"user.new": []byte("value")}}
dst := &attrsTestFS{n: n, attrs: map[string][]byte{"user.old": []byte("remove")}}
dst := &attrsTestFS{n: n, attrs: map[string][]byte{"user.new": []byte("value")}}
if err := syncNativeMetadata(src, dst, "/source", "/target", n, false); err != nil {
t.Fatal(err)
}
if dst.setCalls != 0 {
t.Fatal("unchanged ownership, mode and mtime were rewritten")
t.Fatal("unchanged metadata was rewritten")
}
// An xattr change resets the ctime, so SetAttr must follow it.
dst.attrs = map[string][]byte{"user.old": []byte("remove")}
if err := syncNativeMetadata(src, dst, "/source", "/target", n, false); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(src.attrs, dst.attrs) {
t.Fatal("xattrs must still be synchronized")
t.Fatal("xattrs must be synchronized")
}
if dst.setCalls != 1 || !dst.n.Ctime.Equal(n.Ctime) {
t.Fatalf("ctime not restored after xattr change: calls=%d ctime=%v", dst.setCalls, dst.n.Ctime)
}
dst.n.UID++
@@ -232,7 +248,7 @@ func TestNativeMetadataOnlyWritesChangedAttributes(t *testing.T) {
t.Fatal(err)
}
if dst.setCalls != 1 || dst.n.UID != n.UID {
if dst.setCalls != 2 || dst.n.UID != n.UID {
t.Fatal("changed ownership was not restored")
}
}
@@ -245,7 +261,8 @@ func TestNativeVerifyCommittedRound(t *testing.T) {
}
defer index.close()
n := storage.Node{Mode: storage.Regular | 0640, Inode: 7, Version: 1, Nlink: 1, Mtime: time.Unix(123, 0)}
n := storage.Node{Mode: storage.Regular | 0640, Inode: 7, Version: 1, Nlink: 1,
Mtime: time.Unix(123, 0), Ctime: time.Unix(124, 0)}
src := &attrsTestFS{n: n, attrs: map[string][]byte{}}
dst := &attrsTestFS{n: n, attrs: map[string][]byte{}}
if err := index.put("groups", nativeInodeKey(n.Inode), []byte(".")); err != nil {
@@ -305,3 +322,58 @@ func TestNativeMetadataPreservesAccessAndBirthTime(t *testing.T) {
})
}
}
func TestNativeMetadataChangeTime(t *testing.T) {
n := storage.Node{Mode: storage.Regular | 0640, Mtime: time.Unix(400, 0), Ctime: time.Unix(500, 5)}
for _, tc := range []struct {
name string
targetCtime time.Time
sourceAttrs map[string][]byte
}{
{"older target is raised", time.Unix(1, 0), nil},
{"newer target is lowered", time.Unix(900, 0), nil},
{"set after xattr changes", n.Ctime, map[string][]byte{"user.new": []byte("v")}},
} {
t.Run(tc.name, func(t *testing.T) {
target := n
target.Ctime = tc.targetCtime
attrs := tc.sourceAttrs
if attrs == nil {
attrs = map[string][]byte{}
}
src := &attrsTestFS{n: n, attrs: attrs}
dst := &attrsTestFS{n: target, attrs: map[string][]byte{}}
if err := syncNativeMetadata(src, dst, "/src", "/dst", n, true); err == nil {
t.Fatal("mismatch not detected")
}
if err := syncNativeMetadata(src, dst, "/src", "/dst", n, false); err != nil {
t.Fatal(err)
}
if !dst.n.Ctime.Equal(n.Ctime) {
t.Fatalf("ctime %v; want %v", dst.n.Ctime, n.Ctime)
}
if err := syncNativeMetadata(src, dst, "/src", "/dst", n, true); err != nil {
t.Fatal(err)
}
})
}
t.Run("ctime below mtime", func(t *testing.T) {
future := n
future.Mtime = time.Unix(800, 0) // mtime set into the future
src := &attrsTestFS{n: future, attrs: map[string][]byte{}}
dst := &attrsTestFS{n: n, attrs: map[string][]byte{}}
if err := syncNativeMetadata(src, dst, "/src", "/dst", future, false); err != nil {
t.Fatal(err)
}
if err := syncNativeMetadata(src, dst, "/src", "/dst", future, true); err != nil {
t.Fatalf("closest reachable ctime rejected: %v", err)
}
})
}