github.com/cdoern/storage@v1.12.13/pkg/devicemapper/devmapper_wrapper.go (about)

     1  // +build linux,cgo
     2  
     3  package devicemapper
     4  
     5  /*
     6  #define _GNU_SOURCE
     7  #include <libdevmapper.h>
     8  #include <linux/fs.h>   // FIXME: present only for BLKGETSIZE64, maybe we can remove it?
     9  
    10  // FIXME: Can't we find a way to do the logging in pure Go?
    11  extern void StorageDevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str);
    12  
    13  static void	log_cb(int level, const char *file, int line, int dm_errno_or_class, const char *f, ...)
    14  {
    15  	char *buffer = NULL;
    16  	va_list ap;
    17  	int ret;
    18  
    19  	va_start(ap, f);
    20  	ret = vasprintf(&buffer, f, ap);
    21  	va_end(ap);
    22  	if (ret < 0) {
    23  		// memory allocation failed -- should never happen?
    24  		return;
    25  	}
    26  
    27  	StorageDevmapperLogCallback(level, (char *)file, line, dm_errno_or_class, buffer);
    28  	free(buffer);
    29  }
    30  
    31  static void	log_with_errno_init()
    32  {
    33  	dm_log_with_errno_init(log_cb);
    34  }
    35  */
    36  import "C"
    37  
    38  import (
    39  	"reflect"
    40  	"unsafe"
    41  )
    42  
    43  type (
    44  	cdmTask C.struct_dm_task
    45  )
    46  
    47  // IOCTL consts
    48  const (
    49  	BlkGetSize64 = C.BLKGETSIZE64
    50  	BlkDiscard   = C.BLKDISCARD
    51  )
    52  
    53  // Devicemapper cookie flags.
    54  const (
    55  	DmUdevDisableSubsystemRulesFlag = C.DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG
    56  	DmUdevDisableDiskRulesFlag      = C.DM_UDEV_DISABLE_DISK_RULES_FLAG
    57  	DmUdevDisableOtherRulesFlag     = C.DM_UDEV_DISABLE_OTHER_RULES_FLAG
    58  	DmUdevDisableLibraryFallback    = C.DM_UDEV_DISABLE_LIBRARY_FALLBACK
    59  )
    60  
    61  // DeviceMapper mapped functions.
    62  var (
    63  	DmGetLibraryVersion       = dmGetLibraryVersionFct
    64  	DmGetNextTarget           = dmGetNextTargetFct
    65  	DmSetDevDir               = dmSetDevDirFct
    66  	DmTaskAddTarget           = dmTaskAddTargetFct
    67  	DmTaskCreate              = dmTaskCreateFct
    68  	DmTaskDestroy             = dmTaskDestroyFct
    69  	DmTaskGetDeps             = dmTaskGetDepsFct
    70  	DmTaskGetInfo             = dmTaskGetInfoFct
    71  	DmTaskGetDriverVersion    = dmTaskGetDriverVersionFct
    72  	DmTaskRun                 = dmTaskRunFct
    73  	DmTaskSetAddNode          = dmTaskSetAddNodeFct
    74  	DmTaskSetCookie           = dmTaskSetCookieFct
    75  	DmTaskSetMessage          = dmTaskSetMessageFct
    76  	DmTaskSetName             = dmTaskSetNameFct
    77  	DmTaskSetRo               = dmTaskSetRoFct
    78  	DmTaskSetSector           = dmTaskSetSectorFct
    79  	DmUdevWait                = dmUdevWaitFct
    80  	DmUdevSetSyncSupport      = dmUdevSetSyncSupportFct
    81  	DmUdevGetSyncSupport      = dmUdevGetSyncSupportFct
    82  	DmCookieSupported         = dmCookieSupportedFct
    83  	LogWithErrnoInit          = logWithErrnoInitFct
    84  	DmTaskDeferredRemove      = dmTaskDeferredRemoveFct
    85  	DmTaskGetInfoWithDeferred = dmTaskGetInfoWithDeferredFct
    86  )
    87  
    88  func free(p *C.char) {
    89  	C.free(unsafe.Pointer(p))
    90  }
    91  
    92  func dmTaskDestroyFct(task *cdmTask) {
    93  	C.dm_task_destroy((*C.struct_dm_task)(task))
    94  }
    95  
    96  func dmTaskCreateFct(taskType int) *cdmTask {
    97  	return (*cdmTask)(C.dm_task_create(C.int(taskType)))
    98  }
    99  
   100  func dmTaskRunFct(task *cdmTask) int {
   101  	ret, _ := C.dm_task_run((*C.struct_dm_task)(task))
   102  	return int(ret)
   103  }
   104  
   105  func dmTaskSetNameFct(task *cdmTask, name string) int {
   106  	Cname := C.CString(name)
   107  	defer free(Cname)
   108  
   109  	return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname))
   110  }
   111  
   112  func dmTaskSetMessageFct(task *cdmTask, message string) int {
   113  	Cmessage := C.CString(message)
   114  	defer free(Cmessage)
   115  
   116  	return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage))
   117  }
   118  
   119  func dmTaskSetSectorFct(task *cdmTask, sector uint64) int {
   120  	return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector)))
   121  }
   122  
   123  func dmTaskSetCookieFct(task *cdmTask, cookie *uint, flags uint16) int {
   124  	cCookie := C.uint32_t(*cookie)
   125  	defer func() {
   126  		*cookie = uint(cCookie)
   127  	}()
   128  	return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags)))
   129  }
   130  
   131  func dmTaskSetAddNodeFct(task *cdmTask, addNode AddNodeType) int {
   132  	return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode)))
   133  }
   134  
   135  func dmTaskSetRoFct(task *cdmTask) int {
   136  	return int(C.dm_task_set_ro((*C.struct_dm_task)(task)))
   137  }
   138  
   139  func dmTaskAddTargetFct(task *cdmTask,
   140  	start, size uint64, ttype, params string) int {
   141  
   142  	Cttype := C.CString(ttype)
   143  	defer free(Cttype)
   144  
   145  	Cparams := C.CString(params)
   146  	defer free(Cparams)
   147  
   148  	return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
   149  }
   150  
   151  func dmTaskGetDepsFct(task *cdmTask) *Deps {
   152  	Cdeps := C.dm_task_get_deps((*C.struct_dm_task)(task))
   153  	if Cdeps == nil {
   154  		return nil
   155  	}
   156  
   157  	// golang issue: https://github.com/golang/go/issues/11925
   158  	hdr := reflect.SliceHeader{
   159  		Data: uintptr(unsafe.Pointer(uintptr(unsafe.Pointer(Cdeps)) + unsafe.Sizeof(*Cdeps))),
   160  		Len:  int(Cdeps.count),
   161  		Cap:  int(Cdeps.count),
   162  	}
   163  	devices := *(*[]C.uint64_t)(unsafe.Pointer(&hdr))
   164  
   165  	deps := &Deps{
   166  		Count:  uint32(Cdeps.count),
   167  		Filler: uint32(Cdeps.filler),
   168  	}
   169  	for _, device := range devices {
   170  		deps.Device = append(deps.Device, uint64(device))
   171  	}
   172  	return deps
   173  }
   174  
   175  func dmTaskGetInfoFct(task *cdmTask, info *Info) int {
   176  	Cinfo := C.struct_dm_info{}
   177  	defer func() {
   178  		info.Exists = int(Cinfo.exists)
   179  		info.Suspended = int(Cinfo.suspended)
   180  		info.LiveTable = int(Cinfo.live_table)
   181  		info.InactiveTable = int(Cinfo.inactive_table)
   182  		info.OpenCount = int32(Cinfo.open_count)
   183  		info.EventNr = uint32(Cinfo.event_nr)
   184  		info.Major = uint32(Cinfo.major)
   185  		info.Minor = uint32(Cinfo.minor)
   186  		info.ReadOnly = int(Cinfo.read_only)
   187  		info.TargetCount = int32(Cinfo.target_count)
   188  	}()
   189  	return int(C.dm_task_get_info((*C.struct_dm_task)(task), &Cinfo))
   190  }
   191  
   192  func dmTaskGetDriverVersionFct(task *cdmTask) string {
   193  	buffer := C.malloc(128)
   194  	defer C.free(buffer)
   195  	res := C.dm_task_get_driver_version((*C.struct_dm_task)(task), (*C.char)(buffer), 128)
   196  	if res == 0 {
   197  		return ""
   198  	}
   199  	return C.GoString((*C.char)(buffer))
   200  }
   201  
   202  func dmGetNextTargetFct(task *cdmTask, next unsafe.Pointer, start, length *uint64, target, params *string) unsafe.Pointer {
   203  	var (
   204  		Cstart, Clength      C.uint64_t
   205  		CtargetType, Cparams *C.char
   206  	)
   207  	defer func() {
   208  		*start = uint64(Cstart)
   209  		*length = uint64(Clength)
   210  		*target = C.GoString(CtargetType)
   211  		*params = C.GoString(Cparams)
   212  	}()
   213  
   214  	nextp := C.dm_get_next_target((*C.struct_dm_task)(task), next, &Cstart, &Clength, &CtargetType, &Cparams)
   215  	return nextp
   216  }
   217  
   218  func dmUdevSetSyncSupportFct(syncWithUdev int) {
   219  	(C.dm_udev_set_sync_support(C.int(syncWithUdev)))
   220  }
   221  
   222  func dmUdevGetSyncSupportFct() int {
   223  	return int(C.dm_udev_get_sync_support())
   224  }
   225  
   226  func dmUdevWaitFct(cookie uint) int {
   227  	return int(C.dm_udev_wait(C.uint32_t(cookie)))
   228  }
   229  
   230  func dmCookieSupportedFct() int {
   231  	return int(C.dm_cookie_supported())
   232  }
   233  
   234  func logWithErrnoInitFct() {
   235  	C.log_with_errno_init()
   236  }
   237  
   238  func dmSetDevDirFct(dir string) int {
   239  	Cdir := C.CString(dir)
   240  	defer free(Cdir)
   241  
   242  	return int(C.dm_set_dev_dir(Cdir))
   243  }
   244  
   245  func dmGetLibraryVersionFct(version *string) int {
   246  	buffer := C.CString(string(make([]byte, 128)))
   247  	defer free(buffer)
   248  	defer func() {
   249  		*version = C.GoString(buffer)
   250  	}()
   251  	return int(C.dm_get_library_version(buffer, 128))
   252  }