]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super0.c
DDF: compare_super_ddf: merge local info of other superblock
[thirdparty/mdadm.git] / super0.c
CommitLineData
4b1ac34b
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
4b1ac34b
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
e736b623 22 * Email: <neilb@suse.de>
4b1ac34b
NB
23 */
24
8268ed74 25#define HAVE_STDINT_H 1
4b1ac34b 26#include "mdadm.h"
8268ed74 27#include "sha1.h"
4b1ac34b
NB
28/*
29 * All handling for the 0.90.0 version superblock is in
30 * this file.
31 * This includes:
32 * - finding, loading, and writing the superblock.
33 * - initialising a new superblock
34 * - printing the superblock for --examine
35 * - printing part of the superblock for --detail
aba69144 36 * .. other stuff
4b1ac34b
NB
37 */
38
39
40static unsigned long calc_sb0_csum(mdp_super_t *super)
41{
42 unsigned long csum = super->sb_csum;
43 unsigned long newcsum;
44 super->sb_csum= 0 ;
45 newcsum = calc_csum(super, MD_SB_BYTES);
46 super->sb_csum = csum;
47 return newcsum;
48}
49
586ed405 50
ec9688ca 51static void super0_swap_endian(struct mdp_superblock_s *sb)
586ed405
NB
52{
53 /* as super0 superblocks are host-endian, it is sometimes
aba69144 54 * useful to be able to swap the endianness
586ed405
NB
55 * as (almost) everything is u32's we byte-swap every 4byte
56 * number.
57 * We then also have to swap the events_hi and events_lo
58 */
59 char *sbc = (char *)sb;
60 __u32 t32;
61 int i;
62
63 for (i=0; i < MD_SB_BYTES ; i+=4) {
64 char t = sbc[i];
65 sbc[i] = sbc[i+3];
66 sbc[i+3] = t;
67 t=sbc[i+1];
68 sbc[i+1]=sbc[i+2];
69 sbc[i+2]=t;
70 }
71 t32 = sb->events_hi;
72 sb->events_hi = sb->events_lo;
73 sb->events_lo = t32;
74
75 t32 = sb->cp_events_hi;
76 sb->cp_events_hi = sb->cp_events_lo;
77 sb->cp_events_lo = t32;
78
353632d9 79}
586ed405 80
c7654afc 81#ifndef MDASSEMBLE
586ed405 82
3da92f27 83static void examine_super0(struct supertype *st, char *homehost)
4b1ac34b 84{
3da92f27 85 mdp_super_t *sb = st->sb;
4b1ac34b
NB
86 time_t atime;
87 int d;
4180aa4d 88 int delta_extra = 0;
4b1ac34b
NB
89 char *c;
90
91 printf(" Magic : %08x\n", sb->md_magic);
d7ee65c9 92 printf(" Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version,
4b1ac34b 93 sb->patch_version);
a1cbd7d0
NB
94 if (sb->minor_version >= 90) {
95 printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
4b1ac34b 96 sb->set_uuid2, sb->set_uuid3);
a1cbd7d0 97 if (homehost) {
8268ed74
NB
98 char buf[20];
99 void *hash = sha1_buffer(homehost,
100 strlen(homehost),
101 buf);
a1cbd7d0
NB
102 if (memcmp(&sb->set_uuid2, hash, 8)==0)
103 printf(" (local to host %s)", homehost);
104 }
105 printf("\n");
106 } else
4b1ac34b
NB
107 printf(" UUID : %08x\n", sb->set_uuid0);
108
8382f19b
NB
109 if (sb->not_persistent)
110 printf(" Eedk : not persistent\n");
111
4b1ac34b
NB
112 atime = sb->ctime;
113 printf(" Creation Time : %.24s\n", ctime(&atime));
114 c=map_num(pers, sb->level);
115 printf(" Raid Level : %s\n", c?c:"-unknown-");
c43f7d91 116 if ((int)sb->level > 0) {
d4633e06 117 int ddsks = 0, ddsks_denom = 1;
e3362544
NB
118 printf(" Used Dev Size : %d%s\n", sb->size,
119 human_size((long long)sb->size<<10));
b674b5b8
NB
120 switch(sb->level) {
121 case 1: ddsks=1;break;
122 case 4:
123 case 5: ddsks = sb->raid_disks-1; break;
124 case 6: ddsks = sb->raid_disks-2; break;
d4633e06
N
125 case 10: ddsks = sb->raid_disks;
126 ddsks_denom = (sb->layout&255) * ((sb->layout>>8)&255);
127 }
128 if (ddsks) {
129 long long asize = sb->size;
130 asize = (asize << 10) * ddsks / ddsks_denom;
131 printf(" Array Size : %llu%s\n",
132 asize >> 10, human_size(asize));
b674b5b8 133 }
b674b5b8 134 }
4b1ac34b
NB
135 printf(" Raid Devices : %d\n", sb->raid_disks);
136 printf(" Total Devices : %d\n", sb->nr_disks);
137 printf("Preferred Minor : %d\n", sb->md_minor);
138 printf("\n");
b674b5b8 139 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
1e0d770c 140 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
b674b5b8
NB
141 if (sb->delta_disks) {
142 printf(" Delta Devices : %d", sb->delta_disks);
4180aa4d
N
143 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
144 if (((int)sb->delta_disks) < 0)
145 delta_extra = - sb->delta_disks;
b674b5b8
NB
146 }
147 if (sb->new_level != sb->level) {
148 c = map_num(pers, sb->new_level);
149 printf(" New Level : %s\n", c?c:"-unknown-");
150 }
151 if (sb->new_layout != sb->layout) {
152 if (sb->level == 5) {
153 c = map_num(r5layout, sb->new_layout);
154 printf(" New Layout : %s\n", c?c:"-unknown-");
155 }
fe77a154
N
156 if (sb->level == 6) {
157 c = map_num(r6layout, sb->new_layout);
158 printf(" New Layout : %s\n", c?c:"-unknown-");
159 }
b674b5b8 160 if (sb->level == 10) {
b578481c
NB
161 printf(" New Layout : near=%d, %s=%d\n",
162 sb->new_layout&255,
163 (sb->new_layout&0x10000)?"offset":"far",
164 (sb->new_layout>>8)&255);
b674b5b8
NB
165 }
166 }
167 if (sb->new_chunk != sb->chunk_size)
168 printf(" New Chunksize : %d\n", sb->new_chunk);
169 printf("\n");
170 }
4b1ac34b
NB
171 atime = sb->utime;
172 printf(" Update Time : %.24s\n", ctime(&atime));
173 printf(" State : %s\n",
174 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
55935d51
NB
175 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
176 printf("Internal Bitmap : present\n");
4b1ac34b
NB
177 printf(" Active Devices : %d\n", sb->active_disks);
178 printf("Working Devices : %d\n", sb->working_disks);
179 printf(" Failed Devices : %d\n", sb->failed_disks);
180 printf(" Spare Devices : %d\n", sb->spare_disks);
181 if (calc_sb0_csum(sb) == sb->sb_csum)
182 printf(" Checksum : %x - correct\n", sb->sb_csum);
183 else
184 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
1486e43f
NB
185 printf(" Events : %llu\n",
186 ((unsigned long long)sb->events_hi << 32)
187 + sb->events_lo);
4b1ac34b
NB
188 printf("\n");
189 if (sb->level == 5) {
190 c = map_num(r5layout, sb->layout);
191 printf(" Layout : %s\n", c?c:"-unknown-");
192 }
fe77a154
N
193 if (sb->level == 6) {
194 c = map_num(r6layout, sb->layout);
195 printf(" Layout : %s\n", c?c:"-unknown-");
196 }
265e0f17 197 if (sb->level == 10) {
e4965ef8
N
198 printf(" Layout :");
199 print_r10_layout(sb->layout);
200 printf("\n");
265e0f17 201 }
4b1ac34b
NB
202 switch(sb->level) {
203 case 0:
204 case 4:
205 case 5:
2c102711
NB
206 case 6:
207 case 10:
4b1ac34b
NB
208 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
209 break;
210 case -1:
211 printf(" Rounding : %dK\n", sb->chunk_size/1024);
212 break;
353632d9 213 default: break;
4b1ac34b
NB
214 }
215 printf("\n");
216 printf(" Number Major Minor RaidDevice State\n");
4180aa4d 217 for (d= -1; d<(signed int)(sb->raid_disks+delta_extra + sb->spare_disks); d++) {
4b1ac34b
NB
218 mdp_disk_t *dp;
219 char *dv;
220 char nb[5];
dfd4d8ee 221 int wonly;
4b1ac34b
NB
222 if (d>=0) dp = &sb->disks[d];
223 else dp = &sb->this_disk;
8f23b0b3 224 snprintf(nb, sizeof(nb), "%4d", d);
4b1ac34b
NB
225 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
226 dp->number, dp->major, dp->minor, dp->raid_disk);
dfd4d8ee
NB
227 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
228 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
4b1ac34b
NB
229 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
230 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
231 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
232 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
dfd4d8ee 233 if (wonly) printf(" write-mostly");
4b1ac34b 234 if (dp->state == 0) printf(" spare");
16c6fa80 235 if ((dv=map_dev(dp->major, dp->minor, 0)))
4b1ac34b
NB
236 printf(" %s", dv);
237 printf("\n");
238 if (d == -1) printf("\n");
239 }
240}
241
061f2c6a 242static void brief_examine_super0(struct supertype *st, int verbose)
4b1ac34b 243{
3da92f27 244 mdp_super_t *sb = st->sb;
4b1ac34b 245 char *c=map_num(pers, sb->level);
2998bc01 246 char devname[20];
2998bc01
NB
247
248 sprintf(devname, "/dev/md%d", sb->md_minor);
4b1ac34b 249
061f2c6a
N
250 if (verbose) {
251 printf("ARRAY %s level=%s num-devices=%d",
252 devname,
253 c?c:"-unknown-", sb->raid_disks);
254 } else
255 printf("ARRAY %s", devname);
256
4b1ac34b 257 if (sb->minor_version >= 90)
061f2c6a 258 printf(" UUID=%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
4b1ac34b
NB
259 sb->set_uuid2, sb->set_uuid3);
260 else
061f2c6a 261 printf(" UUID=%08x", sb->set_uuid0);
4b1ac34b
NB
262 printf("\n");
263}
264
0d726f17
KS
265static void export_examine_super0(struct supertype *st)
266{
267 mdp_super_t *sb = st->sb;
268
269 printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
270 printf("MD_DEVICES=%d\n", sb->raid_disks);
271 if (sb->minor_version >= 90)
272 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
273 sb->set_uuid0, sb->set_uuid1,
274 sb->set_uuid2, sb->set_uuid3);
275 else
276 printf("MD_UUID=%08x\n", sb->set_uuid0);
277 printf("MD_UPDATE_TIME=%llu\n",
278 __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL);
279 printf("MD_EVENTS=%llu\n",
280 ((unsigned long long)sb->events_hi << 32)
281 + sb->events_lo);
282}
283
3da92f27 284static void detail_super0(struct supertype *st, char *homehost)
4b1ac34b 285{
3da92f27 286 mdp_super_t *sb = st->sb;
4b1ac34b
NB
287 printf(" UUID : ");
288 if (sb->minor_version >= 90)
289 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
290 sb->set_uuid2, sb->set_uuid3);
291 else
292 printf("%08x", sb->set_uuid0);
b6750aa8 293 if (homehost) {
8268ed74
NB
294 char buf[20];
295 void *hash = sha1_buffer(homehost,
296 strlen(homehost),
297 buf);
b6750aa8
NB
298 if (memcmp(&sb->set_uuid2, hash, 8)==0)
299 printf(" (local to host %s)", homehost);
300 }
4b1ac34b
NB
301 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
302}
303
3da92f27 304static void brief_detail_super0(struct supertype *st)
4b1ac34b 305{
3da92f27 306 mdp_super_t *sb = st->sb;
4b1ac34b
NB
307 printf(" UUID=");
308 if (sb->minor_version >= 90)
309 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
310 sb->set_uuid2, sb->set_uuid3);
311 else
312 printf("%08x", sb->set_uuid0);
313}
c7654afc 314#endif
83b6208e 315
3da92f27 316static int match_home0(struct supertype *st, char *homehost)
83b6208e 317{
3da92f27 318 mdp_super_t *sb = st->sb;
8268ed74 319 char buf[20];
40d28f0d
N
320 char *hash;
321
322 if (!homehost)
323 return 0;
324 hash = sha1_buffer(homehost,
325 strlen(homehost),
326 buf);
83b6208e
NB
327
328 return (memcmp(&sb->set_uuid2, hash, 8)==0);
329}
330
3da92f27 331static void uuid_from_super0(struct supertype *st, int uuid[4])
4b1ac34b 332{
3da92f27 333 mdp_super_t *super = st->sb;
4b1ac34b
NB
334 uuid[0] = super->set_uuid0;
335 if (super->minor_version >= 90) {
336 uuid[1] = super->set_uuid1;
337 uuid[2] = super->set_uuid2;
338 uuid[3] = super->set_uuid3;
339 } else {
340 uuid[1] = 0;
341 uuid[2] = 0;
342 uuid[3] = 0;
343 }
344}
345
a5d85af7 346static void getinfo_super0(struct supertype *st, struct mdinfo *info, char *map)
4b1ac34b 347{
3da92f27 348 mdp_super_t *sb = st->sb;
4b1ac34b
NB
349 int working = 0;
350 int i;
a5d85af7 351 int map_disks = info->array.raid_disks;
4b1ac34b 352
95eeceeb 353 memset(info, 0, sizeof(*info));
4b1ac34b
NB
354 info->array.major_version = sb->major_version;
355 info->array.minor_version = sb->minor_version;
356 info->array.patch_version = sb->patch_version;
357 info->array.raid_disks = sb->raid_disks;
358 info->array.level = sb->level;
265e0f17 359 info->array.layout = sb->layout;
4b1ac34b
NB
360 info->array.md_minor = sb->md_minor;
361 info->array.ctime = sb->ctime;
353632d9
NB
362 info->array.utime = sb->utime;
363 info->array.chunk_size = sb->chunk_size;
583315d9 364 info->array.state = sb->state;
353632d9 365 info->component_size = sb->size*2;
4b1ac34b 366
c0c1acd6
N
367 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
368 info->bitmap_offset = 8;
369
4b1ac34b
NB
370 info->disk.state = sb->this_disk.state;
371 info->disk.major = sb->this_disk.major;
372 info->disk.minor = sb->this_disk.minor;
373 info->disk.raid_disk = sb->this_disk.raid_disk;
fbf8a0b7 374 info->disk.number = sb->this_disk.number;
4b1ac34b
NB
375
376 info->events = md_event(sb);
353632d9 377 info->data_offset = 0;
4b1ac34b 378
1522c538 379 sprintf(info->text_version, "0.%d", sb->minor_version);
a67dd8cc 380 info->safe_mode_delay = 200;
1522c538 381
3da92f27 382 uuid_from_super0(st, info->uuid);
947fd4dd 383
921d9e16 384 info->recovery_start = MaxSector;
353632d9
NB
385 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
386 info->reshape_active = 1;
387 info->reshape_progress = sb->reshape_position;
388 info->new_level = sb->new_level;
389 info->delta_disks = sb->delta_disks;
390 info->new_layout = sb->new_layout;
391 info->new_chunk = sb->new_chunk;
4180aa4d
N
392 if (info->delta_disks < 0)
393 info->array.raid_disks -= info->delta_disks;
353632d9
NB
394 } else
395 info->reshape_active = 0;
396
6e75048b
AK
397 info->recovery_blocked = info->reshape_active;
398
8a46fe84 399 sprintf(info->name, "%d", sb->md_minor);
4b1ac34b
NB
400 /* work_disks is calculated rather than read directly */
401 for (i=0; i < MD_SB_DISKS; i++)
402 if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) &&
f21e18ca 403 (sb->disks[i].raid_disk < (unsigned)info->array.raid_disks) &&
4b1ac34b 404 (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) &&
a5d85af7 405 !(sb->disks[i].state & (1<<MD_DISK_FAULTY))) {
4b1ac34b 406 working ++;
a5d85af7
N
407 if (map && i < map_disks)
408 map[i] = 1;
409 } else if (map && i < map_disks)
410 map[i] = 0;
4b1ac34b
NB
411 info->array.working_disks = working;
412}
413
00bbdbda
N
414static struct mdinfo *container_content0(struct supertype *st, char *subarray)
415{
416 struct mdinfo *info;
417
418 if (subarray)
419 return NULL;
420
503975b9 421 info = xmalloc(sizeof(*info));
00bbdbda
N
422 getinfo_super0(st, info, NULL);
423 return info;
424}
4b1ac34b 425
68c7d6d7 426static int update_super0(struct supertype *st, struct mdinfo *info,
3da92f27 427 char *update,
e5eac01f
NB
428 char *devname, int verbose,
429 int uuid_set, char *homehost)
4b1ac34b 430{
5a31170d
N
431 /* NOTE: for 'assemble' and 'force' we need to return non-zero
432 * if any change was made. For others, the return value is
433 * ignored.
c4d831e1 434 */
4b1ac34b 435 int rv = 0;
3b7e9d0c 436 int uuid[4];
3da92f27 437 mdp_super_t *sb = st->sb;
def11332
N
438
439 if (strcmp(update, "homehost") == 0 &&
440 homehost) {
441 /* note that 'homehost' is special as it is really
442 * a "uuid" update.
443 */
444 uuid_set = 0;
445 update = "uuid";
446 info->uuid[0] = sb->set_uuid0;
447 info->uuid[1] = sb->set_uuid1;
448 }
449
4b1ac34b
NB
450 if (strcmp(update, "sparc2.2")==0 ) {
451 /* 2.2 sparc put the events in the wrong place
452 * So we copy the tail of the superblock
453 * up 4 bytes before continuing
454 */
455 __u32 *sb32 = (__u32*)sb;
456 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
457 sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
458 (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4);
dab6685f 459 if (verbose >= 0)
dae45415 460 pr_err("adjusting superblock of %s for 2.2/sparc compatibility.\n",
e7b84f9d 461 devname);
1e2b2765 462 } else if (strcmp(update, "super-minor") ==0) {
4b1ac34b 463 sb->md_minor = info->array.md_minor;
dab6685f 464 if (verbose > 0)
e7b84f9d 465 pr_err("updating superblock of %s with minor number %d\n",
4b1ac34b 466 devname, info->array.md_minor);
1e2b2765 467 } else if (strcmp(update, "summaries") == 0) {
f21e18ca 468 unsigned int i;
4b1ac34b 469 /* set nr_disks, active_disks, working_disks,
aba69144 470 * failed_disks, spare_disks based on disks[]
4b1ac34b
NB
471 * array in superblock.
472 * Also make sure extra slots aren't 'failed'
473 */
474 sb->nr_disks = sb->active_disks =
475 sb->working_disks = sb->failed_disks =
476 sb->spare_disks = 0;
aba69144 477 for (i=0; i < MD_SB_DISKS ; i++)
4b1ac34b
NB
478 if (sb->disks[i].major ||
479 sb->disks[i].minor) {
480 int state = sb->disks[i].state;
481 if (state & (1<<MD_DISK_REMOVED))
482 continue;
483 sb->nr_disks++;
484 if (state & (1<<MD_DISK_ACTIVE))
485 sb->active_disks++;
486 if (state & (1<<MD_DISK_FAULTY))
487 sb->failed_disks++;
488 else
489 sb->working_disks++;
490 if (state == 0)
491 sb->spare_disks++;
492 } else if (i >= sb->raid_disks && sb->disks[i].number == 0)
493 sb->disks[i].state = 0;
1e2b2765 494 } else if (strcmp(update, "force-one")==0) {
67a8c82d
NB
495 /* Not enough devices for a working array, so
496 * bring this one up-to-date.
497 */
c4d831e1 498 __u32 ehi = sb->events_hi, elo = sb->events_lo;
4b1ac34b
NB
499 sb->events_hi = (info->events>>32) & 0xFFFFFFFF;
500 sb->events_lo = (info->events) & 0xFFFFFFFF;
c4d831e1
NB
501 if (sb->events_hi != ehi ||
502 sb->events_lo != elo)
503 rv = 1;
1e2b2765 504 } else if (strcmp(update, "force-array")==0) {
67a8c82d
NB
505 /* degraded array and 'force' requested, so
506 * maybe need to mark it 'clean'
507 */
c4d831e1
NB
508 if ((sb->level == 5 || sb->level == 4 || sb->level == 6) &&
509 (sb->state & (1 << MD_SB_CLEAN)) == 0) {
4b1ac34b
NB
510 /* need to force clean */
511 sb->state |= (1 << MD_SB_CLEAN);
c4d831e1
NB
512 rv = 1;
513 }
1e2b2765 514 } else if (strcmp(update, "assemble")==0) {
4b1ac34b 515 int d = info->disk.number;
dfd4d8ee 516 int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY);
672ca1b7
N
517 int mask = (1<<MD_DISK_WRITEMOSTLY);
518 int add = 0;
519 if (sb->minor_version >= 91)
520 /* During reshape we don't insist on everything
521 * being marked 'sync'
522 */
523 add = (1<<MD_DISK_SYNC);
524 if (((sb->disks[d].state & ~mask) | add)
f21e18ca 525 != (unsigned)info->disk.state) {
c4d831e1 526 sb->disks[d].state = info->disk.state | wonly;
4b1ac34b
NB
527 rv = 1;
528 }
d43494fc
N
529 if (info->reshape_active &&
530 sb->minor_version > 90 && (sb->reshape_position+1) != 0 &&
531 info->delta_disks >= 0 &&
532 info->reshape_progress < sb->reshape_position) {
533 sb->reshape_position = info->reshape_progress;
534 rv = 1;
535 }
536 if (info->reshape_active &&
537 sb->minor_version > 90 && (sb->reshape_position+1) != 0 &&
538 info->delta_disks < 0 &&
539 info->reshape_progress > sb->reshape_position) {
540 sb->reshape_position = info->reshape_progress;
541 rv = 1;
542 }
1e2b2765 543 } else if (strcmp(update, "linear-grow-new") == 0) {
f752781f
NB
544 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
545 sb->disks[info->disk.number].number = info->disk.number;
546 sb->disks[info->disk.number].major = info->disk.major;
547 sb->disks[info->disk.number].minor = info->disk.minor;
548 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
549 sb->disks[info->disk.number].state = info->disk.state;
550 sb->this_disk = sb->disks[info->disk.number];
1e2b2765 551 } else if (strcmp(update, "linear-grow-update") == 0) {
4b1ac34b
NB
552 sb->raid_disks = info->array.raid_disks;
553 sb->nr_disks = info->array.nr_disks;
554 sb->active_disks = info->array.active_disks;
555 sb->working_disks = info->array.working_disks;
556 memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0]));
557 sb->disks[info->disk.number].number = info->disk.number;
558 sb->disks[info->disk.number].major = info->disk.major;
559 sb->disks[info->disk.number].minor = info->disk.minor;
560 sb->disks[info->disk.number].raid_disk = info->disk.raid_disk;
561 sb->disks[info->disk.number].state = info->disk.state;
1e2b2765 562 } else if (strcmp(update, "resync") == 0) {
4b1ac34b
NB
563 /* make sure resync happens */
564 sb->state &= ~(1<<MD_SB_CLEAN);
565 sb->recovery_cp = 0;
1e2b2765 566 } else if (strcmp(update, "uuid") == 0) {
e5eac01f 567 if (!uuid_set && homehost) {
8268ed74
NB
568 char buf[20];
569 char *hash = sha1_buffer(homehost,
570 strlen(homehost),
571 buf);
e5eac01f
NB
572 memcpy(info->uuid+2, hash, 8);
573 }
7d99579f
NB
574 sb->set_uuid0 = info->uuid[0];
575 sb->set_uuid1 = info->uuid[1];
576 sb->set_uuid2 = info->uuid[2];
577 sb->set_uuid3 = info->uuid[3];
14263cf1
NB
578 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
579 struct bitmap_super_s *bm;
580 bm = (struct bitmap_super_s*)(sb+1);
3b7e9d0c
LB
581 uuid_from_super0(st, uuid);
582 memcpy(bm->uuid, uuid, 16);
14263cf1 583 }
5a31170d
N
584 } else if (strcmp(update, "no-bitmap") == 0) {
585 sb->state &= ~(1<<MD_SB_BITMAP_PRESENT);
1e2b2765 586 } else if (strcmp(update, "_reshape_progress")==0)
353632d9 587 sb->reshape_position = info->reshape_progress;
16715c01
DL
588 else if (strcmp(update, "writemostly")==0)
589 sb->state |= (1<<MD_DISK_WRITEMOSTLY);
590 else if (strcmp(update, "readwrite")==0)
591 sb->state &= ~(1<<MD_DISK_WRITEMOSTLY);
1e2b2765
N
592 else
593 rv = -1;
4b1ac34b
NB
594
595 sb->sb_csum = calc_sb0_csum(sb);
596 return rv;
597}
598
05697ec1
NB
599/*
600 * For verion-0 superblock, the homehost is 'stored' in the
601 * uuid. 8 bytes for a hash of the host leaving 8 bytes
602 * of random material.
603 * We use the first 8 bytes (64bits) of the sha1 of the
604 * host name
605 */
4b1ac34b
NB
606
607
3da92f27 608static int init_super0(struct supertype *st, mdu_array_info_t *info,
3d3dd91e 609 unsigned long long size, char *ignored_name, char *homehost,
83cd1e97 610 int *uuid, unsigned long long data_offset)
4b1ac34b 611{
6416d527 612 mdp_super_t *sb;
82d9eba6 613 int spares;
6416d527 614
83cd1e97
N
615 if (data_offset != INVALID_SECTORS) {
616 fprintf(stderr, Name ": data-offset not support for 0.90\n");
617 return 0;
618 }
619
b550887f
N
620 if (posix_memalign((void**)&sb, 4096,
621 MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
e7b84f9d 622 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
623 return 0;
624 }
55935d51 625 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
4b1ac34b 626
64557c33 627 st->sb = sb;
ba7eb04f 628 if (info == NULL) {
f9ce90ba 629 /* zeroing the superblock */
82d9eba6
NB
630 return 0;
631 }
632
633 spares = info->working_disks - info->active_disks;
634 if (info->raid_disks + spares > MD_SB_DISKS) {
e7b84f9d 635 pr_err("too many devices requested: %d+%d > %d\n",
82d9eba6
NB
636 info->raid_disks , spares, MD_SB_DISKS);
637 return 0;
f9ce90ba
NB
638 }
639
4b1ac34b
NB
640 sb->md_magic = MD_SB_MAGIC;
641 sb->major_version = 0;
642 sb->minor_version = 90;
643 sb->patch_version = 0;
644 sb->gvalid_words = 0; /* ignored */
4b1ac34b
NB
645 sb->ctime = time(0);
646 sb->level = info->level;
63ebe78f
N
647 sb->size = size;
648 if (size != (unsigned long long)sb->size)
5dd497ee 649 return 0;
4b1ac34b
NB
650 sb->nr_disks = info->nr_disks;
651 sb->raid_disks = info->raid_disks;
652 sb->md_minor = info->md_minor;
653 sb->not_persistent = 0;
3d3dd91e
NB
654 if (uuid) {
655 sb->set_uuid0 = uuid[0];
656 sb->set_uuid1 = uuid[1];
657 sb->set_uuid2 = uuid[2];
658 sb->set_uuid3 = uuid[3];
659 } else {
660 int rfd = open("/dev/urandom", O_RDONLY);
661 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
662 sb->set_uuid0 = random();
663 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
664 sb->set_uuid1 = random();
665 sb->set_uuid2 = random();
666 sb->set_uuid3 = random();
667 }
668 if (rfd >= 0)
669 close(rfd);
dfe47e00 670 }
30d48159 671 if (homehost && !uuid) {
8268ed74
NB
672 char buf[20];
673 char *hash = sha1_buffer(homehost,
674 strlen(homehost),
675 buf);
05697ec1
NB
676 memcpy(&sb->set_uuid2, hash, 8);
677 }
4b1ac34b
NB
678
679 sb->utime = sb->ctime;
680 sb->state = info->state;
681 sb->active_disks = info->active_disks;
682 sb->working_disks = info->working_disks;
683 sb->failed_disks = info->failed_disks;
234a131a 684 sb->spare_disks = info->spare_disks;
4b1ac34b
NB
685 sb->events_hi = 0;
686 sb->events_lo = 1;
687
688 sb->layout = info->layout;
689 sb->chunk_size = info->chunk_size;
690
82d9eba6 691 return 1;
4b1ac34b
NB
692}
693
111d01fc
NB
694struct devinfo {
695 int fd;
696 char *devname;
697 mdu_disk_info_t disk;
698 struct devinfo *next;
699};
0e600426
N
700
701#ifndef MDASSEMBLE
4b1ac34b 702/* Add a device to the superblock being created */
f20c3968 703static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
72ca9bcf 704 int fd, char *devname, unsigned long long data_offset)
4b1ac34b 705{
3da92f27 706 mdp_super_t *sb = st->sb;
4b1ac34b 707 mdp_disk_t *dk = &sb->disks[dinfo->number];
111d01fc 708 struct devinfo *di, **dip;
353632d9 709
4b1ac34b
NB
710 dk->number = dinfo->number;
711 dk->major = dinfo->major;
712 dk->minor = dinfo->minor;
713 dk->raid_disk = dinfo->raid_disk;
714 dk->state = dinfo->state;
16715c01
DL
715 /* In case our source disk was writemostly, don't copy that bit */
716 dk->state &= ~(1<<MD_DISK_WRITEMOSTLY);
111d01fc 717
d2ca6449
NB
718 sb->this_disk = sb->disks[dinfo->number];
719 sb->sb_csum = calc_sb0_csum(sb);
720
111d01fc
NB
721 dip = (struct devinfo **)&st->info;
722 while (*dip)
723 dip = &(*dip)->next;
503975b9 724 di = xmalloc(sizeof(struct devinfo));
111d01fc
NB
725 di->fd = fd;
726 di->devname = devname;
727 di->disk = *dinfo;
728 di->next = NULL;
729 *dip = di;
f20c3968
DW
730
731 return 0;
4b1ac34b 732}
0e600426 733#endif
4b1ac34b 734
3da92f27 735static int store_super0(struct supertype *st, int fd)
4b1ac34b 736{
4b1ac34b
NB
737 unsigned long long dsize;
738 unsigned long long offset;
3da92f27 739 mdp_super_t *super = st->sb;
beae1dfe
NB
740
741 if (!get_dev_size(fd, NULL, &dsize))
742 return 1;
4b1ac34b 743
eb6dae98 744 if (dsize < MD_RESERVED_SECTORS*512)
4b1ac34b 745 return 2;
353632d9 746
4b1ac34b
NB
747 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
748
749 offset *= 512;
750
751 if (lseek64(fd, offset, 0)< 0LL)
752 return 3;
753
754 if (write(fd, super, sizeof(*super)) != sizeof(*super))
755 return 4;
756
14263cf1
NB
757 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
758 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
759 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
b550887f
N
760 if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) !=
761 ROUND_UP(sizeof(*bm),4096))
9fca7d62 762 return 5;
14263cf1
NB
763 }
764
570c0542 765 fsync(fd);
4b1ac34b
NB
766 return 0;
767}
768
111d01fc
NB
769#ifndef MDASSEMBLE
770static int write_init_super0(struct supertype *st)
4b1ac34b 771{
3da92f27 772 mdp_super_t *sb = st->sb;
111d01fc
NB
773 int rv = 0;
774 struct devinfo *di;
4b1ac34b 775
111d01fc 776 for (di = st->info ; di && ! rv ; di = di->next) {
4b1ac34b 777
111d01fc
NB
778 if (di->disk.state == 1)
779 continue;
a0c8a17f
NB
780 if (di->fd == -1)
781 continue;
ba728be7 782 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
9277cc77 783 ;
8d75b7fc 784
111d01fc 785 sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY);
55935d51 786
111d01fc
NB
787 sb->this_disk = sb->disks[di->disk.number];
788 sb->sb_csum = calc_sb0_csum(sb);
789 rv = store_super0(st, di->fd);
55935d51 790
111d01fc
NB
791 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
792 rv = st->ss->write_bitmap(st, di->fd);
793
794 if (rv)
e7b84f9d
N
795 pr_err("failed to write superblock to %s\n",
796 di->devname);
111d01fc 797 }
4b1ac34b
NB
798 return rv;
799}
111d01fc 800#endif
4b1ac34b 801
64557c33 802static int compare_super0(struct supertype *st, struct supertype *tst)
4b1ac34b
NB
803{
804 /*
805 * return:
806 * 0 same, or first was empty, and second was copied
807 * 1 second had wrong number
808 * 2 wrong uuid
809 * 3 wrong other info
810 */
64557c33
NB
811 mdp_super_t *first = st->sb;
812 mdp_super_t *second = tst->sb;
4b1ac34b 813 int uuid1[4], uuid2[4];
64557c33 814
4b1ac34b
NB
815 if (second->md_magic != MD_SB_MAGIC)
816 return 1;
817 if (!first) {
b550887f
N
818 if (posix_memalign((void**)&first, 4096,
819 MD_SB_BYTES +
820 ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
e7b84f9d 821 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
822 return 1;
823 }
14263cf1 824 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
64557c33 825 st->sb = first;
4b1ac34b
NB
826 return 0;
827 }
828
3da92f27
NB
829 uuid_from_super0(st, uuid1);
830 uuid_from_super0(tst, uuid2);
f277ce36 831 if (!same_uuid(uuid1, uuid2, 0))
4b1ac34b
NB
832 return 2;
833 if (first->major_version != second->major_version ||
834 first->minor_version != second->minor_version ||
835 first->patch_version != second->patch_version ||
836 first->gvalid_words != second->gvalid_words ||
837 first->ctime != second->ctime ||
838 first->level != second->level ||
839 first->size != second->size ||
840 first->raid_disks != second->raid_disks )
841 return 3;
842
843 return 0;
844}
845
846
3da92f27
NB
847static void free_super0(struct supertype *st);
848
849static int load_super0(struct supertype *st, int fd, char *devname)
4b1ac34b
NB
850{
851 /* try to read in the superblock
852 * Return:
853 * 0 on success
854 * 1 on cannot get superblock
855 * 2 on superblock meaningless
856 */
4b1ac34b
NB
857 unsigned long long dsize;
858 unsigned long long offset;
859 mdp_super_t *super;
14263cf1
NB
860 int uuid[4];
861 struct bitmap_super_s *bsb;
aba69144 862
3da92f27
NB
863 free_super0(st);
864
beae1dfe
NB
865 if (!get_dev_size(fd, devname, &dsize))
866 return 1;
4b1ac34b 867
eb6dae98 868 if (dsize < MD_RESERVED_SECTORS*512) {
4b1ac34b 869 if (devname)
e7b84f9d
N
870 pr_err("%s is too small for md: size is %llu sectors.\n",
871 devname, dsize);
4b1ac34b
NB
872 return 1;
873 }
353632d9 874
4b1ac34b
NB
875 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
876
877 offset *= 512;
878
4b1ac34b
NB
879 if (lseek64(fd, offset, 0)< 0LL) {
880 if (devname)
e7b84f9d 881 pr_err("Cannot seek to superblock on %s: %s\n",
4b1ac34b
NB
882 devname, strerror(errno));
883 return 1;
884 }
885
b550887f
N
886 if (posix_memalign((void**)&super, 4096,
887 MD_SB_BYTES +
888 ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
e7b84f9d 889 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
890 return 1;
891 }
4b1ac34b
NB
892
893 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
894 if (devname)
e7b84f9d 895 pr_err("Cannot read superblock on %s\n",
4b1ac34b
NB
896 devname);
897 free(super);
898 return 1;
899 }
900
586ed405
NB
901 if (st->ss && st->minor_version == 9)
902 super0_swap_endian(super);
903
4b1ac34b
NB
904 if (super->md_magic != MD_SB_MAGIC) {
905 if (devname)
e7b84f9d 906 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
4b1ac34b
NB
907 devname, MD_SB_MAGIC, super->md_magic);
908 free(super);
909 return 2;
910 }
911
912 if (super->major_version != 0) {
913 if (devname)
e7b84f9d 914 pr_err("Cannot interpret superblock on %s - version is %d\n",
4b1ac34b
NB
915 devname, super->major_version);
916 free(super);
917 return 2;
918 }
64557c33 919 st->sb = super;
3da92f27 920
82d9eba6
NB
921 if (st->ss == NULL) {
922 st->ss = &super0;
5f98d3cb 923 st->minor_version = super->minor_version;
ea329559 924 st->max_devs = MD_SB_DISKS;
111d01fc 925 st->info = NULL;
82d9eba6
NB
926 }
927
14263cf1
NB
928 /* Now check on the bitmap superblock */
929 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
930 return 0;
931 /* Read the bitmap superblock and make sure it looks
932 * valid. If it doesn't clear the bit. An --assemble --force
933 * should get that written out.
934 */
b550887f
N
935 if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096))
936 != ROUND_UP(sizeof(struct bitmap_super_s),4096))
14263cf1
NB
937 goto no_bitmap;
938
3da92f27 939 uuid_from_super0(st, uuid);
14263cf1
NB
940 bsb = (struct bitmap_super_s *)(super+1);
941 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
942 memcmp(bsb->uuid, uuid, 16) != 0)
943 goto no_bitmap;
944 return 0;
945
946 no_bitmap:
947 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
948
4b1ac34b
NB
949 return 0;
950}
f9ce90ba 951
82d9eba6 952static struct supertype *match_metadata_desc0(char *arg)
f9ce90ba 953{
503975b9 954 struct supertype *st = xcalloc(1, sizeof(*st));
82d9eba6 955
4dd2df09 956 st->container_devnm[0] = 0;
82d9eba6 957 st->ss = &super0;
111d01fc 958 st->info = NULL;
82d9eba6 959 st->minor_version = 90;
ea329559 960 st->max_devs = MD_SB_DISKS;
64557c33 961 st->sb = NULL;
9b2a22d3
N
962 /* we sometimes get 00.90 */
963 while (arg[0] == '0' && arg[1] == '0')
964 arg++;
f9ce90ba 965 if (strcmp(arg, "0") == 0 ||
26f467a9 966#ifdef DEFAULT_OLD_METADATA /* ifndef in super1.c */
967 strcmp(arg, "default") == 0 ||
968#endif /* DEFAULT_OLD_METADATA */
50827504
N
969 strcmp(arg, "0.90") == 0 ||
970 strcmp(arg, "") == 0 /* no metadata - i.e. non_persistent */
f9ce90ba 971 )
82d9eba6
NB
972 return st;
973
56f8add2
NB
974 st->minor_version = 91; /* reshape in progress */
975 if (strcmp(arg, "0.91") == 0) /* For dup_super support */
976 return st;
977
586ed405 978 st->minor_version = 9; /* flag for 'byte-swapped' */
ff1f6545
NB
979 if (strcmp(arg, "0.swap")==0 ||
980 strcmp(arg, "0.9") == 0) /* For dup_super support */
586ed405
NB
981 return st;
982
82d9eba6
NB
983 free(st);
984 return NULL;
985}
986
387fcd59
N
987static __u64 avail_size0(struct supertype *st, __u64 devsize,
988 unsigned long long data_offset)
82d9eba6 989{
387fcd59
N
990 if (data_offset != 0 && data_offset != INVALID_SECTORS)
991 return 0ULL;
eb6dae98 992 if (devsize < MD_RESERVED_SECTORS)
82d9eba6
NB
993 return 0ULL;
994 return MD_NEW_SIZE_SECTORS(devsize);
f9ce90ba
NB
995}
996
3da92f27 997static int add_internal_bitmap0(struct supertype *st, int *chunkp,
199171a2
NB
998 int delay, int write_behind,
999 unsigned long long size, int may_change,
1000 int major)
55935d51
NB
1001{
1002 /*
1003 * The bitmap comes immediately after the superblock and must be 60K in size
1004 * at most. The default size is between 30K and 60K
1005 *
8686f3ed 1006 * size is in sectors, chunk is in bytes !!!
55935d51 1007 */
1bf4e2d9 1008 unsigned long long bits;
50526e90 1009 unsigned long long max_bits = (60*1024 - sizeof(bitmap_super_t))*8;
55935d51 1010 unsigned long long min_chunk;
199171a2 1011 int chunk = *chunkp;
3da92f27 1012 mdp_super_t *sb = st->sb;
55935d51 1013 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
3b7e9d0c 1014 int uuid[4];
55935d51 1015
353632d9 1016
f277ce36 1017 min_chunk = 4096; /* sub-page chunks don't work yet.. */
8686f3ed 1018 bits = (size * 512) / min_chunk + 1;
55935d51
NB
1019 while (bits > max_bits) {
1020 min_chunk *= 2;
1021 bits = (bits+1)/2;
1022 }
b8ab2a50
N
1023 if (chunk == UnSet) {
1024 /* A chunk size less than a few Megabytes gives poor
1025 * performance without increasing resync noticeably
1026 */
55935d51 1027 chunk = min_chunk;
b8ab2a50
N
1028 if (chunk < 64*1024*1024)
1029 chunk = 64*1024*1024;
f21e18ca 1030 } else if ((unsigned long long)chunk < min_chunk)
55935d51
NB
1031 return 0; /* chunk size too small */
1032
1033 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1034
29e766a5 1035 memset(bms, 0, sizeof(*bms));
dfd4d8ee 1036 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1037 bms->version = __cpu_to_le32(major);
3b7e9d0c
LB
1038 uuid_from_super0(st, uuid);
1039 memcpy(bms->uuid, uuid, 16);
dfd4d8ee
NB
1040 bms->chunksize = __cpu_to_le32(chunk);
1041 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1042 bms->sync_size = __cpu_to_le64(size);
dfd4d8ee 1043 bms->write_behind = __cpu_to_le32(write_behind);
199171a2 1044 *chunkp = chunk;
55935d51
NB
1045 return 1;
1046}
353632d9 1047
55935d51 1048
ec9688ca 1049static void locate_bitmap0(struct supertype *st, int fd)
55935d51
NB
1050{
1051 unsigned long long dsize;
55935d51 1052 unsigned long long offset;
55935d51 1053
beae1dfe
NB
1054 if (!get_dev_size(fd, NULL, &dsize))
1055 return;
1056
eb6dae98 1057 if (dsize < MD_RESERVED_SECTORS*512)
55935d51 1058 return;
353632d9 1059
55935d51
NB
1060 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1061
1062 offset *= 512;
1063
1064 offset += MD_SB_BYTES;
1065
1066 lseek64(fd, offset, 0);
1067}
1068
ec9688ca 1069static int write_bitmap0(struct supertype *st, int fd)
8d75b7fc 1070{
8d75b7fc
NB
1071 unsigned long long dsize;
1072 unsigned long long offset;
3da92f27 1073 mdp_super_t *sb = st->sb;
aba69144 1074
8d75b7fc
NB
1075 int rv = 0;
1076
1077 int towrite, n;
d669228f 1078 void *buf;
8d75b7fc 1079
beae1dfe
NB
1080 if (!get_dev_size(fd, NULL, &dsize))
1081 return 1;
1082
eb6dae98
NB
1083 if (dsize < MD_RESERVED_SECTORS*512)
1084 return -1;
353632d9 1085
8d75b7fc
NB
1086 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1087
1088 offset *= 512;
1089
1090 if (lseek64(fd, offset + 4096, 0)< 0LL)
1091 return 3;
1092
d669228f
JS
1093 if (posix_memalign(&buf, 4096, 4096))
1094 return -ENOMEM;
1095
6416d527
NB
1096 memset(buf, 0xff, 4096);
1097 memcpy(buf, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t));
50526e90 1098 towrite = 60*1024;
8d75b7fc
NB
1099 while (towrite > 0) {
1100 n = towrite;
6416d527
NB
1101 if (n > 4096)
1102 n = 4096;
8d75b7fc
NB
1103 n = write(fd, buf, n);
1104 if (n > 0)
1105 towrite -= n;
1106 else
1107 break;
6416d527 1108 memset(buf, 0xff, 4096);
8d75b7fc 1109 }
dfd4d8ee 1110 fsync(fd);
8d75b7fc
NB
1111 if (towrite)
1112 rv = -2;
1113
d669228f 1114 free(buf);
8d75b7fc
NB
1115 return rv;
1116}
55935d51 1117
3da92f27 1118static void free_super0(struct supertype *st)
df37ffc0 1119{
3da92f27
NB
1120 if (st->sb)
1121 free(st->sb);
1cc7f4fe
N
1122 while (st->info) {
1123 struct devinfo *di = st->info;
1124 st->info = di->next;
1125 if (di->fd >= 0)
1126 close(di->fd);
1127 free(di);
1128 }
3da92f27 1129 st->sb = NULL;
df37ffc0
NB
1130}
1131
0e600426 1132#ifndef MDASSEMBLE
17f25ca6
NB
1133static int validate_geometry0(struct supertype *st, int level,
1134 int layout, int raiddisks,
c21e737b 1135 int *chunk, unsigned long long size,
af4348dd 1136 unsigned long long data_offset,
2c514b71
NB
1137 char *subdev, unsigned long long *freesize,
1138 int verbose)
17f25ca6
NB
1139{
1140 unsigned long long ldsize;
1141 int fd;
01619b48
N
1142 unsigned int tbmax = 4;
1143
1144 /* prior to linux 3.1, a but limits usable device size to 2TB.
1145 * It was introduced in 2.6.29, but we won't worry about that detail
1146 */
1147 if (get_linux_version() < 3001000)
1148 tbmax = 2;
17f25ca6 1149
b42f577a
N
1150 if (level == LEVEL_CONTAINER) {
1151 if (verbose)
e7b84f9d 1152 pr_err("0.90 metadata does not support containers\n");
17f25ca6 1153 return 0;
b42f577a
N
1154 }
1155 if (raiddisks > MD_SB_DISKS) {
1156 if (verbose)
e7b84f9d 1157 pr_err("0.90 metadata supports at most %d devices per array\n",
b42f577a 1158 MD_SB_DISKS);
17f25ca6 1159 return 0;
b42f577a 1160 }
9c8c1218 1161 if (size >= tbmax * 2ULL*1024*1024*1024) {
b42f577a 1162 if (verbose)
e7b84f9d
N
1163 pr_err("0.90 metadata supports at most "
1164 "%d terabytes per device\n", tbmax);
17f25ca6 1165 return 0;
b42f577a 1166 }
bb7295f1
N
1167 if (chunk && *chunk == UnSet)
1168 *chunk = DEFAULT_CHUNK;
1169
17f25ca6
NB
1170 if (!subdev)
1171 return 1;
1172
1173 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1174 if (fd < 0) {
2c514b71 1175 if (verbose)
e7b84f9d 1176 pr_err("super0.90 cannot open %s: %s\n",
2c514b71 1177 subdev, strerror(errno));
17f25ca6
NB
1178 return 0;
1179 }
2c514b71 1180
17f25ca6
NB
1181 if (!get_dev_size(fd, subdev, &ldsize)) {
1182 close(fd);
1183 return 0;
1184 }
1185 close(fd);
1186
1187 if (ldsize < MD_RESERVED_SECTORS * 512)
1188 return 0;
17f25ca6
NB
1189 *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
1190 return 1;
1191}
0e600426 1192#endif /* MDASSEMBLE */
17f25ca6 1193
f9ce90ba 1194struct superswitch super0 = {
c7654afc 1195#ifndef MDASSEMBLE
f9ce90ba
NB
1196 .examine_super = examine_super0,
1197 .brief_examine_super = brief_examine_super0,
0d726f17 1198 .export_examine_super = export_examine_super0,
f9ce90ba
NB
1199 .detail_super = detail_super0,
1200 .brief_detail_super = brief_detail_super0,
111d01fc 1201 .write_init_super = write_init_super0,
0e600426
N
1202 .validate_geometry = validate_geometry0,
1203 .add_to_super = add_to_super0,
c7654afc 1204#endif
83b6208e 1205 .match_home = match_home0,
f9ce90ba
NB
1206 .uuid_from_super = uuid_from_super0,
1207 .getinfo_super = getinfo_super0,
00bbdbda 1208 .container_content = container_content0,
f9ce90ba 1209 .update_super = update_super0,
f9ce90ba 1210 .init_super = init_super0,
f9ce90ba 1211 .store_super = store_super0,
f9ce90ba
NB
1212 .compare_super = compare_super0,
1213 .load_super = load_super0,
1214 .match_metadata_desc = match_metadata_desc0,
82d9eba6 1215 .avail_size = avail_size0,
55935d51
NB
1216 .add_internal_bitmap = add_internal_bitmap0,
1217 .locate_bitmap = locate_bitmap0,
8d75b7fc 1218 .write_bitmap = write_bitmap0,
df37ffc0 1219 .free_super = free_super0,
4cce4069 1220 .name = "0.90",
f9ce90ba 1221};