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

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

View file

@ -505,27 +505,20 @@ func (devices *DeviceSet) getNextDeviceId() int {
return devices.NextDeviceId return devices.NextDeviceId
} }
func (devices *DeviceSet) createDevice(deviceId *int) error { func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) {
deviceId := devices.getNextDeviceId()
for { for {
if err := devicemapper.CreateDevice(devices.getPoolDevName(), *deviceId); err != nil { if err := devicemapper.CreateDevice(devices.getPoolDevName(), deviceId); err != nil {
if devicemapper.DeviceIdExists(err) { if devicemapper.DeviceIdExists(err) {
// Device Id already exists. Try a new one. // Device Id already exists. Try a new one.
*deviceId = devices.getNextDeviceId() deviceId = devices.getNextDeviceId()
continue continue
} }
log.Debugf("Error creating device: %s", err) log.Debugf("Error creating device: %s", err)
return err return nil, err
} }
break 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() transactionId := devices.allocateTransactionId()
log.Debugf("Registering device (id %v) with FS size %v", deviceId, devices.baseFsSize) 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 return info, nil
} }
func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) error { func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error {
log.Debugf("[deviceset] createSnapDevice() DeviceId=%d", *deviceId) deviceId := devices.getNextDeviceId()
defer log.Debugf("[deviceset] createSnapDevice() END DeviceId=%d", *deviceId)
for { 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) { if devicemapper.DeviceIdExists(err) {
// Device Id already exists. Try a new one. // Device Id already exists. Try a new one.
*deviceId = devices.getNextDeviceId() deviceId = devices.getNextDeviceId()
continue continue
} }
log.Debugf("Error creating snap device: %s", err) log.Debugf("Error creating snap device: %s", err)
@ -559,15 +550,6 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err
} }
break 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() transactionId := devices.allocateTransactionId()
if _, err := devices.registerDevice(deviceId, hash, baseInfo.Size, transactionId); err != nil { if _, err := devices.registerDevice(deviceId, hash, baseInfo.Size, transactionId); err != nil {