]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2006 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@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
30 | #define HAVE_STDINT_H 1 | |
31 | #include "mdadm.h" | |
32 | #include "sha1.h" | |
33 | /* | |
34 | * All handling for the 0.90.0 version superblock is in | |
35 | * this file. | |
36 | * This includes: | |
37 | * - finding, loading, and writing the superblock. | |
38 | * - initialising a new superblock | |
39 | * - printing the superblock for --examine | |
40 | * - printing part of the superblock for --detail | |
41 | * .. other stuff | |
42 | */ | |
43 | ||
44 | ||
45 | static unsigned long calc_sb0_csum(mdp_super_t *super) | |
46 | { | |
47 | unsigned long csum = super->sb_csum; | |
48 | unsigned long newcsum; | |
49 | super->sb_csum= 0 ; | |
50 | newcsum = calc_csum(super, MD_SB_BYTES); | |
51 | super->sb_csum = csum; | |
52 | return newcsum; | |
53 | } | |
54 | ||
55 | ||
56 | static void super0_swap_endian(struct mdp_superblock_s *sb) | |
57 | { | |
58 | /* as super0 superblocks are host-endian, it is sometimes | |
59 | * useful to be able to swap the endianness | |
60 | * as (almost) everything is u32's we byte-swap every 4byte | |
61 | * number. | |
62 | * We then also have to swap the events_hi and events_lo | |
63 | */ | |
64 | char *sbc = (char *)sb; | |
65 | __u32 t32; | |
66 | int i; | |
67 | ||
68 | for (i=0; i < MD_SB_BYTES ; i+=4) { | |
69 | char t = sbc[i]; | |
70 | sbc[i] = sbc[i+3]; | |
71 | sbc[i+3] = t; | |
72 | t=sbc[i+1]; | |
73 | sbc[i+1]=sbc[i+2]; | |
74 | sbc[i+2]=t; | |
75 | } | |
76 | t32 = sb->events_hi; | |
77 | sb->events_hi = sb->events_lo; | |
78 | sb->events_lo = t32; | |
79 | ||
80 | t32 = sb->cp_events_hi; | |
81 | sb->cp_events_hi = sb->cp_events_lo; | |
82 | sb->cp_events_lo = t32; | |
83 | ||
84 | } | |
85 | ||
86 | #ifndef MDASSEMBLE | |
87 | ||
88 | static void examine_super0(struct supertype *st, char *homehost) | |
89 | { | |
90 | mdp_super_t *sb = st->sb; | |
91 | time_t atime; | |
92 | int d; | |
93 | char *c; | |
94 | ||
95 | printf(" Magic : %08x\n", sb->md_magic); | |
96 | printf(" Version : %d.%02d.%02d\n", sb->major_version, sb->minor_version, | |
97 | sb->patch_version); | |
98 | if (sb->minor_version >= 90) { | |
99 | printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
100 | sb->set_uuid2, sb->set_uuid3); | |
101 | if (homehost) { | |
102 | char buf[20]; | |
103 | void *hash = sha1_buffer(homehost, | |
104 | strlen(homehost), | |
105 | buf); | |
106 | if (memcmp(&sb->set_uuid2, hash, 8)==0) | |
107 | printf(" (local to host %s)", homehost); | |
108 | } | |
109 | printf("\n"); | |
110 | } else | |
111 | printf(" UUID : %08x\n", sb->set_uuid0); | |
112 | ||
113 | if (sb->not_persistent) | |
114 | printf(" Eedk : not persistent\n"); | |
115 | ||
116 | atime = sb->ctime; | |
117 | printf(" Creation Time : %.24s\n", ctime(&atime)); | |
118 | c=map_num(pers, sb->level); | |
119 | printf(" Raid Level : %s\n", c?c:"-unknown-"); | |
120 | if ((int)sb->level >= 0) { | |
121 | int ddsks=0; | |
122 | printf(" Used Dev Size : %d%s\n", sb->size, | |
123 | human_size((long long)sb->size<<10)); | |
124 | switch(sb->level) { | |
125 | case 1: ddsks=1;break; | |
126 | case 4: | |
127 | case 5: ddsks = sb->raid_disks-1; break; | |
128 | case 6: ddsks = sb->raid_disks-2; break; | |
129 | case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255); | |
130 | } | |
131 | if (ddsks) | |
132 | printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size, | |
133 | human_size(ddsks*(long long)sb->size<<10)); | |
134 | } | |
135 | printf(" Raid Devices : %d\n", sb->raid_disks); | |
136 | printf(" Total Devices : %d\n", sb->nr_disks); | |
137 | printf("Preferred Minor : %d\n", sb->md_minor); | |
138 | printf("\n"); | |
139 | if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) { | |
140 | printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9)); | |
141 | if (sb->delta_disks) { | |
142 | printf(" Delta Devices : %d", sb->delta_disks); | |
143 | if (sb->delta_disks) | |
144 | printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks); | |
145 | else | |
146 | printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks); | |
147 | } | |
148 | if (sb->new_level != sb->level) { | |
149 | c = map_num(pers, sb->new_level); | |
150 | printf(" New Level : %s\n", c?c:"-unknown-"); | |
151 | } | |
152 | if (sb->new_layout != sb->layout) { | |
153 | if (sb->level == 5) { | |
154 | c = map_num(r5layout, sb->new_layout); | |
155 | printf(" New Layout : %s\n", c?c:"-unknown-"); | |
156 | } | |
157 | if (sb->level == 10) { | |
158 | printf(" New Layout : near=%d, %s=%d\n", | |
159 | sb->new_layout&255, | |
160 | (sb->new_layout&0x10000)?"offset":"far", | |
161 | (sb->new_layout>>8)&255); | |
162 | } | |
163 | } | |
164 | if (sb->new_chunk != sb->chunk_size) | |
165 | printf(" New Chunksize : %d\n", sb->new_chunk); | |
166 | printf("\n"); | |
167 | } | |
168 | atime = sb->utime; | |
169 | printf(" Update Time : %.24s\n", ctime(&atime)); | |
170 | printf(" State : %s\n", | |
171 | (sb->state&(1<<MD_SB_CLEAN))?"clean":"active"); | |
172 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) | |
173 | printf("Internal Bitmap : present\n"); | |
174 | printf(" Active Devices : %d\n", sb->active_disks); | |
175 | printf("Working Devices : %d\n", sb->working_disks); | |
176 | printf(" Failed Devices : %d\n", sb->failed_disks); | |
177 | printf(" Spare Devices : %d\n", sb->spare_disks); | |
178 | if (calc_sb0_csum(sb) == sb->sb_csum) | |
179 | printf(" Checksum : %x - correct\n", sb->sb_csum); | |
180 | else | |
181 | printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb)); | |
182 | printf(" Events : %llu\n", | |
183 | ((unsigned long long)sb->events_hi << 32) | |
184 | + sb->events_lo); | |
185 | printf("\n"); | |
186 | if (sb->level == 5) { | |
187 | c = map_num(r5layout, sb->layout); | |
188 | printf(" Layout : %s\n", c?c:"-unknown-"); | |
189 | } | |
190 | if (sb->level == 10) { | |
191 | printf(" Layout :"); | |
192 | print_r10_layout(sb->layout); | |
193 | printf("\n"); | |
194 | } | |
195 | switch(sb->level) { | |
196 | case 0: | |
197 | case 4: | |
198 | case 5: | |
199 | case 6: | |
200 | case 10: | |
201 | printf(" Chunk Size : %dK\n", sb->chunk_size/1024); | |
202 | break; | |
203 | case -1: | |
204 | printf(" Rounding : %dK\n", sb->chunk_size/1024); | |
205 | break; | |
206 | default: break; | |
207 | } | |
208 | printf("\n"); | |
209 | printf(" Number Major Minor RaidDevice State\n"); | |
210 | for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) { | |
211 | mdp_disk_t *dp; | |
212 | char *dv; | |
213 | char nb[5]; | |
214 | int wonly; | |
215 | if (d>=0) dp = &sb->disks[d]; | |
216 | else dp = &sb->this_disk; | |
217 | snprintf(nb, sizeof(nb), "%4d", d); | |
218 | printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb, | |
219 | dp->number, dp->major, dp->minor, dp->raid_disk); | |
220 | wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY); | |
221 | dp->state &= ~(1<<MD_DISK_WRITEMOSTLY); | |
222 | if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty"); | |
223 | if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active"); | |
224 | if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync"); | |
225 | if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed"); | |
226 | if (wonly) printf(" write-mostly"); | |
227 | if (dp->state == 0) printf(" spare"); | |
228 | if ((dv=map_dev(dp->major, dp->minor, 0))) | |
229 | printf(" %s", dv); | |
230 | printf("\n"); | |
231 | if (d == -1) printf("\n"); | |
232 | } | |
233 | } | |
234 | ||
235 | static void brief_examine_super0(struct supertype *st) | |
236 | { | |
237 | mdp_super_t *sb = st->sb; | |
238 | char *c=map_num(pers, sb->level); | |
239 | char devname[20]; | |
240 | ||
241 | sprintf(devname, "/dev/md%d", sb->md_minor); | |
242 | ||
243 | printf("ARRAY %s level=%s num-devices=%d UUID=", | |
244 | devname, | |
245 | c?c:"-unknown-", sb->raid_disks); | |
246 | if (sb->minor_version >= 90) | |
247 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
248 | sb->set_uuid2, sb->set_uuid3); | |
249 | else | |
250 | printf("%08x", sb->set_uuid0); | |
251 | printf("\n"); | |
252 | } | |
253 | ||
254 | static void export_examine_super0(struct supertype *st) | |
255 | { | |
256 | mdp_super_t *sb = st->sb; | |
257 | ||
258 | printf("MD_LEVEL=%s\n", map_num(pers, sb->level)); | |
259 | printf("MD_DEVICES=%d\n", sb->raid_disks); | |
260 | if (sb->minor_version >= 90) | |
261 | printf("MD_UUID=%08x:%08x:%08x:%08x\n", | |
262 | sb->set_uuid0, sb->set_uuid1, | |
263 | sb->set_uuid2, sb->set_uuid3); | |
264 | else | |
265 | printf("MD_UUID=%08x\n", sb->set_uuid0); | |
266 | printf("MD_UPDATE_TIME=%llu\n", | |
267 | __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL); | |
268 | printf("MD_EVENTS=%llu\n", | |
269 | ((unsigned long long)sb->events_hi << 32) | |
270 | + sb->events_lo); | |
271 | } | |
272 | ||
273 | static void detail_super0(struct supertype *st, char *homehost) | |
274 | { | |
275 | mdp_super_t *sb = st->sb; | |
276 | printf(" UUID : "); | |
277 | if (sb->minor_version >= 90) | |
278 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
279 | sb->set_uuid2, sb->set_uuid3); | |
280 | else | |
281 | printf("%08x", sb->set_uuid0); | |
282 | if (homehost) { | |
283 | char buf[20]; | |
284 | void *hash = sha1_buffer(homehost, | |
285 | strlen(homehost), | |
286 | buf); | |
287 | if (memcmp(&sb->set_uuid2, hash, 8)==0) | |
288 | printf(" (local to host %s)", homehost); | |
289 | } | |
290 | printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo); | |
291 | } | |
292 | ||
293 | static void brief_detail_super0(struct supertype *st) | |
294 | { | |
295 | mdp_super_t *sb = st->sb; | |
296 | printf(" UUID="); | |
297 | if (sb->minor_version >= 90) | |
298 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
299 | sb->set_uuid2, sb->set_uuid3); | |
300 | else | |
301 | printf("%08x", sb->set_uuid0); | |
302 | } | |
303 | #endif | |
304 | ||
305 | static int match_home0(struct supertype *st, char *homehost) | |
306 | { | |
307 | mdp_super_t *sb = st->sb; | |
308 | char buf[20]; | |
309 | char *hash = sha1_buffer(homehost, | |
310 | strlen(homehost), | |
311 | buf); | |
312 | ||
313 | return (memcmp(&sb->set_uuid2, hash, 8)==0); | |
314 | } | |
315 | ||
316 | static void uuid_from_super0(struct supertype *st, int uuid[4]) | |
317 | { | |
318 | mdp_super_t *super = st->sb; | |
319 | uuid[0] = super->set_uuid0; | |
320 | if (super->minor_version >= 90) { | |
321 | uuid[1] = super->set_uuid1; | |
322 | uuid[2] = super->set_uuid2; | |
323 | uuid[3] = super->set_uuid3; | |
324 | } else { | |
325 | uuid[1] = 0; | |
326 | uuid[2] = 0; | |
327 | uuid[3] = 0; | |
328 | } | |
329 | } | |
330 | ||
331 | static void getinfo_super0(struct supertype *st, struct mdinfo *info) | |
332 | { | |
333 | mdp_super_t *sb = st->sb; | |
334 | int working = 0; | |
335 | int i; | |
336 | ||
337 | info->array.major_version = sb->major_version; | |
338 | info->array.minor_version = sb->minor_version; | |
339 | info->array.patch_version = sb->patch_version; | |
340 | info->array.raid_disks = sb->raid_disks; | |
341 | info->array.level = sb->level; | |
342 | info->array.layout = sb->layout; | |
343 | info->array.md_minor = sb->md_minor; | |
344 | info->array.ctime = sb->ctime; | |
345 | info->array.utime = sb->utime; | |
346 | info->array.chunk_size = sb->chunk_size; | |
347 | info->array.state = sb->state; | |
348 | info->component_size = sb->size*2; | |
349 | ||
350 | info->disk.state = sb->this_disk.state; | |
351 | info->disk.major = sb->this_disk.major; | |
352 | info->disk.minor = sb->this_disk.minor; | |
353 | info->disk.raid_disk = sb->this_disk.raid_disk; | |
354 | info->disk.number = sb->this_disk.number; | |
355 | ||
356 | info->events = md_event(sb); | |
357 | info->data_offset = 0; | |
358 | ||
359 | sprintf(info->text_version, "0.%d", sb->minor_version); | |
360 | info->safe_mode_delay = 200; | |
361 | ||
362 | uuid_from_super0(st, info->uuid); | |
363 | ||
364 | if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) { | |
365 | info->reshape_active = 1; | |
366 | info->reshape_progress = sb->reshape_position; | |
367 | info->new_level = sb->new_level; | |
368 | info->delta_disks = sb->delta_disks; | |
369 | info->new_layout = sb->new_layout; | |
370 | info->new_chunk = sb->new_chunk; | |
371 | } else | |
372 | info->reshape_active = 0; | |
373 | ||
374 | sprintf(info->name, "%d", sb->md_minor); | |
375 | /* work_disks is calculated rather than read directly */ | |
376 | for (i=0; i < MD_SB_DISKS; i++) | |
377 | if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) && | |
378 | (sb->disks[i].raid_disk < info->array.raid_disks) && | |
379 | (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) && | |
380 | !(sb->disks[i].state & (1<<MD_DISK_FAULTY))) | |
381 | working ++; | |
382 | info->array.working_disks = working; | |
383 | } | |
384 | ||
385 | ||
386 | static int update_super0(struct supertype *st, struct mdinfo *info, | |
387 | char *update, | |
388 | char *devname, int verbose, | |
389 | int uuid_set, char *homehost) | |
390 | { | |
391 | /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made. | |
392 | * For others, the return value is ignored. | |
393 | */ | |
394 | int rv = 0; | |
395 | mdp_super_t *sb = st->sb; | |
396 | if (strcmp(update, "sparc2.2")==0 ) { | |
397 | /* 2.2 sparc put the events in the wrong place | |
398 | * So we copy the tail of the superblock | |
399 | * up 4 bytes before continuing | |
400 | */ | |
401 | __u32 *sb32 = (__u32*)sb; | |
402 | memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7, | |
403 | sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1, | |
404 | (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4); | |
405 | if (verbose >= 0) | |
406 | fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n", | |
407 | devname); | |
408 | } | |
409 | if (strcmp(update, "super-minor") ==0) { | |
410 | sb->md_minor = info->array.md_minor; | |
411 | if (verbose > 0) | |
412 | fprintf(stderr, Name ": updating superblock of %s with minor number %d\n", | |
413 | devname, info->array.md_minor); | |
414 | } | |
415 | if (strcmp(update, "summaries") == 0) { | |
416 | int i; | |
417 | /* set nr_disks, active_disks, working_disks, | |
418 | * failed_disks, spare_disks based on disks[] | |
419 | * array in superblock. | |
420 | * Also make sure extra slots aren't 'failed' | |
421 | */ | |
422 | sb->nr_disks = sb->active_disks = | |
423 | sb->working_disks = sb->failed_disks = | |
424 | sb->spare_disks = 0; | |
425 | for (i=0; i < MD_SB_DISKS ; i++) | |
426 | if (sb->disks[i].major || | |
427 | sb->disks[i].minor) { | |
428 | int state = sb->disks[i].state; | |
429 | if (state & (1<<MD_DISK_REMOVED)) | |
430 | continue; | |
431 | sb->nr_disks++; | |
432 | if (state & (1<<MD_DISK_ACTIVE)) | |
433 | sb->active_disks++; | |
434 | if (state & (1<<MD_DISK_FAULTY)) | |
435 | sb->failed_disks++; | |
436 | else | |
437 | sb->working_disks++; | |
438 | if (state == 0) | |
439 | sb->spare_disks++; | |
440 | } else if (i >= sb->raid_disks && sb->disks[i].number == 0) | |
441 | sb->disks[i].state = 0; | |
442 | } | |
443 | if (strcmp(update, "force-one")==0) { | |
444 | /* Not enough devices for a working array, so | |
445 | * bring this one up-to-date. | |
446 | */ | |
447 | __u32 ehi = sb->events_hi, elo = sb->events_lo; | |
448 | sb->events_hi = (info->events>>32) & 0xFFFFFFFF; | |
449 | sb->events_lo = (info->events) & 0xFFFFFFFF; | |
450 | if (sb->events_hi != ehi || | |
451 | sb->events_lo != elo) | |
452 | rv = 1; | |
453 | } | |
454 | if (strcmp(update, "force-array")==0) { | |
455 | /* degraded array and 'force' requested, so | |
456 | * maybe need to mark it 'clean' | |
457 | */ | |
458 | if ((sb->level == 5 || sb->level == 4 || sb->level == 6) && | |
459 | (sb->state & (1 << MD_SB_CLEAN)) == 0) { | |
460 | /* need to force clean */ | |
461 | sb->state |= (1 << MD_SB_CLEAN); | |
462 | rv = 1; | |
463 | } | |
464 | } | |
465 | if (strcmp(update, "assemble")==0) { | |
466 | int d = info->disk.number; | |
467 | int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY); | |
468 | if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY)) | |
469 | != info->disk.state) { | |
470 | sb->disks[d].state = info->disk.state | wonly; | |
471 | rv = 1; | |
472 | } | |
473 | } | |
474 | if (strcmp(update, "linear-grow-new") == 0) { | |
475 | memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0])); | |
476 | sb->disks[info->disk.number].number = info->disk.number; | |
477 | sb->disks[info->disk.number].major = info->disk.major; | |
478 | sb->disks[info->disk.number].minor = info->disk.minor; | |
479 | sb->disks[info->disk.number].raid_disk = info->disk.raid_disk; | |
480 | sb->disks[info->disk.number].state = info->disk.state; | |
481 | sb->this_disk = sb->disks[info->disk.number]; | |
482 | } | |
483 | if (strcmp(update, "linear-grow-update") == 0) { | |
484 | sb->raid_disks = info->array.raid_disks; | |
485 | sb->nr_disks = info->array.nr_disks; | |
486 | sb->active_disks = info->array.active_disks; | |
487 | sb->working_disks = info->array.working_disks; | |
488 | memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0])); | |
489 | sb->disks[info->disk.number].number = info->disk.number; | |
490 | sb->disks[info->disk.number].major = info->disk.major; | |
491 | sb->disks[info->disk.number].minor = info->disk.minor; | |
492 | sb->disks[info->disk.number].raid_disk = info->disk.raid_disk; | |
493 | sb->disks[info->disk.number].state = info->disk.state; | |
494 | } | |
495 | if (strcmp(update, "resync") == 0) { | |
496 | /* make sure resync happens */ | |
497 | sb->state &= ~(1<<MD_SB_CLEAN); | |
498 | sb->recovery_cp = 0; | |
499 | } | |
500 | if (strcmp(update, "homehost") == 0 && | |
501 | homehost) { | |
502 | uuid_set = 0; | |
503 | update = "uuid"; | |
504 | info->uuid[0] = sb->set_uuid0; | |
505 | info->uuid[1] = sb->set_uuid1; | |
506 | } | |
507 | if (strcmp(update, "uuid") == 0) { | |
508 | if (!uuid_set && homehost) { | |
509 | char buf[20]; | |
510 | char *hash = sha1_buffer(homehost, | |
511 | strlen(homehost), | |
512 | buf); | |
513 | memcpy(info->uuid+2, hash, 8); | |
514 | } | |
515 | sb->set_uuid0 = info->uuid[0]; | |
516 | sb->set_uuid1 = info->uuid[1]; | |
517 | sb->set_uuid2 = info->uuid[2]; | |
518 | sb->set_uuid3 = info->uuid[3]; | |
519 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { | |
520 | struct bitmap_super_s *bm; | |
521 | bm = (struct bitmap_super_s*)(sb+1); | |
522 | uuid_from_super0(st, (int*)bm->uuid); | |
523 | } | |
524 | } | |
525 | if (strcmp(update, "_reshape_progress")==0) | |
526 | sb->reshape_position = info->reshape_progress; | |
527 | ||
528 | sb->sb_csum = calc_sb0_csum(sb); | |
529 | return rv; | |
530 | } | |
531 | ||
532 | /* | |
533 | * For verion-0 superblock, the homehost is 'stored' in the | |
534 | * uuid. 8 bytes for a hash of the host leaving 8 bytes | |
535 | * of random material. | |
536 | * We use the first 8 bytes (64bits) of the sha1 of the | |
537 | * host name | |
538 | */ | |
539 | ||
540 | ||
541 | static int init_super0(struct supertype *st, mdu_array_info_t *info, | |
542 | unsigned long long size, char *ignored_name, char *homehost, | |
543 | int *uuid) | |
544 | { | |
545 | mdp_super_t *sb; | |
546 | int spares; | |
547 | ||
548 | if (posix_memalign((void**)&sb, 4096, | |
549 | MD_SB_BYTES + ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) { | |
550 | fprintf(stderr, Name ": %s could not allocate superblock\n", __func__); | |
551 | return 0; | |
552 | } | |
553 | memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t)); | |
554 | ||
555 | st->sb = sb; | |
556 | if (info == NULL) { | |
557 | /* zeroing the superblock */ | |
558 | return 0; | |
559 | } | |
560 | ||
561 | spares = info->working_disks - info->active_disks; | |
562 | if (info->raid_disks + spares > MD_SB_DISKS) { | |
563 | fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n", | |
564 | info->raid_disks , spares, MD_SB_DISKS); | |
565 | return 0; | |
566 | } | |
567 | ||
568 | sb->md_magic = MD_SB_MAGIC; | |
569 | sb->major_version = 0; | |
570 | sb->minor_version = 90; | |
571 | sb->patch_version = 0; | |
572 | sb->gvalid_words = 0; /* ignored */ | |
573 | sb->ctime = time(0); | |
574 | sb->level = info->level; | |
575 | if (size != info->size) | |
576 | return 0; | |
577 | sb->size = info->size; | |
578 | sb->nr_disks = info->nr_disks; | |
579 | sb->raid_disks = info->raid_disks; | |
580 | sb->md_minor = info->md_minor; | |
581 | sb->not_persistent = 0; | |
582 | if (uuid) { | |
583 | sb->set_uuid0 = uuid[0]; | |
584 | sb->set_uuid1 = uuid[1]; | |
585 | sb->set_uuid2 = uuid[2]; | |
586 | sb->set_uuid3 = uuid[3]; | |
587 | } else { | |
588 | int rfd = open("/dev/urandom", O_RDONLY); | |
589 | if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4) | |
590 | sb->set_uuid0 = random(); | |
591 | if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) { | |
592 | sb->set_uuid1 = random(); | |
593 | sb->set_uuid2 = random(); | |
594 | sb->set_uuid3 = random(); | |
595 | } | |
596 | if (rfd >= 0) | |
597 | close(rfd); | |
598 | } | |
599 | if (homehost) { | |
600 | char buf[20]; | |
601 | char *hash = sha1_buffer(homehost, | |
602 | strlen(homehost), | |
603 | buf); | |
604 | memcpy(&sb->set_uuid2, hash, 8); | |
605 | } | |
606 | ||
607 | sb->utime = sb->ctime; | |
608 | sb->state = info->state; | |
609 | sb->active_disks = info->active_disks; | |
610 | sb->working_disks = info->working_disks; | |
611 | sb->failed_disks = info->failed_disks; | |
612 | sb->spare_disks = info->spare_disks; | |
613 | sb->events_hi = 0; | |
614 | sb->events_lo = 1; | |
615 | ||
616 | sb->layout = info->layout; | |
617 | sb->chunk_size = info->chunk_size; | |
618 | ||
619 | return 1; | |
620 | } | |
621 | ||
622 | struct devinfo { | |
623 | int fd; | |
624 | char *devname; | |
625 | mdu_disk_info_t disk; | |
626 | struct devinfo *next; | |
627 | }; | |
628 | ||
629 | #ifndef MDASSEMBLE | |
630 | /* Add a device to the superblock being created */ | |
631 | static int add_to_super0(struct supertype *st, mdu_disk_info_t *dinfo, | |
632 | int fd, char *devname) | |
633 | { | |
634 | mdp_super_t *sb = st->sb; | |
635 | mdp_disk_t *dk = &sb->disks[dinfo->number]; | |
636 | struct devinfo *di, **dip; | |
637 | ||
638 | dk->number = dinfo->number; | |
639 | dk->major = dinfo->major; | |
640 | dk->minor = dinfo->minor; | |
641 | dk->raid_disk = dinfo->raid_disk; | |
642 | dk->state = dinfo->state; | |
643 | ||
644 | sb->this_disk = sb->disks[dinfo->number]; | |
645 | sb->sb_csum = calc_sb0_csum(sb); | |
646 | ||
647 | dip = (struct devinfo **)&st->info; | |
648 | while (*dip) | |
649 | dip = &(*dip)->next; | |
650 | di = malloc(sizeof(struct devinfo)); | |
651 | di->fd = fd; | |
652 | di->devname = devname; | |
653 | di->disk = *dinfo; | |
654 | di->next = NULL; | |
655 | *dip = di; | |
656 | ||
657 | return 0; | |
658 | } | |
659 | #endif | |
660 | ||
661 | static int store_super0(struct supertype *st, int fd) | |
662 | { | |
663 | unsigned long long dsize; | |
664 | unsigned long long offset; | |
665 | mdp_super_t *super = st->sb; | |
666 | ||
667 | if (!get_dev_size(fd, NULL, &dsize)) | |
668 | return 1; | |
669 | ||
670 | if (dsize < MD_RESERVED_SECTORS*512) | |
671 | return 2; | |
672 | ||
673 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); | |
674 | ||
675 | offset *= 512; | |
676 | ||
677 | if (lseek64(fd, offset, 0)< 0LL) | |
678 | return 3; | |
679 | ||
680 | if (write(fd, super, sizeof(*super)) != sizeof(*super)) | |
681 | return 4; | |
682 | ||
683 | if (super->state & (1<<MD_SB_BITMAP_PRESENT)) { | |
684 | struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1); | |
685 | if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) | |
686 | if (write(fd, bm, ROUND_UP(sizeof(*bm),4096)) != | |
687 | ROUND_UP(sizeof(*bm),4096)) | |
688 | return 5; | |
689 | } | |
690 | ||
691 | fsync(fd); | |
692 | return 0; | |
693 | } | |
694 | ||
695 | #ifndef MDASSEMBLE | |
696 | static int write_init_super0(struct supertype *st) | |
697 | { | |
698 | mdp_super_t *sb = st->sb; | |
699 | int rv = 0; | |
700 | struct devinfo *di; | |
701 | ||
702 | for (di = st->info ; di && ! rv ; di = di->next) { | |
703 | ||
704 | if (di->disk.state == 1) | |
705 | continue; | |
706 | if (di->fd == -1) | |
707 | continue; | |
708 | Kill(di->devname, 0, 1, 1); | |
709 | Kill(di->devname, 0, 1, 1); | |
710 | ||
711 | sb->disks[di->disk.number].state &= ~(1<<MD_DISK_FAULTY); | |
712 | ||
713 | sb->this_disk = sb->disks[di->disk.number]; | |
714 | sb->sb_csum = calc_sb0_csum(sb); | |
715 | rv = store_super0(st, di->fd); | |
716 | ||
717 | if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT))) | |
718 | rv = st->ss->write_bitmap(st, di->fd); | |
719 | ||
720 | if (rv) | |
721 | fprintf(stderr, | |
722 | Name ": failed to write superblock to %s\n", | |
723 | di->devname); | |
724 | close(di->fd); | |
725 | di->fd = -1; | |
726 | } | |
727 | return rv; | |
728 | } | |
729 | #endif | |
730 | ||
731 | static int compare_super0(struct supertype *st, struct supertype *tst) | |
732 | { | |
733 | /* | |
734 | * return: | |
735 | * 0 same, or first was empty, and second was copied | |
736 | * 1 second had wrong number | |
737 | * 2 wrong uuid | |
738 | * 3 wrong other info | |
739 | */ | |
740 | mdp_super_t *first = st->sb; | |
741 | mdp_super_t *second = tst->sb; | |
742 | int uuid1[4], uuid2[4]; | |
743 | ||
744 | if (second->md_magic != MD_SB_MAGIC) | |
745 | return 1; | |
746 | if (!first) { | |
747 | if (posix_memalign((void**)&first, 4096, | |
748 | MD_SB_BYTES + | |
749 | ROUND_UP(sizeof(struct bitmap_super_s), 4096)) != 0) { | |
750 | fprintf(stderr, Name | |
751 | ": %s could not allocate superblock\n", __func__); | |
752 | return 1; | |
753 | } | |
754 | memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s)); | |
755 | st->sb = first; | |
756 | return 0; | |
757 | } | |
758 | ||
759 | uuid_from_super0(st, uuid1); | |
760 | uuid_from_super0(tst, uuid2); | |
761 | if (!same_uuid(uuid1, uuid2, 0)) | |
762 | return 2; | |
763 | if (first->major_version != second->major_version || | |
764 | first->minor_version != second->minor_version || | |
765 | first->patch_version != second->patch_version || | |
766 | first->gvalid_words != second->gvalid_words || | |
767 | first->ctime != second->ctime || | |
768 | first->level != second->level || | |
769 | first->size != second->size || | |
770 | first->raid_disks != second->raid_disks ) | |
771 | return 3; | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | ||
777 | static void free_super0(struct supertype *st); | |
778 | ||
779 | static int load_super0(struct supertype *st, int fd, char *devname) | |
780 | { | |
781 | /* try to read in the superblock | |
782 | * Return: | |
783 | * 0 on success | |
784 | * 1 on cannot get superblock | |
785 | * 2 on superblock meaningless | |
786 | */ | |
787 | unsigned long long dsize; | |
788 | unsigned long long offset; | |
789 | mdp_super_t *super; | |
790 | int uuid[4]; | |
791 | struct bitmap_super_s *bsb; | |
792 | ||
793 | free_super0(st); | |
794 | ||
795 | if (st->subarray[0]) | |
796 | return 1; | |
797 | ||
798 | if (!get_dev_size(fd, devname, &dsize)) | |
799 | return 1; | |
800 | ||
801 | if (dsize < MD_RESERVED_SECTORS*512) { | |
802 | if (devname) | |
803 | fprintf(stderr, Name | |
804 | ": %s is too small for md: size is %llu sectors.\n", | |
805 | devname, dsize); | |
806 | return 1; | |
807 | } | |
808 | ||
809 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); | |
810 | ||
811 | offset *= 512; | |
812 | ||
813 | ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */ | |
814 | ||
815 | if (lseek64(fd, offset, 0)< 0LL) { | |
816 | if (devname) | |
817 | fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n", | |
818 | devname, strerror(errno)); | |
819 | return 1; | |
820 | } | |
821 | ||
822 | if (posix_memalign((void**)&super, 4096, | |
823 | MD_SB_BYTES + | |
824 | ROUND_UP(sizeof(bitmap_super_t), 4096)) != 0) { | |
825 | fprintf(stderr, Name | |
826 | ": %s could not allocate superblock\n", __func__); | |
827 | return 1; | |
828 | } | |
829 | ||
830 | if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) { | |
831 | if (devname) | |
832 | fprintf(stderr, Name ": Cannot read superblock on %s\n", | |
833 | devname); | |
834 | free(super); | |
835 | return 1; | |
836 | } | |
837 | ||
838 | if (st->ss && st->minor_version == 9) | |
839 | super0_swap_endian(super); | |
840 | ||
841 | if (super->md_magic != MD_SB_MAGIC) { | |
842 | if (devname) | |
843 | fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n", | |
844 | devname, MD_SB_MAGIC, super->md_magic); | |
845 | free(super); | |
846 | return 2; | |
847 | } | |
848 | ||
849 | if (super->major_version != 0) { | |
850 | if (devname) | |
851 | fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n", | |
852 | devname, super->major_version); | |
853 | free(super); | |
854 | return 2; | |
855 | } | |
856 | st->sb = super; | |
857 | ||
858 | if (st->ss == NULL) { | |
859 | st->ss = &super0; | |
860 | st->minor_version = super->minor_version; | |
861 | st->max_devs = MD_SB_DISKS; | |
862 | st->info = NULL; | |
863 | } | |
864 | ||
865 | /* Now check on the bitmap superblock */ | |
866 | if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0) | |
867 | return 0; | |
868 | /* Read the bitmap superblock and make sure it looks | |
869 | * valid. If it doesn't clear the bit. An --assemble --force | |
870 | * should get that written out. | |
871 | */ | |
872 | if (read(fd, super+1, ROUND_UP(sizeof(struct bitmap_super_s),4096)) | |
873 | != ROUND_UP(sizeof(struct bitmap_super_s),4096)) | |
874 | goto no_bitmap; | |
875 | ||
876 | uuid_from_super0(st, uuid); | |
877 | bsb = (struct bitmap_super_s *)(super+1); | |
878 | if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC || | |
879 | memcmp(bsb->uuid, uuid, 16) != 0) | |
880 | goto no_bitmap; | |
881 | return 0; | |
882 | ||
883 | no_bitmap: | |
884 | super->state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
889 | static struct supertype *match_metadata_desc0(char *arg) | |
890 | { | |
891 | struct supertype *st = malloc(sizeof(*st)); | |
892 | if (!st) return st; | |
893 | ||
894 | memset(st, 0, sizeof(*st)); | |
895 | st->ss = &super0; | |
896 | st->info = NULL; | |
897 | st->minor_version = 90; | |
898 | st->max_devs = MD_SB_DISKS; | |
899 | st->sb = NULL; | |
900 | /* we sometimes get 00.90 */ | |
901 | while (arg[0] == '0' && arg[1] == '0') | |
902 | arg++; | |
903 | if (strcmp(arg, "0") == 0 || | |
904 | strcmp(arg, "0.90") == 0 || | |
905 | strcmp(arg, "default") == 0 || | |
906 | strcmp(arg, "") == 0 /* no metadata */ | |
907 | ) | |
908 | return st; | |
909 | ||
910 | st->minor_version = 91; /* reshape in progress */ | |
911 | if (strcmp(arg, "0.91") == 0) /* For dup_super support */ | |
912 | return st; | |
913 | ||
914 | st->minor_version = 9; /* flag for 'byte-swapped' */ | |
915 | if (strcmp(arg, "0.swap")==0 || | |
916 | strcmp(arg, "0.9") == 0) /* For dup_super support */ | |
917 | return st; | |
918 | ||
919 | free(st); | |
920 | return NULL; | |
921 | } | |
922 | ||
923 | static __u64 avail_size0(struct supertype *st, __u64 devsize) | |
924 | { | |
925 | if (devsize < MD_RESERVED_SECTORS) | |
926 | return 0ULL; | |
927 | return MD_NEW_SIZE_SECTORS(devsize); | |
928 | } | |
929 | ||
930 | static int add_internal_bitmap0(struct supertype *st, int *chunkp, | |
931 | int delay, int write_behind, | |
932 | unsigned long long size, int may_change, | |
933 | int major) | |
934 | { | |
935 | /* | |
936 | * The bitmap comes immediately after the superblock and must be 60K in size | |
937 | * at most. The default size is between 30K and 60K | |
938 | * | |
939 | * size is in sectors, chunk is in bytes !!! | |
940 | */ | |
941 | unsigned long long bits; | |
942 | unsigned long long max_bits = 60*1024*8; | |
943 | unsigned long long min_chunk; | |
944 | int chunk = *chunkp; | |
945 | mdp_super_t *sb = st->sb; | |
946 | bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES); | |
947 | ||
948 | ||
949 | min_chunk = 4096; /* sub-page chunks don't work yet.. */ | |
950 | bits = (size * 512) / min_chunk + 1; | |
951 | while (bits > max_bits) { | |
952 | min_chunk *= 2; | |
953 | bits = (bits+1)/2; | |
954 | } | |
955 | if (chunk == UnSet) | |
956 | chunk = min_chunk; | |
957 | else if (chunk < min_chunk) | |
958 | return 0; /* chunk size too small */ | |
959 | ||
960 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); | |
961 | ||
962 | memset(bms, 0, sizeof(*bms)); | |
963 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); | |
964 | bms->version = __cpu_to_le32(major); | |
965 | uuid_from_super0(st, (int*)bms->uuid); | |
966 | bms->chunksize = __cpu_to_le32(chunk); | |
967 | bms->daemon_sleep = __cpu_to_le32(delay); | |
968 | bms->sync_size = __cpu_to_le64(size); | |
969 | bms->write_behind = __cpu_to_le32(write_behind); | |
970 | *chunkp = chunk; | |
971 | return 1; | |
972 | } | |
973 | ||
974 | ||
975 | static void locate_bitmap0(struct supertype *st, int fd) | |
976 | { | |
977 | unsigned long long dsize; | |
978 | unsigned long long offset; | |
979 | ||
980 | if (!get_dev_size(fd, NULL, &dsize)) | |
981 | return; | |
982 | ||
983 | if (dsize < MD_RESERVED_SECTORS*512) | |
984 | return; | |
985 | ||
986 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); | |
987 | ||
988 | offset *= 512; | |
989 | ||
990 | offset += MD_SB_BYTES; | |
991 | ||
992 | lseek64(fd, offset, 0); | |
993 | } | |
994 | ||
995 | static int write_bitmap0(struct supertype *st, int fd) | |
996 | { | |
997 | unsigned long long dsize; | |
998 | unsigned long long offset; | |
999 | mdp_super_t *sb = st->sb; | |
1000 | ||
1001 | int rv = 0; | |
1002 | ||
1003 | int towrite, n; | |
1004 | char abuf[4096+4096]; | |
1005 | char *buf = (char*)(((long)(abuf+4096))&~4095L); | |
1006 | ||
1007 | if (!get_dev_size(fd, NULL, &dsize)) | |
1008 | return 1; | |
1009 | ||
1010 | ||
1011 | if (dsize < MD_RESERVED_SECTORS*512) | |
1012 | return -1; | |
1013 | ||
1014 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); | |
1015 | ||
1016 | offset *= 512; | |
1017 | ||
1018 | if (lseek64(fd, offset + 4096, 0)< 0LL) | |
1019 | return 3; | |
1020 | ||
1021 | memset(buf, 0xff, 4096); | |
1022 | memcpy(buf, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)); | |
1023 | towrite = 64*1024; | |
1024 | while (towrite > 0) { | |
1025 | n = towrite; | |
1026 | if (n > 4096) | |
1027 | n = 4096; | |
1028 | n = write(fd, buf, n); | |
1029 | if (n > 0) | |
1030 | towrite -= n; | |
1031 | else | |
1032 | break; | |
1033 | memset(buf, 0xff, 4096); | |
1034 | } | |
1035 | fsync(fd); | |
1036 | if (towrite) | |
1037 | rv = -2; | |
1038 | ||
1039 | return rv; | |
1040 | } | |
1041 | ||
1042 | static void free_super0(struct supertype *st) | |
1043 | { | |
1044 | if (st->sb) | |
1045 | free(st->sb); | |
1046 | st->sb = NULL; | |
1047 | } | |
1048 | ||
1049 | #ifndef MDASSEMBLE | |
1050 | static int validate_geometry0(struct supertype *st, int level, | |
1051 | int layout, int raiddisks, | |
1052 | int chunk, unsigned long long size, | |
1053 | char *subdev, unsigned long long *freesize, | |
1054 | int verbose) | |
1055 | { | |
1056 | unsigned long long ldsize; | |
1057 | int fd; | |
1058 | ||
1059 | if (level == LEVEL_CONTAINER) | |
1060 | return 0; | |
1061 | if (raiddisks > MD_SB_DISKS) | |
1062 | return 0; | |
1063 | if (size > (0x7fffffffULL<<9)) | |
1064 | return 0; | |
1065 | if (!subdev) | |
1066 | return 1; | |
1067 | ||
1068 | fd = open(subdev, O_RDONLY|O_EXCL, 0); | |
1069 | if (fd < 0) { | |
1070 | if (verbose) | |
1071 | fprintf(stderr, Name ": super0.90 cannot open %s: %s\n", | |
1072 | subdev, strerror(errno)); | |
1073 | return 0; | |
1074 | } | |
1075 | ||
1076 | if (!get_dev_size(fd, subdev, &ldsize)) { | |
1077 | close(fd); | |
1078 | return 0; | |
1079 | } | |
1080 | close(fd); | |
1081 | ||
1082 | if (ldsize < MD_RESERVED_SECTORS * 512) | |
1083 | return 0; | |
1084 | if (size > (0x7fffffffULL<<9)) | |
1085 | return 0; | |
1086 | *freesize = MD_NEW_SIZE_SECTORS(ldsize >> 9); | |
1087 | return 1; | |
1088 | } | |
1089 | #endif /* MDASSEMBLE */ | |
1090 | ||
1091 | struct superswitch super0 = { | |
1092 | #ifndef MDASSEMBLE | |
1093 | .examine_super = examine_super0, | |
1094 | .brief_examine_super = brief_examine_super0, | |
1095 | .export_examine_super = export_examine_super0, | |
1096 | .detail_super = detail_super0, | |
1097 | .brief_detail_super = brief_detail_super0, | |
1098 | .write_init_super = write_init_super0, | |
1099 | .validate_geometry = validate_geometry0, | |
1100 | .add_to_super = add_to_super0, | |
1101 | #endif | |
1102 | .match_home = match_home0, | |
1103 | .uuid_from_super = uuid_from_super0, | |
1104 | .getinfo_super = getinfo_super0, | |
1105 | .update_super = update_super0, | |
1106 | .init_super = init_super0, | |
1107 | .store_super = store_super0, | |
1108 | .compare_super = compare_super0, | |
1109 | .load_super = load_super0, | |
1110 | .match_metadata_desc = match_metadata_desc0, | |
1111 | .avail_size = avail_size0, | |
1112 | .add_internal_bitmap = add_internal_bitmap0, | |
1113 | .locate_bitmap = locate_bitmap0, | |
1114 | .write_bitmap = write_bitmap0, | |
1115 | .free_super = free_super0, | |
1116 | .name = "0.90", | |
1117 | }; |