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:
1 parent
a6fda8f8d3
commit
02ebc08c88
7 files changed
+137
-19
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user