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>
| ... | ... |
@@ -500,13 +500,17 @@ func (devices *DeviceSet) incNextDeviceId() {
|
| 500 | 500 |
devices.NextDeviceId = (devices.NextDeviceId + 1) & MaxDeviceId |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 |
+func (devices *DeviceSet) getNextDeviceId() int {
|
|
| 504 |
+ devices.incNextDeviceId() |
|
| 505 |
+ return devices.NextDeviceId |
|
| 506 |
+} |
|
| 507 |
+ |
|
| 503 | 508 |
func (devices *DeviceSet) createDevice(deviceId *int) error {
|
| 504 | 509 |
for {
|
| 505 | 510 |
if err := devicemapper.CreateDevice(devices.getPoolDevName(), *deviceId); err != nil {
|
| 506 | 511 |
if devicemapper.DeviceIdExists(err) {
|
| 507 | 512 |
// Device Id already exists. Try a new one. |
| 508 |
- devices.incNextDeviceId() |
|
| 509 |
- *deviceId = devices.NextDeviceId |
|
| 513 |
+ *deviceId = devices.getNextDeviceId() |
|
| 510 | 514 |
continue |
| 511 | 515 |
} |
| 512 | 516 |
log.Debugf("Error creating device: %s", err)
|
| ... | ... |
@@ -514,12 +518,11 @@ func (devices *DeviceSet) createDevice(deviceId *int) error {
|
| 514 | 514 |
} |
| 515 | 515 |
break |
| 516 | 516 |
} |
| 517 |
- devices.incNextDeviceId() |
|
| 518 | 517 |
return nil |
| 519 | 518 |
} |
| 520 | 519 |
|
| 521 | 520 |
func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) {
|
| 522 |
- deviceId := devices.NextDeviceId |
|
| 521 |
+ deviceId := devices.getNextDeviceId() |
|
| 523 | 522 |
if err := devices.createDevice(&deviceId); err != nil {
|
| 524 | 523 |
return nil, err |
| 525 | 524 |
} |
| ... | ... |
@@ -548,8 +551,7 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err |
| 548 | 548 |
if err := devicemapper.CreateSnapDevice(devices.getPoolDevName(), *deviceId, baseInfo.Name(), baseInfo.DeviceId); err != nil {
|
| 549 | 549 |
if devicemapper.DeviceIdExists(err) {
|
| 550 | 550 |
// Device Id already exists. Try a new one. |
| 551 |
- devices.incNextDeviceId() |
|
| 552 |
- *deviceId = devices.NextDeviceId |
|
| 551 |
+ *deviceId = devices.getNextDeviceId() |
|
| 553 | 552 |
continue |
| 554 | 553 |
} |
| 555 | 554 |
log.Debugf("Error creating snap device: %s", err)
|
| ... | ... |
@@ -557,12 +559,11 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err |
| 557 | 557 |
} |
| 558 | 558 |
break |
| 559 | 559 |
} |
| 560 |
- devices.incNextDeviceId() |
|
| 561 | 560 |
return nil |
| 562 | 561 |
} |
| 563 | 562 |
|
| 564 | 563 |
func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error {
|
| 565 |
- deviceId := devices.NextDeviceId |
|
| 564 |
+ deviceId := devices.getNextDeviceId() |
|
| 566 | 565 |
if err := devices.createSnapDevice(baseInfo, &deviceId); err != nil {
|
| 567 | 566 |
log.Debugf("Error creating snap device: %s", err)
|
| 568 | 567 |
return err |