2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2006-2009 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 #define DDF_NOTFOUND (~0U)
48 #define DDF_CONTAINER (DDF_NOTFOUND-1)
50 /* The DDF metadata handling.
51 * DDF metadata lives at the end of the device.
52 * The last 512 byte block provides an 'anchor' which is used to locate
53 * the rest of the metadata which usually lives immediately behind the anchor.
56 * - all multibyte numeric fields are bigendian.
57 * - all strings are space padded.
61 /* Primary Raid Level (PRL) */
62 #define DDF_RAID0 0x00
63 #define DDF_RAID1 0x01
64 #define DDF_RAID3 0x03
65 #define DDF_RAID4 0x04
66 #define DDF_RAID5 0x05
67 #define DDF_RAID1E 0x11
69 #define DDF_CONCAT 0x1f
70 #define DDF_RAID5E 0x15
71 #define DDF_RAID5EE 0x25
72 #define DDF_RAID6 0x06
74 /* Raid Level Qualifier (RLQ) */
75 #define DDF_RAID0_SIMPLE 0x00
76 #define DDF_RAID1_SIMPLE 0x00 /* just 2 devices in this plex */
77 #define DDF_RAID1_MULTI 0x01 /* exactly 3 devices in this plex */
78 #define DDF_RAID3_0 0x00 /* parity in first extent */
79 #define DDF_RAID3_N 0x01 /* parity in last extent */
80 #define DDF_RAID4_0 0x00 /* parity in first extent */
81 #define DDF_RAID4_N 0x01 /* parity in last extent */
82 /* these apply to raid5e and raid5ee as well */
83 #define DDF_RAID5_0_RESTART 0x00 /* same as 'right asymmetric' - layout 1 */
84 #define DDF_RAID6_0_RESTART 0x01 /* raid6 different from raid5 here!!! */
85 #define DDF_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
86 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
88 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
89 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
91 /* Secondary RAID Level (SRL) */
92 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
93 #define DDF_2MIRRORED 0x01
94 #define DDF_2CONCAT 0x02
95 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
98 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
99 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
100 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
101 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
102 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
103 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
104 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
105 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
106 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
107 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
109 #define DDF_GUID_LEN 24
110 #define DDF_REVISION_0 "01.00.00"
111 #define DDF_REVISION_2 "01.02.00"
114 __u32 magic
; /* DDF_HEADER_MAGIC */
116 char guid
[DDF_GUID_LEN
];
117 char revision
[8]; /* 01.02.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
;
397 struct ddf_controller_data controller
;
398 struct ddf_header
*active
;
399 struct phys_disk
*phys
;
400 struct virtual_disk
*virt
;
402 unsigned int max_part
, mppe
, conf_rec_len
;
410 __u64
*lba_offset
; /* location in 'conf' of
412 unsigned int vcnum
; /* index into ->virt */
413 struct vd_config
**other_bvds
;
414 __u64
*block_sizes
; /* NULL if all the same */
417 struct vd_config conf
;
418 } *conflist
, *currentconf
;
427 unsigned long long size
; /* sectors */
428 unsigned long long primary_lba
; /* sectors */
429 unsigned long long secondary_lba
; /* sectors */
430 unsigned long long workspace_lba
; /* sectors */
431 int pdnum
; /* index in ->phys */
432 struct spare_assign
*spare
;
433 void *mdupdate
; /* hold metadata update */
435 /* These fields used by auto-layout */
436 int raiddisk
; /* slot to fill in autolayout */
440 struct disk_data disk
;
441 struct vcl
*vlist
[0]; /* max_part in size */
446 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
450 static int all_ff(const char *guid
);
451 static void pr_state(struct ddf_super
*ddf
, const char *msg
)
454 dprintf("%s/%s: ", __func__
, msg
);
455 for (i
= 0; i
< __be16_to_cpu(ddf
->active
->max_vd_entries
); i
++) {
456 if (all_ff(ddf
->virt
->entries
[i
].guid
))
458 dprintf("%u(s=%02x i=%02x) ", i
,
459 ddf
->virt
->entries
[i
].state
,
460 ddf
->virt
->entries
[i
].init_state
);
465 static void pr_state(const struct ddf_super
*ddf
, const char *msg
) {}
468 #define ddf_set_updates_pending(x) \
469 do { (x)->updates_pending = 1; pr_state(x, __func__); } while (0)
471 static unsigned int calc_crc(void *buf
, int len
)
473 /* crcs are always at the same place as in the ddf_header */
474 struct ddf_header
*ddf
= buf
;
475 __u32 oldcrc
= ddf
->crc
;
477 ddf
->crc
= 0xffffffff;
479 newcrc
= crc32(0, buf
, len
);
481 /* The crc is store (like everything) bigendian, so convert
482 * here for simplicity
484 return __cpu_to_be32(newcrc
);
487 #define DDF_INVALID_LEVEL 0xff
488 #define DDF_NO_SECONDARY 0xff
489 static int err_bad_md_layout(const mdu_array_info_t
*array
)
491 pr_err("RAID%d layout %x with %d disks is unsupported for DDF\n",
492 array
->level
, array
->layout
, array
->raid_disks
);
493 return DDF_INVALID_LEVEL
;
496 static int layout_md2ddf(const mdu_array_info_t
*array
,
497 struct vd_config
*conf
)
499 __u16 prim_elmnt_count
= __cpu_to_be16(array
->raid_disks
);
500 __u8 prl
= DDF_INVALID_LEVEL
, rlq
= 0;
501 __u8 sec_elmnt_count
= 1;
502 __u8 srl
= DDF_NO_SECONDARY
;
504 switch (array
->level
) {
509 rlq
= DDF_RAID0_SIMPLE
;
513 switch (array
->raid_disks
) {
515 rlq
= DDF_RAID1_SIMPLE
;
518 rlq
= DDF_RAID1_MULTI
;
521 return err_bad_md_layout(array
);
526 if (array
->layout
!= 0)
527 return err_bad_md_layout(array
);
532 switch (array
->layout
) {
533 case ALGORITHM_LEFT_ASYMMETRIC
:
534 rlq
= DDF_RAID5_N_RESTART
;
536 case ALGORITHM_RIGHT_ASYMMETRIC
:
537 rlq
= DDF_RAID5_0_RESTART
;
539 case ALGORITHM_LEFT_SYMMETRIC
:
540 rlq
= DDF_RAID5_N_CONTINUE
;
542 case ALGORITHM_RIGHT_SYMMETRIC
:
543 /* not mentioned in standard */
545 return err_bad_md_layout(array
);
550 switch (array
->layout
) {
551 case ALGORITHM_ROTATING_N_RESTART
:
552 rlq
= DDF_RAID5_N_RESTART
;
554 case ALGORITHM_ROTATING_ZERO_RESTART
:
555 rlq
= DDF_RAID6_0_RESTART
;
557 case ALGORITHM_ROTATING_N_CONTINUE
:
558 rlq
= DDF_RAID5_N_CONTINUE
;
561 return err_bad_md_layout(array
);
566 if (array
->raid_disks
% 2 == 0 && array
->layout
== 0x102) {
567 rlq
= DDF_RAID1_SIMPLE
;
568 prim_elmnt_count
= __cpu_to_be16(2);
569 sec_elmnt_count
= array
->raid_disks
/ 2;
570 } else if (array
->raid_disks
% 3 == 0
571 && array
->layout
== 0x103) {
572 rlq
= DDF_RAID1_MULTI
;
573 prim_elmnt_count
= __cpu_to_be16(3);
574 sec_elmnt_count
= array
->raid_disks
/ 3;
576 return err_bad_md_layout(array
);
581 return err_bad_md_layout(array
);
584 conf
->prim_elmnt_count
= prim_elmnt_count
;
587 conf
->sec_elmnt_count
= sec_elmnt_count
;
591 static int err_bad_ddf_layout(const struct vd_config
*conf
)
593 pr_err("DDF RAID %u qualifier %u with %u disks is unsupported\n",
594 conf
->prl
, conf
->rlq
, __be16_to_cpu(conf
->prim_elmnt_count
));
598 static int layout_ddf2md(const struct vd_config
*conf
,
599 mdu_array_info_t
*array
)
601 int level
= LEVEL_UNSUPPORTED
;
603 int raiddisks
= __be16_to_cpu(conf
->prim_elmnt_count
);
605 if (conf
->sec_elmnt_count
> 1) {
606 /* see also check_secondary() */
607 if (conf
->prl
!= DDF_RAID1
||
608 (conf
->srl
!= DDF_2STRIPED
&& conf
->srl
!= DDF_2SPANNED
)) {
609 pr_err("Unsupported secondary RAID level %u/%u\n",
610 conf
->prl
, conf
->srl
);
613 if (raiddisks
== 2 && conf
->rlq
== DDF_RAID1_SIMPLE
)
615 else if (raiddisks
== 3 && conf
->rlq
== DDF_RAID1_MULTI
)
618 return err_bad_ddf_layout(conf
);
619 raiddisks
*= conf
->sec_elmnt_count
;
626 level
= LEVEL_LINEAR
;
629 if (conf
->rlq
!= DDF_RAID0_SIMPLE
)
630 return err_bad_ddf_layout(conf
);
634 if (!((conf
->rlq
== DDF_RAID1_SIMPLE
&& raiddisks
== 2) ||
635 (conf
->rlq
== DDF_RAID1_MULTI
&& raiddisks
== 3)))
636 return err_bad_ddf_layout(conf
);
640 if (conf
->rlq
!= DDF_RAID4_N
)
641 return err_bad_ddf_layout(conf
);
646 case DDF_RAID5_N_RESTART
:
647 layout
= ALGORITHM_LEFT_ASYMMETRIC
;
649 case DDF_RAID5_0_RESTART
:
650 layout
= ALGORITHM_RIGHT_ASYMMETRIC
;
652 case DDF_RAID5_N_CONTINUE
:
653 layout
= ALGORITHM_LEFT_SYMMETRIC
;
656 return err_bad_ddf_layout(conf
);
662 case DDF_RAID5_N_RESTART
:
663 layout
= ALGORITHM_ROTATING_N_RESTART
;
665 case DDF_RAID6_0_RESTART
:
666 layout
= ALGORITHM_ROTATING_ZERO_RESTART
;
668 case DDF_RAID5_N_CONTINUE
:
669 layout
= ALGORITHM_ROTATING_N_CONTINUE
;
672 return err_bad_ddf_layout(conf
);
677 return err_bad_ddf_layout(conf
);
681 array
->level
= level
;
682 array
->layout
= layout
;
683 array
->raid_disks
= raiddisks
;
687 static int load_ddf_header(int fd
, unsigned long long lba
,
688 unsigned long long size
,
690 struct ddf_header
*hdr
, struct ddf_header
*anchor
)
692 /* read a ddf header (primary or secondary) from fd/lba
693 * and check that it is consistent with anchor
695 * magic, crc, guid, rev, and LBA's header_type, and
696 * everything after header_type must be the same
701 if (lseek64(fd
, lba
<<9, 0) < 0)
704 if (read(fd
, hdr
, 512) != 512)
707 if (hdr
->magic
!= DDF_HEADER_MAGIC
)
709 if (calc_crc(hdr
, 512) != hdr
->crc
)
711 if (memcmp(anchor
->guid
, hdr
->guid
, DDF_GUID_LEN
) != 0 ||
712 memcmp(anchor
->revision
, hdr
->revision
, 8) != 0 ||
713 anchor
->primary_lba
!= hdr
->primary_lba
||
714 anchor
->secondary_lba
!= hdr
->secondary_lba
||
716 memcmp(anchor
->pad2
, hdr
->pad2
, 512 -
717 offsetof(struct ddf_header
, pad2
)) != 0)
720 /* Looks good enough to me... */
724 static void *load_section(int fd
, struct ddf_super
*super
, void *buf
,
725 __u32 offset_be
, __u32 len_be
, int check
)
727 unsigned long long offset
= __be32_to_cpu(offset_be
);
728 unsigned long long len
= __be32_to_cpu(len_be
);
729 int dofree
= (buf
== NULL
);
732 if (len
!= 2 && len
!= 8 && len
!= 32
733 && len
!= 128 && len
!= 512)
739 /* All pre-allocated sections are a single block */
742 } else if (posix_memalign(&buf
, 512, len
<<9) != 0)
748 if (super
->active
->type
== 1)
749 offset
+= __be64_to_cpu(super
->active
->primary_lba
);
751 offset
+= __be64_to_cpu(super
->active
->secondary_lba
);
753 if ((unsigned long long)lseek64(fd
, offset
<<9, 0) != (offset
<<9)) {
758 if ((unsigned long long)read(fd
, buf
, len
<<9) != (len
<<9)) {
766 static int load_ddf_headers(int fd
, struct ddf_super
*super
, char *devname
)
768 unsigned long long dsize
;
770 get_dev_size(fd
, NULL
, &dsize
);
772 if (lseek64(fd
, dsize
-512, 0) < 0) {
774 pr_err("Cannot seek to anchor block on %s: %s\n",
775 devname
, strerror(errno
));
778 if (read(fd
, &super
->anchor
, 512) != 512) {
780 pr_err("Cannot read anchor block on %s: %s\n",
781 devname
, strerror(errno
));
784 if (super
->anchor
.magic
!= DDF_HEADER_MAGIC
) {
786 pr_err("no DDF anchor found on %s\n",
790 if (calc_crc(&super
->anchor
, 512) != super
->anchor
.crc
) {
792 pr_err("bad CRC on anchor on %s\n",
796 if (memcmp(super
->anchor
.revision
, DDF_REVISION_0
, 8) != 0 &&
797 memcmp(super
->anchor
.revision
, DDF_REVISION_2
, 8) != 0) {
799 pr_err("can only support super revision"
800 " %.8s and earlier, not %.8s on %s\n",
801 DDF_REVISION_2
, super
->anchor
.revision
,devname
);
804 super
->active
= NULL
;
805 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.primary_lba
),
807 &super
->primary
, &super
->anchor
) == 0) {
809 pr_err("Failed to load primary DDF header "
812 super
->active
= &super
->primary
;
813 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.secondary_lba
),
815 &super
->secondary
, &super
->anchor
)) {
816 if ((__be32_to_cpu(super
->primary
.seq
)
817 < __be32_to_cpu(super
->secondary
.seq
) &&
818 !super
->secondary
.openflag
)
819 || (__be32_to_cpu(super
->primary
.seq
)
820 == __be32_to_cpu(super
->secondary
.seq
) &&
821 super
->primary
.openflag
&& !super
->secondary
.openflag
)
822 || super
->active
== NULL
824 super
->active
= &super
->secondary
;
826 pr_err("Failed to load secondary DDF header on %s\n",
828 if (super
->active
== NULL
)
833 static int load_ddf_global(int fd
, struct ddf_super
*super
, char *devname
)
836 ok
= load_section(fd
, super
, &super
->controller
,
837 super
->active
->controller_section_offset
,
838 super
->active
->controller_section_length
,
840 super
->phys
= load_section(fd
, super
, NULL
,
841 super
->active
->phys_section_offset
,
842 super
->active
->phys_section_length
,
844 super
->pdsize
= __be32_to_cpu(super
->active
->phys_section_length
) * 512;
846 super
->virt
= load_section(fd
, super
, NULL
,
847 super
->active
->virt_section_offset
,
848 super
->active
->virt_section_length
,
850 super
->vdsize
= __be32_to_cpu(super
->active
->virt_section_length
) * 512;
860 super
->conflist
= NULL
;
863 super
->max_part
= __be16_to_cpu(super
->active
->max_partitions
);
864 super
->mppe
= __be16_to_cpu(super
->active
->max_primary_element_entries
);
865 super
->conf_rec_len
= __be16_to_cpu(super
->active
->config_record_len
);
869 static void add_other_bvd(struct vcl
*vcl
, struct vd_config
*vd
,
873 for (i
= 0; i
< vcl
->conf
.sec_elmnt_count
-1; i
++)
874 if (vcl
->other_bvds
[i
] != NULL
&&
875 vcl
->other_bvds
[i
]->sec_elmnt_seq
== vd
->sec_elmnt_seq
)
878 if (i
< vcl
->conf
.sec_elmnt_count
-1) {
879 if (vd
->seqnum
<= vcl
->other_bvds
[i
]->seqnum
)
882 for (i
= 0; i
< vcl
->conf
.sec_elmnt_count
-1; i
++)
883 if (vcl
->other_bvds
[i
] == NULL
)
885 if (i
== vcl
->conf
.sec_elmnt_count
-1) {
886 pr_err("no space for sec level config %u, count is %u\n",
887 vd
->sec_elmnt_seq
, vcl
->conf
.sec_elmnt_count
);
890 if (posix_memalign((void **)&vcl
->other_bvds
[i
], 512, len
)
892 pr_err("%s could not allocate vd buf\n", __func__
);
896 memcpy(vcl
->other_bvds
[i
], vd
, len
);
899 static int load_ddf_local(int fd
, struct ddf_super
*super
,
900 char *devname
, int keep
)
906 unsigned int confsec
;
908 unsigned int max_virt_disks
= __be16_to_cpu(super
->active
->max_vd_entries
);
909 unsigned long long dsize
;
911 /* First the local disk info */
912 if (posix_memalign((void**)&dl
, 512,
914 (super
->max_part
) * sizeof(dl
->vlist
[0])) != 0) {
915 pr_err("%s could not allocate disk info buffer\n",
920 load_section(fd
, super
, &dl
->disk
,
921 super
->active
->data_section_offset
,
922 super
->active
->data_section_length
,
924 dl
->devname
= devname
? xstrdup(devname
) : NULL
;
927 dl
->major
= major(stb
.st_rdev
);
928 dl
->minor
= minor(stb
.st_rdev
);
929 dl
->next
= super
->dlist
;
930 dl
->fd
= keep
? fd
: -1;
933 if (get_dev_size(fd
, devname
, &dsize
))
934 dl
->size
= dsize
>> 9;
935 /* If the disks have different sizes, the LBAs will differ
936 * between phys disks.
937 * At this point here, the values in super->active must be valid
938 * for this phys disk. */
939 dl
->primary_lba
= super
->active
->primary_lba
;
940 dl
->secondary_lba
= super
->active
->secondary_lba
;
941 dl
->workspace_lba
= super
->active
->workspace_lba
;
943 for (i
= 0 ; i
< super
->max_part
; i
++)
947 for (i
= 0; i
< __be16_to_cpu(super
->active
->max_pd_entries
); i
++)
948 if (memcmp(super
->phys
->entries
[i
].guid
,
949 dl
->disk
.guid
, DDF_GUID_LEN
) == 0)
952 /* Now the config list. */
953 /* 'conf' is an array of config entries, some of which are
954 * probably invalid. Those which are good need to be copied into
958 conf
= load_section(fd
, super
, NULL
,
959 super
->active
->config_section_offset
,
960 super
->active
->config_section_length
,
965 confsec
< __be32_to_cpu(super
->active
->config_section_length
);
966 confsec
+= super
->conf_rec_len
) {
967 struct vd_config
*vd
=
968 (struct vd_config
*)((char*)conf
+ confsec
*512);
971 if (vd
->magic
== DDF_SPARE_ASSIGN_MAGIC
) {
974 if (posix_memalign((void**)&dl
->spare
, 512,
975 super
->conf_rec_len
*512) != 0) {
976 pr_err("%s could not allocate spare info buf\n",
981 memcpy(dl
->spare
, vd
, super
->conf_rec_len
*512);
984 if (vd
->magic
!= DDF_VD_CONF_MAGIC
)
986 for (vcl
= super
->conflist
; vcl
; vcl
= vcl
->next
) {
987 if (memcmp(vcl
->conf
.guid
,
988 vd
->guid
, DDF_GUID_LEN
) == 0)
993 dl
->vlist
[vnum
++] = vcl
;
994 if (vcl
->other_bvds
!= NULL
&&
995 vcl
->conf
.sec_elmnt_seq
!= vd
->sec_elmnt_seq
) {
996 add_other_bvd(vcl
, vd
, super
->conf_rec_len
*512);
999 if (__be32_to_cpu(vd
->seqnum
) <=
1000 __be32_to_cpu(vcl
->conf
.seqnum
))
1003 if (posix_memalign((void**)&vcl
, 512,
1004 (super
->conf_rec_len
*512 +
1005 offsetof(struct vcl
, conf
))) != 0) {
1006 pr_err("%s could not allocate vcl buf\n",
1010 vcl
->next
= super
->conflist
;
1011 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
1012 if (vd
->sec_elmnt_count
> 1)
1014 xcalloc(vd
->sec_elmnt_count
- 1,
1015 sizeof(struct vd_config
*));
1017 vcl
->other_bvds
= NULL
;
1018 super
->conflist
= vcl
;
1019 dl
->vlist
[vnum
++] = vcl
;
1021 memcpy(&vcl
->conf
, vd
, super
->conf_rec_len
*512);
1022 vcl
->lba_offset
= (__u64
*)
1023 &vcl
->conf
.phys_refnum
[super
->mppe
];
1025 for (i
=0; i
< max_virt_disks
; i
++)
1026 if (memcmp(super
->virt
->entries
[i
].guid
,
1027 vcl
->conf
.guid
, DDF_GUID_LEN
)==0)
1029 if (i
< max_virt_disks
)
1038 static int load_super_ddf_all(struct supertype
*st
, int fd
,
1039 void **sbp
, char *devname
);
1042 static void free_super_ddf(struct supertype
*st
);
1044 static int load_super_ddf(struct supertype
*st
, int fd
,
1047 unsigned long long dsize
;
1048 struct ddf_super
*super
;
1051 if (get_dev_size(fd
, devname
, &dsize
) == 0)
1054 if (!st
->ignore_hw_compat
&& test_partition(fd
))
1055 /* DDF is not allowed on partitions */
1058 /* 32M is a lower bound */
1059 if (dsize
<= 32*1024*1024) {
1061 pr_err("%s is too small for ddf: "
1062 "size is %llu sectors.\n",
1068 pr_err("%s is an odd size for ddf: "
1069 "size is %llu bytes.\n",
1076 if (posix_memalign((void**)&super
, 512, sizeof(*super
))!= 0) {
1077 pr_err("malloc of %zu failed.\n",
1081 memset(super
, 0, sizeof(*super
));
1083 rv
= load_ddf_headers(fd
, super
, devname
);
1089 /* Have valid headers and have chosen the best. Let's read in the rest*/
1091 rv
= load_ddf_global(fd
, super
, devname
);
1095 pr_err("Failed to load all information "
1096 "sections on %s\n", devname
);
1101 rv
= load_ddf_local(fd
, super
, devname
, 0);
1105 pr_err("Failed to load all information "
1106 "sections on %s\n", devname
);
1111 /* Should possibly check the sections .... */
1114 if (st
->ss
== NULL
) {
1115 st
->ss
= &super_ddf
;
1116 st
->minor_version
= 0;
1123 static void free_super_ddf(struct supertype
*st
)
1125 struct ddf_super
*ddf
= st
->sb
;
1130 while (ddf
->conflist
) {
1131 struct vcl
*v
= ddf
->conflist
;
1132 ddf
->conflist
= v
->next
;
1134 free(v
->block_sizes
);
1135 if (v
->other_bvds
) {
1137 for (i
= 0; i
< v
->conf
.sec_elmnt_count
-1; i
++)
1138 if (v
->other_bvds
[i
] != NULL
)
1139 free(v
->other_bvds
[i
]);
1140 free(v
->other_bvds
);
1144 while (ddf
->dlist
) {
1145 struct dl
*d
= ddf
->dlist
;
1146 ddf
->dlist
= d
->next
;
1153 while (ddf
->add_list
) {
1154 struct dl
*d
= ddf
->add_list
;
1155 ddf
->add_list
= d
->next
;
1166 static struct supertype
*match_metadata_desc_ddf(char *arg
)
1168 /* 'ddf' only support containers */
1169 struct supertype
*st
;
1170 if (strcmp(arg
, "ddf") != 0 &&
1171 strcmp(arg
, "default") != 0
1175 st
= xcalloc(1, sizeof(*st
));
1176 st
->ss
= &super_ddf
;
1178 st
->minor_version
= 0;
1185 static mapping_t ddf_state
[] = {
1191 { "Partially Optimal", 5},
1197 static mapping_t ddf_init_state
[] = {
1198 { "Not Initialised", 0},
1199 { "QuickInit in Progress", 1},
1200 { "Fully Initialised", 2},
1204 static mapping_t ddf_access
[] = {
1208 { "Blocked (no access)", 3},
1212 static mapping_t ddf_level
[] = {
1213 { "RAID0", DDF_RAID0
},
1214 { "RAID1", DDF_RAID1
},
1215 { "RAID3", DDF_RAID3
},
1216 { "RAID4", DDF_RAID4
},
1217 { "RAID5", DDF_RAID5
},
1218 { "RAID1E",DDF_RAID1E
},
1219 { "JBOD", DDF_JBOD
},
1220 { "CONCAT",DDF_CONCAT
},
1221 { "RAID5E",DDF_RAID5E
},
1222 { "RAID5EE",DDF_RAID5EE
},
1223 { "RAID6", DDF_RAID6
},
1226 static mapping_t ddf_sec_level
[] = {
1227 { "Striped", DDF_2STRIPED
},
1228 { "Mirrored", DDF_2MIRRORED
},
1229 { "Concat", DDF_2CONCAT
},
1230 { "Spanned", DDF_2SPANNED
},
1235 static int all_ff(const char *guid
)
1238 for (i
= 0; i
< DDF_GUID_LEN
; i
++)
1239 if (guid
[i
] != (char)0xff)
1245 static void print_guid(char *guid
, int tstamp
)
1247 /* A GUIDs are part (or all) ASCII and part binary.
1248 * They tend to be space padded.
1249 * We print the GUID in HEX, then in parentheses add
1250 * any initial ASCII sequence, and a possible
1251 * time stamp from bytes 16-19
1253 int l
= DDF_GUID_LEN
;
1256 for (i
=0 ; i
<DDF_GUID_LEN
; i
++) {
1257 if ((i
&3)==0 && i
!= 0) printf(":");
1258 printf("%02X", guid
[i
]&255);
1262 while (l
&& guid
[l
-1] == ' ')
1264 for (i
=0 ; i
<l
; i
++) {
1265 if (guid
[i
] >= 0x20 && guid
[i
] < 0x7f)
1266 fputc(guid
[i
], stdout
);
1271 time_t then
= __be32_to_cpu(*(__u32
*)(guid
+16)) + DECADE
;
1274 tm
= localtime(&then
);
1275 strftime(tbuf
, 100, " %D %T",tm
);
1276 fputs(tbuf
, stdout
);
1281 static void examine_vd(int n
, struct ddf_super
*sb
, char *guid
)
1283 int crl
= sb
->conf_rec_len
;
1286 for (vcl
= sb
->conflist
; vcl
; vcl
= vcl
->next
) {
1288 struct vd_config
*vc
= &vcl
->conf
;
1290 if (calc_crc(vc
, crl
*512) != vc
->crc
)
1292 if (memcmp(vc
->guid
, guid
, DDF_GUID_LEN
) != 0)
1295 /* Ok, we know about this VD, let's give more details */
1296 printf(" Raid Devices[%d] : %d (", n
,
1297 __be16_to_cpu(vc
->prim_elmnt_count
));
1298 for (i
= 0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++) {
1300 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1301 for (j
=0; j
<cnt
; j
++)
1302 if (vc
->phys_refnum
[i
] == sb
->phys
->entries
[j
].refnum
)
1311 if (vc
->chunk_shift
!= 255)
1312 printf(" Chunk Size[%d] : %d sectors\n", n
,
1313 1 << vc
->chunk_shift
);
1314 printf(" Raid Level[%d] : %s\n", n
,
1315 map_num(ddf_level
, vc
->prl
)?:"-unknown-");
1316 if (vc
->sec_elmnt_count
!= 1) {
1317 printf(" Secondary Position[%d] : %d of %d\n", n
,
1318 vc
->sec_elmnt_seq
, vc
->sec_elmnt_count
);
1319 printf(" Secondary Level[%d] : %s\n", n
,
1320 map_num(ddf_sec_level
, vc
->srl
) ?: "-unknown-");
1322 printf(" Device Size[%d] : %llu\n", n
,
1323 (unsigned long long)__be64_to_cpu(vc
->blocks
)/2);
1324 printf(" Array Size[%d] : %llu\n", n
,
1325 (unsigned long long)__be64_to_cpu(vc
->array_blocks
)/2);
1329 static void examine_vds(struct ddf_super
*sb
)
1331 int cnt
= __be16_to_cpu(sb
->virt
->populated_vdes
);
1333 printf(" Virtual Disks : %d\n", cnt
);
1335 for (i
= 0; i
< __be16_to_cpu(sb
->virt
->max_vdes
); i
++) {
1336 struct virtual_entry
*ve
= &sb
->virt
->entries
[i
];
1337 if (all_ff(ve
->guid
))
1340 printf(" VD GUID[%d] : ", i
); print_guid(ve
->guid
, 1);
1342 printf(" unit[%d] : %d\n", i
, __be16_to_cpu(ve
->unit
));
1343 printf(" state[%d] : %s, %s%s\n", i
,
1344 map_num(ddf_state
, ve
->state
& 7),
1345 (ve
->state
& 8) ? "Morphing, ": "",
1346 (ve
->state
& 16)? "Not Consistent" : "Consistent");
1347 printf(" init state[%d] : %s\n", i
,
1348 map_num(ddf_init_state
, ve
->init_state
&3));
1349 printf(" access[%d] : %s\n", i
,
1350 map_num(ddf_access
, (ve
->init_state
>>6) & 3));
1351 printf(" Name[%d] : %.16s\n", i
, ve
->name
);
1352 examine_vd(i
, sb
, ve
->guid
);
1354 if (cnt
) printf("\n");
1357 static void examine_pds(struct ddf_super
*sb
)
1359 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1362 printf(" Physical Disks : %d\n", cnt
);
1363 printf(" Number RefNo Size Device Type/State\n");
1365 for (i
=0 ; i
<cnt
; i
++) {
1366 struct phys_disk_entry
*pd
= &sb
->phys
->entries
[i
];
1367 int type
= __be16_to_cpu(pd
->type
);
1368 int state
= __be16_to_cpu(pd
->state
);
1370 //printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1372 printf(" %3d %08x ", i
,
1373 __be32_to_cpu(pd
->refnum
));
1375 (unsigned long long)__be64_to_cpu(pd
->config_size
)>>1);
1376 for (dl
= sb
->dlist
; dl
; dl
= dl
->next
) {
1377 if (dl
->disk
.refnum
== pd
->refnum
) {
1378 char *dv
= map_dev(dl
->major
, dl
->minor
, 0);
1380 printf("%-15s", dv
);
1387 printf(" %s%s%s%s%s",
1388 (type
&2) ? "active":"",
1389 (type
&4) ? "Global-Spare":"",
1390 (type
&8) ? "spare" : "",
1391 (type
&16)? ", foreign" : "",
1392 (type
&32)? "pass-through" : "");
1393 if (state
& DDF_Failed
)
1394 /* This over-rides these three */
1395 state
&= ~(DDF_Online
|DDF_Rebuilding
|DDF_Transition
);
1396 printf("/%s%s%s%s%s%s%s",
1397 (state
&1)? "Online": "Offline",
1398 (state
&2)? ", Failed": "",
1399 (state
&4)? ", Rebuilding": "",
1400 (state
&8)? ", in-transition": "",
1401 (state
&16)? ", SMART-errors": "",
1402 (state
&32)? ", Unrecovered-Read-Errors": "",
1403 (state
&64)? ", Missing" : "");
1408 static void examine_super_ddf(struct supertype
*st
, char *homehost
)
1410 struct ddf_super
*sb
= st
->sb
;
1412 printf(" Magic : %08x\n", __be32_to_cpu(sb
->anchor
.magic
));
1413 printf(" Version : %.8s\n", sb
->anchor
.revision
);
1414 printf("Controller GUID : "); print_guid(sb
->controller
.guid
, 0);
1416 printf(" Container GUID : "); print_guid(sb
->anchor
.guid
, 1);
1418 printf(" Seq : %08x\n", __be32_to_cpu(sb
->active
->seq
));
1419 printf(" Redundant hdr : %s\n", sb
->secondary
.magic
== DDF_HEADER_MAGIC
1425 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1427 static void uuid_from_ddf_guid(const char *guid
, int uuid
[4]);
1428 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4]);
1430 static unsigned int get_vd_num_of_subarray(struct supertype
*st
)
1433 * Figure out the VD number for this supertype.
1434 * Returns DDF_CONTAINER for the container itself,
1435 * and DDF_NOTFOUND on error.
1437 struct ddf_super
*ddf
= st
->sb
;
1442 if (*st
->container_devnm
== '\0')
1443 return DDF_CONTAINER
;
1445 sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
1446 if (!sra
|| sra
->array
.major_version
!= -1 ||
1447 sra
->array
.minor_version
!= -2 ||
1448 !is_subarray(sra
->text_version
))
1449 return DDF_NOTFOUND
;
1451 sub
= strchr(sra
->text_version
+ 1, '/');
1453 vcnum
= strtoul(sub
+ 1, &end
, 10);
1454 if (sub
== NULL
|| *sub
== '\0' || *end
!= '\0' ||
1455 vcnum
>= __be16_to_cpu(ddf
->active
->max_vd_entries
))
1456 return DDF_NOTFOUND
;
1461 static void brief_examine_super_ddf(struct supertype
*st
, int verbose
)
1463 /* We just write a generic DDF ARRAY entry
1467 getinfo_super_ddf(st
, &info
, NULL
);
1468 fname_from_uuid(st
, &info
, nbuf
, ':');
1470 printf("ARRAY metadata=ddf UUID=%s\n", nbuf
+ 5);
1473 static void brief_examine_subarrays_ddf(struct supertype
*st
, int verbose
)
1475 /* We just write a generic DDF ARRAY entry
1477 struct ddf_super
*ddf
= st
->sb
;
1481 getinfo_super_ddf(st
, &info
, NULL
);
1482 fname_from_uuid(st
, &info
, nbuf
, ':');
1484 for (i
= 0; i
< __be16_to_cpu(ddf
->virt
->max_vdes
); i
++) {
1485 struct virtual_entry
*ve
= &ddf
->virt
->entries
[i
];
1488 if (all_ff(ve
->guid
))
1490 memcpy(vcl
.conf
.guid
, ve
->guid
, DDF_GUID_LEN
);
1491 ddf
->currentconf
=&vcl
;
1492 uuid_from_super_ddf(st
, info
.uuid
);
1493 fname_from_uuid(st
, &info
, nbuf1
, ':');
1494 printf("ARRAY container=%s member=%d UUID=%s\n",
1495 nbuf
+5, i
, nbuf1
+5);
1499 static void export_examine_super_ddf(struct supertype
*st
)
1503 getinfo_super_ddf(st
, &info
, NULL
);
1504 fname_from_uuid(st
, &info
, nbuf
, ':');
1505 printf("MD_METADATA=ddf\n");
1506 printf("MD_LEVEL=container\n");
1507 printf("MD_UUID=%s\n", nbuf
+5);
1510 static int copy_metadata_ddf(struct supertype
*st
, int from
, int to
)
1513 unsigned long long dsize
, offset
;
1515 struct ddf_header
*ddf
;
1518 /* The meta consists of an anchor, a primary, and a secondary.
1519 * This all lives at the end of the device.
1520 * So it is easiest to find the earliest of primary and
1521 * secondary, and copy everything from there.
1523 * Anchor is 512 from end It contains primary_lba and secondary_lba
1524 * we choose one of those
1527 if (posix_memalign(&buf
, 4096, 4096) != 0)
1530 if (!get_dev_size(from
, NULL
, &dsize
))
1533 if (lseek64(from
, dsize
-512, 0) < 0)
1535 if (read(from
, buf
, 512) != 512)
1538 if (ddf
->magic
!= DDF_HEADER_MAGIC
||
1539 calc_crc(ddf
, 512) != ddf
->crc
||
1540 (memcmp(ddf
->revision
, DDF_REVISION_0
, 8) != 0 &&
1541 memcmp(ddf
->revision
, DDF_REVISION_2
, 8) != 0))
1544 offset
= dsize
- 512;
1545 if ((__be64_to_cpu(ddf
->primary_lba
) << 9) < offset
)
1546 offset
= __be64_to_cpu(ddf
->primary_lba
) << 9;
1547 if ((__be64_to_cpu(ddf
->secondary_lba
) << 9) < offset
)
1548 offset
= __be64_to_cpu(ddf
->secondary_lba
) << 9;
1550 bytes
= dsize
- offset
;
1552 if (lseek64(from
, offset
, 0) < 0 ||
1553 lseek64(to
, offset
, 0) < 0)
1555 while (written
< bytes
) {
1556 int n
= bytes
- written
;
1559 if (read(from
, buf
, n
) != n
)
1561 if (write(to
, buf
, n
) != n
)
1572 static void detail_super_ddf(struct supertype
*st
, char *homehost
)
1575 * Could print DDF GUID
1576 * Need to find which array
1577 * If whole, briefly list all arrays
1582 static void brief_detail_super_ddf(struct supertype
*st
)
1586 struct ddf_super
*ddf
= st
->sb
;
1587 unsigned int vcnum
= get_vd_num_of_subarray(st
);
1588 if (vcnum
== DDF_CONTAINER
)
1589 uuid_from_super_ddf(st
, info
.uuid
);
1590 else if (vcnum
== DDF_NOTFOUND
)
1593 uuid_from_ddf_guid(ddf
->virt
->entries
[vcnum
].guid
, info
.uuid
);
1594 fname_from_uuid(st
, &info
, nbuf
,':');
1595 printf(" UUID=%s", nbuf
+ 5);
1599 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1601 /* It matches 'this' host if the controller is a
1602 * Linux-MD controller with vendor_data matching
1605 struct ddf_super
*ddf
= st
->sb
;
1610 len
= strlen(homehost
);
1612 return (memcmp(ddf
->controller
.guid
, T10
, 8) == 0 &&
1613 len
< sizeof(ddf
->controller
.vendor_data
) &&
1614 memcmp(ddf
->controller
.vendor_data
, homehost
,len
) == 0 &&
1615 ddf
->controller
.vendor_data
[len
] == 0);
1619 static int find_index_in_bvd(const struct ddf_super
*ddf
,
1620 const struct vd_config
*conf
, unsigned int n
,
1621 unsigned int *n_bvd
)
1624 * Find the index of the n-th valid physical disk in this BVD
1627 for (i
= 0, j
= 0; i
< ddf
->mppe
&&
1628 j
< __be16_to_cpu(conf
->prim_elmnt_count
); i
++) {
1629 if (conf
->phys_refnum
[i
] != 0xffffffff) {
1637 dprintf("%s: couldn't find BVD member %u (total %u)\n",
1638 __func__
, n
, __be16_to_cpu(conf
->prim_elmnt_count
));
1642 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
, unsigned int inst
,
1644 unsigned int *n_bvd
, struct vcl
**vcl
)
1648 for (v
= ddf
->conflist
; v
; v
= v
->next
) {
1649 unsigned int nsec
, ibvd
;
1650 struct vd_config
*conf
;
1651 if (inst
!= v
->vcnum
)
1654 if (conf
->sec_elmnt_count
== 1) {
1655 if (find_index_in_bvd(ddf
, conf
, n
, n_bvd
)) {
1661 if (v
->other_bvds
== NULL
) {
1662 pr_err("%s: BUG: other_bvds is NULL, nsec=%u\n",
1663 __func__
, conf
->sec_elmnt_count
);
1666 nsec
= n
/ __be16_to_cpu(conf
->prim_elmnt_count
);
1667 if (conf
->sec_elmnt_seq
!= nsec
) {
1668 for (ibvd
= 1; ibvd
< conf
->sec_elmnt_count
; ibvd
++) {
1669 if (v
->other_bvds
[ibvd
-1] == NULL
)
1671 if (v
->other_bvds
[ibvd
-1]->sec_elmnt_seq
1675 if (ibvd
== conf
->sec_elmnt_count
)
1677 conf
= v
->other_bvds
[ibvd
-1];
1679 if (!find_index_in_bvd(ddf
, conf
,
1680 n
- nsec
*conf
->sec_elmnt_count
, n_bvd
))
1682 dprintf("%s: found disk %u as member %u in bvd %d of array %u\n"
1683 , __func__
, n
, *n_bvd
, ibvd
-1, inst
);
1688 pr_err("%s: Could't find disk %d in array %u\n", __func__
, n
, inst
);
1693 static int find_phys(const struct ddf_super
*ddf
, __u32 phys_refnum
)
1695 /* Find the entry in phys_disk which has the given refnum
1696 * and return it's index
1699 for (i
= 0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
); i
++)
1700 if (ddf
->phys
->entries
[i
].refnum
== phys_refnum
)
1705 static void uuid_from_ddf_guid(const char *guid
, int uuid
[4])
1708 struct sha1_ctx ctx
;
1709 sha1_init_ctx(&ctx
);
1710 sha1_process_bytes(guid
, DDF_GUID_LEN
, &ctx
);
1711 sha1_finish_ctx(&ctx
, buf
);
1712 memcpy(uuid
, buf
, 4*4);
1715 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
1717 /* The uuid returned here is used for:
1718 * uuid to put into bitmap file (Create, Grow)
1719 * uuid for backup header when saving critical section (Grow)
1720 * comparing uuids when re-adding a device into an array
1721 * In these cases the uuid required is that of the data-array,
1722 * not the device-set.
1723 * uuid to recognise same set when adding a missing device back
1724 * to an array. This is a uuid for the device-set.
1726 * For each of these we can make do with a truncated
1727 * or hashed uuid rather than the original, as long as
1729 * In the case of SVD we assume the BVD is of interest,
1730 * though that might be the case if a bitmap were made for
1731 * a mirrored SVD - worry about that later.
1732 * So we need to find the VD configuration record for the
1733 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1734 * The first 16 bytes of the sha1 of these is used.
1736 struct ddf_super
*ddf
= st
->sb
;
1737 struct vcl
*vcl
= ddf
->currentconf
;
1741 guid
= vcl
->conf
.guid
;
1743 guid
= ddf
->anchor
.guid
;
1744 uuid_from_ddf_guid(guid
, uuid
);
1747 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1749 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1751 struct ddf_super
*ddf
= st
->sb
;
1752 int map_disks
= info
->array
.raid_disks
;
1755 if (ddf
->currentconf
) {
1756 getinfo_super_ddf_bvd(st
, info
, map
);
1759 memset(info
, 0, sizeof(*info
));
1761 info
->array
.raid_disks
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1762 info
->array
.level
= LEVEL_CONTAINER
;
1763 info
->array
.layout
= 0;
1764 info
->array
.md_minor
= -1;
1765 cptr
= (__u32
*)(ddf
->anchor
.guid
+ 16);
1766 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1768 info
->array
.utime
= 0;
1769 info
->array
.chunk_size
= 0;
1770 info
->container_enough
= 1;
1772 info
->disk
.major
= 0;
1773 info
->disk
.minor
= 0;
1775 info
->disk
.number
= __be32_to_cpu(ddf
->dlist
->disk
.refnum
);
1776 info
->disk
.raid_disk
= find_phys(ddf
, ddf
->dlist
->disk
.refnum
);
1778 info
->data_offset
= __be64_to_cpu(ddf
->phys
->
1779 entries
[info
->disk
.raid_disk
].
1781 info
->component_size
= ddf
->dlist
->size
- info
->data_offset
;
1783 info
->disk
.number
= -1;
1784 info
->disk
.raid_disk
= -1;
1785 // info->disk.raid_disk = find refnum in the table and use index;
1787 info
->disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
1789 info
->recovery_start
= MaxSector
;
1790 info
->reshape_active
= 0;
1791 info
->recovery_blocked
= 0;
1794 info
->array
.major_version
= -1;
1795 info
->array
.minor_version
= -2;
1796 strcpy(info
->text_version
, "ddf");
1797 info
->safe_mode_delay
= 0;
1799 uuid_from_super_ddf(st
, info
->uuid
);
1803 for (i
= 0 ; i
< map_disks
; i
++) {
1804 if (i
< info
->array
.raid_disks
&&
1805 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1806 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1814 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1816 struct ddf_super
*ddf
= st
->sb
;
1817 struct vcl
*vc
= ddf
->currentconf
;
1818 int cd
= ddf
->currentdev
;
1821 int map_disks
= info
->array
.raid_disks
;
1824 memset(info
, 0, sizeof(*info
));
1825 if (layout_ddf2md(&vc
->conf
, &info
->array
) == -1)
1827 info
->array
.md_minor
= -1;
1828 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
1829 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1830 info
->array
.utime
= DECADE
+ __be32_to_cpu(vc
->conf
.timestamp
);
1831 info
->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
1832 info
->custom_array_size
= 0;
1834 if (cd
>= 0 && (unsigned)cd
< ddf
->mppe
) {
1835 info
->data_offset
= __be64_to_cpu(vc
->lba_offset
[cd
]);
1836 if (vc
->block_sizes
)
1837 info
->component_size
= vc
->block_sizes
[cd
];
1839 info
->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
1842 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
1843 if (dl
->raiddisk
== ddf
->currentdev
)
1846 info
->disk
.major
= 0;
1847 info
->disk
.minor
= 0;
1848 info
->disk
.state
= 0;
1850 info
->disk
.major
= dl
->major
;
1851 info
->disk
.minor
= dl
->minor
;
1852 info
->disk
.raid_disk
= dl
->raiddisk
;
1853 info
->disk
.number
= dl
->pdnum
;
1854 info
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
1857 info
->container_member
= ddf
->currentconf
->vcnum
;
1859 info
->recovery_start
= MaxSector
;
1860 info
->resync_start
= 0;
1861 info
->reshape_active
= 0;
1862 info
->recovery_blocked
= 0;
1863 if (!(ddf
->virt
->entries
[info
->container_member
].state
1864 & DDF_state_inconsistent
) &&
1865 (ddf
->virt
->entries
[info
->container_member
].init_state
1866 & DDF_initstate_mask
)
1868 info
->resync_start
= MaxSector
;
1870 uuid_from_super_ddf(st
, info
->uuid
);
1872 info
->array
.major_version
= -1;
1873 info
->array
.minor_version
= -2;
1874 sprintf(info
->text_version
, "/%s/%d",
1875 st
->container_devnm
,
1876 info
->container_member
);
1877 info
->safe_mode_delay
= 200;
1879 memcpy(info
->name
, ddf
->virt
->entries
[info
->container_member
].name
, 16);
1882 if (info
->name
[j
] == ' ')
1886 for (j
= 0; j
< map_disks
; j
++) {
1888 if (j
< info
->array
.raid_disks
) {
1889 int i
= find_phys(ddf
, vc
->conf
.phys_refnum
[j
]);
1891 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1892 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1898 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1900 char *devname
, int verbose
,
1901 int uuid_set
, char *homehost
)
1903 /* For 'assemble' and 'force' we need to return non-zero if any
1904 * change was made. For others, the return value is ignored.
1905 * Update options are:
1906 * force-one : This device looks a bit old but needs to be included,
1907 * update age info appropriately.
1908 * assemble: clear any 'faulty' flag to allow this device to
1910 * force-array: Array is degraded but being forced, mark it clean
1911 * if that will be needed to assemble it.
1913 * newdev: not used ????
1914 * grow: Array has gained a new device - this is currently for
1916 * resync: mark as dirty so a resync will happen.
1917 * uuid: Change the uuid of the array to match what is given
1918 * homehost: update the recorded homehost
1919 * name: update the name - preserving the homehost
1920 * _reshape_progress: record new reshape_progress position.
1922 * Following are not relevant for this version:
1923 * sparc2.2 : update from old dodgey metadata
1924 * super-minor: change the preferred_minor number
1925 * summaries: update redundant counters.
1928 // struct ddf_super *ddf = st->sb;
1929 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1930 // struct virtual_entry *ve = find_ve(ddf);
1932 /* we don't need to handle "force-*" or "assemble" as
1933 * there is no need to 'trick' the kernel. We the metadata is
1934 * first updated to activate the array, all the implied modifications
1938 if (strcmp(update
, "grow") == 0) {
1940 } else if (strcmp(update
, "resync") == 0) {
1941 // info->resync_checkpoint = 0;
1942 } else if (strcmp(update
, "homehost") == 0) {
1943 /* homehost is stored in controller->vendor_data,
1944 * or it is when we are the vendor
1946 // if (info->vendor_is_local)
1947 // strcpy(ddf->controller.vendor_data, homehost);
1949 } else if (strcmp(update
, "name") == 0) {
1950 /* name is stored in virtual_entry->name */
1951 // memset(ve->name, ' ', 16);
1952 // strncpy(ve->name, info->name, 16);
1954 } else if (strcmp(update
, "_reshape_progress") == 0) {
1955 /* We don't support reshape yet */
1956 } else if (strcmp(update
, "assemble") == 0 ) {
1957 /* Do nothing, just succeed */
1962 // update_all_csum(ddf);
1967 static void make_header_guid(char *guid
)
1970 /* Create a DDF Header of Virtual Disk GUID */
1972 /* 24 bytes of fiction required.
1973 * first 8 are a 'vendor-id' - "Linux-MD"
1974 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1975 * Remaining 8 random number plus timestamp
1977 memcpy(guid
, T10
, sizeof(T10
));
1978 stamp
= __cpu_to_be32(0xdeadbeef);
1979 memcpy(guid
+8, &stamp
, 4);
1980 stamp
= __cpu_to_be32(0);
1981 memcpy(guid
+12, &stamp
, 4);
1982 stamp
= __cpu_to_be32(time(0) - DECADE
);
1983 memcpy(guid
+16, &stamp
, 4);
1985 memcpy(guid
+20, &stamp
, 4);
1988 static unsigned int find_unused_vde(const struct ddf_super
*ddf
)
1991 for (i
= 0; i
< __be16_to_cpu(ddf
->virt
->max_vdes
); i
++) {
1992 if (all_ff(ddf
->virt
->entries
[i
].guid
))
1995 return DDF_NOTFOUND
;
1998 static unsigned int find_vde_by_name(const struct ddf_super
*ddf
,
2003 return DDF_NOTFOUND
;
2004 for (i
= 0; i
< __be16_to_cpu(ddf
->virt
->max_vdes
); i
++) {
2005 if (all_ff(ddf
->virt
->entries
[i
].guid
))
2007 if (!strncmp(name
, ddf
->virt
->entries
[i
].name
,
2008 sizeof(ddf
->virt
->entries
[i
].name
)))
2011 return DDF_NOTFOUND
;
2014 static unsigned int find_vde_by_guid(const struct ddf_super
*ddf
,
2018 if (guid
== NULL
|| all_ff(guid
))
2019 return DDF_NOTFOUND
;
2020 for (i
= 0; i
< __be16_to_cpu(ddf
->virt
->max_vdes
); i
++)
2021 if (!memcmp(ddf
->virt
->entries
[i
].guid
, guid
, DDF_GUID_LEN
))
2023 return DDF_NOTFOUND
;
2026 static int init_super_ddf_bvd(struct supertype
*st
,
2027 mdu_array_info_t
*info
,
2028 unsigned long long size
,
2029 char *name
, char *homehost
,
2030 int *uuid
, unsigned long long data_offset
);
2032 static int init_super_ddf(struct supertype
*st
,
2033 mdu_array_info_t
*info
,
2034 unsigned long long size
, char *name
, char *homehost
,
2035 int *uuid
, unsigned long long data_offset
)
2037 /* This is primarily called by Create when creating a new array.
2038 * We will then get add_to_super called for each component, and then
2039 * write_init_super called to write it out to each device.
2040 * For DDF, Create can create on fresh devices or on a pre-existing
2042 * To create on a pre-existing array a different method will be called.
2043 * This one is just for fresh drives.
2045 * We need to create the entire 'ddf' structure which includes:
2046 * DDF headers - these are easy.
2047 * Controller data - a Sector describing this controller .. not that
2048 * this is a controller exactly.
2049 * Physical Disk Record - one entry per device, so
2050 * leave plenty of space.
2051 * Virtual Disk Records - again, just leave plenty of space.
2052 * This just lists VDs, doesn't give details
2053 * Config records - describes the VDs that use this disk
2054 * DiskData - describes 'this' device.
2055 * BadBlockManagement - empty
2056 * Diag Space - empty
2057 * Vendor Logs - Could we put bitmaps here?
2060 struct ddf_super
*ddf
;
2063 int max_phys_disks
, max_virt_disks
;
2064 unsigned long long sector
;
2068 struct phys_disk
*pd
;
2069 struct virtual_disk
*vd
;
2071 if (data_offset
!= INVALID_SECTORS
) {
2072 pr_err("data-offset not supported by DDF\n");
2077 return init_super_ddf_bvd(st
, info
, size
, name
, homehost
, uuid
,
2080 if (posix_memalign((void**)&ddf
, 512, sizeof(*ddf
)) != 0) {
2081 pr_err("%s could not allocate superblock\n", __func__
);
2084 memset(ddf
, 0, sizeof(*ddf
));
2085 ddf
->dlist
= NULL
; /* no physical disks yet */
2086 ddf
->conflist
= NULL
; /* No virtual disks yet */
2090 /* zeroing superblock */
2094 /* At least 32MB *must* be reserved for the ddf. So let's just
2095 * start 32MB from the end, and put the primary header there.
2096 * Don't do secondary for now.
2097 * We don't know exactly where that will be yet as it could be
2098 * different on each device. To just set up the lengths.
2102 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
2103 make_header_guid(ddf
->anchor
.guid
);
2105 memcpy(ddf
->anchor
.revision
, DDF_REVISION_2
, 8);
2106 ddf
->anchor
.seq
= __cpu_to_be32(1);
2107 ddf
->anchor
.timestamp
= __cpu_to_be32(time(0) - DECADE
);
2108 ddf
->anchor
.openflag
= 0xFF;
2109 ddf
->anchor
.foreignflag
= 0;
2110 ddf
->anchor
.enforcegroups
= 0; /* Is this best?? */
2111 ddf
->anchor
.pad0
= 0xff;
2112 memset(ddf
->anchor
.pad1
, 0xff, 12);
2113 memset(ddf
->anchor
.header_ext
, 0xff, 32);
2114 ddf
->anchor
.primary_lba
= ~(__u64
)0;
2115 ddf
->anchor
.secondary_lba
= ~(__u64
)0;
2116 ddf
->anchor
.type
= DDF_HEADER_ANCHOR
;
2117 memset(ddf
->anchor
.pad2
, 0xff, 3);
2118 ddf
->anchor
.workspace_len
= __cpu_to_be32(32768); /* Must be reserved */
2119 ddf
->anchor
.workspace_lba
= ~(__u64
)0; /* Put this at bottom
2120 of 32M reserved.. */
2121 max_phys_disks
= 1023; /* Should be enough */
2122 ddf
->anchor
.max_pd_entries
= __cpu_to_be16(max_phys_disks
);
2123 max_virt_disks
= 255;
2124 ddf
->anchor
.max_vd_entries
= __cpu_to_be16(max_virt_disks
); /* ?? */
2125 ddf
->anchor
.max_partitions
= __cpu_to_be16(64); /* ?? */
2128 ddf
->conf_rec_len
= 1 + ROUND_UP(ddf
->mppe
* (4+8), 512)/512;
2129 ddf
->anchor
.config_record_len
= __cpu_to_be16(ddf
->conf_rec_len
);
2130 ddf
->anchor
.max_primary_element_entries
= __cpu_to_be16(ddf
->mppe
);
2131 memset(ddf
->anchor
.pad3
, 0xff, 54);
2132 /* controller sections is one sector long immediately
2133 * after the ddf header */
2135 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
2136 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
2139 /* phys is 8 sectors after that */
2140 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
2141 sizeof(struct phys_disk_entry
)*max_phys_disks
,
2143 switch(pdsize
/512) {
2144 case 2: case 8: case 32: case 128: case 512: break;
2147 ddf
->anchor
.phys_section_offset
= __cpu_to_be32(sector
);
2148 ddf
->anchor
.phys_section_length
=
2149 __cpu_to_be32(pdsize
/512); /* max_primary_element_entries/8 */
2150 sector
+= pdsize
/512;
2152 /* virt is another 32 sectors */
2153 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
2154 sizeof(struct virtual_entry
) * max_virt_disks
,
2156 switch(vdsize
/512) {
2157 case 2: case 8: case 32: case 128: case 512: break;
2160 ddf
->anchor
.virt_section_offset
= __cpu_to_be32(sector
);
2161 ddf
->anchor
.virt_section_length
=
2162 __cpu_to_be32(vdsize
/512); /* max_vd_entries/8 */
2163 sector
+= vdsize
/512;
2165 clen
= ddf
->conf_rec_len
* (ddf
->max_part
+1);
2166 ddf
->anchor
.config_section_offset
= __cpu_to_be32(sector
);
2167 ddf
->anchor
.config_section_length
= __cpu_to_be32(clen
);
2170 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
2171 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
2174 ddf
->anchor
.bbm_section_length
= __cpu_to_be32(0);
2175 ddf
->anchor
.bbm_section_offset
= __cpu_to_be32(0xFFFFFFFF);
2176 ddf
->anchor
.diag_space_length
= __cpu_to_be32(0);
2177 ddf
->anchor
.diag_space_offset
= __cpu_to_be32(0xFFFFFFFF);
2178 ddf
->anchor
.vendor_length
= __cpu_to_be32(0);
2179 ddf
->anchor
.vendor_offset
= __cpu_to_be32(0xFFFFFFFF);
2181 memset(ddf
->anchor
.pad4
, 0xff, 256);
2183 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
2184 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
2186 ddf
->primary
.openflag
= 1; /* I guess.. */
2187 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
2189 ddf
->secondary
.openflag
= 1; /* I guess.. */
2190 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
2192 ddf
->active
= &ddf
->primary
;
2194 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
2196 /* 24 more bytes of fiction required.
2197 * first 8 are a 'vendor-id' - "Linux-MD"
2198 * Remaining 16 are serial number.... maybe a hostname would do?
2200 memcpy(ddf
->controller
.guid
, T10
, sizeof(T10
));
2201 gethostname(hostname
, sizeof(hostname
));
2202 hostname
[sizeof(hostname
) - 1] = 0;
2203 hostlen
= strlen(hostname
);
2204 memcpy(ddf
->controller
.guid
+ 24 - hostlen
, hostname
, hostlen
);
2205 for (i
= strlen(T10
) ; i
+hostlen
< 24; i
++)
2206 ddf
->controller
.guid
[i
] = ' ';
2208 ddf
->controller
.type
.vendor_id
= __cpu_to_be16(0xDEAD);
2209 ddf
->controller
.type
.device_id
= __cpu_to_be16(0xBEEF);
2210 ddf
->controller
.type
.sub_vendor_id
= 0;
2211 ddf
->controller
.type
.sub_device_id
= 0;
2212 memcpy(ddf
->controller
.product_id
, "What Is My PID??", 16);
2213 memset(ddf
->controller
.pad
, 0xff, 8);
2214 memset(ddf
->controller
.vendor_data
, 0xff, 448);
2215 if (homehost
&& strlen(homehost
) < 440)
2216 strcpy((char*)ddf
->controller
.vendor_data
, homehost
);
2218 if (posix_memalign((void**)&pd
, 512, pdsize
) != 0) {
2219 pr_err("%s could not allocate pd\n", __func__
);
2223 ddf
->pdsize
= pdsize
;
2225 memset(pd
, 0xff, pdsize
);
2226 memset(pd
, 0, sizeof(*pd
));
2227 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2228 pd
->used_pdes
= __cpu_to_be16(0);
2229 pd
->max_pdes
= __cpu_to_be16(max_phys_disks
);
2230 memset(pd
->pad
, 0xff, 52);
2232 if (posix_memalign((void**)&vd
, 512, vdsize
) != 0) {
2233 pr_err("%s could not allocate vd\n", __func__
);
2237 ddf
->vdsize
= vdsize
;
2238 memset(vd
, 0, vdsize
);
2239 vd
->magic
= DDF_VIRT_RECORDS_MAGIC
;
2240 vd
->populated_vdes
= __cpu_to_be16(0);
2241 vd
->max_vdes
= __cpu_to_be16(max_virt_disks
);
2242 memset(vd
->pad
, 0xff, 52);
2244 for (i
=0; i
<max_virt_disks
; i
++)
2245 memset(&vd
->entries
[i
], 0xff, sizeof(struct virtual_entry
));
2248 ddf_set_updates_pending(ddf
);
2252 static int chunk_to_shift(int chunksize
)
2254 return ffs(chunksize
/512)-1;
2259 unsigned long long start
, size
;
2261 static int cmp_extent(const void *av
, const void *bv
)
2263 const struct extent
*a
= av
;
2264 const struct extent
*b
= bv
;
2265 if (a
->start
< b
->start
)
2267 if (a
->start
> b
->start
)
2272 static struct extent
*get_extents(struct ddf_super
*ddf
, struct dl
*dl
)
2274 /* find a list of used extents on the give physical device
2275 * (dnum) of the given ddf.
2276 * Return a malloced array of 'struct extent'
2278 * FIXME ignore DDF_Legacy devices?
2285 rv
= xmalloc(sizeof(struct extent
) * (ddf
->max_part
+ 2));
2287 for (i
= 0; i
< ddf
->max_part
; i
++) {
2288 struct vcl
*v
= dl
->vlist
[i
];
2291 for (j
= 0; j
< v
->conf
.prim_elmnt_count
; j
++)
2292 if (v
->conf
.phys_refnum
[j
] == dl
->disk
.refnum
) {
2293 /* This device plays role 'j' in 'v'. */
2294 rv
[n
].start
= __be64_to_cpu(v
->lba_offset
[j
]);
2295 rv
[n
].size
= __be64_to_cpu(v
->conf
.blocks
);
2300 qsort(rv
, n
, sizeof(*rv
), cmp_extent
);
2302 rv
[n
].start
= __be64_to_cpu(ddf
->phys
->entries
[dl
->pdnum
].config_size
);
2308 static int init_super_ddf_bvd(struct supertype
*st
,
2309 mdu_array_info_t
*info
,
2310 unsigned long long size
,
2311 char *name
, char *homehost
,
2312 int *uuid
, unsigned long long data_offset
)
2314 /* We are creating a BVD inside a pre-existing container.
2315 * so st->sb is already set.
2316 * We need to create a new vd_config and a new virtual_entry
2318 struct ddf_super
*ddf
= st
->sb
;
2320 struct virtual_entry
*ve
;
2322 struct vd_config
*vc
;
2324 if (find_vde_by_name(ddf
, name
) != DDF_NOTFOUND
) {
2325 pr_err("This ddf already has an array called %s\n", name
);
2328 venum
= find_unused_vde(ddf
);
2329 if (venum
== DDF_NOTFOUND
) {
2330 pr_err("Cannot find spare slot for virtual disk\n");
2333 ve
= &ddf
->virt
->entries
[venum
];
2335 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
2336 * timestamp, random number
2338 make_header_guid(ve
->guid
);
2339 ve
->unit
= __cpu_to_be16(info
->md_minor
);
2341 ve
->guid_crc
= crc32(0, (unsigned char*)ddf
->anchor
.guid
, DDF_GUID_LEN
);
2343 ve
->state
= DDF_state_degraded
; /* Will be modified as devices are added */
2344 if (info
->state
& 1) /* clean */
2345 ve
->init_state
= DDF_init_full
;
2347 ve
->init_state
= DDF_init_not
;
2349 memset(ve
->pad1
, 0xff, 14);
2350 memset(ve
->name
, ' ', 16);
2352 strncpy(ve
->name
, name
, 16);
2353 ddf
->virt
->populated_vdes
=
2354 __cpu_to_be16(__be16_to_cpu(ddf
->virt
->populated_vdes
)+1);
2356 /* Now create a new vd_config */
2357 if (posix_memalign((void**)&vcl
, 512,
2358 (offsetof(struct vcl
, conf
) + ddf
->conf_rec_len
* 512)) != 0) {
2359 pr_err("%s could not allocate vd_config\n", __func__
);
2362 vcl
->lba_offset
= (__u64
*) &vcl
->conf
.phys_refnum
[ddf
->mppe
];
2364 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
2365 vcl
->other_bvds
= NULL
;
2369 vc
->magic
= DDF_VD_CONF_MAGIC
;
2370 memcpy(vc
->guid
, ve
->guid
, DDF_GUID_LEN
);
2371 vc
->timestamp
= __cpu_to_be32(time(0)-DECADE
);
2372 vc
->seqnum
= __cpu_to_be32(1);
2373 memset(vc
->pad0
, 0xff, 24);
2374 vc
->chunk_shift
= chunk_to_shift(info
->chunk_size
);
2375 if (layout_md2ddf(info
, vc
) == -1 ||
2376 __be16_to_cpu(vc
->prim_elmnt_count
) > ddf
->mppe
) {
2377 pr_err("%s: unsupported RAID level/layout %d/%d with %d disks\n",
2378 __func__
, info
->level
, info
->layout
, info
->raid_disks
);
2382 vc
->sec_elmnt_seq
= 0;
2383 vc
->blocks
= __cpu_to_be64(info
->size
* 2);
2384 vc
->array_blocks
= __cpu_to_be64(
2385 calc_array_size(info
->level
, info
->raid_disks
, info
->layout
,
2386 info
->chunk_size
, info
->size
*2));
2387 memset(vc
->pad1
, 0xff, 8);
2388 vc
->spare_refs
[0] = 0xffffffff;
2389 vc
->spare_refs
[1] = 0xffffffff;
2390 vc
->spare_refs
[2] = 0xffffffff;
2391 vc
->spare_refs
[3] = 0xffffffff;
2392 vc
->spare_refs
[4] = 0xffffffff;
2393 vc
->spare_refs
[5] = 0xffffffff;
2394 vc
->spare_refs
[6] = 0xffffffff;
2395 vc
->spare_refs
[7] = 0xffffffff;
2396 memset(vc
->cache_pol
, 0, 8);
2398 memset(vc
->pad2
, 0xff, 3);
2399 memset(vc
->pad3
, 0xff, 52);
2400 memset(vc
->pad4
, 0xff, 192);
2401 memset(vc
->v0
, 0xff, 32);
2402 memset(vc
->v1
, 0xff, 32);
2403 memset(vc
->v2
, 0xff, 16);
2404 memset(vc
->v3
, 0xff, 16);
2405 memset(vc
->vendor
, 0xff, 32);
2407 memset(vc
->phys_refnum
, 0xff, 4*ddf
->mppe
);
2408 memset(vc
->phys_refnum
+ddf
->mppe
, 0x00, 8*ddf
->mppe
);
2410 vcl
->next
= ddf
->conflist
;
2411 ddf
->conflist
= vcl
;
2412 ddf
->currentconf
= vcl
;
2413 ddf_set_updates_pending(ddf
);
2418 static void add_to_super_ddf_bvd(struct supertype
*st
,
2419 mdu_disk_info_t
*dk
, int fd
, char *devname
)
2421 /* fd and devname identify a device with-in the ddf container (st).
2422 * dk identifies a location in the new BVD.
2423 * We need to find suitable free space in that device and update
2424 * the phys_refnum and lba_offset for the newly created vd_config.
2425 * We might also want to update the type in the phys_disk
2428 * Alternately: fd == -1 and we have already chosen which device to
2429 * use and recorded in dlist->raid_disk;
2432 struct ddf_super
*ddf
= st
->sb
;
2433 struct vd_config
*vc
;
2435 unsigned int working
;
2437 unsigned long long blocks
, pos
, esize
;
2441 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2442 if (dl
->raiddisk
== dk
->raid_disk
)
2445 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2446 if (dl
->major
== dk
->major
&&
2447 dl
->minor
== dk
->minor
)
2450 if (!dl
|| ! (dk
->state
& (1<<MD_DISK_SYNC
)))
2453 vc
= &ddf
->currentconf
->conf
;
2454 lba_offset
= ddf
->currentconf
->lba_offset
;
2456 ex
= get_extents(ddf
, dl
);
2461 blocks
= __be64_to_cpu(vc
->blocks
);
2462 if (ddf
->currentconf
->block_sizes
)
2463 blocks
= ddf
->currentconf
->block_sizes
[dk
->raid_disk
];
2466 esize
= ex
[i
].start
- pos
;
2467 if (esize
>= blocks
)
2469 pos
= ex
[i
].start
+ ex
[i
].size
;
2471 } while (ex
[i
-1].size
);
2477 ddf
->currentdev
= dk
->raid_disk
;
2478 vc
->phys_refnum
[dk
->raid_disk
] = dl
->disk
.refnum
;
2479 lba_offset
[dk
->raid_disk
] = __cpu_to_be64(pos
);
2481 for (i
= 0; i
< ddf
->max_part
; i
++)
2482 if (dl
->vlist
[i
] == NULL
)
2484 if (i
== ddf
->max_part
)
2486 dl
->vlist
[i
] = ddf
->currentconf
;
2491 dl
->devname
= devname
;
2493 /* Check how many working raid_disks, and if we can mark
2494 * array as optimal yet
2498 for (i
= 0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++)
2499 if (vc
->phys_refnum
[i
] != 0xffffffff)
2502 /* Find which virtual_entry */
2503 i
= ddf
->currentconf
->vcnum
;
2504 if (working
== __be16_to_cpu(vc
->prim_elmnt_count
))
2505 ddf
->virt
->entries
[i
].state
=
2506 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2507 | DDF_state_optimal
;
2509 if (vc
->prl
== DDF_RAID6
&&
2510 working
+1 == __be16_to_cpu(vc
->prim_elmnt_count
))
2511 ddf
->virt
->entries
[i
].state
=
2512 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2513 | DDF_state_part_optimal
;
2515 ddf
->phys
->entries
[dl
->pdnum
].type
&= ~__cpu_to_be16(DDF_Global_Spare
);
2516 ddf
->phys
->entries
[dl
->pdnum
].type
|= __cpu_to_be16(DDF_Active_in_VD
);
2517 ddf_set_updates_pending(ddf
);
2520 /* add a device to a container, either while creating it or while
2521 * expanding a pre-existing container
2523 static int add_to_super_ddf(struct supertype
*st
,
2524 mdu_disk_info_t
*dk
, int fd
, char *devname
,
2525 unsigned long long data_offset
)
2527 struct ddf_super
*ddf
= st
->sb
;
2531 unsigned long long size
;
2532 struct phys_disk_entry
*pde
;
2537 if (ddf
->currentconf
) {
2538 add_to_super_ddf_bvd(st
, dk
, fd
, devname
);
2542 /* This is device numbered dk->number. We need to create
2543 * a phys_disk entry and a more detailed disk_data entry.
2546 if (posix_memalign((void**)&dd
, 512,
2547 sizeof(*dd
) + sizeof(dd
->vlist
[0]) * ddf
->max_part
) != 0) {
2548 pr_err("%s could allocate buffer for new disk, aborting\n",
2552 dd
->major
= major(stb
.st_rdev
);
2553 dd
->minor
= minor(stb
.st_rdev
);
2554 dd
->devname
= devname
;
2558 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
2560 tm
= localtime(&now
);
2561 sprintf(dd
->disk
.guid
, "%8s%04d%02d%02d",
2562 T10
, tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
2563 tptr
= (__u32
*)(dd
->disk
.guid
+ 16);
2564 *tptr
++ = random32();
2568 /* Cannot be bothered finding a CRC of some irrelevant details*/
2569 dd
->disk
.refnum
= random32();
2570 for (i
= __be16_to_cpu(ddf
->active
->max_pd_entries
);
2572 if (ddf
->phys
->entries
[i
-1].refnum
== dd
->disk
.refnum
)
2576 dd
->disk
.forced_ref
= 1;
2577 dd
->disk
.forced_guid
= 1;
2578 memset(dd
->disk
.vendor
, ' ', 32);
2579 memcpy(dd
->disk
.vendor
, "Linux", 5);
2580 memset(dd
->disk
.pad
, 0xff, 442);
2581 for (i
= 0; i
< ddf
->max_part
; i
++)
2582 dd
->vlist
[i
] = NULL
;
2584 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
2585 pde
= &ddf
->phys
->entries
[n
];
2588 if (st
->update_tail
) {
2589 int len
= (sizeof(struct phys_disk
) +
2590 sizeof(struct phys_disk_entry
));
2591 struct phys_disk
*pd
;
2594 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2595 pd
->used_pdes
= __cpu_to_be16(n
);
2596 pde
= &pd
->entries
[0];
2600 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
2603 memcpy(pde
->guid
, dd
->disk
.guid
, DDF_GUID_LEN
);
2604 pde
->refnum
= dd
->disk
.refnum
;
2605 pde
->type
= __cpu_to_be16(DDF_Forced_PD_GUID
| DDF_Global_Spare
);
2606 pde
->state
= __cpu_to_be16(DDF_Online
);
2607 get_dev_size(fd
, NULL
, &size
);
2608 /* We are required to reserve 32Meg, and record the size in sectors */
2609 pde
->config_size
= __cpu_to_be64( (size
- 32*1024*1024) / 512);
2610 sprintf(pde
->path
, "%17.17s","Information: nil") ;
2611 memset(pde
->pad
, 0xff, 6);
2613 dd
->size
= size
>> 9;
2614 if (st
->update_tail
) {
2615 dd
->next
= ddf
->add_list
;
2618 dd
->next
= ddf
->dlist
;
2620 ddf_set_updates_pending(ddf
);
2626 static int remove_from_super_ddf(struct supertype
*st
, mdu_disk_info_t
*dk
)
2628 struct ddf_super
*ddf
= st
->sb
;
2631 /* mdmon has noticed that this disk (dk->major/dk->minor) has
2632 * disappeared from the container.
2633 * We need to arrange that it disappears from the metadata and
2634 * internal data structures too.
2635 * Most of the work is done by ddf_process_update which edits
2636 * the metadata and closes the file handle and attaches the memory
2637 * where free_updates will free it.
2639 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2640 if (dl
->major
== dk
->major
&&
2641 dl
->minor
== dk
->minor
)
2646 if (st
->update_tail
) {
2647 int len
= (sizeof(struct phys_disk
) +
2648 sizeof(struct phys_disk_entry
));
2649 struct phys_disk
*pd
;
2652 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2653 pd
->used_pdes
= __cpu_to_be16(dl
->pdnum
);
2654 pd
->entries
[0].state
= __cpu_to_be16(DDF_Missing
);
2655 append_metadata_update(st
, pd
, len
);
2661 * This is the write_init_super method for a ddf container. It is
2662 * called when creating a container or adding another device to a
2665 #define NULL_CONF_SZ 4096
2667 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
2668 __u32 refnum
, unsigned int nmax
,
2669 const struct vd_config
**bvd
,
2672 static int __write_ddf_structure(struct dl
*d
, struct ddf_super
*ddf
, __u8 type
,
2675 unsigned long long sector
;
2676 struct ddf_header
*header
;
2677 int fd
, i
, n_config
, conf_size
;
2683 case DDF_HEADER_PRIMARY
:
2684 header
= &ddf
->primary
;
2685 sector
= __be64_to_cpu(header
->primary_lba
);
2687 case DDF_HEADER_SECONDARY
:
2688 header
= &ddf
->secondary
;
2689 sector
= __be64_to_cpu(header
->secondary_lba
);
2695 header
->type
= type
;
2696 header
->openflag
= 1;
2697 header
->crc
= calc_crc(header
, 512);
2699 lseek64(fd
, sector
<<9, 0);
2700 if (write(fd
, header
, 512) < 0)
2703 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
2704 if (write(fd
, &ddf
->controller
, 512) < 0)
2707 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
2708 if (write(fd
, ddf
->phys
, ddf
->pdsize
) < 0)
2710 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
2711 if (write(fd
, ddf
->virt
, ddf
->vdsize
) < 0)
2714 /* Now write lots of config records. */
2715 n_config
= ddf
->max_part
;
2716 conf_size
= ddf
->conf_rec_len
* 512;
2717 for (i
= 0 ; i
<= n_config
; i
++) {
2719 struct vd_config
*vdc
= NULL
;
2720 if (i
== n_config
) {
2721 c
= (struct vcl
*)d
->spare
;
2728 get_pd_index_from_refnum(
2731 (const struct vd_config
**)&vdc
,
2735 vdc
->seqnum
= header
->seq
;
2736 vdc
->crc
= calc_crc(vdc
, conf_size
);
2737 if (write(fd
, vdc
, conf_size
) < 0)
2740 unsigned int togo
= conf_size
;
2741 while (togo
> NULL_CONF_SZ
) {
2742 if (write(fd
, null_aligned
, NULL_CONF_SZ
) < 0)
2744 togo
-= NULL_CONF_SZ
;
2746 if (write(fd
, null_aligned
, togo
) < 0)
2753 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
2754 if (write(fd
, &d
->disk
, 512) < 0)
2759 header
->openflag
= 0;
2760 header
->crc
= calc_crc(header
, 512);
2762 lseek64(fd
, sector
<<9, 0);
2763 if (write(fd
, header
, 512) < 0)
2769 static int __write_init_super_ddf(struct supertype
*st
)
2771 struct ddf_super
*ddf
= st
->sb
;
2775 unsigned long long size
;
2779 pr_state(ddf
, __func__
);
2780 if (posix_memalign((void**)&null_aligned
, 4096, NULL_CONF_SZ
) != 0) {
2783 memset(null_aligned
, 0xff, NULL_CONF_SZ
);
2785 seq
= ddf
->active
->seq
+ 1;
2787 /* try to write updated metadata,
2788 * if we catch a failure move on to the next disk
2790 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
2797 /* We need to fill in the primary, (secondary) and workspace
2798 * lba's in the headers, set their checksums,
2799 * Also checksum phys, virt....
2801 * Then write everything out, finally the anchor is written.
2803 get_dev_size(fd
, NULL
, &size
);
2805 if (d
->workspace_lba
!= 0)
2806 ddf
->anchor
.workspace_lba
= d
->workspace_lba
;
2808 ddf
->anchor
.workspace_lba
=
2809 __cpu_to_be64(size
- 32*1024*2);
2810 if (d
->primary_lba
!= 0)
2811 ddf
->anchor
.primary_lba
= d
->primary_lba
;
2813 ddf
->anchor
.primary_lba
=
2814 __cpu_to_be64(size
- 16*1024*2);
2815 if (d
->secondary_lba
!= 0)
2816 ddf
->anchor
.secondary_lba
= d
->secondary_lba
;
2818 ddf
->anchor
.secondary_lba
=
2819 __cpu_to_be64(size
- 32*1024*2);
2820 ddf
->anchor
.seq
= seq
;
2821 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
2822 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
2824 ddf
->anchor
.openflag
= 0xFF; /* 'open' means nothing */
2825 ddf
->anchor
.seq
= 0xFFFFFFFF; /* no sequencing in anchor */
2826 ddf
->anchor
.crc
= calc_crc(&ddf
->anchor
, 512);
2828 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_PRIMARY
,
2832 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_SECONDARY
,
2836 lseek64(fd
, (size
-1)*512, SEEK_SET
);
2837 if (write(fd
, &ddf
->anchor
, 512) < 0)
2843 return attempts
!= successes
;
2846 static int write_init_super_ddf(struct supertype
*st
)
2848 struct ddf_super
*ddf
= st
->sb
;
2849 struct vcl
*currentconf
= ddf
->currentconf
;
2851 /* we are done with currentconf reset it to point st at the container */
2852 ddf
->currentconf
= NULL
;
2854 if (st
->update_tail
) {
2855 /* queue the virtual_disk and vd_config as metadata updates */
2856 struct virtual_disk
*vd
;
2857 struct vd_config
*vc
;
2861 int len
= (sizeof(struct phys_disk
) +
2862 sizeof(struct phys_disk_entry
));
2864 /* adding a disk to the container. */
2868 append_metadata_update(st
, ddf
->add_list
->mdupdate
, len
);
2869 ddf
->add_list
->mdupdate
= NULL
;
2873 /* Newly created VD */
2875 /* First the virtual disk. We have a slightly fake header */
2876 len
= sizeof(struct virtual_disk
) + sizeof(struct virtual_entry
);
2879 vd
->entries
[0] = ddf
->virt
->entries
[currentconf
->vcnum
];
2880 vd
->populated_vdes
= __cpu_to_be16(currentconf
->vcnum
);
2881 append_metadata_update(st
, vd
, len
);
2883 /* Then the vd_config */
2884 len
= ddf
->conf_rec_len
* 512;
2886 memcpy(vc
, ¤tconf
->conf
, len
);
2887 append_metadata_update(st
, vc
, len
);
2889 /* FIXME I need to close the fds! */
2893 for (d
= ddf
->dlist
; d
; d
=d
->next
)
2894 while (Kill(d
->devname
, NULL
, 0, -1, 1) == 0);
2895 return __write_init_super_ddf(st
);
2901 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
,
2902 unsigned long long data_offset
)
2904 /* We must reserve the last 32Meg */
2905 if (devsize
<= 32*1024*2)
2907 return devsize
- 32*1024*2;
2912 static int reserve_space(struct supertype
*st
, int raiddisks
,
2913 unsigned long long size
, int chunk
,
2914 unsigned long long *freesize
)
2916 /* Find 'raiddisks' spare extents at least 'size' big (but
2917 * only caring about multiples of 'chunk') and remember
2919 * If the cannot be found, fail.
2922 struct ddf_super
*ddf
= st
->sb
;
2925 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2929 /* Now find largest extent on each device */
2930 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2931 struct extent
*e
= get_extents(ddf
, dl
);
2932 unsigned long long pos
= 0;
2935 unsigned long long minsize
= size
;
2943 unsigned long long esize
;
2944 esize
= e
[i
].start
- pos
;
2945 if (esize
>= minsize
) {
2949 pos
= e
[i
].start
+ e
[i
].size
;
2951 } while (e
[i
-1].size
);
2954 dl
->esize
= minsize
;
2958 if (cnt
< raiddisks
) {
2959 pr_err("not enough devices with space to create array.\n");
2960 return 0; /* No enough free spaces large enough */
2963 /* choose the largest size of which there are at least 'raiddisk' */
2964 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2966 if (dl
->esize
<= size
)
2968 /* This is bigger than 'size', see if there are enough */
2970 for (dl2
= ddf
->dlist
; dl2
; dl2
=dl2
->next
)
2971 if (dl2
->esize
>= dl
->esize
)
2973 if (cnt
>= raiddisks
)
2977 size
= size
/ chunk
;
2982 pr_err("not enough spare devices to create array.\n");
2986 /* We have a 'size' of which there are enough spaces.
2987 * We simply do a first-fit */
2989 for (dl
= ddf
->dlist
; dl
&& cnt
< raiddisks
; dl
=dl
->next
) {
2990 if (dl
->esize
< size
)
3000 validate_geometry_ddf_container(struct supertype
*st
,
3001 int level
, int layout
, int raiddisks
,
3002 int chunk
, unsigned long long size
,
3003 unsigned long long data_offset
,
3004 char *dev
, unsigned long long *freesize
,
3007 static int validate_geometry_ddf_bvd(struct supertype
*st
,
3008 int level
, int layout
, int raiddisks
,
3009 int *chunk
, unsigned long long size
,
3010 unsigned long long data_offset
,
3011 char *dev
, unsigned long long *freesize
,
3014 static int validate_geometry_ddf(struct supertype
*st
,
3015 int level
, int layout
, int raiddisks
,
3016 int *chunk
, unsigned long long size
,
3017 unsigned long long data_offset
,
3018 char *dev
, unsigned long long *freesize
,
3025 /* ddf potentially supports lots of things, but it depends on
3026 * what devices are offered (and maybe kernel version?)
3027 * If given unused devices, we will make a container.
3028 * If given devices in a container, we will make a BVD.
3029 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
3032 if (chunk
&& *chunk
== UnSet
)
3033 *chunk
= DEFAULT_CHUNK
;
3035 if (level
== -1000000) level
= LEVEL_CONTAINER
;
3036 if (level
== LEVEL_CONTAINER
) {
3037 /* Must be a fresh device to add to a container */
3038 return validate_geometry_ddf_container(st
, level
, layout
,
3039 raiddisks
, chunk
?*chunk
:0,
3040 size
, data_offset
, dev
,
3046 mdu_array_info_t array
= {
3047 .level
= level
, .layout
= layout
,
3048 .raid_disks
= raiddisks
3050 struct vd_config conf
;
3051 if (layout_md2ddf(&array
, &conf
) == -1) {
3053 pr_err("DDF does not support level %d arrays\n",
3057 /* Should check layout? etc */
3059 if (st
->sb
&& freesize
) {
3060 /* --create was given a container to create in.
3061 * So we need to check that there are enough
3062 * free spaces and return the amount of space.
3063 * We may as well remember which drives were
3064 * chosen so that add_to_super/getinfo_super
3067 return reserve_space(st
, raiddisks
, size
, chunk
?*chunk
:0, freesize
);
3073 /* A container has already been opened, so we are
3074 * creating in there. Maybe a BVD, maybe an SVD.
3075 * Should make a distinction one day.
3077 return validate_geometry_ddf_bvd(st
, level
, layout
, raiddisks
,
3078 chunk
, size
, data_offset
, dev
,
3082 /* This is the first device for the array.
3083 * If it is a container, we read it in and do automagic allocations,
3084 * no other devices should be given.
3085 * Otherwise it must be a member device of a container, and we
3086 * do manual allocation.
3087 * Later we should check for a BVD and make an SVD.
3089 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3091 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
3093 if (sra
&& sra
->array
.major_version
== -1 &&
3094 strcmp(sra
->text_version
, "ddf") == 0) {
3097 /* find space for 'n' devices. */
3098 /* remember the devices */
3099 /* Somehow return the fact that we have enough */
3103 pr_err("ddf: Cannot create this array "
3104 "on device %s - a container is required.\n",
3108 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
3110 pr_err("ddf: Cannot open %s: %s\n",
3111 dev
, strerror(errno
));
3114 /* Well, it is in use by someone, maybe a 'ddf' container. */
3115 cfd
= open_container(fd
);
3119 pr_err("ddf: Cannot use %s: %s\n",
3120 dev
, strerror(EBUSY
));
3123 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
3125 if (sra
&& sra
->array
.major_version
== -1 &&
3126 strcmp(sra
->text_version
, "ddf") == 0) {
3127 /* This is a member of a ddf container. Load the container
3128 * and try to create a bvd
3130 struct ddf_super
*ddf
;
3131 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
) == 0) {
3133 strcpy(st
->container_devnm
, fd2devnm(cfd
));
3135 return validate_geometry_ddf_bvd(st
, level
, layout
,
3136 raiddisks
, chunk
, size
,
3142 } else /* device may belong to a different container */
3149 validate_geometry_ddf_container(struct supertype
*st
,
3150 int level
, int layout
, int raiddisks
,
3151 int chunk
, unsigned long long size
,
3152 unsigned long long data_offset
,
3153 char *dev
, unsigned long long *freesize
,
3157 unsigned long long ldsize
;
3159 if (level
!= LEVEL_CONTAINER
)
3164 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
3167 pr_err("ddf: Cannot open %s: %s\n",
3168 dev
, strerror(errno
));
3171 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3177 *freesize
= avail_size_ddf(st
, ldsize
>> 9, INVALID_SECTORS
);
3184 static int validate_geometry_ddf_bvd(struct supertype
*st
,
3185 int level
, int layout
, int raiddisks
,
3186 int *chunk
, unsigned long long size
,
3187 unsigned long long data_offset
,
3188 char *dev
, unsigned long long *freesize
,
3192 struct ddf_super
*ddf
= st
->sb
;
3194 unsigned long long pos
= 0;
3195 unsigned long long maxsize
;
3198 /* ddf/bvd supports lots of things, but not containers */
3199 if (level
== LEVEL_CONTAINER
) {
3201 pr_err("DDF cannot create a container within an container\n");
3204 /* We must have the container info already read in. */
3209 /* General test: make sure there is space for
3210 * 'raiddisks' device extents of size 'size'.
3212 unsigned long long minsize
= size
;
3216 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
3222 e
= get_extents(ddf
, dl
);
3225 unsigned long long esize
;
3226 esize
= e
[i
].start
- pos
;
3227 if (esize
>= minsize
)
3229 pos
= e
[i
].start
+ e
[i
].size
;
3231 } while (e
[i
-1].size
);
3236 if (dcnt
< raiddisks
) {
3238 pr_err("ddf: Not enough devices with "
3239 "space for this array (%d < %d)\n",
3245 /* This device must be a member of the set */
3246 if (stat(dev
, &stb
) < 0)
3248 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
3250 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
3251 if (dl
->major
== (int)major(stb
.st_rdev
) &&
3252 dl
->minor
== (int)minor(stb
.st_rdev
))
3257 pr_err("ddf: %s is not in the "
3262 e
= get_extents(ddf
, dl
);
3266 unsigned long long esize
;
3267 esize
= e
[i
].start
- pos
;
3268 if (esize
>= maxsize
)
3270 pos
= e
[i
].start
+ e
[i
].size
;
3272 } while (e
[i
-1].size
);
3273 *freesize
= maxsize
;
3279 static int load_super_ddf_all(struct supertype
*st
, int fd
,
3280 void **sbp
, char *devname
)
3283 struct ddf_super
*super
;
3284 struct mdinfo
*sd
, *best
= NULL
;
3290 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3293 if (sra
->array
.major_version
!= -1 ||
3294 sra
->array
.minor_version
!= -2 ||
3295 strcmp(sra
->text_version
, "ddf") != 0)
3298 if (posix_memalign((void**)&super
, 512, sizeof(*super
)) != 0)
3300 memset(super
, 0, sizeof(*super
));
3302 /* first, try each device, and choose the best ddf */
3303 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3305 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3306 dfd
= dev_open(nm
, O_RDONLY
);
3309 rv
= load_ddf_headers(dfd
, super
, NULL
);
3312 seq
= __be32_to_cpu(super
->active
->seq
);
3313 if (super
->active
->openflag
)
3315 if (!best
|| seq
> bestseq
) {
3323 /* OK, load this ddf */
3324 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
3325 dfd
= dev_open(nm
, O_RDONLY
);
3328 load_ddf_headers(dfd
, super
, NULL
);
3329 load_ddf_global(dfd
, super
, NULL
);
3331 /* Now we need the device-local bits */
3332 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3335 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3336 dfd
= dev_open(nm
, O_RDWR
);
3339 rv
= load_ddf_headers(dfd
, super
, NULL
);
3341 rv
= load_ddf_local(dfd
, super
, NULL
, 1);
3347 if (st
->ss
== NULL
) {
3348 st
->ss
= &super_ddf
;
3349 st
->minor_version
= 0;
3352 strcpy(st
->container_devnm
, fd2devnm(fd
));
3356 static int load_container_ddf(struct supertype
*st
, int fd
,
3359 return load_super_ddf_all(st
, fd
, &st
->sb
, devname
);
3362 #endif /* MDASSEMBLE */
3364 static int check_secondary(const struct vcl
*vc
)
3366 const struct vd_config
*conf
= &vc
->conf
;
3369 /* The only DDF secondary RAID level md can support is
3370 * RAID 10, if the stripe sizes and Basic volume sizes
3372 * Other configurations could in theory be supported by exposing
3373 * the BVDs to user space and using device mapper for the secondary
3374 * mapping. So far we don't support that.
3377 __u64 sec_elements
[4] = {0, 0, 0, 0};
3378 #define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3379 #define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3381 if (vc
->other_bvds
== NULL
) {
3382 pr_err("No BVDs for secondary RAID found\n");
3385 if (conf
->prl
!= DDF_RAID1
) {
3386 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3389 if (conf
->srl
!= DDF_2STRIPED
&& conf
->srl
!= DDF_2SPANNED
) {
3390 pr_err("Secondary RAID level %d is unsupported\n",
3394 __set_sec_seen(conf
->sec_elmnt_seq
);
3395 for (i
= 0; i
< conf
->sec_elmnt_count
-1; i
++) {
3396 const struct vd_config
*bvd
= vc
->other_bvds
[i
];
3399 if (bvd
->srl
!= conf
->srl
) {
3400 pr_err("Inconsistent secondary RAID level across BVDs\n");
3403 if (bvd
->prl
!= conf
->prl
) {
3404 pr_err("Different RAID levels for BVDs are unsupported\n");
3407 if (bvd
->prim_elmnt_count
!= conf
->prim_elmnt_count
) {
3408 pr_err("All BVDs must have the same number of primary elements\n");
3411 if (bvd
->chunk_shift
!= conf
->chunk_shift
) {
3412 pr_err("Different strip sizes for BVDs are unsupported\n");
3415 if (bvd
->array_blocks
!= conf
->array_blocks
) {
3416 pr_err("Different BVD sizes are unsupported\n");
3419 __set_sec_seen(bvd
->sec_elmnt_seq
);
3421 for (i
= 0; i
< conf
->sec_elmnt_count
; i
++) {
3422 if (!__was_sec_seen(i
)) {
3423 pr_err("BVD %d is missing\n", i
);
3430 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
3431 __u32 refnum
, unsigned int nmax
,
3432 const struct vd_config
**bvd
,
3435 unsigned int i
, j
, n
, sec
, cnt
;
3437 cnt
= __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3438 sec
= (vc
->conf
.sec_elmnt_count
== 1 ? 0 : vc
->conf
.sec_elmnt_seq
);
3440 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3441 /* j counts valid entries for this BVD */
3442 if (vc
->conf
.phys_refnum
[i
] != 0xffffffff)
3444 if (vc
->conf
.phys_refnum
[i
] == refnum
) {
3447 return sec
* cnt
+ j
- 1;
3450 if (vc
->other_bvds
== NULL
)
3453 for (n
= 1; n
< vc
->conf
.sec_elmnt_count
; n
++) {
3454 struct vd_config
*vd
= vc
->other_bvds
[n
-1];
3457 sec
= vd
->sec_elmnt_seq
;
3458 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3459 if (vd
->phys_refnum
[i
] != 0xffffffff)
3461 if (vd
->phys_refnum
[i
] == refnum
) {
3464 return sec
* cnt
+ j
- 1;
3470 return DDF_NOTFOUND
;
3473 static struct mdinfo
*container_content_ddf(struct supertype
*st
, char *subarray
)
3475 /* Given a container loaded by load_super_ddf_all,
3476 * extract information about all the arrays into
3479 * For each vcl in conflist: create an mdinfo, fill it in,
3480 * then look for matching devices (phys_refnum) in dlist
3481 * and create appropriate device mdinfo.
3483 struct ddf_super
*ddf
= st
->sb
;
3484 struct mdinfo
*rest
= NULL
;
3487 for (vc
= ddf
->conflist
; vc
; vc
=vc
->next
)
3491 struct mdinfo
*this;
3497 (strtoul(subarray
, &ep
, 10) != vc
->vcnum
||
3501 if (vc
->conf
.sec_elmnt_count
> 1) {
3502 if (check_secondary(vc
) != 0)
3506 this = xcalloc(1, sizeof(*this));
3510 if (layout_ddf2md(&vc
->conf
, &this->array
))
3512 this->array
.md_minor
= -1;
3513 this->array
.major_version
= -1;
3514 this->array
.minor_version
= -2;
3515 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
3516 this->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
3517 this->array
.utime
= DECADE
+
3518 __be32_to_cpu(vc
->conf
.timestamp
);
3519 this->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
3522 if ((ddf
->virt
->entries
[i
].state
& DDF_state_inconsistent
) ||
3523 (ddf
->virt
->entries
[i
].init_state
& DDF_initstate_mask
) !=
3525 this->array
.state
= 0;
3526 this->resync_start
= 0;
3528 this->array
.state
= 1;
3529 this->resync_start
= MaxSector
;
3531 memcpy(this->name
, ddf
->virt
->entries
[i
].name
, 16);
3534 if (this->name
[j
] == ' ')
3537 memset(this->uuid
, 0, sizeof(this->uuid
));
3538 this->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
3539 this->array
.size
= this->component_size
/ 2;
3540 this->container_member
= i
;
3542 ddf
->currentconf
= vc
;
3543 uuid_from_super_ddf(st
, this->uuid
);
3544 ddf
->currentconf
= NULL
;
3546 sprintf(this->text_version
, "/%s/%d",
3547 st
->container_devnm
, this->container_member
);
3549 for (pd
= 0; pd
< __be16_to_cpu(ddf
->phys
->used_pdes
); pd
++) {
3552 const struct vd_config
*bvd
;
3557 if (ddf
->phys
->entries
[pd
].refnum
== 0xFFFFFFFF)
3560 stt
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3561 if ((stt
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3565 i
= get_pd_index_from_refnum(
3566 vc
, ddf
->phys
->entries
[pd
].refnum
,
3567 ddf
->mppe
, &bvd
, &iphys
);
3568 if (i
== DDF_NOTFOUND
)
3571 this->array
.working_disks
++;
3573 for (d
= ddf
->dlist
; d
; d
=d
->next
)
3574 if (d
->disk
.refnum
==
3575 ddf
->phys
->entries
[pd
].refnum
)
3578 /* Haven't found that one yet, maybe there are others */
3581 dev
= xcalloc(1, sizeof(*dev
));
3582 dev
->next
= this->devs
;
3585 dev
->disk
.number
= __be32_to_cpu(d
->disk
.refnum
);
3586 dev
->disk
.major
= d
->major
;
3587 dev
->disk
.minor
= d
->minor
;
3588 dev
->disk
.raid_disk
= i
;
3589 dev
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
3590 dev
->recovery_start
= MaxSector
;
3592 dev
->events
= __be32_to_cpu(ddf
->primary
.seq
);
3593 lba_offset
= (__u64
*)&bvd
->phys_refnum
[ddf
->mppe
];
3594 dev
->data_offset
= __be64_to_cpu(lba_offset
[iphys
]);
3595 dev
->component_size
= __be64_to_cpu(bvd
->blocks
);
3597 strcpy(dev
->name
, d
->devname
);
3603 static int store_super_ddf(struct supertype
*st
, int fd
)
3605 struct ddf_super
*ddf
= st
->sb
;
3606 unsigned long long dsize
;
3613 if (!get_dev_size(fd
, NULL
, &dsize
))
3616 if (ddf
->dlist
|| ddf
->conflist
) {
3621 if (fstat(fd
, &sta
) == -1 || !S_ISBLK(sta
.st_mode
)) {
3622 pr_err("%s: file descriptor for invalid device\n",
3626 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
3627 if (dl
->major
== (int)major(sta
.st_rdev
) &&
3628 dl
->minor
== (int)minor(sta
.st_rdev
))
3631 pr_err("%s: couldn't find disk %d/%d\n", __func__
,
3632 (int)major(sta
.st_rdev
),
3633 (int)minor(sta
.st_rdev
));
3637 For DDF, writing to just one disk makes no sense.
3638 We would run the risk of writing inconsistent meta data
3639 to the devices. So just call __write_init_super_ddf and
3640 write to all devices, including this one.
3641 Use the fd passed to this function, just in case dl->fd
3646 ret
= __write_init_super_ddf(st
);
3651 if (posix_memalign(&buf
, 512, 512) != 0)
3653 memset(buf
, 0, 512);
3655 lseek64(fd
, dsize
-512, 0);
3656 rc
= write(fd
, buf
, 512);
3663 static int compare_super_ddf(struct supertype
*st
, struct supertype
*tst
)
3667 * 0 same, or first was empty, and second was copied
3668 * 1 second had wrong number
3670 * 3 wrong other info
3672 struct ddf_super
*first
= st
->sb
;
3673 struct ddf_super
*second
= tst
->sb
;
3674 struct dl
*dl1
, *dl2
;
3675 struct vcl
*vl1
, *vl2
;
3676 unsigned int max_vds
, max_pds
, pd
, vd
;
3684 if (memcmp(first
->anchor
.guid
, second
->anchor
.guid
, DDF_GUID_LEN
) != 0)
3687 if (first
->anchor
.seq
!= second
->anchor
.seq
) {
3688 dprintf("%s: sequence number mismatch %u/%u\n", __func__
,
3689 __be32_to_cpu(first
->anchor
.seq
),
3690 __be32_to_cpu(second
->anchor
.seq
));
3693 if (first
->max_part
!= second
->max_part
||
3694 first
->phys
->used_pdes
!= second
->phys
->used_pdes
||
3695 first
->virt
->populated_vdes
!= second
->virt
->populated_vdes
) {
3696 dprintf("%s: PD/VD number mismatch\n", __func__
);
3700 max_pds
= __be16_to_cpu(first
->phys
->used_pdes
);
3701 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3702 for (pd
= 0; pd
< max_pds
; pd
++)
3703 if (first
->phys
->entries
[pd
].refnum
== dl2
->disk
.refnum
)
3705 if (pd
== max_pds
) {
3706 dprintf("%s: no match for disk %08x\n", __func__
,
3707 __be32_to_cpu(dl2
->disk
.refnum
));
3712 max_vds
= __be16_to_cpu(first
->active
->max_vd_entries
);
3713 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3714 if (vl2
->conf
.magic
!= DDF_VD_CONF_MAGIC
)
3716 for (vd
= 0; vd
< max_vds
; vd
++)
3717 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3718 vl2
->conf
.guid
, DDF_GUID_LEN
))
3720 if (vd
== max_vds
) {
3721 dprintf("%s: no match for VD config\n", __func__
);
3725 /* FIXME should I look at anything else? */
3728 At this point we are fairly sure that the meta data matches.
3729 But the new disk may contain additional local data.
3730 Add it to the super block.
3732 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3733 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
)
3734 if (!memcmp(vl1
->conf
.guid
, vl2
->conf
.guid
,
3738 if (vl1
->other_bvds
!= NULL
&&
3739 vl1
->conf
.sec_elmnt_seq
!=
3740 vl2
->conf
.sec_elmnt_seq
) {
3741 dprintf("%s: adding BVD %u\n", __func__
,
3742 vl2
->conf
.sec_elmnt_seq
);
3743 add_other_bvd(vl1
, &vl2
->conf
,
3744 first
->conf_rec_len
*512);
3749 if (posix_memalign((void **)&vl1
, 512,
3750 (first
->conf_rec_len
*512 +
3751 offsetof(struct vcl
, conf
))) != 0) {
3752 pr_err("%s could not allocate vcl buf\n",
3757 vl1
->next
= first
->conflist
;
3758 vl1
->block_sizes
= NULL
;
3759 if (vl2
->conf
.sec_elmnt_count
> 1) {
3760 vl1
->other_bvds
= xcalloc(vl2
->conf
.sec_elmnt_count
- 1,
3761 sizeof(struct vd_config
*));
3763 vl1
->other_bvds
= NULL
;
3764 memcpy(&vl1
->conf
, &vl2
->conf
, first
->conf_rec_len
*512);
3765 vl1
->lba_offset
= (__u64
*)
3766 &vl1
->conf
.phys_refnum
[first
->mppe
];
3767 for (vd
= 0; vd
< max_vds
; vd
++)
3768 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3769 vl1
->conf
.guid
, DDF_GUID_LEN
))
3772 dprintf("%s: added config for VD %u\n", __func__
, vl1
->vcnum
);
3773 first
->conflist
= vl1
;
3776 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3777 for (dl1
= first
->dlist
; dl1
; dl1
= dl1
->next
)
3778 if (dl1
->disk
.refnum
== dl2
->disk
.refnum
)
3783 if (posix_memalign((void **)&dl1
, 512,
3784 sizeof(*dl1
) + (first
->max_part
) * sizeof(dl1
->vlist
[0]))
3786 pr_err("%s could not allocate disk info buffer\n",
3790 memcpy(dl1
, dl2
, sizeof(*dl1
));
3791 dl1
->mdupdate
= NULL
;
3792 dl1
->next
= first
->dlist
;
3794 for (pd
= 0; pd
< max_pds
; pd
++)
3795 if (first
->phys
->entries
[pd
].refnum
== dl1
->disk
.refnum
)
3799 if (posix_memalign((void **)&dl1
->spare
, 512,
3800 first
->conf_rec_len
*512) != 0) {
3801 pr_err("%s could not allocate spare info buf\n",
3805 memcpy(dl1
->spare
, dl2
->spare
, first
->conf_rec_len
*512);
3807 for (vd
= 0 ; vd
< first
->max_part
; vd
++) {
3808 if (!dl2
->vlist
[vd
]) {
3809 dl1
->vlist
[vd
] = NULL
;
3812 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
) {
3813 if (!memcmp(vl1
->conf
.guid
,
3814 dl2
->vlist
[vd
]->conf
.guid
,
3817 dl1
->vlist
[vd
] = vl1
;
3821 dprintf("%s: added disk %d: %08x\n", __func__
, dl1
->pdnum
,
3830 * A new array 'a' has been started which claims to be instance 'inst'
3831 * within container 'c'.
3832 * We need to confirm that the array matches the metadata in 'c' so
3833 * that we don't corrupt any metadata.
3835 static int ddf_open_new(struct supertype
*c
, struct active_array
*a
, char *inst
)
3837 struct ddf_super
*ddf
= c
->sb
;
3839 if (all_ff(ddf
->virt
->entries
[n
].guid
)) {
3840 pr_err("%s: subarray %d doesn't exist\n", __func__
, n
);
3843 dprintf("ddf: open_new %d\n", n
);
3844 a
->info
.container_member
= n
;
3849 * The array 'a' is to be marked clean in the metadata.
3850 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
3851 * clean up to the point (in sectors). If that cannot be recorded in the
3852 * metadata, then leave it as dirty.
3854 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3855 * !global! virtual_disk.virtual_entry structure.
3857 static int ddf_set_array_state(struct active_array
*a
, int consistent
)
3859 struct ddf_super
*ddf
= a
->container
->sb
;
3860 int inst
= a
->info
.container_member
;
3861 int old
= ddf
->virt
->entries
[inst
].state
;
3862 if (consistent
== 2) {
3863 /* Should check if a recovery should be started FIXME */
3865 if (!is_resync_complete(&a
->info
))
3869 ddf
->virt
->entries
[inst
].state
&= ~DDF_state_inconsistent
;
3871 ddf
->virt
->entries
[inst
].state
|= DDF_state_inconsistent
;
3872 if (old
!= ddf
->virt
->entries
[inst
].state
)
3873 ddf_set_updates_pending(ddf
);
3875 old
= ddf
->virt
->entries
[inst
].init_state
;
3876 ddf
->virt
->entries
[inst
].init_state
&= ~DDF_initstate_mask
;
3877 if (is_resync_complete(&a
->info
))
3878 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_full
;
3879 else if (a
->info
.resync_start
== 0)
3880 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_not
;
3882 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_quick
;
3883 if (old
!= ddf
->virt
->entries
[inst
].init_state
)
3884 ddf_set_updates_pending(ddf
);
3886 dprintf("ddf mark %d %s %llu\n", inst
, consistent
?"clean":"dirty",
3887 a
->info
.resync_start
);
3891 static int get_bvd_state(const struct ddf_super
*ddf
,
3892 const struct vd_config
*vc
)
3894 unsigned int i
, n_bvd
, working
= 0;
3895 unsigned int n_prim
= __be16_to_cpu(vc
->prim_elmnt_count
);
3897 for (i
= 0; i
< n_prim
; i
++) {
3898 if (!find_index_in_bvd(ddf
, vc
, i
, &n_bvd
))
3900 pd
= find_phys(ddf
, vc
->phys_refnum
[n_bvd
]);
3903 st
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3904 if ((st
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3909 state
= DDF_state_degraded
;
3910 if (working
== n_prim
)
3911 state
= DDF_state_optimal
;
3917 state
= DDF_state_failed
;
3921 state
= DDF_state_failed
;
3922 else if (working
>= 2)
3923 state
= DDF_state_part_optimal
;
3927 if (working
< n_prim
- 1)
3928 state
= DDF_state_failed
;
3931 if (working
< n_prim
- 2)
3932 state
= DDF_state_failed
;
3933 else if (working
== n_prim
- 1)
3934 state
= DDF_state_part_optimal
;
3940 static int secondary_state(int state
, int other
, int seclevel
)
3942 if (state
== DDF_state_optimal
&& other
== DDF_state_optimal
)
3943 return DDF_state_optimal
;
3944 if (seclevel
== DDF_2MIRRORED
) {
3945 if (state
== DDF_state_optimal
|| other
== DDF_state_optimal
)
3946 return DDF_state_part_optimal
;
3947 if (state
== DDF_state_failed
&& other
== DDF_state_failed
)
3948 return DDF_state_failed
;
3949 return DDF_state_degraded
;
3951 if (state
== DDF_state_failed
|| other
== DDF_state_failed
)
3952 return DDF_state_failed
;
3953 if (state
== DDF_state_degraded
|| other
== DDF_state_degraded
)
3954 return DDF_state_degraded
;
3955 return DDF_state_part_optimal
;
3959 static int get_svd_state(const struct ddf_super
*ddf
, const struct vcl
*vcl
)
3961 int state
= get_bvd_state(ddf
, &vcl
->conf
);
3963 for (i
= 1; i
< vcl
->conf
.sec_elmnt_count
; i
++) {
3964 state
= secondary_state(
3966 get_bvd_state(ddf
, vcl
->other_bvds
[i
-1]),
3973 * The state of each disk is stored in the global phys_disk structure
3974 * in phys_disk.entries[n].state.
3975 * This makes various combinations awkward.
3976 * - When a device fails in any array, it must be failed in all arrays
3977 * that include a part of this device.
3978 * - When a component is rebuilding, we cannot include it officially in the
3979 * array unless this is the only array that uses the device.
3981 * So: when transitioning:
3982 * Online -> failed, just set failed flag. monitor will propagate
3983 * spare -> online, the device might need to be added to the array.
3984 * spare -> failed, just set failed. Don't worry if in array or not.
3986 static void ddf_set_disk(struct active_array
*a
, int n
, int state
)
3988 struct ddf_super
*ddf
= a
->container
->sb
;
3989 unsigned int inst
= a
->info
.container_member
, n_bvd
;
3991 struct vd_config
*vc
= find_vdcr(ddf
, inst
, (unsigned int)n
,
3998 dprintf("ddf: cannot find instance %d!!\n", inst
);
4001 /* Find the matching slot in 'info'. */
4002 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
4003 if (mdi
->disk
.raid_disk
== n
)
4008 /* and find the 'dl' entry corresponding to that. */
4009 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
4010 if (mdi
->state_fd
>= 0 &&
4011 mdi
->disk
.major
== dl
->major
&&
4012 mdi
->disk
.minor
== dl
->minor
)
4017 pd
= find_phys(ddf
, vc
->phys_refnum
[n_bvd
]);
4018 if (pd
< 0 || pd
!= dl
->pdnum
) {
4019 /* disk doesn't currently exist or has changed.
4020 * If it is now in_sync, insert it. */
4021 dprintf("%s: phys disk not found for %d: %d/%d ref %08x\n",
4022 __func__
, dl
->pdnum
, dl
->major
, dl
->minor
,
4024 dprintf("%s: array %u disk %u ref %08x pd %d\n",
4025 __func__
, inst
, n_bvd
, vc
->phys_refnum
[n_bvd
], pd
);
4026 if ((state
& DS_INSYNC
) && ! (state
& DS_FAULTY
)) {
4028 pd
= dl
->pdnum
; /* FIXME: is this really correct ? */
4029 vc
->phys_refnum
[n_bvd
] = dl
->disk
.refnum
;
4030 lba_offset
= (__u64
*)&vc
->phys_refnum
[ddf
->mppe
];
4031 lba_offset
[n_bvd
] = mdi
->data_offset
;
4032 ddf
->phys
->entries
[pd
].type
&=
4033 ~__cpu_to_be16(DDF_Global_Spare
);
4034 ddf
->phys
->entries
[pd
].type
|=
4035 __cpu_to_be16(DDF_Active_in_VD
);
4036 ddf_set_updates_pending(ddf
);
4039 int old
= ddf
->phys
->entries
[pd
].state
;
4040 if (state
& DS_FAULTY
)
4041 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Failed
);
4042 if (state
& DS_INSYNC
) {
4043 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Online
);
4044 ddf
->phys
->entries
[pd
].state
&= __cpu_to_be16(~DDF_Rebuilding
);
4046 if (old
!= ddf
->phys
->entries
[pd
].state
)
4047 ddf_set_updates_pending(ddf
);
4050 dprintf("ddf: set_disk %d to %x\n", n
, state
);
4052 /* Now we need to check the state of the array and update
4053 * virtual_disk.entries[n].state.
4054 * It needs to be one of "optimal", "degraded", "failed".
4055 * I don't understand 'deleted' or 'missing'.
4057 state
= get_svd_state(ddf
, vcl
);
4059 if (ddf
->virt
->entries
[inst
].state
!=
4060 ((ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
4063 ddf
->virt
->entries
[inst
].state
=
4064 (ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
4066 ddf_set_updates_pending(ddf
);
4071 static void ddf_sync_metadata(struct supertype
*st
)
4075 * Write all data to all devices.
4076 * Later, we might be able to track whether only local changes
4077 * have been made, or whether any global data has been changed,
4078 * but ddf is sufficiently weird that it probably always
4079 * changes global data ....
4081 struct ddf_super
*ddf
= st
->sb
;
4082 if (!ddf
->updates_pending
)
4084 ddf
->updates_pending
= 0;
4085 __write_init_super_ddf(st
);
4086 dprintf("ddf: sync_metadata\n");
4089 static void ddf_process_update(struct supertype
*st
,
4090 struct metadata_update
*update
)
4092 /* Apply this update to the metadata.
4093 * The first 4 bytes are a DDF_*_MAGIC which guides
4095 * Possible update are:
4096 * DDF_PHYS_RECORDS_MAGIC
4097 * Add a new physical device or remove an old one.
4098 * Changes to this record only happen implicitly.
4099 * used_pdes is the device number.
4100 * DDF_VIRT_RECORDS_MAGIC
4101 * Add a new VD. Possibly also change the 'access' bits.
4102 * populated_vdes is the entry number.
4104 * New or updated VD. the VIRT_RECORD must already
4105 * exist. For an update, phys_refnum and lba_offset
4106 * (at least) are updated, and the VD_CONF must
4107 * be written to precisely those devices listed with
4109 * DDF_SPARE_ASSIGN_MAGIC
4110 * replacement Spare Assignment Record... but for which device?
4113 * - to create a new array, we send a VIRT_RECORD and
4114 * a VD_CONF. Then assemble and start the array.
4115 * - to activate a spare we send a VD_CONF to add the phys_refnum
4116 * and offset. This will also mark the spare as active with
4117 * a spare-assignment record.
4119 struct ddf_super
*ddf
= st
->sb
;
4120 __u32
*magic
= (__u32
*)update
->buf
;
4121 struct phys_disk
*pd
;
4122 struct virtual_disk
*vd
;
4123 struct vd_config
*vc
;
4128 unsigned int pdnum
, pd2
;
4130 dprintf("Process update %x\n", *magic
);
4133 case DDF_PHYS_RECORDS_MAGIC
:
4135 if (update
->len
!= (sizeof(struct phys_disk
) +
4136 sizeof(struct phys_disk_entry
)))
4138 pd
= (struct phys_disk
*)update
->buf
;
4140 ent
= __be16_to_cpu(pd
->used_pdes
);
4141 if (ent
>= __be16_to_cpu(ddf
->phys
->max_pdes
))
4143 if (pd
->entries
[0].state
& __cpu_to_be16(DDF_Missing
)) {
4145 /* removing this disk. */
4146 ddf
->phys
->entries
[ent
].state
|= __cpu_to_be16(DDF_Missing
);
4147 for (dlp
= &ddf
->dlist
; *dlp
; dlp
= &(*dlp
)->next
) {
4148 struct dl
*dl
= *dlp
;
4149 if (dl
->pdnum
== (signed)ent
) {
4152 /* FIXME this doesn't free
4159 ddf_set_updates_pending(ddf
);
4162 if (!all_ff(ddf
->phys
->entries
[ent
].guid
))
4164 ddf
->phys
->entries
[ent
] = pd
->entries
[0];
4165 ddf
->phys
->used_pdes
= __cpu_to_be16(1 +
4166 __be16_to_cpu(ddf
->phys
->used_pdes
));
4167 ddf_set_updates_pending(ddf
);
4168 if (ddf
->add_list
) {
4169 struct active_array
*a
;
4170 struct dl
*al
= ddf
->add_list
;
4171 ddf
->add_list
= al
->next
;
4173 al
->next
= ddf
->dlist
;
4176 /* As a device has been added, we should check
4177 * for any degraded devices that might make
4178 * use of this spare */
4179 for (a
= st
->arrays
; a
; a
=a
->next
)
4180 a
->check_degraded
= 1;
4184 case DDF_VIRT_RECORDS_MAGIC
:
4186 if (update
->len
!= (sizeof(struct virtual_disk
) +
4187 sizeof(struct virtual_entry
)))
4189 vd
= (struct virtual_disk
*)update
->buf
;
4191 ent
= find_unused_vde(ddf
);
4192 if (ent
== DDF_NOTFOUND
)
4194 ddf
->virt
->entries
[ent
] = vd
->entries
[0];
4195 ddf
->virt
->populated_vdes
= __cpu_to_be16(1 +
4196 __be16_to_cpu(ddf
->virt
->populated_vdes
));
4197 ddf_set_updates_pending(ddf
);
4200 case DDF_VD_CONF_MAGIC
:
4201 dprintf("len %d %d\n", update
->len
, ddf
->conf_rec_len
);
4203 mppe
= __be16_to_cpu(ddf
->anchor
.max_primary_element_entries
);
4204 if ((unsigned)update
->len
!= ddf
->conf_rec_len
* 512)
4206 vc
= (struct vd_config
*)update
->buf
;
4207 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
4208 if (memcmp(vcl
->conf
.guid
, vc
->guid
, DDF_GUID_LEN
) == 0)
4210 dprintf("vcl = %p\n", vcl
);
4212 /* An update, just copy the phys_refnum and lba_offset
4215 memcpy(vcl
->conf
.phys_refnum
, vc
->phys_refnum
,
4216 mppe
* (sizeof(__u32
) + sizeof(__u64
)));
4221 vcl
= update
->space
;
4222 update
->space
= NULL
;
4223 vcl
->next
= ddf
->conflist
;
4224 memcpy(&vcl
->conf
, vc
, update
->len
);
4225 vcl
->lba_offset
= (__u64
*)
4226 &vcl
->conf
.phys_refnum
[mppe
];
4227 ent
= find_vde_by_guid(ddf
, vc
->guid
);
4228 if (ent
== DDF_NOTFOUND
)
4231 ddf
->conflist
= vcl
;
4233 /* Set DDF_Transition on all Failed devices - to help
4234 * us detect those that are no longer in use
4236 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
4237 if (ddf
->phys
->entries
[pdnum
].state
4238 & __be16_to_cpu(DDF_Failed
))
4239 ddf
->phys
->entries
[pdnum
].state
4240 |= __be16_to_cpu(DDF_Transition
);
4241 /* Now make sure vlist is correct for each dl. */
4242 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
4244 unsigned int vn
= 0;
4245 int in_degraded
= 0;
4246 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
4247 for (dn
=0; dn
< ddf
->mppe
; dn
++)
4248 if (vcl
->conf
.phys_refnum
[dn
] ==
4251 dprintf("dev %d has %p at %d\n",
4252 dl
->pdnum
, vcl
, vn
);
4253 /* Clear the Transition flag */
4254 if (ddf
->phys
->entries
[dl
->pdnum
].state
4255 & __be16_to_cpu(DDF_Failed
))
4256 ddf
->phys
->entries
[dl
->pdnum
].state
&=
4257 ~__be16_to_cpu(DDF_Transition
);
4259 dl
->vlist
[vn
++] = vcl
;
4260 vstate
= ddf
->virt
->entries
[vcl
->vcnum
].state
4262 if (vstate
== DDF_state_degraded
||
4263 vstate
== DDF_state_part_optimal
)
4267 while (vn
< ddf
->max_part
)
4268 dl
->vlist
[vn
++] = NULL
;
4270 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4271 ~__cpu_to_be16(DDF_Global_Spare
);
4272 if (!(ddf
->phys
->entries
[dl
->pdnum
].type
&
4273 __cpu_to_be16(DDF_Active_in_VD
))) {
4274 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4275 __cpu_to_be16(DDF_Active_in_VD
);
4277 ddf
->phys
->entries
[dl
->pdnum
].state
|=
4278 __cpu_to_be16(DDF_Rebuilding
);
4282 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4283 ~__cpu_to_be16(DDF_Global_Spare
);
4284 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4285 __cpu_to_be16(DDF_Spare
);
4287 if (!dl
->vlist
[0] && !dl
->spare
) {
4288 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4289 __cpu_to_be16(DDF_Global_Spare
);
4290 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4291 ~__cpu_to_be16(DDF_Spare
|
4296 /* Now remove any 'Failed' devices that are not part
4297 * of any VD. They will have the Transition flag set.
4298 * Once done, we need to update all dl->pdnum numbers.
4301 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
4302 if ((ddf
->phys
->entries
[pdnum
].state
4303 & __be16_to_cpu(DDF_Failed
))
4304 && (ddf
->phys
->entries
[pdnum
].state
4305 & __be16_to_cpu(DDF_Transition
)))
4306 /* skip this one */;
4307 else if (pdnum
== pd2
)
4310 ddf
->phys
->entries
[pd2
] = ddf
->phys
->entries
[pdnum
];
4311 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
4312 if (dl
->pdnum
== (int)pdnum
)
4316 ddf
->phys
->used_pdes
= __cpu_to_be16(pd2
);
4317 while (pd2
< pdnum
) {
4318 memset(ddf
->phys
->entries
[pd2
].guid
, 0xff, DDF_GUID_LEN
);
4322 ddf_set_updates_pending(ddf
);
4324 case DDF_SPARE_ASSIGN_MAGIC
:
4329 static void ddf_prepare_update(struct supertype
*st
,
4330 struct metadata_update
*update
)
4332 /* This update arrived at managemon.
4333 * We are about to pass it to monitor.
4334 * If a malloc is needed, do it here.
4336 struct ddf_super
*ddf
= st
->sb
;
4337 __u32
*magic
= (__u32
*)update
->buf
;
4338 if (*magic
== DDF_VD_CONF_MAGIC
)
4339 if (posix_memalign(&update
->space
, 512,
4340 offsetof(struct vcl
, conf
)
4341 + ddf
->conf_rec_len
* 512) != 0)
4342 update
->space
= NULL
;
4346 * Check if the array 'a' is degraded but not failed.
4347 * If it is, find as many spares as are available and needed and
4348 * arrange for their inclusion.
4349 * We only choose devices which are not already in the array,
4350 * and prefer those with a spare-assignment to this array.
4351 * otherwise we choose global spares - assuming always that
4352 * there is enough room.
4353 * For each spare that we assign, we return an 'mdinfo' which
4354 * describes the position for the device in the array.
4355 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4356 * the new phys_refnum and lba_offset values.
4358 * Only worry about BVDs at the moment.
4360 static struct mdinfo
*ddf_activate_spare(struct active_array
*a
,
4361 struct metadata_update
**updates
)
4365 struct ddf_super
*ddf
= a
->container
->sb
;
4367 struct mdinfo
*rv
= NULL
;
4369 struct metadata_update
*mu
;
4373 struct vd_config
*vc
;
4377 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
4378 if ((d
->curr_state
& DS_FAULTY
) &&
4380 /* wait for Removal to happen */
4382 if (d
->state_fd
>= 0)
4386 dprintf("ddf_activate: working=%d (%d) level=%d\n", working
, a
->info
.array
.raid_disks
,
4387 a
->info
.array
.level
);
4388 if (working
== a
->info
.array
.raid_disks
)
4389 return NULL
; /* array not degraded */
4390 switch (a
->info
.array
.level
) {
4393 return NULL
; /* failed */
4397 if (working
< a
->info
.array
.raid_disks
- 1)
4398 return NULL
; /* failed */
4401 if (working
< a
->info
.array
.raid_disks
- 2)
4402 return NULL
; /* failed */
4404 default: /* concat or stripe */
4405 return NULL
; /* failed */
4408 /* For each slot, if it is not working, find a spare */
4410 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
4411 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4412 if (d
->disk
.raid_disk
== i
)
4414 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
4415 if (d
&& (d
->state_fd
>= 0))
4418 /* OK, this device needs recovery. Find a spare */
4420 for ( ; dl
; dl
= dl
->next
) {
4421 unsigned long long esize
;
4422 unsigned long long pos
;
4425 int is_dedicated
= 0;
4428 /* If in this array, skip */
4429 for (d2
= a
->info
.devs
; d2
; d2
= d2
->next
)
4430 if (d2
->state_fd
>= 0 &&
4431 d2
->disk
.major
== dl
->major
&&
4432 d2
->disk
.minor
== dl
->minor
) {
4433 dprintf("%x:%x already in array\n", dl
->major
, dl
->minor
);
4438 if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4439 __cpu_to_be16(DDF_Spare
)) {
4440 /* Check spare assign record */
4442 if (dl
->spare
->type
& DDF_spare_dedicated
) {
4443 /* check spare_ents for guid */
4445 j
< __be16_to_cpu(dl
->spare
->populated
);
4447 if (memcmp(dl
->spare
->spare_ents
[j
].guid
,
4448 ddf
->virt
->entries
[a
->info
.container_member
].guid
,
4455 } else if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4456 __cpu_to_be16(DDF_Global_Spare
)) {
4458 } else if (!(ddf
->phys
->entries
[dl
->pdnum
].state
&
4459 __cpu_to_be16(DDF_Failed
))) {
4460 /* we can possibly use some of this */
4463 if ( ! (is_dedicated
||
4464 (is_global
&& global_ok
))) {
4465 dprintf("%x:%x not suitable: %d %d\n", dl
->major
, dl
->minor
,
4466 is_dedicated
, is_global
);
4470 /* We are allowed to use this device - is there space?
4471 * We need a->info.component_size sectors */
4472 ex
= get_extents(ddf
, dl
);
4474 dprintf("cannot get extents\n");
4481 esize
= ex
[j
].start
- pos
;
4482 if (esize
>= a
->info
.component_size
)
4484 pos
= ex
[j
].start
+ ex
[j
].size
;
4486 } while (ex
[j
-1].size
);
4489 if (esize
< a
->info
.component_size
) {
4490 dprintf("%x:%x has no room: %llu %llu\n",
4491 dl
->major
, dl
->minor
,
4492 esize
, a
->info
.component_size
);
4497 /* Cool, we have a device with some space at pos */
4498 di
= xcalloc(1, sizeof(*di
));
4499 di
->disk
.number
= i
;
4500 di
->disk
.raid_disk
= i
;
4501 di
->disk
.major
= dl
->major
;
4502 di
->disk
.minor
= dl
->minor
;
4504 di
->recovery_start
= 0;
4505 di
->data_offset
= pos
;
4506 di
->component_size
= a
->info
.component_size
;
4507 di
->container_member
= dl
->pdnum
;
4510 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
4515 if (!dl
&& ! global_ok
) {
4516 /* not enough dedicated spares, try global */
4524 /* No spares found */
4526 /* Now 'rv' has a list of devices to return.
4527 * Create a metadata_update record to update the
4528 * phys_refnum and lba_offset values
4530 mu
= xmalloc(sizeof(*mu
));
4531 if (posix_memalign(&mu
->space
, 512, sizeof(struct vcl
)) != 0) {
4535 mu
->buf
= xmalloc(ddf
->conf_rec_len
* 512);
4536 mu
->len
= ddf
->conf_rec_len
* 512;
4538 mu
->space_list
= NULL
;
4539 mu
->next
= *updates
;
4540 vc
= find_vdcr(ddf
, a
->info
.container_member
, di
->disk
.raid_disk
,
4542 memcpy(mu
->buf
, vc
, ddf
->conf_rec_len
* 512);
4544 vc
= (struct vd_config
*)mu
->buf
;
4545 lba
= (__u64
*)&vc
->phys_refnum
[ddf
->mppe
];
4546 for (di
= rv
; di
; di
= di
->next
) {
4547 vc
->phys_refnum
[di
->disk
.raid_disk
] =
4548 ddf
->phys
->entries
[dl
->pdnum
].refnum
;
4549 lba
[di
->disk
.raid_disk
] = di
->data_offset
;
4554 #endif /* MDASSEMBLE */
4556 static int ddf_level_to_layout(int level
)
4563 return ALGORITHM_LEFT_SYMMETRIC
;
4565 return ALGORITHM_ROTATING_N_CONTINUE
;
4573 static void default_geometry_ddf(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4575 if (level
&& *level
== UnSet
)
4576 *level
= LEVEL_CONTAINER
;
4578 if (level
&& layout
&& *layout
== UnSet
)
4579 *layout
= ddf_level_to_layout(*level
);
4582 struct superswitch super_ddf
= {
4584 .examine_super
= examine_super_ddf
,
4585 .brief_examine_super
= brief_examine_super_ddf
,
4586 .brief_examine_subarrays
= brief_examine_subarrays_ddf
,
4587 .export_examine_super
= export_examine_super_ddf
,
4588 .detail_super
= detail_super_ddf
,
4589 .brief_detail_super
= brief_detail_super_ddf
,
4590 .validate_geometry
= validate_geometry_ddf
,
4591 .write_init_super
= write_init_super_ddf
,
4592 .add_to_super
= add_to_super_ddf
,
4593 .remove_from_super
= remove_from_super_ddf
,
4594 .load_container
= load_container_ddf
,
4595 .copy_metadata
= copy_metadata_ddf
,
4597 .match_home
= match_home_ddf
,
4598 .uuid_from_super
= uuid_from_super_ddf
,
4599 .getinfo_super
= getinfo_super_ddf
,
4600 .update_super
= update_super_ddf
,
4602 .avail_size
= avail_size_ddf
,
4604 .compare_super
= compare_super_ddf
,
4606 .load_super
= load_super_ddf
,
4607 .init_super
= init_super_ddf
,
4608 .store_super
= store_super_ddf
,
4609 .free_super
= free_super_ddf
,
4610 .match_metadata_desc
= match_metadata_desc_ddf
,
4611 .container_content
= container_content_ddf
,
4612 .default_geometry
= default_geometry_ddf
,
4618 .open_new
= ddf_open_new
,
4619 .set_array_state
= ddf_set_array_state
,
4620 .set_disk
= ddf_set_disk
,
4621 .sync_metadata
= ddf_sync_metadata
,
4622 .process_update
= ddf_process_update
,
4623 .prepare_update
= ddf_prepare_update
,
4624 .activate_spare
= ddf_activate_spare
,