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