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