diff --git a/README.md b/README.md index 9653d67..7410a90 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # CephMig: CephFS-Subvolumes zwischen Ceph-Clustern migrieren -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 -Anwendung weiterläuft. +Ein Go-Werkzeug, das ein CephFS-Subvolume samt Snapshots, POSIX-ACLs, xattrs, +Quotas und Zeitstempeln von einem Ceph-Cluster in einen anderen kopiert, +während die Anwendung weiterläuft. ## Beschreibung @@ -27,9 +27,11 @@ Es gibt zwei Backends: **Status:** Prototyp. Die Funktionen sind im lokalen Labor geprüft. Lastnachweise für die Zielgröße und die produktive Umschaltung fehlen noch. -> **Dokumentierte Abweichung:** Native Snapshot-IDs und Ceph-Geburtszeitpunkte -> werden am Ziel neu vergeben. Die ursprünglichen Erstellungszeiten stehen im -> Journal. Details: [docs/migration.md](docs/migration.md). +> **Dokumentierte Abweichungen:** Native Snapshot-IDs und Snapshot-Erstellungszeiten +> werden am Ziel neu vergeben, ebenso die ctime jeder Datei. mtime, atime und +> 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 diff --git a/dev/native-fixture.py b/dev/native-fixture.py index a6fa6fe..5825591 100644 --- a/dev/native-fixture.py +++ b/dev/native-fixture.py @@ -12,6 +12,8 @@ import sys import time import uuid +import cephfs + def ceph(cluster, *args): return subprocess.check_output([ @@ -122,6 +124,48 @@ def compare(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] if action == "setup": name = "native-itest-" + str(uuid.uuid4()) @@ -201,12 +245,14 @@ elif action == "mutate": elif action in ("verify", "final"): roots = json.load(open(fixture)) compare(roots["source"], roots["target"]) + compare_times(roots["source"], roots["target"], atime=False) s = json.load(open(state)) for snap in s["snapshots"]: assert snap["done"], snap a, b = [f"/mnt/{c}-fs" + ceph(c, "fs", "subvolume", "snapshot", "getpath", "cephfs", name, snap["name"], "migration") for c in ("source", "target")] compare(a, b) + assert compare_times(a, b, atime=True) > 10 assert snapshot("source", "metadata", "ls", "cephfs", name, snap["name"]) == snapshot( "target", "metadata", "ls", "cephfs", name, snap["name"]) h2 = next(x for x in s["snapshots"] if x["name"] == "history-2")["transfer"] diff --git a/docs/migration.md b/docs/migration.md index 4ad5da7..a1813a4 100644 --- a/docs/migration.md +++ b/docs/migration.md @@ -91,7 +91,22 @@ Rückmigration nach Schreibzugriffen am Ziel sind nicht automatisiert. ## Übertragene Daten - 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. - Hardlinks innerhalb des Subvolumes, Symlinks und FIFOs. - Sparse-Dateiinhalte; der native Pfad überspringt Nullbereiche bzw. verwendet Hole-Punching. diff --git a/internal/cephfsnative/client_ceph.go b/internal/cephfsnative/client_ceph.go index dad93db..14c3621 100644 --- a/internal/cephfsnative/client_ceph.go +++ b/internal/cephfsnative/client_ceph.go @@ -16,20 +16,28 @@ package cephfsnative 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, - unsigned gid, long sec, long nsec) + unsigned gid, const long *t) { struct ceph_statx s = {0}; s.stx_mode = mode; s.stx_uid = uid; s.stx_gid = gid; - s.stx_mtime.tv_sec = sec; - s.stx_mtime.tv_nsec = nsec; + s.stx_atime.tv_sec = t[0]; + 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) mask |= CEPH_SETATTR_MODE; 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), 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), - 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. @@ -282,7 +294,7 @@ func (d *dir) Next() (*storage.Entry, error) { var entry C.struct_dirent var s C.struct_ceph_statx 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 { return nil, err @@ -379,11 +391,18 @@ func (c *Client) Readlink(path string) (string, error) { 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 { p, f := str(path) 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), ×[0])) } // GetXattr returns the value of an extended attribute. diff --git a/internal/migration/native_helpers.go b/internal/migration/native_helpers.go index 9a879cc..fcd6f0c 100644 --- a/internal/migration/native_helpers.go +++ b/internal/migration/native_helpers.go @@ -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) } - 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 verify { return fmt.Errorf("native metadata mismatch at %s", to) diff --git a/internal/migration/native_test.go b/internal/migration/native_test.go index 36a724b..7e502ad 100644 --- a/internal/migration/native_test.go +++ b/internal/migration/native_test.go @@ -273,3 +273,35 @@ func TestNativeVerifyCommittedRound(t *testing.T) { 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) + } + }) + } +} diff --git a/internal/storage/fs.go b/internal/storage/fs.go index f25af36..81d975b 100644 --- a/internal/storage/fs.go +++ b/internal/storage/fs.go @@ -11,7 +11,10 @@ type Node struct { Mode uint32 Inode, Version, Nlink, Size, Rdev uint64 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). @@ -80,7 +83,7 @@ type FS interface { Symlink(string, string) error Readlink(string) (string, 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) GetXattr(string, string) ([]byte, error) SetXattr(string, string, []byte) error