]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super0.c
Change some "fprintf(stderr,"s to pr_err.
[thirdparty/mdadm.git] / super0.c
CommitLineData
4b1ac34b
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
4b1ac34b
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
e736b623 22 * Email: <neilb@suse.de>
4b1ac34b
NB
23 */
24
8268ed74 25#define HAVE_STDINT_H 1
4b1ac34b 26#include "mdadm.h"
8268ed74 27#include "sha1.h"
4b1ac34b
NB
28/*
29 * All handling for the 0.90.0 version superblock is in
30 * this file.
31 * This includes:
32 * - finding, loading, and writing the superblock.
33 * - initialising a new superblock
34 * - printing the superblock for --examine
35 * - printing part of the superblock for --detail
aba69144 36 * .. other stuff
4b1ac34b
NB
37 */
38
39
40static unsigned long calc_sb0_csum(mdp_super_t *super)
41{
42 unsigned long csum = super->sb_csum;
43 unsigned long newcsum;
44 super->sb_csum= 0 ;
45 newcsum = calc_csum(super, MD_SB_BYTES);
46 super->sb_csum = csum;
47 return newcsum;
48}
49
ec9688ca 50static void super0_swap_endian(struct mdp_superblock_s *sb)
586ed405
NB
51{
52 /* as super0 superblocks are host-endian, it is sometimes
aba69144 53 * useful to be able to swap the endianness
586ed405
NB
54 * as (almost) everything is u32's we byte-swap every 4byte
55 * number.
56 * We then also have to swap the events_hi and events_lo
57 */
58 char *sbc = (char *)sb;
59 __u32 t32;
60 int i;
61
62 for (i=0; i < MD_SB_BYTES ; i+=4) {
63 char t = sbc[i];
64 sbc[i] = sbc[i+3];
65 sbc[i+3] = t;
66 t=sbc[i+1];
67 sbc[i+1]=sbc[i+2];
68 sbc[i+2]=t;
69 }
70 t32 = sb->events_hi;
71 sb->events_hi = sb->events_lo;
72 sb->events_lo = t32;
73
74 t32 = sb->cp_events_hi;
75 sb->cp_events_hi = sb->cp_events_lo;
76 sb->cp_events_lo = t32;
77
353632d9 78}
586ed405 79
c7654afc 80#ifndef MDASSEMBLE
586ed405 81
3da92f27 82static void examine_super0(struct supertype *st, char *homehost)
4b1ac34b 83{
3da92f27 84 mdp_super_t *sb = st->sb;
4b1ac34b
NB
85 time_t atime;
86 int d;
4180aa4d 87 int delta_extra = 0;
4b1ac34b
NB
88 char *c;
89
90 printf(" Magic : %08x\n", sb->md_magic);
d7ee65c9 91 printf(" Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version,
4b1ac34b 92 sb->patch_version);
a1cbd7d0
NB
93 if (sb->minor_version >= 90) {
94 printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
4b1ac34b 95 sb->set_uuid2, sb->set_uuid3);
a1cbd7d0 96 if (homehost) {
8268ed74
NB
97 char buf[20];
98 void *hash = sha1_buffer(homehost,
99 strlen(homehost),
100 buf);
a1cbd7d0
NB
101 if (memcmp(&sb->set_uuid2, hash, 8)==0)
102 printf(" (local to host %s)", homehost);
103 }
104 printf("\n");
105 } else
4b1ac34b
NB
106 printf(" UUID : %08x\n", sb->set_uuid0);
107
8382f19b
NB
108 if (sb->not_persistent)
109 printf(" Eedk : not persistent\n");
110
4b1ac34b
NB
111 atime = sb->ctime;
112 printf(" Creation Time : %.24s\n", ctime(&atime));
113 c=map_num(pers, sb->level);
114 printf(" Raid Level : %s\n", c?c:"-unknown-");
c43f7d91 115 if ((int)sb->level > 0) {
d4633e06 116 int ddsks = 0, ddsks_denom = 1;
e3362544
NB
117 printf(" Used Dev Size : %d%s\n", sb->size,
118 human_size((long long)sb->size<<10));
b674b5b8
NB
119 switch(sb->level) {
120 case 1: ddsks=1;break;
121 case 4:
122 case 5: ddsks = sb->raid_disks-1; break;
123 case 6: ddsks = sb->raid_disks-2; break;
d4633e06
N
124 case 10: ddsks = sb->raid_disks;
125 ddsks_denom = (sb->layout&255) * ((sb->layout>>8)&255);
126 }
127 if (ddsks) {
128 long long asize = sb->size;
129 asize = (asize << 10) * ddsks / ddsks_denom;
130 printf(" Array Size : %llu%s\n",
131 asize >> 10, human_size(asize));
b674b5b8 132 }
b674b5b8 133 }
4b1ac34b
NB
134 printf(" Raid Devices : %d\n", sb->raid_disks);
135 printf(" Total Devices : %d\n", sb->nr_disks);
136 printf("Preferred Minor : %d\n", sb->md_minor);
137 printf("\n");
b674b5b8 138 if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) {
1e0d770c 139 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9));
b674b5b8
NB
140 if (sb->delta_disks) {
141 printf(" Delta Devices : %d", sb->delta_disks);
4180aa4d
N
142 printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks);
143 if (((int)sb->delta_disks) < 0)
144 delta_extra = - sb->delta_disks;
b674b5b8
NB
145 }
146 if (sb->new_level != sb->level) {
147 c = map_num(pers, sb->new_level);
148 printf(" New Level : %s\n", c?c:"-unknown-");
149 }
150 if (sb->new_layout != sb->layout) {
151 if (sb->level == 5) {
152 c = map_num(r5layout, sb->new_layout);
153 printf(" New Layout : %s\n", c?c:"-unknown-");
154 }
fe77a154
N
155 if (sb->level == 6) {
156 c = map_num(r6layout, sb->new_layout);
157 printf(" New Layout : %s\n", c?c:"-unknown-");
158 }
b674b5b8 159 if (sb->level == 10) {
b578481c
NB
160 printf(" New Layout : near=%d, %s=%d\n",
161 sb->new_layout&255,
162 (sb->new_layout&0x10000)?"offset":"far",
163 (sb->new_layout>>8)&255);
b674b5b8
NB
164 }
165 }
166 if (sb->new_chunk != sb->chunk_size)
167 printf(" New Chunksize : %d\n", sb->new_chunk);
168 printf("\n");
169 }
4b1ac34b
NB
170 atime = sb->utime;
171 printf(" Update Time : %.24s\n", ctime(&atime));
172 printf(" State : %s\n",
173 (sb->state&(1<<MD_SB_CLEAN))?"clean":"active");
55935d51
NB
174 if (sb->state & (1<<MD_SB_BITMAP_PRESENT))
175 printf("Internal Bitmap : present\n");
4b1ac34b
NB
176 printf(" Active Devices : %d\n", sb->active_disks);
177 printf("Working Devices : %d\n", sb->working_disks);
178 printf(" Failed Devices : %d\n", sb->failed_disks);
179 printf(" Spare Devices : %d\n", sb->spare_disks);
180 if (calc_sb0_csum(sb) == sb->sb_csum)
181 printf(" Checksum : %x - correct\n", sb->sb_csum);
182 else
183 printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb));
1486e43f
NB
184 printf(" Events : %llu\n",
185 ((unsigned long long)sb->events_hi << 32)
186 + sb->events_lo);
4b1ac34b
NB
187 printf("\n");
188 if (sb->level == 5) {
189 c = map_num(r5layout, sb->layout);
190 printf(" Layout : %s\n", c?c:"-unknown-");
191 }
fe77a154
N
192 if (sb->level == 6) {
193 c = map_num(r6layout, sb->layout);
194 printf(" Layout : %s\n", c?c:"-unknown-");
195 }
265e0f17 196 if (sb->level == 10) {
e4965ef8
N
197 printf(" Layout :");
198 print_r10_layout(sb->layout);
199 printf("\n");
265e0f17 200 }
4b1ac34b
NB
201 switch(sb->level) {
202 case 0:
203 case 4:
204 case 5:
2c102711
NB
205 case 6:
206 case 10:
4b1ac34b
NB
207 printf(" Chunk Size : %dK\n", sb->chunk_size/1024);
208 break;
209 case -1:
210 printf(" Rounding : %dK\n", sb->chunk_size/1024);
211 break;
353632d9 212 default: break;
4b1ac34b
NB
213 }
214 printf("\n");
215 printf(" Number Major Minor RaidDevice State\n");
4180aa4d 216 for (d= -1; d<(signed int)(sb->raid_disks+delta_extra + sb->spare_disks); d++) {
4b1ac34b
NB
217 mdp_disk_t *dp;
218 char *dv;
219 char nb[5];
dfd4d8ee 220 int wonly;
4b1ac34b
NB
221 if (d>=0) dp = &sb->disks[d];
222 else dp = &sb->this_disk;
8f23b0b3 223 snprintf(nb, sizeof(nb), "%4d", d);
4b1ac34b
NB
224 printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb,
225 dp->number, dp->major, dp->minor, dp->raid_disk);
dfd4d8ee
NB
226 wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY);
227 dp->state &= ~(1<<MD_DISK_WRITEMOSTLY);
4b1ac34b
NB
228 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
229 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
230 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
231 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
dfd4d8ee 232 if (wonly) printf(" write-mostly");
4b1ac34b 233 if (dp->state == 0) printf(" spare");
16c6fa80 234 if ((dv=map_dev(dp->major, dp->minor, 0)))
4b1ac34b
NB
235 printf(" %s", dv);
236 printf("\n");
237 if (d == -1) printf("\n");
238 }
239}
240
061f2c6a 241static void brief_examine_super0(struct supertype *st, int verbose)
4b1ac34b 242{
3da92f27 243 mdp_super_t *sb = st->sb;
4b1ac34b 244 char *c=map_num(pers, sb->level);
2998bc01 245 char devname[20];
2998bc01
NB
246
247 sprintf(devname, "/dev/md%d", sb->md_minor);
4b1ac34b 248
061f2c6a
N
249 if (verbose) {
250 printf("ARRAY %s level=%s num-devices=%d",
251 devname,
252 c?c:"-unknown-", sb->raid_disks);
253 } else
254 printf("ARRAY %s", devname);
255
4b1ac34b 256 if (sb->minor_version >= 90)
061f2c6a 257 printf(" UUID=%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
4b1ac34b
NB
258 sb->set_uuid2, sb->set_uuid3);
259 else
061f2c6a 260 printf(" UUID=%08x", sb->set_uuid0);
4b1ac34b
NB
261 printf("\n");
262}
263
0d726f17
KS
264static void export_examine_super0(struct supertype *st)
265{
266 mdp_super_t *sb = st->sb;
267
268 printf("MD_LEVEL=%s\n", map_num(pers, sb->level));
269 printf("MD_DEVICES=%d\n", sb->raid_disks);
270 if (sb->minor_version >= 90)
271 printf("MD_UUID=%08x:%08x:%08x:%08x\n",
272 sb->set_uuid0, sb->set_uuid1,
273 sb->set_uuid2, sb->set_uuid3);
274 else
275 printf("MD_UUID=%08x\n", sb->set_uuid0);
276 printf("MD_UPDATE_TIME=%llu\n",
277 __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL);
278 printf("MD_EVENTS=%llu\n",
279 ((unsigned long long)sb->events_hi << 32)
280 + sb->events_lo);
281}
282
74db60b0
N
283static int copy_metadata0(struct supertype *st, int from, int to)
284{
285 /* Read 64K from the appropriate offset of 'from'
286 * and if it looks a little like a 0.90 superblock,
287 * write it to the same offset of 'to'
288 */
289 void *buf;
290 unsigned long long dsize, offset;
291 const int bufsize = 64*1024;
292 mdp_super_t *super;
293
294 if (posix_memalign(&buf, 4096, bufsize) != 0)
295 return 1;
296
297 if (!get_dev_size(from, NULL, &dsize))
298 goto err;
299
300 if (dsize < MD_RESERVED_SECTORS*512)
301 goto err;
302
303 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
304
305 offset *= 512;
306
307 if (lseek64(from, offset, 0) < 0LL)
308 goto err;
309 if (read(from, buf, bufsize) != bufsize)
310 goto err;
311
312 if (lseek64(to, offset, 0) < 0LL)
313 goto err;
314 super = buf;
315 if (super->md_magic != MD_SB_MAGIC ||
316 super->major_version != 0 ||
317 calc_sb0_csum(super) != super->sb_csum)
318 goto err;
319 if (write(to, buf, bufsize) != bufsize)
320 goto err;
321 free(buf);
322 return 0;
323err:
324 free(buf);
325 return 1;
326}
327
3da92f27 328static void detail_super0(struct supertype *st, char *homehost)
4b1ac34b 329{
3da92f27 330 mdp_super_t *sb = st->sb;
4b1ac34b
NB
331 printf(" UUID : ");
332 if (sb->minor_version >= 90)
333 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
334 sb->set_uuid2, sb->set_uuid3);
335 else
336 printf("%08x", sb->set_uuid0);
b6750aa8 337 if (homehost) {
8268ed74
NB
338 char buf[20];
339 void *hash = sha1_buffer(homehost,
340 strlen(homehost),
341 buf);
b6750aa8
NB
342 if (memcmp(&sb->set_uuid2, hash, 8)==0)
343 printf(" (local to host %s)", homehost);
344 }
4b1ac34b
NB
345 printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo);
346}
347
3da92f27 348static void brief_detail_super0(struct supertype *st)
4b1ac34b 349{
3da92f27 350 mdp_super_t *sb = st->sb;
4b1ac34b
NB
351 printf(" UUID=");
352 if (sb->minor_version >= 90)
353 printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1,
354 sb->set_uuid2, sb->set_uuid3);
355 else
356 printf("%08x", sb->set_uuid0);
357}
c7654afc 358#endif
83b6208e 359
3da92f27 360static int match_home0(struct supertype *st, char *homehost)
83b6208e 361{
3da92f27 362 mdp_super_t *sb = st->sb;
8268ed74 363 char buf[20];
40d28f0d
N
364 char *hash;
365
366 if (!homehost)
367 return 0;
368 hash = sha1_buffer(homehost,
369 strlen(homehost),
370 buf);
83b6208e
NB
371
372 return (memcmp(&sb->set_uuid2, hash, 8)==0);
373}
374
3da92f27 375static void uuid_from_super0(struct supertype *st, int uuid[4])
4b1ac34b 376{
3da92f27 377 mdp_super_t *super = st->sb;
4b1ac34b
NB
378 uuid[0] = super->set_uuid0;
379 if (super->minor_version >= 90) {
380 uuid[1] = super->set_uuid1;
381 uuid[2] = super->set_uuid2;
382 uuid[3] = super->set_uuid3;
383 } else {
384 uuid[1] = 0;
385 uuid[2] = 0;
386 uuid[3] = 0;
387 }
388}
389
a5d85af7 390static void getinfo_super0(struct supertype *st, struct mdinfo *info, char *map)
4b1ac34b 391{
3da92f27 392 mdp_super_t *sb = st->sb;
4b1ac34b
NB
393 int working = 0;
394 int i;
a5d85af7 395 int map_disks = info->array.raid_disks;
4b1ac34b 396
95eeceeb 397 memset(info, 0, sizeof(*info));
4b1ac34b
NB
398 info->array.major_version = sb->major_version;
399 info->array.minor_version = sb->minor_version;
400 info->array.patch_version = sb->patch_version;
401 info->array.raid_disks = sb->raid_disks;
402 info->array.level = sb->level;
265e0f17 403 info->array.layout = sb->layout;
4b1ac34b
NB
404 info->array.md_minor = sb->md_minor;
405 info->array.ctime = sb->ctime;
353632d9
NB
406 info->array.utime = sb->utime;
407 info->array.chunk_size = sb->chunk_size;
583315d9 408 info->array.state = sb->state;
353632d9 409 info->component_size = sb->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 }
5a31170d
N
628 } else if (strcmp(update, "no-bitmap") == 0) {
629 sb->state &= ~(1<<MD_SB_BITMAP_PRESENT);
1e2b2765 630 } else if (strcmp(update, "_reshape_progress")==0)
353632d9 631 sb->reshape_position = info->reshape_progress;
16715c01
DL
632 else if (strcmp(update, "writemostly")==0)
633 sb->state |= (1<<MD_DISK_WRITEMOSTLY);
634 else if (strcmp(update, "readwrite")==0)
635 sb->state &= ~(1<<MD_DISK_WRITEMOSTLY);
1e2b2765
N
636 else
637 rv = -1;
4b1ac34b
NB
638
639 sb->sb_csum = calc_sb0_csum(sb);
640 return rv;
641}
642
05697ec1
NB
643/*
644 * For verion-0 superblock, the homehost is 'stored' in the
645 * uuid. 8 bytes for a hash of the host leaving 8 bytes
646 * of random material.
647 * We use the first 8 bytes (64bits) of the sha1 of the
648 * host name
649 */
4b1ac34b
NB
650
651
3da92f27 652static int init_super0(struct supertype *st, mdu_array_info_t *info,
3d3dd91e 653 unsigned long long size, char *ignored_name, char *homehost,
83cd1e97 654 int *uuid, unsigned long long data_offset)
4b1ac34b 655{
6416d527 656 mdp_super_t *sb;
82d9eba6 657 int spares;
6416d527 658
83cd1e97 659 if (data_offset != INVALID_SECTORS) {
ed503f89 660 pr_err("data-offset not support for 0.90\n");
83cd1e97
N
661 return 0;
662 }
663
b550887f
N
664 if (posix_memalign((void**)&sb, 4096,
665 MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
e7b84f9d 666 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
667 return 0;
668 }
55935d51 669 memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
4b1ac34b 670
64557c33 671 st->sb = sb;
ba7eb04f 672 if (info == NULL) {
f9ce90ba 673 /* zeroing the superblock */
82d9eba6
NB
674 return 0;
675 }
676
677 spares = info->working_disks - info->active_disks;
678 if (info->raid_disks + spares > MD_SB_DISKS) {
e7b84f9d 679 pr_err("too many devices requested: %d+%d > %d\n",
82d9eba6
NB
680 info->raid_disks , spares, MD_SB_DISKS);
681 return 0;
f9ce90ba
NB
682 }
683
4b1ac34b
NB
684 sb->md_magic = MD_SB_MAGIC;
685 sb->major_version = 0;
686 sb->minor_version = 90;
687 sb->patch_version = 0;
688 sb->gvalid_words = 0; /* ignored */
4b1ac34b
NB
689 sb->ctime = time(0);
690 sb->level = info->level;
63ebe78f
N
691 sb->size = size;
692 if (size != (unsigned long long)sb->size)
5dd497ee 693 return 0;
4b1ac34b
NB
694 sb->nr_disks = info->nr_disks;
695 sb->raid_disks = info->raid_disks;
696 sb->md_minor = info->md_minor;
697 sb->not_persistent = 0;
3d3dd91e
NB
698 if (uuid) {
699 sb->set_uuid0 = uuid[0];
700 sb->set_uuid1 = uuid[1];
701 sb->set_uuid2 = uuid[2];
702 sb->set_uuid3 = uuid[3];
703 } else {
704 int rfd = open("/dev/urandom", O_RDONLY);
705 if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
706 sb->set_uuid0 = random();
707 if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
708 sb->set_uuid1 = random();
709 sb->set_uuid2 = random();
710 sb->set_uuid3 = random();
711 }
712 if (rfd >= 0)
713 close(rfd);
dfe47e00 714 }
30d48159 715 if (homehost && !uuid) {
8268ed74
NB
716 char buf[20];
717 char *hash = sha1_buffer(homehost,
718 strlen(homehost),
719 buf);
05697ec1
NB
720 memcpy(&sb->set_uuid2, hash, 8);
721 }
4b1ac34b
NB
722
723 sb->utime = sb->ctime;
724 sb->state = info->state;
725 sb->active_disks = info->active_disks;
726 sb->working_disks = info->working_disks;
727 sb->failed_disks = info->failed_disks;
234a131a 728 sb->spare_disks = info->spare_disks;
4b1ac34b
NB
729 sb->events_hi = 0;
730 sb->events_lo = 1;
731
732 sb->layout = info->layout;
733 sb->chunk_size = info->chunk_size;
734
82d9eba6 735 return 1;
4b1ac34b
NB
736}
737
111d01fc
NB
738struct devinfo {
739 int fd;
740 char *devname;
741 mdu_disk_info_t disk;
742 struct devinfo *next;
743};
0e600426
N
744
745#ifndef MDASSEMBLE
4b1ac34b 746/* Add a device to the superblock being created */
f20c3968 747static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo,
72ca9bcf 748 int fd, char *devname, unsigned long long data_offset)
4b1ac34b 749{
3da92f27 750 mdp_super_t *sb = st->sb;
4b1ac34b 751 mdp_disk_t *dk = &sb->disks[dinfo->number];
111d01fc 752 struct devinfo *di, **dip;
353632d9 753
4b1ac34b
NB
754 dk->number = dinfo->number;
755 dk->major = dinfo->major;
756 dk->minor = dinfo->minor;
757 dk->raid_disk = dinfo->raid_disk;
758 dk->state = dinfo->state;
16715c01
DL
759 /* In case our source disk was writemostly, don't copy that bit */
760 dk->state &= ~(1<<MD_DISK_WRITEMOSTLY);
111d01fc 761
d2ca6449
NB
762 sb->this_disk = sb->disks[dinfo->number];
763 sb->sb_csum = calc_sb0_csum(sb);
764
111d01fc
NB
765 dip = (struct devinfo **)&st->info;
766 while (*dip)
767 dip = &(*dip)->next;
503975b9 768 di = xmalloc(sizeof(struct devinfo));
111d01fc
NB
769 di->fd = fd;
770 di->devname = devname;
771 di->disk = *dinfo;
772 di->next = NULL;
773 *dip = di;
f20c3968
DW
774
775 return 0;
4b1ac34b 776}
0e600426 777#endif
4b1ac34b 778
3da92f27 779static int store_super0(struct supertype *st, int fd)
4b1ac34b 780{
4b1ac34b
NB
781 unsigned long long dsize;
782 unsigned long long offset;
3da92f27 783 mdp_super_t *super = st->sb;
beae1dfe
NB
784
785 if (!get_dev_size(fd, NULL, &dsize))
786 return 1;
4b1ac34b 787
eb6dae98 788 if (dsize < MD_RESERVED_SECTORS*512)
4b1ac34b 789 return 2;
353632d9 790
4b1ac34b
NB
791 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
792
793 offset *= 512;
794
795 if (lseek64(fd, offset, 0)< 0LL)
796 return 3;
797
798 if (write(fd, super, sizeof(*super)) != sizeof(*super))
799 return 4;
800
14263cf1
NB
801 if (super->state & (1<<MD_SB_BITMAP_PRESENT)) {
802 struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1);
803 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC)
b550887f
N
804 if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) !=
805 ROUND_UP(sizeof(*bm),4096))
9fca7d62 806 return 5;
14263cf1
NB
807 }
808
570c0542 809 fsync(fd);
4b1ac34b
NB
810 return 0;
811}
812
111d01fc
NB
813#ifndef MDASSEMBLE
814static int write_init_super0(struct supertype *st)
4b1ac34b 815{
3da92f27 816 mdp_super_t *sb = st->sb;
111d01fc
NB
817 int rv = 0;
818 struct devinfo *di;
4b1ac34b 819
111d01fc 820 for (di = st->info ; di && ! rv ; di = di->next) {
4b1ac34b 821
111d01fc
NB
822 if (di->disk.state == 1)
823 continue;
a0c8a17f
NB
824 if (di->fd == -1)
825 continue;
ba728be7 826 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
9277cc77 827 ;
8d75b7fc 828
111d01fc 829 sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY);
55935d51 830
111d01fc
NB
831 sb->this_disk = sb->disks[di->disk.number];
832 sb->sb_csum = calc_sb0_csum(sb);
833 rv = store_super0(st, di->fd);
55935d51 834
111d01fc
NB
835 if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT)))
836 rv = st->ss->write_bitmap(st, di->fd);
837
838 if (rv)
e7b84f9d
N
839 pr_err("failed to write superblock to %s\n",
840 di->devname);
111d01fc 841 }
4b1ac34b
NB
842 return rv;
843}
111d01fc 844#endif
4b1ac34b 845
64557c33 846static int compare_super0(struct supertype *st, struct supertype *tst)
4b1ac34b
NB
847{
848 /*
849 * return:
850 * 0 same, or first was empty, and second was copied
851 * 1 second had wrong number
852 * 2 wrong uuid
853 * 3 wrong other info
854 */
64557c33
NB
855 mdp_super_t *first = st->sb;
856 mdp_super_t *second = tst->sb;
4b1ac34b 857 int uuid1[4], uuid2[4];
64557c33 858
4b1ac34b
NB
859 if (second->md_magic != MD_SB_MAGIC)
860 return 1;
861 if (!first) {
b550887f
N
862 if (posix_memalign((void**)&first, 4096,
863 MD_SB_BYTES +
864 ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) {
e7b84f9d 865 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
866 return 1;
867 }
14263cf1 868 memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s));
64557c33 869 st->sb = first;
4b1ac34b
NB
870 return 0;
871 }
872
3da92f27
NB
873 uuid_from_super0(st, uuid1);
874 uuid_from_super0(tst, uuid2);
f277ce36 875 if (!same_uuid(uuid1, uuid2, 0))
4b1ac34b
NB
876 return 2;
877 if (first->major_version != second->major_version ||
878 first->minor_version != second->minor_version ||
879 first->patch_version != second->patch_version ||
880 first->gvalid_words != second->gvalid_words ||
881 first->ctime != second->ctime ||
882 first->level != second->level ||
883 first->size != second->size ||
884 first->raid_disks != second->raid_disks )
885 return 3;
886
887 return 0;
888}
889
890
3da92f27
NB
891static void free_super0(struct supertype *st);
892
893static int load_super0(struct supertype *st, int fd, char *devname)
4b1ac34b
NB
894{
895 /* try to read in the superblock
896 * Return:
897 * 0 on success
898 * 1 on cannot get superblock
899 * 2 on superblock meaningless
900 */
4b1ac34b
NB
901 unsigned long long dsize;
902 unsigned long long offset;
903 mdp_super_t *super;
14263cf1
NB
904 int uuid[4];
905 struct bitmap_super_s *bsb;
aba69144 906
3da92f27
NB
907 free_super0(st);
908
beae1dfe
NB
909 if (!get_dev_size(fd, devname, &dsize))
910 return 1;
4b1ac34b 911
eb6dae98 912 if (dsize < MD_RESERVED_SECTORS*512) {
4b1ac34b 913 if (devname)
e7b84f9d
N
914 pr_err("%s is too small for md: size is %llu sectors.\n",
915 devname, dsize);
4b1ac34b
NB
916 return 1;
917 }
353632d9 918
4b1ac34b
NB
919 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
920
921 offset *= 512;
922
4b1ac34b
NB
923 if (lseek64(fd, offset, 0)< 0LL) {
924 if (devname)
e7b84f9d 925 pr_err("Cannot seek to superblock on %s: %s\n",
4b1ac34b
NB
926 devname, strerror(errno));
927 return 1;
928 }
929
b550887f
N
930 if (posix_memalign((void**)&super, 4096,
931 MD_SB_BYTES +
932 ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) {
e7b84f9d 933 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
934 return 1;
935 }
4b1ac34b
NB
936
937 if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) {
938 if (devname)
e7b84f9d 939 pr_err("Cannot read superblock on %s\n",
4b1ac34b
NB
940 devname);
941 free(super);
942 return 1;
943 }
944
586ed405
NB
945 if (st->ss && st->minor_version == 9)
946 super0_swap_endian(super);
947
4b1ac34b
NB
948 if (super->md_magic != MD_SB_MAGIC) {
949 if (devname)
e7b84f9d 950 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
4b1ac34b
NB
951 devname, MD_SB_MAGIC, super->md_magic);
952 free(super);
953 return 2;
954 }
955
956 if (super->major_version != 0) {
957 if (devname)
e7b84f9d 958 pr_err("Cannot interpret superblock on %s - version is %d\n",
4b1ac34b
NB
959 devname, super->major_version);
960 free(super);
961 return 2;
962 }
64557c33 963 st->sb = super;
3da92f27 964
82d9eba6
NB
965 if (st->ss == NULL) {
966 st->ss = &super0;
5f98d3cb 967 st->minor_version = super->minor_version;
ea329559 968 st->max_devs = MD_SB_DISKS;
111d01fc 969 st->info = NULL;
82d9eba6
NB
970 }
971
14263cf1
NB
972 /* Now check on the bitmap superblock */
973 if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0)
974 return 0;
975 /* Read the bitmap superblock and make sure it looks
976 * valid. If it doesn't clear the bit. An --assemble --force
977 * should get that written out.
978 */
b550887f
N
979 if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096))
980 != ROUND_UP(sizeof(struct bitmap_super_s),4096))
14263cf1
NB
981 goto no_bitmap;
982
3da92f27 983 uuid_from_super0(st, uuid);
14263cf1
NB
984 bsb = (struct bitmap_super_s *)(super+1);
985 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
986 memcmp(bsb->uuid, uuid, 16) != 0)
987 goto no_bitmap;
988 return 0;
989
990 no_bitmap:
991 super->state &= ~(1<<MD_SB_BITMAP_PRESENT);
992
4b1ac34b
NB
993 return 0;
994}
f9ce90ba 995
82d9eba6 996static struct supertype *match_metadata_desc0(char *arg)
f9ce90ba 997{
503975b9 998 struct supertype *st = xcalloc(1, sizeof(*st));
82d9eba6 999
4dd2df09 1000 st->container_devnm[0] = 0;
82d9eba6 1001 st->ss = &super0;
111d01fc 1002 st->info = NULL;
82d9eba6 1003 st->minor_version = 90;
ea329559 1004 st->max_devs = MD_SB_DISKS;
64557c33 1005 st->sb = NULL;
9b2a22d3
N
1006 /* we sometimes get 00.90 */
1007 while (arg[0] == '0' && arg[1] == '0')
1008 arg++;
f9ce90ba 1009 if (strcmp(arg, "0") == 0 ||
26f467a9 1010#ifdef DEFAULT_OLD_METADATA /* ifndef in super1.c */
1011 strcmp(arg, "default") == 0 ||
1012#endif /* DEFAULT_OLD_METADATA */
50827504
N
1013 strcmp(arg, "0.90") == 0 ||
1014 strcmp(arg, "") == 0 /* no metadata - i.e. non_persistent */
f9ce90ba 1015 )
82d9eba6
NB
1016 return st;
1017
56f8add2
NB
1018 st->minor_version = 91; /* reshape in progress */
1019 if (strcmp(arg, "0.91") == 0) /* For dup_super support */
1020 return st;
1021
586ed405 1022 st->minor_version = 9; /* flag for 'byte-swapped' */
ff1f6545
NB
1023 if (strcmp(arg, "0.swap")==0 ||
1024 strcmp(arg, "0.9") == 0) /* For dup_super support */
586ed405
NB
1025 return st;
1026
82d9eba6
NB
1027 free(st);
1028 return NULL;
1029}
1030
387fcd59
N
1031static __u64 avail_size0(struct supertype *st, __u64 devsize,
1032 unsigned long long data_offset)
82d9eba6 1033{
387fcd59
N
1034 if (data_offset != 0 && data_offset != INVALID_SECTORS)
1035 return 0ULL;
eb6dae98 1036 if (devsize < MD_RESERVED_SECTORS)
82d9eba6
NB
1037 return 0ULL;
1038 return MD_NEW_SIZE_SECTORS(devsize);
f9ce90ba
NB
1039}
1040
3da92f27 1041static int add_internal_bitmap0(struct supertype *st, int *chunkp,
199171a2
NB
1042 int delay, int write_behind,
1043 unsigned long long size, int may_change,
1044 int major)
55935d51
NB
1045{
1046 /*
1047 * The bitmap comes immediately after the superblock and must be 60K in size
1048 * at most. The default size is between 30K and 60K
1049 *
8686f3ed 1050 * size is in sectors, chunk is in bytes !!!
55935d51 1051 */
1bf4e2d9 1052 unsigned long long bits;
50526e90 1053 unsigned long long max_bits = (60*1024 - sizeof(bitmap_super_t))*8;
55935d51 1054 unsigned long long min_chunk;
199171a2 1055 int chunk = *chunkp;
3da92f27 1056 mdp_super_t *sb = st->sb;
55935d51 1057 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES);
3b7e9d0c 1058 int uuid[4];
55935d51 1059
353632d9 1060
f277ce36 1061 min_chunk = 4096; /* sub-page chunks don't work yet.. */
8686f3ed 1062 bits = (size * 512) / min_chunk + 1;
55935d51
NB
1063 while (bits > max_bits) {
1064 min_chunk *= 2;
1065 bits = (bits+1)/2;
1066 }
b8ab2a50
N
1067 if (chunk == UnSet) {
1068 /* A chunk size less than a few Megabytes gives poor
1069 * performance without increasing resync noticeably
1070 */
55935d51 1071 chunk = min_chunk;
b8ab2a50
N
1072 if (chunk < 64*1024*1024)
1073 chunk = 64*1024*1024;
f21e18ca 1074 } else if ((unsigned long long)chunk < min_chunk)
55935d51
NB
1075 return 0; /* chunk size too small */
1076
1077 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1078
29e766a5 1079 memset(bms, 0, sizeof(*bms));
dfd4d8ee 1080 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1081 bms->version = __cpu_to_le32(major);
3b7e9d0c
LB
1082 uuid_from_super0(st, uuid);
1083 memcpy(bms->uuid, uuid, 16);
dfd4d8ee
NB
1084 bms->chunksize = __cpu_to_le32(chunk);
1085 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1086 bms->sync_size = __cpu_to_le64(size);
dfd4d8ee 1087 bms->write_behind = __cpu_to_le32(write_behind);
199171a2 1088 *chunkp = chunk;
55935d51
NB
1089 return 1;
1090}
353632d9 1091
55935d51 1092
ec9688ca 1093static void locate_bitmap0(struct supertype *st, int fd)
55935d51
NB
1094{
1095 unsigned long long dsize;
55935d51 1096 unsigned long long offset;
55935d51 1097
beae1dfe
NB
1098 if (!get_dev_size(fd, NULL, &dsize))
1099 return;
1100
eb6dae98 1101 if (dsize < MD_RESERVED_SECTORS*512)
55935d51 1102 return;
353632d9 1103
55935d51
NB
1104 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1105
1106 offset *= 512;
1107
1108 offset += MD_SB_BYTES;
1109
1110 lseek64(fd, offset, 0);
1111}
1112
ec9688ca 1113static int write_bitmap0(struct supertype *st, int fd)
8d75b7fc 1114{
8d75b7fc
NB
1115 unsigned long long dsize;
1116 unsigned long long offset;
3da92f27 1117 mdp_super_t *sb = st->sb;
aba69144 1118
8d75b7fc
NB
1119 int rv = 0;
1120
1121 int towrite, n;
d669228f 1122 void *buf;
8d75b7fc 1123
beae1dfe
NB
1124 if (!get_dev_size(fd, NULL, &dsize))
1125 return 1;
1126
eb6dae98
NB
1127 if (dsize < MD_RESERVED_SECTORS*512)
1128 return -1;
353632d9 1129
8d75b7fc
NB
1130 offset = MD_NEW_SIZE_SECTORS(dsize>>9);
1131
1132 offset *= 512;
1133
1134 if (lseek64(fd, offset + 4096, 0)< 0LL)
1135 return 3;
1136
d669228f
JS
1137 if (posix_memalign(&buf, 4096, 4096))
1138 return -ENOMEM;
1139
6416d527
NB
1140 memset(buf, 0xff, 4096);
1141 memcpy(buf, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t));
50526e90 1142 towrite = 60*1024;
8d75b7fc
NB
1143 while (towrite > 0) {
1144 n = towrite;
6416d527
NB
1145 if (n > 4096)
1146 n = 4096;
8d75b7fc
NB
1147 n = write(fd, buf, n);
1148 if (n > 0)
1149 towrite -= n;
1150 else
1151 break;
6416d527 1152 memset(buf, 0xff, 4096);
8d75b7fc 1153 }
dfd4d8ee 1154 fsync(fd);
8d75b7fc
NB
1155 if (towrite)
1156 rv = -2;
1157
d669228f 1158 free(buf);
8d75b7fc
NB
1159 return rv;
1160}
55935d51 1161
3da92f27 1162static void free_super0(struct supertype *st)
df37ffc0 1163{
3da92f27
NB
1164 if (st->sb)
1165 free(st->sb);
1cc7f4fe
N
1166 while (st->info) {
1167 struct devinfo *di = st->info;
1168 st->info = di->next;
1169 if (di->fd >= 0)
1170 close(di->fd);
1171 free(di);
1172 }
3da92f27 1173 st->sb = NULL;
df37ffc0
NB
1174}
1175
0e600426 1176#ifndef MDASSEMBLE
17f25ca6
NB
1177static int validate_geometry0(struct supertype *st, int level,
1178 int layout, int raiddisks,
c21e737b 1179 int *chunk, unsigned long long size,
af4348dd 1180 unsigned long long data_offset,
2c514b71
NB
1181 char *subdev, unsigned long long *freesize,
1182 int verbose)
17f25ca6
NB
1183{
1184 unsigned long long ldsize;
1185 int fd;
01619b48
N
1186 unsigned int tbmax = 4;
1187
1188 /* prior to linux 3.1, a but limits usable device size to 2TB.
1189 * It was introduced in 2.6.29, but we won't worry about that detail
1190 */
1191 if (get_linux_version() < 3001000)
1192 tbmax = 2;
17f25ca6 1193
b42f577a
N
1194 if (level == LEVEL_CONTAINER) {
1195 if (verbose)
e7b84f9d 1196 pr_err("0.90 metadata does not support containers\n");
17f25ca6 1197 return 0;
b42f577a
N
1198 }
1199 if (raiddisks > MD_SB_DISKS) {
1200 if (verbose)
e7b84f9d 1201 pr_err("0.90 metadata supports at most %d devices per array\n",
b42f577a 1202 MD_SB_DISKS);
17f25ca6 1203 return 0;
b42f577a 1204 }
9c8c1218 1205 if (size >= tbmax * 2ULL*1024*1024*1024) {
b42f577a 1206 if (verbose)
e7b84f9d
N
1207 pr_err("0.90 metadata supports at most "
1208 "%d terabytes per device\n", tbmax);
17f25ca6 1209 return 0;
b42f577a 1210 }
bb7295f1
N
1211 if (chunk && *chunk == UnSet)
1212 *chunk = DEFAULT_CHUNK;
1213
17f25ca6
NB
1214 if (!subdev)
1215 return 1;
1216
1217 fd = open(subdev, O_RDONLY|O_EXCL, 0);
1218 if (fd < 0) {
2c514b71 1219 if (verbose)
e7b84f9d 1220 pr_err("super0.90 cannot open %s: %s\n",
2c514b71 1221 subdev, strerror(errno));
17f25ca6
NB
1222 return 0;
1223 }
2c514b71 1224
17f25ca6
NB
1225 if (!get_dev_size(fd, subdev, &ldsize)) {
1226 close(fd);
1227 return 0;
1228 }
1229 close(fd);
1230
1231 if (ldsize < MD_RESERVED_SECTORS * 512)
1232 return 0;
17f25ca6
NB
1233 *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9);
1234 return 1;
1235}
0e600426 1236#endif /* MDASSEMBLE */
17f25ca6 1237
f9ce90ba 1238struct superswitch super0 = {
c7654afc 1239#ifndef MDASSEMBLE
f9ce90ba
NB
1240 .examine_super = examine_super0,
1241 .brief_examine_super = brief_examine_super0,
0d726f17 1242 .export_examine_super = export_examine_super0,
f9ce90ba
NB
1243 .detail_super = detail_super0,
1244 .brief_detail_super = brief_detail_super0,
111d01fc 1245 .write_init_super = write_init_super0,
0e600426
N
1246 .validate_geometry = validate_geometry0,
1247 .add_to_super = add_to_super0,
74db60b0 1248 .copy_metadata = copy_metadata0,
c7654afc 1249#endif
83b6208e 1250 .match_home = match_home0,
f9ce90ba
NB
1251 .uuid_from_super = uuid_from_super0,
1252 .getinfo_super = getinfo_super0,
00bbdbda 1253 .container_content = container_content0,
f9ce90ba 1254 .update_super = update_super0,
f9ce90ba 1255 .init_super = init_super0,
f9ce90ba 1256 .store_super = store_super0,
f9ce90ba
NB
1257 .compare_super = compare_super0,
1258 .load_super = load_super0,
1259 .match_metadata_desc = match_metadata_desc0,
82d9eba6 1260 .avail_size = avail_size0,
55935d51
NB
1261 .add_internal_bitmap = add_internal_bitmap0,
1262 .locate_bitmap = locate_bitmap0,
8d75b7fc 1263 .write_bitmap = write_bitmap0,
df37ffc0 1264 .free_super = free_super0,
4cce4069 1265 .name = "0.90",
f9ce90ba 1266};