Browse code

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>

Vivek Goyal authored on 2014/12/04 03:06:43
Showing 1 changed files
... ...
@@ -505,27 +505,20 @@ func (devices *DeviceSet) getNextDeviceId() int {
505 505
 	return devices.NextDeviceId
506 506
 }
507 507
 
508
-func (devices *DeviceSet) createDevice(deviceId *int) error {
508
+func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) {
509
+	deviceId := devices.getNextDeviceId()
509 510
 	for {
510
-		if err := devicemapper.CreateDevice(devices.getPoolDevName(), *deviceId); err != nil {
511
+		if err := devicemapper.CreateDevice(devices.getPoolDevName(), deviceId); err != nil {
511 512
 			if devicemapper.DeviceIdExists(err) {
512 513
 				// Device Id already exists. Try a new one.
513
-				*deviceId = devices.getNextDeviceId()
514
+				deviceId = devices.getNextDeviceId()
514 515
 				continue
515 516
 			}
516 517
 			log.Debugf("Error creating device: %s", err)
517
-			return err
518
+			return nil, err
518 519
 		}
519 520
 		break
520 521
 	}
521
-	return nil
522
-}
523
-
524
-func (devices *DeviceSet) createRegisterDevice(hash string) (*DevInfo, error) {
525
-	deviceId := devices.getNextDeviceId()
526
-	if err := devices.createDevice(&deviceId); err != nil {
527
-		return nil, err
528
-	}
529 522
 
530 523
 	transactionId := devices.allocateTransactionId()
531 524
 	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) {
543 543
 	return info, nil
544 544
 }
545 545
 
546
-func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) error {
547
-	log.Debugf("[deviceset] createSnapDevice() DeviceId=%d", *deviceId)
548
-	defer log.Debugf("[deviceset] createSnapDevice() END DeviceId=%d", *deviceId)
549
-
546
+func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error {
547
+	deviceId := devices.getNextDeviceId()
550 548
 	for {
551
-		if err := devicemapper.CreateSnapDevice(devices.getPoolDevName(), *deviceId, baseInfo.Name(), baseInfo.DeviceId); err != nil {
549
+		if err := devicemapper.CreateSnapDevice(devices.getPoolDevName(), deviceId, baseInfo.Name(), baseInfo.DeviceId); err != nil {
552 550
 			if devicemapper.DeviceIdExists(err) {
553 551
 				// Device Id already exists. Try a new one.
554
-				*deviceId = devices.getNextDeviceId()
552
+				deviceId = devices.getNextDeviceId()
555 553
 				continue
556 554
 			}
557 555
 			log.Debugf("Error creating snap device: %s", err)
... ...
@@ -559,15 +550,6 @@ func (devices *DeviceSet) createSnapDevice(baseInfo *DevInfo, deviceId *int) err
559 559
 		}
560 560
 		break
561 561
 	}
562
-	return nil
563
-}
564
-
565
-func (devices *DeviceSet) createRegisterSnapDevice(hash string, baseInfo *DevInfo) error {
566
-	deviceId := devices.getNextDeviceId()
567
-	if err := devices.createSnapDevice(baseInfo, &deviceId); err != nil {
568
-		log.Debugf("Error creating snap device: %s", err)
569
-		return err
570
-	}
571 562
 
572 563
 	transactionId := devices.allocateTransactionId()
573 564
 	if _, err := devices.registerDevice(deviceId, hash, baseInfo.Size, transactionId); err != nil {