2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux && cgo
|
2017-03-29 20:55:42 -04:00
|
|
|
// +build linux,cgo
|
2013-11-27 22:12:51 -05:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package devicemapper // import "github.com/docker/docker/pkg/devicemapper"
|
2013-11-13 17:36:31 -05:00
|
|
|
|
|
|
|
/*
|
2017-06-27 11:44:31 -04:00
|
|
|
#define _GNU_SOURCE
|
2013-11-13 17:36:31 -05:00
|
|
|
#include <libdevmapper.h>
|
2013-11-27 02:16:34 -05:00
|
|
|
#include <linux/fs.h> // FIXME: present only for BLKGETSIZE64, maybe we can remove it?
|
2013-11-13 17:36:31 -05:00
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
// FIXME: Can't we find a way to do the logging in pure Go?
|
2013-11-13 17:36:31 -05:00
|
|
|
extern void DevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str);
|
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
static void log_cb(int level, const char *file, int line, int dm_errno_or_class, const char *f, ...)
|
2013-11-13 17:36:31 -05:00
|
|
|
{
|
2017-06-27 11:44:31 -04:00
|
|
|
char *buffer = NULL;
|
|
|
|
va_list ap;
|
2017-07-17 20:40:29 -04:00
|
|
|
int ret;
|
2013-11-13 17:36:31 -05:00
|
|
|
|
2017-06-27 11:44:31 -04:00
|
|
|
va_start(ap, f);
|
2017-07-17 20:40:29 -04:00
|
|
|
ret = vasprintf(&buffer, f, ap);
|
2017-06-27 11:44:31 -04:00
|
|
|
va_end(ap);
|
2017-07-17 20:40:29 -04:00
|
|
|
if (ret < 0) {
|
|
|
|
// memory allocation failed -- should never happen?
|
|
|
|
return;
|
|
|
|
}
|
2013-11-13 17:36:31 -05:00
|
|
|
|
2017-06-27 11:44:31 -04:00
|
|
|
DevmapperLogCallback(level, (char *)file, line, dm_errno_or_class, buffer);
|
|
|
|
free(buffer);
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 18:35:52 -05:00
|
|
|
static void log_with_errno_init()
|
2013-11-13 17:36:31 -05:00
|
|
|
{
|
2017-06-27 11:44:31 -04:00
|
|
|
dm_log_with_errno_init(log_cb);
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2015-08-07 10:18:20 -04:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2013-11-13 17:36:31 -05:00
|
|
|
|
|
|
|
type (
|
2015-09-04 17:02:29 -04:00
|
|
|
cdmTask C.struct_dm_task
|
2013-11-13 17:36:31 -05:00
|
|
|
)
|
|
|
|
|
2013-11-28 14:53:09 -05:00
|
|
|
// IOCTL consts
|
2013-11-27 02:16:34 -05:00
|
|
|
const (
|
2013-11-28 14:53:09 -05:00
|
|
|
BlkGetSize64 = C.BLKGETSIZE64
|
2013-12-17 03:12:44 -05:00
|
|
|
BlkDiscard = C.BLKDISCARD
|
2013-11-27 02:16:34 -05:00
|
|
|
)
|
|
|
|
|
2015-09-30 04:35:02 -04:00
|
|
|
// Devicemapper cookie flags.
|
2014-10-30 20:26:39 -04:00
|
|
|
const (
|
|
|
|
DmUdevDisableSubsystemRulesFlag = C.DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG
|
|
|
|
DmUdevDisableDiskRulesFlag = C.DM_UDEV_DISABLE_DISK_RULES_FLAG
|
|
|
|
DmUdevDisableOtherRulesFlag = C.DM_UDEV_DISABLE_OTHER_RULES_FLAG
|
2015-01-19 15:52:39 -05:00
|
|
|
DmUdevDisableLibraryFallback = C.DM_UDEV_DISABLE_LIBRARY_FALLBACK
|
2014-10-30 20:26:39 -04:00
|
|
|
)
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
// DeviceMapper mapped functions.
|
2013-11-13 17:36:31 -05:00
|
|
|
var (
|
2015-04-21 18:14:59 -04:00
|
|
|
DmGetLibraryVersion = dmGetLibraryVersionFct
|
|
|
|
DmGetNextTarget = dmGetNextTargetFct
|
|
|
|
DmSetDevDir = dmSetDevDirFct
|
|
|
|
DmTaskAddTarget = dmTaskAddTargetFct
|
|
|
|
DmTaskCreate = dmTaskCreateFct
|
|
|
|
DmTaskDestroy = dmTaskDestroyFct
|
|
|
|
DmTaskGetDeps = dmTaskGetDepsFct
|
|
|
|
DmTaskGetInfo = dmTaskGetInfoFct
|
|
|
|
DmTaskGetDriverVersion = dmTaskGetDriverVersionFct
|
|
|
|
DmTaskRun = dmTaskRunFct
|
|
|
|
DmTaskSetAddNode = dmTaskSetAddNodeFct
|
|
|
|
DmTaskSetCookie = dmTaskSetCookieFct
|
|
|
|
DmTaskSetMessage = dmTaskSetMessageFct
|
|
|
|
DmTaskSetName = dmTaskSetNameFct
|
|
|
|
DmTaskSetSector = dmTaskSetSectorFct
|
|
|
|
DmUdevWait = dmUdevWaitFct
|
|
|
|
DmUdevSetSyncSupport = dmUdevSetSyncSupportFct
|
|
|
|
DmUdevGetSyncSupport = dmUdevGetSyncSupportFct
|
|
|
|
DmCookieSupported = dmCookieSupportedFct
|
|
|
|
LogWithErrnoInit = logWithErrnoInitFct
|
|
|
|
DmTaskDeferredRemove = dmTaskDeferredRemoveFct
|
|
|
|
DmTaskGetInfoWithDeferred = dmTaskGetInfoWithDeferredFct
|
2013-11-13 17:36:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func free(p *C.char) {
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskDestroyFct(task *cdmTask) {
|
2013-11-13 17:36:31 -05:00
|
|
|
C.dm_task_destroy((*C.struct_dm_task)(task))
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskCreateFct(taskType int) *cdmTask {
|
|
|
|
return (*cdmTask)(C.dm_task_create(C.int(taskType)))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskRunFct(task *cdmTask) int {
|
2013-11-27 02:16:34 -05:00
|
|
|
ret, _ := C.dm_task_run((*C.struct_dm_task)(task))
|
|
|
|
return int(ret)
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskSetNameFct(task *cdmTask, name string) int {
|
2013-11-13 17:36:31 -05:00
|
|
|
Cname := C.CString(name)
|
|
|
|
defer free(Cname)
|
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskSetMessageFct(task *cdmTask, message string) int {
|
2013-11-13 17:36:31 -05:00
|
|
|
Cmessage := C.CString(message)
|
|
|
|
defer free(Cmessage)
|
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskSetSectorFct(task *cdmTask, sector uint64) int {
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector)))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskSetCookieFct(task *cdmTask, cookie *uint, flags uint16) int {
|
2013-11-13 17:36:31 -05:00
|
|
|
cCookie := C.uint32_t(*cookie)
|
|
|
|
defer func() {
|
|
|
|
*cookie = uint(cCookie)
|
|
|
|
}()
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags)))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskSetAddNodeFct(task *cdmTask, addNode AddNodeType) int {
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode)))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskAddTargetFct(task *cdmTask,
|
2013-11-13 17:36:31 -05:00
|
|
|
start, size uint64, ttype, params string) int {
|
|
|
|
|
|
|
|
Cttype := C.CString(ttype)
|
|
|
|
defer free(Cttype)
|
|
|
|
|
|
|
|
Cparams := C.CString(params)
|
|
|
|
defer free(Cparams)
|
|
|
|
|
2013-11-27 02:16:34 -05:00
|
|
|
return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskGetDepsFct(task *cdmTask) *Deps {
|
2014-09-25 10:57:37 -04:00
|
|
|
Cdeps := C.dm_task_get_deps((*C.struct_dm_task)(task))
|
|
|
|
if Cdeps == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-07 10:18:20 -04:00
|
|
|
|
|
|
|
// golang issue: https://github.com/golang/go/issues/11925
|
2020-11-03 02:16:03 -05:00
|
|
|
var devices []C.uint64_t
|
|
|
|
devicesHdr := (*reflect.SliceHeader)(unsafe.Pointer(&devices))
|
|
|
|
devicesHdr.Data = uintptr(unsafe.Pointer(uintptr(unsafe.Pointer(Cdeps)) + unsafe.Sizeof(*Cdeps)))
|
|
|
|
devicesHdr.Len = int(Cdeps.count)
|
|
|
|
devicesHdr.Cap = int(Cdeps.count)
|
2015-08-07 10:18:20 -04:00
|
|
|
|
2014-09-25 10:57:37 -04:00
|
|
|
deps := &Deps{
|
|
|
|
Count: uint32(Cdeps.count),
|
|
|
|
Filler: uint32(Cdeps.filler),
|
|
|
|
}
|
2015-08-07 10:18:20 -04:00
|
|
|
for _, device := range devices {
|
|
|
|
deps.Device = append(deps.Device, uint64(device))
|
2014-09-25 10:57:37 -04:00
|
|
|
}
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskGetInfoFct(task *cdmTask, info *Info) int {
|
2013-11-13 17:36:31 -05:00
|
|
|
Cinfo := C.struct_dm_info{}
|
|
|
|
defer func() {
|
|
|
|
info.Exists = int(Cinfo.exists)
|
|
|
|
info.Suspended = int(Cinfo.suspended)
|
|
|
|
info.LiveTable = int(Cinfo.live_table)
|
|
|
|
info.InactiveTable = int(Cinfo.inactive_table)
|
|
|
|
info.OpenCount = int32(Cinfo.open_count)
|
|
|
|
info.EventNr = uint32(Cinfo.event_nr)
|
|
|
|
info.Major = uint32(Cinfo.major)
|
|
|
|
info.Minor = uint32(Cinfo.minor)
|
|
|
|
info.ReadOnly = int(Cinfo.read_only)
|
|
|
|
info.TargetCount = int32(Cinfo.target_count)
|
|
|
|
}()
|
|
|
|
return int(C.dm_task_get_info((*C.struct_dm_task)(task), &Cinfo))
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmTaskGetDriverVersionFct(task *cdmTask) string {
|
2014-03-27 12:45:02 -04:00
|
|
|
buffer := C.malloc(128)
|
|
|
|
defer C.free(buffer)
|
|
|
|
res := C.dm_task_get_driver_version((*C.struct_dm_task)(task), (*C.char)(buffer), 128)
|
|
|
|
if res == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return C.GoString((*C.char)(buffer))
|
|
|
|
}
|
|
|
|
|
2015-09-04 17:02:29 -04:00
|
|
|
func dmGetNextTargetFct(task *cdmTask, next unsafe.Pointer, start, length *uint64, target, params *string) unsafe.Pointer {
|
2013-11-13 17:36:31 -05:00
|
|
|
var (
|
|
|
|
Cstart, Clength C.uint64_t
|
|
|
|
CtargetType, Cparams *C.char
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
*start = uint64(Cstart)
|
|
|
|
*length = uint64(Clength)
|
|
|
|
*target = C.GoString(CtargetType)
|
|
|
|
*params = C.GoString(Cparams)
|
|
|
|
}()
|
2013-11-20 15:49:01 -05:00
|
|
|
|
2019-08-28 13:36:06 -04:00
|
|
|
//lint:ignore SA4000 false positive on (identical expressions on the left and right side of the '==' operator) (staticcheck)
|
2015-03-27 15:16:25 -04:00
|
|
|
nextp := C.dm_get_next_target((*C.struct_dm_task)(task), next, &Cstart, &Clength, &CtargetType, &Cparams)
|
|
|
|
return nextp
|
2013-11-13 17:36:31 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 15:10:37 -05:00
|
|
|
func dmUdevSetSyncSupportFct(syncWithUdev int) {
|
2018-05-19 07:38:54 -04:00
|
|
|
C.dm_udev_set_sync_support(C.int(syncWithUdev))
|
2015-01-19 15:10:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func dmUdevGetSyncSupportFct() int {
|
|
|
|
return int(C.dm_udev_get_sync_support())
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func dmUdevWaitFct(cookie uint) int {
|
|
|
|
return int(C.dm_udev_wait(C.uint32_t(cookie)))
|
|
|
|
}
|
|
|
|
|
2015-01-19 15:31:54 -05:00
|
|
|
func dmCookieSupportedFct() int {
|
|
|
|
return int(C.dm_cookie_supported())
|
|
|
|
}
|
|
|
|
|
2013-11-13 17:36:31 -05:00
|
|
|
func logWithErrnoInitFct() {
|
|
|
|
C.log_with_errno_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dmSetDevDirFct(dir string) int {
|
|
|
|
Cdir := C.CString(dir)
|
|
|
|
defer free(Cdir)
|
|
|
|
|
|
|
|
return int(C.dm_set_dev_dir(Cdir))
|
|
|
|
}
|
|
|
|
|
|
|
|
func dmGetLibraryVersionFct(version *string) int {
|
|
|
|
buffer := C.CString(string(make([]byte, 128)))
|
|
|
|
defer free(buffer)
|
|
|
|
defer func() {
|
|
|
|
*version = C.GoString(buffer)
|
|
|
|
}()
|
|
|
|
return int(C.dm_get_library_version(buffer, 128))
|
|
|
|
}
|