github.com/xuyutom/docker@v1.6.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  	DmCookieSupported      = dmCookieSupportedFct
   114  	LogWithErrnoInit       = logWithErrnoInitFct
   115  )
   116  
   117  func free(p *C.char) {
   118  	C.free(unsafe.Pointer(p))
   119  }
   120  
   121  func dmTaskDestroyFct(task *CDmTask) {
   122  	C.dm_task_destroy((*C.struct_dm_task)(task))
   123  }
   124  
   125  func dmTaskCreateFct(taskType int) *CDmTask {
   126  	return (*CDmTask)(C.dm_task_create(C.int(taskType)))
   127  }
   128  
   129  func dmTaskRunFct(task *CDmTask) int {
   130  	ret, _ := C.dm_task_run((*C.struct_dm_task)(task))
   131  	return int(ret)
   132  }
   133  
   134  func dmTaskSetNameFct(task *CDmTask, name string) int {
   135  	Cname := C.CString(name)
   136  	defer free(Cname)
   137  
   138  	return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname))
   139  }
   140  
   141  func dmTaskSetMessageFct(task *CDmTask, message string) int {
   142  	Cmessage := C.CString(message)
   143  	defer free(Cmessage)
   144  
   145  	return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage))
   146  }
   147  
   148  func dmTaskSetSectorFct(task *CDmTask, sector uint64) int {
   149  	return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector)))
   150  }
   151  
   152  func dmTaskSetCookieFct(task *CDmTask, cookie *uint, flags uint16) int {
   153  	cCookie := C.uint32_t(*cookie)
   154  	defer func() {
   155  		*cookie = uint(cCookie)
   156  	}()
   157  	return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags)))
   158  }
   159  
   160  func dmTaskSetAddNodeFct(task *CDmTask, addNode AddNodeType) int {
   161  	return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode)))
   162  }
   163  
   164  func dmTaskSetRoFct(task *CDmTask) int {
   165  	return int(C.dm_task_set_ro((*C.struct_dm_task)(task)))
   166  }
   167  
   168  func dmTaskAddTargetFct(task *CDmTask,
   169  	start, size uint64, ttype, params string) int {
   170  
   171  	Cttype := C.CString(ttype)
   172  	defer free(Cttype)
   173  
   174  	Cparams := C.CString(params)
   175  	defer free(Cparams)
   176  
   177  	return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
   178  }
   179  
   180  func dmTaskGetDepsFct(task *CDmTask) *Deps {
   181  	Cdeps := C.dm_task_get_deps((*C.struct_dm_task)(task))
   182  	if Cdeps == nil {
   183  		return nil
   184  	}
   185  	deps := &Deps{
   186  		Count:  uint32(Cdeps.count),
   187  		Filler: uint32(Cdeps.filler),
   188  	}
   189  	for _, device := range Cdeps.device {
   190  		deps.Device = append(deps.Device, (uint64)(device))
   191  	}
   192  	return deps
   193  }
   194  
   195  func dmTaskGetInfoFct(task *CDmTask, info *Info) int {
   196  	Cinfo := C.struct_dm_info{}
   197  	defer func() {
   198  		info.Exists = int(Cinfo.exists)
   199  		info.Suspended = int(Cinfo.suspended)
   200  		info.LiveTable = int(Cinfo.live_table)
   201  		info.InactiveTable = int(Cinfo.inactive_table)
   202  		info.OpenCount = int32(Cinfo.open_count)
   203  		info.EventNr = uint32(Cinfo.event_nr)
   204  		info.Major = uint32(Cinfo.major)
   205  		info.Minor = uint32(Cinfo.minor)
   206  		info.ReadOnly = int(Cinfo.read_only)
   207  		info.TargetCount = int32(Cinfo.target_count)
   208  	}()
   209  	return int(C.dm_task_get_info((*C.struct_dm_task)(task), &Cinfo))
   210  }
   211  
   212  func dmTaskGetDriverVersionFct(task *CDmTask) string {
   213  	buffer := C.malloc(128)
   214  	defer C.free(buffer)
   215  	res := C.dm_task_get_driver_version((*C.struct_dm_task)(task), (*C.char)(buffer), 128)
   216  	if res == 0 {
   217  		return ""
   218  	}
   219  	return C.GoString((*C.char)(buffer))
   220  }
   221  
   222  func dmGetNextTargetFct(task *CDmTask, next uintptr, start, length *uint64, target, params *string) uintptr {
   223  	var (
   224  		Cstart, Clength      C.uint64_t
   225  		CtargetType, Cparams *C.char
   226  	)
   227  	defer func() {
   228  		*start = uint64(Cstart)
   229  		*length = uint64(Clength)
   230  		*target = C.GoString(CtargetType)
   231  		*params = C.GoString(Cparams)
   232  	}()
   233  
   234  	nextp := C.dm_get_next_target((*C.struct_dm_task)(task), unsafe.Pointer(next), &Cstart, &Clength, &CtargetType, &Cparams)
   235  	return uintptr(nextp)
   236  }
   237  
   238  func dmUdevSetSyncSupportFct(syncWithUdev int) {
   239  	(C.dm_udev_set_sync_support(C.int(syncWithUdev)))
   240  }
   241  
   242  func dmUdevGetSyncSupportFct() int {
   243  	return int(C.dm_udev_get_sync_support())
   244  }
   245  
   246  func dmUdevWaitFct(cookie uint) int {
   247  	return int(C.dm_udev_wait(C.uint32_t(cookie)))
   248  }
   249  
   250  func dmCookieSupportedFct() int {
   251  	return int(C.dm_cookie_supported())
   252  }
   253  
   254  func dmLogInitVerboseFct(level int) {
   255  	C.dm_log_init_verbose(C.int(level))
   256  }
   257  
   258  func logWithErrnoInitFct() {
   259  	C.log_with_errno_init()
   260  }
   261  
   262  func dmSetDevDirFct(dir string) int {
   263  	Cdir := C.CString(dir)
   264  	defer free(Cdir)
   265  
   266  	return int(C.dm_set_dev_dir(Cdir))
   267  }
   268  
   269  func dmGetLibraryVersionFct(version *string) int {
   270  	buffer := C.CString(string(make([]byte, 128)))
   271  	defer free(buffer)
   272  	defer func() {
   273  		*version = C.GoString(buffer)
   274  	}()
   275  	return int(C.dm_get_library_version(buffer, 128))
   276  }