teach imsm and ddf what st->subarray means at load_super time
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "sha1.h"
24 #include "platform-intel.h"
25 #include <values.h>
26 #include <scsi/sg.h>
27 #include <ctype.h>
28 #include <dirent.h>
29
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
43
44 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
45 #define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
46 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
51 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
52 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
53
54 #define MPB_SECTOR_CNT 418
55 #define IMSM_RESERVED_SECTORS 4096
56 #define SECT_PER_MB_SHIFT 11
57
58 /* Disk configuration info. */
59 #define IMSM_MAX_DEVICES 255
60 struct imsm_disk {
61 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
62 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
63 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
64 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
65 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
66 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
67 #define USABLE_DISK __cpu_to_le32(0x08) /* Fully usable unless FAILED_DISK is set */
68 __u32 status; /* 0xF0 - 0xF3 */
69 __u32 owner_cfg_num; /* which config 0,1,2... owns this disk */
70 #define IMSM_DISK_FILLERS 4
71 __u32 filler[IMSM_DISK_FILLERS]; /* 0xF4 - 0x107 MPB_DISK_FILLERS for future expansion */
72 };
73
74 /* RAID map configuration infos. */
75 struct imsm_map {
76 __u32 pba_of_lba0; /* start address of partition */
77 __u32 blocks_per_member;/* blocks per member */
78 __u32 num_data_stripes; /* number of data stripes */
79 __u16 blocks_per_strip;
80 __u8 map_state; /* Normal, Uninitialized, Degraded, Failed */
81 #define IMSM_T_STATE_NORMAL 0
82 #define IMSM_T_STATE_UNINITIALIZED 1
83 #define IMSM_T_STATE_DEGRADED 2
84 #define IMSM_T_STATE_FAILED 3
85 __u8 raid_level;
86 #define IMSM_T_RAID0 0
87 #define IMSM_T_RAID1 1
88 #define IMSM_T_RAID5 5 /* since metadata version 1.2.02 ? */
89 __u8 num_members; /* number of member disks */
90 __u8 num_domains; /* number of parity domains */
91 __u8 failed_disk_num; /* valid only when state is degraded */
92 __u8 ddf;
93 __u32 filler[7]; /* expansion area */
94 #define IMSM_ORD_REBUILD (1 << 24)
95 __u32 disk_ord_tbl[1]; /* disk_ord_tbl[num_members],
96 * top byte contains some flags
97 */
98 } __attribute__ ((packed));
99
100 struct imsm_vol {
101 __u32 curr_migr_unit;
102 __u32 checkpoint_id; /* id to access curr_migr_unit */
103 __u8 migr_state; /* Normal or Migrating */
104 #define MIGR_INIT 0
105 #define MIGR_REBUILD 1
106 #define MIGR_VERIFY 2 /* analagous to echo check > sync_action */
107 #define MIGR_GEN_MIGR 3
108 #define MIGR_STATE_CHANGE 4
109 #define MIGR_REPAIR 5
110 __u8 migr_type; /* Initializing, Rebuilding, ... */
111 __u8 dirty;
112 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
113 __u16 verify_errors; /* number of mismatches */
114 __u16 bad_blocks; /* number of bad blocks during verify */
115 __u32 filler[4];
116 struct imsm_map map[1];
117 /* here comes another one if migr_state */
118 } __attribute__ ((packed));
119
120 struct imsm_dev {
121 __u8 volume[MAX_RAID_SERIAL_LEN];
122 __u32 size_low;
123 __u32 size_high;
124 #define DEV_BOOTABLE __cpu_to_le32(0x01)
125 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
126 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
127 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
128 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
129 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
130 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
131 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
132 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
133 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
134 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
135 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
136 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
137 __u32 status; /* Persistent RaidDev status */
138 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
139 __u8 migr_priority;
140 __u8 num_sub_vols;
141 __u8 tid;
142 __u8 cng_master_disk;
143 __u16 cache_policy;
144 __u8 cng_state;
145 __u8 cng_sub_state;
146 #define IMSM_DEV_FILLERS 10
147 __u32 filler[IMSM_DEV_FILLERS];
148 struct imsm_vol vol;
149 } __attribute__ ((packed));
150
151 struct imsm_super {
152 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
153 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
154 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
155 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
156 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
157 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
158 __u32 attributes; /* 0x34 - 0x37 */
159 __u8 num_disks; /* 0x38 Number of configured disks */
160 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
161 __u8 error_log_pos; /* 0x3A */
162 __u8 fill[1]; /* 0x3B */
163 __u32 cache_size; /* 0x3c - 0x40 in mb */
164 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
165 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
166 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
167 #define IMSM_FILLERS 35
168 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
169 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
170 /* here comes imsm_dev[num_raid_devs] */
171 /* here comes BBM logs */
172 } __attribute__ ((packed));
173
174 #define BBM_LOG_MAX_ENTRIES 254
175
176 struct bbm_log_entry {
177 __u64 defective_block_start;
178 #define UNREADABLE 0xFFFFFFFF
179 __u32 spare_block_offset;
180 __u16 remapped_marked_count;
181 __u16 disk_ordinal;
182 } __attribute__ ((__packed__));
183
184 struct bbm_log {
185 __u32 signature; /* 0xABADB10C */
186 __u32 entry_count;
187 __u32 reserved_spare_block_count; /* 0 */
188 __u32 reserved; /* 0xFFFF */
189 __u64 first_spare_lba;
190 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
191 } __attribute__ ((__packed__));
192
193
194 #ifndef MDASSEMBLE
195 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
196 #endif
197
198 static __u8 migr_type(struct imsm_dev *dev)
199 {
200 if (dev->vol.migr_type == MIGR_VERIFY &&
201 dev->status & DEV_VERIFY_AND_FIX)
202 return MIGR_REPAIR;
203 else
204 return dev->vol.migr_type;
205 }
206
207 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
208 {
209 /* for compatibility with older oroms convert MIGR_REPAIR, into
210 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
211 */
212 if (migr_type == MIGR_REPAIR) {
213 dev->vol.migr_type = MIGR_VERIFY;
214 dev->status |= DEV_VERIFY_AND_FIX;
215 } else {
216 dev->vol.migr_type = migr_type;
217 dev->status &= ~DEV_VERIFY_AND_FIX;
218 }
219 }
220
221 static unsigned int sector_count(__u32 bytes)
222 {
223 return ((bytes + (512-1)) & (~(512-1))) / 512;
224 }
225
226 static unsigned int mpb_sectors(struct imsm_super *mpb)
227 {
228 return sector_count(__le32_to_cpu(mpb->mpb_size));
229 }
230
231 struct intel_dev {
232 struct imsm_dev *dev;
233 struct intel_dev *next;
234 int index;
235 };
236
237 /* internal representation of IMSM metadata */
238 struct intel_super {
239 union {
240 void *buf; /* O_DIRECT buffer for reading/writing metadata */
241 struct imsm_super *anchor; /* immovable parameters */
242 };
243 size_t len; /* size of the 'buf' allocation */
244 void *next_buf; /* for realloc'ing buf from the manager */
245 size_t next_len;
246 int updates_pending; /* count of pending updates for mdmon */
247 int creating_imsm; /* flag to indicate container creation */
248 int current_vol; /* index of raid device undergoing creation */
249 __u32 create_offset; /* common start for 'current_vol' */
250 struct intel_dev *devlist;
251 struct dl {
252 struct dl *next;
253 int index;
254 __u8 serial[MAX_RAID_SERIAL_LEN];
255 int major, minor;
256 char *devname;
257 struct imsm_disk disk;
258 int fd;
259 int extent_cnt;
260 struct extent *e; /* for determining freespace @ create */
261 int raiddisk; /* slot to fill in autolayout */
262 } *disks;
263 struct dl *add; /* list of disks to add while mdmon active */
264 struct dl *missing; /* disks removed while we weren't looking */
265 struct bbm_log *bbm_log;
266 const char *hba; /* device path of the raid controller for this metadata */
267 const struct imsm_orom *orom; /* platform firmware support */
268 };
269
270 struct extent {
271 unsigned long long start, size;
272 };
273
274 /* definition of messages passed to imsm_process_update */
275 enum imsm_update_type {
276 update_activate_spare,
277 update_create_array,
278 update_add_disk,
279 };
280
281 struct imsm_update_activate_spare {
282 enum imsm_update_type type;
283 struct dl *dl;
284 int slot;
285 int array;
286 struct imsm_update_activate_spare *next;
287 };
288
289 struct disk_info {
290 __u8 serial[MAX_RAID_SERIAL_LEN];
291 };
292
293 struct imsm_update_create_array {
294 enum imsm_update_type type;
295 int dev_idx;
296 struct imsm_dev dev;
297 };
298
299 struct imsm_update_add_disk {
300 enum imsm_update_type type;
301 };
302
303 static struct supertype *match_metadata_desc_imsm(char *arg)
304 {
305 struct supertype *st;
306
307 if (strcmp(arg, "imsm") != 0 &&
308 strcmp(arg, "default") != 0
309 )
310 return NULL;
311
312 st = malloc(sizeof(*st));
313 memset(st, 0, sizeof(*st));
314 st->ss = &super_imsm;
315 st->max_devs = IMSM_MAX_DEVICES;
316 st->minor_version = 0;
317 st->sb = NULL;
318 return st;
319 }
320
321 #ifndef MDASSEMBLE
322 static __u8 *get_imsm_version(struct imsm_super *mpb)
323 {
324 return &mpb->sig[MPB_SIG_LEN];
325 }
326 #endif
327
328 /* retrieve a disk directly from the anchor when the anchor is known to be
329 * up-to-date, currently only at load time
330 */
331 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
332 {
333 if (index >= mpb->num_disks)
334 return NULL;
335 return &mpb->disk[index];
336 }
337
338 #ifndef MDASSEMBLE
339 /* retrieve a disk from the parsed metadata */
340 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
341 {
342 struct dl *d;
343
344 for (d = super->disks; d; d = d->next)
345 if (d->index == index)
346 return &d->disk;
347
348 return NULL;
349 }
350 #endif
351
352 /* generate a checksum directly from the anchor when the anchor is known to be
353 * up-to-date, currently only at load or write_super after coalescing
354 */
355 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
356 {
357 __u32 end = mpb->mpb_size / sizeof(end);
358 __u32 *p = (__u32 *) mpb;
359 __u32 sum = 0;
360
361 while (end--) {
362 sum += __le32_to_cpu(*p);
363 p++;
364 }
365
366 return sum - __le32_to_cpu(mpb->check_sum);
367 }
368
369 static size_t sizeof_imsm_map(struct imsm_map *map)
370 {
371 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
372 }
373
374 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
375 {
376 struct imsm_map *map = &dev->vol.map[0];
377
378 if (second_map && !dev->vol.migr_state)
379 return NULL;
380 else if (second_map) {
381 void *ptr = map;
382
383 return ptr + sizeof_imsm_map(map);
384 } else
385 return map;
386
387 }
388
389 /* return the size of the device.
390 * migr_state increases the returned size if map[0] were to be duplicated
391 */
392 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
393 {
394 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
395 sizeof_imsm_map(get_imsm_map(dev, 0));
396
397 /* migrating means an additional map */
398 if (dev->vol.migr_state)
399 size += sizeof_imsm_map(get_imsm_map(dev, 1));
400 else if (migr_state)
401 size += sizeof_imsm_map(get_imsm_map(dev, 0));
402
403 return size;
404 }
405
406 #ifndef MDASSEMBLE
407 /* retrieve disk serial number list from a metadata update */
408 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
409 {
410 void *u = update;
411 struct disk_info *inf;
412
413 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
414 sizeof_imsm_dev(&update->dev, 0);
415
416 return inf;
417 }
418 #endif
419
420 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
421 {
422 int offset;
423 int i;
424 void *_mpb = mpb;
425
426 if (index >= mpb->num_raid_devs)
427 return NULL;
428
429 /* devices start after all disks */
430 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
431
432 for (i = 0; i <= index; i++)
433 if (i == index)
434 return _mpb + offset;
435 else
436 offset += sizeof_imsm_dev(_mpb + offset, 0);
437
438 return NULL;
439 }
440
441 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
442 {
443 struct intel_dev *dv;
444
445 if (index >= super->anchor->num_raid_devs)
446 return NULL;
447 for (dv = super->devlist; dv; dv = dv->next)
448 if (dv->index == index)
449 return dv->dev;
450 return NULL;
451 }
452
453 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
454 {
455 struct imsm_map *map;
456
457 if (dev->vol.migr_state)
458 map = get_imsm_map(dev, 1);
459 else
460 map = get_imsm_map(dev, 0);
461
462 /* top byte identifies disk under rebuild */
463 return __le32_to_cpu(map->disk_ord_tbl[slot]);
464 }
465
466 #define ord_to_idx(ord) (((ord) << 8) >> 8)
467 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
468 {
469 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
470
471 return ord_to_idx(ord);
472 }
473
474 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
475 {
476 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
477 }
478
479 static int get_imsm_disk_slot(struct imsm_map *map, int idx)
480 {
481 int slot;
482 __u32 ord;
483
484 for (slot = 0; slot < map->num_members; slot++) {
485 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
486 if (ord_to_idx(ord) == idx)
487 return slot;
488 }
489
490 return -1;
491 }
492
493 static int get_imsm_raid_level(struct imsm_map *map)
494 {
495 if (map->raid_level == 1) {
496 if (map->num_members == 2)
497 return 1;
498 else
499 return 10;
500 }
501
502 return map->raid_level;
503 }
504
505 static int cmp_extent(const void *av, const void *bv)
506 {
507 const struct extent *a = av;
508 const struct extent *b = bv;
509 if (a->start < b->start)
510 return -1;
511 if (a->start > b->start)
512 return 1;
513 return 0;
514 }
515
516 static int count_memberships(struct dl *dl, struct intel_super *super)
517 {
518 int memberships = 0;
519 int i;
520
521 for (i = 0; i < super->anchor->num_raid_devs; i++) {
522 struct imsm_dev *dev = get_imsm_dev(super, i);
523 struct imsm_map *map = get_imsm_map(dev, 0);
524
525 if (get_imsm_disk_slot(map, dl->index) >= 0)
526 memberships++;
527 }
528
529 return memberships;
530 }
531
532 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
533 {
534 /* find a list of used extents on the given physical device */
535 struct extent *rv, *e;
536 int i;
537 int memberships = count_memberships(dl, super);
538 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
539
540 rv = malloc(sizeof(struct extent) * (memberships + 1));
541 if (!rv)
542 return NULL;
543 e = rv;
544
545 for (i = 0; i < super->anchor->num_raid_devs; i++) {
546 struct imsm_dev *dev = get_imsm_dev(super, i);
547 struct imsm_map *map = get_imsm_map(dev, 0);
548
549 if (get_imsm_disk_slot(map, dl->index) >= 0) {
550 e->start = __le32_to_cpu(map->pba_of_lba0);
551 e->size = __le32_to_cpu(map->blocks_per_member);
552 e++;
553 }
554 }
555 qsort(rv, memberships, sizeof(*rv), cmp_extent);
556
557 /* determine the start of the metadata
558 * when no raid devices are defined use the default
559 * ...otherwise allow the metadata to truncate the value
560 * as is the case with older versions of imsm
561 */
562 if (memberships) {
563 struct extent *last = &rv[memberships - 1];
564 __u32 remainder;
565
566 remainder = __le32_to_cpu(dl->disk.total_blocks) -
567 (last->start + last->size);
568 /* round down to 1k block to satisfy precision of the kernel
569 * 'size' interface
570 */
571 remainder &= ~1UL;
572 /* make sure remainder is still sane */
573 if (remainder < ROUND_UP(super->len, 512) >> 9)
574 remainder = ROUND_UP(super->len, 512) >> 9;
575 if (reservation > remainder)
576 reservation = remainder;
577 }
578 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
579 e->size = 0;
580 return rv;
581 }
582
583 /* try to determine how much space is reserved for metadata from
584 * the last get_extents() entry, otherwise fallback to the
585 * default
586 */
587 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
588 {
589 struct extent *e;
590 int i;
591 __u32 rv;
592
593 /* for spares just return a minimal reservation which will grow
594 * once the spare is picked up by an array
595 */
596 if (dl->index == -1)
597 return MPB_SECTOR_CNT;
598
599 e = get_extents(super, dl);
600 if (!e)
601 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
602
603 /* scroll to last entry */
604 for (i = 0; e[i].size; i++)
605 continue;
606
607 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
608
609 free(e);
610
611 return rv;
612 }
613
614 #ifndef MDASSEMBLE
615 static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
616 {
617 __u64 sz;
618 int slot;
619 struct imsm_map *map = get_imsm_map(dev, 0);
620 __u32 ord;
621
622 printf("\n");
623 printf("[%.16s]:\n", dev->volume);
624 printf(" UUID : %s\n", uuid);
625 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
626 printf(" Members : %d\n", map->num_members);
627 slot = get_imsm_disk_slot(map, disk_idx);
628 if (slot >= 0) {
629 ord = get_imsm_ord_tbl_ent(dev, slot);
630 printf(" This Slot : %d%s\n", slot,
631 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
632 } else
633 printf(" This Slot : ?\n");
634 sz = __le32_to_cpu(dev->size_high);
635 sz <<= 32;
636 sz += __le32_to_cpu(dev->size_low);
637 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
638 human_size(sz * 512));
639 sz = __le32_to_cpu(map->blocks_per_member);
640 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
641 human_size(sz * 512));
642 printf(" Sector Offset : %u\n",
643 __le32_to_cpu(map->pba_of_lba0));
644 printf(" Num Stripes : %u\n",
645 __le32_to_cpu(map->num_data_stripes));
646 printf(" Chunk Size : %u KiB\n",
647 __le16_to_cpu(map->blocks_per_strip) / 2);
648 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
649 printf(" Migrate State : %s", dev->vol.migr_state ? "migrating" : "idle\n");
650 if (dev->vol.migr_state) {
651 if (migr_type(dev) == MIGR_INIT)
652 printf(": initializing\n");
653 else if (migr_type(dev) == MIGR_REBUILD)
654 printf(": rebuilding\n");
655 else if (migr_type(dev) == MIGR_VERIFY)
656 printf(": check\n");
657 else if (migr_type(dev) == MIGR_GEN_MIGR)
658 printf(": general migration\n");
659 else if (migr_type(dev) == MIGR_STATE_CHANGE)
660 printf(": state change\n");
661 else if (migr_type(dev) == MIGR_REPAIR)
662 printf(": repair\n");
663 else
664 printf(": <unknown:%d>\n", migr_type(dev));
665 }
666 printf(" Map State : %s", map_state_str[map->map_state]);
667 if (dev->vol.migr_state) {
668 struct imsm_map *map = get_imsm_map(dev, 1);
669 printf(" <-- %s", map_state_str[map->map_state]);
670 }
671 printf("\n");
672 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
673 }
674
675 static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
676 {
677 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
678 char str[MAX_RAID_SERIAL_LEN + 1];
679 __u32 s;
680 __u64 sz;
681
682 if (index < 0)
683 return;
684
685 printf("\n");
686 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
687 printf(" Disk%02d Serial : %s\n", index, str);
688 s = disk->status;
689 printf(" State :%s%s%s%s\n", s&SPARE_DISK ? " spare" : "",
690 s&CONFIGURED_DISK ? " active" : "",
691 s&FAILED_DISK ? " failed" : "",
692 s&USABLE_DISK ? " usable" : "");
693 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
694 sz = __le32_to_cpu(disk->total_blocks) - reserved;
695 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
696 human_size(sz * 512));
697 }
698
699 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info);
700
701 static void examine_super_imsm(struct supertype *st, char *homehost)
702 {
703 struct intel_super *super = st->sb;
704 struct imsm_super *mpb = super->anchor;
705 char str[MAX_SIGNATURE_LENGTH];
706 int i;
707 struct mdinfo info;
708 char nbuf[64];
709 __u32 sum;
710 __u32 reserved = imsm_reserved_sectors(super, super->disks);
711
712
713 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
714 printf(" Magic : %s\n", str);
715 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
716 printf(" Version : %s\n", get_imsm_version(mpb));
717 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
718 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
719 getinfo_super_imsm(st, &info);
720 fname_from_uuid(st, &info, nbuf, ':');
721 printf(" UUID : %s\n", nbuf + 5);
722 sum = __le32_to_cpu(mpb->check_sum);
723 printf(" Checksum : %08x %s\n", sum,
724 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
725 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
726 printf(" Disks : %d\n", mpb->num_disks);
727 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
728 print_imsm_disk(mpb, super->disks->index, reserved);
729 if (super->bbm_log) {
730 struct bbm_log *log = super->bbm_log;
731
732 printf("\n");
733 printf("Bad Block Management Log:\n");
734 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
735 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
736 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
737 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
738 printf(" First Spare : %llx\n",
739 (unsigned long long) __le64_to_cpu(log->first_spare_lba));
740 }
741 for (i = 0; i < mpb->num_raid_devs; i++) {
742 struct mdinfo info;
743 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
744
745 super->current_vol = i;
746 getinfo_super_imsm(st, &info);
747 fname_from_uuid(st, &info, nbuf, ':');
748 print_imsm_dev(dev, nbuf + 5, super->disks->index);
749 }
750 for (i = 0; i < mpb->num_disks; i++) {
751 if (i == super->disks->index)
752 continue;
753 print_imsm_disk(mpb, i, reserved);
754 }
755 }
756
757 static void brief_examine_super_imsm(struct supertype *st, int verbose)
758 {
759 /* We just write a generic IMSM ARRAY entry */
760 struct mdinfo info;
761 char nbuf[64];
762 char nbuf1[64];
763 struct intel_super *super = st->sb;
764 int i;
765
766 if (!super->anchor->num_raid_devs)
767 return;
768
769 getinfo_super_imsm(st, &info);
770 fname_from_uuid(st, &info, nbuf, ':');
771 for (i = 0; i < super->anchor->num_raid_devs; i++) {
772 struct imsm_dev *dev = get_imsm_dev(super, i);
773
774 super->current_vol = i;
775 getinfo_super_imsm(st, &info);
776 fname_from_uuid(st, &info, nbuf1, ':');
777 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
778 dev->volume, nbuf + 5, i, nbuf1 + 5);
779 }
780 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
781 }
782
783 static void export_examine_super_imsm(struct supertype *st)
784 {
785 struct intel_super *super = st->sb;
786 struct imsm_super *mpb = super->anchor;
787 struct mdinfo info;
788 char nbuf[64];
789
790 getinfo_super_imsm(st, &info);
791 fname_from_uuid(st, &info, nbuf, ':');
792 printf("MD_METADATA=imsm\n");
793 printf("MD_LEVEL=container\n");
794 printf("MD_UUID=%s\n", nbuf+5);
795 printf("MD_DEVICES=%u\n", mpb->num_disks);
796 }
797
798 static void detail_super_imsm(struct supertype *st, char *homehost)
799 {
800 struct mdinfo info;
801 char nbuf[64];
802
803 getinfo_super_imsm(st, &info);
804 fname_from_uuid(st, &info, nbuf, ':');
805 printf("\n UUID : %s\n", nbuf + 5);
806 }
807
808 static void brief_detail_super_imsm(struct supertype *st)
809 {
810 struct mdinfo info;
811 char nbuf[64];
812 getinfo_super_imsm(st, &info);
813 fname_from_uuid(st, &info, nbuf, ':');
814 printf(" UUID=%s", nbuf + 5);
815 }
816
817 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
818 static void fd2devname(int fd, char *name);
819
820 static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
821 {
822 /* dump an unsorted list of devices attached to ahci, as well as
823 * non-connected ports
824 */
825 int hba_len = strlen(hba_path) + 1;
826 struct dirent *ent;
827 DIR *dir;
828 char *path = NULL;
829 int err = 0;
830 unsigned long port_mask = (1 << port_count) - 1;
831
832 if (port_count > sizeof(port_mask) * 8) {
833 if (verbose)
834 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
835 return 2;
836 }
837
838 /* scroll through /sys/dev/block looking for devices attached to
839 * this hba
840 */
841 dir = opendir("/sys/dev/block");
842 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
843 int fd;
844 char model[64];
845 char vendor[64];
846 char buf[1024];
847 int major, minor;
848 char *device;
849 char *c;
850 int port;
851 int type;
852
853 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
854 continue;
855 path = devt_to_devpath(makedev(major, minor));
856 if (!path)
857 continue;
858 if (!path_attached_to_hba(path, hba_path)) {
859 free(path);
860 path = NULL;
861 continue;
862 }
863
864 /* retrieve the scsi device type */
865 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
866 if (verbose)
867 fprintf(stderr, Name ": failed to allocate 'device'\n");
868 err = 2;
869 break;
870 }
871 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
872 if (load_sys(device, buf) != 0) {
873 if (verbose)
874 fprintf(stderr, Name ": failed to read device type for %s\n",
875 path);
876 err = 2;
877 free(device);
878 break;
879 }
880 type = strtoul(buf, NULL, 10);
881
882 /* if it's not a disk print the vendor and model */
883 if (!(type == 0 || type == 7 || type == 14)) {
884 vendor[0] = '\0';
885 model[0] = '\0';
886 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
887 if (load_sys(device, buf) == 0) {
888 strncpy(vendor, buf, sizeof(vendor));
889 vendor[sizeof(vendor) - 1] = '\0';
890 c = (char *) &vendor[sizeof(vendor) - 1];
891 while (isspace(*c) || *c == '\0')
892 *c-- = '\0';
893
894 }
895 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
896 if (load_sys(device, buf) == 0) {
897 strncpy(model, buf, sizeof(model));
898 model[sizeof(model) - 1] = '\0';
899 c = (char *) &model[sizeof(model) - 1];
900 while (isspace(*c) || *c == '\0')
901 *c-- = '\0';
902 }
903
904 if (vendor[0] && model[0])
905 sprintf(buf, "%.64s %.64s", vendor, model);
906 else
907 switch (type) { /* numbers from hald/linux/device.c */
908 case 1: sprintf(buf, "tape"); break;
909 case 2: sprintf(buf, "printer"); break;
910 case 3: sprintf(buf, "processor"); break;
911 case 4:
912 case 5: sprintf(buf, "cdrom"); break;
913 case 6: sprintf(buf, "scanner"); break;
914 case 8: sprintf(buf, "media_changer"); break;
915 case 9: sprintf(buf, "comm"); break;
916 case 12: sprintf(buf, "raid"); break;
917 default: sprintf(buf, "unknown");
918 }
919 } else
920 buf[0] = '\0';
921 free(device);
922
923 /* chop device path to 'host%d' and calculate the port number */
924 c = strchr(&path[hba_len], '/');
925 *c = '\0';
926 if (sscanf(&path[hba_len], "host%d", &port) == 1)
927 port -= host_base;
928 else {
929 if (verbose) {
930 *c = '/'; /* repair the full string */
931 fprintf(stderr, Name ": failed to determine port number for %s\n",
932 path);
933 }
934 err = 2;
935 break;
936 }
937
938 /* mark this port as used */
939 port_mask &= ~(1 << port);
940
941 /* print out the device information */
942 if (buf[0]) {
943 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
944 continue;
945 }
946
947 fd = dev_open(ent->d_name, O_RDONLY);
948 if (fd < 0)
949 printf(" Port%d : - disk info unavailable -\n", port);
950 else {
951 fd2devname(fd, buf);
952 printf(" Port%d : %s", port, buf);
953 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
954 printf(" (%s)\n", buf);
955 else
956 printf("()\n");
957 }
958 close(fd);
959 free(path);
960 path = NULL;
961 }
962 if (path)
963 free(path);
964 if (dir)
965 closedir(dir);
966 if (err == 0) {
967 int i;
968
969 for (i = 0; i < port_count; i++)
970 if (port_mask & (1 << i))
971 printf(" Port%d : - no device attached -\n", i);
972 }
973
974 return err;
975 }
976
977 static int detail_platform_imsm(int verbose, int enumerate_only)
978 {
979 /* There are two components to imsm platform support, the ahci SATA
980 * controller and the option-rom. To find the SATA controller we
981 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
982 * controller with the Intel vendor id is present. This approach
983 * allows mdadm to leverage the kernel's ahci detection logic, with the
984 * caveat that if ahci.ko is not loaded mdadm will not be able to
985 * detect platform raid capabilities. The option-rom resides in a
986 * platform "Adapter ROM". We scan for its signature to retrieve the
987 * platform capabilities. If raid support is disabled in the BIOS the
988 * option-rom capability structure will not be available.
989 */
990 const struct imsm_orom *orom;
991 struct sys_dev *list, *hba;
992 DIR *dir;
993 struct dirent *ent;
994 const char *hba_path;
995 int host_base = 0;
996 int port_count = 0;
997
998 if (enumerate_only) {
999 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1000 return 0;
1001 return 2;
1002 }
1003
1004 list = find_driver_devices("pci", "ahci");
1005 for (hba = list; hba; hba = hba->next)
1006 if (devpath_to_vendor(hba->path) == 0x8086)
1007 break;
1008
1009 if (!hba) {
1010 if (verbose)
1011 fprintf(stderr, Name ": unable to find active ahci controller\n");
1012 free_sys_dev(&list);
1013 return 2;
1014 } else if (verbose)
1015 fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
1016 hba_path = hba->path;
1017 hba->path = NULL;
1018 free_sys_dev(&list);
1019
1020 orom = find_imsm_orom();
1021 if (!orom) {
1022 if (verbose)
1023 fprintf(stderr, Name ": imsm option-rom not found\n");
1024 return 2;
1025 }
1026
1027 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1028 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1029 orom->hotfix_ver, orom->build);
1030 printf(" RAID Levels :%s%s%s%s%s\n",
1031 imsm_orom_has_raid0(orom) ? " raid0" : "",
1032 imsm_orom_has_raid1(orom) ? " raid1" : "",
1033 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1034 imsm_orom_has_raid10(orom) ? " raid10" : "",
1035 imsm_orom_has_raid5(orom) ? " raid5" : "");
1036 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1037 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1038 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1039 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1040 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1041 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1042 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1043 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1044 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1045 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1046 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1047 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1048 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1049 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1050 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1051 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1052 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
1053 printf(" Max Disks : %d\n", orom->tds);
1054 printf(" Max Volumes : %d\n", orom->vpa);
1055 printf(" I/O Controller : %s\n", hba_path);
1056
1057 /* find the smallest scsi host number to determine a port number base */
1058 dir = opendir(hba_path);
1059 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1060 int host;
1061
1062 if (sscanf(ent->d_name, "host%d", &host) != 1)
1063 continue;
1064 if (port_count == 0)
1065 host_base = host;
1066 else if (host < host_base)
1067 host_base = host;
1068
1069 if (host + 1 > port_count + host_base)
1070 port_count = host + 1 - host_base;
1071
1072 }
1073 if (dir)
1074 closedir(dir);
1075
1076 if (!port_count || imsm_enumerate_ports(hba_path, port_count,
1077 host_base, verbose) != 0) {
1078 if (verbose)
1079 fprintf(stderr, Name ": failed to enumerate ports\n");
1080 return 2;
1081 }
1082
1083 return 0;
1084 }
1085 #endif
1086
1087 static int match_home_imsm(struct supertype *st, char *homehost)
1088 {
1089 /* the imsm metadata format does not specify any host
1090 * identification information. We return -1 since we can never
1091 * confirm nor deny whether a given array is "meant" for this
1092 * host. We rely on compare_super and the 'family_num' field to
1093 * exclude member disks that do not belong, and we rely on
1094 * mdadm.conf to specify the arrays that should be assembled.
1095 * Auto-assembly may still pick up "foreign" arrays.
1096 */
1097
1098 return -1;
1099 }
1100
1101 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1102 {
1103 /* The uuid returned here is used for:
1104 * uuid to put into bitmap file (Create, Grow)
1105 * uuid for backup header when saving critical section (Grow)
1106 * comparing uuids when re-adding a device into an array
1107 * In these cases the uuid required is that of the data-array,
1108 * not the device-set.
1109 * uuid to recognise same set when adding a missing device back
1110 * to an array. This is a uuid for the device-set.
1111 *
1112 * For each of these we can make do with a truncated
1113 * or hashed uuid rather than the original, as long as
1114 * everyone agrees.
1115 * In each case the uuid required is that of the data-array,
1116 * not the device-set.
1117 */
1118 /* imsm does not track uuid's so we synthesis one using sha1 on
1119 * - The signature (Which is constant for all imsm array, but no matter)
1120 * - the family_num of the container
1121 * - the index number of the volume
1122 * - the 'serial' number of the volume.
1123 * Hopefully these are all constant.
1124 */
1125 struct intel_super *super = st->sb;
1126
1127 char buf[20];
1128 struct sha1_ctx ctx;
1129 struct imsm_dev *dev = NULL;
1130
1131 sha1_init_ctx(&ctx);
1132 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
1133 sha1_process_bytes(&super->anchor->family_num, sizeof(__u32), &ctx);
1134 if (super->current_vol >= 0)
1135 dev = get_imsm_dev(super, super->current_vol);
1136 if (dev) {
1137 __u32 vol = super->current_vol;
1138 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1139 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1140 }
1141 sha1_finish_ctx(&ctx, buf);
1142 memcpy(uuid, buf, 4*4);
1143 }
1144
1145 #if 0
1146 static void
1147 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
1148 {
1149 __u8 *v = get_imsm_version(mpb);
1150 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1151 char major[] = { 0, 0, 0 };
1152 char minor[] = { 0 ,0, 0 };
1153 char patch[] = { 0, 0, 0 };
1154 char *ver_parse[] = { major, minor, patch };
1155 int i, j;
1156
1157 i = j = 0;
1158 while (*v != '\0' && v < end) {
1159 if (*v != '.' && j < 2)
1160 ver_parse[i][j++] = *v;
1161 else {
1162 i++;
1163 j = 0;
1164 }
1165 v++;
1166 }
1167
1168 *m = strtol(minor, NULL, 0);
1169 *p = strtol(patch, NULL, 0);
1170 }
1171 #endif
1172
1173 static int imsm_level_to_layout(int level)
1174 {
1175 switch (level) {
1176 case 0:
1177 case 1:
1178 return 0;
1179 case 5:
1180 case 6:
1181 return ALGORITHM_LEFT_ASYMMETRIC;
1182 case 10:
1183 return 0x102;
1184 }
1185 return UnSet;
1186 }
1187
1188 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info)
1189 {
1190 struct intel_super *super = st->sb;
1191 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1192 struct imsm_map *map = get_imsm_map(dev, 0);
1193 struct dl *dl;
1194
1195 for (dl = super->disks; dl; dl = dl->next)
1196 if (dl->raiddisk == info->disk.raid_disk)
1197 break;
1198 info->container_member = super->current_vol;
1199 info->array.raid_disks = map->num_members;
1200 info->array.level = get_imsm_raid_level(map);
1201 info->array.layout = imsm_level_to_layout(info->array.level);
1202 info->array.md_minor = -1;
1203 info->array.ctime = 0;
1204 info->array.utime = 0;
1205 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1206 info->array.state = !dev->vol.dirty;
1207 info->custom_array_size = __le32_to_cpu(dev->size_high);
1208 info->custom_array_size <<= 32;
1209 info->custom_array_size |= __le32_to_cpu(dev->size_low);
1210
1211 info->disk.major = 0;
1212 info->disk.minor = 0;
1213 if (dl) {
1214 info->disk.major = dl->major;
1215 info->disk.minor = dl->minor;
1216 }
1217
1218 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1219 info->component_size = __le32_to_cpu(map->blocks_per_member);
1220 memset(info->uuid, 0, sizeof(info->uuid));
1221
1222 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty)
1223 info->resync_start = 0;
1224 else if (dev->vol.migr_state)
1225 /* FIXME add curr_migr_unit to resync_start conversion */
1226 info->resync_start = 0;
1227 else
1228 info->resync_start = ~0ULL;
1229
1230 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1231 info->name[MAX_RAID_SERIAL_LEN] = 0;
1232
1233 info->array.major_version = -1;
1234 info->array.minor_version = -2;
1235 sprintf(info->text_version, "/%s/%d",
1236 devnum2devname(st->container_dev),
1237 info->container_member);
1238 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1239 uuid_from_super_imsm(st, info->uuid);
1240 }
1241
1242 /* check the config file to see if we can return a real uuid for this spare */
1243 static void fixup_container_spare_uuid(struct mdinfo *inf)
1244 {
1245 struct mddev_ident_s *array_list;
1246
1247 if (inf->array.level != LEVEL_CONTAINER ||
1248 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1249 return;
1250
1251 array_list = conf_get_ident(NULL);
1252
1253 for (; array_list; array_list = array_list->next) {
1254 if (array_list->uuid_set) {
1255 struct supertype *_sst; /* spare supertype */
1256 struct supertype *_cst; /* container supertype */
1257
1258 _cst = array_list->st;
1259 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1260 if (_sst) {
1261 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1262 free(_sst);
1263 break;
1264 }
1265 }
1266 }
1267 }
1268
1269 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info)
1270 {
1271 struct intel_super *super = st->sb;
1272 struct imsm_disk *disk;
1273 __u32 s;
1274
1275 if (super->current_vol >= 0) {
1276 getinfo_super_imsm_volume(st, info);
1277 return;
1278 }
1279
1280 /* Set raid_disks to zero so that Assemble will always pull in valid
1281 * spares
1282 */
1283 info->array.raid_disks = 0;
1284 info->array.level = LEVEL_CONTAINER;
1285 info->array.layout = 0;
1286 info->array.md_minor = -1;
1287 info->array.ctime = 0; /* N/A for imsm */
1288 info->array.utime = 0;
1289 info->array.chunk_size = 0;
1290
1291 info->disk.major = 0;
1292 info->disk.minor = 0;
1293 info->disk.raid_disk = -1;
1294 info->reshape_active = 0;
1295 info->array.major_version = -1;
1296 info->array.minor_version = -2;
1297 strcpy(info->text_version, "imsm");
1298 info->safe_mode_delay = 0;
1299 info->disk.number = -1;
1300 info->disk.state = 0;
1301 info->name[0] = 0;
1302
1303 if (super->disks) {
1304 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1305
1306 disk = &super->disks->disk;
1307 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1308 info->component_size = reserved;
1309 s = disk->status;
1310 info->disk.state = s & CONFIGURED_DISK ? (1 << MD_DISK_ACTIVE) : 0;
1311 /* we don't change info->disk.raid_disk here because
1312 * this state will be finalized in mdmon after we have
1313 * found the 'most fresh' version of the metadata
1314 */
1315 info->disk.state |= s & FAILED_DISK ? (1 << MD_DISK_FAULTY) : 0;
1316 info->disk.state |= s & SPARE_DISK ? 0 : (1 << MD_DISK_SYNC);
1317 }
1318
1319 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1320 * ->compare_super may have updated the 'num_raid_devs' field for spares
1321 */
1322 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
1323 uuid_from_super_imsm(st, info->uuid);
1324 else {
1325 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
1326 fixup_container_spare_uuid(info);
1327 }
1328 }
1329
1330 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1331 char *update, char *devname, int verbose,
1332 int uuid_set, char *homehost)
1333 {
1334 /* FIXME */
1335
1336 /* For 'assemble' and 'force' we need to return non-zero if any
1337 * change was made. For others, the return value is ignored.
1338 * Update options are:
1339 * force-one : This device looks a bit old but needs to be included,
1340 * update age info appropriately.
1341 * assemble: clear any 'faulty' flag to allow this device to
1342 * be assembled.
1343 * force-array: Array is degraded but being forced, mark it clean
1344 * if that will be needed to assemble it.
1345 *
1346 * newdev: not used ????
1347 * grow: Array has gained a new device - this is currently for
1348 * linear only
1349 * resync: mark as dirty so a resync will happen.
1350 * name: update the name - preserving the homehost
1351 *
1352 * Following are not relevant for this imsm:
1353 * sparc2.2 : update from old dodgey metadata
1354 * super-minor: change the preferred_minor number
1355 * summaries: update redundant counters.
1356 * uuid: Change the uuid of the array to match watch is given
1357 * homehost: update the recorded homehost
1358 * _reshape_progress: record new reshape_progress position.
1359 */
1360 int rv = 0;
1361 //struct intel_super *super = st->sb;
1362 //struct imsm_super *mpb = super->mpb;
1363
1364 if (strcmp(update, "grow") == 0) {
1365 }
1366 if (strcmp(update, "resync") == 0) {
1367 /* dev->vol.dirty = 1; */
1368 }
1369
1370 /* IMSM has no concept of UUID or homehost */
1371
1372 return rv;
1373 }
1374
1375 static size_t disks_to_mpb_size(int disks)
1376 {
1377 size_t size;
1378
1379 size = sizeof(struct imsm_super);
1380 size += (disks - 1) * sizeof(struct imsm_disk);
1381 size += 2 * sizeof(struct imsm_dev);
1382 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1383 size += (4 - 2) * sizeof(struct imsm_map);
1384 /* 4 possible disk_ord_tbl's */
1385 size += 4 * (disks - 1) * sizeof(__u32);
1386
1387 return size;
1388 }
1389
1390 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1391 {
1392 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1393 return 0;
1394
1395 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1396 }
1397
1398 static void free_devlist(struct intel_super *super)
1399 {
1400 struct intel_dev *dv;
1401
1402 while (super->devlist) {
1403 dv = super->devlist->next;
1404 free(super->devlist->dev);
1405 free(super->devlist);
1406 super->devlist = dv;
1407 }
1408 }
1409
1410 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1411 {
1412 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1413 }
1414
1415 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1416 {
1417 /*
1418 * return:
1419 * 0 same, or first was empty, and second was copied
1420 * 1 second had wrong number
1421 * 2 wrong uuid
1422 * 3 wrong other info
1423 */
1424 struct intel_super *first = st->sb;
1425 struct intel_super *sec = tst->sb;
1426
1427 if (!first) {
1428 st->sb = tst->sb;
1429 tst->sb = NULL;
1430 return 0;
1431 }
1432
1433 if (memcmp(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH) != 0)
1434 return 3;
1435
1436 /* if an anchor does not have num_raid_devs set then it is a free
1437 * floating spare
1438 */
1439 if (first->anchor->num_raid_devs > 0 &&
1440 sec->anchor->num_raid_devs > 0) {
1441 if (first->anchor->family_num != sec->anchor->family_num)
1442 return 3;
1443 }
1444
1445 /* if 'first' is a spare promote it to a populated mpb with sec's
1446 * family number
1447 */
1448 if (first->anchor->num_raid_devs == 0 &&
1449 sec->anchor->num_raid_devs > 0) {
1450 int i;
1451 struct intel_dev *dv;
1452 struct imsm_dev *dev;
1453
1454 /* we need to copy raid device info from sec if an allocation
1455 * fails here we don't associate the spare
1456 */
1457 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1458 dv = malloc(sizeof(*dv));
1459 if (!dv)
1460 break;
1461 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1462 if (!dev) {
1463 free(dv);
1464 break;
1465 }
1466 dv->dev = dev;
1467 dv->index = i;
1468 dv->next = first->devlist;
1469 first->devlist = dv;
1470 }
1471 if (i <= sec->anchor->num_raid_devs) {
1472 /* allocation failure */
1473 free_devlist(first);
1474 fprintf(stderr, "imsm: failed to associate spare\n");
1475 return 3;
1476 }
1477 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1478 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
1479
1480 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1481 first->anchor->family_num = sec->anchor->family_num;
1482 }
1483
1484 return 0;
1485 }
1486
1487 static void fd2devname(int fd, char *name)
1488 {
1489 struct stat st;
1490 char path[256];
1491 char dname[100];
1492 char *nm;
1493 int rv;
1494
1495 name[0] = '\0';
1496 if (fstat(fd, &st) != 0)
1497 return;
1498 sprintf(path, "/sys/dev/block/%d:%d",
1499 major(st.st_rdev), minor(st.st_rdev));
1500
1501 rv = readlink(path, dname, sizeof(dname));
1502 if (rv <= 0)
1503 return;
1504
1505 dname[rv] = '\0';
1506 nm = strrchr(dname, '/');
1507 nm++;
1508 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
1509 }
1510
1511
1512 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
1513
1514 static int imsm_read_serial(int fd, char *devname,
1515 __u8 serial[MAX_RAID_SERIAL_LEN])
1516 {
1517 unsigned char scsi_serial[255];
1518 int rv;
1519 int rsp_len;
1520 int len;
1521 char *dest;
1522 char *src;
1523 char *rsp_buf;
1524 int i;
1525
1526 memset(scsi_serial, 0, sizeof(scsi_serial));
1527
1528 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
1529
1530 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
1531 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1532 fd2devname(fd, (char *) serial);
1533 return 0;
1534 }
1535
1536 if (rv != 0) {
1537 if (devname)
1538 fprintf(stderr,
1539 Name ": Failed to retrieve serial for %s\n",
1540 devname);
1541 return rv;
1542 }
1543
1544 rsp_len = scsi_serial[3];
1545 if (!rsp_len) {
1546 if (devname)
1547 fprintf(stderr,
1548 Name ": Failed to retrieve serial for %s\n",
1549 devname);
1550 return 2;
1551 }
1552 rsp_buf = (char *) &scsi_serial[4];
1553
1554 /* trim all whitespace and non-printable characters and convert
1555 * ':' to ';'
1556 */
1557 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
1558 src = &rsp_buf[i];
1559 if (*src > 0x20) {
1560 /* ':' is reserved for use in placeholder serial
1561 * numbers for missing disks
1562 */
1563 if (*src == ':')
1564 *dest++ = ';';
1565 else
1566 *dest++ = *src;
1567 }
1568 }
1569 len = dest - rsp_buf;
1570 dest = rsp_buf;
1571
1572 /* truncate leading characters */
1573 if (len > MAX_RAID_SERIAL_LEN) {
1574 dest += len - MAX_RAID_SERIAL_LEN;
1575 len = MAX_RAID_SERIAL_LEN;
1576 }
1577
1578 memset(serial, 0, MAX_RAID_SERIAL_LEN);
1579 memcpy(serial, dest, len);
1580
1581 return 0;
1582 }
1583
1584 static int serialcmp(__u8 *s1, __u8 *s2)
1585 {
1586 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
1587 }
1588
1589 static void serialcpy(__u8 *dest, __u8 *src)
1590 {
1591 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
1592 }
1593
1594 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
1595 {
1596 struct dl *dl;
1597
1598 for (dl = super->disks; dl; dl = dl->next)
1599 if (serialcmp(dl->serial, serial) == 0)
1600 break;
1601
1602 return dl;
1603 }
1604
1605 static int
1606 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
1607 {
1608 struct dl *dl;
1609 struct stat stb;
1610 int rv;
1611 int i;
1612 int alloc = 1;
1613 __u8 serial[MAX_RAID_SERIAL_LEN];
1614
1615 rv = imsm_read_serial(fd, devname, serial);
1616
1617 if (rv != 0)
1618 return 2;
1619
1620 /* check if this is a disk we have seen before. it may be a spare in
1621 * super->disks while the current anchor believes it is a raid member,
1622 * check if we need to update dl->index
1623 */
1624 dl = serial_to_dl(serial, super);
1625 if (!dl)
1626 dl = malloc(sizeof(*dl));
1627 else
1628 alloc = 0;
1629
1630 if (!dl) {
1631 if (devname)
1632 fprintf(stderr,
1633 Name ": failed to allocate disk buffer for %s\n",
1634 devname);
1635 return 2;
1636 }
1637
1638 if (alloc) {
1639 fstat(fd, &stb);
1640 dl->major = major(stb.st_rdev);
1641 dl->minor = minor(stb.st_rdev);
1642 dl->next = super->disks;
1643 dl->fd = keep_fd ? fd : -1;
1644 dl->devname = devname ? strdup(devname) : NULL;
1645 serialcpy(dl->serial, serial);
1646 dl->index = -2;
1647 dl->e = NULL;
1648 } else if (keep_fd) {
1649 close(dl->fd);
1650 dl->fd = fd;
1651 }
1652
1653 /* look up this disk's index in the current anchor */
1654 for (i = 0; i < super->anchor->num_disks; i++) {
1655 struct imsm_disk *disk_iter;
1656
1657 disk_iter = __get_imsm_disk(super->anchor, i);
1658
1659 if (serialcmp(disk_iter->serial, dl->serial) == 0) {
1660 dl->disk = *disk_iter;
1661 /* only set index on disks that are a member of a
1662 * populated contianer, i.e. one with raid_devs
1663 */
1664 if (dl->disk.status & FAILED_DISK)
1665 dl->index = -2;
1666 else if (dl->disk.status & SPARE_DISK)
1667 dl->index = -1;
1668 else
1669 dl->index = i;
1670
1671 break;
1672 }
1673 }
1674
1675 /* no match, maybe a stale failed drive */
1676 if (i == super->anchor->num_disks && dl->index >= 0) {
1677 dl->disk = *__get_imsm_disk(super->anchor, dl->index);
1678 if (dl->disk.status & FAILED_DISK)
1679 dl->index = -2;
1680 }
1681
1682 if (alloc)
1683 super->disks = dl;
1684
1685 return 0;
1686 }
1687
1688 #ifndef MDASSEMBLE
1689 /* When migrating map0 contains the 'destination' state while map1
1690 * contains the current state. When not migrating map0 contains the
1691 * current state. This routine assumes that map[0].map_state is set to
1692 * the current array state before being called.
1693 *
1694 * Migration is indicated by one of the following states
1695 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
1696 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
1697 * map1state=unitialized)
1698 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
1699 * map1state=normal)
1700 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
1701 * map1state=degraded)
1702 */
1703 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
1704 {
1705 struct imsm_map *dest;
1706 struct imsm_map *src = get_imsm_map(dev, 0);
1707
1708 dev->vol.migr_state = 1;
1709 set_migr_type(dev, migr_type);
1710 dev->vol.curr_migr_unit = 0;
1711 dest = get_imsm_map(dev, 1);
1712
1713 /* duplicate and then set the target end state in map[0] */
1714 memcpy(dest, src, sizeof_imsm_map(src));
1715 if (migr_type == MIGR_REBUILD) {
1716 __u32 ord;
1717 int i;
1718
1719 for (i = 0; i < src->num_members; i++) {
1720 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
1721 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
1722 }
1723 }
1724
1725 src->map_state = to_state;
1726 }
1727
1728 static void end_migration(struct imsm_dev *dev, __u8 map_state)
1729 {
1730 struct imsm_map *map = get_imsm_map(dev, 0);
1731 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
1732 int i;
1733
1734 /* merge any IMSM_ORD_REBUILD bits that were not successfully
1735 * completed in the last migration.
1736 *
1737 * FIXME add support for online capacity expansion and
1738 * raid-level-migration
1739 */
1740 for (i = 0; i < prev->num_members; i++)
1741 map->disk_ord_tbl[i] |= prev->disk_ord_tbl[i];
1742
1743 dev->vol.migr_state = 0;
1744 dev->vol.curr_migr_unit = 0;
1745 map->map_state = map_state;
1746 }
1747 #endif
1748
1749 static int parse_raid_devices(struct intel_super *super)
1750 {
1751 int i;
1752 struct imsm_dev *dev_new;
1753 size_t len, len_migr;
1754 size_t space_needed = 0;
1755 struct imsm_super *mpb = super->anchor;
1756
1757 for (i = 0; i < super->anchor->num_raid_devs; i++) {
1758 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
1759 struct intel_dev *dv;
1760
1761 len = sizeof_imsm_dev(dev_iter, 0);
1762 len_migr = sizeof_imsm_dev(dev_iter, 1);
1763 if (len_migr > len)
1764 space_needed += len_migr - len;
1765
1766 dv = malloc(sizeof(*dv));
1767 if (!dv)
1768 return 1;
1769 dev_new = malloc(len_migr);
1770 if (!dev_new) {
1771 free(dv);
1772 return 1;
1773 }
1774 imsm_copy_dev(dev_new, dev_iter);
1775 dv->dev = dev_new;
1776 dv->index = i;
1777 dv->next = super->devlist;
1778 super->devlist = dv;
1779 }
1780
1781 /* ensure that super->buf is large enough when all raid devices
1782 * are migrating
1783 */
1784 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
1785 void *buf;
1786
1787 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
1788 if (posix_memalign(&buf, 512, len) != 0)
1789 return 1;
1790
1791 memcpy(buf, super->buf, super->len);
1792 memset(buf + super->len, 0, len - super->len);
1793 free(super->buf);
1794 super->buf = buf;
1795 super->len = len;
1796 }
1797
1798 return 0;
1799 }
1800
1801 /* retrieve a pointer to the bbm log which starts after all raid devices */
1802 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
1803 {
1804 void *ptr = NULL;
1805
1806 if (__le32_to_cpu(mpb->bbm_log_size)) {
1807 ptr = mpb;
1808 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
1809 }
1810
1811 return ptr;
1812 }
1813
1814 static void __free_imsm(struct intel_super *super, int free_disks);
1815
1816 /* load_imsm_mpb - read matrix metadata
1817 * allocates super->mpb to be freed by free_super
1818 */
1819 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
1820 {
1821 unsigned long long dsize;
1822 unsigned long long sectors;
1823 struct stat;
1824 struct imsm_super *anchor;
1825 __u32 check_sum;
1826 int rc;
1827
1828 get_dev_size(fd, NULL, &dsize);
1829
1830 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
1831 if (devname)
1832 fprintf(stderr,
1833 Name ": Cannot seek to anchor block on %s: %s\n",
1834 devname, strerror(errno));
1835 return 1;
1836 }
1837
1838 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
1839 if (devname)
1840 fprintf(stderr,
1841 Name ": Failed to allocate imsm anchor buffer"
1842 " on %s\n", devname);
1843 return 1;
1844 }
1845 if (read(fd, anchor, 512) != 512) {
1846 if (devname)
1847 fprintf(stderr,
1848 Name ": Cannot read anchor block on %s: %s\n",
1849 devname, strerror(errno));
1850 free(anchor);
1851 return 1;
1852 }
1853
1854 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
1855 if (devname)
1856 fprintf(stderr,
1857 Name ": no IMSM anchor on %s\n", devname);
1858 free(anchor);
1859 return 2;
1860 }
1861
1862 __free_imsm(super, 0);
1863 super->len = ROUND_UP(anchor->mpb_size, 512);
1864 if (posix_memalign(&super->buf, 512, super->len) != 0) {
1865 if (devname)
1866 fprintf(stderr,
1867 Name ": unable to allocate %zu byte mpb buffer\n",
1868 super->len);
1869 free(anchor);
1870 return 2;
1871 }
1872 memcpy(super->buf, anchor, 512);
1873
1874 sectors = mpb_sectors(anchor) - 1;
1875 free(anchor);
1876 if (!sectors) {
1877 check_sum = __gen_imsm_checksum(super->anchor);
1878 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1879 if (devname)
1880 fprintf(stderr,
1881 Name ": IMSM checksum %x != %x on %s\n",
1882 check_sum,
1883 __le32_to_cpu(super->anchor->check_sum),
1884 devname);
1885 return 2;
1886 }
1887
1888 rc = load_imsm_disk(fd, super, devname, 0);
1889 if (rc == 0)
1890 rc = parse_raid_devices(super);
1891 return rc;
1892 }
1893
1894 /* read the extended mpb */
1895 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
1896 if (devname)
1897 fprintf(stderr,
1898 Name ": Cannot seek to extended mpb on %s: %s\n",
1899 devname, strerror(errno));
1900 return 1;
1901 }
1902
1903 if (read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
1904 if (devname)
1905 fprintf(stderr,
1906 Name ": Cannot read extended mpb on %s: %s\n",
1907 devname, strerror(errno));
1908 return 2;
1909 }
1910
1911 check_sum = __gen_imsm_checksum(super->anchor);
1912 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
1913 if (devname)
1914 fprintf(stderr,
1915 Name ": IMSM checksum %x != %x on %s\n",
1916 check_sum, __le32_to_cpu(super->anchor->check_sum),
1917 devname);
1918 return 3;
1919 }
1920
1921 /* FIXME the BBM log is disk specific so we cannot use this global
1922 * buffer for all disks. Ok for now since we only look at the global
1923 * bbm_log_size parameter to gate assembly
1924 */
1925 super->bbm_log = __get_imsm_bbm_log(super->anchor);
1926
1927 rc = load_imsm_disk(fd, super, devname, 0);
1928 if (rc == 0)
1929 rc = parse_raid_devices(super);
1930
1931 return rc;
1932 }
1933
1934 static void __free_imsm_disk(struct dl *d)
1935 {
1936 if (d->fd >= 0)
1937 close(d->fd);
1938 if (d->devname)
1939 free(d->devname);
1940 if (d->e)
1941 free(d->e);
1942 free(d);
1943
1944 }
1945 static void free_imsm_disks(struct intel_super *super)
1946 {
1947 struct dl *d;
1948
1949 while (super->disks) {
1950 d = super->disks;
1951 super->disks = d->next;
1952 __free_imsm_disk(d);
1953 }
1954 while (super->missing) {
1955 d = super->missing;
1956 super->missing = d->next;
1957 __free_imsm_disk(d);
1958 }
1959
1960 }
1961
1962 /* free all the pieces hanging off of a super pointer */
1963 static void __free_imsm(struct intel_super *super, int free_disks)
1964 {
1965 if (super->buf) {
1966 free(super->buf);
1967 super->buf = NULL;
1968 }
1969 if (free_disks)
1970 free_imsm_disks(super);
1971 free_devlist(super);
1972 if (super->hba) {
1973 free((void *) super->hba);
1974 super->hba = NULL;
1975 }
1976 }
1977
1978 static void free_imsm(struct intel_super *super)
1979 {
1980 __free_imsm(super, 1);
1981 free(super);
1982 }
1983
1984 static void free_super_imsm(struct supertype *st)
1985 {
1986 struct intel_super *super = st->sb;
1987
1988 if (!super)
1989 return;
1990
1991 free_imsm(super);
1992 st->sb = NULL;
1993 }
1994
1995 static struct intel_super *alloc_super(int creating_imsm)
1996 {
1997 struct intel_super *super = malloc(sizeof(*super));
1998
1999 if (super) {
2000 memset(super, 0, sizeof(*super));
2001 super->creating_imsm = creating_imsm;
2002 super->current_vol = -1;
2003 super->create_offset = ~((__u32 ) 0);
2004 if (!check_env("IMSM_NO_PLATFORM"))
2005 super->orom = find_imsm_orom();
2006 if (super->orom && !check_env("IMSM_TEST_OROM")) {
2007 struct sys_dev *list, *ent;
2008
2009 /* find the first intel ahci controller */
2010 list = find_driver_devices("pci", "ahci");
2011 for (ent = list; ent; ent = ent->next)
2012 if (devpath_to_vendor(ent->path) == 0x8086)
2013 break;
2014 if (ent) {
2015 super->hba = ent->path;
2016 ent->path = NULL;
2017 }
2018 free_sys_dev(&list);
2019 }
2020 }
2021
2022 return super;
2023 }
2024
2025 #ifndef MDASSEMBLE
2026 /* find_missing - helper routine for load_super_imsm_all that identifies
2027 * disks that have disappeared from the system. This routine relies on
2028 * the mpb being uptodate, which it is at load time.
2029 */
2030 static int find_missing(struct intel_super *super)
2031 {
2032 int i;
2033 struct imsm_super *mpb = super->anchor;
2034 struct dl *dl;
2035 struct imsm_disk *disk;
2036
2037 for (i = 0; i < mpb->num_disks; i++) {
2038 disk = __get_imsm_disk(mpb, i);
2039 dl = serial_to_dl(disk->serial, super);
2040 if (dl)
2041 continue;
2042
2043 dl = malloc(sizeof(*dl));
2044 if (!dl)
2045 return 1;
2046 dl->major = 0;
2047 dl->minor = 0;
2048 dl->fd = -1;
2049 dl->devname = strdup("missing");
2050 dl->index = i;
2051 serialcpy(dl->serial, disk->serial);
2052 dl->disk = *disk;
2053 dl->e = NULL;
2054 dl->next = super->missing;
2055 super->missing = dl;
2056 }
2057
2058 return 0;
2059 }
2060
2061 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2062 char *devname, int keep_fd)
2063 {
2064 struct mdinfo *sra;
2065 struct intel_super *super;
2066 struct mdinfo *sd, *best = NULL;
2067 __u32 bestgen = 0;
2068 __u32 gen;
2069 char nm[20];
2070 int dfd;
2071 int rv;
2072 int devnum = fd2devnum(fd);
2073 int retry;
2074 enum sysfs_read_flags flags;
2075
2076 flags = GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE;
2077 if (mdmon_running(devnum))
2078 flags |= SKIP_GONE_DEVS;
2079
2080 /* check if 'fd' an opened container */
2081 sra = sysfs_read(fd, 0, flags);
2082 if (!sra)
2083 return 1;
2084
2085 if (sra->array.major_version != -1 ||
2086 sra->array.minor_version != -2 ||
2087 strcmp(sra->text_version, "imsm") != 0)
2088 return 1;
2089
2090 super = alloc_super(0);
2091 if (!super)
2092 return 1;
2093
2094 /* find the most up to date disk in this array, skipping spares */
2095 for (sd = sra->devs; sd; sd = sd->next) {
2096 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2097 dfd = dev_open(nm, keep_fd ? O_RDWR : O_RDONLY);
2098 if (dfd < 0) {
2099 free_imsm(super);
2100 return 2;
2101 }
2102 rv = load_imsm_mpb(dfd, super, NULL);
2103
2104 /* retry the load if we might have raced against mdmon */
2105 if (rv == 3 && mdmon_running(devnum))
2106 for (retry = 0; retry < 3; retry++) {
2107 usleep(3000);
2108 rv = load_imsm_mpb(dfd, super, NULL);
2109 if (rv != 3)
2110 break;
2111 }
2112 if (!keep_fd)
2113 close(dfd);
2114 if (rv == 0) {
2115 if (super->anchor->num_raid_devs == 0)
2116 gen = 0;
2117 else
2118 gen = __le32_to_cpu(super->anchor->generation_num);
2119 if (!best || gen > bestgen) {
2120 bestgen = gen;
2121 best = sd;
2122 }
2123 } else {
2124 free_imsm(super);
2125 return rv;
2126 }
2127 }
2128
2129 if (!best) {
2130 free_imsm(super);
2131 return 1;
2132 }
2133
2134 /* load the most up to date anchor */
2135 sprintf(nm, "%d:%d", best->disk.major, best->disk.minor);
2136 dfd = dev_open(nm, O_RDONLY);
2137 if (dfd < 0) {
2138 free_imsm(super);
2139 return 1;
2140 }
2141 rv = load_imsm_mpb(dfd, super, NULL);
2142 close(dfd);
2143 if (rv != 0) {
2144 free_imsm(super);
2145 return 2;
2146 }
2147
2148 /* re-parse the disk list with the current anchor */
2149 for (sd = sra->devs ; sd ; sd = sd->next) {
2150 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2151 dfd = dev_open(nm, keep_fd? O_RDWR : O_RDONLY);
2152 if (dfd < 0) {
2153 free_imsm(super);
2154 return 2;
2155 }
2156 load_imsm_disk(dfd, super, NULL, keep_fd);
2157 if (!keep_fd)
2158 close(dfd);
2159 }
2160
2161
2162 if (find_missing(super) != 0) {
2163 free_imsm(super);
2164 return 2;
2165 }
2166
2167 if (st->subarray[0]) {
2168 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2169 super->current_vol = atoi(st->subarray);
2170 else {
2171 free_imsm(super);
2172 return 1;
2173 }
2174 }
2175
2176 *sbp = super;
2177 st->container_dev = devnum;
2178 if (st->ss == NULL) {
2179 st->ss = &super_imsm;
2180 st->minor_version = 0;
2181 st->max_devs = IMSM_MAX_DEVICES;
2182 }
2183 st->loaded_container = 1;
2184
2185 return 0;
2186 }
2187 #endif
2188
2189 static int load_super_imsm(struct supertype *st, int fd, char *devname)
2190 {
2191 struct intel_super *super;
2192 int rv;
2193
2194 #ifndef MDASSEMBLE
2195 if (load_super_imsm_all(st, fd, &st->sb, devname, 1) == 0)
2196 return 0;
2197 #endif
2198
2199 super = alloc_super(0);
2200 if (!super) {
2201 fprintf(stderr,
2202 Name ": malloc of %zu failed.\n",
2203 sizeof(*super));
2204 return 1;
2205 }
2206
2207 rv = load_imsm_mpb(fd, super, devname);
2208
2209 if (rv) {
2210 if (devname)
2211 fprintf(stderr,
2212 Name ": Failed to load all information "
2213 "sections on %s\n", devname);
2214 free_imsm(super);
2215 return rv;
2216 }
2217
2218 if (st->subarray[0]) {
2219 if (atoi(st->subarray) <= super->anchor->num_raid_devs)
2220 super->current_vol = atoi(st->subarray);
2221 else {
2222 free_imsm(super);
2223 return 1;
2224 }
2225 }
2226
2227 st->sb = super;
2228 if (st->ss == NULL) {
2229 st->ss = &super_imsm;
2230 st->minor_version = 0;
2231 st->max_devs = IMSM_MAX_DEVICES;
2232 }
2233 st->loaded_container = 0;
2234
2235 return 0;
2236 }
2237
2238 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
2239 {
2240 if (info->level == 1)
2241 return 128;
2242 return info->chunk_size >> 9;
2243 }
2244
2245 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
2246 {
2247 __u32 num_stripes;
2248
2249 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
2250 num_stripes /= num_domains;
2251
2252 return num_stripes;
2253 }
2254
2255 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
2256 {
2257 if (info->level == 1)
2258 return info->size * 2;
2259 else
2260 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
2261 }
2262
2263 static void imsm_update_version_info(struct intel_super *super)
2264 {
2265 /* update the version and attributes */
2266 struct imsm_super *mpb = super->anchor;
2267 char *version;
2268 struct imsm_dev *dev;
2269 struct imsm_map *map;
2270 int i;
2271
2272 for (i = 0; i < mpb->num_raid_devs; i++) {
2273 dev = get_imsm_dev(super, i);
2274 map = get_imsm_map(dev, 0);
2275 if (__le32_to_cpu(dev->size_high) > 0)
2276 mpb->attributes |= MPB_ATTRIB_2TB;
2277
2278 /* FIXME detect when an array spans a port multiplier */
2279 #if 0
2280 mpb->attributes |= MPB_ATTRIB_PM;
2281 #endif
2282
2283 if (mpb->num_raid_devs > 1 ||
2284 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
2285 version = MPB_VERSION_ATTRIBS;
2286 switch (get_imsm_raid_level(map)) {
2287 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
2288 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
2289 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
2290 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
2291 }
2292 } else {
2293 if (map->num_members >= 5)
2294 version = MPB_VERSION_5OR6_DISK_ARRAY;
2295 else if (dev->status == DEV_CLONE_N_GO)
2296 version = MPB_VERSION_CNG;
2297 else if (get_imsm_raid_level(map) == 5)
2298 version = MPB_VERSION_RAID5;
2299 else if (map->num_members >= 3)
2300 version = MPB_VERSION_3OR4_DISK_ARRAY;
2301 else if (get_imsm_raid_level(map) == 1)
2302 version = MPB_VERSION_RAID1;
2303 else
2304 version = MPB_VERSION_RAID0;
2305 }
2306 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
2307 }
2308 }
2309
2310 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
2311 unsigned long long size, char *name,
2312 char *homehost, int *uuid)
2313 {
2314 /* We are creating a volume inside a pre-existing container.
2315 * so st->sb is already set.
2316 */
2317 struct intel_super *super = st->sb;
2318 struct imsm_super *mpb = super->anchor;
2319 struct intel_dev *dv;
2320 struct imsm_dev *dev;
2321 struct imsm_vol *vol;
2322 struct imsm_map *map;
2323 int idx = mpb->num_raid_devs;
2324 int i;
2325 unsigned long long array_blocks;
2326 size_t size_old, size_new;
2327 __u32 num_data_stripes;
2328
2329 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
2330 fprintf(stderr, Name": This imsm-container already has the "
2331 "maximum of %d volumes\n", super->orom->vpa);
2332 return 0;
2333 }
2334
2335 /* ensure the mpb is large enough for the new data */
2336 size_old = __le32_to_cpu(mpb->mpb_size);
2337 size_new = disks_to_mpb_size(info->nr_disks);
2338 if (size_new > size_old) {
2339 void *mpb_new;
2340 size_t size_round = ROUND_UP(size_new, 512);
2341
2342 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
2343 fprintf(stderr, Name": could not allocate new mpb\n");
2344 return 0;
2345 }
2346 memcpy(mpb_new, mpb, size_old);
2347 free(mpb);
2348 mpb = mpb_new;
2349 super->anchor = mpb_new;
2350 mpb->mpb_size = __cpu_to_le32(size_new);
2351 memset(mpb_new + size_old, 0, size_round - size_old);
2352 }
2353 super->current_vol = idx;
2354 /* when creating the first raid device in this container set num_disks
2355 * to zero, i.e. delete this spare and add raid member devices in
2356 * add_to_super_imsm_volume()
2357 */
2358 if (super->current_vol == 0)
2359 mpb->num_disks = 0;
2360
2361 for (i = 0; i < super->current_vol; i++) {
2362 dev = get_imsm_dev(super, i);
2363 if (strncmp((char *) dev->volume, name,
2364 MAX_RAID_SERIAL_LEN) == 0) {
2365 fprintf(stderr, Name": '%s' is already defined for this container\n",
2366 name);
2367 return 0;
2368 }
2369 }
2370
2371 sprintf(st->subarray, "%d", idx);
2372 dv = malloc(sizeof(*dv));
2373 if (!dv) {
2374 fprintf(stderr, Name ": failed to allocate device list entry\n");
2375 return 0;
2376 }
2377 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
2378 if (!dev) {
2379 free(dv);
2380 fprintf(stderr, Name": could not allocate raid device\n");
2381 return 0;
2382 }
2383 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
2384 if (info->level == 1)
2385 array_blocks = info_to_blocks_per_member(info);
2386 else
2387 array_blocks = calc_array_size(info->level, info->raid_disks,
2388 info->layout, info->chunk_size,
2389 info->size*2);
2390 /* round array size down to closest MB */
2391 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
2392
2393 dev->size_low = __cpu_to_le32((__u32) array_blocks);
2394 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
2395 dev->status = __cpu_to_le32(0);
2396 dev->reserved_blocks = __cpu_to_le32(0);
2397 vol = &dev->vol;
2398 vol->migr_state = 0;
2399 set_migr_type(dev, MIGR_INIT);
2400 vol->dirty = 0;
2401 vol->curr_migr_unit = 0;
2402 map = get_imsm_map(dev, 0);
2403 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
2404 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
2405 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
2406 map->failed_disk_num = ~0;
2407 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
2408 IMSM_T_STATE_NORMAL;
2409 map->ddf = 1;
2410
2411 if (info->level == 1 && info->raid_disks > 2) {
2412 fprintf(stderr, Name": imsm does not support more than 2 disks"
2413 "in a raid1 volume\n");
2414 return 0;
2415 }
2416
2417 map->raid_level = info->level;
2418 if (info->level == 10) {
2419 map->raid_level = 1;
2420 map->num_domains = info->raid_disks / 2;
2421 } else if (info->level == 1)
2422 map->num_domains = info->raid_disks;
2423 else
2424 map->num_domains = 1;
2425
2426 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
2427 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
2428
2429 map->num_members = info->raid_disks;
2430 for (i = 0; i < map->num_members; i++) {
2431 /* initialized in add_to_super */
2432 set_imsm_ord_tbl_ent(map, i, 0);
2433 }
2434 mpb->num_raid_devs++;
2435
2436 dv->dev = dev;
2437 dv->index = super->current_vol;
2438 dv->next = super->devlist;
2439 super->devlist = dv;
2440
2441 imsm_update_version_info(super);
2442
2443 return 1;
2444 }
2445
2446 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
2447 unsigned long long size, char *name,
2448 char *homehost, int *uuid)
2449 {
2450 /* This is primarily called by Create when creating a new array.
2451 * We will then get add_to_super called for each component, and then
2452 * write_init_super called to write it out to each device.
2453 * For IMSM, Create can create on fresh devices or on a pre-existing
2454 * array.
2455 * To create on a pre-existing array a different method will be called.
2456 * This one is just for fresh drives.
2457 */
2458 struct intel_super *super;
2459 struct imsm_super *mpb;
2460 size_t mpb_size;
2461 char *version;
2462
2463 if (!info) {
2464 st->sb = NULL;
2465 return 0;
2466 }
2467 if (st->sb)
2468 return init_super_imsm_volume(st, info, size, name, homehost,
2469 uuid);
2470
2471 super = alloc_super(1);
2472 if (!super)
2473 return 0;
2474 mpb_size = disks_to_mpb_size(info->nr_disks);
2475 if (posix_memalign(&super->buf, 512, mpb_size) != 0) {
2476 free(super);
2477 return 0;
2478 }
2479 mpb = super->buf;
2480 memset(mpb, 0, mpb_size);
2481
2482 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
2483
2484 version = (char *) mpb->sig;
2485 strcpy(version, MPB_SIGNATURE);
2486 version += strlen(MPB_SIGNATURE);
2487 strcpy(version, MPB_VERSION_RAID0);
2488 mpb->mpb_size = mpb_size;
2489
2490 st->sb = super;
2491 return 1;
2492 }
2493
2494 #ifndef MDASSEMBLE
2495 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
2496 int fd, char *devname)
2497 {
2498 struct intel_super *super = st->sb;
2499 struct imsm_super *mpb = super->anchor;
2500 struct dl *dl;
2501 struct imsm_dev *dev;
2502 struct imsm_map *map;
2503
2504 dev = get_imsm_dev(super, super->current_vol);
2505 map = get_imsm_map(dev, 0);
2506
2507 if (! (dk->state & (1<<MD_DISK_SYNC))) {
2508 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
2509 devname);
2510 return 1;
2511 }
2512
2513 if (fd == -1) {
2514 /* we're doing autolayout so grab the pre-marked (in
2515 * validate_geometry) raid_disk
2516 */
2517 for (dl = super->disks; dl; dl = dl->next)
2518 if (dl->raiddisk == dk->raid_disk)
2519 break;
2520 } else {
2521 for (dl = super->disks; dl ; dl = dl->next)
2522 if (dl->major == dk->major &&
2523 dl->minor == dk->minor)
2524 break;
2525 }
2526
2527 if (!dl) {
2528 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
2529 return 1;
2530 }
2531
2532 /* add a pristine spare to the metadata */
2533 if (dl->index < 0) {
2534 dl->index = super->anchor->num_disks;
2535 super->anchor->num_disks++;
2536 }
2537 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
2538 dl->disk.status = CONFIGURED_DISK | USABLE_DISK;
2539
2540 /* if we are creating the first raid device update the family number */
2541 if (super->current_vol == 0) {
2542 __u32 sum;
2543 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
2544 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
2545
2546 *_dev = *dev;
2547 *_disk = dl->disk;
2548 sum = __gen_imsm_checksum(mpb);
2549 mpb->family_num = __cpu_to_le32(sum);
2550 }
2551
2552 return 0;
2553 }
2554
2555 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
2556 int fd, char *devname)
2557 {
2558 struct intel_super *super = st->sb;
2559 struct dl *dd;
2560 unsigned long long size;
2561 __u32 id;
2562 int rv;
2563 struct stat stb;
2564
2565 /* if we are on an RAID enabled platform check that the disk is
2566 * attached to the raid controller
2567 */
2568 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
2569 fprintf(stderr,
2570 Name ": %s is not attached to the raid controller: %s\n",
2571 devname ? : "disk", super->hba);
2572 return 1;
2573 }
2574
2575 if (super->current_vol >= 0)
2576 return add_to_super_imsm_volume(st, dk, fd, devname);
2577
2578 fstat(fd, &stb);
2579 dd = malloc(sizeof(*dd));
2580 if (!dd) {
2581 fprintf(stderr,
2582 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
2583 return 1;
2584 }
2585 memset(dd, 0, sizeof(*dd));
2586 dd->major = major(stb.st_rdev);
2587 dd->minor = minor(stb.st_rdev);
2588 dd->index = -1;
2589 dd->devname = devname ? strdup(devname) : NULL;
2590 dd->fd = fd;
2591 dd->e = NULL;
2592 rv = imsm_read_serial(fd, devname, dd->serial);
2593 if (rv) {
2594 fprintf(stderr,
2595 Name ": failed to retrieve scsi serial, aborting\n");
2596 free(dd);
2597 abort();
2598 }
2599
2600 get_dev_size(fd, NULL, &size);
2601 size /= 512;
2602 serialcpy(dd->disk.serial, dd->serial);
2603 dd->disk.total_blocks = __cpu_to_le32(size);
2604 dd->disk.status = USABLE_DISK | SPARE_DISK;
2605 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
2606 dd->disk.scsi_id = __cpu_to_le32(id);
2607 else
2608 dd->disk.scsi_id = __cpu_to_le32(0);
2609
2610 if (st->update_tail) {
2611 dd->next = super->add;
2612 super->add = dd;
2613 } else {
2614 dd->next = super->disks;
2615 super->disks = dd;
2616 }
2617
2618 return 0;
2619 }
2620
2621 static int store_imsm_mpb(int fd, struct intel_super *super);
2622
2623 /* spare records have their own family number and do not have any defined raid
2624 * devices
2625 */
2626 static int write_super_imsm_spares(struct intel_super *super, int doclose)
2627 {
2628 struct imsm_super mpb_save;
2629 struct imsm_super *mpb = super->anchor;
2630 __u32 sum;
2631 struct dl *d;
2632
2633 mpb_save = *mpb;
2634 mpb->num_raid_devs = 0;
2635 mpb->num_disks = 1;
2636 mpb->mpb_size = sizeof(struct imsm_super);
2637 mpb->generation_num = __cpu_to_le32(1UL);
2638
2639 for (d = super->disks; d; d = d->next) {
2640 if (d->index != -1)
2641 continue;
2642
2643 mpb->disk[0] = d->disk;
2644 sum = __gen_imsm_checksum(mpb);
2645 mpb->family_num = __cpu_to_le32(sum);
2646 sum = __gen_imsm_checksum(mpb);
2647 mpb->check_sum = __cpu_to_le32(sum);
2648
2649 if (store_imsm_mpb(d->fd, super)) {
2650 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2651 __func__, d->major, d->minor, strerror(errno));
2652 *mpb = mpb_save;
2653 return 1;
2654 }
2655 if (doclose) {
2656 close(d->fd);
2657 d->fd = -1;
2658 }
2659 }
2660
2661 *mpb = mpb_save;
2662 return 0;
2663 }
2664
2665 static int write_super_imsm(struct intel_super *super, int doclose)
2666 {
2667 struct imsm_super *mpb = super->anchor;
2668 struct dl *d;
2669 __u32 generation;
2670 __u32 sum;
2671 int spares = 0;
2672 int i;
2673 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
2674
2675 /* 'generation' is incremented everytime the metadata is written */
2676 generation = __le32_to_cpu(mpb->generation_num);
2677 generation++;
2678 mpb->generation_num = __cpu_to_le32(generation);
2679
2680 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
2681 for (d = super->disks; d; d = d->next) {
2682 if (d->index == -1)
2683 spares++;
2684 else
2685 mpb->disk[d->index] = d->disk;
2686 }
2687 for (d = super->missing; d; d = d->next)
2688 mpb->disk[d->index] = d->disk;
2689
2690 for (i = 0; i < mpb->num_raid_devs; i++) {
2691 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
2692
2693 imsm_copy_dev(dev, get_imsm_dev(super, i));
2694 mpb_size += sizeof_imsm_dev(dev, 0);
2695 }
2696 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
2697 mpb->mpb_size = __cpu_to_le32(mpb_size);
2698
2699 /* recalculate checksum */
2700 sum = __gen_imsm_checksum(mpb);
2701 mpb->check_sum = __cpu_to_le32(sum);
2702
2703 /* write the mpb for disks that compose raid devices */
2704 for (d = super->disks; d ; d = d->next) {
2705 if (d->index < 0)
2706 continue;
2707 if (store_imsm_mpb(d->fd, super))
2708 fprintf(stderr, "%s: failed for device %d:%d %s\n",
2709 __func__, d->major, d->minor, strerror(errno));
2710 if (doclose) {
2711 close(d->fd);
2712 d->fd = -1;
2713 }
2714 }
2715
2716 if (spares)
2717 return write_super_imsm_spares(super, doclose);
2718
2719 return 0;
2720 }
2721
2722
2723 static int create_array(struct supertype *st)
2724 {
2725 size_t len;
2726 struct imsm_update_create_array *u;
2727 struct intel_super *super = st->sb;
2728 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
2729 struct imsm_map *map = get_imsm_map(dev, 0);
2730 struct disk_info *inf;
2731 struct imsm_disk *disk;
2732 int i;
2733 int idx;
2734
2735 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
2736 sizeof(*inf) * map->num_members;
2737 u = malloc(len);
2738 if (!u) {
2739 fprintf(stderr, "%s: failed to allocate update buffer\n",
2740 __func__);
2741 return 1;
2742 }
2743
2744 u->type = update_create_array;
2745 u->dev_idx = super->current_vol;
2746 imsm_copy_dev(&u->dev, dev);
2747 inf = get_disk_info(u);
2748 for (i = 0; i < map->num_members; i++) {
2749 idx = get_imsm_disk_idx(dev, i);
2750 disk = get_imsm_disk(super, idx);
2751 serialcpy(inf[i].serial, disk->serial);
2752 }
2753 append_metadata_update(st, u, len);
2754
2755 return 0;
2756 }
2757
2758 static int _add_disk(struct supertype *st)
2759 {
2760 struct intel_super *super = st->sb;
2761 size_t len;
2762 struct imsm_update_add_disk *u;
2763
2764 if (!super->add)
2765 return 0;
2766
2767 len = sizeof(*u);
2768 u = malloc(len);
2769 if (!u) {
2770 fprintf(stderr, "%s: failed to allocate update buffer\n",
2771 __func__);
2772 return 1;
2773 }
2774
2775 u->type = update_add_disk;
2776 append_metadata_update(st, u, len);
2777
2778 return 0;
2779 }
2780
2781 static int write_init_super_imsm(struct supertype *st)
2782 {
2783 if (st->update_tail) {
2784 /* queue the recently created array / added disk
2785 * as a metadata update */
2786 struct intel_super *super = st->sb;
2787 struct dl *d;
2788 int rv;
2789
2790 /* determine if we are creating a volume or adding a disk */
2791 if (super->current_vol < 0) {
2792 /* in the add disk case we are running in mdmon
2793 * context, so don't close fd's
2794 */
2795 return _add_disk(st);
2796 } else
2797 rv = create_array(st);
2798
2799 for (d = super->disks; d ; d = d->next) {
2800 close(d->fd);
2801 d->fd = -1;
2802 }
2803
2804 return rv;
2805 } else
2806 return write_super_imsm(st->sb, 1);
2807 }
2808 #endif
2809
2810 static int store_zero_imsm(struct supertype *st, int fd)
2811 {
2812 unsigned long long dsize;
2813 void *buf;
2814
2815 get_dev_size(fd, NULL, &dsize);
2816
2817 /* first block is stored on second to last sector of the disk */
2818 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
2819 return 1;
2820
2821 if (posix_memalign(&buf, 512, 512) != 0)
2822 return 1;
2823
2824 memset(buf, 0, 512);
2825 if (write(fd, buf, 512) != 512)
2826 return 1;
2827 return 0;
2828 }
2829
2830 static int imsm_bbm_log_size(struct imsm_super *mpb)
2831 {
2832 return __le32_to_cpu(mpb->bbm_log_size);
2833 }
2834
2835 #ifndef MDASSEMBLE
2836 static int validate_geometry_imsm_container(struct supertype *st, int level,
2837 int layout, int raiddisks, int chunk,
2838 unsigned long long size, char *dev,
2839 unsigned long long *freesize,
2840 int verbose)
2841 {
2842 int fd;
2843 unsigned long long ldsize;
2844 const struct imsm_orom *orom;
2845
2846 if (level != LEVEL_CONTAINER)
2847 return 0;
2848 if (!dev)
2849 return 1;
2850
2851 if (check_env("IMSM_NO_PLATFORM"))
2852 orom = NULL;
2853 else
2854 orom = find_imsm_orom();
2855 if (orom && raiddisks > orom->tds) {
2856 if (verbose)
2857 fprintf(stderr, Name ": %d exceeds maximum number of"
2858 " platform supported disks: %d\n",
2859 raiddisks, orom->tds);
2860 return 0;
2861 }
2862
2863 fd = open(dev, O_RDONLY|O_EXCL, 0);
2864 if (fd < 0) {
2865 if (verbose)
2866 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
2867 dev, strerror(errno));
2868 return 0;
2869 }
2870 if (!get_dev_size(fd, dev, &ldsize)) {
2871 close(fd);
2872 return 0;
2873 }
2874 close(fd);
2875
2876 *freesize = avail_size_imsm(st, ldsize >> 9);
2877
2878 return 1;
2879 }
2880
2881 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
2882 {
2883 const unsigned long long base_start = e[*idx].start;
2884 unsigned long long end = base_start + e[*idx].size;
2885 int i;
2886
2887 if (base_start == end)
2888 return 0;
2889
2890 *idx = *idx + 1;
2891 for (i = *idx; i < num_extents; i++) {
2892 /* extend overlapping extents */
2893 if (e[i].start >= base_start &&
2894 e[i].start <= end) {
2895 if (e[i].size == 0)
2896 return 0;
2897 if (e[i].start + e[i].size > end)
2898 end = e[i].start + e[i].size;
2899 } else if (e[i].start > end) {
2900 *idx = i;
2901 break;
2902 }
2903 }
2904
2905 return end - base_start;
2906 }
2907
2908 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
2909 {
2910 /* build a composite disk with all known extents and generate a new
2911 * 'maxsize' given the "all disks in an array must share a common start
2912 * offset" constraint
2913 */
2914 struct extent *e = calloc(sum_extents, sizeof(*e));
2915 struct dl *dl;
2916 int i, j;
2917 int start_extent;
2918 unsigned long long pos;
2919 unsigned long long start = 0;
2920 unsigned long long maxsize;
2921 unsigned long reserve;
2922
2923 if (!e)
2924 return ~0ULL; /* error */
2925
2926 /* coalesce and sort all extents. also, check to see if we need to
2927 * reserve space between member arrays
2928 */
2929 j = 0;
2930 for (dl = super->disks; dl; dl = dl->next) {
2931 if (!dl->e)
2932 continue;
2933 for (i = 0; i < dl->extent_cnt; i++)
2934 e[j++] = dl->e[i];
2935 }
2936 qsort(e, sum_extents, sizeof(*e), cmp_extent);
2937
2938 /* merge extents */
2939 i = 0;
2940 j = 0;
2941 while (i < sum_extents) {
2942 e[j].start = e[i].start;
2943 e[j].size = find_size(e, &i, sum_extents);
2944 j++;
2945 if (e[j-1].size == 0)
2946 break;
2947 }
2948
2949 pos = 0;
2950 maxsize = 0;
2951 start_extent = 0;
2952 i = 0;
2953 do {
2954 unsigned long long esize;
2955
2956 esize = e[i].start - pos;
2957 if (esize >= maxsize) {
2958 maxsize = esize;
2959 start = pos;
2960 start_extent = i;
2961 }
2962 pos = e[i].start + e[i].size;
2963 i++;
2964 } while (e[i-1].size);
2965 free(e);
2966
2967 if (start_extent > 0)
2968 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
2969 else
2970 reserve = 0;
2971
2972 if (maxsize < reserve)
2973 return ~0ULL;
2974
2975 super->create_offset = ~((__u32) 0);
2976 if (start + reserve > super->create_offset)
2977 return ~0ULL; /* start overflows create_offset */
2978 super->create_offset = start + reserve;
2979
2980 return maxsize - reserve;
2981 }
2982
2983 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
2984 {
2985 if (level < 0 || level == 6 || level == 4)
2986 return 0;
2987
2988 /* if we have an orom prevent invalid raid levels */
2989 if (orom)
2990 switch (level) {
2991 case 0: return imsm_orom_has_raid0(orom);
2992 case 1:
2993 if (raiddisks > 2)
2994 return imsm_orom_has_raid1e(orom);
2995 return imsm_orom_has_raid1(orom) && raiddisks == 2;
2996 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
2997 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
2998 }
2999 else
3000 return 1; /* not on an Intel RAID platform so anything goes */
3001
3002 return 0;
3003 }
3004
3005 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3006 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3007 * FIX ME add ahci details
3008 */
3009 static int validate_geometry_imsm_volume(struct supertype *st, int level,
3010 int layout, int raiddisks, int chunk,
3011 unsigned long long size, char *dev,
3012 unsigned long long *freesize,
3013 int verbose)
3014 {
3015 struct stat stb;
3016 struct intel_super *super = st->sb;
3017 struct imsm_super *mpb = super->anchor;
3018 struct dl *dl;
3019 unsigned long long pos = 0;
3020 unsigned long long maxsize;
3021 struct extent *e;
3022 int i;
3023
3024 /* We must have the container info already read in. */
3025 if (!super)
3026 return 0;
3027
3028 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3029 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3030 level, raiddisks, raiddisks > 1 ? "s" : "");
3031 return 0;
3032 }
3033 if (super->orom && level != 1 &&
3034 !imsm_orom_has_chunk(super->orom, chunk)) {
3035 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3036 return 0;
3037 }
3038 if (layout != imsm_level_to_layout(level)) {
3039 if (level == 5)
3040 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3041 else if (level == 10)
3042 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3043 else
3044 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3045 layout, level);
3046 return 0;
3047 }
3048
3049 if (!dev) {
3050 /* General test: make sure there is space for
3051 * 'raiddisks' device extents of size 'size' at a given
3052 * offset
3053 */
3054 unsigned long long minsize = size;
3055 unsigned long long start_offset = ~0ULL;
3056 int dcnt = 0;
3057 if (minsize == 0)
3058 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3059 for (dl = super->disks; dl ; dl = dl->next) {
3060 int found = 0;
3061
3062 pos = 0;
3063 i = 0;
3064 e = get_extents(super, dl);
3065 if (!e) continue;
3066 do {
3067 unsigned long long esize;
3068 esize = e[i].start - pos;
3069 if (esize >= minsize)
3070 found = 1;
3071 if (found && start_offset == ~0ULL) {
3072 start_offset = pos;
3073 break;
3074 } else if (found && pos != start_offset) {
3075 found = 0;
3076 break;
3077 }
3078 pos = e[i].start + e[i].size;
3079 i++;
3080 } while (e[i-1].size);
3081 if (found)
3082 dcnt++;
3083 free(e);
3084 }
3085 if (dcnt < raiddisks) {
3086 if (verbose)
3087 fprintf(stderr, Name ": imsm: Not enough "
3088 "devices with space for this array "
3089 "(%d < %d)\n",
3090 dcnt, raiddisks);
3091 return 0;
3092 }
3093 return 1;
3094 }
3095
3096 /* This device must be a member of the set */
3097 if (stat(dev, &stb) < 0)
3098 return 0;
3099 if ((S_IFMT & stb.st_mode) != S_IFBLK)
3100 return 0;
3101 for (dl = super->disks ; dl ; dl = dl->next) {
3102 if (dl->major == major(stb