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