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