]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-ddf.c
DDF: get_extents: support secondary RAID level
[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
1299static void examine_vd(int n, struct ddf_super *sb, char *guid)
1300{
8c3b8c2c 1301 int crl = sb->conf_rec_len;
a322f70c
DW
1302 struct vcl *vcl;
1303
1304 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
f21e18ca 1305 unsigned int i;
a322f70c
DW
1306 struct vd_config *vc = &vcl->conf;
1307
1308 if (calc_crc(vc, crl*512) != vc->crc)
1309 continue;
1310 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
1311 continue;
1312
1313 /* Ok, we know about this VD, let's give more details */
b06e3095 1314 printf(" Raid Devices[%d] : %d (", n,
a322f70c 1315 __be16_to_cpu(vc->prim_elmnt_count));
f21e18ca 1316 for (i = 0; i < __be16_to_cpu(vc->prim_elmnt_count); i++) {
b06e3095
N
1317 int j;
1318 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1319 for (j=0; j<cnt; j++)
1320 if (vc->phys_refnum[i] == sb->phys->entries[j].refnum)
1321 break;
1322 if (i) printf(" ");
1323 if (j < cnt)
1324 printf("%d", j);
1325 else
1326 printf("--");
1327 }
1328 printf(")\n");
1329 if (vc->chunk_shift != 255)
613b0d17
N
1330 printf(" Chunk Size[%d] : %d sectors\n", n,
1331 1 << vc->chunk_shift);
a322f70c
DW
1332 printf(" Raid Level[%d] : %s\n", n,
1333 map_num(ddf_level, vc->prl)?:"-unknown-");
1334 if (vc->sec_elmnt_count != 1) {
1335 printf(" Secondary Position[%d] : %d of %d\n", n,
1336 vc->sec_elmnt_seq, vc->sec_elmnt_count);
1337 printf(" Secondary Level[%d] : %s\n", n,
1338 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
1339 }
1340 printf(" Device Size[%d] : %llu\n", n,
c9b6907b 1341 (unsigned long long)__be64_to_cpu(vc->blocks)/2);
a322f70c 1342 printf(" Array Size[%d] : %llu\n", n,
c9b6907b 1343 (unsigned long long)__be64_to_cpu(vc->array_blocks)/2);
a322f70c
DW
1344 }
1345}
1346
1347static void examine_vds(struct ddf_super *sb)
1348{
1349 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
fb9d0acb 1350 unsigned int i;
a322f70c
DW
1351 printf(" Virtual Disks : %d\n", cnt);
1352
fb9d0acb 1353 for (i = 0; i < __be16_to_cpu(sb->virt->max_vdes); i++) {
a322f70c 1354 struct virtual_entry *ve = &sb->virt->entries[i];
fb9d0acb 1355 if (all_ff(ve->guid))
1356 continue;
b06e3095 1357 printf("\n");
a322f70c
DW
1358 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
1359 printf("\n");
1360 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
1361 printf(" state[%d] : %s, %s%s\n", i,
1362 map_num(ddf_state, ve->state & 7),
1363 (ve->state & 8) ? "Morphing, ": "",
1364 (ve->state & 16)? "Not Consistent" : "Consistent");
1365 printf(" init state[%d] : %s\n", i,
1366 map_num(ddf_init_state, ve->init_state&3));
1367 printf(" access[%d] : %s\n", i,
1368 map_num(ddf_access, (ve->init_state>>6) & 3));
1369 printf(" Name[%d] : %.16s\n", i, ve->name);
1370 examine_vd(i, sb, ve->guid);
1371 }
1372 if (cnt) printf("\n");
1373}
1374
1375static void examine_pds(struct ddf_super *sb)
1376{
1377 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1378 int i;
1379 struct dl *dl;
1380 printf(" Physical Disks : %d\n", cnt);
962371a5 1381 printf(" Number RefNo Size Device Type/State\n");
a322f70c
DW
1382
1383 for (i=0 ; i<cnt ; i++) {
1384 struct phys_disk_entry *pd = &sb->phys->entries[i];
1385 int type = __be16_to_cpu(pd->type);
1386 int state = __be16_to_cpu(pd->state);
1387
b06e3095
N
1388 //printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1389 //printf("\n");
1390 printf(" %3d %08x ", i,
a322f70c 1391 __be32_to_cpu(pd->refnum));
613b0d17 1392 printf("%8lluK ",
c9b6907b 1393 (unsigned long long)__be64_to_cpu(pd->config_size)>>1);
b06e3095
N
1394 for (dl = sb->dlist; dl ; dl = dl->next) {
1395 if (dl->disk.refnum == pd->refnum) {
1396 char *dv = map_dev(dl->major, dl->minor, 0);
1397 if (dv) {
962371a5 1398 printf("%-15s", dv);
b06e3095
N
1399 break;
1400 }
1401 }
1402 }
1403 if (!dl)
962371a5 1404 printf("%15s","");
b06e3095 1405 printf(" %s%s%s%s%s",
a322f70c 1406 (type&2) ? "active":"",
b06e3095 1407 (type&4) ? "Global-Spare":"",
a322f70c
DW
1408 (type&8) ? "spare" : "",
1409 (type&16)? ", foreign" : "",
1410 (type&32)? "pass-through" : "");
18cb4496
N
1411 if (state & DDF_Failed)
1412 /* This over-rides these three */
1413 state &= ~(DDF_Online|DDF_Rebuilding|DDF_Transition);
b06e3095 1414 printf("/%s%s%s%s%s%s%s",
a322f70c
DW
1415 (state&1)? "Online": "Offline",
1416 (state&2)? ", Failed": "",
1417 (state&4)? ", Rebuilding": "",
1418 (state&8)? ", in-transition": "",
b06e3095
N
1419 (state&16)? ", SMART-errors": "",
1420 (state&32)? ", Unrecovered-Read-Errors": "",
a322f70c 1421 (state&64)? ", Missing" : "");
a322f70c
DW
1422 printf("\n");
1423 }
1424}
1425
1426static void examine_super_ddf(struct supertype *st, char *homehost)
1427{
1428 struct ddf_super *sb = st->sb;
1429
1430 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1431 printf(" Version : %.8s\n", sb->anchor.revision);
598f0d58
NB
1432 printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
1433 printf("\n");
1434 printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
a322f70c
DW
1435 printf("\n");
1436 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1437 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1438 ?"yes" : "no");
1439 examine_vds(sb);
1440 examine_pds(sb);
1441}
1442
a5d85af7 1443static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map);
ff54de6e 1444
bedbf68a 1445static void uuid_from_ddf_guid(const char *guid, int uuid[4]);
42dc2744 1446static void uuid_from_super_ddf(struct supertype *st, int uuid[4]);
ff54de6e 1447
bedbf68a 1448static unsigned int get_vd_num_of_subarray(struct supertype *st)
1449{
1450 /*
1451 * Figure out the VD number for this supertype.
1452 * Returns DDF_CONTAINER for the container itself,
1453 * and DDF_NOTFOUND on error.
1454 */
1455 struct ddf_super *ddf = st->sb;
1456 struct mdinfo *sra;
1457 char *sub, *end;
1458 unsigned int vcnum;
1459
1460 if (*st->container_devnm == '\0')
1461 return DDF_CONTAINER;
1462
1463 sra = sysfs_read(-1, st->devnm, GET_VERSION);
1464 if (!sra || sra->array.major_version != -1 ||
1465 sra->array.minor_version != -2 ||
1466 !is_subarray(sra->text_version))
1467 return DDF_NOTFOUND;
1468
1469 sub = strchr(sra->text_version + 1, '/');
1470 if (sub != NULL)
1471 vcnum = strtoul(sub + 1, &end, 10);
1472 if (sub == NULL || *sub == '\0' || *end != '\0' ||
1473 vcnum >= __be16_to_cpu(ddf->active->max_vd_entries))
1474 return DDF_NOTFOUND;
1475
1476 return vcnum;
1477}
1478
061f2c6a 1479static void brief_examine_super_ddf(struct supertype *st, int verbose)
4737ae25
N
1480{
1481 /* We just write a generic DDF ARRAY entry
1482 */
1483 struct mdinfo info;
1484 char nbuf[64];
a5d85af7 1485 getinfo_super_ddf(st, &info, NULL);
4737ae25
N
1486 fname_from_uuid(st, &info, nbuf, ':');
1487
1488 printf("ARRAY metadata=ddf UUID=%s\n", nbuf + 5);
1489}
1490
1491static void brief_examine_subarrays_ddf(struct supertype *st, int verbose)
a322f70c
DW
1492{
1493 /* We just write a generic DDF ARRAY entry
a322f70c 1494 */
42dc2744 1495 struct ddf_super *ddf = st->sb;
ff54de6e 1496 struct mdinfo info;
f21e18ca 1497 unsigned int i;
ff54de6e 1498 char nbuf[64];
a5d85af7 1499 getinfo_super_ddf(st, &info, NULL);
ff54de6e 1500 fname_from_uuid(st, &info, nbuf, ':');
42dc2744 1501
f21e18ca 1502 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
42dc2744
N
1503 struct virtual_entry *ve = &ddf->virt->entries[i];
1504 struct vcl vcl;
1505 char nbuf1[64];
1506 if (all_ff(ve->guid))
1507 continue;
1508 memcpy(vcl.conf.guid, ve->guid, DDF_GUID_LEN);
1509 ddf->currentconf =&vcl;
1510 uuid_from_super_ddf(st, info.uuid);
1511 fname_from_uuid(st, &info, nbuf1, ':');
1512 printf("ARRAY container=%s member=%d UUID=%s\n",
1513 nbuf+5, i, nbuf1+5);
1514 }
a322f70c
DW
1515}
1516
bceedeec
N
1517static void export_examine_super_ddf(struct supertype *st)
1518{
1519 struct mdinfo info;
1520 char nbuf[64];
a5d85af7 1521 getinfo_super_ddf(st, &info, NULL);
bceedeec
N
1522 fname_from_uuid(st, &info, nbuf, ':');
1523 printf("MD_METADATA=ddf\n");
1524 printf("MD_LEVEL=container\n");
1525 printf("MD_UUID=%s\n", nbuf+5);
1526}
bceedeec 1527
74db60b0
N
1528static int copy_metadata_ddf(struct supertype *st, int from, int to)
1529{
1530 void *buf;
1531 unsigned long long dsize, offset;
1532 int bytes;
1533 struct ddf_header *ddf;
1534 int written = 0;
1535
1536 /* The meta consists of an anchor, a primary, and a secondary.
1537 * This all lives at the end of the device.
1538 * So it is easiest to find the earliest of primary and
1539 * secondary, and copy everything from there.
1540 *
1541 * Anchor is 512 from end It contains primary_lba and secondary_lba
1542 * we choose one of those
1543 */
1544
1545 if (posix_memalign(&buf, 4096, 4096) != 0)
1546 return 1;
1547
1548 if (!get_dev_size(from, NULL, &dsize))
1549 goto err;
1550
1551 if (lseek64(from, dsize-512, 0) < 0)
1552 goto err;
1553 if (read(from, buf, 512) != 512)
1554 goto err;
1555 ddf = buf;
1556 if (ddf->magic != DDF_HEADER_MAGIC ||
1557 calc_crc(ddf, 512) != ddf->crc ||
1558 (memcmp(ddf->revision, DDF_REVISION_0, 8) != 0 &&
1559 memcmp(ddf->revision, DDF_REVISION_2, 8) != 0))
1560 goto err;
1561
1562 offset = dsize - 512;
1563 if ((__be64_to_cpu(ddf->primary_lba) << 9) < offset)
1564 offset = __be64_to_cpu(ddf->primary_lba) << 9;
1565 if ((__be64_to_cpu(ddf->secondary_lba) << 9) < offset)
1566 offset = __be64_to_cpu(ddf->secondary_lba) << 9;
1567
1568 bytes = dsize - offset;
1569
1570 if (lseek64(from, offset, 0) < 0 ||
1571 lseek64(to, offset, 0) < 0)
1572 goto err;
1573 while (written < bytes) {
1574 int n = bytes - written;
1575 if (n > 4096)
1576 n = 4096;
1577 if (read(from, buf, n) != n)
1578 goto err;
1579 if (write(to, buf, n) != n)
1580 goto err;
1581 written += n;
1582 }
1583 free(buf);
1584 return 0;
1585err:
1586 free(buf);
1587 return 1;
1588}
1589
a322f70c
DW
1590static void detail_super_ddf(struct supertype *st, char *homehost)
1591{
1592 /* FIXME later
1593 * Could print DDF GUID
1594 * Need to find which array
1595 * If whole, briefly list all arrays
1596 * If one, give name
1597 */
1598}
1599
1600static void brief_detail_super_ddf(struct supertype *st)
1601{
ff54de6e
N
1602 struct mdinfo info;
1603 char nbuf[64];
bedbf68a 1604 struct ddf_super *ddf = st->sb;
1605 unsigned int vcnum = get_vd_num_of_subarray(st);
1606 if (vcnum == DDF_CONTAINER)
1607 uuid_from_super_ddf(st, info.uuid);
1608 else if (vcnum == DDF_NOTFOUND)
1609 return;
1610 else
1611 uuid_from_ddf_guid(ddf->virt->entries[vcnum].guid, info.uuid);
ff54de6e
N
1612 fname_from_uuid(st, &info, nbuf,':');
1613 printf(" UUID=%s", nbuf + 5);
a322f70c 1614}
a322f70c
DW
1615#endif
1616
1617static int match_home_ddf(struct supertype *st, char *homehost)
1618{
1619 /* It matches 'this' host if the controller is a
1620 * Linux-MD controller with vendor_data matching
1621 * the hostname
1622 */
1623 struct ddf_super *ddf = st->sb;
f21e18ca 1624 unsigned int len;
d1d3482b
N
1625
1626 if (!homehost)
1627 return 0;
1628 len = strlen(homehost);
a322f70c
DW
1629
1630 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1631 len < sizeof(ddf->controller.vendor_data) &&
1632 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1633 ddf->controller.vendor_data[len] == 0);
1634}
1635
0e600426 1636#ifndef MDASSEMBLE
baba3f4e 1637static int find_index_in_bvd(const struct ddf_super *ddf,
1638 const struct vd_config *conf, unsigned int n,
1639 unsigned int *n_bvd)
1640{
1641 /*
1642 * Find the index of the n-th valid physical disk in this BVD
1643 */
1644 unsigned int i, j;
1645 for (i = 0, j = 0; i < ddf->mppe &&
1646 j < __be16_to_cpu(conf->prim_elmnt_count); i++) {
1647 if (conf->phys_refnum[i] != 0xffffffff) {
1648 if (n == j) {
1649 *n_bvd = i;
1650 return 1;
1651 }
1652 j++;
1653 }
1654 }
1655 dprintf("%s: couldn't find BVD member %u (total %u)\n",
1656 __func__, n, __be16_to_cpu(conf->prim_elmnt_count));
1657 return 0;
1658}
1659
1660static struct vd_config *find_vdcr(struct ddf_super *ddf, unsigned int inst,
1661 unsigned int n,
1662 unsigned int *n_bvd, struct vcl **vcl)
a322f70c 1663{
7a7cc504 1664 struct vcl *v;
59e36268 1665
baba3f4e 1666 for (v = ddf->conflist; v; v = v->next) {
1667 unsigned int nsec, ibvd;
1668 struct vd_config *conf;
1669 if (inst != v->vcnum)
1670 continue;
1671 conf = &v->conf;
1672 if (conf->sec_elmnt_count == 1) {
1673 if (find_index_in_bvd(ddf, conf, n, n_bvd)) {
1674 *vcl = v;
1675 return conf;
1676 } else
1677 goto bad;
1678 }
1679 if (v->other_bvds == NULL) {
1680 pr_err("%s: BUG: other_bvds is NULL, nsec=%u\n",
1681 __func__, conf->sec_elmnt_count);
1682 goto bad;
1683 }
1684 nsec = n / __be16_to_cpu(conf->prim_elmnt_count);
1685 if (conf->sec_elmnt_seq != nsec) {
1686 for (ibvd = 1; ibvd < conf->sec_elmnt_count; ibvd++) {
baba3f4e 1687 if (v->other_bvds[ibvd-1]->sec_elmnt_seq
1688 == nsec)
1689 break;
1690 }
1691 if (ibvd == conf->sec_elmnt_count)
1692 goto bad;
1693 conf = v->other_bvds[ibvd-1];
1694 }
1695 if (!find_index_in_bvd(ddf, conf,
1696 n - nsec*conf->sec_elmnt_count, n_bvd))
1697 goto bad;
1698 dprintf("%s: found disk %u as member %u in bvd %d of array %u\n"
1699 , __func__, n, *n_bvd, ibvd-1, inst);
1700 *vcl = v;
1701 return conf;
1702 }
1703bad:
1704 pr_err("%s: Could't find disk %d in array %u\n", __func__, n, inst);
7a7cc504
NB
1705 return NULL;
1706}
0e600426 1707#endif
7a7cc504 1708
5ec636b7 1709static int find_phys(const struct ddf_super *ddf, __u32 phys_refnum)
7a7cc504
NB
1710{
1711 /* Find the entry in phys_disk which has the given refnum
1712 * and return it's index
1713 */
f21e18ca
N
1714 unsigned int i;
1715 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes); i++)
7a7cc504
NB
1716 if (ddf->phys->entries[i].refnum == phys_refnum)
1717 return i;
1718 return -1;
a322f70c
DW
1719}
1720
bedbf68a 1721static void uuid_from_ddf_guid(const char *guid, int uuid[4])
1722{
1723 char buf[20];
1724 struct sha1_ctx ctx;
1725 sha1_init_ctx(&ctx);
1726 sha1_process_bytes(guid, DDF_GUID_LEN, &ctx);
1727 sha1_finish_ctx(&ctx, buf);
1728 memcpy(uuid, buf, 4*4);
1729}
1730
a322f70c
DW
1731static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1732{
1733 /* The uuid returned here is used for:
1734 * uuid to put into bitmap file (Create, Grow)
1735 * uuid for backup header when saving critical section (Grow)
1736 * comparing uuids when re-adding a device into an array
51006d85
N
1737 * In these cases the uuid required is that of the data-array,
1738 * not the device-set.
1739 * uuid to recognise same set when adding a missing device back
1740 * to an array. This is a uuid for the device-set.
613b0d17 1741 *
a322f70c
DW
1742 * For each of these we can make do with a truncated
1743 * or hashed uuid rather than the original, as long as
1744 * everyone agrees.
a322f70c
DW
1745 * In the case of SVD we assume the BVD is of interest,
1746 * though that might be the case if a bitmap were made for
1747 * a mirrored SVD - worry about that later.
1748 * So we need to find the VD configuration record for the
1749 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1750 * The first 16 bytes of the sha1 of these is used.
1751 */
1752 struct ddf_super *ddf = st->sb;
d2ca6449 1753 struct vcl *vcl = ddf->currentconf;
c5afc314 1754 char *guid;
a322f70c 1755
c5afc314
N
1756 if (vcl)
1757 guid = vcl->conf.guid;
1758 else
1759 guid = ddf->anchor.guid;
bedbf68a 1760 uuid_from_ddf_guid(guid, uuid);
a322f70c
DW
1761}
1762
a5d85af7 1763static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map);
78e44928 1764
a5d85af7 1765static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info, char *map)
a322f70c
DW
1766{
1767 struct ddf_super *ddf = st->sb;
a5d85af7 1768 int map_disks = info->array.raid_disks;
90fa1a29 1769 __u32 *cptr;
a322f70c 1770
78e44928 1771 if (ddf->currentconf) {
a5d85af7 1772 getinfo_super_ddf_bvd(st, info, map);
78e44928
NB
1773 return;
1774 }
95eeceeb 1775 memset(info, 0, sizeof(*info));
78e44928 1776
a322f70c
DW
1777 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1778 info->array.level = LEVEL_CONTAINER;
1779 info->array.layout = 0;
1780 info->array.md_minor = -1;
90fa1a29
JS
1781 cptr = (__u32 *)(ddf->anchor.guid + 16);
1782 info->array.ctime = DECADE + __be32_to_cpu(*cptr);
1783
a322f70c
DW
1784 info->array.utime = 0;
1785 info->array.chunk_size = 0;
510242aa 1786 info->container_enough = 1;
a322f70c 1787
a322f70c
DW
1788 info->disk.major = 0;
1789 info->disk.minor = 0;
cba0191b
NB
1790 if (ddf->dlist) {
1791 info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
59e36268 1792 info->disk.raid_disk = find_phys(ddf, ddf->dlist->disk.refnum);
d2ca6449
NB
1793
1794 info->data_offset = __be64_to_cpu(ddf->phys->
613b0d17
N
1795 entries[info->disk.raid_disk].
1796 config_size);
d2ca6449 1797 info->component_size = ddf->dlist->size - info->data_offset;
cba0191b
NB
1798 } else {
1799 info->disk.number = -1;
661dce36 1800 info->disk.raid_disk = -1;
cba0191b
NB
1801// info->disk.raid_disk = find refnum in the table and use index;
1802 }
f22385f9 1803 info->disk.state = (1 << MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE);
a19c88b8 1804
921d9e16 1805 info->recovery_start = MaxSector;
a19c88b8 1806 info->reshape_active = 0;
6e75048b 1807 info->recovery_blocked = 0;
c5afc314 1808 info->name[0] = 0;
a322f70c 1809
f35f2525
N
1810 info->array.major_version = -1;
1811 info->array.minor_version = -2;
159c3a1a 1812 strcpy(info->text_version, "ddf");
a67dd8cc 1813 info->safe_mode_delay = 0;
159c3a1a 1814
c5afc314 1815 uuid_from_super_ddf(st, info->uuid);
a322f70c 1816
a5d85af7
N
1817 if (map) {
1818 int i;
1819 for (i = 0 ; i < map_disks; i++) {
1820 if (i < info->array.raid_disks &&
1821 (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) &&
1822 !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed))
1823 map[i] = 1;
1824 else
1825 map[i] = 0;
1826 }
1827 }
a322f70c
DW
1828}
1829
a5d85af7 1830static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info, char *map)
a322f70c
DW
1831{
1832 struct ddf_super *ddf = st->sb;
d2ca6449
NB
1833 struct vcl *vc = ddf->currentconf;
1834 int cd = ddf->currentdev;
db42fa9b 1835 int j;
8592f29d 1836 struct dl *dl;
a5d85af7 1837 int map_disks = info->array.raid_disks;
90fa1a29 1838 __u32 *cptr;
a322f70c 1839
95eeceeb 1840 memset(info, 0, sizeof(*info));
8a2848a7 1841 if (layout_ddf2md(&vc->conf, &info->array) == -1)
1842 return;
a322f70c 1843 info->array.md_minor = -1;
90fa1a29
JS
1844 cptr = (__u32 *)(vc->conf.guid + 16);
1845 info->array.ctime = DECADE + __be32_to_cpu(*cptr);
d2ca6449
NB
1846 info->array.utime = DECADE + __be32_to_cpu(vc->conf.timestamp);
1847 info->array.chunk_size = 512 << vc->conf.chunk_shift;
da9b4a62 1848 info->custom_array_size = 0;
d2ca6449 1849
f21e18ca 1850 if (cd >= 0 && (unsigned)cd < ddf->mppe) {
57a66662 1851 info->data_offset =
1852 __be64_to_cpu(LBA_OFFSET(ddf, &vc->conf)[cd]);
d2ca6449
NB
1853 if (vc->block_sizes)
1854 info->component_size = vc->block_sizes[cd];
1855 else
1856 info->component_size = __be64_to_cpu(vc->conf.blocks);
1857 }
a322f70c 1858
fb204fb2
N
1859 for (dl = ddf->dlist; dl ; dl = dl->next)
1860 if (dl->raiddisk == ddf->currentdev)
1861 break;
1862
a322f70c
DW
1863 info->disk.major = 0;
1864 info->disk.minor = 0;
fb204fb2 1865 info->disk.state = 0;
8592f29d
N
1866 if (dl) {
1867 info->disk.major = dl->major;
1868 info->disk.minor = dl->minor;
fb204fb2
N
1869 info->disk.raid_disk = dl->raiddisk;
1870 info->disk.number = dl->pdnum;
1871 info->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
8592f29d 1872 }
a322f70c 1873
103f2410
NB
1874 info->container_member = ddf->currentconf->vcnum;
1875
921d9e16 1876 info->recovery_start = MaxSector;
80d26cb2 1877 info->resync_start = 0;
624c5ad4 1878 info->reshape_active = 0;
6e75048b 1879 info->recovery_blocked = 0;
80d26cb2
NB
1880 if (!(ddf->virt->entries[info->container_member].state
1881 & DDF_state_inconsistent) &&
1882 (ddf->virt->entries[info->container_member].init_state
1883 & DDF_initstate_mask)
1884 == DDF_init_full)
b7528a20 1885 info->resync_start = MaxSector;
80d26cb2 1886
a322f70c
DW
1887 uuid_from_super_ddf(st, info->uuid);
1888
f35f2525
N
1889 info->array.major_version = -1;
1890 info->array.minor_version = -2;
9b63e648 1891 sprintf(info->text_version, "/%s/%d",
4dd2df09 1892 st->container_devnm,
9b63e648 1893 info->container_member);
a67dd8cc 1894 info->safe_mode_delay = 200;
159c3a1a 1895
db42fa9b
N
1896 memcpy(info->name, ddf->virt->entries[info->container_member].name, 16);
1897 info->name[16]=0;
1898 for(j=0; j<16; j++)
1899 if (info->name[j] == ' ')
1900 info->name[j] = 0;
a5d85af7
N
1901
1902 if (map)
1903 for (j = 0; j < map_disks; j++) {
1904 map[j] = 0;
1905 if (j < info->array.raid_disks) {
1906 int i = find_phys(ddf, vc->conf.phys_refnum[j]);
613b0d17 1907 if (i >= 0 &&
a5d85af7
N
1908 (__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Online) &&
1909 !(__be16_to_cpu(ddf->phys->entries[i].state) & DDF_Failed))
1910 map[i] = 1;
1911 }
1912 }
a322f70c
DW
1913}
1914
1915static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1916 char *update,
1917 char *devname, int verbose,
1918 int uuid_set, char *homehost)
1919{
1920 /* For 'assemble' and 'force' we need to return non-zero if any
1921 * change was made. For others, the return value is ignored.
1922 * Update options are:
1923 * force-one : This device looks a bit old but needs to be included,
1924 * update age info appropriately.
1925 * assemble: clear any 'faulty' flag to allow this device to
1926 * be assembled.
1927 * force-array: Array is degraded but being forced, mark it clean
1928 * if that will be needed to assemble it.
1929 *
1930 * newdev: not used ????
1931 * grow: Array has gained a new device - this is currently for
1932 * linear only
1933 * resync: mark as dirty so a resync will happen.
59e36268 1934 * uuid: Change the uuid of the array to match what is given
a322f70c
DW
1935 * homehost: update the recorded homehost
1936 * name: update the name - preserving the homehost
1937 * _reshape_progress: record new reshape_progress position.
1938 *
1939 * Following are not relevant for this version:
1940 * sparc2.2 : update from old dodgey metadata
1941 * super-minor: change the preferred_minor number
1942 * summaries: update redundant counters.
1943 */
1944 int rv = 0;
1945// struct ddf_super *ddf = st->sb;
7a7cc504 1946// struct vd_config *vd = find_vdcr(ddf, info->container_member);
a322f70c
DW
1947// struct virtual_entry *ve = find_ve(ddf);
1948
a322f70c
DW
1949 /* we don't need to handle "force-*" or "assemble" as
1950 * there is no need to 'trick' the kernel. We the metadata is
1951 * first updated to activate the array, all the implied modifications
1952 * will just happen.
1953 */
1954
1955 if (strcmp(update, "grow") == 0) {
1956 /* FIXME */
1e2b2765 1957 } else if (strcmp(update, "resync") == 0) {
a322f70c 1958// info->resync_checkpoint = 0;
1e2b2765 1959 } else if (strcmp(update, "homehost") == 0) {
a322f70c
DW
1960 /* homehost is stored in controller->vendor_data,
1961 * or it is when we are the vendor
1962 */
1963// if (info->vendor_is_local)
1964// strcpy(ddf->controller.vendor_data, homehost);
1e2b2765 1965 rv = -1;
f49208ec 1966 } else if (strcmp(update, "name") == 0) {
a322f70c
DW
1967 /* name is stored in virtual_entry->name */
1968// memset(ve->name, ' ', 16);
1969// strncpy(ve->name, info->name, 16);
1e2b2765 1970 rv = -1;
f49208ec 1971 } else if (strcmp(update, "_reshape_progress") == 0) {
a322f70c 1972 /* We don't support reshape yet */
f49208ec
N
1973 } else if (strcmp(update, "assemble") == 0 ) {
1974 /* Do nothing, just succeed */
1975 rv = 0;
1e2b2765
N
1976 } else
1977 rv = -1;
a322f70c
DW
1978
1979// update_all_csum(ddf);
1980
1981 return rv;
1982}
1983
5f8097be
NB
1984static void make_header_guid(char *guid)
1985{
1986 __u32 stamp;
5f8097be
NB
1987 /* Create a DDF Header of Virtual Disk GUID */
1988
1989 /* 24 bytes of fiction required.
1990 * first 8 are a 'vendor-id' - "Linux-MD"
1991 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1992 * Remaining 8 random number plus timestamp
1993 */
1994 memcpy(guid, T10, sizeof(T10));
1995 stamp = __cpu_to_be32(0xdeadbeef);
1996 memcpy(guid+8, &stamp, 4);
1997 stamp = __cpu_to_be32(0);
1998 memcpy(guid+12, &stamp, 4);
1999 stamp = __cpu_to_be32(time(0) - DECADE);
2000 memcpy(guid+16, &stamp, 4);
bfb7ea78 2001 stamp = random32();
5f8097be 2002 memcpy(guid+20, &stamp, 4);
5f8097be 2003}
59e36268 2004
fb9d0acb 2005static unsigned int find_unused_vde(const struct ddf_super *ddf)
2006{
2007 unsigned int i;
2008 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
2009 if (all_ff(ddf->virt->entries[i].guid))
2010 return i;
2011 }
2012 return DDF_NOTFOUND;
2013}
2014
2015static unsigned int find_vde_by_name(const struct ddf_super *ddf,
2016 const char *name)
2017{
2018 unsigned int i;
2019 if (name == NULL)
2020 return DDF_NOTFOUND;
2021 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++) {
2022 if (all_ff(ddf->virt->entries[i].guid))
2023 continue;
2024 if (!strncmp(name, ddf->virt->entries[i].name,
2025 sizeof(ddf->virt->entries[i].name)))
2026 return i;
2027 }
2028 return DDF_NOTFOUND;
2029}
2030
2031static unsigned int find_vde_by_guid(const struct ddf_super *ddf,
2032 const char *guid)
2033{
2034 unsigned int i;
2035 if (guid == NULL || all_ff(guid))
2036 return DDF_NOTFOUND;
2037 for (i = 0; i < __be16_to_cpu(ddf->virt->max_vdes); i++)
2038 if (!memcmp(ddf->virt->entries[i].guid, guid, DDF_GUID_LEN))
2039 return i;
2040 return DDF_NOTFOUND;
2041}
2042
78e44928
NB
2043static int init_super_ddf_bvd(struct supertype *st,
2044 mdu_array_info_t *info,
2045 unsigned long long size,
2046 char *name, char *homehost,
83cd1e97 2047 int *uuid, unsigned long long data_offset);
78e44928 2048
a322f70c
DW
2049static int init_super_ddf(struct supertype *st,
2050 mdu_array_info_t *info,
2051 unsigned long long size, char *name, char *homehost,
83cd1e97 2052 int *uuid, unsigned long long data_offset)
a322f70c
DW
2053{
2054 /* This is primarily called by Create when creating a new array.
2055 * We will then get add_to_super called for each component, and then
2056 * write_init_super called to write it out to each device.
2057 * For DDF, Create can create on fresh devices or on a pre-existing
2058 * array.
2059 * To create on a pre-existing array a different method will be called.
2060 * This one is just for fresh drives.
2061 *
2062 * We need to create the entire 'ddf' structure which includes:
2063 * DDF headers - these are easy.
2064 * Controller data - a Sector describing this controller .. not that
2065 * this is a controller exactly.
2066 * Physical Disk Record - one entry per device, so
2067 * leave plenty of space.
2068 * Virtual Disk Records - again, just leave plenty of space.
2069 * This just lists VDs, doesn't give details
2070 * Config records - describes the VDs that use this disk
2071 * DiskData - describes 'this' device.
2072 * BadBlockManagement - empty
2073 * Diag Space - empty
2074 * Vendor Logs - Could we put bitmaps here?
2075 *
2076 */
2077 struct ddf_super *ddf;
2078 char hostname[17];
2079 int hostlen;
a322f70c
DW
2080 int max_phys_disks, max_virt_disks;
2081 unsigned long long sector;
2082 int clen;
2083 int i;
2084 int pdsize, vdsize;
2085 struct phys_disk *pd;
2086 struct virtual_disk *vd;
2087
83cd1e97 2088 if (data_offset != INVALID_SECTORS) {
ed503f89 2089 pr_err("data-offset not supported by DDF\n");
83cd1e97
N
2090 return 0;
2091 }
2092
78e44928 2093 if (st->sb)
83cd1e97
N
2094 return init_super_ddf_bvd(st, info, size, name, homehost, uuid,
2095 data_offset);
ba7eb04f 2096
3d2c4fc7 2097 if (posix_memalign((void**)&ddf, 512, sizeof(*ddf)) != 0) {
e7b84f9d 2098 pr_err("%s could not allocate superblock\n", __func__);
3d2c4fc7
DW
2099 return 0;
2100 }
6264b437 2101 memset(ddf, 0, sizeof(*ddf));
a322f70c
DW
2102 ddf->dlist = NULL; /* no physical disks yet */
2103 ddf->conflist = NULL; /* No virtual disks yet */
955e9ea1
DW
2104 st->sb = ddf;
2105
2106 if (info == NULL) {
2107 /* zeroing superblock */
2108 return 0;
2109 }
a322f70c
DW
2110
2111 /* At least 32MB *must* be reserved for the ddf. So let's just
2112 * start 32MB from the end, and put the primary header there.
2113 * Don't do secondary for now.
2114 * We don't know exactly where that will be yet as it could be
2115 * different on each device. To just set up the lengths.
2116 *
2117 */
2118
2119 ddf->anchor.magic = DDF_HEADER_MAGIC;
5f8097be 2120 make_header_guid(ddf->anchor.guid);
a322f70c 2121
59e36268 2122 memcpy(ddf->anchor.revision, DDF_REVISION_2, 8);
a322f70c
DW
2123 ddf->anchor.seq = __cpu_to_be32(1);
2124 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
2125 ddf->anchor.openflag = 0xFF;
2126 ddf->anchor.foreignflag = 0;
2127 ddf->anchor.enforcegroups = 0; /* Is this best?? */
2128 ddf->anchor.pad0 = 0xff;
2129 memset(ddf->anchor.pad1, 0xff, 12);
2130 memset(ddf->anchor.header_ext, 0xff, 32);
2131 ddf->anchor.primary_lba = ~(__u64)0;
2132 ddf->anchor.secondary_lba = ~(__u64)0;
2133 ddf->anchor.type = DDF_HEADER_ANCHOR;
2134 memset(ddf->anchor.pad2, 0xff, 3);
2135 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
2136 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
2137 of 32M reserved.. */
2138 max_phys_disks = 1023; /* Should be enough */
2139 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
2140 max_virt_disks = 255;
2141 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
2142 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
2143 ddf->max_part = 64;
8c3b8c2c 2144 ddf->mppe = 256;
59e36268
NB
2145 ddf->conf_rec_len = 1 + ROUND_UP(ddf->mppe * (4+8), 512)/512;
2146 ddf->anchor.config_record_len = __cpu_to_be16(ddf->conf_rec_len);
2147 ddf->anchor.max_primary_element_entries = __cpu_to_be16(ddf->mppe);
a322f70c 2148 memset(ddf->anchor.pad3, 0xff, 54);
a322f70c
DW
2149 /* controller sections is one sector long immediately
2150 * after the ddf header */
2151 sector = 1;
2152 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
2153 ddf->anchor.controller_section_length = __cpu_to_be32(1);
2154 sector += 1;
2155
2156 /* phys is 8 sectors after that */
2157 pdsize = ROUND_UP(sizeof(struct phys_disk) +
2158 sizeof(struct phys_disk_entry)*max_phys_disks,
2159 512);
2160 switch(pdsize/512) {
2161 case 2: case 8: case 32: case 128: case 512: break;
2162 default: abort();
2163 }
2164 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
2165 ddf->anchor.phys_section_length =
2166 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
2167 sector += pdsize/512;
2168
2169 /* virt is another 32 sectors */
2170 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
2171 sizeof(struct virtual_entry) * max_virt_disks,
2172 512);
2173 switch(vdsize/512) {
2174 case 2: case 8: case 32: case 128: case 512: break;
2175 default: abort();
2176 }
2177 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
2178 ddf->anchor.virt_section_length =
2179 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
2180 sector += vdsize/512;
2181
59e36268 2182 clen = ddf->conf_rec_len * (ddf->max_part+1);
a322f70c
DW
2183 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
2184 ddf->anchor.config_section_length = __cpu_to_be32(clen);
2185 sector += clen;
2186
2187 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
2188 ddf->anchor.data_section_length = __cpu_to_be32(1);
2189 sector += 1;
2190
2191 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
2192 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
2193 ddf->anchor.diag_space_length = __cpu_to_be32(0);
2194 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
2195 ddf->anchor.vendor_length = __cpu_to_be32(0);
2196 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
2197
2198 memset(ddf->anchor.pad4, 0xff, 256);
2199
2200 memcpy(&ddf->primary, &ddf->anchor, 512);
2201 memcpy(&ddf->secondary, &ddf->anchor, 512);
2202
2203 ddf->primary.openflag = 1; /* I guess.. */
2204 ddf->primary.type = DDF_HEADER_PRIMARY;
2205
2206 ddf->secondary.openflag = 1; /* I guess.. */
2207 ddf->secondary.type = DDF_HEADER_SECONDARY;
2208
2209 ddf->active = &ddf->primary;
2210
2211 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
2212
2213 /* 24 more bytes of fiction required.
2214 * first 8 are a 'vendor-id' - "Linux-MD"
2215 * Remaining 16 are serial number.... maybe a hostname would do?
2216 */
2217 memcpy(ddf->controller.guid, T10, sizeof(T10));
1ba6bff9
DW
2218 gethostname(hostname, sizeof(hostname));
2219 hostname[sizeof(hostname) - 1] = 0;
a322f70c
DW
2220 hostlen = strlen(hostname);
2221 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
2222 for (i = strlen(T10) ; i+hostlen < 24; i++)
2223 ddf->controller.guid[i] = ' ';
2224
2225 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
2226 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
2227 ddf->controller.type.sub_vendor_id = 0;
2228 ddf->controller.type.sub_device_id = 0;
2229 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
2230 memset(ddf->controller.pad, 0xff, 8);
2231 memset(ddf->controller.vendor_data, 0xff, 448);
a9e1c11d
N
2232 if (homehost && strlen(homehost) < 440)
2233 strcpy((char*)ddf->controller.vendor_data, homehost);
a322f70c 2234
3d2c4fc7 2235 if (posix_memalign((void**)&pd, 512, pdsize) != 0) {
e7b84f9d 2236 pr_err("%s could not allocate pd\n", __func__);
3d2c4fc7
DW
2237 return 0;
2238 }
6416d527 2239 ddf->phys = pd;
a322f70c
DW
2240 ddf->pdsize = pdsize;
2241
2242 memset(pd, 0xff, pdsize);
2243 memset(pd, 0, sizeof(*pd));
076515ba 2244 pd->magic = DDF_PHYS_RECORDS_MAGIC;
a322f70c
DW
2245 pd->used_pdes = __cpu_to_be16(0);
2246 pd->max_pdes = __cpu_to_be16(max_phys_disks);
2247 memset(pd->pad, 0xff, 52);
2248
3d2c4fc7 2249 if (posix_memalign((void**)&vd, 512, vdsize) != 0) {
e7b84f9d 2250 pr_err("%s could not allocate vd\n", __func__);
3d2c4fc7
DW
2251 return 0;
2252 }
6416d527 2253 ddf->virt = vd;
a322f70c
DW
2254 ddf->vdsize = vdsize;
2255 memset(vd, 0, vdsize);
2256 vd->magic = DDF_VIRT_RECORDS_MAGIC;
2257 vd->populated_vdes = __cpu_to_be16(0);
2258 vd->max_vdes = __cpu_to_be16(max_virt_disks);
2259 memset(vd->pad, 0xff, 52);
2260
5f8097be
NB
2261 for (i=0; i<max_virt_disks; i++)
2262 memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
2263
a322f70c 2264 st->sb = ddf;
7d5a7ff3 2265 ddf_set_updates_pending(ddf);
a322f70c
DW
2266 return 1;
2267}
2268
5f8097be
NB
2269static int chunk_to_shift(int chunksize)
2270{
2271 return ffs(chunksize/512)-1;
2272}
2273
0e600426 2274#ifndef MDASSEMBLE
59e36268
NB
2275struct extent {
2276 unsigned long long start, size;
2277};
78e44928 2278static int cmp_extent(const void *av, const void *bv)
59e36268
NB
2279{
2280 const struct extent *a = av;
2281 const struct extent *b = bv;
2282 if (a->start < b->start)
2283 return -1;
2284 if (a->start > b->start)
2285 return 1;
2286 return 0;
2287}
2288
78e44928 2289static struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
59e36268
NB
2290{
2291 /* find a list of used extents on the give physical device
2292 * (dnum) of the given ddf.
2293 * Return a malloced array of 'struct extent'
2294
613b0d17 2295 * FIXME ignore DDF_Legacy devices?
59e36268
NB
2296
2297 */
2298 struct extent *rv;
2299 int n = 0;
fcc22180 2300 unsigned int i;
59e36268 2301
503975b9 2302 rv = xmalloc(sizeof(struct extent) * (ddf->max_part + 2));
59e36268
NB
2303
2304 for (i = 0; i < ddf->max_part; i++) {
fcc22180 2305 const struct vd_config *bvd;
2306 unsigned int ibvd;
59e36268 2307 struct vcl *v = dl->vlist[i];
fcc22180 2308 if (v == NULL ||
2309 get_pd_index_from_refnum(v, dl->disk.refnum, ddf->mppe,
2310 &bvd, &ibvd) == DDF_NOTFOUND)
59e36268 2311 continue;
fcc22180 2312 rv[n].start = __be64_to_cpu(LBA_OFFSET(ddf, bvd)[ibvd]);
2313 rv[n].size = __be64_to_cpu(bvd->blocks);
2314 n++;
59e36268
NB
2315 }
2316 qsort(rv, n, sizeof(*rv), cmp_extent);
2317
2318 rv[n].start = __be64_to_cpu(ddf->phys->entries[dl->pdnum].config_size);
2319 rv[n].size = 0;
2320 return rv;
2321}
0e600426 2322#endif
59e36268 2323
5f8097be
NB
2324static int init_super_ddf_bvd(struct supertype *st,
2325 mdu_array_info_t *info,
2326 unsigned long long size,
2327 char *name, char *homehost,
83cd1e97 2328 int *uuid, unsigned long long data_offset)
5f8097be
NB
2329{
2330 /* We are creating a BVD inside a pre-existing container.
2331 * so st->sb is already set.
2332 * We need to create a new vd_config and a new virtual_entry
2333 */
2334 struct ddf_super *ddf = st->sb;
5aaf6c7b 2335 unsigned int venum, i;
5f8097be
NB
2336 struct virtual_entry *ve;
2337 struct vcl *vcl;
2338 struct vd_config *vc;
5f8097be 2339
fb9d0acb 2340 if (find_vde_by_name(ddf, name) != DDF_NOTFOUND) {
2341 pr_err("This ddf already has an array called %s\n", name);
5f8097be
NB
2342 return 0;
2343 }
fb9d0acb 2344 venum = find_unused_vde(ddf);
2345 if (venum == DDF_NOTFOUND) {
2346 pr_err("Cannot find spare slot for virtual disk\n");
5f8097be
NB
2347 return 0;
2348 }
2349 ve = &ddf->virt->entries[venum];
2350
2351 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
2352 * timestamp, random number
2353 */
2354 make_header_guid(ve->guid);
2355 ve->unit = __cpu_to_be16(info->md_minor);
2356 ve->pad0 = 0xFFFF;
2357 ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
2358 ve->type = 0;
7a7cc504
NB
2359 ve->state = DDF_state_degraded; /* Will be modified as devices are added */
2360 if (info->state & 1) /* clean */
2361 ve->init_state = DDF_init_full;
2362 else
2363 ve->init_state = DDF_init_not;
2364
5f8097be
NB
2365 memset(ve->pad1, 0xff, 14);
2366 memset(ve->name, ' ', 16);
2367 if (name)
2368 strncpy(ve->name, name, 16);
2369 ddf->virt->populated_vdes =
2370 __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
2371
2372 /* Now create a new vd_config */
3d2c4fc7
DW
2373 if (posix_memalign((void**)&vcl, 512,
2374 (offsetof(struct vcl, conf) + ddf->conf_rec_len * 512)) != 0) {
e7b84f9d 2375 pr_err("%s could not allocate vd_config\n", __func__);
3d2c4fc7
DW
2376 return 0;
2377 }
59e36268
NB
2378 vcl->vcnum = venum;
2379 vcl->block_sizes = NULL; /* FIXME not for CONCAT */
5f8097be
NB
2380 vc = &vcl->conf;
2381
2382 vc->magic = DDF_VD_CONF_MAGIC;
2383 memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
2384 vc->timestamp = __cpu_to_be32(time(0)-DECADE);
2385 vc->seqnum = __cpu_to_be32(1);
2386 memset(vc->pad0, 0xff, 24);
5f8097be 2387 vc->chunk_shift = chunk_to_shift(info->chunk_size);
a3163bf0 2388 if (layout_md2ddf(info, vc) == -1 ||
2389 __be16_to_cpu(vc->prim_elmnt_count) > ddf->mppe) {
2390 pr_err("%s: unsupported RAID level/layout %d/%d with %d disks\n",
2391 __func__, info->level, info->layout, info->raid_disks);
2392 free(vcl);
2393 return 0;
2394 }
5f8097be 2395 vc->sec_elmnt_seq = 0;
3c48f7be 2396 if (alloc_other_bvds(ddf, vcl) != 0) {
2397 pr_err("%s could not allocate other bvds\n",
2398 __func__);
2399 free(vcl);
2400 return 0;
2401 }
5f8097be
NB
2402 vc->blocks = __cpu_to_be64(info->size * 2);
2403 vc->array_blocks = __cpu_to_be64(
2404 calc_array_size(info->level, info->raid_disks, info->layout,
2405 info->chunk_size, info->size*2));
2406 memset(vc->pad1, 0xff, 8);
2407 vc->spare_refs[0] = 0xffffffff;
2408 vc->spare_refs[1] = 0xffffffff;
2409 vc->spare_refs[2] = 0xffffffff;
2410 vc->spare_refs[3] = 0xffffffff;
2411 vc->spare_refs[4] = 0xffffffff;
2412 vc->spare_refs[5] = 0xffffffff;
2413 vc->spare_refs[6] = 0xffffffff;
2414 vc->spare_refs[7] = 0xffffffff;
2415 memset(vc->cache_pol, 0, 8);
2416 vc->bg_rate = 0x80;
2417 memset(vc->pad2, 0xff, 3);
2418 memset(vc->pad3, 0xff, 52);
2419 memset(vc->pad4, 0xff, 192);
2420 memset(vc->v0, 0xff, 32);
2421 memset(vc->v1, 0xff, 32);
2422 memset(vc->v2, 0xff, 16);
2423 memset(vc->v3, 0xff, 16);
2424 memset(vc->vendor, 0xff, 32);
598f0d58 2425
8c3b8c2c 2426 memset(vc->phys_refnum, 0xff, 4*ddf->mppe);
e5a2a3cf 2427 memset(vc->phys_refnum+ddf->mppe, 0x00, 8*ddf->mppe);
5f8097be 2428
5aaf6c7b 2429 for (i = 1; i < vc->sec_elmnt_count; i++) {
2430 memcpy(vcl->other_bvds[i-1], vc, ddf->conf_rec_len * 512);
2431 vcl->other_bvds[i-1]->sec_elmnt_seq = i;
2432 }
2433
5f8097be
NB
2434 vcl->next = ddf->conflist;
2435 ddf->conflist = vcl;
d2ca6449 2436 ddf->currentconf = vcl;
7d5a7ff3 2437 ddf_set_updates_pending(ddf);
5f8097be
NB
2438 return 1;
2439}
2440
0e600426 2441#ifndef MDASSEMBLE
5f8097be
NB
2442static void add_to_super_ddf_bvd(struct supertype *st,
2443 mdu_disk_info_t *dk, int fd, char *devname)
2444{
2445 /* fd and devname identify a device with-in the ddf container (st).
2446 * dk identifies a location in the new BVD.
2447 * We need to find suitable free space in that device and update
2448 * the phys_refnum and lba_offset for the newly created vd_config.
2449 * We might also want to update the type in the phys_disk
5575e7d9 2450 * section.
8592f29d
N
2451 *
2452 * Alternately: fd == -1 and we have already chosen which device to
2453 * use and recorded in dlist->raid_disk;
5f8097be
NB
2454 */
2455 struct dl *dl;
2456 struct ddf_super *ddf = st->sb;
2457 struct vd_config *vc;
2458 __u64 *lba_offset;
f21e18ca
N
2459 unsigned int working;
2460 unsigned int i;
59e36268
NB
2461 unsigned long long blocks, pos, esize;
2462 struct extent *ex;
5f8097be 2463
8592f29d
N
2464 if (fd == -1) {
2465 for (dl = ddf->dlist; dl ; dl = dl->next)
2466 if (dl->raiddisk == dk->raid_disk)
2467 break;
2468 } else {
2469 for (dl = ddf->dlist; dl ; dl = dl->next)
2470 if (dl->major == dk->major &&
2471 dl->minor == dk->minor)
2472 break;
2473 }
5f8097be
NB
2474 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
2475 return;
2476
d2ca6449 2477 vc = &ddf->currentconf->conf;
57a66662 2478 lba_offset = LBA_OFFSET(ddf, &ddf->currentconf->conf);
59e36268
NB
2479
2480 ex = get_extents(ddf, dl);
2481 if (!ex)
2482 return;
2483
2484 i = 0; pos = 0;
2485 blocks = __be64_to_cpu(vc->blocks);
d2ca6449
NB
2486 if (ddf->currentconf->block_sizes)
2487 blocks = ddf->currentconf->block_sizes[dk->raid_disk];
59e36268
NB
2488
2489 do {
2490 esize = ex[i].start - pos;
2491 if (esize >= blocks)
2492 break;
2493 pos = ex[i].start + ex[i].size;
2494 i++;
2495 } while (ex[i-1].size);
2496
2497 free(ex);
2498 if (esize < blocks)
2499 return;
2500
d2ca6449 2501 ddf->currentdev = dk->raid_disk;
5f8097be 2502 vc->phys_refnum[dk->raid_disk] = dl->disk.refnum;
59e36268 2503 lba_offset[dk->raid_disk] = __cpu_to_be64(pos);
5f8097be 2504
f21e18ca 2505 for (i = 0; i < ddf->max_part ; i++)
5575e7d9
NB
2506 if (dl->vlist[i] == NULL)
2507 break;
2508 if (i == ddf->max_part)
2509 return;
d2ca6449 2510 dl->vlist[i] = ddf->currentconf;
5f8097be 2511
8592f29d
N
2512 if (fd >= 0)
2513 dl->fd = fd;
2514 if (devname)
2515 dl->devname = devname;
7a7cc504
NB
2516
2517 /* Check how many working raid_disks, and if we can mark
2518 * array as optimal yet
2519 */
2520 working = 0;
5575e7d9 2521
f21e18ca 2522 for (i = 0; i < __be16_to_cpu(vc->prim_elmnt_count); i++)
7a7cc504
NB
2523 if (vc->phys_refnum[i] != 0xffffffff)
2524 working++;
59e36268 2525
5575e7d9 2526 /* Find which virtual_entry */
d2ca6449 2527 i = ddf->currentconf->vcnum;
7a7cc504 2528 if (working == __be16_to_cpu(vc->prim_elmnt_count))
5575e7d9
NB
2529 ddf->virt->entries[i].state =
2530 (ddf->virt->entries[i].state & ~DDF_state_mask)
7a7cc504
NB
2531 | DDF_state_optimal;
2532
2533 if (vc->prl == DDF_RAID6 &&
2534 working+1 == __be16_to_cpu(vc->prim_elmnt_count))
5575e7d9
NB
2535 ddf->virt->entries[i].state =
2536 (ddf->virt->entries[i].state & ~DDF_state_mask)
7a7cc504 2537 | DDF_state_part_optimal;
5575e7d9
NB
2538
2539 ddf->phys->entries[dl->pdnum].type &= ~__cpu_to_be16(DDF_Global_Spare);
2540 ddf->phys->entries[dl->pdnum].type |= __cpu_to_be16(DDF_Active_in_VD);
7d5a7ff3 2541 ddf_set_updates_pending(ddf);
5f8097be
NB
2542}
2543
a322f70c
DW
2544/* add a device to a container, either while creating it or while
2545 * expanding a pre-existing container
2546 */
f20c3968 2547static int add_to_super_ddf(struct supertype *st,
72ca9bcf
N
2548 mdu_disk_info_t *dk, int fd, char *devname,
2549 unsigned long long data_offset)
a322f70c
DW
2550{
2551 struct ddf_super *ddf = st->sb;
2552 struct dl *dd;
2553 time_t now;
2554 struct tm *tm;
2555 unsigned long long size;
2556 struct phys_disk_entry *pde;
f21e18ca 2557 unsigned int n, i;
a322f70c 2558 struct stat stb;
90fa1a29 2559 __u32 *tptr;
a322f70c 2560
78e44928
NB
2561 if (ddf->currentconf) {
2562 add_to_super_ddf_bvd(st, dk, fd, devname);
f20c3968 2563 return 0;
78e44928
NB
2564 }
2565
a322f70c
DW
2566 /* This is device numbered dk->number. We need to create
2567 * a phys_disk entry and a more detailed disk_data entry.
2568 */
2569 fstat(fd, &stb);
3d2c4fc7
DW
2570 if (posix_memalign((void**)&dd, 512,
2571 sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part) != 0) {
e7b84f9d
N
2572 pr_err("%s could allocate buffer for new disk, aborting\n",
2573 __func__);
f20c3968 2574 return 1;
3d2c4fc7 2575 }
a322f70c
DW
2576 dd->major = major(stb.st_rdev);
2577 dd->minor = minor(stb.st_rdev);
2578 dd->devname = devname;
a322f70c 2579 dd->fd = fd;
b2280677 2580 dd->spare = NULL;
a322f70c
DW
2581
2582 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
2583 now = time(0);
2584 tm = localtime(&now);
2585 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
2586 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
90fa1a29
JS
2587 tptr = (__u32 *)(dd->disk.guid + 16);
2588 *tptr++ = random32();
2589 *tptr = random32();
a322f70c 2590
59e36268
NB
2591 do {
2592 /* Cannot be bothered finding a CRC of some irrelevant details*/
bfb7ea78 2593 dd->disk.refnum = random32();
f21e18ca
N
2594 for (i = __be16_to_cpu(ddf->active->max_pd_entries);
2595 i > 0; i--)
2596 if (ddf->phys->entries[i-1].refnum == dd->disk.refnum)
59e36268 2597 break;
f21e18ca 2598 } while (i > 0);
59e36268 2599
a322f70c
DW
2600 dd->disk.forced_ref = 1;
2601 dd->disk.forced_guid = 1;
2602 memset(dd->disk.vendor, ' ', 32);
2603 memcpy(dd->disk.vendor, "Linux", 5);
2604 memset(dd->disk.pad, 0xff, 442);
b2280677 2605 for (i = 0; i < ddf->max_part ; i++)
a322f70c
DW
2606 dd->vlist[i] = NULL;
2607
2608 n = __be16_to_cpu(ddf->phys->used_pdes);
2609 pde = &ddf->phys->entries[n];
5575e7d9
NB
2610 dd->pdnum = n;
2611
2cc2983d
N
2612 if (st->update_tail) {
2613 int len = (sizeof(struct phys_disk) +
2614 sizeof(struct phys_disk_entry));
2615 struct phys_disk *pd;
2616
503975b9 2617 pd = xmalloc(len);
2cc2983d
N
2618 pd->magic = DDF_PHYS_RECORDS_MAGIC;
2619 pd->used_pdes = __cpu_to_be16(n);
2620 pde = &pd->entries[0];
2621 dd->mdupdate = pd;
2622 } else {
2623 n++;
2624 ddf->phys->used_pdes = __cpu_to_be16(n);
2625 }
a322f70c
DW
2626
2627 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
2628 pde->refnum = dd->disk.refnum;
5575e7d9 2629 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID | DDF_Global_Spare);
a322f70c
DW
2630 pde->state = __cpu_to_be16(DDF_Online);
2631 get_dev_size(fd, NULL, &size);
2632 /* We are required to reserve 32Meg, and record the size in sectors */
2633 pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512);
2634 sprintf(pde->path, "%17.17s","Information: nil") ;
2635 memset(pde->pad, 0xff, 6);
2636
d2ca6449 2637 dd->size = size >> 9;
2cc2983d
N
2638 if (st->update_tail) {
2639 dd->next = ddf->add_list;
2640 ddf->add_list = dd;
2641 } else {
2642 dd->next = ddf->dlist;
2643 ddf->dlist = dd;
7d5a7ff3 2644 ddf_set_updates_pending(ddf);
2cc2983d 2645 }
f20c3968
DW
2646
2647 return 0;
a322f70c
DW
2648}
2649
4dd968cc
N
2650static int remove_from_super_ddf(struct supertype *st, mdu_disk_info_t *dk)
2651{
2652 struct ddf_super *ddf = st->sb;
2653 struct dl *dl;
2654
2655 /* mdmon has noticed that this disk (dk->major/dk->minor) has
2656 * disappeared from the container.
2657 * We need to arrange that it disappears from the metadata and
2658 * internal data structures too.
2659 * Most of the work is done by ddf_process_update which edits
2660 * the metadata and closes the file handle and attaches the memory
2661 * where free_updates will free it.
2662 */
2663 for (dl = ddf->dlist; dl ; dl = dl->next)
2664 if (dl->major == dk->major &&
2665 dl->minor == dk->minor)
2666 break;
2667 if (!dl)
2668 return -1;
2669
2670 if (st->update_tail) {
2671 int len = (sizeof(struct phys_disk) +
2672 sizeof(struct phys_disk_entry));
2673 struct phys_disk *pd;
2674
503975b9 2675 pd = xmalloc(len);
4dd968cc
N
2676 pd->magic = DDF_PHYS_RECORDS_MAGIC;
2677 pd->used_pdes = __cpu_to_be16(dl->pdnum);
2678 pd->entries[0].state = __cpu_to_be16(DDF_Missing);
2679 append_metadata_update(st, pd, len);
2680 }
2681 return 0;
2682}
2683
a322f70c
DW
2684/*
2685 * This is the write_init_super method for a ddf container. It is
2686 * called when creating a container or adding another device to a
2687 * container.
2688 */
42d5dfd9 2689#define NULL_CONF_SZ 4096
18a2f463 2690
7f798aca 2691static int __write_ddf_structure(struct dl *d, struct ddf_super *ddf, __u8 type,
2692 char *null_aligned)
a322f70c 2693{
7f798aca 2694 unsigned long long sector;
2695 struct ddf_header *header;
2696 int fd, i, n_config, conf_size;
a4057a88 2697 int ret = 0;
7f798aca 2698
2699 fd = d->fd;
2700
2701 switch (type) {
2702 case DDF_HEADER_PRIMARY:
2703 header = &ddf->primary;
2704 sector = __be64_to_cpu(header->primary_lba);
2705 break;
2706 case DDF_HEADER_SECONDARY:
2707 header = &ddf->secondary;
2708 sector = __be64_to_cpu(header->secondary_lba);
2709 break;
2710 default:
2711 return 0;
2712 }
2713
2714 header->type = type;
a4057a88 2715 header->openflag = 1;
7f798aca 2716 header->crc = calc_crc(header, 512);
2717
2718 lseek64(fd, sector<<9, 0);
2719 if (write(fd, header, 512) < 0)
a4057a88 2720 goto out;
7f798aca 2721
2722 ddf->controller.crc = calc_crc(&ddf->controller, 512);
2723 if (write(fd, &ddf->controller, 512) < 0)
a4057a88 2724 goto out;
a322f70c 2725
7f798aca 2726 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
2727 if (write(fd, ddf->phys, ddf->pdsize) < 0)
a4057a88 2728 goto out;
7f798aca 2729 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
2730 if (write(fd, ddf->virt, ddf->vdsize) < 0)
a4057a88 2731 goto out;
7f798aca 2732
2733 /* Now write lots of config records. */
2734 n_config = ddf->max_part;
2735 conf_size = ddf->conf_rec_len * 512;
2736 for (i = 0 ; i <= n_config ; i++) {
e3c2a365 2737 struct vcl *c;
2738 struct vd_config *vdc = NULL;
2739 if (i == n_config) {
7f798aca 2740 c = (struct vcl *)d->spare;
e3c2a365 2741 if (c)
2742 vdc = &c->conf;
2743 } else {
2744 unsigned int dummy;
2745 c = d->vlist[i];
2746 if (c)
2747 get_pd_index_from_refnum(
2748 c, d->disk.refnum,
2749 ddf->mppe,
2750 (const struct vd_config **)&vdc,
2751 &dummy);
2752 }
7f798aca 2753 if (c) {
dacf3dc5 2754 vdc->seqnum = header->seq;
e3c2a365 2755 vdc->crc = calc_crc(vdc, conf_size);
2756 if (write(fd, vdc, conf_size) < 0)
7f798aca 2757 break;
2758 } else {
2759 unsigned int togo = conf_size;
2760 while (togo > NULL_CONF_SZ) {
2761 if (write(fd, null_aligned, NULL_CONF_SZ) < 0)
2762 break;
2763 togo -= NULL_CONF_SZ;
2764 }
2765 if (write(fd, null_aligned, togo) < 0)
2766 break;
2767 }
2768 }
2769 if (i <= n_config)
a4057a88 2770 goto out;
7f798aca 2771
2772 d->disk.crc = calc_crc(&d->disk, 512);
2773 if (write(fd, &d->disk, 512) < 0)
a4057a88 2774 goto out;
7f798aca 2775
a4057a88 2776 ret = 1;
2777out:
2778 header->openflag = 0;
2779 header->crc = calc_crc(header, 512);
2780
2781 lseek64(fd, sector<<9, 0);
2782 if (write(fd, header, 512) < 0)
2783 ret = 0;
2784
2785 return ret;
7f798aca 2786}
2787
2788static int __write_init_super_ddf(struct supertype *st)
2789{
a322f70c 2790 struct ddf_super *ddf = st->sb;
a322f70c 2791 struct dl *d;
175593bf
DW
2792 int attempts = 0;
2793 int successes = 0;
7f798aca 2794 unsigned long long size;
42d5dfd9 2795 char *null_aligned;
0175cbf6 2796 __u32 seq;
42d5dfd9 2797
7d5a7ff3 2798 pr_state(ddf, __func__);
42d5dfd9
JS
2799 if (posix_memalign((void**)&null_aligned, 4096, NULL_CONF_SZ) != 0) {
2800 return -ENOMEM;
2801 }
2802 memset(null_aligned, 0xff, NULL_CONF_SZ);
a322f70c 2803
dc9e279c 2804 seq = ddf->active->seq + 1;
0175cbf6 2805
175593bf
DW
2806 /* try to write updated metadata,
2807 * if we catch a failure move on to the next disk
2808 */
a322f70c
DW
2809 for (d = ddf->dlist; d; d=d->next) {
2810 int fd = d->fd;
2811
2812 if (fd < 0)
2813 continue;
2814
175593bf 2815 attempts++;
a322f70c
DW
2816 /* We need to fill in the primary, (secondary) and workspace
2817 * lba's in the headers, set their checksums,
2818 * Also checksum phys, virt....
2819 *
2820 * Then write everything out, finally the anchor is written.
2821 */
2822 get_dev_size(fd, NULL, &size);
2823 size /= 512;
097bcf00 2824 if (d->workspace_lba != 0)
2825 ddf->anchor.workspace_lba = d->workspace_lba;
2826 else
2827 ddf->anchor.workspace_lba =
2828 __cpu_to_be64(size - 32*1024*2);
2829 if (d->primary_lba != 0)
2830 ddf->anchor.primary_lba = d->primary_lba;
2831 else
2832 ddf->anchor.primary_lba =
2833 __cpu_to_be64(size - 16*1024*2);
2834 if (d->secondary_lba != 0)
2835 ddf->anchor.secondary_lba = d->secondary_lba;
2836 else
2837 ddf->anchor.secondary_lba =
2838 __cpu_to_be64(size - 32*1024*2);
0175cbf6 2839 ddf->anchor.seq = seq;
a322f70c
DW
2840 memcpy(&ddf->primary, &ddf->anchor, 512);
2841 memcpy(&ddf->secondary, &ddf->anchor, 512);
2842
2843 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
2844 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
2845 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
2846
7f798aca 2847 if (!__write_ddf_structure(d, ddf, DDF_HEADER_PRIMARY,
2848 null_aligned))
175593bf 2849 continue;
a322f70c 2850
7f798aca 2851 if (!__write_ddf_structure(d, ddf, DDF_HEADER_SECONDARY,
2852 null_aligned))
175593bf 2853 continue;
a322f70c 2854
a322f70c 2855 lseek64(fd, (size-1)*512, SEEK_SET);
175593bf
DW
2856 if (write(fd, &ddf->anchor, 512) < 0)
2857 continue;
2858 successes++;
2859 }
42d5dfd9 2860 free(null_aligned);
175593bf 2861
175593bf 2862 return attempts != successes;
a322f70c 2863}
7a7cc504
NB
2864
2865static int write_init_super_ddf(struct supertype *st)
2866{
9b1fb677
DW
2867 struct ddf_super *ddf = st->sb;
2868 struct vcl *currentconf = ddf->currentconf;
2869
2870 /* we are done with currentconf reset it to point st at the container */
2871 ddf->currentconf = NULL;
edd8d13c
NB
2872
2873 if (st->update_tail) {
2874 /* queue the virtual_disk and vd_config as metadata updates */
2875 struct virtual_disk *vd;
2876 struct vd_config *vc;
edd8d13c
NB
2877 int len;
2878
9b1fb677 2879 if (!currentconf) {
2cc2983d
N
2880 int len = (sizeof(struct phys_disk) +
2881 sizeof(struct phys_disk_entry));
2882
2883 /* adding a disk to the container. */
2884 if (!ddf->add_list)
2885 return 0;
2886
2887 append_metadata_update(st, ddf->add_list->mdupdate, len);
2888 ddf->add_list->mdupdate = NULL;
2889 return 0;
2890 }
2891
2892 /* Newly created VD */
2893
edd8d13c
NB
2894 /* First the virtual disk. We have a slightly fake header */
2895 len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry);
503975b9 2896 vd = xmalloc(len);
edd8d13c 2897 *vd = *ddf->virt;
9b1fb677
DW
2898 vd->entries[0] = ddf->virt->entries[currentconf->vcnum];
2899 vd->populated_vdes = __cpu_to_be16(currentconf->vcnum);
edd8d13c
NB
2900 append_metadata_update(st, vd, len);
2901
2902 /* Then the vd_config */
2903 len = ddf->conf_rec_len * 512;
503975b9 2904 vc = xmalloc(len);
9b1fb677 2905 memcpy(vc, &currentconf->conf, len);
edd8d13c
NB
2906 append_metadata_update(st, vc, len);
2907
2908 /* FIXME I need to close the fds! */
2909 return 0;
613b0d17 2910 } else {
d682f344
N
2911 struct dl *d;
2912 for (d = ddf->dlist; d; d=d->next)
ba728be7 2913 while (Kill(d->devname, NULL, 0, -1, 1) == 0);
1cc7f4fe 2914 return __write_init_super_ddf(st);
d682f344 2915 }
7a7cc504
NB
2916}
2917
a322f70c
DW
2918#endif
2919
387fcd59
N
2920static __u64 avail_size_ddf(struct supertype *st, __u64 devsize,
2921 unsigned long long data_offset)
a322f70c
DW
2922{
2923 /* We must reserve the last 32Meg */
2924 if (devsize <= 32*1024*2)
2925 return 0;
2926 return devsize - 32*1024*2;
2927}
2928
2929#ifndef MDASSEMBLE
8592f29d
N
2930
2931static int reserve_space(struct supertype *st, int raiddisks,
2932 unsigned long long size, int chunk,
2933 unsigned long long *freesize)
2934{
2935 /* Find 'raiddisks' spare extents at least 'size' big (but
2936 * only caring about multiples of 'chunk') and remember
2937 * them.
2938 * If the cannot be found, fail.
2939 */
2940 struct dl *dl;
2941 struct ddf_super *ddf = st->sb;
2942 int cnt = 0;
2943
2944 for (dl = ddf->dlist; dl ; dl=dl->next) {
613b0d17 2945 dl->raiddisk = -1;
8592f29d
N
2946 dl->esize = 0;
2947 }
2948 /* Now find largest extent on each device */
2949 for (dl = ddf->dlist ; dl ; dl=dl->next) {
2950 struct extent *e = get_extents(ddf, dl);
2951 unsigned long long pos = 0;
2952 int i = 0;
2953 int found = 0;
2954 unsigned long long minsize = size;
2955
2956 if (size == 0)
2957 minsize = chunk;
2958
2959 if (!e)
2960 continue;
2961 do {
2962 unsigned long long esize;
2963 esize = e[i].start - pos;
2964 if (esize >= minsize) {
2965 found = 1;
2966 minsize = esize;
2967 }
2968 pos = e[i].start + e[i].size;
2969 i++;
2970 } while (e[i-1].size);
2971 if (found) {
2972 cnt++;
2973 dl->esize = minsize;
2974 }
2975 free(e);
2976 }
2977 if (cnt < raiddisks) {
e7b84f9d 2978 pr_err("not enough devices with space to create array.\n");
8592f29d
N
2979 return 0; /* No enough free spaces large enough */
2980 }
2981 if (size == 0) {
2982 /* choose the largest size of which there are at least 'raiddisk' */
2983 for (dl = ddf->dlist ; dl ; dl=dl->next) {
2984 struct dl *dl2;
2985 if (dl->esize <= size)
2986 continue;
2987 /* This is bigger than 'size', see if there are enough */
2988 cnt = 0;
7b80ad6a 2989 for (dl2 = ddf->dlist; dl2 ; dl2=dl2->next)
8592f29d
N
2990 if (dl2->esize >= dl->esize)
2991 cnt++;
2992 if (cnt >= raiddisks)
2993 size = dl->esize;
2994 }
2995 if (chunk) {
2996 size = size / chunk;
2997 size *= chunk;
2998 }
2999 *freesize = size;
3000 if (size < 32) {
e7b84f9d 3001 pr_err("not enough spare devices to create array.\n");
8592f29d
N
3002 return 0;
3003 }
3004 }
3005 /* We have a 'size' of which there are enough spaces.
3006 * We simply do a first-fit */
3007 cnt = 0;
3008 for (dl = ddf->dlist ; dl && cnt < raiddisks ; dl=dl->next) {
3009 if (dl->esize < size)
3010 continue;
613b0d17 3011
8592f29d
N
3012 dl->raiddisk = cnt;
3013 cnt++;
3014 }
3015 return 1;
3016}
3017
2c514b71
NB
3018static int
3019validate_geometry_ddf_container(struct supertype *st,
3020 int level, int layout, int raiddisks,
3021 int chunk, unsigned long long size,
af4348dd 3022 unsigned long long data_offset,
2c514b71
NB
3023 char *dev, unsigned long long *freesize,
3024 int verbose);
78e44928
NB
3025
3026static int validate_geometry_ddf_bvd(struct supertype *st,
3027 int level, int layout, int raiddisks,
c21e737b 3028 int *chunk, unsigned long long size,
af4348dd 3029 unsigned long long data_offset,
2c514b71
NB
3030 char *dev, unsigned long long *freesize,
3031 int verbose);
78e44928
NB
3032
3033static int validate_geometry_ddf(struct supertype *st,
2c514b71 3034 int level, int layout, int raiddisks,
c21e737b 3035 int *chunk, unsigned long long size,
af4348dd 3036 unsigned long long data_offset,
2c514b71
NB
3037 char *dev, unsigned long long *freesize,
3038 int verbose)
a322f70c
DW
3039{
3040 int fd;
3041 struct mdinfo *sra;
3042 int cfd;
3043
3044 /* ddf potentially supports lots of things, but it depends on
3045 * what devices are offered (and maybe kernel version?)
3046 * If given unused devices, we will make a container.
3047 * If given devices in a container, we will make a BVD.
3048 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
3049 */
3050
bb7295f1
N
3051 if (chunk && *chunk == UnSet)
3052 *chunk = DEFAULT_CHUNK;
3053
542ef4ec 3054 if (level == -1000000) level = LEVEL_CONTAINER;
a322f70c 3055 if (level == LEVEL_CONTAINER) {
78e44928
NB
3056 /* Must be a fresh device to add to a container */
3057 return validate_geometry_ddf_container(st, level, layout,
c21e737b 3058 raiddisks, chunk?*chunk:0,
af4348dd
N
3059 size, data_offset, dev,
3060 freesize,
2c514b71 3061 verbose);
5f8097be
NB
3062 }
3063
78e44928 3064 if (!dev) {
a3163bf0 3065 mdu_array_info_t array = {
3066 .level = level, .layout = layout,
3067 .raid_disks = raiddisks
3068 };
3069 struct vd_config conf;
3070 if (layout_md2ddf(&array, &conf) == -1) {
b42f577a 3071 if (verbose)
94b08b7c 3072 pr_err("DDF does not support level %d /layout %d arrays with %d disks\n",
3073 level, layout, raiddisks);
78e44928 3074 return 0;
b42f577a 3075 }
78e44928 3076 /* Should check layout? etc */
8592f29d
N
3077
3078 if (st->sb && freesize) {
3079 /* --create was given a container to create in.
3080 * So we need to check that there are enough
3081 * free spaces and return the amount of space.
3082 * We may as well remember which drives were
3083 * chosen so that add_to_super/getinfo_super
3084 * can return them.
3085 */
c21e737b 3086 return reserve_space(st, raiddisks, size, chunk?*chunk:0, freesize);
8592f29d 3087 }
a322f70c 3088 return 1;
78e44928 3089 }
a322f70c 3090
8592f29d
N
3091 if (st->sb) {
3092 /* A container has already been opened, so we are
3093 * creating in there. Maybe a BVD, maybe an SVD.
3094 * Should make a distinction one day.
3095 */
3096 return validate_geometry_ddf_bvd(st, level, layout, raiddisks,
af4348dd
N
3097 chunk, size, data_offset, dev,
3098 freesize,
8592f29d
N
3099 verbose);
3100 }
78e44928
NB
3101 /* This is the first device for the array.
3102 * If it is a container, we read it in and do automagic allocations,
3103 * no other devices should be given.
3104 * Otherwise it must be a member device of a container, and we
3105 * do manual allocation.
3106 * Later we should check for a BVD and make an SVD.
a322f70c 3107 */
a322f70c
DW
3108 fd = open(dev, O_RDONLY|O_EXCL, 0);
3109 if (fd >= 0) {
4dd2df09 3110 sra = sysfs_read(fd, NULL, GET_VERSION);
a322f70c
DW
3111 close(fd);
3112 if (sra && sra->array.major_version == -1 &&
78e44928
NB
3113 strcmp(sra->text_version, "ddf") == 0) {
3114
3115 /* load super */
3116 /* find space for 'n' devices. */
3117 /* remember the devices */
3118 /* Somehow return the fact that we have enough */
a322f70c
DW
3119 }
3120
2c514b71 3121 if (verbose)
e7b84f9d
N
3122 pr_err("ddf: Cannot create this array "
3123 "on device %s - a container is required.\n",
3124 dev);
a322f70c
DW
3125 return 0;
3126 }
3127 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2c514b71 3128 if (verbose)
e7b84f9d 3129 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 3130 dev, strerror(errno));
a322f70c
DW
3131 return 0;
3132 }
3133 /* Well, it is in use by someone, maybe a 'ddf' container. */
3134 cfd = open_container(fd);
3135 if (cfd < 0) {
3136 close(fd);
2c514b71 3137 if (verbose)
e7b84f9d 3138 pr_err("ddf: Cannot use %s: %s\n",
613b0d17 3139 dev, strerror(EBUSY));
a322f70c
DW
3140 return 0;
3141 }
4dd2df09 3142 sra = sysfs_read(cfd, NULL, GET_VERSION);
a322f70c
DW
3143 close(fd);
3144 if (sra && sra->array.major_version == -1 &&
3145 strcmp(sra->text_version, "ddf") == 0) {
3146 /* This is a member of a ddf container. Load the container
3147 * and try to create a bvd
3148 */
3149 struct ddf_super *ddf;
e1902a7b 3150 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL) == 0) {
5f8097be 3151 st->sb = ddf;
4dd2df09 3152 strcpy(st->container_devnm, fd2devnm(cfd));
a322f70c 3153 close(cfd);
78e44928 3154 return validate_geometry_ddf_bvd(st, level, layout,
a322f70c 3155 raiddisks, chunk, size,
af4348dd 3156 data_offset,
2c514b71
NB
3157 dev, freesize,
3158 verbose);
a322f70c
DW
3159 }
3160 close(cfd);
c42ec1ed
DW
3161 } else /* device may belong to a different container */
3162 return 0;
3163
a322f70c
DW
3164 return 1;
3165}
3166
2c514b71
NB
3167static int
3168validate_geometry_ddf_container(struct supertype *st,
3169 int level, int layout, int raiddisks,
3170 int chunk, unsigned long long size,
af4348dd 3171 unsigned long long data_offset,
2c514b71
NB
3172 char *dev, unsigned long long *freesize,
3173 int verbose)
a322f70c
DW
3174{
3175 int fd;
3176 unsigned long long ldsize;
3177
3178 if (level != LEVEL_CONTAINER)
3179 return 0;
3180 if (!dev)
3181 return 1;
3182
3183 fd = open(dev, O_RDONLY|O_EXCL, 0);
3184 if (fd < 0) {
2c514b71 3185 if (verbose)
e7b84f9d 3186 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 3187 dev, strerror(errno));
a322f70c
DW
3188 return 0;
3189 }
3190 if (!get_dev_size(fd, dev, &ldsize)) {
3191 close(fd);
3192 return 0;
3193 }
3194 close(fd);
3195
387fcd59 3196 *freesize = avail_size_ddf(st, ldsize >> 9, INVALID_SECTORS);
ea17e7aa
N
3197 if (*freesize == 0)
3198 return 0;
a322f70c
DW
3199
3200 return 1;
3201}
3202
78e44928
NB
3203static int validate_geometry_ddf_bvd(struct supertype *st,
3204 int level, int layout, int raiddisks,
c21e737b 3205 int *chunk, unsigned long long size,
af4348dd 3206 unsigned long long data_offset,
2c514b71
NB
3207 char *dev, unsigned long long *freesize,
3208 int verbose)
a322f70c
DW
3209{
3210 struct stat stb;
3211 struct ddf_super *ddf = st->sb;
3212 struct dl *dl;
5f8097be
NB
3213 unsigned long long pos = 0;
3214 unsigned long long maxsize;
3215 struct extent *e;
3216 int i;
a322f70c 3217 /* ddf/bvd supports lots of things, but not containers */
b42f577a
N
3218 if (level == LEVEL_CONTAINER) {
3219 if (verbose)
e7b84f9d 3220 pr_err("DDF cannot create a container within an container\n");
a322f70c 3221 return 0;
b42f577a 3222 }
a322f70c
DW
3223 /* We must have the container info already read in. */
3224 if (!ddf)
3225 return 0;
3226
5f8097be
NB
3227 if (!dev) {
3228 /* General test: make sure there is space for
3229 * 'raiddisks' device extents of size 'size'.
3230 */
3231 unsigned long long minsize = size;
3232 int dcnt = 0;
3233 if (minsize == 0)
3234 minsize = 8;
3235 for (dl = ddf->dlist; dl ; dl = dl->next)
3236 {
3237 int found = 0;
7e1432fb 3238 pos = 0;
5f8097be
NB
3239
3240 i = 0;
3241 e = get_extents(ddf, dl);
3242 if (!e) continue;
3243 do {
3244 unsigned long long esize;
3245 esize = e[i].start - pos;
3246 if (esize >= minsize)
3247 found = 1;
3248 pos = e[i].start + e[i].size;
3249 i++;
3250 } while (e[i-1].size);
3251 if (found)
3252 dcnt++;
3253 free(e);
3254 }
3255 if (dcnt < raiddisks) {
2c514b71 3256 if (verbose)
e7b84f9d
N
3257 pr_err("ddf: Not enough devices with "
3258 "space for this array (%d < %d)\n",
3259 dcnt, raiddisks);
5f8097be
NB
3260 return 0;
3261 }
3262 return 1;
3263 }
a322f70c
DW
3264 /* This device must be a member of the set */
3265 if (stat(dev, &stb) < 0)
3266 return 0;
3267 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3268 return 0;
3269 for (dl = ddf->dlist ; dl ; dl = dl->next) {
f21e18ca
N
3270 if (dl->major == (int)major(stb.st_rdev) &&
3271 dl->minor == (int)minor(stb.st_rdev))
a322f70c
DW
3272 break;
3273 }
5f8097be 3274 if (!dl) {
2c514b71 3275 if (verbose)
e7b84f9d 3276 pr_err("ddf: %s is not in the "
613b0d17
N
3277 "same DDF set\n",
3278 dev);
5f8097be
NB
3279 return 0;
3280 }
3281 e = get_extents(ddf, dl);
3282 maxsize = 0;
3283 i = 0;
3284 if (e) do {
613b0d17
N
3285 unsigned long long esize;
3286 esize = e[i].start - pos;
3287 if (esize >= maxsize)
3288 maxsize = esize;
3289 pos = e[i].start + e[i].size;
3290 i++;
3291 } while (e[i-1].size);
5f8097be 3292 *freesize = maxsize;
a322f70c
DW
3293 // FIXME here I am
3294
3295 return 1;
3296}
59e36268 3297
a322f70c 3298static int load_super_ddf_all(struct supertype *st, int fd,
e1902a7b 3299 void **sbp, char *devname)
a322f70c
DW
3300{
3301 struct mdinfo *sra;
3302 struct ddf_super *super;
3303 struct mdinfo *sd, *best = NULL;
3304 int bestseq = 0;
3305 int seq;
3306 char nm[20];
3307 int dfd;
3308
b526e52d 3309 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
a322f70c
DW
3310 if (!sra)
3311 return 1;
3312 if (sra->array.major_version != -1 ||
3313 sra->array.minor_version != -2 ||
3314 strcmp(sra->text_version, "ddf") != 0)
3315 return 1;
3316
6416d527 3317 if (posix_memalign((void**)&super, 512, sizeof(*super)) != 0)
a322f70c 3318 return 1;
a2349791 3319 memset(super, 0, sizeof(*super));
a322f70c
DW
3320
3321 /* first, try each device, and choose the best ddf */
3322 for (sd = sra->devs ; sd ; sd = sd->next) {
3323 int rv;
3324 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
7a7cc504
NB
3325 dfd = dev_open(nm, O_RDONLY);
3326 if (dfd < 0)
a322f70c
DW
3327 return 2;
3328 rv = load_ddf_headers(dfd, super, NULL);
7a7cc504 3329 close(dfd);
a322f70c
DW
3330 if (rv == 0) {
3331 seq = __be32_to_cpu(super->active->seq);
3332 if (super->active->openflag)
3333 seq--;
3334 if (!best || seq > bestseq) {
3335 bestseq = seq;
3336 best = sd;
3337 }
3338 }
3339 }
3340 if (!best)
3341 return 1;
3342 /* OK, load this ddf */
3343 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
3344 dfd = dev_open(nm, O_RDONLY);
7a7cc504 3345 if (dfd < 0)
a322f70c
DW
3346 return 1;
3347 load_ddf_headers(dfd, super, NULL);
3348 load_ddf_global(dfd, super, NULL);
3349 close(dfd);
3350 /* Now we need the device-local bits */
3351 for (sd = sra->devs ; sd ; sd = sd->next) {
3d2c4fc7
DW
3352 int rv;
3353
a322f70c 3354 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
e1902a7b 3355 dfd = dev_open(nm, O_RDWR);
7a7cc504 3356 if (dfd < 0)
a322f70c 3357 return 2;
3d2c4fc7
DW
3358 rv = load_ddf_headers(dfd, super, NULL);
3359 if (rv == 0)
e1902a7b 3360 rv = load_ddf_local(dfd, super, NULL, 1);
3d2c4fc7
DW
3361 if (rv)
3362 return 1;
a322f70c 3363 }
33414a01 3364
a322f70c
DW
3365 *sbp = super;
3366 if (st->ss == NULL) {
78e44928 3367 st->ss = &super_ddf;
a322f70c
DW
3368 st->minor_version = 0;
3369 st->max_devs = 512;
3370 }
4dd2df09 3371 strcpy(st->container_devnm, fd2devnm(fd));
a322f70c
DW
3372 return 0;
3373}
2b959fbf
N
3374
3375static int load_container_ddf(struct supertype *st, int fd,
3376 char *devname)
3377{
3378 return load_super_ddf_all(st, fd, &st->sb, devname);
3379}
3380
0e600426 3381#endif /* MDASSEMBLE */
a322f70c 3382
a5c7adb3 3383static int check_secondary(const struct vcl *vc)
3384{
3385 const struct vd_config *conf = &vc->conf;
3386 int i;
3387
3388 /* The only DDF secondary RAID level md can support is
3389 * RAID 10, if the stripe sizes and Basic volume sizes
3390 * are all equal.
3391 * Other configurations could in theory be supported by exposing
3392 * the BVDs to user space and using device mapper for the secondary
3393 * mapping. So far we don't support that.
3394 */
3395
3396 __u64 sec_elements[4] = {0, 0, 0, 0};
3397#define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3398#define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3399
3400 if (vc->other_bvds == NULL) {
3401 pr_err("No BVDs for secondary RAID found\n");
3402 return -1;
3403 }
3404 if (conf->prl != DDF_RAID1) {
3405 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3406 return -1;
3407 }
3408 if (conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED) {
3409 pr_err("Secondary RAID level %d is unsupported\n",
3410 conf->srl);
3411 return -1;
3412 }
3413 __set_sec_seen(conf->sec_elmnt_seq);
3414 for (i = 0; i < conf->sec_elmnt_count-1; i++) {
3415 const struct vd_config *bvd = vc->other_bvds[i];
3c48f7be 3416 if (bvd->sec_elmnt_seq == DDF_UNUSED_BVD)
c98567ba 3417 continue;
a5c7adb3 3418 if (bvd->srl != conf->srl) {
3419 pr_err("Inconsistent secondary RAID level across BVDs\n");
3420 return -1;
3421 }
3422 if (bvd->prl != conf->prl) {
3423 pr_err("Different RAID levels for BVDs are unsupported\n");
3424 return -1;
3425 }
3426 if (bvd->prim_elmnt_count != conf->prim_elmnt_count) {
3427 pr_err("All BVDs must have the same number of primary elements\n");
3428 return -1;
3429 }
3430 if (bvd->chunk_shift != conf->chunk_shift) {
3431 pr_err("Different strip sizes for BVDs are unsupported\n");
3432 return -1;
3433 }
3434 if (bvd->array_blocks != conf->array_blocks) {
3435 pr_err("Different BVD sizes are unsupported\n");
3436 return -1;
3437 }
3438 __set_sec_seen(bvd->sec_elmnt_seq);
3439 }
3440 for (i = 0; i < conf->sec_elmnt_count; i++) {
3441 if (!__was_sec_seen(i)) {
3442 pr_err("BVD %d is missing\n", i);
3443 return -1;
3444 }
3445 }
3446 return 0;
3447}
3448
8a38db86 3449static unsigned int get_pd_index_from_refnum(const struct vcl *vc,
4e587018 3450 __u32 refnum, unsigned int nmax,
3451 const struct vd_config **bvd,
3452 unsigned int *idx)
8a38db86 3453{
4e587018 3454 unsigned int i, j, n, sec, cnt;
3455
3456 cnt = __be16_to_cpu(vc->conf.prim_elmnt_count);
3457 sec = (vc->conf.sec_elmnt_count == 1 ? 0 : vc->conf.sec_elmnt_seq);
3458
3459 for (i = 0, j = 0 ; i < nmax ; i++) {
3460 /* j counts valid entries for this BVD */
3461 if (vc->conf.phys_refnum[i] != 0xffffffff)
3462 j++;
3463 if (vc->conf.phys_refnum[i] == refnum) {
3464 *bvd = &vc->conf;
3465 *idx = i;
3466 return sec * cnt + j - 1;
3467 }
3468 }
3469 if (vc->other_bvds == NULL)
3470 goto bad;
3471
3472 for (n = 1; n < vc->conf.sec_elmnt_count; n++) {
3473 struct vd_config *vd = vc->other_bvds[n-1];
4e587018 3474 sec = vd->sec_elmnt_seq;
3c48f7be 3475 if (sec == DDF_UNUSED_BVD)
3476 continue;
4e587018 3477 for (i = 0, j = 0 ; i < nmax ; i++) {
3478 if (vd->phys_refnum[i] != 0xffffffff)
3479 j++;
3480 if (vd->phys_refnum[i] == refnum) {
3481 *bvd = vd;
3482 *idx = i;
3483 return sec * cnt + j - 1;
3484 }
3485 }
3486 }
3487bad:
3488 *bvd = NULL;
d6e7b083 3489 return DDF_NOTFOUND;
8a38db86 3490}
3491
00bbdbda 3492static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray)
598f0d58
NB
3493{
3494 /* Given a container loaded by load_super_ddf_all,
3495 * extract information about all the arrays into
3496 * an mdinfo tree.
3497 *
3498 * For each vcl in conflist: create an mdinfo, fill it in,
3499 * then look for matching devices (phys_refnum) in dlist
3500 * and create appropriate device mdinfo.
3501 */
3502 struct ddf_super *ddf = st->sb;
3503 struct mdinfo *rest = NULL;
3504 struct vcl *vc;
3505
3506 for (vc = ddf->conflist ; vc ; vc=vc->next)
3507 {
f21e18ca
N
3508 unsigned int i;
3509 unsigned int j;
598f0d58 3510 struct mdinfo *this;
00bbdbda 3511 char *ep;
90fa1a29 3512 __u32 *cptr;
8a38db86 3513 unsigned int pd;
00bbdbda
N
3514
3515 if (subarray &&
3516 (strtoul(subarray, &ep, 10) != vc->vcnum ||
3517 *ep != '\0'))
3518 continue;
3519
a5c7adb3 3520 if (vc->conf.sec_elmnt_count > 1) {
3521 if (check_secondary(vc) != 0)
3522 continue;
3523 }
3524
503975b9 3525 this = xcalloc(1, sizeof(*this));
598f0d58
NB
3526 this->next = rest;
3527 rest = this;
3528
8a2848a7 3529 if (layout_ddf2md(&vc->conf, &this->array))
3530 continue;
598f0d58 3531 this->array.md_minor = -1;
f35f2525
N
3532 this->array.major_version = -1;
3533 this->array.minor_version = -2;
90fa1a29
JS
3534 cptr = (__u32 *)(vc->conf.guid + 16);
3535 this->array.ctime = DECADE + __be32_to_cpu(*cptr);
598f0d58
NB
3536 this->array.utime = DECADE +
3537 __be32_to_cpu(vc->conf.timestamp);
3538 this->array.chunk_size = 512 << vc->conf.chunk_shift;
3539
59e36268 3540 i = vc->vcnum;
7a7cc504
NB
3541 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
3542 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
ed9d66aa 3543 DDF_init_full) {
598f0d58 3544 this->array.state = 0;
ed9d66aa
NB
3545 this->resync_start = 0;
3546 } else {
598f0d58 3547 this->array.state = 1;
b7528a20 3548 this->resync_start = MaxSector;
ed9d66aa 3549 }
db42fa9b
N
3550 memcpy(this->name, ddf->virt->entries[i].name, 16);
3551 this->name[16]=0;
3552 for(j=0; j<16; j++)
3553 if (this->name[j] == ' ')
3554 this->name[j] = 0;
598f0d58
NB
3555
3556 memset(this->uuid, 0, sizeof(this->uuid));
3557 this->component_size = __be64_to_cpu(vc->conf.blocks);
3558 this->array.size = this->component_size / 2;
5f2aace8 3559 this->container_member = i;
598f0d58 3560
c5afc314
N
3561 ddf->currentconf = vc;
3562 uuid_from_super_ddf(st, this->uuid);
3563 ddf->currentconf = NULL;
3564
60f18132 3565 sprintf(this->text_version, "/%s/%d",
4dd2df09 3566 st->container_devnm, this->container_member);
60f18132 3567
8a38db86 3568 for (pd = 0; pd < __be16_to_cpu(ddf->phys->used_pdes); pd++) {
598f0d58
NB
3569 struct mdinfo *dev;
3570 struct dl *d;
4e587018 3571 const struct vd_config *bvd;
3572 unsigned int iphys;
fa033bec 3573 int stt;
598f0d58 3574
8a38db86 3575 if (ddf->phys->entries[pd].refnum == 0xFFFFFFFF)
bc17324f 3576 continue;
0cf5ef67
N
3577
3578 stt = __be16_to_cpu(ddf->phys->entries[pd].state);
fa033bec
N
3579 if ((stt & (DDF_Online|DDF_Failed|DDF_Rebuilding))
3580 != DDF_Online)
3581 continue;
3582
8a38db86 3583 i = get_pd_index_from_refnum(
4e587018 3584 vc, ddf->phys->entries[pd].refnum,
3585 ddf->mppe, &bvd, &iphys);
d6e7b083 3586 if (i == DDF_NOTFOUND)
8a38db86 3587 continue;
3588
fa033bec 3589 this->array.working_disks++;
bc17324f 3590
0cf5ef67 3591 for (d = ddf->dlist; d ; d=d->next)
8a38db86 3592 if (d->disk.refnum ==
3593 ddf->phys->entries[pd].refnum)
0cf5ef67
N
3594 break;
3595 if (d == NULL)
3596 /* Haven't found that one yet, maybe there are others */
3597 continue;
3598
503975b9 3599 dev = xcalloc(1, sizeof(*dev));
598f0d58
NB
3600 dev->next = this->devs;
3601 this->devs = dev;
3602
3603 dev->disk.number = __be32_to_cpu(d->disk.refnum);
3604 dev->disk.major = d->major;
3605 dev->disk.minor = d->minor;
3606 dev->disk.raid_disk = i;
3607 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
d23534e4 3608 dev->recovery_start = MaxSector;
598f0d58 3609
120f7677 3610 dev->events = __be32_to_cpu(ddf->primary.seq);
57a66662 3611 dev->data_offset =
3612 __be64_to_cpu(LBA_OFFSET(ddf, bvd)[iphys]);
4e587018 3613 dev->component_size = __be64_to_cpu(bvd->blocks);
598f0d58
NB
3614 if (d->devname)
3615 strcpy(dev->name, d->devname);
3616 }
3617 }
3618 return rest;
3619}
3620
955e9ea1 3621static int store_super_ddf(struct supertype *st, int fd)
a322f70c 3622{
955e9ea1 3623 struct ddf_super *ddf = st->sb;
a322f70c 3624 unsigned long long dsize;
6416d527 3625 void *buf;
3d2c4fc7 3626 int rc;
a322f70c 3627
955e9ea1
DW
3628 if (!ddf)
3629 return 1;
3630
a322f70c
DW
3631 if (!get_dev_size(fd, NULL, &dsize))
3632 return 1;
3633
dbf98368 3634 if (ddf->dlist || ddf->conflist) {
3635 struct stat sta;
3636 struct dl *dl;
3637 int ofd, ret;
3638
3639 if (fstat(fd, &sta) == -1 || !S_ISBLK(sta.st_mode)) {
3640 pr_err("%s: file descriptor for invalid device\n",
3641 __func__);
3642 return 1;
3643 }
3644 for (dl = ddf->dlist; dl; dl = dl->next)
3645 if (dl->major == (int)major(sta.st_rdev) &&
3646 dl->minor == (int)minor(sta.st_rdev))
3647 break;
3648 if (!dl) {
3649 pr_err("%s: couldn't find disk %d/%d\n", __func__,
3650 (int)major(sta.st_rdev),
3651 (int)minor(sta.st_rdev));
3652 return 1;
3653 }
3654 /*
3655 For DDF, writing to just one disk makes no sense.
3656 We would run the risk of writing inconsistent meta data
3657 to the devices. So just call __write_init_super_ddf and
3658 write to all devices, including this one.
3659 Use the fd passed to this function, just in case dl->fd
3660 is invalid.
3661 */
3662 ofd = dl->fd;
3663 dl->fd = fd;
3664 ret = __write_init_super_ddf(st);
3665 dl->fd = ofd;
3666 return ret;
3667 }
3668
3d2c4fc7
DW
3669 if (posix_memalign(&buf, 512, 512) != 0)
3670 return 1;
6416d527
NB
3671 memset(buf, 0, 512);
3672
a322f70c 3673 lseek64(fd, dsize-512, 0);
3d2c4fc7 3674 rc = write(fd, buf, 512);
6416d527 3675 free(buf);
3d2c4fc7
DW
3676 if (rc < 0)
3677 return 1;
a322f70c
DW
3678 return 0;
3679}
3680
a19c88b8
NB
3681static int compare_super_ddf(struct supertype *st, struct supertype *tst)
3682{
3683 /*
3684 * return:
3685 * 0 same, or first was empty, and second was copied
3686 * 1 second had wrong number
3687 * 2 wrong uuid
3688 * 3 wrong other info
3689 */
3690 struct ddf_super *first = st->sb;
3691 struct ddf_super *second = tst->sb;
4eefd651 3692 struct dl *dl1, *dl2;
3693 struct vcl *vl1, *vl2;
2d210697 3694 unsigned int max_vds, max_pds, pd, vd;
a19c88b8
NB
3695
3696 if (!first) {
3697 st->sb = tst->sb;
3698 tst->sb = NULL;
3699 return 0;
3700 }
3701
3702 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
3703 return 2;
3704
2d210697 3705 if (first->anchor.seq != second->anchor.seq) {
3706 dprintf("%s: sequence number mismatch %u/%u\n", __func__,
3707 __be32_to_cpu(first->anchor.seq),
3708 __be32_to_cpu(second->anchor.seq));
3709 return 3;
3710 }
3711 if (first->max_part != second->max_part ||
3712 first->phys->used_pdes != second->phys->used_pdes ||
3713 first->virt->populated_vdes != second->virt->populated_vdes) {
3714 dprintf("%s: PD/VD number mismatch\n", __func__);
3715 return 3;
3716 }
3717
3718 max_pds = __be16_to_cpu(first->phys->used_pdes);
3719 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3720 for (pd = 0; pd < max_pds; pd++)
3721 if (first->phys->entries[pd].refnum == dl2->disk.refnum)
3722 break;
3723 if (pd == max_pds) {
3724 dprintf("%s: no match for disk %08x\n", __func__,
3725 __be32_to_cpu(dl2->disk.refnum));
3726 return 3;
3727 }
3728 }
3729
3730 max_vds = __be16_to_cpu(first->active->max_vd_entries);
3731 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3732 if (vl2->conf.magic != DDF_VD_CONF_MAGIC)
3733 continue;
3734 for (vd = 0; vd < max_vds; vd++)
3735 if (!memcmp(first->virt->entries[vd].guid,
3736 vl2->conf.guid, DDF_GUID_LEN))
3737 break;
3738 if (vd == max_vds) {
3739 dprintf("%s: no match for VD config\n", __func__);
3740 return 3;
3741 }
3742 }
a19c88b8 3743 /* FIXME should I look at anything else? */
2d210697 3744
4eefd651 3745 /*
3746 At this point we are fairly sure that the meta data matches.
3747 But the new disk may contain additional local data.
3748 Add it to the super block.
3749 */
3750 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3751 for (vl1 = first->conflist; vl1; vl1 = vl1->next)
3752 if (!memcmp(vl1->conf.guid, vl2->conf.guid,
3753 DDF_GUID_LEN))
3754 break;
3755 if (vl1) {
3756 if (vl1->other_bvds != NULL &&
3757 vl1->conf.sec_elmnt_seq !=
3758 vl2->conf.sec_elmnt_seq) {
3759 dprintf("%s: adding BVD %u\n", __func__,
3760 vl2->conf.sec_elmnt_seq);
3761 add_other_bvd(vl1, &vl2->conf,
3762 first->conf_rec_len*512);
3763 }
3764 continue;
3765 }
3766
3767 if (posix_memalign((void **)&vl1, 512,
3768 (first->conf_rec_len*512 +
3769 offsetof(struct vcl, conf))) != 0) {
3770 pr_err("%s could not allocate vcl buf\n",
3771 __func__);
3772 return 3;
3773 }
3774
3775 vl1->next = first->conflist;
3776 vl1->block_sizes = NULL;
4eefd651 3777 memcpy(&vl1->conf, &vl2->conf, first->conf_rec_len*512);
3c48f7be 3778 if (alloc_other_bvds(first, vl1) != 0) {
3779 pr_err("%s could not allocate other bvds\n",
3780 __func__);
3781 free(vl1);
3782 return 3;
3783 }
4eefd651 3784 for (vd = 0; vd < max_vds; vd++)
3785 if (!memcmp(first->virt->entries[vd].guid,
3786 vl1->conf.guid, DDF_GUID_LEN))
3787 break;
3788 vl1->vcnum = vd;
3789 dprintf("%s: added config for VD %u\n", __func__, vl1->vcnum);
3790 first->conflist = vl1;
3791 }
3792
3793 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3794 for (dl1 = first->dlist; dl1; dl1 = dl1->next)
3795 if (dl1->disk.refnum == dl2->disk.refnum)
3796 break;
3797 if (dl1)
3798 continue;
3799
3800 if (posix_memalign((void **)&dl1, 512,
3801 sizeof(*dl1) + (first->max_part) * sizeof(dl1->vlist[0]))
3802 != 0) {
3803 pr_err("%s could not allocate disk info buffer\n",
3804 __func__);
3805 return 3;
3806 }
3807 memcpy(dl1, dl2, sizeof(*dl1));
3808 dl1->mdupdate = NULL;
3809 dl1->next = first->dlist;
3810 dl1->fd = -1;
3811 for (pd = 0; pd < max_pds; pd++)
3812 if (first->phys->entries[pd].refnum == dl1->disk.refnum)
3813 break;
3814 dl1->pdnum = pd;
3815 if (dl2->spare) {
3816 if (posix_memalign((void **)&dl1->spare, 512,
3817 first->conf_rec_len*512) != 0) {
3818 pr_err("%s could not allocate spare info buf\n",
3819 __func__);
3820 return 3;
3821 }
3822 memcpy(dl1->spare, dl2->spare, first->conf_rec_len*512);
3823 }
3824 for (vd = 0 ; vd < first->max_part ; vd++) {
3825 if (!dl2->vlist[vd]) {
3826 dl1->vlist[vd] = NULL;
3827 continue;
3828 }
3829 for (vl1 = first->conflist; vl1; vl1 = vl1->next) {
3830 if (!memcmp(vl1->conf.guid,
3831 dl2->vlist[vd]->conf.guid,
3832 DDF_GUID_LEN))
3833 break;
3834 dl1->vlist[vd] = vl1;
3835 }
3836 }
3837 first->dlist = dl1;
3838 dprintf("%s: added disk %d: %08x\n", __func__, dl1->pdnum,
3839 dl1->disk.refnum);
3840 }
3841
a19c88b8
NB
3842 return 0;
3843}
3844
0e600426 3845#ifndef MDASSEMBLE
4e5528c6
NB
3846/*
3847 * A new array 'a' has been started which claims to be instance 'inst'
3848 * within container 'c'.
3849 * We need to confirm that the array matches the metadata in 'c' so
3850 * that we don't corrupt any metadata.
3851 */
cba0191b 3852static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst)
549e9569 3853{
a2aa439e 3854 struct ddf_super *ddf = c->sb;
3855 int n = atoi(inst);
fb9d0acb 3856 if (all_ff(ddf->virt->entries[n].guid)) {
3857 pr_err("%s: subarray %d doesn't exist\n", __func__, n);
a2aa439e 3858 return -ENODEV;
3859 }
3860 dprintf("ddf: open_new %d\n", n);
3861 a->info.container_member = n;
549e9569
NB
3862 return 0;
3863}
3864
4e5528c6
NB
3865/*
3866 * The array 'a' is to be marked clean in the metadata.
ed9d66aa 3867 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
4e5528c6
NB
3868 * clean up to the point (in sectors). If that cannot be recorded in the
3869 * metadata, then leave it as dirty.
3870 *
3871 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3872 * !global! virtual_disk.virtual_entry structure.
3873 */
01f157d7 3874static int ddf_set_array_state(struct active_array *a, int consistent)
549e9569 3875{
4e5528c6
NB
3876 struct ddf_super *ddf = a->container->sb;
3877 int inst = a->info.container_member;
18a2f463 3878 int old = ddf->virt->entries[inst].state;
01f157d7
N
3879 if (consistent == 2) {
3880 /* Should check if a recovery should be started FIXME */
3881 consistent = 1;
b7941fd6 3882 if (!is_resync_complete(&a->info))
01f157d7
N
3883 consistent = 0;
3884 }
ed9d66aa
NB
3885 if (consistent)
3886 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
3887 else
4e5528c6 3888 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
18a2f463 3889 if (old != ddf->virt->entries[inst].state)
7d5a7ff3 3890 ddf_set_updates_pending(ddf);
18a2f463
NB
3891
3892 old = ddf->virt->entries[inst].init_state;
ed9d66aa 3893 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
b7941fd6 3894 if (is_resync_complete(&a->info))
ed9d66aa 3895 ddf->virt->entries[inst].init_state |= DDF_init_full;
b7941fd6 3896 else if (a->info.resync_start == 0)
ed9d66aa 3897 ddf->virt->entries[inst].init_state |= DDF_init_not;
4e5528c6 3898 else
ed9d66aa 3899 ddf->virt->entries[inst].init_state |= DDF_init_quick;
18a2f463 3900 if (old != ddf->virt->entries[inst].init_state)
7d5a7ff3 3901 ddf_set_updates_pending(ddf);
ed9d66aa 3902
2c514b71 3903 dprintf("ddf mark %d %s %llu\n", inst, consistent?"clean":"dirty",
b7941fd6 3904 a->info.resync_start);
01f157d7 3905 return consistent;
fd7cde1b
DW
3906}
3907
5ec636b7 3908static int get_bvd_state(const struct ddf_super *ddf,
3909 const struct vd_config *vc)
3910{
3911 unsigned int i, n_bvd, working = 0;
3912 unsigned int n_prim = __be16_to_cpu(vc->prim_elmnt_count);
3913 int pd, st, state;
3914 for (i = 0; i < n_prim; i++) {
3915 if (!find_index_in_bvd(ddf, vc, i, &n_bvd))
3916 continue;
3917 pd = find_phys(ddf, vc->phys_refnum[n_bvd]);
3918 if (pd < 0)
3919 continue;
3920 st = __be16_to_cpu(ddf->phys->entries[pd].state);
3921 if ((st & (DDF_Online|DDF_Failed|DDF_Rebuilding))
3922 == DDF_Online)
3923 working++;
3924 }
3925
3926 state = DDF_state_degraded;
3927 if (working == n_prim)
3928 state = DDF_state_optimal;
3929 else
3930 switch (vc->prl) {
3931 case DDF_RAID0:
3932 case DDF_CONCAT:
3933 case DDF_JBOD:
3934 state = DDF_state_failed;
3935 break;
3936 case DDF_RAID1:
3937 if (working == 0)
3938 state = DDF_state_failed;
3939 else if (working >= 2)
3940 state = DDF_state_part_optimal;
3941 break;
3942 case DDF_RAID4:
3943 case DDF_RAID5:
3944 if (working < n_prim - 1)
3945 state = DDF_state_failed;
3946 break;
3947 case DDF_RAID6:
3948 if (working < n_prim - 2)
3949 state = DDF_state_failed;
3950 else if (working == n_prim - 1)
3951 state = DDF_state_part_optimal;
3952 break;
3953 }
3954 return state;
3955}
3956
0777d17d 3957static int secondary_state(int state, int other, int seclevel)
3958{
3959 if (state == DDF_state_optimal && other == DDF_state_optimal)
3960 return DDF_state_optimal;
3961 if (seclevel == DDF_2MIRRORED) {
3962 if (state == DDF_state_optimal || other == DDF_state_optimal)
3963 return DDF_state_part_optimal;
3964 if (state == DDF_state_failed && other == DDF_state_failed)
3965 return DDF_state_failed;
3966 return DDF_state_degraded;
3967 } else {
3968 if (state == DDF_state_failed || other == DDF_state_failed)
3969 return DDF_state_failed;
3970 if (state == DDF_state_degraded || other == DDF_state_degraded)
3971 return DDF_state_degraded;
3972 return DDF_state_part_optimal;
3973 }
3974}
3975
3976static int get_svd_state(const struct ddf_super *ddf, const struct vcl *vcl)
3977{
3978 int state = get_bvd_state(ddf, &vcl->conf);
3979 unsigned int i;
3980 for (i = 1; i < vcl->conf.sec_elmnt_count; i++) {
3981 state = secondary_state(
3982 state,
3983 get_bvd_state(ddf, vcl->other_bvds[i-1]),
3984 vcl->conf.srl);
3985 }
3986 return state;
3987}
3988
7a7cc504
NB
3989/*
3990 * The state of each disk is stored in the global phys_disk structure
3991 * in phys_disk.entries[n].state.
3992 * This makes various combinations awkward.
3993 * - When a device fails in any array, it must be failed in all arrays
3994 * that include a part of this device.
3995 * - When a component is rebuilding, we cannot include it officially in the
3996 * array unless this is the only array that uses the device.
3997 *
3998 * So: when transitioning:
3999 * Online -> failed, just set failed flag. monitor will propagate
4000 * spare -> online, the device might need to be added to the array.
4001 * spare -> failed, just set failed. Don't worry if in array or not.
4002 */
8d45d196 4003static void ddf_set_disk(struct active_array *a, int n, int state)
549e9569 4004{
7a7cc504 4005 struct ddf_super *ddf = a->container->sb;
baba3f4e 4006 unsigned int inst = a->info.container_member, n_bvd;
4007 struct vcl *vcl;
4008 struct vd_config *vc = find_vdcr(ddf, inst, (unsigned int)n,
4009 &n_bvd, &vcl);
4010 int pd;
e1316fab
N
4011 struct mdinfo *mdi;
4012 struct dl *dl;
7a7cc504
NB
4013
4014 if (vc == NULL) {
2c514b71 4015 dprintf("ddf: cannot find instance %d!!\n", inst);
7a7cc504
NB
4016 return;
4017 }
e1316fab
N
4018 /* Find the matching slot in 'info'. */
4019 for (mdi = a->info.devs; mdi; mdi = mdi->next)
4020 if (mdi->disk.raid_disk == n)
4021 break;
4022 if (!mdi)
4023 return;
4024
4025 /* and find the 'dl' entry corresponding to that. */
4026 for (dl = ddf->dlist; dl; dl = dl->next)
77632af9
N
4027 if (mdi->state_fd >= 0 &&
4028 mdi->disk.major == dl->major &&
e1316fab
N
4029 mdi->disk.minor == dl->minor)
4030 break;
4031 if (!dl)
4032 return;
4033
baba3f4e 4034 pd = find_phys(ddf, vc->phys_refnum[n_bvd]);
e1316fab
N
4035 if (pd < 0 || pd != dl->pdnum) {
4036 /* disk doesn't currently exist or has changed.
4037 * If it is now in_sync, insert it. */
baba3f4e 4038 dprintf("%s: phys disk not found for %d: %d/%d ref %08x\n",
4039 __func__, dl->pdnum, dl->major, dl->minor,
4040 dl->disk.refnum);
4041 dprintf("%s: array %u disk %u ref %08x pd %d\n",
4042 __func__, inst, n_bvd, vc->phys_refnum[n_bvd], pd);
7a7cc504 4043 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
baba3f4e 4044 pd = dl->pdnum; /* FIXME: is this really correct ? */
4045 vc->phys_refnum[n_bvd] = dl->disk.refnum;
57a66662 4046 LBA_OFFSET(ddf, vc)[n_bvd] =
4047 __cpu_to_be64(mdi->data_offset);
e1316fab
N
4048 ddf->phys->entries[pd].type &=
4049 ~__cpu_to_be16(DDF_Global_Spare);
4050 ddf->phys->entries[pd].type |=
4051 __cpu_to_be16(DDF_Active_in_VD);
7d5a7ff3 4052 ddf_set_updates_pending(ddf);
7a7cc504
NB
4053 }
4054 } else {
18a2f463 4055 int old = ddf->phys->entries[pd].state;
7a7cc504
NB
4056 if (state & DS_FAULTY)
4057 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
4058 if (state & DS_INSYNC) {
4059 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
4060 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
4061 }
18a2f463 4062 if (old != ddf->phys->entries[pd].state)
7d5a7ff3 4063 ddf_set_updates_pending(ddf);
7a7cc504
NB
4064 }
4065
2c514b71 4066 dprintf("ddf: set_disk %d to %x\n", n, state);
7e1432fb 4067
7a7cc504
NB
4068 /* Now we need to check the state of the array and update
4069 * virtual_disk.entries[n].state.
4070 * It needs to be one of "optimal", "degraded", "failed".
4071 * I don't understand 'deleted' or 'missing'.
4072 */
0777d17d 4073 state = get_svd_state(ddf, vcl);
7a7cc504 4074
18a2f463
NB
4075 if (ddf->virt->entries[inst].state !=
4076 ((ddf->virt->entries[inst].state & ~DDF_state_mask)
4077 | state)) {
4078
4079 ddf->virt->entries[inst].state =
4080 (ddf->virt->entries[inst].state & ~DDF_state_mask)
4081 | state;
7d5a7ff3 4082 ddf_set_updates_pending(ddf);
18a2f463 4083 }
7a7cc504 4084
549e9569
NB
4085}
4086
2e735d19 4087static void ddf_sync_metadata(struct supertype *st)
549e9569 4088{
7a7cc504
NB
4089
4090 /*
4091 * Write all data to all devices.
4092 * Later, we might be able to track whether only local changes
4093 * have been made, or whether any global data has been changed,
4094 * but ddf is sufficiently weird that it probably always
4095 * changes global data ....
4096 */
18a2f463
NB
4097 struct ddf_super *ddf = st->sb;
4098 if (!ddf->updates_pending)
4099 return;
4100 ddf->updates_pending = 0;
1cc7f4fe 4101 __write_init_super_ddf(st);
2c514b71 4102 dprintf("ddf: sync_metadata\n");
549e9569
NB
4103}
4104
88c164f4
NB
4105static void ddf_process_update(struct supertype *st,
4106 struct metadata_update *update)
4107{
4108 /* Apply this update to the metadata.
4109 * The first 4 bytes are a DDF_*_MAGIC which guides
4110 * our actions.
4111 * Possible update are:
4112 * DDF_PHYS_RECORDS_MAGIC
4dd968cc
N
4113 * Add a new physical device or remove an old one.
4114 * Changes to this record only happen implicitly.
88c164f4
NB
4115 * used_pdes is the device number.
4116 * DDF_VIRT_RECORDS_MAGIC
4117 * Add a new VD. Possibly also change the 'access' bits.
4118 * populated_vdes is the entry number.
4119 * DDF_VD_CONF_MAGIC
4120 * New or updated VD. the VIRT_RECORD must already
4121 * exist. For an update, phys_refnum and lba_offset
4122 * (at least) are updated, and the VD_CONF must
4123 * be written to precisely those devices listed with
4124 * a phys_refnum.
4125 * DDF_SPARE_ASSIGN_MAGIC
4126 * replacement Spare Assignment Record... but for which device?
4127 *
4128 * So, e.g.:
4129 * - to create a new array, we send a VIRT_RECORD and
4130 * a VD_CONF. Then assemble and start the array.
4131 * - to activate a spare we send a VD_CONF to add the phys_refnum
4132 * and offset. This will also mark the spare as active with
4133 * a spare-assignment record.
4134 */
4135 struct ddf_super *ddf = st->sb;
4136 __u32 *magic = (__u32*)update->buf;
4137 struct phys_disk *pd;
4138 struct virtual_disk *vd;
4139 struct vd_config *vc;
4140 struct vcl *vcl;
4141 struct dl *dl;
f21e18ca
N
4142 unsigned int mppe;
4143 unsigned int ent;
c7079c84 4144 unsigned int pdnum, pd2;
88c164f4 4145
2c514b71 4146 dprintf("Process update %x\n", *magic);
7e1432fb 4147
88c164f4
NB
4148 switch (*magic) {
4149 case DDF_PHYS_RECORDS_MAGIC:
4150
4151 if (update->len != (sizeof(struct phys_disk) +
4152 sizeof(struct phys_disk_entry)))
4153 return;
4154 pd = (struct phys_disk*)update->buf;
4155
4156 ent = __be16_to_cpu(pd->used_pdes);
4157 if (ent >= __be16_to_cpu(ddf->phys->max_pdes))
4158 return;
4dd968cc
N
4159 if (pd->entries[0].state & __cpu_to_be16(DDF_Missing)) {
4160 struct dl **dlp;
4161 /* removing this disk. */
4162 ddf->phys->entries[ent].state |= __cpu_to_be16(DDF_Missing);
4163 for (dlp = &ddf->dlist; *dlp; dlp = &(*dlp)->next) {
4164 struct dl *dl = *dlp;
4165 if (dl->pdnum == (signed)ent) {
4166 close(dl->fd);
4167 dl->fd = -1;
4168 /* FIXME this doesn't free
4169 * dl->devname */
4170 update->space = dl;
4171 *dlp = dl->next;
4172 break;
4173 }
4174 }
7d5a7ff3 4175 ddf_set_updates_pending(ddf);
4dd968cc
N
4176 return;
4177 }
88c164f4
NB
4178 if (!all_ff(ddf->phys->entries[ent].guid))
4179 return;
4180 ddf->phys->entries[ent] = pd->entries[0];
4181 ddf->phys->used_pdes = __cpu_to_be16(1 +
613b0d17 4182 __be16_to_cpu(ddf->phys->used_pdes));
7d5a7ff3 4183 ddf_set_updates_pending(ddf);
2cc2983d
N
4184 if (ddf->add_list) {
4185 struct active_array *a;
4186 struct dl *al = ddf->add_list;
4187 ddf->add_list = al->next;
4188
4189 al->next = ddf->dlist;
4190 ddf->dlist = al;
4191
4192 /* As a device has been added, we should check
4193 * for any degraded devices that might make
4194 * use of this spare */
4195 for (a = st->arrays ; a; a=a->next)
4196 a->check_degraded = 1;
4197 }
88c164f4
NB
4198 break;
4199
4200 case DDF_VIRT_RECORDS_MAGIC:
4201
4202 if (update->len != (sizeof(struct virtual_disk) +
4203 sizeof(struct virtual_entry)))
4204 return;
4205 vd = (struct virtual_disk*)update->buf;
4206
fb9d0acb 4207 ent = find_unused_vde(ddf);
4208 if (ent == DDF_NOTFOUND)
88c164f4
NB
4209 return;
4210 ddf->virt->entries[ent] = vd->entries[0];
4211 ddf->virt->populated_vdes = __cpu_to_be16(1 +
613b0d17 4212 __be16_to_cpu(ddf->virt->populated_vdes));
7d5a7ff3 4213 ddf_set_updates_pending(ddf);
88c164f4
NB
4214 break;
4215
4216 case DDF_VD_CONF_MAGIC:
2c514b71 4217 dprintf("len %d %d\n", update->len, ddf->conf_rec_len);
88c164f4
NB
4218
4219 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
f21e18ca 4220 if ((unsigned)update->len != ddf->conf_rec_len * 512)
88c164f4
NB
4221 return;
4222 vc = (struct vd_config*)update->buf;
4223 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
4224 if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0)
4225 break;
2c514b71 4226 dprintf("vcl = %p\n", vcl);
88c164f4
NB
4227 if (vcl) {
4228 /* An update, just copy the phys_refnum and lba_offset
4229 * fields
4230 */
4231 memcpy(vcl->conf.phys_refnum, vc->phys_refnum,
4232 mppe * (sizeof(__u32) + sizeof(__u64)));
4233 } else {
4234 /* A new VD_CONF */
e6b9548d
DW
4235 if (!update->space)
4236 return;
88c164f4
NB
4237 vcl = update->space;
4238 update->space = NULL;
4239 vcl->next = ddf->conflist;
edd8d13c 4240 memcpy(&vcl->conf, vc, update->len);
fb9d0acb 4241 ent = find_vde_by_guid(ddf, vc->guid);
4242 if (ent == DDF_NOTFOUND)
4243 return;
4244 vcl->vcnum = ent;
88c164f4
NB
4245 ddf->conflist = vcl;
4246 }
c7079c84
N
4247 /* Set DDF_Transition on all Failed devices - to help
4248 * us detect those that are no longer in use
4249 */
4250 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4251 if (ddf->phys->entries[pdnum].state
4252 & __be16_to_cpu(DDF_Failed))
4253 ddf->phys->entries[pdnum].state
4254 |= __be16_to_cpu(DDF_Transition);
88c164f4
NB
4255 /* Now make sure vlist is correct for each dl. */
4256 for (dl = ddf->dlist; dl; dl = dl->next) {
f21e18ca
N
4257 unsigned int dn;
4258 unsigned int vn = 0;
8401644c 4259 int in_degraded = 0;
88c164f4
NB
4260 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
4261 for (dn=0; dn < ddf->mppe ; dn++)
4262 if (vcl->conf.phys_refnum[dn] ==
4263 dl->disk.refnum) {
8401644c 4264 int vstate;
2c514b71
NB
4265 dprintf("dev %d has %p at %d\n",
4266 dl->pdnum, vcl, vn);
c7079c84
N
4267 /* Clear the Transition flag */
4268 if (ddf->phys->entries[dl->pdnum].state
4269 & __be16_to_cpu(DDF_Failed))
4270 ddf->phys->entries[dl->pdnum].state &=
4271 ~__be16_to_cpu(DDF_Transition);
4272
88c164f4 4273 dl->vlist[vn++] = vcl;
8401644c
N
4274 vstate = ddf->virt->entries[vcl->vcnum].state
4275 & DDF_state_mask;
4276 if (vstate == DDF_state_degraded ||
4277 vstate == DDF_state_part_optimal)
4278 in_degraded = 1;
88c164f4
NB
4279 break;
4280 }
4281 while (vn < ddf->max_part)
4282 dl->vlist[vn++] = NULL;
7e1432fb
NB
4283 if (dl->vlist[0]) {
4284 ddf->phys->entries[dl->pdnum].type &=
4285 ~__cpu_to_be16(DDF_Global_Spare);
8401644c
N
4286 if (!(ddf->phys->entries[dl->pdnum].type &
4287 __cpu_to_be16(DDF_Active_in_VD))) {
613b0d17
N
4288 ddf->phys->entries[dl->pdnum].type |=
4289 __cpu_to_be16(DDF_Active_in_VD);
4290 if (in_degraded)
4291 ddf->phys->entries[dl->pdnum].state |=
4292 __cpu_to_be16(DDF_Rebuilding);
4293 }
7e1432fb
NB
4294 }
4295 if (dl->spare) {
4296 ddf->phys->entries[dl->pdnum].type &=
4297 ~__cpu_to_be16(DDF_Global_Spare);
4298 ddf->phys->entries[dl->pdnum].type |=
4299 __cpu_to_be16(DDF_Spare);
4300 }
4301 if (!dl->vlist[0] && !dl->spare) {
4302 ddf->phys->entries[dl->pdnum].type |=
4303 __cpu_to_be16(DDF_Global_Spare);
4304 ddf->phys->entries[dl->pdnum].type &=
4305 ~__cpu_to_be16(DDF_Spare |
4306 DDF_Active_in_VD);
4307 }
88c164f4 4308 }
c7079c84
N
4309
4310 /* Now remove any 'Failed' devices that are not part
4311 * of any VD. They will have the Transition flag set.
4312 * Once done, we need to update all dl->pdnum numbers.
4313 */
4314 pd2 = 0;
4315 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4316 if ((ddf->phys->entries[pdnum].state
4317 & __be16_to_cpu(DDF_Failed))
4318 && (ddf->phys->entries[pdnum].state
4319 & __be16_to_cpu(DDF_Transition)))
4320 /* skip this one */;
4321 else if (pdnum == pd2)
4322 pd2++;
4323 else {
4324 ddf->phys->entries[pd2] = ddf->phys->entries[pdnum];
4325 for (dl = ddf->dlist; dl; dl = dl->next)
4326 if (dl->pdnum == (int)pdnum)
4327 dl->pdnum = pd2;
4328 pd2++;
4329 }
4330 ddf->phys->used_pdes = __cpu_to_be16(pd2);
4331 while (pd2 < pdnum) {
4332 memset(ddf->phys->entries[pd2].guid, 0xff, DDF_GUID_LEN);
4333 pd2++;
4334 }
4335
7d5a7ff3 4336 ddf_set_updates_pending(ddf);
88c164f4
NB
4337 break;
4338 case DDF_SPARE_ASSIGN_MAGIC:
4339 default: break;
4340 }
4341}
4342
edd8d13c
NB
4343static void ddf_prepare_update(struct supertype *st,
4344 struct metadata_update *update)
4345{
4346 /* This update arrived at managemon.
4347 * We are about to pass it to monitor.
4348 * If a malloc is needed, do it here.
4349 */
4350 struct ddf_super *ddf = st->sb;
4351 __u32 *magic = (__u32*)update->buf;
4352 if (*magic == DDF_VD_CONF_MAGIC)
e6b9548d 4353 if (posix_memalign(&update->space, 512,
613b0d17
N
4354 offsetof(struct vcl, conf)
4355 + ddf->conf_rec_len * 512) != 0)
e6b9548d 4356 update->space = NULL;
edd8d13c
NB
4357}
4358
7e1432fb
NB
4359/*
4360 * Check if the array 'a' is degraded but not failed.
4361 * If it is, find as many spares as are available and needed and
4362 * arrange for their inclusion.
4363 * We only choose devices which are not already in the array,
4364 * and prefer those with a spare-assignment to this array.
4365 * otherwise we choose global spares - assuming always that
4366 * there is enough room.
4367 * For each spare that we assign, we return an 'mdinfo' which
4368 * describes the position for the device in the array.
4369 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4370 * the new phys_refnum and lba_offset values.
4371 *
4372 * Only worry about BVDs at the moment.
4373 */
4374static struct mdinfo *ddf_activate_spare(struct active_array *a,
4375 struct metadata_update **updates)
4376{
4377 int working = 0;
4378 struct mdinfo *d;
4379 struct ddf_super *ddf = a->container->sb;
4380 int global_ok = 0;
4381 struct mdinfo *rv = NULL;
4382 struct mdinfo *di;
4383 struct metadata_update *mu;
4384 struct dl *dl;
4385 int i;
baba3f4e 4386 struct vcl *vcl;
7e1432fb 4387 struct vd_config *vc;
baba3f4e 4388 unsigned int n_bvd;
7e1432fb 4389
7e1432fb
NB
4390 for (d = a->info.devs ; d ; d = d->next) {
4391 if ((d->curr_state & DS_FAULTY) &&
613b0d17 4392 d->state_fd >= 0)
7e1432fb
NB
4393 /* wait for Removal to happen */
4394 return NULL;
4395 if (d->state_fd >= 0)
4396 working ++;
4397 }
4398
2c514b71
NB
4399 dprintf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks,
4400 a->info.array.level);
7e1432fb
NB
4401 if (working == a->info.array.raid_disks)
4402 return NULL; /* array not degraded */
4403 switch (a->info.array.level) {
4404 case 1:
4405 if (working == 0)
4406 return NULL; /* failed */
4407 break;
4408 case 4:
4409 case 5:
4410 if (working < a->info.array.raid_disks - 1)
4411 return NULL; /* failed */
4412 break;
4413 case 6:
4414 if (working < a->info.array.raid_disks - 2)
4415 return NULL; /* failed */
4416 break;
4417 default: /* concat or stripe */
4418 return NULL; /* failed */
4419 }
4420
4421 /* For each slot, if it is not working, find a spare */
4422 dl = ddf->dlist;
4423 for (i = 0; i < a->info.array.raid_disks; i++) {
4424 for (d = a->info.devs ; d ; d = d->next)
4425 if (d->disk.raid_disk == i)
4426 break;
2c514b71 4427 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
7e1432fb
NB
4428 if (d && (d->state_fd >= 0))
4429 continue;
4430
4431 /* OK, this device needs recovery. Find a spare */
4432 again:
4433 for ( ; dl ; dl = dl->next) {
4434 unsigned long long esize;
4435 unsigned long long pos;
4436 struct mdinfo *d2;
4437 int is_global = 0;
4438 int is_dedicated = 0;
4439 struct extent *ex;
f21e18ca 4440 unsigned int j;
7e1432fb
NB
4441 /* If in this array, skip */
4442 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
7590d562
N
4443 if (d2->state_fd >= 0 &&
4444 d2->disk.major == dl->major &&
7e1432fb 4445 d2->disk.minor == dl->minor) {
2c514b71 4446 dprintf("%x:%x already in array\n", dl->major, dl->minor);
7e1432fb
NB
4447 break;
4448 }
4449 if (d2)
4450 continue;
4451 if (ddf->phys->entries[dl->pdnum].type &
4452 __cpu_to_be16(DDF_Spare)) {
4453 /* Check spare assign record */
4454 if (dl->spare) {
4455 if (dl->spare->type & DDF_spare_dedicated) {
4456 /* check spare_ents for guid */
4457 for (j = 0 ;
4458 j < __be16_to_cpu(dl->spare->populated);
4459 j++) {
4460 if (memcmp(dl->spare->spare_ents[j].guid,
4461 ddf->virt->entries[a->info.container_member].guid,
4462 DDF_GUID_LEN) == 0)
4463 is_dedicated = 1;
4464 }
4465 } else
4466 is_global = 1;
4467 }
4468 } else if (ddf->phys->entries[dl->pdnum].type &
4469 __cpu_to_be16(DDF_Global_Spare)) {
4470 is_global = 1;
e0e7aeaa
N
4471 } else if (!(ddf->phys->entries[dl->pdnum].state &
4472 __cpu_to_be16(DDF_Failed))) {
4473 /* we can possibly use some of this */
4474 is_global = 1;
7e1432fb
NB
4475 }
4476 if ( ! (is_dedicated ||
4477 (is_global && global_ok))) {
2c514b71 4478 dprintf("%x:%x not suitable: %d %d\n", dl->major, dl->minor,
613b0d17 4479 is_dedicated, is_global);
7e1432fb
NB
4480 continue;
4481 }
4482
4483 /* We are allowed to use this device - is there space?
4484 * We need a->info.component_size sectors */
4485 ex = get_extents(ddf, dl);
4486 if (!ex) {
2c514b71 4487 dprintf("cannot get extents\n");
7e1432fb
NB
4488 continue;
4489 }
4490 j = 0; pos = 0;
4491 esize = 0;
4492
4493 do {
4494 esize = ex[j].start - pos;
4495 if (esize >= a->info.component_size)
4496 break;
e5cc7d46
N
4497 pos = ex[j].start + ex[j].size;
4498 j++;
4499 } while (ex[j-1].size);
7e1432fb
NB
4500
4501 free(ex);
4502 if (esize < a->info.component_size) {
e5cc7d46
N
4503 dprintf("%x:%x has no room: %llu %llu\n",
4504 dl->major, dl->minor,
2c514b71 4505 esize, a->info.component_size);
7e1432fb
NB
4506 /* No room */
4507 continue;
4508 }
4509
4510 /* Cool, we have a device with some space at pos */
503975b9 4511 di = xcalloc(1, sizeof(*di));
7e1432fb
NB
4512 di->disk.number = i;
4513 di->disk.raid_disk = i;
4514 di->disk.major = dl->major;
4515 di->disk.minor = dl->minor;
4516 di->disk.state = 0;
d23534e4 4517 di->recovery_start = 0;
7e1432fb
NB
4518 di->data_offset = pos;
4519 di->component_size = a->info.component_size;
4520 di->container_member = dl->pdnum;
4521 di->next = rv;
4522 rv = di;
2c514b71
NB
4523 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
4524 i, pos);
7e1432fb
NB
4525
4526 break;
4527 }
4528 if (!dl && ! global_ok) {
4529 /* not enough dedicated spares, try global */
4530 global_ok = 1;
4531 dl = ddf->dlist;
4532 goto again;
4533 }
4534 }
4535
4536 if (!rv)
4537 /* No spares found */
4538 return rv;
4539 /* Now 'rv' has a list of devices to return.
4540 * Create a metadata_update record to update the
4541 * phys_refnum and lba_offset values
4542 */
503975b9
N
4543 mu = xmalloc(sizeof(*mu));
4544 if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
79244939
DW
4545 free(mu);
4546 mu = NULL;
4547 }
503975b9 4548 mu->buf = xmalloc(ddf->conf_rec_len * 512);
7590d562
N
4549 mu->len = ddf->conf_rec_len * 512;
4550 mu->space = NULL;
f50ae22e 4551 mu->space_list = NULL;
7e1432fb 4552 mu->next = *updates;
baba3f4e 4553 vc = find_vdcr(ddf, a->info.container_member, di->disk.raid_disk,
4554 &n_bvd, &vcl);
7e1432fb
NB
4555 memcpy(mu->buf, vc, ddf->conf_rec_len * 512);
4556
4557 vc = (struct vd_config*)mu->buf;
7e1432fb
NB
4558 for (di = rv ; di ; di = di->next) {
4559 vc->phys_refnum[di->disk.raid_disk] =
4560 ddf->phys->entries[dl->pdnum].refnum;
57a66662 4561 LBA_OFFSET(ddf, vc)[di->disk.raid_disk]
4562 = __cpu_to_be64(di->data_offset);
7e1432fb
NB
4563 }
4564 *updates = mu;
4565 return rv;
4566}
0e600426 4567#endif /* MDASSEMBLE */
7e1432fb 4568
b640a252
N
4569static int ddf_level_to_layout(int level)
4570{
4571 switch(level) {
4572 case 0:
4573 case 1:
4574 return 0;
4575 case 5:
4576 return ALGORITHM_LEFT_SYMMETRIC;
4577 case 6:
4578 return ALGORITHM_ROTATING_N_CONTINUE;
4579 case 10:
4580 return 0x102;
4581 default:
4582 return UnSet;
4583 }
4584}
4585
30f58b22
DW
4586static void default_geometry_ddf(struct supertype *st, int *level, int *layout, int *chunk)
4587{
4588 if (level && *level == UnSet)
4589 *level = LEVEL_CONTAINER;
4590
4591 if (level && layout && *layout == UnSet)
4592 *layout = ddf_level_to_layout(*level);
4593}
4594
a322f70c
DW
4595struct superswitch super_ddf = {
4596#ifndef MDASSEMBLE
4597 .examine_super = examine_super_ddf,
4598 .brief_examine_super = brief_examine_super_ddf,
4737ae25 4599 .brief_examine_subarrays = brief_examine_subarrays_ddf,
bceedeec 4600 .export_examine_super = export_examine_super_ddf,
a322f70c
DW
4601 .detail_super = detail_super_ddf,
4602 .brief_detail_super = brief_detail_super_ddf,
4603 .validate_geometry = validate_geometry_ddf,
78e44928 4604 .write_init_super = write_init_super_ddf,
0e600426 4605 .add_to_super = add_to_super_ddf,
4dd968cc 4606 .remove_from_super = remove_from_super_ddf,
2b959fbf 4607 .load_container = load_container_ddf,
74db60b0 4608 .copy_metadata = copy_metadata_ddf,
a322f70c
DW
4609#endif
4610 .match_home = match_home_ddf,
4611 .uuid_from_super= uuid_from_super_ddf,
4612 .getinfo_super = getinfo_super_ddf,
4613 .update_super = update_super_ddf,
4614
4615 .avail_size = avail_size_ddf,
4616
a19c88b8
NB
4617 .compare_super = compare_super_ddf,
4618
a322f70c 4619 .load_super = load_super_ddf,
ba7eb04f 4620 .init_super = init_super_ddf,
955e9ea1 4621 .store_super = store_super_ddf,
a322f70c
DW
4622 .free_super = free_super_ddf,
4623 .match_metadata_desc = match_metadata_desc_ddf,
78e44928 4624 .container_content = container_content_ddf,
30f58b22 4625 .default_geometry = default_geometry_ddf,
a322f70c 4626
a322f70c 4627 .external = 1,
549e9569 4628
0e600426 4629#ifndef MDASSEMBLE
549e9569
NB
4630/* for mdmon */
4631 .open_new = ddf_open_new,
ed9d66aa 4632 .set_array_state= ddf_set_array_state,
549e9569
NB
4633 .set_disk = ddf_set_disk,
4634 .sync_metadata = ddf_sync_metadata,
88c164f4 4635 .process_update = ddf_process_update,
edd8d13c 4636 .prepare_update = ddf_prepare_update,
7e1432fb 4637 .activate_spare = ddf_activate_spare,
0e600426 4638#endif
4cce4069 4639 .name = "ddf",
a322f70c 4640};