Intermediate rounds skipped a directory when its inode version and its ceph.dir.rctime matched the previous round. In the 10,000-file lab test a round snapshot taken seconds after changes skipped tree/ with about 250 changed files in its subdirectories: the inode version of tree/ changes only with its own entries, and Ceph had not yet propagated the rctime of the grandchildren. The target snapshot of that round was incomplete, and its verification skipped the same subtrees; only finalize closed the gap. Every round now scans all metadata again. Base index entries that the old version marked carried are never trusted, so such files are copied in full in the next round. The smallfiles verification now checks only snapshots that sync did not remove. BREAKING CHANGE: the configuration field "full_scan" and the transfer statistic "skipped_subtrees" are removed; a configuration with "full_scan" is rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
628 lines
15 KiB
Go
628 lines
15 KiB
Go
package migration
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
func readNativeAt(f storage.File, b []byte, offset int64) error {
|
|
n, err := f.ReadAt(b, offset)
|
|
if n != len(b) {
|
|
if err == nil {
|
|
err = io.ErrUnexpectedEOF
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func writeNativeAt(f storage.File, b []byte, offset int64) error {
|
|
n, err := f.WriteAt(b, offset)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if n != len(b) {
|
|
return io.ErrShortWrite
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func allZero(b []byte) bool {
|
|
for _, v := range b {
|
|
if v != 0 {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// baseTrusted reports whether the base entry previous still describes the
|
|
// source node n and its target copy, so the copy may be skipped or patched.
|
|
// A carried entry was written by an earlier version that skipped subtrees;
|
|
// it may describe an older round and is never trusted.
|
|
func baseTrusted(previous baseEntry, n, target storage.Node) bool {
|
|
return !previous.Carried && previous.Inode == n.Inode && previous.TargetInode == target.Inode && previous.Size == target.Size
|
|
}
|
|
|
|
func (b *nativeTransport) copyRegular(ctx context.Context, job nativeJob) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
from, to := path.Join(b.round.SourcePath, job.rel), path.Join(b.round.TargetPath, job.rel)
|
|
target, statErr := b.target.Stat(to)
|
|
if statErr != nil && !errors.Is(statErr, syscall.ENOENT) {
|
|
return statErr
|
|
}
|
|
|
|
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 && !replace {
|
|
var ok bool
|
|
var err error
|
|
previous, ok, err = b.base.entry(job.rel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
trusted = ok && baseTrusted(previous, job.node, target)
|
|
}
|
|
|
|
changed := true
|
|
if trusted && job.node.Version != 0 && job.node.Version == previous.Version && job.node.Size == previous.Size {
|
|
changedBytes, err := b.pending.get("changed", nativeInodeKey(job.node.Inode))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
changed = changedBytes != nil
|
|
}
|
|
|
|
if changed {
|
|
usedBlockDiff := false
|
|
if trusted && job.node.Size >= nativeBlockThreshold {
|
|
old, err := b.source.Stat(path.Join(b.round.BaseSourcePath, job.rel))
|
|
if err == nil && old.Kind() == storage.Regular && old.Inode == job.node.Inode {
|
|
stream, err := b.source.BlockDiff(b.snapRoot, path.Join(b.snapRel, job.rel), b.round.BaseName, b.round.Name)
|
|
if err == nil {
|
|
usedBlockDiff = true
|
|
if err = b.copyBlocks(ctx, job, stream); err != nil {
|
|
return fmt.Errorf("native blockdiff %s: %w", job.rel, err)
|
|
}
|
|
|
|
b.blockFiles.Add(1)
|
|
} else if !errors.Is(err, syscall.ENOSYS) && !errors.Is(err, syscall.EOPNOTSUPP) && !errors.Is(err, syscall.ENOENT) && !errors.Is(err, syscall.EINVAL) {
|
|
return err
|
|
}
|
|
} else if err != nil && !errors.Is(err, syscall.ENOENT) && !errors.Is(err, syscall.ENOTDIR) {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if !usedBlockDiff {
|
|
if err := b.copyFull(ctx, job); err != nil {
|
|
return fmt.Errorf("native full copy %s: %w", job.rel, err)
|
|
}
|
|
|
|
if err := b.pending.put("full", []byte(job.rel), []byte{1}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
b.copied.Add(1)
|
|
}
|
|
|
|
if err := syncNativeMetadata(b.source, b.target, from, to, job.node, false); err != nil {
|
|
return err
|
|
}
|
|
|
|
current, err := b.target.Stat(to)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return b.pending.put("paths", []byte(job.rel), encodeEntry(baseEntry{Inode: job.node.Inode, Version: job.node.Version, Size: job.node.Size, TargetInode: current.Inode}))
|
|
}
|
|
|
|
func (b *nativeTransport) copyFull(ctx context.Context, job nativeJob) (result error) {
|
|
from, to := path.Join(b.round.SourcePath, job.rel), path.Join(b.round.TargetPath, job.rel)
|
|
source, err := b.source.Open(from, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer source.Close()
|
|
old, err := b.target.Stat(to)
|
|
if err != nil && !errors.Is(err, syscall.ENOENT) {
|
|
return err
|
|
}
|
|
|
|
if err == nil && old.Kind() == storage.Directory {
|
|
if err = removeNative(ctx, b.target, to); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
id, err := newID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
temporary := path.Join(path.Dir(to), ".cephmig-file-"+id)
|
|
target, err := b.target.Open(temporary, syscall.O_CREAT|syscall.O_EXCL|syscall.O_RDWR|syscall.O_NOFOLLOW, 0600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func() {
|
|
if target != nil {
|
|
if err := target.Close(); result == nil {
|
|
result = err
|
|
}
|
|
}
|
|
|
|
if result != nil {
|
|
_ = b.target.Unlink(temporary)
|
|
}
|
|
}()
|
|
|
|
buffer := make([]byte, min(job.node.Size, uint64(nativeChunk)))
|
|
for off := uint64(0); off < job.node.Size; {
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
length := min(uint64(len(buffer)), job.node.Size-off)
|
|
chunk := buffer[:int(length)]
|
|
if err = readNativeAt(source, chunk, int64(off)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !allZero(chunk) {
|
|
if err = b.rate(ctx, len(chunk)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = writeNativeAt(target, chunk, int64(off)); err != nil {
|
|
return err
|
|
}
|
|
|
|
b.written.Add(int64(length))
|
|
}
|
|
|
|
off += length
|
|
}
|
|
|
|
if err = target.Truncate(int64(job.node.Size)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = target.Sync(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = target.Close(); err != nil {
|
|
return err
|
|
}
|
|
|
|
target = nil
|
|
return b.target.Rename(temporary, to)
|
|
}
|
|
|
|
func (b *nativeTransport) copyBlocks(ctx context.Context, job nativeJob, blocks storage.BlockStream) (result error) {
|
|
defer func() {
|
|
if err := blocks.Close(); result == nil {
|
|
result = err
|
|
}
|
|
}()
|
|
|
|
source, err := b.source.Open(path.Join(b.round.SourcePath, job.rel), syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer source.Close()
|
|
target, err := b.target.Open(path.Join(b.round.TargetPath, job.rel), syscall.O_RDWR|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer func() {
|
|
if err := target.Close(); result == nil {
|
|
result = err
|
|
}
|
|
}()
|
|
|
|
buffer := make([]byte, nativeChunk)
|
|
for {
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
ranges, err := blocks.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, extent := range ranges {
|
|
if extent.Offset > job.node.Size {
|
|
continue
|
|
}
|
|
|
|
length := min(extent.Length, job.node.Size-extent.Offset)
|
|
if extent.Length > ^uint64(0)-extent.Offset {
|
|
return errors.New("blockdiff extent overflows")
|
|
}
|
|
|
|
for copied := uint64(0); copied < length; {
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
count := min(uint64(len(buffer)), length-copied)
|
|
chunk := buffer[:int(count)]
|
|
off := extent.Offset + copied
|
|
if err = readNativeAt(source, chunk, int64(off)); err != nil {
|
|
return err
|
|
}
|
|
|
|
punched := false
|
|
if allZero(chunk) {
|
|
err = target.PunchHole(int64(off), int64(count))
|
|
if err == nil {
|
|
punched = true
|
|
} else if !errors.Is(err, syscall.EOPNOTSUPP) && !errors.Is(err, syscall.ENOSYS) && !errors.Is(err, syscall.EINVAL) {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if !punched {
|
|
if err = b.rate(ctx, len(chunk)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = writeNativeAt(target, chunk, int64(off)); err != nil {
|
|
return err
|
|
}
|
|
|
|
b.written.Add(int64(count))
|
|
}
|
|
|
|
if err = b.pending.extent(job.rel, off, count); err != nil {
|
|
return err
|
|
}
|
|
|
|
copied += count
|
|
}
|
|
}
|
|
}
|
|
|
|
if err = target.Truncate(int64(job.node.Size)); err != nil {
|
|
return err
|
|
}
|
|
|
|
return target.Sync()
|
|
}
|
|
|
|
func (b *nativeTransport) Verify(ctx context.Context, from, to string) error {
|
|
if b.pending == nil || from != b.round.SourcePath {
|
|
return errors.New("native verification round is not initialized")
|
|
}
|
|
|
|
if err := b.pending.requireInventory(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if b.openTarget != nil {
|
|
// Replace the writing client before reading the target. Its cache
|
|
// may hold a ctime that differs from the MDS, and the MDS would have
|
|
// to recall its caps on every entry it wrote last, which in the lab
|
|
// cost about five seconds per lookup. Closing it returns all caps at
|
|
// once. The engine calls transport methods one at a time, and the
|
|
// claim lock is held by a client of its own.
|
|
v, err := b.openTarget()
|
|
if err != nil {
|
|
return fmt.Errorf("native verification client: %w", err)
|
|
}
|
|
|
|
err = b.target.Close()
|
|
b.target = v
|
|
if err != nil {
|
|
return fmt.Errorf("close native writing client: %w", err)
|
|
}
|
|
}
|
|
|
|
skipData := b.verified[to]
|
|
scratch, err := osNativeVerifyIndex(b.workspace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer scratch.cleanup()
|
|
v := &nativeVerification{b: b, to: to, skipData: skipData, index: scratch.index}
|
|
|
|
// The walk and the directories stay serial; every other entry is checked by
|
|
// one of the workers.
|
|
ctx, cancel := context.WithCancelCause(ctx)
|
|
defer cancel(nil)
|
|
workers := b.config.Workers
|
|
if workers == 0 {
|
|
workers = 8
|
|
}
|
|
|
|
type entry struct {
|
|
p, rel string
|
|
n storage.Node
|
|
}
|
|
|
|
jobs := make(chan entry, workers*4)
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < workers; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for job := range jobs {
|
|
if ctx.Err() != nil {
|
|
continue
|
|
}
|
|
|
|
if err := v.entry(ctx, job.p, job.rel, job.n); err != nil {
|
|
cancel(err)
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
lastProgress := time.Now()
|
|
var verifiedPaths uint64
|
|
err = walkNative(ctx, b.source, from, func(p string, n storage.Node, _ int) error {
|
|
verifiedPaths++
|
|
if b.log != nil && time.Since(lastProgress) >= 30*time.Second {
|
|
fmt.Fprintf(b.log, "native verifying paths=%d target=%s\n", verifiedPaths, to)
|
|
lastProgress = time.Now()
|
|
}
|
|
|
|
rel, err := relativeNative(from, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if n.Kind() == storage.Directory {
|
|
return v.entry(ctx, p, rel, n)
|
|
}
|
|
|
|
select {
|
|
case jobs <- entry{p: p, rel: rel, n: n}:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return context.Cause(ctx)
|
|
}
|
|
})
|
|
close(jobs)
|
|
wg.Wait()
|
|
if cause := context.Cause(ctx); cause != nil && (err == nil || errors.Is(err, context.Canceled)) {
|
|
err = cause
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = b.verifyInventory(ctx, to); err == nil {
|
|
b.verified[to] = true
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// nativeVerification holds the state of one Verify call that its workers
|
|
// share.
|
|
type nativeVerification struct {
|
|
b *nativeTransport
|
|
to string
|
|
skipData bool
|
|
mu sync.Mutex // guards index, which holds one write transaction
|
|
index *linkIndex
|
|
}
|
|
|
|
// observe records a hardlink relation in the scratch index.
|
|
func (v *nativeVerification) observe(bucket string, key, value [16]byte) (bool, error) {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
return v.index.observe(bucket, key, value)
|
|
}
|
|
|
|
// entry checks one source entry p against its target copy. It is safe for
|
|
// concurrent use.
|
|
func (v *nativeVerification) entry(ctx context.Context, p, rel string, n storage.Node) error {
|
|
b := v.b
|
|
targetPath := path.Join(v.to, rel)
|
|
target, err := b.target.Stat(targetPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = syncNativeMetadataKnown(b.source, b.target, p, targetPath, n, target, true); err != nil {
|
|
return err
|
|
}
|
|
|
|
if n.Kind() == storage.Directory {
|
|
return nil
|
|
}
|
|
|
|
if n.Nlink > 1 {
|
|
same, err := v.observe("source", nodeKey(n.Inode), nodeKey(target.Inode))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !same {
|
|
return fmt.Errorf("native hardlink group was split at %s", rel)
|
|
}
|
|
}
|
|
|
|
if target.Nlink > 1 {
|
|
same, err := v.observe("target", nodeKey(target.Inode), nodeKey(n.Inode))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !same {
|
|
return fmt.Errorf("native unrelated files became hardlinked at %s", rel)
|
|
}
|
|
}
|
|
|
|
switch n.Kind() {
|
|
case storage.Regular:
|
|
if n.Size != target.Size {
|
|
return fmt.Errorf("native size mismatch at %s", targetPath)
|
|
}
|
|
|
|
canonical, err := b.pending.get("groups", nativeInodeKey(n.Inode))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !v.skipData && string(canonical) == rel {
|
|
full, err := b.pending.get("full", []byte(rel))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if full != nil {
|
|
if err = b.compareRange(ctx, p, targetPath, 0, n.Size); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err = b.pending.ranges(rel, func(off, length uint64) error { return b.compareRange(ctx, p, targetPath, off, length) }); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// Only live target identities may be reused as the next blockdiff base.
|
|
if v.to == b.round.TargetPath && !v.skipData {
|
|
return b.pending.putEntryIfChanged(rel, baseEntry{Inode: n.Inode, Version: n.Version, Size: n.Size, TargetInode: target.Inode})
|
|
}
|
|
|
|
case storage.Symlink:
|
|
a, err := b.source.Readlink(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
t, err := b.target.Readlink(targetPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if a != t {
|
|
return fmt.Errorf("native symlink mismatch at %s", rel)
|
|
}
|
|
|
|
default:
|
|
if n.Rdev != target.Rdev {
|
|
return fmt.Errorf("native device mismatch at %s", rel)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *nativeTransport) verifyInventory(ctx context.Context, to string) error {
|
|
if err := b.pending.requireInventory(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// The source snapshot is immutable. Check the complete local inventory,
|
|
// including aliases/directories/special files, rather than stat every source
|
|
// path again. Target traversal still detects every unexpected entry.
|
|
return walkNative(ctx, b.target, to, func(p string, _ storage.Node, _ int) error {
|
|
rel, err := relativeNative(to, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
present, err := b.pending.containsPath(rel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !present {
|
|
return fmt.Errorf("unexpected native target entry %s", rel)
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (b *nativeTransport) compareRange(ctx context.Context, from, to string, offset, length uint64) error {
|
|
if length == 0 {
|
|
return nil
|
|
}
|
|
|
|
source, err := b.source.Open(from, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer source.Close()
|
|
target, err := b.target.Open(to, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer target.Close()
|
|
bufferSize := min(length, uint64(nativeChunk))
|
|
a, v := make([]byte, bufferSize), make([]byte, bufferSize)
|
|
for done := uint64(0); done < length; {
|
|
if err = ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
count := min(uint64(len(a)), length-done)
|
|
if err = readNativeAt(source, a[:int(count)], int64(offset+done)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = readNativeAt(target, v[:int(count)], int64(offset+done)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !bytes.Equal(a[:int(count)], v[:int(count)]) {
|
|
return fmt.Errorf("native content mismatch at %s offset %d", to, offset+done)
|
|
}
|
|
|
|
b.checked.Add(int64(count))
|
|
done += count
|
|
}
|
|
|
|
return nil
|
|
}
|