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