feat(native): preserve access and birth time of every entry

Read atime and btime with statx and set them together with owner,
mode and mtime. Verification compares both, so a published snapshot
must match the source in all three times. The integration fixture
compares the times through libcephfs, since ceph-fuse does not expose
the birth time.

The ctime cannot be preserved: Ceph 21.2.0 accepts CEPH_SETATTR_CTIME
but stores the time of the call. The rsync backend still preserves
only the mtime.

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 18:48:25 +01:00
1 parent a6fda8f8d3
commit 02ebc08c88
7 files changed
+137 -19

No files matched your search

+8 -6
View File
@@ -1,8 +1,8 @@
# CephMig: CephFS-Subvolumes zwischen Ceph-Clustern migrieren # CephMig: CephFS-Subvolumes zwischen Ceph-Clustern migrieren
Ein Go-Werkzeug, das ein CephFS-Subvolume samt Snapshots, POSIX-ACLs, xattrs Ein Go-Werkzeug, das ein CephFS-Subvolume samt Snapshots, POSIX-ACLs, xattrs,
und Quotas von einem Ceph-Cluster in einen anderen kopiert, während die Quotas und Zeitstempeln von einem Ceph-Cluster in einen anderen kopiert,
Anwendung weiterläuft. während die Anwendung weiterläuft.
## Beschreibung ## Beschreibung
@@ -27,9 +27,11 @@ Es gibt zwei Backends:
**Status:** Prototyp. Die Funktionen sind im lokalen Labor geprüft. **Status:** Prototyp. Die Funktionen sind im lokalen Labor geprüft.
Lastnachweise für die Zielgröße und die produktive Umschaltung fehlen noch. Lastnachweise für die Zielgröße und die produktive Umschaltung fehlen noch.
> **Dokumentierte Abweichung:** Native Snapshot-IDs und Ceph-Geburtszeitpunkte > **Dokumentierte Abweichungen:** Native Snapshot-IDs und Snapshot-Erstellungszeiten
> werden am Ziel neu vergeben. Die ursprünglichen Erstellungszeiten stehen im > werden am Ziel neu vergeben, ebenso die ctime jeder Datei. mtime, atime und
> Journal. Details: [docs/migration.md](docs/migration.md). > Erstellungszeit (btime) der Dateien übernimmt das native Backend von der
> Quelle. Die ursprünglichen Snapshot-Erstellungszeiten stehen im Journal.
> Details: [docs/migration.md](docs/migration.md).
## Installation ## Installation
+46
View File
@@ -12,6 +12,8 @@ import sys
import time import time
import uuid import uuid
import cephfs
def ceph(cluster, *args): def ceph(cluster, *args):
return subprocess.check_output([ return subprocess.check_output([
@@ -122,6 +124,48 @@ def compare(src, dst):
visit(src, dst) visit(src, dst)
def mount(cluster):
fs = cephfs.LibCephFS(conffile=f"/clusters/{cluster}/ceph.conf",
conf={"keyring": f"/clusters/{cluster}/ceph.client.admin.keyring"})
fs.mount(filesystem_name=b"cephfs")
return fs
def compare_times(src, dst, atime):
"""Compare times through libcephfs; FUSE does not expose the birth time.
The access time is only compared for immutable snapshots, because the FUSE
reads in compare() update it in the live trees."""
keys = ("atime", "mtime", "btime") if atime else ("mtime", "btime")
mask = cephfs.CEPH_STATX_BASIC_STATS | cephfs.CEPH_STATX_BTIME
fa, fb = mount("source"), mount("target")
def ceph_path(p, cluster):
prefix = f"/mnt/{cluster}-fs"
assert p.startswith(prefix + "/"), p
return p[len(prefix):].encode()
def visit(a, b):
x = fa.statx(a, mask, cephfs.AT_SYMLINK_NOFOLLOW)
y = fb.statx(b, mask, cephfs.AT_SYMLINK_NOFOLLOW)
assert {k: x[k] for k in keys} == {k: y[k] for k in keys}, (a, x, y)
if not stat.S_ISDIR(x["mode"]):
return 1
d = fa.opendir(a)
names = []
while entry := fa.readdir(d):
if entry.d_name not in (b".", b"..", b".snap"):
names.append(entry.d_name)
fa.closedir(d)
return 1 + sum(visit(a + b"/" + n, b + b"/" + n) for n in names)
try:
return visit(ceph_path(src, "source"), ceph_path(dst, "target"))
finally:
fa.unmount()
fb.unmount()
action = sys.argv[1] action = sys.argv[1]
if action == "setup": if action == "setup":
name = "native-itest-" + str(uuid.uuid4()) name = "native-itest-" + str(uuid.uuid4())
@@ -201,12 +245,14 @@ elif action == "mutate":
elif action in ("verify", "final"): elif action in ("verify", "final"):
roots = json.load(open(fixture)) roots = json.load(open(fixture))
compare(roots["source"], roots["target"]) compare(roots["source"], roots["target"])
compare_times(roots["source"], roots["target"], atime=False)
s = json.load(open(state)) s = json.load(open(state))
for snap in s["snapshots"]: for snap in s["snapshots"]:
assert snap["done"], snap assert snap["done"], snap
a, b = [f"/mnt/{c}-fs" + ceph(c, "fs", "subvolume", "snapshot", "getpath", a, b = [f"/mnt/{c}-fs" + ceph(c, "fs", "subvolume", "snapshot", "getpath",
"cephfs", name, snap["name"], "migration") for c in ("source", "target")] "cephfs", name, snap["name"], "migration") for c in ("source", "target")]
compare(a, b) compare(a, b)
assert compare_times(a, b, atime=True) > 10
assert snapshot("source", "metadata", "ls", "cephfs", name, snap["name"]) == snapshot( assert snapshot("source", "metadata", "ls", "cephfs", name, snap["name"]) == snapshot(
"target", "metadata", "ls", "cephfs", name, snap["name"]) "target", "metadata", "ls", "cephfs", name, snap["name"])
h2 = next(x for x in s["snapshots"] if x["name"] == "history-2")["transfer"] h2 = next(x for x in s["snapshots"] if x["name"] == "history-2")["transfer"]
+16 -1
View File
@@ -91,7 +91,22 @@ Rückmigration nach Schreibzugriffen am Ziel sind nicht automatisiert.
## Übertragene Daten ## Übertragene Daten
- Snapshotnamen und Inhalte; bestehende Snapshots werden chronologisch übertragen. - Snapshotnamen und Inhalte; bestehende Snapshots werden chronologisch übertragen.
- Numerische UID/GID, Modus, mtime, POSIX-ACLs einschließlich Default-ACLs. - Numerische UID/GID, Modus, POSIX-ACLs einschließlich Default-ACLs.
- Zeitstempel je Datei, Verzeichnis und Symlink:
| Zeitstempel | native | rsync |
| --- | --- | --- |
| mtime (letzte Änderung) | ja | ja |
| btime (Erstellung) | ja | nein |
| atime (letzter Zugriff) | ja | nein |
| ctime (letzte Statusänderung) | nein | nein |
Die ctime lässt sich mit keiner Dateisystemschnittstelle setzen. Ceph 21.2.0
meldet `CEPH_SETATTR_CTIME` als erfolgreich, speichert aber den Zeitpunkt
des Aufrufs (im Labor geprüft). Die atime im Ziel ändert sich danach durch
normale Lesezugriffe der Anwendung. rsync kann unter Linux keine btime
übertragen; atime wird nicht übernommen, weil die Prüfsummenprüfung über
den Mount die atime im Ziel verändern kann.
- Benutzer-xattrs und weitere portable xattrs; einschließlich leerer Werte und Löschungen. - Benutzer-xattrs und weitere portable xattrs; einschließlich leerer Werte und Löschungen.
- Hardlinks innerhalb des Subvolumes, Symlinks und FIFOs. - Hardlinks innerhalb des Subvolumes, Symlinks und FIFOs.
- Sparse-Dateiinhalte; der native Pfad überspringt Nullbereiche bzw. verwendet Hole-Punching. - Sparse-Dateiinhalte; der native Pfad überspringt Nullbereiche bzw. verwendet Hole-Punching.
+28 -9
View File
@@ -16,20 +16,28 @@ package cephfsnative
static int cm_stat(struct ceph_mount_info *m, const char *p, struct ceph_statx *s, int flags) static int cm_stat(struct ceph_mount_info *m, const char *p, struct ceph_statx *s, int flags)
{ {
return ceph_statx(m, p, s, CEPH_STATX_BASIC_STATS | CEPH_STATX_VERSION, flags); return ceph_statx(m, p, s, CEPH_STATX_BASIC_STATS | CEPH_STATX_VERSION | CEPH_STATX_BTIME, flags);
} }
// cm_attr sets owner, mode and the access, modification and birth times.
// t holds seconds and nanoseconds for atime, mtime and btime in this order.
// The change time cannot be set: the MDS always records the time of the call.
static int cm_attr(struct ceph_mount_info *m, const char *p, unsigned mode, unsigned uid, static int cm_attr(struct ceph_mount_info *m, const char *p, unsigned mode, unsigned uid,
unsigned gid, long sec, long nsec) unsigned gid, const long *t)
{ {
struct ceph_statx s = {0}; struct ceph_statx s = {0};
s.stx_mode = mode; s.stx_mode = mode;
s.stx_uid = uid; s.stx_uid = uid;
s.stx_gid = gid; s.stx_gid = gid;
s.stx_mtime.tv_sec = sec; s.stx_atime.tv_sec = t[0];
s.stx_mtime.tv_nsec = nsec; s.stx_atime.tv_nsec = t[1];
s.stx_mtime.tv_sec = t[2];
s.stx_mtime.tv_nsec = t[3];
s.stx_btime.tv_sec = t[4];
s.stx_btime.tv_nsec = t[5];
int mask = CEPH_SETATTR_UID | CEPH_SETATTR_GID | CEPH_SETATTR_MTIME; int mask = CEPH_SETATTR_UID | CEPH_SETATTR_GID | CEPH_SETATTR_ATIME |
CEPH_SETATTR_MTIME | CEPH_SETATTR_BTIME;
if ((mode & S_IFMT) != S_IFLNK) if ((mode & S_IFMT) != S_IFLNK)
mask |= CEPH_SETATTR_MODE; mask |= CEPH_SETATTR_MODE;
return ceph_setattrx(m, p, &s, mask, AT_SYMLINK_NOFOLLOW); return ceph_setattrx(m, p, &s, mask, AT_SYMLINK_NOFOLLOW);
@@ -159,7 +167,11 @@ func node(s C.struct_ceph_statx) storage.Node {
return storage.Node{Mode: uint32(s.stx_mode), Inode: uint64(s.stx_ino), return storage.Node{Mode: uint32(s.stx_mode), Inode: uint64(s.stx_ino),
Version: uint64(s.stx_version), Nlink: uint64(s.stx_nlink), Size: uint64(s.stx_size), Version: uint64(s.stx_version), Nlink: uint64(s.stx_nlink), Size: uint64(s.stx_size),
Rdev: uint64(s.stx_rdev), UID: uint32(s.stx_uid), GID: uint32(s.stx_gid), Rdev: uint64(s.stx_rdev), UID: uint32(s.stx_uid), GID: uint32(s.stx_gid),
Mtime: time.Unix(int64(s.stx_mtime.tv_sec), int64(s.stx_mtime.tv_nsec))} Atime: timespec(s.stx_atime), Mtime: timespec(s.stx_mtime), Btime: timespec(s.stx_btime)}
}
func timespec(t C.struct_timespec) time.Time {
return time.Unix(int64(t.tv_sec), int64(t.tv_nsec))
} }
// Stat returns the attributes of path without following a final symlink. // Stat returns the attributes of path without following a final symlink.
@@ -282,7 +294,7 @@ func (d *dir) Next() (*storage.Entry, error) {
var entry C.struct_dirent var entry C.struct_dirent
var s C.struct_ceph_statx var s C.struct_ceph_statx
ret := C.ceph_readdirplus_r(d.client.mount, d.handle, &entry, &s, ret := C.ceph_readdirplus_r(d.client.mount, d.handle, &entry, &s,
C.CEPH_STATX_BASIC_STATS|C.CEPH_STATX_VERSION, C.uint(d.flags), nil) C.CEPH_STATX_BASIC_STATS|C.CEPH_STATX_VERSION|C.CEPH_STATX_BTIME, C.uint(d.flags), nil)
if err := errno(ret); err != nil { if err := errno(ret); err != nil {
return nil, err return nil, err
@@ -379,11 +391,18 @@ func (c *Client) Readlink(path string) (string, error) {
return "", errors.New("symlink target exceeds 1 MiB") return "", errors.New("symlink target exceeds 1 MiB")
} }
// SetAttr sets numeric owner, mode and mtime without following symlinks. // SetAttr sets numeric owner, mode, atime, mtime and btime without following
// symlinks. The ctime cannot be set; Ceph records the time of the call.
func (c *Client) SetAttr(path string, n storage.Node) error { func (c *Client) SetAttr(path string, n storage.Node) error {
p, f := str(path) p, f := str(path)
defer f() defer f()
return errno(C.cm_attr(c.mount, p, C.uint(n.Mode), C.uint(n.UID), C.uint(n.GID), C.long(n.Mtime.Unix()), C.long(n.Mtime.Nanosecond()))) times := [6]C.long{
C.long(n.Atime.Unix()), C.long(n.Atime.Nanosecond()),
C.long(n.Mtime.Unix()), C.long(n.Mtime.Nanosecond()),
C.long(n.Btime.Unix()), C.long(n.Btime.Nanosecond()),
}
return errno(C.cm_attr(c.mount, p, C.uint(n.Mode), C.uint(n.UID), C.uint(n.GID), &times[0]))
} }
// GetXattr returns the value of an extended attribute. // GetXattr returns the value of an extended attribute.
+2 -1
View File
@@ -202,7 +202,8 @@ func syncNativeMetadataKnown(src, dst storage.FS, from, to string, n, target sto
return fmt.Errorf("native type mismatch at %s", to) return fmt.Errorf("native type mismatch at %s", to)
} }
different := n.Mode != target.Mode || n.UID != target.UID || n.GID != target.GID || !n.Mtime.Equal(target.Mtime) 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 different {
if verify { if verify {
return fmt.Errorf("native metadata mismatch at %s", to) return fmt.Errorf("native metadata mismatch at %s", to)
+32
View File
@@ -273,3 +273,35 @@ func TestNativeVerifyCommittedRound(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestNativeMetadataPreservesAccessAndBirthTime(t *testing.T) {
n := storage.Node{Mode: storage.Regular | 0640, UID: 1, GID: 2,
Atime: time.Unix(100, 1), Mtime: time.Unix(200, 2), Btime: time.Unix(50, 3)}
for _, tc := range []struct {
name string
change func(*storage.Node)
}{
{"atime", func(x *storage.Node) { x.Atime = time.Unix(999, 0) }},
{"btime", func(x *storage.Node) { x.Btime = time.Unix(999, 0) }},
} {
t.Run(tc.name, func(t *testing.T) {
target := n
tc.change(&target)
src := &attrsTestFS{n: n, attrs: map[string][]byte{}}
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.setCalls != 1 || !dst.n.Atime.Equal(n.Atime) || !dst.n.Btime.Equal(n.Btime) {
t.Fatalf("times not restored: %+v", dst.n)
}
})
}
}
+5 -2
View File
@@ -11,7 +11,10 @@ type Node struct {
Mode uint32 Mode uint32
Inode, Version, Nlink, Size, Rdev uint64 Inode, Version, Nlink, Size, Rdev uint64
UID, GID uint32 UID, GID uint32
Mtime time.Time
// Access, modification and birth time. The change time is not part of
// Node because no filesystem interface allows setting it.
Atime, Mtime, Btime time.Time
} }
// File type bits of Node.Mode, as in stat(2). // File type bits of Node.Mode, as in stat(2).
@@ -80,7 +83,7 @@ type FS interface {
Symlink(string, string) error Symlink(string, string) error
Readlink(string) (string, error) Readlink(string) (string, error)
Mknod(string, uint32, uint64) error Mknod(string, uint32, uint64) error
SetAttr(string, Node) error // Numeric owner, mode and mtime; no symlink following. SetAttr(string, Node) error // Numeric owner, mode, atime, mtime and btime; no symlink following.
ListXattr(string) ([]string, error) ListXattr(string) ([]string, error)
GetXattr(string, string) ([]byte, error) GetXattr(string, string) ([]byte, error)
SetXattr(string, string, []byte) error SetXattr(string, string, []byte) error