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