Skip to content

drivers/bch: Report BCH capacity in pathname stat() - #22754

Open
OgnjenX wants to merge 2 commits into
apache:masterfrom
OgnjenX:fix/bch-stat-capacity
Open

OgnjenX wants to merge 2 commits into
apache:masterfrom
OgnjenX:fix/bch-stat-capacity

Conversation

@OgnjenX

@OgnjenX OgnjenX commented Dec 2, 2026 •

Copy link
Copy Markdown

Summary

Refs #16074. Pathname stat() succeeds on a BCH character device but
currently reports st_size = 0 because the node registers without a size.
Use the block geometry BCH already holds to register its exposed byte
length when it fits in off_t.

Impact

The BCH node remains a character device with the same permissions.
Unrepresentable lengths continue to report zero. This change does not
make pathname stat() identify the backing MTD device. Documentation
describes the distinction and points to MTDIOC_GEOMETRY.

Testing

Kubuntu host, NuttX simulator, RAM-backed MTD partition with FTL and BCH:

Before: BCH stat mode=020600 size=0
After:  BCH stat mode=020600 size=65536

Passed with and without large-file support. The 64 KiB capacity matched
fstat() and SEEK_END, and the control devices were unchanged.
ELF32 i386 simulator: ten synthetic geometry cases passed with 32-bit
size_t and both 32-bit and 64-bit off_t, including the boundaries
at 2 GiB and 4 GiB. An unrepresentable size reported zero without
preventing registration. tools/checkpatch.sh passed.

ESP32-DevKitC-32UE hardware (ESP32-D0WD-V3 revision 3.1)

Built both images on Kubuntu using the same Xtensa toolchain and
esp32-devkitc:nsh WROOM32 configuration with BCH, FTL and RAM-backed
MTD enabled. Both images booted to NSH after esptool verified their
writes. Ran bchstat against a 64 KiB RAM-backed MTD partition on the
board, not real flash media:

Before: BCH stat mode=020600 size=0
        BCH fstat mode=110666 size=65536
        FAIL BCH pathname capacity
After:  BCH stat mode=020600 size=65536
        BCH fstat mode=110666 size=65536
        PASS BCH pathname capacity and control devices

SEEK_END and MTDIOC_GEOMETRY returned 65536 in both builds.

@OgnjenX OgnjenX changed the title [WIP] drivers/bch: Report BCH capacity in pathname stat() drivers/bch: Report BCH capacity in pathname stat() Dec 2, 2026
@OgnjenX
OgnjenX marked this pull request as ready for review December 2, 2026 16:54
@xiaoxiang781216 xiaoxiang781216 linked an issue Dec 2, 2026 that may be closed by this pull request
1 task done
Comment thread drivers/bch/bchdev_register.c Resolved
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +44 B (+0.1%, 74,313 B / 262,144 B, total: 28% used)

esp32-devkitc

  • ROM: .flash.text +48 B (+0.0%, 138,052 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +48 B (+0.0%, 104,172 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +36 B (+0.0%, 88,436 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +36 B (+0.1%, 66,600 B / 131,072 B, total: 51% used)

mps2-an500-citest

  • flash: .text +48 B (+0.0%, 262,060 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +48 B (+0.0%, 126,004 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.file_fstat +28 B, .text.inode_stat +28 B, .text.register_driver_with_size +12 B (+0.0%, 398,140 B)
  • Data: .bss.g_root_inode_storage -8 B, .data.g_dir_inode -8 B, .data.g_epoll_inode -8 B (-0.0%, 78,629 B)

qemu-intel64

  • Code: .text +84 B (+0.0%, 8,789,889 B)

rx65n-rsk2mb

  • ROM: .text +64 B (+0.1%, 99,260 B / 2,097,152 B, total: 5% used)

s698pm-dkit

  • Code: .text +304 B (+0.1%, 429,296 B)

stm32-nucleo-f103rb

  • flash: .text +12 B (+0.0%, 35,512 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Dec 5, 2026
@OgnjenX
OgnjenX force-pushed the fix/bch-stat-capacity branch from 194c2b4 to 0324aea Compare December 5, 2026 13:39
{
case SEEK_CUR:
newpos = filep->f_pos + offset;
base = filep->f_pos;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why change

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I changed this to avoid signed overflow in f_pos + offset. SEEK_CUR now uses the same bounds check as SEEK_END before adding the offset.

}

ret = bchlib_read(bch, buffer, filep->f_pos, len);
ret = bch_limit_transfer(bch, filep->f_pos, &len);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bchlib_read already done the check, why need check here again?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

bchlib_read() checks the device boundary. bch_limit_transfer() checks whether advancing f_pos after the transfer will still fit in off_t. For example, on a device of NX_OFF_MAX + 1 bytes, a two byte read from NX_OFF_MAX - 1 fits on the device and would advance f_pos past its limit. The driver caps that read at one byte. The write path applies the same check before doing I/O.

Comment thread drivers/bch/bchdev_register.c Resolved
Comment thread fs/vfs/fs_fstat.c Resolved
Use off_t for register_driver_with_size() and the inode size so
large-file builds can retain capacities larger than size_t. Keep
memory-backed pseudofile operations within their allocation limits.

Signed-off-by: ocadovski <ognjen.cadovski@robbit.rs>
Register the character device with the byte capacity of the backing
block device. Use blkcnt_t for sector counts and indices, and check
seek and transfer positions against the off_t limit.

Report zero for capacities outside the range of off_t. Preserve the
character-device type and permissions. Document how to query the
underlying MTD geometry.

Refs: apache#16074
Signed-off-by: ocadovski <ognjen.cadovski@robbit.rs>
@OgnjenX
OgnjenX force-pushed the fix/bch-stat-capacity branch from 0324aea to b551aa5 Compare December 5, 2026 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] stat() does not work on flash partition with BCH layer

2 participants