]>
Commit | Line | Data |
---|---|---|
4b1ac34b NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
4b1ac34b NB |
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 | ||
8268ed74 | 30 | #define HAVE_STDINT_H 1 |
4b1ac34b | 31 | #include "mdadm.h" |
8268ed74 | 32 | #include "sha1.h" |
4b1ac34b NB |
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 | ||
586ed405 NB |
55 | |
56 | 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 | ||
353632d9 | 84 | } |
586ed405 | 85 | |
c7654afc | 86 | #ifndef MDASSEMBLE |
586ed405 | 87 | |
a1cbd7d0 | 88 | static void examine_super0(void *sbv, char *homehost) |
4b1ac34b NB |
89 | { |
90 | mdp_super_t *sb = sbv; | |
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); | |
a1cbd7d0 NB |
98 | if (sb->minor_version >= 90) { |
99 | printf(" UUID : %08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
4b1ac34b | 100 | sb->set_uuid2, sb->set_uuid3); |
a1cbd7d0 | 101 | if (homehost) { |
8268ed74 NB |
102 | char buf[20]; |
103 | void *hash = sha1_buffer(homehost, | |
104 | strlen(homehost), | |
105 | buf); | |
a1cbd7d0 NB |
106 | if (memcmp(&sb->set_uuid2, hash, 8)==0) |
107 | printf(" (local to host %s)", homehost); | |
108 | } | |
109 | printf("\n"); | |
110 | } else | |
4b1ac34b NB |
111 | printf(" UUID : %08x\n", sb->set_uuid0); |
112 | ||
113 | atime = sb->ctime; | |
114 | printf(" Creation Time : %.24s\n", ctime(&atime)); | |
115 | c=map_num(pers, sb->level); | |
116 | printf(" Raid Level : %s\n", c?c:"-unknown-"); | |
b674b5b8 NB |
117 | if ((int)sb->level >= 0) { |
118 | int ddsks=0; | |
4b1ac34b | 119 | printf(" Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10)); |
b674b5b8 NB |
120 | switch(sb->level) { |
121 | case 1: ddsks=1;break; | |
122 | case 4: | |
123 | case 5: ddsks = sb->raid_disks-1; break; | |
124 | case 6: ddsks = sb->raid_disks-2; break; | |
125 | case 10: ddsks = sb->raid_disks / (sb->layout&255) / ((sb->layout>>8)&255); | |
126 | } | |
127 | if (ddsks) | |
128 | printf(" Array Size : %llu%s\n", (unsigned long long)ddsks * sb->size, | |
129 | human_size(ddsks*(long long)sb->size<<10)); | |
130 | } | |
4b1ac34b NB |
131 | printf(" Raid Devices : %d\n", sb->raid_disks); |
132 | printf(" Total Devices : %d\n", sb->nr_disks); | |
133 | printf("Preferred Minor : %d\n", sb->md_minor); | |
134 | printf("\n"); | |
b674b5b8 | 135 | if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) { |
1e0d770c | 136 | printf(" Reshape pos'n : %llu%s\n", (unsigned long long)sb->reshape_position/2, human_size((long long)sb->reshape_position<<9)); |
b674b5b8 NB |
137 | if (sb->delta_disks) { |
138 | printf(" Delta Devices : %d", sb->delta_disks); | |
139 | if (sb->delta_disks) | |
140 | printf(" (%d->%d)\n", sb->raid_disks-sb->delta_disks, sb->raid_disks); | |
141 | else | |
142 | printf(" (%d->%d)\n", sb->raid_disks, sb->raid_disks+sb->delta_disks); | |
143 | } | |
144 | if (sb->new_level != sb->level) { | |
145 | c = map_num(pers, sb->new_level); | |
146 | printf(" New Level : %s\n", c?c:"-unknown-"); | |
147 | } | |
148 | if (sb->new_layout != sb->layout) { | |
149 | if (sb->level == 5) { | |
150 | c = map_num(r5layout, sb->new_layout); | |
151 | printf(" New Layout : %s\n", c?c:"-unknown-"); | |
152 | } | |
153 | if (sb->level == 10) { | |
b578481c NB |
154 | printf(" New Layout : near=%d, %s=%d\n", |
155 | sb->new_layout&255, | |
156 | (sb->new_layout&0x10000)?"offset":"far", | |
157 | (sb->new_layout>>8)&255); | |
b674b5b8 NB |
158 | } |
159 | } | |
160 | if (sb->new_chunk != sb->chunk_size) | |
161 | printf(" New Chunksize : %d\n", sb->new_chunk); | |
162 | printf("\n"); | |
163 | } | |
4b1ac34b NB |
164 | atime = sb->utime; |
165 | printf(" Update Time : %.24s\n", ctime(&atime)); | |
166 | printf(" State : %s\n", | |
167 | (sb->state&(1<<MD_SB_CLEAN))?"clean":"active"); | |
55935d51 NB |
168 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) |
169 | printf("Internal Bitmap : present\n"); | |
4b1ac34b NB |
170 | printf(" Active Devices : %d\n", sb->active_disks); |
171 | printf("Working Devices : %d\n", sb->working_disks); | |
172 | printf(" Failed Devices : %d\n", sb->failed_disks); | |
173 | printf(" Spare Devices : %d\n", sb->spare_disks); | |
174 | if (calc_sb0_csum(sb) == sb->sb_csum) | |
175 | printf(" Checksum : %x - correct\n", sb->sb_csum); | |
176 | else | |
177 | printf(" Checksum : %x - expected %lx\n", sb->sb_csum, calc_sb0_csum(sb)); | |
178 | printf(" Events : %d.%d\n", sb->events_hi, sb->events_lo); | |
179 | printf("\n"); | |
180 | if (sb->level == 5) { | |
181 | c = map_num(r5layout, sb->layout); | |
182 | printf(" Layout : %s\n", c?c:"-unknown-"); | |
183 | } | |
265e0f17 | 184 | if (sb->level == 10) { |
b578481c NB |
185 | printf(" Layout : near=%d, %s=%d\n", |
186 | sb->layout&255, | |
187 | (sb->layout&0x10000)?"offset":"far", | |
188 | (sb->layout>>8)&255); | |
265e0f17 | 189 | } |
4b1ac34b NB |
190 | switch(sb->level) { |
191 | case 0: | |
192 | case 4: | |
193 | case 5: | |
2c102711 NB |
194 | case 6: |
195 | case 10: | |
4b1ac34b NB |
196 | printf(" Chunk Size : %dK\n", sb->chunk_size/1024); |
197 | break; | |
198 | case -1: | |
199 | printf(" Rounding : %dK\n", sb->chunk_size/1024); | |
200 | break; | |
353632d9 | 201 | default: break; |
4b1ac34b NB |
202 | } |
203 | printf("\n"); | |
204 | printf(" Number Major Minor RaidDevice State\n"); | |
205 | for (d= -1; d<(signed int)(sb->raid_disks+sb->spare_disks); d++) { | |
206 | mdp_disk_t *dp; | |
207 | char *dv; | |
208 | char nb[5]; | |
dfd4d8ee | 209 | int wonly; |
4b1ac34b NB |
210 | if (d>=0) dp = &sb->disks[d]; |
211 | else dp = &sb->this_disk; | |
8f23b0b3 | 212 | snprintf(nb, sizeof(nb), "%4d", d); |
4b1ac34b NB |
213 | printf("%4s %5d %5d %5d %5d ", d < 0 ? "this" : nb, |
214 | dp->number, dp->major, dp->minor, dp->raid_disk); | |
dfd4d8ee NB |
215 | wonly = dp->state & (1<<MD_DISK_WRITEMOSTLY); |
216 | dp->state &= ~(1<<MD_DISK_WRITEMOSTLY); | |
4b1ac34b NB |
217 | if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty"); |
218 | if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active"); | |
219 | if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync"); | |
220 | if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed"); | |
dfd4d8ee | 221 | if (wonly) printf(" write-mostly"); |
4b1ac34b | 222 | if (dp->state == 0) printf(" spare"); |
16c6fa80 | 223 | if ((dv=map_dev(dp->major, dp->minor, 0))) |
4b1ac34b NB |
224 | printf(" %s", dv); |
225 | printf("\n"); | |
226 | if (d == -1) printf("\n"); | |
227 | } | |
228 | } | |
229 | ||
f9ce90ba | 230 | static void brief_examine_super0(void *sbv) |
4b1ac34b NB |
231 | { |
232 | mdp_super_t *sb = sbv; | |
233 | char *c=map_num(pers, sb->level); | |
2998bc01 | 234 | char devname[20]; |
2998bc01 NB |
235 | |
236 | sprintf(devname, "/dev/md%d", sb->md_minor); | |
4b1ac34b NB |
237 | |
238 | printf("ARRAY %s level=%s num-devices=%d UUID=", | |
2998bc01 | 239 | devname, |
4b1ac34b NB |
240 | c?c:"-unknown-", sb->raid_disks); |
241 | if (sb->minor_version >= 90) | |
242 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
243 | sb->set_uuid2, sb->set_uuid3); | |
244 | else | |
245 | printf("%08x", sb->set_uuid0); | |
246 | printf("\n"); | |
247 | } | |
248 | ||
b6750aa8 | 249 | static void detail_super0(void *sbv, char *homehost) |
4b1ac34b NB |
250 | { |
251 | mdp_super_t *sb = sbv; | |
252 | printf(" UUID : "); | |
253 | if (sb->minor_version >= 90) | |
254 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
255 | sb->set_uuid2, sb->set_uuid3); | |
256 | else | |
257 | printf("%08x", sb->set_uuid0); | |
b6750aa8 | 258 | if (homehost) { |
8268ed74 NB |
259 | char buf[20]; |
260 | void *hash = sha1_buffer(homehost, | |
261 | strlen(homehost), | |
262 | buf); | |
b6750aa8 NB |
263 | if (memcmp(&sb->set_uuid2, hash, 8)==0) |
264 | printf(" (local to host %s)", homehost); | |
265 | } | |
4b1ac34b NB |
266 | printf("\n Events : %d.%d\n\n", sb->events_hi, sb->events_lo); |
267 | } | |
268 | ||
f9ce90ba | 269 | static void brief_detail_super0(void *sbv) |
4b1ac34b NB |
270 | { |
271 | mdp_super_t *sb = sbv; | |
272 | printf(" UUID="); | |
273 | if (sb->minor_version >= 90) | |
274 | printf("%08x:%08x:%08x:%08x", sb->set_uuid0, sb->set_uuid1, | |
275 | sb->set_uuid2, sb->set_uuid3); | |
276 | else | |
277 | printf("%08x", sb->set_uuid0); | |
278 | } | |
c7654afc | 279 | #endif |
83b6208e NB |
280 | |
281 | static int match_home0(void *sbv, char *homehost) | |
282 | { | |
283 | mdp_super_t *sb = sbv; | |
8268ed74 NB |
284 | char buf[20]; |
285 | char *hash = sha1_buffer(homehost, | |
286 | strlen(homehost), | |
287 | buf); | |
83b6208e NB |
288 | |
289 | return (memcmp(&sb->set_uuid2, hash, 8)==0); | |
290 | } | |
291 | ||
f9ce90ba | 292 | static void uuid_from_super0(int uuid[4], void * sbv) |
4b1ac34b NB |
293 | { |
294 | mdp_super_t *super = sbv; | |
295 | uuid[0] = super->set_uuid0; | |
296 | if (super->minor_version >= 90) { | |
297 | uuid[1] = super->set_uuid1; | |
298 | uuid[2] = super->set_uuid2; | |
299 | uuid[3] = super->set_uuid3; | |
300 | } else { | |
301 | uuid[1] = 0; | |
302 | uuid[2] = 0; | |
303 | uuid[3] = 0; | |
304 | } | |
305 | } | |
306 | ||
31317663 | 307 | static void getinfo_super0(struct mdinfo *info, void *sbv) |
4b1ac34b NB |
308 | { |
309 | mdp_super_t *sb = sbv; | |
310 | int working = 0; | |
311 | int i; | |
312 | ||
313 | info->array.major_version = sb->major_version; | |
314 | info->array.minor_version = sb->minor_version; | |
315 | info->array.patch_version = sb->patch_version; | |
316 | info->array.raid_disks = sb->raid_disks; | |
317 | info->array.level = sb->level; | |
265e0f17 | 318 | info->array.layout = sb->layout; |
4b1ac34b NB |
319 | info->array.md_minor = sb->md_minor; |
320 | info->array.ctime = sb->ctime; | |
353632d9 NB |
321 | info->array.utime = sb->utime; |
322 | info->array.chunk_size = sb->chunk_size; | |
323 | info->component_size = sb->size*2; | |
4b1ac34b NB |
324 | |
325 | info->disk.state = sb->this_disk.state; | |
326 | info->disk.major = sb->this_disk.major; | |
327 | info->disk.minor = sb->this_disk.minor; | |
328 | info->disk.raid_disk = sb->this_disk.raid_disk; | |
fbf8a0b7 | 329 | info->disk.number = sb->this_disk.number; |
4b1ac34b NB |
330 | |
331 | info->events = md_event(sb); | |
353632d9 | 332 | info->data_offset = 0; |
4b1ac34b NB |
333 | |
334 | uuid_from_super0(info->uuid, sbv); | |
947fd4dd | 335 | |
353632d9 NB |
336 | if (sb->minor_version > 90 && (sb->reshape_position+1) != 0) { |
337 | info->reshape_active = 1; | |
338 | info->reshape_progress = sb->reshape_position; | |
339 | info->new_level = sb->new_level; | |
340 | info->delta_disks = sb->delta_disks; | |
341 | info->new_layout = sb->new_layout; | |
342 | info->new_chunk = sb->new_chunk; | |
343 | } else | |
344 | info->reshape_active = 0; | |
345 | ||
8a46fe84 | 346 | sprintf(info->name, "%d", sb->md_minor); |
4b1ac34b NB |
347 | /* work_disks is calculated rather than read directly */ |
348 | for (i=0; i < MD_SB_DISKS; i++) | |
349 | if ((sb->disks[i].state & (1<<MD_DISK_SYNC)) && | |
08110d41 | 350 | (sb->disks[i].raid_disk < info->array.raid_disks) && |
4b1ac34b NB |
351 | (sb->disks[i].state & (1<<MD_DISK_ACTIVE)) && |
352 | !(sb->disks[i].state & (1<<MD_DISK_FAULTY))) | |
353 | working ++; | |
354 | info->array.working_disks = working; | |
355 | } | |
356 | ||
357 | ||
e5eac01f NB |
358 | static int update_super0(struct mdinfo *info, void *sbv, char *update, |
359 | char *devname, int verbose, | |
360 | int uuid_set, char *homehost) | |
4b1ac34b | 361 | { |
c4d831e1 NB |
362 | /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made. |
363 | * For others, the return value is ignored. | |
364 | */ | |
4b1ac34b NB |
365 | int rv = 0; |
366 | mdp_super_t *sb = sbv; | |
367 | if (strcmp(update, "sparc2.2")==0 ) { | |
368 | /* 2.2 sparc put the events in the wrong place | |
369 | * So we copy the tail of the superblock | |
370 | * up 4 bytes before continuing | |
371 | */ | |
372 | __u32 *sb32 = (__u32*)sb; | |
373 | memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7, | |
374 | sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1, | |
375 | (MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1))*4); | |
dab6685f NB |
376 | if (verbose >= 0) |
377 | fprintf (stderr, Name ": adjusting superblock of %s for 2.2/sparc compatability.\n", | |
378 | devname); | |
4b1ac34b NB |
379 | } |
380 | if (strcmp(update, "super-minor") ==0) { | |
381 | sb->md_minor = info->array.md_minor; | |
dab6685f | 382 | if (verbose > 0) |
4b1ac34b NB |
383 | fprintf(stderr, Name ": updating superblock of %s with minor number %d\n", |
384 | devname, info->array.md_minor); | |
385 | } | |
386 | if (strcmp(update, "summaries") == 0) { | |
387 | int i; | |
388 | /* set nr_disks, active_disks, working_disks, | |
389 | * failed_disks, spare_disks based on disks[] | |
390 | * array in superblock. | |
391 | * Also make sure extra slots aren't 'failed' | |
392 | */ | |
393 | sb->nr_disks = sb->active_disks = | |
394 | sb->working_disks = sb->failed_disks = | |
395 | sb->spare_disks = 0; | |
396 | for (i=0; i < MD_SB_DISKS ; i++) | |
397 | if (sb->disks[i].major || | |
398 | sb->disks[i].minor) { | |
399 | int state = sb->disks[i].state; | |
400 | if (state & (1<<MD_DISK_REMOVED)) | |
401 | continue; | |
402 | sb->nr_disks++; | |
403 | if (state & (1<<MD_DISK_ACTIVE)) | |
404 | sb->active_disks++; | |
405 | if (state & (1<<MD_DISK_FAULTY)) | |
406 | sb->failed_disks++; | |
407 | else | |
408 | sb->working_disks++; | |
409 | if (state == 0) | |
410 | sb->spare_disks++; | |
411 | } else if (i >= sb->raid_disks && sb->disks[i].number == 0) | |
412 | sb->disks[i].state = 0; | |
413 | } | |
67a8c82d NB |
414 | if (strcmp(update, "force-one")==0) { |
415 | /* Not enough devices for a working array, so | |
416 | * bring this one up-to-date. | |
417 | */ | |
c4d831e1 | 418 | __u32 ehi = sb->events_hi, elo = sb->events_lo; |
4b1ac34b NB |
419 | sb->events_hi = (info->events>>32) & 0xFFFFFFFF; |
420 | sb->events_lo = (info->events) & 0xFFFFFFFF; | |
c4d831e1 NB |
421 | if (sb->events_hi != ehi || |
422 | sb->events_lo != elo) | |
423 | rv = 1; | |
67a8c82d NB |
424 | } |
425 | if (strcmp(update, "force-array")==0) { | |
426 | /* degraded array and 'force' requested, so | |
427 | * maybe need to mark it 'clean' | |
428 | */ | |
c4d831e1 NB |
429 | if ((sb->level == 5 || sb->level == 4 || sb->level == 6) && |
430 | (sb->state & (1 << MD_SB_CLEAN)) == 0) { | |
4b1ac34b NB |
431 | /* need to force clean */ |
432 | sb->state |= (1 << MD_SB_CLEAN); | |
c4d831e1 NB |
433 | rv = 1; |
434 | } | |
4b1ac34b NB |
435 | } |
436 | if (strcmp(update, "assemble")==0) { | |
437 | int d = info->disk.number; | |
dfd4d8ee | 438 | int wonly = sb->disks[d].state & (1<<MD_DISK_WRITEMOSTLY); |
c4d831e1 NB |
439 | if ((sb->disks[d].state & ~(1<<MD_DISK_WRITEMOSTLY)) |
440 | != info->disk.state) { | |
441 | sb->disks[d].state = info->disk.state | wonly; | |
4b1ac34b NB |
442 | rv = 1; |
443 | } | |
444 | } | |
4b1ac34b NB |
445 | if (strcmp(update, "grow") == 0) { |
446 | sb->raid_disks = info->array.raid_disks; | |
447 | sb->nr_disks = info->array.nr_disks; | |
448 | sb->active_disks = info->array.active_disks; | |
449 | sb->working_disks = info->array.working_disks; | |
450 | memset(&sb->disks[info->disk.number], 0, sizeof(sb->disks[0])); | |
451 | sb->disks[info->disk.number].number = info->disk.number; | |
452 | sb->disks[info->disk.number].major = info->disk.major; | |
453 | sb->disks[info->disk.number].minor = info->disk.minor; | |
454 | sb->disks[info->disk.number].raid_disk = info->disk.raid_disk; | |
455 | sb->disks[info->disk.number].state = info->disk.state; | |
456 | if (sb->this_disk.number == info->disk.number) | |
457 | sb->this_disk = sb->disks[info->disk.number]; | |
458 | } | |
459 | if (strcmp(update, "resync") == 0) { | |
460 | /* make sure resync happens */ | |
461 | sb->state &= ~(1<<MD_SB_CLEAN); | |
462 | sb->recovery_cp = 0; | |
463 | } | |
0237e0ca NB |
464 | if (strcmp(update, "homehost") == 0 && |
465 | homehost) { | |
466 | uuid_set = 0; | |
467 | update = "uuid"; | |
468 | info->uuid[0] = sb->set_uuid0; | |
469 | info->uuid[1] = sb->set_uuid1; | |
470 | } | |
7d99579f | 471 | if (strcmp(update, "uuid") == 0) { |
e5eac01f | 472 | if (!uuid_set && homehost) { |
8268ed74 NB |
473 | char buf[20]; |
474 | char *hash = sha1_buffer(homehost, | |
475 | strlen(homehost), | |
476 | buf); | |
e5eac01f NB |
477 | memcpy(info->uuid+2, hash, 8); |
478 | } | |
7d99579f NB |
479 | sb->set_uuid0 = info->uuid[0]; |
480 | sb->set_uuid1 = info->uuid[1]; | |
481 | sb->set_uuid2 = info->uuid[2]; | |
482 | sb->set_uuid3 = info->uuid[3]; | |
14263cf1 NB |
483 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) { |
484 | struct bitmap_super_s *bm; | |
485 | bm = (struct bitmap_super_s*)(sb+1); | |
486 | uuid_from_super0((int*)bm->uuid, sbv); | |
487 | } | |
7d99579f | 488 | } |
353632d9 NB |
489 | if (strcmp(update, "_reshape_progress")==0) |
490 | sb->reshape_position = info->reshape_progress; | |
4b1ac34b NB |
491 | |
492 | sb->sb_csum = calc_sb0_csum(sb); | |
493 | return rv; | |
494 | } | |
495 | ||
05697ec1 NB |
496 | /* |
497 | * For verion-0 superblock, the homehost is 'stored' in the | |
498 | * uuid. 8 bytes for a hash of the host leaving 8 bytes | |
499 | * of random material. | |
500 | * We use the first 8 bytes (64bits) of the sha1 of the | |
501 | * host name | |
502 | */ | |
4b1ac34b NB |
503 | |
504 | ||
05697ec1 NB |
505 | static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info, |
506 | unsigned long long size, char *ignored_name, char *homehost) | |
4b1ac34b | 507 | { |
55935d51 | 508 | mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t)); |
82d9eba6 | 509 | int spares; |
dfe47e00 | 510 | int rfd; |
55935d51 | 511 | memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t)); |
4b1ac34b | 512 | |
f9ce90ba NB |
513 | if (info->major_version == -1) { |
514 | /* zeroing the superblock */ | |
05697ec1 | 515 | *sbp = sb; |
82d9eba6 NB |
516 | return 0; |
517 | } | |
518 | ||
519 | spares = info->working_disks - info->active_disks; | |
520 | if (info->raid_disks + spares > MD_SB_DISKS) { | |
521 | fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n", | |
522 | info->raid_disks , spares, MD_SB_DISKS); | |
523 | return 0; | |
f9ce90ba NB |
524 | } |
525 | ||
dfe47e00 | 526 | rfd = open("/dev/urandom", O_RDONLY); |
4b1ac34b NB |
527 | sb->md_magic = MD_SB_MAGIC; |
528 | sb->major_version = 0; | |
529 | sb->minor_version = 90; | |
530 | sb->patch_version = 0; | |
531 | sb->gvalid_words = 0; /* ignored */ | |
dfe47e00 NB |
532 | if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4) |
533 | sb->set_uuid0 = random(); | |
4b1ac34b NB |
534 | sb->ctime = time(0); |
535 | sb->level = info->level; | |
5dd497ee NB |
536 | if (size != info->size) |
537 | return 0; | |
4b1ac34b NB |
538 | sb->size = info->size; |
539 | sb->nr_disks = info->nr_disks; | |
540 | sb->raid_disks = info->raid_disks; | |
541 | sb->md_minor = info->md_minor; | |
542 | sb->not_persistent = 0; | |
dfe47e00 NB |
543 | if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) { |
544 | sb->set_uuid1 = random(); | |
545 | sb->set_uuid2 = random(); | |
546 | sb->set_uuid3 = random(); | |
547 | } | |
548 | if (rfd >= 0) | |
549 | close(rfd); | |
05697ec1 | 550 | if (homehost) { |
8268ed74 NB |
551 | char buf[20]; |
552 | char *hash = sha1_buffer(homehost, | |
553 | strlen(homehost), | |
554 | buf); | |
05697ec1 NB |
555 | memcpy(&sb->set_uuid2, hash, 8); |
556 | } | |
4b1ac34b NB |
557 | |
558 | sb->utime = sb->ctime; | |
559 | sb->state = info->state; | |
560 | sb->active_disks = info->active_disks; | |
561 | sb->working_disks = info->working_disks; | |
562 | sb->failed_disks = info->failed_disks; | |
234a131a | 563 | sb->spare_disks = info->spare_disks; |
4b1ac34b NB |
564 | sb->events_hi = 0; |
565 | sb->events_lo = 1; | |
566 | ||
567 | sb->layout = info->layout; | |
568 | sb->chunk_size = info->chunk_size; | |
569 | ||
570 | *sbp = sb; | |
82d9eba6 | 571 | return 1; |
4b1ac34b NB |
572 | } |
573 | ||
574 | /* Add a device to the superblock being created */ | |
f9ce90ba | 575 | static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo) |
4b1ac34b NB |
576 | { |
577 | mdp_super_t *sb = sbv; | |
578 | mdp_disk_t *dk = &sb->disks[dinfo->number]; | |
353632d9 | 579 | |
4b1ac34b NB |
580 | dk->number = dinfo->number; |
581 | dk->major = dinfo->major; | |
582 | dk->minor = dinfo->minor; | |
583 | dk->raid_disk = dinfo->raid_disk; | |
584 | dk->state = dinfo->state; | |
585 | } | |
586 | ||
96395475 | 587 | static int store_super0(struct supertype *st, int fd, void *sbv) |
4b1ac34b NB |
588 | { |
589 | unsigned long size; | |
590 | unsigned long long dsize; | |
591 | unsigned long long offset; | |
f9ce90ba | 592 | mdp_super_t *super = sbv; |
4b1ac34b NB |
593 | |
594 | #ifdef BLKGETSIZE64 | |
595 | if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) | |
596 | #endif | |
597 | { | |
598 | if (ioctl(fd, BLKGETSIZE, &size)) | |
599 | return 1; | |
600 | else | |
601 | dsize = ((unsigned long long)size)<<9; | |
602 | } | |
603 | ||
5dd497ee | 604 | if (dsize < MD_RESERVED_SECTORS*2*512) |
4b1ac34b | 605 | return 2; |
353632d9 | 606 | |
4b1ac34b NB |
607 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); |
608 | ||
609 | offset *= 512; | |
610 | ||
611 | if (lseek64(fd, offset, 0)< 0LL) | |
612 | return 3; | |
613 | ||
614 | if (write(fd, super, sizeof(*super)) != sizeof(*super)) | |
615 | return 4; | |
616 | ||
14263cf1 NB |
617 | if (super->state & (1<<MD_SB_BITMAP_PRESENT)) { |
618 | struct bitmap_super_s * bm = (struct bitmap_super_s*)(super+1); | |
619 | if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) | |
9fca7d62 NB |
620 | if (write(fd, bm, sizeof(*bm)) != sizeof(*bm)) |
621 | return 5; | |
14263cf1 NB |
622 | } |
623 | ||
570c0542 | 624 | fsync(fd); |
4b1ac34b NB |
625 | return 0; |
626 | } | |
627 | ||
1bf4e2d9 | 628 | static int write_init_super0(struct supertype *st, void *sbv, mdu_disk_info_t *dinfo, char *devname) |
4b1ac34b NB |
629 | { |
630 | mdp_super_t *sb = sbv; | |
dfd4d8ee | 631 | int fd = open(devname, O_RDWR|O_EXCL); |
4b1ac34b NB |
632 | int rv; |
633 | ||
634 | if (fd < 0) { | |
635 | fprintf(stderr, Name ": Failed to open %s to write superblock\n", devname); | |
636 | return -1; | |
637 | } | |
638 | ||
8d75b7fc | 639 | sb->disks[dinfo->number].state &= ~(1<<MD_DISK_FAULTY); |
8d75b7fc | 640 | |
4b1ac34b NB |
641 | sb->this_disk = sb->disks[dinfo->number]; |
642 | sb->sb_csum = calc_sb0_csum(sb); | |
96395475 | 643 | rv = store_super0(st, fd, sb); |
55935d51 | 644 | |
1bf4e2d9 NB |
645 | if (rv == 0 && (sb->state & (1<<MD_SB_BITMAP_PRESENT))) |
646 | rv = st->ss->write_bitmap(st, fd, sbv); | |
55935d51 | 647 | |
4b1ac34b NB |
648 | close(fd); |
649 | if (rv) | |
650 | fprintf(stderr, Name ": failed to write superblock to %s\n", devname); | |
651 | return rv; | |
652 | } | |
653 | ||
f9ce90ba | 654 | static int compare_super0(void **firstp, void *secondv) |
4b1ac34b NB |
655 | { |
656 | /* | |
657 | * return: | |
658 | * 0 same, or first was empty, and second was copied | |
659 | * 1 second had wrong number | |
660 | * 2 wrong uuid | |
661 | * 3 wrong other info | |
662 | */ | |
663 | mdp_super_t *first = *firstp; | |
664 | mdp_super_t *second = secondv; | |
665 | ||
666 | int uuid1[4], uuid2[4]; | |
667 | if (second->md_magic != MD_SB_MAGIC) | |
668 | return 1; | |
669 | if (!first) { | |
14263cf1 NB |
670 | first = malloc(MD_SB_BYTES + sizeof(struct bitmap_super_s)); |
671 | memcpy(first, second, MD_SB_BYTES + sizeof(struct bitmap_super_s)); | |
4b1ac34b NB |
672 | *firstp = first; |
673 | return 0; | |
674 | } | |
675 | ||
676 | uuid_from_super0(uuid1, first); | |
677 | uuid_from_super0(uuid2, second); | |
f277ce36 | 678 | if (!same_uuid(uuid1, uuid2, 0)) |
4b1ac34b NB |
679 | return 2; |
680 | if (first->major_version != second->major_version || | |
681 | first->minor_version != second->minor_version || | |
682 | first->patch_version != second->patch_version || | |
683 | first->gvalid_words != second->gvalid_words || | |
684 | first->ctime != second->ctime || | |
685 | first->level != second->level || | |
686 | first->size != second->size || | |
687 | first->raid_disks != second->raid_disks ) | |
688 | return 3; | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
693 | ||
82d9eba6 | 694 | static int load_super0(struct supertype *st, int fd, void **sbp, char *devname) |
4b1ac34b NB |
695 | { |
696 | /* try to read in the superblock | |
697 | * Return: | |
698 | * 0 on success | |
699 | * 1 on cannot get superblock | |
700 | * 2 on superblock meaningless | |
701 | */ | |
702 | unsigned long size; | |
703 | unsigned long long dsize; | |
704 | unsigned long long offset; | |
705 | mdp_super_t *super; | |
14263cf1 NB |
706 | int uuid[4]; |
707 | struct bitmap_super_s *bsb; | |
4b1ac34b NB |
708 | |
709 | #ifdef BLKGETSIZE64 | |
710 | if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) | |
711 | #endif | |
712 | { | |
713 | if (ioctl(fd, BLKGETSIZE, &size)) { | |
714 | if (devname) | |
715 | fprintf(stderr, Name ": cannot find device size for %s: %s\n", | |
716 | devname, strerror(errno)); | |
717 | return 1; | |
718 | } else | |
719 | dsize = size << 9; | |
720 | } | |
721 | ||
722 | if (dsize < MD_RESERVED_SECTORS*2) { | |
723 | if (devname) | |
724 | fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n", | |
725 | devname, size); | |
726 | return 1; | |
727 | } | |
353632d9 | 728 | |
4b1ac34b NB |
729 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); |
730 | ||
731 | offset *= 512; | |
732 | ||
733 | ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */ | |
734 | ||
735 | if (lseek64(fd, offset, 0)< 0LL) { | |
736 | if (devname) | |
737 | fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n", | |
738 | devname, strerror(errno)); | |
739 | return 1; | |
740 | } | |
741 | ||
8d75b7fc | 742 | super = malloc(MD_SB_BYTES + sizeof(bitmap_super_t)); |
4b1ac34b NB |
743 | |
744 | if (read(fd, super, sizeof(*super)) != MD_SB_BYTES) { | |
745 | if (devname) | |
746 | fprintf(stderr, Name ": Cannot read superblock on %s\n", | |
747 | devname); | |
748 | free(super); | |
749 | return 1; | |
750 | } | |
751 | ||
586ed405 NB |
752 | if (st->ss && st->minor_version == 9) |
753 | super0_swap_endian(super); | |
754 | ||
4b1ac34b NB |
755 | if (super->md_magic != MD_SB_MAGIC) { |
756 | if (devname) | |
757 | fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n", | |
758 | devname, MD_SB_MAGIC, super->md_magic); | |
759 | free(super); | |
760 | return 2; | |
761 | } | |
762 | ||
763 | if (super->major_version != 0) { | |
764 | if (devname) | |
765 | fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n", | |
766 | devname, super->major_version); | |
767 | free(super); | |
768 | return 2; | |
769 | } | |
770 | *sbp = super; | |
82d9eba6 NB |
771 | if (st->ss == NULL) { |
772 | st->ss = &super0; | |
773 | st->minor_version = 90; | |
ea329559 | 774 | st->max_devs = MD_SB_DISKS; |
82d9eba6 NB |
775 | } |
776 | ||
14263cf1 NB |
777 | /* Now check on the bitmap superblock */ |
778 | if ((super->state & (1<<MD_SB_BITMAP_PRESENT)) == 0) | |
779 | return 0; | |
780 | /* Read the bitmap superblock and make sure it looks | |
781 | * valid. If it doesn't clear the bit. An --assemble --force | |
782 | * should get that written out. | |
783 | */ | |
784 | if (read(fd, super+1, sizeof(struct bitmap_super_s)) | |
785 | != sizeof(struct bitmap_super_s)) | |
786 | goto no_bitmap; | |
787 | ||
788 | uuid_from_super0(uuid, super); | |
789 | bsb = (struct bitmap_super_s *)(super+1); | |
790 | if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC || | |
791 | memcmp(bsb->uuid, uuid, 16) != 0) | |
792 | goto no_bitmap; | |
793 | return 0; | |
794 | ||
795 | no_bitmap: | |
796 | super->state &= ~(1<<MD_SB_BITMAP_PRESENT); | |
797 | ||
4b1ac34b NB |
798 | return 0; |
799 | } | |
f9ce90ba | 800 | |
82d9eba6 | 801 | static struct supertype *match_metadata_desc0(char *arg) |
f9ce90ba | 802 | { |
82d9eba6 NB |
803 | struct supertype *st = malloc(sizeof(*st)); |
804 | if (!st) return st; | |
805 | ||
806 | st->ss = &super0; | |
807 | st->minor_version = 90; | |
ea329559 | 808 | st->max_devs = MD_SB_DISKS; |
f9ce90ba NB |
809 | if (strcmp(arg, "0") == 0 || |
810 | strcmp(arg, "0.90") == 0 || | |
811 | strcmp(arg, "default") == 0 | |
812 | ) | |
82d9eba6 NB |
813 | return st; |
814 | ||
586ed405 NB |
815 | st->minor_version = 9; /* flag for 'byte-swapped' */ |
816 | if (strcmp(arg, "0.swap")==0) | |
817 | return st; | |
818 | ||
82d9eba6 NB |
819 | free(st); |
820 | return NULL; | |
821 | } | |
822 | ||
1bf4e2d9 | 823 | static __u64 avail_size0(struct supertype *st, __u64 devsize) |
82d9eba6 NB |
824 | { |
825 | if (devsize < MD_RESERVED_SECTORS*2) | |
826 | return 0ULL; | |
827 | return MD_NEW_SIZE_SECTORS(devsize); | |
f9ce90ba NB |
828 | } |
829 | ||
f9c25f1d | 830 | static int add_internal_bitmap0(struct supertype *st, void *sbv, int chunk, int delay, int write_behind, unsigned long long size, int may_change, int major) |
55935d51 NB |
831 | { |
832 | /* | |
833 | * The bitmap comes immediately after the superblock and must be 60K in size | |
834 | * at most. The default size is between 30K and 60K | |
835 | * | |
8686f3ed | 836 | * size is in sectors, chunk is in bytes !!! |
55935d51 | 837 | */ |
1bf4e2d9 | 838 | unsigned long long bits; |
55935d51 NB |
839 | unsigned long long max_bits = 60*1024*8; |
840 | unsigned long long min_chunk; | |
841 | mdp_super_t *sb = sbv; | |
842 | bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + MD_SB_BYTES); | |
843 | ||
353632d9 | 844 | |
f277ce36 | 845 | min_chunk = 4096; /* sub-page chunks don't work yet.. */ |
8686f3ed | 846 | bits = (size * 512) / min_chunk + 1; |
55935d51 NB |
847 | while (bits > max_bits) { |
848 | min_chunk *= 2; | |
849 | bits = (bits+1)/2; | |
850 | } | |
851 | if (chunk == UnSet) | |
852 | chunk = min_chunk; | |
853 | else if (chunk < min_chunk) | |
854 | return 0; /* chunk size too small */ | |
855 | ||
856 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); | |
857 | ||
29e766a5 | 858 | memset(bms, 0, sizeof(*bms)); |
dfd4d8ee | 859 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); |
dcec9ee5 | 860 | bms->version = __cpu_to_le32(major); |
55935d51 | 861 | uuid_from_super0((int*)bms->uuid, sb); |
dfd4d8ee NB |
862 | bms->chunksize = __cpu_to_le32(chunk); |
863 | bms->daemon_sleep = __cpu_to_le32(delay); | |
f9c25f1d | 864 | bms->sync_size = __cpu_to_le64(size); |
dfd4d8ee | 865 | bms->write_behind = __cpu_to_le32(write_behind); |
55935d51 | 866 | |
55935d51 NB |
867 | return 1; |
868 | } | |
353632d9 | 869 | |
55935d51 | 870 | |
f6d75de8 | 871 | void locate_bitmap0(struct supertype *st, int fd, void *sbv) |
55935d51 NB |
872 | { |
873 | unsigned long long dsize; | |
874 | unsigned long size; | |
875 | unsigned long long offset; | |
876 | #ifdef BLKGETSIZE64 | |
877 | if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) | |
878 | #endif | |
879 | { | |
880 | if (ioctl(fd, BLKGETSIZE, &size)) | |
881 | return; | |
882 | else | |
883 | dsize = ((unsigned long long)size)<<9; | |
884 | } | |
885 | ||
886 | if (dsize < MD_RESERVED_SECTORS*2) | |
887 | return; | |
353632d9 | 888 | |
55935d51 NB |
889 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); |
890 | ||
891 | offset *= 512; | |
892 | ||
893 | offset += MD_SB_BYTES; | |
894 | ||
895 | lseek64(fd, offset, 0); | |
896 | } | |
897 | ||
8d75b7fc NB |
898 | int write_bitmap0(struct supertype *st, int fd, void *sbv) |
899 | { | |
900 | unsigned long size; | |
901 | unsigned long long dsize; | |
902 | unsigned long long offset; | |
903 | mdp_super_t *sb = sbv; | |
904 | ||
905 | int rv = 0; | |
906 | ||
907 | int towrite, n; | |
908 | char buf[4096]; | |
909 | ||
910 | #ifdef BLKGETSIZE64 | |
911 | if (ioctl(fd, BLKGETSIZE64, &dsize) != 0) | |
912 | #endif | |
913 | { | |
914 | if (ioctl(fd, BLKGETSIZE, &size)) | |
915 | return 1; | |
916 | else | |
917 | dsize = ((unsigned long long)size)<<9; | |
918 | } | |
919 | ||
920 | if (dsize < MD_RESERVED_SECTORS*2) | |
353632d9 NB |
921 | return -1; |
922 | ||
8d75b7fc NB |
923 | offset = MD_NEW_SIZE_SECTORS(dsize>>9); |
924 | ||
925 | offset *= 512; | |
926 | ||
927 | if (lseek64(fd, offset + 4096, 0)< 0LL) | |
928 | return 3; | |
929 | ||
930 | ||
1e0d770c NB |
931 | if (write(fd, ((char*)sb)+MD_SB_BYTES, sizeof(bitmap_super_t)) != |
932 | sizeof(bitmap_super_t)) | |
933 | return -2; | |
8d75b7fc NB |
934 | towrite = 64*1024 - MD_SB_BYTES - sizeof(bitmap_super_t); |
935 | memset(buf, 0xff, sizeof(buf)); | |
936 | while (towrite > 0) { | |
937 | n = towrite; | |
938 | if (n > sizeof(buf)) | |
939 | n = sizeof(buf); | |
940 | n = write(fd, buf, n); | |
941 | if (n > 0) | |
942 | towrite -= n; | |
943 | else | |
944 | break; | |
945 | } | |
dfd4d8ee | 946 | fsync(fd); |
8d75b7fc NB |
947 | if (towrite) |
948 | rv = -2; | |
949 | ||
950 | return rv; | |
951 | } | |
55935d51 | 952 | |
f9ce90ba | 953 | struct superswitch super0 = { |
c7654afc | 954 | #ifndef MDASSEMBLE |
f9ce90ba NB |
955 | .examine_super = examine_super0, |
956 | .brief_examine_super = brief_examine_super0, | |
957 | .detail_super = detail_super0, | |
958 | .brief_detail_super = brief_detail_super0, | |
c7654afc | 959 | #endif |
83b6208e | 960 | .match_home = match_home0, |
f9ce90ba NB |
961 | .uuid_from_super = uuid_from_super0, |
962 | .getinfo_super = getinfo_super0, | |
963 | .update_super = update_super0, | |
f9ce90ba NB |
964 | .init_super = init_super0, |
965 | .add_to_super = add_to_super0, | |
966 | .store_super = store_super0, | |
967 | .write_init_super = write_init_super0, | |
968 | .compare_super = compare_super0, | |
969 | .load_super = load_super0, | |
970 | .match_metadata_desc = match_metadata_desc0, | |
82d9eba6 | 971 | .avail_size = avail_size0, |
55935d51 NB |
972 | .add_internal_bitmap = add_internal_bitmap0, |
973 | .locate_bitmap = locate_bitmap0, | |
8d75b7fc | 974 | .write_bitmap = write_bitmap0, |
82d9eba6 | 975 | .major = 0, |
f277ce36 | 976 | .swapuuid = 0, |
f9ce90ba | 977 | }; |