]> git.ipfire.org Git - thirdparty/mdadm.git/blame - super-intel.c
imsm: Do not indicate resync during reshape
[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);
efb30e7f 1533 struct dl *dl;
e207da2f 1534 char *devname;
a5d85af7 1535 int map_disks = info->array.raid_disks;
bf5a934a 1536
efb30e7f
DW
1537 for (dl = super->disks; dl; dl = dl->next)
1538 if (dl->raiddisk == info->disk.raid_disk)
1539 break;
bf5a934a
DW
1540 info->container_member = super->current_vol;
1541 info->array.raid_disks = map->num_members;
1542 info->array.level = get_imsm_raid_level(map);
1543 info->array.layout = imsm_level_to_layout(info->array.level);
1544 info->array.md_minor = -1;
1545 info->array.ctime = 0;
1546 info->array.utime = 0;
301406c9
DW
1547 info->array.chunk_size = __le16_to_cpu(map->blocks_per_strip) << 9;
1548 info->array.state = !dev->vol.dirty;
da9b4a62
DW
1549 info->custom_array_size = __le32_to_cpu(dev->size_high);
1550 info->custom_array_size <<= 32;
1551 info->custom_array_size |= __le32_to_cpu(dev->size_low);
301406c9
DW
1552
1553 info->disk.major = 0;
1554 info->disk.minor = 0;
efb30e7f
DW
1555 if (dl) {
1556 info->disk.major = dl->major;
1557 info->disk.minor = dl->minor;
1558 }
bf5a934a
DW
1559
1560 info->data_offset = __le32_to_cpu(map->pba_of_lba0);
1561 info->component_size = __le32_to_cpu(map->blocks_per_member);
301406c9 1562 memset(info->uuid, 0, sizeof(info->uuid));
921d9e16 1563 info->recovery_start = MaxSector;
624c5ad4 1564 info->reshape_active = 0;
bf5a934a 1565
1e5c6983 1566 if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) {
301406c9 1567 info->resync_start = 0;
1e5c6983
DW
1568 } else if (dev->vol.migr_state) {
1569 switch (migr_type(dev)) {
1570 case MIGR_REPAIR:
1571 case MIGR_INIT: {
1572 __u64 blocks_per_unit = blocks_per_migr_unit(dev);
1573 __u64 units = __le32_to_cpu(dev->vol.curr_migr_unit);
1574
1575 info->resync_start = blocks_per_unit * units;
1576 break;
1577 }
1578 case MIGR_VERIFY:
1579 /* we could emulate the checkpointing of
1580 * 'sync_action=check' migrations, but for now
1581 * we just immediately complete them
1582 */
1583 case MIGR_REBUILD:
1584 /* this is handled by container_content_imsm() */
1585 case MIGR_GEN_MIGR:
1586 case MIGR_STATE_CHANGE:
1587 /* FIXME handle other migrations */
1588 default:
1589 /* we are not dirty, so... */
1590 info->resync_start = MaxSector;
1591 }
1592 } else
b7528a20 1593 info->resync_start = MaxSector;
301406c9
DW
1594
1595 strncpy(info->name, (char *) dev->volume, MAX_RAID_SERIAL_LEN);
1596 info->name[MAX_RAID_SERIAL_LEN] = 0;
bf5a934a 1597
f35f2525
N
1598 info->array.major_version = -1;
1599 info->array.minor_version = -2;
e207da2f
AW
1600 devname = devnum2devname(st->container_dev);
1601 *info->text_version = '\0';
1602 if (devname)
1603 sprintf(info->text_version, "/%s/%d", devname, info->container_member);
1604 free(devname);
a67dd8cc 1605 info->safe_mode_delay = 4000; /* 4 secs like the Matrix driver */
51006d85 1606 uuid_from_super_imsm(st, info->uuid);
a5d85af7
N
1607
1608 if (dmap) {
1609 int i, j;
1610 for (i=0; i<map_disks; i++) {
1611 dmap[i] = 0;
1612 if (i < info->array.raid_disks) {
1613 struct imsm_disk *dsk;
1614 j = get_imsm_disk_idx(dev, i);
1615 dsk = get_imsm_disk(super, j);
1616 if (dsk && (dsk->status & CONFIGURED_DISK))
1617 dmap[i] = 1;
1618 }
1619 }
1620 }
1621}
bf5a934a 1622
7a70e8aa
DW
1623/* check the config file to see if we can return a real uuid for this spare */
1624static void fixup_container_spare_uuid(struct mdinfo *inf)
1625{
fa56eddb 1626 struct mddev_ident *array_list;
7a70e8aa
DW
1627
1628 if (inf->array.level != LEVEL_CONTAINER ||
1629 memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
1630 return;
1631
1632 array_list = conf_get_ident(NULL);
1633
1634 for (; array_list; array_list = array_list->next) {
1635 if (array_list->uuid_set) {
1636 struct supertype *_sst; /* spare supertype */
1637 struct supertype *_cst; /* container supertype */
1638
1639 _cst = array_list->st;
7e8545e9
DW
1640 if (_cst)
1641 _sst = _cst->ss->match_metadata_desc(inf->text_version);
1642 else
1643 _sst = NULL;
1644
7a70e8aa
DW
1645 if (_sst) {
1646 memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
1647 free(_sst);
1648 break;
1649 }
1650 }
1651 }
1652}
bf5a934a 1653
97b4d0e9
DW
1654
1655static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed);
1656static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev);
1657
1658static struct imsm_disk *get_imsm_missing(struct intel_super *super, __u8 index)
1659{
1660 struct dl *d;
1661
1662 for (d = super->missing; d; d = d->next)
1663 if (d->index == index)
1664 return &d->disk;
1665 return NULL;
1666}
1667
a5d85af7 1668static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map)
4f5bc454
DW
1669{
1670 struct intel_super *super = st->sb;
4f5bc454 1671 struct imsm_disk *disk;
a5d85af7 1672 int map_disks = info->array.raid_disks;
ab3cb6b3
N
1673 int max_enough = -1;
1674 int i;
1675 struct imsm_super *mpb;
4f5bc454 1676
bf5a934a 1677 if (super->current_vol >= 0) {
a5d85af7 1678 getinfo_super_imsm_volume(st, info, map);
bf5a934a
DW
1679 return;
1680 }
d23fe947
DW
1681
1682 /* Set raid_disks to zero so that Assemble will always pull in valid
1683 * spares
1684 */
1685 info->array.raid_disks = 0;
cdddbdbc
DW
1686 info->array.level = LEVEL_CONTAINER;
1687 info->array.layout = 0;
1688 info->array.md_minor = -1;
c2c087e6 1689 info->array.ctime = 0; /* N/A for imsm */
cdddbdbc
DW
1690 info->array.utime = 0;
1691 info->array.chunk_size = 0;
1692
1693 info->disk.major = 0;
1694 info->disk.minor = 0;
cdddbdbc 1695 info->disk.raid_disk = -1;
c2c087e6 1696 info->reshape_active = 0;
f35f2525
N
1697 info->array.major_version = -1;
1698 info->array.minor_version = -2;
c2c087e6 1699 strcpy(info->text_version, "imsm");
a67dd8cc 1700 info->safe_mode_delay = 0;
c2c087e6
DW
1701 info->disk.number = -1;
1702 info->disk.state = 0;
c5afc314 1703 info->name[0] = 0;
921d9e16 1704 info->recovery_start = MaxSector;
c2c087e6 1705
97b4d0e9 1706 /* do we have the all the insync disks that we expect? */
ab3cb6b3 1707 mpb = super->anchor;
97b4d0e9 1708
ab3cb6b3
N
1709 for (i = 0; i < mpb->num_raid_devs; i++) {
1710 struct imsm_dev *dev = get_imsm_dev(super, i);
1711 int failed, enough, j, missing = 0;
1712 struct imsm_map *map;
1713 __u8 state;
97b4d0e9 1714
ab3cb6b3
N
1715 failed = imsm_count_failed(super, dev);
1716 state = imsm_check_degraded(super, dev, failed);
1717 map = get_imsm_map(dev, dev->vol.migr_state);
1718
1719 /* any newly missing disks?
1720 * (catches single-degraded vs double-degraded)
1721 */
1722 for (j = 0; j < map->num_members; j++) {
1723 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
1724 __u32 idx = ord_to_idx(ord);
1725
1726 if (!(ord & IMSM_ORD_REBUILD) &&
1727 get_imsm_missing(super, idx)) {
1728 missing = 1;
1729 break;
1730 }
97b4d0e9 1731 }
ab3cb6b3
N
1732
1733 if (state == IMSM_T_STATE_FAILED)
1734 enough = -1;
1735 else if (state == IMSM_T_STATE_DEGRADED &&
1736 (state != map->map_state || missing))
1737 enough = 0;
1738 else /* we're normal, or already degraded */
1739 enough = 1;
1740
1741 /* in the missing/failed disk case check to see
1742 * if at least one array is runnable
1743 */
1744 max_enough = max(max_enough, enough);
1745 }
1746 dprintf("%s: enough: %d\n", __func__, max_enough);
1747 info->container_enough = max_enough;
97b4d0e9 1748
4a04ec6c 1749 if (super->disks) {
14e8215b
DW
1750 __u32 reserved = imsm_reserved_sectors(super, super->disks);
1751
b9f594fe 1752 disk = &super->disks->disk;
14e8215b
DW
1753 info->data_offset = __le32_to_cpu(disk->total_blocks) - reserved;
1754 info->component_size = reserved;
25ed7e59 1755 info->disk.state = is_configured(disk) ? (1 << MD_DISK_ACTIVE) : 0;
df474657
DW
1756 /* we don't change info->disk.raid_disk here because
1757 * this state will be finalized in mdmon after we have
1758 * found the 'most fresh' version of the metadata
1759 */
25ed7e59
DW
1760 info->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1761 info->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
cdddbdbc 1762 }
a575e2a7
DW
1763
1764 /* only call uuid_from_super_imsm when this disk is part of a populated container,
1765 * ->compare_super may have updated the 'num_raid_devs' field for spares
1766 */
1767 if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
36ba7d48 1768 uuid_from_super_imsm(st, info->uuid);
7a70e8aa 1769 else {
032e9e29 1770 memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
7a70e8aa
DW
1771 fixup_container_spare_uuid(info);
1772 }
a5d85af7
N
1773
1774 /* I don't know how to compute 'map' on imsm, so use safe default */
1775 if (map) {
1776 int i;
1777 for (i = 0; i < map_disks; i++)
1778 map[i] = 1;
1779 }
1780
cdddbdbc
DW
1781}
1782
5c4cd5da
AC
1783/* allocates memory and fills disk in mdinfo structure
1784 * for each disk in array */
1785struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
1786{
1787 struct mdinfo *mddev = NULL;
1788 struct intel_super *super = st->sb;
1789 struct imsm_disk *disk;
1790 int count = 0;
1791 struct dl *dl;
1792 if (!super || !super->disks)
1793 return NULL;
1794 dl = super->disks;
1795 mddev = malloc(sizeof(*mddev));
1796 if (!mddev) {
1797 fprintf(stderr, Name ": Failed to allocate memory.\n");
1798 return NULL;
1799 }
1800 memset(mddev, 0, sizeof(*mddev));
1801 while (dl) {
1802 struct mdinfo *tmp;
1803 disk = &dl->disk;
1804 tmp = malloc(sizeof(*tmp));
1805 if (!tmp) {
1806 fprintf(stderr, Name ": Failed to allocate memory.\n");
1807 if (mddev)
1808 sysfs_free(mddev);
1809 return NULL;
1810 }
1811 memset(tmp, 0, sizeof(*tmp));
1812 if (mddev->devs)
1813 tmp->next = mddev->devs;
1814 mddev->devs = tmp;
1815 tmp->disk.number = count++;
1816 tmp->disk.major = dl->major;
1817 tmp->disk.minor = dl->minor;
1818 tmp->disk.state = is_configured(disk) ?
1819 (1 << MD_DISK_ACTIVE) : 0;
1820 tmp->disk.state |= is_failed(disk) ? (1 << MD_DISK_FAULTY) : 0;
1821 tmp->disk.state |= is_spare(disk) ? 0 : (1 << MD_DISK_SYNC);
1822 tmp->disk.raid_disk = -1;
1823 dl = dl->next;
1824 }
1825 return mddev;
1826}
1827
cdddbdbc
DW
1828static int update_super_imsm(struct supertype *st, struct mdinfo *info,
1829 char *update, char *devname, int verbose,
1830 int uuid_set, char *homehost)
1831{
f352c545
DW
1832 /* For 'assemble' and 'force' we need to return non-zero if any
1833 * change was made. For others, the return value is ignored.
1834 * Update options are:
1835 * force-one : This device looks a bit old but needs to be included,
1836 * update age info appropriately.
1837 * assemble: clear any 'faulty' flag to allow this device to
1838 * be assembled.
1839 * force-array: Array is degraded but being forced, mark it clean
1840 * if that will be needed to assemble it.
1841 *
1842 * newdev: not used ????
1843 * grow: Array has gained a new device - this is currently for
1844 * linear only
1845 * resync: mark as dirty so a resync will happen.
1846 * name: update the name - preserving the homehost
6e46bf34 1847 * uuid: Change the uuid of the array to match watch is given
f352c545
DW
1848 *
1849 * Following are not relevant for this imsm:
1850 * sparc2.2 : update from old dodgey metadata
1851 * super-minor: change the preferred_minor number
1852 * summaries: update redundant counters.
f352c545
DW
1853 * homehost: update the recorded homehost
1854 * _reshape_progress: record new reshape_progress position.
1855 */
6e46bf34
DW
1856 int rv = 1;
1857 struct intel_super *super = st->sb;
1858 struct imsm_super *mpb;
f352c545 1859
6e46bf34
DW
1860 /* we can only update container info */
1861 if (!super || super->current_vol >= 0 || !super->anchor)
1862 return 1;
1863
1864 mpb = super->anchor;
1865
1866 if (strcmp(update, "uuid") == 0 && uuid_set && !info->update_private)
1e2b2765 1867 rv = -1;
6e46bf34
DW
1868 else if (strcmp(update, "uuid") == 0 && uuid_set && info->update_private) {
1869 mpb->orig_family_num = *((__u32 *) info->update_private);
1870 rv = 0;
1871 } else if (strcmp(update, "uuid") == 0) {
1872 __u32 *new_family = malloc(sizeof(*new_family));
1873
1874 /* update orig_family_number with the incoming random
1875 * data, report the new effective uuid, and store the
1876 * new orig_family_num for future updates.
1877 */
1878 if (new_family) {
1879 memcpy(&mpb->orig_family_num, info->uuid, sizeof(__u32));
1880 uuid_from_super_imsm(st, info->uuid);
1881 *new_family = mpb->orig_family_num;
1882 info->update_private = new_family;
1883 rv = 0;
1884 }
1885 } else if (strcmp(update, "assemble") == 0)
1886 rv = 0;
1887 else
1e2b2765 1888 rv = -1;
f352c545 1889
6e46bf34
DW
1890 /* successful update? recompute checksum */
1891 if (rv == 0)
1892 mpb->check_sum = __le32_to_cpu(__gen_imsm_checksum(mpb));
f352c545
DW
1893
1894 return rv;
cdddbdbc
DW
1895}
1896
c2c087e6 1897static size_t disks_to_mpb_size(int disks)
cdddbdbc 1898{
c2c087e6 1899 size_t size;
cdddbdbc 1900
c2c087e6
DW
1901 size = sizeof(struct imsm_super);
1902 size += (disks - 1) * sizeof(struct imsm_disk);
1903 size += 2 * sizeof(struct imsm_dev);
1904 /* up to 2 maps per raid device (-2 for imsm_maps in imsm_dev */
1905 size += (4 - 2) * sizeof(struct imsm_map);
1906 /* 4 possible disk_ord_tbl's */
1907 size += 4 * (disks - 1) * sizeof(__u32);
1908
1909 return size;
1910}
1911
1912static __u64 avail_size_imsm(struct supertype *st, __u64 devsize)
1913{
1914 if (devsize < (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS))
1915 return 0;
1916
1917 return devsize - (MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS);
cdddbdbc
DW
1918}
1919
ba2de7ba
DW
1920static void free_devlist(struct intel_super *super)
1921{
1922 struct intel_dev *dv;
1923
1924 while (super->devlist) {
1925 dv = super->devlist->next;
1926 free(super->devlist->dev);
1927 free(super->devlist);
1928 super->devlist = dv;
1929 }
1930}
1931
1932static void imsm_copy_dev(struct imsm_dev *dest, struct imsm_dev *src)
1933{
1934 memcpy(dest, src, sizeof_imsm_dev(src, 0));
1935}
1936
cdddbdbc
DW
1937static int compare_super_imsm(struct supertype *st, struct supertype *tst)
1938{
1939 /*
1940 * return:
1941 * 0 same, or first was empty, and second was copied
1942 * 1 second had wrong number
1943 * 2 wrong uuid
1944 * 3 wrong other info
1945 */
1946 struct intel_super *first = st->sb;
1947 struct intel_super *sec = tst->sb;
1948
1949 if (!first) {
1950 st->sb = tst->sb;
1951 tst->sb = NULL;
1952 return 0;
1953 }
1954
d23fe947
DW
1955 /* if an anchor does not have num_raid_devs set then it is a free
1956 * floating spare
1957 */
1958 if (first->anchor->num_raid_devs > 0 &&
1959 sec->anchor->num_raid_devs > 0) {
a2b97981
DW
1960 /* Determine if these disks might ever have been
1961 * related. Further disambiguation can only take place
1962 * in load_super_imsm_all
1963 */
1964 __u32 first_family = first->anchor->orig_family_num;
1965 __u32 sec_family = sec->anchor->orig_family_num;
1966
f796af5d
DW
1967 if (memcmp(first->anchor->sig, sec->anchor->sig,
1968 MAX_SIGNATURE_LENGTH) != 0)
1969 return 3;
1970
a2b97981
DW
1971 if (first_family == 0)
1972 first_family = first->anchor->family_num;
1973 if (sec_family == 0)
1974 sec_family = sec->anchor->family_num;
1975
1976 if (first_family != sec_family)
d23fe947 1977 return 3;
f796af5d 1978
d23fe947 1979 }
cdddbdbc 1980
f796af5d 1981
3e372e5a
DW
1982 /* if 'first' is a spare promote it to a populated mpb with sec's
1983 * family number
1984 */
1985 if (first->anchor->num_raid_devs == 0 &&
1986 sec->anchor->num_raid_devs > 0) {
78d30f94 1987 int i;
ba2de7ba
DW
1988 struct intel_dev *dv;
1989 struct imsm_dev *dev;
78d30f94
DW
1990
1991 /* we need to copy raid device info from sec if an allocation
1992 * fails here we don't associate the spare
1993 */
1994 for (i = 0; i < sec->anchor->num_raid_devs; i++) {
ba2de7ba
DW
1995 dv = malloc(sizeof(*dv));
1996 if (!dv)
1997 break;
1998 dev = malloc(sizeof_imsm_dev(get_imsm_dev(sec, i), 1));
1999 if (!dev) {
2000 free(dv);
2001 break;
78d30f94 2002 }
ba2de7ba
DW
2003 dv->dev = dev;
2004 dv->index = i;
2005 dv->next = first->devlist;
2006 first->devlist = dv;
78d30f94 2007 }
709743c5 2008 if (i < sec->anchor->num_raid_devs) {
ba2de7ba
DW
2009 /* allocation failure */
2010 free_devlist(first);
2011 fprintf(stderr, "imsm: failed to associate spare\n");
2012 return 3;
78d30f94 2013 }
3e372e5a 2014 first->anchor->num_raid_devs = sec->anchor->num_raid_devs;
148acb7b 2015 first->anchor->orig_family_num = sec->anchor->orig_family_num;
3e372e5a 2016 first->anchor->family_num = sec->anchor->family_num;
ac6449be 2017 memcpy(first->anchor->sig, sec->anchor->sig, MAX_SIGNATURE_LENGTH);
709743c5
DW
2018 for (i = 0; i < sec->anchor->num_raid_devs; i++)
2019 imsm_copy_dev(get_imsm_dev(first, i), get_imsm_dev(sec, i));
3e372e5a
DW
2020 }
2021
cdddbdbc
DW
2022 return 0;
2023}
2024
0030e8d6
DW
2025static void fd2devname(int fd, char *name)
2026{
2027 struct stat st;
2028 char path[256];
33a6535d 2029 char dname[PATH_MAX];
0030e8d6
DW
2030 char *nm;
2031 int rv;
2032
2033 name[0] = '\0';
2034 if (fstat(fd, &st) != 0)
2035 return;
2036 sprintf(path, "/sys/dev/block/%d:%d",
2037 major(st.st_rdev), minor(st.st_rdev));
2038
2039 rv = readlink(path, dname, sizeof(dname));
2040 if (rv <= 0)
2041 return;
2042
2043 dname[rv] = '\0';
2044 nm = strrchr(dname, '/');
2045 nm++;
2046 snprintf(name, MAX_RAID_SERIAL_LEN, "/dev/%s", nm);
2047}
2048
cdddbdbc
DW
2049extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
2050
2051static int imsm_read_serial(int fd, char *devname,
2052 __u8 serial[MAX_RAID_SERIAL_LEN])
2053{
2054 unsigned char scsi_serial[255];
cdddbdbc
DW
2055 int rv;
2056 int rsp_len;
1f24f035 2057 int len;
316e2bf4
DW
2058 char *dest;
2059 char *src;
2060 char *rsp_buf;
2061 int i;
cdddbdbc
DW
2062
2063 memset(scsi_serial, 0, sizeof(scsi_serial));
cdddbdbc 2064
f9ba0ff1
DW
2065 rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
2066
40ebbb9c 2067 if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
f9ba0ff1
DW
2068 memset(serial, 0, MAX_RAID_SERIAL_LEN);
2069 fd2devname(fd, (char *) serial);
0030e8d6
DW
2070 return 0;
2071 }
2072
cdddbdbc
DW
2073 if (rv != 0) {
2074 if (devname)
2075 fprintf(stderr,
2076 Name ": Failed to retrieve serial for %s\n",
2077 devname);
2078 return rv;
2079 }
2080
2081 rsp_len = scsi_serial[3];
03cd4cc8
DW
2082 if (!rsp_len) {
2083 if (devname)
2084 fprintf(stderr,
2085 Name ": Failed to retrieve serial for %s\n",
2086 devname);
2087 return 2;
2088 }
1f24f035 2089 rsp_buf = (char *) &scsi_serial[4];
5c3db629 2090
316e2bf4
DW
2091 /* trim all whitespace and non-printable characters and convert
2092 * ':' to ';'
2093 */
2094 for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
2095 src = &rsp_buf[i];
2096 if (*src > 0x20) {
2097 /* ':' is reserved for use in placeholder serial
2098 * numbers for missing disks
2099 */
2100 if (*src == ':')
2101 *dest++ = ';';
2102 else
2103 *dest++ = *src;
2104 }
2105 }
2106 len = dest - rsp_buf;
2107 dest = rsp_buf;
2108
2109 /* truncate leading characters */
2110 if (len > MAX_RAID_SERIAL_LEN) {
2111 dest += len - MAX_RAID_SERIAL_LEN;
1f24f035 2112 len = MAX_RAID_SERIAL_LEN;
316e2bf4 2113 }
5c3db629 2114
5c3db629 2115 memset(serial, 0, MAX_RAID_SERIAL_LEN);
316e2bf4 2116 memcpy(serial, dest, len);
cdddbdbc
DW
2117
2118 return 0;
2119}
2120
1f24f035
DW
2121static int serialcmp(__u8 *s1, __u8 *s2)
2122{
2123 return strncmp((char *) s1, (char *) s2, MAX_RAID_SERIAL_LEN);
2124}
2125
2126static void serialcpy(__u8 *dest, __u8 *src)
2127{
2128 strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
2129}
2130
1799c9e8 2131#ifndef MDASSEMBLE
54c2c1ea
DW
2132static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
2133{
2134 struct dl *dl;
2135
2136 for (dl = super->disks; dl; dl = dl->next)
2137 if (serialcmp(dl->serial, serial) == 0)
2138 break;
2139
2140 return dl;
2141}
1799c9e8 2142#endif
54c2c1ea 2143
a2b97981
DW
2144static struct imsm_disk *
2145__serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
2146{
2147 int i;
2148
2149 for (i = 0; i < mpb->num_disks; i++) {
2150 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2151
2152 if (serialcmp(disk->serial, serial) == 0) {
2153 if (idx)
2154 *idx = i;
2155 return disk;
2156 }
2157 }
2158
2159 return NULL;
2160}
2161
cdddbdbc
DW
2162static int
2163load_imsm_disk(int fd, struct intel_super *super, char *devname, int keep_fd)
2164{
a2b97981 2165 struct imsm_disk *disk;
cdddbdbc
DW
2166 struct dl *dl;
2167 struct stat stb;
cdddbdbc 2168 int rv;
a2b97981 2169 char name[40];
d23fe947
DW
2170 __u8 serial[MAX_RAID_SERIAL_LEN];
2171
2172 rv = imsm_read_serial(fd, devname, serial);
2173
2174 if (rv != 0)
2175 return 2;
2176
a2b97981 2177 dl = calloc(1, sizeof(*dl));
b9f594fe 2178 if (!dl) {
cdddbdbc
DW
2179 if (devname)
2180 fprintf(stderr,
2181 Name ": failed to allocate disk buffer for %s\n",
2182 devname);
2183 return 2;
2184 }
cdddbdbc 2185
a2b97981
DW
2186 fstat(fd, &stb);
2187 dl->major = major(stb.st_rdev);
2188 dl->minor = minor(stb.st_rdev);
2189 dl->next = super->disks;
2190 dl->fd = keep_fd ? fd : -1;
2191 assert(super->disks == NULL);
2192 super->disks = dl;
2193 serialcpy(dl->serial, serial);
2194 dl->index = -2;
2195 dl->e = NULL;
2196 fd2devname(fd, name);
2197 if (devname)
2198 dl->devname = strdup(devname);
2199 else
2200 dl->devname = strdup(name);
cdddbdbc 2201
d23fe947 2202 /* look up this disk's index in the current anchor */
a2b97981
DW
2203 disk = __serial_to_disk(dl->serial, super->anchor, &dl->index);
2204 if (disk) {
2205 dl->disk = *disk;
2206 /* only set index on disks that are a member of a
2207 * populated contianer, i.e. one with raid_devs
2208 */
2209 if (is_failed(&dl->disk))
3f6efecc 2210 dl->index = -2;
a2b97981
DW
2211 else if (is_spare(&dl->disk))
2212 dl->index = -1;
3f6efecc
DW
2213 }
2214
949c47a0
DW
2215 return 0;
2216}
2217
0e600426 2218#ifndef MDASSEMBLE
0c046afd
DW
2219/* When migrating map0 contains the 'destination' state while map1
2220 * contains the current state. When not migrating map0 contains the
2221 * current state. This routine assumes that map[0].map_state is set to
2222 * the current array state before being called.
2223 *
2224 * Migration is indicated by one of the following states
2225 * 1/ Idle (migr_state=0 map0state=normal||unitialized||degraded||failed)
e3bba0e0 2226 * 2/ Initialize (migr_state=1 migr_type=MIGR_INIT map0state=normal
0c046afd 2227 * map1state=unitialized)
1484e727 2228 * 3/ Repair (Resync) (migr_state=1 migr_type=MIGR_REPAIR map0state=normal
0c046afd 2229 * map1state=normal)
e3bba0e0 2230 * 4/ Rebuild (migr_state=1 migr_type=MIGR_REBUILD map0state=normal
0c046afd
DW
2231 * map1state=degraded)
2232 */
0556e1a2 2233static void migrate(struct imsm_dev *dev, __u8 to_state, int migr_type)
3393c6af 2234{
0c046afd 2235 struct imsm_map *dest;
3393c6af
DW
2236 struct imsm_map *src = get_imsm_map(dev, 0);
2237
0c046afd 2238 dev->vol.migr_state = 1;
1484e727 2239 set_migr_type(dev, migr_type);
f8f603f1 2240 dev->vol.curr_migr_unit = 0;
0c046afd
DW
2241 dest = get_imsm_map(dev, 1);
2242
0556e1a2 2243 /* duplicate and then set the target end state in map[0] */
3393c6af 2244 memcpy(dest, src, sizeof_imsm_map(src));
28bce06f
AK
2245 if ((migr_type == MIGR_REBUILD) ||
2246 (migr_type == MIGR_GEN_MIGR)) {
0556e1a2
DW
2247 __u32 ord;
2248 int i;
2249
2250 for (i = 0; i < src->num_members; i++) {
2251 ord = __le32_to_cpu(src->disk_ord_tbl[i]);
2252 set_imsm_ord_tbl_ent(src, i, ord_to_idx(ord));
2253 }
2254 }
2255
0c046afd 2256 src->map_state = to_state;
949c47a0 2257}
f8f603f1
DW
2258
2259static void end_migration(struct imsm_dev *dev, __u8 map_state)
2260{
2261 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2 2262 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
28bce06f 2263 int i, j;
0556e1a2
DW
2264
2265 /* merge any IMSM_ORD_REBUILD bits that were not successfully
2266 * completed in the last migration.
2267 *
28bce06f 2268 * FIXME add support for raid-level-migration
0556e1a2
DW
2269 */
2270 for (i = 0; i < prev->num_members; i++)
28bce06f
AK
2271 for (j = 0; j < map->num_members; j++)
2272 /* during online capacity expansion
2273 * disks position can be changed if takeover is used
2274 */
2275 if (ord_to_idx(map->disk_ord_tbl[j]) ==
2276 ord_to_idx(prev->disk_ord_tbl[i])) {
2277 map->disk_ord_tbl[j] |= prev->disk_ord_tbl[i];
2278 break;
2279 }
f8f603f1
DW
2280
2281 dev->vol.migr_state = 0;
28bce06f 2282 dev->vol.migr_type = 0;
f8f603f1
DW
2283 dev->vol.curr_migr_unit = 0;
2284 map->map_state = map_state;
2285}
0e600426 2286#endif
949c47a0
DW
2287
2288static int parse_raid_devices(struct intel_super *super)
2289{
2290 int i;
2291 struct imsm_dev *dev_new;
4d7b1503
DW
2292 size_t len, len_migr;
2293 size_t space_needed = 0;
2294 struct imsm_super *mpb = super->anchor;
949c47a0
DW
2295
2296 for (i = 0; i < super->anchor->num_raid_devs; i++) {
2297 struct imsm_dev *dev_iter = __get_imsm_dev(super->anchor, i);
ba2de7ba 2298 struct intel_dev *dv;
949c47a0 2299
4d7b1503
DW
2300 len = sizeof_imsm_dev(dev_iter, 0);
2301 len_migr = sizeof_imsm_dev(dev_iter, 1);
2302 if (len_migr > len)
2303 space_needed += len_migr - len;
2304
ba2de7ba
DW
2305 dv = malloc(sizeof(*dv));
2306 if (!dv)
2307 return 1;
4d7b1503 2308 dev_new = malloc(len_migr);
ba2de7ba
DW
2309 if (!dev_new) {
2310 free(dv);
949c47a0 2311 return 1;
ba2de7ba 2312 }
949c47a0 2313 imsm_copy_dev(dev_new, dev_iter);
ba2de7ba
DW
2314 dv->dev = dev_new;
2315 dv->index = i;
2316 dv->next = super->devlist;
2317 super->devlist = dv;
949c47a0 2318 }
cdddbdbc 2319
4d7b1503
DW
2320 /* ensure that super->buf is large enough when all raid devices
2321 * are migrating
2322 */
2323 if (__le32_to_cpu(mpb->mpb_size) + space_needed > super->len) {
2324 void *buf;
2325
2326 len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + space_needed, 512);
2327 if (posix_memalign(&buf, 512, len) != 0)
2328 return 1;
2329
1f45a8ad
DW
2330 memcpy(buf, super->buf, super->len);
2331 memset(buf + super->len, 0, len - super->len);
4d7b1503
DW
2332 free(super->buf);
2333 super->buf = buf;
2334 super->len = len;
2335 }
2336
cdddbdbc
DW
2337 return 0;
2338}
2339
604b746f
JD
2340/* retrieve a pointer to the bbm log which starts after all raid devices */
2341struct bbm_log *__get_imsm_bbm_log(struct imsm_super *mpb)
2342{
2343 void *ptr = NULL;
2344
2345 if (__le32_to_cpu(mpb->bbm_log_size)) {
2346 ptr = mpb;
2347 ptr += mpb->mpb_size - __le32_to_cpu(mpb->bbm_log_size);
2348 }
2349
2350 return ptr;
2351}
2352
d23fe947 2353static void __free_imsm(struct intel_super *super, int free_disks);
9ca2c81c 2354
cdddbdbc
DW
2355/* load_imsm_mpb - read matrix metadata
2356 * allocates super->mpb to be freed by free_super
2357 */
2358static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
2359{
2360 unsigned long long dsize;
cdddbdbc
DW
2361 unsigned long long sectors;
2362 struct stat;
6416d527 2363 struct imsm_super *anchor;
cdddbdbc
DW
2364 __u32 check_sum;
2365
cdddbdbc 2366 get_dev_size(fd, NULL, &dsize);
64436f06
N
2367 if (dsize < 1024) {
2368 if (devname)
2369 fprintf(stderr,
2370 Name ": %s: device to small for imsm\n",
2371 devname);
2372 return 1;
2373 }
cdddbdbc
DW
2374
2375 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
2376 if (devname)
2377 fprintf(stderr,
2378 Name ": Cannot seek to anchor block on %s: %s\n",
2379 devname, strerror(errno));
2380 return 1;
2381 }
2382
949c47a0 2383 if (posix_memalign((void**)&anchor, 512, 512) != 0) {
ad97895e
DW
2384 if (devname)
2385 fprintf(stderr,
2386 Name ": Failed to allocate imsm anchor buffer"
2387 " on %s\n", devname);
2388 return 1;
2389 }
949c47a0 2390 if (read(fd, anchor, 512) != 512) {
cdddbdbc
DW
2391 if (devname)
2392 fprintf(stderr,
2393 Name ": Cannot read anchor block on %s: %s\n",
2394 devname, strerror(errno));
6416d527 2395 free(anchor);
cdddbdbc
DW
2396 return 1;
2397 }
2398
6416d527 2399 if (strncmp((char *) anchor->sig, MPB_SIGNATURE, MPB_SIG_LEN) != 0) {
cdddbdbc
DW
2400 if (devname)
2401 fprintf(stderr,
2402 Name ": no IMSM anchor on %s\n", devname);
6416d527 2403 free(anchor);
cdddbdbc
DW
2404 return 2;
2405 }
2406
d23fe947 2407 __free_imsm(super, 0);
949c47a0
DW
2408 super->len = ROUND_UP(anchor->mpb_size, 512);
2409 if (posix_memalign(&super->buf, 512, super->len) != 0) {
cdddbdbc
DW
2410 if (devname)
2411 fprintf(stderr,
2412 Name ": unable to allocate %zu byte mpb buffer\n",
949c47a0 2413 super->len);
6416d527 2414 free(anchor);
cdddbdbc
DW
2415 return 2;
2416 }
949c47a0 2417 memcpy(super->buf, anchor, 512);
cdddbdbc 2418
6416d527
NB
2419 sectors = mpb_sectors(anchor) - 1;
2420 free(anchor);
949c47a0 2421 if (!sectors) {
ecf45690
DW
2422 check_sum = __gen_imsm_checksum(super->anchor);
2423 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
2424 if (devname)
2425 fprintf(stderr,
2426 Name ": IMSM checksum %x != %x on %s\n",
2427 check_sum,
2428 __le32_to_cpu(super->anchor->check_sum),
2429 devname);
2430 return 2;
2431 }
2432
a2b97981 2433 return 0;
949c47a0 2434 }
cdddbdbc
DW
2435
2436 /* read the extended mpb */
2437 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0) {
2438 if (devname)
2439 fprintf(stderr,
2440 Name ": Cannot seek to extended mpb on %s: %s\n",
2441 devname, strerror(errno));
2442 return 1;
2443 }
2444
f21e18ca 2445 if ((unsigned)read(fd, super->buf + 512, super->len - 512) != super->len - 512) {
cdddbdbc
DW
2446 if (devname)
2447 fprintf(stderr,
2448 Name ": Cannot read extended mpb on %s: %s\n",
2449 devname, strerror(errno));
2450 return 2;
2451 }
2452
949c47a0
DW
2453 check_sum = __gen_imsm_checksum(super->anchor);
2454 if (check_sum != __le32_to_cpu(super->anchor->check_sum)) {
cdddbdbc
DW
2455 if (devname)
2456 fprintf(stderr,
2457 Name ": IMSM checksum %x != %x on %s\n",
949c47a0 2458 check_sum, __le32_to_cpu(super->anchor->check_sum),
cdddbdbc 2459 devname);
db575f3b 2460 return 3;
cdddbdbc
DW
2461 }
2462
604b746f
JD
2463 /* FIXME the BBM log is disk specific so we cannot use this global
2464 * buffer for all disks. Ok for now since we only look at the global
2465 * bbm_log_size parameter to gate assembly
2466 */
2467 super->bbm_log = __get_imsm_bbm_log(super->anchor);
2468
a2b97981
DW
2469 return 0;
2470}
2471
2472static int
2473load_and_parse_mpb(int fd, struct intel_super *super, char *devname, int keep_fd)
2474{
2475 int err;
2476
2477 err = load_imsm_mpb(fd, super, devname);
2478 if (err)
2479 return err;
2480 err = load_imsm_disk(fd, super, devname, keep_fd);
2481 if (err)
2482 return err;
2483 err = parse_raid_devices(super);
4d7b1503 2484
a2b97981 2485 return err;
cdddbdbc
DW
2486}
2487
ae6aad82
DW
2488static void __free_imsm_disk(struct dl *d)
2489{
2490 if (d->fd >= 0)
2491 close(d->fd);
2492 if (d->devname)
2493 free(d->devname);
0dcecb2e
DW
2494 if (d->e)
2495 free(d->e);
ae6aad82
DW
2496 free(d);
2497
2498}
1a64be56 2499
cdddbdbc
DW
2500static void free_imsm_disks(struct intel_super *super)
2501{
47ee5a45 2502 struct dl *d;
cdddbdbc 2503
47ee5a45
DW
2504 while (super->disks) {
2505 d = super->disks;
cdddbdbc 2506 super->disks = d->next;
ae6aad82 2507 __free_imsm_disk(d);
cdddbdbc 2508 }
47ee5a45
DW
2509 while (super->missing) {
2510 d = super->missing;
2511 super->missing = d->next;
2512 __free_imsm_disk(d);
2513 }
2514
cdddbdbc
DW
2515}
2516
9ca2c81c 2517/* free all the pieces hanging off of a super pointer */
d23fe947 2518static void __free_imsm(struct intel_super *super, int free_disks)
cdddbdbc 2519{
9ca2c81c 2520 if (super->buf) {
949c47a0 2521 free(super->buf);
9ca2c81c
DW
2522 super->buf = NULL;
2523 }
d23fe947
DW
2524 if (free_disks)
2525 free_imsm_disks(super);
ba2de7ba 2526 free_devlist(super);
88c32bb1
DW
2527 if (super->hba) {
2528 free((void *) super->hba);
2529 super->hba = NULL;
2530 }
cdddbdbc
DW
2531}
2532
9ca2c81c
DW
2533static void free_imsm(struct intel_super *super)
2534{
d23fe947 2535 __free_imsm(super, 1);
9ca2c81c
DW
2536 free(super);
2537}
cdddbdbc
DW
2538
2539static void free_super_imsm(struct supertype *st)
2540{
2541 struct intel_super *super = st->sb;
2542
2543 if (!super)
2544 return;
2545
2546 free_imsm(super);
2547 st->sb = NULL;
2548}
2549
49133e57 2550static struct intel_super *alloc_super(void)
c2c087e6
DW
2551{
2552 struct intel_super *super = malloc(sizeof(*super));
2553
2554 if (super) {
2555 memset(super, 0, sizeof(*super));
bf5a934a 2556 super->current_vol = -1;
0dcecb2e 2557 super->create_offset = ~((__u32 ) 0);
88c32bb1
DW
2558 if (!check_env("IMSM_NO_PLATFORM"))
2559 super->orom = find_imsm_orom();
cceebc67 2560 if (super->orom && !check_env("IMSM_TEST_OROM")) {
88c32bb1
DW
2561 struct sys_dev *list, *ent;
2562
2563 /* find the first intel ahci controller */
2564 list = find_driver_devices("pci", "ahci");
2565 for (ent = list; ent; ent = ent->next)
2566 if (devpath_to_vendor(ent->path) == 0x8086)
2567 break;
2568 if (ent) {
2569 super->hba = ent->path;
2570 ent->path = NULL;
2571 }
2572 free_sys_dev(&list);
2573 }
c2c087e6
DW
2574 }
2575
2576 return super;
2577}
2578
cdddbdbc 2579#ifndef MDASSEMBLE
47ee5a45
DW
2580/* find_missing - helper routine for load_super_imsm_all that identifies
2581 * disks that have disappeared from the system. This routine relies on
2582 * the mpb being uptodate, which it is at load time.
2583 */
2584static int find_missing(struct intel_super *super)
2585{
2586 int i;
2587 struct imsm_super *mpb = super->anchor;
2588 struct dl *dl;
2589 struct imsm_disk *disk;
47ee5a45
DW
2590
2591 for (i = 0; i < mpb->num_disks; i++) {
2592 disk = __get_imsm_disk(mpb, i);
54c2c1ea 2593 dl = serial_to_dl(disk->serial, super);
47ee5a45
DW
2594 if (dl)
2595 continue;
47ee5a45
DW
2596
2597 dl = malloc(sizeof(*dl));
2598 if (!dl)
2599 return 1;
2600 dl->major = 0;
2601 dl->minor = 0;
2602 dl->fd = -1;
2603 dl->devname = strdup("missing");
2604 dl->index = i;
2605 serialcpy(dl->serial, disk->serial);
2606 dl->disk = *disk;
689c9bf3 2607 dl->e = NULL;
47ee5a45
DW
2608 dl->next = super->missing;
2609 super->missing = dl;
2610 }
2611
2612 return 0;
2613}
2614
a2b97981
DW
2615static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
2616{
2617 struct intel_disk *idisk = disk_list;
2618
2619 while (idisk) {
2620 if (serialcmp(idisk->disk.serial, serial) == 0)
2621 break;
2622 idisk = idisk->next;
2623 }
2624
2625 return idisk;
2626}
2627
2628static int __prep_thunderdome(struct intel_super **table, int tbl_size,
2629 struct intel_super *super,
2630 struct intel_disk **disk_list)
2631{
2632 struct imsm_disk *d = &super->disks->disk;
2633 struct imsm_super *mpb = super->anchor;
2634 int i, j;
2635
2636 for (i = 0; i < tbl_size; i++) {
2637 struct imsm_super *tbl_mpb = table[i]->anchor;
2638 struct imsm_disk *tbl_d = &table[i]->disks->disk;
2639
2640 if (tbl_mpb->family_num == mpb->family_num) {
2641 if (tbl_mpb->check_sum == mpb->check_sum) {
2642 dprintf("%s: mpb from %d:%d matches %d:%d\n",
2643 __func__, super->disks->major,
2644 super->disks->minor,
2645 table[i]->disks->major,
2646 table[i]->disks->minor);
2647 break;
2648 }
2649
2650 if (((is_configured(d) && !is_configured(tbl_d)) ||
2651 is_configured(d) == is_configured(tbl_d)) &&
2652 tbl_mpb->generation_num < mpb->generation_num) {
2653 /* current version of the mpb is a
2654 * better candidate than the one in
2655 * super_table, but copy over "cross
2656 * generational" status
2657 */
2658 struct intel_disk *idisk;
2659
2660 dprintf("%s: mpb from %d:%d replaces %d:%d\n",
2661 __func__, super->disks->major,
2662 super->disks->minor,
2663 table[i]->disks->major,
2664 table[i]->disks->minor);
2665
2666 idisk = disk_list_get(tbl_d->serial, *disk_list);
2667 if (idisk && is_failed(&idisk->disk))
2668 tbl_d->status |= FAILED_DISK;
2669 break;
2670 } else {
2671 struct intel_disk *idisk;
2672 struct imsm_disk *disk;
2673
2674 /* tbl_mpb is more up to date, but copy
2675 * over cross generational status before
2676 * returning
2677 */
2678 disk = __serial_to_disk(d->serial, mpb, NULL);
2679 if (disk && is_failed(disk))
2680 d->status |= FAILED_DISK;
2681
2682 idisk = disk_list_get(d->serial, *disk_list);
2683 if (idisk) {
2684 idisk->owner = i;
2685 if (disk && is_configured(disk))
2686 idisk->disk.status |= CONFIGURED_DISK;
2687 }
2688
2689 dprintf("%s: mpb from %d:%d prefer %d:%d\n",
2690 __func__, super->disks->major,
2691 super->disks->minor,
2692 table[i]->disks->major,
2693 table[i]->disks->minor);
2694
2695 return tbl_size;
2696 }
2697 }
2698 }
2699
2700 if (i >= tbl_size)
2701 table[tbl_size++] = super;
2702 else
2703 table[i] = super;
2704
2705 /* update/extend the merged list of imsm_disk records */
2706 for (j = 0; j < mpb->num_disks; j++) {
2707 struct imsm_disk *disk = __get_imsm_disk(mpb, j);
2708 struct intel_disk *idisk;
2709
2710 idisk = disk_list_get(disk->serial, *disk_list);
2711 if (idisk) {
2712 idisk->disk.status |= disk->status;
2713 if (is_configured(&idisk->disk) ||
2714 is_failed(&idisk->disk))
2715 idisk->disk.status &= ~(SPARE_DISK);
2716 } else {
2717 idisk = calloc(1, sizeof(*idisk));
2718 if (!idisk)
2719 return -1;
2720 idisk->owner = IMSM_UNKNOWN_OWNER;
2721 idisk->disk = *disk;
2722 idisk->next = *disk_list;
2723 *disk_list = idisk;
2724 }
2725
2726 if (serialcmp(idisk->disk.serial, d->serial) == 0)
2727 idisk->owner = i;
2728 }
2729
2730 return tbl_size;
2731}
2732
2733static struct intel_super *
2734validate_members(struct intel_super *super, struct intel_disk *disk_list,
2735 const int owner)
2736{
2737 struct imsm_super *mpb = super->anchor;
2738 int ok_count = 0;
2739 int i;
2740
2741 for (i = 0; i < mpb->num_disks; i++) {
2742 struct imsm_disk *disk = __get_imsm_disk(mpb, i);
2743 struct intel_disk *idisk;
2744
2745 idisk = disk_list_get(disk->serial, disk_list);
2746 if (idisk) {
2747 if (idisk->owner == owner ||
2748 idisk->owner == IMSM_UNKNOWN_OWNER)
2749 ok_count++;
2750 else
2751 dprintf("%s: '%.16s' owner %d != %d\n",
2752 __func__, disk->serial, idisk->owner,
2753 owner);
2754 } else {
2755 dprintf("%s: unknown disk %x [%d]: %.16s\n",
2756 __func__, __le32_to_cpu(mpb->family_num), i,
2757 disk->serial);
2758 break;
2759 }
2760 }
2761
2762 if (ok_count == mpb->num_disks)
2763 return super;
2764 return NULL;
2765}
2766
2767static void show_conflicts(__u32 family_num, struct intel_super *super_list)
2768{
2769 struct intel_super *s;
2770
2771 for (s = super_list; s; s = s->next) {
2772 if (family_num != s->anchor->family_num)
2773 continue;
2774 fprintf(stderr, "Conflict, offlining family %#x on '%s'\n",
2775 __le32_to_cpu(family_num), s->disks->devname);
2776 }
2777}
2778
2779static struct intel_super *
2780imsm_thunderdome(struct intel_super **super_list, int len)
2781{
2782 struct intel_super *super_table[len];
2783 struct intel_disk *disk_list = NULL;
2784 struct intel_super *champion, *spare;
2785 struct intel_super *s, **del;
2786 int tbl_size = 0;
2787 int conflict;
2788 int i;
2789
2790 memset(super_table, 0, sizeof(super_table));
2791 for (s = *super_list; s; s = s->next)
2792 tbl_size = __prep_thunderdome(super_table, tbl_size, s, &disk_list);
2793
2794 for (i = 0; i < tbl_size; i++) {
2795 struct imsm_disk *d;
2796 struct intel_disk *idisk;
2797 struct imsm_super *mpb = super_table[i]->anchor;
2798
2799 s = super_table[i];
2800 d = &s->disks->disk;
2801
2802 /* 'd' must appear in merged disk list for its
2803 * configuration to be valid
2804 */
2805 idisk = disk_list_get(d->serial, disk_list);
2806 if (idisk && idisk->owner == i)
2807 s = validate_members(s, disk_list, i);
2808 else
2809 s = NULL;
2810
2811 if (!s)
2812 dprintf("%s: marking family: %#x from %d:%d offline\n",
2813 __func__, mpb->family_num,
2814 super_table[i]->disks->major,
2815 super_table[i]->disks->minor);
2816 super_table[i] = s;
2817 }
2818
2819 /* This is where the mdadm implementation differs from the Windows
2820 * driver which has no strict concept of a container. We can only
2821 * assemble one family from a container, so when returning a prodigal
2822 * array member to this system the code will not be able to disambiguate
2823 * the container contents that should be assembled ("foreign" versus
2824 * "local"). It requires user intervention to set the orig_family_num
2825 * to a new value to establish a new container. The Windows driver in
2826 * this situation fixes up the volume name in place and manages the
2827 * foreign array as an independent entity.
2828 */
2829 s = NULL;
2830 spare = NULL;
2831 conflict = 0;
2832 for (i = 0; i < tbl_size; i++) {
2833 struct intel_super *tbl_ent = super_table[i];
2834 int is_spare = 0;
2835
2836 if (!tbl_ent)
2837 continue;
2838
2839 if (tbl_ent->anchor->num_raid_devs == 0) {
2840 spare = tbl_ent;
2841 is_spare = 1;
2842 }
2843
2844 if (s && !is_spare) {
2845 show_conflicts(tbl_ent->anchor->family_num, *super_list);
2846 conflict++;
2847 } else if (!s && !is_spare)
2848 s = tbl_ent;
2849 }
2850
2851 if (!s)
2852 s = spare;
2853 if (!s) {
2854 champion = NULL;
2855 goto out;
2856 }
2857 champion = s;
2858
2859 if (conflict)
2860 fprintf(stderr, "Chose family %#x on '%s', "
2861 "assemble conflicts to new container with '--update=uuid'\n",
2862 __le32_to_cpu(s->anchor->family_num), s->disks->devname);
2863
2864 /* collect all dl's onto 'champion', and update them to
2865 * champion's version of the status
2866 */
2867 for (s = *super_list; s; s = s->next) {
2868 struct imsm_super *mpb = champion->anchor;
2869 struct dl *dl = s->disks;
2870
2871 if (s == champion)
2872 continue;
2873
2874 for (i = 0; i < mpb->num_disks; i++) {
2875 struct imsm_disk *disk;
2876
2877 disk = __serial_to_disk(dl->serial, mpb, &dl->index);
2878 if (disk) {
2879 dl->disk = *disk;
2880 /* only set index on disks that are a member of
2881 * a populated contianer, i.e. one with
2882 * raid_devs
2883 */
2884 if (is_failed(&dl->disk))
2885 dl->index = -2;
2886 else if (is_spare(&dl->disk))
2887 dl->index = -1;
2888 break;
2889 }
2890 }
2891
2892 if (i >= mpb->num_disks) {
2893 struct intel_disk *idisk;
2894
2895 idisk = disk_list_get(dl->serial, disk_list);
ecf408e9 2896 if (idisk && is_spare(&idisk->disk) &&
a2b97981
DW
2897 !is_failed(&idisk->disk) && !is_configured(&idisk->disk))
2898 dl->index = -1;
2899 else {
2900 dl->index = -2;
2901 continue;
2902 }
2903 }
2904
2905 dl->next = champion->disks;
2906 champion->disks = dl;
2907 s->disks = NULL;
2908 }
2909
2910 /* delete 'champion' from super_list */
2911 for (del = super_list; *del; ) {
2912 if (*del == champion) {
2913 *del = (*del)->next;
2914 break;
2915 } else
2916 del = &(*del)->next;
2917 }
2918 champion->next = NULL;
2919
2920 out:
2921 while (disk_list) {
2922 struct intel_disk *idisk = disk_list;
2923
2924 disk_list = disk_list->next;
2925 free(idisk);
2926 }
2927
2928 return champion;
2929}
2930
cdddbdbc 2931static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
e1902a7b 2932 char *devname)
cdddbdbc
DW
2933{
2934 struct mdinfo *sra;
a2b97981
DW
2935 struct intel_super *super_list = NULL;
2936 struct intel_super *super = NULL;
db575f3b 2937 int devnum = fd2devnum(fd);
a2b97981 2938 struct mdinfo *sd;
db575f3b 2939 int retry;
a2b97981
DW
2940 int err = 0;
2941 int i;
dab4a513
DW
2942
2943 /* check if 'fd' an opened container */
b526e52d 2944 sra = sysfs_read(fd, 0, GET_LEVEL|GET_VERSION|GET_DEVS|GET_STATE);
cdddbdbc
DW
2945 if (!sra)
2946 return 1;
2947
2948 if (sra->array.major_version != -1 ||
2949 sra->array.minor_version != -2 ||
1602d52c
AW
2950 strcmp(sra->text_version, "imsm") != 0) {
2951 err = 1;
2952 goto error;
2953 }
a2b97981
DW
2954 /* load all mpbs */
2955 for (sd = sra->devs, i = 0; sd; sd = sd->next, i++) {
49133e57 2956 struct intel_super *s = alloc_super();
7a6ecd55 2957 char nm[32];
a2b97981
DW
2958 int dfd;
2959
2960 err = 1;
2961 if (!s)
2962 goto error;
2963 s->next = super_list;
2964 super_list = s;
cdddbdbc 2965
a2b97981 2966 err = 2;
cdddbdbc 2967 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
e1902a7b 2968 dfd = dev_open(nm, O_RDWR);
a2b97981
DW
2969 if (dfd < 0)
2970 goto error;
2971
e1902a7b 2972 err = load_and_parse_mpb(dfd, s, NULL, 1);
db575f3b
DW
2973
2974 /* retry the load if we might have raced against mdmon */
a2b97981 2975 if (err == 3 && mdmon_running(devnum))
db575f3b
DW
2976 for (retry = 0; retry < 3; retry++) {
2977 usleep(3000);
e1902a7b 2978 err = load_and_parse_mpb(dfd, s, NULL, 1);
a2b97981 2979 if (err != 3)
db575f3b
DW
2980 break;
2981 }
a2b97981
DW
2982 if (err)
2983 goto error;
cdddbdbc
DW
2984 }
2985
a2b97981
DW
2986 /* all mpbs enter, maybe one leaves */
2987 super = imsm_thunderdome(&super_list, i);
2988 if (!super) {
2989 err = 1;
2990 goto error;
cdddbdbc
DW
2991 }
2992
47ee5a45
DW
2993 if (find_missing(super) != 0) {
2994 free_imsm(super);
a2b97981
DW
2995 err = 2;
2996 goto error;
47ee5a45 2997 }
a2b97981
DW
2998 err = 0;
2999
3000 error:
3001 while (super_list) {
3002 struct intel_super *s = super_list;
3003
3004 super_list = super_list->next;
3005 free_imsm(s);
3006 }
1602d52c 3007 sysfs_free(sra);
a2b97981
DW
3008
3009 if (err)
3010 return err;
f7e7067b 3011
cdddbdbc 3012 *sbp = super;
db575f3b 3013 st->container_dev = devnum;
a2b97981 3014 if (err == 0 && st->ss == NULL) {
bf5a934a 3015 st->ss = &super_imsm;
cdddbdbc
DW
3016 st->minor_version = 0;
3017 st->max_devs = IMSM_MAX_DEVICES;
3018 }
cdddbdbc
DW
3019 return 0;
3020}
2b959fbf
N
3021
3022static int load_container_imsm(struct supertype *st, int fd, char *devname)
3023{
3024 return load_super_imsm_all(st, fd, &st->sb, devname);
3025}
cdddbdbc
DW
3026#endif
3027
3028static int load_super_imsm(struct supertype *st, int fd, char *devname)
3029{
3030 struct intel_super *super;
3031 int rv;
3032
3033#ifndef MDASSEMBLE
e1902a7b 3034 if (load_super_imsm_all(st, fd, &st->sb, devname) == 0)
cdddbdbc
DW
3035 return 0;
3036#endif
3037
691c6ee1
N
3038 if (test_partition(fd))
3039 /* IMSM not allowed on partitions */
3040 return 1;
3041
37424f13
DW
3042 free_super_imsm(st);
3043
49133e57 3044 super = alloc_super();
cdddbdbc
DW
3045 if (!super) {
3046 fprintf(stderr,
3047 Name ": malloc of %zu failed.\n",
3048 sizeof(*super));
3049 return 1;
3050 }
3051
a2b97981 3052 rv = load_and_parse_mpb(fd, super, devname, 0);
cdddbdbc
DW
3053
3054 if (rv) {
3055 if (devname)
3056 fprintf(stderr,
3057 Name ": Failed to load all information "
3058 "sections on %s\n", devname);
3059 free_imsm(super);
3060 return rv;
3061 }
3062
3063 st->sb = super;
3064 if (st->ss == NULL) {
3065 st->ss = &super_imsm;
3066 st->minor_version = 0;
3067 st->max_devs = IMSM_MAX_DEVICES;
3068 }
cdddbdbc
DW
3069 return 0;
3070}
3071
ef6ffade
DW
3072static __u16 info_to_blocks_per_strip(mdu_array_info_t *info)
3073{
3074 if (info->level == 1)
3075 return 128;
3076 return info->chunk_size >> 9;
3077}
3078
ff596308 3079static __u32 info_to_num_data_stripes(mdu_array_info_t *info, int num_domains)
ef6ffade
DW
3080{
3081 __u32 num_stripes;
3082
3083 num_stripes = (info->size * 2) / info_to_blocks_per_strip(info);
ff596308 3084 num_stripes /= num_domains;
ef6ffade
DW
3085
3086 return num_stripes;
3087}
3088
fcfd9599
DW
3089static __u32 info_to_blocks_per_member(mdu_array_info_t *info)
3090{
4025c288
DW
3091 if (info->level == 1)
3092 return info->size * 2;
3093 else
3094 return (info->size * 2) & ~(info_to_blocks_per_strip(info) - 1);
fcfd9599
DW
3095}
3096
4d1313e9
DW
3097static void imsm_update_version_info(struct intel_super *super)
3098{
3099 /* update the version and attributes */
3100 struct imsm_super *mpb = super->anchor;
3101 char *version;
3102 struct imsm_dev *dev;
3103 struct imsm_map *map;
3104 int i;
3105
3106 for (i = 0; i < mpb->num_raid_devs; i++) {
3107 dev = get_imsm_dev(super, i);
3108 map = get_imsm_map(dev, 0);
3109 if (__le32_to_cpu(dev->size_high) > 0)
3110 mpb->attributes |= MPB_ATTRIB_2TB;
3111
3112 /* FIXME detect when an array spans a port multiplier */
3113 #if 0
3114 mpb->attributes |= MPB_ATTRIB_PM;
3115 #endif
3116
3117 if (mpb->num_raid_devs > 1 ||
3118 mpb->attributes != MPB_ATTRIB_CHECKSUM_VERIFY) {
3119 version = MPB_VERSION_ATTRIBS;
3120 switch (get_imsm_raid_level(map)) {
3121 case 0: mpb->attributes |= MPB_ATTRIB_RAID0; break;
3122 case 1: mpb->attributes |= MPB_ATTRIB_RAID1; break;
3123 case 10: mpb->attributes |= MPB_ATTRIB_RAID10; break;
3124 case 5: mpb->attributes |= MPB_ATTRIB_RAID5; break;
3125 }
3126 } else {
3127 if (map->num_members >= 5)
3128 version = MPB_VERSION_5OR6_DISK_ARRAY;
3129 else if (dev->status == DEV_CLONE_N_GO)
3130 version = MPB_VERSION_CNG;
3131 else if (get_imsm_raid_level(map) == 5)
3132 version = MPB_VERSION_RAID5;
3133 else if (map->num_members >= 3)
3134 version = MPB_VERSION_3OR4_DISK_ARRAY;
3135 else if (get_imsm_raid_level(map) == 1)
3136 version = MPB_VERSION_RAID1;
3137 else
3138 version = MPB_VERSION_RAID0;
3139 }
3140 strcpy(((char *) mpb->sig) + strlen(MPB_SIGNATURE), version);
3141 }
3142}
3143
aa534678
DW
3144static int check_name(struct intel_super *super, char *name, int quiet)
3145{
3146 struct imsm_super *mpb = super->anchor;
3147 char *reason = NULL;
3148 int i;
3149
3150 if (strlen(name) > MAX_RAID_SERIAL_LEN)
3151 reason = "must be 16 characters or less";
3152
3153 for (i = 0; i < mpb->num_raid_devs; i++) {
3154 struct imsm_dev *dev = get_imsm_dev(super, i);
3155
3156 if (strncmp((char *) dev->volume, name, MAX_RAID_SERIAL_LEN) == 0) {
3157 reason = "already exists";
3158 break;
3159 }
3160 }
3161
3162 if (reason && !quiet)
3163 fprintf(stderr, Name ": imsm volume name %s\n", reason);
3164
3165 return !reason;
3166}
3167
8b353278
DW
3168static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
3169 unsigned long long size, char *name,
3170 char *homehost, int *uuid)
cdddbdbc 3171{
c2c087e6
DW
3172 /* We are creating a volume inside a pre-existing container.
3173 * so st->sb is already set.
3174 */
3175 struct intel_super *super = st->sb;
949c47a0 3176 struct imsm_super *mpb = super->anchor;
ba2de7ba 3177 struct intel_dev *dv;
c2c087e6
DW
3178 struct imsm_dev *dev;
3179 struct imsm_vol *vol;
3180 struct imsm_map *map;
3181 int idx = mpb->num_raid_devs;
3182 int i;
3183 unsigned long long array_blocks;
2c092cad 3184 size_t size_old, size_new;
ff596308 3185 __u32 num_data_stripes;
cdddbdbc 3186
88c32bb1 3187 if (super->orom && mpb->num_raid_devs >= super->orom->vpa) {
c2c087e6 3188 fprintf(stderr, Name": This imsm-container already has the "
88c32bb1 3189 "maximum of %d volumes\n", super->orom->vpa);
c2c087e6
DW
3190 return 0;
3191 }
3192
2c092cad
DW
3193 /* ensure the mpb is large enough for the new data */
3194 size_old = __le32_to_cpu(mpb->mpb_size);
3195 size_new = disks_to_mpb_size(info->nr_disks);
3196 if (size_new > size_old) {
3197 void *mpb_new;
3198 size_t size_round = ROUND_UP(size_new, 512);
3199
3200 if (posix_memalign(&mpb_new, 512, size_round) != 0) {
3201 fprintf(stderr, Name": could not allocate new mpb\n");
3202 return 0;
3203 }
3204 memcpy(mpb_new, mpb, size_old);
3205 free(mpb);
3206 mpb = mpb_new;
949c47a0 3207 super->anchor = mpb_new;
2c092cad
DW
3208 mpb->mpb_size = __cpu_to_le32(size_new);
3209 memset(mpb_new + size_old, 0, size_round - size_old);
3210 }
bf5a934a 3211 super->current_vol = idx;
d23fe947
DW
3212 /* when creating the first raid device in this container set num_disks
3213 * to zero, i.e. delete this spare and add raid member devices in
3214 * add_to_super_imsm_volume()
3215 */
3216 if (super->current_vol == 0)
3217 mpb->num_disks = 0;
5a038140 3218
aa534678
DW
3219 if (!check_name(super, name, 0))
3220 return 0;
ba2de7ba
DW
3221 dv = malloc(sizeof(*dv));
3222 if (!dv) {
3223 fprintf(stderr, Name ": failed to allocate device list entry\n");
3224 return 0;
3225 }
949c47a0
DW
3226 dev = malloc(sizeof(*dev) + sizeof(__u32) * (info->raid_disks - 1));
3227 if (!dev) {
ba2de7ba 3228 free(dv);
949c47a0
DW
3229 fprintf(stderr, Name": could not allocate raid device\n");
3230 return 0;
3231 }
c2c087e6 3232 strncpy((char *) dev->volume, name, MAX_RAID_SERIAL_LEN);
03bcbc65
DW
3233 if (info->level == 1)
3234 array_blocks = info_to_blocks_per_member(info);
3235 else
3236 array_blocks = calc_array_size(info->level, info->raid_disks,
3237 info->layout, info->chunk_size,
3238 info->size*2);
979d38be
DW
3239 /* round array size down to closest MB */
3240 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT) << SECT_PER_MB_SHIFT;
3241
c2c087e6
DW
3242 dev->size_low = __cpu_to_le32((__u32) array_blocks);
3243 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
3244 dev->status = __cpu_to_le32(0);
3245 dev->reserved_blocks = __cpu_to_le32(0);
3246 vol = &dev->vol;
3247 vol->migr_state = 0;
1484e727 3248 set_migr_type(dev, MIGR_INIT);
c2c087e6 3249 vol->dirty = 0;
f8f603f1 3250 vol->curr_migr_unit = 0;
a965f303 3251 map = get_imsm_map(dev, 0);
0dcecb2e 3252 map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
fcfd9599 3253 map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
ef6ffade 3254 map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
0556e1a2 3255 map->failed_disk_num = ~0;
c2c087e6
DW
3256 map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
3257 IMSM_T_STATE_NORMAL;
252d23c0 3258 map->ddf = 1;
ef6ffade
DW
3259
3260 if (info->level == 1 && info->raid_disks > 2) {
38950822
AW
3261 free(dev);
3262 free(dv);
ef6ffade
DW
3263 fprintf(stderr, Name": imsm does not support more than 2 disks"
3264 "in a raid1 volume\n");
3265 return 0;
3266 }
81062a36
DW
3267
3268 map->raid_level = info->level;
4d1313e9 3269 if (info->level == 10) {
c2c087e6 3270 map->raid_level = 1;
4d1313e9 3271 map->num_domains = info->raid_disks / 2;
81062a36
DW
3272 } else if (info->level == 1)
3273 map->num_domains = info->raid_disks;
3274 else
ff596308 3275 map->num_domains = 1;
81062a36 3276
ff596308
DW
3277 num_data_stripes = info_to_num_data_stripes(info, map->num_domains);
3278 map->num_data_stripes = __cpu_to_le32(num_data_stripes);
ef6ffade 3279
c2c087e6
DW
3280 map->num_members = info->raid_disks;
3281 for (i = 0; i < map->num_members; i++) {
3282 /* initialized in add_to_super */
4eb26970 3283 set_imsm_ord_tbl_ent(map, i, IMSM_ORD_REBUILD);
c2c087e6 3284 }
949c47a0 3285 mpb->num_raid_devs++;
ba2de7ba
DW
3286
3287 dv->dev = dev;
3288 dv->index = super->current_vol;
3289 dv->next = super->devlist;
3290 super->devlist = dv;
c2c087e6 3291
4d1313e9
DW
3292 imsm_update_version_info(super);
3293
c2c087e6 3294 return 1;
cdddbdbc
DW
3295}
3296
bf5a934a
DW
3297static int init_super_imsm(struct supertype *st, mdu_array_info_t *info,
3298 unsigned long long size, char *name,
3299 char *homehost, int *uuid)
3300{
3301 /* This is primarily called by Create when creating a new array.
3302 * We will then get add_to_super called for each component, and then
3303 * write_init_super called to write it out to each device.
3304 * For IMSM, Create can create on fresh devices or on a pre-existing
3305 * array.
3306 * To create on a pre-existing array a different method will be called.
3307 * This one is just for fresh drives.
3308 */
3309 struct intel_super *super;
3310 struct imsm_super *mpb;
3311 size_t mpb_size;
4d1313e9 3312 char *version;
bf5a934a 3313
bf5a934a 3314 if (st->sb)
e683ca88
DW
3315 return init_super_imsm_volume(st, info, size, name, homehost, uuid);
3316
3317 if (info)
3318 mpb_size = disks_to_mpb_size(info->nr_disks);
3319 else
3320 mpb_size = 512;
bf5a934a 3321
49133e57 3322 super = alloc_super();
e683ca88 3323 if (super && posix_memalign(&super->buf, 512, mpb_size) != 0) {
bf5a934a 3324 free(super);
e683ca88
DW
3325 super = NULL;
3326 }
3327 if (!super) {
3328 fprintf(stderr, Name
3329 ": %s could not allocate superblock\n", __func__);
bf5a934a
DW
3330 return 0;
3331 }
e683ca88 3332 memset(super->buf, 0, mpb_size);
ef649044 3333 mpb = super->buf;
e683ca88
DW
3334 mpb->mpb_size = __cpu_to_le32(mpb_size);
3335 st->sb = super;
3336
3337 if (info == NULL) {
3338 /* zeroing superblock */
3339 return 0;
3340 }
bf5a934a 3341
4d1313e9
DW
3342 mpb->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3343
3344 version = (char *) mpb->sig;
3345 strcpy(version, MPB_SIGNATURE);
3346 version += strlen(MPB_SIGNATURE);
3347 strcpy(version, MPB_VERSION_RAID0);
bf5a934a 3348
bf5a934a
DW
3349 return 1;
3350}
3351
0e600426 3352#ifndef MDASSEMBLE
f20c3968 3353static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
bf5a934a
DW
3354 int fd, char *devname)
3355{
3356 struct intel_super *super = st->sb;
d23fe947 3357 struct imsm_super *mpb = super->anchor;
bf5a934a
DW
3358 struct dl *dl;
3359 struct imsm_dev *dev;
3360 struct imsm_map *map;
4eb26970 3361 int slot;
bf5a934a 3362
949c47a0 3363 dev = get_imsm_dev(super, super->current_vol);
a965f303 3364 map = get_imsm_map(dev, 0);
bf5a934a 3365
208933a7
N
3366 if (! (dk->state & (1<<MD_DISK_SYNC))) {
3367 fprintf(stderr, Name ": %s: Cannot add spare devices to IMSM volume\n",
3368 devname);
3369 return 1;
3370 }
3371
efb30e7f
DW
3372 if (fd == -1) {
3373 /* we're doing autolayout so grab the pre-marked (in
3374 * validate_geometry) raid_disk
3375 */
3376 for (dl = super->disks; dl; dl = dl->next)
3377 if (dl->raiddisk == dk->raid_disk)
3378 break;
3379 } else {
3380 for (dl = super->disks; dl ; dl = dl->next)
3381 if (dl->major == dk->major &&
3382 dl->minor == dk->minor)
3383 break;
3384 }
d23fe947 3385
208933a7
N
3386 if (!dl) {
3387 fprintf(stderr, Name ": %s is not a member of the same container\n", devname);
f20c3968 3388 return 1;
208933a7 3389 }
bf5a934a 3390
d23fe947
DW
3391 /* add a pristine spare to the metadata */
3392 if (dl->index < 0) {
3393 dl->index = super->anchor->num_disks;
3394 super->anchor->num_disks++;
3395 }
4eb26970
DW
3396 /* Check the device has not already been added */
3397 slot = get_imsm_disk_slot(map, dl->index);
3398 if (slot >= 0 &&
3399 (get_imsm_ord_tbl_ent(dev, slot) & IMSM_ORD_REBUILD) == 0) {
3400 fprintf(stderr, Name ": %s has been included in this array twice\n",
3401 devname);
3402 return 1;
3403 }
be73972f 3404 set_imsm_ord_tbl_ent(map, dk->number, dl->index);
ee5aad5a 3405 dl->disk.status = CONFIGURED_DISK;
d23fe947
DW
3406
3407 /* if we are creating the first raid device update the family number */
3408 if (super->current_vol == 0) {
3409 __u32 sum;
3410 struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
3411 struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
3412
791b666a
AW
3413 if (!_dev || !_disk) {
3414 fprintf(stderr, Name ": BUG mpb setup error\n");
3415 return 1;
3416 }
d23fe947
DW
3417 *_dev = *dev;
3418 *_disk = dl->disk;
148acb7b
DW
3419 sum = random32();
3420 sum += __gen_imsm_checksum(mpb);
d23fe947 3421 mpb->family_num = __cpu_to_le32(sum);
148acb7b 3422 mpb->orig_family_num = mpb->family_num;
d23fe947 3423 }
f20c3968
DW
3424
3425 return 0;
bf5a934a
DW
3426}
3427
f20c3968 3428static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
cdddbdbc
DW
3429 int fd, char *devname)
3430{
c2c087e6 3431 struct intel_super *super = st->sb;
c2c087e6
DW
3432 struct dl *dd;
3433 unsigned long long size;
f2f27e63 3434 __u32 id;
c2c087e6
DW
3435 int rv;
3436 struct stat stb;
3437
88c32bb1
DW
3438 /* if we are on an RAID enabled platform check that the disk is
3439 * attached to the raid controller
3440 */
3441 if (super->hba && !disk_attached_to_hba(fd, super->hba)) {
3442 fprintf(stderr,
3443 Name ": %s is not attached to the raid controller: %s\n",
3444 devname ? : "disk", super->hba);
3445 return 1;
3446 }
3447
f20c3968
DW
3448 if (super->current_vol >= 0)
3449 return add_to_super_imsm_volume(st, dk, fd, devname);
bf5a934a 3450
c2c087e6
DW
3451 fstat(fd, &stb);
3452 dd = malloc(sizeof(*dd));
b9f594fe 3453 if (!dd) {
c2c087e6
DW
3454 fprintf(stderr,
3455 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
f20c3968 3456 return 1;
c2c087e6
DW
3457 }
3458 memset(dd, 0, sizeof(*dd));
3459 dd->major = major(stb.st_rdev);
3460 dd->minor = minor(stb.st_rdev);
b9f594fe 3461 dd->index = -1;
c2c087e6 3462 dd->devname = devname ? strdup(devname) : NULL;
c2c087e6 3463 dd->fd = fd;
689c9bf3 3464 dd->e = NULL;
1a64be56 3465 dd->action = DISK_ADD;
c2c087e6 3466 rv = imsm_read_serial(fd, devname, dd->serial);
32ba9157 3467 if (rv) {
c2c087e6 3468 fprintf(stderr,
0030e8d6 3469 Name ": failed to retrieve scsi serial, aborting\n");
949c47a0 3470 free(dd);
0030e8d6 3471 abort();
c2c087e6
DW
3472 }
3473
c2c087e6
DW
3474 get_dev_size(fd, NULL, &size);
3475 size /= 512;
1f24f035 3476 serialcpy(dd->disk.serial, dd->serial);
b9f594fe 3477 dd->disk.total_blocks = __cpu_to_le32(size);
ee5aad5a 3478 dd->disk.status = SPARE_DISK;
c2c087e6 3479 if (sysfs_disk_to_scsi_id(fd, &id) == 0)
b9f594fe 3480 dd->disk.scsi_id = __cpu_to_le32(id);
c2c087e6 3481 else
b9f594fe 3482 dd->disk.scsi_id = __cpu_to_le32(0);
43dad3d6
DW
3483
3484 if (st->update_tail) {
1a64be56
LM
3485 dd->next = super->disk_mgmt_list;
3486 super->disk_mgmt_list = dd;
43dad3d6
DW
3487 } else {
3488 dd->next = super->disks;
3489 super->disks = dd;
3490 }
f20c3968
DW
3491
3492 return 0;
cdddbdbc
DW
3493}
3494
1a64be56
LM
3495
3496static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
3497{
3498 struct intel_super *super = st->sb;
3499 struct dl *dd;
3500
3501 /* remove from super works only in mdmon - for communication
3502 * manager - monitor. Check if communication memory buffer
3503 * is prepared.
3504 */
3505 if (!st->update_tail) {
3506 fprintf(stderr,
3507 Name ": %s shall be used in mdmon context only"
3508 "(line %d).\n", __func__, __LINE__);
3509 return 1;
3510 }
3511 dd = malloc(sizeof(*dd));
3512 if (!dd) {
3513 fprintf(stderr,
3514 Name ": malloc failed %s:%d.\n", __func__, __LINE__);
3515 return 1;
3516 }
3517 memset(dd, 0, sizeof(*dd));
3518 dd->major = dk->major;
3519 dd->minor = dk->minor;
3520 dd->index = -1;
3521 dd->fd = -1;
3522 dd->disk.status = SPARE_DISK;
3523 dd->action = DISK_REMOVE;
3524
3525 dd->next = super->disk_mgmt_list;
3526 super->disk_mgmt_list = dd;
3527
3528
3529 return 0;
3530}
3531
f796af5d
DW
3532static int store_imsm_mpb(int fd, struct imsm_super *mpb);
3533
3534static union {
3535 char buf[512];
3536 struct imsm_super anchor;
3537} spare_record __attribute__ ((aligned(512)));
c2c087e6 3538
d23fe947
DW
3539/* spare records have their own family number and do not have any defined raid
3540 * devices
3541 */
3542static int write_super_imsm_spares(struct intel_super *super, int doclose)
3543{
d23fe947 3544 struct imsm_super *mpb = super->anchor;
f796af5d 3545 struct imsm_super *spare = &spare_record.anchor;
d23fe947
DW
3546 __u32 sum;
3547 struct dl *d;
3548
f796af5d
DW
3549 spare->mpb_size = __cpu_to_le32(sizeof(struct imsm_super)),
3550 spare->generation_num = __cpu_to_le32(1UL),
3551 spare->attributes = MPB_ATTRIB_CHECKSUM_VERIFY;
3552 spare->num_disks = 1,
3553 spare->num_raid_devs = 0,
3554 spare->cache_size = mpb->cache_size,
3555 spare->pwr_cycle_count = __cpu_to_le32(1),
3556
3557 snprintf((char *) spare->sig, MAX_SIGNATURE_LENGTH,
3558 MPB_SIGNATURE MPB_VERSION_RAID0);
d23fe947
DW
3559
3560 for (d = super->disks; d; d = d->next) {
8796fdc4 3561 if (d->index != -1)
d23fe947
DW
3562 continue;
3563
f796af5d
DW
3564 spare->disk[0] = d->disk;
3565 sum = __gen_imsm_checksum(spare);
3566 spare->family_num = __cpu_to_le32(sum);
3567 spare->orig_family_num = 0;
3568 sum = __gen_imsm_checksum(spare);
3569 spare->check_sum = __cpu_to_le32(sum);
d23fe947 3570
f796af5d 3571 if (store_imsm_mpb(d->fd, spare)) {
d23fe947
DW
3572 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3573 __func__, d->major, d->minor, strerror(errno));
e74255d9 3574 return 1;
d23fe947
DW
3575 }
3576 if (doclose) {
3577 close(d->fd);
3578 d->fd = -1;
3579 }
3580 }
3581
e74255d9 3582 return 0;
d23fe947
DW
3583}
3584
36988a3d 3585static int write_super_imsm(struct supertype *st, int doclose)
cdddbdbc 3586{
36988a3d 3587 struct intel_super *super = st->sb;
949c47a0 3588 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
3589 struct dl *d;
3590 __u32 generation;
3591 __u32 sum;
d23fe947 3592 int spares = 0;
949c47a0 3593 int i;
a48ac0a8 3594 __u32 mpb_size = sizeof(struct imsm_super) - sizeof(struct imsm_disk);
36988a3d 3595 int num_disks = 0;
cdddbdbc 3596
c2c087e6
DW
3597 /* 'generation' is incremented everytime the metadata is written */
3598 generation = __le32_to_cpu(mpb->generation_num);
3599 generation++;
3600 mpb->generation_num = __cpu_to_le32(generation);
3601
148acb7b
DW
3602 /* fix up cases where previous mdadm releases failed to set
3603 * orig_family_num
3604 */
3605 if (mpb->orig_family_num == 0)
3606 mpb->orig_family_num = mpb->family_num;
3607
d23fe947 3608 for (d = super->disks; d; d = d->next) {
8796fdc4 3609 if (d->index == -1)
d23fe947 3610 spares++;
36988a3d 3611 else {
d23fe947 3612 mpb->disk[d->index] = d->disk;
36988a3d
AK
3613 num_disks++;
3614 }
d23fe947 3615 }
36988a3d 3616 for (d = super->missing; d; d = d->next) {
47ee5a45 3617 mpb->disk[d->index] = d->disk;
36988a3d
AK
3618 num_disks++;
3619 }
3620 mpb->num_disks = num_disks;
3621 mpb_size += sizeof(struct imsm_disk) * mpb->num_disks;
b9f594fe 3622
949c47a0
DW
3623 for (i = 0; i < mpb->num_raid_devs; i++) {
3624 struct imsm_dev *dev = __get_imsm_dev(mpb, i);
36988a3d
AK
3625 struct imsm_dev *dev2 = get_imsm_dev(super, i);
3626 if (dev && dev2) {
3627 imsm_copy_dev(dev, dev2);
3628 mpb_size += sizeof_imsm_dev(dev, 0);
3629 }
949c47a0 3630 }
a48ac0a8
DW
3631 mpb_size += __le32_to_cpu(mpb->bbm_log_size);
3632 mpb->mpb_size = __cpu_to_le32(mpb_size);
949c47a0 3633
c2c087e6 3634 /* recalculate checksum */
949c47a0 3635 sum = __gen_imsm_checksum(mpb);
c2c087e6
DW
3636 mpb->check_sum = __cpu_to_le32(sum);
3637
d23fe947 3638 /* write the mpb for disks that compose raid devices */
c2c087e6 3639 for (d = super->disks; d ; d = d->next) {
d23fe947
DW
3640 if (d->index < 0)
3641 continue;
f796af5d 3642 if (store_imsm_mpb(d->fd, mpb))
c2c087e6
DW
3643 fprintf(stderr, "%s: failed for device %d:%d %s\n",
3644 __func__, d->major, d->minor, strerror(errno));
c2c087e6
DW
3645 if (doclose) {
3646 close(d->fd);
3647 d->fd = -1;
3648 }
3649 }
3650
d23fe947
DW
3651 if (spares)
3652 return write_super_imsm_spares(super, doclose);
3653
e74255d9 3654 return 0;
c2c087e6
DW
3655}
3656
0e600426 3657
9b1fb677 3658static int create_array(struct supertype *st, int dev_idx)
43dad3d6
DW
3659{
3660 size_t len;
3661 struct imsm_update_create_array *u;
3662 struct intel_super *super = st->sb;
9b1fb677 3663 struct imsm_dev *dev = get_imsm_dev(super, dev_idx);
54c2c1ea
DW
3664 struct imsm_map *map = get_imsm_map(dev, 0);
3665 struct disk_info *inf;
3666 struct imsm_disk *disk;
3667 int i;
43dad3d6 3668
54c2c1ea
DW
3669 len = sizeof(*u) - sizeof(*dev) + sizeof_imsm_dev(dev, 0) +
3670 sizeof(*inf) * map->num_members;
43dad3d6
DW
3671 u = malloc(len);
3672 if (!u) {
3673 fprintf(stderr, "%s: failed to allocate update buffer\n",
3674 __func__);
3675 return 1;
3676 }
3677
3678 u->type = update_create_array;
9b1fb677 3679 u->dev_idx = dev_idx;
43dad3d6 3680 imsm_copy_dev(&u->dev, dev);
54c2c1ea
DW
3681 inf = get_disk_info(u);
3682 for (i = 0; i < map->num_members; i++) {
9b1fb677
DW
3683 int idx = get_imsm_disk_idx(dev, i);
3684
54c2c1ea
DW
3685 disk = get_imsm_disk(super, idx);
3686 serialcpy(inf[i].serial, disk->serial);
3687 }
43dad3d6
DW
3688 append_metadata_update(st, u, len);
3689
3690 return 0;
3691}
3692
1a64be56 3693static int mgmt_disk(struct supertype *st)
43dad3d6
DW
3694{
3695 struct intel_super *super = st->sb;
3696 size_t len;
1a64be56 3697 struct imsm_update_add_remove_disk *u;
43dad3d6 3698
1a64be56 3699 if (!super->disk_mgmt_list)
43dad3d6
DW
3700 return 0;
3701
3702 len = sizeof(*u);
3703 u = malloc(len);
3704 if (!u) {
3705 fprintf(stderr, "%s: failed to allocate update buffer\n",
3706 __func__);
3707 return 1;
3708 }
3709
1a64be56 3710 u->type = update_add_remove_disk;
43dad3d6
DW
3711 append_metadata_update(st, u, len);
3712
3713 return 0;
3714}
3715
c2c087e6
DW
3716static int write_init_super_imsm(struct supertype *st)
3717{
9b1fb677
DW
3718 struct intel_super *super = st->sb;
3719 int current_vol = super->current_vol;
3720
3721 /* we are done with current_vol reset it to point st at the container */
3722 super->current_vol = -1;
3723
8273f55e 3724 if (st->update_tail) {
43dad3d6
DW
3725 /* queue the recently created array / added disk
3726 * as a metadata update */
8273f55e 3727 struct dl *d;
43dad3d6 3728 int rv;
8273f55e 3729
43dad3d6 3730 /* determine if we are creating a volume or adding a disk */
9b1fb677 3731 if (current_vol < 0) {
1a64be56
LM
3732 /* in the mgmt (add/remove) disk case we are running
3733 * in mdmon context, so don't close fd's
43dad3d6 3734 */
1a64be56 3735 return mgmt_disk(st);
43dad3d6 3736 } else
9b1fb677 3737 rv = create_array(st, current_vol);
8273f55e
DW
3738
3739 for (d = super->disks; d ; d = d->next) {
3740 close(d->fd);
3741 d->fd = -1;
3742 }
3743
43dad3d6 3744 return rv;
d682f344
N
3745 } else {
3746 struct dl *d;
3747 for (d = super->disks; d; d = d->next)
3748 Kill(d->devname, NULL, 0, 1, 1);
36988a3d 3749 return write_super_imsm(st, 1);
d682f344 3750 }
cdddbdbc 3751}
0e600426 3752#endif
cdddbdbc 3753
e683ca88 3754static int store_super_imsm(struct supertype *st, int fd)
cdddbdbc 3755{
e683ca88
DW
3756 struct intel_super *super = st->sb;
3757 struct imsm_super *mpb = super ? super->anchor : NULL;
551c80c1 3758
e683ca88 3759 if (!mpb)
ad97895e
DW
3760 return 1;
3761
1799c9e8 3762#ifndef MDASSEMBLE
e683ca88 3763 return store_imsm_mpb(fd, mpb);
1799c9e8
N
3764#else
3765 return 1;
3766#endif
cdddbdbc
DW
3767}
3768
0e600426
N
3769static int imsm_bbm_log_size(struct imsm_super *mpb)
3770{
3771 return __le32_to_cpu(mpb->bbm_log_size);
3772}
3773
3774#ifndef MDASSEMBLE
cdddbdbc
DW
3775static int validate_geometry_imsm_container(struct supertype *st, int level,
3776 int layout, int raiddisks, int chunk,
c2c087e6 3777 unsigned long long size, char *dev,
2c514b71
NB
3778 unsigned long long *freesize,
3779 int verbose)
cdddbdbc 3780{
c2c087e6
DW
3781 int fd;
3782 unsigned long long ldsize;
88c32bb1 3783 const struct imsm_orom *orom;
cdddbdbc 3784
c2c087e6
DW
3785 if (level != LEVEL_CONTAINER)
3786 return 0;
3787 if (!dev)
3788 return 1;
3789
88c32bb1
DW
3790 if (check_env("IMSM_NO_PLATFORM"))
3791 orom = NULL;
3792 else
3793 orom = find_imsm_orom();
3794 if (orom && raiddisks > orom->tds) {
3795 if (verbose)
3796 fprintf(stderr, Name ": %d exceeds maximum number of"
3797 " platform supported disks: %d\n",
3798 raiddisks, orom->tds);
3799 return 0;
3800 }
3801
c2c087e6
DW
3802 fd = open(dev, O_RDONLY|O_EXCL, 0);
3803 if (fd < 0) {
2c514b71
NB
3804 if (verbose)
3805 fprintf(stderr, Name ": imsm: Cannot open %s: %s\n",
3806 dev, strerror(errno));
c2c087e6
DW
3807 return 0;
3808 }
3809 if (!get_dev_size(fd, dev, &ldsize)) {
3810 close(fd);
3811 return 0;
3812 }
3813 close(fd);
3814
3815 *freesize = avail_size_imsm(st, ldsize >> 9);
3816
3817 return 1;
cdddbdbc
DW
3818}
3819
0dcecb2e
DW
3820static unsigned long long find_size(struct extent *e, int *idx, int num_extents)
3821{
3822 const unsigned long long base_start = e[*idx].start;
3823 unsigned long long end = base_start + e[*idx].size;
3824 int i;
3825
3826 if (base_start == end)
3827 return 0;
3828
3829 *idx = *idx + 1;
3830 for (i = *idx; i < num_extents; i++) {
3831 /* extend overlapping extents */
3832 if (e[i].start >= base_start &&
3833 e[i].start <= end) {
3834 if (e[i].size == 0)
3835 return 0;
3836 if (e[i].start + e[i].size > end)
3837 end = e[i].start + e[i].size;
3838 } else if (e[i].start > end) {
3839 *idx = i;
3840 break;
3841 }
3842 }
3843
3844 return end - base_start;
3845}
3846
3847static unsigned long long merge_extents(struct intel_super *super, int sum_extents)
3848{
3849 /* build a composite disk with all known extents and generate a new
3850 * 'maxsize' given the "all disks in an array must share a common start
3851 * offset" constraint
3852 */
3853 struct extent *e = calloc(sum_extents, sizeof(*e));
3854 struct dl *dl;
3855 int i, j;
3856 int start_extent;
3857 unsigned long long pos;
b9d77223 3858 unsigned long long start = 0;
0dcecb2e
DW
3859 unsigned long long maxsize;
3860 unsigned long reserve;
3861
3862 if (!e)
a7dd165b 3863 return 0;
0dcecb2e
DW
3864
3865 /* coalesce and sort all extents. also, check to see if we need to
3866 * reserve space between member arrays
3867 */
3868 j = 0;
3869 for (dl = super->disks; dl; dl = dl->next) {
3870 if (!dl->e)
3871 continue;
3872 for (i = 0; i < dl->extent_cnt; i++)
3873 e[j++] = dl->e[i];
3874 }
3875 qsort(e, sum_extents, sizeof(*e), cmp_extent);
3876
3877 /* merge extents */
3878 i = 0;
3879 j = 0;
3880 while (i < sum_extents) {
3881 e[j].start = e[i].start;
3882 e[j].size = find_size(e, &i, sum_extents);
3883 j++;
3884 if (e[j-1].size == 0)
3885 break;
3886 }
3887
3888 pos = 0;
3889 maxsize = 0;
3890 start_extent = 0;
3891 i = 0;
3892 do {
3893 unsigned long long esize;
3894
3895 esize = e[i].start - pos;
3896 if (esize >= maxsize) {
3897 maxsize = esize;
3898 start = pos;
3899 start_extent = i;
3900 }
3901 pos = e[i].start + e[i].size;
3902 i++;
3903 } while (e[i-1].size);
3904 free(e);
3905
a7dd165b
DW
3906 if (maxsize == 0)
3907 return 0;
3908
3909 /* FIXME assumes volume at offset 0 is the first volume in a
3910 * container
3911 */
0dcecb2e
DW
3912 if (start_extent > 0)
3913 reserve = IMSM_RESERVED_SECTORS; /* gap between raid regions */
3914 else
3915 reserve = 0;
3916
3917 if (maxsize < reserve)
a7dd165b 3918 return 0;
0dcecb2e
DW
3919
3920 super->create_offset = ~((__u32) 0);
3921 if (start + reserve > super->create_offset)
a7dd165b 3922 return 0; /* start overflows create_offset */
0dcecb2e
DW
3923 super->create_offset = start + reserve;
3924
3925 return maxsize - reserve;
3926}
3927
88c32bb1
DW
3928static int is_raid_level_supported(const struct imsm_orom *orom, int level, int raiddisks)
3929{
3930 if (level < 0 || level == 6 || level == 4)
3931 return 0;
3932
3933 /* if we have an orom prevent invalid raid levels */
3934 if (orom)
3935 switch (level) {
3936 case 0: return imsm_orom_has_raid0(orom);
3937 case 1:
3938 if (raiddisks > 2)
3939 return imsm_orom_has_raid1e(orom);
1c556e92
DW
3940 return imsm_orom_has_raid1(orom) && raiddisks == 2;
3941 case 10: return imsm_orom_has_raid10(orom) && raiddisks == 4;
3942 case 5: return imsm_orom_has_raid5(orom) && raiddisks > 2;
88c32bb1
DW
3943 }
3944 else
3945 return 1; /* not on an Intel RAID platform so anything goes */
3946
3947 return 0;
3948}
3949
35f81cbb 3950#define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
6592ce37
DW
3951static int
3952validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
3953 int raiddisks, int chunk, int verbose)
3954{
3955 if (!is_raid_level_supported(super->orom, level, raiddisks)) {
3956 pr_vrb(": platform does not support raid%d with %d disk%s\n",
3957 level, raiddisks, raiddisks > 1 ? "s" : "");
3958 return 0;
3959 }
3960 if (super->orom && level != 1 &&
3961 !imsm_orom_has_chunk(super->orom, chunk)) {
3962 pr_vrb(": platform does not support a chunk size of: %d\n", chunk);
3963 return 0;
3964 }
3965 if (layout != imsm_level_to_layout(level)) {
3966 if (level == 5)
3967 pr_vrb(": imsm raid 5 only supports the left-asymmetric layout\n");
3968 else if (level == 10)
3969 pr_vrb(": imsm raid 10 only supports the n2 layout\n");
3970 else
3971 pr_vrb(": imsm unknown layout %#x for this raid level %d\n",
3972 layout, level);
3973 return 0;
3974 }
3975
3976 return 1;
3977}
3978
c2c087e6
DW
3979/* validate_geometry_imsm_volume - lifted from validate_geometry_ddf_bvd
3980 * FIX ME add ahci details
3981 */
8b353278
DW
3982static int validate_geometry_imsm_volume(struct supertype *st, int level,
3983 int layout, int raiddisks, int chunk,
c2c087e6 3984 unsigned long long size, char *dev,
2c514b71
NB
3985 unsigned long long *freesize,
3986 int verbose)
cdddbdbc 3987{
c2c087e6
DW
3988 struct stat stb;
3989 struct intel_super *super = st->sb;
a20d2ba5 3990 struct imsm_super *mpb = super->anchor;
c2c087e6
DW
3991 struct dl *dl;
3992 unsigned long long pos = 0;
3993 unsigned long long maxsize;
3994 struct extent *e;
3995 int i;
cdddbdbc 3996
88c32bb1
DW
3997 /* We must have the container info already read in. */
3998 if (!super)
c2c087e6
DW
3999 return 0;
4000
6592ce37 4001 if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose))
c2c087e6 4002 return 0;
c2c087e6
DW
4003
4004 if (!dev) {
4005 /* General test: make sure there is space for
2da8544a
DW
4006 * 'raiddisks' device extents of size 'size' at a given
4007 * offset
c2c087e6 4008 */
e46273eb 4009 unsigned long long minsize = size;
b7528a20 4010 unsigned long long start_offset = MaxSector;
c2c087e6
DW
4011 int dcnt = 0;
4012 if (minsize == 0)
4013 minsize = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
4014 for (dl = super->disks; dl ; dl = dl->next) {
4015 int found = 0;
4016
bf5a934a 4017 pos = 0;
c2c087e6
DW
4018 i = 0;
4019 e = get_extents(super, dl);
4020 if (!e) continue;
4021 do {
4022 unsigned long long esize;
4023 esize = e[i].start - pos;
4024 if (esize >= minsize)
4025 found = 1;
b7528a20 4026 if (found && start_offset == MaxSector) {
2da8544a
DW
4027 start_offset = pos;
4028 break;
4029 } else if (found && pos != start_offset) {
4030 found = 0;
4031 break;
4032 }
c2c087e6
DW
4033 pos = e[i].start + e[i].size;
4034 i++;
4035 } while (e[i-1].size);
4036 if (found)
4037 dcnt++;
4038 free(e);
4039 }
4040 if (dcnt < raiddisks) {
2c514b71
NB
4041 if (verbose)
4042 fprintf(stderr, Name ": imsm: Not enough "
4043 "devices with space for this array "
4044 "(%d < %d)\n",
4045 dcnt, raiddisks);
c2c087e6
DW
4046 return 0;
4047 }
4048 return 1;
4049 }
0dcecb2e 4050
c2c087e6
DW
4051 /* This device must be a member of the set */
4052 if (stat(dev, &stb) < 0)
4053 return 0;
4054 if ((S_IFMT & stb.st_mode) != S_IFBLK)
4055 return 0;
4056 for (dl = super->disks ; dl ; dl = dl->next) {
f21e18ca
N
4057 if (dl->major == (int)major(stb.st_rdev) &&
4058 dl->minor == (int)minor(stb.st_rdev))
c2c087e6
DW
4059 break;
4060 }
4061 if (!dl) {
2c514b71
NB
4062 if (verbose)
4063 fprintf(stderr, Name ": %s is not in the "
4064 "same imsm set\n", dev);
c2c087e6 4065 return 0;
a20d2ba5
DW
4066 } else if (super->orom && dl->index < 0 && mpb->num_raid_devs) {
4067 /* If a volume is present then the current creation attempt
4068 * cannot incorporate new spares because the orom may not
4069 * understand this configuration (all member disks must be
4070 * members of each array in the container).
4071 */
4072 fprintf(stderr, Name ": %s is a spare and a volume"
4073 " is already defined for this container\n", dev);
4074 fprintf(stderr, Name ": The option-rom requires all member"
4075 " disks to be a member of all volumes\n");
4076 return 0;
c2c087e6 4077 }
0dcecb2e
DW
4078
4079 /* retrieve the largest free space block */
c2c087e6
DW
4080 e = get_extents(super, dl);
4081 maxsize = 0;
4082 i = 0;
0dcecb2e
DW
4083 if (e) {
4084 do {
4085 unsigned long long esize;
4086
4087 esize = e[i].start - pos;
4088 if (esize >= maxsize)
4089 maxsize = esize;
4090 pos = e[i].start + e[i].size;
4091 i++;
4092 } while (e[i-1].size);
4093 dl->e = e;
4094 dl->extent_cnt = i;
4095 } else {
4096 if (verbose)
4097 fprintf(stderr, Name ": unable to determine free space for: %s\n",
4098 dev);
4099 return 0;
4100 }
4101 if (maxsize < size) {
4102 if (verbose)
4103 fprintf(stderr, Name ": %s not enough space (%llu < %llu)\n",
4104 dev, maxsize, size);
4105 return 0;
4106 }
4107
4108 /* count total number of extents for merge */
4109 i = 0;
4110 for (dl = super->disks; dl; dl = dl->next)
4111 if (dl->e)
4112 i += dl->extent_cnt;
4113
4114 maxsize = merge_extents(super, i);
a7dd165b 4115 if (maxsize < size || maxsize == 0) {
0dcecb2e
DW
4116 if (verbose)
4117 fprintf(stderr, Name ": not enough space after merge (%llu < %llu)\n",
4118 maxsize, size);
4119 return 0;
0dcecb2e
DW
4120 }
4121
c2c087e6
DW
4122 *freesize = maxsize;
4123
4124 return 1;
cdddbdbc
DW
4125}
4126
efb30e7f
DW
4127static int reserve_space(struct supertype *st, int raiddisks,
4128 unsigned long long size, int chunk,
4129 unsigned long long *freesize)
4130{
4131 struct intel_super *super = st->sb;
4132 struct imsm_super *mpb = super->anchor;
4133 struct dl *dl;
4134 int i;
4135 int extent_cnt;
4136 struct extent *e;
4137 unsigned long long maxsize;
4138 unsigned long long minsize;
4139 int cnt;
4140 int used;
4141
4142 /* find the largest common start free region of the possible disks */
4143 used = 0;
4144 extent_cnt = 0;
4145 cnt = 0;
4146 for (dl = super->disks; dl; dl = dl->next) {
4147 dl->raiddisk = -1;
4148
4149 if (dl->index >= 0)
4150 used++;
4151
4152 /* don't activate new spares if we are orom constrained
4153 * and there is already a volume active in the container
4154 */
4155 if (super->orom && dl->index < 0 && mpb->num_raid_devs)
4156 continue;
4157
4158 e = get_extents(super, dl);
4159 if (!e)
4160 continue;
4161 for (i = 1; e[i-1].size; i++)
4162 ;
4163 dl->e = e;
4164 dl->extent_cnt = i;
4165 extent_cnt += i;
4166 cnt++;
4167 }
4168
4169 maxsize = merge_extents(super, extent_cnt);
4170 minsize = size;
4171 if (size == 0)
4172 minsize = chunk;
4173
4174 if (cnt < raiddisks ||
4175 (super->orom && used && used != raiddisks) ||
a7dd165b
DW
4176 maxsize < minsize ||
4177 maxsize == 0) {
efb30e7f
DW
4178 fprintf(stderr, Name ": not enough devices with space to create array.\n");
4179 return 0; /* No enough free spaces large enough */
4180 }
4181
4182 if (size == 0) {
4183 size = maxsize;
4184 if (chunk) {
4185 size /= chunk;
4186 size *= chunk;
4187 }
4188 }
4189
4190 cnt = 0;
4191 for (dl = super->disks; dl; dl = dl->next)
4192 if (dl->e)
4193 dl->raiddisk = cnt++;
4194
4195 *freesize = size;
4196
4197 return 1;
4198}
4199
bf5a934a
DW
4200static int validate_geometry_imsm(struct supertype *st, int level, int layout,
4201 int raiddisks, int chunk, unsigned long long size,
4202 char *dev, unsigned long long *freesize,
4203 int verbose)
4204{
4205 int fd, cfd;
4206 struct mdinfo *sra;
20cbe8d2 4207 int is_member = 0;
bf5a934a
DW
4208
4209 /* if given unused devices create a container
4210 * if given given devices in a container create a member volume
4211 */
4212 if (level == LEVEL_CONTAINER) {
4213 /* Must be a fresh device to add to a container */
4214 return validate_geometry_imsm_container(st, level, layout,
4215 raiddisks, chunk, size,
4216 dev, freesize,
4217 verbose);
4218 }
4219
8592f29d
N
4220 if (!dev) {
4221 if (st->sb && freesize) {
efb30e7f
DW
4222 /* we are being asked to automatically layout a
4223 * new volume based on the current contents of
4224 * the container. If the the parameters can be
4225 * satisfied reserve_space will record the disks,
4226 * start offset, and size of the volume to be
4227 * created. add_to_super and getinfo_super
4228 * detect when autolayout is in progress.
4229 */
6592ce37
DW
4230 if (!validate_geometry_imsm_orom(st->sb, level, layout,
4231 raiddisks, chunk,
4232 verbose))
4233 return 0;
efb30e7f 4234 return reserve_space(st, raiddisks, size, chunk, freesize);
8592f29d
N
4235 }
4236 return 1;
4237 }
bf5a934a
DW
4238 if (st->sb) {
4239 /* creating in a given container */
4240 return validate_geometry_imsm_volume(st, level, layout,
4241 raiddisks, chunk, size,
4242 dev, freesize, verbose);
4243 }
4244
bf5a934a
DW
4245 /* This device needs to be a device in an 'imsm' container */
4246 fd = open(dev, O_RDONLY|O_EXCL, 0);
4247 if (fd >= 0) {
4248 if (verbose)
4249 fprintf(stderr,
4250 Name ": Cannot create this array on device %s\n",
4251 dev);
4252 close(fd);
4253 return 0;
4254 }
4255 if (errno != EBUSY || (fd = open(dev, O_RDONLY, 0)) < 0) {
4256 if (verbose)
4257 fprintf(stderr, Name ": Cannot open %s: %s\n",
4258 dev, strerror(errno));
4259 return 0;
4260 }
4261 /* Well, it is in use by someone, maybe an 'imsm' container. */
4262 cfd = open_container(fd);
20cbe8d2 4263 close(fd);
bf5a934a 4264 if (cfd < 0) {
bf5a934a
DW
4265 if (verbose)
4266 fprintf(stderr, Name ": Cannot use %s: It is busy\n",
4267 dev);
4268 return 0;
4269 }
4270 sra = sysfs_read(cfd, 0, GET_VERSION);
bf5a934a 4271 if (sra && sra->array.major_version == -1 &&
20cbe8d2
AW
4272 strcmp(sra->text_version, "imsm") == 0)
4273 is_member = 1;
4274 sysfs_free(sra);
4275 if (is_member) {
bf5a934a
DW
4276 /* This is a member of a imsm container. Load the container
4277 * and try to create a volume
4278 */
4279 struct intel_super *super;
4280
e1902a7b 4281 if (load_super_imsm_all(st, cfd, (void **) &super, NULL) == 0) {
bf5a934a
DW
4282 st->sb = super;
4283 st->container_dev = fd2devnum(cfd);
4284 close(cfd);
4285 return validate_geometry_imsm_volume(st, level, layout,
4286 raiddisks, chunk,
4287 size, dev,
4288 freesize, verbose);
4289 }
20cbe8d2 4290 }
bf5a934a 4291
20cbe8d2
AW
4292 if (verbose)
4293 fprintf(stderr, Name ": failed container membership check\n");
4294
4295 close(cfd);
4296 return 0;
bf5a934a 4297}
0bd16cf2 4298
30f58b22 4299static void default_geometry_imsm(struct supertype *st, int *level, int *layout, int *chunk)
0bd16cf2
DJ
4300{
4301 struct intel_super *super = st->sb;
4302
30f58b22
DW
4303 if (level && *level == UnSet)
4304 *level = LEVEL_CONTAINER;
4305
4306 if (level && layout && *layout == UnSet)
4307 *layout = imsm_level_to_layout(*level);
0bd16cf2 4308
1d54f286
N
4309 if (chunk && (*chunk == UnSet || *chunk == 0) &&
4310 super && super->orom)
30f58b22 4311 *chunk = imsm_orom_default_chunk(super->orom);
0bd16cf2
DJ
4312}
4313
33414a01
DW
4314static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
4315
4316static int kill_subarray_imsm(struct supertype *st)
4317{
4318 /* remove the subarray currently referenced by ->current_vol */
4319 __u8 i;
4320 struct intel_dev **dp;
4321 struct intel_super *super = st->sb;
4322 __u8 current_vol = super->current_vol;
4323 struct imsm_super *mpb = super->anchor;
4324
4325 if (super->current_vol < 0)
4326 return 2;
4327 super->current_vol = -1; /* invalidate subarray cursor */
4328
4329 /* block deletions that would change the uuid of active subarrays
4330 *
4331 * FIXME when immutable ids are available, but note that we'll
4332 * also need to fixup the invalidated/active subarray indexes in
4333 * mdstat
4334 */
4335 for (i = 0; i < mpb->num_raid_devs; i++) {
4336 char subarray[4];
4337
4338 if (i < current_vol)
4339 continue;
4340 sprintf(subarray, "%u", i);
4341 if (is_subarray_active(subarray, st->devname)) {
4342 fprintf(stderr,
4343 Name ": deleting subarray-%d would change the UUID of active subarray-%d, aborting\n",
4344 current_vol, i);
4345
4346 return 2;
4347 }
4348 }
4349
4350 if (st->update_tail) {
4351 struct imsm_update_kill_array *u = malloc(sizeof(*u));
4352
4353 if (!u)
4354 return 2;
4355 u->type = update_kill_array;
4356 u->dev_idx = current_vol;
4357 append_metadata_update(st, u, sizeof(*u));
4358
4359 return 0;
4360 }
4361
4362 for (dp = &super->devlist; *dp;)
4363 if ((*dp)->index == current_vol) {
4364 *dp = (*dp)->next;
4365 } else {
4366 handle_missing(super, (*dp)->dev);
4367 if ((*dp)->index > current_vol)
4368 (*dp)->index--;
4369 dp = &(*dp)->next;
4370 }
4371
4372 /* no more raid devices, all active components are now spares,
4373 * but of course failed are still failed
4374 */
4375 if (--mpb->num_raid_devs == 0) {
4376 struct dl *d;
4377
4378 for (d = super->disks; d; d = d->next)
4379 if (d->index > -2) {
4380 d->index = -1;
4381 d->disk.status = SPARE_DISK;
4382 }
4383 }
4384
4385 super->updates_pending++;
4386
4387 return 0;
4388}
aa534678 4389
a951a4f7 4390static int update_subarray_imsm(struct supertype *st, char *subarray,
fa56eddb 4391 char *update, struct mddev_ident *ident)
aa534678
DW
4392{
4393 /* update the subarray currently referenced by ->current_vol */
4394 struct intel_super *super = st->sb;
4395 struct imsm_super *mpb = super->anchor;
4396
aa534678
DW
4397 if (strcmp(update, "name") == 0) {
4398 char *name = ident->name;
a951a4f7
N
4399 char *ep;
4400 int vol;
aa534678 4401
a951a4f7 4402 if (is_subarray_active(subarray, st->devname)) {
aa534678
DW
4403 fprintf(stderr,
4404 Name ": Unable to update name of active subarray\n");
4405 return 2;
4406 }
4407
4408 if (!check_name(super, name, 0))
4409 return 2;
4410
a951a4f7
N
4411 vol = strtoul(subarray, &ep, 10);
4412 if (*ep != '\0' || vol >= super->anchor->num_raid_devs)
4413 return 2;
4414
aa534678
DW
4415 if (st->update_tail) {
4416 struct imsm_update_rename_array *u = malloc(sizeof(*u));
4417
4418 if (!u)
4419 return 2;
4420 u->type = update_rename_array;
a951a4f7 4421 u->dev_idx = vol;
aa534678
DW
4422 snprintf((char *) u->name, MAX_RAID_SERIAL_LEN, "%s", name);
4423 append_metadata_update(st, u, sizeof(*u));
4424 } else {
4425 struct imsm_dev *dev;
4426 int i;
4427
a951a4f7 4428 dev = get_imsm_dev(super, vol);
aa534678
DW
4429 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
4430 for (i = 0; i < mpb->num_raid_devs; i++) {
4431 dev = get_imsm_dev(super, i);
4432 handle_missing(super, dev);
4433 }
4434 super->updates_pending++;
4435 }
4436 } else
4437 return 2;
4438
4439 return 0;
4440}
0e600426 4441#endif /* MDASSEMBLE */
bf5a934a 4442
28bce06f
AK
4443static int is_gen_migration(struct imsm_dev *dev)
4444{
4445 if (!dev->vol.migr_state)
4446 return 0;
4447
4448 if (migr_type(dev) == MIGR_GEN_MIGR)
4449 return 1;
4450
4451 return 0;
4452}
4453
1e5c6983
DW
4454static int is_rebuilding(struct imsm_dev *dev)
4455{
4456 struct imsm_map *migr_map;
4457
4458 if (!dev->vol.migr_state)
4459 return 0;
4460
4461 if (migr_type(dev) != MIGR_REBUILD)
4462 return 0;
4463
4464 migr_map = get_imsm_map(dev, 1);
4465
4466 if (migr_map->map_state == IMSM_T_STATE_DEGRADED)
4467 return 1;
4468 else
4469 return 0;
4470}
4471
4472static void update_recovery_start(struct imsm_dev *dev, struct mdinfo *array)
4473{
4474 struct mdinfo *rebuild = NULL;
4475 struct mdinfo *d;
4476 __u32 units;
4477
4478 if (!is_rebuilding(dev))
4479 return;
4480
4481 /* Find the rebuild target, but punt on the dual rebuild case */
4482 for (d = array->devs; d; d = d->next)
4483 if (d->recovery_start == 0) {
4484 if (rebuild)
4485 return;
4486 rebuild = d;
4487 }
4488
4363fd80
DW
4489 if (!rebuild) {
4490 /* (?) none of the disks are marked with
4491 * IMSM_ORD_REBUILD, so assume they are missing and the
4492 * disk_ord_tbl was not correctly updated
4493 */
4494 dprintf("%s: failed to locate out-of-sync disk\n", __func__);
4495 return;
4496 }
4497
1e5c6983
DW
4498 units = __le32_to_cpu(dev->vol.curr_migr_unit);
4499 rebuild->recovery_start = units * blocks_per_migr_unit(dev);
4500}
4501
4502
00bbdbda 4503static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
cdddbdbc 4504{
4f5bc454
DW
4505 /* Given a container loaded by load_super_imsm_all,
4506 * extract information about all the arrays into
4507 * an mdinfo tree.
00bbdbda 4508 * If 'subarray' is given, just extract info about that array.
4f5bc454
DW
4509 *
4510 * For each imsm_dev create an mdinfo, fill it in,
4511 * then look for matching devices in super->disks
4512 * and create appropriate device mdinfo.
4513 */
4514 struct intel_super *super = st->sb;
949c47a0 4515 struct imsm_super *mpb = super->anchor;
4f5bc454 4516 struct mdinfo *rest = NULL;
00bbdbda 4517 unsigned int i;
cdddbdbc 4518
604b746f
JD
4519 /* do not assemble arrays that might have bad blocks */
4520 if (imsm_bbm_log_size(super->anchor)) {
4521 fprintf(stderr, Name ": BBM log found in metadata. "
4522 "Cannot activate array(s).\n");
4523 return NULL;
4524 }
4525
4f5bc454 4526 for (i = 0; i < mpb->num_raid_devs; i++) {
00bbdbda
N
4527 struct imsm_dev *dev;
4528 struct imsm_map *map;
4f5bc454 4529 struct mdinfo *this;
4f5bc454 4530 int slot;
00bbdbda
N
4531 char *ep;
4532
4533 if (subarray &&
4534 (i != strtoul(subarray, &ep, 10) || *ep != '\0'))
4535 continue;
4536
4537 dev = get_imsm_dev(super, i);
4538 map = get_imsm_map(dev, 0);
4f5bc454 4539
1ce0101c
DW
4540 /* do not publish arrays that are in the middle of an
4541 * unsupported migration
4542 */
4543 if (dev->vol.migr_state &&
28bce06f 4544 (migr_type(dev) == MIGR_STATE_CHANGE)) {
1ce0101c
DW
4545 fprintf(stderr, Name ": cannot assemble volume '%.16s':"
4546 " unsupported migration in progress\n",
4547 dev->volume);
4548 continue;
4549 }
4550
4f5bc454 4551 this = malloc(sizeof(*this));
0fbd635c 4552 if (!this) {
cf1be220 4553 fprintf(stderr, Name ": failed to allocate %zu bytes\n",
0fbd635c
AW
4554 sizeof(*this));
4555 break;
4556 }
4f5bc454
DW
4557 memset(this, 0, sizeof(*this));
4558 this->next = rest;
4f5bc454 4559
301406c9 4560 super->current_vol = i;
a5d85af7 4561 getinfo_super_imsm_volume(st, this, NULL);
4f5bc454 4562 for (slot = 0 ; slot < map->num_members; slot++) {
1e5c6983 4563 unsigned long long recovery_start;
4f5bc454
DW
4564 struct mdinfo *info_d;
4565 struct dl *d;
4566 int idx;
9a1608e5 4567 int skip;
7eef0453 4568 __u32 ord;
4f5bc454 4569
9a1608e5 4570 skip = 0;
ff077194 4571 idx = get_imsm_disk_idx(dev, slot);
7eef0453 4572 ord = get_imsm_ord_tbl_ent(dev, slot);
4f5bc454
DW
4573 for (d = super->disks; d ; d = d->next)
4574 if (d->index == idx)
0fbd635c 4575 break;
4f5bc454 4576
1e5c6983 4577 recovery_start = MaxSector;
4f5bc454 4578 if (d == NULL)
9a1608e5 4579 skip = 1;
25ed7e59 4580 if (d && is_failed(&d->disk))
9a1608e5 4581 skip = 1;
7eef0453 4582 if (ord & IMSM_ORD_REBUILD)
1e5c6983 4583 recovery_start = 0;
9a1608e5
DW
4584
4585 /*
4586 * if we skip some disks the array will be assmebled degraded;
1e5c6983
DW
4587 * reset resync start to avoid a dirty-degraded
4588 * situation when performing the intial sync
9a1608e5
DW
4589 *
4590 * FIXME handle dirty degraded
4591 */
1e5c6983 4592 if ((skip || recovery_start == 0) && !dev->vol.dirty)
b7528a20 4593 this->resync_start = MaxSector;
9a1608e5
DW
4594 if (skip)
4595 continue;
4f5bc454 4596
1e5c6983 4597 info_d = calloc(1, sizeof(*info_d));
9a1608e5
DW
4598 if (!info_d) {
4599 fprintf(stderr, Name ": failed to allocate disk"
1ce0101c 4600 " for volume %.16s\n", dev->volume);
1e5c6983
DW
4601 info_d = this->devs;
4602 while (info_d) {
4603 struct mdinfo *d = info_d->next;
4604
4605 free(info_d);
4606 info_d = d;
4607 }
9a1608e5
DW
4608 free(this);
4609 this = rest;
4610 break;
4611 }
4f5bc454
DW
4612 info_d->next = this->devs;
4613 this->devs = info_d;
4614
4f5bc454
DW
4615 info_d->disk.number = d->index;
4616 info_d->disk.major = d->major;
4617 info_d->disk.minor = d->minor;
4618 info_d->disk.raid_disk = slot;
1e5c6983 4619 info_d->recovery_start = recovery_start;
4f5bc454 4620
1e5c6983
DW
4621 if (info_d->recovery_start == MaxSector)
4622 this->array.working_disks++;
4f5bc454
DW
4623
4624 info_d->events = __le32_to_cpu(mpb->generation_num);
4625 info_d->data_offset = __le32_to_cpu(map->pba_of_lba0);
4626 info_d->component_size = __le32_to_cpu(map->blocks_per_member);
4f5bc454 4627 }
1e5c6983
DW
4628 /* now that the disk list is up-to-date fixup recovery_start */
4629 update_recovery_start(dev, this);
9a1608e5 4630 rest = this;
4f5bc454
DW
4631 }
4632
4633 return rest;
cdddbdbc
DW
4634}
4635
845dea95 4636
fb49eef2 4637static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed)
c2a1e7da 4638{
a965f303 4639 struct imsm_map *map = get_imsm_map(dev, 0);
c2a1e7da
DW
4640
4641 if (!failed)
3393c6af
DW
4642 return map->map_state == IMSM_T_STATE_UNINITIALIZED ?
4643 IMSM_T_STATE_UNINITIALIZED : IMSM_T_STATE_NORMAL;
c2a1e7da
DW
4644
4645 switch (get_imsm_raid_level(map)) {
4646 case 0:
4647 return IMSM_T_STATE_FAILED;
4648 break;
4649 case 1:
4650 if (failed < map->num_members)
4651 return IMSM_T_STATE_DEGRADED;
4652 else
4653 return IMSM_T_STATE_FAILED;
4654 break;
4655 case 10:
4656 {
4657 /**
c92a2527
DW
4658 * check to see if any mirrors have failed, otherwise we
4659 * are degraded. Even numbered slots are mirrored on
4660 * slot+1
c2a1e7da 4661 */
c2a1e7da 4662 int i;
d9b420a5
N
4663 /* gcc -Os complains that this is unused */
4664 int insync = insync;
c2a1e7da
DW
4665
4666 for (i = 0; i < map->num_members; i++) {
c92a2527
DW
4667 __u32 ord = get_imsm_ord_tbl_ent(dev, i);
4668 int idx = ord_to_idx(ord);
4669 struct imsm_disk *disk;
c2a1e7da 4670
c92a2527
DW
4671 /* reset the potential in-sync count on even-numbered
4672 * slots. num_copies is always 2 for imsm raid10
4673 */
4674 if ((i & 1) == 0)
4675 insync = 2;
c2a1e7da 4676
c92a2527 4677 disk = get_imsm_disk(super, idx);
25ed7e59 4678 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
c92a2527 4679 insync--;
c2a1e7da 4680
c92a2527
DW
4681 /* no in-sync disks left in this mirror the
4682 * array has failed
4683 */
4684 if (insync == 0)
4685 return IMSM_T_STATE_FAILED;
c2a1e7da
DW
4686 }
4687
4688 return IMSM_T_STATE_DEGRADED;
4689 }
4690 case 5:
4691 if (failed < 2)
4692 return IMSM_T_STATE_DEGRADED;
4693 else
4694 return IMSM_T_STATE_FAILED;
4695 break;
4696 default:
4697 break;
4698 }
4699
4700 return map->map_state;
4701}
4702
ff077194 4703static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev)
c2a1e7da
DW
4704{
4705 int i;
4706 int failed = 0;
4707 struct imsm_disk *disk;
ff077194 4708 struct imsm_map *map = get_imsm_map(dev, 0);
0556e1a2
DW
4709 struct imsm_map *prev = get_imsm_map(dev, dev->vol.migr_state);
4710 __u32 ord;
4711 int idx;
c2a1e7da 4712
0556e1a2
DW
4713 /* at the beginning of migration we set IMSM_ORD_REBUILD on
4714 * disks that are being rebuilt. New failures are recorded to
4715 * map[0]. So we look through all the disks we started with and
4716 * see if any failures are still present, or if any new ones
4717 * have arrived
4718 *
4719 * FIXME add support for online capacity expansion and
4720 * raid-level-migration
4721 */
4722 for (i = 0; i < prev->num_members; i++) {
4723 ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
4724 ord |= __le32_to_cpu(map->disk_ord_tbl[i]);
4725 idx = ord_to_idx(ord);
c2a1e7da 4726
949c47a0 4727 disk = get_imsm_disk(super, idx);
25ed7e59 4728 if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
fcb84475 4729 failed++;
c2a1e7da
DW
4730 }
4731
4732 return failed;
845dea95
NB
4733}
4734
97b4d0e9
DW
4735#ifndef MDASSEMBLE
4736static int imsm_open_new(struct supertype *c, struct active_array *a,
4737 char *inst)
4738{
4739 struct intel_super *super = c->sb;
4740 struct imsm_super *mpb = super->anchor;
4741
4742 if (atoi(inst) >= mpb->num_raid_devs) {
4743 fprintf(stderr, "%s: subarry index %d, out of range\n",
4744 __func__, atoi(inst));
4745 return -ENODEV;
4746 }
4747
4748 dprintf("imsm: open_new %s\n", inst);
4749 a->info.container_member = atoi(inst);
4750 return 0;
4751}
4752
0c046afd
DW
4753static int is_resyncing(struct imsm_dev *dev)
4754{
4755 struct imsm_map *migr_map;
4756
4757 if (!dev->vol.migr_state)
4758 return 0;
4759
1484e727
DW
4760 if (migr_type(dev) == MIGR_INIT ||
4761 migr_type(dev) == MIGR_REPAIR)
0c046afd
DW
4762 return 1;
4763
4c9bc37b
AK
4764 if (migr_type(dev) == MIGR_GEN_MIGR)
4765 return 0;
4766
0c046afd
DW
4767 migr_map = get_imsm_map(dev, 1);
4768
4c9bc37b
AK
4769 if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
4770 (dev->vol.migr_type != MIGR_GEN_MIGR))
0c046afd
DW
4771 return 1;
4772 else
4773 return 0;
4774}
4775
0556e1a2
DW
4776/* return true if we recorded new information */
4777static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
47ee5a45 4778{
0556e1a2
DW
4779 __u32 ord;
4780 int slot;
4781 struct imsm_map *map;
4782
4783 /* new failures are always set in map[0] */
4784 map = get_imsm_map(dev, 0);
4785
4786 slot = get_imsm_disk_slot(map, idx);
4787 if (slot < 0)
4788 return 0;
4789
4790 ord = __le32_to_cpu(map->disk_ord_tbl[slot]);
25ed7e59 4791 if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
0556e1a2
DW
4792 return 0;
4793
f2f27e63 4794 disk->status |= FAILED_DISK;
cf53434e 4795 disk->status &= ~CONFIGURED_DISK;
0556e1a2 4796 set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
f21e18ca 4797 if (map->failed_disk_num == 0xff)
0556e1a2
DW
4798 map->failed_disk_num = slot;
4799 return 1;
4800}
4801
4802static void mark_missing(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
4803{
4804 mark_failure(dev, disk, idx);
4805
4806 if (disk->scsi_id == __cpu_to_le32(~(__u32)0))
4807 return;
4808
47ee5a45
DW
4809 disk->scsi_id = __cpu_to_le32(~(__u32)0);
4810 memmove(&disk->serial[0], &disk->serial[1], MAX_RAID_SERIAL_LEN - 1);
4811}
4812
33414a01
DW
4813static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
4814{
4815 __u8 map_state;
4816 struct dl *dl;
4817 int failed;
4818
4819 if (!super->missing)
4820 return;
4821 failed = imsm_count_failed(super, dev);
4822 map_state = imsm_check_degraded(super, dev, failed);
4823
4824 dprintf("imsm: mark missing\n");
4825 end_migration(dev, map_state);
4826 for (dl = super->missing; dl; dl = dl->next)
4827 mark_missing(dev, &dl->disk, dl->index);
4828 super->updates_pending++;
4829}
4830
28bce06f
AK
4831static void imsm_set_disk(struct active_array *a, int n, int state);
4832
aad6f216 4833/* Handle dirty -> clean transititions, resync and reshape. Degraded and rebuild
0c046afd
DW
4834 * states are handled in imsm_set_disk() with one exception, when a
4835 * resync is stopped due to a new failure this routine will set the
4836 * 'degraded' state for the array.
4837 */
01f157d7 4838static int imsm_set_array_state(struct active_array *a, int consistent)
a862209d
DW
4839{
4840 int inst = a->info.container_member;
4841 struct intel_super *super = a->container->sb;
949c47a0 4842 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 4843 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd
DW
4844 int failed = imsm_count_failed(super, dev);
4845 __u8 map_state = imsm_check_degraded(super, dev, failed);
1e5c6983 4846 __u32 blocks_per_unit;
a862209d 4847
1af97990
AK
4848 if (dev->vol.migr_state &&
4849 dev->vol.migr_type == MIGR_GEN_MIGR) {
4850 /* array state change is blocked due to reshape action
aad6f216
N
4851 * We might need to
4852 * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
4853 * - finish the reshape (if last_checkpoint is big and action != reshape)
4854 * - update curr_migr_unit
1af97990 4855 */
aad6f216
N
4856 if (a->curr_action == reshape) {
4857 /* still reshaping, maybe update curr_migr_unit */
4858 long long blocks_per_unit = blocks_per_migr_unit(dev);
4859 long long unit = a->last_checkpoint;
4860 unit /= blocks_per_unit;
4861 if (unit > __le32_to_cpu(dev->vol.curr_migr_unit)) {
4862 dev->vol.curr_migr_unit = __cpu_to_le32(unit);
4863 super->updates_pending++;
4864 }
4865 } else {
4866 if (a->last_checkpoint == 0 && a->prev_action == reshape) {
4867 /* for some reason we aborted the reshape.
4868 * Better clean up
4869 */
4870 struct imsm_map *map2 = get_imsm_map(dev, 1);
4871 dev->vol.migr_state = 0;
4872 dev->vol.migr_type = 0;
4873 dev->vol.curr_migr_unit = 0;
4874 memcpy(map, map2, sizeof_imsm_map(map2));
4875 super->updates_pending++;
4876 }
4877 if (a->last_checkpoint >= a->info.component_size) {
4878 unsigned long long array_blocks;
4879 int used_disks;
4880 /* it seems the reshape is all done */
4881 dev->vol.migr_state = 0;
4882 dev->vol.migr_type = 0;
4883 dev->vol.curr_migr_unit = 0;
4884
4885 used_disks = imsm_num_data_members(dev);
4886 array_blocks = map->blocks_per_member * used_disks;
4887 /* round array size down to closest MB */
4888 array_blocks = (array_blocks >> SECT_PER_MB_SHIFT)
4889 << SECT_PER_MB_SHIFT;
4890 dev->size_low = __cpu_to_le32((__u32) array_blocks);
4891 dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
4892 a->info.custom_array_size = array_blocks;
4893 a->check_reshape = 1; /* encourage manager to update
4894 * array size
4895 */
4896 super->updates_pending++;
4897 }
4898 }
4899 return 0;
1af97990
AK
4900 }
4901
47ee5a45 4902 /* before we activate this array handle any missing disks */
33414a01
DW
4903 if (consistent == 2)
4904 handle_missing(super, dev);
1e5c6983 4905
0c046afd 4906 if (consistent == 2 &&
b7941fd6 4907 (!is_resync_complete(&a->info) ||
0c046afd
DW
4908 map_state != IMSM_T_STATE_NORMAL ||
4909 dev->vol.migr_state))
01f157d7 4910 consistent = 0;
272906ef 4911
b7941fd6 4912 if (is_resync_complete(&a->info)) {
0c046afd 4913 /* complete intialization / resync,
0556e1a2
DW
4914 * recovery and interrupted recovery is completed in
4915 * ->set_disk
0c046afd
DW
4916 */
4917 if (is_resyncing(dev)) {
4918 dprintf("imsm: mark resync done\n");
f8f603f1 4919 end_migration(dev, map_state);
115c3803 4920 super->updates_pending++;
484240d8 4921 a->last_checkpoint = 0;
115c3803 4922 }
0c046afd
DW
4923 } else if (!is_resyncing(dev) && !failed) {
4924 /* mark the start of the init process if nothing is failed */
b7941fd6 4925 dprintf("imsm: mark resync start\n");
1484e727 4926 if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
e3bba0e0 4927 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_INIT);
1484e727
DW
4928 else
4929 migrate(dev, IMSM_T_STATE_NORMAL, MIGR_REPAIR);
3393c6af 4930 super->updates_pending++;
115c3803 4931 }
a862209d 4932
1e5c6983
DW
4933 /* check if we can update curr_migr_unit from resync_start, recovery_start */
4934 blocks_per_unit = blocks_per_migr_unit(dev);
4f0a7acc 4935 if (blocks_per_unit) {
1e5c6983
DW
4936 __u32 units32;
4937 __u64 units;
4938
4f0a7acc 4939 units = a->last_checkpoint / blocks_per_unit;
1e5c6983
DW
4940 units32 = units;
4941
4942 /* check that we did not overflow 32-bits, and that
4943 * curr_migr_unit needs updating
4944 */
4945 if (units32 == units &&
4946 __le32_to_cpu(dev->vol.curr_migr_unit) != units32) {
4947 dprintf("imsm: mark checkpoint (%u)\n", units32);
4948 dev->vol.curr_migr_unit = __cpu_to_le32(units32);
4949 super->updates_pending++;
4950 }
4951 }
f8f603f1 4952
3393c6af 4953 /* mark dirty / clean */
0c046afd 4954 if (dev->vol.dirty != !consistent) {
b7941fd6 4955 dprintf("imsm: mark '%s'\n", consistent ? "clean" : "dirty");
0c046afd
DW
4956 if (consistent)
4957 dev->vol.dirty = 0;
4958 else
4959 dev->vol.dirty = 1;
a862209d
DW
4960 super->updates_pending++;
4961 }
28bce06f
AK
4962
4963 /* finalize online capacity expansion/reshape */
4964 if ((a->curr_action != reshape) &&
4965 (a->prev_action == reshape)) {
4966 struct mdinfo *mdi;
4967
4968 for (mdi = a->info.devs; mdi; mdi = mdi->next)
4969 imsm_set_disk(a, mdi->disk.raid_disk, mdi->curr_state);
4970 }
4971
01f157d7 4972 return consistent;
a862209d
DW
4973}
4974
8d45d196 4975static void imsm_set_disk(struct active_array *a, int n, int state)
845dea95 4976{
8d45d196
DW
4977 int inst = a->info.container_member;
4978 struct intel_super *super = a->container->sb;
949c47a0 4979 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 4980 struct imsm_map *map = get_imsm_map(dev, 0);
8d45d196 4981 struct imsm_disk *disk;
0c046afd 4982 int failed;
b10b37b8 4983 __u32 ord;
0c046afd 4984 __u8 map_state;
8d45d196
DW
4985
4986 if (n > map->num_members)
4987 fprintf(stderr, "imsm: set_disk %d out of range 0..%d\n",
4988 n, map->num_members - 1);
4989
4990 if (n < 0)
4991 return;
4992
4e6e574a 4993 dprintf("imsm: set_disk %d:%x\n", n, state);
8d45d196 4994
b10b37b8
DW
4995 ord = get_imsm_ord_tbl_ent(dev, n);
4996 disk = get_imsm_disk(super, ord_to_idx(ord));
8d45d196 4997
5802a811 4998 /* check for new failures */
0556e1a2
DW
4999 if (state & DS_FAULTY) {
5000 if (mark_failure(dev, disk, ord_to_idx(ord)))
5001 super->updates_pending++;
8d45d196 5002 }
47ee5a45 5003
19859edc 5004 /* check if in_sync */
0556e1a2 5005 if (state & DS_INSYNC && ord & IMSM_ORD_REBUILD && is_rebuilding(dev)) {
b10b37b8
DW
5006 struct imsm_map *migr_map = get_imsm_map(dev, 1);
5007
5008 set_imsm_ord_tbl_ent(migr_map, n, ord_to_idx(ord));
19859edc
DW
5009 super->updates_pending++;
5010 }
8d45d196 5011
0c046afd
DW
5012 failed = imsm_count_failed(super, dev);
5013 map_state = imsm_check_degraded(super, dev, failed);
5802a811 5014
0c046afd
DW
5015 /* check if recovery complete, newly degraded, or failed */
5016 if (map_state == IMSM_T_STATE_NORMAL && is_rebuilding(dev)) {
f8f603f1 5017 end_migration(dev, map_state);
0556e1a2
DW
5018 map = get_imsm_map(dev, 0);
5019 map->failed_disk_num = ~0;
0c046afd 5020 super->updates_pending++;
484240d8 5021 a->last_checkpoint = 0;
0c046afd
DW
5022 } else if (map_state == IMSM_T_STATE_DEGRADED &&
5023 map->map_state != map_state &&
5024 !dev->vol.migr_state) {
5025 dprintf("imsm: mark degraded\n");
5026 map->map_state = map_state;
5027 super->updates_pending++;
484240d8 5028 a->last_checkpoint = 0;
0c046afd
DW
5029 } else if (map_state == IMSM_T_STATE_FAILED &&
5030 map->map_state != map_state) {
5031 dprintf("imsm: mark failed\n");
f8f603f1 5032 end_migration(dev, map_state);
0c046afd 5033 super->updates_pending++;
484240d8 5034 a->last_checkpoint = 0;
28bce06f
AK
5035 } else if (is_gen_migration(dev)) {
5036 dprintf("imsm: Detected General Migration in state: ");
5037 if (map_state == IMSM_T_STATE_NORMAL) {
5038 end_migration(dev, map_state);
5039 map = get_imsm_map(dev, 0);
5040 map->failed_disk_num = ~0;
5041 dprintf("normal\n");
5042 } else {
5043 if (map_state == IMSM_T_STATE_DEGRADED) {
5044 printf("degraded\n");
5045 end_migration(dev, map_state);
5046 } else {
5047 dprintf("failed\n");
5048 }
5049 map->map_state = map_state;
5050 }
5051 super->updates_pending++;
5802a811 5052 }
845dea95
NB
5053}
5054
f796af5d 5055static int store_imsm_mpb(int fd, struct imsm_super *mpb)
c2a1e7da 5056{
f796af5d 5057 void *buf = mpb;
c2a1e7da
DW
5058 __u32 mpb_size = __le32_to_cpu(mpb->mpb_size);
5059 unsigned long long dsize;
5060 unsigned long long sectors;
5061
5062 get_dev_size(fd, NULL, &dsize);
5063
272f648f
DW
5064 if (mpb_size > 512) {
5065 /* -1 to account for anchor */
5066 sectors = mpb_sectors(mpb) - 1;
c2a1e7da 5067
272f648f
DW
5068 /* write the extended mpb to the sectors preceeding the anchor */
5069 if (lseek64(fd, dsize - (512 * (2 + sectors)), SEEK_SET) < 0)
5070 return 1;
c2a1e7da 5071
f21e18ca
N
5072 if ((unsigned long long)write(fd, buf + 512, 512 * sectors)
5073 != 512 * sectors)
272f648f
DW
5074 return 1;
5075 }
c2a1e7da 5076
272f648f
DW
5077 /* first block is stored on second to last sector of the disk */
5078 if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0)
c2a1e7da
DW
5079 return 1;
5080
f796af5d 5081 if (write(fd, buf, 512) != 512)
c2a1e7da
DW
5082 return 1;
5083
c2a1e7da
DW
5084 return 0;
5085}
5086
2e735d19 5087static void imsm_sync_metadata(struct supertype *container)
845dea95 5088{
2e735d19 5089 struct intel_super *super = container->sb;
c2a1e7da 5090
1a64be56 5091 dprintf("sync metadata: %d\n", super->updates_pending);
c2a1e7da
DW
5092 if (!super->updates_pending)
5093 return;
5094
36988a3d 5095 write_super_imsm(container, 0);
c2a1e7da
DW
5096
5097 super->updates_pending = 0;
845dea95
NB
5098}
5099
272906ef
DW
5100static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
5101{
5102 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
ff077194 5103 int i = get_imsm_disk_idx(dev, idx);
272906ef
DW
5104 struct dl *dl;
5105
5106 for (dl = super->disks; dl; dl = dl->next)
5107 if (dl->index == i)
5108 break;
5109
25ed7e59 5110 if (dl && is_failed(&dl->disk))
272906ef
DW
5111 dl = NULL;
5112
5113 if (dl)
5114 dprintf("%s: found %x:%x\n", __func__, dl->major, dl->minor);
5115
5116 return dl;
5117}
5118
a20d2ba5 5119static struct dl *imsm_add_spare(struct intel_super *super, int slot,
8ba77d32
AK
5120 struct active_array *a, int activate_new,
5121 struct mdinfo *additional_test_list)
272906ef
DW
5122{
5123 struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
e553d2a4 5124 int idx = get_imsm_disk_idx(dev, slot);
a20d2ba5
DW
5125 struct imsm_super *mpb = super->anchor;
5126 struct imsm_map *map;
272906ef
DW
5127 unsigned long long pos;
5128 struct mdinfo *d;
5129 struct extent *ex;
a20d2ba5 5130 int i, j;
272906ef 5131 int found;
569cc43f
DW
5132 __u32 array_start = 0;
5133 __u32 array_end = 0;
272906ef 5134 struct dl *dl;
6c932028 5135 struct mdinfo *test_list;
272906ef
DW
5136
5137 for (dl = super->disks; dl; dl = dl->next) {
5138 /* If in this array, skip */
5139 for (d = a->info.devs ; d ; d = d->next)
e553d2a4
DW
5140 if (d->state_fd >= 0 &&
5141 d->disk.major == dl->major &&
272906ef 5142 d->disk.minor == dl->minor) {
8ba77d32
AK
5143 dprintf("%x:%x already in array\n",
5144 dl->major, dl->minor);
272906ef
DW
5145 break;
5146 }
5147 if (d)
5148 continue;
6c932028
AK
5149 test_list = additional_test_list;
5150 while (test_list) {
5151 if (test_list->disk.major == dl->major &&
5152 test_list->disk.minor == dl->minor) {
8ba77d32
AK
5153 dprintf("%x:%x already in additional test list\n",
5154 dl->major, dl->minor);
5155 break;
5156 }
6c932028 5157 test_list = test_list->next;
8ba77d32 5158 }
6c932028 5159 if (test_list)
8ba77d32 5160 continue;
272906ef 5161
e553d2a4 5162 /* skip in use or failed drives */
25ed7e59 5163 if (is_failed(&dl->disk) || idx == dl->index ||
df474657
DW
5164 dl->index == -2) {
5165 dprintf("%x:%x status (failed: %d index: %d)\n",
25ed7e59 5166 dl->major, dl->minor, is_failed(&dl->disk), idx);
9a1608e5
DW
5167 continue;
5168 }
5169
a20d2ba5
DW
5170 /* skip pure spares when we are looking for partially
5171 * assimilated drives
5172 */
5173 if (dl->index == -1 && !activate_new)
5174 continue;
5175
272906ef 5176 /* Does this unused device have the requisite free space?
a20d2ba5 5177 * It needs to be able to cover all member volumes
272906ef
DW
5178 */
5179 ex = get_extents(super, dl);
5180 if (!ex) {
5181 dprintf("cannot get extents\n");
5182 continue;
5183 }
a20d2ba5
DW
5184 for (i = 0; i < mpb->num_raid_devs; i++) {
5185 dev = get_imsm_dev(super, i);
5186 map = get_imsm_map(dev, 0);
272906ef 5187
a20d2ba5
DW
5188 /* check if this disk is already a member of
5189 * this array
272906ef 5190 */
620b1713 5191 if (get_imsm_disk_slot(map, dl->index) >= 0)
a20d2ba5
DW
5192 continue;
5193
5194 found = 0;
5195 j = 0;
5196 pos = 0;
5197 array_start = __le32_to_cpu(map->pba_of_lba0);
329c8278
DW
5198 array_end = array_start +
5199 __le32_to_cpu(map->blocks_per_member) - 1;
a20d2ba5
DW
5200
5201 do {
5202 /* check that we can start at pba_of_lba0 with
5203 * blocks_per_member of space
5204 */
329c8278 5205 if (array_start >= pos && array_end < ex[j].start) {
a20d2ba5
DW
5206 found = 1;
5207 break;
5208 }
5209 pos = ex[j].start + ex[j].size;
5210 j++;
5211 } while (ex[j-1].size);
5212
5213 if (!found)
272906ef 5214 break;
a20d2ba5 5215 }
272906ef
DW
5216
5217 free(ex);
a20d2ba5 5218 if (i < mpb->num_raid_devs) {
329c8278
DW
5219 dprintf("%x:%x does not have %u to %u available\n",
5220 dl->major, dl->minor, array_start, array_end);
272906ef
DW
5221 /* No room */
5222 continue;
a20d2ba5
DW
5223 }
5224 return dl;
272906ef
DW
5225 }
5226
5227 return dl;
5228}
5229
95d07a2c
LM
5230
5231static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
5232{
5233 struct imsm_dev *dev2;
5234 struct imsm_map *map;
5235 struct dl *idisk;
5236 int slot;
5237 int idx;
5238 __u8 state;
5239
5240 dev2 = get_imsm_dev(cont->sb, dev_idx);
5241 if (dev2) {
5242 state = imsm_check_degraded(cont->sb, dev2, failed);
5243 if (state == IMSM_T_STATE_FAILED) {
5244 map = get_imsm_map(dev2, 0);
5245 if (!map)
5246 return 1;
5247 for (slot = 0; slot < map->num_members; slot++) {
5248 /*
5249 * Check if failed disks are deleted from intel
5250 * disk list or are marked to be deleted
5251 */
5252 idx = get_imsm_disk_idx(dev2, slot);
5253 idisk = get_imsm_dl_disk(cont->sb, idx);
5254 /*
5255 * Do not rebuild the array if failed disks
5256 * from failed sub-array are not removed from
5257 * container.
5258 */
5259 if (idisk &&
5260 is_failed(&idisk->disk) &&
5261 (idisk->action != DISK_REMOVE))
5262 return 0;
5263 }
5264 }
5265 }
5266 return 1;
5267}
5268
88758e9d
DW
5269static struct mdinfo *imsm_activate_spare(struct active_array *a,
5270 struct metadata_update **updates)
5271{
5272 /**
d23fe947
DW
5273 * Find a device with unused free space and use it to replace a
5274 * failed/vacant region in an array. We replace failed regions one a
5275 * array at a time. The result is that a new spare disk will be added
5276 * to the first failed array and after the monitor has finished
5277 * propagating failures the remainder will be consumed.
88758e9d 5278 *
d23fe947
DW
5279 * FIXME add a capability for mdmon to request spares from another
5280 * container.
88758e9d
DW
5281 */
5282
5283 struct intel_super *super = a->container->sb;
88758e9d 5284 int inst = a->info.container_member;
949c47a0 5285 struct imsm_dev *dev = get_imsm_dev(super, inst);
a965f303 5286 struct imsm_map *map = get_imsm_map(dev, 0);
88758e9d
DW
5287 int failed = a->info.array.raid_disks;
5288 struct mdinfo *rv = NULL;
5289 struct mdinfo *d;
5290 struct mdinfo *di;
5291 struct metadata_update *mu;
5292 struct dl *dl;
5293 struct imsm_update_activate_spare *u;
5294 int num_spares = 0;
5295 int i;
95d07a2c 5296 int allowed;
88758e9d
DW
5297
5298 for (d = a->info.devs ; d ; d = d->next) {
5299 if ((d->curr_state & DS_FAULTY) &&
5300 d->state_fd >= 0)
5301 /* wait for Removal to happen */
5302 return NULL;
5303 if (d->state_fd >= 0)
5304 failed--;
5305 }
5306
5307 dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
5308 inst, failed, a->info.array.raid_disks, a->info.array.level);
1af97990
AK
5309
5310 if (dev->vol.migr_state &&
5311 dev->vol.migr_type == MIGR_GEN_MIGR)
5312 /* No repair during migration */
5313 return NULL;
5314
fb49eef2 5315 if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
88758e9d
DW
5316 return NULL;
5317
95d07a2c
LM
5318 /*
5319 * If there are any failed disks check state of the other volume.
5320 * Block rebuild if the another one is failed until failed disks
5321 * are removed from container.
5322 */
5323 if (failed) {
5324 dprintf("found failed disks in %s, check if there another"
5325 "failed sub-array.\n",
5326 dev->volume);
5327 /* check if states of the other volumes allow for rebuild */
5328 for (i = 0; i < super->anchor->num_raid_devs; i++) {
5329 if (i != inst) {
5330 allowed = imsm_rebuild_allowed(a->container,
5331 i, failed);
5332 if (!allowed)
5333 return NULL;
5334 }
5335 }
5336 }
5337
88758e9d 5338 /* For each slot, if it is not working, find a spare */
88758e9d
DW
5339 for (i = 0; i < a->info.array.raid_disks; i++) {
5340 for (d = a->info.devs ; d ; d = d->next)
5341 if (d->disk.raid_disk == i)
5342 break;
5343 dprintf("found %d: %p %x\n", i, d, d?d->curr_state:0);
5344 if (d && (d->state_fd >= 0))
5345 continue;
5346
272906ef 5347 /*
a20d2ba5
DW
5348 * OK, this device needs recovery. Try to re-add the
5349 * previous occupant of this slot, if this fails see if
5350 * we can continue the assimilation of a spare that was
5351 * partially assimilated, finally try to activate a new
5352 * spare.
272906ef
DW
5353 */
5354 dl = imsm_readd(super, i, a);
5355 if (!dl)
8ba77d32 5356 dl = imsm_add_spare(super, i, a, 0, NULL);
a20d2ba5 5357 if (!dl)
8ba77d32 5358 dl = imsm_add_spare(super, i, a, 1, NULL);
272906ef
DW
5359 if (!dl)
5360 continue;
5361
5362 /* found a usable disk with enough space */
5363 di = malloc(sizeof(*di));
79244939
DW
5364 if (!di)
5365 continue;
272906ef
DW
5366 memset(di, 0, sizeof(*di));
5367
5368 /* dl->index will be -1 in the case we are activating a
5369 * pristine spare. imsm_process_update() will create a
5370 * new index in this case. Once a disk is found to be
5371 * failed in all member arrays it is kicked from the
5372 * metadata
5373 */
5374 di->disk.number = dl->index;
d23fe947 5375
272906ef
DW
5376 /* (ab)use di->devs to store a pointer to the device
5377 * we chose
5378 */
5379 di->devs = (struct mdinfo *) dl;
5380
5381 di->disk.raid_disk = i;
5382 di->disk.major = dl->major;
5383 di->disk.minor = dl->minor;
5384 di->disk.state = 0;
d23534e4 5385 di->recovery_start = 0;
272906ef
DW
5386 di->data_offset = __le32_to_cpu(map->pba_of_lba0);
5387 di->component_size = a->info.component_size;
5388 di->container_member = inst;
148acb7b 5389 super->random = random32();
272906ef
DW
5390 di->next = rv;
5391 rv = di;
5392 num_spares++;
5393 dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
5394 i, di->data_offset);
88758e9d 5395
272906ef 5396 break;
88758e9d
DW
5397 }
5398
5399 if (!rv)
5400 /* No spares found */
5401 return rv;
5402 /* Now 'rv' has a list of devices to return.
5403 * Create a metadata_update record to update the
5404 * disk_ord_tbl for the array
5405 */
5406 mu = malloc(sizeof(*mu));
79244939
DW
5407 if (mu) {
5408 mu->buf = malloc(sizeof(struct imsm_update_activate_spare) * num_spares);
5409 if (mu->buf == NULL) {
5410 free(mu);
5411 mu = NULL;
5412 }
5413 }
5414 if (!mu) {
5415 while (rv) {
5416 struct mdinfo *n = rv->next;
5417
5418 free(rv);
5419 rv = n;
5420 }
5421 return NULL;
5422 }
5423
88758e9d 5424 mu->space = NULL;
cb23f1f4 5425 mu->space_list = NULL;
88758e9d
DW
5426 mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
5427 mu->next = *updates;
5428 u = (struct imsm_update_activate_spare *) mu->buf;
5429
5430 for (di = rv ; di ; di = di->next) {
5431 u->type = update_activate_spare;
d23fe947
DW
5432 u->dl = (struct dl *) di->devs;
5433 di->devs = NULL;
88758e9d
DW
5434 u->slot = di->disk.raid_disk;
5435 u->array = inst;
5436 u->next = u + 1;
5437 u++;
5438 }
5439 (u-1)->next = NULL;
5440 *updates = mu;
5441
5442 return rv;
5443}
5444
54c2c1ea 5445static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_create_array *u)
8273f55e 5446{
54c2c1ea
DW
5447 struct imsm_dev *dev = get_imsm_dev(super, idx);
5448 struct imsm_map *map = get_imsm_map(dev, 0);
5449 struct imsm_map *new_map = get_imsm_map(&u->dev, 0);
5450 struct disk_info *inf = get_disk_info(u);
5451 struct imsm_disk *disk;
8273f55e
DW
5452 int i;
5453 int j;
8273f55e 5454
54c2c1ea
DW
5455 for (i = 0; i < map->num_members; i++) {
5456 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
5457 for (j = 0; j < new_map->num_members; j++)
5458 if (serialcmp(disk->serial, inf[j].serial) == 0)
8273f55e
DW
5459 return 1;
5460 }
5461
5462 return 0;
5463}
5464
1a64be56
LM
5465
5466static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
5467{
5468 struct dl *dl = NULL;
5469 for (dl = super->disks; dl; dl = dl->next)
5470 if ((dl->major == major) && (dl->minor == minor))
5471 return dl;
5472 return NULL;
5473}
5474
5475static int remove_disk_super(struct intel_super *super, int major, int minor)
5476{
5477 struct dl *prev = NULL;
5478 struct dl *dl;
5479
5480 prev = NULL;
5481 for (dl = super->disks; dl; dl = dl->next) {
5482 if ((dl->major == major) && (dl->minor == minor)) {
5483 /* remove */
5484 if (prev)
5485 prev->next = dl->next;
5486 else
5487 super->disks = dl->next;
5488 dl->next = NULL;
5489 __free_imsm_disk(dl);
5490 dprintf("%s: removed %x:%x\n",
5491 __func__, major, minor);
5492 break;
5493 }
5494 prev = dl;
5495 }
5496 return 0;
5497}
5498
f21e18ca 5499static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
ae6aad82 5500
1a64be56
LM
5501static int add_remove_disk_update(struct intel_super *super)
5502{
5503 int check_degraded = 0;
5504 struct dl *disk = NULL;
5505 /* add/remove some spares to/from the metadata/contrainer */
5506 while (super->disk_mgmt_list) {
5507 struct dl *disk_cfg;
5508
5509 disk_cfg = super->disk_mgmt_list;
5510 super->disk_mgmt_list = disk_cfg->next;
5511 disk_cfg->next = NULL;
5512
5513 if (disk_cfg->action == DISK_ADD) {
5514 disk_cfg->next = super->disks;
5515 super->disks = disk_cfg;
5516 check_degraded = 1;
5517 dprintf("%s: added %x:%x\n",
5518 __func__, disk_cfg->major,
5519 disk_cfg->minor);
5520 } else if (disk_cfg->action == DISK_REMOVE) {
5521 dprintf("Disk remove action processed: %x.%x\n",
5522 disk_cfg->major, disk_cfg->minor);
5523 disk = get_disk_super(super,
5524 disk_cfg->major,
5525 disk_cfg->minor);
5526 if (disk) {
5527 /* store action status */
5528 disk->action = DISK_REMOVE;
5529 /* remove spare disks only */
5530 if (disk->index == -1) {
5531 remove_disk_super(super,
5532 disk_cfg->major,
5533 disk_cfg->minor);
5534 }
5535 }
5536 /* release allocate disk structure */
5537 __free_imsm_disk(disk_cfg);
5538 }
5539 }
5540 return check_degraded;
5541}
5542
e8319a19
DW
5543static void imsm_process_update(struct supertype *st,
5544 struct metadata_update *update)
5545{
5546 /**
5547 * crack open the metadata_update envelope to find the update record
5548 * update can be one of:
d195167d
AK
5549 * update_reshape_container_disks - all the arrays in the container
5550 * are being reshaped to have more devices. We need to mark
5551 * the arrays for general migration and convert selected spares
5552 * into active devices.
5553 * update_activate_spare - a spare device has replaced a failed
e8319a19
DW
5554 * device in an array, update the disk_ord_tbl. If this disk is
5555 * present in all member arrays then also clear the SPARE_DISK
5556 * flag
d195167d
AK
5557 * update_create_array
5558 * update_kill_array
5559 * update_rename_array
5560 * update_add_remove_disk
e8319a19
DW
5561 */
5562 struct intel_super *super = st->sb;
4d7b1503 5563 struct imsm_super *mpb;
e8319a19
DW
5564 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
5565
4d7b1503
DW
5566 /* update requires a larger buf but the allocation failed */
5567 if (super->next_len && !super->next_buf) {
5568 super->next_len = 0;
5569 return;
5570 }
5571
5572 if (super->next_buf) {
5573 memcpy(super->next_buf, super->buf, super->len);
5574 free(super->buf);
5575 super->len = super->next_len;
5576 super->buf = super->next_buf;
5577
5578 super->next_len = 0;
5579 super->next_buf = NULL;
5580 }
5581
5582 mpb = super->anchor;
5583
e8319a19 5584 switch (type) {
78b10e66 5585 case update_reshape_container_disks: {
d195167d
AK
5586 struct imsm_update_reshape *u = (void *)update->buf;
5587 struct dl *new_disk;
5588 struct intel_dev *id;
5589 int i;
5590 int delta_disks = u->new_raid_disks - u->old_raid_disks;
5591 void **tofree = NULL;
5592
5593 dprintf("imsm: imsm_process_update() for update_reshape\n");
5594
5595 /* enable spares to use in array */
5596 for (i = 0; i < delta_disks; i++) {
5597
5598 new_disk = get_disk_super(super,
5599 major(u->new_disks[i]),
5600 minor(u->new_disks[i]));
5601 if (new_disk == NULL || new_disk->index < 0)
5602 goto update_reshape_exit;
5603
5604 new_disk->index = mpb->num_disks++;
5605 /* slot to fill in autolayout */
5606 new_disk->raiddisk = new_disk->index;
5607 new_disk->disk.status |=
5608 CONFIGURED_DISK;
5609 new_disk->disk.status &= ~SPARE_DISK;
5610 }
5611
5612 dprintf("imsm: process_update(): update_reshape: volume set"\
5613 " mpb->num_raid_devs = %i\n", mpb->num_raid_devs);
5614 /* manage changes in volumes
5615 */
5616 for (id = super->devlist ; id; id = id->next) {
5617 void **sp = update->space_list;
5618 struct imsm_dev *newdev;
5619 struct imsm_map *newmap, *oldmap;
5620
5621 if (!sp)
5622 continue;
5623 update->space_list = *sp;
5624 newdev = (void*)sp;
5625 /* Copy the dev, but not (all of) the map */
5626 memcpy(newdev, id->dev, sizeof(*newdev));
5627 oldmap = get_imsm_map(id->dev, 0);
5628 newmap = get_imsm_map(newdev, 0);
5629 /* Copy the current map */
5630 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
5631 newdev->vol.migr_state = 1;
5632 newdev->vol.curr_migr_unit = 0;
5633 newdev->vol.migr_type = MIGR_GEN_MIGR;
5634 newmap->num_members = u->new_raid_disks;
5635 for (i = 0; i < delta_disks; i++) {
5636 set_imsm_ord_tbl_ent(newmap,
5637 u->old_raid_disks + i,
5638 u->old_raid_disks + i);
5639 }
5640 /* New map is correct, now need to save old map */
5641 oldmap = get_imsm_map(newdev, 1);
5642 memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
5643
5644 sp = (void **)id->dev;
5645 id->dev = newdev;
5646 *sp = tofree;
5647 tofree = sp;
5648 }
5649
5650 update->space_list = tofree;
5651 super->updates_pending++;
5652update_reshape_exit:
78b10e66
N
5653 break;
5654 }
e8319a19
DW
5655 case update_activate_spare: {
5656 struct imsm_update_activate_spare *u = (void *) update->buf;
949c47a0 5657 struct imsm_dev *dev = get_imsm_dev(super, u->array);
a965f303 5658 struct imsm_map *map = get_imsm_map(dev, 0);
0c046afd 5659 struct imsm_map *migr_map;
e8319a19
DW
5660 struct active_array *a;
5661 struct imsm_disk *disk;
0c046afd 5662 __u8 to_state;
e8319a19 5663 struct dl *dl;
e8319a19 5664 unsigned int found;
0c046afd
DW
5665 int failed;
5666 int victim = get_imsm_disk_idx(dev, u->slot);
e8319a19
DW
5667 int i;
5668
5669 for (dl = super->disks; dl; dl = dl->next)
d23fe947 5670 if (dl == u->dl)
e8319a19
DW
5671 break;
5672
5673 if (!dl) {
5674 fprintf(stderr, "error: imsm_activate_spare passed "
1f24f035
DW
5675 "an unknown disk (index: %d)\n",
5676 u->dl->index);
e8319a19
DW
5677 return;
5678 }
5679
5680 super->updates_pending++;
5681
0c046afd
DW
5682 /* count failures (excluding rebuilds and the victim)
5683 * to determine map[0] state
5684 */
5685 failed = 0;
5686 for (i = 0; i < map->num_members; i++) {
5687 if (i == u->slot)
5688 continue;
5689 disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
25ed7e59 5690 if (!disk || is_failed(disk))
0c046afd
DW
5691 failed++;
5692 }
5693
d23fe947
DW
5694 /* adding a pristine spare, assign a new index */
5695 if (dl->index < 0) {
5696 dl->index = super->anchor->num_disks;
5697 super->anchor->num_disks++;
5698 }
d23fe947 5699 disk = &dl->disk;
f2f27e63
DW
5700 disk->status |= CONFIGURED_DISK;
5701 disk->status &= ~SPARE_DISK;
e8319a19 5702
0c046afd
DW
5703 /* mark rebuild */
5704 to_state = imsm_check_degraded(super, dev, failed);
5705 map->map_state = IMSM_T_STATE_DEGRADED;
e3bba0e0 5706 migrate(dev, to_state, MIGR_REBUILD);
0c046afd
DW
5707 migr_map = get_imsm_map(dev, 1);
5708 set_imsm_ord_tbl_ent(map, u->slot, dl->index);
5709 set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
5710
148acb7b
DW
5711 /* update the family_num to mark a new container
5712 * generation, being careful to record the existing
5713 * family_num in orig_family_num to clean up after
5714 * earlier mdadm versions that neglected to set it.
5715 */
5716 if (mpb->orig_family_num == 0)
5717 mpb->orig_family_num = mpb->family_num;
5718 mpb->family_num += super->random;
5719
e8319a19
DW
5720 /* count arrays using the victim in the metadata */
5721 found = 0;
5722 for (a = st->arrays; a ; a = a->next) {
949c47a0 5723 dev = get_imsm_dev(super, a->info.container_member);
620b1713
DW
5724 map = get_imsm_map(dev, 0);
5725
5726 if (get_imsm_disk_slot(map, victim) >= 0)
5727 found++;
e8319a19
DW
5728 }
5729
24565c9a 5730 /* delete the victim if it is no longer being
e8319a19
DW
5731 * utilized anywhere
5732 */
e8319a19 5733 if (!found) {
ae6aad82 5734 struct dl **dlp;
24565c9a 5735
47ee5a45
DW
5736 /* We know that 'manager' isn't touching anything,
5737 * so it is safe to delete
5738 */
24565c9a 5739 for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
ae6aad82
DW
5740 if ((*dlp)->index == victim)
5741 break;
47ee5a45
DW
5742
5743 /* victim may be on the missing list */
5744 if (!*dlp)
5745 for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
5746 if ((*dlp)->index == victim)
5747 break;
24565c9a 5748 imsm_delete(super, dlp, victim);
e8319a19 5749 }
8273f55e
DW
5750 break;
5751 }
5752 case update_create_array: {
5753 /* someone wants to create a new array, we need to be aware of
5754 * a few races/collisions:
5755 * 1/ 'Create' called by two separate instances of mdadm
5756 * 2/ 'Create' versus 'activate_spare': mdadm has chosen
5757 * devices that have since been assimilated via
5758 * activate_spare.
5759 * In the event this update can not be carried out mdadm will
5760 * (FIX ME) notice that its update did not take hold.
5761 */
5762 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 5763 struct intel_dev *dv;
8273f55e
DW
5764 struct imsm_dev *dev;
5765 struct imsm_map *map, *new_map;
5766 unsigned long long start, end;
5767 unsigned long long new_start, new_end;
5768 int i;
54c2c1ea
DW
5769 struct disk_info *inf;
5770 struct dl *dl;
8273f55e
DW
5771
5772 /* handle racing creates: first come first serve */
5773 if (u->dev_idx < mpb->num_raid_devs) {
5774 dprintf("%s: subarray %d already defined\n",
5775 __func__, u->dev_idx);
ba2de7ba 5776 goto create_error;
8273f55e
DW
5777 }
5778
5779 /* check update is next in sequence */
5780 if (u->dev_idx != mpb->num_raid_devs) {
6a3e913e
DW
5781 dprintf("%s: can not create array %d expected index %d\n",
5782 __func__, u->dev_idx, mpb->num_raid_devs);
ba2de7ba 5783 goto create_error;
8273f55e
DW
5784 }
5785
a965f303 5786 new_map = get_imsm_map(&u->dev, 0);
8273f55e
DW
5787 new_start = __le32_to_cpu(new_map->pba_of_lba0);
5788 new_end = new_start + __le32_to_cpu(new_map->blocks_per_member);
54c2c1ea 5789 inf = get_disk_info(u);
8273f55e
DW
5790
5791 /* handle activate_spare versus create race:
5792 * check to make sure that overlapping arrays do not include
5793 * overalpping disks
5794 */
5795 for (i = 0; i < mpb->num_raid_devs; i++) {
949c47a0 5796 dev = get_imsm_dev(super, i);
a965f303 5797 map = get_imsm_map(dev, 0);
8273f55e
DW
5798 start = __le32_to_cpu(map->pba_of_lba0);
5799 end = start + __le32_to_cpu(map->blocks_per_member);
5800 if ((new_start >= start && new_start <= end) ||
5801 (start >= new_start && start <= new_end))
54c2c1ea
DW
5802 /* overlap */;
5803 else
5804 continue;
5805
5806 if (disks_overlap(super, i, u)) {
8273f55e 5807 dprintf("%s: arrays overlap\n", __func__);
ba2de7ba 5808 goto create_error;
8273f55e
DW
5809 }
5810 }
8273f55e 5811
949c47a0
DW
5812 /* check that prepare update was successful */
5813 if (!update->space) {
5814 dprintf("%s: prepare update failed\n", __func__);
ba2de7ba 5815 goto create_error;
949c47a0
DW
5816 }
5817
54c2c1ea
DW
5818 /* check that all disks are still active before committing
5819 * changes. FIXME: could we instead handle this by creating a
5820 * degraded array? That's probably not what the user expects,
5821 * so better to drop this update on the floor.
5822 */
5823 for (i = 0; i < new_map->num_members; i++) {
5824 dl = serial_to_dl(inf[i].serial, super);
5825 if (!dl) {
5826 dprintf("%s: disk disappeared\n", __func__);
ba2de7ba 5827 goto create_error;
54c2c1ea 5828 }
949c47a0
DW
5829 }
5830
8273f55e 5831 super->updates_pending++;
54c2c1ea
DW
5832
5833 /* convert spares to members and fixup ord_tbl */
5834 for (i = 0; i < new_map->num_members; i++) {
5835 dl = serial_to_dl(inf[i].serial, super);
5836 if (dl->index == -1) {
5837 dl->index = mpb->num_disks;
5838 mpb->num_disks++;
5839 dl->disk.status |= CONFIGURED_DISK;
5840 dl->disk.status &= ~SPARE_DISK;
5841 }
5842 set_imsm_ord_tbl_ent(new_map, i, dl->index);
5843 }
5844
ba2de7ba
DW
5845 dv = update->space;
5846 dev = dv->dev;
949c47a0
DW
5847 update->space = NULL;
5848 imsm_copy_dev(dev, &u->dev);
ba2de7ba
DW
5849 dv->index = u->dev_idx;
5850 dv->next = super->devlist;
5851 super->devlist = dv;
8273f55e 5852 mpb->num_raid_devs++;
8273f55e 5853
4d1313e9 5854 imsm_update_version_info(super);
8273f55e 5855 break;
ba2de7ba
DW
5856 create_error:
5857 /* mdmon knows how to release update->space, but not
5858 * ((struct intel_dev *) update->space)->dev
5859 */
5860 if (update->space) {
5861 dv = update->space;
5862 free(dv->dev);
5863 }
8273f55e 5864 break;
e8319a19 5865 }
33414a01
DW
5866 case update_kill_array: {
5867 struct imsm_update_kill_array *u = (void *) update->buf;
5868 int victim = u->dev_idx;
5869 struct active_array *a;
5870 struct intel_dev **dp;
5871 struct imsm_dev *dev;
5872
5873 /* sanity check that we are not affecting the uuid of
5874 * active arrays, or deleting an active array
5875 *
5876 * FIXME when immutable ids are available, but note that
5877 * we'll also need to fixup the invalidated/active
5878 * subarray indexes in mdstat
5879 */
5880 for (a = st->arrays; a; a = a->next)
5881 if (a->info.container_member >= victim)
5882 break;
5883 /* by definition if mdmon is running at least one array
5884 * is active in the container, so checking
5885 * mpb->num_raid_devs is just extra paranoia
5886 */
5887 dev = get_imsm_dev(super, victim);
5888 if (a || !dev || mpb->num_raid_devs == 1) {
5889 dprintf("failed to delete subarray-%d\n", victim);
5890 break;
5891 }
5892
5893 for (dp = &super->devlist; *dp;)
f21e18ca 5894 if ((*dp)->index == (unsigned)super->current_vol) {
33414a01
DW
5895 *dp = (*dp)->next;
5896 } else {
f21e18ca 5897 if ((*dp)->index > (unsigned)victim)
33414a01
DW
5898 (*dp)->index--;
5899 dp = &(*dp)->next;
5900 }
5901 mpb->num_raid_devs--;
5902 super->updates_pending++;
5903 break;
5904 }
aa534678
DW
5905 case update_rename_array: {
5906 struct imsm_update_rename_array *u = (void *) update->buf;
5907 char name[MAX_RAID_SERIAL_LEN+1];
5908 int target = u->dev_idx;
5909 struct active_array *a;
5910 struct imsm_dev *dev;
5911
5912 /* sanity check that we are not affecting the uuid of
5913 * an active array
5914 */
5915 snprintf(name, MAX_RAID_SERIAL_LEN, "%s", (char *) u->name);
5916 name[MAX_RAID_SERIAL_LEN] = '\0';
5917 for (a = st->arrays; a; a = a->next)
5918 if (a->info.container_member == target)
5919 break;
5920 dev = get_imsm_dev(super, u->dev_idx);
5921 if (a || !dev || !check_name(super, name, 1)) {
5922 dprintf("failed to rename subarray-%d\n", target);
5923 break;
5924 }
5925
cdbe98cd 5926 snprintf((char *) dev->volume, MAX_RAID_SERIAL_LEN, "%s", name);
aa534678
DW
5927 super->updates_pending++;
5928 break;
5929 }
1a64be56 5930 case update_add_remove_disk: {
43dad3d6 5931 /* we may be able to repair some arrays if disks are
1a64be56
LM
5932 * being added, check teh status of add_remove_disk
5933 * if discs has been added.
5934 */
5935 if (add_remove_disk_update(super)) {
43dad3d6 5936 struct active_array *a;
072b727f
DW
5937
5938 super->updates_pending++;
1a64be56 5939 for (a = st->arrays; a; a = a->next)
43dad3d6
DW
5940 a->check_degraded = 1;
5941 }
43dad3d6 5942 break;
e8319a19 5943 }
1a64be56
LM
5944 default:
5945 fprintf(stderr, "error: unsuported process update type:"
5946 "(type: %d)\n", type);
5947 }
e8319a19 5948}
88758e9d 5949
8273f55e
DW
5950static void imsm_prepare_update(struct supertype *st,
5951 struct metadata_update *update)
5952{
949c47a0 5953 /**
4d7b1503
DW
5954 * Allocate space to hold new disk entries, raid-device entries or a new
5955 * mpb if necessary. The manager synchronously waits for updates to
5956 * complete in the monitor, so new mpb buffers allocated here can be
5957 * integrated by the monitor thread without worrying about live pointers
5958 * in the manager thread.
8273f55e 5959 */
949c47a0 5960 enum imsm_update_type type = *(enum imsm_update_type *) update->buf;
4d7b1503
DW
5961 struct intel_super *super = st->sb;
5962 struct imsm_super *mpb = super->anchor;
5963 size_t buf_len;
5964 size_t len = 0;
949c47a0
DW
5965
5966 switch (type) {
78b10e66 5967 case update_reshape_container_disks: {
d195167d
AK
5968 /* Every raid device in the container is about to
5969 * gain some more devices, and we will enter a
5970 * reconfiguration.
5971 * So each 'imsm_map' will be bigger, and the imsm_vol
5972 * will now hold 2 of them.
5973 * Thus we need new 'struct imsm_dev' allocations sized
5974 * as sizeof_imsm_dev but with more devices in both maps.
5975 */
5976 struct imsm_update_reshape *u = (void *)update->buf;
5977 struct intel_dev *dl;
5978 void **space_tail = (void**)&update->space_list;
5979
5980 dprintf("imsm: imsm_prepare_update() for update_reshape\n");
5981
5982 for (dl = super->devlist; dl; dl = dl->next) {
5983 int size = sizeof_imsm_dev(dl->dev, 1);
5984 void *s;
5985 size += sizeof(__u32) * 2 *
5986 (u->new_raid_disks - u->old_raid_disks);
5987 s = malloc(size);
5988 if (!s)
5989 break;
5990 *space_tail = s;
5991 space_tail = s;
5992 *space_tail = NULL;
5993 }
5994
5995 len = disks_to_mpb_size(u->new_raid_disks);
5996 dprintf("New anchor length is %llu\n", (unsigned long long)len);
78b10e66
N
5997 break;
5998 }
949c47a0
DW
5999 case update_create_array: {
6000 struct imsm_update_create_array *u = (void *) update->buf;
ba2de7ba 6001 struct intel_dev *dv;
54c2c1ea
DW
6002 struct imsm_dev *dev = &u->dev;
6003 struct imsm_map *map = get_imsm_map(dev, 0);
6004 struct dl *dl;
6005 struct disk_info *inf;
6006 int i;
6007 int activate = 0;
949c47a0 6008
54c2c1ea
DW
6009 inf = get_disk_info(u);
6010 len = sizeof_imsm_dev(dev, 1);
ba2de7ba
DW
6011 /* allocate a new super->devlist entry */
6012 dv = malloc(sizeof(*dv));
6013 if (dv) {
6014 dv->dev = malloc(len);
6015 if (dv->dev)
6016 update->space = dv;
6017 else {
6018 free(dv);
6019 update->space = NULL;
6020 }
6021 }
949c47a0 6022
54c2c1ea
DW
6023 /* count how many spares will be converted to members */
6024 for (i = 0; i < map->num_members; i++) {
6025 dl = serial_to_dl(inf[i].serial, super);
6026 if (!dl) {
6027 /* hmm maybe it failed?, nothing we can do about
6028 * it here
6029 */
6030 continue;
6031 }
6032 if (count_memberships(dl, super) == 0)
6033 activate++;
6034 }
6035 len += activate * sizeof(struct imsm_disk);
949c47a0
DW
6036 break;
6037 default:
6038 break;
6039 }
6040 }
8273f55e 6041
4d7b1503
DW
6042 /* check if we need a larger metadata buffer */
6043 if (super->next_buf)
6044 buf_len = super->next_len;
6045 else
6046 buf_len = super->len;
6047
6048 if (__le32_to_cpu(mpb->mpb_size) + len > buf_len) {
6049 /* ok we need a larger buf than what is currently allocated
6050 * if this allocation fails process_update will notice that
6051 * ->next_len is set and ->next_buf is NULL
6052 */
6053 buf_len = ROUND_UP(__le32_to_cpu(mpb->mpb_size) + len, 512);
6054 if (super->next_buf)
6055 free(super->next_buf);
6056
6057 super->next_len = buf_len;
1f45a8ad
DW
6058 if (posix_memalign(&super->next_buf, 512, buf_len) == 0)
6059 memset(super->next_buf, 0, buf_len);
6060 else
4d7b1503
DW
6061 super->next_buf = NULL;
6062 }
8273f55e
DW
6063}
6064
ae6aad82 6065/* must be called while manager is quiesced */
f21e18ca 6066static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index)
ae6aad82
DW
6067{
6068 struct imsm_super *mpb = super->anchor;
ae6aad82
DW
6069 struct dl *iter;
6070 struct imsm_dev *dev;
6071 struct imsm_map *map;
24565c9a
DW
6072 int i, j, num_members;
6073 __u32 ord;
ae6aad82 6074
24565c9a
DW
6075 dprintf("%s: deleting device[%d] from imsm_super\n",
6076 __func__, index);
ae6aad82
DW
6077
6078 /* shift all indexes down one */
6079 for (iter = super->disks; iter; iter = iter->next)
f21e18ca 6080 if (iter->index > (int)index)
ae6aad82 6081 iter->index--;
47ee5a45 6082 for (iter = super->missing; iter; iter = iter->next)
f21e18ca 6083 if (iter->index > (int)index)
47ee5a45 6084 iter->index--;
ae6aad82
DW
6085
6086 for (i = 0; i < mpb->num_raid_devs; i++) {
6087 dev = get_imsm_dev(super, i);
6088 map = get_imsm_map(dev, 0);
24565c9a
DW
6089 num_members = map->num_members;
6090 for (j = 0; j < num_members; j++) {
6091 /* update ord entries being careful not to propagate
6092 * ord-flags to the first map
6093 */
6094 ord = get_imsm_ord_tbl_ent(dev, j);
ae6aad82 6095
24565c9a
DW
6096 if (ord_to_idx(ord) <= index)
6097 continue;
ae6aad82 6098
24565c9a
DW
6099 map = get_imsm_map(dev, 0);
6100 set_imsm_ord_tbl_ent(map, j, ord_to_idx(ord - 1));
6101 map = get_imsm_map(dev, 1);
6102 if (map)
6103 set_imsm_ord_tbl_ent(map, j, ord - 1);
ae6aad82
DW
6104 }
6105 }
6106
6107 mpb->num_disks--;
6108 super->updates_pending++;
24565c9a
DW
6109 if (*dlp) {
6110 struct dl *dl = *dlp;
6111
6112 *dlp = (*dlp)->next;
6113 __free_imsm_disk(dl);
6114 }
ae6aad82 6115}
0e600426 6116#endif /* MDASSEMBLE */
ae6aad82 6117
2cda7640
ML
6118static char disk_by_path[] = "/dev/disk/by-path/";
6119
6120static const char *imsm_get_disk_controller_domain(const char *path)
6121{
6122 struct sys_dev *list, *hba = NULL;
6123 char disk_path[PATH_MAX];
6124 int ahci = 0;
6125 char *dpath = NULL;
6126
6127 list = find_driver_devices("pci", "ahci");
6128 for (hba = list; hba; hba = hba->next)
6129 if (devpath_to_vendor(hba->path) == 0x8086)
6130 break;
6131
6132 if (hba) {
6133 struct stat st;
6134
6135 strncpy(disk_path, disk_by_path, PATH_MAX - 1);
6136 strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
6137 if (stat(disk_path, &st) == 0) {
6138 dpath = devt_to_devpath(st.st_rdev);
6139 if (dpath)
6140 ahci = path_attached_to_hba(dpath, hba->path);
6141 }
6142 }
6143 dprintf("path: %s(%s) hba: %s attached: %d\n",
6144 path, dpath, (hba) ? hba->path : "NULL", ahci);
6145 free_sys_dev(&list);
6146 if (ahci)
6147 return "ahci";
6148 else
6149 return NULL;
6150}
6151
78b10e66
N
6152static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
6153{
6154 char subdev_name[20];
6155 struct mdstat_ent *mdstat;
6156
6157 sprintf(subdev_name, "%d", subdev);
6158 mdstat = mdstat_by_subdev(subdev_name, container);
6159 if (!mdstat)
6160 return -1;
6161
6162 *minor = mdstat->devnum;
6163 free_mdstat(mdstat);
6164 return 0;
6165}
6166
6167static int imsm_reshape_is_allowed_on_container(struct supertype *st,
6168 struct geo_params *geo,
6169 int *old_raid_disks)
6170{
6171 int ret_val = 0;
6172 struct mdinfo *info, *member;
6173 int devices_that_can_grow = 0;
6174
6175 dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
6176 "st->devnum = (%i)\n",
6177 st->devnum);
6178
6179 if (geo->size != -1 ||
6180 geo->level != UnSet ||
6181 geo->layout != UnSet ||
6182 geo->chunksize != 0 ||
6183 geo->raid_disks == UnSet) {
6184 dprintf("imsm: Container operation is allowed for "
6185 "raid disks number change only.\n");
6186 return ret_val;
6187 }
6188
6189 info = container_content_imsm(st, NULL);
6190 for (member = info; member; member = member->next) {
6191 int result;
6192 int minor;
6193
6194 dprintf("imsm: checking device_num: %i\n",
6195 member->container_member);
6196
6197 if (geo->raid_disks < member->array.raid_disks) {
6198 /* we work on container for Online Capacity Expansion
6199 * only so raid_disks has to grow
6200 */
6201 dprintf("imsm: for container operation raid disks "
6202 "increase is required\n");
6203 break;
6204 }
6205
6206 if ((info->array.level != 0) &&
6207 (info->array.level != 5)) {
6208 /* we cannot use this container with other raid level
6209 */
6210 dprintf("imsm: for container operation wrong"\
6211 " raid level (%i) detected\n",
6212 info->array.level);
6213 break;
6214 } else {
6215 /* check for platform support
6216 * for this raid level configuration
6217 */
6218 struct intel_super *super = st->sb;
6219 if (!is_raid_level_supported(super->orom,
6220 member->array.level,
6221 geo->raid_disks)) {
6222 dprintf("platform does not support raid%d with"\
6223 " %d disk%s\n",
6224 info->array.level,
6225 geo->raid_disks,
6226 geo->raid_disks > 1 ? "s" : "");
6227 break;
6228 }
6229 }
6230
6231 if (*old_raid_disks &&
6232 info->array.raid_disks != *old_raid_disks)
6233 break;
6234 *old_raid_disks = info->array.raid_disks;
6235
6236 /* All raid5 and raid0 volumes in container
6237 * have to be ready for Online Capacity Expansion
6238 * so they need to be assembled. We have already
6239 * checked that no recovery etc is happening.
6240 */
6241 result = imsm_find_array_minor_by_subdev(member->container_member,
6242 st->container_dev,
6243 &minor);
6244 if (result < 0) {
6245 dprintf("imsm: cannot find array\n");
6246 break;
6247 }
6248 devices_that_can_grow++;
6249 }
6250 sysfs_free(info);
6251 if (!member && devices_that_can_grow)
6252 ret_val = 1;
6253
6254 if (ret_val)
6255 dprintf("\tContainer operation allowed\n");
6256 else
6257 dprintf("\tError: %i\n", ret_val);
6258
6259 return ret_val;
6260}
6261
6262/* Function: get_spares_for_grow
6263 * Description: Allocates memory and creates list of spare devices
6264 * avaliable in container. Checks if spare drive size is acceptable.
6265 * Parameters: Pointer to the supertype structure
6266 * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
6267 * NULL if fail
6268 */
6269static struct mdinfo *get_spares_for_grow(struct supertype *st)
6270{
6271 dev_t dev = 0;
6272 struct mdinfo *disks, *d, **dp;
6273 unsigned long long min_size = min_acceptable_spare_size_imsm(st);
6274
6275 /* get list of alldisks in container */
6276 disks = getinfo_super_disks_imsm(st);
6277
6278 if (!disks)
6279 return NULL;
6280 /* find spare devices on the list */
6281 dp = &disks->devs;
6282 disks->array.spare_disks = 0;
6283 while (*dp) {
6284 int found = 0;
6285 d = *dp;
6286 if (d->disk.state == 0) {
6287 /* check if size is acceptable */
6288 unsigned long long dev_size;
6289 dev = makedev(d->disk.major,d->disk.minor);
6290 if (min_size &&
6291 dev_size_from_id(dev, &dev_size) &&
6292 dev_size >= min_size) {
6293 dev = 0;
6294 found = 1;
6295 }
6296 }
6297 if (found) {
6298 dp = &d->next;
6299 disks->array.spare_disks++;
6300 } else {
6301 *dp = d->next;
6302 d->next = NULL;
6303 sysfs_free(d);
6304 }
6305 }
6306 return disks;
6307}
6308
6309/******************************************************************************
6310 * function: imsm_create_metadata_update_for_reshape
6311 * Function creates update for whole IMSM container.
6312 *
6313 ******************************************************************************/
6314static int imsm_create_metadata_update_for_reshape(
6315 struct supertype *st,
6316 struct geo_params *geo,
6317 int old_raid_disks,
6318 struct imsm_update_reshape **updatep)
6319{
6320 struct intel_super *super = st->sb;
6321 struct imsm_super *mpb = super->anchor;
6322 int update_memory_size = 0;
6323 struct imsm_update_reshape *u = NULL;
6324 struct mdinfo *spares = NULL;
6325 int i;
6326 int delta_disks = 0;
6327
6328 dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
6329 geo->raid_disks);
6330
6331 delta_disks = geo->raid_disks - old_raid_disks;
6332
6333 /* size of all update data without anchor */
6334 update_memory_size = sizeof(struct imsm_update_reshape);
6335
6336 /* now add space for spare disks that we need to add. */
6337 update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
6338
6339 u = calloc(1, update_memory_size);
6340 if (u == NULL) {
6341 dprintf("error: "
6342 "cannot get memory for imsm_update_reshape update\n");
6343 return 0;
6344 }
6345 u->type = update_reshape_container_disks;
6346 u->old_raid_disks = old_raid_disks;
6347 u->new_raid_disks = geo->raid_disks;
6348
6349 /* now get spare disks list
6350 */
6351 spares = get_spares_for_grow(st);
6352
6353 if (spares == NULL
6354 || delta_disks > spares->array.spare_disks) {
6355 dprintf("imsm: ERROR: Cannot get spare devices.\n");
6356 goto abort;
6357 }
6358
6359 /* we have got spares
6360 * update disk list in imsm_disk list table in anchor
6361 */
6362 dprintf("imsm: %i spares are available.\n\n",
6363 spares->array.spare_disks);
6364
6365 for (i = 0; i < delta_disks; i++) {
6366 struct mdinfo *dev = spares->devs;
6367 struct dl *dl;
6368
6369 u->new_disks[i] = makedev(dev->disk.major,
6370 dev->disk.minor);
6371 dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
6372 dl->index = mpb->num_disks++;
6373 }
6374 /* Now update the metadata so that container_content will find
6375 * the new devices
6376 */
6377 for (i = 0; i < mpb->num_raid_devs; i++) {
6378 int d;
6379 struct imsm_dev *dev = get_imsm_dev(super, i);
6380 struct imsm_map *map = get_imsm_map(dev, 0);
6381 map->num_members = geo->raid_disks;
6382 for (d = 0; d < delta_disks; d++) {
6383 set_imsm_ord_tbl_ent(map, old_raid_disks + d,
6384 mpb->num_disks - delta_disks + d);
6385 }
6386 }
6387
6388abort:
6389 /* free spares
6390 */
6391 sysfs_free(spares);
6392
6393 if (i == delta_disks) {
6394 *updatep = u;
6395 return update_memory_size;
6396 }
6397 free(u);
6398
6399 return 0;
6400}
6401
6402
6403static int imsm_reshape_super(struct supertype *st, long long size, int level,
6404 int layout, int chunksize, int raid_disks,
6405 char *backup, char *dev, int verbouse)
6406{
6407 /* currently we only support increasing the number of devices
6408 * for a container. This increases the number of device for each
6409 * member array. They must all be RAID0 or RAID5.
6410 */
6411
6412 int ret_val = 1;
6413 struct geo_params geo;
6414
6415 dprintf("imsm: reshape_super called.\n");
6416
6417 memset(&geo, sizeof(struct geo_params), 0);
6418
6419 geo.dev_name = dev;
6420 geo.size = size;
6421 geo.level = level;
6422 geo.layout = layout;
6423 geo.chunksize = chunksize;
6424 geo.raid_disks = raid_disks;
6425
6426 dprintf("\tfor level : %i\n", geo.level);
6427 dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
6428
6429 if (experimental() == 0)
6430 return ret_val;
6431
6432 /* verify reshape conditions
6433 * on container level we can only increase number of devices. */
6434 if (st->container_dev == st->devnum) {
6435 /* check for delta_disks > 0
6436 *and supported raid levels 0 and 5 only in container */
6437 int old_raid_disks = 0;
6438 if (imsm_reshape_is_allowed_on_container(
6439 st, &geo, &old_raid_disks)) {
6440 struct imsm_update_reshape *u = NULL;
6441 int len;
6442
6443 len = imsm_create_metadata_update_for_reshape(
6444 st, &geo, old_raid_disks, &u);
6445
6446 if (len) {
6447 ret_val = 0;
6448 append_metadata_update(st, u, len);
6449 } else
6450 dprintf("imsm: Cannot prepare "\
6451 "update\n");
6452 } else
6453 dprintf("imsm: Operation is not allowed "\
6454 "on this container\n");
6455 } else
6456 dprintf("imsm: not a container operation\n");
6457
6458 dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
6459 return ret_val;
6460}
2cda7640 6461
cdddbdbc
DW
6462struct superswitch super_imsm = {
6463#ifndef MDASSEMBLE
6464 .examine_super = examine_super_imsm,
6465 .brief_examine_super = brief_examine_super_imsm,
4737ae25 6466 .brief_examine_subarrays = brief_examine_subarrays_imsm,
9d84c8ea 6467 .export_examine_super = export_examine_super_imsm,
cdddbdbc
DW
6468 .detail_super = detail_super_imsm,
6469 .brief_detail_super = brief_detail_super_imsm,
bf5a934a 6470 .write_init_super = write_init_super_imsm,
0e600426
N
6471 .validate_geometry = validate_geometry_imsm,
6472 .add_to_super = add_to_super_imsm,
1a64be56 6473 .remove_from_super = remove_from_super_imsm,
d665cc31 6474 .detail_platform = detail_platform_imsm,
33414a01 6475 .kill_subarray = kill_subarray_imsm,
aa534678 6476 .update_subarray = update_subarray_imsm,
2b959fbf 6477 .load_container = load_container_imsm,
cdddbdbc
DW
6478#endif
6479 .match_home = match_home_imsm,
6480 .uuid_from_super= uuid_from_super_imsm,
6481 .getinfo_super = getinfo_super_imsm,
5c4cd5da 6482 .getinfo_super_disks = getinfo_super_disks_imsm,
cdddbdbc
DW
6483 .update_super = update_super_imsm,
6484
6485 .avail_size = avail_size_imsm,
80e7f8c3 6486 .min_acceptable_spare_size = min_acceptable_spare_size_imsm,
cdddbdbc
DW
6487
6488 .compare_super = compare_super_imsm,
6489
6490 .load_super = load_super_imsm,
bf5a934a 6491 .init_super = init_super_imsm,
e683ca88 6492 .store_super = store_super_imsm,
cdddbdbc
DW
6493 .free_super = free_super_imsm,
6494 .match_metadata_desc = match_metadata_desc_imsm,
bf5a934a 6495 .container_content = container_content_imsm,
30f58b22 6496 .default_geometry = default_geometry_imsm,
2cda7640 6497 .get_disk_controller_domain = imsm_get_disk_controller_domain,
78b10e66 6498 .reshape_super = imsm_reshape_super,
cdddbdbc 6499
cdddbdbc 6500 .external = 1,
4cce4069 6501 .name = "imsm",
845dea95 6502
0e600426 6503#ifndef MDASSEMBLE
845dea95
NB
6504/* for mdmon */
6505 .open_new = imsm_open_new,
ed9d66aa 6506 .set_array_state= imsm_set_array_state,
845dea95
NB
6507 .set_disk = imsm_set_disk,
6508 .sync_metadata = imsm_sync_metadata,
88758e9d 6509 .activate_spare = imsm_activate_spare,
e8319a19 6510 .process_update = imsm_process_update,
8273f55e 6511 .prepare_update = imsm_prepare_update,
0e600426 6512#endif /* MDASSEMBLE */
cdddbdbc 6513};