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