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