]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-ddf.c
Fix write_init_super usage when hot-adding a spare
[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 memset(st, 0, sizeof(*st));
824 st->ss = &super_ddf;
825 st->max_devs = 512;
826 st->minor_version = 0;
827 st->sb = NULL;
828 return st;
829 }
830
831 static struct supertype *match_metadata_desc_ddf_bvd(char *arg)
832 {
833 struct supertype *st;
834 if (strcmp(arg, "ddf/bvd") != 0 &&
835 strcmp(arg, "bvd") != 0 &&
836 strcmp(arg, "default") != 0
837 )
838 return NULL;
839
840 st = malloc(sizeof(*st));
841 memset(st, 0, sizeof(*st));
842 st->ss = &super_ddf_bvd;
843 st->max_devs = 512;
844 st->minor_version = 0;
845 st->sb = NULL;
846 return st;
847 }
848 static struct supertype *match_metadata_desc_ddf_svd(char *arg)
849 {
850 struct supertype *st;
851 if (strcmp(arg, "ddf/svd") != 0 &&
852 strcmp(arg, "svd") != 0 &&
853 strcmp(arg, "default") != 0
854 )
855 return NULL;
856
857 st = malloc(sizeof(*st));
858 memset(st, 0, sizeof(*st));
859 st->ss = &super_ddf_svd;
860 st->max_devs = 512;
861 st->minor_version = 0;
862 st->sb = NULL;
863 return st;
864 }
865
866 #ifndef MDASSEMBLE
867
868 static mapping_t ddf_state[] = {
869 { "Optimal", 0},
870 { "Degraded", 1},
871 { "Deleted", 2},
872 { "Missing", 3},
873 { "Failed", 4},
874 { "Partially Optimal", 5},
875 { "-reserved-", 6},
876 { "-reserved-", 7},
877 { NULL, 0}
878 };
879
880 static mapping_t ddf_init_state[] = {
881 { "Not Initialised", 0},
882 { "QuickInit in Progress", 1},
883 { "Fully Initialised", 2},
884 { "*UNKNOWN*", 3},
885 { NULL, 0}
886 };
887 static mapping_t ddf_access[] = {
888 { "Read/Write", 0},
889 { "Reserved", 1},
890 { "Read Only", 2},
891 { "Blocked (no access)", 3},
892 { NULL ,0}
893 };
894
895 static mapping_t ddf_level[] = {
896 { "RAID0", DDF_RAID0},
897 { "RAID1", DDF_RAID1},
898 { "RAID3", DDF_RAID3},
899 { "RAID4", DDF_RAID4},
900 { "RAID5", DDF_RAID5},
901 { "RAID1E",DDF_RAID1E},
902 { "JBOD", DDF_JBOD},
903 { "CONCAT",DDF_CONCAT},
904 { "RAID5E",DDF_RAID5E},
905 { "RAID5EE",DDF_RAID5EE},
906 { "RAID6", DDF_RAID6},
907 { NULL, 0}
908 };
909 static mapping_t ddf_sec_level[] = {
910 { "Striped", DDF_2STRIPED},
911 { "Mirrored", DDF_2MIRRORED},
912 { "Concat", DDF_2CONCAT},
913 { "Spanned", DDF_2SPANNED},
914 { NULL, 0}
915 };
916 #endif
917
918 struct num_mapping {
919 int num1, num2;
920 };
921 static struct num_mapping ddf_level_num[] = {
922 { DDF_RAID0, 0 },
923 { DDF_RAID1, 1 },
924 { DDF_RAID3, LEVEL_UNSUPPORTED },
925 { DDF_RAID4, 4 },
926 { DDF_RAID5, 5 },
927 { DDF_RAID1E, LEVEL_UNSUPPORTED },
928 { DDF_JBOD, LEVEL_UNSUPPORTED },
929 { DDF_CONCAT, LEVEL_LINEAR },
930 { DDF_RAID5E, LEVEL_UNSUPPORTED },
931 { DDF_RAID5EE, LEVEL_UNSUPPORTED },
932 { DDF_RAID6, 6},
933 { MAXINT, MAXINT }
934 };
935
936 static int map_num1(struct num_mapping *map, int num)
937 {
938 int i;
939 for (i=0 ; map[i].num1 != MAXINT; i++)
940 if (map[i].num1 == num)
941 break;
942 return map[i].num2;
943 }
944
945 #ifndef MDASSEMBLE
946 static void print_guid(char *guid, int tstamp)
947 {
948 /* A GUIDs are part (or all) ASCII and part binary.
949 * They tend to be space padded.
950 * We ignore trailing spaces and print numbers
951 * <0x20 and >=0x7f as \xXX
952 * Some GUIDs have a time stamp in bytes 16-19.
953 * We print that if appropriate
954 */
955 int l = DDF_GUID_LEN;
956 int i;
957 while (l && guid[l-1] == ' ')
958 l--;
959 for (i=0 ; i<l ; i++) {
960 if (guid[i] >= 0x20 && guid[i] < 0x7f)
961 fputc(guid[i], stdout);
962 else
963 fprintf(stdout, "\\x%02x", guid[i]&255);
964 }
965 if (tstamp) {
966 time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE;
967 char tbuf[100];
968 struct tm *tm;
969 tm = localtime(&then);
970 strftime(tbuf, 100, " (%D %T)",tm);
971 fputs(tbuf, stdout);
972 }
973 }
974
975 static void examine_vd(int n, struct ddf_super *sb, char *guid)
976 {
977 int crl = sb->conf_rec_len;
978 struct vcl *vcl;
979
980 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
981 struct vd_config *vc = &vcl->conf;
982
983 if (calc_crc(vc, crl*512) != vc->crc)
984 continue;
985 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
986 continue;
987
988 /* Ok, we know about this VD, let's give more details */
989 printf(" Raid Devices[%d] : %d\n", n,
990 __be16_to_cpu(vc->prim_elmnt_count));
991 printf(" Chunk Size[%d] : %d sectors\n", n,
992 1 << vc->chunk_shift);
993 printf(" Raid Level[%d] : %s\n", n,
994 map_num(ddf_level, vc->prl)?:"-unknown-");
995 if (vc->sec_elmnt_count != 1) {
996 printf(" Secondary Position[%d] : %d of %d\n", n,
997 vc->sec_elmnt_seq, vc->sec_elmnt_count);
998 printf(" Secondary Level[%d] : %s\n", n,
999 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
1000 }
1001 printf(" Device Size[%d] : %llu\n", n,
1002 __be64_to_cpu(vc->blocks)/2);
1003 printf(" Array Size[%d] : %llu\n", n,
1004 __be64_to_cpu(vc->array_blocks)/2);
1005 }
1006 }
1007
1008 static void examine_vds(struct ddf_super *sb)
1009 {
1010 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
1011 int i;
1012 printf(" Virtual Disks : %d\n", cnt);
1013
1014 for (i=0; i<cnt; i++) {
1015 struct virtual_entry *ve = &sb->virt->entries[i];
1016 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
1017 printf("\n");
1018 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
1019 printf(" state[%d] : %s, %s%s\n", i,
1020 map_num(ddf_state, ve->state & 7),
1021 (ve->state & 8) ? "Morphing, ": "",
1022 (ve->state & 16)? "Not Consistent" : "Consistent");
1023 printf(" init state[%d] : %s\n", i,
1024 map_num(ddf_init_state, ve->init_state&3));
1025 printf(" access[%d] : %s\n", i,
1026 map_num(ddf_access, (ve->init_state>>6) & 3));
1027 printf(" Name[%d] : %.16s\n", i, ve->name);
1028 examine_vd(i, sb, ve->guid);
1029 }
1030 if (cnt) printf("\n");
1031 }
1032
1033 static void examine_pds(struct ddf_super *sb)
1034 {
1035 int cnt = __be16_to_cpu(sb->phys->used_pdes);
1036 int i;
1037 struct dl *dl;
1038 printf(" Physical Disks : %d\n", cnt);
1039
1040 for (i=0 ; i<cnt ; i++) {
1041 struct phys_disk_entry *pd = &sb->phys->entries[i];
1042 int type = __be16_to_cpu(pd->type);
1043 int state = __be16_to_cpu(pd->state);
1044
1045 printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1046 printf("\n");
1047 printf(" ref[%d] : %08x\n", i,
1048 __be32_to_cpu(pd->refnum));
1049 printf(" mode[%d] : %s%s%s%s%s\n", i,
1050 (type&2) ? "active":"",
1051 (type&4) ? "Global Spare":"",
1052 (type&8) ? "spare" : "",
1053 (type&16)? ", foreign" : "",
1054 (type&32)? "pass-through" : "");
1055 printf(" state[%d] : %s%s%s%s%s%s%s\n", i,
1056 (state&1)? "Online": "Offline",
1057 (state&2)? ", Failed": "",
1058 (state&4)? ", Rebuilding": "",
1059 (state&8)? ", in-transition": "",
1060 (state&16)? ", SMART errors": "",
1061 (state&32)? ", Unrecovered Read Errors": "",
1062 (state&64)? ", Missing" : "");
1063 printf(" Avail Size[%d] : %llu K\n", i,
1064 __be64_to_cpu(pd->config_size)>>1);
1065 for (dl = sb->dlist; dl ; dl = dl->next) {
1066 if (dl->disk.refnum == pd->refnum) {
1067 char *dv = map_dev(dl->major, dl->minor, 0);
1068 if (dv)
1069 printf(" Device[%d] : %s\n",
1070 i, dv);
1071 }
1072 }
1073 printf("\n");
1074 }
1075 }
1076
1077 static void examine_super_ddf(struct supertype *st, char *homehost)
1078 {
1079 struct ddf_super *sb = st->sb;
1080
1081 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1082 printf(" Version : %.8s\n", sb->anchor.revision);
1083 printf("Controller GUID : "); print_guid(sb->controller.guid, 0);
1084 printf("\n");
1085 printf(" Container GUID : "); print_guid(sb->anchor.guid, 1);
1086 printf("\n");
1087 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1088 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1089 ?"yes" : "no");
1090 examine_vds(sb);
1091 examine_pds(sb);
1092 }
1093
1094 static void brief_examine_super_ddf(struct supertype *st)
1095 {
1096 /* We just write a generic DDF ARRAY entry
1097 * The uuid is all hex, 6 groups of 4 bytes
1098 */
1099 struct ddf_super *ddf = st->sb;
1100 int i;
1101 printf("ARRAY /dev/ddf UUID=");
1102 for (i = 0; i < DDF_GUID_LEN; i++) {
1103 printf("%02x", ddf->anchor.guid[i]);
1104 if ((i&3) == 0 && i != 0)
1105 printf(":");
1106 }
1107 printf("\n");
1108 }
1109
1110 static void detail_super_ddf(struct supertype *st, char *homehost)
1111 {
1112 /* FIXME later
1113 * Could print DDF GUID
1114 * Need to find which array
1115 * If whole, briefly list all arrays
1116 * If one, give name
1117 */
1118 }
1119
1120 static void brief_detail_super_ddf(struct supertype *st)
1121 {
1122 /* FIXME I really need to know which array we are detailing.
1123 * Can that be stored in ddf_super??
1124 */
1125 // struct ddf_super *ddf = st->sb;
1126 }
1127
1128
1129 #endif
1130
1131 static int match_home_ddf(struct supertype *st, char *homehost)
1132 {
1133 /* It matches 'this' host if the controller is a
1134 * Linux-MD controller with vendor_data matching
1135 * the hostname
1136 */
1137 struct ddf_super *ddf = st->sb;
1138 int len = strlen(homehost);
1139
1140 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1141 len < sizeof(ddf->controller.vendor_data) &&
1142 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1143 ddf->controller.vendor_data[len] == 0);
1144 }
1145
1146 static struct vd_config *find_vdcr(struct ddf_super *ddf, int inst)
1147 {
1148 struct vcl *v;
1149 if (inst < 0 || inst > __be16_to_cpu(ddf->virt->populated_vdes))
1150 return NULL;
1151 for (v = ddf->conflist; v; v = v->next)
1152 if (memcmp(v->conf.guid,
1153 ddf->virt->entries[inst].guid,
1154 DDF_GUID_LEN) == 0)
1155 return &v->conf;
1156 return NULL;
1157 }
1158
1159 static int find_phys(struct ddf_super *ddf, __u32 phys_refnum)
1160 {
1161 /* Find the entry in phys_disk which has the given refnum
1162 * and return it's index
1163 */
1164 int i;
1165 for (i=0; i < __be16_to_cpu(ddf->phys->max_pdes); i++)
1166 if (ddf->phys->entries[i].refnum == phys_refnum)
1167 return i;
1168 return -1;
1169 }
1170
1171 static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1172 {
1173 /* The uuid returned here is used for:
1174 * uuid to put into bitmap file (Create, Grow)
1175 * uuid for backup header when saving critical section (Grow)
1176 * comparing uuids when re-adding a device into an array
1177 * For each of these we can make do with a truncated
1178 * or hashed uuid rather than the original, as long as
1179 * everyone agrees.
1180 * In each case the uuid required is that of the data-array,
1181 * not the device-set.
1182 * In the case of SVD we assume the BVD is of interest,
1183 * though that might be the case if a bitmap were made for
1184 * a mirrored SVD - worry about that later.
1185 * So we need to find the VD configuration record for the
1186 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1187 * The first 16 bytes of the sha1 of these is used.
1188 */
1189 struct ddf_super *ddf = st->sb;
1190 struct vd_config *vd = find_vdcr(ddf, ddf->conf_num);
1191
1192 if (!vd)
1193 memset(uuid, 0, sizeof (uuid));
1194 else {
1195 char buf[20];
1196 struct sha1_ctx ctx;
1197 sha1_init_ctx(&ctx);
1198 sha1_process_bytes(&vd->guid, DDF_GUID_LEN, &ctx);
1199 if (vd->sec_elmnt_count > 1)
1200 sha1_process_bytes(&vd->sec_elmnt_seq, 1, &ctx);
1201 sha1_finish_ctx(&ctx, buf);
1202 memcpy(uuid, buf, sizeof(uuid));
1203 }
1204 }
1205
1206 static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
1207 {
1208 struct ddf_super *ddf = st->sb;
1209 int i;
1210
1211 info->array.major_version = 1000;
1212 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1213 info->array.patch_version = 0;
1214 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1215 info->array.level = LEVEL_CONTAINER;
1216 info->array.layout = 0;
1217 info->array.md_minor = -1;
1218 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)
1219 (ddf->anchor.guid+16));
1220 info->array.utime = 0;
1221 info->array.chunk_size = 0;
1222
1223 // info->data_offset = ???;
1224 // info->component_size = ???;
1225
1226 info->disk.major = 0;
1227 info->disk.minor = 0;
1228 if (ddf->dlist) {
1229 info->disk.number = __be32_to_cpu(ddf->dlist->disk.refnum);
1230 info->disk.raid_disk = -1;
1231 for (i = 0; i < __be16_to_cpu(ddf->phys->max_pdes) ; i++)
1232 if (ddf->phys->entries[i].refnum ==
1233 ddf->dlist->disk.refnum) {
1234 info->disk.raid_disk = i;
1235 break;
1236 }
1237 } else {
1238 info->disk.number = -1;
1239 // info->disk.raid_disk = find refnum in the table and use index;
1240 }
1241 info->disk.state = (1 << MD_DISK_SYNC);
1242
1243 info->reshape_active = 0;
1244
1245 strcpy(info->text_version, "ddf");
1246
1247 // uuid_from_super_ddf(info->uuid, sbv);
1248
1249 // info->name[] ?? ;
1250 }
1251
1252 static void getinfo_super_n_container(struct supertype *st, struct mdinfo *info)
1253 {
1254 /* just need offset and size */
1255 struct ddf_super *ddf = st->sb;
1256 int n = info->disk.number;
1257
1258 info->data_offset = __be64_to_cpu(ddf->phys->entries[n].config_size);
1259 info->component_size = 32*1024*1024 / 512;
1260 }
1261
1262 static int rlq_to_layout(int rlq, int prl, int raiddisks);
1263
1264 static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
1265 {
1266 struct ddf_super *ddf = st->sb;
1267 struct vd_config *vd = find_vdcr(ddf, info->container_member);
1268
1269 /* FIXME this returns BVD info - what if we want SVD ?? */
1270
1271 info->array.major_version = 1000;
1272 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1273 info->array.patch_version = 0;
1274 info->array.raid_disks = __be16_to_cpu(vd->prim_elmnt_count);
1275 info->array.level = map_num1(ddf_level_num, vd->prl);
1276 info->array.layout = rlq_to_layout(vd->rlq, vd->prl,
1277 info->array.raid_disks);
1278 info->array.md_minor = -1;
1279 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)(vd->guid+16));
1280 info->array.utime = DECADE + __be32_to_cpu(vd->timestamp);
1281 info->array.chunk_size = 512 << vd->chunk_shift;
1282
1283 // info->data_offset = ???;
1284 // info->component_size = ???;
1285
1286 info->disk.major = 0;
1287 info->disk.minor = 0;
1288 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1289 // info->disk.raid_disk = find refnum in the table and use index;
1290 // info->disk.state = ???;
1291
1292 info->resync_start = 0;
1293 if (!(ddf->virt->entries[info->container_member].state
1294 & DDF_state_inconsistent) &&
1295 (ddf->virt->entries[info->container_member].init_state
1296 & DDF_initstate_mask)
1297 == DDF_init_full)
1298 info->resync_start = ~0ULL;
1299
1300 uuid_from_super_ddf(st, info->uuid);
1301
1302 sprintf(info->text_version, "/%s/%d",
1303 devnum2devname(st->container_dev),
1304 info->container_member);
1305
1306 // info->name[] ?? ;
1307 }
1308
1309 static void getinfo_super_n_bvd(struct supertype *st, struct mdinfo *info)
1310 {
1311 /* Find the particular details for info->disk.raid_disk.
1312 * This includes data_offset, component_size,
1313 */
1314 struct ddf_super *ddf = st->sb;
1315 __u64 *lba_offset = ddf->newconf->lba_offset;
1316 struct vd_config *conf = &ddf->newconf->conf;
1317 info->data_offset = __be64_to_cpu(lba_offset[info->disk.raid_disk]);
1318 info->component_size = __be64_to_cpu(conf->blocks);
1319 }
1320
1321 static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1322 char *update,
1323 char *devname, int verbose,
1324 int uuid_set, char *homehost)
1325 {
1326 /* For 'assemble' and 'force' we need to return non-zero if any
1327 * change was made. For others, the return value is ignored.
1328 * Update options are:
1329 * force-one : This device looks a bit old but needs to be included,
1330 * update age info appropriately.
1331 * assemble: clear any 'faulty' flag to allow this device to
1332 * be assembled.
1333 * force-array: Array is degraded but being forced, mark it clean
1334 * if that will be needed to assemble it.
1335 *
1336 * newdev: not used ????
1337 * grow: Array has gained a new device - this is currently for
1338 * linear only
1339 * resync: mark as dirty so a resync will happen.
1340 * uuid: Change the uuid of the array to match watch is given
1341 * homehost: update the recorded homehost
1342 * name: update the name - preserving the homehost
1343 * _reshape_progress: record new reshape_progress position.
1344 *
1345 * Following are not relevant for this version:
1346 * sparc2.2 : update from old dodgey metadata
1347 * super-minor: change the preferred_minor number
1348 * summaries: update redundant counters.
1349 */
1350 int rv = 0;
1351 // struct ddf_super *ddf = st->sb;
1352 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1353 // struct virtual_entry *ve = find_ve(ddf);
1354
1355
1356 /* we don't need to handle "force-*" or "assemble" as
1357 * there is no need to 'trick' the kernel. We the metadata is
1358 * first updated to activate the array, all the implied modifications
1359 * will just happen.
1360 */
1361
1362 if (strcmp(update, "grow") == 0) {
1363 /* FIXME */
1364 }
1365 if (strcmp(update, "resync") == 0) {
1366 // info->resync_checkpoint = 0;
1367 }
1368 /* We ignore UUID updates as they make even less sense
1369 * with DDF
1370 */
1371 if (strcmp(update, "homehost") == 0) {
1372 /* homehost is stored in controller->vendor_data,
1373 * or it is when we are the vendor
1374 */
1375 // if (info->vendor_is_local)
1376 // strcpy(ddf->controller.vendor_data, homehost);
1377 }
1378 if (strcmp(update, "name") == 0) {
1379 /* name is stored in virtual_entry->name */
1380 // memset(ve->name, ' ', 16);
1381 // strncpy(ve->name, info->name, 16);
1382 }
1383 if (strcmp(update, "_reshape_progress") == 0) {
1384 /* We don't support reshape yet */
1385 }
1386
1387 // update_all_csum(ddf);
1388
1389 return rv;
1390 }
1391
1392 static void make_header_guid(char *guid)
1393 {
1394 __u32 stamp;
1395 int rfd;
1396 /* Create a DDF Header of Virtual Disk GUID */
1397
1398 /* 24 bytes of fiction required.
1399 * first 8 are a 'vendor-id' - "Linux-MD"
1400 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1401 * Remaining 8 random number plus timestamp
1402 */
1403 memcpy(guid, T10, sizeof(T10));
1404 stamp = __cpu_to_be32(0xdeadbeef);
1405 memcpy(guid+8, &stamp, 4);
1406 stamp = __cpu_to_be32(0);
1407 memcpy(guid+12, &stamp, 4);
1408 stamp = __cpu_to_be32(time(0) - DECADE);
1409 memcpy(guid+16, &stamp, 4);
1410 rfd = open("/dev/urandom", O_RDONLY);
1411 if (rfd < 0 || read(rfd, &stamp, 4) != 4)
1412 stamp = random();
1413 memcpy(guid+20, &stamp, 4);
1414 if (rfd >= 0) close(rfd);
1415 }
1416 static int init_super_ddf(struct supertype *st,
1417 mdu_array_info_t *info,
1418 unsigned long long size, char *name, char *homehost,
1419 int *uuid)
1420 {
1421 /* This is primarily called by Create when creating a new array.
1422 * We will then get add_to_super called for each component, and then
1423 * write_init_super called to write it out to each device.
1424 * For DDF, Create can create on fresh devices or on a pre-existing
1425 * array.
1426 * To create on a pre-existing array a different method will be called.
1427 * This one is just for fresh drives.
1428 *
1429 * We need to create the entire 'ddf' structure which includes:
1430 * DDF headers - these are easy.
1431 * Controller data - a Sector describing this controller .. not that
1432 * this is a controller exactly.
1433 * Physical Disk Record - one entry per device, so
1434 * leave plenty of space.
1435 * Virtual Disk Records - again, just leave plenty of space.
1436 * This just lists VDs, doesn't give details
1437 * Config records - describes the VDs that use this disk
1438 * DiskData - describes 'this' device.
1439 * BadBlockManagement - empty
1440 * Diag Space - empty
1441 * Vendor Logs - Could we put bitmaps here?
1442 *
1443 */
1444 struct ddf_super *ddf;
1445 char hostname[17];
1446 int hostlen;
1447 int max_phys_disks, max_virt_disks;
1448 unsigned long long sector;
1449 int clen;
1450 int i;
1451 int pdsize, vdsize;
1452 struct phys_disk *pd;
1453 struct virtual_disk *vd;
1454
1455 ddf = malloc(sizeof(*ddf));
1456 memset(ddf, 0, sizeof(*ddf));
1457 ddf->dlist = NULL; /* no physical disks yet */
1458 ddf->conflist = NULL; /* No virtual disks yet */
1459
1460 /* At least 32MB *must* be reserved for the ddf. So let's just
1461 * start 32MB from the end, and put the primary header there.
1462 * Don't do secondary for now.
1463 * We don't know exactly where that will be yet as it could be
1464 * different on each device. To just set up the lengths.
1465 *
1466 */
1467
1468 ddf->anchor.magic = DDF_HEADER_MAGIC;
1469 make_header_guid(ddf->anchor.guid);
1470
1471 memcpy(ddf->anchor.revision, DDF_REVISION, 8);
1472 ddf->anchor.seq = __cpu_to_be32(1);
1473 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
1474 ddf->anchor.openflag = 0xFF;
1475 ddf->anchor.foreignflag = 0;
1476 ddf->anchor.enforcegroups = 0; /* Is this best?? */
1477 ddf->anchor.pad0 = 0xff;
1478 memset(ddf->anchor.pad1, 0xff, 12);
1479 memset(ddf->anchor.header_ext, 0xff, 32);
1480 ddf->anchor.primary_lba = ~(__u64)0;
1481 ddf->anchor.secondary_lba = ~(__u64)0;
1482 ddf->anchor.type = DDF_HEADER_ANCHOR;
1483 memset(ddf->anchor.pad2, 0xff, 3);
1484 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
1485 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
1486 of 32M reserved.. */
1487 max_phys_disks = 1023; /* Should be enough */
1488 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
1489 max_virt_disks = 255;
1490 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
1491 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
1492 ddf->max_part = 64;
1493 ddf->conf_rec_len = 1 + 256 * 12 / 512;
1494 ddf->anchor.config_record_len = __cpu_to_be16(ddf->conf_rec_len);
1495 ddf->anchor.max_primary_element_entries = __cpu_to_be16(256);
1496 ddf->mppe = 256;
1497 memset(ddf->anchor.pad3, 0xff, 54);
1498
1499 /* controller sections is one sector long immediately
1500 * after the ddf header */
1501 sector = 1;
1502 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
1503 ddf->anchor.controller_section_length = __cpu_to_be32(1);
1504 sector += 1;
1505
1506 /* phys is 8 sectors after that */
1507 pdsize = ROUND_UP(sizeof(struct phys_disk) +
1508 sizeof(struct phys_disk_entry)*max_phys_disks,
1509 512);
1510 switch(pdsize/512) {
1511 case 2: case 8: case 32: case 128: case 512: break;
1512 default: abort();
1513 }
1514 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
1515 ddf->anchor.phys_section_length =
1516 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
1517 sector += pdsize/512;
1518
1519 /* virt is another 32 sectors */
1520 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
1521 sizeof(struct virtual_entry) * max_virt_disks,
1522 512);
1523 switch(vdsize/512) {
1524 case 2: case 8: case 32: case 128: case 512: break;
1525 default: abort();
1526 }
1527 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
1528 ddf->anchor.virt_section_length =
1529 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
1530 sector += vdsize/512;
1531
1532 clen = (1 + 256*12/512) * (64+1);
1533 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
1534 ddf->anchor.config_section_length = __cpu_to_be32(clen);
1535 sector += clen;
1536
1537 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
1538 ddf->anchor.data_section_length = __cpu_to_be32(1);
1539 sector += 1;
1540
1541 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
1542 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
1543 ddf->anchor.diag_space_length = __cpu_to_be32(0);
1544 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
1545 ddf->anchor.vendor_length = __cpu_to_be32(0);
1546 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
1547
1548 memset(ddf->anchor.pad4, 0xff, 256);
1549
1550 memcpy(&ddf->primary, &ddf->anchor, 512);
1551 memcpy(&ddf->secondary, &ddf->anchor, 512);
1552
1553 ddf->primary.openflag = 1; /* I guess.. */
1554 ddf->primary.type = DDF_HEADER_PRIMARY;
1555
1556 ddf->secondary.openflag = 1; /* I guess.. */
1557 ddf->secondary.type = DDF_HEADER_SECONDARY;
1558
1559 ddf->active = &ddf->primary;
1560
1561 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
1562
1563 /* 24 more bytes of fiction required.
1564 * first 8 are a 'vendor-id' - "Linux-MD"
1565 * Remaining 16 are serial number.... maybe a hostname would do?
1566 */
1567 memcpy(ddf->controller.guid, T10, sizeof(T10));
1568 gethostname(hostname, sizeof(hostname));
1569 hostname[sizeof(hostname) - 1] = 0;
1570 hostlen = strlen(hostname);
1571 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
1572 for (i = strlen(T10) ; i+hostlen < 24; i++)
1573 ddf->controller.guid[i] = ' ';
1574
1575 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
1576 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
1577 ddf->controller.type.sub_vendor_id = 0;
1578 ddf->controller.type.sub_device_id = 0;
1579 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
1580 memset(ddf->controller.pad, 0xff, 8);
1581 memset(ddf->controller.vendor_data, 0xff, 448);
1582
1583 pd = ddf->phys = malloc(pdsize);
1584 ddf->pdsize = pdsize;
1585
1586 memset(pd, 0xff, pdsize);
1587 memset(pd, 0, sizeof(*pd));
1588 pd->magic = DDF_PHYS_DATA_MAGIC;
1589 pd->used_pdes = __cpu_to_be16(0);
1590 pd->max_pdes = __cpu_to_be16(max_phys_disks);
1591 memset(pd->pad, 0xff, 52);
1592
1593 vd = ddf->virt = malloc(vdsize);
1594 ddf->vdsize = vdsize;
1595 memset(vd, 0, vdsize);
1596 vd->magic = DDF_VIRT_RECORDS_MAGIC;
1597 vd->populated_vdes = __cpu_to_be16(0);
1598 vd->max_vdes = __cpu_to_be16(max_virt_disks);
1599 memset(vd->pad, 0xff, 52);
1600
1601 for (i=0; i<max_virt_disks; i++)
1602 memset(&vd->entries[i], 0xff, sizeof(struct virtual_entry));
1603
1604 st->sb = ddf;
1605 return 1;
1606 }
1607
1608 static int all_ff(char *guid)
1609 {
1610 int i;
1611 for (i = 0; i < DDF_GUID_LEN; i++)
1612 if (guid[i] != (char)0xff)
1613 return 0;
1614 return 1;
1615 }
1616 static int chunk_to_shift(int chunksize)
1617 {
1618 return ffs(chunksize/512)-1;
1619 }
1620
1621 static int level_to_prl(int level)
1622 {
1623 switch (level) {
1624 case LEVEL_LINEAR: return DDF_CONCAT;
1625 case 0: return DDF_RAID0;
1626 case 1: return DDF_RAID1;
1627 case 4: return DDF_RAID4;
1628 case 5: return DDF_RAID5;
1629 case 6: return DDF_RAID6;
1630 default: return -1;
1631 }
1632 }
1633 static int layout_to_rlq(int level, int layout, int raiddisks)
1634 {
1635 switch(level) {
1636 case 0:
1637 return DDF_RAID0_SIMPLE;
1638 case 1:
1639 switch(raiddisks) {
1640 case 2: return DDF_RAID1_SIMPLE;
1641 case 3: return DDF_RAID1_MULTI;
1642 default: return -1;
1643 }
1644 case 4:
1645 switch(layout) {
1646 case 0: return DDF_RAID4_N;
1647 }
1648 break;
1649 case 5:
1650 case 6:
1651 switch(layout) {
1652 case ALGORITHM_LEFT_ASYMMETRIC:
1653 return DDF_RAID5_N_RESTART;
1654 case ALGORITHM_RIGHT_ASYMMETRIC:
1655 return DDF_RAID5_0_RESTART;
1656 case ALGORITHM_LEFT_SYMMETRIC:
1657 return DDF_RAID5_N_CONTINUE;
1658 case ALGORITHM_RIGHT_SYMMETRIC:
1659 return -1; /* not mentioned in standard */
1660 }
1661 }
1662 return -1;
1663 }
1664
1665 static int rlq_to_layout(int rlq, int prl, int raiddisks)
1666 {
1667 switch(prl) {
1668 case DDF_RAID0:
1669 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1670 case DDF_RAID1:
1671 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1672 on raiddisks*/
1673 case DDF_RAID4:
1674 switch(rlq) {
1675 case DDF_RAID4_N:
1676 return 0;
1677 default:
1678 /* not supported */
1679 return -1; /* FIXME this isn't checked */
1680 }
1681 case DDF_RAID5:
1682 case DDF_RAID6:
1683 switch(rlq) {
1684 case DDF_RAID5_N_RESTART:
1685 return ALGORITHM_LEFT_ASYMMETRIC;
1686 case DDF_RAID5_0_RESTART:
1687 return ALGORITHM_RIGHT_ASYMMETRIC;
1688 case DDF_RAID5_N_CONTINUE:
1689 return ALGORITHM_LEFT_SYMMETRIC;
1690 default:
1691 return -1;
1692 }
1693 }
1694 return -1;
1695 }
1696
1697 static int init_super_ddf_bvd(struct supertype *st,
1698 mdu_array_info_t *info,
1699 unsigned long long size,
1700 char *name, char *homehost,
1701 int *uuid)
1702 {
1703 /* We are creating a BVD inside a pre-existing container.
1704 * so st->sb is already set.
1705 * We need to create a new vd_config and a new virtual_entry
1706 */
1707 struct ddf_super *ddf = st->sb;
1708 int venum;
1709 struct virtual_entry *ve;
1710 struct vcl *vcl;
1711 struct vd_config *vc;
1712
1713 if (__be16_to_cpu(ddf->virt->populated_vdes)
1714 >= __be16_to_cpu(ddf->virt->max_vdes)) {
1715 fprintf(stderr, Name": This ddf already has the "
1716 "maximum of %d virtual devices\n",
1717 __be16_to_cpu(ddf->virt->max_vdes));
1718 return 0;
1719 }
1720
1721 for (venum = 0; venum < __be16_to_cpu(ddf->virt->max_vdes); venum++)
1722 if (all_ff(ddf->virt->entries[venum].guid))
1723 break;
1724 if (venum == __be16_to_cpu(ddf->virt->max_vdes)) {
1725 fprintf(stderr, Name ": Cannot find spare slot for "
1726 "virtual disk - DDF is corrupt\n");
1727 return 0;
1728 }
1729 ve = &ddf->virt->entries[venum];
1730 ddf->conf_num = venum;
1731
1732 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
1733 * timestamp, random number
1734 */
1735 make_header_guid(ve->guid);
1736 ve->unit = __cpu_to_be16(info->md_minor);
1737 ve->pad0 = 0xFFFF;
1738 ve->guid_crc = crc32(0, (unsigned char*)ddf->anchor.guid, DDF_GUID_LEN);
1739 ve->type = 0;
1740 ve->state = DDF_state_degraded; /* Will be modified as devices are added */
1741 if (info->state & 1) /* clean */
1742 ve->init_state = DDF_init_full;
1743 else
1744 ve->init_state = DDF_init_not;
1745
1746 memset(ve->pad1, 0xff, 14);
1747 memset(ve->name, ' ', 16);
1748 if (name)
1749 strncpy(ve->name, name, 16);
1750 ddf->virt->populated_vdes =
1751 __cpu_to_be16(__be16_to_cpu(ddf->virt->populated_vdes)+1);
1752
1753 /* Now create a new vd_config */
1754 vcl = malloc(offsetof(struct vcl, conf) + ddf->conf_rec_len * 512);
1755 vcl->lba_offset = (__u64*) &vcl->conf.phys_refnum[ddf->mppe];
1756
1757 vc = &vcl->conf;
1758
1759 vc->magic = DDF_VD_CONF_MAGIC;
1760 memcpy(vc->guid, ve->guid, DDF_GUID_LEN);
1761 vc->timestamp = __cpu_to_be32(time(0)-DECADE);
1762 vc->seqnum = __cpu_to_be32(1);
1763 memset(vc->pad0, 0xff, 24);
1764 vc->prim_elmnt_count = __cpu_to_be16(info->raid_disks);
1765 vc->chunk_shift = chunk_to_shift(info->chunk_size);
1766 vc->prl = level_to_prl(info->level);
1767 vc->rlq = layout_to_rlq(info->level, info->layout, info->raid_disks);
1768 vc->sec_elmnt_count = 1;
1769 vc->sec_elmnt_seq = 0;
1770 vc->srl = 0;
1771 vc->blocks = __cpu_to_be64(info->size * 2);
1772 vc->array_blocks = __cpu_to_be64(
1773 calc_array_size(info->level, info->raid_disks, info->layout,
1774 info->chunk_size, info->size*2));
1775 memset(vc->pad1, 0xff, 8);
1776 vc->spare_refs[0] = 0xffffffff;
1777 vc->spare_refs[1] = 0xffffffff;
1778 vc->spare_refs[2] = 0xffffffff;
1779 vc->spare_refs[3] = 0xffffffff;
1780 vc->spare_refs[4] = 0xffffffff;
1781 vc->spare_refs[5] = 0xffffffff;
1782 vc->spare_refs[6] = 0xffffffff;
1783 vc->spare_refs[7] = 0xffffffff;
1784 memset(vc->cache_pol, 0, 8);
1785 vc->bg_rate = 0x80;
1786 memset(vc->pad2, 0xff, 3);
1787 memset(vc->pad3, 0xff, 52);
1788 memset(vc->pad4, 0xff, 192);
1789 memset(vc->v0, 0xff, 32);
1790 memset(vc->v1, 0xff, 32);
1791 memset(vc->v2, 0xff, 16);
1792 memset(vc->v3, 0xff, 16);
1793 memset(vc->vendor, 0xff, 32);
1794
1795 memset(vc->phys_refnum, 0xff, 4*ddf->mppe);
1796 memset(vc->phys_refnum+(ddf->mppe * 4), 0x00, 8*ddf->mppe);
1797
1798 vcl->next = ddf->conflist;
1799 ddf->conflist = vcl;
1800 ddf->newconf = vcl;
1801 return 1;
1802 }
1803
1804 static void add_to_super_ddf_bvd(struct supertype *st,
1805 mdu_disk_info_t *dk, int fd, char *devname)
1806 {
1807 /* fd and devname identify a device with-in the ddf container (st).
1808 * dk identifies a location in the new BVD.
1809 * We need to find suitable free space in that device and update
1810 * the phys_refnum and lba_offset for the newly created vd_config.
1811 * We might also want to update the type in the phys_disk
1812 * section.
1813 */
1814 struct dl *dl;
1815 struct ddf_super *ddf = st->sb;
1816 struct vd_config *vc;
1817 __u64 *lba_offset;
1818 int working;
1819 int i;
1820 int max_virt_disks;
1821
1822 for (dl = ddf->dlist; dl ; dl = dl->next)
1823 if (dl->major == dk->major &&
1824 dl->minor == dk->minor)
1825 break;
1826 if (!dl || ! (dk->state & (1<<MD_DISK_SYNC)))
1827 return;
1828
1829 vc = &ddf->newconf->conf;
1830 lba_offset = ddf->newconf->lba_offset;
1831 vc->phys_refnum[dk->raid_disk] = dl->disk.refnum;
1832 lba_offset[dk->raid_disk] = 0; /* FIXME */
1833
1834 for (i=0; i < ddf->max_part ; i++)
1835 if (dl->vlist[i] == NULL)
1836 break;
1837 if (i == ddf->max_part)
1838 return;
1839 dl->vlist[i] = ddf->newconf;
1840
1841 dl->fd = fd;
1842 dl->devname = devname;
1843
1844 /* Check how many working raid_disks, and if we can mark
1845 * array as optimal yet
1846 */
1847 working = 0;
1848
1849 for (i=0; i < __be16_to_cpu(vc->prim_elmnt_count); i++)
1850 if (vc->phys_refnum[i] != 0xffffffff)
1851 working++;
1852 /* Find which virtual_entry */
1853 max_virt_disks = __be16_to_cpu(ddf->active->max_vd_entries);
1854 for (i=0; i < max_virt_disks ; i++)
1855 if (memcmp(ddf->virt->entries[i].guid,
1856 vc->guid, DDF_GUID_LEN)==0)
1857 break;
1858 if (i == max_virt_disks)
1859 return;
1860 if (working == __be16_to_cpu(vc->prim_elmnt_count))
1861 ddf->virt->entries[i].state =
1862 (ddf->virt->entries[i].state & ~DDF_state_mask)
1863 | DDF_state_optimal;
1864
1865 if (vc->prl == DDF_RAID6 &&
1866 working+1 == __be16_to_cpu(vc->prim_elmnt_count))
1867 ddf->virt->entries[i].state =
1868 (ddf->virt->entries[i].state & ~DDF_state_mask)
1869 | DDF_state_part_optimal;
1870
1871 ddf->phys->entries[dl->pdnum].type &= ~__cpu_to_be16(DDF_Global_Spare);
1872 ddf->phys->entries[dl->pdnum].type |= __cpu_to_be16(DDF_Active_in_VD);
1873 }
1874
1875 /* add a device to a container, either while creating it or while
1876 * expanding a pre-existing container
1877 */
1878 static void add_to_super_ddf(struct supertype *st,
1879 mdu_disk_info_t *dk, int fd, char *devname)
1880 {
1881 struct ddf_super *ddf = st->sb;
1882 struct dl *dd;
1883 time_t now;
1884 struct tm *tm;
1885 unsigned long long size;
1886 struct phys_disk_entry *pde;
1887 int n, i;
1888 struct stat stb;
1889
1890 /* This is device numbered dk->number. We need to create
1891 * a phys_disk entry and a more detailed disk_data entry.
1892 */
1893 fstat(fd, &stb);
1894 dd = malloc(sizeof(*dd) + sizeof(dd->vlist[0]) * ddf->max_part);
1895 dd->major = major(stb.st_rdev);
1896 dd->minor = minor(stb.st_rdev);
1897 dd->devname = devname;
1898 dd->next = ddf->dlist;
1899 dd->fd = fd;
1900 dd->spare = NULL;
1901
1902 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
1903 now = time(0);
1904 tm = localtime(&now);
1905 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
1906 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
1907 *(__u32*)(dd->disk.guid + 16) = random();
1908 *(__u32*)(dd->disk.guid + 20) = random();
1909
1910 dd->disk.refnum = random(); /* and hope for the best FIXME check this is unique!!*/
1911 dd->disk.forced_ref = 1;
1912 dd->disk.forced_guid = 1;
1913 memset(dd->disk.vendor, ' ', 32);
1914 memcpy(dd->disk.vendor, "Linux", 5);
1915 memset(dd->disk.pad, 0xff, 442);
1916 for (i = 0; i < ddf->max_part ; i++)
1917 dd->vlist[i] = NULL;
1918
1919 n = __be16_to_cpu(ddf->phys->used_pdes);
1920 pde = &ddf->phys->entries[n];
1921 dd->pdnum = n;
1922
1923 n++;
1924 ddf->phys->used_pdes = __cpu_to_be16(n);
1925
1926 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
1927 pde->refnum = dd->disk.refnum;
1928 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID | DDF_Global_Spare);
1929 pde->state = __cpu_to_be16(DDF_Online);
1930 get_dev_size(fd, NULL, &size);
1931 /* We are required to reserve 32Meg, and record the size in sectors */
1932 pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512);
1933 sprintf(pde->path, "%17.17s","Information: nil") ;
1934 memset(pde->pad, 0xff, 6);
1935
1936 ddf->dlist = dd;
1937 }
1938
1939 /*
1940 * This is the write_init_super method for a ddf container. It is
1941 * called when creating a container or adding another device to a
1942 * container.
1943 */
1944
1945 #ifndef MDASSEMBLE
1946 static int __write_init_super_ddf(struct supertype *st, int do_close)
1947 {
1948
1949 struct ddf_super *ddf = st->sb;
1950 int i;
1951 struct dl *d;
1952 int n_config;
1953 int conf_size;
1954
1955 unsigned long long size, sector;
1956
1957 for (d = ddf->dlist; d; d=d->next) {
1958 int fd = d->fd;
1959
1960 if (fd < 0)
1961 continue;
1962
1963 /* We need to fill in the primary, (secondary) and workspace
1964 * lba's in the headers, set their checksums,
1965 * Also checksum phys, virt....
1966 *
1967 * Then write everything out, finally the anchor is written.
1968 */
1969 get_dev_size(fd, NULL, &size);
1970 size /= 512;
1971 ddf->anchor.workspace_lba = __cpu_to_be64(size - 32*1024*2);
1972 ddf->anchor.primary_lba = __cpu_to_be64(size - 16*1024*2);
1973 ddf->anchor.seq = __cpu_to_be32(1);
1974 memcpy(&ddf->primary, &ddf->anchor, 512);
1975 memcpy(&ddf->secondary, &ddf->anchor, 512);
1976
1977 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
1978 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
1979 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
1980
1981 ddf->primary.openflag = 0;
1982 ddf->primary.type = DDF_HEADER_PRIMARY;
1983
1984 ddf->secondary.openflag = 0;
1985 ddf->secondary.type = DDF_HEADER_SECONDARY;
1986
1987 ddf->primary.crc = calc_crc(&ddf->primary, 512);
1988 ddf->secondary.crc = calc_crc(&ddf->secondary, 512);
1989
1990 sector = size - 16*1024*2;
1991 lseek64(fd, sector<<9, 0);
1992 write(fd, &ddf->primary, 512);
1993
1994 ddf->controller.crc = calc_crc(&ddf->controller, 512);
1995 write(fd, &ddf->controller, 512);
1996
1997 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
1998
1999 write(fd, ddf->phys, ddf->pdsize);
2000
2001 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
2002 write(fd, ddf->virt, ddf->vdsize);
2003
2004 /* Now write lots of config records. */
2005 n_config = ddf->max_part;
2006 conf_size = ddf->conf_rec_len * 512;
2007 for (i = 0 ; i <= n_config ; i++) {
2008 struct vcl *c = d->vlist[i];
2009 if (i == n_config)
2010 c = (struct vcl*)d->spare;
2011
2012 if (c) {
2013 c->conf.crc = calc_crc(&c->conf, conf_size);
2014 write(fd, &c->conf, conf_size);
2015 } else {
2016 __u32 sig = 0xffffffff;
2017 write(fd, &sig, 4);
2018 lseek64(fd, conf_size-4, SEEK_CUR);
2019 }
2020 }
2021 d->disk.crc = calc_crc(&d->disk, 512);
2022 write(fd, &d->disk, 512);
2023
2024 /* Maybe do the same for secondary */
2025
2026 lseek64(fd, (size-1)*512, SEEK_SET);
2027 write(fd, &ddf->anchor, 512);
2028 if (do_close) {
2029 close(fd);
2030 d->fd = -1;
2031 }
2032 }
2033 return 1;
2034 }
2035
2036 static int write_init_super_ddf(struct supertype *st)
2037 {
2038 return __write_init_super_ddf(st, 1);
2039 }
2040
2041 #endif
2042
2043 static __u64 avail_size_ddf(struct supertype *st, __u64 devsize)
2044 {
2045 /* We must reserve the last 32Meg */
2046 if (devsize <= 32*1024*2)
2047 return 0;
2048 return devsize - 32*1024*2;
2049 }
2050
2051 #ifndef MDASSEMBLE
2052 int validate_geometry_ddf(struct supertype *st,
2053 int level, int layout, int raiddisks,
2054 int chunk, unsigned long long size,
2055 char *dev, unsigned long long *freesize)
2056 {
2057 int fd;
2058 struct mdinfo *sra;
2059 int cfd;
2060
2061 /* ddf potentially supports lots of things, but it depends on
2062 * what devices are offered (and maybe kernel version?)
2063 * If given unused devices, we will make a container.
2064 * If given devices in a container, we will make a BVD.
2065 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2066 */
2067
2068 if (level == LEVEL_CONTAINER) {
2069 st->ss = &super_ddf_container;
2070 if (dev) {
2071 int rv =st->ss->validate_geometry(st, level, layout,
2072 raiddisks, chunk,
2073 size,
2074 NULL, freesize);
2075 if (rv)
2076 return rv;
2077 }
2078 return st->ss->validate_geometry(st, level, layout, raiddisks,
2079 chunk, size, dev, freesize);
2080 }
2081
2082 if (st->sb) {
2083 /* creating in a given container */
2084 st->ss = &super_ddf_bvd;
2085 if (dev) {
2086 int rv =st->ss->validate_geometry(st, level, layout,
2087 raiddisks, chunk,
2088 size,
2089 NULL, freesize);
2090 if (rv)
2091 return rv;
2092 }
2093 return st->ss->validate_geometry(st, level, layout, raiddisks,
2094 chunk, size, dev, freesize);
2095 }
2096 /* FIXME should exclude MULTIPATH, or more appropriately, allow
2097 * only known levels.
2098 */
2099 if (!dev)
2100 return 1;
2101
2102 /* This device needs to be either a device in a 'ddf' container,
2103 * or it needs to be a 'ddf-bvd' array.
2104 */
2105
2106 fd = open(dev, O_RDONLY|O_EXCL, 0);
2107 if (fd >= 0) {
2108 sra = sysfs_read(fd, 0, GET_VERSION);
2109 close(fd);
2110 if (sra && sra->array.major_version == -1 &&
2111 strcmp(sra->text_version, "ddf-bvd") == 0) {
2112 st->ss = &super_ddf_svd;
2113 return st->ss->validate_geometry(st, level, layout,
2114 raiddisks, chunk, size,
2115 dev, freesize);
2116 }
2117
2118 fprintf(stderr,
2119 Name ": Cannot create this array on device %s\n",
2120 dev);
2121 return 0;
2122 }
2123 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
2124 fprintf(stderr, Name ": Cannot open %s: %s\n",
2125 dev, strerror(errno));
2126 return 0;
2127 }
2128 /* Well, it is in use by someone, maybe a 'ddf' container. */
2129 cfd = open_container(fd);
2130 if (cfd < 0) {
2131 close(fd);
2132 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
2133 dev);
2134 return 0;
2135 }
2136 sra = sysfs_read(cfd, 0, GET_VERSION);
2137 close(fd);
2138 if (sra && sra->array.major_version == -1 &&
2139 strcmp(sra->text_version, "ddf") == 0) {
2140 /* This is a member of a ddf container. Load the container
2141 * and try to create a bvd
2142 */
2143 struct ddf_super *ddf;
2144 st->ss = &super_ddf_bvd;
2145 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL, 1) == 0) {
2146 st->sb = ddf;
2147 st->container_dev = fd2devnum(cfd);
2148 close(cfd);
2149 return st->ss->validate_geometry(st, level, layout,
2150 raiddisks, chunk, size,
2151 dev, freesize);
2152 }
2153 close(cfd);
2154 } else /* device may belong to a different container */
2155 return 0;
2156
2157 return 1;
2158 }
2159
2160 int validate_geometry_ddf_container(struct supertype *st,
2161 int level, int layout, int raiddisks,
2162 int chunk, unsigned long long size,
2163 char *dev, unsigned long long *freesize)
2164 {
2165 int fd;
2166 unsigned long long ldsize;
2167
2168 if (level != LEVEL_CONTAINER)
2169 return 0;
2170 if (!dev)
2171 return 1;
2172
2173 fd = open(dev, O_RDONLY|O_EXCL, 0);
2174 if (fd < 0) {
2175 fprintf(stderr, Name ": Cannot open %s: %s\n",
2176 dev, strerror(errno));
2177 return 0;
2178 }
2179 if (!get_dev_size(fd, dev, &ldsize)) {
2180 close(fd);
2181 return 0;
2182 }
2183 close(fd);
2184
2185 *freesize = avail_size_ddf(st, ldsize >> 9);
2186
2187 return 1;
2188 }
2189
2190 struct extent {
2191 unsigned long long start, size;
2192 };
2193 int cmp_extent(const void *av, const void *bv)
2194 {
2195 const struct extent *a = av;
2196 const struct extent *b = bv;
2197 if (a->start < b->start)
2198 return -1;
2199 if (a->start > b->start)
2200 return 1;
2201 return 0;
2202 }
2203
2204 struct extent *get_extents(struct ddf_super *ddf, struct dl *dl)
2205 {
2206 /* find a list of used extents on the give physical device
2207 * (dnum) of the given ddf.
2208 * Return a malloced array of 'struct extent'
2209
2210 FIXME ignore DDF_Legacy devices?
2211
2212 */
2213 struct extent *rv;
2214 int n = 0;
2215 int dnum;
2216 int i, j;
2217
2218 /* FIXME this is dl->pdnum */
2219 for (dnum = 0; dnum < ddf->phys->used_pdes; dnum++)
2220 if (memcmp(dl->disk.guid,
2221 ddf->phys->entries[dnum].guid,
2222 DDF_GUID_LEN) == 0)
2223 break;
2224
2225 if (dnum == ddf->phys->used_pdes)
2226 return NULL;
2227
2228 rv = malloc(sizeof(struct extent) * (ddf->max_part + 2));
2229 if (!rv)
2230 return NULL;
2231
2232 for (i = 0; i < ddf->max_part; i++) {
2233 struct vcl *v = dl->vlist[i];
2234 if (v == NULL)
2235 continue;
2236 for (j=0; j < v->conf.prim_elmnt_count; j++)
2237 if (v->conf.phys_refnum[j] == dl->disk.refnum) {
2238 /* This device plays role 'j' in 'v'. */
2239 rv[n].start = __be64_to_cpu(v->lba_offset[j]);
2240 rv[n].size = __be64_to_cpu(v->conf.blocks);
2241 n++;
2242 break;
2243 }
2244 }
2245 qsort(rv, n, sizeof(*rv), cmp_extent);
2246
2247 rv[n].start = __be64_to_cpu(ddf->phys->entries[dnum].config_size);
2248 rv[n].size = 0;
2249 return rv;
2250 }
2251
2252 int validate_geometry_ddf_bvd(struct supertype *st,
2253 int level, int layout, int raiddisks,
2254 int chunk, unsigned long long size,
2255 char *dev, unsigned long long *freesize)
2256 {
2257 struct stat stb;
2258 struct ddf_super *ddf = st->sb;
2259 struct dl *dl;
2260 unsigned long long pos = 0;
2261 unsigned long long maxsize;
2262 struct extent *e;
2263 int i;
2264 /* ddf/bvd supports lots of things, but not containers */
2265 if (level == LEVEL_CONTAINER)
2266 return 0;
2267 /* We must have the container info already read in. */
2268 if (!ddf)
2269 return 0;
2270
2271 if (!dev) {
2272 /* General test: make sure there is space for
2273 * 'raiddisks' device extents of size 'size'.
2274 */
2275 unsigned long long minsize = size;
2276 int dcnt = 0;
2277 if (minsize == 0)
2278 minsize = 8;
2279 for (dl = ddf->dlist; dl ; dl = dl->next)
2280 {
2281 int found = 0;
2282 pos = 0;
2283
2284 i = 0;
2285 e = get_extents(ddf, dl);
2286 if (!e) continue;
2287 do {
2288 unsigned long long esize;
2289 esize = e[i].start - pos;
2290 if (esize >= minsize)
2291 found = 1;
2292 pos = e[i].start + e[i].size;
2293 i++;
2294 } while (e[i-1].size);
2295 if (found)
2296 dcnt++;
2297 free(e);
2298 }
2299 if (dcnt < raiddisks) {
2300 fprintf(stderr, Name ": Not enough devices with space "
2301 "for this array (%d < %d)\n",
2302 dcnt, raiddisks);
2303 return 0;
2304 }
2305 return 1;
2306 }
2307 /* This device must be a member of the set */
2308 if (stat(dev, &stb) < 0)
2309 return 0;
2310 if ((S_IFMT & stb.st_mode) != S_IFBLK)
2311 return 0;
2312 for (dl = ddf->dlist ; dl ; dl = dl->next) {
2313 if (dl->major == major(stb.st_rdev) &&
2314 dl->minor == minor(stb.st_rdev))
2315 break;
2316 }
2317 if (!dl) {
2318 fprintf(stderr, Name ": %s is not in the same DDF set\n",
2319 dev);
2320 return 0;
2321 }
2322 e = get_extents(ddf, dl);
2323 maxsize = 0;
2324 i = 0;
2325 if (e) do {
2326 unsigned long long esize;
2327 esize = e[i].start - pos;
2328 if (esize >= maxsize)
2329 maxsize = esize;
2330 pos = e[i].start + e[i].size;
2331 i++;
2332 } while (e[i-1].size);
2333 *freesize = maxsize;
2334 // FIXME here I am
2335
2336 return 1;
2337 }
2338 int validate_geometry_ddf_svd(struct supertype *st,
2339 int level, int layout, int raiddisks,
2340 int chunk, unsigned long long size,
2341 char *dev, unsigned long long *freesize)
2342 {
2343 /* dd/svd only supports striped, mirrored, concat, spanned... */
2344 if (level != LEVEL_LINEAR &&
2345 level != 0 &&
2346 level != 1)
2347 return 0;
2348 return 1;
2349 }
2350
2351
2352 static int load_super_ddf_all(struct supertype *st, int fd,
2353 void **sbp, char *devname, int keep_fd)
2354 {
2355 struct mdinfo *sra;
2356 struct ddf_super *super;
2357 struct mdinfo *sd, *best = NULL;
2358 int bestseq = 0;
2359 int seq;
2360 char nm[20];
2361 int dfd;
2362
2363 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
2364 if (!sra)
2365 return 1;
2366 if (sra->array.major_version != -1 ||
2367 sra->array.minor_version != -2 ||
2368 strcmp(sra->text_version, "ddf") != 0)
2369 return 1;
2370
2371 super = malloc(sizeof(*super));
2372 if (!super)
2373 return 1;
2374 memset(super, 0, sizeof(*super));
2375
2376 /* first, try each device, and choose the best ddf */
2377 for (sd = sra->devs ; sd ; sd = sd->next) {
2378 int rv;
2379 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2380 dfd = dev_open(nm, O_RDONLY);
2381 if (dfd < 0)
2382 return 2;
2383 rv = load_ddf_headers(dfd, super, NULL);
2384 close(dfd);
2385 if (rv == 0) {
2386 seq = __be32_to_cpu(super->active->seq);
2387 if (super->active->openflag)
2388 seq--;
2389 if (!best || seq > bestseq) {
2390 bestseq = seq;
2391 best = sd;
2392 }
2393 }
2394 }
2395 if (!best)
2396 return 1;
2397 /* OK, load this ddf */
2398 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2399 dfd = dev_open(nm, O_RDONLY);
2400 if (dfd < 0)
2401 return 1;
2402 load_ddf_headers(dfd, super, NULL);
2403 load_ddf_global(dfd, super, NULL);
2404 close(dfd);
2405 /* Now we need the device-local bits */
2406 for (sd = sra->devs ; sd ; sd = sd->next) {
2407 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2408 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2409 if (dfd < 0)
2410 return 2;
2411 seq = load_ddf_local(dfd, super, NULL, keep_fd);
2412 if (!keep_fd) close(dfd);
2413 }
2414 *sbp = super;
2415 if (st->ss == NULL) {
2416 st->ss = &super_ddf_container;
2417 st->minor_version = 0;
2418 st->max_devs = 512;
2419 st->container_dev = fd2devnum(fd);
2420 }
2421 return 0;
2422 }
2423 #endif
2424
2425
2426
2427 static struct mdinfo *container_content_ddf(struct supertype *st)
2428 {
2429 /* Given a container loaded by load_super_ddf_all,
2430 * extract information about all the arrays into
2431 * an mdinfo tree.
2432 *
2433 * For each vcl in conflist: create an mdinfo, fill it in,
2434 * then look for matching devices (phys_refnum) in dlist
2435 * and create appropriate device mdinfo.
2436 */
2437 struct ddf_super *ddf = st->sb;
2438 struct mdinfo *rest = NULL;
2439 struct vcl *vc;
2440
2441 for (vc = ddf->conflist ; vc ; vc=vc->next)
2442 {
2443 int i;
2444 struct mdinfo *this;
2445 this = malloc(sizeof(*this));
2446 memset(this, 0, sizeof(*this));
2447 this->next = rest;
2448 rest = this;
2449
2450 this->array.major_version = 1000;
2451 this->array.minor_version = 0;
2452 this->array.patch_version = 0;
2453 this->array.level = map_num1(ddf_level_num, vc->conf.prl);
2454 this->array.raid_disks =
2455 __be16_to_cpu(vc->conf.prim_elmnt_count);
2456 this->array.layout = rlq_to_layout(vc->conf.rlq, vc->conf.prl,
2457 this->array.raid_disks);
2458 this->array.md_minor = -1;
2459 this->array.ctime = DECADE +
2460 __be32_to_cpu(*(__u32*)(vc->conf.guid+16));
2461 this->array.utime = DECADE +
2462 __be32_to_cpu(vc->conf.timestamp);
2463 this->array.chunk_size = 512 << vc->conf.chunk_shift;
2464
2465 for (i=0; i < __be16_to_cpu(ddf->virt->populated_vdes); i++)
2466 if (memcmp(ddf->virt->entries[i].guid,
2467 vc->conf.guid, DDF_GUID_LEN) == 0)
2468 break;
2469 if ((ddf->virt->entries[i].state & DDF_state_inconsistent) ||
2470 (ddf->virt->entries[i].init_state & DDF_initstate_mask) !=
2471 DDF_init_full) {
2472 this->array.state = 0;
2473 this->resync_start = 0;
2474 } else {
2475 this->array.state = 1;
2476 this->resync_start = ~0ULL;
2477 }
2478 memcpy(this->name, ddf->virt->entries[i].name, 32);
2479 this->name[33]=0;
2480
2481 memset(this->uuid, 0, sizeof(this->uuid));
2482 this->component_size = __be64_to_cpu(vc->conf.blocks);
2483 this->array.size = this->component_size / 2;
2484 this->container_member = i;
2485
2486 sprintf(this->text_version, "/%s/%d",
2487 devnum2devname(st->container_dev),
2488 this->container_member);
2489
2490
2491 for (i=0 ; i < ddf->mppe ; i++) {
2492 struct mdinfo *dev;
2493 struct dl *d;
2494
2495 if (vc->conf.phys_refnum[i] == 0xFFFFFFFF)
2496 continue;
2497
2498 this->array.working_disks++;
2499
2500 for (d = ddf->dlist; d ; d=d->next)
2501 if (d->disk.refnum == vc->conf.phys_refnum[i])
2502 break;
2503 if (d == NULL)
2504 break;
2505
2506 dev = malloc(sizeof(*dev));
2507 memset(dev, 0, sizeof(*dev));
2508 dev->next = this->devs;
2509 this->devs = dev;
2510
2511 dev->disk.number = __be32_to_cpu(d->disk.refnum);
2512 dev->disk.major = d->major;
2513 dev->disk.minor = d->minor;
2514 dev->disk.raid_disk = i;
2515 dev->disk.state = (1<<MD_DISK_SYNC)|(1<<MD_DISK_ACTIVE);
2516
2517 dev->events = __le32_to_cpu(ddf->primary.seq);
2518 dev->data_offset = vc->lba_offset[i];
2519 dev->component_size = __be64_to_cpu(vc->conf.blocks);
2520 if (d->devname)
2521 strcpy(dev->name, d->devname);
2522 }
2523 }
2524 return rest;
2525 }
2526
2527 static int init_zero_ddf(struct supertype *st,
2528 mdu_array_info_t *info,
2529 unsigned long long size, char *name,
2530 char *homehost, int *uuid)
2531 {
2532 st->sb = NULL;
2533 return 0;
2534 }
2535
2536 static int store_zero_ddf(struct supertype *st, int fd)
2537 {
2538 unsigned long long dsize;
2539 char buf[512];
2540 memset(buf, 0, 512);
2541
2542
2543 if (!get_dev_size(fd, NULL, &dsize))
2544 return 1;
2545
2546 lseek64(fd, dsize-512, 0);
2547 write(fd, buf, 512);
2548 return 0;
2549 }
2550
2551 static int compare_super_ddf(struct supertype *st, struct supertype *tst)
2552 {
2553 /*
2554 * return:
2555 * 0 same, or first was empty, and second was copied
2556 * 1 second had wrong number
2557 * 2 wrong uuid
2558 * 3 wrong other info
2559 */
2560 struct ddf_super *first = st->sb;
2561 struct ddf_super *second = tst->sb;
2562
2563 if (!first) {
2564 st->sb = tst->sb;
2565 tst->sb = NULL;
2566 return 0;
2567 }
2568
2569 if (memcmp(first->anchor.guid, second->anchor.guid, DDF_GUID_LEN) != 0)
2570 return 2;
2571
2572 /* FIXME should I look at anything else? */
2573 return 0;
2574 }
2575
2576 /*
2577 * A new array 'a' has been started which claims to be instance 'inst'
2578 * within container 'c'.
2579 * We need to confirm that the array matches the metadata in 'c' so
2580 * that we don't corrupt any metadata.
2581 */
2582 static int ddf_open_new(struct supertype *c, struct active_array *a, char *inst)
2583 {
2584 fprintf(stderr, "ddf: open_new %s\n", inst);
2585 a->info.container_member = atoi(inst);
2586 return 0;
2587 }
2588
2589 /*
2590 * The array 'a' is to be marked clean in the metadata.
2591 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
2592 * clean up to the point (in sectors). If that cannot be recorded in the
2593 * metadata, then leave it as dirty.
2594 *
2595 * For DDF, we need to clear the DDF_state_inconsistent bit in the
2596 * !global! virtual_disk.virtual_entry structure.
2597 */
2598 static void ddf_set_array_state(struct active_array *a, int consistent)
2599 {
2600 struct ddf_super *ddf = a->container->sb;
2601 int inst = a->info.container_member;
2602 if (consistent)
2603 ddf->virt->entries[inst].state &= ~DDF_state_inconsistent;
2604 else
2605 ddf->virt->entries[inst].state |= DDF_state_inconsistent;
2606 ddf->virt->entries[inst].init_state &= ~DDF_initstate_mask;
2607 if (a->resync_start == ~0ULL)
2608 ddf->virt->entries[inst].init_state |= DDF_init_full;
2609 else if (a->resync_start == 0)
2610 ddf->virt->entries[inst].init_state |= DDF_init_not;
2611 else
2612 ddf->virt->entries[inst].init_state |= DDF_init_quick;
2613
2614 printf("ddf mark %s %llu\n", consistent?"clean":"dirty",
2615 a->resync_start);
2616 }
2617
2618 /*
2619 * The state of each disk is stored in the global phys_disk structure
2620 * in phys_disk.entries[n].state.
2621 * This makes various combinations awkward.
2622 * - When a device fails in any array, it must be failed in all arrays
2623 * that include a part of this device.
2624 * - When a component is rebuilding, we cannot include it officially in the
2625 * array unless this is the only array that uses the device.
2626 *
2627 * So: when transitioning:
2628 * Online -> failed, just set failed flag. monitor will propagate
2629 * spare -> online, the device might need to be added to the array.
2630 * spare -> failed, just set failed. Don't worry if in array or not.
2631 */
2632 static void ddf_set_disk(struct active_array *a, int n, int state)
2633 {
2634 struct ddf_super *ddf = a->container->sb;
2635 int inst = a->info.container_member;
2636 struct vd_config *vc = find_vdcr(ddf, inst);
2637 int pd = find_phys(ddf, vc->phys_refnum[n]);
2638 int i, st, working;
2639
2640 if (vc == NULL) {
2641 fprintf(stderr, "ddf: cannot find instance %d!!\n", inst);
2642 return;
2643 }
2644 if (pd < 0) {
2645 /* disk doesn't currently exist. If it is now in_sync,
2646 * insert it. */
2647 if ((state & DS_INSYNC) && ! (state & DS_FAULTY)) {
2648 /* Find dev 'n' in a->info->devs, determine the
2649 * ddf refnum, and set vc->phys_refnum and update
2650 * phys->entries[]
2651 */
2652 /* FIXME */
2653 }
2654 } else {
2655 if (state & DS_FAULTY)
2656 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Failed);
2657 if (state & DS_INSYNC) {
2658 ddf->phys->entries[pd].state |= __cpu_to_be16(DDF_Online);
2659 ddf->phys->entries[pd].state &= __cpu_to_be16(~DDF_Rebuilding);
2660 }
2661 }
2662
2663 fprintf(stderr, "ddf: set_disk %d to %x\n", n, state);
2664
2665 /* Now we need to check the state of the array and update
2666 * virtual_disk.entries[n].state.
2667 * It needs to be one of "optimal", "degraded", "failed".
2668 * I don't understand 'deleted' or 'missing'.
2669 */
2670 working = 0;
2671 for (i=0; i < a->info.array.raid_disks; i++) {
2672 pd = find_phys(ddf, vc->phys_refnum[i]);
2673 if (pd < 0)
2674 continue;
2675 st = __be16_to_cpu(ddf->phys->entries[pd].state);
2676 if ((st & (DDF_Online|DDF_Failed|DDF_Rebuilding))
2677 == DDF_Online)
2678 working++;
2679 }
2680 state = DDF_state_degraded;
2681 if (working == a->info.array.raid_disks)
2682 state = DDF_state_optimal;
2683 else switch(vc->prl) {
2684 case DDF_RAID0:
2685 case DDF_CONCAT:
2686 case DDF_JBOD:
2687 state = DDF_state_failed;
2688 break;
2689 case DDF_RAID1:
2690 if (working == 0)
2691 state = DDF_state_failed;
2692 break;
2693 case DDF_RAID4:
2694 case DDF_RAID5:
2695 if (working < a->info.array.raid_disks-1)
2696 state = DDF_state_failed;
2697 break;
2698 case DDF_RAID6:
2699 if (working < a->info.array.raid_disks-2)
2700 state = DDF_state_failed;
2701 else if (working == a->info.array.raid_disks-1)
2702 state = DDF_state_part_optimal;
2703 break;
2704 }
2705
2706 ddf->virt->entries[inst].state =
2707 (ddf->virt->entries[inst].state & ~DDF_state_mask)
2708 | state;
2709
2710 }
2711
2712 static void ddf_sync_metadata(struct supertype *st)
2713 {
2714
2715 /*
2716 * Write all data to all devices.
2717 * Later, we might be able to track whether only local changes
2718 * have been made, or whether any global data has been changed,
2719 * but ddf is sufficiently weird that it probably always
2720 * changes global data ....
2721 */
2722 __write_init_super_ddf(st, 0);
2723 fprintf(stderr, "ddf: sync_metadata\n");
2724 }
2725
2726 static void ddf_process_update(struct supertype *st,
2727 struct metadata_update *update)
2728 {
2729 /* Apply this update to the metadata.
2730 * The first 4 bytes are a DDF_*_MAGIC which guides
2731 * our actions.
2732 * Possible update are:
2733 * DDF_PHYS_RECORDS_MAGIC
2734 * Add a new physical device. Changes to this record
2735 * only happen implicitly.
2736 * used_pdes is the device number.
2737 * DDF_VIRT_RECORDS_MAGIC
2738 * Add a new VD. Possibly also change the 'access' bits.
2739 * populated_vdes is the entry number.
2740 * DDF_VD_CONF_MAGIC
2741 * New or updated VD. the VIRT_RECORD must already
2742 * exist. For an update, phys_refnum and lba_offset
2743 * (at least) are updated, and the VD_CONF must
2744 * be written to precisely those devices listed with
2745 * a phys_refnum.
2746 * DDF_SPARE_ASSIGN_MAGIC
2747 * replacement Spare Assignment Record... but for which device?
2748 *
2749 * So, e.g.:
2750 * - to create a new array, we send a VIRT_RECORD and
2751 * a VD_CONF. Then assemble and start the array.
2752 * - to activate a spare we send a VD_CONF to add the phys_refnum
2753 * and offset. This will also mark the spare as active with
2754 * a spare-assignment record.
2755 */
2756 struct ddf_super *ddf = st->sb;
2757 __u32 *magic = (__u32*)update->buf;
2758 struct phys_disk *pd;
2759 struct virtual_disk *vd;
2760 struct vd_config *vc;
2761 struct vcl *vcl;
2762 struct dl *dl;
2763 int mppe;
2764 int ent;
2765
2766 printf("Process update %x\n", *magic);
2767
2768 switch (*magic) {
2769 case DDF_PHYS_RECORDS_MAGIC:
2770
2771 if (update->len != (sizeof(struct phys_disk) +
2772 sizeof(struct phys_disk_entry)))
2773 return;
2774 pd = (struct phys_disk*)update->buf;
2775
2776 ent = __be16_to_cpu(pd->used_pdes);
2777 if (ent >= __be16_to_cpu(ddf->phys->max_pdes))
2778 return;
2779 if (!all_ff(ddf->phys->entries[ent].guid))
2780 return;
2781 ddf->phys->entries[ent] = pd->entries[0];
2782 ddf->phys->used_pdes = __cpu_to_be16(1 +
2783 __be16_to_cpu(ddf->phys->used_pdes));
2784 break;
2785
2786 case DDF_VIRT_RECORDS_MAGIC:
2787
2788 if (update->len != (sizeof(struct virtual_disk) +
2789 sizeof(struct virtual_entry)))
2790 return;
2791 vd = (struct virtual_disk*)update->buf;
2792
2793 ent = __be16_to_cpu(vd->populated_vdes);
2794 if (ent >= __be16_to_cpu(ddf->virt->max_vdes))
2795 return;
2796 if (!all_ff(ddf->virt->entries[ent].guid))
2797 return;
2798 ddf->virt->entries[ent] = vd->entries[0];
2799 ddf->virt->populated_vdes = __cpu_to_be16(1 +
2800 __be16_to_cpu(ddf->virt->populated_vdes));
2801 break;
2802
2803 case DDF_VD_CONF_MAGIC:
2804 printf("len %d %d\n", update->len, ddf->conf_rec_len);
2805
2806 mppe = __be16_to_cpu(ddf->anchor.max_primary_element_entries);
2807 if (update->len != ddf->conf_rec_len)
2808 return;
2809 vc = (struct vd_config*)update->buf;
2810 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
2811 if (memcmp(vcl->conf.guid, vc->guid, DDF_GUID_LEN) == 0)
2812 break;
2813 printf("vcl = %p\n", vcl);
2814 if (vcl) {
2815 /* An update, just copy the phys_refnum and lba_offset
2816 * fields
2817 */
2818 memcpy(vcl->conf.phys_refnum, vc->phys_refnum,
2819 mppe * (sizeof(__u32) + sizeof(__u64)));
2820 } else {
2821 /* A new VD_CONF */
2822 vcl = update->space;
2823 update->space = NULL;
2824 vcl->next = ddf->conflist;
2825 vcl->conf = *vc;
2826 vcl->lba_offset = (__u64*)
2827 &vcl->conf.phys_refnum[mppe];
2828 ddf->conflist = vcl;
2829 }
2830 /* Now make sure vlist is correct for each dl. */
2831 for (dl = ddf->dlist; dl; dl = dl->next) {
2832 int dn;
2833 int vn = 0;
2834 for (vcl = ddf->conflist; vcl ; vcl = vcl->next)
2835 for (dn=0; dn < ddf->mppe ; dn++)
2836 if (vcl->conf.phys_refnum[dn] ==
2837 dl->disk.refnum) {
2838 printf("dev %d has %p at %d\n",
2839 dl->pdnum, vcl, vn);
2840 dl->vlist[vn++] = vcl;
2841 break;
2842 }
2843 while (vn < ddf->max_part)
2844 dl->vlist[vn++] = NULL;
2845 if (dl->vlist[0]) {
2846 ddf->phys->entries[dl->pdnum].type &=
2847 ~__cpu_to_be16(DDF_Global_Spare);
2848 ddf->phys->entries[dl->pdnum].type |=
2849 __cpu_to_be16(DDF_Active_in_VD);
2850 }
2851 if (dl->spare) {
2852 ddf->phys->entries[dl->pdnum].type &=
2853 ~__cpu_to_be16(DDF_Global_Spare);
2854 ddf->phys->entries[dl->pdnum].type |=
2855 __cpu_to_be16(DDF_Spare);
2856 }
2857 if (!dl->vlist[0] && !dl->spare) {
2858 ddf->phys->entries[dl->pdnum].type |=
2859 __cpu_to_be16(DDF_Global_Spare);
2860 ddf->phys->entries[dl->pdnum].type &=
2861 ~__cpu_to_be16(DDF_Spare |
2862 DDF_Active_in_VD);
2863 }
2864 }
2865 break;
2866 case DDF_SPARE_ASSIGN_MAGIC:
2867 default: break;
2868 }
2869 }
2870
2871 /*
2872 * Check if the array 'a' is degraded but not failed.
2873 * If it is, find as many spares as are available and needed and
2874 * arrange for their inclusion.
2875 * We only choose devices which are not already in the array,
2876 * and prefer those with a spare-assignment to this array.
2877 * otherwise we choose global spares - assuming always that
2878 * there is enough room.
2879 * For each spare that we assign, we return an 'mdinfo' which
2880 * describes the position for the device in the array.
2881 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
2882 * the new phys_refnum and lba_offset values.
2883 *
2884 * Only worry about BVDs at the moment.
2885 */
2886 static struct mdinfo *ddf_activate_spare(struct active_array *a,
2887 struct metadata_update **updates)
2888 {
2889 int working = 0;
2890 struct mdinfo *d;
2891 struct ddf_super *ddf = a->container->sb;
2892 int global_ok = 0;
2893 struct mdinfo *rv = NULL;
2894 struct mdinfo *di;
2895 struct metadata_update *mu;
2896 struct dl *dl;
2897 int i;
2898 struct vd_config *vc;
2899 __u64 *lba;
2900
2901 /* FIXME, If there is a DS_FAULTY, we want to wait for it to be
2902 * removed. Then only look at DS_REMOVE devices.
2903 * What about !DS_INSYNC - how can that happen?
2904 */
2905 for (d = a->info.devs ; d ; d = d->next) {
2906 if ((d->curr_state & DS_FAULTY) &&
2907 d->state_fd >= 0)
2908 /* wait for Removal to happen */
2909 return NULL;
2910 if (d->state_fd >= 0)
2911 working ++;
2912 }
2913
2914 printf("ddf_activate: working=%d (%d) level=%d\n", working, a->info.array.raid_disks,
2915 a->info.array.level);
2916 if (working == a->info.array.raid_disks)
2917 return NULL; /* array not degraded */
2918 switch (a->info.array.level) {
2919 case 1:
2920 if (working == 0)
2921 return NULL; /* failed */
2922 break;
2923 case 4:
2924 case 5:
2925 if (working < a->info.array.raid_disks - 1)
2926 return NULL; /* failed */
2927 break;
2928 case 6:
2929 if (working < a->info.array.raid_disks - 2)
2930 return NULL; /* failed */
2931 break;
2932 default: /* concat or stripe */
2933 return NULL; /* failed */
2934 }
2935
2936 /* For each slot, if it is not working, find a spare */
2937 dl = ddf->dlist;
2938 for (i = 0; i < a->info.array.raid_disks; i++) {
2939 for (d = a->info.devs ; d ; d = d->next)
2940 if (d->disk.raid_disk == i)
2941 break;
2942 printf("found %d: %p %x\n", i, d, d?d->curr_state:0);
2943 if (d && (d->state_fd >= 0))
2944 continue;
2945
2946 /* OK, this device needs recovery. Find a spare */
2947 again:
2948 for ( ; dl ; dl = dl->next) {
2949 unsigned long long esize;
2950 unsigned long long pos;
2951 struct mdinfo *d2;
2952 int is_global = 0;
2953 int is_dedicated = 0;
2954 struct extent *ex;
2955 int j;
2956 /* If in this array, skip */
2957 for (d2 = a->info.devs ; d2 ; d2 = d2->next)
2958 if (d2->disk.major == dl->major &&
2959 d2->disk.minor == dl->minor) {
2960 printf("%x:%x already in array\n", dl->major, dl->minor);
2961 break;
2962 }
2963 if (d2)
2964 continue;
2965 if (ddf->phys->entries[dl->pdnum].type &
2966 __cpu_to_be16(DDF_Spare)) {
2967 /* Check spare assign record */
2968 if (dl->spare) {
2969 if (dl->spare->type & DDF_spare_dedicated) {
2970 /* check spare_ents for guid */
2971 for (j = 0 ;
2972 j < __be16_to_cpu(dl->spare->populated);
2973 j++) {
2974 if (memcmp(dl->spare->spare_ents[j].guid,
2975 ddf->virt->entries[a->info.container_member].guid,
2976 DDF_GUID_LEN) == 0)
2977 is_dedicated = 1;
2978 }
2979 } else
2980 is_global = 1;
2981 }
2982 } else if (ddf->phys->entries[dl->pdnum].type &
2983 __cpu_to_be16(DDF_Global_Spare)) {
2984 is_global = 1;
2985 }
2986 if ( ! (is_dedicated ||
2987 (is_global && global_ok))) {
2988 printf("%x:%x not suitable: %d %d\n", dl->major, dl->minor,
2989 is_dedicated, is_global);
2990 continue;
2991 }
2992
2993 /* We are allowed to use this device - is there space?
2994 * We need a->info.component_size sectors */
2995 ex = get_extents(ddf, dl);
2996 if (!ex) {
2997 printf("cannot get extents\n");
2998 continue;
2999 }
3000 j = 0; pos = 0;
3001 esize = 0;
3002
3003 do {
3004 esize = ex[j].start - pos;
3005 if (esize >= a->info.component_size)
3006 break;
3007 pos = ex[i].start + ex[i].size;
3008 i++;
3009 } while (ex[i-1].size);
3010
3011 free(ex);
3012 if (esize < a->info.component_size) {
3013 printf("%x:%x has no room: %llu %llu\n", dl->major, dl->minor,
3014 esize, a->info.component_size);
3015 /* No room */
3016 continue;
3017 }
3018
3019 /* Cool, we have a device with some space at pos */
3020 di = malloc(sizeof(*di));
3021 memset(di, 0, sizeof(*di));
3022 di->disk.number = i;
3023 di->disk.raid_disk = i;
3024 di->disk.major = dl->major;
3025 di->disk.minor = dl->minor;
3026 di->disk.state = 0;
3027 di->data_offset = pos;
3028 di->component_size = a->info.component_size;
3029 di->container_member = dl->pdnum;
3030 di->next = rv;
3031 rv = di;
3032 printf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
3033 i, pos);
3034
3035 break;
3036 }
3037 if (!dl && ! global_ok) {
3038 /* not enough dedicated spares, try global */
3039 global_ok = 1;
3040 dl = ddf->dlist;
3041 goto again;
3042 }
3043 }
3044
3045 if (!rv)
3046 /* No spares found */
3047 return rv;
3048 /* Now 'rv' has a list of devices to return.
3049 * Create a metadata_update record to update the
3050 * phys_refnum and lba_offset values
3051 */
3052 mu = malloc(sizeof(*mu));
3053 mu->buf = malloc(ddf->conf_rec_len * 512);
3054 mu->space = malloc(sizeof(struct vcl));
3055 mu->len = ddf->conf_rec_len;
3056 mu->next = *updates;
3057 vc = find_vdcr(ddf, a->info.container_member);
3058 memcpy(mu->buf, vc, ddf->conf_rec_len * 512);
3059
3060 vc = (struct vd_config*)mu->buf;
3061 lba = (__u64*)&vc->phys_refnum[ddf->mppe];
3062 for (di = rv ; di ; di = di->next) {
3063 vc->phys_refnum[di->disk.raid_disk] =
3064 ddf->phys->entries[dl->pdnum].refnum;
3065 lba[di->disk.raid_disk] = di->data_offset;
3066 }
3067 *updates = mu;
3068 return rv;
3069 }
3070
3071 struct superswitch super_ddf = {
3072 #ifndef MDASSEMBLE
3073 .examine_super = examine_super_ddf,
3074 .brief_examine_super = brief_examine_super_ddf,
3075 .detail_super = detail_super_ddf,
3076 .brief_detail_super = brief_detail_super_ddf,
3077 .validate_geometry = validate_geometry_ddf,
3078 #endif
3079 .match_home = match_home_ddf,
3080 .uuid_from_super= uuid_from_super_ddf,
3081 .getinfo_super = getinfo_super_ddf,
3082 .update_super = update_super_ddf,
3083
3084 .avail_size = avail_size_ddf,
3085
3086 .compare_super = compare_super_ddf,
3087
3088 .load_super = load_super_ddf,
3089 .init_super = init_zero_ddf,
3090 .store_super = store_zero_ddf,
3091 .free_super = free_super_ddf,
3092 .match_metadata_desc = match_metadata_desc_ddf,
3093 .getinfo_super_n = getinfo_super_n_container,
3094
3095
3096 .major = 1000,
3097 .swapuuid = 0,
3098 .external = 1,
3099
3100 /* for mdmon */
3101 .open_new = ddf_open_new,
3102 .set_array_state= ddf_set_array_state,
3103 .set_disk = ddf_set_disk,
3104 .sync_metadata = ddf_sync_metadata,
3105 .process_update = ddf_process_update,
3106 .activate_spare = ddf_activate_spare,
3107
3108 };
3109
3110 /* Super_ddf_container is set by validate_geometry_ddf when given a
3111 * device that is not part of any array
3112 */
3113 struct superswitch super_ddf_container = {
3114 #ifndef MDASSEMBLE
3115 .validate_geometry = validate_geometry_ddf_container,
3116 .write_init_super = write_init_super_ddf,
3117 #endif
3118
3119 .load_super = load_super_ddf,
3120 .init_super = init_super_ddf,
3121 .add_to_super = add_to_super_ddf,
3122 .getinfo_super = getinfo_super_ddf,
3123
3124 .free_super = free_super_ddf,
3125
3126 .container_content = container_content_ddf,
3127 .getinfo_super_n = getinfo_super_n_container,
3128
3129 .major = 1000,
3130 .swapuuid = 0,
3131 .external = 1,
3132 };
3133
3134 struct superswitch super_ddf_bvd = {
3135 #ifndef MDASSEMBLE
3136 // .detail_super = detail_super_ddf_bvd,
3137 // .brief_detail_super = brief_detail_super_ddf_bvd,
3138 .validate_geometry = validate_geometry_ddf_bvd,
3139 .write_init_super = write_init_super_ddf,
3140 #endif
3141 .update_super = update_super_ddf,
3142 .init_super = init_super_ddf_bvd,
3143 .add_to_super = add_to_super_ddf_bvd,
3144 .getinfo_super = getinfo_super_ddf_bvd,
3145 .getinfo_super_n = getinfo_super_n_bvd,
3146
3147 .load_super = load_super_ddf,
3148 .free_super = free_super_ddf,
3149 .match_metadata_desc = match_metadata_desc_ddf_bvd,
3150
3151
3152 .major = 1001,
3153 .swapuuid = 0,
3154 .external = 2,
3155 };
3156
3157 struct superswitch super_ddf_svd = {
3158 #ifndef MDASSEMBLE
3159 // .detail_super = detail_super_ddf_svd,
3160 // .brief_detail_super = brief_detail_super_ddf_svd,
3161 .validate_geometry = validate_geometry_ddf_svd,
3162 #endif
3163 .update_super = update_super_ddf,
3164 .init_super = init_super_ddf,
3165
3166 .load_super = load_super_ddf,
3167 .free_super = free_super_ddf,
3168 .match_metadata_desc = match_metadata_desc_ddf_svd,
3169
3170 .major = 1002,
3171 .swapuuid = 0,
3172 .external = 2,
3173 };