Make --examine report chunk size when it should
[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;
137 return csum;
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;
82d9eba6
NB
150
151 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
91eedefc
NB
152 printf(" Version : %02d\n", 1);
153 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
154 printf(" Array UUID : ");
155 for (i=0; i<16; i++) {
82d9eba6 156 if ((i&3)==0 && i != 0) printf(":");
34163fc7 157 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
158 }
159 printf("\n");
a1cbd7d0
NB
160 printf(" Name : %.32s", sb->set_name);
161 if (l > 0 && l < 32 &&
162 sb->set_name[l] == ':' &&
163 strncmp(sb->set_name, homehost, l) == 0)
164 printf(" (local to host %s)", homehost);
165 printf("\n");
82d9eba6
NB
166 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
167 printf(" Creation Time : %.24s\n", ctime(&atime));
168 c=map_num(pers, __le32_to_cpu(sb->level));
169 printf(" Raid Level : %s\n", c?c:"-unknown-");
170 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
171 printf("\n");
570c0542 172 printf(" Device Size : %llu%s\n", (unsigned long long)sb->data_size, human_size(sb->data_size<<9));
b674b5b8
NB
173 if (__le32_to_cpu(sb->level) >= 0) {
174 int ddsks=0;
175 switch(__le32_to_cpu(sb->level)) {
176 case 1: ddsks=1;break;
177 case 4:
178 case 5: ddsks = sb->raid_disks-1; break;
179 case 6: ddsks = sb->raid_disks-2; break;
180 case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255);
181 }
182 if (ddsks)
183 printf(" Array Size : %llu%s\n", ddsks*(unsigned long long)sb->size, human_size(ddsks*sb->size<<9));
184 if (sb->size != sb->data_size)
185 printf(" Used Size : %llu%s\n", (unsigned long long)sb->size, human_size(sb->size<<9));
186 }
82d9eba6 187 if (sb->data_offset)
570c0542 188 printf(" Data Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->data_offset));
82d9eba6 189 if (sb->super_offset)
570c0542 190 printf(" Super Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
191 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
192 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
a3fd117c 193 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
194 printf(" Device UUID : ");
195 for (i=0; i<16; i++) {
82d9eba6 196 if ((i&3)==0 && i != 0) printf(":");
34163fc7 197 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
198 }
199 printf("\n");
b674b5b8 200 printf("\n");
91eedefc
NB
201 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
202 printf("Internal Bitmap : %ld sectors from superblock\n",
8fac0577
NB
203 (long)__le32_to_cpu(sb->bitmap_offset));
204 }
b674b5b8 205 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 206 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
207 human_size(__le64_to_cpu(sb->reshape_position)<<9));
208 if (__le32_to_cpu(sb->delta_disks)) {
209 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
210 if (__le32_to_cpu(sb->delta_disks))
211 printf(" (%d->%d)\n",
212 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
213 __le32_to_cpu(sb->raid_disks));
214 else
215 printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks),
216 __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks));
217 }
218 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
219 c = map_num(pers, __le32_to_cpu(sb->new_level));
220 printf(" New Level : %s\n", c?c:"-unknown-");
221 }
222 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
223 if (__le32_to_cpu(sb->level) == 5) {
224 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
225 printf(" New Layout : %s\n", c?c:"-unknown-");
226 }
227 if (__le32_to_cpu(sb->level) == 10) {
b578481c 228 printf(" New Layout : near=%d, %s=%d\n",
b674b5b8 229 __le32_to_cpu(sb->new_layout)&255,
b578481c 230 (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far",
b674b5b8
NB
231 (__le32_to_cpu(sb->new_layout)>>8)&255);
232 }
233 }
234 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
235 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
236 printf("\n");
237 }
dfd4d8ee
NB
238 if (sb->devflags) {
239 printf(" Flags :");
34163fc7 240 if (sb->devflags & WriteMostly1)
dfd4d8ee
NB
241 printf(" write-mostly");
242 printf("\n");
243 }
82d9eba6
NB
244
245 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
246 printf(" Update Time : %.24s\n", ctime(&atime));
247
248 if (calc_sb_1_csum(sb) == sb->sb_csum)
249 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
250 else
251 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
252 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 253 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
254 printf("\n");
255 if (__le32_to_cpu(sb->level) == 5) {
256 c = map_num(r5layout, __le32_to_cpu(sb->layout));
257 printf(" Layout : %s\n", c?c:"-unknown-");
258 }
265e0f17
NB
259 if (__le32_to_cpu(sb->level) == 10) {
260 int lo = __le32_to_cpu(sb->layout);
b578481c
NB
261 printf(" Layout : near=%d, %s=%d\n",
262 lo&255,
263 (lo&0x10000)?"offset":"far",
264 (lo>>8)&255);
265e0f17 265 }
82d9eba6
NB
266 switch(__le32_to_cpu(sb->level)) {
267 case 0:
268 case 4:
269 case 5:
d2cd3ffc
NB
270 case 6:
271 case 10:
b674b5b8 272 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
273 break;
274 case -1:
b674b5b8 275 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
276 break;
277 default: break;
278 }
279 printf("\n");
c3684618
NB
280 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
281 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
282 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
283 break;
284 for (d=0; d < i; d++) {
285 int role = __le16_to_cpu(sb->dev_roles[d]);
286 if (d) printf(", ");
287 if (role == 0xffff) printf("empty");
288 else if(role == 0xfffe) printf("failed");
289 else printf("%d", role);
290 }
291 printf(")\n");
82d9eba6
NB
292 printf(" Array State : ");
293 for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) {
294 int cnt = 0;
295 int me = 0;
296 int i;
297 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
298 int role = __le16_to_cpu(sb->dev_roles[i]);
299 if (role == d) {
300 if (i == __le32_to_cpu(sb->dev_number))
301 me = 1;
302 cnt++;
303 }
304 }
305 if (cnt > 1) printf("?");
306 else if (cnt == 1 && me) printf("U");
307 else if (cnt == 1) printf("u");
308 else printf ("_");
309 }
6fbba4c9 310 faulty = 0;
82d9eba6
NB
311 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
312 int role = __le16_to_cpu(sb->dev_roles[i]);
6fbba4c9
NB
313 if (role == 0xFFFE)
314 faulty++;
82d9eba6 315 }
82d9eba6
NB
316 if (faulty) printf(" %d failed", faulty);
317 printf("\n");
318}
319
320
321static void brief_examine_super1(void *sbv)
322{
323 struct mdp_superblock_1 *sb = sbv;
324 int i;
2998bc01 325 char *nm;
b015e6c2 326 char *c=map_num(pers, __le32_to_cpu(sb->level));
2998bc01
NB
327
328 nm = strchr(sb->set_name, ':');
329 if (nm)
330 nm++;
331 else if (sb->set_name[0])
332 nm = sb->set_name;
333 else
334 nm = "??";
82d9eba6 335
2998bc01
NB
336 printf("ARRAY /dev/md/%s level=%s metadata=1 num-devices=%d UUID=",
337 nm,
82d9eba6
NB
338 c?c:"-unknown-", sb->raid_disks);
339 for (i=0; i<16; i++) {
340 printf("%02x", sb->set_uuid[i]);
341 if ((i&3)==0 && i != 0) printf(":");
342 }
947fd4dd
NB
343 if (sb->set_name[0])
344 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
345 printf("\n");
346}
347
b6750aa8 348static void detail_super1(void *sbv, char *homehost)
82d9eba6
NB
349{
350 struct mdp_superblock_1 *sb = sbv;
351 int i;
b6750aa8 352 int l = homehost ? strlen(homehost) : 0;
82d9eba6 353
b6750aa8
NB
354 printf(" Name : %.32s", sb->set_name);
355 if (l > 0 && l < 32 &&
356 sb->set_name[l] == ':' &&
357 strncmp(sb->set_name, homehost, l) == 0)
358 printf(" (local to host %s)", homehost);
359 printf("\n UUID : ");
82d9eba6 360 for (i=0; i<16; i++) {
82d9eba6 361 if ((i&3)==0 && i != 0) printf(":");
34163fc7 362 printf("%02x", sb->set_uuid[i]);
82d9eba6 363 }
570c0542 364 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
365}
366
367static void brief_detail_super1(void *sbv)
368{
369 struct mdp_superblock_1 *sb = sbv;
370 int i;
371
947fd4dd
NB
372 if (sb->set_name[0])
373 printf(" name=%.32s", sb->set_name);
82d9eba6
NB
374 printf(" UUID=");
375 for (i=0; i<16; i++) {
82d9eba6 376 if ((i&3)==0 && i != 0) printf(":");
34163fc7 377 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
378 }
379}
380
c7654afc
NB
381#endif
382
83b6208e
NB
383static int match_home1(void *sbv, char *homehost)
384{
385 struct mdp_superblock_1 *sb = sbv;
386 int l = homehost ? strlen(homehost) : 0;
387
388 return (l > 0 && l < 32 &&
389 sb->set_name[l] == ':' &&
390 strncmp(sb->set_name, homehost, l) == 0);
391}
392
82d9eba6
NB
393static void uuid_from_super1(int uuid[4], void * sbv)
394{
395 struct mdp_superblock_1 *super = sbv;
396 char *cuuid = (char*)uuid;
397 int i;
398 for (i=0; i<16; i++)
399 cuuid[i] = super->set_uuid[i];
400}
401
31317663 402static void getinfo_super1(struct mdinfo *info, void *sbv)
82d9eba6
NB
403{
404 struct mdp_superblock_1 *sb = sbv;
405 int working = 0;
406 int i;
407 int role;
408
409 info->array.major_version = 1;
410 info->array.minor_version = __le32_to_cpu(sb->feature_map);
411 info->array.patch_version = 0;
412 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
413 info->array.level = __le32_to_cpu(sb->level);
265e0f17 414 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
415 info->array.md_minor = -1;
416 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 417 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 418 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
353632d9
NB
419
420 info->data_offset = __le64_to_cpu(sb->data_offset);
421 info->component_size = __le64_to_cpu(sb->size);
82d9eba6
NB
422
423 info->disk.major = 0;
424 info->disk.minor = 0;
fbf8a0b7 425 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6
NB
426 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
427 __le32_to_cpu(sb->max_dev) > 512)
428 role = 0xfffe;
429 else
430 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
431
432 info->disk.raid_disk = -1;
433 switch(role) {
434 case 0xFFFF:
435 info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */
436 break;
437 case 0xFFFE:
438 info->disk.state = 1; /* faulty */
439 break;
440 default:
441 info->disk.state = 6; /* active and in sync */
442 info->disk.raid_disk = role;
443 }
444 info->events = __le64_to_cpu(sb->events);
445
446 memcpy(info->uuid, sb->set_uuid, 16);
447
31317663
NB
448 strncpy(info->name, sb->set_name, 32);
449 info->name[32] = 0;
947fd4dd 450
353632d9
NB
451 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
452 info->reshape_active = 1;
453 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
454 info->new_level = __le32_to_cpu(sb->new_level);
455 info->delta_disks = __le32_to_cpu(sb->delta_disks);
456 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 457 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
353632d9
NB
458 } else
459 info->reshape_active = 0;
460
82d9eba6
NB
461 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
462 role = __le16_to_cpu(sb->dev_roles[i]);
fbf8a0b7 463 if (/*role == 0xFFFF || */role < info->array.raid_disks)
82d9eba6
NB
464 working++;
465 }
466
467 info->array.working_disks = working;
468}
469
e5eac01f
NB
470static int update_super1(struct mdinfo *info, void *sbv, char *update,
471 char *devname, int verbose,
472 int uuid_set, char *homehost)
82d9eba6 473{
c4d831e1
NB
474 /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made.
475 * For others, the return value is ignored.
476 */
82d9eba6
NB
477 int rv = 0;
478 struct mdp_superblock_1 *sb = sbv;
479
480 if (strcmp(update, "force")==0) {
c4d831e1
NB
481 if (sb->events != __cpu_to_le64(info->events))
482 rv = 1;
6d3d5804 483 sb->events = __cpu_to_le64(info->events);
82d9eba6
NB
484 switch(__le32_to_cpu(sb->level)) {
485 case 5: case 4: case 6:
486 /* need to force clean */
c4d831e1
NB
487 if (sb->resync_offset != ~0ULL)
488 rv = 1;
82d9eba6
NB
489 sb->resync_offset = ~0ULL;
490 }
491 }
492 if (strcmp(update, "assemble")==0) {
493 int d = info->disk.number;
494 int want;
495 if (info->disk.state == 6)
496 want = __cpu_to_le32(info->disk.raid_disk);
497 else
498 want = 0xFFFF;
499 if (sb->dev_roles[d] != want) {
500 sb->dev_roles[d] = want;
501 rv = 1;
502 }
503 }
504#if 0
505 if (strcmp(update, "newdev") == 0) {
506 int d = info->disk.number;
507 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
508 sb->disks[d].number = d;
509 sb->disks[d].major = info->disk.major;
510 sb->disks[d].minor = info->disk.minor;
511 sb->disks[d].raid_disk = info->disk.raid_disk;
512 sb->disks[d].state = info->disk.state;
513 sb->this_disk = sb->disks[d];
514 }
515#endif
516 if (strcmp(update, "grow") == 0) {
517 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
518 /* FIXME */
519 }
520 if (strcmp(update, "resync") == 0) {
521 /* make sure resync happens */
522 sb->resync_offset = ~0ULL;
523 }
14263cf1 524 if (strcmp(update, "uuid") == 0) {
838acbc2 525 memcpy(sb->set_uuid, info->uuid, 16);
14263cf1
NB
526 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
527 struct bitmap_super_s *bm;
528 bm = (struct bitmap_super_s*)(sbv+1024);
529 memcpy(bm->uuid, info->uuid, 16);
530 }
531 }
0237e0ca
NB
532 if (strcmp(update, "homehost") == 0 &&
533 homehost) {
534 char *c;
535 update = "name";
536 c = strchr(sb->set_name, ':');
537 if (c)
538 strncpy(info->name, c+1, 31 - (c-sb->set_name));
539 else
540 strncpy(info->name, sb->set_name, 32);
541 info->name[33] = 0;
542 }
c4f12c13
NB
543 if (strcmp(update, "name") == 0) {
544 if (info->name[0] == 0)
545 sprintf(info->name, "%d", info->array.md_minor);
546 memset(sb->set_name, 0, sizeof(sb->set_name));
547 if (homehost &&
548 strchr(info->name, ':') == NULL &&
549 strlen(homehost)+1+strlen(info->name) < 32) {
550 strcpy(sb->set_name, homehost);
551 strcat(sb->set_name, ":");
552 strcat(sb->set_name, info->name);
553 } else
554 strcpy(sb->set_name, info->name);
555 }
353632d9
NB
556 if (strcmp(update, "_reshape_progress")==0)
557 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
558
559 sb->sb_csum = calc_sb_1_csum(sb);
560 return rv;
561}
562
563
564static __u64 event_super1(void *sbv)
565{
566 struct mdp_superblock_1 *sb = sbv;
567 return __le64_to_cpu(sb->events);
568}
569
05697ec1
NB
570static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
571 unsigned long long size, char *name, char *homehost)
82d9eba6 572{
34163fc7 573 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 574 int spares;
34163fc7 575 int rfd;
05697ec1 576 char defname[10];
82d9eba6
NB
577 memset(sb, 0, 1024);
578
05697ec1 579 if (info->major_version == -1) {
82d9eba6 580 /* zeroing superblock */
05697ec1 581 *sbp = sb;
82d9eba6 582 return 0;
05697ec1 583 }
82d9eba6
NB
584
585 spares = info->working_disks - info->active_disks;
586 if (info->raid_disks + spares > 384) {
587 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
588 info->raid_disks , spares, 384);
589 return 0;
590 }
591
82d9eba6
NB
592 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
593 sb->major_version = __cpu_to_le32(1);
594 sb->feature_map = 0;
595 sb->pad0 = 0;
34163fc7
NB
596
597 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
598 read(rfd, sb->set_uuid, 16) != 16) {
599 *(__u32*)(sb->set_uuid) = random();
600 *(__u32*)(sb->set_uuid+4) = random();
601 *(__u32*)(sb->set_uuid+8) = random();
602 *(__u32*)(sb->set_uuid+12) = random();
603 }
604 if (rfd >= 0) close(rfd);
82d9eba6 605
05697ec1
NB
606 if (name == NULL || *name == 0) {
607 sprintf(defname, "%d", info->md_minor);
608 name = defname;
609 }
947fd4dd 610 memset(sb->set_name, 0, 32);
05697ec1
NB
611 if (homehost &&
612 strchr(name, ':')== NULL &&
613 strlen(homehost)+1+strlen(name) < 32) {
614 strcpy(sb->set_name, homehost);
615 strcat(sb->set_name, ":");
616 strcat(sb->set_name, name);
617 } else
618 strcpy(sb->set_name, name);
82d9eba6
NB
619
620 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
621 sb->level = __cpu_to_le32(info->level);
ea329559 622 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 623 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
624 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
625 sb->raid_disks = __cpu_to_le32(info->raid_disks);
626
627 sb->data_offset = __cpu_to_le64(0);
628 sb->data_size = __cpu_to_le64(0);
629 sb->super_offset = __cpu_to_le64(0);
630 sb->recovery_offset = __cpu_to_le64(0);
631
632 sb->utime = sb->ctime;
633 sb->events = __cpu_to_le64(1);
34321279 634 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
635 sb->resync_offset = ~0ULL;
636 else
637 sb->resync_offset = 0;
34163fc7 638 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
639 sizeof(sb->dev_roles[0]));
640 memset(sb->pad3, 0, sizeof(sb->pad3));
641
642 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
643
644 *sbp = sb;
645 return 1;
646}
647
648/* Add a device to the superblock being created */
649static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
650{
651 struct mdp_superblock_1 *sb = sbv;
652 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 653 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 654 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 655 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 656 *rp = 0xffff;
34163fc7 657 else
82d9eba6
NB
658 *rp = 0xfffe;
659}
660
14263cf1
NB
661static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
662
96395475 663static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
664{
665 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 666 unsigned long long sb_offset;
82d9eba6 667 int sbsize;
5dd497ee
NB
668 unsigned long size;
669 unsigned long long dsize;
96395475 670
5dd497ee
NB
671#ifdef BLKGETSIZE64
672 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
673#endif
674 {
675 if (ioctl(fd, BLKGETSIZE, &size))
676 return 1;
677 else
678 dsize = (unsigned long long)size;
679 } else
680 dsize >>= 9;
96395475 681
5dd497ee 682 if (dsize < 24)
96395475
NB
683 return 2;
684
685 /*
686 * Calculate the position of the superblock.
687 * It is always aligned to a 4K boundary and
688 * depending on minor_version, it can be:
689 * 0: At least 8K, but less than 12K, from end of device
690 * 1: At start of device
691 * 2: 4K from start of device.
692 */
693 switch(st->minor_version) {
694 case 0:
5dd497ee 695 sb_offset = dsize;
96395475
NB
696 sb_offset -= 8*2;
697 sb_offset &= ~(4*2-1);
698 break;
699 case 1:
6fbba4c9 700 sb_offset = 0;
96395475
NB
701 break;
702 case 2:
703 sb_offset = 4*2;
704 break;
705 default:
706 return -EINVAL;
707 }
708
82d9eba6 709
34163fc7 710
6fbba4c9
NB
711 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
712 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
713 ) {
714 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
715 abort();
716 }
82d9eba6 717
6fbba4c9 718 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
719 return 3;
720
721 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
722
723 if (write(fd, sb, sbsize) != sbsize)
724 return 4;
725
14263cf1
NB
726 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
727 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 728 (((char*)sb)+1024);
14263cf1
NB
729 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
730 locate_bitmap1(st, fd, sbv);
9fca7d62
NB
731 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
732 return 5;
14263cf1
NB
733 }
734 }
570c0542 735 fsync(fd);
82d9eba6
NB
736 return 0;
737}
738
892debc8
NB
739static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
740
34163fc7 741static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 742 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
743{
744 struct mdp_superblock_1 *sb = sbv;
e478dc86 745 void *refsbv = NULL;
892debc8 746 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 747 int rfd;
82d9eba6
NB
748 int rv;
749
8fac0577
NB
750 unsigned long size, space;
751 unsigned long long dsize, array_size;
82d9eba6
NB
752 long long sb_offset;
753
754
755 if (fd < 0) {
756 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
757 devname);
758 return -1;
759 }
760
761 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 762 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
dfd4d8ee 763 sb->devflags |= WriteMostly1;
892debc8 764
dfe47e00
NB
765 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
766 read(rfd, sb->device_uuid, 16) != 16) {
767 *(__u32*)(sb->device_uuid) = random();
768 *(__u32*)(sb->device_uuid+4) = random();
769 *(__u32*)(sb->device_uuid+8) = random();
770 *(__u32*)(sb->device_uuid+12) = random();
771 }
772 if (rfd >= 0) close(rfd);
892debc8
NB
773 sb->events = 0;
774
e478dc86
NB
775 if (load_super1(st, fd, &refsbv, NULL)==0) {
776 struct mdp_superblock_1 *refsb = refsbv;
777
892debc8
NB
778 memcpy(sb->device_uuid, refsb->device_uuid, 16);
779 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
780 /* same array, so preserve events and dev_number */
781 sb->events = refsb->events;
7eae7080
NB
782 /* bugs in 2.6.17 and earlier mean the dev_number
783 * chosen in Manage must be preserved
784 */
785 if (get_linux_version() >= 2006018)
786 sb->dev_number = refsb->dev_number;
892debc8
NB
787 }
788 free(refsb);
789 }
34163fc7 790
5dd497ee
NB
791#ifdef BLKGETSIZE64
792 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
793#endif
794 {
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);
82d9eba6
NB
819 switch(st->minor_version) {
820 case 0:
5dd497ee 821 sb_offset = dsize;
82d9eba6
NB
822 sb_offset -= 8*2;
823 sb_offset &= ~(4*2-1);
824 sb->super_offset = __cpu_to_le64(sb_offset);
825 sb->data_offset = __cpu_to_le64(0);
561832f9 826 if (sb_offset-64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
827 sb->data_size = __cpu_to_le64(sb_offset-64*2);
828 else
829 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
830 break;
831 case 1:
832 sb->super_offset = __cpu_to_le64(0);
561832f9 833 if (dsize - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
834 space = 64*2;
835 else
836 space = 4*2;
837 sb->data_offset = __cpu_to_le64(space); /* leave space for super and bitmap */
838 sb->data_size = __cpu_to_le64(dsize - space);
82d9eba6
NB
839 break;
840 case 2:
841 sb_offset = 4*2;
561832f9 842 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
843 space = 64*2;
844 else
845 space = 4*2;
82d9eba6 846 sb->super_offset = __cpu_to_le64(sb_offset);
21e92547
NB
847 sb->data_offset = __cpu_to_le64(sb_offset+space);
848 sb->data_size = __cpu_to_le64(dsize - 4*2 - space);
82d9eba6
NB
849 break;
850 default:
851 return -EINVAL;
852 }
853
854
855 sb->sb_csum = calc_sb_1_csum(sb);
96395475 856 rv = store_super1(st, fd, sb);
82d9eba6
NB
857 if (rv)
858 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 859
1bf4e2d9
NB
860 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
861 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 862 close(fd);
82d9eba6
NB
863 return rv;
864}
865
866static int compare_super1(void **firstp, void *secondv)
867{
868 /*
869 * return:
870 * 0 same, or first was empty, and second was copied
871 * 1 second had wrong number
872 * 2 wrong uuid
873 * 3 wrong other info
874 */
875 struct mdp_superblock_1 *first = *firstp;
876 struct mdp_superblock_1 *second = secondv;
877
878 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
879 return 1;
880 if (second->major_version != __cpu_to_le32(1))
881 return 1;
882
883 if (!first) {
14263cf1
NB
884 first = malloc(1024+sizeof(bitmap_super_t));
885 memcpy(first, second, 1024+sizeof(bitmap_super_t));
82d9eba6
NB
886 *firstp = first;
887 return 0;
888 }
889 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
890 return 2;
891
892 if (first->ctime != second->ctime ||
893 first->level != second->level ||
894 first->layout != second->layout ||
895 first->size != second->size ||
896 first->chunksize != second->chunksize ||
897 first->raid_disks != second->raid_disks)
898 return 3;
899 return 0;
900}
901
902static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
903{
904 unsigned long size;
5dd497ee 905 unsigned long long dsize;
82d9eba6
NB
906 unsigned long long sb_offset;
907 struct mdp_superblock_1 *super;
14263cf1
NB
908 int uuid[4];
909 struct bitmap_super_s *bsb;
82d9eba6
NB
910
911
912 if (st->ss == NULL) {
570c0542
NB
913 int bestvers = -1;
914 __u64 bestctime = 0;
915 /* guess... choose latest ctime */
82d9eba6
NB
916 st->ss = &super1;
917 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
918 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
919 case 0: super = *sbp;
920 if (bestvers == -1 ||
921 bestctime < __le64_to_cpu(super->ctime)) {
922 bestvers = st->minor_version;
923 bestctime = __le64_to_cpu(super->ctime);
924 }
925 free(super);
926 *sbp = NULL;
927 break;
82d9eba6
NB
928 case 1: st->ss = NULL; return 1; /*bad device */
929 case 2: break; /* bad, try next */
930 }
931 }
570c0542
NB
932 if (bestvers != -1) {
933 int rv;
934 st->minor_version = bestvers;
935 st->ss = &super1;
ea329559 936 st->max_devs = 384;
570c0542
NB
937 rv = load_super1(st, fd, sbp, devname);
938 if (rv) st->ss = NULL;
939 return rv;
940 }
82d9eba6
NB
941 st->ss = NULL;
942 return 2;
943 }
5dd497ee
NB
944#ifdef BLKGETSIZE64
945 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
946#endif
947 {
948 if (ioctl(fd, BLKGETSIZE, &size)) {
949 if (devname)
950 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
951 devname, strerror(errno));
952 return 1;
953 }
954 dsize = size;
955 } else
956 dsize >>= 9;
82d9eba6 957
5dd497ee 958 if (dsize < 24) {
82d9eba6 959 if (devname)
5dd497ee
NB
960 fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n",
961 devname, dsize);
82d9eba6
NB
962 return 1;
963 }
964
965 /*
966 * Calculate the position of the superblock.
967 * It is always aligned to a 4K boundary and
14263cf1 968 * depending on minor_version, it can be:
82d9eba6
NB
969 * 0: At least 8K, but less than 12K, from end of device
970 * 1: At start of device
971 * 2: 4K from start of device.
972 */
973 switch(st->minor_version) {
974 case 0:
5dd497ee 975 sb_offset = dsize;
82d9eba6
NB
976 sb_offset -= 8*2;
977 sb_offset &= ~(4*2-1);
978 break;
979 case 1:
980 sb_offset = 0;
981 break;
982 case 2:
983 sb_offset = 4*2;
984 break;
985 default:
986 return -EINVAL;
987 }
988
989 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
990
991
992 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
993 if (devname)
994 fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
995 devname, strerror(errno));
996 return 1;
997 }
998
34163fc7 999 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
1000
1001 if (read(fd, super, 1024) != 1024) {
1002 if (devname)
1003 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1004 devname);
1005 free(super);
1006 return 1;
1007 }
1008
1009 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1010 if (devname)
1011 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1012 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1013 free(super);
1014 return 2;
1015 }
1016
1017 if (__le32_to_cpu(super->major_version) != 1) {
1018 if (devname)
1019 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1020 devname, __le32_to_cpu(super->major_version));
1021 free(super);
1022 return 2;
1023 }
1024 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1025 if (devname)
1026 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1027 devname);
1028 free(super);
1029 return 2;
1030 }
1031 *sbp = super;
14263cf1
NB
1032
1033 /* Now check on the bitmap superblock */
1034 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1035 return 0;
1036 /* Read the bitmap superblock and make sure it looks
1037 * valid. If it doesn't clear the bit. An --assemble --force
1038 * should get that written out.
1039 */
1040 locate_bitmap1(st, fd, super);
1041 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1042 != sizeof(struct bitmap_super_s))
1043 goto no_bitmap;
1044
1045 uuid_from_super1(uuid, super);
1046 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1047 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1048 memcmp(bsb->uuid, uuid, 16) != 0)
1049 goto no_bitmap;
1050 return 0;
1051
1052 no_bitmap:
1053 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1054 return 0;
1055}
1056
1057
1058static struct supertype *match_metadata_desc1(char *arg)
1059{
1060 struct supertype *st = malloc(sizeof(*st));
1061 if (!st) return st;
1062
1063 st->ss = &super1;
ea329559 1064 st->max_devs = 384;
82d9eba6 1065 if (strcmp(arg, "1") == 0 ||
576d6d83
NB
1066 strcmp(arg, "1.0") == 0 ||
1067 strcmp(arg, "default/large") == 0) {
82d9eba6
NB
1068 st->minor_version = 0;
1069 return st;
1070 }
1071 if (strcmp(arg, "1.1") == 0) {
1072 st->minor_version = 1;
1073 return st;
1074 }
1075 if (strcmp(arg, "1.2") == 0) {
1076 st->minor_version = 2;
1077 return st;
1078 }
1079
1080 free(st);
1081 return NULL;
1082}
1083
34163fc7
NB
1084/* find available size on device with this devsize, using
1085 * superblock type st, and reserving 'reserve' sectors for
1086 * a possible bitmap
1087 */
1bf4e2d9 1088static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
1089{
1090 if (devsize < 24)
1091 return 0;
1092
21e92547
NB
1093 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1094 * if biffer than 200Gig, save 128k
1095 */
1096 if (devsize > 200*1024*1024*2)
1097 devsize -= 128*2;
1098 else if (devsize > 8*1024*1024*2)
1099 devsize -= 64*2;
1100
34163fc7
NB
1101 switch(st->minor_version) {
1102 case 0:
1bf4e2d9
NB
1103 /* at end */
1104 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1105 case 1:
1bf4e2d9
NB
1106 /* at start, 4K for superblock and possible bitmap */
1107 return devsize - 4*2;
34163fc7 1108 case 2:
1bf4e2d9
NB
1109 /* 4k from start, 4K for superblock and possible bitmap */
1110 return devsize - (4+4)*2;
34163fc7
NB
1111 }
1112 return 0;
1113}
1114
1bf4e2d9
NB
1115static int
1116add_internal_bitmap1(struct supertype *st, void *sbv,
21e92547
NB
1117 int chunk, int delay, int write_behind, unsigned long long size,
1118 int may_change, int major)
34163fc7
NB
1119{
1120 /*
1bf4e2d9
NB
1121 * If not may_change, then this is a 'Grow', and the bitmap
1122 * must fit after the superblock.
1123 * If may_change, then this is create, and we can put the bitmap
1124 * before the superblock if we like, or may move the start.
1125 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7 1126 *
f9c25f1d 1127 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1128 */
1129
1bf4e2d9
NB
1130 unsigned long long bits;
1131 unsigned long long max_bits = (3*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1132 unsigned long long min_chunk;
1133 struct mdp_superblock_1 *sb = sbv;
1134 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1135
1bf4e2d9
NB
1136 if (st->minor_version && !may_change &&
1137 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
1138 return 0; /* doesn't fit */
1139
1140
34163fc7
NB
1141
1142 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1143 bits = (size*512)/min_chunk +1;
34163fc7
NB
1144 while (bits > max_bits) {
1145 min_chunk *= 2;
1146 bits = (bits+1)/2;
1147 }
1148 if (chunk == UnSet)
1149 chunk = min_chunk;
1150 else if (chunk < min_chunk)
1151 return 0; /* chunk size too small */
21e92547
NB
1152 if (chunk == 0) /* rounding problem */
1153 return 0;
34163fc7 1154
1bf4e2d9 1155 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
1156
1157 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1158 memset(bms, 0, sizeof(*bms));
34163fc7 1159 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1160 bms->version = __cpu_to_le32(major);
34163fc7
NB
1161 uuid_from_super1((int*)bms->uuid, sb);
1162 bms->chunksize = __cpu_to_le32(chunk);
1163 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1164 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1165 bms->write_behind = __cpu_to_le32(write_behind);
1166
34163fc7
NB
1167 return 1;
1168}
1169
1170
14263cf1 1171static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 1172{
34163fc7 1173 unsigned long long offset;
1e0d770c
NB
1174 struct mdp_superblock_1 *sb;
1175 int mustfree = 0;
34163fc7 1176
1e0d770c
NB
1177 if (!sbv) {
1178 if (st->ss->load_super(st, fd, &sbv, NULL))
f6d75de8 1179 return; /* no error I hope... */
1e0d770c
NB
1180 mustfree = 1;
1181 }
1182 sb = sbv;
e478dc86 1183
1bf4e2d9
NB
1184 offset = __le64_to_cpu(sb->super_offset);
1185 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1186 if (mustfree)
f6d75de8 1187 free(sb);
1bf4e2d9 1188 lseek64(fd, offset<<9, 0);
34163fc7
NB
1189}
1190
14263cf1 1191static int write_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7
NB
1192{
1193 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 1194 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1195 int rv = 0;
1196
1197 int towrite, n;
1198 char buf[4096];
1199
f6d75de8 1200 locate_bitmap1(st, fd, sbv);
34163fc7 1201
1e0d770c
NB
1202 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1203 sizeof(bitmap_super_t))
1204 return -2;
1bf4e2d9
NB
1205 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1206 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1207 memset(buf, 0xff, sizeof(buf));
1208 while (towrite > 0) {
1209 n = towrite;
1210 if (n > sizeof(buf))
1211 n = sizeof(buf);
1212 n = write(fd, buf, n);
1213 if (n > 0)
1214 towrite -= n;
1215 else
1216 break;
1217 }
1218 fsync(fd);
1219 if (towrite)
1220 rv = -2;
1221
1222 return rv;
82d9eba6
NB
1223}
1224
1225struct superswitch super1 = {
c7654afc 1226#ifndef MDASSEMBLE
82d9eba6
NB
1227 .examine_super = examine_super1,
1228 .brief_examine_super = brief_examine_super1,
1229 .detail_super = detail_super1,
1230 .brief_detail_super = brief_detail_super1,
c7654afc 1231#endif
83b6208e 1232 .match_home = match_home1,
82d9eba6
NB
1233 .uuid_from_super = uuid_from_super1,
1234 .getinfo_super = getinfo_super1,
1235 .update_super = update_super1,
1236 .event_super = event_super1,
1237 .init_super = init_super1,
1238 .add_to_super = add_to_super1,
1239 .store_super = store_super1,
1240 .write_init_super = write_init_super1,
1241 .compare_super = compare_super1,
1242 .load_super = load_super1,
1243 .match_metadata_desc = match_metadata_desc1,
1244 .avail_size = avail_size1,
34163fc7
NB
1245 .add_internal_bitmap = add_internal_bitmap1,
1246 .locate_bitmap = locate_bitmap1,
1247 .write_bitmap = write_bitmap1,
82d9eba6 1248 .major = 1,
f277ce36
NB
1249#if __BYTE_ORDER == BIG_ENDIAN
1250 .swapuuid = 0,
1251#else
1252 .swapuuid = 1,
1253#endif
82d9eba6 1254};