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 /* a non-official T10 name for creation GUIDs */
35 static char T10
[] = "Linux-MD";
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.
41 #define DECADE (3600*24*(365*10+2))
44 const unsigned char *buf
,
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.
53 * - all multibyte numeric fields are bigendian.
54 * - all strings are space padded.
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
66 #define DDF_CONCAT 0x1f
67 #define DDF_RAID5E 0x15
68 #define DDF_RAID5EE 0x25
69 #define DDF_RAID6 0x06
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 */
85 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
86 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
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 */
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)
106 #define DDF_GUID_LEN 24
107 #define DDF_REVISION_0 "01.00.00"
108 #define DDF_REVISION_2 "01.02.00"
111 __u32 magic
; /* DDF_HEADER_MAGIC */
113 char guid
[DDF_GUID_LEN
];
114 char revision
[8]; /* 01.02.00 */
115 __u32 seq
; /* starts at '1' */
120 __u8 pad0
; /* 0xff */
121 __u8 pad1
[12]; /* 12 * 0xff */
122 /* 64 bytes so far */
123 __u8 header_ext
[32]; /* reserved: fill with 0xff */
127 __u8 pad2
[3]; /* 0xff */
128 __u32 workspace_len
; /* sectors for vendor space -
129 * at least 32768(sectors) */
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
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
;
156 /* 256 bytes so far */
157 __u8 pad4
[256]; /* 0xff */
161 #define DDF_HEADER_ANCHOR 0x00
162 #define DDF_HEADER_PRIMARY 0x01
163 #define DDF_HEADER_SECONDARY 0x02
165 /* The content of the 'controller section' - global scope */
166 struct ddf_controller_data
{
167 __u32 magic
; /* DDF_CONTROLLER_MAGIC */
169 char guid
[DDF_GUID_LEN
];
170 struct controller_type
{
177 __u8 pad
[8]; /* 0xff */
178 __u8 vendor_data
[448];
181 /* The content of phys_section - global scope */
183 __u32 magic
; /* DDF_PHYS_RECORDS_MAGIC */
188 struct phys_disk_entry
{
189 char guid
[DDF_GUID_LEN
];
193 __u64 config_size
; /* DDF structures must be after here */
194 char path
[18]; /* another horrible structure really */
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 */
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
213 /* phys_disk_entry.state is a bigendian bitmap */
215 #define DDF_Failed 2 /* overrides 1,4,8 */
216 #define DDF_Rebuilding 4
217 #define DDF_Transition 8
219 #define DDF_ReadErrors 32
220 #define DDF_Missing 64
222 /* The content of the virt_section global scope */
223 struct virtual_disk
{
224 __u32 magic
; /* DDF_VIRT_RECORDS_MAGIC */
226 __u16 populated_vdes
;
229 struct virtual_entry
{
230 char guid
[DDF_GUID_LEN
];
232 __u16 pad0
; /* 0xffff */
242 /* virtual_entry.type is a bitmap - bigendian */
244 #define DDF_Enforce_Groups 2
245 #define DDF_Unicode 4
246 #define DDF_Owner_Valid 8
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
257 #define DDF_state_morphing 0x8
258 #define DDF_state_inconsistent 0x10
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
267 #define DDF_access_mask 0xc0
268 #define DDF_access_rw 0x00
269 #define DDF_access_ro 0x80
270 #define DDF_access_blocked 0xc0
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
278 __u32 magic
; /* DDF_VD_CONF_MAGIC */
280 char guid
[DDF_GUID_LEN
];
284 __u16 prim_elmnt_count
;
285 __u8 chunk_shift
; /* 0 == 512, 1==1024 etc */
288 __u8 sec_elmnt_count
;
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 */
302 __u8 v0
[32]; /* reserved- 0xff */
303 __u8 v1
[32]; /* reserved- 0xff */
304 __u8 v2
[16]; /* reserved- 0xff */
305 __u8 v3
[16]; /* reserved- 0xff */
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 */
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 */
321 struct spare_assign
{
322 __u32 magic
; /* DDF_SPARE_ASSIGN_MAGIC */
327 __u16 populated
; /* SAEs used */
328 __u16 max
; /* max SAEs */
330 struct spare_assign_entry
{
331 char guid
[DDF_GUID_LEN
];
332 __u16 secondary_element
;
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 */
342 /* The data_section contents - local scope */
344 __u32 magic
; /* DDF_PHYS_DATA_MAGIC */
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 */
354 /* bbm_section content */
355 struct bad_block_log
{
362 struct mapped_block
{
363 __u64 defective_start
;
364 __u32 replacement_start
;
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:
376 * - Physical disk records
377 * - Virtual disk records
379 * - Configuration records
380 * - Physical Disk data section
381 * ( and Bad block and vendor which I don't care about yet).
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.
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
;
399 int max_part
, mppe
, conf_rec_len
;
407 __u64
*lba_offset
; /* location in 'conf' of
409 int vcnum
; /* index into ->virt */
410 __u64
*block_sizes
; /* NULL if all the same */
413 struct vd_config conf
;
414 } *conflist
, *currentconf
;
423 unsigned long long size
; /* sectors */
424 int pdnum
; /* index in ->phys */
425 struct spare_assign
*spare
;
428 struct disk_data disk
;
429 void *mdupdate
; /* hold metadata update */
430 struct vcl
*vlist
[0]; /* max_part in size */
435 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
439 static int calc_crc(void *buf
, int len
)
441 /* crcs are always at the same place as in the ddf_header */
442 struct ddf_header
*ddf
= buf
;
443 __u32 oldcrc
= ddf
->crc
;
445 ddf
->crc
= 0xffffffff;
447 newcrc
= crc32(0, buf
, len
);
449 /* The crc is store (like everything) bigendian, so convert
450 * here for simplicity
452 return __cpu_to_be32(newcrc
);
455 static int load_ddf_header(int fd
, unsigned long long lba
,
456 unsigned long long size
,
458 struct ddf_header
*hdr
, struct ddf_header
*anchor
)
460 /* read a ddf header (primary or secondary) from fd/lba
461 * and check that it is consistent with anchor
463 * magic, crc, guid, rev, and LBA's header_type, and
464 * everything after header_type must be the same
469 if (lseek64(fd
, lba
<<9, 0) < 0)
472 if (read(fd
, hdr
, 512) != 512)
475 if (hdr
->magic
!= DDF_HEADER_MAGIC
)
477 if (calc_crc(hdr
, 512) != hdr
->crc
)
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
||
484 memcmp(anchor
->pad2
, hdr
->pad2
, 512 -
485 offsetof(struct ddf_header
, pad2
)) != 0)
488 /* Looks good enough to me... */
492 static void *load_section(int fd
, struct ddf_super
*super
, void *buf
,
493 __u32 offset_be
, __u32 len_be
, int check
)
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
);
500 if (len
!= 2 && len
!= 8 && len
!= 32
501 && len
!= 128 && len
!= 512)
507 /* All pre-allocated sections are a single block */
510 } else if (posix_memalign(&buf
, 512, len
<<9) != 0)
516 if (super
->active
->type
== 1)
517 offset
+= __be64_to_cpu(super
->active
->primary_lba
);
519 offset
+= __be64_to_cpu(super
->active
->secondary_lba
);
521 if (lseek64(fd
, offset
<<9, 0) != (offset
<<9)) {
526 if (read(fd
, buf
, len
<<9) != (len
<<9)) {
534 static int load_ddf_headers(int fd
, struct ddf_super
*super
, char *devname
)
536 unsigned long long dsize
;
538 get_dev_size(fd
, NULL
, &dsize
);
540 if (lseek64(fd
, dsize
-512, 0) < 0) {
543 Name
": Cannot seek to anchor block on %s: %s\n",
544 devname
, strerror(errno
));
547 if (read(fd
, &super
->anchor
, 512) != 512) {
550 Name
": Cannot read anchor block on %s: %s\n",
551 devname
, strerror(errno
));
554 if (super
->anchor
.magic
!= DDF_HEADER_MAGIC
) {
556 fprintf(stderr
, Name
": no DDF anchor found on %s\n",
560 if (calc_crc(&super
->anchor
, 512) != super
->anchor
.crc
) {
562 fprintf(stderr
, Name
": bad CRC on anchor on %s\n",
566 if (memcmp(super
->anchor
.revision
, DDF_REVISION_0
, 8) != 0 &&
567 memcmp(super
->anchor
.revision
, DDF_REVISION_2
, 8) != 0) {
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
);
574 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.primary_lba
),
576 &super
->primary
, &super
->anchor
) == 0) {
579 Name
": Failed to load primary DDF header "
583 super
->active
= &super
->primary
;
584 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.secondary_lba
),
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
)
594 super
->active
= &super
->secondary
;
599 static int load_ddf_global(int fd
, struct ddf_super
*super
, char *devname
)
602 ok
= load_section(fd
, super
, &super
->controller
,
603 super
->active
->controller_section_offset
,
604 super
->active
->controller_section_length
,
606 super
->phys
= load_section(fd
, super
, NULL
,
607 super
->active
->phys_section_offset
,
608 super
->active
->phys_section_length
,
610 super
->pdsize
= __be32_to_cpu(super
->active
->phys_section_length
) * 512;
612 super
->virt
= load_section(fd
, super
, NULL
,
613 super
->active
->virt_section_offset
,
614 super
->active
->virt_section_length
,
616 super
->vdsize
= __be32_to_cpu(super
->active
->virt_section_length
) * 512;
626 super
->conflist
= NULL
;
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
);
635 static int load_ddf_local(int fd
, struct ddf_super
*super
,
636 char *devname
, int keep
)
643 int max_virt_disks
= __be16_to_cpu(super
->active
->max_vd_entries
);
644 unsigned long long dsize
;
646 /* First the local disk info */
647 if (posix_memalign((void**)&dl
, 512,
649 (super
->max_part
) * sizeof(dl
->vlist
[0])) != 0) {
650 fprintf(stderr
, Name
": %s could not allocate disk info buffer\n",
655 load_section(fd
, super
, &dl
->disk
,
656 super
->active
->data_section_offset
,
657 super
->active
->data_section_length
,
659 dl
->devname
= devname
? strdup(devname
) : NULL
;
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;
668 if (get_dev_size(fd
, devname
, &dsize
))
669 dl
->size
= dsize
>> 9;
671 for (i
=0 ; i
< super
->max_part
; i
++)
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)
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
686 conf
= load_section(fd
, super
, NULL
,
687 super
->active
->config_section_offset
,
688 super
->active
->config_section_length
,
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);
699 if (vd
->magic
== DDF_SPARE_ASSIGN_MAGIC
) {
702 if (posix_memalign((void**)&dl
->spare
, 512,
703 super
->conf_rec_len
*512) != 0) {
705 ": %s could not allocate spare info buf\n",
710 memcpy(dl
->spare
, vd
, super
->conf_rec_len
*512);
713 if (vd
->magic
!= DDF_VD_CONF_MAGIC
)
715 for (vcl
= super
->conflist
; vcl
; vcl
= vcl
->next
) {
716 if (memcmp(vcl
->conf
.guid
,
717 vd
->guid
, DDF_GUID_LEN
) == 0)
722 dl
->vlist
[vnum
++] = vcl
;
723 if (__be32_to_cpu(vd
->seqnum
) <=
724 __be32_to_cpu(vcl
->conf
.seqnum
))
727 if (posix_memalign((void**)&vcl
, 512,
728 (super
->conf_rec_len
*512 +
729 offsetof(struct vcl
, conf
))) != 0) {
731 ": %s could not allocate vcl buf\n",
735 vcl
->next
= super
->conflist
;
736 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
737 super
->conflist
= vcl
;
738 dl
->vlist
[vnum
++] = vcl
;
740 memcpy(&vcl
->conf
, vd
, super
->conf_rec_len
*512);
741 vcl
->lba_offset
= (__u64
*)
742 &vcl
->conf
.phys_refnum
[super
->mppe
];
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)
748 if (i
< max_virt_disks
)
757 static int load_super_ddf_all(struct supertype
*st
, int fd
,
758 void **sbp
, char *devname
, int keep_fd
);
760 static int load_super_ddf(struct supertype
*st
, int fd
,
763 unsigned long long dsize
;
764 struct ddf_super
*super
;
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)
773 return 1; /* FIXME Is this correct */
775 if (get_dev_size(fd
, devname
, &dsize
) == 0)
778 /* 32M is a lower bound */
779 if (dsize
<= 32*1024*1024) {
782 Name
": %s is too small for ddf: "
783 "size is %llu sectors.\n",
791 Name
": %s is an odd size for ddf: "
792 "size is %llu bytes.\n",
798 if (posix_memalign((void**)&super
, 512, sizeof(*super
))!= 0) {
799 fprintf(stderr
, Name
": malloc of %zu failed.\n",
803 memset(super
, 0, sizeof(*super
));
805 rv
= load_ddf_headers(fd
, super
, devname
);
811 /* Have valid headers and have chosen the best. Let's read in the rest*/
813 rv
= load_ddf_global(fd
, super
, devname
);
818 Name
": Failed to load all information "
819 "sections on %s\n", devname
);
824 rv
= load_ddf_local(fd
, super
, devname
, 0);
829 Name
": Failed to load all information "
830 "sections on %s\n", devname
);
835 /* Should possibly check the sections .... */
838 if (st
->ss
== NULL
) {
840 st
->minor_version
= 0;
843 st
->loaded_container
= 0;
848 static void free_super_ddf(struct supertype
*st
)
850 struct ddf_super
*ddf
= st
->sb
;
855 while (ddf
->conflist
) {
856 struct vcl
*v
= ddf
->conflist
;
857 ddf
->conflist
= v
->next
;
859 free(v
->block_sizes
);
863 struct dl
*d
= ddf
->dlist
;
864 ddf
->dlist
= d
->next
;
875 static struct supertype
*match_metadata_desc_ddf(char *arg
)
877 /* 'ddf' only support containers */
878 struct supertype
*st
;
879 if (strcmp(arg
, "ddf") != 0 &&
880 strcmp(arg
, "default") != 0
884 st
= malloc(sizeof(*st
));
885 memset(st
, 0, sizeof(*st
));
888 st
->minor_version
= 0;
896 static mapping_t ddf_state
[] = {
902 { "Partially Optimal", 5},
908 static mapping_t ddf_init_state
[] = {
909 { "Not Initialised", 0},
910 { "QuickInit in Progress", 1},
911 { "Fully Initialised", 2},
915 static mapping_t ddf_access
[] = {
919 { "Blocked (no access)", 3},
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
},
931 { "CONCAT",DDF_CONCAT
},
932 { "RAID5E",DDF_RAID5E
},
933 { "RAID5EE",DDF_RAID5EE
},
934 { "RAID6", DDF_RAID6
},
937 static mapping_t ddf_sec_level
[] = {
938 { "Striped", DDF_2STRIPED
},
939 { "Mirrored", DDF_2MIRRORED
},
940 { "Concat", DDF_2CONCAT
},
941 { "Spanned", DDF_2SPANNED
},
949 static struct num_mapping ddf_level_num
[] = {
952 { DDF_RAID3
, LEVEL_UNSUPPORTED
},
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
},
964 static int map_num1(struct num_mapping
*map
, int num
)
967 for (i
=0 ; map
[i
].num1
!= MAXINT
; i
++)
968 if (map
[i
].num1
== num
)
974 static void print_guid(char *guid
, int tstamp
)
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
982 int l
= DDF_GUID_LEN
;
985 for (i
=0 ; i
<DDF_GUID_LEN
; i
++) {
986 if ((i
&3)==0 && i
!= 0) printf(":");
987 printf("%02X", guid
[i
]&255);
991 while (l
&& guid
[l
-1] == ' ')
993 for (i
=0 ; i
<l
; i
++) {
994 if (guid
[i
] >= 0x20 && guid
[i
] < 0x7f)
995 fputc(guid
[i
], stdout
);
1000 time_t then
= __be32_to_cpu(*(__u32
*)(guid
+16)) + DECADE
;
1003 tm
= localtime(&then
);
1004 strftime(tbuf
, 100, " %D %T",tm
);
1005 fputs(tbuf
, stdout
);
1010 static void examine_vd(int n
, struct ddf_super
*sb
, char *guid
)
1012 int crl
= sb
->conf_rec_len
;
1015 for (vcl
= sb
->conflist
; vcl
; vcl
= vcl
->next
) {
1016 struct vd_config
*vc
= &vcl
->conf
;
1018 if (calc_crc(vc
, crl
*512) != vc
->crc
)
1020 if (memcmp(vc
->guid
, guid
, DDF_GUID_LEN
) != 0)
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-");
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);
1043 static void examine_vds(struct ddf_super
*sb
)
1045 int cnt
= __be16_to_cpu(sb
->virt
->populated_vdes
);
1047 printf(" Virtual Disks : %d\n", cnt
);
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);
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
);
1065 if (cnt
) printf("\n");
1068 static void examine_pds(struct ddf_super
*sb
)
1070 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1073 printf(" Physical Disks : %d\n", cnt
);
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
);
1080 printf(" PD GUID[%d] : ", i
); print_guid(pd
->guid
, 0);
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);
1104 printf(" Device[%d] : %s\n",
1112 static void examine_super_ddf(struct supertype
*st
, char *homehost
)
1114 struct ddf_super
*sb
= st
->sb
;
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);
1120 printf(" Container GUID : "); print_guid(sb
->anchor
.guid
, 1);
1122 printf(" Seq : %08x\n", __be32_to_cpu(sb
->active
->seq
));
1123 printf(" Redundant hdr : %s\n", sb
->secondary
.magic
== DDF_HEADER_MAGIC
1129 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
);
1132 static void brief_examine_super_ddf(struct supertype
*st
)
1134 /* We just write a generic DDF ARRAY entry
1138 getinfo_super_ddf(st
, &info
);
1139 fname_from_uuid(st
, &info
, nbuf
, ':');
1140 printf("ARRAY /dev/ddf metadata=ddf UUID=%s\n", nbuf
+ 5);
1143 static void detail_super_ddf(struct supertype
*st
, char *homehost
)
1146 * Could print DDF GUID
1147 * Need to find which array
1148 * If whole, briefly list all arrays
1153 static void brief_detail_super_ddf(struct supertype
*st
)
1155 /* FIXME I really need to know which array we are detailing.
1156 * Can that be stored in ddf_super??
1158 // struct ddf_super *ddf = st->sb;
1161 getinfo_super_ddf(st
, &info
);
1162 fname_from_uuid(st
, &info
, nbuf
,':');
1163 printf(" UUID=%s", nbuf
+ 5);
1167 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1169 /* It matches 'this' host if the controller is a
1170 * Linux-MD controller with vendor_data matching
1173 struct ddf_super
*ddf
= st
->sb
;
1174 int len
= strlen(homehost
);
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);
1183 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
, int inst
)
1187 for (v
= ddf
->conflist
; v
; v
= v
->next
)
1188 if (inst
== v
->vcnum
)
1194 static int find_phys(struct ddf_super
*ddf
, __u32 phys_refnum
)
1196 /* Find the entry in phys_disk which has the given refnum
1197 * and return it's index
1200 for (i
=0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
); i
++)
1201 if (ddf
->phys
->entries
[i
].refnum
== phys_refnum
)
1206 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
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.
1217 * For each of these we can make do with a truncated
1218 * or hashed uuid rather than the original, as long as
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.
1227 struct ddf_super
*ddf
= st
->sb
;
1228 struct vcl
*vcl
= ddf
->currentconf
;
1231 struct sha1_ctx ctx
;
1234 guid
= vcl
->conf
.guid
;
1236 guid
= ddf
->anchor
.guid
;
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);
1246 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
);
1248 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
)
1250 struct ddf_super
*ddf
= st
->sb
;
1252 if (ddf
->currentconf
) {
1253 getinfo_super_ddf_bvd(st
, info
);
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;
1267 info
->disk
.major
= 0;
1268 info
->disk
.minor
= 0;
1270 info
->disk
.number
= __be32_to_cpu(ddf
->dlist
->disk
.refnum
);
1271 info
->disk
.raid_disk
= find_phys(ddf
, ddf
->dlist
->disk
.refnum
);
1273 info
->data_offset
= __be64_to_cpu(ddf
->phys
->
1274 entries
[info
->disk
.raid_disk
].
1276 info
->component_size
= ddf
->dlist
->size
- info
->data_offset
;
1278 info
->disk
.number
= -1;
1279 // info->disk.raid_disk = find refnum in the table and use index;
1281 info
->disk
.state
= (1 << MD_DISK_SYNC
);
1284 info
->reshape_active
= 0;
1287 info
->array
.major_version
= -1;
1288 info
->array
.minor_version
= -2;
1289 strcpy(info
->text_version
, "ddf");
1290 info
->safe_mode_delay
= 0;
1292 uuid_from_super_ddf(st
, info
->uuid
);
1296 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
);
1298 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
)
1300 struct ddf_super
*ddf
= st
->sb
;
1301 struct vcl
*vc
= ddf
->currentconf
;
1302 int cd
= ddf
->currentdev
;
1304 /* FIXME this returns BVD info - what if we want SVD ?? */
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
;
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
];
1321 info
->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
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 = ???;
1330 info
->container_member
= ddf
->currentconf
->vcnum
;
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
)
1338 info
->resync_start
= ~0ULL;
1340 uuid_from_super_ddf(st
, info
->uuid
);
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
),
1348 info
->safe_mode_delay
= 200;
1354 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1356 char *devname
, int verbose
,
1357 int uuid_set
, char *homehost
)
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
1366 * force-array: Array is degraded but being forced, mark it clean
1367 * if that will be needed to assemble it.
1369 * newdev: not used ????
1370 * grow: Array has gained a new device - this is currently for
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.
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.
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);
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
1394 if (strcmp(update
, "grow") == 0) {
1397 if (strcmp(update
, "resync") == 0) {
1398 // info->resync_checkpoint = 0;
1400 /* We ignore UUID updates as they make even less sense
1403 if (strcmp(update
, "homehost") == 0) {
1404 /* homehost is stored in controller->vendor_data,
1405 * or it is when we are the vendor
1407 // if (info->vendor_is_local)
1408 // strcpy(ddf->controller.vendor_data, homehost);
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);
1415 if (strcmp(update
, "_reshape_progress") == 0) {
1416 /* We don't support reshape yet */
1419 // update_all_csum(ddf);
1424 static void make_header_guid(char *guid
)
1428 /* Create a DDF Header of Virtual Disk GUID */
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
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)
1445 memcpy(guid
+20, &stamp
, 4);
1446 if (rfd
>= 0) close(rfd
);
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
,
1455 static int init_super_ddf(struct supertype
*st
,
1456 mdu_array_info_t
*info
,
1457 unsigned long long size
, char *name
, char *homehost
,
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
1465 * To create on a pre-existing array a different method will be called.
1466 * This one is just for fresh drives.
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?
1483 struct ddf_super
*ddf
;
1486 int max_phys_disks
, max_virt_disks
;
1487 unsigned long long sector
;
1491 struct phys_disk
*pd
;
1492 struct virtual_disk
*vd
;
1499 return init_super_ddf_bvd(st
, info
, size
, name
, homehost
,
1502 if (posix_memalign((void**)&ddf
, 512, sizeof(*ddf
)) != 0) {
1503 fprintf(stderr
, Name
": %s could not allocate superblock\n", __func__
);
1506 memset(ddf
, 0, sizeof(*ddf
));
1507 ddf
->dlist
= NULL
; /* no physical disks yet */
1508 ddf
->conflist
= NULL
; /* No virtual disks yet */
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.
1518 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
1519 make_header_guid(ddf
->anchor
.guid
);
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); /* ?? */
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 */
1551 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
1552 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
1555 /* phys is 8 sectors after that */
1556 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
1557 sizeof(struct phys_disk_entry
)*max_phys_disks
,
1559 switch(pdsize
/512) {
1560 case 2: case 8: case 32: case 128: case 512: break;
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;
1568 /* virt is another 32 sectors */
1569 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
1570 sizeof(struct virtual_entry
) * max_virt_disks
,
1572 switch(vdsize
/512) {
1573 case 2: case 8: case 32: case 128: case 512: break;
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;
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
);
1586 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
1587 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
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);
1597 memset(ddf
->anchor
.pad4
, 0xff, 256);
1599 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1600 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1602 ddf
->primary
.openflag
= 1; /* I guess.. */
1603 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1605 ddf
->secondary
.openflag
= 1; /* I guess.. */
1606 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1608 ddf
->active
= &ddf
->primary
;
1610 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
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?
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
] = ' ';
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);
1632 if (posix_memalign((void**)&pd
, 512, pdsize
) != 0) {
1633 fprintf(stderr
, Name
": %s could not allocate pd\n", __func__
);
1637 ddf
->pdsize
= pdsize
;
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);
1646 if (posix_memalign((void**)&vd
, 512, vdsize
) != 0) {
1647 fprintf(stderr
, Name
": %s could not allocate vd\n", __func__
);
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);
1658 for (i
=0; i
<max_virt_disks
; i
++)
1659 memset(&vd
->entries
[i
], 0xff, sizeof(struct virtual_entry
));
1662 ddf
->updates_pending
= 1;
1666 static int all_ff(char *guid
)
1669 for (i
= 0; i
< DDF_GUID_LEN
; i
++)
1670 if (guid
[i
] != (char)0xff)
1674 static int chunk_to_shift(int chunksize
)
1676 return ffs(chunksize
/512)-1;
1679 static int level_to_prl(int 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
;
1691 static int layout_to_rlq(int level
, int layout
, int raiddisks
)
1695 return DDF_RAID0_SIMPLE
;
1698 case 2: return DDF_RAID1_SIMPLE
;
1699 case 3: return DDF_RAID1_MULTI
;
1704 case 0: return DDF_RAID4_N
;
1710 case ALGORITHM_LEFT_ASYMMETRIC
:
1711 return DDF_RAID5_N_RESTART
;
1712 case ALGORITHM_RIGHT_ASYMMETRIC
:
1714 return DDF_RAID5_0_RESTART
;
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 */
1726 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
)
1730 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1732 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1740 return -1; /* FIXME this isn't checked */
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
;
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
;
1770 unsigned long long start
, size
;
1772 static int cmp_extent(const void *av
, const void *bv
)
1774 const struct extent
*a
= av
;
1775 const struct extent
*b
= bv
;
1776 if (a
->start
< b
->start
)
1778 if (a
->start
> b
->start
)
1783 static struct extent
*get_extents(struct ddf_super
*ddf
, struct dl
*dl
)
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'
1789 FIXME ignore DDF_Legacy devices?
1796 rv
= malloc(sizeof(struct extent
) * (ddf
->max_part
+ 2));
1800 for (i
= 0; i
< ddf
->max_part
; i
++) {
1801 struct vcl
*v
= dl
->vlist
[i
];
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
);
1813 qsort(rv
, n
, sizeof(*rv
), cmp_extent
);
1815 rv
[n
].start
= __be64_to_cpu(ddf
->phys
->entries
[dl
->pdnum
].config_size
);
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
,
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
1831 struct ddf_super
*ddf
= st
->sb
;
1833 struct virtual_entry
*ve
;
1835 struct vd_config
*vc
;
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
));
1845 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
1846 if (all_ff(ddf
->virt
->entries
[venum
].guid
))
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");
1853 ve
= &ddf
->virt
->entries
[venum
];
1855 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
1856 * timestamp, random number
1858 make_header_guid(ve
->guid
);
1859 ve
->unit
= __cpu_to_be16(info
->md_minor
);
1861 ve
->guid_crc
= crc32(0, (unsigned char*)ddf
->anchor
.guid
, DDF_GUID_LEN
);
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
;
1867 ve
->init_state
= DDF_init_not
;
1869 memset(ve
->pad1
, 0xff, 14);
1870 memset(ve
->name
, ' ', 16);
1872 strncpy(ve
->name
, name
, 16);
1873 ddf
->virt
->populated_vdes
=
1874 __cpu_to_be16(__be16_to_cpu(ddf
->virt
->populated_vdes
)+1);
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__
);
1882 vcl
->lba_offset
= (__u64
*) &vcl
->conf
.phys_refnum
[ddf
->mppe
];
1884 sprintf(st
->subarray
, "%d", venum
);
1885 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
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;
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);
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);
1925 memset(vc
->phys_refnum
, 0xff, 4*ddf
->mppe
);
1926 memset(vc
->phys_refnum
+(ddf
->mppe
* 4), 0x00, 8*ddf
->mppe
);
1928 vcl
->next
= ddf
->conflist
;
1929 ddf
->conflist
= vcl
;
1930 ddf
->currentconf
= vcl
;
1931 ddf
->updates_pending
= 1;
1936 static void add_to_super_ddf_bvd(struct supertype
*st
,
1937 mdu_disk_info_t
*dk
, int fd
, char *devname
)
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
1947 struct ddf_super
*ddf
= st
->sb
;
1948 struct vd_config
*vc
;
1952 unsigned long long blocks
, pos
, esize
;
1955 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
1956 if (dl
->major
== dk
->major
&&
1957 dl
->minor
== dk
->minor
)
1959 if (!dl
|| ! (dk
->state
& (1<<MD_DISK_SYNC
)))
1962 vc
= &ddf
->currentconf
->conf
;
1963 lba_offset
= ddf
->currentconf
->lba_offset
;
1965 ex
= get_extents(ddf
, dl
);
1970 blocks
= __be64_to_cpu(vc
->blocks
);
1971 if (ddf
->currentconf
->block_sizes
)
1972 blocks
= ddf
->currentconf
->block_sizes
[dk
->raid_disk
];
1975 esize
= ex
[i
].start
- pos
;
1976 if (esize
>= blocks
)
1978 pos
= ex
[i
].start
+ ex
[i
].size
;
1980 } while (ex
[i
-1].size
);
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
);
1990 for (i
=0; i
< ddf
->max_part
; i
++)
1991 if (dl
->vlist
[i
] == NULL
)
1993 if (i
== ddf
->max_part
)
1995 dl
->vlist
[i
] = ddf
->currentconf
;
1998 dl
->devname
= devname
;
2000 /* Check how many working raid_disks, and if we can mark
2001 * array as optimal yet
2005 for (i
=0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++)
2006 if (vc
->phys_refnum
[i
] != 0xffffffff)
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
;
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
;
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;
2027 /* add a device to a container, either while creating it or while
2028 * expanding a pre-existing container
2030 static void add_to_super_ddf(struct supertype
*st
,
2031 mdu_disk_info_t
*dk
, int fd
, char *devname
)
2033 struct ddf_super
*ddf
= st
->sb
;
2037 unsigned long long size
;
2038 struct phys_disk_entry
*pde
;
2042 if (ddf
->currentconf
) {
2043 add_to_super_ddf_bvd(st
, dk
, fd
, devname
);
2047 /* This is device numbered dk->number. We need to create
2048 * a phys_disk entry and a more detailed disk_data entry.
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",
2058 dd
->major
= major(stb
.st_rdev
);
2059 dd
->minor
= minor(stb
.st_rdev
);
2060 dd
->devname
= devname
;
2064 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
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();
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;
2077 if (ddf
->phys
->entries
[i
].refnum
== dd
->disk
.refnum
)
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
;
2089 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
2090 pde
= &ddf
->phys
->entries
[n
];
2093 if (st
->update_tail
) {
2094 int len
= (sizeof(struct phys_disk
) +
2095 sizeof(struct phys_disk_entry
));
2096 struct phys_disk
*pd
;
2099 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2100 pd
->used_pdes
= __cpu_to_be16(n
);
2101 pde
= &pd
->entries
[0];
2105 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
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);
2118 dd
->size
= size
>> 9;
2119 if (st
->update_tail
) {
2120 dd
->next
= ddf
->add_list
;
2123 dd
->next
= ddf
->dlist
;
2125 ddf
->updates_pending
= 1;
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
2135 static unsigned char null_conf
[4096+512];
2137 static int __write_init_super_ddf(struct supertype
*st
, int do_close
)
2140 struct ddf_super
*ddf
= st
->sb
;
2147 unsigned long long size
, sector
;
2149 /* try to write updated metadata,
2150 * if we catch a failure move on to the next disk
2152 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
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....
2163 * Then write everything out, finally the anchor is written.
2165 get_dev_size(fd
, NULL
, &size
);
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);
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);
2177 ddf
->primary
.openflag
= 0;
2178 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
2180 ddf
->secondary
.openflag
= 0;
2181 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
2183 ddf
->primary
.crc
= calc_crc(&ddf
->primary
, 512);
2184 ddf
->secondary
.crc
= calc_crc(&ddf
->secondary
, 512);
2186 sector
= size
- 16*1024*2;
2187 lseek64(fd
, sector
<<9, 0);
2188 if (write(fd
, &ddf
->primary
, 512) < 0)
2191 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
2192 if (write(fd
, &ddf
->controller
, 512) < 0)
2195 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
2197 if (write(fd
, ddf
->phys
, ddf
->pdsize
) < 0)
2200 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
2201 if (write(fd
, ddf
->virt
, ddf
->vdsize
) < 0)
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
];
2210 c
= (struct vcl
*)d
->spare
;
2213 c
->conf
.crc
= calc_crc(&c
->conf
, conf_size
);
2214 if (write(fd
, &c
->conf
, conf_size
) < 0)
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)
2224 togo
-= sizeof(null_conf
)-512;
2226 if (write(fd
, null_aligned
, togo
) < 0)
2232 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
2233 if (write(fd
, &d
->disk
, 512) < 0)
2236 /* Maybe do the same for secondary */
2238 lseek64(fd
, (size
-1)*512, SEEK_SET
);
2239 if (write(fd
, &ddf
->anchor
, 512) < 0)
2245 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
2250 return attempts
!= successes
;
2253 static int write_init_super_ddf(struct supertype
*st
)
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
;
2263 if (!ddf
->currentconf
) {
2264 int len
= (sizeof(struct phys_disk
) +
2265 sizeof(struct phys_disk_entry
));
2267 /* adding a disk to the container. */
2271 append_metadata_update(st
, ddf
->add_list
->mdupdate
, len
);
2272 ddf
->add_list
->mdupdate
= NULL
;
2276 /* Newly created VD */
2278 /* First the virtual disk. We have a slightly fake header */
2279 len
= sizeof(struct virtual_disk
) + sizeof(struct virtual_entry
);
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
);
2286 /* Then the vd_config */
2287 len
= ddf
->conf_rec_len
* 512;
2289 memcpy(vc
, &ddf
->currentconf
->conf
, len
);
2290 append_metadata_update(st
, vc
, len
);
2292 /* FIXME I need to close the fds! */
2295 return __write_init_super_ddf(st
, 1);
2300 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
)
2302 /* We must reserve the last 32Meg */
2303 if (devsize
<= 32*1024*2)
2305 return devsize
- 32*1024*2;
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
,
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
,
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
,
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.
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
,
2343 size
, dev
, freesize
,
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.
2352 return validate_geometry_ddf_bvd(st
, level
, layout
, raiddisks
,
2353 chunk
, size
, dev
, freesize
,
2357 /* Initial sanity check. Exclude illegal levels. */
2359 for (i
=0; ddf_level_num
[i
].num1
!= MAXINT
; i
++)
2360 if (ddf_level_num
[i
].num2
== level
)
2362 if (ddf_level_num
[i
].num1
== MAXINT
)
2364 /* Should check layout? etc */
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.
2375 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2377 sra
= sysfs_read(fd
, 0, GET_VERSION
);
2379 if (sra
&& sra
->array
.major_version
== -1 &&
2380 strcmp(sra
->text_version
, "ddf") == 0) {
2383 /* find space for 'n' devices. */
2384 /* remember the devices */
2385 /* Somehow return the fact that we have enough */
2390 Name
": ddf: Cannot create this array "
2395 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
2397 fprintf(stderr
, Name
": ddf: Cannot open %s: %s\n",
2398 dev
, strerror(errno
));
2401 /* Well, it is in use by someone, maybe a 'ddf' container. */
2402 cfd
= open_container(fd
);
2406 fprintf(stderr
, Name
": ddf: Cannot use %s: %s\n",
2407 dev
, strerror(EBUSY
));
2410 sra
= sysfs_read(cfd
, 0, GET_VERSION
);
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
2417 struct ddf_super
*ddf
;
2418 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
, 1) == 0) {
2420 st
->container_dev
= fd2devnum(cfd
);
2422 return validate_geometry_ddf_bvd(st
, level
, layout
,
2423 raiddisks
, chunk
, size
,
2428 } else /* device may belong to a different container */
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
,
2442 unsigned long long ldsize
;
2444 if (level
!= LEVEL_CONTAINER
)
2449 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2452 fprintf(stderr
, Name
": ddf: Cannot open %s: %s\n",
2453 dev
, strerror(errno
));
2456 if (!get_dev_size(fd
, dev
, &ldsize
)) {
2462 *freesize
= avail_size_ddf(st
, ldsize
>> 9);
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
,
2474 struct ddf_super
*ddf
= st
->sb
;
2476 unsigned long long pos
= 0;
2477 unsigned long long maxsize
;
2480 /* ddf/bvd supports lots of things, but not containers */
2481 if (level
== LEVEL_CONTAINER
)
2483 /* We must have the container info already read in. */
2488 /* General test: make sure there is space for
2489 * 'raiddisks' device extents of size 'size'.
2491 unsigned long long minsize
= size
;
2495 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2501 e
= get_extents(ddf
, dl
);
2504 unsigned long long esize
;
2505 esize
= e
[i
].start
- pos
;
2506 if (esize
>= minsize
)
2508 pos
= e
[i
].start
+ e
[i
].size
;
2510 } while (e
[i
-1].size
);
2515 if (dcnt
< raiddisks
) {
2518 Name
": ddf: Not enough devices with "
2519 "space for this array (%d < %d)\n",
2525 /* This device must be a member of the set */
2526 if (stat(dev
, &stb
) < 0)
2528 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
2530 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
2531 if (dl
->major
== major(stb
.st_rdev
) &&
2532 dl
->minor
== minor(stb
.st_rdev
))
2537 fprintf(stderr
, Name
": ddf: %s is not in the "
2542 e
= get_extents(ddf
, dl
);
2546 unsigned long long esize
;
2547 esize
= e
[i
].start
- pos
;
2548 if (esize
>= maxsize
)
2550 pos
= e
[i
].start
+ e
[i
].size
;
2552 } while (e
[i
-1].size
);
2553 *freesize
= maxsize
;
2559 static int load_super_ddf_all(struct supertype
*st
, int fd
,
2560 void **sbp
, char *devname
, int keep_fd
)
2563 struct ddf_super
*super
;
2564 struct mdinfo
*sd
, *best
= NULL
;
2570 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
2573 if (sra
->array
.major_version
!= -1 ||
2574 sra
->array
.minor_version
!= -2 ||
2575 strcmp(sra
->text_version
, "ddf") != 0)
2578 if (posix_memalign((void**)&super
, 512, sizeof(*super
)) != 0)
2580 memset(super
, 0, sizeof(*super
));
2582 /* first, try each device, and choose the best ddf */
2583 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2585 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2586 dfd
= dev_open(nm
, O_RDONLY
);
2589 rv
= load_ddf_headers(dfd
, super
, NULL
);
2592 seq
= __be32_to_cpu(super
->active
->seq
);
2593 if (super
->active
->openflag
)
2595 if (!best
|| seq
> bestseq
) {
2603 /* OK, load this ddf */
2604 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
2605 dfd
= dev_open(nm
, O_RDONLY
);
2608 load_ddf_headers(dfd
, super
, NULL
);
2609 load_ddf_global(dfd
, super
, NULL
);
2611 /* Now we need the device-local bits */
2612 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
2615 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
2616 dfd
= dev_open(nm
, keep_fd
? O_RDWR
: O_RDONLY
);
2619 rv
= load_ddf_headers(dfd
, super
, NULL
);
2621 rv
= load_ddf_local(dfd
, super
, NULL
, keep_fd
);
2622 if (!keep_fd
) close(dfd
);
2626 if (st
->subarray
[0]) {
2629 for (v
= super
->conflist
; v
; v
= v
->next
)
2630 if (v
->vcnum
== atoi(st
->subarray
))
2631 super
->currentconf
= v
;
2632 if (!super
->currentconf
)
2636 if (st
->ss
== NULL
) {
2637 st
->ss
= &super_ddf
;
2638 st
->minor_version
= 0;
2640 st
->container_dev
= fd2devnum(fd
);
2642 st
->loaded_container
= 1;
2645 #endif /* MDASSEMBLE */
2647 static struct mdinfo
*container_content_ddf(struct supertype
*st
)
2649 /* Given a container loaded by load_super_ddf_all,
2650 * extract information about all the arrays into
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.
2657 struct ddf_super
*ddf
= st
->sb
;
2658 struct mdinfo
*rest
= NULL
;
2661 for (vc
= ddf
->conflist
; vc
; vc
=vc
->next
)
2664 struct mdinfo
*this;
2665 this = malloc(sizeof(*this));
2666 memset(this, 0, sizeof(*this));
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
;
2685 if ((ddf
->virt
->entries
[i
].state
& DDF_state_inconsistent
) ||
2686 (ddf
->virt
->entries
[i
].init_state
& DDF_initstate_mask
) !=
2688 this->array
.state
= 0;
2689 this->resync_start
= 0;
2691 this->array
.state
= 1;
2692 this->resync_start
= ~0ULL;
2694 memcpy(this->name
, ddf
->virt
->entries
[i
].name
, 32);
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
;
2702 ddf
->currentconf
= vc
;
2703 uuid_from_super_ddf(st
, this->uuid
);
2704 ddf
->currentconf
= NULL
;
2706 sprintf(this->text_version
, "/%s/%d",
2707 devnum2devname(st
->container_dev
),
2708 this->container_member
);
2710 for (i
=0 ; i
< ddf
->mppe
; i
++) {
2714 if (vc
->conf
.phys_refnum
[i
] == 0xFFFFFFFF)
2717 this->array
.working_disks
++;
2719 for (d
= ddf
->dlist
; d
; d
=d
->next
)
2720 if (d
->disk
.refnum
== vc
->conf
.phys_refnum
[i
])
2725 dev
= malloc(sizeof(*dev
));
2726 memset(dev
, 0, sizeof(*dev
));
2727 dev
->next
= this->devs
;
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
);
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
);
2740 strcpy(dev
->name
, d
->devname
);
2746 static int store_zero_ddf(struct supertype
*st
, int fd
)
2748 unsigned long long dsize
;
2752 if (!get_dev_size(fd
, NULL
, &dsize
))
2755 if (posix_memalign(&buf
, 512, 512) != 0)
2757 memset(buf
, 0, 512);
2759 lseek64(fd
, dsize
-512, 0);
2760 rc
= write(fd
, buf
, 512);
2767 static int compare_super_ddf(struct supertype
*st
, struct supertype
*tst
)
2771 * 0 same, or first was empty, and second was copied
2772 * 1 second had wrong number
2774 * 3 wrong other info
2776 struct ddf_super
*first
= st
->sb
;
2777 struct ddf_super
*second
= tst
->sb
;
2785 if (memcmp(first
->anchor
.guid
, second
->anchor
.guid
, DDF_GUID_LEN
) != 0)
2788 /* FIXME should I look at anything else? */
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.
2799 static int ddf_open_new(struct supertype
*c
, struct active_array
*a
, char *inst
)
2801 dprintf("ddf: open_new %s\n", inst
);
2802 a
->info
.container_member
= atoi(inst
);
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.
2812 * For DDF, we need to clear the DDF_state_inconsistent bit in the
2813 * !global! virtual_disk.virtual_entry structure.
2815 static int ddf_set_array_state(struct active_array
*a
, int consistent
)
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 */
2823 if (!is_resync_complete(a
))
2827 ddf
->virt
->entries
[inst
].state
&= ~DDF_state_inconsistent
;
2829 ddf
->virt
->entries
[inst
].state
|= DDF_state_inconsistent
;
2830 if (old
!= ddf
->virt
->entries
[inst
].state
)
2831 ddf
->updates_pending
= 1;
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
;
2840 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_quick
;
2841 if (old
!= ddf
->virt
->entries
[inst
].init_state
)
2842 ddf
->updates_pending
= 1;
2844 dprintf("ddf mark %d %s %llu\n", inst
, consistent
?"clean":"dirty",
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.
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.
2863 static void ddf_set_disk(struct active_array
*a
, int n
, int state
)
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
]);
2872 dprintf("ddf: cannot find instance %d!!\n", inst
);
2876 /* disk doesn't currently exist. If it is now in_sync,
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
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
);
2893 if (old
!= ddf
->phys
->entries
[pd
].state
)
2894 ddf
->updates_pending
= 1;
2897 dprintf("ddf: set_disk %d to %x\n", n
, state
);
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'.
2905 for (i
=0; i
< a
->info
.array
.raid_disks
; i
++) {
2906 pd
= find_phys(ddf
, vc
->phys_refnum
[i
]);
2909 st
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
2910 if ((st
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
2914 state
= DDF_state_degraded
;
2915 if (working
== a
->info
.array
.raid_disks
)
2916 state
= DDF_state_optimal
;
2917 else switch(vc
->prl
) {
2921 state
= DDF_state_failed
;
2925 state
= DDF_state_failed
;
2929 if (working
< a
->info
.array
.raid_disks
-1)
2930 state
= DDF_state_failed
;
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
;
2940 if (ddf
->virt
->entries
[inst
].state
!=
2941 ((ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
2944 ddf
->virt
->entries
[inst
].state
=
2945 (ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
2947 ddf
->updates_pending
= 1;
2952 static void ddf_sync_metadata(struct supertype
*st
)
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 ....
2962 struct ddf_super
*ddf
= st
->sb
;
2963 if (!ddf
->updates_pending
)
2965 ddf
->updates_pending
= 0;
2966 __write_init_super_ddf(st
, 0);
2967 dprintf("ddf: sync_metadata\n");
2970 static void ddf_process_update(struct supertype
*st
,
2971 struct metadata_update
*update
)
2973 /* Apply this update to the metadata.
2974 * The first 4 bytes are a DDF_*_MAGIC which guides
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.
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
2990 * DDF_SPARE_ASSIGN_MAGIC
2991 * replacement Spare Assignment Record... but for which device?
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.
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
;
3010 dprintf("Process update %x\n", *magic
);
3013 case DDF_PHYS_RECORDS_MAGIC
:
3015 if (update
->len
!= (sizeof(struct phys_disk
) +
3016 sizeof(struct phys_disk_entry
)))
3018 pd
= (struct phys_disk
*)update
->buf
;
3020 ent
= __be16_to_cpu(pd
->used_pdes
);
3021 if (ent
>= __be16_to_cpu(ddf
->phys
->max_pdes
))
3023 if (!all_ff(ddf
->phys
->entries
[ent
].guid
))
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
;
3034 al
->next
= ddf
->dlist
;
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;
3045 case DDF_VIRT_RECORDS_MAGIC
:
3047 if (update
->len
!= (sizeof(struct virtual_disk
) +
3048 sizeof(struct virtual_entry
)))
3050 vd
= (struct virtual_disk
*)update
->buf
;
3052 ent
= __be16_to_cpu(vd
->populated_vdes
);
3053 if (ent
>= __be16_to_cpu(ddf
->virt
->max_vdes
))
3055 if (!all_ff(ddf
->virt
->entries
[ent
].guid
))
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;
3063 case DDF_VD_CONF_MAGIC
:
3064 dprintf("len %d %d\n", update
->len
, ddf
->conf_rec_len
);
3066 mppe
= __be16_to_cpu(ddf
->anchor
.max_primary_element_entries
);
3067 if (update
->len
!= ddf
->conf_rec_len
* 512)
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)
3073 dprintf("vcl = %p\n", vcl
);
3075 /* An update, just copy the phys_refnum and lba_offset
3078 memcpy(vcl
->conf
.phys_refnum
, vc
->phys_refnum
,
3079 mppe
* (sizeof(__u32
) + sizeof(__u64
)));
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
;
3092 /* Now make sure vlist is correct for each dl. */
3093 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
3096 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
3097 for (dn
=0; dn
< ddf
->mppe
; dn
++)
3098 if (vcl
->conf
.phys_refnum
[dn
] ==
3100 dprintf("dev %d has %p at %d\n",
3101 dl
->pdnum
, vcl
, vn
);
3102 dl
->vlist
[vn
++] = vcl
;
3105 while (vn
< ddf
->max_part
)
3106 dl
->vlist
[vn
++] = NULL
;
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
);
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
);
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
|
3127 ddf
->updates_pending
= 1;
3129 case DDF_SPARE_ASSIGN_MAGIC
:
3134 static void ddf_prepare_update(struct supertype
*st
,
3135 struct metadata_update
*update
)
3137 /* This update arrived at managemon.
3138 * We are about to pass it to monitor.
3139 * If a malloc is needed, do it here.
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
;
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.
3163 * Only worry about BVDs at the moment.
3165 static struct mdinfo
*ddf_activate_spare(struct active_array
*a
,
3166 struct metadata_update
**updates
)
3170 struct ddf_super
*ddf
= a
->container
->sb
;
3172 struct mdinfo
*rv
= NULL
;
3174 struct metadata_update
*mu
;
3177 struct vd_config
*vc
;
3180 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
3181 if ((d
->curr_state
& DS_FAULTY
) &&
3183 /* wait for Removal to happen */
3185 if (d
->state_fd
>= 0)
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
) {
3196 return NULL
; /* failed */
3200 if (working
< a
->info
.array
.raid_disks
- 1)
3201 return NULL
; /* failed */
3204 if (working
< a
->info
.array
.raid_disks
- 2)
3205 return NULL
; /* failed */
3207 default: /* concat or stripe */
3208 return NULL
; /* failed */
3211 /* For each slot, if it is not working, find a spare */
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
)
3217 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
3218 if (d
&& (d
->state_fd
>= 0))
3221 /* OK, this device needs recovery. Find a spare */
3223 for ( ; dl
; dl
= dl
->next
) {
3224 unsigned long long esize
;
3225 unsigned long long pos
;
3228 int is_dedicated
= 0;
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
);
3240 if (ddf
->phys
->entries
[dl
->pdnum
].type
&
3241 __cpu_to_be16(DDF_Spare
)) {
3242 /* Check spare assign record */
3244 if (dl
->spare
->type
& DDF_spare_dedicated
) {
3245 /* check spare_ents for guid */
3247 j
< __be16_to_cpu(dl
->spare
->populated
);
3249 if (memcmp(dl
->spare
->spare_ents
[j
].guid
,
3250 ddf
->virt
->entries
[a
->info
.container_member
].guid
,
3257 } else if (ddf
->phys
->entries
[dl
->pdnum
].type
&
3258 __cpu_to_be16(DDF_Global_Spare
)) {
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
);
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
);
3272 dprintf("cannot get extents\n");
3279 esize
= ex
[j
].start
- pos
;
3280 if (esize
>= a
->info
.component_size
)
3282 pos
= ex
[i
].start
+ ex
[i
].size
;
3284 } while (ex
[i
-1].size
);
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
);
3294 /* Cool, we have a device with some space at pos */
3295 di
= malloc(sizeof(*di
));
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
;
3304 di
->data_offset
= pos
;
3305 di
->component_size
= a
->info
.component_size
;
3306 di
->container_member
= dl
->pdnum
;
3309 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
3314 if (!dl
&& ! global_ok
) {
3315 /* not enough dedicated spares, try global */
3323 /* No spares found */
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
3329 mu
= malloc(sizeof(*mu
));
3330 if (mu
&& posix_memalign(&mu
->space
, 512, sizeof(struct vcl
)) != 0) {
3336 struct mdinfo
*n
= rv
->next
;
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);
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
;
3360 #endif /* MDASSEMBLE */
3362 struct superswitch super_ddf
= {
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
,
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
,
3377 .avail_size
= avail_size_ddf
,
3379 .compare_super
= compare_super_ddf
,
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
,
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
,