From 14d0dd855ee1e7cd1a3185c3d5a00e7afccb5c43 Mon Sep 17 00:00:00 2001 From: Vivek Goyal Date: Wed, 3 Dec 2014 13:06:43 -0500 Subject: [PATCH] devmapper: Open code createDevice() and createSnapDevice() Open code createDevice() and createSnapDevice() and move all the logic in the caller. This is a sheer code reorganization so that all device Id allocation logic is in one function. That way in case of erros, one can easily cleanup and mark device Id free again. (Later patches benefit from it). Signed-off-by: Vivek Goyal --- daemon/graphdriver/devmapper/deviceset.go | 36 ++++++----------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/daemon/graphdriver/devmapper/deviceset.go b/daemon/graphdriver/devmapper/deviceset.go index bf24b5164c..17fbf715a3 100644 --- a/daemon/graphdriver/devmapper/deviceset.go +++ b/daemon/graphdriver/devmapper/deviceset.go @@ -505,27 +505,20 @@ func (devices *DeviceSet) getNextDeviceId() int { return devices.NextDeviceId } -func (devices *DeviceSet) createDevice(deviceId *int) error { +func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) { + deviceId := devices.getNextDeviceId() for { - if err := devicemapper.CreateDevice(devices.getPoolDevName(), *deviceId); err != nil { + if err := devicemapper.CreateDevice(devices.getPoolDevName(), deviceId); err != nil { if devicemapper.DeviceIdExists(err) { // Device Id already exists. Try a new one. - *deviceId = devices.getNextDeviceId() + deviceId = devices.getNextDeviceId() continue } log.Debugf("Error creating device: %s", err) - return err + return nil, err } break } - return nil -} - -func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) { - deviceId := devices.getNextDeviceId() - if err := devices.createDevice(&deviceId); err != nil { - return nil, err - } transactionId := devices.allocateTransactionId() log.Debugf("Registering device (id %v) with FS size %v", deviceId, devices.baseFsSize) @@ -543,15 +536,13 @@ func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) { return info, nil } -func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) error { - log.Debugf("[deviceset] createSnapDevice() DeviceId=%d", *deviceId) - defer log.Debugf("[deviceset] createSnapDevice() END DeviceId=%d", *deviceId) - +func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error { + deviceId := devices.getNextDeviceId() for { - if err := devicemapper.CreateSnapDevice(devices.getPoolDevName(), *deviceId, baseInfo.Name(), baseInfo.DeviceId); err != nil { + if err := devicemapper.CreateSnapDevice(devices.getPoolDevName(), deviceId, baseInfo.Name(), baseInfo.DeviceId); err != nil { if devicemapper.DeviceIdExists(err) { // Device Id already exists. Try a new one. - *deviceId = devices.getNextDeviceId() + deviceId = devices.getNextDeviceId() continue } log.Debugf("Error creating snap device: %s", err) @@ -559,15 +550,6 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err } break } - return nil -} - -func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error { - deviceId := devices.getNextDeviceId() - if err := devices.createSnapDevice(baseInfo, &deviceId); err != nil { - log.Debugf("Error creating snap device: %s", err) - return err - } transactionId := devices.allocateTransactionId() if _, err := devices.registerDevice(deviceId, hash, baseInfo.Size, transactionId); err != nil {