Improve allocation and use of space for bitmaps in version1 metadata
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
82d9eba6
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30#include "mdadm.h"
82d9eba6
NB
31/*
32 * The version-1 superblock :
33 * All numeric fields are little-endian.
34 *
35 * total size: 256 bytes plus 2 per device.
36 * 1K allows 384 devices.
37 */
38struct mdp_superblock_1 {
39 /* constant array information - 128 bytes */
40 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
41 __u32 major_version; /* 1 */
42 __u32 feature_map; /* 0 for now */
43 __u32 pad0; /* always set to 0 when writing */
44
45 __u8 set_uuid[16]; /* user-space generated. */
46 char set_name[32]; /* set and interpreted by user-space */
47
48 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
49 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
50 __u32 layout; /* only for raid5 currently */
51 __u64 size; /* used size of component devices, in 512byte sectors */
52
53 __u32 chunksize; /* in 512byte sectors */
54 __u32 raid_disks;
34163fc7
NB
55 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
56 * NOTE: signed, so bitmap can be before superblock
57 * only meaningful of feature_map[0] is set.
58 */
b674b5b8
NB
59
60 /* These are only valid with feature bit '4' */
b674b5b8 61 __u32 new_level; /* new level we are reshaping to */
ac957baa 62 __u64 reshape_position; /* next address in array-space for reshape */
b674b5b8
NB
63 __u32 delta_disks; /* change in number of raid_disks */
64 __u32 new_layout; /* new layout */
65 __u32 new_chunk; /* new chunk size (bytes) */
66 __u8 pad1[128-124]; /* set to 0 when written */
82d9eba6
NB
67
68 /* constant this-device information - 64 bytes */
69 __u64 data_offset; /* sector start of data, often 0 */
70 __u64 data_size; /* sectors in this device that can be used for data */
71 __u64 super_offset; /* sector start of this superblock */
72 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
73 __u32 dev_number; /* permanent identifier of this device - not role in raid */
74 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
75 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
dfd4d8ee
NB
76 __u8 devflags; /* per-device flags. Only one defined...*/
77#define WriteMostly1 1 /* mask for writemostly flag in above */
78 __u8 pad2[64-57]; /* set to 0 when writing */
82d9eba6
NB
79
80 /* array state information - 64 bytes */
81 __u64 utime; /* 40 bits second, 24 btes microseconds */
82 __u64 events; /* incremented when superblock updated */
83 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
84 __u32 sb_csum; /* checksum upto devs[max_dev] */
85 __u32 max_dev; /* size of devs[] array to consider */
86 __u8 pad3[64-32]; /* set to 0 when writing */
87
88 /* device state information. Indexed by dev_number.
89 * 2 bytes per device
90 * Note there are no per-device state flags. State information is rolled
91 * into the 'roles' value. If a device is spare or faulty, then it doesn't
92 * have a meaningful role.
93 */
94 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
95};
96
d2cd3ffc
NB
97/* feature_map bits */
98#define MD_FEATURE_BITMAP_OFFSET 1
99#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
100 * must be honoured
101 */
b674b5b8 102#define MD_FEATURE_RESHAPE_ACTIVE 4
d2cd3ffc 103
b674b5b8 104#define MD_FEATURE_ALL (1|2|4)
d2cd3ffc 105
570c0542 106#ifndef offsetof
067db4df 107#define offsetof(t,f) ((size_t)&(((t*)0)->f))
570c0542 108#endif
82d9eba6
NB
109static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
110{
111 unsigned int disk_csum, csum;
112 unsigned long long newcsum;
113 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
114 unsigned int *isuper = (unsigned int*)sb;
115 int i;
116
117/* make sure I can count... */
118 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
119 offsetof(struct mdp_superblock_1, utime) != 192 ||
120 sizeof(struct mdp_superblock_1) != 256) {
121 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
122 }
123
124 disk_csum = sb->sb_csum;
125 sb->sb_csum = 0;
126 newcsum = 0;
722966c6
PC
127 for (i=0; size>=4; size -= 4 ) {
128 newcsum += __le32_to_cpu(*isuper);
129 isuper++;
130 }
82d9eba6
NB
131
132 if (size == 2)
133 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
134
135 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
136 sb->sb_csum = disk_csum;
83205b64 137 return __cpu_to_le32(csum);
82d9eba6
NB
138}
139
c7654afc 140#ifndef MDASSEMBLE
a1cbd7d0 141static void examine_super1(void *sbv, char *homehost)
82d9eba6
NB
142{
143 struct mdp_superblock_1 *sb = sbv;
144 time_t atime;
145 int d;
6fbba4c9 146 int faulty;
82d9eba6
NB
147 int i;
148 char *c;
a1cbd7d0 149 int l = homehost ? strlen(homehost) : 0;
83205b64 150 int layout;
82d9eba6
NB
151
152 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
91eedefc
NB
153 printf(" Version : %02d\n", 1);
154 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
155 printf(" Array UUID : ");
156 for (i=0; i<16; i++) {
82d9eba6 157 if ((i&3)==0 && i != 0) printf(":");
34163fc7 158 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
159 }
160 printf("\n");
a1cbd7d0
NB
161 printf(" Name : %.32s", sb->set_name);
162 if (l > 0 && l < 32 &&
163 sb->set_name[l] == ':' &&
164 strncmp(sb->set_name, homehost, l) == 0)
165 printf(" (local to host %s)", homehost);
166 printf("\n");
82d9eba6
NB
167 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
168 printf(" Creation Time : %.24s\n", ctime(&atime));
169 c=map_num(pers, __le32_to_cpu(sb->level));
170 printf(" Raid Level : %s\n", c?c:"-unknown-");
171 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
172 printf("\n");
570c0542 173 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
b674b5b8
NB
174 if (__le32_to_cpu(sb->level) >= 0) {
175 int ddsks=0;
176 switch(__le32_to_cpu(sb->level)) {
177 case 1: ddsks=1;break;
178 case 4:
83205b64
NB
179 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
180 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
181 case 10:
182 layout = __le32_to_cpu(sb->layout);
183 ddsks = __le32_to_cpu(sb->raid_disks)
184 / (layout&255) / ((layout>>8)&255);
b674b5b8
NB
185 }
186 if (ddsks)
83205b64 187 printf(" Array Size : %llu%s\n",
c1c05f7f 188 ddsks*(unsigned long long)__le64_to_cpu(sb->size),
83205b64 189 human_size(ddsks*__le64_to_cpu(sb->size)<<9));
b674b5b8 190 if (sb->size != sb->data_size)
83205b64
NB
191 printf(" Used Size : %llu%s\n",
192 (unsigned long long)__le64_to_cpu(sb->size),
193 human_size(__le64_to_cpu(sb->size)<<9));
b674b5b8 194 }
82d9eba6 195 if (sb->data_offset)
199171a2
NB
196 printf(" Data Offset : %llu sectors\n",
197 (unsigned long long)__le64_to_cpu(sb->data_offset));
198 printf(" Super Offset : %llu sectors\n",
199 (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
200 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
201 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 202 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
203 printf(" Device UUID : ");
204 for (i=0; i<16; i++) {
82d9eba6 205 if ((i&3)==0 && i != 0) printf(":");
34163fc7 206 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
207 }
208 printf("\n");
b674b5b8 209 printf("\n");
91eedefc
NB
210 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
211 printf("Internal Bitmap : %ld sectors from superblock\n",
8fac0577
NB
212 (long)__le32_to_cpu(sb->bitmap_offset));
213 }
b674b5b8 214 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 215 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
216 human_size(__le64_to_cpu(sb->reshape_position)<<9));
217 if (__le32_to_cpu(sb->delta_disks)) {
218 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
219 if (__le32_to_cpu(sb->delta_disks))
220 printf(" (%d->%d)\n",
221 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
222 __le32_to_cpu(sb->raid_disks));
223 else
224 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
225 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
226 }
227 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
228 c = map_num(pers, __le32_to_cpu(sb->new_level));
229 printf(" New Level : %s\n", c?c:"-unknown-");
230 }
231 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
232 if (__le32_to_cpu(sb->level) == 5) {
233 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
234 printf(" New Layout : %s\n", c?c:"-unknown-");
235 }
236 if (__le32_to_cpu(sb->level) == 10) {
b578481c 237 printf(" New Layout : near=%d, %s=%d\n",
b674b5b8 238 __le32_to_cpu(sb->new_layout)&255,
b578481c 239 (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
b674b5b8
NB
240 (__le32_to_cpu(sb->new_layout)>>8)&255);
241 }
242 }
243 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
244 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
245 printf("\n");
246 }
dfd4d8ee
NB
247 if (sb->devflags) {
248 printf(" Flags :");
34163fc7 249 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
250 printf(" write-mostly");
251 printf("\n");
252 }
82d9eba6
NB
253
254 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
255 printf(" Update Time : %.24s\n", ctime(&atime));
256
257 if (calc_sb_1_csum(sb) == sb->sb_csum)
258 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
259 else
260 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
261 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 262 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
263 printf("\n");
264 if (__le32_to_cpu(sb->level) == 5) {
265 c = map_num(r5layout, __le32_to_cpu(sb->layout));
266 printf(" Layout : %s\n", c?c:"-unknown-");
267 }
265e0f17
NB
268 if (__le32_to_cpu(sb->level) == 10) {
269 int lo = __le32_to_cpu(sb->layout);
b578481c
NB
270 printf(" Layout : near=%d, %s=%d\n",
271 lo&255,
272 (lo&0x10000)?"offset":"far",
273 (lo>>8)&255);
265e0f17 274 }
82d9eba6
NB
275 switch(__le32_to_cpu(sb->level)) {
276 case 0:
277 case 4:
278 case 5:
d2cd3ffc
NB
279 case 6:
280 case 10:
b674b5b8 281 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
282 break;
283 case -1:
b674b5b8 284 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
285 break;
286 default: break;
287 }
288 printf("\n");
c3684618
NB
289 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
290 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
291 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
292 break;
293 for (d=0; d < i; d++) {
294 int role = __le16_to_cpu(sb->dev_roles[d]);
295 if (d) printf(", ");
296 if (role == 0xffff) printf("empty");
297 else if(role == 0xfffe) printf("failed");
298 else printf("%d", role);
299 }
300 printf(")\n");
82d9eba6
NB
301 printf(" Array State : ");
302 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
303 int cnt = 0;
304 int me = 0;
305 int i;
306 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
307 int role = __le16_to_cpu(sb->dev_roles[i]);
308 if (role == d) {
309 if (i == __le32_to_cpu(sb->dev_number))
310 me = 1;
311 cnt++;
312 }
313 }
314 if (cnt > 1) printf("?");
315 else if (cnt == 1 && me) printf("U");
316 else if (cnt == 1) printf("u");
317 else printf ("_");
318 }
6fbba4c9 319 faulty = 0;
82d9eba6
NB
320 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
321 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
322 if (role == 0xFFFE)
323 faulty++;
82d9eba6 324 }
82d9eba6
NB
325 if (faulty) printf(" %d failed", faulty);
326 printf("\n");
327}
328
329
330static void brief_examine_super1(void *sbv)
331{
332 struct mdp_superblock_1 *sb = sbv;
333 int i;
2998bc01 334 char *nm;
b015e6c2 335 char *c=map_num(pers, __le32_to_cpu(sb->level));
2998bc01
NB
336
337 nm = strchr(sb->set_name, ':');
338 if (nm)
339 nm++;
340 else if (sb->set_name[0])
341 nm = sb->set_name;
342 else
343 nm = "??";
82d9eba6 344
2998bc01
NB
345 printf("ARRAY /dev/md/%s level=%s metadata=1 num-devices=%d UUID=",
346 nm,
83205b64 347 c?c:"-unknown-", __le32_to_cpu(sb->raid_disks));
82d9eba6 348 for (i=0; i<16; i++) {
82d9eba6 349 if ((i&3)==0 && i != 0) printf(":");
b7a708af 350 printf("%02x", sb->set_uuid[i]);
82d9eba6 351 }
947fd4dd
NB
352 if (sb->set_name[0])
353 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
354 printf("\n");
355}
356
b6750aa8 357static void detail_super1(void *sbv, char *homehost)
82d9eba6
NB
358{
359 struct mdp_superblock_1 *sb = sbv;
360 int i;
b6750aa8 361 int l = homehost ? strlen(homehost) : 0;
82d9eba6 362
b6750aa8
NB
363 printf(" Name : %.32s", sb->set_name);
364 if (l > 0 && l < 32 &&
365 sb->set_name[l] == ':' &&
366 strncmp(sb->set_name, homehost, l) == 0)
367 printf(" (local to host %s)", homehost);
368 printf("\n UUID : ");
82d9eba6 369 for (i=0; i<16; i++) {
82d9eba6 370 if ((i&3)==0 && i != 0) printf(":");
34163fc7 371 printf("%02x", sb->set_uuid[i]);
82d9eba6 372 }
570c0542 373 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
374}
375
376static void brief_detail_super1(void *sbv)
377{
378 struct mdp_superblock_1 *sb = sbv;
379 int i;
380
947fd4dd
NB
381 if (sb->set_name[0])
382 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
383 printf(" UUID=");
384 for (i=0; i<16; i++) {
82d9eba6 385 if ((i&3)==0 && i != 0) printf(":");
34163fc7 386 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
387 }
388}
389
c7654afc
NB
390#endif
391
83b6208e
NB
392static int match_home1(void *sbv, char *homehost)
393{
394 struct mdp_superblock_1 *sb = sbv;
395 int l = homehost ? strlen(homehost) : 0;
396
397 return (l > 0 && l < 32 &&
398 sb->set_name[l] == ':' &&
399 strncmp(sb->set_name, homehost, l) == 0);
400}
401
82d9eba6
NB
402static void uuid_from_super1(int uuid[4], void * sbv)
403{
404 struct mdp_superblock_1 *super = sbv;
405 char *cuuid = (char*)uuid;
406 int i;
407 for (i=0; i<16; i++)
408 cuuid[i] = super->set_uuid[i];
409}
410
31317663 411static void getinfo_super1(struct mdinfo *info, void *sbv)
82d9eba6
NB
412{
413 struct mdp_superblock_1 *sb = sbv;
414 int working = 0;
415 int i;
416 int role;
417
418 info->array.major_version = 1;
419 info->array.minor_version = __le32_to_cpu(sb->feature_map);
420 info->array.patch_version = 0;
421 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
422 info->array.level = __le32_to_cpu(sb->level);
265e0f17 423 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
424 info->array.md_minor = -1;
425 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 426 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 427 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
353632d9
NB
428
429 info->data_offset = __le64_to_cpu(sb->data_offset);
430 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
431
432 info->disk.major = 0;
433 info->disk.minor = 0;
fbf8a0b7 434 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
435 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
436 __le32_to_cpu(sb->max_dev) > 512)
437 role = 0xfffe;
438 else
439 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
440
441 info->disk.raid_disk = -1;
442 switch(role) {
443 case 0xFFFF:
444 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
445 break;
446 case 0xFFFE:
447 info->disk.state = 1; /* faulty */
448 break;
449 default:
450 info->disk.state = 6; /* active and in sync */
451 info->disk.raid_disk = role;
452 }
453 info->events = __le64_to_cpu(sb->events);
454
455 memcpy(info->uuid, sb->set_uuid, 16);
456
31317663
NB
457 strncpy(info->name, sb->set_name, 32);
458 info->name[32] = 0;
947fd4dd 459
353632d9
NB
460 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
461 info->reshape_active = 1;
462 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
463 info->new_level = __le32_to_cpu(sb->new_level);
464 info->delta_disks = __le32_to_cpu(sb->delta_disks);
465 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 466 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
353632d9
NB
467 } else
468 info->reshape_active = 0;
469
82d9eba6
NB
470 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
471 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 472 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
473 working++;
474 }
475
476 info->array.working_disks = working;
477}
478
e5eac01f
NB
479static int update_super1(struct mdinfo *info, void *sbv, char *update,
480 char *devname, int verbose,
481 int uuid_set, char *homehost)
82d9eba6 482{
c4d831e1
NB
483 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
484 * For others, the return value is ignored.
485 */
82d9eba6
NB
486 int rv = 0;
487 struct mdp_superblock_1 *sb = sbv;
488
67a8c82d
NB
489 if (strcmp(update, "force-one")==0) {
490 /* Not enough devices for a working array,
491 * so bring this one up-to-date
492 */
c4d831e1
NB
493 if (sb->events != __cpu_to_le64(info->events))
494 rv = 1;
6d3d5804 495 sb->events = __cpu_to_le64(info->events);
67a8c82d
NB
496 }
497 if (strcmp(update, "force-array")==0) {
498 /* Degraded array and 'force' requests to
499 * maybe need to mark it 'clean'.
500 */
82d9eba6
NB
501 switch(__le32_to_cpu(sb->level)) {
502 case 5: case 4: case 6:
503 /* need to force clean */
c4d831e1
NB
504 if (sb->resync_offset != ~0ULL)
505 rv = 1;
82d9eba6
NB
506 sb->resync_offset = ~0ULL;
507 }
508 }
509 if (strcmp(update, "assemble")==0) {
510 int d = info->disk.number;
511 int want;
512 if (info->disk.state == 6)
513 want = __cpu_to_le32(info->disk.raid_disk);
514 else
515 want = 0xFFFF;
516 if (sb->dev_roles[d] != want) {
517 sb->dev_roles[d] = want;
518 rv = 1;
519 }
520 }
82d9eba6
NB
521 if (strcmp(update, "grow") == 0) {
522 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
523 /* FIXME */
524 }
525 if (strcmp(update, "resync") == 0) {
526 /* make sure resync happens */
434b7755 527 sb->resync_offset = 0ULL;
82d9eba6 528 }
14263cf1 529 if (strcmp(update, "uuid") == 0) {
838acbc2 530 memcpy(sb->set_uuid, info->uuid, 16);
14263cf1
NB
531 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
532 struct bitmap_super_s *bm;
533 bm = (struct bitmap_super_s*)(sbv+1024);
534 memcpy(bm->uuid, info->uuid, 16);
535 }
536 }
0237e0ca
NB
537 if (strcmp(update, "homehost") == 0 &&
538 homehost) {
539 char *c;
540 update = "name";
541 c = strchr(sb->set_name, ':');
542 if (c)
543 strncpy(info->name, c+1, 31 - (c-sb->set_name));
544 else
545 strncpy(info->name, sb->set_name, 32);
26a0b8fd 546 info->name[32] = 0;
0237e0ca 547 }
c4f12c13
NB
548 if (strcmp(update, "name") == 0) {
549 if (info->name[0] == 0)
550 sprintf(info->name, "%d", info->array.md_minor);
551 memset(sb->set_name, 0, sizeof(sb->set_name));
552 if (homehost &&
553 strchr(info->name, ':') == NULL &&
554 strlen(homehost)+1+strlen(info->name) < 32) {
555 strcpy(sb->set_name, homehost);
556 strcat(sb->set_name, ":");
557 strcat(sb->set_name, info->name);
558 } else
559 strcpy(sb->set_name, info->name);
560 }
353632d9
NB
561 if (strcmp(update, "_reshape_progress")==0)
562 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
563
564 sb->sb_csum = calc_sb_1_csum(sb);
565 return rv;
566}
567
05697ec1
NB
568static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
569 unsigned long long size, char *name, char *homehost)
82d9eba6 570{
34163fc7 571 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 572 int spares;
34163fc7 573 int rfd;
05697ec1 574 char defname[10];
82d9eba6
NB
575 memset(sb, 0, 1024);
576
05697ec1 577 if (info->major_version == -1) {
82d9eba6 578 /* zeroing superblock */
05697ec1 579 *sbp = sb;
82d9eba6 580 return 0;
05697ec1 581 }
82d9eba6
NB
582
583 spares = info->working_disks - info->active_disks;
584 if (info->raid_disks + spares > 384) {
585 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
586 info->raid_disks , spares, 384);
587 return 0;
588 }
589
82d9eba6
NB
590 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
591 sb->major_version = __cpu_to_le32(1);
592 sb->feature_map = 0;
593 sb->pad0 = 0;
34163fc7
NB
594
595 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
596 read(rfd, sb->set_uuid, 16) != 16) {
597 *(__u32*)(sb->set_uuid) = random();
598 *(__u32*)(sb->set_uuid+4) = random();
599 *(__u32*)(sb->set_uuid+8) = random();
600 *(__u32*)(sb->set_uuid+12) = random();
601 }
602 if (rfd >= 0) close(rfd);
82d9eba6 603
05697ec1
NB
604 if (name == NULL || *name == 0) {
605 sprintf(defname, "%d", info->md_minor);
606 name = defname;
607 }
947fd4dd 608 memset(sb->set_name, 0, 32);
05697ec1
NB
609 if (homehost &&
610 strchr(name, ':')== NULL &&
611 strlen(homehost)+1+strlen(name) < 32) {
612 strcpy(sb->set_name, homehost);
613 strcat(sb->set_name, ":");
614 strcat(sb->set_name, name);
615 } else
616 strcpy(sb->set_name, name);
82d9eba6
NB
617
618 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
619 sb->level = __cpu_to_le32(info->level);
ea329559 620 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 621 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
622 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
623 sb->raid_disks = __cpu_to_le32(info->raid_disks);
624
625 sb->data_offset = __cpu_to_le64(0);
626 sb->data_size = __cpu_to_le64(0);
627 sb->super_offset = __cpu_to_le64(0);
628 sb->recovery_offset = __cpu_to_le64(0);
629
630 sb->utime = sb->ctime;
631 sb->events = __cpu_to_le64(1);
34321279 632 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
633 sb->resync_offset = ~0ULL;
634 else
635 sb->resync_offset = 0;
34163fc7 636 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
637 sizeof(sb->dev_roles[0]));
638 memset(sb->pad3, 0, sizeof(sb->pad3));
639
640 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
641
642 *sbp = sb;
643 return 1;
644}
645
646/* Add a device to the superblock being created */
647static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
648{
649 struct mdp_superblock_1 *sb = sbv;
650 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 651 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 652 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 653 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 654 *rp = 0xffff;
34163fc7 655 else
82d9eba6
NB
656 *rp = 0xfffe;
657}
658
14263cf1
NB
659static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
660
96395475 661static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
662{
663 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 664 unsigned long long sb_offset;
82d9eba6 665 int sbsize;
5dd497ee
NB
666 unsigned long size;
667 unsigned long long dsize;
96395475 668
5dd497ee
NB
669#ifdef BLKGETSIZE64
670 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
671#endif
672 {
673 if (ioctl(fd, BLKGETSIZE, &size))
674 return 1;
675 else
676 dsize = (unsigned long long)size;
677 } else
678 dsize >>= 9;
96395475 679
5dd497ee 680 if (dsize < 24)
96395475
NB
681 return 2;
682
683 /*
684 * Calculate the position of the superblock.
685 * It is always aligned to a 4K boundary and
686 * depending on minor_version, it can be:
687 * 0: At least 8K, but less than 12K, from end of device
688 * 1: At start of device
689 * 2: 4K from start of device.
690 */
691 switch(st->minor_version) {
692 case 0:
5dd497ee 693 sb_offset = dsize;
96395475
NB
694 sb_offset -= 8*2;
695 sb_offset &= ~(4*2-1);
696 break;
697 case 1:
6fbba4c9 698 sb_offset = 0;
96395475
NB
699 break;
700 case 2:
701 sb_offset = 4*2;
702 break;
703 default:
704 return -EINVAL;
705 }
706
82d9eba6 707
34163fc7 708
6fbba4c9
NB
709 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
710 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
711 ) {
712 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
713 abort();
714 }
82d9eba6 715
6fbba4c9 716 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
717 return 3;
718
719 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
720
721 if (write(fd, sb, sbsize) != sbsize)
722 return 4;
723
14263cf1
NB
724 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
725 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 726 (((char*)sb)+1024);
14263cf1
NB
727 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
728 locate_bitmap1(st, fd, sbv);
9fca7d62
NB
729 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
730 return 5;
14263cf1
NB
731 }
732 }
570c0542 733 fsync(fd);
82d9eba6
NB
734 return 0;
735}
736
892debc8
NB
737static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
738
34163fc7 739static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 740 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
741{
742 struct mdp_superblock_1 *sb = sbv;
e478dc86 743 void *refsbv = NULL;
892debc8 744 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 745 int rfd;
82d9eba6 746 int rv;
199171a2 747 int bm_space;
82d9eba6 748
199171a2 749 unsigned long space;
8fac0577 750 unsigned long long dsize, array_size;
82d9eba6
NB
751 long long sb_offset;
752
753
754 if (fd < 0) {
755 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
756 devname);
757 return -1;
758 }
759
760 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 761 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
83205b64 762 sb->devflags |= __cpu_to_le32(WriteMostly1);
892debc8 763
dfe47e00
NB
764 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
765 read(rfd, sb->device_uuid, 16) != 16) {
766 *(__u32*)(sb->device_uuid) = random();
767 *(__u32*)(sb->device_uuid+4) = random();
768 *(__u32*)(sb->device_uuid+8) = random();
769 *(__u32*)(sb->device_uuid+12) = random();
770 }
771 if (rfd >= 0) close(rfd);
892debc8
NB
772 sb->events = 0;
773
e478dc86
NB
774 if (load_super1(st, fd, &refsbv, NULL)==0) {
775 struct mdp_superblock_1 *refsb = refsbv;
776
892debc8
NB
777 memcpy(sb->device_uuid, refsb->device_uuid, 16);
778 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
779 /* same array, so preserve events and dev_number */
780 sb->events = refsb->events;
7eae7080
NB
781 /* bugs in 2.6.17 and earlier mean the dev_number
782 * chosen in Manage must be preserved
783 */
784 if (get_linux_version() >= 2006018)
785 sb->dev_number = refsb->dev_number;
892debc8
NB
786 }
787 free(refsb);
788 }
34163fc7 789
5dd497ee
NB
790#ifdef BLKGETSIZE64
791 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
792#endif
793 {
199171a2 794 unsigned long size;
5dd497ee
NB
795 if (ioctl(fd, BLKGETSIZE, &size))
796 return 1;
797 else
798 dsize = size;
799 } else
800 dsize >>= 9;
82d9eba6 801
5dd497ee 802 if (dsize < 24) {
024ce7fe 803 close(fd);
82d9eba6 804 return 2;
024ce7fe 805 }
82d9eba6
NB
806
807
808 /*
809 * Calculate the position of the superblock.
810 * It is always aligned to a 4K boundary and
811 * depending on minor_version, it can be:
812 * 0: At least 8K, but less than 12K, from end of device
813 * 1: At start of device
814 * 2: 4K from start of device.
21e92547
NB
815 * Depending on the array size, we might leave extra space
816 * for a bitmap.
82d9eba6 817 */
8fac0577 818 array_size = __le64_to_cpu(sb->size);
199171a2
NB
819 /* work out how much space we left of a bitmap */
820 if (array_size >= 200*1024*1024*2)
821 bm_space = 128*2;
822 else if (array_size > 8*1024*1024*2)
823 bm_space = 64*2;
824 else
825 bm_space = 0;
826
82d9eba6
NB
827 switch(st->minor_version) {
828 case 0:
5dd497ee 829 sb_offset = dsize;
82d9eba6
NB
830 sb_offset -= 8*2;
831 sb_offset &= ~(4*2-1);
832 sb->super_offset = __cpu_to_le64(sb_offset);
833 sb->data_offset = __cpu_to_le64(0);
199171a2 834 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
82d9eba6
NB
835 break;
836 case 1:
837 sb->super_offset = __cpu_to_le64(0);
199171a2
NB
838 sb->data_offset = __cpu_to_le64(bm_space + 4*2);
839 sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2);
82d9eba6
NB
840 break;
841 case 2:
842 sb_offset = 4*2;
561832f9 843 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
844 space = 64*2;
845 else
846 space = 4*2;
199171a2
NB
847 sb->super_offset = __cpu_to_le64(4*2);
848 sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space);
849 sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space );
82d9eba6
NB
850 break;
851 default:
852 return -EINVAL;
853 }
854
855
856 sb->sb_csum = calc_sb_1_csum(sb);
96395475 857 rv = store_super1(st, fd, sb);
82d9eba6
NB
858 if (rv)
859 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 860
1bf4e2d9
NB
861 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
862 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 863 close(fd);
82d9eba6
NB
864 return rv;
865}
866
867static int compare_super1(void **firstp, void *secondv)
868{
869 /*
870 * return:
871 * 0 same, or first was empty, and second was copied
872 * 1 second had wrong number
873 * 2 wrong uuid
874 * 3 wrong other info
875 */
876 struct mdp_superblock_1 *first = *firstp;
877 struct mdp_superblock_1 *second = secondv;
878
879 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
880 return 1;
881 if (second->major_version != __cpu_to_le32(1))
882 return 1;
883
884 if (!first) {
14263cf1
NB
885 first = malloc(1024+sizeof(bitmap_super_t));
886 memcpy(first, second, 1024+sizeof(bitmap_super_t));
82d9eba6
NB
887 *firstp = first;
888 return 0;
889 }
890 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
891 return 2;
892
893 if (first->ctime != second->ctime ||
894 first->level != second->level ||
895 first->layout != second->layout ||
896 first->size != second->size ||
897 first->chunksize != second->chunksize ||
898 first->raid_disks != second->raid_disks)
899 return 3;
900 return 0;
901}
902
903static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
904{
905 unsigned long size;
5dd497ee 906 unsigned long long dsize;
82d9eba6
NB
907 unsigned long long sb_offset;
908 struct mdp_superblock_1 *super;
14263cf1
NB
909 int uuid[4];
910 struct bitmap_super_s *bsb;
82d9eba6
NB
911
912
913 if (st->ss == NULL) {
570c0542
NB
914 int bestvers = -1;
915 __u64 bestctime = 0;
916 /* guess... choose latest ctime */
82d9eba6
NB
917 st->ss = &super1;
918 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
919 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
920 case 0: super = *sbp;
921 if (bestvers == -1 ||
922 bestctime < __le64_to_cpu(super->ctime)) {
923 bestvers = st->minor_version;
924 bestctime = __le64_to_cpu(super->ctime);
925 }
926 free(super);
927 *sbp = NULL;
928 break;
82d9eba6
NB
929 case 1: st->ss = NULL; return 1; /*bad device */
930 case 2: break; /* bad, try next */
931 }
932 }
570c0542
NB
933 if (bestvers != -1) {
934 int rv;
935 st->minor_version = bestvers;
936 st->ss = &super1;
ea329559 937 st->max_devs = 384;
570c0542
NB
938 rv = load_super1(st, fd, sbp, devname);
939 if (rv) st->ss = NULL;
940 return rv;
941 }
82d9eba6
NB
942 st->ss = NULL;
943 return 2;
944 }
5dd497ee
NB
945#ifdef BLKGETSIZE64
946 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
947#endif
948 {
949 if (ioctl(fd, BLKGETSIZE, &size)) {
950 if (devname)
951 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
952 devname, strerror(errno));
953 return 1;
954 }
955 dsize = size;
956 } else
957 dsize >>= 9;
82d9eba6 958
5dd497ee 959 if (dsize < 24) {
82d9eba6 960 if (devname)
5dd497ee
NB
961 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
962 devname, dsize);
82d9eba6
NB
963 return 1;
964 }
965
966 /*
967 * Calculate the position of the superblock.
968 * It is always aligned to a 4K boundary and
14263cf1 969 * depending on minor_version, it can be:
82d9eba6
NB
970 * 0: At least 8K, but less than 12K, from end of device
971 * 1: At start of device
972 * 2: 4K from start of device.
973 */
974 switch(st->minor_version) {
975 case 0:
5dd497ee 976 sb_offset = dsize;
82d9eba6
NB
977 sb_offset -= 8*2;
978 sb_offset &= ~(4*2-1);
979 break;
980 case 1:
981 sb_offset = 0;
982 break;
983 case 2:
984 sb_offset = 4*2;
985 break;
986 default:
987 return -EINVAL;
988 }
989
990 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
991
992
993 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
994 if (devname)
995 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
996 devname, strerror(errno));
997 return 1;
998 }
999
34163fc7 1000 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
1001
1002 if (read(fd, super, 1024) != 1024) {
1003 if (devname)
1004 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1005 devname);
1006 free(super);
1007 return 1;
1008 }
1009
1010 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1011 if (devname)
1012 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1013 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1014 free(super);
1015 return 2;
1016 }
1017
1018 if (__le32_to_cpu(super->major_version) != 1) {
1019 if (devname)
1020 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1021 devname, __le32_to_cpu(super->major_version));
1022 free(super);
1023 return 2;
1024 }
1025 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1026 if (devname)
1027 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1028 devname);
1029 free(super);
1030 return 2;
1031 }
1032 *sbp = super;
14263cf1
NB
1033
1034 /* Now check on the bitmap superblock */
1035 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1036 return 0;
1037 /* Read the bitmap superblock and make sure it looks
1038 * valid. If it doesn't clear the bit. An --assemble --force
1039 * should get that written out.
1040 */
1041 locate_bitmap1(st, fd, super);
1042 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1043 != sizeof(struct bitmap_super_s))
1044 goto no_bitmap;
1045
1046 uuid_from_super1(uuid, super);
1047 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1048 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1049 memcmp(bsb->uuid, uuid, 16) != 0)
1050 goto no_bitmap;
1051 return 0;
1052
1053 no_bitmap:
1054 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1055 return 0;
1056}
1057
1058
1059static struct supertype *match_metadata_desc1(char *arg)
1060{
1061 struct supertype *st = malloc(sizeof(*st));
1062 if (!st) return st;
1063
1064 st->ss = &super1;
ea329559 1065 st->max_devs = 384;
82d9eba6 1066 if (strcmp(arg, "1") == 0 ||
576d6d83
NB
1067 strcmp(arg, "1.0") == 0 ||
1068 strcmp(arg, "default/large") == 0) {
82d9eba6
NB
1069 st->minor_version = 0;
1070 return st;
1071 }
1072 if (strcmp(arg, "1.1") == 0) {
1073 st->minor_version = 1;
1074 return st;
1075 }
1076 if (strcmp(arg, "1.2") == 0) {
1077 st->minor_version = 2;
1078 return st;
1079 }
1080
1081 free(st);
1082 return NULL;
1083}
1084
34163fc7
NB
1085/* find available size on device with this devsize, using
1086 * superblock type st, and reserving 'reserve' sectors for
1087 * a possible bitmap
1088 */
1bf4e2d9 1089static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
1090{
1091 if (devsize < 24)
1092 return 0;
1093
21e92547 1094 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
199171a2 1095 * if bigger than 200Gig, save 128k
21e92547 1096 */
199171a2 1097 if (devsize-64*2 >= 200*1024*1024*2)
21e92547 1098 devsize -= 128*2;
199171a2 1099 else if (devsize >= 8*1024*1024*2)
21e92547
NB
1100 devsize -= 64*2;
1101
34163fc7
NB
1102 switch(st->minor_version) {
1103 case 0:
1bf4e2d9
NB
1104 /* at end */
1105 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1106 case 1:
1bf4e2d9
NB
1107 /* at start, 4K for superblock and possible bitmap */
1108 return devsize - 4*2;
34163fc7 1109 case 2:
1bf4e2d9
NB
1110 /* 4k from start, 4K for superblock and possible bitmap */
1111 return devsize - (4+4)*2;
34163fc7
NB
1112 }
1113 return 0;
1114}
1115
1bf4e2d9
NB
1116static int
1117add_internal_bitmap1(struct supertype *st, void *sbv,
199171a2
NB
1118 int *chunkp, int delay, int write_behind,
1119 unsigned long long size,
21e92547 1120 int may_change, int major)
34163fc7
NB
1121{
1122 /*
1bf4e2d9
NB
1123 * If not may_change, then this is a 'Grow', and the bitmap
1124 * must fit after the superblock.
1125 * If may_change, then this is create, and we can put the bitmap
1126 * before the superblock if we like, or may move the start.
199171a2
NB
1127 * If !may_change, the bitmap MUST live at offset of 1K, until
1128 * we get a sysfs interface.
34163fc7 1129 *
f9c25f1d 1130 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1131 */
1132
1bf4e2d9 1133 unsigned long long bits;
199171a2 1134 unsigned long long max_bits;
34163fc7 1135 unsigned long long min_chunk;
199171a2
NB
1136 long offset;
1137 int chunk = *chunkp;
1138 int room;
34163fc7
NB
1139 struct mdp_superblock_1 *sb = sbv;
1140 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1141
199171a2
NB
1142 switch(st->minor_version) {
1143 case 0:
1144 /* either 3K after the superblock, or some amount of space
1145 * before.
1146 */
1147 if (may_change) {
1148 /* We are creating array, so we *know* how much room has
1149 * been left.
1150 */
1151 offset = 0;
1152 if (__le64_to_cpu(sb->size) >= 200*1024*1024*2)
1153 room = 128*2;
1154 else if (__le64_to_cpu(sb->size) > 8*1024*1024*2)
1155 room = 64*2;
1156 else {
1157 room = 3*2;
1158 offset = 2;
1159 }
1160 } else {
1161 room = __le64_to_cpu(sb->super_offset)
1162 - __le64_to_cpu(sb->data_offset)
1163 - __le64_to_cpu(sb->data_size);
1164 /* remove '1 ||' when we can set offset via sysfs */
1165 if (1 || (room < 3*2 &&
1166 __le32_to_cpu(sb->max_dev) <= 384)) {
1167 room = 3*2;
1168 offset = 1*2;
1169 } else {
1170 offset = 0; /* means movable offset */
1171 }
1172 }
1173 break;
1174 case 1:
1175 case 2: /* between superblock and data */
1176 if (may_change) {
1177 offset = 4*2;
1178 if (__le64_to_cpu(sb->size) >= 200*1024*1024*2)
1179 room = 128*2;
1180 else if (__le64_to_cpu(sb->size) > 8*1024*1024*2)
1181 room = 64*2;
1182 else
1183 room = 3*2;
1184 } else {
1185 room = __le64_to_cpu(sb->data_offset)
1186 - __le64_to_cpu(sb->super_offset);
1187 if (1 || __le32_to_cpu(sb->max_dev) <= 384) {
1188 room -= 2;
1189 offset = 2;
1190 } else {
1191 room -= 4*2;
1192 offset = 4*2;
1193 }
1194 }
1195 break;
1196 }
1bf4e2d9 1197
199171a2
NB
1198 if (chunk == UnSet && room > 128*2)
1199 /* Limit to 128K of bitmap when chunk size not requested */
1200 room = 128*2;
1bf4e2d9 1201
199171a2 1202 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1203
1204 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1205 bits = (size*512)/min_chunk +1;
34163fc7
NB
1206 while (bits > max_bits) {
1207 min_chunk *= 2;
1208 bits = (bits+1)/2;
1209 }
1210 if (chunk == UnSet)
1211 chunk = min_chunk;
1212 else if (chunk < min_chunk)
1213 return 0; /* chunk size too small */
21e92547
NB
1214 if (chunk == 0) /* rounding problem */
1215 return 0;
34163fc7 1216
199171a2
NB
1217 if (offset == 0) {
1218 bits = (size*512) / chunk + 1;
1219 room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512;
1220 offset = -room;
1221 }
1222
1223 sb->bitmap_offset = __cpu_to_le32(offset);
34163fc7
NB
1224
1225 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1226 memset(bms, 0, sizeof(*bms));
34163fc7 1227 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1228 bms->version = __cpu_to_le32(major);
34163fc7
NB
1229 uuid_from_super1((int*)bms->uuid, sb);
1230 bms->chunksize = __cpu_to_le32(chunk);
1231 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1232 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1233 bms->write_behind = __cpu_to_le32(write_behind);
1234
199171a2 1235 *chunkp = chunk;
34163fc7
NB
1236 return 1;
1237}
1238
1239
14263cf1 1240static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 1241{
34163fc7 1242 unsigned long long offset;
1e0d770c
NB
1243 struct mdp_superblock_1 *sb;
1244 int mustfree = 0;
34163fc7 1245
1e0d770c
NB
1246 if (!sbv) {
1247 if (st->ss->load_super(st, fd, &sbv, NULL))
f6d75de8 1248 return; /* no error I hope... */
1e0d770c
NB
1249 mustfree = 1;
1250 }
1251 sb = sbv;
e478dc86 1252
1bf4e2d9
NB
1253 offset = __le64_to_cpu(sb->super_offset);
1254 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1255 if (mustfree)
f6d75de8 1256 free(sb);
1bf4e2d9 1257 lseek64(fd, offset<<9, 0);
34163fc7
NB
1258}
1259
14263cf1 1260static int write_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7
NB
1261{
1262 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 1263 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1264 int rv = 0;
1265
1266 int towrite, n;
1267 char buf[4096];
1268
f6d75de8 1269 locate_bitmap1(st, fd, sbv);
34163fc7 1270
1e0d770c
NB
1271 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1272 sizeof(bitmap_super_t))
1273 return -2;
1bf4e2d9
NB
1274 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1275 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1276 memset(buf, 0xff, sizeof(buf));
1277 while (towrite > 0) {
1278 n = towrite;
1279 if (n > sizeof(buf))
1280 n = sizeof(buf);
1281 n = write(fd, buf, n);
1282 if (n > 0)
1283 towrite -= n;
1284 else
1285 break;
1286 }
1287 fsync(fd);
1288 if (towrite)
1289 rv = -2;
1290
1291 return rv;
82d9eba6
NB
1292}
1293
1294struct superswitch super1 = {
c7654afc 1295#ifndef MDASSEMBLE
82d9eba6
NB
1296 .examine_super = examine_super1,
1297 .brief_examine_super = brief_examine_super1,
1298 .detail_super = detail_super1,
1299 .brief_detail_super = brief_detail_super1,
c7654afc 1300#endif
83b6208e 1301 .match_home = match_home1,
82d9eba6
NB
1302 .uuid_from_super = uuid_from_super1,
1303 .getinfo_super = getinfo_super1,
1304 .update_super = update_super1,
82d9eba6
NB
1305 .init_super = init_super1,
1306 .add_to_super = add_to_super1,
1307 .store_super = store_super1,
1308 .write_init_super = write_init_super1,
1309 .compare_super = compare_super1,
1310 .load_super = load_super1,
1311 .match_metadata_desc = match_metadata_desc1,
1312 .avail_size = avail_size1,
34163fc7
NB
1313 .add_internal_bitmap = add_internal_bitmap1,
1314 .locate_bitmap = locate_bitmap1,
1315 .write_bitmap = write_bitmap1,
82d9eba6 1316 .major = 1,
f277ce36
NB
1317#if __BYTE_ORDER == BIG_ENDIAN
1318 .swapuuid = 0,
1319#else
1320 .swapuuid = 1,
1321#endif
82d9eba6 1322};