github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/devicemapper/devmapper_wrapper.go (about) 1 // +build linux,cgo 2 3 package devicemapper // import "github.com/demonoid81/moby/pkg/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 DevmapperLogCallback(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 DevmapperLogCallback(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 DmTaskSetSector = dmTaskSetSectorFct 78 DmUdevWait = dmUdevWaitFct 79 DmUdevSetSyncSupport = dmUdevSetSyncSupportFct 80 DmUdevGetSyncSupport = dmUdevGetSyncSupportFct 81 DmCookieSupported = dmCookieSupportedFct 82 LogWithErrnoInit = logWithErrnoInitFct 83 DmTaskDeferredRemove = dmTaskDeferredRemoveFct 84 DmTaskGetInfoWithDeferred = dmTaskGetInfoWithDeferredFct 85 ) 86 87 func free(p *C.char) { 88 C.free(unsafe.Pointer(p)) 89 } 90 91 func dmTaskDestroyFct(task *cdmTask) { 92 C.dm_task_destroy((*C.struct_dm_task)(task)) 93 } 94 95 func dmTaskCreateFct(taskType int) *cdmTask { 96 return (*cdmTask)(C.dm_task_create(C.int(taskType))) 97 } 98 99 func dmTaskRunFct(task *cdmTask) int { 100 ret, _ := C.dm_task_run((*C.struct_dm_task)(task)) 101 return int(ret) 102 } 103 104 func dmTaskSetNameFct(task *cdmTask, name string) int { 105 Cname := C.CString(name) 106 defer free(Cname) 107 108 return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname)) 109 } 110 111 func dmTaskSetMessageFct(task *cdmTask, message string) int { 112 Cmessage := C.CString(message) 113 defer free(Cmessage) 114 115 return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage)) 116 } 117 118 func dmTaskSetSectorFct(task *cdmTask, sector uint64) int { 119 return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector))) 120 } 121 122 func dmTaskSetCookieFct(task *cdmTask, cookie *uint, flags uint16) int { 123 cCookie := C.uint32_t(*cookie) 124 defer func() { 125 *cookie = uint(cCookie) 126 }() 127 return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags))) 128 } 129 130 func dmTaskSetAddNodeFct(task *cdmTask, addNode AddNodeType) int { 131 return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode))) 132 } 133 134 func dmTaskAddTargetFct(task *cdmTask, 135 start, size uint64, ttype, params string) int { 136 137 Cttype := C.CString(ttype) 138 defer free(Cttype) 139 140 Cparams := C.CString(params) 141 defer free(Cparams) 142 143 return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams)) 144 } 145 146 func dmTaskGetDepsFct(task *cdmTask) *Deps { 147 Cdeps := C.dm_task_get_deps((*C.struct_dm_task)(task)) 148 if Cdeps == nil { 149 return nil 150 } 151 152 // golang issue: https://github.com/golang/go/issues/11925 153 hdr := reflect.SliceHeader{ 154 Data: uintptr(unsafe.Pointer(uintptr(unsafe.Pointer(Cdeps)) + unsafe.Sizeof(*Cdeps))), 155 Len: int(Cdeps.count), 156 Cap: int(Cdeps.count), 157 } 158 devices := *(*[]C.uint64_t)(unsafe.Pointer(&hdr)) 159 160 deps := &Deps{ 161 Count: uint32(Cdeps.count), 162 Filler: uint32(Cdeps.filler), 163 } 164 for _, device := range devices { 165 deps.Device = append(deps.Device, uint64(device)) 166 } 167 return deps 168 } 169 170 func dmTaskGetInfoFct(task *cdmTask, info *Info) int { 171 Cinfo := C.struct_dm_info{} 172 defer func() { 173 info.Exists = int(Cinfo.exists) 174 info.Suspended = int(Cinfo.suspended) 175 info.LiveTable = int(Cinfo.live_table) 176 info.InactiveTable = int(Cinfo.inactive_table) 177 info.OpenCount = int32(Cinfo.open_count) 178 info.EventNr = uint32(Cinfo.event_nr) 179 info.Major = uint32(Cinfo.major) 180 info.Minor = uint32(Cinfo.minor) 181 info.ReadOnly = int(Cinfo.read_only) 182 info.TargetCount = int32(Cinfo.target_count) 183 }() 184 return int(C.dm_task_get_info((*C.struct_dm_task)(task), &Cinfo)) 185 } 186 187 func dmTaskGetDriverVersionFct(task *cdmTask) string { 188 buffer := C.malloc(128) 189 defer C.free(buffer) 190 res := C.dm_task_get_driver_version((*C.struct_dm_task)(task), (*C.char)(buffer), 128) 191 if res == 0 { 192 return "" 193 } 194 return C.GoString((*C.char)(buffer)) 195 } 196 197 func dmGetNextTargetFct(task *cdmTask, next unsafe.Pointer, start, length *uint64, target, params *string) unsafe.Pointer { 198 var ( 199 Cstart, Clength C.uint64_t 200 CtargetType, Cparams *C.char 201 ) 202 defer func() { 203 *start = uint64(Cstart) 204 *length = uint64(Clength) 205 *target = C.GoString(CtargetType) 206 *params = C.GoString(Cparams) 207 }() 208 209 //lint:ignore SA4000 false positive on (identical expressions on the left and right side of the '==' operator) (staticcheck) 210 nextp := C.dm_get_next_target((*C.struct_dm_task)(task), next, &Cstart, &Clength, &CtargetType, &Cparams) 211 return nextp 212 } 213 214 func dmUdevSetSyncSupportFct(syncWithUdev int) { 215 C.dm_udev_set_sync_support(C.int(syncWithUdev)) 216 } 217 218 func dmUdevGetSyncSupportFct() int { 219 return int(C.dm_udev_get_sync_support()) 220 } 221 222 func dmUdevWaitFct(cookie uint) int { 223 return int(C.dm_udev_wait(C.uint32_t(cookie))) 224 } 225 226 func dmCookieSupportedFct() int { 227 return int(C.dm_cookie_supported()) 228 } 229 230 func logWithErrnoInitFct() { 231 C.log_with_errno_init() 232 } 233 234 func dmSetDevDirFct(dir string) int { 235 Cdir := C.CString(dir) 236 defer free(Cdir) 237 238 return int(C.dm_set_dev_dir(Cdir)) 239 } 240 241 func dmGetLibraryVersionFct(version *string) int { 242 buffer := C.CString(string(make([]byte, 128))) 243 defer free(buffer) 244 defer func() { 245 *version = C.GoString(buffer) 246 }() 247 return int(C.dm_get_library_version(buffer, 128)) 248 }