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>
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package migration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// Endpoint describes how to reach one side of a migration: the Ceph
|
|
// client identity and configuration and the subvolume.
|
|
type Endpoint struct {
|
|
User string `json:"user,omitempty"`
|
|
Config string `json:"config"`
|
|
Keyring string `json:"keyring"`
|
|
Filesystem string `json:"filesystem"`
|
|
Group string `json:"group"`
|
|
Subvolume string `json:"subvolume"`
|
|
}
|
|
|
|
// Config is the JSON migration configuration read by ReadConfig.
|
|
// Backend is optional; the only backend is "native" (libcephfs).
|
|
type Config struct {
|
|
Backend string `json:"backend,omitempty"`
|
|
Workers int `json:"workers,omitempty"`
|
|
Source Endpoint `json:"source"`
|
|
Target Endpoint `json:"target"`
|
|
BandwidthKiB int64 `json:"bandwidth_kib,omitempty"`
|
|
}
|
|
|
|
// ReadConfig reads exactly one JSON object from the regular file at path,
|
|
// rejects unknown fields and returns the validated configuration.
|
|
func ReadConfig(path string) (Config, error) {
|
|
var c Config
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
|
|
defer f.Close()
|
|
st, err := f.Stat()
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
|
|
if !st.Mode().IsRegular() || st.Size() > 1<<20 {
|
|
return c, errors.New("config must be a regular file of at most 1 MiB")
|
|
}
|
|
|
|
dec := json.NewDecoder(io.LimitReader(f, 1<<20))
|
|
dec.DisallowUnknownFields()
|
|
if err = dec.Decode(&c); err != nil {
|
|
return c, fmt.Errorf("config: %w", err)
|
|
}
|
|
|
|
if err = dec.Decode(new(any)); !errors.Is(err, io.EOF) {
|
|
return c, errors.New("config must contain exactly one JSON object")
|
|
}
|
|
|
|
return c, c.Validate()
|
|
}
|
|
|
|
func validName(s string) bool {
|
|
return s != "" && s != "." && s != ".." && !strings.HasPrefix(s, "-") &&
|
|
!strings.ContainsAny(s, "/\x00\n\r")
|
|
}
|
|
|
|
// Validate checks names, paths, worker and bandwidth limits and reports
|
|
// the first problem found.
|
|
func (c Config) Validate() error {
|
|
if c.Backend != "" && c.Backend != "native" {
|
|
return errors.New(`backend must be "native"; the rsync backend was removed`)
|
|
}
|
|
|
|
if c.Workers < 0 || c.Workers > 64 {
|
|
return errors.New("workers must be between 0 and 64 (0 selects the default)")
|
|
}
|
|
|
|
for _, p := range []struct {
|
|
name string
|
|
e Endpoint
|
|
}{{"source", c.Source}, {"target", c.Target}} {
|
|
if p.e.User != "" && (!strings.HasPrefix(p.e.User, "client.") || !validName(p.e.User)) {
|
|
return fmt.Errorf("%s: user must be a client identity", p.name)
|
|
}
|
|
|
|
for _, s := range []string{p.e.Filesystem, p.e.Subvolume} {
|
|
if !validName(s) {
|
|
return fmt.Errorf("%s: invalid filesystem or subvolume name", p.name)
|
|
}
|
|
}
|
|
|
|
if p.e.Group != "" && !validName(p.e.Group) {
|
|
return fmt.Errorf("%s: invalid group", p.name)
|
|
}
|
|
|
|
for _, path := range []string{p.e.Config, p.e.Keyring} {
|
|
if !filepath.IsAbs(path) || filepath.Clean(path) != path {
|
|
return fmt.Errorf("%s: paths must be absolute and clean: %q", p.name, path)
|
|
}
|
|
}
|
|
}
|
|
|
|
if c.BandwidthKiB < 0 || c.BandwidthKiB > (1<<63-1)/1024 {
|
|
return errors.New("bandwidth_kib must be nonnegative and fit in bytes per second")
|
|
}
|
|
|
|
return nil
|
|
}
|