diff --git a/lib/utils_device.c b/lib/utils_device.c index 75449c09..04f9f89a 100644 --- a/lib/utils_device.c +++ b/lib/utils_device.c @@ -520,10 +520,16 @@ void device_topology_alignment(struct crypt_device *cd, temp_alignment = (unsigned long)min_io_size; - /* Ignore bogus opt-io that could break alignment */ + /* + * Ignore bogus opt-io that could break alignment. + * Also real opt_io_size should be aligned to minimal page size (4k). + * Some bogus USB enclosures reports wrong data here. + */ if ((temp_alignment < (unsigned long)opt_io_size) && - !((unsigned long)opt_io_size % temp_alignment)) + !((unsigned long)opt_io_size % temp_alignment) && !MISALIGNED_4K(opt_io_size)) temp_alignment = (unsigned long)opt_io_size; + else if (opt_io_size) + log_err(cd, "Ignoring bogus optimal-io size for data device (%u bytes).", opt_io_size); /* If calculated alignment is multiple of default, keep default */ if (temp_alignment && (default_alignment % temp_alignment)) diff --git a/tests/align-test2 b/tests/align-test2 index f1b387e0..75d9cf44 100755 --- a/tests/align-test2 +++ b/tests/align-test2 @@ -100,7 +100,7 @@ format() # expected [forced] [encryption_sector_size] if [ -z "$2" ] ; then echo -n "Formatting using topology info$_smsg..." - echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null || fail + echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --sector-size $_sec_size >/dev/null 2>&1 || fail else echo -n "Formatting using forced sector alignment $2$_smsg..." echo $PWD1 | $CRYPTSETUP luksFormat $FAST_PBKDF --type luks2 $DEV -q -c aes-cbc-essiv:sha256 --align-payload=$2 --sector-size $_sec_size >/dev/null || fail @@ -222,6 +222,31 @@ format $EXPCT $EXPCT s2048 format $EXPCT $EXPCT s4096 cleanup +echo "# Create drive with misaligned opt-io to page-size (some bad USB enclosures)" +echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0, opt-io=33553920)" +add_device dev_size_mb=32 sector_size=512 num_tgts=1 opt_blks=65535 +format $EXPCT +format $EXPCT s1024 +format $EXPCT s2048 +format $EXPCT s4096 +format $EXPCT 1 +format $EXPCT 1 s1024 +format $EXPCT 1 s2048 +format $EXPCT 1 s4096 +format $EXPCT 8 +format $EXPCT 8 s1024 +format $EXPCT 8 s2048 +format $EXPCT 8 s4096 +format $((EXPCT+1)) $((EXPCT+1)) +format_fail $((EXPCT+1)) $((EXPCT+1)) s1024 +format_fail $((EXPCT+1)) $((EXPCT+1)) s2048 +format_fail $((EXPCT+1)) $((EXPCT+1)) s4096 +format $EXPCT $EXPCT +format $EXPCT $EXPCT s1024 +format $EXPCT $EXPCT s2048 +format $EXPCT $EXPCT s4096 +cleanup + echo "# Create desktop-class 4K drive w/ 1-sector shift (original bug report)" echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=512)" add_device dev_size_mb=32 sector_size=512 physblk_exp=3 lowest_aligned=1 num_tgts=1