1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <vgoyal@redhat.com>
This commit is contained in:
Vivek Goyal 2014-12-03 13:06:43 -05:00 committed by root
parent 39dc7829de
commit a44c23fe66

View file

@ -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