Use default data alignment to 1MiB.
authorMilan Broz <gmazyland@gmail.com>
Mon, 9 Aug 2010 16:34:42 +0000 (16:34 +0000)
committerMilan Broz <gmazyland@gmail.com>
Mon, 9 Aug 2010 16:34:42 +0000 (16:34 +0000)
If there is topology info, use default if topology is multiple of default,
otherwise use topology values.

See https://bugzilla.redhat.com/show_bug.cgi?id=621684 and issue 55.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@325 36d66b0a-2a48-0410-832c-cd162a569da5

ChangeLog
lib/internal.h
lib/setup.c
lib/utils.c
luks/keymanage.c
luks/luks.h
tests/align-test

index 2164c75..fce1cf5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
 2010-08-05  Milan Broz  <mbroz@redhat.com>
        * Wipe iteration and salt after KillSlot in LUKS header.
        * Rewrite file differ test to C (and fix it to really work).
+       * Switch to 1MiB default alignment of data.
+         For more info see https://bugzilla.redhat.com/show_bug.cgi?id=621684
 
 2010-07-28  Arno Wagner <arno@wagner.name>
        * Add FAQ (Frequently Asked Questions) file to distribution.
index 7b31cf8..cdfcef2 100644 (file)
@@ -14,7 +14,8 @@
 
 #define SECTOR_SHIFT           9
 #define SECTOR_SIZE            (1 << SECTOR_SHIFT)
-#define DEFAULT_ALIGNMENT      4096
+#define DEFAULT_DISK_ALIGNMENT 1048576 /* 1MiB */
+#define DEFAULT_MEM_ALIGNMENT  4096
 
 #define MAX_TTY_PASSWORD_LEN   512
 
index 720ea9b..06ee656 100644 (file)
@@ -1087,7 +1087,7 @@ static int _crypt_format_luks1(struct crypt_device *cd,
                               struct crypt_params_luks1 *params)
 {
        int r;
-       unsigned long required_alignment = DEFAULT_ALIGNMENT;
+       unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
        unsigned long alignment_offset = 0;
 
        if (!cd->device) {
@@ -1099,7 +1099,7 @@ static int _crypt_format_luks1(struct crypt_device *cd,
                required_alignment = params->data_alignment * SECTOR_SIZE;
        else
                get_topology_alignment(cd->device, &required_alignment,
-                                      &alignment_offset, DEFAULT_ALIGNMENT);
+                                      &alignment_offset, DEFAULT_DISK_ALIGNMENT);
 
        r = LUKS_generate_phdr(&cd->hdr, cd->volume_key, cipher, cipher_mode,
                               (params && params->hash) ? params->hash : "sha1",
index 1d1faa9..bbb423c 100644 (file)
@@ -125,12 +125,12 @@ char *safe_strdup(const char *s)
 
 static int get_alignment(int fd)
 {
-       int alignment = DEFAULT_ALIGNMENT;
+       int alignment = DEFAULT_MEM_ALIGNMENT;
 
 #ifdef _PC_REC_XFER_ALIGN
        alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
        if (alignment < 0)
-               alignment = DEFAULT_ALIGNMENT;
+               alignment = DEFAULT_MEM_ALIGNMENT;
 #endif
        return alignment;
 }
@@ -692,6 +692,7 @@ void get_topology_alignment(const char *device,
 {
        int dev_alignment_offset = 0;
        unsigned int min_io_size = 0, opt_io_size = 0;
+       unsigned long temp_alignment = 0;
        int fd;
 
        *required_alignment = default_alignment;
@@ -715,14 +716,16 @@ void get_topology_alignment(const char *device,
        /* alignment offset, bogus -1 means misaligned/unknown */
        if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
                dev_alignment_offset = 0;
+       *alignment_offset = (unsigned long)dev_alignment_offset;
 
-       if (*required_alignment < (unsigned long)min_io_size)
-               *required_alignment = (unsigned long)min_io_size;
+       temp_alignment = (unsigned long)min_io_size;
 
-       if (*required_alignment < (unsigned long)opt_io_size)
-               *required_alignment = (unsigned long)opt_io_size;
+       if (temp_alignment < (unsigned long)opt_io_size)
+               temp_alignment = (unsigned long)opt_io_size;
 
-       *alignment_offset = (unsigned long)dev_alignment_offset;
+       /* If calculated alignment is multiple of default, keep default */
+       if (temp_alignment && (default_alignment % temp_alignment))
+               *required_alignment = temp_alignment;
 
        log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
                min_io_size, opt_io_size, *alignment_offset, *required_alignment);
index 965b391..a7f61a4 100644 (file)
@@ -435,9 +435,9 @@ int LUKS_generate_phdr(struct luks_phdr *header,
        char luksMagic[] = LUKS_MAGIC;
        uuid_t partitionUuid;
        int currentSector;
-       int alignSectors = LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE;
+
        if (alignPayload == 0)
-               alignPayload = alignSectors;
+               alignPayload = DEFAULT_DISK_ALIGNMENT / SECTOR_SIZE;
 
        memset(header,0,sizeof(struct luks_phdr));
 
@@ -480,12 +480,13 @@ int LUKS_generate_phdr(struct luks_phdr *header,
                return r;
        }
 
-       currentSector = round_up_modulo(LUKS_PHDR_SIZE, alignSectors);
+       currentSector = round_up_modulo(LUKS_PHDR_SIZE, LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
        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, alignSectors);
+               currentSector = round_up_modulo(currentSector + blocksPerStripeSet,
+                                               LUKS_ALIGN_KEYSLOTS / SECTOR_SIZE);
        }
        currentSector = round_up_modulo(currentSector, alignPayload);
 
index e9e3909..3ed95c0 100644 (file)
@@ -36,7 +36,7 @@
 /* Actually we need only 37, but we don't want struct autoaligning to kick in */
 #define UUID_STRING_L 40
 
-/* Offset to align kesylot area */
+/* Offset to keyslot area [in bytes] */
 #define LUKS_ALIGN_KEYSLOTS 4096
 
 /* Any integer values are stored in network byte order on disk and must be
index 2fdb9b6..4524ff4 100755 (executable)
@@ -2,9 +2,11 @@
 
 CRYPTSETUP="../src/cryptsetup"
 DEV=""
+DEV_STACKED="luks0xbabe"
 
 cleanup() {
        udevadm settle 2>/dev/null 2>&1
+       [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove $DEV_STACKED 2>/dev/null 2>&1
        rmmod scsi_debug 2>/dev/null
        sleep 2
 }
@@ -41,10 +43,10 @@ format() # key_bits expected [forced]
 {
        if [ -z "$3" ] ; then
                echo -n "Formatting using topology info ($1 bits key)...."
-               echo xxx| $CRYPTSETUP luksFormat $DEV -q -s $1
+               echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -s $1
        else
-               echo -n "Formatting using forced offset $3 ($1 bits key)..."
-               echo xxx| $CRYPTSETUP luksFormat $DEV -q -s $1 --align-payload=$2
+               echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
+               echo xxx| $CRYPTSETUP luksFormat $DEV -q -i1 -s $1 --align-payload=$2
        fi
 
        ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
@@ -69,8 +71,10 @@ cleanup
 echo "# Create desktop-class 4K drive"
 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
-format 256 2112
-format 128 1088
+format 256 4096
+format 256 2112 8
+format 128 2048
+format 128 1088 8
 format 256 8192 8192
 format 128 8192 8192
 cleanup
@@ -78,25 +82,30 @@ cleanup
 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
-format 256 2119
-format 128 1095
+format 256 4103
+format 256 2119 8
+format 128 2055
+format 128 1095 8
 cleanup
 
 echo "# Create enterprise-class 4K drive"
 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
-format 256 2560
-format 128 1536 
+format 256 4096
+format 256 2560 8
+format 128 2048
+format 128 1536 8
 cleanup
 
 echo "# Create classic 512b drive and stack dm-linear"
 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
 add_device dev_size_mb=16 sector_size=512 num_tgts=1
 DEV2=$DEV
-DEV=/dev/mapper/luks0xbabe
-dmsetup create luks0xbabe --table "0 32768 linear $DEV2 0"
-format 256 2112
-format 128 1088
+DEV=/dev/mapper/$DEV_STACKED
+dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
+format 256 4096
+format 256 2112 8
+format 128 2048
+format 128 1088 8
 format 128 8192 8192
-dmsetup remove luks0xbabe
 cleanup