- switch(st->minor_version) {
- case 0:
- sb_offset = dsize;
- sb_offset -= 8*2;
- sb_offset &= ~(4*2-1);
- sb->super_offset = __cpu_to_le64(sb_offset);
- sb->data_offset = __cpu_to_le64(0);
- sb->data_size = __cpu_to_le64(sb_offset - bm_space);
- break;
- case 1:
- sb->super_offset = __cpu_to_le64(0);
- sb->data_offset = __cpu_to_le64(bm_space + 4*2);
- sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
- break;
- case 2:
- sb_offset = 4*2;
- if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
- space = 64*2;
- else
- space = 4*2;
- sb->super_offset = __cpu_to_le64(4*2);
- sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
- sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space );
- break;
- default:
- return -EINVAL;
- }
+ /*
+ * Calculate the position of the superblock.
+ * It is always aligned to a 4K boundary and
+ * depending on minor_version, it can be:
+ * 0: At least 8K, but less than 12K, from end of device
+ * 1: At start of device
+ * 2: 4K from start of device.
+ * Depending on the array size, we might leave extra space
+ * for a bitmap.
+ */
+ array_size = __le64_to_cpu(sb->size);
+ /* work out how much space we left for a bitmap */
+ bm_space = choose_bm_space(array_size);
+
+ switch(st->minor_version) {
+ case 0:
+ sb_offset = dsize;
+ sb_offset -= 8*2;
+ sb_offset &= ~(4*2-1);
+ sb->super_offset = __cpu_to_le64(sb_offset);
+ sb->data_offset = __cpu_to_le64(0);
+ if (sb_offset - bm_space < array_size)
+ bm_space = sb_offset - array_size;
+ sb->data_size = __cpu_to_le64(sb_offset - bm_space);
+ break;
+ case 1:
+ sb->super_offset = __cpu_to_le64(0);
+ if (4*2 + bm_space + __le64_to_cpu(sb->size) > dsize)
+ bm_space = dsize - __le64_to_cpu(sb->size) -4*2;
+ sb->data_offset = __cpu_to_le64(bm_space + 4*2);
+ sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
+ break;
+ case 2:
+ sb_offset = 4*2;
+ sb->super_offset = __cpu_to_le64(4*2);
+ if (4*2 + 4*2 + bm_space + __le64_to_cpu(sb->size)
+ > dsize)
+ bm_space = dsize - __le64_to_cpu(sb->size)
+ - 4*2 - 4*2;
+ sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
+ sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2
+ - bm_space );
+ break;
+ default:
+ return -EINVAL;
+ }