--update=resync did exactly the wrong thing for version1 metadata.
[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
488 if (strcmp(update, "force")==0) {
c4d831e1
NB
489 if (sb->events != __cpu_to_le64(info->events))
490 rv = 1;
6d3d5804 491 sb->events = __cpu_to_le64(info->events);
82d9eba6
NB
492 switch(__le32_to_cpu(sb->level)) {
493 case 5: case 4: case 6:
494 /* need to force clean */
c4d831e1
NB
495 if (sb->resync_offset != ~0ULL)
496 rv = 1;
82d9eba6
NB
497 sb->resync_offset = ~0ULL;
498 }
499 }
500 if (strcmp(update, "assemble")==0) {
501 int d = info->disk.number;
502 int want;
503 if (info->disk.state == 6)
504 want = __cpu_to_le32(info->disk.raid_disk);
505 else
506 want = 0xFFFF;
507 if (sb->dev_roles[d] != want) {
508 sb->dev_roles[d] = want;
509 rv = 1;
510 }
511 }
512#if 0
513 if (strcmp(update, "newdev") == 0) {
514 int d = info->disk.number;
515 memset(&sb->disks[d], 0, sizeof(sb->disks[d]));
516 sb->disks[d].number = d;
517 sb->disks[d].major = info->disk.major;
518 sb->disks[d].minor = info->disk.minor;
519 sb->disks[d].raid_disk = info->disk.raid_disk;
520 sb->disks[d].state = info->disk.state;
521 sb->this_disk = sb->disks[d];
522 }
523#endif
524 if (strcmp(update, "grow") == 0) {
525 sb->raid_disks = __cpu_to_le32(info->array.raid_disks);
526 /* FIXME */
527 }
528 if (strcmp(update, "resync") == 0) {
529 /* make sure resync happens */
434b7755 530 sb->resync_offset = 0ULL;
82d9eba6 531 }
14263cf1 532 if (strcmp(update, "uuid") == 0) {
838acbc2 533 memcpy(sb->set_uuid, info->uuid, 16);
14263cf1
NB
534 if (__le32_to_cpu(sb->feature_map)&MD_FEATURE_BITMAP_OFFSET) {
535 struct bitmap_super_s *bm;
536 bm = (struct bitmap_super_s*)(sbv+1024);
537 memcpy(bm->uuid, info->uuid, 16);
538 }
539 }
0237e0ca
NB
540 if (strcmp(update, "homehost") == 0 &&
541 homehost) {
542 char *c;
543 update = "name";
544 c = strchr(sb->set_name, ':');
545 if (c)
546 strncpy(info->name, c+1, 31 - (c-sb->set_name));
547 else
548 strncpy(info->name, sb->set_name, 32);
26a0b8fd 549 info->name[32] = 0;
0237e0ca 550 }
c4f12c13
NB
551 if (strcmp(update, "name") == 0) {
552 if (info->name[0] == 0)
553 sprintf(info->name, "%d", info->array.md_minor);
554 memset(sb->set_name, 0, sizeof(sb->set_name));
555 if (homehost &&
556 strchr(info->name, ':') == NULL &&
557 strlen(homehost)+1+strlen(info->name) < 32) {
558 strcpy(sb->set_name, homehost);
559 strcat(sb->set_name, ":");
560 strcat(sb->set_name, info->name);
561 } else
562 strcpy(sb->set_name, info->name);
563 }
353632d9
NB
564 if (strcmp(update, "_reshape_progress")==0)
565 sb->reshape_position = __cpu_to_le64(info->reshape_progress);
82d9eba6
NB
566
567 sb->sb_csum = calc_sb_1_csum(sb);
568 return rv;
569}
570
571
572static __u64 event_super1(void *sbv)
573{
574 struct mdp_superblock_1 *sb = sbv;
575 return __le64_to_cpu(sb->events);
576}
577
05697ec1
NB
578static int init_super1(struct supertype *st, void **sbp, mdu_array_info_t *info,
579 unsigned long long size, char *name, char *homehost)
82d9eba6 580{
34163fc7 581 struct mdp_superblock_1 *sb = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6 582 int spares;
34163fc7 583 int rfd;
05697ec1 584 char defname[10];
82d9eba6
NB
585 memset(sb, 0, 1024);
586
05697ec1 587 if (info->major_version == -1) {
82d9eba6 588 /* zeroing superblock */
05697ec1 589 *sbp = sb;
82d9eba6 590 return 0;
05697ec1 591 }
82d9eba6
NB
592
593 spares = info->working_disks - info->active_disks;
594 if (info->raid_disks + spares > 384) {
595 fprintf(stderr, Name ": too many devices requested: %d+%d > %d\n",
596 info->raid_disks , spares, 384);
597 return 0;
598 }
599
82d9eba6
NB
600 sb->magic = __cpu_to_le32(MD_SB_MAGIC);
601 sb->major_version = __cpu_to_le32(1);
602 sb->feature_map = 0;
603 sb->pad0 = 0;
34163fc7
NB
604
605 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
606 read(rfd, sb->set_uuid, 16) != 16) {
607 *(__u32*)(sb->set_uuid) = random();
608 *(__u32*)(sb->set_uuid+4) = random();
609 *(__u32*)(sb->set_uuid+8) = random();
610 *(__u32*)(sb->set_uuid+12) = random();
611 }
612 if (rfd >= 0) close(rfd);
82d9eba6 613
05697ec1
NB
614 if (name == NULL || *name == 0) {
615 sprintf(defname, "%d", info->md_minor);
616 name = defname;
617 }
947fd4dd 618 memset(sb->set_name, 0, 32);
05697ec1
NB
619 if (homehost &&
620 strchr(name, ':')== NULL &&
621 strlen(homehost)+1+strlen(name) < 32) {
622 strcpy(sb->set_name, homehost);
623 strcat(sb->set_name, ":");
624 strcat(sb->set_name, name);
625 } else
626 strcpy(sb->set_name, name);
82d9eba6
NB
627
628 sb->ctime = __cpu_to_le64((unsigned long long)time(0));
629 sb->level = __cpu_to_le32(info->level);
ea329559 630 sb->layout = __cpu_to_le32(info->layout);
5dd497ee 631 sb->size = __cpu_to_le64(size*2ULL);
82d9eba6
NB
632 sb->chunksize = __cpu_to_le32(info->chunk_size>>9);
633 sb->raid_disks = __cpu_to_le32(info->raid_disks);
634
635 sb->data_offset = __cpu_to_le64(0);
636 sb->data_size = __cpu_to_le64(0);
637 sb->super_offset = __cpu_to_le64(0);
638 sb->recovery_offset = __cpu_to_le64(0);
639
640 sb->utime = sb->ctime;
641 sb->events = __cpu_to_le64(1);
34321279 642 if (info->state & (1<<MD_SB_CLEAN))
82d9eba6
NB
643 sb->resync_offset = ~0ULL;
644 else
645 sb->resync_offset = 0;
34163fc7 646 sb->max_dev = __cpu_to_le32((1024- sizeof(struct mdp_superblock_1))/
82d9eba6
NB
647 sizeof(sb->dev_roles[0]));
648 memset(sb->pad3, 0, sizeof(sb->pad3));
649
650 memset(sb->dev_roles, 0xff, 1024 - sizeof(struct mdp_superblock_1));
651
652 *sbp = sb;
653 return 1;
654}
655
656/* Add a device to the superblock being created */
657static void add_to_super1(void *sbv, mdu_disk_info_t *dk)
658{
659 struct mdp_superblock_1 *sb = sbv;
660 __u16 *rp = sb->dev_roles + dk->number;
dfd4d8ee 661 if ((dk->state & 6) == 6) /* active, sync */
82d9eba6 662 *rp = __cpu_to_le16(dk->raid_disk);
412ca2e5 663 else if ((dk->state & ~2) == 0) /* active or idle -> spare */
82d9eba6 664 *rp = 0xffff;
34163fc7 665 else
82d9eba6
NB
666 *rp = 0xfffe;
667}
668
14263cf1
NB
669static void locate_bitmap1(struct supertype *st, int fd, void *sbv);
670
96395475 671static int store_super1(struct supertype *st, int fd, void *sbv)
82d9eba6
NB
672{
673 struct mdp_superblock_1 *sb = sbv;
6fbba4c9 674 unsigned long long sb_offset;
82d9eba6 675 int sbsize;
5dd497ee
NB
676 unsigned long size;
677 unsigned long long dsize;
96395475 678
5dd497ee
NB
679#ifdef BLKGETSIZE64
680 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
681#endif
682 {
683 if (ioctl(fd, BLKGETSIZE, &size))
684 return 1;
685 else
686 dsize = (unsigned long long)size;
687 } else
688 dsize >>= 9;
96395475 689
5dd497ee 690 if (dsize < 24)
96395475
NB
691 return 2;
692
693 /*
694 * Calculate the position of the superblock.
695 * It is always aligned to a 4K boundary and
696 * depending on minor_version, it can be:
697 * 0: At least 8K, but less than 12K, from end of device
698 * 1: At start of device
699 * 2: 4K from start of device.
700 */
701 switch(st->minor_version) {
702 case 0:
5dd497ee 703 sb_offset = dsize;
96395475
NB
704 sb_offset -= 8*2;
705 sb_offset &= ~(4*2-1);
706 break;
707 case 1:
6fbba4c9 708 sb_offset = 0;
96395475
NB
709 break;
710 case 2:
711 sb_offset = 4*2;
712 break;
713 default:
714 return -EINVAL;
715 }
716
82d9eba6 717
34163fc7 718
6fbba4c9
NB
719 if (sb_offset != __le64_to_cpu(sb->super_offset) &&
720 0 != __le64_to_cpu(sb->super_offset)
96395475
NB
721 ) {
722 fprintf(stderr, Name ": internal error - sb_offset is wrong\n");
723 abort();
724 }
82d9eba6 725
6fbba4c9 726 if (lseek64(fd, sb_offset << 9, 0)< 0LL)
82d9eba6
NB
727 return 3;
728
729 sbsize = sizeof(*sb) + 2 * __le32_to_cpu(sb->max_dev);
730
731 if (write(fd, sb, sbsize) != sbsize)
732 return 4;
733
14263cf1
NB
734 if (sb->feature_map & __cpu_to_le32(MD_FEATURE_BITMAP_OFFSET)) {
735 struct bitmap_super_s *bm = (struct bitmap_super_s*)
41a3b72a 736 (((char*)sb)+1024);
14263cf1
NB
737 if (__le32_to_cpu(bm->magic) == BITMAP_MAGIC) {
738 locate_bitmap1(st, fd, sbv);
9fca7d62
NB
739 if (write(fd, bm, sizeof(*bm)) != sizeof(*bm))
740 return 5;
14263cf1
NB
741 }
742 }
570c0542 743 fsync(fd);
82d9eba6
NB
744 return 0;
745}
746
892debc8
NB
747static int load_super1(struct supertype *st, int fd, void **sbp, char *devname);
748
34163fc7 749static int write_init_super1(struct supertype *st, void *sbv,
1bf4e2d9 750 mdu_disk_info_t *dinfo, char *devname)
82d9eba6
NB
751{
752 struct mdp_superblock_1 *sb = sbv;
e478dc86 753 void *refsbv = NULL;
892debc8 754 int fd = open(devname, O_RDWR | O_EXCL);
dfe47e00 755 int rfd;
82d9eba6
NB
756 int rv;
757
8fac0577
NB
758 unsigned long size, space;
759 unsigned long long dsize, array_size;
82d9eba6
NB
760 long long sb_offset;
761
762
763 if (fd < 0) {
764 fprintf(stderr, Name ": Failed to open %s to write superblock\n",
765 devname);
766 return -1;
767 }
768
769 sb->dev_number = __cpu_to_le32(dinfo->number);
34163fc7 770 if (dinfo->state & (1<<MD_DISK_WRITEMOSTLY))
83205b64 771 sb->devflags |= __cpu_to_le32(WriteMostly1);
892debc8 772
dfe47e00
NB
773 if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
774 read(rfd, sb->device_uuid, 16) != 16) {
775 *(__u32*)(sb->device_uuid) = random();
776 *(__u32*)(sb->device_uuid+4) = random();
777 *(__u32*)(sb->device_uuid+8) = random();
778 *(__u32*)(sb->device_uuid+12) = random();
779 }
780 if (rfd >= 0) close(rfd);
892debc8
NB
781 sb->events = 0;
782
e478dc86
NB
783 if (load_super1(st, fd, &refsbv, NULL)==0) {
784 struct mdp_superblock_1 *refsb = refsbv;
785
892debc8
NB
786 memcpy(sb->device_uuid, refsb->device_uuid, 16);
787 if (memcmp(sb->set_uuid, refsb->set_uuid, 16)==0) {
788 /* same array, so preserve events and dev_number */
789 sb->events = refsb->events;
7eae7080
NB
790 /* bugs in 2.6.17 and earlier mean the dev_number
791 * chosen in Manage must be preserved
792 */
793 if (get_linux_version() >= 2006018)
794 sb->dev_number = refsb->dev_number;
892debc8
NB
795 }
796 free(refsb);
797 }
34163fc7 798
5dd497ee
NB
799#ifdef BLKGETSIZE64
800 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
801#endif
802 {
803 if (ioctl(fd, BLKGETSIZE, &size))
804 return 1;
805 else
806 dsize = size;
807 } else
808 dsize >>= 9;
82d9eba6 809
5dd497ee 810 if (dsize < 24) {
024ce7fe 811 close(fd);
82d9eba6 812 return 2;
024ce7fe 813 }
82d9eba6
NB
814
815
816 /*
817 * Calculate the position of the superblock.
818 * It is always aligned to a 4K boundary and
819 * depending on minor_version, it can be:
820 * 0: At least 8K, but less than 12K, from end of device
821 * 1: At start of device
822 * 2: 4K from start of device.
21e92547
NB
823 * Depending on the array size, we might leave extra space
824 * for a bitmap.
82d9eba6 825 */
8fac0577 826 array_size = __le64_to_cpu(sb->size);
82d9eba6
NB
827 switch(st->minor_version) {
828 case 0:
5dd497ee 829 sb_offset = dsize;
82d9eba6
NB
830 sb_offset -= 8*2;
831 sb_offset &= ~(4*2-1);
832 sb->super_offset = __cpu_to_le64(sb_offset);
833 sb->data_offset = __cpu_to_le64(0);
561832f9 834 if (sb_offset-64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
835 sb->data_size = __cpu_to_le64(sb_offset-64*2);
836 else
837 sb->data_size = __cpu_to_le64(sb_offset);
82d9eba6
NB
838 break;
839 case 1:
840 sb->super_offset = __cpu_to_le64(0);
561832f9 841 if (dsize - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
842 space = 64*2;
843 else
844 space = 4*2;
845 sb->data_offset = __cpu_to_le64(space); /* leave space for super and bitmap */
846 sb->data_size = __cpu_to_le64(dsize - space);
82d9eba6
NB
847 break;
848 case 2:
849 sb_offset = 4*2;
561832f9 850 if (dsize - 4*2 - 64*2 >= array_size && array_size > 8*1024*1024*2)
21e92547
NB
851 space = 64*2;
852 else
853 space = 4*2;
82d9eba6 854 sb->super_offset = __cpu_to_le64(sb_offset);
21e92547
NB
855 sb->data_offset = __cpu_to_le64(sb_offset+space);
856 sb->data_size = __cpu_to_le64(dsize - 4*2 - space);
82d9eba6
NB
857 break;
858 default:
859 return -EINVAL;
860 }
861
862
863 sb->sb_csum = calc_sb_1_csum(sb);
96395475 864 rv = store_super1(st, fd, sb);
82d9eba6
NB
865 if (rv)
866 fprintf(stderr, Name ": failed to write superblock to %s\n", devname);
34163fc7 867
1bf4e2d9
NB
868 if (rv == 0 && (__le32_to_cpu(sb->feature_map) & 1))
869 rv = st->ss->write_bitmap(st, fd, sbv);
024ce7fe 870 close(fd);
82d9eba6
NB
871 return rv;
872}
873
874static int compare_super1(void **firstp, void *secondv)
875{
876 /*
877 * return:
878 * 0 same, or first was empty, and second was copied
879 * 1 second had wrong number
880 * 2 wrong uuid
881 * 3 wrong other info
882 */
883 struct mdp_superblock_1 *first = *firstp;
884 struct mdp_superblock_1 *second = secondv;
885
886 if (second->magic != __cpu_to_le32(MD_SB_MAGIC))
887 return 1;
888 if (second->major_version != __cpu_to_le32(1))
889 return 1;
890
891 if (!first) {
14263cf1
NB
892 first = malloc(1024+sizeof(bitmap_super_t));
893 memcpy(first, second, 1024+sizeof(bitmap_super_t));
82d9eba6
NB
894 *firstp = first;
895 return 0;
896 }
897 if (memcmp(first->set_uuid, second->set_uuid, 16)!= 0)
898 return 2;
899
900 if (first->ctime != second->ctime ||
901 first->level != second->level ||
902 first->layout != second->layout ||
903 first->size != second->size ||
904 first->chunksize != second->chunksize ||
905 first->raid_disks != second->raid_disks)
906 return 3;
907 return 0;
908}
909
910static int load_super1(struct supertype *st, int fd, void **sbp, char *devname)
911{
912 unsigned long size;
5dd497ee 913 unsigned long long dsize;
82d9eba6
NB
914 unsigned long long sb_offset;
915 struct mdp_superblock_1 *super;
14263cf1
NB
916 int uuid[4];
917 struct bitmap_super_s *bsb;
82d9eba6
NB
918
919
920 if (st->ss == NULL) {
570c0542
NB
921 int bestvers = -1;
922 __u64 bestctime = 0;
923 /* guess... choose latest ctime */
82d9eba6
NB
924 st->ss = &super1;
925 for (st->minor_version = 0; st->minor_version <= 2 ; st->minor_version++) {
926 switch(load_super1(st, fd, sbp, devname)) {
570c0542
NB
927 case 0: super = *sbp;
928 if (bestvers == -1 ||
929 bestctime < __le64_to_cpu(super->ctime)) {
930 bestvers = st->minor_version;
931 bestctime = __le64_to_cpu(super->ctime);
932 }
933 free(super);
934 *sbp = NULL;
935 break;
82d9eba6
NB
936 case 1: st->ss = NULL; return 1; /*bad device */
937 case 2: break; /* bad, try next */
938 }
939 }
570c0542
NB
940 if (bestvers != -1) {
941 int rv;
942 st->minor_version = bestvers;
943 st->ss = &super1;
ea329559 944 st->max_devs = 384;
570c0542
NB
945 rv = load_super1(st, fd, sbp, devname);
946 if (rv) st->ss = NULL;
947 return rv;
948 }
82d9eba6
NB
949 st->ss = NULL;
950 return 2;
951 }
5dd497ee
NB
952#ifdef BLKGETSIZE64
953 if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
954#endif
955 {
956 if (ioctl(fd, BLKGETSIZE, &size)) {
957 if (devname)
958 fprintf(stderr, Name ": cannot find device size for %s: %s\n",
959 devname, strerror(errno));
960 return 1;
961 }
962 dsize = size;
963 } else
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
34163fc7 1007 super = malloc(1024 + sizeof(bitmap_super_t));
82d9eba6
NB
1008
1009 if (read(fd, super, 1024) != 1024) {
1010 if (devname)
1011 fprintf(stderr, Name ": Cannot read superblock on %s\n",
1012 devname);
1013 free(super);
1014 return 1;
1015 }
1016
1017 if (__le32_to_cpu(super->magic) != MD_SB_MAGIC) {
1018 if (devname)
1019 fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
1020 devname, MD_SB_MAGIC, __le32_to_cpu(super->magic));
1021 free(super);
1022 return 2;
1023 }
1024
1025 if (__le32_to_cpu(super->major_version) != 1) {
1026 if (devname)
1027 fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
1028 devname, __le32_to_cpu(super->major_version));
1029 free(super);
1030 return 2;
1031 }
1032 if (__le64_to_cpu(super->super_offset) != sb_offset) {
1033 if (devname)
1034 fprintf(stderr, Name ": No superblock found on %s (super_offset is wrong)\n",
1035 devname);
1036 free(super);
1037 return 2;
1038 }
1039 *sbp = super;
14263cf1
NB
1040
1041 /* Now check on the bitmap superblock */
1042 if ((__le32_to_cpu(super->feature_map)&MD_FEATURE_BITMAP_OFFSET) == 0)
1043 return 0;
1044 /* Read the bitmap superblock and make sure it looks
1045 * valid. If it doesn't clear the bit. An --assemble --force
1046 * should get that written out.
1047 */
1048 locate_bitmap1(st, fd, super);
1049 if (read(fd, ((char*)super)+1024, sizeof(struct bitmap_super_s))
1050 != sizeof(struct bitmap_super_s))
1051 goto no_bitmap;
1052
1053 uuid_from_super1(uuid, super);
1054 bsb = (struct bitmap_super_s *)(((char*)super)+1024);
1055 if (__le32_to_cpu(bsb->magic) != BITMAP_MAGIC ||
1056 memcmp(bsb->uuid, uuid, 16) != 0)
1057 goto no_bitmap;
1058 return 0;
1059
1060 no_bitmap:
1061 super->feature_map = __cpu_to_le32(__le32_to_cpu(super->feature_map) & ~1);
82d9eba6
NB
1062 return 0;
1063}
1064
1065
1066static struct supertype *match_metadata_desc1(char *arg)
1067{
1068 struct supertype *st = malloc(sizeof(*st));
1069 if (!st) return st;
1070
1071 st->ss = &super1;
ea329559 1072 st->max_devs = 384;
82d9eba6 1073 if (strcmp(arg, "1") == 0 ||
576d6d83
NB
1074 strcmp(arg, "1.0") == 0 ||
1075 strcmp(arg, "default/large") == 0) {
82d9eba6
NB
1076 st->minor_version = 0;
1077 return st;
1078 }
1079 if (strcmp(arg, "1.1") == 0) {
1080 st->minor_version = 1;
1081 return st;
1082 }
1083 if (strcmp(arg, "1.2") == 0) {
1084 st->minor_version = 2;
1085 return st;
1086 }
1087
1088 free(st);
1089 return NULL;
1090}
1091
34163fc7
NB
1092/* find available size on device with this devsize, using
1093 * superblock type st, and reserving 'reserve' sectors for
1094 * a possible bitmap
1095 */
1bf4e2d9 1096static __u64 avail_size1(struct supertype *st, __u64 devsize)
82d9eba6
NB
1097{
1098 if (devsize < 24)
1099 return 0;
1100
21e92547
NB
1101 /* if the device is bigger than 8Gig, save 64k for bitmap usage,
1102 * if biffer than 200Gig, save 128k
1103 */
1104 if (devsize > 200*1024*1024*2)
1105 devsize -= 128*2;
1106 else if (devsize > 8*1024*1024*2)
1107 devsize -= 64*2;
1108
34163fc7
NB
1109 switch(st->minor_version) {
1110 case 0:
1bf4e2d9
NB
1111 /* at end */
1112 return ((devsize - 8*2 ) & ~(4*2-1));
34163fc7 1113 case 1:
1bf4e2d9
NB
1114 /* at start, 4K for superblock and possible bitmap */
1115 return devsize - 4*2;
34163fc7 1116 case 2:
1bf4e2d9
NB
1117 /* 4k from start, 4K for superblock and possible bitmap */
1118 return devsize - (4+4)*2;
34163fc7
NB
1119 }
1120 return 0;
1121}
1122
1bf4e2d9
NB
1123static int
1124add_internal_bitmap1(struct supertype *st, void *sbv,
21e92547
NB
1125 int chunk, int delay, int write_behind, unsigned long long size,
1126 int may_change, int major)
34163fc7
NB
1127{
1128 /*
1bf4e2d9
NB
1129 * If not may_change, then this is a 'Grow', and the bitmap
1130 * must fit after the superblock.
1131 * If may_change, then this is create, and we can put the bitmap
1132 * before the superblock if we like, or may move the start.
1133 * For now, just squeeze the bitmap into 3k and don't change anything.
34163fc7 1134 *
f9c25f1d 1135 * size is in sectors, chunk is in bytes !!!
34163fc7
NB
1136 */
1137
1bf4e2d9 1138 unsigned long long bits;
17467116 1139 unsigned long long max_bits = (6*512 - sizeof(bitmap_super_t)) * 8;
34163fc7
NB
1140 unsigned long long min_chunk;
1141 struct mdp_superblock_1 *sb = sbv;
1142 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb) + 1024);
1143
1bf4e2d9
NB
1144 if (st->minor_version && !may_change &&
1145 __le64_to_cpu(sb->data_offset) - __le64_to_cpu(sb->super_offset) < 8)
1146 return 0; /* doesn't fit */
1147
1148
34163fc7
NB
1149
1150 min_chunk = 4096; /* sub-page chunks don't work yet.. */
f9c25f1d 1151 bits = (size*512)/min_chunk +1;
34163fc7
NB
1152 while (bits > max_bits) {
1153 min_chunk *= 2;
1154 bits = (bits+1)/2;
1155 }
1156 if (chunk == UnSet)
1157 chunk = min_chunk;
1158 else if (chunk < min_chunk)
1159 return 0; /* chunk size too small */
21e92547
NB
1160 if (chunk == 0) /* rounding problem */
1161 return 0;
34163fc7 1162
1bf4e2d9 1163 sb->bitmap_offset = __cpu_to_le32(2);
34163fc7
NB
1164
1165 sb->feature_map = __cpu_to_le32(__le32_to_cpu(sb->feature_map) | 1);
29e766a5 1166 memset(bms, 0, sizeof(*bms));
34163fc7 1167 bms->magic = __cpu_to_le32(BITMAP_MAGIC);
dcec9ee5 1168 bms->version = __cpu_to_le32(major);
34163fc7
NB
1169 uuid_from_super1((int*)bms->uuid, sb);
1170 bms->chunksize = __cpu_to_le32(chunk);
1171 bms->daemon_sleep = __cpu_to_le32(delay);
f9c25f1d 1172 bms->sync_size = __cpu_to_le64(size);
34163fc7
NB
1173 bms->write_behind = __cpu_to_le32(write_behind);
1174
34163fc7
NB
1175 return 1;
1176}
1177
1178
14263cf1 1179static void locate_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7 1180{
34163fc7 1181 unsigned long long offset;
1e0d770c
NB
1182 struct mdp_superblock_1 *sb;
1183 int mustfree = 0;
34163fc7 1184
1e0d770c
NB
1185 if (!sbv) {
1186 if (st->ss->load_super(st, fd, &sbv, NULL))
f6d75de8 1187 return; /* no error I hope... */
1e0d770c
NB
1188 mustfree = 1;
1189 }
1190 sb = sbv;
e478dc86 1191
1bf4e2d9
NB
1192 offset = __le64_to_cpu(sb->super_offset);
1193 offset += (long) __le32_to_cpu(sb->bitmap_offset);
1e0d770c 1194 if (mustfree)
f6d75de8 1195 free(sb);
1bf4e2d9 1196 lseek64(fd, offset<<9, 0);
34163fc7
NB
1197}
1198
14263cf1 1199static int write_bitmap1(struct supertype *st, int fd, void *sbv)
34163fc7
NB
1200{
1201 struct mdp_superblock_1 *sb = sbv;
1bf4e2d9 1202 bitmap_super_t *bms = (bitmap_super_t*)(((char*)sb)+1024);
34163fc7
NB
1203 int rv = 0;
1204
1205 int towrite, n;
1206 char buf[4096];
1207
f6d75de8 1208 locate_bitmap1(st, fd, sbv);
34163fc7 1209
1e0d770c
NB
1210 if (write(fd, ((char*)sb)+1024, sizeof(bitmap_super_t)) !=
1211 sizeof(bitmap_super_t))
1212 return -2;
1bf4e2d9
NB
1213 towrite = __le64_to_cpu(bms->sync_size) / (__le32_to_cpu(bms->chunksize)>>9);
1214 towrite = (towrite+7) >> 3; /* bits to bytes */
34163fc7
NB
1215 memset(buf, 0xff, sizeof(buf));
1216 while (towrite > 0) {
1217 n = towrite;
1218 if (n > sizeof(buf))
1219 n = sizeof(buf);
1220 n = write(fd, buf, n);
1221 if (n > 0)
1222 towrite -= n;
1223 else
1224 break;
1225 }
1226 fsync(fd);
1227 if (towrite)
1228 rv = -2;
1229
1230 return rv;
82d9eba6
NB
1231}
1232
1233struct superswitch super1 = {
c7654afc 1234#ifndef MDASSEMBLE
82d9eba6
NB
1235 .examine_super = examine_super1,
1236 .brief_examine_super = brief_examine_super1,
1237 .detail_super = detail_super1,
1238 .brief_detail_super = brief_detail_super1,
c7654afc 1239#endif
83b6208e 1240 .match_home = match_home1,
82d9eba6
NB
1241 .uuid_from_super = uuid_from_super1,
1242 .getinfo_super = getinfo_super1,
1243 .update_super = update_super1,
1244 .event_super = event_super1,
1245 .init_super = init_super1,
1246 .add_to_super = add_to_super1,
1247 .store_super = store_super1,
1248 .write_init_super = write_init_super1,
1249 .compare_super = compare_super1,
1250 .load_super = load_super1,
1251 .match_metadata_desc = match_metadata_desc1,
1252 .avail_size = avail_size1,
34163fc7
NB
1253 .add_internal_bitmap = add_internal_bitmap1,
1254 .locate_bitmap = locate_bitmap1,
1255 .write_bitmap = write_bitmap1,
82d9eba6 1256 .major = 1,
f277ce36
NB
1257#if __BYTE_ORDER == BIG_ENDIAN
1258 .swapuuid = 0,
1259#else
1260 .swapuuid = 1,
1261#endif
82d9eba6 1262};