Files
CephfsMig/internal/migration/linux.go
T
Sebastian UnterschützandClaude Opus 5.5 c2ef7a6c3b docs: add package and doc comments for exported identifiers
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>
2026-12-03 18:07:48 +01:00

235 lines
5.6 KiB
Go

//go:build linux
package migration
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"syscall"
)
func unescapeMount(s string) string {
return strings.NewReplacer("\\040", " ", "\\011", "\t", "\\012", "\n", "\\134", "\\").Replace(s)
}
func privateDirectories(path string) error {
if !filepath.IsAbs(path) {
return errors.New("directory path must be absolute")
}
current := "/"
for _, component := range strings.Split(strings.TrimPrefix(filepath.Clean(path), "/"), "/") {
if component == "" {
continue
}
current = filepath.Join(current, component)
if err := os.Mkdir(current, 0700); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
st, err := os.Lstat(current)
if err != nil {
return err
}
if !st.IsDir() {
return fmt.Errorf("directory contains a symlink or non-directory: %s", current)
}
}
return nil
}
func checkMount(root string, writable bool) error {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return err
}
defer f.Close()
scan := bufio.NewScanner(f)
found := false
for scan.Scan() {
parts := strings.SplitN(scan.Text(), " - ", 2)
if len(parts) != 2 {
continue
}
fields, after := strings.Fields(parts[0]), strings.Fields(parts[1])
if len(fields) < 6 || len(after) < 2 {
continue
}
mount := unescapeMount(fields[4])
if mount == root {
if after[0] != "ceph" && after[0] != "fuse.ceph-fuse" {
return fmt.Errorf("%s is not a CephFS mount", root)
}
if writable && !strings.Contains(","+fields[5]+",", ",rw,") {
return fmt.Errorf("target %s must be mounted read-write", root)
}
found = true
} else if beneath(root, mount) {
return fmt.Errorf("nested mount %s below %s is unsupported", mount, root)
}
}
if err := scan.Err(); err != nil {
return err
}
if !found {
return fmt.Errorf("%s must be a dedicated CephFS filesystem-root mount", root)
}
resolved, err := filepath.EvalSymlinks(root)
if err != nil {
return err
}
if resolved != root {
return errors.New("mount path must not contain symlinks")
}
return nil
}
func xattr(path, key string) ([]byte, error) {
for attempt := 0; attempt < 3; attempt++ {
n, err := syscall.Getxattr(path, key, nil)
if errors.Is(err, syscall.ENODATA) {
return []byte("0"), nil
}
if err != nil {
return nil, err
}
if n > 1<<20 {
return nil, fmt.Errorf("xattr too large: %s", key)
}
b := make([]byte, n)
n, err = syscall.Getxattr(path, key, b)
if errors.Is(err, syscall.ERANGE) {
continue
}
if err != nil {
return nil, err
}
return b[:n], nil
}
return nil, errors.New("xattr changed repeatedly")
}
var quotaKeys = []string{"ceph.quota.max_bytes", "ceph.quota.max_files"}
// walkDirectories calls visit for every directory below root.
func walkDirectories(ctx context.Context, root string, visit func(string) error) error {
return walkTree(ctx, root, visit, nil)
}
// walkTree walks root depth-first, skipping .snap. ReadDir batches keep a
// single very large directory from exhausting memory.
func walkTree(ctx context.Context, root string, visitDir func(string) error, visitFile func(string, os.FileInfo) error) error {
var walk func(string, int) error
walk = func(path string, depth int) error {
if err := ctx.Err(); err != nil {
return err
}
if depth > 128 {
return errors.New("directory depth exceeds supported limit (128)")
}
if visitDir != nil {
if err := visitDir(path); errors.Is(err, fs.SkipDir) {
return nil
} else if err != nil {
return err
}
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
for {
batch, readErr := f.ReadDir(128)
for _, e := range batch {
if err := ctx.Err(); err != nil {
return err
}
if e.Name() == ".snap" {
continue
}
child := filepath.Join(path, e.Name())
if e.IsDir() {
if err := walk(child, depth+1); err != nil {
return err
}
} else if visitFile != nil {
info, err := e.Info()
if err != nil {
return err
}
if err = visitFile(child, info); err != nil {
return err
}
}
}
if errors.Is(readErr, io.EOF) {
return nil
}
if readErr != nil {
return readErr
}
}
}
return walk(root, 0)
}
func clearQuotas(ctx context.Context, root string) error {
return walkDirectories(ctx, root, func(path string) error {
for _, key := range quotaKeys {
old, err := xattr(path, key)
if err != nil {
return fmt.Errorf("read quota %s: %w", path, err)
}
if string(old) == "0" {
continue
}
if err := syscall.Setxattr(path, key, []byte("0"), 0); err != nil {
return fmt.Errorf("clear quota %s: %w", path, err)
}
}
return nil
})
}
func quotas(ctx context.Context, src, dst string, verify bool) error {
return walkDirectories(ctx, src, func(path string) error {
rel, err := filepath.Rel(src, path)
if err != nil {
return err
}
target := filepath.Join(dst, rel)
st, err := os.Lstat(target)
if err != nil {
return err
}
if !st.IsDir() {
return fmt.Errorf("quota target is not a real directory: %s", target)
}
for _, key := range quotaKeys {
value, err := xattr(path, key)
if err != nil {
return fmt.Errorf("source quota %s %s: %w", path, key, err)
}
actual, err := xattr(target, key)
if err != nil {
return fmt.Errorf("target quota %s %s: %w", target, key, err)
}
if string(value) == string(actual) {
continue
}
if verify {
return fmt.Errorf("quota mismatch: %s %s", rel, key)
}
if err := syscall.Setxattr(target, key, value, 0); err != nil {
return fmt.Errorf("set quota %s %s: %w", target, key, err)
}
}
return nil
})
}