vold: fix 64 bit ioctl error

Changing the num_sectors used in ioctl with BLKGETSIZE because
the kernel expects an unsigned long type and then changes 64 bits
with a 64 bits userspace. This overwrites what's located close to
the parameter location if any.

Change-Id: I78fd61a1084de2741f39b926aa436462518709a0
Signed-off-by: Mateusz Nowak <mateusz.nowak@intel.com>
Signed-off-by: Zhiquan Liu <zhiquan.liu@intel.com>
diff --git a/CommandListener.cpp b/CommandListener.cpp
index a9a8031..03f8c73 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -408,7 +408,7 @@
             return 0;
         }
 
-        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
+        unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
         const bool isExternal = (atoi(argv[7]) == 1);
         rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
     } else if (!strcmp(argv[1], "resize")) {
@@ -417,7 +417,7 @@
             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
             return 0;
         }
-        unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
+        unsigned long numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
         rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
     } else if (!strcmp(argv[1], "finalize")) {
         dumpArgs(argc, argv, -1);
diff --git a/Devmapper.cpp b/Devmapper.cpp
index 703eade..e56a4da 100644
--- a/Devmapper.cpp
+++ b/Devmapper.cpp
@@ -164,7 +164,7 @@
 }
 
 int Devmapper::create(const char *name, const char *loopFile, const char *key,
-                      unsigned int numSectors, char *ubuffer, size_t len) {
+                      unsigned long numSectors, char *ubuffer, size_t len) {
     char *buffer = (char *) malloc(DEVMAPPER_BUFFER_SIZE);
     if (!buffer) {
         SLOGE("Error allocating memory (%s)", strerror(errno));
diff --git a/Devmapper.h b/Devmapper.h
index 54f808f..5b65b53 100644
--- a/Devmapper.h
+++ b/Devmapper.h
@@ -25,7 +25,7 @@
 class Devmapper {
 public:
     static int create(const char *name, const char *loopFile, const char *key,
-                      unsigned int numSectors, char *buffer, size_t len);
+                      unsigned long numSectors, char *buffer, size_t len);
     static int destroy(const char *name);
     static int lookupActive(const char *name, char *buffer, size_t len);
     static int dumpState(SocketClient *c);
diff --git a/Loop.cpp b/Loop.cpp
index 059b963..a5863b3 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -253,7 +253,7 @@
     return -1;
 }
 
-int Loop::createImageFile(const char *file, unsigned int numSectors) {
+int Loop::createImageFile(const char *file, unsigned long numSectors) {
     int fd;
 
     if ((fd = creat(file, 0600)) < 0) {
@@ -270,7 +270,7 @@
     return 0;
 }
 
-int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
+int Loop::resizeImageFile(const char *file, unsigned long numSectors) {
     int fd;
 
     if ((fd = open(file, O_RDWR | O_CLOEXEC)) < 0) {
@@ -278,7 +278,7 @@
         return -1;
     }
 
-    SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);
+    SLOGD("Attempting to increase size of %s to %lu sectors.", file, numSectors);
 
     if (fallocate(fd, 0, 0, numSectors * 512)) {
         if (errno == ENOSYS || errno == ENOTSUP) {
diff --git a/Loop.h b/Loop.h
index f7c0392..72130b0 100644
--- a/Loop.h
+++ b/Loop.h
@@ -31,8 +31,8 @@
     static int create(const char *id, 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, unsigned int numSectors);
-    static int resizeImageFile(const char *file, unsigned int numSectors);
+    static int createImageFile(const char *file, unsigned long numSectors);
+    static int resizeImageFile(const char *file, unsigned long numSectors);
 
     static int dumpState(SocketClient *c);
 };
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index ce355df..46682dc 100755
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -115,21 +115,21 @@
     return 0;
 }
 
-static int adjustSectorNumExt4(unsigned numSectors) {
+static unsigned long adjustSectorNumExt4(unsigned long numSectors) {
     // Ext4 started to reserve 2% or 4096 clusters, whichever is smaller for
     // preventing costly operations or unexpected ENOSPC error.
     // Ext4::format() uses default block size without clustering.
-    unsigned clusterSectors = 4096 / 512;
-    unsigned reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
+    unsigned long clusterSectors = 4096 / 512;
+    unsigned long reservedSectors = (numSectors * 2)/100 + (numSectors % 50 > 0);
     numSectors += reservedSectors > (4096 * clusterSectors) ? (4096 * clusterSectors) : reservedSectors;
     return ROUND_UP_POWER_OF_2(numSectors, 3);
 }
 
-static int adjustSectorNumFAT(unsigned numSectors) {
+static unsigned long adjustSectorNumFAT(unsigned long numSectors) {
     /*
     * Add some headroom
     */
-    unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2;
+    unsigned long fatSize = (((numSectors * 4) / 512) + 1) * 2;
     numSectors += fatSize + 2;
     /*
     * FAT is aligned to 32 kb with 512b sectors.
@@ -154,7 +154,7 @@
     return 0;
 }
 
-static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , int numImgSectors, bool* createdDMDevice, bool debug) {
+static int setupDevMapperDevice(char* buffer, size_t len, const char* loopDevice, const char* asecFileName, const char* key, const char* idHash , unsigned long numImgSectors, bool* createdDMDevice, bool debug) {
     if (strcmp(key, "none")) {
         if (Devmapper::lookupActive(idHash, buffer, len)) {
             if (Devmapper::create(idHash, loopDevice, key, numImgSectors,
@@ -767,7 +767,7 @@
     return 0;
 }
 
-int VolumeManager::createAsec(const char *id, unsigned int numSectors, const char *fstype,
+int VolumeManager::createAsec(const char *id, unsigned long numSectors, const char *fstype,
         const char *key, const int ownerUid, bool isExternal) {
     struct asec_superblock sb;
     memset(&sb, 0, sizeof(sb));
@@ -795,7 +795,7 @@
     sb.ver = ASEC_SB_VER;
 
     if (numSectors < ((1024*1024)/512)) {
-        SLOGE("Invalid container size specified (%d sectors)", numSectors);
+        SLOGE("Invalid container size specified (%lu sectors)", numSectors);
         errno = EINVAL;
         return -1;
     }
@@ -824,7 +824,7 @@
         return -1;
     }
 
-    unsigned numImgSectors;
+    unsigned long numImgSectors;
     if (usingExt4)
         numImgSectors = adjustSectorNumExt4(numSectors);
     else
@@ -961,7 +961,7 @@
     return 0;
 }
 
-int VolumeManager::resizeAsec(const char *id, unsigned numSectors, const char *key) {
+int VolumeManager::resizeAsec(const char *id, unsigned long numSectors, const char *key) {
     char asecFileName[255];
     char mountPoint[255];
     bool cleanupDm = false;
@@ -991,7 +991,7 @@
 
     struct asec_superblock sb;
     int fd;
-    unsigned int oldNumSec = 0;
+    unsigned long oldNumSec = 0;
 
     if ((fd = open(asecFileName, O_RDONLY | O_CLOEXEC)) < 0) {
         SLOGE("Failed to open ASEC file (%s)", strerror(errno));
@@ -1007,7 +1007,7 @@
 
     oldNumSec = info.st_size / 512;
 
-    unsigned numImgSectors;
+    unsigned long numImgSectors;
     if (sb.c_opts & ASEC_SB_C_OPTS_EXT4)
         numImgSectors = adjustSectorNumExt4(numSectors);
     else
@@ -1015,7 +1015,7 @@
     /*
      *  add one block for the superblock
      */
-    SLOGD("Resizing from %d sectors to %d sectors", oldNumSec, numImgSectors + 1);
+    SLOGD("Resizing from %lu sectors to %lu sectors", oldNumSec, numImgSectors + 1);
     if (oldNumSec == numImgSectors + 1) {
         SLOGW("Size unchanged; ignoring resize request");
         return 0;
diff --git a/VolumeManager.h b/VolumeManager.h
index fa2237f..39fc8f9 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -143,9 +143,9 @@
     /* ASEC */
     int findAsec(const char *id, char *asecPath = NULL, size_t asecPathLen = 0,
             const char **directory = NULL) const;
-    int createAsec(const char *id, unsigned numSectors, const char *fstype,
+    int createAsec(const char *id, unsigned long numSectors, const char *fstype,
                    const char *key, const int ownerUid, bool isExternal);
-    int resizeAsec(const char *id, unsigned numSectors, const char *key);
+    int resizeAsec(const char *id, unsigned long numSectors, const char *key);
     int finalizeAsec(const char *id);
 
     /**
diff --git a/fs/Ext4.cpp b/fs/Ext4.cpp
index 3ae4159..4cb9d31 100644
--- a/fs/Ext4.cpp
+++ b/fs/Ext4.cpp
@@ -151,17 +151,17 @@
     return rc;
 }
 
-status_t Resize(const std::string& source, unsigned int numSectors) {
+status_t Resize(const std::string& source, unsigned long numSectors) {
     std::vector<std::string> cmd;
     cmd.push_back(kResizefsPath);
     cmd.push_back("-f");
     cmd.push_back(source);
-    cmd.push_back(StringPrintf("%u", numSectors));
+    cmd.push_back(StringPrintf("%lu", numSectors));
 
     return ForkExecvp(cmd);
 }
 
-status_t Format(const std::string& source, unsigned int numSectors,
+status_t Format(const std::string& source, unsigned long numSectors,
         const std::string& target) {
     std::vector<std::string> cmd;
     cmd.push_back(kMkfsPath);
@@ -172,7 +172,7 @@
 
     if (numSectors) {
         cmd.push_back("-l");
-        cmd.push_back(StringPrintf("%u", numSectors * 512));
+        cmd.push_back(StringPrintf("%lu", numSectors * 512));
     }
 
     // Always generate a real UUID
diff --git a/fs/Ext4.h b/fs/Ext4.h
index a5efa74..f78dc95 100644
--- a/fs/Ext4.h
+++ b/fs/Ext4.h
@@ -30,9 +30,9 @@
 status_t Check(const std::string& source, const std::string& target);
 status_t Mount(const std::string& source, const std::string& target, bool ro,
         bool remount, bool executable);
-status_t Format(const std::string& source, unsigned int numSectors,
+status_t Format(const std::string& source, unsigned long numSectors,
         const std::string& target);
-status_t Resize(const std::string& source, unsigned int numSectors);
+status_t Resize(const std::string& source, unsigned long numSectors);
 
 }  // namespace ext4
 }  // namespace vold
diff --git a/fs/Vfat.cpp b/fs/Vfat.cpp
index 7bd05ec..7338c1e 100644
--- a/fs/Vfat.cpp
+++ b/fs/Vfat.cpp
@@ -164,7 +164,7 @@
     return rc;
 }
 
-status_t Format(const std::string& source, unsigned int numSectors) {
+status_t Format(const std::string& source, unsigned long numSectors) {
     std::vector<std::string> cmd;
     cmd.push_back(kMkfsPath);
     cmd.push_back("-F");
@@ -177,7 +177,7 @@
 
     if (numSectors) {
         cmd.push_back("-s");
-        cmd.push_back(StringPrintf("%u", numSectors));
+        cmd.push_back(StringPrintf("%lu", numSectors));
     }
 
     cmd.push_back(source);
diff --git a/fs/Vfat.h b/fs/Vfat.h
index 306c7db..40be5f6 100644
--- a/fs/Vfat.h
+++ b/fs/Vfat.h
@@ -31,7 +31,7 @@
 status_t Mount(const std::string& source, const std::string& target, bool ro,
         bool remount, bool executable, int ownerUid, int ownerGid, int permMask,
         bool createLost);
-status_t Format(const std::string& source, unsigned int numSectors);
+status_t Format(const std::string& source, unsigned long numSectors);
 
 }  // namespace vfat
 }  // namespace vold