]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-ddf.c
DDF: Implement store_super_ddf
[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;
a4057a88 2521 int ret = 0;
7f798aca 2522
2523 fd = d->fd;
2524
2525 switch (type) {
2526 case DDF_HEADER_PRIMARY:
2527 header = &ddf->primary;
2528 sector = __be64_to_cpu(header->primary_lba);
2529 break;
2530 case DDF_HEADER_SECONDARY:
2531 header = &ddf->secondary;
2532 sector = __be64_to_cpu(header->secondary_lba);
2533 break;
2534 default:
2535 return 0;
2536 }
2537
2538 header->type = type;
a4057a88 2539 header->openflag = 1;
7f798aca 2540 header->crc = calc_crc(header, 512);
2541
2542 lseek64(fd, sector<<9, 0);
2543 if (write(fd, header, 512) < 0)
a4057a88 2544 goto out;
7f798aca 2545
2546 ddf->controller.crc = calc_crc(&ddf->controller, 512);
2547 if (write(fd, &ddf->controller, 512) < 0)
a4057a88 2548 goto out;
a322f70c 2549
7f798aca 2550 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
2551 if (write(fd, ddf->phys, ddf->pdsize) < 0)
a4057a88 2552 goto out;
7f798aca 2553 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
2554 if (write(fd, ddf->virt, ddf->vdsize) < 0)
a4057a88 2555 goto out;
7f798aca 2556
2557 /* Now write lots of config records. */
2558 n_config = ddf->max_part;
2559 conf_size = ddf->conf_rec_len * 512;
2560 for (i = 0 ; i <= n_config ; i++) {
e3c2a365 2561 struct vcl *c;
2562 struct vd_config *vdc = NULL;
2563 if (i == n_config) {
7f798aca 2564 c = (struct vcl *)d->spare;
e3c2a365 2565 if (c)
2566 vdc = &c->conf;
2567 } else {
2568 unsigned int dummy;
2569 c = d->vlist[i];
2570 if (c)
2571 get_pd_index_from_refnum(
2572 c, d->disk.refnum,
2573 ddf->mppe,
2574 (const struct vd_config **)&vdc,
2575 &dummy);
2576 }
7f798aca 2577 if (c) {
dacf3dc5 2578 vdc->seqnum = header->seq;
e3c2a365 2579 vdc->crc = calc_crc(vdc, conf_size);
2580 if (write(fd, vdc, conf_size) < 0)
7f798aca 2581 break;
2582 } else {
2583 unsigned int togo = conf_size;
2584 while (togo > NULL_CONF_SZ) {
2585 if (write(fd, null_aligned, NULL_CONF_SZ) < 0)
2586 break;
2587 togo -= NULL_CONF_SZ;
2588 }
2589 if (write(fd, null_aligned, togo) < 0)
2590 break;
2591 }
2592 }
2593 if (i <= n_config)
a4057a88 2594 goto out;
7f798aca 2595
2596 d->disk.crc = calc_crc(&d->disk, 512);
2597 if (write(fd, &d->disk, 512) < 0)
a4057a88 2598 goto out;
7f798aca 2599
a4057a88 2600 ret = 1;
2601out:
2602 header->openflag = 0;
2603 header->crc = calc_crc(header, 512);
2604
2605 lseek64(fd, sector<<9, 0);
2606 if (write(fd, header, 512) < 0)
2607 ret = 0;
2608
2609 return ret;
7f798aca 2610}
2611
2612static int __write_init_super_ddf(struct supertype *st)
2613{
a322f70c 2614 struct ddf_super *ddf = st->sb;
a322f70c 2615 struct dl *d;
175593bf
DW
2616 int attempts = 0;
2617 int successes = 0;
7f798aca 2618 unsigned long long size;
42d5dfd9 2619 char *null_aligned;
0175cbf6 2620 __u32 seq;
42d5dfd9 2621
7d5a7ff3 2622 pr_state(ddf, __func__);
42d5dfd9
JS
2623 if (posix_memalign((void**)&null_aligned, 4096, NULL_CONF_SZ) != 0) {
2624 return -ENOMEM;
2625 }
2626 memset(null_aligned, 0xff, NULL_CONF_SZ);
a322f70c 2627
dc9e279c 2628 seq = ddf->active->seq + 1;
0175cbf6 2629
175593bf
DW
2630 /* try to write updated metadata,
2631 * if we catch a failure move on to the next disk
2632 */
a322f70c
DW
2633 for (d = ddf->dlist; d; d=d->next) {
2634 int fd = d->fd;
2635
2636 if (fd < 0)
2637 continue;
2638
175593bf 2639 attempts++;
a322f70c
DW
2640 /* We need to fill in the primary, (secondary) and workspace
2641 * lba's in the headers, set their checksums,
2642 * Also checksum phys, virt....
2643 *
2644 * Then write everything out, finally the anchor is written.
2645 */
2646 get_dev_size(fd, NULL, &size);
2647 size /= 512;
097bcf00 2648 if (d->workspace_lba != 0)
2649 ddf->anchor.workspace_lba = d->workspace_lba;
2650 else
2651 ddf->anchor.workspace_lba =
2652 __cpu_to_be64(size - 32*1024*2);
2653 if (d->primary_lba != 0)
2654 ddf->anchor.primary_lba = d->primary_lba;
2655 else
2656 ddf->anchor.primary_lba =
2657 __cpu_to_be64(size - 16*1024*2);
2658 if (d->secondary_lba != 0)
2659 ddf->anchor.secondary_lba = d->secondary_lba;
2660 else
2661 ddf->anchor.secondary_lba =
2662 __cpu_to_be64(size - 32*1024*2);
0175cbf6 2663 ddf->anchor.seq = seq;
a322f70c
DW
2664 memcpy(&ddf->primary, &ddf->anchor, 512);
2665 memcpy(&ddf->secondary, &ddf->anchor, 512);
2666
2667 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
2668 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
2669 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
2670
7f798aca 2671 if (!__write_ddf_structure(d, ddf, DDF_HEADER_PRIMARY,
2672 null_aligned))
175593bf 2673 continue;
a322f70c 2674
7f798aca 2675 if (!__write_ddf_structure(d, ddf, DDF_HEADER_SECONDARY,
2676 null_aligned))
175593bf 2677 continue;
a322f70c 2678
a322f70c 2679 lseek64(fd, (size-1)*512, SEEK_SET);
175593bf
DW
2680 if (write(fd, &ddf->anchor, 512) < 0)
2681 continue;
2682 successes++;
2683 }
42d5dfd9 2684 free(null_aligned);
175593bf 2685
175593bf 2686 return attempts != successes;
a322f70c 2687}
7a7cc504
NB
2688
2689static int write_init_super_ddf(struct supertype *st)
2690{
9b1fb677
DW
2691 struct ddf_super *ddf = st->sb;
2692 struct vcl *currentconf = ddf->currentconf;
2693
2694 /* we are done with currentconf reset it to point st at the container */
2695 ddf->currentconf = NULL;
edd8d13c
NB
2696
2697 if (st->update_tail) {
2698 /* queue the virtual_disk and vd_config as metadata updates */
2699 struct virtual_disk *vd;
2700 struct vd_config *vc;
edd8d13c
NB
2701 int len;
2702
9b1fb677 2703 if (!currentconf) {
2cc2983d
N
2704 int len = (sizeof(struct phys_disk) +
2705 sizeof(struct phys_disk_entry));
2706
2707 /* adding a disk to the container. */
2708 if (!ddf->add_list)
2709 return 0;
2710
2711 append_metadata_update(st, ddf->add_list->mdupdate, len);
2712 ddf->add_list->mdupdate = NULL;
2713 return 0;
2714 }
2715
2716 /* Newly created VD */
2717
edd8d13c
NB
2718 /* First the virtual disk. We have a slightly fake header */
2719 len = sizeof(struct virtual_disk) + sizeof(struct virtual_entry);
503975b9 2720 vd = xmalloc(len);
edd8d13c 2721 *vd = *ddf->virt;
9b1fb677
DW
2722 vd->entries[0] = ddf->virt->entries[currentconf->vcnum];
2723 vd->populated_vdes = __cpu_to_be16(currentconf->vcnum);
edd8d13c
NB
2724 append_metadata_update(st, vd, len);
2725
2726 /* Then the vd_config */
2727 len = ddf->conf_rec_len * 512;
503975b9 2728 vc = xmalloc(len);
9b1fb677 2729 memcpy(vc, &currentconf->conf, len);
edd8d13c
NB
2730 append_metadata_update(st, vc, len);
2731
2732 /* FIXME I need to close the fds! */
2733 return 0;
613b0d17 2734 } else {
d682f344
N
2735 struct dl *d;
2736 for (d = ddf->dlist; d; d=d->next)
ba728be7 2737 while (Kill(d->devname, NULL, 0, -1, 1) == 0);
1cc7f4fe 2738 return __write_init_super_ddf(st);
d682f344 2739 }
7a7cc504
NB
2740}
2741
a322f70c
DW
2742#endif
2743
387fcd59
N
2744static __u64 avail_size_ddf(struct supertype *st, __u64 devsize,
2745 unsigned long long data_offset)
a322f70c
DW
2746{
2747 /* We must reserve the last 32Meg */
2748 if (devsize <= 32*1024*2)
2749 return 0;
2750 return devsize - 32*1024*2;
2751}
2752
2753#ifndef MDASSEMBLE
8592f29d
N
2754
2755static int reserve_space(struct supertype *st, int raiddisks,
2756 unsigned long long size, int chunk,
2757 unsigned long long *freesize)
2758{
2759 /* Find 'raiddisks' spare extents at least 'size' big (but
2760 * only caring about multiples of 'chunk') and remember
2761 * them.
2762 * If the cannot be found, fail.
2763 */
2764 struct dl *dl;
2765 struct ddf_super *ddf = st->sb;
2766 int cnt = 0;
2767
2768 for (dl = ddf->dlist; dl ; dl=dl->next) {
613b0d17 2769 dl->raiddisk = -1;
8592f29d
N
2770 dl->esize = 0;
2771 }
2772 /* Now find largest extent on each device */
2773 for (dl = ddf->dlist ; dl ; dl=dl->next) {
2774 struct extent *e = get_extents(ddf, dl);
2775 unsigned long long pos = 0;
2776 int i = 0;
2777 int found = 0;
2778 unsigned long long minsize = size;
2779
2780 if (size == 0)
2781 minsize = chunk;
2782
2783 if (!e)
2784 continue;
2785 do {
2786 unsigned long long esize;
2787 esize = e[i].start - pos;
2788 if (esize >= minsize) {
2789 found = 1;
2790 minsize = esize;
2791 }
2792 pos = e[i].start + e[i].size;
2793 i++;
2794 } while (e[i-1].size);
2795 if (found) {
2796 cnt++;
2797 dl->esize = minsize;
2798 }
2799 free(e);
2800 }
2801 if (cnt < raiddisks) {
e7b84f9d 2802 pr_err("not enough devices with space to create array.\n");
8592f29d
N
2803 return 0; /* No enough free spaces large enough */
2804 }
2805 if (size == 0) {
2806 /* choose the largest size of which there are at least 'raiddisk' */
2807 for (dl = ddf->dlist ; dl ; dl=dl->next) {
2808 struct dl *dl2;
2809 if (dl->esize <= size)
2810 continue;
2811 /* This is bigger than 'size', see if there are enough */
2812 cnt = 0;
7b80ad6a 2813 for (dl2 = ddf->dlist; dl2 ; dl2=dl2->next)
8592f29d
N
2814 if (dl2->esize >= dl->esize)
2815 cnt++;
2816 if (cnt >= raiddisks)
2817 size = dl->esize;
2818 }
2819 if (chunk) {
2820 size = size / chunk;
2821 size *= chunk;
2822 }
2823 *freesize = size;
2824 if (size < 32) {
e7b84f9d 2825 pr_err("not enough spare devices to create array.\n");
8592f29d
N
2826 return 0;
2827 }
2828 }
2829 /* We have a 'size' of which there are enough spaces.
2830 * We simply do a first-fit */
2831 cnt = 0;
2832 for (dl = ddf->dlist ; dl && cnt < raiddisks ; dl=dl->next) {
2833 if (dl->esize < size)
2834 continue;
613b0d17 2835
8592f29d
N
2836 dl->raiddisk = cnt;
2837 cnt++;
2838 }
2839 return 1;
2840}
2841
2c514b71
NB
2842static int
2843validate_geometry_ddf_container(struct supertype *st,
2844 int level, int layout, int raiddisks,
2845 int chunk, unsigned long long size,
af4348dd 2846 unsigned long long data_offset,
2c514b71
NB
2847 char *dev, unsigned long long *freesize,
2848 int verbose);
78e44928
NB
2849
2850static int validate_geometry_ddf_bvd(struct supertype *st,
2851 int level, int layout, int raiddisks,
c21e737b 2852 int *chunk, unsigned long long size,
af4348dd 2853 unsigned long long data_offset,
2c514b71
NB
2854 char *dev, unsigned long long *freesize,
2855 int verbose);
78e44928
NB
2856
2857static int validate_geometry_ddf(struct supertype *st,
2c514b71 2858 int level, int layout, int raiddisks,
c21e737b 2859 int *chunk, unsigned long long size,
af4348dd 2860 unsigned long long data_offset,
2c514b71
NB
2861 char *dev, unsigned long long *freesize,
2862 int verbose)
a322f70c
DW
2863{
2864 int fd;
2865 struct mdinfo *sra;
2866 int cfd;
2867
2868 /* ddf potentially supports lots of things, but it depends on
2869 * what devices are offered (and maybe kernel version?)
2870 * If given unused devices, we will make a container.
2871 * If given devices in a container, we will make a BVD.
2872 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2873 */
2874
bb7295f1
N
2875 if (chunk && *chunk == UnSet)
2876 *chunk = DEFAULT_CHUNK;
2877
542ef4ec 2878 if (level == -1000000) level = LEVEL_CONTAINER;
a322f70c 2879 if (level == LEVEL_CONTAINER) {
78e44928
NB
2880 /* Must be a fresh device to add to a container */
2881 return validate_geometry_ddf_container(st, level, layout,
c21e737b 2882 raiddisks, chunk?*chunk:0,
af4348dd
N
2883 size, data_offset, dev,
2884 freesize,
2c514b71 2885 verbose);
5f8097be
NB
2886 }
2887
78e44928
NB
2888 if (!dev) {
2889 /* Initial sanity check. Exclude illegal levels. */
2890 int i;
2891 for (i=0; ddf_level_num[i].num1 != MAXINT; i++)
2892 if (ddf_level_num[i].num2 == level)
2893 break;
b42f577a
N
2894 if (ddf_level_num[i].num1 == MAXINT) {
2895 if (verbose)
e7b84f9d 2896 pr_err("DDF does not support level %d arrays\n",
613b0d17 2897 level);
78e44928 2898 return 0;
b42f577a 2899 }
78e44928 2900 /* Should check layout? etc */
8592f29d
N
2901
2902 if (st->sb && freesize) {
2903 /* --create was given a container to create in.
2904 * So we need to check that there are enough
2905 * free spaces and return the amount of space.
2906 * We may as well remember which drives were
2907 * chosen so that add_to_super/getinfo_super
2908 * can return them.
2909 */
c21e737b 2910 return reserve_space(st, raiddisks, size, chunk?*chunk:0, freesize);
8592f29d 2911 }
a322f70c 2912 return 1;
78e44928 2913 }
a322f70c 2914
8592f29d
N
2915 if (st->sb) {
2916 /* A container has already been opened, so we are
2917 * creating in there. Maybe a BVD, maybe an SVD.
2918 * Should make a distinction one day.
2919 */
2920 return validate_geometry_ddf_bvd(st, level, layout, raiddisks,
af4348dd
N
2921 chunk, size, data_offset, dev,
2922 freesize,
8592f29d
N
2923 verbose);
2924 }
78e44928
NB
2925 /* This is the first device for the array.
2926 * If it is a container, we read it in and do automagic allocations,
2927 * no other devices should be given.
2928 * Otherwise it must be a member device of a container, and we
2929 * do manual allocation.
2930 * Later we should check for a BVD and make an SVD.
a322f70c 2931 */
a322f70c
DW
2932 fd = open(dev, O_RDONLY|O_EXCL, 0);
2933 if (fd >= 0) {
4dd2df09 2934 sra = sysfs_read(fd, NULL, GET_VERSION);
a322f70c
DW
2935 close(fd);
2936 if (sra && sra->array.major_version == -1 &&
78e44928
NB
2937 strcmp(sra->text_version, "ddf") == 0) {
2938
2939 /* load super */
2940 /* find space for 'n' devices. */
2941 /* remember the devices */
2942 /* Somehow return the fact that we have enough */
a322f70c
DW
2943 }
2944
2c514b71 2945 if (verbose)
e7b84f9d
N
2946 pr_err("ddf: Cannot create this array "
2947 "on device %s - a container is required.\n",
2948 dev);
a322f70c
DW
2949 return 0;
2950 }
2951 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2c514b71 2952 if (verbose)
e7b84f9d 2953 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 2954 dev, strerror(errno));
a322f70c
DW
2955 return 0;
2956 }
2957 /* Well, it is in use by someone, maybe a 'ddf' container. */
2958 cfd = open_container(fd);
2959 if (cfd < 0) {
2960 close(fd);
2c514b71 2961 if (verbose)
e7b84f9d 2962 pr_err("ddf: Cannot use %s: %s\n",
613b0d17 2963 dev, strerror(EBUSY));
a322f70c
DW
2964 return 0;
2965 }
4dd2df09 2966 sra = sysfs_read(cfd, NULL, GET_VERSION);
a322f70c
DW
2967 close(fd);
2968 if (sra && sra->array.major_version == -1 &&
2969 strcmp(sra->text_version, "ddf") == 0) {
2970 /* This is a member of a ddf container. Load the container
2971 * and try to create a bvd
2972 */
2973 struct ddf_super *ddf;
e1902a7b 2974 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL) == 0) {
5f8097be 2975 st->sb = ddf;
4dd2df09 2976 strcpy(st->container_devnm, fd2devnm(cfd));
a322f70c 2977 close(cfd);
78e44928 2978 return validate_geometry_ddf_bvd(st, level, layout,
a322f70c 2979 raiddisks, chunk, size,
af4348dd 2980 data_offset,
2c514b71
NB
2981 dev, freesize,
2982 verbose);
a322f70c
DW
2983 }
2984 close(cfd);
c42ec1ed
DW
2985 } else /* device may belong to a different container */
2986 return 0;
2987
a322f70c
DW
2988 return 1;
2989}
2990
2c514b71
NB
2991static int
2992validate_geometry_ddf_container(struct supertype *st,
2993 int level, int layout, int raiddisks,
2994 int chunk, unsigned long long size,
af4348dd 2995 unsigned long long data_offset,
2c514b71
NB
2996 char *dev, unsigned long long *freesize,
2997 int verbose)
a322f70c
DW
2998{
2999 int fd;
3000 unsigned long long ldsize;
3001
3002 if (level != LEVEL_CONTAINER)
3003 return 0;
3004 if (!dev)
3005 return 1;
3006
3007 fd = open(dev, O_RDONLY|O_EXCL, 0);
3008 if (fd < 0) {
2c514b71 3009 if (verbose)
e7b84f9d 3010 pr_err("ddf: Cannot open %s: %s\n",
613b0d17 3011 dev, strerror(errno));
a322f70c
DW
3012 return 0;
3013 }
3014 if (!get_dev_size(fd, dev, &ldsize)) {
3015 close(fd);
3016 return 0;
3017 }
3018 close(fd);
3019
387fcd59 3020 *freesize = avail_size_ddf(st, ldsize >> 9, INVALID_SECTORS);
ea17e7aa
N
3021 if (*freesize == 0)
3022 return 0;
a322f70c
DW
3023
3024 return 1;
3025}
3026
78e44928
NB
3027static int validate_geometry_ddf_bvd(struct supertype *st,
3028 int level, int layout, int raiddisks,
c21e737b 3029 int *chunk, unsigned long long size,
af4348dd 3030 unsigned long long data_offset,
2c514b71
NB
3031 char *dev, unsigned long long *freesize,
3032 int verbose)
a322f70c
DW
3033{
3034 struct stat stb;
3035 struct ddf_super *ddf = st->sb;
3036 struct dl *dl;
5f8097be
NB
3037 unsigned long long pos = 0;
3038 unsigned long long maxsize;
3039 struct extent *e;
3040 int i;
a322f70c 3041 /* ddf/bvd supports lots of things, but not containers */
b42f577a
N
3042 if (level == LEVEL_CONTAINER) {
3043 if (verbose)
e7b84f9d 3044 pr_err("DDF cannot create a container within an container\n");
a322f70c 3045 return 0;
b42f577a 3046 }
a322f70c
DW
3047 /* We must have the container info already read in. */
3048 if (!ddf)
3049 return 0;
3050
5f8097be
NB
3051 if (!dev) {
3052 /* General test: make sure there is space for
3053 * 'raiddisks' device extents of size 'size'.
3054 */
3055 unsigned long long minsize = size;
3056 int dcnt = 0;
3057 if (minsize == 0)
3058 minsize = 8;
3059 for (dl = ddf->dlist; dl ; dl = dl->next)
3060 {
3061 int found = 0;
7e1432fb 3062 pos = 0;
5f8097be
NB
3063
3064 i = 0;
3065 e = get_extents(ddf, dl);
3066 if (!e) continue;
3067 do {
3068 unsigned long long esize;
3069 esize = e[i].start - pos;
3070 if (esize >= minsize)
3071 found = 1;
3072 pos = e[i].start + e[i].size;
3073 i++;
3074 } while (e[i-1].size);
3075 if (found)
3076 dcnt++;
3077 free(e);
3078 }
3079 if (dcnt < raiddisks) {
2c514b71 3080 if (verbose)
e7b84f9d
N
3081 pr_err("ddf: Not enough devices with "
3082 "space for this array (%d < %d)\n",
3083 dcnt, raiddisks);
5f8097be
NB
3084 return 0;
3085 }
3086 return 1;
3087 }
a322f70c
DW
3088 /* This device must be a member of the set */
3089 if (stat(dev, &stb) < 0)
3090 return 0;
3091 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3092 return 0;
3093 for (dl = ddf->dlist ; dl ; dl = dl->next) {
f21e18ca
N
3094 if (dl->major == (int)major(stb.st_rdev) &&
3095 dl->minor == (int)minor(stb.st_rdev))
a322f70c
DW
3096 break;
3097 }
5f8097be 3098 if (!dl) {
2c514b71 3099 if (verbose)
e7b84f9d 3100 pr_err("ddf: %s is not in the "
613b0d17
N
3101 "same DDF set\n",
3102 dev);
5f8097be
NB
3103 return 0;
3104 }
3105 e = get_extents(ddf, dl);
3106 maxsize = 0;
3107 i = 0;
3108 if (e) do {
613b0d17
N
3109 unsigned long long esize;
3110 esize = e[i].start - pos;
3111 if (esize >= maxsize)
3112 maxsize = esize;
3113 pos = e[i].start + e[i].size;
3114 i++;
3115 } while (e[i-1].size);
5f8097be 3116 *freesize = maxsize;
a322f70c
DW
3117 // FIXME here I am
3118
3119 return 1;
3120}
59e36268 3121
a322f70c 3122static int load_super_ddf_all(struct supertype *st, int fd,
e1902a7b 3123 void **sbp, char *devname)
a322f70c
DW
3124{
3125 struct mdinfo *sra;
3126 struct ddf_super *super;
3127 struct mdinfo *sd, *best = NULL;
3128 int bestseq = 0;
3129 int seq;
3130 char nm[20];
3131 int dfd;
3132
b526e52d 3133 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
a322f70c
DW
3134 if (!sra)
3135 return 1;
3136 if (sra->array.major_version != -1 ||
3137 sra->array.minor_version != -2 ||
3138 strcmp(sra->text_version, "ddf") != 0)
3139 return 1;
3140
6416d527 3141 if (posix_memalign((void**)&super, 512, sizeof(*super)) != 0)
a322f70c 3142 return 1;
a2349791 3143 memset(super, 0, sizeof(*super));
a322f70c
DW
3144
3145 /* first, try each device, and choose the best ddf */
3146 for (sd = sra->devs ; sd ; sd = sd->next) {
3147 int rv;
3148 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
7a7cc504
NB
3149 dfd = dev_open(nm, O_RDONLY);
3150 if (dfd < 0)
a322f70c
DW
3151 return 2;
3152 rv = load_ddf_headers(dfd, super, NULL);
7a7cc504 3153 close(dfd);
a322f70c
DW
3154 if (rv == 0) {
3155 seq = __be32_to_cpu(super->active->seq);
3156 if (super->active->openflag)
3157 seq--;
3158 if (!best || seq > bestseq) {
3159 bestseq = seq;
3160 best = sd;
3161 }
3162 }
3163 }
3164 if (!best)
3165 return 1;
3166 /* OK, load this ddf */
3167 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
3168 dfd = dev_open(nm, O_RDONLY);
7a7cc504 3169 if (dfd < 0)
a322f70c
DW
3170 return 1;
3171 load_ddf_headers(dfd, super, NULL);
3172 load_ddf_global(dfd, super, NULL);
3173 close(dfd);
3174 /* Now we need the device-local bits */
3175 for (sd = sra->devs ; sd ; sd = sd->next) {
3d2c4fc7
DW
3176 int rv;
3177
a322f70c 3178 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
e1902a7b 3179 dfd = dev_open(nm, O_RDWR);
7a7cc504 3180 if (dfd < 0)
a322f70c 3181 return 2;
3d2c4fc7
DW
3182 rv = load_ddf_headers(dfd, super, NULL);
3183 if (rv == 0)
e1902a7b 3184 rv = load_ddf_local(dfd, super, NULL, 1);
3d2c4fc7
DW
3185 if (rv)
3186 return 1;
a322f70c 3187 }
33414a01 3188
a322f70c
DW
3189 *sbp = super;
3190 if (st->ss == NULL) {
78e44928 3191 st->ss = &super_ddf;
a322f70c
DW
3192 st->minor_version = 0;
3193 st->max_devs = 512;
3194 }
4dd2df09 3195 strcpy(st->container_devnm, fd2devnm(fd));
a322f70c
DW
3196 return 0;
3197}
2b959fbf
N
3198
3199static int load_container_ddf(struct supertype *st, int fd,
3200 char *devname)
3201{
3202 return load_super_ddf_all(st, fd, &st->sb, devname);
3203}
3204
0e600426 3205#endif /* MDASSEMBLE */
a322f70c 3206
a5c7adb3 3207static int check_secondary(const struct vcl *vc)
3208{
3209 const struct vd_config *conf = &vc->conf;
3210 int i;
3211
3212 /* The only DDF secondary RAID level md can support is
3213 * RAID 10, if the stripe sizes and Basic volume sizes
3214 * are all equal.
3215 * Other configurations could in theory be supported by exposing
3216 * the BVDs to user space and using device mapper for the secondary
3217 * mapping. So far we don't support that.
3218 */
3219
3220 __u64 sec_elements[4] = {0, 0, 0, 0};
3221#define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3222#define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3223
3224 if (vc->other_bvds == NULL) {
3225 pr_err("No BVDs for secondary RAID found\n");
3226 return -1;
3227 }
3228 if (conf->prl != DDF_RAID1) {
3229 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3230 return -1;
3231 }
3232 if (conf->srl != DDF_2STRIPED && conf->srl != DDF_2SPANNED) {
3233 pr_err("Secondary RAID level %d is unsupported\n",
3234 conf->srl);
3235 return -1;
3236 }
3237 __set_sec_seen(conf->sec_elmnt_seq);
3238 for (i = 0; i < conf->sec_elmnt_count-1; i++) {
3239 const struct vd_config *bvd = vc->other_bvds[i];
c98567ba 3240 if (bvd == NULL)
3241 continue;
a5c7adb3 3242 if (bvd->srl != conf->srl) {
3243 pr_err("Inconsistent secondary RAID level across BVDs\n");
3244 return -1;
3245 }
3246 if (bvd->prl != conf->prl) {
3247 pr_err("Different RAID levels for BVDs are unsupported\n");
3248 return -1;
3249 }
3250 if (bvd->prim_elmnt_count != conf->prim_elmnt_count) {
3251 pr_err("All BVDs must have the same number of primary elements\n");
3252 return -1;
3253 }
3254 if (bvd->chunk_shift != conf->chunk_shift) {
3255 pr_err("Different strip sizes for BVDs are unsupported\n");
3256 return -1;
3257 }
3258 if (bvd->array_blocks != conf->array_blocks) {
3259 pr_err("Different BVD sizes are unsupported\n");
3260 return -1;
3261 }
3262 __set_sec_seen(bvd->sec_elmnt_seq);
3263 }
3264 for (i = 0; i < conf->sec_elmnt_count; i++) {
3265 if (!__was_sec_seen(i)) {
3266 pr_err("BVD %d is missing\n", i);
3267 return -1;
3268 }
3269 }
3270 return 0;
3271}
3272
8a38db86 3273static unsigned int get_pd_index_from_refnum(const struct vcl *vc,
4e587018 3274 __u32 refnum, unsigned int nmax,
3275 const struct vd_config **bvd,
3276 unsigned int *idx)
8a38db86 3277{
4e587018 3278 unsigned int i, j, n, sec, cnt;
3279
3280 cnt = __be16_to_cpu(vc->conf.prim_elmnt_count);
3281 sec = (vc->conf.sec_elmnt_count == 1 ? 0 : vc->conf.sec_elmnt_seq);
3282
3283 for (i = 0, j = 0 ; i < nmax ; i++) {
3284 /* j counts valid entries for this BVD */
3285 if (vc->conf.phys_refnum[i] != 0xffffffff)
3286 j++;
3287 if (vc->conf.phys_refnum[i] == refnum) {
3288 *bvd = &vc->conf;
3289 *idx = i;
3290 return sec * cnt + j - 1;
3291 }
3292 }
3293 if (vc->other_bvds == NULL)
3294 goto bad;
3295
3296 for (n = 1; n < vc->conf.sec_elmnt_count; n++) {
3297 struct vd_config *vd = vc->other_bvds[n-1];
3298 if (vd == NULL)
3299 continue;
3300 sec = vd->sec_elmnt_seq;
3301 for (i = 0, j = 0 ; i < nmax ; i++) {
3302 if (vd->phys_refnum[i] != 0xffffffff)
3303 j++;
3304 if (vd->phys_refnum[i] == refnum) {
3305 *bvd = vd;
3306 *idx = i;
3307 return sec * cnt + j - 1;
3308 }
3309 }
3310 }
3311bad:
3312 *bvd = NULL;
d6e7b083 3313 return DDF_NOTFOUND;
8a38db86 3314}
3315
00bbdbda 3316static struct mdinfo *container_content_ddf(struct supertype *st, char *subarray)
598f0d58
NB
3317{
3318 /* Given a container loaded by load_super_ddf_all,
3319 * extract information about all the arrays into
3320 * an mdinfo tree.
3321 *
3322 * For each vcl in conflist: create an mdinfo, fill it in,
3323 * then look for matching devices (phys_refnum) in dlist
3324 * and create appropriate device mdinfo.
3325 */
3326 struct ddf_super *ddf = st->sb;
3327 struct mdinfo *rest = NULL;
3328 struct vcl *vc;
3329
3330 for (vc = ddf->conflist ; vc ; vc=vc->next)
3331 {
f21e18ca
N
3332 unsigned int i;
3333 unsigned int j;
598f0d58 3334 struct mdinfo *this;
00bbdbda 3335 char *ep;
90fa1a29 3336 __u32 *cptr;
8a38db86 3337 unsigned int pd;
00bbdbda
N
3338
3339 if (subarray &&
3340 (strtoul(subarray, &ep, 10) != vc->vcnum ||
3341 *ep != '\0'))
3342 continue;
3343
a5c7adb3 3344 if (vc->conf.sec_elmnt_count > 1) {
3345 if (check_secondary(vc) != 0)
3346 continue;
3347 }
3348
503975b9 3349 this = xcalloc(1, sizeof(*this));
598f0d58
NB
3350 this->next = rest;
3351 rest = this;
3352
4e587018 3353 if (vc->conf.sec_elmnt_count == 1) {
3354 this->array.level = map_num1(ddf_level_num,
3355 vc->conf.prl);
3356 this->array.raid_disks =
3357 __be16_to_cpu(vc->conf.prim_elmnt_count);
3358 this->array.layout =
3359 rlq_to_layout(vc->conf.rlq, vc->conf.prl,
3360 this->array.raid_disks);
3361 } else {
3362 /* The only supported layout is RAID 10.
3363 * Compatibility has been checked in check_secondary()
3364 * above.
3365 */
3366 this->array.level = 10;
3367 this->array.raid_disks =
3368 __be16_to_cpu(vc->conf.prim_elmnt_count)
3369 * vc->conf.sec_elmnt_count;
3370 this->array.layout = 0x100 |
3371 __be16_to_cpu(vc->conf.prim_elmnt_count);
3372 }
598f0d58 3373 this->array.md_minor = -1;
f35f2525
N
3374 this->array.major_version = -1;
3375 this->array.minor_version = -2;
90fa1a29
JS
3376 cptr = (__u32 *)(vc->conf.guid + 16);
3377 this->array.ctime = DECADE + __be32_to_cpu(*cptr);
598f0d58
NB
3378 this->array.utime = DECADE +
3379 __be32_to_cpu(vc->conf.timestamp);
3380 this->array.chunk_size = 512 << vc->conf.chunk_shift;
3381
59e36268 3382 i = vc->vcnum;
7a7cc504
NB
3383 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
3384 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
ed9d66aa 3385 DDF_init_full) {
598f0d58 3386 this->array.state = 0;
ed9d66aa
NB
3387 this->resync_start = 0;
3388 } else {
598f0d58 3389 this->array.state = 1;
b7528a20 3390 this->resync_start = MaxSector;
ed9d66aa 3391 }
db42fa9b
N
3392 memcpy(this->name, ddf->virt->entries[i].name, 16);
3393 this->name[16]=0;
3394 for(j=0; j<16; j++)
3395 if (this->name[j] == ' ')
3396 this->name[j] = 0;
598f0d58
NB
3397
3398 memset(this->uuid, 0, sizeof(this->uuid));
3399 this->component_size = __be64_to_cpu(vc->conf.blocks);
3400 this->array.size = this->component_size / 2;
5f2aace8 3401 this->container_member = i;
598f0d58 3402
c5afc314
N
3403 ddf->currentconf = vc;
3404 uuid_from_super_ddf(st, this->uuid);
3405 ddf->currentconf = NULL;
3406
60f18132 3407 sprintf(this->text_version, "/%s/%d",
4dd2df09 3408 st->container_devnm, this->container_member);
60f18132 3409
8a38db86 3410 for (pd = 0; pd < __be16_to_cpu(ddf->phys->used_pdes); pd++) {
598f0d58
NB
3411 struct mdinfo *dev;
3412 struct dl *d;
4e587018 3413 const struct vd_config *bvd;
3414 unsigned int iphys;
3415 __u64 *lba_offset;
fa033bec 3416 int stt;
598f0d58 3417
8a38db86 3418 if (ddf->phys->entries[pd].refnum == 0xFFFFFFFF)
bc17324f 3419 continue;
0cf5ef67
N
3420
3421 stt = __be16_to_cpu(ddf->phys->entries[pd].state);
fa033bec
N
3422 if ((stt & (DDF_Online|DDF_Failed|DDF_Rebuilding))
3423 != DDF_Online)
3424 continue;
3425
8a38db86 3426 i = get_pd_index_from_refnum(
4e587018 3427 vc, ddf->phys->entries[pd].refnum,
3428 ddf->mppe, &bvd, &iphys);
d6e7b083 3429 if (i == DDF_NOTFOUND)
8a38db86 3430 continue;
3431
fa033bec 3432 this->array.working_disks++;
bc17324f 3433
0cf5ef67 3434 for (d = ddf->dlist; d ; d=d->next)
8a38db86 3435 if (d->disk.refnum ==
3436 ddf->phys->entries[pd].refnum)
0cf5ef67
N
3437 break;
3438 if (d == NULL)
3439 /* Haven't found that one yet, maybe there are others */
3440 continue;
3441
503975b9 3442 dev = xcalloc(1, sizeof(*dev));
598f0d58
NB
3443 dev->next = this->devs;
3444 this->devs = dev;
3445
3446 dev->disk.number = __be32_to_cpu(d->disk.refnum);
3447 dev->disk.major = d->major;
3448 dev->disk.minor = d->minor;
3449 dev->disk.raid_disk = i;
3450 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
d23534e4 3451 dev->recovery_start = MaxSector;
598f0d58 3452
120f7677 3453 dev->events = __be32_to_cpu(ddf->primary.seq);
4e587018 3454 lba_offset = (__u64 *)&bvd->phys_refnum[ddf->mppe];
3455 dev->data_offset = __be64_to_cpu(lba_offset[iphys]);
3456 dev->component_size = __be64_to_cpu(bvd->blocks);
598f0d58
NB
3457 if (d->devname)
3458 strcpy(dev->name, d->devname);
3459 }
3460 }
3461 return rest;
3462}
3463
955e9ea1 3464static int store_super_ddf(struct supertype *st, int fd)
a322f70c 3465{
955e9ea1 3466 struct ddf_super *ddf = st->sb;
a322f70c 3467 unsigned long long dsize;
6416d527 3468 void *buf;
3d2c4fc7 3469 int rc;
a322f70c 3470
955e9ea1
DW
3471 if (!ddf)
3472 return 1;
3473
a322f70c
DW
3474 if (!get_dev_size(fd, NULL, &dsize))
3475 return 1;
3476
dbf98368 3477 if (ddf->dlist || ddf->conflist) {
3478 struct stat sta;
3479 struct dl *dl;
3480 int ofd, ret;
3481
3482 if (fstat(fd, &sta) == -1 || !S_ISBLK(sta.st_mode)) {
3483 pr_err("%s: file descriptor for invalid device\n",
3484 __func__);
3485 return 1;
3486 }
3487 for (dl = ddf->dlist; dl; dl = dl->next)
3488 if (dl->major == (int)major(sta.st_rdev) &&
3489 dl->minor == (int)minor(sta.st_rdev))
3490 break;
3491 if (!dl) {
3492 pr_err("%s: couldn't find disk %d/%d\n", __func__,
3493 (int)major(sta.st_rdev),
3494 (int)minor(sta.st_rdev));
3495 return 1;
3496 }
3497 /*
3498 For DDF, writing to just one disk makes no sense.
3499 We would run the risk of writing inconsistent meta data
3500 to the devices. So just call __write_init_super_ddf and
3501 write to all devices, including this one.
3502 Use the fd passed to this function, just in case dl->fd
3503 is invalid.
3504 */
3505 ofd = dl->fd;
3506 dl->fd = fd;
3507 ret = __write_init_super_ddf(st);
3508 dl->fd = ofd;
3509 return ret;
3510 }
3511
3d2c4fc7
DW
3512 if (posix_memalign(&buf, 512, 512) != 0)
3513 return 1;
6416d527
NB
3514 memset(buf, 0, 512);
3515
a322f70c 3516 lseek64(fd, dsize-512, 0);
3d2c4fc7 3517 rc = write(fd, buf, 512);
6416d527 3518 free(buf);
3d2c4fc7
DW
3519 if (rc < 0)
3520 return 1;
a322f70c
DW
3521 return 0;
3522}
3523
a19c88b8
NB
3524static int compare_super_ddf(struct supertype *st, struct supertype *tst)
3525{
3526 /*
3527 * return:
3528 * 0 same, or first was empty, and second was copied
3529 * 1 second had wrong number
3530 * 2 wrong uuid
3531 * 3 wrong other info
3532 */
3533 struct ddf_super *first = st->sb;
3534 struct ddf_super *second = tst->sb;
4eefd651 3535 struct dl *dl1, *dl2;
3536 struct vcl *vl1, *vl2;
2d210697 3537 unsigned int max_vds, max_pds, pd, vd;
a19c88b8
NB
3538
3539 if (!first) {
3540 st->sb = tst->sb;
3541 tst->sb = NULL;
3542 return 0;
3543 }
3544
3545 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
3546 return 2;
3547
2d210697 3548 if (first->anchor.seq != second->anchor.seq) {
3549 dprintf("%s: sequence number mismatch %u/%u\n", __func__,
3550 __be32_to_cpu(first->anchor.seq),
3551 __be32_to_cpu(second->anchor.seq));
3552 return 3;
3553 }
3554 if (first->max_part != second->max_part ||
3555 first->phys->used_pdes != second->phys->used_pdes ||
3556 first->virt->populated_vdes != second->virt->populated_vdes) {
3557 dprintf("%s: PD/VD number mismatch\n", __func__);
3558 return 3;
3559 }
3560
3561 max_pds = __be16_to_cpu(first->phys->used_pdes);
3562 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3563 for (pd = 0; pd < max_pds; pd++)
3564 if (first->phys->entries[pd].refnum == dl2->disk.refnum)
3565 break;
3566 if (pd == max_pds) {
3567 dprintf("%s: no match for disk %08x\n", __func__,
3568 __be32_to_cpu(dl2->disk.refnum));
3569 return 3;
3570 }
3571 }
3572
3573 max_vds = __be16_to_cpu(first->active->max_vd_entries);
3574 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3575 if (vl2->conf.magic != DDF_VD_CONF_MAGIC)
3576 continue;
3577 for (vd = 0; vd < max_vds; vd++)
3578 if (!memcmp(first->virt->entries[vd].guid,
3579 vl2->conf.guid, DDF_GUID_LEN))
3580 break;
3581 if (vd == max_vds) {
3582 dprintf("%s: no match for VD config\n", __func__);
3583 return 3;
3584 }
3585 }
a19c88b8 3586 /* FIXME should I look at anything else? */
2d210697 3587
4eefd651 3588 /*
3589 At this point we are fairly sure that the meta data matches.
3590 But the new disk may contain additional local data.
3591 Add it to the super block.
3592 */
3593 for (vl2 = second->conflist; vl2; vl2 = vl2->next) {
3594 for (vl1 = first->conflist; vl1; vl1 = vl1->next)
3595 if (!memcmp(vl1->conf.guid, vl2->conf.guid,
3596 DDF_GUID_LEN))
3597 break;
3598 if (vl1) {
3599 if (vl1->other_bvds != NULL &&
3600 vl1->conf.sec_elmnt_seq !=
3601 vl2->conf.sec_elmnt_seq) {
3602 dprintf("%s: adding BVD %u\n", __func__,
3603 vl2->conf.sec_elmnt_seq);
3604 add_other_bvd(vl1, &vl2->conf,
3605 first->conf_rec_len*512);
3606 }
3607 continue;
3608 }
3609
3610 if (posix_memalign((void **)&vl1, 512,
3611 (first->conf_rec_len*512 +
3612 offsetof(struct vcl, conf))) != 0) {
3613 pr_err("%s could not allocate vcl buf\n",
3614 __func__);
3615 return 3;
3616 }
3617
3618 vl1->next = first->conflist;
3619 vl1->block_sizes = NULL;
3f188b10 3620 if (vl2->conf.sec_elmnt_count > 1) {
4eefd651 3621 vl1->other_bvds = xcalloc(vl2->conf.sec_elmnt_count - 1,
3622 sizeof(struct vd_config *));
3623 } else
3624 vl1->other_bvds = NULL;
3625 memcpy(&vl1->conf, &vl2->conf, first->conf_rec_len*512);
3626 vl1->lba_offset = (__u64 *)
3627 &vl1->conf.phys_refnum[first->mppe];
3628 for (vd = 0; vd < max_vds; vd++)
3629 if (!memcmp(first->virt->entries[vd].guid,
3630 vl1->conf.guid, DDF_GUID_LEN))
3631 break;
3632 vl1->vcnum = vd;
3633 dprintf("%s: added config for VD %u\n", __func__, vl1->vcnum);
3634 first->conflist = vl1;
3635 }
3636
3637 for (dl2 = second->dlist; dl2; dl2 = dl2->next) {
3638 for (dl1 = first->dlist; dl1; dl1 = dl1->next)
3639 if (dl1->disk.refnum == dl2->disk.refnum)
3640 break;
3641 if (dl1)
3642 continue;
3643
3644 if (posix_memalign((void **)&dl1, 512,
3645 sizeof(*dl1) + (first->max_part) * sizeof(dl1->vlist[0]))
3646 != 0) {
3647 pr_err("%s could not allocate disk info buffer\n",
3648 __func__);
3649 return 3;
3650 }
3651 memcpy(dl1, dl2, sizeof(*dl1));
3652 dl1->mdupdate = NULL;
3653 dl1->next = first->dlist;
3654 dl1->fd = -1;
3655 for (pd = 0; pd < max_pds; pd++)
3656 if (first->phys->entries[pd].refnum == dl1->disk.refnum)
3657 break;
3658 dl1->pdnum = pd;
3659 if (dl2->spare) {
3660 if (posix_memalign((void **)&dl1->spare, 512,
3661 first->conf_rec_len*512) != 0) {
3662 pr_err("%s could not allocate spare info buf\n",
3663 __func__);
3664 return 3;
3665 }
3666 memcpy(dl1->spare, dl2->spare, first->conf_rec_len*512);
3667 }
3668 for (vd = 0 ; vd < first->max_part ; vd++) {
3669 if (!dl2->vlist[vd]) {
3670 dl1->vlist[vd] = NULL;
3671 continue;
3672 }
3673 for (vl1 = first->conflist; vl1; vl1 = vl1->next) {
3674 if (!memcmp(vl1->conf.guid,
3675 dl2->vlist[vd]->conf.guid,
3676 DDF_GUID_LEN))
3677 break;
3678 dl1->vlist[vd] = vl1;
3679 }
3680 }
3681 first->dlist = dl1;
3682 dprintf("%s: added disk %d: %08x\n", __func__, dl1->pdnum,
3683 dl1->disk.refnum);
3684 }
3685
a19c88b8
NB
3686 return 0;
3687}
3688
0e600426 3689#ifndef MDASSEMBLE
4e5528c6
NB
3690/*
3691 * A new array 'a' has been started which claims to be instance 'inst'
3692 * within container 'c'.
3693 * We need to confirm that the array matches the metadata in 'c' so
3694 * that we don't corrupt any metadata.
3695 */
cba0191b 3696static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst)
549e9569 3697{
2c514b71 3698 dprintf("ddf: open_new %s\n", inst);
cba0191b 3699 a->info.container_member = atoi(inst);
549e9569
NB
3700 return 0;
3701}
3702
4e5528c6
NB
3703/*
3704 * The array 'a' is to be marked clean in the metadata.
ed9d66aa 3705 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
4e5528c6
NB
3706 * clean up to the point (in sectors). If that cannot be recorded in the
3707 * metadata, then leave it as dirty.
3708 *
3709 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3710 * !global! virtual_disk.virtual_entry structure.
3711 */
01f157d7 3712static int ddf_set_array_state(struct active_array *a, int consistent)
549e9569 3713{
4e5528c6
NB
3714 struct ddf_super *ddf = a->container->sb;
3715 int inst = a->info.container_member;
18a2f463 3716 int old = ddf->virt->entries[inst].state;
01f157d7
N
3717 if (consistent == 2) {
3718 /* Should check if a recovery should be started FIXME */
3719 consistent = 1;
b7941fd6 3720 if (!is_resync_complete(&a->info))
01f157d7
N
3721 consistent = 0;
3722 }
ed9d66aa
NB
3723 if (consistent)
3724 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
3725 else
4e5528c6 3726 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
18a2f463 3727 if (old != ddf->virt->entries[inst].state)
7d5a7ff3 3728 ddf_set_updates_pending(ddf);
18a2f463
NB
3729
3730 old = ddf->virt->entries[inst].init_state;
ed9d66aa 3731 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
b7941fd6 3732 if (is_resync_complete(&a->info))
ed9d66aa 3733 ddf->virt->entries[inst].init_state |= DDF_init_full;
b7941fd6 3734 else if (a->info.resync_start == 0)
ed9d66aa 3735 ddf->virt->entries[inst].init_state |= DDF_init_not;
4e5528c6 3736 else
ed9d66aa 3737 ddf->virt->entries[inst].init_state |= DDF_init_quick;
18a2f463 3738 if (old != ddf->virt->entries[inst].init_state)
7d5a7ff3 3739 ddf_set_updates_pending(ddf);
ed9d66aa 3740
2c514b71 3741 dprintf("ddf mark %d %s %llu\n", inst, consistent?"clean":"dirty",
b7941fd6 3742 a->info.resync_start);
01f157d7 3743 return consistent;
fd7cde1b
DW
3744}
3745
e1316fab 3746#define container_of(ptr, type, member) ({ \
5d500228
N
3747 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
3748 (type *)( (char *)__mptr - offsetof(type,member) );})
7a7cc504
NB
3749/*
3750 * The state of each disk is stored in the global phys_disk structure
3751 * in phys_disk.entries[n].state.
3752 * This makes various combinations awkward.
3753 * - When a device fails in any array, it must be failed in all arrays
3754 * that include a part of this device.
3755 * - When a component is rebuilding, we cannot include it officially in the
3756 * array unless this is the only array that uses the device.
3757 *
3758 * So: when transitioning:
3759 * Online -> failed, just set failed flag. monitor will propagate
3760 * spare -> online, the device might need to be added to the array.
3761 * spare -> failed, just set failed. Don't worry if in array or not.
3762 */
8d45d196 3763static void ddf_set_disk(struct active_array *a, int n, int state)
549e9569 3764{
7a7cc504 3765 struct ddf_super *ddf = a->container->sb;
f21e18ca 3766 unsigned int inst = a->info.container_member;
7a7cc504
NB
3767 struct vd_config *vc = find_vdcr(ddf, inst);
3768 int pd = find_phys(ddf, vc->phys_refnum[n]);
3769 int i, st, working;
e1316fab
N
3770 struct mdinfo *mdi;
3771 struct dl *dl;
7a7cc504
NB
3772
3773 if (vc == NULL) {
2c514b71 3774 dprintf("ddf: cannot find instance %d!!\n", inst);
7a7cc504
NB
3775 return;
3776 }
e1316fab
N
3777 /* Find the matching slot in 'info'. */
3778 for (mdi = a->info.devs; mdi; mdi = mdi->next)
3779 if (mdi->disk.raid_disk == n)
3780 break;
3781 if (!mdi)
3782 return;
3783
3784 /* and find the 'dl' entry corresponding to that. */
3785 for (dl = ddf->dlist; dl; dl = dl->next)
77632af9
N
3786 if (mdi->state_fd >= 0 &&
3787 mdi->disk.major == dl->major &&
e1316fab
N
3788 mdi->disk.minor == dl->minor)
3789 break;
3790 if (!dl)
3791 return;
3792
3793 if (pd < 0 || pd != dl->pdnum) {
3794 /* disk doesn't currently exist or has changed.
3795 * If it is now in_sync, insert it. */
7a7cc504 3796 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
e1316fab
N
3797 struct vcl *vcl;
3798 pd = dl->pdnum;
3799 vc->phys_refnum[n] = dl->disk.refnum;
3800 vcl = container_of(vc, struct vcl, conf);
3801 vcl->lba_offset[n] = mdi->data_offset;
3802 ddf->phys->entries[pd].type &=
3803 ~__cpu_to_be16(DDF_Global_Spare);
3804 ddf->phys->entries[pd].type |=
3805 __cpu_to_be16(DDF_Active_in_VD);
7d5a7ff3 3806 ddf_set_updates_pending(ddf);
7a7cc504
NB
3807 }
3808 } else {
18a2f463 3809 int old = ddf->phys->entries[pd].state;
7a7cc504
NB
3810 if (state & DS_FAULTY)
3811 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
3812 if (state & DS_INSYNC) {
3813 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
3814 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
3815 }
18a2f463 3816 if (old != ddf->phys->entries[pd].state)
7d5a7ff3 3817 ddf_set_updates_pending(ddf);
7a7cc504
NB
3818 }
3819
2c514b71 3820 dprintf("ddf: set_disk %d to %x\n", n, state);
7e1432fb 3821
7a7cc504
NB
3822 /* Now we need to check the state of the array and update
3823 * virtual_disk.entries[n].state.
3824 * It needs to be one of "optimal", "degraded", "failed".
3825 * I don't understand 'deleted' or 'missing'.
3826 */
3827 working = 0;
3828 for (i=0; i < a->info.array.raid_disks; i++) {
3829 pd = find_phys(ddf, vc->phys_refnum[i]);
3830 if (pd < 0)
3831 continue;
57632f4a
NB
3832 st = __be16_to_cpu(ddf->phys->entries[pd].state);
3833 if ((st & (DDF_Online|DDF_Failed|DDF_Rebuilding))
7a7cc504
NB
3834 == DDF_Online)
3835 working++;
3836 }
3837 state = DDF_state_degraded;
3838 if (working == a->info.array.raid_disks)
3839 state = DDF_state_optimal;
3840 else switch(vc->prl) {
613b0d17
N
3841 case DDF_RAID0:
3842 case DDF_CONCAT:
3843 case DDF_JBOD:
7a7cc504 3844 state = DDF_state_failed;
613b0d17
N
3845 break;
3846 case DDF_RAID1:
3847 if (working == 0)
3848 state = DDF_state_failed;
3849 else if (working == 2 && state == DDF_state_degraded)
3850 state = DDF_state_part_optimal;
3851 break;
3852 case DDF_RAID4:
3853 case DDF_RAID5:
3854 if (working < a->info.array.raid_disks-1)
3855 state = DDF_state_failed;
3856 break;
3857 case DDF_RAID6:
3858 if (working < a->info.array.raid_disks-2)
3859 state = DDF_state_failed;
3860 else if (working == a->info.array.raid_disks-1)
3861 state = DDF_state_part_optimal;
3862 break;
3863 }
7a7cc504 3864
18a2f463
NB
3865 if (ddf->virt->entries[inst].state !=
3866 ((ddf->virt->entries[inst].state & ~DDF_state_mask)
3867 | state)) {
3868
3869 ddf->virt->entries[inst].state =
3870 (ddf->virt->entries[inst].state & ~DDF_state_mask)
3871 | state;
7d5a7ff3 3872 ddf_set_updates_pending(ddf);
18a2f463 3873 }
7a7cc504 3874
549e9569
NB
3875}
3876
2e735d19 3877static void ddf_sync_metadata(struct supertype *st)
549e9569 3878{
7a7cc504
NB
3879
3880 /*
3881 * Write all data to all devices.
3882 * Later, we might be able to track whether only local changes
3883 * have been made, or whether any global data has been changed,
3884 * but ddf is sufficiently weird that it probably always
3885 * changes global data ....
3886 */
18a2f463
NB
3887 struct ddf_super *ddf = st->sb;
3888 if (!ddf->updates_pending)
3889 return;
3890 ddf->updates_pending = 0;
1cc7f4fe 3891 __write_init_super_ddf(st);
2c514b71 3892 dprintf("ddf: sync_metadata\n");
549e9569
NB
3893}
3894
88c164f4
NB
3895static void ddf_process_update(struct supertype *st,
3896 struct metadata_update *update)
3897{
3898 /* Apply this update to the metadata.
3899 * The first 4 bytes are a DDF_*_MAGIC which guides
3900 * our actions.
3901 * Possible update are:
3902 * DDF_PHYS_RECORDS_MAGIC
4dd968cc
N
3903 * Add a new physical device or remove an old one.
3904 * Changes to this record only happen implicitly.
88c164f4
NB
3905 * used_pdes is the device number.
3906 * DDF_VIRT_RECORDS_MAGIC
3907 * Add a new VD. Possibly also change the 'access' bits.
3908 * populated_vdes is the entry number.
3909 * DDF_VD_CONF_MAGIC
3910 * New or updated VD. the VIRT_RECORD must already
3911 * exist. For an update, phys_refnum and lba_offset
3912 * (at least) are updated, and the VD_CONF must
3913 * be written to precisely those devices listed with
3914 * a phys_refnum.
3915 * DDF_SPARE_ASSIGN_MAGIC
3916 * replacement Spare Assignment Record... but for which device?
3917 *
3918 * So, e.g.:
3919 * - to create a new array, we send a VIRT_RECORD and
3920 * a VD_CONF. Then assemble and start the array.
3921 * - to activate a spare we send a VD_CONF to add the phys_refnum
3922 * and offset. This will also mark the spare as active with
3923 * a spare-assignment record.
3924 */
3925 struct ddf_super *ddf = st->sb;
3926 __u32 *magic = (__u32*)update->buf;
3927 struct phys_disk *pd;
3928 struct virtual_disk *vd;
3929 struct vd_config *vc;
3930 struct vcl *vcl;
3931 struct dl *dl;
f21e18ca
N
3932 unsigned int mppe;
3933 unsigned int ent;
c7079c84 3934 unsigned int pdnum, pd2;
88c164f4 3935
2c514b71 3936 dprintf("Process update %x\n", *magic);
7e1432fb 3937
88c164f4
NB
3938 switch (*magic) {
3939 case DDF_PHYS_RECORDS_MAGIC:
3940
3941 if (update->len != (sizeof(struct phys_disk) +
3942 sizeof(struct phys_disk_entry)))
3943 return;
3944 pd = (struct phys_disk*)update->buf;
3945
3946 ent = __be16_to_cpu(pd->used_pdes);
3947 if (ent >= __be16_to_cpu(ddf->phys->max_pdes))
3948 return;
4dd968cc
N
3949 if (pd->entries[0].state & __cpu_to_be16(DDF_Missing)) {
3950 struct dl **dlp;
3951 /* removing this disk. */
3952 ddf->phys->entries[ent].state |= __cpu_to_be16(DDF_Missing);
3953 for (dlp = &ddf->dlist; *dlp; dlp = &(*dlp)->next) {
3954 struct dl *dl = *dlp;
3955 if (dl->pdnum == (signed)ent) {
3956 close(dl->fd);
3957 dl->fd = -1;
3958 /* FIXME this doesn't free
3959 * dl->devname */
3960 update->space = dl;
3961 *dlp = dl->next;
3962 break;
3963 }
3964 }
7d5a7ff3 3965 ddf_set_updates_pending(ddf);
4dd968cc
N
3966 return;
3967 }
88c164f4
NB
3968 if (!all_ff(ddf->phys->entries[ent].guid))
3969 return;
3970 ddf->phys->entries[ent] = pd->entries[0];
3971 ddf->phys->used_pdes = __cpu_to_be16(1 +
613b0d17 3972 __be16_to_cpu(ddf->phys->used_pdes));
7d5a7ff3 3973 ddf_set_updates_pending(ddf);
2cc2983d
N
3974 if (ddf->add_list) {
3975 struct active_array *a;
3976 struct dl *al = ddf->add_list;
3977 ddf->add_list = al->next;
3978
3979 al->next = ddf->dlist;
3980 ddf->dlist = al;
3981
3982 /* As a device has been added, we should check
3983 * for any degraded devices that might make
3984 * use of this spare */
3985 for (a = st->arrays ; a; a=a->next)
3986 a->check_degraded = 1;
3987 }
88c164f4
NB
3988 break;
3989
3990 case DDF_VIRT_RECORDS_MAGIC:
3991
3992 if (update->len != (sizeof(struct virtual_disk) +
3993 sizeof(struct virtual_entry)))
3994 return;
3995 vd = (struct virtual_disk*)update->buf;
3996
3997 ent = __be16_to_cpu(vd->populated_vdes);
3998 if (ent >= __be16_to_cpu(ddf->virt->max_vdes))
3999 return;
4000 if (!all_ff(ddf->virt->entries[ent].guid))
4001 return;
4002 ddf->virt->entries[ent] = vd->entries[0];
4003 ddf->virt->populated_vdes = __cpu_to_be16(1 +
613b0d17 4004 __be16_to_cpu(ddf->virt->populated_vdes));
7d5a7ff3 4005 ddf_set_updates_pending(ddf);
88c164f4
NB
4006 break;
4007
4008 case DDF_VD_CONF_MAGIC:
2c514b71 4009 dprintf("len %d %d\n", update->len, ddf->conf_rec_len);
88c164f4
NB
4010
4011 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
f21e18ca 4012 if ((unsigned)update->len != ddf->conf_rec_len * 512)
88c164f4
NB
4013 return;
4014 vc = (struct vd_config*)update->buf;
4015 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
4016 if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0)
4017 break;
2c514b71 4018 dprintf("vcl = %p\n", vcl);
88c164f4
NB
4019 if (vcl) {
4020 /* An update, just copy the phys_refnum and lba_offset
4021 * fields
4022 */
4023 memcpy(vcl->conf.phys_refnum, vc->phys_refnum,
4024 mppe * (sizeof(__u32) + sizeof(__u64)));
4025 } else {
4026 /* A new VD_CONF */
e6b9548d
DW
4027 if (!update->space)
4028 return;
88c164f4
NB
4029 vcl = update->space;
4030 update->space = NULL;
4031 vcl->next = ddf->conflist;
edd8d13c 4032 memcpy(&vcl->conf, vc, update->len);
88c164f4
NB
4033 vcl->lba_offset = (__u64*)
4034 &vcl->conf.phys_refnum[mppe];
1502a43a
N
4035 for (ent = 0;
4036 ent < __be16_to_cpu(ddf->virt->populated_vdes);
4037 ent++)
4038 if (memcmp(vc->guid, ddf->virt->entries[ent].guid,
4039 DDF_GUID_LEN) == 0) {
4040 vcl->vcnum = ent;
4041 break;
4042 }
88c164f4
NB
4043 ddf->conflist = vcl;
4044 }
c7079c84
N
4045 /* Set DDF_Transition on all Failed devices - to help
4046 * us detect those that are no longer in use
4047 */
4048 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4049 if (ddf->phys->entries[pdnum].state
4050 & __be16_to_cpu(DDF_Failed))
4051 ddf->phys->entries[pdnum].state
4052 |= __be16_to_cpu(DDF_Transition);
88c164f4
NB
4053 /* Now make sure vlist is correct for each dl. */
4054 for (dl = ddf->dlist; dl; dl = dl->next) {
f21e18ca
N
4055 unsigned int dn;
4056 unsigned int vn = 0;
8401644c 4057 int in_degraded = 0;
88c164f4
NB
4058 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
4059 for (dn=0; dn < ddf->mppe ; dn++)
4060 if (vcl->conf.phys_refnum[dn] ==
4061 dl->disk.refnum) {
8401644c 4062 int vstate;
2c514b71
NB
4063 dprintf("dev %d has %p at %d\n",
4064 dl->pdnum, vcl, vn);
c7079c84
N
4065 /* Clear the Transition flag */
4066 if (ddf->phys->entries[dl->pdnum].state
4067 & __be16_to_cpu(DDF_Failed))
4068 ddf->phys->entries[dl->pdnum].state &=
4069 ~__be16_to_cpu(DDF_Transition);
4070
88c164f4 4071 dl->vlist[vn++] = vcl;
8401644c
N
4072 vstate = ddf->virt->entries[vcl->vcnum].state
4073 & DDF_state_mask;
4074 if (vstate == DDF_state_degraded ||
4075 vstate == DDF_state_part_optimal)
4076 in_degraded = 1;
88c164f4
NB
4077 break;
4078 }
4079 while (vn < ddf->max_part)
4080 dl->vlist[vn++] = NULL;
7e1432fb
NB
4081 if (dl->vlist[0]) {
4082 ddf->phys->entries[dl->pdnum].type &=
4083 ~__cpu_to_be16(DDF_Global_Spare);
8401644c
N
4084 if (!(ddf->phys->entries[dl->pdnum].type &
4085 __cpu_to_be16(DDF_Active_in_VD))) {
613b0d17
N
4086 ddf->phys->entries[dl->pdnum].type |=
4087 __cpu_to_be16(DDF_Active_in_VD);
4088 if (in_degraded)
4089 ddf->phys->entries[dl->pdnum].state |=
4090 __cpu_to_be16(DDF_Rebuilding);
4091 }
7e1432fb
NB
4092 }
4093 if (dl->spare) {
4094 ddf->phys->entries[dl->pdnum].type &=
4095 ~__cpu_to_be16(DDF_Global_Spare);
4096 ddf->phys->entries[dl->pdnum].type |=
4097 __cpu_to_be16(DDF_Spare);
4098 }
4099 if (!dl->vlist[0] && !dl->spare) {
4100 ddf->phys->entries[dl->pdnum].type |=
4101 __cpu_to_be16(DDF_Global_Spare);
4102 ddf->phys->entries[dl->pdnum].type &=
4103 ~__cpu_to_be16(DDF_Spare |
4104 DDF_Active_in_VD);
4105 }
88c164f4 4106 }
c7079c84
N
4107
4108 /* Now remove any 'Failed' devices that are not part
4109 * of any VD. They will have the Transition flag set.
4110 * Once done, we need to update all dl->pdnum numbers.
4111 */
4112 pd2 = 0;
4113 for (pdnum = 0; pdnum < __be16_to_cpu(ddf->phys->used_pdes); pdnum++)
4114 if ((ddf->phys->entries[pdnum].state
4115 & __be16_to_cpu(DDF_Failed))
4116 && (ddf->phys->entries[pdnum].state
4117 & __be16_to_cpu(DDF_Transition)))
4118 /* skip this one */;
4119 else if (pdnum == pd2)
4120 pd2++;
4121 else {
4122 ddf->phys->entries[pd2] = ddf->phys->entries[pdnum];
4123 for (dl = ddf->dlist; dl; dl = dl->next)
4124 if (dl->pdnum == (int)pdnum)
4125 dl->pdnum = pd2;
4126 pd2++;
4127 }
4128 ddf->phys->used_pdes = __cpu_to_be16(pd2);
4129 while (pd2 < pdnum) {
4130 memset(ddf->phys->entries[pd2].guid, 0xff, DDF_GUID_LEN);
4131 pd2++;
4132 }
4133
7d5a7ff3 4134 ddf_set_updates_pending(ddf);
88c164f4
NB
4135 break;
4136 case DDF_SPARE_ASSIGN_MAGIC:
4137 default: break;
4138 }
4139}
4140
edd8d13c
NB
4141static void ddf_prepare_update(struct supertype *st,
4142 struct metadata_update *update)
4143{
4144 /* This update arrived at managemon.
4145 * We are about to pass it to monitor.
4146 * If a malloc is needed, do it here.
4147 */
4148 struct ddf_super *ddf = st->sb;
4149 __u32 *magic = (__u32*)update->buf;
4150 if (*magic == DDF_VD_CONF_MAGIC)
e6b9548d 4151 if (posix_memalign(&update->space, 512,
613b0d17
N
4152 offsetof(struct vcl, conf)
4153 + ddf->conf_rec_len * 512) != 0)
e6b9548d 4154 update->space = NULL;
edd8d13c
NB
4155}
4156
7e1432fb
NB
4157/*
4158 * Check if the array 'a' is degraded but not failed.
4159 * If it is, find as many spares as are available and needed and
4160 * arrange for their inclusion.
4161 * We only choose devices which are not already in the array,
4162 * and prefer those with a spare-assignment to this array.
4163 * otherwise we choose global spares - assuming always that
4164 * there is enough room.
4165 * For each spare that we assign, we return an 'mdinfo' which
4166 * describes the position for the device in the array.
4167 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4168 * the new phys_refnum and lba_offset values.
4169 *
4170 * Only worry about BVDs at the moment.
4171 */
4172static struct mdinfo *ddf_activate_spare(struct active_array *a,
4173 struct metadata_update **updates)
4174{
4175 int working = 0;
4176 struct mdinfo *d;
4177 struct ddf_super *ddf = a->container->sb;
4178 int global_ok = 0;
4179 struct mdinfo *rv = NULL;
4180 struct mdinfo *di;
4181 struct metadata_update *mu;
4182 struct dl *dl;
4183 int i;
4184 struct vd_config *vc;
4185 __u64 *lba;
4186
7e1432fb
NB
4187 for (d = a->info.devs ; d ; d = d->next) {
4188 if ((d->curr_state & DS_FAULTY) &&
613b0d17 4189 d->state_fd >= 0)
7e1432fb
NB
4190 /* wait for Removal to happen */
4191 return NULL;
4192 if (d->state_fd >= 0)
4193 working ++;
4194 }
4195
2c514b71
NB
4196 dprintf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks,
4197 a->info.array.level);
7e1432fb
NB
4198 if (working == a->info.array.raid_disks)
4199 return NULL; /* array not degraded */
4200 switch (a->info.array.level) {
4201 case 1:
4202 if (working == 0)
4203 return NULL; /* failed */
4204 break;
4205 case 4:
4206 case 5:
4207 if (working < a->info.array.raid_disks - 1)
4208 return NULL; /* failed */
4209 break;
4210 case 6:
4211 if (working < a->info.array.raid_disks - 2)
4212 return NULL; /* failed */
4213 break;
4214 default: /* concat or stripe */
4215 return NULL; /* failed */
4216 }
4217
4218 /* For each slot, if it is not working, find a spare */
4219 dl = ddf->dlist;
4220 for (i = 0; i < a->info.array.raid_disks; i++) {
4221 for (d = a->info.devs ; d ; d = d->next)
4222 if (d->disk.raid_disk == i)
4223 break;
2c514b71 4224 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
7e1432fb
NB
4225 if (d && (d->state_fd >= 0))
4226 continue;
4227
4228 /* OK, this device needs recovery. Find a spare */
4229 again:
4230 for ( ; dl ; dl = dl->next) {
4231 unsigned long long esize;
4232 unsigned long long pos;
4233 struct mdinfo *d2;
4234 int is_global = 0;
4235 int is_dedicated = 0;
4236 struct extent *ex;
f21e18ca 4237 unsigned int j;
7e1432fb
NB
4238 /* If in this array, skip */
4239 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
7590d562
N
4240 if (d2->state_fd >= 0 &&
4241 d2->disk.major == dl->major &&
7e1432fb 4242 d2->disk.minor == dl->minor) {
2c514b71 4243 dprintf("%x:%x already in array\n", dl->major, dl->minor);
7e1432fb
NB
4244 break;
4245 }
4246 if (d2)
4247 continue;
4248 if (ddf->phys->entries[dl->pdnum].type &
4249 __cpu_to_be16(DDF_Spare)) {
4250 /* Check spare assign record */
4251 if (dl->spare) {
4252 if (dl->spare->type & DDF_spare_dedicated) {
4253 /* check spare_ents for guid */
4254 for (j = 0 ;
4255 j < __be16_to_cpu(dl->spare->populated);
4256 j++) {
4257 if (memcmp(dl->spare->spare_ents[j].guid,
4258 ddf->virt->entries[a->info.container_member].guid,
4259 DDF_GUID_LEN) == 0)
4260 is_dedicated = 1;
4261 }
4262 } else
4263 is_global = 1;
4264 }
4265 } else if (ddf->phys->entries[dl->pdnum].type &
4266 __cpu_to_be16(DDF_Global_Spare)) {
4267 is_global = 1;
e0e7aeaa
N
4268 } else if (!(ddf->phys->entries[dl->pdnum].state &
4269 __cpu_to_be16(DDF_Failed))) {
4270 /* we can possibly use some of this */
4271 is_global = 1;
7e1432fb
NB
4272 }
4273 if ( ! (is_dedicated ||
4274 (is_global && global_ok))) {
2c514b71 4275 dprintf("%x:%x not suitable: %d %d\n", dl->major, dl->minor,
613b0d17 4276 is_dedicated, is_global);
7e1432fb
NB
4277 continue;
4278 }
4279
4280 /* We are allowed to use this device - is there space?
4281 * We need a->info.component_size sectors */
4282 ex = get_extents(ddf, dl);
4283 if (!ex) {
2c514b71 4284 dprintf("cannot get extents\n");
7e1432fb
NB
4285 continue;
4286 }
4287 j = 0; pos = 0;
4288 esize = 0;
4289
4290 do {
4291 esize = ex[j].start - pos;
4292 if (esize >= a->info.component_size)
4293 break;
e5cc7d46
N
4294 pos = ex[j].start + ex[j].size;
4295 j++;
4296 } while (ex[j-1].size);
7e1432fb
NB
4297
4298 free(ex);
4299 if (esize < a->info.component_size) {
e5cc7d46
N
4300 dprintf("%x:%x has no room: %llu %llu\n",
4301 dl->major, dl->minor,
2c514b71 4302 esize, a->info.component_size);
7e1432fb
NB
4303 /* No room */
4304 continue;
4305 }
4306
4307 /* Cool, we have a device with some space at pos */
503975b9 4308 di = xcalloc(1, sizeof(*di));
7e1432fb
NB
4309 di->disk.number = i;
4310 di->disk.raid_disk = i;
4311 di->disk.major = dl->major;
4312 di->disk.minor = dl->minor;
4313 di->disk.state = 0;
d23534e4 4314 di->recovery_start = 0;
7e1432fb
NB
4315 di->data_offset = pos;
4316 di->component_size = a->info.component_size;
4317 di->container_member = dl->pdnum;
4318 di->next = rv;
4319 rv = di;
2c514b71
NB
4320 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
4321 i, pos);
7e1432fb
NB
4322
4323 break;
4324 }
4325 if (!dl && ! global_ok) {
4326 /* not enough dedicated spares, try global */
4327 global_ok = 1;
4328 dl = ddf->dlist;
4329 goto again;
4330 }
4331 }
4332
4333 if (!rv)
4334 /* No spares found */
4335 return rv;
4336 /* Now 'rv' has a list of devices to return.
4337 * Create a metadata_update record to update the
4338 * phys_refnum and lba_offset values
4339 */
503975b9
N
4340 mu = xmalloc(sizeof(*mu));
4341 if (posix_memalign(&mu->space, 512, sizeof(struct vcl)) != 0) {
79244939
DW
4342 free(mu);
4343 mu = NULL;
4344 }
503975b9 4345 mu->buf = xmalloc(ddf->conf_rec_len * 512);
7590d562
N
4346 mu->len = ddf->conf_rec_len * 512;
4347 mu->space = NULL;
f50ae22e 4348 mu->space_list = NULL;
7e1432fb
NB
4349 mu->next = *updates;
4350 vc = find_vdcr(ddf, a->info.container_member);
4351 memcpy(mu->buf, vc, ddf->conf_rec_len * 512);
4352
4353 vc = (struct vd_config*)mu->buf;
4354 lba = (__u64*)&vc->phys_refnum[ddf->mppe];
4355 for (di = rv ; di ; di = di->next) {
4356 vc->phys_refnum[di->disk.raid_disk] =
4357 ddf->phys->entries[dl->pdnum].refnum;
4358 lba[di->disk.raid_disk] = di->data_offset;
4359 }
4360 *updates = mu;
4361 return rv;
4362}
0e600426 4363#endif /* MDASSEMBLE */
7e1432fb 4364
b640a252
N
4365static int ddf_level_to_layout(int level)
4366{
4367 switch(level) {
4368 case 0:
4369 case 1:
4370 return 0;
4371 case 5:
4372 return ALGORITHM_LEFT_SYMMETRIC;
4373 case 6:
4374 return ALGORITHM_ROTATING_N_CONTINUE;
4375 case 10:
4376 return 0x102;
4377 default:
4378 return UnSet;
4379 }
4380}
4381
30f58b22
DW
4382static void default_geometry_ddf(struct supertype *st, int *level, int *layout, int *chunk)
4383{
4384 if (level && *level == UnSet)
4385 *level = LEVEL_CONTAINER;
4386
4387 if (level && layout && *layout == UnSet)
4388 *layout = ddf_level_to_layout(*level);
4389}
4390
a322f70c
DW
4391struct superswitch super_ddf = {
4392#ifndef MDASSEMBLE
4393 .examine_super = examine_super_ddf,
4394 .brief_examine_super = brief_examine_super_ddf,
4737ae25 4395 .brief_examine_subarrays = brief_examine_subarrays_ddf,
bceedeec 4396 .export_examine_super = export_examine_super_ddf,
a322f70c
DW
4397 .detail_super = detail_super_ddf,
4398 .brief_detail_super = brief_detail_super_ddf,
4399 .validate_geometry = validate_geometry_ddf,
78e44928 4400 .write_init_super = write_init_super_ddf,
0e600426 4401 .add_to_super = add_to_super_ddf,
4dd968cc 4402 .remove_from_super = remove_from_super_ddf,
2b959fbf 4403 .load_container = load_container_ddf,
74db60b0 4404 .copy_metadata = copy_metadata_ddf,
a322f70c
DW
4405#endif
4406 .match_home = match_home_ddf,
4407 .uuid_from_super= uuid_from_super_ddf,
4408 .getinfo_super = getinfo_super_ddf,
4409 .update_super = update_super_ddf,
4410
4411 .avail_size = avail_size_ddf,
4412
a19c88b8
NB
4413 .compare_super = compare_super_ddf,
4414
a322f70c 4415 .load_super = load_super_ddf,
ba7eb04f 4416 .init_super = init_super_ddf,
955e9ea1 4417 .store_super = store_super_ddf,
a322f70c
DW
4418 .free_super = free_super_ddf,
4419 .match_metadata_desc = match_metadata_desc_ddf,
78e44928 4420 .container_content = container_content_ddf,
30f58b22 4421 .default_geometry = default_geometry_ddf,
a322f70c 4422
a322f70c 4423 .external = 1,
549e9569 4424
0e600426 4425#ifndef MDASSEMBLE
549e9569
NB
4426/* for mdmon */
4427 .open_new = ddf_open_new,
ed9d66aa 4428 .set_array_state= ddf_set_array_state,
549e9569
NB
4429 .set_disk = ddf_set_disk,
4430 .sync_metadata = ddf_sync_metadata,
88c164f4 4431 .process_update = ddf_process_update,
edd8d13c 4432 .prepare_update = ddf_prepare_update,
7e1432fb 4433 .activate_spare = ddf_activate_spare,
0e600426 4434#endif
4cce4069 4435 .name = "ddf",
a322f70c 4436};