2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2006-2007 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neil@brown.name>
24 * Specifications for DDF takes from Common RAID DDF Specification Revision 1.2
25 * (July 28 2006). Reused by permission of SNIA.
28 #define HAVE_STDINT_H 1
34 static inline int ROUND_UP(int a
, int base
)
36 return ((a
+base
-1)/base
)*base
;
39 /* a non-official T10 name for creation GUIDs */
40 static char T10
[] = "Linux-MD";
42 /* DDF timestamps are 1980 based, so we need to add
43 * second-in-decade-of-seventies to convert to linux timestamps.
44 * 10 years with 2 leap years.
46 #define DECADE (3600*24*(365*10+2))
49 const unsigned char *buf
,
52 /* The DDF metadata handling.
53 * DDF metadata lives at the end of the device.
54 * The last 512 byte block provides an 'anchor' which is used to locate
55 * the rest of the metadata which usually lives immediately behind the anchor.
58 * - all multibyte numeric fields are bigendian.
59 * - all strings are space padded.
63 /* Primary Raid Level (PRL) */
64 #define DDF_RAID0 0x00
65 #define DDF_RAID1 0x01
66 #define DDF_RAID3 0x03
67 #define DDF_RAID4 0x04
68 #define DDF_RAID5 0x05
69 #define DDF_RAID1E 0x11
71 #define DDF_CONCAT 0x1f
72 #define DDF_RAID5E 0x15
73 #define DDF_RAID5EE 0x25
74 #define DDF_RAID6 0x16 /* Vendor unique layout */
76 /* Raid Level Qualifier (RLQ) */
77 #define DDF_RAID0_SIMPLE 0x00
78 #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
79 #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
80 #define DDF_RAID3_0 0x00 /* parity in first extent */
81 #define DDF_RAID3_N 0x01 /* parity in last extent */
82 #define DDF_RAID4_0 0x00 /* parity in first extent */
83 #define DDF_RAID4_N 0x01 /* parity in last extent */
84 /* these apply to raid5e and raid5ee as well */
85 #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
86 #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
87 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
89 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
90 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
92 /* Secondary RAID Level (SRL) */
93 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
94 #define DDF_2MIRRORED 0x01
95 #define DDF_2CONCAT 0x02
96 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
99 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
100 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
101 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
102 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
103 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
104 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
105 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
106 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
107 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
108 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
110 #define DDF_GUID_LEN 24
111 #define DDF_REVISION "01.00.00"
114 __u32 magic
; /* DDF_HEADER_MAGIC */
116 char guid
[DDF_GUID_LEN
];
117 char revision
[8]; /* 01.00.00 */
118 __u32 seq
; /* starts at '1' */
123 __u8 pad0
; /* 0xff */
124 __u8 pad1
[12]; /* 12 * 0xff */
125 /* 64 bytes so far */
126 __u8 header_ext
[32]; /* reserved: fill with 0xff */
130 __u8 pad2
[3]; /* 0xff */
131 __u32 workspace_len
; /* sectors for vendor space -
132 * at least 32768(sectors) */
134 __u16 max_pd_entries
; /* one of 15, 63, 255, 1023, 4095 */
135 __u16 max_vd_entries
; /* 2^(4,6,8,10,12)-1 : i.e. as above */
136 __u16 max_partitions
; /* i.e. max num of configuration
137 record entries per disk */
138 __u16 config_record_len
; /* 1 +ROUNDUP(max_primary_element_entries
140 __u16 max_primary_element_entries
; /* 16, 64, 256, 1024, or 4096 */
141 __u8 pad3
[54]; /* 0xff */
142 /* 192 bytes so far */
143 __u32 controller_section_offset
;
144 __u32 controller_section_length
;
145 __u32 phys_section_offset
;
146 __u32 phys_section_length
;
147 __u32 virt_section_offset
;
148 __u32 virt_section_length
;
149 __u32 config_section_offset
;
150 __u32 config_section_length
;
151 __u32 data_section_offset
;
152 __u32 data_section_length
;
153 __u32 bbm_section_offset
;
154 __u32 bbm_section_length
;
155 __u32 diag_space_offset
;
156 __u32 diag_space_length
;
159 /* 256 bytes so far */
160 __u8 pad4
[256]; /* 0xff */
164 #define DDF_HEADER_ANCHOR 0x00
165 #define DDF_HEADER_PRIMARY 0x01
166 #define DDF_HEADER_SECONDARY 0x02
168 /* The content of the 'controller section' - global scope */
169 struct ddf_controller_data
{
170 __u32 magic
; /* DDF_CONTROLLER_MAGIC */
172 char guid
[DDF_GUID_LEN
];
173 struct controller_type
{
180 __u8 pad
[8]; /* 0xff */
181 __u8 vendor_data
[448];
184 /* The content of phys_section - global scope */
186 __u32 magic
; /* DDF_PHYS_RECORDS_MAGIC */
191 struct phys_disk_entry
{
192 char guid
[DDF_GUID_LEN
];
196 __u64 config_size
; /* DDF structures must be after here */
197 char path
[18]; /* another horrible structure really */
202 /* phys_disk_entry.type is a bitmap - bigendian remember */
203 #define DDF_Forced_PD_GUID 1
204 #define DDF_Active_in_VD 2
205 #define DDF_Global_Spare 4 /* VD_CONF records are ignored */
206 #define DDF_Spare 8 /* overrides Global_spare */
207 #define DDF_Foreign 16
208 #define DDF_Legacy 32 /* no DDF on this device */
210 #define DDF_Interface_mask 0xf00
211 #define DDF_Interface_SCSI 0x100
212 #define DDF_Interface_SAS 0x200
213 #define DDF_Interface_SATA 0x300
214 #define DDF_Interface_FC 0x400
216 /* phys_disk_entry.state is a bigendian bitmap */
218 #define DDF_Failed 2 /* overrides 1,4,8 */
219 #define DDF_Rebuilding 4
220 #define DDF_Transition 8
222 #define DDF_ReadErrors 32
223 #define DDF_Missing 64
225 /* The content of the virt_section global scope */
226 struct virtual_disk
{
227 __u32 magic
; /* DDF_VIRT_RECORDS_MAGIC */
229 __u16 populated_vdes
;
232 struct virtual_entry
{
233 char guid
[DDF_GUID_LEN
];
235 __u16 pad0
; /* 0xffff */
245 /* virtual_entry.type is a bitmap - bigendian */
247 #define DDF_Enforce_Groups 2
248 #define DDF_Unicode 4
249 #define DDF_Owner_Valid 8
251 /* virtual_entry.state is a bigendian bitmap */
252 #define DDF_state_mask 0x7
253 #define DDF_state_optimal 0x0
254 #define DDF_state_degraded 0x1
255 #define DDF_state_deleted 0x2
256 #define DDF_state_missing 0x3
257 #define DDF_state_failed 0x4
258 #define DDF_state_part_optimal 0x5
260 #define DDF_state_morphing 0x8
261 #define DDF_state_inconsistent 0x10
263 /* virtual_entry.init_state is a bigendian bitmap */
264 #define DDF_initstate_mask 0x03
265 #define DDF_init_not 0x00
266 #define DDF_init_quick 0x01 /* initialisation is progress.
267 * i.e. 'state_inconsistent' */
268 #define DDF_init_full 0x02
270 #define DDF_access_mask 0xc0
271 #define DDF_access_rw 0x00
272 #define DDF_access_ro 0x80
273 #define DDF_access_blocked 0xc0
275 /* The content of the config_section - local scope
276 * It has multiple records each config_record_len sectors
277 * They can be vd_config or spare_assign
281 __u32 magic
; /* DDF_VD_CONF_MAGIC */
283 char guid
[DDF_GUID_LEN
];
287 __u16 prim_elmnt_count
;
288 __u8 chunk_shift
; /* 0 == 512, 1==1024 etc */
291 __u8 sec_elmnt_count
;
294 __u64 blocks
; /* blocks per component could be different
295 * on different component devices...(only
296 * for concat I hope) */
297 __u64 array_blocks
; /* blocks in array */
305 __u8 v0
[32]; /* reserved- 0xff */
306 __u8 v1
[32]; /* reserved- 0xff */
307 __u8 v2
[16]; /* reserved- 0xff */
308 __u8 v3
[16]; /* reserved- 0xff */
310 __u32 phys_refnum
[0]; /* refnum of each disk in sequence */
311 /*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a
312 bvd are always the same size */
315 /* vd_config.cache_pol[7] is a bitmap */
316 #define DDF_cache_writeback 1 /* else writethrough */
317 #define DDF_cache_wadaptive 2 /* only applies if writeback */
318 #define DDF_cache_readahead 4
319 #define DDF_cache_radaptive 8 /* only if doing read-ahead */
320 #define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
321 #define DDF_cache_wallowed 32 /* enable write caching */
322 #define DDF_cache_rallowed 64 /* enable read caching */
324 struct spare_assign
{
325 __u32 magic
; /* DDF_SPARE_ASSIGN_MAGIC */
330 __u16 populated
; /* SAEs used */
331 __u16 max
; /* max SAEs */
333 struct spare_assign_entry
{
334 char guid
[DDF_GUID_LEN
];
335 __u16 secondary_element
;
339 /* spare_assign.type is a bitmap */
340 #define DDF_spare_dedicated 0x1 /* else global */
341 #define DDF_spare_revertible 0x2 /* else committable */
342 #define DDF_spare_active 0x4 /* else not active */
343 #define DDF_spare_affinity 0x8 /* enclosure affinity */
345 /* The data_section contents - local scope */
347 __u32 magic
; /* DDF_PHYS_DATA_MAGIC */
349 char guid
[DDF_GUID_LEN
];
350 __u32 refnum
; /* crc of some magic drive data ... */
351 __u8 forced_ref
; /* set when above was not result of magic */
352 __u8 forced_guid
; /* set if guid was forced rather than magic */
357 /* bbm_section content */
358 struct bad_block_log
{
365 struct mapped_block
{
366 __u64 defective_start
;
367 __u32 replacement_start
;
373 /* Struct for internally holding ddf structures */
374 /* The DDF structure stored on each device is potentially
375 * quite different, as some data is global and some is local.
376 * The global data is:
379 * - Physical disk records
380 * - Virtual disk records
382 * - Configuration records
383 * - Physical Disk data section
384 * ( and Bad block and vendor which I don't care about yet).
386 * The local data is parsed into separate lists as it is read
387 * and reconstructed for writing. This means that we only need
388 * to make config changes once and they are automatically
389 * propagated to all devices.
390 * Note that the ddf_super has space of the conf and disk data
391 * for this disk and also for a list of all such data.
392 * The list is only used for the superblock that is being
393 * built in Create or Assemble to describe the whole array.
396 struct ddf_header anchor
, primary
, secondary
, *active
;
397 struct ddf_controller_data controller
;
398 struct phys_disk
*phys
;
399 struct virtual_disk
*virt
;
401 int max_part
, mppe
, conf_rec_len
;
404 __u64
*lba_offset
; /* location in 'conf' of
406 struct vd_config conf
;
407 } *conflist
, *newconf
;
408 int conf_num
; /* Index into 'virt' of entry matching 'newconf' */
411 struct disk_data disk
;
415 int pdnum
; /* index in ->phys */
416 struct spare_assign
*spare
;
417 struct vcl
*vlist
[0]; /* max_part in size */
422 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
425 extern struct superswitch super_ddf_container
, super_ddf_bvd
, super_ddf
;
427 static int calc_crc(void *buf
, int len
)
429 /* crcs are always at the same place as in the ddf_header */
430 struct ddf_header
*ddf
= buf
;
431 __u32 oldcrc
= ddf
->crc
;
433 ddf
->crc
= 0xffffffff;
435 newcrc
= crc32(0, buf
, len
);
440 static int load_ddf_header(int fd
, unsigned long long lba
,
441 unsigned long long size
,
443 struct ddf_header
*hdr
, struct ddf_header
*anchor
)
445 /* read a ddf header (primary or secondary) from fd/lba
446 * and check that it is consistent with anchor
448 * magic, crc, guid, rev, and LBA's header_type, and
449 * everything after header_type must be the same
454 if (lseek64(fd
, lba
<<9, 0) < 0)
457 if (read(fd
, hdr
, 512) != 512)
460 if (hdr
->magic
!= DDF_HEADER_MAGIC
)
462 if (calc_crc(hdr
, 512) != hdr
->crc
)
464 if (memcmp(anchor
->guid
, hdr
->guid
, DDF_GUID_LEN
) != 0 ||
465 memcmp(anchor
->revision
, hdr
->revision
, 8) != 0 ||
466 anchor
->primary_lba
!= hdr
->primary_lba
||
467 anchor
->secondary_lba
!= hdr
->secondary_lba
||
469 memcmp(anchor
->pad2
, hdr
->pad2
, 512 -
470 offsetof(struct ddf_header
, pad2
)) != 0)
473 /* Looks good enough to me... */
477 static void *load_section(int fd
, struct ddf_super
*super
, void *buf
,
478 __u32 offset_be
, __u32 len_be
, int check
)
480 unsigned long long offset
= __be32_to_cpu(offset_be
);
481 unsigned long long len
= __be32_to_cpu(len_be
);
482 int dofree
= (buf
== NULL
);
485 if (len
!= 2 && len
!= 8 && len
!= 32
486 && len
!= 128 && len
!= 512)
492 /* All pre-allocated sections are a single block */
496 buf
= malloc(len
<<9);
500 if (super
->active
->type
== 1)
501 offset
+= __be64_to_cpu(super
->active
->primary_lba
);
503 offset
+= __be64_to_cpu(super
->active
->secondary_lba
);
505 if (lseek64(fd
, offset
<<9, 0) != (offset
<<9)) {
510 if (read(fd
, buf
, len
<<9) != (len
<<9)) {
518 static int load_ddf_headers(int fd
, struct ddf_super
*super
, char *devname
)
520 unsigned long long dsize
;
522 get_dev_size(fd
, NULL
, &dsize
);
524 if (lseek64(fd
, dsize
-512, 0) < 0) {
527 Name
": Cannot seek to anchor block on %s: %s\n",
528 devname
, strerror(errno
));
531 if (read(fd
, &super
->anchor
, 512) != 512) {
534 Name
": Cannot read anchor block on %s: %s\n",
535 devname
, strerror(errno
));
538 if (super
->anchor
.magic
!= DDF_HEADER_MAGIC
) {
540 fprintf(stderr
, Name
": no DDF anchor found on %s\n",
544 if (calc_crc(&super
->anchor
, 512) != super
->anchor
.crc
) {
546 fprintf(stderr
, Name
": bad CRC on anchor on %s\n",
550 if (memcmp(super
->anchor
.revision
, DDF_REVISION
, 8) != 0) {
552 fprintf(stderr
, Name
": can only support super revision"
553 " %.8s, not %.8s on %s\n",
554 DDF_REVISION
, super
->anchor
.revision
, devname
);
557 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.primary_lba
),
559 &super
->primary
, &super
->anchor
) == 0) {
562 Name
": Failed to load primary DDF header "
566 super
->active
= &super
->primary
;
567 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.secondary_lba
),
569 &super
->secondary
, &super
->anchor
)) {
570 if ((__be32_to_cpu(super
->primary
.seq
)
571 < __be32_to_cpu(super
->secondary
.seq
) &&
572 !super
->secondary
.openflag
)
573 || (__be32_to_cpu(super
->primary
.seq
)
574 == __be32_to_cpu(super
->secondary
.seq
) &&
575 super
->primary
.openflag
&& !super
->secondary
.openflag
)
577 super
->active
= &super
->secondary
;
582 static int load_ddf_global(int fd
, struct ddf_super
*super
, char *devname
)
585 ok
= load_section(fd
, super
, &super
->controller
,
586 super
->active
->controller_section_offset
,
587 super
->active
->controller_section_length
,
589 super
->phys
= load_section(fd
, super
, NULL
,
590 super
->active
->phys_section_offset
,
591 super
->active
->phys_section_length
,
593 super
->pdsize
= __be32_to_cpu(super
->active
->phys_section_length
) * 512;
595 super
->virt
= load_section(fd
, super
, NULL
,
596 super
->active
->virt_section_offset
,
597 super
->active
->virt_section_length
,
599 super
->vdsize
= __be32_to_cpu(super
->active
->virt_section_length
) * 512;
609 super
->conflist
= NULL
;
612 super
->max_part
= __be16_to_cpu(super
->active
->max_partitions
);
613 super
->mppe
= __be16_to_cpu(super
->active
->max_primary_element_entries
);
614 super
->conf_rec_len
= __be16_to_cpu(super
->active
->config_record_len
);
618 static int load_ddf_local(int fd
, struct ddf_super
*super
,
619 char *devname
, int keep
)
627 /* First the local disk info */
628 dl
= malloc(sizeof(*dl
) +
629 (super
->max_part
) * sizeof(dl
->vlist
[0]));
631 load_section(fd
, super
, &dl
->disk
,
632 super
->active
->data_section_offset
,
633 super
->active
->data_section_length
,
635 dl
->devname
= devname
? strdup(devname
) : NULL
;
638 dl
->major
= major(stb
.st_rdev
);
639 dl
->minor
= minor(stb
.st_rdev
);
640 dl
->next
= super
->dlist
;
641 dl
->fd
= keep
? fd
: -1;
643 for (i
=0 ; i
< super
->max_part
; i
++)
647 for (i
=0; i
< __be16_to_cpu(super
->active
->max_pd_entries
); i
++)
648 if (memcmp(super
->phys
->entries
[i
].guid
,
649 dl
->disk
.guid
, DDF_GUID_LEN
) == 0)
653 /* Now the config list. */
654 /* 'conf' is an array of config entries, some of which are
655 * probably invalid. Those which are good need to be copied into
659 conf
= load_section(fd
, super
, NULL
,
660 super
->active
->config_section_offset
,
661 super
->active
->config_section_length
,
666 i
< __be32_to_cpu(super
->active
->config_section_length
);
667 i
+= super
->conf_rec_len
) {
668 struct vd_config
*vd
=
669 (struct vd_config
*)((char*)conf
+ i
*512);
672 if (vd
->magic
== DDF_SPARE_ASSIGN_MAGIC
) {
675 dl
->spare
= malloc(super
->conf_rec_len
*512);
676 memcpy(dl
->spare
, vd
, super
->conf_rec_len
*512);
679 if (vd
->magic
!= DDF_VD_CONF_MAGIC
)
681 for (vcl
= super
->conflist
; vcl
; vcl
= vcl
->next
) {
682 if (memcmp(vcl
->conf
.guid
,
683 vd
->guid
, DDF_GUID_LEN
) == 0)
688 dl
->vlist
[vnum
++] = vcl
;
689 if (__be32_to_cpu(vd
->seqnum
) <=
690 __be32_to_cpu(vcl
->conf
.seqnum
))
693 vcl
= malloc(super
->conf_rec_len
*512 +
694 offsetof(struct vcl
, conf
));
695 vcl
->next
= super
->conflist
;
696 super
->conflist
= vcl
;
697 dl
->vlist
[vnum
++] = vcl
;
699 memcpy(&vcl
->conf
, vd
, super
->conf_rec_len
*512);
700 vcl
->lba_offset
= (__u64
*)
701 &vcl
->conf
.phys_refnum
[super
->mppe
];
709 static int load_super_ddf_all(struct supertype
*st
, int fd
,
710 void **sbp
, char *devname
, int keep_fd
);
712 static int load_super_ddf(struct supertype
*st
, int fd
,
715 unsigned long long dsize
;
716 struct ddf_super
*super
;
720 if (load_super_ddf_all(st
, fd
, &st
->sb
, devname
, 1) == 0)
724 if (get_dev_size(fd
, devname
, &dsize
) == 0)
727 /* 32M is a lower bound */
728 if (dsize
<= 32*1024*1024) {
731 Name
": %s is too small for ddf: "
732 "size is %llu sectors.\n",
740 Name
": %s is an odd size for ddf: "
741 "size is %llu bytes.\n",
747 super
= malloc(sizeof(*super
));
749 fprintf(stderr
, Name
": malloc of %zu failed.\n",
753 memset(super
, 0, sizeof(*super
));
755 rv
= load_ddf_headers(fd
, super
, devname
);
761 /* Have valid headers and have chosen the best. Let's read in the rest*/
763 rv
= load_ddf_global(fd
, super
, devname
);
768 Name
": Failed to load all information "
769 "sections on %s\n", devname
);
774 load_ddf_local(fd
, super
, devname
, 0);
776 /* Should possibly check the sections .... */
779 if (st
->ss
== NULL
) {
781 st
->minor_version
= 0;
788 static void free_super_ddf(struct supertype
*st
)
790 struct ddf_super
*ddf
= st
->sb
;
795 while (ddf
->conflist
) {
796 struct vcl
*v
= ddf
->conflist
;
797 ddf
->conflist
= v
->next
;
801 struct dl
*d
= ddf
->dlist
;
802 ddf
->dlist
= d
->next
;
813 static struct supertype
*match_metadata_desc_ddf(char *arg
)
815 /* 'ddf' only support containers */
816 struct supertype
*st
;
817 if (strcmp(arg
, "ddf") != 0 &&
818 strcmp(arg
, "default") != 0
822 st
= malloc(sizeof(*st
));
825 st
->minor_version
= 0;
830 static struct supertype
*match_metadata_desc_ddf_bvd(char *arg
)
832 struct supertype
*st
;
833 if (strcmp(arg
, "ddf/bvd") != 0 &&
834 strcmp(arg
, "bvd") != 0 &&
835 strcmp(arg
, "default") != 0
839 st
= malloc(sizeof(*st
));
840 st
->ss
= &super_ddf_bvd
;
842 st
->minor_version
= 0;
846 static struct supertype
*match_metadata_desc_ddf_svd(char *arg
)
848 struct supertype
*st
;
849 if (strcmp(arg
, "ddf/svd") != 0 &&
850 strcmp(arg
, "svd") != 0 &&
851 strcmp(arg
, "default") != 0
855 st
= malloc(sizeof(*st
));
856 st
->ss
= &super_ddf_svd
;
858 st
->minor_version
= 0;
865 static mapping_t ddf_state
[] = {
871 { "Partially Optimal", 5},
877 static mapping_t ddf_init_state
[] = {
878 { "Not Initialised", 0},
879 { "QuickInit in Progress", 1},
880 { "Fully Initialised", 2},
884 static mapping_t ddf_access
[] = {
888 { "Blocked (no access)", 3},
892 static mapping_t ddf_level
[] = {
893 { "RAID0", DDF_RAID0
},
894 { "RAID1", DDF_RAID1
},
895 { "RAID3", DDF_RAID3
},
896 { "RAID4", DDF_RAID4
},
897 { "RAID5", DDF_RAID5
},
898 { "RAID1E",DDF_RAID1E
},
900 { "CONCAT",DDF_CONCAT
},
901 { "RAID5E",DDF_RAID5E
},
902 { "RAID5EE",DDF_RAID5EE
},
903 { "RAID6", DDF_RAID6
},
906 static mapping_t ddf_sec_level
[] = {
907 { "Striped", DDF_2STRIPED
},
908 { "Mirrored", DDF_2MIRRORED
},
909 { "Concat", DDF_2CONCAT
},
910 { "Spanned", DDF_2SPANNED
},
918 static struct num_mapping ddf_level_num
[] = {
921 { DDF_RAID3
, LEVEL_UNSUPPORTED
},
924 { DDF_RAID1E
, LEVEL_UNSUPPORTED
},
925 { DDF_JBOD
, LEVEL_UNSUPPORTED
},
926 { DDF_CONCAT
, LEVEL_LINEAR
},
927 { DDF_RAID5E
, LEVEL_UNSUPPORTED
},
928 { DDF_RAID5EE
, LEVEL_UNSUPPORTED
},
933 static int map_num1(struct num_mapping
*map
, int num
)
936 for (i
=0 ; map
[i
].num1
!= MAXINT
; i
++)
937 if (map
[i
].num1
== num
)
943 static void print_guid(char *guid
, int tstamp
)
945 /* A GUIDs are part (or all) ASCII and part binary.
946 * They tend to be space padded.
947 * We ignore trailing spaces and print numbers
948 * <0x20 and >=0x7f as \xXX
949 * Some GUIDs have a time stamp in bytes 16-19.
950 * We print that if appropriate
952 int l
= DDF_GUID_LEN
;
954 while (l
&& guid
[l
-1] == ' ')
956 for (i
=0 ; i
<l
; i
++) {
957 if (guid
[i
] >= 0x20 && guid
[i
] < 0x7f)
958 fputc(guid
[i
], stdout
);
960 fprintf(stdout
, "\\x%02x", guid
[i
]&255);
963 time_t then
= __be32_to_cpu(*(__u32
*)(guid
+16)) + DECADE
;
966 tm
= localtime(&then
);
967 strftime(tbuf
, 100, " (%D %T)",tm
);
972 static void examine_vd(int n
, struct ddf_super
*sb
, char *guid
)
974 int crl
= sb
->conf_rec_len
;
977 for (vcl
= sb
->conflist
; vcl
; vcl
= vcl
->next
) {
978 struct vd_config
*vc
= &vcl
->conf
;
980 if (calc_crc(vc
, crl
*512) != vc
->crc
)
982 if (memcmp(vc
->guid
, guid
, DDF_GUID_LEN
) != 0)
985 /* Ok, we know about this VD, let's give more details */
986 printf(" Raid Devices[%d] : %d\n", n
,
987 __be16_to_cpu(vc
->prim_elmnt_count
));
988 printf(" Chunk Size[%d] : %d sectors\n", n
,
989 1 << vc
->chunk_shift
);
990 printf(" Raid Level[%d] : %s\n", n
,
991 map_num(ddf_level
, vc
->prl
)?:"-unknown-");
992 if (vc
->sec_elmnt_count
!= 1) {
993 printf(" Secondary Position[%d] : %d of %d\n", n
,
994 vc
->sec_elmnt_seq
, vc
->sec_elmnt_count
);
995 printf(" Secondary Level[%d] : %s\n", n
,
996 map_num(ddf_sec_level
, vc
->srl
) ?: "-unknown-");
998 printf(" Device Size[%d] : %llu\n", n
,
999 __be64_to_cpu(vc
->blocks
)/2);
1000 printf(" Array Size[%d] : %llu\n", n
,
1001 __be64_to_cpu(vc
->array_blocks
)/2);
1005 static void examine_vds(struct ddf_super
*sb
)
1007 int cnt
= __be16_to_cpu(sb
->virt
->populated_vdes
);
1009 printf(" Virtual Disks : %d\n", cnt
);
1011 for (i
=0; i
<cnt
; i
++) {
1012 struct virtual_entry
*ve
= &sb
->virt
->entries
[i
];
1013 printf(" VD GUID[%d] : ", i
); print_guid(ve
->guid
, 1);
1015 printf(" unit[%d] : %d\n", i
, __be16_to_cpu(ve
->unit
));
1016 printf(" state[%d] : %s, %s%s\n", i
,
1017 map_num(ddf_state
, ve
->state
& 7),
1018 (ve
->state
& 8) ? "Morphing, ": "",
1019 (ve
->state
& 16)? "Not Consistent" : "Consistent");
1020 printf(" init state[%d] : %s\n", i
,
1021 map_num(ddf_init_state
, ve
->init_state
&3));
1022 printf(" access[%d] : %s\n", i
,
1023 map_num(ddf_access
, (ve
->init_state
>>6) & 3));
1024 printf(" Name[%d] : %.16s\n", i
, ve
->name
);
1025 examine_vd(i
, sb
, ve
->guid
);
1027 if (cnt
) printf("\n");
1030 static void examine_pds(struct ddf_super
*sb
)
1032 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1035 printf(" Physical Disks : %d\n", cnt
);
1037 for (i
=0 ; i
<cnt
; i
++) {
1038 struct phys_disk_entry
*pd
= &sb
->phys
->entries
[i
];
1039 int type
= __be16_to_cpu(pd
->type
);
1040 int state
= __be16_to_cpu(pd
->state
);
1042 printf(" PD GUID[%d] : ", i
); print_guid(pd
->guid
, 0);
1044 printf(" ref[%d] : %08x\n", i
,
1045 __be32_to_cpu(pd
->refnum
));
1046 printf(" mode[%d] : %s%s%s%s%s\n", i
,
1047 (type
&2) ? "active":"",
1048 (type
&4) ? "Global Spare":"",
1049 (type
&8) ? "spare" : "",
1050 (type
&16)? ", foreign" : "",
1051 (type
&32)? "pass-through" : "");
1052 printf(" state[%d] : %s%s%s%s%s%s%s\n", i
,
1053 (state
&1)? "Online": "Offline",
1054 (state
&2)? ", Failed": "",
1055 (state
&4)? ", Rebuilding": "",
1056 (state
&8)? ", in-transition": "",
1057 (state
&16)? ", SMART errors": "",
1058 (state
&32)? ", Unrecovered Read Errors": "",
1059 (state
&64)? ", Missing" : "");
1060 printf(" Avail Size[%d] : %llu K\n", i
,
1061 __be64_to_cpu(pd
->config_size
)>>1);
1062 for (dl
= sb
->dlist
; dl
; dl
= dl
->next
) {
1063 if (dl
->disk
.refnum
== pd
->refnum
) {
1064 char *dv
= map_dev(dl
->major
, dl
->minor
, 0);
1066 printf(" Device[%d] : %s\n",
1074 static void examine_super_ddf(struct supertype
*st
, char *homehost
)
1076 struct ddf_super
*sb
= st
->sb
;
1078 printf(" Magic : %08x\n", __be32_to_cpu(sb
->anchor
.magic
));
1079 printf(" Version : %.8s\n", sb
->anchor
.revision
);
1080 printf("Controller GUID : "); print_guid(sb
->controller
.guid
, 0);
1082 printf(" Container GUID : "); print_guid(sb
->anchor
.guid
, 1);
1084 printf(" Seq : %08x\n", __be32_to_cpu(sb
->active
->seq
));
1085 printf(" Redundant hdr : %s\n", sb
->secondary
.magic
== DDF_HEADER_MAGIC
1091 static void brief_examine_super_ddf(struct supertype
*st
)
1093 /* We just write a generic DDF ARRAY entry
1094 * The uuid is all hex, 6 groups of 4 bytes
1096 struct ddf_super
*ddf
= st
->sb
;
1098 printf("ARRAY /dev/ddf UUID=");
1099 for (i
= 0; i
< DDF_GUID_LEN
; i
++) {
1100 printf("%02x", ddf
->anchor
.guid
[i
]);
1101 if ((i
&3) == 0 && i
!= 0)
1107 static void detail_super_ddf(struct supertype
*st
, char *homehost
)
1110 * Could print DDF GUID
1111 * Need to find which array
1112 * If whole, briefly list all arrays
1117 static void brief_detail_super_ddf(struct supertype
*st
)
1119 /* FIXME I really need to know which array we are detailing.
1120 * Can that be stored in ddf_super??
1122 // struct ddf_super *ddf = st->sb;
1128 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1130 /* It matches 'this' host if the controller is a
1131 * Linux-MD controller with vendor_data matching
1134 struct ddf_super
*ddf
= st
->sb
;
1135 int len
= strlen(homehost
);
1137 return (memcmp(ddf
->controller
.guid
, T10
, 8) == 0 &&
1138 len
< sizeof(ddf
->controller
.vendor_data
) &&
1139 memcmp(ddf
->controller
.vendor_data
, homehost
,len
) == 0 &&
1140 ddf
->controller
.vendor_data
[len
] == 0);
1143 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
, int inst
)
1146 if (inst
< 0 || inst
> __be16_to_cpu(ddf
->virt
->populated_vdes
))
1148 for (v
= ddf
->conflist
; v
; v
= v
->next
)
1149 if (memcmp(v
->conf
.guid
,
1150 ddf
->virt
->entries
[inst
].guid
,
1156 static int find_phys(struct ddf_super
*ddf
, __u32 phys_refnum
)
1158 /* Find the entry in phys_disk which has the given refnum
1159 * and return it's index
1162 for (i
=0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
); i
++)
1163 if (ddf
->phys
->entries
[i
].refnum
== phys_refnum
)
1168 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
1170 /* The uuid returned here is used for:
1171 * uuid to put into bitmap file (Create, Grow)
1172 * uuid for backup header when saving critical section (Grow)
1173 * comparing uuids when re-adding a device into an array
1174 * For each of these we can make do with a truncated
1175 * or hashed uuid rather than the original, as long as
1177 * In each case the uuid required is that of the data-array,
1178 * not the device-set.
1179 * In the case of SVD we assume the BVD is of interest,
1180 * though that might be the case if a bitmap were made for
1181 * a mirrored SVD - worry about that later.
1182 * So we need to find the VD configuration record for the
1183 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1184 * The first 16 bytes of the sha1 of these is used.
1186 struct ddf_super
*ddf
= st
->sb
;
1187 struct vd_config
*vd
= find_vdcr(ddf
, ddf
->conf_num
);
1190 memset(uuid
, 0, sizeof (uuid
));
1193 struct sha1_ctx ctx
;
1194 sha1_init_ctx(&ctx
);
1195 sha1_process_bytes(&vd
->guid
, DDF_GUID_LEN
, &ctx
);
1196 if (vd
->sec_elmnt_count
> 1)
1197 sha1_process_bytes(&vd
->sec_elmnt_seq
, 1, &ctx
);
1198 sha1_finish_ctx(&ctx
, buf
);
1199 memcpy(uuid
, buf
, sizeof(uuid
));
1203 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
)
1205 struct ddf_super
*ddf
= st
->sb
;
1208 info
->array
.major_version
= 1000;
1209 info
->array
.minor_version
= 0; /* FIXME use ddf->revision somehow */
1210 info
->array
.patch_version
= 0;
1211 info
->array
.raid_disks
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1212 info
->array
.level
= LEVEL_CONTAINER
;
1213 info
->array
.layout
= 0;
1214 info
->array
.md_minor
= -1;
1215 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*(__u32
*)
1216 (ddf
->anchor
.guid
+16));
1217 info
->array
.utime
= 0;
1218 info
->array
.chunk_size
= 0;
1220 // info->data_offset = ???;
1221 // info->component_size = ???;
1223 info
->disk
.major
= 0;
1224 info
->disk
.minor
= 0;
1226 info
->disk
.number
= __be32_to_cpu(ddf
->dlist
->disk
.refnum
);
1227 info
->disk
.raid_disk
= -1;
1228 for (i
= 0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
) ; i
++)
1229 if (ddf
->phys
->entries
[i
].refnum
==
1230 ddf
->dlist
->disk
.refnum
) {
1231 info
->disk
.raid_disk
= i
;
1235 info
->disk
.number
= -1;
1236 // info->disk.raid_disk = find refnum in the table and use index;
1238 info
->disk
.state
= (1 << MD_DISK_SYNC
);
1240 info
->reshape_active
= 0;
1242 strcpy(info
->text_version
, "ddf");
1244 // uuid_from_super_ddf(info->uuid, sbv);
1246 // info->name[] ?? ;
1249 static void getinfo_super_n_container(struct supertype
*st
, struct mdinfo
*info
)
1251 /* just need offset and size */
1252 struct ddf_super
*ddf
= st
->sb
;
1253 int n
= info
->disk
.number
;
1255 info
->data_offset
= __be64_to_cpu(ddf
->phys
->entries
[n
].config_size
);
1256 info
->component_size
= 32*1024*1024 / 512;
1259 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
);
1261 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
)
1263 struct ddf_super
*ddf
= st
->sb
;
1264 struct vd_config
*vd
= find_vdcr(ddf
, info
->container_member
);
1266 /* FIXME this returns BVD info - what if we want SVD ?? */
1268 info
->array
.major_version
= 1000;
1269 info
->array
.minor_version
= 0; /* FIXME use ddf->revision somehow */
1270 info
->array
.patch_version
= 0;
1271 info
->array
.raid_disks
= __be16_to_cpu(vd
->prim_elmnt_count
);
1272 info
->array
.level
= map_num1(ddf_level_num
, vd
->prl
);
1273 info
->array
.layout
= rlq_to_layout(vd
->rlq
, vd
->prl
,
1274 info
->array
.raid_disks
);
1275 info
->array
.md_minor
= -1;
1276 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*(__u32
*)(vd
->guid
+16));
1277 info
->array
.utime
= DECADE
+ __be32_to_cpu(vd
->timestamp
);
1278 info
->array
.chunk_size
= 512 << vd
->chunk_shift
;
1280 // info->data_offset = ???;
1281 // info->component_size = ???;
1283 info
->disk
.major
= 0;
1284 info
->disk
.minor
= 0;
1285 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1286 // info->disk.raid_disk = find refnum in the table and use index;
1287 // info->disk.state = ???;
1289 info
->resync_start
= 0;
1290 if (!(ddf
->virt
->entries
[info
->container_member
].state
1291 & DDF_state_inconsistent
) &&
1292 (ddf
->virt
->entries
[info
->container_member
].init_state
1293 & DDF_initstate_mask
)
1295 info
->resync_start
= ~0ULL;
1297 uuid_from_super_ddf(st
, info
->uuid
);
1299 sprintf(info
->text_version
, "/%s/%d",
1300 devnum2devname(st
->container_dev
),
1301 info
->container_member
);
1303 // info->name[] ?? ;
1306 static void getinfo_super_n_bvd(struct supertype
*st
, struct mdinfo
*info
)
1308 /* Find the particular details for info->disk.raid_disk.
1309 * This includes data_offset, component_size,
1311 struct ddf_super
*ddf
= st
->sb
;
1312 __u64
*lba_offset
= ddf
->newconf
->lba_offset
;
1313 struct vd_config
*conf
= &ddf
->newconf
->conf
;
1314 info
->data_offset
= __be64_to_cpu(lba_offset
[info
->disk
.raid_disk
]);
1315 info
->component_size
= __be64_to_cpu(conf
->blocks
);
1318 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1320 char *devname
, int verbose
,
1321 int uuid_set
, char *homehost
)
1323 /* For 'assemble' and 'force' we need to return non-zero if any
1324 * change was made. For others, the return value is ignored.
1325 * Update options are:
1326 * force-one : This device looks a bit old but needs to be included,
1327 * update age info appropriately.
1328 * assemble: clear any 'faulty' flag to allow this device to
1330 * force-array: Array is degraded but being forced, mark it clean
1331 * if that will be needed to assemble it.
1333 * newdev: not used ????
1334 * grow: Array has gained a new device - this is currently for
1336 * resync: mark as dirty so a resync will happen.
1337 * uuid: Change the uuid of the array to match watch is given
1338 * homehost: update the recorded homehost
1339 * name: update the name - preserving the homehost
1340 * _reshape_progress: record new reshape_progress position.
1342 * Following are not relevant for this version:
1343 * sparc2.2 : update from old dodgey metadata
1344 * super-minor: change the preferred_minor number
1345 * summaries: update redundant counters.
1348 // struct ddf_super *ddf = st->sb;
1349 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1350 // struct virtual_entry *ve = find_ve(ddf);
1353 /* we don't need to handle "force-*" or "assemble" as
1354 * there is no need to 'trick' the kernel. We the metadata is
1355 * first updated to activate the array, all the implied modifications
1359 if (strcmp(update
, "grow") == 0) {
1362 if (strcmp(update
, "resync") == 0) {
1363 // info->resync_checkpoint = 0;
1365 /* We ignore UUID updates as they make even less sense
1368 if (strcmp(update
, "homehost") == 0) {
1369 /* homehost is stored in controller->vendor_data,
1370 * or it is when we are the vendor
1372 // if (info->vendor_is_local)
1373 // strcpy(ddf->controller.vendor_data, homehost);
1375 if (strcmp(update
, "name") == 0) {
1376 /* name is stored in virtual_entry->name */
1377 // memset(ve->name, ' ', 16);
1378 // strncpy(ve->name, info->name, 16);
1380 if (strcmp(update
, "_reshape_progress") == 0) {
1381 /* We don't support reshape yet */
1384 // update_all_csum(ddf);
1389 static void make_header_guid(char *guid
)
1393 /* Create a DDF Header of Virtual Disk GUID */
1395 /* 24 bytes of fiction required.
1396 * first 8 are a 'vendor-id' - "Linux-MD"
1397 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1398 * Remaining 8 random number plus timestamp
1400 memcpy(guid
, T10
, sizeof(T10
));
1401 stamp
= __cpu_to_be32(0xdeadbeef);
1402 memcpy(guid
+8, &stamp
, 4);
1403 stamp
= __cpu_to_be32(0);
1404 memcpy(guid
+12, &stamp
, 4);
1405 stamp
= __cpu_to_be32(time(0) - DECADE
);
1406 memcpy(guid
+16, &stamp
, 4);
1407 rfd
= open("/dev/urandom", O_RDONLY
);
1408 if (rfd
< 0 || read(rfd
, &stamp
, 4) != 4)
1410 memcpy(guid
+20, &stamp
, 4);
1411 if (rfd
>= 0) close(rfd
);
1413 static int init_super_ddf(struct supertype
*st
,
1414 mdu_array_info_t
*info
,
1415 unsigned long long size
, char *name
, char *homehost
,
1418 /* This is primarily called by Create when creating a new array.
1419 * We will then get add_to_super called for each component, and then
1420 * write_init_super called to write it out to each device.
1421 * For DDF, Create can create on fresh devices or on a pre-existing
1423 * To create on a pre-existing array a different method will be called.
1424 * This one is just for fresh drives.
1426 * We need to create the entire 'ddf' structure which includes:
1427 * DDF headers - these are easy.
1428 * Controller data - a Sector describing this controller .. not that
1429 * this is a controller exactly.
1430 * Physical Disk Record - one entry per device, so
1431 * leave plenty of space.
1432 * Virtual Disk Records - again, just leave plenty of space.
1433 * This just lists VDs, doesn't give details
1434 * Config records - describes the VDs that use this disk
1435 * DiskData - describes 'this' device.
1436 * BadBlockManagement - empty
1437 * Diag Space - empty
1438 * Vendor Logs - Could we put bitmaps here?
1441 struct ddf_super
*ddf
;
1444 int max_phys_disks
, max_virt_disks
;
1445 unsigned long long sector
;
1449 struct phys_disk
*pd
;
1450 struct virtual_disk
*vd
;
1452 ddf
= malloc(sizeof(*ddf
));
1453 ddf
->dlist
= NULL
; /* no physical disks yet */
1454 ddf
->conflist
= NULL
; /* No virtual disks yet */
1456 /* At least 32MB *must* be reserved for the ddf. So let's just
1457 * start 32MB from the end, and put the primary header there.
1458 * Don't do secondary for now.
1459 * We don't know exactly where that will be yet as it could be
1460 * different on each device. To just set up the lengths.
1464 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
1465 make_header_guid(ddf
->anchor
.guid
);
1467 memcpy(ddf
->anchor
.revision
, DDF_REVISION
, 8);
1468 ddf
->anchor
.seq
= __cpu_to_be32(1);
1469 ddf
->anchor
.timestamp
= __cpu_to_be32(time(0) - DECADE
);
1470 ddf
->anchor
.openflag
= 0xFF;
1471 ddf
->anchor
.foreignflag
= 0;
1472 ddf
->anchor
.enforcegroups
= 0; /* Is this best?? */
1473 ddf
->anchor
.pad0
= 0xff;
1474 memset(ddf
->anchor
.pad1
, 0xff, 12);
1475 memset(ddf
->anchor
.header_ext
, 0xff, 32);
1476 ddf
->anchor
.primary_lba
= ~(__u64
)0;
1477 ddf
->anchor
.secondary_lba
= ~(__u64
)0;
1478 ddf
->anchor
.type
= DDF_HEADER_ANCHOR
;
1479 memset(ddf
->anchor
.pad2
, 0xff, 3);
1480 ddf
->anchor
.workspace_len
= __cpu_to_be32(32768); /* Must be reserved */
1481 ddf
->anchor
.workspace_lba
= ~(__u64
)0; /* Put this at bottom
1482 of 32M reserved.. */
1483 max_phys_disks
= 1023; /* Should be enough */
1484 ddf
->anchor
.max_pd_entries
= __cpu_to_be16(max_phys_disks
);
1485 max_virt_disks
= 255;
1486 ddf
->anchor
.max_vd_entries
= __cpu_to_be16(max_virt_disks
); /* ?? */
1487 ddf
->anchor
.max_partitions
= __cpu_to_be16(64); /* ?? */
1489 ddf
->conf_rec_len
= 1 + 256 * 12 / 512;
1490 ddf
->anchor
.config_record_len
= __cpu_to_be16(ddf
->conf_rec_len
);
1491 ddf
->anchor
.max_primary_element_entries
= __cpu_to_be16(256);
1493 memset(ddf
->anchor
.pad3
, 0xff, 54);
1495 /* controller sections is one sector long immediately
1496 * after the ddf header */
1498 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
1499 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
1502 /* phys is 8 sectors after that */
1503 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
1504 sizeof(struct phys_disk_entry
)*max_phys_disks
,
1506 switch(pdsize
/512) {
1507 case 2: case 8: case 32: case 128: case 512: break;
1510 ddf
->anchor
.phys_section_offset
= __cpu_to_be32(sector
);
1511 ddf
->anchor
.phys_section_length
=
1512 __cpu_to_be32(pdsize
/512); /* max_primary_element_entries/8 */
1513 sector
+= pdsize
/512;
1515 /* virt is another 32 sectors */
1516 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
1517 sizeof(struct virtual_entry
) * max_virt_disks
,
1519 switch(vdsize
/512) {
1520 case 2: case 8: case 32: case 128: case 512: break;
1523 ddf
->anchor
.virt_section_offset
= __cpu_to_be32(sector
);
1524 ddf
->anchor
.virt_section_length
=
1525 __cpu_to_be32(vdsize
/512); /* max_vd_entries/8 */
1526 sector
+= vdsize
/512;
1528 clen
= (1 + 256*12/512) * (64+1);
1529 ddf
->anchor
.config_section_offset
= __cpu_to_be32(sector
);
1530 ddf
->anchor
.config_section_length
= __cpu_to_be32(clen
);
1533 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
1534 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
1537 ddf
->anchor
.bbm_section_length
= __cpu_to_be32(0);
1538 ddf
->anchor
.bbm_section_offset
= __cpu_to_be32(0xFFFFFFFF);
1539 ddf
->anchor
.diag_space_length
= __cpu_to_be32(0);
1540 ddf
->anchor
.diag_space_offset
= __cpu_to_be32(0xFFFFFFFF);
1541 ddf
->anchor
.vendor_length
= __cpu_to_be32(0);
1542 ddf
->anchor
.vendor_offset
= __cpu_to_be32(0xFFFFFFFF);
1544 memset(ddf
->anchor
.pad4
, 0xff, 256);
1546 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1547 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1549 ddf
->primary
.openflag
= 1; /* I guess.. */
1550 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1552 ddf
->secondary
.openflag
= 1; /* I guess.. */
1553 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1555 ddf
->active
= &ddf
->primary
;
1557 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
1559 /* 24 more bytes of fiction required.
1560 * first 8 are a 'vendor-id' - "Linux-MD"
1561 * Remaining 16 are serial number.... maybe a hostname would do?
1563 memcpy(ddf
->controller
.guid
, T10
, sizeof(T10
));
1564 gethostname(hostname
, sizeof(hostname
));
1565 hostname
[sizeof(hostname
) - 1] = 0;
1566 hostlen
= strlen(hostname
);
1567 memcpy(ddf
->controller
.guid
+ 24 - hostlen
, hostname
, hostlen
);
1568 for (i
= strlen(T10
) ; i
+hostlen
< 24; i
++)
1569 ddf
->controller
.guid
[i
] = ' ';
1571 ddf
->controller
.type
.vendor_id
= __cpu_to_be16(0xDEAD);
1572 ddf
->controller
.type
.device_id
= __cpu_to_be16(0xBEEF);
1573 ddf
->controller
.type
.sub_vendor_id
= 0;
1574 ddf
->controller
.type
.sub_device_id
= 0;
1575 memcpy(ddf
->controller
.product_id
, "What Is My PID??", 16);
1576 memset(ddf
->controller
.pad
, 0xff, 8);
1577 memset(ddf
->controller
.vendor_data
, 0xff, 448);
1579 pd
= ddf
->phys
= malloc(pdsize
);
1580 ddf
->pdsize
= pdsize
;
1582 memset(pd
, 0xff, pdsize
);
1583 memset(pd
, 0, sizeof(*pd
));
1584 pd
->magic
= DDF_PHYS_DATA_MAGIC
;
1585 pd
->used_pdes
= __cpu_to_be16(0);
1586 pd
->max_pdes
= __cpu_to_be16(max_phys_disks
);
1587 memset(pd
->pad
, 0xff, 52);
1589 vd
= ddf
->virt
= malloc(vdsize
);
1590 ddf
->vdsize
= vdsize
;
1591 memset(vd
, 0, vdsize
);
1592 vd
->magic
= DDF_VIRT_RECORDS_MAGIC
;
1593 vd
->populated_vdes
= __cpu_to_be16(0);
1594 vd
->max_vdes
= __cpu_to_be16(max_virt_disks
);
1595 memset(vd
->pad
, 0xff, 52);
1597 for (i
=0; i
<max_virt_disks
; i
++)
1598 memset(&vd
->entries
[i
], 0xff, sizeof(struct virtual_entry
));
1604 static int all_ff(char *guid
)
1607 for (i
= 0; i
< DDF_GUID_LEN
; i
++)
1608 if (guid
[i
] != (char)0xff)
1612 static int chunk_to_shift(int chunksize
)
1614 return ffs(chunksize
/512)-1;
1617 static int level_to_prl(int level
)
1620 case LEVEL_LINEAR
: return DDF_CONCAT
;
1621 case 0: return DDF_RAID0
;
1622 case 1: return DDF_RAID1
;
1623 case 4: return DDF_RAID4
;
1624 case 5: return DDF_RAID5
;
1625 case 6: return DDF_RAID6
;
1629 static int layout_to_rlq(int level
, int layout
, int raiddisks
)
1633 return DDF_RAID0_SIMPLE
;
1636 case 2: return DDF_RAID1_SIMPLE
;
1637 case 3: return DDF_RAID1_MULTI
;
1642 case 0: return DDF_RAID4_N
;
1648 case ALGORITHM_LEFT_ASYMMETRIC
:
1649 return DDF_RAID5_N_RESTART
;
1650 case ALGORITHM_RIGHT_ASYMMETRIC
:
1651 return DDF_RAID5_0_RESTART
;
1652 case ALGORITHM_LEFT_SYMMETRIC
:
1653 return DDF_RAID5_N_CONTINUE
;
1654 case ALGORITHM_RIGHT_SYMMETRIC
:
1655 return -1; /* not mentioned in standard */
1661 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
)
1665 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1667 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1675 return -1; /* FIXME this isn't checked */
1680 case DDF_RAID5_N_RESTART
:
1681 return ALGORITHM_LEFT_ASYMMETRIC
;
1682 case DDF_RAID5_0_RESTART
:
1683 return ALGORITHM_RIGHT_ASYMMETRIC
;
1684 case DDF_RAID5_N_CONTINUE
:
1685 return ALGORITHM_LEFT_SYMMETRIC
;
1693 static int init_super_ddf_bvd(struct supertype
*st
,
1694 mdu_array_info_t
*info
,
1695 unsigned long long size
,
1696 char *name
, char *homehost
,
1699 /* We are creating a BVD inside a pre-existing container.
1700 * so st->sb is already set.
1701 * We need to create a new vd_config and a new virtual_entry
1703 struct ddf_super
*ddf
= st
->sb
;
1705 struct virtual_entry
*ve
;
1707 struct vd_config
*vc
;
1709 if (__be16_to_cpu(ddf
->virt
->populated_vdes
)
1710 >= __be16_to_cpu(ddf
->virt
->max_vdes
)) {
1711 fprintf(stderr
, Name
": This ddf already has the "
1712 "maximum of %d virtual devices\n",
1713 __be16_to_cpu(ddf
->virt
->max_vdes
));
1717 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
1718 if (all_ff(ddf
->virt
->entries
[venum
].guid
))
1720 if (venum
== __be16_to_cpu(ddf
->virt
->max_vdes
)) {
1721 fprintf(stderr
, Name
": Cannot find spare slot for "
1722 "virtual disk - DDF is corrupt\n");
1725 ve
= &ddf
->virt
->entries
[venum
];
1726 ddf
->conf_num
= venum
;
1728 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
1729 * timestamp, random number
1731 make_header_guid(ve
->guid
);
1732 ve
->unit
= __cpu_to_be16(info
->md_minor
);
1734 ve
->guid_crc
= crc32(0, (unsigned char*)ddf
->anchor
.guid
, DDF_GUID_LEN
);
1736 ve
->state
= DDF_state_degraded
; /* Will be modified as devices are added */
1737 if (info
->state
& 1) /* clean */
1738 ve
->init_state
= DDF_init_full
;
1740 ve
->init_state
= DDF_init_not
;
1742 memset(ve
->pad1
, 0xff, 14);
1743 memset(ve
->name
, ' ', 16);
1745 strncpy(ve
->name
, name
, 16);
1746 ddf
->virt
->populated_vdes
=
1747 __cpu_to_be16(__be16_to_cpu(ddf
->virt
->populated_vdes
)+1);
1749 /* Now create a new vd_config */
1750 vcl
= malloc(offsetof(struct vcl
, conf
) + ddf
->conf_rec_len
* 512);
1751 vcl
->lba_offset
= (__u64
*) &vcl
->conf
.phys_refnum
[ddf
->mppe
];
1755 vc
->magic
= DDF_VD_CONF_MAGIC
;
1756 memcpy(vc
->guid
, ve
->guid
, DDF_GUID_LEN
);
1757 vc
->timestamp
= __cpu_to_be32(time(0)-DECADE
);
1758 vc
->seqnum
= __cpu_to_be32(1);
1759 memset(vc
->pad0
, 0xff, 24);
1760 vc
->prim_elmnt_count
= __cpu_to_be16(info
->raid_disks
);
1761 vc
->chunk_shift
= chunk_to_shift(info
->chunk_size
);
1762 vc
->prl
= level_to_prl(info
->level
);
1763 vc
->rlq
= layout_to_rlq(info
->level
, info
->layout
, info
->raid_disks
);
1764 vc
->sec_elmnt_count
= 1;
1765 vc
->sec_elmnt_seq
= 0;
1767 vc
->blocks
= __cpu_to_be64(info
->size
* 2);
1768 vc
->array_blocks
= __cpu_to_be64(
1769 calc_array_size(info
->level
, info
->raid_disks
, info
->layout
,
1770 info
->chunk_size
, info
->size
*2));
1771 memset(vc
->pad1
, 0xff, 8);
1772 vc
->spare_refs
[0] = 0xffffffff;
1773 vc
->spare_refs
[1] = 0xffffffff;
1774 vc
->spare_refs
[2] = 0xffffffff;
1775 vc
->spare_refs
[3] = 0xffffffff;
1776 vc
->spare_refs
[4] = 0xffffffff;
1777 vc
->spare_refs
[5] = 0xffffffff;
1778 vc
->spare_refs
[6] = 0xffffffff;
1779 vc
->spare_refs
[7] = 0xffffffff;
1780 memset(vc
->cache_pol
, 0, 8);
1782 memset(vc
->pad2
, 0xff, 3);
1783 memset(vc
->pad3
, 0xff, 52);
1784 memset(vc
->pad4
, 0xff, 192);
1785 memset(vc
->v0
, 0xff, 32);
1786 memset(vc
->v1
, 0xff, 32);
1787 memset(vc
->v2
, 0xff, 16);
1788 memset(vc
->v3
, 0xff, 16);
1789 memset(vc
->vendor
, 0xff, 32);
1791 memset(vc
->phys_refnum
, 0xff, 4*ddf
->mppe
);
1792 memset(vc
->phys_refnum
+(ddf
->mppe
* 4), 0x00, 8*ddf
->mppe
);
1794 vcl
->next
= ddf
->conflist
;
1795 ddf
->conflist
= vcl
;
1800 static void add_to_super_ddf_bvd(struct supertype
*st
,
1801 mdu_disk_info_t
*dk
, int fd
, char *devname
)
1803 /* fd and devname identify a device with-in the ddf container (st).
1804 * dk identifies a location in the new BVD.
1805 * We need to find suitable free space in that device and update
1806 * the phys_refnum and lba_offset for the newly created vd_config.
1807 * We might also want to update the type in the phys_disk
1811 struct ddf_super
*ddf
= st
->sb
;
1812 struct vd_config
*vc
;
1818 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
1819 if (dl
->major
== dk
->major
&&
1820 dl
->minor
== dk
->minor
)
1822 if (!dl
|| ! (dk
->state
& (1<<MD_DISK_SYNC
)))
1825 vc
= &ddf
->newconf
->conf
;
1826 lba_offset
= ddf
->newconf
->lba_offset
;
1827 vc
->phys_refnum
[dk
->raid_disk
] = dl
->disk
.refnum
;
1828 lba_offset
[dk
->raid_disk
] = 0; /* FIXME */
1830 for (i
=0; i
< ddf
->max_part
; i
++)
1831 if (dl
->vlist
[i
] == NULL
)
1833 if (i
== ddf
->max_part
)
1835 dl
->vlist
[i
] = ddf
->newconf
;
1838 dl
->devname
= devname
;
1840 /* Check how many working raid_disks, and if we can mark
1841 * array as optimal yet
1845 for (i
=0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++)
1846 if (vc
->phys_refnum
[i
] != 0xffffffff)
1848 /* Find which virtual_entry */
1849 max_virt_disks
= __be16_to_cpu(ddf
->active
->max_vd_entries
);
1850 for (i
=0; i
< max_virt_disks
; i
++)
1851 if (memcmp(ddf
->virt
->entries
[i
].guid
,
1852 vc
->guid
, DDF_GUID_LEN
)==0)
1854 if (i
== max_virt_disks
)
1856 if (working
== __be16_to_cpu(vc
->prim_elmnt_count
))
1857 ddf
->virt
->entries
[i
].state
=
1858 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
1859 | DDF_state_optimal
;
1861 if (vc
->prl
== DDF_RAID6
&&
1862 working
+1 == __be16_to_cpu(vc
->prim_elmnt_count
))
1863 ddf
->virt
->entries
[i
].state
=
1864 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
1865 | DDF_state_part_optimal
;
1867 ddf
->phys
->entries
[dl
->pdnum
].type
&= ~__cpu_to_be16(DDF_Global_Spare
);
1868 ddf
->phys
->entries
[dl
->pdnum
].type
|= __cpu_to_be16(DDF_Active_in_VD
);
1871 /* add a device to a container, either while creating it or while
1872 * expanding a pre-existing container
1874 static void add_to_super_ddf(struct supertype
*st
,
1875 mdu_disk_info_t
*dk
, int fd
, char *devname
)
1877 struct ddf_super
*ddf
= st
->sb
;
1881 unsigned long long size
;
1882 struct phys_disk_entry
*pde
;
1886 /* This is device numbered dk->number. We need to create
1887 * a phys_disk entry and a more detailed disk_data entry.
1890 dd
= malloc(sizeof(*dd
) + sizeof(dd
->vlist
[0]) * ddf
->max_part
);
1891 dd
->major
= major(stb
.st_rdev
);
1892 dd
->minor
= minor(stb
.st_rdev
);
1893 dd
->devname
= devname
;
1894 dd
->next
= ddf
->dlist
;
1898 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
1900 tm
= localtime(&now
);
1901 sprintf(dd
->disk
.guid
, "%8s%04d%02d%02d",
1902 T10
, tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
1903 *(__u32
*)(dd
->disk
.guid
+ 16) = random();
1904 *(__u32
*)(dd
->disk
.guid
+ 20) = random();
1906 dd
->disk
.refnum
= random(); /* and hope for the best FIXME check this is unique!!*/
1907 dd
->disk
.forced_ref
= 1;
1908 dd
->disk
.forced_guid
= 1;
1909 memset(dd
->disk
.vendor
, ' ', 32);
1910 memcpy(dd
->disk
.vendor
, "Linux", 5);
1911 memset(dd
->disk
.pad
, 0xff, 442);
1912 for (i
= 0; i
< ddf
->max_part
; i
++)
1913 dd
->vlist
[i
] = NULL
;
1915 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1916 pde
= &ddf
->phys
->entries
[n
];
1920 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
1922 memcpy(pde
->guid
, dd
->disk
.guid
, DDF_GUID_LEN
);
1923 pde
->refnum
= dd
->disk
.refnum
;
1924 pde
->type
= __cpu_to_be16(DDF_Forced_PD_GUID
| DDF_Global_Spare
);
1925 pde
->state
= __cpu_to_be16(DDF_Online
);
1926 get_dev_size(fd
, NULL
, &size
);
1927 /* We are required to reserve 32Meg, and record the size in sectors */
1928 pde
->config_size
= __cpu_to_be64( (size
- 32*1024*1024) / 512);
1929 sprintf(pde
->path
, "%17.17s","Information: nil") ;
1930 memset(pde
->pad
, 0xff, 6);
1936 * This is the write_init_super method for a ddf container. It is
1937 * called when creating a container or adding another device to a
1942 static int __write_init_super_ddf(struct supertype
*st
, int do_close
)
1945 struct ddf_super
*ddf
= st
->sb
;
1951 unsigned long long size
, sector
;
1953 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
1959 /* We need to fill in the primary, (secondary) and workspace
1960 * lba's in the headers, set their checksums,
1961 * Also checksum phys, virt....
1963 * Then write everything out, finally the anchor is written.
1965 get_dev_size(fd
, NULL
, &size
);
1967 ddf
->anchor
.workspace_lba
= __cpu_to_be64(size
- 32*1024*2);
1968 ddf
->anchor
.primary_lba
= __cpu_to_be64(size
- 16*1024*2);
1969 ddf
->anchor
.seq
= __cpu_to_be32(1);
1970 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1971 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1973 ddf
->anchor
.openflag
= 0xFF; /* 'open' means nothing */
1974 ddf
->anchor
.seq
= 0xFFFFFFFF; /* no sequencing in anchor */
1975 ddf
->anchor
.crc
= calc_crc(&ddf
->anchor
, 512);
1977 ddf
->primary
.openflag
= 0;
1978 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1980 ddf
->secondary
.openflag
= 0;
1981 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1983 ddf
->primary
.crc
= calc_crc(&ddf
->primary
, 512);
1984 ddf
->secondary
.crc
= calc_crc(&ddf
->secondary
, 512);
1986 sector
= size
- 16*1024*2;
1987 lseek64(fd
, sector
<<9, 0);
1988 write(fd
, &ddf
->primary
, 512);
1990 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
1991 write(fd
, &ddf
->controller
, 512);
1993 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
1995 write(fd
, ddf
->phys
, ddf
->pdsize
);
1997 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
1998 write(fd
, ddf
->virt
, ddf
->vdsize
);
2000 /* Now write lots of config records. */
2001 n_config
= ddf
->max_part
;
2002 conf_size
= ddf
->conf_rec_len
* 512;
2003 for (i
= 0 ; i
<= n_config
; i
++) {
2004 struct vcl
*c
= d
->vlist
[i
];
2006 c
= (struct vcl
*)d
->spare
;
2009 c
->conf
.crc
= calc_crc(&c
->conf
, conf_size
);
2010 write(fd
, &c
->conf
, conf_size
);
2012 __u32 sig
= 0xffffffff;
2014 lseek64(fd
, conf_size
-4, SEEK_CUR
);
2017 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
2018 write(fd
, &d
->disk
, 512);
2020 /* Maybe do the same for secondary */
2022 lseek64(fd
, (size
-1)*512, SEEK_SET
);
2023 write(fd
, &ddf
->anchor
, 512);
2032 static int write_init_super_ddf(struct supertype
*st
)
2034 return __write_init_super_ddf(st
, 1);
2039 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
)
2041 /* We must reserve the last 32Meg */
2042 if (devsize
<= 32*1024*2)
2044 return devsize
- 32*1024*2;
2048 int validate_geometry_ddf(struct supertype
*st
,
2049 int level
, int layout
, int raiddisks
,
2050 int chunk
, unsigned long long size
,
2051 char *dev
, unsigned long long *freesize
)
2057 /* ddf potentially supports lots of things, but it depends on
2058 * what devices are offered (and maybe kernel version?)
2059 * If given unused devices, we will make a container.
2060 * If given devices in a container, we will make a BVD.
2061 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2064 if (level
== LEVEL_CONTAINER
) {
2065 st
->ss
= &super_ddf_container
;
2067 int rv
=st
->ss
->validate_geometry(st
, level
, layout
,
2074 return st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
2075 chunk
, size
, dev
, freesize
);
2079 /* creating in a given container */
2080 st
->ss
= &super_ddf_bvd
;
2082 int rv
=st
->ss
->validate_geometry(st
, level
, layout
,
2089 return st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
2090 chunk
, size
, dev
, freesize
);
2092 /* FIXME should exclude MULTIPATH, or more appropriately, allow
2093 * only known levels.
2098 /* This device needs to be either a device in a 'ddf' container,
2099 * or it needs to be a 'ddf-bvd' array.
2102 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2104 sra
= sysfs_read(fd
, 0, GET_VERSION
);
2106 if (sra
&& sra
->array
.major_version
== -1 &&
2107 strcmp(sra
->text_version
, "ddf-bvd") == 0) {
2108 st
->ss
= &super_ddf_svd
;
2109 return st
->ss
->validate_geometry(st
, level
, layout
,
2110 raiddisks
, chunk
, size
,
2115 Name
": Cannot create this array on device %s\n",
2119 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
2120 fprintf(stderr
, Name
": Cannot open %s: %s\n",
2121 dev
, strerror(errno
));
2124 /* Well, it is in use by someone, maybe a 'ddf' container. */
2125 cfd
= open_container(fd
);
2128 fprintf(stderr
, Name
": Cannot use %s: It is busy\n",
2132 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
2134 if (sra
&& sra
->array
.major_version
== -1 &&
2135 strcmp(sra
->text_version
, "ddf") == 0) {
2136 /* This is a member of a ddf container. Load the container
2137 * and try to create a bvd
2139 struct ddf_super
*ddf
;
2140 st
->ss
= &super_ddf_bvd
;
2141 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
, 1) == 0) {
2143 st
->container_dev
= fd2devnum(cfd
);
2145 return st
->ss
->validate_geometry(st
, level
, layout
,
2146 raiddisks
, chunk
, size
,
2151 fprintf(stderr
, Name
": Cannot use %s: Already in use\n",
2156 int validate_geometry_ddf_container(struct supertype
*st
,
2157 int level
, int layout
, int raiddisks
,
2158 int chunk
, unsigned long long size
,
2159 char *dev
, unsigned long long *freesize
)
2162 unsigned long long ldsize
;
2164 if (level
!= LEVEL_CONTAINER
)
2169 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2171 fprintf(stderr
, Name
": Cannot open %s: %s\n",
2172 dev
, strerror(errno
));
2175 if (!get_dev_size(fd
, dev
, &ldsize
)) {
2181 *freesize
= avail_size_ddf(st
, ldsize
>> 9);
2187 unsigned long long start
, size
;
2189 int cmp_extent(const void *av
, const void *bv
)
2191 const struct extent
*a
= av
;
2192 const struct extent
*b
= bv
;
2193 if (a
->start
< b
->start
)
2195 if (a
->start
> b
->start
)
2200 struct extent
*get_extents(struct ddf_super
*ddf
, struct dl
*dl
)
2202 /* find a list of used extents on the give physical device
2203 * (dnum) of the given ddf.
2204 * Return a malloced array of 'struct extent'
2206 FIXME ignore DDF_Legacy devices?
2214 /* FIXME this is dl->pdnum */
2215 for (dnum
= 0; dnum
< ddf
->phys
->used_pdes
; dnum
++)
2216 if (memcmp(dl
->disk
.guid
,
2217 ddf
->phys
->entries
[dnum
].guid
,
2221 if (dnum
== ddf
->phys
->used_pdes
)
2224 rv
= malloc(sizeof(struct extent
) * (ddf
->max_part
+ 2));
2228 for (i
= 0; i
< ddf
->max_part
; i
++) {
2229 struct vcl
*v
= dl
->vlist
[i
];
2232 for (j
=0; j
< v
->conf
.prim_elmnt_count
; j
++)
2233 if (v
->conf
.phys_refnum
[j
] == dl
->disk
.refnum
) {
2234 /* This device plays role 'j' in 'v'. */
2235 rv
[n
].start
= __be64_to_cpu(v
->lba_offset
[j
]);
2236 rv
[n
].size
= __be64_to_cpu(v
->conf
.blocks
);
2241 qsort(rv
, n
, sizeof(*rv
), cmp_extent
);
2243 rv
[n
].start
= __be64_to_cpu(ddf
->phys
->entries
[dnum
].config_size
);
2248 int validate_geometry_ddf_bvd(struct supertype
*st
,
2249 int level
, int layout
, int raiddisks
,
2250 int chunk
, unsigned long long size
,
2251 char *dev
, unsigned long long *freesize
)
2254 struct ddf_super
*ddf
= st
->sb
;
2256 unsigned long long pos
= 0;
2257 unsigned long long maxsize
;
2260 /* ddf/bvd supports lots of things, but not containers */
2261 if (level
== LEVEL_CONTAINER
)
2263 /* We must have the container info already read in. */
2268 /* General test: make sure there is space for
2269 * 'raiddisks' device extents of size 'size'.
2271 unsigned long long minsize
= size
;
2275 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2281 e
= get_extents(ddf
, dl
);
2284 unsigned long long esize
;
2285 esize
= e
[i
].start
- pos
;
2286 if (esize
>= minsize
)
2288 pos
= e
[i
].start
+ e
[i
].size
;
2290 } while (e
[i
-1].size
);
2295 if (dcnt
< raiddisks
) {
2296 fprintf(stderr
, Name
": Not enough devices with space "
2297 "for this array (%d < %d)\n",
2303 /* This device must be a member of the set */
2304 if (stat(dev
, &stb
) < 0)
2306 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
2308 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
2309 if (dl
->major
== major(stb
.st_rdev
) &&
2310 dl
->minor
== minor(stb
.st_rdev
))
2314 fprintf(stderr
, Name
": %s is not in the same DDF set\n",
2318 e
= get_extents(ddf
, dl
);
2322 unsigned long long esize
;
2323 esize
= e
[i
].start
- pos
;
2324 if (esize
>= maxsize
)
2326 pos
= e
[i
].start
+ e
[i
].size
;
2328 } while (e
[i
-1].size
);
2329 *freesize
= maxsize
;
2334 int validate_geometry_ddf_svd(struct supertype
*st
,
2335 int level
, int layout
, int raiddisks
,
2336 int chunk
, unsigned long long size
,
2337 char *dev
, unsigned long long *freesize
)
2339 /* dd/svd only supports striped, mirrored, concat, spanned... */
2340 if (level
!= LEVEL_LINEAR
&&
2348 static int load_super_ddf_all(struct supertype
*st
, int fd
,
2349 void **sbp
, char *devname
, int keep_fd
)
2352 struct ddf_super
*super
;
2353 struct mdinfo
*sd
, *best
= NULL
;
2359 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2362 if (sra
->array
.major_version
!= -1 ||
2363 sra
->array
.minor_version
!= -2 ||
2364 strcmp(sra
->text_version
, "ddf") != 0)
2367 super
= malloc(sizeof(*super
));
2370 memset(super
, 0, sizeof(*super
));
2372 /* first, try each device, and choose the best ddf */
2373 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2375 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2376 dfd
= dev_open(nm
, O_RDONLY
);
2379 rv
= load_ddf_headers(dfd
, super
, NULL
);
2382 seq
= __be32_to_cpu(super
->active
->seq
);
2383 if (super
->active
->openflag
)
2385 if (!best
|| seq
> bestseq
) {
2393 /* OK, load this ddf */
2394 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
2395 dfd
= dev_open(nm
, O_RDONLY
);
2398 load_ddf_headers(dfd
, super
, NULL
);
2399 load_ddf_global(dfd
, super
, NULL
);
2401 /* Now we need the device-local bits */
2402 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2403 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2404 dfd
= dev_open(nm
, keep_fd
? O_RDWR
: O_RDONLY
);
2407 seq
= load_ddf_local(dfd
, super
, NULL
, keep_fd
);
2408 if (!keep_fd
) close(dfd
);
2411 if (st
->ss
== NULL
) {
2412 st
->ss
= &super_ddf_container
;
2413 st
->minor_version
= 0;
2415 st
->container_dev
= fd2devnum(fd
);
2423 static struct mdinfo
*container_content_ddf(struct supertype
*st
)
2425 /* Given a container loaded by load_super_ddf_all,
2426 * extract information about all the arrays into
2429 * For each vcl in conflist: create an mdinfo, fill it in,
2430 * then look for matching devices (phys_refnum) in dlist
2431 * and create appropriate device mdinfo.
2433 struct ddf_super
*ddf
= st
->sb
;
2434 struct mdinfo
*rest
= NULL
;
2437 for (vc
= ddf
->conflist
; vc
; vc
=vc
->next
)
2440 struct mdinfo
*this;
2441 this = malloc(sizeof(*this));
2442 memset(this, 0, sizeof(*this));
2446 this->array
.major_version
= 1000;
2447 this->array
.minor_version
= 0;
2448 this->array
.patch_version
= 0;
2449 this->array
.level
= map_num1(ddf_level_num
, vc
->conf
.prl
);
2450 this->array
.raid_disks
=
2451 __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
2452 this->array
.layout
= rlq_to_layout(vc
->conf
.rlq
, vc
->conf
.prl
,
2453 this->array
.raid_disks
);
2454 this->array
.md_minor
= -1;
2455 this->array
.ctime
= DECADE
+
2456 __be32_to_cpu(*(__u32
*)(vc
->conf
.guid
+16));
2457 this->array
.utime
= DECADE
+
2458 __be32_to_cpu(vc
->conf
.timestamp
);
2459 this->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
2461 for (i
=0; i
< __be16_to_cpu(ddf
->virt
->populated_vdes
); i
++)
2462 if (memcmp(ddf
->virt
->entries
[i
].guid
,
2463 vc
->conf
.guid
, DDF_GUID_LEN
) == 0)
2465 if ((ddf
->virt
->entries
[i
].state
& DDF_state_inconsistent
) ||
2466 (ddf
->virt
->entries
[i
].init_state
& DDF_initstate_mask
) !=
2468 this->array
.state
= 0;
2469 this->resync_start
= 0;
2471 this->array
.state
= 1;
2472 this->resync_start
= ~0ULL;
2474 memcpy(this->name
, ddf
->virt
->entries
[i
].name
, 32);
2477 memset(this->uuid
, 0, sizeof(this->uuid
));
2478 this->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
2479 this->array
.size
= this->component_size
/ 2;
2480 this->container_member
= i
;
2482 sprintf(this->text_version
, "/%s/%d",
2483 devnum2devname(st
->container_dev
),
2484 this->container_member
);
2487 for (i
=0 ; i
< ddf
->mppe
; i
++) {
2491 if (vc
->conf
.phys_refnum
[i
] == 0xFFFFFFFF)
2494 this->array
.working_disks
++;
2496 for (d
= ddf
->dlist
; d
; d
=d
->next
)
2497 if (d
->disk
.refnum
== vc
->conf
.phys_refnum
[i
])
2502 dev
= malloc(sizeof(*dev
));
2503 memset(dev
, 0, sizeof(*dev
));
2504 dev
->next
= this->devs
;
2507 dev
->disk
.number
= __be32_to_cpu(d
->disk
.refnum
);
2508 dev
->disk
.major
= d
->major
;
2509 dev
->disk
.minor
= d
->minor
;
2510 dev
->disk
.raid_disk
= i
;
2511 dev
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
2513 dev
->events
= __le32_to_cpu(ddf
->primary
.seq
);
2514 dev
->data_offset
= vc
->lba_offset
[i
];
2515 dev
->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
2517 strcpy(dev
->name
, d
->devname
);
2523 static int init_zero_ddf(struct supertype
*st
,
2524 mdu_array_info_t
*info
,
2525 unsigned long long size
, char *name
,
2526 char *homehost
, int *uuid
)
2532 static int store_zero_ddf(struct supertype
*st
, int fd
)
2534 unsigned long long dsize
;
2536 memset(buf
, 0, 512);
2539 if (!get_dev_size(fd
, NULL
, &dsize
))
2542 lseek64(fd
, dsize
-512, 0);
2543 write(fd
, buf
, 512);
2547 static int compare_super_ddf(struct supertype
*st
, struct supertype
*tst
)
2551 * 0 same, or first was empty, and second was copied
2552 * 1 second had wrong number
2554 * 3 wrong other info
2556 struct ddf_super
*first
= st
->sb
;
2557 struct ddf_super
*second
= tst
->sb
;
2565 if (memcmp(first
->anchor
.guid
, second
->anchor
.guid
, DDF_GUID_LEN
) != 0)
2568 /* FIXME should I look at anything else? */
2573 * A new array 'a' has been started which claims to be instance 'inst'
2574 * within container 'c'.
2575 * We need to confirm that the array matches the metadata in 'c' so
2576 * that we don't corrupt any metadata.
2578 static int ddf_open_new(struct supertype
*c
, struct active_array
*a
, char *inst
)
2580 fprintf(stderr
, "ddf: open_new %s\n", inst
);
2581 a
->info
.container_member
= atoi(inst
);
2586 * The array 'a' is to be marked clean in the metadata.
2587 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
2588 * clean up to the point (in sectors). If that cannot be recorded in the
2589 * metadata, then leave it as dirty.
2591 * For DDF, we need to clear the DDF_state_inconsistent bit in the
2592 * !global! virtual_disk.virtual_entry structure.
2594 static void ddf_set_array_state(struct active_array
*a
, int consistent
)
2596 struct ddf_super
*ddf
= a
->container
->sb
;
2597 int inst
= a
->info
.container_member
;
2599 ddf
->virt
->entries
[inst
].state
&= ~DDF_state_inconsistent
;
2601 ddf
->virt
->entries
[inst
].state
|= DDF_state_inconsistent
;
2602 ddf
->virt
->entries
[inst
].init_state
&= ~DDF_initstate_mask
;
2603 if (a
->resync_start
== ~0ULL)
2604 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_full
;
2605 else if (a
->resync_start
== 0)
2606 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_not
;
2608 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_quick
;
2610 printf("ddf mark %s %llu\n", consistent
?"clean":"dirty",
2615 * The state of each disk is stored in the global phys_disk structure
2616 * in phys_disk.entries[n].state.
2617 * This makes various combinations awkward.
2618 * - When a device fails in any array, it must be failed in all arrays
2619 * that include a part of this device.
2620 * - When a component is rebuilding, we cannot include it officially in the
2621 * array unless this is the only array that uses the device.
2623 * So: when transitioning:
2624 * Online -> failed, just set failed flag. monitor will propagate
2625 * spare -> online, the device might need to be added to the array.
2626 * spare -> failed, just set failed. Don't worry if in array or not.
2628 static void ddf_set_disk(struct active_array
*a
, int n
, int state
)
2630 struct ddf_super
*ddf
= a
->container
->sb
;
2631 int inst
= a
->info
.container_member
;
2632 struct vd_config
*vc
= find_vdcr(ddf
, inst
);
2633 int pd
= find_phys(ddf
, vc
->phys_refnum
[n
]);
2637 fprintf(stderr
, "ddf: cannot find instance %d!!\n", inst
);
2641 /* disk doesn't currently exist. If it is now in_sync,
2643 if ((state
& DS_INSYNC
) && ! (state
& DS_FAULTY
)) {
2644 /* Find dev 'n' in a->info->devs, determine the
2645 * ddf refnum, and set vc->phys_refnum and update
2651 if (state
& DS_FAULTY
)
2652 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Failed
);
2653 if (state
& DS_INSYNC
) {
2654 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Online
);
2655 ddf
->phys
->entries
[pd
].state
&= __cpu_to_be16(~DDF_Rebuilding
);
2659 fprintf(stderr
, "ddf: set_disk %d to %x\n", n
, state
);
2661 /* Now we need to check the state of the array and update
2662 * virtual_disk.entries[n].state.
2663 * It needs to be one of "optimal", "degraded", "failed".
2664 * I don't understand 'deleted' or 'missing'.
2667 for (i
=0; i
< a
->info
.array
.raid_disks
; i
++) {
2668 pd
= find_phys(ddf
, vc
->phys_refnum
[i
]);
2671 st
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
2672 if ((st
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
2676 state
= DDF_state_degraded
;
2677 if (working
== a
->info
.array
.raid_disks
)
2678 state
= DDF_state_optimal
;
2679 else switch(vc
->prl
) {
2683 state
= DDF_state_failed
;
2687 state
= DDF_state_failed
;
2691 if (working
< a
->info
.array
.raid_disks
-1)
2692 state
= DDF_state_failed
;
2695 if (working
< a
->info
.array
.raid_disks
-2)
2696 state
= DDF_state_failed
;
2697 else if (working
== a
->info
.array
.raid_disks
-1)
2698 state
= DDF_state_part_optimal
;
2702 ddf
->virt
->entries
[inst
].state
=
2703 (ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
2708 static void ddf_sync_metadata(struct supertype
*st
)
2712 * Write all data to all devices.
2713 * Later, we might be able to track whether only local changes
2714 * have been made, or whether any global data has been changed,
2715 * but ddf is sufficiently weird that it probably always
2716 * changes global data ....
2718 __write_init_super_ddf(st
, 0);
2719 fprintf(stderr
, "ddf: sync_metadata\n");
2722 static void ddf_process_update(struct supertype
*st
,
2723 struct metadata_update
*update
)
2725 /* Apply this update to the metadata.
2726 * The first 4 bytes are a DDF_*_MAGIC which guides
2728 * Possible update are:
2729 * DDF_PHYS_RECORDS_MAGIC
2730 * Add a new physical device. Changes to this record
2731 * only happen implicitly.
2732 * used_pdes is the device number.
2733 * DDF_VIRT_RECORDS_MAGIC
2734 * Add a new VD. Possibly also change the 'access' bits.
2735 * populated_vdes is the entry number.
2737 * New or updated VD. the VIRT_RECORD must already
2738 * exist. For an update, phys_refnum and lba_offset
2739 * (at least) are updated, and the VD_CONF must
2740 * be written to precisely those devices listed with
2742 * DDF_SPARE_ASSIGN_MAGIC
2743 * replacement Spare Assignment Record... but for which device?
2746 * - to create a new array, we send a VIRT_RECORD and
2747 * a VD_CONF. Then assemble and start the array.
2748 * - to activate a spare we send a VD_CONF to add the phys_refnum
2749 * and offset. This will also mark the spare as active with
2750 * a spare-assignment record.
2752 struct ddf_super
*ddf
= st
->sb
;
2753 __u32
*magic
= (__u32
*)update
->buf
;
2754 struct phys_disk
*pd
;
2755 struct virtual_disk
*vd
;
2756 struct vd_config
*vc
;
2762 printf("Process update %x\n", *magic
);
2765 case DDF_PHYS_RECORDS_MAGIC
:
2767 if (update
->len
!= (sizeof(struct phys_disk
) +
2768 sizeof(struct phys_disk_entry
)))
2770 pd
= (struct phys_disk
*)update
->buf
;
2772 ent
= __be16_to_cpu(pd
->used_pdes
);
2773 if (ent
>= __be16_to_cpu(ddf
->phys
->max_pdes
))
2775 if (!all_ff(ddf
->phys
->entries
[ent
].guid
))
2777 ddf
->phys
->entries
[ent
] = pd
->entries
[0];
2778 ddf
->phys
->used_pdes
= __cpu_to_be16(1 +
2779 __be16_to_cpu(ddf
->phys
->used_pdes
));
2782 case DDF_VIRT_RECORDS_MAGIC
:
2784 if (update
->len
!= (sizeof(struct virtual_disk
) +
2785 sizeof(struct virtual_entry
)))
2787 vd
= (struct virtual_disk
*)update
->buf
;
2789 ent
= __be16_to_cpu(vd
->populated_vdes
);
2790 if (ent
>= __be16_to_cpu(ddf
->virt
->max_vdes
))
2792 if (!all_ff(ddf
->virt
->entries
[ent
].guid
))
2794 ddf
->virt
->entries
[ent
] = vd
->entries
[0];
2795 ddf
->virt
->populated_vdes
= __cpu_to_be16(1 +
2796 __be16_to_cpu(ddf
->virt
->populated_vdes
));
2799 case DDF_VD_CONF_MAGIC
:
2800 printf("len %d %d\n", update
->len
, ddf
->conf_rec_len
);
2802 mppe
= __be16_to_cpu(ddf
->anchor
.max_primary_element_entries
);
2803 if (update
->len
!= ddf
->conf_rec_len
)
2805 vc
= (struct vd_config
*)update
->buf
;
2806 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
2807 if (memcmp(vcl
->conf
.guid
, vc
->guid
, DDF_GUID_LEN
) == 0)
2809 printf("vcl = %p\n", vcl
);
2811 /* An update, just copy the phys_refnum and lba_offset
2814 memcpy(vcl
->conf
.phys_refnum
, vc
->phys_refnum
,
2815 mppe
* (sizeof(__u32
) + sizeof(__u64
)));
2818 vcl
= update
->space
;
2819 update
->space
= NULL
;
2820 vcl
->next
= ddf
->conflist
;
2822 vcl
->lba_offset
= (__u64
*)
2823 &vcl
->conf
.phys_refnum
[mppe
];
2824 ddf
->conflist
= vcl
;
2826 /* Now make sure vlist is correct for each dl. */
2827 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
2830 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
2831 for (dn
=0; dn
< ddf
->mppe
; dn
++)
2832 if (vcl
->conf
.phys_refnum
[dn
] ==
2834 printf("dev %d has %p at %d\n",
2835 dl
->pdnum
, vcl
, vn
);
2836 dl
->vlist
[vn
++] = vcl
;
2839 while (vn
< ddf
->max_part
)
2840 dl
->vlist
[vn
++] = NULL
;
2842 ddf
->phys
->entries
[dl
->pdnum
].type
&=
2843 ~__cpu_to_be16(DDF_Global_Spare
);
2844 ddf
->phys
->entries
[dl
->pdnum
].type
|=
2845 __cpu_to_be16(DDF_Active_in_VD
);
2848 ddf
->phys
->entries
[dl
->pdnum
].type
&=
2849 ~__cpu_to_be16(DDF_Global_Spare
);
2850 ddf
->phys
->entries
[dl
->pdnum
].type
|=
2851 __cpu_to_be16(DDF_Spare
);
2853 if (!dl
->vlist
[0] && !dl
->spare
) {
2854 ddf
->phys
->entries
[dl
->pdnum
].type
|=
2855 __cpu_to_be16(DDF_Global_Spare
);
2856 ddf
->phys
->entries
[dl
->pdnum
].type
&=
2857 ~__cpu_to_be16(DDF_Spare
|
2862 case DDF_SPARE_ASSIGN_MAGIC
:
2868 * Check if the array 'a' is degraded but not failed.
2869 * If it is, find as many spares as are available and needed and
2870 * arrange for their inclusion.
2871 * We only choose devices which are not already in the array,
2872 * and prefer those with a spare-assignment to this array.
2873 * otherwise we choose global spares - assuming always that
2874 * there is enough room.
2875 * For each spare that we assign, we return an 'mdinfo' which
2876 * describes the position for the device in the array.
2877 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
2878 * the new phys_refnum and lba_offset values.
2880 * Only worry about BVDs at the moment.
2882 static struct mdinfo
*ddf_activate_spare(struct active_array
*a
,
2883 struct metadata_update
**updates
)
2887 struct ddf_super
*ddf
= a
->container
->sb
;
2889 struct mdinfo
*rv
= NULL
;
2891 struct metadata_update
*mu
;
2894 struct vd_config
*vc
;
2897 /* FIXME, If there is a DS_FAULTY, we want to wait for it to be
2898 * removed. Then only look at DS_REMOVE devices.
2899 * What about !DS_INSYNC - how can that happen?
2901 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
2902 if ((d
->curr_state
& DS_FAULTY
) &&
2904 /* wait for Removal to happen */
2906 if (d
->state_fd
>= 0)
2910 printf("ddf_activate: working=%d (%d) level=%d\n", working
, a
->info
.array
.raid_disks
,
2911 a
->info
.array
.level
);
2912 if (working
== a
->info
.array
.raid_disks
)
2913 return NULL
; /* array not degraded */
2914 switch (a
->info
.array
.level
) {
2917 return NULL
; /* failed */
2921 if (working
< a
->info
.array
.raid_disks
- 1)
2922 return NULL
; /* failed */
2925 if (working
< a
->info
.array
.raid_disks
- 2)
2926 return NULL
; /* failed */
2928 default: /* concat or stripe */
2929 return NULL
; /* failed */
2932 /* For each slot, if it is not working, find a spare */
2934 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
2935 for (d
= a
->info
.devs
; d
; d
= d
->next
)
2936 if (d
->disk
.raid_disk
== i
)
2938 printf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
2939 if (d
&& (d
->state_fd
>= 0))
2942 /* OK, this device needs recovery. Find a spare */
2944 for ( ; dl
; dl
= dl
->next
) {
2945 unsigned long long esize
;
2946 unsigned long long pos
;
2949 int is_dedicated
= 0;
2952 /* If in this array, skip */
2953 for (d2
= a
->info
.devs
; d2
; d2
= d2
->next
)
2954 if (d2
->disk
.major
== dl
->major
&&
2955 d2
->disk
.minor
== dl
->minor
) {
2956 printf("%x:%x already in array\n", dl
->major
, dl
->minor
);
2961 if (ddf
->phys
->entries
[dl
->pdnum
].type
&
2962 __cpu_to_be16(DDF_Spare
)) {
2963 /* Check spare assign record */
2965 if (dl
->spare
->type
& DDF_spare_dedicated
) {
2966 /* check spare_ents for guid */
2968 j
< __be16_to_cpu(dl
->spare
->populated
);
2970 if (memcmp(dl
->spare
->spare_ents
[j
].guid
,
2971 ddf
->virt
->entries
[a
->info
.container_member
].guid
,
2978 } else if (ddf
->phys
->entries
[dl
->pdnum
].type
&
2979 __cpu_to_be16(DDF_Global_Spare
)) {
2982 if ( ! (is_dedicated
||
2983 (is_global
&& global_ok
))) {
2984 printf("%x:%x not suitable: %d %d\n", dl
->major
, dl
->minor
,
2985 is_dedicated
, is_global
);
2989 /* We are allowed to use this device - is there space?
2990 * We need a->info.component_size sectors */
2991 ex
= get_extents(ddf
, dl
);
2993 printf("cannot get extents\n");
3000 esize
= ex
[j
].start
- pos
;
3001 if (esize
>= a
->info
.component_size
)
3003 pos
= ex
[i
].start
+ ex
[i
].size
;
3005 } while (ex
[i
-1].size
);
3008 if (esize
< a
->info
.component_size
) {
3009 printf("%x:%x has no room: %llu %llu\n", dl
->major
, dl
->minor
,
3010 esize
, a
->info
.component_size
);
3015 /* Cool, we have a device with some space at pos */
3016 di
= malloc(sizeof(*di
));
3017 memset(di
, 0, sizeof(*di
));
3018 di
->disk
.number
= i
;
3019 di
->disk
.raid_disk
= i
;
3020 di
->disk
.major
= dl
->major
;
3021 di
->disk
.minor
= dl
->minor
;
3023 di
->data_offset
= pos
;
3024 di
->component_size
= a
->info
.component_size
;
3025 di
->container_member
= dl
->pdnum
;
3028 printf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
3033 if (!dl
&& ! global_ok
) {
3034 /* not enough dedicated spares, try global */
3042 /* No spares found */
3044 /* Now 'rv' has a list of devices to return.
3045 * Create a metadata_update record to update the
3046 * phys_refnum and lba_offset values
3048 mu
= malloc(sizeof(*mu
) + ddf
->conf_rec_len
* 512);
3049 mu
->buf
= (char*)(mu
+1);
3050 mu
->space
= malloc(sizeof(struct vcl
));
3051 mu
->len
= ddf
->conf_rec_len
;
3052 mu
->next
= *updates
;
3053 vc
= find_vdcr(ddf
, a
->info
.container_member
);
3054 memcpy(mu
->buf
, vc
, ddf
->conf_rec_len
* 512);
3056 vc
= (struct vd_config
*)mu
->buf
;
3057 lba
= (__u64
*)&vc
->phys_refnum
[ddf
->mppe
];
3058 for (di
= rv
; di
; di
= di
->next
) {
3059 vc
->phys_refnum
[di
->disk
.raid_disk
] =
3060 ddf
->phys
->entries
[dl
->pdnum
].refnum
;
3061 lba
[di
->disk
.raid_disk
] = di
->data_offset
;
3067 struct superswitch super_ddf
= {
3069 .examine_super
= examine_super_ddf
,
3070 .brief_examine_super
= brief_examine_super_ddf
,
3071 .detail_super
= detail_super_ddf
,
3072 .brief_detail_super
= brief_detail_super_ddf
,
3073 .validate_geometry
= validate_geometry_ddf
,
3075 .match_home
= match_home_ddf
,
3076 .uuid_from_super
= uuid_from_super_ddf
,
3077 .getinfo_super
= getinfo_super_ddf
,
3078 .update_super
= update_super_ddf
,
3080 .avail_size
= avail_size_ddf
,
3082 .compare_super
= compare_super_ddf
,
3084 .load_super
= load_super_ddf
,
3085 .init_super
= init_zero_ddf
,
3086 .store_super
= store_zero_ddf
,
3087 .free_super
= free_super_ddf
,
3088 .match_metadata_desc
= match_metadata_desc_ddf
,
3089 .getinfo_super_n
= getinfo_super_n_container
,
3097 .open_new
= ddf_open_new
,
3098 .set_array_state
= ddf_set_array_state
,
3099 .set_disk
= ddf_set_disk
,
3100 .sync_metadata
= ddf_sync_metadata
,
3101 .process_update
= ddf_process_update
,
3102 .activate_spare
= ddf_activate_spare
,
3106 /* Super_ddf_container is set by validate_geometry_ddf when given a
3107 * device that is not part of any array
3109 struct superswitch super_ddf_container
= {
3111 .validate_geometry
= validate_geometry_ddf_container
,
3112 .write_init_super
= write_init_super_ddf
,
3115 .load_super
= load_super_ddf
,
3116 .init_super
= init_super_ddf
,
3117 .add_to_super
= add_to_super_ddf
,
3118 .getinfo_super
= getinfo_super_ddf
,
3120 .free_super
= free_super_ddf
,
3122 .container_content
= container_content_ddf
,
3123 .getinfo_super_n
= getinfo_super_n_container
,
3130 struct superswitch super_ddf_bvd
= {
3132 // .detail_super = detail_super_ddf_bvd,
3133 // .brief_detail_super = brief_detail_super_ddf_bvd,
3134 .validate_geometry
= validate_geometry_ddf_bvd
,
3135 .write_init_super
= write_init_super_ddf
,
3137 .update_super
= update_super_ddf
,
3138 .init_super
= init_super_ddf_bvd
,
3139 .add_to_super
= add_to_super_ddf_bvd
,
3140 .getinfo_super
= getinfo_super_ddf_bvd
,
3141 .getinfo_super_n
= getinfo_super_n_bvd
,
3143 .load_super
= load_super_ddf
,
3144 .free_super
= free_super_ddf
,
3145 .match_metadata_desc
= match_metadata_desc_ddf_bvd
,
3153 struct superswitch super_ddf_svd
= {
3155 // .detail_super = detail_super_ddf_svd,
3156 // .brief_detail_super = brief_detail_super_ddf_svd,
3157 .validate_geometry
= validate_geometry_ddf_svd
,
3159 .update_super
= update_super_ddf
,
3160 .init_super
= init_super_ddf
,
3162 .load_super
= load_super_ddf
,
3163 .free_super
= free_super_ddf
,
3164 .match_metadata_desc
= match_metadata_desc_ddf_svd
,