From 4882f7004045491f1bf949ecd369b7573f82c37d Mon Sep 17 00:00:00 2001 From: Milan Broz Date: Tue, 28 Aug 2012 13:11:02 +0200 Subject: [PATCH] Replace round_up macro with function. --- lib/internal.h | 1 + lib/luks1/keyencryption.c | 14 ++------------ lib/luks1/keymanage.c | 26 ++++++++------------------ lib/utils_device.c | 6 ++++++ 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/lib/internal.h b/lib/internal.h index 2d7b5db..1b16aa3 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -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); diff --git a/lib/luks1/keyencryption.c b/lib/luks1/keyencryption.c index 05bc382..9b3b9ae 100644 --- a/lib/luks1/keyencryption.c +++ b/lib/luks1/keyencryption.c @@ -36,22 +36,12 @@ #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 = { diff --git a/lib/luks1/keymanage.c b/lib/luks1/keymanage.c index f7fa4a2..bc346e7 100644 --- a/lib/luks1/keymanage.c +++ b/lib/luks1/keymanage.c @@ -36,27 +36,17 @@ #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; } diff --git a/lib/utils_device.c b/lib/utils_device.c index 9e19a97..2e13721 100644 --- a/lib/utils_device.c +++ b/lib/utils_device.c @@ -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; +} -- 2.7.4