]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super1.c
Add 'ppl' and 'no-ppl' options for --update=
[thirdparty/mdadm.git] / super1.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2016 Neil Brown <neilb@suse.com>
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 <stddef.h>
26 #include "mdadm.h"
27 /*
28 * The version-1 superblock :
29 * All numeric fields are little-endian.
30 *
31 * total size: 256 bytes plus 2 per device.
32 * 1K allows 384 devices.
33 */
34 struct mdp_superblock_1 {
35 /* constant array information - 128 bytes */
36 __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
37 __u32 major_version; /* 1 */
38 __u32 feature_map; /* 0 for now */
39 __u32 pad0; /* always set to 0 when writing */
40
41 __u8 set_uuid[16]; /* user-space generated. */
42 char set_name[32]; /* set and interpreted by user-space */
43
44 __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
45 __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */
46 __u32 layout; /* only for raid5 currently */
47 __u64 size; /* used size of component devices, in 512byte sectors */
48
49 __u32 chunksize; /* in 512byte sectors */
50 __u32 raid_disks;
51 union {
52 __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts
53 * NOTE: signed, so bitmap can be before superblock
54 * only meaningful of feature_map[0] is set.
55 */
56
57 /* only meaningful when feature_map[MD_FEATURE_PPL] is set */
58 struct {
59 __s16 offset; /* sectors from start of superblock that ppl starts */
60 __u16 size; /* ppl size in sectors */
61 } ppl;
62 };
63
64 /* These are only valid with feature bit '4' */
65 __u32 new_level; /* new level we are reshaping to */
66 __u64 reshape_position; /* next address in array-space for reshape */
67 __u32 delta_disks; /* change in number of raid_disks */
68 __u32 new_layout; /* new layout */
69 __u32 new_chunk; /* new chunk size (sectors) */
70 __u32 new_offset; /* signed number to add to data_offset in new
71 * layout. 0 == no-change. This can be
72 * different on each device in the array.
73 */
74
75 /* constant this-device information - 64 bytes */
76 __u64 data_offset; /* sector start of data, often 0 */
77 __u64 data_size; /* sectors in this device that can be used for data */
78 __u64 super_offset; /* sector start of this superblock */
79 union {
80 __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */
81 __u64 journal_tail;/* journal tail of journal device (from data_offset) */
82 };
83 __u32 dev_number; /* permanent identifier of this device - not role in raid */
84 __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */
85 __u8 device_uuid[16]; /* user-space setable, ignored by kernel */
86 __u8 devflags; /* per-device flags. Only one defined...*/
87 #define WriteMostly1 1 /* mask for writemostly flag in above */
88 #define FailFast1 2 /* Device should get FailFast requests */
89 /* bad block log. If there are any bad blocks the feature flag is set.
90 * if offset and size are non-zero, that space is reserved and available.
91 */
92 __u8 bblog_shift; /* shift from sectors to block size for badblocklist */
93 __u16 bblog_size; /* number of sectors reserved for badblocklist */
94 __u32 bblog_offset; /* sector offset from superblock to bblog, signed */
95
96 /* array state information - 64 bytes */
97 __u64 utime; /* 40 bits second, 24 btes microseconds */
98 __u64 events; /* incremented when superblock updated */
99 __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */
100 __u32 sb_csum; /* checksum upto dev_roles[max_dev] */
101 __u32 max_dev; /* size of dev_roles[] array to consider */
102 __u8 pad3[64-32]; /* set to 0 when writing */
103
104 /* device state information. Indexed by dev_number.
105 * 2 bytes per device
106 * Note there are no per-device state flags. State information is rolled
107 * into the 'roles' value. If a device is spare or faulty, then it doesn't
108 * have a meaningful role.
109 */
110 __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
111 };
112
113 #define MAX_SB_SIZE 4096
114 /* bitmap super size is 256, but we round up to a sector for alignment */
115 #define BM_SUPER_SIZE 512
116 #define MAX_DEVS ((int)(MAX_SB_SIZE - sizeof(struct mdp_superblock_1)) / 2)
117 #define SUPER1_SIZE (MAX_SB_SIZE + BM_SUPER_SIZE \
118 + sizeof(struct misc_dev_info))
119
120 struct misc_dev_info {
121 __u64 device_size;
122 };
123
124 /* feature_map bits */
125 #define MD_FEATURE_BITMAP_OFFSET 1
126 #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and
127 * must be honoured
128 */
129 #define MD_FEATURE_RESHAPE_ACTIVE 4
130 #define MD_FEATURE_BAD_BLOCKS 8 /* badblock list is not empty */
131 #define MD_FEATURE_REPLACEMENT 16 /* This device is replacing an
132 * active device with same 'role'.
133 * 'recovery_offset' is also set.
134 */
135 #define MD_FEATURE_RESHAPE_BACKWARDS 32 /* Reshape doesn't change number
136 * of devices, but is going
137 * backwards anyway.
138 */
139 #define MD_FEATURE_NEW_OFFSET 64 /* new_offset must be honoured */
140 #define MD_FEATURE_BITMAP_VERSIONED 256 /* bitmap version number checked properly */
141 #define MD_FEATURE_JOURNAL 512 /* support write journal */
142 #define MD_FEATURE_PPL 1024 /* support PPL */
143 #define MD_FEATURE_ALL (MD_FEATURE_BITMAP_OFFSET \
144 |MD_FEATURE_RECOVERY_OFFSET \
145 |MD_FEATURE_RESHAPE_ACTIVE \
146 |MD_FEATURE_BAD_BLOCKS \
147 |MD_FEATURE_REPLACEMENT \
148 |MD_FEATURE_RESHAPE_BACKWARDS \
149 |MD_FEATURE_NEW_OFFSET \
150 |MD_FEATURE_BITMAP_VERSIONED \
151 |MD_FEATURE_JOURNAL \
152 |MD_FEATURE_PPL \
153 )
154
155 #ifndef MDASSEMBLE
156 static int role_from_sb(struct mdp_superblock_1 *sb)
157 {
158 unsigned int d;
159 int role;
160
161 d = __le32_to_cpu(sb->dev_number);
162 if (d < __le32_to_cpu(sb->max_dev))
163 role = __le16_to_cpu(sb->dev_roles[d]);
164 else
165 role = MD_DISK_ROLE_SPARE;
166 return role;
167 }
168 #endif
169
170 /* return how many bytes are needed for bitmap, for cluster-md each node
171 * should have it's own bitmap */
172 static unsigned int calc_bitmap_size(bitmap_super_t *bms, unsigned int boundary)
173 {
174 unsigned long long bits, bytes;
175
176 bits = bitmap_bits(__le64_to_cpu(bms->sync_size),
177 __le32_to_cpu(bms->chunksize));
178 bytes = (bits+7) >> 3;
179 bytes += sizeof(bitmap_super_t);
180 bytes = ROUND_UP(bytes, boundary);
181
182 return bytes;
183 }
184
185 static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
186 {
187 unsigned int disk_csum, csum;
188 unsigned long long newcsum;
189 int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2;
190 unsigned int *isuper = (unsigned int*)sb;
191
192 /* make sure I can count... */
193 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
194 offsetof(struct mdp_superblock_1, utime) != 192 ||
195 sizeof(struct mdp_superblock_1) != 256) {
196 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
197 }
198
199 disk_csum = sb->sb_csum;
200 sb->sb_csum = 0;
201 newcsum = 0;
202 for (; size>=4; size -= 4 ) {
203 newcsum += __le32_to_cpu(*isuper);
204 isuper++;
205 }
206
207 if (size == 2)
208 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
209
210 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
211 sb->sb_csum = disk_csum;
212 return __cpu_to_le32(csum);
213 }
214
215 /*
216 * Information related to file descriptor used for aligned reads/writes.
217 * Cache the block size.
218 */
219 struct align_fd {
220 int fd;
221 int blk_sz;
222 };
223
224 static void init_afd(struct align_fd *afd, int fd)
225 {
226 afd->fd = fd;
227 if (!get_dev_sector_size(afd->fd, NULL, (unsigned int *)&afd->blk_sz))
228 afd->blk_sz = 512;
229 }
230
231 static char abuf[4096+4096];
232 static int aread(struct align_fd *afd, void *buf, int len)
233 {
234 /* aligned read.
235 * On devices with a 4K sector size, we need to read
236 * the full sector and copy relevant bits into
237 * the buffer
238 */
239 int bsize, iosize;
240 char *b;
241 int n;
242
243 bsize = afd->blk_sz;
244
245 if (!bsize || bsize > 4096 || len > 4096) {
246 if (!bsize)
247 fprintf(stderr, "WARNING - aread() called with invalid block size\n");
248 return -1;
249 }
250 b = ROUND_UP_PTR((char *)abuf, 4096);
251
252 for (iosize = 0; iosize < len; iosize += bsize)
253 ;
254 n = read(afd->fd, b, iosize);
255 if (n <= 0)
256 return n;
257 lseek(afd->fd, len - n, 1);
258 if (n > len)
259 n = len;
260 memcpy(buf, b, n);
261 return n;
262 }
263
264 static int awrite(struct align_fd *afd, void *buf, int len)
265 {
266 /* aligned write.
267 * On devices with a 4K sector size, we need to write
268 * the full sector. We pre-read if the sector is larger
269 * than the write.
270 * The address must be sector-aligned.
271 */
272 int bsize, iosize;
273 char *b;
274 int n;
275
276 bsize = afd->blk_sz;
277 if (!bsize || bsize > 4096 || len > 4096) {
278 if (!bsize)
279 fprintf(stderr, "WARNING - awrite() called with invalid block size\n");
280 return -1;
281 }
282 b = ROUND_UP_PTR((char *)abuf, 4096);
283
284 for (iosize = 0; iosize < len ; iosize += bsize)
285 ;
286
287 if (len != iosize) {
288 n = read(afd->fd, b, iosize);
289 if (n <= 0)
290 return n;
291 lseek(afd->fd, -n, 1);
292 }
293
294 memcpy(b, buf, len);
295 n = write(afd->fd, b, iosize);
296 if (n <= 0)
297 return n;
298 lseek(afd->fd, len - n, 1);
299 return len;
300 }
301
302 static inline unsigned int choose_ppl_space(int chunk)
303 {
304 return (PPL_HEADER_SIZE >> 9) + (chunk > 128*2 ? chunk : 128*2);
305 }
306
307 #ifndef MDASSEMBLE
308 static void examine_super1(struct supertype *st, char *homehost)
309 {
310 struct mdp_superblock_1 *sb = st->sb;
311 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
312 time_t atime;
313 unsigned int d;
314 int role;
315 int delta_extra = 0;
316 int i;
317 char *c;
318 int l = homehost ? strlen(homehost) : 0;
319 int layout;
320 unsigned long long sb_offset;
321 struct mdinfo info;
322
323 printf(" Magic : %08x\n", __le32_to_cpu(sb->magic));
324 printf(" Version : 1");
325 sb_offset = __le64_to_cpu(sb->super_offset);
326 if (sb_offset <= 4)
327 printf(".1\n");
328 else if (sb_offset <= 8)
329 printf(".2\n");
330 else
331 printf(".0\n");
332 printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map));
333 printf(" Array UUID : ");
334 for (i=0; i<16; i++) {
335 if ((i&3)==0 && i != 0) printf(":");
336 printf("%02x", sb->set_uuid[i]);
337 }
338 printf("\n");
339 printf(" Name : %.32s", sb->set_name);
340 if (l > 0 && l < 32 &&
341 sb->set_name[l] == ':' &&
342 strncmp(sb->set_name, homehost, l) == 0)
343 printf(" (local to host %s)", homehost);
344 printf("\n");
345 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
346 printf(" Cluster Name : %-64s\n", bms->cluster_name);
347 atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL;
348 printf(" Creation Time : %.24s\n", ctime(&atime));
349 c=map_num(pers, __le32_to_cpu(sb->level));
350 printf(" Raid Level : %s\n", c?c:"-unknown-");
351 printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks));
352 printf("\n");
353 printf(" Avail Dev Size : %llu%s\n",
354 (unsigned long long)__le64_to_cpu(sb->data_size),
355 human_size(__le64_to_cpu(sb->data_size)<<9));
356 if (__le32_to_cpu(sb->level) > 0) {
357 int ddsks = 0, ddsks_denom = 1;
358 switch(__le32_to_cpu(sb->level)) {
359 case 1: ddsks=1;break;
360 case 4:
361 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
362 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
363 case 10:
364 layout = __le32_to_cpu(sb->layout);
365 ddsks = __le32_to_cpu(sb->raid_disks);
366 ddsks_denom = (layout&255) * ((layout>>8)&255);
367 }
368 if (ddsks) {
369 long long asize = __le64_to_cpu(sb->size);
370 asize = (asize << 9) * ddsks / ddsks_denom;
371 printf(" Array Size : %llu%s\n",
372 asize >> 10, human_size(asize));
373 }
374 if (sb->size != sb->data_size)
375 printf(" Used Dev Size : %llu%s\n",
376 (unsigned long long)__le64_to_cpu(sb->size),
377 human_size(__le64_to_cpu(sb->size)<<9));
378 }
379 if (sb->data_offset)
380 printf(" Data Offset : %llu sectors\n",
381 (unsigned long long)__le64_to_cpu(sb->data_offset));
382 if (sb->new_offset &&
383 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) {
384 unsigned long long offset = __le64_to_cpu(sb->data_offset);
385 offset += (signed)(int32_t)__le32_to_cpu(sb->new_offset);
386 printf(" New Offset : %llu sectors\n", offset);
387 }
388 printf(" Super Offset : %llu sectors\n",
389 (unsigned long long)__le64_to_cpu(sb->super_offset));
390 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)
391 printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset));
392
393 st->ss->getinfo_super(st, &info, NULL);
394 if (info.space_after != 1 &&
395 !(__le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET))
396 printf(" Unused Space : before=%llu sectors, after=%llu sectors\n",
397 info.space_before, info.space_after);
398
399 printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean");
400 printf(" Device UUID : ");
401 for (i=0; i<16; i++) {
402 if ((i&3)==0 && i != 0) printf(":");
403 printf("%02x", sb->device_uuid[i]);
404 }
405 printf("\n");
406 printf("\n");
407 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
408 printf("Internal Bitmap : %ld sectors from superblock\n",
409 (long)(int32_t)__le32_to_cpu(sb->bitmap_offset));
410 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
411 printf(" PPL : %u sectors at offset %d sectors from superblock\n",
412 __le16_to_cpu(sb->ppl.size),
413 __le16_to_cpu(sb->ppl.offset));
414 }
415 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)) {
416 printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2,
417 human_size(__le64_to_cpu(sb->reshape_position)<<9));
418 if (__le32_to_cpu(sb->delta_disks)) {
419 printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks));
420 printf(" (%d->%d)\n",
421 __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks),
422 __le32_to_cpu(sb->raid_disks));
423 if ((int)__le32_to_cpu(sb->delta_disks) < 0)
424 delta_extra = -__le32_to_cpu(sb->delta_disks);
425 }
426 if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) {
427 c = map_num(pers, __le32_to_cpu(sb->new_level));
428 printf(" New Level : %s\n", c?c:"-unknown-");
429 }
430 if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) {
431 if (__le32_to_cpu(sb->level) == 5) {
432 c = map_num(r5layout, __le32_to_cpu(sb->new_layout));
433 printf(" New Layout : %s\n", c?c:"-unknown-");
434 }
435 if (__le32_to_cpu(sb->level) == 6) {
436 c = map_num(r6layout, __le32_to_cpu(sb->new_layout));
437 printf(" New Layout : %s\n", c?c:"-unknown-");
438 }
439 if (__le32_to_cpu(sb->level) == 10) {
440 printf(" New Layout :");
441 print_r10_layout(__le32_to_cpu(sb->new_layout));
442 printf("\n");
443 }
444 }
445 if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize))
446 printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2);
447 printf("\n");
448 }
449 if (sb->devflags) {
450 printf(" Flags :");
451 if (sb->devflags & WriteMostly1)
452 printf(" write-mostly");
453 if (sb->devflags & FailFast1)
454 printf(" failfast");
455 printf("\n");
456 }
457
458 atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL;
459 printf(" Update Time : %.24s\n", ctime(&atime));
460
461 if (sb->bblog_size && sb->bblog_offset) {
462 printf(" Bad Block Log : %d entries available at offset %ld sectors",
463 __le16_to_cpu(sb->bblog_size)*512/8,
464 (long)(int32_t)__le32_to_cpu(sb->bblog_offset));
465 if (sb->feature_map &
466 __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
467 printf(" - bad blocks present.");
468 printf("\n");
469 }
470
471 if (calc_sb_1_csum(sb) == sb->sb_csum)
472 printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum));
473 else
474 printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum),
475 __le32_to_cpu(calc_sb_1_csum(sb)));
476 printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events));
477 printf("\n");
478 if (__le32_to_cpu(sb->level) == 5) {
479 c = map_num(r5layout, __le32_to_cpu(sb->layout));
480 printf(" Layout : %s\n", c?c:"-unknown-");
481 }
482 if (__le32_to_cpu(sb->level) == 6) {
483 c = map_num(r6layout, __le32_to_cpu(sb->layout));
484 printf(" Layout : %s\n", c?c:"-unknown-");
485 }
486 if (__le32_to_cpu(sb->level) == 10) {
487 int lo = __le32_to_cpu(sb->layout);
488 printf(" Layout :");
489 print_r10_layout(lo);
490 printf("\n");
491 }
492 switch(__le32_to_cpu(sb->level)) {
493 case 0:
494 case 4:
495 case 5:
496 case 6:
497 case 10:
498 printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2);
499 break;
500 case -1:
501 printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2);
502 break;
503 default: break;
504 }
505 printf("\n");
506 #if 0
507 /* This turns out to just be confusing */
508 printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number));
509 for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--)
510 if (__le16_to_cpu(sb->dev_roles[i-1]) != MD_DISK_ROLE_SPARE)
511 break;
512 for (d=0; d < i; d++) {
513 int role = __le16_to_cpu(sb->dev_roles[d]);
514 if (d) printf(", ");
515 if (role == MD_DISK_ROLE_SPARE) printf("empty");
516 else if(role == MD_DISK_ROLE_FAULTY) printf("failed");
517 else printf("%d", role);
518 }
519 printf(")\n");
520 #endif
521 printf(" Device Role : ");
522 role = role_from_sb(sb);
523 if (role >= MD_DISK_ROLE_FAULTY)
524 printf("spare\n");
525 else if (role == MD_DISK_ROLE_JOURNAL)
526 printf("Journal\n");
527 else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_REPLACEMENT))
528 printf("Replacement device %d\n", role);
529 else
530 printf("Active device %d\n", role);
531
532 printf(" Array State : ");
533 for (d=0; d<__le32_to_cpu(sb->raid_disks) + delta_extra; d++) {
534 int cnt = 0;
535 unsigned int i;
536 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
537 unsigned int role = __le16_to_cpu(sb->dev_roles[i]);
538 if (role == d)
539 cnt++;
540 }
541 if (cnt == 2)
542 printf("R");
543 else if (cnt == 1)
544 printf("A");
545 else if (cnt == 0)
546 printf(".");
547 else
548 printf("?");
549 }
550 #if 0
551 /* This is confusing too */
552 faulty = 0;
553 for (i=0; i< __le32_to_cpu(sb->max_dev); i++) {
554 int role = __le16_to_cpu(sb->dev_roles[i]);
555 if (role == MD_DISK_ROLE_FAULTY)
556 faulty++;
557 }
558 if (faulty) printf(" %d failed", faulty);
559 #endif
560 printf(" ('A' == active, '.' == missing, 'R' == replacing)");
561 printf("\n");
562 }
563
564 static void brief_examine_super1(struct supertype *st, int verbose)
565 {
566 struct mdp_superblock_1 *sb = st->sb;
567 int i;
568 unsigned long long sb_offset;
569 char *nm;
570 char *c=map_num(pers, __le32_to_cpu(sb->level));
571
572 nm = strchr(sb->set_name, ':');
573 if (nm)
574 nm++;
575 else if (sb->set_name[0])
576 nm = sb->set_name;
577 else
578 nm = NULL;
579
580 printf("ARRAY ");
581 if (nm) {
582 printf("/dev/md/");
583 print_escape(nm);
584 putchar(' ');
585 }
586 if (verbose && c)
587 printf(" level=%s", c);
588 sb_offset = __le64_to_cpu(sb->super_offset);
589 if (sb_offset <= 4)
590 printf(" metadata=1.1 ");
591 else if (sb_offset <= 8)
592 printf(" metadata=1.2 ");
593 else
594 printf(" metadata=1.0 ");
595 if (verbose)
596 printf("num-devices=%d ", __le32_to_cpu(sb->raid_disks));
597 printf("UUID=");
598 for (i=0; i<16; i++) {
599 if ((i&3)==0 && i != 0) printf(":");
600 printf("%02x", sb->set_uuid[i]);
601 }
602 if (sb->set_name[0]) {
603 printf(" name=");
604 print_quoted(sb->set_name);
605 }
606 printf("\n");
607 }
608
609 static void export_examine_super1(struct supertype *st)
610 {
611 struct mdp_superblock_1 *sb = st->sb;
612 int i;
613 int len = 32;
614 int layout;
615
616 printf("MD_LEVEL=%s\n", map_num(pers, __le32_to_cpu(sb->level)));
617 printf("MD_DEVICES=%d\n", __le32_to_cpu(sb->raid_disks));
618 for (i=0; i<32; i++)
619 if (sb->set_name[i] == '\n' ||
620 sb->set_name[i] == '\0') {
621 len = i;
622 break;
623 }
624 if (len)
625 printf("MD_NAME=%.*s\n", len, sb->set_name);
626 if (__le32_to_cpu(sb->level) > 0) {
627 int ddsks = 0, ddsks_denom = 1;
628 switch(__le32_to_cpu(sb->level)) {
629 case 1: ddsks=1;break;
630 case 4:
631 case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break;
632 case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break;
633 case 10:
634 layout = __le32_to_cpu(sb->layout);
635 ddsks = __le32_to_cpu(sb->raid_disks);
636 ddsks_denom = (layout&255) * ((layout>>8)&255);
637 }
638 if (ddsks) {
639 long long asize = __le64_to_cpu(sb->size);
640 asize = (asize << 9) * ddsks / ddsks_denom;
641 printf("MD_ARRAY_SIZE=%s\n",human_size_brief(asize,JEDEC));
642 }
643 }
644 printf("MD_UUID=");
645 for (i=0; i<16; i++) {
646 if ((i&3)==0 && i != 0) printf(":");
647 printf("%02x", sb->set_uuid[i]);
648 }
649 printf("\n");
650 printf("MD_UPDATE_TIME=%llu\n",
651 __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL);
652 printf("MD_DEV_UUID=");
653 for (i=0; i<16; i++) {
654 if ((i&3)==0 && i != 0) printf(":");
655 printf("%02x", sb->device_uuid[i]);
656 }
657 printf("\n");
658 printf("MD_EVENTS=%llu\n",
659 (unsigned long long)__le64_to_cpu(sb->events));
660 }
661
662 static int copy_metadata1(struct supertype *st, int from, int to)
663 {
664 /* Read superblock. If it looks good, write it out.
665 * Then if a bitmap is present, copy that.
666 * And if a bad-block-list is present, copy that too.
667 */
668 void *buf;
669 unsigned long long dsize, sb_offset;
670 const int bufsize = 4*1024;
671 struct mdp_superblock_1 super, *sb;
672
673 if (posix_memalign(&buf, 4096, bufsize) != 0)
674 return 1;
675
676 if (!get_dev_size(from, NULL, &dsize))
677 goto err;
678
679 dsize >>= 9;
680 if (dsize < 24)
681 goto err;
682 switch(st->minor_version) {
683 case 0:
684 sb_offset = dsize;
685 sb_offset -= 8*2;
686 sb_offset &= ~(4*2-1);
687 break;
688 case 1:
689 sb_offset = 0;
690 break;
691 case 2:
692 sb_offset = 4*2;
693 break;
694 default:
695 goto err;
696 }
697
698 if (lseek64(from, sb_offset << 9, 0) < 0LL)
699 goto err;
700 if (read(from, buf, bufsize) != bufsize)
701 goto err;
702
703 sb = buf;
704 super = *sb; // save most of sb for when we reuse buf
705
706 if (__le32_to_cpu(super.magic) != MD_SB_MAGIC ||
707 __le32_to_cpu(super.major_version) != 1 ||
708 __le64_to_cpu(super.super_offset) != sb_offset ||
709 calc_sb_1_csum(sb) != super.sb_csum)
710 goto err;
711
712 if (lseek64(to, sb_offset << 9, 0) < 0LL)
713 goto err;
714 if (write(to, buf, bufsize) != bufsize)
715 goto err;
716
717 if (super.feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
718 unsigned long long bitmap_offset = sb_offset;
719 int bytes = 4096; // just an estimate.
720 int written = 0;
721 struct align_fd afrom, ato;
722
723 init_afd(&afrom, from);
724 init_afd(&ato, to);
725
726 bitmap_offset += (int32_t)__le32_to_cpu(super.bitmap_offset);
727
728 if (lseek64(from, bitmap_offset<<9, 0) < 0)
729 goto err;
730 if (lseek64(to, bitmap_offset<<9, 0) < 0)
731 goto err;
732
733 for (written = 0; written < bytes ; ) {
734 int n = bytes - written;
735 if (n > 4096)
736 n = 4096;
737 if (aread(&afrom, buf, n) != n)
738 goto err;
739 if (written == 0) {
740 /* have the header, can calculate
741 * correct bitmap bytes */
742 bitmap_super_t *bms;
743 bms = (void*)buf;
744 bytes = calc_bitmap_size(bms, 512);
745 if (n > bytes)
746 n = bytes;
747 }
748 if (awrite(&ato, buf, n) != n)
749 goto err;
750 written += n;
751 }
752 }
753
754 if (super.bblog_size != 0 &&
755 __le16_to_cpu(super.bblog_size) <= 100 &&
756 super.bblog_offset != 0 &&
757 (super.feature_map & __le32_to_cpu(MD_FEATURE_BAD_BLOCKS))) {
758 /* There is a bad block log */
759 unsigned long long bb_offset = sb_offset;
760 int bytes = __le16_to_cpu(super.bblog_size) * 512;
761 int written = 0;
762 struct align_fd afrom, ato;
763
764 init_afd(&afrom, from);
765 init_afd(&ato, to);
766
767 bb_offset += (int32_t)__le32_to_cpu(super.bblog_offset);
768
769 if (lseek64(from, bb_offset<<9, 0) < 0)
770 goto err;
771 if (lseek64(to, bb_offset<<9, 0) < 0)
772 goto err;
773
774 for (written = 0; written < bytes ; ) {
775 int n = bytes - written;
776 if (n > 4096)
777 n = 4096;
778 if (aread(&afrom, buf, n) != n)
779 goto err;
780
781 if (awrite(&ato, buf, n) != n)
782 goto err;
783 written += n;
784 }
785 }
786
787 free(buf);
788 return 0;
789
790 err:
791 free(buf);
792 return 1;
793 }
794
795 static void detail_super1(struct supertype *st, char *homehost)
796 {
797 struct mdp_superblock_1 *sb = st->sb;
798 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
799 int i;
800 int l = homehost ? strlen(homehost) : 0;
801
802 printf(" Name : %.32s", sb->set_name);
803 if (l > 0 && l < 32 &&
804 sb->set_name[l] == ':' &&
805 strncmp(sb->set_name, homehost, l) == 0)
806 printf(" (local to host %s)", homehost);
807 if (bms->nodes > 0 && (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
808 printf("\n Cluster Name : %-64s", bms->cluster_name);
809 printf("\n UUID : ");
810 for (i=0; i<16; i++) {
811 if ((i&3)==0 && i != 0) printf(":");
812 printf("%02x", sb->set_uuid[i]);
813 }
814 printf("\n Events : %llu\n\n",
815 (unsigned long long)__le64_to_cpu(sb->events));
816 }
817
818 static void brief_detail_super1(struct supertype *st)
819 {
820 struct mdp_superblock_1 *sb = st->sb;
821 int i;
822
823 if (sb->set_name[0]) {
824 printf(" name=");
825 print_quoted(sb->set_name);
826 }
827 printf(" UUID=");
828 for (i=0; i<16; i++) {
829 if ((i&3)==0 && i != 0) printf(":");
830 printf("%02x", sb->set_uuid[i]);
831 }
832 }
833
834 static void export_detail_super1(struct supertype *st)
835 {
836 struct mdp_superblock_1 *sb = st->sb;
837 int i;
838 int len = 32;
839
840 for (i=0; i<32; i++)
841 if (sb->set_name[i] == '\n' ||
842 sb->set_name[i] == '\0') {
843 len = i;
844 break;
845 }
846 if (len)
847 printf("MD_NAME=%.*s\n", len, sb->set_name);
848 }
849
850 static int examine_badblocks_super1(struct supertype *st, int fd, char *devname)
851 {
852 struct mdp_superblock_1 *sb = st->sb;
853 unsigned long long offset;
854 int size;
855 __u64 *bbl, *bbp;
856 int i;
857
858 if (!sb->bblog_size || __le16_to_cpu(sb->bblog_size) > 100
859 || !sb->bblog_offset){
860 printf("No bad-blocks list configured on %s\n", devname);
861 return 0;
862 }
863 if ((sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
864 == 0) {
865 printf("Bad-blocks list is empty in %s\n", devname);
866 return 0;
867 }
868
869 size = __le16_to_cpu(sb->bblog_size)* 512;
870 if (posix_memalign((void**)&bbl, 4096, size) != 0) {
871 pr_err("could not allocate badblocks list\n");
872 return 0;
873 }
874 offset = __le64_to_cpu(sb->super_offset) +
875 (int)__le32_to_cpu(sb->bblog_offset);
876 offset <<= 9;
877 if (lseek64(fd, offset, 0) < 0) {
878 pr_err("Cannot seek to bad-blocks list\n");
879 return 1;
880 }
881 if (read(fd, bbl, size) != size) {
882 pr_err("Cannot read bad-blocks list\n");
883 return 1;
884 }
885 /* 64bits per entry. 10 bits is block-count, 54 bits is block
886 * offset. Blocks are sectors unless bblog->shift makes them bigger
887 */
888 bbp = (__u64*)bbl;
889 printf("Bad-blocks on %s:\n", devname);
890 for (i = 0; i < size/8; i++, bbp++) {
891 __u64 bb = __le64_to_cpu(*bbp);
892 int count = bb & 0x3ff;
893 unsigned long long sector = bb >> 10;
894
895 if (bb + 1 == 0)
896 break;
897
898 sector <<= sb->bblog_shift;
899 count <<= sb->bblog_shift;
900
901 printf("%20llu for %d sectors\n", sector, count);
902 }
903 return 0;
904 }
905
906 #endif
907
908 static int match_home1(struct supertype *st, char *homehost)
909 {
910 struct mdp_superblock_1 *sb = st->sb;
911 int l = homehost ? strlen(homehost) : 0;
912
913 return (l > 0 && l < 32 &&
914 sb->set_name[l] == ':' &&
915 strncmp(sb->set_name, homehost, l) == 0);
916 }
917
918 static void uuid_from_super1(struct supertype *st, int uuid[4])
919 {
920 struct mdp_superblock_1 *super = st->sb;
921 char *cuuid = (char*)uuid;
922 int i;
923 for (i=0; i<16; i++)
924 cuuid[i] = super->set_uuid[i];
925 }
926
927 static void getinfo_super1(struct supertype *st, struct mdinfo *info, char *map)
928 {
929 struct mdp_superblock_1 *sb = st->sb;
930 struct bitmap_super_s *bsb = (void*)(((char*)sb)+MAX_SB_SIZE);
931 struct misc_dev_info *misc = (void*)(((char*)sb)+MAX_SB_SIZE+BM_SUPER_SIZE);
932 int working = 0;
933 unsigned int i;
934 unsigned int role;
935 unsigned int map_disks = info->array.raid_disks;
936 unsigned long long super_offset;
937 unsigned long long data_size;
938
939 memset(info, 0, sizeof(*info));
940 info->array.major_version = 1;
941 info->array.minor_version = st->minor_version;
942 info->array.patch_version = 0;
943 info->array.raid_disks = __le32_to_cpu(sb->raid_disks);
944 info->array.level = __le32_to_cpu(sb->level);
945 info->array.layout = __le32_to_cpu(sb->layout);
946 info->array.md_minor = -1;
947 info->array.ctime = __le64_to_cpu(sb->ctime);
948 info->array.utime = __le64_to_cpu(sb->utime);
949 info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512;
950 info->array.state =
951 (__le64_to_cpu(sb->resync_offset) == MaxSector)
952 ? 1 : 0;
953 if (__le32_to_cpu(bsb->nodes) > 1)
954 info->array.state |= (1 << MD_SB_CLUSTERED);
955
956 super_offset = __le64_to_cpu(sb->super_offset);
957 info->data_offset = __le64_to_cpu(sb->data_offset);
958 info->component_size = __le64_to_cpu(sb->size);
959 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
960 info->bitmap_offset = (int32_t)__le32_to_cpu(sb->bitmap_offset);
961 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_PPL)) {
962 info->ppl_offset = __le16_to_cpu(sb->ppl.offset);
963 info->ppl_size = __le16_to_cpu(sb->ppl.size);
964 info->ppl_sector = super_offset + info->ppl_offset;
965 }
966
967 info->disk.major = 0;
968 info->disk.minor = 0;
969 info->disk.number = __le32_to_cpu(sb->dev_number);
970 if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) ||
971 __le32_to_cpu(sb->dev_number) >= MAX_DEVS)
972 role = MD_DISK_ROLE_FAULTY;
973 else
974 role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]);
975
976 if (info->array.level <= 0)
977 data_size = __le64_to_cpu(sb->data_size);
978 else
979 data_size = __le64_to_cpu(sb->size);
980 if (info->data_offset < super_offset) {
981 unsigned long long end;
982 info->space_before = info->data_offset;
983 end = super_offset;
984
985 if (sb->bblog_offset && sb->bblog_size) {
986 unsigned long long bboffset = super_offset;
987 bboffset += (int32_t)__le32_to_cpu(sb->bblog_offset);
988 if (bboffset < end)
989 end = bboffset;
990 }
991
992 if (super_offset + info->bitmap_offset + info->ppl_offset < end)
993 end = super_offset + info->bitmap_offset + info->ppl_offset;
994
995 if (info->data_offset + data_size < end)
996 info->space_after = end - data_size - info->data_offset;
997 else
998 info->space_after = 0;
999 } else {
1000 unsigned long long earliest;
1001 earliest = super_offset + (32+4)*2; /* match kernel */
1002 if (info->bitmap_offset > 0) {
1003 unsigned long long bmend = info->bitmap_offset;
1004 unsigned long long size = calc_bitmap_size(bsb, 4096);
1005 size /= 512;
1006 bmend += size;
1007 if (bmend > earliest)
1008 earliest = bmend;
1009 } else if (info->ppl_offset > 0) {
1010 unsigned long long pplend = info->ppl_offset +
1011 info->ppl_size;
1012 if (pplend > earliest)
1013 earliest = pplend;
1014 }
1015 if (sb->bblog_offset && sb->bblog_size) {
1016 unsigned long long bbend = super_offset;
1017 bbend += (int32_t)__le32_to_cpu(sb->bblog_offset);
1018 bbend += __le16_to_cpu(sb->bblog_size);
1019 if (bbend > earliest)
1020 earliest = bbend;
1021 }
1022 if (earliest < info->data_offset)
1023 info->space_before = info->data_offset - earliest;
1024 else
1025 info->space_before = 0;
1026 info->space_after = misc->device_size - data_size - info->data_offset;
1027 }
1028 if (info->space_before == 0 && info->space_after == 0) {
1029 /* It will look like we don't support data_offset changes,
1030 * be we do - it's just that there is no room.
1031 * A change that reduced the number of devices should
1032 * still be allowed, so set the otherwise useless value of '1'
1033 */
1034 info->space_after = 1;
1035 }
1036
1037 info->disk.raid_disk = -1;
1038 switch(role) {
1039 case MD_DISK_ROLE_SPARE:
1040 info->disk.state = 0; /* spare: not active, not sync, not faulty */
1041 break;
1042 case MD_DISK_ROLE_FAULTY:
1043 info->disk.state = 1; /* faulty */
1044 break;
1045 case MD_DISK_ROLE_JOURNAL:
1046 info->disk.state = (1 << MD_DISK_JOURNAL);
1047 info->disk.raid_disk = role;
1048 info->space_after = (misc->device_size - info->data_offset) % 8; /* journal uses all 4kB blocks*/
1049 break;
1050 default:
1051 info->disk.state = 6; /* active and in sync */
1052 info->disk.raid_disk = role;
1053 }
1054 if (sb->devflags & WriteMostly1)
1055 info->disk.state |= (1 << MD_DISK_WRITEMOSTLY);
1056 if (sb->devflags & FailFast1)
1057 info->disk.state |= (1 << MD_DISK_FAILFAST);
1058 info->events = __le64_to_cpu(sb->events);
1059 sprintf(info->text_version, "1.%d", st->minor_version);
1060 info->safe_mode_delay = 200;
1061
1062 memcpy(info->uuid, sb->set_uuid, 16);
1063
1064 strncpy(info->name, sb->set_name, 32);
1065 info->name[32] = 0;
1066
1067 if ((__le32_to_cpu(sb->feature_map)&MD_FEATURE_REPLACEMENT)) {
1068 info->disk.state &= ~(1 << MD_DISK_SYNC);
1069 info->disk.state |= 1 << MD_DISK_REPLACEMENT;
1070 }
1071
1072 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RECOVERY_OFFSET))
1073 info->recovery_start = __le32_to_cpu(sb->recovery_offset);
1074 else
1075 info->recovery_start = MaxSector;
1076
1077 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) {
1078 info->reshape_active = 1;
1079 if ((sb->feature_map & __le32_to_cpu(MD_FEATURE_NEW_OFFSET)) &&
1080 sb->new_offset != 0)
1081 info->reshape_active |= RESHAPE_NO_BACKUP;
1082 info->reshape_progress = __le64_to_cpu(sb->reshape_position);
1083 info->new_level = __le32_to_cpu(sb->new_level);
1084 info->delta_disks = __le32_to_cpu(sb->delta_disks);
1085 info->new_layout = __le32_to_cpu(sb->new_layout);
1086 info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9;
1087 if (info->delta_disks < 0)
1088 info->array.raid_disks -= info->delta_disks;
1089 } else
1090 info->reshape_active = 0;
1091
1092 info->recovery_blocked = info->reshape_active;
1093
1094 if (map)
1095 for (i=0; i<map_disks; i++)
1096 map[i] = 0;
1097 for (i = 0; i < __le32_to_cpu(sb->max_dev); i++) {
1098 role = __le16_to_cpu(sb->dev_roles[i]);
1099 if (/*role == MD_DISK_ROLE_SPARE || */role < (unsigned) info->array.raid_disks) {
1100 working++;
1101 if (map && role < map_disks)
1102 map[role] = 1;
1103 }
1104 }
1105
1106 info->array.working_disks = working;
1107
1108 if (sb->feature_map & __le32_to_cpu(MD_FEATURE_JOURNAL)) {
1109 info->journal_device_required = 1;
1110 info->consistency_policy = CONSISTENCY_POLICY_JOURNAL;
1111 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_PPL)) {
1112 info->consistency_policy = CONSISTENCY_POLICY_PPL;
1113 } else if (sb->feature_map & __le32_to_cpu(MD_FEATURE_BITMAP_OFFSET)) {
1114 info->consistency_policy = CONSISTENCY_POLICY_BITMAP;
1115 } else if (info->array.level <= 0) {
1116 info->consistency_policy = CONSISTENCY_POLICY_NONE;
1117 } else {
1118 info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
1119 }
1120
1121 info->journal_clean = 0;
1122 }
1123
1124 static struct mdinfo *container_content1(struct supertype *st, char *subarray)
1125 {
1126 struct mdinfo *info;
1127
1128 if (subarray)
1129 return NULL;
1130
1131 info = xmalloc(sizeof(*info));
1132 getinfo_super1(st, info, NULL);
1133 return info;
1134 }
1135
1136 static int update_super1(struct supertype *st, struct mdinfo *info,
1137 char *update,
1138 char *devname, int verbose,
1139 int uuid_set, char *homehost)
1140 {
1141 /* NOTE: for 'assemble' and 'force' we need to return non-zero
1142 * if any change was made. For others, the return value is
1143 * ignored.
1144 */
1145 int rv = 0;
1146 int lockid;
1147 struct mdp_superblock_1 *sb = st->sb;
1148 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1149
1150 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1151 rv = cluster_get_dlmlock(&lockid);
1152 if (rv) {
1153 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1154 cluster_release_dlmlock(lockid);
1155 return rv;
1156 }
1157 }
1158
1159 if (strcmp(update, "homehost") == 0 &&
1160 homehost) {
1161 /* Note that 'homehost' is special as it is really
1162 * a "name" update.
1163 */
1164 char *c;
1165 update = "name";
1166 c = strchr(sb->set_name, ':');
1167 if (c)
1168 strncpy(info->name, c+1, 31 - (c-sb->set_name));
1169 else
1170 strncpy(info->name, sb->set_name, 32);
1171 info->name[32] = 0;
1172 }
1173
1174 if (strcmp(update, "force-one")==0) {
1175 /* Not enough devices for a working array,
1176 * so bring this one up-to-date
1177 */
1178 if (sb->events != __cpu_to_le64(info->events))
1179 rv = 1;
1180 sb->events = __cpu_to_le64(info->events);
1181 } else if (strcmp(update, "force-array")==0) {
1182 /* Degraded array and 'force' requests to
1183 * maybe need to mark it 'clean'.
1184 */
1185 switch(__le32_to_cpu(sb->level)) {
1186 case 5: case 4: case 6:
1187 /* need to force clean */
1188 if (sb->resync_offset != MaxSector)
1189 rv = 1;
1190 sb->resync_offset = MaxSector;
1191 }
1192 } else if (strcmp(update, "assemble")==0) {
1193 int d = info->disk.number;
1194 int want;
1195 if (info->disk.state & (1<<MD_DISK_ACTIVE))
1196 want = info->disk.raid_disk;
1197 else if (info->disk.state & (1<<MD_DISK_JOURNAL))
1198 want = MD_DISK_ROLE_JOURNAL;
1199 else
1200 want = MD_DISK_ROLE_SPARE;
1201 if (sb->dev_roles[d] != __cpu_to_le16(want)) {
1202 sb->dev_roles[d] = __cpu_to_le16(want);
1203 rv = 1;
1204 }
1205 if (info->reshape_active &&
1206 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1207 info->delta_disks >= 0 &&
1208 info->reshape_progress < __le64_to_cpu(sb->reshape_position)) {
1209 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1210 rv = 1;
1211 }
1212 if (info->reshape_active &&
1213 sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE) &&
1214 info->delta_disks < 0 &&
1215 info->reshape_progress > __le64_to_cpu(sb->reshape_position)) {
1216 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1217 rv = 1;
1218 }
1219 } else if (strcmp(update, "linear-grow-new") == 0) {
1220 unsigned int i;
1221 int fd;
1222 unsigned int max = __le32_to_cpu(sb->max_dev);
1223
1224 for (i=0 ; i < max ; i++)
1225 if (__le16_to_cpu(sb->dev_roles[i]) >= MD_DISK_ROLE_FAULTY)
1226 break;
1227 sb->dev_number = __cpu_to_le32(i);
1228 info->disk.number = i;
1229 if (max >= __le32_to_cpu(sb->max_dev))
1230 sb->max_dev = __cpu_to_le32(max+1);
1231
1232 random_uuid(sb->device_uuid);
1233
1234 sb->dev_roles[i] =
1235 __cpu_to_le16(info->disk.raid_disk);
1236
1237 fd = open(devname, O_RDONLY);
1238 if (fd >= 0) {
1239 unsigned long long ds;
1240 get_dev_size(fd, devname, &ds);
1241 close(fd);
1242 ds >>= 9;
1243 if (__le64_to_cpu(sb->super_offset) <
1244 __le64_to_cpu(sb->data_offset)) {
1245 sb->data_size = __cpu_to_le64(
1246 ds - __le64_to_cpu(sb->data_offset));
1247 } else {
1248 ds -= 8*2;
1249 ds &= ~(unsigned long long)(4*2-1);
1250 sb->super_offset = __cpu_to_le64(ds);
1251 sb->data_size = __cpu_to_le64(
1252 ds - __le64_to_cpu(sb->data_offset));
1253 }
1254 }
1255 } else if (strcmp(update, "linear-grow-update") == 0) {
1256 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
1257 sb->dev_roles[info->disk.number] =
1258 __cpu_to_le16(info->disk.raid_disk);
1259 } else if (strcmp(update, "resync") == 0) {
1260 /* make sure resync happens */
1261 sb->resync_offset = 0ULL;
1262 } else if (strcmp(update, "uuid") == 0) {
1263 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
1264
1265 if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)
1266 memcpy(bms->uuid, sb->set_uuid, 16);
1267 } else if (strcmp(update, "no-bitmap") == 0) {
1268 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
1269 } else if (strcmp(update, "bbl") == 0) {
1270 /* only possible if there is room after the bitmap, or if
1271 * there is no bitmap
1272 */
1273 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1274 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
1275 long bitmap_offset = 0;
1276 long bm_sectors = 0;
1277 long space;
1278
1279 #ifndef MDASSEMBLE
1280 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1281 bitmap_offset = (long)__le32_to_cpu(sb->bitmap_offset);
1282 bm_sectors = calc_bitmap_size(bms, 4096) >> 9;
1283 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
1284 bitmap_offset = (long)__le16_to_cpu(sb->ppl.offset);
1285 bm_sectors = (long)__le16_to_cpu(sb->ppl.size);
1286 }
1287 #endif
1288 if (sb_offset < data_offset) {
1289 /* 1.1 or 1.2. Put bbl after bitmap leaving at least 32K
1290 */
1291 long bb_offset;
1292 bb_offset = sb_offset + 8;
1293 if (bm_sectors && bitmap_offset > 0)
1294 bb_offset = bitmap_offset + bm_sectors;
1295 while (bb_offset < (long)sb_offset + 8 + 32*2
1296 && bb_offset + 8+8 <= (long)data_offset)
1297 /* too close to bitmap, and room to grow */
1298 bb_offset += 8;
1299 if (bb_offset + 8 <= (long)data_offset) {
1300 sb->bblog_size = __cpu_to_le16(8);
1301 sb->bblog_offset = __cpu_to_le32(bb_offset);
1302 }
1303 } else {
1304 /* 1.0 - Put bbl just before super block */
1305 if (bm_sectors && bitmap_offset < 0)
1306 space = -bitmap_offset - bm_sectors;
1307 else
1308 space = sb_offset - data_offset -
1309 __le64_to_cpu(sb->data_size);
1310 if (space >= 8) {
1311 sb->bblog_size = __cpu_to_le16(8);
1312 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1313 }
1314 }
1315 } else if (strcmp(update, "no-bbl") == 0) {
1316 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BAD_BLOCKS))
1317 pr_err("Cannot remove active bbl from %s\n",devname);
1318 else {
1319 sb->bblog_size = 0;
1320 sb->bblog_shift = 0;
1321 sb->bblog_offset = 0;
1322 }
1323 } else if (strcmp(update, "force-no-bbl") == 0) {
1324 sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1325 sb->bblog_size = 0;
1326 sb->bblog_shift = 0;
1327 sb->bblog_offset = 0;
1328 } else if (strcmp(update, "ppl") == 0) {
1329 unsigned long long sb_offset = __le64_to_cpu(sb->super_offset);
1330 unsigned long long data_offset = __le64_to_cpu(sb->data_offset);
1331 unsigned long long data_size = __le64_to_cpu(sb->data_size);
1332 long bb_offset = __le32_to_cpu(sb->bblog_offset);
1333 int space;
1334 int optimal_space;
1335 int offset;
1336
1337 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1338 pr_err("Cannot add PPL to array with bitmap\n");
1339 return -2;
1340 }
1341
1342 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_JOURNAL)) {
1343 pr_err("Cannot add PPL to array with journal\n");
1344 return -2;
1345 }
1346
1347 if (sb_offset < data_offset) {
1348 if (bb_offset)
1349 space = bb_offset - 8;
1350 else
1351 space = data_offset - sb_offset - 8;
1352 offset = 8;
1353 } else {
1354 offset = -(sb_offset - data_offset - data_size);
1355 if (offset < INT16_MIN)
1356 offset = INT16_MIN;
1357 space = -(offset - bb_offset);
1358 }
1359
1360 if (space < (PPL_HEADER_SIZE >> 9) + 8) {
1361 pr_err("Not enough space to add ppl\n");
1362 return -2;
1363 }
1364
1365 optimal_space = choose_ppl_space(__le32_to_cpu(sb->chunksize));
1366
1367 if (space > optimal_space)
1368 space = optimal_space;
1369 if (space > UINT16_MAX)
1370 space = UINT16_MAX;
1371
1372 sb->ppl.offset = __cpu_to_le16(offset);
1373 sb->ppl.size = __cpu_to_le16(space);
1374 sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
1375 } else if (strcmp(update, "no-ppl") == 0) {
1376 sb->feature_map &= ~ __cpu_to_le32(MD_FEATURE_PPL);
1377 } else if (strcmp(update, "name") == 0) {
1378 if (info->name[0] == 0)
1379 sprintf(info->name, "%d", info->array.md_minor);
1380 memset(sb->set_name, 0, sizeof(sb->set_name));
1381 if (homehost &&
1382 strchr(info->name, ':') == NULL &&
1383 strlen(homehost)+1+strlen(info->name) < 32) {
1384 strcpy(sb->set_name, homehost);
1385 strcat(sb->set_name, ":");
1386 strcat(sb->set_name, info->name);
1387 } else
1388 strncpy(sb->set_name, info->name, sizeof(sb->set_name));
1389 } else if (strcmp(update, "devicesize") == 0 &&
1390 __le64_to_cpu(sb->super_offset) <
1391 __le64_to_cpu(sb->data_offset)) {
1392 /* set data_size to device size less data_offset */
1393 struct misc_dev_info *misc = (struct misc_dev_info*)
1394 (st->sb + MAX_SB_SIZE + BM_SUPER_SIZE);
1395 sb->data_size = __cpu_to_le64(
1396 misc->device_size - __le64_to_cpu(sb->data_offset));
1397 } else if (strncmp(update, "revert-reshape", 14) == 0) {
1398 rv = -2;
1399 if (!(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE)))
1400 pr_err("No active reshape to revert on %s\n",
1401 devname);
1402 else {
1403 __u32 temp;
1404 unsigned long long reshape_sectors;
1405 long reshape_chunk;
1406 rv = 0;
1407 /* If the reshape hasn't started, just stop it.
1408 * It is conceivable that a stripe was modified but
1409 * the metadata not updated. In that case the backup
1410 * should have been used to get passed the critical stage.
1411 * If that couldn't happen, the "-nobackup" version
1412 * will be used.
1413 */
1414 if (strcmp(update, "revert-reshape-nobackup") == 0 &&
1415 sb->reshape_position == 0 &&
1416 (__le32_to_cpu(sb->delta_disks) > 0 ||
1417 (__le32_to_cpu(sb->delta_disks) == 0 &&
1418 !(sb->feature_map & __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS))))) {
1419 sb->feature_map &= ~__cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1420 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
1421 __le32_to_cpu(sb->delta_disks));
1422 sb->delta_disks = 0;
1423 goto done;
1424 }
1425 /* reshape_position is a little messy.
1426 * Its value must be a multiple of the larger
1427 * chunk size, and of the "after" data disks.
1428 * So when reverting we need to change it to
1429 * be a multiple of the new "after" data disks,
1430 * which is the old "before".
1431 * If it isn't already a multiple of 'before',
1432 * the only thing we could do would be
1433 * copy some block around on the disks, which
1434 * is easy to get wrong.
1435 * So we reject a revert-reshape unless the
1436 * alignment is good.
1437 */
1438 if (__le32_to_cpu(sb->level) >= 4 &&
1439 __le32_to_cpu(sb->level) <= 6) {
1440 reshape_sectors = __le64_to_cpu(sb->reshape_position);
1441 reshape_chunk = __le32_to_cpu(sb->new_chunk);
1442 reshape_chunk *= __le32_to_cpu(sb->raid_disks) - __le32_to_cpu(sb->delta_disks) -
1443 (__le32_to_cpu(sb->level)==6 ? 2 : 1);
1444 if (reshape_sectors % reshape_chunk) {
1445 pr_err("Reshape position is not suitably aligned.\n");
1446 pr_err("Try normal assembly and stop again\n");
1447 return -2;
1448 }
1449 }
1450 sb->raid_disks = __cpu_to_le32(__le32_to_cpu(sb->raid_disks) -
1451 __le32_to_cpu(sb->delta_disks));
1452 if (sb->delta_disks == 0)
1453 sb->feature_map ^= __cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS);
1454 else
1455 sb->delta_disks = __cpu_to_le32(-__le32_to_cpu(sb->delta_disks));
1456
1457 temp = sb->new_layout;
1458 sb->new_layout = sb->layout;
1459 sb->layout = temp;
1460
1461 temp = sb->new_chunk;
1462 sb->new_chunk = sb->chunksize;
1463 sb->chunksize = temp;
1464
1465 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_NEW_OFFSET)) {
1466 long offset_delta = (int32_t)__le32_to_cpu(sb->new_offset);
1467 sb->data_offset = __cpu_to_le64(__le64_to_cpu(sb->data_offset) + offset_delta);
1468 sb->new_offset = __cpu_to_le32(-offset_delta);
1469 sb->data_size = __cpu_to_le64(__le64_to_cpu(sb->data_size) - offset_delta);
1470 }
1471 done:;
1472 }
1473 } else if (strcmp(update, "_reshape_progress")==0)
1474 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
1475 else if (strcmp(update, "writemostly")==0)
1476 sb->devflags |= WriteMostly1;
1477 else if (strcmp(update, "readwrite")==0)
1478 sb->devflags &= ~WriteMostly1;
1479 else if (strcmp(update, "failfast") == 0)
1480 sb->devflags |= FailFast1;
1481 else if (strcmp(update, "nofailfast") == 0)
1482 sb->devflags &= ~FailFast1;
1483 else
1484 rv = -1;
1485
1486 sb->sb_csum = calc_sb_1_csum(sb);
1487 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1488 cluster_release_dlmlock(lockid);
1489
1490 return rv;
1491 }
1492
1493 static int init_super1(struct supertype *st, mdu_array_info_t *info,
1494 struct shape *s, char *name, char *homehost,
1495 int *uuid, unsigned long long data_offset)
1496 {
1497 struct mdp_superblock_1 *sb;
1498 int spares;
1499 char defname[10];
1500 int sbsize;
1501
1502 if (posix_memalign((void**)&sb, 4096, SUPER1_SIZE) != 0) {
1503 pr_err("could not allocate superblock\n");
1504 return 0;
1505 }
1506 memset(sb, 0, SUPER1_SIZE);
1507
1508 st->sb = sb;
1509 if (info == NULL) {
1510 /* zeroing superblock */
1511 return 0;
1512 }
1513
1514 spares = info->working_disks - info->active_disks;
1515 if (info->raid_disks + spares > MAX_DEVS) {
1516 pr_err("too many devices requested: %d+%d > %d\n",
1517 info->raid_disks , spares, MAX_DEVS);
1518 return 0;
1519 }
1520
1521 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
1522 sb->major_version = __cpu_to_le32(1);
1523 sb->feature_map = 0;
1524 sb->pad0 = 0;
1525
1526 if (uuid)
1527 copy_uuid(sb->set_uuid, uuid, super1.swapuuid);
1528 else
1529 random_uuid(sb->set_uuid);;
1530
1531 if (name == NULL || *name == 0) {
1532 sprintf(defname, "%d", info->md_minor);
1533 name = defname;
1534 }
1535 if (homehost &&
1536 strchr(name, ':')== NULL &&
1537 strlen(homehost)+1+strlen(name) < 32) {
1538 strcpy(sb->set_name, homehost);
1539 strcat(sb->set_name, ":");
1540 strcat(sb->set_name, name);
1541 } else
1542 strncpy(sb->set_name, name, sizeof(sb->set_name));
1543
1544 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
1545 sb->level = __cpu_to_le32(info->level);
1546 sb->layout = __cpu_to_le32(info->layout);
1547 sb->size = __cpu_to_le64(s->size*2ULL);
1548 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
1549 sb->raid_disks = __cpu_to_le32(info->raid_disks);
1550
1551 sb->data_offset = __cpu_to_le64(data_offset);
1552 sb->data_size = __cpu_to_le64(0);
1553 sb->super_offset = __cpu_to_le64(0);
1554 sb->recovery_offset = __cpu_to_le64(0);
1555
1556 sb->utime = sb->ctime;
1557 sb->events = __cpu_to_le64(1);
1558 if (info->state & (1<<MD_SB_CLEAN))
1559 sb->resync_offset = MaxSector;
1560 else
1561 sb->resync_offset = 0;
1562 sbsize = sizeof(struct mdp_superblock_1) + 2 * (info->raid_disks + spares);
1563 sbsize = ROUND_UP(sbsize, 512);
1564 sb->max_dev = __cpu_to_le32((sbsize - sizeof(struct mdp_superblock_1)) / 2);
1565
1566 memset(sb->dev_roles, 0xff, MAX_SB_SIZE - sizeof(struct mdp_superblock_1));
1567
1568 if (s->consistency_policy == CONSISTENCY_POLICY_PPL)
1569 sb->feature_map |= __cpu_to_le32(MD_FEATURE_PPL);
1570
1571 return 1;
1572 }
1573
1574 struct devinfo {
1575 int fd;
1576 char *devname;
1577 long long data_offset;
1578 mdu_disk_info_t disk;
1579 struct devinfo *next;
1580 };
1581 #ifndef MDASSEMBLE
1582 /* Add a device to the superblock being created */
1583 static int add_to_super1(struct supertype *st, mdu_disk_info_t *dk,
1584 int fd, char *devname, unsigned long long data_offset)
1585 {
1586 struct mdp_superblock_1 *sb = st->sb;
1587 __u16 *rp = sb->dev_roles + dk->number;
1588 struct devinfo *di, **dip;
1589 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1590 int rv, lockid;
1591 int dk_state;
1592
1593 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1594 rv = cluster_get_dlmlock(&lockid);
1595 if (rv) {
1596 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1597 cluster_release_dlmlock(lockid);
1598 return rv;
1599 }
1600 }
1601
1602 dk_state = dk->state & ~(1<<MD_DISK_FAILFAST);
1603 if ((dk_state & 6) == 6) /* active, sync */
1604 *rp = __cpu_to_le16(dk->raid_disk);
1605 else if (dk_state & (1<<MD_DISK_JOURNAL))
1606 *rp = MD_DISK_ROLE_JOURNAL;
1607 else if ((dk_state & ~2) == 0) /* active or idle -> spare */
1608 *rp = MD_DISK_ROLE_SPARE;
1609 else
1610 *rp = MD_DISK_ROLE_FAULTY;
1611
1612 if (dk->number >= (int)__le32_to_cpu(sb->max_dev) &&
1613 __le32_to_cpu(sb->max_dev) < MAX_DEVS)
1614 sb->max_dev = __cpu_to_le32(dk->number+1);
1615
1616 sb->dev_number = __cpu_to_le32(dk->number);
1617 sb->devflags = 0; /* don't copy another disks flags */
1618 sb->sb_csum = calc_sb_1_csum(sb);
1619
1620 dip = (struct devinfo **)&st->info;
1621 while (*dip)
1622 dip = &(*dip)->next;
1623 di = xmalloc(sizeof(struct devinfo));
1624 di->fd = fd;
1625 di->devname = devname;
1626 di->disk = *dk;
1627 di->data_offset = data_offset;
1628 di->next = NULL;
1629 *dip = di;
1630
1631 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1632 cluster_release_dlmlock(lockid);
1633
1634 return 0;
1635 }
1636 #endif
1637
1638 static int locate_bitmap1(struct supertype *st, int fd, int node_num);
1639
1640 static int store_super1(struct supertype *st, int fd)
1641 {
1642 struct mdp_superblock_1 *sb = st->sb;
1643 unsigned long long sb_offset;
1644 struct align_fd afd;
1645 int sbsize;
1646 unsigned long long dsize;
1647 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
1648 int rv, lockid;
1649
1650 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready()) {
1651 rv = cluster_get_dlmlock(&lockid);
1652 if (rv) {
1653 pr_err("Cannot get dlmlock in %s return %d\n", __func__, rv);
1654 cluster_release_dlmlock(lockid);
1655 return rv;
1656 }
1657 }
1658
1659 if (!get_dev_size(fd, NULL, &dsize))
1660 return 1;
1661
1662 dsize >>= 9;
1663
1664 if (dsize < 24)
1665 return 2;
1666
1667 init_afd(&afd, fd);
1668
1669 /*
1670 * Calculate the position of the superblock.
1671 * It is always aligned to a 4K boundary and
1672 * depending on minor_version, it can be:
1673 * 0: At least 8K, but less than 12K, from end of device
1674 * 1: At start of device
1675 * 2: 4K from start of device.
1676 */
1677 switch(st->minor_version) {
1678 case 0:
1679 sb_offset = dsize;
1680 sb_offset -= 8*2;
1681 sb_offset &= ~(4*2-1);
1682 break;
1683 case 1:
1684 sb_offset = 0;
1685 break;
1686 case 2:
1687 sb_offset = 4*2;
1688 break;
1689 default:
1690 return -EINVAL;
1691 }
1692
1693 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
1694 0 != __le64_to_cpu(sb->super_offset)
1695 ) {
1696 pr_err("internal error - sb_offset is wrong\n");
1697 abort();
1698 }
1699
1700 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
1701 return 3;
1702
1703 sbsize = ROUND_UP(sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev), 512);
1704
1705 if (awrite(&afd, sb, sbsize) != sbsize)
1706 return 4;
1707
1708 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1709 struct bitmap_super_s *bm = (struct bitmap_super_s*)
1710 (((char*)sb)+MAX_SB_SIZE);
1711 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
1712 locate_bitmap1(st, fd, 0);
1713 if (awrite(&afd, bm, sizeof(*bm)) != sizeof(*bm))
1714 return 5;
1715 }
1716 }
1717 fsync(fd);
1718 if (bms->version == BITMAP_MAJOR_CLUSTERED && dlm_funs_ready())
1719 cluster_release_dlmlock(lockid);
1720
1721 return 0;
1722 }
1723
1724 static int load_super1(struct supertype *st, int fd, char *devname);
1725
1726 static unsigned long choose_bm_space(unsigned long devsize)
1727 {
1728 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1729 * if bigger than 200Gig, save 128k
1730 * NOTE: result must be multiple of 4K else bad things happen
1731 * on 4K-sector devices.
1732 */
1733 if (devsize < 64*2)
1734 return 0;
1735 if (devsize - 64*2 >= 200*1024*1024*2)
1736 return 128*2;
1737 if (devsize - 4*2 > 8*1024*1024*2)
1738 return 64*2;
1739 return 4*2;
1740 }
1741
1742 static void free_super1(struct supertype *st);
1743
1744 #ifndef MDASSEMBLE
1745
1746 __u32 crc32c_le(__u32 crc, unsigned char const *p, size_t len);
1747
1748 static int write_init_ppl1(struct supertype *st, struct mdinfo *info, int fd)
1749 {
1750 struct mdp_superblock_1 *sb = st->sb;
1751 void *buf;
1752 struct ppl_header *ppl_hdr;
1753 int ret;
1754
1755 ret = posix_memalign(&buf, 4096, PPL_HEADER_SIZE);
1756 if (ret) {
1757 pr_err("Failed to allocate PPL header buffer\n");
1758 return ret;
1759 }
1760
1761 memset(buf, 0, PPL_HEADER_SIZE);
1762 ppl_hdr = buf;
1763 memset(ppl_hdr->reserved, 0xff, PPL_HDR_RESERVED);
1764 ppl_hdr->signature = __cpu_to_le32(~crc32c_le(~0, sb->set_uuid,
1765 sizeof(sb->set_uuid)));
1766 ppl_hdr->checksum = __cpu_to_le32(~crc32c_le(~0, buf, PPL_HEADER_SIZE));
1767
1768 if (lseek64(fd, info->ppl_sector * 512, SEEK_SET) < 0) {
1769 ret = errno;
1770 perror("Failed to seek to PPL header location");
1771 }
1772
1773 if (!ret && write(fd, buf, PPL_HEADER_SIZE) != PPL_HEADER_SIZE) {
1774 ret = errno;
1775 perror("Write PPL header failed");
1776 }
1777
1778 if (!ret)
1779 fsync(fd);
1780
1781 free(buf);
1782 return ret;
1783 }
1784
1785 #define META_BLOCK_SIZE 4096
1786
1787 static int write_empty_r5l_meta_block(struct supertype *st, int fd)
1788 {
1789 struct r5l_meta_block *mb;
1790 struct mdp_superblock_1 *sb = st->sb;
1791 struct align_fd afd;
1792 __u32 crc;
1793
1794 init_afd(&afd, fd);
1795
1796 if (posix_memalign((void**)&mb, 4096, META_BLOCK_SIZE) != 0) {
1797 pr_err("Could not allocate memory for the meta block.\n");
1798 return 1;
1799 }
1800
1801 memset(mb, 0, META_BLOCK_SIZE);
1802
1803 mb->magic = __cpu_to_le32(R5LOG_MAGIC);
1804 mb->version = R5LOG_VERSION;
1805 mb->meta_size = __cpu_to_le32(sizeof(struct r5l_meta_block));
1806 mb->seq = __cpu_to_le64(random32());
1807 mb->position = __cpu_to_le64(0);
1808
1809 crc = crc32c_le(0xffffffff, sb->set_uuid, sizeof(sb->set_uuid));
1810 crc = crc32c_le(crc, (void *)mb, META_BLOCK_SIZE);
1811 mb->checksum = crc;
1812
1813 if (lseek64(fd, __le64_to_cpu(sb->data_offset) * 512, 0) < 0LL) {
1814 pr_err("cannot seek to offset of the meta block\n");
1815 goto fail_to_write;
1816 }
1817
1818 if (awrite(&afd, mb, META_BLOCK_SIZE) != META_BLOCK_SIZE) {
1819 pr_err("failed to store write the meta block \n");
1820 goto fail_to_write;
1821 }
1822 fsync(fd);
1823
1824 free(mb);
1825 return 0;
1826
1827 fail_to_write:
1828 free(mb);
1829 return 1;
1830 }
1831
1832 static int write_init_super1(struct supertype *st)
1833 {
1834 struct mdp_superblock_1 *sb = st->sb;
1835 struct supertype *refst;
1836 int rv = 0;
1837 unsigned long long bm_space;
1838 struct devinfo *di;
1839 unsigned long long dsize, array_size;
1840 unsigned long long sb_offset;
1841 unsigned long long data_offset;
1842 long bm_offset;
1843
1844 for (di = st->info; di; di = di->next) {
1845 if (di->disk.state & (1 << MD_DISK_JOURNAL))
1846 sb->feature_map |= __cpu_to_le32(MD_FEATURE_JOURNAL);
1847 }
1848
1849 for (di = st->info; di; di = di->next) {
1850 if (di->disk.state & (1 << MD_DISK_FAULTY))
1851 continue;
1852 if (di->fd < 0)
1853 continue;
1854
1855 while (Kill(di->devname, NULL, 0, -1, 1) == 0)
1856 ;
1857
1858 sb->dev_number = __cpu_to_le32(di->disk.number);
1859 if (di->disk.state & (1<<MD_DISK_WRITEMOSTLY))
1860 sb->devflags |= WriteMostly1;
1861 else
1862 sb->devflags &= ~WriteMostly1;
1863 if (di->disk.state & (1<<MD_DISK_FAILFAST))
1864 sb->devflags |= FailFast1;
1865 else
1866 sb->devflags &= ~FailFast1;
1867
1868 random_uuid(sb->device_uuid);
1869
1870 if (!(di->disk.state & (1<<MD_DISK_JOURNAL)))
1871 sb->events = 0;
1872
1873 refst = dup_super(st);
1874 if (load_super1(refst, di->fd, NULL)==0) {
1875 struct mdp_superblock_1 *refsb = refst->sb;
1876
1877 memcpy(sb->device_uuid, refsb->device_uuid, 16);
1878 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
1879 /* same array, so preserve events and
1880 * dev_number */
1881 sb->events = refsb->events;
1882 /* bugs in 2.6.17 and earlier mean the
1883 * dev_number chosen in Manage must be preserved
1884 */
1885 if (get_linux_version() >= 2006018)
1886 sb->dev_number = refsb->dev_number;
1887 }
1888 free_super1(refst);
1889 }
1890 free(refst);
1891
1892 if (!get_dev_size(di->fd, NULL, &dsize)) {
1893 rv = 1;
1894 goto error_out;
1895 }
1896 dsize >>= 9;
1897
1898 if (dsize < 24) {
1899 close(di->fd);
1900 rv = 2;
1901 goto error_out;
1902 }
1903
1904 /*
1905 * Calculate the position of the superblock.
1906 * It is always aligned to a 4K boundary and
1907 * depending on minor_version, it can be:
1908 * 0: At least 8K, but less than 12K, from end of device
1909 * 1: At start of device
1910 * 2: 4K from start of device.
1911 * data_offset has already been set.
1912 */
1913 array_size = __le64_to_cpu(sb->size);
1914
1915 /* work out how much space we left for a bitmap */
1916 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
1917 bitmap_super_t *bms = (bitmap_super_t *)
1918 (((char *)sb) + MAX_SB_SIZE);
1919 bm_space = calc_bitmap_size(bms, 4096) >> 9;
1920 bm_offset = (long)__le32_to_cpu(sb->bitmap_offset);
1921 } else if (sb->feature_map & __cpu_to_le32(MD_FEATURE_PPL)) {
1922 bm_space = choose_ppl_space(__le32_to_cpu(sb->chunksize));
1923 if (bm_space > UINT16_MAX)
1924 bm_space = UINT16_MAX;
1925 if (st->minor_version == 0) {
1926 bm_offset = -bm_space - 8;
1927 if (bm_offset < INT16_MIN) {
1928 bm_offset = INT16_MIN;
1929 bm_space = -bm_offset - 8;
1930 }
1931 } else {
1932 bm_offset = 8;
1933 }
1934 sb->ppl.offset = __cpu_to_le16(bm_offset);
1935 sb->ppl.size = __cpu_to_le16(bm_space);
1936 } else {
1937 bm_space = choose_bm_space(array_size);
1938 bm_offset = 8;
1939 }
1940
1941 data_offset = di->data_offset;
1942 if (data_offset == INVALID_SECTORS)
1943 data_offset = st->data_offset;
1944 switch(st->minor_version) {
1945 case 0:
1946 /* Add 8 sectors for bad block log */
1947 bm_space += 8;
1948 if (data_offset == INVALID_SECTORS)
1949 data_offset = 0;
1950 sb_offset = dsize;
1951 sb_offset -= 8*2;
1952 sb_offset &= ~(4*2-1);
1953 sb->data_offset = __cpu_to_le64(data_offset);
1954 sb->super_offset = __cpu_to_le64(sb_offset);
1955 if (sb_offset < array_size + bm_space)
1956 bm_space = sb_offset - array_size;
1957 sb->data_size = __cpu_to_le64(sb_offset - bm_space);
1958 if (bm_space >= 8) {
1959 sb->bblog_size = __cpu_to_le16(8);
1960 sb->bblog_offset = __cpu_to_le32((unsigned)-8);
1961 }
1962 break;
1963 case 1:
1964 case 2:
1965 sb_offset = st->minor_version == 2 ? 8 : 0;
1966 sb->super_offset = __cpu_to_le64(sb_offset);
1967 if (data_offset == INVALID_SECTORS)
1968 data_offset = sb_offset + 16;
1969
1970 sb->data_offset = __cpu_to_le64(data_offset);
1971 sb->data_size = __cpu_to_le64(dsize - data_offset);
1972 if (data_offset >= sb_offset+bm_offset+bm_space+8) {
1973 sb->bblog_size = __cpu_to_le16(8);
1974 sb->bblog_offset = __cpu_to_le32(bm_offset +
1975 bm_space);
1976 } else if (data_offset >= sb_offset + 16) {
1977 sb->bblog_size = __cpu_to_le16(8);
1978 /* '8' sectors for the bblog, and 'sb_offset'
1979 * because we want offset from superblock, not
1980 * start of device.
1981 */
1982 sb->bblog_offset = __cpu_to_le32(data_offset -
1983 8 - sb_offset);
1984 }
1985 break;
1986 default:
1987 pr_err("Failed to write invalid metadata format 1.%i to %s\n",
1988 st->minor_version, di->devname);
1989 rv = -EINVAL;
1990 goto out;
1991 }
1992 /* Disable badblock log on clusters, or when explicitly requested */
1993 if (st->nodes > 0 || conf_get_create_info()->bblist == 0) {
1994 sb->bblog_size = 0;
1995 sb->bblog_offset = 0;
1996 }
1997
1998 sb->sb_csum = calc_sb_1_csum(sb);
1999 rv = store_super1(st, di->fd);
2000
2001 if (rv == 0 && (di->disk.state & (1 << MD_DISK_JOURNAL))) {
2002 rv = write_empty_r5l_meta_block(st, di->fd);
2003 if (rv)
2004 goto error_out;
2005 }
2006
2007 if (rv == 0 &&
2008 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
2009 rv = st->ss->write_bitmap(st, di->fd, NodeNumUpdate);
2010 } else if (rv == 0 &&
2011 (__le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL)) {
2012 struct mdinfo info;
2013
2014 st->ss->getinfo_super(st, &info, NULL);
2015 rv = st->ss->write_init_ppl(st, &info, di->fd);
2016 }
2017
2018 close(di->fd);
2019 di->fd = -1;
2020 if (rv)
2021 goto error_out;
2022 }
2023 error_out:
2024 if (rv)
2025 pr_err("Failed to write metadata to %s\n",
2026 di->devname);
2027 out:
2028 return rv;
2029 }
2030 #endif
2031
2032 static int compare_super1(struct supertype *st, struct supertype *tst)
2033 {
2034 /*
2035 * return:
2036 * 0 same, or first was empty, and second was copied
2037 * 1 second had wrong number
2038 * 2 wrong uuid
2039 * 3 wrong other info
2040 */
2041 struct mdp_superblock_1 *first = st->sb;
2042 struct mdp_superblock_1 *second = tst->sb;
2043
2044 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
2045 return 1;
2046 if (second->major_version != __cpu_to_le32(1))
2047 return 1;
2048
2049 if (!first) {
2050 if (posix_memalign((void**)&first, 4096, SUPER1_SIZE) != 0) {
2051 pr_err("could not allocate superblock\n");
2052 return 1;
2053 }
2054 memcpy(first, second, SUPER1_SIZE);
2055 st->sb = first;
2056 return 0;
2057 }
2058 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
2059 return 2;
2060
2061 if (first->ctime != second->ctime ||
2062 first->level != second->level ||
2063 first->layout != second->layout ||
2064 first->size != second->size ||
2065 first->chunksize != second->chunksize ||
2066 first->raid_disks != second->raid_disks)
2067 return 3;
2068 return 0;
2069 }
2070
2071 static int load_super1(struct supertype *st, int fd, char *devname)
2072 {
2073 unsigned long long dsize;
2074 unsigned long long sb_offset;
2075 struct mdp_superblock_1 *super;
2076 int uuid[4];
2077 struct bitmap_super_s *bsb;
2078 struct misc_dev_info *misc;
2079 struct align_fd afd;
2080
2081 free_super1(st);
2082
2083 init_afd(&afd, fd);
2084
2085 if (st->ss == NULL || st->minor_version == -1) {
2086 int bestvers = -1;
2087 struct supertype tst;
2088 __u64 bestctime = 0;
2089 /* guess... choose latest ctime */
2090 memset(&tst, 0, sizeof(tst));
2091 tst.ss = &super1;
2092 for (tst.minor_version = 0; tst.minor_version <= 2 ; tst.minor_version++) {
2093 switch(load_super1(&tst, fd, devname)) {
2094 case 0: super = tst.sb;
2095 if (bestvers == -1 ||
2096 bestctime < __le64_to_cpu(super->ctime)) {
2097 bestvers = tst.minor_version;
2098 bestctime = __le64_to_cpu(super->ctime);
2099 }
2100 free(super);
2101 tst.sb = NULL;
2102 break;
2103 case 1: return 1; /*bad device */
2104 case 2: break; /* bad, try next */
2105 }
2106 }
2107 if (bestvers != -1) {
2108 int rv;
2109 tst.minor_version = bestvers;
2110 tst.ss = &super1;
2111 tst.max_devs = MAX_DEVS;
2112 rv = load_super1(&tst, fd, devname);
2113 if (rv == 0)
2114 *st = tst;
2115 return rv;
2116 }
2117 return 2;
2118 }
2119 if (!get_dev_size(fd, devname, &dsize))
2120 return 1;
2121 dsize >>= 9;
2122
2123 if (dsize < 24) {
2124 if (devname)
2125 pr_err("%s is too small for md: size is %llu sectors.\n",
2126 devname, dsize);
2127 return 1;
2128 }
2129
2130 /*
2131 * Calculate the position of the superblock.
2132 * It is always aligned to a 4K boundary and
2133 * depending on minor_version, it can be:
2134 * 0: At least 8K, but less than 12K, from end of device
2135 * 1: At start of device
2136 * 2: 4K from start of device.
2137 */
2138 switch(st->minor_version) {
2139 case 0:
2140 sb_offset = dsize;
2141 sb_offset -= 8*2;
2142 sb_offset &= ~(4*2-1);
2143 break;
2144 case 1:
2145 sb_offset = 0;
2146 break;
2147 case 2:
2148 sb_offset = 4*2;
2149 break;
2150 default:
2151 return -EINVAL;
2152 }
2153
2154 if (lseek64(fd, sb_offset << 9, 0)< 0LL) {
2155 if (devname)
2156 pr_err("Cannot seek to superblock on %s: %s\n",
2157 devname, strerror(errno));
2158 return 1;
2159 }
2160
2161 if (posix_memalign((void**)&super, 4096, SUPER1_SIZE) != 0) {
2162 pr_err("could not allocate superblock\n");
2163 return 1;
2164 }
2165
2166 memset(super, 0, SUPER1_SIZE);
2167
2168 if (aread(&afd, super, MAX_SB_SIZE) != MAX_SB_SIZE) {
2169 if (devname)
2170 pr_err("Cannot read superblock on %s\n",
2171 devname);
2172 free(super);
2173 return 1;
2174 }
2175
2176 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
2177 if (devname)
2178 pr_err("No super block found on %s (Expected magic %08x, got %08x)\n",
2179 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
2180 free(super);
2181 return 2;
2182 }
2183
2184 if (__le32_to_cpu(super->major_version) != 1) {
2185 if (devname)
2186 pr_err("Cannot interpret superblock on %s - version is %d\n",
2187 devname, __le32_to_cpu(super->major_version));
2188 free(super);
2189 return 2;
2190 }
2191 if (__le64_to_cpu(super->super_offset) != sb_offset) {
2192 if (devname)
2193 pr_err("No superblock found on %s (super_offset is wrong)\n",
2194 devname);
2195 free(super);
2196 return 2;
2197 }
2198 st->sb = super;
2199
2200 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2201
2202 misc = (struct misc_dev_info*) (((char*)super)+MAX_SB_SIZE+BM_SUPER_SIZE);
2203 misc->device_size = dsize;
2204 if (st->data_offset == INVALID_SECTORS)
2205 st->data_offset = __le64_to_cpu(super->data_offset);
2206
2207 /* Now check on the bitmap superblock */
2208 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
2209 return 0;
2210 /* Read the bitmap superblock and make sure it looks
2211 * valid. If it doesn't clear the bit. An --assemble --force
2212 * should get that written out.
2213 */
2214 locate_bitmap1(st, fd, 0);
2215 if (aread(&afd, bsb, 512) != 512)
2216 goto no_bitmap;
2217
2218 uuid_from_super1(st, uuid);
2219 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
2220 memcmp(bsb->uuid, uuid, 16) != 0)
2221 goto no_bitmap;
2222 return 0;
2223
2224 no_bitmap:
2225 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map)
2226 & ~MD_FEATURE_BITMAP_OFFSET);
2227 return 0;
2228 }
2229
2230 static struct supertype *match_metadata_desc1(char *arg)
2231 {
2232 struct supertype *st = xcalloc(1, sizeof(*st));
2233
2234 st->container_devnm[0] = 0;
2235 st->ss = &super1;
2236 st->max_devs = MAX_DEVS;
2237 st->sb = NULL;
2238 st->data_offset = INVALID_SECTORS;
2239 /* leading zeros can be safely ignored. --detail generates them. */
2240 while (*arg == '0')
2241 arg++;
2242 if (strcmp(arg, "1.0") == 0 ||
2243 strcmp(arg, "1.00") == 0) {
2244 st->minor_version = 0;
2245 return st;
2246 }
2247 if (strcmp(arg, "1.1") == 0 ||
2248 strcmp(arg, "1.01") == 0
2249 ) {
2250 st->minor_version = 1;
2251 return st;
2252 }
2253 if (strcmp(arg, "1.2") == 0 ||
2254 #ifndef DEFAULT_OLD_METADATA /* ifdef in super0.c */
2255 strcmp(arg, "default") == 0 ||
2256 #endif /* DEFAULT_OLD_METADATA */
2257 strcmp(arg, "1.02") == 0) {
2258 st->minor_version = 2;
2259 return st;
2260 }
2261 if (strcmp(arg, "1") == 0 ||
2262 strcmp(arg, "default") == 0) {
2263 st->minor_version = -1;
2264 return st;
2265 }
2266
2267 free(st);
2268 return NULL;
2269 }
2270
2271 /* find available size on device with this devsize, using
2272 * superblock type st, and reserving 'reserve' sectors for
2273 * a possible bitmap
2274 */
2275 static __u64 avail_size1(struct supertype *st, __u64 devsize,
2276 unsigned long long data_offset)
2277 {
2278 struct mdp_superblock_1 *super = st->sb;
2279 int bmspace = 0;
2280 int bbspace = 0;
2281 if (devsize < 24)
2282 return 0;
2283
2284 #ifndef MDASSEMBLE
2285 if (__le32_to_cpu(super->feature_map) & MD_FEATURE_BITMAP_OFFSET) {
2286 /* hot-add. allow for actual size of bitmap */
2287 struct bitmap_super_s *bsb;
2288 bsb = (struct bitmap_super_s *)(((char*)super)+MAX_SB_SIZE);
2289 bmspace = calc_bitmap_size(bsb, 4096) >> 9;
2290 } else if (__le32_to_cpu(super->feature_map) & MD_FEATURE_PPL) {
2291 bmspace = __le16_to_cpu(super->ppl.size);
2292 }
2293 #endif
2294 /* Allow space for bad block log */
2295 if (super->bblog_size)
2296 bbspace = __le16_to_cpu(super->bblog_size);
2297
2298 if (st->minor_version < 0)
2299 /* not specified, so time to set default */
2300 st->minor_version = 2;
2301
2302 if (data_offset == INVALID_SECTORS)
2303 data_offset = st->data_offset;
2304
2305 if (data_offset != INVALID_SECTORS)
2306 switch(st->minor_version) {
2307 case 0:
2308 return devsize - data_offset - 8*2 - bbspace;
2309 case 1:
2310 case 2:
2311 return devsize - data_offset;
2312 default:
2313 return 0;
2314 }
2315
2316 devsize -= bmspace;
2317
2318 switch(st->minor_version) {
2319 case 0:
2320 /* at end */
2321 return ((devsize - 8*2 - bbspace ) & ~(4*2-1));
2322 case 1:
2323 /* at start, 4K for superblock and possible bitmap */
2324 return devsize - 4*2 - bbspace;
2325 case 2:
2326 /* 4k from start, 4K for superblock and possible bitmap */
2327 return devsize - (4+4)*2 - bbspace;
2328 }
2329 return 0;
2330 }
2331
2332 static int
2333 add_internal_bitmap1(struct supertype *st,
2334 int *chunkp, int delay, int write_behind,
2335 unsigned long long size,
2336 int may_change, int major)
2337 {
2338 /*
2339 * If not may_change, then this is a 'Grow' without sysfs support for
2340 * bitmaps, and the bitmap must fit after the superblock at 1K offset.
2341 * If may_change, then this is create or a Grow with sysfs syupport,
2342 * and we can put the bitmap wherever we like.
2343 *
2344 * size is in sectors, chunk is in bytes !!!
2345 */
2346
2347 unsigned long long bits;
2348 unsigned long long max_bits;
2349 unsigned long long min_chunk;
2350 long offset;
2351 long bbl_offset, bbl_size;
2352 unsigned long long chunk = *chunkp;
2353 int room = 0;
2354 int creating = 0;
2355 int len;
2356 struct mdp_superblock_1 *sb = st->sb;
2357 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MAX_SB_SIZE);
2358 int uuid[4];
2359
2360 if (__le64_to_cpu(sb->data_size) == 0)
2361 /* Must be creating the array, else data_size would be non-zero */
2362 creating = 1;
2363 switch(st->minor_version) {
2364 case 0:
2365 /* either 3K after the superblock (when hot-add),
2366 * or some amount of space before.
2367 */
2368 if (creating) {
2369 /* We are creating array, so we *know* how much room has
2370 * been left.
2371 */
2372 offset = 0;
2373 bbl_size = 8;
2374 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2375 } else {
2376 room = __le64_to_cpu(sb->super_offset)
2377 - __le64_to_cpu(sb->data_offset)
2378 - __le64_to_cpu(sb->data_size);
2379 bbl_size = __le16_to_cpu(sb->bblog_size);
2380 if (bbl_size < 8)
2381 bbl_size = 8;
2382 bbl_offset = (__s32)__le32_to_cpu(sb->bblog_offset);
2383 if (bbl_size < -bbl_offset)
2384 bbl_size = -bbl_offset;
2385
2386 if (!may_change || (room < 3*2 &&
2387 __le32_to_cpu(sb->max_dev) <= 384)) {
2388 room = 3*2;
2389 offset = 1*2;
2390 bbl_size = 0;
2391 } else {
2392 offset = 0; /* means movable offset */
2393 }
2394 }
2395 break;
2396 case 1:
2397 case 2: /* between superblock and data */
2398 if (creating) {
2399 offset = 4*2;
2400 bbl_size = 8;
2401 room = choose_bm_space(__le64_to_cpu(sb->size)) + bbl_size;
2402 } else {
2403 room = __le64_to_cpu(sb->data_offset)
2404 - __le64_to_cpu(sb->super_offset);
2405 bbl_size = __le16_to_cpu(sb->bblog_size);
2406 if (bbl_size)
2407 room = __le32_to_cpu(sb->bblog_offset) + bbl_size;
2408 else
2409 bbl_size = 8;
2410
2411 if (!may_change) {
2412 room -= 2; /* Leave 1K for superblock */
2413 offset = 2;
2414 bbl_size = 0;
2415 } else {
2416 room -= 4*2; /* leave 4K for superblock */
2417 offset = 4*2;
2418 }
2419 }
2420 break;
2421 default:
2422 return -ENOSPC;
2423 }
2424
2425 room -= bbl_size;
2426 if (chunk == UnSet && room > 128*2)
2427 /* Limit to 128K of bitmap when chunk size not requested */
2428 room = 128*2;
2429
2430 if (room <= 1)
2431 /* No room for a bitmap */
2432 return -ENOSPC;
2433
2434 max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8;
2435
2436 min_chunk = 4096; /* sub-page chunks don't work yet.. */
2437 bits = (size*512)/min_chunk +1;
2438 while (bits > max_bits) {
2439 min_chunk *= 2;
2440 bits = (bits+1)/2;
2441 }
2442 if (chunk == UnSet) {
2443 /* For practical purpose, 64Meg is a good
2444 * default chunk size for internal bitmaps.
2445 */
2446 chunk = min_chunk;
2447 if (chunk < 64*1024*1024)
2448 chunk = 64*1024*1024;
2449 } else if (chunk < min_chunk)
2450 return -EINVAL; /* chunk size too small */
2451 if (chunk == 0) /* rounding problem */
2452 return -EINVAL;
2453
2454 if (offset == 0) {
2455 /* start bitmap on a 4K boundary with enough space for
2456 * the bitmap
2457 */
2458 bits = (size*512) / chunk + 1;
2459 room = ((bits+7)/8 + sizeof(bitmap_super_t) +4095)/4096;
2460 room *= 8; /* convert 4K blocks to sectors */
2461 offset = -room - bbl_size;
2462 }
2463
2464 sb->bitmap_offset = (int32_t)__cpu_to_le32(offset);
2465
2466 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2467 | MD_FEATURE_BITMAP_OFFSET);
2468 memset(bms, 0, sizeof(*bms));
2469 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
2470 bms->version = __cpu_to_le32(major);
2471 uuid_from_super1(st, uuid);
2472 memcpy(bms->uuid, uuid, 16);
2473 bms->chunksize = __cpu_to_le32(chunk);
2474 bms->daemon_sleep = __cpu_to_le32(delay);
2475 bms->sync_size = __cpu_to_le64(size);
2476 bms->write_behind = __cpu_to_le32(write_behind);
2477 bms->nodes = __cpu_to_le32(st->nodes);
2478 if (st->nodes)
2479 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map)
2480 | MD_FEATURE_BITMAP_VERSIONED);
2481 if (st->cluster_name) {
2482 len = sizeof(bms->cluster_name);
2483 strncpy((char *)bms->cluster_name, st->cluster_name, len);
2484 bms->cluster_name[len - 1] = '\0';
2485 }
2486
2487 *chunkp = chunk;
2488 return 0;
2489 }
2490
2491 static int locate_bitmap1(struct supertype *st, int fd, int node_num)
2492 {
2493 unsigned long long offset;
2494 struct mdp_superblock_1 *sb;
2495 int mustfree = 0;
2496 int ret;
2497
2498 if (!st->sb) {
2499 if (st->ss->load_super(st, fd, NULL))
2500 return -1; /* no error I hope... */
2501 mustfree = 1;
2502 }
2503 sb = st->sb;
2504
2505 if ((__le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET))
2506 ret = 0;
2507 else
2508 ret = -1;
2509 offset = __le64_to_cpu(sb->super_offset);
2510 offset += (int32_t) __le32_to_cpu(sb->bitmap_offset) * (node_num + 1);
2511 if (mustfree)
2512 free(sb);
2513 lseek64(fd, offset<<9, 0);
2514 return ret;
2515 }
2516
2517 static int write_bitmap1(struct supertype *st, int fd, enum bitmap_update update)
2518 {
2519 struct mdp_superblock_1 *sb = st->sb;
2520 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+MAX_SB_SIZE);
2521 int rv = 0;
2522 void *buf;
2523 int towrite, n, len;
2524 struct align_fd afd;
2525 unsigned int i = 0;
2526 unsigned long long total_bm_space, bm_space_per_node;
2527
2528 switch (update) {
2529 case NameUpdate:
2530 /* update cluster name */
2531 if (st->cluster_name) {
2532 len = sizeof(bms->cluster_name);
2533 memset((char *)bms->cluster_name, 0, len);
2534 strncpy((char *)bms->cluster_name,
2535 st->cluster_name, len);
2536 bms->cluster_name[len - 1] = '\0';
2537 }
2538 break;
2539 case NodeNumUpdate:
2540 /* cluster md only supports superblock 1.2 now */
2541 if (st->minor_version != 2 && bms->version == BITMAP_MAJOR_CLUSTERED) {
2542 pr_err("Warning: cluster md only works with superblock 1.2\n");
2543 return -EINVAL;
2544 }
2545
2546 if (bms->version == BITMAP_MAJOR_CLUSTERED) {
2547 if (__cpu_to_le32(st->nodes) < bms->nodes) {
2548 /* Since the nodes num is not increased, no need to check the space
2549 * is enough or not, just update bms->nodes */
2550 bms->nodes = __cpu_to_le32(st->nodes);
2551 break;
2552 }
2553 } else {
2554 /* no need to change bms->nodes for other bitmap types */
2555 if (st->nodes)
2556 pr_err("Warning: --nodes option is only suitable for clustered bitmap\n");
2557 break;
2558 }
2559
2560 /* Each node has an independent bitmap, it is necessary to calculate the
2561 * space is enough or not, first get how many bytes for the total bitmap */
2562 bm_space_per_node = calc_bitmap_size(bms, 4096);
2563
2564 total_bm_space = 512 * (__le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset));
2565 total_bm_space = total_bm_space - 4096; /* leave another 4k for superblock */
2566
2567 if (bm_space_per_node * st->nodes > total_bm_space) {
2568 pr_err("Warning: The max num of nodes can't exceed %llu\n",
2569 total_bm_space / bm_space_per_node);
2570 return -ENOMEM;
2571 }
2572
2573 bms->nodes = __cpu_to_le32(st->nodes);
2574 break;
2575 case NoUpdate:
2576 default:
2577 break;
2578 }
2579
2580 init_afd(&afd, fd);
2581
2582 locate_bitmap1(st, fd, 0);
2583
2584 if (posix_memalign(&buf, 4096, 4096))
2585 return -ENOMEM;
2586
2587 do {
2588 /* Only the bitmap[0] should resync
2589 * whole device on initial assembly
2590 */
2591 if (i)
2592 memset(buf, 0x00, 4096);
2593 else
2594 memset(buf, 0xff, 4096);
2595 memcpy(buf, (char *)bms, sizeof(bitmap_super_t));
2596
2597 /*
2598 * use 4096 boundary if bitmap_offset is aligned
2599 * with 8 sectors, then it should compatible with
2600 * older mdadm.
2601 */
2602 if (__le32_to_cpu(sb->bitmap_offset) & 7)
2603 towrite = calc_bitmap_size(bms, 512);
2604 else
2605 towrite = calc_bitmap_size(bms, 4096);
2606 while (towrite > 0) {
2607 n = towrite;
2608 if (n > 4096)
2609 n = 4096;
2610 n = awrite(&afd, buf, n);
2611 if (n > 0)
2612 towrite -= n;
2613 else
2614 break;
2615 if (i)
2616 memset(buf, 0x00, 4096);
2617 else
2618 memset(buf, 0xff, 4096);
2619 }
2620 fsync(fd);
2621 if (towrite) {
2622 rv = -2;
2623 break;
2624 }
2625 } while (++i < __le32_to_cpu(bms->nodes));
2626
2627 free(buf);
2628 return rv;
2629 }
2630
2631 static void free_super1(struct supertype *st)
2632 {
2633
2634 if (st->sb)
2635 free(st->sb);
2636 while (st->info) {
2637 struct devinfo *di = st->info;
2638 st->info = di->next;
2639 if (di->fd >= 0)
2640 close(di->fd);
2641 free(di);
2642 }
2643 st->sb = NULL;
2644 }
2645
2646 #ifndef MDASSEMBLE
2647 static int validate_geometry1(struct supertype *st, int level,
2648 int layout, int raiddisks,
2649 int *chunk, unsigned long long size,
2650 unsigned long long data_offset,
2651 char *subdev, unsigned long long *freesize,
2652 int consistency_policy, int verbose)
2653 {
2654 unsigned long long ldsize, devsize;
2655 int bmspace;
2656 unsigned long long headroom;
2657 int fd;
2658
2659 if (level == LEVEL_CONTAINER) {
2660 if (verbose)
2661 pr_err("1.x metadata does not support containers\n");
2662 return 0;
2663 }
2664 if (*chunk == UnSet)
2665 *chunk = DEFAULT_CHUNK;
2666
2667 if (!subdev)
2668 return 1;
2669
2670 if (st->minor_version < 0)
2671 /* not specified, so time to set default */
2672 st->minor_version = 2;
2673
2674 fd = open(subdev, O_RDONLY|O_EXCL, 0);
2675 if (fd < 0) {
2676 if (verbose)
2677 pr_err("super1.x cannot open %s: %s\n",
2678 subdev, strerror(errno));
2679 return 0;
2680 }
2681
2682 if (!get_dev_size(fd, subdev, &ldsize)) {
2683 close(fd);
2684 return 0;
2685 }
2686 close(fd);
2687
2688 devsize = ldsize >> 9;
2689 if (devsize < 24) {
2690 *freesize = 0;
2691 return 0;
2692 }
2693
2694 /* creating: allow suitable space for bitmap or PPL */
2695 bmspace = consistency_policy == CONSISTENCY_POLICY_PPL ?
2696 choose_ppl_space((*chunk)*2) : choose_bm_space(devsize);
2697
2698 if (data_offset == INVALID_SECTORS)
2699 data_offset = st->data_offset;
2700 if (data_offset == INVALID_SECTORS)
2701 switch (st->minor_version) {
2702 case 0:
2703 data_offset = 0;
2704 break;
2705 case 1:
2706 case 2:
2707 /* Choose data offset appropriate for this device
2708 * and use as default for whole array.
2709 * The data_offset must allow for bitmap space
2710 * and base metadata, should allow for some headroom
2711 * for reshape, and should be rounded to multiple
2712 * of 1M.
2713 * Headroom is limited to 128M, but aim for about 0.1%
2714 */
2715 headroom = 128*1024*2;
2716 while ((headroom << 10) > devsize &&
2717 (*chunk == 0 ||
2718 headroom / 2 >= ((unsigned)(*chunk)*2)*2))
2719 headroom >>= 1;
2720 data_offset = 12*2 + bmspace + headroom;
2721 #define ONE_MEG (2*1024)
2722 if (data_offset > ONE_MEG)
2723 data_offset = (data_offset / ONE_MEG) * ONE_MEG;
2724 break;
2725 }
2726 if (st->data_offset == INVALID_SECTORS)
2727 st->data_offset = data_offset;
2728 switch(st->minor_version) {
2729 case 0: /* metadata at end. Round down and subtract space to reserve */
2730 devsize = (devsize & ~(4ULL*2-1));
2731 /* space for metadata, bblog, bitmap/ppl */
2732 devsize -= 8*2 + 8 + bmspace;
2733 break;
2734 case 1:
2735 case 2:
2736 devsize -= data_offset;
2737 break;
2738 }
2739 *freesize = devsize;
2740 return 1;
2741 }
2742 #endif /* MDASSEMBLE */
2743
2744 void *super1_make_v0(struct supertype *st, struct mdinfo *info, mdp_super_t *sb0)
2745 {
2746 /* Create a v1.0 superblock based on 'info'*/
2747 void *ret;
2748 struct mdp_superblock_1 *sb;
2749 int i;
2750 unsigned long long offset;
2751
2752 if (posix_memalign(&ret, 4096, 1024) != 0)
2753 return NULL;
2754 sb = ret;
2755 memset(ret, 0, 1024);
2756 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
2757 sb->major_version = __cpu_to_le32(1);
2758
2759 copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid);
2760 sprintf(sb->set_name, "%d", sb0->md_minor);
2761 sb->ctime = __cpu_to_le32(info->array.ctime+1);
2762 sb->level = __cpu_to_le32(info->array.level);
2763 sb->layout = __cpu_to_le32(info->array.layout);
2764 sb->size = __cpu_to_le64(info->component_size);
2765 sb->chunksize = __cpu_to_le32(info->array.chunk_size/512);
2766 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
2767 if (info->array.level > 0)
2768 sb->data_size = sb->size;
2769 else
2770 sb->data_size = st->ss->avail_size(st, st->devsize/512, 0);
2771 sb->resync_offset = MaxSector;
2772 sb->max_dev = __cpu_to_le32(MD_SB_DISKS);
2773 sb->dev_number = __cpu_to_le32(info->disk.number);
2774 sb->utime = __cpu_to_le64(info->array.utime);
2775
2776 offset = st->devsize/512 - 8*2;
2777 offset &= ~(4*2-1);
2778 sb->super_offset = __cpu_to_le64(offset);
2779 //*(__u64*)(st->other + 128 + 8 + 8) = __cpu_to_le64(offset);
2780
2781 random_uuid(sb->device_uuid);
2782
2783 for (i = 0; i < MD_SB_DISKS; i++) {
2784 int state = sb0->disks[i].state;
2785 sb->dev_roles[i] = MD_DISK_ROLE_SPARE;
2786 if ((state & (1<<MD_DISK_SYNC)) &&
2787 !(state & (1<<MD_DISK_FAULTY)))
2788 sb->dev_roles[i] = __cpu_to_le16(sb0->disks[i].raid_disk);
2789 }
2790 sb->sb_csum = calc_sb_1_csum(sb);
2791 return ret;
2792 }
2793
2794 struct superswitch super1 = {
2795 #ifndef MDASSEMBLE
2796 .examine_super = examine_super1,
2797 .brief_examine_super = brief_examine_super1,
2798 .export_examine_super = export_examine_super1,
2799 .detail_super = detail_super1,
2800 .brief_detail_super = brief_detail_super1,
2801 .export_detail_super = export_detail_super1,
2802 .write_init_super = write_init_super1,
2803 .validate_geometry = validate_geometry1,
2804 .add_to_super = add_to_super1,
2805 .examine_badblocks = examine_badblocks_super1,
2806 .copy_metadata = copy_metadata1,
2807 .write_init_ppl = write_init_ppl1,
2808 #endif
2809 .match_home = match_home1,
2810 .uuid_from_super = uuid_from_super1,
2811 .getinfo_super = getinfo_super1,
2812 .container_content = container_content1,
2813 .update_super = update_super1,
2814 .init_super = init_super1,
2815 .store_super = store_super1,
2816 .compare_super = compare_super1,
2817 .load_super = load_super1,
2818 .match_metadata_desc = match_metadata_desc1,
2819 .avail_size = avail_size1,
2820 .add_internal_bitmap = add_internal_bitmap1,
2821 .locate_bitmap = locate_bitmap1,
2822 .write_bitmap = write_bitmap1,
2823 .free_super = free_super1,
2824 #if __BYTE_ORDER == BIG_ENDIAN
2825 .swapuuid = 0,
2826 #else
2827 .swapuuid = 1,
2828 #endif
2829 .name = "1.x",
2830 };