Browse code

devmapper: Remove transaction Id update from saveMetaData()

Right now saveMetaData() is kind of little overloaded function. It is
supposed to save file metadata to disk. But in addition if user has
bumped up NewTransactionId before calling saveMetaData(), then it will
also update the transaction ID in pool.

Keep saveMetaData() simple and let it just save the file. Any update
of pool transaction ID is done inline in the code which needs it.

Also create an helper function updatePoolTransactionId() to update pool
transaction Id.

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>

Vivek Goyal authored on 2014/12/04 03:06:43
Showing 1 changed files
... ...
@@ -204,6 +204,16 @@ func (devices *DeviceSet) allocateTransactionId() uint64 {
204 204
 	return devices.NewTransactionId
205 205
 }
206 206
 
207
+func (devices *DeviceSet) updatePoolTransactionId() error {
208
+	if devices.NewTransactionId != devices.TransactionId {
209
+		if err := devicemapper.SetTransactionId(devices.getPoolDevName(), devices.TransactionId, devices.NewTransactionId); err != nil {
210
+			return fmt.Errorf("Error setting devmapper transaction ID: %s", err)
211
+		}
212
+		devices.TransactionId = devices.NewTransactionId
213
+	}
214
+	return nil
215
+}
216
+
207 217
 func (devices *DeviceSet) removeMetadata(info *DevInfo) error {
208 218
 	if err := os.RemoveAll(devices.metadataFile(info)); err != nil {
209 219
 		return fmt.Errorf("Error removing metadata file %s: %s", devices.metadataFile(info), err)
... ...
@@ -246,13 +256,6 @@ func (devices *DeviceSet) saveMetadata(info *DevInfo) error {
246 246
 	if err := devices.writeMetaFile(jsonData, devices.metadataFile(info)); err != nil {
247 247
 		return err
248 248
 	}
249
-
250
-	if devices.NewTransactionId != devices.TransactionId {
251
-		if err = devicemapper.SetTransactionId(devices.getPoolDevName(), devices.TransactionId, devices.NewTransactionId); err != nil {
252
-			return fmt.Errorf("Error setting devmapper transition ID: %s", err)
253
-		}
254
-		devices.TransactionId = devices.NewTransactionId
255
-	}
256 249
 	return nil
257 250
 }
258 251
 
... ...
@@ -294,6 +297,15 @@ func (devices *DeviceSet) registerDevice(id int, hash string, size uint64) (*Dev
294 294
 		return nil, err
295 295
 	}
296 296
 
297
+	if err := devices.updatePoolTransactionId(); err != nil {
298
+		// Remove unused device
299
+		devices.devicesLock.Lock()
300
+		delete(devices.Devices, hash)
301
+		devices.devicesLock.Unlock()
302
+		devices.removeMetadata(info)
303
+		return nil, err
304
+	}
305
+
297 306
 	return info, nil
298 307
 }
299 308