[GUIDE] EXT4 to F2FS migration for performance improved file system [upd. 2017.01.24]

Search This thread

Top Liked Posts

  • There are no posts matching your filters.
  • 29
    F2FS is a brand new file system developed by Samsung. The F2FS acronym stands for Flash Friendly File System. It creates an additional layer that improves the performance and life of the flash memory and the overall performance of any system with flash storage. Almost all the Android phones today still come with the old linux ext4 filesystem. This type of file system is not hardware optimized so it is a baseline for performance. There are plenty of benchmarks and tests throughout the web providing evidence of the overall performance improvement of F2FS over ext4 in Android phones. New flagship devices from Huawei, One Plus and many Motorola phones are now using F2FS as stock filesystem. The flagship Moto Z comes with F2FS, sporting a fabulous 1/10 of lag compared to the Pixel XL. However mind that it is a new technology with barely 3 years of development and you might encounter some issues if you try different configurations. But, we are here testing unofficial things, so risk is a known partner. Just keep a safe backup, that is all you need.

    Migrating /system has no sense since it is a read only partition (most of the times) and ext4 is faster for pure readings. On the other hand /cache is formatted to ext4 at boot by the kernel. So patching the kernel is the only way to migrate that partition.

    Data partition along with the internal storage can be upgraded to F2FS with visible results. Here I share a method to migrate your system without losing any information. The procedure is reversible, so you can return back to the traditional ext4 in case you are not satisfied with the result. The procedure only requires TWRP recovery 3.0.3 and an external sdcard with enough free space to do at least a backup of the data partition. Since we are not modifying boot or system, this procedure is compatible with unrooted systems with locked bootloaders. It is compatible with all the AXON 7 ROMs.

    INSTALLATION

    1. Reboot to TWRP and perform a backup of your data partition to your external sd (you can't use internal storage since it will be wiped during the process along with the data partition).

    2. Backing up your Internal SD card requires a different way. Here I propose 2 options:
    2a. If you have an External SD card with enough free space (check that first), you can use it to temporary store the content of the internal sdcard. While in TWRP root menu, go to Advanced / File Manager and browse into the sdcard (You will see the contents of your internal storage). Tap on the checked folder icon on lower right corner. Select Copy Folder. Now browse into the external_sd folder (you will see the contents of yout external SD) and tap the checked folder icon again. Upon confirmation, it will create a folder named sdcard into the external sd with all the contents inside.
    2b. If you do not have a micro SD card with enough space then you can use your computer as backup location. From your computer type adb pull /sdcard and all the content of the internal sdcard will be transferred into a folder named sdcard under you current directory in your computer.

    3. Now we can safely change the format of the data filesystem. Go back to the root menu of TWRP, go to Wipe / Advanced Wipe and select only the Data partition. Go to Repair or Change File System / Change File System / F2FS. Upon confirmation, the Data and internal sdcard will use the improved F2FS filesystem. If you have an AOSP based ROM such as LOS, RR or AEX among others, then you can do the same to format the Cache partition as F2FS. But do not reboot yet!

    4. Go back to the root menu of TWRP and go to Settings. Ensure "Use rm -rf instead of formatting" is set. This will avoid future restores to change the type of filesystem.

    5. We can't restore the data at this time since the system won't boot properly with all kernels, the Stock kernel is one of them. At this point it is safer to reboot the system and wait until the Android welcome wizard shows up on the screen. This will setup everything internally.

    6. Now reboot to TWRP, go to Advanced > Terminal and enter the commands (you can bypass these commands only if you are using TWRP 3.0.4 or above):
    Code:
    mount -o remount,discard,nosuid,nodev,noatime,nodiratime,nobarrier,inline_xattr,inline_data /data
    mount -o remount,discard,nosuid,nodev,noatime,nodiratime,nobarrier,inline_xattr,inline_data /sdcard
    and then without leaving TWRP restore the backup you created in step 1. This will restore your data partition but this time applying an optimized F2FS structure. Do not leave TWRP yet, there is one more step left before booting into the system.

    7. depending on the option you used in step 2 you have to follow one of the following options to restore your internal sdcard:
    7a. If you used your External SD card for the internal sdcard backup then while in TWRP root menu, go to Advanced / File Manager and browse into the external_sd/sdcard folder. Tap on the checked folder icon on lower right corner. Select Copy Folder. Now while the root directory is listed tap the checked folder icon again. Upon confirmation, it will restore all the backup into the external sdcard back into the Axon 7 internal memory.
    7b. From your computer type the following commands:
    Code:
    adb push sdcard /
    adb shell chmod -R 777 /sdcard
    This will restore all the backup of the internal sdcard back into the Axon 7 internal memory. All but the empty directories.

    You are done!!!! just reboot into the system with the new state of the art F2FS filesystem.

    Sometimes the lockscreen information gets corrupted after restoring a backup. Since this process relies on restoring a TWRP backup, chances are that you are experiencing this issue too. In such case please follow the guide for clearing the lockscreen security settings in the AXON 7.

    UNINSTALL

    In case you want to go back to the initial state with ext4 filesystem in the Data and internal storage just follow these steps:

    1. Reboot to TWRP and go to Settings. Ensure "Use rm -rf instead of formatting" is NOT set. This will allow to restore the data partition to the original filesystem of the backup.

    2. Follow steps 6 and 7 of the installation instructions, exactly the same way, to restore the Data and internal storage to ext4 with the same contents.


    I hope you find this interesting since it is a performance improving method that drains less battery, doesn't involve rooting, doesn't overheat the processors and provides noticeable results. Just let me know if this guide has been useful for you.

    NOTE: If you delete Dlavik/cache in the process, please note that rebuilding it is quite fast, less than 5 seconds. This is caused by the cache of the F2FS filesystem, but all processing has to be done. The system will boot after a quick apps initialization but you will see the system somehow a little slow. Leave it for some minutes until all the apps had been completely processed. It is done in the background, but al least you have your system up and running quite fast even after deleting dalvik/cache partitions.

    UPDATE (2017-01-20)

    If you have already switched to F2FS filesystem without the optimization parameters for TWRP (using TWRP 3.0.3-2 or below with the initial version of this guide), then you just have to enter in TWRP and follow all the steps without leaving recovery:

    STEP 1: reboot to recovery and go to advanced / terminal. Type the command df. This will give you the current usage of the partitions. Write down the total 1K blocks, used and available statistics for the /dev/block/dm-0, the partition shared by data and sdcard. You will use these statistics to compare before and after.
    STEP 2: Backup of your system (Boot, system and Data) to external SD.
    STEP 3: Backup your internal storage (either way of those detailed in the guide)
    STEP 4: Format Data and internal storage.
    STEP 5: If you are using TWRP 3.0.4 or above then you can bypass this step. Otherwise open the terminal under advanced menu and type
    Code:
    mount -o remount,discard,nosuid,nodev,noatime,nodiratime,nobarrier,inline_xattr,inline_data /data
    mount -o remount,discard,nosuid,nodev,noatime,nodiratime,nobarrier,inline_xattr,inline_data /sdcard
    STEP 6: Restore your Data partition.
    STEP 7: Restore your internal storage.
    STEP 8: Go again to the terminal inside TWRP and type df again to see the difference in the data and sdcard shared partition.

    Please let us know your statistics before and after.

    Soon this guide will include the final F2FS optimization patch to be used within your system, it will require root, but is that a problem? On the other hand @jcadduono is working on a new version of TWRP using the backported f2fs drivers from Android kernel 4.10 (our stock kernels still use Android 3.18) with the correct optimized mount options. It is still under development but as soon as it was finished, I will update this guide on how to use the new features. Stay tuned!


    That's all folks!

    :)
    4
    I do not find those files inside the patch.d folder inside the zip file (the minimal kernel patch). I will take the file from the full kernel and I will insert into the minimal patch.

    I would like to enable F2FS for the cache partition as well as using the mount options: rw,seclabel,nosuid,nodev,noatime,nodiratime,background_gc=on,discard,user_xattr,inline_xattr,acl,inline_data,nobarrier,extent_cache,active_logs=6 for the data partition. It should increase the performance over the current options set. System partition has no reason to be migrated.
    I will modify fs_data_opts string definition in the 06-f2fs-fstab file as well as trying to configure cache as a F2FS. If Motorola is using F2FS with successful results, then probably it worths it.

    https://raw.githubusercontent.com/o...er/marshmallow/ailsa_ii/patch.d/06-f2fs-fstab

    add it to the patch.d folder then. change the data mount flags to whatever you want.

    i highly advise against uncommenting system/cache, f2fs is nothing but downsides for them. (incompatibilities for cache, which is unused by OS anyways), system is going to be read only all the time and ext4 has much better inode access/index times than f2fs so clearly better for read only, ex. $PATH reading to find locations of binaries to run, so please don't use f2fs on /system.

    rw - lol all filesystems are rw by default
    seclabel - this isn't a flag for you to use
    nosuid/nodev - somewhat of a safety mechanism, unrelated to performance
    noatime - very tiny write performance increase that won't be noticeable
    nodiratime - same as noatime, but even less so
    discard - i don't believe discard mount option is supported on Axon 7.
    acl - acl will reduce performance (that's just extra access control/permission management)
    nobarrier - this will increase the performance, yes, and it's probably a good idea because you don't have to worry at all about power outages other than severe kernel panics
    extent_cache - this is on by default, so no need to specify it
    inline_data - this is also on by default, no need to specify it
    inline_xattr - no noticeable changes in performance, but normally specified on android i'd keep it
    active_logs=6 - the default is 6, so no need to specify it, and there's no need to change this value (6 is optimal)
    background_gc=on - the default is on, so need to specify it....
    user_xattr - this isn't actually a valid f2fs flag, it is the default though, and don't specify nouser_xattr on android, it will cause bad things to happen

    so....nobarrier is the only flag you should care about adding. no, it won't make a noticeable difference.

    read: https://www.kernel.org/doc/Documentation/filesystems/f2fs.txt
    3
    Switching to most F2FS doesn't make sense for anything other /cache (which is rarely even used, so probably not worth it) and /data (probably very worth it). The benefits that F2FS related to writing to the partition a lot over time.

    There is no benefit to using F2FS for /system over ext4, in fact there are probably downsides. You aren't generally writing to /system very often, so no need to worry about write speeds to it. Read speeds are going to be similar anyway. And unless all the more recent F2FS development is backported into the kernel, you'd be running a very old version of F2FS as well.
    2
    How can we apply the optimizations in step 6 if we've already switched to f2fs? Also where can we find your modified version of jcadduono's kernel?
    The optimizations in Step 6 are used to create an optimized filesystem structure when restoring the data backup. TWRP doesn't have them as default so you just have to follow the steps and execute that command from the adb interface or the TWRP terminal under advanced menu. Otherwise you will begin with an not optimum structure.

    The "patched" patch for the kernel ailsa_ii_xda_safetynet_kcal-f2fs-1.2-20170104.zip includes the original stand alone kernel by jcadduono with bootloader unlock cloaking, no-verify patch, Qualcomm kcal color calibration support and my patch with the optimized options for F2FS mounts. You just have to flash the ZIP using TWRP or Flashfire app. This will make F2FS options permanent for the system by patching the fstab file.

    The other goodies comes from @jcadduono. This file is just the result of the conversation in this thread. So give also thanks to jcaduonno for his patch and for his help on using it to re-patch it for including the F2FS options.

    I would like to include inline_dentry option to store also small directories information within the inodes to save read/writes cycles (time, battery, etc) when dealing with small directories (most of them). However, the F2FS implementation of our current stock kernel is not based on the current linux code and this option is not available for us yet. I hope custom roms and new Nougat rom will come with updated F2FS code so we could enable this additional option to optimize it even further.
    2
    I am trying to optimize the filesystem even more. The next step is to provide specific parameters for the geometry with the fs geometry options in the mkfs.f2fs -s- z: these options control number of segments per section and number of sections per zone. Sizes that match chip parameters may speed up FLASH write operations. I assume this should have been taken care of, but I am just trying to find more optimization opportunities within the options.
    Does anyone know these two geometry parameters for our eMMC?
    @jcadduono Do you know what is the default geometry for mkfs.f2fs partitions in TWRP?

    On the options part, I am also considering inline_data and inline_dentry.

    Thanks