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