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