]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-ddf.c
DDF: Update array state and disk state properly when adding to a BVD
[thirdparty/mdadm.git] / super-ddf.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2006-2007 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 static inline int ROUND_UP(int a, int base)
35 {
36 return ((a+base-1)/base)*base;
37 }
38
39 /* a non-official T10 name for creation GUIDs */
40 static char T10[] = "Linux-MD";
41
42 /* DDF timestamps are 1980 based, so we need to add
43 * second-in-decade-of-seventies to convert to linux timestamps.
44 * 10 years with 2 leap years.
45 */
46 #define DECADE (3600*24*(365*10+2))
47 unsigned long crc32(
48 unsigned long crc,
49 const unsigned char *buf,
50 unsigned len);
51
52 /* The DDF metadata handling.
53 * DDF metadata lives at the end of the device.
54 * The last 512 byte block provides an 'anchor' which is used to locate
55 * the rest of the metadata which usually lives immediately behind the anchor.
56 *
57 * Note:
58 * - all multibyte numeric fields are bigendian.
59 * - all strings are space padded.
60 *
61 */
62
63 /* Primary Raid Level (PRL) */
64 #define DDF_RAID0 0x00
65 #define DDF_RAID1 0x01
66 #define DDF_RAID3 0x03
67 #define DDF_RAID4 0x04
68 #define DDF_RAID5 0x05
69 #define DDF_RAID1E 0x11
70 #define DDF_JBOD 0x0f
71 #define DDF_CONCAT 0x1f
72 #define DDF_RAID5E 0x15
73 #define DDF_RAID5EE 0x25
74 #define DDF_RAID6 0x16 /* Vendor unique layout */
75
76 /* Raid Level Qualifier (RLQ) */
77 #define DDF_RAID0_SIMPLE 0x00
78 #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
79 #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
80 #define DDF_RAID3_0 0x00 /* parity in first extent */
81 #define DDF_RAID3_N 0x01 /* parity in last extent */
82 #define DDF_RAID4_0 0x00 /* parity in first extent */
83 #define DDF_RAID4_N 0x01 /* parity in last extent */
84 /* these apply to raid5e and raid5ee as well */
85 #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
86 #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
87 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
88
89 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
90 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
91
92 /* Secondary RAID Level (SRL) */
93 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
94 #define DDF_2MIRRORED 0x01
95 #define DDF_2CONCAT 0x02
96 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
97
98 /* Magic numbers */
99 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
100 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
101 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
102 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
103 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
104 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
105 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
106 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
107 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
108 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
109
110 #define DDF_GUID_LEN 24
111 #define DDF_REVISION "01.00.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.00.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, *active;
397 struct ddf_controller_data controller;
398 struct phys_disk *phys;
399 struct virtual_disk *virt;
400 int pdsize, vdsize;
401 int max_part, mppe, conf_rec_len;
402 struct vcl {
403 struct vcl *next;
404 __u64 *lba_offset; /* location in 'conf' of
405 * the lba table */
406 struct vd_config conf;
407 } *conflist, *newconf;
408 int conf_num; /* Index into 'virt' of entry matching 'newconf' */
409 struct dl {
410 struct dl *next;
411 struct disk_data disk;
412 int major, minor;
413 char *devname;
414 int fd;
415 int pdnum; /* index in ->phys */
416 struct spare_assign *spare;
417 struct vcl *vlist[0]; /* max_part in size */
418 } *dlist;
419 };
420
421 #ifndef offsetof
422 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
423 #endif
424
425 extern struct superswitch super_ddf_container, super_ddf_bvd, super_ddf;
426
427 static int calc_crc(void *buf, int len)
428 {
429 /* crcs are always at the same place as in the ddf_header */
430 struct ddf_header *ddf = buf;
431 __u32 oldcrc = ddf->crc;
432 __u32 newcrc;
433 ddf->crc = 0xffffffff;
434
435 newcrc = crc32(0, buf, len);
436 ddf->crc = oldcrc;
437 return newcrc;
438 }
439
440 static int load_ddf_header(int fd, unsigned long long lba,
441 unsigned long long size,
442 int type,
443 struct ddf_header *hdr, struct ddf_header *anchor)
444 {
445 /* read a ddf header (primary or secondary) from fd/lba
446 * and check that it is consistent with anchor
447 * Need to check:
448 * magic, crc, guid, rev, and LBA's header_type, and
449 * everything after header_type must be the same
450 */
451 if (lba >= size-1)
452 return 0;
453
454 if (lseek64(fd, lba<<9, 0) < 0)
455 return 0;
456
457 if (read(fd, hdr, 512) != 512)
458 return 0;
459
460 if (hdr->magic != DDF_HEADER_MAGIC)
461 return 0;
462 if (calc_crc(hdr, 512) != hdr->crc)
463 return 0;
464 if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 ||
465 memcmp(anchor->revision, hdr->revision, 8) != 0 ||
466 anchor->primary_lba != hdr->primary_lba ||
467 anchor->secondary_lba != hdr->secondary_lba ||
468 hdr->type != type ||
469 memcmp(anchor->pad2, hdr->pad2, 512 -
470 offsetof(struct ddf_header, pad2)) != 0)
471 return 0;
472
473 /* Looks good enough to me... */
474 return 1;
475 }
476
477 static void *load_section(int fd, struct ddf_super *super, void *buf,
478 __u32 offset_be, __u32 len_be, int check)
479 {
480 unsigned long long offset = __be32_to_cpu(offset_be);
481 unsigned long long len = __be32_to_cpu(len_be);
482 int dofree = (buf == NULL);
483
484 if (check)
485 if (len != 2 && len != 8 && len != 32
486 && len != 128 && len != 512)
487 return NULL;
488
489 if (len > 1024)
490 return NULL;
491 if (buf) {
492 /* All pre-allocated sections are a single block */
493 if (len != 1)
494 return NULL;
495 } else
496 buf = malloc(len<<9);
497 if (!buf)
498 return NULL;
499
500 if (super->active->type == 1)
501 offset += __be64_to_cpu(super->active->primary_lba);
502 else
503 offset += __be64_to_cpu(super->active->secondary_lba);
504
505 if (lseek64(fd, offset<<9, 0) != (offset<<9)) {
506 if (dofree)
507 free(buf);
508 return NULL;
509 }
510 if (read(fd, buf, len<<9) != (len<<9)) {
511 if (dofree)
512 free(buf);
513 return NULL;
514 }
515 return buf;
516 }
517
518 static int load_ddf_headers(int fd, struct ddf_super *super, char *devname)
519 {
520 unsigned long long dsize;
521
522 get_dev_size(fd, NULL, &dsize);
523
524 if (lseek64(fd, dsize-512, 0) < 0) {
525 if (devname)
526 fprintf(stderr,
527 Name": Cannot seek to anchor block on %s: %s\n",
528 devname, strerror(errno));
529 return 1;
530 }
531 if (read(fd, &super->anchor, 512) != 512) {
532 if (devname)
533 fprintf(stderr,
534 Name ": Cannot read anchor block on %s: %s\n",
535 devname, strerror(errno));
536 return 1;
537 }
538 if (super->anchor.magic != DDF_HEADER_MAGIC) {
539 if (devname)
540 fprintf(stderr, Name ": no DDF anchor found on %s\n",
541 devname);
542 return 2;
543 }
544 if (calc_crc(&super->anchor, 512) != super->anchor.crc) {
545 if (devname)
546 fprintf(stderr, Name ": bad CRC on anchor on %s\n",
547 devname);
548 return 2;
549 }
550 if (memcmp(super->anchor.revision, DDF_REVISION, 8) != 0) {
551 if (devname)
552 fprintf(stderr, Name ": can only support super revision"
553 " %.8s, not %.8s on %s\n",
554 DDF_REVISION, super->anchor.revision, devname);
555 return 2;
556 }
557 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.primary_lba),
558 dsize >> 9, 1,
559 &super->primary, &super->anchor) == 0) {
560 if (devname)
561 fprintf(stderr,
562 Name ": Failed to load primary DDF header "
563 "on %s\n", devname);
564 return 2;
565 }
566 super->active = &super->primary;
567 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.secondary_lba),
568 dsize >> 9, 2,
569 &super->secondary, &super->anchor)) {
570 if ((__be32_to_cpu(super->primary.seq)
571 < __be32_to_cpu(super->secondary.seq) &&
572 !super->secondary.openflag)
573 || (__be32_to_cpu(super->primary.seq)
574 == __be32_to_cpu(super->secondary.seq) &&
575 super->primary.openflag && !super->secondary.openflag)
576 )
577 super->active = &super->secondary;
578 }
579 return 0;
580 }
581
582 static int load_ddf_global(int fd, struct ddf_super *super, char *devname)
583 {
584 void *ok;
585 ok = load_section(fd, super, &super->controller,
586 super->active->controller_section_offset,
587 super->active->controller_section_length,
588 0);
589 super->phys = load_section(fd, super, NULL,
590 super->active->phys_section_offset,
591 super->active->phys_section_length,
592 1);
593 super->pdsize = __be32_to_cpu(super->active->phys_section_length) * 512;
594
595 super->virt = load_section(fd, super, NULL,
596 super->active->virt_section_offset,
597 super->active->virt_section_length,
598 1);
599 super->vdsize = __be32_to_cpu(super->active->virt_section_length) * 512;
600 if (!ok ||
601 !super->phys ||
602 !super->virt) {
603 free(super->phys);
604 free(super->virt);
605 super->phys = NULL;
606 super->virt = NULL;
607 return 2;
608 }
609 super->conflist = NULL;
610 super->dlist = NULL;
611
612 super->max_part = __be16_to_cpu(super->active->max_partitions);
613 super->mppe = __be16_to_cpu(super->active->max_primary_element_entries);
614 super->conf_rec_len = __be16_to_cpu(super->active->config_record_len);
615 return 0;
616 }
617
618 static int load_ddf_local(int fd, struct ddf_super *super,
619 char *devname, int keep)
620 {
621 struct dl *dl;
622 struct stat stb;
623 char *conf;
624 int i;
625 int vnum;
626
627 /* First the local disk info */
628 dl = malloc(sizeof(*dl) +
629 (super->max_part) * sizeof(dl->vlist[0]));
630
631 load_section(fd, super, &dl->disk,
632 super->active->data_section_offset,
633 super->active->data_section_length,
634 0);
635 dl->devname = devname ? strdup(devname) : NULL;
636
637 fstat(fd, &stb);
638 dl->major = major(stb.st_rdev);
639 dl->minor = minor(stb.st_rdev);
640 dl->next = super->dlist;
641 dl->fd = keep ? fd : -1;
642 dl->spare = NULL;
643 for (i=0 ; i < super->max_part ; i++)
644 dl->vlist[i] = NULL;
645 super->dlist = dl;
646 dl->pdnum = 0;
647 for (i=0; i < __be16_to_cpu(super->active->max_pd_entries); i++)
648 if (memcmp(super->phys->entries[i].guid,
649 dl->disk.guid, DDF_GUID_LEN) == 0)
650 dl->pdnum = i;
651
652
653 /* Now the config list. */
654 /* 'conf' is an array of config entries, some of which are
655 * probably invalid. Those which are good need to be copied into
656 * the conflist
657 */
658
659 conf = load_section(fd, super, NULL,
660 super->active->config_section_offset,
661 super->active->config_section_length,
662 0);
663
664 vnum = 0;
665 for (i = 0;
666 i < __be32_to_cpu(super->active->config_section_length);
667 i += super->conf_rec_len) {
668 struct vd_config *vd =
669 (struct vd_config *)((char*)conf + i*512);
670 struct vcl *vcl;
671
672 if (vd->magic == DDF_SPARE_ASSIGN_MAGIC) {
673 if (dl->spare)
674 continue;
675 dl->spare = malloc(super->conf_rec_len*512);
676 memcpy(dl->spare, vd, super->conf_rec_len*512);
677 continue;
678 }
679 if (vd->magic != DDF_VD_CONF_MAGIC)
680 continue;
681 for (vcl = super->conflist; vcl; vcl = vcl->next) {
682 if (memcmp(vcl->conf.guid,
683 vd->guid, DDF_GUID_LEN) == 0)
684 break;
685 }
686
687 if (vcl) {
688 dl->vlist[vnum++] = vcl;
689 if (__be32_to_cpu(vd->seqnum) <=
690 __be32_to_cpu(vcl->conf.seqnum))
691 continue;
692 } else {
693 vcl = malloc(super->conf_rec_len*512 +
694 offsetof(struct vcl, conf));
695 vcl->next = super->conflist;
696 super->conflist = vcl;
697 dl->vlist[vnum++] = vcl;
698 }
699 memcpy(&vcl->conf, vd, super->conf_rec_len*512);
700 vcl->lba_offset = (__u64*)
701 &vcl->conf.phys_refnum[super->mppe];
702 }
703 free(conf);
704
705 return 0;
706 }
707
708 #ifndef MDASSEMBLE
709 static int load_super_ddf_all(struct supertype *st, int fd,
710 void **sbp, char *devname, int keep_fd);
711 #endif
712 static int load_super_ddf(struct supertype *st, int fd,
713 char *devname)
714 {
715 unsigned long long dsize;
716 struct ddf_super *super;
717 int rv;
718
719 #ifndef MDASSEMBLE
720 if (load_super_ddf_all(st, fd, &st->sb, devname, 1) == 0)
721 return 0;
722 #endif
723
724 if (get_dev_size(fd, devname, &dsize) == 0)
725 return 1;
726
727 /* 32M is a lower bound */
728 if (dsize <= 32*1024*1024) {
729 if (devname) {
730 fprintf(stderr,
731 Name ": %s is too small for ddf: "
732 "size is %llu sectors.\n",
733 devname, dsize>>9);
734 return 1;
735 }
736 }
737 if (dsize & 511) {
738 if (devname) {
739 fprintf(stderr,
740 Name ": %s is an odd size for ddf: "
741 "size is %llu bytes.\n",
742 devname, dsize);
743 return 1;
744 }
745 }
746
747 super = malloc(sizeof(*super));
748 if (!super) {
749 fprintf(stderr, Name ": malloc of %zu failed.\n",
750 sizeof(*super));
751 return 1;
752 }
753 memset(super, 0, sizeof(*super));
754
755 rv = load_ddf_headers(fd, super, devname);
756 if (rv) {
757 free(super);
758 return rv;
759 }
760
761 /* Have valid headers and have chosen the best. Let's read in the rest*/
762
763 rv = load_ddf_global(fd, super, devname);
764
765 if (rv) {
766 if (devname)
767 fprintf(stderr,
768 Name ": Failed to load all information "
769 "sections on %s\n", devname);
770 free(super);
771 return rv;
772 }
773
774 load_ddf_local(fd, super, devname, 0);
775
776 /* Should possibly check the sections .... */
777
778 st->sb = super;
779 if (st->ss == NULL) {
780 st->ss = &super_ddf;
781 st->minor_version = 0;
782 st->max_devs = 512;
783 }
784 return 0;
785
786 }
787
788 static void free_super_ddf(struct supertype *st)
789 {
790 struct ddf_super *ddf = st->sb;
791 if (ddf == NULL)
792 return;
793 free(ddf->phys);
794 free(ddf->virt);
795 while (ddf->conflist) {
796 struct vcl *v = ddf->conflist;
797 ddf->conflist = v->next;
798 free(v);
799 }
800 while (ddf->dlist) {
801 struct dl *d = ddf->dlist;
802 ddf->dlist = d->next;
803 if (d->fd >= 0)
804 close(d->fd);
805 if (d->spare)
806 free(d->spare);
807 free(d);
808 }
809 free(ddf);
810 st->sb = NULL;
811 }
812
813 static struct supertype *match_metadata_desc_ddf(char *arg)
814 {
815 /* 'ddf' only support containers */
816 struct supertype *st;
817 if (strcmp(arg, "ddf") != 0 &&
818 strcmp(arg, "default") != 0
819 )
820 return NULL;
821
822 st = malloc(sizeof(*st));
823 st->ss = &super_ddf;
824 st->max_devs = 512;
825 st->minor_version = 0;
826 st->sb = NULL;
827 return st;
828 }
829
830 static struct supertype *match_metadata_desc_ddf_bvd(char *arg)
831 {
832 struct supertype *st;
833 if (strcmp(arg, "ddf/bvd") != 0 &&
834 strcmp(arg, "bvd") != 0 &&
835 strcmp(arg, "default") != 0
836 )
837 return NULL;
838
839 st = malloc(sizeof(*st));
840 st->ss = &super_ddf_bvd;
841 st->max_devs = 512;
842 st->minor_version = 0;
843 st->sb = NULL;
844 return st;
845 }
846 static struct supertype *match_metadata_desc_ddf_svd(char *arg)
847 {
848 struct supertype *st;
849 if (strcmp(arg, "ddf/svd") != 0 &&
850 strcmp(arg, "svd") != 0 &&
851 strcmp(arg, "default") != 0
852 )
853 return NULL;
854
855 st = malloc(sizeof(*st));
856 st->ss = &super_ddf_svd;
857 st->max_devs = 512;
858 st->minor_version = 0;
859 st->sb = NULL;
860 return st;
861 }
862
863 #ifndef MDASSEMBLE
864
865 static mapping_t ddf_state[] = {
866 { "Optimal", 0},
867 { "Degraded", 1},
868 { "Deleted", 2},
869 { "Missing", 3},
870 { "Failed", 4},
871 { "Partially Optimal", 5},
872 { "-reserved-", 6},
873 { "-reserved-", 7},
874 { NULL, 0}
875 };
876
877 static mapping_t ddf_init_state[] = {
878 { "Not Initialised", 0},
879 { "QuickInit in Progress", 1},
880 { "Fully Initialised", 2},
881 { "*UNKNOWN*", 3},
882 { NULL, 0}
883 };
884 static mapping_t ddf_access[] = {
885 { "Read/Write", 0},
886 { "Reserved", 1},
887 { "Read Only", 2},
888 { "Blocked (no access)", 3},
889 { NULL ,0}
890 };
891
892 static mapping_t ddf_level[] = {
893 { "RAID0", DDF_RAID0},
894 { "RAID1", DDF_RAID1},
895 { "RAID3", DDF_RAID3},
896 { "RAID4", DDF_RAID4},
897 { "RAID5", DDF_RAID5},
898 { "RAID1E",DDF_RAID1E},
899 { "JBOD", DDF_JBOD},
900 { "CONCAT",DDF_CONCAT},
901 { "RAID5E",DDF_RAID5E},
902 { "RAID5EE",DDF_RAID5EE},
903 { "RAID6", DDF_RAID6},
904 { NULL, 0}
905 };
906 static mapping_t ddf_sec_level[] = {
907 { "Striped", DDF_2STRIPED},
908 { "Mirrored", DDF_2MIRRORED},
909 { "Concat", DDF_2CONCAT},
910 { "Spanned", DDF_2SPANNED},
911 { NULL, 0}
912 };
913 #endif
914
915 struct num_mapping {
916 int num1, num2;
917 };
918 static struct num_mapping ddf_level_num[] = {
919 { DDF_RAID0, 0 },
920 { DDF_RAID1, 1 },
921 { DDF_RAID3, LEVEL_UNSUPPORTED },
922 { DDF_RAID4, 4 },
923 { DDF_RAID5, 5 },
924 { DDF_RAID1E, LEVEL_UNSUPPORTED },
925 { DDF_JBOD, LEVEL_UNSUPPORTED },
926 { DDF_CONCAT, LEVEL_LINEAR },
927 { DDF_RAID5E, LEVEL_UNSUPPORTED },
928 { DDF_RAID5EE, LEVEL_UNSUPPORTED },
929 { DDF_RAID6, 6},
930 { MAXINT, MAXINT }
931 };
932
933 static int map_num1(struct num_mapping *map, int num)
934 {
935 int i;
936 for (i=0 ; map[i].num1 != MAXINT; i++)
937 if (map[i].num1 == num)
938 break;
939 return map[i].num2;
940 }
941
942 #ifndef MDASSEMBLE
943 static void print_guid(char *guid, int tstamp)
944 {
945 /* A GUIDs are part (or all) ASCII and part binary.
946 * They tend to be space padded.
947 * We ignore trailing spaces and print numbers
948 * <0x20 and >=0x7f as \xXX
949 * Some GUIDs have a time stamp in bytes 16-19.
950 * We print that if appropriate
951 */
952 int l = DDF_GUID_LEN;
953 int i;
954 while (l && guid[l-1] == ' ')
955 l--;
956 for (i=0 ; i<l ; i++) {
957 if (guid[i] >= 0x20 && guid[i] < 0x7f)
958 fputc(guid[i], stdout);
959 else
960 fprintf(stdout, "\\x%02x", guid[i]&255);
961 }
962 if (tstamp) {
963 time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE;
964 char tbuf[100];
965 struct tm *tm;
966 tm = localtime(&then);
967 strftime(tbuf, 100, " (%D %T)",tm);
968 fputs(tbuf, stdout);
969 }
970 }
971
972 static void examine_vd(int n, struct ddf_super *sb, char *guid)
973 {
974 int crl = sb->conf_rec_len;
975 struct vcl *vcl;
976
977 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
978 struct vd_config *vc = &vcl->conf;
979
980 if (calc_crc(vc, crl*512) != vc->crc)
981 continue;
982 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
983 continue;
984
985 /* Ok, we know about this VD, let's give more details */
986 printf(" Raid Devices[%d] : %d\n", n,
987 __be16_to_cpu(vc->prim_elmnt_count));
988 printf(" Chunk Size[%d] : %d sectors\n", n,
989 1 << vc->chunk_shift);
990 printf(" Raid Level[%d] : %s\n", n,
991 map_num(ddf_level, vc->prl)?:"-unknown-");
992 if (vc->sec_elmnt_count != 1) {
993 printf(" Secondary Position[%d] : %d of %d\n", n,
994 vc->sec_elmnt_seq, vc->sec_elmnt_count);
995 printf(" Secondary Level[%d] : %s\n", n,
996 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
997 }
998 printf(" Device Size[%d] : %llu\n", n,
999 __be64_to_cpu(vc->blocks)/2);
1000 printf(" Array Size[%d] : %llu\n", n,
1001 __be64_to_cpu(vc->array_blocks)/2);
1002 }
1003 }
1004
1005 static void examine_vds(struct ddf_super *sb)
1006 {
1007 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
1008 int i;
1009 printf(" Virtual Disks : %d\n", cnt);
1010
1011 for (i=0; i<cnt; i++) {
1012 struct virtual_entry *ve = &sb->virt->entries[i];
1013 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
1014 printf("\n");
1015 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
1016 printf(" state[%d] : %s, %s%s\n", i,
1017 map_num(ddf_state, ve->state & 7),
1018 (ve->state & 8) ? "Morphing, ": "",
1019 (ve->state & 16)? "Not Consistent" : "Consistent");
1020 printf(" init state[%d] : %s\n", i,
1021 map_num(ddf_init_state, ve->init_state&3));
1022 printf(" access[%d] : %s\n", i,
1023 map_num(ddf_access, (ve->init_state>>6) & 3));
1024 printf(" Name[%d] : %.16s\n", i, ve->name);
1025 examine_vd(i, sb, ve->guid);
1026 }
1027 if (cnt) printf("\n");
1028 }
1029
1030 static void examine_pds(struct ddf_super *sb)
1031 {
1032 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1033 int i;
1034 struct dl *dl;
1035 printf(" Physical Disks : %d\n", cnt);
1036
1037 for (i=0 ; i<cnt ; i++) {
1038 struct phys_disk_entry *pd = &sb->phys->entries[i];
1039 int type = __be16_to_cpu(pd->type);
1040 int state = __be16_to_cpu(pd->state);
1041
1042 printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1043 printf("\n");
1044 printf(" ref[%d] : %08x\n", i,
1045 __be32_to_cpu(pd->refnum));
1046 printf(" mode[%d] : %s%s%s%s%s\n", i,
1047 (type&2) ? "active":"",
1048 (type&4) ? "Global Spare":"",
1049 (type&8) ? "spare" : "",
1050 (type&16)? ", foreign" : "",
1051 (type&32)? "pass-through" : "");
1052 printf(" state[%d] : %s%s%s%s%s%s%s\n", i,
1053 (state&1)? "Online": "Offline",
1054 (state&2)? ", Failed": "",
1055 (state&4)? ", Rebuilding": "",
1056 (state&8)? ", in-transition": "",
1057 (state&16)? ", SMART errors": "",
1058 (state&32)? ", Unrecovered Read Errors": "",
1059 (state&64)? ", Missing" : "");
1060 printf(" Avail Size[%d] : %llu K\n", i,
1061 __be64_to_cpu(pd->config_size)>>1);
1062 for (dl = sb->dlist; dl ; dl = dl->next) {
1063 if (dl->disk.refnum == pd->refnum) {
1064 char *dv = map_dev(dl->major, dl->minor, 0);
1065 if (dv)
1066 printf(" Device[%d] : %s\n",
1067 i, dv);
1068 }
1069 }
1070 printf("\n");
1071 }
1072 }
1073
1074 static void examine_super_ddf(struct supertype *st, char *homehost)
1075 {
1076 struct ddf_super *sb = st->sb;
1077
1078 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1079 printf(" Version : %.8s\n", sb->anchor.revision);
1080 printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
1081 printf("\n");
1082 printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
1083 printf("\n");
1084 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1085 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1086 ?"yes" : "no");
1087 examine_vds(sb);
1088 examine_pds(sb);
1089 }
1090
1091 static void brief_examine_super_ddf(struct supertype *st)
1092 {
1093 /* We just write a generic DDF ARRAY entry
1094 * The uuid is all hex, 6 groups of 4 bytes
1095 */
1096 struct ddf_super *ddf = st->sb;
1097 int i;
1098 printf("ARRAY /dev/ddf UUID=");
1099 for (i = 0; i < DDF_GUID_LEN; i++) {
1100 printf("%02x", ddf->anchor.guid[i]);
1101 if ((i&3) == 0 && i != 0)
1102 printf(":");
1103 }
1104 printf("\n");
1105 }
1106
1107 static void detail_super_ddf(struct supertype *st, char *homehost)
1108 {
1109 /* FIXME later
1110 * Could print DDF GUID
1111 * Need to find which array
1112 * If whole, briefly list all arrays
1113 * If one, give name
1114 */
1115 }
1116
1117 static void brief_detail_super_ddf(struct supertype *st)
1118 {
1119 /* FIXME I really need to know which array we are detailing.
1120 * Can that be stored in ddf_super??
1121 */
1122 // struct ddf_super *ddf = st->sb;
1123 }
1124
1125
1126 #endif
1127
1128 static int match_home_ddf(struct supertype *st, char *homehost)
1129 {
1130 /* It matches 'this' host if the controller is a
1131 * Linux-MD controller with vendor_data matching
1132 * the hostname
1133 */
1134 struct ddf_super *ddf = st->sb;
1135 int len = strlen(homehost);
1136
1137 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1138 len < sizeof(ddf->controller.vendor_data) &&
1139 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1140 ddf->controller.vendor_data[len] == 0);
1141 }
1142
1143 static struct vd_config *find_vdcr(struct ddf_super *ddf, int inst)
1144 {
1145 struct vcl *v;
1146 if (inst < 0 || inst > __be16_to_cpu(ddf->virt->populated_vdes))
1147 return NULL;
1148 for (v = ddf->conflist; v; v = v->next)
1149 if (memcmp(v->conf.guid,
1150 ddf->virt->entries[inst].guid,
1151 DDF_GUID_LEN) == 0)
1152 return &v->conf;
1153 return NULL;
1154 }
1155
1156 static int find_phys(struct ddf_super *ddf, __u32 phys_refnum)
1157 {
1158 /* Find the entry in phys_disk which has the given refnum
1159 * and return it's index
1160 */
1161 int i;
1162 for (i=0; i < __be16_to_cpu(ddf->phys->max_pdes); i++)
1163 if (ddf->phys->entries[i].refnum == phys_refnum)
1164 return i;
1165 return -1;
1166 }
1167
1168 static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1169 {
1170 /* The uuid returned here is used for:
1171 * uuid to put into bitmap file (Create, Grow)
1172 * uuid for backup header when saving critical section (Grow)
1173 * comparing uuids when re-adding a device into an array
1174 * For each of these we can make do with a truncated
1175 * or hashed uuid rather than the original, as long as
1176 * everyone agrees.
1177 * In each case the uuid required is that of the data-array,
1178 * not the device-set.
1179 * In the case of SVD we assume the BVD is of interest,
1180 * though that might be the case if a bitmap were made for
1181 * a mirrored SVD - worry about that later.
1182 * So we need to find the VD configuration record for the
1183 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1184 * The first 16 bytes of the sha1 of these is used.
1185 */
1186 struct ddf_super *ddf = st->sb;
1187 struct vd_config *vd = find_vdcr(ddf, ddf->conf_num);
1188
1189 if (!vd)
1190 memset(uuid, 0, sizeof (uuid));
1191 else {
1192 char buf[20];
1193 struct sha1_ctx ctx;
1194 sha1_init_ctx(&ctx);
1195 sha1_process_bytes(&vd->guid, DDF_GUID_LEN, &ctx);
1196 if (vd->sec_elmnt_count > 1)
1197 sha1_process_bytes(&vd->sec_elmnt_seq, 1, &ctx);
1198 sha1_finish_ctx(&ctx, buf);
1199 memcpy(uuid, buf, sizeof(uuid));
1200 }
1201 }
1202
1203 static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
1204 {
1205 struct ddf_super *ddf = st->sb;
1206 int i;
1207
1208 info->array.major_version = 1000;
1209 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1210 info->array.patch_version = 0;
1211 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1212 info->array.level = LEVEL_CONTAINER;
1213 info->array.layout = 0;
1214 info->array.md_minor = -1;
1215 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)
1216 (ddf->anchor.guid+16));
1217 info->array.utime = 0;
1218 info->array.chunk_size = 0;
1219
1220 // info->data_offset = ???;
1221 // info->component_size = ???;
1222
1223 info->disk.major = 0;
1224 info->disk.minor = 0;
1225 if (ddf->dlist) {
1226 info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
1227 info->disk.raid_disk = -1;
1228 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes) ; i++)
1229 if (ddf->phys->entries[i].refnum ==
1230 ddf->dlist->disk.refnum) {
1231 info->disk.raid_disk = i;
1232 break;
1233 }
1234 } else {
1235 info->disk.number = -1;
1236 // info->disk.raid_disk = find refnum in the table and use index;
1237 }
1238 info->disk.state = (1 << MD_DISK_SYNC);
1239
1240 info->reshape_active = 0;
1241
1242 strcpy(info->text_version, "ddf");
1243
1244 // uuid_from_super_ddf(info->uuid, sbv);
1245
1246 // info->name[] ?? ;
1247 }
1248
1249 static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
1250 {
1251 /* just need offset and size */
1252 struct ddf_super *ddf = st->sb;
1253 int n = info->disk.number;
1254
1255 info->data_offset = __be64_to_cpu(ddf->phys->entries[n].config_size);
1256 info->component_size = 32*1024*1024 / 512;
1257 }
1258
1259 static int rlq_to_layout(int rlq, int prl, int raiddisks);
1260
1261 static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
1262 {
1263 struct ddf_super *ddf = st->sb;
1264 struct vd_config *vd = find_vdcr(ddf, info->container_member);
1265
1266 /* FIXME this returns BVD info - what if we want SVD ?? */
1267
1268 info->array.major_version = 1000;
1269 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1270 info->array.patch_version = 0;
1271 info->array.raid_disks = __be16_to_cpu(vd->prim_elmnt_count);
1272 info->array.level = map_num1(ddf_level_num, vd->prl);
1273 info->array.layout = rlq_to_layout(vd->rlq, vd->prl,
1274 info->array.raid_disks);
1275 info->array.md_minor = -1;
1276 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)(vd->guid+16));
1277 info->array.utime = DECADE + __be32_to_cpu(vd->timestamp);
1278 info->array.chunk_size = 512 << vd->chunk_shift;
1279
1280 // info->data_offset = ???;
1281 // info->component_size = ???;
1282
1283 info->disk.major = 0;
1284 info->disk.minor = 0;
1285 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1286 // info->disk.raid_disk = find refnum in the table and use index;
1287 // info->disk.state = ???;
1288
1289 uuid_from_super_ddf(st, info->uuid);
1290
1291 sprintf(info->text_version, "/%s/%d",
1292 devnum2devname(st->container_dev),
1293 info->container_member);
1294
1295 // info->name[] ?? ;
1296 }
1297
1298 static void getinfo_super_n_bvd(struct supertype *st, struct mdinfo *info)
1299 {
1300 /* Find the particular details for info->disk.raid_disk.
1301 * This includes data_offset, component_size,
1302 */
1303 struct ddf_super *ddf = st->sb;
1304 __u64 *lba_offset = ddf->newconf->lba_offset;
1305 struct vd_config *conf = &ddf->newconf->conf;
1306 info->data_offset = __be64_to_cpu(lba_offset[info->disk.raid_disk]);
1307 info->component_size = __be64_to_cpu(conf->blocks);
1308 }
1309
1310 static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1311 char *update,
1312 char *devname, int verbose,
1313 int uuid_set, char *homehost)
1314 {
1315 /* For 'assemble' and 'force' we need to return non-zero if any
1316 * change was made. For others, the return value is ignored.
1317 * Update options are:
1318 * force-one : This device looks a bit old but needs to be included,
1319 * update age info appropriately.
1320 * assemble: clear any 'faulty' flag to allow this device to
1321 * be assembled.
1322 * force-array: Array is degraded but being forced, mark it clean
1323 * if that will be needed to assemble it.
1324 *
1325 * newdev: not used ????
1326 * grow: Array has gained a new device - this is currently for
1327 * linear only
1328 * resync: mark as dirty so a resync will happen.
1329 * uuid: Change the uuid of the array to match watch is given
1330 * homehost: update the recorded homehost
1331 * name: update the name - preserving the homehost
1332 * _reshape_progress: record new reshape_progress position.
1333 *
1334 * Following are not relevant for this version:
1335 * sparc2.2 : update from old dodgey metadata
1336 * super-minor: change the preferred_minor number
1337 * summaries: update redundant counters.
1338 */
1339 int rv = 0;
1340 // struct ddf_super *ddf = st->sb;
1341 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1342 // struct virtual_entry *ve = find_ve(ddf);
1343
1344
1345 /* we don't need to handle "force-*" or "assemble" as
1346 * there is no need to 'trick' the kernel. We the metadata is
1347 * first updated to activate the array, all the implied modifications
1348 * will just happen.
1349 */
1350
1351 if (strcmp(update, "grow") == 0) {
1352 /* FIXME */
1353 }
1354 if (strcmp(update, "resync") == 0) {
1355 // info->resync_checkpoint = 0;
1356 }
1357 /* We ignore UUID updates as they make even less sense
1358 * with DDF
1359 */
1360 if (strcmp(update, "homehost") == 0) {
1361 /* homehost is stored in controller->vendor_data,
1362 * or it is when we are the vendor
1363 */
1364 // if (info->vendor_is_local)
1365 // strcpy(ddf->controller.vendor_data, homehost);
1366 }
1367 if (strcmp(update, "name") == 0) {
1368 /* name is stored in virtual_entry->name */
1369 // memset(ve->name, ' ', 16);
1370 // strncpy(ve->name, info->name, 16);
1371 }
1372 if (strcmp(update, "_reshape_progress") == 0) {
1373 /* We don't support reshape yet */
1374 }
1375
1376 // update_all_csum(ddf);
1377
1378 return rv;
1379 }
1380
1381 static void make_header_guid(char *guid)
1382 {
1383 __u32 stamp;
1384 int rfd;
1385 /* Create a DDF Header of Virtual Disk GUID */
1386
1387 /* 24 bytes of fiction required.
1388 * first 8 are a 'vendor-id' - "Linux-MD"
1389 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1390 * Remaining 8 random number plus timestamp
1391 */
1392 memcpy(guid, T10, sizeof(T10));
1393 stamp = __cpu_to_be32(0xdeadbeef);
1394 memcpy(guid+8, &stamp, 4);
1395 stamp = __cpu_to_be32(0);
1396 memcpy(guid+12, &stamp, 4);
1397 stamp = __cpu_to_be32(time(0) - DECADE);
1398 memcpy(guid+16, &stamp, 4);
1399 rfd = open("/dev/urandom", O_RDONLY);
1400 if (rfd < 0 || read(rfd, &stamp, 4) != 4)
1401 stamp = random();
1402 memcpy(guid+20, &stamp, 4);
1403 if (rfd >= 0) close(rfd);
1404 }
1405 static int init_super_ddf(struct supertype *st,
1406 mdu_array_info_t *info,
1407 unsigned long long size, char *name, char *homehost,
1408 int *uuid)
1409 {
1410 /* This is primarily called by Create when creating a new array.
1411 * We will then get add_to_super called for each component, and then
1412 * write_init_super called to write it out to each device.
1413 * For DDF, Create can create on fresh devices or on a pre-existing
1414 * array.
1415 * To create on a pre-existing array a different method will be called.
1416 * This one is just for fresh drives.
1417 *
1418 * We need to create the entire 'ddf' structure which includes:
1419 * DDF headers - these are easy.
1420 * Controller data - a Sector describing this controller .. not that
1421 * this is a controller exactly.
1422 * Physical Disk Record - one entry per device, so
1423 * leave plenty of space.
1424 * Virtual Disk Records - again, just leave plenty of space.
1425 * This just lists VDs, doesn't give details
1426 * Config records - describes the VDs that use this disk
1427 * DiskData - describes 'this' device.
1428 * BadBlockManagement - empty
1429 * Diag Space - empty
1430 * Vendor Logs - Could we put bitmaps here?
1431 *
1432 */
1433 struct ddf_super *ddf;
1434 char hostname[17];
1435 int hostlen;
1436 int max_phys_disks, max_virt_disks;
1437 unsigned long long sector;
1438 int clen;
1439 int i;
1440 int pdsize, vdsize;
1441 struct phys_disk *pd;
1442 struct virtual_disk *vd;
1443
1444 ddf = malloc(sizeof(*ddf));
1445 ddf->dlist = NULL; /* no physical disks yet */
1446 ddf->conflist = NULL; /* No virtual disks yet */
1447
1448 /* At least 32MB *must* be reserved for the ddf. So let's just
1449 * start 32MB from the end, and put the primary header there.
1450 * Don't do secondary for now.
1451 * We don't know exactly where that will be yet as it could be
1452 * different on each device. To just set up the lengths.
1453 *
1454 */
1455
1456 ddf->anchor.magic = DDF_HEADER_MAGIC;
1457 make_header_guid(ddf->anchor.guid);
1458
1459 memcpy(ddf->anchor.revision, DDF_REVISION, 8);
1460 ddf->anchor.seq = __cpu_to_be32(1);
1461 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
1462 ddf->anchor.openflag = 0xFF;
1463 ddf->anchor.foreignflag = 0;
1464 ddf->anchor.enforcegroups = 0; /* Is this best?? */
1465 ddf->anchor.pad0 = 0xff;
1466 memset(ddf->anchor.pad1, 0xff, 12);
1467 memset(ddf->anchor.header_ext, 0xff, 32);
1468 ddf->anchor.primary_lba = ~(__u64)0;
1469 ddf->anchor.secondary_lba = ~(__u64)0;
1470 ddf->anchor.type = DDF_HEADER_ANCHOR;
1471 memset(ddf->anchor.pad2, 0xff, 3);
1472 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
1473 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
1474 of 32M reserved.. */
1475 max_phys_disks = 1023; /* Should be enough */
1476 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
1477 max_virt_disks = 255;
1478 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
1479 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
1480 ddf->max_part = 64;
1481 ddf->conf_rec_len = 1 + 256 * 12 / 512;
1482 ddf->anchor.config_record_len = __cpu_to_be16(ddf->conf_rec_len);
1483 ddf->anchor.max_primary_element_entries = __cpu_to_be16(256);
1484 ddf->mppe = 256;
1485 memset(ddf->anchor.pad3, 0xff, 54);
1486
1487 /* controller sections is one sector long immediately
1488 * after the ddf header */
1489 sector = 1;
1490 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
1491 ddf->anchor.controller_section_length = __cpu_to_be32(1);
1492 sector += 1;
1493
1494 /* phys is 8 sectors after that */
1495 pdsize = ROUND_UP(sizeof(struct phys_disk) +
1496 sizeof(struct phys_disk_entry)*max_phys_disks,
1497 512);
1498 switch(pdsize/512) {
1499 case 2: case 8: case 32: case 128: case 512: break;
1500 default: abort();
1501 }
1502 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
1503 ddf->anchor.phys_section_length =
1504 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
1505 sector += pdsize/512;
1506
1507 /* virt is another 32 sectors */
1508 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
1509 sizeof(struct virtual_entry) * max_virt_disks,
1510 512);
1511 switch(vdsize/512) {
1512 case 2: case 8: case 32: case 128: case 512: break;
1513 default: abort();
1514 }
1515 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
1516 ddf->anchor.virt_section_length =
1517 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
1518 sector += vdsize/512;
1519
1520 clen = (1 + 256*12/512) * (64+1);
1521 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
1522 ddf->anchor.config_section_length = __cpu_to_be32(clen);
1523 sector += clen;
1524
1525 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
1526 ddf->anchor.data_section_length = __cpu_to_be32(1);
1527 sector += 1;
1528
1529 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
1530 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
1531 ddf->anchor.diag_space_length = __cpu_to_be32(0);
1532 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
1533 ddf->anchor.vendor_length = __cpu_to_be32(0);
1534 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
1535
1536 memset(ddf->anchor.pad4, 0xff, 256);
1537
1538 memcpy(&ddf->primary, &ddf->anchor, 512);
1539 memcpy(&ddf->secondary, &ddf->anchor, 512);
1540
1541 ddf->primary.openflag = 1; /* I guess.. */
1542 ddf->primary.type = DDF_HEADER_PRIMARY;
1543
1544 ddf->secondary.openflag = 1; /* I guess.. */
1545 ddf->secondary.type = DDF_HEADER_SECONDARY;
1546
1547 ddf->active = &ddf->primary;
1548
1549 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
1550
1551 /* 24 more bytes of fiction required.
1552 * first 8 are a 'vendor-id' - "Linux-MD"
1553 * Remaining 16 are serial number.... maybe a hostname would do?
1554 */
1555 memcpy(ddf->controller.guid, T10, sizeof(T10));
1556 gethostname(hostname, 17);
1557 hostname[17] = 0;
1558 hostlen = strlen(hostname);
1559 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
1560 for (i = strlen(T10) ; i+hostlen < 24; i++)
1561 ddf->controller.guid[i] = ' ';
1562
1563 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
1564 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
1565 ddf->controller.type.sub_vendor_id = 0;
1566 ddf->controller.type.sub_device_id = 0;
1567 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
1568 memset(ddf->controller.pad, 0xff, 8);
1569 memset(ddf->controller.vendor_data, 0xff, 448);
1570
1571 pd = ddf->phys = malloc(pdsize);
1572 ddf->pdsize = pdsize;
1573
1574 memset(pd, 0xff, pdsize);
1575 memset(pd, 0, sizeof(*pd));
1576 pd->magic = DDF_PHYS_DATA_MAGIC;
1577 pd->used_pdes = __cpu_to_be16(0);
1578 pd->max_pdes = __cpu_to_be16(max_phys_disks);
1579 memset(pd->pad, 0xff, 52);
1580
1581 vd = ddf->virt = malloc(vdsize);
1582 ddf->vdsize = vdsize;
1583 memset(vd, 0, vdsize);
1584 vd->magic = DDF_VIRT_RECORDS_MAGIC;
1585 vd->populated_vdes = __cpu_to_be16(0);
1586 vd->max_vdes = __cpu_to_be16(max_virt_disks);
1587 memset(vd->pad, 0xff, 52);
1588
1589 for (i=0; i<max_virt_disks; i++)
1590 memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
1591
1592 st->sb = ddf;
1593 return 1;
1594 }
1595
1596 static int all_ff(char *guid)
1597 {
1598 int i;
1599 for (i = 0; i < DDF_GUID_LEN; i++)
1600 if (guid[i] != (char)0xff)
1601 return 0;
1602 return 1;
1603 }
1604 static int chunk_to_shift(int chunksize)
1605 {
1606 return ffs(chunksize/512)-1;
1607 }
1608
1609 static int level_to_prl(int level)
1610 {
1611 switch (level) {
1612 case LEVEL_LINEAR: return DDF_CONCAT;
1613 case 0: return DDF_RAID0;
1614 case 1: return DDF_RAID1;
1615 case 4: return DDF_RAID4;
1616 case 5: return DDF_RAID5;
1617 case 6: return DDF_RAID6;
1618 default: return -1;
1619 }
1620 }
1621 static int layout_to_rlq(int level, int layout, int raiddisks)
1622 {
1623 switch(level) {
1624 case 0:
1625 return DDF_RAID0_SIMPLE;
1626 case 1:
1627 switch(raiddisks) {
1628 case 2: return DDF_RAID1_SIMPLE;
1629 case 3: return DDF_RAID1_MULTI;
1630 default: return -1;
1631 }
1632 case 4:
1633 switch(layout) {
1634 case 0: return DDF_RAID4_N;
1635 }
1636 break;
1637 case 5:
1638 case 6:
1639 switch(layout) {
1640 case ALGORITHM_LEFT_ASYMMETRIC:
1641 return DDF_RAID5_N_RESTART;
1642 case ALGORITHM_RIGHT_ASYMMETRIC:
1643 return DDF_RAID5_0_RESTART;
1644 case ALGORITHM_LEFT_SYMMETRIC:
1645 return DDF_RAID5_N_CONTINUE;
1646 case ALGORITHM_RIGHT_SYMMETRIC:
1647 return -1; /* not mentioned in standard */
1648 }
1649 }
1650 return -1;
1651 }
1652
1653 static int rlq_to_layout(int rlq, int prl, int raiddisks)
1654 {
1655 switch(prl) {
1656 case DDF_RAID0:
1657 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1658 case DDF_RAID1:
1659 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1660 on raiddisks*/
1661 case DDF_RAID4:
1662 switch(rlq) {
1663 case DDF_RAID4_N:
1664 return 0;
1665 default:
1666 /* not supported */
1667 return -1; /* FIXME this isn't checked */
1668 }
1669 case DDF_RAID5:
1670 case DDF_RAID6:
1671 switch(rlq) {
1672 case DDF_RAID5_N_RESTART:
1673 return ALGORITHM_LEFT_ASYMMETRIC;
1674 case DDF_RAID5_0_RESTART:
1675 return ALGORITHM_RIGHT_ASYMMETRIC;
1676 case DDF_RAID5_N_CONTINUE:
1677 return ALGORITHM_LEFT_SYMMETRIC;
1678 default:
1679 return -1;
1680 }
1681 }
1682 return -1;
1683 }
1684
1685 static int init_super_ddf_bvd(struct supertype *st,
1686 mdu_array_info_t *info,
1687 unsigned long long size,
1688 char *name, char *homehost,
1689 int *uuid)
1690 {
1691 /* We are creating a BVD inside a pre-existing container.
1692 * so st->sb is already set.
1693 * We need to create a new vd_config and a new virtual_entry
1694 */
1695 struct ddf_super *ddf = st->sb;
1696 int venum;
1697 struct virtual_entry *ve;
1698 struct vcl *vcl;
1699 struct vd_config *vc;
1700
1701 if (__be16_to_cpu(ddf->virt->populated_vdes)
1702 >= __be16_to_cpu(ddf->virt->max_vdes)) {
1703 fprintf(stderr, Name": This ddf already has the "
1704 "maximum of %d virtual devices\n",
1705 __be16_to_cpu(ddf->virt->max_vdes));
1706 return 0;
1707 }
1708
1709 for (venum = 0; venum < __be16_to_cpu(ddf->virt->max_vdes); venum++)
1710 if (all_ff(ddf->virt->entries[venum].guid))
1711 break;
1712 if (venum == __be16_to_cpu(ddf->virt->max_vdes)) {
1713 fprintf(stderr, Name ": Cannot find spare slot for "
1714 "virtual disk - DDF is corrupt\n");
1715 return 0;
1716 }
1717 ve = &ddf->virt->entries[venum];
1718 ddf->conf_num = venum;
1719
1720 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
1721 * timestamp, random number
1722 */
1723 make_header_guid(ve->guid);
1724 ve->unit = __cpu_to_be16(info->md_minor);
1725 ve->pad0 = 0xFFFF;
1726 ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
1727 ve->type = 0;
1728 ve->state = DDF_state_degraded; /* Will be modified as devices are added */
1729 if (info->state & 1) /* clean */
1730 ve->init_state = DDF_init_full;
1731 else
1732 ve->init_state = DDF_init_not;
1733
1734 memset(ve->pad1, 0xff, 14);
1735 memset(ve->name, ' ', 16);
1736 if (name)
1737 strncpy(ve->name, name, 16);
1738 ddf->virt->populated_vdes =
1739 __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
1740
1741 /* Now create a new vd_config */
1742 vcl = malloc(offsetof(struct vcl, conf) + ddf->conf_rec_len * 512);
1743 vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[ddf->mppe];
1744
1745 vc = &vcl->conf;
1746
1747 vc->magic = DDF_VD_CONF_MAGIC;
1748 memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
1749 vc->timestamp = __cpu_to_be32(time(0)-DECADE);
1750 vc->seqnum = __cpu_to_be32(1);
1751 memset(vc->pad0, 0xff, 24);
1752 vc->prim_elmnt_count = __cpu_to_be16(info->raid_disks);
1753 vc->chunk_shift = chunk_to_shift(info->chunk_size);
1754 vc->prl = level_to_prl(info->level);
1755 vc->rlq = layout_to_rlq(info->level, info->layout, info->raid_disks);
1756 vc->sec_elmnt_count = 1;
1757 vc->sec_elmnt_seq = 0;
1758 vc->srl = 0;
1759 vc->blocks = __cpu_to_be64(info->size * 2);
1760 vc->array_blocks = __cpu_to_be64(
1761 calc_array_size(info->level, info->raid_disks, info->layout,
1762 info->chunk_size, info->size*2));
1763 memset(vc->pad1, 0xff, 8);
1764 vc->spare_refs[0] = 0xffffffff;
1765 vc->spare_refs[1] = 0xffffffff;
1766 vc->spare_refs[2] = 0xffffffff;
1767 vc->spare_refs[3] = 0xffffffff;
1768 vc->spare_refs[4] = 0xffffffff;
1769 vc->spare_refs[5] = 0xffffffff;
1770 vc->spare_refs[6] = 0xffffffff;
1771 vc->spare_refs[7] = 0xffffffff;
1772 memset(vc->cache_pol, 0, 8);
1773 vc->bg_rate = 0x80;
1774 memset(vc->pad2, 0xff, 3);
1775 memset(vc->pad3, 0xff, 52);
1776 memset(vc->pad4, 0xff, 192);
1777 memset(vc->v0, 0xff, 32);
1778 memset(vc->v1, 0xff, 32);
1779 memset(vc->v2, 0xff, 16);
1780 memset(vc->v3, 0xff, 16);
1781 memset(vc->vendor, 0xff, 32);
1782
1783 memset(vc->phys_refnum, 0xff, 4*ddf->mppe);
1784 memset(vc->phys_refnum+(ddf->mppe * 4), 0x00, 8*ddf->mppe);
1785
1786 vcl->next = ddf->conflist;
1787 ddf->conflist = vcl;
1788 ddf->newconf = vcl;
1789 return 1;
1790 }
1791
1792 static void add_to_super_ddf_bvd(struct supertype *st,
1793 mdu_disk_info_t *dk, int fd, char *devname)
1794 {
1795 /* fd and devname identify a device with-in the ddf container (st).
1796 * dk identifies a location in the new BVD.
1797 * We need to find suitable free space in that device and update
1798 * the phys_refnum and lba_offset for the newly created vd_config.
1799 * We might also want to update the type in the phys_disk
1800 * section.
1801 */
1802 struct dl *dl;
1803 struct ddf_super *ddf = st->sb;
1804 struct vd_config *vc;
1805 __u64 *lba_offset;
1806 int working;
1807 int i;
1808 int max_virt_disks;
1809
1810 for (dl = ddf->dlist; dl ; dl = dl->next)
1811 if (dl->major == dk->major &&
1812 dl->minor == dk->minor)
1813 break;
1814 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1815 return;
1816
1817 vc = &ddf->newconf->conf;
1818 lba_offset = ddf->newconf->lba_offset;
1819 vc->phys_refnum[dk->raid_disk] = dl->disk.refnum;
1820 lba_offset[dk->raid_disk] = 0; /* FIXME */
1821
1822 for (i=0; i < ddf->max_part ; i++)
1823 if (dl->vlist[i] == NULL)
1824 break;
1825 if (i == ddf->max_part)
1826 return;
1827 dl->vlist[i] = ddf->newconf;
1828
1829 dl->fd = fd;
1830 dl->devname = devname;
1831
1832 /* Check how many working raid_disks, and if we can mark
1833 * array as optimal yet
1834 */
1835 working = 0;
1836
1837 for (i=0; i < __be16_to_cpu(vc->prim_elmnt_count); i++)
1838 if (vc->phys_refnum[i] != 0xffffffff)
1839 working++;
1840 /* Find which virtual_entry */
1841 max_virt_disks = __be16_to_cpu(ddf->active->max_vd_entries);
1842 for (i=0; i < max_virt_disks ; i++)
1843 if (memcmp(ddf->virt->entries[i].guid,
1844 vc->guid, DDF_GUID_LEN)==0)
1845 break;
1846 if (i == max_virt_disks)
1847 return;
1848 if (working == __be16_to_cpu(vc->prim_elmnt_count))
1849 ddf->virt->entries[i].state =
1850 (ddf->virt->entries[i].state & ~DDF_state_mask)
1851 | DDF_state_optimal;
1852
1853 if (vc->prl == DDF_RAID6 &&
1854 working+1 == __be16_to_cpu(vc->prim_elmnt_count))
1855 ddf->virt->entries[i].state =
1856 (ddf->virt->entries[i].state & ~DDF_state_mask)
1857 | DDF_state_part_optimal;
1858
1859 ddf->phys->entries[dl->pdnum].type &= ~__cpu_to_be16(DDF_Global_Spare);
1860 ddf->phys->entries[dl->pdnum].type |= __cpu_to_be16(DDF_Active_in_VD);
1861 }
1862
1863 /* add a device to a container, either while creating it or while
1864 * expanding a pre-existing container
1865 */
1866 static void add_to_super_ddf(struct supertype *st,
1867 mdu_disk_info_t *dk, int fd, char *devname)
1868 {
1869 struct ddf_super *ddf = st->sb;
1870 struct dl *dd;
1871 time_t now;
1872 struct tm *tm;
1873 unsigned long long size;
1874 struct phys_disk_entry *pde;
1875 int n, i;
1876 struct stat stb;
1877
1878 /* This is device numbered dk->number. We need to create
1879 * a phys_disk entry and a more detailed disk_data entry.
1880 */
1881 fstat(fd, &stb);
1882 dd = malloc(sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part);
1883 dd->major = major(stb.st_rdev);
1884 dd->minor = minor(stb.st_rdev);
1885 dd->devname = devname;
1886 dd->next = ddf->dlist;
1887 dd->fd = fd;
1888 dd->spare = NULL;
1889
1890 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
1891 now = time(0);
1892 tm = localtime(&now);
1893 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
1894 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
1895 *(__u32*)(dd->disk.guid + 16) = random();
1896 *(__u32*)(dd->disk.guid + 20) = random();
1897
1898 dd->disk.refnum = random(); /* and hope for the best FIXME check this is unique!!*/
1899 dd->disk.forced_ref = 1;
1900 dd->disk.forced_guid = 1;
1901 memset(dd->disk.vendor, ' ', 32);
1902 memcpy(dd->disk.vendor, "Linux", 5);
1903 memset(dd->disk.pad, 0xff, 442);
1904 for (i = 0; i < ddf->max_part ; i++)
1905 dd->vlist[i] = NULL;
1906
1907 n = __be16_to_cpu(ddf->phys->used_pdes);
1908 pde = &ddf->phys->entries[n];
1909 dd->pdnum = n;
1910
1911 n++;
1912 ddf->phys->used_pdes = __cpu_to_be16(n);
1913
1914 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
1915 pde->refnum = dd->disk.refnum;
1916 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID | DDF_Global_Spare);
1917 pde->state = __cpu_to_be16(DDF_Online);
1918 get_dev_size(fd, NULL, &size);
1919 /* We are required to reserve 32Meg, and record the size in sectors */
1920 pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512);
1921 sprintf(pde->path, "%17.17s","Information: nil") ;
1922 memset(pde->pad, 0xff, 6);
1923
1924 ddf->dlist = dd;
1925 }
1926
1927 /*
1928 * This is the write_init_super method for a ddf container. It is
1929 * called when creating a container or adding another device to a
1930 * container.
1931 */
1932
1933 #ifndef MDASSEMBLE
1934 static int __write_init_super_ddf(struct supertype *st, int do_close)
1935 {
1936
1937 struct ddf_super *ddf = st->sb;
1938 int i;
1939 struct dl *d;
1940 int n_config;
1941 int conf_size;
1942
1943 unsigned long long size, sector;
1944
1945 for (d = ddf->dlist; d; d=d->next) {
1946 int fd = d->fd;
1947
1948 if (fd < 0)
1949 continue;
1950
1951 /* We need to fill in the primary, (secondary) and workspace
1952 * lba's in the headers, set their checksums,
1953 * Also checksum phys, virt....
1954 *
1955 * Then write everything out, finally the anchor is written.
1956 */
1957 get_dev_size(fd, NULL, &size);
1958 size /= 512;
1959 ddf->anchor.workspace_lba = __cpu_to_be64(size - 32*1024*2);
1960 ddf->anchor.primary_lba = __cpu_to_be64(size - 16*1024*2);
1961 ddf->anchor.seq = __cpu_to_be32(1);
1962 memcpy(&ddf->primary, &ddf->anchor, 512);
1963 memcpy(&ddf->secondary, &ddf->anchor, 512);
1964
1965 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
1966 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
1967 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
1968
1969 ddf->primary.openflag = 0;
1970 ddf->primary.type = DDF_HEADER_PRIMARY;
1971
1972 ddf->secondary.openflag = 0;
1973 ddf->secondary.type = DDF_HEADER_SECONDARY;
1974
1975 ddf->primary.crc = calc_crc(&ddf->primary, 512);
1976 ddf->secondary.crc = calc_crc(&ddf->secondary, 512);
1977
1978 sector = size - 16*1024*2;
1979 lseek64(fd, sector<<9, 0);
1980 write(fd, &ddf->primary, 512);
1981
1982 ddf->controller.crc = calc_crc(&ddf->controller, 512);
1983 write(fd, &ddf->controller, 512);
1984
1985 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
1986
1987 write(fd, ddf->phys, ddf->pdsize);
1988
1989 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
1990 write(fd, ddf->virt, ddf->vdsize);
1991
1992 /* Now write lots of config records. */
1993 n_config = ddf->max_part;
1994 conf_size = ddf->conf_rec_len * 512;
1995 for (i = 0 ; i <= n_config ; i++) {
1996 struct vcl *c = d->vlist[i];
1997 if (i == n_config)
1998 c = (struct vcl*)d->spare;
1999
2000 if (c) {
2001 c->conf.crc = calc_crc(&c->conf, conf_size);
2002 write(fd, &c->conf, conf_size);
2003 } else {
2004 __u32 sig = 0xffffffff;
2005 write(fd, &sig, 4);
2006 lseek64(fd, conf_size-4, SEEK_CUR);
2007 }
2008 }
2009 d->disk.crc = calc_crc(&d->disk, 512);
2010 write(fd, &d->disk, 512);
2011
2012 /* Maybe do the same for secondary */
2013
2014 lseek64(fd, (size-1)*512, SEEK_SET);
2015 write(fd, &ddf->anchor, 512);
2016 if (do_close) {
2017 close(fd);
2018 d->fd = -1;
2019 }
2020 }
2021 return 1;
2022 }
2023
2024 static int write_init_super_ddf(struct supertype *st)
2025 {
2026 return __write_init_super_ddf(st, 1);
2027 }
2028
2029 #endif
2030
2031 static __u64 avail_size_ddf(struct supertype *st, __u64 devsize)
2032 {
2033 /* We must reserve the last 32Meg */
2034 if (devsize <= 32*1024*2)
2035 return 0;
2036 return devsize - 32*1024*2;
2037 }
2038
2039 #ifndef MDASSEMBLE
2040 int validate_geometry_ddf(struct supertype *st,
2041 int level, int layout, int raiddisks,
2042 int chunk, unsigned long long size,
2043 char *dev, unsigned long long *freesize)
2044 {
2045 int fd;
2046 struct mdinfo *sra;
2047 int cfd;
2048
2049 /* ddf potentially supports lots of things, but it depends on
2050 * what devices are offered (and maybe kernel version?)
2051 * If given unused devices, we will make a container.
2052 * If given devices in a container, we will make a BVD.
2053 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2054 */
2055
2056 if (level == LEVEL_CONTAINER) {
2057 st->ss = &super_ddf_container;
2058 if (dev) {
2059 int rv =st->ss->validate_geometry(st, level, layout,
2060 raiddisks, chunk,
2061 size,
2062 NULL, freesize);
2063 if (rv)
2064 return rv;
2065 }
2066 return st->ss->validate_geometry(st, level, layout, raiddisks,
2067 chunk, size, dev, freesize);
2068 }
2069
2070 if (st->sb) {
2071 /* creating in a given container */
2072 st->ss = &super_ddf_bvd;
2073 if (dev) {
2074 int rv =st->ss->validate_geometry(st, level, layout,
2075 raiddisks, chunk,
2076 size,
2077 NULL, freesize);
2078 if (rv)
2079 return rv;
2080 }
2081 return st->ss->validate_geometry(st, level, layout, raiddisks,
2082 chunk, size, dev, freesize);
2083 }
2084 /* FIXME should exclude MULTIPATH, or more appropriately, allow
2085 * only known levels.
2086 */
2087 if (!dev)
2088 return 1;
2089
2090 /* This device needs to be either a device in a 'ddf' container,
2091 * or it needs to be a 'ddf-bvd' array.
2092 */
2093
2094 fd = open(dev, O_RDONLY|O_EXCL, 0);
2095 if (fd >= 0) {
2096 sra = sysfs_read(fd, 0, GET_VERSION);
2097 close(fd);
2098 if (sra && sra->array.major_version == -1 &&
2099 strcmp(sra->text_version, "ddf-bvd") == 0) {
2100 st->ss = &super_ddf_svd;
2101 return st->ss->validate_geometry(st, level, layout,
2102 raiddisks, chunk, size,
2103 dev, freesize);
2104 }
2105
2106 fprintf(stderr,
2107 Name ": Cannot create this array on device %s\n",
2108 dev);
2109 return 0;
2110 }
2111 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2112 fprintf(stderr, Name ": Cannot open %s: %s\n",
2113 dev, strerror(errno));
2114 return 0;
2115 }
2116 /* Well, it is in use by someone, maybe a 'ddf' container. */
2117 cfd = open_container(fd);
2118 if (cfd < 0) {
2119 close(fd);
2120 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2121 dev);
2122 return 0;
2123 }
2124 sra = sysfs_read(cfd, 0, GET_VERSION);
2125 close(fd);
2126 if (sra && sra->array.major_version == -1 &&
2127 strcmp(sra->text_version, "ddf") == 0) {
2128 /* This is a member of a ddf container. Load the container
2129 * and try to create a bvd
2130 */
2131 struct ddf_super *ddf;
2132 st->ss = &super_ddf_bvd;
2133 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL, 1) == 0) {
2134 st->sb = ddf;
2135 st->container_dev = fd2devnum(cfd);
2136 close(cfd);
2137 return st->ss->validate_geometry(st, level, layout,
2138 raiddisks, chunk, size,
2139 dev, freesize);
2140 }
2141 close(cfd);
2142 }
2143 fprintf(stderr, Name ": Cannot use %s: Already in use\n",
2144 dev);
2145 return 1;
2146 }
2147
2148 int validate_geometry_ddf_container(struct supertype *st,
2149 int level, int layout, int raiddisks,
2150 int chunk, unsigned long long size,
2151 char *dev, unsigned long long *freesize)
2152 {
2153 int fd;
2154 unsigned long long ldsize;
2155
2156 if (level != LEVEL_CONTAINER)
2157 return 0;
2158 if (!dev)
2159 return 1;
2160
2161 fd = open(dev, O_RDONLY|O_EXCL, 0);
2162 if (fd < 0) {
2163 fprintf(stderr, Name ": Cannot open %s: %s\n",
2164 dev, strerror(errno));
2165 return 0;
2166 }
2167 if (!get_dev_size(fd, dev, &ldsize)) {
2168 close(fd);
2169 return 0;
2170 }
2171 close(fd);
2172
2173 *freesize = avail_size_ddf(st, ldsize);
2174
2175 return 1;
2176 }
2177
2178 struct extent {
2179 unsigned long long start, size;
2180 };
2181 int cmp_extent(const void *av, const void *bv)
2182 {
2183 const struct extent *a = av;
2184 const struct extent *b = bv;
2185 if (a->start < b->start)
2186 return -1;
2187 if (a->start > b->start)
2188 return 1;
2189 return 0;
2190 }
2191
2192 struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
2193 {
2194 /* find a list of used extents on the give physical device
2195 * (dnum) or the given ddf.
2196 * Return a malloced array of 'struct extent'
2197
2198 FIXME ignore DDF_Legacy devices?
2199
2200 */
2201 struct extent *rv;
2202 int n = 0;
2203 int dnum;
2204 int i, j;
2205
2206 for (dnum = 0; dnum < ddf->phys->used_pdes; dnum++)
2207 if (memcmp(dl->disk.guid,
2208 ddf->phys->entries[dnum].guid,
2209 DDF_GUID_LEN) == 0)
2210 break;
2211
2212 if (dnum == ddf->phys->used_pdes)
2213 return NULL;
2214
2215 rv = malloc(sizeof(struct extent) * (ddf->max_part + 2));
2216 if (!rv)
2217 return NULL;
2218
2219 for (i = 0; i < ddf->max_part; i++) {
2220 struct vcl *v = dl->vlist[i];
2221 if (v == NULL)
2222 continue;
2223 for (j=0; j < v->conf.prim_elmnt_count; j++)
2224 if (v->conf.phys_refnum[j] == dl->disk.refnum) {
2225 /* This device plays role 'j' in 'v'. */
2226 rv[n].start = __be64_to_cpu(v->lba_offset[j]);
2227 rv[n].size = __be64_to_cpu(v->conf.blocks);
2228 n++;
2229 break;
2230 }
2231 }
2232 qsort(rv, n, sizeof(*rv), cmp_extent);
2233
2234 rv[n].start = __be64_to_cpu(ddf->phys->entries[dnum].config_size);
2235 rv[n].size = 0;
2236 return rv;
2237 }
2238
2239 int validate_geometry_ddf_bvd(struct supertype *st,
2240 int level, int layout, int raiddisks,
2241 int chunk, unsigned long long size,
2242 char *dev, unsigned long long *freesize)
2243 {
2244 struct stat stb;
2245 struct ddf_super *ddf = st->sb;
2246 struct dl *dl;
2247 unsigned long long pos = 0;
2248 unsigned long long maxsize;
2249 struct extent *e;
2250 int i;
2251 /* ddf/bvd supports lots of things, but not containers */
2252 if (level == LEVEL_CONTAINER)
2253 return 0;
2254 /* We must have the container info already read in. */
2255 if (!ddf)
2256 return 0;
2257
2258 if (!dev) {
2259 /* General test: make sure there is space for
2260 * 'raiddisks' device extents of size 'size'.
2261 */
2262 unsigned long long minsize = size;
2263 int dcnt = 0;
2264 if (minsize == 0)
2265 minsize = 8;
2266 for (dl = ddf->dlist; dl ; dl = dl->next)
2267 {
2268 int found = 0;
2269
2270 i = 0;
2271 e = get_extents(ddf, dl);
2272 if (!e) continue;
2273 do {
2274 unsigned long long esize;
2275 esize = e[i].start - pos;
2276 if (esize >= minsize)
2277 found = 1;
2278 pos = e[i].start + e[i].size;
2279 i++;
2280 } while (e[i-1].size);
2281 if (found)
2282 dcnt++;
2283 free(e);
2284 }
2285 if (dcnt < raiddisks) {
2286 fprintf(stderr, Name ": Not enough devices with space "
2287 "for this array (%d < %d)\n",
2288 dcnt, raiddisks);
2289 return 0;
2290 }
2291 return 1;
2292 }
2293 /* This device must be a member of the set */
2294 if (stat(dev, &stb) < 0)
2295 return 0;
2296 if ((S_IFMT & stb.st_mode) != S_IFBLK)
2297 return 0;
2298 for (dl = ddf->dlist ; dl ; dl = dl->next) {
2299 if (dl->major == major(stb.st_rdev) &&
2300 dl->minor == minor(stb.st_rdev))
2301 break;
2302 }
2303 if (!dl) {
2304 fprintf(stderr, Name ": %s is not in the same DDF set\n",
2305 dev);
2306 return 0;
2307 }
2308 e = get_extents(ddf, dl);
2309 maxsize = 0;
2310 i = 0;
2311 if (e) do {
2312 unsigned long long esize;
2313 esize = e[i].start - pos;
2314 if (esize >= maxsize)
2315 maxsize = esize;
2316 pos = e[i].start + e[i].size;
2317 i++;
2318 } while (e[i-1].size);
2319 *freesize = maxsize;
2320 // FIXME here I am
2321
2322 return 1;
2323 }
2324 int validate_geometry_ddf_svd(struct supertype *st,
2325 int level, int layout, int raiddisks,
2326 int chunk, unsigned long long size,
2327 char *dev, unsigned long long *freesize)
2328 {
2329 /* dd/svd only supports striped, mirrored, concat, spanned... */
2330 if (level != LEVEL_LINEAR &&
2331 level != 0 &&
2332 level != 1)
2333 return 0;
2334 return 1;
2335 }
2336
2337
2338 static int load_super_ddf_all(struct supertype *st, int fd,
2339 void **sbp, char *devname, int keep_fd)
2340 {
2341 struct mdinfo *sra;
2342 struct ddf_super *super;
2343 struct mdinfo *sd, *best = NULL;
2344 int bestseq = 0;
2345 int seq;
2346 char nm[20];
2347 int dfd;
2348
2349 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
2350 if (!sra)
2351 return 1;
2352 if (sra->array.major_version != -1 ||
2353 sra->array.minor_version != -2 ||
2354 strcmp(sra->text_version, "ddf") != 0)
2355 return 1;
2356
2357 super = malloc(sizeof(*super));
2358 if (!super)
2359 return 1;
2360 memset(super, 0, sizeof(*super));
2361
2362 /* first, try each device, and choose the best ddf */
2363 for (sd = sra->devs ; sd ; sd = sd->next) {
2364 int rv;
2365 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2366 dfd = dev_open(nm, O_RDONLY);
2367 if (dfd < 0)
2368 return 2;
2369 rv = load_ddf_headers(dfd, super, NULL);
2370 close(dfd);
2371 if (rv == 0) {
2372 seq = __be32_to_cpu(super->active->seq);
2373 if (super->active->openflag)
2374 seq--;
2375 if (!best || seq > bestseq) {
2376 bestseq = seq;
2377 best = sd;
2378 }
2379 }
2380 }
2381 if (!best)
2382 return 1;
2383 /* OK, load this ddf */
2384 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2385 dfd = dev_open(nm, O_RDONLY);
2386 if (dfd < 0)
2387 return 1;
2388 load_ddf_headers(dfd, super, NULL);
2389 load_ddf_global(dfd, super, NULL);
2390 close(dfd);
2391 /* Now we need the device-local bits */
2392 for (sd = sra->devs ; sd ; sd = sd->next) {
2393 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2394 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2395 if (dfd < 0)
2396 return 2;
2397 seq = load_ddf_local(dfd, super, NULL, keep_fd);
2398 if (!keep_fd) close(dfd);
2399 }
2400 *sbp = super;
2401 if (st->ss == NULL) {
2402 st->ss = &super_ddf_container;
2403 st->minor_version = 0;
2404 st->max_devs = 512;
2405 st->container_dev = fd2devnum(fd);
2406 }
2407 return 0;
2408 }
2409 #endif
2410
2411
2412
2413 static struct mdinfo *container_content_ddf(struct supertype *st)
2414 {
2415 /* Given a container loaded by load_super_ddf_all,
2416 * extract information about all the arrays into
2417 * an mdinfo tree.
2418 *
2419 * For each vcl in conflist: create an mdinfo, fill it in,
2420 * then look for matching devices (phys_refnum) in dlist
2421 * and create appropriate device mdinfo.
2422 */
2423 struct ddf_super *ddf = st->sb;
2424 struct mdinfo *rest = NULL;
2425 struct vcl *vc;
2426
2427 for (vc = ddf->conflist ; vc ; vc=vc->next)
2428 {
2429 int i;
2430 struct mdinfo *this;
2431 this = malloc(sizeof(*this));
2432 memset(this, 0, sizeof(*this));
2433 this->next = rest;
2434 rest = this;
2435
2436 this->array.major_version = 1000;
2437 this->array.minor_version = 0;
2438 this->array.patch_version = 0;
2439 this->array.level = map_num1(ddf_level_num, vc->conf.prl);
2440 this->array.raid_disks =
2441 __be16_to_cpu(vc->conf.prim_elmnt_count);
2442 this->array.layout = rlq_to_layout(vc->conf.rlq, vc->conf.prl,
2443 this->array.raid_disks);
2444 this->array.md_minor = -1;
2445 this->array.ctime = DECADE +
2446 __be32_to_cpu(*(__u32*)(vc->conf.guid+16));
2447 this->array.utime = DECADE +
2448 __be32_to_cpu(vc->conf.timestamp);
2449 this->array.chunk_size = 512 << vc->conf.chunk_shift;
2450
2451 for (i=0; i < __be16_to_cpu(ddf->virt->populated_vdes); i++)
2452 if (memcmp(ddf->virt->entries[i].guid,
2453 vc->conf.guid, DDF_GUID_LEN) == 0)
2454 break;
2455 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
2456 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
2457 DDF_init_full) {
2458 this->array.state = 0;
2459 this->resync_start = 0;
2460 } else {
2461 this->array.state = 1;
2462 this->resync_start = ~0ULL;
2463 }
2464 memcpy(this->name, ddf->virt->entries[i].name, 32);
2465 this->name[33]=0;
2466
2467 memset(this->uuid, 0, sizeof(this->uuid));
2468 this->component_size = __be64_to_cpu(vc->conf.blocks);
2469 this->array.size = this->component_size / 2;
2470 this->container_member = i;
2471
2472 sprintf(this->text_version, "/%s/%d",
2473 devnum2devname(st->container_dev),
2474 this->container_member);
2475
2476
2477 for (i=0 ; i < ddf->mppe ; i++) {
2478 struct mdinfo *dev;
2479 struct dl *d;
2480
2481 if (vc->conf.phys_refnum[i] == 0xFFFFFFFF)
2482 continue;
2483
2484 this->array.working_disks++;
2485
2486 for (d = ddf->dlist; d ; d=d->next)
2487 if (d->disk.refnum == vc->conf.phys_refnum[i])
2488 break;
2489 if (d == NULL)
2490 break;
2491
2492 dev = malloc(sizeof(*dev));
2493 memset(dev, 0, sizeof(*dev));
2494 dev->next = this->devs;
2495 this->devs = dev;
2496
2497 dev->disk.number = __be32_to_cpu(d->disk.refnum);
2498 dev->disk.major = d->major;
2499 dev->disk.minor = d->minor;
2500 dev->disk.raid_disk = i;
2501 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
2502
2503 dev->events = __le32_to_cpu(ddf->primary.seq);
2504 dev->data_offset = vc->lba_offset[i];
2505 dev->component_size = __be64_to_cpu(vc->conf.blocks);
2506 if (d->devname)
2507 strcpy(dev->name, d->devname);
2508 }
2509 }
2510 return rest;
2511 }
2512
2513 static int init_zero_ddf(struct supertype *st,
2514 mdu_array_info_t *info,
2515 unsigned long long size, char *name,
2516 char *homehost, int *uuid)
2517 {
2518 st->sb = NULL;
2519 return 0;
2520 }
2521
2522 static int store_zero_ddf(struct supertype *st, int fd)
2523 {
2524 unsigned long long dsize;
2525 char buf[512];
2526 memset(buf, 0, 512);
2527
2528
2529 if (!get_dev_size(fd, NULL, &dsize))
2530 return 1;
2531
2532 lseek64(fd, dsize-512, 0);
2533 write(fd, buf, 512);
2534 return 0;
2535 }
2536
2537 static int compare_super_ddf(struct supertype *st, struct supertype *tst)
2538 {
2539 /*
2540 * return:
2541 * 0 same, or first was empty, and second was copied
2542 * 1 second had wrong number
2543 * 2 wrong uuid
2544 * 3 wrong other info
2545 */
2546 struct ddf_super *first = st->sb;
2547 struct ddf_super *second = tst->sb;
2548
2549 if (!first) {
2550 st->sb = tst->sb;
2551 tst->sb = NULL;
2552 return 0;
2553 }
2554
2555 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
2556 return 2;
2557
2558 /* FIXME should I look at anything else? */
2559 return 0;
2560 }
2561
2562 /*
2563 * A new array 'a' has been started which claims to be instance 'inst'
2564 * within container 'c'.
2565 * We need to confirm that the array matches the metadata in 'c' so
2566 * that we don't corrupt any metadata.
2567 */
2568 static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst)
2569 {
2570 fprintf(stderr, "ddf: open_new %s\n", inst);
2571 a->info.container_member = atoi(inst);
2572 return 0;
2573 }
2574
2575 /*
2576 * The array 'a' is to be marked clean in the metadata.
2577 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
2578 * clean up to the point (in sectors). If that cannot be recorded in the
2579 * metadata, then leave it as dirty.
2580 *
2581 * For DDF, we need to clear the DDF_state_inconsistent bit in the
2582 * !global! virtual_disk.virtual_entry structure.
2583 */
2584 static void ddf_set_array_state(struct active_array *a, int consistent)
2585 {
2586 struct ddf_super *ddf = a->container->sb;
2587 int inst = a->info.container_member;
2588 if (consistent)
2589 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
2590 else
2591 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
2592 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
2593 if (a->resync_start == ~0ULL)
2594 ddf->virt->entries[inst].init_state |= DDF_init_full;
2595 else if (a->resync_start == 0)
2596 ddf->virt->entries[inst].init_state |= DDF_init_not;
2597 else
2598 ddf->virt->entries[inst].init_state |= DDF_init_quick;
2599
2600 printf("ddf mark %s %llu\n", consistent?"clean":"dirty",
2601 a->resync_start);
2602 }
2603
2604 /*
2605 * The state of each disk is stored in the global phys_disk structure
2606 * in phys_disk.entries[n].state.
2607 * This makes various combinations awkward.
2608 * - When a device fails in any array, it must be failed in all arrays
2609 * that include a part of this device.
2610 * - When a component is rebuilding, we cannot include it officially in the
2611 * array unless this is the only array that uses the device.
2612 *
2613 * So: when transitioning:
2614 * Online -> failed, just set failed flag. monitor will propagate
2615 * spare -> online, the device might need to be added to the array.
2616 * spare -> failed, just set failed. Don't worry if in array or not.
2617 */
2618 static void ddf_set_disk(struct active_array *a, int n, int state)
2619 {
2620 struct ddf_super *ddf = a->container->sb;
2621 int inst = a->info.container_member;
2622 struct vd_config *vc = find_vdcr(ddf, inst);
2623 int pd = find_phys(ddf, vc->phys_refnum[n]);
2624 int i, st, working;
2625
2626 if (vc == NULL) {
2627 fprintf(stderr, "ddf: cannot find instance %d!!\n", inst);
2628 return;
2629 }
2630 if (pd < 0) {
2631 /* disk doesn't currently exist. If it is now in_sync,
2632 * insert it. */
2633 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
2634 /* Find dev 'n' in a->info->devs, determine the
2635 * ddf refnum, and set vc->phys_refnum and update
2636 * phys->entries[]
2637 */
2638 /* FIXME */
2639 }
2640 } else {
2641 if (state & DS_FAULTY)
2642 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
2643 if (state & DS_INSYNC) {
2644 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
2645 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
2646 }
2647 }
2648
2649 /* Now we need to check the state of the array and update
2650 * virtual_disk.entries[n].state.
2651 * It needs to be one of "optimal", "degraded", "failed".
2652 * I don't understand 'deleted' or 'missing'.
2653 */
2654 working = 0;
2655 for (i=0; i < a->info.array.raid_disks; i++) {
2656 pd = find_phys(ddf, vc->phys_refnum[i]);
2657 if (pd < 0)
2658 continue;
2659 st = ddf->phys->entries[pd].state;
2660 if ((state & (DDF_Online|DDF_Failed|DDF_Rebuilding))
2661 == DDF_Online)
2662 working++;
2663 }
2664 state = DDF_state_degraded;
2665 if (working == a->info.array.raid_disks)
2666 state = DDF_state_optimal;
2667 else switch(vc->prl) {
2668 case DDF_RAID0:
2669 case DDF_CONCAT:
2670 case DDF_JBOD:
2671 state = DDF_state_failed;
2672 break;
2673 case DDF_RAID1:
2674 if (working == 0)
2675 state = DDF_state_failed;
2676 break;
2677 case DDF_RAID4:
2678 case DDF_RAID5:
2679 if (working < a->info.array.raid_disks-1)
2680 state = DDF_state_failed;
2681 break;
2682 case DDF_RAID6:
2683 if (working < a->info.array.raid_disks-2)
2684 state = DDF_state_failed;
2685 else if (working == a->info.array.raid_disks-1)
2686 state = DDF_state_part_optimal;
2687 break;
2688 }
2689
2690 ddf->virt->entries[inst].state =
2691 (ddf->virt->entries[inst].state & ~DDF_state_mask)
2692 | state;
2693
2694 fprintf(stderr, "ddf: set_disk %d\n", n);
2695 }
2696
2697 static void ddf_sync_metadata(struct supertype *st)
2698 {
2699
2700 /*
2701 * Write all data to all devices.
2702 * Later, we might be able to track whether only local changes
2703 * have been made, or whether any global data has been changed,
2704 * but ddf is sufficiently weird that it probably always
2705 * changes global data ....
2706 */
2707 __write_init_super_ddf(st, 0);
2708 fprintf(stderr, "ddf: sync_metadata\n");
2709 }
2710
2711 static void ddf_process_update(struct supertype *st,
2712 struct metadata_update *update)
2713 {
2714 /* Apply this update to the metadata.
2715 * The first 4 bytes are a DDF_*_MAGIC which guides
2716 * our actions.
2717 * Possible update are:
2718 * DDF_PHYS_RECORDS_MAGIC
2719 * Add a new physical device. Changes to this record
2720 * only happen implicitly.
2721 * used_pdes is the device number.
2722 * DDF_VIRT_RECORDS_MAGIC
2723 * Add a new VD. Possibly also change the 'access' bits.
2724 * populated_vdes is the entry number.
2725 * DDF_VD_CONF_MAGIC
2726 * New or updated VD. the VIRT_RECORD must already
2727 * exist. For an update, phys_refnum and lba_offset
2728 * (at least) are updated, and the VD_CONF must
2729 * be written to precisely those devices listed with
2730 * a phys_refnum.
2731 * DDF_SPARE_ASSIGN_MAGIC
2732 * replacement Spare Assignment Record... but for which device?
2733 *
2734 * So, e.g.:
2735 * - to create a new array, we send a VIRT_RECORD and
2736 * a VD_CONF. Then assemble and start the array.
2737 * - to activate a spare we send a VD_CONF to add the phys_refnum
2738 * and offset. This will also mark the spare as active with
2739 * a spare-assignment record.
2740 */
2741 struct ddf_super *ddf = st->sb;
2742 __u32 *magic = (__u32*)update->buf;
2743 struct phys_disk *pd;
2744 struct virtual_disk *vd;
2745 struct vd_config *vc;
2746 struct vcl *vcl;
2747 struct dl *dl;
2748 int mppe;
2749 int ent;
2750
2751 switch (*magic) {
2752 case DDF_PHYS_RECORDS_MAGIC:
2753
2754 if (update->len != (sizeof(struct phys_disk) +
2755 sizeof(struct phys_disk_entry)))
2756 return;
2757 pd = (struct phys_disk*)update->buf;
2758
2759 ent = __be16_to_cpu(pd->used_pdes);
2760 if (ent >= __be16_to_cpu(ddf->phys->max_pdes))
2761 return;
2762 if (!all_ff(ddf->phys->entries[ent].guid))
2763 return;
2764 ddf->phys->entries[ent] = pd->entries[0];
2765 ddf->phys->used_pdes = __cpu_to_be16(1 +
2766 __be16_to_cpu(ddf->phys->used_pdes));
2767 break;
2768
2769 case DDF_VIRT_RECORDS_MAGIC:
2770
2771 if (update->len != (sizeof(struct virtual_disk) +
2772 sizeof(struct virtual_entry)))
2773 return;
2774 vd = (struct virtual_disk*)update->buf;
2775
2776 ent = __be16_to_cpu(vd->populated_vdes);
2777 if (ent >= __be16_to_cpu(ddf->virt->max_vdes))
2778 return;
2779 if (!all_ff(ddf->virt->entries[ent].guid))
2780 return;
2781 ddf->virt->entries[ent] = vd->entries[0];
2782 ddf->virt->populated_vdes = __cpu_to_be16(1 +
2783 __be16_to_cpu(ddf->virt->populated_vdes));
2784 break;
2785
2786 case DDF_VD_CONF_MAGIC:
2787
2788 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
2789 if (update->len != ddf->conf_rec_len)
2790 return;
2791 vc = (struct vd_config*)update->buf;
2792 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
2793 if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0)
2794 break;
2795 if (vcl) {
2796 /* An update, just copy the phys_refnum and lba_offset
2797 * fields
2798 */
2799 memcpy(vcl->conf.phys_refnum, vc->phys_refnum,
2800 mppe * (sizeof(__u32) + sizeof(__u64)));
2801 } else {
2802 /* A new VD_CONF */
2803 vcl = update->space;
2804 update->space = NULL;
2805 vcl->next = ddf->conflist;
2806 vcl->conf = *vc;
2807 vcl->lba_offset = (__u64*)
2808 &vcl->conf.phys_refnum[mppe];
2809 ddf->conflist = vcl;
2810 }
2811 /* Now make sure vlist is correct for each dl. */
2812 for (dl = ddf->dlist; dl; dl = dl->next) {
2813 int dn;
2814 int vn = 0;
2815 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
2816 for (dn=0; dn < ddf->mppe ; dn++)
2817 if (vcl->conf.phys_refnum[dn] ==
2818 dl->disk.refnum) {
2819 dl->vlist[vn++] = vcl;
2820 break;
2821 }
2822 while (vn < ddf->max_part)
2823 dl->vlist[vn++] = NULL;
2824 }
2825 break;
2826 case DDF_SPARE_ASSIGN_MAGIC:
2827 default: break;
2828 }
2829 }
2830
2831 struct superswitch super_ddf = {
2832 #ifndef MDASSEMBLE
2833 .examine_super = examine_super_ddf,
2834 .brief_examine_super = brief_examine_super_ddf,
2835 .detail_super = detail_super_ddf,
2836 .brief_detail_super = brief_detail_super_ddf,
2837 .validate_geometry = validate_geometry_ddf,
2838 #endif
2839 .match_home = match_home_ddf,
2840 .uuid_from_super= uuid_from_super_ddf,
2841 .getinfo_super = getinfo_super_ddf,
2842 .update_super = update_super_ddf,
2843
2844 .avail_size = avail_size_ddf,
2845
2846 .compare_super = compare_super_ddf,
2847
2848 .load_super = load_super_ddf,
2849 .init_super = init_zero_ddf,
2850 .store_super = store_zero_ddf,
2851 .free_super = free_super_ddf,
2852 .match_metadata_desc = match_metadata_desc_ddf,
2853 .getinfo_super_n = getinfo_super_n_container,
2854
2855
2856 .major = 1000,
2857 .swapuuid = 0,
2858 .external = 1,
2859
2860 /* for mdmon */
2861 .open_new = ddf_open_new,
2862 .set_array_state= ddf_set_array_state,
2863 .set_disk = ddf_set_disk,
2864 .sync_metadata = ddf_sync_metadata,
2865 .process_update = ddf_process_update,
2866
2867 };
2868
2869 /* Super_ddf_container is set by validate_geometry_ddf when given a
2870 * device that is not part of any array
2871 */
2872 struct superswitch super_ddf_container = {
2873 #ifndef MDASSEMBLE
2874 .validate_geometry = validate_geometry_ddf_container,
2875 .write_init_super = write_init_super_ddf,
2876 #endif
2877
2878 .load_super = load_super_ddf,
2879 .init_super = init_super_ddf,
2880 .add_to_super = add_to_super_ddf,
2881 .getinfo_super = getinfo_super_ddf,
2882
2883 .free_super = free_super_ddf,
2884
2885 .container_content = container_content_ddf,
2886 .getinfo_super_n = getinfo_super_n_container,
2887
2888 .major = 1000,
2889 .swapuuid = 0,
2890 .external = 1,
2891 };
2892
2893 struct superswitch super_ddf_bvd = {
2894 #ifndef MDASSEMBLE
2895 // .detail_super = detail_super_ddf_bvd,
2896 // .brief_detail_super = brief_detail_super_ddf_bvd,
2897 .validate_geometry = validate_geometry_ddf_bvd,
2898 .write_init_super = write_init_super_ddf,
2899 #endif
2900 .update_super = update_super_ddf,
2901 .init_super = init_super_ddf_bvd,
2902 .add_to_super = add_to_super_ddf_bvd,
2903 .getinfo_super = getinfo_super_ddf_bvd,
2904 .getinfo_super_n = getinfo_super_n_bvd,
2905
2906 .load_super = load_super_ddf,
2907 .free_super = free_super_ddf,
2908 .match_metadata_desc = match_metadata_desc_ddf_bvd,
2909
2910
2911 .major = 1001,
2912 .swapuuid = 0,
2913 .external = 2,
2914 };
2915
2916 struct superswitch super_ddf_svd = {
2917 #ifndef MDASSEMBLE
2918 // .detail_super = detail_super_ddf_svd,
2919 // .brief_detail_super = brief_detail_super_ddf_svd,
2920 .validate_geometry = validate_geometry_ddf_svd,
2921 #endif
2922 .update_super = update_super_ddf,
2923 .init_super = init_super_ddf,
2924
2925 .load_super = load_super_ddf,
2926 .free_super = free_super_ddf,
2927 .match_metadata_desc = match_metadata_desc_ddf_svd,
2928
2929 .major = 1002,
2930 .swapuuid = 0,
2931 .external = 2,
2932 };