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