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