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

+146 -24
View File
@@ -6,22 +6,65 @@ package cephfsnative
/*
#cgo CFLAGS: -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
#cgo LDFLAGS: -lcephfs
#cgo LDFLAGS: -lcephfs -ldl
#include <dlfcn.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <time.h>
#include <cephfs/libcephfs.h>
// Change-time control.
//
// The MDS derives an inode's ctime from the time stamp the client sends with
// each request; libcephfs takes it from clock_gettime(CLOCK_REALTIME) in the
// calling thread. Measured on Ceph 21.2.0: setattr and cap flushes only move
// the ctime forward, while link, unlink, rename and xattr requests set it to
// max(stamp, mtime), also backwards.
//
// The definition below overrides clock_gettime for the whole process, but
// changes the result only in a thread that set cm_clock, and only for the
// duration of one call. Every modifying call runs with the stamp
// 1970-01-01T00:00:01Z, so it never raises a ctime. cm_attr ends with an xattr
// request stamped with the source ctime, which sets the ctime exactly. The Go
// runtime reads the clock through the vDSO and is not affected.
static __thread struct timespec cm_clock;
static int (*cm_real_clock_gettime)(clockid_t, struct timespec *);
int clock_gettime(clockid_t clk, struct timespec *ts)
{
if (cm_clock.tv_sec && (clk == CLOCK_REALTIME || clk == CLOCK_REALTIME_COARSE)) {
*ts = cm_clock;
return 0;
}
if (!cm_real_clock_gettime)
cm_real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
return cm_real_clock_gettime(clk, ts);
}
#define CM_ANCIENT(call) ({ \
cm_clock.tv_sec = 1; \
cm_clock.tv_nsec = 0; \
__typeof__(call) cm_ret = (call); \
cm_clock.tv_sec = 0; \
cm_ret; \
})
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 | 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.
// cm_attr sets owner, mode, the access, modification and birth times and, last,
// the change time. t holds seconds and nanoseconds for atime, mtime, btime and
// ctime in this order. A zero ctime leaves the change time alone.
//
// The ctime step creates and removes the xattr user.cephmig.ctime with the
// clock set to the wanted ctime. The MDS then sets ctime = max(wanted, mtime).
// It must be the last change to the inode: later links, unlinks, renames or
// xattr changes reset the ctime to the mtime.
static int cm_attr(struct ceph_mount_info *m, const char *p, unsigned mode, unsigned uid,
unsigned gid, const long *t)
{
@@ -40,7 +83,17 @@ static int cm_attr(struct ceph_mount_info *m, const char *p, unsigned mode, unsi
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);
int ret = CM_ANCIENT(ceph_setattrx(m, p, &s, mask, AT_SYMLINK_NOFOLLOW));
if (ret < 0 || (t[6] == 0 && t[7] == 0))
return ret;
cm_clock.tv_sec = t[6];
cm_clock.tv_nsec = t[7];
ret = ceph_lsetxattr(m, p, "user.cephmig.ctime", "", 0, XATTR_CREATE);
if (ret == 0)
ret = ceph_lremovexattr(m, p, "user.cephmig.ctime");
cm_clock.tv_sec = 0;
return ret;
}
// ceph_link resolves a symbolic-link source. Use inode-based linking so the
@@ -63,6 +116,70 @@ static int cm_link_nofollow(struct ceph_mount_info *m, const char *oldpath,
ceph_ll_put(m, in);
return ret;
}
static int cm_open(struct ceph_mount_info *m, const char *p, int flags, mode_t mode)
{
if ((flags & O_ACCMODE) == O_RDONLY && !(flags & (O_CREAT | O_TRUNC)))
return ceph_open(m, p, flags, mode);
return CM_ANCIENT(ceph_open(m, p, flags, mode));
}
static int cm_close(struct ceph_mount_info *m, int fd) { return CM_ANCIENT(ceph_close(m, fd)); }
static int cm_fsync(struct ceph_mount_info *m, int fd) { return CM_ANCIENT(ceph_fsync(m, fd, 0)); }
static int cm_sync_fs(struct ceph_mount_info *m) { return CM_ANCIENT(ceph_sync_fs(m)); }
static int cm_write(struct ceph_mount_info *m, int fd, const char *b, int64_t n, int64_t off)
{
return CM_ANCIENT(ceph_write(m, fd, b, n, off));
}
static int cm_ftruncate(struct ceph_mount_info *m, int fd, int64_t n)
{
return CM_ANCIENT(ceph_ftruncate(m, fd, n));
}
static int cm_fallocate(struct ceph_mount_info *m, int fd, int mode, int64_t off, int64_t n)
{
return CM_ANCIENT(ceph_fallocate(m, fd, mode, off, n));
}
static int cm_mkdir(struct ceph_mount_info *m, const char *p, mode_t mode)
{
return CM_ANCIENT(ceph_mkdir(m, p, mode));
}
static int cm_unlink(struct ceph_mount_info *m, const char *p) { return CM_ANCIENT(ceph_unlink(m, p)); }
static int cm_rmdir(struct ceph_mount_info *m, const char *p) { return CM_ANCIENT(ceph_rmdir(m, p)); }
static int cm_rename(struct ceph_mount_info *m, const char *a, const char *b)
{
return CM_ANCIENT(ceph_rename(m, a, b));
}
static int cm_link(struct ceph_mount_info *m, const char *a, const char *parent, const char *name)
{
return CM_ANCIENT(cm_link_nofollow(m, a, parent, name));
}
static int cm_symlink(struct ceph_mount_info *m, const char *a, const char *b)
{
return CM_ANCIENT(ceph_symlink(m, a, b));
}
static int cm_mknod(struct ceph_mount_info *m, const char *p, mode_t mode, dev_t dev)
{
return CM_ANCIENT(ceph_mknod(m, p, mode, dev));
}
static int cm_setxattr(struct ceph_mount_info *m, const char *p, const char *k, const void *v, size_t n)
{
return CM_ANCIENT(ceph_lsetxattr(m, p, k, v, n, 0));
}
static int cm_removexattr(struct ceph_mount_info *m, const char *p, const char *k)
{
return CM_ANCIENT(ceph_lremovexattr(m, p, k));
}
*/
import "C"
@@ -167,7 +284,8 @@ 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),
Atime: timespec(s.stx_atime), Mtime: timespec(s.stx_mtime), Btime: timespec(s.stx_btime)}
Atime: timespec(s.stx_atime), Mtime: timespec(s.stx_mtime),
Ctime: timespec(s.stx_ctime), Btime: timespec(s.stx_btime)}
}
func timespec(t C.struct_timespec) time.Time {
@@ -199,7 +317,7 @@ func nativeStatFlags(path string) C.int {
func (c *Client) Open(path string, flags int, mode uint32) (storage.File, error) {
p, f := str(path)
defer f()
ret := C.ceph_open(c.mount, p, C.int(flags), C.mode_t(mode))
ret := C.cm_open(c.mount, p, C.int(flags), C.mode_t(mode))
if err := errno(ret); err != nil {
return nil, err
}
@@ -219,7 +337,7 @@ func (f *file) Close() error {
}
f.closed = true
return errno(C.ceph_close(f.client.mount, f.fd))
return errno(C.cm_close(f.client.mount, f.fd))
}
func (f *file) ReadAt(b []byte, off int64) (int, error) {
@@ -244,7 +362,7 @@ func (f *file) WriteAt(b []byte, off int64) (int, error) {
return 0, nil
}
n := C.ceph_write(f.client.mount, f.fd, (*C.char)(unsafe.Pointer(&b[0])), C.int64_t(len(b)), C.int64_t(off))
n := C.cm_write(f.client.mount, f.fd, (*C.char)(unsafe.Pointer(&b[0])), C.int64_t(len(b)), C.int64_t(off))
if err := errno(n); err != nil {
return 0, err
}
@@ -257,17 +375,17 @@ func (f *file) WriteAt(b []byte, off int64) (int, error) {
}
func (f *file) Truncate(n int64) error {
return errno(C.ceph_ftruncate(f.client.mount, f.fd, C.int64_t(n)))
return errno(C.cm_ftruncate(f.client.mount, f.fd, C.int64_t(n)))
}
func (f *file) Sync() error { return errno(C.ceph_fsync(f.client.mount, f.fd, 0)) }
func (f *file) Sync() error { return errno(C.cm_fsync(f.client.mount, f.fd)) }
func (f *file) Lock(owner uint64) error {
return errno(C.ceph_flock(f.client.mount, f.fd, C.LOCK_EX|C.LOCK_NB, C.uint64_t(owner)))
}
func (f *file) PunchHole(off, length int64) error {
return errno(C.ceph_fallocate(f.client.mount, f.fd, 3, C.int64_t(off), C.int64_t(length)))
return errno(C.cm_fallocate(f.client.mount, f.fd, 3, C.int64_t(off), C.int64_t(length)))
}
type dir struct {
@@ -319,21 +437,21 @@ func (d *dir) Close() error { return errno(C.ceph_closedir(d.client.mount, d.han
func (c *Client) Mkdir(path string, mode uint32) error {
p, f := str(path)
defer f()
return errno(C.ceph_mkdir(c.mount, p, C.mode_t(mode)))
return errno(C.cm_mkdir(c.mount, p, C.mode_t(mode)))
}
// Unlink removes a non-directory entry.
func (c *Client) Unlink(path string) error {
p, f := str(path)
defer f()
return errno(C.ceph_unlink(c.mount, p))
return errno(C.cm_unlink(c.mount, p))
}
// Rmdir removes an empty directory.
func (c *Client) Rmdir(path string) error {
p, f := str(path)
defer f()
return errno(C.ceph_rmdir(c.mount, p))
return errno(C.cm_rmdir(c.mount, p))
}
// Rename renames a to b.
@@ -342,7 +460,7 @@ func (c *Client) Rename(a, b string) error {
defer f()
q, g := str(b)
defer g()
return errno(C.ceph_rename(c.mount, p, q))
return errno(C.cm_rename(c.mount, p, q))
}
// Link creates the hard link b to a without following a symlink at a.
@@ -353,7 +471,7 @@ func (c *Client) Link(a, b string) error {
defer g()
name, h := str(path.Base(b))
defer h()
return errno(C.cm_link_nofollow(c.mount, p, q, name))
return errno(C.cm_link(c.mount, p, q, name))
}
// Symlink creates the symbolic link b pointing to a.
@@ -362,14 +480,14 @@ func (c *Client) Symlink(a, b string) error {
defer f()
q, g := str(b)
defer g()
return errno(C.ceph_symlink(c.mount, p, q))
return errno(C.cm_symlink(c.mount, p, q))
}
// Mknod creates a special file.
func (c *Client) Mknod(path string, mode uint32, dev uint64) error {
p, f := str(path)
defer f()
return errno(C.ceph_mknod(c.mount, p, C.mode_t(mode), C.dev_t(dev)))
return errno(C.cm_mknod(c.mount, p, C.mode_t(mode), C.dev_t(dev)))
}
// Readlink returns the target of a symbolic link.
@@ -392,15 +510,19 @@ func (c *Client) Readlink(path string) (string, error) {
}
// SetAttr sets numeric owner, mode, atime, mtime and btime without following
// symlinks. The ctime cannot be set; Ceph records the time of the call.
// symlinks, then sets the ctime to max(n.Ctime, n.Mtime) (see cm_attr). It
// must be the last change to the entry in a round.
func (c *Client) SetAttr(path string, n storage.Node) error {
p, f := str(path)
defer f()
times := [6]C.long{
times := [8]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()),
}
if !n.Ctime.IsZero() {
times[6], times[7] = C.long(n.Ctime.Unix()), C.long(n.Ctime.Nanosecond())
}
return errno(C.cm_attr(c.mount, p, C.uint(n.Mode), C.uint(n.UID), C.uint(n.GID), &times[0]))
}
@@ -467,7 +589,7 @@ func (c *Client) SetXattr(path, key string, b []byte) error {
ptr = unsafe.Pointer(&b[0])
}
return errno(C.ceph_lsetxattr(c.mount, p, k, ptr, C.size_t(len(b)), 0))
return errno(C.cm_setxattr(c.mount, p, k, ptr, C.size_t(len(b))))
}
// RemoveXattr removes an extended attribute.
@@ -476,11 +598,11 @@ func (c *Client) RemoveXattr(path, key string) error {
defer f()
k, g := str(key)
defer g()
return errno(C.ceph_lremovexattr(c.mount, p, k))
return errno(C.cm_removexattr(c.mount, p, k))
}
// Sync flushes all dirty data and metadata of the filesystem.
func (c *Client) Sync() error { return errno(C.ceph_sync_fs(c.mount)) }
func (c *Client) Sync() error { return errno(C.cm_sync_fs(c.mount)) }
type snapDiff struct{ info C.struct_ceph_snapdiff_info }
+20 -1
View File
@@ -65,9 +65,13 @@ func (b *nativeTransport) copyRegular(ctx context.Context, job nativeJob) error
}
exists := statErr == nil && target.Kind() == storage.Regular
// A newer ctime than the migration set means the file was changed outside
// the migration, so a block delta against the previous round would be
// wrong. A full copy replaces it with a new inode.
replace := exists && target.Ctime.After(wantedCtime(job.node))
var previous baseEntry
trusted := false
if b.delta && exists {
if b.delta && exists && !replace {
var ok bool
var err error
previous, ok, err = b.base.entry(job.rel)
@@ -326,6 +330,21 @@ func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
return err
}
if b.openVerifier != nil {
v, err := b.openVerifier()
if err != nil {
return fmt.Errorf("native verification client: %w", err)
}
// The engine calls transport methods one at a time.
writer := b.target
b.target = v
defer func() {
b.target = writer
v.Close()
}()
}
skipData := b.verified[to]
scratch, err := osNativeVerifyIndex(b.workspace)
if err != nil {
+11 -1
View File
@@ -7,6 +7,7 @@ import (
"io"
"git.zb-server.de/Sebi/CephfsMig/internal/cephfsnative"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
)
func openNativeEngine(ctx context.Context, c Config, x Executor, log io.Writer) (Engine, io.Closer, error) {
@@ -25,6 +26,15 @@ func openNativeEngine(ctx context.Context, c Config, x Executor, log io.Writer)
return Engine{}, nil, err
}
backend := &nativeTransport{source: source, target: target, config: c, log: log}
verifier := func() (storage.FS, error) {
v, err := cephfsnative.Open(options(c.Target))
if err != nil {
return nil, err
}
return v, nil
}
backend := &nativeTransport{source: source, target: target, config: c, log: log, openVerifier: verifier}
return Engine{Exec: x, Log: log, Transport: backend, Access: nativeAccess{backend}}, backend, nil
}
+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 {
+1 -1
View File
@@ -11,7 +11,7 @@ import (
bolt "go.etcd.io/bbolt"
)
var nativeBuckets = []string{"meta", "paths", "groups", "aliases", "dirs", "full", "ranges", "changed", "delete"}
var nativeBuckets = []string{"meta", "paths", "groups", "aliases", "dirs", "full", "ranges", "changed", "delete", "relinked"}
type nativeIndex struct {
db *bolt.DB
+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)
}
})
}
+79 -9
View File
@@ -25,8 +25,15 @@ const nativeBlockThreshold = 16 << 20
type nativeTransport struct {
source, target storage.FS
config Config
log io.Writer
identity string
// openVerifier opens a separate client of the target for Verify. The
// writing client may cache a ctime that differs from the MDS, so only a
// client without caps of its own sees what other clients see. Nil means
// verify through target.
openVerifier func() (storage.FS, error)
log io.Writer
identity string
// State of the current round.
round Round
@@ -393,7 +400,14 @@ func (b *nativeTransport) Copy(ctx context.Context, from, to string) error {
return fmt.Errorf("native hardlink %s -> %s: %w", string(k), string(v), err)
}
return nil
// Linking resets the ctime of the inode; set it again.
src := path.Join(from, string(k))
n, err := b.source.Stat(src)
if err != nil {
return err
}
return restoreNativeMetadata(b.source, b.target, src, dst, n)
}); err != nil {
return err
}
@@ -403,6 +417,9 @@ func (b *nativeTransport) Copy(ctx context.Context, from, to string) error {
}
// Child creation/deletion has finished; restore directory metadata last.
// Creating or removing a child sets the ctime of its directory on the
// MDS, while the cache of this client may keep the old value, so the
// cached stat cannot tell whether a directory is current.
if err = b.pending.each("dirs", true, func(_, v []byte) error {
if err := ctx.Err(); err != nil {
return err
@@ -415,7 +432,7 @@ func (b *nativeTransport) Copy(ctx context.Context, from, to string) error {
return err
}
return syncNativeMetadata(b.source, b.target, src, dst, n, false)
return restoreNativeMetadata(b.source, b.target, src, dst, n)
}); err != nil {
return err
}
@@ -469,7 +486,7 @@ func (b *nativeTransport) prune(ctx context.Context, from, to string) error {
}
// Store deletions on disk and apply after closing the directory streams.
err := walkNative(ctx, b.target, to, func(p string, _ storage.Node, _ int) error {
err := walkNative(ctx, b.target, to, func(p string, n storage.Node, _ int) error {
rel, err := relativeNative(to, p)
if err != nil {
return err
@@ -484,17 +501,70 @@ func (b *nativeTransport) prune(ctx context.Context, from, to string) error {
return err
}
if !present {
return b.pending.put("delete", []byte(rel), []byte{1})
if present {
return nil
}
return nil
// Removing one name of a hardlinked inode resets its ctime; the
// remaining names are set again below.
if n.Kind() != storage.Directory && n.Nlink > 1 {
if err = b.pending.put("relinked", nativeInodeKey(n.Inode), []byte{1}); err != nil {
return err
}
}
return b.pending.put("delete", []byte(rel), []byte{1})
})
if err != nil {
return err
}
return b.pending.each("delete", true, func(k, _ []byte) error { return removeNative(ctx, b.target, path.Join(to, string(k))) })
err = b.pending.each("delete", true, func(k, _ []byte) error {
return removeNative(ctx, b.target, path.Join(to, string(k)))
})
if err != nil {
return err
}
return b.restoreRelinked(from, to)
}
// restoreRelinked sets the metadata of regular files again whose target inode
// lost a name during pruning. The source inventory records the target inode
// of every regular file copied in this round.
func (b *nativeTransport) restoreRelinked(from, to string) error {
inodes := map[uint64]bool{}
if err := b.pending.each("relinked", false, func(k, _ []byte) error {
inodes[binary.BigEndian.Uint64(k)] = true
return nil
}); err != nil || len(inodes) == 0 {
return err
}
var rels []string
if err := b.pending.each("paths", false, func(k, v []byte) error {
if e, ok := decodeEntry(v); ok && e.TargetInode != 0 && inodes[e.TargetInode] {
rels = append(rels, string(k))
}
return nil
}); err != nil {
return err
}
for _, rel := range rels {
src := path.Join(from, rel)
n, err := b.source.Stat(src)
if err != nil {
return err
}
if err = restoreNativeMetadata(b.source, b.target, src, path.Join(to, rel), n); err != nil {
return err
}
}
return nil
}
func (b *nativeTransport) Published(p string) { b.verified[p] = true }
+4 -4
View File
@@ -12,9 +12,9 @@ type Node struct {
Inode, Version, Nlink, Size, Rdev uint64
UID, GID uint32
// 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
// Access, modification, change and birth time. SetAttr sets the change
// time to Ctime, but not below Mtime.
Atime, Mtime, Ctime, Btime time.Time
}
// File type bits of Node.Mode, as in stat(2).
@@ -83,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, atime, mtime and btime; no symlink following.
SetAttr(string, Node) error // Numeric owner, mode, all four times; no symlink following.
ListXattr(string) ([]string, error)
GetXattr(string, string) ([]byte, error)
SetXattr(string, string, []byte) error