md_bitmap_read_sb uses wrong bitmap blocks
PATCH v2 1/2 md_bitmap_read_sb uses wrong bitmap blocks
The patched code is used to get chunks number, should use round-up div to replace current sector_div.
The same code is in md_bitmap_resize():
``` chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift); ```
---
v2:
- change grammar mistake in patch Subject
- fix code syntax error
--- drivers/md/md-bitmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/md/md-bitmap.c +2 -2
@@ -605,8 +605,8 @@ static int md_bitmap_read_sb(struct bitmap *bitmap)if (bitmap->cluster_slot >=0 ) {sector_t bm_blocks = bitmap->mddev->resync_max_sectors; -sector_div (bm_blocks, - bitmap->mddev->bitmap_info.chunksize >>9 ); + bm_blocks =DIV_ROUND_UP_SECTOR_T (bm_blocks, + (bitmap->mddev->bitmap_info.chunksize >>9 ));/* bits to bytes */ bm_blocks = ((bm_blocks+7 ) >>3 ) +sizeof (bitmap_super_t);/* to 4k blocks */
Signature · 1 line
2.27.0
PATCH v2 2/2 md_bitmap_get_counter returns wrong blocks
md_bitmap_get_counter() has code:
```
if (bitmap->bp[page].hijacked ||
bitmap->bp[page].map == NULL)
csize = ((sector_t)1) << (bitmap->chunkshift +
PAGE_COUNTER_SHIFT - 1);
```the minus 1 is wrong, this branch should report 2048 bits of space. with "-1" action, this only report 1024 bit of space.
this bug code returns wrong blocks, but it doesn't inflence bitmap logic:
1. most callers focus this function return value (the counter of offset), not the parameter blocks.
2. the bug is only triggered when hijacked is true or map is NULL. the hijacked true condition is very rare.
the "map == null" only true when array is creating or resizing.
3. even the caller gets wrong blocks, current code makes caller just to call md_bitmap_get_counter() one more time.
---
v2:
- shorten patch Subject
--- drivers/md/md-bitmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/md-bitmap.c +1 -1
@@ -1367,7 +1367,7 @@ __acquires(bitmap->lock)if (bitmap->bp[page].hijacked || bitmap->bp[page].map ==NULL ) csize = ((sector_t )1 ) << (bitmap->chunkshift + -PAGE_COUNTER_SHIFT -1 ); +PAGE_COUNTER_SHIFT );else csize = ((sector_t )1 ) << bitmap->chunkshift; *blocks = csize - (offset & (csize -1 ));
Signature · 1 line
2.27.0
Quoted 9 lines · Zhao Heming
The patched code is used to get chunks number, should use round-up div to replace current
sector_div.
The same code is inmd_bitmap_resize():``` chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift); ```Signed-off-by Zhao Heming <[email protected]>
Applied both patches to md-next.
Thanks,
Song