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