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 (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 void detail_super_ddf(struct supertype
*st
, char *homehost
)
1333 * Could print DDF GUID
1334 * Need to find which array
1335 * If whole, briefly list all arrays
1340 static void brief_detail_super_ddf(struct supertype
*st
)
1344 struct ddf_super
*ddf
= st
->sb
;
1345 unsigned int vcnum
= get_vd_num_of_subarray(st
);
1346 if (vcnum
== DDF_CONTAINER
)
1347 uuid_from_super_ddf(st
, info
.uuid
);
1348 else if (vcnum
== DDF_NOTFOUND
)
1351 uuid_from_ddf_guid(ddf
->virt
->entries
[vcnum
].guid
, info
.uuid
);
1352 fname_from_uuid(st
, &info
, nbuf
,':');
1353 printf(" UUID=%s", nbuf
+ 5);
1357 static int match_home_ddf(struct supertype
*st
, char *homehost
)
1359 /* It matches 'this' host if the controller is a
1360 * Linux-MD controller with vendor_data matching
1363 struct ddf_super
*ddf
= st
->sb
;
1368 len
= strlen(homehost
);
1370 return (memcmp(ddf
->controller
.guid
, T10
, 8) == 0 &&
1371 len
< sizeof(ddf
->controller
.vendor_data
) &&
1372 memcmp(ddf
->controller
.vendor_data
, homehost
,len
) == 0 &&
1373 ddf
->controller
.vendor_data
[len
] == 0);
1377 static struct vd_config
*find_vdcr(struct ddf_super
*ddf
, unsigned int inst
)
1381 for (v
= ddf
->conflist
; v
; v
= v
->next
)
1382 if (inst
== v
->vcnum
)
1388 static int find_phys(struct ddf_super
*ddf
, __u32 phys_refnum
)
1390 /* Find the entry in phys_disk which has the given refnum
1391 * and return it's index
1394 for (i
= 0; i
< __be16_to_cpu(ddf
->phys
->max_pdes
); i
++)
1395 if (ddf
->phys
->entries
[i
].refnum
== phys_refnum
)
1400 static void uuid_from_ddf_guid(const char *guid
, int uuid
[4])
1403 struct sha1_ctx ctx
;
1404 sha1_init_ctx(&ctx
);
1405 sha1_process_bytes(guid
, DDF_GUID_LEN
, &ctx
);
1406 sha1_finish_ctx(&ctx
, buf
);
1407 memcpy(uuid
, buf
, 4*4);
1410 static void uuid_from_super_ddf(struct supertype
*st
, int uuid
[4])
1412 /* The uuid returned here is used for:
1413 * uuid to put into bitmap file (Create, Grow)
1414 * uuid for backup header when saving critical section (Grow)
1415 * comparing uuids when re-adding a device into an array
1416 * In these cases the uuid required is that of the data-array,
1417 * not the device-set.
1418 * uuid to recognise same set when adding a missing device back
1419 * to an array. This is a uuid for the device-set.
1421 * For each of these we can make do with a truncated
1422 * or hashed uuid rather than the original, as long as
1424 * In the case of SVD we assume the BVD is of interest,
1425 * though that might be the case if a bitmap were made for
1426 * a mirrored SVD - worry about that later.
1427 * So we need to find the VD configuration record for the
1428 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1429 * The first 16 bytes of the sha1 of these is used.
1431 struct ddf_super
*ddf
= st
->sb
;
1432 struct vcl
*vcl
= ddf
->currentconf
;
1436 guid
= vcl
->conf
.guid
;
1438 guid
= ddf
->anchor
.guid
;
1439 uuid_from_ddf_guid(guid
, uuid
);
1442 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
);
1444 static void getinfo_super_ddf(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1446 struct ddf_super
*ddf
= st
->sb
;
1447 int map_disks
= info
->array
.raid_disks
;
1450 if (ddf
->currentconf
) {
1451 getinfo_super_ddf_bvd(st
, info
, map
);
1454 memset(info
, 0, sizeof(*info
));
1456 info
->array
.raid_disks
= __be16_to_cpu(ddf
->phys
->used_pdes
);
1457 info
->array
.level
= LEVEL_CONTAINER
;
1458 info
->array
.layout
= 0;
1459 info
->array
.md_minor
= -1;
1460 cptr
= (__u32
*)(ddf
->anchor
.guid
+ 16);
1461 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1463 info
->array
.utime
= 0;
1464 info
->array
.chunk_size
= 0;
1465 info
->container_enough
= 1;
1467 info
->disk
.major
= 0;
1468 info
->disk
.minor
= 0;
1470 info
->disk
.number
= __be32_to_cpu(ddf
->dlist
->disk
.refnum
);
1471 info
->disk
.raid_disk
= find_phys(ddf
, ddf
->dlist
->disk
.refnum
);
1473 info
->data_offset
= __be64_to_cpu(ddf
->phys
->
1474 entries
[info
->disk
.raid_disk
].
1476 info
->component_size
= ddf
->dlist
->size
- info
->data_offset
;
1478 info
->disk
.number
= -1;
1479 info
->disk
.raid_disk
= -1;
1480 // info->disk.raid_disk = find refnum in the table and use index;
1482 info
->disk
.state
= (1 << MD_DISK_SYNC
) | (1 << MD_DISK_ACTIVE
);
1484 info
->recovery_start
= MaxSector
;
1485 info
->reshape_active
= 0;
1486 info
->recovery_blocked
= 0;
1489 info
->array
.major_version
= -1;
1490 info
->array
.minor_version
= -2;
1491 strcpy(info
->text_version
, "ddf");
1492 info
->safe_mode_delay
= 0;
1494 uuid_from_super_ddf(st
, info
->uuid
);
1498 for (i
= 0 ; i
< map_disks
; i
++) {
1499 if (i
< info
->array
.raid_disks
&&
1500 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1501 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1509 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
);
1511 static void getinfo_super_ddf_bvd(struct supertype
*st
, struct mdinfo
*info
, char *map
)
1513 struct ddf_super
*ddf
= st
->sb
;
1514 struct vcl
*vc
= ddf
->currentconf
;
1515 int cd
= ddf
->currentdev
;
1518 int map_disks
= info
->array
.raid_disks
;
1521 memset(info
, 0, sizeof(*info
));
1522 /* FIXME this returns BVD info - what if we want SVD ?? */
1524 info
->array
.raid_disks
= __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
1525 info
->array
.level
= map_num1(ddf_level_num
, vc
->conf
.prl
);
1526 info
->array
.layout
= rlq_to_layout(vc
->conf
.rlq
, vc
->conf
.prl
,
1527 info
->array
.raid_disks
);
1528 info
->array
.md_minor
= -1;
1529 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
1530 info
->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
1531 info
->array
.utime
= DECADE
+ __be32_to_cpu(vc
->conf
.timestamp
);
1532 info
->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
1533 info
->custom_array_size
= 0;
1535 if (cd
>= 0 && (unsigned)cd
< ddf
->mppe
) {
1536 info
->data_offset
= __be64_to_cpu(vc
->lba_offset
[cd
]);
1537 if (vc
->block_sizes
)
1538 info
->component_size
= vc
->block_sizes
[cd
];
1540 info
->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
1543 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
1544 if (dl
->raiddisk
== ddf
->currentdev
)
1547 info
->disk
.major
= 0;
1548 info
->disk
.minor
= 0;
1549 info
->disk
.state
= 0;
1551 info
->disk
.major
= dl
->major
;
1552 info
->disk
.minor
= dl
->minor
;
1553 info
->disk
.raid_disk
= dl
->raiddisk
;
1554 info
->disk
.number
= dl
->pdnum
;
1555 info
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
1558 info
->container_member
= ddf
->currentconf
->vcnum
;
1560 info
->recovery_start
= MaxSector
;
1561 info
->resync_start
= 0;
1562 info
->reshape_active
= 0;
1563 info
->recovery_blocked
= 0;
1564 if (!(ddf
->virt
->entries
[info
->container_member
].state
1565 & DDF_state_inconsistent
) &&
1566 (ddf
->virt
->entries
[info
->container_member
].init_state
1567 & DDF_initstate_mask
)
1569 info
->resync_start
= MaxSector
;
1571 uuid_from_super_ddf(st
, info
->uuid
);
1573 info
->array
.major_version
= -1;
1574 info
->array
.minor_version
= -2;
1575 sprintf(info
->text_version
, "/%s/%d",
1576 st
->container_devnm
,
1577 info
->container_member
);
1578 info
->safe_mode_delay
= 200;
1580 memcpy(info
->name
, ddf
->virt
->entries
[info
->container_member
].name
, 16);
1583 if (info
->name
[j
] == ' ')
1587 for (j
= 0; j
< map_disks
; j
++) {
1589 if (j
< info
->array
.raid_disks
) {
1590 int i
= find_phys(ddf
, vc
->conf
.phys_refnum
[j
]);
1592 (__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Online
) &&
1593 !(__be16_to_cpu(ddf
->phys
->entries
[i
].state
) & DDF_Failed
))
1599 static int update_super_ddf(struct supertype
*st
, struct mdinfo
*info
,
1601 char *devname
, int verbose
,
1602 int uuid_set
, char *homehost
)
1604 /* For 'assemble' and 'force' we need to return non-zero if any
1605 * change was made. For others, the return value is ignored.
1606 * Update options are:
1607 * force-one : This device looks a bit old but needs to be included,
1608 * update age info appropriately.
1609 * assemble: clear any 'faulty' flag to allow this device to
1611 * force-array: Array is degraded but being forced, mark it clean
1612 * if that will be needed to assemble it.
1614 * newdev: not used ????
1615 * grow: Array has gained a new device - this is currently for
1617 * resync: mark as dirty so a resync will happen.
1618 * uuid: Change the uuid of the array to match what is given
1619 * homehost: update the recorded homehost
1620 * name: update the name - preserving the homehost
1621 * _reshape_progress: record new reshape_progress position.
1623 * Following are not relevant for this version:
1624 * sparc2.2 : update from old dodgey metadata
1625 * super-minor: change the preferred_minor number
1626 * summaries: update redundant counters.
1629 // struct ddf_super *ddf = st->sb;
1630 // struct vd_config *vd = find_vdcr(ddf, info->container_member);
1631 // struct virtual_entry *ve = find_ve(ddf);
1633 /* we don't need to handle "force-*" or "assemble" as
1634 * there is no need to 'trick' the kernel. We the metadata is
1635 * first updated to activate the array, all the implied modifications
1639 if (strcmp(update
, "grow") == 0) {
1641 } else if (strcmp(update
, "resync") == 0) {
1642 // info->resync_checkpoint = 0;
1643 } else if (strcmp(update
, "homehost") == 0) {
1644 /* homehost is stored in controller->vendor_data,
1645 * or it is when we are the vendor
1647 // if (info->vendor_is_local)
1648 // strcpy(ddf->controller.vendor_data, homehost);
1650 } else if (strcmp(update
, "name") == 0) {
1651 /* name is stored in virtual_entry->name */
1652 // memset(ve->name, ' ', 16);
1653 // strncpy(ve->name, info->name, 16);
1655 } else if (strcmp(update
, "_reshape_progress") == 0) {
1656 /* We don't support reshape yet */
1657 } else if (strcmp(update
, "assemble") == 0 ) {
1658 /* Do nothing, just succeed */
1663 // update_all_csum(ddf);
1668 static void make_header_guid(char *guid
)
1671 /* Create a DDF Header of Virtual Disk GUID */
1673 /* 24 bytes of fiction required.
1674 * first 8 are a 'vendor-id' - "Linux-MD"
1675 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1676 * Remaining 8 random number plus timestamp
1678 memcpy(guid
, T10
, sizeof(T10
));
1679 stamp
= __cpu_to_be32(0xdeadbeef);
1680 memcpy(guid
+8, &stamp
, 4);
1681 stamp
= __cpu_to_be32(0);
1682 memcpy(guid
+12, &stamp
, 4);
1683 stamp
= __cpu_to_be32(time(0) - DECADE
);
1684 memcpy(guid
+16, &stamp
, 4);
1686 memcpy(guid
+20, &stamp
, 4);
1689 static int init_super_ddf_bvd(struct supertype
*st
,
1690 mdu_array_info_t
*info
,
1691 unsigned long long size
,
1692 char *name
, char *homehost
,
1693 int *uuid
, unsigned long long data_offset
);
1695 static int init_super_ddf(struct supertype
*st
,
1696 mdu_array_info_t
*info
,
1697 unsigned long long size
, char *name
, char *homehost
,
1698 int *uuid
, unsigned long long data_offset
)
1700 /* This is primarily called by Create when creating a new array.
1701 * We will then get add_to_super called for each component, and then
1702 * write_init_super called to write it out to each device.
1703 * For DDF, Create can create on fresh devices or on a pre-existing
1705 * To create on a pre-existing array a different method will be called.
1706 * This one is just for fresh drives.
1708 * We need to create the entire 'ddf' structure which includes:
1709 * DDF headers - these are easy.
1710 * Controller data - a Sector describing this controller .. not that
1711 * this is a controller exactly.
1712 * Physical Disk Record - one entry per device, so
1713 * leave plenty of space.
1714 * Virtual Disk Records - again, just leave plenty of space.
1715 * This just lists VDs, doesn't give details
1716 * Config records - describes the VDs that use this disk
1717 * DiskData - describes 'this' device.
1718 * BadBlockManagement - empty
1719 * Diag Space - empty
1720 * Vendor Logs - Could we put bitmaps here?
1723 struct ddf_super
*ddf
;
1726 int max_phys_disks
, max_virt_disks
;
1727 unsigned long long sector
;
1731 struct phys_disk
*pd
;
1732 struct virtual_disk
*vd
;
1734 if (data_offset
!= INVALID_SECTORS
) {
1735 fprintf(stderr
, Name
": data-offset not supported by DDF\n");
1740 return init_super_ddf_bvd(st
, info
, size
, name
, homehost
, uuid
,
1743 if (posix_memalign((void**)&ddf
, 512, sizeof(*ddf
)) != 0) {
1744 pr_err("%s could not allocate superblock\n", __func__
);
1747 memset(ddf
, 0, sizeof(*ddf
));
1748 ddf
->dlist
= NULL
; /* no physical disks yet */
1749 ddf
->conflist
= NULL
; /* No virtual disks yet */
1753 /* zeroing superblock */
1757 /* At least 32MB *must* be reserved for the ddf. So let's just
1758 * start 32MB from the end, and put the primary header there.
1759 * Don't do secondary for now.
1760 * We don't know exactly where that will be yet as it could be
1761 * different on each device. To just set up the lengths.
1765 ddf
->anchor
.magic
= DDF_HEADER_MAGIC
;
1766 make_header_guid(ddf
->anchor
.guid
);
1768 memcpy(ddf
->anchor
.revision
, DDF_REVISION_2
, 8);
1769 ddf
->anchor
.seq
= __cpu_to_be32(1);
1770 ddf
->anchor
.timestamp
= __cpu_to_be32(time(0) - DECADE
);
1771 ddf
->anchor
.openflag
= 0xFF;
1772 ddf
->anchor
.foreignflag
= 0;
1773 ddf
->anchor
.enforcegroups
= 0; /* Is this best?? */
1774 ddf
->anchor
.pad0
= 0xff;
1775 memset(ddf
->anchor
.pad1
, 0xff, 12);
1776 memset(ddf
->anchor
.header_ext
, 0xff, 32);
1777 ddf
->anchor
.primary_lba
= ~(__u64
)0;
1778 ddf
->anchor
.secondary_lba
= ~(__u64
)0;
1779 ddf
->anchor
.type
= DDF_HEADER_ANCHOR
;
1780 memset(ddf
->anchor
.pad2
, 0xff, 3);
1781 ddf
->anchor
.workspace_len
= __cpu_to_be32(32768); /* Must be reserved */
1782 ddf
->anchor
.workspace_lba
= ~(__u64
)0; /* Put this at bottom
1783 of 32M reserved.. */
1784 max_phys_disks
= 1023; /* Should be enough */
1785 ddf
->anchor
.max_pd_entries
= __cpu_to_be16(max_phys_disks
);
1786 max_virt_disks
= 255;
1787 ddf
->anchor
.max_vd_entries
= __cpu_to_be16(max_virt_disks
); /* ?? */
1788 ddf
->anchor
.max_partitions
= __cpu_to_be16(64); /* ?? */
1791 ddf
->conf_rec_len
= 1 + ROUND_UP(ddf
->mppe
* (4+8), 512)/512;
1792 ddf
->anchor
.config_record_len
= __cpu_to_be16(ddf
->conf_rec_len
);
1793 ddf
->anchor
.max_primary_element_entries
= __cpu_to_be16(ddf
->mppe
);
1794 memset(ddf
->anchor
.pad3
, 0xff, 54);
1795 /* controller sections is one sector long immediately
1796 * after the ddf header */
1798 ddf
->anchor
.controller_section_offset
= __cpu_to_be32(sector
);
1799 ddf
->anchor
.controller_section_length
= __cpu_to_be32(1);
1802 /* phys is 8 sectors after that */
1803 pdsize
= ROUND_UP(sizeof(struct phys_disk
) +
1804 sizeof(struct phys_disk_entry
)*max_phys_disks
,
1806 switch(pdsize
/512) {
1807 case 2: case 8: case 32: case 128: case 512: break;
1810 ddf
->anchor
.phys_section_offset
= __cpu_to_be32(sector
);
1811 ddf
->anchor
.phys_section_length
=
1812 __cpu_to_be32(pdsize
/512); /* max_primary_element_entries/8 */
1813 sector
+= pdsize
/512;
1815 /* virt is another 32 sectors */
1816 vdsize
= ROUND_UP(sizeof(struct virtual_disk
) +
1817 sizeof(struct virtual_entry
) * max_virt_disks
,
1819 switch(vdsize
/512) {
1820 case 2: case 8: case 32: case 128: case 512: break;
1823 ddf
->anchor
.virt_section_offset
= __cpu_to_be32(sector
);
1824 ddf
->anchor
.virt_section_length
=
1825 __cpu_to_be32(vdsize
/512); /* max_vd_entries/8 */
1826 sector
+= vdsize
/512;
1828 clen
= ddf
->conf_rec_len
* (ddf
->max_part
+1);
1829 ddf
->anchor
.config_section_offset
= __cpu_to_be32(sector
);
1830 ddf
->anchor
.config_section_length
= __cpu_to_be32(clen
);
1833 ddf
->anchor
.data_section_offset
= __cpu_to_be32(sector
);
1834 ddf
->anchor
.data_section_length
= __cpu_to_be32(1);
1837 ddf
->anchor
.bbm_section_length
= __cpu_to_be32(0);
1838 ddf
->anchor
.bbm_section_offset
= __cpu_to_be32(0xFFFFFFFF);
1839 ddf
->anchor
.diag_space_length
= __cpu_to_be32(0);
1840 ddf
->anchor
.diag_space_offset
= __cpu_to_be32(0xFFFFFFFF);
1841 ddf
->anchor
.vendor_length
= __cpu_to_be32(0);
1842 ddf
->anchor
.vendor_offset
= __cpu_to_be32(0xFFFFFFFF);
1844 memset(ddf
->anchor
.pad4
, 0xff, 256);
1846 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
1847 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
1849 ddf
->primary
.openflag
= 1; /* I guess.. */
1850 ddf
->primary
.type
= DDF_HEADER_PRIMARY
;
1852 ddf
->secondary
.openflag
= 1; /* I guess.. */
1853 ddf
->secondary
.type
= DDF_HEADER_SECONDARY
;
1855 ddf
->active
= &ddf
->primary
;
1857 ddf
->controller
.magic
= DDF_CONTROLLER_MAGIC
;
1859 /* 24 more bytes of fiction required.
1860 * first 8 are a 'vendor-id' - "Linux-MD"
1861 * Remaining 16 are serial number.... maybe a hostname would do?
1863 memcpy(ddf
->controller
.guid
, T10
, sizeof(T10
));
1864 gethostname(hostname
, sizeof(hostname
));
1865 hostname
[sizeof(hostname
) - 1] = 0;
1866 hostlen
= strlen(hostname
);
1867 memcpy(ddf
->controller
.guid
+ 24 - hostlen
, hostname
, hostlen
);
1868 for (i
= strlen(T10
) ; i
+hostlen
< 24; i
++)
1869 ddf
->controller
.guid
[i
] = ' ';
1871 ddf
->controller
.type
.vendor_id
= __cpu_to_be16(0xDEAD);
1872 ddf
->controller
.type
.device_id
= __cpu_to_be16(0xBEEF);
1873 ddf
->controller
.type
.sub_vendor_id
= 0;
1874 ddf
->controller
.type
.sub_device_id
= 0;
1875 memcpy(ddf
->controller
.product_id
, "What Is My PID??", 16);
1876 memset(ddf
->controller
.pad
, 0xff, 8);
1877 memset(ddf
->controller
.vendor_data
, 0xff, 448);
1878 if (homehost
&& strlen(homehost
) < 440)
1879 strcpy((char*)ddf
->controller
.vendor_data
, homehost
);
1881 if (posix_memalign((void**)&pd
, 512, pdsize
) != 0) {
1882 pr_err("%s could not allocate pd\n", __func__
);
1886 ddf
->pdsize
= pdsize
;
1888 memset(pd
, 0xff, pdsize
);
1889 memset(pd
, 0, sizeof(*pd
));
1890 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
1891 pd
->used_pdes
= __cpu_to_be16(0);
1892 pd
->max_pdes
= __cpu_to_be16(max_phys_disks
);
1893 memset(pd
->pad
, 0xff, 52);
1895 if (posix_memalign((void**)&vd
, 512, vdsize
) != 0) {
1896 pr_err("%s could not allocate vd\n", __func__
);
1900 ddf
->vdsize
= vdsize
;
1901 memset(vd
, 0, vdsize
);
1902 vd
->magic
= DDF_VIRT_RECORDS_MAGIC
;
1903 vd
->populated_vdes
= __cpu_to_be16(0);
1904 vd
->max_vdes
= __cpu_to_be16(max_virt_disks
);
1905 memset(vd
->pad
, 0xff, 52);
1907 for (i
=0; i
<max_virt_disks
; i
++)
1908 memset(&vd
->entries
[i
], 0xff, sizeof(struct virtual_entry
));
1911 ddf_set_updates_pending(ddf
);
1915 static int chunk_to_shift(int chunksize
)
1917 return ffs(chunksize
/512)-1;
1920 static int level_to_prl(int level
)
1923 case LEVEL_LINEAR
: return DDF_CONCAT
;
1924 case 0: return DDF_RAID0
;
1925 case 1: return DDF_RAID1
;
1926 case 4: return DDF_RAID4
;
1927 case 5: return DDF_RAID5
;
1928 case 6: return DDF_RAID6
;
1933 static int layout_to_rlq(int level
, int layout
, int raiddisks
)
1937 return DDF_RAID0_SIMPLE
;
1940 case 2: return DDF_RAID1_SIMPLE
;
1941 case 3: return DDF_RAID1_MULTI
;
1946 case 0: return DDF_RAID4_N
;
1951 case ALGORITHM_LEFT_ASYMMETRIC
:
1952 return DDF_RAID5_N_RESTART
;
1953 case ALGORITHM_RIGHT_ASYMMETRIC
:
1954 return DDF_RAID5_0_RESTART
;
1955 case ALGORITHM_LEFT_SYMMETRIC
:
1956 return DDF_RAID5_N_CONTINUE
;
1957 case ALGORITHM_RIGHT_SYMMETRIC
:
1958 return -1; /* not mentioned in standard */
1962 case ALGORITHM_ROTATING_N_RESTART
:
1963 return DDF_RAID5_N_RESTART
;
1964 case ALGORITHM_ROTATING_ZERO_RESTART
:
1965 return DDF_RAID6_0_RESTART
;
1966 case ALGORITHM_ROTATING_N_CONTINUE
:
1967 return DDF_RAID5_N_CONTINUE
;
1973 static int rlq_to_layout(int rlq
, int prl
, int raiddisks
)
1977 return 0; /* hopefully rlq == DDF_RAID0_SIMPLE */
1979 return 0; /* hopefully rlq == SIMPLE or MULTI depending
1987 return -1; /* FIXME this isn't checked */
1991 case DDF_RAID5_N_RESTART
:
1992 return ALGORITHM_LEFT_ASYMMETRIC
;
1993 case DDF_RAID5_0_RESTART
:
1994 return ALGORITHM_RIGHT_ASYMMETRIC
;
1995 case DDF_RAID5_N_CONTINUE
:
1996 return ALGORITHM_LEFT_SYMMETRIC
;
2002 case DDF_RAID5_N_RESTART
:
2003 return ALGORITHM_ROTATING_N_RESTART
;
2004 case DDF_RAID6_0_RESTART
:
2005 return ALGORITHM_ROTATING_ZERO_RESTART
;
2006 case DDF_RAID5_N_CONTINUE
:
2007 return ALGORITHM_ROTATING_N_CONTINUE
;
2017 unsigned long long start
, size
;
2019 static int cmp_extent(const void *av
, const void *bv
)
2021 const struct extent
*a
= av
;
2022 const struct extent
*b
= bv
;
2023 if (a
->start
< b
->start
)
2025 if (a
->start
> b
->start
)
2030 static struct extent
*get_extents(struct ddf_super
*ddf
, struct dl
*dl
)
2032 /* find a list of used extents on the give physical device
2033 * (dnum) of the given ddf.
2034 * Return a malloced array of 'struct extent'
2036 * FIXME ignore DDF_Legacy devices?
2043 rv
= xmalloc(sizeof(struct extent
) * (ddf
->max_part
+ 2));
2045 for (i
= 0; i
< ddf
->max_part
; i
++) {
2046 struct vcl
*v
= dl
->vlist
[i
];
2049 for (j
= 0; j
< v
->conf
.prim_elmnt_count
; j
++)
2050 if (v
->conf
.phys_refnum
[j
] == dl
->disk
.refnum
) {
2051 /* This device plays role 'j' in 'v'. */
2052 rv
[n
].start
= __be64_to_cpu(v
->lba_offset
[j
]);
2053 rv
[n
].size
= __be64_to_cpu(v
->conf
.blocks
);
2058 qsort(rv
, n
, sizeof(*rv
), cmp_extent
);
2060 rv
[n
].start
= __be64_to_cpu(ddf
->phys
->entries
[dl
->pdnum
].config_size
);
2066 static int init_super_ddf_bvd(struct supertype
*st
,
2067 mdu_array_info_t
*info
,
2068 unsigned long long size
,
2069 char *name
, char *homehost
,
2070 int *uuid
, unsigned long long data_offset
)
2072 /* We are creating a BVD inside a pre-existing container.
2073 * so st->sb is already set.
2074 * We need to create a new vd_config and a new virtual_entry
2076 struct ddf_super
*ddf
= st
->sb
;
2078 struct virtual_entry
*ve
;
2080 struct vd_config
*vc
;
2082 if (__be16_to_cpu(ddf
->virt
->populated_vdes
)
2083 >= __be16_to_cpu(ddf
->virt
->max_vdes
)) {
2084 pr_err("This ddf already has the "
2085 "maximum of %d virtual devices\n",
2086 __be16_to_cpu(ddf
->virt
->max_vdes
));
2091 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
2092 if (!all_ff(ddf
->virt
->entries
[venum
].guid
)) {
2093 char *n
= ddf
->virt
->entries
[venum
].name
;
2095 if (strncmp(name
, n
, 16) == 0) {
2096 pr_err("This ddf already"
2097 " has an array called %s\n",
2103 for (venum
= 0; venum
< __be16_to_cpu(ddf
->virt
->max_vdes
); venum
++)
2104 if (all_ff(ddf
->virt
->entries
[venum
].guid
))
2106 if (venum
== __be16_to_cpu(ddf
->virt
->max_vdes
)) {
2107 pr_err("Cannot find spare slot for "
2108 "virtual disk - DDF is corrupt\n");
2111 ve
= &ddf
->virt
->entries
[venum
];
2113 /* A Virtual Disk GUID contains the T10 Vendor ID, controller type,
2114 * timestamp, random number
2116 make_header_guid(ve
->guid
);
2117 ve
->unit
= __cpu_to_be16(info
->md_minor
);
2119 ve
->guid_crc
= crc32(0, (unsigned char*)ddf
->anchor
.guid
, DDF_GUID_LEN
);
2121 ve
->state
= DDF_state_degraded
; /* Will be modified as devices are added */
2122 if (info
->state
& 1) /* clean */
2123 ve
->init_state
= DDF_init_full
;
2125 ve
->init_state
= DDF_init_not
;
2127 memset(ve
->pad1
, 0xff, 14);
2128 memset(ve
->name
, ' ', 16);
2130 strncpy(ve
->name
, name
, 16);
2131 ddf
->virt
->populated_vdes
=
2132 __cpu_to_be16(__be16_to_cpu(ddf
->virt
->populated_vdes
)+1);
2134 /* Now create a new vd_config */
2135 if (posix_memalign((void**)&vcl
, 512,
2136 (offsetof(struct vcl
, conf
) + ddf
->conf_rec_len
* 512)) != 0) {
2137 pr_err("%s could not allocate vd_config\n", __func__
);
2140 vcl
->lba_offset
= (__u64
*) &vcl
->conf
.phys_refnum
[ddf
->mppe
];
2142 vcl
->block_sizes
= NULL
; /* FIXME not for CONCAT */
2143 vcl
->other_bvds
= NULL
;
2147 vc
->magic
= DDF_VD_CONF_MAGIC
;
2148 memcpy(vc
->guid
, ve
->guid
, DDF_GUID_LEN
);
2149 vc
->timestamp
= __cpu_to_be32(time(0)-DECADE
);
2150 vc
->seqnum
= __cpu_to_be32(1);
2151 memset(vc
->pad0
, 0xff, 24);
2152 vc
->prim_elmnt_count
= __cpu_to_be16(info
->raid_disks
);
2153 vc
->chunk_shift
= chunk_to_shift(info
->chunk_size
);
2154 vc
->prl
= level_to_prl(info
->level
);
2155 vc
->rlq
= layout_to_rlq(info
->level
, info
->layout
, info
->raid_disks
);
2156 vc
->sec_elmnt_count
= 1;
2157 vc
->sec_elmnt_seq
= 0;
2159 vc
->blocks
= __cpu_to_be64(info
->size
* 2);
2160 vc
->array_blocks
= __cpu_to_be64(
2161 calc_array_size(info
->level
, info
->raid_disks
, info
->layout
,
2162 info
->chunk_size
, info
->size
*2));
2163 memset(vc
->pad1
, 0xff, 8);
2164 vc
->spare_refs
[0] = 0xffffffff;
2165 vc
->spare_refs
[1] = 0xffffffff;
2166 vc
->spare_refs
[2] = 0xffffffff;
2167 vc
->spare_refs
[3] = 0xffffffff;
2168 vc
->spare_refs
[4] = 0xffffffff;
2169 vc
->spare_refs
[5] = 0xffffffff;
2170 vc
->spare_refs
[6] = 0xffffffff;
2171 vc
->spare_refs
[7] = 0xffffffff;
2172 memset(vc
->cache_pol
, 0, 8);
2174 memset(vc
->pad2
, 0xff, 3);
2175 memset(vc
->pad3
, 0xff, 52);
2176 memset(vc
->pad4
, 0xff, 192);
2177 memset(vc
->v0
, 0xff, 32);
2178 memset(vc
->v1
, 0xff, 32);
2179 memset(vc
->v2
, 0xff, 16);
2180 memset(vc
->v3
, 0xff, 16);
2181 memset(vc
->vendor
, 0xff, 32);
2183 memset(vc
->phys_refnum
, 0xff, 4*ddf
->mppe
);
2184 memset(vc
->phys_refnum
+ddf
->mppe
, 0x00, 8*ddf
->mppe
);
2186 vcl
->next
= ddf
->conflist
;
2187 ddf
->conflist
= vcl
;
2188 ddf
->currentconf
= vcl
;
2189 ddf_set_updates_pending(ddf
);
2194 static void add_to_super_ddf_bvd(struct supertype
*st
,
2195 mdu_disk_info_t
*dk
, int fd
, char *devname
)
2197 /* fd and devname identify a device with-in the ddf container (st).
2198 * dk identifies a location in the new BVD.
2199 * We need to find suitable free space in that device and update
2200 * the phys_refnum and lba_offset for the newly created vd_config.
2201 * We might also want to update the type in the phys_disk
2204 * Alternately: fd == -1 and we have already chosen which device to
2205 * use and recorded in dlist->raid_disk;
2208 struct ddf_super
*ddf
= st
->sb
;
2209 struct vd_config
*vc
;
2211 unsigned int working
;
2213 unsigned long long blocks
, pos
, esize
;
2217 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2218 if (dl
->raiddisk
== dk
->raid_disk
)
2221 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2222 if (dl
->major
== dk
->major
&&
2223 dl
->minor
== dk
->minor
)
2226 if (!dl
|| ! (dk
->state
& (1<<MD_DISK_SYNC
)))
2229 vc
= &ddf
->currentconf
->conf
;
2230 lba_offset
= ddf
->currentconf
->lba_offset
;
2232 ex
= get_extents(ddf
, dl
);
2237 blocks
= __be64_to_cpu(vc
->blocks
);
2238 if (ddf
->currentconf
->block_sizes
)
2239 blocks
= ddf
->currentconf
->block_sizes
[dk
->raid_disk
];
2242 esize
= ex
[i
].start
- pos
;
2243 if (esize
>= blocks
)
2245 pos
= ex
[i
].start
+ ex
[i
].size
;
2247 } while (ex
[i
-1].size
);
2253 ddf
->currentdev
= dk
->raid_disk
;
2254 vc
->phys_refnum
[dk
->raid_disk
] = dl
->disk
.refnum
;
2255 lba_offset
[dk
->raid_disk
] = __cpu_to_be64(pos
);
2257 for (i
= 0; i
< ddf
->max_part
; i
++)
2258 if (dl
->vlist
[i
] == NULL
)
2260 if (i
== ddf
->max_part
)
2262 dl
->vlist
[i
] = ddf
->currentconf
;
2267 dl
->devname
= devname
;
2269 /* Check how many working raid_disks, and if we can mark
2270 * array as optimal yet
2274 for (i
= 0; i
< __be16_to_cpu(vc
->prim_elmnt_count
); i
++)
2275 if (vc
->phys_refnum
[i
] != 0xffffffff)
2278 /* Find which virtual_entry */
2279 i
= ddf
->currentconf
->vcnum
;
2280 if (working
== __be16_to_cpu(vc
->prim_elmnt_count
))
2281 ddf
->virt
->entries
[i
].state
=
2282 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2283 | DDF_state_optimal
;
2285 if (vc
->prl
== DDF_RAID6
&&
2286 working
+1 == __be16_to_cpu(vc
->prim_elmnt_count
))
2287 ddf
->virt
->entries
[i
].state
=
2288 (ddf
->virt
->entries
[i
].state
& ~DDF_state_mask
)
2289 | DDF_state_part_optimal
;
2291 ddf
->phys
->entries
[dl
->pdnum
].type
&= ~__cpu_to_be16(DDF_Global_Spare
);
2292 ddf
->phys
->entries
[dl
->pdnum
].type
|= __cpu_to_be16(DDF_Active_in_VD
);
2293 ddf_set_updates_pending(ddf
);
2296 /* add a device to a container, either while creating it or while
2297 * expanding a pre-existing container
2299 static int add_to_super_ddf(struct supertype
*st
,
2300 mdu_disk_info_t
*dk
, int fd
, char *devname
,
2301 unsigned long long data_offset
)
2303 struct ddf_super
*ddf
= st
->sb
;
2307 unsigned long long size
;
2308 struct phys_disk_entry
*pde
;
2313 if (ddf
->currentconf
) {
2314 add_to_super_ddf_bvd(st
, dk
, fd
, devname
);
2318 /* This is device numbered dk->number. We need to create
2319 * a phys_disk entry and a more detailed disk_data entry.
2322 if (posix_memalign((void**)&dd
, 512,
2323 sizeof(*dd
) + sizeof(dd
->vlist
[0]) * ddf
->max_part
) != 0) {
2324 pr_err("%s could allocate buffer for new disk, aborting\n",
2328 dd
->major
= major(stb
.st_rdev
);
2329 dd
->minor
= minor(stb
.st_rdev
);
2330 dd
->devname
= devname
;
2334 dd
->disk
.magic
= DDF_PHYS_DATA_MAGIC
;
2336 tm
= localtime(&now
);
2337 sprintf(dd
->disk
.guid
, "%8s%04d%02d%02d",
2338 T10
, tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
);
2339 tptr
= (__u32
*)(dd
->disk
.guid
+ 16);
2340 *tptr
++ = random32();
2344 /* Cannot be bothered finding a CRC of some irrelevant details*/
2345 dd
->disk
.refnum
= random32();
2346 for (i
= __be16_to_cpu(ddf
->active
->max_pd_entries
);
2348 if (ddf
->phys
->entries
[i
-1].refnum
== dd
->disk
.refnum
)
2352 dd
->disk
.forced_ref
= 1;
2353 dd
->disk
.forced_guid
= 1;
2354 memset(dd
->disk
.vendor
, ' ', 32);
2355 memcpy(dd
->disk
.vendor
, "Linux", 5);
2356 memset(dd
->disk
.pad
, 0xff, 442);
2357 for (i
= 0; i
< ddf
->max_part
; i
++)
2358 dd
->vlist
[i
] = NULL
;
2360 n
= __be16_to_cpu(ddf
->phys
->used_pdes
);
2361 pde
= &ddf
->phys
->entries
[n
];
2364 if (st
->update_tail
) {
2365 int len
= (sizeof(struct phys_disk
) +
2366 sizeof(struct phys_disk_entry
));
2367 struct phys_disk
*pd
;
2370 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2371 pd
->used_pdes
= __cpu_to_be16(n
);
2372 pde
= &pd
->entries
[0];
2376 ddf
->phys
->used_pdes
= __cpu_to_be16(n
);
2379 memcpy(pde
->guid
, dd
->disk
.guid
, DDF_GUID_LEN
);
2380 pde
->refnum
= dd
->disk
.refnum
;
2381 pde
->type
= __cpu_to_be16(DDF_Forced_PD_GUID
| DDF_Global_Spare
);
2382 pde
->state
= __cpu_to_be16(DDF_Online
);
2383 get_dev_size(fd
, NULL
, &size
);
2384 /* We are required to reserve 32Meg, and record the size in sectors */
2385 pde
->config_size
= __cpu_to_be64( (size
- 32*1024*1024) / 512);
2386 sprintf(pde
->path
, "%17.17s","Information: nil") ;
2387 memset(pde
->pad
, 0xff, 6);
2389 dd
->size
= size
>> 9;
2390 if (st
->update_tail
) {
2391 dd
->next
= ddf
->add_list
;
2394 dd
->next
= ddf
->dlist
;
2396 ddf_set_updates_pending(ddf
);
2402 static int remove_from_super_ddf(struct supertype
*st
, mdu_disk_info_t
*dk
)
2404 struct ddf_super
*ddf
= st
->sb
;
2407 /* mdmon has noticed that this disk (dk->major/dk->minor) has
2408 * disappeared from the container.
2409 * We need to arrange that it disappears from the metadata and
2410 * internal data structures too.
2411 * Most of the work is done by ddf_process_update which edits
2412 * the metadata and closes the file handle and attaches the memory
2413 * where free_updates will free it.
2415 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2416 if (dl
->major
== dk
->major
&&
2417 dl
->minor
== dk
->minor
)
2422 if (st
->update_tail
) {
2423 int len
= (sizeof(struct phys_disk
) +
2424 sizeof(struct phys_disk_entry
));
2425 struct phys_disk
*pd
;
2428 pd
->magic
= DDF_PHYS_RECORDS_MAGIC
;
2429 pd
->used_pdes
= __cpu_to_be16(dl
->pdnum
);
2430 pd
->entries
[0].state
= __cpu_to_be16(DDF_Missing
);
2431 append_metadata_update(st
, pd
, len
);
2437 * This is the write_init_super method for a ddf container. It is
2438 * called when creating a container or adding another device to a
2441 #define NULL_CONF_SZ 4096
2443 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
2444 __u32 refnum
, unsigned int nmax
,
2445 const struct vd_config
**bvd
,
2448 static int __write_ddf_structure(struct dl
*d
, struct ddf_super
*ddf
, __u8 type
,
2451 unsigned long long sector
;
2452 struct ddf_header
*header
;
2453 int fd
, i
, n_config
, conf_size
;
2458 case DDF_HEADER_PRIMARY
:
2459 header
= &ddf
->primary
;
2460 sector
= __be64_to_cpu(header
->primary_lba
);
2462 case DDF_HEADER_SECONDARY
:
2463 header
= &ddf
->secondary
;
2464 sector
= __be64_to_cpu(header
->secondary_lba
);
2470 header
->type
= type
;
2471 header
->openflag
= 0;
2472 header
->crc
= calc_crc(header
, 512);
2474 lseek64(fd
, sector
<<9, 0);
2475 if (write(fd
, header
, 512) < 0)
2478 ddf
->controller
.crc
= calc_crc(&ddf
->controller
, 512);
2479 if (write(fd
, &ddf
->controller
, 512) < 0)
2482 ddf
->phys
->crc
= calc_crc(ddf
->phys
, ddf
->pdsize
);
2483 if (write(fd
, ddf
->phys
, ddf
->pdsize
) < 0)
2485 ddf
->virt
->crc
= calc_crc(ddf
->virt
, ddf
->vdsize
);
2486 if (write(fd
, ddf
->virt
, ddf
->vdsize
) < 0)
2489 /* Now write lots of config records. */
2490 n_config
= ddf
->max_part
;
2491 conf_size
= ddf
->conf_rec_len
* 512;
2492 for (i
= 0 ; i
<= n_config
; i
++) {
2494 struct vd_config
*vdc
= NULL
;
2495 if (i
== n_config
) {
2496 c
= (struct vcl
*)d
->spare
;
2503 get_pd_index_from_refnum(
2506 (const struct vd_config
**)&vdc
,
2510 vdc
->seqnum
= header
->seq
;
2511 vdc
->crc
= calc_crc(vdc
, conf_size
);
2512 if (write(fd
, vdc
, conf_size
) < 0)
2515 unsigned int togo
= conf_size
;
2516 while (togo
> NULL_CONF_SZ
) {
2517 if (write(fd
, null_aligned
, NULL_CONF_SZ
) < 0)
2519 togo
-= NULL_CONF_SZ
;
2521 if (write(fd
, null_aligned
, togo
) < 0)
2528 d
->disk
.crc
= calc_crc(&d
->disk
, 512);
2529 if (write(fd
, &d
->disk
, 512) < 0)
2535 static int __write_init_super_ddf(struct supertype
*st
)
2537 struct ddf_super
*ddf
= st
->sb
;
2541 unsigned long long size
;
2545 pr_state(ddf
, __func__
);
2546 if (posix_memalign((void**)&null_aligned
, 4096, NULL_CONF_SZ
) != 0) {
2549 memset(null_aligned
, 0xff, NULL_CONF_SZ
);
2551 seq
= ddf
->active
->seq
+ 1;
2553 /* try to write updated metadata,
2554 * if we catch a failure move on to the next disk
2556 for (d
= ddf
->dlist
; d
; d
=d
->next
) {
2563 /* We need to fill in the primary, (secondary) and workspace
2564 * lba's in the headers, set their checksums,
2565 * Also checksum phys, virt....
2567 * Then write everything out, finally the anchor is written.
2569 get_dev_size(fd
, NULL
, &size
);
2571 if (d
->workspace_lba
!= 0)
2572 ddf
->anchor
.workspace_lba
= d
->workspace_lba
;
2574 ddf
->anchor
.workspace_lba
=
2575 __cpu_to_be64(size
- 32*1024*2);
2576 if (d
->primary_lba
!= 0)
2577 ddf
->anchor
.primary_lba
= d
->primary_lba
;
2579 ddf
->anchor
.primary_lba
=
2580 __cpu_to_be64(size
- 16*1024*2);
2581 if (d
->secondary_lba
!= 0)
2582 ddf
->anchor
.secondary_lba
= d
->secondary_lba
;
2584 ddf
->anchor
.secondary_lba
=
2585 __cpu_to_be64(size
- 32*1024*2);
2586 ddf
->anchor
.seq
= seq
;
2587 memcpy(&ddf
->primary
, &ddf
->anchor
, 512);
2588 memcpy(&ddf
->secondary
, &ddf
->anchor
, 512);
2590 ddf
->anchor
.openflag
= 0xFF; /* 'open' means nothing */
2591 ddf
->anchor
.seq
= 0xFFFFFFFF; /* no sequencing in anchor */
2592 ddf
->anchor
.crc
= calc_crc(&ddf
->anchor
, 512);
2594 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_PRIMARY
,
2598 if (!__write_ddf_structure(d
, ddf
, DDF_HEADER_SECONDARY
,
2602 lseek64(fd
, (size
-1)*512, SEEK_SET
);
2603 if (write(fd
, &ddf
->anchor
, 512) < 0)
2609 return attempts
!= successes
;
2612 static int write_init_super_ddf(struct supertype
*st
)
2614 struct ddf_super
*ddf
= st
->sb
;
2615 struct vcl
*currentconf
= ddf
->currentconf
;
2617 /* we are done with currentconf reset it to point st at the container */
2618 ddf
->currentconf
= NULL
;
2620 if (st
->update_tail
) {
2621 /* queue the virtual_disk and vd_config as metadata updates */
2622 struct virtual_disk
*vd
;
2623 struct vd_config
*vc
;
2627 int len
= (sizeof(struct phys_disk
) +
2628 sizeof(struct phys_disk_entry
));
2630 /* adding a disk to the container. */
2634 append_metadata_update(st
, ddf
->add_list
->mdupdate
, len
);
2635 ddf
->add_list
->mdupdate
= NULL
;
2639 /* Newly created VD */
2641 /* First the virtual disk. We have a slightly fake header */
2642 len
= sizeof(struct virtual_disk
) + sizeof(struct virtual_entry
);
2645 vd
->entries
[0] = ddf
->virt
->entries
[currentconf
->vcnum
];
2646 vd
->populated_vdes
= __cpu_to_be16(currentconf
->vcnum
);
2647 append_metadata_update(st
, vd
, len
);
2649 /* Then the vd_config */
2650 len
= ddf
->conf_rec_len
* 512;
2652 memcpy(vc
, ¤tconf
->conf
, len
);
2653 append_metadata_update(st
, vc
, len
);
2655 /* FIXME I need to close the fds! */
2659 for (d
= ddf
->dlist
; d
; d
=d
->next
)
2660 while (Kill(d
->devname
, NULL
, 0, -1, 1) == 0);
2661 return __write_init_super_ddf(st
);
2667 static __u64
avail_size_ddf(struct supertype
*st
, __u64 devsize
,
2668 unsigned long long data_offset
)
2670 /* We must reserve the last 32Meg */
2671 if (devsize
<= 32*1024*2)
2673 return devsize
- 32*1024*2;
2678 static int reserve_space(struct supertype
*st
, int raiddisks
,
2679 unsigned long long size
, int chunk
,
2680 unsigned long long *freesize
)
2682 /* Find 'raiddisks' spare extents at least 'size' big (but
2683 * only caring about multiples of 'chunk') and remember
2685 * If the cannot be found, fail.
2688 struct ddf_super
*ddf
= st
->sb
;
2691 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2695 /* Now find largest extent on each device */
2696 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2697 struct extent
*e
= get_extents(ddf
, dl
);
2698 unsigned long long pos
= 0;
2701 unsigned long long minsize
= size
;
2709 unsigned long long esize
;
2710 esize
= e
[i
].start
- pos
;
2711 if (esize
>= minsize
) {
2715 pos
= e
[i
].start
+ e
[i
].size
;
2717 } while (e
[i
-1].size
);
2720 dl
->esize
= minsize
;
2724 if (cnt
< raiddisks
) {
2725 pr_err("not enough devices with space to create array.\n");
2726 return 0; /* No enough free spaces large enough */
2729 /* choose the largest size of which there are at least 'raiddisk' */
2730 for (dl
= ddf
->dlist
; dl
; dl
=dl
->next
) {
2732 if (dl
->esize
<= size
)
2734 /* This is bigger than 'size', see if there are enough */
2736 for (dl2
= ddf
->dlist
; dl2
; dl2
=dl2
->next
)
2737 if (dl2
->esize
>= dl
->esize
)
2739 if (cnt
>= raiddisks
)
2743 size
= size
/ chunk
;
2748 pr_err("not enough spare devices to create array.\n");
2752 /* We have a 'size' of which there are enough spaces.
2753 * We simply do a first-fit */
2755 for (dl
= ddf
->dlist
; dl
&& cnt
< raiddisks
; dl
=dl
->next
) {
2756 if (dl
->esize
< size
)
2766 validate_geometry_ddf_container(struct supertype
*st
,
2767 int level
, int layout
, int raiddisks
,
2768 int chunk
, unsigned long long size
,
2769 unsigned long long data_offset
,
2770 char *dev
, unsigned long long *freesize
,
2773 static int validate_geometry_ddf_bvd(struct supertype
*st
,
2774 int level
, int layout
, int raiddisks
,
2775 int *chunk
, unsigned long long size
,
2776 unsigned long long data_offset
,
2777 char *dev
, unsigned long long *freesize
,
2780 static int validate_geometry_ddf(struct supertype
*st
,
2781 int level
, int layout
, int raiddisks
,
2782 int *chunk
, unsigned long long size
,
2783 unsigned long long data_offset
,
2784 char *dev
, unsigned long long *freesize
,
2791 /* ddf potentially supports lots of things, but it depends on
2792 * what devices are offered (and maybe kernel version?)
2793 * If given unused devices, we will make a container.
2794 * If given devices in a container, we will make a BVD.
2795 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
2798 if (chunk
&& *chunk
== UnSet
)
2799 *chunk
= DEFAULT_CHUNK
;
2801 if (level
== -1000000) level
= LEVEL_CONTAINER
;
2802 if (level
== LEVEL_CONTAINER
) {
2803 /* Must be a fresh device to add to a container */
2804 return validate_geometry_ddf_container(st
, level
, layout
,
2805 raiddisks
, chunk
?*chunk
:0,
2806 size
, data_offset
, dev
,
2812 /* Initial sanity check. Exclude illegal levels. */
2814 for (i
=0; ddf_level_num
[i
].num1
!= MAXINT
; i
++)
2815 if (ddf_level_num
[i
].num2
== level
)
2817 if (ddf_level_num
[i
].num1
== MAXINT
) {
2819 pr_err("DDF does not support level %d arrays\n",
2823 /* Should check layout? etc */
2825 if (st
->sb
&& freesize
) {
2826 /* --create was given a container to create in.
2827 * So we need to check that there are enough
2828 * free spaces and return the amount of space.
2829 * We may as well remember which drives were
2830 * chosen so that add_to_super/getinfo_super
2833 return reserve_space(st
, raiddisks
, size
, chunk
?*chunk
:0, freesize
);
2839 /* A container has already been opened, so we are
2840 * creating in there. Maybe a BVD, maybe an SVD.
2841 * Should make a distinction one day.
2843 return validate_geometry_ddf_bvd(st
, level
, layout
, raiddisks
,
2844 chunk
, size
, data_offset
, dev
,
2848 /* This is the first device for the array.
2849 * If it is a container, we read it in and do automagic allocations,
2850 * no other devices should be given.
2851 * Otherwise it must be a member device of a container, and we
2852 * do manual allocation.
2853 * Later we should check for a BVD and make an SVD.
2855 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2857 sra
= sysfs_read(fd
, NULL
, GET_VERSION
);
2859 if (sra
&& sra
->array
.major_version
== -1 &&
2860 strcmp(sra
->text_version
, "ddf") == 0) {
2863 /* find space for 'n' devices. */
2864 /* remember the devices */
2865 /* Somehow return the fact that we have enough */
2869 pr_err("ddf: Cannot create this array "
2870 "on device %s - a container is required.\n",
2874 if (errno
!= EBUSY
|| (fd
= open(dev
, O_RDONLY
, 0)) < 0) {
2876 pr_err("ddf: Cannot open %s: %s\n",
2877 dev
, strerror(errno
));
2880 /* Well, it is in use by someone, maybe a 'ddf' container. */
2881 cfd
= open_container(fd
);
2885 pr_err("ddf: Cannot use %s: %s\n",
2886 dev
, strerror(EBUSY
));
2889 sra
= sysfs_read(cfd
, NULL
, GET_VERSION
);
2891 if (sra
&& sra
->array
.major_version
== -1 &&
2892 strcmp(sra
->text_version
, "ddf") == 0) {
2893 /* This is a member of a ddf container. Load the container
2894 * and try to create a bvd
2896 struct ddf_super
*ddf
;
2897 if (load_super_ddf_all(st
, cfd
, (void **)&ddf
, NULL
) == 0) {
2899 strcpy(st
->container_devnm
, fd2devnm(cfd
));
2901 return validate_geometry_ddf_bvd(st
, level
, layout
,
2902 raiddisks
, chunk
, size
,
2908 } else /* device may belong to a different container */
2915 validate_geometry_ddf_container(struct supertype
*st
,
2916 int level
, int layout
, int raiddisks
,
2917 int chunk
, unsigned long long size
,
2918 unsigned long long data_offset
,
2919 char *dev
, unsigned long long *freesize
,
2923 unsigned long long ldsize
;
2925 if (level
!= LEVEL_CONTAINER
)
2930 fd
= open(dev
, O_RDONLY
|O_EXCL
, 0);
2933 pr_err("ddf: Cannot open %s: %s\n",
2934 dev
, strerror(errno
));
2937 if (!get_dev_size(fd
, dev
, &ldsize
)) {
2943 *freesize
= avail_size_ddf(st
, ldsize
>> 9, INVALID_SECTORS
);
2950 static int validate_geometry_ddf_bvd(struct supertype
*st
,
2951 int level
, int layout
, int raiddisks
,
2952 int *chunk
, unsigned long long size
,
2953 unsigned long long data_offset
,
2954 char *dev
, unsigned long long *freesize
,
2958 struct ddf_super
*ddf
= st
->sb
;
2960 unsigned long long pos
= 0;
2961 unsigned long long maxsize
;
2964 /* ddf/bvd supports lots of things, but not containers */
2965 if (level
== LEVEL_CONTAINER
) {
2967 pr_err("DDF cannot create a container within an container\n");
2970 /* We must have the container info already read in. */
2975 /* General test: make sure there is space for
2976 * 'raiddisks' device extents of size 'size'.
2978 unsigned long long minsize
= size
;
2982 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
2988 e
= get_extents(ddf
, dl
);
2991 unsigned long long esize
;
2992 esize
= e
[i
].start
- pos
;
2993 if (esize
>= minsize
)
2995 pos
= e
[i
].start
+ e
[i
].size
;
2997 } while (e
[i
-1].size
);
3002 if (dcnt
< raiddisks
) {
3004 pr_err("ddf: Not enough devices with "
3005 "space for this array (%d < %d)\n",
3011 /* This device must be a member of the set */
3012 if (stat(dev
, &stb
) < 0)
3014 if ((S_IFMT
& stb
.st_mode
) != S_IFBLK
)
3016 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
3017 if (dl
->major
== (int)major(stb
.st_rdev
) &&
3018 dl
->minor
== (int)minor(stb
.st_rdev
))
3023 pr_err("ddf: %s is not in the "
3028 e
= get_extents(ddf
, dl
);
3032 unsigned long long esize
;
3033 esize
= e
[i
].start
- pos
;
3034 if (esize
>= maxsize
)
3036 pos
= e
[i
].start
+ e
[i
].size
;
3038 } while (e
[i
-1].size
);
3039 *freesize
= maxsize
;
3045 static int load_super_ddf_all(struct supertype
*st
, int fd
,
3046 void **sbp
, char *devname
)
3049 struct ddf_super
*super
;
3050 struct mdinfo
*sd
, *best
= NULL
;
3056 sra
= sysfs_read(fd
, 0, GET_LEVEL
|GET_VERSION
|GET_DEVS
|GET_STATE
);
3059 if (sra
->array
.major_version
!= -1 ||
3060 sra
->array
.minor_version
!= -2 ||
3061 strcmp(sra
->text_version
, "ddf") != 0)
3064 if (posix_memalign((void**)&super
, 512, sizeof(*super
)) != 0)
3066 memset(super
, 0, sizeof(*super
));
3068 /* first, try each device, and choose the best ddf */
3069 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3071 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3072 dfd
= dev_open(nm
, O_RDONLY
);
3075 rv
= load_ddf_headers(dfd
, super
, NULL
);
3078 seq
= __be32_to_cpu(super
->active
->seq
);
3079 if (super
->active
->openflag
)
3081 if (!best
|| seq
> bestseq
) {
3089 /* OK, load this ddf */
3090 sprintf(nm
, "%d:%d", best
->disk
.major
, best
->disk
.minor
);
3091 dfd
= dev_open(nm
, O_RDONLY
);
3094 load_ddf_headers(dfd
, super
, NULL
);
3095 load_ddf_global(dfd
, super
, NULL
);
3097 /* Now we need the device-local bits */
3098 for (sd
= sra
->devs
; sd
; sd
= sd
->next
) {
3101 sprintf(nm
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
3102 dfd
= dev_open(nm
, O_RDWR
);
3105 rv
= load_ddf_headers(dfd
, super
, NULL
);
3107 rv
= load_ddf_local(dfd
, super
, NULL
, 1);
3113 if (st
->ss
== NULL
) {
3114 st
->ss
= &super_ddf
;
3115 st
->minor_version
= 0;
3118 strcpy(st
->container_devnm
, fd2devnm(fd
));
3122 static int load_container_ddf(struct supertype
*st
, int fd
,
3125 return load_super_ddf_all(st
, fd
, &st
->sb
, devname
);
3128 #endif /* MDASSEMBLE */
3130 static int check_secondary(const struct vcl
*vc
)
3132 const struct vd_config
*conf
= &vc
->conf
;
3135 /* The only DDF secondary RAID level md can support is
3136 * RAID 10, if the stripe sizes and Basic volume sizes
3138 * Other configurations could in theory be supported by exposing
3139 * the BVDs to user space and using device mapper for the secondary
3140 * mapping. So far we don't support that.
3143 __u64 sec_elements
[4] = {0, 0, 0, 0};
3144 #define __set_sec_seen(n) (sec_elements[(n)>>6] |= (1<<((n)&63)))
3145 #define __was_sec_seen(n) ((sec_elements[(n)>>6] & (1<<((n)&63))) != 0)
3147 if (vc
->other_bvds
== NULL
) {
3148 pr_err("No BVDs for secondary RAID found\n");
3151 if (conf
->prl
!= DDF_RAID1
) {
3152 pr_err("Secondary RAID level only supported for mirrored BVD\n");
3155 if (conf
->srl
!= DDF_2STRIPED
&& conf
->srl
!= DDF_2SPANNED
) {
3156 pr_err("Secondary RAID level %d is unsupported\n",
3160 __set_sec_seen(conf
->sec_elmnt_seq
);
3161 for (i
= 0; i
< conf
->sec_elmnt_count
-1; i
++) {
3162 const struct vd_config
*bvd
= vc
->other_bvds
[i
];
3164 pr_err("BVD %d is missing\n", i
+1);
3167 if (bvd
->srl
!= conf
->srl
) {
3168 pr_err("Inconsistent secondary RAID level across BVDs\n");
3171 if (bvd
->prl
!= conf
->prl
) {
3172 pr_err("Different RAID levels for BVDs are unsupported\n");
3175 if (bvd
->prim_elmnt_count
!= conf
->prim_elmnt_count
) {
3176 pr_err("All BVDs must have the same number of primary elements\n");
3179 if (bvd
->chunk_shift
!= conf
->chunk_shift
) {
3180 pr_err("Different strip sizes for BVDs are unsupported\n");
3183 if (bvd
->array_blocks
!= conf
->array_blocks
) {
3184 pr_err("Different BVD sizes are unsupported\n");
3187 __set_sec_seen(bvd
->sec_elmnt_seq
);
3189 for (i
= 0; i
< conf
->sec_elmnt_count
; i
++) {
3190 if (!__was_sec_seen(i
)) {
3191 pr_err("BVD %d is missing\n", i
);
3198 #define NO_SUCH_REFNUM (0xFFFFFFFF)
3199 static unsigned int get_pd_index_from_refnum(const struct vcl
*vc
,
3200 __u32 refnum
, unsigned int nmax
,
3201 const struct vd_config
**bvd
,
3204 unsigned int i
, j
, n
, sec
, cnt
;
3206 cnt
= __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3207 sec
= (vc
->conf
.sec_elmnt_count
== 1 ? 0 : vc
->conf
.sec_elmnt_seq
);
3209 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3210 /* j counts valid entries for this BVD */
3211 if (vc
->conf
.phys_refnum
[i
] != 0xffffffff)
3213 if (vc
->conf
.phys_refnum
[i
] == refnum
) {
3216 return sec
* cnt
+ j
- 1;
3219 if (vc
->other_bvds
== NULL
)
3222 for (n
= 1; n
< vc
->conf
.sec_elmnt_count
; n
++) {
3223 struct vd_config
*vd
= vc
->other_bvds
[n
-1];
3226 sec
= vd
->sec_elmnt_seq
;
3227 for (i
= 0, j
= 0 ; i
< nmax
; i
++) {
3228 if (vd
->phys_refnum
[i
] != 0xffffffff)
3230 if (vd
->phys_refnum
[i
] == refnum
) {
3233 return sec
* cnt
+ j
- 1;
3239 return NO_SUCH_REFNUM
;
3242 static struct mdinfo
*container_content_ddf(struct supertype
*st
, char *subarray
)
3244 /* Given a container loaded by load_super_ddf_all,
3245 * extract information about all the arrays into
3248 * For each vcl in conflist: create an mdinfo, fill it in,
3249 * then look for matching devices (phys_refnum) in dlist
3250 * and create appropriate device mdinfo.
3252 struct ddf_super
*ddf
= st
->sb
;
3253 struct mdinfo
*rest
= NULL
;
3256 for (vc
= ddf
->conflist
; vc
; vc
=vc
->next
)
3260 struct mdinfo
*this;
3266 (strtoul(subarray
, &ep
, 10) != vc
->vcnum
||
3270 if (vc
->conf
.sec_elmnt_count
> 1) {
3271 if (check_secondary(vc
) != 0)
3275 this = xcalloc(1, sizeof(*this));
3279 if (vc
->conf
.sec_elmnt_count
== 1) {
3280 this->array
.level
= map_num1(ddf_level_num
,
3282 this->array
.raid_disks
=
3283 __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3284 this->array
.layout
=
3285 rlq_to_layout(vc
->conf
.rlq
, vc
->conf
.prl
,
3286 this->array
.raid_disks
);
3288 /* The only supported layout is RAID 10.
3289 * Compatibility has been checked in check_secondary()
3292 this->array
.level
= 10;
3293 this->array
.raid_disks
=
3294 __be16_to_cpu(vc
->conf
.prim_elmnt_count
)
3295 * vc
->conf
.sec_elmnt_count
;
3296 this->array
.layout
= 0x100 |
3297 __be16_to_cpu(vc
->conf
.prim_elmnt_count
);
3299 this->array
.md_minor
= -1;
3300 this->array
.major_version
= -1;
3301 this->array
.minor_version
= -2;
3302 cptr
= (__u32
*)(vc
->conf
.guid
+ 16);
3303 this->array
.ctime
= DECADE
+ __be32_to_cpu(*cptr
);
3304 this->array
.utime
= DECADE
+
3305 __be32_to_cpu(vc
->conf
.timestamp
);
3306 this->array
.chunk_size
= 512 << vc
->conf
.chunk_shift
;
3309 if ((ddf
->virt
->entries
[i
].state
& DDF_state_inconsistent
) ||
3310 (ddf
->virt
->entries
[i
].init_state
& DDF_initstate_mask
) !=
3312 this->array
.state
= 0;
3313 this->resync_start
= 0;
3315 this->array
.state
= 1;
3316 this->resync_start
= MaxSector
;
3318 memcpy(this->name
, ddf
->virt
->entries
[i
].name
, 16);
3321 if (this->name
[j
] == ' ')
3324 memset(this->uuid
, 0, sizeof(this->uuid
));
3325 this->component_size
= __be64_to_cpu(vc
->conf
.blocks
);
3326 this->array
.size
= this->component_size
/ 2;
3327 this->container_member
= i
;
3329 ddf
->currentconf
= vc
;
3330 uuid_from_super_ddf(st
, this->uuid
);
3331 ddf
->currentconf
= NULL
;
3333 sprintf(this->text_version
, "/%s/%d",
3334 st
->container_devnm
, this->container_member
);
3336 for (pd
= 0; pd
< __be16_to_cpu(ddf
->phys
->used_pdes
); pd
++) {
3339 const struct vd_config
*bvd
;
3344 if (ddf
->phys
->entries
[pd
].refnum
== 0xFFFFFFFF)
3347 stt
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3348 if ((stt
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3352 i
= get_pd_index_from_refnum(
3353 vc
, ddf
->phys
->entries
[pd
].refnum
,
3354 ddf
->mppe
, &bvd
, &iphys
);
3355 if (i
== NO_SUCH_REFNUM
)
3358 this->array
.working_disks
++;
3360 for (d
= ddf
->dlist
; d
; d
=d
->next
)
3361 if (d
->disk
.refnum
==
3362 ddf
->phys
->entries
[pd
].refnum
)
3365 /* Haven't found that one yet, maybe there are others */
3368 dev
= xcalloc(1, sizeof(*dev
));
3369 dev
->next
= this->devs
;
3372 dev
->disk
.number
= __be32_to_cpu(d
->disk
.refnum
);
3373 dev
->disk
.major
= d
->major
;
3374 dev
->disk
.minor
= d
->minor
;
3375 dev
->disk
.raid_disk
= i
;
3376 dev
->disk
.state
= (1<<MD_DISK_SYNC
)|(1<<MD_DISK_ACTIVE
);
3377 dev
->recovery_start
= MaxSector
;
3379 dev
->events
= __be32_to_cpu(ddf
->primary
.seq
);
3380 lba_offset
= (__u64
*)&bvd
->phys_refnum
[ddf
->mppe
];
3381 dev
->data_offset
= __be64_to_cpu(lba_offset
[iphys
]);
3382 dev
->component_size
= __be64_to_cpu(bvd
->blocks
);
3384 strcpy(dev
->name
, d
->devname
);
3390 static int store_super_ddf(struct supertype
*st
, int fd
)
3392 struct ddf_super
*ddf
= st
->sb
;
3393 unsigned long long dsize
;
3400 /* ->dlist and ->conflist will be set for updates, currently not
3403 if (ddf
->dlist
|| ddf
->conflist
)
3406 if (!get_dev_size(fd
, NULL
, &dsize
))
3409 if (posix_memalign(&buf
, 512, 512) != 0)
3411 memset(buf
, 0, 512);
3413 lseek64(fd
, dsize
-512, 0);
3414 rc
= write(fd
, buf
, 512);
3421 static int compare_super_ddf(struct supertype
*st
, struct supertype
*tst
)
3425 * 0 same, or first was empty, and second was copied
3426 * 1 second had wrong number
3428 * 3 wrong other info
3430 struct ddf_super
*first
= st
->sb
;
3431 struct ddf_super
*second
= tst
->sb
;
3432 struct dl
*dl1
, *dl2
;
3433 struct vcl
*vl1
, *vl2
;
3434 unsigned int max_vds
, max_pds
, pd
, vd
;
3442 if (memcmp(first
->anchor
.guid
, second
->anchor
.guid
, DDF_GUID_LEN
) != 0)
3445 if (first
->anchor
.seq
!= second
->anchor
.seq
) {
3446 dprintf("%s: sequence number mismatch %u/%u\n", __func__
,
3447 __be32_to_cpu(first
->anchor
.seq
),
3448 __be32_to_cpu(second
->anchor
.seq
));
3451 if (first
->max_part
!= second
->max_part
||
3452 first
->phys
->used_pdes
!= second
->phys
->used_pdes
||
3453 first
->virt
->populated_vdes
!= second
->virt
->populated_vdes
) {
3454 dprintf("%s: PD/VD number mismatch\n", __func__
);
3458 max_pds
= __be16_to_cpu(first
->phys
->used_pdes
);
3459 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3460 for (pd
= 0; pd
< max_pds
; pd
++)
3461 if (first
->phys
->entries
[pd
].refnum
== dl2
->disk
.refnum
)
3463 if (pd
== max_pds
) {
3464 dprintf("%s: no match for disk %08x\n", __func__
,
3465 __be32_to_cpu(dl2
->disk
.refnum
));
3470 max_vds
= __be16_to_cpu(first
->active
->max_vd_entries
);
3471 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3472 if (vl2
->conf
.magic
!= DDF_VD_CONF_MAGIC
)
3474 for (vd
= 0; vd
< max_vds
; vd
++)
3475 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3476 vl2
->conf
.guid
, DDF_GUID_LEN
))
3478 if (vd
== max_vds
) {
3479 dprintf("%s: no match for VD config\n", __func__
);
3483 /* FIXME should I look at anything else? */
3486 At this point we are fairly sure that the meta data matches.
3487 But the new disk may contain additional local data.
3488 Add it to the super block.
3490 for (vl2
= second
->conflist
; vl2
; vl2
= vl2
->next
) {
3491 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
)
3492 if (!memcmp(vl1
->conf
.guid
, vl2
->conf
.guid
,
3496 if (vl1
->other_bvds
!= NULL
&&
3497 vl1
->conf
.sec_elmnt_seq
!=
3498 vl2
->conf
.sec_elmnt_seq
) {
3499 dprintf("%s: adding BVD %u\n", __func__
,
3500 vl2
->conf
.sec_elmnt_seq
);
3501 add_other_bvd(vl1
, &vl2
->conf
,
3502 first
->conf_rec_len
*512);
3507 if (posix_memalign((void **)&vl1
, 512,
3508 (first
->conf_rec_len
*512 +
3509 offsetof(struct vcl
, conf
))) != 0) {
3510 pr_err("%s could not allocate vcl buf\n",
3515 vl1
->next
= first
->conflist
;
3516 vl1
->block_sizes
= NULL
;
3517 if (vl2
->conf
.sec_elmnt_count
> 1) {
3518 vl1
->other_bvds
= xcalloc(vl2
->conf
.sec_elmnt_count
- 1,
3519 sizeof(struct vd_config
*));
3521 vl1
->other_bvds
= NULL
;
3522 memcpy(&vl1
->conf
, &vl2
->conf
, first
->conf_rec_len
*512);
3523 vl1
->lba_offset
= (__u64
*)
3524 &vl1
->conf
.phys_refnum
[first
->mppe
];
3525 for (vd
= 0; vd
< max_vds
; vd
++)
3526 if (!memcmp(first
->virt
->entries
[vd
].guid
,
3527 vl1
->conf
.guid
, DDF_GUID_LEN
))
3530 dprintf("%s: added config for VD %u\n", __func__
, vl1
->vcnum
);
3531 first
->conflist
= vl1
;
3534 for (dl2
= second
->dlist
; dl2
; dl2
= dl2
->next
) {
3535 for (dl1
= first
->dlist
; dl1
; dl1
= dl1
->next
)
3536 if (dl1
->disk
.refnum
== dl2
->disk
.refnum
)
3541 if (posix_memalign((void **)&dl1
, 512,
3542 sizeof(*dl1
) + (first
->max_part
) * sizeof(dl1
->vlist
[0]))
3544 pr_err("%s could not allocate disk info buffer\n",
3548 memcpy(dl1
, dl2
, sizeof(*dl1
));
3549 dl1
->mdupdate
= NULL
;
3550 dl1
->next
= first
->dlist
;
3552 for (pd
= 0; pd
< max_pds
; pd
++)
3553 if (first
->phys
->entries
[pd
].refnum
== dl1
->disk
.refnum
)
3557 if (posix_memalign((void **)&dl1
->spare
, 512,
3558 first
->conf_rec_len
*512) != 0) {
3559 pr_err("%s could not allocate spare info buf\n",
3563 memcpy(dl1
->spare
, dl2
->spare
, first
->conf_rec_len
*512);
3565 for (vd
= 0 ; vd
< first
->max_part
; vd
++) {
3566 if (!dl2
->vlist
[vd
]) {
3567 dl1
->vlist
[vd
] = NULL
;
3570 for (vl1
= first
->conflist
; vl1
; vl1
= vl1
->next
) {
3571 if (!memcmp(vl1
->conf
.guid
,
3572 dl2
->vlist
[vd
]->conf
.guid
,
3575 dl1
->vlist
[vd
] = vl1
;
3579 dprintf("%s: added disk %d: %08x\n", __func__
, dl1
->pdnum
,
3588 * A new array 'a' has been started which claims to be instance 'inst'
3589 * within container 'c'.
3590 * We need to confirm that the array matches the metadata in 'c' so
3591 * that we don't corrupt any metadata.
3593 static int ddf_open_new(struct supertype
*c
, struct active_array
*a
, char *inst
)
3595 dprintf("ddf: open_new %s\n", inst
);
3596 a
->info
.container_member
= atoi(inst
);
3601 * The array 'a' is to be marked clean in the metadata.
3602 * If '->resync_start' is not ~(unsigned long long)0, then the array is only
3603 * clean up to the point (in sectors). If that cannot be recorded in the
3604 * metadata, then leave it as dirty.
3606 * For DDF, we need to clear the DDF_state_inconsistent bit in the
3607 * !global! virtual_disk.virtual_entry structure.
3609 static int ddf_set_array_state(struct active_array
*a
, int consistent
)
3611 struct ddf_super
*ddf
= a
->container
->sb
;
3612 int inst
= a
->info
.container_member
;
3613 int old
= ddf
->virt
->entries
[inst
].state
;
3614 if (consistent
== 2) {
3615 /* Should check if a recovery should be started FIXME */
3617 if (!is_resync_complete(&a
->info
))
3621 ddf
->virt
->entries
[inst
].state
&= ~DDF_state_inconsistent
;
3623 ddf
->virt
->entries
[inst
].state
|= DDF_state_inconsistent
;
3624 if (old
!= ddf
->virt
->entries
[inst
].state
)
3625 ddf_set_updates_pending(ddf
);
3627 old
= ddf
->virt
->entries
[inst
].init_state
;
3628 ddf
->virt
->entries
[inst
].init_state
&= ~DDF_initstate_mask
;
3629 if (is_resync_complete(&a
->info
))
3630 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_full
;
3631 else if (a
->info
.resync_start
== 0)
3632 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_not
;
3634 ddf
->virt
->entries
[inst
].init_state
|= DDF_init_quick
;
3635 if (old
!= ddf
->virt
->entries
[inst
].init_state
)
3636 ddf_set_updates_pending(ddf
);
3638 dprintf("ddf mark %d %s %llu\n", inst
, consistent
?"clean":"dirty",
3639 a
->info
.resync_start
);
3643 #define container_of(ptr, type, member) ({ \
3644 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
3645 (type *)( (char *)__mptr - offsetof(type,member) );})
3647 * The state of each disk is stored in the global phys_disk structure
3648 * in phys_disk.entries[n].state.
3649 * This makes various combinations awkward.
3650 * - When a device fails in any array, it must be failed in all arrays
3651 * that include a part of this device.
3652 * - When a component is rebuilding, we cannot include it officially in the
3653 * array unless this is the only array that uses the device.
3655 * So: when transitioning:
3656 * Online -> failed, just set failed flag. monitor will propagate
3657 * spare -> online, the device might need to be added to the array.
3658 * spare -> failed, just set failed. Don't worry if in array or not.
3660 static void ddf_set_disk(struct active_array
*a
, int n
, int state
)
3662 struct ddf_super
*ddf
= a
->container
->sb
;
3663 unsigned int inst
= a
->info
.container_member
;
3664 struct vd_config
*vc
= find_vdcr(ddf
, inst
);
3665 int pd
= find_phys(ddf
, vc
->phys_refnum
[n
]);
3671 dprintf("ddf: cannot find instance %d!!\n", inst
);
3674 /* Find the matching slot in 'info'. */
3675 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
3676 if (mdi
->disk
.raid_disk
== n
)
3681 /* and find the 'dl' entry corresponding to that. */
3682 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
3683 if (mdi
->state_fd
>= 0 &&
3684 mdi
->disk
.major
== dl
->major
&&
3685 mdi
->disk
.minor
== dl
->minor
)
3690 if (pd
< 0 || pd
!= dl
->pdnum
) {
3691 /* disk doesn't currently exist or has changed.
3692 * If it is now in_sync, insert it. */
3693 if ((state
& DS_INSYNC
) && ! (state
& DS_FAULTY
)) {
3696 vc
->phys_refnum
[n
] = dl
->disk
.refnum
;
3697 vcl
= container_of(vc
, struct vcl
, conf
);
3698 vcl
->lba_offset
[n
] = mdi
->data_offset
;
3699 ddf
->phys
->entries
[pd
].type
&=
3700 ~__cpu_to_be16(DDF_Global_Spare
);
3701 ddf
->phys
->entries
[pd
].type
|=
3702 __cpu_to_be16(DDF_Active_in_VD
);
3703 ddf_set_updates_pending(ddf
);
3706 int old
= ddf
->phys
->entries
[pd
].state
;
3707 if (state
& DS_FAULTY
)
3708 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Failed
);
3709 if (state
& DS_INSYNC
) {
3710 ddf
->phys
->entries
[pd
].state
|= __cpu_to_be16(DDF_Online
);
3711 ddf
->phys
->entries
[pd
].state
&= __cpu_to_be16(~DDF_Rebuilding
);
3713 if (old
!= ddf
->phys
->entries
[pd
].state
)
3714 ddf_set_updates_pending(ddf
);
3717 dprintf("ddf: set_disk %d to %x\n", n
, state
);
3719 /* Now we need to check the state of the array and update
3720 * virtual_disk.entries[n].state.
3721 * It needs to be one of "optimal", "degraded", "failed".
3722 * I don't understand 'deleted' or 'missing'.
3725 for (i
=0; i
< a
->info
.array
.raid_disks
; i
++) {
3726 pd
= find_phys(ddf
, vc
->phys_refnum
[i
]);
3729 st
= __be16_to_cpu(ddf
->phys
->entries
[pd
].state
);
3730 if ((st
& (DDF_Online
|DDF_Failed
|DDF_Rebuilding
))
3734 state
= DDF_state_degraded
;
3735 if (working
== a
->info
.array
.raid_disks
)
3736 state
= DDF_state_optimal
;
3737 else switch(vc
->prl
) {
3741 state
= DDF_state_failed
;
3745 state
= DDF_state_failed
;
3746 else if (working
== 2 && state
== DDF_state_degraded
)
3747 state
= DDF_state_part_optimal
;
3751 if (working
< a
->info
.array
.raid_disks
-1)
3752 state
= DDF_state_failed
;
3755 if (working
< a
->info
.array
.raid_disks
-2)
3756 state
= DDF_state_failed
;
3757 else if (working
== a
->info
.array
.raid_disks
-1)
3758 state
= DDF_state_part_optimal
;
3762 if (ddf
->virt
->entries
[inst
].state
!=
3763 ((ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
3766 ddf
->virt
->entries
[inst
].state
=
3767 (ddf
->virt
->entries
[inst
].state
& ~DDF_state_mask
)
3769 ddf_set_updates_pending(ddf
);
3774 static void ddf_sync_metadata(struct supertype
*st
)
3778 * Write all data to all devices.
3779 * Later, we might be able to track whether only local changes
3780 * have been made, or whether any global data has been changed,
3781 * but ddf is sufficiently weird that it probably always
3782 * changes global data ....
3784 struct ddf_super
*ddf
= st
->sb
;
3785 if (!ddf
->updates_pending
)
3787 ddf
->updates_pending
= 0;
3788 __write_init_super_ddf(st
);
3789 dprintf("ddf: sync_metadata\n");
3792 static void ddf_process_update(struct supertype
*st
,
3793 struct metadata_update
*update
)
3795 /* Apply this update to the metadata.
3796 * The first 4 bytes are a DDF_*_MAGIC which guides
3798 * Possible update are:
3799 * DDF_PHYS_RECORDS_MAGIC
3800 * Add a new physical device or remove an old one.
3801 * Changes to this record only happen implicitly.
3802 * used_pdes is the device number.
3803 * DDF_VIRT_RECORDS_MAGIC
3804 * Add a new VD. Possibly also change the 'access' bits.
3805 * populated_vdes is the entry number.
3807 * New or updated VD. the VIRT_RECORD must already
3808 * exist. For an update, phys_refnum and lba_offset
3809 * (at least) are updated, and the VD_CONF must
3810 * be written to precisely those devices listed with
3812 * DDF_SPARE_ASSIGN_MAGIC
3813 * replacement Spare Assignment Record... but for which device?
3816 * - to create a new array, we send a VIRT_RECORD and
3817 * a VD_CONF. Then assemble and start the array.
3818 * - to activate a spare we send a VD_CONF to add the phys_refnum
3819 * and offset. This will also mark the spare as active with
3820 * a spare-assignment record.
3822 struct ddf_super
*ddf
= st
->sb
;
3823 __u32
*magic
= (__u32
*)update
->buf
;
3824 struct phys_disk
*pd
;
3825 struct virtual_disk
*vd
;
3826 struct vd_config
*vc
;
3831 unsigned int pdnum
, pd2
;
3833 dprintf("Process update %x\n", *magic
);
3836 case DDF_PHYS_RECORDS_MAGIC
:
3838 if (update
->len
!= (sizeof(struct phys_disk
) +
3839 sizeof(struct phys_disk_entry
)))
3841 pd
= (struct phys_disk
*)update
->buf
;
3843 ent
= __be16_to_cpu(pd
->used_pdes
);
3844 if (ent
>= __be16_to_cpu(ddf
->phys
->max_pdes
))
3846 if (pd
->entries
[0].state
& __cpu_to_be16(DDF_Missing
)) {
3848 /* removing this disk. */
3849 ddf
->phys
->entries
[ent
].state
|= __cpu_to_be16(DDF_Missing
);
3850 for (dlp
= &ddf
->dlist
; *dlp
; dlp
= &(*dlp
)->next
) {
3851 struct dl
*dl
= *dlp
;
3852 if (dl
->pdnum
== (signed)ent
) {
3855 /* FIXME this doesn't free
3862 ddf_set_updates_pending(ddf
);
3865 if (!all_ff(ddf
->phys
->entries
[ent
].guid
))
3867 ddf
->phys
->entries
[ent
] = pd
->entries
[0];
3868 ddf
->phys
->used_pdes
= __cpu_to_be16(1 +
3869 __be16_to_cpu(ddf
->phys
->used_pdes
));
3870 ddf_set_updates_pending(ddf
);
3871 if (ddf
->add_list
) {
3872 struct active_array
*a
;
3873 struct dl
*al
= ddf
->add_list
;
3874 ddf
->add_list
= al
->next
;
3876 al
->next
= ddf
->dlist
;
3879 /* As a device has been added, we should check
3880 * for any degraded devices that might make
3881 * use of this spare */
3882 for (a
= st
->arrays
; a
; a
=a
->next
)
3883 a
->check_degraded
= 1;
3887 case DDF_VIRT_RECORDS_MAGIC
:
3889 if (update
->len
!= (sizeof(struct virtual_disk
) +
3890 sizeof(struct virtual_entry
)))
3892 vd
= (struct virtual_disk
*)update
->buf
;
3894 ent
= __be16_to_cpu(vd
->populated_vdes
);
3895 if (ent
>= __be16_to_cpu(ddf
->virt
->max_vdes
))
3897 if (!all_ff(ddf
->virt
->entries
[ent
].guid
))
3899 ddf
->virt
->entries
[ent
] = vd
->entries
[0];
3900 ddf
->virt
->populated_vdes
= __cpu_to_be16(1 +
3901 __be16_to_cpu(ddf
->virt
->populated_vdes
));
3902 ddf_set_updates_pending(ddf
);
3905 case DDF_VD_CONF_MAGIC
:
3906 dprintf("len %d %d\n", update
->len
, ddf
->conf_rec_len
);
3908 mppe
= __be16_to_cpu(ddf
->anchor
.max_primary_element_entries
);
3909 if ((unsigned)update
->len
!= ddf
->conf_rec_len
* 512)
3911 vc
= (struct vd_config
*)update
->buf
;
3912 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
3913 if (memcmp(vcl
->conf
.guid
, vc
->guid
, DDF_GUID_LEN
) == 0)
3915 dprintf("vcl = %p\n", vcl
);
3917 /* An update, just copy the phys_refnum and lba_offset
3920 memcpy(vcl
->conf
.phys_refnum
, vc
->phys_refnum
,
3921 mppe
* (sizeof(__u32
) + sizeof(__u64
)));
3926 vcl
= update
->space
;
3927 update
->space
= NULL
;
3928 vcl
->next
= ddf
->conflist
;
3929 memcpy(&vcl
->conf
, vc
, update
->len
);
3930 vcl
->lba_offset
= (__u64
*)
3931 &vcl
->conf
.phys_refnum
[mppe
];
3933 ent
< __be16_to_cpu(ddf
->virt
->populated_vdes
);
3935 if (memcmp(vc
->guid
, ddf
->virt
->entries
[ent
].guid
,
3936 DDF_GUID_LEN
) == 0) {
3940 ddf
->conflist
= vcl
;
3942 /* Set DDF_Transition on all Failed devices - to help
3943 * us detect those that are no longer in use
3945 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
3946 if (ddf
->phys
->entries
[pdnum
].state
3947 & __be16_to_cpu(DDF_Failed
))
3948 ddf
->phys
->entries
[pdnum
].state
3949 |= __be16_to_cpu(DDF_Transition
);
3950 /* Now make sure vlist is correct for each dl. */
3951 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
) {
3953 unsigned int vn
= 0;
3954 int in_degraded
= 0;
3955 for (vcl
= ddf
->conflist
; vcl
; vcl
= vcl
->next
)
3956 for (dn
=0; dn
< ddf
->mppe
; dn
++)
3957 if (vcl
->conf
.phys_refnum
[dn
] ==
3960 dprintf("dev %d has %p at %d\n",
3961 dl
->pdnum
, vcl
, vn
);
3962 /* Clear the Transition flag */
3963 if (ddf
->phys
->entries
[dl
->pdnum
].state
3964 & __be16_to_cpu(DDF_Failed
))
3965 ddf
->phys
->entries
[dl
->pdnum
].state
&=
3966 ~__be16_to_cpu(DDF_Transition
);
3968 dl
->vlist
[vn
++] = vcl
;
3969 vstate
= ddf
->virt
->entries
[vcl
->vcnum
].state
3971 if (vstate
== DDF_state_degraded
||
3972 vstate
== DDF_state_part_optimal
)
3976 while (vn
< ddf
->max_part
)
3977 dl
->vlist
[vn
++] = NULL
;
3979 ddf
->phys
->entries
[dl
->pdnum
].type
&=
3980 ~__cpu_to_be16(DDF_Global_Spare
);
3981 if (!(ddf
->phys
->entries
[dl
->pdnum
].type
&
3982 __cpu_to_be16(DDF_Active_in_VD
))) {
3983 ddf
->phys
->entries
[dl
->pdnum
].type
|=
3984 __cpu_to_be16(DDF_Active_in_VD
);
3986 ddf
->phys
->entries
[dl
->pdnum
].state
|=
3987 __cpu_to_be16(DDF_Rebuilding
);
3991 ddf
->phys
->entries
[dl
->pdnum
].type
&=
3992 ~__cpu_to_be16(DDF_Global_Spare
);
3993 ddf
->phys
->entries
[dl
->pdnum
].type
|=
3994 __cpu_to_be16(DDF_Spare
);
3996 if (!dl
->vlist
[0] && !dl
->spare
) {
3997 ddf
->phys
->entries
[dl
->pdnum
].type
|=
3998 __cpu_to_be16(DDF_Global_Spare
);
3999 ddf
->phys
->entries
[dl
->pdnum
].type
&=
4000 ~__cpu_to_be16(DDF_Spare
|
4005 /* Now remove any 'Failed' devices that are not part
4006 * of any VD. They will have the Transition flag set.
4007 * Once done, we need to update all dl->pdnum numbers.
4010 for (pdnum
= 0; pdnum
< __be16_to_cpu(ddf
->phys
->used_pdes
); pdnum
++)
4011 if ((ddf
->phys
->entries
[pdnum
].state
4012 & __be16_to_cpu(DDF_Failed
))
4013 && (ddf
->phys
->entries
[pdnum
].state
4014 & __be16_to_cpu(DDF_Transition
)))
4015 /* skip this one */;
4016 else if (pdnum
== pd2
)
4019 ddf
->phys
->entries
[pd2
] = ddf
->phys
->entries
[pdnum
];
4020 for (dl
= ddf
->dlist
; dl
; dl
= dl
->next
)
4021 if (dl
->pdnum
== (int)pdnum
)
4025 ddf
->phys
->used_pdes
= __cpu_to_be16(pd2
);
4026 while (pd2
< pdnum
) {
4027 memset(ddf
->phys
->entries
[pd2
].guid
, 0xff, DDF_GUID_LEN
);
4031 ddf_set_updates_pending(ddf
);
4033 case DDF_SPARE_ASSIGN_MAGIC
:
4038 static void ddf_prepare_update(struct supertype
*st
,
4039 struct metadata_update
*update
)
4041 /* This update arrived at managemon.
4042 * We are about to pass it to monitor.
4043 * If a malloc is needed, do it here.
4045 struct ddf_super
*ddf
= st
->sb
;
4046 __u32
*magic
= (__u32
*)update
->buf
;
4047 if (*magic
== DDF_VD_CONF_MAGIC
)
4048 if (posix_memalign(&update
->space
, 512,
4049 offsetof(struct vcl
, conf
)
4050 + ddf
->conf_rec_len
* 512) != 0)
4051 update
->space
= NULL
;
4055 * Check if the array 'a' is degraded but not failed.
4056 * If it is, find as many spares as are available and needed and
4057 * arrange for their inclusion.
4058 * We only choose devices which are not already in the array,
4059 * and prefer those with a spare-assignment to this array.
4060 * otherwise we choose global spares - assuming always that
4061 * there is enough room.
4062 * For each spare that we assign, we return an 'mdinfo' which
4063 * describes the position for the device in the array.
4064 * We also add to 'updates' a DDF_VD_CONF_MAGIC update with
4065 * the new phys_refnum and lba_offset values.
4067 * Only worry about BVDs at the moment.
4069 static struct mdinfo
*ddf_activate_spare(struct active_array
*a
,
4070 struct metadata_update
**updates
)
4074 struct ddf_super
*ddf
= a
->container
->sb
;
4076 struct mdinfo
*rv
= NULL
;
4078 struct metadata_update
*mu
;
4081 struct vd_config
*vc
;
4084 for (d
= a
->info
.devs
; d
; d
= d
->next
) {
4085 if ((d
->curr_state
& DS_FAULTY
) &&
4087 /* wait for Removal to happen */
4089 if (d
->state_fd
>= 0)
4093 dprintf("ddf_activate: working=%d (%d) level=%d\n", working
, a
->info
.array
.raid_disks
,
4094 a
->info
.array
.level
);
4095 if (working
== a
->info
.array
.raid_disks
)
4096 return NULL
; /* array not degraded */
4097 switch (a
->info
.array
.level
) {
4100 return NULL
; /* failed */
4104 if (working
< a
->info
.array
.raid_disks
- 1)
4105 return NULL
; /* failed */
4108 if (working
< a
->info
.array
.raid_disks
- 2)
4109 return NULL
; /* failed */
4111 default: /* concat or stripe */
4112 return NULL
; /* failed */
4115 /* For each slot, if it is not working, find a spare */
4117 for (i
= 0; i
< a
->info
.array
.raid_disks
; i
++) {
4118 for (d
= a
->info
.devs
; d
; d
= d
->next
)
4119 if (d
->disk
.raid_disk
== i
)
4121 dprintf("found %d: %p %x\n", i
, d
, d
?d
->curr_state
:0);
4122 if (d
&& (d
->state_fd
>= 0))
4125 /* OK, this device needs recovery. Find a spare */
4127 for ( ; dl
; dl
= dl
->next
) {
4128 unsigned long long esize
;
4129 unsigned long long pos
;
4132 int is_dedicated
= 0;
4135 /* If in this array, skip */
4136 for (d2
= a
->info
.devs
; d2
; d2
= d2
->next
)
4137 if (d2
->state_fd
>= 0 &&
4138 d2
->disk
.major
== dl
->major
&&
4139 d2
->disk
.minor
== dl
->minor
) {
4140 dprintf("%x:%x already in array\n", dl
->major
, dl
->minor
);
4145 if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4146 __cpu_to_be16(DDF_Spare
)) {
4147 /* Check spare assign record */
4149 if (dl
->spare
->type
& DDF_spare_dedicated
) {
4150 /* check spare_ents for guid */
4152 j
< __be16_to_cpu(dl
->spare
->populated
);
4154 if (memcmp(dl
->spare
->spare_ents
[j
].guid
,
4155 ddf
->virt
->entries
[a
->info
.container_member
].guid
,
4162 } else if (ddf
->phys
->entries
[dl
->pdnum
].type
&
4163 __cpu_to_be16(DDF_Global_Spare
)) {
4165 } else if (!(ddf
->phys
->entries
[dl
->pdnum
].state
&
4166 __cpu_to_be16(DDF_Failed
))) {
4167 /* we can possibly use some of this */
4170 if ( ! (is_dedicated
||
4171 (is_global
&& global_ok
))) {
4172 dprintf("%x:%x not suitable: %d %d\n", dl
->major
, dl
->minor
,
4173 is_dedicated
, is_global
);
4177 /* We are allowed to use this device - is there space?
4178 * We need a->info.component_size sectors */
4179 ex
= get_extents(ddf
, dl
);
4181 dprintf("cannot get extents\n");
4188 esize
= ex
[j
].start
- pos
;
4189 if (esize
>= a
->info
.component_size
)
4191 pos
= ex
[j
].start
+ ex
[j
].size
;
4193 } while (ex
[j
-1].size
);
4196 if (esize
< a
->info
.component_size
) {
4197 dprintf("%x:%x has no room: %llu %llu\n",
4198 dl
->major
, dl
->minor
,
4199 esize
, a
->info
.component_size
);
4204 /* Cool, we have a device with some space at pos */
4205 di
= xcalloc(1, sizeof(*di
));
4206 di
->disk
.number
= i
;
4207 di
->disk
.raid_disk
= i
;
4208 di
->disk
.major
= dl
->major
;
4209 di
->disk
.minor
= dl
->minor
;
4211 di
->recovery_start
= 0;
4212 di
->data_offset
= pos
;
4213 di
->component_size
= a
->info
.component_size
;
4214 di
->container_member
= dl
->pdnum
;
4217 dprintf("%x:%x to be %d at %llu\n", dl
->major
, dl
->minor
,
4222 if (!dl
&& ! global_ok
) {
4223 /* not enough dedicated spares, try global */
4231 /* No spares found */
4233 /* Now 'rv' has a list of devices to return.
4234 * Create a metadata_update record to update the
4235 * phys_refnum and lba_offset values
4237 mu
= xmalloc(sizeof(*mu
));
4238 if (posix_memalign(&mu
->space
, 512, sizeof(struct vcl
)) != 0) {
4242 mu
->buf
= xmalloc(ddf
->conf_rec_len
* 512);
4243 mu
->len
= ddf
->conf_rec_len
* 512;
4245 mu
->space_list
= NULL
;
4246 mu
->next
= *updates
;
4247 vc
= find_vdcr(ddf
, a
->info
.container_member
);
4248 memcpy(mu
->buf
, vc
, ddf
->conf_rec_len
* 512);
4250 vc
= (struct vd_config
*)mu
->buf
;
4251 lba
= (__u64
*)&vc
->phys_refnum
[ddf
->mppe
];
4252 for (di
= rv
; di
; di
= di
->next
) {
4253 vc
->phys_refnum
[di
->disk
.raid_disk
] =
4254 ddf
->phys
->entries
[dl
->pdnum
].refnum
;
4255 lba
[di
->disk
.raid_disk
] = di
->data_offset
;
4260 #endif /* MDASSEMBLE */
4262 static int ddf_level_to_layout(int level
)
4269 return ALGORITHM_LEFT_SYMMETRIC
;
4271 return ALGORITHM_ROTATING_N_CONTINUE
;
4279 static void default_geometry_ddf(struct supertype
*st
, int *level
, int *layout
, int *chunk
)
4281 if (level
&& *level
== UnSet
)
4282 *level
= LEVEL_CONTAINER
;
4284 if (level
&& layout
&& *layout
== UnSet
)
4285 *layout
= ddf_level_to_layout(*level
);
4288 struct superswitch super_ddf
= {
4290 .examine_super
= examine_super_ddf
,
4291 .brief_examine_super
= brief_examine_super_ddf
,
4292 .brief_examine_subarrays
= brief_examine_subarrays_ddf
,
4293 .export_examine_super
= export_examine_super_ddf
,
4294 .detail_super
= detail_super_ddf
,
4295 .brief_detail_super
= brief_detail_super_ddf
,
4296 .validate_geometry
= validate_geometry_ddf
,
4297 .write_init_super
= write_init_super_ddf
,
4298 .add_to_super
= add_to_super_ddf
,
4299 .remove_from_super
= remove_from_super_ddf
,
4300 .load_container
= load_container_ddf
,
4302 .match_home
= match_home_ddf
,
4303 .uuid_from_super
= uuid_from_super_ddf
,
4304 .getinfo_super
= getinfo_super_ddf
,
4305 .update_super
= update_super_ddf
,
4307 .avail_size
= avail_size_ddf
,
4309 .compare_super
= compare_super_ddf
,
4311 .load_super
= load_super_ddf
,
4312 .init_super
= init_super_ddf
,
4313 .store_super
= store_super_ddf
,
4314 .free_super
= free_super_ddf
,
4315 .match_metadata_desc
= match_metadata_desc_ddf
,
4316 .container_content
= container_content_ddf
,
4317 .default_geometry
= default_geometry_ddf
,
4323 .open_new
= ddf_open_new
,
4324 .set_array_state
= ddf_set_array_state
,
4325 .set_disk
= ddf_set_disk
,
4326 .sync_metadata
= ddf_sync_metadata
,
4327 .process_update
= ddf_process_update
,
4328 .prepare_update
= ddf_prepare_update
,
4329 .activate_spare
= ddf_activate_spare
,