vold: Internally use sector counts for asec lengths

Signed-off-by: San Mehat <san@google.com>
diff --git a/CommandListener.cpp b/CommandListener.cpp
index db6899d..45cf1b7 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -182,6 +182,7 @@
         return 0;
     }
 
+    unsigned int numSectors = (atoi(argv[2]) * (1024 * 1024)) / 512;
     if (VolumeManager::Instance()->createAsec(argv[1], atoi(argv[2]),
                                               argv[3], argv[4],
                                               atoi(argv[5]))) {
diff --git a/Devmapper.cpp b/Devmapper.cpp
index 2b7d9a0..74ed4fb 100644
--- a/Devmapper.cpp
+++ b/Devmapper.cpp
@@ -60,8 +60,8 @@
  
     ioctlInit(io, 4096, name, 0);
     if (ioctl(fd, DM_DEV_STATUS, io)) {
-        if (errno != ENODEV) {
-            LOGE("Error retrieving device status (%s)", strerror(errno));
+        if (errno != ENXIO) {
+            LOGE("DM_DEV_STATUS ioctl failed for lookup (%s)", strerror(errno));
         }
         free(buffer);
         close(fd);
@@ -76,8 +76,8 @@
     return 0;
 }
 
-int Devmapper::create(const char *name, const char *loopFile, const char *key, int sizeMb,
-                      char *ubuffer, size_t len) {
+int Devmapper::create(const char *name, const char *loopFile, const char *key,
+                      unsigned int numSectors, char *ubuffer, size_t len) {
     char *buffer = (char *) malloc(4096);
     if (!buffer) {
         LOGE("Error allocating memory (%s)", strerror(errno));
@@ -121,7 +121,7 @@
     // Retrieve the device number we were allocated
     ioctlInit(io, 4096, name, 0);
     if (ioctl(fd, DM_DEV_STATUS, io)) {
-        LOGE("Error retrieving device status (%s)", strerror(errno));
+        LOGE("Error retrieving devmapper status (%s)", strerror(errno));
         free(buffer);
         close(fd);
         return -1;
@@ -139,7 +139,7 @@
     io->target_count = 1;
     tgt->status = 0;
     tgt->sector_start = 0;
-    tgt->length = (sizeMb * (1024 * 1024)) / 512;
+    tgt->length = numSectors;
     strcpy(tgt->target_type, "crypt");
 
     char *cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
diff --git a/Devmapper.h b/Devmapper.h
index d6cdd05..52e2bed 100644
--- a/Devmapper.h
+++ b/Devmapper.h
@@ -22,8 +22,8 @@
 
 class Devmapper {
 public:
-    static int create(const char *name, const char *loopFile, const char *key, int sizeMb,
-                      char *buffer, size_t len);
+    static int create(const char *name, const char *loopFile, const char *key,
+                      unsigned int numSectors, char *buffer, size_t len);
     static int destroy(const char *name);
     static int lookupActive(const char *name, char *buffer, size_t len);
 private:
diff --git a/Loop.cpp b/Loop.cpp
index b8106ef..a54d73e 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -181,17 +181,17 @@
     return -1;
 }
 
-int Loop::createImageFile(const char *file, size_t sizeMb) {
+int Loop::createImageFile(const char *file, unsigned int numSectors) {
     int fd;
 
-    LOGD("Creating ASEC image file %s (%d mb)", file, sizeMb);
+    LOGD("Creating ASEC image file %s (%u sectors)", file, numSectors);
 
     if ((fd = creat(file, 0600)) < 0) {
         LOGE("Error creating imagefile (%s)", strerror(errno));
         return -1;
     }
 
-    if (ftruncate(fd, 1024 + (sizeMb * (1024 * 1024))) < 0) {
+    if (ftruncate(fd, numSectors * 512) < 0) {
         LOGE("Error truncating imagefile (%s)", strerror(errno));
         close(fd);
         return -1;
diff --git a/Loop.h b/Loop.h
index da91dc2..f06f91d 100644
--- a/Loop.h
+++ b/Loop.h
@@ -28,7 +28,7 @@
     static int create(const char *loopFile, char *loopDeviceBuffer, size_t len);
     static int destroyByDevice(const char *loopDevice);
     static int destroyByFile(const char *loopFile);
-    static int createImageFile(const char *file, size_t sizeMb);
+    static int createImageFile(const char *file, unsigned int numSectors);
 };
 
 #endif
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 201ad98..9bdc980 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -171,7 +171,7 @@
     return 0;
 }
 
-int VolumeManager::createAsec(const char *id, int sizeMb,
+int VolumeManager::createAsec(const char *id, unsigned int numSectors,
                               const char *fstype, const char *key, int ownerUid) {
 
     mkdir("/sdcard/android_secure", 0777);
@@ -193,7 +193,7 @@
         return -1;
     }
 
-    if (Loop::createImageFile(asecFileName, sizeMb)) {
+    if (Loop::createImageFile(asecFileName, numSectors)) {
         LOGE("ASEC image file creation failed (%s)", strerror(errno));
         return -1;
     }
@@ -209,7 +209,7 @@
     bool cleanupDm = false;
 
     if (strcmp(key, "none")) {
-        if (Devmapper::create(id, loopDevice, key, sizeMb, dmDevice,
+        if (Devmapper::create(id, loopDevice, key, numSectors, dmDevice,
                              sizeof(dmDevice))) {
             LOGE("ASEC device mapping failed (%s)", strerror(errno));
             Loop::destroyByDevice(loopDevice);
@@ -397,8 +397,7 @@
                 return -1;
             }
             close(fd);
-            if (Devmapper::create(id, loopDevice, key,
-                                  (nr_sec * 512) / (1024 * 1024),
+            if (Devmapper::create(id, loopDevice, key, nr_sec,
                                   dmDevice, sizeof(dmDevice))) {
                 LOGE("ASEC device mapping failed (%s)", strerror(errno));
                 Loop::destroyByDevice(loopDevice);
diff --git a/VolumeManager.h b/VolumeManager.h
index 80b4ccf..15dddaf 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -55,7 +55,7 @@
     int shareAvailable(const char *method, bool *avail);
     int simulate(const char *cmd, const char *arg);
     int formatVolume(const char *label);
-    int createAsec(const char *id, int sizeMb, const char *fstype,
+    int createAsec(const char *id, unsigned numSectors, const char *fstype,
                    const char *key, int ownerUid);
     int finalizeAsec(const char *id);
     int destroyAsec(const char *id);