Retry deleting dm devices.

For some reason this can be racy; until we understand the root cause,
retry to unblock AdoptableHostTest.

Bug: 149396179
Test: atest AdoptableHostTest no longer hangs
Change-Id: I162ff8ad305535e7a4fab3d88f38b687b50cf4a3
diff --git a/model/PrivateVolume.cpp b/model/PrivateVolume.cpp
index 9f1f089..75aa938 100644
--- a/model/PrivateVolume.cpp
+++ b/model/PrivateVolume.cpp
@@ -36,6 +36,7 @@
 #include <sys/sysmacros.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <thread>
 
 using android::base::StringPrintf;
 
@@ -68,8 +69,19 @@
 
     // Recover from stale vold by tearing down any old mappings
     auto& dm = dm::DeviceMapper::Instance();
-    if (!dm.DeleteDeviceIfExists(getId())) {
+    // TODO(b/149396179) there appears to be a race somewhere in the system where trying
+    // to delete the device fails with EBUSY; for now, work around this by retrying.
+    bool ret;
+    int tries = 10;
+    while (tries-- > 0) {
+        ret = dm.DeleteDeviceIfExists(getId());
+        if (ret || errno != EBUSY) {
+            break;
+        }
         PLOG(ERROR) << "Cannot remove dm device " << getId();
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    }
+    if (!ret) {
         return -EIO;
     }
 
@@ -86,8 +98,19 @@
 
 status_t PrivateVolume::doDestroy() {
     auto& dm = dm::DeviceMapper::Instance();
-    if (!dm.DeleteDevice(getId())) {
+    // TODO(b/149396179) there appears to be a race somewhere in the system where trying
+    // to delete the device fails with EBUSY; for now, work around this by retrying.
+    bool ret;
+    int tries = 10;
+    while (tries-- > 0) {
+        ret = dm.DeleteDevice(getId());
+        if (ret || errno != EBUSY) {
+            break;
+        }
         PLOG(ERROR) << "Cannot remove dm device " << getId();
+        std::this_thread::sleep_for(std::chrono::milliseconds(100));
+    }
+    if (!ret) {
         return -EIO;
     }
     return DestroyDeviceNode(mRawDevPath);