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