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