Files
Sebastian UnterschützandClaude Opus 5.5 330a3ec12c feat!: remove the rsync backend
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>
2026-12-03 22:03:22 +01:00

279 lines
6.5 KiB
Go

package migration
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
)
type nativeAccess struct{ backend *nativeTransport }
func (a nativeAccess) Inspect(ctx context.Context, c Config, x Executor) (*Inspection, error) {
if err := c.Validate(); err != nil {
return nil, err
}
sf, si, err := queryEndpoint(ctx, ceph{endpoint: c.Source, exec: x})
if err != nil {
return nil, err
}
tf, ti, err := queryEndpoint(ctx, ceph{endpoint: c.Target, exec: x})
if err != nil {
return nil, err
}
if sf == tf {
return nil, errors.New("native source and target must be different clusters")
}
for _, p := range []struct {
fs storage.FS
path, fsid string
}{{a.backend.source, si.Path, sf}, {a.backend.target, ti.Path, tf}} {
if _, err := nativePath(p.path); err != nil {
return nil, err
}
if err := validateNativeDirectory(p.fs, p.path); err != nil {
return nil, err
}
id, err := p.fs.GetXattr(p.path, "ceph.cluster_fsid")
if err != nil {
return nil, err
}
if string(id) != p.fsid {
return nil, errors.New("native connection belongs to the wrong cluster")
}
}
names, err := (ceph{endpoint: c.Source, exec: x}).names(ctx)
if err != nil {
return nil, err
}
fingerprint := sha256.Sum256([]byte(sf + "\x00" + tf + "\x00" + si.Path + "\x00" + ti.Path))
a.backend.identity = hex.EncodeToString(fingerprint[:])
return &Inspection{SourceFSID: sf, TargetFSID: tf, SourcePath: si.Path, TargetPath: ti.Path,
SourceBytes: si.BytesUsed, SourceSnapshots: names, Notes: []string{SnapshotIdentityNote}}, nil
}
func (a nativeAccess) Empty(ctx context.Context, p string) error {
if err := ctx.Err(); err != nil {
return err
}
d, err := a.backend.target.OpenDir(p)
if err != nil {
return err
}
defer d.Close()
_, err = d.Next()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
return errors.New("new native migration requires an empty target subvolume")
}
func (a nativeAccess) Claim(ctx context.Context, c Config, fsid, id string) (io.Closer, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if a.backend.openTarget == nil {
return claimNative(a.backend.target, c, fsid, id)
}
// The lock lives as long as this client; Verify replaces target.
client, err := a.backend.openTarget()
if err != nil {
return nil, fmt.Errorf("native claim client: %w", err)
}
f, err := claimNative(client, c, fsid, id)
if err != nil {
client.Close()
return nil, err
}
return nativeClaim{file: f, client: client}, nil
}
// nativeClaim is a claim lock held through a client of its own.
type nativeClaim struct {
file storage.File
client storage.FS
}
func (c nativeClaim) Close() error {
return errors.Join(c.file.Close(), c.client.Close())
}
// claimNative creates the claim file for id if needed, locks it through fs
// and checks that it names id. The returned file holds the lock.
func claimNative(fs storage.FS, c Config, fsid, id string) (storage.File, error) {
current := "/"
for _, component := range []string{".cephmig", "claims"} {
current = path.Join(current, component)
n, err := fs.Stat(current)
if errors.Is(err, syscall.ENOENT) {
if err = fs.Mkdir(current, 0700); err != nil && !errors.Is(err, syscall.EEXIST) {
return nil, err
}
n, err = fs.Stat(current)
}
if err != nil {
return nil, err
}
if n.Kind() != storage.Directory {
return nil, errors.New("native claim directory contains a symlink or non-directory")
}
}
p := path.Join(current, claimName(fsid, c.Target))
f, err := fs.Open(p, syscall.O_CREAT|syscall.O_EXCL|syscall.O_RDWR|syscall.O_NOFOLLOW, 0600)
if err == nil {
if err = writeNativeAt(f, []byte(id+"\n"), 0); err == nil {
err = f.Sync()
}
closeErr := f.Close()
if err == nil {
err = closeErr
}
if err != nil {
return nil, err
}
} else if !errors.Is(err, syscall.EEXIST) {
return nil, err
}
n, err := fs.Stat(p)
if err != nil {
return nil, err
}
if n.Kind() != storage.Regular {
return nil, errors.New("native target claim is not a regular file")
}
f, err = fs.Open(p, syscall.O_RDWR|syscall.O_NOFOLLOW, 0)
if err != nil {
return nil, err
}
if err = f.Lock(1); err != nil {
f.Close()
return nil, fmt.Errorf("native target is locked: %w", err)
}
b := make([]byte, 256)
count, readErr := f.ReadAt(b, 0)
if readErr != nil && !errors.Is(readErr, io.EOF) {
f.Close()
return nil, readErr
}
if string(b[:count]) != id+"\n" {
f.Close()
return nil, errors.New("native target belongs to a different job")
}
return f, nil
}
func (a nativeAccess) Release(ctx context.Context, c Config, fsid, id string) error {
if err := ctx.Err(); err != nil {
return err
}
fs := a.backend.target
dir := "/.cephmig/claims"
p := path.Join(dir, claimName(fsid, c.Target))
f, err := fs.Open(p, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
if errors.Is(err, syscall.ENOENT) {
return nil
}
if err != nil {
return err
}
b := make([]byte, 256)
n, err := f.ReadAt(b, 0)
f.Close()
if err != nil && !errors.Is(err, io.EOF) {
return err
}
if string(b[:n]) != id+"\n" {
return errors.New("native target claim belongs to a different job; not removed")
}
if err = fs.Unlink(p); err != nil {
return err
}
for _, d := range []string{dir, "/.cephmig"} {
if err := fs.Rmdir(d); err != nil && !errors.Is(err, syscall.ENOENT) && !errors.Is(err, syscall.ENOTEMPTY) {
return err
}
}
return nil
}
func (a nativeAccess) Prepare(ctx context.Context, _, target, _ string) error {
return clearNativeQuotas(ctx, a.backend.target, target)
}
func (a nativeAccess) Apply(context.Context, string, string) error { return a.backend.target.Sync() }
func (a nativeAccess) Verify(context.Context, string, string, string) error { return nil } // Transport verifies all native metadata and links.
type nativeVerifyIndex struct {
index *linkIndex
dir string
}
func osNativeVerifyIndex(parent string) (*nativeVerifyIndex, error) {
dir, err := os.MkdirTemp(parent, "verify-")
if err != nil {
return nil, err
}
index, err := openLinkIndex(filepath.Join(dir, "links.bolt"))
if err != nil {
os.RemoveAll(dir)
return nil, err
}
return &nativeVerifyIndex{index: index, dir: dir}, nil
}
func (i *nativeVerifyIndex) cleanup() {
_ = i.index.close()
_ = os.RemoveAll(i.dir)
}
func options(ep Endpoint) storage.Options {
return storage.Options{Config: ep.Config, Keyring: ep.Keyring, User: strings.TrimPrefix(ep.User, "client."), Filesystem: ep.Filesystem}
}