Document every exported type, function and method in the migration, storage and cephfsnative packages and add package comments for migration and the cephmig command, following the commentary rules of Effective Go. Comments on unexported helpers now start with the name they describe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
4.5 KiB
Go
187 lines
4.5 KiB
Go
package migration
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
)
|
|
|
|
type linkIndex struct {
|
|
db *bolt.DB
|
|
tx *bolt.Tx
|
|
observations int
|
|
}
|
|
|
|
func openLinkIndex(path string) (*linkIndex, error) {
|
|
// Rebuildable scratch data only: never used as a durable migration checkpoint.
|
|
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: time.Second, NoSync: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tx, err := db.Begin(true)
|
|
if err != nil {
|
|
db.Close()
|
|
return nil, err
|
|
}
|
|
for _, name := range []string{"source", "target"} {
|
|
if _, err = tx.CreateBucket([]byte(name)); err != nil {
|
|
tx.Rollback()
|
|
db.Close()
|
|
return nil, err
|
|
}
|
|
}
|
|
return &linkIndex{db: db, tx: tx}, nil
|
|
}
|
|
func (x *linkIndex) close() error {
|
|
if x.tx != nil {
|
|
_ = x.tx.Rollback()
|
|
}
|
|
return x.db.Close()
|
|
}
|
|
func (x *linkIndex) observe(bucket string, key, value [16]byte) (bool, error) {
|
|
b := x.tx.Bucket([]byte(bucket))
|
|
old := b.Get(key[:])
|
|
same := true
|
|
if old == nil {
|
|
if err := b.Put(key[:], value[:]); err != nil {
|
|
return false, err
|
|
}
|
|
} else {
|
|
same = bytes.Equal(old, value[:])
|
|
}
|
|
x.observations++
|
|
// Limit dirty transaction state; the complete relation stays on disk.
|
|
if x.observations%4096 == 0 {
|
|
err := x.tx.Commit()
|
|
x.tx = nil
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
x.tx, err = x.db.Begin(true)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
return same, nil
|
|
}
|
|
func inodeIdentity(info os.FileInfo) ([16]byte, uint64, error) {
|
|
var key [16]byte
|
|
st, ok := info.Sys().(*syscall.Stat_t)
|
|
if !ok {
|
|
return key, 0, errors.New("missing Linux inode information")
|
|
}
|
|
binary.BigEndian.PutUint64(key[:8], uint64(st.Dev))
|
|
binary.BigEndian.PutUint64(key[8:], st.Ino)
|
|
return key, uint64(st.Nlink), nil
|
|
}
|
|
|
|
// hardlinkTopology compares the hard-link groups of src and dst.
|
|
// In repair mode it removes only excess target hardlinks. Missing paths are then
|
|
// recreated by the copy backend. In verification mode check both directions:
|
|
// source groups must stay joined, and unrelated source files must stay separate.
|
|
func hardlinkTopology(ctx context.Context, src, dst, scratchParent string, repair bool) (result error) {
|
|
scratch, err := os.MkdirTemp(scratchParent, ".cephmig-links-")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(scratch)
|
|
index, err := openLinkIndex(filepath.Join(scratch, "index.bolt"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err := index.close(); result == nil {
|
|
result = err
|
|
}
|
|
}()
|
|
targetPath := func(path string) (string, error) {
|
|
rel, err := filepath.Rel(src, path)
|
|
if err != nil || !beneath(src, path) {
|
|
return "", fmt.Errorf("hardlink path outside source: %s", path)
|
|
}
|
|
return filepath.Join(dst, rel), nil
|
|
}
|
|
visitDir := func(path string) error {
|
|
target, err := targetPath(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
st, err := os.Lstat(target)
|
|
if repair && (errors.Is(err, os.ErrNotExist) || (err == nil && !st.IsDir())) {
|
|
// Never follow a target symlink directory, even when its referent exists.
|
|
return fs.SkipDir
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !st.IsDir() {
|
|
return fmt.Errorf("target directory changed type: %s", target)
|
|
}
|
|
return nil
|
|
}
|
|
visitFile := func(path string, sourceInfo os.FileInfo) error {
|
|
if !sourceInfo.Mode().IsRegular() {
|
|
return nil
|
|
}
|
|
target, err := targetPath(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
targetInfo, err := os.Lstat(target)
|
|
if repair && errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !targetInfo.Mode().IsRegular() {
|
|
if repair {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("hardlink target is not a regular file: %s", target)
|
|
}
|
|
sourceKey, sourceLinks, err := inodeIdentity(sourceInfo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
targetKey, targetLinks, err := inodeIdentity(targetInfo)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !repair && sourceLinks > 1 {
|
|
same, err := index.observe("source", sourceKey, targetKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !same {
|
|
return fmt.Errorf("source hardlink group was split at %s", target)
|
|
}
|
|
}
|
|
if targetLinks > 1 {
|
|
same, err := index.observe("target", targetKey, sourceKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !same {
|
|
if !repair {
|
|
return fmt.Errorf("unrelated source files became hardlinked at %s", target)
|
|
}
|
|
if err := os.Remove(target); err != nil {
|
|
return fmt.Errorf("break obsolete target hardlink: %w", err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
return walkTree(ctx, src, visitDir, visitFile)
|
|
}
|