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