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