How to resize the /data F2FS partition Context:

I wiped all the partitions from my Moto G device with the TWRP recovery and installed Cyanogen 13 on it. Then I had issues when enabling Encryption: the phone just restarts but does not encrypt. This is a known issue and is described here.

I wanted to follow a procedure described in this answer (based on this), which reportedly works. The explanation is:


  When encrypting the phone, partition /dev/block/mmcblk0pXX -
  originally containing an ext4 filesystem which is mounted as /data and
  /sdcard - now contains an encryption container. This will be decrypted
  early in the boot process and returns a logical partition
  /dev/block/dm-0 which then holds the ext4 filesystem for /data and
  /sdcard. (...)
  
  To fix that, the filesystem in /dev/block/mmcblk0pXX needs to be at
  least 16KiB smaller than the partition itself, which can easily be
  done by resize2fs.



In short, the proposed solution is to resize the /data partition, removing some sectors from it...


  Important is the number of blocks (...) From this number, we subtract 8, which leaves not 16KiB but a safe 32 KiB of space for the encryption header (you don't mind 16KiB on a 12GB volume, seriously)


The actual number of blocks to be removed from the /data partition depends on the sector size for the partition. In the example above it's 4KB, so 8x4 =32.



The problem

These instructions only apply to the ext2/3/4 file systems; the necessary commands (e2fsck, tune2fs, resize2fs) won't work with the F2FS partition used by Cyanogen v13.

I found fsck.f2fs in the /sbin directory, which I used instead of e2fsck and tune2fs. Below are the steps from the root ADB Shell, booted to TWRP:



~ # mount | grep data

/dev/block/platform/msm_sdcc.1/by-name/modem on /firmware type ext4 (rw,seclabel,relatime,data=ordered)
/dev/block/mmcblk0p36 on /data type f2fs (rw,seclabel,relatime,background_gc=on,user_xattr,inline_xattr,acl,inline_data,active_logs=6)
/dev/block/mmcblk0p36 on /sdcard type f2fs (rw,seclabel,relatime,background_gc=on,user_xattr,inline_xattr,acl,inline_data,active_logs=6)
/dev/block/mmcblk0p33 on /cache type ext4 (rw,seclabel,relatime,data=ordered)

~ # umount /dev/block/mmcblk0p36

~ # fsck.f2fs -f /dev/block/mmcblk0p36

Info: Force to fix corruption
Info: sector size = 512
Info: total sectors = 11583232 (in 512 bytes)

(...)



So, my /data partition is mmcblk0p36, and is 11 583 232 sectors in size. Each sector is 512 bytes, thus I must shrink the partition to a final size of 11 583 168 sectors in order to leave 32KB unallocated.

But how can it be done? Everything seem to indicate that shrinking an F2FS partition is not possible, but I'm not sure. If it is not, and assuming I did a full backup of the /data partition, what are the steps to delete and re-create it with a smaller size?
As of today (May 30, 2016), the F2FS file system doesn't support shrinking. If it does by the time someone reads this, just let me know in the comments.

The solution is then to delete and re-create the entire filesystem for /data. From this related answer on Unix &amp; Linux Stack Exchange:

mkfs.f2fs /dev/block/mmcblk0p36 11583168


This will do the trick. After rebooting and restoring the backup, and finally, rebooting to Android, the encryption process worked.

Important: Be sure to check Use rm -rf instead of formatting from the TWRP settings prior to restoring the backup.