]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Report replacement devices correctly with --detail and --examine
[thirdparty/mdadm.git] / super1.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
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 */
33 struct 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;
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 */
54
55 /* These are only valid with feature bit '4' */
56 __u32 new_level; /* new level we are reshaping to */
57 __u64 reshape_position; /* next address in array-space for reshape */
58 __u32 delta_disks; /* change in number of raid_disks */
59 __u32 new_layout; /* new layout */
60 __u32 new_chunk; /* new chunk size (bytes) */
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 */
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 */
74 __u8 devflags; /* per-device flags. Only one defined...*/
75 #define WriteMostly1 1 /* mask for writemostly flag in above */
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 */
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 */
87 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
88 __u32 max_dev; /* size of dev_roles[] array to consider */
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
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)
104 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
105 + sizeof(struct misc_dev_info))
106
107 struct misc_dev_info {
108 __u64 device_size;
109 };
110
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 */
116 #define MD_FEATURE_RESHAPE_ACTIVE 4
117 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
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 )
135
136 #ifndef offsetof
137 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
138 #endif
139 static 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;
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;
156 for (; size>=4; size -= 4 ) {
157 newcsum += __le32_to_cpu(*isuper);
158 isuper++;
159 }
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;
166 return __cpu_to_le32(csum);
167 }
168
169 /*
170 * Information related to file descriptor used for aligned reads/writes.
171 * Cache the block size.
172 */
173 struct align_fd {
174 int fd;
175 int blk_sz;
176 };
177
178 static 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
186 static char abuf[4096+4096];
187 static int aread(struct align_fd *afd, void *buf, int len)
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 */
194 int bsize, iosize;
195 char *b;
196 int n;
197
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");
204 return -1;
205 }
206 b = ROUND_UP_PTR((char *)abuf, 4096);
207
208 for (iosize = 0; iosize < len; iosize += bsize)
209 ;
210 n = read(afd->fd, b, iosize);
211 if (n <= 0)
212 return n;
213 lseek(afd->fd, len - n, 1);
214 if (n > len)
215 n = len;
216 memcpy(buf, b, n);
217 return n;
218 }
219
220 static int awrite(struct align_fd *afd, void *buf, int len)
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 */
228 int bsize, iosize;
229 char *b;
230 int n;
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");
237 return -1;
238 }
239 b = ROUND_UP_PTR((char *)abuf, 4096);
240
241 for (iosize = 0; iosize < len ; iosize += bsize)
242 ;
243
244 if (len != iosize) {
245 n = read(afd->fd, b, iosize);
246 if (n <= 0)
247 return n;
248 lseek(afd->fd, -n, 1);
249 }
250
251 memcpy(b, buf, len);
252 n = write(afd->fd, b, iosize);
253 if (n <= 0)
254 return n;
255 lseek(afd->fd, len - n, 1);
256 return len;
257 }
258
259 #ifndef MDASSEMBLE
260 static void examine_super1(struct supertype *st, char *homehost)
261 {
262 struct mdp_superblock_1 *sb = st->sb;
263 time_t atime;
264 unsigned int d;
265 int role;
266 int delta_extra = 0;
267 int i;
268 char *c;
269 int l = homehost ? strlen(homehost) : 0;
270 int layout;
271 unsigned long long sb_offset;
272
273 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
274 printf(" Version : 1");
275 sb_offset = __le64_to_cpu(sb->super_offset);
276 if (sb_offset <= 4)
277 printf(".1\n");
278 else if (sb_offset <= 8)
279 printf(".2\n");
280 else
281 printf(".0\n");
282 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
283 printf(" Array UUID : ");
284 for (i=0; i<16; i++) {
285 if ((i&3)==0 && i != 0) printf(":");
286 printf("%02x", sb->set_uuid[i]);
287 }
288 printf("\n");
289 printf(" Name : %.32s", sb->set_name);
290 if (l > 0 && l < 32 &&
291 sb->set_name[l] == ':' &&
292 strncmp(sb->set_name, homehost, l) == 0)
293 printf(" (local to host %s)", homehost);
294 printf("\n");
295 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
296 printf(" Creation Time : %.24s\n", ctime(&atime));
297 c=map_num(pers, __le32_to_cpu(sb->level));
298 printf(" Raid Level : %s\n", c?c:"-unknown-");
299 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
300 printf("\n");
301 printf(" Avail Dev Size : %llu%s\n",
302 (unsigned long long)__le64_to_cpu(sb->data_size),
303 human_size(__le64_to_cpu(sb->data_size)<<9));
304 if (__le32_to_cpu(sb->level) > 0) {
305 int ddsks = 0, ddsks_denom = 1;
306 switch(__le32_to_cpu(sb->level)) {
307 case 1: ddsks=1;break;
308 case 4:
309 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
310 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
311 case 10:
312 layout = __le32_to_cpu(sb->layout);
313 ddsks = __le32_to_cpu(sb->raid_disks);
314 ddsks_denom = (layout&255) * ((layout>>8)&255);
315 }
316 if (ddsks) {
317 long long asize = __le64_to_cpu(sb->size);
318 asize = (asize << 9) * ddsks / ddsks_denom;
319 printf(" Array Size : %llu%s\n",
320 asize >> 10, human_size(asize));
321 }
322 if (sb->size != sb->data_size)
323 printf(" Used Dev Size : %llu%s\n",
324 (unsigned long long)__le64_to_cpu(sb->size),
325 human_size(__le64_to_cpu(sb->size)<<9));
326 }
327 if (sb->data_offset)
328 printf(" Data Offset : %llu sectors\n",
329 (unsigned long long)__le64_to_cpu(sb->data_offset));
330 if (sb->new_offset) {
331 unsigned long long offset = __le64_to_cpu(sb->data_offset);
332 offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
333 printf(" New Offset : %llu sectors\n", offset);
334 }
335 printf(" Super Offset : %llu sectors\n",
336 (unsigned long long)__le64_to_cpu(sb->super_offset));
337 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
338 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
339 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
340 printf(" Device UUID : ");
341 for (i=0; i<16; i++) {
342 if ((i&3)==0 && i != 0) printf(":");
343 printf("%02x", sb->device_uuid[i]);
344 }
345 printf("\n");
346 printf("\n");
347 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
348 printf("Internal Bitmap : %ld sectors from superblock\n",
349 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
350 }
351 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
352 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
353 human_size(__le64_to_cpu(sb->reshape_position)<<9));
354 if (__le32_to_cpu(sb->delta_disks)) {
355 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
356 printf(" (%d->%d)\n",
357 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
358 __le32_to_cpu(sb->raid_disks));
359 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
360 delta_extra = -__le32_to_cpu(sb->delta_disks);
361 }
362 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
363 c = map_num(pers, __le32_to_cpu(sb->new_level));
364 printf(" New Level : %s\n", c?c:"-unknown-");
365 }
366 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
367 if (__le32_to_cpu(sb->level) == 5) {
368 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
369 printf(" New Layout : %s\n", c?c:"-unknown-");
370 }
371 if (__le32_to_cpu(sb->level) == 6) {
372 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
373 printf(" New Layout : %s\n", c?c:"-unknown-");
374 }
375 if (__le32_to_cpu(sb->level) == 10) {
376 printf(" New Layout :");
377 print_r10_layout(__le32_to_cpu(sb->new_layout));
378 printf("\n");
379 }
380 }
381 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
382 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
383 printf("\n");
384 }
385 if (sb->devflags) {
386 printf(" Flags :");
387 if (sb->devflags & WriteMostly1)
388 printf(" write-mostly");
389 printf("\n");
390 }
391
392 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
393 printf(" Update Time : %.24s\n", ctime(&atime));
394
395 if (sb->bblog_size && sb->bblog_offset) {
396 printf(" Bad Block Log : %d entries available at offset %ld sectors",
397 __le16_to_cpu(sb->bblog_size)*512/8,
398 (long)__le32_to_cpu(sb->bblog_offset));
399 if (sb->feature_map &
400 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
401 printf(" - bad blocks present.");
402 printf("\n");
403 }
404
405
406 if (calc_sb_1_csum(sb) == sb->sb_csum)
407 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
408 else
409 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
410 __le32_to_cpu(calc_sb_1_csum(sb)));
411 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
412 printf("\n");
413 if (__le32_to_cpu(sb->level) == 5) {
414 c = map_num(r5layout, __le32_to_cpu(sb->layout));
415 printf(" Layout : %s\n", c?c:"-unknown-");
416 }
417 if (__le32_to_cpu(sb->level) == 6) {
418 c = map_num(r6layout, __le32_to_cpu(sb->layout));
419 printf(" Layout : %s\n", c?c:"-unknown-");
420 }
421 if (__le32_to_cpu(sb->level) == 10) {
422 int lo = __le32_to_cpu(sb->layout);
423 printf(" Layout :");
424 print_r10_layout(lo);
425 printf("\n");
426 }
427 switch(__le32_to_cpu(sb->level)) {
428 case 0:
429 case 4:
430 case 5:
431 case 6:
432 case 10:
433 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
434 break;
435 case -1:
436 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
437 break;
438 default: break;
439 }
440 printf("\n");
441 #if 0
442 /* This turns out to just be confusing */
443 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
444 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
445 if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff)
446 break;
447 for (d=0; d < i; d++) {
448 int role = __le16_to_cpu(sb->dev_roles[d]);
449 if (d) printf(", ");
450 if (role == 0xffff) printf("empty");
451 else if(role == 0xfffe) printf("failed");
452 else printf("%d", role);
453 }
454 printf(")\n");
455 #endif
456 printf(" Device Role : ");
457 d = __le32_to_cpu(sb->dev_number);
458 if (d < __le32_to_cpu(sb->max_dev))
459 role = __le16_to_cpu(sb->dev_roles[d]);
460 else
461 role = 0xFFFF;
462 if (role >= 0xFFFE)
463 printf("spare\n");
464 else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
465 printf("Replacement device %d\n", role);
466 else
467 printf("Active device %d\n", role);
468
469 printf(" Array State : ");
470 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
471 int cnt = 0;
472 unsigned int i;
473 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
474 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
475 if (role == d)
476 cnt++;
477 }
478 if (cnt == 2)
479 printf("R");
480 else if (cnt == 1)
481 printf("A");
482 else if (cnt == 0)
483 printf(".");
484 else
485 printf("?");
486 }
487 #if 0
488 /* This is confusing too */
489 faulty = 0;
490 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
491 int role = __le16_to_cpu(sb->dev_roles[i]);
492 if (role == 0xFFFE)
493 faulty++;
494 }
495 if (faulty) printf(" %d failed", faulty);
496 #endif
497 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
498 printf("\n");
499 }
500
501
502 static void brief_examine_super1(struct supertype *st, int verbose)
503 {
504 struct mdp_superblock_1 *sb = st->sb;
505 int i;
506 unsigned long long sb_offset;
507 char *nm;
508 char *c=map_num(pers, __le32_to_cpu(sb->level));
509
510 nm = strchr(sb->set_name, ':');
511 if (nm)
512 nm++;
513 else if (sb->set_name[0])
514 nm = sb->set_name;
515 else
516 nm = NULL;
517
518 printf("ARRAY ");
519 if (nm) {
520 printf("/dev/md/");
521 print_escape(nm);
522 putchar(' ');
523 }
524 if (verbose && c)
525 printf(" level=%s", c);
526 sb_offset = __le64_to_cpu(sb->super_offset);
527 if (sb_offset <= 4)
528 printf(" metadata=1.1 ");
529 else if (sb_offset <= 8)
530 printf(" metadata=1.2 ");
531 else
532 printf(" metadata=1.0 ");
533 if (verbose)
534 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
535 printf("UUID=");
536 for (i=0; i<16; i++) {
537 if ((i&3)==0 && i != 0) printf(":");
538 printf("%02x", sb->set_uuid[i]);
539 }
540 if (sb->set_name[0]) {
541 printf(" name=");
542 print_quoted(sb->set_name);
543 }
544 printf("\n");
545 }
546
547 static void export_examine_super1(struct supertype *st)
548 {
549 struct mdp_superblock_1 *sb = st->sb;
550 int i;
551 int len = 32;
552 int layout;
553
554 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
555 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
556 for (i=0; i<32; i++)
557 if (sb->set_name[i] == '\n' ||
558 sb->set_name[i] == '\0') {
559 len = i;
560 break;
561 }
562 if (len)
563 printf("MD_NAME=%.*s\n", len, sb->set_name);
564 if (__le32_to_cpu(sb->level) > 0) {
565 int ddsks = 0, ddsks_denom = 1;
566 switch(__le32_to_cpu(sb->level)) {
567 case 1: ddsks=1;break;
568 case 4:
569 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
570 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
571 case 10:
572 layout = __le32_to_cpu(sb->layout);
573 ddsks = __le32_to_cpu(sb->raid_disks);
574 ddsks_denom = (layout&255) * ((layout>>8)&255);
575 }
576 if (ddsks) {
577 long long asize = __le64_to_cpu(sb->size);
578 asize = (asize << 9) * ddsks / ddsks_denom;
579 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize,JEDEC));
580 }
581 }
582 printf("MD_UUID=");
583 for (i=0; i<16; i++) {
584 if ((i&3)==0 && i != 0) printf(":");
585 printf("%02x", sb->set_uuid[i]);
586 }
587 printf("\n");
588 printf("MD_UPDATE_TIME=%llu\n",
589 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
590 printf("MD_DEV_UUID=");
591 for (i=0; i<16; i++) {
592 if ((i&3)==0 && i != 0) printf(":");
593 printf("%02x", sb->device_uuid[i]);
594 }
595 printf("\n");
596 printf("MD_EVENTS=%llu\n",
597 (unsigned long long)__le64_to_cpu(sb->events));
598 }
599
600 static void detail_super1(struct supertype *st, char *homehost)
601 {
602 struct mdp_superblock_1 *sb = st->sb;
603 int i;
604 int l = homehost ? strlen(homehost) : 0;
605
606 printf(" Name : %.32s", sb->set_name);
607 if (l > 0 && l < 32 &&
608 sb->set_name[l] == ':' &&
609 strncmp(sb->set_name, homehost, l) == 0)
610 printf(" (local to host %s)", homehost);
611 printf("\n UUID : ");
612 for (i=0; i<16; i++) {
613 if ((i&3)==0 && i != 0) printf(":");
614 printf("%02x", sb->set_uuid[i]);
615 }
616 printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events));
617 }
618
619 static void brief_detail_super1(struct supertype *st)
620 {
621 struct mdp_superblock_1 *sb = st->sb;
622 int i;
623
624 if (sb->set_name[0]) {
625 printf(" name=");
626 print_quoted(sb->set_name);
627 }
628 printf(" UUID=");
629 for (i=0; i<16; i++) {
630 if ((i&3)==0 && i != 0) printf(":");
631 printf("%02x", sb->set_uuid[i]);
632 }
633 }
634
635 static void export_detail_super1(struct supertype *st)
636 {
637 struct mdp_superblock_1 *sb = st->sb;
638 int i;
639 int len = 32;
640
641 for (i=0; i<32; i++)
642 if (sb->set_name[i] == '\n' ||
643 sb->set_name[i] == '\0') {
644 len = i;
645 break;
646 }
647 if (len)
648 printf("MD_NAME=%.*s\n", len, sb->set_name);
649 }
650
651 #endif
652
653 static int match_home1(struct supertype *st, char *homehost)
654 {
655 struct mdp_superblock_1 *sb = st->sb;
656 int l = homehost ? strlen(homehost) : 0;
657
658 return (l > 0 && l < 32 &&
659 sb->set_name[l] == ':' &&
660 strncmp(sb->set_name, homehost, l) == 0);
661 }
662
663 static void uuid_from_super1(struct supertype *st, int uuid[4])
664 {
665 struct mdp_superblock_1 *super = st->sb;
666 char *cuuid = (char*)uuid;
667 int i;
668 for (i=0; i<16; i++)
669 cuuid[i] = super->set_uuid[i];
670 }
671
672 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
673 {
674 struct mdp_superblock_1 *sb = st->sb;
675 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
676 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
677 int working = 0;
678 unsigned int i;
679 unsigned int role;
680 unsigned int map_disks = info->array.raid_disks;
681 unsigned long long super_offset;
682 unsigned long long data_size;
683
684 memset(info, 0, sizeof(*info));
685 info->array.major_version = 1;
686 info->array.minor_version = st->minor_version;
687 info->array.patch_version = 0;
688 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
689 info->array.level = __le32_to_cpu(sb->level);
690 info->array.layout = __le32_to_cpu(sb->layout);
691 info->array.md_minor = -1;
692 info->array.ctime = __le64_to_cpu(sb->ctime);
693 info->array.utime = __le64_to_cpu(sb->utime);
694 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
695 info->array.state =
696 (__le64_to_cpu(sb->resync_offset) == MaxSector)
697 ? 1 : 0;
698
699 info->data_offset = __le64_to_cpu(sb->data_offset);
700 info->component_size = __le64_to_cpu(sb->size);
701 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
702 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
703
704 info->disk.major = 0;
705 info->disk.minor = 0;
706 info->disk.number = __le32_to_cpu(sb->dev_number);
707 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
708 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
709 role = 0xfffe;
710 else
711 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
712
713 super_offset = __le64_to_cpu(sb->super_offset);
714 data_size = __le64_to_cpu(sb->size);
715 if (info->data_offset < super_offset) {
716 unsigned long long end;
717 info->space_before = info->data_offset;
718 end = super_offset;
719 if (info->bitmap_offset < 0)
720 end += info->bitmap_offset;
721 if (info->data_offset + data_size < end)
722 info->space_after = end - data_size - info->data_offset;
723 else
724 info->space_after = 0;
725 } else {
726 info->space_before = (info->data_offset -
727 super_offset);
728 if (info->bitmap_offset > 0) {
729 unsigned long long bmend = info->bitmap_offset;
730 unsigned long long size = __le64_to_cpu(bsb->sync_size);
731 size /= __le32_to_cpu(bsb->chunksize) >> 9;
732 size = (size + 7) >> 3;
733 size += sizeof(bitmap_super_t);
734 size = ROUND_UP(size, 4096);
735 size /= 512;
736 size += bmend;
737 if (size < info->space_before)
738 info->space_before -= size;
739 else
740 info->space_before = 0;
741 } else
742 info->space_before -= 8; /* superblock */
743 info->space_after = misc->device_size - data_size - info->data_offset;
744 }
745
746 info->disk.raid_disk = -1;
747 switch(role) {
748 case 0xFFFF:
749 info->disk.state = 0; /* spare: not active, not sync, not faulty */
750 break;
751 case 0xFFFE:
752 info->disk.state = 1; /* faulty */
753 break;
754 default:
755 info->disk.state = 6; /* active and in sync */
756 info->disk.raid_disk = role;
757 }
758 if (sb->devflags & WriteMostly1)
759 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
760 info->events = __le64_to_cpu(sb->events);
761 sprintf(info->text_version, "1.%d", st->minor_version);
762 info->safe_mode_delay = 200;
763
764 memcpy(info->uuid, sb->set_uuid, 16);
765
766 strncpy(info->name, sb->set_name, 32);
767 info->name[32] = 0;
768
769 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
770 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
771 else
772 info->recovery_start = MaxSector;
773
774 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
775 info->reshape_active = 1;
776 if (info->array.level == 10)
777 info->reshape_active |= RESHAPE_NO_BACKUP;
778 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
779 info->new_level = __le32_to_cpu(sb->new_level);
780 info->delta_disks = __le32_to_cpu(sb->delta_disks);
781 info->new_layout = __le32_to_cpu(sb->new_layout);
782 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
783 if (info->delta_disks < 0)
784 info->array.raid_disks -= info->delta_disks;
785 } else
786 info->reshape_active = 0;
787
788 info->recovery_blocked = info->reshape_active;
789
790 if (map)
791 for (i=0; i<map_disks; i++)
792 map[i] = 0;
793 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
794 role = __le16_to_cpu(sb->dev_roles[i]);
795 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
796 working++;
797 if (map && role < map_disks)
798 map[role] = 1;
799 }
800 }
801
802 info->array.working_disks = working;
803 }
804
805 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
806 {
807 struct mdinfo *info;
808
809 if (subarray)
810 return NULL;
811
812 info = xmalloc(sizeof(*info));
813 getinfo_super1(st, info, NULL);
814 return info;
815 }
816
817 static int update_super1(struct supertype *st, struct mdinfo *info,
818 char *update,
819 char *devname, int verbose,
820 int uuid_set, char *homehost)
821 {
822 /* NOTE: for 'assemble' and 'force' we need to return non-zero
823 * if any change was made. For others, the return value is
824 * ignored.
825 */
826 int rv = 0;
827 struct mdp_superblock_1 *sb = st->sb;
828
829 if (strcmp(update, "force-one")==0) {
830 /* Not enough devices for a working array,
831 * so bring this one up-to-date
832 */
833 if (sb->events != __cpu_to_le64(info->events))
834 rv = 1;
835 sb->events = __cpu_to_le64(info->events);
836 } else if (strcmp(update, "force-array")==0) {
837 /* Degraded array and 'force' requests to
838 * maybe need to mark it 'clean'.
839 */
840 switch(__le32_to_cpu(sb->level)) {
841 case 5: case 4: case 6:
842 /* need to force clean */
843 if (sb->resync_offset != MaxSector)
844 rv = 1;
845 sb->resync_offset = MaxSector;
846 }
847 } else if (strcmp(update, "assemble")==0) {
848 int d = info->disk.number;
849 int want;
850 if (info->disk.state == 6)
851 want = info->disk.raid_disk;
852 else
853 want = 0xFFFF;
854 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
855 sb->dev_roles[d] = __cpu_to_le16(want);
856 rv = 1;
857 }
858 if (info->reshape_active &&
859 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
860 info->delta_disks >= 0 &&
861 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
862 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
863 rv = 1;
864 }
865 if (info->reshape_active &&
866 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
867 info->delta_disks < 0 &&
868 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
869 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
870 rv = 1;
871 }
872 } else if (strcmp(update, "linear-grow-new") == 0) {
873 unsigned int i;
874 int rfd, fd;
875 unsigned int max = __le32_to_cpu(sb->max_dev);
876
877 for (i=0 ; i < max ; i++)
878 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
879 break;
880 sb->dev_number = __cpu_to_le32(i);
881 info->disk.number = i;
882 if (max >= __le32_to_cpu(sb->max_dev))
883 sb->max_dev = __cpu_to_le32(max+1);
884
885 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
886 read(rfd, sb->device_uuid, 16) != 16) {
887 __u32 r[4] = {random(), random(), random(), random()};
888 memcpy(sb->device_uuid, r, 16);
889 }
890 if (rfd >= 0)
891 close(rfd);
892
893 sb->dev_roles[i] =
894 __cpu_to_le16(info->disk.raid_disk);
895
896 fd = open(devname, O_RDONLY);
897 if (fd >= 0) {
898 unsigned long long ds;
899 get_dev_size(fd, devname, &ds);
900 close(fd);
901 ds >>= 9;
902 if (__le64_to_cpu(sb->super_offset) <
903 __le64_to_cpu(sb->data_offset)) {
904 sb->data_size = __cpu_to_le64(
905 ds - __le64_to_cpu(sb->data_offset));
906 } else {
907 ds -= 8*2;
908 ds &= ~(unsigned long long)(4*2-1);
909 sb->super_offset = __cpu_to_le64(ds);
910 sb->data_size = __cpu_to_le64(
911 ds - __le64_to_cpu(sb->data_offset));
912 }
913 }
914 } else if (strcmp(update, "linear-grow-update") == 0) {
915 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
916 sb->dev_roles[info->disk.number] =
917 __cpu_to_le16(info->disk.raid_disk);
918 } else if (strcmp(update, "resync") == 0) {
919 /* make sure resync happens */
920 sb->resync_offset = 0ULL;
921 } else if (strcmp(update, "uuid") == 0) {
922 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
923
924 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
925 struct bitmap_super_s *bm;
926 bm = (struct bitmap_super_s*)(st->sb+MAX_SB_SIZE);
927 memcpy(bm->uuid, sb->set_uuid, 16);
928 }
929 } else if (strcmp(update, "no-bitmap") == 0) {
930 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
931 } else if (strcmp(update, "bbl") == 0) {
932 /* only possible if there is room after the bitmap, or if
933 * there is no bitmap
934 */
935 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
936 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
937 long bitmap_offset = (long)__le64_to_cpu(sb->bitmap_offset);
938 long bm_sectors = 0;
939 long space;
940
941 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
942 struct bitmap_super_s *bsb;
943 bsb = (struct bitmap_super_s *)(((char*)sb)+MAX_SB_SIZE);
944 bm_sectors = bitmap_sectors(bsb);
945 }
946
947 if (sb_offset < data_offset) {
948 /* 1.1 or 1.2. Put bbl just before data
949 */
950 long bb_offset;
951 space = data_offset - sb_offset;
952 bb_offset = space - 8;
953 if (bm_sectors && bitmap_offset > 0)
954 space -= (bitmap_offset + bm_sectors);
955 else
956 space -= 8; /* The superblock */
957 if (space >= 8) {
958 sb->bblog_size = __cpu_to_le16(8);
959 sb->bblog_offset = __cpu_to_le32(bb_offset);
960 }
961 } else {
962 /* 1.0 - Put bbl just before super block */
963 if (bm_sectors && bitmap_offset < 0)
964 space = -bitmap_offset - bm_sectors;
965 else
966 space = sb_offset - data_offset -
967 __le64_to_cpu(sb->data_size);
968 if (space >= 8) {
969 sb->bblog_size = __cpu_to_le16(8);
970 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
971 }
972 }
973 } else if (strcmp(update, "no-bbl") == 0) {
974 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
975 pr_err("Cannot remove active bbl from %s\n",devname);
976 else {
977 sb->bblog_size = 0;
978 sb->bblog_shift = 0;
979 sb->bblog_offset = 0;
980 }
981 } else if (strcmp(update, "homehost") == 0 &&
982 homehost) {
983 char *c;
984 update = "name";
985 c = strchr(sb->set_name, ':');
986 if (c)
987 strncpy(info->name, c+1, 31 - (c-sb->set_name));
988 else
989 strncpy(info->name, sb->set_name, 32);
990 info->name[32] = 0;
991 } else if (strcmp(update, "name") == 0) {
992 if (info->name[0] == 0)
993 sprintf(info->name, "%d", info->array.md_minor);
994 memset(sb->set_name, 0, sizeof(sb->set_name));
995 if (homehost &&
996 strchr(info->name, ':') == NULL &&
997 strlen(homehost)+1+strlen(info->name) < 32) {
998 strcpy(sb->set_name, homehost);
999 strcat(sb->set_name, ":");
1000 strcat(sb->set_name, info->name);
1001 } else
1002 strcpy(sb->set_name, info->name);
1003 } else if (strcmp(update, "devicesize") == 0 &&
1004 __le64_to_cpu(sb->super_offset) <
1005 __le64_to_cpu(sb->data_offset)) {
1006 /* set data_size to device size less data_offset */
1007 struct misc_dev_info *misc = (struct misc_dev_info*)
1008 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
1009 printf("Size was %llu\n", (unsigned long long)
1010 __le64_to_cpu(sb->data_size));
1011 sb->data_size = __cpu_to_le64(
1012 misc->device_size - __le64_to_cpu(sb->data_offset));
1013 printf("Size is %llu\n", (unsigned long long)
1014 __le64_to_cpu(sb->data_size));
1015 } else if (strcmp(update, "_reshape_progress")==0)
1016 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1017 else if (strcmp(update, "writemostly")==0)
1018 sb->devflags |= WriteMostly1;
1019 else if (strcmp(update, "readwrite")==0)
1020 sb->devflags &= ~WriteMostly1;
1021 else
1022 rv = -1;
1023
1024 sb->sb_csum = calc_sb_1_csum(sb);
1025 return rv;
1026 }
1027
1028 static int init_super1(struct supertype *st, mdu_array_info_t *info,
1029 unsigned long long size, char *name, char *homehost,
1030 int *uuid, unsigned long long data_offset)
1031 {
1032 struct mdp_superblock_1 *sb;
1033 int spares;
1034 int rfd;
1035 char defname[10];
1036 int sbsize;
1037
1038 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1039 pr_err("%s could not allocate superblock\n", __func__);
1040 return 0;
1041 }
1042 memset(sb, 0, SUPER1_SIZE);
1043
1044 st->sb = sb;
1045 if (info == NULL) {
1046 /* zeroing superblock */
1047 return 0;
1048 }
1049
1050 spares = info->working_disks - info->active_disks;
1051 if (info->raid_disks + spares > MAX_DEVS) {
1052 pr_err("too many devices requested: %d+%d > %d\n",
1053 info->raid_disks , spares, MAX_DEVS);
1054 return 0;
1055 }
1056
1057 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1058 sb->major_version = __cpu_to_le32(1);
1059 sb->feature_map = 0;
1060 sb->pad0 = 0;
1061
1062 if (uuid)
1063 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
1064 else {
1065 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1066 read(rfd, sb->set_uuid, 16) != 16) {
1067 __u32 r[4] = {random(), random(), random(), random()};
1068 memcpy(sb->set_uuid, r, 16);
1069 }
1070 if (rfd >= 0) close(rfd);
1071 }
1072
1073 if (name == NULL || *name == 0) {
1074 sprintf(defname, "%d", info->md_minor);
1075 name = defname;
1076 }
1077 if (homehost &&
1078 strchr(name, ':')== NULL &&
1079 strlen(homehost)+1+strlen(name) < 32) {
1080 strcpy(sb->set_name, homehost);
1081 strcat(sb->set_name, ":");
1082 strcat(sb->set_name, name);
1083 } else
1084 strcpy(sb->set_name, name);
1085
1086 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1087 sb->level = __cpu_to_le32(info->level);
1088 sb->layout = __cpu_to_le32(info->layout);
1089 sb->size = __cpu_to_le64(size*2ULL);
1090 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1091 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1092
1093 sb->data_offset = __cpu_to_le64(data_offset);
1094 sb->data_size = __cpu_to_le64(0);
1095 sb->super_offset = __cpu_to_le64(0);
1096 sb->recovery_offset = __cpu_to_le64(0);
1097
1098 sb->utime = sb->ctime;
1099 sb->events = __cpu_to_le64(1);
1100 if (info->state & (1<<MD_SB_CLEAN))
1101 sb->resync_offset = MaxSector;
1102 else
1103 sb->resync_offset = 0;
1104 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1105 sbsize = ROUND_UP(sbsize, 512);
1106 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
1107
1108 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
1109
1110 return 1;
1111 }
1112
1113 struct devinfo {
1114 int fd;
1115 char *devname;
1116 long long data_offset;
1117 mdu_disk_info_t disk;
1118 struct devinfo *next;
1119 };
1120 #ifndef MDASSEMBLE
1121 /* Add a device to the superblock being created */
1122 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
1123 int fd, char *devname, unsigned long long data_offset)
1124 {
1125 struct mdp_superblock_1 *sb = st->sb;
1126 __u16 *rp = sb->dev_roles + dk->number;
1127 struct devinfo *di, **dip;
1128
1129 if ((dk->state & 6) == 6) /* active, sync */
1130 *rp = __cpu_to_le16(dk->raid_disk);
1131 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
1132 *rp = 0xffff;
1133 else
1134 *rp = 0xfffe;
1135
1136 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
1137 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
1138 sb->max_dev = __cpu_to_le32(dk->number+1);
1139
1140 sb->dev_number = __cpu_to_le32(dk->number);
1141 sb->devflags = 0; /* don't copy another disks flags */
1142 sb->sb_csum = calc_sb_1_csum(sb);
1143
1144 dip = (struct devinfo **)&st->info;
1145 while (*dip)
1146 dip = &(*dip)->next;
1147 di = xmalloc(sizeof(struct devinfo));
1148 di->fd = fd;
1149 di->devname = devname;
1150 di->disk = *dk;
1151 di->data_offset = data_offset;
1152 di->next = NULL;
1153 *dip = di;
1154
1155 return 0;
1156 }
1157 #endif
1158
1159 static void locate_bitmap1(struct supertype *st, int fd);
1160
1161 static int store_super1(struct supertype *st, int fd)
1162 {
1163 struct mdp_superblock_1 *sb = st->sb;
1164 unsigned long long sb_offset;
1165 struct align_fd afd;
1166 int sbsize;
1167 unsigned long long dsize;
1168
1169 if (!get_dev_size(fd, NULL, &dsize))
1170 return 1;
1171
1172 dsize >>= 9;
1173
1174 if (dsize < 24)
1175 return 2;
1176
1177 init_afd(&afd, fd);
1178
1179 /*
1180 * Calculate the position of the superblock.
1181 * It is always aligned to a 4K boundary and
1182 * depending on minor_version, it can be:
1183 * 0: At least 8K, but less than 12K, from end of device
1184 * 1: At start of device
1185 * 2: 4K from start of device.
1186 */
1187 switch(st->minor_version) {
1188 case 0:
1189 sb_offset = dsize;
1190 sb_offset -= 8*2;
1191 sb_offset &= ~(4*2-1);
1192 break;
1193 case 1:
1194 sb_offset = 0;
1195 break;
1196 case 2:
1197 sb_offset = 4*2;
1198 break;
1199 default:
1200 return -EINVAL;
1201 }
1202
1203
1204
1205 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1206 0 != __le64_to_cpu(sb->super_offset)
1207 ) {
1208 pr_err("internal error - sb_offset is wrong\n");
1209 abort();
1210 }
1211
1212 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1213 return 3;
1214
1215 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1216
1217 if (awrite(&afd, sb, sbsize) != sbsize)
1218 return 4;
1219
1220 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1221 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1222 (((char*)sb)+MAX_SB_SIZE);
1223 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1224 locate_bitmap1(st, fd);
1225 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1226 return 5;
1227 }
1228 }
1229 fsync(fd);
1230 return 0;
1231 }
1232
1233 static int load_super1(struct supertype *st, int fd, char *devname);
1234
1235 static unsigned long choose_bm_space(unsigned long devsize)
1236 {
1237 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1238 * if bigger than 200Gig, save 128k
1239 * NOTE: result must be multiple of 4K else bad things happen
1240 * on 4K-sector devices.
1241 */
1242 if (devsize < 64*2) return 0;
1243 if (devsize - 64*2 >= 200*1024*1024*2)
1244 return 128*2;
1245 if (devsize - 4*2 > 8*1024*1024*2)
1246 return 64*2;
1247 return 4*2;
1248 }
1249
1250 static void free_super1(struct supertype *st);
1251
1252 #ifndef MDASSEMBLE
1253 static int write_init_super1(struct supertype *st)
1254 {
1255 struct mdp_superblock_1 *sb = st->sb;
1256 struct supertype *refst;
1257 int rfd;
1258 int rv = 0;
1259 unsigned long long bm_space;
1260 unsigned long long reserved;
1261 struct devinfo *di;
1262 unsigned long long dsize, array_size;
1263 unsigned long long sb_offset, headroom;
1264 unsigned long long data_offset;
1265
1266 for (di = st->info; di; di = di->next) {
1267 if (di->disk.state == 1)
1268 continue;
1269 if (di->fd < 0)
1270 continue;
1271
1272 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1273 ;
1274
1275 sb->dev_number = __cpu_to_le32(di->disk.number);
1276 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1277 sb->devflags |= WriteMostly1;
1278 else
1279 sb->devflags &= ~WriteMostly1;
1280
1281 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1282 read(rfd, sb->device_uuid, 16) != 16) {
1283 __u32 r[4] = {random(), random(), random(), random()};
1284 memcpy(sb->device_uuid, r, 16);
1285 }
1286 if (rfd >= 0)
1287 close(rfd);
1288
1289 sb->events = 0;
1290
1291 refst = dup_super(st);
1292 if (load_super1(refst, di->fd, NULL)==0) {
1293 struct mdp_superblock_1 *refsb = refst->sb;
1294
1295 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1296 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1297 /* same array, so preserve events and
1298 * dev_number */
1299 sb->events = refsb->events;
1300 /* bugs in 2.6.17 and earlier mean the
1301 * dev_number chosen in Manage must be preserved
1302 */
1303 if (get_linux_version() >= 2006018)
1304 sb->dev_number = refsb->dev_number;
1305 }
1306 free_super1(refst);
1307 }
1308 free(refst);
1309
1310 if (!get_dev_size(di->fd, NULL, &dsize)) {
1311 rv = 1;
1312 goto error_out;
1313 }
1314 dsize >>= 9;
1315
1316 if (dsize < 24) {
1317 close(di->fd);
1318 rv = 2;
1319 goto error_out;
1320 }
1321
1322
1323 /*
1324 * Calculate the position of the superblock.
1325 * It is always aligned to a 4K boundary and
1326 * depending on minor_version, it can be:
1327 * 0: At least 8K, but less than 12K, from end of device
1328 * 1: At start of device
1329 * 2: 4K from start of device.
1330 * Depending on the array size, we might leave extra space
1331 * for a bitmap.
1332 * Also leave 4K for bad-block log.
1333 */
1334 array_size = __le64_to_cpu(sb->size);
1335 /* work out how much space we left for a bitmap,
1336 * Add 8 sectors for bad block log */
1337 bm_space = choose_bm_space(array_size) + 8;
1338
1339 /* We try to leave 0.1% at the start for reshape
1340 * operations, but limit this to 128Meg (0.1% of 10Gig)
1341 * which is plenty for efficient reshapes
1342 * However we make it at least 2 chunks as one chunk
1343 * is minimum needed for reshape.
1344 */
1345 headroom = 128 * 1024 * 2;
1346 while (headroom << 10 > array_size &&
1347 headroom/2 >= __le32_to_cpu(sb->chunksize) * 2)
1348 headroom >>= 1;
1349
1350 data_offset = di->data_offset;
1351 switch(st->minor_version) {
1352 case 0:
1353 sb_offset = dsize;
1354 sb_offset -= 8*2;
1355 sb_offset &= ~(4*2-1);
1356 sb->super_offset = __cpu_to_le64(sb_offset);
1357 if (data_offset == INVALID_SECTORS)
1358 sb->data_offset = 0;
1359 if (sb_offset < array_size + bm_space)
1360 bm_space = sb_offset - array_size;
1361 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1362 if (bm_space >= 8) {
1363 sb->bblog_size = __cpu_to_le16(8);
1364 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1365 }
1366 break;
1367 case 1:
1368 sb->super_offset = __cpu_to_le64(0);
1369 if (data_offset == INVALID_SECTORS) {
1370 reserved = bm_space + 4*2;
1371 if (reserved < headroom)
1372 reserved = headroom;
1373 if (reserved + array_size > dsize)
1374 reserved = dsize - array_size;
1375 /* Try for multiple of 1Meg so it is nicely aligned */
1376 #define ONE_MEG (2*1024)
1377 if (reserved > ONE_MEG)
1378 reserved = (reserved/ONE_MEG) * ONE_MEG;
1379
1380 /* force 4K alignment */
1381 reserved &= ~7ULL;
1382
1383 } else
1384 reserved = data_offset;
1385
1386 sb->data_offset = __cpu_to_le64(reserved);
1387 sb->data_size = __cpu_to_le64(dsize - reserved);
1388 if (reserved >= 16) {
1389 sb->bblog_size = __cpu_to_le16(8);
1390 sb->bblog_offset = __cpu_to_le32(reserved-8);
1391 }
1392 break;
1393 case 2:
1394 sb_offset = 4*2;
1395 sb->super_offset = __cpu_to_le64(4*2);
1396 if (data_offset == INVALID_SECTORS) {
1397 if (4*2 + 4*2 + bm_space + array_size
1398 > dsize)
1399 bm_space = dsize - array_size
1400 - 4*2 - 4*2;
1401
1402 reserved = bm_space + 4*2 + 4*2;
1403 if (reserved < headroom)
1404 reserved = headroom;
1405 if (reserved + array_size > dsize)
1406 reserved = dsize - array_size;
1407 /* Try for multiple of 1Meg so it is nicely aligned */
1408 #define ONE_MEG (2*1024)
1409 if (reserved > ONE_MEG)
1410 reserved = (reserved/ONE_MEG) * ONE_MEG;
1411
1412 /* force 4K alignment */
1413 reserved &= ~7ULL;
1414
1415 } else
1416 reserved = data_offset;
1417
1418 sb->data_offset = __cpu_to_le64(reserved);
1419 sb->data_size = __cpu_to_le64(dsize - reserved);
1420 if (reserved >= 16+16) {
1421 sb->bblog_size = __cpu_to_le16(8);
1422 /* '8' sectors for the bblog, and another '8'
1423 * because we want offset from superblock, not
1424 * start of device.
1425 */
1426 sb->bblog_offset = __cpu_to_le32(reserved-8-8);
1427 }
1428 break;
1429 default:
1430 pr_err("Failed to write invalid "
1431 "metadata format 1.%i to %s\n",
1432 st->minor_version, di->devname);
1433 rv = -EINVAL;
1434 goto out;
1435 }
1436
1437 sb->sb_csum = calc_sb_1_csum(sb);
1438 rv = store_super1(st, di->fd);
1439 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1440 rv = st->ss->write_bitmap(st, di->fd);
1441 close(di->fd);
1442 di->fd = -1;
1443 if (rv)
1444 goto error_out;
1445 }
1446 error_out:
1447 if (rv)
1448 pr_err("Failed to write metadata to %s\n",
1449 di->devname);
1450 out:
1451 return rv;
1452 }
1453 #endif
1454
1455 static int compare_super1(struct supertype *st, struct supertype *tst)
1456 {
1457 /*
1458 * return:
1459 * 0 same, or first was empty, and second was copied
1460 * 1 second had wrong number
1461 * 2 wrong uuid
1462 * 3 wrong other info
1463 */
1464 struct mdp_superblock_1 *first = st->sb;
1465 struct mdp_superblock_1 *second = tst->sb;
1466
1467 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1468 return 1;
1469 if (second->major_version != __cpu_to_le32(1))
1470 return 1;
1471
1472 if (!first) {
1473 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1474 pr_err("%s could not allocate superblock\n", __func__);
1475 return 1;
1476 }
1477 memcpy(first, second, SUPER1_SIZE);
1478 st->sb = first;
1479 return 0;
1480 }
1481 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1482 return 2;
1483
1484 if (first->ctime != second->ctime ||
1485 first->level != second->level ||
1486 first->layout != second->layout ||
1487 first->size != second->size ||
1488 first->chunksize != second->chunksize ||
1489 first->raid_disks != second->raid_disks)
1490 return 3;
1491 return 0;
1492 }
1493
1494 static int load_super1(struct supertype *st, int fd, char *devname)
1495 {
1496 unsigned long long dsize;
1497 unsigned long long sb_offset;
1498 struct mdp_superblock_1 *super;
1499 int uuid[4];
1500 struct bitmap_super_s *bsb;
1501 struct misc_dev_info *misc;
1502 struct align_fd afd;
1503
1504 free_super1(st);
1505
1506 init_afd(&afd, fd);
1507
1508 if (st->ss == NULL || st->minor_version == -1) {
1509 int bestvers = -1;
1510 struct supertype tst;
1511 __u64 bestctime = 0;
1512 /* guess... choose latest ctime */
1513 memset(&tst, 0, sizeof(tst));
1514 tst.ss = &super1;
1515 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1516 switch(load_super1(&tst, fd, devname)) {
1517 case 0: super = tst.sb;
1518 if (bestvers == -1 ||
1519 bestctime < __le64_to_cpu(super->ctime)) {
1520 bestvers = tst.minor_version;
1521 bestctime = __le64_to_cpu(super->ctime);
1522 }
1523 free(super);
1524 tst.sb = NULL;
1525 break;
1526 case 1: return 1; /*bad device */
1527 case 2: break; /* bad, try next */
1528 }
1529 }
1530 if (bestvers != -1) {
1531 int rv;
1532 tst.minor_version = bestvers;
1533 tst.ss = &super1;
1534 tst.max_devs = MAX_DEVS;
1535 rv = load_super1(&tst, fd, devname);
1536 if (rv == 0)
1537 *st = tst;
1538 return rv;
1539 }
1540 return 2;
1541 }
1542 if (!get_dev_size(fd, devname, &dsize))
1543 return 1;
1544 dsize >>= 9;
1545
1546 if (dsize < 24) {
1547 if (devname)
1548 pr_err("%s is too small for md: size is %llu sectors.\n",
1549 devname, dsize);
1550 return 1;
1551 }
1552
1553 /*
1554 * Calculate the position of the superblock.
1555 * It is always aligned to a 4K boundary and
1556 * depending on minor_version, it can be:
1557 * 0: At least 8K, but less than 12K, from end of device
1558 * 1: At start of device
1559 * 2: 4K from start of device.
1560 */
1561 switch(st->minor_version) {
1562 case 0:
1563 sb_offset = dsize;
1564 sb_offset -= 8*2;
1565 sb_offset &= ~(4*2-1);
1566 break;
1567 case 1:
1568 sb_offset = 0;
1569 break;
1570 case 2:
1571 sb_offset = 4*2;
1572 break;
1573 default:
1574 return -EINVAL;
1575 }
1576
1577 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1578
1579
1580 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1581 if (devname)
1582 pr_err("Cannot seek to superblock on %s: %s\n",
1583 devname, strerror(errno));
1584 return 1;
1585 }
1586
1587 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1588 pr_err("%s could not allocate superblock\n",
1589 __func__);
1590 return 1;
1591 }
1592
1593 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
1594 if (devname)
1595 pr_err("Cannot read superblock on %s\n",
1596 devname);
1597 free(super);
1598 return 1;
1599 }
1600
1601 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1602 if (devname)
1603 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1604 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1605 free(super);
1606 return 2;
1607 }
1608
1609 if (__le32_to_cpu(super->major_version) != 1) {
1610 if (devname)
1611 pr_err("Cannot interpret superblock on %s - version is %d\n",
1612 devname, __le32_to_cpu(super->major_version));
1613 free(super);
1614 return 2;
1615 }
1616 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1617 if (devname)
1618 pr_err("No superblock found on %s (super_offset is wrong)\n",
1619 devname);
1620 free(super);
1621 return 2;
1622 }
1623 st->sb = super;
1624
1625 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1626
1627 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
1628 misc->device_size = dsize;
1629
1630 /* Now check on the bitmap superblock */
1631 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1632 return 0;
1633 /* Read the bitmap superblock and make sure it looks
1634 * valid. If it doesn't clear the bit. An --assemble --force
1635 * should get that written out.
1636 */
1637 locate_bitmap1(st, fd);
1638 if (aread(&afd, bsb, 512) != 512)
1639 goto no_bitmap;
1640
1641 uuid_from_super1(st, uuid);
1642 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1643 memcmp(bsb->uuid, uuid, 16) != 0)
1644 goto no_bitmap;
1645 return 0;
1646
1647 no_bitmap:
1648 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
1649 & ~MD_FEATURE_BITMAP_OFFSET);
1650 return 0;
1651 }
1652
1653
1654 static struct supertype *match_metadata_desc1(char *arg)
1655 {
1656 struct supertype *st = xcalloc(1, sizeof(*st));
1657
1658 st->container_dev = NoMdDev;
1659 st->ss = &super1;
1660 st->max_devs = MAX_DEVS;
1661 st->sb = NULL;
1662 /* leading zeros can be safely ignored. --detail generates them. */
1663 while (*arg == '0')
1664 arg++;
1665 if (strcmp(arg, "1.0") == 0 ||
1666 strcmp(arg, "1.00") == 0) {
1667 st->minor_version = 0;
1668 return st;
1669 }
1670 if (strcmp(arg, "1.1") == 0 ||
1671 strcmp(arg, "1.01") == 0
1672 ) {
1673 st->minor_version = 1;
1674 return st;
1675 }
1676 if (strcmp(arg, "1.2") == 0 ||
1677 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1678 strcmp(arg, "default") == 0 ||
1679 #endif /* DEFAULT_OLD_METADATA */
1680 strcmp(arg, "1.02") == 0) {
1681 st->minor_version = 2;
1682 return st;
1683 }
1684 if (strcmp(arg, "1") == 0 ||
1685 strcmp(arg, "default") == 0) {
1686 st->minor_version = -1;
1687 return st;
1688 }
1689
1690 free(st);
1691 return NULL;
1692 }
1693
1694 /* find available size on device with this devsize, using
1695 * superblock type st, and reserving 'reserve' sectors for
1696 * a possible bitmap
1697 */
1698 static __u64 _avail_size1(struct supertype *st, __u64 devsize,
1699 unsigned long long data_offset, int chunksize)
1700 {
1701 struct mdp_superblock_1 *super = st->sb;
1702 int bmspace = 0;
1703 if (devsize < 24)
1704 return 0;
1705
1706 if (super == NULL)
1707 /* creating: allow suitable space for bitmap */
1708 bmspace = choose_bm_space(devsize);
1709 #ifndef MDASSEMBLE
1710 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1711 /* hot-add. allow for actual size of bitmap */
1712 struct bitmap_super_s *bsb;
1713 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1714 bmspace = bitmap_sectors(bsb);
1715 }
1716 #endif
1717 /* Allow space for bad block log */
1718 if (super && super->bblog_size)
1719 devsize -= __le16_to_cpu(super->bblog_size);
1720 else
1721 devsize -= 8;
1722
1723
1724 if (st->minor_version < 0)
1725 /* not specified, so time to set default */
1726 st->minor_version = 2;
1727
1728 if (data_offset != INVALID_SECTORS)
1729 switch(st->minor_version) {
1730 case 0:
1731 return devsize - data_offset - 8*2;
1732 case 1:
1733 case 2:
1734 return devsize - data_offset;
1735 default:
1736 return 0;
1737 }
1738
1739 devsize -= bmspace;
1740
1741 if (super == NULL && st->minor_version > 0) {
1742 /* haven't committed to a size yet, so allow some
1743 * slack for space for reshape.
1744 * Limit slack to 128M, but aim for about 0.1%
1745 */
1746 unsigned long long headroom = 128*1024*2;
1747 while ((headroom << 10) > devsize &&
1748 (chunksize == 0 ||
1749 headroom / 2 >= ((unsigned)chunksize*2)*2))
1750 headroom >>= 1;
1751 devsize -= headroom;
1752 }
1753 switch(st->minor_version) {
1754 case 0:
1755 /* at end */
1756 return ((devsize - 8*2 ) & ~(4*2-1));
1757 case 1:
1758 /* at start, 4K for superblock and possible bitmap */
1759 return devsize - 4*2;
1760 case 2:
1761 /* 4k from start, 4K for superblock and possible bitmap */
1762 return devsize - (4+4)*2;
1763 }
1764 return 0;
1765 }
1766 static __u64 avail_size1(struct supertype *st, __u64 devsize,
1767 unsigned long long data_offset)
1768 {
1769 return _avail_size1(st, devsize, data_offset, 0);
1770 }
1771
1772 static int
1773 add_internal_bitmap1(struct supertype *st,
1774 int *chunkp, int delay, int write_behind,
1775 unsigned long long size,
1776 int may_change, int major)
1777 {
1778 /*
1779 * If not may_change, then this is a 'Grow' without sysfs support for
1780 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1781 * If may_change, then this is create or a Grow with sysfs syupport,
1782 * and we can put the bitmap wherever we like.
1783 *
1784 * size is in sectors, chunk is in bytes !!!
1785 */
1786
1787 unsigned long long bits;
1788 unsigned long long max_bits;
1789 unsigned long long min_chunk;
1790 long offset;
1791 long bbl_offset, bbl_size;
1792 unsigned long long chunk = *chunkp;
1793 int room = 0;
1794 int creating = 0;
1795 struct mdp_superblock_1 *sb = st->sb;
1796 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1797 int uuid[4];
1798
1799
1800 if (__le64_to_cpu(sb->data_size) == 0)
1801 /* Must be creating the array, else data_size would be non-zero */
1802 creating = 1;
1803 switch(st->minor_version) {
1804 case 0:
1805 /* either 3K after the superblock (when hot-add),
1806 * or some amount of space before.
1807 */
1808 if (creating) {
1809 /* We are creating array, so we *know* how much room has
1810 * been left.
1811 */
1812 offset = 0;
1813 room = choose_bm_space(__le64_to_cpu(sb->size));
1814 bbl_size = 8;
1815 } else {
1816 room = __le64_to_cpu(sb->super_offset)
1817 - __le64_to_cpu(sb->data_offset)
1818 - __le64_to_cpu(sb->data_size);
1819 bbl_size = __le16_to_cpu(sb->bblog_size);
1820 if (bbl_size < 8)
1821 bbl_size = 8;
1822 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
1823 if (bbl_size < -bbl_offset)
1824 bbl_size = -bbl_offset;
1825
1826 if (!may_change || (room < 3*2 &&
1827 __le32_to_cpu(sb->max_dev) <= 384)) {
1828 room = 3*2;
1829 offset = 1*2;
1830 bbl_size = 0;
1831 } else {
1832 offset = 0; /* means movable offset */
1833 }
1834 }
1835 break;
1836 case 1:
1837 case 2: /* between superblock and data */
1838 if (creating) {
1839 offset = 4*2;
1840 room = choose_bm_space(__le64_to_cpu(sb->size));
1841 bbl_size = 8;
1842 } else {
1843 room = __le64_to_cpu(sb->data_offset)
1844 - __le64_to_cpu(sb->super_offset);
1845 bbl_size = __le16_to_cpu(sb->bblog_size);
1846 if (bbl_size)
1847 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
1848 else
1849 bbl_size = 8;
1850
1851 if (!may_change) {
1852 room -= 2; /* Leave 1K for superblock */
1853 offset = 2;
1854 bbl_size = 0;
1855 } else {
1856 room -= 4*2; /* leave 4K for superblock */
1857 offset = 4*2;
1858 }
1859 }
1860 break;
1861 default:
1862 return 0;
1863 }
1864
1865 room -= bbl_size;
1866 if (chunk == UnSet && room > 128*2)
1867 /* Limit to 128K of bitmap when chunk size not requested */
1868 room = 128*2;
1869
1870 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1871
1872 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1873 bits = (size*512)/min_chunk +1;
1874 while (bits > max_bits) {
1875 min_chunk *= 2;
1876 bits = (bits+1)/2;
1877 }
1878 if (chunk == UnSet) {
1879 /* For practical purpose, 64Meg is a good
1880 * default chunk size for internal bitmaps.
1881 */
1882 chunk = min_chunk;
1883 if (chunk < 64*1024*1024)
1884 chunk = 64*1024*1024;
1885 } else if (chunk < min_chunk)
1886 return 0; /* chunk size too small */
1887 if (chunk == 0) /* rounding problem */
1888 return 0;
1889
1890 if (offset == 0) {
1891 /* start bitmap on a 4K boundary with enough space for
1892 * the bitmap
1893 */
1894 bits = (size*512) / chunk + 1;
1895 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1896 room *= 8; /* convert 4K blocks to sectors */
1897 offset = -room - bbl_size;
1898 }
1899
1900 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
1901
1902 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
1903 | MD_FEATURE_BITMAP_OFFSET);
1904 memset(bms, 0, sizeof(*bms));
1905 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1906 bms->version = __cpu_to_le32(major);
1907 uuid_from_super1(st, uuid);
1908 memcpy(bms->uuid, uuid, 16);
1909 bms->chunksize = __cpu_to_le32(chunk);
1910 bms->daemon_sleep = __cpu_to_le32(delay);
1911 bms->sync_size = __cpu_to_le64(size);
1912 bms->write_behind = __cpu_to_le32(write_behind);
1913
1914 *chunkp = chunk;
1915 return 1;
1916 }
1917
1918 static void locate_bitmap1(struct supertype *st, int fd)
1919 {
1920 unsigned long long offset;
1921 struct mdp_superblock_1 *sb;
1922 int mustfree = 0;
1923
1924 if (!st->sb) {
1925 if (st->ss->load_super(st, fd, NULL))
1926 return; /* no error I hope... */
1927 mustfree = 1;
1928 }
1929 sb = st->sb;
1930
1931 offset = __le64_to_cpu(sb->super_offset);
1932 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1933 if (mustfree)
1934 free(sb);
1935 lseek64(fd, offset<<9, 0);
1936 }
1937
1938 static int write_bitmap1(struct supertype *st, int fd)
1939 {
1940 struct mdp_superblock_1 *sb = st->sb;
1941 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
1942 int rv = 0;
1943 void *buf;
1944 int towrite, n;
1945 struct align_fd afd;
1946
1947 init_afd(&afd, fd);
1948
1949 locate_bitmap1(st, fd);
1950
1951 if (posix_memalign(&buf, 4096, 4096))
1952 return -ENOMEM;
1953
1954 memset(buf, 0xff, 4096);
1955 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
1956
1957 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1958 towrite = (towrite+7) >> 3; /* bits to bytes */
1959 towrite += sizeof(bitmap_super_t);
1960 towrite = ROUND_UP(towrite, 512);
1961 while (towrite > 0) {
1962 n = towrite;
1963 if (n > 4096)
1964 n = 4096;
1965 n = awrite(&afd, buf, n);
1966 if (n > 0)
1967 towrite -= n;
1968 else
1969 break;
1970 memset(buf, 0xff, 4096);
1971 }
1972 fsync(fd);
1973 if (towrite)
1974 rv = -2;
1975
1976 free(buf);
1977 return rv;
1978 }
1979
1980 static void free_super1(struct supertype *st)
1981 {
1982 if (st->sb)
1983 free(st->sb);
1984 while (st->info) {
1985 struct devinfo *di = st->info;
1986 st->info = di->next;
1987 if (di->fd >= 0)
1988 close(di->fd);
1989 free(di);
1990 }
1991 st->sb = NULL;
1992 }
1993
1994 #ifndef MDASSEMBLE
1995 static int validate_geometry1(struct supertype *st, int level,
1996 int layout, int raiddisks,
1997 int *chunk, unsigned long long size,
1998 unsigned long long data_offset,
1999 char *subdev, unsigned long long *freesize,
2000 int verbose)
2001 {
2002 unsigned long long ldsize;
2003 int fd;
2004
2005 if (level == LEVEL_CONTAINER) {
2006 if (verbose)
2007 pr_err("1.x metadata does not support containers\n");
2008 return 0;
2009 }
2010 if (chunk && *chunk == UnSet)
2011 *chunk = DEFAULT_CHUNK;
2012
2013 if (!subdev)
2014 return 1;
2015
2016 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2017 if (fd < 0) {
2018 if (verbose)
2019 pr_err("super1.x cannot open %s: %s\n",
2020 subdev, strerror(errno));
2021 return 0;
2022 }
2023
2024 if (!get_dev_size(fd, subdev, &ldsize)) {
2025 close(fd);
2026 return 0;
2027 }
2028 close(fd);
2029
2030 *freesize = _avail_size1(st, ldsize >> 9, data_offset, *chunk);
2031 return 1;
2032 }
2033 #endif /* MDASSEMBLE */
2034
2035 struct superswitch super1 = {
2036 #ifndef MDASSEMBLE
2037 .examine_super = examine_super1,
2038 .brief_examine_super = brief_examine_super1,
2039 .export_examine_super = export_examine_super1,
2040 .detail_super = detail_super1,
2041 .brief_detail_super = brief_detail_super1,
2042 .export_detail_super = export_detail_super1,
2043 .write_init_super = write_init_super1,
2044 .validate_geometry = validate_geometry1,
2045 .add_to_super = add_to_super1,
2046 #endif
2047 .match_home = match_home1,
2048 .uuid_from_super = uuid_from_super1,
2049 .getinfo_super = getinfo_super1,
2050 .container_content = container_content1,
2051 .update_super = update_super1,
2052 .init_super = init_super1,
2053 .store_super = store_super1,
2054 .compare_super = compare_super1,
2055 .load_super = load_super1,
2056 .match_metadata_desc = match_metadata_desc1,
2057 .avail_size = avail_size1,
2058 .add_internal_bitmap = add_internal_bitmap1,
2059 .locate_bitmap = locate_bitmap1,
2060 .write_bitmap = write_bitmap1,
2061 .free_super = free_super1,
2062 #if __BYTE_ORDER == BIG_ENDIAN
2063 .swapuuid = 0,
2064 #else
2065 .swapuuid = 1,
2066 #endif
2067 .name = "1.x",
2068 };