I probably don't quite understand the nature of the real problem. Why does the user have to enter anything more than the number of 1K blocks available? From that it should be easy for the system to calculate the maximum possible number of data blocks and then ask the user how many to actually allocate. Shouldn't it? That would make it easy to check if the user entered too large a value.
The minimum number of 1K blocks to create anything would be two. One bitmap block (mostly wasted) and one data block. For 8192 data blocks, 8193 1K blocks are needed. For 8193 data blocks, 8195 1K blocks (two bitmap blocks and 8193 data blocks).
So given N 1K blocks, the maximum number of wholly allocated bitmap blocks W would be:
Code:
W = N/8193 (floored division)
and the number of partially allocated bitmap blocks P would be:
Code:
P = 1 if N%8193 != 0 else 0
So the total number of data blocks D is:
Code:
D = N - W - P
Trying this for a few values of N:
1: D = 1 - 0 - 1 = 0 (minimum two required for one data block)
2: D = 2 - 0 - 1 = 1
3: D = 3 - 0 - 1 = 2
8192: D = 8192 - 0 - 1 = 8191
8193: D = 8193 - 1 - 0 = 8192
8194: D = 8194 - 1 - 1 = 8192 (which is correct, as two more 1K blocks are needed to add one more 1K data block)
8195: D = 8195 - 1 - 1 = 8193