am 918e5f9f: Better error code for unmounting not-mounted volumes

Merge commit '918e5f9f10b9c1ff929683743ffbf229027ce240' into gingerbread-plus-aosp

* commit '918e5f9f10b9c1ff929683743ffbf229027ce240':
  Better error code for unmounting not-mounted volumes
diff --git a/DirectVolume.cpp b/DirectVolume.cpp
index ce5b892..0960724 100644
--- a/DirectVolume.cpp
+++ b/DirectVolume.cpp
@@ -197,9 +197,13 @@
 #ifdef PARTITION_DEBUG
     SLOGD("Dv:partAdd: part_num = %d, minor = %d\n", part_num, minor);
 #endif
-    mPartMinors[part_num -1] = minor;
-
+    if (part_num >= MAX_PARTITIONS) {
+        SLOGE("Dv:partAdd: ignoring part_num = %d (max: %d)\n", part_num, MAX_PARTITIONS-1);
+    } else {
+        mPartMinors[part_num -1] = minor;
+    }
     mPendingPartMap &= ~(1 << part_num);
+
     if (!mPendingPartMap) {
 #ifdef PARTITION_DEBUG
         SLOGD("Dv:partAdd: Got all partitions - ready to rock!");
@@ -270,6 +274,7 @@
     int major = atoi(evt->findParam("MAJOR"));
     int minor = atoi(evt->findParam("MINOR"));
     char msg[255];
+    int state;
 
     SLOGD("Volume %s %s partition %d:%d removed\n", getLabel(), getMountpoint(), major, minor);
 
@@ -279,7 +284,8 @@
      * the removal notification will be sent on the Disk
      * itself
      */
-    if (getState() != Volume::State_Mounted) {
+    state = getState();
+    if (state != Volume::State_Mounted && state != Volume::State_Shared) {
         return;
     }
         
@@ -304,6 +310,19 @@
         } else {
             SLOGD("Crisis averted");
         }
+    } else if (state == Volume::State_Shared) {
+        /* removed during mass storage */
+        snprintf(msg, sizeof(msg), "Volume %s bad removal (%d:%d)",
+                 getLabel(), major, minor);
+        mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeBadRemoval,
+                                             msg, false);
+
+        if (mVm->unshareVolume(getLabel(), "ums")) {
+            SLOGE("Failed to unshare volume on bad removal (%s)",
+                strerror(errno));
+        } else {
+            SLOGD("Crisis averted");
+        }
     }
 }