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(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 static int load_ddf_header(int fd
, unsigned long long lba
,
488 unsigned long long size
,
490 struct ddf_header
*hdr
, struct ddf_header
*anchor
)
492 /* read a ddf header (primary or secondary) from fd/lba
493 * and check that it is consistent with anchor
495 * magic, crc, guid, rev, and LBA's header_type, and
496 * everything after header_type must be the same
501 if (lseek64(fd
, lba
<<9, 0) < 0)
504 if (read(fd
, hdr
, 512) != 512)
507 if (hdr
->magic
!= DDF_HEADER_MAGIC
)
509 if (calc_crc(hdr
, 512) != hdr
->crc
)
511 if (memcmp(anchor
->guid
, hdr
->guid
, DDF_GUID_LEN
) != 0 ||
512 memcmp(anchor
->revision
, hdr
->revision
, 8) != 0 ||
513 anchor
->primary_lba
!= hdr
->primary_lba
||
514 anchor
->secondary_lba
!= hdr
->secondary_lba
||
516 memcmp(anchor
->pad2
, hdr
->pad2
, 512 -
517 offsetof(struct ddf_header
, pad2
)) != 0)
520 /* Looks good enough to me... */
524 static void *load_section(int fd
, struct ddf_super
*super
, void *buf
,
525 __u32 offset_be
, __u32 len_be
, int check
)
527 unsigned long long offset
= __be32_to_cpu(offset_be
);
528 unsigned long long len
= __be32_to_cpu(len_be
);
529 int dofree
= (buf
== NULL
);
532 if (len
!= 2 && len
!= 8 && len
!= 32
533 && len
!= 128 && len
!= 512)
539 /* All pre-allocated sections are a single block */
542 } else if (posix_memalign(&buf
, 512, len
<<9) != 0)
548 if (super
->active
->type
== 1)
549 offset
+= __be64_to_cpu(super
->active
->primary_lba
);
551 offset
+= __be64_to_cpu(super
->active
->secondary_lba
);
553 if ((unsigned long long)lseek64(fd
, offset
<<9, 0) != (offset
<<9)) {
558 if ((unsigned long long)read(fd
, buf
, len
<<9) != (len
<<9)) {
566 static int load_ddf_headers(int fd
, struct ddf_super
*super
, char *devname
)
568 unsigned long long dsize
;
570 get_dev_size(fd
, NULL
, &dsize
);
572 if (lseek64(fd
, dsize
-512, 0) < 0) {
574 pr_err("Cannot seek to anchor block on %s: %s\n",
575 devname
, strerror(errno
));
578 if (read(fd
, &super
->anchor
, 512) != 512) {
580 pr_err("Cannot read anchor block on %s: %s\n",
581 devname
, strerror(errno
));
584 if (super
->anchor
.magic
!= DDF_HEADER_MAGIC
) {
586 pr_err("no DDF anchor found on %s\n",
590 if (calc_crc(&super
->anchor
, 512) != super
->anchor
.crc
) {
592 pr_err("bad CRC on anchor on %s\n",
596 if (memcmp(super
->anchor
.revision
, DDF_REVISION_0
, 8) != 0 &&
597 memcmp(super
->anchor
.revision
, DDF_REVISION_2
, 8) != 0) {
599 pr_err("can only support super revision"
600 " %.8s and earlier, not %.8s on %s\n",
601 DDF_REVISION_2
, super
->anchor
.revision
,devname
);
604 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.primary_lba
),
606 &super
->primary
, &super
->anchor
) == 0) {
608 pr_err("Failed to load primary DDF header "
612 super
->active
= &super
->primary
;
613 if (load_ddf_header(fd
, __be64_to_cpu(super
->anchor
.secondary_lba
),
615 &super
->secondary
, &super
->anchor
)) {
616 if ((__be32_to_cpu(super
->primary
.seq
)
617 < __be32_to_cpu(super
->secondary
.seq
) &&
618 !super
->secondary
.openflag
)
619 || (__be32_to_cpu(super
->primary
.seq
)
620 == __be32_to_cpu(super
->secondary
.seq
) &&
621 super
->primary
.openflag
&& !super
->secondary
.openflag
)
623 super
->active
= &super
->secondary
;
628 static int load_ddf_global(int fd
, struct ddf_super
*super
, char *devname
)
631 ok
= load_section(fd
, super
, &super
->controller
,
632 super
->active
->controller_section_offset
,
633 super
->active
->controller_section_length
,
635 super
->phys
= load_section(fd
, super
, NULL
,
636 super
->active
->phys_section_offset
,
637 super
->active
->phys_section_length
,
639 super
->pdsize
= __be32_to_cpu(super
->active
->phys_section_length
) * 512;
641 super
->virt
= load_section(fd
, super
, NULL
,
642 super
->active
->virt_section_offset
,
643 super
->active
->virt_section_length
,
645 super
->vdsize
= __be32_to_cpu(super
->active
->virt_section_length
) * 512;
655 super
->conflist
= NULL
;
658 super
->max_part
= __be16_to_cpu(super
->active
->max_partitions
);
659 super
->mppe
= __be16_to_cpu(super
->active
->max_primary_element_entries
);
660 super
->conf_rec_len
= __be16_to_cpu(super
->active
->config_record_len
);
664 static void add_other_bvd(struct vcl
*vcl
, struct vd_config
*vd
,
668 for (i
= 0; i
< vcl
->conf
.sec_elmnt_count
-1; i
++)
669 if (vcl
->other_bvds
[i
] != NULL
&&
670 vcl
->other_bvds
[i
]->sec_elmnt_seq
== vd
->sec_elmnt_seq
)
673 if (i
< vcl
->conf
.sec_elmnt_count
-1) {
674 if (vd
->seqnum
<= vcl
->other_bvds
[i
]->seqnum
)
677 for (i
= 0; i
< vcl
->conf
.sec_elmnt_count
-1; i
++)
678 if (vcl
->other_bvds
[i
] == NULL
)
680 if (i
== vcl
->conf
.sec_elmnt_count
-1) {
681 pr_err("no space for sec level config %u, count is %u\n",
682 vd
->sec_elmnt_seq
, vcl
->conf
.sec_elmnt_count
);
685 if (posix_memalign((void **)&vcl
->other_bvds
[i
], 512, len
)
687 pr_err("%s could not allocate vd buf\n", __func__
);
691 memcpy(vcl
->other_bvds
[i
], vd
, len
);
694 static int load_ddf_local(int fd
, struct ddf_super
*super
,
695 char *devname
, int keep
)
701 unsigned int confsec
;
703 unsigned int max_virt_disks
= __be16_to_cpu(super
->active
->max_vd_entries
);
704 unsigned long long dsize
;
706 /* First the local disk info */
707 if (posix_memalign((void**)&dl
, 512,
709 (super
->max_part
) * sizeof(dl
->vlist
[0])) != 0) {
710 pr_err("%s could not allocate disk info buffer\n",
715 load_section(fd
, super
, &dl
->disk
,
716 super
->active
->data_section_offset
,
717 super
->active
->data_section_length
,
719 dl
->devname
= devname
? xstrdup(devname
) : NULL
;
722 dl
->major
= major(stb
.st_rdev
);
723 dl
->minor
= minor(stb
.st_rdev
);
724 dl
->next
= super
->dlist
;
725 dl
->fd
= keep
? fd
: -1;
728 if (get_dev_size(fd
, devname
, &dsize
))
729 dl
->size
= dsize
>> 9;
730 /* If the disks have different sizes, the LBAs will differ
731 * between phys disks.
732 * At this point here, the values in super->active must be valid
733 * for this phys disk. */
734 dl
->primary_lba
= super
->active
->primary_lba
;
735 dl
->secondary_lba
= super
->active
->secondary_lba
;
736 dl
->workspace_lba
= super
->active
->workspace_lba
;
738 for (i
= 0 ; i
< super
->max_part
; i
++)
742 for (i
= 0; i
< __be16_to_cpu(super
->active
->max_pd_entries
); i
++)
743 if (memcmp(super
->phys
->entries
[i
].guid
,
744 dl
->disk
.guid
, DDF_GUID_LEN
) == 0)
747 /* Now the config list. */
748 /* 'conf' is an array of config entries, some of which are
749 * probably invalid. Those which are good need to be copied into
753 conf
= load_section(fd
, super
, NULL
,
754 super
->active
->config_section_offset
,
755 super
->active
->config_section_length
,
760 confsec
< __be32_to_cpu(super
->active
->config_section_length
);
761 confsec
+= super
->conf_rec_len
) {
762 struct vd_config
*vd
=
763 (struct vd_config
*)((char*)conf
+ confsec
*512);
766 if (vd
->magic
== DDF_SPARE_ASSIGN_MAGIC
) {
769 if (posix_memalign((void**)&dl
->spare
, 512,
770 super
->conf_rec_len
*512) != 0) {
771 pr_err("%s could not allocate spare info buf\n",
776 memcpy(dl
->spare
, vd
, super
->conf_rec_len
*512);
779 if (vd
->magic
!= DDF_VD_CONF_MAGIC
)
781 for (vcl
= super
->conflist
; vcl
; vcl
= vcl
->next
) {
782 if (memcmp(vcl
->conf
.guid
,
783 vd
->guid
, DDF_GUID_LEN
) == 0)
788 dl
->vlist
[vnum
++] = vcl
;
789 if (vcl
->other_bvds
!= NULL
&&
790 vcl
->conf
.sec_elmnt_seq
!= vd
->sec_elmnt_seq
) {
791 add_other_bvd(vcl
, vd
, super
->conf_rec_len
*512);
794 if (__be32_to_cpu(vd
->seqnum
) <=
795 __be32_to_cpu(vcl
->conf
.seqnum
))
798 if (posix_memalign((void**)&vcl
, 512,
799 (super
->conf_rec_len
*512 +
800 offsetof(struct vcl
, conf
))) != 0) {
801 pr_err("%s could not allocate vcl buf\n",
805 vcl
->next
= super
->conflist
;
806 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
807 if (vd
->sec_elmnt_count
> 1)
809 xcalloc(vd
->sec_elmnt_count
- 1,
810 sizeof(struct vd_config
*));
812 vcl
->other_bvds
= NULL
;
813 super
->conflist
= vcl
;
814 dl
->vlist
[vnum
++] = vcl
;
816 memcpy(&vcl
->conf
, vd
, super
->conf_rec_len
*512);
817 vcl
->lba_offset
= (__u64
*)
818 &vcl
->conf
.phys_refnum
[super
->mppe
];
820 for (i
=0; i
< max_virt_disks
; i
++)
821 if (memcmp(super
->virt
->entries
[i
].guid
,
822 vcl
->conf
.guid
, DDF_GUID_LEN
)==0)
824 if (i
< max_virt_disks
)
833 static int load_super_ddf_all(struct supertype
*st
, int fd
,
834 void **sbp
, char *devname
);
837 static void free_super_ddf(struct supertype
*st
);
839 static int load_super_ddf(struct supertype
*st
, int fd
,
842 unsigned long long dsize
;
843 struct ddf_super
*super
;
846 if (get_dev_size(fd
, devname
, &dsize
) == 0)
849 if (!st
->ignore_hw_compat
&& test_partition(fd
))
850 /* DDF is not allowed on partitions */
853 /* 32M is a lower bound */
854 if (dsize
<= 32*1024*1024) {
856 pr_err("%s is too small for ddf: "
857 "size is %llu sectors.\n",
863 pr_err("%s is an odd size for ddf: "
864 "size is %llu bytes.\n",
871 if (posix_memalign((void**)&super
, 512, sizeof(*super
))!= 0) {
872 pr_err("malloc of %zu failed.\n",
876 memset(super
, 0, sizeof(*super
));
878 rv
= load_ddf_headers(fd
, super
, devname
);
884 /* Have valid headers and have chosen the best. Let's read in the rest*/
886 rv
= load_ddf_global(fd
, super
, devname
);
890 pr_err("Failed to load all information "
891 "sections on %s\n", devname
);
896 rv
= load_ddf_local(fd
, super
, devname
, 0);
900 pr_err("Failed to load all information "
901 "sections on %s\n", devname
);
906 /* Should possibly check the sections .... */
909 if (st
->ss
== NULL
) {
911 st
->minor_version
= 0;
918 static void free_super_ddf(struct supertype
*st
)
920 struct ddf_super
*ddf
= st
->sb
;
925 while (ddf
->conflist
) {
926 struct vcl
*v
= ddf
->conflist
;
927 ddf
->conflist
= v
->next
;
929 free(v
->block_sizes
);
932 for (i
= 0; i
< v
->conf
.sec_elmnt_count
-1; i
++)
933 if (v
->other_bvds
[i
] != NULL
)
934 free(v
->other_bvds
[i
]);
940 struct dl
*d
= ddf
->dlist
;
941 ddf
->dlist
= d
->next
;
948 while (ddf
->add_list
) {
949 struct dl
*d
= ddf
->add_list
;
950 ddf
->add_list
= d
->next
;
961 static struct supertype
*match_metadata_desc_ddf(char *arg
)
963 /* 'ddf' only support containers */
964 struct supertype
*st
;
965 if (strcmp(arg
, "ddf") != 0 &&
966 strcmp(arg
, "default") != 0
970 st
= xcalloc(1, sizeof(*st
));
973 st
->minor_version
= 0;
980 static mapping_t ddf_state
[] = {
986 { "Partially Optimal", 5},
992 static mapping_t ddf_init_state
[] = {
993 { "Not Initialised", 0},
994 { "QuickInit in Progress", 1},
995 { "Fully Initialised", 2},
999 static mapping_t ddf_access
[] = {
1003 { "Blocked (no access)", 3},
1007 static mapping_t ddf_level
[] = {
1008 { "RAID0", DDF_RAID0
},
1009 { "RAID1", DDF_RAID1
},
1010 { "RAID3", DDF_RAID3
},
1011 { "RAID4", DDF_RAID4
},
1012 { "RAID5", DDF_RAID5
},
1013 { "RAID1E",DDF_RAID1E
},
1014 { "JBOD", DDF_JBOD
},
1015 { "CONCAT",DDF_CONCAT
},
1016 { "RAID5E",DDF_RAID5E
},
1017 { "RAID5EE",DDF_RAID5EE
},
1018 { "RAID6", DDF_RAID6
},
1021 static mapping_t ddf_sec_level
[] = {
1022 { "Striped", DDF_2STRIPED
},
1023 { "Mirrored", DDF_2MIRRORED
},
1024 { "Concat", DDF_2CONCAT
},
1025 { "Spanned", DDF_2SPANNED
},
1030 struct num_mapping
{
1033 static struct num_mapping ddf_level_num
[] = {
1036 { DDF_RAID3
, LEVEL_UNSUPPORTED
},
1039 { DDF_RAID1E
, LEVEL_UNSUPPORTED
},
1040 { DDF_JBOD
, LEVEL_UNSUPPORTED
},
1041 { DDF_CONCAT
, LEVEL_LINEAR
},
1042 { DDF_RAID5E
, LEVEL_UNSUPPORTED
},
1043 { DDF_RAID5EE
, LEVEL_UNSUPPORTED
},
1048 static int map_num1(struct num_mapping
*map
, int num
)
1051 for (i
=0 ; map
[i
].num1
!= MAXINT
; i
++)
1052 if (map
[i
].num1
== num
)
1057 static int all_ff(char *guid
)
1060 for (i
= 0; i
< DDF_GUID_LEN
; i
++)
1061 if (guid
[i
] != (char)0xff)
1067 static void print_guid(char *guid
, int tstamp
)
1069 /* A GUIDs are part (or all) ASCII and part binary.
1070 * They tend to be space padded.
1071 * We print the GUID in HEX, then in parentheses add
1072 * any initial ASCII sequence, and a possible
1073 * time stamp from bytes 16-19
1075 int l
= DDF_GUID_LEN
;
1078 for (i
=0 ; i
<DDF_GUID_LEN
; i
++) {
1079 if ((i
&3)==0 && i
!= 0) printf(":");
1080 printf("%02X", guid
[i
]&255);
1084 while (l
&& guid
[l
-1] == ' ')
1086 for (i
=0 ; i
<l
; i
++) {
1087 if (guid
[i
] >= 0x20 && guid
[i
] < 0x7f)
1088 fputc(guid
[i
], stdout
);
1093 time_t then
= __be32_to_cpu(*(__u32
*)(guid
+16)) + DECADE
;
1096 tm
= localtime(&then
);
1097 strftime(tbuf
, 100, " %D %T",tm
);
1098 fputs(tbuf
, stdout
);
1103 static void examine_vd(int n
, struct ddf_super
*sb
, char *guid
)
1105 int crl
= sb
->conf_rec_len
;
1108 for (vcl
= sb
->conflist
; vcl
; vcl
= vcl
->next
) {
1110 struct vd_config
*vc
= &vcl
->conf
;
1112 if (calc_crc(vc
, crl
*512) != vc
->crc
)
1114 if (memcmp(vc
->guid
, guid
, DDF_GUID_LEN
) != 0)
1117 /* Ok, we know about this VD, let's give more details */
1118 printf(" Raid Devices[%d] : %d (", n
,
1119 __be16_to_cpu(vc
->prim_elmnt_count
));
1120 for (i
= 0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++) {
1122 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1123 for (j
=0; j
<cnt
; j
++)
1124 if (vc
->phys_refnum
[i
] == sb
->phys
->entries
[j
].refnum
)
1133 if (vc
->chunk_shift
!= 255)
1134 printf(" Chunk Size[%d] : %d sectors\n", n
,
1135 1 << vc
->chunk_shift
);
1136 printf(" Raid Level[%d] : %s\n", n
,
1137 map_num(ddf_level
, vc
->prl
)?:"-unknown-");
1138 if (vc
->sec_elmnt_count
!= 1) {
1139 printf(" Secondary Position[%d] : %d of %d\n", n
,
1140 vc
->sec_elmnt_seq
, vc
->sec_elmnt_count
);
1141 printf(" Secondary Level[%d] : %s\n", n
,
1142 map_num(ddf_sec_level
, vc
->srl
) ?: "-unknown-");
1144 printf(" Device Size[%d] : %llu\n", n
,
1145 (unsigned long long)__be64_to_cpu(vc
->blocks
)/2);
1146 printf(" Array Size[%d] : %llu\n", n
,
1147 (unsigned long long)__be64_to_cpu(vc
->array_blocks
)/2);
1151 static void examine_vds(struct ddf_super
*sb
)
1153 int cnt
= __be16_to_cpu(sb
->virt
->populated_vdes
);
1155 printf(" Virtual Disks : %d\n", cnt
);
1157 for (i
=0; i
<cnt
; i
++) {
1158 struct virtual_entry
*ve
= &sb
->virt
->entries
[i
];
1160 printf(" VD GUID[%d] : ", i
); print_guid(ve
->guid
, 1);
1162 printf(" unit[%d] : %d\n", i
, __be16_to_cpu(ve
->unit
));
1163 printf(" state[%d] : %s, %s%s\n", i
,
1164 map_num(ddf_state
, ve
->state
& 7),
1165 (ve
->state
& 8) ? "Morphing, ": "",
1166 (ve
->state
& 16)? "Not Consistent" : "Consistent");
1167 printf(" init state[%d] : %s\n", i
,
1168 map_num(ddf_init_state
, ve
->init_state
&3));
1169 printf(" access[%d] : %s\n", i
,
1170 map_num(ddf_access
, (ve
->init_state
>>6) & 3));
1171 printf(" Name[%d] : %.16s\n", i
, ve
->name
);
1172 examine_vd(i
, sb
, ve
->guid
);
1174 if (cnt
) printf("\n");
1177 static void examine_pds(struct ddf_super
*sb
)
1179 int cnt
= __be16_to_cpu(sb
->phys
->used_pdes
);
1182 printf(" Physical Disks : %d\n", cnt
);
1183 printf(" Number RefNo Size Device Type/State\n");
1185 for (i
=0 ; i
<cnt
; i
++) {
1186 struct phys_disk_entry
*pd
= &sb
->phys
->entries
[i
];
1187 int type
= __be16_to_cpu(pd
->type
);
1188 int state
= __be16_to_cpu(pd
->state
);
1190 //printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1192 printf(" %3d %08x ", i
,
1193 __be32_to_cpu(pd
->refnum
));
1195 (unsigned long long)__be64_to_cpu(pd
->config_size
)>>1);
1196 for (dl
= sb
->dlist
; dl
; dl
= dl
->next
) {
1197 if (dl
->disk
.refnum
== pd
->refnum
) {
1198 char *dv
= map_dev(dl
->major
, dl
->minor
, 0);
1200 printf("%-15s", dv
);
1207 printf(" %s%s%s%s%s",
1208 (type
&2) ? "active":"",
1209 (type
&4) ? "Global-Spare":"",
1210 (type
&8) ? "spare" : "",
1211 (type
&16)? ", foreign" : "",
1212 (type
&32)? "pass-through" : "");
1213 if (state
& DDF_Failed
)
1214 /* This over-rides these three */
1215 state
&= ~(DDF_Online
|DDF_Rebuilding
|DDF_Transition
);
1216 printf("/%s%s%s%s%s%s%s",
1217 (state
&1)? "Online": "Offline",
1218 (state
&2)? ", Failed": "",
1219 (state
&4)? ", Rebuilding": "",
1220 (state
&8)? ", in-transition": "",
1221 (state
&16)? ", SMART-errors": "",
1222 (state
&32)? ", Unrecovered-Read-Errors": "",
1223 (state
&64)? ", Missing" : "");
1228 static void examine_super_ddf(struct supertype
*st
, char *homehost
)
1230 struct ddf_super
*sb
= st
->sb
;
1232 printf(" Magic : %08x\n", __be32_to_cpu(sb
->anchor
.magic
));
1233 printf(" Version : %.8s\n", sb
->anchor
.revision
);
1234 printf("Controller GUID : "); print_guid(sb
->controller
.guid
, 0);
1236 printf(" Container GUID : "); print_guid(sb
->anchor
.guid
, 1);
1238 printf(" Seq : %08x\n", __be32_to_cpu(sb
->active
->seq
));
1239 printf(" Redundant hdr : %s\n", sb
->secondary
.magic
== DDF_HEADER_MAGIC
1245 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1247 static void uuid_from_ddf_guid(const char *guid
, int uuid
[4]);
1248 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4]);
1250 static unsigned int get_vd_num_of_subarray(struct supertype
*st
)
1253 * Figure out the VD number for this supertype.
1254 * Returns DDF_CONTAINER for the container itself,
1255 * and DDF_NOTFOUND on error.
1257 struct ddf_super
*ddf
= st
->sb
;
1262 if (*st
->container_devnm
== '\0')
1263 return DDF_CONTAINER
;
1265 sra
= sysfs_read(-1, st
->devnm
, GET_VERSION
);
1266 if (!sra
|| sra
->array
.major_version
!= -1 ||
1267 sra
->array
.minor_version
!= -2 ||
1268 !is_subarray(sra
->text_version
))
1269 return DDF_NOTFOUND
;
1271 sub
= strchr(sra
->text_version
+ 1, '/');
1273 vcnum
= strtoul(sub
+ 1, &end
, 10);
1274 if (sub
== NULL
|| *sub
== '\0' || *end
!= '\0' ||
1275 vcnum
>= __be16_to_cpu(ddf
->active
->max_vd_entries
))
1276 return DDF_NOTFOUND
;
1281 static void brief_examine_super_ddf(struct supertype
*st
, int verbose
)
1283 /* We just write a generic DDF ARRAY entry
1287 getinfo_super_ddf(st
, &info
, NULL
);
1288 fname_from_uuid(st
, &info
, nbuf
, ':');
1290 printf("ARRAY metadata=ddf UUID=%s\n", nbuf
+ 5);
1293 static void brief_examine_subarrays_ddf(struct supertype
*st
, int verbose
)
1295 /* We just write a generic DDF ARRAY entry
1297 struct ddf_super
*ddf
= st
->sb
;
1301 getinfo_super_ddf(st
, &info
, NULL
);
1302 fname_from_uuid(st
, &info
, nbuf
, ':');
1304 for (i
= 0; i
< __be16_to_cpu(ddf
->virt
->max_vdes
); i
++) {
1305 struct virtual_entry
*ve
= &ddf
->virt
->entries
[i
];
1308 if (all_ff(ve
->guid
))
1310 memcpy(vcl
.conf
.guid
, ve
->guid
, DDF_GUID_LEN
);
1311 ddf
->currentconf
=&vcl
;
1312 uuid_from_super_ddf(st
, info
.uuid
);
1313 fname_from_uuid(st
, &info
, nbuf1
, ':');
1314 printf("ARRAY container=%s member=%d UUID=%s\n",
1315 nbuf
+5, i
, nbuf1
+5);
1319 static void export_examine_super_ddf(struct supertype
*st
)
1323 getinfo_super_ddf(st
, &info
, NULL
);
1324 fname_from_uuid(st
, &info
, nbuf
, ':');
1325 printf("MD_METADATA=ddf\n");
1326 printf("MD_LEVEL=container\n");
1327 printf("MD_UUID=%s\n", nbuf
+5);
1330 static int copy_metadata_ddf(struct supertype
*st
, int from
, int to
)
1333 unsigned long long dsize
, offset
;
1335 struct ddf_header
*ddf
;
1338 /* The meta consists of an anchor, a primary, and a secondary.
1339 * This all lives at the end of the device.
1340 * So it is easiest to find the earliest of primary and
1341 * secondary, and copy everything from there.
1343 * Anchor is 512 from end It contains primary_lba and secondary_lba
1344 * we choose one of those
1347 if (posix_memalign(&buf
, 4096, 4096) != 0)
1350 if (!get_dev_size(from
, NULL
, &dsize
))
1353 if (lseek64(from
, dsize
-512, 0) < 0)
1355 if (read(from
, buf
, 512) != 512)
1358 if (ddf
->magic
!= DDF_HEADER_MAGIC
||
1359 calc_crc(ddf
, 512) != ddf
->crc
||
1360 (memcmp(ddf
->revision
, DDF_REVISION_0
, 8) != 0 &&
1361 memcmp(ddf
->revision
, DDF_REVISION_2
, 8) != 0))
1364 offset
= dsize
- 512;
1365 if ((__be64_to_cpu(ddf
->primary_lba
) << 9) < offset
)
1366 offset
= __be64_to_cpu(ddf
->primary_lba
) << 9;
1367 if ((__be64_to_cpu(ddf
->secondary_lba
) << 9) < offset
)
1368 offset
= __be64_to_cpu(ddf
->secondary_lba
) << 9;
1370 bytes
= dsize
- offset
;
1372 if (lseek64(from
, offset
, 0) < 0 ||
1373 lseek64(to
, offset
, 0) < 0)
1375 while (written
< bytes
) {
1376 int n
= bytes
- written
;
1379 if (read(from
, buf
, n
) != n
)
1381 if (write(to
, buf
, n
) != n
)
1392 static void detail_super_ddf(struct supertype
*st
, char *homehost
)
1395 * Could print DDF GUID
1396 * Need to find which array
1397 * If whole, briefly list all arrays
1402 static void brief_detail_super_ddf(struct supertype
*st
)
1406 struct ddf_super
*ddf
= st
->sb
;
1407 unsigned int vcnum
= get_vd_num_of_subarray(st
);
1408 if (vcnum
== DDF_CONTAINER
)
1409 uuid_from_super_ddf(st
, info
.uuid
);
1410 else if (vcnum
== DDF_NOTFOUND
)
1413 uuid_from_ddf_guid(ddf
->virt
->entries
[vcnum
].guid
, info
.uuid
);
1414 fname_from_uuid(st
, &info
, nbuf
,':');
1415 printf(" UUID=%s", nbuf
+ 5);
1419 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1421 /* It matches 'this' host if the controller is a
1422 * Linux-MD controller with vendor_data matching
1425 struct ddf_super
*ddf
= st
->sb
;
1430 len
= strlen(homehost
);
1432 return (memcmp(ddf
->controller
.guid
, T10
, 8) == 0 &&
1433 len
< sizeof(ddf
->controller
.vendor_data
) &&
1434 memcmp(ddf
->controller
.vendor_data
, homehost
,len
) == 0 &&
1435 ddf
->controller
.vendor_data
[len
] == 0);
1439 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
, unsigned int inst
)
1443 for (v
= ddf
->conflist
; v
; v
= v
->next
)
1444 if (inst
== v
->vcnum
)
1450 static int find_phys(struct ddf_super
*ddf
, __u32 phys_refnum
)
1452 /* Find the entry in phys_disk which has the given refnum
1453 * and return it's index
1456 for (i
= 0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
); i
++)
1457 if (ddf
->phys
->entries
[i
].refnum
== phys_refnum
)
1462 static void uuid_from_ddf_guid(const char *guid
, int uuid
[4])
1465 struct sha1_ctx ctx
;
1466 sha1_init_ctx(&ctx
);
1467 sha1_process_bytes(guid
, DDF_GUID_LEN
, &ctx
);
1468 sha1_finish_ctx(&ctx
, buf
);
1469 memcpy(uuid
, buf
, 4*4);
1472 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
1474 /* The uuid returned here is used for:
1475 * uuid to put into bitmap file (Create, Grow)
1476 * uuid for backup header when saving critical section (Grow)
1477 * comparing uuids when re-adding a device into an array
1478 * In these cases the uuid required is that of the data-array,
1479 * not the device-set.
1480 * uuid to recognise same set when adding a missing device back
1481 * to an array. This is a uuid for the device-set.
1483 * For each of these we can make do with a truncated
1484 * or hashed uuid rather than the original, as long as
1486 * In the case of SVD we assume the BVD is of interest,
1487 * though that might be the case if a bitmap were made for
1488 * a mirrored SVD - worry about that later.
1489 * So we need to find the VD configuration record for the
1490 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1491 * The first 16 bytes of the sha1 of these is used.
1493 struct ddf_super
*ddf
= st
->sb
;
1494 struct vcl
*vcl
= ddf
->currentconf
;
1498 guid
= vcl
->conf
.guid
;
1500 guid
= ddf
->anchor
.guid
;
1501 uuid_from_ddf_guid(guid
, uuid
);
1504 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1506 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1508 struct ddf_super
*ddf
= st
->sb
;
1509 int map_disks
= info
->array
.raid_disks
;
1512 if (ddf
->currentconf
) {
1513 getinfo_super_ddf_bvd(st
, info
, map
);
1516 memset(info
, 0, sizeof(*info
));
1518 info
->array
.raid_disks
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1519 info
->array
.level
= LEVEL_CONTAINER
;
1520 info
->array
.layout
= 0;
1521 info
->array
.md_minor
= -1;
1522 cptr
= (__u32
*)(ddf
->anchor
.guid
+ 16);
1523 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1525 info
->array
.utime
= 0;
1526 info
->array
.chunk_size
= 0;
1527 info
->container_enough
= 1;
1529 info
->disk
.major
= 0;
1530 info
->disk
.minor
= 0;
1532 info
->disk
.number
= __be32_to_cpu(ddf
->dlist
->disk
.refnum
);
1533 info
->disk
.raid_disk
= find_phys(ddf
, ddf
->dlist
->disk
.refnum
);
1535 info
->data_offset
= __be64_to_cpu(ddf
->phys
->
1536 entries
[info
->disk
.raid_disk
].
1538 info
->component_size
= ddf
->dlist
->size
- info
->data_offset
;
1540 info
->disk
.number
= -1;
1541 info
->disk
.raid_disk
= -1;
1542 // info->disk.raid_disk = find refnum in the table and use index;
1544 info
->disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
1546 info
->recovery_start
= MaxSector
;
1547 info
->reshape_active
= 0;
1548 info
->recovery_blocked
= 0;
1551 info
->array
.major_version
= -1;
1552 info
->array
.minor_version
= -2;
1553 strcpy(info
->text_version
, "ddf");
1554 info
->safe_mode_delay
= 0;
1556 uuid_from_super_ddf(st
, info
->uuid
);
1560 for (i
= 0 ; i
< map_disks
; i
++) {
1561 if (i
< info
->array
.raid_disks
&&
1562 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1563 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1571 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
);
1573 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1575 struct ddf_super
*ddf
= st
->sb
;
1576 struct vcl
*vc
= ddf
->currentconf
;
1577 int cd
= ddf
->currentdev
;
1580 int map_disks
= info
->array
.raid_disks
;
1583 memset(info
, 0, sizeof(*info
));
1584 /* FIXME this returns BVD info - what if we want SVD ?? */
1586 info
->array
.raid_disks
= __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
1587 info
->array
.level
= map_num1(ddf_level_num
, vc
->conf
.prl
);
1588 info
->array
.layout
= rlq_to_layout(vc
->conf
.rlq
, vc
->conf
.prl
,
1589 info
->array
.raid_disks
);
1590 info
->array
.md_minor
= -1;
1591 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
1592 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1593 info
->array
.utime
= DECADE
+ __be32_to_cpu(vc
->conf
.timestamp
);
1594 info
->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
1595 info
->custom_array_size
= 0;
1597 if (cd
>= 0 && (unsigned)cd
< ddf
->mppe
) {
1598 info
->data_offset
= __be64_to_cpu(vc
->lba_offset
[cd
]);
1599 if (vc
->block_sizes
)
1600 info
->component_size
= vc
->block_sizes
[cd
];
1602 info
->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
1605 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
1606 if (dl
->raiddisk
== ddf
->currentdev
)
1609 info
->disk
.major
= 0;
1610 info
->disk
.minor
= 0;
1611 info
->disk
.state
= 0;
1613 info
->disk
.major
= dl
->major
;
1614 info
->disk
.minor
= dl
->minor
;
1615 info
->disk
.raid_disk
= dl
->raiddisk
;
1616 info
->disk
.number
= dl
->pdnum
;
1617 info
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
1620 info
->container_member
= ddf
->currentconf
->vcnum
;
1622 info
->recovery_start
= MaxSector
;
1623 info
->resync_start
= 0;
1624 info
->reshape_active
= 0;
1625 info
->recovery_blocked
= 0;
1626 if (!(ddf
->virt
->entries
[info
->container_member
].state
1627 & DDF_state_inconsistent
) &&
1628 (ddf
->virt
->entries
[info
->container_member
].init_state
1629 & DDF_initstate_mask
)
1631 info
->resync_start
= MaxSector
;
1633 uuid_from_super_ddf(st
, info
->uuid
);
1635 info
->array
.major_version
= -1;
1636 info
->array
.minor_version
= -2;
1637 sprintf(info
->text_version
, "/%s/%d",
1638 st
->container_devnm
,
1639 info
->container_member
);
1640 info
->safe_mode_delay
= 200;
1642 memcpy(info
->name
, ddf
->virt
->entries
[info
->container_member
].name
, 16);
1645 if (info
->name
[j
] == ' ')
1649 for (j
= 0; j
< map_disks
; j
++) {
1651 if (j
< info
->array
.raid_disks
) {
1652 int i
= find_phys(ddf
, vc
->conf
.phys_refnum
[j
]);
1654 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1655 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1661 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1663 char *devname
, int verbose
,
1664 int uuid_set
, char *homehost
)
1666 /* For 'assemble' and 'force' we need to return non-zero if any
1667 * change was made. For others, the return value is ignored.
1668 * Update options are:
1669 * force-one : This device looks a bit old but needs to be included,
1670 * update age info appropriately.
1671 * assemble: clear any 'faulty' flag to allow this device to
1673 * force-array: Array is degraded but being forced, mark it clean
1674 * if that will be needed to assemble it.
1676 * newdev: not used ????
1677 * grow: Array has gained a new device - this is currently for
1679 * resync: mark as dirty so a resync will happen.
1680 * uuid: Change the uuid of the array to match what is given
1681 * homehost: update the recorded homehost
1682 * name: update the name - preserving the homehost
1683 * _reshape_progress: record new reshape_progress position.
1685 * Following are not relevant for this version:
1686 * sparc2.2 : update from old dodgey metadata
1687 * super-minor: change the preferred_minor number
1688 * summaries: update redundant counters.
1691 // struct ddf_super *ddf = st->sb;
1692 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1693 // struct virtual_entry *ve = find_ve(ddf);
1695 /* we don't need to handle "force-*" or "assemble" as
1696 * there is no need to 'trick' the kernel. We the metadata is
1697 * first updated to activate the array, all the implied modifications
1701 if (strcmp(update
, "grow") == 0) {
1703 } else if (strcmp(update
, "resync") == 0) {
1704 // info->resync_checkpoint = 0;
1705 } else if (strcmp(update
, "homehost") == 0) {
1706 /* homehost is stored in controller->vendor_data,
1707 * or it is when we are the vendor
1709 // if (info->vendor_is_local)
1710 // strcpy(ddf->controller.vendor_data, homehost);
1712 } else if (strcmp(update
, "name") == 0) {
1713 /* name is stored in virtual_entry->name */
1714 // memset(ve->name, ' ', 16);
1715 // strncpy(ve->name, info->name, 16);
1717 } else if (strcmp(update
, "_reshape_progress") == 0) {
1718 /* We don't support reshape yet */
1719 } else if (strcmp(update
, "assemble") == 0 ) {
1720 /* Do nothing, just succeed */
1725 // update_all_csum(ddf);
1730 static void make_header_guid(char *guid
)
1733 /* Create a DDF Header of Virtual Disk GUID */
1735 /* 24 bytes of fiction required.
1736 * first 8 are a 'vendor-id' - "Linux-MD"
1737 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1738 * Remaining 8 random number plus timestamp
1740 memcpy(guid
, T10
, sizeof(T10
));
1741 stamp
= __cpu_to_be32(0xdeadbeef);
1742 memcpy(guid
+8, &stamp
, 4);
1743 stamp
= __cpu_to_be32(0);
1744 memcpy(guid
+12, &stamp
, 4);
1745 stamp
= __cpu_to_be32(time(0) - DECADE
);
1746 memcpy(guid
+16, &stamp
, 4);
1748 memcpy(guid
+20, &stamp
, 4);
1751 static int init_super_ddf_bvd(struct supertype
*st
,
1752 mdu_array_info_t
*info
,
1753 unsigned long long size
,
1754 char *name
, char *homehost
,
1755 int *uuid
, unsigned long long data_offset
);
1757 static int init_super_ddf(struct supertype
*st
,
1758 mdu_array_info_t
*info
,
1759 unsigned long long size
, char *name
, char *homehost
,
1760 int *uuid
, unsigned long long data_offset
)
1762 /* This is primarily called by Create when creating a new array.
1763 * We will then get add_to_super called for each component, and then
1764 * write_init_super called to write it out to each device.
1765 * For DDF, Create can create on fresh devices or on a pre-existing
1767 * To create on a pre-existing array a different method will be called.
1768 * This one is just for fresh drives.
1770 * We need to create the entire 'ddf' structure which includes:
1771 * DDF headers - these are easy.
1772 * Controller data - a Sector describing this controller .. not that
1773 * this is a controller exactly.
1774 * Physical Disk Record - one entry per device, so
1775 * leave plenty of space.
1776 * Virtual Disk Records - again, just leave plenty of space.
1777 * This just lists VDs, doesn't give details
1778 * Config records - describes the VDs that use this disk
1779 * DiskData - describes 'this' device.
1780 * BadBlockManagement - empty
1781 * Diag Space - empty
1782 * Vendor Logs - Could we put bitmaps here?
1785 struct ddf_super
*ddf
;
1788 int max_phys_disks
, max_virt_disks
;
1789 unsigned long long sector
;
1793 struct phys_disk
*pd
;
1794 struct virtual_disk
*vd
;
1796 if (data_offset
!= INVALID_SECTORS
) {
1797 pr_err("data-offset not supported by DDF\n");
1802 return init_super_ddf_bvd(st
, info
, size
, name
, homehost
, uuid
,
1805 if (posix_memalign((void**)&ddf
, 512, sizeof(*ddf
)) != 0) {
1806 pr_err("%s could not allocate superblock\n", __func__
);
1809 memset(ddf
, 0, sizeof(*ddf
));
1810 ddf
->dlist
= NULL
; /* no physical disks yet */
1811 ddf
->conflist
= NULL
; /* No virtual disks yet */
1815 /* zeroing superblock */
1819 /* At least 32MB *must* be reserved for the ddf. So let's just
1820 * start 32MB from the end, and put the primary header there.
1821 * Don't do secondary for now.
1822 * We don't know exactly where that will be yet as it could be
1823 * different on each device. To just set up the lengths.
1827 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
1828 make_header_guid(ddf
->anchor
.guid
);
1830 memcpy(ddf
->anchor
.revision
, DDF_REVISION_2
, 8);
1831 ddf
->anchor
.seq
= __cpu_to_be32(1);
1832 ddf
->anchor
.timestamp
= __cpu_to_be32(time(0) - DECADE
);
1833 ddf
->anchor
.openflag
= 0xFF;
1834 ddf
->anchor
.foreignflag
= 0;
1835 ddf
->anchor
.enforcegroups
= 0; /* Is this best?? */
1836 ddf
->anchor
.pad0
= 0xff;
1837 memset(ddf
->anchor
.pad1
, 0xff, 12);
1838 memset(ddf
->anchor
.header_ext
, 0xff, 32);
1839 ddf
->anchor
.primary_lba
= ~(__u64
)0;
1840 ddf
->anchor
.secondary_lba
= ~(__u64
)0;
1841 ddf
->anchor
.type
= DDF_HEADER_ANCHOR
;
1842 memset(ddf
->anchor
.pad2
, 0xff, 3);
1843 ddf
->anchor
.workspace_len
= __cpu_to_be32(32768); /* Must be reserved */
1844 ddf
->anchor
.workspace_lba
= ~(__u64
)0; /* Put this at bottom
1845 of 32M reserved.. */
1846 max_phys_disks
= 1023; /* Should be enough */
1847 ddf
->anchor
.max_pd_entries
= __cpu_to_be16(max_phys_disks
);
1848 max_virt_disks
= 255;
1849 ddf
->anchor
.max_vd_entries
= __cpu_to_be16(max_virt_disks
); /* ?? */
1850 ddf
->anchor
.max_partitions
= __cpu_to_be16(64); /* ?? */
1853 ddf
->conf_rec_len
= 1 + ROUND_UP(ddf
->mppe
* (4+8), 512)/512;
1854 ddf
->anchor
.config_record_len
= __cpu_to_be16(ddf
->conf_rec_len
);
1855 ddf
->anchor
.max_primary_element_entries
= __cpu_to_be16(ddf
->mppe
);
1856 memset(ddf
->anchor
.pad3
, 0xff, 54);
1857 /* controller sections is one sector long immediately
1858 * after the ddf header */
1860 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
1861 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
1864 /* phys is 8 sectors after that */
1865 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
1866 sizeof(struct phys_disk_entry
)*max_phys_disks
,
1868 switch(pdsize
/512) {
1869 case 2: case 8: case 32: case 128: case 512: break;
1872 ddf
->anchor
.phys_section_offset
= __cpu_to_be32(sector
);
1873 ddf
->anchor
.phys_section_length
=
1874 __cpu_to_be32(pdsize
/512); /* max_primary_element_entries/8 */
1875 sector
+= pdsize
/512;
1877 /* virt is another 32 sectors */
1878 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
1879 sizeof(struct virtual_entry
) * max_virt_disks
,
1881 switch(vdsize
/512) {
1882 case 2: case 8: case 32: case 128: case 512: break;
1885 ddf
->anchor
.virt_section_offset
= __cpu_to_be32(sector
);
1886 ddf
->anchor
.virt_section_length
=
1887 __cpu_to_be32(vdsize
/512); /* max_vd_entries/8 */
1888 sector
+= vdsize
/512;
1890 clen
= ddf
->conf_rec_len
* (ddf
->max_part
+1);
1891 ddf
->anchor
.config_section_offset
= __cpu_to_be32(sector
);
1892 ddf
->anchor
.config_section_length
= __cpu_to_be32(clen
);
1895 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
1896 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
1899 ddf
->anchor
.bbm_section_length
= __cpu_to_be32(0);
1900 ddf
->anchor
.bbm_section_offset
= __cpu_to_be32(0xFFFFFFFF);
1901 ddf
->anchor
.diag_space_length
= __cpu_to_be32(0);
1902 ddf
->anchor
.diag_space_offset
= __cpu_to_be32(0xFFFFFFFF);
1903 ddf
->anchor
.vendor_length
= __cpu_to_be32(0);
1904 ddf
->anchor
.vendor_offset
= __cpu_to_be32(0xFFFFFFFF);
1906 memset(ddf
->anchor
.pad4
, 0xff, 256);
1908 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1909 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1911 ddf
->primary
.openflag
= 1; /* I guess.. */
1912 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1914 ddf
->secondary
.openflag
= 1; /* I guess.. */
1915 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1917 ddf
->active
= &ddf
->primary
;
1919 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
1921 /* 24 more bytes of fiction required.
1922 * first 8 are a 'vendor-id' - "Linux-MD"
1923 * Remaining 16 are serial number.... maybe a hostname would do?
1925 memcpy(ddf
->controller
.guid
, T10
, sizeof(T10
));
1926 gethostname(hostname
, sizeof(hostname
));
1927 hostname
[sizeof(hostname
) - 1] = 0;
1928 hostlen
= strlen(hostname
);
1929 memcpy(ddf
->controller
.guid
+ 24 - hostlen
, hostname
, hostlen
);
1930 for (i
= strlen(T10
) ; i
+hostlen
< 24; i
++)
1931 ddf
->controller
.guid
[i
] = ' ';
1933 ddf
->controller
.type
.vendor_id
= __cpu_to_be16(0xDEAD);
1934 ddf
->controller
.type
.device_id
= __cpu_to_be16(0xBEEF);
1935 ddf
->controller
.type
.sub_vendor_id
= 0;
1936 ddf
->controller
.type
.sub_device_id
= 0;
1937 memcpy(ddf
->controller
.product_id
, "What Is My PID??", 16);
1938 memset(ddf
->controller
.pad
, 0xff, 8);
1939 memset(ddf
->controller
.vendor_data
, 0xff, 448);
1940 if (homehost
&& strlen(homehost
) < 440)
1941 strcpy((char*)ddf
->controller
.vendor_data
, homehost
);
1943 if (posix_memalign((void**)&pd
, 512, pdsize
) != 0) {
1944 pr_err("%s could not allocate pd\n", __func__
);
1948 ddf
->pdsize
= pdsize
;
1950 memset(pd
, 0xff, pdsize
);
1951 memset(pd
, 0, sizeof(*pd
));
1952 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
1953 pd
->used_pdes
= __cpu_to_be16(0);
1954 pd
->max_pdes
= __cpu_to_be16(max_phys_disks
);
1955 memset(pd
->pad
, 0xff, 52);
1957 if (posix_memalign((void**)&vd
, 512, vdsize
) != 0) {
1958 pr_err("%s could not allocate vd\n", __func__
);
1962 ddf
->vdsize
= vdsize
;
1963 memset(vd
, 0, vdsize
);
1964 vd
->magic
= DDF_VIRT_RECORDS_MAGIC
;
1965 vd
->populated_vdes
= __cpu_to_be16(0);
1966 vd
->max_vdes
= __cpu_to_be16(max_virt_disks
);
1967 memset(vd
->pad
, 0xff, 52);
1969 for (i
=0; i
<max_virt_disks
; i
++)
1970 memset(&vd
->entries
[i
], 0xff, sizeof(struct virtual_entry
));
1973 ddf_set_updates_pending(ddf
);
1977 static int chunk_to_shift(int chunksize
)
1979 return ffs(chunksize
/512)-1;
1982 static int level_to_prl(int level
)
1985 case LEVEL_LINEAR
: return DDF_CONCAT
;
1986 case 0: return DDF_RAID0
;
1987 case 1: return DDF_RAID1
;
1988 case 4: return DDF_RAID4
;
1989 case 5: return DDF_RAID5
;
1990 case 6: return DDF_RAID6
;
1995 static int layout_to_rlq(int level
, int layout
, int raiddisks
)
1999 return DDF_RAID0_SIMPLE
;
2002 case 2: return DDF_RAID1_SIMPLE
;
2003 case 3: return DDF_RAID1_MULTI
;
2008 case 0: return DDF_RAID4_N
;
2013 case ALGORITHM_LEFT_ASYMMETRIC
:
2014 return DDF_RAID5_N_RESTART
;
2015 case ALGORITHM_RIGHT_ASYMMETRIC
:
2016 return DDF_RAID5_0_RESTART
;
2017 case ALGORITHM_LEFT_SYMMETRIC
:
2018 return DDF_RAID5_N_CONTINUE
;
2019 case ALGORITHM_RIGHT_SYMMETRIC
:
2020 return -1; /* not mentioned in standard */
2024 case ALGORITHM_ROTATING_N_RESTART
:
2025 return DDF_RAID5_N_RESTART
;
2026 case ALGORITHM_ROTATING_ZERO_RESTART
:
2027 return DDF_RAID6_0_RESTART
;
2028 case ALGORITHM_ROTATING_N_CONTINUE
:
2029 return DDF_RAID5_N_CONTINUE
;
2035 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
)
2039 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
2041 return 0; /* hopefully rlq == SIMPLE or MULTI depending
2049 return -1; /* FIXME this isn't checked */
2053 case DDF_RAID5_N_RESTART
:
2054 return ALGORITHM_LEFT_ASYMMETRIC
;
2055 case DDF_RAID5_0_RESTART
:
2056 return ALGORITHM_RIGHT_ASYMMETRIC
;
2057 case DDF_RAID5_N_CONTINUE
:
2058 return ALGORITHM_LEFT_SYMMETRIC
;
2064 case DDF_RAID5_N_RESTART
:
2065 return ALGORITHM_ROTATING_N_RESTART
;
2066 case DDF_RAID6_0_RESTART
:
2067 return ALGORITHM_ROTATING_ZERO_RESTART
;
2068 case DDF_RAID5_N_CONTINUE
:
2069 return ALGORITHM_ROTATING_N_CONTINUE
;
2079 unsigned long long start
, size
;
2081 static int cmp_extent(const void *av
, const void *bv
)
2083 const struct extent
*a
= av
;
2084 const struct extent
*b
= bv
;
2085 if (a
->start
< b
->start
)
2087 if (a
->start
> b
->start
)
2092 static struct extent
*get_extents(struct ddf_super
*ddf
, struct dl
*dl
)
2094 /* find a list of used extents on the give physical device
2095 * (dnum) of the given ddf.
2096 * Return a malloced array of 'struct extent'
2098 * FIXME ignore DDF_Legacy devices?
2105 rv
= xmalloc(sizeof(struct extent
) * (ddf
->max_part
+ 2));
2107 for (i
= 0; i
< ddf
->max_part
; i
++) {
2108 struct vcl
*v
= dl
->vlist
[i
];
2111 for (j
= 0; j
< v
->conf
.prim_elmnt_count
; j
++)
2112 if (v
->conf
.phys_refnum
[j
] == dl
->disk
.refnum
) {
2113 /* This device plays role 'j' in 'v'. */
2114 rv
[n
].start
= __be64_to_cpu(v
->lba_offset
[j
]);
2115 rv
[n
].size
= __be64_to_cpu(v
->conf
.blocks
);
2120 qsort(rv
, n
, sizeof(*rv
), cmp_extent
);
2122 rv
[n
].start
= __be64_to_cpu(ddf
->phys
->entries
[dl
->pdnum
].config_size
);
2128 static int init_super_ddf_bvd(struct supertype
*st
,
2129 mdu_array_info_t
*info
,
2130 unsigned long long size
,
2131 char *name
, char *homehost
,
2132 int *uuid
, unsigned long long data_offset
)
2134 /* We are creating a BVD inside a pre-existing container.
2135 * so st->sb is already set.
2136 * We need to create a new vd_config and a new virtual_entry
2138 struct ddf_super
*ddf
= st
->sb
;
2140 struct virtual_entry
*ve
;
2142 struct vd_config
*vc
;
2144 if (__be16_to_cpu(ddf
->virt
->populated_vdes
)
2145 >= __be16_to_cpu(ddf
->virt
->max_vdes
)) {
2146 pr_err("This ddf already has the "
2147 "maximum of %d virtual devices\n",
2148 __be16_to_cpu(ddf
->virt
->max_vdes
));
2153 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
2154 if (!all_ff(ddf
->virt
->entries
[venum
].guid
)) {
2155 char *n
= ddf
->virt
->entries
[venum
].name
;
2157 if (strncmp(name
, n
, 16) == 0) {
2158 pr_err("This ddf already"
2159 " has an array called %s\n",
2165 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
2166 if (all_ff(ddf
->virt
->entries
[venum
].guid
))
2168 if (venum
== __be16_to_cpu(ddf
->virt
->max_vdes
)) {
2169 pr_err("Cannot find spare slot for "
2170 "virtual disk - DDF is corrupt\n");
2173 ve
= &ddf
->virt
->entries
[venum
];
2175 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
2176 * timestamp, random number
2178 make_header_guid(ve
->guid
);
2179 ve
->unit
= __cpu_to_be16(info
->md_minor
);
2181 ve
->guid_crc
= crc32(0, (unsigned char*)ddf
->anchor
.guid
, DDF_GUID_LEN
);
2183 ve
->state
= DDF_state_degraded
; /* Will be modified as devices are added */
2184 if (info
->state
& 1) /* clean */
2185 ve
->init_state
= DDF_init_full
;
2187 ve
->init_state
= DDF_init_not
;
2189 memset(ve
->pad1
, 0xff, 14);
2190 memset(ve
->name
, ' ', 16);
2192 strncpy(ve
->name
, name
, 16);
2193 ddf
->virt
->populated_vdes
=
2194 __cpu_to_be16(__be16_to_cpu(ddf
->virt
->populated_vdes
)+1);
2196 /* Now create a new vd_config */
2197 if (posix_memalign((void**)&vcl
, 512,
2198 (offsetof(struct vcl
, conf
) + ddf
->conf_rec_len
* 512)) != 0) {
2199 pr_err("%s could not allocate vd_config\n", __func__
);
2202 vcl
->lba_offset
= (__u64
*) &vcl
->conf
.phys_refnum
[ddf
->mppe
];
2204 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
2205 vcl
->other_bvds
= NULL
;
2209 vc
->magic
= DDF_VD_CONF_MAGIC
;
2210 memcpy(vc
->guid
, ve
->guid
, DDF_GUID_LEN
);
2211 vc
->timestamp
= __cpu_to_be32(time(0)-DECADE
);
2212 vc
->seqnum
= __cpu_to_be32(1);
2213 memset(vc
->pad0
, 0xff, 24);
2214 vc
->prim_elmnt_count
= __cpu_to_be16(info
->raid_disks
);
2215 vc
->chunk_shift
= chunk_to_shift(info
->chunk_size
);
2216 vc
->prl
= level_to_prl(info
->level
);
2217 vc
->rlq
= layout_to_rlq(info
->level
, info
->layout
, info
->raid_disks
);
2218 vc
->sec_elmnt_count
= 1;
2219 vc
->sec_elmnt_seq
= 0;
2221 vc
->blocks
= __cpu_to_be64(info
->size
* 2);
2222 vc
->array_blocks
= __cpu_to_be64(
2223 calc_array_size(info
->level
, info
->raid_disks
, info
->layout
,
2224 info
->chunk_size
, info
->size
*2));
2225 memset(vc
->pad1
, 0xff, 8);
2226 vc
->spare_refs
[0] = 0xffffffff;
2227 vc
->spare_refs
[1] = 0xffffffff;
2228 vc
->spare_refs
[2] = 0xffffffff;
2229 vc
->spare_refs
[3] = 0xffffffff;
2230 vc
->spare_refs
[4] = 0xffffffff;
2231 vc
->spare_refs
[5] = 0xffffffff;
2232 vc
->spare_refs
[6] = 0xffffffff;
2233 vc
->spare_refs
[7] = 0xffffffff;
2234 memset(vc
->cache_pol
, 0, 8);
2236 memset(vc
->pad2
, 0xff, 3);
2237 memset(vc
->pad3
, 0xff, 52);
2238 memset(vc
->pad4
, 0xff, 192);
2239 memset(vc
->v0
, 0xff, 32);
2240 memset(vc
->v1
, 0xff, 32);
2241 memset(vc
->v2
, 0xff, 16);
2242 memset(vc
->v3
, 0xff, 16);
2243 memset(vc
->vendor
, 0xff, 32);
2245 memset(vc
->phys_refnum
, 0xff, 4*ddf
->mppe
);
2246 memset(vc
->phys_refnum
+ddf
->mppe
, 0x00, 8*ddf
->mppe
);
2248 vcl
->next
= ddf
->conflist
;
2249 ddf
->conflist
= vcl
;
2250 ddf
->currentconf
= vcl
;
2251 ddf_set_updates_pending(ddf
);
2256 static void add_to_super_ddf_bvd(struct supertype
*st
,
2257 mdu_disk_info_t
*dk
, int fd
, char *devname
)
2259 /* fd and devname identify a device with-in the ddf container (st).
2260 * dk identifies a location in the new BVD.
2261 * We need to find suitable free space in that device and update
2262 * the phys_refnum and lba_offset for the newly created vd_config.
2263 * We might also want to update the type in the phys_disk
2266 * Alternately: fd == -1 and we have already chosen which device to
2267 * use and recorded in dlist->raid_disk;
2270 struct ddf_super
*ddf
= st
->sb
;
2271 struct vd_config
*vc
;
2273 unsigned int working
;
2275 unsigned long long blocks
, pos
, esize
;
2279 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2280 if (dl
->raiddisk
== dk
->raid_disk
)
2283 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2284 if (dl
->major
== dk
->major
&&
2285 dl
->minor
== dk
->minor
)
2288 if (!dl
|| ! (dk
->state
& (1<<MD_DISK_SYNC
)))
2291 vc
= &ddf
->currentconf
->conf
;
2292 lba_offset
= ddf
->currentconf
->lba_offset
;
2294 ex
= get_extents(ddf
, dl
);
2299 blocks
= __be64_to_cpu(vc
->blocks
);
2300 if (ddf
->currentconf
->block_sizes
)
2301 blocks
= ddf
->currentconf
->block_sizes
[dk
->raid_disk
];
2304 esize
= ex
[i
].start
- pos
;
2305 if (esize
>= blocks
)
2307 pos
= ex
[i
].start
+ ex
[i
].size
;
2309 } while (ex
[i
-1].size
);
2315 ddf
->currentdev
= dk
->raid_disk
;
2316 vc
->phys_refnum
[dk
->raid_disk
] = dl
->disk
.refnum
;
2317 lba_offset
[dk
->raid_disk
] = __cpu_to_be64(pos
);
2319 for (i
= 0; i
< ddf
->max_part
; i
++)
2320 if (dl
->vlist
[i
] == NULL
)
2322 if (i
== ddf
->max_part
)
2324 dl
->vlist
[i
] = ddf
->currentconf
;
2329 dl
->devname
= devname
;
2331 /* Check how many working raid_disks, and if we can mark
2332 * array as optimal yet
2336 for (i
= 0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++)
2337 if (vc
->phys_refnum
[i
] != 0xffffffff)
2340 /* Find which virtual_entry */
2341 i
= ddf
->currentconf
->vcnum
;
2342 if (working
== __be16_to_cpu(vc
->prim_elmnt_count
))
2343 ddf
->virt
->entries
[i
].state
=
2344 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2345 | DDF_state_optimal
;
2347 if (vc
->prl
== DDF_RAID6
&&
2348 working
+1 == __be16_to_cpu(vc
->prim_elmnt_count
))
2349 ddf
->virt
->entries
[i
].state
=
2350 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2351 | DDF_state_part_optimal
;
2353 ddf
->phys
->entries
[dl
->pdnum
].type
&= ~__cpu_to_be16(DDF_Global_Spare
);
2354 ddf
->phys
->entries
[dl
->pdnum
].type
|= __cpu_to_be16(DDF_Active_in_VD
);
2355 ddf_set_updates_pending(ddf
);
2358 /* add a device to a container, either while creating it or while
2359 * expanding a pre-existing container
2361 static int add_to_super_ddf(struct supertype
*st
,
2362 mdu_disk_info_t
*dk
, int fd
, char *devname
,
2363 unsigned long long data_offset
)
2365 struct ddf_super
*ddf
= st
->sb
;
2369 unsigned long long size
;
2370 struct phys_disk_entry
*pde
;
2375 if (ddf
->currentconf
) {
2376 add_to_super_ddf_bvd(st
, dk
, fd
, devname
);
2380 /* This is device numbered dk->number. We need to create
2381 * a phys_disk entry and a more detailed disk_data entry.
2384 if (posix_memalign((void**)&dd
, 512,
2385 sizeof(*dd
) + sizeof(dd
->vlist
[0]) * ddf
->max_part
) != 0) {
2386 pr_err("%s could allocate buffer for new disk, aborting\n",
2390 dd
->major
= major(stb
.st_rdev
);
2391 dd
->minor
= minor(stb
.st_rdev
);
2392 dd
->devname
= devname
;
2396 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
2398 tm
= localtime(&now
);
2399 sprintf(dd
->disk
.guid
, "%8s%04d%02d%02d",
2400 T10
, tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
2401 tptr
= (__u32
*)(dd
->disk
.guid
+ 16);
2402 *tptr
++ = random32();
2406 /* Cannot be bothered finding a CRC of some irrelevant details*/
2407 dd
->disk
.refnum
= random32();
2408 for (i
= __be16_to_cpu(ddf
->active
->max_pd_entries
);
2410 if (ddf
->phys
->entries
[i
-1].refnum
== dd
->disk
.refnum
)
2414 dd
->disk
.forced_ref
= 1;
2415 dd
->disk
.forced_guid
= 1;
2416 memset(dd
->disk
.vendor
, ' ', 32);
2417 memcpy(dd
->disk
.vendor
, "Linux", 5);
2418 memset(dd
->disk
.pad
, 0xff, 442);
2419 for (i
= 0; i
< ddf
->max_part
; i
++)
2420 dd
->vlist
[i
] = NULL
;
2422 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
2423 pde
= &ddf
->phys
->entries
[n
];
2426 if (st
->update_tail
) {
2427 int len
= (sizeof(struct phys_disk
) +
2428 sizeof(struct phys_disk_entry
));
2429 struct phys_disk
*pd
;
2432 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2433 pd
->used_pdes
= __cpu_to_be16(n
);
2434 pde
= &pd
->entries
[0];
2438 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
2441 memcpy(pde
->guid
, dd
->disk
.guid
, DDF_GUID_LEN
);
2442 pde
->refnum
= dd
->disk
.refnum
;
2443 pde
->type
= __cpu_to_be16(DDF_Forced_PD_GUID
| DDF_Global_Spare
);
2444 pde
->state
= __cpu_to_be16(DDF_Online
);
2445 get_dev_size(fd
, NULL
, &size
);
2446 /* We are required to reserve 32Meg, and record the size in sectors */
2447 pde
->config_size
= __cpu_to_be64( (size
- 32*1024*1024) / 512);
2448 sprintf(pde
->path
, "%17.17s","Information: nil") ;
2449 memset(pde
->pad
, 0xff, 6);
2451 dd
->size
= size
>> 9;
2452 if (st
->update_tail
) {
2453 dd
->next
= ddf
->add_list
;
2456 dd
->next
= ddf
->dlist
;
2458 ddf_set_updates_pending(ddf
);
2464 static int remove_from_super_ddf(struct supertype
*st
, mdu_disk_info_t
*dk
)
2466 struct ddf_super
*ddf
= st
->sb
;
2469 /* mdmon has noticed that this disk (dk->major/dk->minor) has
2470 * disappeared from the container.
2471 * We need to arrange that it disappears from the metadata and
2472 * internal data structures too.
2473 * Most of the work is done by ddf_process_update which edits
2474 * the metadata and closes the file handle and attaches the memory
2475 * where free_updates will free it.
2477 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2478 if (dl
->major
== dk
->major
&&
2479 dl
->minor
== dk
->minor
)
2484 if (st
->update_tail
) {
2485 int len
= (sizeof(struct phys_disk
) +
2486 sizeof(struct phys_disk_entry
));
2487 struct phys_disk
*pd
;
2490 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2491 pd
->used_pdes
= __cpu_to_be16(dl
->pdnum
);
2492 pd
->entries
[0].state
= __cpu_to_be16(DDF_Missing
);
2493 append_metadata_update(st
, pd
, len
);
2499 * This is the write_init_super method for a ddf container. It is
2500 * called when creating a container or adding another device to a
2503 #define NULL_CONF_SZ 4096
2505 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
2506 __u32 refnum
, unsigned int nmax
,
2507 const struct vd_config
**bvd
,
2510 static int __write_ddf_structure(struct dl
*d
, struct ddf_super
*ddf
, __u8 type
,
2513 unsigned long long sector
;
2514 struct ddf_header
*header
;
2515 int fd
, i
, n_config
, conf_size
;
2520 case DDF_HEADER_PRIMARY
:
2521 header
= &ddf
->primary
;
2522 sector
= __be64_to_cpu(header
->primary_lba
);
2524 case DDF_HEADER_SECONDARY
:
2525 header
= &ddf
->secondary
;
2526 sector
= __be64_to_cpu(header
->secondary_lba
);
2532 header
->type
= type
;
2533 header
->openflag
= 0;
2534 header
->crc
= calc_crc(header
, 512);
2536 lseek64(fd
, sector
<<9, 0);
2537 if (write(fd
, header
, 512) < 0)
2540 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
2541 if (write(fd
, &ddf
->controller
, 512) < 0)
2544 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
2545 if (write(fd
, ddf
->phys
, ddf
->pdsize
) < 0)
2547 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
2548 if (write(fd
, ddf
->virt
, ddf
->vdsize
) < 0)
2551 /* Now write lots of config records. */
2552 n_config
= ddf
->max_part
;
2553 conf_size
= ddf
->conf_rec_len
* 512;
2554 for (i
= 0 ; i
<= n_config
; i
++) {
2556 struct vd_config
*vdc
= NULL
;
2557 if (i
== n_config
) {
2558 c
= (struct vcl
*)d
->spare
;
2565 get_pd_index_from_refnum(
2568 (const struct vd_config
**)&vdc
,
2572 vdc
->seqnum
= header
->seq
;
2573 vdc
->crc
= calc_crc(vdc
, conf_size
);
2574 if (write(fd
, vdc
, conf_size
) < 0)
2577 unsigned int togo
= conf_size
;
2578 while (togo
> NULL_CONF_SZ
) {
2579 if (write(fd
, null_aligned
, NULL_CONF_SZ
) < 0)
2581 togo
-= NULL_CONF_SZ
;
2583 if (write(fd
, null_aligned
, togo
) < 0)
2590 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
2591 if (write(fd
, &d
->disk
, 512) < 0)
2597 static int __write_init_super_ddf(struct supertype
*st
)
2599 struct ddf_super
*ddf
= st
->sb
;
2603 unsigned long long size
;
2607 pr_state(ddf
, __func__
);
2608 if (posix_memalign((void**)&null_aligned
, 4096, NULL_CONF_SZ
) != 0) {
2611 memset(null_aligned
, 0xff, NULL_CONF_SZ
);
2613 seq
= ddf
->active
->seq
+ 1;
2615 /* try to write updated metadata,
2616 * if we catch a failure move on to the next disk
2618 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
2625 /* We need to fill in the primary, (secondary) and workspace
2626 * lba's in the headers, set their checksums,
2627 * Also checksum phys, virt....
2629 * Then write everything out, finally the anchor is written.
2631 get_dev_size(fd
, NULL
, &size
);
2633 if (d
->workspace_lba
!= 0)
2634 ddf
->anchor
.workspace_lba
= d
->workspace_lba
;
2636 ddf
->anchor
.workspace_lba
=
2637 __cpu_to_be64(size
- 32*1024*2);
2638 if (d
->primary_lba
!= 0)
2639 ddf
->anchor
.primary_lba
= d
->primary_lba
;
2641 ddf
->anchor
.primary_lba
=
2642 __cpu_to_be64(size
- 16*1024*2);
2643 if (d
->secondary_lba
!= 0)
2644 ddf
->anchor
.secondary_lba
= d
->secondary_lba
;
2646 ddf
->anchor
.secondary_lba
=
2647 __cpu_to_be64(size
- 32*1024*2);
2648 ddf
->anchor
.seq
= seq
;
2649 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
2650 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
2652 ddf
->anchor
.openflag
= 0xFF; /* 'open' means nothing */
2653 ddf
->anchor
.seq
= 0xFFFFFFFF; /* no sequencing in anchor */
2654 ddf
->anchor
.crc
= calc_crc(&ddf
->anchor
, 512);
2656 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_PRIMARY
,
2660 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_SECONDARY
,
2664 lseek64(fd
, (size
-1)*512, SEEK_SET
);
2665 if (write(fd
, &ddf
->anchor
, 512) < 0)
2671 return attempts
!= successes
;
2674 static int write_init_super_ddf(struct supertype
*st
)
2676 struct ddf_super
*ddf
= st
->sb
;
2677 struct vcl
*currentconf
= ddf
->currentconf
;
2679 /* we are done with currentconf reset it to point st at the container */
2680 ddf
->currentconf
= NULL
;
2682 if (st
->update_tail
) {
2683 /* queue the virtual_disk and vd_config as metadata updates */
2684 struct virtual_disk
*vd
;
2685 struct vd_config
*vc
;
2689 int len
= (sizeof(struct phys_disk
) +
2690 sizeof(struct phys_disk_entry
));
2692 /* adding a disk to the container. */
2696 append_metadata_update(st
, ddf
->add_list
->mdupdate
, len
);
2697 ddf
->add_list
->mdupdate
= NULL
;
2701 /* Newly created VD */
2703 /* First the virtual disk. We have a slightly fake header */
2704 len
= sizeof(struct virtual_disk
) + sizeof(struct virtual_entry
);
2707 vd
->entries
[0] = ddf
->virt
->entries
[currentconf
->vcnum
];
2708 vd
->populated_vdes
= __cpu_to_be16(currentconf
->vcnum
);
2709 append_metadata_update(st
, vd
, len
);
2711 /* Then the vd_config */
2712 len
= ddf
->conf_rec_len
* 512;
2714 memcpy(vc
, ¤tconf
->conf
, len
);
2715 append_metadata_update(st
, vc
, len
);
2717 /* FIXME I need to close the fds! */
2721 for (d
= ddf
->dlist
; d
; d
=d
->next
)
2722 while (Kill(d
->devname
, NULL
, 0, -1, 1) == 0);
2723 return __write_init_super_ddf(st
);
2729 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
,
2730 unsigned long long data_offset
)
2732 /* We must reserve the last 32Meg */
2733 if (devsize
<= 32*1024*2)
2735 return devsize
- 32*1024*2;
2740 static int reserve_space(struct supertype
*st
, int raiddisks
,
2741 unsigned long long size
, int chunk
,
2742 unsigned long long *freesize
)
2744 /* Find 'raiddisks' spare extents at least 'size' big (but
2745 * only caring about multiples of 'chunk') and remember
2747 * If the cannot be found, fail.
2750 struct ddf_super
*ddf
= st
->sb
;
2753 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2757 /* Now find largest extent on each device */
2758 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2759 struct extent
*e
= get_extents(ddf
, dl
);
2760 unsigned long long pos
= 0;
2763 unsigned long long minsize
= size
;
2771 unsigned long long esize
;
2772 esize
= e
[i
].start
- pos
;
2773 if (esize
>= minsize
) {
2777 pos
= e
[i
].start
+ e
[i
].size
;
2779 } while (e
[i
-1].size
);
2782 dl
->esize
= minsize
;
2786 if (cnt
< raiddisks
) {
2787 pr_err("not enough devices with space to create array.\n");
2788 return 0; /* No enough free spaces large enough */
2791 /* choose the largest size of which there are at least 'raiddisk' */
2792 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2794 if (dl
->esize
<= size
)
2796 /* This is bigger than 'size', see if there are enough */
2798 for (dl2
= ddf
->dlist
; dl2
; dl2
=dl2
->next
)
2799 if (dl2
->esize
>= dl
->esize
)
2801 if (cnt
>= raiddisks
)
2805 size
= size
/ chunk
;
2810 pr_err("not enough spare devices to create array.\n");
2814 /* We have a 'size' of which there are enough spaces.
2815 * We simply do a first-fit */
2817 for (dl
= ddf
->dlist
; dl
&& cnt
< raiddisks
; dl
=dl
->next
) {
2818 if (dl
->esize
< size
)
2828 validate_geometry_ddf_container(struct supertype
*st
,
2829 int level
, int layout
, int raiddisks
,
2830 int chunk
, unsigned long long size
,
2831 unsigned long long data_offset
,
2832 char *dev
, unsigned long long *freesize
,
2835 static int validate_geometry_ddf_bvd(struct supertype
*st
,
2836 int level
, int layout
, int raiddisks
,
2837 int *chunk
, unsigned long long size
,
2838 unsigned long long data_offset
,
2839 char *dev
, unsigned long long *freesize
,
2842 static int validate_geometry_ddf(struct supertype
*st
,
2843 int level
, int layout
, int raiddisks
,
2844 int *chunk
, unsigned long long size
,
2845 unsigned long long data_offset
,
2846 char *dev
, unsigned long long *freesize
,
2853 /* ddf potentially supports lots of things, but it depends on
2854 * what devices are offered (and maybe kernel version?)
2855 * If given unused devices, we will make a container.
2856 * If given devices in a container, we will make a BVD.
2857 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2860 if (chunk
&& *chunk
== UnSet
)
2861 *chunk
= DEFAULT_CHUNK
;
2863 if (level
== -1000000) level
= LEVEL_CONTAINER
;
2864 if (level
== LEVEL_CONTAINER
) {
2865 /* Must be a fresh device to add to a container */
2866 return validate_geometry_ddf_container(st
, level
, layout
,
2867 raiddisks
, chunk
?*chunk
:0,
2868 size
, data_offset
, dev
,
2874 /* Initial sanity check. Exclude illegal levels. */
2876 for (i
=0; ddf_level_num
[i
].num1
!= MAXINT
; i
++)
2877 if (ddf_level_num
[i
].num2
== level
)
2879 if (ddf_level_num
[i
].num1
== MAXINT
) {
2881 pr_err("DDF does not support level %d arrays\n",
2885 /* Should check layout? etc */
2887 if (st
->sb
&& freesize
) {
2888 /* --create was given a container to create in.
2889 * So we need to check that there are enough
2890 * free spaces and return the amount of space.
2891 * We may as well remember which drives were
2892 * chosen so that add_to_super/getinfo_super
2895 return reserve_space(st
, raiddisks
, size
, chunk
?*chunk
:0, freesize
);
2901 /* A container has already been opened, so we are
2902 * creating in there. Maybe a BVD, maybe an SVD.
2903 * Should make a distinction one day.
2905 return validate_geometry_ddf_bvd(st
, level
, layout
, raiddisks
,
2906 chunk
, size
, data_offset
, dev
,
2910 /* This is the first device for the array.
2911 * If it is a container, we read it in and do automagic allocations,
2912 * no other devices should be given.
2913 * Otherwise it must be a member device of a container, and we
2914 * do manual allocation.
2915 * Later we should check for a BVD and make an SVD.
2917 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2919 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
2921 if (sra
&& sra
->array
.major_version
== -1 &&
2922 strcmp(sra
->text_version
, "ddf") == 0) {
2925 /* find space for 'n' devices. */
2926 /* remember the devices */
2927 /* Somehow return the fact that we have enough */
2931 pr_err("ddf: Cannot create this array "
2932 "on device %s - a container is required.\n",
2936 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
2938 pr_err("ddf: Cannot open %s: %s\n",
2939 dev
, strerror(errno
));
2942 /* Well, it is in use by someone, maybe a 'ddf' container. */
2943 cfd
= open_container(fd
);
2947 pr_err("ddf: Cannot use %s: %s\n",
2948 dev
, strerror(EBUSY
));
2951 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
2953 if (sra
&& sra
->array
.major_version
== -1 &&
2954 strcmp(sra
->text_version
, "ddf") == 0) {
2955 /* This is a member of a ddf container. Load the container
2956 * and try to create a bvd
2958 struct ddf_super
*ddf
;
2959 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
) == 0) {
2961 strcpy(st
->container_devnm
, fd2devnm(cfd
));
2963 return validate_geometry_ddf_bvd(st
, level
, layout
,
2964 raiddisks
, chunk
, size
,
2970 } else /* device may belong to a different container */
2977 validate_geometry_ddf_container(struct supertype
*st
,
2978 int level
, int layout
, int raiddisks
,
2979 int chunk
, unsigned long long size
,
2980 unsigned long long data_offset
,
2981 char *dev
, unsigned long long *freesize
,
2985 unsigned long long ldsize
;
2987 if (level
!= LEVEL_CONTAINER
)
2992 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2995 pr_err("ddf: Cannot open %s: %s\n",
2996 dev
, strerror(errno
));
2999 if (!get_dev_size(fd
, dev
, &ldsize
)) {
3005 *freesize
= avail_size_ddf(st
, ldsize
>> 9, INVALID_SECTORS
);
3012 static int validate_geometry_ddf_bvd(struct supertype
*st
,
3013 int level
, int layout
, int raiddisks
,
3014 int *chunk
, unsigned long long size
,
3015 unsigned long long data_offset
,
3016 char *dev
, unsigned long long *freesize
,
3020 struct ddf_super
*ddf
= st
->sb
;
3022 unsigned long long pos
= 0;
3023 unsigned long long maxsize
;
3026 /* ddf/bvd supports lots of things, but not containers */
3027 if (level
== LEVEL_CONTAINER
) {
3029 pr_err("DDF cannot create a container within an container\n");
3032 /* We must have the container info already read in. */
3037 /* General test: make sure there is space for
3038 * 'raiddisks' device extents of size 'size'.
3040 unsigned long long minsize
= size
;
3044 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
3050 e
= get_extents(ddf
, dl
);
3053 unsigned long long esize
;
3054 esize
= e
[i
].start
- pos
;
3055 if (esize
>= minsize
)
3057 pos
= e
[i
].start
+ e
[i
].size
;
3059 } while (e
[i
-1].size
);
3064 if (dcnt
< raiddisks
) {
3066 pr_err("ddf: Not enough devices with "
3067 "space for this array (%d < %d)\n",
3073 /* This device must be a member of the set */
3074 if (stat(dev
, &stb
) < 0)
3076 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
3078 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
3079 if (dl
->major
== (int)major(stb
.st_rdev
) &&
3080 dl
->minor
== (int)minor(stb
.st_rdev
))
3085 pr_err("ddf: %s is not in the "
3090 e
= get_extents(ddf
, dl
);
3094 unsigned long long esize
;
3095 esize
= e
[i
].start
- pos
;
3096 if (esize
>= maxsize
)
3098 pos
= e
[i
].start
+ e
[i
].size
;
3100 } while (e
[i
-1].size
);
3101 *freesize
= maxsize
;
3107 static int load_super_ddf_all(struct supertype
*st
, int fd
,
3108 void **sbp
, char *devname
)
3111 struct ddf_super
*super
;
3112 struct mdinfo
*sd
, *best
= NULL
;
3118 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3121 if (sra
->array
.major_version
!= -1 ||
3122 sra
->array
.minor_version
!= -2 ||
3123 strcmp(sra
->text_version
, "ddf") != 0)
3126 if (posix_memalign((void**)&super
, 512, sizeof(*super
)) != 0)
3128 memset(super
, 0, sizeof(*super
));
3130 /* first, try each device, and choose the best ddf */
3131 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3133 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3134 dfd
= dev_open(nm
, O_RDONLY
);
3137 rv
= load_ddf_headers(dfd
, super
, NULL
);
3140 seq
= __be32_to_cpu(super
->active
->seq
);
3141 if (super
->active
->openflag
)
3143 if (!best
|| seq
> bestseq
) {
3151 /* OK, load this ddf */
3152 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
3153 dfd
= dev_open(nm
, O_RDONLY
);
3156 load_ddf_headers(dfd
, super
, NULL
);
3157 load_ddf_global(dfd
, super
, NULL
);
3159 /* Now we need the device-local bits */
3160 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3163 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3164 dfd
= dev_open(nm
, O_RDWR
);
3167 rv
= load_ddf_headers(dfd
, super
, NULL
);
3169 rv
= load_ddf_local(dfd
, super
, NULL
, 1);
3175 if (st
->ss
== NULL
) {
3176 st
->ss
= &super_ddf
;
3177 st
->minor_version
= 0;
3180 strcpy(st
->container_devnm
, fd2devnm(fd
));
3184 static int load_container_ddf(struct supertype
*st
, int fd
,
3187 return load_super_ddf_all(st
, fd
, &st
->sb
, devname
);
3190 #endif /* MDASSEMBLE */
3192 static int check_secondary(const struct vcl
*vc
)
3194 const struct vd_config
*conf
= &vc
->conf
;
3197 /* The only DDF secondary RAID level md can support is
3198 * RAID 10, if the stripe sizes and Basic volume sizes
3200 * Other configurations could in theory be supported by exposing
3201 * the BVDs to user space and using device mapper for the secondary
3202 * mapping. So far we don't support that.
3205 __u64 sec_elements
[4] = {0, 0, 0, 0};
3206 #define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3207 #define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3209 if (vc
->other_bvds
== NULL
) {
3210 pr_err("No BVDs for secondary RAID found\n");
3213 if (conf
->prl
!= DDF_RAID1
) {
3214 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3217 if (conf
->srl
!= DDF_2STRIPED
&& conf
->srl
!= DDF_2SPANNED
) {
3218 pr_err("Secondary RAID level %d is unsupported\n",
3222 __set_sec_seen(conf
->sec_elmnt_seq
);
3223 for (i
= 0; i
< conf
->sec_elmnt_count
-1; i
++) {
3224 const struct vd_config
*bvd
= vc
->other_bvds
[i
];
3226 pr_err("BVD %d is missing\n", i
+1);
3229 if (bvd
->srl
!= conf
->srl
) {
3230 pr_err("Inconsistent secondary RAID level across BVDs\n");
3233 if (bvd
->prl
!= conf
->prl
) {
3234 pr_err("Different RAID levels for BVDs are unsupported\n");
3237 if (bvd
->prim_elmnt_count
!= conf
->prim_elmnt_count
) {
3238 pr_err("All BVDs must have the same number of primary elements\n");
3241 if (bvd
->chunk_shift
!= conf
->chunk_shift
) {
3242 pr_err("Different strip sizes for BVDs are unsupported\n");
3245 if (bvd
->array_blocks
!= conf
->array_blocks
) {
3246 pr_err("Different BVD sizes are unsupported\n");
3249 __set_sec_seen(bvd
->sec_elmnt_seq
);
3251 for (i
= 0; i
< conf
->sec_elmnt_count
; i
++) {
3252 if (!__was_sec_seen(i
)) {
3253 pr_err("BVD %d is missing\n", i
);
3260 #define NO_SUCH_REFNUM (0xFFFFFFFF)
3261 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
3262 __u32 refnum
, unsigned int nmax
,
3263 const struct vd_config
**bvd
,
3266 unsigned int i
, j
, n
, sec
, cnt
;
3268 cnt
= __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3269 sec
= (vc
->conf
.sec_elmnt_count
== 1 ? 0 : vc
->conf
.sec_elmnt_seq
);
3271 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3272 /* j counts valid entries for this BVD */
3273 if (vc
->conf
.phys_refnum
[i
] != 0xffffffff)
3275 if (vc
->conf
.phys_refnum
[i
] == refnum
) {
3278 return sec
* cnt
+ j
- 1;
3281 if (vc
->other_bvds
== NULL
)
3284 for (n
= 1; n
< vc
->conf
.sec_elmnt_count
; n
++) {
3285 struct vd_config
*vd
= vc
->other_bvds
[n
-1];
3288 sec
= vd
->sec_elmnt_seq
;
3289 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3290 if (vd
->phys_refnum
[i
] != 0xffffffff)
3292 if (vd
->phys_refnum
[i
] == refnum
) {
3295 return sec
* cnt
+ j
- 1;
3301 return NO_SUCH_REFNUM
;
3304 static struct mdinfo
*container_content_ddf(struct supertype
*st
, char *subarray
)
3306 /* Given a container loaded by load_super_ddf_all,
3307 * extract information about all the arrays into
3310 * For each vcl in conflist: create an mdinfo, fill it in,
3311 * then look for matching devices (phys_refnum) in dlist
3312 * and create appropriate device mdinfo.
3314 struct ddf_super
*ddf
= st
->sb
;
3315 struct mdinfo
*rest
= NULL
;
3318 for (vc
= ddf
->conflist
; vc
; vc
=vc
->next
)
3322 struct mdinfo
*this;
3328 (strtoul(subarray
, &ep
, 10) != vc
->vcnum
||
3332 if (vc
->conf
.sec_elmnt_count
> 1) {
3333 if (check_secondary(vc
) != 0)
3337 this = xcalloc(1, sizeof(*this));
3341 if (vc
->conf
.sec_elmnt_count
== 1) {
3342 this->array
.level
= map_num1(ddf_level_num
,
3344 this->array
.raid_disks
=
3345 __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3346 this->array
.layout
=
3347 rlq_to_layout(vc
->conf
.rlq
, vc
->conf
.prl
,
3348 this->array
.raid_disks
);
3350 /* The only supported layout is RAID 10.
3351 * Compatibility has been checked in check_secondary()
3354 this->array
.level
= 10;
3355 this->array
.raid_disks
=
3356 __be16_to_cpu(vc
->conf
.prim_elmnt_count
)
3357 * vc
->conf
.sec_elmnt_count
;
3358 this->array
.layout
= 0x100 |
3359 __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3361 this->array
.md_minor
= -1;
3362 this->array
.major_version
= -1;
3363 this->array
.minor_version
= -2;
3364 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
3365 this->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
3366 this->array
.utime
= DECADE
+
3367 __be32_to_cpu(vc
->conf
.timestamp
);
3368 this->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
3371 if ((ddf
->virt
->entries
[i
].state
& DDF_state_inconsistent
) ||
3372 (ddf
->virt
->entries
[i
].init_state
& DDF_initstate_mask
) !=
3374 this->array
.state
= 0;
3375 this->resync_start
= 0;
3377 this->array
.state
= 1;
3378 this->resync_start
= MaxSector
;
3380 memcpy(this->name
, ddf
->virt
->entries
[i
].name
, 16);
3383 if (this->name
[j
] == ' ')
3386 memset(this->uuid
, 0, sizeof(this->uuid
));
3387 this->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
3388 this->array
.size
= this->component_size
/ 2;
3389 this->container_member
= i
;
3391 ddf
->currentconf
= vc
;
3392 uuid_from_super_ddf(st
, this->uuid
);
3393 ddf
->currentconf
= NULL
;
3395 sprintf(this->text_version
, "/%s/%d",
3396 st
->container_devnm
, this->container_member
);
3398 for (pd
= 0; pd
< __be16_to_cpu(ddf
->phys
->used_pdes
); pd
++) {
3401 const struct vd_config
*bvd
;
3406 if (ddf
->phys
->entries
[pd
].refnum
== 0xFFFFFFFF)
3409 stt
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3410 if ((stt
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3414 i
= get_pd_index_from_refnum(
3415 vc
, ddf
->phys
->entries
[pd
].refnum
,
3416 ddf
->mppe
, &bvd
, &iphys
);
3417 if (i
== NO_SUCH_REFNUM
)
3420 this->array
.working_disks
++;
3422 for (d
= ddf
->dlist
; d
; d
=d
->next
)
3423 if (d
->disk
.refnum
==
3424 ddf
->phys
->entries
[pd
].refnum
)
3427 /* Haven't found that one yet, maybe there are others */
3430 dev
= xcalloc(1, sizeof(*dev
));
3431 dev
->next
= this->devs
;
3434 dev
->disk
.number
= __be32_to_cpu(d
->disk
.refnum
);
3435 dev
->disk
.major
= d
->major
;
3436 dev
->disk
.minor
= d
->minor
;
3437 dev
->disk
.raid_disk
= i
;
3438 dev
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
3439 dev
->recovery_start
= MaxSector
;
3441 dev
->events
= __be32_to_cpu(ddf
->primary
.seq
);
3442 lba_offset
= (__u64
*)&bvd
->phys_refnum
[ddf
->mppe
];
3443 dev
->data_offset
= __be64_to_cpu(lba_offset
[iphys
]);
3444 dev
->component_size
= __be64_to_cpu(bvd
->blocks
);
3446 strcpy(dev
->name
, d
->devname
);
3452 static int store_super_ddf(struct supertype
*st
, int fd
)
3454 struct ddf_super
*ddf
= st
->sb
;
3455 unsigned long long dsize
;
3462 /* ->dlist and ->conflist will be set for updates, currently not
3465 if (ddf
->dlist
|| ddf
->conflist
)
3468 if (!get_dev_size(fd
, NULL
, &dsize
))
3471 if (posix_memalign(&buf
, 512, 512) != 0)
3473 memset(buf
, 0, 512);
3475 lseek64(fd
, dsize
-512, 0);
3476 rc
= write(fd
, buf
, 512);
3483 static int compare_super_ddf(struct supertype
*st
, struct supertype
*tst
)
3487 * 0 same, or first was empty, and second was copied
3488 * 1 second had wrong number
3490 * 3 wrong other info
3492 struct ddf_super
*first
= st
->sb
;
3493 struct ddf_super
*second
= tst
->sb
;
3494 struct dl
*dl1
, *dl2
;
3495 struct vcl
*vl1
, *vl2
;
3496 unsigned int max_vds
, max_pds
, pd
, vd
;
3504 if (memcmp(first
->anchor
.guid
, second
->anchor
.guid
, DDF_GUID_LEN
) != 0)
3507 if (first
->anchor
.seq
!= second
->anchor
.seq
) {
3508 dprintf("%s: sequence number mismatch %u/%u\n", __func__
,
3509 __be32_to_cpu(first
->anchor
.seq
),
3510 __be32_to_cpu(second
->anchor
.seq
));
3513 if (first
->max_part
!= second
->max_part
||
3514 first
->phys
->used_pdes
!= second
->phys
->used_pdes
||
3515 first
->virt
->populated_vdes
!= second
->virt
->populated_vdes
) {
3516 dprintf("%s: PD/VD number mismatch\n", __func__
);
3520 max_pds
= __be16_to_cpu(first
->phys
->used_pdes
);
3521 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3522 for (pd
= 0; pd
< max_pds
; pd
++)
3523 if (first
->phys
->entries
[pd
].refnum
== dl2
->disk
.refnum
)
3525 if (pd
== max_pds
) {
3526 dprintf("%s: no match for disk %08x\n", __func__
,
3527 __be32_to_cpu(dl2
->disk
.refnum
));
3532 max_vds
= __be16_to_cpu(first
->active
->max_vd_entries
);
3533 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3534 if (vl2
->conf
.magic
!= DDF_VD_CONF_MAGIC
)
3536 for (vd
= 0; vd
< max_vds
; vd
++)
3537 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3538 vl2
->conf
.guid
, DDF_GUID_LEN
))
3540 if (vd
== max_vds
) {
3541 dprintf("%s: no match for VD config\n", __func__
);
3545 /* FIXME should I look at anything else? */
3548 At this point we are fairly sure that the meta data matches.
3549 But the new disk may contain additional local data.
3550 Add it to the super block.
3552 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3553 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
)
3554 if (!memcmp(vl1
->conf
.guid
, vl2
->conf
.guid
,
3558 if (vl1
->other_bvds
!= NULL
&&
3559 vl1
->conf
.sec_elmnt_seq
!=
3560 vl2
->conf
.sec_elmnt_seq
) {
3561 dprintf("%s: adding BVD %u\n", __func__
,
3562 vl2
->conf
.sec_elmnt_seq
);
3563 add_other_bvd(vl1
, &vl2
->conf
,
3564 first
->conf_rec_len
*512);
3569 if (posix_memalign((void **)&vl1
, 512,
3570 (first
->conf_rec_len
*512 +
3571 offsetof(struct vcl
, conf
))) != 0) {
3572 pr_err("%s could not allocate vcl buf\n",
3577 vl1
->next
= first
->conflist
;
3578 vl1
->block_sizes
= NULL
;
3579 if (vl2
->conf
.sec_elmnt_count
> 1) {
3580 vl1
->other_bvds
= xcalloc(vl2
->conf
.sec_elmnt_count
- 1,
3581 sizeof(struct vd_config
*));
3583 vl1
->other_bvds
= NULL
;
3584 memcpy(&vl1
->conf
, &vl2
->conf
, first
->conf_rec_len
*512);
3585 vl1
->lba_offset
= (__u64
*)
3586 &vl1
->conf
.phys_refnum
[first
->mppe
];
3587 for (vd
= 0; vd
< max_vds
; vd
++)
3588 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3589 vl1
->conf
.guid
, DDF_GUID_LEN
))
3592 dprintf("%s: added config for VD %u\n", __func__
, vl1
->vcnum
);
3593 first
->conflist
= vl1
;
3596 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3597 for (dl1
= first
->dlist
; dl1
; dl1
= dl1
->next
)
3598 if (dl1
->disk
.refnum
== dl2
->disk
.refnum
)
3603 if (posix_memalign((void **)&dl1
, 512,
3604 sizeof(*dl1
) + (first
->max_part
) * sizeof(dl1
->vlist
[0]))
3606 pr_err("%s could not allocate disk info buffer\n",
3610 memcpy(dl1
, dl2
, sizeof(*dl1
));
3611 dl1
->mdupdate
= NULL
;
3612 dl1
->next
= first
->dlist
;
3614 for (pd
= 0; pd
< max_pds
; pd
++)
3615 if (first
->phys
->entries
[pd
].refnum
== dl1
->disk
.refnum
)
3619 if (posix_memalign((void **)&dl1
->spare
, 512,
3620 first
->conf_rec_len
*512) != 0) {
3621 pr_err("%s could not allocate spare info buf\n",
3625 memcpy(dl1
->spare
, dl2
->spare
, first
->conf_rec_len
*512);
3627 for (vd
= 0 ; vd
< first
->max_part
; vd
++) {
3628 if (!dl2
->vlist
[vd
]) {
3629 dl1
->vlist
[vd
] = NULL
;
3632 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
) {
3633 if (!memcmp(vl1
->conf
.guid
,
3634 dl2
->vlist
[vd
]->conf
.guid
,
3637 dl1
->vlist
[vd
] = vl1
;
3641 dprintf("%s: added disk %d: %08x\n", __func__
, dl1
->pdnum
,
3650 * A new array 'a' has been started which claims to be instance 'inst'
3651 * within container 'c'.
3652 * We need to confirm that the array matches the metadata in 'c' so
3653 * that we don't corrupt any metadata.
3655 static int ddf_open_new(struct supertype
*c
, struct active_array
*a
, char *inst
)
3657 dprintf("ddf: open_new %s\n", inst
);
3658 a
->info
.container_member
= atoi(inst
);
3663 * The array 'a' is to be marked clean in the metadata.
3664 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
3665 * clean up to the point (in sectors). If that cannot be recorded in the
3666 * metadata, then leave it as dirty.
3668 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3669 * !global! virtual_disk.virtual_entry structure.
3671 static int ddf_set_array_state(struct active_array
*a
, int consistent
)
3673 struct ddf_super
*ddf
= a
->container
->sb
;
3674 int inst
= a
->info
.container_member
;
3675 int old
= ddf
->virt
->entries
[inst
].state
;
3676 if (consistent
== 2) {
3677 /* Should check if a recovery should be started FIXME */
3679 if (!is_resync_complete(&a
->info
))
3683 ddf
->virt
->entries
[inst
].state
&= ~DDF_state_inconsistent
;
3685 ddf
->virt
->entries
[inst
].state
|= DDF_state_inconsistent
;
3686 if (old
!= ddf
->virt
->entries
[inst
].state
)
3687 ddf_set_updates_pending(ddf
);
3689 old
= ddf
->virt
->entries
[inst
].init_state
;
3690 ddf
->virt
->entries
[inst
].init_state
&= ~DDF_initstate_mask
;
3691 if (is_resync_complete(&a
->info
))
3692 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_full
;
3693 else if (a
->info
.resync_start
== 0)
3694 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_not
;
3696 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_quick
;
3697 if (old
!= ddf
->virt
->entries
[inst
].init_state
)
3698 ddf_set_updates_pending(ddf
);
3700 dprintf("ddf mark %d %s %llu\n", inst
, consistent
?"clean":"dirty",
3701 a
->info
.resync_start
);
3705 #define container_of(ptr, type, member) ({ \
3706 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
3707 (type *)( (char *)__mptr - offsetof(type,member) );})
3709 * The state of each disk is stored in the global phys_disk structure
3710 * in phys_disk.entries[n].state.
3711 * This makes various combinations awkward.
3712 * - When a device fails in any array, it must be failed in all arrays
3713 * that include a part of this device.
3714 * - When a component is rebuilding, we cannot include it officially in the
3715 * array unless this is the only array that uses the device.
3717 * So: when transitioning:
3718 * Online -> failed, just set failed flag. monitor will propagate
3719 * spare -> online, the device might need to be added to the array.
3720 * spare -> failed, just set failed. Don't worry if in array or not.
3722 static void ddf_set_disk(struct active_array
*a
, int n
, int state
)
3724 struct ddf_super
*ddf
= a
->container
->sb
;
3725 unsigned int inst
= a
->info
.container_member
;
3726 struct vd_config
*vc
= find_vdcr(ddf
, inst
);
3727 int pd
= find_phys(ddf
, vc
->phys_refnum
[n
]);
3733 dprintf("ddf: cannot find instance %d!!\n", inst
);
3736 /* Find the matching slot in 'info'. */
3737 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
3738 if (mdi
->disk
.raid_disk
== n
)
3743 /* and find the 'dl' entry corresponding to that. */
3744 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
3745 if (mdi
->state_fd
>= 0 &&
3746 mdi
->disk
.major
== dl
->major
&&
3747 mdi
->disk
.minor
== dl
->minor
)
3752 if (pd
< 0 || pd
!= dl
->pdnum
) {
3753 /* disk doesn't currently exist or has changed.
3754 * If it is now in_sync, insert it. */
3755 if ((state
& DS_INSYNC
) && ! (state
& DS_FAULTY
)) {
3758 vc
->phys_refnum
[n
] = dl
->disk
.refnum
;
3759 vcl
= container_of(vc
, struct vcl
, conf
);
3760 vcl
->lba_offset
[n
] = mdi
->data_offset
;
3761 ddf
->phys
->entries
[pd
].type
&=
3762 ~__cpu_to_be16(DDF_Global_Spare
);
3763 ddf
->phys
->entries
[pd
].type
|=
3764 __cpu_to_be16(DDF_Active_in_VD
);
3765 ddf_set_updates_pending(ddf
);
3768 int old
= ddf
->phys
->entries
[pd
].state
;
3769 if (state
& DS_FAULTY
)
3770 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Failed
);
3771 if (state
& DS_INSYNC
) {
3772 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Online
);
3773 ddf
->phys
->entries
[pd
].state
&= __cpu_to_be16(~DDF_Rebuilding
);
3775 if (old
!= ddf
->phys
->entries
[pd
].state
)
3776 ddf_set_updates_pending(ddf
);
3779 dprintf("ddf: set_disk %d to %x\n", n
, state
);
3781 /* Now we need to check the state of the array and update
3782 * virtual_disk.entries[n].state.
3783 * It needs to be one of "optimal", "degraded", "failed".
3784 * I don't understand 'deleted' or 'missing'.
3787 for (i
=0; i
< a
->info
.array
.raid_disks
; i
++) {
3788 pd
= find_phys(ddf
, vc
->phys_refnum
[i
]);
3791 st
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3792 if ((st
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3796 state
= DDF_state_degraded
;
3797 if (working
== a
->info
.array
.raid_disks
)
3798 state
= DDF_state_optimal
;
3799 else switch(vc
->prl
) {
3803 state
= DDF_state_failed
;
3807 state
= DDF_state_failed
;
3808 else if (working
== 2 && state
== DDF_state_degraded
)
3809 state
= DDF_state_part_optimal
;
3813 if (working
< a
->info
.array
.raid_disks
-1)
3814 state
= DDF_state_failed
;
3817 if (working
< a
->info
.array
.raid_disks
-2)
3818 state
= DDF_state_failed
;
3819 else if (working
== a
->info
.array
.raid_disks
-1)
3820 state
= DDF_state_part_optimal
;
3824 if (ddf
->virt
->entries
[inst
].state
!=
3825 ((ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
3828 ddf
->virt
->entries
[inst
].state
=
3829 (ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
3831 ddf_set_updates_pending(ddf
);
3836 static void ddf_sync_metadata(struct supertype
*st
)
3840 * Write all data to all devices.
3841 * Later, we might be able to track whether only local changes
3842 * have been made, or whether any global data has been changed,
3843 * but ddf is sufficiently weird that it probably always
3844 * changes global data ....
3846 struct ddf_super
*ddf
= st
->sb
;
3847 if (!ddf
->updates_pending
)
3849 ddf
->updates_pending
= 0;
3850 __write_init_super_ddf(st
);
3851 dprintf("ddf: sync_metadata\n");
3854 static void ddf_process_update(struct supertype
*st
,
3855 struct metadata_update
*update
)
3857 /* Apply this update to the metadata.
3858 * The first 4 bytes are a DDF_*_MAGIC which guides
3860 * Possible update are:
3861 * DDF_PHYS_RECORDS_MAGIC
3862 * Add a new physical device or remove an old one.
3863 * Changes to this record only happen implicitly.
3864 * used_pdes is the device number.
3865 * DDF_VIRT_RECORDS_MAGIC
3866 * Add a new VD. Possibly also change the 'access' bits.
3867 * populated_vdes is the entry number.
3869 * New or updated VD. the VIRT_RECORD must already
3870 * exist. For an update, phys_refnum and lba_offset
3871 * (at least) are updated, and the VD_CONF must
3872 * be written to precisely those devices listed with
3874 * DDF_SPARE_ASSIGN_MAGIC
3875 * replacement Spare Assignment Record... but for which device?
3878 * - to create a new array, we send a VIRT_RECORD and
3879 * a VD_CONF. Then assemble and start the array.
3880 * - to activate a spare we send a VD_CONF to add the phys_refnum
3881 * and offset. This will also mark the spare as active with
3882 * a spare-assignment record.
3884 struct ddf_super
*ddf
= st
->sb
;
3885 __u32
*magic
= (__u32
*)update
->buf
;
3886 struct phys_disk
*pd
;
3887 struct virtual_disk
*vd
;
3888 struct vd_config
*vc
;
3893 unsigned int pdnum
, pd2
;
3895 dprintf("Process update %x\n", *magic
);
3898 case DDF_PHYS_RECORDS_MAGIC
:
3900 if (update
->len
!= (sizeof(struct phys_disk
) +
3901 sizeof(struct phys_disk_entry
)))
3903 pd
= (struct phys_disk
*)update
->buf
;
3905 ent
= __be16_to_cpu(pd
->used_pdes
);
3906 if (ent
>= __be16_to_cpu(ddf
->phys
->max_pdes
))
3908 if (pd
->entries
[0].state
& __cpu_to_be16(DDF_Missing
)) {
3910 /* removing this disk. */
3911 ddf
->phys
->entries
[ent
].state
|= __cpu_to_be16(DDF_Missing
);
3912 for (dlp
= &ddf
->dlist
; *dlp
; dlp
= &(*dlp
)->next
) {
3913 struct dl
*dl
= *dlp
;
3914 if (dl
->pdnum
== (signed)ent
) {
3917 /* FIXME this doesn't free
3924 ddf_set_updates_pending(ddf
);
3927 if (!all_ff(ddf
->phys
->entries
[ent
].guid
))
3929 ddf
->phys
->entries
[ent
] = pd
->entries
[0];
3930 ddf
->phys
->used_pdes
= __cpu_to_be16(1 +
3931 __be16_to_cpu(ddf
->phys
->used_pdes
));
3932 ddf_set_updates_pending(ddf
);
3933 if (ddf
->add_list
) {
3934 struct active_array
*a
;
3935 struct dl
*al
= ddf
->add_list
;
3936 ddf
->add_list
= al
->next
;
3938 al
->next
= ddf
->dlist
;
3941 /* As a device has been added, we should check
3942 * for any degraded devices that might make
3943 * use of this spare */
3944 for (a
= st
->arrays
; a
; a
=a
->next
)
3945 a
->check_degraded
= 1;
3949 case DDF_VIRT_RECORDS_MAGIC
:
3951 if (update
->len
!= (sizeof(struct virtual_disk
) +
3952 sizeof(struct virtual_entry
)))
3954 vd
= (struct virtual_disk
*)update
->buf
;
3956 ent
= __be16_to_cpu(vd
->populated_vdes
);
3957 if (ent
>= __be16_to_cpu(ddf
->virt
->max_vdes
))
3959 if (!all_ff(ddf
->virt
->entries
[ent
].guid
))
3961 ddf
->virt
->entries
[ent
] = vd
->entries
[0];
3962 ddf
->virt
->populated_vdes
= __cpu_to_be16(1 +
3963 __be16_to_cpu(ddf
->virt
->populated_vdes
));
3964 ddf_set_updates_pending(ddf
);
3967 case DDF_VD_CONF_MAGIC
:
3968 dprintf("len %d %d\n", update
->len
, ddf
->conf_rec_len
);
3970 mppe
= __be16_to_cpu(ddf
->anchor
.max_primary_element_entries
);
3971 if ((unsigned)update
->len
!= ddf
->conf_rec_len
* 512)
3973 vc
= (struct vd_config
*)update
->buf
;
3974 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
3975 if (memcmp(vcl
->conf
.guid
, vc
->guid
, DDF_GUID_LEN
) == 0)
3977 dprintf("vcl = %p\n", vcl
);
3979 /* An update, just copy the phys_refnum and lba_offset
3982 memcpy(vcl
->conf
.phys_refnum
, vc
->phys_refnum
,
3983 mppe
* (sizeof(__u32
) + sizeof(__u64
)));
3988 vcl
= update
->space
;
3989 update
->space
= NULL
;
3990 vcl
->next
= ddf
->conflist
;
3991 memcpy(&vcl
->conf
, vc
, update
->len
);
3992 vcl
->lba_offset
= (__u64
*)
3993 &vcl
->conf
.phys_refnum
[mppe
];
3995 ent
< __be16_to_cpu(ddf
->virt
->populated_vdes
);
3997 if (memcmp(vc
->guid
, ddf
->virt
->entries
[ent
].guid
,
3998 DDF_GUID_LEN
) == 0) {
4002 ddf
->conflist
= vcl
;
4004 /* Set DDF_Transition on all Failed devices - to help
4005 * us detect those that are no longer in use
4007 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
4008 if (ddf
->phys
->entries
[pdnum
].state
4009 & __be16_to_cpu(DDF_Failed
))
4010 ddf
->phys
->entries
[pdnum
].state
4011 |= __be16_to_cpu(DDF_Transition
);
4012 /* Now make sure vlist is correct for each dl. */
4013 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
4015 unsigned int vn
= 0;
4016 int in_degraded
= 0;
4017 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
4018 for (dn
=0; dn
< ddf
->mppe
; dn
++)
4019 if (vcl
->conf
.phys_refnum
[dn
] ==
4022 dprintf("dev %d has %p at %d\n",
4023 dl
->pdnum
, vcl
, vn
);
4024 /* Clear the Transition flag */
4025 if (ddf
->phys
->entries
[dl
->pdnum
].state
4026 & __be16_to_cpu(DDF_Failed
))
4027 ddf
->phys
->entries
[dl
->pdnum
].state
&=
4028 ~__be16_to_cpu(DDF_Transition
);
4030 dl
->vlist
[vn
++] = vcl
;
4031 vstate
= ddf
->virt
->entries
[vcl
->vcnum
].state
4033 if (vstate
== DDF_state_degraded
||
4034 vstate
== DDF_state_part_optimal
)
4038 while (vn
< ddf
->max_part
)
4039 dl
->vlist
[vn
++] = NULL
;
4041 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4042 ~__cpu_to_be16(DDF_Global_Spare
);
4043 if (!(ddf
->phys
->entries
[dl
->pdnum
].type
&
4044 __cpu_to_be16(DDF_Active_in_VD
))) {
4045 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4046 __cpu_to_be16(DDF_Active_in_VD
);
4048 ddf
->phys
->entries
[dl
->pdnum
].state
|=
4049 __cpu_to_be16(DDF_Rebuilding
);
4053 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4054 ~__cpu_to_be16(DDF_Global_Spare
);
4055 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4056 __cpu_to_be16(DDF_Spare
);
4058 if (!dl
->vlist
[0] && !dl
->spare
) {
4059 ddf
->phys
->entries
[dl
->pdnum
].type
|=
4060 __cpu_to_be16(DDF_Global_Spare
);
4061 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4062 ~__cpu_to_be16(DDF_Spare
|
4067 /* Now remove any 'Failed' devices that are not part
4068 * of any VD. They will have the Transition flag set.
4069 * Once done, we need to update all dl->pdnum numbers.
4072 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
4073 if ((ddf
->phys
->entries
[pdnum
].state
4074 & __be16_to_cpu(DDF_Failed
))
4075 && (ddf
->phys
->entries
[pdnum
].state
4076 & __be16_to_cpu(DDF_Transition
)))
4077 /* skip this one */;
4078 else if (pdnum
== pd2
)
4081 ddf
->phys
->entries
[pd2
] = ddf
->phys
->entries
[pdnum
];
4082 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
4083 if (dl
->pdnum
== (int)pdnum
)
4087 ddf
->phys
->used_pdes
= __cpu_to_be16(pd2
);
4088 while (pd2
< pdnum
) {
4089 memset(ddf
->phys
->entries
[pd2
].guid
, 0xff, DDF_GUID_LEN
);
4093 ddf_set_updates_pending(ddf
);
4095 case DDF_SPARE_ASSIGN_MAGIC
:
4100 static void ddf_prepare_update(struct supertype
*st
,
4101 struct metadata_update
*update
)
4103 /* This update arrived at managemon.
4104 * We are about to pass it to monitor.
4105 * If a malloc is needed, do it here.
4107 struct ddf_super
*ddf
= st
->sb
;
4108 __u32
*magic
= (__u32
*)update
->buf
;
4109 if (*magic
== DDF_VD_CONF_MAGIC
)
4110 if (posix_memalign(&update
->space
, 512,
4111 offsetof(struct vcl
, conf
)
4112 + ddf
->conf_rec_len
* 512) != 0)
4113 update
->space
= NULL
;
4117 * Check if the array 'a' is degraded but not failed.
4118 * If it is, find as many spares as are available and needed and
4119 * arrange for their inclusion.
4120 * We only choose devices which are not already in the array,
4121 * and prefer those with a spare-assignment to this array.
4122 * otherwise we choose global spares - assuming always that
4123 * there is enough room.
4124 * For each spare that we assign, we return an 'mdinfo' which
4125 * describes the position for the device in the array.
4126 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4127 * the new phys_refnum and lba_offset values.
4129 * Only worry about BVDs at the moment.
4131 static struct mdinfo
*ddf_activate_spare(struct active_array
*a
,
4132 struct metadata_update
**updates
)
4136 struct ddf_super
*ddf
= a
->container
->sb
;
4138 struct mdinfo
*rv
= NULL
;
4140 struct metadata_update
*mu
;
4143 struct vd_config
*vc
;
4146 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
4147 if ((d
->curr_state
& DS_FAULTY
) &&
4149 /* wait for Removal to happen */
4151 if (d
->state_fd
>= 0)
4155 dprintf("ddf_activate: working=%d (%d) level=%d\n", working
, a
->info
.array
.raid_disks
,
4156 a
->info
.array
.level
);
4157 if (working
== a
->info
.array
.raid_disks
)
4158 return NULL
; /* array not degraded */
4159 switch (a
->info
.array
.level
) {
4162 return NULL
; /* failed */
4166 if (working
< a
->info
.array
.raid_disks
- 1)
4167 return NULL
; /* failed */
4170 if (working
< a
->info
.array
.raid_disks
- 2)
4171 return NULL
; /* failed */
4173 default: /* concat or stripe */
4174 return NULL
; /* failed */
4177 /* For each slot, if it is not working, find a spare */
4179 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
4180 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4181 if (d
->disk
.raid_disk
== i
)
4183 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
4184 if (d
&& (d
->state_fd
>= 0))
4187 /* OK, this device needs recovery. Find a spare */
4189 for ( ; dl
; dl
= dl
->next
) {
4190 unsigned long long esize
;
4191 unsigned long long pos
;
4194 int is_dedicated
= 0;
4197 /* If in this array, skip */
4198 for (d2
= a
->info
.devs
; d2
; d2
= d2
->next
)
4199 if (d2
->state_fd
>= 0 &&
4200 d2
->disk
.major
== dl
->major
&&
4201 d2
->disk
.minor
== dl
->minor
) {
4202 dprintf("%x:%x already in array\n", dl
->major
, dl
->minor
);
4207 if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4208 __cpu_to_be16(DDF_Spare
)) {
4209 /* Check spare assign record */
4211 if (dl
->spare
->type
& DDF_spare_dedicated
) {
4212 /* check spare_ents for guid */
4214 j
< __be16_to_cpu(dl
->spare
->populated
);
4216 if (memcmp(dl
->spare
->spare_ents
[j
].guid
,
4217 ddf
->virt
->entries
[a
->info
.container_member
].guid
,
4224 } else if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4225 __cpu_to_be16(DDF_Global_Spare
)) {
4227 } else if (!(ddf
->phys
->entries
[dl
->pdnum
].state
&
4228 __cpu_to_be16(DDF_Failed
))) {
4229 /* we can possibly use some of this */
4232 if ( ! (is_dedicated
||
4233 (is_global
&& global_ok
))) {
4234 dprintf("%x:%x not suitable: %d %d\n", dl
->major
, dl
->minor
,
4235 is_dedicated
, is_global
);
4239 /* We are allowed to use this device - is there space?
4240 * We need a->info.component_size sectors */
4241 ex
= get_extents(ddf
, dl
);
4243 dprintf("cannot get extents\n");
4250 esize
= ex
[j
].start
- pos
;
4251 if (esize
>= a
->info
.component_size
)
4253 pos
= ex
[j
].start
+ ex
[j
].size
;
4255 } while (ex
[j
-1].size
);
4258 if (esize
< a
->info
.component_size
) {
4259 dprintf("%x:%x has no room: %llu %llu\n",
4260 dl
->major
, dl
->minor
,
4261 esize
, a
->info
.component_size
);
4266 /* Cool, we have a device with some space at pos */
4267 di
= xcalloc(1, sizeof(*di
));
4268 di
->disk
.number
= i
;
4269 di
->disk
.raid_disk
= i
;
4270 di
->disk
.major
= dl
->major
;
4271 di
->disk
.minor
= dl
->minor
;
4273 di
->recovery_start
= 0;
4274 di
->data_offset
= pos
;
4275 di
->component_size
= a
->info
.component_size
;
4276 di
->container_member
= dl
->pdnum
;
4279 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
4284 if (!dl
&& ! global_ok
) {
4285 /* not enough dedicated spares, try global */
4293 /* No spares found */
4295 /* Now 'rv' has a list of devices to return.
4296 * Create a metadata_update record to update the
4297 * phys_refnum and lba_offset values
4299 mu
= xmalloc(sizeof(*mu
));
4300 if (posix_memalign(&mu
->space
, 512, sizeof(struct vcl
)) != 0) {
4304 mu
->buf
= xmalloc(ddf
->conf_rec_len
* 512);
4305 mu
->len
= ddf
->conf_rec_len
* 512;
4307 mu
->space_list
= NULL
;
4308 mu
->next
= *updates
;
4309 vc
= find_vdcr(ddf
, a
->info
.container_member
);
4310 memcpy(mu
->buf
, vc
, ddf
->conf_rec_len
* 512);
4312 vc
= (struct vd_config
*)mu
->buf
;
4313 lba
= (__u64
*)&vc
->phys_refnum
[ddf
->mppe
];
4314 for (di
= rv
; di
; di
= di
->next
) {
4315 vc
->phys_refnum
[di
->disk
.raid_disk
] =
4316 ddf
->phys
->entries
[dl
->pdnum
].refnum
;
4317 lba
[di
->disk
.raid_disk
] = di
->data_offset
;
4322 #endif /* MDASSEMBLE */
4324 static int ddf_level_to_layout(int level
)
4331 return ALGORITHM_LEFT_SYMMETRIC
;
4333 return ALGORITHM_ROTATING_N_CONTINUE
;
4341 static void default_geometry_ddf(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4343 if (level
&& *level
== UnSet
)
4344 *level
= LEVEL_CONTAINER
;
4346 if (level
&& layout
&& *layout
== UnSet
)
4347 *layout
= ddf_level_to_layout(*level
);
4350 struct superswitch super_ddf
= {
4352 .examine_super
= examine_super_ddf
,
4353 .brief_examine_super
= brief_examine_super_ddf
,
4354 .brief_examine_subarrays
= brief_examine_subarrays_ddf
,
4355 .export_examine_super
= export_examine_super_ddf
,
4356 .detail_super
= detail_super_ddf
,
4357 .brief_detail_super
= brief_detail_super_ddf
,
4358 .validate_geometry
= validate_geometry_ddf
,
4359 .write_init_super
= write_init_super_ddf
,
4360 .add_to_super
= add_to_super_ddf
,
4361 .remove_from_super
= remove_from_super_ddf
,
4362 .load_container
= load_container_ddf
,
4363 .copy_metadata
= copy_metadata_ddf
,
4365 .match_home
= match_home_ddf
,
4366 .uuid_from_super
= uuid_from_super_ddf
,
4367 .getinfo_super
= getinfo_super_ddf
,
4368 .update_super
= update_super_ddf
,
4370 .avail_size
= avail_size_ddf
,
4372 .compare_super
= compare_super_ddf
,
4374 .load_super
= load_super_ddf
,
4375 .init_super
= init_super_ddf
,
4376 .store_super
= store_super_ddf
,
4377 .free_super
= free_super_ddf
,
4378 .match_metadata_desc
= match_metadata_desc_ddf
,
4379 .container_content
= container_content_ddf
,
4380 .default_geometry
= default_geometry_ddf
,
4386 .open_new
= ddf_open_new
,
4387 .set_array_state
= ddf_set_array_state
,
4388 .set_disk
= ddf_set_disk
,
4389 .sync_metadata
= ddf_sync_metadata
,
4390 .process_update
= ddf_process_update
,
4391 .prepare_update
= ddf_prepare_update
,
4392 .activate_spare
= ddf_activate_spare
,