]>
Commit | Line | Data |
---|---|---|
82d9eba6 NB |
1 | /* |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
82d9eba6 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 | ||
30 | #include "mdadm.h" | |
82d9eba6 NB |
31 | /* |
32 | * The version-1 superblock : | |
33 | * All numeric fields are little-endian. | |
34 | * | |
35 | * total size: 256 bytes plus 2 per device. | |
36 | * 1K allows 384 devices. | |
37 | */ | |
38 | struct mdp_superblock_1 { | |
39 | /* constant array information - 128 bytes */ | |
40 | __u32 magic; /* MD_SB_MAGIC: 0xa92b4efc - little endian */ | |
41 | __u32 major_version; /* 1 */ | |
42 | __u32 feature_map; /* 0 for now */ | |
43 | __u32 pad0; /* always set to 0 when writing */ | |
44 | ||
45 | __u8 set_uuid[16]; /* user-space generated. */ | |
46 | char set_name[32]; /* set and interpreted by user-space */ | |
47 | ||
48 | __u64 ctime; /* lo 40 bits are seconds, top 24 are microseconds or 0*/ | |
49 | __u32 level; /* -4 (multipath), -1 (linear), 0,1,4,5 */ | |
50 | __u32 layout; /* only for raid5 currently */ | |
51 | __u64 size; /* used size of component devices, in 512byte sectors */ | |
52 | ||
53 | __u32 chunksize; /* in 512byte sectors */ | |
54 | __u32 raid_disks; | |
34163fc7 NB |
55 | __u32 bitmap_offset; /* sectors after start of superblock that bitmap starts |
56 | * NOTE: signed, so bitmap can be before superblock | |
57 | * only meaningful of feature_map[0] is set. | |
58 | */ | |
b674b5b8 NB |
59 | |
60 | /* These are only valid with feature bit '4' */ | |
b674b5b8 | 61 | __u32 new_level; /* new level we are reshaping to */ |
ac957baa | 62 | __u64 reshape_position; /* next address in array-space for reshape */ |
b674b5b8 NB |
63 | __u32 delta_disks; /* change in number of raid_disks */ |
64 | __u32 new_layout; /* new layout */ | |
65 | __u32 new_chunk; /* new chunk size (bytes) */ | |
66 | __u8 pad1[128-124]; /* set to 0 when written */ | |
82d9eba6 NB |
67 | |
68 | /* constant this-device information - 64 bytes */ | |
69 | __u64 data_offset; /* sector start of data, often 0 */ | |
70 | __u64 data_size; /* sectors in this device that can be used for data */ | |
71 | __u64 super_offset; /* sector start of this superblock */ | |
72 | __u64 recovery_offset;/* sectors before this offset (from data_offset) have been recovered */ | |
73 | __u32 dev_number; /* permanent identifier of this device - not role in raid */ | |
74 | __u32 cnt_corrected_read; /* number of read errors that were corrected by re-writing */ | |
75 | __u8 device_uuid[16]; /* user-space setable, ignored by kernel */ | |
dfd4d8ee NB |
76 | __u8 devflags; /* per-device flags. Only one defined...*/ |
77 | #define WriteMostly1 1 /* mask for writemostly flag in above */ | |
78 | __u8 pad2[64-57]; /* set to 0 when writing */ | |
82d9eba6 NB |
79 | |
80 | /* array state information - 64 bytes */ | |
81 | __u64 utime; /* 40 bits second, 24 btes microseconds */ | |
82 | __u64 events; /* incremented when superblock updated */ | |
83 | __u64 resync_offset; /* data before this offset (from data_offset) known to be in sync */ | |
84 | __u32 sb_csum; /* checksum upto devs[max_dev] */ | |
85 | __u32 max_dev; /* size of devs[] array to consider */ | |
86 | __u8 pad3[64-32]; /* set to 0 when writing */ | |
87 | ||
88 | /* device state information. Indexed by dev_number. | |
89 | * 2 bytes per device | |
90 | * Note there are no per-device state flags. State information is rolled | |
91 | * into the 'roles' value. If a device is spare or faulty, then it doesn't | |
92 | * have a meaningful role. | |
93 | */ | |
94 | __u16 dev_roles[0]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */ | |
95 | }; | |
96 | ||
bee8ec56 NB |
97 | struct misc_dev_info { |
98 | __u64 device_size; | |
99 | }; | |
100 | ||
d2cd3ffc NB |
101 | /* feature_map bits */ |
102 | #define MD_FEATURE_BITMAP_OFFSET 1 | |
103 | #define MD_FEATURE_RECOVERY_OFFSET 2 /* recovery_offset is present and | |
104 | * must be honoured | |
105 | */ | |
b674b5b8 | 106 | #define MD_FEATURE_RESHAPE_ACTIVE 4 |
d2cd3ffc | 107 | |
b674b5b8 | 108 | #define MD_FEATURE_ALL (1|2|4) |
d2cd3ffc | 109 | |
570c0542 | 110 | #ifndef offsetof |
067db4df | 111 | #define offsetof(t,f) ((size_t)&(((t*)0)->f)) |
570c0542 | 112 | #endif |
82d9eba6 NB |
113 | static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb) |
114 | { | |
115 | unsigned int disk_csum, csum; | |
116 | unsigned long long newcsum; | |
117 | int size = sizeof(*sb) + __le32_to_cpu(sb->max_dev)*2; | |
118 | unsigned int *isuper = (unsigned int*)sb; | |
119 | int i; | |
120 | ||
121 | /* make sure I can count... */ | |
122 | if (offsetof(struct mdp_superblock_1,data_offset) != 128 || | |
123 | offsetof(struct mdp_superblock_1, utime) != 192 || | |
124 | sizeof(struct mdp_superblock_1) != 256) { | |
125 | fprintf(stderr, "WARNING - superblock isn't sized correctly\n"); | |
126 | } | |
127 | ||
128 | disk_csum = sb->sb_csum; | |
129 | sb->sb_csum = 0; | |
130 | newcsum = 0; | |
722966c6 PC |
131 | for (i=0; size>=4; size -= 4 ) { |
132 | newcsum += __le32_to_cpu(*isuper); | |
133 | isuper++; | |
134 | } | |
82d9eba6 NB |
135 | |
136 | if (size == 2) | |
137 | newcsum += __le16_to_cpu(*(unsigned short*) isuper); | |
138 | ||
139 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); | |
140 | sb->sb_csum = disk_csum; | |
83205b64 | 141 | return __cpu_to_le32(csum); |
82d9eba6 NB |
142 | } |
143 | ||
c7654afc | 144 | #ifndef MDASSEMBLE |
a1cbd7d0 | 145 | static void examine_super1(void *sbv, char *homehost) |
82d9eba6 NB |
146 | { |
147 | struct mdp_superblock_1 *sb = sbv; | |
148 | time_t atime; | |
149 | int d; | |
6fbba4c9 | 150 | int faulty; |
82d9eba6 NB |
151 | int i; |
152 | char *c; | |
a1cbd7d0 | 153 | int l = homehost ? strlen(homehost) : 0; |
83205b64 | 154 | int layout; |
82d9eba6 NB |
155 | |
156 | printf(" Magic : %08x\n", __le32_to_cpu(sb->magic)); | |
91eedefc NB |
157 | printf(" Version : %02d\n", 1); |
158 | printf(" Feature Map : 0x%x\n", __le32_to_cpu(sb->feature_map)); | |
82d9eba6 NB |
159 | printf(" Array UUID : "); |
160 | for (i=0; i<16; i++) { | |
82d9eba6 | 161 | if ((i&3)==0 && i != 0) printf(":"); |
34163fc7 | 162 | printf("%02x", sb->set_uuid[i]); |
82d9eba6 NB |
163 | } |
164 | printf("\n"); | |
a1cbd7d0 NB |
165 | printf(" Name : %.32s", sb->set_name); |
166 | if (l > 0 && l < 32 && | |
167 | sb->set_name[l] == ':' && | |
168 | strncmp(sb->set_name, homehost, l) == 0) | |
169 | printf(" (local to host %s)", homehost); | |
170 | printf("\n"); | |
82d9eba6 NB |
171 | atime = __le64_to_cpu(sb->ctime) & 0xFFFFFFFFFFULL; |
172 | printf(" Creation Time : %.24s\n", ctime(&atime)); | |
173 | c=map_num(pers, __le32_to_cpu(sb->level)); | |
174 | printf(" Raid Level : %s\n", c?c:"-unknown-"); | |
175 | printf(" Raid Devices : %d\n", __le32_to_cpu(sb->raid_disks)); | |
176 | printf("\n"); | |
e3362544 NB |
177 | printf(" Used Dev Size : %llu%s\n", |
178 | (unsigned long long)sb->data_size, | |
179 | human_size(sb->data_size<<9)); | |
b674b5b8 NB |
180 | if (__le32_to_cpu(sb->level) >= 0) { |
181 | int ddsks=0; | |
182 | switch(__le32_to_cpu(sb->level)) { | |
183 | case 1: ddsks=1;break; | |
184 | case 4: | |
83205b64 NB |
185 | case 5: ddsks = __le32_to_cpu(sb->raid_disks)-1; break; |
186 | case 6: ddsks = __le32_to_cpu(sb->raid_disks)-2; break; | |
187 | case 10: | |
188 | layout = __le32_to_cpu(sb->layout); | |
189 | ddsks = __le32_to_cpu(sb->raid_disks) | |
190 | / (layout&255) / ((layout>>8)&255); | |
b674b5b8 NB |
191 | } |
192 | if (ddsks) | |
83205b64 | 193 | printf(" Array Size : %llu%s\n", |
c1c05f7f | 194 | ddsks*(unsigned long long)__le64_to_cpu(sb->size), |
83205b64 | 195 | human_size(ddsks*__le64_to_cpu(sb->size)<<9)); |
b674b5b8 | 196 | if (sb->size != sb->data_size) |
83205b64 NB |
197 | printf(" Used Size : %llu%s\n", |
198 | (unsigned long long)__le64_to_cpu(sb->size), | |
199 | human_size(__le64_to_cpu(sb->size)<<9)); | |
b674b5b8 | 200 | } |
82d9eba6 | 201 | if (sb->data_offset) |
199171a2 NB |
202 | printf(" Data Offset : %llu sectors\n", |
203 | (unsigned long long)__le64_to_cpu(sb->data_offset)); | |
204 | printf(" Super Offset : %llu sectors\n", | |
205 | (unsigned long long)__le64_to_cpu(sb->super_offset)); | |
d2cd3ffc NB |
206 | if (__le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET) |
207 | printf("Recovery Offset : %llu sectors\n", (unsigned long long)__le64_to_cpu(sb->recovery_offset)); | |
a3fd117c | 208 | printf(" State : %s\n", (__le64_to_cpu(sb->resync_offset)+1)? "active":"clean"); |
82d9eba6 NB |
209 | printf(" Device UUID : "); |
210 | for (i=0; i<16; i++) { | |
82d9eba6 | 211 | if ((i&3)==0 && i != 0) printf(":"); |
34163fc7 | 212 | printf("%02x", sb->device_uuid[i]); |
82d9eba6 NB |
213 | } |
214 | printf("\n"); | |
b674b5b8 | 215 | printf("\n"); |
91eedefc NB |
216 | if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) { |
217 | printf("Internal Bitmap : %ld sectors from superblock\n", | |
8fac0577 NB |
218 | (long)__le32_to_cpu(sb->bitmap_offset)); |
219 | } | |
b674b5b8 | 220 | if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) { |
1e0d770c | 221 | printf(" Reshape pos'n : %llu%s\n", (unsigned long long)__le64_to_cpu(sb->reshape_position)/2, |
b674b5b8 NB |
222 | human_size(__le64_to_cpu(sb->reshape_position)<<9)); |
223 | if (__le32_to_cpu(sb->delta_disks)) { | |
224 | printf(" Delta Devices : %d", __le32_to_cpu(sb->delta_disks)); | |
225 | if (__le32_to_cpu(sb->delta_disks)) | |
226 | printf(" (%d->%d)\n", | |
227 | __le32_to_cpu(sb->raid_disks)-__le32_to_cpu(sb->delta_disks), | |
228 | __le32_to_cpu(sb->raid_disks)); | |
229 | else | |
230 | printf(" (%d->%d)\n", __le32_to_cpu(sb->raid_disks), | |
231 | __le32_to_cpu(sb->raid_disks)+__le32_to_cpu(sb->delta_disks)); | |
232 | } | |
233 | if (__le32_to_cpu(sb->new_level) != __le32_to_cpu(sb->level)) { | |
234 | c = map_num(pers, __le32_to_cpu(sb->new_level)); | |
235 | printf(" New Level : %s\n", c?c:"-unknown-"); | |
236 | } | |
237 | if (__le32_to_cpu(sb->new_layout) != __le32_to_cpu(sb->layout)) { | |
238 | if (__le32_to_cpu(sb->level) == 5) { | |
239 | c = map_num(r5layout, __le32_to_cpu(sb->new_layout)); | |
240 | printf(" New Layout : %s\n", c?c:"-unknown-"); | |
241 | } | |
242 | if (__le32_to_cpu(sb->level) == 10) { | |
b578481c | 243 | printf(" New Layout : near=%d, %s=%d\n", |
b674b5b8 | 244 | __le32_to_cpu(sb->new_layout)&255, |
b578481c | 245 | (__le32_to_cpu(sb->new_layout)&0x10000)?"offset":"far", |
b674b5b8 NB |
246 | (__le32_to_cpu(sb->new_layout)>>8)&255); |
247 | } | |
248 | } | |
249 | if (__le32_to_cpu(sb->new_chunk) != __le32_to_cpu(sb->chunksize)) | |
250 | printf(" New Chunksize : %dK\n", __le32_to_cpu(sb->new_chunk)/2); | |
251 | printf("\n"); | |
252 | } | |
dfd4d8ee NB |
253 | if (sb->devflags) { |
254 | printf(" Flags :"); | |
34163fc7 | 255 | if (sb->devflags & WriteMostly1) |
dfd4d8ee NB |
256 | printf(" write-mostly"); |
257 | printf("\n"); | |
258 | } | |
82d9eba6 NB |
259 | |
260 | atime = __le64_to_cpu(sb->utime) & 0xFFFFFFFFFFULL; | |
261 | printf(" Update Time : %.24s\n", ctime(&atime)); | |
262 | ||
263 | if (calc_sb_1_csum(sb) == sb->sb_csum) | |
264 | printf(" Checksum : %x - correct\n", __le32_to_cpu(sb->sb_csum)); | |
265 | else | |
266 | printf(" Checksum : %x - expected %x\n", __le32_to_cpu(sb->sb_csum), | |
267 | __le32_to_cpu(calc_sb_1_csum(sb))); | |
570c0542 | 268 | printf(" Events : %llu\n", (unsigned long long)__le64_to_cpu(sb->events)); |
82d9eba6 NB |
269 | printf("\n"); |
270 | if (__le32_to_cpu(sb->level) == 5) { | |
271 | c = map_num(r5layout, __le32_to_cpu(sb->layout)); | |
272 | printf(" Layout : %s\n", c?c:"-unknown-"); | |
273 | } | |
265e0f17 NB |
274 | if (__le32_to_cpu(sb->level) == 10) { |
275 | int lo = __le32_to_cpu(sb->layout); | |
b578481c NB |
276 | printf(" Layout : near=%d, %s=%d\n", |
277 | lo&255, | |
278 | (lo&0x10000)?"offset":"far", | |
279 | (lo>>8)&255); | |
265e0f17 | 280 | } |
82d9eba6 NB |
281 | switch(__le32_to_cpu(sb->level)) { |
282 | case 0: | |
283 | case 4: | |
284 | case 5: | |
d2cd3ffc NB |
285 | case 6: |
286 | case 10: | |
b674b5b8 | 287 | printf(" Chunk Size : %dK\n", __le32_to_cpu(sb->chunksize)/2); |
82d9eba6 NB |
288 | break; |
289 | case -1: | |
b674b5b8 | 290 | printf(" Rounding : %dK\n", __le32_to_cpu(sb->chunksize)/2); |
82d9eba6 NB |
291 | break; |
292 | default: break; | |
293 | } | |
294 | printf("\n"); | |
c3684618 NB |
295 | printf(" Array Slot : %d (", __le32_to_cpu(sb->dev_number)); |
296 | for (i= __le32_to_cpu(sb->max_dev); i> 0 ; i--) | |
297 | if (__le16_to_cpu(sb->dev_roles[i-1]) != 0xffff) | |
298 | break; | |
299 | for (d=0; d < i; d++) { | |
300 | int role = __le16_to_cpu(sb->dev_roles[d]); | |
301 | if (d) printf(", "); | |
302 | if (role == 0xffff) printf("empty"); | |
303 | else if(role == 0xfffe) printf("failed"); | |
304 | else printf("%d", role); | |
305 | } | |
306 | printf(")\n"); | |
82d9eba6 NB |
307 | printf(" Array State : "); |
308 | for (d=0; d<__le32_to_cpu(sb->raid_disks); d++) { | |
309 | int cnt = 0; | |
310 | int me = 0; | |
311 | int i; | |
312 | for (i=0; i< __le32_to_cpu(sb->max_dev); i++) { | |
313 | int role = __le16_to_cpu(sb->dev_roles[i]); | |
314 | if (role == d) { | |
315 | if (i == __le32_to_cpu(sb->dev_number)) | |
316 | me = 1; | |
317 | cnt++; | |
318 | } | |
319 | } | |
320 | if (cnt > 1) printf("?"); | |
321 | else if (cnt == 1 && me) printf("U"); | |
322 | else if (cnt == 1) printf("u"); | |
323 | else printf ("_"); | |
324 | } | |
6fbba4c9 | 325 | faulty = 0; |
82d9eba6 NB |
326 | for (i=0; i< __le32_to_cpu(sb->max_dev); i++) { |
327 | int role = __le16_to_cpu(sb->dev_roles[i]); | |
6fbba4c9 NB |
328 | if (role == 0xFFFE) |
329 | faulty++; | |
82d9eba6 | 330 | } |
82d9eba6 NB |
331 | if (faulty) printf(" %d failed", faulty); |
332 | printf("\n"); | |
333 | } | |
334 | ||
335 | ||
336 | static void brief_examine_super1(void *sbv) | |
337 | { | |
338 | struct mdp_superblock_1 *sb = sbv; | |
339 | int i; | |
2998bc01 | 340 | char *nm; |
b015e6c2 | 341 | char *c=map_num(pers, __le32_to_cpu(sb->level)); |
2998bc01 NB |
342 | |
343 | nm = strchr(sb->set_name, ':'); | |
344 | if (nm) | |
345 | nm++; | |
346 | else if (sb->set_name[0]) | |
347 | nm = sb->set_name; | |
348 | else | |
349 | nm = "??"; | |
82d9eba6 | 350 | |
2998bc01 NB |
351 | printf("ARRAY /dev/md/%s level=%s metadata=1 num-devices=%d UUID=", |
352 | nm, | |
83205b64 | 353 | c?c:"-unknown-", __le32_to_cpu(sb->raid_disks)); |
82d9eba6 | 354 | for (i=0; i<16; i++) { |
82d9eba6 | 355 | if ((i&3)==0 && i != 0) printf(":"); |
b7a708af | 356 | printf("%02x", sb->set_uuid[i]); |
82d9eba6 | 357 | } |
947fd4dd NB |
358 | if (sb->set_name[0]) |
359 | printf(" name=%.32s", sb->set_name); | |
82d9eba6 NB |
360 | printf("\n"); |
361 | } | |
362 | ||
b6750aa8 | 363 | static void detail_super1(void *sbv, char *homehost) |
82d9eba6 NB |
364 | { |
365 | struct mdp_superblock_1 *sb = sbv; | |
366 | int i; | |
b6750aa8 | 367 | int l = homehost ? strlen(homehost) : 0; |
82d9eba6 | 368 | |
b6750aa8 NB |
369 | printf(" Name : %.32s", sb->set_name); |
370 | if (l > 0 && l < 32 && | |
371 | sb->set_name[l] == ':' && | |
372 | strncmp(sb->set_name, homehost, l) == 0) | |
373 | printf(" (local to host %s)", homehost); | |
374 | printf("\n UUID : "); | |
82d9eba6 | 375 | for (i=0; i<16; i++) { |
82d9eba6 | 376 | if ((i&3)==0 && i != 0) printf(":"); |
34163fc7 | 377 | printf("%02x", sb->set_uuid[i]); |
82d9eba6 | 378 | } |
570c0542 | 379 | printf("\n Events : %llu\n\n", (unsigned long long)__le64_to_cpu(sb->events)); |
82d9eba6 NB |
380 | } |
381 | ||
382 | static void brief_detail_super1(void *sbv) | |
383 | { | |
384 | struct mdp_superblock_1 *sb = sbv; | |
385 | int i; | |
386 | ||
947fd4dd NB |
387 | if (sb->set_name[0]) |
388 | printf(" name=%.32s", sb->set_name); | |
82d9eba6 NB |
389 | printf(" UUID="); |
390 | for (i=0; i<16; i++) { | |
82d9eba6 | 391 | if ((i&3)==0 && i != 0) printf(":"); |
34163fc7 | 392 | printf("%02x", sb->set_uuid[i]); |
82d9eba6 NB |
393 | } |
394 | } | |
395 | ||
c7654afc NB |
396 | #endif |
397 | ||
83b6208e NB |
398 | static int match_home1(void *sbv, char *homehost) |
399 | { | |
400 | struct mdp_superblock_1 *sb = sbv; | |
401 | int l = homehost ? strlen(homehost) : 0; | |
402 | ||
403 | return (l > 0 && l < 32 && | |
404 | sb->set_name[l] == ':' && | |
405 | strncmp(sb->set_name, homehost, l) == 0); | |
406 | } | |
407 | ||
82d9eba6 NB |
408 | static void uuid_from_super1(int uuid[4], void * sbv) |
409 | { | |
410 | struct mdp_superblock_1 *super = sbv; | |
411 | char *cuuid = (char*)uuid; | |
412 | int i; | |
413 | for (i=0; i<16; i++) | |
414 | cuuid[i] = super->set_uuid[i]; | |
415 | } | |
416 | ||
31317663 | 417 | static void getinfo_super1(struct mdinfo *info, void *sbv) |
82d9eba6 NB |
418 | { |
419 | struct mdp_superblock_1 *sb = sbv; | |
420 | int working = 0; | |
421 | int i; | |
422 | int role; | |
423 | ||
424 | info->array.major_version = 1; | |
425 | info->array.minor_version = __le32_to_cpu(sb->feature_map); | |
426 | info->array.patch_version = 0; | |
427 | info->array.raid_disks = __le32_to_cpu(sb->raid_disks); | |
428 | info->array.level = __le32_to_cpu(sb->level); | |
265e0f17 | 429 | info->array.layout = __le32_to_cpu(sb->layout); |
82d9eba6 NB |
430 | info->array.md_minor = -1; |
431 | info->array.ctime = __le64_to_cpu(sb->ctime); | |
353632d9 | 432 | info->array.utime = __le64_to_cpu(sb->utime); |
bed256c2 | 433 | info->array.chunk_size = __le32_to_cpu(sb->chunksize)*512; |
4855f95c NB |
434 | info->array.state = |
435 | (__le64_to_cpu(sb->resync_offset) >= __le64_to_cpu(sb->size)) | |
436 | ? 1 : 0; | |
353632d9 NB |
437 | |
438 | info->data_offset = __le64_to_cpu(sb->data_offset); | |
439 | info->component_size = __le64_to_cpu(sb->size); | |
82d9eba6 NB |
440 | |
441 | info->disk.major = 0; | |
442 | info->disk.minor = 0; | |
fbf8a0b7 | 443 | info->disk.number = __le32_to_cpu(sb->dev_number); |
82d9eba6 NB |
444 | if (__le32_to_cpu(sb->dev_number) >= __le32_to_cpu(sb->max_dev) || |
445 | __le32_to_cpu(sb->max_dev) > 512) | |
446 | role = 0xfffe; | |
447 | else | |
448 | role = __le16_to_cpu(sb->dev_roles[__le32_to_cpu(sb->dev_number)]); | |
449 | ||
450 | info->disk.raid_disk = -1; | |
451 | switch(role) { | |
452 | case 0xFFFF: | |
453 | info->disk.state = 2; /* spare: ACTIVE, not sync, not faulty */ | |
454 | break; | |
455 | case 0xFFFE: | |
456 | info->disk.state = 1; /* faulty */ | |
457 | break; | |
458 | default: | |
459 | info->disk.state = 6; /* active and in sync */ | |
460 | info->disk.raid_disk = role; | |
461 | } | |
462 | info->events = __le64_to_cpu(sb->events); | |
463 | ||
464 | memcpy(info->uuid, sb->set_uuid, 16); | |
465 | ||
31317663 NB |
466 | strncpy(info->name, sb->set_name, 32); |
467 | info->name[32] = 0; | |
947fd4dd | 468 | |
353632d9 NB |
469 | if (sb->feature_map & __le32_to_cpu(MD_FEATURE_RESHAPE_ACTIVE)) { |
470 | info->reshape_active = 1; | |
471 | info->reshape_progress = __le64_to_cpu(sb->reshape_position); | |
472 | info->new_level = __le32_to_cpu(sb->new_level); | |
473 | info->delta_disks = __le32_to_cpu(sb->delta_disks); | |
474 | info->new_layout = __le32_to_cpu(sb->new_layout); | |
bed256c2 | 475 | info->new_chunk = __le32_to_cpu(sb->new_chunk)<<9; |
353632d9 NB |
476 | } else |
477 | info->reshape_active = 0; | |
478 | ||
82d9eba6 NB |
479 | for (i=0; i< __le32_to_cpu(sb->max_dev); i++) { |
480 | role = __le16_to_cpu(sb->dev_roles[i]); | |
fbf8a0b7 | 481 | if (/*role == 0xFFFF || */role < info->array.raid_disks) |
82d9eba6 NB |
482 | working++; |
483 | } | |
484 | ||
485 | info->array.working_disks = working; | |
486 | } | |
487 | ||
e5eac01f NB |
488 | static int update_super1(struct mdinfo *info, void *sbv, char *update, |
489 | char *devname, int verbose, | |
490 | int uuid_set, char *homehost) | |
82d9eba6 | 491 | { |
c4d831e1 NB |
492 | /* NOTE: for 'assemble' and 'force' we need to return non-zero if any change was made. |
493 | * For others, the return value is ignored. | |
494 | */ | |
82d9eba6 NB |
495 | int rv = 0; |
496 | struct mdp_superblock_1 *sb = sbv; | |
497 | ||
67a8c82d NB |
498 | if (strcmp(update, "force-one")==0) { |
499 | /* Not enough devices for a working array, | |
500 | * so bring this one up-to-date | |
501 | */ | |
c4d831e1 NB |
502 | if (sb->events != __cpu_to_le64(info->events)) |
503 | rv = 1; | |
6d3d5804 | 504 | sb->events = __cpu_to_le64(info->events); |
67a8c82d NB |
505 | } |
506 | if (strcmp(update, "force-array")==0) { | |
507 | /* Degraded array and 'force' requests to | |
508 | * maybe need to mark it 'clean'. | |
509 | */ | |
82d9eba6 NB |
510 | switch(__le32_to_cpu(sb->level)) { |
511 | case 5: case 4: case 6: | |
512 | /* need to force clean */ | |
c4d831e1 NB |
513 | if (sb->resync_offset != ~0ULL) |
514 | rv = 1; | |
82d9eba6 NB |
515 | sb->resync_offset = ~0ULL; |
516 | } | |
517 | } | |
518 | if (strcmp(update, "assemble")==0) { | |
519 | int d = info->disk.number; | |
520 | int want; | |
521 | if (info->disk.state == 6) | |
522 | want = __cpu_to_le32(info->disk.raid_disk); | |
523 | else | |
524 | want = 0xFFFF; | |
525 | if (sb->dev_roles[d] != want) { | |
526 | sb->dev_roles[d] = want; | |
527 | rv = 1; | |
528 | } | |
529 | } | |
82d9eba6 NB |
530 | if (strcmp(update, "grow") == 0) { |
531 | sb->raid_disks = __cpu_to_le32(info->array.raid_disks); | |
d645b91a NB |
532 | /* As we are just adding a spare, there is no need to |
533 | * make any change to the dev_roles array | |
534 | */ | |
82d9eba6 NB |
535 | } |
536 | if (strcmp(update, "resync") == 0) { | |
537 | /* make sure resync happens */ | |
434b7755 | 538 | sb->resync_offset = 0ULL; |
82d9eba6 | 539 | } |
14263cf1 | 540 | if (strcmp(update, "uuid") == 0) { |
350f29f9 | 541 | copy_uuid(sb->set_uuid, info->uuid, super1.swapuuid); |
bf4fb153 | 542 | |
14263cf1 NB |
543 | if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) { |
544 | struct bitmap_super_s *bm; | |
545 | bm = (struct bitmap_super_s*)(sbv+1024); | |
bf4fb153 | 546 | memcpy(bm->uuid, sb->set_uuid, 16); |
14263cf1 NB |
547 | } |
548 | } | |
0237e0ca NB |
549 | if (strcmp(update, "homehost") == 0 && |
550 | homehost) { | |
551 | char *c; | |
552 | update = "name"; | |
553 | c = strchr(sb->set_name, ':'); | |
554 | if (c) | |
555 | strncpy(info->name, c+1, 31 - (c-sb->set_name)); | |
556 | else | |
557 | strncpy(info->name, sb->set_name, 32); | |
26a0b8fd | 558 | info->name[32] = 0; |
0237e0ca | 559 | } |
c4f12c13 NB |
560 | if (strcmp(update, "name") == 0) { |
561 | if (info->name[0] == 0) | |
562 | sprintf(info->name, "%d", info->array.md_minor); | |
563 | memset(sb->set_name, 0, sizeof(sb->set_name)); | |
564 | if (homehost && | |
565 | strchr(info->name, ':') == NULL && | |
566 | strlen(homehost)+1+strlen(info->name) < 32) { | |
567 | strcpy(sb->set_name, homehost); | |
568 | strcat(sb->set_name, ":"); | |
569 | strcat(sb->set_name, info->name); | |
570 | } else | |
571 | strcpy(sb->set_name, info->name); | |
572 | } | |
bee8ec56 NB |
573 | if (strcmp(update, "devicesize") == 0 && |
574 | __le64_to_cpu(sb->super_offset) < | |
575 | __le64_to_cpu(sb->data_offset)) { | |
576 | /* set data_size to device size less data_offset */ | |
577 | struct misc_dev_info *misc = (struct misc_dev_info*) | |
578 | (sbv + 1024 + sizeof(struct bitmap_super_s)); | |
eb9199fb NB |
579 | printf("Size was %llu\n", (unsigned long long) |
580 | __le64_to_cpu(sb->data_size)); | |
bee8ec56 NB |
581 | sb->data_size = __cpu_to_le64( |
582 | misc->device_size - __le64_to_cpu(sb->data_offset)); | |
eb9199fb NB |
583 | printf("Size is %llu\n", (unsigned long long) |
584 | __le64_to_cpu(sb->data_size)); | |
bee8ec56 | 585 | } |
353632d9 NB |
586 | if (strcmp(update, "_reshape_progress")==0) |
587 | sb->reshape_position = __cpu_to_le64(info->reshape_progress); | |
82d9eba6 NB |
588 | |
589 | sb->sb_csum = calc_sb_1_csum(sb); | |
590 | return rv; | |
591 | } | |
592 | ||
05697ec1 | 593 | static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info, |
3d3dd91e | 594 | unsigned long long size, char *name, char *homehost, int *uuid) |
82d9eba6 | 595 | { |
bee8ec56 NB |
596 | struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t) + |
597 | sizeof(struct misc_dev_info)); | |
82d9eba6 | 598 | int spares; |
34163fc7 | 599 | int rfd; |
05697ec1 | 600 | char defname[10]; |
82d9eba6 NB |
601 | memset(sb, 0, 1024); |
602 | ||
05697ec1 | 603 | if (info->major_version == -1) { |
82d9eba6 | 604 | /* zeroing superblock */ |
05697ec1 | 605 | *sbp = sb; |
82d9eba6 | 606 | return 0; |
05697ec1 | 607 | } |
82d9eba6 NB |
608 | |
609 | spares = info->working_disks - info->active_disks; | |
610 | if (info->raid_disks + spares > 384) { | |
611 | fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n", | |
612 | info->raid_disks , spares, 384); | |
613 | return 0; | |
614 | } | |
615 | ||
82d9eba6 NB |
616 | sb->magic = __cpu_to_le32(MD_SB_MAGIC); |
617 | sb->major_version = __cpu_to_le32(1); | |
618 | sb->feature_map = 0; | |
619 | sb->pad0 = 0; | |
34163fc7 | 620 | |
350f29f9 NB |
621 | if (uuid) |
622 | copy_uuid(sb->set_uuid, uuid, super1.swapuuid); | |
623 | else { | |
3d3dd91e NB |
624 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || |
625 | read(rfd, sb->set_uuid, 16) != 16) { | |
626 | *(__u32*)(sb->set_uuid) = random(); | |
627 | *(__u32*)(sb->set_uuid+4) = random(); | |
628 | *(__u32*)(sb->set_uuid+8) = random(); | |
629 | *(__u32*)(sb->set_uuid+12) = random(); | |
630 | } | |
631 | if (rfd >= 0) close(rfd); | |
34163fc7 | 632 | } |
82d9eba6 | 633 | |
05697ec1 NB |
634 | if (name == NULL || *name == 0) { |
635 | sprintf(defname, "%d", info->md_minor); | |
636 | name = defname; | |
637 | } | |
947fd4dd | 638 | memset(sb->set_name, 0, 32); |
05697ec1 NB |
639 | if (homehost && |
640 | strchr(name, ':')== NULL && | |
641 | strlen(homehost)+1+strlen(name) < 32) { | |
642 | strcpy(sb->set_name, homehost); | |
643 | strcat(sb->set_name, ":"); | |
644 | strcat(sb->set_name, name); | |
645 | } else | |
646 | strcpy(sb->set_name, name); | |
82d9eba6 NB |
647 | |
648 | sb->ctime = __cpu_to_le64((unsigned long long)time(0)); | |
649 | sb->level = __cpu_to_le32(info->level); | |
ea329559 | 650 | sb->layout = __cpu_to_le32(info->layout); |
5dd497ee | 651 | sb->size = __cpu_to_le64(size*2ULL); |
82d9eba6 NB |
652 | sb->chunksize = __cpu_to_le32(info->chunk_size>>9); |
653 | sb->raid_disks = __cpu_to_le32(info->raid_disks); | |
654 | ||
655 | sb->data_offset = __cpu_to_le64(0); | |
656 | sb->data_size = __cpu_to_le64(0); | |
657 | sb->super_offset = __cpu_to_le64(0); | |
658 | sb->recovery_offset = __cpu_to_le64(0); | |
659 | ||
660 | sb->utime = sb->ctime; | |
661 | sb->events = __cpu_to_le64(1); | |
34321279 | 662 | if (info->state & (1<<MD_SB_CLEAN)) |
82d9eba6 NB |
663 | sb->resync_offset = ~0ULL; |
664 | else | |
665 | sb->resync_offset = 0; | |
34163fc7 | 666 | sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/ |
82d9eba6 NB |
667 | sizeof(sb->dev_roles[0])); |
668 | memset(sb->pad3, 0, sizeof(sb->pad3)); | |
669 | ||
670 | memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1)); | |
671 | ||
672 | *sbp = sb; | |
673 | return 1; | |
674 | } | |
675 | ||
676 | /* Add a device to the superblock being created */ | |
677 | static void add_to_super1(void *sbv, mdu_disk_info_t *dk) | |
678 | { | |
679 | struct mdp_superblock_1 *sb = sbv; | |
680 | __u16 *rp = sb->dev_roles + dk->number; | |
dfd4d8ee | 681 | if ((dk->state & 6) == 6) /* active, sync */ |
82d9eba6 | 682 | *rp = __cpu_to_le16(dk->raid_disk); |
412ca2e5 | 683 | else if ((dk->state & ~2) == 0) /* active or idle -> spare */ |
82d9eba6 | 684 | *rp = 0xffff; |
34163fc7 | 685 | else |
82d9eba6 NB |
686 | *rp = 0xfffe; |
687 | } | |
688 | ||
14263cf1 NB |
689 | static void locate_bitmap1(struct supertype *st, int fd, void *sbv); |
690 | ||
96395475 | 691 | static int store_super1(struct supertype *st, int fd, void *sbv) |
82d9eba6 NB |
692 | { |
693 | struct mdp_superblock_1 *sb = sbv; | |
6fbba4c9 | 694 | unsigned long long sb_offset; |
82d9eba6 | 695 | int sbsize; |
5dd497ee | 696 | unsigned long long dsize; |
96395475 | 697 | |
beae1dfe NB |
698 | if (!get_dev_size(fd, NULL, &dsize)) |
699 | return 1; | |
700 | ||
701 | dsize >>= 9; | |
96395475 | 702 | |
5dd497ee | 703 | if (dsize < 24) |
96395475 NB |
704 | return 2; |
705 | ||
706 | /* | |
707 | * Calculate the position of the superblock. | |
708 | * It is always aligned to a 4K boundary and | |
709 | * depending on minor_version, it can be: | |
710 | * 0: At least 8K, but less than 12K, from end of device | |
711 | * 1: At start of device | |
712 | * 2: 4K from start of device. | |
713 | */ | |
714 | switch(st->minor_version) { | |
715 | case 0: | |
5dd497ee | 716 | sb_offset = dsize; |
96395475 NB |
717 | sb_offset -= 8*2; |
718 | sb_offset &= ~(4*2-1); | |
719 | break; | |
720 | case 1: | |
6fbba4c9 | 721 | sb_offset = 0; |
96395475 NB |
722 | break; |
723 | case 2: | |
724 | sb_offset = 4*2; | |
725 | break; | |
726 | default: | |
727 | return -EINVAL; | |
728 | } | |
729 | ||
82d9eba6 | 730 | |
34163fc7 | 731 | |
6fbba4c9 NB |
732 | if (sb_offset != __le64_to_cpu(sb->super_offset) && |
733 | 0 != __le64_to_cpu(sb->super_offset) | |
96395475 NB |
734 | ) { |
735 | fprintf(stderr, Name ": internal error - sb_offset is wrong\n"); | |
736 | abort(); | |
737 | } | |
82d9eba6 | 738 | |
6fbba4c9 | 739 | if (lseek64(fd, sb_offset << 9, 0)< 0LL) |
82d9eba6 NB |
740 | return 3; |
741 | ||
742 | sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev); | |
743 | ||
744 | if (write(fd, sb, sbsize) != sbsize) | |
745 | return 4; | |
746 | ||
14263cf1 NB |
747 | if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) { |
748 | struct bitmap_super_s *bm = (struct bitmap_super_s*) | |
41a3b72a | 749 | (((char*)sb)+1024); |
14263cf1 NB |
750 | if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) { |
751 | locate_bitmap1(st, fd, sbv); | |
9fca7d62 NB |
752 | if (write(fd, bm, sizeof(*bm)) != sizeof(*bm)) |
753 | return 5; | |
14263cf1 NB |
754 | } |
755 | } | |
570c0542 | 756 | fsync(fd); |
82d9eba6 NB |
757 | return 0; |
758 | } | |
759 | ||
892debc8 NB |
760 | static int load_super1(struct supertype *st, int fd, void **sbp, char *devname); |
761 | ||
34163fc7 | 762 | static int write_init_super1(struct supertype *st, void *sbv, |
1bf4e2d9 | 763 | mdu_disk_info_t *dinfo, char *devname) |
82d9eba6 NB |
764 | { |
765 | struct mdp_superblock_1 *sb = sbv; | |
e478dc86 | 766 | void *refsbv = NULL; |
892debc8 | 767 | int fd = open(devname, O_RDWR | O_EXCL); |
dfe47e00 | 768 | int rfd; |
82d9eba6 | 769 | int rv; |
199171a2 | 770 | int bm_space; |
82d9eba6 | 771 | |
199171a2 | 772 | unsigned long space; |
8fac0577 | 773 | unsigned long long dsize, array_size; |
82d9eba6 NB |
774 | long long sb_offset; |
775 | ||
776 | ||
777 | if (fd < 0) { | |
778 | fprintf(stderr, Name ": Failed to open %s to write superblock\n", | |
779 | devname); | |
780 | return -1; | |
781 | } | |
782 | ||
783 | sb->dev_number = __cpu_to_le32(dinfo->number); | |
34163fc7 | 784 | if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY)) |
83205b64 | 785 | sb->devflags |= __cpu_to_le32(WriteMostly1); |
892debc8 | 786 | |
dfe47e00 NB |
787 | if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 || |
788 | read(rfd, sb->device_uuid, 16) != 16) { | |
789 | *(__u32*)(sb->device_uuid) = random(); | |
790 | *(__u32*)(sb->device_uuid+4) = random(); | |
791 | *(__u32*)(sb->device_uuid+8) = random(); | |
792 | *(__u32*)(sb->device_uuid+12) = random(); | |
793 | } | |
794 | if (rfd >= 0) close(rfd); | |
892debc8 NB |
795 | sb->events = 0; |
796 | ||
e478dc86 NB |
797 | if (load_super1(st, fd, &refsbv, NULL)==0) { |
798 | struct mdp_superblock_1 *refsb = refsbv; | |
799 | ||
892debc8 NB |
800 | memcpy(sb->device_uuid, refsb->device_uuid, 16); |
801 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) { | |
802 | /* same array, so preserve events and dev_number */ | |
803 | sb->events = refsb->events; | |
7eae7080 NB |
804 | /* bugs in 2.6.17 and earlier mean the dev_number |
805 | * chosen in Manage must be preserved | |
806 | */ | |
807 | if (get_linux_version() >= 2006018) | |
808 | sb->dev_number = refsb->dev_number; | |
892debc8 NB |
809 | } |
810 | free(refsb); | |
811 | } | |
34163fc7 | 812 | |
beae1dfe NB |
813 | if (!get_dev_size(fd, NULL, &dsize)) |
814 | return 1; | |
815 | dsize >>= 9; | |
82d9eba6 | 816 | |
5dd497ee | 817 | if (dsize < 24) { |
024ce7fe | 818 | close(fd); |
82d9eba6 | 819 | return 2; |
024ce7fe | 820 | } |
82d9eba6 NB |
821 | |
822 | ||
823 | /* | |
824 | * Calculate the position of the superblock. | |
825 | * It is always aligned to a 4K boundary and | |
826 | * depending on minor_version, it can be: | |
827 | * 0: At least 8K, but less than 12K, from end of device | |
828 | * 1: At start of device | |
829 | * 2: 4K from start of device. | |
21e92547 NB |
830 | * Depending on the array size, we might leave extra space |
831 | * for a bitmap. | |
82d9eba6 | 832 | */ |
8fac0577 | 833 | array_size = __le64_to_cpu(sb->size); |
199171a2 NB |
834 | /* work out how much space we left of a bitmap */ |
835 | if (array_size >= 200*1024*1024*2) | |
836 | bm_space = 128*2; | |
837 | else if (array_size > 8*1024*1024*2) | |
838 | bm_space = 64*2; | |
839 | else | |
840 | bm_space = 0; | |
841 | ||
82d9eba6 NB |
842 | switch(st->minor_version) { |
843 | case 0: | |
5dd497ee | 844 | sb_offset = dsize; |
82d9eba6 NB |
845 | sb_offset -= 8*2; |
846 | sb_offset &= ~(4*2-1); | |
847 | sb->super_offset = __cpu_to_le64(sb_offset); | |
848 | sb->data_offset = __cpu_to_le64(0); | |
199171a2 | 849 | sb->data_size = __cpu_to_le64(sb_offset - bm_space); |
82d9eba6 NB |
850 | break; |
851 | case 1: | |
852 | sb->super_offset = __cpu_to_le64(0); | |
199171a2 NB |
853 | sb->data_offset = __cpu_to_le64(bm_space + 4*2); |
854 | sb->data_size = __cpu_to_le64(dsize - bm_space - 4*2); | |
82d9eba6 NB |
855 | break; |
856 | case 2: | |
857 | sb_offset = 4*2; | |
561832f9 | 858 | if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2) |
21e92547 NB |
859 | space = 64*2; |
860 | else | |
861 | space = 4*2; | |
199171a2 NB |
862 | sb->super_offset = __cpu_to_le64(4*2); |
863 | sb->data_offset = __cpu_to_le64(4*2 + 4*2 + bm_space); | |
864 | sb->data_size = __cpu_to_le64(dsize - 4*2 - 4*2 - bm_space ); | |
82d9eba6 NB |
865 | break; |
866 | default: | |
867 | return -EINVAL; | |
868 | } | |
869 | ||
870 | ||
871 | sb->sb_csum = calc_sb_1_csum(sb); | |
96395475 | 872 | rv = store_super1(st, fd, sb); |
82d9eba6 NB |
873 | if (rv) |
874 | fprintf(stderr, Name ": failed to write superblock to %s\n", devname); | |
34163fc7 | 875 | |
1bf4e2d9 NB |
876 | if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1)) |
877 | rv = st->ss->write_bitmap(st, fd, sbv); | |
024ce7fe | 878 | close(fd); |
82d9eba6 NB |
879 | return rv; |
880 | } | |
881 | ||
882 | static int compare_super1(void **firstp, void *secondv) | |
883 | { | |
884 | /* | |
885 | * return: | |
886 | * 0 same, or first was empty, and second was copied | |
887 | * 1 second had wrong number | |
888 | * 2 wrong uuid | |
889 | * 3 wrong other info | |
890 | */ | |
891 | struct mdp_superblock_1 *first = *firstp; | |
892 | struct mdp_superblock_1 *second = secondv; | |
893 | ||
894 | if (second->magic != __cpu_to_le32(MD_SB_MAGIC)) | |
895 | return 1; | |
896 | if (second->major_version != __cpu_to_le32(1)) | |
897 | return 1; | |
898 | ||
899 | if (!first) { | |
bee8ec56 NB |
900 | first = malloc(1024+sizeof(bitmap_super_t) + |
901 | sizeof(struct misc_dev_info)); | |
902 | memcpy(first, second, 1024+sizeof(bitmap_super_t) + | |
903 | sizeof(struct misc_dev_info)); | |
82d9eba6 NB |
904 | *firstp = first; |
905 | return 0; | |
906 | } | |
907 | if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0) | |
908 | return 2; | |
909 | ||
910 | if (first->ctime != second->ctime || | |
911 | first->level != second->level || | |
912 | first->layout != second->layout || | |
913 | first->size != second->size || | |
914 | first->chunksize != second->chunksize || | |
915 | first->raid_disks != second->raid_disks) | |
916 | return 3; | |
917 | return 0; | |
918 | } | |
919 | ||
920 | static int load_super1(struct supertype *st, int fd, void **sbp, char *devname) | |
921 | { | |
5dd497ee | 922 | unsigned long long dsize; |
82d9eba6 NB |
923 | unsigned long long sb_offset; |
924 | struct mdp_superblock_1 *super; | |
14263cf1 NB |
925 | int uuid[4]; |
926 | struct bitmap_super_s *bsb; | |
bee8ec56 | 927 | struct misc_dev_info *misc; |
82d9eba6 NB |
928 | |
929 | ||
930 | if (st->ss == NULL) { | |
570c0542 NB |
931 | int bestvers = -1; |
932 | __u64 bestctime = 0; | |
933 | /* guess... choose latest ctime */ | |
82d9eba6 NB |
934 | st->ss = &super1; |
935 | for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) { | |
936 | switch(load_super1(st, fd, sbp, devname)) { | |
570c0542 NB |
937 | case 0: super = *sbp; |
938 | if (bestvers == -1 || | |
939 | bestctime < __le64_to_cpu(super->ctime)) { | |
940 | bestvers = st->minor_version; | |
941 | bestctime = __le64_to_cpu(super->ctime); | |
942 | } | |
943 | free(super); | |
944 | *sbp = NULL; | |
945 | break; | |
82d9eba6 NB |
946 | case 1: st->ss = NULL; return 1; /*bad device */ |
947 | case 2: break; /* bad, try next */ | |
948 | } | |
949 | } | |
570c0542 NB |
950 | if (bestvers != -1) { |
951 | int rv; | |
952 | st->minor_version = bestvers; | |
953 | st->ss = &super1; | |
ea329559 | 954 | st->max_devs = 384; |
570c0542 NB |
955 | rv = load_super1(st, fd, sbp, devname); |
956 | if (rv) st->ss = NULL; | |
957 | return rv; | |
958 | } | |
82d9eba6 NB |
959 | st->ss = NULL; |
960 | return 2; | |
961 | } | |
beae1dfe NB |
962 | if (!get_dev_size(fd, devname, &dsize)) |
963 | return 1; | |
964 | dsize >>= 9; | |
82d9eba6 | 965 | |
5dd497ee | 966 | if (dsize < 24) { |
82d9eba6 | 967 | if (devname) |
5dd497ee NB |
968 | fprintf(stderr, Name ": %s is too small for md: size is %llu sectors.\n", |
969 | devname, dsize); | |
82d9eba6 NB |
970 | return 1; |
971 | } | |
972 | ||
973 | /* | |
974 | * Calculate the position of the superblock. | |
975 | * It is always aligned to a 4K boundary and | |
14263cf1 | 976 | * depending on minor_version, it can be: |
82d9eba6 NB |
977 | * 0: At least 8K, but less than 12K, from end of device |
978 | * 1: At start of device | |
979 | * 2: 4K from start of device. | |
980 | */ | |
981 | switch(st->minor_version) { | |
982 | case 0: | |
5dd497ee | 983 | sb_offset = dsize; |
82d9eba6 NB |
984 | sb_offset -= 8*2; |
985 | sb_offset &= ~(4*2-1); | |
986 | break; | |
987 | case 1: | |
988 | sb_offset = 0; | |
989 | break; | |
990 | case 2: | |
991 | sb_offset = 4*2; | |
992 | break; | |
993 | default: | |
994 | return -EINVAL; | |
995 | } | |
996 | ||
997 | ioctl(fd, BLKFLSBUF, 0); /* make sure we read current data */ | |
998 | ||
999 | ||
1000 | if (lseek64(fd, sb_offset << 9, 0)< 0LL) { | |
1001 | if (devname) | |
1002 | fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n", | |
1003 | devname, strerror(errno)); | |
1004 | return 1; | |
1005 | } | |
1006 | ||
bee8ec56 NB |
1007 | super = malloc(1024 + sizeof(bitmap_super_t) + |
1008 | sizeof(struct misc_dev_info)); | |
82d9eba6 NB |
1009 | |
1010 | if (read(fd, super, 1024) != 1024) { | |
1011 | if (devname) | |
1012 | fprintf(stderr, Name ": Cannot read superblock on %s\n", | |
1013 | devname); | |
1014 | free(super); | |
1015 | return 1; | |
1016 | } | |
1017 | ||
1018 | if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) { | |
1019 | if (devname) | |
1020 | fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n", | |
1021 | devname, MD_SB_MAGIC, __le32_to_cpu(super->magic)); | |
1022 | free(super); | |
1023 | return 2; | |
1024 | } | |
1025 | ||
1026 | if (__le32_to_cpu(super->major_version) != 1) { | |
1027 | if (devname) | |
1028 | fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n", | |
1029 | devname, __le32_to_cpu(super->major_version)); | |
1030 | free(super); | |
1031 | return 2; | |
1032 | } | |
1033 | if (__le64_to_cpu(super->super_offset) != sb_offset) { | |
1034 | if (devname) | |
1035 | fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n", | |
1036 | devname); | |
1037 | free(super); | |
1038 | return 2; | |
1039 | } | |
1040 | *sbp = super; | |
14263cf1 | 1041 | |
bee8ec56 NB |
1042 | bsb = (struct bitmap_super_s *)(((char*)super)+1024); |
1043 | ||
1044 | misc = (struct misc_dev_info*) (bsb+1); | |
1045 | misc->device_size = dsize; | |
1046 | ||
14263cf1 NB |
1047 | /* Now check on the bitmap superblock */ |
1048 | if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0) | |
1049 | return 0; | |
1050 | /* Read the bitmap superblock and make sure it looks | |
1051 | * valid. If it doesn't clear the bit. An --assemble --force | |
1052 | * should get that written out. | |
1053 | */ | |
1054 | locate_bitmap1(st, fd, super); | |
1055 | if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s)) | |
1056 | != sizeof(struct bitmap_super_s)) | |
1057 | goto no_bitmap; | |
1058 | ||
1059 | uuid_from_super1(uuid, super); | |
14263cf1 NB |
1060 | if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC || |
1061 | memcmp(bsb->uuid, uuid, 16) != 0) | |
1062 | goto no_bitmap; | |
1063 | return 0; | |
1064 | ||
1065 | no_bitmap: | |
1066 | super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1); | |
82d9eba6 NB |
1067 | return 0; |
1068 | } | |
1069 | ||
1070 | ||
1071 | static struct supertype *match_metadata_desc1(char *arg) | |
1072 | { | |
1073 | struct supertype *st = malloc(sizeof(*st)); | |
1074 | if (!st) return st; | |
1075 | ||
1076 | st->ss = &super1; | |
ea329559 | 1077 | st->max_devs = 384; |
82d9eba6 | 1078 | if (strcmp(arg, "1") == 0 || |
576d6d83 NB |
1079 | strcmp(arg, "1.0") == 0 || |
1080 | strcmp(arg, "default/large") == 0) { | |
82d9eba6 NB |
1081 | st->minor_version = 0; |
1082 | return st; | |
1083 | } | |
1084 | if (strcmp(arg, "1.1") == 0) { | |
1085 | st->minor_version = 1; | |
1086 | return st; | |
1087 | } | |
1088 | if (strcmp(arg, "1.2") == 0) { | |
1089 | st->minor_version = 2; | |
1090 | return st; | |
1091 | } | |
1092 | ||
1093 | free(st); | |
1094 | return NULL; | |
1095 | } | |
1096 | ||
34163fc7 NB |
1097 | /* find available size on device with this devsize, using |
1098 | * superblock type st, and reserving 'reserve' sectors for | |
1099 | * a possible bitmap | |
1100 | */ | |
1bf4e2d9 | 1101 | static __u64 avail_size1(struct supertype *st, __u64 devsize) |
82d9eba6 NB |
1102 | { |
1103 | if (devsize < 24) | |
1104 | return 0; | |
1105 | ||
21e92547 | 1106 | /* if the device is bigger than 8Gig, save 64k for bitmap usage, |
199171a2 | 1107 | * if bigger than 200Gig, save 128k |
21e92547 | 1108 | */ |
199171a2 | 1109 | if (devsize-64*2 >= 200*1024*1024*2) |
21e92547 | 1110 | devsize -= 128*2; |
199171a2 | 1111 | else if (devsize >= 8*1024*1024*2) |
21e92547 NB |
1112 | devsize -= 64*2; |
1113 | ||
34163fc7 NB |
1114 | switch(st->minor_version) { |
1115 | case 0: | |
1bf4e2d9 NB |
1116 | /* at end */ |
1117 | return ((devsize - 8*2 ) & ~(4*2-1)); | |
34163fc7 | 1118 | case 1: |
1bf4e2d9 NB |
1119 | /* at start, 4K for superblock and possible bitmap */ |
1120 | return devsize - 4*2; | |
34163fc7 | 1121 | case 2: |
1bf4e2d9 NB |
1122 | /* 4k from start, 4K for superblock and possible bitmap */ |
1123 | return devsize - (4+4)*2; | |
34163fc7 NB |
1124 | } |
1125 | return 0; | |
1126 | } | |
1127 | ||
1bf4e2d9 NB |
1128 | static int |
1129 | add_internal_bitmap1(struct supertype *st, void *sbv, | |
199171a2 NB |
1130 | int *chunkp, int delay, int write_behind, |
1131 | unsigned long long size, | |
21e92547 | 1132 | int may_change, int major) |
34163fc7 NB |
1133 | { |
1134 | /* | |
1bf4e2d9 NB |
1135 | * If not may_change, then this is a 'Grow', and the bitmap |
1136 | * must fit after the superblock. | |
1137 | * If may_change, then this is create, and we can put the bitmap | |
1138 | * before the superblock if we like, or may move the start. | |
199171a2 NB |
1139 | * If !may_change, the bitmap MUST live at offset of 1K, until |
1140 | * we get a sysfs interface. | |
34163fc7 | 1141 | * |
f9c25f1d | 1142 | * size is in sectors, chunk is in bytes !!! |
34163fc7 NB |
1143 | */ |
1144 | ||
1bf4e2d9 | 1145 | unsigned long long bits; |
199171a2 | 1146 | unsigned long long max_bits; |
34163fc7 | 1147 | unsigned long long min_chunk; |
199171a2 NB |
1148 | long offset; |
1149 | int chunk = *chunkp; | |
37dfc3d6 | 1150 | int room = 0; |
34163fc7 NB |
1151 | struct mdp_superblock_1 *sb = sbv; |
1152 | bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024); | |
1153 | ||
199171a2 NB |
1154 | switch(st->minor_version) { |
1155 | case 0: | |
1156 | /* either 3K after the superblock, or some amount of space | |
1157 | * before. | |
1158 | */ | |
1159 | if (may_change) { | |
1160 | /* We are creating array, so we *know* how much room has | |
1161 | * been left. | |
1162 | */ | |
1163 | offset = 0; | |
1164 | if (__le64_to_cpu(sb->size) >= 200*1024*1024*2) | |
1165 | room = 128*2; | |
1166 | else if (__le64_to_cpu(sb->size) > 8*1024*1024*2) | |
1167 | room = 64*2; | |
1168 | else { | |
1169 | room = 3*2; | |
1170 | offset = 2; | |
1171 | } | |
1172 | } else { | |
1173 | room = __le64_to_cpu(sb->super_offset) | |
1174 | - __le64_to_cpu(sb->data_offset) | |
1175 | - __le64_to_cpu(sb->data_size); | |
1176 | /* remove '1 ||' when we can set offset via sysfs */ | |
1177 | if (1 || (room < 3*2 && | |
1178 | __le32_to_cpu(sb->max_dev) <= 384)) { | |
1179 | room = 3*2; | |
1180 | offset = 1*2; | |
1181 | } else { | |
1182 | offset = 0; /* means movable offset */ | |
1183 | } | |
1184 | } | |
1185 | break; | |
1186 | case 1: | |
1187 | case 2: /* between superblock and data */ | |
1188 | if (may_change) { | |
1189 | offset = 4*2; | |
1190 | if (__le64_to_cpu(sb->size) >= 200*1024*1024*2) | |
1191 | room = 128*2; | |
1192 | else if (__le64_to_cpu(sb->size) > 8*1024*1024*2) | |
1193 | room = 64*2; | |
1194 | else | |
1195 | room = 3*2; | |
1196 | } else { | |
1197 | room = __le64_to_cpu(sb->data_offset) | |
1198 | - __le64_to_cpu(sb->super_offset); | |
1199 | if (1 || __le32_to_cpu(sb->max_dev) <= 384) { | |
1200 | room -= 2; | |
1201 | offset = 2; | |
1202 | } else { | |
1203 | room -= 4*2; | |
1204 | offset = 4*2; | |
1205 | } | |
1206 | } | |
1207 | break; | |
ae491d1e NB |
1208 | default: |
1209 | return 0; | |
199171a2 | 1210 | } |
1bf4e2d9 | 1211 | |
199171a2 NB |
1212 | if (chunk == UnSet && room > 128*2) |
1213 | /* Limit to 128K of bitmap when chunk size not requested */ | |
1214 | room = 128*2; | |
1bf4e2d9 | 1215 | |
199171a2 | 1216 | max_bits = (room * 512 - sizeof(bitmap_super_t)) * 8; |
34163fc7 NB |
1217 | |
1218 | min_chunk = 4096; /* sub-page chunks don't work yet.. */ | |
f9c25f1d | 1219 | bits = (size*512)/min_chunk +1; |
34163fc7 NB |
1220 | while (bits > max_bits) { |
1221 | min_chunk *= 2; | |
1222 | bits = (bits+1)/2; | |
1223 | } | |
1224 | if (chunk == UnSet) | |
1225 | chunk = min_chunk; | |
1226 | else if (chunk < min_chunk) | |
1227 | return 0; /* chunk size too small */ | |
21e92547 NB |
1228 | if (chunk == 0) /* rounding problem */ |
1229 | return 0; | |
34163fc7 | 1230 | |
199171a2 NB |
1231 | if (offset == 0) { |
1232 | bits = (size*512) / chunk + 1; | |
1233 | room = ((bits+7)/8 + sizeof(bitmap_super_t) +511)/512; | |
1234 | offset = -room; | |
1235 | } | |
1236 | ||
1237 | sb->bitmap_offset = __cpu_to_le32(offset); | |
34163fc7 NB |
1238 | |
1239 | sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1); | |
29e766a5 | 1240 | memset(bms, 0, sizeof(*bms)); |
34163fc7 | 1241 | bms->magic = __cpu_to_le32(BITMAP_MAGIC); |
dcec9ee5 | 1242 | bms->version = __cpu_to_le32(major); |
34163fc7 NB |
1243 | uuid_from_super1((int*)bms->uuid, sb); |
1244 | bms->chunksize = __cpu_to_le32(chunk); | |
1245 | bms->daemon_sleep = __cpu_to_le32(delay); | |
f9c25f1d | 1246 | bms->sync_size = __cpu_to_le64(size); |
34163fc7 NB |
1247 | bms->write_behind = __cpu_to_le32(write_behind); |
1248 | ||
199171a2 | 1249 | *chunkp = chunk; |
34163fc7 NB |
1250 | return 1; |
1251 | } | |
1252 | ||
1253 | ||
14263cf1 | 1254 | static void locate_bitmap1(struct supertype *st, int fd, void *sbv) |
34163fc7 | 1255 | { |
34163fc7 | 1256 | unsigned long long offset; |
1e0d770c NB |
1257 | struct mdp_superblock_1 *sb; |
1258 | int mustfree = 0; | |
34163fc7 | 1259 | |
1e0d770c NB |
1260 | if (!sbv) { |
1261 | if (st->ss->load_super(st, fd, &sbv, NULL)) | |
f6d75de8 | 1262 | return; /* no error I hope... */ |
1e0d770c NB |
1263 | mustfree = 1; |
1264 | } | |
1265 | sb = sbv; | |
e478dc86 | 1266 | |
1bf4e2d9 NB |
1267 | offset = __le64_to_cpu(sb->super_offset); |
1268 | offset += (long) __le32_to_cpu(sb->bitmap_offset); | |
1e0d770c | 1269 | if (mustfree) |
f6d75de8 | 1270 | free(sb); |
1bf4e2d9 | 1271 | lseek64(fd, offset<<9, 0); |
34163fc7 NB |
1272 | } |
1273 | ||
14263cf1 | 1274 | static int write_bitmap1(struct supertype *st, int fd, void *sbv) |
34163fc7 NB |
1275 | { |
1276 | struct mdp_superblock_1 *sb = sbv; | |
1bf4e2d9 | 1277 | bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024); |
34163fc7 NB |
1278 | int rv = 0; |
1279 | ||
1280 | int towrite, n; | |
1281 | char buf[4096]; | |
1282 | ||
f6d75de8 | 1283 | locate_bitmap1(st, fd, sbv); |
34163fc7 | 1284 | |
1e0d770c NB |
1285 | if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) != |
1286 | sizeof(bitmap_super_t)) | |
1287 | return -2; | |
1bf4e2d9 NB |
1288 | towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9); |
1289 | towrite = (towrite+7) >> 3; /* bits to bytes */ | |
34163fc7 NB |
1290 | memset(buf, 0xff, sizeof(buf)); |
1291 | while (towrite > 0) { | |
1292 | n = towrite; | |
1293 | if (n > sizeof(buf)) | |
1294 | n = sizeof(buf); | |
1295 | n = write(fd, buf, n); | |
1296 | if (n > 0) | |
1297 | towrite -= n; | |
1298 | else | |
1299 | break; | |
1300 | } | |
1301 | fsync(fd); | |
1302 | if (towrite) | |
1303 | rv = -2; | |
1304 | ||
1305 | return rv; | |
82d9eba6 NB |
1306 | } |
1307 | ||
1308 | struct superswitch super1 = { | |
c7654afc | 1309 | #ifndef MDASSEMBLE |
82d9eba6 NB |
1310 | .examine_super = examine_super1, |
1311 | .brief_examine_super = brief_examine_super1, | |
1312 | .detail_super = detail_super1, | |
1313 | .brief_detail_super = brief_detail_super1, | |
c7654afc | 1314 | #endif |
83b6208e | 1315 | .match_home = match_home1, |
82d9eba6 NB |
1316 | .uuid_from_super = uuid_from_super1, |
1317 | .getinfo_super = getinfo_super1, | |
1318 | .update_super = update_super1, | |
82d9eba6 NB |
1319 | .init_super = init_super1, |
1320 | .add_to_super = add_to_super1, | |
1321 | .store_super = store_super1, | |
1322 | .write_init_super = write_init_super1, | |
1323 | .compare_super = compare_super1, | |
1324 | .load_super = load_super1, | |
1325 | .match_metadata_desc = match_metadata_desc1, | |
1326 | .avail_size = avail_size1, | |
34163fc7 NB |
1327 | .add_internal_bitmap = add_internal_bitmap1, |
1328 | .locate_bitmap = locate_bitmap1, | |
1329 | .write_bitmap = write_bitmap1, | |
82d9eba6 | 1330 | .major = 1, |
f277ce36 NB |
1331 | #if __BYTE_ORDER == BIG_ENDIAN |
1332 | .swapuuid = 0, | |
1333 | #else | |
1334 | .swapuuid = 1, | |
1335 | #endif | |
82d9eba6 | 1336 | }; |