Browse code

Refactor attach loop device in pure Go

Guillaume J. Charmes authored on 2013/11/27 16:16:34
Showing 4 changed files
... ...
@@ -383,7 +383,7 @@ func (devices *DeviceSet) ResizePool(size int64) error {
383 383
 		return fmt.Errorf("Can't shrink file")
384 384
 	}
385 385
 
386
-	dataloopback := FindLoopDeviceFor(&osFile{File: datafile})
386
+	dataloopback := FindLoopDeviceFor(datafile)
387 387
 	if dataloopback == nil {
388 388
 		return fmt.Errorf("Unable to find loopback mount for: %s", datafilename)
389 389
 	}
... ...
@@ -395,7 +395,7 @@ func (devices *DeviceSet) ResizePool(size int64) error {
395 395
 	}
396 396
 	defer metadatafile.Close()
397 397
 
398
-	metadataloopback := FindLoopDeviceFor(&osFile{File: metadatafile})
398
+	metadataloopback := FindLoopDeviceFor(metadatafile)
399 399
 	if metadataloopback == nil {
400 400
 		return fmt.Errorf("Unable to find loopback mount for: %s", metadatafilename)
401 401
 	}
... ...
@@ -491,14 +491,14 @@ func (devices *DeviceSet) initDevmapper(doInit bool) error {
491 491
 	if info.Exists == 0 {
492 492
 		utils.Debugf("Pool doesn't exist. Creating it.")
493 493
 
494
-		dataFile, err := AttachLoopDevice(data)
494
+		dataFile, err := attachLoopDevice(data)
495 495
 		if err != nil {
496 496
 			utils.Debugf("\n--->Err: %s\n", err)
497 497
 			return err
498 498
 		}
499 499
 		defer dataFile.Close()
500 500
 
501
-		metadataFile, err := AttachLoopDevice(metadata)
501
+		metadataFile, err := attachLoopDevice(metadata)
502 502
 		if err != nil {
503 503
 			utils.Debugf("\n--->Err: %s\n", err)
504 504
 			return err
... ...
@@ -637,7 +637,7 @@ func (devices *DeviceSet) deactivateDevice(hash string) error {
637 637
 // or b) the 1 second timeout expires.
638 638
 func (devices *DeviceSet) waitRemove(hash string) error {
639 639
 	utils.Debugf("[deviceset %s] waitRemove(%s)", devices.devicePrefix, hash)
640
-	defer utils.Debugf("[deviceset %s] waitRemove END", devices.devicePrefix, hash)
640
+	defer utils.Debugf("[deviceset %s] waitRemove(%) END", devices.devicePrefix, hash)
641 641
 	devname, err := devices.byHash(hash)
642 642
 	if err != nil {
643 643
 		return err
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker/utils"
7 7
 	"runtime"
8
+	"unsafe"
8 9
 )
9 10
 
10 11
 type DevmapperLogger interface {
... ...
@@ -177,15 +178,6 @@ func (t *Task) GetNextTarget(next uintptr) (nextPtr uintptr, start uint64,
177 177
 		start, length, targetType, params
178 178
 }
179 179
 
180
-func AttachLoopDevice(filename string) (*osFile, error) {
181
-	var fd int
182
-	res := DmAttachLoopDevice(filename, &fd)
183
-	if res == "" {
184
-		return nil, ErrAttachLoopbackDevice
185
-	}
186
-	return &osFile{File: osNewFile(uintptr(fd), res)}, nil
187
-}
188
-
189 180
 func getLoopbackBackingFile(file *osFile) (uint64, uint64, error) {
190 181
 	dev, inode, err := DmGetLoopbackBackingFile(file.Fd())
191 182
 	if err != 0 {
... ...
@@ -223,11 +215,10 @@ func FindLoopDeviceFor(file *osFile) *osFile {
223 223
 			continue
224 224
 		}
225 225
 
226
-		dev, inode, err := getLoopbackBackingFile(&osFile{File: file})
226
+		dev, inode, err := getLoopbackBackingFile(file)
227 227
 		if err == nil && dev == targetDevice && inode == targetInode {
228
-			return &osFile{File: file}
228
+			return file
229 229
 		}
230
-
231 230
 		file.Close()
232 231
 	}
233 232
 
... ...
@@ -420,7 +411,7 @@ func suspendDevice(name string) error {
420 420
 		return err
421 421
 	}
422 422
 	if err := task.Run(); err != nil {
423
-		return fmt.Errorf("Error running DeviceSuspend")
423
+		return fmt.Errorf("Error running DeviceSuspend: %s", err)
424 424
 	}
425 425
 	return nil
426 426
 }
... ...
@@ -437,7 +428,7 @@ func resumeDevice(name string) error {
437 437
 	}
438 438
 
439 439
 	if err := task.Run(); err != nil {
440
-		return fmt.Errorf("Error running DeviceSuspend")
440
+		return fmt.Errorf("Error running DeviceResume")
441 441
 	}
442 442
 
443 443
 	UdevWait(cookie)
... ...
@@ -574,3 +565,109 @@ func (devices *DeviceSet) createSnapDevice(poolName string, deviceId int, baseNa
574 574
 
575 575
 	return nil
576 576
 }
577
+
578
+type LoopInfo64 struct {
579
+	loDevice           uint64 /* ioctl r/o */
580
+	loInode            uint64 /* ioctl r/o */
581
+	loRdevice          uint64 /* ioctl r/o */
582
+	loOffset           uint64
583
+	loSizelimit        uint64 /* bytes, 0 == max available */
584
+	loNumber           uint32 /* ioctl r/o */
585
+	loEncrypt_type     uint32
586
+	loEncrypt_key_size uint32 /* ioctl w/o */
587
+	loFlags            uint32 /* ioctl r/o */
588
+	loFileName         [LoNameSize]uint8
589
+	loCryptName        [LoNameSize]uint8
590
+	loEncryptKey       [LoKeySize]uint8 /* ioctl w/o */
591
+	loInit             [2]uint64
592
+}
593
+
594
+// attachLoopDevice attaches the given sparse file to the next
595
+// available loopback device. It returns an opened *osFile.
596
+func attachLoopDevice(filename string) (loop *osFile, err error) {
597
+	startIndex := 0
598
+
599
+	// Try to retrieve the next available loopback device via syscall.
600
+	// If it fails, we discard error and start loopking for a
601
+	// loopback from index 0.
602
+	if f, err := osOpenFile("/dev/loop-control", osORdOnly, 0644); err == nil {
603
+		if index, _, err := sysSyscall(sysSysIoctl, f.Fd(), LoopCtlGetFree, 0); err != 0 {
604
+			utils.Debugf("Error retrieving the next available loopback: %s", err)
605
+		} else if index > 0 {
606
+			startIndex = int(index)
607
+		}
608
+		f.Close()
609
+	}
610
+
611
+	// Open the given sparse file (use OpenFile because Open sets O_CLOEXEC)
612
+	f, err := osOpenFile(filename, osORdWr, 0644)
613
+	if err != nil {
614
+		return nil, err
615
+	}
616
+	defer f.Close()
617
+
618
+	var (
619
+		target   string
620
+		loopFile *osFile
621
+	)
622
+	// Start looking for a free /dev/loop
623
+	for i := startIndex; ; {
624
+		target = fmt.Sprintf("/dev/loop%d", i)
625
+
626
+		fi, err := osStat(target)
627
+		if err != nil {
628
+			if osIsNotExist(err) {
629
+				utils.Errorf("There are no more loopback device available.")
630
+			}
631
+		}
632
+
633
+		// FIXME: Check here if target is a block device (in C: S_ISBLK(mode))
634
+		if fi.IsDir() {
635
+		}
636
+
637
+		// Open the targeted loopback (use OpenFile because Open sets O_CLOEXEC)
638
+		loopFile, err = osOpenFile(target, osORdWr, 0644)
639
+		if err != nil {
640
+			return nil, err
641
+		}
642
+
643
+		// Try to attach to the loop file
644
+		if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopSetFd, f.Fd()); err != 0 {
645
+			loopFile.Close()
646
+			// If the error is EBUSY, then try the next loopback
647
+			if err != sysEBusy {
648
+				utils.Errorf("Cannot set up loopback device %s: %s", target, err)
649
+				return nil, err
650
+			}
651
+		} else {
652
+			// In case of success, we finished. Break the loop.
653
+			break
654
+		}
655
+		// In case of EBUSY error, the loop keep going.
656
+	}
657
+
658
+	// This can't happen, but let's be sure
659
+	if loopFile == nil {
660
+		return nil, fmt.Errorf("Unreachable code reached! Error attaching %s to a loopback device.", filename)
661
+	}
662
+
663
+	// Set the status of the loopback device
664
+	var loopInfo LoopInfo64
665
+
666
+	// Due to type incompatibility (string vs [64]uint8), we copy data
667
+	copy(loopInfo.loFileName[:], target[:])
668
+	loopInfo.loOffset = 0
669
+	loopInfo.loFlags = LoFlagsAutoClear
670
+
671
+	if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopSetStatus64, uintptr(unsafe.Pointer(&loopInfo))); err != 0 {
672
+		// If the call failed, then free the loopback device
673
+		utils.Errorf("Cannot set up loopback device info: %s", err)
674
+		if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopClrFd, 0); err != 0 {
675
+			utils.Errorf("Error while cleaning up the loopback device")
676
+		}
677
+		loopFile.Close()
678
+		return nil, err
679
+	}
680
+
681
+	return loopFile, nil
682
+}
... ...
@@ -2,124 +2,14 @@ package devmapper
2 2
 
3 3
 /*
4 4
 #cgo LDFLAGS: -L. -ldevmapper
5
-#include <stdio.h>
6
-#include <stdlib.h>
7
-#include <unistd.h>
8 5
 #include <libdevmapper.h>
9
-#include <linux/loop.h>
10
-#include <sys/types.h>
11
-#include <sys/stat.h>
12
-#include <fcntl.h>
13
-#include <sys/ioctl.h>
14
-#include <linux/fs.h>
15
-#include <errno.h>
16
-
17
-#ifndef LOOP_CTL_GET_FREE
18
-#define LOOP_CTL_GET_FREE       0x4C82
19
-#endif
20
-
21
-// FIXME: this could easily be rewritten in go
22
-char*			attach_loop_device(const char *filename, int *loop_fd_out)
23
-{
24
-  struct loop_info64	loopinfo = {0};
25
-  struct stat		st;
26
-  char			buf[64];
27
-  int			i, loop_fd, fd, start_index;
28
-  char*			loopname;
29
-
30
-
31
-  *loop_fd_out = -1;
32
-
33
-  start_index = 0;
34
-  fd = open("/dev/loop-control", O_RDONLY);
35
-  if (fd >= 0) {
36
-    start_index = ioctl(fd, LOOP_CTL_GET_FREE);
37
-    close(fd);
38
-
39
-    if (start_index < 0)
40
-      start_index = 0;
41
-  }
42
-
43
-  fd = open(filename, O_RDWR);
44
-  if (fd < 0) {
45
-    perror("open");
46
-    return NULL;
47
-  }
48
-
49
-  loop_fd = -1;
50
-  for (i = start_index ; loop_fd < 0 ; i++ ) {
51
-    if (sprintf(buf, "/dev/loop%d", i) < 0) {
52
-	close(fd);
53
-	return NULL;
54
-    }
55
-
56
-    if (stat(buf, &st)) {
57
-      if (!S_ISBLK(st.st_mode)) {
58
-	 fprintf(stderr, "[error] Loopback device %s is not a block device.\n", buf);
59
-      } else if (errno == ENOENT) {
60
-	fprintf(stderr, "[error] There are no more loopback device available.\n");
61
-      } else {
62
-	fprintf(stderr, "[error] Unkown error trying to stat the loopback device %s (errno: %d).\n", buf, errno);
63
-      }
64
-      close(fd);
65
-      return NULL;
66
-    }
67
-
68
-    loop_fd = open(buf, O_RDWR);
69
-    if (loop_fd < 0 && errno == ENOENT) {
70
-      fprintf(stderr, "[error] The loopback device %s does not exists.\n", buf);
71
-      close(fd);
72
-      return NULL;
73
-    } else if (loop_fd < 0) {
74
-	fprintf(stderr, "[error] Unkown error openning the loopback device %s. (errno: %d)\n", buf, errno);
75
-	continue;
76
-    }
77
-
78
-    if (ioctl(loop_fd, LOOP_SET_FD, (void *)(size_t)fd) < 0) {
79
-      int errsv = errno;
80
-      close(loop_fd);
81
-      loop_fd = -1;
82
-      if (errsv != EBUSY) {
83
-        close(fd);
84
-        fprintf(stderr, "cannot set up loopback device %s: %s", buf, strerror(errsv));
85
-        return NULL;
86
-      }
87
-      continue;
88
-    }
89
-
90
-    close(fd);
91
-
92
-    strncpy((char*)loopinfo.lo_file_name, buf, LO_NAME_SIZE);
93
-    loopinfo.lo_offset = 0;
94
-    loopinfo.lo_flags = LO_FLAGS_AUTOCLEAR;
95
-
96
-    if (ioctl(loop_fd, LOOP_SET_STATUS64, &loopinfo) < 0) {
97
-      perror("ioctl LOOP_SET_STATUS64");
98
-      if (ioctl(loop_fd, LOOP_CLR_FD, 0) < 0) {
99
-        perror("ioctl LOOP_CLR_FD");
100
-      }
101
-      close(loop_fd);
102
-      fprintf (stderr, "cannot set up loopback device info");
103
-      return (NULL);
104
-    }
105
-
106
-    loopname = strdup(buf);
107
-    if (loopname == NULL) {
108
-      close(loop_fd);
109
-      return (NULL);
110
-    }
111
-
112
-    *loop_fd_out = loop_fd;
113
-    return (loopname);
114
-  }
115
-
116
-  return (NULL);
117
-}
6
+#include <linux/loop.h> // FIXME: present only for defines, maybe we can remove it?
7
+#include <linux/fs.h>   // FIXME: present only for BLKGETSIZE64, maybe we can remove it?
118 8
 
9
+// FIXME: Can't we find a way to do the logging in pure Go?
119 10
 extern void DevmapperLogCallback(int level, char *file, int line, int dm_errno_or_class, char *str);
120 11
 
121
-static void	log_cb(int level, const char *file, int line,
122
-		       int dm_errno_or_class, const char *f, ...)
12
+static void	log_cb(int level, const char *file, int line, int dm_errno_or_class, const char *f, ...)
123 13
 {
124 14
   char buffer[256];
125 15
   va_list ap;
... ...
@@ -135,7 +25,6 @@ static void	log_with_errno_init()
135 135
 {
136 136
   dm_log_with_errno_init(log_cb);
137 137
 }
138
-
139 138
 */
140 139
 import "C"
141 140
 
... ...
@@ -147,6 +36,19 @@ type (
147 147
 	CDmTask C.struct_dm_task
148 148
 )
149 149
 
150
+// FIXME: Make sure the values are defined in C
151
+const (
152
+	LoopSetFd        = C.LOOP_SET_FD
153
+	LoopCtlGetFree   = C.LOOP_CTL_GET_FREE
154
+	LoopSetStatus64  = C.LOOP_SET_STATUS64
155
+	LoopClrFd        = C.LOOP_CLR_FD
156
+	LoFlagsAutoClear = C.LO_FLAGS_AUTOCLEAR
157
+	LoFlagsReadOnly  = C.LO_FLAGS_READ_ONLY
158
+	LoFlagsPartScan  = C.LO_FLAGS_PARTSCAN
159
+	LoKeySize        = C.LO_KEY_SIZE
160
+	LoNameSize       = C.LO_NAME_SIZE
161
+)
162
+
150 163
 var (
151 164
 	DmAttachLoopDevice       = dmAttachLoopDeviceFct
152 165
 	DmGetBlockSize           = dmGetBlockSizeFct
... ...
@@ -185,28 +87,26 @@ func dmTaskCreateFct(taskType int) *CDmTask {
185 185
 }
186 186
 
187 187
 func dmTaskRunFct(task *CDmTask) int {
188
-	return int(C.dm_task_run((*C.struct_dm_task)(task)))
188
+	ret, _ := C.dm_task_run((*C.struct_dm_task)(task))
189
+	return int(ret)
189 190
 }
190 191
 
191 192
 func dmTaskSetNameFct(task *CDmTask, name string) int {
192 193
 	Cname := C.CString(name)
193 194
 	defer free(Cname)
194 195
 
195
-	return int(C.dm_task_set_name((*C.struct_dm_task)(task),
196
-		Cname))
196
+	return int(C.dm_task_set_name((*C.struct_dm_task)(task), Cname))
197 197
 }
198 198
 
199 199
 func dmTaskSetMessageFct(task *CDmTask, message string) int {
200 200
 	Cmessage := C.CString(message)
201 201
 	defer free(Cmessage)
202 202
 
203
-	return int(C.dm_task_set_message((*C.struct_dm_task)(task),
204
-		Cmessage))
203
+	return int(C.dm_task_set_message((*C.struct_dm_task)(task), Cmessage))
205 204
 }
206 205
 
207 206
 func dmTaskSetSectorFct(task *CDmTask, sector uint64) int {
208
-	return int(C.dm_task_set_sector((*C.struct_dm_task)(task),
209
-		C.uint64_t(sector)))
207
+	return int(C.dm_task_set_sector((*C.struct_dm_task)(task), C.uint64_t(sector)))
210 208
 }
211 209
 
212 210
 func dmTaskSetCookieFct(task *CDmTask, cookie *uint, flags uint16) int {
... ...
@@ -214,13 +114,11 @@ func dmTaskSetCookieFct(task *CDmTask, cookie *uint, flags uint16) int {
214 214
 	defer func() {
215 215
 		*cookie = uint(cCookie)
216 216
 	}()
217
-	return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie,
218
-		C.uint16_t(flags)))
217
+	return int(C.dm_task_set_cookie((*C.struct_dm_task)(task), &cCookie, C.uint16_t(flags)))
219 218
 }
220 219
 
221 220
 func dmTaskSetAddNodeFct(task *CDmTask, addNode AddNodeType) int {
222
-	return int(C.dm_task_set_add_node((*C.struct_dm_task)(task),
223
-		C.dm_add_node_t(addNode)))
221
+	return int(C.dm_task_set_add_node((*C.struct_dm_task)(task), C.dm_add_node_t(addNode)))
224 222
 }
225 223
 
226 224
 func dmTaskSetRoFct(task *CDmTask) int {
... ...
@@ -236,14 +134,12 @@ func dmTaskAddTargetFct(task *CDmTask,
236 236
 	Cparams := C.CString(params)
237 237
 	defer free(Cparams)
238 238
 
239
-	return int(C.dm_task_add_target((*C.struct_dm_task)(task),
240
-		C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
239
+	return int(C.dm_task_add_target((*C.struct_dm_task)(task), C.uint64_t(start), C.uint64_t(size), Cttype, Cparams))
241 240
 }
242 241
 
243 242
 func dmGetLoopbackBackingFileFct(fd uintptr) (uint64, uint64, sysErrno) {
244 243
 	var lo64 C.struct_loop_info64
245
-	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64,
246
-		uintptr(unsafe.Pointer(&lo64)))
244
+	_, _, err := sysSyscall(sysSysIoctl, fd, C.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(&lo64)))
247 245
 	return uint64(lo64.lo_device), uint64(lo64.lo_inode), sysErrno(err)
248 246
 }
249 247
 
... ...
@@ -287,23 +183,23 @@ func dmGetNextTargetFct(task *CDmTask, next uintptr, start, length *uint64, targ
287 287
 		*params = C.GoString(Cparams)
288 288
 	}()
289 289
 
290
-	nextp := C.dm_get_next_target((*C.struct_dm_task)(task),
291
-		unsafe.Pointer(next), &Cstart, &Clength, &CtargetType, &Cparams)
290
+	nextp := C.dm_get_next_target((*C.struct_dm_task)(task), unsafe.Pointer(next), &Cstart, &Clength, &CtargetType, &Cparams)
292 291
 	return uintptr(nextp)
293 292
 }
294 293
 
295 294
 func dmAttachLoopDeviceFct(filename string, fd *int) string {
296
-	cFilename := C.CString(filename)
297
-	defer free(cFilename)
298
-
299
-	var cFd C.int
300
-	defer func() {
301
-		*fd = int(cFd)
302
-	}()
303
-
304
-	ret := C.attach_loop_device(cFilename, &cFd)
305
-	defer free(ret)
306
-	return C.GoString(ret)
295
+	return ""
296
+	// cFilename := C.CString(filename)
297
+	// defer free(cFilename)
298
+
299
+	// var cFd C.int
300
+	// defer func() {
301
+	// 	*fd = int(cFd)
302
+	// }()
303
+
304
+	// ret := C.attach_loop_device(cFilename, &cFd)
305
+	// defer free(ret)
306
+	// return C.GoString(ret)
307 307
 }
308 308
 
309 309
 func getBlockSizeFct(fd uintptr, size *uint64) sysErrno {
... ...
@@ -19,7 +19,11 @@ var (
19 19
 	sysCloseOnExec = syscall.CloseOnExec
20 20
 	sysSyscall     = syscall.Syscall
21 21
 
22
-	osOpenFile   = os.OpenFile
22
+	osOpenFile = func(name string, flag int, perm os.FileMode) (*osFile, error) {
23
+		f, err := os.OpenFile(name, flag, perm)
24
+		return &osFile{File: f}, err
25
+	}
26
+	osOpen       = func(name string) (*osFile, error) { f, err := os.Open(name); return &osFile{File: f}, err }
23 27
 	osNewFile    = os.NewFile
24 28
 	osCreate     = os.Create
25 29
 	osStat       = os.Stat
... ...
@@ -40,7 +44,9 @@ const (
40 40
 	sysMsRdOnly = syscall.MS_RDONLY
41 41
 	sysEInval   = syscall.EINVAL
42 42
 	sysSysIoctl = syscall.SYS_IOCTL
43
+	sysEBusy    = syscall.EBUSY
43 44
 
45
+	osORdOnly = os.O_RDONLY
44 46
 	osORdWr   = os.O_RDWR
45 47
 	osOCreate = os.O_CREATE
46 48
 )