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