# Inconsistent blocking addressing in RFS

Reported by: Owned by: Fan Deng Fan Deng normal 5.1 fs 4.11 normal Chris Johns

### Description

Background
There are two ways to address a block in RFS:

1. Via a single 32bit block number (bno)
2. Via a group number(gno) and a bit offset (bit)

They should be fully convertible (1-1 mapping). In other words, the equation to convert 1 to 2 should be unique within the RFS implementation.

The bug
The RFS implementation contains two different conversions between 1 and 2.

Details

1. In rtems_rfs_group_bitmap_alloc (rtems-rfs-group.c, line 172)
```bno = gno * group_blocks + bit
```
1. In rtems_rfs_group_bitmap_alloc (rtems-rfs-group.c, line 228)
```bno = gno * group_blocks + bit + 1 (via rtems_rfs_group_block() function)
```
1. In rtems_rfs_group_bitmap_free (rtems-rfs-group.c, line 283)
```bno = gno * group_blocks + bit + 1 (RTEMS_RFS_SUPERBLOCK_SIZE)
```
1. In rtems_rfs_group_bitmap_test (rtems-rfs-group.c, line 332)
```bno = gno * group_blocks + bit
```

To summarize, the implementation contains two ways of converting a bno to a (gno, bit) pair:

Either:

```bno = gno * group_blocks + bit
```

Or:

```bno = gno * group_blocks + bit + 1
```

The Fix
The RFS implementation should consistently convert a bno to a (gno, bit) pair with:

```bno = gno * group_blocks + bit + RTEMS_RFS_SUPERBLOCK_SIZE
```

This is because the superblock is not accounted for in the block bitmaps. So places to change:

1. rtems-rfs-group.c: all references to the conversion must be updated to use RTEMS_RFS_SUPERBLOCK_SIZE explicitly.
1. rtems_rfs_group_block converts the pair to bno via:
```#define rtems_rfs_group_block(_g, _b) (((_g)->base) + (_b))
```

(_g)->base is calculated via rtems-rfs-format.c from:

```#define rtems_rfs_fs_block(_fs, _grp, _blk) \
((((_fs)->group_blocks) * (_grp)) + (_blk) + 1)
```

The "+ 1" part should really be "+ RTEMS_RFS_SUPERBLOCK_SIZE" to be logically correct. As RTEMS_RFS_SUPERBLOCK_SIZE itself has a comment saying:

```/**
* Number of blocks in the superblock. Yes I know it is a superblock and not
* superblocks but if for any reason this needs to change it is handled.
*/
#define RTEMS_RFS_SUPERBLOCK_SIZE (1)
```

### comment:1 Changed on 11/09/17 at 06:27:14 by Sebastian Huber

Milestone: 4.12.0 → 5.1

Milestone renamed

### comment:2 Changed on 04/11/18 at 01:57:45 by Chris Johns <chrisj@…>

Resolution: → fixed assigned → closed

fstest/fsrfsbitmap01: Update RFS bitmap tests to test fixes.

Add tests to check the patches for this ticket exist and are fixed.

Close #3089

Note: See TracTickets for help on using tickets.