devmapper: Use device id as specified by caller
Currently devicemapper CreateDevice and CreateSnapDevice keep on retrying device creation till a suitable device id is found. With new transaction mechanism we need to store device id in transaction before it has been created. So change the logic in such a way that caller decides the devices Id to use. If that device Id is not available, caller bumps up the device Id and retries. That way caller can update transaciton too when it tries a new Id. Transaction related patches will come later in the series. Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
This commit is contained in:
parent
2b403ab360
commit
12524d5657
1 changed files with 55 additions and 53 deletions
|
@ -67,6 +67,7 @@ var (
|
||||||
ErrGetLoopbackBackingFile = errors.New("Unable to get loopback backing file")
|
ErrGetLoopbackBackingFile = errors.New("Unable to get loopback backing file")
|
||||||
ErrLoopbackSetCapacity = errors.New("Unable set loopback capacity")
|
ErrLoopbackSetCapacity = errors.New("Unable set loopback capacity")
|
||||||
ErrBusy = errors.New("Device is Busy")
|
ErrBusy = errors.New("Device is Busy")
|
||||||
|
ErrDeviceIdExists = errors.New("Device Id Exists")
|
||||||
|
|
||||||
dmSawBusy bool
|
dmSawBusy bool
|
||||||
dmSawExist bool
|
dmSawExist bool
|
||||||
|
@ -97,6 +98,16 @@ type (
|
||||||
AddNodeType int
|
AddNodeType int
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Returns whether error conveys the information about device Id already
|
||||||
|
// exist or not. This will be true if device creation or snap creation
|
||||||
|
// operation fails if device or snap device already exists in pool.
|
||||||
|
// Current implementation is little crude as it scans the error string
|
||||||
|
// for exact pattern match. Replacing it with more robust implementation
|
||||||
|
// is desirable.
|
||||||
|
func DeviceIdExists(err error) bool {
|
||||||
|
return fmt.Sprint(err) == fmt.Sprint(ErrDeviceIdExists)
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Task) destroy() {
|
func (t *Task) destroy() {
|
||||||
if t != nil {
|
if t != nil {
|
||||||
DmTaskDestroy(t.unmanaged)
|
DmTaskDestroy(t.unmanaged)
|
||||||
|
@ -528,10 +539,8 @@ func ResumeDevice(name string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDevice(poolName string, deviceId *int) error {
|
func CreateDevice(poolName string, deviceId int) error {
|
||||||
log.Debugf("[devmapper] CreateDevice(poolName=%v, deviceId=%v)", poolName, *deviceId)
|
log.Debugf("[devmapper] CreateDevice(poolName=%v, deviceId=%v)", poolName, deviceId)
|
||||||
|
|
||||||
for {
|
|
||||||
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
||||||
if task == nil {
|
if task == nil {
|
||||||
return err
|
return err
|
||||||
|
@ -541,20 +550,18 @@ func CreateDevice(poolName string, deviceId *int) error {
|
||||||
return fmt.Errorf("Can't set sector %s", err)
|
return fmt.Errorf("Can't set sector %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := task.SetMessage(fmt.Sprintf("create_thin %d", *deviceId)); err != nil {
|
if err := task.SetMessage(fmt.Sprintf("create_thin %d", deviceId)); err != nil {
|
||||||
return fmt.Errorf("Can't set message %s", err)
|
return fmt.Errorf("Can't set message %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dmSawExist = false // reset before the task is run
|
dmSawExist = false // reset before the task is run
|
||||||
if err := task.Run(); err != nil {
|
if err := task.Run(); err != nil {
|
||||||
|
// Caller wants to know about ErrDeviceIdExists so that it can try with a different device id.
|
||||||
if dmSawExist {
|
if dmSawExist {
|
||||||
// Already exists, try next id
|
return ErrDeviceIdExists
|
||||||
*deviceId++
|
} else {
|
||||||
continue
|
|
||||||
}
|
|
||||||
return fmt.Errorf("Error running CreateDevice %s", err)
|
return fmt.Errorf("Error running CreateDevice %s", err)
|
||||||
}
|
}
|
||||||
break
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -607,7 +614,7 @@ func ActivateDevice(poolName string, name string, deviceId int, size uint64) err
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSnapDevice(poolName string, deviceId *int, baseName string, baseDeviceId int) error {
|
func CreateSnapDevice(poolName string, deviceId int, baseName string, baseDeviceId int) error {
|
||||||
devinfo, _ := GetInfo(baseName)
|
devinfo, _ := GetInfo(baseName)
|
||||||
doSuspend := devinfo != nil && devinfo.Exists != 0
|
doSuspend := devinfo != nil && devinfo.Exists != 0
|
||||||
|
|
||||||
|
@ -617,7 +624,6 @@ func CreateSnapDevice(poolName string, deviceId *int, baseName string, baseDevic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
|
||||||
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
task, err := TaskCreateNamed(DeviceTargetMsg, poolName)
|
||||||
if task == nil {
|
if task == nil {
|
||||||
if doSuspend {
|
if doSuspend {
|
||||||
|
@ -633,7 +639,7 @@ func CreateSnapDevice(poolName string, deviceId *int, baseName string, baseDevic
|
||||||
return fmt.Errorf("Can't set sector %s", err)
|
return fmt.Errorf("Can't set sector %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := task.SetMessage(fmt.Sprintf("create_snap %d %d", *deviceId, baseDeviceId)); err != nil {
|
if err := task.SetMessage(fmt.Sprintf("create_snap %d %d", deviceId, baseDeviceId)); err != nil {
|
||||||
if doSuspend {
|
if doSuspend {
|
||||||
ResumeDevice(baseName)
|
ResumeDevice(baseName)
|
||||||
}
|
}
|
||||||
|
@ -642,19 +648,15 @@ func CreateSnapDevice(poolName string, deviceId *int, baseName string, baseDevic
|
||||||
|
|
||||||
dmSawExist = false // reset before the task is run
|
dmSawExist = false // reset before the task is run
|
||||||
if err := task.Run(); err != nil {
|
if err := task.Run(); err != nil {
|
||||||
if dmSawExist {
|
|
||||||
// Already exists, try next id
|
|
||||||
*deviceId++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if doSuspend {
|
if doSuspend {
|
||||||
ResumeDevice(baseName)
|
ResumeDevice(baseName)
|
||||||
}
|
}
|
||||||
|
// Caller wants to know about ErrDeviceIdExists so that it can try with a different device id.
|
||||||
|
if dmSawExist {
|
||||||
|
return ErrDeviceIdExists
|
||||||
|
} else {
|
||||||
return fmt.Errorf("Error running DeviceCreate (createSnapDevice) %s", err)
|
return fmt.Errorf("Error running DeviceCreate (createSnapDevice) %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if doSuspend {
|
if doSuspend {
|
||||||
|
|
Loading…
Reference in a new issue