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