]> git.ipfire.org Git - thirdparty/mdadm.git/blob - super-intel.c
Use one function chosing spares from container
[thirdparty/mdadm.git] / super-intel.c
1 /*
2 * mdadm - Intel(R) Matrix Storage Manager Support
3 *
4 * Copyright (C) 2002-2008 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define HAVE_STDINT_H 1
21 #include "mdadm.h"
22 #include "mdmon.h"
23 #include "sha1.h"
24 #include "platform-intel.h"
25 #include <values.h>
26 #include <scsi/sg.h>
27 #include <ctype.h>
28 #include <dirent.h>
29
30 /* MPB == Metadata Parameter Block */
31 #define MPB_SIGNATURE "Intel Raid ISM Cfg Sig. "
32 #define MPB_SIG_LEN (strlen(MPB_SIGNATURE))
33 #define MPB_VERSION_RAID0 "1.0.00"
34 #define MPB_VERSION_RAID1 "1.1.00"
35 #define MPB_VERSION_MANY_VOLUMES_PER_ARRAY "1.2.00"
36 #define MPB_VERSION_3OR4_DISK_ARRAY "1.2.01"
37 #define MPB_VERSION_RAID5 "1.2.02"
38 #define MPB_VERSION_5OR6_DISK_ARRAY "1.2.04"
39 #define MPB_VERSION_CNG "1.2.06"
40 #define MPB_VERSION_ATTRIBS "1.3.00"
41 #define MAX_SIGNATURE_LENGTH 32
42 #define MAX_RAID_SERIAL_LEN 16
43
44 #define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
45 #define MPB_ATTRIB_PM __cpu_to_le32(0x40000000)
46 #define MPB_ATTRIB_2TB __cpu_to_le32(0x20000000)
47 #define MPB_ATTRIB_RAID0 __cpu_to_le32(0x00000001)
48 #define MPB_ATTRIB_RAID1 __cpu_to_le32(0x00000002)
49 #define MPB_ATTRIB_RAID10 __cpu_to_le32(0x00000004)
50 #define MPB_ATTRIB_RAID1E __cpu_to_le32(0x00000008)
51 #define MPB_ATTRIB_RAID5 __cpu_to_le32(0x00000010)
52 #define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
53
54 #define MPB_SECTOR_CNT 418
55 #define IMSM_RESERVED_SECTORS 4096
56 #define SECT_PER_MB_SHIFT 11
57
58 /* Disk configuration info. */
59 #define IMSM_MAX_DEVICES 255
60 struct imsm_disk {
61 __u8 serial[MAX_RAID_SERIAL_LEN];/* 0xD8 - 0xE7 ascii serial number */
62 __u32 total_blocks; /* 0xE8 - 0xEB total blocks */
63 __u32 scsi_id; /* 0xEC - 0xEF scsi ID */
64 #define SPARE_DISK __cpu_to_le32(0x01) /* Spare */
65 #define CONFIGURED_DISK __cpu_to_le32(0x02) /* Member of some RaidDev */
66 #define FAILED_DISK __cpu_to_le32(0x04) /* Permanent failure */
67 __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 ddf;
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 #define MIGR_REPAIR 5
109 __u8 migr_type; /* Initializing, Rebuilding, ... */
110 __u8 dirty;
111 __u8 fs_state; /* fast-sync state for CnG (0xff == disabled) */
112 __u16 verify_errors; /* number of mismatches */
113 __u16 bad_blocks; /* number of bad blocks during verify */
114 __u32 filler[4];
115 struct imsm_map map[1];
116 /* here comes another one if migr_state */
117 } __attribute__ ((packed));
118
119 struct imsm_dev {
120 __u8 volume[MAX_RAID_SERIAL_LEN];
121 __u32 size_low;
122 __u32 size_high;
123 #define DEV_BOOTABLE __cpu_to_le32(0x01)
124 #define DEV_BOOT_DEVICE __cpu_to_le32(0x02)
125 #define DEV_READ_COALESCING __cpu_to_le32(0x04)
126 #define DEV_WRITE_COALESCING __cpu_to_le32(0x08)
127 #define DEV_LAST_SHUTDOWN_DIRTY __cpu_to_le32(0x10)
128 #define DEV_HIDDEN_AT_BOOT __cpu_to_le32(0x20)
129 #define DEV_CURRENTLY_HIDDEN __cpu_to_le32(0x40)
130 #define DEV_VERIFY_AND_FIX __cpu_to_le32(0x80)
131 #define DEV_MAP_STATE_UNINIT __cpu_to_le32(0x100)
132 #define DEV_NO_AUTO_RECOVERY __cpu_to_le32(0x200)
133 #define DEV_CLONE_N_GO __cpu_to_le32(0x400)
134 #define DEV_CLONE_MAN_SYNC __cpu_to_le32(0x800)
135 #define DEV_CNG_MASTER_DISK_NUM __cpu_to_le32(0x1000)
136 __u32 status; /* Persistent RaidDev status */
137 __u32 reserved_blocks; /* Reserved blocks at beginning of volume */
138 __u8 migr_priority;
139 __u8 num_sub_vols;
140 __u8 tid;
141 __u8 cng_master_disk;
142 __u16 cache_policy;
143 __u8 cng_state;
144 __u8 cng_sub_state;
145 #define IMSM_DEV_FILLERS 10
146 __u32 filler[IMSM_DEV_FILLERS];
147 struct imsm_vol vol;
148 } __attribute__ ((packed));
149
150 struct imsm_super {
151 __u8 sig[MAX_SIGNATURE_LENGTH]; /* 0x00 - 0x1F */
152 __u32 check_sum; /* 0x20 - 0x23 MPB Checksum */
153 __u32 mpb_size; /* 0x24 - 0x27 Size of MPB */
154 __u32 family_num; /* 0x28 - 0x2B Checksum from first time this config was written */
155 __u32 generation_num; /* 0x2C - 0x2F Incremented each time this array's MPB is written */
156 __u32 error_log_size; /* 0x30 - 0x33 in bytes */
157 __u32 attributes; /* 0x34 - 0x37 */
158 __u8 num_disks; /* 0x38 Number of configured disks */
159 __u8 num_raid_devs; /* 0x39 Number of configured volumes */
160 __u8 error_log_pos; /* 0x3A */
161 __u8 fill[1]; /* 0x3B */
162 __u32 cache_size; /* 0x3c - 0x40 in mb */
163 __u32 orig_family_num; /* 0x40 - 0x43 original family num */
164 __u32 pwr_cycle_count; /* 0x44 - 0x47 simulated power cycle count for array */
165 __u32 bbm_log_size; /* 0x48 - 0x4B - size of bad Block Mgmt Log in bytes */
166 #define IMSM_FILLERS 35
167 __u32 filler[IMSM_FILLERS]; /* 0x4C - 0xD7 RAID_MPB_FILLERS */
168 struct imsm_disk disk[1]; /* 0xD8 diskTbl[numDisks] */
169 /* here comes imsm_dev[num_raid_devs] */
170 /* here comes BBM logs */
171 } __attribute__ ((packed));
172
173 #define BBM_LOG_MAX_ENTRIES 254
174
175 struct bbm_log_entry {
176 __u64 defective_block_start;
177 #define UNREADABLE 0xFFFFFFFF
178 __u32 spare_block_offset;
179 __u16 remapped_marked_count;
180 __u16 disk_ordinal;
181 } __attribute__ ((__packed__));
182
183 struct bbm_log {
184 __u32 signature; /* 0xABADB10C */
185 __u32 entry_count;
186 __u32 reserved_spare_block_count; /* 0 */
187 __u32 reserved; /* 0xFFFF */
188 __u64 first_spare_lba;
189 struct bbm_log_entry mapped_block_entries[BBM_LOG_MAX_ENTRIES];
190 } __attribute__ ((__packed__));
191
192
193 #ifndef MDASSEMBLE
194 static char *map_state_str[] = { "normal", "uninitialized", "degraded", "failed" };
195 #endif
196
197 static __u8 migr_type(struct imsm_dev *dev)
198 {
199 if (dev->vol.migr_type == MIGR_VERIFY &&
200 dev->status & DEV_VERIFY_AND_FIX)
201 return MIGR_REPAIR;
202 else
203 return dev->vol.migr_type;
204 }
205
206 static void set_migr_type(struct imsm_dev *dev, __u8 migr_type)
207 {
208 /* for compatibility with older oroms convert MIGR_REPAIR, into
209 * MIGR_VERIFY w/ DEV_VERIFY_AND_FIX status
210 */
211 if (migr_type == MIGR_REPAIR) {
212 dev->vol.migr_type = MIGR_VERIFY;
213 dev->status |= DEV_VERIFY_AND_FIX;
214 } else {
215 dev->vol.migr_type = migr_type;
216 dev->status &= ~DEV_VERIFY_AND_FIX;
217 }
218 }
219
220 static unsigned int sector_count(__u32 bytes)
221 {
222 return ((bytes + (512-1)) & (~(512-1))) / 512;
223 }
224
225 static unsigned int mpb_sectors(struct imsm_super *mpb)
226 {
227 return sector_count(__le32_to_cpu(mpb->mpb_size));
228 }
229
230 struct intel_dev {
231 struct imsm_dev *dev;
232 struct intel_dev *next;
233 unsigned index;
234 };
235
236 enum action {
237 DISK_REMOVE = 1,
238 DISK_ADD
239 };
240 /* internal representation of IMSM metadata */
241 struct intel_super {
242 union {
243 void *buf; /* O_DIRECT buffer for reading/writing metadata */
244 struct imsm_super *anchor; /* immovable parameters */
245 };
246 size_t len; /* size of the 'buf' allocation */
247 void *next_buf; /* for realloc'ing buf from the manager */
248 size_t next_len;
249 int updates_pending; /* count of pending updates for mdmon */
250 int current_vol; /* index of raid device undergoing creation */
251 __u32 create_offset; /* common start for 'current_vol' */
252 __u32 random; /* random data for seeding new family numbers */
253 struct intel_dev *devlist;
254 struct dl {
255 struct dl *next;
256 int index;
257 __u8 serial[MAX_RAID_SERIAL_LEN];
258 int major, minor;
259 char *devname;
260 struct imsm_disk disk;
261 int fd;
262 int extent_cnt;
263 struct extent *e; /* for determining freespace @ create */
264 int raiddisk; /* slot to fill in autolayout */
265 enum action action;
266 } *disks;
267 struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
268 active */
269 struct dl *missing; /* disks removed while we weren't looking */
270 struct bbm_log *bbm_log;
271 const char *hba; /* device path of the raid controller for this metadata */
272 const struct imsm_orom *orom; /* platform firmware support */
273 struct intel_super *next; /* (temp) list for disambiguating family_num */
274 };
275
276 struct intel_disk {
277 struct imsm_disk disk;
278 #define IMSM_UNKNOWN_OWNER (-1)
279 int owner;
280 struct intel_disk *next;
281 };
282
283 struct extent {
284 unsigned long long start, size;
285 };
286
287 /* definition of messages passed to imsm_process_update */
288 enum imsm_update_type {
289 update_activate_spare,
290 update_create_array,
291 update_kill_array,
292 update_rename_array,
293 update_add_remove_disk,
294 update_reshape_container_disks,
295 };
296
297 struct imsm_update_activate_spare {
298 enum imsm_update_type type;
299 struct dl *dl;
300 int slot;
301 int array;
302 struct imsm_update_activate_spare *next;
303 };
304
305 struct geo_params {
306 int dev_id;
307 char *dev_name;
308 long long size;
309 int level;
310 int layout;
311 int chunksize;
312 int raid_disks;
313 };
314
315
316 struct imsm_update_reshape {
317 enum imsm_update_type type;
318 int old_raid_disks;
319 int new_raid_disks;
320 int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
321 };
322
323 struct disk_info {
324 __u8 serial[MAX_RAID_SERIAL_LEN];
325 };
326
327 struct imsm_update_create_array {
328 enum imsm_update_type type;
329 int dev_idx;
330 struct imsm_dev dev;
331 };
332
333 struct imsm_update_kill_array {
334 enum imsm_update_type type;
335 int dev_idx;
336 };
337
338 struct imsm_update_rename_array {
339 enum imsm_update_type type;
340 __u8 name[MAX_RAID_SERIAL_LEN];
341 int dev_idx;
342 };
343
344 struct imsm_update_add_remove_disk {
345 enum imsm_update_type type;
346 };
347
348 static struct supertype *match_metadata_desc_imsm(char *arg)
349 {
350 struct supertype *st;
351
352 if (strcmp(arg, "imsm") != 0 &&
353 strcmp(arg, "default") != 0
354 )
355 return NULL;
356
357 st = malloc(sizeof(*st));
358 if (!st)
359 return NULL;
360 memset(st, 0, sizeof(*st));
361 st->container_dev = NoMdDev;
362 st->ss = &super_imsm;
363 st->max_devs = IMSM_MAX_DEVICES;
364 st->minor_version = 0;
365 st->sb = NULL;
366 return st;
367 }
368
369 #ifndef MDASSEMBLE
370 static __u8 *get_imsm_version(struct imsm_super *mpb)
371 {
372 return &mpb->sig[MPB_SIG_LEN];
373 }
374 #endif
375
376 /* retrieve a disk directly from the anchor when the anchor is known to be
377 * up-to-date, currently only at load time
378 */
379 static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
380 {
381 if (index >= mpb->num_disks)
382 return NULL;
383 return &mpb->disk[index];
384 }
385
386 /* retrieve the disk description based on a index of the disk
387 * in the sub-array
388 */
389 static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
390 {
391 struct dl *d;
392
393 for (d = super->disks; d; d = d->next)
394 if (d->index == index)
395 return d;
396
397 return NULL;
398 }
399 /* retrieve a disk from the parsed metadata */
400 static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
401 {
402 struct dl *dl;
403
404 dl = get_imsm_dl_disk(super, index);
405 if (dl)
406 return &dl->disk;
407
408 return NULL;
409 }
410
411 /* generate a checksum directly from the anchor when the anchor is known to be
412 * up-to-date, currently only at load or write_super after coalescing
413 */
414 static __u32 __gen_imsm_checksum(struct imsm_super *mpb)
415 {
416 __u32 end = mpb->mpb_size / sizeof(end);
417 __u32 *p = (__u32 *) mpb;
418 __u32 sum = 0;
419
420 while (end--) {
421 sum += __le32_to_cpu(*p);
422 p++;
423 }
424
425 return sum - __le32_to_cpu(mpb->check_sum);
426 }
427
428 static size_t sizeof_imsm_map(struct imsm_map *map)
429 {
430 return sizeof(struct imsm_map) + sizeof(__u32) * (map->num_members - 1);
431 }
432
433 struct imsm_map *get_imsm_map(struct imsm_dev *dev, int second_map)
434 {
435 struct imsm_map *map = &dev->vol.map[0];
436
437 if (second_map && !dev->vol.migr_state)
438 return NULL;
439 else if (second_map) {
440 void *ptr = map;
441
442 return ptr + sizeof_imsm_map(map);
443 } else
444 return map;
445
446 }
447
448 /* return the size of the device.
449 * migr_state increases the returned size if map[0] were to be duplicated
450 */
451 static size_t sizeof_imsm_dev(struct imsm_dev *dev, int migr_state)
452 {
453 size_t size = sizeof(*dev) - sizeof(struct imsm_map) +
454 sizeof_imsm_map(get_imsm_map(dev, 0));
455
456 /* migrating means an additional map */
457 if (dev->vol.migr_state)
458 size += sizeof_imsm_map(get_imsm_map(dev, 1));
459 else if (migr_state)
460 size += sizeof_imsm_map(get_imsm_map(dev, 0));
461
462 return size;
463 }
464
465 #ifndef MDASSEMBLE
466 /* retrieve disk serial number list from a metadata update */
467 static struct disk_info *get_disk_info(struct imsm_update_create_array *update)
468 {
469 void *u = update;
470 struct disk_info *inf;
471
472 inf = u + sizeof(*update) - sizeof(struct imsm_dev) +
473 sizeof_imsm_dev(&update->dev, 0);
474
475 return inf;
476 }
477 #endif
478
479 static struct imsm_dev *__get_imsm_dev(struct imsm_super *mpb, __u8 index)
480 {
481 int offset;
482 int i;
483 void *_mpb = mpb;
484
485 if (index >= mpb->num_raid_devs)
486 return NULL;
487
488 /* devices start after all disks */
489 offset = ((void *) &mpb->disk[mpb->num_disks]) - _mpb;
490
491 for (i = 0; i <= index; i++)
492 if (i == index)
493 return _mpb + offset;
494 else
495 offset += sizeof_imsm_dev(_mpb + offset, 0);
496
497 return NULL;
498 }
499
500 static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
501 {
502 struct intel_dev *dv;
503
504 if (index >= super->anchor->num_raid_devs)
505 return NULL;
506 for (dv = super->devlist; dv; dv = dv->next)
507 if (dv->index == index)
508 return dv->dev;
509 return NULL;
510 }
511
512 static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
513 {
514 struct imsm_map *map;
515
516 if (dev->vol.migr_state)
517 map = get_imsm_map(dev, 1);
518 else
519 map = get_imsm_map(dev, 0);
520
521 /* top byte identifies disk under rebuild */
522 return __le32_to_cpu(map->disk_ord_tbl[slot]);
523 }
524
525 #define ord_to_idx(ord) (((ord) << 8) >> 8)
526 static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
527 {
528 __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
529
530 return ord_to_idx(ord);
531 }
532
533 static void set_imsm_ord_tbl_ent(struct imsm_map *map, int slot, __u32 ord)
534 {
535 map->disk_ord_tbl[slot] = __cpu_to_le32(ord);
536 }
537
538 static int get_imsm_disk_slot(struct imsm_map *map, unsigned idx)
539 {
540 int slot;
541 __u32 ord;
542
543 for (slot = 0; slot < map->num_members; slot++) {
544 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
545 if (ord_to_idx(ord) == idx)
546 return slot;
547 }
548
549 return -1;
550 }
551
552 static int get_imsm_raid_level(struct imsm_map *map)
553 {
554 if (map->raid_level == 1) {
555 if (map->num_members == 2)
556 return 1;
557 else
558 return 10;
559 }
560
561 return map->raid_level;
562 }
563
564 static int cmp_extent(const void *av, const void *bv)
565 {
566 const struct extent *a = av;
567 const struct extent *b = bv;
568 if (a->start < b->start)
569 return -1;
570 if (a->start > b->start)
571 return 1;
572 return 0;
573 }
574
575 static int count_memberships(struct dl *dl, struct intel_super *super)
576 {
577 int memberships = 0;
578 int i;
579
580 for (i = 0; i < super->anchor->num_raid_devs; i++) {
581 struct imsm_dev *dev = get_imsm_dev(super, i);
582 struct imsm_map *map = get_imsm_map(dev, 0);
583
584 if (get_imsm_disk_slot(map, dl->index) >= 0)
585 memberships++;
586 }
587
588 return memberships;
589 }
590
591 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
592 {
593 /* find a list of used extents on the given physical device */
594 struct extent *rv, *e;
595 int i;
596 int memberships = count_memberships(dl, super);
597 __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
598
599 rv = malloc(sizeof(struct extent) * (memberships + 1));
600 if (!rv)
601 return NULL;
602 e = rv;
603
604 for (i = 0; i < super->anchor->num_raid_devs; i++) {
605 struct imsm_dev *dev = get_imsm_dev(super, i);
606 struct imsm_map *map = get_imsm_map(dev, 0);
607
608 if (get_imsm_disk_slot(map, dl->index) >= 0) {
609 e->start = __le32_to_cpu(map->pba_of_lba0);
610 e->size = __le32_to_cpu(map->blocks_per_member);
611 e++;
612 }
613 }
614 qsort(rv, memberships, sizeof(*rv), cmp_extent);
615
616 /* determine the start of the metadata
617 * when no raid devices are defined use the default
618 * ...otherwise allow the metadata to truncate the value
619 * as is the case with older versions of imsm
620 */
621 if (memberships) {
622 struct extent *last = &rv[memberships - 1];
623 __u32 remainder;
624
625 remainder = __le32_to_cpu(dl->disk.total_blocks) -
626 (last->start + last->size);
627 /* round down to 1k block to satisfy precision of the kernel
628 * 'size' interface
629 */
630 remainder &= ~1UL;
631 /* make sure remainder is still sane */
632 if (remainder < (unsigned)ROUND_UP(super->len, 512) >> 9)
633 remainder = ROUND_UP(super->len, 512) >> 9;
634 if (reservation > remainder)
635 reservation = remainder;
636 }
637 e->start = __le32_to_cpu(dl->disk.total_blocks) - reservation;
638 e->size = 0;
639 return rv;
640 }
641
642 /* try to determine how much space is reserved for metadata from
643 * the last get_extents() entry, otherwise fallback to the
644 * default
645 */
646 static __u32 imsm_reserved_sectors(struct intel_super *super, struct dl *dl)
647 {
648 struct extent *e;
649 int i;
650 __u32 rv;
651
652 /* for spares just return a minimal reservation which will grow
653 * once the spare is picked up by an array
654 */
655 if (dl->index == -1)
656 return MPB_SECTOR_CNT;
657
658 e = get_extents(super, dl);
659 if (!e)
660 return MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
661
662 /* scroll to last entry */
663 for (i = 0; e[i].size; i++)
664 continue;
665
666 rv = __le32_to_cpu(dl->disk.total_blocks) - e[i].start;
667
668 free(e);
669
670 return rv;
671 }
672
673 static int is_spare(struct imsm_disk *disk)
674 {
675 return (disk->status & SPARE_DISK) == SPARE_DISK;
676 }
677
678 static int is_configured(struct imsm_disk *disk)
679 {
680 return (disk->status & CONFIGURED_DISK) == CONFIGURED_DISK;
681 }
682
683 static int is_failed(struct imsm_disk *disk)
684 {
685 return (disk->status & FAILED_DISK) == FAILED_DISK;
686 }
687
688 /* Return minimum size of a spare that can be used in this array*/
689 static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st)
690 {
691 struct intel_super *super = st->sb;
692 struct dl *dl;
693 struct extent *e;
694 int i;
695 unsigned long long rv = 0;
696
697 if (!super)
698 return rv;
699 /* find first active disk in array */
700 dl = super->disks;
701 while (dl && (is_failed(&dl->disk) || dl->index == -1))
702 dl = dl->next;
703 if (!dl)
704 return rv;
705 /* find last lba used by subarrays */
706 e = get_extents(super, dl);
707 if (!e)
708 return rv;
709 for (i = 0; e[i].size; i++)
710 continue;
711 if (i > 0)
712 rv = e[i-1].start + e[i-1].size;
713 free(e);
714 /* add the amount of space needed for metadata */
715 rv = rv + MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
716 return rv * 512;
717 }
718
719 #ifndef MDASSEMBLE
720 static __u64 blocks_per_migr_unit(struct imsm_dev *dev);
721
722 static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
723 {
724 __u64 sz;
725 int slot, i;
726 struct imsm_map *map = get_imsm_map(dev, 0);
727 __u32 ord;
728
729 printf("\n");
730 printf("[%.16s]:\n", dev->volume);
731 printf(" UUID : %s\n", uuid);
732 printf(" RAID Level : %d\n", get_imsm_raid_level(map));
733 printf(" Members : %d\n", map->num_members);
734 printf(" Slots : [");
735 for (i = 0; i < map->num_members; i++) {
736 ord = get_imsm_ord_tbl_ent(dev, i);
737 printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
738 }
739 printf("]\n");
740 slot = get_imsm_disk_slot(map, disk_idx);
741 if (slot >= 0) {
742 ord = get_imsm_ord_tbl_ent(dev, slot);
743 printf(" This Slot : %d%s\n", slot,
744 ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
745 } else
746 printf(" This Slot : ?\n");
747 sz = __le32_to_cpu(dev->size_high);
748 sz <<= 32;
749 sz += __le32_to_cpu(dev->size_low);
750 printf(" Array Size : %llu%s\n", (unsigned long long)sz,
751 human_size(sz * 512));
752 sz = __le32_to_cpu(map->blocks_per_member);
753 printf(" Per Dev Size : %llu%s\n", (unsigned long long)sz,
754 human_size(sz * 512));
755 printf(" Sector Offset : %u\n",
756 __le32_to_cpu(map->pba_of_lba0));
757 printf(" Num Stripes : %u\n",
758 __le32_to_cpu(map->num_data_stripes));
759 printf(" Chunk Size : %u KiB\n",
760 __le16_to_cpu(map->blocks_per_strip) / 2);
761 printf(" Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
762 printf(" Migrate State : ");
763 if (dev->vol.migr_state) {
764 if (migr_type(dev) == MIGR_INIT)
765 printf("initialize\n");
766 else if (migr_type(dev) == MIGR_REBUILD)
767 printf("rebuild\n");
768 else if (migr_type(dev) == MIGR_VERIFY)
769 printf("check\n");
770 else if (migr_type(dev) == MIGR_GEN_MIGR)
771 printf("general migration\n");
772 else if (migr_type(dev) == MIGR_STATE_CHANGE)
773 printf("state change\n");
774 else if (migr_type(dev) == MIGR_REPAIR)
775 printf("repair\n");
776 else
777 printf("<unknown:%d>\n", migr_type(dev));
778 } else
779 printf("idle\n");
780 printf(" Map State : %s", map_state_str[map->map_state]);
781 if (dev->vol.migr_state) {
782 struct imsm_map *map = get_imsm_map(dev, 1);
783
784 printf(" <-- %s", map_state_str[map->map_state]);
785 printf("\n Checkpoint : %u (%llu)",
786 __le32_to_cpu(dev->vol.curr_migr_unit),
787 (unsigned long long)blocks_per_migr_unit(dev));
788 }
789 printf("\n");
790 printf(" Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
791 }
792
793 static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
794 {
795 struct imsm_disk *disk = __get_imsm_disk(mpb, index);
796 char str[MAX_RAID_SERIAL_LEN + 1];
797 __u64 sz;
798
799 if (index < 0 || !disk)
800 return;
801
802 printf("\n");
803 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
804 printf(" Disk%02d Serial : %s\n", index, str);
805 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
806 is_configured(disk) ? " active" : "",
807 is_failed(disk) ? " failed" : "");
808 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
809 sz = __le32_to_cpu(disk->total_blocks) - reserved;
810 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
811 human_size(sz * 512));
812 }
813
814 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
815
816 static void examine_super_imsm(struct supertype *st, char *homehost)
817 {
818 struct intel_super *super = st->sb;
819 struct imsm_super *mpb = super->anchor;
820 char str[MAX_SIGNATURE_LENGTH];
821 int i;
822 struct mdinfo info;
823 char nbuf[64];
824 __u32 sum;
825 __u32 reserved = imsm_reserved_sectors(super, super->disks);
826 struct dl *dl;
827
828 snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
829 printf(" Magic : %s\n", str);
830 snprintf(str, strlen(MPB_VERSION_RAID0), "%s", get_imsm_version(mpb));
831 printf(" Version : %s\n", get_imsm_version(mpb));
832 printf(" Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
833 printf(" Family : %08x\n", __le32_to_cpu(mpb->family_num));
834 printf(" Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
835 getinfo_super_imsm(st, &info, NULL);
836 fname_from_uuid(st, &info, nbuf, ':');
837 printf(" UUID : %s\n", nbuf + 5);
838 sum = __le32_to_cpu(mpb->check_sum);
839 printf(" Checksum : %08x %s\n", sum,
840 __gen_imsm_checksum(mpb) == sum ? "correct" : "incorrect");
841 printf(" MPB Sectors : %d\n", mpb_sectors(mpb));
842 printf(" Disks : %d\n", mpb->num_disks);
843 printf(" RAID Devices : %d\n", mpb->num_raid_devs);
844 print_imsm_disk(mpb, super->disks->index, reserved);
845 if (super->bbm_log) {
846 struct bbm_log *log = super->bbm_log;
847
848 printf("\n");
849 printf("Bad Block Management Log:\n");
850 printf(" Log Size : %d\n", __le32_to_cpu(mpb->bbm_log_size));
851 printf(" Signature : %x\n", __le32_to_cpu(log->signature));
852 printf(" Entry Count : %d\n", __le32_to_cpu(log->entry_count));
853 printf(" Spare Blocks : %d\n", __le32_to_cpu(log->reserved_spare_block_count));
854 printf(" First Spare : %llx\n",
855 (unsigned long long) __le64_to_cpu(log->first_spare_lba));
856 }
857 for (i = 0; i < mpb->num_raid_devs; i++) {
858 struct mdinfo info;
859 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
860
861 super->current_vol = i;
862 getinfo_super_imsm(st, &info, NULL);
863 fname_from_uuid(st, &info, nbuf, ':');
864 print_imsm_dev(dev, nbuf + 5, super->disks->index);
865 }
866 for (i = 0; i < mpb->num_disks; i++) {
867 if (i == super->disks->index)
868 continue;
869 print_imsm_disk(mpb, i, reserved);
870 }
871 for (dl = super->disks ; dl; dl = dl->next) {
872 struct imsm_disk *disk;
873 char str[MAX_RAID_SERIAL_LEN + 1];
874 __u64 sz;
875
876 if (dl->index >= 0)
877 continue;
878
879 disk = &dl->disk;
880 printf("\n");
881 snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
882 printf(" Disk Serial : %s\n", str);
883 printf(" State :%s%s%s\n", is_spare(disk) ? " spare" : "",
884 is_configured(disk) ? " active" : "",
885 is_failed(disk) ? " failed" : "");
886 printf(" Id : %08x\n", __le32_to_cpu(disk->scsi_id));
887 sz = __le32_to_cpu(disk->total_blocks) - reserved;
888 printf(" Usable Size : %llu%s\n", (unsigned long long)sz,
889 human_size(sz * 512));
890 }
891 }
892
893 static void brief_examine_super_imsm(struct supertype *st, int verbose)
894 {
895 /* We just write a generic IMSM ARRAY entry */
896 struct mdinfo info;
897 char nbuf[64];
898 struct intel_super *super = st->sb;
899
900 if (!super->anchor->num_raid_devs) {
901 printf("ARRAY metadata=imsm\n");
902 return;
903 }
904
905 getinfo_super_imsm(st, &info, NULL);
906 fname_from_uuid(st, &info, nbuf, ':');
907 printf("ARRAY metadata=imsm UUID=%s\n", nbuf + 5);
908 }
909
910 static void brief_examine_subarrays_imsm(struct supertype *st, int verbose)
911 {
912 /* We just write a generic IMSM ARRAY entry */
913 struct mdinfo info;
914 char nbuf[64];
915 char nbuf1[64];
916 struct intel_super *super = st->sb;
917 int i;
918
919 if (!super->anchor->num_raid_devs)
920 return;
921
922 getinfo_super_imsm(st, &info, NULL);
923 fname_from_uuid(st, &info, nbuf, ':');
924 for (i = 0; i < super->anchor->num_raid_devs; i++) {
925 struct imsm_dev *dev = get_imsm_dev(super, i);
926
927 super->current_vol = i;
928 getinfo_super_imsm(st, &info, NULL);
929 fname_from_uuid(st, &info, nbuf1, ':');
930 printf("ARRAY /dev/md/%.16s container=%s member=%d UUID=%s\n",
931 dev->volume, nbuf + 5, i, nbuf1 + 5);
932 }
933 }
934
935 static void export_examine_super_imsm(struct supertype *st)
936 {
937 struct intel_super *super = st->sb;
938 struct imsm_super *mpb = super->anchor;
939 struct mdinfo info;
940 char nbuf[64];
941
942 getinfo_super_imsm(st, &info, NULL);
943 fname_from_uuid(st, &info, nbuf, ':');
944 printf("MD_METADATA=imsm\n");
945 printf("MD_LEVEL=container\n");
946 printf("MD_UUID=%s\n", nbuf+5);
947 printf("MD_DEVICES=%u\n", mpb->num_disks);
948 }
949
950 static void detail_super_imsm(struct supertype *st, char *homehost)
951 {
952 struct mdinfo info;
953 char nbuf[64];
954
955 getinfo_super_imsm(st, &info, NULL);
956 fname_from_uuid(st, &info, nbuf, ':');
957 printf("\n UUID : %s\n", nbuf + 5);
958 }
959
960 static void brief_detail_super_imsm(struct supertype *st)
961 {
962 struct mdinfo info;
963 char nbuf[64];
964 getinfo_super_imsm(st, &info, NULL);
965 fname_from_uuid(st, &info, nbuf, ':');
966 printf(" UUID=%s", nbuf + 5);
967 }
968
969 static int imsm_read_serial(int fd, char *devname, __u8 *serial);
970 static void fd2devname(int fd, char *name);
971
972 static int imsm_enumerate_ports(const char *hba_path, int port_count, int host_base, int verbose)
973 {
974 /* dump an unsorted list of devices attached to ahci, as well as
975 * non-connected ports
976 */
977 int hba_len = strlen(hba_path) + 1;
978 struct dirent *ent;
979 DIR *dir;
980 char *path = NULL;
981 int err = 0;
982 unsigned long port_mask = (1 << port_count) - 1;
983
984 if (port_count > (int)sizeof(port_mask) * 8) {
985 if (verbose)
986 fprintf(stderr, Name ": port_count %d out of range\n", port_count);
987 return 2;
988 }
989
990 /* scroll through /sys/dev/block looking for devices attached to
991 * this hba
992 */
993 dir = opendir("/sys/dev/block");
994 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
995 int fd;
996 char model[64];
997 char vendor[64];
998 char buf[1024];
999 int major, minor;
1000 char *device;
1001 char *c;
1002 int port;
1003 int type;
1004
1005 if (sscanf(ent->d_name, "%d:%d", &major, &minor) != 2)
1006 continue;
1007 path = devt_to_devpath(makedev(major, minor));
1008 if (!path)
1009 continue;
1010 if (!path_attached_to_hba(path, hba_path)) {
1011 free(path);
1012 path = NULL;
1013 continue;
1014 }
1015
1016 /* retrieve the scsi device type */
1017 if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
1018 if (verbose)
1019 fprintf(stderr, Name ": failed to allocate 'device'\n");
1020 err = 2;
1021 break;
1022 }
1023 sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
1024 if (load_sys(device, buf) != 0) {
1025 if (verbose)
1026 fprintf(stderr, Name ": failed to read device type for %s\n",
1027 path);
1028 err = 2;
1029 free(device);
1030 break;
1031 }
1032 type = strtoul(buf, NULL, 10);
1033
1034 /* if it's not a disk print the vendor and model */
1035 if (!(type == 0 || type == 7 || type == 14)) {
1036 vendor[0] = '\0';
1037 model[0] = '\0';
1038 sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
1039 if (load_sys(device, buf) == 0) {
1040 strncpy(vendor, buf, sizeof(vendor));
1041 vendor[sizeof(vendor) - 1] = '\0';
1042 c = (char *) &vendor[sizeof(vendor) - 1];
1043 while (isspace(*c) || *c == '\0')
1044 *c-- = '\0';
1045
1046 }
1047 sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
1048 if (load_sys(device, buf) == 0) {
1049 strncpy(model, buf, sizeof(model));
1050 model[sizeof(model) - 1] = '\0';
1051 c = (char *) &model[sizeof(model) - 1];
1052 while (isspace(*c) || *c == '\0')
1053 *c-- = '\0';
1054 }
1055
1056 if (vendor[0] && model[0])
1057 sprintf(buf, "%.64s %.64s", vendor, model);
1058 else
1059 switch (type) { /* numbers from hald/linux/device.c */
1060 case 1: sprintf(buf, "tape"); break;
1061 case 2: sprintf(buf, "printer"); break;
1062 case 3: sprintf(buf, "processor"); break;
1063 case 4:
1064 case 5: sprintf(buf, "cdrom"); break;
1065 case 6: sprintf(buf, "scanner"); break;
1066 case 8: sprintf(buf, "media_changer"); break;
1067 case 9: sprintf(buf, "comm"); break;
1068 case 12: sprintf(buf, "raid"); break;
1069 default: sprintf(buf, "unknown");
1070 }
1071 } else
1072 buf[0] = '\0';
1073 free(device);
1074
1075 /* chop device path to 'host%d' and calculate the port number */
1076 c = strchr(&path[hba_len], '/');
1077 if (!c) {
1078 if (verbose)
1079 fprintf(stderr, Name ": %s - invalid path name\n", path + hba_len);
1080 err = 2;
1081 break;
1082 }
1083 *c = '\0';
1084 if (sscanf(&path[hba_len], "host%d", &port) == 1)
1085 port -= host_base;
1086 else {
1087 if (verbose) {
1088 *c = '/'; /* repair the full string */
1089 fprintf(stderr, Name ": failed to determine port number for %s\n",
1090 path);
1091 }
1092 err = 2;
1093 break;
1094 }
1095
1096 /* mark this port as used */
1097 port_mask &= ~(1 << port);
1098
1099 /* print out the device information */
1100 if (buf[0]) {
1101 printf(" Port%d : - non-disk device (%s) -\n", port, buf);
1102 continue;
1103 }
1104
1105 fd = dev_open(ent->d_name, O_RDONLY);
1106 if (fd < 0)
1107 printf(" Port%d : - disk info unavailable -\n", port);
1108 else {
1109 fd2devname(fd, buf);
1110 printf(" Port%d : %s", port, buf);
1111 if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
1112 printf(" (%s)\n", buf);
1113 else
1114 printf("()\n");
1115 }
1116 close(fd);
1117 free(path);
1118 path = NULL;
1119 }
1120 if (path)
1121 free(path);
1122 if (dir)
1123 closedir(dir);
1124 if (err == 0) {
1125 int i;
1126
1127 for (i = 0; i < port_count; i++)
1128 if (port_mask & (1 << i))
1129 printf(" Port%d : - no device attached -\n", i);
1130 }
1131
1132 return err;
1133 }
1134
1135 static int detail_platform_imsm(int verbose, int enumerate_only)
1136 {
1137 /* There are two components to imsm platform support, the ahci SATA
1138 * controller and the option-rom. To find the SATA controller we
1139 * simply look in /sys/bus/pci/drivers/ahci to see if an ahci
1140 * controller with the Intel vendor id is present. This approach
1141 * allows mdadm to leverage the kernel's ahci detection logic, with the
1142 * caveat that if ahci.ko is not loaded mdadm will not be able to
1143 * detect platform raid capabilities. The option-rom resides in a
1144 * platform "Adapter ROM". We scan for its signature to retrieve the
1145 * platform capabilities. If raid support is disabled in the BIOS the
1146 * option-rom capability structure will not be available.
1147 */
1148 const struct imsm_orom *orom;
1149 struct sys_dev *list, *hba;
1150 DIR *dir;
1151 struct dirent *ent;
1152 const char *hba_path;
1153 int host_base = 0;
1154 int port_count = 0;
1155
1156 if (enumerate_only) {
1157 if (check_env("IMSM_NO_PLATFORM") || find_imsm_orom())
1158 return 0;
1159 return 2;
1160 }
1161
1162 list = find_driver_devices("pci", "ahci");
1163 for (hba = list; hba; hba = hba->next)
1164 if (devpath_to_vendor(hba->path) == 0x8086)
1165 break;
1166
1167 if (!hba) {
1168 if (verbose)
1169 fprintf(stderr, Name ": unable to find active ahci controller\n");
1170 free_sys_dev(&list);
1171 return 2;
1172 } else if (verbose)
1173 fprintf(stderr, Name ": found Intel SATA AHCI Controller\n");
1174 hba_path = hba->path;
1175 hba->path = NULL;
1176 free_sys_dev(&list);
1177
1178 orom = find_imsm_orom();
1179 if (!orom) {
1180 if (verbose)
1181 fprintf(stderr, Name ": imsm option-rom not found\n");
1182 return 2;
1183 }
1184
1185 printf(" Platform : Intel(R) Matrix Storage Manager\n");
1186 printf(" Version : %d.%d.%d.%d\n", orom->major_ver, orom->minor_ver,
1187 orom->hotfix_ver, orom->build);
1188 printf(" RAID Levels :%s%s%s%s%s\n",
1189 imsm_orom_has_raid0(orom) ? " raid0" : "",
1190 imsm_orom_has_raid1(orom) ? " raid1" : "",
1191 imsm_orom_has_raid1e(orom) ? " raid1e" : "",
1192 imsm_orom_has_raid10(orom) ? " raid10" : "",
1193 imsm_orom_has_raid5(orom) ? " raid5" : "");
1194 printf(" Chunk Sizes :%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1195 imsm_orom_has_chunk(orom, 2) ? " 2k" : "",
1196 imsm_orom_has_chunk(orom, 4) ? " 4k" : "",
1197 imsm_orom_has_chunk(orom, 8) ? " 8k" : "",
1198 imsm_orom_has_chunk(orom, 16) ? " 16k" : "",
1199 imsm_orom_has_chunk(orom, 32) ? " 32k" : "",
1200 imsm_orom_has_chunk(orom, 64) ? " 64k" : "",
1201 imsm_orom_has_chunk(orom, 128) ? " 128k" : "",
1202 imsm_orom_has_chunk(orom, 256) ? " 256k" : "",
1203 imsm_orom_has_chunk(orom, 512) ? " 512k" : "",
1204 imsm_orom_has_chunk(orom, 1024*1) ? " 1M" : "",
1205 imsm_orom_has_chunk(orom, 1024*2) ? " 2M" : "",
1206 imsm_orom_has_chunk(orom, 1024*4) ? " 4M" : "",
1207 imsm_orom_has_chunk(orom, 1024*8) ? " 8M" : "",
1208 imsm_orom_has_chunk(orom, 1024*16) ? " 16M" : "",
1209 imsm_orom_has_chunk(orom, 1024*32) ? " 32M" : "",
1210 imsm_orom_has_chunk(orom, 1024*64) ? " 64M" : "");
1211 printf(" Max Disks : %d\n", orom->tds);
1212 printf(" Max Volumes : %d\n", orom->vpa);
1213 printf(" I/O Controller : %s\n", hba_path);
1214
1215 /* find the smallest scsi host number to determine a port number base */
1216 dir = opendir(hba_path);
1217 for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
1218 int host;
1219
1220 if (sscanf(ent->d_name, "host%d", &host) != 1)
1221 continue;
1222 if (port_count == 0)
1223 host_base = host;
1224 else if (host < host_base)
1225 host_base = host;
1226
1227 if (host + 1 > port_count + host_base)
1228 port_count = host + 1 - host_base;
1229
1230 }
1231 if (dir)
1232 closedir(dir);
1233
1234 if (!port_count || imsm_enumerate_ports(hba_path, port_count,
1235 host_base, verbose) != 0) {
1236 if (verbose)
1237 fprintf(stderr, Name ": failed to enumerate ports\n");
1238 return 2;
1239 }
1240
1241 return 0;
1242 }
1243 #endif
1244
1245 static int match_home_imsm(struct supertype *st, char *homehost)
1246 {
1247 /* the imsm metadata format does not specify any host
1248 * identification information. We return -1 since we can never
1249 * confirm nor deny whether a given array is "meant" for this
1250 * host. We rely on compare_super and the 'family_num' fields to
1251 * exclude member disks that do not belong, and we rely on
1252 * mdadm.conf to specify the arrays that should be assembled.
1253 * Auto-assembly may still pick up "foreign" arrays.
1254 */
1255
1256 return -1;
1257 }
1258
1259 static void uuid_from_super_imsm(struct supertype *st, int uuid[4])
1260 {
1261 /* The uuid returned here is used for:
1262 * uuid to put into bitmap file (Create, Grow)
1263 * uuid for backup header when saving critical section (Grow)
1264 * comparing uuids when re-adding a device into an array
1265 * In these cases the uuid required is that of the data-array,
1266 * not the device-set.
1267 * uuid to recognise same set when adding a missing device back
1268 * to an array. This is a uuid for the device-set.
1269 *
1270 * For each of these we can make do with a truncated
1271 * or hashed uuid rather than the original, as long as
1272 * everyone agrees.
1273 * In each case the uuid required is that of the data-array,
1274 * not the device-set.
1275 */
1276 /* imsm does not track uuid's so we synthesis one using sha1 on
1277 * - The signature (Which is constant for all imsm array, but no matter)
1278 * - the orig_family_num of the container
1279 * - the index number of the volume
1280 * - the 'serial' number of the volume.
1281 * Hopefully these are all constant.
1282 */
1283 struct intel_super *super = st->sb;
1284
1285 char buf[20];
1286 struct sha1_ctx ctx;
1287 struct imsm_dev *dev = NULL;
1288 __u32 family_num;
1289
1290 /* some mdadm versions failed to set ->orig_family_num, in which
1291 * case fall back to ->family_num. orig_family_num will be
1292 * fixed up with the first metadata update.
1293 */
1294 family_num = super->anchor->orig_family_num;
1295 if (family_num == 0)
1296 family_num = super->anchor->family_num;
1297 sha1_init_ctx(&ctx);
1298 sha1_process_bytes(super->anchor->sig, MPB_SIG_LEN, &ctx);
1299 sha1_process_bytes(&family_num, sizeof(__u32), &ctx);
1300 if (super->current_vol >= 0)
1301 dev = get_imsm_dev(super, super->current_vol);
1302 if (dev) {
1303 __u32 vol = super->current_vol;
1304 sha1_process_bytes(&vol, sizeof(vol), &ctx);
1305 sha1_process_bytes(dev->volume, MAX_RAID_SERIAL_LEN, &ctx);
1306 }
1307 sha1_finish_ctx(&ctx, buf);
1308 memcpy(uuid, buf, 4*4);
1309 }
1310
1311 #if 0
1312 static void
1313 get_imsm_numerical_version(struct imsm_super *mpb, int *m, int *p)
1314 {
1315 __u8 *v = get_imsm_version(mpb);
1316 __u8 *end = mpb->sig + MAX_SIGNATURE_LENGTH;
1317 char major[] = { 0, 0, 0 };
1318 char minor[] = { 0 ,0, 0 };
1319 char patch[] = { 0, 0, 0 };
1320 char *ver_parse[] = { major, minor, patch };
1321 int i, j;
1322
1323 i = j = 0;
1324 while (*v != '\0' && v < end) {
1325 if (*v != '.' && j < 2)
1326 ver_parse[i][j++] = *v;
1327 else {
1328 i++;
1329 j = 0;
1330 }
1331 v++;
1332 }
1333
1334 *m = strtol(minor, NULL, 0);
1335 *p = strtol(patch, NULL, 0);
1336 }
1337 #endif
1338
1339 static __u32 migr_strip_blocks_resync(struct imsm_dev *dev)
1340 {
1341 /* migr_strip_size when repairing or initializing parity */
1342 struct imsm_map *map = get_imsm_map(dev, 0);
1343 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1344
1345 switch (get_imsm_raid_level(map)) {
1346 case 5:
1347 case 10:
1348 return chunk;
1349 default:
1350 return 128*1024 >> 9;
1351 }
1352 }
1353
1354 static __u32 migr_strip_blocks_rebuild(struct imsm_dev *dev)
1355 {
1356 /* migr_strip_size when rebuilding a degraded disk, no idea why
1357 * this is different than migr_strip_size_resync(), but it's good
1358 * to be compatible
1359 */
1360 struct imsm_map *map = get_imsm_map(dev, 1);
1361 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1362
1363 switch (get_imsm_raid_level(map)) {
1364 case 1:
1365 case 10:
1366 if (map->num_members % map->num_domains == 0)
1367 return 128*1024 >> 9;
1368 else
1369 return chunk;
1370 case 5:
1371 return max((__u32) 64*1024 >> 9, chunk);
1372 default:
1373 return 128*1024 >> 9;
1374 }
1375 }
1376
1377 static __u32 num_stripes_per_unit_resync(struct imsm_dev *dev)
1378 {
1379 struct imsm_map *lo = get_imsm_map(dev, 0);
1380 struct imsm_map *hi = get_imsm_map(dev, 1);
1381 __u32 lo_chunk = __le32_to_cpu(lo->blocks_per_strip);
1382 __u32 hi_chunk = __le32_to_cpu(hi->blocks_per_strip);
1383
1384 return max((__u32) 1, hi_chunk / lo_chunk);
1385 }
1386
1387 static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
1388 {
1389 struct imsm_map *lo = get_imsm_map(dev, 0);
1390 int level = get_imsm_raid_level(lo);
1391
1392 if (level == 1 || level == 10) {
1393 struct imsm_map *hi = get_imsm_map(dev, 1);
1394
1395 return hi->num_domains;
1396 } else
1397 return num_stripes_per_unit_resync(dev);
1398 }
1399
1400 static __u8 imsm_num_data_members(struct imsm_dev *dev)
1401 {
1402 /* named 'imsm_' because raid0, raid1 and raid10
1403 * counter-intuitively have the same number of data disks
1404 */
1405 struct imsm_map *map = get_imsm_map(dev, 0);
1406
1407 switch (get_imsm_raid_level(map)) {
1408 case 0:
1409 case 1:
1410 case 10:
1411 return map->num_members;
1412 case 5:
1413 return map->num_members - 1;
1414 default:
1415 dprintf("%s: unsupported raid level\n", __func__);
1416 return 0;
1417 }
1418 }
1419
1420 static __u32 parity_segment_depth(struct imsm_dev *dev)
1421 {
1422 struct imsm_map *map = get_imsm_map(dev, 0);
1423 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1424
1425 switch(get_imsm_raid_level(map)) {
1426 case 1:
1427 case 10:
1428 return chunk * map->num_domains;
1429 case 5:
1430 return chunk * map->num_members;
1431 default:
1432 return chunk;
1433 }
1434 }
1435
1436 static __u32 map_migr_block(struct imsm_dev *dev, __u32 block)
1437 {
1438 struct imsm_map *map = get_imsm_map(dev, 1);
1439 __u32 chunk = __le32_to_cpu(map->blocks_per_strip);
1440 __u32 strip = block / chunk;
1441
1442 switch (get_imsm_raid_level(map)) {
1443 case 1:
1444 case 10: {
1445 __u32 vol_strip = (strip * map->num_domains) + 1;
1446 __u32 vol_stripe = vol_strip / map->num_members;
1447
1448 return vol_stripe * chunk + block % chunk;
1449 } case 5: {
1450 __u32 stripe = strip / (map->num_members - 1);
1451
1452 return stripe * chunk + block % chunk;
1453 }
1454 default:
1455 return 0;
1456 }
1457 }
1458
1459 static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
1460 {
1461 /* calculate the conversion factor between per member 'blocks'
1462 * (md/{resync,rebuild}_start) and imsm migration units, return
1463 * 0 for the 'not migrating' and 'unsupported migration' cases
1464 */
1465 if (!dev->vol.migr_state)
1466 return 0;
1467
1468 switch (migr_type(dev)) {
1469 case MIGR_VERIFY:
1470 case MIGR_REPAIR:
1471 case MIGR_INIT: {
1472 struct imsm_map *map = get_imsm_map(dev, 0);
1473 __u32 stripes_per_unit;
1474 __u32 blocks_per_unit;
1475 __u32 parity_depth;
1476 __u32 migr_chunk;
1477 __u32 block_map;
1478 __u32 block_rel;
1479 __u32 segment;
1480 __u32 stripe;
1481 __u8 disks;
1482
1483 /* yes, this is really the translation of migr_units to
1484 * per-member blocks in the 'resync' case
1485 */
1486 stripes_per_unit = num_stripes_per_unit_resync(dev);
1487 migr_chunk = migr_strip_blocks_resync(dev);
1488 disks = imsm_num_data_members(dev);
1489 blocks_per_unit = stripes_per_unit * migr_chunk * disks;
1490 stripe = __le32_to_cpu(map->blocks_per_strip) * disks;
1491 segment = blocks_per_unit / stripe;
1492 block_rel = blocks_per_unit - segment * stripe;
1493 parity_depth = parity_segment_depth(dev);
1494 block_map = map_migr_block(dev, block_rel);
1495 return block_map + parity_depth * segment;
1496 }
1497 case MIGR_REBUILD: {
1498 __u32 stripes_per_unit;
1499 __u32 migr_chunk;
1500
1501 stripes_per_unit = num_stripes_per_unit_rebuild(dev);
1502 migr_chunk = migr_strip_blocks_rebuild(dev);
1503 return migr_chunk * stripes_per_unit;
1504 }
1505 case MIGR_GEN_MIGR:
1506 /* FIXME I need a number here */
1507 case MIGR_STATE_CHANGE:
1508 default:
1509 return 0;
1510 }
1511 }
1512
1513 static int imsm_level_to_layout(int level)
1514 {
1515 switch (level) {
1516 case 0:
1517 case 1:
1518 return 0;
1519 case 5:
1520 case 6:
1521 return ALGORITHM_LEFT_ASYMMETRIC;
1522 case 10:
1523 return 0x102;
1524 }
1525 return UnSet;
1526 }
1527
1528 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
1529 {
1530 struct intel_super *super = st->sb;
1531 struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
1532 struct imsm_map *map = get_imsm_map(dev, 0);
1533 struct imsm_map *prev_map = get_imsm_map(dev, 1);
1534 struct dl *dl;
1535 char *devname;
1536 int map_disks = info->array.raid_disks;
1537
1538 for (dl = super->disks; dl; dl = dl->next)
1539 if (dl->raiddisk == info->disk.raid_disk)
1540 break;
1541 info->container_member = super->current_vol;
1542 info->array.raid_disks = map->num_members;
1543 info->array.level = get_imsm_raid_level(map);
1544 info->array.layout = imsm_level_to_layout(info->array.level);
1545 info->array.md_minor = -1;
1546 info->array.ctime = 0;
1547 info->array.utime = 0;
1548 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1549 info->array.state = !dev->vol.dirty;
1550 info->custom_array_size = __le32_to_cpu(dev->size_high);
1551 info->custom_array_size <<= 32;
1552 info->custom_array_size |= __le32_to_cpu(dev->size_low);
1553
1554 info->disk.major = 0;
1555 info->disk.minor = 0;
1556 if (dl) {
1557 info->disk.major = dl->major;
1558 info->disk.minor = dl->minor;
1559 }
1560
1561 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1562 info->component_size = __le32_to_cpu(map->blocks_per_member);
1563 memset(info->uuid, 0, sizeof(info->uuid));
1564 info->recovery_start = MaxSector;
1565 info->reshape_active = (prev_map != NULL);
1566 if (info->reshape_active)
1567 info->delta_disks = map->num_members - prev_map->num_members;
1568 else
1569 info->delta_disks = 0;
1570
1571 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) {
1572 info->resync_start = 0;
1573 } else if (dev->vol.migr_state) {
1574 switch (migr_type(dev)) {
1575 case MIGR_REPAIR:
1576 case MIGR_INIT: {
1577 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1578 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1579
1580 info->resync_start = blocks_per_unit * units;
1581 break;
1582 }
1583 case MIGR_VERIFY:
1584 /* we could emulate the checkpointing of
1585 * 'sync_action=check' migrations, but for now
1586 * we just immediately complete them
1587 */
1588 case MIGR_REBUILD:
1589 /* this is handled by container_content_imsm() */
1590 case MIGR_GEN_MIGR:
1591 case MIGR_STATE_CHANGE:
1592 /* FIXME handle other migrations */
1593 default:
1594 /* we are not dirty, so... */
1595 info->resync_start = MaxSector;
1596 }
1597 } else
1598 info->resync_start = MaxSector;
1599
1600 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1601 info->name[MAX_RAID_SERIAL_LEN] = 0;
1602
1603 info->array.major_version = -1;
1604 info->array.minor_version = -2;
1605 devname = devnum2devname(st->container_dev);
1606 *info->text_version = '\0';
1607 if (devname)
1608 sprintf(info->text_version, "/%s/%d", devname, info->container_member);
1609 free(devname);
1610 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
1611 uuid_from_super_imsm(st, info->uuid);
1612
1613 if (dmap) {
1614 int i, j;
1615 for (i=0; i<map_disks; i++) {
1616 dmap[i] = 0;
1617 if (i < info->array.raid_disks) {
1618 struct imsm_disk *dsk;
1619 j = get_imsm_disk_idx(dev, i);
1620 dsk = get_imsm_disk(super, j);
1621 if (dsk && (dsk->status & CONFIGURED_DISK))
1622 dmap[i] = 1;
1623 }
1624 }
1625 }
1626 }
1627
1628 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed);
1629 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev);
1630
1631 static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
1632 {
1633 struct dl *d;
1634
1635 for (d = super->missing; d; d = d->next)
1636 if (d->index == index)
1637 return &d->disk;
1638 return NULL;
1639 }
1640
1641 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
1642 {
1643 struct intel_super *super = st->sb;
1644 struct imsm_disk *disk;
1645 int map_disks = info->array.raid_disks;
1646 int max_enough = -1;
1647 int i;
1648 struct imsm_super *mpb;
1649
1650 if (super->current_vol >= 0) {
1651 getinfo_super_imsm_volume(st, info, map);
1652 return;
1653 }
1654
1655 /* Set raid_disks to zero so that Assemble will always pull in valid
1656 * spares
1657 */
1658 info->array.raid_disks = 0;
1659 info->array.level = LEVEL_CONTAINER;
1660 info->array.layout = 0;
1661 info->array.md_minor = -1;
1662 info->array.ctime = 0; /* N/A for imsm */
1663 info->array.utime = 0;
1664 info->array.chunk_size = 0;
1665
1666 info->disk.major = 0;
1667 info->disk.minor = 0;
1668 info->disk.raid_disk = -1;
1669 info->reshape_active = 0;
1670 info->array.major_version = -1;
1671 info->array.minor_version = -2;
1672 strcpy(info->text_version, "imsm");
1673 info->safe_mode_delay = 0;
1674 info->disk.number = -1;
1675 info->disk.state = 0;
1676 info->name[0] = 0;
1677 info->recovery_start = MaxSector;
1678
1679 /* do we have the all the insync disks that we expect? */
1680 mpb = super->anchor;
1681
1682 for (i = 0; i < mpb->num_raid_devs; i++) {
1683 struct imsm_dev *dev = get_imsm_dev(super, i);
1684 int failed, enough, j, missing = 0;
1685 struct imsm_map *map;
1686 __u8 state;
1687
1688 failed = imsm_count_failed(super, dev);
1689 state = imsm_check_degraded(super, dev, failed);
1690 map = get_imsm_map(dev, dev->vol.migr_state);
1691
1692 /* any newly missing disks?
1693 * (catches single-degraded vs double-degraded)
1694 */
1695 for (j = 0; j < map->num_members; j++) {
1696 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
1697 __u32 idx = ord_to_idx(ord);
1698
1699 if (!(ord & IMSM_ORD_REBUILD) &&
1700 get_imsm_missing(super, idx)) {
1701 missing = 1;
1702 break;
1703 }
1704 }
1705
1706 if (state == IMSM_T_STATE_FAILED)
1707 enough = -1;
1708 else if (state == IMSM_T_STATE_DEGRADED &&
1709 (state != map->map_state || missing))
1710 enough = 0;
1711 else /* we're normal, or already degraded */
1712 enough = 1;
1713
1714 /* in the missing/failed disk case check to see
1715 * if at least one array is runnable
1716 */
1717 max_enough = max(max_enough, enough);
1718 }
1719 dprintf("%s: enough: %d\n", __func__, max_enough);
1720 info->container_enough = max_enough;
1721
1722 if (super->disks) {
1723 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1724
1725 disk = &super->disks->disk;
1726 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1727 info->component_size = reserved;
1728 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
1729 /* we don't change info->disk.raid_disk here because
1730 * this state will be finalized in mdmon after we have
1731 * found the 'most fresh' version of the metadata
1732 */
1733 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1734 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
1735 }
1736
1737 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1738 * ->compare_super may have updated the 'num_raid_devs' field for spares
1739 */
1740 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
1741 uuid_from_super_imsm(st, info->uuid);
1742 else
1743 memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
1744
1745 /* I don't know how to compute 'map' on imsm, so use safe default */
1746 if (map) {
1747 int i;
1748 for (i = 0; i < map_disks; i++)
1749 map[i] = 1;
1750 }
1751
1752 }
1753
1754 /* allocates memory and fills disk in mdinfo structure
1755 * for each disk in array */
1756 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
1757 {
1758 struct mdinfo *mddev = NULL;
1759 struct intel_super *super = st->sb;
1760 struct imsm_disk *disk;
1761 int count = 0;
1762 struct dl *dl;
1763 if (!super || !super->disks)
1764 return NULL;
1765 dl = super->disks;
1766 mddev = malloc(sizeof(*mddev));
1767 if (!mddev) {
1768 fprintf(stderr, Name ": Failed to allocate memory.\n");
1769 return NULL;
1770 }
1771 memset(mddev, 0, sizeof(*mddev));
1772 while (dl) {
1773 struct mdinfo *tmp;
1774 disk = &dl->disk;
1775 tmp = malloc(sizeof(*tmp));
1776 if (!tmp) {
1777 fprintf(stderr, Name ": Failed to allocate memory.\n");
1778 if (mddev)
1779 sysfs_free(mddev);
1780 return NULL;
1781 }
1782 memset(tmp, 0, sizeof(*tmp));
1783 if (mddev->devs)
1784 tmp->next = mddev->devs;
1785 mddev->devs = tmp;
1786 tmp->disk.number = count++;
1787 tmp->disk.major = dl->major;
1788 tmp->disk.minor = dl->minor;
1789 tmp->disk.state = is_configured(disk) ?
1790 (1 << MD_DISK_ACTIVE) : 0;
1791 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1792 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
1793 tmp->disk.raid_disk = -1;
1794 dl = dl->next;
1795 }
1796 return mddev;
1797 }
1798
1799 static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1800 char *update, char *devname, int verbose,
1801 int uuid_set, char *homehost)
1802 {
1803 /* For 'assemble' and 'force' we need to return non-zero if any
1804 * change was made. For others, the return value is ignored.
1805 * Update options are:
1806 * force-one : This device looks a bit old but needs to be included,
1807 * update age info appropriately.
1808 * assemble: clear any 'faulty' flag to allow this device to
1809 * be assembled.
1810 * force-array: Array is degraded but being forced, mark it clean
1811 * if that will be needed to assemble it.
1812 *
1813 * newdev: not used ????
1814 * grow: Array has gained a new device - this is currently for
1815 * linear only
1816 * resync: mark as dirty so a resync will happen.
1817 * name: update the name - preserving the homehost
1818 * uuid: Change the uuid of the array to match watch is given
1819 *
1820 * Following are not relevant for this imsm:
1821 * sparc2.2 : update from old dodgey metadata
1822 * super-minor: change the preferred_minor number
1823 * summaries: update redundant counters.
1824 * homehost: update the recorded homehost
1825 * _reshape_progress: record new reshape_progress position.
1826 */
1827 int rv = 1;
1828 struct intel_super *super = st->sb;
1829 struct imsm_super *mpb;
1830
1831 /* we can only update container info */
1832 if (!super || super->current_vol >= 0 || !super->anchor)
1833 return 1;
1834
1835 mpb = super->anchor;
1836
1837 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
1838 rv = -1;
1839 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
1840 mpb->orig_family_num = *((__u32 *) info->update_private);
1841 rv = 0;
1842 } else if (strcmp(update, "uuid") == 0) {
1843 __u32 *new_family = malloc(sizeof(*new_family));
1844
1845 /* update orig_family_number with the incoming random
1846 * data, report the new effective uuid, and store the
1847 * new orig_family_num for future updates.
1848 */
1849 if (new_family) {
1850 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
1851 uuid_from_super_imsm(st, info->uuid);
1852 *new_family = mpb->orig_family_num;
1853 info->update_private = new_family;
1854 rv = 0;
1855 }
1856 } else if (strcmp(update, "assemble") == 0)
1857 rv = 0;
1858 else
1859 rv = -1;
1860
1861 /* successful update? recompute checksum */
1862 if (rv == 0)
1863 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
1864
1865 return rv;
1866 }
1867
1868 static size_t disks_to_mpb_size(int disks)
1869 {
1870 size_t size;
1871
1872 size = sizeof(struct imsm_super);
1873 size += (disks - 1) * sizeof(struct imsm_disk);
1874 size += 2 * sizeof(struct imsm_dev);
1875 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1876 size += (4 - 2) * sizeof(struct imsm_map);
1877 /* 4 possible disk_ord_tbl's */
1878 size += 4 * (disks - 1) * sizeof(__u32);
1879
1880 return size;
1881 }
1882
1883 static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1884 {
1885 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1886 return 0;
1887
1888 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
1889 }
1890
1891 static void free_devlist(struct intel_super *super)
1892 {
1893 struct intel_dev *dv;
1894
1895 while (super->devlist) {
1896 dv = super->devlist->next;
1897 free(super->devlist->dev);
1898 free(super->devlist);
1899 super->devlist = dv;
1900 }
1901 }
1902
1903 static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1904 {
1905 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1906 }
1907
1908 static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1909 {
1910 /*
1911 * return:
1912 * 0 same, or first was empty, and second was copied
1913 * 1 second had wrong number
1914 * 2 wrong uuid
1915 * 3 wrong other info
1916 */
1917 struct intel_super *first = st->sb;
1918 struct intel_super *sec = tst->sb;
1919
1920 if (!first) {
1921 st->sb = tst->sb;
1922 tst->sb = NULL;
1923 return 0;
1924 }
1925
1926 /* if an anchor does not have num_raid_devs set then it is a free
1927 * floating spare
1928 */
1929 if (first->anchor->num_raid_devs > 0 &&
1930 sec->anchor->num_raid_devs > 0) {
1931 /* Determine if these disks might ever have been
1932 * related. Further disambiguation can only take place
1933 * in load_super_imsm_all
1934 */
1935 __u32 first_family = first->anchor->orig_family_num;
1936 __u32 sec_family = sec->anchor->orig_family_num;
1937
1938 if (memcmp(first->anchor->sig, sec->anchor->sig,
1939 MAX_SIGNATURE_LENGTH) != 0)
1940 return 3;
1941
1942 if (first_family == 0)
1943 first_family = first->anchor->family_num;
1944 if (sec_family == 0)
1945 sec_family = sec->anchor->family_num;
1946
1947 if (first_family != sec_family)
1948 return 3;
1949
1950 }
1951
1952
1953 /* if 'first' is a spare promote it to a populated mpb with sec's
1954 * family number
1955 */
1956 if (first->anchor->num_raid_devs == 0 &&
1957 sec->anchor->num_raid_devs > 0) {
1958 int i;
1959 struct intel_dev *dv;
1960 struct imsm_dev *dev;
1961
1962 /* we need to copy raid device info from sec if an allocation
1963 * fails here we don't associate the spare
1964 */
1965 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
1966 dv = malloc(sizeof(*dv));
1967 if (!dv)
1968 break;
1969 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1970 if (!dev) {
1971 free(dv);
1972 break;
1973 }
1974 dv->dev = dev;
1975 dv->index = i;
1976 dv->next = first->devlist;
1977 first->devlist = dv;
1978 }
1979 if (i < sec->anchor->num_raid_devs) {
1980 /* allocation failure */
1981 free_devlist(first);
1982 fprintf(stderr, "imsm: failed to associate spare\n");
1983 return 3;
1984 }
1985 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
1986 first->anchor->orig_family_num = sec->anchor->orig_family_num;
1987 first->anchor->family_num = sec->anchor->family_num;
1988 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
1989 for (i = 0; i < sec->anchor->num_raid_devs; i++)
1990 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
1991 }
1992
1993 return 0;
1994 }
1995
1996 static void fd2devname(int fd, char *name)
1997 {
1998 struct stat st;
1999 char path[256];
2000 char dname[PATH_MAX];
2001 char *nm;
2002 int rv;
2003
2004 name[0] = '\0';
2005 if (fstat(fd, &st) != 0)
2006 return;
2007 sprintf(path, "/sys/dev/block/%d:%d",
2008 major(st.st_rdev), minor(st.st_rdev));
2009
2010 rv = readlink(path, dname, sizeof(dname));
2011 if (rv <= 0)
2012 return;
2013
2014 dname[rv] = '\0';
2015 nm = strrchr(dname, '/');
2016 nm++;
2017 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
2018 }
2019
2020 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
2021
2022 static int imsm_read_serial(int fd, char *devname,
2023 __u8 serial[MAX_RAID_SERIAL_LEN])
2024 {
2025 unsigned char scsi_serial[255];
2026 int rv;
2027 int rsp_len;
2028 int len;
2029 char *dest;
2030 char *src;
2031 char *rsp_buf;
2032 int i;
2033
2034 memset(scsi_serial, 0, sizeof(scsi_serial));
2035
2036 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
2037
2038 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
2039 memset(serial, 0, MAX_RAID_SERIAL_LEN);
2040 fd2devname(fd, (char *) serial);
2041 return 0;
2042 }
2043
2044 if (rv != 0) {
2045 if (devname)
2046 fprintf(stderr,
2047 Name ": Failed to retrieve serial for %s\n",
2048 devname);
2049 return rv;
2050 }
2051
2052 rsp_len = scsi_serial[3];
2053 if (!rsp_len) {
2054 if (devname)
2055 fprintf(stderr,
2056 Name ": Failed to retrieve serial for %s\n",
2057 devname);
2058 return 2;
2059 }
2060 rsp_buf = (char *) &scsi_serial[4];
2061
2062 /* trim all whitespace and non-printable characters and convert
2063 * ':' to ';'
2064 */
2065 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
2066 src = &rsp_buf[i];
2067 if (*src > 0x20) {
2068 /* ':' is reserved for use in placeholder serial
2069 * numbers for missing disks
2070 */
2071 if (*src == ':')
2072 *dest++ = ';';
2073 else
2074 *dest++ = *src;
2075 }
2076 }
2077 len = dest - rsp_buf;
2078 dest = rsp_buf;
2079
2080 /* truncate leading characters */
2081 if (len > MAX_RAID_SERIAL_LEN) {
2082 dest += len - MAX_RAID_SERIAL_LEN;
2083 len = MAX_RAID_SERIAL_LEN;
2084 }
2085
2086 memset(serial, 0, MAX_RAID_SERIAL_LEN);
2087 memcpy(serial, dest, len);
2088
2089 return 0;
2090 }
2091
2092 static int serialcmp(__u8 *s1, __u8 *s2)
2093 {
2094 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
2095 }
2096
2097 static void serialcpy(__u8 *dest, __u8 *src)
2098 {
2099 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
2100 }
2101
2102 #ifndef MDASSEMBLE
2103 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
2104 {
2105 struct dl *dl;
2106
2107 for (dl = super->disks; dl; dl = dl->next)
2108 if (serialcmp(dl->serial, serial) == 0)
2109 break;
2110
2111 return dl;
2112 }
2113 #endif
2114
2115 static struct imsm_disk *
2116 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
2117 {
2118 int i;
2119
2120 for (i = 0; i < mpb->num_disks; i++) {
2121 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2122
2123 if (serialcmp(disk->serial, serial) == 0) {
2124 if (idx)
2125 *idx = i;
2126 return disk;
2127 }
2128 }
2129
2130 return NULL;
2131 }
2132
2133 static int
2134 load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
2135 {
2136 struct imsm_disk *disk;
2137 struct dl *dl;
2138 struct stat stb;
2139 int rv;
2140 char name[40];
2141 __u8 serial[MAX_RAID_SERIAL_LEN];
2142
2143 rv = imsm_read_serial(fd, devname, serial);
2144
2145 if (rv != 0)
2146 return 2;
2147
2148 dl = calloc(1, sizeof(*dl));
2149 if (!dl) {
2150 if (devname)
2151 fprintf(stderr,
2152 Name ": failed to allocate disk buffer for %s\n",
2153 devname);
2154 return 2;
2155 }
2156
2157 fstat(fd, &stb);
2158 dl->major = major(stb.st_rdev);
2159 dl->minor = minor(stb.st_rdev);
2160 dl->next = super->disks;
2161 dl->fd = keep_fd ? fd : -1;
2162 assert(super->disks == NULL);
2163 super->disks = dl;
2164 serialcpy(dl->serial, serial);
2165 dl->index = -2;
2166 dl->e = NULL;
2167 fd2devname(fd, name);
2168 if (devname)
2169 dl->devname = strdup(devname);
2170 else
2171 dl->devname = strdup(name);
2172
2173 /* look up this disk's index in the current anchor */
2174 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
2175 if (disk) {
2176 dl->disk = *disk;
2177 /* only set index on disks that are a member of a
2178 * populated contianer, i.e. one with raid_devs
2179 */
2180 if (is_failed(&dl->disk))
2181 dl->index = -2;
2182 else if (is_spare(&dl->disk))
2183 dl->index = -1;
2184 }
2185
2186 return 0;
2187 }
2188
2189 #ifndef MDASSEMBLE
2190 /* When migrating map0 contains the 'destination' state while map1
2191 * contains the current state. When not migrating map0 contains the
2192 * current state. This routine assumes that map[0].map_state is set to
2193 * the current array state before being called.
2194 *
2195 * Migration is indicated by one of the following states
2196 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
2197 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
2198 * map1state=unitialized)
2199 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
2200 * map1state=normal)
2201 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
2202 * map1state=degraded)
2203 */
2204 static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
2205 {
2206 struct imsm_map *dest;
2207 struct imsm_map *src = get_imsm_map(dev, 0);
2208
2209 dev->vol.migr_state = 1;
2210 set_migr_type(dev, migr_type);
2211 dev->vol.curr_migr_unit = 0;
2212 dest = get_imsm_map(dev, 1);
2213
2214 /* duplicate and then set the target end state in map[0] */
2215 memcpy(dest, src, sizeof_imsm_map(src));
2216 if ((migr_type == MIGR_REBUILD) ||
2217 (migr_type == MIGR_GEN_MIGR)) {
2218 __u32 ord;
2219 int i;
2220
2221 for (i = 0; i < src->num_members; i++) {
2222 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
2223 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
2224 }
2225 }
2226
2227 src->map_state = to_state;
2228 }
2229
2230 static void end_migration(struct imsm_dev *dev, __u8 map_state)
2231 {
2232 struct imsm_map *map = get_imsm_map(dev, 0);
2233 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
2234 int i, j;
2235
2236 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2237 * completed in the last migration.
2238 *
2239 * FIXME add support for raid-level-migration
2240 */
2241 for (i = 0; i < prev->num_members; i++)
2242 for (j = 0; j < map->num_members; j++)
2243 /* during online capacity expansion
2244 * disks position can be changed if takeover is used
2245 */
2246 if (ord_to_idx(map->disk_ord_tbl[j]) ==
2247 ord_to_idx(prev->disk_ord_tbl[i])) {
2248 map->disk_ord_tbl[j] |= prev->disk_ord_tbl[i];
2249 break;
2250 }
2251
2252 dev->vol.migr_state = 0;
2253 dev->vol.migr_type = 0;
2254 dev->vol.curr_migr_unit = 0;
2255 map->map_state = map_state;
2256 }
2257 #endif
2258
2259 static int parse_raid_devices(struct intel_super *super)
2260 {
2261 int i;
2262 struct imsm_dev *dev_new;
2263 size_t len, len_migr;
2264 size_t space_needed = 0;
2265 struct imsm_super *mpb = super->anchor;
2266
2267 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2268 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
2269 struct intel_dev *dv;
2270
2271 len = sizeof_imsm_dev(dev_iter, 0);
2272 len_migr = sizeof_imsm_dev(dev_iter, 1);
2273 if (len_migr > len)
2274 space_needed += len_migr - len;
2275
2276 dv = malloc(sizeof(*dv));
2277 if (!dv)
2278 return 1;
2279 dev_new = malloc(len_migr);
2280 if (!dev_new) {
2281 free(dv);
2282 return 1;
2283 }
2284 imsm_copy_dev(dev_new, dev_iter);
2285 dv->dev = dev_new;
2286 dv->index = i;
2287 dv->next = super->devlist;
2288 super->devlist = dv;
2289 }
2290
2291 /* ensure that super->buf is large enough when all raid devices
2292 * are migrating
2293 */
2294 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
2295 void *buf;
2296
2297 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
2298 if (posix_memalign(&buf, 512, len) != 0)
2299 return 1;
2300
2301 memcpy(buf, super->buf, super->len);
2302 memset(buf + super->len, 0, len - super->len);
2303 free(super->buf);
2304 super->buf = buf;
2305 super->len = len;
2306 }
2307
2308 return 0;
2309 }
2310
2311 /* retrieve a pointer to the bbm log which starts after all raid devices */
2312 struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
2313 {
2314 void *ptr = NULL;
2315
2316 if (__le32_to_cpu(mpb->bbm_log_size)) {
2317 ptr = mpb;
2318 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
2319 }
2320
2321 return ptr;
2322 }
2323
2324 static void __free_imsm(struct intel_super *super, int free_disks);
2325
2326 /* load_imsm_mpb - read matrix metadata
2327 * allocates super->mpb to be freed by free_super
2328 */
2329 static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
2330 {
2331 unsigned long long dsize;
2332 unsigned long long sectors;
2333 struct stat;
2334 struct imsm_super *anchor;
2335 __u32 check_sum;
2336
2337 get_dev_size(fd, NULL, &dsize);
2338 if (dsize < 1024) {
2339 if (devname)
2340 fprintf(stderr,
2341 Name ": %s: device to small for imsm\n",
2342 devname);
2343 return 1;
2344 }
2345
2346 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
2347 if (devname)
2348 fprintf(stderr,
2349 Name ": Cannot seek to anchor block on %s: %s\n",
2350 devname, strerror(errno));
2351 return 1;
2352 }
2353
2354 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
2355 if (devname)
2356 fprintf(stderr,
2357 Name ": Failed to allocate imsm anchor buffer"
2358 " on %s\n", devname);
2359 return 1;
2360 }
2361 if (read(fd, anchor, 512) != 512) {
2362 if (devname)
2363 fprintf(stderr,
2364 Name ": Cannot read anchor block on %s: %s\n",
2365 devname, strerror(errno));
2366 free(anchor);
2367 return 1;
2368 }
2369
2370 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
2371 if (devname)
2372 fprintf(stderr,
2373 Name ": no IMSM anchor on %s\n", devname);
2374 free(anchor);
2375 return 2;
2376 }
2377
2378 __free_imsm(super, 0);
2379 super->len = ROUND_UP(anchor->mpb_size, 512);
2380 if (posix_memalign(&super->buf, 512, super->len) != 0) {
2381 if (devname)
2382 fprintf(stderr,
2383 Name ": unable to allocate %zu byte mpb buffer\n",
2384 super->len);
2385 free(anchor);
2386 return 2;
2387 }
2388 memcpy(super->buf, anchor, 512);
2389
2390 sectors = mpb_sectors(anchor) - 1;
2391 free(anchor);
2392 if (!sectors) {
2393 check_sum = __gen_imsm_checksum(super->anchor);
2394 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2395 if (devname)
2396 fprintf(stderr,
2397 Name ": IMSM checksum %x != %x on %s\n",
2398 check_sum,
2399 __le32_to_cpu(super->anchor->check_sum),
2400 devname);
2401 return 2;
2402 }
2403
2404 return 0;
2405 }
2406
2407 /* read the extended mpb */
2408 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
2409 if (devname)
2410 fprintf(stderr,
2411 Name ": Cannot seek to extended mpb on %s: %s\n",
2412 devname, strerror(errno));
2413 return 1;
2414 }
2415
2416 if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
2417 if (devname)
2418 fprintf(stderr,
2419 Name ": Cannot read extended mpb on %s: %s\n",
2420 devname, strerror(errno));
2421 return 2;
2422 }
2423
2424 check_sum = __gen_imsm_checksum(super->anchor);
2425 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2426 if (devname)
2427 fprintf(stderr,
2428 Name ": IMSM checksum %x != %x on %s\n",
2429 check_sum, __le32_to_cpu(super->anchor->check_sum),
2430 devname);
2431 return 3;
2432 }
2433
2434 /* FIXME the BBM log is disk specific so we cannot use this global
2435 * buffer for all disks. Ok for now since we only look at the global
2436 * bbm_log_size parameter to gate assembly
2437 */
2438 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2439
2440 return 0;
2441 }
2442
2443 static int
2444 load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2445 {
2446 int err;
2447
2448 err = load_imsm_mpb(fd, super, devname);
2449 if (err)
2450 return err;
2451 err = load_imsm_disk(fd, super, devname, keep_fd);
2452 if (err)
2453 return err;
2454 err = parse_raid_devices(super);
2455
2456 return err;
2457 }
2458
2459 static void __free_imsm_disk(struct dl *d)
2460 {
2461 if (d->fd >= 0)
2462 close(d->fd);
2463 if (d->devname)
2464 free(d->devname);
2465 if (d->e)
2466 free(d->e);
2467 free(d);
2468
2469 }
2470
2471 static void free_imsm_disks(struct intel_super *super)
2472 {
2473 struct dl *d;
2474
2475 while (super->disks) {
2476 d = super->disks;
2477 super->disks = d->next;
2478 __free_imsm_disk(d);
2479 }
2480 while (super->missing) {
2481 d = super->missing;
2482 super->missing = d->next;
2483 __free_imsm_disk(d);
2484 }
2485
2486 }
2487
2488 /* free all the pieces hanging off of a super pointer */
2489 static void __free_imsm(struct intel_super *super, int free_disks)
2490 {
2491 if (super->buf) {
2492 free(super->buf);
2493 super->buf = NULL;
2494 }
2495 if (free_disks)
2496 free_imsm_disks(super);
2497 free_devlist(super);
2498 if (super->hba) {
2499 free((void *) super->hba);
2500 super->hba = NULL;
2501 }
2502 }
2503
2504 static void free_imsm(struct intel_super *super)
2505 {
2506 __free_imsm(super, 1);
2507 free(super);
2508 }
2509
2510 static void free_super_imsm(struct supertype *st)
2511 {
2512 struct intel_super *super = st->sb;
2513
2514 if (!super)
2515 return;
2516
2517 free_imsm(super);
2518 st->sb = NULL;
2519 }
2520
2521 static struct intel_super *alloc_super(void)
2522 {
2523 struct intel_super *super = malloc(sizeof(*super));
2524
2525 if (super) {
2526 memset(super, 0, sizeof(*super));
2527 super->current_vol = -1;
2528 super->create_offset = ~((__u32 ) 0);
2529 if (!check_env("IMSM_NO_PLATFORM"))
2530 super->orom = find_imsm_orom();
2531 if (super->orom && !check_env("IMSM_TEST_OROM")) {
2532 struct sys_dev *list, *ent;
2533
2534 /* find the first intel ahci controller */
2535 list = find_driver_devices("pci", "ahci");
2536 for (ent = list; ent; ent = ent->next)
2537 if (devpath_to_vendor(ent->path) == 0x8086)
2538 break;
2539 if (ent) {
2540 super->hba = ent->path;
2541 ent->path = NULL;
2542 }
2543 free_sys_dev(&list);
2544 }
2545 }
2546
2547 return super;
2548 }
2549
2550 #ifndef MDASSEMBLE
2551 /* find_missing - helper routine for load_super_imsm_all that identifies
2552 * disks that have disappeared from the system. This routine relies on
2553 * the mpb being uptodate, which it is at load time.
2554 */
2555 static int find_missing(struct intel_super *super)
2556 {
2557 int i;
2558 struct imsm_super *mpb = super->anchor;
2559 struct dl *dl;
2560 struct imsm_disk *disk;
2561
2562 for (i = 0; i < mpb->num_disks; i++) {
2563 disk = __get_imsm_disk(mpb, i);
2564 dl = serial_to_dl(disk->serial, super);
2565 if (dl)
2566 continue;
2567
2568 dl = malloc(sizeof(*dl));
2569 if (!dl)
2570 return 1;
2571 dl->major = 0;
2572 dl->minor = 0;
2573 dl->fd = -1;
2574 dl->devname = strdup("missing");
2575 dl->index = i;
2576 serialcpy(dl->serial, disk->serial);
2577 dl->disk = *disk;
2578 dl->e = NULL;
2579 dl->next = super->missing;
2580 super->missing = dl;
2581 }
2582
2583 return 0;
2584 }
2585
2586 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2587 {
2588 struct intel_disk *idisk = disk_list;
2589
2590 while (idisk) {
2591 if (serialcmp(idisk->disk.serial, serial) == 0)
2592 break;
2593 idisk = idisk->next;
2594 }
2595
2596 return idisk;
2597 }
2598
2599 static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2600 struct intel_super *super,
2601 struct intel_disk **disk_list)
2602 {
2603 struct imsm_disk *d = &super->disks->disk;
2604 struct imsm_super *mpb = super->anchor;
2605 int i, j;
2606
2607 for (i = 0; i < tbl_size; i++) {
2608 struct imsm_super *tbl_mpb = table[i]->anchor;
2609 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2610
2611 if (tbl_mpb->family_num == mpb->family_num) {
2612 if (tbl_mpb->check_sum == mpb->check_sum) {
2613 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2614 __func__, super->disks->major,
2615 super->disks->minor,
2616 table[i]->disks->major,
2617 table[i]->disks->minor);
2618 break;
2619 }
2620
2621 if (((is_configured(d) && !is_configured(tbl_d)) ||
2622 is_configured(d) == is_configured(tbl_d)) &&
2623 tbl_mpb->generation_num < mpb->generation_num) {
2624 /* current version of the mpb is a
2625 * better candidate than the one in
2626 * super_table, but copy over "cross
2627 * generational" status
2628 */
2629 struct intel_disk *idisk;
2630
2631 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2632 __func__, super->disks->major,
2633 super->disks->minor,
2634 table[i]->disks->major,
2635 table[i]->disks->minor);
2636
2637 idisk = disk_list_get(tbl_d->serial, *disk_list);
2638 if (idisk && is_failed(&idisk->disk))
2639 tbl_d->status |= FAILED_DISK;
2640 break;
2641 } else {
2642 struct intel_disk *idisk;
2643 struct imsm_disk *disk;
2644
2645 /* tbl_mpb is more up to date, but copy
2646 * over cross generational status before
2647 * returning
2648 */
2649 disk = __serial_to_disk(d->serial, mpb, NULL);
2650 if (disk && is_failed(disk))
2651 d->status |= FAILED_DISK;
2652
2653 idisk = disk_list_get(d->serial, *disk_list);
2654 if (idisk) {
2655 idisk->owner = i;
2656 if (disk && is_configured(disk))
2657 idisk->disk.status |= CONFIGURED_DISK;
2658 }
2659
2660 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2661 __func__, super->disks->major,
2662 super->disks->minor,
2663 table[i]->disks->major,
2664 table[i]->disks->minor);
2665
2666 return tbl_size;
2667 }
2668 }
2669 }
2670
2671 if (i >= tbl_size)
2672 table[tbl_size++] = super;
2673 else
2674 table[i] = super;
2675
2676 /* update/extend the merged list of imsm_disk records */
2677 for (j = 0; j < mpb->num_disks; j++) {
2678 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2679 struct intel_disk *idisk;
2680
2681 idisk = disk_list_get(disk->serial, *disk_list);
2682 if (idisk) {
2683 idisk->disk.status |= disk->status;
2684 if (is_configured(&idisk->disk) ||
2685 is_failed(&idisk->disk))
2686 idisk->disk.status &= ~(SPARE_DISK);
2687 } else {
2688 idisk = calloc(1, sizeof(*idisk));
2689 if (!idisk)
2690 return -1;
2691 idisk->owner = IMSM_UNKNOWN_OWNER;
2692 idisk->disk = *disk;
2693 idisk->next = *disk_list;
2694 *disk_list = idisk;
2695 }
2696
2697 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2698 idisk->owner = i;
2699 }
2700
2701 return tbl_size;
2702 }
2703
2704 static struct intel_super *
2705 validate_members(struct intel_super *super, struct intel_disk *disk_list,
2706 const int owner)
2707 {
2708 struct imsm_super *mpb = super->anchor;
2709 int ok_count = 0;
2710 int i;
2711
2712 for (i = 0; i < mpb->num_disks; i++) {
2713 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2714 struct intel_disk *idisk;
2715
2716 idisk = disk_list_get(disk->serial, disk_list);
2717 if (idisk) {
2718 if (idisk->owner == owner ||
2719 idisk->owner == IMSM_UNKNOWN_OWNER)
2720 ok_count++;
2721 else
2722 dprintf("%s: '%.16s' owner %d != %d\n",
2723 __func__, disk->serial, idisk->owner,
2724 owner);
2725 } else {
2726 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2727 __func__, __le32_to_cpu(mpb->family_num), i,
2728 disk->serial);
2729 break;
2730 }
2731 }
2732
2733 if (ok_count == mpb->num_disks)
2734 return super;
2735 return NULL;
2736 }
2737
2738 static void show_conflicts(__u32 family_num, struct intel_super *super_list)
2739 {
2740 struct intel_super *s;
2741
2742 for (s = super_list; s; s = s->next) {
2743 if (family_num != s->anchor->family_num)
2744 continue;
2745 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
2746 __le32_to_cpu(family_num), s->disks->devname);
2747 }
2748 }
2749
2750 static struct intel_super *
2751 imsm_thunderdome(struct intel_super **super_list, int len)
2752 {
2753 struct intel_super *super_table[len];
2754 struct intel_disk *disk_list = NULL;
2755 struct intel_super *champion, *spare;
2756 struct intel_super *s, **del;
2757 int tbl_size = 0;
2758 int conflict;
2759 int i;
2760
2761 memset(super_table, 0, sizeof(super_table));
2762 for (s = *super_list; s; s = s->next)
2763 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
2764
2765 for (i = 0; i < tbl_size; i++) {
2766 struct imsm_disk *d;
2767 struct intel_disk *idisk;
2768 struct imsm_super *mpb = super_table[i]->anchor;
2769
2770 s = super_table[i];
2771 d = &s->disks->disk;
2772
2773 /* 'd' must appear in merged disk list for its
2774 * configuration to be valid
2775 */
2776 idisk = disk_list_get(d->serial, disk_list);
2777 if (idisk && idisk->owner == i)
2778 s = validate_members(s, disk_list, i);
2779 else
2780 s = NULL;
2781
2782 if (!s)
2783 dprintf("%s: marking family: %#x from %d:%d offline\n",
2784 __func__, mpb->family_num,
2785 super_table[i]->disks->major,
2786 super_table[i]->disks->minor);
2787 super_table[i] = s;
2788 }
2789
2790 /* This is where the mdadm implementation differs from the Windows
2791 * driver which has no strict concept of a container. We can only
2792 * assemble one family from a container, so when returning a prodigal
2793 * array member to this system the code will not be able to disambiguate
2794 * the container contents that should be assembled ("foreign" versus
2795 * "local"). It requires user intervention to set the orig_family_num
2796 * to a new value to establish a new container. The Windows driver in
2797 * this situation fixes up the volume name in place and manages the
2798 * foreign array as an independent entity.
2799 */
2800 s = NULL;
2801 spare = NULL;
2802 conflict = 0;
2803 for (i = 0; i < tbl_size; i++) {
2804 struct intel_super *tbl_ent = super_table[i];
2805 int is_spare = 0;
2806
2807 if (!tbl_ent)
2808 continue;
2809
2810 if (tbl_ent->anchor->num_raid_devs == 0) {
2811 spare = tbl_ent;
2812 is_spare = 1;
2813 }
2814
2815 if (s && !is_spare) {
2816 show_conflicts(tbl_ent->anchor->family_num, *super_list);
2817 conflict++;
2818 } else if (!s && !is_spare)
2819 s = tbl_ent;
2820 }
2821
2822 if (!s)
2823 s = spare;
2824 if (!s) {
2825 champion = NULL;
2826 goto out;
2827 }
2828 champion = s;
2829
2830 if (conflict)
2831 fprintf(stderr, "Chose family %#x on '%s', "
2832 "assemble conflicts to new container with '--update=uuid'\n",
2833 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
2834
2835 /* collect all dl's onto 'champion', and update them to
2836 * champion's version of the status
2837 */
2838 for (s = *super_list; s; s = s->next) {
2839 struct imsm_super *mpb = champion->anchor;
2840 struct dl *dl = s->disks;
2841
2842 if (s == champion)
2843 continue;
2844
2845 for (i = 0; i < mpb->num_disks; i++) {
2846 struct imsm_disk *disk;
2847
2848 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
2849 if (disk) {
2850 dl->disk = *disk;
2851 /* only set index on disks that are a member of
2852 * a populated contianer, i.e. one with
2853 * raid_devs
2854 */
2855 if (is_failed(&dl->disk))
2856 dl->index = -2;
2857 else if (is_spare(&dl->disk))
2858 dl->index = -1;
2859 break;
2860 }
2861 }
2862
2863 if (i >= mpb->num_disks) {
2864 struct intel_disk *idisk;
2865
2866 idisk = disk_list_get(dl->serial, disk_list);
2867 if (idisk && is_spare(&idisk->disk) &&
2868 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
2869 dl->index = -1;
2870 else {
2871 dl->index = -2;
2872 continue;
2873 }
2874 }
2875
2876 dl->next = champion->disks;
2877 champion->disks = dl;
2878 s->disks = NULL;
2879 }
2880
2881 /* delete 'champion' from super_list */
2882 for (del = super_list; *del; ) {
2883 if (*del == champion) {
2884 *del = (*del)->next;
2885 break;
2886 } else
2887 del = &(*del)->next;
2888 }
2889 champion->next = NULL;
2890
2891 out:
2892 while (disk_list) {
2893 struct intel_disk *idisk = disk_list;
2894
2895 disk_list = disk_list->next;
2896 free(idisk);
2897 }
2898
2899 return champion;
2900 }
2901
2902 static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
2903 char *devname)
2904 {
2905 struct mdinfo *sra;
2906 struct intel_super *super_list = NULL;
2907 struct intel_super *super = NULL;
2908 int devnum = fd2devnum(fd);
2909 struct mdinfo *sd;
2910 int retry;
2911 int err = 0;
2912 int i;
2913
2914 /* check if 'fd' an opened container */
2915 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
2916 if (!sra)
2917 return 1;
2918
2919 if (sra->array.major_version != -1 ||
2920 sra->array.minor_version != -2 ||
2921 strcmp(sra->text_version, "imsm") != 0) {
2922 err = 1;
2923 goto error;
2924 }
2925 /* load all mpbs */
2926 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
2927 struct intel_super *s = alloc_super();
2928 char nm[32];
2929 int dfd;
2930
2931 err = 1;
2932 if (!s)
2933 goto error;
2934 s->next = super_list;
2935 super_list = s;
2936
2937 err = 2;
2938 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
2939 dfd = dev_open(nm, O_RDWR);
2940 if (dfd < 0)
2941 goto error;
2942
2943 err = load_and_parse_mpb(dfd, s, NULL, 1);
2944
2945 /* retry the load if we might have raced against mdmon */
2946 if (err == 3 && mdmon_running(devnum))
2947 for (retry = 0; retry < 3; retry++) {
2948 usleep(3000);
2949 err = load_and_parse_mpb(dfd, s, NULL, 1);
2950 if (err != 3)
2951 break;
2952 }
2953 if (err)
2954 goto error;
2955 }
2956
2957 /* all mpbs enter, maybe one leaves */
2958 super = imsm_thunderdome(&super_list, i);
2959 if (!super) {
2960 err = 1;
2961 goto error;
2962 }
2963
2964 if (find_missing(super) != 0) {
2965 free_imsm(super);
2966 err = 2;
2967 goto error;
2968 }
2969 err = 0;
2970
2971 error:
2972 while (super_list) {
2973 struct intel_super *s = super_list;
2974
2975 super_list = super_list->next;
2976 free_imsm(s);
2977 }
2978 sysfs_free(sra);
2979
2980 if (err)
2981 return err;
2982
2983 *sbp = super;
2984 st->container_dev = devnum;
2985 if (err == 0 && st->ss == NULL) {
2986 st->ss = &super_imsm;
2987 st->minor_version = 0;
2988 st->max_devs = IMSM_MAX_DEVICES;
2989 }
2990 return 0;
2991 }
2992
2993 static int load_container_imsm(struct supertype *st, int fd, char *devname)
2994 {
2995 return load_super_imsm_all(st, fd, &st->sb, devname);
2996 }
2997 #endif
2998
2999 static int load_super_imsm(struct supertype *st, int fd, char *devname)
3000 {
3001 struct intel_super *super;
3002 int rv;
3003
3004 #ifndef MDASSEMBLE
3005 if (load_super_imsm_all(st, fd, &st->sb, devname) == 0)
3006 return 0;
3007 #endif
3008
3009 if (test_partition(fd))
3010 /* IMSM not allowed on partitions */
3011 return 1;
3012
3013 free_super_imsm(st);
3014
3015 super = alloc_super();
3016 if (!super) {
3017 fprintf(stderr,
3018 Name ": malloc of %zu failed.\n",
3019 sizeof(*super));
3020 return 1;
3021 }
3022
3023 rv = load_and_parse_mpb(fd, super, devname, 0);
3024
3025 if (rv) {
3026 if (devname)
3027 fprintf(stderr,
3028 Name ": Failed to load all information "
3029 "sections on %s\n", devname);
3030 free_imsm(super);
3031 return rv;
3032 }
3033
3034 st->sb = super;
3035 if (st->ss == NULL) {
3036 st->ss = &super_imsm;
3037 st->minor_version = 0;
3038 st->max_devs = IMSM_MAX_DEVICES;
3039 }
3040 return 0;
3041 }
3042
3043 static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
3044 {
3045 if (info->level == 1)
3046 return 128;
3047 return info->chunk_size >> 9;
3048 }
3049
3050 static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
3051 {
3052 __u32 num_stripes;
3053
3054 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
3055 num_stripes /= num_domains;
3056
3057 return num_stripes;
3058 }
3059
3060 static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
3061 {
3062 if (info->level == 1)
3063 return info->size * 2;
3064 else
3065 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
3066 }
3067
3068 static void imsm_update_version_info(struct intel_super *super)
3069 {
3070 /* update the version and attributes */
3071 struct imsm_super *mpb = super->anchor;
3072 char *version;
3073 struct imsm_dev *dev;
3074 struct imsm_map *map;
3075 int i;
3076
3077 for (i = 0; i < mpb->num_raid_devs; i++) {
3078 dev = get_imsm_dev(super, i);
3079 map = get_imsm_map(dev, 0);
3080 if (__le32_to_cpu(dev->size_high) > 0)
3081 mpb->attributes |= MPB_ATTRIB_2TB;
3082
3083 /* FIXME detect when an array spans a port multiplier */
3084 #if 0
3085 mpb->attributes |= MPB_ATTRIB_PM;
3086 #endif
3087
3088 if (mpb->num_raid_devs > 1 ||
3089 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
3090 version = MPB_VERSION_ATTRIBS;
3091 switch (get_imsm_raid_level(map)) {
3092 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
3093 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
3094 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
3095 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
3096 }
3097 } else {
3098 if (map->num_members >= 5)
3099 version = MPB_VERSION_5OR6_DISK_ARRAY;
3100 else if (dev->status == DEV_CLONE_N_GO)
3101 version = MPB_VERSION_CNG;
3102 else if (get_imsm_raid_level(map) == 5)
3103 version = MPB_VERSION_RAID5;
3104 else if (map->num_members >= 3)
3105 version = MPB_VERSION_3OR4_DISK_ARRAY;
3106 else if (get_imsm_raid_level(map) == 1)
3107 version = MPB_VERSION_RAID1;
3108 else
3109 version = MPB_VERSION_RAID0;
3110 }
3111 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
3112 }
3113 }
3114
3115 static int check_name(struct intel_super *super, char *name, int quiet)
3116 {
3117 struct imsm_super *mpb = super->anchor;
3118 char *reason = NULL;
3119 int i;
3120
3121 if (strlen(name) > MAX_RAID_SERIAL_LEN)
3122 reason = "must be 16 characters or less";
3123
3124 for (i = 0; i < mpb->num_raid_devs; i++) {
3125 struct imsm_dev *dev = get_imsm_dev(super, i);
3126
3127 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
3128 reason = "already exists";
3129 break;
3130 }
3131 }
3132
3133 if (reason && !quiet)
3134 fprintf(stderr, Name ": imsm volume name %s\n", reason);
3135
3136 return !reason;
3137 }
3138
3139 static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
3140 unsigned long long size, char *name,
3141 char *homehost, int *uuid)
3142 {
3143 /* We are creating a volume inside a pre-existing container.
3144 * so st->sb is already set.
3145 */
3146 struct intel_super *super = st->sb;
3147 struct imsm_super *mpb = super->anchor;
3148 struct intel_dev *dv;
3149 struct imsm_dev *dev;
3150 struct imsm_vol *vol;
3151 struct imsm_map *map;
3152 int idx = mpb->num_raid_devs;
3153 int i;
3154 unsigned long long array_blocks;
3155 size_t size_old, size_new;
3156 __u32 num_data_stripes;
3157
3158 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
3159 fprintf(stderr, Name": This imsm-container already has the "
3160 "maximum of %d volumes\n", super->orom->vpa);
3161 return 0;
3162 }
3163
3164 /* ensure the mpb is large enough for the new data */
3165 size_old = __le32_to_cpu(mpb->mpb_size);
3166 size_new = disks_to_mpb_size(info->nr_disks);
3167 if (size_new > size_old) {
3168 void *mpb_new;
3169 size_t size_round = ROUND_UP(size_new, 512);
3170
3171 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
3172 fprintf(stderr, Name": could not allocate new mpb\n");
3173 return 0;
3174 }
3175 memcpy(mpb_new, mpb, size_old);
3176 free(mpb);
3177 mpb = mpb_new;
3178 super->anchor = mpb_new;
3179 mpb->mpb_size = __cpu_to_le32(size_new);
3180 memset(mpb_new + size_old, 0, size_round - size_old);
3181 }
3182 super->current_vol = idx;
3183 /* when creating the first raid device in this container set num_disks
3184 * to zero, i.e. delete this spare and add raid member devices in
3185 * add_to_super_imsm_volume()
3186 */
3187 if (super->current_vol == 0)
3188 mpb->num_disks = 0;
3189
3190 if (!check_name(super, name, 0))
3191 return 0;
3192 dv = malloc(sizeof(*dv));
3193 if (!dv) {
3194 fprintf(stderr, Name ": failed to allocate device list entry\n");
3195 return 0;
3196 }
3197 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
3198 if (!dev) {
3199 free(dv);
3200 fprintf(stderr, Name": could not allocate raid device\n");
3201 return 0;
3202 }
3203 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
3204 if (info->level == 1)
3205 array_blocks = info_to_blocks_per_member(info);
3206 else
3207 array_blocks = calc_array_size(info->level, info->raid_disks,
3208 info->layout, info->chunk_size,
3209 info->size*2);
3210 /* round array size down to closest MB */
3211 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
3212
3213 dev->size_low = __cpu_to_le32((__u32) array_blocks);
3214 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
3215 dev->status = __cpu_to_le32(0);
3216 dev->reserved_blocks = __cpu_to_le32(0);
3217 vol = &dev->vol;
3218 vol->migr_state = 0;
3219 set_migr_type(dev, MIGR_INIT);
3220 vol->dirty = 0;
3221 vol->curr_migr_unit = 0;
3222 map = get_imsm_map(dev, 0);
3223 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
3224 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
3225 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
3226 map->failed_disk_num = ~0;
3227 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
3228 IMSM_T_STATE_NORMAL;
3229 map->ddf = 1;
3230
3231 if (info->level == 1 && info->raid_disks > 2) {
3232 free(dev);
3233 free(dv);
3234 fprintf(stderr, Name": imsm does not support more than 2 disks"
3235 "in a raid1 volume\n");
3236 return 0;
3237 }
3238
3239 map->raid_level = info->level;
3240 if (info->level == 10) {
3241 map->raid_level = 1;
3242 map->num_domains = info->raid_disks / 2;
3243 } else if (info->level == 1)
3244 map->num_domains = info->raid_disks;
3245 else
3246 map->num_domains = 1;
3247
3248 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
3249 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
3250
3251 map->num_members = info->raid_disks;
3252 for (i = 0; i < map->num_members; i++) {
3253 /* initialized in add_to_super */
3254 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
3255 }
3256 mpb->num_raid_devs++;
3257
3258 dv->dev = dev;
3259 dv->index = super->current_vol;
3260 dv->next = super->devlist;
3261 super->devlist = dv;
3262
3263 imsm_update_version_info(super);
3264
3265 return 1;
3266 }
3267
3268 static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
3269 unsigned long long size, char *name,
3270 char *homehost, int *uuid)
3271 {
3272 /* This is primarily called by Create when creating a new array.
3273 * We will then get add_to_super called for each component, and then
3274 * write_init_super called to write it out to each device.
3275 * For IMSM, Create can create on fresh devices or on a pre-existing
3276 * array.
3277 * To create on a pre-existing array a different method will be called.
3278 * This one is just for fresh drives.
3279 */
3280 struct intel_super *super;
3281 struct imsm_super *mpb;
3282 size_t mpb_size;
3283 char *version;
3284
3285 if (st->sb)
3286 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
3287
3288 if (info)
3289 mpb_size = disks_to_mpb_size(info->nr_disks);
3290 else
3291 mpb_size = 512;
3292
3293 super = alloc_super();
3294 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
3295 free(super);
3296 super = NULL;
3297 }
3298 if (!super) {
3299 fprintf(stderr, Name
3300 ": %s could not allocate superblock\n", __func__);
3301 return 0;
3302 }
3303 memset(super->buf, 0, mpb_size);
3304 mpb = super->buf;
3305 mpb->mpb_size = __cpu_to_le32(mpb_size);
3306 st->sb = super;
3307
3308 if (info == NULL) {
3309 /* zeroing superblock */
3310 return 0;
3311 }
3312
3313 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3314
3315 version = (char *) mpb->sig;
3316 strcpy(version, MPB_SIGNATURE);
3317 version += strlen(MPB_SIGNATURE);
3318 strcpy(version, MPB_VERSION_RAID0);
3319
3320 return 1;
3321 }
3322
3323 #ifndef MDASSEMBLE
3324 static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
3325 int fd, char *devname)
3326 {
3327 struct intel_super *super = st->sb;
3328 struct imsm_super *mpb = super->anchor;
3329 struct dl *dl;
3330 struct imsm_dev *dev;
3331 struct imsm_map *map;
3332 int slot;
3333
3334 dev = get_imsm_dev(super, super->current_vol);
3335 map = get_imsm_map(dev, 0);
3336
3337 if (! (dk->state & (1<<MD_DISK_SYNC))) {
3338 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
3339 devname);
3340 return 1;
3341 }
3342
3343 if (fd == -1) {
3344 /* we're doing autolayout so grab the pre-marked (in
3345 * validate_geometry) raid_disk
3346 */
3347 for (dl = super->disks; dl; dl = dl->next)
3348 if (dl->raiddisk == dk->raid_disk)
3349 break;
3350 } else {
3351 for (dl = super->disks; dl ; dl = dl->next)
3352 if (dl->major == dk->major &&
3353 dl->minor == dk->minor)
3354 break;
3355 }
3356
3357 if (!dl) {
3358 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
3359 return 1;
3360 }
3361
3362 /* add a pristine spare to the metadata */
3363 if (dl->index < 0) {
3364 dl->index = super->anchor->num_disks;
3365 super->anchor->num_disks++;
3366 }
3367 /* Check the device has not already been added */
3368 slot = get_imsm_disk_slot(map, dl->index);
3369 if (slot >= 0 &&
3370 (get_imsm_ord_tbl_ent(dev, slot) & IMSM_ORD_REBUILD) == 0) {
3371 fprintf(stderr, Name ": %s has been included in this array twice\n",
3372 devname);
3373 return 1;
3374 }
3375 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
3376 dl->disk.status = CONFIGURED_DISK;
3377
3378 /* if we are creating the first raid device update the family number */
3379 if (super->current_vol == 0) {
3380 __u32 sum;
3381 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
3382 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
3383
3384 if (!_dev || !_disk) {
3385 fprintf(stderr, Name ": BUG mpb setup error\n");
3386 return 1;
3387 }
3388 *_dev = *dev;
3389 *_disk = dl->disk;
3390 sum = random32();
3391 sum += __gen_imsm_checksum(mpb);
3392 mpb->family_num = __cpu_to_le32(sum);
3393 mpb->orig_family_num = mpb->family_num;
3394 }
3395
3396 return 0;
3397 }
3398
3399 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
3400 int fd, char *devname)
3401 {
3402 struct intel_super *super = st->sb;
3403 struct dl *dd;
3404 unsigned long long size;
3405 __u32 id;
3406 int rv;
3407 struct stat stb;
3408
3409 /* if we are on an RAID enabled platform check that the disk is
3410 * attached to the raid controller
3411 */
3412 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
3413 fprintf(stderr,
3414 Name ": %s is not attached to the raid controller: %s\n",
3415 devname ? : "disk", super->hba);
3416 return 1;
3417 }
3418
3419 if (super->current_vol >= 0)
3420 return add_to_super_imsm_volume(st, dk, fd, devname);
3421
3422 fstat(fd, &stb);
3423 dd = malloc(sizeof(*dd));
3424 if (!dd) {
3425 fprintf(stderr,
3426 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3427 return 1;
3428 }
3429 memset(dd, 0, sizeof(*dd));
3430 dd->major = major(stb.st_rdev);
3431 dd->minor = minor(stb.st_rdev);
3432 dd->index = -1;
3433 dd->devname = devname ? strdup(devname) : NULL;
3434 dd->fd = fd;
3435 dd->e = NULL;
3436 dd->action = DISK_ADD;
3437 rv = imsm_read_serial(fd, devname, dd->serial);
3438 if (rv) {
3439 fprintf(stderr,
3440 Name ": failed to retrieve scsi serial, aborting\n");
3441 free(dd);
3442 abort();
3443 }
3444
3445 get_dev_size(fd, NULL, &size);
3446 size /= 512;
3447 serialcpy(dd->disk.serial, dd->serial);
3448 dd->disk.total_blocks = __cpu_to_le32(size);
3449 dd->disk.status = SPARE_DISK;
3450 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
3451 dd->disk.scsi_id = __cpu_to_le32(id);
3452 else
3453 dd->disk.scsi_id = __cpu_to_le32(0);
3454
3455 if (st->update_tail) {
3456 dd->next = super->disk_mgmt_list;
3457 super->disk_mgmt_list = dd;
3458 } else {
3459 dd->next = super->disks;
3460 super->disks = dd;
3461 }
3462
3463 return 0;
3464 }
3465
3466
3467 static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
3468 {
3469 struct intel_super *super = st->sb;
3470 struct dl *dd;
3471
3472 /* remove from super works only in mdmon - for communication
3473 * manager - monitor. Check if communication memory buffer
3474 * is prepared.
3475 */
3476 if (!st->update_tail) {
3477 fprintf(stderr,
3478 Name ": %s shall be used in mdmon context only"
3479 "(line %d).\n", __func__, __LINE__);
3480 return 1;
3481 }
3482 dd = malloc(sizeof(*dd));
3483 if (!dd) {
3484 fprintf(stderr,
3485 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3486 return 1;
3487 }
3488 memset(dd, 0, sizeof(*dd));
3489 dd->major = dk->major;
3490 dd->minor = dk->minor;
3491 dd->index = -1;
3492 dd->fd = -1;
3493 dd->disk.status = SPARE_DISK;
3494 dd->action = DISK_REMOVE;
3495
3496 dd->next = super->disk_mgmt_list;
3497 super->disk_mgmt_list = dd;
3498
3499
3500 return 0;
3501 }
3502
3503 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3504
3505 static union {
3506 char buf[512];
3507 struct imsm_super anchor;
3508 } spare_record __attribute__ ((aligned(512)));
3509
3510 /* spare records have their own family number and do not have any defined raid
3511 * devices
3512 */
3513 static int write_super_imsm_spares(struct intel_super *super, int doclose)
3514 {
3515 struct imsm_super *mpb = super->anchor;
3516 struct imsm_super *spare = &spare_record.anchor;
3517 __u32 sum;
3518 struct dl *d;
3519
3520 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3521 spare->generation_num = __cpu_to_le32(1UL),
3522 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3523 spare->num_disks = 1,
3524 spare->num_raid_devs = 0,
3525 spare->cache_size = mpb->cache_size,
3526 spare->pwr_cycle_count = __cpu_to_le32(1),
3527
3528 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3529 MPB_SIGNATURE MPB_VERSION_RAID0);
3530
3531 for (d = super->disks; d; d = d->next) {
3532 if (d->index != -1)
3533 continue;
3534
3535 spare->disk[0] = d->disk;
3536 sum = __gen_imsm_checksum(spare);
3537 spare->family_num = __cpu_to_le32(sum);
3538 spare->orig_family_num = 0;
3539 sum = __gen_imsm_checksum(spare);
3540 spare->check_sum = __cpu_to_le32(sum);
3541
3542 if (store_imsm_mpb(d->fd, spare)) {
3543 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3544 __func__, d->major, d->minor, strerror(errno));
3545 return 1;
3546 }
3547 if (doclose) {
3548 close(d->fd);
3549 d->fd = -1;
3550 }
3551 }
3552
3553 return 0;
3554 }
3555
3556 static int write_super_imsm(struct supertype *st, int doclose)
3557 {
3558 struct intel_super *super = st->sb;
3559 struct imsm_super *mpb = super->anchor;
3560 struct dl *d;
3561 __u32 generation;
3562 __u32 sum;
3563 int spares = 0;
3564 int i;
3565 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
3566 int num_disks = 0;
3567
3568 /* 'generation' is incremented everytime the metadata is written */
3569 generation = __le32_to_cpu(mpb->generation_num);
3570 generation++;
3571 mpb->generation_num = __cpu_to_le32(generation);
3572
3573 /* fix up cases where previous mdadm releases failed to set
3574 * orig_family_num
3575 */
3576 if (mpb->orig_family_num == 0)
3577 mpb->orig_family_num = mpb->family_num;
3578
3579 for (d = super->disks; d; d = d->next) {
3580 if (d->index == -1)
3581 spares++;
3582 else {
3583 mpb->disk[d->index] = d->disk;
3584 num_disks++;
3585 }
3586 }
3587 for (d = super->missing; d; d = d->next) {
3588 mpb->disk[d->index] = d->disk;
3589 num_disks++;
3590 }
3591 mpb->num_disks = num_disks;
3592 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
3593
3594 for (i = 0; i < mpb->num_raid_devs; i++) {
3595 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
3596 struct imsm_dev *dev2 = get_imsm_dev(super, i);
3597 if (dev && dev2) {
3598 imsm_copy_dev(dev, dev2);
3599 mpb_size += sizeof_imsm_dev(dev, 0);
3600 }
3601 }
3602 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3603 mpb->mpb_size = __cpu_to_le32(mpb_size);
3604
3605 /* recalculate checksum */
3606 sum = __gen_imsm_checksum(mpb);
3607 mpb->check_sum = __cpu_to_le32(sum);
3608
3609 /* write the mpb for disks that compose raid devices */
3610 for (d = super->disks; d ; d = d->next) {
3611 if (d->index < 0)
3612 continue;
3613 if (store_imsm_mpb(d->fd, mpb))
3614 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3615 __func__, d->major, d->minor, strerror(errno));
3616 if (doclose) {
3617 close(d->fd);
3618 d->fd = -1;
3619 }
3620 }
3621
3622 if (spares)
3623 return write_super_imsm_spares(super, doclose);
3624
3625 return 0;
3626 }
3627
3628
3629 static int create_array(struct supertype *st, int dev_idx)
3630 {
3631 size_t len;
3632 struct imsm_update_create_array *u;
3633 struct intel_super *super = st->sb;
3634 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
3635 struct imsm_map *map = get_imsm_map(dev, 0);
3636 struct disk_info *inf;
3637 struct imsm_disk *disk;
3638 int i;
3639
3640 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3641 sizeof(*inf) * map->num_members;
3642 u = malloc(len);
3643 if (!u) {
3644 fprintf(stderr, "%s: failed to allocate update buffer\n",
3645 __func__);
3646 return 1;
3647 }
3648
3649 u->type = update_create_array;
3650 u->dev_idx = dev_idx;
3651 imsm_copy_dev(&u->dev, dev);
3652 inf = get_disk_info(u);
3653 for (i = 0; i < map->num_members; i++) {
3654 int idx = get_imsm_disk_idx(dev, i);
3655
3656 disk = get_imsm_disk(super, idx);
3657 serialcpy(inf[i].serial, disk->serial);
3658 }
3659 append_metadata_update(st, u, len);
3660
3661 return 0;
3662 }
3663
3664 static int mgmt_disk(struct supertype *st)
3665 {
3666 struct intel_super *super = st->sb;
3667 size_t len;
3668 struct imsm_update_add_remove_disk *u;
3669
3670 if (!super->disk_mgmt_list)
3671 return 0;
3672
3673 len = sizeof(*u);
3674 u = malloc(len);
3675 if (!u) {
3676 fprintf(stderr, "%s: failed to allocate update buffer\n",
3677 __func__);
3678 return 1;
3679 }
3680
3681 u->type = update_add_remove_disk;
3682 append_metadata_update(st, u, len);
3683
3684 return 0;
3685 }
3686
3687 static int write_init_super_imsm(struct supertype *st)
3688 {
3689 struct intel_super *super = st->sb;
3690 int current_vol = super->current_vol;
3691
3692 /* we are done with current_vol reset it to point st at the container */
3693 super->current_vol = -1;
3694
3695 if (st->update_tail) {
3696 /* queue the recently created array / added disk
3697 * as a metadata update */
3698 struct dl *d;
3699 int rv;
3700
3701 /* determine if we are creating a volume or adding a disk */
3702 if (current_vol < 0) {
3703 /* in the mgmt (add/remove) disk case we are running
3704 * in mdmon context, so don't close fd's
3705 */
3706 return mgmt_disk(st);
3707 } else
3708 rv = create_array(st, current_vol);
3709
3710 for (d = super->disks; d ; d = d->next) {
3711 close(d->fd);
3712 d->fd = -1;
3713 }
3714
3715 return rv;
3716 } else {
3717 struct dl *d;
3718 for (d = super->disks; d; d = d->next)
3719 Kill(d->devname, NULL, 0, 1, 1);
3720 return write_super_imsm(st, 1);
3721 }
3722 }
3723 #endif
3724
3725 static int store_super_imsm(struct supertype *st, int fd)
3726 {
3727 struct intel_super *super = st->sb;
3728 struct imsm_super *mpb = super ? super->anchor : NULL;
3729
3730 if (!mpb)
3731 return 1;
3732
3733 #ifndef MDASSEMBLE
3734 return store_imsm_mpb(fd, mpb);
3735 #else
3736 return 1;
3737 #endif
3738 }
3739
3740 static int imsm_bbm_log_size(struct imsm_super *mpb)
3741 {
3742 return __le32_to_cpu(mpb->bbm_log_size);
3743 }
3744
3745 #ifndef MDASSEMBLE
3746 static int validate_geometry_imsm_container(struct supertype *st, int level,
3747 int layout, int raiddisks, int chunk,
3748 unsigned long long size, char *dev,
3749 unsigned long long *freesize,
3750 int verbose)
3751 {
3752 int fd;
3753 unsigned long long ldsize;
3754 const struct imsm_orom *orom;
3755
3756 if (level != LEVEL_CONTAINER)
3757 return 0;
3758 if (!dev)
3759 return 1;
3760
3761 if (check_env("IMSM_NO_PLATFORM"))
3762 orom = NULL;
3763 else
3764 orom = find_imsm_orom();
3765 if (orom && raiddisks > orom->tds) {
3766 if (verbose)
3767 fprintf(stderr, Name ": %d exceeds maximum number of"
3768 " platform supported disks: %d\n",
3769 raiddisks, orom->tds);
3770 return 0;
3771 }
3772
3773 fd = open(dev, O_RDONLY|O_EXCL, 0);
3774 if (fd < 0) {
3775 if (verbose)
3776 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
3777 dev, strerror(errno));
3778 return 0;
3779 }
3780 if (!get_dev_size(fd, dev, &ldsize)) {
3781 close(fd);
3782 return 0;
3783 }
3784 close(fd);
3785
3786 *freesize = avail_size_imsm(st, ldsize >> 9);
3787
3788 return 1;
3789 }
3790
3791 static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
3792 {
3793 const unsigned long long base_start = e[*idx].start;
3794 unsigned long long end = base_start + e[*idx].size;
3795 int i;
3796
3797 if (base_start == end)
3798 return 0;
3799
3800 *idx = *idx + 1;
3801 for (i = *idx; i < num_extents; i++) {
3802 /* extend overlapping extents */
3803 if (e[i].start >= base_start &&
3804 e[i].start <= end) {
3805 if (e[i].size == 0)
3806 return 0;
3807 if (e[i].start + e[i].size > end)
3808 end = e[i].start + e[i].size;
3809 } else if (e[i].start > end) {
3810 *idx = i;
3811 break;
3812 }
3813 }
3814
3815 return end - base_start;
3816 }
3817
3818 static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
3819 {
3820 /* build a composite disk with all known extents and generate a new
3821 * 'maxsize' given the "all disks in an array must share a common start
3822 * offset" constraint
3823 */
3824 struct extent *e = calloc(sum_extents, sizeof(*e));
3825 struct dl *dl;
3826 int i, j;
3827 int start_extent;
3828 unsigned long long pos;
3829 unsigned long long start = 0;
3830 unsigned long long maxsize;
3831 unsigned long reserve;
3832
3833 if (!e)
3834 return 0;
3835
3836 /* coalesce and sort all extents. also, check to see if we need to
3837 * reserve space between member arrays
3838 */
3839 j = 0;
3840 for (dl = super->disks; dl; dl = dl->next) {
3841 if (!dl->e)
3842 continue;
3843 for (i = 0; i < dl->extent_cnt; i++)
3844 e[j++] = dl->e[i];
3845 }
3846 qsort(e, sum_extents, sizeof(*e), cmp_extent);
3847
3848 /* merge extents */
3849 i = 0;
3850 j = 0;
3851 while (i < sum_extents) {
3852 e[j].start = e[i].start;
3853 e[j].size = find_size(e, &i, sum_extents);
3854 j++;
3855 if (e[j-1].size == 0)
3856 break;
3857 }
3858
3859 pos = 0;
3860 maxsize = 0;
3861 start_extent = 0;
3862 i = 0;
3863 do {
3864 unsigned long long esize;
3865
3866 esize = e[i].start - pos;
3867 if (esize >= maxsize) {
3868 maxsize = esize;
3869 start = pos;
3870 start_extent = i;
3871 }
3872 pos = e[i].start + e[i].size;
3873 i++;
3874 } while (e[i-1].size);
3875 free(e);
3876
3877 if (maxsize == 0)
3878 return 0;
3879
3880 /* FIXME assumes volume at offset 0 is the first volume in a
3881 * container
3882 */
3883 if (start_extent > 0)
3884 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
3885 else
3886 reserve = 0;
3887
3888 if (maxsize < reserve)
3889 return 0;
3890
3891 super->create_offset = ~((__u32) 0);
3892 if (start + reserve > super->create_offset)
3893 return 0; /* start overflows create_offset */
3894 super->create_offset = start + reserve;
3895
3896 return maxsize - reserve;
3897 }
3898
3899 static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
3900 {
3901 if (level < 0 || level == 6 || level == 4)
3902 return 0;
3903
3904 /* if we have an orom prevent invalid raid levels */
3905 if (orom)
3906 switch (level) {
3907 case 0: return imsm_orom_has_raid0(orom);
3908 case 1:
3909 if (raiddisks > 2)
3910 return imsm_orom_has_raid1e(orom);
3911 return imsm_orom_has_raid1(orom) && raiddisks == 2;
3912 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
3913 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
3914 }
3915 else
3916 return 1; /* not on an Intel RAID platform so anything goes */
3917
3918 return 0;
3919 }
3920
3921 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
3922 static int
3923 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
3924 int raiddisks, int chunk, int verbose)
3925 {
3926 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3927 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3928 level, raiddisks, raiddisks > 1 ? "s" : "");
3929 return 0;
3930 }
3931 if (super->orom && level != 1 &&
3932 !imsm_orom_has_chunk(super->orom, chunk)) {
3933 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3934 return 0;
3935 }
3936 if (layout != imsm_level_to_layout(level)) {
3937 if (level == 5)
3938 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3939 else if (level == 10)
3940 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3941 else
3942 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3943 layout, level);
3944 return 0;
3945 }
3946
3947 return 1;
3948 }
3949
3950 /* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3951 * FIX ME add ahci details
3952 */
3953 static int validate_geometry_imsm_volume(struct supertype *st, int level,
3954 int layout, int raiddisks, int chunk,
3955 unsigned long long size, char *dev,
3956 unsigned long long *freesize,
3957 int verbose)
3958 {
3959 struct stat stb;
3960 struct intel_super *super = st->sb;
3961 struct imsm_super *mpb = super->anchor;
3962 struct dl *dl;
3963 unsigned long long pos = 0;
3964 unsigned long long maxsize;
3965 struct extent *e;
3966 int i;
3967
3968 /* We must have the container info already read in. */
3969 if (!super)
3970 return 0;
3971
3972 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose))
3973 return 0;
3974
3975 if (!dev) {
3976 /* General test: make sure there is space for
3977 * 'raiddisks' device extents of size 'size' at a given
3978 * offset
3979 */
3980 unsigned long long minsize = size;
3981 unsigned long long start_offset = MaxSector;
3982 int dcnt = 0;
3983 if (minsize == 0)
3984 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
3985 for (dl = super->disks; dl ; dl = dl->next) {
3986 int found = 0;
3987
3988 pos = 0;
3989 i = 0;
3990 e = get_extents(super, dl);
3991 if (!e) continue;
3992 do {
3993 unsigned long long esize;
3994 esize = e[i].start - pos;
3995 if (esize >= minsize)
3996 found = 1;
3997 if (found && start_offset == MaxSector) {
3998 start_offset = pos;
3999 break;
4000 } else if (found && pos != start_offset) {
4001 found = 0;
4002 break;
4003 }
4004 pos = e[i].start + e[i].size;
4005 i++;
4006 } while (e[i-1].size);
4007 if (found)
4008 dcnt++;
4009 free(e);
4010 }
4011 if (dcnt < raiddisks) {
4012 if (verbose)
4013 fprintf(stderr, Name ": imsm: Not enough "
4014 "devices with space for this array "
4015 "(%d < %d)\n",
4016 dcnt, raiddisks);
4017 return 0;
4018 }
4019 return 1;
4020 }
4021
4022 /* This device must be a member of the set */
4023 if (stat(dev, &stb) < 0)
4024 return 0;
4025 if ((S_IFMT & stb.st_mode) != S_IFBLK)
4026 return 0;
4027 for (dl = super->disks ; dl ; dl = dl->next) {
4028 if (dl->major == (int)major(stb.st_rdev) &&
4029 dl->minor == (int)minor(stb.st_rdev))
4030 break;
4031 }
4032 if (!dl) {
4033 if (verbose)
4034 fprintf(stderr, Name ": %s is not in the "
4035 "same imsm set\n", dev);
4036 return 0;
4037 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
4038 /* If a volume is present then the current creation attempt
4039 * cannot incorporate new spares because the orom may not
4040 * understand this configuration (all member disks must be
4041 * members of each array in the container).
4042 */
4043 fprintf(stderr, Name ": %s is a spare and a volume"
4044 " is already defined for this container\n", dev);
4045 fprintf(stderr, Name ": The option-rom requires all member"
4046 " disks to be a member of all volumes\n");
4047 return 0;
4048 }
4049
4050 /* retrieve the largest free space block */
4051 e = get_extents(super, dl);
4052 maxsize = 0;
4053 i = 0;
4054 if (e) {
4055 do {
4056 unsigned long long esize;
4057
4058 esize = e[i].start - pos;
4059 if (esize >= maxsize)
4060 maxsize = esize;
4061 pos = e[i].start + e[i].size;
4062 i++;
4063 } while (e[i-1].size);
4064 dl->e = e;
4065 dl->extent_cnt = i;
4066 } else {
4067 if (verbose)
4068 fprintf(stderr, Name ": unable to determine free space for: %s\n",
4069 dev);
4070 return 0;
4071 }
4072 if (maxsize < size) {
4073 if (verbose)
4074 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
4075 dev, maxsize, size);
4076 return 0;
4077 }
4078
4079 /* count total number of extents for merge */
4080 i = 0;
4081 for (dl = super->disks; dl; dl = dl->next)
4082 if (dl->e)
4083 i += dl->extent_cnt;
4084
4085 maxsize = merge_extents(super, i);
4086 if (maxsize < size || maxsize == 0) {
4087 if (verbose)
4088 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
4089 maxsize, size);
4090 return 0;
4091 }
4092
4093 *freesize = maxsize;
4094
4095 return 1;
4096 }
4097
4098 static int reserve_space(struct supertype *st, int raiddisks,
4099 unsigned long long size, int chunk,
4100 unsigned long long *freesize)
4101 {
4102 struct intel_super *super = st->sb;
4103 struct imsm_super *mpb = super->anchor;
4104 struct dl *dl;
4105 int i;
4106 int extent_cnt;
4107 struct extent *e;
4108 unsigned long long maxsize;
4109 unsigned long long minsize;
4110 int cnt;
4111 int used;
4112
4113 /* find the largest common start free region of the possible disks */
4114 used = 0;
4115 extent_cnt = 0;
4116 cnt = 0;
4117 for (dl = super->disks; dl; dl = dl->next) {
4118 dl->raiddisk = -1;
4119
4120 if (dl->index >= 0)
4121 used++;
4122
4123 /* don't activate new spares if we are orom constrained
4124 * and there is already a volume active in the container
4125 */
4126 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
4127 continue;
4128
4129 e = get_extents(super, dl);
4130 if (!e)
4131 continue;
4132 for (i = 1; e[i-1].size; i++)
4133 ;
4134 dl->e = e;
4135 dl->extent_cnt = i;
4136 extent_cnt += i;
4137 cnt++;
4138 }
4139
4140 maxsize = merge_extents(super, extent_cnt);
4141 minsize = size;
4142 if (size == 0)
4143 minsize = chunk;
4144
4145 if (cnt < raiddisks ||
4146 (super->orom && used && used != raiddisks) ||
4147 maxsize < minsize ||
4148 maxsize == 0) {
4149 fprintf(stderr, Name ": not enough devices with space to create array.\n");
4150 return 0; /* No enough free spaces large enough */
4151 }
4152
4153 if (size == 0) {
4154 size = maxsize;
4155 if (chunk) {
4156 size /= chunk;
4157 size *= chunk;
4158 }
4159 }
4160
4161 cnt = 0;
4162 for (dl = super->disks; dl; dl = dl->next)
4163 if (dl->e)
4164 dl->raiddisk = cnt++;
4165
4166 *freesize = size;
4167
4168 return 1;
4169 }
4170
4171 static int validate_geometry_imsm(struct supertype *st, int level, int layout,
4172 int raiddisks, int chunk, unsigned long long size,
4173 char *dev, unsigned long long *freesize,
4174 int verbose)
4175 {
4176 int fd, cfd;
4177 struct mdinfo *sra;
4178 int is_member = 0;
4179
4180 /* if given unused devices create a container
4181 * if given given devices in a container create a member volume
4182 */
4183 if (level == LEVEL_CONTAINER) {
4184 /* Must be a fresh device to add to a container */
4185 return validate_geometry_imsm_container(st, level, layout,
4186 raiddisks, chunk, size,
4187 dev, freesize,
4188 verbose);
4189 }
4190
4191 if (!dev) {
4192 if (st->sb && freesize) {
4193 /* we are being asked to automatically layout a
4194 * new volume based on the current contents of
4195 * the container. If the the parameters can be
4196 * satisfied reserve_space will record the disks,
4197 * start offset, and size of the volume to be
4198 * created. add_to_super and getinfo_super
4199 * detect when autolayout is in progress.
4200 */
4201 if (!validate_geometry_imsm_orom(st->sb, level, layout,
4202 raiddisks, chunk,
4203 verbose))
4204 return 0;
4205 return reserve_space(st, raiddisks, size, chunk, freesize);
4206 }
4207 return 1;
4208 }
4209 if (st->sb) {
4210 /* creating in a given container */
4211 return validate_geometry_imsm_volume(st, level, layout,
4212 raiddisks, chunk, size,
4213 dev, freesize, verbose);
4214 }
4215
4216 /* This device needs to be a device in an 'imsm' container */
4217 fd = open(dev, O_RDONLY|O_EXCL, 0);
4218 if (fd >= 0) {
4219 if (verbose)
4220 fprintf(stderr,
4221 Name ": Cannot create this array on device %s\n",
4222 dev);
4223 close(fd);
4224 return 0;
4225 }
4226 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
4227 if (verbose)
4228 fprintf(stderr, Name ": Cannot open %s: %s\n",
4229 dev, strerror(errno));
4230 return 0;
4231 }
4232 /* Well, it is in use by someone, maybe an 'imsm' container. */
4233 cfd = open_container(fd);
4234 close(fd);
4235 if (cfd < 0) {
4236 if (verbose)
4237 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
4238 dev);
4239 return 0;
4240 }
4241 sra = sysfs_read(cfd, 0, GET_VERSION);
4242 if (sra && sra->array.major_version == -1 &&
4243 strcmp(sra->text_version, "imsm") == 0)
4244 is_member = 1;
4245 sysfs_free(sra);
4246 if (is_member) {
4247 /* This is a member of a imsm container. Load the container
4248 * and try to create a volume
4249 */
4250 struct intel_super *super;
4251
4252 if (load_super_imsm_all(st, cfd, (void **) &super, NULL) == 0) {
4253 st->sb = super;
4254 st->container_dev = fd2devnum(cfd);
4255 close(cfd);
4256 return validate_geometry_imsm_volume(st, level, layout,
4257 raiddisks, chunk,
4258 size, dev,
4259 freesize, verbose);
4260 }
4261 }
4262
4263 if (verbose)
4264 fprintf(stderr, Name ": failed container membership check\n");
4265
4266 close(cfd);
4267 return 0;
4268 }
4269
4270 static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
4271 {
4272 struct intel_super *super = st->sb;
4273
4274 if (level && *level == UnSet)
4275 *level = LEVEL_CONTAINER;
4276
4277 if (level && layout && *layout == UnSet)
4278 *layout = imsm_level_to_layout(*level);
4279
4280 if (chunk && (*chunk == UnSet || *chunk == 0) &&
4281 super && super->orom)
4282 *chunk = imsm_orom_default_chunk(super->orom);
4283 }
4284
4285 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
4286
4287 static int kill_subarray_imsm(struct supertype *st)
4288 {
4289 /* remove the subarray currently referenced by ->current_vol */
4290 __u8 i;
4291 struct intel_dev **dp;
4292 struct intel_super *super = st->sb;
4293 __u8 current_vol = super->current_vol;
4294 struct imsm_super *mpb = super->anchor;
4295
4296 if (super->current_vol < 0)
4297 return 2;
4298 super->current_vol = -1; /* invalidate subarray cursor */
4299
4300 /* block deletions that would change the uuid of active subarrays
4301 *
4302 * FIXME when immutable ids are available, but note that we'll
4303 * also need to fixup the invalidated/active subarray indexes in
4304 * mdstat
4305 */
4306 for (i = 0; i < mpb->num_raid_devs; i++) {
4307 char subarray[4];
4308
4309 if (i < current_vol)
4310 continue;
4311 sprintf(subarray, "%u", i);
4312 if (is_subarray_active(subarray, st->devname)) {
4313 fprintf(stderr,
4314 Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4315 current_vol, i);
4316
4317 return 2;
4318 }
4319 }
4320
4321 if (st->update_tail) {
4322 struct imsm_update_kill_array *u = malloc(sizeof(*u));
4323
4324 if (!u)
4325 return 2;
4326 u->type = update_kill_array;
4327 u->dev_idx = current_vol;
4328 append_metadata_update(st, u, sizeof(*u));
4329
4330 return 0;
4331 }
4332
4333 for (dp = &super->devlist; *dp;)
4334 if ((*dp)->index == current_vol) {
4335 *dp = (*dp)->next;
4336 } else {
4337 handle_missing(super, (*dp)->dev);
4338 if ((*dp)->index > current_vol)
4339 (*dp)->index--;
4340 dp = &(*dp)->next;
4341 }
4342
4343 /* no more raid devices, all active components are now spares,
4344 * but of course failed are still failed
4345 */
4346 if (--mpb->num_raid_devs == 0) {
4347 struct dl *d;
4348
4349 for (d = super->disks; d; d = d->next)
4350 if (d->index > -2) {
4351 d->index = -1;
4352 d->disk.status = SPARE_DISK;
4353 }
4354 }
4355
4356 super->updates_pending++;
4357
4358 return 0;
4359 }
4360
4361 static int update_subarray_imsm(struct supertype *st, char *subarray,
4362 char *update, struct mddev_ident *ident)
4363 {
4364 /* update the subarray currently referenced by ->current_vol */
4365 struct intel_super *super = st->sb;
4366 struct imsm_super *mpb = super->anchor;
4367
4368 if (strcmp(update, "name") == 0) {
4369 char *name = ident->name;
4370 char *ep;
4371 int vol;
4372
4373 if (is_subarray_active(subarray, st->devname)) {
4374 fprintf(stderr,
4375 Name ": Unable to update name of active subarray\n");
4376 return 2;
4377 }
4378
4379 if (!check_name(super, name, 0))
4380 return 2;
4381
4382 vol = strtoul(subarray, &ep, 10);
4383 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
4384 return 2;
4385
4386 if (st->update_tail) {
4387 struct imsm_update_rename_array *u = malloc(sizeof(*u));
4388
4389 if (!u)
4390 return 2;
4391 u->type = update_rename_array;
4392 u->dev_idx = vol;
4393 snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
4394 append_metadata_update(st, u, sizeof(*u));
4395 } else {
4396 struct imsm_dev *dev;
4397 int i;
4398
4399 dev = get_imsm_dev(super, vol);
4400 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
4401 for (i = 0; i < mpb->num_raid_devs; i++) {
4402 dev = get_imsm_dev(super, i);
4403 handle_missing(super, dev);
4404 }
4405 super->updates_pending++;
4406 }
4407 } else
4408 return 2;
4409
4410 return 0;
4411 }
4412 #endif /* MDASSEMBLE */
4413
4414 static int is_gen_migration(struct imsm_dev *dev)
4415 {
4416 if (!dev->vol.migr_state)
4417 return 0;
4418
4419 if (migr_type(dev) == MIGR_GEN_MIGR)
4420 return 1;
4421
4422 return 0;
4423 }
4424
4425 static int is_rebuilding(struct imsm_dev *dev)
4426 {
4427 struct imsm_map *migr_map;
4428
4429 if (!dev->vol.migr_state)
4430 return 0;
4431
4432 if (migr_type(dev) != MIGR_REBUILD)
4433 return 0;
4434
4435 migr_map = get_imsm_map(dev, 1);
4436
4437 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
4438 return 1;
4439 else
4440 return 0;
4441 }
4442
4443 static void update_recovery_start(struct imsm_dev *dev, struct mdinfo *array)
4444 {
4445 struct mdinfo *rebuild = NULL;
4446 struct mdinfo *d;
4447 __u32 units;
4448
4449 if (!is_rebuilding(dev))
4450 return;
4451
4452 /* Find the rebuild target, but punt on the dual rebuild case */
4453 for (d = array->devs; d; d = d->next)
4454 if (d->recovery_start == 0) {
4455 if (rebuild)
4456 return;
4457 rebuild = d;
4458 }
4459
4460 if (!rebuild) {
4461 /* (?) none of the disks are marked with
4462 * IMSM_ORD_REBUILD, so assume they are missing and the
4463 * disk_ord_tbl was not correctly updated
4464 */
4465 dprintf("%s: failed to locate out-of-sync disk\n", __func__);
4466 return;
4467 }
4468
4469 units = __le32_to_cpu(dev->vol.curr_migr_unit);
4470 rebuild->recovery_start = units * blocks_per_migr_unit(dev);
4471 }
4472
4473
4474 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
4475 {
4476 /* Given a container loaded by load_super_imsm_all,
4477 * extract information about all the arrays into
4478 * an mdinfo tree.
4479 * If 'subarray' is given, just extract info about that array.
4480 *
4481 * For each imsm_dev create an mdinfo, fill it in,
4482 * then look for matching devices in super->disks
4483 * and create appropriate device mdinfo.
4484 */
4485 struct intel_super *super = st->sb;
4486 struct imsm_super *mpb = super->anchor;
4487 struct mdinfo *rest = NULL;
4488 unsigned int i;
4489 int bbm_errors = 0;
4490
4491 /* check for bad blocks */
4492 if (imsm_bbm_log_size(super->anchor))
4493 bbm_errors = 1;
4494
4495 for (i = 0; i < mpb->num_raid_devs; i++) {
4496 struct imsm_dev *dev;
4497 struct imsm_map *map;
4498 struct mdinfo *this;
4499 int slot;
4500 char *ep;
4501
4502 if (subarray &&
4503 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
4504 continue;
4505
4506 dev = get_imsm_dev(super, i);
4507 map = get_imsm_map(dev, 0);
4508
4509 /* do not publish arrays that are in the middle of an
4510 * unsupported migration
4511 */
4512 if (dev->vol.migr_state &&
4513 (migr_type(dev) == MIGR_STATE_CHANGE)) {
4514 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
4515 " unsupported migration in progress\n",
4516 dev->volume);
4517 continue;
4518 }
4519
4520 this = malloc(sizeof(*this));
4521 if (!this) {
4522 fprintf(stderr, Name ": failed to allocate %zu bytes\n",
4523 sizeof(*this));
4524 break;
4525 }
4526 memset(this, 0, sizeof(*this));
4527 this->next = rest;
4528
4529 super->current_vol = i;
4530 getinfo_super_imsm_volume(st, this, NULL);
4531 for (slot = 0 ; slot < map->num_members; slot++) {
4532 unsigned long long recovery_start;
4533 struct mdinfo *info_d;
4534 struct dl *d;
4535 int idx;
4536 int skip;
4537 __u32 ord;
4538
4539 skip = 0;
4540 idx = get_imsm_disk_idx(dev, slot);
4541 ord = get_imsm_ord_tbl_ent(dev, slot);
4542 for (d = super->disks; d ; d = d->next)
4543 if (d->index == idx)
4544 break;
4545
4546 recovery_start = MaxSector;
4547 if (d == NULL)
4548 skip = 1;
4549 if (d && is_failed(&d->disk))
4550 skip = 1;
4551 if (ord & IMSM_ORD_REBUILD)
4552 recovery_start = 0;
4553
4554 /*
4555 * if we skip some disks the array will be assmebled degraded;
4556 * reset resync start to avoid a dirty-degraded
4557 * situation when performing the intial sync
4558 *
4559 * FIXME handle dirty degraded
4560 */
4561 if ((skip || recovery_start == 0) && !dev->vol.dirty)
4562 this->resync_start = MaxSector;
4563 if (skip)
4564 continue;
4565
4566 info_d = calloc(1, sizeof(*info_d));
4567 if (!info_d) {
4568 fprintf(stderr, Name ": failed to allocate disk"
4569 " for volume %.16s\n", dev->volume);
4570 info_d = this->devs;
4571 while (info_d) {
4572 struct mdinfo *d = info_d->next;
4573
4574 free(info_d);
4575 info_d = d;
4576 }
4577 free(this);
4578 this = rest;
4579 break;
4580 }
4581 info_d->next = this->devs;
4582 this->devs = info_d;
4583
4584 info_d->disk.number = d->index;
4585 info_d->disk.major = d->major;
4586 info_d->disk.minor = d->minor;
4587 info_d->disk.raid_disk = slot;
4588 info_d->recovery_start = recovery_start;
4589
4590 if (info_d->recovery_start == MaxSector)
4591 this->array.working_disks++;
4592
4593 info_d->events = __le32_to_cpu(mpb->generation_num);
4594 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
4595 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4596 }
4597 /* now that the disk list is up-to-date fixup recovery_start */
4598 update_recovery_start(dev, this);
4599 rest = this;
4600 }
4601
4602 /* if array has bad blocks, set suitable bit in array status */
4603 if (bbm_errors)
4604 rest->array.state |= (1<<MD_SB_BBM_ERRORS);
4605
4606 return rest;
4607 }
4608
4609
4610 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
4611 {
4612 struct imsm_map *map = get_imsm_map(dev, 0);
4613
4614 if (!failed)
4615 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
4616 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
4617
4618 switch (get_imsm_raid_level(map)) {
4619 case 0:
4620 return IMSM_T_STATE_FAILED;
4621 break;
4622 case 1:
4623 if (failed < map->num_members)
4624 return IMSM_T_STATE_DEGRADED;
4625 else
4626 return IMSM_T_STATE_FAILED;
4627 break;
4628 case 10:
4629 {
4630 /**
4631 * check to see if any mirrors have failed, otherwise we
4632 * are degraded. Even numbered slots are mirrored on
4633 * slot+1
4634 */
4635 int i;
4636 /* gcc -Os complains that this is unused */
4637 int insync = insync;
4638
4639 for (i = 0; i < map->num_members; i++) {
4640 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
4641 int idx = ord_to_idx(ord);
4642 struct imsm_disk *disk;
4643
4644 /* reset the potential in-sync count on even-numbered
4645 * slots. num_copies is always 2 for imsm raid10
4646 */
4647 if ((i & 1) == 0)
4648 insync = 2;
4649
4650 disk = get_imsm_disk(super, idx);
4651 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
4652 insync--;
4653
4654 /* no in-sync disks left in this mirror the
4655 * array has failed
4656 */
4657 if (insync == 0)
4658 return IMSM_T_STATE_FAILED;
4659 }
4660
4661 return IMSM_T_STATE_DEGRADED;
4662 }
4663 case 5:
4664 if (failed < 2)
4665 return IMSM_T_STATE_DEGRADED;
4666 else
4667 return IMSM_T_STATE_FAILED;
4668 break;
4669 default:
4670 break;
4671 }
4672
4673 return map->map_state;
4674 }
4675
4676 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
4677 {
4678 int i;
4679 int failed = 0;
4680 struct imsm_disk *disk;
4681 struct imsm_map *map = get_imsm_map(dev, 0);
4682 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
4683 __u32 ord;
4684 int idx;
4685
4686 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4687 * disks that are being rebuilt. New failures are recorded to
4688 * map[0]. So we look through all the disks we started with and
4689 * see if any failures are still present, or if any new ones
4690 * have arrived
4691 *
4692 * FIXME add support for online capacity expansion and
4693 * raid-level-migration
4694 */
4695 for (i = 0; i < prev->num_members; i++) {
4696 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
4697 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
4698 idx = ord_to_idx(ord);
4699
4700 disk = get_imsm_disk(super, idx);
4701 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
4702 failed++;
4703 }
4704
4705 return failed;
4706 }
4707
4708 #ifndef MDASSEMBLE
4709 static int imsm_open_new(struct supertype *c, struct active_array *a,
4710 char *inst)
4711 {
4712 struct intel_super *super = c->sb;
4713 struct imsm_super *mpb = super->anchor;
4714
4715 if (atoi(inst) >= mpb->num_raid_devs) {
4716 fprintf(stderr, "%s: subarry index %d, out of range\n",
4717 __func__, atoi(inst));
4718 return -ENODEV;
4719 }
4720
4721 dprintf("imsm: open_new %s\n", inst);
4722 a->info.container_member = atoi(inst);
4723 return 0;
4724 }
4725
4726 static int is_resyncing(struct imsm_dev *dev)
4727 {
4728 struct imsm_map *migr_map;
4729
4730 if (!dev->vol.migr_state)
4731 return 0;
4732
4733 if (migr_type(dev) == MIGR_INIT ||
4734 migr_type(dev) == MIGR_REPAIR)
4735 return 1;
4736
4737 if (migr_type(dev) == MIGR_GEN_MIGR)
4738 return 0;
4739
4740 migr_map = get_imsm_map(dev, 1);
4741
4742 if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
4743 (dev->vol.migr_type != MIGR_GEN_MIGR))
4744 return 1;
4745 else
4746 return 0;
4747 }
4748
4749 /* return true if we recorded new information */
4750 static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4751 {
4752 __u32 ord;
4753 int slot;
4754 struct imsm_map *map;
4755
4756 /* new failures are always set in map[0] */
4757 map = get_imsm_map(dev, 0);
4758
4759 slot = get_imsm_disk_slot(map, idx);
4760 if (slot < 0)
4761 return 0;
4762
4763 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
4764 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
4765 return 0;
4766
4767 disk->status |= FAILED_DISK;
4768 disk->status &= ~CONFIGURED_DISK;
4769 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
4770 if (map->failed_disk_num == 0xff)
4771 map->failed_disk_num = slot;
4772 return 1;
4773 }
4774
4775 static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4776 {
4777 mark_failure(dev, disk, idx);
4778
4779 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
4780 return;
4781
4782 disk->scsi_id = __cpu_to_le32(~(__u32)0);
4783 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
4784 }
4785
4786 static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
4787 {
4788 __u8 map_state;
4789 struct dl *dl;
4790 int failed;
4791
4792 if (!super->missing)
4793 return;
4794 failed = imsm_count_failed(super, dev);
4795 map_state = imsm_check_degraded(super, dev, failed);
4796
4797 dprintf("imsm: mark missing\n");
4798 end_migration(dev, map_state);
4799 for (dl = super->missing; dl; dl = dl->next)
4800 mark_missing(dev, &dl->disk, dl->index);
4801 super->updates_pending++;
4802 }
4803
4804 static void imsm_set_disk(struct active_array *a, int n, int state);
4805
4806 /* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
4807 * states are handled in imsm_set_disk() with one exception, when a
4808 * resync is stopped due to a new failure this routine will set the
4809 * 'degraded' state for the array.
4810 */
4811 static int imsm_set_array_state(struct active_array *a, int consistent)
4812 {
4813 int inst = a->info.container_member;
4814 struct intel_super *super = a->container->sb;
4815 struct imsm_dev *dev = get_imsm_dev(super, inst);
4816 struct imsm_map *map = get_imsm_map(dev, 0);
4817 int failed = imsm_count_failed(super, dev);
4818 __u8 map_state = imsm_check_degraded(super, dev, failed);
4819 __u32 blocks_per_unit;
4820
4821 if (dev->vol.migr_state &&
4822 dev->vol.migr_type == MIGR_GEN_MIGR) {
4823 /* array state change is blocked due to reshape action
4824 * We might need to
4825 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
4826 * - finish the reshape (if last_checkpoint is big and action != reshape)
4827 * - update curr_migr_unit
4828 */
4829 if (a->curr_action == reshape) {
4830 /* still reshaping, maybe update curr_migr_unit */
4831 long long blocks_per_unit = blocks_per_migr_unit(dev);
4832 long long unit = a->last_checkpoint;
4833 unit /= blocks_per_unit;
4834 if (unit > __le32_to_cpu(dev->vol.curr_migr_unit)) {
4835 dev->vol.curr_migr_unit = __cpu_to_le32(unit);
4836 super->updates_pending++;
4837 }
4838 } else {
4839 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
4840 /* for some reason we aborted the reshape.
4841 * Better clean up
4842 */
4843 struct imsm_map *map2 = get_imsm_map(dev, 1);
4844 dev->vol.migr_state = 0;
4845 dev->vol.migr_type = 0;
4846 dev->vol.curr_migr_unit = 0;
4847 memcpy(map, map2, sizeof_imsm_map(map2));
4848 super->updates_pending++;
4849 }
4850 if (a->last_checkpoint >= a->info.component_size) {
4851 unsigned long long array_blocks;
4852 int used_disks;
4853 /* it seems the reshape is all done */
4854 dev->vol.migr_state = 0;
4855 dev->vol.migr_type = 0;
4856 dev->vol.curr_migr_unit = 0;
4857
4858 used_disks = imsm_num_data_members(dev);
4859 array_blocks = map->blocks_per_member * used_disks;
4860 /* round array size down to closest MB */
4861 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT)
4862 << SECT_PER_MB_SHIFT;
4863 dev->size_low = __cpu_to_le32((__u32) array_blocks);
4864 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
4865 a->info.custom_array_size = array_blocks;
4866 a->check_reshape = 1; /* encourage manager to update
4867 * array size
4868 */
4869 super->updates_pending++;
4870 }
4871 }
4872 return 0;
4873 }
4874
4875 /* before we activate this array handle any missing disks */
4876 if (consistent == 2)
4877 handle_missing(super, dev);
4878
4879 if (consistent == 2 &&
4880 (!is_resync_complete(&a->info) ||
4881 map_state != IMSM_T_STATE_NORMAL ||
4882 dev->vol.migr_state))
4883 consistent = 0;
4884
4885 if (is_resync_complete(&a->info)) {
4886 /* complete intialization / resync,
4887 * recovery and interrupted recovery is completed in
4888 * ->set_disk
4889 */
4890 if (is_resyncing(dev)) {
4891 dprintf("imsm: mark resync done\n");
4892 end_migration(dev, map_state);
4893 super->updates_pending++;
4894 a->last_checkpoint = 0;
4895 }
4896 } else if (!is_resyncing(dev) && !failed) {
4897 /* mark the start of the init process if nothing is failed */
4898 dprintf("imsm: mark resync start\n");
4899 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
4900 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
4901 else
4902 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
4903 super->updates_pending++;
4904 }
4905
4906 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4907 blocks_per_unit = blocks_per_migr_unit(dev);
4908 if (blocks_per_unit) {
4909 __u32 units32;
4910 __u64 units;
4911
4912 units = a->last_checkpoint / blocks_per_unit;
4913 units32 = units;
4914
4915 /* check that we did not overflow 32-bits, and that
4916 * curr_migr_unit needs updating
4917 */
4918 if (units32 == units &&
4919 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
4920 dprintf("imsm: mark checkpoint (%u)\n", units32);
4921 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
4922 super->updates_pending++;
4923 }
4924 }
4925
4926 /* mark dirty / clean */
4927 if (dev->vol.dirty != !consistent) {
4928 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
4929 if (consistent)
4930 dev->vol.dirty = 0;
4931 else
4932 dev->vol.dirty = 1;
4933 super->updates_pending++;
4934 }
4935
4936 /* finalize online capacity expansion/reshape */
4937 if ((a->curr_action != reshape) &&
4938 (a->prev_action == reshape)) {
4939 struct mdinfo *mdi;
4940
4941 for (mdi = a->info.devs; mdi; mdi = mdi->next)
4942 imsm_set_disk(a, mdi->disk.raid_disk, mdi->curr_state);
4943 }
4944
4945 return consistent;
4946 }
4947
4948 static void imsm_set_disk(struct active_array *a, int n, int state)
4949 {
4950 int inst = a->info.container_member;
4951 struct intel_super *super = a->container->sb;
4952 struct imsm_dev *dev = get_imsm_dev(super, inst);
4953 struct imsm_map *map = get_imsm_map(dev, 0);
4954 struct imsm_disk *disk;
4955 int failed;
4956 __u32 ord;
4957 __u8 map_state;
4958
4959 if (n > map->num_members)
4960 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
4961 n, map->num_members - 1);
4962
4963 if (n < 0)
4964 return;
4965
4966 dprintf("imsm: set_disk %d:%x\n", n, state);
4967
4968 ord = get_imsm_ord_tbl_ent(dev, n);
4969 disk = get_imsm_disk(super, ord_to_idx(ord));
4970
4971 /* check for new failures */
4972 if (state & DS_FAULTY) {
4973 if (mark_failure(dev, disk, ord_to_idx(ord)))
4974 super->updates_pending++;
4975 }
4976
4977 /* check if in_sync */
4978 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
4979 struct imsm_map *migr_map = get_imsm_map(dev, 1);
4980
4981 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
4982 super->updates_pending++;
4983 }
4984
4985 failed = imsm_count_failed(super, dev);
4986 map_state = imsm_check_degraded(super, dev, failed);
4987
4988 /* check if recovery complete, newly degraded, or failed */
4989 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
4990 end_migration(dev, map_state);
4991 map = get_imsm_map(dev, 0);
4992 map->failed_disk_num = ~0;
4993 super->updates_pending++;
4994 a->last_checkpoint = 0;
4995 } else if (map_state == IMSM_T_STATE_DEGRADED &&
4996 map->map_state != map_state &&
4997 !dev->vol.migr_state) {
4998 dprintf("imsm: mark degraded\n");
4999 map->map_state = map_state;
5000 super->updates_pending++;
5001 a->last_checkpoint = 0;
5002 } else if (map_state == IMSM_T_STATE_FAILED &&
5003 map->map_state != map_state) {
5004 dprintf("imsm: mark failed\n");
5005 end_migration(dev, map_state);
5006 super->updates_pending++;
5007 a->last_checkpoint = 0;
5008 } else if (is_gen_migration(dev)) {
5009 dprintf("imsm: Detected General Migration in state: ");
5010 if (map_state == IMSM_T_STATE_NORMAL) {
5011 end_migration(dev, map_state);
5012 map = get_imsm_map(dev, 0);
5013 map->failed_disk_num = ~0;
5014 dprintf("normal\n");
5015 } else {
5016 if (map_state == IMSM_T_STATE_DEGRADED) {
5017 printf("degraded\n");
5018 end_migration(dev, map_state);
5019 } else {
5020 dprintf("failed\n");
5021 }
5022 map->map_state = map_state;
5023 }
5024 super->updates_pending++;
5025 }
5026 }
5027
5028 static int store_imsm_mpb(int fd, struct imsm_super *mpb)
5029 {
5030 void *buf = mpb;
5031 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
5032 unsigned long long dsize;
5033 unsigned long long sectors;
5034
5035 get_dev_size(fd, NULL, &dsize);
5036
5037 if (mpb_size > 512) {
5038 /* -1 to account for anchor */
5039 sectors = mpb_sectors(mpb) - 1;
5040
5041 /* write the extended mpb to the sectors preceeding the anchor */
5042 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
5043 return 1;
5044
5045 if ((unsigned long long)write(fd, buf + 512, 512 * sectors)
5046 != 512 * sectors)
5047 return 1;
5048 }
5049
5050 /* first block is stored on second to last sector of the disk */
5051 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
5052 return 1;
5053
5054 if (write(fd, buf, 512) != 512)
5055 return 1;
5056
5057 return 0;
5058 }
5059
5060 static void imsm_sync_metadata(struct supertype *container)
5061 {
5062 struct intel_super *super = container->sb;
5063
5064 dprintf("sync metadata: %d\n", super->updates_pending);
5065 if (!super->updates_pending)
5066 return;
5067
5068 write_super_imsm(container, 0);
5069
5070 super->updates_pending = 0;
5071 }
5072
5073 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
5074 {
5075 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
5076 int i = get_imsm_disk_idx(dev, idx);
5077 struct dl *dl;
5078
5079 for (dl = super->disks; dl; dl = dl->next)
5080 if (dl->index == i)
5081 break;
5082
5083 if (dl && is_failed(&dl->disk))
5084 dl = NULL;
5085
5086 if (dl)
5087 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
5088
5089 return dl;
5090 }
5091
5092 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
5093 struct active_array *a, int activate_new,
5094 struct mdinfo *additional_test_list)
5095 {
5096 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
5097 int idx = get_imsm_disk_idx(dev, slot);
5098 struct imsm_super *mpb = super->anchor;
5099 struct imsm_map *map;
5100 unsigned long long pos;
5101 struct mdinfo *d;
5102 struct extent *ex;
5103 int i, j;
5104 int found;
5105 __u32 array_start = 0;
5106 __u32 array_end = 0;
5107 struct dl *dl;
5108 struct mdinfo *test_list;
5109
5110 for (dl = super->disks; dl; dl = dl->next) {
5111 /* If in this array, skip */
5112 for (d = a->info.devs ; d ; d = d->next)
5113 if (d->state_fd >= 0 &&
5114 d->disk.major == dl->major &&
5115 d->disk.minor == dl->minor) {
5116 dprintf("%x:%x already in array\n",
5117 dl->major, dl->minor);
5118 break;
5119 }
5120 if (d)
5121 continue;
5122 test_list = additional_test_list;
5123 while (test_list) {
5124 if (test_list->disk.major == dl->major &&
5125 test_list->disk.minor == dl->minor) {
5126 dprintf("%x:%x already in additional test list\n",
5127 dl->major, dl->minor);
5128 break;
5129 }
5130 test_list = test_list->next;
5131 }
5132 if (test_list)
5133 continue;
5134
5135 /* skip in use or failed drives */
5136 if (is_failed(&dl->disk) || idx == dl->index ||
5137 dl->index == -2) {
5138 dprintf("%x:%x status (failed: %d index: %d)\n",
5139 dl->major, dl->minor, is_failed(&dl->disk), idx);
5140 continue;
5141 }
5142
5143 /* skip pure spares when we are looking for partially
5144 * assimilated drives
5145 */
5146 if (dl->index == -1 && !activate_new)
5147 continue;
5148
5149 /* Does this unused device have the requisite free space?
5150 * It needs to be able to cover all member volumes
5151 */
5152 ex = get_extents(super, dl);
5153 if (!ex) {
5154 dprintf("cannot get extents\n");
5155 continue;
5156 }
5157 for (i = 0; i < mpb->num_raid_devs; i++) {
5158 dev = get_imsm_dev(super, i);
5159 map = get_imsm_map(dev, 0);
5160
5161 /* check if this disk is already a member of
5162 * this array
5163 */
5164 if (get_imsm_disk_slot(map, dl->index) >= 0)
5165 continue;
5166
5167 found = 0;
5168 j = 0;
5169 pos = 0;
5170 array_start = __le32_to_cpu(map->pba_of_lba0);
5171 array_end = array_start +
5172 __le32_to_cpu(map->blocks_per_member) - 1;
5173
5174 do {
5175 /* check that we can start at pba_of_lba0 with
5176 * blocks_per_member of space
5177 */
5178 if (array_start >= pos && array_end < ex[j].start) {
5179 found = 1;
5180 break;
5181 }
5182 pos = ex[j].start + ex[j].size;
5183 j++;
5184 } while (ex[j-1].size);
5185
5186 if (!found)
5187 break;
5188 }
5189
5190 free(ex);
5191 if (i < mpb->num_raid_devs) {
5192 dprintf("%x:%x does not have %u to %u available\n",
5193 dl->major, dl->minor, array_start, array_end);
5194 /* No room */
5195 continue;
5196 }
5197 return dl;
5198 }
5199
5200 return dl;
5201 }
5202
5203
5204 static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
5205 {
5206 struct imsm_dev *dev2;
5207 struct imsm_map *map;
5208 struct dl *idisk;
5209 int slot;
5210 int idx;
5211 __u8 state;
5212
5213 dev2 = get_imsm_dev(cont->sb, dev_idx);
5214 if (dev2) {
5215 state = imsm_check_degraded(cont->sb, dev2, failed);
5216 if (state == IMSM_T_STATE_FAILED) {
5217 map = get_imsm_map(dev2, 0);
5218 if (!map)
5219 return 1;
5220 for (slot = 0; slot < map->num_members; slot++) {
5221 /*
5222 * Check if failed disks are deleted from intel
5223 * disk list or are marked to be deleted
5224 */
5225 idx = get_imsm_disk_idx(dev2, slot);
5226 idisk = get_imsm_dl_disk(cont->sb, idx);
5227 /*
5228 * Do not rebuild the array if failed disks
5229 * from failed sub-array are not removed from
5230 * container.
5231 */
5232 if (idisk &&
5233 is_failed(&idisk->disk) &&
5234 (idisk->action != DISK_REMOVE))
5235 return 0;
5236 }
5237 }
5238 }
5239 return 1;
5240 }
5241
5242 static struct mdinfo *imsm_activate_spare(struct active_array *a,
5243 struct metadata_update **updates)
5244 {
5245 /**
5246 * Find a device with unused free space and use it to replace a
5247 * failed/vacant region in an array. We replace failed regions one a
5248 * array at a time. The result is that a new spare disk will be added
5249 * to the first failed array and after the monitor has finished
5250 * propagating failures the remainder will be consumed.
5251 *
5252 * FIXME add a capability for mdmon to request spares from another
5253 * container.
5254 */
5255
5256 struct intel_super *super = a->container->sb;
5257 int inst = a->info.container_member;
5258 struct imsm_dev *dev = get_imsm_dev(super, inst);
5259 struct imsm_map *map = get_imsm_map(dev, 0);
5260 int failed = a->info.array.raid_disks;
5261 struct mdinfo *rv = NULL;
5262 struct mdinfo *d;
5263 struct mdinfo *di;
5264 struct metadata_update *mu;
5265 struct dl *dl;
5266 struct imsm_update_activate_spare *u;
5267 int num_spares = 0;
5268 int i;
5269 int allowed;
5270
5271 for (d = a->info.devs ; d ; d = d->next) {
5272 if ((d->curr_state & DS_FAULTY) &&
5273 d->state_fd >= 0)
5274 /* wait for Removal to happen */
5275 return NULL;
5276 if (d->state_fd >= 0)
5277 failed--;
5278 }
5279
5280 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5281 inst, failed, a->info.array.raid_disks, a->info.array.level);
5282
5283 if (dev->vol.migr_state &&
5284 dev->vol.migr_type == MIGR_GEN_MIGR)
5285 /* No repair during migration */
5286 return NULL;
5287
5288 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
5289 return NULL;
5290
5291 /*
5292 * If there are any failed disks check state of the other volume.
5293 * Block rebuild if the another one is failed until failed disks
5294 * are removed from container.
5295 */
5296 if (failed) {
5297 dprintf("found failed disks in %s, check if there another"
5298 "failed sub-array.\n",
5299 dev->volume);
5300 /* check if states of the other volumes allow for rebuild */
5301 for (i = 0; i < super->anchor->num_raid_devs; i++) {
5302 if (i != inst) {
5303 allowed = imsm_rebuild_allowed(a->container,
5304 i, failed);
5305 if (!allowed)
5306 return NULL;
5307 }
5308 }
5309 }
5310
5311 /* For each slot, if it is not working, find a spare */
5312 for (i = 0; i < a->info.array.raid_disks; i++) {
5313 for (d = a->info.devs ; d ; d = d->next)
5314 if (d->disk.raid_disk == i)
5315 break;
5316 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
5317 if (d && (d->state_fd >= 0))
5318 continue;
5319
5320 /*
5321 * OK, this device needs recovery. Try to re-add the
5322 * previous occupant of this slot, if this fails see if
5323 * we can continue the assimilation of a spare that was
5324 * partially assimilated, finally try to activate a new
5325 * spare.
5326 */
5327 dl = imsm_readd(super, i, a);
5328 if (!dl)
5329 dl = imsm_add_spare(super, i, a, 0, NULL);
5330 if (!dl)
5331 dl = imsm_add_spare(super, i, a, 1, NULL);
5332 if (!dl)
5333 continue;
5334
5335 /* found a usable disk with enough space */
5336 di = malloc(sizeof(*di));
5337 if (!di)
5338 continue;
5339 memset(di, 0, sizeof(*di));
5340
5341 /* dl->index will be -1 in the case we are activating a
5342 * pristine spare. imsm_process_update() will create a
5343 * new index in this case. Once a disk is found to be
5344 * failed in all member arrays it is kicked from the
5345 * metadata
5346 */
5347 di->disk.number = dl->index;
5348
5349 /* (ab)use di->devs to store a pointer to the device
5350 * we chose
5351 */
5352 di->devs = (struct mdinfo *) dl;
5353
5354 di->disk.raid_disk = i;
5355 di->disk.major = dl->major;
5356 di->disk.minor = dl->minor;
5357 di->disk.state = 0;
5358 di->recovery_start = 0;
5359 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
5360 di->component_size = a->info.component_size;
5361 di->container_member = inst;
5362 super->random = random32();
5363 di->next = rv;
5364 rv = di;
5365 num_spares++;
5366 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
5367 i, di->data_offset);
5368
5369 break;
5370 }
5371
5372 if (!rv)
5373 /* No spares found */
5374 return rv;
5375 /* Now 'rv' has a list of devices to return.
5376 * Create a metadata_update record to update the
5377 * disk_ord_tbl for the array
5378 */
5379 mu = malloc(sizeof(*mu));
5380 if (mu) {
5381 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
5382 if (mu->buf == NULL) {
5383 free(mu);
5384 mu = NULL;
5385 }
5386 }
5387 if (!mu) {
5388 while (rv) {
5389 struct mdinfo *n = rv->next;
5390
5391 free(rv);
5392 rv = n;
5393 }
5394 return NULL;
5395 }
5396
5397 mu->space = NULL;
5398 mu->space_list = NULL;
5399 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
5400 mu->next = *updates;
5401 u = (struct imsm_update_activate_spare *) mu->buf;
5402
5403 for (di = rv ; di ; di = di->next) {
5404 u->type = update_activate_spare;
5405 u->dl = (struct dl *) di->devs;
5406 di->devs = NULL;
5407 u->slot = di->disk.raid_disk;
5408 u->array = inst;
5409 u->next = u + 1;
5410 u++;
5411 }
5412 (u-1)->next = NULL;
5413 *updates = mu;
5414
5415 return rv;
5416 }
5417
5418 static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
5419 {
5420 struct imsm_dev *dev = get_imsm_dev(super, idx);
5421 struct imsm_map *map = get_imsm_map(dev, 0);
5422 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
5423 struct disk_info *inf = get_disk_info(u);
5424 struct imsm_disk *disk;
5425 int i;
5426 int j;
5427
5428 for (i = 0; i < map->num_members; i++) {
5429 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
5430 for (j = 0; j < new_map->num_members; j++)
5431 if (serialcmp(disk->serial, inf[j].serial) == 0)
5432 return 1;
5433 }
5434
5435 return 0;
5436 }
5437
5438
5439 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
5440 {
5441 struct dl *dl = NULL;
5442 for (dl = super->disks; dl; dl = dl->next)
5443 if ((dl->major == major) && (dl->minor == minor))
5444 return dl;
5445 return NULL;
5446 }
5447
5448 static int remove_disk_super(struct intel_super *super, int major, int minor)
5449 {
5450 struct dl *prev = NULL;
5451 struct dl *dl;
5452
5453 prev = NULL;
5454 for (dl = super->disks; dl; dl = dl->next) {
5455 if ((dl->major == major) && (dl->minor == minor)) {
5456 /* remove */
5457 if (prev)
5458 prev->next = dl->next;
5459 else
5460 super->disks = dl->next;
5461 dl->next = NULL;
5462 __free_imsm_disk(dl);
5463 dprintf("%s: removed %x:%x\n",
5464 __func__, major, minor);
5465 break;
5466 }
5467 prev = dl;
5468 }
5469 return 0;
5470 }
5471
5472 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
5473
5474 static int add_remove_disk_update(struct intel_super *super)
5475 {
5476 int check_degraded = 0;
5477 struct dl *disk = NULL;
5478 /* add/remove some spares to/from the metadata/contrainer */
5479 while (super->disk_mgmt_list) {
5480 struct dl *disk_cfg;
5481
5482 disk_cfg = super->disk_mgmt_list;
5483 super->disk_mgmt_list = disk_cfg->next;
5484 disk_cfg->next = NULL;
5485
5486 if (disk_cfg->action == DISK_ADD) {
5487 disk_cfg->next = super->disks;
5488 super->disks = disk_cfg;
5489 check_degraded = 1;
5490 dprintf("%s: added %x:%x\n",
5491 __func__, disk_cfg->major,
5492 disk_cfg->minor);
5493 } else if (disk_cfg->action == DISK_REMOVE) {
5494 dprintf("Disk remove action processed: %x.%x\n",
5495 disk_cfg->major, disk_cfg->minor);
5496 disk = get_disk_super(super,
5497 disk_cfg->major,
5498 disk_cfg->minor);
5499 if (disk) {
5500 /* store action status */
5501 disk->action = DISK_REMOVE;
5502 /* remove spare disks only */
5503 if (disk->index == -1) {
5504 remove_disk_super(super,
5505 disk_cfg->major,
5506 disk_cfg->minor);
5507 }
5508 }
5509 /* release allocate disk structure */
5510 __free_imsm_disk(disk_cfg);
5511 }
5512 }
5513 return check_degraded;
5514 }
5515
5516 static void imsm_process_update(struct supertype *st,
5517 struct metadata_update *update)
5518 {
5519 /**
5520 * crack open the metadata_update envelope to find the update record
5521 * update can be one of:
5522 * update_reshape_container_disks - all the arrays in the container
5523 * are being reshaped to have more devices. We need to mark
5524 * the arrays for general migration and convert selected spares
5525 * into active devices.
5526 * update_activate_spare - a spare device has replaced a failed
5527 * device in an array, update the disk_ord_tbl. If this disk is
5528 * present in all member arrays then also clear the SPARE_DISK
5529 * flag
5530 * update_create_array
5531 * update_kill_array
5532 * update_rename_array
5533 * update_add_remove_disk
5534 */
5535 struct intel_super *super = st->sb;
5536 struct imsm_super *mpb;
5537 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
5538
5539 /* update requires a larger buf but the allocation failed */
5540 if (super->next_len && !super->next_buf) {
5541 super->next_len = 0;
5542 return;
5543 }
5544
5545 if (super->next_buf) {
5546 memcpy(super->next_buf, super->buf, super->len);
5547 free(super->buf);
5548 super->len = super->next_len;
5549 super->buf = super->next_buf;
5550
5551 super->next_len = 0;
5552 super->next_buf = NULL;
5553 }
5554
5555 mpb = super->anchor;
5556
5557 switch (type) {
5558 case update_reshape_container_disks: {
5559 struct imsm_update_reshape *u = (void *)update->buf;
5560 struct dl *new_disk;
5561 struct intel_dev *id;
5562 int i;
5563 int delta_disks = u->new_raid_disks - u->old_raid_disks;
5564 void **tofree = NULL;
5565
5566 dprintf("imsm: imsm_process_update() for update_reshape\n");
5567
5568 /* enable spares to use in array */
5569 for (i = 0; i < delta_disks; i++) {
5570
5571 new_disk = get_disk_super(super,
5572 major(u->new_disks[i]),
5573 minor(u->new_disks[i]));
5574 if (new_disk == NULL || new_disk->index < 0)
5575 goto update_reshape_exit;
5576
5577 new_disk->index = mpb->num_disks++;
5578 /* slot to fill in autolayout */
5579 new_disk->raiddisk = new_disk->index;
5580 new_disk->disk.status |=
5581 CONFIGURED_DISK;
5582 new_disk->disk.status &= ~SPARE_DISK;
5583 }
5584
5585 dprintf("imsm: process_update(): update_reshape: volume set"\
5586 " mpb->num_raid_devs = %i\n", mpb->num_raid_devs);
5587 /* manage changes in volumes
5588 */
5589 for (id = super->devlist ; id; id = id->next) {
5590 void **sp = update->space_list;
5591 struct imsm_dev *newdev;
5592 struct imsm_map *newmap, *oldmap;
5593
5594 if (!sp)
5595 continue;
5596 update->space_list = *sp;
5597 newdev = (void*)sp;
5598 /* Copy the dev, but not (all of) the map */
5599 memcpy(newdev, id->dev, sizeof(*newdev));
5600 oldmap = get_imsm_map(id->dev, 0);
5601 newmap = get_imsm_map(newdev, 0);
5602 /* Copy the current map */
5603 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
5604 newdev->vol.migr_state = 1;
5605 newdev->vol.curr_migr_unit = 0;
5606 newdev->vol.migr_type = MIGR_GEN_MIGR;
5607 newmap->num_members = u->new_raid_disks;
5608 for (i = 0; i < delta_disks; i++) {
5609 set_imsm_ord_tbl_ent(newmap,
5610 u->old_raid_disks + i,
5611 u->old_raid_disks + i);
5612 }
5613 /* New map is correct, now need to save old map */
5614 oldmap = get_imsm_map(newdev, 1);
5615 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
5616
5617 sp = (void **)id->dev;
5618 id->dev = newdev;
5619 *sp = tofree;
5620 tofree = sp;
5621 }
5622
5623 update->space_list = tofree;
5624 super->updates_pending++;
5625 update_reshape_exit:
5626 break;
5627 }
5628 case update_activate_spare: {
5629 struct imsm_update_activate_spare *u = (void *) update->buf;
5630 struct imsm_dev *dev = get_imsm_dev(super, u->array);
5631 struct imsm_map *map = get_imsm_map(dev, 0);
5632 struct imsm_map *migr_map;
5633 struct active_array *a;
5634 struct imsm_disk *disk;
5635 __u8 to_state;
5636 struct dl *dl;
5637 unsigned int found;
5638 int failed;
5639 int victim = get_imsm_disk_idx(dev, u->slot);
5640 int i;
5641
5642 for (dl = super->disks; dl; dl = dl->next)
5643 if (dl == u->dl)
5644 break;
5645
5646 if (!dl) {
5647 fprintf(stderr, "error: imsm_activate_spare passed "
5648 "an unknown disk (index: %d)\n",
5649 u->dl->index);
5650 return;
5651 }
5652
5653 super->updates_pending++;
5654
5655 /* count failures (excluding rebuilds and the victim)
5656 * to determine map[0] state
5657 */
5658 failed = 0;
5659 for (i = 0; i < map->num_members; i++) {
5660 if (i == u->slot)
5661 continue;
5662 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
5663 if (!disk || is_failed(disk))
5664 failed++;
5665 }
5666
5667 /* adding a pristine spare, assign a new index */
5668 if (dl->index < 0) {
5669 dl->index = super->anchor->num_disks;
5670 super->anchor->num_disks++;
5671 }
5672 disk = &dl->disk;
5673 disk->status |= CONFIGURED_DISK;
5674 disk->status &= ~SPARE_DISK;
5675
5676 /* mark rebuild */
5677 to_state = imsm_check_degraded(super, dev, failed);
5678 map->map_state = IMSM_T_STATE_DEGRADED;
5679 migrate(dev, to_state, MIGR_REBUILD);
5680 migr_map = get_imsm_map(dev, 1);
5681 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
5682 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
5683
5684 /* update the family_num to mark a new container
5685 * generation, being careful to record the existing
5686 * family_num in orig_family_num to clean up after
5687 * earlier mdadm versions that neglected to set it.
5688 */
5689 if (mpb->orig_family_num == 0)
5690 mpb->orig_family_num = mpb->family_num;
5691 mpb->family_num += super->random;
5692
5693 /* count arrays using the victim in the metadata */
5694 found = 0;
5695 for (a = st->arrays; a ; a = a->next) {
5696 dev = get_imsm_dev(super, a->info.container_member);
5697 map = get_imsm_map(dev, 0);
5698
5699 if (get_imsm_disk_slot(map, victim) >= 0)
5700 found++;
5701 }
5702
5703 /* delete the victim if it is no longer being
5704 * utilized anywhere
5705 */
5706 if (!found) {
5707 struct dl **dlp;
5708
5709 /* We know that 'manager' isn't touching anything,
5710 * so it is safe to delete
5711 */
5712 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
5713 if ((*dlp)->index == victim)
5714 break;
5715
5716 /* victim may be on the missing list */
5717 if (!*dlp)
5718 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
5719 if ((*dlp)->index == victim)
5720 break;
5721 imsm_delete(super, dlp, victim);
5722 }
5723 break;
5724 }
5725 case update_create_array: {
5726 /* someone wants to create a new array, we need to be aware of
5727 * a few races/collisions:
5728 * 1/ 'Create' called by two separate instances of mdadm
5729 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5730 * devices that have since been assimilated via
5731 * activate_spare.
5732 * In the event this update can not be carried out mdadm will
5733 * (FIX ME) notice that its update did not take hold.
5734 */
5735 struct imsm_update_create_array *u = (void *) update->buf;
5736 struct intel_dev *dv;
5737 struct imsm_dev *dev;
5738 struct imsm_map *map, *new_map;
5739 unsigned long long start, end;
5740 unsigned long long new_start, new_end;
5741 int i;
5742 struct disk_info *inf;
5743 struct dl *dl;
5744
5745 /* handle racing creates: first come first serve */
5746 if (u->dev_idx < mpb->num_raid_devs) {
5747 dprintf("%s: subarray %d already defined\n",
5748 __func__, u->dev_idx);
5749 goto create_error;
5750 }
5751
5752 /* check update is next in sequence */
5753 if (u->dev_idx != mpb->num_raid_devs) {
5754 dprintf("%s: can not create array %d expected index %d\n",
5755 __func__, u->dev_idx, mpb->num_raid_devs);
5756 goto create_error;
5757 }
5758
5759 new_map = get_imsm_map(&u->dev, 0);
5760 new_start = __le32_to_cpu(new_map->pba_of_lba0);
5761 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
5762 inf = get_disk_info(u);
5763
5764 /* handle activate_spare versus create race:
5765 * check to make sure that overlapping arrays do not include
5766 * overalpping disks
5767 */
5768 for (i = 0; i < mpb->num_raid_devs; i++) {
5769 dev = get_imsm_dev(super, i);
5770 map = get_imsm_map(dev, 0);
5771 start = __le32_to_cpu(map->pba_of_lba0);
5772 end = start + __le32_to_cpu(map->blocks_per_member);
5773 if ((new_start >= start && new_start <= end) ||
5774 (start >= new_start && start <= new_end))
5775 /* overlap */;
5776 else
5777 continue;
5778
5779 if (disks_overlap(super, i, u)) {
5780 dprintf("%s: arrays overlap\n", __func__);
5781 goto create_error;
5782 }
5783 }
5784
5785 /* check that prepare update was successful */
5786 if (!update->space) {
5787 dprintf("%s: prepare update failed\n", __func__);
5788 goto create_error;
5789 }
5790
5791 /* check that all disks are still active before committing
5792 * changes. FIXME: could we instead handle this by creating a
5793 * degraded array? That's probably not what the user expects,
5794 * so better to drop this update on the floor.
5795 */
5796 for (i = 0; i < new_map->num_members; i++) {
5797 dl = serial_to_dl(inf[i].serial, super);
5798 if (!dl) {
5799 dprintf("%s: disk disappeared\n", __func__);
5800 goto create_error;
5801 }
5802 }
5803
5804 super->updates_pending++;
5805
5806 /* convert spares to members and fixup ord_tbl */
5807 for (i = 0; i < new_map->num_members; i++) {
5808 dl = serial_to_dl(inf[i].serial, super);
5809 if (dl->index == -1) {
5810 dl->index = mpb->num_disks;
5811 mpb->num_disks++;
5812 dl->disk.status |= CONFIGURED_DISK;
5813 dl->disk.status &= ~SPARE_DISK;
5814 }
5815 set_imsm_ord_tbl_ent(new_map, i, dl->index);
5816 }
5817
5818 dv = update->space;
5819 dev = dv->dev;
5820 update->space = NULL;
5821 imsm_copy_dev(dev, &u->dev);
5822 dv->index = u->dev_idx;
5823 dv->next = super->devlist;
5824 super->devlist = dv;
5825 mpb->num_raid_devs++;
5826
5827 imsm_update_version_info(super);
5828 break;
5829 create_error:
5830 /* mdmon knows how to release update->space, but not
5831 * ((struct intel_dev *) update->space)->dev
5832 */
5833 if (update->space) {
5834 dv = update->space;
5835 free(dv->dev);
5836 }
5837 break;
5838 }
5839 case update_kill_array: {
5840 struct imsm_update_kill_array *u = (void *) update->buf;
5841 int victim = u->dev_idx;
5842 struct active_array *a;
5843 struct intel_dev **dp;
5844 struct imsm_dev *dev;
5845
5846 /* sanity check that we are not affecting the uuid of
5847 * active arrays, or deleting an active array
5848 *
5849 * FIXME when immutable ids are available, but note that
5850 * we'll also need to fixup the invalidated/active
5851 * subarray indexes in mdstat
5852 */
5853 for (a = st->arrays; a; a = a->next)
5854 if (a->info.container_member >= victim)
5855 break;
5856 /* by definition if mdmon is running at least one array
5857 * is active in the container, so checking
5858 * mpb->num_raid_devs is just extra paranoia
5859 */
5860 dev = get_imsm_dev(super, victim);
5861 if (a || !dev || mpb->num_raid_devs == 1) {
5862 dprintf("failed to delete subarray-%d\n", victim);
5863 break;
5864 }
5865
5866 for (dp = &super->devlist; *dp;)
5867 if ((*dp)->index == (unsigned)super->current_vol) {
5868 *dp = (*dp)->next;
5869 } else {
5870 if ((*dp)->index > (unsigned)victim)
5871 (*dp)->index--;
5872 dp = &(*dp)->next;
5873 }
5874 mpb->num_raid_devs--;
5875 super->updates_pending++;
5876 break;
5877 }
5878 case update_rename_array: {
5879 struct imsm_update_rename_array *u = (void *) update->buf;
5880 char name[MAX_RAID_SERIAL_LEN+1];
5881 int target = u->dev_idx;
5882 struct active_array *a;
5883 struct imsm_dev *dev;
5884
5885 /* sanity check that we are not affecting the uuid of
5886 * an active array
5887 */
5888 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
5889 name[MAX_RAID_SERIAL_LEN] = '\0';
5890 for (a = st->arrays; a; a = a->next)
5891 if (a->info.container_member == target)
5892 break;
5893 dev = get_imsm_dev(super, u->dev_idx);
5894 if (a || !dev || !check_name(super, name, 1)) {
5895 dprintf("failed to rename subarray-%d\n", target);
5896 break;
5897 }
5898
5899 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
5900 super->updates_pending++;
5901 break;
5902 }
5903 case update_add_remove_disk: {
5904 /* we may be able to repair some arrays if disks are
5905 * being added, check teh status of add_remove_disk
5906 * if discs has been added.
5907 */
5908 if (add_remove_disk_update(super)) {
5909 struct active_array *a;
5910
5911 super->updates_pending++;
5912 for (a = st->arrays; a; a = a->next)
5913 a->check_degraded = 1;
5914 }
5915 break;
5916 }
5917 default:
5918 fprintf(stderr, "error: unsuported process update type:"
5919 "(type: %d)\n", type);
5920 }
5921 }
5922
5923 static void imsm_prepare_update(struct supertype *st,
5924 struct metadata_update *update)
5925 {
5926 /**
5927 * Allocate space to hold new disk entries, raid-device entries or a new
5928 * mpb if necessary. The manager synchronously waits for updates to
5929 * complete in the monitor, so new mpb buffers allocated here can be
5930 * integrated by the monitor thread without worrying about live pointers
5931 * in the manager thread.
5932 */
5933 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
5934 struct intel_super *super = st->sb;
5935 struct imsm_super *mpb = super->anchor;
5936 size_t buf_len;
5937 size_t len = 0;
5938
5939 switch (type) {
5940 case update_reshape_container_disks: {
5941 /* Every raid device in the container is about to
5942 * gain some more devices, and we will enter a
5943 * reconfiguration.
5944 * So each 'imsm_map' will be bigger, and the imsm_vol
5945 * will now hold 2 of them.
5946 * Thus we need new 'struct imsm_dev' allocations sized
5947 * as sizeof_imsm_dev but with more devices in both maps.
5948 */
5949 struct imsm_update_reshape *u = (void *)update->buf;
5950 struct intel_dev *dl;
5951 void **space_tail = (void**)&update->space_list;
5952
5953 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
5954
5955 for (dl = super->devlist; dl; dl = dl->next) {
5956 int size = sizeof_imsm_dev(dl->dev, 1);
5957 void *s;
5958 size += sizeof(__u32) * 2 *
5959 (u->new_raid_disks - u->old_raid_disks);
5960 s = malloc(size);
5961 if (!s)
5962 break;
5963 *space_tail = s;
5964 space_tail = s;
5965 *space_tail = NULL;
5966 }
5967
5968 len = disks_to_mpb_size(u->new_raid_disks);
5969 dprintf("New anchor length is %llu\n", (unsigned long long)len);
5970 break;
5971 }
5972 case update_create_array: {
5973 struct imsm_update_create_array *u = (void *) update->buf;
5974 struct intel_dev *dv;
5975 struct imsm_dev *dev = &u->dev;
5976 struct imsm_map *map = get_imsm_map(dev, 0);
5977 struct dl *dl;
5978 struct disk_info *inf;
5979 int i;
5980 int activate = 0;
5981
5982 inf = get_disk_info(u);
5983 len = sizeof_imsm_dev(dev, 1);
5984 /* allocate a new super->devlist entry */
5985 dv = malloc(sizeof(*dv));
5986 if (dv) {
5987 dv->dev = malloc(len);
5988 if (dv->dev)
5989 update->space = dv;
5990 else {
5991 free(dv);
5992 update->space = NULL;
5993 }
5994 }
5995
5996 /* count how many spares will be converted to members */
5997 for (i = 0; i < map->num_members; i++) {
5998 dl = serial_to_dl(inf[i].serial, super);
5999 if (!dl) {
6000 /* hmm maybe it failed?, nothing we can do about
6001 * it here
6002 */
6003 continue;
6004 }
6005 if (count_memberships(dl, super) == 0)
6006 activate++;
6007 }
6008 len += activate * sizeof(struct imsm_disk);
6009 break;
6010 default:
6011 break;
6012 }
6013 }
6014
6015 /* check if we need a larger metadata buffer */
6016 if (super->next_buf)
6017 buf_len = super->next_len;
6018 else
6019 buf_len = super->len;
6020
6021 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
6022 /* ok we need a larger buf than what is currently allocated
6023 * if this allocation fails process_update will notice that
6024 * ->next_len is set and ->next_buf is NULL
6025 */
6026 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
6027 if (super->next_buf)
6028 free(super->next_buf);
6029
6030 super->next_len = buf_len;
6031 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
6032 memset(super->next_buf, 0, buf_len);
6033 else
6034 super->next_buf = NULL;
6035 }
6036 }
6037
6038 /* must be called while manager is quiesced */
6039 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
6040 {
6041 struct imsm_super *mpb = super->anchor;
6042 struct dl *iter;
6043 struct imsm_dev *dev;
6044 struct imsm_map *map;
6045 int i, j, num_members;
6046 __u32 ord;
6047
6048 dprintf("%s: deleting device[%d] from imsm_super\n",
6049 __func__, index);
6050
6051 /* shift all indexes down one */
6052 for (iter = super->disks; iter; iter = iter->next)
6053 if (iter->index > (int)index)
6054 iter->index--;
6055 for (iter = super->missing; iter; iter = iter->next)
6056 if (iter->index > (int)index)
6057 iter->index--;
6058
6059 for (i = 0; i < mpb->num_raid_devs; i++) {
6060 dev = get_imsm_dev(super, i);
6061 map = get_imsm_map(dev, 0);
6062 num_members = map->num_members;
6063 for (j = 0; j < num_members; j++) {
6064 /* update ord entries being careful not to propagate
6065 * ord-flags to the first map
6066 */
6067 ord = get_imsm_ord_tbl_ent(dev, j);
6068
6069 if (ord_to_idx(ord) <= index)
6070 continue;
6071
6072 map = get_imsm_map(dev, 0);
6073 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
6074 map = get_imsm_map(dev, 1);
6075 if (map)
6076 set_imsm_ord_tbl_ent(map, j, ord - 1);
6077 }
6078 }
6079
6080 mpb->num_disks--;
6081 super->updates_pending++;
6082 if (*dlp) {
6083 struct dl *dl = *dlp;
6084
6085 *dlp = (*dlp)->next;
6086 __free_imsm_disk(dl);
6087 }
6088 }
6089 #endif /* MDASSEMBLE */
6090
6091 static char disk_by_path[] = "/dev/disk/by-path/";
6092
6093 static const char *imsm_get_disk_controller_domain(const char *path)
6094 {
6095 struct sys_dev *list, *hba = NULL;
6096 char disk_path[PATH_MAX];
6097 int ahci = 0;
6098 char *dpath = NULL;
6099
6100 list = find_driver_devices("pci", "ahci");
6101 for (hba = list; hba; hba = hba->next)
6102 if (devpath_to_vendor(hba->path) == 0x8086)
6103 break;
6104
6105 if (hba) {
6106 struct stat st;
6107
6108 strncpy(disk_path, disk_by_path, PATH_MAX - 1);
6109 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
6110 if (stat(disk_path, &st) == 0) {
6111 dpath = devt_to_devpath(st.st_rdev);
6112 if (dpath)
6113 ahci = path_attached_to_hba(dpath, hba->path);
6114 }
6115 }
6116 dprintf("path: %s(%s) hba: %s attached: %d\n",
6117 path, dpath, (hba) ? hba->path : "NULL", ahci);
6118 free_sys_dev(&list);
6119 if (ahci)
6120 return "ahci";
6121 else
6122 return NULL;
6123 }
6124
6125 static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
6126 {
6127 char subdev_name[20];
6128 struct mdstat_ent *mdstat;
6129
6130 sprintf(subdev_name, "%d", subdev);
6131 mdstat = mdstat_by_subdev(subdev_name, container);
6132 if (!mdstat)
6133 return -1;
6134
6135 *minor = mdstat->devnum;
6136 free_mdstat(mdstat);
6137 return 0;
6138 }
6139
6140 static int imsm_reshape_is_allowed_on_container(struct supertype *st,
6141 struct geo_params *geo,
6142 int *old_raid_disks)
6143 {
6144 int ret_val = 0;
6145 struct mdinfo *info, *member;
6146 int devices_that_can_grow = 0;
6147
6148 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6149 "st->devnum = (%i)\n",
6150 st->devnum);
6151
6152 if (geo->size != -1 ||
6153 geo->level != UnSet ||
6154 geo->layout != UnSet ||
6155 geo->chunksize != 0 ||
6156 geo->raid_disks == UnSet) {
6157 dprintf("imsm: Container operation is allowed for "
6158 "raid disks number change only.\n");
6159 return ret_val;
6160 }
6161
6162 info = container_content_imsm(st, NULL);
6163 for (member = info; member; member = member->next) {
6164 int result;
6165 int minor;
6166
6167 dprintf("imsm: checking device_num: %i\n",
6168 member->container_member);
6169
6170 if (geo->raid_disks < member->array.raid_disks) {
6171 /* we work on container for Online Capacity Expansion
6172 * only so raid_disks has to grow
6173 */
6174 dprintf("imsm: for container operation raid disks "
6175 "increase is required\n");
6176 break;
6177 }
6178
6179 if ((info->array.level != 0) &&
6180 (info->array.level != 5)) {
6181 /* we cannot use this container with other raid level
6182 */
6183 dprintf("imsm: for container operation wrong"\
6184 " raid level (%i) detected\n",
6185 info->array.level);
6186 break;
6187 } else {
6188 /* check for platform support
6189 * for this raid level configuration
6190 */
6191 struct intel_super *super = st->sb;
6192 if (!is_raid_level_supported(super->orom,
6193 member->array.level,
6194 geo->raid_disks)) {
6195 dprintf("platform does not support raid%d with"\
6196 " %d disk%s\n",
6197 info->array.level,
6198 geo->raid_disks,
6199 geo->raid_disks > 1 ? "s" : "");
6200 break;
6201 }
6202 }
6203
6204 if (*old_raid_disks &&
6205 info->array.raid_disks != *old_raid_disks)
6206 break;
6207 *old_raid_disks = info->array.raid_disks;
6208
6209 /* All raid5 and raid0 volumes in container
6210 * have to be ready for Online Capacity Expansion
6211 * so they need to be assembled. We have already
6212 * checked that no recovery etc is happening.
6213 */
6214 result = imsm_find_array_minor_by_subdev(member->container_member,
6215 st->container_dev,
6216 &minor);
6217 if (result < 0) {
6218 dprintf("imsm: cannot find array\n");
6219 break;
6220 }
6221 devices_that_can_grow++;
6222 }
6223 sysfs_free(info);
6224 if (!member && devices_that_can_grow)
6225 ret_val = 1;
6226
6227 if (ret_val)
6228 dprintf("\tContainer operation allowed\n");
6229 else
6230 dprintf("\tError: %i\n", ret_val);
6231
6232 return ret_val;
6233 }
6234
6235 /* Function: get_spares_for_grow
6236 * Description: Allocates memory and creates list of spare devices
6237 * avaliable in container. Checks if spare drive size is acceptable.
6238 * Parameters: Pointer to the supertype structure
6239 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6240 * NULL if fail
6241 */
6242 static struct mdinfo *get_spares_for_grow(struct supertype *st)
6243 {
6244 unsigned long long min_size = min_acceptable_spare_size_imsm(st);
6245 return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
6246 }
6247
6248 /******************************************************************************
6249 * function: imsm_create_metadata_update_for_reshape
6250 * Function creates update for whole IMSM container.
6251 *
6252 ******************************************************************************/
6253 static int imsm_create_metadata_update_for_reshape(
6254 struct supertype *st,
6255 struct geo_params *geo,
6256 int old_raid_disks,
6257 struct imsm_update_reshape **updatep)
6258 {
6259 struct intel_super *super = st->sb;
6260 struct imsm_super *mpb = super->anchor;
6261 int update_memory_size = 0;
6262 struct imsm_update_reshape *u = NULL;
6263 struct mdinfo *spares = NULL;
6264 int i;
6265 int delta_disks = 0;
6266
6267 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6268 geo->raid_disks);
6269
6270 delta_disks = geo->raid_disks - old_raid_disks;
6271
6272 /* size of all update data without anchor */
6273 update_memory_size = sizeof(struct imsm_update_reshape);
6274
6275 /* now add space for spare disks that we need to add. */
6276 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
6277
6278 u = calloc(1, update_memory_size);
6279 if (u == NULL) {
6280 dprintf("error: "
6281 "cannot get memory for imsm_update_reshape update\n");
6282 return 0;
6283 }
6284 u->type = update_reshape_container_disks;
6285 u->old_raid_disks = old_raid_disks;
6286 u->new_raid_disks = geo->raid_disks;
6287
6288 /* now get spare disks list
6289 */
6290 spares = get_spares_for_grow(st);
6291
6292 if (spares == NULL
6293 || delta_disks > spares->array.spare_disks) {
6294 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6295 goto abort;
6296 }
6297
6298 /* we have got spares
6299 * update disk list in imsm_disk list table in anchor
6300 */
6301 dprintf("imsm: %i spares are available.\n\n",
6302 spares->array.spare_disks);
6303
6304 for (i = 0; i < delta_disks; i++) {
6305 struct mdinfo *dev = spares->devs;
6306 struct dl *dl;
6307
6308 u->new_disks[i] = makedev(dev->disk.major,
6309 dev->disk.minor);
6310 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
6311 dl->index = mpb->num_disks++;
6312 }
6313 /* Now update the metadata so that container_content will find
6314 * the new devices
6315 */
6316 for (i = 0; i < mpb->num_raid_devs; i++) {
6317 int d;
6318 struct imsm_dev *dev = get_imsm_dev(super, i);
6319 struct imsm_map *map = get_imsm_map(dev, 0);
6320 map->num_members = geo->raid_disks;
6321 for (d = 0; d < delta_disks; d++) {
6322 set_imsm_ord_tbl_ent(map, old_raid_disks + d,
6323 mpb->num_disks - delta_disks + d);
6324 }
6325 }
6326
6327 abort:
6328 /* free spares
6329 */
6330 sysfs_free(spares);
6331
6332 if (i == delta_disks) {
6333 *updatep = u;
6334 return update_memory_size;
6335 }
6336 free(u);
6337
6338 return 0;
6339 }
6340
6341
6342 static int imsm_reshape_super(struct supertype *st, long long size, int level,
6343 int layout, int chunksize, int raid_disks,
6344 char *backup, char *dev, int verbouse)
6345 {
6346 /* currently we only support increasing the number of devices
6347 * for a container. This increases the number of device for each
6348 * member array. They must all be RAID0 or RAID5.
6349 */
6350
6351 int ret_val = 1;
6352 struct geo_params geo;
6353
6354 dprintf("imsm: reshape_super called.\n");
6355
6356 memset(&geo, sizeof(struct geo_params), 0);
6357
6358 geo.dev_name = dev;
6359 geo.size = size;
6360 geo.level = level;
6361 geo.layout = layout;
6362 geo.chunksize = chunksize;
6363 geo.raid_disks = raid_disks;
6364
6365 dprintf("\tfor level : %i\n", geo.level);
6366 dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
6367
6368 if (experimental() == 0)
6369 return ret_val;
6370
6371 /* verify reshape conditions
6372 * on container level we can only increase number of devices. */
6373 if (st->container_dev == st->devnum) {
6374 /* check for delta_disks > 0
6375 *and supported raid levels 0 and 5 only in container */
6376 int old_raid_disks = 0;
6377 if (imsm_reshape_is_allowed_on_container(
6378 st, &geo, &old_raid_disks)) {
6379 struct imsm_update_reshape *u = NULL;
6380 int len;
6381
6382 len = imsm_create_metadata_update_for_reshape(
6383 st, &geo, old_raid_disks, &u);
6384
6385 if (len) {
6386 ret_val = 0;
6387 append_metadata_update(st, u, len);
6388 } else
6389 dprintf("imsm: Cannot prepare "\
6390 "update\n");
6391 } else
6392 dprintf("imsm: Operation is not allowed "\
6393 "on this container\n");
6394 } else
6395 dprintf("imsm: not a container operation\n");
6396
6397 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
6398 return ret_val;
6399 }
6400
6401 struct superswitch super_imsm = {
6402 #ifndef MDASSEMBLE
6403 .examine_super = examine_super_imsm,
6404 .brief_examine_super = brief_examine_super_imsm,
6405 .brief_examine_subarrays = brief_examine_subarrays_imsm,
6406 .export_examine_super = export_examine_super_imsm,
6407 .detail_super = detail_super_imsm,
6408 .brief_detail_super = brief_detail_super_imsm,
6409 .write_init_super = write_init_super_imsm,
6410 .validate_geometry = validate_geometry_imsm,
6411 .add_to_super = add_to_super_imsm,
6412 .remove_from_super = remove_from_super_imsm,
6413 .detail_platform = detail_platform_imsm,
6414 .kill_subarray = kill_subarray_imsm,
6415 .update_subarray = update_subarray_imsm,
6416 .load_container = load_container_imsm,
6417 #endif
6418 .match_home = match_home_imsm,
6419 .uuid_from_super= uuid_from_super_imsm,
6420 .getinfo_super = getinfo_super_imsm,
6421 .getinfo_super_disks = getinfo_super_disks_imsm,
6422 .update_super = update_super_imsm,
6423
6424 .avail_size = avail_size_imsm,
6425 .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
6426
6427 .compare_super = compare_super_imsm,
6428
6429 .load_super = load_super_imsm,
6430 .init_super = init_super_imsm,
6431 .store_super = store_super_imsm,
6432 .free_super = free_super_imsm,
6433 .match_metadata_desc = match_metadata_desc_imsm,
6434 .container_content = container_content_imsm,
6435 .default_geometry = default_geometry_imsm,
6436 .get_disk_controller_domain = imsm_get_disk_controller_domain,
6437 .reshape_super = imsm_reshape_super,
6438
6439 .external = 1,
6440 .name = "imsm",
6441
6442 #ifndef MDASSEMBLE
6443 /* for mdmon */
6444 .open_new = imsm_open_new,
6445 .set_array_state= imsm_set_array_state,
6446 .set_disk = imsm_set_disk,
6447 .sync_metadata = imsm_sync_metadata,
6448 .activate_spare = imsm_activate_spare,
6449 .process_update = imsm_process_update,
6450 .prepare_update = imsm_prepare_update,
6451 #endif /* MDASSEMBLE */
6452 };