]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super1.c
sysfs: Use the presence of /sys/block/<dev>/md as indicator of valid device
[thirdparty/mdadm.git] / super1.c
CommitLineData
82d9eba6
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
7071320a 4 * Copyright (C) 2001-2016 Neil Brown <neilb@suse.com>
82d9eba6
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>
82d9eba6
NB
23 */
24
04f903b2 25#include <stddef.h>
82d9eba6 26#include "mdadm.h"
82d9eba6
NB
27/*
28 * The version-1 superblock :
29 * All numeric fields are little-endian.
30 *
31 * total size: 256 bytes plus 2 per device.
32 * 1K allows 384 devices.
33 */
34struct mdp_superblock_1 {
35 /* constant array information - 128 bytes */
36 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
37 __u32 major_version; /* 1 */
38 __u32 feature_map; /* 0 for now */
39 __u32 pad0; /* always set to 0 when writing */
40
41 __u8 set_uuid[16]; /* user-space generated. */
42 char set_name[32]; /* set and interpreted by user-space */
43
44 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
45 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
46 __u32 layout; /* only for raid5 currently */
47 __u64 size; /* used size of component devices, in 512byte sectors */
48
49 __u32 chunksize; /* in 512byte sectors */
50 __u32 raid_disks;
e97a7cd0
AP
51 union {
52 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
53 * NOTE: signed, so bitmap can be before superblock
54 * only meaningful of feature_map[0] is set.
55 */
56
57 /* only meaningful when feature_map[MD_FEATURE_PPL] is set */
58 struct {
59 __s16 offset; /* sectors from start of superblock that ppl starts */
60 __u16 size; /* ppl size in sectors */
61 } ppl;
62 };
b674b5b8
NB
63
64 /* These are only valid with feature bit '4' */
b674b5b8 65 __u32 new_level; /* new level we are reshaping to */
ac957baa 66 __u64 reshape_position; /* next address in array-space for reshape */
b674b5b8
NB
67 __u32 delta_disks; /* change in number of raid_disks */
68 __u32 new_layout; /* new layout */
a2836f12 69 __u32 new_chunk; /* new chunk size (sectors) */
8fe1c44f
N
70 __u32 new_offset; /* signed number to add to data_offset in new
71 * layout. 0 == no-change. This can be
72 * different on each device in the array.
73 */
82d9eba6
NB
74
75 /* constant this-device information - 64 bytes */
76 __u64 data_offset; /* sector start of data, often 0 */
77 __u64 data_size; /* sectors in this device that can be used for data */
78 __u64 super_offset; /* sector start of this superblock */
cc1799c3
SL
79 union {
80 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
81 __u64 journal_tail;/* journal tail of journal device (from data_offset) */
82 };
82d9eba6
NB
83 __u32 dev_number; /* permanent identifier of this device - not role in raid */
84 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
85 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
5d500228 86 __u8 devflags; /* per-device flags. Only one defined...*/
dfd4d8ee 87#define WriteMostly1 1 /* mask for writemostly flag in above */
71574efb 88#define FailFast1 2 /* Device should get FailFast requests */
bf95d0f3
N
89 /* bad block log. If there are any bad blocks the feature flag is set.
90 * if offset and size are non-zero, that space is reserved and available.
91 */
92 __u8 bblog_shift; /* shift from sectors to block size for badblocklist */
93 __u16 bblog_size; /* number of sectors reserved for badblocklist */
94 __u32 bblog_offset; /* sector offset from superblock to bblog, signed */
82d9eba6
NB
95
96 /* array state information - 64 bytes */
97 __u64 utime; /* 40 bits second, 24 btes microseconds */
98 __u64 events; /* incremented when superblock updated */
99 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
4180aa4d
N
100 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
101 __u32 max_dev; /* size of dev_roles[] array to consider */
82d9eba6
NB
102 __u8 pad3[64-32]; /* set to 0 when writing */
103
104 /* device state information. Indexed by dev_number.
105 * 2 bytes per device
106 * Note there are no per-device state flags. State information is rolled
107 * into the 'roles' value. If a device is spare or faulty, then it doesn't
108 * have a meaningful role.
109 */
110 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
111};
112
911cead7
N
113#define MAX_SB_SIZE 4096
114/* bitmap super size is 256, but we round up to a sector for alignment */
115#define BM_SUPER_SIZE 512
116#define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
41226756
JS
117#define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
118 + sizeof(struct misc_dev_info))
911cead7 119
bee8ec56
NB
120struct misc_dev_info {
121 __u64 device_size;
122};
123
d2cd3ffc
NB
124/* feature_map bits */
125#define MD_FEATURE_BITMAP_OFFSET 1
126#define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
127 * must be honoured
128 */
b674b5b8 129#define MD_FEATURE_RESHAPE_ACTIVE 4
bf95d0f3 130#define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
8fe1c44f
N
131#define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
132 * active device with same 'role'.
133 * 'recovery_offset' is also set.
134 */
135#define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
136 * of devices, but is going
137 * backwards anyway.
138 */
139#define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
6d9c7c25 140#define MD_FEATURE_BITMAP_VERSIONED 256 /* bitmap version number checked properly */
ed94976d 141#define MD_FEATURE_JOURNAL 512 /* support write journal */
e97a7cd0 142#define MD_FEATURE_PPL 1024 /* support PPL */
8fe1c44f
N
143#define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
144 |MD_FEATURE_RECOVERY_OFFSET \
145 |MD_FEATURE_RESHAPE_ACTIVE \
146 |MD_FEATURE_BAD_BLOCKS \
147 |MD_FEATURE_REPLACEMENT \
148 |MD_FEATURE_RESHAPE_BACKWARDS \
149 |MD_FEATURE_NEW_OFFSET \
6d9c7c25 150 |MD_FEATURE_BITMAP_VERSIONED \
ed94976d 151 |MD_FEATURE_JOURNAL \
e97a7cd0 152 |MD_FEATURE_PPL \
8fe1c44f 153 )
69a48116 154
7071320a 155#ifndef MDASSEMBLE
69a48116
SL
156static int role_from_sb(struct mdp_superblock_1 *sb)
157{
158 unsigned int d;
159 int role;
160
161 d = __le32_to_cpu(sb->dev_number);
162 if (d < __le32_to_cpu(sb->max_dev))
163 role = __le16_to_cpu(sb->dev_roles[d]);
164 else
165 role = MD_DISK_ROLE_SPARE;
166 return role;
167}
7071320a 168#endif
d2cd3ffc 169
7e6e839a
GJ
170/* return how many bytes are needed for bitmap, for cluster-md each node
171 * should have it's own bitmap */
172static unsigned int calc_bitmap_size(bitmap_super_t *bms, unsigned int boundary)
173{
174 unsigned long long bits, bytes;
175
561ad559
AP
176 bits = bitmap_bits(__le64_to_cpu(bms->sync_size),
177 __le32_to_cpu(bms->chunksize));
7e6e839a
GJ
178 bytes = (bits+7) >> 3;
179 bytes += sizeof(bitmap_super_t);
180 bytes = ROUND_UP(bytes, boundary);
181
182 return bytes;
183}
184
82d9eba6
NB
185static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
186{
187 unsigned int disk_csum, csum;
188 unsigned long long newcsum;
189 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
190 unsigned int *isuper = (unsigned int*)sb;
82d9eba6
NB
191
192/* make sure I can count... */
193 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
194 offsetof(struct mdp_superblock_1, utime) != 192 ||
195 sizeof(struct mdp_superblock_1) != 256) {
196 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
197 }
198
199 disk_csum = sb->sb_csum;
200 sb->sb_csum = 0;
201 newcsum = 0;
e4c72d1d 202 for (; size>=4; size -= 4 ) {
722966c6
PC
203 newcsum += __le32_to_cpu(*isuper);
204 isuper++;
205 }
82d9eba6
NB
206
207 if (size == 2)
208 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
209
210 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
211 sb->sb_csum = disk_csum;
83205b64 212 return __cpu_to_le32(csum);
82d9eba6
NB
213}
214
308340aa
JS
215/*
216 * Information related to file descriptor used for aligned reads/writes.
217 * Cache the block size.
218 */
219struct align_fd {
220 int fd;
221 int blk_sz;
222};
223
224static void init_afd(struct align_fd *afd, int fd)
225{
226 afd->fd = fd;
32971509 227 if (!get_dev_sector_size(afd->fd, NULL, (unsigned int *)&afd->blk_sz))
308340aa
JS
228 afd->blk_sz = 512;
229}
230
5ea022a1 231static char abuf[4096+4096];
308340aa 232static int aread(struct align_fd *afd, void *buf, int len)
5ea022a1
N
233{
234 /* aligned read.
235 * On devices with a 4K sector size, we need to read
236 * the full sector and copy relevant bits into
237 * the buffer
238 */
6ef89052 239 int bsize, iosize;
5ea022a1
N
240 char *b;
241 int n;
6ef89052 242
308340aa
JS
243 bsize = afd->blk_sz;
244
245 if (!bsize || bsize > 4096 || len > 4096) {
246 if (!bsize)
7a862a02 247 fprintf(stderr, "WARNING - aread() called with invalid block size\n");
5ea022a1 248 return -1;
308340aa 249 }
0a2f1894 250 b = ROUND_UP_PTR((char *)abuf, 4096);
5ea022a1 251
6ef89052
N
252 for (iosize = 0; iosize < len; iosize += bsize)
253 ;
308340aa 254 n = read(afd->fd, b, iosize);
5ea022a1
N
255 if (n <= 0)
256 return n;
308340aa 257 lseek(afd->fd, len - n, 1);
5ea022a1
N
258 if (n > len)
259 n = len;
260 memcpy(buf, b, n);
261 return n;
262}
263
308340aa 264static int awrite(struct align_fd *afd, void *buf, int len)
5ea022a1
N
265{
266 /* aligned write.
267 * On devices with a 4K sector size, we need to write
268 * the full sector. We pre-read if the sector is larger
269 * than the write.
270 * The address must be sector-aligned.
271 */
6ef89052 272 int bsize, iosize;
5ea022a1
N
273 char *b;
274 int n;
308340aa
JS
275
276 bsize = afd->blk_sz;
277 if (!bsize || bsize > 4096 || len > 4096) {
278 if (!bsize)
7a862a02 279 fprintf(stderr, "WARNING - awrite() called with invalid block size\n");
5ea022a1 280 return -1;
308340aa 281 }
0a2f1894 282 b = ROUND_UP_PTR((char *)abuf, 4096);
5ea022a1 283
6ef89052
N
284 for (iosize = 0; iosize < len ; iosize += bsize)
285 ;
286
287 if (len != iosize) {
308340aa 288 n = read(afd->fd, b, iosize);
6ef89052
N
289 if (n <= 0)
290 return n;
308340aa 291 lseek(afd->fd, -n, 1);
6ef89052
N
292 }
293
5ea022a1 294 memcpy(b, buf, len);
308340aa 295 n = write(afd->fd, b, iosize);
5ea022a1
N
296 if (n <= 0)
297 return n;
308340aa 298 lseek(afd->fd, len - n, 1);
5ea022a1
N
299 return len;
300}
301
e97a7cd0
AP
302static inline unsigned int choose_ppl_space(int chunk)
303{
304 return (PPL_HEADER_SIZE >> 9) + (chunk > 128*2 ? chunk : 128*2);
305}
306
c7654afc 307#ifndef MDASSEMBLE
3da92f27 308static void examine_super1(struct supertype *st, char *homehost)
82d9eba6 309{
3da92f27 310 struct mdp_superblock_1 *sb = st->sb;
0aa2f15b 311 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
82d9eba6 312 time_t atime;
f21e18ca 313 unsigned int d;
e8e1c176 314 int role;
4180aa4d 315 int delta_extra = 0;
82d9eba6
NB
316 int i;
317 char *c;
a1cbd7d0 318 int l = homehost ? strlen(homehost) : 0;
83205b64 319 int layout;
a17a3de3 320 unsigned long long sb_offset;
5e1863d4 321 struct mdinfo info;
82d9eba6
NB
322
323 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
a17a3de3
DL
324 printf(" Version : 1");
325 sb_offset = __le64_to_cpu(sb->super_offset);
326 if (sb_offset <= 4)
327 printf(".1\n");
328 else if (sb_offset <= 8)
329 printf(".2\n");
330 else
331 printf(".0\n");
91eedefc 332 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
82d9eba6
NB
333 printf(" Array UUID : ");
334 for (i=0; i<16; i++) {
82d9eba6 335 if ((i&3)==0 && i != 0) printf(":");
34163fc7 336 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
337 }
338 printf("\n");
a1cbd7d0
NB
339 printf(" Name : %.32s", sb->set_name);
340 if (l > 0 && l < 32 &&
341 sb->set_name[l] == ':' &&
342 strncmp(sb->set_name, homehost, l) == 0)
343 printf(" (local to host %s)", homehost);
344 printf("\n");
8b2202de 345 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
2cf42394 346 printf(" Cluster Name : %-64s\n", bms->cluster_name);
82d9eba6
NB
347 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
348 printf(" Creation Time : %.24s\n", ctime(&atime));
349 c=map_num(pers, __le32_to_cpu(sb->level));
350 printf(" Raid Level : %s\n", c?c:"-unknown-");
351 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
352 printf("\n");
5cda0964 353 printf(" Avail Dev Size : %llu%s\n",
2fb749d1
NB
354 (unsigned long long)__le64_to_cpu(sb->data_size),
355 human_size(__le64_to_cpu(sb->data_size)<<9));
c43f7d91 356 if (__le32_to_cpu(sb->level) > 0) {
d4633e06 357 int ddsks = 0, ddsks_denom = 1;
b674b5b8
NB
358 switch(__le32_to_cpu(sb->level)) {
359 case 1: ddsks=1;break;
360 case 4:
83205b64
NB
361 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
362 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
363 case 10:
364 layout = __le32_to_cpu(sb->layout);
d4633e06
N
365 ddsks = __le32_to_cpu(sb->raid_disks);
366 ddsks_denom = (layout&255) * ((layout>>8)&255);
b674b5b8 367 }
d4633e06
N
368 if (ddsks) {
369 long long asize = __le64_to_cpu(sb->size);
370 asize = (asize << 9) * ddsks / ddsks_denom;
83205b64 371 printf(" Array Size : %llu%s\n",
d4633e06
N
372 asize >> 10, human_size(asize));
373 }
b674b5b8 374 if (sb->size != sb->data_size)
5cda0964 375 printf(" Used Dev Size : %llu%s\n",
83205b64
NB
376 (unsigned long long)__le64_to_cpu(sb->size),
377 human_size(__le64_to_cpu(sb->size)<<9));
b674b5b8 378 }
82d9eba6 379 if (sb->data_offset)
199171a2
NB
380 printf(" Data Offset : %llu sectors\n",
381 (unsigned long long)__le64_to_cpu(sb->data_offset));
8772113a
N
382 if (sb->new_offset &&
383 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
8fe1c44f
N
384 unsigned long long offset = __le64_to_cpu(sb->data_offset);
385 offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
386 printf(" New Offset : %llu sectors\n", offset);
387 }
199171a2
NB
388 printf(" Super Offset : %llu sectors\n",
389 (unsigned long long)__le64_to_cpu(sb->super_offset));
d2cd3ffc
NB
390 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
391 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
5e1863d4
N
392
393 st->ss->getinfo_super(st, &info, NULL);
394 if (info.space_after != 1 &&
395 !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
396 printf(" Unused Space : before=%llu sectors, after=%llu sectors\n",
397 info.space_before, info.space_after);
398
a3fd117c 399 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
82d9eba6
NB
400 printf(" Device UUID : ");
401 for (i=0; i<16; i++) {
82d9eba6 402 if ((i&3)==0 && i != 0) printf(":");
34163fc7 403 printf("%02x", sb->device_uuid[i]);
82d9eba6
NB
404 }
405 printf("\n");
b674b5b8 406 printf("\n");
91eedefc
NB
407 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
408 printf("Internal Bitmap : %ld sectors from superblock\n",
68754bd1 409 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
e97a7cd0
AP
410 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
411 printf(" PPL : %u sectors at offset %d sectors from superblock\n",
412 __le16_to_cpu(sb->ppl.size),
413 __le16_to_cpu(sb->ppl.offset));
8fac0577 414 }
bf95d0f3 415 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
1e0d770c 416 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
b674b5b8
NB
417 human_size(__le64_to_cpu(sb->reshape_position)<<9));
418 if (__le32_to_cpu(sb->delta_disks)) {
419 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
4180aa4d
N
420 printf(" (%d->%d)\n",
421 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
422 __le32_to_cpu(sb->raid_disks));
423 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
424 delta_extra = -__le32_to_cpu(sb->delta_disks);
b674b5b8
NB
425 }
426 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
427 c = map_num(pers, __le32_to_cpu(sb->new_level));
428 printf(" New Level : %s\n", c?c:"-unknown-");
429 }
430 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
431 if (__le32_to_cpu(sb->level) == 5) {
432 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
433 printf(" New Layout : %s\n", c?c:"-unknown-");
434 }
fe77a154
N
435 if (__le32_to_cpu(sb->level) == 6) {
436 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
437 printf(" New Layout : %s\n", c?c:"-unknown-");
438 }
b674b5b8 439 if (__le32_to_cpu(sb->level) == 10) {
e4965ef8
N
440 printf(" New Layout :");
441 print_r10_layout(__le32_to_cpu(sb->new_layout));
442 printf("\n");
b674b5b8
NB
443 }
444 }
445 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
446 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
447 printf("\n");
448 }
dfd4d8ee 449 if (sb->devflags) {
62184891 450 printf(" Flags :");
34163fc7 451 if (sb->devflags & WriteMostly1)
dfd4d8ee 452 printf(" write-mostly");
71574efb
N
453 if (sb->devflags & FailFast1)
454 printf(" failfast");
dfd4d8ee
NB
455 printf("\n");
456 }
82d9eba6
NB
457
458 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
459 printf(" Update Time : %.24s\n", ctime(&atime));
460
bf95d0f3
N
461 if (sb->bblog_size && sb->bblog_offset) {
462 printf(" Bad Block Log : %d entries available at offset %ld sectors",
463 __le16_to_cpu(sb->bblog_size)*512/8,
d6e4b44f 464 (long)(int32_t)__le32_to_cpu(sb->bblog_offset));
bf95d0f3
N
465 if (sb->feature_map &
466 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
467 printf(" - bad blocks present.");
468 printf("\n");
469 }
470
82d9eba6
NB
471 if (calc_sb_1_csum(sb) == sb->sb_csum)
472 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
473 else
474 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
475 __le32_to_cpu(calc_sb_1_csum(sb)));
570c0542 476 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
477 printf("\n");
478 if (__le32_to_cpu(sb->level) == 5) {
479 c = map_num(r5layout, __le32_to_cpu(sb->layout));
480 printf(" Layout : %s\n", c?c:"-unknown-");
481 }
fe77a154
N
482 if (__le32_to_cpu(sb->level) == 6) {
483 c = map_num(r6layout, __le32_to_cpu(sb->layout));
484 printf(" Layout : %s\n", c?c:"-unknown-");
485 }
265e0f17
NB
486 if (__le32_to_cpu(sb->level) == 10) {
487 int lo = __le32_to_cpu(sb->layout);
e4965ef8
N
488 printf(" Layout :");
489 print_r10_layout(lo);
490 printf("\n");
265e0f17 491 }
82d9eba6
NB
492 switch(__le32_to_cpu(sb->level)) {
493 case 0:
494 case 4:
495 case 5:
d2cd3ffc
NB
496 case 6:
497 case 10:
b674b5b8 498 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
499 break;
500 case -1:
b674b5b8 501 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
82d9eba6
NB
502 break;
503 default: break;
504 }
505 printf("\n");
e8e1c176
N
506#if 0
507 /* This turns out to just be confusing */
c3684618
NB
508 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
509 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
fa7574f6 510 if (__le16_to_cpu(sb->dev_roles[i-1]) != MD_DISK_ROLE_SPARE)
c3684618
NB
511 break;
512 for (d=0; d < i; d++) {
513 int role = __le16_to_cpu(sb->dev_roles[d]);
514 if (d) printf(", ");
fa7574f6
SL
515 if (role == MD_DISK_ROLE_SPARE) printf("empty");
516 else if(role == MD_DISK_ROLE_FAULTY) printf("failed");
c3684618
NB
517 else printf("%d", role);
518 }
519 printf(")\n");
e8e1c176
N
520#endif
521 printf(" Device Role : ");
69a48116 522 role = role_from_sb(sb);
fa7574f6 523 if (role >= MD_DISK_ROLE_FAULTY)
e8e1c176 524 printf("spare\n");
ed94976d
SL
525 else if (role == MD_DISK_ROLE_JOURNAL)
526 printf("Journal\n");
24c7bc84
N
527 else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
528 printf("Replacement device %d\n", role);
e8e1c176
N
529 else
530 printf("Active device %d\n", role);
531
82d9eba6 532 printf(" Array State : ");
4180aa4d 533 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
82d9eba6 534 int cnt = 0;
f21e18ca 535 unsigned int i;
82d9eba6 536 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
f21e18ca 537 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
e4c72d1d 538 if (role == d)
82d9eba6 539 cnt++;
82d9eba6 540 }
24c7bc84
N
541 if (cnt == 2)
542 printf("R");
543 else if (cnt == 1)
544 printf("A");
545 else if (cnt == 0)
546 printf(".");
547 else
548 printf("?");
82d9eba6 549 }
e8e1c176
N
550#if 0
551 /* This is confusing too */
6fbba4c9 552 faulty = 0;
82d9eba6
NB
553 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
554 int role = __le16_to_cpu(sb->dev_roles[i]);
fa7574f6 555 if (role == MD_DISK_ROLE_FAULTY)
6fbba4c9 556 faulty++;
82d9eba6 557 }
82d9eba6 558 if (faulty) printf(" %d failed", faulty);
e8e1c176 559#endif
24c7bc84 560 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
82d9eba6
NB
561 printf("\n");
562}
563
061f2c6a 564static void brief_examine_super1(struct supertype *st, int verbose)
82d9eba6 565{
3da92f27 566 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 567 int i;
a17a3de3 568 unsigned long long sb_offset;
2998bc01 569 char *nm;
b015e6c2 570 char *c=map_num(pers, __le32_to_cpu(sb->level));
2998bc01
NB
571
572 nm = strchr(sb->set_name, ':');
573 if (nm)
574 nm++;
575 else if (sb->set_name[0])
576 nm = sb->set_name;
577 else
cf8de691 578 nm = NULL;
82d9eba6 579
7103b9b8
N
580 printf("ARRAY ");
581 if (nm) {
582 printf("/dev/md/");
583 print_escape(nm);
584 putchar(' ');
585 }
061f2c6a
N
586 if (verbose && c)
587 printf(" level=%s", c);
a17a3de3
DL
588 sb_offset = __le64_to_cpu(sb->super_offset);
589 if (sb_offset <= 4)
e9a0e728 590 printf(" metadata=1.1 ");
a17a3de3 591 else if (sb_offset <= 8)
e9a0e728 592 printf(" metadata=1.2 ");
a17a3de3 593 else
e9a0e728 594 printf(" metadata=1.0 ");
061f2c6a
N
595 if (verbose)
596 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
597 printf("UUID=");
82d9eba6 598 for (i=0; i<16; i++) {
82d9eba6 599 if ((i&3)==0 && i != 0) printf(":");
b7a708af 600 printf("%02x", sb->set_uuid[i]);
82d9eba6 601 }
7103b9b8
N
602 if (sb->set_name[0]) {
603 printf(" name=");
604 print_quoted(sb->set_name);
605 }
82d9eba6
NB
606 printf("\n");
607}
608
0d726f17
KS
609static void export_examine_super1(struct supertype *st)
610{
611 struct mdp_superblock_1 *sb = st->sb;
612 int i;
613 int len = 32;
80730bae 614 int layout;
0d726f17
KS
615
616 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
617 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
618 for (i=0; i<32; i++)
619 if (sb->set_name[i] == '\n' ||
620 sb->set_name[i] == '\0') {
621 len = i;
622 break;
623 }
624 if (len)
625 printf("MD_NAME=%.*s\n", len, sb->set_name);
80730bae
MN
626 if (__le32_to_cpu(sb->level) > 0) {
627 int ddsks = 0, ddsks_denom = 1;
628 switch(__le32_to_cpu(sb->level)) {
629 case 1: ddsks=1;break;
630 case 4:
631 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
632 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
633 case 10:
634 layout = __le32_to_cpu(sb->layout);
635 ddsks = __le32_to_cpu(sb->raid_disks);
636 ddsks_denom = (layout&255) * ((layout>>8)&255);
637 }
638 if (ddsks) {
639 long long asize = __le64_to_cpu(sb->size);
640 asize = (asize << 9) * ddsks / ddsks_denom;
641 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize,JEDEC));
642 }
643 }
0d726f17
KS
644 printf("MD_UUID=");
645 for (i=0; i<16; i++) {
646 if ((i&3)==0 && i != 0) printf(":");
647 printf("%02x", sb->set_uuid[i]);
648 }
649 printf("\n");
650 printf("MD_UPDATE_TIME=%llu\n",
651 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
652 printf("MD_DEV_UUID=");
653 for (i=0; i<16; i++) {
654 if ((i&3)==0 && i != 0) printf(":");
655 printf("%02x", sb->device_uuid[i]);
656 }
657 printf("\n");
658 printf("MD_EVENTS=%llu\n",
659 (unsigned long long)__le64_to_cpu(sb->events));
660}
661
74db60b0
N
662static int copy_metadata1(struct supertype *st, int from, int to)
663{
664 /* Read superblock. If it looks good, write it out.
665 * Then if a bitmap is present, copy that.
666 * And if a bad-block-list is present, copy that too.
667 */
668 void *buf;
669 unsigned long long dsize, sb_offset;
670 const int bufsize = 4*1024;
671 struct mdp_superblock_1 super, *sb;
672
673 if (posix_memalign(&buf, 4096, bufsize) != 0)
674 return 1;
675
676 if (!get_dev_size(from, NULL, &dsize))
677 goto err;
678
679 dsize >>= 9;
680 if (dsize < 24)
681 goto err;
682 switch(st->minor_version) {
683 case 0:
684 sb_offset = dsize;
685 sb_offset -= 8*2;
686 sb_offset &= ~(4*2-1);
687 break;
688 case 1:
689 sb_offset = 0;
690 break;
691 case 2:
692 sb_offset = 4*2;
693 break;
694 default:
695 goto err;
696 }
697
698 if (lseek64(from, sb_offset << 9, 0) < 0LL)
699 goto err;
700 if (read(from, buf, bufsize) != bufsize)
701 goto err;
702
703 sb = buf;
704 super = *sb; // save most of sb for when we reuse buf
705
706 if (__le32_to_cpu(super.magic) != MD_SB_MAGIC ||
707 __le32_to_cpu(super.major_version) != 1 ||
708 __le64_to_cpu(super.super_offset) != sb_offset ||
709 calc_sb_1_csum(sb) != super.sb_csum)
710 goto err;
711
712 if (lseek64(to, sb_offset << 9, 0) < 0LL)
713 goto err;
714 if (write(to, buf, bufsize) != bufsize)
715 goto err;
716
717 if (super.feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
718 unsigned long long bitmap_offset = sb_offset;
719 int bytes = 4096; // just an estimate.
720 int written = 0;
721 struct align_fd afrom, ato;
722
723 init_afd(&afrom, from);
724 init_afd(&ato, to);
725
726 bitmap_offset += (int32_t)__le32_to_cpu(super.bitmap_offset);
727
728 if (lseek64(from, bitmap_offset<<9, 0) < 0)
729 goto err;
730 if (lseek64(to, bitmap_offset<<9, 0) < 0)
731 goto err;
732
733 for (written = 0; written < bytes ; ) {
734 int n = bytes - written;
735 if (n > 4096)
736 n = 4096;
737 if (aread(&afrom, buf, n) != n)
738 goto err;
739 if (written == 0) {
740 /* have the header, can calculate
741 * correct bitmap bytes */
742 bitmap_super_t *bms;
74db60b0 743 bms = (void*)buf;
4a3d29ed 744 bytes = calc_bitmap_size(bms, 512);
74db60b0
N
745 if (n > bytes)
746 n = bytes;
747 }
748 if (awrite(&ato, buf, n) != n)
749 goto err;
750 written += n;
751 }
752 }
753
754 if (super.bblog_size != 0 &&
27c7c87a 755 __le16_to_cpu(super.bblog_size) <= 100 &&
74db60b0
N
756 super.bblog_offset != 0 &&
757 (super.feature_map & __le32_to_cpu(MD_FEATURE_BAD_BLOCKS))) {
758 /* There is a bad block log */
759 unsigned long long bb_offset = sb_offset;
27c7c87a 760 int bytes = __le16_to_cpu(super.bblog_size) * 512;
74db60b0
N
761 int written = 0;
762 struct align_fd afrom, ato;
763
764 init_afd(&afrom, from);
765 init_afd(&ato, to);
766
767 bb_offset += (int32_t)__le32_to_cpu(super.bblog_offset);
768
769 if (lseek64(from, bb_offset<<9, 0) < 0)
770 goto err;
771 if (lseek64(to, bb_offset<<9, 0) < 0)
772 goto err;
773
774 for (written = 0; written < bytes ; ) {
775 int n = bytes - written;
776 if (n > 4096)
777 n = 4096;
778 if (aread(&afrom, buf, n) != n)
779 goto err;
780
781 if (awrite(&ato, buf, n) != n)
782 goto err;
783 written += n;
784 }
785 }
786
787 free(buf);
788 return 0;
789
790err:
791 free(buf);
792 return 1;
793}
794
3da92f27 795static void detail_super1(struct supertype *st, char *homehost)
82d9eba6 796{
3da92f27 797 struct mdp_superblock_1 *sb = st->sb;
0aa2f15b 798 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
82d9eba6 799 int i;
b6750aa8 800 int l = homehost ? strlen(homehost) : 0;
82d9eba6 801
65884368 802 printf(" Name : %.32s", sb->set_name);
b6750aa8
NB
803 if (l > 0 && l < 32 &&
804 sb->set_name[l] == ':' &&
805 strncmp(sb->set_name, homehost, l) == 0)
806 printf(" (local to host %s)", homehost);
8b2202de 807 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
65884368
AP
808 printf("\n Cluster Name : %-64s", bms->cluster_name);
809 printf("\n UUID : ");
82d9eba6 810 for (i=0; i<16; i++) {
82d9eba6 811 if ((i&3)==0 && i != 0) printf(":");
34163fc7 812 printf("%02x", sb->set_uuid[i]);
82d9eba6 813 }
65884368
AP
814 printf("\n Events : %llu\n\n",
815 (unsigned long long)__le64_to_cpu(sb->events));
82d9eba6
NB
816}
817
3da92f27 818static void brief_detail_super1(struct supertype *st)
82d9eba6 819{
3da92f27 820 struct mdp_superblock_1 *sb = st->sb;
82d9eba6
NB
821 int i;
822
7103b9b8
N
823 if (sb->set_name[0]) {
824 printf(" name=");
825 print_quoted(sb->set_name);
826 }
82d9eba6
NB
827 printf(" UUID=");
828 for (i=0; i<16; i++) {
82d9eba6 829 if ((i&3)==0 && i != 0) printf(":");
34163fc7 830 printf("%02x", sb->set_uuid[i]);
82d9eba6
NB
831 }
832}
833
0d726f17 834static void export_detail_super1(struct supertype *st)
54bad364 835{
3da92f27 836 struct mdp_superblock_1 *sb = st->sb;
54bad364
KS
837 int i;
838 int len = 32;
839
840 for (i=0; i<32; i++)
841 if (sb->set_name[i] == '\n' ||
842 sb->set_name[i] == '\0') {
843 len = i;
844 break;
845 }
846 if (len)
847 printf("MD_NAME=%.*s\n", len, sb->set_name);
54bad364
KS
848}
849
6d388a88
N
850static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
851{
852 struct mdp_superblock_1 *sb = st->sb;
853 unsigned long long offset;
854 int size;
855 __u64 *bbl, *bbp;
856 int i;
857
27c7c87a 858 if (!sb->bblog_size || __le16_to_cpu(sb->bblog_size) > 100
6d388a88
N
859 || !sb->bblog_offset){
860 printf("No bad-blocks list configured on %s\n", devname);
861 return 0;
862 }
863 if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
864 == 0) {
865 printf("Bad-blocks list is empty in %s\n", devname);
866 return 0;
867 }
868
27c7c87a 869 size = __le16_to_cpu(sb->bblog_size)* 512;
0cf83229 870 if (posix_memalign((void**)&bbl, 4096, size) != 0) {
1ade5cc1 871 pr_err("could not allocate badblocks list\n");
0cf83229
N
872 return 0;
873 }
6d388a88
N
874 offset = __le64_to_cpu(sb->super_offset) +
875 (int)__le32_to_cpu(sb->bblog_offset);
876 offset <<= 9;
877 if (lseek64(fd, offset, 0) < 0) {
878 pr_err("Cannot seek to bad-blocks list\n");
879 return 1;
880 }
881 if (read(fd, bbl, size) != size) {
882 pr_err("Cannot read bad-blocks list\n");
883 return 1;
884 }
885 /* 64bits per entry. 10 bits is block-count, 54 bits is block
886 * offset. Blocks are sectors unless bblog->shift makes them bigger
887 */
888 bbp = (__u64*)bbl;
889 printf("Bad-blocks on %s:\n", devname);
890 for (i = 0; i < size/8; i++, bbp++) {
891 __u64 bb = __le64_to_cpu(*bbp);
892 int count = bb & 0x3ff;
893 unsigned long long sector = bb >> 10;
894
895 if (bb + 1 == 0)
896 break;
897
898 sector <<= sb->bblog_shift;
899 count <<= sb->bblog_shift;
900
901 printf("%20llu for %d sectors\n", sector, count);
902 }
903 return 0;
904}
905
c7654afc
NB
906#endif
907
3da92f27 908static int match_home1(struct supertype *st, char *homehost)
83b6208e 909{
3da92f27 910 struct mdp_superblock_1 *sb = st->sb;
83b6208e
NB
911 int l = homehost ? strlen(homehost) : 0;
912
913 return (l > 0 && l < 32 &&
914 sb->set_name[l] == ':' &&
915 strncmp(sb->set_name, homehost, l) == 0);
916}
917
3da92f27 918static void uuid_from_super1(struct supertype *st, int uuid[4])
82d9eba6 919{
3da92f27 920 struct mdp_superblock_1 *super = st->sb;
82d9eba6
NB
921 char *cuuid = (char*)uuid;
922 int i;
923 for (i=0; i<16; i++)
924 cuuid[i] = super->set_uuid[i];
925}
926
a5d85af7 927static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
82d9eba6 928{
3da92f27 929 struct mdp_superblock_1 *sb = st->sb;
80bf9135
N
930 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
931 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
82d9eba6 932 int working = 0;
f21e18ca 933 unsigned int i;
a5d85af7
N
934 unsigned int role;
935 unsigned int map_disks = info->array.raid_disks;
80bf9135
N
936 unsigned long long super_offset;
937 unsigned long long data_size;
82d9eba6 938
95eeceeb 939 memset(info, 0, sizeof(*info));
82d9eba6 940 info->array.major_version = 1;
b8ac1967 941 info->array.minor_version = st->minor_version;
82d9eba6
NB
942 info->array.patch_version = 0;
943 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
944 info->array.level = __le32_to_cpu(sb->level);
265e0f17 945 info->array.layout = __le32_to_cpu(sb->layout);
82d9eba6
NB
946 info->array.md_minor = -1;
947 info->array.ctime = __le64_to_cpu(sb->ctime);
353632d9 948 info->array.utime = __le64_to_cpu(sb->utime);
bed256c2 949 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
4855f95c 950 info->array.state =
d5977056 951 (__le64_to_cpu(sb->resync_offset) == MaxSector)
4855f95c 952 ? 1 : 0;
06bd6793
GJ
953 if (__le32_to_cpu(bsb->nodes) > 1)
954 info->array.state |= (1 << MD_SB_CLUSTERED);
353632d9 955
e97a7cd0 956 super_offset = __le64_to_cpu(sb->super_offset);
353632d9
NB
957 info->data_offset = __le64_to_cpu(sb->data_offset);
958 info->component_size = __le64_to_cpu(sb->size);
e97a7cd0 959 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
34a13953 960 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
e97a7cd0
AP
961 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_PPL)) {
962 info->ppl_offset = __le16_to_cpu(sb->ppl.offset);
963 info->ppl_size = __le16_to_cpu(sb->ppl.size);
964 info->ppl_sector = super_offset + info->ppl_offset;
965 }
82d9eba6
NB
966
967 info->disk.major = 0;
968 info->disk.minor = 0;
fbf8a0b7 969 info->disk.number = __le32_to_cpu(sb->dev_number);
82d9eba6 970 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
911cead7 971 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
fa7574f6 972 role = MD_DISK_ROLE_FAULTY;
82d9eba6
NB
973 else
974 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
975
0ddc35be
N
976 if (info->array.level <= 0)
977 data_size = __le64_to_cpu(sb->data_size);
978 else
979 data_size = __le64_to_cpu(sb->size);
80bf9135
N
980 if (info->data_offset < super_offset) {
981 unsigned long long end;
982 info->space_before = info->data_offset;
983 end = super_offset;
cc3130a7
N
984
985 if (sb->bblog_offset && sb->bblog_size) {
986 unsigned long long bboffset = super_offset;
987 bboffset += (int32_t)__le32_to_cpu(sb->bblog_offset);
988 if (bboffset < end)
989 end = bboffset;
990 }
991
e97a7cd0
AP
992 if (super_offset + info->bitmap_offset + info->ppl_offset < end)
993 end = super_offset + info->bitmap_offset + info->ppl_offset;
cc3130a7 994
80bf9135
N
995 if (info->data_offset + data_size < end)
996 info->space_after = end - data_size - info->data_offset;
997 else
998 info->space_after = 0;
999 } else {
cc3130a7
N
1000 unsigned long long earliest;
1001 earliest = super_offset + (32+4)*2; /* match kernel */
80bf9135
N
1002 if (info->bitmap_offset > 0) {
1003 unsigned long long bmend = info->bitmap_offset;
561ad559 1004 unsigned long long size = calc_bitmap_size(bsb, 4096);
80bf9135 1005 size /= 512;
cc3130a7
N
1006 bmend += size;
1007 if (bmend > earliest)
ac92b44a 1008 earliest = bmend;
e97a7cd0
AP
1009 } else if (info->ppl_offset > 0) {
1010 unsigned long long pplend = info->ppl_offset +
1011 info->ppl_size;
1012 if (pplend > earliest)
1013 earliest = pplend;
cc3130a7
N
1014 }
1015 if (sb->bblog_offset && sb->bblog_size) {
1016 unsigned long long bbend = super_offset;
1017 bbend += (int32_t)__le32_to_cpu(sb->bblog_offset);
27c7c87a 1018 bbend += __le16_to_cpu(sb->bblog_size);
cc3130a7
N
1019 if (bbend > earliest)
1020 earliest = bbend;
1021 }
1022 if (earliest < info->data_offset)
1023 info->space_before = info->data_offset - earliest;
1024 else
1025 info->space_before = 0;
80bf9135
N
1026 info->space_after = misc->device_size - data_size - info->data_offset;
1027 }
c4b26c64
N
1028 if (info->space_before == 0 && info->space_after == 0) {
1029 /* It will look like we don't support data_offset changes,
1030 * be we do - it's just that there is no room.
1031 * A change that reduced the number of devices should
1032 * still be allowed, so set the otherwise useless value of '1'
1033 */
1034 info->space_after = 1;
1035 }
80bf9135 1036
82d9eba6
NB
1037 info->disk.raid_disk = -1;
1038 switch(role) {
fa7574f6 1039 case MD_DISK_ROLE_SPARE:
f22385f9 1040 info->disk.state = 0; /* spare: not active, not sync, not faulty */
82d9eba6 1041 break;
fa7574f6 1042 case MD_DISK_ROLE_FAULTY:
aa313118 1043 info->disk.state = (1 << MD_DISK_FAULTY); /* faulty */
82d9eba6 1044 break;
ed94976d
SL
1045 case MD_DISK_ROLE_JOURNAL:
1046 info->disk.state = (1 << MD_DISK_JOURNAL);
1047 info->disk.raid_disk = role;
1048 info->space_after = (misc->device_size - info->data_offset) % 8; /* journal uses all 4kB blocks*/
1049 break;
82d9eba6
NB
1050 default:
1051 info->disk.state = 6; /* active and in sync */
1052 info->disk.raid_disk = role;
1053 }
cb099724
N
1054 if (sb->devflags & WriteMostly1)
1055 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
71574efb
N
1056 if (sb->devflags & FailFast1)
1057 info->disk.state |= (1 << MD_DISK_FAILFAST);
82d9eba6 1058 info->events = __le64_to_cpu(sb->events);
1522c538 1059 sprintf(info->text_version, "1.%d", st->minor_version);
a67dd8cc 1060 info->safe_mode_delay = 200;
82d9eba6
NB
1061
1062 memcpy(info->uuid, sb->set_uuid, 16);
1063
31317663
NB
1064 strncpy(info->name, sb->set_name, 32);
1065 info->name[32] = 0;
947fd4dd 1066
aacb2f81 1067 if ((__le32_to_cpu(sb->feature_map)&MD_FEATURE_REPLACEMENT)) {
72e7fb13 1068 info->disk.state &= ~(1 << MD_DISK_SYNC);
aacb2f81
N
1069 info->disk.state |= 1 << MD_DISK_REPLACEMENT;
1070 }
1071
921d9e16
N
1072 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
1073 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
1074 else
1075 info->recovery_start = MaxSector;
1076
353632d9
NB
1077 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1078 info->reshape_active = 1;
26bf5587
N
1079 if ((sb->feature_map & __le32_to_cpu(MD_FEATURE_NEW_OFFSET)) &&
1080 sb->new_offset != 0)
5e88ab2e 1081 info->reshape_active |= RESHAPE_NO_BACKUP;
353632d9
NB
1082 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
1083 info->new_level = __le32_to_cpu(sb->new_level);
1084 info->delta_disks = __le32_to_cpu(sb->delta_disks);
1085 info->new_layout = __le32_to_cpu(sb->new_layout);
bed256c2 1086 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
4180aa4d
N
1087 if (info->delta_disks < 0)
1088 info->array.raid_disks -= info->delta_disks;
353632d9
NB
1089 } else
1090 info->reshape_active = 0;
1091
6e75048b
AK
1092 info->recovery_blocked = info->reshape_active;
1093
a5d85af7
N
1094 if (map)
1095 for (i=0; i<map_disks; i++)
1096 map[i] = 0;
f21e18ca 1097 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
82d9eba6 1098 role = __le16_to_cpu(sb->dev_roles[i]);
fa7574f6 1099 if (/*role == MD_DISK_ROLE_SPARE || */role < (unsigned) info->array.raid_disks) {
82d9eba6 1100 working++;
a5d85af7
N
1101 if (map && role < map_disks)
1102 map[role] = 1;
1103 }
82d9eba6
NB
1104 }
1105
1106 info->array.working_disks = working;
e97a7cd0
AP
1107
1108 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_JOURNAL)) {
051f3265 1109 info->journal_device_required = 1;
e97a7cd0
AP
1110 info->consistency_policy = CONSISTENCY_POLICY_JOURNAL;
1111 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_PPL)) {
1112 info->consistency_policy = CONSISTENCY_POLICY_PPL;
1113 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
1114 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
1115 } else if (info->array.level <= 0) {
1116 info->consistency_policy = CONSISTENCY_POLICY_NONE;
1117 } else {
1118 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
1119 }
1120
051f3265 1121 info->journal_clean = 0;
82d9eba6
NB
1122}
1123
00bbdbda
N
1124static struct mdinfo *container_content1(struct supertype *st, char *subarray)
1125{
1126 struct mdinfo *info;
1127
1128 if (subarray)
1129 return NULL;
1130
503975b9 1131 info = xmalloc(sizeof(*info));
00bbdbda
N
1132 getinfo_super1(st, info, NULL);
1133 return info;
1134}
1135
68c7d6d7 1136static int update_super1(struct supertype *st, struct mdinfo *info,
3da92f27 1137 char *update,
e5eac01f
NB
1138 char *devname, int verbose,
1139 int uuid_set, char *homehost)
82d9eba6 1140{
5a31170d
N
1141 /* NOTE: for 'assemble' and 'force' we need to return non-zero
1142 * if any change was made. For others, the return value is
1143 * ignored.
c4d831e1 1144 */
82d9eba6 1145 int rv = 0;
d15a1f72 1146 int lockid;
3da92f27 1147 struct mdp_superblock_1 *sb = st->sb;
81a8a694 1148 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
82d9eba6 1149
81a8a694
GJ
1150 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1151 rv = cluster_get_dlmlock(&lockid);
d15a1f72
GJ
1152 if (rv) {
1153 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
81a8a694 1154 cluster_release_dlmlock(lockid);
d15a1f72
GJ
1155 return rv;
1156 }
1157 }
1158
def11332
N
1159 if (strcmp(update, "homehost") == 0 &&
1160 homehost) {
1161 /* Note that 'homehost' is special as it is really
1162 * a "name" update.
1163 */
1164 char *c;
1165 update = "name";
1166 c = strchr(sb->set_name, ':');
1167 if (c)
1168 strncpy(info->name, c+1, 31 - (c-sb->set_name));
1169 else
1170 strncpy(info->name, sb->set_name, 32);
1171 info->name[32] = 0;
1172 }
1173
67a8c82d
NB
1174 if (strcmp(update, "force-one")==0) {
1175 /* Not enough devices for a working array,
1176 * so bring this one up-to-date
1177 */
c4d831e1
NB
1178 if (sb->events != __cpu_to_le64(info->events))
1179 rv = 1;
6d3d5804 1180 sb->events = __cpu_to_le64(info->events);
1e2b2765 1181 } else if (strcmp(update, "force-array")==0) {
67a8c82d
NB
1182 /* Degraded array and 'force' requests to
1183 * maybe need to mark it 'clean'.
1184 */
82d9eba6
NB
1185 switch(__le32_to_cpu(sb->level)) {
1186 case 5: case 4: case 6:
1187 /* need to force clean */
b7528a20 1188 if (sb->resync_offset != MaxSector)
c4d831e1 1189 rv = 1;
b7528a20 1190 sb->resync_offset = MaxSector;
82d9eba6 1191 }
1e2b2765 1192 } else if (strcmp(update, "assemble")==0) {
82d9eba6
NB
1193 int d = info->disk.number;
1194 int want;
aacb2f81 1195 if (info->disk.state & (1<<MD_DISK_ACTIVE))
a2ce5a1a 1196 want = info->disk.raid_disk;
69a48116
SL
1197 else if (info->disk.state & (1<<MD_DISK_JOURNAL))
1198 want = MD_DISK_ROLE_JOURNAL;
82d9eba6 1199 else
fa7574f6 1200 want = MD_DISK_ROLE_SPARE;
a2ce5a1a
N
1201 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
1202 sb->dev_roles[d] = __cpu_to_le16(want);
82d9eba6
NB
1203 rv = 1;
1204 }
d43494fc
N
1205 if (info->reshape_active &&
1206 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1207 info->delta_disks >= 0 &&
1208 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
1209 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1210 rv = 1;
1211 }
1212 if (info->reshape_active &&
1213 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1214 info->delta_disks < 0 &&
1215 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
1216 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1217 rv = 1;
1218 }
1e2b2765 1219 } else if (strcmp(update, "linear-grow-new") == 0) {
f21e18ca 1220 unsigned int i;
c5f71c24 1221 int fd;
f21e18ca 1222 unsigned int max = __le32_to_cpu(sb->max_dev);
f752781f
NB
1223
1224 for (i=0 ; i < max ; i++)
fa7574f6 1225 if (__le16_to_cpu(sb->dev_roles[i]) >= MD_DISK_ROLE_FAULTY)
f752781f
NB
1226 break;
1227 sb->dev_number = __cpu_to_le32(i);
1228 info->disk.number = i;
1229 if (max >= __le32_to_cpu(sb->max_dev))
1230 sb->max_dev = __cpu_to_le32(max+1);
1231
c5f71c24 1232 random_uuid(sb->device_uuid);
f752781f
NB
1233
1234 sb->dev_roles[i] =
1235 __cpu_to_le16(info->disk.raid_disk);
1c6cb603
N
1236
1237 fd = open(devname, O_RDONLY);
1238 if (fd >= 0) {
1239 unsigned long long ds;
1240 get_dev_size(fd, devname, &ds);
1241 close(fd);
1242 ds >>= 9;
1243 if (__le64_to_cpu(sb->super_offset) <
1244 __le64_to_cpu(sb->data_offset)) {
1245 sb->data_size = __cpu_to_le64(
1246 ds - __le64_to_cpu(sb->data_offset));
1247 } else {
1248 ds -= 8*2;
1249 ds &= ~(unsigned long long)(4*2-1);
1250 sb->super_offset = __cpu_to_le64(ds);
1251 sb->data_size = __cpu_to_le64(
1252 ds - __le64_to_cpu(sb->data_offset));
1253 }
1254 }
1e2b2765 1255 } else if (strcmp(update, "linear-grow-update") == 0) {
82d9eba6 1256 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
f752781f
NB
1257 sb->dev_roles[info->disk.number] =
1258 __cpu_to_le16(info->disk.raid_disk);
1e2b2765 1259 } else if (strcmp(update, "resync") == 0) {
82d9eba6 1260 /* make sure resync happens */
434b7755 1261 sb->resync_offset = 0ULL;
1e2b2765 1262 } else if (strcmp(update, "uuid") == 0) {
350f29f9 1263 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
bf4fb153 1264
561ad559
AP
1265 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)
1266 memcpy(bms->uuid, sb->set_uuid, 16);
5a31170d
N
1267 } else if (strcmp(update, "no-bitmap") == 0) {
1268 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
688e99a7
N
1269 } else if (strcmp(update, "bbl") == 0) {
1270 /* only possible if there is room after the bitmap, or if
1271 * there is no bitmap
1272 */
1273 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1274 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
561ad559 1275 long bitmap_offset = 0;
688e99a7
N
1276 long bm_sectors = 0;
1277 long space;
1278
5a23a06e 1279#ifndef MDASSEMBLE
688e99a7 1280 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
561ad559
AP
1281 bitmap_offset = (long)__le32_to_cpu(sb->bitmap_offset);
1282 bm_sectors = calc_bitmap_size(bms, 4096) >> 9;
e97a7cd0
AP
1283 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
1284 bitmap_offset = (long)__le16_to_cpu(sb->ppl.offset);
1285 bm_sectors = (long)__le16_to_cpu(sb->ppl.size);
688e99a7 1286 }
5a23a06e 1287#endif
688e99a7 1288 if (sb_offset < data_offset) {
f79bbf4f 1289 /* 1.1 or 1.2. Put bbl after bitmap leaving at least 32K
688e99a7
N
1290 */
1291 long bb_offset;
f79bbf4f 1292 bb_offset = sb_offset + 8;
688e99a7 1293 if (bm_sectors && bitmap_offset > 0)
f79bbf4f
N
1294 bb_offset = bitmap_offset + bm_sectors;
1295 while (bb_offset < (long)sb_offset + 8 + 32*2
1296 && bb_offset + 8+8 <= (long)data_offset)
1297 /* too close to bitmap, and room to grow */
1298 bb_offset += 8;
1299 if (bb_offset + 8 <= (long)data_offset) {
688e99a7
N
1300 sb->bblog_size = __cpu_to_le16(8);
1301 sb->bblog_offset = __cpu_to_le32(bb_offset);
1302 }
1303 } else {
1304 /* 1.0 - Put bbl just before super block */
1305 if (bm_sectors && bitmap_offset < 0)
1306 space = -bitmap_offset - bm_sectors;
1307 else
1308 space = sb_offset - data_offset -
1309 __le64_to_cpu(sb->data_size);
1310 if (space >= 8) {
1311 sb->bblog_size = __cpu_to_le16(8);
1312 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1313 }
1314 }
1315 } else if (strcmp(update, "no-bbl") == 0) {
1316 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
1317 pr_err("Cannot remove active bbl from %s\n",devname);
1318 else {
1319 sb->bblog_size = 0;
1320 sb->bblog_shift = 0;
1321 sb->bblog_offset = 0;
1322 }
6dd16dac
N
1323 } else if (strcmp(update, "force-no-bbl") == 0) {
1324 sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1325 sb->bblog_size = 0;
1326 sb->bblog_shift = 0;
1327 sb->bblog_offset = 0;
e6e9dd3f
AP
1328 } else if (strcmp(update, "ppl") == 0) {
1329 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1330 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
1331 unsigned long long data_size = __le64_to_cpu(sb->data_size);
1332 long bb_offset = __le32_to_cpu(sb->bblog_offset);
1333 int space;
1334 int optimal_space;
1335 int offset;
1336
1337 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1338 pr_err("Cannot add PPL to array with bitmap\n");
1339 return -2;
1340 }
1341
1342 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_JOURNAL)) {
1343 pr_err("Cannot add PPL to array with journal\n");
1344 return -2;
1345 }
1346
1347 if (sb_offset < data_offset) {
1348 if (bb_offset)
1349 space = bb_offset - 8;
1350 else
1351 space = data_offset - sb_offset - 8;
1352 offset = 8;
1353 } else {
1354 offset = -(sb_offset - data_offset - data_size);
1355 if (offset < INT16_MIN)
1356 offset = INT16_MIN;
1357 space = -(offset - bb_offset);
1358 }
1359
1360 if (space < (PPL_HEADER_SIZE >> 9) + 8) {
1361 pr_err("Not enough space to add ppl\n");
1362 return -2;
1363 }
1364
1365 optimal_space = choose_ppl_space(__le32_to_cpu(sb->chunksize));
1366
1367 if (space > optimal_space)
1368 space = optimal_space;
1369 if (space > UINT16_MAX)
1370 space = UINT16_MAX;
1371
1372 sb->ppl.offset = __cpu_to_le16(offset);
1373 sb->ppl.size = __cpu_to_le16(space);
1374 sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
1375 } else if (strcmp(update, "no-ppl") == 0) {
1376 sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_PPL);
1e2b2765 1377 } else if (strcmp(update, "name") == 0) {
c4f12c13
NB
1378 if (info->name[0] == 0)
1379 sprintf(info->name, "%d", info->array.md_minor);
1380 memset(sb->set_name, 0, sizeof(sb->set_name));
1381 if (homehost &&
1382 strchr(info->name, ':') == NULL &&
1383 strlen(homehost)+1+strlen(info->name) < 32) {
1384 strcpy(sb->set_name, homehost);
1385 strcat(sb->set_name, ":");
1386 strcat(sb->set_name, info->name);
1387 } else
47426701 1388 strncpy(sb->set_name, info->name, sizeof(sb->set_name));
1e2b2765 1389 } else if (strcmp(update, "devicesize") == 0 &&
bee8ec56
NB
1390 __le64_to_cpu(sb->super_offset) <
1391 __le64_to_cpu(sb->data_offset)) {
1392 /* set data_size to device size less data_offset */
1393 struct misc_dev_info *misc = (struct misc_dev_info*)
911cead7 1394 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
bee8ec56
NB
1395 sb->data_size = __cpu_to_le64(
1396 misc->device_size - __le64_to_cpu(sb->data_offset));
d5ff855d 1397 } else if (strncmp(update, "revert-reshape", 14) == 0) {
199f1a1f
N
1398 rv = -2;
1399 if (!(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
1400 pr_err("No active reshape to revert on %s\n",
1401 devname);
1402 else {
199f1a1f 1403 __u32 temp;
a2836f12
N
1404 unsigned long long reshape_sectors;
1405 long reshape_chunk;
1406 rv = 0;
d5ff855d
N
1407 /* If the reshape hasn't started, just stop it.
1408 * It is conceivable that a stripe was modified but
1409 * the metadata not updated. In that case the backup
1410 * should have been used to get passed the critical stage.
1411 * If that couldn't happen, the "-nobackup" version
1412 * will be used.
1413 */
1414 if (strcmp(update, "revert-reshape-nobackup") == 0 &&
1415 sb->reshape_position == 0 &&
1416 (__le32_to_cpu(sb->delta_disks) > 0 ||
1417 (__le32_to_cpu(sb->delta_disks) == 0 &&
1418 !(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS))))) {
1419 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1420 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
1421 __le32_to_cpu(sb->delta_disks));
1422 sb->delta_disks = 0;
1423 goto done;
1424 }
a2836f12
N
1425 /* reshape_position is a little messy.
1426 * Its value must be a multiple of the larger
1427 * chunk size, and of the "after" data disks.
1428 * So when reverting we need to change it to
1429 * be a multiple of the new "after" data disks,
1430 * which is the old "before".
1431 * If it isn't already a multiple of 'before',
1432 * the only thing we could do would be
1433 * copy some block around on the disks, which
1434 * is easy to get wrong.
1435 * So we reject a revert-reshape unless the
1436 * alignment is good.
1437 */
efb3994e
N
1438 if (__le32_to_cpu(sb->level) >= 4 &&
1439 __le32_to_cpu(sb->level) <= 6) {
1440 reshape_sectors = __le64_to_cpu(sb->reshape_position);
1441 reshape_chunk = __le32_to_cpu(sb->new_chunk);
1442 reshape_chunk *= __le32_to_cpu(sb->raid_disks) - __le32_to_cpu(sb->delta_disks) -
1443 (__le32_to_cpu(sb->level)==6 ? 2 : 1);
1444 if (reshape_sectors % reshape_chunk) {
1445 pr_err("Reshape position is not suitably aligned.\n");
2bf62891 1446 pr_err("Try normal assembly and stop again\n");
efb3994e
N
1447 return -2;
1448 }
a2836f12
N
1449 }
1450 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
199f1a1f
N
1451 __le32_to_cpu(sb->delta_disks));
1452 if (sb->delta_disks == 0)
1453 sb->feature_map ^= __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1454 else
1455 sb->delta_disks = __cpu_to_le32(-__le32_to_cpu(sb->delta_disks));
1456
1457 temp = sb->new_layout;
1458 sb->new_layout = sb->layout;
1459 sb->layout = temp;
1460
1461 temp = sb->new_chunk;
1462 sb->new_chunk = sb->chunksize;
1463 sb->chunksize = temp;
1464
1465 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_NEW_OFFSET)) {
419e0182
N
1466 long offset_delta = (int32_t)__le32_to_cpu(sb->new_offset);
1467 sb->data_offset = __cpu_to_le64(__le64_to_cpu(sb->data_offset) + offset_delta);
1468 sb->new_offset = __cpu_to_le32(-offset_delta);
1469 sb->data_size = __cpu_to_le64(__le64_to_cpu(sb->data_size) - offset_delta);
199f1a1f 1470 }
d5ff855d 1471 done:;
199f1a1f 1472 }
1e2b2765 1473 } else if (strcmp(update, "_reshape_progress")==0)
353632d9 1474 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
16715c01
DL
1475 else if (strcmp(update, "writemostly")==0)
1476 sb->devflags |= WriteMostly1;
1477 else if (strcmp(update, "readwrite")==0)
1478 sb->devflags &= ~WriteMostly1;
71574efb
N
1479 else if (strcmp(update, "failfast") == 0)
1480 sb->devflags |= FailFast1;
1481 else if (strcmp(update, "nofailfast") == 0)
1482 sb->devflags &= ~FailFast1;
1e2b2765
N
1483 else
1484 rv = -1;
82d9eba6
NB
1485
1486 sb->sb_csum = calc_sb_1_csum(sb);
81a8a694
GJ
1487 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1488 cluster_release_dlmlock(lockid);
d15a1f72 1489
82d9eba6
NB
1490 return rv;
1491}
1492
3da92f27 1493static int init_super1(struct supertype *st, mdu_array_info_t *info,
5308f117 1494 struct shape *s, char *name, char *homehost,
83cd1e97 1495 int *uuid, unsigned long long data_offset)
82d9eba6 1496{
6416d527 1497 struct mdp_superblock_1 *sb;
82d9eba6 1498 int spares;
05697ec1 1499 char defname[10];
911cead7 1500 int sbsize;
6416d527 1501
3c0bcd46 1502 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1ade5cc1 1503 pr_err("could not allocate superblock\n");
3d2c4fc7
DW
1504 return 0;
1505 }
1afa9308 1506 memset(sb, 0, SUPER1_SIZE);
82d9eba6 1507
64557c33 1508 st->sb = sb;
ba7eb04f 1509 if (info == NULL) {
82d9eba6
NB
1510 /* zeroing superblock */
1511 return 0;
05697ec1 1512 }
82d9eba6
NB
1513
1514 spares = info->working_disks - info->active_disks;
911cead7 1515 if (info->raid_disks + spares > MAX_DEVS) {
e7b84f9d 1516 pr_err("too many devices requested: %d+%d > %d\n",
911cead7 1517 info->raid_disks , spares, MAX_DEVS);
82d9eba6
NB
1518 return 0;
1519 }
1520
82d9eba6
NB
1521 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1522 sb->major_version = __cpu_to_le32(1);
1523 sb->feature_map = 0;
1524 sb->pad0 = 0;
34163fc7 1525
350f29f9
NB
1526 if (uuid)
1527 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
c5f71c24
JS
1528 else
1529 random_uuid(sb->set_uuid);;
82d9eba6 1530
05697ec1
NB
1531 if (name == NULL || *name == 0) {
1532 sprintf(defname, "%d", info->md_minor);
1533 name = defname;
1534 }
05697ec1
NB
1535 if (homehost &&
1536 strchr(name, ':')== NULL &&
1537 strlen(homehost)+1+strlen(name) < 32) {
1538 strcpy(sb->set_name, homehost);
1539 strcat(sb->set_name, ":");
1540 strcat(sb->set_name, name);
1541 } else
47426701 1542 strncpy(sb->set_name, name, sizeof(sb->set_name));
82d9eba6
NB
1543
1544 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1545 sb->level = __cpu_to_le32(info->level);
ea329559 1546 sb->layout = __cpu_to_le32(info->layout);
5308f117 1547 sb->size = __cpu_to_le64(s->size*2ULL);
82d9eba6
NB
1548 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1549 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1550
83cd1e97 1551 sb->data_offset = __cpu_to_le64(data_offset);
82d9eba6
NB
1552 sb->data_size = __cpu_to_le64(0);
1553 sb->super_offset = __cpu_to_le64(0);
1554 sb->recovery_offset = __cpu_to_le64(0);
1555
1556 sb->utime = sb->ctime;
1557 sb->events = __cpu_to_le64(1);
34321279 1558 if (info->state & (1<<MD_SB_CLEAN))
b7528a20 1559 sb->resync_offset = MaxSector;
82d9eba6
NB
1560 else
1561 sb->resync_offset = 0;
911cead7
N
1562 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1563 sbsize = ROUND_UP(sbsize, 512);
1564 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
82d9eba6 1565
911cead7 1566 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
82d9eba6 1567
e97a7cd0
AP
1568 if (s->consistency_policy == CONSISTENCY_POLICY_PPL)
1569 sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
1570
82d9eba6
NB
1571 return 1;
1572}
1573
111d01fc
NB
1574struct devinfo {
1575 int fd;
1576 char *devname;
72ca9bcf 1577 long long data_offset;
111d01fc
NB
1578 mdu_disk_info_t disk;
1579 struct devinfo *next;
1580};
0e600426 1581#ifndef MDASSEMBLE
82d9eba6 1582/* Add a device to the superblock being created */
f20c3968 1583static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
72ca9bcf 1584 int fd, char *devname, unsigned long long data_offset)
82d9eba6 1585{
3da92f27 1586 struct mdp_superblock_1 *sb = st->sb;
82d9eba6 1587 __u16 *rp = sb->dev_roles + dk->number;
111d01fc 1588 struct devinfo *di, **dip;
81a8a694 1589 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
d15a1f72 1590 int rv, lockid;
e23c2663 1591 int dk_state;
d15a1f72 1592
81a8a694
GJ
1593 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1594 rv = cluster_get_dlmlock(&lockid);
d15a1f72
GJ
1595 if (rv) {
1596 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
81a8a694 1597 cluster_release_dlmlock(lockid);
d15a1f72
GJ
1598 return rv;
1599 }
1600 }
111d01fc 1601
e23c2663 1602 dk_state = dk->state & ~(1<<MD_DISK_FAILFAST);
aa313118
GK
1603 if ((dk_state & (1<<MD_DISK_ACTIVE)) &&
1604 (dk_state & (1<<MD_DISK_SYNC)))/* active, sync */
82d9eba6 1605 *rp = __cpu_to_le16(dk->raid_disk);
e23c2663 1606 else if (dk_state & (1<<MD_DISK_JOURNAL))
cc1799c3 1607 *rp = MD_DISK_ROLE_JOURNAL;
aa313118 1608 else if ((dk_state & ~(1<<MD_DISK_ACTIVE)) == 0) /* active or idle -> spare */
fa7574f6 1609 *rp = MD_DISK_ROLE_SPARE;
34163fc7 1610 else
fa7574f6 1611 *rp = MD_DISK_ROLE_FAULTY;
111d01fc 1612
f21e18ca 1613 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
911cead7 1614 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
9df04a4f 1615 sb->max_dev = __cpu_to_le32(dk->number+1);
8844e291 1616
d2ca6449 1617 sb->dev_number = __cpu_to_le32(dk->number);
16715c01 1618 sb->devflags = 0; /* don't copy another disks flags */
d2ca6449
NB
1619 sb->sb_csum = calc_sb_1_csum(sb);
1620
111d01fc
NB
1621 dip = (struct devinfo **)&st->info;
1622 while (*dip)
1623 dip = &(*dip)->next;
503975b9 1624 di = xmalloc(sizeof(struct devinfo));
111d01fc
NB
1625 di->fd = fd;
1626 di->devname = devname;
1627 di->disk = *dk;
72ca9bcf 1628 di->data_offset = data_offset;
111d01fc
NB
1629 di->next = NULL;
1630 *dip = di;
f20c3968 1631
81a8a694
GJ
1632 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1633 cluster_release_dlmlock(lockid);
d15a1f72 1634
f20c3968 1635 return 0;
82d9eba6 1636}
0e600426 1637#endif
82d9eba6 1638
b138214f 1639static int locate_bitmap1(struct supertype *st, int fd, int node_num);
14263cf1 1640
3da92f27 1641static int store_super1(struct supertype *st, int fd)
82d9eba6 1642{
3da92f27 1643 struct mdp_superblock_1 *sb = st->sb;
6fbba4c9 1644 unsigned long long sb_offset;
308340aa 1645 struct align_fd afd;
82d9eba6 1646 int sbsize;
5dd497ee 1647 unsigned long long dsize;
81a8a694 1648 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
d15a1f72
GJ
1649 int rv, lockid;
1650
81a8a694
GJ
1651 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1652 rv = cluster_get_dlmlock(&lockid);
d15a1f72
GJ
1653 if (rv) {
1654 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
81a8a694 1655 cluster_release_dlmlock(lockid);
d15a1f72
GJ
1656 return rv;
1657 }
1658 }
96395475 1659
beae1dfe
NB
1660 if (!get_dev_size(fd, NULL, &dsize))
1661 return 1;
1662
1663 dsize >>= 9;
96395475 1664
5dd497ee 1665 if (dsize < 24)
96395475
NB
1666 return 2;
1667
308340aa
JS
1668 init_afd(&afd, fd);
1669
96395475
NB
1670 /*
1671 * Calculate the position of the superblock.
1672 * It is always aligned to a 4K boundary and
1673 * depending on minor_version, it can be:
1674 * 0: At least 8K, but less than 12K, from end of device
1675 * 1: At start of device
1676 * 2: 4K from start of device.
1677 */
1678 switch(st->minor_version) {
1679 case 0:
5dd497ee 1680 sb_offset = dsize;
96395475
NB
1681 sb_offset -= 8*2;
1682 sb_offset &= ~(4*2-1);
1683 break;
1684 case 1:
6fbba4c9 1685 sb_offset = 0;
96395475
NB
1686 break;
1687 case 2:
1688 sb_offset = 4*2;
1689 break;
1690 default:
1691 return -EINVAL;
1692 }
1693
6fbba4c9
NB
1694 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1695 0 != __le64_to_cpu(sb->super_offset)
96395475 1696 ) {
e7b84f9d 1697 pr_err("internal error - sb_offset is wrong\n");
96395475
NB
1698 abort();
1699 }
82d9eba6 1700
6fbba4c9 1701 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
1702 return 3;
1703
0a2f1894 1704 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
82d9eba6 1705
308340aa 1706 if (awrite(&afd, sb, sbsize) != sbsize)
82d9eba6
NB
1707 return 4;
1708
14263cf1
NB
1709 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1710 struct bitmap_super_s *bm = (struct bitmap_super_s*)
911cead7 1711 (((char*)sb)+MAX_SB_SIZE);
14263cf1 1712 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
b138214f 1713 locate_bitmap1(st, fd, 0);
308340aa
JS
1714 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1715 return 5;
14263cf1
NB
1716 }
1717 }
570c0542 1718 fsync(fd);
81a8a694
GJ
1719 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1720 cluster_release_dlmlock(lockid);
d15a1f72 1721
82d9eba6
NB
1722 return 0;
1723}
1724
3da92f27 1725static int load_super1(struct supertype *st, int fd, char *devname);
892debc8 1726
2fb749d1
NB
1727static unsigned long choose_bm_space(unsigned long devsize)
1728{
1729 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1730 * if bigger than 200Gig, save 128k
a380e275
N
1731 * NOTE: result must be multiple of 4K else bad things happen
1732 * on 4K-sector devices.
2fb749d1 1733 */
7eef9be2
JS
1734 if (devsize < 64*2)
1735 return 0;
2fb749d1
NB
1736 if (devsize - 64*2 >= 200*1024*1024*2)
1737 return 128*2;
1738 if (devsize - 4*2 > 8*1024*1024*2)
1739 return 64*2;
1740 return 4*2;
1741}
1742
e8090005
N
1743static void free_super1(struct supertype *st);
1744
e97a7cd0
AP
1745#ifndef MDASSEMBLE
1746
198d5478 1747__u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
cc1799c3 1748
e97a7cd0
AP
1749static int write_init_ppl1(struct supertype *st, struct mdinfo *info, int fd)
1750{
1751 struct mdp_superblock_1 *sb = st->sb;
1752 void *buf;
1753 struct ppl_header *ppl_hdr;
1754 int ret;
1755
1756 ret = posix_memalign(&buf, 4096, PPL_HEADER_SIZE);
1757 if (ret) {
1758 pr_err("Failed to allocate PPL header buffer\n");
1759 return ret;
1760 }
1761
1762 memset(buf, 0, PPL_HEADER_SIZE);
1763 ppl_hdr = buf;
1764 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
1765 ppl_hdr->signature = __cpu_to_le32(~crc32c_le(~0, sb->set_uuid,
1766 sizeof(sb->set_uuid)));
1767 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
1768
1769 if (lseek64(fd, info->ppl_sector * 512, SEEK_SET) < 0) {
1770 ret = errno;
1771 perror("Failed to seek to PPL header location");
1772 }
1773
1774 if (!ret && write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
1775 ret = errno;
1776 perror("Write PPL header failed");
1777 }
1778
1779 if (!ret)
1780 fsync(fd);
1781
1782 free(buf);
1783 return ret;
1784}
1785
1786#define META_BLOCK_SIZE 4096
1787
cc1799c3
SL
1788static int write_empty_r5l_meta_block(struct supertype *st, int fd)
1789{
1790 struct r5l_meta_block *mb;
1791 struct mdp_superblock_1 *sb = st->sb;
1792 struct align_fd afd;
1793 __u32 crc;
1794
1795 init_afd(&afd, fd);
1796
1797 if (posix_memalign((void**)&mb, 4096, META_BLOCK_SIZE) != 0) {
1798 pr_err("Could not allocate memory for the meta block.\n");
1799 return 1;
1800 }
1801
1802 memset(mb, 0, META_BLOCK_SIZE);
1803
1804 mb->magic = __cpu_to_le32(R5LOG_MAGIC);
1805 mb->version = R5LOG_VERSION;
1806 mb->meta_size = __cpu_to_le32(sizeof(struct r5l_meta_block));
1807 mb->seq = __cpu_to_le64(random32());
1808 mb->position = __cpu_to_le64(0);
1809
198d5478
SL
1810 crc = crc32c_le(0xffffffff, sb->set_uuid, sizeof(sb->set_uuid));
1811 crc = crc32c_le(crc, (void *)mb, META_BLOCK_SIZE);
1812 mb->checksum = crc;
cc1799c3 1813
e97a7cd0 1814 if (lseek64(fd, __le64_to_cpu(sb->data_offset) * 512, 0) < 0LL) {
cc1799c3
SL
1815 pr_err("cannot seek to offset of the meta block\n");
1816 goto fail_to_write;
1817 }
1818
1819 if (awrite(&afd, mb, META_BLOCK_SIZE) != META_BLOCK_SIZE) {
1820 pr_err("failed to store write the meta block \n");
1821 goto fail_to_write;
1822 }
1823 fsync(fd);
1824
1825 free(mb);
1826 return 0;
1827
1828fail_to_write:
1829 free(mb);
1830 return 1;
1831}
1832
111d01fc 1833static int write_init_super1(struct supertype *st)
82d9eba6 1834{
3da92f27 1835 struct mdp_superblock_1 *sb = st->sb;
e8090005 1836 struct supertype *refst;
111d01fc 1837 int rv = 0;
f21e18ca 1838 unsigned long long bm_space;
111d01fc 1839 struct devinfo *di;
8fac0577 1840 unsigned long long dsize, array_size;
23bf42cc 1841 unsigned long long sb_offset;
83cd1e97 1842 unsigned long long data_offset;
1b7eb672 1843 long bm_offset;
82d9eba6 1844
cc1799c3
SL
1845 for (di = st->info; di; di = di->next) {
1846 if (di->disk.state & (1 << MD_DISK_JOURNAL))
e97a7cd0 1847 sb->feature_map |= __cpu_to_le32(MD_FEATURE_JOURNAL);
cc1799c3
SL
1848 }
1849
4687f160 1850 for (di = st->info; di; di = di->next) {
f4dc5e9b 1851 if (di->disk.state & (1 << MD_DISK_FAULTY))
111d01fc 1852 continue;
a0c8a17f
NB
1853 if (di->fd < 0)
1854 continue;
82d9eba6 1855
ba728be7 1856 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
9277cc77 1857 ;
82d9eba6 1858
111d01fc
NB
1859 sb->dev_number = __cpu_to_le32(di->disk.number);
1860 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
adbb382b 1861 sb->devflags |= WriteMostly1;
9a88e7b6 1862 else
adbb382b 1863 sb->devflags &= ~WriteMostly1;
71574efb
N
1864 if (di->disk.state & (1<<MD_DISK_FAILFAST))
1865 sb->devflags |= FailFast1;
1866 else
1867 sb->devflags &= ~FailFast1;
892debc8 1868
c5f71c24 1869 random_uuid(sb->device_uuid);
83208785 1870
01290056
SL
1871 if (!(di->disk.state & (1<<MD_DISK_JOURNAL)))
1872 sb->events = 0;
111d01fc 1873
e8090005 1874 refst = dup_super(st);
1011e834 1875 if (load_super1(refst, di->fd, NULL)==0) {
e8090005 1876 struct mdp_superblock_1 *refsb = refst->sb;
111d01fc
NB
1877
1878 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1879 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1880 /* same array, so preserve events and
1881 * dev_number */
1882 sb->events = refsb->events;
1883 /* bugs in 2.6.17 and earlier mean the
1884 * dev_number chosen in Manage must be preserved
1885 */
1886 if (get_linux_version() >= 2006018)
1887 sb->dev_number = refsb->dev_number;
1888 }
e8090005 1889 free_super1(refst);
892debc8 1890 }
e8090005 1891 free(refst);
34163fc7 1892
40114213
JS
1893 if (!get_dev_size(di->fd, NULL, &dsize)) {
1894 rv = 1;
1895 goto error_out;
1896 }
111d01fc 1897 dsize >>= 9;
82d9eba6 1898
111d01fc
NB
1899 if (dsize < 24) {
1900 close(di->fd);
40114213
JS
1901 rv = 2;
1902 goto error_out;
111d01fc 1903 }
82d9eba6 1904
111d01fc
NB
1905 /*
1906 * Calculate the position of the superblock.
1907 * It is always aligned to a 4K boundary and
1908 * depending on minor_version, it can be:
1909 * 0: At least 8K, but less than 12K, from end of device
1910 * 1: At start of device
1911 * 2: 4K from start of device.
23bf42cc 1912 * data_offset has already been set.
111d01fc
NB
1913 */
1914 array_size = __le64_to_cpu(sb->size);
1b7eb672
AP
1915
1916 /* work out how much space we left for a bitmap */
1917 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1918 bitmap_super_t *bms = (bitmap_super_t *)
1919 (((char *)sb) + MAX_SB_SIZE);
1920 bm_space = calc_bitmap_size(bms, 4096) >> 9;
1921 bm_offset = (long)__le32_to_cpu(sb->bitmap_offset);
e97a7cd0
AP
1922 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
1923 bm_space = choose_ppl_space(__le32_to_cpu(sb->chunksize));
1924 if (bm_space > UINT16_MAX)
1925 bm_space = UINT16_MAX;
1926 if (st->minor_version == 0) {
1927 bm_offset = -bm_space - 8;
1928 if (bm_offset < INT16_MIN) {
1929 bm_offset = INT16_MIN;
1930 bm_space = -bm_offset - 8;
1931 }
1932 } else {
1933 bm_offset = 8;
1934 }
1935 sb->ppl.offset = __cpu_to_le16(bm_offset);
1936 sb->ppl.size = __cpu_to_le16(bm_space);
1b7eb672
AP
1937 } else {
1938 bm_space = choose_bm_space(array_size);
1939 bm_offset = 8;
1940 }
111d01fc 1941
72ca9bcf 1942 data_offset = di->data_offset;
23bf42cc
N
1943 if (data_offset == INVALID_SECTORS)
1944 data_offset = st->data_offset;
111d01fc
NB
1945 switch(st->minor_version) {
1946 case 0:
1b7eb672
AP
1947 /* Add 8 sectors for bad block log */
1948 bm_space += 8;
23bf42cc
N
1949 if (data_offset == INVALID_SECTORS)
1950 data_offset = 0;
111d01fc
NB
1951 sb_offset = dsize;
1952 sb_offset -= 8*2;
1953 sb_offset &= ~(4*2-1);
4c0ea7b0 1954 sb->data_offset = __cpu_to_le64(data_offset);
111d01fc 1955 sb->super_offset = __cpu_to_le64(sb_offset);
f21e18ca 1956 if (sb_offset < array_size + bm_space)
a380e275 1957 bm_space = sb_offset - array_size;
111d01fc 1958 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
bf95d0f3
N
1959 if (bm_space >= 8) {
1960 sb->bblog_size = __cpu_to_le16(8);
1961 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1962 }
111d01fc
NB
1963 break;
1964 case 1:
111d01fc 1965 case 2:
1b7eb672 1966 sb_offset = st->minor_version == 2 ? 8 : 0;
23bf42cc
N
1967 sb->super_offset = __cpu_to_le64(sb_offset);
1968 if (data_offset == INVALID_SECTORS)
1b7eb672 1969 data_offset = sb_offset + 16;
23bf42cc
N
1970
1971 sb->data_offset = __cpu_to_le64(data_offset);
1972 sb->data_size = __cpu_to_le64(dsize - data_offset);
1b7eb672 1973 if (data_offset >= sb_offset+bm_offset+bm_space+8) {
f79bbf4f 1974 sb->bblog_size = __cpu_to_le16(8);
1b7eb672
AP
1975 sb->bblog_offset = __cpu_to_le32(bm_offset +
1976 bm_space);
1977 } else if (data_offset >= sb_offset + 16) {
bf95d0f3 1978 sb->bblog_size = __cpu_to_le16(8);
1b7eb672 1979 /* '8' sectors for the bblog, and 'sb_offset'
bf95d0f3
N
1980 * because we want offset from superblock, not
1981 * start of device.
1982 */
1b7eb672
AP
1983 sb->bblog_offset = __cpu_to_le32(data_offset -
1984 8 - sb_offset);
bf95d0f3 1985 }
111d01fc
NB
1986 break;
1987 default:
7a862a02 1988 pr_err("Failed to write invalid metadata format 1.%i to %s\n",
e7b84f9d 1989 st->minor_version, di->devname);
40114213
JS
1990 rv = -EINVAL;
1991 goto out;
111d01fc 1992 }
86a406c2
N
1993 /* Disable badblock log on clusters, or when explicitly requested */
1994 if (st->nodes > 0 || conf_get_create_info()->bblist == 0) {
e2efe9e7
N
1995 sb->bblog_size = 0;
1996 sb->bblog_offset = 0;
1997 }
82d9eba6 1998
111d01fc
NB
1999 sb->sb_csum = calc_sb_1_csum(sb);
2000 rv = store_super1(st, di->fd);
cc1799c3
SL
2001
2002 if (rv == 0 && (di->disk.state & (1 << MD_DISK_JOURNAL))) {
2003 rv = write_empty_r5l_meta_block(st, di->fd);
2004 if (rv)
2005 goto error_out;
2006 }
2007
e97a7cd0
AP
2008 if (rv == 0 &&
2009 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
81306e02 2010 rv = st->ss->write_bitmap(st, di->fd, NodeNumUpdate);
e97a7cd0
AP
2011 } else if (rv == 0 &&
2012 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL)) {
2013 struct mdinfo info;
2014
2015 st->ss->getinfo_super(st, &info, NULL);
2016 rv = st->ss->write_init_ppl(st, &info, di->fd);
2017 }
2018
111d01fc
NB
2019 close(di->fd);
2020 di->fd = -1;
4687f160 2021 if (rv)
2022 goto error_out;
111d01fc 2023 }
40114213
JS
2024error_out:
2025 if (rv)
e7b84f9d
N
2026 pr_err("Failed to write metadata to %s\n",
2027 di->devname);
40114213 2028out:
82d9eba6
NB
2029 return rv;
2030}
111d01fc 2031#endif
82d9eba6 2032
64557c33 2033static int compare_super1(struct supertype *st, struct supertype *tst)
82d9eba6
NB
2034{
2035 /*
2036 * return:
2037 * 0 same, or first was empty, and second was copied
2038 * 1 second had wrong number
2039 * 2 wrong uuid
2040 * 3 wrong other info
2041 */
64557c33
NB
2042 struct mdp_superblock_1 *first = st->sb;
2043 struct mdp_superblock_1 *second = tst->sb;
82d9eba6
NB
2044
2045 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
2046 return 1;
2047 if (second->major_version != __cpu_to_le32(1))
2048 return 1;
2049
2050 if (!first) {
3c0bcd46 2051 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1ade5cc1 2052 pr_err("could not allocate superblock\n");
3d2c4fc7
DW
2053 return 1;
2054 }
41226756 2055 memcpy(first, second, SUPER1_SIZE);
64557c33 2056 st->sb = first;
82d9eba6
NB
2057 return 0;
2058 }
2059 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
2060 return 2;
2061
2062 if (first->ctime != second->ctime ||
2063 first->level != second->level ||
2064 first->layout != second->layout ||
2065 first->size != second->size ||
2066 first->chunksize != second->chunksize ||
2067 first->raid_disks != second->raid_disks)
2068 return 3;
2069 return 0;
2070}
2071
3da92f27 2072static int load_super1(struct supertype *st, int fd, char *devname)
82d9eba6 2073{
5dd497ee 2074 unsigned long long dsize;
82d9eba6
NB
2075 unsigned long long sb_offset;
2076 struct mdp_superblock_1 *super;
14263cf1
NB
2077 int uuid[4];
2078 struct bitmap_super_s *bsb;
bee8ec56 2079 struct misc_dev_info *misc;
308340aa 2080 struct align_fd afd;
82d9eba6 2081
3da92f27 2082 free_super1(st);
82d9eba6 2083
308340aa
JS
2084 init_afd(&afd, fd);
2085
a17a3de3 2086 if (st->ss == NULL || st->minor_version == -1) {
570c0542 2087 int bestvers = -1;
23dc1ae8 2088 struct supertype tst;
570c0542
NB
2089 __u64 bestctime = 0;
2090 /* guess... choose latest ctime */
ef609477 2091 memset(&tst, 0, sizeof(tst));
23dc1ae8
NB
2092 tst.ss = &super1;
2093 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
3da92f27
NB
2094 switch(load_super1(&tst, fd, devname)) {
2095 case 0: super = tst.sb;
570c0542
NB
2096 if (bestvers == -1 ||
2097 bestctime < __le64_to_cpu(super->ctime)) {
23dc1ae8 2098 bestvers = tst.minor_version;
570c0542
NB
2099 bestctime = __le64_to_cpu(super->ctime);
2100 }
2101 free(super);
3da92f27 2102 tst.sb = NULL;
570c0542 2103 break;
23dc1ae8 2104 case 1: return 1; /*bad device */
82d9eba6
NB
2105 case 2: break; /* bad, try next */
2106 }
2107 }
570c0542
NB
2108 if (bestvers != -1) {
2109 int rv;
23dc1ae8
NB
2110 tst.minor_version = bestvers;
2111 tst.ss = &super1;
911cead7 2112 tst.max_devs = MAX_DEVS;
3da92f27 2113 rv = load_super1(&tst, fd, devname);
23dc1ae8
NB
2114 if (rv == 0)
2115 *st = tst;
570c0542
NB
2116 return rv;
2117 }
82d9eba6
NB
2118 return 2;
2119 }
beae1dfe
NB
2120 if (!get_dev_size(fd, devname, &dsize))
2121 return 1;
2122 dsize >>= 9;
82d9eba6 2123
5dd497ee 2124 if (dsize < 24) {
82d9eba6 2125 if (devname)
e7b84f9d 2126 pr_err("%s is too small for md: size is %llu sectors.\n",
5dd497ee 2127 devname, dsize);
82d9eba6
NB
2128 return 1;
2129 }
2130
2131 /*
2132 * Calculate the position of the superblock.
2133 * It is always aligned to a 4K boundary and
14263cf1 2134 * depending on minor_version, it can be:
82d9eba6
NB
2135 * 0: At least 8K, but less than 12K, from end of device
2136 * 1: At start of device
2137 * 2: 4K from start of device.
2138 */
2139 switch(st->minor_version) {
2140 case 0:
5dd497ee 2141 sb_offset = dsize;
82d9eba6
NB
2142 sb_offset -= 8*2;
2143 sb_offset &= ~(4*2-1);
2144 break;
2145 case 1:
2146 sb_offset = 0;
2147 break;
2148 case 2:
2149 sb_offset = 4*2;
2150 break;
2151 default:
2152 return -EINVAL;
2153 }
2154
82d9eba6
NB
2155 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
2156 if (devname)
e7b84f9d 2157 pr_err("Cannot seek to superblock on %s: %s\n",
82d9eba6
NB
2158 devname, strerror(errno));
2159 return 1;
2160 }
2161
3c0bcd46 2162 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1ade5cc1 2163 pr_err("could not allocate superblock\n");
3d2c4fc7
DW
2164 return 1;
2165 }
82d9eba6 2166
1dcee1c9
JS
2167 memset(super, 0, SUPER1_SIZE);
2168
308340aa 2169 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
82d9eba6 2170 if (devname)
e7b84f9d 2171 pr_err("Cannot read superblock on %s\n",
82d9eba6
NB
2172 devname);
2173 free(super);
2174 return 1;
2175 }
2176
2177 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
2178 if (devname)
e7b84f9d 2179 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
82d9eba6
NB
2180 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
2181 free(super);
2182 return 2;
2183 }
2184
2185 if (__le32_to_cpu(super->major_version) != 1) {
2186 if (devname)
e7b84f9d 2187 pr_err("Cannot interpret superblock on %s - version is %d\n",
82d9eba6
NB
2188 devname, __le32_to_cpu(super->major_version));
2189 free(super);
2190 return 2;
2191 }
2192 if (__le64_to_cpu(super->super_offset) != sb_offset) {
2193 if (devname)
e7b84f9d 2194 pr_err("No superblock found on %s (super_offset is wrong)\n",
82d9eba6
NB
2195 devname);
2196 free(super);
2197 return 2;
2198 }
64557c33 2199 st->sb = super;
14263cf1 2200
911cead7 2201 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
bee8ec56 2202
911cead7 2203 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
bee8ec56 2204 misc->device_size = dsize;
23bf42cc
N
2205 if (st->data_offset == INVALID_SECTORS)
2206 st->data_offset = __le64_to_cpu(super->data_offset);
bee8ec56 2207
14263cf1
NB
2208 /* Now check on the bitmap superblock */
2209 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
2210 return 0;
2211 /* Read the bitmap superblock and make sure it looks
2212 * valid. If it doesn't clear the bit. An --assemble --force
2213 * should get that written out.
2214 */
b138214f 2215 locate_bitmap1(st, fd, 0);
308340aa 2216 if (aread(&afd, bsb, 512) != 512)
14263cf1
NB
2217 goto no_bitmap;
2218
3da92f27 2219 uuid_from_super1(st, uuid);
14263cf1
NB
2220 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
2221 memcmp(bsb->uuid, uuid, 16) != 0)
2222 goto no_bitmap;
2223 return 0;
2224
2225 no_bitmap:
c0c1acd6
N
2226 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
2227 & ~MD_FEATURE_BITMAP_OFFSET);
82d9eba6
NB
2228 return 0;
2229}
2230
82d9eba6
NB
2231static struct supertype *match_metadata_desc1(char *arg)
2232{
503975b9 2233 struct supertype *st = xcalloc(1, sizeof(*st));
82d9eba6 2234
4dd2df09 2235 st->container_devnm[0] = 0;
82d9eba6 2236 st->ss = &super1;
911cead7 2237 st->max_devs = MAX_DEVS;
64557c33 2238 st->sb = NULL;
23bf42cc 2239 st->data_offset = INVALID_SECTORS;
9b2a22d3
N
2240 /* leading zeros can be safely ignored. --detail generates them. */
2241 while (*arg == '0')
2242 arg++;
2243 if (strcmp(arg, "1.0") == 0 ||
2244 strcmp(arg, "1.00") == 0) {
82d9eba6
NB
2245 st->minor_version = 0;
2246 return st;
2247 }
9b2a22d3 2248 if (strcmp(arg, "1.1") == 0 ||
50827504 2249 strcmp(arg, "1.01") == 0
7d5c3964 2250 ) {
82d9eba6
NB
2251 st->minor_version = 1;
2252 return st;
2253 }
9b2a22d3 2254 if (strcmp(arg, "1.2") == 0 ||
26f467a9 2255#ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
ad90adb6 2256 strcmp(arg, "default") == 0 ||
26f467a9 2257#endif /* DEFAULT_OLD_METADATA */
9b2a22d3 2258 strcmp(arg, "1.02") == 0) {
82d9eba6
NB
2259 st->minor_version = 2;
2260 return st;
2261 }
a17a3de3 2262 if (strcmp(arg, "1") == 0 ||
17f25ca6 2263 strcmp(arg, "default") == 0) {
a17a3de3
DL
2264 st->minor_version = -1;
2265 return st;
2266 }
82d9eba6
NB
2267
2268 free(st);
2269 return NULL;
2270}
2271
34163fc7
NB
2272/* find available size on device with this devsize, using
2273 * superblock type st, and reserving 'reserve' sectors for
2274 * a possible bitmap
2275 */
641da745
N
2276static __u64 avail_size1(struct supertype *st, __u64 devsize,
2277 unsigned long long data_offset)
82d9eba6 2278{
2a528478 2279 struct mdp_superblock_1 *super = st->sb;
387fcd59 2280 int bmspace = 0;
23bf42cc 2281 int bbspace = 0;
82d9eba6
NB
2282 if (devsize < 24)
2283 return 0;
2284
2a528478 2285#ifndef MDASSEMBLE
e97a7cd0 2286 if (__le32_to_cpu(super->feature_map) & MD_FEATURE_BITMAP_OFFSET) {
2a528478
N
2287 /* hot-add. allow for actual size of bitmap */
2288 struct bitmap_super_s *bsb;
911cead7 2289 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
561ad559 2290 bmspace = calc_bitmap_size(bsb, 4096) >> 9;
e97a7cd0
AP
2291 } else if (__le32_to_cpu(super->feature_map) & MD_FEATURE_PPL) {
2292 bmspace = __le16_to_cpu(super->ppl.size);
2a528478
N
2293 }
2294#endif
88af981f 2295 /* Allow space for bad block log */
641da745 2296 if (super->bblog_size)
23bf42cc 2297 bbspace = __le16_to_cpu(super->bblog_size);
21e92547 2298
a380e275
N
2299 if (st->minor_version < 0)
2300 /* not specified, so time to set default */
2301 st->minor_version = 2;
387fcd59 2302
23bf42cc
N
2303 if (data_offset == INVALID_SECTORS)
2304 data_offset = st->data_offset;
2305
387fcd59
N
2306 if (data_offset != INVALID_SECTORS)
2307 switch(st->minor_version) {
2308 case 0:
23bf42cc 2309 return devsize - data_offset - 8*2 - bbspace;
387fcd59
N
2310 case 1:
2311 case 2:
2312 return devsize - data_offset;
2313 default:
2314 return 0;
2315 }
2316
2317 devsize -= bmspace;
2318
34163fc7
NB
2319 switch(st->minor_version) {
2320 case 0:
1bf4e2d9 2321 /* at end */
23bf42cc 2322 return ((devsize - 8*2 - bbspace ) & ~(4*2-1));
34163fc7 2323 case 1:
1bf4e2d9 2324 /* at start, 4K for superblock and possible bitmap */
23bf42cc 2325 return devsize - 4*2 - bbspace;
34163fc7 2326 case 2:
1bf4e2d9 2327 /* 4k from start, 4K for superblock and possible bitmap */
23bf42cc 2328 return devsize - (4+4)*2 - bbspace;
34163fc7
NB
2329 }
2330 return 0;
2331}
2332
1bf4e2d9 2333static int
3da92f27 2334add_internal_bitmap1(struct supertype *st,
199171a2
NB
2335 int *chunkp, int delay, int write_behind,
2336 unsigned long long size,
21e92547 2337 int may_change, int major)
34163fc7
NB
2338{
2339 /*
c0c1acd6
N
2340 * If not may_change, then this is a 'Grow' without sysfs support for
2341 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2342 * If may_change, then this is create or a Grow with sysfs syupport,
2343 * and we can put the bitmap wherever we like.
34163fc7 2344 *
f9c25f1d 2345 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
2346 */
2347
1bf4e2d9 2348 unsigned long long bits;
199171a2 2349 unsigned long long max_bits;
34163fc7 2350 unsigned long long min_chunk;
199171a2 2351 long offset;
88af981f 2352 long bbl_offset, bbl_size;
f21e18ca 2353 unsigned long long chunk = *chunkp;
37dfc3d6 2354 int room = 0;
c0c1acd6 2355 int creating = 0;
4b3eb4d2 2356 int len;
3da92f27 2357 struct mdp_superblock_1 *sb = st->sb;
911cead7 2358 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
3b7e9d0c 2359 int uuid[4];
34163fc7 2360
c0c1acd6
N
2361 if (__le64_to_cpu(sb->data_size) == 0)
2362 /* Must be creating the array, else data_size would be non-zero */
2363 creating = 1;
199171a2
NB
2364 switch(st->minor_version) {
2365 case 0:
a31128d2
N
2366 /* either 3K after the superblock (when hot-add),
2367 * or some amount of space before.
199171a2 2368 */
c0c1acd6 2369 if (creating) {
199171a2
NB
2370 /* We are creating array, so we *know* how much room has
2371 * been left.
2372 */
2373 offset = 0;
88af981f 2374 bbl_size = 8;
6ac17e73 2375 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
199171a2
NB
2376 } else {
2377 room = __le64_to_cpu(sb->super_offset)
2378 - __le64_to_cpu(sb->data_offset)
2379 - __le64_to_cpu(sb->data_size);
88af981f
N
2380 bbl_size = __le16_to_cpu(sb->bblog_size);
2381 if (bbl_size < 8)
2382 bbl_size = 8;
2383 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
2384 if (bbl_size < -bbl_offset)
2385 bbl_size = -bbl_offset;
c0c1acd6
N
2386
2387 if (!may_change || (room < 3*2 &&
7716570e 2388 __le32_to_cpu(sb->max_dev) <= 384)) {
199171a2
NB
2389 room = 3*2;
2390 offset = 1*2;
88af981f 2391 bbl_size = 0;
199171a2
NB
2392 } else {
2393 offset = 0; /* means movable offset */
2394 }
2395 }
2396 break;
2397 case 1:
2398 case 2: /* between superblock and data */
c0c1acd6 2399 if (creating) {
199171a2 2400 offset = 4*2;
88af981f 2401 bbl_size = 8;
6ac17e73 2402 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
199171a2
NB
2403 } else {
2404 room = __le64_to_cpu(sb->data_offset)
2405 - __le64_to_cpu(sb->super_offset);
88af981f
N
2406 bbl_size = __le16_to_cpu(sb->bblog_size);
2407 if (bbl_size)
2408 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
2409 else
2410 bbl_size = 8;
2411
c0c1acd6
N
2412 if (!may_change) {
2413 room -= 2; /* Leave 1K for superblock */
199171a2 2414 offset = 2;
88af981f 2415 bbl_size = 0;
199171a2 2416 } else {
c0c1acd6 2417 room -= 4*2; /* leave 4K for superblock */
199171a2
NB
2418 offset = 4*2;
2419 }
2420 }
2421 break;
ae491d1e 2422 default:
2ec2b7e9 2423 return -ENOSPC;
199171a2 2424 }
1bf4e2d9 2425
88af981f 2426 room -= bbl_size;
199171a2
NB
2427 if (chunk == UnSet && room > 128*2)
2428 /* Limit to 128K of bitmap when chunk size not requested */
2429 room = 128*2;
1bf4e2d9 2430
268cccac
N
2431 if (room <= 1)
2432 /* No room for a bitmap */
2ec2b7e9 2433 return -ENOSPC;
268cccac 2434
199171a2 2435 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
2436
2437 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 2438 bits = (size*512)/min_chunk +1;
34163fc7
NB
2439 while (bits > max_bits) {
2440 min_chunk *= 2;
2441 bits = (bits+1)/2;
2442 }
b8ab2a50
N
2443 if (chunk == UnSet) {
2444 /* For practical purpose, 64Meg is a good
2445 * default chunk size for internal bitmaps.
2446 */
34163fc7 2447 chunk = min_chunk;
b8ab2a50
N
2448 if (chunk < 64*1024*1024)
2449 chunk = 64*1024*1024;
2450 } else if (chunk < min_chunk)
2ec2b7e9 2451 return -EINVAL; /* chunk size too small */
21e92547 2452 if (chunk == 0) /* rounding problem */
2ec2b7e9 2453 return -EINVAL;
34163fc7 2454
199171a2 2455 if (offset == 0) {
a31128d2
N
2456 /* start bitmap on a 4K boundary with enough space for
2457 * the bitmap
2458 */
199171a2 2459 bits = (size*512) / chunk + 1;
a31128d2
N
2460 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
2461 room *= 8; /* convert 4K blocks to sectors */
88af981f 2462 offset = -room - bbl_size;
199171a2
NB
2463 }
2464
34a13953 2465 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
34163fc7 2466
c0c1acd6
N
2467 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2468 | MD_FEATURE_BITMAP_OFFSET);
29e766a5 2469 memset(bms, 0, sizeof(*bms));
34163fc7 2470 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 2471 bms->version = __cpu_to_le32(major);
3b7e9d0c
LB
2472 uuid_from_super1(st, uuid);
2473 memcpy(bms->uuid, uuid, 16);
34163fc7
NB
2474 bms->chunksize = __cpu_to_le32(chunk);
2475 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 2476 bms->sync_size = __cpu_to_le64(size);
34163fc7 2477 bms->write_behind = __cpu_to_le32(write_behind);
529e2aa5 2478 bms->nodes = __cpu_to_le32(st->nodes);
6d9c7c25
GR
2479 if (st->nodes)
2480 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2481 | MD_FEATURE_BITMAP_VERSIONED);
4b3eb4d2
JS
2482 if (st->cluster_name) {
2483 len = sizeof(bms->cluster_name);
2484 strncpy((char *)bms->cluster_name, st->cluster_name, len);
2485 bms->cluster_name[len - 1] = '\0';
2486 }
34163fc7 2487
199171a2 2488 *chunkp = chunk;
2ec2b7e9 2489 return 0;
34163fc7
NB
2490}
2491
b138214f 2492static int locate_bitmap1(struct supertype *st, int fd, int node_num)
34163fc7 2493{
34163fc7 2494 unsigned long long offset;
1e0d770c
NB
2495 struct mdp_superblock_1 *sb;
2496 int mustfree = 0;
53e76b1d 2497 int ret;
34163fc7 2498
3da92f27
NB
2499 if (!st->sb) {
2500 if (st->ss->load_super(st, fd, NULL))
53e76b1d 2501 return -1; /* no error I hope... */
1e0d770c
NB
2502 mustfree = 1;
2503 }
3da92f27 2504 sb = st->sb;
e478dc86 2505
53e76b1d
GJ
2506 if ((__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
2507 ret = 0;
2508 else
2509 ret = -1;
1bf4e2d9 2510 offset = __le64_to_cpu(sb->super_offset);
b138214f 2511 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
1e0d770c 2512 if (mustfree)
f6d75de8 2513 free(sb);
1bf4e2d9 2514 lseek64(fd, offset<<9, 0);
53e76b1d 2515 return ret;
34163fc7
NB
2516}
2517
0aa2f15b 2518static int write_bitmap1(struct supertype *st, int fd, enum bitmap_update update)
34163fc7 2519{
3da92f27 2520 struct mdp_superblock_1 *sb = st->sb;
911cead7 2521 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
34163fc7 2522 int rv = 0;
d669228f 2523 void *buf;
4b3eb4d2 2524 int towrite, n, len;
308340aa 2525 struct align_fd afd;
95a05b37 2526 unsigned int i = 0;
7e6e839a 2527 unsigned long long total_bm_space, bm_space_per_node;
308340aa 2528
0aa2f15b
GJ
2529 switch (update) {
2530 case NameUpdate:
2531 /* update cluster name */
2532 if (st->cluster_name) {
4b3eb4d2
JS
2533 len = sizeof(bms->cluster_name);
2534 memset((char *)bms->cluster_name, 0, len);
2535 strncpy((char *)bms->cluster_name,
2536 st->cluster_name, len);
2537 bms->cluster_name[len - 1] = '\0';
0aa2f15b
GJ
2538 }
2539 break;
7e6e839a
GJ
2540 case NodeNumUpdate:
2541 /* cluster md only supports superblock 1.2 now */
bbc24bb3 2542 if (st->minor_version != 2 && bms->version == BITMAP_MAJOR_CLUSTERED) {
7e6e839a
GJ
2543 pr_err("Warning: cluster md only works with superblock 1.2\n");
2544 return -EINVAL;
2545 }
2546
45a87c2f 2547 if (bms->version == BITMAP_MAJOR_CLUSTERED) {
9d67f649 2548 if (__cpu_to_le32(st->nodes) < bms->nodes) {
45a87c2f
GJ
2549 /* Since the nodes num is not increased, no need to check the space
2550 * is enough or not, just update bms->nodes */
2551 bms->nodes = __cpu_to_le32(st->nodes);
2552 break;
2553 }
2554 } else {
2555 /* no need to change bms->nodes for other bitmap types */
2556 if (st->nodes)
2557 pr_err("Warning: --nodes option is only suitable for clustered bitmap\n");
2558 break;
261b57fe
GJ
2559 }
2560
7e6e839a
GJ
2561 /* Each node has an independent bitmap, it is necessary to calculate the
2562 * space is enough or not, first get how many bytes for the total bitmap */
2563 bm_space_per_node = calc_bitmap_size(bms, 4096);
2564
2565 total_bm_space = 512 * (__le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset));
2566 total_bm_space = total_bm_space - 4096; /* leave another 4k for superblock */
2567
2568 if (bm_space_per_node * st->nodes > total_bm_space) {
2569 pr_err("Warning: The max num of nodes can't exceed %llu\n",
2570 total_bm_space / bm_space_per_node);
2571 return -ENOMEM;
2572 }
2573
2574 bms->nodes = __cpu_to_le32(st->nodes);
2575 break;
0aa2f15b
GJ
2576 case NoUpdate:
2577 default:
2578 break;
2579 }
2580
308340aa 2581 init_afd(&afd, fd);
34163fc7 2582
b138214f 2583 locate_bitmap1(st, fd, 0);
34163fc7 2584
d669228f
JS
2585 if (posix_memalign(&buf, 4096, 4096))
2586 return -ENOMEM;
2587
95a05b37
GJ
2588 do {
2589 /* Only the bitmap[0] should resync
2590 * whole device on initial assembly
2591 */
2592 if (i)
2593 memset(buf, 0x00, 4096);
34163fc7 2594 else
95a05b37
GJ
2595 memset(buf, 0xff, 4096);
2596 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2597
119b66a4
GJ
2598 /*
2599 * use 4096 boundary if bitmap_offset is aligned
2600 * with 8 sectors, then it should compatible with
2601 * older mdadm.
2602 */
2603 if (__le32_to_cpu(sb->bitmap_offset) & 7)
2604 towrite = calc_bitmap_size(bms, 512);
2605 else
2606 towrite = calc_bitmap_size(bms, 4096);
95a05b37
GJ
2607 while (towrite > 0) {
2608 n = towrite;
2609 if (n > 4096)
2610 n = 4096;
2611 n = awrite(&afd, buf, n);
2612 if (n > 0)
2613 towrite -= n;
2614 else
2615 break;
2616 if (i)
2617 memset(buf, 0x00, 4096);
2618 else
2619 memset(buf, 0xff, 4096);
2620 }
2621 fsync(fd);
2622 if (towrite) {
2623 rv = -2;
34163fc7 2624 break;
95a05b37
GJ
2625 }
2626 } while (++i < __le32_to_cpu(bms->nodes));
34163fc7 2627
d669228f 2628 free(buf);
34163fc7 2629 return rv;
82d9eba6
NB
2630}
2631
3da92f27 2632static void free_super1(struct supertype *st)
df37ffc0 2633{
d15a1f72 2634
3da92f27
NB
2635 if (st->sb)
2636 free(st->sb);
1cc7f4fe
N
2637 while (st->info) {
2638 struct devinfo *di = st->info;
2639 st->info = di->next;
2640 if (di->fd >= 0)
2641 close(di->fd);
2642 free(di);
2643 }
3da92f27 2644 st->sb = NULL;
df37ffc0
NB
2645}
2646
0e600426 2647#ifndef MDASSEMBLE
17f25ca6
NB
2648static int validate_geometry1(struct supertype *st, int level,
2649 int layout, int raiddisks,
c21e737b 2650 int *chunk, unsigned long long size,
af4348dd 2651 unsigned long long data_offset,
2c514b71 2652 char *subdev, unsigned long long *freesize,
5308f117 2653 int consistency_policy, int verbose)
17f25ca6 2654{
641da745
N
2655 unsigned long long ldsize, devsize;
2656 int bmspace;
23bf42cc 2657 unsigned long long headroom;
17f25ca6
NB
2658 int fd;
2659
b42f577a
N
2660 if (level == LEVEL_CONTAINER) {
2661 if (verbose)
e7b84f9d 2662 pr_err("1.x metadata does not support containers\n");
17f25ca6 2663 return 0;
b42f577a 2664 }
7ccc4cc4 2665 if (*chunk == UnSet)
bb7295f1
N
2666 *chunk = DEFAULT_CHUNK;
2667
17f25ca6
NB
2668 if (!subdev)
2669 return 1;
2670
23bf42cc
N
2671 if (st->minor_version < 0)
2672 /* not specified, so time to set default */
2673 st->minor_version = 2;
2674
17f25ca6
NB
2675 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2676 if (fd < 0) {
2c514b71 2677 if (verbose)
e7b84f9d 2678 pr_err("super1.x cannot open %s: %s\n",
2c514b71 2679 subdev, strerror(errno));
17f25ca6
NB
2680 return 0;
2681 }
2c514b71 2682
17f25ca6
NB
2683 if (!get_dev_size(fd, subdev, &ldsize)) {
2684 close(fd);
2685 return 0;
2686 }
2687 close(fd);
2688
641da745
N
2689 devsize = ldsize >> 9;
2690 if (devsize < 24) {
2691 *freesize = 0;
2692 return 0;
2693 }
2694
e97a7cd0
AP
2695 /* creating: allow suitable space for bitmap or PPL */
2696 bmspace = consistency_policy == CONSISTENCY_POLICY_PPL ?
2697 choose_ppl_space((*chunk)*2) : choose_bm_space(devsize);
641da745 2698
23bf42cc
N
2699 if (data_offset == INVALID_SECTORS)
2700 data_offset = st->data_offset;
2701 if (data_offset == INVALID_SECTORS)
2702 switch (st->minor_version) {
641da745 2703 case 0:
23bf42cc
N
2704 data_offset = 0;
2705 break;
641da745
N
2706 case 1:
2707 case 2:
23bf42cc
N
2708 /* Choose data offset appropriate for this device
2709 * and use as default for whole array.
2710 * The data_offset must allow for bitmap space
2711 * and base metadata, should allow for some headroom
2712 * for reshape, and should be rounded to multiple
2713 * of 1M.
2714 * Headroom is limited to 128M, but aim for about 0.1%
641da745 2715 */
23bf42cc 2716 headroom = 128*1024*2;
641da745
N
2717 while ((headroom << 10) > devsize &&
2718 (*chunk == 0 ||
2719 headroom / 2 >= ((unsigned)(*chunk)*2)*2))
2720 headroom >>= 1;
23bf42cc
N
2721 data_offset = 12*2 + bmspace + headroom;
2722 #define ONE_MEG (2*1024)
2723 if (data_offset > ONE_MEG)
2724 data_offset = (data_offset / ONE_MEG) * ONE_MEG;
641da745
N
2725 break;
2726 }
23bf42cc
N
2727 if (st->data_offset == INVALID_SECTORS)
2728 st->data_offset = data_offset;
2729 switch(st->minor_version) {
2730 case 0: /* metadata at end. Round down and subtract space to reserve */
2731 devsize = (devsize & ~(4ULL*2-1));
e97a7cd0 2732 /* space for metadata, bblog, bitmap/ppl */
4c0ea7b0 2733 devsize -= 8*2 + 8 + bmspace;
23bf42cc
N
2734 break;
2735 case 1:
2736 case 2:
2737 devsize -= data_offset;
2738 break;
641da745 2739 }
23bf42cc 2740 *freesize = devsize;
17f25ca6
NB
2741 return 1;
2742}
0e600426 2743#endif /* MDASSEMBLE */
17f25ca6 2744
afa368f4
N
2745void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
2746{
2747 /* Create a v1.0 superblock based on 'info'*/
2748 void *ret;
2749 struct mdp_superblock_1 *sb;
2750 int i;
afa368f4
N
2751 unsigned long long offset;
2752
2753 if (posix_memalign(&ret, 4096, 1024) != 0)
2754 return NULL;
2755 sb = ret;
2756 memset(ret, 0, 1024);
2757 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
2758 sb->major_version = __cpu_to_le32(1);
2759
2760 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
2761 sprintf(sb->set_name, "%d", sb0->md_minor);
2762 sb->ctime = __cpu_to_le32(info->array.ctime+1);
2763 sb->level = __cpu_to_le32(info->array.level);
2764 sb->layout = __cpu_to_le32(info->array.layout);
2765 sb->size = __cpu_to_le64(info->component_size);
2766 sb->chunksize = __cpu_to_le32(info->array.chunk_size/512);
2767 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
ccec2685
N
2768 if (info->array.level > 0)
2769 sb->data_size = sb->size;
2770 else
2771 sb->data_size = st->ss->avail_size(st, st->devsize/512, 0);
afa368f4
N
2772 sb->resync_offset = MaxSector;
2773 sb->max_dev = __cpu_to_le32(MD_SB_DISKS);
2774 sb->dev_number = __cpu_to_le32(info->disk.number);
2775 sb->utime = __cpu_to_le64(info->array.utime);
2776
2777 offset = st->devsize/512 - 8*2;
2778 offset &= ~(4*2-1);
2779 sb->super_offset = __cpu_to_le64(offset);
2780 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2781
c5f71c24 2782 random_uuid(sb->device_uuid);
afa368f4
N
2783
2784 for (i = 0; i < MD_SB_DISKS; i++) {
2785 int state = sb0->disks[i].state;
fa7574f6 2786 sb->dev_roles[i] = MD_DISK_ROLE_SPARE;
afa368f4
N
2787 if ((state & (1<<MD_DISK_SYNC)) &&
2788 !(state & (1<<MD_DISK_FAULTY)))
2789 sb->dev_roles[i] = __cpu_to_le16(sb0->disks[i].raid_disk);
2790 }
2791 sb->sb_csum = calc_sb_1_csum(sb);
2792 return ret;
2793}
2794
82d9eba6 2795struct superswitch super1 = {
c7654afc 2796#ifndef MDASSEMBLE
82d9eba6
NB
2797 .examine_super = examine_super1,
2798 .brief_examine_super = brief_examine_super1,
0d726f17 2799 .export_examine_super = export_examine_super1,
82d9eba6
NB
2800 .detail_super = detail_super1,
2801 .brief_detail_super = brief_detail_super1,
0d726f17 2802 .export_detail_super = export_detail_super1,
111d01fc 2803 .write_init_super = write_init_super1,
0e600426
N
2804 .validate_geometry = validate_geometry1,
2805 .add_to_super = add_to_super1,
6d388a88 2806 .examine_badblocks = examine_badblocks_super1,
74db60b0 2807 .copy_metadata = copy_metadata1,
e97a7cd0 2808 .write_init_ppl = write_init_ppl1,
c7654afc 2809#endif
83b6208e 2810 .match_home = match_home1,
82d9eba6
NB
2811 .uuid_from_super = uuid_from_super1,
2812 .getinfo_super = getinfo_super1,
00bbdbda 2813 .container_content = container_content1,
82d9eba6 2814 .update_super = update_super1,
82d9eba6 2815 .init_super = init_super1,
82d9eba6 2816 .store_super = store_super1,
82d9eba6
NB
2817 .compare_super = compare_super1,
2818 .load_super = load_super1,
2819 .match_metadata_desc = match_metadata_desc1,
2820 .avail_size = avail_size1,
34163fc7
NB
2821 .add_internal_bitmap = add_internal_bitmap1,
2822 .locate_bitmap = locate_bitmap1,
2823 .write_bitmap = write_bitmap1,
df37ffc0 2824 .free_super = free_super1,
f277ce36
NB
2825#if __BYTE_ORDER == BIG_ENDIAN
2826 .swapuuid = 0,
2827#else
2828 .swapuuid = 1,
2829#endif
31015d57 2830 .name = "1.x",
82d9eba6 2831};