Skip to content

Conversation

@jingfei195887
Copy link
Contributor

@jingfei195887 jingfei195887 commented Jun 27, 2025

Summary

  1. The registration method for MTD devices in nuttx/boards have been adjusted , replacing the previous approach using ftl_initialize() andbchdev_register()with register_mtddriver().When registering MTD devices via register_mtddriver(), FTL and BCH wrappers will be added during the open() process:

    • When accessing the MTD device node via the open() interface, the device will be automatically converted to a character device. Both FTL and BCH wrappers will be implicitly added, provided that BCH support is enabled in the configuration.

    • When accessing the MTD device node via open_blockdriver(), the device will be treated as a block device, with only the FTL wrapper automatically applied.

    Due to the automatic wrapping of MTD devices during the open() process, the legacy registration methods
    ftl_initialize() and bchdev_register() are no longer required for MTD device registration for user code and should
    be used only internally within fs and driver code.

    The functions register_partition_with_mtd() and register_mtdpartition() are actually wrappers built on top of register_mtddriver(), and they can be used to create sub-partition devices for MTD devices.

  2. To save more space (equivalent to the size of one erase sector of MTD device) and to achieve faster read and write speeds, a method for direct writing was introduced at the FTL layer. This can be accomplished simply by using the following oflags during the open operation:

    • O_DIRECT. when this flag is passed in, ftl internally uses the direct write strategy and no read cache is used in ftl;
      otherwise, each write will be executed with the minimum granularity of flash erase sector size which means a
      "sector read back - erase sector - write sector" operation is performed by using a read cache buffer in heap.

    • O_SYNC. When this flag is passed in, we assume that the flash has been erased in advance and no erase operation
      will be performed internally within ftl. O_SYNC will take effect only when both O_DIRECT and O_SYNC are passed in
      simultaneously.

  3. For uniformity, we remapped the mount flag in mount.h and unified it with the open flag in fcntl.h. The repetitive
    parts of their definitions were reused, and the remaining part of the mount flag redefine to the unused bit of open
    flags.**

Impact

  1. The ftl_initialize() and bchdev_register() methods, originally used to wrap MTD device inodes, are no longer recommended for creating MTD device nodes. Instead, developers are advised to use the register_mtddriver(), register_partition_with_mtd(), or register_mtdpartition() functions. If these older methods were previously used in your code, please update your implementation to follow the new recommendations.

  2. When an application wants to bypass the following logic in the FTL layer during the process of writing to Flash: using the sector size as the buffer for "read-back - erase - write", the MTD device node can be opened with the O_DIRECT flag. At this point, the FTL will directly write to the MTD device. The open method is as follows:
    fd = open("/dev/mtd_device", O_RDWR | O_DIRECT);

  3. If the application ensures that the flash has been erased in advance, then adding the O_SYNC logic can control the FTL not to perform erasure. The open method is as follows:
    fd = open("/dev/mtd_device", O_RDWR | O_DIRECT | O_SYNC);

@github-actions github-actions bot added Area: Drivers Drivers issues Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. labels Jun 27, 2025
@jingfei195887 jingfei195887 marked this pull request as draft June 27, 2025 11:30
@xiaoxiang781216 xiaoxiang781216 marked this pull request as ready for review June 27, 2025 12:23
@xiaoxiang781216 xiaoxiang781216 marked this pull request as draft June 27, 2025 12:24
@xiaoxiang781216
Copy link
Contributor

please remove the merge patch, @jingfei195887

@jingfei195887 jingfei195887 reopened this Jun 29, 2025
@jingfei195887 jingfei195887 deleted the mtd_optimize branch June 29, 2025 05:35
@jingfei195887 jingfei195887 restored the mtd_optimize branch June 29, 2025 05:38
@jingfei195887 jingfei195887 reopened this Jun 29, 2025
@github-actions github-actions bot added the Area: File System File System issues label Jun 29, 2025
@jingfei195887 jingfei195887 force-pushed the mtd_optimize branch 2 times, most recently from 92fd5a1 to 83d4a23 Compare June 30, 2025 15:12
@github-actions github-actions bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Jul 7, 2025
@jingfei195887 jingfei195887 changed the title [BCH] Expose more public functions of the BCH library. [FEATURE] Add a direct writing method for FTL Jul 7, 2025
@jingfei195887 jingfei195887 marked this pull request as ready for review July 7, 2025 04:56
xiaoxiang781216
xiaoxiang781216 previously approved these changes Jul 7, 2025
jerpelea
jerpelea previously approved these changes Jul 7, 2025
Copy link
Contributor

@jerpelea jerpelea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please change commit title to
drivers/bch: Add direct writing method for FTL

thanks for adding the commit message

@jerpelea jerpelea changed the title [FEATURE] Add a direct writing method for FTL drivers/bch: Add a direct writing method for FTL Jul 7, 2025
Copy link
Contributor

@michallenc michallenc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with example application in #16624, nxboot update and custom logging library.

@jingfei195887 jingfei195887 changed the title drivers/bch: Add a direct writing method for FTL drivers/bch: Add direct writing method for FTL Jul 7, 2025
cederom
cederom previously requested changes Jul 7, 2025
Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jingfei195887 looks interesting :-)

  • @michallenc is out expert in this area so I follow all his remarks :-)
  • What happens if we use SYNC flag assuming the memory is erased but it is not erased? Is this scenario handled properly with returning error code?
  • Please update commits topic prefix to bch: ... and fs/mtd: ....
  • Should we mark this change with [BREAKING] tag in the commit topic too? It changes filesystem / mount / mtd in a non-backward compatible way and this should be clearly marked if someone bumps the release and gets a broken code it will help find a fix quickly. Maybe it would be possible to create new function calls with additional flags as new functionality while old API remains untouched (could call new functions with default parameters)? What other folks think? :-)
  • Should we also update the documentation on the new behavior (i.e. https://nuttx.apache.org/docs/latest/components/drivers/special/mtd.html)?

We have adjusted the registration method for MTD devices
in nuttx/boards, replacing the previous approach using
ftl_initialize() and bchdev_register() with
register_mtddriver().

When registering MTD devices via register_mtddriver(),
FTL and BCH wrappers will be added during the open() process:

1. Character Device Mode:
   When accessing the MTD device node via the open() interface,
   the device will be automatically converted to a character
   device. Both FTL and BCH wrappers will be implicitly added,
   provided that BCH support is enabled in the configuration.

2. Block Device Mode:
   When accessing the MTD device node via open_blockdriver(),
   the device will be treated as a block device, with only
   the FTL wrapper automatically applied.

Due to the automatic wrapping of MTD devices during the
open() process, the legacy registration methods
ftl_initialize() and bchdev_register() are no longer
required for MTD device registration for user code and should
be used only internally within fs and driver code.

Signed-off-by: jingfei <jingfei@xiaomi.com>
This reverts commit 20fcdcf.
The reason is that the erase buffer isn't always used in most cases.

Signed-off-by: jingfei <jingfei@xiaomi.com>
…open process.

To save more space (equivalent to the size of one erase sector of
MTD device) and to achieve faster read and write speeds, a method
for direct writing was introduced at the FTL layer.
This can be accomplished simply by using the following oflags during
the open operation:

1. O_DIRECT. when this flag is passed in, ftl internally uses
   the direct write strategy and no read cache is used in ftl;
   otherwise, each write will be executed with the minimum
   granularity of flash erase sector size which means a
   "sector read back - erase sector - write sector" operation
   is performed by using a read cache buffer in heap.

2. O_SYNC. When this flag is passed in, we assume that the
   flash has been erased in advance and no erasure operation
   will be performed internally within ftl. O_SYNC will take
   effect only when both O_DIRECT and O_SYNC are passed in
   simultaneously.

3. For uniformity, we remapped the mount flag in mount.h and
   unified it with the open flag in fcntl.h. The repetitive
   parts of their definitions were reused, and the remaining
   part of the mount flag redefine to the unused bit of open
   flags.

Signed-off-by: jingfei <jingfei@xiaomi.com>
…Control

Documentation:
1. Updates the registration process description and flag usage guidelines
2. Includes the MTD open sequence diagram (mtd_open_flow.png)
@TimJTi
Copy link
Contributor

TimJTi commented Jul 15, 2025

@michallenc @acassis @cederom @TimJTi this pr is ready for merging now, please review again.

@xiaoxiang781216 @jingfei195887 It's today's (additional!) task.

FYI - when I set up the test environment yesterday my existing code seemed to work OK without me yet having changed to the new methodology. So it's not a "breaking" change thankfully and I see the documentation recommends the new method, not demands it. Thank you :-)

@TimJTi
Copy link
Contributor

TimJTi commented Jul 15, 2025

this pr is ready for merging now, please review again.

@xiaoxiang781216 @jingfei195887 I have just tested this on my board, using NXboot to flash a recovery image from /dev/mtd0 to /dev/mtd2 and an "ota" update image from /dev/mtd1 to /dev/mtd0 having changed my startup code to directly register /dev/mtd%d instead of using FTL and BCH steps/

I can confirm it works for me.

If you want to invite me as a reviewer I'm happy to approve it: if not, I am equally happy to see it merged as soon as you're both done here.

I very much appreciate being kept in the loop and being given the opportunity to check it out before it just happened 👍 👍

@jingfei195887
Copy link
Contributor Author

this pr is ready for merging now, please review again.

@xiaoxiang781216 @jingfei195887 I have just tested this on my board, using NXboot to flash a recovery image from /dev/mtd0 to /dev/mtd2 and an "ota" update image from /dev/mtd1 to /dev/mtd0 having changed my startup code to directly register /dev/mtd%d instead of using FTL and BCH steps/

I can confirm it works for me.

If you want to invite me as a reviewer I'm happy to approve it: if not, I am equally happy to see it merged as soon as you're both done here.

I very much appreciate being kept in the loop and being given the opportunity to check it out before it just happened 👍 👍

@TimJTi Thank you very much for helping test this feature and really appreciate your work!

@jingfei195887
Copy link
Contributor Author

Hi @xiaoxiang781216 there is a CI build error in xtensa-01 for esp32-devkitc/nxlooper ,so I tried to do build in my own environment:

  1. ./tools/refresh.sh --silent esp32-devkitc/nxloop
  2. make

but another problem happened and I can't finish the build (even without my patch): maybe this is for a wrong version of xtensa gcc
image

does anybody konw why this is happening in xtensa01:
image

@xiaoxiang781216
Copy link
Contributor

xiaoxiang781216 commented Jul 15, 2025

@tmedicci @eren-terzioglu could you look at the above problem?

@xiaoxiang781216
Copy link
Contributor

let's ignore esp_tool ci error since it's unrelated to the change in this pr.

@xiaoxiang781216 xiaoxiang781216 dismissed stale reviews from acassis and cederom July 16, 2025 06:11

fixed

@xiaoxiang781216 xiaoxiang781216 merged commit 4a765b5 into apache:master Jul 16, 2025
66 of 76 checks passed
@jingfei195887 jingfei195887 deleted the mtd_optimize branch July 16, 2025 11:04
@fdcavalcanti
Copy link
Contributor

fdcavalcanti commented Jul 17, 2025

Hi @xiaoxiang781216 there is a CI build error in xtensa-01 for esp32-devkitc/nxlooper ,so I tried to do build in my own environment:

1. ./tools/refresh.sh --silent esp32-devkitc/nxloop

2. make

but another problem happened and I can't finish the build (even without my patch): maybe this is for a wrong version of xtensa gcc image

does anybody konw why this is happening in xtensa01: image

Hi all, seems we still have this issue. I have run a git bisect since this CI error is still happening.
Looks like it was indeed introduced by drivers/fs: Control the behavior of FTL by passing oflags during the open process..
I'll be taking a look into it, but if you guys have any idea let me know.

@xiaoxiang781216
Copy link
Contributor

it's strange why host esptool depend on the code change inside fs/mtd:(, @fdcavalcanti .

@michallenc
Copy link
Contributor

@xiaoxiang781216 @jingfei195887 Hi guys, I have now tested the patch in my environment and all seems to work as expected. Thanks for all the work and effort with fixing the issues!

And thanks to @TimJTi for the quick review with nxboot.

@jingfei195887
Copy link
Contributor Author

@xiaoxiang781216 @jingfei195887 Hi guys, I have now tested the patch in my environment and all seems to work as expected. Thanks for all the work and effort with fixing the issues!

And thanks to @TimJTi for the quick review with nxboot.

thankyou

@jingfei195887
Copy link
Contributor Author

Hi @xiaoxiang781216 there is a CI build error in xtensa-01 for esp32-devkitc/nxlooper ,so I tried to do build in my own environment:

1. ./tools/refresh.sh --silent esp32-devkitc/nxloop

2. make

but another problem happened and I can't finish the build (even without my patch): maybe this is for a wrong version of xtensa gcc image
does anybody konw why this is happening in xtensa01: image

Hi all, seems we still have this issue. I have run a git bisect since this CI error is still happening. Looks like it was indeed introduced by drivers/fs: Control the behavior of FTL by passing oflags during the open process.. I'll be taking a look into it, but if you guys have any idea let me know.

Thanks for the helping, any process please let me know~

@fdcavalcanti
Copy link
Contributor

Hi @xiaoxiang781216 there is a CI build error in xtensa-01 for esp32-devkitc/nxlooper ,so I tried to do build in my own environment:

1. ./tools/refresh.sh --silent esp32-devkitc/nxloop

2. make

but another problem happened and I can't finish the build (even without my patch): maybe this is for a wrong version of xtensa gcc image
does anybody konw why this is happening in xtensa01: image

Hi all, seems we still have this issue. I have run a git bisect since this CI error is still happening. Looks like it was indeed introduced by drivers/fs: Control the behavior of FTL by passing oflags during the open process.. I'll be taking a look into it, but if you guys have any idea let me know.

Thanks for the helping, any process please let me know~

So the issue is not related to this MR specifically. Its an alignment issue on ESP32 and it was a coincidence that happened on this PR. Upgrading to esptool v5.0.x fixes the problem and we should keep discussing on #16723.

xiaoxiang781216 added a commit to xiaoxiang781216/incubator-nuttx that referenced this pull request Jul 22, 2025
since ftl_initialize isn't used anymore after:
apache#16642
apache#16738

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
xiaoxiang781216 added a commit to xiaoxiang781216/incubator-nuttx that referenced this pull request Jul 29, 2025
since ftl_initialize isn't used anymore after:
apache#16642
apache#16738

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
xiaoxiang781216 added a commit to xiaoxiang781216/incubator-nuttx that referenced this pull request Jul 30, 2025
since ftl_initialize isn't used anymore after:
apache#16642
apache#16738

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Area: Drivers Drivers issues Area: File System File System issues Area: OS Components OS Components issues Board: arm Board: risc-v Board: xtensa Board: z80 breaking change This change requires a mitigation entry in the release notes. Size: L The size of the change in this PR is large Size: M The size of the change in this PR is medium Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants