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