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
Merged-In: Id8f571b551f50fc759e78d917e4ac3080e926722
gugelfrei
Paul Crowley 6 years ago
parent ddbd8325f5
commit cfe3972f2d

@ -19,6 +19,7 @@
#include "Process.h"
#include "sehandle.h"
#include <android-base/chrono_utils.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
@ -40,13 +41,16 @@
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <list>
#include <mutex>
#include <thread>
#ifndef UMOUNT_NOFOLLOW
#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
#endif
using namespace std::chrono_literals;
using android::base::ReadFileToString;
using android::base::StringPrintf;
@ -788,5 +792,20 @@ status_t UnmountTree(const std::string& prefix) {
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 android

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

@ -30,6 +30,7 @@
#include "Keymaster.h"
#include "Process.h"
#include "ScryptParameters.h"
#include "Utils.h"
#include "VoldUtil.h"
#include "VolumeManager.h"
#include "secontext.h"
@ -73,6 +74,8 @@ extern "C" {
#include <crypto_scrypt.h>
}
using namespace std::chrono_literals;
#define UNUSED __attribute__((unused))
#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;
}
/* 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! */
retval = 0;

Loading…
Cancel
Save