github.com/wlan0/docker@v1.5.0/pkg/devicemapper/devmapper_wrapper.go (about)

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