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