From a44c23fe6604d1de59c64bbb9dc234c7c3dbede9 Mon Sep 17 00:00:00 2001 From: Vivek Goyal Date: Wed, 3 Dec 2014 13:06:43 -0500 Subject: [PATCH] devmapper: Provide a helper function getNextDeviceId() Right now we are accessing devices.NextDeviceId directly and also incrementing it at various places. Instead provide a helper function which is responsile for incrementing NextDeviceId and return next deviceId. This is just code structuring. This will help later once we convert this function to find a free device Id and it goes through a bitmap of used/free device Ids. Signed-off-by: Vivek Goyal --- daemon/graphdriver/devmapper/deviceset.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/daemon/graphdriver/devmapper/deviceset.go b/daemon/graphdriver/devmapper/deviceset.go index fd4a11e5e2..bf24b5164c 100644 --- a/daemon/graphdriver/devmapper/deviceset.go +++ b/daemon/graphdriver/devmapper/deviceset.go @@ -500,13 +500,17 @@ func (devices *DeviceSet) incNextDeviceId() { devices.NextDeviceId = (devices.NextDeviceId + 1) & MaxDeviceId } +func (devices *DeviceSet) getNextDeviceId() int { + devices.incNextDeviceId() + return devices.NextDeviceId +} + func (devices *DeviceSet) createDevice(deviceId *int) error { for { if err := devicemapper.CreateDevice(devices.getPoolDevName(), *deviceId); err != nil { if devicemapper.DeviceIdExists(err) { // Device Id already exists. Try a new one. - devices.incNextDeviceId() - *deviceId = devices.NextDeviceId + *deviceId = devices.getNextDeviceId() continue } log.Debugf("Error creating device: %s", err) @@ -514,12 +518,11 @@ func (devices *DeviceSet) createDevice(deviceId *int) error { } break } - devices.incNextDeviceId() return nil } func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) { - deviceId := devices.NextDeviceId + deviceId := devices.getNextDeviceId() if err := devices.createDevice(&deviceId); err != nil { return nil, err } @@ -548,8 +551,7 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err 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. - devices.incNextDeviceId() - *deviceId = devices.NextDeviceId + *deviceId = devices.getNextDeviceId() continue } log.Debugf("Error creating snap device: %s", err) @@ -557,12 +559,11 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err } break } - devices.incNextDeviceId() return nil } func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error { - deviceId := devices.NextDeviceId + deviceId := devices.getNextDeviceId() if err := devices.createSnapDevice(baseInfo, &deviceId); err != nil { log.Debugf("Error creating snap device: %s", err) return err