Browse code

Extract ioctl from wrapper

Guillaume J. Charmes authored on 2013/11/28 10:44:54
Showing 4 changed files
... ...
@@ -3,63 +3,14 @@ package devmapper
3 3
 import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/utils"
6
-	"unsafe"
7 6
 )
8 7
 
9
-func ioctlLoopCtlGetFree(fd uintptr) (int, error) {
10
-	index, _, err := sysSyscall(sysSysIoctl, fd, LoopCtlGetFree, 0)
11
-	if err != 0 {
12
-		return 0, err
13
-	}
14
-	return int(index), nil
15
-}
16
-
17
-func ioctlLoopSetFd(loopFd, sparseFd uintptr) error {
18
-	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopSetFd, sparseFd); err != 0 {
19
-		return err
20
-	}
21
-	return nil
22
-}
23
-
24
-func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *LoopInfo64) error {
25
-	_, _, err := sysSyscall(sysSysIoctl, loopFd, LoopSetStatus64, uintptr(unsafe.Pointer(loopInfo)))
26
-	if err != 0 {
27
-		return err
28
-	}
29
-	return nil
30
-}
31
-
32
-func ioctlLoopClrFd(loopFd uintptr) error {
33
-	_, _, err := sysSyscall(sysSysIoctl, loopFd, LoopClrFd, 0)
34
-	if err != 0 {
35
-		return err
36
-	}
37
-	return nil
8
+func stringToLoopName(src string) [LoNameSize]uint8 {
9
+	var dst [LoNameSize]uint8
10
+	copy(dst[:], src[:])
11
+	return dst
38 12
 }
39 13
 
40
-// //func dmGetLoopbackBackingFileFct(fd uintptr) (uint64, uint64, sysErrno) {
41
-// func ioctlLoopGetStatus64(loopFd uintptr) (*LoopInfo64, error) {
42
-// 	var lo64 C.struct_loop_info64
43
-// 	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(&lo64)))
44
-// 	return uint64(lo64.lo_device), uint64(lo64.lo_inode), sysErrno(err)
45
-// }
46
-
47
-// func dmLoopbackSetCapacityFct(fd uintptr) sysErrno {
48
-// 	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_SET_CAPACITY, 0)
49
-// 	return sysErrno(err)
50
-// }
51
-
52
-// func dmGetBlockSizeFct(fd uintptr) (int64, sysErrno) {
53
-// 	var size int64
54
-// 	_, _, err := sysSyscall(sysSysIoctl, fd, C.BLKGETSIZE64, uintptr(unsafe.Pointer(&size)))
55
-// 	return size, sysErrno(err)
56
-// }
57
-
58
-// func getBlockSizeFct(fd uintptr, size *uint64) sysErrno {
59
-// 	_, _, err := sysSyscall(sysSysIoctl, fd, C.BLKGETSIZE64, uintptr(unsafe.Pointer(&size)))
60
-// 	return sysErrno(err)
61
-// }
62
-
63 14
 func getNextFreeLoopbackIndex() (int, error) {
64 15
 	f, err := osOpenFile("/dev/loop-control", osORdOnly, 0644)
65 16
 	if err != nil {
... ...
@@ -125,12 +76,6 @@ func openNextAvailableLoopback(index int, sparseFile *osFile) (loopFile *osFile,
125 125
 	return loopFile, nil
126 126
 }
127 127
 
128
-func stringToLoopName(src string) [LoNameSize]uint8 {
129
-	var dst [LoNameSize]uint8
130
-	copy(dst[:], src[:])
131
-	return dst
132
-}
133
-
134 128
 // attachLoopDevice attaches the given sparse file to the next
135 129
 // available loopback device. It returns an opened *osFile.
136 130
 func attachLoopDevice(sparseName string) (loop *osFile, err error) {
... ...
@@ -178,15 +178,17 @@ func (t *Task) GetNextTarget(next uintptr) (nextPtr uintptr, start uint64,
178 178
 }
179 179
 
180 180
 func getLoopbackBackingFile(file *osFile) (uint64, uint64, error) {
181
-	dev, inode, err := DmGetLoopbackBackingFile(file.Fd())
182
-	if err != 0 {
181
+	loopInfo, err := ioctlLoopGetStatus64(file.Fd())
182
+	if err != nil {
183
+		utils.Errorf("Error get loopback backing file: %s\n", err)
183 184
 		return 0, 0, ErrGetLoopbackBackingFile
184 185
 	}
185
-	return dev, inode, nil
186
+	return loopInfo.loDevice, loopInfo.loInode, nil
186 187
 }
187 188
 
188 189
 func LoopbackSetCapacity(file *osFile) error {
189
-	if err := DmLoopbackSetCapacity(file.Fd()); err != 0 {
190
+	if err := ioctlLoopSetCapacity(file.Fd(), 0); err != nil {
191
+		utils.Errorf("Error loopbackSetCapacity: %s", err)
190 192
 		return ErrLoopbackSetCapacity
191 193
 	}
192 194
 	return nil
... ...
@@ -276,8 +278,9 @@ func RemoveDevice(name string) error {
276 276
 }
277 277
 
278 278
 func GetBlockDeviceSize(file *osFile) (uint64, error) {
279
-	size, errno := DmGetBlockSize(file.Fd())
280
-	if size == -1 || errno != 0 {
279
+	size, err := ioctlBlkGetSize64(file.Fd())
280
+	if err != nil {
281
+		utils.Errorf("Error getblockdevicesize: %s", err)
281 282
 		return 0, ErrGetBlockSize
282 283
 	}
283 284
 	return uint64(size), nil
... ...
@@ -33,7 +33,8 @@ import (
33 33
 )
34 34
 
35 35
 type (
36
-	CDmTask     C.struct_dm_task
36
+	CDmTask C.struct_dm_task
37
+
37 38
 	CLoopInfo64 C.struct_loop_info64
38 39
 	LoopInfo64  struct {
39 40
 		loDevice           uint64 /* ioctl r/o */
... ...
@@ -54,39 +55,40 @@ type (
54 54
 
55 55
 // FIXME: Make sure the values are defined in C
56 56
 const (
57
-	LoopSetFd        = C.LOOP_SET_FD
58
-	LoopCtlGetFree   = C.LOOP_CTL_GET_FREE
59
-	LoopSetStatus64  = C.LOOP_SET_STATUS64
60
-	LoopClrFd        = C.LOOP_CLR_FD
57
+	LoopSetFd       = C.LOOP_SET_FD
58
+	LoopCtlGetFree  = C.LOOP_CTL_GET_FREE
59
+	LoopGetStatus64 = C.LOOP_GET_STATUS64
60
+	LoopSetStatus64 = C.LOOP_SET_STATUS64
61
+	LoopClrFd       = C.LOOP_CLR_FD
62
+	LoopSetCapacity = C.LOOP_SET_CAPACITY
63
+
61 64
 	LoFlagsAutoClear = C.LO_FLAGS_AUTOCLEAR
62 65
 	LoFlagsReadOnly  = C.LO_FLAGS_READ_ONLY
63 66
 	LoFlagsPartScan  = C.LO_FLAGS_PARTSCAN
64 67
 	LoKeySize        = C.LO_KEY_SIZE
65 68
 	LoNameSize       = C.LO_NAME_SIZE
69
+
70
+	BlkGetSize64 = C.BLKGETSIZE64
66 71
 )
67 72
 
68 73
 var (
69
-	DmGetBlockSize           = dmGetBlockSizeFct
70
-	DmGetLibraryVersion      = dmGetLibraryVersionFct
71
-	DmGetNextTarget          = dmGetNextTargetFct
72
-	DmLogInitVerbose         = dmLogInitVerboseFct
73
-	DmSetDevDir              = dmSetDevDirFct
74
-	DmTaskAddTarget          = dmTaskAddTargetFct
75
-	DmTaskCreate             = dmTaskCreateFct
76
-	DmTaskDestroy            = dmTaskDestroyFct
77
-	DmTaskGetInfo            = dmTaskGetInfoFct
78
-	DmTaskRun                = dmTaskRunFct
79
-	DmTaskSetAddNode         = dmTaskSetAddNodeFct
80
-	DmTaskSetCookie          = dmTaskSetCookieFct
81
-	DmTaskSetMessage         = dmTaskSetMessageFct
82
-	DmTaskSetName            = dmTaskSetNameFct
83
-	DmTaskSetRo              = dmTaskSetRoFct
84
-	DmTaskSetSector          = dmTaskSetSectorFct
85
-	DmUdevWait               = dmUdevWaitFct
86
-	GetBlockSize             = getBlockSizeFct
87
-	LogWithErrnoInit         = logWithErrnoInitFct
88
-	DmGetLoopbackBackingFile = dmGetLoopbackBackingFileFct
89
-	DmLoopbackSetCapacity    = dmLoopbackSetCapacityFct
74
+	DmGetLibraryVersion = dmGetLibraryVersionFct
75
+	DmGetNextTarget     = dmGetNextTargetFct
76
+	DmLogInitVerbose    = dmLogInitVerboseFct
77
+	DmSetDevDir         = dmSetDevDirFct
78
+	DmTaskAddTarget     = dmTaskAddTargetFct
79
+	DmTaskCreate        = dmTaskCreateFct
80
+	DmTaskDestroy       = dmTaskDestroyFct
81
+	DmTaskGetInfo       = dmTaskGetInfoFct
82
+	DmTaskRun           = dmTaskRunFct
83
+	DmTaskSetAddNode    = dmTaskSetAddNodeFct
84
+	DmTaskSetCookie     = dmTaskSetCookieFct
85
+	DmTaskSetMessage    = dmTaskSetMessageFct
86
+	DmTaskSetName       = dmTaskSetNameFct
87
+	DmTaskSetRo         = dmTaskSetRoFct
88
+	DmTaskSetSector     = dmTaskSetSectorFct
89
+	DmUdevWait          = dmUdevWaitFct
90
+	LogWithErrnoInit    = logWithErrnoInitFct
90 91
 )
91 92
 
92 93
 func free(p *C.char) {
... ...
@@ -185,28 +187,6 @@ func dmGetNextTargetFct(task *CDmTask, next uintptr, start, length *uint64, targ
185 185
 	return uintptr(nextp)
186 186
 }
187 187
 
188
-func dmGetLoopbackBackingFileFct(fd uintptr) (uint64, uint64, sysErrno) {
189
-	var lo64 C.struct_loop_info64
190
-	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(&lo64)))
191
-	return uint64(lo64.lo_device), uint64(lo64.lo_inode), sysErrno(err)
192
-}
193
-
194
-func dmLoopbackSetCapacityFct(fd uintptr) sysErrno {
195
-	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_SET_CAPACITY, 0)
196
-	return sysErrno(err)
197
-}
198
-
199
-func dmGetBlockSizeFct(fd uintptr) (int64, sysErrno) {
200
-	var size int64
201
-	_, _, err := sysSyscall(sysSysIoctl, fd, C.BLKGETSIZE64, uintptr(unsafe.Pointer(&size)))
202
-	return size, sysErrno(err)
203
-}
204
-
205
-func getBlockSizeFct(fd uintptr, size *uint64) sysErrno {
206
-	_, _, err := sysSyscall(sysSysIoctl, fd, C.BLKGETSIZE64, uintptr(unsafe.Pointer(&size)))
207
-	return sysErrno(err)
208
-}
209
-
210 188
 func dmUdevWaitFct(cookie uint) int {
211 189
 	return int(C.dm_udev_wait(C.uint32_t(cookie)))
212 190
 }
213 191
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+package devmapper
1
+
2
+import (
3
+	"unsafe"
4
+)
5
+
6
+func ioctlLoopCtlGetFree(fd uintptr) (int, error) {
7
+	index, _, err := sysSyscall(sysSysIoctl, fd, LoopCtlGetFree, 0)
8
+	if err != 0 {
9
+		return 0, err
10
+	}
11
+	return int(index), nil
12
+}
13
+
14
+func ioctlLoopSetFd(loopFd, sparseFd uintptr) error {
15
+	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopSetFd, sparseFd); err != 0 {
16
+		return err
17
+	}
18
+	return nil
19
+}
20
+
21
+func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *LoopInfo64) error {
22
+	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopSetStatus64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
23
+		return err
24
+	}
25
+	return nil
26
+}
27
+
28
+func ioctlLoopClrFd(loopFd uintptr) error {
29
+	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopClrFd, 0); err != 0 {
30
+		return err
31
+	}
32
+	return nil
33
+}
34
+
35
+func ioctlLoopGetStatus64(loopFd uintptr) (*LoopInfo64, error) {
36
+	loopInfo := &LoopInfo64{}
37
+
38
+	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopGetStatus64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
39
+		return nil, err
40
+	}
41
+	return loopInfo, nil
42
+}
43
+
44
+func ioctlLoopSetCapacity(loopFd uintptr, value int) error {
45
+	if _, _, err := sysSyscall(sysSysIoctl, loopFd, LoopSetCapacity, uintptr(value)); err != 0 {
46
+		return err
47
+	}
48
+	return nil
49
+}
50
+
51
+func ioctlBlkGetSize64(fd uintptr) (int64, error) {
52
+	var size int64
53
+	if _, _, err := sysSyscall(sysSysIoctl, fd, BlkGetSize64, uintptr(unsafe.Pointer(&size))); err != 0 {
54
+		return 0, err
55
+	}
56
+	return size, nil
57
+}