]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-ddf.c
DDF: fix endianness of refnum in debug messages
[thirdparty/mdadm.git] / super-ddf.c
CommitLineData
a322f70c
DW
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
e736b623 4 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
a322f70c
DW
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: <neil@brown.name>
23 *
24 * Specifications for DDF takes from Common RAID DDF Specification Revision 1.2
25 * (July 28 2006). Reused by permission of SNIA.
26 */
27
28#define HAVE_STDINT_H 1
29#include "mdadm.h"
549e9569 30#include "mdmon.h"
a322f70c
DW
31#include "sha1.h"
32#include <values.h>
33
a322f70c
DW
34/* a non-official T10 name for creation GUIDs */
35static char T10[] = "Linux-MD";
36
37/* DDF timestamps are 1980 based, so we need to add
38 * second-in-decade-of-seventies to convert to linux timestamps.
39 * 10 years with 2 leap years.
40 */
41#define DECADE (3600*24*(365*10+2))
42unsigned long crc32(
43 unsigned long crc,
44 const unsigned char *buf,
45 unsigned len);
46
bedbf68a 47#define DDF_NOTFOUND (~0U)
48#define DDF_CONTAINER (DDF_NOTFOUND-1)
49
a322f70c
DW
50/* The DDF metadata handling.
51 * DDF metadata lives at the end of the device.
52 * The last 512 byte block provides an 'anchor' which is used to locate
53 * the rest of the metadata which usually lives immediately behind the anchor.
54 *
55 * Note:
56 * - all multibyte numeric fields are bigendian.
57 * - all strings are space padded.
58 *
59 */
60
61/* Primary Raid Level (PRL) */
62#define DDF_RAID0 0x00
63#define DDF_RAID1 0x01
64#define DDF_RAID3 0x03
65#define DDF_RAID4 0x04
66#define DDF_RAID5 0x05
67#define DDF_RAID1E 0x11
68#define DDF_JBOD 0x0f
69#define DDF_CONCAT 0x1f
70#define DDF_RAID5E 0x15
71#define DDF_RAID5EE 0x25
59e36268 72#define DDF_RAID6 0x06
a322f70c
DW
73
74/* Raid Level Qualifier (RLQ) */
75#define DDF_RAID0_SIMPLE 0x00
76#define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
77#define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
78#define DDF_RAID3_0 0x00 /* parity in first extent */
79#define DDF_RAID3_N 0x01 /* parity in last extent */
80#define DDF_RAID4_0 0x00 /* parity in first extent */
81#define DDF_RAID4_N 0x01 /* parity in last extent */
82/* these apply to raid5e and raid5ee as well */
83#define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
59e36268 84#define DDF_RAID6_0_RESTART 0x01 /* raid6 different from raid5 here!!! */
a322f70c
DW
85#define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
86#define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
87
88#define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
89#define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
90
91/* Secondary RAID Level (SRL) */
92#define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
93#define DDF_2MIRRORED 0x01
94#define DDF_2CONCAT 0x02
95#define DDF_2SPANNED 0x03 /* This is also weird - be careful */
96
97/* Magic numbers */
98#define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
99#define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
100#define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
101#define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
102#define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
103#define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
104#define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
105#define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
106#define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
107#define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
108
109#define DDF_GUID_LEN 24
59e36268
NB
110#define DDF_REVISION_0 "01.00.00"
111#define DDF_REVISION_2 "01.02.00"
a322f70c
DW
112
113struct ddf_header {
88c164f4 114 __u32 magic; /* DDF_HEADER_MAGIC */
a322f70c
DW
115 __u32 crc;
116 char guid[DDF_GUID_LEN];
59e36268 117 char revision[8]; /* 01.02.00 */
a322f70c
DW
118 __u32 seq; /* starts at '1' */
119 __u32 timestamp;
120 __u8 openflag;
121 __u8 foreignflag;
122 __u8 enforcegroups;
123 __u8 pad0; /* 0xff */
124 __u8 pad1[12]; /* 12 * 0xff */
125 /* 64 bytes so far */
126 __u8 header_ext[32]; /* reserved: fill with 0xff */
127 __u64 primary_lba;
128 __u64 secondary_lba;
129 __u8 type;
130 __u8 pad2[3]; /* 0xff */
131 __u32 workspace_len; /* sectors for vendor space -
132 * at least 32768(sectors) */
133 __u64 workspace_lba;
134 __u16 max_pd_entries; /* one of 15, 63, 255, 1023, 4095 */
135 __u16 max_vd_entries; /* 2^(4,6,8,10,12)-1 : i.e. as above */
136 __u16 max_partitions; /* i.e. max num of configuration
137 record entries per disk */
138 __u16 config_record_len; /* 1 +ROUNDUP(max_primary_element_entries
139 *12/512) */
140 __u16 max_primary_element_entries; /* 16, 64, 256, 1024, or 4096 */
141 __u8 pad3[54]; /* 0xff */
142 /* 192 bytes so far */
143 __u32 controller_section_offset;
144 __u32 controller_section_length;
145 __u32 phys_section_offset;
146 __u32 phys_section_length;
147 __u32 virt_section_offset;
148 __u32 virt_section_length;
149 __u32 config_section_offset;
150 __u32 config_section_length;
151 __u32 data_section_offset;
152 __u32 data_section_length;
153 __u32 bbm_section_offset;
154 __u32 bbm_section_length;
155 __u32 diag_space_offset;
156 __u32 diag_space_length;
157 __u32 vendor_offset;
158 __u32 vendor_length;
159 /* 256 bytes so far */
160 __u8 pad4[256]; /* 0xff */
161};
162
163/* type field */
164#define DDF_HEADER_ANCHOR 0x00
165#define DDF_HEADER_PRIMARY 0x01
166#define DDF_HEADER_SECONDARY 0x02
167
168/* The content of the 'controller section' - global scope */
169struct ddf_controller_data {
88c164f4 170 __u32 magic; /* DDF_CONTROLLER_MAGIC */
a322f70c
DW
171 __u32 crc;
172 char guid[DDF_GUID_LEN];
173 struct controller_type {
174 __u16 vendor_id;
175 __u16 device_id;
176 __u16 sub_vendor_id;
177 __u16 sub_device_id;
178 } type;
179 char product_id[16];
180 __u8 pad[8]; /* 0xff */
181 __u8 vendor_data[448];
182};
183
184/* The content of phys_section - global scope */
185struct phys_disk {
88c164f4 186 __u32 magic; /* DDF_PHYS_RECORDS_MAGIC */
a322f70c
DW
187 __u32 crc;
188 __u16 used_pdes;
189 __u16 max_pdes;
190 __u8 pad[52];
191 struct phys_disk_entry {
192 char guid[DDF_GUID_LEN];
193 __u32 refnum;
194 __u16 type;
195 __u16 state;
196 __u64 config_size; /* DDF structures must be after here */
197 char path[18]; /* another horrible structure really */
198 __u8 pad[6];
199 } entries[0];
200};
201
202/* phys_disk_entry.type is a bitmap - bigendian remember */
203#define DDF_Forced_PD_GUID 1
204#define DDF_Active_in_VD 2
88c164f4 205#define DDF_Global_Spare 4 /* VD_CONF records are ignored */
a322f70c
DW
206#define DDF_Spare 8 /* overrides Global_spare */
207#define DDF_Foreign 16
208#define DDF_Legacy 32 /* no DDF on this device */
209
210#define DDF_Interface_mask 0xf00
211#define DDF_Interface_SCSI 0x100
212#define DDF_Interface_SAS 0x200
213#define DDF_Interface_SATA 0x300
214#define DDF_Interface_FC 0x400
215
216/* phys_disk_entry.state is a bigendian bitmap */
217#define DDF_Online 1
218#define DDF_Failed 2 /* overrides 1,4,8 */
219#define DDF_Rebuilding 4
220#define DDF_Transition 8
221#define DDF_SMART 16
222#define DDF_ReadErrors 32
223#define DDF_Missing 64
224
225/* The content of the virt_section global scope */
226struct virtual_disk {
88c164f4 227 __u32 magic; /* DDF_VIRT_RECORDS_MAGIC */
a322f70c
DW
228 __u32 crc;
229 __u16 populated_vdes;
230 __u16 max_vdes;
231 __u8 pad[52];
232 struct virtual_entry {
233 char guid[DDF_GUID_LEN];
234 __u16 unit;
235 __u16 pad0; /* 0xffff */
236 __u16 guid_crc;
237 __u16 type;
238 __u8 state;
239 __u8 init_state;
240 __u8 pad1[14];
241 char name[16];
242 } entries[0];
243};
244
245/* virtual_entry.type is a bitmap - bigendian */
246#define DDF_Shared 1
247#define DDF_Enforce_Groups 2
248#define DDF_Unicode 4
249#define DDF_Owner_Valid 8
250
251/* virtual_entry.state is a bigendian bitmap */
252#define DDF_state_mask 0x7
253#define DDF_state_optimal 0x0
254#define DDF_state_degraded 0x1
255#define DDF_state_deleted 0x2
256#define DDF_state_missing 0x3
257#define DDF_state_failed 0x4
7a7cc504 258#define DDF_state_part_optimal 0x5
a322f70c
DW
259
260#define DDF_state_morphing 0x8
261#define DDF_state_inconsistent 0x10
262
263/* virtual_entry.init_state is a bigendian bitmap */
264#define DDF_initstate_mask 0x03
265#define DDF_init_not 0x00
7a7cc504
NB
266#define DDF_init_quick 0x01 /* initialisation is progress.
267 * i.e. 'state_inconsistent' */
a322f70c
DW
268#define DDF_init_full 0x02
269
270#define DDF_access_mask 0xc0
271#define DDF_access_rw 0x00
272#define DDF_access_ro 0x80
273#define DDF_access_blocked 0xc0
274
275/* The content of the config_section - local scope
276 * It has multiple records each config_record_len sectors
277 * They can be vd_config or spare_assign
278 */
279
280struct vd_config {
88c164f4 281 __u32 magic; /* DDF_VD_CONF_MAGIC */
a322f70c
DW
282 __u32 crc;
283 char guid[DDF_GUID_LEN];
284 __u32 timestamp;
285 __u32 seqnum;
286 __u8 pad0[24];
287 __u16 prim_elmnt_count;
288 __u8 chunk_shift; /* 0 == 512, 1==1024 etc */
289 __u8 prl;
290 __u8 rlq;
291 __u8 sec_elmnt_count;
292 __u8 sec_elmnt_seq;
293 __u8 srl;
598f0d58
NB
294 __u64 blocks; /* blocks per component could be different
295 * on different component devices...(only
296 * for concat I hope) */
297 __u64 array_blocks; /* blocks in array */
a322f70c
DW
298 __u8 pad1[8];
299 __u32 spare_refs[8];
300 __u8 cache_pol[8];
301 __u8 bg_rate;
302 __u8 pad2[3];
303 __u8 pad3[52];
304 __u8 pad4[192];
305 __u8 v0[32]; /* reserved- 0xff */
306 __u8 v1[32]; /* reserved- 0xff */
307 __u8 v2[16]; /* reserved- 0xff */
308 __u8 v3[16]; /* reserved- 0xff */
309 __u8 vendor[32];
310 __u32 phys_refnum[0]; /* refnum of each disk in sequence */
311 /*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a
312 bvd are always the same size */
313};
57a66662 314#define LBA_OFFSET(ddf, vd) ((__u64 *) &(vd)->phys_refnum[(ddf)->mppe])
a322f70c
DW
315
316/* vd_config.cache_pol[7] is a bitmap */
317#define DDF_cache_writeback 1 /* else writethrough */
318#define DDF_cache_wadaptive 2 /* only applies if writeback */
319#define DDF_cache_readahead 4
320#define DDF_cache_radaptive 8 /* only if doing read-ahead */
321#define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
322#define DDF_cache_wallowed 32 /* enable write caching */
323#define DDF_cache_rallowed 64 /* enable read caching */
324
325struct spare_assign {
88c164f4 326 __u32 magic; /* DDF_SPARE_ASSIGN_MAGIC */
a322f70c
DW
327 __u32 crc;
328 __u32 timestamp;
329 __u8 reserved[7];
330 __u8 type;
331 __u16 populated; /* SAEs used */
332 __u16 max; /* max SAEs */
333 __u8 pad[8];
334 struct spare_assign_entry {
335 char guid[DDF_GUID_LEN];
336 __u16 secondary_element;
337 __u8 pad[6];
338 } spare_ents[0];
339};
340/* spare_assign.type is a bitmap */
341#define DDF_spare_dedicated 0x1 /* else global */
342#define DDF_spare_revertible 0x2 /* else committable */
343#define DDF_spare_active 0x4 /* else not active */
344#define DDF_spare_affinity 0x8 /* enclosure affinity */
345
346/* The data_section contents - local scope */
347struct disk_data {
88c164f4 348 __u32 magic; /* DDF_PHYS_DATA_MAGIC */
a322f70c
DW
349 __u32 crc;
350 char guid[DDF_GUID_LEN];
351 __u32 refnum; /* crc of some magic drive data ... */
352 __u8 forced_ref; /* set when above was not result of magic */
353 __u8 forced_guid; /* set if guid was forced rather than magic */
354 __u8 vendor[32];
355 __u8 pad[442];
356};
357
358/* bbm_section content */
359struct bad_block_log {
360 __u32 magic;
361 __u32 crc;
362 __u16 entry_count;
363 __u32 spare_count;
364 __u8 pad[10];
365 __u64 first_spare;
366 struct mapped_block {
367 __u64 defective_start;
368 __u32 replacement_start;
369 __u16 remap_count;
370 __u8 pad[2];
371 } entries[0];
372};
373
374/* Struct for internally holding ddf structures */
375/* The DDF structure stored on each device is potentially
376 * quite different, as some data is global and some is local.
377 * The global data is:
378 * - ddf header
379 * - controller_data
380 * - Physical disk records
381 * - Virtual disk records
382 * The local data is:
383 * - Configuration records
384 * - Physical Disk data section
385 * ( and Bad block and vendor which I don't care about yet).
386 *
387 * The local data is parsed into separate lists as it is read
388 * and reconstructed for writing. This means that we only need
389 * to make config changes once and they are automatically
390 * propagated to all devices.
391 * Note that the ddf_super has space of the conf and disk data
392 * for this disk and also for a list of all such data.
393 * The list is only used for the superblock that is being
394 * built in Create or Assemble to describe the whole array.
395 */
396struct ddf_super {
6416d527 397 struct ddf_header anchor, primary, secondary;
a322f70c 398 struct ddf_controller_data controller;
6416d527 399 struct ddf_header *active;
a322f70c
DW
400 struct phys_disk *phys;
401 struct virtual_disk *virt;
402 int pdsize, vdsize;
f21e18ca 403 unsigned int max_part, mppe, conf_rec_len;
d2ca6449 404 int currentdev;
18a2f463 405 int updates_pending;
a322f70c 406 struct vcl {
6416d527
NB
407 union {
408 char space[512];
409 struct {
410 struct vcl *next;
f21e18ca 411 unsigned int vcnum; /* index into ->virt */
8ec5d685 412 struct vd_config **other_bvds;
6416d527
NB
413 __u64 *block_sizes; /* NULL if all the same */
414 };
415 };
a322f70c 416 struct vd_config conf;
d2ca6449 417 } *conflist, *currentconf;
a322f70c 418 struct dl {
6416d527
NB
419 union {
420 char space[512];
421 struct {
422 struct dl *next;
423 int major, minor;
424 char *devname;
425 int fd;
426 unsigned long long size; /* sectors */
097bcf00 427 unsigned long long primary_lba; /* sectors */
428 unsigned long long secondary_lba; /* sectors */
429 unsigned long long workspace_lba; /* sectors */
6416d527
NB
430 int pdnum; /* index in ->phys */
431 struct spare_assign *spare;
8592f29d
N
432 void *mdupdate; /* hold metadata update */
433
434 /* These fields used by auto-layout */
435 int raiddisk; /* slot to fill in autolayout */
436 __u64 esize;
6416d527
NB
437 };
438 };
a322f70c 439 struct disk_data disk;
b2280677 440 struct vcl *vlist[0]; /* max_part in size */
2cc2983d 441 } *dlist, *add_list;
a322f70c
DW
442};
443
444#ifndef offsetof
445#define offsetof(t,f) ((size_t)&(((t*)0)->f))
446#endif
447
7d5a7ff3 448#if DEBUG
fb9d0acb 449static int all_ff(const char *guid);
7d5a7ff3 450static void pr_state(struct ddf_super *ddf, const char *msg)
451{
452 unsigned int i;
453 dprintf("%s/%s: ", __func__, msg);
454 for (i = 0; i < __be16_to_cpu(ddf->active->max_vd_entries); i++) {
455 if (all_ff(ddf->virt->entries[i].guid))
456 continue;
457 dprintf("%u(s=%02x i=%02x) ", i,
458 ddf->virt->entries[i].state,
459 ddf->virt->entries[i].init_state);
460 }
461 dprintf("\n");
462}
463#else
464static void pr_state(const struct ddf_super *ddf, const char *msg) {}
465#endif
466
467#define ddf_set_updates_pending(x) \
468 do { (x)->updates_pending = 1; pr_state(x, __func__); } while (0)
469
fcc22180 470static unsigned int get_pd_index_from_refnum(const struct vcl *vc,
471 __u32 refnum, unsigned int nmax,
472 const struct vd_config **bvd,
473 unsigned int *idx);
474
f21e18ca 475static unsigned int calc_crc(void *buf, int len)
a322f70c
DW
476{
477 /* crcs are always at the same place as in the ddf_header */
478 struct ddf_header *ddf = buf;
479 __u32 oldcrc = ddf->crc;
480 __u32 newcrc;
481 ddf->crc = 0xffffffff;
482
483 newcrc = crc32(0, buf, len);
484 ddf->crc = oldcrc;
4abe6b70
N
485 /* The crc is store (like everything) bigendian, so convert
486 * here for simplicity
487 */
488 return __cpu_to_be32(newcrc);
a322f70c
DW
489}
490
a3163bf0 491#define DDF_INVALID_LEVEL 0xff
492#define DDF_NO_SECONDARY 0xff
493static int err_bad_md_layout(const mdu_array_info_t *array)
494{
495 pr_err("RAID%d layout %x with %d disks is unsupported for DDF\n",
496 array->level, array->layout, array->raid_disks);
497 return DDF_INVALID_LEVEL;
498}
499
500static int layout_md2ddf(const mdu_array_info_t *array,
501 struct vd_config *conf)
502{
503 __u16 prim_elmnt_count = __cpu_to_be16(array->raid_disks);
504 __u8 prl = DDF_INVALID_LEVEL, rlq = 0;
505 __u8 sec_elmnt_count = 1;
506 __u8 srl = DDF_NO_SECONDARY;
507
508 switch (array->level) {
509 case LEVEL_LINEAR:
510 prl = DDF_CONCAT;
511 break;
512 case 0:
513 rlq = DDF_RAID0_SIMPLE;
514 prl = DDF_RAID0;
515 break;
516 case 1:
517 switch (array->raid_disks) {
518 case 2:
519 rlq = DDF_RAID1_SIMPLE;
520 break;
521 case 3:
522 rlq = DDF_RAID1_MULTI;
523 break;
524 default:
525 return err_bad_md_layout(array);
526 }
527 prl = DDF_RAID1;
528 break;
529 case 4:
530 if (array->layout != 0)
531 return err_bad_md_layout(array);
532 rlq = DDF_RAID4_N;
533 prl = DDF_RAID4;
534 break;
535 case 5:
536 switch (array->layout) {
537 case ALGORITHM_LEFT_ASYMMETRIC:
538 rlq = DDF_RAID5_N_RESTART;
539 break;
540 case ALGORITHM_RIGHT_ASYMMETRIC:
541 rlq = DDF_RAID5_0_RESTART;
542 break;
543 case ALGORITHM_LEFT_SYMMETRIC:
544 rlq = DDF_RAID5_N_CONTINUE;
545 break;
546 case ALGORITHM_RIGHT_SYMMETRIC:
547 /* not mentioned in standard */
548 default:
549 return err_bad_md_layout(array);
550 }
551 prl = DDF_RAID5;
552 break;
553 case 6:
554 switch (array->layout) {
555 case ALGORITHM_ROTATING_N_RESTART:
556 rlq = DDF_RAID5_N_RESTART;
557 break;
558 case ALGORITHM_ROTATING_ZERO_RESTART:
559 rlq = DDF_RAID6_0_RESTART;
560 break;
561 case ALGORITHM_ROTATING_N_CONTINUE:
562 rlq = DDF_RAID5_N_CONTINUE;
563 break;
564 default:
565 return err_bad_md_layout(array);
566 }
567 prl = DDF_RAID6;
568 break;
569 case 10:
570 if (array->raid_disks % 2 == 0 && array->layout == 0x102) {
571 rlq = DDF_RAID1_SIMPLE;
572 prim_elmnt_count = __cpu_to_be16(2);
573 sec_elmnt_count = array->raid_disks / 2;
574 } else if (array->raid_disks % 3 == 0
575 && array->layout == 0x103) {
576 rlq = DDF_RAID1_MULTI;
577 prim_elmnt_count = __cpu_to_be16(3);
578 sec_elmnt_count = array->raid_disks / 3;
579 } else
580 return err_bad_md_layout(array);
581 srl = DDF_2SPANNED;
582 prl = DDF_RAID1;
583 break;
584 default:
585 return err_bad_md_layout(array);
586 }
587 conf->prl = prl;
588 conf->prim_elmnt_count = prim_elmnt_count;
589 conf->rlq = rlq;
590 conf->srl = srl;
591 conf->sec_elmnt_count = sec_elmnt_count;
592 return 0;
593}
594
8a2848a7 595static int err_bad_ddf_layout(const struct vd_config *conf)
596{
597 pr_err("DDF RAID %u qualifier %u with %u disks is unsupported\n",
598 conf->prl, conf->rlq, __be16_to_cpu(conf->prim_elmnt_count));
599 return -1;
600}
601
602static int layout_ddf2md(const struct vd_config *conf,
603 mdu_array_info_t *array)
604{
605 int level = LEVEL_UNSUPPORTED;
606 int layout = 0;
607 int raiddisks = __be16_to_cpu(conf->prim_elmnt_count);
608
609 if (conf->sec_elmnt_count > 1) {
610 /* see also check_secondary() */
611 if (conf->prl != DDF_RAID1 ||
612 (conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED)) {
613 pr_err("Unsupported secondary RAID level %u/%u\n",
614 conf->prl, conf->srl);
615 return -1;
616 }
617 if (raiddisks == 2 && conf->rlq == DDF_RAID1_SIMPLE)
618 layout = 0x102;
619 else if (raiddisks == 3 && conf->rlq == DDF_RAID1_MULTI)
620 layout = 0x103;
621 else
622 return err_bad_ddf_layout(conf);
623 raiddisks *= conf->sec_elmnt_count;
624 level = 10;
625 goto good;
626 }
627
628 switch (conf->prl) {
629 case DDF_CONCAT:
630 level = LEVEL_LINEAR;
631 break;
632 case DDF_RAID0:
633 if (conf->rlq != DDF_RAID0_SIMPLE)
634 return err_bad_ddf_layout(conf);
635 level = 0;
636 break;
637 case DDF_RAID1:
638 if (!((conf->rlq == DDF_RAID1_SIMPLE && raiddisks == 2) ||
639 (conf->rlq == DDF_RAID1_MULTI && raiddisks == 3)))
640 return err_bad_ddf_layout(conf);
641 level = 1;
642 break;
643 case DDF_RAID4:
644 if (conf->rlq != DDF_RAID4_N)
645 return err_bad_ddf_layout(conf);
646 level = 4;
647 break;
648 case DDF_RAID5:
649 switch (conf->rlq) {
650 case DDF_RAID5_N_RESTART:
651 layout = ALGORITHM_LEFT_ASYMMETRIC;
652 break;
653 case DDF_RAID5_0_RESTART:
654 layout = ALGORITHM_RIGHT_ASYMMETRIC;
655 break;
656 case DDF_RAID5_N_CONTINUE:
657 layout = ALGORITHM_LEFT_SYMMETRIC;
658 break;
659 default:
660 return err_bad_ddf_layout(conf);
661 }
662 level = 5;
663 break;
664 case DDF_RAID6:
665 switch (conf->rlq) {
666 case DDF_RAID5_N_RESTART:
667 layout = ALGORITHM_ROTATING_N_RESTART;
668 break;
669 case DDF_RAID6_0_RESTART:
670 layout = ALGORITHM_ROTATING_ZERO_RESTART;
671 break;
672 case DDF_RAID5_N_CONTINUE:
673 layout = ALGORITHM_ROTATING_N_CONTINUE;
674 break;
675 default:
676 return err_bad_ddf_layout(conf);
677 }
678 level = 6;
679 break;
680 default:
681 return err_bad_ddf_layout(conf);
682 };
683
684good:
685 array->level = level;
686 array->layout = layout;
687 array->raid_disks = raiddisks;
688 return 0;
689}
690
a322f70c
DW
691static int load_ddf_header(int fd, unsigned long long lba,
692 unsigned long long size,
693 int type,
694 struct ddf_header *hdr, struct ddf_header *anchor)
695{
696 /* read a ddf header (primary or secondary) from fd/lba
697 * and check that it is consistent with anchor
698 * Need to check:
699 * magic, crc, guid, rev, and LBA's header_type, and
700 * everything after header_type must be the same
701 */
702 if (lba >= size-1)
703 return 0;
704
705 if (lseek64(fd, lba<<9, 0) < 0)
706 return 0;
707
708 if (read(fd, hdr, 512) != 512)
709 return 0;
710
711 if (hdr->magic != DDF_HEADER_MAGIC)
712 return 0;
713 if (calc_crc(hdr, 512) != hdr->crc)
714 return 0;
715 if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 ||
716 memcmp(anchor->revision, hdr->revision, 8) != 0 ||
717 anchor->primary_lba != hdr->primary_lba ||
718 anchor->secondary_lba != hdr->secondary_lba ||
719 hdr->type != type ||
720 memcmp(anchor->pad2, hdr->pad2, 512 -
721 offsetof(struct ddf_header, pad2)) != 0)
722 return 0;
723
724 /* Looks good enough to me... */
725 return 1;
726}
727
728static void *load_section(int fd, struct ddf_super *super, void *buf,
729 __u32 offset_be, __u32 len_be, int check)
730{
731 unsigned long long offset = __be32_to_cpu(offset_be);
732 unsigned long long len = __be32_to_cpu(len_be);
733 int dofree = (buf == NULL);
734
735 if (check)
736 if (len != 2 && len != 8 && len != 32
737 && len != 128 && len != 512)
738 return NULL;
739
740 if (len > 1024)
741 return NULL;
742 if (buf) {
743 /* All pre-allocated sections are a single block */
744 if (len != 1)
745 return NULL;
3d2c4fc7
DW
746 } else if (posix_memalign(&buf, 512, len<<9) != 0)
747 buf = NULL;
6416d527 748
a322f70c
DW
749 if (!buf)
750 return NULL;
751
752 if (super->active->type == 1)
753 offset += __be64_to_cpu(super->active->primary_lba);
754 else
755 offset += __be64_to_cpu(super->active->secondary_lba);
756
f21e18ca 757 if ((unsigned long long)lseek64(fd, offset<<9, 0) != (offset<<9)) {
a322f70c
DW
758 if (dofree)
759 free(buf);
760 return NULL;
761 }
f21e18ca 762 if ((unsigned long long)read(fd, buf, len<<9) != (len<<9)) {
a322f70c
DW
763 if (dofree)
764 free(buf);
765 return NULL;
766 }
767 return buf;
768}
769
770static int load_ddf_headers(int fd, struct ddf_super *super, char *devname)
771{
772 unsigned long long dsize;
773
774 get_dev_size(fd, NULL, &dsize);
775
776 if (lseek64(fd, dsize-512, 0) < 0) {
777 if (devname)
e7b84f9d
N
778 pr_err("Cannot seek to anchor block on %s: %s\n",
779 devname, strerror(errno));
a322f70c
DW
780 return 1;
781 }
782 if (read(fd, &super->anchor, 512) != 512) {
783 if (devname)
e7b84f9d
N
784 pr_err("Cannot read anchor block on %s: %s\n",
785 devname, strerror(errno));
a322f70c
DW
786 return 1;
787 }
788 if (super->anchor.magic != DDF_HEADER_MAGIC) {
789 if (devname)
e7b84f9d 790 pr_err("no DDF anchor found on %s\n",
a322f70c
DW
791 devname);
792 return 2;
793 }
794 if (calc_crc(&super->anchor, 512) != super->anchor.crc) {
795 if (devname)
e7b84f9d 796 pr_err("bad CRC on anchor on %s\n",
a322f70c
DW
797 devname);
798 return 2;
799 }
59e36268
NB
800 if (memcmp(super->anchor.revision, DDF_REVISION_0, 8) != 0 &&
801 memcmp(super->anchor.revision, DDF_REVISION_2, 8) != 0) {
a322f70c 802 if (devname)
e7b84f9d 803 pr_err("can only support super revision"
59e36268
NB
804 " %.8s and earlier, not %.8s on %s\n",
805 DDF_REVISION_2, super->anchor.revision,devname);
a322f70c
DW
806 return 2;
807 }
dbeb699a 808 super->active = NULL;
a322f70c
DW
809 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.primary_lba),
810 dsize >> 9, 1,
811 &super->primary, &super->anchor) == 0) {
812 if (devname)
e7b84f9d
N
813 pr_err("Failed to load primary DDF header "
814 "on %s\n", devname);
dbeb699a 815 } else
816 super->active = &super->primary;
a322f70c
DW
817 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.secondary_lba),
818 dsize >> 9, 2,
819 &super->secondary, &super->anchor)) {
820 if ((__be32_to_cpu(super->primary.seq)
821 < __be32_to_cpu(super->secondary.seq) &&
822 !super->secondary.openflag)
823 || (__be32_to_cpu(super->primary.seq)
824 == __be32_to_cpu(super->secondary.seq) &&
825 super->primary.openflag && !super->secondary.openflag)
dbeb699a 826 || super->active == NULL
a322f70c
DW
827 )
828 super->active = &super->secondary;
dbeb699a 829 } else if (devname)
830 pr_err("Failed to load secondary DDF header on %s\n",
831 devname);
832 if (super->active == NULL)
833 return 2;
a322f70c
DW
834 return 0;
835}
836
837static int load_ddf_global(int fd, struct ddf_super *super, char *devname)
838{
839 void *ok;
840 ok = load_section(fd, super, &super->controller,
841 super->active->controller_section_offset,
842 super->active->controller_section_length,
843 0);
844 super->phys = load_section(fd, super, NULL,
845 super->active->phys_section_offset,
846 super->active->phys_section_length,
847 1);
848 super->pdsize = __be32_to_cpu(super->active->phys_section_length) * 512;
849
850 super->virt = load_section(fd, super, NULL,
851 super->active->virt_section_offset,
852 super->active->virt_section_length,
853 1);
854 super->vdsize = __be32_to_cpu(super->active->virt_section_length) * 512;
855 if (!ok ||
856 !super->phys ||
857 !super->virt) {
858 free(super->phys);
859 free(super->virt);
a2349791
NB
860 super->phys = NULL;
861 super->virt = NULL;
a322f70c
DW
862 return 2;
863 }
864 super->conflist = NULL;
865 super->dlist = NULL;
8c3b8c2c
NB
866
867 super->max_part = __be16_to_cpu(super->active->max_partitions);
868 super->mppe = __be16_to_cpu(super->active->max_primary_element_entries);
869 super->conf_rec_len = __be16_to_cpu(super->active->config_record_len);
a322f70c
DW
870 return 0;
871}
872
3c48f7be 873#define DDF_UNUSED_BVD 0xff
874static int alloc_other_bvds(const struct ddf_super *ddf, struct vcl *vcl)
875{
876 unsigned int n_vds = vcl->conf.sec_elmnt_count - 1;
877 unsigned int i, vdsize;
878 void *p;
879 if (n_vds == 0) {
880 vcl->other_bvds = NULL;
881 return 0;
882 }
883 vdsize = ddf->conf_rec_len * 512;
884 if (posix_memalign(&p, 512, n_vds *
885 (vdsize + sizeof(struct vd_config *))) != 0)
886 return -1;
887 vcl->other_bvds = (struct vd_config **) (p + n_vds * vdsize);
888 for (i = 0; i < n_vds; i++) {
889 vcl->other_bvds[i] = p + i * vdsize;
890 memset(vcl->other_bvds[i], 0, vdsize);
891 vcl->other_bvds[i]->sec_elmnt_seq = DDF_UNUSED_BVD;
892 }
893 return 0;
894}
895
3dc821b0 896static void add_other_bvd(struct vcl *vcl, struct vd_config *vd,
897 unsigned int len)
898{
899 int i;
900 for (i = 0; i < vcl->conf.sec_elmnt_count-1; i++)
3c48f7be 901 if (vcl->other_bvds[i]->sec_elmnt_seq == vd->sec_elmnt_seq)
3dc821b0 902 break;
903
904 if (i < vcl->conf.sec_elmnt_count-1) {
905 if (vd->seqnum <= vcl->other_bvds[i]->seqnum)
906 return;
907 } else {
908 for (i = 0; i < vcl->conf.sec_elmnt_count-1; i++)
3c48f7be 909 if (vcl->other_bvds[i]->sec_elmnt_seq == DDF_UNUSED_BVD)
3dc821b0 910 break;
911 if (i == vcl->conf.sec_elmnt_count-1) {
912 pr_err("no space for sec level config %u, count is %u\n",
913 vd->sec_elmnt_seq, vcl->conf.sec_elmnt_count);
914 return;
915 }
3dc821b0 916 }
917 memcpy(vcl->other_bvds[i], vd, len);
918}
919
a322f70c
DW
920static int load_ddf_local(int fd, struct ddf_super *super,
921 char *devname, int keep)
922{
923 struct dl *dl;
924 struct stat stb;
925 char *conf;
f21e18ca
N
926 unsigned int i;
927 unsigned int confsec;
b2280677 928 int vnum;
f21e18ca 929 unsigned int max_virt_disks = __be16_to_cpu(super->active->max_vd_entries);
d2ca6449 930 unsigned long long dsize;
a322f70c
DW
931
932 /* First the local disk info */
3d2c4fc7 933 if (posix_memalign((void**)&dl, 512,
6416d527 934 sizeof(*dl) +
3d2c4fc7 935 (super->max_part) * sizeof(dl->vlist[0])) != 0) {
e7b84f9d 936 pr_err("%s could not allocate disk info buffer\n",
3d2c4fc7
DW
937 __func__);
938 return 1;
939 }
a322f70c
DW
940
941 load_section(fd, super, &dl->disk,
942 super->active->data_section_offset,
943 super->active->data_section_length,
944 0);
503975b9 945 dl->devname = devname ? xstrdup(devname) : NULL;
598f0d58 946
a322f70c
DW
947 fstat(fd, &stb);
948 dl->major = major(stb.st_rdev);
949 dl->minor = minor(stb.st_rdev);
950 dl->next = super->dlist;
951 dl->fd = keep ? fd : -1;
d2ca6449
NB
952
953 dl->size = 0;
954 if (get_dev_size(fd, devname, &dsize))
955 dl->size = dsize >> 9;
097bcf00 956 /* If the disks have different sizes, the LBAs will differ
957 * between phys disks.
958 * At this point here, the values in super->active must be valid
959 * for this phys disk. */
960 dl->primary_lba = super->active->primary_lba;
961 dl->secondary_lba = super->active->secondary_lba;
962 dl->workspace_lba = super->active->workspace_lba;
b2280677 963 dl->spare = NULL;
f21e18ca 964 for (i = 0 ; i < super->max_part ; i++)
a322f70c
DW
965 dl->vlist[i] = NULL;
966 super->dlist = dl;
59e36268 967 dl->pdnum = -1;
f21e18ca 968 for (i = 0; i < __be16_to_cpu(super->active->max_pd_entries); i++)
5575e7d9
NB
969 if (memcmp(super->phys->entries[i].guid,
970 dl->disk.guid, DDF_GUID_LEN) == 0)
971 dl->pdnum = i;
972
a322f70c
DW
973 /* Now the config list. */
974 /* 'conf' is an array of config entries, some of which are
975 * probably invalid. Those which are good need to be copied into
976 * the conflist
977 */
a322f70c
DW
978
979 conf = load_section(fd, super, NULL,
980 super->active->config_section_offset,
981 super->active->config_section_length,
982 0);
983
b2280677 984 vnum = 0;
e223334f
N
985 for (confsec = 0;
986 confsec < __be32_to_cpu(super->active->config_section_length);
987 confsec += super->conf_rec_len) {
a322f70c 988 struct vd_config *vd =
e223334f 989 (struct vd_config *)((char*)conf + confsec*512);
a322f70c
DW
990 struct vcl *vcl;
991
b2280677
NB
992 if (vd->magic == DDF_SPARE_ASSIGN_MAGIC) {
993 if (dl->spare)
994 continue;
3d2c4fc7
DW
995 if (posix_memalign((void**)&dl->spare, 512,
996 super->conf_rec_len*512) != 0) {
e7b84f9d
N
997 pr_err("%s could not allocate spare info buf\n",
998 __func__);
3d2c4fc7
DW
999 return 1;
1000 }
613b0d17 1001
b2280677
NB
1002 memcpy(dl->spare, vd, super->conf_rec_len*512);
1003 continue;
1004 }
a322f70c
DW
1005 if (vd->magic != DDF_VD_CONF_MAGIC)
1006 continue;
1007 for (vcl = super->conflist; vcl; vcl = vcl->next) {
1008 if (memcmp(vcl->conf.guid,
1009 vd->guid, DDF_GUID_LEN) == 0)
1010 break;
1011 }
1012
1013 if (vcl) {
b2280677 1014 dl->vlist[vnum++] = vcl;
3dc821b0 1015 if (vcl->other_bvds != NULL &&
1016 vcl->conf.sec_elmnt_seq != vd->sec_elmnt_seq) {
1017 add_other_bvd(vcl, vd, super->conf_rec_len*512);
1018 continue;
1019 }
a322f70c
DW
1020 if (__be32_to_cpu(vd->seqnum) <=
1021 __be32_to_cpu(vcl->conf.seqnum))
1022 continue;
59e36268 1023 } else {
3d2c4fc7 1024 if (posix_memalign((void**)&vcl, 512,
6416d527 1025 (super->conf_rec_len*512 +
3d2c4fc7 1026 offsetof(struct vcl, conf))) != 0) {
e7b84f9d
N
1027 pr_err("%s could not allocate vcl buf\n",
1028 __func__);
3d2c4fc7
DW
1029 return 1;
1030 }
a322f70c 1031 vcl->next = super->conflist;
59e36268 1032 vcl->block_sizes = NULL; /* FIXME not for CONCAT */
3c48f7be 1033 vcl->conf.sec_elmnt_count = vd->sec_elmnt_count;
1034 if (alloc_other_bvds(super, vcl) != 0) {
1035 pr_err("%s could not allocate other bvds\n",
1036 __func__);
1037 free(vcl);
1038 return 1;
1039 };
a322f70c 1040 super->conflist = vcl;
b2280677 1041 dl->vlist[vnum++] = vcl;
a322f70c 1042 }
8c3b8c2c 1043 memcpy(&vcl->conf, vd, super->conf_rec_len*512);
59e36268
NB
1044 for (i=0; i < max_virt_disks ; i++)
1045 if (memcmp(super->virt->entries[i].guid,
1046 vcl->conf.guid, DDF_GUID_LEN)==0)
1047 break;
1048 if (i < max_virt_disks)
1049 vcl->vcnum = i;
a322f70c
DW
1050 }
1051 free(conf);
1052
1053 return 0;
1054}
1055
1056#ifndef MDASSEMBLE
1057static int load_super_ddf_all(struct supertype *st, int fd,
e1902a7b 1058 void **sbp, char *devname);
a322f70c 1059#endif
37424f13
DW
1060
1061static void free_super_ddf(struct supertype *st);
1062
a322f70c
DW
1063static int load_super_ddf(struct supertype *st, int fd,
1064 char *devname)
1065{
1066 unsigned long long dsize;
1067 struct ddf_super *super;
1068 int rv;
1069
a322f70c
DW
1070 if (get_dev_size(fd, devname, &dsize) == 0)
1071 return 1;
1072
b31df436 1073 if (!st->ignore_hw_compat && test_partition(fd))
691c6ee1
N
1074 /* DDF is not allowed on partitions */
1075 return 1;
1076
a322f70c
DW
1077 /* 32M is a lower bound */
1078 if (dsize <= 32*1024*1024) {
97320d7c 1079 if (devname)
e7b84f9d
N
1080 pr_err("%s is too small for ddf: "
1081 "size is %llu sectors.\n",
1082 devname, dsize>>9);
97320d7c 1083 return 1;
a322f70c
DW
1084 }
1085 if (dsize & 511) {
97320d7c 1086 if (devname)
e7b84f9d
N
1087 pr_err("%s is an odd size for ddf: "
1088 "size is %llu bytes.\n",
1089 devname, dsize);
97320d7c 1090 return 1;
a322f70c
DW
1091 }
1092
37424f13
DW
1093 free_super_ddf(st);
1094
6416d527 1095 if (posix_memalign((void**)&super, 512, sizeof(*super))!= 0) {
e7b84f9d 1096 pr_err("malloc of %zu failed.\n",
a322f70c
DW
1097 sizeof(*super));
1098 return 1;
1099 }
a2349791 1100 memset(super, 0, sizeof(*super));
a322f70c
DW
1101
1102 rv = load_ddf_headers(fd, super, devname);
1103 if (rv) {
1104 free(super);
1105 return rv;
1106 }
1107
1108 /* Have valid headers and have chosen the best. Let's read in the rest*/
1109
1110 rv = load_ddf_global(fd, super, devname);
1111
1112 if (rv) {
1113 if (devname)
e7b84f9d
N
1114 pr_err("Failed to load all information "
1115 "sections on %s\n", devname);
a322f70c
DW
1116 free(super);
1117 return rv;
1118 }
1119
3d2c4fc7
DW
1120 rv = load_ddf_local(fd, super, devname, 0);
1121
1122 if (rv) {
1123 if (devname)
e7b84f9d
N
1124 pr_err("Failed to load all information "
1125 "sections on %s\n", devname);
3d2c4fc7
DW
1126 free(super);
1127 return rv;
1128 }
a322f70c
DW
1129
1130 /* Should possibly check the sections .... */
1131
1132 st->sb = super;
1133 if (st->ss == NULL) {
1134 st->ss = &super_ddf;
1135 st->minor_version = 0;
1136 st->max_devs = 512;
1137 }
1138 return 0;
1139
1140}
1141
1142static void free_super_ddf(struct supertype *st)
1143{
1144 struct ddf_super *ddf = st->sb;
1145 if (ddf == NULL)
1146 return;
1147 free(ddf->phys);
1148 free(ddf->virt);
1149 while (ddf->conflist) {
1150 struct vcl *v = ddf->conflist;
1151 ddf->conflist = v->next;
59e36268
NB
1152 if (v->block_sizes)
1153 free(v->block_sizes);
3c48f7be 1154 if (v->other_bvds)
1155 /*
1156 v->other_bvds[0] points to beginning of buffer,
1157 see alloc_other_bvds()
1158 */
1159 free(v->other_bvds[0]);
a322f70c
DW
1160 free(v);
1161 }
1162 while (ddf->dlist) {
1163 struct dl *d = ddf->dlist;
1164 ddf->dlist = d->next;
1165 if (d->fd >= 0)
1166 close(d->fd);
b2280677
NB
1167 if (d->spare)
1168 free(d->spare);
a322f70c
DW
1169 free(d);
1170 }
8a38cb04
N
1171 while (ddf->add_list) {
1172 struct dl *d = ddf->add_list;
1173 ddf->add_list = d->next;
1174 if (d->fd >= 0)
1175 close(d->fd);
1176 if (d->spare)
1177 free(d->spare);
1178 free(d);
1179 }
a322f70c
DW
1180 free(ddf);
1181 st->sb = NULL;
1182}
1183
1184static struct supertype *match_metadata_desc_ddf(char *arg)
1185{
1186 /* 'ddf' only support containers */
1187 struct supertype *st;
1188 if (strcmp(arg, "ddf") != 0 &&
1189 strcmp(arg, "default") != 0
1190 )
1191 return NULL;
1192
503975b9 1193 st = xcalloc(1, sizeof(*st));
a322f70c
DW
1194 st->ss = &super_ddf;
1195 st->max_devs = 512;
1196 st->minor_version = 0;
1197 st->sb = NULL;
1198 return st;
1199}
1200
a322f70c
DW
1201#ifndef MDASSEMBLE
1202
1203static mapping_t ddf_state[] = {
1204 { "Optimal", 0},
1205 { "Degraded", 1},
1206 { "Deleted", 2},
1207 { "Missing", 3},
1208 { "Failed", 4},
1209 { "Partially Optimal", 5},
1210 { "-reserved-", 6},
1211 { "-reserved-", 7},
1212 { NULL, 0}
1213};
1214
1215static mapping_t ddf_init_state[] = {
1216 { "Not Initialised", 0},
1217 { "QuickInit in Progress", 1},
1218 { "Fully Initialised", 2},
1219 { "*UNKNOWN*", 3},
1220 { NULL, 0}
1221};
1222static mapping_t ddf_access[] = {
1223 { "Read/Write", 0},
1224 { "Reserved", 1},
1225 { "Read Only", 2},
1226 { "Blocked (no access)", 3},
1227 { NULL ,0}
1228};
1229
1230static mapping_t ddf_level[] = {
1231 { "RAID0", DDF_RAID0},
1232 { "RAID1", DDF_RAID1},
1233 { "RAID3", DDF_RAID3},
1234 { "RAID4", DDF_RAID4},
1235 { "RAID5", DDF_RAID5},
1236 { "RAID1E",DDF_RAID1E},
1237 { "JBOD", DDF_JBOD},
1238 { "CONCAT",DDF_CONCAT},
1239 { "RAID5E",DDF_RAID5E},
1240 { "RAID5EE",DDF_RAID5EE},
1241 { "RAID6", DDF_RAID6},
1242 { NULL, 0}
1243};
1244static mapping_t ddf_sec_level[] = {
1245 { "Striped", DDF_2STRIPED},
1246 { "Mirrored", DDF_2MIRRORED},
1247 { "Concat", DDF_2CONCAT},
1248 { "Spanned", DDF_2SPANNED},
1249 { NULL, 0}
1250};
1251#endif
1252
fb9d0acb 1253static int all_ff(const char *guid)
42dc2744
N
1254{
1255 int i;
1256 for (i = 0; i < DDF_GUID_LEN; i++)
1257 if (guid[i] != (char)0xff)
1258 return 0;
1259 return 1;
1260}
1261
a322f70c
DW
1262#ifndef MDASSEMBLE
1263static void print_guid(char *guid, int tstamp)
1264{
1265 /* A GUIDs are part (or all) ASCII and part binary.
1266 * They tend to be space padded.
59e36268
NB
1267 * We print the GUID in HEX, then in parentheses add
1268 * any initial ASCII sequence, and a possible
1269 * time stamp from bytes 16-19
a322f70c
DW
1270 */
1271 int l = DDF_GUID_LEN;
1272 int i;
59e36268
NB
1273
1274 for (i=0 ; i<DDF_GUID_LEN ; i++) {
1275 if ((i&3)==0 && i != 0) printf(":");
1276 printf("%02X", guid[i]&255);
1277 }
1278
cfccea8c 1279 printf("\n (");
a322f70c
DW
1280 while (l && guid[l-1] == ' ')
1281 l--;
1282 for (i=0 ; i<l ; i++) {
1283 if (guid[i] >= 0x20 && guid[i] < 0x7f)
1284 fputc(guid[i], stdout);
1285 else
59e36268 1286 break;
a322f70c
DW
1287 }
1288 if (tstamp) {
1289 time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE;
1290 char tbuf[100];
1291 struct tm *tm;
1292 tm = localtime(&then);
59e36268 1293 strftime(tbuf, 100, " %D %T",tm);
a322f70c
DW
1294 fputs(tbuf, stdout);
1295 }
59e36268 1296 printf(")");
a322f70c
DW
1297}
1298
be9b9ef4 1299static const char *guid_str(const char *guid)
1300{
1301 static char buf[DDF_GUID_LEN*2+1];
1302 int i;
1303 char *p = buf;
1304 for (i = 0; i < DDF_GUID_LEN; i++)
1305 p += sprintf(p, "%02x", (unsigned char)guid[i]);
1306 *p = '\0';
1307 return (const char *) buf;
1308}
1309
a322f70c
DW
1310static void examine_vd(int n, struct ddf_super *sb, char *guid)
1311{
8c3b8c2c 1312 int crl = sb->conf_rec_len;
a322f70c
DW
1313 struct vcl *vcl;
1314
1315 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
f21e18ca 1316 unsigned int i;
a322f70c
DW
1317 struct vd_config *vc = &vcl->conf;
1318
1319 if (calc_crc(vc, crl*512) != vc->crc)
1320 continue;
1321 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
1322 continue;
1323
1324 /* Ok, we know about this VD, let's give more details */
b06e3095 1325 printf(" Raid Devices[%d] : %d (", n,
a322f70c 1326 __be16_to_cpu(vc->prim_elmnt_count));
f21e18ca 1327 for (i = 0; i < __be16_to_cpu(vc->prim_elmnt_count); i++) {
b06e3095
N
1328 int j;
1329 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1330 for (j=0; j<cnt; j++)
1331 if (vc->phys_refnum[i] == sb->phys->entries[j].refnum)
1332 break;
1333 if (i) printf(" ");
1334 if (j < cnt)
1335 printf("%d", j);
1336 else
1337 printf("--");
1338 }
1339 printf(")\n");
1340 if (vc->chunk_shift != 255)
613b0d17
N
1341 printf(" Chunk Size[%d] : %d sectors\n", n,
1342 1 << vc->chunk_shift);
a322f70c
DW
1343 printf(" Raid Level[%d] : %s\n", n,
1344 map_num(ddf_level, vc->prl)?:"-unknown-");
1345 if (vc->sec_elmnt_count != 1) {
1346 printf(" Secondary Position[%d] : %d of %d\n", n,
1347 vc->sec_elmnt_seq, vc->sec_elmnt_count);
1348 printf(" Secondary Level[%d] : %s\n", n,
1349 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
1350 }
1351 printf(" Device Size[%d] : %llu\n", n,
c9b6907b 1352 (unsigned long long)__be64_to_cpu(vc->blocks)/2);
a322f70c 1353 printf(" Array Size[%d] : %llu\n", n,
c9b6907b 1354 (unsigned long long)__be64_to_cpu(vc->array_blocks)/2);
a322f70c
DW
1355 }
1356}
1357
1358static void examine_vds(struct ddf_super *sb)
1359{
1360 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
fb9d0acb 1361 unsigned int i;
a322f70c
DW
1362 printf(" Virtual Disks : %d\n", cnt);
1363
fb9d0acb 1364 for (i = 0; i < __be16_to_cpu(sb->virt->max_vdes); i++) {
a322f70c 1365 struct virtual_entry *ve = &sb->virt->entries[i];
fb9d0acb 1366 if (all_ff(ve->guid))
1367 continue;
b06e3095 1368 printf("\n");
a322f70c
DW
1369 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
1370 printf("\n");
1371 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
1372 printf(" state[%d] : %s, %s%s\n", i,
1373 map_num(ddf_state, ve->state & 7),
1374 (ve->state & 8) ? "Morphing, ": "",
1375 (ve->state & 16)? "Not Consistent" : "Consistent");
1376 printf(" init state[%d] : %s\n", i,
1377 map_num(ddf_init_state, ve->init_state&3));
1378 printf(" access[%d] : %s\n", i,
1379 map_num(ddf_access, (ve->init_state>>6) & 3));
1380 printf(" Name[%d] : %.16s\n", i, ve->name);
1381 examine_vd(i, sb, ve->guid);
1382 }
1383 if (cnt) printf("\n");
1384}
1385
1386static void examine_pds(struct ddf_super *sb)
1387{
1388 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1389 int i;
1390 struct dl *dl;
1391 printf(" Physical Disks : %d\n", cnt);
962371a5 1392 printf(" Number RefNo Size Device Type/State\n");
a322f70c
DW
1393
1394 for (i=0 ; i<cnt ; i++) {
1395 struct phys_disk_entry *pd = &sb->phys->entries[i];
1396 int type = __be16_to_cpu(pd->type);
1397 int state = __be16_to_cpu(pd->state);
1398
b06e3095
N
1399 //printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1400 //printf("\n");
1401 printf(" %3d %08x ", i,
a322f70c 1402 __be32_to_cpu(pd->refnum));
613b0d17 1403 printf("%8lluK ",
c9b6907b 1404 (unsigned long long)__be64_to_cpu(pd->config_size)>>1);
b06e3095
N
1405 for (dl = sb->dlist; dl ; dl = dl->next) {
1406 if (dl->disk.refnum == pd->refnum) {
1407 char *dv = map_dev(dl->major, dl->minor, 0);
1408 if (dv) {
962371a5 1409 printf("%-15s", dv);
b06e3095
N
1410 break;
1411 }
1412 }
1413 }
1414 if (!dl)
962371a5 1415 printf("%15s","");
b06e3095 1416 printf(" %s%s%s%s%s",
a322f70c 1417 (type&2) ? "active":"",
b06e3095 1418 (type&4) ? "Global-Spare":"",
a322f70c
DW
1419 (type&8) ? "spare" : "",
1420 (type&16)? ", foreign" : "",
1421 (type&32)? "pass-through" : "");
18cb4496
N
1422 if (state & DDF_Failed)
1423 /* This over-rides these three */
1424 state &= ~(DDF_Online|DDF_Rebuilding|DDF_Transition);
b06e3095 1425 printf("/%s%s%s%s%s%s%s",
a322f70c
DW
1426 (state&1)? "Online": "Offline",
1427 (state&2)? ", Failed": "",
1428 (state&4)? ", Rebuilding": "",
1429 (state&8)? ", in-transition": "",
b06e3095
N
1430 (state&16)? ", SMART-errors": "",
1431 (state&32)? ", Unrecovered-Read-Errors": "",
a322f70c 1432 (state&64)? ", Missing" : "");
a322f70c
DW
1433 printf("\n");
1434 }
1435}
1436
1437static void examine_super_ddf(struct supertype *st, char *homehost)
1438{
1439 struct ddf_super *sb = st->sb;
1440
1441 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1442 printf(" Version : %.8s\n", sb->anchor.revision);
598f0d58
NB
1443 printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
1444 printf("\n");
1445 printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
a322f70c
DW
1446 printf("\n");
1447 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1448 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1449 ?"yes" : "no");
1450 examine_vds(sb);
1451 examine_pds(sb);
1452}
1453
a5d85af7 1454static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map);
ff54de6e 1455
bedbf68a 1456static void uuid_from_ddf_guid(const char *guid, int uuid[4]);
42dc2744 1457static void uuid_from_super_ddf(struct supertype *st, int uuid[4]);
ff54de6e 1458
bedbf68a 1459static unsigned int get_vd_num_of_subarray(struct supertype *st)
1460{
1461 /*
1462 * Figure out the VD number for this supertype.
1463 * Returns DDF_CONTAINER for the container itself,
1464 * and DDF_NOTFOUND on error.
1465 */
1466 struct ddf_super *ddf = st->sb;
1467 struct mdinfo *sra;
1468 char *sub, *end;
1469 unsigned int vcnum;
1470
1471 if (*st->container_devnm == '\0')
1472 return DDF_CONTAINER;
1473
1474 sra = sysfs_read(-1, st->devnm, GET_VERSION);
1475 if (!sra || sra->array.major_version != -1 ||
1476 sra->array.minor_version != -2 ||
1477 !is_subarray(sra->text_version))
1478 return DDF_NOTFOUND;
1479
1480 sub = strchr(sra->text_version + 1, '/');
1481 if (sub != NULL)
1482 vcnum = strtoul(sub + 1, &end, 10);
1483 if (sub == NULL || *sub == '\0' || *end != '\0' ||
1484 vcnum >= __be16_to_cpu(ddf->active->max_vd_entries))
1485 return DDF_NOTFOUND;
1486
1487 return vcnum;
1488}
1489
061f2c6a 1490static void brief_examine_super_ddf(struct supertype *st, int verbose)
4737ae25
N
1491{
1492 /* We just write a generic DDF ARRAY entry
1493 */
1494 struct mdinfo info;
1495 char nbuf[64];
a5d85af7 1496 getinfo_super_ddf(st, &info, NULL);
4737ae25
N
1497 fname_from_uuid(st, &info, nbuf, ':');
1498
1499 printf("ARRAY metadata=ddf UUID=%s\n", nbuf + 5);
1500}
1501
1502static void brief_examine_subarrays_ddf(struct supertype *st, int verbose)
a322f70c
DW
1503{
1504 /* We just write a generic DDF ARRAY entry
a322f70c 1505 */
42dc2744 1506 struct ddf_super *ddf = st->sb;
ff54de6e 1507 struct mdinfo info;
f21e18ca 1508 unsigned int i;
ff54de6e 1509 char nbuf[64];
a5d85af7 1510 getinfo_super_ddf(st, &info, NULL);
ff54de6e 1511 fname_from_uuid(st, &info, nbuf, ':');
42dc2744 1512
f21e18ca 1513 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
42dc2744
N
1514 struct virtual_entry *ve = &ddf->virt->entries[i];
1515 struct vcl vcl;
1516 char nbuf1[64];
1517 if (all_ff(ve->guid))
1518 continue;
1519 memcpy(vcl.conf.guid, ve->guid, DDF_GUID_LEN);
1520 ddf->currentconf =&vcl;
1521 uuid_from_super_ddf(st, info.uuid);
1522 fname_from_uuid(st, &info, nbuf1, ':');
1523 printf("ARRAY container=%s member=%d UUID=%s\n",
1524 nbuf+5, i, nbuf1+5);
1525 }
a322f70c
DW
1526}
1527
bceedeec
N
1528static void export_examine_super_ddf(struct supertype *st)
1529{
1530 struct mdinfo info;
1531 char nbuf[64];
a5d85af7 1532 getinfo_super_ddf(st, &info, NULL);
bceedeec
N
1533 fname_from_uuid(st, &info, nbuf, ':');
1534 printf("MD_METADATA=ddf\n");
1535 printf("MD_LEVEL=container\n");
1536 printf("MD_UUID=%s\n", nbuf+5);
1537}
bceedeec 1538
74db60b0
N
1539static int copy_metadata_ddf(struct supertype *st, int from, int to)
1540{
1541 void *buf;
1542 unsigned long long dsize, offset;
1543 int bytes;
1544 struct ddf_header *ddf;
1545 int written = 0;
1546
1547 /* The meta consists of an anchor, a primary, and a secondary.
1548 * This all lives at the end of the device.
1549 * So it is easiest to find the earliest of primary and
1550 * secondary, and copy everything from there.
1551 *
1552 * Anchor is 512 from end It contains primary_lba and secondary_lba
1553 * we choose one of those
1554 */
1555
1556 if (posix_memalign(&buf, 4096, 4096) != 0)
1557 return 1;
1558
1559 if (!get_dev_size(from, NULL, &dsize))
1560 goto err;
1561
1562 if (lseek64(from, dsize-512, 0) < 0)
1563 goto err;
1564 if (read(from, buf, 512) != 512)
1565 goto err;
1566 ddf = buf;
1567 if (ddf->magic != DDF_HEADER_MAGIC ||
1568 calc_crc(ddf, 512) != ddf->crc ||
1569 (memcmp(ddf->revision, DDF_REVISION_0, 8) != 0 &&
1570 memcmp(ddf->revision, DDF_REVISION_2, 8) != 0))
1571 goto err;
1572
1573 offset = dsize - 512;
1574 if ((__be64_to_cpu(ddf->primary_lba) << 9) < offset)
1575 offset = __be64_to_cpu(ddf->primary_lba) << 9;
1576 if ((__be64_to_cpu(ddf->secondary_lba) << 9) < offset)
1577 offset = __be64_to_cpu(ddf->secondary_lba) << 9;
1578
1579 bytes = dsize - offset;
1580
1581 if (lseek64(from, offset, 0) < 0 ||
1582 lseek64(to, offset, 0) < 0)
1583 goto err;
1584 while (written < bytes) {
1585 int n = bytes - written;
1586 if (n > 4096)
1587 n = 4096;
1588 if (read(from, buf, n) != n)
1589 goto err;
1590 if (write(to, buf, n) != n)
1591 goto err;
1592 written += n;
1593 }
1594 free(buf);
1595 return 0;
1596err:
1597 free(buf);
1598 return 1;
1599}
1600
a322f70c
DW
1601static void detail_super_ddf(struct supertype *st, char *homehost)
1602{
1603 /* FIXME later
1604 * Could print DDF GUID
1605 * Need to find which array
1606 * If whole, briefly list all arrays
1607 * If one, give name
1608 */
1609}
1610
1611static void brief_detail_super_ddf(struct supertype *st)
1612{
ff54de6e
N
1613 struct mdinfo info;
1614 char nbuf[64];
bedbf68a 1615 struct ddf_super *ddf = st->sb;
1616 unsigned int vcnum = get_vd_num_of_subarray(st);
1617 if (vcnum == DDF_CONTAINER)
1618 uuid_from_super_ddf(st, info.uuid);
1619 else if (vcnum == DDF_NOTFOUND)
1620 return;
1621 else
1622 uuid_from_ddf_guid(ddf->virt->entries[vcnum].guid, info.uuid);
ff54de6e
N
1623 fname_from_uuid(st, &info, nbuf,':');
1624 printf(" UUID=%s", nbuf + 5);
a322f70c 1625}
a322f70c
DW
1626#endif
1627
1628static int match_home_ddf(struct supertype *st, char *homehost)
1629{
1630 /* It matches 'this' host if the controller is a
1631 * Linux-MD controller with vendor_data matching
1632 * the hostname
1633 */
1634 struct ddf_super *ddf = st->sb;
f21e18ca 1635 unsigned int len;
d1d3482b
N
1636
1637 if (!homehost)
1638 return 0;
1639 len = strlen(homehost);
a322f70c
DW
1640
1641 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1642 len < sizeof(ddf->controller.vendor_data) &&
1643 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1644 ddf->controller.vendor_data[len] == 0);
1645}
1646
0e600426 1647#ifndef MDASSEMBLE
baba3f4e 1648static int find_index_in_bvd(const struct ddf_super *ddf,
1649 const struct vd_config *conf, unsigned int n,
1650 unsigned int *n_bvd)
1651{
1652 /*
1653 * Find the index of the n-th valid physical disk in this BVD
1654 */
1655 unsigned int i, j;
1656 for (i = 0, j = 0; i < ddf->mppe &&
1657 j < __be16_to_cpu(conf->prim_elmnt_count); i++) {
1658 if (conf->phys_refnum[i] != 0xffffffff) {
1659 if (n == j) {
1660 *n_bvd = i;
1661 return 1;
1662 }
1663 j++;
1664 }
1665 }
1666 dprintf("%s: couldn't find BVD member %u (total %u)\n",
1667 __func__, n, __be16_to_cpu(conf->prim_elmnt_count));
1668 return 0;
1669}
1670
1671static struct vd_config *find_vdcr(struct ddf_super *ddf, unsigned int inst,
1672 unsigned int n,
1673 unsigned int *n_bvd, struct vcl **vcl)
a322f70c 1674{
7a7cc504 1675 struct vcl *v;
59e36268 1676
baba3f4e 1677 for (v = ddf->conflist; v; v = v->next) {
1678 unsigned int nsec, ibvd;
1679 struct vd_config *conf;
1680 if (inst != v->vcnum)
1681 continue;
1682 conf = &v->conf;
1683 if (conf->sec_elmnt_count == 1) {
1684 if (find_index_in_bvd(ddf, conf, n, n_bvd)) {
1685 *vcl = v;
1686 return conf;
1687 } else
1688 goto bad;
1689 }
1690 if (v->other_bvds == NULL) {
1691 pr_err("%s: BUG: other_bvds is NULL, nsec=%u\n",
1692 __func__, conf->sec_elmnt_count);
1693 goto bad;
1694 }
1695 nsec = n / __be16_to_cpu(conf->prim_elmnt_count);
1696 if (conf->sec_elmnt_seq != nsec) {
1697 for (ibvd = 1; ibvd < conf->sec_elmnt_count; ibvd++) {
baba3f4e 1698 if (v->other_bvds[ibvd-1]->sec_elmnt_seq
1699 == nsec)
1700 break;
1701 }
1702 if (ibvd == conf->sec_elmnt_count)
1703 goto bad;
1704 conf = v->other_bvds[ibvd-1];
1705 }
1706 if (!find_index_in_bvd(ddf, conf,
1707 n - nsec*conf->sec_elmnt_count, n_bvd))
1708 goto bad;
1709 dprintf("%s: found disk %u as member %u in bvd %d of array %u\n"
1710 , __func__, n, *n_bvd, ibvd-1, inst);
1711 *vcl = v;
1712 return conf;
1713 }
1714bad:
1715 pr_err("%s: Could't find disk %d in array %u\n", __func__, n, inst);
7a7cc504
NB
1716 return NULL;
1717}
0e600426 1718#endif
7a7cc504 1719
5ec636b7 1720static int find_phys(const struct ddf_super *ddf, __u32 phys_refnum)
7a7cc504
NB
1721{
1722 /* Find the entry in phys_disk which has the given refnum
1723 * and return it's index
1724 */
f21e18ca
N
1725 unsigned int i;
1726 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes); i++)
7a7cc504
NB
1727 if (ddf->phys->entries[i].refnum == phys_refnum)
1728 return i;
1729 return -1;
a322f70c
DW
1730}
1731
bedbf68a 1732static void uuid_from_ddf_guid(const char *guid, int uuid[4])
1733{
1734 char buf[20];
1735 struct sha1_ctx ctx;
1736 sha1_init_ctx(&ctx);
1737 sha1_process_bytes(guid, DDF_GUID_LEN, &ctx);
1738 sha1_finish_ctx(&ctx, buf);
1739 memcpy(uuid, buf, 4*4);
1740}
1741
a322f70c
DW
1742static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1743{
1744 /* The uuid returned here is used for:
1745 * uuid to put into bitmap file (Create, Grow)
1746 * uuid for backup header when saving critical section (Grow)
1747 * comparing uuids when re-adding a device into an array
51006d85
N
1748 * In these cases the uuid required is that of the data-array,
1749 * not the device-set.
1750 * uuid to recognise same set when adding a missing device back
1751 * to an array. This is a uuid for the device-set.
613b0d17 1752 *
a322f70c
DW
1753 * For each of these we can make do with a truncated
1754 * or hashed uuid rather than the original, as long as
1755 * everyone agrees.
a322f70c
DW
1756 * In the case of SVD we assume the BVD is of interest,
1757 * though that might be the case if a bitmap were made for
1758 * a mirrored SVD - worry about that later.
1759 * So we need to find the VD configuration record for the
1760 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1761 * The first 16 bytes of the sha1 of these is used.
1762 */
1763 struct ddf_super *ddf = st->sb;
d2ca6449 1764 struct vcl *vcl = ddf->currentconf;
c5afc314 1765 char *guid;
a322f70c 1766
c5afc314
N
1767 if (vcl)
1768 guid = vcl->conf.guid;
1769 else
1770 guid = ddf->anchor.guid;
bedbf68a 1771 uuid_from_ddf_guid(guid, uuid);
a322f70c
DW
1772}
1773
a5d85af7 1774static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map);
78e44928 1775
a5d85af7 1776static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map)
a322f70c
DW
1777{
1778 struct ddf_super *ddf = st->sb;
a5d85af7 1779 int map_disks = info->array.raid_disks;
90fa1a29 1780 __u32 *cptr;
a322f70c 1781
78e44928 1782 if (ddf->currentconf) {
a5d85af7 1783 getinfo_super_ddf_bvd(st, info, map);
78e44928
NB
1784 return;
1785 }
95eeceeb 1786 memset(info, 0, sizeof(*info));
78e44928 1787
a322f70c
DW
1788 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1789 info->array.level = LEVEL_CONTAINER;
1790 info->array.layout = 0;
1791 info->array.md_minor = -1;
90fa1a29
JS
1792 cptr = (__u32 *)(ddf->anchor.guid + 16);
1793 info->array.ctime = DECADE + __be32_to_cpu(*cptr);
1794
a322f70c
DW
1795 info->array.utime = 0;
1796 info->array.chunk_size = 0;
510242aa 1797 info->container_enough = 1;
a322f70c 1798
a322f70c
DW
1799 info->disk.major = 0;
1800 info->disk.minor = 0;
cba0191b
NB
1801 if (ddf->dlist) {
1802 info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
59e36268 1803 info->disk.raid_disk = find_phys(ddf, ddf->dlist->disk.refnum);
d2ca6449
NB
1804
1805 info->data_offset = __be64_to_cpu(ddf->phys->
613b0d17
N
1806 entries[info->disk.raid_disk].
1807 config_size);
d2ca6449 1808 info->component_size = ddf->dlist->size - info->data_offset;
cba0191b
NB
1809 } else {
1810 info->disk.number = -1;
661dce36 1811 info->disk.raid_disk = -1;
cba0191b
NB
1812// info->disk.raid_disk = find refnum in the table and use index;
1813 }
f22385f9 1814 info->disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
a19c88b8 1815
921d9e16 1816 info->recovery_start = MaxSector;
a19c88b8 1817 info->reshape_active = 0;
6e75048b 1818 info->recovery_blocked = 0;
c5afc314 1819 info->name[0] = 0;
a322f70c 1820
f35f2525
N
1821 info->array.major_version = -1;
1822 info->array.minor_version = -2;
159c3a1a 1823 strcpy(info->text_version, "ddf");
a67dd8cc 1824 info->safe_mode_delay = 0;
159c3a1a 1825
c5afc314 1826 uuid_from_super_ddf(st, info->uuid);
a322f70c 1827
a5d85af7
N
1828 if (map) {
1829 int i;
1830 for (i = 0 ; i < map_disks; i++) {
1831 if (i < info->array.raid_disks &&
1832 (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) &&
1833 !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed))
1834 map[i] = 1;
1835 else
1836 map[i] = 0;
1837 }
1838 }
a322f70c
DW
1839}
1840
a5d85af7 1841static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map)
a322f70c
DW
1842{
1843 struct ddf_super *ddf = st->sb;
d2ca6449
NB
1844 struct vcl *vc = ddf->currentconf;
1845 int cd = ddf->currentdev;
ddf94a43 1846 int n_prim;
db42fa9b 1847 int j;
8592f29d 1848 struct dl *dl;
a5d85af7 1849 int map_disks = info->array.raid_disks;
90fa1a29 1850 __u32 *cptr;
ddf94a43 1851 struct vd_config *conf;
a322f70c 1852
95eeceeb 1853 memset(info, 0, sizeof(*info));
8a2848a7 1854 if (layout_ddf2md(&vc->conf, &info->array) == -1)
1855 return;
a322f70c 1856 info->array.md_minor = -1;
90fa1a29
JS
1857 cptr = (__u32 *)(vc->conf.guid + 16);
1858 info->array.ctime = DECADE + __be32_to_cpu(*cptr);
d2ca6449
NB
1859 info->array.utime = DECADE + __be32_to_cpu(vc->conf.timestamp);
1860 info->array.chunk_size = 512 << vc->conf.chunk_shift;
da9b4a62 1861 info->custom_array_size = 0;
d2ca6449 1862
ddf94a43 1863 conf = &vc->conf;
1864 n_prim = __be16_to_cpu(conf->prim_elmnt_count);
1865 if (conf->sec_elmnt_count > 1 && cd >= n_prim) {
1866 int ibvd = cd / n_prim - 1;
1867 cd %= n_prim;
1868 conf = vc->other_bvds[ibvd];
1869 }
1870
f21e18ca 1871 if (cd >= 0 && (unsigned)cd < ddf->mppe) {
57a66662 1872 info->data_offset =
1873 __be64_to_cpu(LBA_OFFSET(ddf, &vc->conf)[cd]);
d2ca6449
NB
1874 if (vc->block_sizes)
1875 info->component_size = vc->block_sizes[cd];
1876 else
1877 info->component_size = __be64_to_cpu(vc->conf.blocks);
1878 }
a322f70c 1879
fb204fb2 1880 for (dl = ddf->dlist; dl ; dl = dl->next)
f5ded787 1881 if (dl->disk.refnum == conf->phys_refnum[cd])
fb204fb2
N
1882 break;
1883
a322f70c
DW
1884 info->disk.major = 0;
1885 info->disk.minor = 0;
fb204fb2 1886 info->disk.state = 0;
8592f29d
N
1887 if (dl) {
1888 info->disk.major = dl->major;
1889 info->disk.minor = dl->minor;
7c3fb3ec 1890 info->disk.raid_disk = cd + conf->sec_elmnt_seq
1891 * __be16_to_cpu(conf->prim_elmnt_count);
fb204fb2
N
1892 info->disk.number = dl->pdnum;
1893 info->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
8592f29d 1894 }
a322f70c 1895
103f2410
NB
1896 info->container_member = ddf->currentconf->vcnum;
1897
921d9e16 1898 info->recovery_start = MaxSector;
80d26cb2 1899 info->resync_start = 0;
624c5ad4 1900 info->reshape_active = 0;
6e75048b 1901 info->recovery_blocked = 0;
80d26cb2
NB
1902 if (!(ddf->virt->entries[info->container_member].state
1903 & DDF_state_inconsistent) &&
1904 (ddf->virt->entries[info->container_member].init_state
1905 & DDF_initstate_mask)
1906 == DDF_init_full)
b7528a20 1907 info->resync_start = MaxSector;
80d26cb2 1908
a322f70c
DW
1909 uuid_from_super_ddf(st, info->uuid);
1910
f35f2525
N
1911 info->array.major_version = -1;
1912 info->array.minor_version = -2;
9b63e648 1913 sprintf(info->text_version, "/%s/%d",
4dd2df09 1914 st->container_devnm,
9b63e648 1915 info->container_member);
a67dd8cc 1916 info->safe_mode_delay = 200;
159c3a1a 1917
db42fa9b
N
1918 memcpy(info->name, ddf->virt->entries[info->container_member].name, 16);
1919 info->name[16]=0;
1920 for(j=0; j<16; j++)
1921 if (info->name[j] == ' ')
1922 info->name[j] = 0;
a5d85af7
N
1923
1924 if (map)
1925 for (j = 0; j < map_disks; j++) {
1926 map[j] = 0;
1927 if (j < info->array.raid_disks) {
1928 int i = find_phys(ddf, vc->conf.phys_refnum[j]);
613b0d17 1929 if (i >= 0 &&
a5d85af7
N
1930 (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) &&
1931 !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed))
1932 map[i] = 1;
1933 }
1934 }
a322f70c
DW
1935}
1936
1937static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1938 char *update,
1939 char *devname, int verbose,
1940 int uuid_set, char *homehost)
1941{
1942 /* For 'assemble' and 'force' we need to return non-zero if any
1943 * change was made. For others, the return value is ignored.
1944 * Update options are:
1945 * force-one : This device looks a bit old but needs to be included,
1946 * update age info appropriately.
1947 * assemble: clear any 'faulty' flag to allow this device to
1948 * be assembled.
1949 * force-array: Array is degraded but being forced, mark it clean
1950 * if that will be needed to assemble it.
1951 *
1952 * newdev: not used ????
1953 * grow: Array has gained a new device - this is currently for
1954 * linear only
1955 * resync: mark as dirty so a resync will happen.
59e36268 1956 * uuid: Change the uuid of the array to match what is given
a322f70c
DW
1957 * homehost: update the recorded homehost
1958 * name: update the name - preserving the homehost
1959 * _reshape_progress: record new reshape_progress position.
1960 *
1961 * Following are not relevant for this version:
1962 * sparc2.2 : update from old dodgey metadata
1963 * super-minor: change the preferred_minor number
1964 * summaries: update redundant counters.
1965 */
1966 int rv = 0;
1967// struct ddf_super *ddf = st->sb;
7a7cc504 1968// struct vd_config *vd = find_vdcr(ddf, info->container_member);
a322f70c
DW
1969// struct virtual_entry *ve = find_ve(ddf);
1970
a322f70c
DW
1971 /* we don't need to handle "force-*" or "assemble" as
1972 * there is no need to 'trick' the kernel. We the metadata is
1973 * first updated to activate the array, all the implied modifications
1974 * will just happen.
1975 */
1976
1977 if (strcmp(update, "grow") == 0) {
1978 /* FIXME */
1e2b2765 1979 } else if (strcmp(update, "resync") == 0) {
a322f70c 1980// info->resync_checkpoint = 0;
1e2b2765 1981 } else if (strcmp(update, "homehost") == 0) {
a322f70c
DW
1982 /* homehost is stored in controller->vendor_data,
1983 * or it is when we are the vendor
1984 */
1985// if (info->vendor_is_local)
1986// strcpy(ddf->controller.vendor_data, homehost);
1e2b2765 1987 rv = -1;
f49208ec 1988 } else if (strcmp(update, "name") == 0) {
a322f70c
DW
1989 /* name is stored in virtual_entry->name */
1990// memset(ve->name, ' ', 16);
1991// strncpy(ve->name, info->name, 16);
1e2b2765 1992 rv = -1;
f49208ec 1993 } else if (strcmp(update, "_reshape_progress") == 0) {
a322f70c 1994 /* We don't support reshape yet */
f49208ec
N
1995 } else if (strcmp(update, "assemble") == 0 ) {
1996 /* Do nothing, just succeed */
1997 rv = 0;
1e2b2765
N
1998 } else
1999 rv = -1;
a322f70c
DW
2000
2001// update_all_csum(ddf);
2002
2003 return rv;
2004}
2005
5f8097be
NB
2006static void make_header_guid(char *guid)
2007{
2008 __u32 stamp;
5f8097be
NB
2009 /* Create a DDF Header of Virtual Disk GUID */
2010
2011 /* 24 bytes of fiction required.
2012 * first 8 are a 'vendor-id' - "Linux-MD"
2013 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
2014 * Remaining 8 random number plus timestamp
2015 */
2016 memcpy(guid, T10, sizeof(T10));
2017 stamp = __cpu_to_be32(0xdeadbeef);
2018 memcpy(guid+8, &stamp, 4);
2019 stamp = __cpu_to_be32(0);
2020 memcpy(guid+12, &stamp, 4);
2021 stamp = __cpu_to_be32(time(0) - DECADE);
2022 memcpy(guid+16, &stamp, 4);
bfb7ea78 2023 stamp = random32();
5f8097be 2024 memcpy(guid+20, &stamp, 4);
5f8097be 2025}
59e36268 2026
fb9d0acb 2027static unsigned int find_unused_vde(const struct ddf_super *ddf)
2028{
2029 unsigned int i;
2030 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
2031 if (all_ff(ddf->virt->entries[i].guid))
2032 return i;
2033 }
2034 return DDF_NOTFOUND;
2035}
2036
2037static unsigned int find_vde_by_name(const struct ddf_super *ddf,
2038 const char *name)
2039{
2040 unsigned int i;
2041 if (name == NULL)
2042 return DDF_NOTFOUND;
2043 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
2044 if (all_ff(ddf->virt->entries[i].guid))
2045 continue;
2046 if (!strncmp(name, ddf->virt->entries[i].name,
2047 sizeof(ddf->virt->entries[i].name)))
2048 return i;
2049 }
2050 return DDF_NOTFOUND;
2051}
2052
2053static unsigned int find_vde_by_guid(const struct ddf_super *ddf,
2054 const char *guid)
2055{
2056 unsigned int i;
2057 if (guid == NULL || all_ff(guid))
2058 return DDF_NOTFOUND;
2059 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++)
2060 if (!memcmp(ddf->virt->entries[i].guid, guid, DDF_GUID_LEN))
2061 return i;
2062 return DDF_NOTFOUND;
2063}
2064
78e44928
NB
2065static int init_super_ddf_bvd(struct supertype *st,
2066 mdu_array_info_t *info,
2067 unsigned long long size,
2068 char *name, char *homehost,
83cd1e97 2069 int *uuid, unsigned long long data_offset);
78e44928 2070
a322f70c
DW
2071static int init_super_ddf(struct supertype *st,
2072 mdu_array_info_t *info,
2073 unsigned long long size, char *name, char *homehost,
83cd1e97 2074 int *uuid, unsigned long long data_offset)
a322f70c
DW
2075{
2076 /* This is primarily called by Create when creating a new array.
2077 * We will then get add_to_super called for each component, and then
2078 * write_init_super called to write it out to each device.
2079 * For DDF, Create can create on fresh devices or on a pre-existing
2080 * array.
2081 * To create on a pre-existing array a different method will be called.
2082 * This one is just for fresh drives.
2083 *
2084 * We need to create the entire 'ddf' structure which includes:
2085 * DDF headers - these are easy.
2086 * Controller data - a Sector describing this controller .. not that
2087 * this is a controller exactly.
2088 * Physical Disk Record - one entry per device, so
2089 * leave plenty of space.
2090 * Virtual Disk Records - again, just leave plenty of space.
2091 * This just lists VDs, doesn't give details
2092 * Config records - describes the VDs that use this disk
2093 * DiskData - describes 'this' device.
2094 * BadBlockManagement - empty
2095 * Diag Space - empty
2096 * Vendor Logs - Could we put bitmaps here?
2097 *
2098 */
2099 struct ddf_super *ddf;
2100 char hostname[17];
2101 int hostlen;
a322f70c
DW
2102 int max_phys_disks, max_virt_disks;
2103 unsigned long long sector;
2104 int clen;
2105 int i;
2106 int pdsize, vdsize;
2107 struct phys_disk *pd;
2108 struct virtual_disk *vd;
2109
83cd1e97 2110 if (data_offset != INVALID_SECTORS) {
ed503f89 2111 pr_err("data-offset not supported by DDF\n");
83cd1e97
N
2112 return 0;
2113 }
2114
78e44928 2115 if (st->sb)
83cd1e97
N
2116 return init_super_ddf_bvd(st, info, size, name, homehost, uuid,
2117 data_offset);
ba7eb04f 2118
3d2c4fc7 2119 if (posix_memalign((void**)&ddf, 512, sizeof(*ddf)) != 0) {
e7b84f9d 2120 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
2121 return 0;
2122 }
6264b437 2123 memset(ddf, 0, sizeof(*ddf));
a322f70c
DW
2124 ddf->dlist = NULL; /* no physical disks yet */
2125 ddf->conflist = NULL; /* No virtual disks yet */
955e9ea1
DW
2126 st->sb = ddf;
2127
2128 if (info == NULL) {
2129 /* zeroing superblock */
2130 return 0;
2131 }
a322f70c
DW
2132
2133 /* At least 32MB *must* be reserved for the ddf. So let's just
2134 * start 32MB from the end, and put the primary header there.
2135 * Don't do secondary for now.
2136 * We don't know exactly where that will be yet as it could be
2137 * different on each device. To just set up the lengths.
2138 *
2139 */
2140
2141 ddf->anchor.magic = DDF_HEADER_MAGIC;
5f8097be 2142 make_header_guid(ddf->anchor.guid);
a322f70c 2143
59e36268 2144 memcpy(ddf->anchor.revision, DDF_REVISION_2, 8);
a322f70c
DW
2145 ddf->anchor.seq = __cpu_to_be32(1);
2146 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
2147 ddf->anchor.openflag = 0xFF;
2148 ddf->anchor.foreignflag = 0;
2149 ddf->anchor.enforcegroups = 0; /* Is this best?? */
2150 ddf->anchor.pad0 = 0xff;
2151 memset(ddf->anchor.pad1, 0xff, 12);
2152 memset(ddf->anchor.header_ext, 0xff, 32);
2153 ddf->anchor.primary_lba = ~(__u64)0;
2154 ddf->anchor.secondary_lba = ~(__u64)0;
2155 ddf->anchor.type = DDF_HEADER_ANCHOR;
2156 memset(ddf->anchor.pad2, 0xff, 3);
2157 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
2158 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
2159 of 32M reserved.. */
2160 max_phys_disks = 1023; /* Should be enough */
2161 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
2162 max_virt_disks = 255;
2163 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
2164 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
2165 ddf->max_part = 64;
8c3b8c2c 2166 ddf->mppe = 256;
59e36268
NB
2167 ddf->conf_rec_len = 1 + ROUND_UP(ddf->mppe * (4+8), 512)/512;
2168 ddf->anchor.config_record_len = __cpu_to_be16(ddf->conf_rec_len);
2169 ddf->anchor.max_primary_element_entries = __cpu_to_be16(ddf->mppe);
a322f70c 2170 memset(ddf->anchor.pad3, 0xff, 54);
a322f70c
DW
2171 /* controller sections is one sector long immediately
2172 * after the ddf header */
2173 sector = 1;
2174 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
2175 ddf->anchor.controller_section_length = __cpu_to_be32(1);
2176 sector += 1;
2177
2178 /* phys is 8 sectors after that */
2179 pdsize = ROUND_UP(sizeof(struct phys_disk) +
2180 sizeof(struct phys_disk_entry)*max_phys_disks,
2181 512);
2182 switch(pdsize/512) {
2183 case 2: case 8: case 32: case 128: case 512: break;
2184 default: abort();
2185 }
2186 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
2187 ddf->anchor.phys_section_length =
2188 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
2189 sector += pdsize/512;
2190
2191 /* virt is another 32 sectors */
2192 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
2193 sizeof(struct virtual_entry) * max_virt_disks,
2194 512);
2195 switch(vdsize/512) {
2196 case 2: case 8: case 32: case 128: case 512: break;
2197 default: abort();
2198 }
2199 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
2200 ddf->anchor.virt_section_length =
2201 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
2202 sector += vdsize/512;
2203
59e36268 2204 clen = ddf->conf_rec_len * (ddf->max_part+1);
a322f70c
DW
2205 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
2206 ddf->anchor.config_section_length = __cpu_to_be32(clen);
2207 sector += clen;
2208
2209 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
2210 ddf->anchor.data_section_length = __cpu_to_be32(1);
2211 sector += 1;
2212
2213 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
2214 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
2215 ddf->anchor.diag_space_length = __cpu_to_be32(0);
2216 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
2217 ddf->anchor.vendor_length = __cpu_to_be32(0);
2218 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
2219
2220 memset(ddf->anchor.pad4, 0xff, 256);
2221
2222 memcpy(&ddf->primary, &ddf->anchor, 512);
2223 memcpy(&ddf->secondary, &ddf->anchor, 512);
2224
2225 ddf->primary.openflag = 1; /* I guess.. */
2226 ddf->primary.type = DDF_HEADER_PRIMARY;
2227
2228 ddf->secondary.openflag = 1; /* I guess.. */
2229 ddf->secondary.type = DDF_HEADER_SECONDARY;
2230
2231 ddf->active = &ddf->primary;
2232
2233 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
2234
2235 /* 24 more bytes of fiction required.
2236 * first 8 are a 'vendor-id' - "Linux-MD"
2237 * Remaining 16 are serial number.... maybe a hostname would do?
2238 */
2239 memcpy(ddf->controller.guid, T10, sizeof(T10));
1ba6bff9
DW
2240 gethostname(hostname, sizeof(hostname));
2241 hostname[sizeof(hostname) - 1] = 0;
a322f70c
DW
2242 hostlen = strlen(hostname);
2243 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
2244 for (i = strlen(T10) ; i+hostlen < 24; i++)
2245 ddf->controller.guid[i] = ' ';
2246
2247 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
2248 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
2249 ddf->controller.type.sub_vendor_id = 0;
2250 ddf->controller.type.sub_device_id = 0;
2251 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
2252 memset(ddf->controller.pad, 0xff, 8);
2253 memset(ddf->controller.vendor_data, 0xff, 448);
a9e1c11d
N
2254 if (homehost && strlen(homehost) < 440)
2255 strcpy((char*)ddf->controller.vendor_data, homehost);
a322f70c 2256
3d2c4fc7 2257 if (posix_memalign((void**)&pd, 512, pdsize) != 0) {
e7b84f9d 2258 pr_err("%s could not allocate pd\n", __func__);
3d2c4fc7
DW
2259 return 0;
2260 }
6416d527 2261 ddf->phys = pd;
a322f70c
DW
2262 ddf->pdsize = pdsize;
2263
2264 memset(pd, 0xff, pdsize);
2265 memset(pd, 0, sizeof(*pd));
076515ba 2266 pd->magic = DDF_PHYS_RECORDS_MAGIC;
a322f70c
DW
2267 pd->used_pdes = __cpu_to_be16(0);
2268 pd->max_pdes = __cpu_to_be16(max_phys_disks);
2269 memset(pd->pad, 0xff, 52);
4a3ca8ac 2270 for (i = 0; i < max_phys_disks; i++)
2271 memset(pd->entries[i].guid, 0xff, DDF_GUID_LEN);
a322f70c 2272
3d2c4fc7 2273 if (posix_memalign((void**)&vd, 512, vdsize) != 0) {
e7b84f9d 2274 pr_err("%s could not allocate vd\n", __func__);
3d2c4fc7
DW
2275 return 0;
2276 }
6416d527 2277 ddf->virt = vd;
a322f70c
DW
2278 ddf->vdsize = vdsize;
2279 memset(vd, 0, vdsize);
2280 vd->magic = DDF_VIRT_RECORDS_MAGIC;
2281 vd->populated_vdes = __cpu_to_be16(0);
2282 vd->max_vdes = __cpu_to_be16(max_virt_disks);
2283 memset(vd->pad, 0xff, 52);
2284
5f8097be
NB
2285 for (i=0; i<max_virt_disks; i++)
2286 memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
2287
a322f70c 2288 st->sb = ddf;
7d5a7ff3 2289 ddf_set_updates_pending(ddf);
a322f70c
DW
2290 return 1;
2291}
2292
5f8097be
NB
2293static int chunk_to_shift(int chunksize)
2294{
2295 return ffs(chunksize/512)-1;
2296}
2297
0e600426 2298#ifndef MDASSEMBLE
59e36268
NB
2299struct extent {
2300 unsigned long long start, size;
2301};
78e44928 2302static int cmp_extent(const void *av, const void *bv)
59e36268
NB
2303{
2304 const struct extent *a = av;
2305 const struct extent *b = bv;
2306 if (a->start < b->start)
2307 return -1;
2308 if (a->start > b->start)
2309 return 1;
2310 return 0;
2311}
2312
78e44928 2313static struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
59e36268
NB
2314{
2315 /* find a list of used extents on the give physical device
2316 * (dnum) of the given ddf.
2317 * Return a malloced array of 'struct extent'
2318
613b0d17 2319 * FIXME ignore DDF_Legacy devices?
59e36268
NB
2320
2321 */
2322 struct extent *rv;
2323 int n = 0;
fcc22180 2324 unsigned int i;
59e36268 2325
503975b9 2326 rv = xmalloc(sizeof(struct extent) * (ddf->max_part + 2));
59e36268
NB
2327
2328 for (i = 0; i < ddf->max_part; i++) {
fcc22180 2329 const struct vd_config *bvd;
2330 unsigned int ibvd;
59e36268 2331 struct vcl *v = dl->vlist[i];
fcc22180 2332 if (v == NULL ||
2333 get_pd_index_from_refnum(v, dl->disk.refnum, ddf->mppe,
2334 &bvd, &ibvd) == DDF_NOTFOUND)
59e36268 2335 continue;
fcc22180 2336 rv[n].start = __be64_to_cpu(LBA_OFFSET(ddf, bvd)[ibvd]);
2337 rv[n].size = __be64_to_cpu(bvd->blocks);
2338 n++;
59e36268
NB
2339 }
2340 qsort(rv, n, sizeof(*rv), cmp_extent);
2341
2342 rv[n].start = __be64_to_cpu(ddf->phys->entries[dl->pdnum].config_size);
2343 rv[n].size = 0;
2344 return rv;
2345}
0e600426 2346#endif
59e36268 2347
5f8097be
NB
2348static int init_super_ddf_bvd(struct supertype *st,
2349 mdu_array_info_t *info,
2350 unsigned long long size,
2351 char *name, char *homehost,
83cd1e97 2352 int *uuid, unsigned long long data_offset)
5f8097be
NB
2353{
2354 /* We are creating a BVD inside a pre-existing container.
2355 * so st->sb is already set.
2356 * We need to create a new vd_config and a new virtual_entry
2357 */
2358 struct ddf_super *ddf = st->sb;
5aaf6c7b 2359 unsigned int venum, i;
5f8097be
NB
2360 struct virtual_entry *ve;
2361 struct vcl *vcl;
2362 struct vd_config *vc;
5f8097be 2363
fb9d0acb 2364 if (find_vde_by_name(ddf, name) != DDF_NOTFOUND) {
2365 pr_err("This ddf already has an array called %s\n", name);
5f8097be
NB
2366 return 0;
2367 }
fb9d0acb 2368 venum = find_unused_vde(ddf);
2369 if (venum == DDF_NOTFOUND) {
2370 pr_err("Cannot find spare slot for virtual disk\n");
5f8097be
NB
2371 return 0;
2372 }
2373 ve = &ddf->virt->entries[venum];
2374
2375 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
2376 * timestamp, random number
2377 */
2378 make_header_guid(ve->guid);
2379 ve->unit = __cpu_to_be16(info->md_minor);
2380 ve->pad0 = 0xFFFF;
2381 ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
2382 ve->type = 0;
7a7cc504
NB
2383 ve->state = DDF_state_degraded; /* Will be modified as devices are added */
2384 if (info->state & 1) /* clean */
2385 ve->init_state = DDF_init_full;
2386 else
2387 ve->init_state = DDF_init_not;
2388
5f8097be
NB
2389 memset(ve->pad1, 0xff, 14);
2390 memset(ve->name, ' ', 16);
2391 if (name)
2392 strncpy(ve->name, name, 16);
2393 ddf->virt->populated_vdes =
2394 __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
2395
2396 /* Now create a new vd_config */
3d2c4fc7
DW
2397 if (posix_memalign((void**)&vcl, 512,
2398 (offsetof(struct vcl, conf) + ddf->conf_rec_len * 512)) != 0) {
e7b84f9d 2399 pr_err("%s could not allocate vd_config\n", __func__);
3d2c4fc7
DW
2400 return 0;
2401 }
59e36268
NB
2402 vcl->vcnum = venum;
2403 vcl->block_sizes = NULL; /* FIXME not for CONCAT */
5f8097be
NB
2404 vc = &vcl->conf;
2405
2406 vc->magic = DDF_VD_CONF_MAGIC;
2407 memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
2408 vc->timestamp = __cpu_to_be32(time(0)-DECADE);
2409 vc->seqnum = __cpu_to_be32(1);
2410 memset(vc->pad0, 0xff, 24);
5f8097be 2411 vc->chunk_shift = chunk_to_shift(info->chunk_size);
a3163bf0 2412 if (layout_md2ddf(info, vc) == -1 ||
2413 __be16_to_cpu(vc->prim_elmnt_count) > ddf->mppe) {
2414 pr_err("%s: unsupported RAID level/layout %d/%d with %d disks\n",
2415 __func__, info->level, info->layout, info->raid_disks);
2416 free(vcl);
2417 return 0;
2418 }
5f8097be 2419 vc->sec_elmnt_seq = 0;
3c48f7be 2420 if (alloc_other_bvds(ddf, vcl) != 0) {
2421 pr_err("%s could not allocate other bvds\n",
2422 __func__);
2423 free(vcl);
2424 return 0;
2425 }
5f8097be
NB
2426 vc->blocks = __cpu_to_be64(info->size * 2);
2427 vc->array_blocks = __cpu_to_be64(
2428 calc_array_size(info->level, info->raid_disks, info->layout,
2429 info->chunk_size, info->size*2));
2430 memset(vc->pad1, 0xff, 8);
2431 vc->spare_refs[0] = 0xffffffff;
2432 vc->spare_refs[1] = 0xffffffff;
2433 vc->spare_refs[2] = 0xffffffff;
2434 vc->spare_refs[3] = 0xffffffff;
2435 vc->spare_refs[4] = 0xffffffff;
2436 vc->spare_refs[5] = 0xffffffff;
2437 vc->spare_refs[6] = 0xffffffff;
2438 vc->spare_refs[7] = 0xffffffff;
2439 memset(vc->cache_pol, 0, 8);
2440 vc->bg_rate = 0x80;
2441 memset(vc->pad2, 0xff, 3);
2442 memset(vc->pad3, 0xff, 52);
2443 memset(vc->pad4, 0xff, 192);
2444 memset(vc->v0, 0xff, 32);
2445 memset(vc->v1, 0xff, 32);
2446 memset(vc->v2, 0xff, 16);
2447 memset(vc->v3, 0xff, 16);
2448 memset(vc->vendor, 0xff, 32);
598f0d58 2449
8c3b8c2c 2450 memset(vc->phys_refnum, 0xff, 4*ddf->mppe);
e5a2a3cf 2451 memset(vc->phys_refnum+ddf->mppe, 0x00, 8*ddf->mppe);
5f8097be 2452
5aaf6c7b 2453 for (i = 1; i < vc->sec_elmnt_count; i++) {
2454 memcpy(vcl->other_bvds[i-1], vc, ddf->conf_rec_len * 512);
2455 vcl->other_bvds[i-1]->sec_elmnt_seq = i;
2456 }
2457
5f8097be
NB
2458 vcl->next = ddf->conflist;
2459 ddf->conflist = vcl;
d2ca6449 2460 ddf->currentconf = vcl;
7d5a7ff3 2461 ddf_set_updates_pending(ddf);
5f8097be
NB
2462 return 1;
2463}
2464
63eb2454 2465static int get_svd_state(const struct ddf_super *, const struct vcl *);
2466
0e600426 2467#ifndef MDASSEMBLE
5f8097be
NB
2468static void add_to_super_ddf_bvd(struct supertype *st,
2469 mdu_disk_info_t *dk, int fd, char *devname)
2470{
2471 /* fd and devname identify a device with-in the ddf container (st).
2472 * dk identifies a location in the new BVD.
2473 * We need to find suitable free space in that device and update
2474 * the phys_refnum and lba_offset for the newly created vd_config.
2475 * We might also want to update the type in the phys_disk
5575e7d9 2476 * section.
8592f29d
N
2477 *
2478 * Alternately: fd == -1 and we have already chosen which device to
2479 * use and recorded in dlist->raid_disk;
5f8097be
NB
2480 */
2481 struct dl *dl;
2482 struct ddf_super *ddf = st->sb;
2483 struct vd_config *vc;
f21e18ca 2484 unsigned int i;
59e36268
NB
2485 unsigned long long blocks, pos, esize;
2486 struct extent *ex;
475ccbdb 2487 unsigned int raid_disk = dk->raid_disk;
5f8097be 2488
8592f29d
N
2489 if (fd == -1) {
2490 for (dl = ddf->dlist; dl ; dl = dl->next)
2491 if (dl->raiddisk == dk->raid_disk)
2492 break;
2493 } else {
2494 for (dl = ddf->dlist; dl ; dl = dl->next)
2495 if (dl->major == dk->major &&
2496 dl->minor == dk->minor)
2497 break;
2498 }
5f8097be
NB
2499 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
2500 return;
2501
d2ca6449 2502 vc = &ddf->currentconf->conf;
475ccbdb 2503 if (vc->sec_elmnt_count > 1) {
2504 unsigned int n = __be16_to_cpu(vc->prim_elmnt_count);
2505 if (raid_disk >= n)
2506 vc = ddf->currentconf->other_bvds[raid_disk / n - 1];
2507 raid_disk %= n;
2508 }
59e36268
NB
2509
2510 ex = get_extents(ddf, dl);
2511 if (!ex)
2512 return;
2513
2514 i = 0; pos = 0;
2515 blocks = __be64_to_cpu(vc->blocks);
d2ca6449
NB
2516 if (ddf->currentconf->block_sizes)
2517 blocks = ddf->currentconf->block_sizes[dk->raid_disk];
59e36268
NB
2518
2519 do {
2520 esize = ex[i].start - pos;
2521 if (esize >= blocks)
2522 break;
2523 pos = ex[i].start + ex[i].size;
2524 i++;
2525 } while (ex[i-1].size);
2526
2527 free(ex);
2528 if (esize < blocks)
2529 return;
2530
d2ca6449 2531 ddf->currentdev = dk->raid_disk;
475ccbdb 2532 vc->phys_refnum[raid_disk] = dl->disk.refnum;
2533 LBA_OFFSET(ddf, vc)[raid_disk] = __cpu_to_be64(pos);
5f8097be 2534
f21e18ca 2535 for (i = 0; i < ddf->max_part ; i++)
5575e7d9
NB
2536 if (dl->vlist[i] == NULL)
2537 break;
2538 if (i == ddf->max_part)
2539 return;
d2ca6449 2540 dl->vlist[i] = ddf->currentconf;
5f8097be 2541
8592f29d
N
2542 if (fd >= 0)
2543 dl->fd = fd;
2544 if (devname)
2545 dl->devname = devname;
7a7cc504 2546
63eb2454 2547 /* Check if we can mark array as optimal yet */
d2ca6449 2548 i = ddf->currentconf->vcnum;
63eb2454 2549 ddf->virt->entries[i].state =
2550 (ddf->virt->entries[i].state & ~DDF_state_mask)
2551 | get_svd_state(ddf, ddf->currentconf);
5575e7d9
NB
2552 ddf->phys->entries[dl->pdnum].type &= ~__cpu_to_be16(DDF_Global_Spare);
2553 ddf->phys->entries[dl->pdnum].type |= __cpu_to_be16(DDF_Active_in_VD);
7d5a7ff3 2554 ddf_set_updates_pending(ddf);
5f8097be
NB
2555}
2556
4a3ca8ac 2557static unsigned int find_unused_pde(const struct ddf_super *ddf)
2558{
2559 unsigned int i;
2560 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes); i++) {
2561 if (all_ff(ddf->phys->entries[i].guid))
2562 return i;
2563 }
2564 return DDF_NOTFOUND;
2565}
2566
a322f70c
DW
2567/* add a device to a container, either while creating it or while
2568 * expanding a pre-existing container
2569 */
f20c3968 2570static int add_to_super_ddf(struct supertype *st,
72ca9bcf
N
2571 mdu_disk_info_t *dk, int fd, char *devname,
2572 unsigned long long data_offset)
a322f70c
DW
2573{
2574 struct ddf_super *ddf = st->sb;
2575 struct dl *dd;
2576 time_t now;
2577 struct tm *tm;
2578 unsigned long long size;
2579 struct phys_disk_entry *pde;
f21e18ca 2580 unsigned int n, i;
a322f70c 2581 struct stat stb;
90fa1a29 2582 __u32 *tptr;
a322f70c 2583
78e44928
NB
2584 if (ddf->currentconf) {
2585 add_to_super_ddf_bvd(st, dk, fd, devname);
f20c3968 2586 return 0;
78e44928
NB
2587 }
2588
a322f70c
DW
2589 /* This is device numbered dk->number. We need to create
2590 * a phys_disk entry and a more detailed disk_data entry.
2591 */
2592 fstat(fd, &stb);
4a3ca8ac 2593 n = find_unused_pde(ddf);
2594 if (n == DDF_NOTFOUND) {
2595 pr_err("%s: No free slot in array, cannot add disk\n",
2596 __func__);
2597 return 1;
2598 }
2599 pde = &ddf->phys->entries[n];
4ee8cca9 2600 get_dev_size(fd, NULL, &size);
2601 if (size <= 32*1024*1024) {
2602 pr_err("%s: device size must be at least 32MB\n",
2603 __func__);
2604 return 1;
2605 }
2606 size >>= 9;
4a3ca8ac 2607
3d2c4fc7
DW
2608 if (posix_memalign((void**)&dd, 512,
2609 sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part) != 0) {
e7b84f9d
N
2610 pr_err("%s could allocate buffer for new disk, aborting\n",
2611 __func__);
f20c3968 2612 return 1;
3d2c4fc7 2613 }
a322f70c
DW
2614 dd->major = major(stb.st_rdev);
2615 dd->minor = minor(stb.st_rdev);
2616 dd->devname = devname;
a322f70c 2617 dd->fd = fd;
b2280677 2618 dd->spare = NULL;
a322f70c
DW
2619
2620 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
2621 now = time(0);
2622 tm = localtime(&now);
2623 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
2624 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
90fa1a29
JS
2625 tptr = (__u32 *)(dd->disk.guid + 16);
2626 *tptr++ = random32();
2627 *tptr = random32();
a322f70c 2628
59e36268
NB
2629 do {
2630 /* Cannot be bothered finding a CRC of some irrelevant details*/
bfb7ea78 2631 dd->disk.refnum = random32();
f21e18ca
N
2632 for (i = __be16_to_cpu(ddf->active->max_pd_entries);
2633 i > 0; i--)
2634 if (ddf->phys->entries[i-1].refnum == dd->disk.refnum)
59e36268 2635 break;
f21e18ca 2636 } while (i > 0);
59e36268 2637
a322f70c
DW
2638 dd->disk.forced_ref = 1;
2639 dd->disk.forced_guid = 1;
2640 memset(dd->disk.vendor, ' ', 32);
2641 memcpy(dd->disk.vendor, "Linux", 5);
2642 memset(dd->disk.pad, 0xff, 442);
b2280677 2643 for (i = 0; i < ddf->max_part ; i++)
a322f70c
DW
2644 dd->vlist[i] = NULL;
2645
5575e7d9
NB
2646 dd->pdnum = n;
2647
2cc2983d
N
2648 if (st->update_tail) {
2649 int len = (sizeof(struct phys_disk) +
2650 sizeof(struct phys_disk_entry));
2651 struct phys_disk *pd;
2652
503975b9 2653 pd = xmalloc(len);
2cc2983d
N
2654 pd->magic = DDF_PHYS_RECORDS_MAGIC;
2655 pd->used_pdes = __cpu_to_be16(n);
2656 pde = &pd->entries[0];
2657 dd->mdupdate = pd;
4a3ca8ac 2658 } else
2659 ddf->phys->used_pdes = __cpu_to_be16(
2660 1 + __be16_to_cpu(ddf->phys->used_pdes));
a322f70c
DW
2661
2662 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
2663 pde->refnum = dd->disk.refnum;
5575e7d9 2664 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID | DDF_Global_Spare);
a322f70c 2665 pde->state = __cpu_to_be16(DDF_Online);
4ee8cca9 2666 dd->size = size;
2667 /*
2668 * If there is already a device in dlist, try to reserve the same
2669 * amount of workspace. Otherwise, use 32MB.
2670 * We checked disk size above already.
2671 */
2672#define __calc_lba(new, old, lba, mb) do { \
2673 unsigned long long dif; \
2674 if ((old) != NULL) \
2675 dif = (old)->size - __be64_to_cpu((old)->lba); \
2676 else \
2677 dif = (new)->size; \
2678 if ((new)->size > dif) \
2679 (new)->lba = __cpu_to_be64((new)->size - dif); \
2680 else \
2681 (new)->lba = __cpu_to_be64((new)->size - (mb*1024*2)); \
2682 } while (0)
2683 __calc_lba(dd, ddf->dlist, workspace_lba, 32);
2684 __calc_lba(dd, ddf->dlist, primary_lba, 16);
2685 __calc_lba(dd, ddf->dlist, secondary_lba, 32);
2686 pde->config_size = dd->workspace_lba;
2687
a322f70c
DW
2688 sprintf(pde->path, "%17.17s","Information: nil") ;
2689 memset(pde->pad, 0xff, 6);
2690
2cc2983d
N
2691 if (st->update_tail) {
2692 dd->next = ddf->add_list;
2693 ddf->add_list = dd;
2694 } else {
2695 dd->next = ddf->dlist;
2696 ddf->dlist = dd;
7d5a7ff3 2697 ddf_set_updates_pending(ddf);
2cc2983d 2698 }
f20c3968
DW
2699
2700 return 0;
a322f70c
DW
2701}
2702
4dd968cc
N
2703static int remove_from_super_ddf(struct supertype *st, mdu_disk_info_t *dk)
2704{
2705 struct ddf_super *ddf = st->sb;
2706 struct dl *dl;
2707
2708 /* mdmon has noticed that this disk (dk->major/dk->minor) has
2709 * disappeared from the container.
2710 * We need to arrange that it disappears from the metadata and
2711 * internal data structures too.
2712 * Most of the work is done by ddf_process_update which edits
2713 * the metadata and closes the file handle and attaches the memory
2714 * where free_updates will free it.
2715 */
2716 for (dl = ddf->dlist; dl ; dl = dl->next)
2717 if (dl->major == dk->major &&
2718 dl->minor == dk->minor)
2719 break;
2720 if (!dl)
2721 return -1;
2722
2723 if (st->update_tail) {
2724 int len = (sizeof(struct phys_disk) +
2725 sizeof(struct phys_disk_entry));
2726 struct phys_disk *pd;
2727
503975b9 2728 pd = xmalloc(len);
4dd968cc
N
2729 pd->magic = DDF_PHYS_RECORDS_MAGIC;
2730 pd->used_pdes = __cpu_to_be16(dl->pdnum);
2731 pd->entries[0].state = __cpu_to_be16(DDF_Missing);
2732 append_metadata_update(st, pd, len);
2733 }
2734 return 0;
2735}
2736
a322f70c
DW
2737/*
2738 * This is the write_init_super method for a ddf container. It is
2739 * called when creating a container or adding another device to a
2740 * container.
2741 */
42d5dfd9 2742#define NULL_CONF_SZ 4096
18a2f463 2743
7f798aca 2744static int __write_ddf_structure(struct dl *d, struct ddf_super *ddf, __u8 type,
2745 char *null_aligned)
a322f70c 2746{
7f798aca 2747 unsigned long long sector;
2748 struct ddf_header *header;
2749 int fd, i, n_config, conf_size;
a4057a88 2750 int ret = 0;
7f798aca 2751
2752 fd = d->fd;
2753
2754 switch (type) {
2755 case DDF_HEADER_PRIMARY:
2756 header = &ddf->primary;
2757 sector = __be64_to_cpu(header->primary_lba);
2758 break;
2759 case DDF_HEADER_SECONDARY:
2760 header = &ddf->secondary;
2761 sector = __be64_to_cpu(header->secondary_lba);
2762 break;
2763 default:
2764 return 0;
2765 }
2766
2767 header->type = type;
a4057a88 2768 header->openflag = 1;
7f798aca 2769 header->crc = calc_crc(header, 512);
2770
2771 lseek64(fd, sector<<9, 0);
2772 if (write(fd, header, 512) < 0)
a4057a88 2773 goto out;
7f798aca 2774
2775 ddf->controller.crc = calc_crc(&ddf->controller, 512);
2776 if (write(fd, &ddf->controller, 512) < 0)
a4057a88 2777 goto out;
a322f70c 2778
7f798aca 2779 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
2780 if (write(fd, ddf->phys, ddf->pdsize) < 0)
a4057a88 2781 goto out;
7f798aca 2782 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
2783 if (write(fd, ddf->virt, ddf->vdsize) < 0)
a4057a88 2784 goto out;
7f798aca 2785
2786 /* Now write lots of config records. */
2787 n_config = ddf->max_part;
2788 conf_size = ddf->conf_rec_len * 512;
2789 for (i = 0 ; i <= n_config ; i++) {
e3c2a365 2790 struct vcl *c;
2791 struct vd_config *vdc = NULL;
2792 if (i == n_config) {
7f798aca 2793 c = (struct vcl *)d->spare;
e3c2a365 2794 if (c)
2795 vdc = &c->conf;
2796 } else {
2797 unsigned int dummy;
2798 c = d->vlist[i];
2799 if (c)
2800 get_pd_index_from_refnum(
2801 c, d->disk.refnum,
2802 ddf->mppe,
2803 (const struct vd_config **)&vdc,
2804 &dummy);
2805 }
7f798aca 2806 if (c) {
be9b9ef4 2807 dprintf("writing conf record %i on disk %08x for %s/%u\n",
ad60eea1 2808 i, __be32_to_cpu(d->disk.refnum),
2809 guid_str(vdc->guid),
be9b9ef4 2810 vdc->sec_elmnt_seq);
dacf3dc5 2811 vdc->seqnum = header->seq;
e3c2a365 2812 vdc->crc = calc_crc(vdc, conf_size);
2813 if (write(fd, vdc, conf_size) < 0)
7f798aca 2814 break;
2815 } else {
2816 unsigned int togo = conf_size;
2817 while (togo > NULL_CONF_SZ) {
2818 if (write(fd, null_aligned, NULL_CONF_SZ) < 0)
2819 break;
2820 togo -= NULL_CONF_SZ;
2821 }
2822 if (write(fd, null_aligned, togo) < 0)
2823 break;
2824 }
2825 }
2826 if (i <= n_config)
a4057a88 2827 goto out;
7f798aca 2828
2829 d->disk.crc = calc_crc(&d->disk, 512);
2830 if (write(fd, &d->disk, 512) < 0)
a4057a88 2831 goto out;
7f798aca 2832
a4057a88 2833 ret = 1;
2834out:
2835 header->openflag = 0;
2836 header->crc = calc_crc(header, 512);
2837
2838 lseek64(fd, sector<<9, 0);
2839 if (write(fd, header, 512) < 0)
2840 ret = 0;
2841
2842 return ret;
7f798aca 2843}
2844
2845static int __write_init_super_ddf(struct supertype *st)
2846{
a322f70c 2847 struct ddf_super *ddf = st->sb;
a322f70c 2848 struct dl *d;
175593bf
DW
2849 int attempts = 0;
2850 int successes = 0;
7f798aca 2851 unsigned long long size;
42d5dfd9 2852 char *null_aligned;
0175cbf6 2853 __u32 seq;
42d5dfd9 2854
7d5a7ff3 2855 pr_state(ddf, __func__);
42d5dfd9
JS
2856 if (posix_memalign((void**)&null_aligned, 4096, NULL_CONF_SZ) != 0) {
2857 return -ENOMEM;
2858 }
2859 memset(null_aligned, 0xff, NULL_CONF_SZ);
a322f70c 2860
dc9e279c 2861 seq = ddf->active->seq + 1;
0175cbf6 2862
175593bf
DW
2863 /* try to write updated metadata,
2864 * if we catch a failure move on to the next disk
2865 */
a322f70c
DW
2866 for (d = ddf->dlist; d; d=d->next) {
2867 int fd = d->fd;
2868
2869 if (fd < 0)
2870 continue;
2871
175593bf 2872 attempts++;
a322f70c
DW
2873 /* We need to fill in the primary, (secondary) and workspace
2874 * lba's in the headers, set their checksums,
2875 * Also checksum phys, virt....
2876 *
2877 * Then write everything out, finally the anchor is written.
2878 */
2879 get_dev_size(fd, NULL, &size);
2880 size /= 512;
097bcf00 2881 if (d->workspace_lba != 0)
2882 ddf->anchor.workspace_lba = d->workspace_lba;
2883 else
2884 ddf->anchor.workspace_lba =
2885 __cpu_to_be64(size - 32*1024*2);
2886 if (d->primary_lba != 0)
2887 ddf->anchor.primary_lba = d->primary_lba;
2888 else
2889 ddf->anchor.primary_lba =
2890 __cpu_to_be64(size - 16*1024*2);
2891 if (d->secondary_lba != 0)
2892 ddf->anchor.secondary_lba = d->secondary_lba;
2893 else
2894 ddf->anchor.secondary_lba =
2895 __cpu_to_be64(size - 32*1024*2);
0175cbf6 2896 ddf->anchor.seq = seq;
a322f70c
DW
2897 memcpy(&ddf->primary, &ddf->anchor, 512);
2898 memcpy(&ddf->secondary, &ddf->anchor, 512);
2899
2900 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
2901 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
2902 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
2903
7f798aca 2904 if (!__write_ddf_structure(d, ddf, DDF_HEADER_PRIMARY,
2905 null_aligned))
175593bf 2906 continue;
a322f70c 2907
7f798aca 2908 if (!__write_ddf_structure(d, ddf, DDF_HEADER_SECONDARY,
2909 null_aligned))
175593bf 2910 continue;
a322f70c 2911
a322f70c 2912 lseek64(fd, (size-1)*512, SEEK_SET);
175593bf
DW
2913 if (write(fd, &ddf->anchor, 512) < 0)
2914 continue;
2915 successes++;
2916 }
42d5dfd9 2917 free(null_aligned);
175593bf 2918
175593bf 2919 return attempts != successes;
a322f70c 2920}
7a7cc504
NB
2921
2922static int write_init_super_ddf(struct supertype *st)
2923{
9b1fb677
DW
2924 struct ddf_super *ddf = st->sb;
2925 struct vcl *currentconf = ddf->currentconf;
2926
2927 /* we are done with currentconf reset it to point st at the container */
2928 ddf->currentconf = NULL;
edd8d13c
NB
2929
2930 if (st->update_tail) {
2931 /* queue the virtual_disk and vd_config as metadata updates */
2932 struct virtual_disk *vd;
2933 struct vd_config *vc;
edd8d13c
NB
2934 int len;
2935
9b1fb677 2936 if (!currentconf) {
2cc2983d
N
2937 int len = (sizeof(struct phys_disk) +
2938 sizeof(struct phys_disk_entry));
2939
2940 /* adding a disk to the container. */
2941 if (!ddf->add_list)
2942 return 0;
2943
2944 append_metadata_update(st, ddf->add_list->mdupdate, len);
2945 ddf->add_list->mdupdate = NULL;
2946 return 0;
2947 }
2948
2949 /* Newly created VD */
2950
edd8d13c
NB
2951 /* First the virtual disk. We have a slightly fake header */
2952 len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry);
503975b9 2953 vd = xmalloc(len);
edd8d13c 2954 *vd = *ddf->virt;
9b1fb677
DW
2955 vd->entries[0] = ddf->virt->entries[currentconf->vcnum];
2956 vd->populated_vdes = __cpu_to_be16(currentconf->vcnum);
edd8d13c
NB
2957 append_metadata_update(st, vd, len);
2958
2959 /* Then the vd_config */
2960 len = ddf->conf_rec_len * 512;
503975b9 2961 vc = xmalloc(len);
9b1fb677 2962 memcpy(vc, &currentconf->conf, len);
edd8d13c
NB
2963 append_metadata_update(st, vc, len);
2964
2965 /* FIXME I need to close the fds! */
2966 return 0;
613b0d17 2967 } else {
d682f344 2968 struct dl *d;
19041058 2969 if (!currentconf)
2970 for (d = ddf->dlist; d; d=d->next)
2971 while (Kill(d->devname, NULL, 0, -1, 1) == 0);
1cc7f4fe 2972 return __write_init_super_ddf(st);
d682f344 2973 }
7a7cc504
NB
2974}
2975
a322f70c
DW
2976#endif
2977
387fcd59
N
2978static __u64 avail_size_ddf(struct supertype *st, __u64 devsize,
2979 unsigned long long data_offset)
a322f70c
DW
2980{
2981 /* We must reserve the last 32Meg */
2982 if (devsize <= 32*1024*2)
2983 return 0;
2984 return devsize - 32*1024*2;
2985}
2986
2987#ifndef MDASSEMBLE
8592f29d
N
2988
2989static int reserve_space(struct supertype *st, int raiddisks,
2990 unsigned long long size, int chunk,
2991 unsigned long long *freesize)
2992{
2993 /* Find 'raiddisks' spare extents at least 'size' big (but
2994 * only caring about multiples of 'chunk') and remember
2995 * them.
2996 * If the cannot be found, fail.
2997 */
2998 struct dl *dl;
2999 struct ddf_super *ddf = st->sb;
3000 int cnt = 0;
3001
3002 for (dl = ddf->dlist; dl ; dl=dl->next) {
613b0d17 3003 dl->raiddisk = -1;
8592f29d
N
3004 dl->esize = 0;
3005 }
3006 /* Now find largest extent on each device */
3007 for (dl = ddf->dlist ; dl ; dl=dl->next) {
3008 struct extent *e = get_extents(ddf, dl);
3009 unsigned long long pos = 0;
3010 int i = 0;
3011 int found = 0;
3012 unsigned long long minsize = size;
3013
3014 if (size == 0)
3015 minsize = chunk;
3016
3017 if (!e)
3018 continue;
3019 do {
3020 unsigned long long esize;
3021 esize = e[i].start - pos;
3022 if (esize >= minsize) {
3023 found = 1;
3024 minsize = esize;
3025 }
3026 pos = e[i].start + e[i].size;
3027 i++;
3028 } while (e[i-1].size);
3029 if (found) {
3030 cnt++;
3031 dl->esize = minsize;
3032 }
3033 free(e);
3034 }
3035 if (cnt < raiddisks) {
e7b84f9d 3036 pr_err("not enough devices with space to create array.\n");
8592f29d
N
3037 return 0; /* No enough free spaces large enough */
3038 }
3039 if (size == 0) {
3040 /* choose the largest size of which there are at least 'raiddisk' */
3041 for (dl = ddf->dlist ; dl ; dl=dl->next) {
3042 struct dl *dl2;
3043 if (dl->esize <= size)
3044 continue;
3045 /* This is bigger than 'size', see if there are enough */
3046 cnt = 0;
7b80ad6a 3047 for (dl2 = ddf->dlist; dl2 ; dl2=dl2->next)
8592f29d
N
3048 if (dl2->esize >= dl->esize)
3049 cnt++;
3050 if (cnt >= raiddisks)
3051 size = dl->esize;
3052 }
3053 if (chunk) {
3054 size = size / chunk;
3055 size *= chunk;
3056 }
3057 *freesize = size;
3058 if (size < 32) {
e7b84f9d 3059 pr_err("not enough spare devices to create array.\n");
8592f29d
N
3060 return 0;
3061 }
3062 }
3063 /* We have a 'size' of which there are enough spaces.
3064 * We simply do a first-fit */
3065 cnt = 0;
3066 for (dl = ddf->dlist ; dl && cnt < raiddisks ; dl=dl->next) {
3067 if (dl->esize < size)
3068 continue;
613b0d17 3069
8592f29d
N
3070 dl->raiddisk = cnt;
3071 cnt++;
3072 }
3073 return 1;
3074}
3075
2c514b71
NB
3076static int
3077validate_geometry_ddf_container(struct supertype *st,
3078 int level, int layout, int raiddisks,
3079 int chunk, unsigned long long size,
af4348dd 3080 unsigned long long data_offset,
2c514b71
NB
3081 char *dev, unsigned long long *freesize,
3082 int verbose);
78e44928
NB
3083
3084static int validate_geometry_ddf_bvd(struct supertype *st,
3085 int level, int layout, int raiddisks,
c21e737b 3086 int *chunk, unsigned long long size,
af4348dd 3087 unsigned long long data_offset,
2c514b71
NB
3088 char *dev, unsigned long long *freesize,
3089 int verbose);
78e44928
NB
3090
3091static int validate_geometry_ddf(struct supertype *st,
2c514b71 3092 int level, int layout, int raiddisks,
c21e737b 3093 int *chunk, unsigned long long size,
af4348dd 3094 unsigned long long data_offset,
2c514b71
NB
3095 char *dev, unsigned long long *freesize,
3096 int verbose)
a322f70c
DW
3097{
3098 int fd;
3099 struct mdinfo *sra;
3100 int cfd;
3101
3102 /* ddf potentially supports lots of things, but it depends on
3103 * what devices are offered (and maybe kernel version?)
3104 * If given unused devices, we will make a container.
3105 * If given devices in a container, we will make a BVD.
3106 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
3107 */
3108
bb7295f1
N
3109 if (chunk && *chunk == UnSet)
3110 *chunk = DEFAULT_CHUNK;
3111
542ef4ec 3112 if (level == -1000000) level = LEVEL_CONTAINER;
a322f70c 3113 if (level == LEVEL_CONTAINER) {
78e44928
NB
3114 /* Must be a fresh device to add to a container */
3115 return validate_geometry_ddf_container(st, level, layout,
c21e737b 3116 raiddisks, chunk?*chunk:0,
af4348dd
N
3117 size, data_offset, dev,
3118 freesize,
2c514b71 3119 verbose);
5f8097be
NB
3120 }
3121
78e44928 3122 if (!dev) {
a3163bf0 3123 mdu_array_info_t array = {
3124 .level = level, .layout = layout,
3125 .raid_disks = raiddisks
3126 };
3127 struct vd_config conf;
3128 if (layout_md2ddf(&array, &conf) == -1) {
b42f577a 3129 if (verbose)
94b08b7c 3130 pr_err("DDF does not support level %d /layout %d arrays with %d disks\n",
3131 level, layout, raiddisks);
78e44928 3132 return 0;
b42f577a 3133 }
78e44928 3134 /* Should check layout? etc */
8592f29d
N
3135
3136 if (st->sb && freesize) {
3137 /* --create was given a container to create in.
3138 * So we need to check that there are enough
3139 * free spaces and return the amount of space.
3140 * We may as well remember which drives were
3141 * chosen so that add_to_super/getinfo_super
3142 * can return them.
3143 */
c21e737b 3144 return reserve_space(st, raiddisks, size, chunk?*chunk:0, freesize);
8592f29d 3145 }
a322f70c 3146 return 1;
78e44928 3147 }
a322f70c 3148
8592f29d
N
3149 if (st->sb) {
3150 /* A container has already been opened, so we are
3151 * creating in there. Maybe a BVD, maybe an SVD.
3152 * Should make a distinction one day.
3153 */
3154 return validate_geometry_ddf_bvd(st, level, layout, raiddisks,
af4348dd
N
3155 chunk, size, data_offset, dev,
3156 freesize,
8592f29d
N
3157 verbose);
3158 }
78e44928
NB
3159 /* This is the first device for the array.
3160 * If it is a container, we read it in and do automagic allocations,
3161 * no other devices should be given.
3162 * Otherwise it must be a member device of a container, and we
3163 * do manual allocation.
3164 * Later we should check for a BVD and make an SVD.
a322f70c 3165 */
a322f70c
DW
3166 fd = open(dev, O_RDONLY|O_EXCL, 0);
3167 if (fd >= 0) {
4dd2df09 3168 sra = sysfs_read(fd, NULL, GET_VERSION);
a322f70c
DW
3169 close(fd);
3170 if (sra && sra->array.major_version == -1 &&
78e44928
NB
3171 strcmp(sra->text_version, "ddf") == 0) {
3172
3173 /* load super */
3174 /* find space for 'n' devices. */
3175 /* remember the devices */
3176 /* Somehow return the fact that we have enough */
a322f70c
DW
3177 }
3178
2c514b71 3179 if (verbose)
e7b84f9d
N
3180 pr_err("ddf: Cannot create this array "
3181 "on device %s - a container is required.\n",
3182 dev);
a322f70c
DW
3183 return 0;
3184 }
3185 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2c514b71 3186 if (verbose)
e7b84f9d 3187 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 3188 dev, strerror(errno));
a322f70c
DW
3189 return 0;
3190 }
3191 /* Well, it is in use by someone, maybe a 'ddf' container. */
3192 cfd = open_container(fd);
3193 if (cfd < 0) {
3194 close(fd);
2c514b71 3195 if (verbose)
e7b84f9d 3196 pr_err("ddf: Cannot use %s: %s\n",
613b0d17 3197 dev, strerror(EBUSY));
a322f70c
DW
3198 return 0;
3199 }
4dd2df09 3200 sra = sysfs_read(cfd, NULL, GET_VERSION);
a322f70c
DW
3201 close(fd);
3202 if (sra && sra->array.major_version == -1 &&
3203 strcmp(sra->text_version, "ddf") == 0) {
3204 /* This is a member of a ddf container. Load the container
3205 * and try to create a bvd
3206 */
3207 struct ddf_super *ddf;
e1902a7b 3208 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL) == 0) {
5f8097be 3209 st->sb = ddf;
4dd2df09 3210 strcpy(st->container_devnm, fd2devnm(cfd));
a322f70c 3211 close(cfd);
78e44928 3212 return validate_geometry_ddf_bvd(st, level, layout,
a322f70c 3213 raiddisks, chunk, size,
af4348dd 3214 data_offset,
2c514b71
NB
3215 dev, freesize,
3216 verbose);
a322f70c
DW
3217 }
3218 close(cfd);
c42ec1ed
DW
3219 } else /* device may belong to a different container */
3220 return 0;
3221
a322f70c
DW
3222 return 1;
3223}
3224
2c514b71
NB
3225static int
3226validate_geometry_ddf_container(struct supertype *st,
3227 int level, int layout, int raiddisks,
3228 int chunk, unsigned long long size,
af4348dd 3229 unsigned long long data_offset,
2c514b71
NB
3230 char *dev, unsigned long long *freesize,
3231 int verbose)
a322f70c
DW
3232{
3233 int fd;
3234 unsigned long long ldsize;
3235
3236 if (level != LEVEL_CONTAINER)
3237 return 0;
3238 if (!dev)
3239 return 1;
3240
3241 fd = open(dev, O_RDONLY|O_EXCL, 0);
3242 if (fd < 0) {
2c514b71 3243 if (verbose)
e7b84f9d 3244 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 3245 dev, strerror(errno));
a322f70c
DW
3246 return 0;
3247 }
3248 if (!get_dev_size(fd, dev, &ldsize)) {
3249 close(fd);
3250 return 0;
3251 }
3252 close(fd);
3253
387fcd59 3254 *freesize = avail_size_ddf(st, ldsize >> 9, INVALID_SECTORS);
ea17e7aa
N
3255 if (*freesize == 0)
3256 return 0;
a322f70c
DW
3257
3258 return 1;
3259}
3260
78e44928
NB
3261static int validate_geometry_ddf_bvd(struct supertype *st,
3262 int level, int layout, int raiddisks,
c21e737b 3263 int *chunk, unsigned long long size,
af4348dd 3264 unsigned long long data_offset,
2c514b71
NB
3265 char *dev, unsigned long long *freesize,
3266 int verbose)
a322f70c
DW
3267{
3268 struct stat stb;
3269 struct ddf_super *ddf = st->sb;
3270 struct dl *dl;
5f8097be
NB
3271 unsigned long long pos = 0;
3272 unsigned long long maxsize;
3273 struct extent *e;
3274 int i;
a322f70c 3275 /* ddf/bvd supports lots of things, but not containers */
b42f577a
N
3276 if (level == LEVEL_CONTAINER) {
3277 if (verbose)
e7b84f9d 3278 pr_err("DDF cannot create a container within an container\n");
a322f70c 3279 return 0;
b42f577a 3280 }
a322f70c
DW
3281 /* We must have the container info already read in. */
3282 if (!ddf)
3283 return 0;
3284
5f8097be
NB
3285 if (!dev) {
3286 /* General test: make sure there is space for
3287 * 'raiddisks' device extents of size 'size'.
3288 */
3289 unsigned long long minsize = size;
3290 int dcnt = 0;
3291 if (minsize == 0)
3292 minsize = 8;
3293 for (dl = ddf->dlist; dl ; dl = dl->next)
3294 {
3295 int found = 0;
7e1432fb 3296 pos = 0;
5f8097be
NB
3297
3298 i = 0;
3299 e = get_extents(ddf, dl);
3300 if (!e) continue;
3301 do {
3302 unsigned long long esize;
3303 esize = e[i].start - pos;
3304 if (esize >= minsize)
3305 found = 1;
3306 pos = e[i].start + e[i].size;
3307 i++;
3308 } while (e[i-1].size);
3309 if (found)
3310 dcnt++;
3311 free(e);
3312 }
3313 if (dcnt < raiddisks) {
2c514b71 3314 if (verbose)
e7b84f9d
N
3315 pr_err("ddf: Not enough devices with "
3316 "space for this array (%d < %d)\n",
3317 dcnt, raiddisks);
5f8097be
NB
3318 return 0;
3319 }
3320 return 1;
3321 }
a322f70c
DW
3322 /* This device must be a member of the set */
3323 if (stat(dev, &stb) < 0)
3324 return 0;
3325 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3326 return 0;
3327 for (dl = ddf->dlist ; dl ; dl = dl->next) {
f21e18ca
N
3328 if (dl->major == (int)major(stb.st_rdev) &&
3329 dl->minor == (int)minor(stb.st_rdev))
a322f70c
DW
3330 break;
3331 }
5f8097be 3332 if (!dl) {
2c514b71 3333 if (verbose)
e7b84f9d 3334 pr_err("ddf: %s is not in the "
613b0d17
N
3335 "same DDF set\n",
3336 dev);
5f8097be
NB
3337 return 0;
3338 }
3339 e = get_extents(ddf, dl);
3340 maxsize = 0;
3341 i = 0;
3342 if (e) do {
613b0d17
N
3343 unsigned long long esize;
3344 esize = e[i].start - pos;
3345 if (esize >= maxsize)
3346 maxsize = esize;
3347 pos = e[i].start + e[i].size;
3348 i++;
3349 } while (e[i-1].size);
5f8097be 3350 *freesize = maxsize;
a322f70c
DW
3351 // FIXME here I am
3352
3353 return 1;
3354}
59e36268 3355
a322f70c 3356static int load_super_ddf_all(struct supertype *st, int fd,
e1902a7b 3357 void **sbp, char *devname)
a322f70c
DW
3358{
3359 struct mdinfo *sra;
3360 struct ddf_super *super;
3361 struct mdinfo *sd, *best = NULL;
3362 int bestseq = 0;
3363 int seq;
3364 char nm[20];
3365 int dfd;
3366
b526e52d 3367 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
a322f70c
DW
3368 if (!sra)
3369 return 1;
3370 if (sra->array.major_version != -1 ||
3371 sra->array.minor_version != -2 ||
3372 strcmp(sra->text_version, "ddf") != 0)
3373 return 1;
3374
6416d527 3375 if (posix_memalign((void**)&super, 512, sizeof(*super)) != 0)
a322f70c 3376 return 1;
a2349791 3377 memset(super, 0, sizeof(*super));
a322f70c
DW
3378
3379 /* first, try each device, and choose the best ddf */
3380 for (sd = sra->devs ; sd ; sd = sd->next) {
3381 int rv;
3382 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
7a7cc504
NB
3383 dfd = dev_open(nm, O_RDONLY);
3384 if (dfd < 0)
a322f70c
DW
3385 return 2;
3386 rv = load_ddf_headers(dfd, super, NULL);
7a7cc504 3387 close(dfd);
a322f70c
DW
3388 if (rv == 0) {
3389 seq = __be32_to_cpu(super->active->seq);
3390 if (super->active->openflag)
3391 seq--;
3392 if (!best || seq > bestseq) {
3393 bestseq = seq;
3394 best = sd;
3395 }
3396 }
3397 }
3398 if (!best)
3399 return 1;
3400 /* OK, load this ddf */
3401 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
3402 dfd = dev_open(nm, O_RDONLY);
7a7cc504 3403 if (dfd < 0)
a322f70c
DW
3404 return 1;
3405 load_ddf_headers(dfd, super, NULL);
3406 load_ddf_global(dfd, super, NULL);
3407 close(dfd);
3408 /* Now we need the device-local bits */
3409 for (sd = sra->devs ; sd ; sd = sd->next) {
3d2c4fc7
DW
3410 int rv;
3411
a322f70c 3412 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
e1902a7b 3413 dfd = dev_open(nm, O_RDWR);
7a7cc504 3414 if (dfd < 0)
a322f70c 3415 return 2;
3d2c4fc7
DW
3416 rv = load_ddf_headers(dfd, super, NULL);
3417 if (rv == 0)
e1902a7b 3418 rv = load_ddf_local(dfd, super, NULL, 1);
3d2c4fc7
DW
3419 if (rv)
3420 return 1;
a322f70c 3421 }
33414a01 3422
a322f70c
DW
3423 *sbp = super;
3424 if (st->ss == NULL) {
78e44928 3425 st->ss = &super_ddf;
a322f70c
DW
3426 st->minor_version = 0;
3427 st->max_devs = 512;
3428 }
4dd2df09 3429 strcpy(st->container_devnm, fd2devnm(fd));
a322f70c
DW
3430 return 0;
3431}
2b959fbf
N
3432
3433static int load_container_ddf(struct supertype *st, int fd,
3434 char *devname)
3435{
3436 return load_super_ddf_all(st, fd, &st->sb, devname);
3437}
3438
0e600426 3439#endif /* MDASSEMBLE */
a322f70c 3440
a5c7adb3 3441static int check_secondary(const struct vcl *vc)
3442{
3443 const struct vd_config *conf = &vc->conf;
3444 int i;
3445
3446 /* The only DDF secondary RAID level md can support is
3447 * RAID 10, if the stripe sizes and Basic volume sizes
3448 * are all equal.
3449 * Other configurations could in theory be supported by exposing
3450 * the BVDs to user space and using device mapper for the secondary
3451 * mapping. So far we don't support that.
3452 */
3453
3454 __u64 sec_elements[4] = {0, 0, 0, 0};
3455#define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3456#define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3457
3458 if (vc->other_bvds == NULL) {
3459 pr_err("No BVDs for secondary RAID found\n");
3460 return -1;
3461 }
3462 if (conf->prl != DDF_RAID1) {
3463 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3464 return -1;
3465 }
3466 if (conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED) {
3467 pr_err("Secondary RAID level %d is unsupported\n",
3468 conf->srl);
3469 return -1;
3470 }
3471 __set_sec_seen(conf->sec_elmnt_seq);
3472 for (i = 0; i < conf->sec_elmnt_count-1; i++) {
3473 const struct vd_config *bvd = vc->other_bvds[i];
3c48f7be 3474 if (bvd->sec_elmnt_seq == DDF_UNUSED_BVD)
c98567ba 3475 continue;
a5c7adb3 3476 if (bvd->srl != conf->srl) {
3477 pr_err("Inconsistent secondary RAID level across BVDs\n");
3478 return -1;
3479 }
3480 if (bvd->prl != conf->prl) {
3481 pr_err("Different RAID levels for BVDs are unsupported\n");
3482 return -1;
3483 }
3484 if (bvd->prim_elmnt_count != conf->prim_elmnt_count) {
3485 pr_err("All BVDs must have the same number of primary elements\n");
3486 return -1;
3487 }
3488 if (bvd->chunk_shift != conf->chunk_shift) {
3489 pr_err("Different strip sizes for BVDs are unsupported\n");
3490 return -1;
3491 }
3492 if (bvd->array_blocks != conf->array_blocks) {
3493 pr_err("Different BVD sizes are unsupported\n");
3494 return -1;
3495 }
3496 __set_sec_seen(bvd->sec_elmnt_seq);
3497 }
3498 for (i = 0; i < conf->sec_elmnt_count; i++) {
3499 if (!__was_sec_seen(i)) {
3500 pr_err("BVD %d is missing\n", i);
3501 return -1;
3502 }
3503 }
3504 return 0;
3505}
3506
8a38db86 3507static unsigned int get_pd_index_from_refnum(const struct vcl *vc,
4e587018 3508 __u32 refnum, unsigned int nmax,
3509 const struct vd_config **bvd,
3510 unsigned int *idx)
8a38db86 3511{
4e587018 3512 unsigned int i, j, n, sec, cnt;
3513
3514 cnt = __be16_to_cpu(vc->conf.prim_elmnt_count);
3515 sec = (vc->conf.sec_elmnt_count == 1 ? 0 : vc->conf.sec_elmnt_seq);
3516
3517 for (i = 0, j = 0 ; i < nmax ; i++) {
3518 /* j counts valid entries for this BVD */
3519 if (vc->conf.phys_refnum[i] != 0xffffffff)
3520 j++;
3521 if (vc->conf.phys_refnum[i] == refnum) {
3522 *bvd = &vc->conf;
3523 *idx = i;
3524 return sec * cnt + j - 1;
3525 }
3526 }
3527 if (vc->other_bvds == NULL)
3528 goto bad;
3529
3530 for (n = 1; n < vc->conf.sec_elmnt_count; n++) {
3531 struct vd_config *vd = vc->other_bvds[n-1];
4e587018 3532 sec = vd->sec_elmnt_seq;
3c48f7be 3533 if (sec == DDF_UNUSED_BVD)
3534 continue;
4e587018 3535 for (i = 0, j = 0 ; i < nmax ; i++) {
3536 if (vd->phys_refnum[i] != 0xffffffff)
3537 j++;
3538 if (vd->phys_refnum[i] == refnum) {
3539 *bvd = vd;
3540 *idx = i;
3541 return sec * cnt + j - 1;
3542 }
3543 }
3544 }
3545bad:
3546 *bvd = NULL;
d6e7b083 3547 return DDF_NOTFOUND;
8a38db86 3548}
3549
00bbdbda 3550static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray)
598f0d58
NB
3551{
3552 /* Given a container loaded by load_super_ddf_all,
3553 * extract information about all the arrays into
3554 * an mdinfo tree.
3555 *
3556 * For each vcl in conflist: create an mdinfo, fill it in,
3557 * then look for matching devices (phys_refnum) in dlist
3558 * and create appropriate device mdinfo.
3559 */
3560 struct ddf_super *ddf = st->sb;
3561 struct mdinfo *rest = NULL;
3562 struct vcl *vc;
3563
3564 for (vc = ddf->conflist ; vc ; vc=vc->next)
3565 {
f21e18ca
N
3566 unsigned int i;
3567 unsigned int j;
598f0d58 3568 struct mdinfo *this;
00bbdbda 3569 char *ep;
90fa1a29 3570 __u32 *cptr;
8a38db86 3571 unsigned int pd;
00bbdbda
N
3572
3573 if (subarray &&
3574 (strtoul(subarray, &ep, 10) != vc->vcnum ||
3575 *ep != '\0'))
3576 continue;
3577
a5c7adb3 3578 if (vc->conf.sec_elmnt_count > 1) {
3579 if (check_secondary(vc) != 0)
3580 continue;
3581 }
3582
503975b9 3583 this = xcalloc(1, sizeof(*this));
598f0d58
NB
3584 this->next = rest;
3585 rest = this;
3586
8a2848a7 3587 if (layout_ddf2md(&vc->conf, &this->array))
3588 continue;
598f0d58 3589 this->array.md_minor = -1;
f35f2525
N
3590 this->array.major_version = -1;
3591 this->array.minor_version = -2;
90fa1a29
JS
3592 cptr = (__u32 *)(vc->conf.guid + 16);
3593 this->array.ctime = DECADE + __be32_to_cpu(*cptr);
598f0d58
NB
3594 this->array.utime = DECADE +
3595 __be32_to_cpu(vc->conf.timestamp);
3596 this->array.chunk_size = 512 << vc->conf.chunk_shift;
3597
59e36268 3598 i = vc->vcnum;
7a7cc504
NB
3599 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
3600 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
ed9d66aa 3601 DDF_init_full) {
598f0d58 3602 this->array.state = 0;
ed9d66aa
NB
3603 this->resync_start = 0;
3604 } else {
598f0d58 3605 this->array.state = 1;
b7528a20 3606 this->resync_start = MaxSector;
ed9d66aa 3607 }
db42fa9b
N
3608 memcpy(this->name, ddf->virt->entries[i].name, 16);
3609 this->name[16]=0;
3610 for(j=0; j<16; j++)
3611 if (this->name[j] == ' ')
3612 this->name[j] = 0;
598f0d58
NB
3613
3614 memset(this->uuid, 0, sizeof(this->uuid));
3615 this->component_size = __be64_to_cpu(vc->conf.blocks);
3616 this->array.size = this->component_size / 2;
5f2aace8 3617 this->container_member = i;
598f0d58 3618
c5afc314
N
3619 ddf->currentconf = vc;
3620 uuid_from_super_ddf(st, this->uuid);
f646805e 3621 if (!subarray)
3622 ddf->currentconf = NULL;
c5afc314 3623
60f18132 3624 sprintf(this->text_version, "/%s/%d",
4dd2df09 3625 st->container_devnm, this->container_member);
60f18132 3626
8a38db86 3627 for (pd = 0; pd < __be16_to_cpu(ddf->phys->used_pdes); pd++) {
598f0d58
NB
3628 struct mdinfo *dev;
3629 struct dl *d;
4e587018 3630 const struct vd_config *bvd;
3631 unsigned int iphys;
fa033bec 3632 int stt;
598f0d58 3633
8a38db86 3634 if (ddf->phys->entries[pd].refnum == 0xFFFFFFFF)
bc17324f 3635 continue;
0cf5ef67
N
3636
3637 stt = __be16_to_cpu(ddf->phys->entries[pd].state);
fa033bec
N
3638 if ((stt & (DDF_Online|DDF_Failed|DDF_Rebuilding))
3639 != DDF_Online)
3640 continue;
3641
8a38db86 3642 i = get_pd_index_from_refnum(
4e587018 3643 vc, ddf->phys->entries[pd].refnum,
3644 ddf->mppe, &bvd, &iphys);
d6e7b083 3645 if (i == DDF_NOTFOUND)
8a38db86 3646 continue;
3647
fa033bec 3648 this->array.working_disks++;
bc17324f 3649
0cf5ef67 3650 for (d = ddf->dlist; d ; d=d->next)
8a38db86 3651 if (d->disk.refnum ==
3652 ddf->phys->entries[pd].refnum)
0cf5ef67
N
3653 break;
3654 if (d == NULL)
3655 /* Haven't found that one yet, maybe there are others */
3656 continue;
3657
503975b9 3658 dev = xcalloc(1, sizeof(*dev));
598f0d58
NB
3659 dev->next = this->devs;
3660 this->devs = dev;
3661
3662 dev->disk.number = __be32_to_cpu(d->disk.refnum);
3663 dev->disk.major = d->major;
3664 dev->disk.minor = d->minor;
3665 dev->disk.raid_disk = i;
3666 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
d23534e4 3667 dev->recovery_start = MaxSector;
598f0d58 3668
120f7677 3669 dev->events = __be32_to_cpu(ddf->primary.seq);
57a66662 3670 dev->data_offset =
3671 __be64_to_cpu(LBA_OFFSET(ddf, bvd)[iphys]);
4e587018 3672 dev->component_size = __be64_to_cpu(bvd->blocks);
598f0d58
NB
3673 if (d->devname)
3674 strcpy(dev->name, d->devname);
3675 }
3676 }
3677 return rest;
3678}
3679
955e9ea1 3680static int store_super_ddf(struct supertype *st, int fd)
a322f70c 3681{
955e9ea1 3682 struct ddf_super *ddf = st->sb;
a322f70c 3683 unsigned long long dsize;
6416d527 3684 void *buf;
3d2c4fc7 3685 int rc;
a322f70c 3686
955e9ea1
DW
3687 if (!ddf)
3688 return 1;
3689
a322f70c
DW
3690 if (!get_dev_size(fd, NULL, &dsize))
3691 return 1;
3692
dbf98368 3693 if (ddf->dlist || ddf->conflist) {
3694 struct stat sta;
3695 struct dl *dl;
3696 int ofd, ret;
3697
3698 if (fstat(fd, &sta) == -1 || !S_ISBLK(sta.st_mode)) {
3699 pr_err("%s: file descriptor for invalid device\n",
3700 __func__);
3701 return 1;
3702 }
3703 for (dl = ddf->dlist; dl; dl = dl->next)
3704 if (dl->major == (int)major(sta.st_rdev) &&
3705 dl->minor == (int)minor(sta.st_rdev))
3706 break;
3707 if (!dl) {
3708 pr_err("%s: couldn't find disk %d/%d\n", __func__,
3709 (int)major(sta.st_rdev),
3710 (int)minor(sta.st_rdev));
3711 return 1;
3712 }
3713 /*
3714 For DDF, writing to just one disk makes no sense.
3715 We would run the risk of writing inconsistent meta data
3716 to the devices. So just call __write_init_super_ddf and
3717 write to all devices, including this one.
3718 Use the fd passed to this function, just in case dl->fd
3719 is invalid.
3720 */
3721 ofd = dl->fd;
3722 dl->fd = fd;
3723 ret = __write_init_super_ddf(st);
3724 dl->fd = ofd;
3725 return ret;
3726 }
3727
3d2c4fc7
DW
3728 if (posix_memalign(&buf, 512, 512) != 0)
3729 return 1;
6416d527
NB
3730 memset(buf, 0, 512);
3731
a322f70c 3732 lseek64(fd, dsize-512, 0);
3d2c4fc7 3733 rc = write(fd, buf, 512);
6416d527 3734 free(buf);
3d2c4fc7
DW
3735 if (rc < 0)
3736 return 1;
a322f70c
DW
3737 return 0;
3738}
3739
a19c88b8
NB
3740static int compare_super_ddf(struct supertype *st, struct supertype *tst)
3741{
3742 /*
3743 * return:
3744 * 0 same, or first was empty, and second was copied
3745 * 1 second had wrong number
3746 * 2 wrong uuid
3747 * 3 wrong other info
3748 */
3749 struct ddf_super *first = st->sb;
3750 struct ddf_super *second = tst->sb;
4eefd651 3751 struct dl *dl1, *dl2;
3752 struct vcl *vl1, *vl2;
2d210697 3753 unsigned int max_vds, max_pds, pd, vd;
a19c88b8
NB
3754
3755 if (!first) {
3756 st->sb = tst->sb;
3757 tst->sb = NULL;
3758 return 0;
3759 }
3760
3761 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
3762 return 2;
3763
2d210697 3764 if (first->anchor.seq != second->anchor.seq) {
3765 dprintf("%s: sequence number mismatch %u/%u\n", __func__,
3766 __be32_to_cpu(first->anchor.seq),
3767 __be32_to_cpu(second->anchor.seq));
3768 return 3;
3769 }
3770 if (first->max_part != second->max_part ||
3771 first->phys->used_pdes != second->phys->used_pdes ||
3772 first->virt->populated_vdes != second->virt->populated_vdes) {
3773 dprintf("%s: PD/VD number mismatch\n", __func__);
3774 return 3;
3775 }
3776
3777 max_pds = __be16_to_cpu(first->phys->used_pdes);
3778 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3779 for (pd = 0; pd < max_pds; pd++)
3780 if (first->phys->entries[pd].refnum == dl2->disk.refnum)
3781 break;
3782 if (pd == max_pds) {
3783 dprintf("%s: no match for disk %08x\n", __func__,
3784 __be32_to_cpu(dl2->disk.refnum));
3785 return 3;
3786 }
3787 }
3788
3789 max_vds = __be16_to_cpu(first->active->max_vd_entries);
3790 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3791 if (vl2->conf.magic != DDF_VD_CONF_MAGIC)
3792 continue;
3793 for (vd = 0; vd < max_vds; vd++)
3794 if (!memcmp(first->virt->entries[vd].guid,
3795 vl2->conf.guid, DDF_GUID_LEN))
3796 break;
3797 if (vd == max_vds) {
3798 dprintf("%s: no match for VD config\n", __func__);
3799 return 3;
3800 }
3801 }
a19c88b8 3802 /* FIXME should I look at anything else? */
2d210697 3803
4eefd651 3804 /*
3805 At this point we are fairly sure that the meta data matches.
3806 But the new disk may contain additional local data.
3807 Add it to the super block.
3808 */
3809 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3810 for (vl1 = first->conflist; vl1; vl1 = vl1->next)
3811 if (!memcmp(vl1->conf.guid, vl2->conf.guid,
3812 DDF_GUID_LEN))
3813 break;
3814 if (vl1) {
3815 if (vl1->other_bvds != NULL &&
3816 vl1->conf.sec_elmnt_seq !=
3817 vl2->conf.sec_elmnt_seq) {
3818 dprintf("%s: adding BVD %u\n", __func__,
3819 vl2->conf.sec_elmnt_seq);
3820 add_other_bvd(vl1, &vl2->conf,
3821 first->conf_rec_len*512);
3822 }
3823 continue;
3824 }
3825
3826 if (posix_memalign((void **)&vl1, 512,
3827 (first->conf_rec_len*512 +
3828 offsetof(struct vcl, conf))) != 0) {
3829 pr_err("%s could not allocate vcl buf\n",
3830 __func__);
3831 return 3;
3832 }
3833
3834 vl1->next = first->conflist;
3835 vl1->block_sizes = NULL;
4eefd651 3836 memcpy(&vl1->conf, &vl2->conf, first->conf_rec_len*512);
3c48f7be 3837 if (alloc_other_bvds(first, vl1) != 0) {
3838 pr_err("%s could not allocate other bvds\n",
3839 __func__);
3840 free(vl1);
3841 return 3;
3842 }
4eefd651 3843 for (vd = 0; vd < max_vds; vd++)
3844 if (!memcmp(first->virt->entries[vd].guid,
3845 vl1->conf.guid, DDF_GUID_LEN))
3846 break;
3847 vl1->vcnum = vd;
3848 dprintf("%s: added config for VD %u\n", __func__, vl1->vcnum);
3849 first->conflist = vl1;
3850 }
3851
3852 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3853 for (dl1 = first->dlist; dl1; dl1 = dl1->next)
3854 if (dl1->disk.refnum == dl2->disk.refnum)
3855 break;
3856 if (dl1)
3857 continue;
3858
3859 if (posix_memalign((void **)&dl1, 512,
3860 sizeof(*dl1) + (first->max_part) * sizeof(dl1->vlist[0]))
3861 != 0) {
3862 pr_err("%s could not allocate disk info buffer\n",
3863 __func__);
3864 return 3;
3865 }
3866 memcpy(dl1, dl2, sizeof(*dl1));
3867 dl1->mdupdate = NULL;
3868 dl1->next = first->dlist;
3869 dl1->fd = -1;
3870 for (pd = 0; pd < max_pds; pd++)
3871 if (first->phys->entries[pd].refnum == dl1->disk.refnum)
3872 break;
3873 dl1->pdnum = pd;
3874 if (dl2->spare) {
3875 if (posix_memalign((void **)&dl1->spare, 512,
3876 first->conf_rec_len*512) != 0) {
3877 pr_err("%s could not allocate spare info buf\n",
3878 __func__);
3879 return 3;
3880 }
3881 memcpy(dl1->spare, dl2->spare, first->conf_rec_len*512);
3882 }
3883 for (vd = 0 ; vd < first->max_part ; vd++) {
3884 if (!dl2->vlist[vd]) {
3885 dl1->vlist[vd] = NULL;
3886 continue;
3887 }
3888 for (vl1 = first->conflist; vl1; vl1 = vl1->next) {
3889 if (!memcmp(vl1->conf.guid,
3890 dl2->vlist[vd]->conf.guid,
3891 DDF_GUID_LEN))
3892 break;
3893 dl1->vlist[vd] = vl1;
3894 }
3895 }
3896 first->dlist = dl1;
3897 dprintf("%s: added disk %d: %08x\n", __func__, dl1->pdnum,
ad60eea1 3898 __be32_to_cpu(dl1->disk.refnum));
4eefd651 3899 }
3900
a19c88b8
NB
3901 return 0;
3902}
3903
0e600426 3904#ifndef MDASSEMBLE
4e5528c6
NB
3905/*
3906 * A new array 'a' has been started which claims to be instance 'inst'
3907 * within container 'c'.
3908 * We need to confirm that the array matches the metadata in 'c' so
3909 * that we don't corrupt any metadata.
3910 */
cba0191b 3911static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst)
549e9569 3912{
a2aa439e 3913 struct ddf_super *ddf = c->sb;
3914 int n = atoi(inst);
fb9d0acb 3915 if (all_ff(ddf->virt->entries[n].guid)) {
3916 pr_err("%s: subarray %d doesn't exist\n", __func__, n);
a2aa439e 3917 return -ENODEV;
3918 }
3919 dprintf("ddf: open_new %d\n", n);
3920 a->info.container_member = n;
549e9569
NB
3921 return 0;
3922}
3923
4e5528c6
NB
3924/*
3925 * The array 'a' is to be marked clean in the metadata.
ed9d66aa 3926 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
4e5528c6
NB
3927 * clean up to the point (in sectors). If that cannot be recorded in the
3928 * metadata, then leave it as dirty.
3929 *
3930 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3931 * !global! virtual_disk.virtual_entry structure.
3932 */
01f157d7 3933static int ddf_set_array_state(struct active_array *a, int consistent)
549e9569 3934{
4e5528c6
NB
3935 struct ddf_super *ddf = a->container->sb;
3936 int inst = a->info.container_member;
18a2f463 3937 int old = ddf->virt->entries[inst].state;
01f157d7
N
3938 if (consistent == 2) {
3939 /* Should check if a recovery should be started FIXME */
3940 consistent = 1;
b7941fd6 3941 if (!is_resync_complete(&a->info))
01f157d7
N
3942 consistent = 0;
3943 }
ed9d66aa
NB
3944 if (consistent)
3945 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
3946 else
4e5528c6 3947 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
18a2f463 3948 if (old != ddf->virt->entries[inst].state)
7d5a7ff3 3949 ddf_set_updates_pending(ddf);
18a2f463
NB
3950
3951 old = ddf->virt->entries[inst].init_state;
ed9d66aa 3952 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
b7941fd6 3953 if (is_resync_complete(&a->info))
ed9d66aa 3954 ddf->virt->entries[inst].init_state |= DDF_init_full;
b7941fd6 3955 else if (a->info.resync_start == 0)
ed9d66aa 3956 ddf->virt->entries[inst].init_state |= DDF_init_not;
4e5528c6 3957 else
ed9d66aa 3958 ddf->virt->entries[inst].init_state |= DDF_init_quick;
18a2f463 3959 if (old != ddf->virt->entries[inst].init_state)
7d5a7ff3 3960 ddf_set_updates_pending(ddf);
ed9d66aa 3961
b27336a2 3962 dprintf("ddf mark %d/%s (%d) %s %llu\n", inst,
3963 guid_str(ddf->virt->entries[inst].guid), a->curr_state,
3964 consistent?"clean":"dirty",
b7941fd6 3965 a->info.resync_start);
01f157d7 3966 return consistent;
fd7cde1b
DW
3967}
3968
5ec636b7 3969static int get_bvd_state(const struct ddf_super *ddf,
3970 const struct vd_config *vc)
3971{
3972 unsigned int i, n_bvd, working = 0;
3973 unsigned int n_prim = __be16_to_cpu(vc->prim_elmnt_count);
3974 int pd, st, state;
3975 for (i = 0; i < n_prim; i++) {
3976 if (!find_index_in_bvd(ddf, vc, i, &n_bvd))
3977 continue;
3978 pd = find_phys(ddf, vc->phys_refnum[n_bvd]);
3979 if (pd < 0)
3980 continue;
3981 st = __be16_to_cpu(ddf->phys->entries[pd].state);
3982 if ((st & (DDF_Online|DDF_Failed|DDF_Rebuilding))
3983 == DDF_Online)
3984 working++;
3985 }
3986
3987 state = DDF_state_degraded;
3988 if (working == n_prim)
3989 state = DDF_state_optimal;
3990 else
3991 switch (vc->prl) {
3992 case DDF_RAID0:
3993 case DDF_CONCAT:
3994 case DDF_JBOD:
3995 state = DDF_state_failed;
3996 break;
3997 case DDF_RAID1:
3998 if (working == 0)
3999 state = DDF_state_failed;
4000 else if (working >= 2)
4001 state = DDF_state_part_optimal;
4002 break;
4003 case DDF_RAID4:
4004 case DDF_RAID5:
4005 if (working < n_prim - 1)
4006 state = DDF_state_failed;
4007 break;
4008 case DDF_RAID6:
4009 if (working < n_prim - 2)
4010 state = DDF_state_failed;
4011 else if (working == n_prim - 1)
4012 state = DDF_state_part_optimal;
4013 break;
4014 }
4015 return state;
4016}
4017
0777d17d 4018static int secondary_state(int state, int other, int seclevel)
4019{
4020 if (state == DDF_state_optimal && other == DDF_state_optimal)
4021 return DDF_state_optimal;
4022 if (seclevel == DDF_2MIRRORED) {
4023 if (state == DDF_state_optimal || other == DDF_state_optimal)
4024 return DDF_state_part_optimal;
4025 if (state == DDF_state_failed && other == DDF_state_failed)
4026 return DDF_state_failed;
4027 return DDF_state_degraded;
4028 } else {
4029 if (state == DDF_state_failed || other == DDF_state_failed)
4030 return DDF_state_failed;
4031 if (state == DDF_state_degraded || other == DDF_state_degraded)
4032 return DDF_state_degraded;
4033 return DDF_state_part_optimal;
4034 }
4035}
4036
4037static int get_svd_state(const struct ddf_super *ddf, const struct vcl *vcl)
4038{
4039 int state = get_bvd_state(ddf, &vcl->conf);
4040 unsigned int i;
4041 for (i = 1; i < vcl->conf.sec_elmnt_count; i++) {
4042 state = secondary_state(
4043 state,
4044 get_bvd_state(ddf, vcl->other_bvds[i-1]),
4045 vcl->conf.srl);
4046 }
4047 return state;
4048}
4049
7a7cc504
NB
4050/*
4051 * The state of each disk is stored in the global phys_disk structure
4052 * in phys_disk.entries[n].state.
4053 * This makes various combinations awkward.
4054 * - When a device fails in any array, it must be failed in all arrays
4055 * that include a part of this device.
4056 * - When a component is rebuilding, we cannot include it officially in the
4057 * array unless this is the only array that uses the device.
4058 *
4059 * So: when transitioning:
4060 * Online -> failed, just set failed flag. monitor will propagate
4061 * spare -> online, the device might need to be added to the array.
4062 * spare -> failed, just set failed. Don't worry if in array or not.
4063 */
8d45d196 4064static void ddf_set_disk(struct active_array *a, int n, int state)
549e9569 4065{
7a7cc504 4066 struct ddf_super *ddf = a->container->sb;
baba3f4e 4067 unsigned int inst = a->info.container_member, n_bvd;
4068 struct vcl *vcl;
4069 struct vd_config *vc = find_vdcr(ddf, inst, (unsigned int)n,
4070 &n_bvd, &vcl);
4071 int pd;
e1316fab
N
4072 struct mdinfo *mdi;
4073 struct dl *dl;
7a7cc504
NB
4074
4075 if (vc == NULL) {
2c514b71 4076 dprintf("ddf: cannot find instance %d!!\n", inst);
7a7cc504
NB
4077 return;
4078 }
e1316fab
N
4079 /* Find the matching slot in 'info'. */
4080 for (mdi = a->info.devs; mdi; mdi = mdi->next)
4081 if (mdi->disk.raid_disk == n)
4082 break;
4083 if (!mdi)
4084 return;
4085
4086 /* and find the 'dl' entry corresponding to that. */
4087 for (dl = ddf->dlist; dl; dl = dl->next)
77632af9
N
4088 if (mdi->state_fd >= 0 &&
4089 mdi->disk.major == dl->major &&
e1316fab
N
4090 mdi->disk.minor == dl->minor)
4091 break;
4092 if (!dl)
4093 return;
4094
baba3f4e 4095 pd = find_phys(ddf, vc->phys_refnum[n_bvd]);
e1316fab
N
4096 if (pd < 0 || pd != dl->pdnum) {
4097 /* disk doesn't currently exist or has changed.
4098 * If it is now in_sync, insert it. */
baba3f4e 4099 dprintf("%s: phys disk not found for %d: %d/%d ref %08x\n",
4100 __func__, dl->pdnum, dl->major, dl->minor,
ad60eea1 4101 __be32_to_cpu(dl->disk.refnum));
baba3f4e 4102 dprintf("%s: array %u disk %u ref %08x pd %d\n",
4103 __func__, inst, n_bvd, vc->phys_refnum[n_bvd], pd);
7a7cc504 4104 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
baba3f4e 4105 pd = dl->pdnum; /* FIXME: is this really correct ? */
4106 vc->phys_refnum[n_bvd] = dl->disk.refnum;
57a66662 4107 LBA_OFFSET(ddf, vc)[n_bvd] =
4108 __cpu_to_be64(mdi->data_offset);
e1316fab
N
4109 ddf->phys->entries[pd].type &=
4110 ~__cpu_to_be16(DDF_Global_Spare);
4111 ddf->phys->entries[pd].type |=
4112 __cpu_to_be16(DDF_Active_in_VD);
7d5a7ff3 4113 ddf_set_updates_pending(ddf);
7a7cc504
NB
4114 }
4115 } else {
18a2f463 4116 int old = ddf->phys->entries[pd].state;
7a7cc504
NB
4117 if (state & DS_FAULTY)
4118 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
4119 if (state & DS_INSYNC) {
4120 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
4121 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
4122 }
18a2f463 4123 if (old != ddf->phys->entries[pd].state)
7d5a7ff3 4124 ddf_set_updates_pending(ddf);
7a7cc504
NB
4125 }
4126
2c514b71 4127 dprintf("ddf: set_disk %d to %x\n", n, state);
7e1432fb 4128
7a7cc504
NB
4129 /* Now we need to check the state of the array and update
4130 * virtual_disk.entries[n].state.
4131 * It needs to be one of "optimal", "degraded", "failed".
4132 * I don't understand 'deleted' or 'missing'.
4133 */
0777d17d 4134 state = get_svd_state(ddf, vcl);
7a7cc504 4135
18a2f463
NB
4136 if (ddf->virt->entries[inst].state !=
4137 ((ddf->virt->entries[inst].state & ~DDF_state_mask)
4138 | state)) {
4139
4140 ddf->virt->entries[inst].state =
4141 (ddf->virt->entries[inst].state & ~DDF_state_mask)
4142 | state;
7d5a7ff3 4143 ddf_set_updates_pending(ddf);
18a2f463 4144 }
7a7cc504 4145
549e9569
NB
4146}
4147
2e735d19 4148static void ddf_sync_metadata(struct supertype *st)
549e9569 4149{
7a7cc504
NB
4150
4151 /*
4152 * Write all data to all devices.
4153 * Later, we might be able to track whether only local changes
4154 * have been made, or whether any global data has been changed,
4155 * but ddf is sufficiently weird that it probably always
4156 * changes global data ....
4157 */
18a2f463
NB
4158 struct ddf_super *ddf = st->sb;
4159 if (!ddf->updates_pending)
4160 return;
4161 ddf->updates_pending = 0;
1cc7f4fe 4162 __write_init_super_ddf(st);
2c514b71 4163 dprintf("ddf: sync_metadata\n");
549e9569
NB
4164}
4165
f646805e 4166static int del_from_conflist(struct vcl **list, const char *guid)
4167{
4168 struct vcl **p;
4169 int found = 0;
4170 for (p = list; p && *p; p = &((*p)->next))
4171 if (!memcmp((*p)->conf.guid, guid, DDF_GUID_LEN)) {
4172 found = 1;
4173 *p = (*p)->next;
4174 }
4175 return found;
4176}
4177
4178static int _kill_subarray_ddf(struct ddf_super *ddf, const char *guid)
4179{
4180 struct dl *dl;
4181 unsigned int vdnum, i;
4182 vdnum = find_vde_by_guid(ddf, guid);
4183 if (vdnum == DDF_NOTFOUND) {
4184 pr_err("%s: could not find VD %s\n", __func__,
4185 guid_str(guid));
4186 return -1;
4187 }
4188 if (del_from_conflist(&ddf->conflist, guid) == 0) {
4189 pr_err("%s: could not find conf %s\n", __func__,
4190 guid_str(guid));
4191 return -1;
4192 }
4193 for (dl = ddf->dlist; dl; dl = dl->next)
4194 for (i = 0; i < ddf->max_part; i++)
4195 if (dl->vlist[i] != NULL &&
4196 !memcmp(dl->vlist[i]->conf.guid, guid,
4197 DDF_GUID_LEN))
4198 dl->vlist[i] = NULL;
4199 memset(ddf->virt->entries[vdnum].guid, 0xff, DDF_GUID_LEN);
4200 dprintf("%s: deleted %s\n", __func__, guid_str(guid));
4201 return 0;
4202}
4203
4204static int kill_subarray_ddf(struct supertype *st)
4205{
4206 struct ddf_super *ddf = st->sb;
4207 /*
4208 * currentconf is set in container_content_ddf,
4209 * called with subarray arg
4210 */
4211 struct vcl *victim = ddf->currentconf;
4212 struct vd_config *conf;
4213 ddf->currentconf = NULL;
4214 unsigned int vdnum;
4215 if (!victim) {
4216 pr_err("%s: nothing to kill\n", __func__);
4217 return -1;
4218 }
4219 conf = &victim->conf;
4220 vdnum = find_vde_by_guid(ddf, conf->guid);
4221 if (vdnum == DDF_NOTFOUND) {
4222 pr_err("%s: could not find VD %s\n", __func__,
4223 guid_str(conf->guid));
4224 return -1;
4225 }
4226 if (st->update_tail) {
4227 struct virtual_disk *vd;
4228 int len = sizeof(struct virtual_disk)
4229 + sizeof(struct virtual_entry);
4230 vd = xmalloc(len);
4231 if (vd == NULL) {
4232 pr_err("%s: failed to allocate %d bytes\n", __func__,
4233 len);
4234 return -1;
4235 }
4236 memset(vd, 0 , len);
4237 vd->magic = DDF_VIRT_RECORDS_MAGIC;
4238 vd->populated_vdes = 0;
4239 memcpy(vd->entries[0].guid, conf->guid, DDF_GUID_LEN);
4240 /* we use DDF_state_deleted as marker */
4241 vd->entries[0].state = DDF_state_deleted;
4242 append_metadata_update(st, vd, len);
4243 } else
4244 _kill_subarray_ddf(ddf, conf->guid);
4245 return 0;
4246}
4247
88c164f4
NB
4248static void ddf_process_update(struct supertype *st,
4249 struct metadata_update *update)
4250{
4251 /* Apply this update to the metadata.
4252 * The first 4 bytes are a DDF_*_MAGIC which guides
4253 * our actions.
4254 * Possible update are:
4255 * DDF_PHYS_RECORDS_MAGIC
4dd968cc
N
4256 * Add a new physical device or remove an old one.
4257 * Changes to this record only happen implicitly.
88c164f4
NB
4258 * used_pdes is the device number.
4259 * DDF_VIRT_RECORDS_MAGIC
4260 * Add a new VD. Possibly also change the 'access' bits.
4261 * populated_vdes is the entry number.
4262 * DDF_VD_CONF_MAGIC
4263 * New or updated VD. the VIRT_RECORD must already
4264 * exist. For an update, phys_refnum and lba_offset
4265 * (at least) are updated, and the VD_CONF must
4266 * be written to precisely those devices listed with
4267 * a phys_refnum.
4268 * DDF_SPARE_ASSIGN_MAGIC
4269 * replacement Spare Assignment Record... but for which device?
4270 *
4271 * So, e.g.:
4272 * - to create a new array, we send a VIRT_RECORD and
4273 * a VD_CONF. Then assemble and start the array.
4274 * - to activate a spare we send a VD_CONF to add the phys_refnum
4275 * and offset. This will also mark the spare as active with
4276 * a spare-assignment record.
4277 */
4278 struct ddf_super *ddf = st->sb;
4279 __u32 *magic = (__u32*)update->buf;
4280 struct phys_disk *pd;
4281 struct virtual_disk *vd;
4282 struct vd_config *vc;
4283 struct vcl *vcl;
4284 struct dl *dl;
f21e18ca
N
4285 unsigned int mppe;
4286 unsigned int ent;
c7079c84 4287 unsigned int pdnum, pd2;
88c164f4 4288
2c514b71 4289 dprintf("Process update %x\n", *magic);
7e1432fb 4290
88c164f4
NB
4291 switch (*magic) {
4292 case DDF_PHYS_RECORDS_MAGIC:
4293
4294 if (update->len != (sizeof(struct phys_disk) +
4295 sizeof(struct phys_disk_entry)))
4296 return;
4297 pd = (struct phys_disk*)update->buf;
4298
4299 ent = __be16_to_cpu(pd->used_pdes);
4300 if (ent >= __be16_to_cpu(ddf->phys->max_pdes))
4301 return;
4dd968cc
N
4302 if (pd->entries[0].state & __cpu_to_be16(DDF_Missing)) {
4303 struct dl **dlp;
4304 /* removing this disk. */
4305 ddf->phys->entries[ent].state |= __cpu_to_be16(DDF_Missing);
4306 for (dlp = &ddf->dlist; *dlp; dlp = &(*dlp)->next) {
4307 struct dl *dl = *dlp;
4308 if (dl->pdnum == (signed)ent) {
4309 close(dl->fd);
4310 dl->fd = -1;
4311 /* FIXME this doesn't free
4312 * dl->devname */
4313 update->space = dl;
4314 *dlp = dl->next;
4315 break;
4316 }
4317 }
7d5a7ff3 4318 ddf_set_updates_pending(ddf);
4dd968cc
N
4319 return;
4320 }
88c164f4
NB
4321 if (!all_ff(ddf->phys->entries[ent].guid))
4322 return;
4323 ddf->phys->entries[ent] = pd->entries[0];
4324 ddf->phys->used_pdes = __cpu_to_be16(1 +
613b0d17 4325 __be16_to_cpu(ddf->phys->used_pdes));
7d5a7ff3 4326 ddf_set_updates_pending(ddf);
2cc2983d
N
4327 if (ddf->add_list) {
4328 struct active_array *a;
4329 struct dl *al = ddf->add_list;
4330 ddf->add_list = al->next;
4331
4332 al->next = ddf->dlist;
4333 ddf->dlist = al;
4334
4335 /* As a device has been added, we should check
4336 * for any degraded devices that might make
4337 * use of this spare */
4338 for (a = st->arrays ; a; a=a->next)
4339 a->check_degraded = 1;
4340 }
88c164f4
NB
4341 break;
4342
4343 case DDF_VIRT_RECORDS_MAGIC:
4344
4345 if (update->len != (sizeof(struct virtual_disk) +
4346 sizeof(struct virtual_entry)))
4347 return;
4348 vd = (struct virtual_disk*)update->buf;
4349
f646805e 4350 if (vd->entries[0].state == DDF_state_deleted) {
4351 if (_kill_subarray_ddf(ddf, vd->entries[0].guid))
4352 return;
4353 } else {
4354
4355 ent = find_unused_vde(ddf);
4356 if (ent == DDF_NOTFOUND)
4357 return;
4358 ddf->virt->entries[ent] = vd->entries[0];
4359 ddf->virt->populated_vdes =
4360 __cpu_to_be16(
4361 1 + __be16_to_cpu(
4362 ddf->virt->populated_vdes));
4363 }
7d5a7ff3 4364 ddf_set_updates_pending(ddf);
88c164f4
NB
4365 break;
4366
4367 case DDF_VD_CONF_MAGIC:
2c514b71 4368 dprintf("len %d %d\n", update->len, ddf->conf_rec_len);
88c164f4
NB
4369
4370 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
f21e18ca 4371 if ((unsigned)update->len != ddf->conf_rec_len * 512)
88c164f4
NB
4372 return;
4373 vc = (struct vd_config*)update->buf;
4374 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
4375 if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0)
4376 break;
2c514b71 4377 dprintf("vcl = %p\n", vcl);
88c164f4
NB
4378 if (vcl) {
4379 /* An update, just copy the phys_refnum and lba_offset
4380 * fields
4381 */
21a63551 4382 struct vd_config *conf = &vcl->conf;
4383 if (vcl->other_bvds != NULL &&
4384 conf->sec_elmnt_seq != vc->sec_elmnt_seq) {
4385 unsigned int i;
4386 for (i = 1; i < conf->sec_elmnt_count; i++)
4387 if (vcl->other_bvds[i-1]->sec_elmnt_seq
4388 == vc->sec_elmnt_seq)
4389 break;
4390 if (i == conf->sec_elmnt_count) {
4391 pr_err("%s/DDF_VD_CONF_MAGIC: BVD %u not found\n",
4392 __func__, vc->sec_elmnt_seq);
4393 return;
4394 }
4395 conf = vcl->other_bvds[i-1];
4396 }
4397 memcpy(conf->phys_refnum, vc->phys_refnum,
88c164f4
NB
4398 mppe * (sizeof(__u32) + sizeof(__u64)));
4399 } else {
4400 /* A new VD_CONF */
e6b9548d
DW
4401 if (!update->space)
4402 return;
88c164f4
NB
4403 vcl = update->space;
4404 update->space = NULL;
4405 vcl->next = ddf->conflist;
edd8d13c 4406 memcpy(&vcl->conf, vc, update->len);
fb9d0acb 4407 ent = find_vde_by_guid(ddf, vc->guid);
4408 if (ent == DDF_NOTFOUND)
4409 return;
4410 vcl->vcnum = ent;
88c164f4
NB
4411 ddf->conflist = vcl;
4412 }
c7079c84
N
4413 /* Set DDF_Transition on all Failed devices - to help
4414 * us detect those that are no longer in use
4415 */
4416 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4417 if (ddf->phys->entries[pdnum].state
4418 & __be16_to_cpu(DDF_Failed))
4419 ddf->phys->entries[pdnum].state
4420 |= __be16_to_cpu(DDF_Transition);
88c164f4
NB
4421 /* Now make sure vlist is correct for each dl. */
4422 for (dl = ddf->dlist; dl; dl = dl->next) {
f21e18ca 4423 unsigned int vn = 0;
8401644c 4424 int in_degraded = 0;
5838fccd 4425 for (vcl = ddf->conflist; vcl ; vcl = vcl->next) {
4426 unsigned int dn, ibvd;
4427 const struct vd_config *conf;
4428 int vstate;
4429 dn = get_pd_index_from_refnum(vcl,
4430 dl->disk.refnum,
4431 ddf->mppe,
4432 &conf, &ibvd);
4433 if (dn == DDF_NOTFOUND)
4434 continue;
4435 dprintf("dev %d/%08x has %s (sec=%u) at %d\n",
ad60eea1 4436 dl->pdnum,
4437 __be32_to_cpu(dl->disk.refnum),
5838fccd 4438 guid_str(conf->guid),
4439 conf->sec_elmnt_seq, vn);
4440 /* Clear the Transition flag */
4441 if (ddf->phys->entries[dl->pdnum].state
4442 & __be16_to_cpu(DDF_Failed))
4443 ddf->phys->entries[dl->pdnum].state &=
4444 ~__be16_to_cpu(DDF_Transition);
4445 dl->vlist[vn++] = vcl;
4446 vstate = ddf->virt->entries[vcl->vcnum].state
4447 & DDF_state_mask;
4448 if (vstate == DDF_state_degraded ||
4449 vstate == DDF_state_part_optimal)
4450 in_degraded = 1;
4451 }
88c164f4
NB
4452 while (vn < ddf->max_part)
4453 dl->vlist[vn++] = NULL;
7e1432fb
NB
4454 if (dl->vlist[0]) {
4455 ddf->phys->entries[dl->pdnum].type &=
4456 ~__cpu_to_be16(DDF_Global_Spare);
8401644c
N
4457 if (!(ddf->phys->entries[dl->pdnum].type &
4458 __cpu_to_be16(DDF_Active_in_VD))) {
613b0d17
N
4459 ddf->phys->entries[dl->pdnum].type |=
4460 __cpu_to_be16(DDF_Active_in_VD);
4461 if (in_degraded)
4462 ddf->phys->entries[dl->pdnum].state |=
4463 __cpu_to_be16(DDF_Rebuilding);
4464 }
7e1432fb
NB
4465 }
4466 if (dl->spare) {
4467 ddf->phys->entries[dl->pdnum].type &=
4468 ~__cpu_to_be16(DDF_Global_Spare);
4469 ddf->phys->entries[dl->pdnum].type |=
4470 __cpu_to_be16(DDF_Spare);
4471 }
4472 if (!dl->vlist[0] && !dl->spare) {
4473 ddf->phys->entries[dl->pdnum].type |=
4474 __cpu_to_be16(DDF_Global_Spare);
4475 ddf->phys->entries[dl->pdnum].type &=
4476 ~__cpu_to_be16(DDF_Spare |
4477 DDF_Active_in_VD);
4478 }
88c164f4 4479 }
c7079c84
N
4480
4481 /* Now remove any 'Failed' devices that are not part
4482 * of any VD. They will have the Transition flag set.
4483 * Once done, we need to update all dl->pdnum numbers.
4484 */
4485 pd2 = 0;
4486 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4487 if ((ddf->phys->entries[pdnum].state
4488 & __be16_to_cpu(DDF_Failed))
4489 && (ddf->phys->entries[pdnum].state
4490 & __be16_to_cpu(DDF_Transition)))
4491 /* skip this one */;
4492 else if (pdnum == pd2)
4493 pd2++;
4494 else {
4495 ddf->phys->entries[pd2] = ddf->phys->entries[pdnum];
4496 for (dl = ddf->dlist; dl; dl = dl->next)
4497 if (dl->pdnum == (int)pdnum)
4498 dl->pdnum = pd2;
4499 pd2++;
4500 }
4501 ddf->phys->used_pdes = __cpu_to_be16(pd2);
4502 while (pd2 < pdnum) {
4503 memset(ddf->phys->entries[pd2].guid, 0xff, DDF_GUID_LEN);
4504 pd2++;
4505 }
4506
7d5a7ff3 4507 ddf_set_updates_pending(ddf);
88c164f4
NB
4508 break;
4509 case DDF_SPARE_ASSIGN_MAGIC:
4510 default: break;
4511 }
4512}
4513
edd8d13c
NB
4514static void ddf_prepare_update(struct supertype *st,
4515 struct metadata_update *update)
4516{
4517 /* This update arrived at managemon.
4518 * We are about to pass it to monitor.
4519 * If a malloc is needed, do it here.
4520 */
4521 struct ddf_super *ddf = st->sb;
4522 __u32 *magic = (__u32*)update->buf;
4523 if (*magic == DDF_VD_CONF_MAGIC)
e6b9548d 4524 if (posix_memalign(&update->space, 512,
613b0d17
N
4525 offsetof(struct vcl, conf)
4526 + ddf->conf_rec_len * 512) != 0)
e6b9548d 4527 update->space = NULL;
edd8d13c
NB
4528}
4529
7e1432fb
NB
4530/*
4531 * Check if the array 'a' is degraded but not failed.
4532 * If it is, find as many spares as are available and needed and
4533 * arrange for their inclusion.
4534 * We only choose devices which are not already in the array,
4535 * and prefer those with a spare-assignment to this array.
4536 * otherwise we choose global spares - assuming always that
4537 * there is enough room.
4538 * For each spare that we assign, we return an 'mdinfo' which
4539 * describes the position for the device in the array.
4540 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4541 * the new phys_refnum and lba_offset values.
4542 *
4543 * Only worry about BVDs at the moment.
4544 */
4545static struct mdinfo *ddf_activate_spare(struct active_array *a,
4546 struct metadata_update **updates)
4547{
4548 int working = 0;
4549 struct mdinfo *d;
4550 struct ddf_super *ddf = a->container->sb;
4551 int global_ok = 0;
4552 struct mdinfo *rv = NULL;
4553 struct mdinfo *di;
4554 struct metadata_update *mu;
4555 struct dl *dl;
4556 int i;
baba3f4e 4557 struct vcl *vcl;
7e1432fb 4558 struct vd_config *vc;
baba3f4e 4559 unsigned int n_bvd;
7e1432fb 4560
7e1432fb
NB
4561 for (d = a->info.devs ; d ; d = d->next) {
4562 if ((d->curr_state & DS_FAULTY) &&
613b0d17 4563 d->state_fd >= 0)
7e1432fb
NB
4564 /* wait for Removal to happen */
4565 return NULL;
4566 if (d->state_fd >= 0)
4567 working ++;
4568 }
4569
2c514b71
NB
4570 dprintf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks,
4571 a->info.array.level);
7e1432fb
NB
4572 if (working == a->info.array.raid_disks)
4573 return NULL; /* array not degraded */
4574 switch (a->info.array.level) {
4575 case 1:
4576 if (working == 0)
4577 return NULL; /* failed */
4578 break;
4579 case 4:
4580 case 5:
4581 if (working < a->info.array.raid_disks - 1)
4582 return NULL; /* failed */
4583 break;
4584 case 6:
4585 if (working < a->info.array.raid_disks - 2)
4586 return NULL; /* failed */
4587 break;
4588 default: /* concat or stripe */
4589 return NULL; /* failed */
4590 }
4591
4592 /* For each slot, if it is not working, find a spare */
4593 dl = ddf->dlist;
4594 for (i = 0; i < a->info.array.raid_disks; i++) {
4595 for (d = a->info.devs ; d ; d = d->next)
4596 if (d->disk.raid_disk == i)
4597 break;
2c514b71 4598 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
7e1432fb
NB
4599 if (d && (d->state_fd >= 0))
4600 continue;
4601
4602 /* OK, this device needs recovery. Find a spare */
4603 again:
4604 for ( ; dl ; dl = dl->next) {
4605 unsigned long long esize;
4606 unsigned long long pos;
4607 struct mdinfo *d2;
4608 int is_global = 0;
4609 int is_dedicated = 0;
4610 struct extent *ex;
f21e18ca 4611 unsigned int j;
7e1432fb
NB
4612 /* If in this array, skip */
4613 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
7590d562
N
4614 if (d2->state_fd >= 0 &&
4615 d2->disk.major == dl->major &&
7e1432fb 4616 d2->disk.minor == dl->minor) {
2c514b71 4617 dprintf("%x:%x already in array\n", dl->major, dl->minor);
7e1432fb
NB
4618 break;
4619 }
4620 if (d2)
4621 continue;
4622 if (ddf->phys->entries[dl->pdnum].type &
4623 __cpu_to_be16(DDF_Spare)) {
4624 /* Check spare assign record */
4625 if (dl->spare) {
4626 if (dl->spare->type & DDF_spare_dedicated) {
4627 /* check spare_ents for guid */
4628 for (j = 0 ;
4629 j < __be16_to_cpu(dl->spare->populated);
4630 j++) {
4631 if (memcmp(dl->spare->spare_ents[j].guid,
4632 ddf->virt->entries[a->info.container_member].guid,
4633 DDF_GUID_LEN) == 0)
4634 is_dedicated = 1;
4635 }
4636 } else
4637 is_global = 1;
4638 }
4639 } else if (ddf->phys->entries[dl->pdnum].type &
4640 __cpu_to_be16(DDF_Global_Spare)) {
4641 is_global = 1;
e0e7aeaa
N
4642 } else if (!(ddf->phys->entries[dl->pdnum].state &
4643 __cpu_to_be16(DDF_Failed))) {
4644 /* we can possibly use some of this */
4645 is_global = 1;
7e1432fb
NB
4646 }
4647 if ( ! (is_dedicated ||
4648 (is_global && global_ok))) {
2c514b71 4649 dprintf("%x:%x not suitable: %d %d\n", dl->major, dl->minor,
613b0d17 4650 is_dedicated, is_global);
7e1432fb
NB
4651 continue;
4652 }
4653
4654 /* We are allowed to use this device - is there space?
4655 * We need a->info.component_size sectors */
4656 ex = get_extents(ddf, dl);
4657 if (!ex) {
2c514b71 4658 dprintf("cannot get extents\n");
7e1432fb
NB
4659 continue;
4660 }
4661 j = 0; pos = 0;
4662 esize = 0;
4663
4664 do {
4665 esize = ex[j].start - pos;
4666 if (esize >= a->info.component_size)
4667 break;
e5cc7d46
N
4668 pos = ex[j].start + ex[j].size;
4669 j++;
4670 } while (ex[j-1].size);
7e1432fb
NB
4671
4672 free(ex);
4673 if (esize < a->info.component_size) {
e5cc7d46
N
4674 dprintf("%x:%x has no room: %llu %llu\n",
4675 dl->major, dl->minor,
2c514b71 4676 esize, a->info.component_size);
7e1432fb
NB
4677 /* No room */
4678 continue;
4679 }
4680
4681 /* Cool, we have a device with some space at pos */
503975b9 4682 di = xcalloc(1, sizeof(*di));
7e1432fb
NB
4683 di->disk.number = i;
4684 di->disk.raid_disk = i;
4685 di->disk.major = dl->major;
4686 di->disk.minor = dl->minor;
4687 di->disk.state = 0;
d23534e4 4688 di->recovery_start = 0;
7e1432fb
NB
4689 di->data_offset = pos;
4690 di->component_size = a->info.component_size;
4691 di->container_member = dl->pdnum;
4692 di->next = rv;
4693 rv = di;
2c514b71
NB
4694 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
4695 i, pos);
7e1432fb
NB
4696
4697 break;
4698 }
4699 if (!dl && ! global_ok) {
4700 /* not enough dedicated spares, try global */
4701 global_ok = 1;
4702 dl = ddf->dlist;
4703 goto again;
4704 }
4705 }
4706
4707 if (!rv)
4708 /* No spares found */
4709 return rv;
4710 /* Now 'rv' has a list of devices to return.
4711 * Create a metadata_update record to update the
4712 * phys_refnum and lba_offset values
4713 */
503975b9
N
4714 mu = xmalloc(sizeof(*mu));
4715 if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
79244939
DW
4716 free(mu);
4717 mu = NULL;
4718 }
503975b9 4719 mu->buf = xmalloc(ddf->conf_rec_len * 512);
7590d562
N
4720 mu->len = ddf->conf_rec_len * 512;
4721 mu->space = NULL;
f50ae22e 4722 mu->space_list = NULL;
7e1432fb 4723 mu->next = *updates;
baba3f4e 4724 vc = find_vdcr(ddf, a->info.container_member, di->disk.raid_disk,
4725 &n_bvd, &vcl);
7e1432fb
NB
4726 memcpy(mu->buf, vc, ddf->conf_rec_len * 512);
4727
4728 vc = (struct vd_config*)mu->buf;
7e1432fb
NB
4729 for (di = rv ; di ; di = di->next) {
4730 vc->phys_refnum[di->disk.raid_disk] =
4731 ddf->phys->entries[dl->pdnum].refnum;
57a66662 4732 LBA_OFFSET(ddf, vc)[di->disk.raid_disk]
4733 = __cpu_to_be64(di->data_offset);
7e1432fb
NB
4734 }
4735 *updates = mu;
4736 return rv;
4737}
0e600426 4738#endif /* MDASSEMBLE */
7e1432fb 4739
b640a252
N
4740static int ddf_level_to_layout(int level)
4741{
4742 switch(level) {
4743 case 0:
4744 case 1:
4745 return 0;
4746 case 5:
4747 return ALGORITHM_LEFT_SYMMETRIC;
4748 case 6:
4749 return ALGORITHM_ROTATING_N_CONTINUE;
4750 case 10:
4751 return 0x102;
4752 default:
4753 return UnSet;
4754 }
4755}
4756
30f58b22
DW
4757static void default_geometry_ddf(struct supertype *st, int *level, int *layout, int *chunk)
4758{
4759 if (level && *level == UnSet)
4760 *level = LEVEL_CONTAINER;
4761
4762 if (level && layout && *layout == UnSet)
4763 *layout = ddf_level_to_layout(*level);
4764}
4765
a322f70c
DW
4766struct superswitch super_ddf = {
4767#ifndef MDASSEMBLE
4768 .examine_super = examine_super_ddf,
4769 .brief_examine_super = brief_examine_super_ddf,
4737ae25 4770 .brief_examine_subarrays = brief_examine_subarrays_ddf,
bceedeec 4771 .export_examine_super = export_examine_super_ddf,
a322f70c
DW
4772 .detail_super = detail_super_ddf,
4773 .brief_detail_super = brief_detail_super_ddf,
4774 .validate_geometry = validate_geometry_ddf,
78e44928 4775 .write_init_super = write_init_super_ddf,
0e600426 4776 .add_to_super = add_to_super_ddf,
4dd968cc 4777 .remove_from_super = remove_from_super_ddf,
2b959fbf 4778 .load_container = load_container_ddf,
74db60b0 4779 .copy_metadata = copy_metadata_ddf,
a322f70c
DW
4780#endif
4781 .match_home = match_home_ddf,
4782 .uuid_from_super= uuid_from_super_ddf,
4783 .getinfo_super = getinfo_super_ddf,
4784 .update_super = update_super_ddf,
4785
4786 .avail_size = avail_size_ddf,
4787
a19c88b8
NB
4788 .compare_super = compare_super_ddf,
4789
a322f70c 4790 .load_super = load_super_ddf,
ba7eb04f 4791 .init_super = init_super_ddf,
955e9ea1 4792 .store_super = store_super_ddf,
a322f70c
DW
4793 .free_super = free_super_ddf,
4794 .match_metadata_desc = match_metadata_desc_ddf,
78e44928 4795 .container_content = container_content_ddf,
30f58b22 4796 .default_geometry = default_geometry_ddf,
f646805e 4797 .kill_subarray = kill_subarray_ddf,
a322f70c 4798
a322f70c 4799 .external = 1,
549e9569 4800
0e600426 4801#ifndef MDASSEMBLE
549e9569
NB
4802/* for mdmon */
4803 .open_new = ddf_open_new,
ed9d66aa 4804 .set_array_state= ddf_set_array_state,
549e9569
NB
4805 .set_disk = ddf_set_disk,
4806 .sync_metadata = ddf_sync_metadata,
88c164f4 4807 .process_update = ddf_process_update,
edd8d13c 4808 .prepare_update = ddf_prepare_update,
7e1432fb 4809 .activate_spare = ddf_activate_spare,
0e600426 4810#endif
4cce4069 4811 .name = "ddf",
a322f70c 4812};