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