The native libcephfs backend is the only transfer path. The rsync transport, the mounted-filesystem access and its helpers (mount check, path resolution below a mount, mount claim, quota and hardlink repair through the mount) are removed, together with the rsync lab test and the client image that only added rsync. The claim and release tests now exercise the native claim through a local test file system with real flock locks. BREAKING CHANGE: "backend": "rsync" and the endpoint field "mount" are rejected. "backend" is optional and only accepts "native"; jobs created with "native" resume without it. Journals of rsync jobs cannot be resumed. Builds without the build tag ceph only support doctor and status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
407 lines
8.9 KiB
Go
407 lines
8.9 KiB
Go
package migration
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
// errSkipSubtree, returned by a walkNative visitor for a directory, skips
|
|
// the entries below it.
|
|
var errSkipSubtree = errors.New("skip subtree")
|
|
|
|
// quotaKeys are the directory quota xattrs that the migration copies.
|
|
var quotaKeys = []string{"ceph.quota.max_bytes", "ceph.quota.max_files"}
|
|
|
|
func walkNative(ctx context.Context, fs storage.FS, root string, visit func(string, storage.Node, int) error) error {
|
|
var walk func(string, storage.Node, int) error
|
|
walk = func(p string, n storage.Node, depth int) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if depth > 128 {
|
|
return errors.New("native directory depth exceeds 128")
|
|
}
|
|
|
|
if err := visit(p, n, depth); errors.Is(err, errSkipSubtree) && n.Kind() == storage.Directory {
|
|
return nil
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return nil
|
|
}
|
|
|
|
dir, err := fs.OpenDir(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer dir.Close()
|
|
for {
|
|
e, err := dir.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if e == nil || e.Name == "" || strings.ContainsAny(e.Name, "/\x00") || e.Name == "." || e.Name == ".." || e.Name == ".snap" {
|
|
return errors.New("invalid native directory entry")
|
|
}
|
|
|
|
if err = walk(path.Join(p, e.Name), e.Node, depth+1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
n, err := fs.Stat(root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return walk(root, n, 0)
|
|
}
|
|
|
|
func removeNative(ctx context.Context, fs storage.FS, p string) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
n, err := fs.Stat(p)
|
|
if errors.Is(err, syscall.ENOENT) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return fs.Unlink(p)
|
|
}
|
|
|
|
d, err := fs.OpenDir(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Re-open after each bounded batch, so deleting while iterating does not skip
|
|
// entries on directory streams whose offsets change after removal.
|
|
for {
|
|
var names []string
|
|
for len(names) < 128 {
|
|
e, err := d.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
break
|
|
}
|
|
if err != nil {
|
|
d.Close()
|
|
return err
|
|
}
|
|
|
|
names = append(names, e.Name)
|
|
}
|
|
|
|
if err = d.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(names) == 0 {
|
|
break
|
|
}
|
|
|
|
for _, s := range names {
|
|
if err = removeNative(ctx, fs, path.Join(p, s)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
d, err = fs.OpenDir(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return fs.Rmdir(p)
|
|
}
|
|
|
|
func nativeQuota(fs storage.FS, p, key string) ([]byte, error) {
|
|
b, err := fs.GetXattr(p, key)
|
|
if errors.Is(err, syscall.ENODATA) {
|
|
return []byte("0"), nil
|
|
}
|
|
|
|
return b, err
|
|
}
|
|
|
|
func clearNativeQuotas(ctx context.Context, fs storage.FS, root string) error {
|
|
return walkNative(ctx, fs, root, func(p string, n storage.Node, _ int) error {
|
|
if n.Kind() != storage.Directory {
|
|
return nil
|
|
}
|
|
|
|
for _, key := range quotaKeys {
|
|
b, err := nativeQuota(fs, p, key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if string(b) != "0" {
|
|
if err = fs.SetXattr(p, key, []byte("0")); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func portableXattr(s string) bool {
|
|
return !strings.HasPrefix(s, "ceph.") && (!strings.HasPrefix(s, "system.") || s == "system.posix_acl_access" || s == "system.posix_acl_default")
|
|
}
|
|
|
|
func nativeAttrs(fs storage.FS, p string) (map[string][]byte, error) {
|
|
names, err := fs.ListXattr(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
attrs := make(map[string][]byte, len(names))
|
|
for _, key := range names {
|
|
if !portableXattr(key) {
|
|
continue
|
|
}
|
|
|
|
value, err := fs.GetXattr(p, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
attrs[key] = value
|
|
}
|
|
|
|
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 {
|
|
return err
|
|
}
|
|
|
|
return syncNativeMetadataKnown(src, dst, from, to, n, target, verify)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
diff := metadataDiff(n, target)
|
|
different := diff != ""
|
|
if different && verify {
|
|
return fmt.Errorf("native metadata mismatch at %s:%s", to, diff)
|
|
}
|
|
|
|
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 false, err
|
|
}
|
|
|
|
old, err := nativeAttrs(dst, to)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
changed := false
|
|
for k, v := range old {
|
|
value, exists := attrs[k]
|
|
if verify {
|
|
if !exists || !bytes.Equal(value, v) {
|
|
return false, fmt.Errorf("native xattr mismatch at %s: %s", to, k)
|
|
}
|
|
} else if !exists {
|
|
if err = dst.RemoveXattr(to, k); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
changed = true
|
|
}
|
|
}
|
|
|
|
for k, v := range attrs {
|
|
if verify {
|
|
if actual, ok := old[k]; !ok || !bytes.Equal(v, actual) {
|
|
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 false, fmt.Errorf("native setxattr %s: %w", k, err)
|
|
}
|
|
|
|
changed = true
|
|
}
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return changed, 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 {
|
|
st, err := fs.Stat(p)
|
|
if err == nil && st.Kind() == storage.Directory {
|
|
return nil
|
|
}
|
|
|
|
if err != nil && !errors.Is(err, syscall.ENOENT) {
|
|
return err
|
|
}
|
|
|
|
if err == nil {
|
|
if err = removeNative(ctx, fs, p); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return fs.Mkdir(p, 0700)
|
|
}
|
|
|
|
func validateNativeDirectory(fs storage.FS, p string) error {
|
|
current := "/"
|
|
for _, part := range strings.Split(strings.TrimPrefix(p, "/"), "/") {
|
|
if part == "" || part == "." || part == ".." {
|
|
return fmt.Errorf("invalid native path %q", p)
|
|
}
|
|
|
|
current = path.Join(current, part)
|
|
n, err := fs.Stat(current)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return fmt.Errorf("native path contains non-directory %s", current)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|