Codebase list mdadm / a322f70
Initial DDF support code. Create a ddf array by naming the device /dev/ddf* or specifying metadata 'ddf'. If ddf is specified with no level, assume a container (indeed, anything else would be wrong). **Need to use text_Version to set external metadata... More ddf support Load a ddf container. Now --examine /dev/ddf works. super-ddf: fix compile warning From: Dan Williams <dan.j.williams@intel.com> super-ddf.c:723: format %lu expects type long unsigned int, but argument 3 has type unsigned int Signed-off-by: Dan Williams <dan.j.williams@intel.com> Dan Williams authored 16 years ago Neil Brown committed 16 years ago
6 changed file(s) with 2090 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
9292 }
9393 }
9494 if (level == UnSet) {
95 /* "ddf" metadata only supports one level - should possibly
96 * push this into metadata handler??
97 */
98 if (st && st->ss == &super_ddf)
99 level = LEVEL_CONTAINER;
100 }
101
102 if (level == UnSet) {
95103 fprintf(stderr,
96104 Name ": a RAID level is needed to create an array.\n");
97105 return 1;
6868 OBJS = mdadm.o config.o mdstat.o ReadMe.o util.o Manage.o Assemble.o Build.o \
6969 Create.o Detail.o Examine.o Grow.o Monitor.o dlink.o Kill.o Query.o \
7070 Incremental.o \
71 mdopen.o super0.o super1.o bitmap.o restripe.o sysfs.o sha1.o \
72 mapfile.o crc32.o
71 mdopen.o super0.o super1.o super-ddf.o bitmap.o restripe.o sysfs.o \
72 sha1.o mapfile.o crc32.o
7373 SRCS = mdadm.c config.c mdstat.c ReadMe.c util.c Manage.c Assemble.c Build.c \
7474 Create.c Detail.c Examine.c Grow.c Monitor.c dlink.c Kill.c Query.c \
7575 Incremental.c \
76 mdopen.c super0.c super1.c bitmap.c restripe.c sysfs.c sha1.c \
77 mapfile.c crc32.c
76 mdopen.c super0.c super1.c super-ddf.c bitmap.c restripe.c sysfs.c \
77 sha1.c mapfile.c crc32.c
7878
7979 STATICSRC = pwgr.c
8080 STATICOBJS = pwgr.o
8181
82 ASSEMBLE_SRCS := mdassemble.c Assemble.c Manage.c config.c dlink.c util.c super0.c super1.c sha1.c
82 ASSEMBLE_SRCS := mdassemble.c Assemble.c Manage.c config.c dlink.c util.c \
83 super0.c super1.c super-ddf.c sha1.c crc32.c
8384 ASSEMBLE_FLAGS:= $(CFLAGS) -DMDASSEMBLE
8485 ifdef MDASSEMBLE_AUTO
8586 ASSEMBLE_SRCS += mdopen.c mdstat.c
0 - add 'name' field to metadata type and use it.
1 - use validate_geometry more
2 - metadata should be able to check/reject bitmap stuff.
3
4 DDF:
5 Three new metadata types:
6 ddf - used only to create a container.
7 ddf-bvd - used to create an array in a container
8 ddf-svd - used to create a secondary array from bvds.
9
10 Usage:
11 mdadm -C /dev/ddf1 /dev/sd[abcdef]
12 mdadm -C /dev/md1 -e ddf /dev/sd[a-f]
13 mdadm -C /dev/md1 -l container /dev/sd[a-f]
14
15 Each of these create a new ddf container using all those
16 devices. The name 'ddf*' signals that ddf metadata should be used.
17 '-e ddf' only supports one level - 'container'. 'container' is only
18 supported by ddf.
19
20 mdadm -C /dev/md1 -l0 -n4 /dev/ddf1 # or maybe not ???
21 mdadm -C /dev/md1 -l1 -n2 /dev/sda /dev/sdb
22 If exactly one device is given, and it is a container, we select
23 devices from that container.
24 If devices are given that are already in use, they must be in use by
25 a container, and the array is created in the container.
26 If devices given are bvds, we slip under the hood to make
27 the svd arrays.
28
29 mdadm -A /dev/ddf ......
30 base drives make a container. Anything in that container is started
31 auto-read-only.
32 if /dev/ddf is already assembled, we assemble bvds and svds inside it.
33
34
035 2005-dec-20
136 Want an incremental assembly mode to work nicely with udev.
237 Core usage would be something like
379379 char *text_version;
380380 int swapuuid; /* true if uuid is bigending rather than hostendian */
381381 int external;
382 } super0, super1, *superlist[];
382 } super0, super1, super_ddf, super_ddf_bvd, super_ddf_svd, *superlist[];
383383
384384 struct supertype {
385385 struct superswitch *ss;
545545 extern int open_mddev(char *dev, int autof);
546546 extern int open_mddev_devnum(char *devname, int devnum, char *name,
547547 char *chosen_name, int parts);
548 extern int open_container(int fd);
548549
549550
550551 #define LEVEL_MULTIPATH (-4)
553554
554555 /* kernel module doesn't know about these */
555556 #define LEVEL_CONTAINER (-100)
557 #define LEVEL_UNSUPPORTED (-200)
556558
557559
558560 /* faulty stuff */
0 /*
1 * mdadm - manage Linux "md" devices aka RAID arrays.
2 *
3 * Copyright (C) 2006-2007 Neil Brown <neilb@suse.de>
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author: Neil Brown
21 * Email: <neil@brown.name>
22 *
23 * Specifications for DDF takes from Common RAID DDF Specification Revision 1.2
24 * (July 28 2006). Reused by permission of SNIA.
25 */
26
27 #define HAVE_STDINT_H 1
28 #include "mdadm.h"
29 #include "sha1.h"
30 #include <values.h>
31
32 static inline int ROUND_UP(int a, int base)
33 {
34 return ((a+base-1)/base)*base;
35 }
36
37 /* a non-official T10 name for creation GUIDs */
38 static char T10[] = "Linux-MD";
39
40 /* DDF timestamps are 1980 based, so we need to add
41 * second-in-decade-of-seventies to convert to linux timestamps.
42 * 10 years with 2 leap years.
43 */
44 #define DECADE (3600*24*(365*10+2))
45 unsigned long crc32(
46 unsigned long crc,
47 const unsigned char *buf,
48 unsigned len);
49
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.
54 *
55 * Note:
56 * - all multibyte numeric fields are bigendian.
57 * - all strings are space padded.
58 *
59 */
60
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
68 #define DDF_JBOD 0x0f
69 #define DDF_CONCAT 0x1f
70 #define DDF_RAID5E 0x15
71 #define DDF_RAID5EE 0x25
72 #define DDF_RAID6 0x16 /* Vendor unique layout */
73
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_RAID5_N_RESTART 0x02 /* same as 'left asymmetric' - layout 0 */
85 #define DDF_RAID5_N_CONTINUE 0x03 /* same as 'left symmetric' - layout 2 */
86
87 #define DDF_RAID1E_ADJACENT 0x00 /* raid10 nearcopies==2 */
88 #define DDF_RAID1E_OFFSET 0x01 /* raid10 offsetcopies==2 */
89
90 /* Secondary RAID Level (SRL) */
91 #define DDF_2STRIPED 0x00 /* This is weirder than RAID0 !! */
92 #define DDF_2MIRRORED 0x01
93 #define DDF_2CONCAT 0x02
94 #define DDF_2SPANNED 0x03 /* This is also weird - be careful */
95
96 /* Magic numbers */
97 #define DDF_HEADER_MAGIC __cpu_to_be32(0xDE11DE11)
98 #define DDF_CONTROLLER_MAGIC __cpu_to_be32(0xAD111111)
99 #define DDF_PHYS_RECORDS_MAGIC __cpu_to_be32(0x22222222)
100 #define DDF_PHYS_DATA_MAGIC __cpu_to_be32(0x33333333)
101 #define DDF_VIRT_RECORDS_MAGIC __cpu_to_be32(0xDDDDDDDD)
102 #define DDF_VD_CONF_MAGIC __cpu_to_be32(0xEEEEEEEE)
103 #define DDF_SPARE_ASSIGN_MAGIC __cpu_to_be32(0x55555555)
104 #define DDF_VU_CONF_MAGIC __cpu_to_be32(0x88888888)
105 #define DDF_VENDOR_LOG_MAGIC __cpu_to_be32(0x01dBEEF0)
106 #define DDF_BBM_LOG_MAGIC __cpu_to_be32(0xABADB10C)
107
108 #define DDF_GUID_LEN 24
109 #define DDF_REVISION "01.00.00"
110
111 struct ddf_header {
112 __u32 magic;
113 __u32 crc;
114 char guid[DDF_GUID_LEN];
115 char revision[8]; /* 01.00.00 */
116 __u32 seq; /* starts at '1' */
117 __u32 timestamp;
118 __u8 openflag;
119 __u8 foreignflag;
120 __u8 enforcegroups;
121 __u8 pad0; /* 0xff */
122 __u8 pad1[12]; /* 12 * 0xff */
123 /* 64 bytes so far */
124 __u8 header_ext[32]; /* reserved: fill with 0xff */
125 __u64 primary_lba;
126 __u64 secondary_lba;
127 __u8 type;
128 __u8 pad2[3]; /* 0xff */
129 __u32 workspace_len; /* sectors for vendor space -
130 * at least 32768(sectors) */
131 __u64 workspace_lba;
132 __u16 max_pd_entries; /* one of 15, 63, 255, 1023, 4095 */
133 __u16 max_vd_entries; /* 2^(4,6,8,10,12)-1 : i.e. as above */
134 __u16 max_partitions; /* i.e. max num of configuration
135 record entries per disk */
136 __u16 config_record_len; /* 1 +ROUNDUP(max_primary_element_entries
137 *12/512) */
138 __u16 max_primary_element_entries; /* 16, 64, 256, 1024, or 4096 */
139 __u8 pad3[54]; /* 0xff */
140 /* 192 bytes so far */
141 __u32 controller_section_offset;
142 __u32 controller_section_length;
143 __u32 phys_section_offset;
144 __u32 phys_section_length;
145 __u32 virt_section_offset;
146 __u32 virt_section_length;
147 __u32 config_section_offset;
148 __u32 config_section_length;
149 __u32 data_section_offset;
150 __u32 data_section_length;
151 __u32 bbm_section_offset;
152 __u32 bbm_section_length;
153 __u32 diag_space_offset;
154 __u32 diag_space_length;
155 __u32 vendor_offset;
156 __u32 vendor_length;
157 /* 256 bytes so far */
158 __u8 pad4[256]; /* 0xff */
159 };
160
161 /* type field */
162 #define DDF_HEADER_ANCHOR 0x00
163 #define DDF_HEADER_PRIMARY 0x01
164 #define DDF_HEADER_SECONDARY 0x02
165
166 /* The content of the 'controller section' - global scope */
167 struct ddf_controller_data {
168 __u32 magic;
169 __u32 crc;
170 char guid[DDF_GUID_LEN];
171 struct controller_type {
172 __u16 vendor_id;
173 __u16 device_id;
174 __u16 sub_vendor_id;
175 __u16 sub_device_id;
176 } type;
177 char product_id[16];
178 __u8 pad[8]; /* 0xff */
179 __u8 vendor_data[448];
180 };
181
182 /* The content of phys_section - global scope */
183 struct phys_disk {
184 __u32 magic;
185 __u32 crc;
186 __u16 used_pdes;
187 __u16 max_pdes;
188 __u8 pad[52];
189 struct phys_disk_entry {
190 char guid[DDF_GUID_LEN];
191 __u32 refnum;
192 __u16 type;
193 __u16 state;
194 __u64 config_size; /* DDF structures must be after here */
195 char path[18]; /* another horrible structure really */
196 __u8 pad[6];
197 } entries[0];
198 };
199
200 /* phys_disk_entry.type is a bitmap - bigendian remember */
201 #define DDF_Forced_PD_GUID 1
202 #define DDF_Active_in_VD 2
203 #define DDF_Global_Spare 4
204 #define DDF_Spare 8 /* overrides Global_spare */
205 #define DDF_Foreign 16
206 #define DDF_Legacy 32 /* no DDF on this device */
207
208 #define DDF_Interface_mask 0xf00
209 #define DDF_Interface_SCSI 0x100
210 #define DDF_Interface_SAS 0x200
211 #define DDF_Interface_SATA 0x300
212 #define DDF_Interface_FC 0x400
213
214 /* phys_disk_entry.state is a bigendian bitmap */
215 #define DDF_Online 1
216 #define DDF_Failed 2 /* overrides 1,4,8 */
217 #define DDF_Rebuilding 4
218 #define DDF_Transition 8
219 #define DDF_SMART 16
220 #define DDF_ReadErrors 32
221 #define DDF_Missing 64
222
223 /* The content of the virt_section global scope */
224 struct virtual_disk {
225 __u32 magic;
226 __u32 crc;
227 __u16 populated_vdes;
228 __u16 max_vdes;
229 __u8 pad[52];
230 struct virtual_entry {
231 char guid[DDF_GUID_LEN];
232 __u16 unit;
233 __u16 pad0; /* 0xffff */
234 __u16 guid_crc;
235 __u16 type;
236 __u8 state;
237 __u8 init_state;
238 __u8 pad1[14];
239 char name[16];
240 } entries[0];
241 };
242
243 /* virtual_entry.type is a bitmap - bigendian */
244 #define DDF_Shared 1
245 #define DDF_Enforce_Groups 2
246 #define DDF_Unicode 4
247 #define DDF_Owner_Valid 8
248
249 /* virtual_entry.state is a bigendian bitmap */
250 #define DDF_state_mask 0x7
251 #define DDF_state_optimal 0x0
252 #define DDF_state_degraded 0x1
253 #define DDF_state_deleted 0x2
254 #define DDF_state_missing 0x3
255 #define DDF_state_failed 0x4
256
257 #define DDF_state_morphing 0x8
258 #define DDF_state_inconsistent 0x10
259
260 /* virtual_entry.init_state is a bigendian bitmap */
261 #define DDF_initstate_mask 0x03
262 #define DDF_init_not 0x00
263 #define DDF_init_quick 0x01
264 #define DDF_init_full 0x02
265
266 #define DDF_access_mask 0xc0
267 #define DDF_access_rw 0x00
268 #define DDF_access_ro 0x80
269 #define DDF_access_blocked 0xc0
270
271 /* The content of the config_section - local scope
272 * It has multiple records each config_record_len sectors
273 * They can be vd_config or spare_assign
274 */
275
276 struct vd_config {
277 __u32 magic;
278 __u32 crc;
279 char guid[DDF_GUID_LEN];
280 __u32 timestamp;
281 __u32 seqnum;
282 __u8 pad0[24];
283 __u16 prim_elmnt_count;
284 __u8 chunk_shift; /* 0 == 512, 1==1024 etc */
285 __u8 prl;
286 __u8 rlq;
287 __u8 sec_elmnt_count;
288 __u8 sec_elmnt_seq;
289 __u8 srl;
290 __u64 blocks;
291 __u64 array_blocks;
292 __u8 pad1[8];
293 __u32 spare_refs[8];
294 __u8 cache_pol[8];
295 __u8 bg_rate;
296 __u8 pad2[3];
297 __u8 pad3[52];
298 __u8 pad4[192];
299 __u8 v0[32]; /* reserved- 0xff */
300 __u8 v1[32]; /* reserved- 0xff */
301 __u8 v2[16]; /* reserved- 0xff */
302 __u8 v3[16]; /* reserved- 0xff */
303 __u8 vendor[32];
304 __u32 phys_refnum[0]; /* refnum of each disk in sequence */
305 /*__u64 lba_offset[0]; LBA offset in each phys. Note extents in a
306 bvd are always the same size */
307 };
308
309 /* vd_config.cache_pol[7] is a bitmap */
310 #define DDF_cache_writeback 1 /* else writethrough */
311 #define DDF_cache_wadaptive 2 /* only applies if writeback */
312 #define DDF_cache_readahead 4
313 #define DDF_cache_radaptive 8 /* only if doing read-ahead */
314 #define DDF_cache_ifnobatt 16 /* even to write cache if battery is poor */
315 #define DDF_cache_wallowed 32 /* enable write caching */
316 #define DDF_cache_rallowed 64 /* enable read caching */
317
318 struct spare_assign {
319 __u32 magic;
320 __u32 crc;
321 __u32 timestamp;
322 __u8 reserved[7];
323 __u8 type;
324 __u16 populated; /* SAEs used */
325 __u16 max; /* max SAEs */
326 __u8 pad[8];
327 struct spare_assign_entry {
328 char guid[DDF_GUID_LEN];
329 __u16 secondary_element;
330 __u8 pad[6];
331 } spare_ents[0];
332 };
333 /* spare_assign.type is a bitmap */
334 #define DDF_spare_dedicated 0x1 /* else global */
335 #define DDF_spare_revertible 0x2 /* else committable */
336 #define DDF_spare_active 0x4 /* else not active */
337 #define DDF_spare_affinity 0x8 /* enclosure affinity */
338
339 /* The data_section contents - local scope */
340 struct disk_data {
341 __u32 magic;
342 __u32 crc;
343 char guid[DDF_GUID_LEN];
344 __u32 refnum; /* crc of some magic drive data ... */
345 __u8 forced_ref; /* set when above was not result of magic */
346 __u8 forced_guid; /* set if guid was forced rather than magic */
347 __u8 vendor[32];
348 __u8 pad[442];
349 };
350
351 /* bbm_section content */
352 struct bad_block_log {
353 __u32 magic;
354 __u32 crc;
355 __u16 entry_count;
356 __u32 spare_count;
357 __u8 pad[10];
358 __u64 first_spare;
359 struct mapped_block {
360 __u64 defective_start;
361 __u32 replacement_start;
362 __u16 remap_count;
363 __u8 pad[2];
364 } entries[0];
365 };
366
367 /* Struct for internally holding ddf structures */
368 /* The DDF structure stored on each device is potentially
369 * quite different, as some data is global and some is local.
370 * The global data is:
371 * - ddf header
372 * - controller_data
373 * - Physical disk records
374 * - Virtual disk records
375 * The local data is:
376 * - Configuration records
377 * - Physical Disk data section
378 * ( and Bad block and vendor which I don't care about yet).
379 *
380 * The local data is parsed into separate lists as it is read
381 * and reconstructed for writing. This means that we only need
382 * to make config changes once and they are automatically
383 * propagated to all devices.
384 * Note that the ddf_super has space of the conf and disk data
385 * for this disk and also for a list of all such data.
386 * The list is only used for the superblock that is being
387 * built in Create or Assemble to describe the whole array.
388 */
389 struct ddf_super {
390 struct ddf_header anchor, primary, secondary, *active;
391 struct ddf_controller_data controller;
392 struct phys_disk *phys;
393 struct virtual_disk *virt;
394 int pdsize, vdsize;
395 int max_part;
396 struct vcl {
397 struct vcl *next;
398 __u64 *lba_offset; /* location in 'conf' of
399 * the lba table */
400 struct vd_config conf;
401 } *conflist, *newconf;
402 struct dl {
403 struct dl *next;
404 struct disk_data disk;
405 int major, minor;
406 char *devname;
407 int fd;
408 struct vcl *vlist[0]; /* max_part+1 in size */
409 } *dlist;
410 };
411
412 #ifndef offsetof
413 #define offsetof(t,f) ((size_t)&(((t*)0)->f))
414 #endif
415
416 struct superswitch super_ddf_container, super_ddf_bvd;
417
418 static int calc_crc(void *buf, int len)
419 {
420 /* crcs are always at the same place as in the ddf_header */
421 struct ddf_header *ddf = buf;
422 __u32 oldcrc = ddf->crc;
423 __u32 newcrc;
424 ddf->crc = 0xffffffff;
425
426 newcrc = crc32(0, buf, len);
427 ddf->crc = oldcrc;
428 return newcrc;
429 }
430
431 static int load_ddf_header(int fd, unsigned long long lba,
432 unsigned long long size,
433 int type,
434 struct ddf_header *hdr, struct ddf_header *anchor)
435 {
436 /* read a ddf header (primary or secondary) from fd/lba
437 * and check that it is consistent with anchor
438 * Need to check:
439 * magic, crc, guid, rev, and LBA's header_type, and
440 * everything after header_type must be the same
441 */
442 if (lba >= size-1)
443 return 0;
444
445 if (lseek64(fd, lba<<9, 0) < 0)
446 return 0;
447
448 if (read(fd, hdr, 512) != 512)
449 return 0;
450
451 if (hdr->magic != DDF_HEADER_MAGIC)
452 return 0;
453 if (calc_crc(hdr, 512) != hdr->crc)
454 return 0;
455 if (memcmp(anchor->guid, hdr->guid, DDF_GUID_LEN) != 0 ||
456 memcmp(anchor->revision, hdr->revision, 8) != 0 ||
457 anchor->primary_lba != hdr->primary_lba ||
458 anchor->secondary_lba != hdr->secondary_lba ||
459 hdr->type != type ||
460 memcmp(anchor->pad2, hdr->pad2, 512 -
461 offsetof(struct ddf_header, pad2)) != 0)
462 return 0;
463
464 /* Looks good enough to me... */
465 return 1;
466 }
467
468 static void *load_section(int fd, struct ddf_super *super, void *buf,
469 __u32 offset_be, __u32 len_be, int check)
470 {
471 unsigned long long offset = __be32_to_cpu(offset_be);
472 unsigned long long len = __be32_to_cpu(len_be);
473 int dofree = (buf == NULL);
474
475 if (check)
476 if (len != 2 && len != 8 && len != 32
477 && len != 128 && len != 512)
478 return NULL;
479
480 if (len > 1024)
481 return NULL;
482 if (buf) {
483 /* All pre-allocated sections are a single block */
484 if (len != 1)
485 return NULL;
486 } else
487 buf = malloc(len<<9);
488 if (!buf)
489 return NULL;
490
491 if (super->active->type == 1)
492 offset += __be64_to_cpu(super->active->primary_lba);
493 else
494 offset += __be64_to_cpu(super->active->secondary_lba);
495
496 if (lseek64(fd, offset<<9, 0) != (offset<<9)) {
497 if (dofree)
498 free(buf);
499 return NULL;
500 }
501 if (read(fd, buf, len<<9) != (len<<9)) {
502 if (dofree)
503 free(buf);
504 return NULL;
505 }
506 return buf;
507 }
508
509 static int load_ddf_headers(int fd, struct ddf_super *super, char *devname)
510 {
511 unsigned long long dsize;
512
513 get_dev_size(fd, NULL, &dsize);
514
515 if (lseek64(fd, dsize-512, 0) < 0) {
516 if (devname)
517 fprintf(stderr,
518 Name": Cannot seek to anchor block on %s: %s\n",
519 devname, strerror(errno));
520 return 1;
521 }
522 if (read(fd, &super->anchor, 512) != 512) {
523 if (devname)
524 fprintf(stderr,
525 Name ": Cannot read anchor block on %s: %s\n",
526 devname, strerror(errno));
527 return 1;
528 }
529 if (super->anchor.magic != DDF_HEADER_MAGIC) {
530 if (devname)
531 fprintf(stderr, Name ": no DDF anchor found on %s\n",
532 devname);
533 return 2;
534 }
535 if (calc_crc(&super->anchor, 512) != super->anchor.crc) {
536 if (devname)
537 fprintf(stderr, Name ": bad CRC on anchor on %s\n",
538 devname);
539 return 2;
540 }
541 if (memcmp(super->anchor.revision, DDF_REVISION, 8) != 0) {
542 if (devname)
543 fprintf(stderr, Name ": can only support super revision"
544 " %.8s, not %.8s on %s\n",
545 DDF_REVISION, super->anchor.revision, devname);
546 return 2;
547 }
548 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.primary_lba),
549 dsize >> 9, 1,
550 &super->primary, &super->anchor) == 0) {
551 if (devname)
552 fprintf(stderr,
553 Name ": Failed to load primary DDF header "
554 "on %s\n", devname);
555 return 2;
556 }
557 super->active = &super->primary;
558 if (load_ddf_header(fd, __be64_to_cpu(super->anchor.secondary_lba),
559 dsize >> 9, 2,
560 &super->secondary, &super->anchor)) {
561 if ((__be32_to_cpu(super->primary.seq)
562 < __be32_to_cpu(super->secondary.seq) &&
563 !super->secondary.openflag)
564 || (__be32_to_cpu(super->primary.seq)
565 == __be32_to_cpu(super->secondary.seq) &&
566 super->primary.openflag && !super->secondary.openflag)
567 )
568 super->active = &super->secondary;
569 }
570 return 0;
571 }
572
573 static int load_ddf_global(int fd, struct ddf_super *super, char *devname)
574 {
575 void *ok;
576 ok = load_section(fd, super, &super->controller,
577 super->active->controller_section_offset,
578 super->active->controller_section_length,
579 0);
580 super->phys = load_section(fd, super, NULL,
581 super->active->phys_section_offset,
582 super->active->phys_section_length,
583 1);
584 super->pdsize = __be32_to_cpu(super->active->phys_section_length) * 512;
585
586 super->virt = load_section(fd, super, NULL,
587 super->active->virt_section_offset,
588 super->active->virt_section_length,
589 1);
590 super->vdsize = __be32_to_cpu(super->active->virt_section_length) * 512;
591 if (!ok ||
592 !super->phys ||
593 !super->virt) {
594 free(super->phys);
595 free(super->virt);
596 return 2;
597 }
598 super->conflist = NULL;
599 super->dlist = NULL;
600 return 0;
601 }
602
603 static int load_ddf_local(int fd, struct ddf_super *super,
604 char *devname, int keep)
605 {
606 struct dl *dl;
607 struct stat stb;
608 char *conf;
609 int i;
610 int conflen;
611
612 /* First the local disk info */
613 super->max_part = __be16_to_cpu(super->active->max_partitions);
614 dl = malloc(sizeof(*dl) +
615 (super->max_part+1) * sizeof(dl->vlist[0]));
616
617 load_section(fd, super, &dl->disk,
618 super->active->data_section_offset,
619 super->active->data_section_length,
620 0);
621 dl->devname = devname ? strdup(devname) : NULL;
622 fstat(fd, &stb);
623 dl->major = major(stb.st_rdev);
624 dl->minor = minor(stb.st_rdev);
625 dl->next = super->dlist;
626 dl->fd = keep ? fd : -1;
627 for (i=0 ; i < super->max_part + 1 ; i++)
628 dl->vlist[i] = NULL;
629 super->dlist = dl;
630
631 /* Now the config list. */
632 /* 'conf' is an array of config entries, some of which are
633 * probably invalid. Those which are good need to be copied into
634 * the conflist
635 */
636 conflen = __be16_to_cpu(super->active->config_record_len);
637
638 conf = load_section(fd, super, NULL,
639 super->active->config_section_offset,
640 super->active->config_section_length,
641 0);
642
643 for (i = 0;
644 i < __be32_to_cpu(super->active->config_section_length);
645 i += conflen) {
646 struct vd_config *vd =
647 (struct vd_config *)((char*)conf + i*512);
648 struct vcl *vcl;
649
650 if (vd->magic != DDF_VD_CONF_MAGIC)
651 continue;
652 for (vcl = super->conflist; vcl; vcl = vcl->next) {
653 if (memcmp(vcl->conf.guid,
654 vd->guid, DDF_GUID_LEN) == 0)
655 break;
656 }
657
658 if (vcl) {
659 dl->vlist[i/conflen] = vcl;
660 if (__be32_to_cpu(vd->seqnum) <=
661 __be32_to_cpu(vcl->conf.seqnum))
662 continue;
663 } else {
664 vcl = malloc(conflen*512 + offsetof(struct vcl, conf));
665 vcl->next = super->conflist;
666 super->conflist = vcl;
667 }
668 memcpy(&vcl->conf, vd, conflen*512);
669 vcl->lba_offset = (__u64*)
670 &vcl->conf.phys_refnum[super->max_part+1];
671 dl->vlist[i/conflen] = vcl;
672 }
673 free(conf);
674
675 return 0;
676 }
677
678 #ifndef MDASSEMBLE
679 static int load_super_ddf_all(struct supertype *st, int fd,
680 void **sbp, char *devname, int keep_fd);
681 #endif
682 static int load_super_ddf(struct supertype *st, int fd,
683 char *devname)
684 {
685 unsigned long long dsize;
686 struct ddf_super *super;
687 int rv;
688
689 #ifndef MDASSEMBLE
690 if (load_super_ddf_all(st, fd, &st->sb, devname, 0) == 0)
691 return 0;
692 #endif
693
694 if (get_dev_size(fd, devname, &dsize) == 0)
695 return 1;
696
697 /* 32M is a lower bound */
698 if (dsize <= 32*1024*1024) {
699 if (devname) {
700 fprintf(stderr,
701 Name ": %s is too small for ddf: "
702 "size is %llu sectors.\n",
703 devname, dsize>>9);
704 return 1;
705 }
706 }
707 if (dsize & 511) {
708 if (devname) {
709 fprintf(stderr,
710 Name ": %s is an odd size for ddf: "
711 "size is %llu bytes.\n",
712 devname, dsize);
713 return 1;
714 }
715 }
716
717 super = malloc(sizeof(*super));
718 if (!super) {
719 fprintf(stderr, Name ": malloc of %zu failed.\n",
720 sizeof(*super));
721 return 1;
722 }
723
724 rv = load_ddf_headers(fd, super, devname);
725 if (rv) {
726 free(super);
727 return rv;
728 }
729
730 /* Have valid headers and have chosen the best. Let's read in the rest*/
731
732 rv = load_ddf_global(fd, super, devname);
733
734 if (rv) {
735 if (devname)
736 fprintf(stderr,
737 Name ": Failed to load all information "
738 "sections on %s\n", devname);
739 free(super);
740 return rv;
741 }
742
743 load_ddf_local(fd, super, devname, 0);
744
745 /* Should possibly check the sections .... */
746
747 st->sb = super;
748 if (st->ss == NULL) {
749 st->ss = &super_ddf;
750 st->minor_version = 0;
751 st->max_devs = 512;
752 }
753 return 0;
754
755 }
756
757 static void free_super_ddf(struct supertype *st)
758 {
759 struct ddf_super *ddf = st->sb;
760 if (ddf == NULL)
761 return;
762 free(ddf->phys);
763 free(ddf->virt);
764 while (ddf->conflist) {
765 struct vcl *v = ddf->conflist;
766 ddf->conflist = v->next;
767 free(v);
768 }
769 while (ddf->dlist) {
770 struct dl *d = ddf->dlist;
771 ddf->dlist = d->next;
772 if (d->fd >= 0)
773 close(d->fd);
774 free(d);
775 }
776 free(ddf);
777 st->sb = NULL;
778 }
779
780 static struct supertype *match_metadata_desc_ddf(char *arg)
781 {
782 /* 'ddf' only support containers */
783 struct supertype *st;
784 if (strcmp(arg, "ddf") != 0 &&
785 strcmp(arg, "default") != 0
786 )
787 return NULL;
788
789 st = malloc(sizeof(*st));
790 st->ss = &super_ddf;
791 st->max_devs = 512;
792 st->minor_version = 0;
793 st->sb = NULL;
794 return st;
795 }
796
797 static struct supertype *match_metadata_desc_ddf_bvd(char *arg)
798 {
799 struct supertype *st;
800 if (strcmp(arg, "ddf/bvd") != 0 &&
801 strcmp(arg, "bvd") != 0 &&
802 strcmp(arg, "default") != 0
803 )
804 return NULL;
805
806 st = malloc(sizeof(*st));
807 st->ss = &super_ddf_bvd;
808 st->max_devs = 512;
809 st->minor_version = 0;
810 st->sb = NULL;
811 return st;
812 }
813 static struct supertype *match_metadata_desc_ddf_svd(char *arg)
814 {
815 struct supertype *st;
816 if (strcmp(arg, "ddf/svd") != 0 &&
817 strcmp(arg, "svd") != 0 &&
818 strcmp(arg, "default") != 0
819 )
820 return NULL;
821
822 st = malloc(sizeof(*st));
823 st->ss = &super_ddf_svd;
824 st->max_devs = 512;
825 st->minor_version = 0;
826 st->sb = NULL;
827 return st;
828 }
829
830 #ifndef MDASSEMBLE
831
832 static mapping_t ddf_state[] = {
833 { "Optimal", 0},
834 { "Degraded", 1},
835 { "Deleted", 2},
836 { "Missing", 3},
837 { "Failed", 4},
838 { "Partially Optimal", 5},
839 { "-reserved-", 6},
840 { "-reserved-", 7},
841 { NULL, 0}
842 };
843
844 static mapping_t ddf_init_state[] = {
845 { "Not Initialised", 0},
846 { "QuickInit in Progress", 1},
847 { "Fully Initialised", 2},
848 { "*UNKNOWN*", 3},
849 { NULL, 0}
850 };
851 static mapping_t ddf_access[] = {
852 { "Read/Write", 0},
853 { "Reserved", 1},
854 { "Read Only", 2},
855 { "Blocked (no access)", 3},
856 { NULL ,0}
857 };
858
859 static mapping_t ddf_level[] = {
860 { "RAID0", DDF_RAID0},
861 { "RAID1", DDF_RAID1},
862 { "RAID3", DDF_RAID3},
863 { "RAID4", DDF_RAID4},
864 { "RAID5", DDF_RAID5},
865 { "RAID1E",DDF_RAID1E},
866 { "JBOD", DDF_JBOD},
867 { "CONCAT",DDF_CONCAT},
868 { "RAID5E",DDF_RAID5E},
869 { "RAID5EE",DDF_RAID5EE},
870 { "RAID6", DDF_RAID6},
871 { NULL, 0}
872 };
873 static mapping_t ddf_sec_level[] = {
874 { "Striped", DDF_2STRIPED},
875 { "Mirrored", DDF_2MIRRORED},
876 { "Concat", DDF_2CONCAT},
877 { "Spanned", DDF_2SPANNED},
878 { NULL, 0}
879 };
880 #endif
881
882 struct num_mapping {
883 int num1, num2;
884 };
885 static struct num_mapping ddf_level_num[] = {
886 { DDF_RAID0, 0 },
887 { DDF_RAID1, 1 },
888 { DDF_RAID3, LEVEL_UNSUPPORTED },
889 { DDF_RAID5, 4 },
890 { DDF_RAID1E, LEVEL_UNSUPPORTED },
891 { DDF_JBOD, LEVEL_UNSUPPORTED },
892 { DDF_CONCAT, LEVEL_LINEAR },
893 { DDF_RAID5E, LEVEL_UNSUPPORTED },
894 { DDF_RAID5EE, LEVEL_UNSUPPORTED },
895 { DDF_RAID6, 6},
896 { MAXINT, MAXINT }
897 };
898
899 static int map_num1(struct num_mapping *map, int num)
900 {
901 int i;
902 for (i=0 ; map[i].num1 != MAXINT; i++)
903 if (map[i].num1 == num)
904 break;
905 return map[i].num2;
906 }
907
908 #ifndef MDASSEMBLE
909 static void print_guid(char *guid, int tstamp)
910 {
911 /* A GUIDs are part (or all) ASCII and part binary.
912 * They tend to be space padded.
913 * We ignore trailing spaces and print numbers
914 * <0x20 and >=0x7f as \xXX
915 * Some GUIDs have a time stamp in bytes 16-19.
916 * We print that if appropriate
917 */
918 int l = DDF_GUID_LEN;
919 int i;
920 while (l && guid[l-1] == ' ')
921 l--;
922 for (i=0 ; i<l ; i++) {
923 if (guid[i] >= 0x20 && guid[i] < 0x7f)
924 fputc(guid[i], stdout);
925 else
926 fprintf(stdout, "\\x%02x", guid[i]&255);
927 }
928 if (tstamp) {
929 time_t then = __be32_to_cpu(*(__u32*)(guid+16)) + DECADE;
930 char tbuf[100];
931 struct tm *tm;
932 tm = localtime(&then);
933 strftime(tbuf, 100, " (%D %T)",tm);
934 fputs(tbuf, stdout);
935 }
936 }
937
938 static void examine_vd(int n, struct ddf_super *sb, char *guid)
939 {
940 int crl = __be16_to_cpu(sb->anchor.config_record_len);
941 struct vcl *vcl;
942
943 for (vcl = sb->conflist ; vcl ; vcl = vcl->next) {
944 struct vd_config *vc = &vcl->conf;
945
946 if (calc_crc(vc, crl*512) != vc->crc)
947 continue;
948 if (memcmp(vc->guid, guid, DDF_GUID_LEN) != 0)
949 continue;
950
951 /* Ok, we know about this VD, let's give more details */
952 printf(" Raid Devices[%d] : %d\n", n,
953 __be16_to_cpu(vc->prim_elmnt_count));
954 printf(" Chunk Size[%d] : %d sectors\n", n,
955 1 << vc->chunk_shift);
956 printf(" Raid Level[%d] : %s\n", n,
957 map_num(ddf_level, vc->prl)?:"-unknown-");
958 if (vc->sec_elmnt_count != 1) {
959 printf(" Secondary Position[%d] : %d of %d\n", n,
960 vc->sec_elmnt_seq, vc->sec_elmnt_count);
961 printf(" Secondary Level[%d] : %s\n", n,
962 map_num(ddf_sec_level, vc->srl) ?: "-unknown-");
963 }
964 printf(" Device Size[%d] : %llu\n", n,
965 __be64_to_cpu(vc->blocks)/2);
966 printf(" Array Size[%d] : %llu\n", n,
967 __be64_to_cpu(vc->array_blocks)/2);
968 }
969 }
970
971 static void examine_vds(struct ddf_super *sb)
972 {
973 int cnt = __be16_to_cpu(sb->virt->populated_vdes);
974 int i;
975 printf(" Virtual Disks : %d\n", cnt);
976
977 for (i=0; i<cnt; i++) {
978 struct virtual_entry *ve = &sb->virt->entries[i];
979 printf(" VD GUID[%d] : ", i); print_guid(ve->guid, 1);
980 printf("\n");
981 printf(" unit[%d] : %d\n", i, __be16_to_cpu(ve->unit));
982 printf(" state[%d] : %s, %s%s\n", i,
983 map_num(ddf_state, ve->state & 7),
984 (ve->state & 8) ? "Morphing, ": "",
985 (ve->state & 16)? "Not Consistent" : "Consistent");
986 printf(" init state[%d] : %s\n", i,
987 map_num(ddf_init_state, ve->init_state&3));
988 printf(" access[%d] : %s\n", i,
989 map_num(ddf_access, (ve->init_state>>6) & 3));
990 printf(" Name[%d] : %.16s\n", i, ve->name);
991 examine_vd(i, sb, ve->guid);
992 }
993 if (cnt) printf("\n");
994 }
995
996 static void examine_pds(struct ddf_super *sb)
997 {
998 int cnt = __be16_to_cpu(sb->phys->used_pdes);
999 int i;
1000 struct dl *dl;
1001 printf(" Physical Disks : %d\n", cnt);
1002
1003 for (i=0 ; i<cnt ; i++) {
1004 struct phys_disk_entry *pd = &sb->phys->entries[i];
1005 int type = __be16_to_cpu(pd->type);
1006 int state = __be16_to_cpu(pd->state);
1007
1008 printf(" PD GUID[%d] : ", i); print_guid(pd->guid, 0);
1009 printf("\n");
1010 printf(" ref[%d] : %08x\n", i,
1011 __be32_to_cpu(pd->refnum));
1012 printf(" mode[%d] : %s%s%s%s%s\n", i,
1013 (type&2) ? "active":"",
1014 (type&4) ? "Global Spare":"",
1015 (type&8) ? "spare" : "",
1016 (type&16)? ", foreign" : "",
1017 (type&32)? "pass-through" : "");
1018 printf(" state[%d] : %s%s%s%s%s%s%s\n", i,
1019 (state&1)? "Online": "Offline",
1020 (state&2)? ", Failed": "",
1021 (state&4)? ", Rebuilding": "",
1022 (state&8)? ", in-transition": "",
1023 (state&16)? ", SMART errors": "",
1024 (state&32)? ", Unrecovered Read Errors": "",
1025 (state&64)? ", Missing" : "");
1026 printf(" Avail Size[%d] : %llu K\n", i,
1027 __be64_to_cpu(pd->config_size)>>1);
1028 for (dl = sb->dlist; dl ; dl = dl->next) {
1029 if (dl->disk.refnum == pd->refnum) {
1030 char *dv = map_dev(dl->major, dl->minor, 0);
1031 if (dv)
1032 printf(" Device[%d] : %s\n",
1033 i, dv);
1034 }
1035 }
1036 printf("\n");
1037 }
1038 }
1039
1040 static void examine_super_ddf(struct supertype *st, char *homehost)
1041 {
1042 struct ddf_super *sb = st->sb;
1043
1044 printf(" Magic : %08x\n", __be32_to_cpu(sb->anchor.magic));
1045 printf(" Version : %.8s\n", sb->anchor.revision);
1046 printf("Controller GUID : "); print_guid(sb->anchor.guid, 1);
1047 printf("\n");
1048 printf(" Seq : %08x\n", __be32_to_cpu(sb->active->seq));
1049 printf(" Redundant hdr : %s\n", sb->secondary.magic == DDF_HEADER_MAGIC
1050 ?"yes" : "no");
1051 examine_vds(sb);
1052 examine_pds(sb);
1053 }
1054
1055 static void brief_examine_super_ddf(struct supertype *st)
1056 {
1057 /* We just write a generic DDF ARRAY entry
1058 * The uuid is all hex, 6 groups of 4 bytes
1059 */
1060 struct ddf_super *ddf = st->sb;
1061 int i;
1062 printf("ARRAY /dev/ddf UUID=");
1063 for (i = 0; i < DDF_GUID_LEN; i++) {
1064 printf("%02x", ddf->anchor.guid[i]);
1065 if ((i&3) == 0 && i != 0)
1066 printf(":");
1067 }
1068 printf("\n");
1069 }
1070
1071 static void detail_super_ddf(struct supertype *st, char *homehost)
1072 {
1073 /* FIXME later
1074 * Could print DDF GUID
1075 * Need to find which array
1076 * If whole, briefly list all arrays
1077 * If one, give name
1078 */
1079 }
1080
1081 static void brief_detail_super_ddf(struct supertype *st)
1082 {
1083 /* FIXME I really need to know which array we are detailing.
1084 * Can that be stored in ddf_super??
1085 */
1086 // struct ddf_super *ddf = st->sb;
1087 }
1088
1089
1090 #endif
1091
1092 static int match_home_ddf(struct supertype *st, char *homehost)
1093 {
1094 /* It matches 'this' host if the controller is a
1095 * Linux-MD controller with vendor_data matching
1096 * the hostname
1097 */
1098 struct ddf_super *ddf = st->sb;
1099 int len = strlen(homehost);
1100
1101 return (memcmp(ddf->controller.guid, T10, 8) == 0 &&
1102 len < sizeof(ddf->controller.vendor_data) &&
1103 memcmp(ddf->controller.vendor_data, homehost,len) == 0 &&
1104 ddf->controller.vendor_data[len] == 0);
1105 }
1106
1107 static struct vd_config *find_vdcr(struct ddf_super *ddf)
1108 {
1109 /* FIXME this just picks off the first one */
1110 return &ddf->conflist->conf;
1111 }
1112
1113 static void uuid_from_super_ddf(struct supertype *st, int uuid[4])
1114 {
1115 /* The uuid returned here is used for:
1116 * uuid to put into bitmap file (Create, Grow)
1117 * uuid for backup header when saving critical section (Grow)
1118 * comparing uuids when re-adding a device into an array
1119 * For each of these we can make do with a truncated
1120 * or hashed uuid rather than the original, as long as
1121 * everyone agrees.
1122 * In each case the uuid required is that of the data-array,
1123 * not the device-set.
1124 * In the case of SVD we assume the BVD is of interest,
1125 * though that might be the case if a bitmap were made for
1126 * a mirrored SVD - worry about that later.
1127 * So we need to find the VD configuration record for the
1128 * relevant BVD and extract the GUID and Secondary_Element_Seq.
1129 * The first 16 bytes of the sha1 of these is used.
1130 */
1131 struct ddf_super *ddf = st->sb;
1132 struct vd_config *vd = find_vdcr(ddf);
1133
1134 if (!vd)
1135 memset(uuid, 0, sizeof (uuid));
1136 else {
1137 char buf[20];
1138 struct sha1_ctx ctx;
1139 sha1_init_ctx(&ctx);
1140 sha1_process_bytes(&vd->guid, DDF_GUID_LEN, &ctx);
1141 if (vd->sec_elmnt_count > 1)
1142 sha1_process_bytes(&vd->sec_elmnt_seq, 1, &ctx);
1143 sha1_finish_ctx(&ctx, buf);
1144 memcpy(uuid, buf, sizeof(uuid));
1145 }
1146 }
1147
1148 static void getinfo_super_ddf(struct supertype *st, struct mdinfo *info)
1149 {
1150 struct ddf_super *ddf = st->sb;
1151
1152 info->array.major_version = 1000;
1153 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1154 info->array.patch_version = 0;
1155 info->array.raid_disks = __be16_to_cpu(ddf->phys->used_pdes);
1156 info->array.level = LEVEL_CONTAINER;
1157 info->array.layout = 0;
1158 info->array.md_minor = -1;
1159 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)
1160 (ddf->anchor.guid+16));
1161 info->array.utime = 0;
1162 info->array.chunk_size = 0;
1163
1164 // info->data_offset = ???;
1165 // info->component_size = ???;
1166
1167 info->disk.major = 0;
1168 info->disk.minor = 0;
1169 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1170 // info->disk.raid_disk = find refnum in the table and use index;
1171 // info->disk.state = ???;
1172
1173 // uuid_from_super_ddf(info->uuid, sbv);
1174
1175 // info->name[] ?? ;
1176 }
1177
1178 static void getinfo_super_ddf_bvd(struct supertype *st, struct mdinfo *info)
1179 {
1180 struct ddf_super *ddf = st->sb;
1181 struct vd_config *vd = find_vdcr(ddf);
1182
1183 /* FIXME this returns BVD info - what if we want SVD ?? */
1184
1185 info->array.major_version = 1000;
1186 info->array.minor_version = 0; /* FIXME use ddf->revision somehow */
1187 info->array.patch_version = 0;
1188 info->array.raid_disks = __be16_to_cpu(vd->prim_elmnt_count);
1189 info->array.level = map_num1(ddf_level_num, vd->prl);
1190 info->array.layout = vd->rlq; /* FIXME should this be mapped */
1191 info->array.md_minor = -1;
1192 info->array.ctime = DECADE + __be32_to_cpu(*(__u32*)(vd->guid+16));
1193 info->array.utime = DECADE + __be32_to_cpu(vd->timestamp);
1194 info->array.chunk_size = 512 << vd->chunk_shift;
1195
1196 // info->data_offset = ???;
1197 // info->component_size = ???;
1198
1199 info->disk.major = 0;
1200 info->disk.minor = 0;
1201 // info->disk.number = __be32_to_cpu(ddf->disk.refnum);
1202 // info->disk.raid_disk = find refnum in the table and use index;
1203 // info->disk.state = ???;
1204
1205 uuid_from_super_ddf(st, info->uuid);
1206
1207 // info->name[] ?? ;
1208 }
1209
1210 static int update_super_ddf(struct supertype *st, struct mdinfo *info,
1211 char *update,
1212 char *devname, int verbose,
1213 int uuid_set, char *homehost)
1214 {
1215 /* For 'assemble' and 'force' we need to return non-zero if any
1216 * change was made. For others, the return value is ignored.
1217 * Update options are:
1218 * force-one : This device looks a bit old but needs to be included,
1219 * update age info appropriately.
1220 * assemble: clear any 'faulty' flag to allow this device to
1221 * be assembled.
1222 * force-array: Array is degraded but being forced, mark it clean
1223 * if that will be needed to assemble it.
1224 *
1225 * newdev: not used ????
1226 * grow: Array has gained a new device - this is currently for
1227 * linear only
1228 * resync: mark as dirty so a resync will happen.
1229 * uuid: Change the uuid of the array to match watch is given
1230 * homehost: update the recorded homehost
1231 * name: update the name - preserving the homehost
1232 * _reshape_progress: record new reshape_progress position.
1233 *
1234 * Following are not relevant for this version:
1235 * sparc2.2 : update from old dodgey metadata
1236 * super-minor: change the preferred_minor number
1237 * summaries: update redundant counters.
1238 */
1239 int rv = 0;
1240 // struct ddf_super *ddf = st->sb;
1241 // struct vd_config *vd = find_vdcr(ddf);
1242 // struct virtual_entry *ve = find_ve(ddf);
1243
1244
1245 /* we don't need to handle "force-*" or "assemble" as
1246 * there is no need to 'trick' the kernel. We the metadata is
1247 * first updated to activate the array, all the implied modifications
1248 * will just happen.
1249 */
1250
1251 if (strcmp(update, "grow") == 0) {
1252 /* FIXME */
1253 }
1254 if (strcmp(update, "resync") == 0) {
1255 // info->resync_checkpoint = 0;
1256 }
1257 /* We ignore UUID updates as they make even less sense
1258 * with DDF
1259 */
1260 if (strcmp(update, "homehost") == 0) {
1261 /* homehost is stored in controller->vendor_data,
1262 * or it is when we are the vendor
1263 */
1264 // if (info->vendor_is_local)
1265 // strcpy(ddf->controller.vendor_data, homehost);
1266 }
1267 if (strcmp(update, "name") == 0) {
1268 /* name is stored in virtual_entry->name */
1269 // memset(ve->name, ' ', 16);
1270 // strncpy(ve->name, info->name, 16);
1271 }
1272 if (strcmp(update, "_reshape_progress") == 0) {
1273 /* We don't support reshape yet */
1274 }
1275
1276 // update_all_csum(ddf);
1277
1278 return rv;
1279 }
1280
1281 static int init_super_ddf(struct supertype *st,
1282 mdu_array_info_t *info,
1283 unsigned long long size, char *name, char *homehost,
1284 int *uuid)
1285 {
1286 /* This is primarily called by Create when creating a new array.
1287 * We will then get add_to_super called for each component, and then
1288 * write_init_super called to write it out to each device.
1289 * For DDF, Create can create on fresh devices or on a pre-existing
1290 * array.
1291 * To create on a pre-existing array a different method will be called.
1292 * This one is just for fresh drives.
1293 *
1294 * We need to create the entire 'ddf' structure which includes:
1295 * DDF headers - these are easy.
1296 * Controller data - a Sector describing this controller .. not that
1297 * this is a controller exactly.
1298 * Physical Disk Record - one entry per device, so
1299 * leave plenty of space.
1300 * Virtual Disk Records - again, just leave plenty of space.
1301 * This just lists VDs, doesn't give details
1302 * Config records - describes the VDs that use this disk
1303 * DiskData - describes 'this' device.
1304 * BadBlockManagement - empty
1305 * Diag Space - empty
1306 * Vendor Logs - Could we put bitmaps here?
1307 *
1308 */
1309 struct ddf_super *ddf;
1310 char hostname[17];
1311 int hostlen;
1312 __u32 stamp;
1313 int rfd;
1314 int max_phys_disks, max_virt_disks;
1315 unsigned long long sector;
1316 int clen;
1317 int i;
1318 int pdsize, vdsize;
1319 struct phys_disk *pd;
1320 struct virtual_disk *vd;
1321
1322 ddf = malloc(sizeof(*ddf));
1323 ddf->dlist = NULL; /* no physical disks yet */
1324 ddf->conflist = NULL; /* No virtual disks yet */
1325
1326 /* At least 32MB *must* be reserved for the ddf. So let's just
1327 * start 32MB from the end, and put the primary header there.
1328 * Don't do secondary for now.
1329 * We don't know exactly where that will be yet as it could be
1330 * different on each device. To just set up the lengths.
1331 *
1332 */
1333
1334 ddf->anchor.magic = DDF_HEADER_MAGIC;
1335 /* 24 bytes of fiction required.
1336 * first 8 are a 'vendor-id' - "Linux-MD"
1337 * next 8 are controller type.. how about 0X DEAD BEEF 0000 0000
1338 * Remaining 16 are serial number.... maybe a hostname would do?
1339 */
1340 memcpy(ddf->anchor.guid, T10, sizeof(T10));
1341 stamp = __cpu_to_be32(0xdeadbeef);
1342 memcpy(ddf->anchor.guid+8, &stamp, 4);
1343 stamp = __cpu_to_be32(0);
1344 memcpy(ddf->anchor.guid+12, &stamp, 4);
1345 stamp = __cpu_to_be32(time(0) - DECADE);
1346 memcpy(ddf->anchor.guid+16, &stamp, 4);
1347 rfd = open("/dev/urandom", O_RDONLY);
1348 if (rfd < 0 || read(rfd, &stamp, 4) != 4)
1349 stamp = random();
1350 memcpy(ddf->anchor.guid+20, &stamp, 4);
1351 if (rfd >= 0) close(rfd);
1352
1353 memcpy(ddf->anchor.revision, DDF_REVISION, 8);
1354 ddf->anchor.seq = __cpu_to_be32(1);
1355 ddf->anchor.timestamp = __cpu_to_be32(time(0) - DECADE);
1356 ddf->anchor.openflag = 0xFF;
1357 ddf->anchor.foreignflag = 0;
1358 ddf->anchor.enforcegroups = 0; /* Is this best?? */
1359 ddf->anchor.pad0 = 0xff;
1360 memset(ddf->anchor.pad1, 0xff, 12);
1361 memset(ddf->anchor.header_ext, 0xff, 32);
1362 ddf->anchor.primary_lba = ~(__u64)0;
1363 ddf->anchor.secondary_lba = ~(__u64)0;
1364 ddf->anchor.type = DDF_HEADER_ANCHOR;
1365 memset(ddf->anchor.pad2, 0xff, 3);
1366 ddf->anchor.workspace_len = __cpu_to_be32(32768); /* Must be reserved */
1367 ddf->anchor.workspace_lba = ~(__u64)0; /* Put this at bottom
1368 of 32M reserved.. */
1369 max_phys_disks = 1023; /* Should be enough */
1370 ddf->anchor.max_pd_entries = __cpu_to_be16(max_phys_disks);
1371 max_virt_disks = 255;
1372 ddf->anchor.max_vd_entries = __cpu_to_be16(max_virt_disks); /* ?? */
1373 ddf->anchor.max_partitions = __cpu_to_be16(64); /* ?? */
1374 ddf->max_part = 64;
1375 ddf->anchor.config_record_len = __cpu_to_be16(1 + 256*12/512);
1376 ddf->anchor.max_primary_element_entries = __cpu_to_be16(256);
1377 memset(ddf->anchor.pad3, 0xff, 54);
1378
1379 /* controller sections is one sector long immediately
1380 * after the ddf header */
1381 sector = 1;
1382 ddf->anchor.controller_section_offset = __cpu_to_be32(sector);
1383 ddf->anchor.controller_section_length = __cpu_to_be32(1);
1384 sector += 1;
1385
1386 /* phys is 8 sectors after that */
1387 pdsize = ROUND_UP(sizeof(struct phys_disk) +
1388 sizeof(struct phys_disk_entry)*max_phys_disks,
1389 512);
1390 switch(pdsize/512) {
1391 case 2: case 8: case 32: case 128: case 512: break;
1392 default: abort();
1393 }
1394 ddf->anchor.phys_section_offset = __cpu_to_be32(sector);
1395 ddf->anchor.phys_section_length =
1396 __cpu_to_be32(pdsize/512); /* max_primary_element_entries/8 */
1397 sector += pdsize/512;
1398
1399 /* virt is another 32 sectors */
1400 vdsize = ROUND_UP(sizeof(struct virtual_disk) +
1401 sizeof(struct virtual_entry) * max_virt_disks,
1402 512);
1403 switch(vdsize/512) {
1404 case 2: case 8: case 32: case 128: case 512: break;
1405 default: abort();
1406 }
1407 ddf->anchor.virt_section_offset = __cpu_to_be32(sector);
1408 ddf->anchor.virt_section_length =
1409 __cpu_to_be32(vdsize/512); /* max_vd_entries/8 */
1410 sector += vdsize/512;
1411
1412 clen = (1 + 256*12/512) * (64+1);
1413 ddf->anchor.config_section_offset = __cpu_to_be32(sector);
1414 ddf->anchor.config_section_length = __cpu_to_be32(clen);
1415 sector += clen;
1416
1417 ddf->anchor.data_section_offset = __cpu_to_be32(sector);
1418 ddf->anchor.data_section_length = __cpu_to_be32(1);
1419 sector += 1;
1420
1421 ddf->anchor.bbm_section_length = __cpu_to_be32(0);
1422 ddf->anchor.bbm_section_offset = __cpu_to_be32(0xFFFFFFFF);
1423 ddf->anchor.diag_space_length = __cpu_to_be32(0);
1424 ddf->anchor.diag_space_offset = __cpu_to_be32(0xFFFFFFFF);
1425 ddf->anchor.vendor_length = __cpu_to_be32(0);
1426 ddf->anchor.vendor_offset = __cpu_to_be32(0xFFFFFFFF);
1427
1428 memset(ddf->anchor.pad4, 0xff, 256);
1429
1430 memcpy(&ddf->primary, &ddf->anchor, 512);
1431 memcpy(&ddf->secondary, &ddf->anchor, 512);
1432
1433 ddf->primary.openflag = 1; /* I guess.. */
1434 ddf->primary.type = DDF_HEADER_PRIMARY;
1435
1436 ddf->secondary.openflag = 1; /* I guess.. */
1437 ddf->secondary.type = DDF_HEADER_SECONDARY;
1438
1439 ddf->active = &ddf->primary;
1440
1441 ddf->controller.magic = DDF_CONTROLLER_MAGIC;
1442
1443 /* 24 more bytes of fiction required.
1444 * first 8 are a 'vendor-id' - "Linux-MD"
1445 * Remaining 16 are serial number.... maybe a hostname would do?
1446 */
1447 memcpy(ddf->controller.guid, T10, sizeof(T10));
1448 gethostname(hostname, 17);
1449 hostname[17] = 0;
1450 hostlen = strlen(hostname);
1451 memcpy(ddf->controller.guid + 24 - hostlen, hostname, hostlen);
1452 for (i = strlen(T10) ; i+hostlen < 24; i++)
1453 ddf->controller.guid[i] = ' ';
1454
1455 ddf->controller.type.vendor_id = __cpu_to_be16(0xDEAD);
1456 ddf->controller.type.device_id = __cpu_to_be16(0xBEEF);
1457 ddf->controller.type.sub_vendor_id = 0;
1458 ddf->controller.type.sub_device_id = 0;
1459 memcpy(ddf->controller.product_id, "What Is My PID??", 16);
1460 memset(ddf->controller.pad, 0xff, 8);
1461 memset(ddf->controller.vendor_data, 0xff, 448);
1462
1463 pd = ddf->phys = malloc(pdsize);
1464 ddf->pdsize = pdsize;
1465
1466 memset(pd, 0xff, pdsize);
1467 memset(pd, 0, sizeof(*pd));
1468 pd->magic = DDF_PHYS_DATA_MAGIC;
1469 pd->used_pdes = __cpu_to_be16(0);
1470 pd->max_pdes = __cpu_to_be16(max_phys_disks);
1471 memset(pd->pad, 0xff, 52);
1472
1473 vd = ddf->virt = malloc(vdsize);
1474 ddf->vdsize = vdsize;
1475 memset(vd, 0, vdsize);
1476 vd->magic = DDF_VIRT_RECORDS_MAGIC;
1477 vd->populated_vdes = __cpu_to_be16(0);
1478 vd->max_vdes = __cpu_to_be16(max_virt_disks);
1479 memset(vd->pad, 0xff, 52);
1480
1481 st->sb = ddf;
1482 return 1;
1483 }
1484
1485 /* add a device to a container, either while creating it or while
1486 * expanding a pre-existing container
1487 */
1488 static void add_to_super_ddf(struct supertype *st,
1489 mdu_disk_info_t *dk, int fd, char *devname)
1490 {
1491 struct ddf_super *ddf = st->sb;
1492 struct dl *dd;
1493 time_t now;
1494 struct tm *tm;
1495 unsigned long long size;
1496 struct phys_disk_entry *pde;
1497 int n, i;
1498 struct stat stb;
1499
1500 /* This is device numbered dk->number. We need to create
1501 * a phys_disk entry and a more detailed disk_data entry.
1502 */
1503 fstat(fd, &stb);
1504 dd = malloc(sizeof(*dd) + sizeof(dd->vlist[0]) * (ddf->max_part+1));
1505 dd->major = major(stb.st_rdev);
1506 dd->minor = minor(stb.st_rdev);
1507 dd->devname = devname;
1508 dd->next = ddf->dlist;
1509 dd->fd = fd;
1510
1511 dd->disk.magic = DDF_PHYS_DATA_MAGIC;
1512 now = time(0);
1513 tm = localtime(&now);
1514 sprintf(dd->disk.guid, "%8s%04d%02d%02d",
1515 T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
1516 *(__u32*)(dd->disk.guid + 16) = random();
1517 *(__u32*)(dd->disk.guid + 20) = random();
1518
1519 dd->disk.refnum = random(); /* and hope for the best */
1520 dd->disk.forced_ref = 1;
1521 dd->disk.forced_guid = 1;
1522 memset(dd->disk.vendor, ' ', 32);
1523 memcpy(dd->disk.vendor, "Linux", 5);
1524 memset(dd->disk.pad, 0xff, 442);
1525 for (i = 0; i < ddf->max_part+1 ; i++)
1526 dd->vlist[i] = NULL;
1527
1528 n = __be16_to_cpu(ddf->phys->used_pdes);
1529 pde = &ddf->phys->entries[n];
1530 n++;
1531 ddf->phys->used_pdes = __cpu_to_be16(n);
1532
1533 memcpy(pde->guid, dd->disk.guid, DDF_GUID_LEN);
1534 pde->refnum = dd->disk.refnum;
1535 pde->type = __cpu_to_be16(DDF_Forced_PD_GUID |DDF_Global_Spare);
1536 pde->state = __cpu_to_be16(DDF_Online);
1537 get_dev_size(fd, NULL, &size);
1538 /* We are required to reserve 32Meg, and record the size in sectors */
1539 pde->config_size = __cpu_to_be64( (size - 32*1024*1024) / 512);
1540 sprintf(pde->path, "%17.17s","Information: nil") ;
1541 memset(pde->pad, 0xff, 6);
1542
1543 ddf->dlist = dd;
1544 }
1545
1546 /*
1547 * This is the write_init_super method for a ddf container. It is
1548 * called when creating a container or adding another device to a
1549 * container.
1550 */
1551
1552 #ifndef MDASSEMBLE
1553 static int write_init_super_ddf(struct supertype *st)
1554 {
1555
1556 struct ddf_super *ddf = st->sb;
1557 int i;
1558 struct dl *d;
1559 int n_config;
1560 int conf_size;
1561
1562 unsigned long long size, sector;
1563
1564 for (d = ddf->dlist; d; d=d->next) {
1565 int fd = d->fd;
1566
1567 if (fd < 0)
1568 continue;
1569
1570 /* We need to fill in the primary, (secondary) and workspace
1571 * lba's in the headers, set their checksums,
1572 * Also checksum phys, virt....
1573 *
1574 * Then write everything out, finally the anchor is written.
1575 */
1576 get_dev_size(fd, NULL, &size);
1577 size /= 512;
1578 ddf->anchor.workspace_lba = __cpu_to_be64(size - 32*1024*2);
1579 ddf->anchor.primary_lba = __cpu_to_be64(size - 16*1024*2);
1580 ddf->anchor.seq = __cpu_to_be32(1);
1581 memcpy(&ddf->primary, &ddf->anchor, 512);
1582 memcpy(&ddf->secondary, &ddf->anchor, 512);
1583
1584 ddf->anchor.openflag = 0xFF; /* 'open' means nothing */
1585 ddf->anchor.seq = 0xFFFFFFFF; /* no sequencing in anchor */
1586 ddf->anchor.crc = calc_crc(&ddf->anchor, 512);
1587
1588 ddf->primary.openflag = 0;
1589 ddf->primary.type = DDF_HEADER_PRIMARY;
1590
1591 ddf->secondary.openflag = 0;
1592 ddf->secondary.type = DDF_HEADER_SECONDARY;
1593
1594 ddf->primary.crc = calc_crc(&ddf->primary, 512);
1595 ddf->secondary.crc = calc_crc(&ddf->secondary, 512);
1596
1597 sector = size - 16*1024*2;
1598 lseek64(fd, sector<<9, 0);
1599 write(fd, &ddf->primary, 512);
1600
1601 ddf->controller.crc = calc_crc(&ddf->controller, 512);
1602 write(fd, &ddf->controller, 512);
1603
1604 ddf->phys->crc = calc_crc(ddf->phys, ddf->pdsize);
1605
1606 write(fd, ddf->phys, ddf->pdsize);
1607
1608 ddf->virt->crc = calc_crc(ddf->virt, ddf->vdsize);
1609 write(fd, ddf->virt, ddf->vdsize);
1610
1611 /* Now write lots of config records. */
1612 n_config = __be16_to_cpu(ddf->active->max_partitions);
1613 conf_size = __be16_to_cpu(ddf->active->config_record_len) * 512;
1614 for (i = 0 ; i <= n_config ; i++) {
1615 struct vcl *c = d->vlist[i];
1616
1617 if (c) {
1618 c->conf.crc = calc_crc(&c->conf, conf_size);
1619 write(fd, &c->conf, conf_size);
1620 } else {
1621 __u32 sig = 0xffffffff;
1622 write(fd, &sig, 4);
1623 lseek64(fd, conf_size-4, SEEK_CUR);
1624 }
1625 }
1626 d->disk.crc = calc_crc(&d->disk, 512);
1627 write(fd, &d->disk, 512);
1628
1629 /* Maybe do the same for secondary */
1630
1631 lseek64(fd, (size-1)*512, SEEK_SET);
1632 write(fd, &ddf->anchor, 512);
1633 close(fd);
1634 }
1635 return 1;
1636 }
1637 #endif
1638
1639 static __u64 avail_size_ddf(struct supertype *st, __u64 devsize)
1640 {
1641 /* We must reserve the last 32Meg */
1642 if (devsize <= 32*1024*2)
1643 return 0;
1644 return devsize - 32*1024*2;
1645 }
1646
1647 #ifndef MDASSEMBLE
1648 int validate_geometry_ddf(struct supertype *st,
1649 int level, int layout, int raiddisks,
1650 int chunk, unsigned long long size,
1651 char *dev, unsigned long long *freesize)
1652 {
1653 int fd;
1654 struct mdinfo *sra;
1655 int cfd;
1656
1657 /* ddf potentially supports lots of things, but it depends on
1658 * what devices are offered (and maybe kernel version?)
1659 * If given unused devices, we will make a container.
1660 * If given devices in a container, we will make a BVD.
1661 * If given BVDs, we make an SVD, changing all the GUIDs in the process.
1662 */
1663
1664 if (level == LEVEL_CONTAINER) {
1665 st->ss = &super_ddf_container;
1666 return st->ss->validate_geometry(st, level, layout, raiddisks,
1667 chunk, size, dev, freesize);
1668 }
1669 if (!dev)
1670 return 1;
1671
1672 /* This device needs to be either a device in a 'ddf' container,
1673 * or it needs to be a 'ddf-bvd' array. Test the first first.
1674 */
1675
1676 fd = open(dev, O_RDONLY|O_EXCL, 0);
1677 if (fd >= 0) {
1678 sra = sysfs_read(fd, 0, GET_VERSION);
1679 close(fd);
1680 if (sra && sra->array.major_version == -1 &&
1681 strcmp(sra->text_version, "ddf-bvd") == 0) {
1682 st->ss = &super_ddf_svd;
1683 return st->ss->validate_geometry(st, level, layout,
1684 raiddisks, chunk, size,
1685 dev, freesize);
1686 }
1687
1688 fprintf(stderr,
1689 Name ": Cannot create this array on device %s\n",
1690 dev);
1691 return 0;
1692 }
1693 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
1694 fprintf(stderr, Name ": Cannot open %s: %s\n",
1695 dev, strerror(errno));
1696 return 0;
1697 }
1698 /* Well, it is in use by someone, maybe a 'ddf' container. */
1699 cfd = open_container(fd);
1700 if (cfd < 0) {
1701 close(fd);
1702 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
1703 dev);
1704 return 0;
1705 }
1706 sra = sysfs_read(cfd, 0, GET_VERSION);
1707 close(fd);
1708 if (sra && sra->array.major_version == -1 &&
1709 strcmp(sra->text_version, "ddf") == 0) {
1710 /* This is a member of a ddf container. Load the container
1711 * and try to create a bvd
1712 */
1713 struct ddf_super *ddf;
1714 st->ss = &super_ddf_bvd;
1715 if (load_super_ddf_all(st, cfd, (void **)&ddf, NULL, 1) == 0) {
1716 st->info = ddf;
1717 close(cfd);
1718 return st->ss->validate_geometry(st, level, layout,
1719 raiddisks, chunk, size,
1720 dev, freesize);
1721 }
1722 close(cfd);
1723 }
1724 fprintf(stderr, Name ": Cannot use %s: Already in use\n",
1725 dev);
1726 return 1;
1727 }
1728
1729 int validate_geometry_ddf_container(struct supertype *st,
1730 int level, int layout, int raiddisks,
1731 int chunk, unsigned long long size,
1732 char *dev, unsigned long long *freesize)
1733 {
1734 int fd;
1735 unsigned long long ldsize;
1736
1737 if (level != LEVEL_CONTAINER)
1738 return 0;
1739 if (!dev)
1740 return 1;
1741
1742 fd = open(dev, O_RDONLY|O_EXCL, 0);
1743 if (fd < 0) {
1744 fprintf(stderr, Name ": Cannot open %s: %s\n",
1745 dev, strerror(errno));
1746 return 0;
1747 }
1748 if (!get_dev_size(fd, dev, &ldsize)) {
1749 close(fd);
1750 return 0;
1751 }
1752 close(fd);
1753
1754 *freesize = avail_size_ddf(st, ldsize);
1755
1756 return 1;
1757 }
1758
1759 int validate_geometry_ddf_bvd(struct supertype *st,
1760 int level, int layout, int raiddisks,
1761 int chunk, unsigned long long size,
1762 char *dev, unsigned long long *freesize)
1763 {
1764 struct stat stb;
1765 struct ddf_super *ddf = st->sb;
1766 struct dl *dl;
1767 /* ddf/bvd supports lots of things, but not containers */
1768 if (level == LEVEL_CONTAINER)
1769 return 0;
1770 /* We must have the container info already read in. */
1771 if (!ddf)
1772 return 0;
1773
1774 /* This device must be a member of the set */
1775 if (stat(dev, &stb) < 0)
1776 return 0;
1777 if ((S_IFMT & stb.st_mode) != S_IFBLK)
1778 return 0;
1779 for (dl = ddf->dlist ; dl ; dl = dl->next) {
1780 if (dl->major == major(stb.st_rdev) &&
1781 dl->minor == minor(stb.st_rdev))
1782 break;
1783 }
1784 // FIXME here I am
1785
1786 return 1;
1787 }
1788 int validate_geometry_ddf_svd(struct supertype *st,
1789 int level, int layout, int raiddisks,
1790 int chunk, unsigned long long size,
1791 char *dev, unsigned long long *freesize)
1792 {
1793 /* dd/svd only supports striped, mirrored, concat, spanned... */
1794 if (level != LEVEL_LINEAR &&
1795 level != 0 &&
1796 level != 1)
1797 return 0;
1798 return 1;
1799 }
1800
1801
1802 static int load_super_ddf_all(struct supertype *st, int fd,
1803 void **sbp, char *devname, int keep_fd)
1804 {
1805 struct mdinfo *sra;
1806 struct ddf_super *super;
1807 struct mdinfo *sd, *best = NULL;
1808 int bestseq = 0;
1809 int seq;
1810 char nm[20];
1811 int dfd;
1812
1813 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
1814 if (!sra)
1815 return 1;
1816 if (sra->array.major_version != -1 ||
1817 sra->array.minor_version != -2 ||
1818 strcmp(sra->text_version, "ddf") != 0)
1819 return 1;
1820
1821 super = malloc(sizeof(*super));
1822 if (!super)
1823 return 1;
1824
1825 /* first, try each device, and choose the best ddf */
1826 for (sd = sra->devs ; sd ; sd = sd->next) {
1827 int rv;
1828 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1829 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
1830 if (!dfd)
1831 return 2;
1832 rv = load_ddf_headers(dfd, super, NULL);
1833 if (!keep_fd) close(dfd);
1834 if (rv == 0) {
1835 seq = __be32_to_cpu(super->active->seq);
1836 if (super->active->openflag)
1837 seq--;
1838 if (!best || seq > bestseq) {
1839 bestseq = seq;
1840 best = sd;
1841 }
1842 }
1843 }
1844 if (!best)
1845 return 1;
1846 /* OK, load this ddf */
1847 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
1848 dfd = dev_open(nm, O_RDONLY);
1849 if (!dfd)
1850 return 1;
1851 load_ddf_headers(dfd, super, NULL);
1852 load_ddf_global(dfd, super, NULL);
1853 close(dfd);
1854 /* Now we need the device-local bits */
1855 for (sd = sra->devs ; sd ; sd = sd->next) {
1856 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
1857 dfd = dev_open(nm, O_RDONLY);
1858 if (!dfd)
1859 return 2;
1860 seq = load_ddf_local(dfd, super, NULL, keep_fd);
1861 close(dfd);
1862 }
1863 *sbp = super;
1864 if (st->ss == NULL) {
1865 st->ss = &super_ddf;
1866 st->minor_version = 0;
1867 st->max_devs = 512;
1868 }
1869 return 0;
1870 }
1871 #endif
1872
1873
1874
1875 static int init_zero_ddf(struct supertype *st,
1876 mdu_array_info_t *info,
1877 unsigned long long size, char *name,
1878 char *homehost, int *uuid)
1879 {
1880 st->sb = NULL;
1881 return 0;
1882 }
1883
1884 static int store_zero_ddf(struct supertype *st, int fd)
1885 {
1886 unsigned long long dsize;
1887 char buf[512];
1888 memset(buf, 0, 512);
1889
1890
1891 if (!get_dev_size(fd, NULL, &dsize))
1892 return 1;
1893
1894 lseek64(fd, dsize-512, 0);
1895 write(fd, buf, 512);
1896 return 0;
1897 }
1898
1899 struct superswitch super_ddf = {
1900 #ifndef MDASSEMBLE
1901 .examine_super = examine_super_ddf,
1902 .brief_examine_super = brief_examine_super_ddf,
1903 .detail_super = detail_super_ddf,
1904 .brief_detail_super = brief_detail_super_ddf,
1905 .validate_geometry = validate_geometry_ddf,
1906 #endif
1907 .match_home = match_home_ddf,
1908 .uuid_from_super= uuid_from_super_ddf,
1909 .getinfo_super = getinfo_super_ddf,
1910 .update_super = update_super_ddf,
1911
1912 .avail_size = avail_size_ddf,
1913
1914 .load_super = load_super_ddf,
1915 .init_super = init_zero_ddf,
1916 .store_super = store_zero_ddf,
1917 .free_super = free_super_ddf,
1918 .match_metadata_desc = match_metadata_desc_ddf,
1919
1920
1921 .major = 1000,
1922 .swapuuid = 0,
1923 .external = 1,
1924 .text_version = "ddf",
1925 };
1926
1927 /* Super_ddf_container is set by validate_geometry_ddf when given a
1928 * device that is not part of any array
1929 */
1930 struct superswitch super_ddf_container = {
1931 #ifndef MDASSEMBLE
1932 .validate_geometry = validate_geometry_ddf_container,
1933 .write_init_super = write_init_super_ddf,
1934 #endif
1935
1936 .init_super = init_super_ddf,
1937 .add_to_super = add_to_super_ddf,
1938
1939 .free_super = free_super_ddf,
1940
1941 .major = 1000,
1942 .swapuuid = 0,
1943 .external = 1,
1944 .text_version = "ddf",
1945 };
1946
1947 struct superswitch super_ddf_bvd = {
1948 #ifndef MDASSEMBLE
1949 // .detail_super = detail_super_ddf_bvd,
1950 // .brief_detail_super = brief_detail_super_ddf_bvd,
1951 .validate_geometry = validate_geometry_ddf_bvd,
1952 #endif
1953 .update_super = update_super_ddf,
1954 .init_super = init_super_ddf,
1955 .getinfo_super = getinfo_super_ddf_bvd,
1956
1957 .load_super = load_super_ddf,
1958 .free_super = free_super_ddf,
1959 .match_metadata_desc = match_metadata_desc_ddf_bvd,
1960
1961
1962 .major = 1001,
1963 .swapuuid = 0,
1964 .external = 1,
1965 .text_version = "ddf",
1966 };
1967
1968 struct superswitch super_ddf_svd = {
1969 #ifndef MDASSEMBLE
1970 // .detail_super = detail_super_ddf_svd,
1971 // .brief_detail_super = brief_detail_super_ddf_svd,
1972 .validate_geometry = validate_geometry_ddf_bvd,
1973 #endif
1974 .update_super = update_super_ddf,
1975 .init_super = init_super_ddf,
1976
1977 .load_super = load_super_ddf,
1978 .free_super = free_super_ddf,
1979 .match_metadata_desc = match_metadata_desc_ddf_svd,
1980
1981
1982 .major = 1002,
1983 .swapuuid = 0,
1984 .external = 1,
1985 .text_version = "ddf",
1986 };
3030 #include "md_p.h"
3131 #include <sys/utsname.h>
3232 #include <ctype.h>
33 #include <dirent.h>
3334
3435 /*
3536 * following taken from linux/blkpg.h because they aren't
758759 return fd;
759760 }
760761
761 struct superswitch *superlist[] = { &super0, &super1, NULL };
762 struct superswitch *superlist[] = { &super0, &super1, &super_ddf, NULL };
762763
763764 #if !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO)
764765 struct supertype *super_by_fd(int fd)
907908 return;
908909 }
909910
911 int open_container(int fd)
912 {
913 /* 'fd' is a block device. Find out if it is in use
914 * by a container, and return an open fd on that container.
915 */
916 char path[256];
917 char *e;
918 DIR *dir;
919 struct dirent *de;
920 int dfd, n;
921 char buf[200];
922 int major, minor;
923 struct stat st;
924
925 if (fstat(fd, &st) != 0)
926 return -1;
927 sprintf(path, "/sys/dev/block/%d:%d/holders",
928 (int)major(st.st_rdev), (int)minor(st.st_rdev));
929 e = path + strlen(path);
930
931 dir = opendir(path);
932 if (!dir)
933 return -1;
934 while ((de = readdir(dir))) {
935 if (de->d_ino == 0)
936 continue;
937 if (de->d_name[0] == '.')
938 continue;
939 sprintf(e, "/%s/dev", de->d_name);
940 dfd = open(path, O_RDONLY);
941 if (dfd < 0)
942 continue;
943 n = read(dfd, buf, sizeof(buf));
944 close(dfd);
945 if (n <= 0 || n >= sizeof(buf))
946 continue;
947 buf[n] = 0;
948 if (sscanf(buf, "%d:%d", &major, &minor) != 2)
949 continue;
950 sprintf(buf, "%d:%d", major, minor);
951 dfd = dev_open(buf, O_RDONLY);
952 if (dfd >= 0) {
953 closedir(dir);
954 return dfd;
955 }
956 }
957 return -1;
958 }
959
910960 #ifdef __TINYC__
911961 /* tinyc doesn't optimize this check in ioctl.h out ... */
912962 unsigned int __invalid_size_argument_for_IOC = 0;