2015-05-01 19:35:40 +00:00
|
|
|
package cephfs
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -lcephfs
|
|
|
|
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cephfs/libcephfs.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2018-10-09 04:26:51 +00:00
|
|
|
import (
|
|
|
|
"unsafe"
|
2019-12-12 18:27:28 +00:00
|
|
|
|
2019-12-18 22:27:12 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2018-10-09 04:26:51 +00:00
|
|
|
)
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// MountInfo exports ceph's ceph_mount_info from libcephfs.cc
|
2015-05-01 19:35:40 +00:00
|
|
|
type MountInfo struct {
|
2015-07-08 08:34:54 +00:00
|
|
|
mount *C.struct_ceph_mount_info
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 20:11:50 +00:00
|
|
|
func createMount(id *C.char) (*MountInfo, error) {
|
2015-07-08 08:34:54 +00:00
|
|
|
mount := &MountInfo{}
|
2019-12-19 20:11:50 +00:00
|
|
|
ret := C.ceph_create(&mount.mount, id)
|
2018-10-11 18:36:26 +00:00
|
|
|
if ret != 0 {
|
2019-12-12 18:54:55 +00:00
|
|
|
return nil, getError(ret)
|
2015-07-08 08:34:54 +00:00
|
|
|
}
|
2018-10-11 18:36:26 +00:00
|
|
|
return mount, nil
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 20:11:50 +00:00
|
|
|
// CreateMount creates a mount handle for interacting with Ceph.
|
|
|
|
func CreateMount() (*MountInfo, error) {
|
|
|
|
return createMount(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMountWithId creates a mount handle for interacting with Ceph.
|
|
|
|
// The caller can specify a unique id that will identify this client.
|
|
|
|
func CreateMountWithId(id string) (*MountInfo, error) {
|
|
|
|
cid := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
|
|
|
return createMount(cid)
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:27:12 +00:00
|
|
|
// CreateFromRados creates a mount handle using an existing rados cluster
|
|
|
|
// connection.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_create_from_rados(struct ceph_mount_info **cmount, rados_t cluster);
|
|
|
|
func CreateFromRados(conn *rados.Conn) (*MountInfo, error) {
|
|
|
|
mount := &MountInfo{}
|
|
|
|
ret := C.ceph_create_from_rados(&mount.mount, C.rados_t(conn.Cluster()))
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
return mount, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// ReadDefaultConfigFile loads the ceph configuration from the specified config file.
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) ReadDefaultConfigFile() error {
|
|
|
|
ret := C.ceph_conf_read_file(mount.mount, nil)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-10 22:06:47 +00:00
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2020-03-04 18:44:42 +00:00
|
|
|
// SetConfigOption sets the value of the configuration option identified by
|
|
|
|
// the given name.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_conf_set(struct ceph_mount_info *cmount, const char *option, const char *value);
|
|
|
|
func (mount *MountInfo) SetConfigOption(option, value string) error {
|
|
|
|
cOption := C.CString(option)
|
|
|
|
defer C.free(unsafe.Pointer(cOption))
|
|
|
|
cValue := C.CString(value)
|
|
|
|
defer C.free(unsafe.Pointer(cValue))
|
|
|
|
return getError(C.ceph_conf_set(mount.mount, cOption, cValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigOption returns the value of the Ceph configuration option
|
|
|
|
// identified by the given name.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_conf_get(struct ceph_mount_info *cmount, const char *option, char *buf, size_t len);
|
|
|
|
func (mount *MountInfo) GetConfigOption(option string) (string, error) {
|
|
|
|
cOption := C.CString(option)
|
|
|
|
defer C.free(unsafe.Pointer(cOption))
|
|
|
|
buf := make([]byte, 4096)
|
|
|
|
// TODO: handle ENAMETOOLONG cases. problem also exists in rados
|
|
|
|
ret := C.ceph_conf_get(
|
|
|
|
mount.mount,
|
|
|
|
cOption,
|
|
|
|
(*C.char)(unsafe.Pointer(&buf[0])),
|
|
|
|
C.size_t(len(buf)))
|
|
|
|
if ret < 0 {
|
|
|
|
return "", getError(ret)
|
|
|
|
}
|
|
|
|
value := C.GoString((*C.char)(unsafe.Pointer(&buf[0])))
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:54:59 +00:00
|
|
|
// Mount the file system, establishing a connection capable of I/O.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_mount(struct ceph_mount_info *cmount, const char *root);
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) Mount() error {
|
|
|
|
ret := C.ceph_mount(mount.mount, nil)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 22:18:01 +00:00
|
|
|
// MountWithRoot mounts the file system using the path provided for the root of
|
|
|
|
// the mount. This establishes a connection capable of I/O.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_mount(struct ceph_mount_info *cmount, const char *root);
|
|
|
|
func (mount *MountInfo) MountWithRoot(root string) error {
|
|
|
|
croot := C.CString(root)
|
|
|
|
defer C.free(unsafe.Pointer(croot))
|
|
|
|
return getError(C.ceph_mount(mount.mount, croot))
|
|
|
|
}
|
|
|
|
|
2020-02-13 19:54:59 +00:00
|
|
|
// Unmount the file system.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_unmount(struct ceph_mount_info *cmount);
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Unmount() error {
|
|
|
|
ret := C.ceph_unmount(mount.mount)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Release destroys the mount handle.
|
2020-02-13 19:54:59 +00:00
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_release(struct ceph_mount_info *cmount);
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Release() error {
|
2020-03-23 21:00:29 +00:00
|
|
|
if mount.mount == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
ret := C.ceph_release(mount.mount)
|
2020-03-23 21:00:29 +00:00
|
|
|
if err := getError(ret); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mount.mount = nil
|
|
|
|
return nil
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// SyncFs synchronizes all filesystem data to persistent media.
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) SyncFs() error {
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_sync_fs(mount.mount)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// CurrentDir gets the current working directory.
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) CurrentDir() string {
|
2018-10-11 18:36:26 +00:00
|
|
|
cDir := C.ceph_getcwd(mount.mount)
|
|
|
|
return C.GoString(cDir)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// ChangeDir changes the current working directory.
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) ChangeDir(path string) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chdir(mount.mount, cPath)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
2015-05-01 19:54:38 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// MakeDir creates a directory.
|
2015-05-01 19:54:38 +00:00
|
|
|
func (mount *MountInfo) MakeDir(path string, mode uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2015-05-01 19:54:38 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_mkdir(mount.mount, cPath, C.mode_t(mode))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-10 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// RemoveDir removes a directory.
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) RemoveDir(path string) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-10 22:06:47 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_rmdir(mount.mount, cPath)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:54:38 +00:00
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Chmod changes the mode bits (permissions) of a file/directory.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Chmod(path string, mode uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chmod(mount.mount, cPath, C.mode_t(mode))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Chown changes the ownership of a file/directory.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Chown(path string, user uint32, group uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chown(mount.mount, cPath, C.int(user), C.int(group))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// IsMounted checks mount status.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) IsMounted() bool {
|
|
|
|
ret := C.ceph_is_mounted(mount.mount)
|
2018-10-10 22:06:47 +00:00
|
|
|
return ret == 1
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
2020-01-28 18:09:29 +00:00
|
|
|
|
|
|
|
// MdsCommand sends commands to the specified MDS.
|
|
|
|
func (mount *MountInfo) MdsCommand(mdsSpec string, args [][]byte) ([]byte, string, error) {
|
|
|
|
return mount.mdsCommand(mdsSpec, args, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MdsCommandWithInputBuffer sends commands to the specified MDS, with an input
|
|
|
|
// buffer.
|
|
|
|
func (mount *MountInfo) MdsCommandWithInputBuffer(mdsSpec string, args [][]byte, inputBuffer []byte) ([]byte, string, error) {
|
|
|
|
return mount.mdsCommand(mdsSpec, args, inputBuffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mdsCommand supports sending formatted commands to MDS.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_mds_command(struct ceph_mount_info *cmount,
|
|
|
|
// const char *mds_spec,
|
|
|
|
// const char **cmd,
|
|
|
|
// size_t cmdlen,
|
|
|
|
// const char *inbuf, size_t inbuflen,
|
|
|
|
// char **outbuf, size_t *outbuflen,
|
|
|
|
// char **outs, size_t *outslen);
|
|
|
|
func (mount *MountInfo) mdsCommand(mdsSpec string, args [][]byte, inputBuffer []byte) (buffer []byte, info string, err error) {
|
|
|
|
spec := C.CString(mdsSpec)
|
|
|
|
defer C.free(unsafe.Pointer(spec))
|
|
|
|
|
|
|
|
argc := len(args)
|
|
|
|
argv := make([]*C.char, argc)
|
|
|
|
|
|
|
|
for i, arg := range args {
|
|
|
|
argv[i] = C.CString(string(arg))
|
|
|
|
}
|
|
|
|
// free all array elements in a single defer
|
|
|
|
defer func() {
|
|
|
|
for i := range argv {
|
|
|
|
C.free(unsafe.Pointer(argv[i]))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
outs, outbuf *C.char
|
|
|
|
outslen, outbuflen C.size_t
|
|
|
|
)
|
|
|
|
inbuf := C.CString(string(inputBuffer))
|
|
|
|
inbufLen := len(inputBuffer)
|
|
|
|
defer C.free(unsafe.Pointer(inbuf))
|
|
|
|
|
|
|
|
ret := C.ceph_mds_command(
|
|
|
|
mount.mount, // cephfs mount ref
|
|
|
|
spec, // mds spec
|
|
|
|
&argv[0], // cmd array
|
|
|
|
C.size_t(argc), // cmd array length
|
|
|
|
inbuf, // bulk input
|
|
|
|
C.size_t(inbufLen), // length inbuf
|
|
|
|
&outbuf, // buffer
|
|
|
|
&outbuflen, // buffer length
|
|
|
|
&outs, // status string
|
|
|
|
&outslen)
|
|
|
|
|
|
|
|
if outslen > 0 {
|
|
|
|
info = C.GoStringN(outs, C.int(outslen))
|
|
|
|
C.free(unsafe.Pointer(outs))
|
|
|
|
}
|
|
|
|
if outbuflen > 0 {
|
|
|
|
buffer = C.GoBytes(unsafe.Pointer(outbuf), C.int(outbuflen))
|
|
|
|
C.free(unsafe.Pointer(outbuf))
|
|
|
|
}
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, info, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer, info, nil
|
|
|
|
}
|