Wait for dm device to be ready before format

It can sometimes take a moment for the dm-device to appear after
creation, causing operations on it such as formatting to fail.
Ensure the device exists before create_crypto_blk_dev returns.

Test: adb sm set-virtual-disk true and format as adoptable.
Bug: 117586466
Change-Id: Id8f571b551f50fc759e78d917e4ac3080e926722
This commit is contained in:
Paul Crowley 2018-10-30 15:59:24 -07:00
parent 2e4f425995
commit 298fa3264f
3 changed files with 31 additions and 0 deletions

View file

@ -19,6 +19,7 @@
#include "Process.h" #include "Process.h"
#include "sehandle.h" #include "sehandle.h"
#include <android-base/chrono_utils.h>
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/properties.h> #include <android-base/properties.h>
@ -41,13 +42,16 @@
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <thread>
#ifndef UMOUNT_NOFOLLOW #ifndef UMOUNT_NOFOLLOW
#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
#endif #endif
using namespace std::chrono_literals;
using android::base::ReadFileToString; using android::base::ReadFileToString;
using android::base::StringPrintf; using android::base::StringPrintf;
@ -858,5 +862,20 @@ status_t DeleteDirContentsAndDir(const std::string& pathname) {
return OK; return OK;
} }
// TODO(118708649): fix duplication with init/util.h
status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) {
android::base::Timer t;
while (t.duration() < timeout) {
struct stat sb;
if (stat(filename, &sb) != -1) {
LOG(INFO) << "wait for '" << filename << "' took " << t;
return 0;
}
std::this_thread::sleep_for(10ms);
}
LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t;
return -1;
}
} // namespace vold } // namespace vold
} // namespace android } // namespace android

View file

@ -24,6 +24,7 @@
#include <selinux/selinux.h> #include <selinux/selinux.h>
#include <utils/Errors.h> #include <utils/Errors.h>
#include <chrono>
#include <string> #include <string>
#include <vector> #include <vector>
@ -134,6 +135,8 @@ status_t UnmountTree(const std::string& prefix);
status_t DeleteDirContentsAndDir(const std::string& pathname); status_t DeleteDirContentsAndDir(const std::string& pathname);
status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout);
} // namespace vold } // namespace vold
} // namespace android } // namespace android

View file

@ -30,6 +30,7 @@
#include "Keymaster.h" #include "Keymaster.h"
#include "Process.h" #include "Process.h"
#include "ScryptParameters.h" #include "ScryptParameters.h"
#include "Utils.h"
#include "VoldUtil.h" #include "VoldUtil.h"
#include "VolumeManager.h" #include "VolumeManager.h"
#include "secontext.h" #include "secontext.h"
@ -73,6 +74,8 @@ extern "C" {
#include <crypto_scrypt.h> #include <crypto_scrypt.h>
} }
using namespace std::chrono_literals;
#define UNUSED __attribute__((unused)) #define UNUSED __attribute__((unused))
#define DM_CRYPT_BUF_SIZE 4096 #define DM_CRYPT_BUF_SIZE 4096
@ -1099,6 +1102,12 @@ static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned
goto errout; goto errout;
} }
/* Ensure the dm device has been created before returning. */
if (android::vold::WaitForFile(crypto_blk_name, 1s) < 0) {
// WaitForFile generates a suitable log message
goto errout;
}
/* We made it here with no errors. Woot! */ /* We made it here with no errors. Woot! */
retval = 0; retval = 0;