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