recovery: make wiping dynamic partitions work

Dynamic partitions need special handling:
 * Block device path read from fstab is actually
   the partition's name
 * We need to use BLKROSET ioctl for allowing
   write operations

Change-Id: Ib0a018f789716c9fc43db9316d15dbda13991c1e
diff --git a/install/wipe_data.cpp b/install/wipe_data.cpp
index 821a1e3..6b488a0 100644
--- a/install/wipe_data.cpp
+++ b/install/wipe_data.cpp
@@ -16,7 +16,10 @@
 
 #include "install/wipe_data.h"
 
+#include <fcntl.h>
+#include <linux/fs.h>
 #include <string.h>
+#include <sys/ioctl.h>
 
 #include <functional>
 #include <vector>
@@ -25,6 +28,7 @@
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <fs_mgr/roots.h>
+#include <libdm/dm.h>
 
 #include "install/snapshot_utils.h"
 #include "otautil/dirutil.h"
@@ -49,6 +53,40 @@
   ui->Print("Formatting %s...\n", volume);
 
   Volume* vol = volume_for_mount_point(volume);
+  if (vol->fs_mgr_flags.logical) {
+    android::dm::DeviceMapper& dm = android::dm::DeviceMapper::Instance();
+
+    map_logical_partitions();
+    // map_logical_partitions is non-blocking, so check for some limited time
+    // if it succeeded
+    for (int i = 0; i < 500; i++) {
+      if (vol->blk_device[0] == '/' ||
+          dm.GetState(vol->blk_device) == android::dm::DmDeviceState::ACTIVE)
+        break;
+      std::this_thread::sleep_for(std::chrono::milliseconds(1));
+    }
+
+    if (vol->blk_device[0] != '/' && !dm.GetDmDevicePathByName(vol->blk_device, &vol->blk_device)) {
+      PLOG(ERROR) << "Failed to find dm device path for " << vol->blk_device;
+      return false;
+    }
+
+    int fd = open(vol->blk_device.c_str(), O_RDWR);
+    if (fd < 0) {
+      PLOG(ERROR) << "Failed to open " << vol->blk_device;
+      return false;
+    }
+
+    int val = 0;
+    if (ioctl(fd, BLKROSET, &val) != 0) {
+      PLOG(ERROR) << "Failed to set " << vol->blk_device << " rw";
+      close(fd);
+      return false;
+    }
+
+    close(fd);
+  }
+
   if (ensure_volume_unmounted(vol->blk_device) == -1) {
     PLOG(ERROR) << "Failed to unmount volume!";
     return false;