Fix for CTS test CtsAppSecurityHostTestCases

Fix random failures while running CtsAppSecurityHostTestCases with
adoptable storage due to a format failure. The crypto_blkdev node
might not be immediately available after create sometimes. Adding
a wait in create to make sure the node is available.

CRs-Fixed: 2324063
Change-Id: I8a7599a9253ac530b05a97ed34829dad1f7f7a40
diff --git a/model/PrivateVolume.cpp b/model/PrivateVolume.cpp
index a692ea9..3334cbb 100644
--- a/model/PrivateVolume.cpp
+++ b/model/PrivateVolume.cpp
@@ -38,6 +38,9 @@
 #include <sys/wait.h>
 #include <thread>
 
+#define RETRY_MOUNT_ATTEMPTS 10
+#define RETRY_MOUNT_DELAY_SECONDS 1
+
 using android::base::StringPrintf;
 using android::vold::IsVirtioBlkDevice;
 
@@ -94,6 +97,26 @@
         return -EIO;
     }
 
+    int fd = 0;
+    int retries = RETRY_MOUNT_ATTEMPTS;
+    while ((fd = open(mDmDevPath.c_str(), O_WRONLY|O_CLOEXEC)) < 0) {
+        if (retries > 0) {
+            retries--;
+            PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
+                            << " for private volume. err=" << errno
+                            << "(" << strerror(errno) << "), retrying for the "
+                            << RETRY_MOUNT_ATTEMPTS - retries << " time";
+            sleep(RETRY_MOUNT_DELAY_SECONDS);
+        } else {
+            PLOG(ERROR) << "Error opening crypto_blkdev " << mDmDevPath
+                            << " for private volume. err=" << errno
+                            << "(" << strerror(errno) << "), retried "
+                            << RETRY_MOUNT_ATTEMPTS << " times";
+            close(fd);
+            return -EIO;
+        }
+    }
+    close(fd);
     return OK;
 }