sdcard: only bind mount sdcard after successful preparation of data

Change-Id: I589f48a019f90a60ad48f93c5f5555aae4b8a5c9
diff --git a/partition.cpp b/partition.cpp
index 7e6fd55..1d68c24 100755
--- a/partition.cpp
+++ b/partition.cpp
@@ -658,8 +658,6 @@
 	UnMount(false);
 
 #ifdef TW_INCLUDE_CRYPTO
-	if (datamedia)
-		Setup_Data_Media();
 	Can_Be_Encrypted = true;
 	char crypto_blkdev[255];
 	property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
@@ -667,6 +665,8 @@
 		Set_FBE_Status();
 		Decrypted_Block_Device = crypto_blkdev;
 		LOGINFO("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
+		if (datamedia)
+			Setup_Data_Media();
 		DataManager::SetValue(TW_IS_ENCRYPTED, 0);
 	} else if (!Mount(false)) {
 		if (Is_Present) {
@@ -683,6 +683,8 @@
 					DataManager::SetValue("tw_crypto_pwtype_0", cryptfs_get_password_type());
 					DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
 					DataManager::SetValue("tw_crypto_display", "");
+					if (datamedia)
+						Setup_Data_Media();
 				} else {
 					gui_err("mount_data_footer=Could not mount /data and unable to find crypto footer.");
 				}
@@ -695,6 +697,8 @@
 			Primary_Block_Device.c_str(), Mount_Point.c_str());
 		}
 	} else {
+		if (Is_Mounted())
+			UnMount(true);
 		Set_FBE_Status();
 		int is_device_fbe;
 		DataManager::GetValue(TW_IS_FBE, is_device_fbe);
@@ -705,6 +709,9 @@
 				LOGERR("Unable to decrypt FBE device\n");
 		} else {
 			DataManager::SetValue(TW_IS_ENCRYPTED, 0);
+			if (datamedia)
+				Setup_Data_Media();
+
 		}
 	}
 	if (datamedia && (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))) {
@@ -1194,7 +1201,8 @@
 			Make_Dir("/sdcard", false);
 			Symlink_Mount_Point = "/sdcard";
 		}
-		if (Mount(false) && TWFunc::Path_Exists(Mount_Point + "/media/0")) {
+		Mount(false);
+		if (TWFunc::Path_Exists(Mount_Point + "/media/0")) {
 			Storage_Path = Mount_Point + "/media/0";
 			Symlink_Path = Storage_Path;
 			DataManager::SetValue(TW_INTERNAL_PATH, Mount_Point + "/media/0");
@@ -1605,7 +1613,7 @@
 	if (Removable)
 		Update_Size(Display_Error);
 
-	if (!Symlink_Mount_Point.empty() && Symlink_Mount_Point != "/sdcard") {
+	if (!Symlink_Mount_Point.empty()) {
 		if (!Bind_Mount(false))
 			return false;
 	}