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