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