Refactor attach loop device in pure Go

This commit is contained in:
Guillaume J. Charmes
2013-11-27 15:39:30 -08:00
parent 8b99e4ed37
commit 74c8f7af75
4 changed files with 160 additions and 161 deletions
+111 -14
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"github.com/dotcloud/docker/utils"
"runtime"
"unsafe"
)
type DevmapperLogger interface {
@@ -177,15 +178,6 @@ func (t *Task) GetNextTarget(next uintptr) (nextPtr uintptr, start uint64,
start, length, targetType, params
}
func AttachLoopDevice(filename string) (*osFile, error) {
var fd int
res := DmAttachLoopDevice(filename, &fd)
if res == "" {
return nil, ErrAttachLoopbackDevice
}
return &osFile{File: osNewFile(uintptr(fd), res)}, nil
}
func getLoopbackBackingFile(file *osFile) (uint64, uint64, error) {
dev, inode, err := DmGetLoopbackBackingFile(file.Fd())
if err != 0 {
@@ -223,11 +215,10 @@ func FindLoopDeviceFor(file *osFile) *osFile {
continue
}
dev, inode, err := getLoopbackBackingFile(&osFile{File: file})
dev, inode, err := getLoopbackBackingFile(file)
if err == nil && dev == targetDevice && inode == targetInode {
return &osFile{File: file}
return file
}
file.Close()
}
@@ -420,7 +411,7 @@ func suspendDevice(name string) error {
return err
}
if err := task.Run(); err != nil {
return fmt.Errorf("Error running DeviceSuspend")
return fmt.Errorf("Error running DeviceSuspend: %s", err)
}
return nil
}
@@ -437,7 +428,7 @@ func resumeDevice(name string) error {
}
if err := task.Run(); err != nil {
return fmt.Errorf("Error running DeviceSuspend")
return fmt.Errorf("Error running DeviceResume")
}
UdevWait(cookie)
@@ -574,3 +565,109 @@ func (devices *DeviceSet) createSnapDevice(poolName string, deviceId int, baseNa
return nil
}
type LoopInfo64 struct {
loDevice uint64 /* ioctl r/o */
loInode uint64 /* ioctl r/o */
loRdevice uint64 /* ioctl r/o */
loOffset uint64
loSizelimit uint64 /* bytes, 0 == max available */
loNumber uint32 /* ioctl r/o */
loEncrypt_type uint32
loEncrypt_key_size uint32 /* ioctl w/o */
loFlags uint32 /* ioctl r/o */
loFileName [LoNameSize]uint8
loCryptName [LoNameSize]uint8
loEncryptKey [LoKeySize]uint8 /* ioctl w/o */
loInit [2]uint64
}
// attachLoopDevice attaches the given sparse file to the next
// available loopback device. It returns an opened *osFile.
func attachLoopDevice(filename string) (loop *osFile, err error) {
startIndex := 0
// Try to retrieve the next available loopback device via syscall.
// If it fails, we discard error and start loopking for a
// loopback from index 0.
if f, err := osOpenFile("/dev/loop-control", osORdOnly, 0644); err == nil {
if index, _, err := sysSyscall(sysSysIoctl, f.Fd(), LoopCtlGetFree, 0); err != 0 {
utils.Debugf("Error retrieving the next available loopback: %s", err)
} else if index > 0 {
startIndex = int(index)
}
f.Close()
}
// Open the given sparse file (use OpenFile because Open sets O_CLOEXEC)
f, err := osOpenFile(filename, osORdWr, 0644)
if err != nil {
return nil, err
}
defer f.Close()
var (
target string
loopFile *osFile
)
// Start looking for a free /dev/loop
for i := startIndex; ; {
target = fmt.Sprintf("/dev/loop%d", i)
fi, err := osStat(target)
if err != nil {
if osIsNotExist(err) {
utils.Errorf("There are no more loopback device available.")
}
}
// FIXME: Check here if target is a block device (in C: S_ISBLK(mode))
if fi.IsDir() {
}
// Open the targeted loopback (use OpenFile because Open sets O_CLOEXEC)
loopFile, err = osOpenFile(target, osORdWr, 0644)
if err != nil {
return nil, err
}
// Try to attach to the loop file
if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopSetFd, f.Fd()); err != 0 {
loopFile.Close()
// If the error is EBUSY, then try the next loopback
if err != sysEBusy {
utils.Errorf("Cannot set up loopback device %s: %s", target, err)
return nil, err
}
} else {
// In case of success, we finished. Break the loop.
break
}
// In case of EBUSY error, the loop keep going.
}
// This can't happen, but let's be sure
if loopFile == nil {
return nil, fmt.Errorf("Unreachable code reached! Error attaching %s to a loopback device.", filename)
}
// Set the status of the loopback device
var loopInfo LoopInfo64
// Due to type incompatibility (string vs [64]uint8), we copy data
copy(loopInfo.loFileName[:], target[:])
loopInfo.loOffset = 0
loopInfo.loFlags = LoFlagsAutoClear
if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopSetStatus64, uintptr(unsafe.Pointer(&loopInfo))); err != 0 {
// If the call failed, then free the loopback device
utils.Errorf("Cannot set up loopback device info: %s", err)
if _, _, err := sysSyscall(sysSysIoctl, loopFile.Fd(), LoopClrFd, 0); err != 0 {
utils.Errorf("Error while cleaning up the loopback device")
}
loopFile.Close()
return nil, err
}
return loopFile, nil
}