vold2: Refactor the netlink event handling and better define how partitions/disks are handled

Signed-off-by: San Mehat <san@android.com>
diff --git a/DeviceVolume.cpp b/DeviceVolume.cpp
index 187e97f..d744180 100644
--- a/DeviceVolume.cpp
+++ b/DeviceVolume.cpp
@@ -15,12 +15,14 @@
  */
 
 #include <stdio.h>
-#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 
 #define LOG_TAG "Vold"
 
 #include <cutils/log.h>
+#include <sysutils/NetlinkEvent.h>
 
 #include "DeviceVolume.h"
 
@@ -44,30 +46,68 @@
     return 0;
 }
 
-int DeviceVolume::handleDiskInsertion(const char *dp, int maj, int min,
-                                      int nr_parts) {
-    PathCollection::iterator  it;
+int DeviceVolume::handleBlockEvent(NetlinkEvent *evt) {
+    const char *dp = evt->findParam("DEVPATH");
 
-    LOGD("Dv::diskInsertion - %s %d %d %d", dp, maj, min, nr_parts);
+    PathCollection::iterator  it;
     for (it = mPaths->begin(); it != mPaths->end(); ++it) {
-        LOGD("Dv::chk %s", *it);
         if (!strncmp(dp, *it, strlen(*it))) {
-            /*
-             * We can handle this disk. If there are no partitions then we're 
-             * good to go son!
-             */
-            mDiskMaj = maj;
-            mDiskNumParts = nr_parts;
-            if (nr_parts == 0) {
-                LOGD("Dv::diskIns - No partitions - good to go");
-                setState(Volume::State_Idle);
+            /* We can handle this disk */
+            int action = evt->getAction();
+            const char *devtype = evt->findParam("DEVTYPE");
+
+            if (!strcmp(devtype, "disk")) {
+                if (action == NetlinkEvent::NlActionAdd)
+                    handleDiskAdded(dp, evt);
+                else if (action == NetlinkEvent::NlActionRemove)
+                    handleDiskRemoved(dp, evt);
+                else
+                    LOGD("Ignoring non add/remove event");
             } else {
-                LOGD("Dv::diskIns - waiting for %d partitions", nr_parts);
-                setState(Volume::State_Pending);
+                if (action == NetlinkEvent::NlActionAdd)
+                    handlePartitionAdded(dp, evt);
+                else if (action == NetlinkEvent::NlActionRemove)
+                    handlePartitionRemoved(dp, evt);
+                else
+                    LOGD("Ignoring non add/remove event");
             }
+
             return 0;
         }
     }
     errno = ENODEV;
     return -1;
 }
+
+void DeviceVolume::handleDiskAdded(const char *devpath, NetlinkEvent *evt) {
+    mDiskMaj = atoi(evt->findParam("MAJOR"));
+    mDiskNumParts = atoi(evt->findParam("NPARTS"));
+
+    int partmask = 0;
+    int i;
+    for (i = 0; i < mDiskNumParts; i++) {
+        partmask |= (1 << i);
+    }
+    mPendingPartMap = partmask;
+
+    if (mDiskNumParts == 0) {
+        LOGD("Dv::diskIns - No partitions - good to go son!");
+        setState(Volume::State_Idle);
+    } else {
+        LOGD("Dv::diskIns - waiting for %d partitions (mask 0x%x)",
+             mDiskNumParts, mPendingPartMap);
+        setState(Volume::State_Pending);
+    }
+}
+
+void DeviceVolume::handlePartitionAdded(const char *devpath, NetlinkEvent *evt) {
+    int major = atoi(evt->findParam("MAJOR"));
+    int minor = atoi(evt->findParam("MINOR"));
+    int part_num = atoi(evt->findParam("PARTN"));
+}
+
+void DeviceVolume::handleDiskRemoved(const char *devpath, NetlinkEvent *evt) {
+}
+
+void DeviceVolume::handlePartitionRemoved(const char *devpath, NetlinkEvent *evt) {
+}
diff --git a/DeviceVolume.h b/DeviceVolume.h
index edf4c98..e535344 100644
--- a/DeviceVolume.h
+++ b/DeviceVolume.h
@@ -29,6 +29,7 @@
     int            mPartIdx;
     int            mDiskMaj;
     int            mDiskNumParts;
+    unsigned char  mPendingPartMap;
 
 public:
     DeviceVolume(const char *label, const char *mount_point, int partIdx);
@@ -36,7 +37,13 @@
 
     int addPath(const char *path);
 
-    int handleDiskInsertion(const char *dp, int maj, int min, int nr_parts);
+    int handleBlockEvent(NetlinkEvent *evt);
+
+private:
+    void handleDiskAdded(const char *devpath, NetlinkEvent *evt);
+    void handleDiskRemoved(const char *devpath, NetlinkEvent *evt);
+    void handlePartitionAdded(const char *devpath, NetlinkEvent *evt);
+    void handlePartitionRemoved(const char *devpath, NetlinkEvent *evt);
 };
 
 typedef android::List<DeviceVolume *> DeviceVolumeCollection;
diff --git a/NetlinkHandler.cpp b/NetlinkHandler.cpp
index 156079f..19ec932 100644
--- a/NetlinkHandler.cpp
+++ b/NetlinkHandler.cpp
@@ -44,7 +44,6 @@
 void NetlinkHandler::onEvent(NetlinkEvent *evt) {
     VolumeManager *vm = VolumeManager::Instance();
     const char *subsys = evt->getSubsystem();
-    int action = evt->getAction();
 
     if (!subsys) {
         LOGW("No subsystem found in netlink event");
@@ -52,37 +51,7 @@
     }
 
     if (!strcmp(subsys, "block")) {
-        const char *devpath = evt->findParam("DEVPATH");
-        const char *devtype = evt->findParam("DEVTYPE");
-        int major = atoi(evt->findParam("MAJOR"));
-        int minor = atoi(evt->findParam("MINOR"));
-
-        LOGI("Block event %d, type %s, %d:%d, path '%s'", action, devtype, major, minor, devpath);
-
-        if (!strcmp(devtype, "disk")) {
-            const char *tmp = evt->findParam("NPARTS");
-
-            if (!tmp) {
-                LOGE("Disk uevent missing 'NPARTS' parameter");
-                return;
-            }
-            if (action == NetlinkEvent::NlActionAdd)
-                vm->handleDiskInserted(devpath, major, minor, atoi(tmp));
-            else if (action == NetlinkEvent::NlActionRemove)
-                vm->handleDiskRemoved(major, minor);
-        } else {
-            const char *tmp = evt->findParam("PARTN");
-
-            if (!tmp) {
-                LOGE("Partition uevent missing 'PARTN' parameter");
-                return;
-            }
-            if (action == NetlinkEvent::NlActionAdd)
-                vm->handlePartCreated(devpath, major, minor, atoi(tmp));
-            else if (action == NetlinkEvent::NlActionRemove)
-                vm->handlePartRemoved(major, minor);
-        }
-        LOGD("Block event handled");
+        vm->handleBlockEvent(evt);
     } else if (!strcmp(subsys, "battery")) {
     } else if (!strcmp(subsys, "power_supply")) {
     } else {
diff --git a/Volume.cpp b/Volume.cpp
index aaccb2b..52d02cf 100644
--- a/Volume.cpp
+++ b/Volume.cpp
@@ -35,7 +35,7 @@
     free(mMountpoint);
 }
 
-int Volume::handleDiskInsertion(const char *dp, int maj, int min, int nr_parts) {
+int Volume::handleBlockEvent(NetlinkEvent *evt) {
     errno = ENOSYS;
     return -1;
 }
diff --git a/Volume.h b/Volume.h
index bf2a799..f428e91 100644
--- a/Volume.h
+++ b/Volume.h
@@ -19,6 +19,8 @@
 
 #include <utils/List.h>
 
+class NetlinkEvent;
+
 class Volume {
 private:
     int mState;
@@ -43,7 +45,7 @@
     const char *getMountpoint() { return mMountpoint; }
     int getState() { return mState; }
 
-    virtual int handleDiskInsertion(const char *dp, int maj, int min, int nr_parts);
+    virtual int handleBlockEvent(NetlinkEvent *evt);
 
 protected:
     void setState(int state);
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index ffe4e22..a383651 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -15,12 +15,16 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <errno.h>
 
 #define LOG_TAG "Vold"
 
 #include <cutils/log.h>
 
+#include <sysutils/NetlinkEvent.h>
+
 #include "VolumeManager.h"
 #include "DeviceVolume.h"
 #include "ErrorCode.h"
@@ -56,37 +60,24 @@
     return 0;
 }
 
-void VolumeManager::handleDiskInserted(const char *devpath, int maj, int min,
-                                       int nr_parts) {
+void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
+    const char *devpath = evt->findParam("DEVPATH");
 
-    /* Lookup possible candidate DeviceVolumes */
+    /* Lookup a volume to handle this device */
     VolumeCollection::iterator it;
     bool hit = false;
     for (it = mVolumes->begin(); it != mVolumes->end(); ++it) {
-        if (!(*it)->handleDiskInsertion(devpath, maj, min, nr_parts)) {
+        if (!(*it)->handleBlockEvent(evt)) {
             hit = true;
-            LOGD("Volume '%s' has handled disk insertion for '%s'",
-                 (*it)->getLabel(), devpath);
             break;
         }
     }
 
     if (!hit) {
-        LOGW("No volumes handled insertion of disk '%s'", devpath);
+        LOGW("No volumes handled block event for '%s'", devpath);
     }
 }
 
-void VolumeManager::handleDiskRemoved(int maj, int min) {
-}
-
-void VolumeManager::handlePartCreated(const char *devpath, int maj, int min,
-                                      int part_no) {
-}
-
-void VolumeManager::handlePartRemoved(int maj, int min) {
-}
-
-
 int VolumeManager::listVolumes(SocketClient *cli) {
     VolumeCollection::iterator i;
 
diff --git a/VolumeManager.h b/VolumeManager.h
index 8263805..c6beb2a 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -41,13 +41,7 @@
     int start();
     int stop();
 
-    void handleDiskInserted(const char *devpath, int maj, int min,
-                            int nr_parts);
-    void handleDiskRemoved(int maj, int min);
-
-    void handlePartCreated(const char *devpath, int maj, int min,
-                           int part_no);
-    void handlePartRemoved(int maj, int min);
+    void handleBlockEvent(NetlinkEvent *evt);
 
     int addVolume(Volume *v);