Replace round_up macro with function.
authorMilan Broz <gmazyland@gmail.com>
Tue, 28 Aug 2012 11:11:02 +0000 (13:11 +0200)
committerMilan Broz <gmazyland@gmail.com>
Tue, 28 Aug 2012 11:11:02 +0000 (13:11 +0200)
lib/internal.h
lib/luks1/keyencryption.c
lib/luks1/keymanage.c
lib/utils_device.c

index 2d7b5db24443e8a8594ff45fbcc20d41dbecc350..1b16aa3d64388ef702e5de0926c8d90fce8ac643 100644 (file)
@@ -84,6 +84,7 @@ int device_block_adjust(struct crypt_device *cd,
                        uint64_t device_offset,
                        uint64_t *size,
                        uint32_t *flags);
+size_t size_round_up(size_t size, unsigned int block);
 
 /* Receive backend devices from context helpers */
 struct device *crypt_metadata_device(struct crypt_device *cd);
index 05bc38217f65466da658181c4a594b1e388e4040..9b3b9ae3c657d32096e8fb2fbc7d5e80f4da22cf 100644 (file)
 #include "luks.h"
 #include "internal.h"
 
-#define div_round_up(a,b) ({          \
-       typeof(a) __a = (a);          \
-       typeof(b) __b = (b);          \
-       (__a - 1) / __b + 1;          \
-})
-
-static inline int round_up_modulo(int x, int m) {
-       return div_round_up(x, m) * m;
-}
-
 static const char *cleaner_name=NULL;
 static uint64_t cleaner_size = 0;
 static int devfd=-1;
 
 static int setup_mapping(const char *cipher, const char *name,
-                        int bsize, struct volume_key *vk,
+                        unsigned int bsize, struct volume_key *vk,
                         unsigned int sector, size_t srcLength,
                         int mode, struct crypt_device *ctx)
 {
@@ -59,7 +49,7 @@ static int setup_mapping(const char *cipher, const char *name,
        struct crypt_dm_active_device dmd = {
                .target = DM_CRYPT,
                .uuid   = NULL,
-               .size   = round_up_modulo(srcLength, bsize) / SECTOR_SIZE,
+               .size   = size_round_up(srcLength, bsize) / SECTOR_SIZE,
                .flags  = CRYPT_ACTIVATE_PRIVATE,
                .data_device = device,
                .u.crypt = {
index f7fa4a26426b4f352eb10fb14e2e395057f8f382..bc346e7238224d9013558c825eb27cacf09b9255 100644 (file)
 #include "pbkdf.h"
 #include "internal.h"
 
-#define div_round_up(a,b) ({           \
-       typeof(a) __a = (a);          \
-       typeof(b) __b = (b);          \
-       (__a - 1) / __b + 1;        \
-})
-
-static inline int round_up_modulo(int x, int m) {
-       return div_round_up(x, m) * m;
-}
-
-/* Get size of struct luks_phrd with all keyslots material space */
-static uint64_t LUKS_device_sectors(size_t keyLen)
+/* Get size of struct luks_phdr with all keyslots material space */
+static size_t LUKS_device_sectors(size_t keyLen)
 {
-       uint64_t keyslot_sectors, sector;
+       size_t keyslot_sectors, sector;
        int i;
 
        keyslot_sectors = AF_split_sectors(keyLen, LUKS_STRIPES);
        sector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
 
        for (i = 0; i < LUKS_NUMKEYS; i++) {
-               sector = round_up_modulo(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
+               sector = size_round_up(sector, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
                sector += keyslot_sectors;
        }
 
@@ -621,10 +611,9 @@ int LUKS_generate_phdr(struct luks_phdr *header,
                       struct crypt_device *ctx)
 {
        unsigned int i=0;
-       unsigned int blocksPerStripeSet = AF_split_sectors(vk->keylength, stripes);
+       size_t blocksPerStripeSet, currentSector;
        int r;
        uuid_t partitionUuid;
-       int currentSector;
        char luksMagic[] = LUKS_MAGIC;
 
        /* For separate metadata device allow zero alignment */
@@ -685,11 +674,12 @@ int LUKS_generate_phdr(struct luks_phdr *header,
        }
 
        currentSector = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
+       blocksPerStripeSet = AF_split_sectors(vk->keylength, stripes);
        for(i = 0; i < LUKS_NUMKEYS; ++i) {
                header->keyblock[i].active = LUKS_KEY_DISABLED;
                header->keyblock[i].keyMaterialOffset = currentSector;
                header->keyblock[i].stripes = stripes;
-               currentSector = round_up_modulo(currentSector + blocksPerStripeSet,
+               currentSector = size_round_up(currentSector + blocksPerStripeSet,
                                                LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
        }
 
@@ -698,7 +688,7 @@ int LUKS_generate_phdr(struct luks_phdr *header,
                header->payloadOffset = alignPayload;
        } else {
                /* alignOffset - offset from natural device alignment provided by topology info */
-               currentSector = round_up_modulo(currentSector, alignPayload);
+               currentSector = size_round_up(currentSector, alignPayload);
                header->payloadOffset = currentSector + alignOffset;
        }
 
index 9e19a97e1278482eec0a6ebe49d4ce4eabe0bfe0..2e13721b1d1552e5b4c5ff73c0e59377ec6ed807 100644 (file)
@@ -421,3 +421,9 @@ int device_block_adjust(struct crypt_device *cd,
                *size, real_readonly ? "RO" : "RW", device_offset);
        return 0;
 }
+
+size_t size_round_up(size_t size, unsigned int block)
+{
+       size_t s = (size + (block - 1)) / block;
+       return s * block;
+}