+ /*
+ * 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 < array_size + bm_space)
+ 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);
+ reserved = bm_space + 4*2;
+ /* Try for multiple of 1Meg so it is nicely aligned */
+ #define ONE_MEG (2*1024)
+ reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
+ if (reserved + __le64_to_cpu(sb->size) > dsize)
+ reserved = dsize - __le64_to_cpu(sb->size);
+ /* force 4K alignment */
+ reserved &= ~7ULL;
+
+ sb->data_offset = __cpu_to_le64(reserved);
+ sb->data_size = __cpu_to_le64(dsize - reserved);
+ 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;
+
+ reserved = bm_space + 4*2 + 4*2;
+ /* Try for multiple of 1Meg so it is nicely aligned */
+ #define ONE_MEG (2*1024)
+ reserved = ((reserved + ONE_MEG-1)/ONE_MEG) * ONE_MEG;
+ if (reserved + __le64_to_cpu(sb->size) > dsize)
+ reserved = dsize - __le64_to_cpu(sb->size);
+ /* force 4K alignment */
+ reserved &= ~7ULL;
+
+ sb->data_offset = __cpu_to_le64(reserved);
+ sb->data_size = __cpu_to_le64(dsize - reserved);
+ break;
+ default:
+ return -EINVAL;
+ }