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