Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
561 lines
13 KiB
Go
561 lines
13 KiB
Go
//go:build ceph && cgo
|
|
|
|
// Package cephfsnative provides a small binding to the installed libcephfs ABI.
|
|
// Client handles are userspace connections, not operating-system/FUSE mounts.
|
|
package cephfsnative
|
|
|
|
/*
|
|
#cgo CFLAGS: -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
|
|
#cgo LDFLAGS: -lcephfs
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <cephfs/libcephfs.h>
|
|
|
|
static int cm_stat(struct ceph_mount_info *m, const char *p, struct ceph_statx *s, int flags)
|
|
{
|
|
return ceph_statx(m, p, s, CEPH_STATX_BASIC_STATS | CEPH_STATX_VERSION, flags);
|
|
}
|
|
|
|
static int cm_attr(struct ceph_mount_info *m, const char *p, unsigned mode, unsigned uid,
|
|
unsigned gid, long sec, long nsec)
|
|
{
|
|
struct ceph_statx s = {0};
|
|
s.stx_mode = mode;
|
|
s.stx_uid = uid;
|
|
s.stx_gid = gid;
|
|
s.stx_mtime.tv_sec = sec;
|
|
s.stx_mtime.tv_nsec = nsec;
|
|
|
|
int mask = CEPH_SETATTR_UID | CEPH_SETATTR_GID | CEPH_SETATTR_MTIME;
|
|
if ((mode & S_IFMT) != S_IFLNK)
|
|
mask |= CEPH_SETATTR_MODE;
|
|
return ceph_setattrx(m, p, &s, mask, AT_SYMLINK_NOFOLLOW);
|
|
}
|
|
|
|
// ceph_link resolves a symbolic-link source. Use inode-based linking so the
|
|
// link itself, including a dangling symlink, is shared instead of its referent.
|
|
static int cm_link_nofollow(struct ceph_mount_info *m, const char *oldpath,
|
|
const char *newparent, const char *name)
|
|
{
|
|
const UserPerm *perms = ceph_mount_perms(m);
|
|
Inode *in = NULL, *parent = NULL;
|
|
struct ceph_statx s = {0};
|
|
|
|
int ret = ceph_ll_walk(m, oldpath, &in, &s, CEPH_STATX_BASIC_STATS, AT_SYMLINK_NOFOLLOW, perms);
|
|
if (ret < 0)
|
|
return ret;
|
|
ret = ceph_ll_walk(m, newparent, &parent, &s, CEPH_STATX_BASIC_STATS, 0, perms);
|
|
if (ret == 0)
|
|
ret = ceph_ll_link(m, in, parent, name, perms);
|
|
if (parent)
|
|
ceph_ll_put(m, parent);
|
|
ceph_ll_put(m, in);
|
|
return ret;
|
|
}
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"path"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
// Client is a libcephfs connection that implements storage.FS.
|
|
type Client struct{ mount *C.struct_ceph_mount_info }
|
|
|
|
func errno(ret C.int) error {
|
|
if ret < 0 {
|
|
return syscall.Errno(-ret)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func str(s string) (*C.char, func()) {
|
|
p := C.CString(s)
|
|
return p, func() { C.free(unsafe.Pointer(p)) }
|
|
}
|
|
|
|
// Open connects to the cluster described by o and mounts the root of the
|
|
// selected filesystem in userspace.
|
|
func Open(o storage.Options) (*Client, error) {
|
|
c := &Client{}
|
|
user := strings.TrimPrefix(o.User, "client.")
|
|
if user == "" {
|
|
user = "admin"
|
|
}
|
|
|
|
id, free := str(user)
|
|
defer free()
|
|
if err := errno(C.ceph_create(&c.mount, id)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
failed := true
|
|
defer func() {
|
|
if failed {
|
|
c.Close()
|
|
}
|
|
}()
|
|
|
|
conf, release := str(o.Config)
|
|
defer release()
|
|
if err := errno(C.ceph_conf_read_file(c.mount, conf)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
settings := map[string]string{"keyring": o.Keyring, "client_mount_timeout": "30",
|
|
"client_acl_type": "posix_acl", "client_oc": "false"}
|
|
|
|
for k, v := range settings {
|
|
key, fk := str(k)
|
|
value, fv := str(v)
|
|
ret := C.ceph_conf_set(c.mount, key, value)
|
|
fk()
|
|
fv()
|
|
if err := errno(ret); err != nil {
|
|
return nil, fmt.Errorf("Ceph option %s: %w", k, err)
|
|
}
|
|
}
|
|
|
|
fs, ff := str(o.Filesystem)
|
|
defer ff()
|
|
if err := errno(C.ceph_select_filesystem(c.mount, fs)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
root, fr := str("/")
|
|
defer fr()
|
|
if err := errno(C.ceph_mount(c.mount, root)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
failed = false
|
|
return c, nil
|
|
}
|
|
|
|
// Close unmounts and releases the connection. It is safe to call twice.
|
|
func (c *Client) Close() error {
|
|
if c.mount != nil {
|
|
C.ceph_shutdown(c.mount)
|
|
c.mount = nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func node(s C.struct_ceph_statx) storage.Node {
|
|
return storage.Node{Mode: uint32(s.stx_mode), Inode: uint64(s.stx_ino),
|
|
Version: uint64(s.stx_version), Nlink: uint64(s.stx_nlink), Size: uint64(s.stx_size),
|
|
Rdev: uint64(s.stx_rdev), UID: uint32(s.stx_uid), GID: uint32(s.stx_gid),
|
|
Mtime: time.Unix(int64(s.stx_mtime.tv_sec), int64(s.stx_mtime.tv_nsec))}
|
|
}
|
|
|
|
// Stat returns the attributes of path without following a final symlink.
|
|
func (c *Client) Stat(path string) (storage.Node, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
var s C.struct_ceph_statx
|
|
err := errno(C.cm_stat(c.mount, p, &s, nativeStatFlags(path)))
|
|
return node(s), err
|
|
}
|
|
|
|
func nativeStatFlags(path string) C.int {
|
|
flags := C.int(C.AT_SYMLINK_NOFOLLOW)
|
|
// Snapshot inodes are immutable and separately cached by snapshot ID. As in
|
|
// cephfs-mirror, avoid acquiring all shared metadata locks again for them.
|
|
// Live target paths retain normal synchronization semantics.
|
|
if strings.Contains(path, "/.snap/") {
|
|
flags |= C.AT_STATX_DONT_SYNC
|
|
}
|
|
|
|
return flags
|
|
}
|
|
|
|
// Open opens the file at path with open(2) flags and mode.
|
|
func (c *Client) Open(path string, flags int, mode uint32) (storage.File, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
ret := C.ceph_open(c.mount, p, C.int(flags), C.mode_t(mode))
|
|
if err := errno(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &file{client: c, fd: ret}, nil
|
|
}
|
|
|
|
type file struct {
|
|
client *Client
|
|
fd C.int
|
|
closed bool
|
|
}
|
|
|
|
func (f *file) Close() error {
|
|
if f.closed {
|
|
return nil
|
|
}
|
|
|
|
f.closed = true
|
|
return errno(C.ceph_close(f.client.mount, f.fd))
|
|
}
|
|
|
|
func (f *file) ReadAt(b []byte, off int64) (int, error) {
|
|
if len(b) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
n := C.ceph_read(f.client.mount, f.fd, (*C.char)(unsafe.Pointer(&b[0])), C.int64_t(len(b)), C.int64_t(off))
|
|
if err := errno(n); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if int(n) < len(b) {
|
|
return int(n), io.EOF
|
|
}
|
|
|
|
return int(n), nil
|
|
}
|
|
|
|
func (f *file) WriteAt(b []byte, off int64) (int, error) {
|
|
if len(b) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
n := C.ceph_write(f.client.mount, f.fd, (*C.char)(unsafe.Pointer(&b[0])), C.int64_t(len(b)), C.int64_t(off))
|
|
if err := errno(n); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if int(n) != len(b) {
|
|
return int(n), io.ErrShortWrite
|
|
}
|
|
|
|
return int(n), nil
|
|
}
|
|
|
|
func (f *file) Truncate(n int64) error {
|
|
return errno(C.ceph_ftruncate(f.client.mount, f.fd, C.int64_t(n)))
|
|
}
|
|
|
|
func (f *file) Sync() error { return errno(C.ceph_fsync(f.client.mount, f.fd, 0)) }
|
|
|
|
func (f *file) Lock(owner uint64) error {
|
|
return errno(C.ceph_flock(f.client.mount, f.fd, C.LOCK_EX|C.LOCK_NB, C.uint64_t(owner)))
|
|
}
|
|
|
|
func (f *file) PunchHole(off, length int64) error {
|
|
return errno(C.ceph_fallocate(f.client.mount, f.fd, 3, C.int64_t(off), C.int64_t(length)))
|
|
}
|
|
|
|
type dir struct {
|
|
client *Client
|
|
handle *C.struct_ceph_dir_result
|
|
path string
|
|
flags C.int
|
|
}
|
|
|
|
// OpenDir opens the directory at path for iteration.
|
|
func (c *Client) OpenDir(path string) (storage.DirectoryStream, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
d := &dir{client: c, path: path, flags: nativeStatFlags(path)}
|
|
if err := errno(C.ceph_opendir(c.mount, p, &d.handle)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func (d *dir) Next() (*storage.Entry, error) {
|
|
for {
|
|
var entry C.struct_dirent
|
|
var s C.struct_ceph_statx
|
|
ret := C.ceph_readdirplus_r(d.client.mount, d.handle, &entry, &s,
|
|
C.CEPH_STATX_BASIC_STATS|C.CEPH_STATX_VERSION, C.uint(d.flags), nil)
|
|
|
|
if err := errno(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if ret == 0 {
|
|
return nil, io.EOF
|
|
}
|
|
|
|
name := C.GoString(&entry.d_name[0])
|
|
if name == "." || name == ".." || name == ".snap" {
|
|
continue
|
|
}
|
|
|
|
return &storage.Entry{Name: name, Node: node(s)}, nil
|
|
}
|
|
}
|
|
|
|
func (d *dir) Close() error { return errno(C.ceph_closedir(d.client.mount, d.handle)) }
|
|
|
|
// Mkdir creates a directory.
|
|
func (c *Client) Mkdir(path string, mode uint32) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
return errno(C.ceph_mkdir(c.mount, p, C.mode_t(mode)))
|
|
}
|
|
|
|
// Unlink removes a non-directory entry.
|
|
func (c *Client) Unlink(path string) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
return errno(C.ceph_unlink(c.mount, p))
|
|
}
|
|
|
|
// Rmdir removes an empty directory.
|
|
func (c *Client) Rmdir(path string) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
return errno(C.ceph_rmdir(c.mount, p))
|
|
}
|
|
|
|
// Rename renames a to b.
|
|
func (c *Client) Rename(a, b string) error {
|
|
p, f := str(a)
|
|
defer f()
|
|
q, g := str(b)
|
|
defer g()
|
|
return errno(C.ceph_rename(c.mount, p, q))
|
|
}
|
|
|
|
// Link creates the hard link b to a without following a symlink at a.
|
|
func (c *Client) Link(a, b string) error {
|
|
p, f := str(a)
|
|
defer f()
|
|
q, g := str(path.Dir(b))
|
|
defer g()
|
|
name, h := str(path.Base(b))
|
|
defer h()
|
|
return errno(C.cm_link_nofollow(c.mount, p, q, name))
|
|
}
|
|
|
|
// Symlink creates the symbolic link b pointing to a.
|
|
func (c *Client) Symlink(a, b string) error {
|
|
p, f := str(a)
|
|
defer f()
|
|
q, g := str(b)
|
|
defer g()
|
|
return errno(C.ceph_symlink(c.mount, p, q))
|
|
}
|
|
|
|
// Mknod creates a special file.
|
|
func (c *Client) Mknod(path string, mode uint32, dev uint64) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
return errno(C.ceph_mknod(c.mount, p, C.mode_t(mode), C.dev_t(dev)))
|
|
}
|
|
|
|
// Readlink returns the target of a symbolic link.
|
|
func (c *Client) Readlink(path string) (string, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
for size := 256; size <= 1<<20; size *= 2 {
|
|
b := make([]byte, size)
|
|
n := C.ceph_readlink(c.mount, p, (*C.char)(unsafe.Pointer(&b[0])), C.int64_t(size))
|
|
if err := errno(n); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if int(n) < size {
|
|
return string(b[:int(n)]), nil
|
|
}
|
|
}
|
|
|
|
return "", errors.New("symlink target exceeds 1 MiB")
|
|
}
|
|
|
|
// SetAttr sets numeric owner, mode and mtime without following symlinks.
|
|
func (c *Client) SetAttr(path string, n storage.Node) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
return errno(C.cm_attr(c.mount, p, C.uint(n.Mode), C.uint(n.UID), C.uint(n.GID), C.long(n.Mtime.Unix()), C.long(n.Mtime.Nanosecond())))
|
|
}
|
|
|
|
// GetXattr returns the value of an extended attribute.
|
|
func (c *Client) GetXattr(path, key string) ([]byte, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
k, g := str(key)
|
|
defer g()
|
|
for size := 256; size <= 1<<20; size *= 2 {
|
|
b := make([]byte, size)
|
|
ret := C.ceph_lgetxattr(c.mount, p, k, unsafe.Pointer(&b[0]), C.size_t(size))
|
|
if ret == -C.ERANGE {
|
|
continue
|
|
}
|
|
|
|
if err := errno(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return b[:int(ret)], nil
|
|
}
|
|
|
|
return nil, errors.New("xattr exceeds 1 MiB")
|
|
}
|
|
|
|
// ListXattr returns the names of all extended attributes of path.
|
|
func (c *Client) ListXattr(path string) ([]string, error) {
|
|
p, f := str(path)
|
|
defer f()
|
|
for size := 4096; size <= 1<<20; size *= 2 {
|
|
b := make([]byte, size)
|
|
ret := C.ceph_llistxattr(c.mount, p, (*C.char)(unsafe.Pointer(&b[0])), C.size_t(size))
|
|
if ret == -C.ERANGE {
|
|
continue
|
|
}
|
|
|
|
if err := errno(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var names []string
|
|
for _, s := range strings.Split(string(b[:int(ret)]), "\x00") {
|
|
if s != "" {
|
|
names = append(names, s)
|
|
}
|
|
}
|
|
|
|
return names, nil
|
|
}
|
|
|
|
return nil, errors.New("xattr list exceeds 1 MiB")
|
|
}
|
|
|
|
// SetXattr sets an extended attribute.
|
|
func (c *Client) SetXattr(path, key string, b []byte) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
k, g := str(key)
|
|
defer g()
|
|
var ptr unsafe.Pointer
|
|
if len(b) > 0 {
|
|
ptr = unsafe.Pointer(&b[0])
|
|
}
|
|
|
|
return errno(C.ceph_lsetxattr(c.mount, p, k, ptr, C.size_t(len(b)), 0))
|
|
}
|
|
|
|
// RemoveXattr removes an extended attribute.
|
|
func (c *Client) RemoveXattr(path, key string) error {
|
|
p, f := str(path)
|
|
defer f()
|
|
k, g := str(key)
|
|
defer g()
|
|
return errno(C.ceph_lremovexattr(c.mount, p, k))
|
|
}
|
|
|
|
// Sync flushes all dirty data and metadata of the filesystem.
|
|
func (c *Client) Sync() error { return errno(C.ceph_sync_fs(c.mount)) }
|
|
|
|
type snapDiff struct{ info C.struct_ceph_snapdiff_info }
|
|
|
|
// SnapDiff lists the entries below root/rel that differ between
|
|
// snapshots a and b.
|
|
func (c *Client) SnapDiff(root, rel, a, b string) (storage.DiffStream, error) {
|
|
p, f := str(root)
|
|
defer f()
|
|
q, g := str(rel)
|
|
defer g()
|
|
x, h := str(a)
|
|
defer h()
|
|
y, j := str(b)
|
|
defer j()
|
|
s := &snapDiff{}
|
|
if err := errno(C.ceph_open_snapdiff(c.mount, p, q, x, y, &s.info)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (s *snapDiff) Next() (string, error) {
|
|
for {
|
|
var entry C.struct_ceph_snapdiff_entry_t
|
|
ret := C.ceph_readdir_snapdiff(&s.info, &entry)
|
|
if err := errno(ret); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if ret == 0 {
|
|
return "", io.EOF
|
|
}
|
|
|
|
name := C.GoString(&entry.dir_entry.d_name[0])
|
|
if name == "." || name == ".." || name == ".snap" {
|
|
continue
|
|
}
|
|
|
|
return name, nil
|
|
}
|
|
}
|
|
|
|
func (s *snapDiff) Close() error { return errno(C.ceph_close_snapdiff(&s.info)) }
|
|
|
|
type blockDiff struct {
|
|
info C.struct_ceph_file_blockdiff_info
|
|
done bool
|
|
}
|
|
|
|
// BlockDiff lists the changed extents of the file root/rel between
|
|
// snapshots a and b.
|
|
func (c *Client) BlockDiff(root, rel, a, b string) (storage.BlockStream, error) {
|
|
p, f := str(root)
|
|
defer f()
|
|
q, g := str(rel)
|
|
defer g()
|
|
x, h := str(a)
|
|
defer h()
|
|
y, j := str(b)
|
|
defer j()
|
|
s := &blockDiff{}
|
|
if err := errno(C.ceph_file_blockdiff_init(c.mount, p, q, x, y, &s.info)); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (s *blockDiff) Next() ([]storage.Extent, error) {
|
|
if s.done {
|
|
return nil, io.EOF
|
|
}
|
|
|
|
var blocks C.struct_ceph_file_blockdiff_changedblocks
|
|
ret := C.ceph_file_blockdiff(&s.info, &blocks)
|
|
if err := errno(ret); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer C.ceph_free_file_blockdiff_buffer(&blocks)
|
|
s.done = ret == 0
|
|
if uint64(blocks.num_blocks) > 1<<20 {
|
|
return nil, errors.New("blockdiff batch exceeds supported bound")
|
|
}
|
|
|
|
cblocks := unsafe.Slice(blocks.b, int(blocks.num_blocks))
|
|
out := make([]storage.Extent, len(cblocks))
|
|
for i, b := range cblocks {
|
|
out[i] = storage.Extent{Offset: uint64(b.offset), Length: uint64(b.len)}
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func (s *blockDiff) Close() error { return errno(C.ceph_file_blockdiff_finish(&s.info)) }
|