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