title: Android Device Encryption fails on CM-12.1 due to error in device-mapper
body: <p>I am currently trying to compile CM-12.1 (Android 5.1) for a Xiaomi Redmi Note 3 (MTK), but I am having issues with the device encryption (i.e., encrypting the /data partition).</p>
  
  <p>Specifically, when telling Android to encrypt the device (i.e., selecting "Encrypt phone" in the settings, it starts the process, but after less than one minute shows a screen saying that the encryption failed, and that the phone needs to be factory reset.</p>
  
  <p>The logcat shows the following output:</p>
  
  <pre><code>05-15 19:47:49.694 I/Cryptfs (  254): Using scrypt for cryptfs KDF
  05-15 19:47:50.680 I/Cryptfs (  254): Enabling support for allow_discards in dmcrypt.
  05-15 19:47:50.680 I/Cryptfs (  254): load_crypto_mapping_table: target_type = crypt
  05-15 19:47:50.680 I/Cryptfs (  254): load_crypto_mapping_table: real_blk_name = /dev/block/platform/mtk-msdc.0/by-name/userdata, extra_params = 1 allow_discards
  05-15 19:47:55.706 E/Cryptfs (  254): Cannot load dm-crypt mapping table.
  05-15 19:47:55.707 I/Cryptfs (  254): Encrypting ext4 filesystem in place...
  05-15 19:47:55.707 I/Cryptfs (  254): Encrypting group 0
  05-15 19:47:55.710 E/Cryptfs (  254): Error writing crypto_blkdev /dev/block/dm-0 for inplace encrypt
  05-15 19:47:55.710 I/Cryptfs (  254): Encrypted to sector 0
  05-15 19:47:55.710 E/Cryptfs (  254): Error encrypting groups
  05-15 19:47:55.710 D/Cryptfs (  254): cryptfs_enable_inplace_ext4()=-1
  05-15 19:47:55.710 E/Cryptfs (  254): Failed to encrypt f2fs filesystem on /dev/block/platform/mtk-msdc.0/by-name/userdata
  05-15 19:47:55.710 I/Cryptfs (  254): Encrypted to block -1
  05-15 19:47:55.710 D/Cryptfs (  254): cryptfs_enable_inplace_f2fs()=-1
  05-15 19:47:55.710 E/Cryptfs (  254): Encrypting filesystem in place...
  05-15 19:47:55.710 E/Cryptfs (  254): Cannot seek to previously encrypted point on /dev/block/dm-0
  05-15 19:47:55.710 D/Cryptfs (  254): cryptfs_enable_inplace_full()=-1
  </code></pre>
  
  <p>with the most pertinent line probably this one:</p>
  
  <pre><code>05-15 19:47:55.706 E/Cryptfs (  254): Cannot load dm-crypt mapping table.
  </code></pre>
  
  <p>At the same time, the kernel log prints the following:</p>
  
  <pre><code>&lt;3&gt;[ 1196.971648] (3)[324:vold]device-mapper: table: 253:0: crypt: Device lookup failed
  &lt;4&gt;[ 1196.971659] (3)[324:vold]device-mapper: ioctl: error adding target to table
  </code></pre>
  
  <p>I have traced this problem through the source code, and I suspect it is somewhere in dm-crypt.c of the Linux kernel, in the crypt_ctr() function:</p>
  
  <pre><code>if (dm_get_device(ti, argv[3], dm_table_get_mode(ti-&gt;table), &amp;cc-&gt;dev)) {
      ti-&gt;error = "Device lookup failed";
      goto bad;
  }
  </code></pre>
  
  <p>Presumably, dm_get_device() fails for some reason (of course, I might be mistaken and the problem is somewhere else, but tracing the function calls in the source code seems to suggest that this is where the error comes from).</p>
  
  <p>Unfortunately, the kernel sources for the device in question are not available, so I cannot perform any more detailed testing on the kernel.</p>
  
  <p>An additional piece of the puzzle is that there is an AOSP 5.0 ROM for the same device, which uses exactly the same kernel (as in, the same binary), and there the phone encryption works. Unfortunately, I am not familiar enough with dm-crypt, the device mapper and other related things around it to figure out what the difference is, and why it fails on CM-12.1 (e.g., whether there is for example any device mapper configuration somewhere that could be different, etc.).</p>
  
  <p>Even more strangely, if I encrypt the user partition on the AOSP ROM, then use TWRP recovery to flash my CM-12.1 ROM, wipe the data partition (which does not remove encryption, as TWRP can correctly decrypt and mount the data partition), then my CM-12.1 ROM boots normally and decrypts the data partition (presumably then <strong>not</strong> having a problem with the device-mapper).</p>
  
  <p>I have been banging my head against this problem for a bit too long, so if anybody has any ideas or knows that the problem could be and how to solve it, that would be very much appreciated.</p>