]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
3236a7e28b301f3ef481b50c6180b8473734133e
[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 static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
652 {
653 struct mdp_superblock_1 *sb = st->sb;
654 unsigned long long offset;
655 int size;
656 __u64 *bbl, *bbp;
657 int i;
658
659 if (!sb->bblog_size || __le32_to_cpu(sb->bblog_size) > 100
660 || !sb->bblog_offset){
661 printf("No bad-blocks list configured on %s\n", devname);
662 return 0;
663 }
664 if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
665 == 0) {
666 printf("Bad-blocks list is empty in %s\n", devname);
667 return 0;
668 }
669
670 size = __le32_to_cpu(sb->bblog_size)* 512;
671 posix_memalign((void**)&bbl, 4096, size);
672 offset = __le64_to_cpu(sb->super_offset) +
673 (int)__le32_to_cpu(sb->bblog_offset);
674 offset <<= 9;
675 if (lseek64(fd, offset, 0) < 0) {
676 pr_err("Cannot seek to bad-blocks list\n");
677 return 1;
678 }
679 if (read(fd, bbl, size) != size) {
680 pr_err("Cannot read bad-blocks list\n");
681 return 1;
682 }
683 /* 64bits per entry. 10 bits is block-count, 54 bits is block
684 * offset. Blocks are sectors unless bblog->shift makes them bigger
685 */
686 bbp = (__u64*)bbl;
687 printf("Bad-blocks on %s:\n", devname);
688 for (i = 0; i < size/8; i++, bbp++) {
689 __u64 bb = __le64_to_cpu(*bbp);
690 int count = bb & 0x3ff;
691 unsigned long long sector = bb >> 10;
692
693 if (bb + 1 == 0)
694 break;
695
696 sector <<= sb->bblog_shift;
697 count <<= sb->bblog_shift;
698
699 printf("%20llu for %d sectors\n", sector, count);
700 }
701 return 0;
702 }
703
704 #endif
705
706 static int match_home1(struct supertype *st, char *homehost)
707 {
708 struct mdp_superblock_1 *sb = st->sb;
709 int l = homehost ? strlen(homehost) : 0;
710
711 return (l > 0 && l < 32 &&
712 sb->set_name[l] == ':' &&
713 strncmp(sb->set_name, homehost, l) == 0);
714 }
715
716 static void uuid_from_super1(struct supertype *st, int uuid[4])
717 {
718 struct mdp_superblock_1 *super = st->sb;
719 char *cuuid = (char*)uuid;
720 int i;
721 for (i=0; i<16; i++)
722 cuuid[i] = super->set_uuid[i];
723 }
724
725 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
726 {
727 struct mdp_superblock_1 *sb = st->sb;
728 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
729 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
730 int working = 0;
731 unsigned int i;
732 unsigned int role;
733 unsigned int map_disks = info->array.raid_disks;
734 unsigned long long super_offset;
735 unsigned long long data_size;
736
737 memset(info, 0, sizeof(*info));
738 info->array.major_version = 1;
739 info->array.minor_version = st->minor_version;
740 info->array.patch_version = 0;
741 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
742 info->array.level = __le32_to_cpu(sb->level);
743 info->array.layout = __le32_to_cpu(sb->layout);
744 info->array.md_minor = -1;
745 info->array.ctime = __le64_to_cpu(sb->ctime);
746 info->array.utime = __le64_to_cpu(sb->utime);
747 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
748 info->array.state =
749 (__le64_to_cpu(sb->resync_offset) == MaxSector)
750 ? 1 : 0;
751
752 info->data_offset = __le64_to_cpu(sb->data_offset);
753 info->component_size = __le64_to_cpu(sb->size);
754 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET))
755 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
756
757 info->disk.major = 0;
758 info->disk.minor = 0;
759 info->disk.number = __le32_to_cpu(sb->dev_number);
760 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
761 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
762 role = 0xfffe;
763 else
764 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
765
766 super_offset = __le64_to_cpu(sb->super_offset);
767 data_size = __le64_to_cpu(sb->size);
768 if (info->data_offset < super_offset) {
769 unsigned long long end;
770 info->space_before = info->data_offset;
771 end = super_offset;
772 if (info->bitmap_offset < 0)
773 end += info->bitmap_offset;
774 if (info->data_offset + data_size < end)
775 info->space_after = end - data_size - info->data_offset;
776 else
777 info->space_after = 0;
778 } else {
779 info->space_before = (info->data_offset -
780 super_offset);
781 if (info->bitmap_offset > 0) {
782 unsigned long long bmend = info->bitmap_offset;
783 unsigned long long size = __le64_to_cpu(bsb->sync_size);
784 size /= __le32_to_cpu(bsb->chunksize) >> 9;
785 size = (size + 7) >> 3;
786 size += sizeof(bitmap_super_t);
787 size = ROUND_UP(size, 4096);
788 size /= 512;
789 size += bmend;
790 if (size < info->space_before)
791 info->space_before -= size;
792 else
793 info->space_before = 0;
794 } else
795 info->space_before -= 8; /* superblock */
796 info->space_after = misc->device_size - data_size - info->data_offset;
797 }
798
799 info->disk.raid_disk = -1;
800 switch(role) {
801 case 0xFFFF:
802 info->disk.state = 0; /* spare: not active, not sync, not faulty */
803 break;
804 case 0xFFFE:
805 info->disk.state = 1; /* faulty */
806 break;
807 default:
808 info->disk.state = 6; /* active and in sync */
809 info->disk.raid_disk = role;
810 }
811 if (sb->devflags & WriteMostly1)
812 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
813 info->events = __le64_to_cpu(sb->events);
814 sprintf(info->text_version, "1.%d", st->minor_version);
815 info->safe_mode_delay = 200;
816
817 memcpy(info->uuid, sb->set_uuid, 16);
818
819 strncpy(info->name, sb->set_name, 32);
820 info->name[32] = 0;
821
822 if ((__le32_to_cpu(sb->feature_map)&MD_FEATURE_REPLACEMENT)) {
823 info->disk.state &= ~(1 << MD_DISK_SYNC);
824 info->disk.state |= 1 << MD_DISK_REPLACEMENT;
825 }
826
827
828 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
829 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
830 else
831 info->recovery_start = MaxSector;
832
833 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
834 info->reshape_active = 1;
835 if (info->array.level == 10)
836 info->reshape_active |= RESHAPE_NO_BACKUP;
837 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
838 info->new_level = __le32_to_cpu(sb->new_level);
839 info->delta_disks = __le32_to_cpu(sb->delta_disks);
840 info->new_layout = __le32_to_cpu(sb->new_layout);
841 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
842 if (info->delta_disks < 0)
843 info->array.raid_disks -= info->delta_disks;
844 } else
845 info->reshape_active = 0;
846
847 info->recovery_blocked = info->reshape_active;
848
849 if (map)
850 for (i=0; i<map_disks; i++)
851 map[i] = 0;
852 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
853 role = __le16_to_cpu(sb->dev_roles[i]);
854 if (/*role == 0xFFFF || */role < (unsigned) info->array.raid_disks) {
855 working++;
856 if (map && role < map_disks)
857 map[role] = 1;
858 }
859 }
860
861 info->array.working_disks = working;
862 }
863
864 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
865 {
866 struct mdinfo *info;
867
868 if (subarray)
869 return NULL;
870
871 info = xmalloc(sizeof(*info));
872 getinfo_super1(st, info, NULL);
873 return info;
874 }
875
876 static int update_super1(struct supertype *st, struct mdinfo *info,
877 char *update,
878 char *devname, int verbose,
879 int uuid_set, char *homehost)
880 {
881 /* NOTE: for 'assemble' and 'force' we need to return non-zero
882 * if any change was made. For others, the return value is
883 * ignored.
884 */
885 int rv = 0;
886 struct mdp_superblock_1 *sb = st->sb;
887
888 if (strcmp(update, "force-one")==0) {
889 /* Not enough devices for a working array,
890 * so bring this one up-to-date
891 */
892 if (sb->events != __cpu_to_le64(info->events))
893 rv = 1;
894 sb->events = __cpu_to_le64(info->events);
895 } else if (strcmp(update, "force-array")==0) {
896 /* Degraded array and 'force' requests to
897 * maybe need to mark it 'clean'.
898 */
899 switch(__le32_to_cpu(sb->level)) {
900 case 5: case 4: case 6:
901 /* need to force clean */
902 if (sb->resync_offset != MaxSector)
903 rv = 1;
904 sb->resync_offset = MaxSector;
905 }
906 } else if (strcmp(update, "assemble")==0) {
907 int d = info->disk.number;
908 int want;
909 if (info->disk.state & (1<<MD_DISK_ACTIVE))
910 want = info->disk.raid_disk;
911 else
912 want = 0xFFFF;
913 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
914 sb->dev_roles[d] = __cpu_to_le16(want);
915 rv = 1;
916 }
917 if (info->reshape_active &&
918 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
919 info->delta_disks >= 0 &&
920 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
921 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
922 rv = 1;
923 }
924 if (info->reshape_active &&
925 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
926 info->delta_disks < 0 &&
927 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
928 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
929 rv = 1;
930 }
931 } else if (strcmp(update, "linear-grow-new") == 0) {
932 unsigned int i;
933 int rfd, fd;
934 unsigned int max = __le32_to_cpu(sb->max_dev);
935
936 for (i=0 ; i < max ; i++)
937 if (__le16_to_cpu(sb->dev_roles[i]) >= 0xfffe)
938 break;
939 sb->dev_number = __cpu_to_le32(i);
940 info->disk.number = i;
941 if (max >= __le32_to_cpu(sb->max_dev))
942 sb->max_dev = __cpu_to_le32(max+1);
943
944 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
945 read(rfd, sb->device_uuid, 16) != 16) {
946 __u32 r[4] = {random(), random(), random(), random()};
947 memcpy(sb->device_uuid, r, 16);
948 }
949 if (rfd >= 0)
950 close(rfd);
951
952 sb->dev_roles[i] =
953 __cpu_to_le16(info->disk.raid_disk);
954
955 fd = open(devname, O_RDONLY);
956 if (fd >= 0) {
957 unsigned long long ds;
958 get_dev_size(fd, devname, &ds);
959 close(fd);
960 ds >>= 9;
961 if (__le64_to_cpu(sb->super_offset) <
962 __le64_to_cpu(sb->data_offset)) {
963 sb->data_size = __cpu_to_le64(
964 ds - __le64_to_cpu(sb->data_offset));
965 } else {
966 ds -= 8*2;
967 ds &= ~(unsigned long long)(4*2-1);
968 sb->super_offset = __cpu_to_le64(ds);
969 sb->data_size = __cpu_to_le64(
970 ds - __le64_to_cpu(sb->data_offset));
971 }
972 }
973 } else if (strcmp(update, "linear-grow-update") == 0) {
974 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
975 sb->dev_roles[info->disk.number] =
976 __cpu_to_le16(info->disk.raid_disk);
977 } else if (strcmp(update, "resync") == 0) {
978 /* make sure resync happens */
979 sb->resync_offset = 0ULL;
980 } else if (strcmp(update, "uuid") == 0) {
981 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
982
983 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
984 struct bitmap_super_s *bm;
985 bm = (struct bitmap_super_s*)(st->sb+MAX_SB_SIZE);
986 memcpy(bm->uuid, sb->set_uuid, 16);
987 }
988 } else if (strcmp(update, "no-bitmap") == 0) {
989 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
990 } else if (strcmp(update, "bbl") == 0) {
991 /* only possible if there is room after the bitmap, or if
992 * there is no bitmap
993 */
994 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
995 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
996 long bitmap_offset = (long)__le64_to_cpu(sb->bitmap_offset);
997 long bm_sectors = 0;
998 long space;
999
1000 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1001 struct bitmap_super_s *bsb;
1002 bsb = (struct bitmap_super_s *)(((char*)sb)+MAX_SB_SIZE);
1003 bm_sectors = bitmap_sectors(bsb);
1004 }
1005
1006 if (sb_offset < data_offset) {
1007 /* 1.1 or 1.2. Put bbl just before data
1008 */
1009 long bb_offset;
1010 space = data_offset - sb_offset;
1011 bb_offset = space - 8;
1012 if (bm_sectors && bitmap_offset > 0)
1013 space -= (bitmap_offset + bm_sectors);
1014 else
1015 space -= 8; /* The superblock */
1016 if (space >= 8) {
1017 sb->bblog_size = __cpu_to_le16(8);
1018 sb->bblog_offset = __cpu_to_le32(bb_offset);
1019 }
1020 } else {
1021 /* 1.0 - Put bbl just before super block */
1022 if (bm_sectors && bitmap_offset < 0)
1023 space = -bitmap_offset - bm_sectors;
1024 else
1025 space = sb_offset - data_offset -
1026 __le64_to_cpu(sb->data_size);
1027 if (space >= 8) {
1028 sb->bblog_size = __cpu_to_le16(8);
1029 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1030 }
1031 }
1032 } else if (strcmp(update, "no-bbl") == 0) {
1033 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
1034 pr_err("Cannot remove active bbl from %s\n",devname);
1035 else {
1036 sb->bblog_size = 0;
1037 sb->bblog_shift = 0;
1038 sb->bblog_offset = 0;
1039 }
1040 } else if (strcmp(update, "homehost") == 0 &&
1041 homehost) {
1042 char *c;
1043 update = "name";
1044 c = strchr(sb->set_name, ':');
1045 if (c)
1046 strncpy(info->name, c+1, 31 - (c-sb->set_name));
1047 else
1048 strncpy(info->name, sb->set_name, 32);
1049 info->name[32] = 0;
1050 } else if (strcmp(update, "name") == 0) {
1051 if (info->name[0] == 0)
1052 sprintf(info->name, "%d", info->array.md_minor);
1053 memset(sb->set_name, 0, sizeof(sb->set_name));
1054 if (homehost &&
1055 strchr(info->name, ':') == NULL &&
1056 strlen(homehost)+1+strlen(info->name) < 32) {
1057 strcpy(sb->set_name, homehost);
1058 strcat(sb->set_name, ":");
1059 strcat(sb->set_name, info->name);
1060 } else
1061 strcpy(sb->set_name, info->name);
1062 } else if (strcmp(update, "devicesize") == 0 &&
1063 __le64_to_cpu(sb->super_offset) <
1064 __le64_to_cpu(sb->data_offset)) {
1065 /* set data_size to device size less data_offset */
1066 struct misc_dev_info *misc = (struct misc_dev_info*)
1067 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
1068 printf("Size was %llu\n", (unsigned long long)
1069 __le64_to_cpu(sb->data_size));
1070 sb->data_size = __cpu_to_le64(
1071 misc->device_size - __le64_to_cpu(sb->data_offset));
1072 printf("Size is %llu\n", (unsigned long long)
1073 __le64_to_cpu(sb->data_size));
1074 } else if (strcmp(update, "_reshape_progress")==0)
1075 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1076 else if (strcmp(update, "writemostly")==0)
1077 sb->devflags |= WriteMostly1;
1078 else if (strcmp(update, "readwrite")==0)
1079 sb->devflags &= ~WriteMostly1;
1080 else
1081 rv = -1;
1082
1083 sb->sb_csum = calc_sb_1_csum(sb);
1084 return rv;
1085 }
1086
1087 static int init_super1(struct supertype *st, mdu_array_info_t *info,
1088 unsigned long long size, char *name, char *homehost,
1089 int *uuid, unsigned long long data_offset)
1090 {
1091 struct mdp_superblock_1 *sb;
1092 int spares;
1093 int rfd;
1094 char defname[10];
1095 int sbsize;
1096
1097 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1098 pr_err("%s could not allocate superblock\n", __func__);
1099 return 0;
1100 }
1101 memset(sb, 0, SUPER1_SIZE);
1102
1103 st->sb = sb;
1104 if (info == NULL) {
1105 /* zeroing superblock */
1106 return 0;
1107 }
1108
1109 spares = info->working_disks - info->active_disks;
1110 if (info->raid_disks + spares > MAX_DEVS) {
1111 pr_err("too many devices requested: %d+%d > %d\n",
1112 info->raid_disks , spares, MAX_DEVS);
1113 return 0;
1114 }
1115
1116 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1117 sb->major_version = __cpu_to_le32(1);
1118 sb->feature_map = 0;
1119 sb->pad0 = 0;
1120
1121 if (uuid)
1122 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
1123 else {
1124 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1125 read(rfd, sb->set_uuid, 16) != 16) {
1126 __u32 r[4] = {random(), random(), random(), random()};
1127 memcpy(sb->set_uuid, r, 16);
1128 }
1129 if (rfd >= 0) close(rfd);
1130 }
1131
1132 if (name == NULL || *name == 0) {
1133 sprintf(defname, "%d", info->md_minor);
1134 name = defname;
1135 }
1136 if (homehost &&
1137 strchr(name, ':')== NULL &&
1138 strlen(homehost)+1+strlen(name) < 32) {
1139 strcpy(sb->set_name, homehost);
1140 strcat(sb->set_name, ":");
1141 strcat(sb->set_name, name);
1142 } else
1143 strcpy(sb->set_name, name);
1144
1145 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1146 sb->level = __cpu_to_le32(info->level);
1147 sb->layout = __cpu_to_le32(info->layout);
1148 sb->size = __cpu_to_le64(size*2ULL);
1149 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1150 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1151
1152 sb->data_offset = __cpu_to_le64(data_offset);
1153 sb->data_size = __cpu_to_le64(0);
1154 sb->super_offset = __cpu_to_le64(0);
1155 sb->recovery_offset = __cpu_to_le64(0);
1156
1157 sb->utime = sb->ctime;
1158 sb->events = __cpu_to_le64(1);
1159 if (info->state & (1<<MD_SB_CLEAN))
1160 sb->resync_offset = MaxSector;
1161 else
1162 sb->resync_offset = 0;
1163 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1164 sbsize = ROUND_UP(sbsize, 512);
1165 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
1166
1167 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
1168
1169 return 1;
1170 }
1171
1172 struct devinfo {
1173 int fd;
1174 char *devname;
1175 long long data_offset;
1176 mdu_disk_info_t disk;
1177 struct devinfo *next;
1178 };
1179 #ifndef MDASSEMBLE
1180 /* Add a device to the superblock being created */
1181 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
1182 int fd, char *devname, unsigned long long data_offset)
1183 {
1184 struct mdp_superblock_1 *sb = st->sb;
1185 __u16 *rp = sb->dev_roles + dk->number;
1186 struct devinfo *di, **dip;
1187
1188 if ((dk->state & 6) == 6) /* active, sync */
1189 *rp = __cpu_to_le16(dk->raid_disk);
1190 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
1191 *rp = 0xffff;
1192 else
1193 *rp = 0xfffe;
1194
1195 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
1196 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
1197 sb->max_dev = __cpu_to_le32(dk->number+1);
1198
1199 sb->dev_number = __cpu_to_le32(dk->number);
1200 sb->devflags = 0; /* don't copy another disks flags */
1201 sb->sb_csum = calc_sb_1_csum(sb);
1202
1203 dip = (struct devinfo **)&st->info;
1204 while (*dip)
1205 dip = &(*dip)->next;
1206 di = xmalloc(sizeof(struct devinfo));
1207 di->fd = fd;
1208 di->devname = devname;
1209 di->disk = *dk;
1210 di->data_offset = data_offset;
1211 di->next = NULL;
1212 *dip = di;
1213
1214 return 0;
1215 }
1216 #endif
1217
1218 static void locate_bitmap1(struct supertype *st, int fd);
1219
1220 static int store_super1(struct supertype *st, int fd)
1221 {
1222 struct mdp_superblock_1 *sb = st->sb;
1223 unsigned long long sb_offset;
1224 struct align_fd afd;
1225 int sbsize;
1226 unsigned long long dsize;
1227
1228 if (!get_dev_size(fd, NULL, &dsize))
1229 return 1;
1230
1231 dsize >>= 9;
1232
1233 if (dsize < 24)
1234 return 2;
1235
1236 init_afd(&afd, fd);
1237
1238 /*
1239 * Calculate the position of the superblock.
1240 * It is always aligned to a 4K boundary and
1241 * depending on minor_version, it can be:
1242 * 0: At least 8K, but less than 12K, from end of device
1243 * 1: At start of device
1244 * 2: 4K from start of device.
1245 */
1246 switch(st->minor_version) {
1247 case 0:
1248 sb_offset = dsize;
1249 sb_offset -= 8*2;
1250 sb_offset &= ~(4*2-1);
1251 break;
1252 case 1:
1253 sb_offset = 0;
1254 break;
1255 case 2:
1256 sb_offset = 4*2;
1257 break;
1258 default:
1259 return -EINVAL;
1260 }
1261
1262
1263
1264 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1265 0 != __le64_to_cpu(sb->super_offset)
1266 ) {
1267 pr_err("internal error - sb_offset is wrong\n");
1268 abort();
1269 }
1270
1271 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1272 return 3;
1273
1274 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1275
1276 if (awrite(&afd, sb, sbsize) != sbsize)
1277 return 4;
1278
1279 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1280 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1281 (((char*)sb)+MAX_SB_SIZE);
1282 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1283 locate_bitmap1(st, fd);
1284 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1285 return 5;
1286 }
1287 }
1288 fsync(fd);
1289 return 0;
1290 }
1291
1292 static int load_super1(struct supertype *st, int fd, char *devname);
1293
1294 static unsigned long choose_bm_space(unsigned long devsize)
1295 {
1296 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1297 * if bigger than 200Gig, save 128k
1298 * NOTE: result must be multiple of 4K else bad things happen
1299 * on 4K-sector devices.
1300 */
1301 if (devsize < 64*2) return 0;
1302 if (devsize - 64*2 >= 200*1024*1024*2)
1303 return 128*2;
1304 if (devsize - 4*2 > 8*1024*1024*2)
1305 return 64*2;
1306 return 4*2;
1307 }
1308
1309 static void free_super1(struct supertype *st);
1310
1311 #ifndef MDASSEMBLE
1312 static int write_init_super1(struct supertype *st)
1313 {
1314 struct mdp_superblock_1 *sb = st->sb;
1315 struct supertype *refst;
1316 int rfd;
1317 int rv = 0;
1318 unsigned long long bm_space;
1319 unsigned long long reserved;
1320 struct devinfo *di;
1321 unsigned long long dsize, array_size;
1322 unsigned long long sb_offset, headroom;
1323 unsigned long long data_offset;
1324
1325 for (di = st->info; di; di = di->next) {
1326 if (di->disk.state == 1)
1327 continue;
1328 if (di->fd < 0)
1329 continue;
1330
1331 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1332 ;
1333
1334 sb->dev_number = __cpu_to_le32(di->disk.number);
1335 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1336 sb->devflags |= WriteMostly1;
1337 else
1338 sb->devflags &= ~WriteMostly1;
1339
1340 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
1341 read(rfd, sb->device_uuid, 16) != 16) {
1342 __u32 r[4] = {random(), random(), random(), random()};
1343 memcpy(sb->device_uuid, r, 16);
1344 }
1345 if (rfd >= 0)
1346 close(rfd);
1347
1348 sb->events = 0;
1349
1350 refst = dup_super(st);
1351 if (load_super1(refst, di->fd, NULL)==0) {
1352 struct mdp_superblock_1 *refsb = refst->sb;
1353
1354 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1355 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1356 /* same array, so preserve events and
1357 * dev_number */
1358 sb->events = refsb->events;
1359 /* bugs in 2.6.17 and earlier mean the
1360 * dev_number chosen in Manage must be preserved
1361 */
1362 if (get_linux_version() >= 2006018)
1363 sb->dev_number = refsb->dev_number;
1364 }
1365 free_super1(refst);
1366 }
1367 free(refst);
1368
1369 if (!get_dev_size(di->fd, NULL, &dsize)) {
1370 rv = 1;
1371 goto error_out;
1372 }
1373 dsize >>= 9;
1374
1375 if (dsize < 24) {
1376 close(di->fd);
1377 rv = 2;
1378 goto error_out;
1379 }
1380
1381
1382 /*
1383 * Calculate the position of the superblock.
1384 * It is always aligned to a 4K boundary and
1385 * depending on minor_version, it can be:
1386 * 0: At least 8K, but less than 12K, from end of device
1387 * 1: At start of device
1388 * 2: 4K from start of device.
1389 * Depending on the array size, we might leave extra space
1390 * for a bitmap.
1391 * Also leave 4K for bad-block log.
1392 */
1393 array_size = __le64_to_cpu(sb->size);
1394 /* work out how much space we left for a bitmap,
1395 * Add 8 sectors for bad block log */
1396 bm_space = choose_bm_space(array_size) + 8;
1397
1398 /* We try to leave 0.1% at the start for reshape
1399 * operations, but limit this to 128Meg (0.1% of 10Gig)
1400 * which is plenty for efficient reshapes
1401 * However we make it at least 2 chunks as one chunk
1402 * is minimum needed for reshape.
1403 */
1404 headroom = 128 * 1024 * 2;
1405 while (headroom << 10 > array_size &&
1406 headroom/2 >= __le32_to_cpu(sb->chunksize) * 2)
1407 headroom >>= 1;
1408
1409 data_offset = di->data_offset;
1410 switch(st->minor_version) {
1411 case 0:
1412 sb_offset = dsize;
1413 sb_offset -= 8*2;
1414 sb_offset &= ~(4*2-1);
1415 sb->super_offset = __cpu_to_le64(sb_offset);
1416 if (data_offset == INVALID_SECTORS)
1417 sb->data_offset = 0;
1418 if (sb_offset < array_size + bm_space)
1419 bm_space = sb_offset - array_size;
1420 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1421 if (bm_space >= 8) {
1422 sb->bblog_size = __cpu_to_le16(8);
1423 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1424 }
1425 break;
1426 case 1:
1427 sb->super_offset = __cpu_to_le64(0);
1428 if (data_offset == INVALID_SECTORS) {
1429 reserved = bm_space + 4*2;
1430 if (reserved < headroom)
1431 reserved = headroom;
1432 if (reserved + array_size > dsize)
1433 reserved = dsize - array_size;
1434 /* Try for multiple of 1Meg so it is nicely aligned */
1435 #define ONE_MEG (2*1024)
1436 if (reserved > ONE_MEG)
1437 reserved = (reserved/ONE_MEG) * ONE_MEG;
1438
1439 /* force 4K alignment */
1440 reserved &= ~7ULL;
1441
1442 } else
1443 reserved = data_offset;
1444
1445 sb->data_offset = __cpu_to_le64(reserved);
1446 sb->data_size = __cpu_to_le64(dsize - reserved);
1447 if (reserved >= 16) {
1448 sb->bblog_size = __cpu_to_le16(8);
1449 sb->bblog_offset = __cpu_to_le32(reserved-8);
1450 }
1451 break;
1452 case 2:
1453 sb_offset = 4*2;
1454 sb->super_offset = __cpu_to_le64(4*2);
1455 if (data_offset == INVALID_SECTORS) {
1456 if (4*2 + 4*2 + bm_space + array_size
1457 > dsize)
1458 bm_space = dsize - array_size
1459 - 4*2 - 4*2;
1460
1461 reserved = bm_space + 4*2 + 4*2;
1462 if (reserved < headroom)
1463 reserved = headroom;
1464 if (reserved + array_size > dsize)
1465 reserved = dsize - array_size;
1466 /* Try for multiple of 1Meg so it is nicely aligned */
1467 #define ONE_MEG (2*1024)
1468 if (reserved > ONE_MEG)
1469 reserved = (reserved/ONE_MEG) * ONE_MEG;
1470
1471 /* force 4K alignment */
1472 reserved &= ~7ULL;
1473
1474 } else
1475 reserved = data_offset;
1476
1477 sb->data_offset = __cpu_to_le64(reserved);
1478 sb->data_size = __cpu_to_le64(dsize - reserved);
1479 if (reserved >= 16+16) {
1480 sb->bblog_size = __cpu_to_le16(8);
1481 /* '8' sectors for the bblog, and another '8'
1482 * because we want offset from superblock, not
1483 * start of device.
1484 */
1485 sb->bblog_offset = __cpu_to_le32(reserved-8-8);
1486 }
1487 break;
1488 default:
1489 pr_err("Failed to write invalid "
1490 "metadata format 1.%i to %s\n",
1491 st->minor_version, di->devname);
1492 rv = -EINVAL;
1493 goto out;
1494 }
1495
1496 sb->sb_csum = calc_sb_1_csum(sb);
1497 rv = store_super1(st, di->fd);
1498 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
1499 rv = st->ss->write_bitmap(st, di->fd);
1500 close(di->fd);
1501 di->fd = -1;
1502 if (rv)
1503 goto error_out;
1504 }
1505 error_out:
1506 if (rv)
1507 pr_err("Failed to write metadata to %s\n",
1508 di->devname);
1509 out:
1510 return rv;
1511 }
1512 #endif
1513
1514 static int compare_super1(struct supertype *st, struct supertype *tst)
1515 {
1516 /*
1517 * return:
1518 * 0 same, or first was empty, and second was copied
1519 * 1 second had wrong number
1520 * 2 wrong uuid
1521 * 3 wrong other info
1522 */
1523 struct mdp_superblock_1 *first = st->sb;
1524 struct mdp_superblock_1 *second = tst->sb;
1525
1526 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
1527 return 1;
1528 if (second->major_version != __cpu_to_le32(1))
1529 return 1;
1530
1531 if (!first) {
1532 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
1533 pr_err("%s could not allocate superblock\n", __func__);
1534 return 1;
1535 }
1536 memcpy(first, second, SUPER1_SIZE);
1537 st->sb = first;
1538 return 0;
1539 }
1540 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
1541 return 2;
1542
1543 if (first->ctime != second->ctime ||
1544 first->level != second->level ||
1545 first->layout != second->layout ||
1546 first->size != second->size ||
1547 first->chunksize != second->chunksize ||
1548 first->raid_disks != second->raid_disks)
1549 return 3;
1550 return 0;
1551 }
1552
1553 static int load_super1(struct supertype *st, int fd, char *devname)
1554 {
1555 unsigned long long dsize;
1556 unsigned long long sb_offset;
1557 struct mdp_superblock_1 *super;
1558 int uuid[4];
1559 struct bitmap_super_s *bsb;
1560 struct misc_dev_info *misc;
1561 struct align_fd afd;
1562
1563 free_super1(st);
1564
1565 init_afd(&afd, fd);
1566
1567 if (st->ss == NULL || st->minor_version == -1) {
1568 int bestvers = -1;
1569 struct supertype tst;
1570 __u64 bestctime = 0;
1571 /* guess... choose latest ctime */
1572 memset(&tst, 0, sizeof(tst));
1573 tst.ss = &super1;
1574 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
1575 switch(load_super1(&tst, fd, devname)) {
1576 case 0: super = tst.sb;
1577 if (bestvers == -1 ||
1578 bestctime < __le64_to_cpu(super->ctime)) {
1579 bestvers = tst.minor_version;
1580 bestctime = __le64_to_cpu(super->ctime);
1581 }
1582 free(super);
1583 tst.sb = NULL;
1584 break;
1585 case 1: return 1; /*bad device */
1586 case 2: break; /* bad, try next */
1587 }
1588 }
1589 if (bestvers != -1) {
1590 int rv;
1591 tst.minor_version = bestvers;
1592 tst.ss = &super1;
1593 tst.max_devs = MAX_DEVS;
1594 rv = load_super1(&tst, fd, devname);
1595 if (rv == 0)
1596 *st = tst;
1597 return rv;
1598 }
1599 return 2;
1600 }
1601 if (!get_dev_size(fd, devname, &dsize))
1602 return 1;
1603 dsize >>= 9;
1604
1605 if (dsize < 24) {
1606 if (devname)
1607 pr_err("%s is too small for md: size is %llu sectors.\n",
1608 devname, dsize);
1609 return 1;
1610 }
1611
1612 /*
1613 * Calculate the position of the superblock.
1614 * It is always aligned to a 4K boundary and
1615 * depending on minor_version, it can be:
1616 * 0: At least 8K, but less than 12K, from end of device
1617 * 1: At start of device
1618 * 2: 4K from start of device.
1619 */
1620 switch(st->minor_version) {
1621 case 0:
1622 sb_offset = dsize;
1623 sb_offset -= 8*2;
1624 sb_offset &= ~(4*2-1);
1625 break;
1626 case 1:
1627 sb_offset = 0;
1628 break;
1629 case 2:
1630 sb_offset = 4*2;
1631 break;
1632 default:
1633 return -EINVAL;
1634 }
1635
1636 ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */
1637
1638
1639 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
1640 if (devname)
1641 pr_err("Cannot seek to superblock on %s: %s\n",
1642 devname, strerror(errno));
1643 return 1;
1644 }
1645
1646 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
1647 pr_err("%s could not allocate superblock\n",
1648 __func__);
1649 return 1;
1650 }
1651
1652 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
1653 if (devname)
1654 pr_err("Cannot read superblock on %s\n",
1655 devname);
1656 free(super);
1657 return 1;
1658 }
1659
1660 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1661 if (devname)
1662 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
1663 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1664 free(super);
1665 return 2;
1666 }
1667
1668 if (__le32_to_cpu(super->major_version) != 1) {
1669 if (devname)
1670 pr_err("Cannot interpret superblock on %s - version is %d\n",
1671 devname, __le32_to_cpu(super->major_version));
1672 free(super);
1673 return 2;
1674 }
1675 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1676 if (devname)
1677 pr_err("No superblock found on %s (super_offset is wrong)\n",
1678 devname);
1679 free(super);
1680 return 2;
1681 }
1682 st->sb = super;
1683
1684 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1685
1686 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
1687 misc->device_size = dsize;
1688
1689 /* Now check on the bitmap superblock */
1690 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1691 return 0;
1692 /* Read the bitmap superblock and make sure it looks
1693 * valid. If it doesn't clear the bit. An --assemble --force
1694 * should get that written out.
1695 */
1696 locate_bitmap1(st, fd);
1697 if (aread(&afd, bsb, 512) != 512)
1698 goto no_bitmap;
1699
1700 uuid_from_super1(st, uuid);
1701 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1702 memcmp(bsb->uuid, uuid, 16) != 0)
1703 goto no_bitmap;
1704 return 0;
1705
1706 no_bitmap:
1707 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
1708 & ~MD_FEATURE_BITMAP_OFFSET);
1709 return 0;
1710 }
1711
1712
1713 static struct supertype *match_metadata_desc1(char *arg)
1714 {
1715 struct supertype *st = xcalloc(1, sizeof(*st));
1716
1717 st->container_dev = NoMdDev;
1718 st->ss = &super1;
1719 st->max_devs = MAX_DEVS;
1720 st->sb = NULL;
1721 /* leading zeros can be safely ignored. --detail generates them. */
1722 while (*arg == '0')
1723 arg++;
1724 if (strcmp(arg, "1.0") == 0 ||
1725 strcmp(arg, "1.00") == 0) {
1726 st->minor_version = 0;
1727 return st;
1728 }
1729 if (strcmp(arg, "1.1") == 0 ||
1730 strcmp(arg, "1.01") == 0
1731 ) {
1732 st->minor_version = 1;
1733 return st;
1734 }
1735 if (strcmp(arg, "1.2") == 0 ||
1736 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
1737 strcmp(arg, "default") == 0 ||
1738 #endif /* DEFAULT_OLD_METADATA */
1739 strcmp(arg, "1.02") == 0) {
1740 st->minor_version = 2;
1741 return st;
1742 }
1743 if (strcmp(arg, "1") == 0 ||
1744 strcmp(arg, "default") == 0) {
1745 st->minor_version = -1;
1746 return st;
1747 }
1748
1749 free(st);
1750 return NULL;
1751 }
1752
1753 /* find available size on device with this devsize, using
1754 * superblock type st, and reserving 'reserve' sectors for
1755 * a possible bitmap
1756 */
1757 static __u64 _avail_size1(struct supertype *st, __u64 devsize,
1758 unsigned long long data_offset, int chunksize)
1759 {
1760 struct mdp_superblock_1 *super = st->sb;
1761 int bmspace = 0;
1762 if (devsize < 24)
1763 return 0;
1764
1765 if (super == NULL)
1766 /* creating: allow suitable space for bitmap */
1767 bmspace = choose_bm_space(devsize);
1768 #ifndef MDASSEMBLE
1769 else if (__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
1770 /* hot-add. allow for actual size of bitmap */
1771 struct bitmap_super_s *bsb;
1772 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
1773 bmspace = bitmap_sectors(bsb);
1774 }
1775 #endif
1776 /* Allow space for bad block log */
1777 if (super && super->bblog_size)
1778 devsize -= __le16_to_cpu(super->bblog_size);
1779 else
1780 devsize -= 8;
1781
1782
1783 if (st->minor_version < 0)
1784 /* not specified, so time to set default */
1785 st->minor_version = 2;
1786
1787 if (data_offset != INVALID_SECTORS)
1788 switch(st->minor_version) {
1789 case 0:
1790 return devsize - data_offset - 8*2;
1791 case 1:
1792 case 2:
1793 return devsize - data_offset;
1794 default:
1795 return 0;
1796 }
1797
1798 devsize -= bmspace;
1799
1800 if (super == NULL && st->minor_version > 0) {
1801 /* haven't committed to a size yet, so allow some
1802 * slack for space for reshape.
1803 * Limit slack to 128M, but aim for about 0.1%
1804 */
1805 unsigned long long headroom = 128*1024*2;
1806 while ((headroom << 10) > devsize &&
1807 (chunksize == 0 ||
1808 headroom / 2 >= ((unsigned)chunksize*2)*2))
1809 headroom >>= 1;
1810 devsize -= headroom;
1811 }
1812 switch(st->minor_version) {
1813 case 0:
1814 /* at end */
1815 return ((devsize - 8*2 ) & ~(4*2-1));
1816 case 1:
1817 /* at start, 4K for superblock and possible bitmap */
1818 return devsize - 4*2;
1819 case 2:
1820 /* 4k from start, 4K for superblock and possible bitmap */
1821 return devsize - (4+4)*2;
1822 }
1823 return 0;
1824 }
1825 static __u64 avail_size1(struct supertype *st, __u64 devsize,
1826 unsigned long long data_offset)
1827 {
1828 return _avail_size1(st, devsize, data_offset, 0);
1829 }
1830
1831 static int
1832 add_internal_bitmap1(struct supertype *st,
1833 int *chunkp, int delay, int write_behind,
1834 unsigned long long size,
1835 int may_change, int major)
1836 {
1837 /*
1838 * If not may_change, then this is a 'Grow' without sysfs support for
1839 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
1840 * If may_change, then this is create or a Grow with sysfs syupport,
1841 * and we can put the bitmap wherever we like.
1842 *
1843 * size is in sectors, chunk is in bytes !!!
1844 */
1845
1846 unsigned long long bits;
1847 unsigned long long max_bits;
1848 unsigned long long min_chunk;
1849 long offset;
1850 long bbl_offset, bbl_size;
1851 unsigned long long chunk = *chunkp;
1852 int room = 0;
1853 int creating = 0;
1854 struct mdp_superblock_1 *sb = st->sb;
1855 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1856 int uuid[4];
1857
1858
1859 if (__le64_to_cpu(sb->data_size) == 0)
1860 /* Must be creating the array, else data_size would be non-zero */
1861 creating = 1;
1862 switch(st->minor_version) {
1863 case 0:
1864 /* either 3K after the superblock (when hot-add),
1865 * or some amount of space before.
1866 */
1867 if (creating) {
1868 /* We are creating array, so we *know* how much room has
1869 * been left.
1870 */
1871 offset = 0;
1872 room = choose_bm_space(__le64_to_cpu(sb->size));
1873 bbl_size = 8;
1874 } else {
1875 room = __le64_to_cpu(sb->super_offset)
1876 - __le64_to_cpu(sb->data_offset)
1877 - __le64_to_cpu(sb->data_size);
1878 bbl_size = __le16_to_cpu(sb->bblog_size);
1879 if (bbl_size < 8)
1880 bbl_size = 8;
1881 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
1882 if (bbl_size < -bbl_offset)
1883 bbl_size = -bbl_offset;
1884
1885 if (!may_change || (room < 3*2 &&
1886 __le32_to_cpu(sb->max_dev) <= 384)) {
1887 room = 3*2;
1888 offset = 1*2;
1889 bbl_size = 0;
1890 } else {
1891 offset = 0; /* means movable offset */
1892 }
1893 }
1894 break;
1895 case 1:
1896 case 2: /* between superblock and data */
1897 if (creating) {
1898 offset = 4*2;
1899 room = choose_bm_space(__le64_to_cpu(sb->size));
1900 bbl_size = 8;
1901 } else {
1902 room = __le64_to_cpu(sb->data_offset)
1903 - __le64_to_cpu(sb->super_offset);
1904 bbl_size = __le16_to_cpu(sb->bblog_size);
1905 if (bbl_size)
1906 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
1907 else
1908 bbl_size = 8;
1909
1910 if (!may_change) {
1911 room -= 2; /* Leave 1K for superblock */
1912 offset = 2;
1913 bbl_size = 0;
1914 } else {
1915 room -= 4*2; /* leave 4K for superblock */
1916 offset = 4*2;
1917 }
1918 }
1919 break;
1920 default:
1921 return 0;
1922 }
1923
1924 room -= bbl_size;
1925 if (chunk == UnSet && room > 128*2)
1926 /* Limit to 128K of bitmap when chunk size not requested */
1927 room = 128*2;
1928
1929 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
1930
1931 min_chunk = 4096; /* sub-page chunks don't work yet.. */
1932 bits = (size*512)/min_chunk +1;
1933 while (bits > max_bits) {
1934 min_chunk *= 2;
1935 bits = (bits+1)/2;
1936 }
1937 if (chunk == UnSet) {
1938 /* For practical purpose, 64Meg is a good
1939 * default chunk size for internal bitmaps.
1940 */
1941 chunk = min_chunk;
1942 if (chunk < 64*1024*1024)
1943 chunk = 64*1024*1024;
1944 } else if (chunk < min_chunk)
1945 return 0; /* chunk size too small */
1946 if (chunk == 0) /* rounding problem */
1947 return 0;
1948
1949 if (offset == 0) {
1950 /* start bitmap on a 4K boundary with enough space for
1951 * the bitmap
1952 */
1953 bits = (size*512) / chunk + 1;
1954 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
1955 room *= 8; /* convert 4K blocks to sectors */
1956 offset = -room - bbl_size;
1957 }
1958
1959 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
1960
1961 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
1962 | MD_FEATURE_BITMAP_OFFSET);
1963 memset(bms, 0, sizeof(*bms));
1964 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
1965 bms->version = __cpu_to_le32(major);
1966 uuid_from_super1(st, uuid);
1967 memcpy(bms->uuid, uuid, 16);
1968 bms->chunksize = __cpu_to_le32(chunk);
1969 bms->daemon_sleep = __cpu_to_le32(delay);
1970 bms->sync_size = __cpu_to_le64(size);
1971 bms->write_behind = __cpu_to_le32(write_behind);
1972
1973 *chunkp = chunk;
1974 return 1;
1975 }
1976
1977 static void locate_bitmap1(struct supertype *st, int fd)
1978 {
1979 unsigned long long offset;
1980 struct mdp_superblock_1 *sb;
1981 int mustfree = 0;
1982
1983 if (!st->sb) {
1984 if (st->ss->load_super(st, fd, NULL))
1985 return; /* no error I hope... */
1986 mustfree = 1;
1987 }
1988 sb = st->sb;
1989
1990 offset = __le64_to_cpu(sb->super_offset);
1991 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset);
1992 if (mustfree)
1993 free(sb);
1994 lseek64(fd, offset<<9, 0);
1995 }
1996
1997 static int write_bitmap1(struct supertype *st, int fd)
1998 {
1999 struct mdp_superblock_1 *sb = st->sb;
2000 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
2001 int rv = 0;
2002 void *buf;
2003 int towrite, n;
2004 struct align_fd afd;
2005
2006 init_afd(&afd, fd);
2007
2008 locate_bitmap1(st, fd);
2009
2010 if (posix_memalign(&buf, 4096, 4096))
2011 return -ENOMEM;
2012
2013 memset(buf, 0xff, 4096);
2014 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2015
2016 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
2017 towrite = (towrite+7) >> 3; /* bits to bytes */
2018 towrite += sizeof(bitmap_super_t);
2019 towrite = ROUND_UP(towrite, 512);
2020 while (towrite > 0) {
2021 n = towrite;
2022 if (n > 4096)
2023 n = 4096;
2024 n = awrite(&afd, buf, n);
2025 if (n > 0)
2026 towrite -= n;
2027 else
2028 break;
2029 memset(buf, 0xff, 4096);
2030 }
2031 fsync(fd);
2032 if (towrite)
2033 rv = -2;
2034
2035 free(buf);
2036 return rv;
2037 }
2038
2039 static void free_super1(struct supertype *st)
2040 {
2041 if (st->sb)
2042 free(st->sb);
2043 while (st->info) {
2044 struct devinfo *di = st->info;
2045 st->info = di->next;
2046 if (di->fd >= 0)
2047 close(di->fd);
2048 free(di);
2049 }
2050 st->sb = NULL;
2051 }
2052
2053 #ifndef MDASSEMBLE
2054 static int validate_geometry1(struct supertype *st, int level,
2055 int layout, int raiddisks,
2056 int *chunk, unsigned long long size,
2057 unsigned long long data_offset,
2058 char *subdev, unsigned long long *freesize,
2059 int verbose)
2060 {
2061 unsigned long long ldsize;
2062 int fd;
2063
2064 if (level == LEVEL_CONTAINER) {
2065 if (verbose)
2066 pr_err("1.x metadata does not support containers\n");
2067 return 0;
2068 }
2069 if (chunk && *chunk == UnSet)
2070 *chunk = DEFAULT_CHUNK;
2071
2072 if (!subdev)
2073 return 1;
2074
2075 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2076 if (fd < 0) {
2077 if (verbose)
2078 pr_err("super1.x cannot open %s: %s\n",
2079 subdev, strerror(errno));
2080 return 0;
2081 }
2082
2083 if (!get_dev_size(fd, subdev, &ldsize)) {
2084 close(fd);
2085 return 0;
2086 }
2087 close(fd);
2088
2089 *freesize = _avail_size1(st, ldsize >> 9, data_offset, *chunk);
2090 return 1;
2091 }
2092 #endif /* MDASSEMBLE */
2093
2094 struct superswitch super1 = {
2095 #ifndef MDASSEMBLE
2096 .examine_super = examine_super1,
2097 .brief_examine_super = brief_examine_super1,
2098 .export_examine_super = export_examine_super1,
2099 .detail_super = detail_super1,
2100 .brief_detail_super = brief_detail_super1,
2101 .export_detail_super = export_detail_super1,
2102 .write_init_super = write_init_super1,
2103 .validate_geometry = validate_geometry1,
2104 .add_to_super = add_to_super1,
2105 .examine_badblocks = examine_badblocks_super1,
2106 #endif
2107 .match_home = match_home1,
2108 .uuid_from_super = uuid_from_super1,
2109 .getinfo_super = getinfo_super1,
2110 .container_content = container_content1,
2111 .update_super = update_super1,
2112 .init_super = init_super1,
2113 .store_super = store_super1,
2114 .compare_super = compare_super1,
2115 .load_super = load_super1,
2116 .match_metadata_desc = match_metadata_desc1,
2117 .avail_size = avail_size1,
2118 .add_internal_bitmap = add_internal_bitmap1,
2119 .locate_bitmap = locate_bitmap1,
2120 .write_bitmap = write_bitmap1,
2121 .free_super = free_super1,
2122 #if __BYTE_ORDER == BIG_ENDIAN
2123 .swapuuid = 0,
2124 #else
2125 .swapuuid = 1,
2126 #endif
2127 .name = "1.x",
2128 };