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