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